LLVM API Documentation
00001 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the generic AliasAnalysis interface which is used as the 00011 // common interface used by all clients and implementations of alias analysis. 00012 // 00013 // This file also implements the default version of the AliasAnalysis interface 00014 // that is to be used when no other implementation is specified. This does some 00015 // simple tests that detect obvious cases: two different global pointers cannot 00016 // alias, a global cannot alias a malloc, two different mallocs cannot alias, 00017 // etc. 00018 // 00019 // This alias analysis implementation really isn't very good for anything, but 00020 // it is very fast, and makes a nice clean default implementation. Because it 00021 // handles lots of little corner cases, other, more complex, alias analysis 00022 // implementations may choose to rely on this pass to resolve these simple and 00023 // easy cases. 00024 // 00025 //===----------------------------------------------------------------------===// 00026 00027 #include "llvm/Analysis/AliasAnalysis.h" 00028 #include "llvm/Pass.h" 00029 #include "llvm/BasicBlock.h" 00030 #include "llvm/Function.h" 00031 #include "llvm/IntrinsicInst.h" 00032 #include "llvm/Instructions.h" 00033 #include "llvm/Type.h" 00034 #include "llvm/Target/TargetData.h" 00035 using namespace llvm; 00036 00037 // Register the AliasAnalysis interface, providing a nice name to refer to. 00038 static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); 00039 char AliasAnalysis::ID = 0; 00040 00041 //===----------------------------------------------------------------------===// 00042 // Default chaining methods 00043 //===----------------------------------------------------------------------===// 00044 00045 AliasAnalysis::AliasResult 00046 AliasAnalysis::alias(const Value *V1, unsigned V1Size, 00047 const Value *V2, unsigned V2Size) { 00048 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00049 return AA->alias(V1, V1Size, V2, V2Size); 00050 } 00051 00052 void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) { 00053 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00054 return AA->getMustAliases(P, RetVals); 00055 } 00056 00057 bool AliasAnalysis::pointsToConstantMemory(const Value *P) { 00058 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00059 return AA->pointsToConstantMemory(P); 00060 } 00061 00062 bool AliasAnalysis::hasNoModRefInfoForCalls() const { 00063 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00064 return AA->hasNoModRefInfoForCalls(); 00065 } 00066 00067 void AliasAnalysis::deleteValue(Value *V) { 00068 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00069 AA->deleteValue(V); 00070 } 00071 00072 void AliasAnalysis::copyValue(Value *From, Value *To) { 00073 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00074 AA->copyValue(From, To); 00075 } 00076 00077 AliasAnalysis::ModRefResult 00078 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { 00079 // FIXME: we can do better. 00080 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00081 return AA->getModRefInfo(CS1, CS2); 00082 } 00083 00084 00085 //===----------------------------------------------------------------------===// 00086 // AliasAnalysis non-virtual helper method implementation 00087 //===----------------------------------------------------------------------===// 00088 00089 AliasAnalysis::ModRefResult 00090 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { 00091 return alias(L->getOperand(0), getTypeStoreSize(L->getType()), 00092 P, Size) ? Ref : NoModRef; 00093 } 00094 00095 AliasAnalysis::ModRefResult 00096 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { 00097 // If the stored address cannot alias the pointer in question, then the 00098 // pointer cannot be modified by the store. 00099 if (!alias(S->getOperand(1), 00100 getTypeStoreSize(S->getOperand(0)->getType()), P, Size)) 00101 return NoModRef; 00102 00103 // If the pointer is a pointer to constant memory, then it could not have been 00104 // modified by this store. 00105 return pointsToConstantMemory(P) ? NoModRef : Mod; 00106 } 00107 00108 AliasAnalysis::ModRefBehavior 00109 AliasAnalysis::getModRefBehavior(CallSite CS, 00110 std::vector<PointerAccessInfo> *Info) { 00111 if (CS.doesNotAccessMemory()) 00112 // Can't do better than this. 00113 return DoesNotAccessMemory; 00114 ModRefBehavior MRB = getModRefBehavior(CS.getCalledFunction(), Info); 00115 if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory()) 00116 return OnlyReadsMemory; 00117 return MRB; 00118 } 00119 00120 AliasAnalysis::ModRefBehavior 00121 AliasAnalysis::getModRefBehavior(Function *F, 00122 std::vector<PointerAccessInfo> *Info) { 00123 if (F) { 00124 if (F->doesNotAccessMemory()) 00125 // Can't do better than this. 00126 return DoesNotAccessMemory; 00127 if (F->onlyReadsMemory()) 00128 return OnlyReadsMemory; 00129 if (unsigned id = F->getIntrinsicID()) { 00130 #define GET_INTRINSIC_MODREF_BEHAVIOR 00131 #include "llvm/Intrinsics.gen" 00132 #undef GET_INTRINSIC_MODREF_BEHAVIOR 00133 } 00134 } 00135 return UnknownModRefBehavior; 00136 } 00137 00138 AliasAnalysis::ModRefResult 00139 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 00140 ModRefResult Mask = ModRef; 00141 ModRefBehavior MRB = getModRefBehavior(CS); 00142 if (MRB == DoesNotAccessMemory) 00143 return NoModRef; 00144 else if (MRB == OnlyReadsMemory) 00145 Mask = Ref; 00146 else if (MRB == AliasAnalysis::AccessesArguments) { 00147 bool doesAlias = false; 00148 for (CallSite::arg_iterator AI = CS.arg_begin(), AE = CS.arg_end(); 00149 AI != AE; ++AI) 00150 if (alias(*AI, ~0U, P, Size) != NoAlias) { 00151 doesAlias = true; 00152 break; 00153 } 00154 00155 if (!doesAlias) 00156 return NoModRef; 00157 } 00158 00159 if (!AA) return Mask; 00160 00161 // If P points to a constant memory location, the call definitely could not 00162 // modify the memory location. 00163 if ((Mask & Mod) && AA->pointsToConstantMemory(P)) 00164 Mask = ModRefResult(Mask & ~Mod); 00165 00166 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); 00167 } 00168 00169 // AliasAnalysis destructor: DO NOT move this to the header file for 00170 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on 00171 // the AliasAnalysis.o file in the current .a file, causing alias analysis 00172 // support to not be included in the tool correctly! 00173 // 00174 AliasAnalysis::~AliasAnalysis() {} 00175 00176 /// InitializeAliasAnalysis - Subclasses must call this method to initialize the 00177 /// AliasAnalysis interface before any other methods are called. 00178 /// 00179 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { 00180 TD = P->getAnalysisIfAvailable<TargetData>(); 00181 AA = &P->getAnalysis<AliasAnalysis>(); 00182 } 00183 00184 // getAnalysisUsage - All alias analysis implementations should invoke this 00185 // directly (using AliasAnalysis::getAnalysisUsage(AU)). 00186 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00187 AU.addRequired<AliasAnalysis>(); // All AA's chain 00188 } 00189 00190 /// getTypeStoreSize - Return the TargetData store size for the given type, 00191 /// if known, or a conservative value otherwise. 00192 /// 00193 unsigned AliasAnalysis::getTypeStoreSize(const Type *Ty) { 00194 return TD ? TD->getTypeStoreSize(Ty) : ~0u; 00195 } 00196 00197 /// canBasicBlockModify - Return true if it is possible for execution of the 00198 /// specified basic block to modify the value pointed to by Ptr. 00199 /// 00200 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, 00201 const Value *Ptr, unsigned Size) { 00202 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); 00203 } 00204 00205 /// canInstructionRangeModify - Return true if it is possible for the execution 00206 /// of the specified instructions to modify the value pointed to by Ptr. The 00207 /// instructions to consider are all of the instructions in the range of [I1,I2] 00208 /// INCLUSIVE. I1 and I2 must be in the same basic block. 00209 /// 00210 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, 00211 const Instruction &I2, 00212 const Value *Ptr, unsigned Size) { 00213 assert(I1.getParent() == I2.getParent() && 00214 "Instructions not in same basic block!"); 00215 BasicBlock::iterator I = const_cast<Instruction*>(&I1); 00216 BasicBlock::iterator E = const_cast<Instruction*>(&I2); 00217 ++E; // Convert from inclusive to exclusive range. 00218 00219 for (; I != E; ++I) // Check every instruction in range 00220 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) 00221 return true; 00222 return false; 00223 } 00224 00225 /// isNoAliasCall - Return true if this pointer is returned by a noalias 00226 /// function. 00227 bool llvm::isNoAliasCall(const Value *V) { 00228 if (isa<CallInst>(V) || isa<InvokeInst>(V)) 00229 return CallSite(const_cast<Instruction*>(cast<Instruction>(V))) 00230 .paramHasAttr(0, Attribute::NoAlias); 00231 return false; 00232 } 00233 00234 /// isIdentifiedObject - Return true if this pointer refers to a distinct and 00235 /// identifiable object. This returns true for: 00236 /// Global Variables and Functions 00237 /// Allocas and Mallocs 00238 /// ByVal and NoAlias Arguments 00239 /// NoAlias returns 00240 /// 00241 bool llvm::isIdentifiedObject(const Value *V) { 00242 if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V)) 00243 return true; 00244 if (const Argument *A = dyn_cast<Argument>(V)) 00245 return A->hasNoAliasAttr() || A->hasByValAttr(); 00246 return false; 00247 } 00248 00249 // Because of the way .a files work, we must force the BasicAA implementation to 00250 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run 00251 // the risk of AliasAnalysis being used, but the default implementation not 00252 // being linked into the tool that uses it. 00253 DEFINING_FILE_FOR(AliasAnalysis)