6b534e94be17d7ae46695d031d2b5a5f4ada1598
[oota-llvm.git] / lib / Analysis / AliasDebugger.cpp
1 //===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // 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>
28 using namespace llvm;
29
30 namespace {
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     static char ID; // Class identification, replacement for typeinfo
44     AliasDebugger() : ModulePass((intptr_t)&ID) {}
45
46     /// isAnalysis - Return true if this pass is  implementing an analysis pass.
47     virtual bool isAnalysis() const { return true; }
48
49     bool runOnModule(Module &M) {
50       InitializeAliasAnalysis(this);                 // set up super class
51
52       for(Module::global_iterator I = M.global_begin(),
53             E = M.global_end(); I != E; ++I)
54         Vals.insert(&*I);
55
56       for(Module::iterator I = M.begin(),
57             E = M.end(); I != E; ++I){
58         Vals.insert(&*I);
59         if(!I->isDeclaration()) {
60           for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end();
61                AI != AE; ++AI) 
62             Vals.insert(&*AI);     
63           for (Function::const_iterator FI = I->begin(), FE = I->end();
64                FI != FE; ++FI) 
65             for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end();
66                  BI != BE; ++BI)
67               Vals.insert(&*BI);
68         }
69         
70       }
71       return false;
72     }
73
74     virtual void getAnalysisUsage(AnalysisUsage &AU) const {
75       AliasAnalysis::getAnalysisUsage(AU);
76       AU.setPreservesAll();                         // Does not transform code
77     }
78
79     //------------------------------------------------
80     // Implement the AliasAnalysis API
81     //
82     AliasResult alias(const Value *V1, unsigned V1Size,
83                       const Value *V2, unsigned V2Size) {
84       assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before");
85       assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before");    
86       return AliasAnalysis::alias(V1, V1Size, V2, V2Size);
87     }
88
89     ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) {
90       assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
91       return AliasAnalysis::getModRefInfo(CS, P, Size);
92     }
93
94     ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) {
95       return AliasAnalysis::getModRefInfo(CS1,CS2);
96     }
97     
98     void getMustAliases(Value *P, std::vector<Value*> &RetVals) {
99       assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
100       return AliasAnalysis::getMustAliases(P, RetVals);
101     }
102
103     bool pointsToConstantMemory(const Value *P) {
104       assert(Vals.find(P) != Vals.end() && "Never seen value in AA before");
105       return AliasAnalysis::pointsToConstantMemory(P);
106     }
107
108     /// getModRefBehavior - Return the behavior of the specified function if
109     /// called from the specified call site.  The call site may be null in which
110     /// case the most generic behavior of this function should be returned.
111     virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS,
112                                          std::vector<PointerAccessInfo> *Info) {
113       assert(Vals.find(F) != Vals.end() && "Never seen value in AA before");
114       return AliasAnalysis::getModRefBehavior(F, CS, Info);
115     }
116
117     virtual void deleteValue(Value *V) {
118       assert(Vals.find(V) != Vals.end() && "Never seen value in AA before");
119       AliasAnalysis::deleteValue(V);
120     }
121     virtual void copyValue(Value *From, Value *To) {
122       Vals.insert(To);
123       AliasAnalysis::copyValue(From, To);
124     }
125
126   };
127
128   char AliasDebugger::ID = 0;
129   RegisterPass<AliasDebugger> X("debug-aa", "AA use debugger");
130   RegisterAnalysisGroup<AliasAnalysis> Y(X);
131 }
132
133 Pass *llvm::createAliasDebugger() { return new AliasDebugger(); }
134