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