1 //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
10 // simple alias analysis implemented in terms of ScalarEvolution queries.
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.
16 // ScalarEvolution has a more complete understanding of pointer arithmetic
17 // than BasicAliasAnalysis' collection of ad-hoc analyses.
19 //===----------------------------------------------------------------------===//
21 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
24 AliasResult
SCEVAAResult::alias(const MemoryLocation
&LocA
,
25 const MemoryLocation
&LocB
, AAQueryInfo
&AAQI
) {
26 // If either of the memory references is empty, it doesn't matter what the
27 // pointer values are. This allows the code below to ignore this special
29 if (LocA
.Size
.isZero() || LocB
.Size
.isZero())
32 // This is SCEVAAResult. Get the SCEVs!
33 const SCEV
*AS
= SE
.getSCEV(const_cast<Value
*>(LocA
.Ptr
));
34 const SCEV
*BS
= SE
.getSCEV(const_cast<Value
*>(LocB
.Ptr
));
36 // If they evaluate to the same expression, it's a MustAlias.
40 // If something is known about the difference between the two addresses,
41 // see if it's enough to prove a NoAlias.
42 if (SE
.getEffectiveSCEVType(AS
->getType()) ==
43 SE
.getEffectiveSCEVType(BS
->getType())) {
44 unsigned BitWidth
= SE
.getTypeSizeInBits(AS
->getType());
45 APInt
ASizeInt(BitWidth
, LocA
.Size
.hasValue()
46 ? LocA
.Size
.getValue()
47 : MemoryLocation::UnknownSize
);
48 APInt
BSizeInt(BitWidth
, LocB
.Size
.hasValue()
49 ? LocB
.Size
.getValue()
50 : MemoryLocation::UnknownSize
);
52 // Compute the difference between the two pointers.
53 const SCEV
*BA
= SE
.getMinusSCEV(BS
, AS
);
55 // Test whether the difference is known to be great enough that memory of
56 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
57 // are non-zero, which is special-cased above.
58 if (ASizeInt
.ule(SE
.getUnsignedRange(BA
).getUnsignedMin()) &&
59 (-BSizeInt
).uge(SE
.getUnsignedRange(BA
).getUnsignedMax()))
62 // Folding the subtraction while preserving range information can be tricky
63 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
64 // and try again to see if things fold better that way.
66 // Compute the difference between the two pointers.
67 const SCEV
*AB
= SE
.getMinusSCEV(AS
, BS
);
69 // Test whether the difference is known to be great enough that memory of
70 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
71 // are non-zero, which is special-cased above.
72 if (BSizeInt
.ule(SE
.getUnsignedRange(AB
).getUnsignedMin()) &&
73 (-ASizeInt
).uge(SE
.getUnsignedRange(AB
).getUnsignedMax()))
77 // If ScalarEvolution can find an underlying object, form a new query.
78 // The correctness of this depends on ScalarEvolution not recognizing
79 // inttoptr and ptrtoint operators.
80 Value
*AO
= GetBaseValue(AS
);
81 Value
*BO
= GetBaseValue(BS
);
82 if ((AO
&& AO
!= LocA
.Ptr
) || (BO
&& BO
!= LocB
.Ptr
))
83 if (alias(MemoryLocation(AO
? AO
: LocA
.Ptr
,
84 AO
? LocationSize::unknown() : LocA
.Size
,
85 AO
? AAMDNodes() : LocA
.AATags
),
86 MemoryLocation(BO
? BO
: LocB
.Ptr
,
87 BO
? LocationSize::unknown() : LocB
.Size
,
88 BO
? AAMDNodes() : LocB
.AATags
),
92 // Forward the query to the next analysis.
93 return AAResultBase::alias(LocA
, LocB
, AAQI
);
96 /// Given an expression, try to find a base value.
98 /// Returns null if none was found.
99 Value
*SCEVAAResult::GetBaseValue(const SCEV
*S
) {
100 if (const SCEVAddRecExpr
*AR
= dyn_cast
<SCEVAddRecExpr
>(S
)) {
101 // In an addrec, assume that the base will be in the start, rather
103 return GetBaseValue(AR
->getStart());
104 } else if (const SCEVAddExpr
*A
= dyn_cast
<SCEVAddExpr
>(S
)) {
105 // If there's a pointer operand, it'll be sorted at the end of the list.
106 const SCEV
*Last
= A
->getOperand(A
->getNumOperands() - 1);
107 if (Last
->getType()->isPointerTy())
108 return GetBaseValue(Last
);
109 } else if (const SCEVUnknown
*U
= dyn_cast
<SCEVUnknown
>(S
)) {
110 // This is a leaf node.
111 return U
->getValue();
113 // No Identified object found.
117 AnalysisKey
SCEVAA::Key
;
119 SCEVAAResult
SCEVAA::run(Function
&F
, FunctionAnalysisManager
&AM
) {
120 return SCEVAAResult(AM
.getResult
<ScalarEvolutionAnalysis
>(F
));
123 char SCEVAAWrapperPass::ID
= 0;
124 INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass
, "scev-aa",
125 "ScalarEvolution-based Alias Analysis", false, true)
126 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass
)
127 INITIALIZE_PASS_END(SCEVAAWrapperPass
, "scev-aa",
128 "ScalarEvolution-based Alias Analysis", false, true)
130 FunctionPass
*llvm::createSCEVAAWrapperPass() {
131 return new SCEVAAWrapperPass();
134 SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID
) {
135 initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
138 bool SCEVAAWrapperPass::runOnFunction(Function
&F
) {
140 new SCEVAAResult(getAnalysis
<ScalarEvolutionWrapperPass
>().getSE()));
144 void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage
&AU
) const {
145 AU
.setPreservesAll();
146 AU
.addRequired
<ScalarEvolutionWrapperPass
>();