blob: 6bc0d85a61f986913dd2552427d0bfc5c71beade [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;
Chandler Carruthac80dc72015-06-17 07:18:54 +000056 AliasResult alias(const MemoryLocation &LocA,
57 const MemoryLocation &LocB) override;
Dan Gohmand926b982009-08-26 14:53:06 +000058
Dan Gohmanac45c912009-10-31 14:32:25 +000059 Value *GetBaseValue(const SCEV *S);
Dan Gohmand926b982009-08-26 14:53:06 +000060 };
61} // End of anonymous namespace
62
63// Register this pass...
64char ScalarEvolutionAliasAnalysis::ID = 0;
Owen Anderson8ac477f2010-10-12 19:48:12 +000065INITIALIZE_AG_PASS_BEGIN(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
Owen Andersondf7a4f22010-10-07 22:25:06 +000066 "ScalarEvolution-based Alias Analysis", false, true, false)
Owen Anderson8ac477f2010-10-12 19:48:12 +000067INITIALIZE_PASS_DEPENDENCY(ScalarEvolution)
68INITIALIZE_AG_PASS_END(ScalarEvolutionAliasAnalysis, AliasAnalysis, "scev-aa",
69 "ScalarEvolution-based Alias Analysis", false, true, false)
Dan Gohmand926b982009-08-26 14:53:06 +000070
71FunctionPass *llvm::createScalarEvolutionAliasAnalysisPass() {
72 return new ScalarEvolutionAliasAnalysis();
73}
74
75void
76ScalarEvolutionAliasAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
77 AU.addRequiredTransitive<ScalarEvolution>();
78 AU.setPreservesAll();
79 AliasAnalysis::getAnalysisUsage(AU);
80}
81
82bool
83ScalarEvolutionAliasAnalysis::runOnFunction(Function &F) {
Mehdi Amini46a43552015-03-04 18:43:29 +000084 InitializeAliasAnalysis(this, &F.getParent()->getDataLayout());
Dan Gohmand926b982009-08-26 14:53:06 +000085 SE = &getAnalysis<ScalarEvolution>();
86 return false;
87}
88
Dan Gohmanac45c912009-10-31 14:32:25 +000089/// GetBaseValue - Given an expression, try to find a
90/// base value. Return null is none was found.
Dan Gohmand926b982009-08-26 14:53:06 +000091Value *
Dan Gohmanac45c912009-10-31 14:32:25 +000092ScalarEvolutionAliasAnalysis::GetBaseValue(const SCEV *S) {
Dan Gohmand926b982009-08-26 14:53:06 +000093 if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(S)) {
Dan Gohman311d0692009-08-29 23:36:57 +000094 // In an addrec, assume that the base will be in the start, rather
95 // than the step.
Dan Gohmanac45c912009-10-31 14:32:25 +000096 return GetBaseValue(AR->getStart());
Dan Gohmand926b982009-08-26 14:53:06 +000097 } else if (const SCEVAddExpr *A = dyn_cast<SCEVAddExpr>(S)) {
98 // If there's a pointer operand, it'll be sorted at the end of the list.
99 const SCEV *Last = A->getOperand(A->getNumOperands()-1);
Duncan Sands19d0b472010-02-16 11:11:14 +0000100 if (Last->getType()->isPointerTy())
Dan Gohmanac45c912009-10-31 14:32:25 +0000101 return GetBaseValue(Last);
Dan Gohmand926b982009-08-26 14:53:06 +0000102 } else if (const SCEVUnknown *U = dyn_cast<SCEVUnknown>(S)) {
Dan Gohmanac45c912009-10-31 14:32:25 +0000103 // This is a leaf node.
104 return U->getValue();
Dan Gohmand926b982009-08-26 14:53:06 +0000105 }
106 // No Identified object found.
Craig Topper9f008862014-04-15 04:59:12 +0000107 return nullptr;
Dan Gohmand926b982009-08-26 14:53:06 +0000108}
109
Chandler Carruthc3f49eb2015-06-22 02:16:51 +0000110AliasResult ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
111 const MemoryLocation &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))
Chandler Carruthecbd1682015-06-17 07:21:38 +0000164 if (alias(MemoryLocation(AO ? AO : LocA.Ptr,
165 AO ? +MemoryLocation::UnknownSize : LocA.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +0000166 AO ? AAMDNodes() : LocA.AATags),
Chandler Carruthecbd1682015-06-17 07:21:38 +0000167 MemoryLocation(BO ? BO : LocB.Ptr,
168 BO ? +MemoryLocation::UnknownSize : LocB.Size,
Chandler Carruthac80dc72015-06-17 07:18:54 +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}