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" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 29 | #include "llvm/iMemory.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame^] | 31 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 32 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 33 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 34 | namespace { |
| 35 | RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 36 | } |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 38 | AliasAnalysis::ModRefResult |
| 39 | AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { |
| 40 | return alias(L->getOperand(0), TD->getTypeSize(L->getType()), |
| 41 | P, Size) ? Ref : NoModRef; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 42 | } |
| 43 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 44 | AliasAnalysis::ModRefResult |
| 45 | AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { |
Chris Lattner | f4d904d | 2004-01-30 22:16:42 +0000 | [diff] [blame] | 46 | // If the stored address cannot alias the pointer in question, then the |
| 47 | // pointer cannot be modified by the store. |
| 48 | if (!alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), |
| 49 | P, Size)) |
| 50 | return NoModRef; |
| 51 | |
| 52 | // If the pointer is a pointer to constant memory, then it could not have been |
| 53 | // modified by this store. |
| 54 | return pointsToConstantMemory(P) ? NoModRef : Mod; |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 55 | } |
| 56 | |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame^] | 57 | AliasAnalysis::ModRefResult |
| 58 | AliasAnalysis::getModRefInfo(CallSite CS, Value *P, unsigned Size) { |
| 59 | if (Function *F = CS.getCalledFunction()) |
| 60 | if (onlyReadsMemory(F)) { |
| 61 | if (doesNotAccessMemory(F)) return NoModRef; |
| 62 | return Ref; |
| 63 | } |
| 64 | |
| 65 | // If P points to a constant memory location, the call definitely could not |
| 66 | // modify the memory location. |
| 67 | return pointsToConstantMemory(P) ? Ref : ModRef; |
| 68 | } |
| 69 | |
| 70 | AliasAnalysis::ModRefResult |
| 71 | AliasAnalysis::getModRefInfo(CallSite CS1, CallSite CS2) { |
| 72 | // FIXME: could probably do better. |
| 73 | return ModRef; |
| 74 | } |
| 75 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 76 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 77 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 78 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 79 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 80 | // support to not be included in the tool correctly! |
| 81 | // |
| 82 | AliasAnalysis::~AliasAnalysis() {} |
| 83 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 84 | /// setTargetData - Subclasses must call this method to initialize the |
| 85 | /// AliasAnalysis interface before any other methods are called. |
| 86 | /// |
| 87 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
| 88 | TD = &P->getAnalysis<TargetData>(); |
| 89 | } |
| 90 | |
| 91 | // getAnalysisUsage - All alias analysis implementations should invoke this |
| 92 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that |
| 93 | // TargetData is required by the pass. |
| 94 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 95 | AU.addRequired<TargetData>(); // All AA's need TargetData. |
| 96 | } |
| 97 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 98 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 99 | /// specified basic block to modify the value pointed to by Ptr. |
| 100 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 101 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 102 | const Value *Ptr, unsigned Size) { |
| 103 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 106 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 107 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 108 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 109 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 110 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 111 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 112 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 113 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 114 | assert(I1.getParent() == I2.getParent() && |
| 115 | "Instructions not in same basic block!"); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 116 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); |
| 117 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); |
| 118 | ++E; // Convert from inclusive to exclusive range. |
| 119 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 120 | for (; I != E; ++I) // Check every instruction in range |
| 121 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 122 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 123 | return false; |
| 124 | } |
| 125 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 126 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 127 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 128 | // the risk of AliasAnalysis being used, but the default implementation not |
| 129 | // being linked into the tool that uses it. |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 130 | // |
Chris Lattner | 992860c | 2004-03-15 04:07:29 +0000 | [diff] [blame^] | 131 | extern void llvm::BasicAAStub(); |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame] | 132 | static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 133 | |
Chris Lattner | 8dcd17c | 2003-02-26 19:57:10 +0000 | [diff] [blame] | 134 | |
| 135 | namespace { |
| 136 | struct NoAA : public ImmutablePass, public AliasAnalysis { |
| 137 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 138 | AliasAnalysis::getAnalysisUsage(AU); |
| 139 | } |
| 140 | |
| 141 | virtual void initializePass() { |
| 142 | InitializeAliasAnalysis(this); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | // Register this pass... |
| 147 | RegisterOpt<NoAA> |
| 148 | X("no-aa", "No Alias Analysis (always returns 'may' alias)"); |
| 149 | |
| 150 | // Declare that we implement the AliasAnalysis interface |
| 151 | RegisterAnalysisGroup<AliasAnalysis, NoAA> Y; |
| 152 | } // End of anonymous namespace |