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