Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 1 | //===- AliasAnalysis.cpp - Generic Alias Analysis Interface Implementation -==// |
| 2 | // |
| 3 | // This file implements the generic AliasAnalysis interface which is used as the |
| 4 | // common interface used by all clients and implementations of alias analysis. |
| 5 | // |
| 6 | // This file also implements the default version of the AliasAnalysis interface |
| 7 | // that is to be used when no other implementation is specified. This does some |
| 8 | // simple tests that detect obvious cases: two different global pointers cannot |
| 9 | // alias, a global cannot alias a malloc, two different mallocs cannot alias, |
| 10 | // etc. |
| 11 | // |
| 12 | // This alias analysis implementation really isn't very good for anything, but |
| 13 | // it is very fast, and makes a nice clean default implementation. Because it |
| 14 | // handles lots of little corner cases, other, more complex, alias analysis |
| 15 | // implementations may choose to rely on this pass to resolve these simple and |
| 16 | // easy cases. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame^] | 20 | #include "llvm/Analysis/AliasAnalysis.h" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 21 | #include "llvm/BasicBlock.h" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 22 | #include "llvm/iMemory.h" |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 23 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 24 | |
| 25 | // Register the AliasAnalysis interface, providing a nice name to refer to. |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | RegisterAnalysisGroup<AliasAnalysis> Z("Alias Analysis"); |
Chris Lattner | dc1ad19 | 2003-02-03 21:16:17 +0000 | [diff] [blame] | 28 | } |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 30 | AliasAnalysis::ModRefResult |
| 31 | AliasAnalysis::getModRefInfo(LoadInst *L, Value *P, unsigned Size) { |
| 32 | return alias(L->getOperand(0), TD->getTypeSize(L->getType()), |
| 33 | P, Size) ? Ref : NoModRef; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 34 | } |
| 35 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 36 | AliasAnalysis::ModRefResult |
| 37 | AliasAnalysis::getModRefInfo(StoreInst *S, Value *P, unsigned Size) { |
| 38 | return alias(S->getOperand(1), TD->getTypeSize(S->getOperand(0)->getType()), |
| 39 | P, Size) ? Mod : NoModRef; |
| 40 | } |
| 41 | |
| 42 | |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 43 | // AliasAnalysis destructor: DO NOT move this to the header file for |
| 44 | // AliasAnalysis or else clients of the AliasAnalysis class may not depend on |
| 45 | // the AliasAnalysis.o file in the current .a file, causing alias analysis |
| 46 | // support to not be included in the tool correctly! |
| 47 | // |
| 48 | AliasAnalysis::~AliasAnalysis() {} |
| 49 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 50 | /// setTargetData - Subclasses must call this method to initialize the |
| 51 | /// AliasAnalysis interface before any other methods are called. |
| 52 | /// |
| 53 | void AliasAnalysis::InitializeAliasAnalysis(Pass *P) { |
| 54 | TD = &P->getAnalysis<TargetData>(); |
| 55 | } |
| 56 | |
| 57 | // getAnalysisUsage - All alias analysis implementations should invoke this |
| 58 | // directly (using AliasAnalysis::getAnalysisUsage(AU)) to make sure that |
| 59 | // TargetData is required by the pass. |
| 60 | void AliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 61 | AU.addRequired<TargetData>(); // All AA's need TargetData. |
| 62 | } |
| 63 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 64 | /// canBasicBlockModify - Return true if it is possible for execution of the |
| 65 | /// specified basic block to modify the value pointed to by Ptr. |
| 66 | /// |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 67 | bool AliasAnalysis::canBasicBlockModify(const BasicBlock &BB, |
| 68 | const Value *Ptr, unsigned Size) { |
| 69 | return canInstructionRangeModify(BB.front(), BB.back(), Ptr, Size); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Chris Lattner | f9355f6 | 2002-08-22 22:46:39 +0000 | [diff] [blame] | 72 | /// canInstructionRangeModify - Return true if it is possible for the execution |
| 73 | /// of the specified instructions to modify the value pointed to by Ptr. The |
| 74 | /// instructions to consider are all of the instructions in the range of [I1,I2] |
| 75 | /// INCLUSIVE. I1 and I2 must be in the same basic block. |
| 76 | /// |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 77 | bool AliasAnalysis::canInstructionRangeModify(const Instruction &I1, |
| 78 | const Instruction &I2, |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 79 | const Value *Ptr, unsigned Size) { |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 80 | assert(I1.getParent() == I2.getParent() && |
| 81 | "Instructions not in same basic block!"); |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 82 | BasicBlock::iterator I = const_cast<Instruction*>(&I1); |
| 83 | BasicBlock::iterator E = const_cast<Instruction*>(&I2); |
| 84 | ++E; // Convert from inclusive to exclusive range. |
| 85 | |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 86 | for (; I != E; ++I) // Check every instruction in range |
| 87 | if (getModRefInfo(I, const_cast<Value*>(Ptr), Size) & Mod) |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 88 | return true; |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 89 | return false; |
| 90 | } |
| 91 | |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame^] | 92 | // Because of the way .a files work, we must force the BasicAA implementation to |
| 93 | // be pulled in if the AliasAnalysis classes are pulled in. Otherwise we run |
| 94 | // the risk of AliasAnalysis being used, but the default implementation not |
| 95 | // being linked into the tool that uses it. |
Chris Lattner | 53ad0ed | 2002-08-22 18:25:32 +0000 | [diff] [blame] | 96 | // |
Chris Lattner | d501c13 | 2003-02-26 19:41:54 +0000 | [diff] [blame^] | 97 | extern void BasicAAStub(); |
| 98 | static IncludeFile INCLUDE_BASICAA_CPP((void*)&BasicAAStub); |
Chris Lattner | 14ac877 | 2003-02-26 19:26:51 +0000 | [diff] [blame] | 99 | |