blob: 51f6c3aafd913f79c0413964ceba7cb4b25740cd [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 {
130 return Unknown; // TODO: Implement.
131}
132
133LoopDependenceAnalysis::DependenceResult
Andreas Bolka4e661252009-07-28 19:50:13 +0000134LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka89569882009-07-25 12:19:58 +0000135 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000136
Andreas Bolka0a528a02009-07-23 14:32:46 +0000137 // We only analyse loads and stores but no possible memory accesses by e.g.
138 // free, call, or invoke instructions.
139 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka89569882009-07-25 12:19:58 +0000140 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000141 return Unknown;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000142 }
143
Andreas Bolkae00b4942009-07-28 19:49:25 +0000144 Value *aPtr = GetPointerOperand(P->A);
145 Value *bPtr = GetPointerOperand(P->B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000146
Andreas Bolkae00b4942009-07-28 19:49:25 +0000147 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
148 case AliasAnalysis::MayAlias:
149 // We can not analyse objects if we do not know about their aliasing.
Andreas Bolka89569882009-07-25 12:19:58 +0000150 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000151 return Unknown;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000152
Andreas Bolkae00b4942009-07-28 19:49:25 +0000153 case AliasAnalysis::NoAlias:
154 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka89569882009-07-25 12:19:58 +0000155 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolka4e661252009-07-28 19:50:13 +0000156 return Independent;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000157
Andreas Bolkae00b4942009-07-28 19:49:25 +0000158 case AliasAnalysis::MustAlias:
159 break; // The underlying objects alias, test accesses for dependence.
160 }
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000161
Andreas Bolka80478de2009-07-29 05:35:53 +0000162 const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr);
163 const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr);
164
165 if (!aGEP || !bGEP)
166 return Unknown;
167
168 // FIXME: Is filtering coupled subscripts necessary?
169
170 // Analyse indices pairwise (FIXME: use GetGEPOperands from BasicAA), adding
171 // trailing zeroes to the smaller GEP, if needed.
172 GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(),
173 aEnd = aGEP->idx_end(),
174 bIdx = bGEP->idx_begin(),
175 bEnd = bGEP->idx_end();
176 while (aIdx != aEnd && bIdx != bEnd) {
177 const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE);
178 const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE);
179 Subscript subscript;
180 DependenceResult result = analyseSubscript(aSCEV, bSCEV, &subscript);
181 if (result != Dependent) {
182 // We either proved independence or failed to analyse this subscript.
183 // Further subscripts will not improve the situation, so abort early.
184 return result;
185 }
186 P->Subscripts.push_back(subscript);
187 if (aIdx != aEnd) ++aIdx;
188 if (bIdx != bEnd) ++bIdx;
189 }
190 // Either there were no subscripts or all subscripts were analysed to be
191 // dependent; in both cases we know the accesses are dependent.
192 return Dependent;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000193}
194
195bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
196 assert(isDependencePair(A, B) && "Values form no dependence pair!");
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000197 ++NumAnswered;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000198
199 DependencePair *p;
200 if (!findOrInsertDependencePair(A, B, p)) {
201 // The pair is not cached, so analyse it.
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000202 ++NumAnalysed;
Andreas Bolka4e661252009-07-28 19:50:13 +0000203 switch (p->Result = analysePair(p)) {
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000204 case Dependent: ++NumDependent; break;
205 case Independent: ++NumIndependent; break;
206 case Unknown: ++NumUnknown; break;
207 }
Andreas Bolka0a528a02009-07-23 14:32:46 +0000208 }
209 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000210}
211
212//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000213// LoopDependenceAnalysis Implementation
214//===----------------------------------------------------------------------===//
215
216bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
217 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000218 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000219 SE = &getAnalysis<ScalarEvolution>();
220 return false;
221}
222
Andreas Bolka0a528a02009-07-23 14:32:46 +0000223void LoopDependenceAnalysis::releaseMemory() {
224 Pairs.clear();
225 PairAllocator.Reset();
226}
227
Andreas Bolka306b5b22009-06-24 21:29:13 +0000228void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
229 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000230 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000231 AU.addRequiredTransitive<ScalarEvolution>();
232}
233
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000234static void PrintLoopInfo(raw_ostream &OS,
235 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000236 if (!L->empty()) return; // ignore non-innermost loops
237
Andreas Bolkab512fb02009-07-03 01:42:52 +0000238 SmallVector<Instruction*, 8> memrefs;
239 GetMemRefInstrs(L, memrefs);
240
Andreas Bolkab88ee912009-06-28 00:16:08 +0000241 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
242 WriteAsOperand(OS, L->getHeader(), false);
243 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000244
Andreas Bolka158c5902009-06-28 00:35:22 +0000245 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000246 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000247 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000248 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000249
Andreas Bolka158c5902009-06-28 00:35:22 +0000250 OS << " Pairwise dependence results:\n";
251 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000252 end = memrefs.end(); x != end; ++x)
Andreas Bolka158c5902009-06-28 00:35:22 +0000253 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000254 y != end; ++y)
Andreas Bolka158c5902009-06-28 00:35:22 +0000255 if (LDA->isDependencePair(*x, *y))
256 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
257 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
258 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000259}
260
261void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000262 // TODO: doc why const_cast is safe
263 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000264}
265
266void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
267 raw_os_ostream os(OS);
268 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000269}