LLVM API Documentation
00001 //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source 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/Instructions.h" 00031 #include "llvm/Type.h" 00032 #include "llvm/Target/TargetData.h" 00033 #include <iostream> 00034 using namespace llvm; 00035 00036 // Register the AliasAnalysis interface, providing a nice name to refer to. 00037 namespace { 00038 RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); 00039 } 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 AliasAnalysis::ModRefBehavior 00063 AliasAnalysis::getModRefBehavior(Function *F, CallSite CS, 00064 std::vector<PointerAccessInfo> *Info) { 00065 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00066 return AA->getModRefBehavior(F, CS, Info); 00067 } 00068 00069 bool AliasAnalysis::hasNoModRefInfoForCalls() const { 00070 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00071 return AA->hasNoModRefInfoForCalls(); 00072 } 00073 00074 void AliasAnalysis::deleteValue(Value *V) { 00075 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00076 AA->deleteValue(V); 00077 } 00078 00079 void AliasAnalysis::copyValue(Value *From, Value *To) { 00080 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00081 AA->copyValue(From, To); 00082 } 00083 00084 AliasAnalysis::ModRefResult 00085 AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { 00086 // FIXME: we can do better. 00087 assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); 00088 return AA->getModRefInfo(CS1, CS2); 00089 } 00090 00091 00092 //===----------------------------------------------------------------------===// 00093 // AliasAnalysis non-virtual helper method implementation 00094 //===----------------------------------------------------------------------===// 00095 00096 AliasAnalysis::ModRefResult 00097 AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { 00098 return alias(L->getOperand(0), TD->getTypeSize(L->getType()), 00099 P, Size) ? Ref : NoModRef; 00100 } 00101 00102 AliasAnalysis::ModRefResult 00103 AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { 00104 // If the stored address cannot alias the pointer in question, then the 00105 // pointer cannot be modified by the store. 00106 if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), 00107 P, Size)) 00108 return NoModRef; 00109 00110 // If the pointer is a pointer to constant memory, then it could not have been 00111 // modified by this store. 00112 return pointsToConstantMemory(P) ? NoModRef : Mod; 00113 } 00114 00115 AliasAnalysis::ModRefResult 00116 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { 00117 ModRefResult Mask = ModRef; 00118 if (Function *F = CS.getCalledFunction()) { 00119 ModRefBehavior MRB = getModRefBehavior(F, CallSite()); 00120 if (MRB == OnlyReadsMemory) 00121 Mask = Ref; 00122 else if (MRB == DoesNotAccessMemory) 00123 return NoModRef; 00124 } 00125 00126 if (!AA) return Mask; 00127 00128 // If P points to a constant memory location, the call definitely could not 00129 // modify the memory location. 00130 if ((Mask & Mod) && AA->pointsToConstantMemory(P)) 00131 Mask = ModRefResult(Mask & ~Mod); 00132 00133 return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); 00134 } 00135 00136 // AliasAnalysis destructor: DO NOT move this to the header file for 00137 // AliasAnalysis or else clients of the AliasAnalysis class may not depend on 00138 // the AliasAnalysis.o file in the current .a file, causing alias analysis 00139 // support to not be included in the tool correctly! 00140 // 00141 AliasAnalysis::~AliasAnalysis() {} 00142 00143 /// setTargetData - Subclasses must call this method to initialize the 00144 /// AliasAnalysis interface before any other methods are called. 00145 /// 00146 void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { 00147 TD = &P->getAnalysis<TargetData>(); 00148 AA = &P->getAnalysis<AliasAnalysis>(); 00149 } 00150 00151 // getAnalysisUsage - All alias analysis implementations should invoke this 00152 // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that 00153 // TargetData is required by the pass. 00154 void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00155 AU.addRequired<TargetData>(); // All AA's need TargetData. 00156 AU.addRequired<AliasAnalysis>(); // All AA's chain 00157 } 00158 00159 /// canBasicBlockModify - Return true if it is possible for execution of the 00160 /// specified basic block to modify the value pointed to by Ptr. 00161 /// 00162 bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, 00163 const Value *Ptr, unsigned Size) { 00164 return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); 00165 } 00166 00167 /// canInstructionRangeModify - Return true if it is possible for the execution 00168 /// of the specified instructions to modify the value pointed to by Ptr. The 00169 /// instructions to consider are all of the instructions in the range of [I1,I2] 00170 /// INCLUSIVE. I1 and I2 must be in the same basic block. 00171 /// 00172 bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, 00173 const Instruction &I2, 00174 const Value *Ptr, unsigned Size) { 00175 assert(I1.getParent() == I2.getParent() && 00176 "Instructions not in same basic block!"); 00177 BasicBlock::iterator I = const_cast<Instruction*>(&I1); 00178 BasicBlock::iterator E = const_cast<Instruction*>(&I2); 00179 ++E; // Convert from inclusive to exclusive range. 00180 00181 for (; I != E; ++I) // Check every instruction in range 00182 if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) 00183 return true; 00184 return false; 00185 } 00186 00187 // Because of the way .a files work, we must force the BasicAA implementation to 00188 // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run 00189 // the risk of AliasAnalysis being used, but the default implementation not 00190 // being linked into the tool that uses it. 00191 DEFINING_FILE_FOR(AliasAnalysis)