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/Passes.h"
23 #include "llvm/Analysis/AliasAnalysis.h"
24 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
25 #include "llvm/IR/Module.h"
26 #include "llvm/Pass.h"
30 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
31 /// implementation that uses ScalarEvolution to answer queries.
32 class ScalarEvolutionAliasAnalysis : public FunctionPass,
33 public AliasAnalysis {
37 static char ID; // Class identification, replacement for typeinfo
38 ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(nullptr) {
39 initializeScalarEvolutionAliasAnalysisPass(
40 *PassRegistry::getPassRegistry());
43 /// getAdjustedAnalysisPointer - This method is used when a pass implements
44 /// an analysis interface through multiple inheritance. If needed, it
45 /// should override this to adjust the this pointer as needed for the
46 /// specified pass info.
47 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
48 if (PI == &AliasAnalysis::ID)
49 return (AliasAnalysis*)this;
54 void getAnalysisUsage(AnalysisUsage &AU) const override;
55 bool runOnFunction(Function &F) override;
56 AliasResult alias(const MemoryLocation &LocA,
57 const MemoryLocation &LocB) override;
59 Value *GetBaseValue(const SCEV *S);
61 } // End of anonymous namespace
63 // Register this pass...
64 char ScalarEvolutionAliasAnalysis::ID = 0;
65 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
66 "ScalarEvolution-based Alias Analysis", false, true, false)
67 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
68 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
69 "ScalarEvolution-based Alias Analysis", false, true, false)
71 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
72 return new ScalarEvolutionAliasAnalysis();
76 ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.addRequiredTransitive<ScalarEvolution>();
79 AliasAnalysis::getAnalysisUsage(AU);
83 ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
84 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
85 SE = &getAnalysis<ScalarEvolution>();
89 /// GetBaseValue - Given an expression, try to find a
90 /// base value. Return null is none was found.
92 ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
93 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
94 // In an addrec, assume that the base will be in the start, rather
96 return GetBaseValue(AR->getStart());
97 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
98 // If there's a pointer operand, it'll be sorted at the end of the list.
99 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
100 if (Last->getType()->isPointerTy())
101 return GetBaseValue(Last);
102 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
103 // This is a leaf node.
104 return U->getValue();
106 // No Identified object found.
110 AliasAnalysis::AliasResult
111 ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
112 const MemoryLocation &LocB) {
113 // If either of the memory references is empty, it doesn't matter what the
114 // pointer values are. This allows the code below to ignore this special
116 if (LocA.Size == 0 || LocB.Size == 0)
119 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
120 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
121 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
123 // If they evaluate to the same expression, it's a MustAlias.
124 if (AS == BS) return MustAlias;
126 // If something is known about the difference between the two addresses,
127 // see if it's enough to prove a NoAlias.
128 if (SE->getEffectiveSCEVType(AS->getType()) ==
129 SE->getEffectiveSCEVType(BS->getType())) {
130 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
131 APInt ASizeInt(BitWidth, LocA.Size);
132 APInt BSizeInt(BitWidth, LocB.Size);
134 // Compute the difference between the two pointers.
135 const SCEV *BA = SE->getMinusSCEV(BS, AS);
137 // Test whether the difference is known to be great enough that memory of
138 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
139 // are non-zero, which is special-cased above.
140 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
141 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
144 // Folding the subtraction while preserving range information can be tricky
145 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
146 // and try again to see if things fold better that way.
148 // Compute the difference between the two pointers.
149 const SCEV *AB = SE->getMinusSCEV(AS, BS);
151 // Test whether the difference is known to be great enough that memory of
152 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
153 // are non-zero, which is special-cased above.
154 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
155 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
159 // If ScalarEvolution can find an underlying object, form a new query.
160 // The correctness of this depends on ScalarEvolution not recognizing
161 // inttoptr and ptrtoint operators.
162 Value *AO = GetBaseValue(AS);
163 Value *BO = GetBaseValue(BS);
164 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
165 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
166 AO ? +MemoryLocation::UnknownSize : LocA.Size,
167 AO ? AAMDNodes() : LocA.AATags),
168 MemoryLocation(BO ? BO : LocB.Ptr,
169 BO ? +MemoryLocation::UnknownSize : LocB.Size,
170 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
173 // Forward the query to the next analysis.
174 return AliasAnalysis::alias(LocA, LocB);