1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// 2 // 3 // This file implements the generic AliasAnalysis interface which is used as the 4 // common interface used by all clients and implementations of alias analysis. 5 // 6 // This file also implements the default version of the AliasAnalysis interface 7 // that is to be used when no other implementation is specified. This does some 8 // simple tests that detect obvious cases: two different global pointers cannot 9 // alias, a global cannot alias a malloc, two different mallocs cannot alias, 10 // etc. 11 // 12 // This alias analysis implementation really isn't very good for anything, but 13 // it is very fast, and makes a nice clean default implementation. Because it 14 // handles lots of little corner cases, other, more complex, alias analysis 15 // implementations may choose to rely on this pass to resolve these simple and 16 // easy cases. 17 // 18 //===----------------------------------------------------------------------===// 19 20 #include "llvm/Analysis/AliasAnalysis.h" 21 #include "llvm/BasicBlock.h" 22 #include "llvm/iMemory.h" 23 #include "llvm/Target/TargetData.h" 24 25 // Register the AliasAnalysis interface, providing a nice name to refer to. 26 namespace { 27 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); 28 } 29 30 AliasAnalysis::ModRefResult 31 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { 32 return alias(L->getOperand(0), TD->getTypeSize(L->getType()), 33 P, Size) ? Ref : NoModRef; 34 } 35 36 AliasAnalysis::ModRefResult 37 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { 38 return alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), 39 P, Size) ? Mod : NoModRef; 40 } 41 42 43 // AliasAnalysis destructor: DO NOT move this to the header file for 44 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on 45 // the AliasAnalysis.o file in the current .a file, causing alias analysis 46 // support to not be included in the tool correctly! 47 // 48 AliasAnalysis::~AliasAnalysis() {} 49 50 /// setTargetData - Subclasses must call this method to initialize the 51 /// AliasAnalysis interface before any other methods are called. 52 /// 53 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { 54 TD = &P->getAnalysis<TargetData>(); 55 } 56 57 // getAnalysisUsage - All alias analysis implementations should invoke this 58 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that 59 // TargetData is required by the pass. 60 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 61 AU.addRequired<TargetData>(); // All AA's need TargetData. 62 } 63 64 /// canBasicBlockModify - Return true if it is possible for execution of the 65 /// specified basic block to modify the value pointed to by Ptr. 66 /// 67 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, 68 const Value *Ptr, unsigned Size) { 69 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); 70 } 71 72 /// canInstructionRangeModify - Return true if it is possible for the execution 73 /// of the specified instructions to modify the value pointed to by Ptr. The 74 /// instructions to consider are all of the instructions in the range of [I1,I2] 75 /// INCLUSIVE. I1 and I2 must be in the same basic block. 76 /// 77 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, 78 const Instruction &I2, 79 const Value *Ptr, unsigned Size) { 80 assert(I1.getParent() == I2.getParent() && 81 "Instructions not in same basic block!"); 82 BasicBlock::iterator I = const_cast<Instruction*>(&I1); 83 BasicBlock::iterator E = const_cast<Instruction*>(&I2); 84 ++E; // Convert from inclusive to exclusive range. 85 86 for (; I != E; ++I) // Check every instruction in range 87 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) 88 return true; 89 return false; 90 } 91 92 // Because of the way .a files work, we must force the BasicAA implementation to 93 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run 94 // the risk of AliasAnalysis being used, but the default implementation not 95 // being linked into the tool that uses it. 96 // 97 extern void BasicAAStub(); 98 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub); 99 100 101 namespace { 102 struct NoAA : public ImmutablePass, public AliasAnalysis { 103 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 104 AliasAnalysis::getAnalysisUsage(AU); 105 } 106 107 virtual void initializePass() { 108 InitializeAliasAnalysis(this); 109 } 110 }; 111 112 // Register this pass... 113 RegisterOpt<NoAA> 114 X("no-aa", "No Alias Analysis (always returns 'may' alias)"); 115 116 // Declare that we implement the AliasAnalysis interface 117 RegisterAnalysisGroup<AliasAnalysis, NoAA> Y; 118 } // End of anonymous namespace 119