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" |
Owen Anderson | cd89525 | 2009-02-03 06:27:22 +0000 | [diff] [blame] | 31 | #include "llvm/IntrinsicInst.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 32 | #include "llvm/Instructions.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 33 | #include "llvm/Type.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 34 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 35 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 36 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 37 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 38 | static RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 39 | char AliasAnalysis::ID = 0; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 40 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 41 | //===----------------------------------------------------------------------===// |
| 42 | // Default chaining methods |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
| 45 | AliasAnalysis::AliasResult |
| 46 | AliasAnalysis::alias(const Value *V1, unsigned V1Size, |
| 47 | const Value *V2, unsigned V2Size) { |
| 48 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 49 | return AA->alias(V1, V1Size, V2, V2Size); |
| 50 | } |
| 51 | |
| 52 | void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
| 53 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 54 | return AA->getMustAliases(P, RetVals); |
| 55 | } |
| 56 | |
| 57 | bool AliasAnalysis::pointsToConstantMemory(const Value *P) { |
| 58 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 59 | return AA->pointsToConstantMemory(P); |
| 60 | } |
| 61 | |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 62 | AliasAnalysis::ModRefBehavior |
| 63 | AliasAnalysis::getModRefBehavior(Function *F, CallSite CS, |
| 64 | std::vector<PointerAccessInfo> *Info) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 65 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 66 | return AA->getModRefBehavior(F, CS, Info); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | bool AliasAnalysis::hasNoModRefInfoForCalls() const { |
| 70 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 71 | return AA->hasNoModRefInfoForCalls(); |
| 72 | } |
| 73 | |
| 74 | void AliasAnalysis::deleteValue(Value *V) { |
| 75 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 76 | AA->deleteValue(V); |
| 77 | } |
| 78 | |
| 79 | void AliasAnalysis::copyValue(Value *From, Value *To) { |
| 80 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 81 | AA->copyValue(From, To); |
| 82 | } |
| 83 | |
| 84 | AliasAnalysis::ModRefResult |
| 85 | AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 86 | // FIXME: we can do better. |
| 87 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 88 | return AA->getModRefInfo(CS1, CS2); |
| 89 | } |
| 90 | |
| 91 | |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | // AliasAnalysis non-virtual helper method implementation |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 96 | AliasAnalysis::ModRefResult |
| 97 | AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 98 | return alias(L->getOperand(0), TD->getTypeStoreSize(L->getType()), |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 99 | P, Size) ? Ref : NoModRef; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 100 | } |
| 101 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 102 | AliasAnalysis::ModRefResult |
| 103 | AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 104 | // If the stored address cannot alias the pointer in question, then the |
| 105 | // pointer cannot be modified by the store. |
Duncan Sands | 514ab34 | 2007-11-01 20:53:16 +0000 | [diff] [blame] | 106 | if (!alias(S->getOperand(1), |
| 107 | TD->getTypeStoreSize(S->getOperand(0)->getType()), P, Size)) |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 108 | return NoModRef; |
| 109 | |
| 110 | // If the pointer is a pointer to constant memory, then it could not have been |
| 111 | // modified by this store. |
| 112 | return pointsToConstantMemory(P) ? NoModRef : Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 113 | } |
| 114 | |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 115 | AliasAnalysis::ModRefBehavior |
| 116 | AliasAnalysis::getModRefBehavior(CallSite CS, |
| 117 | std::vector<PointerAccessInfo> *Info) { |
Owen Anderson | cd89525 | 2009-02-03 06:27:22 +0000 | [diff] [blame] | 118 | if (IntrinsicInst* II = dyn_cast<IntrinsicInst>(CS.getInstruction())) { |
| 119 | switch (II->getIntrinsicID()) { |
| 120 | case Intrinsic::atomic_cmp_swap: |
| 121 | case Intrinsic::atomic_load_add: |
| 122 | case Intrinsic::atomic_load_and: |
| 123 | case Intrinsic::atomic_load_max: |
| 124 | case Intrinsic::atomic_load_min: |
| 125 | case Intrinsic::atomic_load_nand: |
| 126 | case Intrinsic::atomic_load_or: |
| 127 | case Intrinsic::atomic_load_sub: |
| 128 | case Intrinsic::atomic_load_umax: |
| 129 | case Intrinsic::atomic_load_umin: |
| 130 | case Intrinsic::atomic_load_xor: |
| 131 | case Intrinsic::atomic_swap: |
| 132 | // CAS and related intrinsics only access their arguments. |
| 133 | return AliasAnalysis::AccessesArguments; |
| 134 | default: |
| 135 | break; |
| 136 | } |
| 137 | } |
| 138 | |
Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 139 | if (CS.doesNotAccessMemory()) |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 140 | // Can't do better than this. |
| 141 | return DoesNotAccessMemory; |
| 142 | ModRefBehavior MRB = UnknownModRefBehavior; |
| 143 | if (Function *F = CS.getCalledFunction()) |
| 144 | MRB = getModRefBehavior(F, CS, Info); |
Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 145 | if (MRB != DoesNotAccessMemory && CS.onlyReadsMemory()) |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 146 | return OnlyReadsMemory; |
| 147 | return MRB; |
| 148 | } |
| 149 | |
| 150 | AliasAnalysis::ModRefBehavior |
| 151 | AliasAnalysis::getModRefBehavior(Function *F, |
| 152 | std::vector<PointerAccessInfo> *Info) { |
Owen Anderson | fe9388c | 2009-02-04 05:16:46 +0000 | [diff] [blame] | 153 | if (F->isIntrinsic()) { |
| 154 | switch (F->getIntrinsicID()) { |
| 155 | case Intrinsic::atomic_cmp_swap: |
| 156 | case Intrinsic::atomic_load_add: |
| 157 | case Intrinsic::atomic_load_and: |
| 158 | case Intrinsic::atomic_load_max: |
| 159 | case Intrinsic::atomic_load_min: |
| 160 | case Intrinsic::atomic_load_nand: |
| 161 | case Intrinsic::atomic_load_or: |
| 162 | case Intrinsic::atomic_load_sub: |
| 163 | case Intrinsic::atomic_load_umax: |
| 164 | case Intrinsic::atomic_load_umin: |
| 165 | case Intrinsic::atomic_load_xor: |
| 166 | case Intrinsic::atomic_swap: |
| 167 | // CAS and related intrinsics only access their arguments. |
| 168 | return AliasAnalysis::AccessesArguments; |
| 169 | default: |
| 170 | break; |
| 171 | } |
| 172 | } |
| 173 | |
Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 174 | if (F->doesNotAccessMemory()) |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 175 | // Can't do better than this. |
| 176 | return DoesNotAccessMemory; |
| 177 | ModRefBehavior MRB = getModRefBehavior(F, CallSite(), Info); |
Duncan Sands | 7915cbe | 2007-12-12 16:01:40 +0000 | [diff] [blame] | 178 | if (MRB != DoesNotAccessMemory && F->onlyReadsMemory()) |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 179 | return OnlyReadsMemory; |
| 180 | return MRB; |
| 181 | } |
| 182 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 183 | AliasAnalysis::ModRefResult |
| 184 | AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 185 | ModRefResult Mask = ModRef; |
Duncan Sands | dff6710 | 2007-12-01 07:51:45 +0000 | [diff] [blame] | 186 | ModRefBehavior MRB = getModRefBehavior(CS); |
| 187 | if (MRB == OnlyReadsMemory) |
| 188 | Mask = Ref; |
| 189 | else if (MRB == DoesNotAccessMemory) |
| 190 | return NoModRef; |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 191 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 192 | if (!AA) return Mask; |
| 193 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 194 | // If P points to a constant memory location, the call definitely could not |
| 195 | // modify the memory location. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 196 | if ((Mask & Mod) && AA->pointsToConstantMemory(P)) |
Chris Lattner | d433bde | 2005-03-23 22:06:41 +0000 | [diff] [blame] | 197 | Mask = ModRefResult(Mask & ~Mod); |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 199 | return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 200 | } |
| 201 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 202 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 203 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 204 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 205 | // support to not be included in the tool correctly! |
| 206 | // |
| 207 | AliasAnalysis::~AliasAnalysis() {} |
| 208 | |
Dan Gohman | 5a56bf6 | 2008-05-30 00:02:02 +0000 | [diff] [blame] | 209 | /// InitializeAliasAnalysis - Subclasses must call this method to initialize the |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 210 | /// AliasAnalysis interface before any other methods are called. |
| 211 | /// |
| 212 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
| 213 | TD = &P->getAnalysis<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 214 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | // getAnalysisUsage - All alias analysis implementations should invoke this |
| 218 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that |
| 219 | // TargetData is required by the pass. |
| 220 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 221 | AU.addRequired<TargetData>(); // All AA's need TargetData. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 222 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 225 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 226 | /// specified basic block to modify the value pointed to by Ptr. |
| 227 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 228 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 229 | const Value *Ptr, unsigned Size) { |
| 230 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 231 | } |
| 232 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 233 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 234 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 235 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 236 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 237 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 238 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 239 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 240 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 241 | assert(I1.getParent() == I2.getParent() && |
| 242 | "Instructions not in same basic block!"); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 243 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); |
| 244 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); |
| 245 | ++E; // Convert from inclusive to exclusive range. |
| 246 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 247 | for (; I != E; ++I) // Check every instruction in range |
| 248 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 249 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 250 | return false; |
| 251 | } |
| 252 | |
Dan Gohman | a5f81bb | 2009-02-03 01:28:32 +0000 | [diff] [blame] | 253 | /// isNoAliasCall - Return true if this pointer is returned by a noalias |
| 254 | /// function. |
| 255 | bool llvm::isNoAliasCall(const Value *V) { |
| 256 | if (isa<CallInst>(V) || isa<InvokeInst>(V)) |
| 257 | return CallSite(const_cast<Instruction*>(cast<Instruction>(V))) |
| 258 | .paramHasAttr(0, Attribute::NoAlias); |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | /// isIdentifiedObject - Return true if this pointer refers to a distinct and |
| 263 | /// identifiable object. This returns true for: |
| 264 | /// Global Variables and Functions |
| 265 | /// Allocas and Mallocs |
| 266 | /// ByVal and NoAlias Arguments |
| 267 | /// NoAlias returns |
| 268 | /// |
| 269 | bool llvm::isIdentifiedObject(const Value *V) { |
| 270 | if (isa<GlobalValue>(V) || isa<AllocationInst>(V) || isNoAliasCall(V)) |
| 271 | return true; |
| 272 | if (const Argument *A = dyn_cast<Argument>(V)) |
| 273 | return A->hasNoAliasAttr() || A->hasByValAttr(); |
| 274 | return false; |
| 275 | } |
| 276 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 277 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 278 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 279 | // the risk of AliasAnalysis being used, but the default implementation not |
| 280 | // being linked into the tool that uses it. |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 281 | DEFINING_FILE_FOR(AliasAnalysis) |