Andreas Bolka | cb21010 | 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" |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/AliasAnalysis.h" |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/LoopDependenceAnalysis.h" |
| 23 | #include "llvm/Analysis/LoopPass.h" |
| 24 | #include "llvm/Analysis/ScalarEvolution.h" |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 25 | #include "llvm/Instructions.h" |
Andreas Bolka | 0baa25d | 2009-07-24 23:19:28 +0000 | [diff] [blame] | 26 | #include "llvm/Support/Allocator.h" |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 27 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 28 | #include "llvm/Support/ErrorHandling.h" |
Andreas Bolka | 0baa25d | 2009-07-24 23:19:28 +0000 | [diff] [blame] | 29 | #include "llvm/Support/raw_ostream.h" |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 30 | #include "llvm/Target/TargetData.h" |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | LoopPass *llvm::createLoopDependenceAnalysisPass() { |
| 34 | return new LoopDependenceAnalysis(); |
| 35 | } |
| 36 | |
| 37 | static RegisterPass<LoopDependenceAnalysis> |
| 38 | R("lda", "Loop Dependence Analysis", false, true); |
| 39 | char LoopDependenceAnalysis::ID = 0; |
| 40 | |
| 41 | //===----------------------------------------------------------------------===// |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 42 | // Utility Functions |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | |
Andreas Bolka | 2fbb770 | 2009-06-29 18:51:11 +0000 | [diff] [blame] | 45 | static inline bool IsMemRefInstr(const Value *V) { |
| 46 | const Instruction *I = dyn_cast<const Instruction>(V); |
| 47 | return I && (I->mayReadFromMemory() || I->mayWriteToMemory()); |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 50 | static void GetMemRefInstrs(const Loop *L, |
| 51 | SmallVectorImpl<Instruction*> &Memrefs) { |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 52 | 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 Bolka | acd6f8d | 2009-06-29 00:50:26 +0000 | [diff] [blame] | 56 | if (IsMemRefInstr(i)) |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 57 | Memrefs.push_back(i); |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 60 | static bool IsLoadOrStoreInst(Value *I) { |
| 61 | return isa<LoadInst>(I) || isa<StoreInst>(I); |
| 62 | } |
| 63 | |
| 64 | static 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 Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 69 | llvm_unreachable("Value is no load or store instruction!"); |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 70 | // Never reached. |
| 71 | return 0; |
| 72 | } |
| 73 | |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 74 | //===----------------------------------------------------------------------===// |
| 75 | // Dependence Testing |
| 76 | //===----------------------------------------------------------------------===// |
| 77 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 78 | bool 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 Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 84 | } |
| 85 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 86 | bool 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 Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 93 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 94 | P = Pairs.FindNodeOrInsertPos(id, insertPos); |
| 95 | if (P) return true; |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 96 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 97 | P = PairAllocator.Allocate<DependencePair>(); |
| 98 | new (P) DependencePair(id, X, Y); |
| 99 | Pairs.InsertNode(P, insertPos); |
| 100 | return false; |
| 101 | } |
| 102 | |
| 103 | void 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 Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 121 | AliasAnalysis::AliasResult alias = AA->alias( |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 122 | aobj, AA->getTargetData().getTypeStoreSize(aobj->getType()), |
| 123 | bobj, AA->getTargetData().getTypeStoreSize(bobj->getType())); |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 124 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 125 | // 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 Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 130 | |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 131 | // If the objects noalias, they are distinct, accesses are independent. |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 132 | if (alias == AliasAnalysis::NoAlias) { |
| 133 | DOUT << "---> [I] no alias\n"; |
| 134 | P->Result = Independent; |
| 135 | return; |
| 136 | } |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 137 | |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 138 | // TODO: the underlying objects MustAlias, test for dependence |
| 139 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 140 | DOUT << "---> [?] cannot analyse\n"; |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | bool 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 Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | //===----------------------------------------------------------------------===// |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 156 | // LoopDependenceAnalysis Implementation |
| 157 | //===----------------------------------------------------------------------===// |
| 158 | |
| 159 | bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) { |
| 160 | this->L = L; |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 161 | AA = &getAnalysis<AliasAnalysis>(); |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 162 | SE = &getAnalysis<ScalarEvolution>(); |
| 163 | return false; |
| 164 | } |
| 165 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 166 | void LoopDependenceAnalysis::releaseMemory() { |
| 167 | Pairs.clear(); |
| 168 | PairAllocator.Reset(); |
| 169 | } |
| 170 | |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 171 | void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 172 | AU.setPreservesAll(); |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 173 | AU.addRequiredTransitive<AliasAnalysis>(); |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 174 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 175 | } |
| 176 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 177 | static void PrintLoopInfo(raw_ostream &OS, |
| 178 | LoopDependenceAnalysis *LDA, const Loop *L) { |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 179 | if (!L->empty()) return; // ignore non-innermost loops |
| 180 | |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 181 | SmallVector<Instruction*, 8> memrefs; |
| 182 | GetMemRefInstrs(L, memrefs); |
| 183 | |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 184 | OS << "Loop at depth " << L->getLoopDepth() << ", header block: "; |
| 185 | WriteAsOperand(OS, L->getHeader(), false); |
| 186 | OS << "\n"; |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 187 | |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 188 | OS << " Load/store instructions: " << memrefs.size() << "\n"; |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 189 | for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), |
| 190 | end = memrefs.end(); x != end; ++x) |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 191 | OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n"; |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 192 | |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 193 | 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 Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const { |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 205 | // TODO: doc why const_cast is safe |
| 206 | PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L); |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const { |
| 210 | raw_os_ostream os(OS); |
| 211 | print(os, M); |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 212 | } |