Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 1 | //===- AliasDebugger.cpp - Simple Alias Analysis Use Checker --------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 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" |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Compiler.h" |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 27 | #include <set> |
| 28 | using namespace llvm; |
| 29 | |
| 30 | namespace { |
| 31 | |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 32 | class VISIBILITY_HIDDEN AliasDebugger |
| 33 | : public ModulePass, public AliasAnalysis { |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 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: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 43 | static char ID; // Class identification, replacement for typeinfo |
Devang Patel | c758209 | 2008-03-19 21:56:59 +0000 | [diff] [blame] | 44 | AliasDebugger() : ModulePass((intptr_t)&ID) {} |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 45 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 46 | bool runOnModule(Module &M) { |
| 47 | InitializeAliasAnalysis(this); // set up super class |
| 48 | |
| 49 | for(Module::global_iterator I = M.global_begin(), |
| 50 | E = M.global_end(); I != E; ++I) |
| 51 | Vals.insert(&*I); |
| 52 | |
| 53 | for(Module::iterator I = M.begin(), |
| 54 | E = M.end(); I != E; ++I){ |
| 55 | Vals.insert(&*I); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 56 | if(!I->isDeclaration()) { |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 57 | for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end(); |
| 58 | AI != AE; ++AI) |
| 59 | Vals.insert(&*AI); |
| 60 | for (Function::const_iterator FI = I->begin(), FE = I->end(); |
| 61 | FI != FE; ++FI) |
| 62 | for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); |
| 63 | BI != BE; ++BI) |
| 64 | Vals.insert(&*BI); |
| 65 | } |
| 66 | |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 72 | AliasAnalysis::getAnalysisUsage(AU); |
| 73 | AU.setPreservesAll(); // Does not transform code |
| 74 | } |
| 75 | |
| 76 | //------------------------------------------------ |
| 77 | // Implement the AliasAnalysis API |
| 78 | // |
| 79 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 80 | const Value *V2, unsigned V2Size) { |
| 81 | assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before"); |
| 82 | assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before"); |
| 83 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
| 84 | } |
| 85 | |
| 86 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 87 | assert(Vals.find(P) != Vals.end() && "Never seen value in AA before"); |
| 88 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 89 | } |
| 90 | |
| 91 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 92 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 93 | } |
| 94 | |
| 95 | void getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
| 96 | assert(Vals.find(P) != Vals.end() && "Never seen value in AA before"); |
| 97 | return AliasAnalysis::getMustAliases(P, RetVals); |
| 98 | } |
| 99 | |
| 100 | bool pointsToConstantMemory(const Value *P) { |
| 101 | assert(Vals.find(P) != Vals.end() && "Never seen value in AA before"); |
| 102 | return AliasAnalysis::pointsToConstantMemory(P); |
| 103 | } |
| 104 | |
| 105 | /// getModRefBehavior - Return the behavior of the specified function if |
| 106 | /// called from the specified call site. The call site may be null in which |
| 107 | /// case the most generic behavior of this function should be returned. |
| 108 | virtual ModRefBehavior getModRefBehavior(Function *F, CallSite CS, |
| 109 | std::vector<PointerAccessInfo> *Info) { |
| 110 | assert(Vals.find(F) != Vals.end() && "Never seen value in AA before"); |
| 111 | return AliasAnalysis::getModRefBehavior(F, CS, Info); |
| 112 | } |
| 113 | |
| 114 | virtual void deleteValue(Value *V) { |
| 115 | assert(Vals.find(V) != Vals.end() && "Never seen value in AA before"); |
| 116 | AliasAnalysis::deleteValue(V); |
| 117 | } |
| 118 | virtual void copyValue(Value *From, Value *To) { |
| 119 | Vals.insert(To); |
| 120 | AliasAnalysis::copyValue(From, To); |
| 121 | } |
| 122 | |
| 123 | }; |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame^] | 126 | char AliasDebugger::ID = 0; |
| 127 | static RegisterPass<AliasDebugger> |
| 128 | X("debug-aa", "AA use debugger", false, true); |
| 129 | static RegisterAnalysisGroup<AliasAnalysis> Y(X); |
| 130 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 131 | Pass *llvm::createAliasDebugger() { return new AliasDebugger(); } |
| 132 | |