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 | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 37 | ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) { |
| 38 | initializeScalarEvolutionAliasAnalysisPass( |
| 39 | *PassRegistry::getPassRegistry()); |
| 40 | } |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 41 | |
Chris Lattner | 1bc76d4 | 2010-01-20 20:09:02 +0000 | [diff] [blame] | 42 | /// getAdjustedAnalysisPointer - This method is used when a pass implements |
| 43 | /// an analysis interface through multiple inheritance. If needed, it |
| 44 | /// should override this to adjust the this pointer as needed for the |
| 45 | /// specified pass info. |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 46 | virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { |
| 47 | if (PI == &AliasAnalysis::ID) |
Chris Lattner | 1bc76d4 | 2010-01-20 20:09:02 +0000 | [diff] [blame] | 48 | return (AliasAnalysis*)this; |
| 49 | return this; |
| 50 | } |
Dan Gohman | 7cbf5a8 | 2010-03-01 17:56:04 +0000 | [diff] [blame] | 51 | |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 52 | private: |
| 53 | virtual void getAnalysisUsage(AnalysisUsage &AU) const; |
| 54 | virtual bool runOnFunction(Function &F); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 55 | virtual AliasResult alias(const Location &LocA, const Location &LocB); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 56 | |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 57 | Value *GetBaseValue(const SCEV *S); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 58 | }; |
| 59 | } // End of anonymous namespace |
| 60 | |
| 61 | // Register this pass... |
| 62 | char ScalarEvolutionAliasAnalysis::ID = 0; |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 63 | INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 64 | "ScalarEvolution-based Alias Analysis", false, true, false) |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 65 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 66 | INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa", |
| 67 | "ScalarEvolution-based Alias Analysis", false, true, false) |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 68 | |
| 69 | FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() { |
| 70 | return new ScalarEvolutionAliasAnalysis(); |
| 71 | } |
| 72 | |
| 73 | void |
| 74 | ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 75 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 76 | AU.setPreservesAll(); |
| 77 | AliasAnalysis::getAnalysisUsage(AU); |
| 78 | } |
| 79 | |
| 80 | bool |
| 81 | ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) { |
| 82 | InitializeAliasAnalysis(this); |
| 83 | SE = &getAnalysis<ScalarEvolution>(); |
| 84 | return false; |
| 85 | } |
| 86 | |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 87 | /// GetBaseValue - Given an expression, try to find a |
| 88 | /// base value. Return null is none was found. |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 89 | Value * |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 90 | ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) { |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 91 | if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) { |
Dan Gohman | ed77e52 | 2009-08-29 23:36:57 +0000 | [diff] [blame] | 92 | // In an addrec, assume that the base will be in the start, rather |
| 93 | // than the step. |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 94 | return GetBaseValue(AR->getStart()); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 95 | } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) { |
| 96 | // If there's a pointer operand, it'll be sorted at the end of the list. |
| 97 | const SCEV *Last = A->getOperand(A->getNumOperands()-1); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 98 | if (Last->getType()->isPointerTy()) |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 99 | return GetBaseValue(Last); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 100 | } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) { |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 101 | // This is a leaf node. |
| 102 | return U->getValue(); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 103 | } |
| 104 | // No Identified object found. |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | AliasAnalysis::AliasResult |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 109 | ScalarEvolutionAliasAnalysis::alias(const Location &LocA, |
| 110 | const Location &LocB) { |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 111 | // If either of the memory references is empty, it doesn't matter what the |
| 112 | // pointer values are. This allows the code below to ignore this special |
| 113 | // case. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 114 | if (LocA.Size == 0 || LocB.Size == 0) |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 115 | return NoAlias; |
| 116 | |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 117 | // This is ScalarEvolutionAliasAnalysis. Get the SCEVs! |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 118 | const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr)); |
| 119 | const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr)); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 120 | |
| 121 | // If they evaluate to the same expression, it's a MustAlias. |
| 122 | if (AS == BS) return MustAlias; |
| 123 | |
| 124 | // If something is known about the difference between the two addresses, |
| 125 | // see if it's enough to prove a NoAlias. |
| 126 | if (SE->getEffectiveSCEVType(AS->getType()) == |
| 127 | SE->getEffectiveSCEVType(BS->getType())) { |
| 128 | unsigned BitWidth = SE->getTypeSizeInBits(AS->getType()); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 129 | APInt ASizeInt(BitWidth, LocA.Size); |
| 130 | APInt BSizeInt(BitWidth, LocB.Size); |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 131 | |
| 132 | // Compute the difference between the two pointers. |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 133 | const SCEV *BA = SE->getMinusSCEV(BS, AS); |
Dan Gohman | 49bda91 | 2010-06-30 06:12:16 +0000 | [diff] [blame] | 134 | |
| 135 | // Test whether the difference is known to be great enough that memory of |
| 136 | // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt |
| 137 | // are non-zero, which is special-cased above. |
| 138 | if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) && |
| 139 | (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax())) |
| 140 | return NoAlias; |
| 141 | |
| 142 | // Folding the subtraction while preserving range information can be tricky |
| 143 | // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS |
| 144 | // and try again to see if things fold better that way. |
| 145 | |
| 146 | // Compute the difference between the two pointers. |
| 147 | const SCEV *AB = SE->getMinusSCEV(AS, BS); |
| 148 | |
| 149 | // Test whether the difference is known to be great enough that memory of |
| 150 | // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt |
| 151 | // are non-zero, which is special-cased above. |
| 152 | if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) && |
| 153 | (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax())) |
| 154 | return NoAlias; |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | // If ScalarEvolution can find an underlying object, form a new query. |
| 158 | // The correctness of this depends on ScalarEvolution not recognizing |
| 159 | // inttoptr and ptrtoint operators. |
Dan Gohman | 576fd76 | 2009-10-31 14:32:25 +0000 | [diff] [blame] | 160 | Value *AO = GetBaseValue(AS); |
| 161 | Value *BO = GetBaseValue(BS); |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 162 | if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr)) |
| 163 | if (alias(Location(AO ? AO : LocA.Ptr, |
| 164 | AO ? +UnknownSize : LocA.Size, |
| 165 | AO ? 0 : LocA.TBAATag), |
| 166 | Location(BO ? BO : LocB.Ptr, |
| 167 | BO ? +UnknownSize : LocB.Size, |
| 168 | BO ? 0 : LocB.TBAATag)) == NoAlias) |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 169 | return NoAlias; |
| 170 | |
| 171 | // Forward the query to the next analysis. |
Dan Gohman | b2143b6 | 2010-09-14 21:25:10 +0000 | [diff] [blame] | 172 | return AliasAnalysis::alias(LocA, LocB); |
Dan Gohman | 2385e0e | 2009-08-26 14:53:06 +0000 | [diff] [blame] | 173 | } |