1 //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11 // simple alias analysis implemented in terms of ScalarEvolution queries.
13 // This differs from traditional loop dependence analysis in that it tests
14 // for dependencies within a single iteration of a loop, rather than
15 // dependencies between different iterations.
17 // ScalarEvolution has a more complete understanding of pointer arithmetic
18 // than BasicAliasAnalysis' collection of ad-hoc analyses.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
24 #include "llvm/Analysis/Passes.h"
25 #include "llvm/Pass.h"
29 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
30 /// implementation that uses ScalarEvolution to answer queries.
31 class ScalarEvolutionAliasAnalysis : public FunctionPass,
32 public AliasAnalysis {
36 static char ID; // Class identification, replacement for typeinfo
37 ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) {}
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.
43 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
44 if (PI == &AliasAnalysis::ID)
45 return (AliasAnalysis*)this;
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
51 virtual bool runOnFunction(Function &F);
52 virtual AliasResult alias(const Location &LocA, const Location &LocB);
54 Value *GetBaseValue(const SCEV *S);
56 } // End of anonymous namespace
58 // Register this pass...
59 char ScalarEvolutionAliasAnalysis::ID = 0;
60 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
61 "ScalarEvolution-based Alias Analysis", false, true, false)
62 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
63 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
64 "ScalarEvolution-based Alias Analysis", false, true, false)
66 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
67 return new ScalarEvolutionAliasAnalysis();
71 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequiredTransitive<ScalarEvolution>();
74 AliasAnalysis::getAnalysisUsage(AU);
78 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
79 InitializeAliasAnalysis(this);
80 SE = &getAnalysis<ScalarEvolution>();
84 /// GetBaseValue - Given an expression, try to find a
85 /// base value. Return null is none was found.
87 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
88 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
89 // In an addrec, assume that the base will be in the start, rather
91 return GetBaseValue(AR->getStart());
92 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
93 // If there's a pointer operand, it'll be sorted at the end of the list.
94 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
95 if (Last->getType()->isPointerTy())
96 return GetBaseValue(Last);
97 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
98 // This is a leaf node.
101 // No Identified object found.
105 AliasAnalysis::AliasResult
106 ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
107 const Location &LocB) {
108 // If either of the memory references is empty, it doesn't matter what the
109 // pointer values are. This allows the code below to ignore this special
111 if (LocA.Size == 0 || LocB.Size == 0)
114 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
115 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
116 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
118 // If they evaluate to the same expression, it's a MustAlias.
119 if (AS == BS) return MustAlias;
121 // If something is known about the difference between the two addresses,
122 // see if it's enough to prove a NoAlias.
123 if (SE->getEffectiveSCEVType(AS->getType()) ==
124 SE->getEffectiveSCEVType(BS->getType())) {
125 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
126 APInt ASizeInt(BitWidth, LocA.Size);
127 APInt BSizeInt(BitWidth, LocB.Size);
129 // Compute the difference between the two pointers.
130 const SCEV *BA = SE->getMinusSCEV(BS, AS);
132 // Test whether the difference is known to be great enough that memory of
133 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
134 // are non-zero, which is special-cased above.
135 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
136 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
139 // Folding the subtraction while preserving range information can be tricky
140 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
141 // and try again to see if things fold better that way.
143 // Compute the difference between the two pointers.
144 const SCEV *AB = SE->getMinusSCEV(AS, BS);
146 // Test whether the difference is known to be great enough that memory of
147 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
148 // are non-zero, which is special-cased above.
149 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
150 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
154 // If ScalarEvolution can find an underlying object, form a new query.
155 // The correctness of this depends on ScalarEvolution not recognizing
156 // inttoptr and ptrtoint operators.
157 Value *AO = GetBaseValue(AS);
158 Value *BO = GetBaseValue(BS);
159 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
160 if (alias(Location(AO ? AO : LocA.Ptr,
161 AO ? +UnknownSize : LocA.Size,
162 AO ? 0 : LocA.TBAATag),
163 Location(BO ? BO : LocB.Ptr,
164 BO ? +UnknownSize : LocB.Size,
165 BO ? 0 : LocB.TBAATag)) == NoAlias)
168 // Forward the query to the next analysis.
169 return AliasAnalysis::alias(LocA, LocB);