1 //===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
13 // Please note that this is work in progress and the interface is subject to
16 // TODO: adapt as implementation progresses.
18 //===----------------------------------------------------------------------===//
20 #define DEBUG_TYPE "lda"
21 #include "llvm/ADT/Statistic.h"
22 #include "llvm/Analysis/AliasAnalysis.h"
23 #include "llvm/Analysis/LoopDependenceAnalysis.h"
24 #include "llvm/Analysis/LoopPass.h"
25 #include "llvm/Analysis/ScalarEvolution.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Support/Allocator.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/raw_ostream.h"
31 #include "llvm/Target/TargetData.h"
34 STATISTIC(NumAnswered, "Number of dependence queries answered");
35 STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
36 STATISTIC(NumDependent, "Number of pairs with dependent accesses");
37 STATISTIC(NumIndependent, "Number of pairs with independent accesses");
38 STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
40 LoopPass *llvm::createLoopDependenceAnalysisPass() {
41 return new LoopDependenceAnalysis();
44 static RegisterPass<LoopDependenceAnalysis>
45 R("lda", "Loop Dependence Analysis", false, true);
46 char LoopDependenceAnalysis::ID = 0;
48 //===----------------------------------------------------------------------===//
50 //===----------------------------------------------------------------------===//
52 static inline bool IsMemRefInstr(const Value *V) {
53 const Instruction *I = dyn_cast<const Instruction>(V);
54 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
57 static void GetMemRefInstrs(const Loop *L,
58 SmallVectorImpl<Instruction*> &Memrefs) {
59 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
61 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
67 static bool IsLoadOrStoreInst(Value *I) {
68 return isa<LoadInst>(I) || isa<StoreInst>(I);
71 static 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();
76 llvm_unreachable("Value is no load or store instruction!");
81 static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
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()));
90 //===----------------------------------------------------------------------===//
92 //===----------------------------------------------------------------------===//
94 bool LoopDependenceAnalysis::isDependencePair(const Value *A,
95 const Value *B) const {
96 return IsMemRefInstr(A) &&
98 (cast<const Instruction>(A)->mayWriteToMemory() ||
99 cast<const Instruction>(B)->mayWriteToMemory());
102 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
104 DependencePair *&P) {
110 P = Pairs.FindNodeOrInsertPos(id, insertPos);
113 P = PairAllocator.Allocate<DependencePair>();
114 new (P) DependencePair(id, A, B);
115 Pairs.InsertNode(P, insertPos);
119 void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
120 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
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).
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)) {
129 DEBUG(errs() << "--> [?] no load/store\n");
133 Value *aPtr = GetPointerOperand(P->A);
134 Value *bPtr = GetPointerOperand(P->B);
136 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
137 case AliasAnalysis::MayAlias:
138 // We can not analyse objects if we do not know about their aliasing.
139 DEBUG(errs() << "---> [?] may alias\n");
142 case AliasAnalysis::NoAlias:
143 // If the objects noalias, they are distinct, accesses are independent.
144 DEBUG(errs() << "---> [I] no alias\n");
145 P->Result = Independent;
148 case AliasAnalysis::MustAlias:
149 break; // The underlying objects alias, test accesses for dependence.
152 DEBUG(errs() << "---> [?] cannot analyse\n");
156 bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
157 assert(isDependencePair(A, B) && "Values form no dependence pair!");
161 if (!findOrInsertDependencePair(A, B, p)) {
162 // The pair is not cached, so analyse it.
166 case Dependent: ++NumDependent; break;
167 case Independent: ++NumIndependent; break;
168 case Unknown: ++NumUnknown; break;
171 return p->Result != Independent;
174 //===----------------------------------------------------------------------===//
175 // LoopDependenceAnalysis Implementation
176 //===----------------------------------------------------------------------===//
178 bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
180 AA = &getAnalysis<AliasAnalysis>();
181 SE = &getAnalysis<ScalarEvolution>();
185 void LoopDependenceAnalysis::releaseMemory() {
187 PairAllocator.Reset();
190 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
191 AU.setPreservesAll();
192 AU.addRequiredTransitive<AliasAnalysis>();
193 AU.addRequiredTransitive<ScalarEvolution>();
196 static void PrintLoopInfo(raw_ostream &OS,
197 LoopDependenceAnalysis *LDA, const Loop *L) {
198 if (!L->empty()) return; // ignore non-innermost loops
200 SmallVector<Instruction*, 8> memrefs;
201 GetMemRefInstrs(L, memrefs);
203 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
204 WriteAsOperand(OS, L->getHeader(), false);
207 OS << " Load/store instructions: " << memrefs.size() << "\n";
208 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
209 end = memrefs.end(); x != end; ++x)
210 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
212 OS << " Pairwise dependence results:\n";
213 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
214 end = memrefs.end(); x != end; ++x)
215 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
217 if (LDA->isDependencePair(*x, *y))
218 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
219 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
223 void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
224 // TODO: doc why const_cast is safe
225 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
228 void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
229 raw_os_ostream os(OS);