LLVM API Documentation

AliasAnalysis.cpp

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



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.