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/Analysis/AliasAnalysis.h"
22 #include "llvm/Analysis/LoopDependenceAnalysis.h"
23 #include "llvm/Analysis/LoopPass.h"
24 #include "llvm/Analysis/ScalarEvolution.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Support/Debug.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Target/TargetData.h"
31 LoopPass *llvm::createLoopDependenceAnalysisPass() {
32 return new LoopDependenceAnalysis();
35 static RegisterPass<LoopDependenceAnalysis>
36 R("lda", "Loop Dependence Analysis", false, true);
37 char LoopDependenceAnalysis::ID = 0;
39 //===----------------------------------------------------------------------===//
41 //===----------------------------------------------------------------------===//
43 static inline bool IsMemRefInstr(const Value *V) {
44 const Instruction *I = dyn_cast<const Instruction>(V);
45 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
48 static void GetMemRefInstrs(const Loop *L,
49 SmallVectorImpl<Instruction*> &Memrefs) {
50 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
52 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
58 static bool IsLoadOrStoreInst(Value *I) {
59 return isa<LoadInst>(I) || isa<StoreInst>(I);
62 static 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();
67 llvm_unreachable("Value is no load or store instruction!");
72 //===----------------------------------------------------------------------===//
74 //===----------------------------------------------------------------------===//
76 bool LoopDependenceAnalysis::isDependencePair(const Value *A,
77 const Value *B) const {
78 return IsMemRefInstr(A) &&
80 (cast<const Instruction>(A)->mayWriteToMemory() ||
81 cast<const Instruction>(B)->mayWriteToMemory());
84 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *X,
92 P = Pairs.FindNodeOrInsertPos(id, insertPos);
95 P = PairAllocator.Allocate<DependencePair>();
96 new (P) DependencePair(id, X, Y);
97 Pairs.InsertNode(P, insertPos);
101 void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
102 DOUT << "Analysing:\n" << *P->A << "\n" << *P->B << "\n";
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).
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";
115 Value *aptr = GetPointerOperand(P->A);
116 Value *bptr = GetPointerOperand(P->B);
117 const Value *aobj = aptr->getUnderlyingObject();
118 const Value *bobj = bptr->getUnderlyingObject();
119 AliasAnalysis::AliasResult alias = AA->alias(
120 aobj, AA->getTargetData().getTypeStoreSize(aobj->getType()),
121 bobj, AA->getTargetData().getTypeStoreSize(bobj->getType()));
123 // We can not analyse objects if we do not know about their aliasing.
124 if (alias == AliasAnalysis::MayAlias) {
125 DOUT << "---> [?] may alias\n";
129 // If the objects noalias, they are distinct, accesses are independent.
130 if (alias == AliasAnalysis::NoAlias) {
131 DOUT << "---> [I] no alias\n";
132 P->Result = Independent;
136 // TODO: the underlying objects MustAlias, test for dependence
138 DOUT << "---> [?] cannot analyse\n";
142 bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
143 assert(isDependencePair(A, B) && "Values form no dependence pair!");
146 if (!findOrInsertDependencePair(A, B, p)) {
147 // The pair is not cached, so analyse it.
150 return p->Result != Independent;
153 //===----------------------------------------------------------------------===//
154 // LoopDependenceAnalysis Implementation
155 //===----------------------------------------------------------------------===//
157 bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
159 AA = &getAnalysis<AliasAnalysis>();
160 SE = &getAnalysis<ScalarEvolution>();
164 void LoopDependenceAnalysis::releaseMemory() {
166 PairAllocator.Reset();
169 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
170 AU.setPreservesAll();
171 AU.addRequiredTransitive<AliasAnalysis>();
172 AU.addRequiredTransitive<ScalarEvolution>();
175 static void PrintLoopInfo(raw_ostream &OS,
176 LoopDependenceAnalysis *LDA, const Loop *L) {
177 if (!L->empty()) return; // ignore non-innermost loops
179 SmallVector<Instruction*, 8> memrefs;
180 GetMemRefInstrs(L, memrefs);
182 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
183 WriteAsOperand(OS, L->getHeader(), false);
186 OS << " Load/store instructions: " << memrefs.size() << "\n";
187 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
188 end = memrefs.end(); x != end; ++x)
189 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
191 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;
196 if (LDA->isDependencePair(*x, *y))
197 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
198 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
202 void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
203 // TODO: doc why const_cast is safe
204 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
207 void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
208 raw_os_ostream os(OS);