Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 2 | // |
| 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. |
| 7 | // |
| 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" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 28 | #include "llvm/BasicBlock.h" |
Misha Brukman | 47b14a4 | 2004-07-29 17:30:56 +0000 | [diff] [blame] | 29 | #include "llvm/Instructions.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Reid Spencer | 954da37 | 2004-07-04 12:19:56 +0000 | [diff] [blame] | 31 | #include <iostream> |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 32 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 34 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 35 | namespace { |
| 36 | RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 37 | } |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 39 | //===----------------------------------------------------------------------===// |
| 40 | // Default chaining methods |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | AliasAnalysis::AliasResult |
| 44 | AliasAnalysis::alias(const Value *V1, unsigned V1Size, |
| 45 | const Value *V2, unsigned V2Size) { |
| 46 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 47 | return AA->alias(V1, V1Size, V2, V2Size); |
| 48 | } |
| 49 | |
| 50 | void AliasAnalysis::getMustAliases(Value *P, std::vector<Value*> &RetVals) { |
| 51 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 52 | return AA->getMustAliases(P, RetVals); |
| 53 | } |
| 54 | |
| 55 | bool AliasAnalysis::pointsToConstantMemory(const Value *P) { |
| 56 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 57 | return AA->pointsToConstantMemory(P); |
| 58 | } |
| 59 | |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 60 | AliasAnalysis::ModRefBehavior |
| 61 | AliasAnalysis::getModRefBehavior(Function *F, CallSite CS, |
| 62 | std::vector<PointerAccessInfo> *Info) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 63 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
Chris Lattner | 0af024c | 2004-12-15 07:22:13 +0000 | [diff] [blame] | 64 | return AA->getModRefBehavior(F, CS, Info); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | bool AliasAnalysis::hasNoModRefInfoForCalls() const { |
| 68 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 69 | return AA->hasNoModRefInfoForCalls(); |
| 70 | } |
| 71 | |
| 72 | void AliasAnalysis::deleteValue(Value *V) { |
| 73 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 74 | AA->deleteValue(V); |
| 75 | } |
| 76 | |
| 77 | void AliasAnalysis::copyValue(Value *From, Value *To) { |
| 78 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 79 | AA->copyValue(From, To); |
| 80 | } |
| 81 | |
| 82 | AliasAnalysis::ModRefResult |
| 83 | AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 84 | // FIXME: we can do better. |
| 85 | assert(AA && "AA didn't call InitializeAliasAnalysis in its run method!"); |
| 86 | return AA->getModRefInfo(CS1, CS2); |
| 87 | } |
| 88 | |
| 89 | |
| 90 | //===----------------------------------------------------------------------===// |
| 91 | // AliasAnalysis non-virtual helper method implementation |
| 92 | //===----------------------------------------------------------------------===// |
| 93 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 94 | AliasAnalysis::ModRefResult |
| 95 | AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { |
| 96 | return alias(L->getOperand(0), TD->getTypeSize(L->getType()), |
| 97 | P, Size) ? Ref : NoModRef; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 100 | AliasAnalysis::ModRefResult |
| 101 | AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 102 | // If the stored address cannot alias the pointer in question, then the |
| 103 | // pointer cannot be modified by the store. |
| 104 | if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), |
| 105 | P, Size)) |
| 106 | return NoModRef; |
| 107 | |
| 108 | // If the pointer is a pointer to constant memory, then it could not have been |
| 109 | // modified by this store. |
| 110 | return pointsToConstantMemory(P) ? NoModRef : Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 113 | AliasAnalysis::ModRefResult |
| 114 | AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 115 | ModRefResult Mask = ModRef; |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 116 | if (Function *F = CS.getCalledFunction()) |
| 117 | if (onlyReadsMemory(F)) { |
| 118 | if (doesNotAccessMemory(F)) return NoModRef; |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 119 | Mask = Ref; |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 122 | if (!AA) return Mask; |
| 123 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 124 | // If P points to a constant memory location, the call definitely could not |
| 125 | // modify the memory location. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 126 | if ((Mask & Mod) && AA->pointsToConstantMemory(P)) |
| 127 | Mask = Ref; |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 129 | return ModRefResult(Mask & AA->getModRefInfo(CS, P, Size)); |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 132 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 133 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 134 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 135 | // support to not be included in the tool correctly! |
| 136 | // |
| 137 | AliasAnalysis::~AliasAnalysis() {} |
| 138 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 139 | /// setTargetData - Subclasses must call this method to initialize the |
| 140 | /// AliasAnalysis interface before any other methods are called. |
| 141 | /// |
| 142 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
| 143 | TD = &P->getAnalysis<TargetData>(); |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 144 | AA = &P->getAnalysis<AliasAnalysis>(); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // getAnalysisUsage - All alias analysis implementations should invoke this |
| 148 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that |
| 149 | // TargetData is required by the pass. |
| 150 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 151 | AU.addRequired<TargetData>(); // All AA's need TargetData. |
Chris Lattner | 5a24d70 | 2004-05-23 21:15:48 +0000 | [diff] [blame] | 152 | AU.addRequired<AliasAnalysis>(); // All AA's chain |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 155 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 156 | /// specified basic block to modify the value pointed to by Ptr. |
| 157 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 158 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 159 | const Value *Ptr, unsigned Size) { |
| 160 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 163 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 164 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 165 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 166 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 167 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 168 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 169 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 170 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 171 | assert(I1.getParent() == I2.getParent() && |
| 172 | "Instructions not in same basic block!"); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 173 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); |
| 174 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); |
| 175 | ++E; // Convert from inclusive to exclusive range. |
| 176 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 177 | for (; I != E; ++I) // Check every instruction in range |
| 178 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 179 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 180 | return false; |
| 181 | } |
| 182 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 183 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 184 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 185 | // the risk of AliasAnalysis being used, but the default implementation not |
| 186 | // being linked into the tool that uses it. |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 187 | // |
Chris Lattner | 2c20ef5 | 2004-12-08 21:00:59 +0000 | [diff] [blame] | 188 | namespace llvm { |
| 189 | extern void BasicAAStub(); |
| 190 | } |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 191 | static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub); |