blob: 13d449f64ecdd8791135479b137b1cf1aef3dbde [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 Bolka8f9cd692009-07-01 21:45:23 +000021#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000022#include "llvm/Analysis/LoopDependenceAnalysis.h"
23#include "llvm/Analysis/LoopPass.h"
24#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000025#include "llvm/Instructions.h"
Andreas Bolka5771ed92009-06-30 02:12:10 +000026#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000027#include "llvm/Support/ErrorHandling.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000028#include "llvm/Target/TargetData.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000029using namespace llvm;
30
31LoopPass *llvm::createLoopDependenceAnalysisPass() {
32 return new LoopDependenceAnalysis();
33}
34
35static RegisterPass<LoopDependenceAnalysis>
36R("lda", "Loop Dependence Analysis", false, true);
37char LoopDependenceAnalysis::ID = 0;
38
39//===----------------------------------------------------------------------===//
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000040// Utility Functions
41//===----------------------------------------------------------------------===//
42
Andreas Bolkafc8a04a2009-06-29 18:51:11 +000043static inline bool IsMemRefInstr(const Value *V) {
44 const Instruction *I = dyn_cast<const Instruction>(V);
45 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000046}
47
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000048static void GetMemRefInstrs(const Loop *L,
49 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolka158c5902009-06-28 00:35:22 +000050 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
51 b != be; ++b)
52 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
53 i != ie; ++i)
Andreas Bolka88a17f02009-06-29 00:50:26 +000054 if (IsMemRefInstr(i))
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000055 Memrefs.push_back(i);
Andreas Bolka158c5902009-06-28 00:35:22 +000056}
57
Andreas Bolka5771ed92009-06-30 02:12:10 +000058static bool IsLoadOrStoreInst(Value *I) {
59 return isa<LoadInst>(I) || isa<StoreInst>(I);
60}
61
62static Value *GetPointerOperand(Value *I) {
63 if (LoadInst *i = dyn_cast<LoadInst>(I))
64 return i->getPointerOperand();
65 if (StoreInst *i = dyn_cast<StoreInst>(I))
66 return i->getPointerOperand();
Edwin Törökbd448e32009-07-14 16:55:14 +000067 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolka5771ed92009-06-30 02:12:10 +000068 // Never reached.
69 return 0;
70}
71
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000072//===----------------------------------------------------------------------===//
73// Dependence Testing
74//===----------------------------------------------------------------------===//
75
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000076bool LoopDependenceAnalysis::isDependencePair(const Value *A,
77 const Value *B) const {
78 return IsMemRefInstr(A) &&
79 IsMemRefInstr(B) &&
80 (cast<const Instruction>(A)->mayWriteToMemory() ||
81 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000082}
83
Andreas Bolka0a528a02009-07-23 14:32:46 +000084bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *X,
85 Value *Y,
86 DependencePair *&P) {
87 void *insertPos = 0;
88 FoldingSetNodeID id;
89 id.AddPointer(X);
90 id.AddPointer(Y);
Andreas Bolka5771ed92009-06-30 02:12:10 +000091
Andreas Bolka0a528a02009-07-23 14:32:46 +000092 P = Pairs.FindNodeOrInsertPos(id, insertPos);
93 if (P) return true;
Andreas Bolka5771ed92009-06-30 02:12:10 +000094
Andreas Bolka0a528a02009-07-23 14:32:46 +000095 P = PairAllocator.Allocate<DependencePair>();
96 new (P) DependencePair(id, X, Y);
97 Pairs.InsertNode(P, insertPos);
98 return false;
99}
100
101void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
102 DOUT << "Analysing:\n" << *P->A << "\n" << *P->B << "\n";
103
104 // Our default answer: we don't know anything, i.e. we failed to analyse this
105 // pair to get a more specific answer (dependent, independent).
106 P->Result = Unknown;
107
108 // We only analyse loads and stores but no possible memory accesses by e.g.
109 // free, call, or invoke instructions.
110 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
111 DOUT << "--> [?] no load/store\n";
112 return;
113 }
114
115 Value *aptr = GetPointerOperand(P->A);
116 Value *bptr = GetPointerOperand(P->B);
117 const Value *aobj = aptr->getUnderlyingObject();
118 const Value *bobj = bptr->getUnderlyingObject();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000119 AliasAnalysis::AliasResult alias = AA->alias(
Andreas Bolka0a528a02009-07-23 14:32:46 +0000120 aobj, AA->getTargetData().getTypeStoreSize(aobj->getType()),
121 bobj, AA->getTargetData().getTypeStoreSize(bobj->getType()));
Andreas Bolka5771ed92009-06-30 02:12:10 +0000122
Andreas Bolka0a528a02009-07-23 14:32:46 +0000123 // We can not analyse objects if we do not know about their aliasing.
124 if (alias == AliasAnalysis::MayAlias) {
125 DOUT << "---> [?] may alias\n";
126 return;
127 }
Andreas Bolka5771ed92009-06-30 02:12:10 +0000128
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000129 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka0a528a02009-07-23 14:32:46 +0000130 if (alias == AliasAnalysis::NoAlias) {
131 DOUT << "---> [I] no alias\n";
132 P->Result = Independent;
133 return;
134 }
Andreas Bolka5771ed92009-06-30 02:12:10 +0000135
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000136 // TODO: the underlying objects MustAlias, test for dependence
137
Andreas Bolka0a528a02009-07-23 14:32:46 +0000138 DOUT << "---> [?] cannot analyse\n";
139 return;
140}
141
142bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
143 assert(isDependencePair(A, B) && "Values form no dependence pair!");
144
145 DependencePair *p;
146 if (!findOrInsertDependencePair(A, B, p)) {
147 // The pair is not cached, so analyse it.
148 analysePair(p);
149 }
150 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000151}
152
153//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000154// LoopDependenceAnalysis Implementation
155//===----------------------------------------------------------------------===//
156
157bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
158 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000159 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000160 SE = &getAnalysis<ScalarEvolution>();
161 return false;
162}
163
Andreas Bolka0a528a02009-07-23 14:32:46 +0000164void LoopDependenceAnalysis::releaseMemory() {
165 Pairs.clear();
166 PairAllocator.Reset();
167}
168
Andreas Bolka306b5b22009-06-24 21:29:13 +0000169void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
170 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000171 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000172 AU.addRequiredTransitive<ScalarEvolution>();
173}
174
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000175static void PrintLoopInfo(raw_ostream &OS,
176 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000177 if (!L->empty()) return; // ignore non-innermost loops
178
Andreas Bolkab512fb02009-07-03 01:42:52 +0000179 SmallVector<Instruction*, 8> memrefs;
180 GetMemRefInstrs(L, memrefs);
181
Andreas Bolkab88ee912009-06-28 00:16:08 +0000182 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
183 WriteAsOperand(OS, L->getHeader(), false);
184 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000185
Andreas Bolka158c5902009-06-28 00:35:22 +0000186 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000187 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
188 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000189 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000190
Andreas Bolka158c5902009-06-28 00:35:22 +0000191 OS << " Pairwise dependence results:\n";
192 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
193 end = memrefs.end(); x != end; ++x)
194 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
195 y != end; ++y)
196 if (LDA->isDependencePair(*x, *y))
197 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
198 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
199 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000200}
201
202void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000203 // TODO: doc why const_cast is safe
204 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000205}
206
207void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
208 raw_os_ostream os(OS);
209 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000210}