Andreas Bolka | 306b5b2 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 1 | //===- 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" |
| 21 | #include "llvm/Analysis/LoopDependenceAnalysis.h" |
| 22 | #include "llvm/Analysis/LoopPass.h" |
| 23 | #include "llvm/Analysis/ScalarEvolution.h" |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 24 | #include "llvm/Instructions.h" |
Andreas Bolka | 5771ed9 | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 25 | #include "llvm/Support/Debug.h" |
Andreas Bolka | 306b5b2 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 26 | using namespace llvm; |
| 27 | |
| 28 | LoopPass *llvm::createLoopDependenceAnalysisPass() { |
| 29 | return new LoopDependenceAnalysis(); |
| 30 | } |
| 31 | |
| 32 | static RegisterPass<LoopDependenceAnalysis> |
| 33 | R("lda", "Loop Dependence Analysis", false, true); |
| 34 | char LoopDependenceAnalysis::ID = 0; |
| 35 | |
| 36 | //===----------------------------------------------------------------------===// |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 37 | // Utility Functions |
| 38 | //===----------------------------------------------------------------------===// |
| 39 | |
Andreas Bolka | fc8a04a | 2009-06-29 18:51:11 +0000 | [diff] [blame] | 40 | static inline bool IsMemRefInstr(const Value *V) { |
| 41 | const Instruction *I = dyn_cast<const Instruction>(V); |
| 42 | return I && (I->mayReadFromMemory() || I->mayWriteToMemory()); |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 43 | } |
| 44 | |
Andreas Bolka | 88a17f0 | 2009-06-29 00:50:26 +0000 | [diff] [blame] | 45 | static void GetMemRefInstrs( |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 46 | const Loop *L, SmallVectorImpl<Instruction*> &memrefs) { |
| 47 | for (Loop::block_iterator b = L->block_begin(), be = L->block_end(); |
| 48 | b != be; ++b) |
| 49 | for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end(); |
| 50 | i != ie; ++i) |
Andreas Bolka | 88a17f0 | 2009-06-29 00:50:26 +0000 | [diff] [blame] | 51 | if (IsMemRefInstr(i)) |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 52 | memrefs.push_back(i); |
| 53 | } |
| 54 | |
Andreas Bolka | 5771ed9 | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 55 | static bool IsLoadOrStoreInst(Value *I) { |
| 56 | return isa<LoadInst>(I) || isa<StoreInst>(I); |
| 57 | } |
| 58 | |
| 59 | static Value *GetPointerOperand(Value *I) { |
| 60 | if (LoadInst *i = dyn_cast<LoadInst>(I)) |
| 61 | return i->getPointerOperand(); |
| 62 | if (StoreInst *i = dyn_cast<StoreInst>(I)) |
| 63 | return i->getPointerOperand(); |
| 64 | assert(0 && "Value is no load or store instruction!"); |
| 65 | // Never reached. |
| 66 | return 0; |
| 67 | } |
| 68 | |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 69 | //===----------------------------------------------------------------------===// |
| 70 | // Dependence Testing |
| 71 | //===----------------------------------------------------------------------===// |
| 72 | |
| 73 | bool LoopDependenceAnalysis::isDependencePair(const Value *x, |
| 74 | const Value *y) const { |
Andreas Bolka | fc8a04a | 2009-06-29 18:51:11 +0000 | [diff] [blame] | 75 | return IsMemRefInstr(x) && |
| 76 | IsMemRefInstr(y) && |
| 77 | (cast<const Instruction>(x)->mayWriteToMemory() || |
| 78 | cast<const Instruction>(y)->mayWriteToMemory()); |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | bool LoopDependenceAnalysis::depends(Value *src, Value *dst) { |
| 82 | assert(isDependencePair(src, dst) && "Values form no dependence pair!"); |
Andreas Bolka | 5771ed9 | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 83 | DOUT << "== LDA test ==\n" << *src << *dst; |
| 84 | |
| 85 | // We only analyse loads and stores; for possible memory accesses by e.g. |
| 86 | // free, call, or invoke instructions we conservatively assume dependence. |
| 87 | if (!IsLoadOrStoreInst(src) || !IsLoadOrStoreInst(dst)) |
| 88 | return true; |
| 89 | |
| 90 | Value *srcPtr = GetPointerOperand(src); |
| 91 | Value *dstPtr = GetPointerOperand(dst); |
| 92 | const Value *srcObj = srcPtr->getUnderlyingObject(); |
| 93 | const Value *dstObj = dstPtr->getUnderlyingObject(); |
| 94 | const Type *srcTy = srcObj->getType(); |
| 95 | const Type *dstTy = dstObj->getType(); |
| 96 | |
| 97 | // For now, we only work on (pointers to) global or stack-allocated array |
| 98 | // values, as we know that their underlying memory areas will not overlap. |
| 99 | // MAYBE: relax this and test for aliasing? |
| 100 | if (!((isa<GlobalVariable>(srcObj) || isa<AllocaInst>(srcObj)) && |
| 101 | (isa<GlobalVariable>(dstObj) || isa<AllocaInst>(dstObj)) && |
| 102 | isa<PointerType>(srcTy) && |
| 103 | isa<PointerType>(dstTy) && |
| 104 | isa<ArrayType>(cast<PointerType>(srcTy)->getElementType()) && |
| 105 | isa<ArrayType>(cast<PointerType>(dstTy)->getElementType()))) |
| 106 | return true; |
| 107 | |
| 108 | // If the arrays are different, the underlying memory areas do not overlap |
| 109 | // and the memory accesses are therefore independent. |
| 110 | if (srcObj != dstObj) |
| 111 | return false; |
| 112 | |
| 113 | // We couldn't establish a more precise result, so we have to conservatively |
| 114 | // assume full dependence. |
Andreas Bolka | f17ab7f | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
| 118 | //===----------------------------------------------------------------------===// |
Andreas Bolka | 306b5b2 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 119 | // LoopDependenceAnalysis Implementation |
| 120 | //===----------------------------------------------------------------------===// |
| 121 | |
| 122 | bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) { |
| 123 | this->L = L; |
| 124 | SE = &getAnalysis<ScalarEvolution>(); |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 129 | AU.setPreservesAll(); |
Andreas Bolka | b88ee91 | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 130 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 131 | } |
| 132 | |
| 133 | static void PrintLoopInfo( |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 134 | raw_ostream &OS, LoopDependenceAnalysis *LDA, const Loop *L) { |
Andreas Bolka | b88ee91 | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 135 | if (!L->empty()) return; // ignore non-innermost loops |
| 136 | |
| 137 | OS << "Loop at depth " << L->getLoopDepth() << ", header block: "; |
| 138 | WriteAsOperand(OS, L->getHeader(), false); |
| 139 | OS << "\n"; |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 140 | |
| 141 | SmallVector<Instruction*, 8> memrefs; |
Andreas Bolka | b2662e4 | 2009-06-29 00:53:49 +0000 | [diff] [blame] | 142 | GetMemRefInstrs(L, memrefs); |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 143 | OS << " Load/store instructions: " << memrefs.size() << "\n"; |
| 144 | OS << " Pairwise dependence results:\n"; |
| 145 | for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), |
| 146 | end = memrefs.end(); x != end; ++x) |
| 147 | for (SmallVector<Instruction*, 8>::const_iterator y = x + 1; |
| 148 | y != end; ++y) |
| 149 | if (LDA->isDependencePair(*x, *y)) |
| 150 | OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin()) |
| 151 | << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent") |
| 152 | << "\n"; |
Andreas Bolka | b88ee91 | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const { |
Andreas Bolka | 158c590 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 156 | // TODO: doc why const_cast is safe |
| 157 | PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L); |
Andreas Bolka | b88ee91 | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const { |
| 161 | raw_os_ostream os(OS); |
| 162 | print(os, M); |
Andreas Bolka | 306b5b2 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 163 | } |