AliasDebugger.cpp revision d7d83db5f22d05e5b14b6b1d838668222113c83a
1//===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file was developed by Andrew Lenharth and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This simple pass checks alias analysis users to ensure that if they
11// create a new value, they do not query AA without informing it of the value.
12// It acts as a shim over any other AA pass you want.
13//
14// Yes keeping track of every value in the program is expensive, but this is
15// a debugging pass.
16//
17//===----------------------------------------------------------------------===//
18
19#include "llvm/Analysis/Passes.h"
20#include "llvm/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Instructions.h"
23#include "llvm/Constants.h"
24#include "llvm/DerivedTypes.h"
25#include "llvm/Analysis/AliasAnalysis.h"
26#include "llvm/Support/Compiler.h"
27#include <set>
28using namespace llvm;
29
30namespace {
31
32  class VISIBILITY_HIDDEN AliasDebugger
33      : public ModulePass, public AliasAnalysis {
34
35    //What we do is simple.  Keep track of every value the AA could
36    //know about, and verify that queries are one of those.
37    //A query to a value that didn't exist when the AA was created
38    //means someone forgot to update the AA when creating new values
39
40    std::set<const Value*> Vals;
41
42  public:
43    bool runOnModule(Module &M) {
44      InitializeAliasAnalysis(this);                 // set up super class
45
46      for(Module::global_iterator I = M.global_begin(),
47            E = M.global_end(); I != E; ++I)
48        Vals.insert(&*I);
49
50      for(Module::iterator I = M.begin(),
51            E = M.end(); I != E; ++I){
52        Vals.insert(&*I);
53        if(!I->isDeclaration()) {
54          for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
55               AI != AE; ++AI)
56            Vals.insert(&*AI);
57          for (Function::const_iterator FI = I->begin(), FE = I->end();
58               FI != FE; ++FI)
59            for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
60                 BI != BE; ++BI)
61              Vals.insert(&*BI);
62        }
63
64      }
65      return false;
66    }
67
68    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
69      AliasAnalysis::getAnalysisUsage(AU);
70      AU.setPreservesAll();                         // Does not transform code
71    }
72
73    //------------------------------------------------
74    // Implement the AliasAnalysis API
75    //
76    AliasResult alias(const Value *V1, unsigned V1Size,
77                      const Value *V2, unsigned V2Size) {
78      assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
79      assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");
80      return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
81    }
82
83    ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
84      assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
85      return AliasAnalysis::getModRefInfo(CS, P, Size);
86    }
87
88    ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
89      return AliasAnalysis::getModRefInfo(CS1,CS2);
90    }
91
92    void getMustAliases(Value *P, std::vector<Value*> &RetVals) {
93      assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
94      return AliasAnalysis::getMustAliases(P, RetVals);
95    }
96
97    bool pointsToConstantMemory(const Value *P) {
98      assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
99      return AliasAnalysis::pointsToConstantMemory(P);
100    }
101
102    /// getModRefBehavior - Return the behavior of the specified function if
103    /// called from the specified call site.  The call site may be null in which
104    /// case the most generic behavior of this function should be returned.
105    virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
106                                         std::vector<PointerAccessInfo> *Info) {
107      assert(Vals.find(F) != Vals.end() && "Never seen value in AA before");
108      return AliasAnalysis::getModRefBehavior(F, CS, Info);
109    }
110
111    virtual void deleteValue(Value *V) {
112      assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
113      AliasAnalysis::deleteValue(V);
114    }
115    virtual void copyValue(Value *From, Value *To) {
116      Vals.insert(To);
117      AliasAnalysis::copyValue(From, To);
118    }
119
120  };
121
122  RegisterPass<AliasDebugger> X("debug-aa", "AA use debugger");
123  RegisterAnalysisGroup<AliasAnalysis> Y(X);
124}
125
126Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
127
128