blob: 97ac3886e2ced9fc133c82872eb43632f7d60a88 [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//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "lda"
Andreas Bolka43aa1e02009-07-28 19:49:49 +000021#include "llvm/ADT/Statistic.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000022#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000023#include "llvm/Analysis/LoopDependenceAnalysis.h"
24#include "llvm/Analysis/LoopPass.h"
25#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000026#include "llvm/Instructions.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000027#include "llvm/Support/Allocator.h"
Andreas Bolka5771ed92009-06-30 02:12:10 +000028#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000029#include "llvm/Support/ErrorHandling.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000030#include "llvm/Support/raw_ostream.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000031#include "llvm/Target/TargetData.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000032using namespace llvm;
33
Andreas Bolka43aa1e02009-07-28 19:49:49 +000034STATISTIC(NumAnswered, "Number of dependence queries answered");
35STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
36STATISTIC(NumDependent, "Number of pairs with dependent accesses");
37STATISTIC(NumIndependent, "Number of pairs with independent accesses");
38STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
39
Andreas Bolka306b5b22009-06-24 21:29:13 +000040LoopPass *llvm::createLoopDependenceAnalysisPass() {
41 return new LoopDependenceAnalysis();
42}
43
44static RegisterPass<LoopDependenceAnalysis>
45R("lda", "Loop Dependence Analysis", false, true);
46char LoopDependenceAnalysis::ID = 0;
47
48//===----------------------------------------------------------------------===//
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000049// Utility Functions
50//===----------------------------------------------------------------------===//
51
Andreas Bolkafc8a04a2009-06-29 18:51:11 +000052static inline bool IsMemRefInstr(const Value *V) {
53 const Instruction *I = dyn_cast<const Instruction>(V);
54 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000055}
56
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000057static void GetMemRefInstrs(const Loop *L,
58 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolka158c5902009-06-28 00:35:22 +000059 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000060 b != be; ++b)
Andreas Bolka158c5902009-06-28 00:35:22 +000061 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
Andreas Bolkae00b4942009-07-28 19:49:25 +000062 i != ie; ++i)
Andreas Bolka88a17f02009-06-29 00:50:26 +000063 if (IsMemRefInstr(i))
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000064 Memrefs.push_back(i);
Andreas Bolka158c5902009-06-28 00:35:22 +000065}
66
Andreas Bolka5771ed92009-06-30 02:12:10 +000067static bool IsLoadOrStoreInst(Value *I) {
68 return isa<LoadInst>(I) || isa<StoreInst>(I);
69}
70
71static Value *GetPointerOperand(Value *I) {
72 if (LoadInst *i = dyn_cast<LoadInst>(I))
73 return i->getPointerOperand();
74 if (StoreInst *i = dyn_cast<StoreInst>(I))
75 return i->getPointerOperand();
Edwin Törökbd448e32009-07-14 16:55:14 +000076 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolka5771ed92009-06-30 02:12:10 +000077 // Never reached.
78 return 0;
79}
80
Andreas Bolkae00b4942009-07-28 19:49:25 +000081static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
82 const Value *A,
83 const Value *B) {
84 const Value *aObj = A->getUnderlyingObject();
85 const Value *bObj = B->getUnderlyingObject();
86 return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
87 bObj, AA->getTypeStoreSize(bObj->getType()));
88}
89
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000090//===----------------------------------------------------------------------===//
91// Dependence Testing
92//===----------------------------------------------------------------------===//
93
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000094bool LoopDependenceAnalysis::isDependencePair(const Value *A,
95 const Value *B) const {
96 return IsMemRefInstr(A) &&
97 IsMemRefInstr(B) &&
98 (cast<const Instruction>(A)->mayWriteToMemory() ||
99 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000100}
101
Andreas Bolkae00b4942009-07-28 19:49:25 +0000102bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
103 Value *B,
Andreas Bolka0a528a02009-07-23 14:32:46 +0000104 DependencePair *&P) {
105 void *insertPos = 0;
106 FoldingSetNodeID id;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000107 id.AddPointer(A);
108 id.AddPointer(B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000109
Andreas Bolka0a528a02009-07-23 14:32:46 +0000110 P = Pairs.FindNodeOrInsertPos(id, insertPos);
111 if (P) return true;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000112
Andreas Bolka0a528a02009-07-23 14:32:46 +0000113 P = PairAllocator.Allocate<DependencePair>();
Andreas Bolkae00b4942009-07-28 19:49:25 +0000114 new (P) DependencePair(id, A, B);
Andreas Bolka0a528a02009-07-23 14:32:46 +0000115 Pairs.InsertNode(P, insertPos);
116 return false;
117}
118
119void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka89569882009-07-25 12:19:58 +0000120 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000121
122 // Our default answer: we don't know anything, i.e. we failed to analyse this
123 // pair to get a more specific answer (dependent, independent).
124 P->Result = Unknown;
125
126 // We only analyse loads and stores but no possible memory accesses by e.g.
127 // free, call, or invoke instructions.
128 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka89569882009-07-25 12:19:58 +0000129 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000130 return;
131 }
132
Andreas Bolkae00b4942009-07-28 19:49:25 +0000133 Value *aPtr = GetPointerOperand(P->A);
134 Value *bPtr = GetPointerOperand(P->B);
Andreas Bolka5771ed92009-06-30 02:12:10 +0000135
Andreas Bolkae00b4942009-07-28 19:49:25 +0000136 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
137 case AliasAnalysis::MayAlias:
138 // We can not analyse objects if we do not know about their aliasing.
Andreas Bolka89569882009-07-25 12:19:58 +0000139 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000140 return;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000141
Andreas Bolkae00b4942009-07-28 19:49:25 +0000142 case AliasAnalysis::NoAlias:
143 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka89569882009-07-25 12:19:58 +0000144 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000145 P->Result = Independent;
146 return;
Andreas Bolka5771ed92009-06-30 02:12:10 +0000147
Andreas Bolkae00b4942009-07-28 19:49:25 +0000148 case AliasAnalysis::MustAlias:
149 break; // The underlying objects alias, test accesses for dependence.
150 }
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000151
Andreas Bolka89569882009-07-25 12:19:58 +0000152 DEBUG(errs() << "---> [?] cannot analyse\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000153 return;
154}
155
156bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
157 assert(isDependencePair(A, B) && "Values form no dependence pair!");
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000158 ++NumAnswered;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000159
160 DependencePair *p;
161 if (!findOrInsertDependencePair(A, B, p)) {
162 // The pair is not cached, so analyse it.
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000163 ++NumAnalysed;
Andreas Bolka0a528a02009-07-23 14:32:46 +0000164 analysePair(p);
Andreas Bolka43aa1e02009-07-28 19:49:49 +0000165 switch (p->Result) {
166 case Dependent: ++NumDependent; break;
167 case Independent: ++NumIndependent; break;
168 case Unknown: ++NumUnknown; break;
169 }
Andreas Bolka0a528a02009-07-23 14:32:46 +0000170 }
171 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000172}
173
174//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000175// LoopDependenceAnalysis Implementation
176//===----------------------------------------------------------------------===//
177
178bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
179 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000180 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000181 SE = &getAnalysis<ScalarEvolution>();
182 return false;
183}
184
Andreas Bolka0a528a02009-07-23 14:32:46 +0000185void LoopDependenceAnalysis::releaseMemory() {
186 Pairs.clear();
187 PairAllocator.Reset();
188}
189
Andreas Bolka306b5b22009-06-24 21:29:13 +0000190void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
191 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000192 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000193 AU.addRequiredTransitive<ScalarEvolution>();
194}
195
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000196static void PrintLoopInfo(raw_ostream &OS,
197 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000198 if (!L->empty()) return; // ignore non-innermost loops
199
Andreas Bolkab512fb02009-07-03 01:42:52 +0000200 SmallVector<Instruction*, 8> memrefs;
201 GetMemRefInstrs(L, memrefs);
202
Andreas Bolkab88ee912009-06-28 00:16:08 +0000203 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
204 WriteAsOperand(OS, L->getHeader(), false);
205 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000206
Andreas Bolka158c5902009-06-28 00:35:22 +0000207 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000208 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000209 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000210 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000211
Andreas Bolka158c5902009-06-28 00:35:22 +0000212 OS << " Pairwise dependence results:\n";
213 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
Andreas Bolkae00b4942009-07-28 19:49:25 +0000214 end = memrefs.end(); x != end; ++x)
Andreas Bolka158c5902009-06-28 00:35:22 +0000215 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
Andreas Bolkae00b4942009-07-28 19:49:25 +0000216 y != end; ++y)
Andreas Bolka158c5902009-06-28 00:35:22 +0000217 if (LDA->isDependencePair(*x, *y))
218 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
219 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
220 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000221}
222
223void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000224 // TODO: doc why const_cast is safe
225 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000226}
227
228void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
229 raw_os_ostream os(OS);
230 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000231}