Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 1 | //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a |
| 11 | // simple alias analysis implemented in terms of ScalarEvolution queries. |
| 12 | // |
Dan Gohman | 7cbf5a8 | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 13 | // This differs from traditional loop dependence analysis in that it tests |
| 14 | // for dependencies within a single iteration of a loop, rather than |
Dan Gohman | 4f46e14 | 2010-06-18 00:53:08 +0000 | [diff] [blame] | 15 | // dependencies between different iterations. |
Dan Gohman | 7cbf5a8 | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 16 | // |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 17 | // ScalarEvolution has a more complete understanding of pointer arithmetic |
| 18 | // than BasicAliasAnalysis' collection of ad-hoc analyses. |
| 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "llvm/Analysis/AliasAnalysis.h" |
| 23 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
| 24 | #include "llvm/Analysis/Passes.h" |
| 25 | #include "llvm/Pass.h" |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | namespace { |
| 29 | /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis |
| 30 | /// implementation that uses ScalarEvolution to answer queries. |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 31 | class ScalarEvolutionAliasAnalysis : public FunctionPass, |
| 32 | public AliasAnalysis { |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 33 | ScalarEvolution *SE; |
| 34 | |
| 35 | public: |
| 36 | static char ID; // Class identification, replacement for typeinfo |
Owen Anderson | 9ccaf53 | 2010-08-05 23:42:04 +0000 | [diff] [blame] | 37 | ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) {} |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 1bc76d4 | 2010-01-20 20:09:02 +0000 | [diff] [blame] | 39 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 40 | /// an analysis interface through multiple inheritance. If needed, it |
| 41 | /// should override this to adjust the this pointer as needed for the |
| 42 | /// specified pass info. |
Owen Anderson | 9ccaf53 | 2010-08-05 23:42:04 +0000 | [diff] [blame] | 43 | virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { |
| 44 | if (PI == &AliasAnalysis::ID) |
Chris Lattner | 1bc76d4 | 2010-01-20 20:09:02 +0000 | [diff] [blame] | 45 | return (AliasAnalysis*)this; |
| 46 | return this; |
| 47 | } |
Dan Gohman | 7cbf5a8 | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 49 | private: |
| 50 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 51 | virtual bool runOnFunction(Function &F); |
| 52 | virtual AliasResult alias(const Value *V1, unsigned V1Size, |
| 53 | const Value *V2, unsigned V2Size); |
| 54 | |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 55 | Value *GetBaseValue(const SCEV *S); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 56 | }; |
| 57 | } // End of anonymous namespace |
| 58 | |
| 59 | // Register this pass... |
| 60 | char ScalarEvolutionAliasAnalysis::ID = 0; |
Owen Anderson | d8cc7be | 2010-07-21 23:07:00 +0000 | [diff] [blame] | 61 | INITIALIZE_AG_PASS(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa", |
| 62 | "ScalarEvolution-based Alias Analysis", false, true, false); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 63 | |
| 64 | FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() { |
| 65 | return new ScalarEvolutionAliasAnalysis(); |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 70 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 71 | AU.setPreservesAll(); |
| 72 | AliasAnalysis::getAnalysisUsage(AU); |
| 73 | } |
| 74 | |
| 75 | bool |
| 76 | ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) { |
| 77 | InitializeAliasAnalysis(this); |
| 78 | SE = &getAnalysis<ScalarEvolution>(); |
| 79 | return false; |
| 80 | } |
| 81 | |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 82 | /// GetBaseValue - Given an expression, try to find a |
| 83 | /// base value. Return null is none was found. |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 84 | Value * |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 85 | ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) { |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 86 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | ed77e52 | 2009-08-29 23:36:57 +0000 | [diff] [blame] | 87 | // In an addrec, assume that the base will be in the start, rather |
| 88 | // than the step. |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 89 | return GetBaseValue(AR->getStart()); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 90 | } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
| 91 | // If there's a pointer operand, it'll be sorted at the end of the list. |
| 92 | const SCEV *Last = A->getOperand(A->getNumOperands()-1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 93 | if (Last->getType()->isPointerTy()) |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 94 | return GetBaseValue(Last); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 95 | } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 96 | // This is a leaf node. |
| 97 | return U->getValue(); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 98 | } |
| 99 | // No Identified object found. |
| 100 | return 0; |
| 101 | } |
| 102 | |
| 103 | AliasAnalysis::AliasResult |
| 104 | ScalarEvolutionAliasAnalysis::alias(const Value *A, unsigned ASize, |
| 105 | const Value *B, unsigned BSize) { |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 106 | // If either of the memory references is empty, it doesn't matter what the |
| 107 | // pointer values are. This allows the code below to ignore this special |
| 108 | // case. |
| 109 | if (ASize == 0 || BSize == 0) |
| 110 | return NoAlias; |
| 111 | |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 112 | // This is ScalarEvolutionAliasAnalysis. Get the SCEVs! |
| 113 | const SCEV *AS = SE->getSCEV(const_cast<Value *>(A)); |
| 114 | const SCEV *BS = SE->getSCEV(const_cast<Value *>(B)); |
| 115 | |
| 116 | // If they evaluate to the same expression, it's a MustAlias. |
| 117 | if (AS == BS) return MustAlias; |
| 118 | |
| 119 | // If something is known about the difference between the two addresses, |
| 120 | // see if it's enough to prove a NoAlias. |
| 121 | if (SE->getEffectiveSCEVType(AS->getType()) == |
| 122 | SE->getEffectiveSCEVType(BS->getType())) { |
| 123 | unsigned BitWidth = SE->getTypeSizeInBits(AS->getType()); |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 124 | APInt ASizeInt(BitWidth, ASize); |
| 125 | APInt BSizeInt(BitWidth, BSize); |
| 126 | |
| 127 | // Compute the difference between the two pointers. |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 128 | const SCEV *BA = SE->getMinusSCEV(BS, AS); |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 129 | |
| 130 | // Test whether the difference is known to be great enough that memory of |
| 131 | // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt |
| 132 | // are non-zero, which is special-cased above. |
| 133 | if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) && |
| 134 | (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax())) |
| 135 | return NoAlias; |
| 136 | |
| 137 | // Folding the subtraction while preserving range information can be tricky |
| 138 | // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS |
| 139 | // and try again to see if things fold better that way. |
| 140 | |
| 141 | // Compute the difference between the two pointers. |
| 142 | const SCEV *AB = SE->getMinusSCEV(AS, BS); |
| 143 | |
| 144 | // Test whether the difference is known to be great enough that memory of |
| 145 | // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt |
| 146 | // are non-zero, which is special-cased above. |
| 147 | if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) && |
| 148 | (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax())) |
| 149 | return NoAlias; |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | // If ScalarEvolution can find an underlying object, form a new query. |
| 153 | // The correctness of this depends on ScalarEvolution not recognizing |
| 154 | // inttoptr and ptrtoint operators. |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 155 | Value *AO = GetBaseValue(AS); |
| 156 | Value *BO = GetBaseValue(BS); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 157 | if ((AO && AO != A) || (BO && BO != B)) |
Dan Gohman | ef1cfac | 2010-08-03 01:03:11 +0000 | [diff] [blame] | 158 | if (alias(AO ? AO : A, AO ? UnknownSize : ASize, |
| 159 | BO ? BO : B, BO ? UnknownSize : BSize) == NoAlias) |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 160 | return NoAlias; |
| 161 | |
| 162 | // Forward the query to the next analysis. |
| 163 | return AliasAnalysis::alias(A, ASize, B, BSize); |
| 164 | } |