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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 30 | #include "llvm/Instructions.h" |
Chris Lattner | 5b3a455 | 2005-03-17 15:38:16 +0000 | [diff] [blame] | 31 | #include "llvm/Type.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 32 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 33 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 34 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 35 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 36 | namespace { |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame^] | 37 | const int AliasAnalysis::ID = 0; |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 38 | RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 39 | } |
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) { |
| 98 | return alias(L->getOperand(0), TD->getTypeSize(L->getType()), |
| 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. |
| 106 | if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), |
| 107 | P, Size)) |
| 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 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 115 | AliasAnalysis::ModRefResult |
| 116 | AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 117 | ModRefResult Mask = ModRef; |
Chris Lattner | 8cfd24d | 2005-03-23 23:26:58 +0000 | [diff] [blame] | 118 | if (Function *F = CS.getCalledFunction()) { |
| 119 | ModRefBehavior MRB = getModRefBehavior(F, CallSite()); |
| 120 | if (MRB == OnlyReadsMemory) |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 121 | Mask = Ref; |
Chris Lattner | 8cfd24d | 2005-03-23 23:26:58 +0000 | [diff] [blame] | 122 | else if (MRB == DoesNotAccessMemory) |
| 123 | return NoModRef; |
| 124 | } |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 125 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 126 | if (!AA) return Mask; |
| 127 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 128 | // If P points to a constant memory location, the call definitely could not |
| 129 | // modify the memory location. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 130 | if ((Mask & Mod) && AA->pointsToConstantMemory(P)) |
Chris Lattner | d433bde | 2005-03-23 22:06:41 +0000 | [diff] [blame] | 131 | Mask = ModRefResult(Mask & ~Mod); |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 132 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 133 | return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 136 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 137 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 138 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 139 | // support to not be included in the tool correctly! |
| 140 | // |
| 141 | AliasAnalysis::~AliasAnalysis() {} |
| 142 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 143 | /// setTargetData - Subclasses must call this method to initialize the |
| 144 | /// AliasAnalysis interface before any other methods are called. |
| 145 | /// |
| 146 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
| 147 | TD = &P->getAnalysis<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 148 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | // getAnalysisUsage - All alias analysis implementations should invoke this |
| 152 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that |
| 153 | // TargetData is required by the pass. |
| 154 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 155 | AU.addRequired<TargetData>(); // All AA's need TargetData. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 156 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 159 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 160 | /// specified basic block to modify the value pointed to by Ptr. |
| 161 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 162 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 163 | const Value *Ptr, unsigned Size) { |
| 164 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 167 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 168 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 169 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 170 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 171 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 172 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 173 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 174 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 175 | assert(I1.getParent() == I2.getParent() && |
| 176 | "Instructions not in same basic block!"); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 177 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); |
| 178 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); |
| 179 | ++E; // Convert from inclusive to exclusive range. |
| 180 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 181 | for (; I != E; ++I) // Check every instruction in range |
| 182 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 183 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 184 | return false; |
| 185 | } |
| 186 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 187 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 188 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 189 | // the risk of AliasAnalysis being used, but the default implementation not |
| 190 | // being linked into the tool that uses it. |
Reid Spencer | 4f1bd9e | 2006-06-07 22:00:26 +0000 | [diff] [blame] | 191 | DEFINING_FILE_FOR(AliasAnalysis) |