blob: e9edb3e083def8e4160e33cfa3bb10761226bb41 [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 Anderson081c34b2010-10-19 17:21:58 +000037 ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(0) {
38 initializeScalarEvolutionAliasAnalysisPass(
39 *PassRegistry::getPassRegistry());
40 }
Dan Gohman2385e0e2009-08-26 14:53:06 +000041
Chris Lattner1bc76d42010-01-20 20:09:02 +000042 /// getAdjustedAnalysisPointer - This method is used when a pass implements
43 /// an analysis interface through multiple inheritance. If needed, it
44 /// should override this to adjust the this pointer as needed for the
45 /// specified pass info.
Owen Anderson90c579d2010-08-06 18:33:48 +000046 virtual void *getAdjustedAnalysisPointer(AnalysisID PI) {
47 if (PI == &AliasAnalysis::ID)
Chris Lattner1bc76d42010-01-20 20:09:02 +000048 return (AliasAnalysis*)this;
49 return this;
50 }
Dan Gohman7cbf5a82010-03-01 17:56:04 +000051
Dan Gohman2385e0e2009-08-26 14:53:06 +000052 private:
53 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
54 virtual bool runOnFunction(Function &F);
Dan Gohmanb2143b62010-09-14 21:25:10 +000055 virtual AliasResult alias(const Location &LocA, const Location &LocB);
Dan Gohman2385e0e2009-08-26 14:53:06 +000056
Dan Gohman576fd762009-10-31 14:32:25 +000057 Value *GetBaseValue(const SCEV *S);
Dan Gohman2385e0e2009-08-26 14:53:06 +000058 };
59} // End of anonymous namespace
60
61// Register this pass...
62char ScalarEvolutionAliasAnalysis::ID = 0;
Owen Anderson2ab36d32010-10-12 19:48:12 +000063INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Owen Andersonce665bd2010-10-07 22:25:06 +000064 "ScalarEvolution-based Alias Analysis", false, true, false)
Owen Anderson2ab36d32010-10-12 19:48:12 +000065INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
66INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
67 "ScalarEvolution-based Alias Analysis", false, true, false)
Dan Gohman2385e0e2009-08-26 14:53:06 +000068
69FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
70 return new ScalarEvolutionAliasAnalysis();
71}
72
73void
74ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
75 AU.addRequiredTransitive<ScalarEvolution>();
76 AU.setPreservesAll();
77 AliasAnalysis::getAnalysisUsage(AU);
78}
79
80bool
81ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
82 InitializeAliasAnalysis(this);
83 SE = &getAnalysis<ScalarEvolution>();
84 return false;
85}
86
Dan Gohman576fd762009-10-31 14:32:25 +000087/// GetBaseValue - Given an expression, try to find a
88/// base value. Return null is none was found.
Dan Gohman2385e0e2009-08-26 14:53:06 +000089Value *
Dan Gohman576fd762009-10-31 14:32:25 +000090ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohman2385e0e2009-08-26 14:53:06 +000091 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmaned77e522009-08-29 23:36:57 +000092 // In an addrec, assume that the base will be in the start, rather
93 // than the step.
Dan Gohman576fd762009-10-31 14:32:25 +000094 return GetBaseValue(AR->getStart());
Dan Gohman2385e0e2009-08-26 14:53:06 +000095 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
96 // If there's a pointer operand, it'll be sorted at the end of the list.
97 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands1df98592010-02-16 11:11:14 +000098 if (Last->getType()->isPointerTy())
Dan Gohman576fd762009-10-31 14:32:25 +000099 return GetBaseValue(Last);
Dan Gohman2385e0e2009-08-26 14:53:06 +0000100 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohman576fd762009-10-31 14:32:25 +0000101 // This is a leaf node.
102 return U->getValue();
Dan Gohman2385e0e2009-08-26 14:53:06 +0000103 }
104 // No Identified object found.
105 return 0;
106}
107
108AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000109ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
110 const Location &LocB) {
Dan Gohman49bda912010-06-30 06:12:16 +0000111 // If either of the memory references is empty, it doesn't matter what the
112 // pointer values are. This allows the code below to ignore this special
113 // case.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000114 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman49bda912010-06-30 06:12:16 +0000115 return NoAlias;
116
Dan Gohman2385e0e2009-08-26 14:53:06 +0000117 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohmanb2143b62010-09-14 21:25:10 +0000118 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
119 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohman2385e0e2009-08-26 14:53:06 +0000120
121 // If they evaluate to the same expression, it's a MustAlias.
122 if (AS == BS) return MustAlias;
123
124 // If something is known about the difference between the two addresses,
125 // see if it's enough to prove a NoAlias.
126 if (SE->getEffectiveSCEVType(AS->getType()) ==
127 SE->getEffectiveSCEVType(BS->getType())) {
128 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohmanb2143b62010-09-14 21:25:10 +0000129 APInt ASizeInt(BitWidth, LocA.Size);
130 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman49bda912010-06-30 06:12:16 +0000131
132 // Compute the difference between the two pointers.
Dan Gohman2385e0e2009-08-26 14:53:06 +0000133 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman49bda912010-06-30 06:12:16 +0000134
135 // Test whether the difference is known to be great enough that memory of
136 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
137 // are non-zero, which is special-cased above.
138 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
139 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
140 return NoAlias;
141
142 // Folding the subtraction while preserving range information can be tricky
143 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
144 // and try again to see if things fold better that way.
145
146 // Compute the difference between the two pointers.
147 const SCEV *AB = SE->getMinusSCEV(AS, BS);
148
149 // Test whether the difference is known to be great enough that memory of
150 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
151 // are non-zero, which is special-cased above.
152 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
153 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
154 return NoAlias;
Dan Gohman2385e0e2009-08-26 14:53:06 +0000155 }
156
157 // If ScalarEvolution can find an underlying object, form a new query.
158 // The correctness of this depends on ScalarEvolution not recognizing
159 // inttoptr and ptrtoint operators.
Dan Gohman576fd762009-10-31 14:32:25 +0000160 Value *AO = GetBaseValue(AS);
161 Value *BO = GetBaseValue(BS);
Dan Gohmanb2143b62010-09-14 21:25:10 +0000162 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
163 if (alias(Location(AO ? AO : LocA.Ptr,
164 AO ? +UnknownSize : LocA.Size,
165 AO ? 0 : LocA.TBAATag),
166 Location(BO ? BO : LocB.Ptr,
167 BO ? +UnknownSize : LocB.Size,
168 BO ? 0 : LocB.TBAATag)) == NoAlias)
Dan Gohman2385e0e2009-08-26 14:53:06 +0000169 return NoAlias;
170
171 // Forward the query to the next analysis.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000172 return AliasAnalysis::alias(LocA, LocB);
Dan Gohman2385e0e2009-08-26 14:53:06 +0000173}