blob: f009328854b17a0d59e1bfb969c835ec84c5ee1d [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 Anderson2ab36d32010-10-12 19:48:12 +000060INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Owen Andersonce665bd2010-10-07 22:25:06 +000061 "ScalarEvolution-based Alias Analysis", false, true, false)
Owen Anderson2ab36d32010-10-12 19:48:12 +000062INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
63INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
64 "ScalarEvolution-based Alias Analysis", false, true, false)
Dan Gohman2385e0e2009-08-26 14:53:06 +000065
66FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
67 return new ScalarEvolutionAliasAnalysis();
68}
69
70void
71ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
72 AU.addRequiredTransitive<ScalarEvolution>();
73 AU.setPreservesAll();
74 AliasAnalysis::getAnalysisUsage(AU);
75}
76
77bool
78ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
79 InitializeAliasAnalysis(this);
80 SE = &getAnalysis<ScalarEvolution>();
81 return false;
82}
83
Dan Gohman576fd762009-10-31 14:32:25 +000084/// GetBaseValue - Given an expression, try to find a
85/// base value. Return null is none was found.
Dan Gohman2385e0e2009-08-26 14:53:06 +000086Value *
Dan Gohman576fd762009-10-31 14:32:25 +000087ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohman2385e0e2009-08-26 14:53:06 +000088 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohmaned77e522009-08-29 23:36:57 +000089 // In an addrec, assume that the base will be in the start, rather
90 // than the step.
Dan Gohman576fd762009-10-31 14:32:25 +000091 return GetBaseValue(AR->getStart());
Dan Gohman2385e0e2009-08-26 14:53:06 +000092 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
93 // If there's a pointer operand, it'll be sorted at the end of the list.
94 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands1df98592010-02-16 11:11:14 +000095 if (Last->getType()->isPointerTy())
Dan Gohman576fd762009-10-31 14:32:25 +000096 return GetBaseValue(Last);
Dan Gohman2385e0e2009-08-26 14:53:06 +000097 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohman576fd762009-10-31 14:32:25 +000098 // This is a leaf node.
99 return U->getValue();
Dan Gohman2385e0e2009-08-26 14:53:06 +0000100 }
101 // No Identified object found.
102 return 0;
103}
104
105AliasAnalysis::AliasResult
Dan Gohmanb2143b62010-09-14 21:25:10 +0000106ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
107 const Location &LocB) {
Dan Gohman49bda912010-06-30 06:12:16 +0000108 // If either of the memory references is empty, it doesn't matter what the
109 // pointer values are. This allows the code below to ignore this special
110 // case.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000111 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman49bda912010-06-30 06:12:16 +0000112 return NoAlias;
113
Dan Gohman2385e0e2009-08-26 14:53:06 +0000114 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohmanb2143b62010-09-14 21:25:10 +0000115 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
116 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohman2385e0e2009-08-26 14:53:06 +0000117
118 // If they evaluate to the same expression, it's a MustAlias.
119 if (AS == BS) return MustAlias;
120
121 // If something is known about the difference between the two addresses,
122 // see if it's enough to prove a NoAlias.
123 if (SE->getEffectiveSCEVType(AS->getType()) ==
124 SE->getEffectiveSCEVType(BS->getType())) {
125 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohmanb2143b62010-09-14 21:25:10 +0000126 APInt ASizeInt(BitWidth, LocA.Size);
127 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman49bda912010-06-30 06:12:16 +0000128
129 // Compute the difference between the two pointers.
Dan Gohman2385e0e2009-08-26 14:53:06 +0000130 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman49bda912010-06-30 06:12:16 +0000131
132 // Test whether the difference is known to be great enough that memory of
133 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
134 // are non-zero, which is special-cased above.
135 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
136 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
137 return NoAlias;
138
139 // Folding the subtraction while preserving range information can be tricky
140 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
141 // and try again to see if things fold better that way.
142
143 // Compute the difference between the two pointers.
144 const SCEV *AB = SE->getMinusSCEV(AS, BS);
145
146 // Test whether the difference is known to be great enough that memory of
147 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
148 // are non-zero, which is special-cased above.
149 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
150 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
151 return NoAlias;
Dan Gohman2385e0e2009-08-26 14:53:06 +0000152 }
153
154 // If ScalarEvolution can find an underlying object, form a new query.
155 // The correctness of this depends on ScalarEvolution not recognizing
156 // inttoptr and ptrtoint operators.
Dan Gohman576fd762009-10-31 14:32:25 +0000157 Value *AO = GetBaseValue(AS);
158 Value *BO = GetBaseValue(BS);
Dan Gohmanb2143b62010-09-14 21:25:10 +0000159 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
160 if (alias(Location(AO ? AO : LocA.Ptr,
161 AO ? +UnknownSize : LocA.Size,
162 AO ? 0 : LocA.TBAATag),
163 Location(BO ? BO : LocB.Ptr,
164 BO ? +UnknownSize : LocB.Size,
165 BO ? 0 : LocB.TBAATag)) == NoAlias)
Dan Gohman2385e0e2009-08-26 14:53:06 +0000166 return NoAlias;
167
168 // Forward the query to the next analysis.
Dan Gohmanb2143b62010-09-14 21:25:10 +0000169 return AliasAnalysis::alias(LocA, LocB);
Dan Gohman2385e0e2009-08-26 14:53:06 +0000170}