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" |
| 26 | #include <set> |
| 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 31 | class AliasDebugger : public ModulePass, public AliasAnalysis { |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 32 | |
| 33 | //What we do is simple. Keep track of every value the AA could |
| 34 | //know about, and verify that queries are one of those. |
| 35 | //A query to a value that didn't exist when the AA was created |
| 36 | //means someone forgot to update the AA when creating new values |
| 37 | |
| 38 | std::set<const Value*> Vals; |
| 39 | |
| 40 | public: |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 41 | static char ID; // Class identification, replacement for typeinfo |
Dan Gohman | ae73dc1 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 42 | AliasDebugger() : ModulePass(&ID) {} |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 43 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 44 | bool runOnModule(Module &M) { |
| 45 | InitializeAliasAnalysis(this); // set up super class |
| 46 | |
| 47 | for(Module::global_iterator I = M.global_begin(), |
| 48 | E = M.global_end(); I != E; ++I) |
| 49 | Vals.insert(&*I); |
| 50 | |
| 51 | for(Module::iterator I = M.begin(), |
| 52 | E = M.end(); I != E; ++I){ |
| 53 | Vals.insert(&*I); |
Reid Spencer | 5cbf985 | 2007-01-30 20:08:39 +0000 | [diff] [blame] | 54 | if(!I->isDeclaration()) { |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 55 | for (Function::arg_iterator AI = I->arg_begin(), AE = I->arg_end(); |
| 56 | AI != AE; ++AI) |
| 57 | Vals.insert(&*AI); |
| 58 | for (Function::const_iterator FI = I->begin(), FE = I->end(); |
| 59 | FI != FE; ++FI) |
| 60 | for (BasicBlock::const_iterator BI = FI->begin(), BE = FI->end(); |
| 61 | BI != BE; ++BI) |
| 62 | Vals.insert(&*BI); |
| 63 | } |
| 64 | |
| 65 | } |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 70 | AliasAnalysis::getAnalysisUsage(AU); |
| 71 | AU.setPreservesAll(); // Does not transform code |
| 72 | } |
| 73 | |
Chris Lattner | 1bc76d4 | 2010-01-20 20:09:02 +0000 | [diff] [blame^] | 74 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 75 | /// an analysis interface through multiple inheritance. If needed, it |
| 76 | /// should override this to adjust the this pointer as needed for the |
| 77 | /// specified pass info. |
| 78 | virtual void *getAdjustedAnalysisPointer(const PassInfo *PI) { |
| 79 | if (PI->isPassID(&AliasAnalysis::ID)) |
| 80 | return (AliasAnalysis*)this; |
| 81 | return this; |
| 82 | } |
| 83 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 84 | //------------------------------------------------ |
| 85 | // Implement the AliasAnalysis API |
| 86 | // |
| 87 | AliasResult alias(const Value *V1, unsigned V1Size, |
| 88 | const Value *V2, unsigned V2Size) { |
| 89 | assert(Vals.find(V1) != Vals.end() && "Never seen value in AA before"); |
| 90 | assert(Vals.find(V2) != Vals.end() && "Never seen value in AA before"); |
| 91 | return AliasAnalysis::alias(V1, V1Size, V2, V2Size); |
| 92 | } |
| 93 | |
| 94 | ModRefResult getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 95 | assert(Vals.find(P) != Vals.end() && "Never seen value in AA before"); |
| 96 | return AliasAnalysis::getModRefInfo(CS, P, Size); |
| 97 | } |
| 98 | |
| 99 | ModRefResult getModRefInfo(CallSite CS1, CallSite CS2) { |
| 100 | return AliasAnalysis::getModRefInfo(CS1,CS2); |
| 101 | } |
| 102 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 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 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 108 | virtual void deleteValue(Value *V) { |
| 109 | assert(Vals.find(V) != Vals.end() && "Never seen value in AA before"); |
| 110 | AliasAnalysis::deleteValue(V); |
| 111 | } |
| 112 | virtual void copyValue(Value *From, Value *To) { |
| 113 | Vals.insert(To); |
| 114 | AliasAnalysis::copyValue(From, To); |
| 115 | } |
| 116 | |
| 117 | }; |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 118 | } |
| 119 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 120 | char AliasDebugger::ID = 0; |
| 121 | static RegisterPass<AliasDebugger> |
| 122 | X("debug-aa", "AA use debugger", false, true); |
| 123 | static RegisterAnalysisGroup<AliasAnalysis> Y(X); |
| 124 | |
Andrew Lenharth | 472c791 | 2006-11-14 05:21:04 +0000 | [diff] [blame] | 125 | Pass *llvm::createAliasDebugger() { return new AliasDebugger(); } |
| 126 | |