blob: 0c8bbd7a568c2bc0336e53f0fe48a0dd243ba386 [file] [log] [blame]
Andreas Bolka306b5b22009-06-24 21:29:13 +00001//===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
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 is the (beginning) of an implementation of a loop dependence analysis
11// framework, which is used to detect dependences in memory accesses in loops.
12//
13// Please note that this is work in progress and the interface is subject to
14// change.
15//
16// TODO: adapt as implementation progresses.
17//
Andreas Bolka80478de2009-07-29 05:35:53 +000018// TODO: document lingo (pair, subscript, index)
19//
Andreas Bolka306b5b22009-06-24 21:29:13 +000020//===----------------------------------------------------------------------===//
21
22#define DEBUG_TYPE "lda"
Andreas Bolka43aa1e02009-07-28 19:49:49 +000023#include "llvm/ADT/Statistic.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000024#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000025#include "llvm/Analysis/LoopDependenceAnalysis.h"
26#include "llvm/Analysis/LoopPass.h"
27#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000028#include "llvm/Instructions.h"
Andreas Bolka80478de2009-07-29 05:35:53 +000029#include "llvm/Operator.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000030#include "llvm/Support/Allocator.h"
Andreas Bolka5771ed92009-06-30 02:12:10 +000031#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000032#include "llvm/Support/ErrorHandling.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000033#include "llvm/Support/raw_ostream.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000034#include "llvm/Target/TargetData.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000035using namespace llvm;
36
Andreas Bolka43aa1e02009-07-28 19:49:49 +000037STATISTIC(NumAnswered, "Number of dependence queries answered");
38STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
39STATISTIC(NumDependent, "Number of pairs with dependent accesses");
40STATISTIC(NumIndependent, "Number of pairs with independent accesses");
41STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
42
Andreas Bolka306b5b22009-06-24 21:29:13 +000043LoopPass *llvm::createLoopDependenceAnalysisPass() {
44 return new LoopDependenceAnalysis();
45}
46
47static RegisterPass<LoopDependenceAnalysis>
48R("lda", "Loop Dependence Analysis", false, true);
49char LoopDependenceAnalysis::ID = 0;
50
51//===----------------------------------------------------------------------===//
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000052// Utility Functions
53//===----------------------------------------------------------------------===//
54
Andreas Bolkafc8a04a2009-06-29 18:51:11 +000055static inline bool IsMemRefInstr(const Value *V) {
56 const Instruction *I = dyn_cast<const Instruction>(V);
57 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000058}
59
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000060static void GetMemRefInstrs(const Loop *L,
61 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolka158c5902009-06-28 00:35:22 +000062 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000063 b != be; ++b)
Andreas Bolka158c5902009-06-28 00:35:22 +000064 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000065 i != ie; ++i)
Andreas Bolka88a17f02009-06-29 00:50:26 +000066 if (IsMemRefInstr(i))
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000067 Memrefs.push_back(i);
Andreas Bolka158c5902009-06-28 00:35:22 +000068}
69
Andreas Bolka5771ed92009-06-30 02:12:10 +000070static bool IsLoadOrStoreInst(Value *I) {
71 return isa<LoadInst>(I) || isa<StoreInst>(I);
72}
73
74static Value *GetPointerOperand(Value *I) {
75 if (LoadInst *i = dyn_cast<LoadInst>(I))
76 return i->getPointerOperand();
77 if (StoreInst *i = dyn_cast<StoreInst>(I))
78 return i->getPointerOperand();
Edwin Törökbd448e32009-07-14 16:55:14 +000079 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolka5771ed92009-06-30 02:12:10 +000080 // Never reached.
81 return 0;
82}
83
Andreas Bolkae00b4942009-07-28 19:49:25 +000084static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
85 const Value *A,
86 const Value *B) {
87 const Value *aObj = A->getUnderlyingObject();
88 const Value *bObj = B->getUnderlyingObject();
89 return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
90 bObj, AA->getTypeStoreSize(bObj->getType()));
91}
92
Andreas Bolka80478de2009-07-29 05:35:53 +000093static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) {
94 return SE->getConstant(Type::Int32Ty, 0L);
95}
96
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000097//===----------------------------------------------------------------------===//
98// Dependence Testing
99//===----------------------------------------------------------------------===//
100
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000101bool LoopDependenceAnalysis::isDependencePair(const Value *A,
102 const Value *B) const {
103 return IsMemRefInstr(A) &&
104 IsMemRefInstr(B) &&
105 (cast<const Instruction>(A)->mayWriteToMemory() ||
106 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000107}
108
Andreas Bolkae00b4942009-07-28 19:49:25 +0000109bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
110 Value *B,
Andreas Bolka0a528a02009-07-23 14:32:46 +0000111 DependencePair *&P) {
112 void *insertPos = 0;
113 FoldingSetNodeID id;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000114 id.AddPointer(A);
115 id.AddPointer(B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000116
Andreas Bolka0a528a02009-07-23 14:32:46 +0000117 P = Pairs.FindNodeOrInsertPos(id, insertPos);
118 if (P) return true;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000119
Andreas Bolka0a528a02009-07-23 14:32:46 +0000120 P = PairAllocator.Allocate<DependencePair>();
Andreas Bolkae00b4942009-07-28 19:49:25 +0000121 new (P) DependencePair(id, A, B);
Andreas Bolka0a528a02009-07-23 14:32:46 +0000122 Pairs.InsertNode(P, insertPos);
123 return false;
124}
125
Andreas Bolka4e661252009-07-28 19:50:13 +0000126LoopDependenceAnalysis::DependenceResult
Andreas Bolka80478de2009-07-29 05:35:53 +0000127LoopDependenceAnalysis::analyseSubscript(const SCEV *A,
128 const SCEV *B,
129 Subscript *S) const {
Andreas Bolkaa89b3b32009-07-30 02:26:01 +0000130 DEBUG(errs() << " Testing subscript: " << *A << ", " << *B << "\n");
131
132 if (A == B) {
133 DEBUG(errs() << " -> [D] same SCEV\n");
134 return Dependent;
135 }
136
137 // TODO: Implement ZIV/SIV/MIV testers.
138
139 DEBUG(errs() << " -> [?] cannot analyse subscript\n");
140 return Unknown;
Andreas Bolka80478de2009-07-29 05:35:53 +0000141}
142
143LoopDependenceAnalysis::DependenceResult
Andreas Bolka4e661252009-07-28 19:50:13 +0000144LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka89569882009-07-25 12:19:58 +0000145 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000146
Andreas Bolka0a528a02009-07-23 14:32:46 +0000147 // We only analyse loads and stores but no possible memory accesses by e.g.
148 // free, call, or invoke instructions.
149 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka89569882009-07-25 12:19:58 +0000150 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000151 return Unknown;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000152 }
153
Andreas Bolkae00b4942009-07-28 19:49:25 +0000154 Value *aPtr = GetPointerOperand(P->A);
155 Value *bPtr = GetPointerOperand(P->B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000156
Andreas Bolkae00b4942009-07-28 19:49:25 +0000157 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
158 case AliasAnalysis::MayAlias:
159 // We can not analyse objects if we do not know about their aliasing.
Andreas Bolka89569882009-07-25 12:19:58 +0000160 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000161 return Unknown;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000162
Andreas Bolkae00b4942009-07-28 19:49:25 +0000163 case AliasAnalysis::NoAlias:
164 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka89569882009-07-25 12:19:58 +0000165 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000166 return Independent;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000167
Andreas Bolkae00b4942009-07-28 19:49:25 +0000168 case AliasAnalysis::MustAlias:
169 break; // The underlying objects alias, test accesses for dependence.
170 }
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000171
Andreas Bolka80478de2009-07-29 05:35:53 +0000172 const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr);
173 const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr);
174
175 if (!aGEP || !bGEP)
176 return Unknown;
177
178 // FIXME: Is filtering coupled subscripts necessary?
179
180 // Analyse indices pairwise (FIXME: use GetGEPOperands from BasicAA), adding
181 // trailing zeroes to the smaller GEP, if needed.
182 GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
183 aEnd = aGEP->idx_end(),
184 bIdx = bGEP->idx_begin(),
185 bEnd = bGEP->idx_end();
186 while (aIdx != aEnd && bIdx != bEnd) {
187 const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
188 const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
189 Subscript subscript;
190 DependenceResult result = analyseSubscript(aSCEV, bSCEV, &subscript);
191 if (result != Dependent) {
192 // We either proved independence or failed to analyse this subscript.
193 // Further subscripts will not improve the situation, so abort early.
194 return result;
195 }
196 P->Subscripts.push_back(subscript);
197 if (aIdx != aEnd) ++aIdx;
198 if (bIdx != bEnd) ++bIdx;
199 }
200 // Either there were no subscripts or all subscripts were analysed to be
201 // dependent; in both cases we know the accesses are dependent.
202 return Dependent;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000203}
204
205bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
206 assert(isDependencePair(A, B) && "Values form no dependence pair!");
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000207 ++NumAnswered;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000208
209 DependencePair *p;
210 if (!findOrInsertDependencePair(A, B, p)) {
211 // The pair is not cached, so analyse it.
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000212 ++NumAnalysed;
Andreas Bolka4e661252009-07-28 19:50:13 +0000213 switch (p->Result = analysePair(p)) {
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000214 case Dependent: ++NumDependent; break;
215 case Independent: ++NumIndependent; break;
216 case Unknown: ++NumUnknown; break;
217 }
Andreas Bolka0a528a02009-07-23 14:32:46 +0000218 }
219 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000220}
221
222//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000223// LoopDependenceAnalysis Implementation
224//===----------------------------------------------------------------------===//
225
226bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
227 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000228 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000229 SE = &getAnalysis<ScalarEvolution>();
230 return false;
231}
232
Andreas Bolka0a528a02009-07-23 14:32:46 +0000233void LoopDependenceAnalysis::releaseMemory() {
234 Pairs.clear();
235 PairAllocator.Reset();
236}
237
Andreas Bolka306b5b22009-06-24 21:29:13 +0000238void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
239 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000240 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000241 AU.addRequiredTransitive<ScalarEvolution>();
242}
243
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000244static void PrintLoopInfo(raw_ostream &OS,
245 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000246 if (!L->empty()) return; // ignore non-innermost loops
247
Andreas Bolkab512fb02009-07-03 01:42:52 +0000248 SmallVector<Instruction*, 8> memrefs;
249 GetMemRefInstrs(L, memrefs);
250
Andreas Bolkab88ee912009-06-28 00:16:08 +0000251 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
252 WriteAsOperand(OS, L->getHeader(), false);
253 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000254
Andreas Bolka158c5902009-06-28 00:35:22 +0000255 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000256 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000257 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000258 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000259
Andreas Bolka158c5902009-06-28 00:35:22 +0000260 OS << " Pairwise dependence results:\n";
261 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000262 end = memrefs.end(); x != end; ++x)
Andreas Bolka158c5902009-06-28 00:35:22 +0000263 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000264 y != end; ++y)
Andreas Bolka158c5902009-06-28 00:35:22 +0000265 if (LDA->isDependencePair(*x, *y))
266 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
267 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
268 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000269}
270
271void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000272 // TODO: doc why const_cast is safe
273 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000274}
275
276void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
277 raw_os_ostream os(OS);
278 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000279}