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 | // |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 18 | // TODO: document lingo (pair, subscript, index) |
| 19 | // |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #define DEBUG_TYPE "lda" |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/DenseSet.h" |
Andreas Bolka | 328fb3d | 2009-07-28 19:49:49 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/Statistic.h" |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 25 | #include "llvm/Analysis/AliasAnalysis.h" |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/LoopDependenceAnalysis.h" |
| 27 | #include "llvm/Analysis/LoopPass.h" |
| 28 | #include "llvm/Analysis/ScalarEvolution.h" |
Andreas Bolka | 5eca452 | 2009-08-03 01:03:48 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/ScalarEvolutionExpressions.h" |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 30 | #include "llvm/Analysis/ValueTracking.h" |
Chris Lattner | 9fc5cdf | 2011-01-02 22:09:33 +0000 | [diff] [blame] | 31 | #include "llvm/Assembly/Writer.h" |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 32 | #include "llvm/Instructions.h" |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 33 | #include "llvm/Operator.h" |
Andreas Bolka | 0baa25d | 2009-07-24 23:19:28 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Allocator.h" |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 35 | #include "llvm/Support/Debug.h" |
Torok Edwin | c25e758 | 2009-07-11 20:10:48 +0000 | [diff] [blame] | 36 | #include "llvm/Support/ErrorHandling.h" |
Andreas Bolka | 0baa25d | 2009-07-24 23:19:28 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 38 | #include "llvm/Target/TargetData.h" |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 39 | using namespace llvm; |
| 40 | |
Andreas Bolka | 328fb3d | 2009-07-28 19:49:49 +0000 | [diff] [blame] | 41 | STATISTIC(NumAnswered, "Number of dependence queries answered"); |
| 42 | STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed"); |
| 43 | STATISTIC(NumDependent, "Number of pairs with dependent accesses"); |
| 44 | STATISTIC(NumIndependent, "Number of pairs with independent accesses"); |
| 45 | STATISTIC(NumUnknown, "Number of pairs with unknown accesses"); |
| 46 | |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 47 | LoopPass *llvm::createLoopDependenceAnalysisPass() { |
| 48 | return new LoopDependenceAnalysis(); |
| 49 | } |
| 50 | |
Owen Anderson | 2ab36d3 | 2010-10-12 19:48:12 +0000 | [diff] [blame] | 51 | INITIALIZE_PASS_BEGIN(LoopDependenceAnalysis, "lda", |
| 52 | "Loop Dependence Analysis", false, true) |
| 53 | INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) |
| 54 | INITIALIZE_AG_DEPENDENCY(AliasAnalysis) |
| 55 | INITIALIZE_PASS_END(LoopDependenceAnalysis, "lda", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 56 | "Loop Dependence Analysis", false, true) |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 57 | char LoopDependenceAnalysis::ID = 0; |
| 58 | |
| 59 | //===----------------------------------------------------------------------===// |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 60 | // Utility Functions |
| 61 | //===----------------------------------------------------------------------===// |
| 62 | |
Andreas Bolka | 2fbb770 | 2009-06-29 18:51:11 +0000 | [diff] [blame] | 63 | static inline bool IsMemRefInstr(const Value *V) { |
| 64 | const Instruction *I = dyn_cast<const Instruction>(V); |
| 65 | return I && (I->mayReadFromMemory() || I->mayWriteToMemory()); |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 66 | } |
| 67 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 68 | static void GetMemRefInstrs(const Loop *L, |
| 69 | SmallVectorImpl<Instruction*> &Memrefs) { |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 70 | for (Loop::block_iterator b = L->block_begin(), be = L->block_end(); |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 71 | b != be; ++b) |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 72 | for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end(); |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 73 | i != ie; ++i) |
Andreas Bolka | acd6f8d | 2009-06-29 00:50:26 +0000 | [diff] [blame] | 74 | if (IsMemRefInstr(i)) |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 75 | Memrefs.push_back(i); |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 76 | } |
| 77 | |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 78 | static bool IsLoadOrStoreInst(Value *I) { |
| 79 | return isa<LoadInst>(I) || isa<StoreInst>(I); |
| 80 | } |
| 81 | |
| 82 | static Value *GetPointerOperand(Value *I) { |
| 83 | if (LoadInst *i = dyn_cast<LoadInst>(I)) |
| 84 | return i->getPointerOperand(); |
| 85 | if (StoreInst *i = dyn_cast<StoreInst>(I)) |
| 86 | return i->getPointerOperand(); |
Torok Edwin | c23197a | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 87 | llvm_unreachable("Value is no load or store instruction!"); |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 88 | // Never reached. |
| 89 | return 0; |
| 90 | } |
| 91 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 92 | static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA, |
| 93 | const Value *A, |
| 94 | const Value *B) { |
Dan Gohman | 5034dd3 | 2010-12-15 20:02:24 +0000 | [diff] [blame] | 95 | const Value *aObj = GetUnderlyingObject(A); |
| 96 | const Value *bObj = GetUnderlyingObject(B); |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 97 | return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()), |
| 98 | bObj, AA->getTypeStoreSize(bObj->getType())); |
| 99 | } |
| 100 | |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 101 | static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) { |
Owen Anderson | 1d0be15 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 102 | return SE->getConstant(Type::getInt32Ty(SE->getContext()), 0L); |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 105 | //===----------------------------------------------------------------------===// |
| 106 | // Dependence Testing |
| 107 | //===----------------------------------------------------------------------===// |
| 108 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 109 | bool LoopDependenceAnalysis::isDependencePair(const Value *A, |
| 110 | const Value *B) const { |
| 111 | return IsMemRefInstr(A) && |
| 112 | IsMemRefInstr(B) && |
| 113 | (cast<const Instruction>(A)->mayWriteToMemory() || |
| 114 | cast<const Instruction>(B)->mayWriteToMemory()); |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 117 | bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A, |
| 118 | Value *B, |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 119 | DependencePair *&P) { |
| 120 | void *insertPos = 0; |
| 121 | FoldingSetNodeID id; |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 122 | id.AddPointer(A); |
| 123 | id.AddPointer(B); |
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 | P = Pairs.FindNodeOrInsertPos(id, insertPos); |
| 126 | if (P) return true; |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 127 | |
Dan Gohman | 9553188 | 2010-03-18 18:49:47 +0000 | [diff] [blame] | 128 | P = new (PairAllocator) DependencePair(id, A, B); |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 129 | Pairs.InsertNode(P, insertPos); |
| 130 | return false; |
| 131 | } |
| 132 | |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 133 | void LoopDependenceAnalysis::getLoops(const SCEV *S, |
| 134 | DenseSet<const Loop*>* Loops) const { |
| 135 | // Refactor this into an SCEVVisitor, if efficiency becomes a concern. |
Andreas Bolka | 5eca452 | 2009-08-03 01:03:48 +0000 | [diff] [blame] | 136 | for (const Loop *L = this->L; L != 0; L = L->getParentLoop()) |
Dan Gohman | 17ead4f | 2010-11-17 21:23:15 +0000 | [diff] [blame] | 137 | if (!SE->isLoopInvariant(S, L)) |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 138 | Loops->insert(L); |
| 139 | } |
| 140 | |
| 141 | bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const { |
| 142 | DenseSet<const Loop*> loops; |
| 143 | getLoops(S, &loops); |
| 144 | return loops.empty(); |
Andreas Bolka | 5eca452 | 2009-08-03 01:03:48 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | bool LoopDependenceAnalysis::isAffine(const SCEV *S) const { |
| 148 | const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S); |
| 149 | return isLoopInvariant(S) || (rec && rec->isAffine()); |
| 150 | } |
| 151 | |
Andreas Bolka | 831f6f6 | 2009-08-05 04:26:05 +0000 | [diff] [blame] | 152 | bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const { |
| 153 | return isLoopInvariant(A) && isLoopInvariant(B); |
| 154 | } |
| 155 | |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 156 | bool LoopDependenceAnalysis::isSIVPair(const SCEV *A, const SCEV *B) const { |
| 157 | DenseSet<const Loop*> loops; |
| 158 | getLoops(A, &loops); |
| 159 | getLoops(B, &loops); |
| 160 | return loops.size() == 1; |
| 161 | } |
| 162 | |
Andreas Bolka | 831f6f6 | 2009-08-05 04:26:05 +0000 | [diff] [blame] | 163 | LoopDependenceAnalysis::DependenceResult |
| 164 | LoopDependenceAnalysis::analyseZIV(const SCEV *A, |
| 165 | const SCEV *B, |
| 166 | Subscript *S) const { |
Andreas Bolka | 6151f67 | 2009-08-06 03:10:33 +0000 | [diff] [blame] | 167 | assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!"); |
| 168 | return A == B ? Dependent : Independent; |
Andreas Bolka | 831f6f6 | 2009-08-05 04:26:05 +0000 | [diff] [blame] | 169 | } |
| 170 | |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 171 | LoopDependenceAnalysis::DependenceResult |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 172 | LoopDependenceAnalysis::analyseSIV(const SCEV *A, |
| 173 | const SCEV *B, |
| 174 | Subscript *S) const { |
| 175 | return Unknown; // TODO: Implement. |
| 176 | } |
| 177 | |
| 178 | LoopDependenceAnalysis::DependenceResult |
| 179 | LoopDependenceAnalysis::analyseMIV(const SCEV *A, |
| 180 | const SCEV *B, |
| 181 | Subscript *S) const { |
| 182 | return Unknown; // TODO: Implement. |
| 183 | } |
| 184 | |
| 185 | LoopDependenceAnalysis::DependenceResult |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 186 | LoopDependenceAnalysis::analyseSubscript(const SCEV *A, |
| 187 | const SCEV *B, |
| 188 | Subscript *S) const { |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 189 | DEBUG(dbgs() << " Testing subscript: " << *A << ", " << *B << "\n"); |
Andreas Bolka | 7133959 | 2009-07-30 02:26:01 +0000 | [diff] [blame] | 190 | |
| 191 | if (A == B) { |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 192 | DEBUG(dbgs() << " -> [D] same SCEV\n"); |
Andreas Bolka | 7133959 | 2009-07-30 02:26:01 +0000 | [diff] [blame] | 193 | return Dependent; |
| 194 | } |
| 195 | |
Andreas Bolka | 5eca452 | 2009-08-03 01:03:48 +0000 | [diff] [blame] | 196 | if (!isAffine(A) || !isAffine(B)) { |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 197 | DEBUG(dbgs() << " -> [?] not affine\n"); |
Andreas Bolka | 5eca452 | 2009-08-03 01:03:48 +0000 | [diff] [blame] | 198 | return Unknown; |
| 199 | } |
| 200 | |
Andreas Bolka | 831f6f6 | 2009-08-05 04:26:05 +0000 | [diff] [blame] | 201 | if (isZIVPair(A, B)) |
| 202 | return analyseZIV(A, B, S); |
| 203 | |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 204 | if (isSIVPair(A, B)) |
| 205 | return analyseSIV(A, B, S); |
Andreas Bolka | 7133959 | 2009-07-30 02:26:01 +0000 | [diff] [blame] | 206 | |
Andreas Bolka | 699db99 | 2009-08-07 18:23:41 +0000 | [diff] [blame] | 207 | return analyseMIV(A, B, S); |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | LoopDependenceAnalysis::DependenceResult |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 211 | LoopDependenceAnalysis::analysePair(DependencePair *P) const { |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 212 | DEBUG(dbgs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n"); |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 213 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 214 | // We only analyse loads and stores but no possible memory accesses by e.g. |
| 215 | // free, call, or invoke instructions. |
| 216 | if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) { |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 217 | DEBUG(dbgs() << "--> [?] no load/store\n"); |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 218 | return Unknown; |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 221 | Value *aPtr = GetPointerOperand(P->A); |
| 222 | Value *bPtr = GetPointerOperand(P->B); |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 223 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 224 | switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) { |
| 225 | case AliasAnalysis::MayAlias: |
Dan Gohman | d891acd | 2010-12-10 20:14:49 +0000 | [diff] [blame] | 226 | case AliasAnalysis::PartialAlias: |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 227 | // We can not analyse objects if we do not know about their aliasing. |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 228 | DEBUG(dbgs() << "---> [?] may alias\n"); |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 229 | return Unknown; |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 230 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 231 | case AliasAnalysis::NoAlias: |
| 232 | // If the objects noalias, they are distinct, accesses are independent. |
David Greene | d387b2b | 2009-12-23 20:52:41 +0000 | [diff] [blame] | 233 | DEBUG(dbgs() << "---> [I] no alias\n"); |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 234 | return Independent; |
Andreas Bolka | e9722fc | 2009-06-30 02:12:10 +0000 | [diff] [blame] | 235 | |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 236 | case AliasAnalysis::MustAlias: |
| 237 | break; // The underlying objects alias, test accesses for dependence. |
| 238 | } |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 239 | |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 240 | const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr); |
| 241 | const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr); |
| 242 | |
| 243 | if (!aGEP || !bGEP) |
| 244 | return Unknown; |
| 245 | |
| 246 | // FIXME: Is filtering coupled subscripts necessary? |
| 247 | |
Andreas Bolka | a1b78d1 | 2009-08-05 04:13:41 +0000 | [diff] [blame] | 248 | // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 249 | // trailing zeroes to the smaller GEP, if needed. |
Andreas Bolka | a1b78d1 | 2009-08-05 04:13:41 +0000 | [diff] [blame] | 250 | typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy; |
| 251 | GEPOpdPairsTy opds; |
| 252 | for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(), |
| 253 | aEnd = aGEP->idx_end(), |
| 254 | bIdx = bGEP->idx_begin(), |
| 255 | bEnd = bGEP->idx_end(); |
| 256 | aIdx != aEnd && bIdx != bEnd; |
| 257 | aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) { |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 258 | const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE); |
| 259 | const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE); |
Andreas Bolka | a1b78d1 | 2009-08-05 04:13:41 +0000 | [diff] [blame] | 260 | opds.push_back(std::make_pair(aSCEV, bSCEV)); |
| 261 | } |
| 262 | |
| 263 | if (!opds.empty() && opds[0].first != opds[0].second) { |
| 264 | // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting |
| 265 | // |
| 266 | // TODO: this could be relaxed by adding the size of the underlying object |
| 267 | // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we |
| 268 | // know that x is a [100 x i8]*, we could modify the first subscript to be |
| 269 | // (i, 200-i) instead of (i, -i). |
| 270 | return Unknown; |
| 271 | } |
| 272 | |
| 273 | // Now analyse the collected operand pairs (skipping the GEP ptr offsets). |
| 274 | for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end(); |
| 275 | i != end; ++i) { |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 276 | Subscript subscript; |
Andreas Bolka | a1b78d1 | 2009-08-05 04:13:41 +0000 | [diff] [blame] | 277 | DependenceResult result = analyseSubscript(i->first, i->second, &subscript); |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 278 | if (result != Dependent) { |
| 279 | // We either proved independence or failed to analyse this subscript. |
| 280 | // Further subscripts will not improve the situation, so abort early. |
| 281 | return result; |
| 282 | } |
| 283 | P->Subscripts.push_back(subscript); |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 284 | } |
Andreas Bolka | a1b78d1 | 2009-08-05 04:13:41 +0000 | [diff] [blame] | 285 | // We successfully analysed all subscripts but failed to prove independence. |
Andreas Bolka | 15f72db | 2009-07-29 05:35:53 +0000 | [diff] [blame] | 286 | return Dependent; |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | bool LoopDependenceAnalysis::depends(Value *A, Value *B) { |
| 290 | assert(isDependencePair(A, B) && "Values form no dependence pair!"); |
Andreas Bolka | 328fb3d | 2009-07-28 19:49:49 +0000 | [diff] [blame] | 291 | ++NumAnswered; |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 292 | |
| 293 | DependencePair *p; |
| 294 | if (!findOrInsertDependencePair(A, B, p)) { |
| 295 | // The pair is not cached, so analyse it. |
Andreas Bolka | 328fb3d | 2009-07-28 19:49:49 +0000 | [diff] [blame] | 296 | ++NumAnalysed; |
Andreas Bolka | c3cc45a | 2009-07-28 19:50:13 +0000 | [diff] [blame] | 297 | switch (p->Result = analysePair(p)) { |
Andreas Bolka | 328fb3d | 2009-07-28 19:49:49 +0000 | [diff] [blame] | 298 | case Dependent: ++NumDependent; break; |
| 299 | case Independent: ++NumIndependent; break; |
| 300 | case Unknown: ++NumUnknown; break; |
| 301 | } |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 302 | } |
| 303 | return p->Result != Independent; |
Andreas Bolka | f35626d | 2009-06-28 00:21:21 +0000 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | //===----------------------------------------------------------------------===// |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 307 | // LoopDependenceAnalysis Implementation |
| 308 | //===----------------------------------------------------------------------===// |
| 309 | |
| 310 | bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) { |
| 311 | this->L = L; |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 312 | AA = &getAnalysis<AliasAnalysis>(); |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 313 | SE = &getAnalysis<ScalarEvolution>(); |
| 314 | return false; |
| 315 | } |
| 316 | |
Andreas Bolka | b4c28e9 | 2009-07-23 14:32:46 +0000 | [diff] [blame] | 317 | void LoopDependenceAnalysis::releaseMemory() { |
| 318 | Pairs.clear(); |
| 319 | PairAllocator.Reset(); |
| 320 | } |
| 321 | |
Andreas Bolka | cb21010 | 2009-06-24 21:29:13 +0000 | [diff] [blame] | 322 | void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { |
| 323 | AU.setPreservesAll(); |
Andreas Bolka | fecbc59 | 2009-07-01 21:45:23 +0000 | [diff] [blame] | 324 | AU.addRequiredTransitive<AliasAnalysis>(); |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 325 | AU.addRequiredTransitive<ScalarEvolution>(); |
| 326 | } |
| 327 | |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 328 | static void PrintLoopInfo(raw_ostream &OS, |
| 329 | LoopDependenceAnalysis *LDA, const Loop *L) { |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 330 | if (!L->empty()) return; // ignore non-innermost loops |
| 331 | |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 332 | SmallVector<Instruction*, 8> memrefs; |
| 333 | GetMemRefInstrs(L, memrefs); |
| 334 | |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 335 | OS << "Loop at depth " << L->getLoopDepth() << ", header block: "; |
| 336 | WriteAsOperand(OS, L->getHeader(), false); |
| 337 | OS << "\n"; |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 338 | |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 339 | OS << " Load/store instructions: " << memrefs.size() << "\n"; |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 340 | for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 341 | end = memrefs.end(); x != end; ++x) |
Andreas Bolka | 3b59dd8 | 2009-07-23 01:57:06 +0000 | [diff] [blame] | 342 | OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n"; |
Andreas Bolka | 292aef3 | 2009-07-03 01:42:52 +0000 | [diff] [blame] | 343 | |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 344 | OS << " Pairwise dependence results:\n"; |
| 345 | for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 346 | end = memrefs.end(); x != end; ++x) |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 347 | for (SmallVector<Instruction*, 8>::const_iterator y = x + 1; |
Andreas Bolka | 34ce687 | 2009-07-28 19:49:25 +0000 | [diff] [blame] | 348 | y != end; ++y) |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 349 | if (LDA->isDependencePair(*x, *y)) |
| 350 | OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin()) |
| 351 | << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent") |
| 352 | << "\n"; |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const { |
Andreas Bolka | c6a3030 | 2009-06-28 00:35:22 +0000 | [diff] [blame] | 356 | // TODO: doc why const_cast is safe |
| 357 | PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L); |
Andreas Bolka | 707207a | 2009-06-28 00:16:08 +0000 | [diff] [blame] | 358 | } |