blob: ccec0a877f5a460ab89183ff7de38a1579e0d441 [file] [log] [blame]
Dan Gohmand926b982009-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 Gohman904d34c2010-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 Gohman8ba26b42010-06-18 00:53:08 +000015// dependencies between different iterations.
Dan Gohman904d34c2010-03-01 17:56:04 +000016//
Dan Gohmand926b982009-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
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/Passes.h"
Dan Gohmand926b982009-08-26 14:53:06 +000023#include "llvm/Analysis/AliasAnalysis.h"
24#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Mehdi Amini46a43552015-03-04 18:43:29 +000025#include "llvm/IR/Module.h"
Dan Gohmand926b982009-08-26 14:53:06 +000026#include "llvm/Pass.h"
Dan Gohmand926b982009-08-26 14:53:06 +000027using namespace llvm;
28
29namespace {
30 /// ScalarEvolutionAliasAnalysis - This is a simple alias analysis
31 /// implementation that uses ScalarEvolution to answer queries.
Nick Lewycky02d5f772009-10-25 06:33:48 +000032 class ScalarEvolutionAliasAnalysis : public FunctionPass,
33 public AliasAnalysis {
Dan Gohmand926b982009-08-26 14:53:06 +000034 ScalarEvolution *SE;
35
36 public:
37 static char ID; // Class identification, replacement for typeinfo
Craig Topper9f008862014-04-15 04:59:12 +000038 ScalarEvolutionAliasAnalysis() : FunctionPass(ID), SE(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000039 initializeScalarEvolutionAliasAnalysisPass(
40 *PassRegistry::getPassRegistry());
41 }
Dan Gohmand926b982009-08-26 14:53:06 +000042
Chris Lattnerda363d92010-01-20 20:09:02 +000043 /// getAdjustedAnalysisPointer - This method is used when a pass implements
44 /// an analysis interface through multiple inheritance. If needed, it
45 /// should override this to adjust the this pointer as needed for the
46 /// specified pass info.
Craig Toppere9ba7592014-03-05 07:30:04 +000047 void *getAdjustedAnalysisPointer(AnalysisID PI) override {
Owen Andersona7aed182010-08-06 18:33:48 +000048 if (PI == &AliasAnalysis::ID)
Chris Lattnerda363d92010-01-20 20:09:02 +000049 return (AliasAnalysis*)this;
50 return this;
51 }
Dan Gohman904d34c2010-03-01 17:56:04 +000052
Dan Gohmand926b982009-08-26 14:53:06 +000053 private:
Craig Toppere9ba7592014-03-05 07:30:04 +000054 void getAnalysisUsage(AnalysisUsage &AU) const override;
55 bool runOnFunction(Function &F) override;
56 AliasResult alias(const Location &LocA, const Location &LocB) override;
Dan Gohmand926b982009-08-26 14:53:06 +000057
Dan Gohmanac45c912009-10-31 14:32:25 +000058 Value *GetBaseValue(const SCEV *S);
Dan Gohmand926b982009-08-26 14:53:06 +000059 };
60} // End of anonymous namespace
61
62// Register this pass...
63char ScalarEvolutionAliasAnalysis::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000064INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Owen Andersondf7a4f22010-10-07 22:25:06 +000065 "ScalarEvolution-based Alias Analysis", false, true, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000066INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
67INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
68 "ScalarEvolution-based Alias Analysis", false, true, false)
Dan Gohmand926b982009-08-26 14:53:06 +000069
70FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
71 return new ScalarEvolutionAliasAnalysis();
72}
73
74void
75ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
76 AU.addRequiredTransitive<ScalarEvolution>();
77 AU.setPreservesAll();
78 AliasAnalysis::getAnalysisUsage(AU);
79}
80
81bool
82ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
Mehdi Amini46a43552015-03-04 18:43:29 +000083 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
Dan Gohmand926b982009-08-26 14:53:06 +000084 SE = &getAnalysis<ScalarEvolution>();
85 return false;
86}
87
Dan Gohmanac45c912009-10-31 14:32:25 +000088/// GetBaseValue - Given an expression, try to find a
89/// base value. Return null is none was found.
Dan Gohmand926b982009-08-26 14:53:06 +000090Value *
Dan Gohmanac45c912009-10-31 14:32:25 +000091ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohmand926b982009-08-26 14:53:06 +000092 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohman311d0692009-08-29 23:36:57 +000093 // In an addrec, assume that the base will be in the start, rather
94 // than the step.
Dan Gohmanac45c912009-10-31 14:32:25 +000095 return GetBaseValue(AR->getStart());
Dan Gohmand926b982009-08-26 14:53:06 +000096 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
97 // If there's a pointer operand, it'll be sorted at the end of the list.
98 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands19d0b472010-02-16 11:11:14 +000099 if (Last->getType()->isPointerTy())
Dan Gohmanac45c912009-10-31 14:32:25 +0000100 return GetBaseValue(Last);
Dan Gohmand926b982009-08-26 14:53:06 +0000101 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohmanac45c912009-10-31 14:32:25 +0000102 // This is a leaf node.
103 return U->getValue();
Dan Gohmand926b982009-08-26 14:53:06 +0000104 }
105 // No Identified object found.
Craig Topper9f008862014-04-15 04:59:12 +0000106 return nullptr;
Dan Gohmand926b982009-08-26 14:53:06 +0000107}
108
109AliasAnalysis::AliasResult
Dan Gohman41f14cf2010-09-14 21:25:10 +0000110ScalarEvolutionAliasAnalysis::alias(const Location &LocA,
111 const Location &LocB) {
Dan Gohman08659662010-06-30 06:12:16 +0000112 // If either of the memory references is empty, it doesn't matter what the
113 // pointer values are. This allows the code below to ignore this special
114 // case.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000115 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman08659662010-06-30 06:12:16 +0000116 return NoAlias;
117
Dan Gohmand926b982009-08-26 14:53:06 +0000118 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohman41f14cf2010-09-14 21:25:10 +0000119 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
120 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +0000121
122 // If they evaluate to the same expression, it's a MustAlias.
123 if (AS == BS) return MustAlias;
124
125 // If something is known about the difference between the two addresses,
126 // see if it's enough to prove a NoAlias.
127 if (SE->getEffectiveSCEVType(AS->getType()) ==
128 SE->getEffectiveSCEVType(BS->getType())) {
129 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohman41f14cf2010-09-14 21:25:10 +0000130 APInt ASizeInt(BitWidth, LocA.Size);
131 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman08659662010-06-30 06:12:16 +0000132
133 // Compute the difference between the two pointers.
Dan Gohmand926b982009-08-26 14:53:06 +0000134 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +0000135
136 // Test whether the difference is known to be great enough that memory of
137 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
138 // are non-zero, which is special-cased above.
139 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
140 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
141 return NoAlias;
142
143 // Folding the subtraction while preserving range information can be tricky
144 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
145 // and try again to see if things fold better that way.
146
147 // Compute the difference between the two pointers.
148 const SCEV *AB = SE->getMinusSCEV(AS, BS);
149
150 // Test whether the difference is known to be great enough that memory of
151 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
152 // are non-zero, which is special-cased above.
153 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
154 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
155 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +0000156 }
157
158 // If ScalarEvolution can find an underlying object, form a new query.
159 // The correctness of this depends on ScalarEvolution not recognizing
160 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +0000161 Value *AO = GetBaseValue(AS);
162 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +0000163 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
164 if (alias(Location(AO ? AO : LocA.Ptr,
165 AO ? +UnknownSize : LocA.Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000166 AO ? AAMDNodes() : LocA.AATags),
Dan Gohman41f14cf2010-09-14 21:25:10 +0000167 Location(BO ? BO : LocB.Ptr,
168 BO ? +UnknownSize : LocB.Size,
Hal Finkelcc39b672014-07-24 12:16:19 +0000169 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +0000170 return NoAlias;
171
172 // Forward the query to the next analysis.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000173 return AliasAnalysis::alias(LocA, LocB);
Dan Gohmand926b982009-08-26 14:53:06 +0000174}