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 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/Type.h"
00031 #include "llvm/Target/TargetData.h"
00032 #include <iostream>
00033 using namespace llvm;
00034 
00035 // Register the AliasAnalysis interface, providing a nice name to refer to.
00036 namespace {
00037   RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis");
00038 }
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->getTypeSize(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), TD->getTypeSize(S->getOperand(0)->getType()),
00106              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::ModRefResult
00115 AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) {
00116   ModRefResult Mask = ModRef;
00117   if (Function *F = CS.getCalledFunction()) {
00118     ModRefBehavior MRB = getModRefBehavior(F, CallSite());
00119     if (MRB == OnlyReadsMemory)
00120       Mask = Ref;
00121     else if (MRB == DoesNotAccessMemory)
00122       return NoModRef;
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 = ModRefResult(Mask & ~Mod);
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 namespace llvm {
00192   extern void BasicAAStub();
00193 }
00194 static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub);