Line data Source code
1 : //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
2 : //
3 : // The LLVM Compiler Infrastructure
4 : //
5 : // This file is distributed under the University of Illinois Open Source
6 : // License. See LICENSE.TXT for details.
7 : //
8 : //===----------------------------------------------------------------------===//
9 : //
10 : // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11 : // simple alias analysis implemented in terms of ScalarEvolution queries.
12 : //
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.
16 : //
17 : // ScalarEvolution has a more complete understanding of pointer arithmetic
18 : // than BasicAliasAnalysis' collection of ad-hoc analyses.
19 : //
20 : //===----------------------------------------------------------------------===//
21 :
22 : #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
23 : using namespace llvm;
24 :
25 180 : AliasResult SCEVAAResult::alias(const MemoryLocation &LocA,
26 : const MemoryLocation &LocB) {
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 180 : if (LocA.Size == 0 || LocB.Size == 0)
31 : return NoAlias;
32 :
33 : // This is SCEVAAResult. Get the SCEVs!
34 180 : const SCEV *AS = SE.getSCEV(const_cast<Value *>(LocA.Ptr));
35 180 : const SCEV *BS = SE.getSCEV(const_cast<Value *>(LocB.Ptr));
36 :
37 : // If they evaluate to the same expression, it's a MustAlias.
38 180 : 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 184 : if (SE.getEffectiveSCEVType(AS->getType()) ==
44 92 : SE.getEffectiveSCEVType(BS->getType())) {
45 92 : unsigned BitWidth = SE.getTypeSizeInBits(AS->getType());
46 92 : APInt ASizeInt(BitWidth, LocA.Size.hasValue()
47 : ? LocA.Size.getValue()
48 92 : : MemoryLocation::UnknownSize);
49 92 : APInt BSizeInt(BitWidth, LocB.Size.hasValue()
50 : ? LocB.Size.getValue()
51 92 : : MemoryLocation::UnknownSize);
52 :
53 : // Compute the difference between the two pointers.
54 92 : 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 334 : if (ASizeInt.ule(SE.getUnsignedRange(BA).getUnsignedMin()) &&
60 382 : (-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 66 : 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 200 : if (BSizeInt.ule(SE.getUnsignedRange(AB).getUnsignedMin()) &&
74 76 : (-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 64 : Value *AO = GetBaseValue(AS);
82 64 : Value *BO = GetBaseValue(BS);
83 64 : if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
84 56 : if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
85 : AO ? +MemoryLocation::UnknownSize : LocA.Size,
86 56 : AO ? AAMDNodes() : LocA.AATags),
87 112 : MemoryLocation(BO ? BO : LocB.Ptr,
88 : BO ? +MemoryLocation::UnknownSize : LocB.Size,
89 56 : BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
90 : return NoAlias;
91 :
92 : // Forward the query to the next analysis.
93 64 : return AAResultBase::alias(LocA, LocB);
94 : }
95 :
96 : /// Given an expression, try to find a base value.
97 : ///
98 : /// Returns null if none was found.
99 128 : 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
102 : // than the step.
103 88 : 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 56 : const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
107 56 : if (Last->getType()->isPointerTy())
108 : return GetBaseValue(Last);
109 128 : } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
110 : // This is a leaf node.
111 128 : return U->getValue();
112 : }
113 : // No Identified object found.
114 : return nullptr;
115 : }
116 :
117 : AnalysisKey SCEVAA::Key;
118 :
119 6 : SCEVAAResult SCEVAA::run(Function &F, FunctionAnalysisManager &AM) {
120 6 : return SCEVAAResult(AM.getResult<ScalarEvolutionAnalysis>(F));
121 : }
122 :
123 : char SCEVAAWrapperPass::ID = 0;
124 85402 : INITIALIZE_PASS_BEGIN(SCEVAAWrapperPass, "scev-aa",
125 : "ScalarEvolution-based Alias Analysis", false, true)
126 85402 : INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass)
127 735305 : INITIALIZE_PASS_END(SCEVAAWrapperPass, "scev-aa",
128 : "ScalarEvolution-based Alias Analysis", false, true)
129 :
130 0 : FunctionPass *llvm::createSCEVAAWrapperPass() {
131 0 : return new SCEVAAWrapperPass();
132 : }
133 :
134 16 : SCEVAAWrapperPass::SCEVAAWrapperPass() : FunctionPass(ID) {
135 8 : initializeSCEVAAWrapperPassPass(*PassRegistry::getPassRegistry());
136 8 : }
137 :
138 13 : bool SCEVAAWrapperPass::runOnFunction(Function &F) {
139 : Result.reset(
140 13 : new SCEVAAResult(getAnalysis<ScalarEvolutionWrapperPass>().getSE()));
141 13 : return false;
142 : }
143 :
144 8 : void SCEVAAWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const {
145 : AU.setPreservesAll();
146 : AU.addRequired<ScalarEvolutionWrapperPass>();
147 8 : }
|