| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// | 
| Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // | 
| John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
| Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
| Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // | 
| John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 9 | // | 
|  | 10 | // This file implements the generic AliasAnalysis interface which is used as the | 
|  | 11 | // common interface used by all clients and implementations of alias analysis. | 
|  | 12 | // | 
|  | 13 | // This file also implements the default version of the AliasAnalysis interface | 
|  | 14 | // that is to be used when no other implementation is specified.  This does some | 
|  | 15 | // simple tests that detect obvious cases: two different global pointers cannot | 
|  | 16 | // alias, a global cannot alias a malloc, two different mallocs cannot alias, | 
|  | 17 | // etc. | 
|  | 18 | // | 
|  | 19 | // This alias analysis implementation really isn't very good for anything, but | 
|  | 20 | // it is very fast, and makes a nice clean default implementation.  Because it | 
|  | 21 | // handles lots of little corner cases, other, more complex, alias analysis | 
|  | 22 | // implementations may choose to rely on this pass to resolve these simple and | 
|  | 23 | // easy cases. | 
|  | 24 | // | 
|  | 25 | //===----------------------------------------------------------------------===// | 
|  | 26 |  | 
| Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" | 
| Reid Spencer | 6df60a9 | 2006-06-07 20:00:19 +0000 | [diff] [blame] | 28 | #include "llvm/Pass.h" | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 29 | #include "llvm/BasicBlock.h" | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 30 | #include "llvm/Function.h" | 
| Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 31 | #include "llvm/Instructions.h" | 
| Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 32 | #include "llvm/Type.h" | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 33 | #include "llvm/Target/TargetData.h" | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 34 | using namespace llvm; | 
| Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 35 |  | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 36 | // Register the AliasAnalysis interface, providing a nice name to refer to. | 
| Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 37 | namespace { | 
|  | 38 | RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); | 
| Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 39 | } | 
| Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 40 | char AliasAnalysis::ID = 0; | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 41 |  | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// | 
|  | 43 | // Default chaining methods | 
|  | 44 | //===----------------------------------------------------------------------===// | 
|  | 45 |  | 
|  | 46 | AliasAnalysis::AliasResult | 
|  | 47 | AliasAnalysis::alias(const Value *V1, unsigned V1Size, | 
|  | 48 | const Value *V2, unsigned V2Size) { | 
|  | 49 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 50 | return AA->alias(V1, V1Size, V2, V2Size); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) { | 
|  | 54 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 55 | return AA->getMustAliases(P, RetVals); | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | bool AliasAnalysis::pointsToConstantMemory(const Value *P) { | 
|  | 59 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 60 | return AA->pointsToConstantMemory(P); | 
|  | 61 | } | 
|  | 62 |  | 
| Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 63 | AliasAnalysis::ModRefBehavior | 
|  | 64 | AliasAnalysis::getModRefBehavior(Function *F, CallSite CS, | 
|  | 65 | std::vector<PointerAccessInfo> *Info) { | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 66 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
| Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 67 | return AA->getModRefBehavior(F, CS, Info); | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 68 | } | 
|  | 69 |  | 
|  | 70 | bool AliasAnalysis::hasNoModRefInfoForCalls() const { | 
|  | 71 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 72 | return AA->hasNoModRefInfoForCalls(); | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | void AliasAnalysis::deleteValue(Value *V) { | 
|  | 76 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 77 | AA->deleteValue(V); | 
|  | 78 | } | 
|  | 79 |  | 
|  | 80 | void AliasAnalysis::copyValue(Value *From, Value *To) { | 
|  | 81 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 82 | AA->copyValue(From, To); | 
|  | 83 | } | 
|  | 84 |  | 
|  | 85 | AliasAnalysis::ModRefResult | 
|  | 86 | AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { | 
|  | 87 | // FIXME: we can do better. | 
|  | 88 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); | 
|  | 89 | return AA->getModRefInfo(CS1, CS2); | 
|  | 90 | } | 
|  | 91 |  | 
|  | 92 |  | 
|  | 93 | //===----------------------------------------------------------------------===// | 
|  | 94 | // AliasAnalysis non-virtual helper method implementation | 
|  | 95 | //===----------------------------------------------------------------------===// | 
|  | 96 |  | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 97 | AliasAnalysis::ModRefResult | 
|  | 98 | AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { | 
| Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 99 | return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()), | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 100 | P, Size) ? Ref : NoModRef; | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 101 | } | 
|  | 102 |  | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 103 | AliasAnalysis::ModRefResult | 
|  | 104 | AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { | 
| Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 105 | // If the stored address cannot alias the pointer in question, then the | 
|  | 106 | // pointer cannot be modified by the store. | 
| Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 107 | if (!alias(S->getOperand(1), | 
|  | 108 | TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size)) | 
| Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 109 | return NoModRef; | 
|  | 110 |  | 
|  | 111 | // If the pointer is a pointer to constant memory, then it could not have been | 
|  | 112 | // modified by this store. | 
|  | 113 | return pointsToConstantMemory(P) ? NoModRef : Mod; | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 114 | } | 
|  | 115 |  | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 116 | AliasAnalysis::ModRefBehavior | 
|  | 117 | AliasAnalysis::getModRefBehavior(CallSite CS, | 
|  | 118 | std::vector<PointerAccessInfo> *Info) { | 
| Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 119 | if (CS.doesNotAccessMemory()) | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 120 | // Can't do better than this. | 
|  | 121 | return DoesNotAccessMemory; | 
|  | 122 | ModRefBehavior MRB = UnknownModRefBehavior; | 
|  | 123 | if (Function *F = CS.getCalledFunction()) | 
|  | 124 | MRB = getModRefBehavior(F, CS, Info); | 
| Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 125 | if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory()) | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 126 | return OnlyReadsMemory; | 
|  | 127 | return MRB; | 
|  | 128 | } | 
|  | 129 |  | 
|  | 130 | AliasAnalysis::ModRefBehavior | 
|  | 131 | AliasAnalysis::getModRefBehavior(Function *F, | 
|  | 132 | std::vector<PointerAccessInfo> *Info) { | 
| Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 133 | if (F->doesNotAccessMemory()) | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 134 | // Can't do better than this. | 
|  | 135 | return DoesNotAccessMemory; | 
|  | 136 | ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info); | 
| Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 137 | if (MRB != DoesNotAccessMemory && F->onlyReadsMemory()) | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 138 | return OnlyReadsMemory; | 
|  | 139 | return MRB; | 
|  | 140 | } | 
|  | 141 |  | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 142 | AliasAnalysis::ModRefResult | 
|  | 143 | AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 144 | ModRefResult Mask = ModRef; | 
| Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 145 | ModRefBehavior MRB = getModRefBehavior(CS); | 
|  | 146 | if (MRB == OnlyReadsMemory) | 
|  | 147 | Mask = Ref; | 
|  | 148 | else if (MRB == DoesNotAccessMemory) | 
|  | 149 | return NoModRef; | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 150 |  | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 151 | if (!AA) return Mask; | 
|  | 152 |  | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 153 | // If P points to a constant memory location, the call definitely could not | 
|  | 154 | // modify the memory location. | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 155 | if ((Mask & Mod) && AA->pointsToConstantMemory(P)) | 
| Chris Lattner | d433bde | 2005-03-23 22:06:41 +0000 | [diff] [blame] | 156 | Mask = ModRefResult(Mask & ~Mod); | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 157 |  | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 158 | return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); | 
| Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 161 | // AliasAnalysis destructor: DO NOT move this to the header file for | 
|  | 162 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on | 
|  | 163 | // the AliasAnalysis.o file in the current .a file, causing alias analysis | 
|  | 164 | // support to not be included in the tool correctly! | 
|  | 165 | // | 
|  | 166 | AliasAnalysis::~AliasAnalysis() {} | 
|  | 167 |  | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 168 | /// setTargetData - Subclasses must call this method to initialize the | 
|  | 169 | /// AliasAnalysis interface before any other methods are called. | 
|  | 170 | /// | 
|  | 171 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { | 
|  | 172 | TD = &P->getAnalysis<TargetData>(); | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 173 | AA = &P->getAnalysis<AliasAnalysis>(); | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 174 | } | 
|  | 175 |  | 
|  | 176 | // getAnalysisUsage - All alias analysis implementations should invoke this | 
|  | 177 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that | 
|  | 178 | // TargetData is required by the pass. | 
|  | 179 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { | 
|  | 180 | AU.addRequired<TargetData>();            // All AA's need TargetData. | 
| Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 181 | AU.addRequired<AliasAnalysis>();         // All AA's chain | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 182 | } | 
|  | 183 |  | 
| Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 184 | /// canBasicBlockModify - Return true if it is possible for execution of the | 
|  | 185 | /// specified basic block to modify the value pointed to by Ptr. | 
|  | 186 | /// | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 187 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, | 
|  | 188 | const Value *Ptr, unsigned Size) { | 
|  | 189 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 190 | } | 
|  | 191 |  | 
| Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 192 | /// canInstructionRangeModify - Return true if it is possible for the execution | 
|  | 193 | /// of the specified instructions to modify the value pointed to by Ptr.  The | 
|  | 194 | /// instructions to consider are all of the instructions in the range of [I1,I2] | 
|  | 195 | /// INCLUSIVE.  I1 and I2 must be in the same basic block. | 
|  | 196 | /// | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 197 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, | 
|  | 198 | const Instruction &I2, | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 199 | const Value *Ptr, unsigned Size) { | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 200 | assert(I1.getParent() == I2.getParent() && | 
|  | 201 | "Instructions not in same basic block!"); | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 202 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); | 
|  | 203 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); | 
|  | 204 | ++E;  // Convert from inclusive to exclusive range. | 
|  | 205 |  | 
| Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 206 | for (; I != E; ++I) // Check every instruction in range | 
|  | 207 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 208 | return true; | 
| Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 209 | return false; | 
|  | 210 | } | 
|  | 211 |  | 
| Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 212 | // Because of the way .a files work, we must force the BasicAA implementation to | 
|  | 213 | // be pulled in if the AliasAnalysis classes are pulled in.  Otherwise we run | 
|  | 214 | // the risk of AliasAnalysis being used, but the default implementation not | 
|  | 215 | // being linked into the tool that uses it. | 
| Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 216 | DEFINING_FILE_FOR(AliasAnalysis) |