1 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file was developed by the LLVM research group and is distributed under 6 // the University of Illinois Open Source License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements the generic AliasAnalysis interface which is used as the 11 // common interface used by all clients and implementations of alias analysis. 12 // 13 // This file also implements the default version of the AliasAnalysis interface 14 // that is to be used when no other implementation is specified. This does some 15 // simple tests that detect obvious cases: two different global pointers cannot 16 // alias, a global cannot alias a malloc, two different mallocs cannot alias, 17 // etc. 18 // 19 // This alias analysis implementation really isn't very good for anything, but 20 // it is very fast, and makes a nice clean default implementation. Because it 21 // handles lots of little corner cases, other, more complex, alias analysis 22 // implementations may choose to rely on this pass to resolve these simple and 23 // easy cases. 24 // 25 //===----------------------------------------------------------------------===// 26 27 #include "llvm/Analysis/AliasAnalysis.h" 28 #include "llvm/BasicBlock.h" 29 #include "llvm/Instructions.h" 30 #include "llvm/Type.h" 31 #include "llvm/Target/TargetData.h" 32 #include <iostream> 33 using namespace llvm; 34 35 // Register the AliasAnalysis interface, providing a nice name to refer to. 36 namespace { 37 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); 38 } 39 40 //===----------------------------------------------------------------------===// 41 // Default chaining methods 42 //===----------------------------------------------------------------------===// 43 44 AliasAnalysis::AliasResult 45 AliasAnalysis::alias(const Value *V1, unsigned V1Size, 46 const Value *V2, unsigned V2Size) { 47 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 48 return AA->alias(V1, V1Size, V2, V2Size); 49 } 50 51 void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) { 52 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 53 return AA->getMustAliases(P, RetVals); 54 } 55 56 bool AliasAnalysis::pointsToConstantMemory(const Value *P) { 57 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 58 return AA->pointsToConstantMemory(P); 59 } 60 61 AliasAnalysis::ModRefBehavior 62 AliasAnalysis::getModRefBehavior(Function *F, CallSite CS, 63 std::vector<PointerAccessInfo> *Info) { 64 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 65 return AA->getModRefBehavior(F, CS, Info); 66 } 67 68 bool AliasAnalysis::hasNoModRefInfoForCalls() const { 69 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 70 return AA->hasNoModRefInfoForCalls(); 71 } 72 73 void AliasAnalysis::deleteValue(Value *V) { 74 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 75 AA->deleteValue(V); 76 } 77 78 void AliasAnalysis::copyValue(Value *From, Value *To) { 79 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 80 AA->copyValue(From, To); 81 } 82 83 AliasAnalysis::ModRefResult 84 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { 85 // FIXME: we can do better. 86 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 87 return AA->getModRefInfo(CS1, CS2); 88 } 89 90 91 //===----------------------------------------------------------------------===// 92 // AliasAnalysis non-virtual helper method implementation 93 //===----------------------------------------------------------------------===// 94 95 AliasAnalysis::ModRefResult 96 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { 97 return alias(L->getOperand(0), TD->getTypeSize(L->getType()), 98 P, Size) ? Ref : NoModRef; 99 } 100 101 AliasAnalysis::ModRefResult 102 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { 103 // If the stored address cannot alias the pointer in question, then the 104 // pointer cannot be modified by the store. 105 if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), 106 P, Size)) 107 return NoModRef; 108 109 // If the pointer is a pointer to constant memory, then it could not have been 110 // modified by this store. 111 return pointsToConstantMemory(P) ? NoModRef : Mod; 112 } 113 114 AliasAnalysis::ModRefResult 115 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 116 ModRefResult Mask = ModRef; 117 if (Function *F = CS.getCalledFunction()) { 118 ModRefBehavior MRB = getModRefBehavior(F, CallSite()); 119 if (MRB == OnlyReadsMemory) 120 Mask = Ref; 121 else if (MRB == DoesNotAccessMemory) 122 return NoModRef; 123 } 124 125 if (!AA) return Mask; 126 127 // If P points to a constant memory location, the call definitely could not 128 // modify the memory location. 129 if ((Mask & Mod) && AA->pointsToConstantMemory(P)) 130 Mask = ModRefResult(Mask & ~Mod); 131 132 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); 133 } 134 135 // AliasAnalysis destructor: DO NOT move this to the header file for 136 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on 137 // the AliasAnalysis.o file in the current .a file, causing alias analysis 138 // support to not be included in the tool correctly! 139 // 140 AliasAnalysis::~AliasAnalysis() {} 141 142 /// setTargetData - Subclasses must call this method to initialize the 143 /// AliasAnalysis interface before any other methods are called. 144 /// 145 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { 146 TD = &P->getAnalysis<TargetData>(); 147 AA = &P->getAnalysis<AliasAnalysis>(); 148 } 149 150 // getAnalysisUsage - All alias analysis implementations should invoke this 151 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that 152 // TargetData is required by the pass. 153 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 154 AU.addRequired<TargetData>(); // All AA's need TargetData. 155 AU.addRequired<AliasAnalysis>(); // All AA's chain 156 } 157 158 /// canBasicBlockModify - Return true if it is possible for execution of the 159 /// specified basic block to modify the value pointed to by Ptr. 160 /// 161 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, 162 const Value *Ptr, unsigned Size) { 163 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); 164 } 165 166 /// canInstructionRangeModify - Return true if it is possible for the execution 167 /// of the specified instructions to modify the value pointed to by Ptr. The 168 /// instructions to consider are all of the instructions in the range of [I1,I2] 169 /// INCLUSIVE. I1 and I2 must be in the same basic block. 170 /// 171 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, 172 const Instruction &I2, 173 const Value *Ptr, unsigned Size) { 174 assert(I1.getParent() == I2.getParent() && 175 "Instructions not in same basic block!"); 176 BasicBlock::iterator I = const_cast<Instruction*>(&I1); 177 BasicBlock::iterator E = const_cast<Instruction*>(&I2); 178 ++E; // Convert from inclusive to exclusive range. 179 180 for (; I != E; ++I) // Check every instruction in range 181 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) 182 return true; 183 return false; 184 } 185 186 // Because of the way .a files work, we must force the BasicAA implementation to 187 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run 188 // the risk of AliasAnalysis being used, but the default implementation not 189 // being linked into the tool that uses it. 190 // 191 namespace llvm { 192 extern void BasicAAStub(); 193 } 194 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub); 195