blob: 682f38b74121afbe56da283faefb9ea8185e49c3 [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
110AliasAnalysis::AliasResult
Chandler Carruthac80dc72015-06-17 07:18:54 +0000111ScalarEvolutionAliasAnalysis::alias(const MemoryLocation &LocA,
112 const MemoryLocation &LocB) {
Dan Gohman08659662010-06-30 06:12:16 +0000113 // If either of the memory references is empty, it doesn't matter what the
114 // pointer values are. This allows the code below to ignore this special
115 // case.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000116 if (LocA.Size == 0 || LocB.Size == 0)
Dan Gohman08659662010-06-30 06:12:16 +0000117 return NoAlias;
118
Dan Gohmand926b982009-08-26 14:53:06 +0000119 // This is ScalarEvolutionAliasAnalysis. Get the SCEVs!
Dan Gohman41f14cf2010-09-14 21:25:10 +0000120 const SCEV *AS = SE->getSCEV(const_cast<Value *>(LocA.Ptr));
121 const SCEV *BS = SE->getSCEV(const_cast<Value *>(LocB.Ptr));
Dan Gohmand926b982009-08-26 14:53:06 +0000122
123 // If they evaluate to the same expression, it's a MustAlias.
124 if (AS == BS) return MustAlias;
125
126 // If something is known about the difference between the two addresses,
127 // see if it's enough to prove a NoAlias.
128 if (SE->getEffectiveSCEVType(AS->getType()) ==
129 SE->getEffectiveSCEVType(BS->getType())) {
130 unsigned BitWidth = SE->getTypeSizeInBits(AS->getType());
Dan Gohman41f14cf2010-09-14 21:25:10 +0000131 APInt ASizeInt(BitWidth, LocA.Size);
132 APInt BSizeInt(BitWidth, LocB.Size);
Dan Gohman08659662010-06-30 06:12:16 +0000133
134 // Compute the difference between the two pointers.
Dan Gohmand926b982009-08-26 14:53:06 +0000135 const SCEV *BA = SE->getMinusSCEV(BS, AS);
Dan Gohman08659662010-06-30 06:12:16 +0000136
137 // Test whether the difference is known to be great enough that memory of
138 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
139 // are non-zero, which is special-cased above.
140 if (ASizeInt.ule(SE->getUnsignedRange(BA).getUnsignedMin()) &&
141 (-BSizeInt).uge(SE->getUnsignedRange(BA).getUnsignedMax()))
142 return NoAlias;
143
144 // Folding the subtraction while preserving range information can be tricky
145 // (because of INT_MIN, etc.); if the prior test failed, swap AS and BS
146 // and try again to see if things fold better that way.
147
148 // Compute the difference between the two pointers.
149 const SCEV *AB = SE->getMinusSCEV(AS, BS);
150
151 // Test whether the difference is known to be great enough that memory of
152 // the given sizes don't overlap. This assumes that ASizeInt and BSizeInt
153 // are non-zero, which is special-cased above.
154 if (BSizeInt.ule(SE->getUnsignedRange(AB).getUnsignedMin()) &&
155 (-ASizeInt).uge(SE->getUnsignedRange(AB).getUnsignedMax()))
156 return NoAlias;
Dan Gohmand926b982009-08-26 14:53:06 +0000157 }
158
159 // If ScalarEvolution can find an underlying object, form a new query.
160 // The correctness of this depends on ScalarEvolution not recognizing
161 // inttoptr and ptrtoint operators.
Dan Gohmanac45c912009-10-31 14:32:25 +0000162 Value *AO = GetBaseValue(AS);
163 Value *BO = GetBaseValue(BS);
Dan Gohman41f14cf2010-09-14 21:25:10 +0000164 if ((AO && AO != LocA.Ptr) || (BO && BO != LocB.Ptr))
Chandler Carruthac80dc72015-06-17 07:18:54 +0000165 if (alias(MemoryLocation(AO ? AO : LocA.Ptr, AO ? +UnknownSize : LocA.Size,
166 AO ? AAMDNodes() : LocA.AATags),
167 MemoryLocation(BO ? BO : LocB.Ptr, BO ? +UnknownSize : LocB.Size,
168 BO ? AAMDNodes() : LocB.AATags)) == NoAlias)
Dan Gohmand926b982009-08-26 14:53:06 +0000169 return NoAlias;
170
171 // Forward the query to the next analysis.
Dan Gohman41f14cf2010-09-14 21:25:10 +0000172 return AliasAnalysis::alias(LocA, LocB);
Dan Gohmand926b982009-08-26 14:53:06 +0000173}