LLVM API Documentation
00001 //===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This is the (beginning) of an implementation of a loop dependence analysis 00011 // framework, which is used to detect dependences in memory accesses in loops. 00012 // 00013 // Please note that this is work in progress and the interface is subject to 00014 // change. 00015 // 00016 // TODO: adapt as implementation progresses. 00017 // 00018 // TODO: document lingo (pair, subscript, index) 00019 // 00020 //===----------------------------------------------------------------------===// 00021 00022 #define DEBUG_TYPE "lda" 00023 #include "llvm/ADT/DenseSet.h" 00024 #include "llvm/ADT/Statistic.h" 00025 #include "llvm/Analysis/AliasAnalysis.h" 00026 #include "llvm/Analysis/LoopDependenceAnalysis.h" 00027 #include "llvm/Analysis/LoopPass.h" 00028 #include "llvm/Analysis/ScalarEvolution.h" 00029 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 00030 #include "llvm/Instructions.h" 00031 #include "llvm/Operator.h" 00032 #include "llvm/Support/Allocator.h" 00033 #include "llvm/Support/Debug.h" 00034 #include "llvm/Support/ErrorHandling.h" 00035 #include "llvm/Support/raw_ostream.h" 00036 #include "llvm/Target/TargetData.h" 00037 using namespace llvm; 00038 00039 STATISTIC(NumAnswered, "Number of dependence queries answered"); 00040 STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed"); 00041 STATISTIC(NumDependent, "Number of pairs with dependent accesses"); 00042 STATISTIC(NumIndependent, "Number of pairs with independent accesses"); 00043 STATISTIC(NumUnknown, "Number of pairs with unknown accesses"); 00044 00045 LoopPass *llvm::createLoopDependenceAnalysisPass() { 00046 return new LoopDependenceAnalysis(); 00047 } 00048 00049 static RegisterPass<LoopDependenceAnalysis> 00050 R("lda", "Loop Dependence Analysis", false, true); 00051 char LoopDependenceAnalysis::ID = 0; 00052 00053 //===----------------------------------------------------------------------===// 00054 // Utility Functions 00055 //===----------------------------------------------------------------------===// 00056 00057 static inline bool IsMemRefInstr(const Value *V) { 00058 const Instruction *I = dyn_cast<const Instruction>(V); 00059 return I && (I->mayReadFromMemory() || I->mayWriteToMemory()); 00060 } 00061 00062 static void GetMemRefInstrs(const Loop *L, 00063 SmallVectorImpl<Instruction*> &Memrefs) { 00064 for (Loop::block_iterator b = L->block_begin(), be = L->block_end(); 00065 b != be; ++b) 00066 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end(); 00067 i != ie; ++i) 00068 if (IsMemRefInstr(i)) 00069 Memrefs.push_back(i); 00070 } 00071 00072 static bool IsLoadOrStoreInst(Value *I) { 00073 return isa<LoadInst>(I) || isa<StoreInst>(I); 00074 } 00075 00076 static Value *GetPointerOperand(Value *I) { 00077 if (LoadInst *i = dyn_cast<LoadInst>(I)) 00078 return i->getPointerOperand(); 00079 if (StoreInst *i = dyn_cast<StoreInst>(I)) 00080 return i->getPointerOperand(); 00081 llvm_unreachable("Value is no load or store instruction!"); 00082 // Never reached. 00083 return 0; 00084 } 00085 00086 static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA, 00087 const Value *A, 00088 const Value *B) { 00089 const Value *aObj = A->getUnderlyingObject(); 00090 const Value *bObj = B->getUnderlyingObject(); 00091 return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()), 00092 bObj, AA->getTypeStoreSize(bObj->getType())); 00093 } 00094 00095 static inline const SCEV *GetZeroSCEV(ScalarEvolution *SE) { 00096 return SE->getConstant(Type::getInt32Ty(SE->getContext()), 0L); 00097 } 00098 00099 //===----------------------------------------------------------------------===// 00100 // Dependence Testing 00101 //===----------------------------------------------------------------------===// 00102 00103 bool LoopDependenceAnalysis::isDependencePair(const Value *A, 00104 const Value *B) const { 00105 return IsMemRefInstr(A) && 00106 IsMemRefInstr(B) && 00107 (cast<const Instruction>(A)->mayWriteToMemory() || 00108 cast<const Instruction>(B)->mayWriteToMemory()); 00109 } 00110 00111 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A, 00112 Value *B, 00113 DependencePair *&P) { 00114 void *insertPos = 0; 00115 FoldingSetNodeID id; 00116 id.AddPointer(A); 00117 id.AddPointer(B); 00118 00119 P = Pairs.FindNodeOrInsertPos(id, insertPos); 00120 if (P) return true; 00121 00122 P = PairAllocator.Allocate<DependencePair>(); 00123 new (P) DependencePair(id, A, B); 00124 Pairs.InsertNode(P, insertPos); 00125 return false; 00126 } 00127 00128 void LoopDependenceAnalysis::getLoops(const SCEV *S, 00129 DenseSet<const Loop*>* Loops) const { 00130 // Refactor this into an SCEVVisitor, if efficiency becomes a concern. 00131 for (const Loop *L = this->L; L != 0; L = L->getParentLoop()) 00132 if (!S->isLoopInvariant(L)) 00133 Loops->insert(L); 00134 } 00135 00136 bool LoopDependenceAnalysis::isLoopInvariant(const SCEV *S) const { 00137 DenseSet<const Loop*> loops; 00138 getLoops(S, &loops); 00139 return loops.empty(); 00140 } 00141 00142 bool LoopDependenceAnalysis::isAffine(const SCEV *S) const { 00143 const SCEVAddRecExpr *rec = dyn_cast<SCEVAddRecExpr>(S); 00144 return isLoopInvariant(S) || (rec && rec->isAffine()); 00145 } 00146 00147 bool LoopDependenceAnalysis::isZIVPair(const SCEV *A, const SCEV *B) const { 00148 return isLoopInvariant(A) && isLoopInvariant(B); 00149 } 00150 00151 bool LoopDependenceAnalysis::isSIVPair(const SCEV *A, const SCEV *B) const { 00152 DenseSet<const Loop*> loops; 00153 getLoops(A, &loops); 00154 getLoops(B, &loops); 00155 return loops.size() == 1; 00156 } 00157 00158 LoopDependenceAnalysis::DependenceResult 00159 LoopDependenceAnalysis::analyseZIV(const SCEV *A, 00160 const SCEV *B, 00161 Subscript *S) const { 00162 assert(isZIVPair(A, B) && "Attempted to ZIV-test non-ZIV SCEVs!"); 00163 return A == B ? Dependent : Independent; 00164 } 00165 00166 LoopDependenceAnalysis::DependenceResult 00167 LoopDependenceAnalysis::analyseSIV(const SCEV *A, 00168 const SCEV *B, 00169 Subscript *S) const { 00170 return Unknown; // TODO: Implement. 00171 } 00172 00173 LoopDependenceAnalysis::DependenceResult 00174 LoopDependenceAnalysis::analyseMIV(const SCEV *A, 00175 const SCEV *B, 00176 Subscript *S) const { 00177 return Unknown; // TODO: Implement. 00178 } 00179 00180 LoopDependenceAnalysis::DependenceResult 00181 LoopDependenceAnalysis::analyseSubscript(const SCEV *A, 00182 const SCEV *B, 00183 Subscript *S) const { 00184 DEBUG(errs() << " Testing subscript: " << *A << ", " << *B << "\n"); 00185 00186 if (A == B) { 00187 DEBUG(errs() << " -> [D] same SCEV\n"); 00188 return Dependent; 00189 } 00190 00191 if (!isAffine(A) || !isAffine(B)) { 00192 DEBUG(errs() << " -> [?] not affine\n"); 00193 return Unknown; 00194 } 00195 00196 if (isZIVPair(A, B)) 00197 return analyseZIV(A, B, S); 00198 00199 if (isSIVPair(A, B)) 00200 return analyseSIV(A, B, S); 00201 00202 return analyseMIV(A, B, S); 00203 } 00204 00205 LoopDependenceAnalysis::DependenceResult 00206 LoopDependenceAnalysis::analysePair(DependencePair *P) const { 00207 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n"); 00208 00209 // We only analyse loads and stores but no possible memory accesses by e.g. 00210 // free, call, or invoke instructions. 00211 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) { 00212 DEBUG(errs() << "--> [?] no load/store\n"); 00213 return Unknown; 00214 } 00215 00216 Value *aPtr = GetPointerOperand(P->A); 00217 Value *bPtr = GetPointerOperand(P->B); 00218 00219 switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) { 00220 case AliasAnalysis::MayAlias: 00221 // We can not analyse objects if we do not know about their aliasing. 00222 DEBUG(errs() << "---> [?] may alias\n"); 00223 return Unknown; 00224 00225 case AliasAnalysis::NoAlias: 00226 // If the objects noalias, they are distinct, accesses are independent. 00227 DEBUG(errs() << "---> [I] no alias\n"); 00228 return Independent; 00229 00230 case AliasAnalysis::MustAlias: 00231 break; // The underlying objects alias, test accesses for dependence. 00232 } 00233 00234 const GEPOperator *aGEP = dyn_cast<GEPOperator>(aPtr); 00235 const GEPOperator *bGEP = dyn_cast<GEPOperator>(bPtr); 00236 00237 if (!aGEP || !bGEP) 00238 return Unknown; 00239 00240 // FIXME: Is filtering coupled subscripts necessary? 00241 00242 // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding 00243 // trailing zeroes to the smaller GEP, if needed. 00244 typedef SmallVector<std::pair<const SCEV*, const SCEV*>, 4> GEPOpdPairsTy; 00245 GEPOpdPairsTy opds; 00246 for(GEPOperator::const_op_iterator aIdx = aGEP->idx_begin(), 00247 aEnd = aGEP->idx_end(), 00248 bIdx = bGEP->idx_begin(), 00249 bEnd = bGEP->idx_end(); 00250 aIdx != aEnd && bIdx != bEnd; 00251 aIdx += (aIdx != aEnd), bIdx += (bIdx != bEnd)) { 00252 const SCEV* aSCEV = (aIdx != aEnd) ? SE->getSCEV(*aIdx) : GetZeroSCEV(SE); 00253 const SCEV* bSCEV = (bIdx != bEnd) ? SE->getSCEV(*bIdx) : GetZeroSCEV(SE); 00254 opds.push_back(std::make_pair(aSCEV, bSCEV)); 00255 } 00256 00257 if (!opds.empty() && opds[0].first != opds[0].second) { 00258 // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting 00259 // 00260 // TODO: this could be relaxed by adding the size of the underlying object 00261 // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we 00262 // know that x is a [100 x i8]*, we could modify the first subscript to be 00263 // (i, 200-i) instead of (i, -i). 00264 return Unknown; 00265 } 00266 00267 // Now analyse the collected operand pairs (skipping the GEP ptr offsets). 00268 for (GEPOpdPairsTy::const_iterator i = opds.begin() + 1, end = opds.end(); 00269 i != end; ++i) { 00270 Subscript subscript; 00271 DependenceResult result = analyseSubscript(i->first, i->second, &subscript); 00272 if (result != Dependent) { 00273 // We either proved independence or failed to analyse this subscript. 00274 // Further subscripts will not improve the situation, so abort early. 00275 return result; 00276 } 00277 P->Subscripts.push_back(subscript); 00278 } 00279 // We successfully analysed all subscripts but failed to prove independence. 00280 return Dependent; 00281 } 00282 00283 bool LoopDependenceAnalysis::depends(Value *A, Value *B) { 00284 assert(isDependencePair(A, B) && "Values form no dependence pair!"); 00285 ++NumAnswered; 00286 00287 DependencePair *p; 00288 if (!findOrInsertDependencePair(A, B, p)) { 00289 // The pair is not cached, so analyse it. 00290 ++NumAnalysed; 00291 switch (p->Result = analysePair(p)) { 00292 case Dependent: ++NumDependent; break; 00293 case Independent: ++NumIndependent; break; 00294 case Unknown: ++NumUnknown; break; 00295 } 00296 } 00297 return p->Result != Independent; 00298 } 00299 00300 //===----------------------------------------------------------------------===// 00301 // LoopDependenceAnalysis Implementation 00302 //===----------------------------------------------------------------------===// 00303 00304 bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) { 00305 this->L = L; 00306 AA = &getAnalysis<AliasAnalysis>(); 00307 SE = &getAnalysis<ScalarEvolution>(); 00308 return false; 00309 } 00310 00311 void LoopDependenceAnalysis::releaseMemory() { 00312 Pairs.clear(); 00313 PairAllocator.Reset(); 00314 } 00315 00316 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00317 AU.setPreservesAll(); 00318 AU.addRequiredTransitive<AliasAnalysis>(); 00319 AU.addRequiredTransitive<ScalarEvolution>(); 00320 } 00321 00322 static void PrintLoopInfo(raw_ostream &OS, 00323 LoopDependenceAnalysis *LDA, const Loop *L) { 00324 if (!L->empty()) return; // ignore non-innermost loops 00325 00326 SmallVector<Instruction*, 8> memrefs; 00327 GetMemRefInstrs(L, memrefs); 00328 00329 OS << "Loop at depth " << L->getLoopDepth() << ", header block: "; 00330 WriteAsOperand(OS, L->getHeader(), false); 00331 OS << "\n"; 00332 00333 OS << " Load/store instructions: " << memrefs.size() << "\n"; 00334 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), 00335 end = memrefs.end(); x != end; ++x) 00336 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n"; 00337 00338 OS << " Pairwise dependence results:\n"; 00339 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(), 00340 end = memrefs.end(); x != end; ++x) 00341 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1; 00342 y != end; ++y) 00343 if (LDA->isDependencePair(*x, *y)) 00344 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin()) 00345 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent") 00346 << "\n"; 00347 } 00348 00349 void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const { 00350 // TODO: doc why const_cast is safe 00351 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L); 00352 } 00353 00354 void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const { 00355 raw_os_ostream os(OS); 00356 print(os, M); 00357 }