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