1*9880d681SAndroid Build Coastguard Worker //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11*9880d681SAndroid Build Coastguard Worker // simple alias analysis implemented in terms of ScalarEvolution queries.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker // This differs from traditional loop dependence analysis in that it tests
14*9880d681SAndroid Build Coastguard Worker // for dependencies within a single iteration of a loop, rather than
15*9880d681SAndroid Build Coastguard Worker // dependencies between different iterations.
16*9880d681SAndroid Build Coastguard Worker //
17*9880d681SAndroid Build Coastguard Worker // ScalarEvolution has a more complete understanding of pointer arithmetic
18*9880d681SAndroid Build Coastguard Worker // than BasicAliasAnalysis' collection of ad-hoc analyses.
19*9880d681SAndroid Build Coastguard Worker //
20*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
21*9880d681SAndroid Build Coastguard Worker
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
23*9880d681SAndroid Build Coastguard Worker using namespace llvm;
24*9880d681SAndroid Build Coastguard Worker
alias(const MemoryLocation & LocA,const MemoryLocation & LocB)25*9880d681SAndroid Build Coastguard Worker AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
26*9880d681SAndroid Build Coastguard Worker const MemoryLocation &LocB) {
27*9880d681SAndroid Build Coastguard Worker // If either of the memory references is empty, it doesn't matter what the
28*9880d681SAndroid Build Coastguard Worker // pointer values are. This allows the code below to ignore this special
29*9880d681SAndroid Build Coastguard Worker // case.
30*9880d681SAndroid Build Coastguard Worker if (LocA.Size == 0 || LocB.Size == 0)
31*9880d681SAndroid Build Coastguard Worker return NoAlias;
32*9880d681SAndroid Build Coastguard Worker
33*9880d681SAndroid Build Coastguard Worker // This is SCEVAAResult. Get the SCEVs!
34*9880d681SAndroid Build Coastguard Worker const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
35*9880d681SAndroid Build Coastguard Worker const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker // If they evaluate to the same expression, it's a MustAlias.
38*9880d681SAndroid Build Coastguard Worker if (AS == BS)
39*9880d681SAndroid Build Coastguard Worker return MustAlias;
40*9880d681SAndroid Build Coastguard Worker
41*9880d681SAndroid Build Coastguard Worker // If something is known about the difference between the two addresses,
42*9880d681SAndroid Build Coastguard Worker // see if it's enough to prove a NoAlias.
43*9880d681SAndroid Build Coastguard Worker if (SE.getEffectiveSCEVType(AS->getType()) ==
44*9880d681SAndroid Build Coastguard Worker SE.getEffectiveSCEVType(BS->getType())) {
45*9880d681SAndroid Build Coastguard Worker unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
46*9880d681SAndroid Build Coastguard Worker APInt ASizeInt(BitWidth, LocA.Size);
47*9880d681SAndroid Build Coastguard Worker APInt BSizeInt(BitWidth, LocB.Size);
48*9880d681SAndroid Build Coastguard Worker
49*9880d681SAndroid Build Coastguard Worker // Compute the difference between the two pointers.
50*9880d681SAndroid Build Coastguard Worker const SCEV *BA = SE.getMinusSCEV(BS, AS);
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker // Test whether the difference is known to be great enough that memory of
53*9880d681SAndroid Build Coastguard Worker // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
54*9880d681SAndroid Build Coastguard Worker // are non-zero, which is special-cased above.
55*9880d681SAndroid Build Coastguard Worker if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
56*9880d681SAndroid Build Coastguard Worker (-BSizeInt).uge(SE.getUnsignedRange(BA).getUnsignedMax()))
57*9880d681SAndroid Build Coastguard Worker return NoAlias;
58*9880d681SAndroid Build Coastguard Worker
59*9880d681SAndroid Build Coastguard Worker // Folding the subtraction while preserving range information can be tricky
60*9880d681SAndroid Build Coastguard Worker // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
61*9880d681SAndroid Build Coastguard Worker // and try again to see if things fold better that way.
62*9880d681SAndroid Build Coastguard Worker
63*9880d681SAndroid Build Coastguard Worker // Compute the difference between the two pointers.
64*9880d681SAndroid Build Coastguard Worker const SCEV *AB = SE.getMinusSCEV(AS, BS);
65*9880d681SAndroid Build Coastguard Worker
66*9880d681SAndroid Build Coastguard Worker // Test whether the difference is known to be great enough that memory of
67*9880d681SAndroid Build Coastguard Worker // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
68*9880d681SAndroid Build Coastguard Worker // are non-zero, which is special-cased above.
69*9880d681SAndroid Build Coastguard Worker if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
70*9880d681SAndroid Build Coastguard Worker (-ASizeInt).uge(SE.getUnsignedRange(AB).getUnsignedMax()))
71*9880d681SAndroid Build Coastguard Worker return NoAlias;
72*9880d681SAndroid Build Coastguard Worker }
73*9880d681SAndroid Build Coastguard Worker
74*9880d681SAndroid Build Coastguard Worker // If ScalarEvolution can find an underlying object, form a new query.
75*9880d681SAndroid Build Coastguard Worker // The correctness of this depends on ScalarEvolution not recognizing
76*9880d681SAndroid Build Coastguard Worker // inttoptr and ptrtoint operators.
77*9880d681SAndroid Build Coastguard Worker Value *AO = GetBaseValue(AS);
78*9880d681SAndroid Build Coastguard Worker Value *BO = GetBaseValue(BS);
79*9880d681SAndroid Build Coastguard Worker if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
80*9880d681SAndroid Build Coastguard Worker if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
81*9880d681SAndroid Build Coastguard Worker AO ? +MemoryLocation::UnknownSize : LocA.Size,
82*9880d681SAndroid Build Coastguard Worker AO ? AAMDNodes() : LocA.AATags),
83*9880d681SAndroid Build Coastguard Worker MemoryLocation(BO ? BO : LocB.Ptr,
84*9880d681SAndroid Build Coastguard Worker BO ? +MemoryLocation::UnknownSize : LocB.Size,
85*9880d681SAndroid Build Coastguard Worker BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
86*9880d681SAndroid Build Coastguard Worker return NoAlias;
87*9880d681SAndroid Build Coastguard Worker
88*9880d681SAndroid Build Coastguard Worker // Forward the query to the next analysis.
89*9880d681SAndroid Build Coastguard Worker return AAResultBase::alias(LocA, LocB);
90*9880d681SAndroid Build Coastguard Worker }
91*9880d681SAndroid Build Coastguard Worker
92*9880d681SAndroid Build Coastguard Worker /// Given an expression, try to find a base value.
93*9880d681SAndroid Build Coastguard Worker ///
94*9880d681SAndroid Build Coastguard Worker /// Returns null if none was found.
GetBaseValue(const SCEV * S)95*9880d681SAndroid Build Coastguard Worker Value *SCEVAAResult::GetBaseValue(const SCEV *S) {
96*9880d681SAndroid Build Coastguard Worker if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
97*9880d681SAndroid Build Coastguard Worker // In an addrec, assume that the base will be in the start, rather
98*9880d681SAndroid Build Coastguard Worker // than the step.
99*9880d681SAndroid Build Coastguard Worker return GetBaseValue(AR->getStart());
100*9880d681SAndroid Build Coastguard Worker } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
101*9880d681SAndroid Build Coastguard Worker // If there's a pointer operand, it'll be sorted at the end of the list.
102*9880d681SAndroid Build Coastguard Worker const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
103*9880d681SAndroid Build Coastguard Worker if (Last->getType()->isPointerTy())
104*9880d681SAndroid Build Coastguard Worker return GetBaseValue(Last);
105*9880d681SAndroid Build Coastguard Worker } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
106*9880d681SAndroid Build Coastguard Worker // This is a leaf node.
107*9880d681SAndroid Build Coastguard Worker return U->getValue();
108*9880d681SAndroid Build Coastguard Worker }
109*9880d681SAndroid Build Coastguard Worker // No Identified object found.
110*9880d681SAndroid Build Coastguard Worker return nullptr;
111*9880d681SAndroid Build Coastguard Worker }
112*9880d681SAndroid Build Coastguard Worker
113*9880d681SAndroid Build Coastguard Worker char SCEVAA::PassID;
114*9880d681SAndroid Build Coastguard Worker
run(Function & F,AnalysisManager<Function> & AM)115*9880d681SAndroid Build Coastguard Worker SCEVAAResult SCEVAA::run(Function &F, AnalysisManager<Function> &AM) {
116*9880d681SAndroid Build Coastguard Worker return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
117*9880d681SAndroid Build Coastguard Worker }
118*9880d681SAndroid Build Coastguard Worker
119*9880d681SAndroid Build Coastguard Worker char SCEVAAWrapperPass::ID = 0;
120*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
121*9880d681SAndroid Build Coastguard Worker "ScalarEvolution-based Alias Analysis", false, true)
INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)122*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
123*9880d681SAndroid Build Coastguard Worker INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
124*9880d681SAndroid Build Coastguard Worker "ScalarEvolution-based Alias Analysis", false, true)
125*9880d681SAndroid Build Coastguard Worker
126*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createSCEVAAWrapperPass() {
127*9880d681SAndroid Build Coastguard Worker return new SCEVAAWrapperPass();
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
SCEVAAWrapperPass()130*9880d681SAndroid Build Coastguard Worker SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
131*9880d681SAndroid Build Coastguard Worker initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
132*9880d681SAndroid Build Coastguard Worker }
133*9880d681SAndroid Build Coastguard Worker
runOnFunction(Function & F)134*9880d681SAndroid Build Coastguard Worker bool SCEVAAWrapperPass::runOnFunction(Function &F) {
135*9880d681SAndroid Build Coastguard Worker Result.reset(
136*9880d681SAndroid Build Coastguard Worker new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
137*9880d681SAndroid Build Coastguard Worker return false;
138*9880d681SAndroid Build Coastguard Worker }
139*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const140*9880d681SAndroid Build Coastguard Worker void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
141*9880d681SAndroid Build Coastguard Worker AU.setPreservesAll();
142*9880d681SAndroid Build Coastguard Worker AU.addRequired<ScalarEvolutionWrapperPass>();
143*9880d681SAndroid Build Coastguard Worker }
144