1 //===- ScalarEvolutionAliasAnalysis.cpp - SCEV-based Alias Analysis -------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the ScalarEvolutionAliasAnalysis pass, which implements a
11 // simple alias analysis implemented in terms of ScalarEvolution queries.
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.
17 // ScalarEvolution has a more complete understanding of pointer arithmetic
18 // than BasicAliasAnalysis' collection of ad-hoc analyses.
20 //===----------------------------------------------------------------------===//
22 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
25 // Register this pass...
26 char ScalarEvolutionAliasAnalysis::ID = 0;
27 INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
28 "ScalarEvolution-based Alias Analysis", false, true,
30 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
31 INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
32 "ScalarEvolution-based Alias Analysis", false, true,
35 FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
36 return new ScalarEvolutionAliasAnalysis();
39 void ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
40 AU.addRequiredTransitive<ScalarEvolution>();
42 AliasAnalysis::getAnalysisUsage(AU);
45 bool ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
46 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
47 SE = &getAnalysis<ScalarEvolution>();
51 /// GetBaseValue - Given an expression, try to find a
52 /// base value. Return null is none was found.
53 Value *ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
54 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
55 // In an addrec, assume that the base will be in the start, rather
57 return GetBaseValue(AR->getStart());
58 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
59 // If there's a pointer operand, it'll be sorted at the end of the list.
60 const SCEV *Last = A->getOperand(A->getNumOperands() - 1);
61 if (Last->getType()->isPointerTy())
62 return GetBaseValue(Last);
63 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
64 // This is a leaf node.
67 // No Identified object found.
71 AliasResult ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
72 const MemoryLocation &LocB) {
73 // If either of the memory references is empty, it doesn't matter what the
74 // pointer values are. This allows the code below to ignore this special
76 if (LocA.Size == 0 || LocB.Size == 0)
79 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
80 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
81 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
83 // If they evaluate to the same expression, it's a MustAlias.
87 // If something is known about the difference between the two addresses,
88 // see if it's enough to prove a NoAlias.
89 if (SE->getEffectiveSCEVType(AS->getType()) ==
90 SE->getEffectiveSCEVType(BS->getType())) {
91 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
92 APInt ASizeInt(BitWidth, LocA.Size);
93 APInt BSizeInt(BitWidth, LocB.Size);
95 // Compute the difference between the two pointers.
96 const SCEV *BA = SE->getMinusSCEV(BS, AS);
98 // Test whether the difference is known to be great enough that memory of
99 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
100 // are non-zero, which is special-cased above.
101 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
102 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
105 // Folding the subtraction while preserving range information can be tricky
106 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
107 // and try again to see if things fold better that way.
109 // Compute the difference between the two pointers.
110 const SCEV *AB = SE->getMinusSCEV(AS, BS);
112 // Test whether the difference is known to be great enough that memory of
113 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
114 // are non-zero, which is special-cased above.
115 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
116 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
120 // If ScalarEvolution can find an underlying object, form a new query.
121 // The correctness of this depends on ScalarEvolution not recognizing
122 // inttoptr and ptrtoint operators.
123 Value *AO = GetBaseValue(AS);
124 Value *BO = GetBaseValue(BS);
125 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
126 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
127 AO ? +MemoryLocation::UnknownSize : LocA.Size,
128 AO ? AAMDNodes() : LocA.AATags),
129 MemoryLocation(BO ? BO : LocB.Ptr,
130 BO ? +MemoryLocation::UnknownSize : LocB.Size,
131 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
134 // Forward the query to the next analysis.
135 return AliasAnalysis::alias(LocA, LocB);