blob: d7ecdde3e6c74628f6cb67476dfbb9c1e68278f6 [file] [log] [blame]
Dan Gohman2385e0e2009-08-26 14:53:06 +00001//===- 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//
Dan Gohman7cbf5a82010-03-01 17:56:04 +000013// This differs from traditional loop dependence analysis in that it tests
14// for dependencies within a single iteration of a loop, rather than
Dan Gohman4f46e142010-06-18 00:53:08 +000015// dependencies between different iterations.
Dan Gohman7cbf5a82010-03-01 17:56:04 +000016//
Dan Gohman2385e0e2009-08-26 14:53:06 +000017// ScalarEvolution has a more complete understanding of pointer arithmetic
18// than BasicAliasAnalysis' collection of ad-hoc analyses.
19//
20//===----------------------------------------------------------------------===//
21
22#include "llvm/Analysis/AliasAnalysis.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
24#include "llvm/Analysis/Passes.h"
25#include "llvm/Pass.h"
Dan Gohman2385e0e2009-08-26 14:53:06 +000026using namespace llvm;
27
28namespace {
29 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
30 /// implementation that uses ScalarEvolution to answer queries.
Nick Lewycky6726b6d2009-10-25 06:33:48 +000031 class ScalarEvolutionAliasAnalysis : public FunctionPass,
32 public AliasAnalysis {
Dan Gohman2385e0e2009-08-26 14:53:06 +000033 ScalarEvolution *SE;
34
35 public:
36 static char ID; // Class identification, replacement for typeinfo
Owen Anderson90c579d2010-08-06 18:33:48 +000037 ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) {}
Dan Gohman2385e0e2009-08-26 14:53:06 +000038
Chris Lattner1bc76d42010-01-20 20:09:02 +000039 /// getAdjustedAnalysisPointer - This method is used when a pass implements
40 /// an analysis interface through multiple inheritance. If needed, it
41 /// should override this to adjust the this pointer as needed for the
42 /// specified pass info.
Owen Anderson90c579d2010-08-06 18:33:48 +000043 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
44 if (PI == &AliasAnalysis::ID)
Chris Lattner1bc76d42010-01-20 20:09:02 +000045 return (AliasAnalysis*)this;
46 return this;
47 }
Dan Gohman7cbf5a82010-03-01 17:56:04 +000048
Dan Gohman2385e0e2009-08-26 14:53:06 +000049 private:
50 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
51 virtual bool runOnFunction(Function &F);
Dan Gohmanb2143b62010-09-14 21:25:10 +000052 virtual AliasResult alias(const Location &LocA, const Location &LocB);
Dan Gohman2385e0e2009-08-26 14:53:06 +000053
Dan Gohman576fd762009-10-31 14:32:25 +000054 Value *GetBaseValue(const SCEV *S);
Dan Gohman2385e0e2009-08-26 14:53:06 +000055 };
56} // End of anonymous namespace
57
58// Register this pass...
59char ScalarEvolutionAliasAnalysis::ID = 0;
Owen Andersond8cc7be2010-07-21 23:07:00 +000060INITIALIZE_AG_PASS(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
61 "ScalarEvolution-based Alias Analysis", false, true, false);
Dan Gohman2385e0e2009-08-26 14:53:06 +000062
63FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
64 return new ScalarEvolutionAliasAnalysis();
65}
66
67void
68ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
69 AU.addRequiredTransitive<ScalarEvolution>();
70 AU.setPreservesAll();
71 AliasAnalysis::getAnalysisUsage(AU);
72}
73
74bool
75ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
76 InitializeAliasAnalysis(this);
77 SE = &getAnalysis<ScalarEvolution>();
78 return false;
79}
80
Dan Gohman576fd762009-10-31 14:32:25 +000081/// GetBaseValue - Given an expression, try to find a
82/// base value. Return null is none was found.
Dan Gohman2385e0e2009-08-26 14:53:06 +000083Value *
Dan Gohman576fd762009-10-31 14:32:25 +000084ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohman2385e0e2009-08-26 14:53:06 +000085 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmaned77e522009-08-29 23:36:57 +000086 // In an addrec, assume that the base will be in the start, rather
87 // than the step.
Dan Gohman576fd762009-10-31 14:32:25 +000088 return GetBaseValue(AR->getStart());
Dan Gohman2385e0e2009-08-26 14:53:06 +000089 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
90 // If there's a pointer operand, it'll be sorted at the end of the list.
91 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands1df98592010-02-16 11:11:14 +000092 if (Last->getType()->isPointerTy())
Dan Gohman576fd762009-10-31 14:32:25 +000093 return GetBaseValue(Last);
Dan Gohman2385e0e2009-08-26 14:53:06 +000094 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohman576fd762009-10-31 14:32:25 +000095 // This is a leaf node.
96 return U->getValue();
Dan Gohman2385e0e2009-08-26 14:53:06 +000097 }
98 // No Identified object found.
99 return 0;
100}
101
102AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000103ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
104 const Location &LocB) {
Dan Gohman49bda912010-06-30 06:12:16 +0000105 // If either of the memory references is empty, it doesn't matter what the
106 // pointer values are. This allows the code below to ignore this special
107 // case.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000108 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman49bda912010-06-30 06:12:16 +0000109 return NoAlias;
110
Dan Gohman2385e0e2009-08-26 14:53:06 +0000111 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohmanb2143b62010-09-14 21:25:10 +0000112 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
113 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohman2385e0e2009-08-26 14:53:06 +0000114
115 // If they evaluate to the same expression, it's a MustAlias.
116 if (AS == BS) return MustAlias;
117
118 // If something is known about the difference between the two addresses,
119 // see if it's enough to prove a NoAlias.
120 if (SE->getEffectiveSCEVType(AS->getType()) ==
121 SE->getEffectiveSCEVType(BS->getType())) {
122 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohmanb2143b62010-09-14 21:25:10 +0000123 APInt ASizeInt(BitWidth, LocA.Size);
124 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman49bda912010-06-30 06:12:16 +0000125
126 // Compute the difference between the two pointers.
Dan Gohman2385e0e2009-08-26 14:53:06 +0000127 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman49bda912010-06-30 06:12:16 +0000128
129 // Test whether the difference is known to be great enough that memory of
130 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
131 // are non-zero, which is special-cased above.
132 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
133 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
134 return NoAlias;
135
136 // Folding the subtraction while preserving range information can be tricky
137 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
138 // and try again to see if things fold better that way.
139
140 // Compute the difference between the two pointers.
141 const SCEV *AB = SE->getMinusSCEV(AS, BS);
142
143 // Test whether the difference is known to be great enough that memory of
144 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
145 // are non-zero, which is special-cased above.
146 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
147 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
148 return NoAlias;
Dan Gohman2385e0e2009-08-26 14:53:06 +0000149 }
150
151 // If ScalarEvolution can find an underlying object, form a new query.
152 // The correctness of this depends on ScalarEvolution not recognizing
153 // inttoptr and ptrtoint operators.
Dan Gohman576fd762009-10-31 14:32:25 +0000154 Value *AO = GetBaseValue(AS);
155 Value *BO = GetBaseValue(BS);
Dan Gohmanb2143b62010-09-14 21:25:10 +0000156 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
157 if (alias(Location(AO ? AO : LocA.Ptr,
158 AO ? +UnknownSize : LocA.Size,
159 AO ? 0 : LocA.TBAATag),
160 Location(BO ? BO : LocB.Ptr,
161 BO ? +UnknownSize : LocB.Size,
162 BO ? 0 : LocB.TBAATag)) == NoAlias)
Dan Gohman2385e0e2009-08-26 14:53:06 +0000163 return NoAlias;
164
165 // Forward the query to the next analysis.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000166 return AliasAnalysis::alias(LocA, LocB);
Dan Gohman2385e0e2009-08-26 14:53:06 +0000167}