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