blob: f185c9679583cdf5ec5ad23e041bd0ea2a082dcf [file] [log] [blame]
Andreas Bolkacb210102009-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 Bolkafecbc592009-07-01 21:45:23 +000021#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolkacb210102009-06-24 21:29:13 +000022#include "llvm/Analysis/LoopDependenceAnalysis.h"
23#include "llvm/Analysis/LoopPass.h"
24#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolkaf35626d2009-06-28 00:21:21 +000025#include "llvm/Instructions.h"
Andreas Bolka0baa25d2009-07-24 23:19:28 +000026#include "llvm/Support/Allocator.h"
Andreas Bolkae9722fc2009-06-30 02:12:10 +000027#include "llvm/Support/Debug.h"
Torok Edwinc25e7582009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Andreas Bolka0baa25d2009-07-24 23:19:28 +000029#include "llvm/Support/raw_ostream.h"
Andreas Bolkafecbc592009-07-01 21:45:23 +000030#include "llvm/Target/TargetData.h"
Andreas Bolkacb210102009-06-24 21:29:13 +000031using namespace llvm;
32
33LoopPass *llvm::createLoopDependenceAnalysisPass() {
34 return new LoopDependenceAnalysis();
35}
36
37static RegisterPass<LoopDependenceAnalysis>
38R("lda", "Loop Dependence Analysis", false, true);
39char LoopDependenceAnalysis::ID = 0;
40
41//===----------------------------------------------------------------------===//
Andreas Bolkaf35626d2009-06-28 00:21:21 +000042// Utility Functions
43//===----------------------------------------------------------------------===//
44
Andreas Bolka2fbb7702009-06-29 18:51:11 +000045static inline bool IsMemRefInstr(const Value *V) {
46 const Instruction *I = dyn_cast<const Instruction>(V);
47 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf35626d2009-06-28 00:21:21 +000048}
49
Andreas Bolka3b59dd82009-07-23 01:57:06 +000050static void GetMemRefInstrs(const Loop *L,
51 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolkac6a30302009-06-28 00:35:22 +000052 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
53 b != be; ++b)
54 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
55 i != ie; ++i)
Andreas Bolkaacd6f8d2009-06-29 00:50:26 +000056 if (IsMemRefInstr(i))
Andreas Bolka3b59dd82009-07-23 01:57:06 +000057 Memrefs.push_back(i);
Andreas Bolkac6a30302009-06-28 00:35:22 +000058}
59
Andreas Bolkae9722fc2009-06-30 02:12:10 +000060static bool IsLoadOrStoreInst(Value *I) {
61 return isa<LoadInst>(I) || isa<StoreInst>(I);
62}
63
64static Value *GetPointerOperand(Value *I) {
65 if (LoadInst *i = dyn_cast<LoadInst>(I))
66 return i->getPointerOperand();
67 if (StoreInst *i = dyn_cast<StoreInst>(I))
68 return i->getPointerOperand();
Torok Edwinc23197a2009-07-14 16:55:14 +000069 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolkae9722fc2009-06-30 02:12:10 +000070 // Never reached.
71 return 0;
72}
73
Andreas Bolkaf35626d2009-06-28 00:21:21 +000074//===----------------------------------------------------------------------===//
75// Dependence Testing
76//===----------------------------------------------------------------------===//
77
Andreas Bolka3b59dd82009-07-23 01:57:06 +000078bool LoopDependenceAnalysis::isDependencePair(const Value *A,
79 const Value *B) const {
80 return IsMemRefInstr(A) &&
81 IsMemRefInstr(B) &&
82 (cast<const Instruction>(A)->mayWriteToMemory() ||
83 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf35626d2009-06-28 00:21:21 +000084}
85
Andreas Bolkab4c28e92009-07-23 14:32:46 +000086bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *X,
87 Value *Y,
88 DependencePair *&P) {
89 void *insertPos = 0;
90 FoldingSetNodeID id;
91 id.AddPointer(X);
92 id.AddPointer(Y);
Andreas Bolkae9722fc2009-06-30 02:12:10 +000093
Andreas Bolkab4c28e92009-07-23 14:32:46 +000094 P = Pairs.FindNodeOrInsertPos(id, insertPos);
95 if (P) return true;
Andreas Bolkae9722fc2009-06-30 02:12:10 +000096
Andreas Bolkab4c28e92009-07-23 14:32:46 +000097 P = PairAllocator.Allocate<DependencePair>();
98 new (P) DependencePair(id, X, Y);
99 Pairs.InsertNode(P, insertPos);
100 return false;
101}
102
103void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
104 DOUT << "Analysing:\n" << *P->A << "\n" << *P->B << "\n";
105
106 // Our default answer: we don't know anything, i.e. we failed to analyse this
107 // pair to get a more specific answer (dependent, independent).
108 P->Result = Unknown;
109
110 // We only analyse loads and stores but no possible memory accesses by e.g.
111 // free, call, or invoke instructions.
112 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
113 DOUT << "--> [?] no load/store\n";
114 return;
115 }
116
117 Value *aptr = GetPointerOperand(P->A);
118 Value *bptr = GetPointerOperand(P->B);
119 const Value *aobj = aptr->getUnderlyingObject();
120 const Value *bobj = bptr->getUnderlyingObject();
Andreas Bolkafecbc592009-07-01 21:45:23 +0000121 AliasAnalysis::AliasResult alias = AA->alias(
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000122 aobj, AA->getTargetData().getTypeStoreSize(aobj->getType()),
123 bobj, AA->getTargetData().getTypeStoreSize(bobj->getType()));
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000124
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000125 // We can not analyse objects if we do not know about their aliasing.
126 if (alias == AliasAnalysis::MayAlias) {
127 DOUT << "---> [?] may alias\n";
128 return;
129 }
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000130
Andreas Bolkafecbc592009-07-01 21:45:23 +0000131 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000132 if (alias == AliasAnalysis::NoAlias) {
133 DOUT << "---> [I] no alias\n";
134 P->Result = Independent;
135 return;
136 }
Andreas Bolkae9722fc2009-06-30 02:12:10 +0000137
Andreas Bolkafecbc592009-07-01 21:45:23 +0000138 // TODO: the underlying objects MustAlias, test for dependence
139
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000140 DOUT << "---> [?] cannot analyse\n";
141 return;
142}
143
144bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
145 assert(isDependencePair(A, B) && "Values form no dependence pair!");
146
147 DependencePair *p;
148 if (!findOrInsertDependencePair(A, B, p)) {
149 // The pair is not cached, so analyse it.
150 analysePair(p);
151 }
152 return p->Result != Independent;
Andreas Bolkaf35626d2009-06-28 00:21:21 +0000153}
154
155//===----------------------------------------------------------------------===//
Andreas Bolkacb210102009-06-24 21:29:13 +0000156// LoopDependenceAnalysis Implementation
157//===----------------------------------------------------------------------===//
158
159bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
160 this->L = L;
Andreas Bolkafecbc592009-07-01 21:45:23 +0000161 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolkacb210102009-06-24 21:29:13 +0000162 SE = &getAnalysis<ScalarEvolution>();
163 return false;
164}
165
Andreas Bolkab4c28e92009-07-23 14:32:46 +0000166void LoopDependenceAnalysis::releaseMemory() {
167 Pairs.clear();
168 PairAllocator.Reset();
169}
170
Andreas Bolkacb210102009-06-24 21:29:13 +0000171void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
172 AU.setPreservesAll();
Andreas Bolkafecbc592009-07-01 21:45:23 +0000173 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolka707207a2009-06-28 00:16:08 +0000174 AU.addRequiredTransitive<ScalarEvolution>();
175}
176
Andreas Bolka3b59dd82009-07-23 01:57:06 +0000177static void PrintLoopInfo(raw_ostream &OS,
178 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolka707207a2009-06-28 00:16:08 +0000179 if (!L->empty()) return; // ignore non-innermost loops
180
Andreas Bolka292aef32009-07-03 01:42:52 +0000181 SmallVector<Instruction*, 8> memrefs;
182 GetMemRefInstrs(L, memrefs);
183
Andreas Bolka707207a2009-06-28 00:16:08 +0000184 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
185 WriteAsOperand(OS, L->getHeader(), false);
186 OS << "\n";
Andreas Bolkac6a30302009-06-28 00:35:22 +0000187
Andreas Bolkac6a30302009-06-28 00:35:22 +0000188 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolka292aef32009-07-03 01:42:52 +0000189 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
190 end = memrefs.end(); x != end; ++x)
Andreas Bolka3b59dd82009-07-23 01:57:06 +0000191 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolka292aef32009-07-03 01:42:52 +0000192
Andreas Bolkac6a30302009-06-28 00:35:22 +0000193 OS << " Pairwise dependence results:\n";
194 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
195 end = memrefs.end(); x != end; ++x)
196 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
197 y != end; ++y)
198 if (LDA->isDependencePair(*x, *y))
199 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
200 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
201 << "\n";
Andreas Bolka707207a2009-06-28 00:16:08 +0000202}
203
204void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolkac6a30302009-06-28 00:35:22 +0000205 // TODO: doc why const_cast is safe
206 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolka707207a2009-06-28 00:16:08 +0000207}
208
209void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
210 raw_os_ostream os(OS);
211 print(os, M);
Andreas Bolkacb210102009-06-24 21:29:13 +0000212}