LLVM API Documentation
00001 //===-- DependenceAnalysis.cpp - DA 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 // DependenceAnalysis is an LLVM pass that analyses dependences between memory 00011 // accesses. Currently, it is an (incomplete) implementation of the approach 00012 // described in 00013 // 00014 // Practical Dependence Testing 00015 // Goff, Kennedy, Tseng 00016 // PLDI 1991 00017 // 00018 // There's a single entry point that analyzes the dependence between a pair 00019 // of memory references in a function, returning either NULL, for no dependence, 00020 // or a more-or-less detailed description of the dependence between them. 00021 // 00022 // Currently, the implementation cannot propagate constraints between 00023 // coupled RDIV subscripts and lacks a multi-subscript MIV test. 00024 // Both of these are conservative weaknesses; 00025 // that is, not a source of correctness problems. 00026 // 00027 // The implementation depends on the GEP instruction to 00028 // differentiate subscripts. Since Clang linearizes subscripts 00029 // for most arrays, we give up some precision (though the existing MIV tests 00030 // will help). We trust that the GEP instruction will eventually be extended. 00031 // In the meantime, we should explore Maslov's ideas about delinearization. 00032 // 00033 // We should pay some careful attention to the possibility of integer overflow 00034 // in the implementation of the various tests. This could happen with Add, 00035 // Subtract, or Multiply, with both APInt's and SCEV's. 00036 // 00037 // Some non-linear subscript pairs can be handled by the GCD test 00038 // (and perhaps other tests). 00039 // Should explore how often these things occur. 00040 // 00041 // Finally, it seems like certain test cases expose weaknesses in the SCEV 00042 // simplification, especially in the handling of sign and zero extensions. 00043 // It could be useful to spend time exploring these. 00044 // 00045 // Please note that this is work in progress and the interface is subject to 00046 // change. 00047 // 00048 //===----------------------------------------------------------------------===// 00049 // // 00050 // In memory of Ken Kennedy, 1945 - 2007 // 00051 // // 00052 //===----------------------------------------------------------------------===// 00053 00054 #define DEBUG_TYPE "da" 00055 00056 #include "llvm/Analysis/DependenceAnalysis.h" 00057 #include "llvm/ADT/Statistic.h" 00058 #include "llvm/Analysis/AliasAnalysis.h" 00059 #include "llvm/Analysis/LoopInfo.h" 00060 #include "llvm/Analysis/ScalarEvolution.h" 00061 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 00062 #include "llvm/Analysis/ValueTracking.h" 00063 #include "llvm/IR/Operator.h" 00064 #include "llvm/Support/Debug.h" 00065 #include "llvm/Support/ErrorHandling.h" 00066 #include "llvm/Support/InstIterator.h" 00067 #include "llvm/Support/raw_ostream.h" 00068 00069 using namespace llvm; 00070 00071 //===----------------------------------------------------------------------===// 00072 // statistics 00073 00074 STATISTIC(TotalArrayPairs, "Array pairs tested"); 00075 STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs"); 00076 STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs"); 00077 STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs"); 00078 STATISTIC(ZIVapplications, "ZIV applications"); 00079 STATISTIC(ZIVindependence, "ZIV independence"); 00080 STATISTIC(StrongSIVapplications, "Strong SIV applications"); 00081 STATISTIC(StrongSIVsuccesses, "Strong SIV successes"); 00082 STATISTIC(StrongSIVindependence, "Strong SIV independence"); 00083 STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications"); 00084 STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes"); 00085 STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence"); 00086 STATISTIC(ExactSIVapplications, "Exact SIV applications"); 00087 STATISTIC(ExactSIVsuccesses, "Exact SIV successes"); 00088 STATISTIC(ExactSIVindependence, "Exact SIV independence"); 00089 STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications"); 00090 STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes"); 00091 STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence"); 00092 STATISTIC(ExactRDIVapplications, "Exact RDIV applications"); 00093 STATISTIC(ExactRDIVindependence, "Exact RDIV independence"); 00094 STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications"); 00095 STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence"); 00096 STATISTIC(DeltaApplications, "Delta applications"); 00097 STATISTIC(DeltaSuccesses, "Delta successes"); 00098 STATISTIC(DeltaIndependence, "Delta independence"); 00099 STATISTIC(DeltaPropagations, "Delta propagations"); 00100 STATISTIC(GCDapplications, "GCD applications"); 00101 STATISTIC(GCDsuccesses, "GCD successes"); 00102 STATISTIC(GCDindependence, "GCD independence"); 00103 STATISTIC(BanerjeeApplications, "Banerjee applications"); 00104 STATISTIC(BanerjeeIndependence, "Banerjee independence"); 00105 STATISTIC(BanerjeeSuccesses, "Banerjee successes"); 00106 00107 //===----------------------------------------------------------------------===// 00108 // basics 00109 00110 INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da", 00111 "Dependence Analysis", true, true) 00112 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 00113 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) 00114 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 00115 INITIALIZE_PASS_END(DependenceAnalysis, "da", 00116 "Dependence Analysis", true, true) 00117 00118 char DependenceAnalysis::ID = 0; 00119 00120 00121 FunctionPass *llvm::createDependenceAnalysisPass() { 00122 return new DependenceAnalysis(); 00123 } 00124 00125 00126 bool DependenceAnalysis::runOnFunction(Function &F) { 00127 this->F = &F; 00128 AA = &getAnalysis<AliasAnalysis>(); 00129 SE = &getAnalysis<ScalarEvolution>(); 00130 LI = &getAnalysis<LoopInfo>(); 00131 return false; 00132 } 00133 00134 00135 void DependenceAnalysis::releaseMemory() { 00136 } 00137 00138 00139 void DependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 00140 AU.setPreservesAll(); 00141 AU.addRequiredTransitive<AliasAnalysis>(); 00142 AU.addRequiredTransitive<ScalarEvolution>(); 00143 AU.addRequiredTransitive<LoopInfo>(); 00144 } 00145 00146 00147 // Used to test the dependence analyzer. 00148 // Looks through the function, noting loads and stores. 00149 // Calls depends() on every possible pair and prints out the result. 00150 // Ignores all other instructions. 00151 static 00152 void dumpExampleDependence(raw_ostream &OS, Function *F, 00153 DependenceAnalysis *DA) { 00154 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); 00155 SrcI != SrcE; ++SrcI) { 00156 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) { 00157 for (inst_iterator DstI = SrcI, DstE = inst_end(F); 00158 DstI != DstE; ++DstI) { 00159 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) { 00160 OS << "da analyze - "; 00161 if (Dependence *D = DA->depends(&*SrcI, &*DstI, true)) { 00162 D->dump(OS); 00163 for (unsigned Level = 1; Level <= D->getLevels(); Level++) { 00164 if (D->isSplitable(Level)) { 00165 OS << "da analyze - split level = " << Level; 00166 OS << ", iteration = " << *DA->getSplitIteration(D, Level); 00167 OS << "!\n"; 00168 } 00169 } 00170 delete D; 00171 } 00172 else 00173 OS << "none!\n"; 00174 } 00175 } 00176 } 00177 } 00178 } 00179 00180 00181 void DependenceAnalysis::print(raw_ostream &OS, const Module*) const { 00182 dumpExampleDependence(OS, F, const_cast<DependenceAnalysis *>(this)); 00183 } 00184 00185 //===----------------------------------------------------------------------===// 00186 // Dependence methods 00187 00188 // Returns true if this is an input dependence. 00189 bool Dependence::isInput() const { 00190 return Src->mayReadFromMemory() && Dst->mayReadFromMemory(); 00191 } 00192 00193 00194 // Returns true if this is an output dependence. 00195 bool Dependence::isOutput() const { 00196 return Src->mayWriteToMemory() && Dst->mayWriteToMemory(); 00197 } 00198 00199 00200 // Returns true if this is an flow (aka true) dependence. 00201 bool Dependence::isFlow() const { 00202 return Src->mayWriteToMemory() && Dst->mayReadFromMemory(); 00203 } 00204 00205 00206 // Returns true if this is an anti dependence. 00207 bool Dependence::isAnti() const { 00208 return Src->mayReadFromMemory() && Dst->mayWriteToMemory(); 00209 } 00210 00211 00212 // Returns true if a particular level is scalar; that is, 00213 // if no subscript in the source or destination mention the induction 00214 // variable associated with the loop at this level. 00215 // Leave this out of line, so it will serve as a virtual method anchor 00216 bool Dependence::isScalar(unsigned level) const { 00217 return false; 00218 } 00219 00220 00221 //===----------------------------------------------------------------------===// 00222 // FullDependence methods 00223 00224 FullDependence::FullDependence(Instruction *Source, 00225 Instruction *Destination, 00226 bool PossiblyLoopIndependent, 00227 unsigned CommonLevels) : 00228 Dependence(Source, Destination), 00229 Levels(CommonLevels), 00230 LoopIndependent(PossiblyLoopIndependent) { 00231 Consistent = true; 00232 DV = CommonLevels ? new DVEntry[CommonLevels] : NULL; 00233 } 00234 00235 // The rest are simple getters that hide the implementation. 00236 00237 // getDirection - Returns the direction associated with a particular level. 00238 unsigned FullDependence::getDirection(unsigned Level) const { 00239 assert(0 < Level && Level <= Levels && "Level out of range"); 00240 return DV[Level - 1].Direction; 00241 } 00242 00243 00244 // Returns the distance (or NULL) associated with a particular level. 00245 const SCEV *FullDependence::getDistance(unsigned Level) const { 00246 assert(0 < Level && Level <= Levels && "Level out of range"); 00247 return DV[Level - 1].Distance; 00248 } 00249 00250 00251 // Returns true if a particular level is scalar; that is, 00252 // if no subscript in the source or destination mention the induction 00253 // variable associated with the loop at this level. 00254 bool FullDependence::isScalar(unsigned Level) const { 00255 assert(0 < Level && Level <= Levels && "Level out of range"); 00256 return DV[Level - 1].Scalar; 00257 } 00258 00259 00260 // Returns true if peeling the first iteration from this loop 00261 // will break this dependence. 00262 bool FullDependence::isPeelFirst(unsigned Level) const { 00263 assert(0 < Level && Level <= Levels && "Level out of range"); 00264 return DV[Level - 1].PeelFirst; 00265 } 00266 00267 00268 // Returns true if peeling the last iteration from this loop 00269 // will break this dependence. 00270 bool FullDependence::isPeelLast(unsigned Level) const { 00271 assert(0 < Level && Level <= Levels && "Level out of range"); 00272 return DV[Level - 1].PeelLast; 00273 } 00274 00275 00276 // Returns true if splitting this loop will break the dependence. 00277 bool FullDependence::isSplitable(unsigned Level) const { 00278 assert(0 < Level && Level <= Levels && "Level out of range"); 00279 return DV[Level - 1].Splitable; 00280 } 00281 00282 00283 //===----------------------------------------------------------------------===// 00284 // DependenceAnalysis::Constraint methods 00285 00286 // If constraint is a point <X, Y>, returns X. 00287 // Otherwise assert. 00288 const SCEV *DependenceAnalysis::Constraint::getX() const { 00289 assert(Kind == Point && "Kind should be Point"); 00290 return A; 00291 } 00292 00293 00294 // If constraint is a point <X, Y>, returns Y. 00295 // Otherwise assert. 00296 const SCEV *DependenceAnalysis::Constraint::getY() const { 00297 assert(Kind == Point && "Kind should be Point"); 00298 return B; 00299 } 00300 00301 00302 // If constraint is a line AX + BY = C, returns A. 00303 // Otherwise assert. 00304 const SCEV *DependenceAnalysis::Constraint::getA() const { 00305 assert((Kind == Line || Kind == Distance) && 00306 "Kind should be Line (or Distance)"); 00307 return A; 00308 } 00309 00310 00311 // If constraint is a line AX + BY = C, returns B. 00312 // Otherwise assert. 00313 const SCEV *DependenceAnalysis::Constraint::getB() const { 00314 assert((Kind == Line || Kind == Distance) && 00315 "Kind should be Line (or Distance)"); 00316 return B; 00317 } 00318 00319 00320 // If constraint is a line AX + BY = C, returns C. 00321 // Otherwise assert. 00322 const SCEV *DependenceAnalysis::Constraint::getC() const { 00323 assert((Kind == Line || Kind == Distance) && 00324 "Kind should be Line (or Distance)"); 00325 return C; 00326 } 00327 00328 00329 // If constraint is a distance, returns D. 00330 // Otherwise assert. 00331 const SCEV *DependenceAnalysis::Constraint::getD() const { 00332 assert(Kind == Distance && "Kind should be Distance"); 00333 return SE->getNegativeSCEV(C); 00334 } 00335 00336 00337 // Returns the loop associated with this constraint. 00338 const Loop *DependenceAnalysis::Constraint::getAssociatedLoop() const { 00339 assert((Kind == Distance || Kind == Line || Kind == Point) && 00340 "Kind should be Distance, Line, or Point"); 00341 return AssociatedLoop; 00342 } 00343 00344 00345 void DependenceAnalysis::Constraint::setPoint(const SCEV *X, 00346 const SCEV *Y, 00347 const Loop *CurLoop) { 00348 Kind = Point; 00349 A = X; 00350 B = Y; 00351 AssociatedLoop = CurLoop; 00352 } 00353 00354 00355 void DependenceAnalysis::Constraint::setLine(const SCEV *AA, 00356 const SCEV *BB, 00357 const SCEV *CC, 00358 const Loop *CurLoop) { 00359 Kind = Line; 00360 A = AA; 00361 B = BB; 00362 C = CC; 00363 AssociatedLoop = CurLoop; 00364 } 00365 00366 00367 void DependenceAnalysis::Constraint::setDistance(const SCEV *D, 00368 const Loop *CurLoop) { 00369 Kind = Distance; 00370 A = SE->getConstant(D->getType(), 1); 00371 B = SE->getNegativeSCEV(A); 00372 C = SE->getNegativeSCEV(D); 00373 AssociatedLoop = CurLoop; 00374 } 00375 00376 00377 void DependenceAnalysis::Constraint::setEmpty() { 00378 Kind = Empty; 00379 } 00380 00381 00382 void DependenceAnalysis::Constraint::setAny(ScalarEvolution *NewSE) { 00383 SE = NewSE; 00384 Kind = Any; 00385 } 00386 00387 00388 // For debugging purposes. Dumps the constraint out to OS. 00389 void DependenceAnalysis::Constraint::dump(raw_ostream &OS) const { 00390 if (isEmpty()) 00391 OS << " Empty\n"; 00392 else if (isAny()) 00393 OS << " Any\n"; 00394 else if (isPoint()) 00395 OS << " Point is <" << *getX() << ", " << *getY() << ">\n"; 00396 else if (isDistance()) 00397 OS << " Distance is " << *getD() << 00398 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n"; 00399 else if (isLine()) 00400 OS << " Line is " << *getA() << "*X + " << 00401 *getB() << "*Y = " << *getC() << "\n"; 00402 else 00403 llvm_unreachable("unknown constraint type in Constraint::dump"); 00404 } 00405 00406 00407 // Updates X with the intersection 00408 // of the Constraints X and Y. Returns true if X has changed. 00409 // Corresponds to Figure 4 from the paper 00410 // 00411 // Practical Dependence Testing 00412 // Goff, Kennedy, Tseng 00413 // PLDI 1991 00414 bool DependenceAnalysis::intersectConstraints(Constraint *X, 00415 const Constraint *Y) { 00416 ++DeltaApplications; 00417 DEBUG(dbgs() << "\tintersect constraints\n"); 00418 DEBUG(dbgs() << "\t X ="; X->dump(dbgs())); 00419 DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs())); 00420 assert(!Y->isPoint() && "Y must not be a Point"); 00421 if (X->isAny()) { 00422 if (Y->isAny()) 00423 return false; 00424 *X = *Y; 00425 return true; 00426 } 00427 if (X->isEmpty()) 00428 return false; 00429 if (Y->isEmpty()) { 00430 X->setEmpty(); 00431 return true; 00432 } 00433 00434 if (X->isDistance() && Y->isDistance()) { 00435 DEBUG(dbgs() << "\t intersect 2 distances\n"); 00436 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD())) 00437 return false; 00438 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) { 00439 X->setEmpty(); 00440 ++DeltaSuccesses; 00441 return true; 00442 } 00443 // Hmmm, interesting situation. 00444 // I guess if either is constant, keep it and ignore the other. 00445 if (isa<SCEVConstant>(Y->getD())) { 00446 *X = *Y; 00447 return true; 00448 } 00449 return false; 00450 } 00451 00452 // At this point, the pseudo-code in Figure 4 of the paper 00453 // checks if (X->isPoint() && Y->isPoint()). 00454 // This case can't occur in our implementation, 00455 // since a Point can only arise as the result of intersecting 00456 // two Line constraints, and the right-hand value, Y, is never 00457 // the result of an intersection. 00458 assert(!(X->isPoint() && Y->isPoint()) && 00459 "We shouldn't ever see X->isPoint() && Y->isPoint()"); 00460 00461 if (X->isLine() && Y->isLine()) { 00462 DEBUG(dbgs() << "\t intersect 2 lines\n"); 00463 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB()); 00464 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA()); 00465 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) { 00466 // slopes are equal, so lines are parallel 00467 DEBUG(dbgs() << "\t\tsame slope\n"); 00468 Prod1 = SE->getMulExpr(X->getC(), Y->getB()); 00469 Prod2 = SE->getMulExpr(X->getB(), Y->getC()); 00470 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) 00471 return false; 00472 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) { 00473 X->setEmpty(); 00474 ++DeltaSuccesses; 00475 return true; 00476 } 00477 return false; 00478 } 00479 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) { 00480 // slopes differ, so lines intersect 00481 DEBUG(dbgs() << "\t\tdifferent slopes\n"); 00482 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB()); 00483 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA()); 00484 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB()); 00485 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA()); 00486 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB()); 00487 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB()); 00488 const SCEVConstant *C1A2_C2A1 = 00489 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1)); 00490 const SCEVConstant *C1B2_C2B1 = 00491 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1)); 00492 const SCEVConstant *A1B2_A2B1 = 00493 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1)); 00494 const SCEVConstant *A2B1_A1B2 = 00495 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2)); 00496 if (!C1B2_C2B1 || !C1A2_C2A1 || 00497 !A1B2_A2B1 || !A2B1_A1B2) 00498 return false; 00499 APInt Xtop = C1B2_C2B1->getValue()->getValue(); 00500 APInt Xbot = A1B2_A2B1->getValue()->getValue(); 00501 APInt Ytop = C1A2_C2A1->getValue()->getValue(); 00502 APInt Ybot = A2B1_A1B2->getValue()->getValue(); 00503 DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n"); 00504 DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n"); 00505 DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n"); 00506 DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n"); 00507 APInt Xq = Xtop; // these need to be initialized, even 00508 APInt Xr = Xtop; // though they're just going to be overwritten 00509 APInt::sdivrem(Xtop, Xbot, Xq, Xr); 00510 APInt Yq = Ytop; 00511 APInt Yr = Ytop;; 00512 APInt::sdivrem(Ytop, Ybot, Yq, Yr); 00513 if (Xr != 0 || Yr != 0) { 00514 X->setEmpty(); 00515 ++DeltaSuccesses; 00516 return true; 00517 } 00518 DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n"); 00519 if (Xq.slt(0) || Yq.slt(0)) { 00520 X->setEmpty(); 00521 ++DeltaSuccesses; 00522 return true; 00523 } 00524 if (const SCEVConstant *CUB = 00525 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) { 00526 APInt UpperBound = CUB->getValue()->getValue(); 00527 DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n"); 00528 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) { 00529 X->setEmpty(); 00530 ++DeltaSuccesses; 00531 return true; 00532 } 00533 } 00534 X->setPoint(SE->getConstant(Xq), 00535 SE->getConstant(Yq), 00536 X->getAssociatedLoop()); 00537 ++DeltaSuccesses; 00538 return true; 00539 } 00540 return false; 00541 } 00542 00543 // if (X->isLine() && Y->isPoint()) This case can't occur. 00544 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur"); 00545 00546 if (X->isPoint() && Y->isLine()) { 00547 DEBUG(dbgs() << "\t intersect Point and Line\n"); 00548 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX()); 00549 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY()); 00550 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1); 00551 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC())) 00552 return false; 00553 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) { 00554 X->setEmpty(); 00555 ++DeltaSuccesses; 00556 return true; 00557 } 00558 return false; 00559 } 00560 00561 llvm_unreachable("shouldn't reach the end of Constraint intersection"); 00562 return false; 00563 } 00564 00565 00566 //===----------------------------------------------------------------------===// 00567 // DependenceAnalysis methods 00568 00569 // For debugging purposes. Dumps a dependence to OS. 00570 void Dependence::dump(raw_ostream &OS) const { 00571 bool Splitable = false; 00572 if (isConfused()) 00573 OS << "confused"; 00574 else { 00575 if (isConsistent()) 00576 OS << "consistent "; 00577 if (isFlow()) 00578 OS << "flow"; 00579 else if (isOutput()) 00580 OS << "output"; 00581 else if (isAnti()) 00582 OS << "anti"; 00583 else if (isInput()) 00584 OS << "input"; 00585 unsigned Levels = getLevels(); 00586 OS << " ["; 00587 for (unsigned II = 1; II <= Levels; ++II) { 00588 if (isSplitable(II)) 00589 Splitable = true; 00590 if (isPeelFirst(II)) 00591 OS << 'p'; 00592 const SCEV *Distance = getDistance(II); 00593 if (Distance) 00594 OS << *Distance; 00595 else if (isScalar(II)) 00596 OS << "S"; 00597 else { 00598 unsigned Direction = getDirection(II); 00599 if (Direction == DVEntry::ALL) 00600 OS << "*"; 00601 else { 00602 if (Direction & DVEntry::LT) 00603 OS << "<"; 00604 if (Direction & DVEntry::EQ) 00605 OS << "="; 00606 if (Direction & DVEntry::GT) 00607 OS << ">"; 00608 } 00609 } 00610 if (isPeelLast(II)) 00611 OS << 'p'; 00612 if (II < Levels) 00613 OS << " "; 00614 } 00615 if (isLoopIndependent()) 00616 OS << "|<"; 00617 OS << "]"; 00618 if (Splitable) 00619 OS << " splitable"; 00620 } 00621 OS << "!\n"; 00622 } 00623 00624 00625 00626 static 00627 AliasAnalysis::AliasResult underlyingObjectsAlias(AliasAnalysis *AA, 00628 const Value *A, 00629 const Value *B) { 00630 const Value *AObj = GetUnderlyingObject(A); 00631 const Value *BObj = GetUnderlyingObject(B); 00632 return AA->alias(AObj, AA->getTypeStoreSize(AObj->getType()), 00633 BObj, AA->getTypeStoreSize(BObj->getType())); 00634 } 00635 00636 00637 // Returns true if the load or store can be analyzed. Atomic and volatile 00638 // operations have properties which this analysis does not understand. 00639 static 00640 bool isLoadOrStore(const Instruction *I) { 00641 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) 00642 return LI->isUnordered(); 00643 else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) 00644 return SI->isUnordered(); 00645 return false; 00646 } 00647 00648 00649 static 00650 Value *getPointerOperand(Instruction *I) { 00651 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 00652 return LI->getPointerOperand(); 00653 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 00654 return SI->getPointerOperand(); 00655 llvm_unreachable("Value is not load or store instruction"); 00656 return 0; 00657 } 00658 00659 00660 // Examines the loop nesting of the Src and Dst 00661 // instructions and establishes their shared loops. Sets the variables 00662 // CommonLevels, SrcLevels, and MaxLevels. 00663 // The source and destination instructions needn't be contained in the same 00664 // loop. The routine establishNestingLevels finds the level of most deeply 00665 // nested loop that contains them both, CommonLevels. An instruction that's 00666 // not contained in a loop is at level = 0. MaxLevels is equal to the level 00667 // of the source plus the level of the destination, minus CommonLevels. 00668 // This lets us allocate vectors MaxLevels in length, with room for every 00669 // distinct loop referenced in both the source and destination subscripts. 00670 // The variable SrcLevels is the nesting depth of the source instruction. 00671 // It's used to help calculate distinct loops referenced by the destination. 00672 // Here's the map from loops to levels: 00673 // 0 - unused 00674 // 1 - outermost common loop 00675 // ... - other common loops 00676 // CommonLevels - innermost common loop 00677 // ... - loops containing Src but not Dst 00678 // SrcLevels - innermost loop containing Src but not Dst 00679 // ... - loops containing Dst but not Src 00680 // MaxLevels - innermost loops containing Dst but not Src 00681 // Consider the follow code fragment: 00682 // for (a = ...) { 00683 // for (b = ...) { 00684 // for (c = ...) { 00685 // for (d = ...) { 00686 // A[] = ...; 00687 // } 00688 // } 00689 // for (e = ...) { 00690 // for (f = ...) { 00691 // for (g = ...) { 00692 // ... = A[]; 00693 // } 00694 // } 00695 // } 00696 // } 00697 // } 00698 // If we're looking at the possibility of a dependence between the store 00699 // to A (the Src) and the load from A (the Dst), we'll note that they 00700 // have 2 loops in common, so CommonLevels will equal 2 and the direction 00701 // vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7. 00702 // A map from loop names to loop numbers would look like 00703 // a - 1 00704 // b - 2 = CommonLevels 00705 // c - 3 00706 // d - 4 = SrcLevels 00707 // e - 5 00708 // f - 6 00709 // g - 7 = MaxLevels 00710 void DependenceAnalysis::establishNestingLevels(const Instruction *Src, 00711 const Instruction *Dst) { 00712 const BasicBlock *SrcBlock = Src->getParent(); 00713 const BasicBlock *DstBlock = Dst->getParent(); 00714 unsigned SrcLevel = LI->getLoopDepth(SrcBlock); 00715 unsigned DstLevel = LI->getLoopDepth(DstBlock); 00716 const Loop *SrcLoop = LI->getLoopFor(SrcBlock); 00717 const Loop *DstLoop = LI->getLoopFor(DstBlock); 00718 SrcLevels = SrcLevel; 00719 MaxLevels = SrcLevel + DstLevel; 00720 while (SrcLevel > DstLevel) { 00721 SrcLoop = SrcLoop->getParentLoop(); 00722 SrcLevel--; 00723 } 00724 while (DstLevel > SrcLevel) { 00725 DstLoop = DstLoop->getParentLoop(); 00726 DstLevel--; 00727 } 00728 while (SrcLoop != DstLoop) { 00729 SrcLoop = SrcLoop->getParentLoop(); 00730 DstLoop = DstLoop->getParentLoop(); 00731 SrcLevel--; 00732 } 00733 CommonLevels = SrcLevel; 00734 MaxLevels -= CommonLevels; 00735 } 00736 00737 00738 // Given one of the loops containing the source, return 00739 // its level index in our numbering scheme. 00740 unsigned DependenceAnalysis::mapSrcLoop(const Loop *SrcLoop) const { 00741 return SrcLoop->getLoopDepth(); 00742 } 00743 00744 00745 // Given one of the loops containing the destination, 00746 // return its level index in our numbering scheme. 00747 unsigned DependenceAnalysis::mapDstLoop(const Loop *DstLoop) const { 00748 unsigned D = DstLoop->getLoopDepth(); 00749 if (D > CommonLevels) 00750 return D - CommonLevels + SrcLevels; 00751 else 00752 return D; 00753 } 00754 00755 00756 // Returns true if Expression is loop invariant in LoopNest. 00757 bool DependenceAnalysis::isLoopInvariant(const SCEV *Expression, 00758 const Loop *LoopNest) const { 00759 if (!LoopNest) 00760 return true; 00761 return SE->isLoopInvariant(Expression, LoopNest) && 00762 isLoopInvariant(Expression, LoopNest->getParentLoop()); 00763 } 00764 00765 00766 00767 // Finds the set of loops from the LoopNest that 00768 // have a level <= CommonLevels and are referred to by the SCEV Expression. 00769 void DependenceAnalysis::collectCommonLoops(const SCEV *Expression, 00770 const Loop *LoopNest, 00771 SmallBitVector &Loops) const { 00772 while (LoopNest) { 00773 unsigned Level = LoopNest->getLoopDepth(); 00774 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest)) 00775 Loops.set(Level); 00776 LoopNest = LoopNest->getParentLoop(); 00777 } 00778 } 00779 00780 00781 // removeMatchingExtensions - Examines a subscript pair. 00782 // If the source and destination are identically sign (or zero) 00783 // extended, it strips off the extension in an effect to simplify 00784 // the actual analysis. 00785 void DependenceAnalysis::removeMatchingExtensions(Subscript *Pair) { 00786 const SCEV *Src = Pair->Src; 00787 const SCEV *Dst = Pair->Dst; 00788 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) || 00789 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) { 00790 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src); 00791 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst); 00792 if (SrcCast->getType() == DstCast->getType()) { 00793 Pair->Src = SrcCast->getOperand(); 00794 Pair->Dst = DstCast->getOperand(); 00795 } 00796 } 00797 } 00798 00799 00800 // Examine the scev and return true iff it's linear. 00801 // Collect any loops mentioned in the set of "Loops". 00802 bool DependenceAnalysis::checkSrcSubscript(const SCEV *Src, 00803 const Loop *LoopNest, 00804 SmallBitVector &Loops) { 00805 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src); 00806 if (!AddRec) 00807 return isLoopInvariant(Src, LoopNest); 00808 const SCEV *Start = AddRec->getStart(); 00809 const SCEV *Step = AddRec->getStepRecurrence(*SE); 00810 if (!isLoopInvariant(Step, LoopNest)) 00811 return false; 00812 Loops.set(mapSrcLoop(AddRec->getLoop())); 00813 return checkSrcSubscript(Start, LoopNest, Loops); 00814 } 00815 00816 00817 00818 // Examine the scev and return true iff it's linear. 00819 // Collect any loops mentioned in the set of "Loops". 00820 bool DependenceAnalysis::checkDstSubscript(const SCEV *Dst, 00821 const Loop *LoopNest, 00822 SmallBitVector &Loops) { 00823 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst); 00824 if (!AddRec) 00825 return isLoopInvariant(Dst, LoopNest); 00826 const SCEV *Start = AddRec->getStart(); 00827 const SCEV *Step = AddRec->getStepRecurrence(*SE); 00828 if (!isLoopInvariant(Step, LoopNest)) 00829 return false; 00830 Loops.set(mapDstLoop(AddRec->getLoop())); 00831 return checkDstSubscript(Start, LoopNest, Loops); 00832 } 00833 00834 00835 // Examines the subscript pair (the Src and Dst SCEVs) 00836 // and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear. 00837 // Collects the associated loops in a set. 00838 DependenceAnalysis::Subscript::ClassificationKind 00839 DependenceAnalysis::classifyPair(const SCEV *Src, const Loop *SrcLoopNest, 00840 const SCEV *Dst, const Loop *DstLoopNest, 00841 SmallBitVector &Loops) { 00842 SmallBitVector SrcLoops(MaxLevels + 1); 00843 SmallBitVector DstLoops(MaxLevels + 1); 00844 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops)) 00845 return Subscript::NonLinear; 00846 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops)) 00847 return Subscript::NonLinear; 00848 Loops = SrcLoops; 00849 Loops |= DstLoops; 00850 unsigned N = Loops.count(); 00851 if (N == 0) 00852 return Subscript::ZIV; 00853 if (N == 1) 00854 return Subscript::SIV; 00855 if (N == 2 && (SrcLoops.count() == 0 || 00856 DstLoops.count() == 0 || 00857 (SrcLoops.count() == 1 && DstLoops.count() == 1))) 00858 return Subscript::RDIV; 00859 return Subscript::MIV; 00860 } 00861 00862 00863 // A wrapper around SCEV::isKnownPredicate. 00864 // Looks for cases where we're interested in comparing for equality. 00865 // If both X and Y have been identically sign or zero extended, 00866 // it strips off the (confusing) extensions before invoking 00867 // SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package 00868 // will be similarly updated. 00869 // 00870 // If SCEV::isKnownPredicate can't prove the predicate, 00871 // we try simple subtraction, which seems to help in some cases 00872 // involving symbolics. 00873 bool DependenceAnalysis::isKnownPredicate(ICmpInst::Predicate Pred, 00874 const SCEV *X, 00875 const SCEV *Y) const { 00876 if (Pred == CmpInst::ICMP_EQ || 00877 Pred == CmpInst::ICMP_NE) { 00878 if ((isa<SCEVSignExtendExpr>(X) && 00879 isa<SCEVSignExtendExpr>(Y)) || 00880 (isa<SCEVZeroExtendExpr>(X) && 00881 isa<SCEVZeroExtendExpr>(Y))) { 00882 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X); 00883 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y); 00884 const SCEV *Xop = CX->getOperand(); 00885 const SCEV *Yop = CY->getOperand(); 00886 if (Xop->getType() == Yop->getType()) { 00887 X = Xop; 00888 Y = Yop; 00889 } 00890 } 00891 } 00892 if (SE->isKnownPredicate(Pred, X, Y)) 00893 return true; 00894 // If SE->isKnownPredicate can't prove the condition, 00895 // we try the brute-force approach of subtracting 00896 // and testing the difference. 00897 // By testing with SE->isKnownPredicate first, we avoid 00898 // the possibility of overflow when the arguments are constants. 00899 const SCEV *Delta = SE->getMinusSCEV(X, Y); 00900 switch (Pred) { 00901 case CmpInst::ICMP_EQ: 00902 return Delta->isZero(); 00903 case CmpInst::ICMP_NE: 00904 return SE->isKnownNonZero(Delta); 00905 case CmpInst::ICMP_SGE: 00906 return SE->isKnownNonNegative(Delta); 00907 case CmpInst::ICMP_SLE: 00908 return SE->isKnownNonPositive(Delta); 00909 case CmpInst::ICMP_SGT: 00910 return SE->isKnownPositive(Delta); 00911 case CmpInst::ICMP_SLT: 00912 return SE->isKnownNegative(Delta); 00913 default: 00914 llvm_unreachable("unexpected predicate in isKnownPredicate"); 00915 } 00916 } 00917 00918 00919 // All subscripts are all the same type. 00920 // Loop bound may be smaller (e.g., a char). 00921 // Should zero extend loop bound, since it's always >= 0. 00922 // This routine collects upper bound and extends if needed. 00923 // Return null if no bound available. 00924 const SCEV *DependenceAnalysis::collectUpperBound(const Loop *L, 00925 Type *T) const { 00926 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 00927 const SCEV *UB = SE->getBackedgeTakenCount(L); 00928 return SE->getNoopOrZeroExtend(UB, T); 00929 } 00930 return NULL; 00931 } 00932 00933 00934 // Calls collectUpperBound(), then attempts to cast it to SCEVConstant. 00935 // If the cast fails, returns NULL. 00936 const SCEVConstant *DependenceAnalysis::collectConstantUpperBound(const Loop *L, 00937 Type *T 00938 ) const { 00939 if (const SCEV *UB = collectUpperBound(L, T)) 00940 return dyn_cast<SCEVConstant>(UB); 00941 return NULL; 00942 } 00943 00944 00945 // testZIV - 00946 // When we have a pair of subscripts of the form [c1] and [c2], 00947 // where c1 and c2 are both loop invariant, we attack it using 00948 // the ZIV test. Basically, we test by comparing the two values, 00949 // but there are actually three possible results: 00950 // 1) the values are equal, so there's a dependence 00951 // 2) the values are different, so there's no dependence 00952 // 3) the values might be equal, so we have to assume a dependence. 00953 // 00954 // Return true if dependence disproved. 00955 bool DependenceAnalysis::testZIV(const SCEV *Src, 00956 const SCEV *Dst, 00957 FullDependence &Result) const { 00958 DEBUG(dbgs() << " src = " << *Src << "\n"); 00959 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 00960 ++ZIVapplications; 00961 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) { 00962 DEBUG(dbgs() << " provably dependent\n"); 00963 return false; // provably dependent 00964 } 00965 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) { 00966 DEBUG(dbgs() << " provably independent\n"); 00967 ++ZIVindependence; 00968 return true; // provably independent 00969 } 00970 DEBUG(dbgs() << " possibly dependent\n"); 00971 Result.Consistent = false; 00972 return false; // possibly dependent 00973 } 00974 00975 00976 // strongSIVtest - 00977 // From the paper, Practical Dependence Testing, Section 4.2.1 00978 // 00979 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i], 00980 // where i is an induction variable, c1 and c2 are loop invariant, 00981 // and a is a constant, we can solve it exactly using the Strong SIV test. 00982 // 00983 // Can prove independence. Failing that, can compute distance (and direction). 00984 // In the presence of symbolic terms, we can sometimes make progress. 00985 // 00986 // If there's a dependence, 00987 // 00988 // c1 + a*i = c2 + a*i' 00989 // 00990 // The dependence distance is 00991 // 00992 // d = i' - i = (c1 - c2)/a 00993 // 00994 // A dependence only exists if d is an integer and abs(d) <= U, where U is the 00995 // loop's upper bound. If a dependence exists, the dependence direction is 00996 // defined as 00997 // 00998 // { < if d > 0 00999 // direction = { = if d = 0 01000 // { > if d < 0 01001 // 01002 // Return true if dependence disproved. 01003 bool DependenceAnalysis::strongSIVtest(const SCEV *Coeff, 01004 const SCEV *SrcConst, 01005 const SCEV *DstConst, 01006 const Loop *CurLoop, 01007 unsigned Level, 01008 FullDependence &Result, 01009 Constraint &NewConstraint) const { 01010 DEBUG(dbgs() << "\tStrong SIV test\n"); 01011 DEBUG(dbgs() << "\t Coeff = " << *Coeff); 01012 DEBUG(dbgs() << ", " << *Coeff->getType() << "\n"); 01013 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst); 01014 DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n"); 01015 DEBUG(dbgs() << "\t DstConst = " << *DstConst); 01016 DEBUG(dbgs() << ", " << *DstConst->getType() << "\n"); 01017 ++StrongSIVapplications; 01018 assert(0 < Level && Level <= CommonLevels && "level out of range"); 01019 Level--; 01020 01021 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst); 01022 DEBUG(dbgs() << "\t Delta = " << *Delta); 01023 DEBUG(dbgs() << ", " << *Delta->getType() << "\n"); 01024 01025 // check that |Delta| < iteration count 01026 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 01027 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound); 01028 DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n"); 01029 const SCEV *AbsDelta = 01030 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta); 01031 const SCEV *AbsCoeff = 01032 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff); 01033 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff); 01034 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) { 01035 // Distance greater than trip count - no dependence 01036 ++StrongSIVindependence; 01037 ++StrongSIVsuccesses; 01038 return true; 01039 } 01040 } 01041 01042 // Can we compute distance? 01043 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) { 01044 APInt ConstDelta = cast<SCEVConstant>(Delta)->getValue()->getValue(); 01045 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getValue()->getValue(); 01046 APInt Distance = ConstDelta; // these need to be initialized 01047 APInt Remainder = ConstDelta; 01048 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder); 01049 DEBUG(dbgs() << "\t Distance = " << Distance << "\n"); 01050 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 01051 // Make sure Coeff divides Delta exactly 01052 if (Remainder != 0) { 01053 // Coeff doesn't divide Distance, no dependence 01054 ++StrongSIVindependence; 01055 ++StrongSIVsuccesses; 01056 return true; 01057 } 01058 Result.DV[Level].Distance = SE->getConstant(Distance); 01059 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop); 01060 if (Distance.sgt(0)) 01061 Result.DV[Level].Direction &= Dependence::DVEntry::LT; 01062 else if (Distance.slt(0)) 01063 Result.DV[Level].Direction &= Dependence::DVEntry::GT; 01064 else 01065 Result.DV[Level].Direction &= Dependence::DVEntry::EQ; 01066 ++StrongSIVsuccesses; 01067 } 01068 else if (Delta->isZero()) { 01069 // since 0/X == 0 01070 Result.DV[Level].Distance = Delta; 01071 NewConstraint.setDistance(Delta, CurLoop); 01072 Result.DV[Level].Direction &= Dependence::DVEntry::EQ; 01073 ++StrongSIVsuccesses; 01074 } 01075 else { 01076 if (Coeff->isOne()) { 01077 DEBUG(dbgs() << "\t Distance = " << *Delta << "\n"); 01078 Result.DV[Level].Distance = Delta; // since X/1 == X 01079 NewConstraint.setDistance(Delta, CurLoop); 01080 } 01081 else { 01082 Result.Consistent = false; 01083 NewConstraint.setLine(Coeff, 01084 SE->getNegativeSCEV(Coeff), 01085 SE->getNegativeSCEV(Delta), CurLoop); 01086 } 01087 01088 // maybe we can get a useful direction 01089 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta); 01090 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta); 01091 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta); 01092 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff); 01093 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff); 01094 // The double negatives above are confusing. 01095 // It helps to read !SE->isKnownNonZero(Delta) 01096 // as "Delta might be Zero" 01097 unsigned NewDirection = Dependence::DVEntry::NONE; 01098 if ((DeltaMaybePositive && CoeffMaybePositive) || 01099 (DeltaMaybeNegative && CoeffMaybeNegative)) 01100 NewDirection = Dependence::DVEntry::LT; 01101 if (DeltaMaybeZero) 01102 NewDirection |= Dependence::DVEntry::EQ; 01103 if ((DeltaMaybeNegative && CoeffMaybePositive) || 01104 (DeltaMaybePositive && CoeffMaybeNegative)) 01105 NewDirection |= Dependence::DVEntry::GT; 01106 if (NewDirection < Result.DV[Level].Direction) 01107 ++StrongSIVsuccesses; 01108 Result.DV[Level].Direction &= NewDirection; 01109 } 01110 return false; 01111 } 01112 01113 01114 // weakCrossingSIVtest - 01115 // From the paper, Practical Dependence Testing, Section 4.2.2 01116 // 01117 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i], 01118 // where i is an induction variable, c1 and c2 are loop invariant, 01119 // and a is a constant, we can solve it exactly using the 01120 // Weak-Crossing SIV test. 01121 // 01122 // Given c1 + a*i = c2 - a*i', we can look for the intersection of 01123 // the two lines, where i = i', yielding 01124 // 01125 // c1 + a*i = c2 - a*i 01126 // 2a*i = c2 - c1 01127 // i = (c2 - c1)/2a 01128 // 01129 // If i < 0, there is no dependence. 01130 // If i > upperbound, there is no dependence. 01131 // If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0. 01132 // If i = upperbound, there's a dependence with distance = 0. 01133 // If i is integral, there's a dependence (all directions). 01134 // If the non-integer part = 1/2, there's a dependence (<> directions). 01135 // Otherwise, there's no dependence. 01136 // 01137 // Can prove independence. Failing that, 01138 // can sometimes refine the directions. 01139 // Can determine iteration for splitting. 01140 // 01141 // Return true if dependence disproved. 01142 bool DependenceAnalysis::weakCrossingSIVtest(const SCEV *Coeff, 01143 const SCEV *SrcConst, 01144 const SCEV *DstConst, 01145 const Loop *CurLoop, 01146 unsigned Level, 01147 FullDependence &Result, 01148 Constraint &NewConstraint, 01149 const SCEV *&SplitIter) const { 01150 DEBUG(dbgs() << "\tWeak-Crossing SIV test\n"); 01151 DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n"); 01152 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 01153 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 01154 ++WeakCrossingSIVapplications; 01155 assert(0 < Level && Level <= CommonLevels && "Level out of range"); 01156 Level--; 01157 Result.Consistent = false; 01158 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 01159 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01160 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop); 01161 if (Delta->isZero()) { 01162 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT); 01163 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT); 01164 ++WeakCrossingSIVsuccesses; 01165 if (!Result.DV[Level].Direction) { 01166 ++WeakCrossingSIVindependence; 01167 return true; 01168 } 01169 Result.DV[Level].Distance = Delta; // = 0 01170 return false; 01171 } 01172 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff); 01173 if (!ConstCoeff) 01174 return false; 01175 01176 Result.DV[Level].Splitable = true; 01177 if (SE->isKnownNegative(ConstCoeff)) { 01178 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff)); 01179 assert(ConstCoeff && 01180 "dynamic cast of negative of ConstCoeff should yield constant"); 01181 Delta = SE->getNegativeSCEV(Delta); 01182 } 01183 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive"); 01184 01185 // compute SplitIter for use by DependenceAnalysis::getSplitIteration() 01186 SplitIter = 01187 SE->getUDivExpr(SE->getSMaxExpr(SE->getConstant(Delta->getType(), 0), 01188 Delta), 01189 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), 01190 ConstCoeff)); 01191 DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n"); 01192 01193 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 01194 if (!ConstDelta) 01195 return false; 01196 01197 // We're certain that ConstCoeff > 0; therefore, 01198 // if Delta < 0, then no dependence. 01199 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01200 DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n"); 01201 if (SE->isKnownNegative(Delta)) { 01202 // No dependence, Delta < 0 01203 ++WeakCrossingSIVindependence; 01204 ++WeakCrossingSIVsuccesses; 01205 return true; 01206 } 01207 01208 // We're certain that Delta > 0 and ConstCoeff > 0. 01209 // Check Delta/(2*ConstCoeff) against upper loop bound 01210 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 01211 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 01212 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2); 01213 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), 01214 ConstantTwo); 01215 DEBUG(dbgs() << "\t ML = " << *ML << "\n"); 01216 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) { 01217 // Delta too big, no dependence 01218 ++WeakCrossingSIVindependence; 01219 ++WeakCrossingSIVsuccesses; 01220 return true; 01221 } 01222 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) { 01223 // i = i' = UB 01224 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT); 01225 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT); 01226 ++WeakCrossingSIVsuccesses; 01227 if (!Result.DV[Level].Direction) { 01228 ++WeakCrossingSIVindependence; 01229 return true; 01230 } 01231 Result.DV[Level].Splitable = false; 01232 Result.DV[Level].Distance = SE->getConstant(Delta->getType(), 0); 01233 return false; 01234 } 01235 } 01236 01237 // check that Coeff divides Delta 01238 APInt APDelta = ConstDelta->getValue()->getValue(); 01239 APInt APCoeff = ConstCoeff->getValue()->getValue(); 01240 APInt Distance = APDelta; // these need to be initialzed 01241 APInt Remainder = APDelta; 01242 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder); 01243 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 01244 if (Remainder != 0) { 01245 // Coeff doesn't divide Delta, no dependence 01246 ++WeakCrossingSIVindependence; 01247 ++WeakCrossingSIVsuccesses; 01248 return true; 01249 } 01250 DEBUG(dbgs() << "\t Distance = " << Distance << "\n"); 01251 01252 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible 01253 APInt Two = APInt(Distance.getBitWidth(), 2, true); 01254 Remainder = Distance.srem(Two); 01255 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 01256 if (Remainder != 0) { 01257 // Equal direction isn't possible 01258 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ); 01259 ++WeakCrossingSIVsuccesses; 01260 } 01261 return false; 01262 } 01263 01264 01265 // Kirch's algorithm, from 01266 // 01267 // Optimizing Supercompilers for Supercomputers 01268 // Michael Wolfe 01269 // MIT Press, 1989 01270 // 01271 // Program 2.1, page 29. 01272 // Computes the GCD of AM and BM. 01273 // Also finds a solution to the equation ax - by = gdc(a, b). 01274 // Returns true iff the gcd divides Delta. 01275 static 01276 bool findGCD(unsigned Bits, APInt AM, APInt BM, APInt Delta, 01277 APInt &G, APInt &X, APInt &Y) { 01278 APInt A0(Bits, 1, true), A1(Bits, 0, true); 01279 APInt B0(Bits, 0, true), B1(Bits, 1, true); 01280 APInt G0 = AM.abs(); 01281 APInt G1 = BM.abs(); 01282 APInt Q = G0; // these need to be initialized 01283 APInt R = G0; 01284 APInt::sdivrem(G0, G1, Q, R); 01285 while (R != 0) { 01286 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2; 01287 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2; 01288 G0 = G1; G1 = R; 01289 APInt::sdivrem(G0, G1, Q, R); 01290 } 01291 G = G1; 01292 DEBUG(dbgs() << "\t GCD = " << G << "\n"); 01293 X = AM.slt(0) ? -A1 : A1; 01294 Y = BM.slt(0) ? B1 : -B1; 01295 01296 // make sure gcd divides Delta 01297 R = Delta.srem(G); 01298 if (R != 0) 01299 return true; // gcd doesn't divide Delta, no dependence 01300 Q = Delta.sdiv(G); 01301 X *= Q; 01302 Y *= Q; 01303 return false; 01304 } 01305 01306 01307 static 01308 APInt floorOfQuotient(APInt A, APInt B) { 01309 APInt Q = A; // these need to be initialized 01310 APInt R = A; 01311 APInt::sdivrem(A, B, Q, R); 01312 if (R == 0) 01313 return Q; 01314 if ((A.sgt(0) && B.sgt(0)) || 01315 (A.slt(0) && B.slt(0))) 01316 return Q; 01317 else 01318 return Q - 1; 01319 } 01320 01321 01322 static 01323 APInt ceilingOfQuotient(APInt A, APInt B) { 01324 APInt Q = A; // these need to be initialized 01325 APInt R = A; 01326 APInt::sdivrem(A, B, Q, R); 01327 if (R == 0) 01328 return Q; 01329 if ((A.sgt(0) && B.sgt(0)) || 01330 (A.slt(0) && B.slt(0))) 01331 return Q + 1; 01332 else 01333 return Q; 01334 } 01335 01336 01337 static 01338 APInt maxAPInt(APInt A, APInt B) { 01339 return A.sgt(B) ? A : B; 01340 } 01341 01342 01343 static 01344 APInt minAPInt(APInt A, APInt B) { 01345 return A.slt(B) ? A : B; 01346 } 01347 01348 01349 // exactSIVtest - 01350 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i], 01351 // where i is an induction variable, c1 and c2 are loop invariant, and a1 01352 // and a2 are constant, we can solve it exactly using an algorithm developed 01353 // by Banerjee and Wolfe. See Section 2.5.3 in 01354 // 01355 // Optimizing Supercompilers for Supercomputers 01356 // Michael Wolfe 01357 // MIT Press, 1989 01358 // 01359 // It's slower than the specialized tests (strong SIV, weak-zero SIV, etc), 01360 // so use them if possible. They're also a bit better with symbolics and, 01361 // in the case of the strong SIV test, can compute Distances. 01362 // 01363 // Return true if dependence disproved. 01364 bool DependenceAnalysis::exactSIVtest(const SCEV *SrcCoeff, 01365 const SCEV *DstCoeff, 01366 const SCEV *SrcConst, 01367 const SCEV *DstConst, 01368 const Loop *CurLoop, 01369 unsigned Level, 01370 FullDependence &Result, 01371 Constraint &NewConstraint) const { 01372 DEBUG(dbgs() << "\tExact SIV test\n"); 01373 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n"); 01374 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n"); 01375 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 01376 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 01377 ++ExactSIVapplications; 01378 assert(0 < Level && Level <= CommonLevels && "Level out of range"); 01379 Level--; 01380 Result.Consistent = false; 01381 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 01382 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01383 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), 01384 Delta, CurLoop); 01385 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 01386 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 01387 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 01388 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff) 01389 return false; 01390 01391 // find gcd 01392 APInt G, X, Y; 01393 APInt AM = ConstSrcCoeff->getValue()->getValue(); 01394 APInt BM = ConstDstCoeff->getValue()->getValue(); 01395 unsigned Bits = AM.getBitWidth(); 01396 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) { 01397 // gcd doesn't divide Delta, no dependence 01398 ++ExactSIVindependence; 01399 ++ExactSIVsuccesses; 01400 return true; 01401 } 01402 01403 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); 01404 01405 // since SCEV construction normalizes, LM = 0 01406 APInt UM(Bits, 1, true); 01407 bool UMvalid = false; 01408 // UM is perhaps unavailable, let's check 01409 if (const SCEVConstant *CUB = 01410 collectConstantUpperBound(CurLoop, Delta->getType())) { 01411 UM = CUB->getValue()->getValue(); 01412 DEBUG(dbgs() << "\t UM = " << UM << "\n"); 01413 UMvalid = true; 01414 } 01415 01416 APInt TU(APInt::getSignedMaxValue(Bits)); 01417 APInt TL(APInt::getSignedMinValue(Bits)); 01418 01419 // test(BM/G, LM-X) and test(-BM/G, X-UM) 01420 APInt TMUL = BM.sdiv(G); 01421 if (TMUL.sgt(0)) { 01422 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL)); 01423 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01424 if (UMvalid) { 01425 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL)); 01426 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01427 } 01428 } 01429 else { 01430 TU = minAPInt(TU, floorOfQuotient(-X, TMUL)); 01431 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01432 if (UMvalid) { 01433 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL)); 01434 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01435 } 01436 } 01437 01438 // test(AM/G, LM-Y) and test(-AM/G, Y-UM) 01439 TMUL = AM.sdiv(G); 01440 if (TMUL.sgt(0)) { 01441 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL)); 01442 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01443 if (UMvalid) { 01444 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL)); 01445 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01446 } 01447 } 01448 else { 01449 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL)); 01450 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01451 if (UMvalid) { 01452 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL)); 01453 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01454 } 01455 } 01456 if (TL.sgt(TU)) { 01457 ++ExactSIVindependence; 01458 ++ExactSIVsuccesses; 01459 return true; 01460 } 01461 01462 // explore directions 01463 unsigned NewDirection = Dependence::DVEntry::NONE; 01464 01465 // less than 01466 APInt SaveTU(TU); // save these 01467 APInt SaveTL(TL); 01468 DEBUG(dbgs() << "\t exploring LT direction\n"); 01469 TMUL = AM - BM; 01470 if (TMUL.sgt(0)) { 01471 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL)); 01472 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 01473 } 01474 else { 01475 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL)); 01476 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 01477 } 01478 if (TL.sle(TU)) { 01479 NewDirection |= Dependence::DVEntry::LT; 01480 ++ExactSIVsuccesses; 01481 } 01482 01483 // equal 01484 TU = SaveTU; // restore 01485 TL = SaveTL; 01486 DEBUG(dbgs() << "\t exploring EQ direction\n"); 01487 if (TMUL.sgt(0)) { 01488 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL)); 01489 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 01490 } 01491 else { 01492 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL)); 01493 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 01494 } 01495 TMUL = BM - AM; 01496 if (TMUL.sgt(0)) { 01497 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL)); 01498 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 01499 } 01500 else { 01501 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL)); 01502 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 01503 } 01504 if (TL.sle(TU)) { 01505 NewDirection |= Dependence::DVEntry::EQ; 01506 ++ExactSIVsuccesses; 01507 } 01508 01509 // greater than 01510 TU = SaveTU; // restore 01511 TL = SaveTL; 01512 DEBUG(dbgs() << "\t exploring GT direction\n"); 01513 if (TMUL.sgt(0)) { 01514 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL)); 01515 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 01516 } 01517 else { 01518 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL)); 01519 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 01520 } 01521 if (TL.sle(TU)) { 01522 NewDirection |= Dependence::DVEntry::GT; 01523 ++ExactSIVsuccesses; 01524 } 01525 01526 // finished 01527 Result.DV[Level].Direction &= NewDirection; 01528 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE) 01529 ++ExactSIVindependence; 01530 return Result.DV[Level].Direction == Dependence::DVEntry::NONE; 01531 } 01532 01533 01534 01535 // Return true if the divisor evenly divides the dividend. 01536 static 01537 bool isRemainderZero(const SCEVConstant *Dividend, 01538 const SCEVConstant *Divisor) { 01539 APInt ConstDividend = Dividend->getValue()->getValue(); 01540 APInt ConstDivisor = Divisor->getValue()->getValue(); 01541 return ConstDividend.srem(ConstDivisor) == 0; 01542 } 01543 01544 01545 // weakZeroSrcSIVtest - 01546 // From the paper, Practical Dependence Testing, Section 4.2.2 01547 // 01548 // When we have a pair of subscripts of the form [c1] and [c2 + a*i], 01549 // where i is an induction variable, c1 and c2 are loop invariant, 01550 // and a is a constant, we can solve it exactly using the 01551 // Weak-Zero SIV test. 01552 // 01553 // Given 01554 // 01555 // c1 = c2 + a*i 01556 // 01557 // we get 01558 // 01559 // (c1 - c2)/a = i 01560 // 01561 // If i is not an integer, there's no dependence. 01562 // If i < 0 or > UB, there's no dependence. 01563 // If i = 0, the direction is <= and peeling the 01564 // 1st iteration will break the dependence. 01565 // If i = UB, the direction is >= and peeling the 01566 // last iteration will break the dependence. 01567 // Otherwise, the direction is *. 01568 // 01569 // Can prove independence. Failing that, we can sometimes refine 01570 // the directions. Can sometimes show that first or last 01571 // iteration carries all the dependences (so worth peeling). 01572 // 01573 // (see also weakZeroDstSIVtest) 01574 // 01575 // Return true if dependence disproved. 01576 bool DependenceAnalysis::weakZeroSrcSIVtest(const SCEV *DstCoeff, 01577 const SCEV *SrcConst, 01578 const SCEV *DstConst, 01579 const Loop *CurLoop, 01580 unsigned Level, 01581 FullDependence &Result, 01582 Constraint &NewConstraint) const { 01583 // For the WeakSIV test, it's possible the loop isn't common to 01584 // the Src and Dst loops. If it isn't, then there's no need to 01585 // record a direction. 01586 DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n"); 01587 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n"); 01588 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 01589 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 01590 ++WeakZeroSIVapplications; 01591 assert(0 < Level && Level <= MaxLevels && "Level out of range"); 01592 Level--; 01593 Result.Consistent = false; 01594 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst); 01595 NewConstraint.setLine(SE->getConstant(Delta->getType(), 0), 01596 DstCoeff, Delta, CurLoop); 01597 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01598 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) { 01599 if (Level < CommonLevels) { 01600 Result.DV[Level].Direction &= Dependence::DVEntry::LE; 01601 Result.DV[Level].PeelFirst = true; 01602 ++WeakZeroSIVsuccesses; 01603 } 01604 return false; // dependences caused by first iteration 01605 } 01606 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 01607 if (!ConstCoeff) 01608 return false; 01609 const SCEV *AbsCoeff = 01610 SE->isKnownNegative(ConstCoeff) ? 01611 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff; 01612 const SCEV *NewDelta = 01613 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta; 01614 01615 // check that Delta/SrcCoeff < iteration count 01616 // really check NewDelta < count*AbsCoeff 01617 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 01618 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 01619 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound); 01620 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) { 01621 ++WeakZeroSIVindependence; 01622 ++WeakZeroSIVsuccesses; 01623 return true; 01624 } 01625 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) { 01626 // dependences caused by last iteration 01627 if (Level < CommonLevels) { 01628 Result.DV[Level].Direction &= Dependence::DVEntry::GE; 01629 Result.DV[Level].PeelLast = true; 01630 ++WeakZeroSIVsuccesses; 01631 } 01632 return false; 01633 } 01634 } 01635 01636 // check that Delta/SrcCoeff >= 0 01637 // really check that NewDelta >= 0 01638 if (SE->isKnownNegative(NewDelta)) { 01639 // No dependence, newDelta < 0 01640 ++WeakZeroSIVindependence; 01641 ++WeakZeroSIVsuccesses; 01642 return true; 01643 } 01644 01645 // if SrcCoeff doesn't divide Delta, then no dependence 01646 if (isa<SCEVConstant>(Delta) && 01647 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) { 01648 ++WeakZeroSIVindependence; 01649 ++WeakZeroSIVsuccesses; 01650 return true; 01651 } 01652 return false; 01653 } 01654 01655 01656 // weakZeroDstSIVtest - 01657 // From the paper, Practical Dependence Testing, Section 4.2.2 01658 // 01659 // When we have a pair of subscripts of the form [c1 + a*i] and [c2], 01660 // where i is an induction variable, c1 and c2 are loop invariant, 01661 // and a is a constant, we can solve it exactly using the 01662 // Weak-Zero SIV test. 01663 // 01664 // Given 01665 // 01666 // c1 + a*i = c2 01667 // 01668 // we get 01669 // 01670 // i = (c2 - c1)/a 01671 // 01672 // If i is not an integer, there's no dependence. 01673 // If i < 0 or > UB, there's no dependence. 01674 // If i = 0, the direction is <= and peeling the 01675 // 1st iteration will break the dependence. 01676 // If i = UB, the direction is >= and peeling the 01677 // last iteration will break the dependence. 01678 // Otherwise, the direction is *. 01679 // 01680 // Can prove independence. Failing that, we can sometimes refine 01681 // the directions. Can sometimes show that first or last 01682 // iteration carries all the dependences (so worth peeling). 01683 // 01684 // (see also weakZeroSrcSIVtest) 01685 // 01686 // Return true if dependence disproved. 01687 bool DependenceAnalysis::weakZeroDstSIVtest(const SCEV *SrcCoeff, 01688 const SCEV *SrcConst, 01689 const SCEV *DstConst, 01690 const Loop *CurLoop, 01691 unsigned Level, 01692 FullDependence &Result, 01693 Constraint &NewConstraint) const { 01694 // For the WeakSIV test, it's possible the loop isn't common to the 01695 // Src and Dst loops. If it isn't, then there's no need to record a direction. 01696 DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n"); 01697 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n"); 01698 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 01699 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 01700 ++WeakZeroSIVapplications; 01701 assert(0 < Level && Level <= SrcLevels && "Level out of range"); 01702 Level--; 01703 Result.Consistent = false; 01704 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 01705 NewConstraint.setLine(SrcCoeff, SE->getConstant(Delta->getType(), 0), 01706 Delta, CurLoop); 01707 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01708 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) { 01709 if (Level < CommonLevels) { 01710 Result.DV[Level].Direction &= Dependence::DVEntry::LE; 01711 Result.DV[Level].PeelFirst = true; 01712 ++WeakZeroSIVsuccesses; 01713 } 01714 return false; // dependences caused by first iteration 01715 } 01716 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 01717 if (!ConstCoeff) 01718 return false; 01719 const SCEV *AbsCoeff = 01720 SE->isKnownNegative(ConstCoeff) ? 01721 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff; 01722 const SCEV *NewDelta = 01723 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta; 01724 01725 // check that Delta/SrcCoeff < iteration count 01726 // really check NewDelta < count*AbsCoeff 01727 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 01728 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 01729 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound); 01730 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) { 01731 ++WeakZeroSIVindependence; 01732 ++WeakZeroSIVsuccesses; 01733 return true; 01734 } 01735 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) { 01736 // dependences caused by last iteration 01737 if (Level < CommonLevels) { 01738 Result.DV[Level].Direction &= Dependence::DVEntry::GE; 01739 Result.DV[Level].PeelLast = true; 01740 ++WeakZeroSIVsuccesses; 01741 } 01742 return false; 01743 } 01744 } 01745 01746 // check that Delta/SrcCoeff >= 0 01747 // really check that NewDelta >= 0 01748 if (SE->isKnownNegative(NewDelta)) { 01749 // No dependence, newDelta < 0 01750 ++WeakZeroSIVindependence; 01751 ++WeakZeroSIVsuccesses; 01752 return true; 01753 } 01754 01755 // if SrcCoeff doesn't divide Delta, then no dependence 01756 if (isa<SCEVConstant>(Delta) && 01757 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) { 01758 ++WeakZeroSIVindependence; 01759 ++WeakZeroSIVsuccesses; 01760 return true; 01761 } 01762 return false; 01763 } 01764 01765 01766 // exactRDIVtest - Tests the RDIV subscript pair for dependence. 01767 // Things of the form [c1 + a*i] and [c2 + b*j], 01768 // where i and j are induction variable, c1 and c2 are loop invariant, 01769 // and a and b are constants. 01770 // Returns true if any possible dependence is disproved. 01771 // Marks the result as inconsistent. 01772 // Works in some cases that symbolicRDIVtest doesn't, and vice versa. 01773 bool DependenceAnalysis::exactRDIVtest(const SCEV *SrcCoeff, 01774 const SCEV *DstCoeff, 01775 const SCEV *SrcConst, 01776 const SCEV *DstConst, 01777 const Loop *SrcLoop, 01778 const Loop *DstLoop, 01779 FullDependence &Result) const { 01780 DEBUG(dbgs() << "\tExact RDIV test\n"); 01781 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n"); 01782 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n"); 01783 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 01784 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 01785 ++ExactRDIVapplications; 01786 Result.Consistent = false; 01787 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 01788 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 01789 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 01790 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 01791 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 01792 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff) 01793 return false; 01794 01795 // find gcd 01796 APInt G, X, Y; 01797 APInt AM = ConstSrcCoeff->getValue()->getValue(); 01798 APInt BM = ConstDstCoeff->getValue()->getValue(); 01799 unsigned Bits = AM.getBitWidth(); 01800 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) { 01801 // gcd doesn't divide Delta, no dependence 01802 ++ExactRDIVindependence; 01803 return true; 01804 } 01805 01806 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); 01807 01808 // since SCEV construction seems to normalize, LM = 0 01809 APInt SrcUM(Bits, 1, true); 01810 bool SrcUMvalid = false; 01811 // SrcUM is perhaps unavailable, let's check 01812 if (const SCEVConstant *UpperBound = 01813 collectConstantUpperBound(SrcLoop, Delta->getType())) { 01814 SrcUM = UpperBound->getValue()->getValue(); 01815 DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n"); 01816 SrcUMvalid = true; 01817 } 01818 01819 APInt DstUM(Bits, 1, true); 01820 bool DstUMvalid = false; 01821 // UM is perhaps unavailable, let's check 01822 if (const SCEVConstant *UpperBound = 01823 collectConstantUpperBound(DstLoop, Delta->getType())) { 01824 DstUM = UpperBound->getValue()->getValue(); 01825 DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n"); 01826 DstUMvalid = true; 01827 } 01828 01829 APInt TU(APInt::getSignedMaxValue(Bits)); 01830 APInt TL(APInt::getSignedMinValue(Bits)); 01831 01832 // test(BM/G, LM-X) and test(-BM/G, X-UM) 01833 APInt TMUL = BM.sdiv(G); 01834 if (TMUL.sgt(0)) { 01835 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL)); 01836 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01837 if (SrcUMvalid) { 01838 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL)); 01839 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01840 } 01841 } 01842 else { 01843 TU = minAPInt(TU, floorOfQuotient(-X, TMUL)); 01844 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01845 if (SrcUMvalid) { 01846 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL)); 01847 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01848 } 01849 } 01850 01851 // test(AM/G, LM-Y) and test(-AM/G, Y-UM) 01852 TMUL = AM.sdiv(G); 01853 if (TMUL.sgt(0)) { 01854 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL)); 01855 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01856 if (DstUMvalid) { 01857 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL)); 01858 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01859 } 01860 } 01861 else { 01862 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL)); 01863 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 01864 if (DstUMvalid) { 01865 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL)); 01866 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 01867 } 01868 } 01869 if (TL.sgt(TU)) 01870 ++ExactRDIVindependence; 01871 return TL.sgt(TU); 01872 } 01873 01874 01875 // symbolicRDIVtest - 01876 // In Section 4.5 of the Practical Dependence Testing paper,the authors 01877 // introduce a special case of Banerjee's Inequalities (also called the 01878 // Extreme-Value Test) that can handle some of the SIV and RDIV cases, 01879 // particularly cases with symbolics. Since it's only able to disprove 01880 // dependence (not compute distances or directions), we'll use it as a 01881 // fall back for the other tests. 01882 // 01883 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j] 01884 // where i and j are induction variables and c1 and c2 are loop invariants, 01885 // we can use the symbolic tests to disprove some dependences, serving as a 01886 // backup for the RDIV test. Note that i and j can be the same variable, 01887 // letting this test serve as a backup for the various SIV tests. 01888 // 01889 // For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some 01890 // 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized) 01891 // loop bounds for the i and j loops, respectively. So, ... 01892 // 01893 // c1 + a1*i = c2 + a2*j 01894 // a1*i - a2*j = c2 - c1 01895 // 01896 // To test for a dependence, we compute c2 - c1 and make sure it's in the 01897 // range of the maximum and minimum possible values of a1*i - a2*j. 01898 // Considering the signs of a1 and a2, we have 4 possible cases: 01899 // 01900 // 1) If a1 >= 0 and a2 >= 0, then 01901 // a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0 01902 // -a2*N2 <= c2 - c1 <= a1*N1 01903 // 01904 // 2) If a1 >= 0 and a2 <= 0, then 01905 // a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2 01906 // 0 <= c2 - c1 <= a1*N1 - a2*N2 01907 // 01908 // 3) If a1 <= 0 and a2 >= 0, then 01909 // a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0 01910 // a1*N1 - a2*N2 <= c2 - c1 <= 0 01911 // 01912 // 4) If a1 <= 0 and a2 <= 0, then 01913 // a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2 01914 // a1*N1 <= c2 - c1 <= -a2*N2 01915 // 01916 // return true if dependence disproved 01917 bool DependenceAnalysis::symbolicRDIVtest(const SCEV *A1, 01918 const SCEV *A2, 01919 const SCEV *C1, 01920 const SCEV *C2, 01921 const Loop *Loop1, 01922 const Loop *Loop2) const { 01923 ++SymbolicRDIVapplications; 01924 DEBUG(dbgs() << "\ttry symbolic RDIV test\n"); 01925 DEBUG(dbgs() << "\t A1 = " << *A1); 01926 DEBUG(dbgs() << ", type = " << *A1->getType() << "\n"); 01927 DEBUG(dbgs() << "\t A2 = " << *A2 << "\n"); 01928 DEBUG(dbgs() << "\t C1 = " << *C1 << "\n"); 01929 DEBUG(dbgs() << "\t C2 = " << *C2 << "\n"); 01930 const SCEV *N1 = collectUpperBound(Loop1, A1->getType()); 01931 const SCEV *N2 = collectUpperBound(Loop2, A1->getType()); 01932 DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n"); 01933 DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n"); 01934 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1); 01935 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2); 01936 DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n"); 01937 DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n"); 01938 if (SE->isKnownNonNegative(A1)) { 01939 if (SE->isKnownNonNegative(A2)) { 01940 // A1 >= 0 && A2 >= 0 01941 if (N1) { 01942 // make sure that c2 - c1 <= a1*N1 01943 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 01944 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n"); 01945 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) { 01946 ++SymbolicRDIVindependence; 01947 return true; 01948 } 01949 } 01950 if (N2) { 01951 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2 01952 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 01953 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n"); 01954 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) { 01955 ++SymbolicRDIVindependence; 01956 return true; 01957 } 01958 } 01959 } 01960 else if (SE->isKnownNonPositive(A2)) { 01961 // a1 >= 0 && a2 <= 0 01962 if (N1 && N2) { 01963 // make sure that c2 - c1 <= a1*N1 - a2*N2 01964 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 01965 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 01966 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2); 01967 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n"); 01968 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) { 01969 ++SymbolicRDIVindependence; 01970 return true; 01971 } 01972 } 01973 // make sure that 0 <= c2 - c1 01974 if (SE->isKnownNegative(C2_C1)) { 01975 ++SymbolicRDIVindependence; 01976 return true; 01977 } 01978 } 01979 } 01980 else if (SE->isKnownNonPositive(A1)) { 01981 if (SE->isKnownNonNegative(A2)) { 01982 // a1 <= 0 && a2 >= 0 01983 if (N1 && N2) { 01984 // make sure that a1*N1 - a2*N2 <= c2 - c1 01985 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 01986 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 01987 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2); 01988 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n"); 01989 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) { 01990 ++SymbolicRDIVindependence; 01991 return true; 01992 } 01993 } 01994 // make sure that c2 - c1 <= 0 01995 if (SE->isKnownPositive(C2_C1)) { 01996 ++SymbolicRDIVindependence; 01997 return true; 01998 } 01999 } 02000 else if (SE->isKnownNonPositive(A2)) { 02001 // a1 <= 0 && a2 <= 0 02002 if (N1) { 02003 // make sure that a1*N1 <= c2 - c1 02004 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 02005 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n"); 02006 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) { 02007 ++SymbolicRDIVindependence; 02008 return true; 02009 } 02010 } 02011 if (N2) { 02012 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2 02013 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 02014 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n"); 02015 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) { 02016 ++SymbolicRDIVindependence; 02017 return true; 02018 } 02019 } 02020 } 02021 } 02022 return false; 02023 } 02024 02025 02026 // testSIV - 02027 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i] 02028 // where i is an induction variable, c1 and c2 are loop invariant, and a1 and 02029 // a2 are constant, we attack it with an SIV test. While they can all be 02030 // solved with the Exact SIV test, it's worthwhile to use simpler tests when 02031 // they apply; they're cheaper and sometimes more precise. 02032 // 02033 // Return true if dependence disproved. 02034 bool DependenceAnalysis::testSIV(const SCEV *Src, 02035 const SCEV *Dst, 02036 unsigned &Level, 02037 FullDependence &Result, 02038 Constraint &NewConstraint, 02039 const SCEV *&SplitIter) const { 02040 DEBUG(dbgs() << " src = " << *Src << "\n"); 02041 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 02042 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src); 02043 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst); 02044 if (SrcAddRec && DstAddRec) { 02045 const SCEV *SrcConst = SrcAddRec->getStart(); 02046 const SCEV *DstConst = DstAddRec->getStart(); 02047 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 02048 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE); 02049 const Loop *CurLoop = SrcAddRec->getLoop(); 02050 assert(CurLoop == DstAddRec->getLoop() && 02051 "both loops in SIV should be same"); 02052 Level = mapSrcLoop(CurLoop); 02053 bool disproven; 02054 if (SrcCoeff == DstCoeff) 02055 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 02056 Level, Result, NewConstraint); 02057 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff)) 02058 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 02059 Level, Result, NewConstraint, SplitIter); 02060 else 02061 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, 02062 Level, Result, NewConstraint); 02063 return disproven || 02064 gcdMIVtest(Src, Dst, Result) || 02065 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop); 02066 } 02067 if (SrcAddRec) { 02068 const SCEV *SrcConst = SrcAddRec->getStart(); 02069 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 02070 const SCEV *DstConst = Dst; 02071 const Loop *CurLoop = SrcAddRec->getLoop(); 02072 Level = mapSrcLoop(CurLoop); 02073 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 02074 Level, Result, NewConstraint) || 02075 gcdMIVtest(Src, Dst, Result); 02076 } 02077 if (DstAddRec) { 02078 const SCEV *DstConst = DstAddRec->getStart(); 02079 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE); 02080 const SCEV *SrcConst = Src; 02081 const Loop *CurLoop = DstAddRec->getLoop(); 02082 Level = mapDstLoop(CurLoop); 02083 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst, 02084 CurLoop, Level, Result, NewConstraint) || 02085 gcdMIVtest(Src, Dst, Result); 02086 } 02087 llvm_unreachable("SIV test expected at least one AddRec"); 02088 return false; 02089 } 02090 02091 02092 // testRDIV - 02093 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j] 02094 // where i and j are induction variables, c1 and c2 are loop invariant, 02095 // and a1 and a2 are constant, we can solve it exactly with an easy adaptation 02096 // of the Exact SIV test, the Restricted Double Index Variable (RDIV) test. 02097 // It doesn't make sense to talk about distance or direction in this case, 02098 // so there's no point in making special versions of the Strong SIV test or 02099 // the Weak-crossing SIV test. 02100 // 02101 // With minor algebra, this test can also be used for things like 02102 // [c1 + a1*i + a2*j][c2]. 02103 // 02104 // Return true if dependence disproved. 02105 bool DependenceAnalysis::testRDIV(const SCEV *Src, 02106 const SCEV *Dst, 02107 FullDependence &Result) const { 02108 // we have 3 possible situations here: 02109 // 1) [a*i + b] and [c*j + d] 02110 // 2) [a*i + c*j + b] and [d] 02111 // 3) [b] and [a*i + c*j + d] 02112 // We need to find what we've got and get organized 02113 02114 const SCEV *SrcConst, *DstConst; 02115 const SCEV *SrcCoeff, *DstCoeff; 02116 const Loop *SrcLoop, *DstLoop; 02117 02118 DEBUG(dbgs() << " src = " << *Src << "\n"); 02119 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 02120 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src); 02121 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst); 02122 if (SrcAddRec && DstAddRec) { 02123 SrcConst = SrcAddRec->getStart(); 02124 SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 02125 SrcLoop = SrcAddRec->getLoop(); 02126 DstConst = DstAddRec->getStart(); 02127 DstCoeff = DstAddRec->getStepRecurrence(*SE); 02128 DstLoop = DstAddRec->getLoop(); 02129 } 02130 else if (SrcAddRec) { 02131 if (const SCEVAddRecExpr *tmpAddRec = 02132 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) { 02133 SrcConst = tmpAddRec->getStart(); 02134 SrcCoeff = tmpAddRec->getStepRecurrence(*SE); 02135 SrcLoop = tmpAddRec->getLoop(); 02136 DstConst = Dst; 02137 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE)); 02138 DstLoop = SrcAddRec->getLoop(); 02139 } 02140 else 02141 llvm_unreachable("RDIV reached by surprising SCEVs"); 02142 } 02143 else if (DstAddRec) { 02144 if (const SCEVAddRecExpr *tmpAddRec = 02145 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) { 02146 DstConst = tmpAddRec->getStart(); 02147 DstCoeff = tmpAddRec->getStepRecurrence(*SE); 02148 DstLoop = tmpAddRec->getLoop(); 02149 SrcConst = Src; 02150 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE)); 02151 SrcLoop = DstAddRec->getLoop(); 02152 } 02153 else 02154 llvm_unreachable("RDIV reached by surprising SCEVs"); 02155 } 02156 else 02157 llvm_unreachable("RDIV expected at least one AddRec"); 02158 return exactRDIVtest(SrcCoeff, DstCoeff, 02159 SrcConst, DstConst, 02160 SrcLoop, DstLoop, 02161 Result) || 02162 gcdMIVtest(Src, Dst, Result) || 02163 symbolicRDIVtest(SrcCoeff, DstCoeff, 02164 SrcConst, DstConst, 02165 SrcLoop, DstLoop); 02166 } 02167 02168 02169 // Tests the single-subscript MIV pair (Src and Dst) for dependence. 02170 // Return true if dependence disproved. 02171 // Can sometimes refine direction vectors. 02172 bool DependenceAnalysis::testMIV(const SCEV *Src, 02173 const SCEV *Dst, 02174 const SmallBitVector &Loops, 02175 FullDependence &Result) const { 02176 DEBUG(dbgs() << " src = " << *Src << "\n"); 02177 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 02178 Result.Consistent = false; 02179 return gcdMIVtest(Src, Dst, Result) || 02180 banerjeeMIVtest(Src, Dst, Loops, Result); 02181 } 02182 02183 02184 // Given a product, e.g., 10*X*Y, returns the first constant operand, 02185 // in this case 10. If there is no constant part, returns NULL. 02186 static 02187 const SCEVConstant *getConstantPart(const SCEVMulExpr *Product) { 02188 for (unsigned Op = 0, Ops = Product->getNumOperands(); Op < Ops; Op++) { 02189 if (const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Product->getOperand(Op))) 02190 return Constant; 02191 } 02192 return NULL; 02193 } 02194 02195 02196 //===----------------------------------------------------------------------===// 02197 // gcdMIVtest - 02198 // Tests an MIV subscript pair for dependence. 02199 // Returns true if any possible dependence is disproved. 02200 // Marks the result as inconsistent. 02201 // Can sometimes disprove the equal direction for 1 or more loops, 02202 // as discussed in Michael Wolfe's book, 02203 // High Performance Compilers for Parallel Computing, page 235. 02204 // 02205 // We spend some effort (code!) to handle cases like 02206 // [10*i + 5*N*j + 15*M + 6], where i and j are induction variables, 02207 // but M and N are just loop-invariant variables. 02208 // This should help us handle linearized subscripts; 02209 // also makes this test a useful backup to the various SIV tests. 02210 // 02211 // It occurs to me that the presence of loop-invariant variables 02212 // changes the nature of the test from "greatest common divisor" 02213 // to "a common divisor". 02214 bool DependenceAnalysis::gcdMIVtest(const SCEV *Src, 02215 const SCEV *Dst, 02216 FullDependence &Result) const { 02217 DEBUG(dbgs() << "starting gcd\n"); 02218 ++GCDapplications; 02219 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType()); 02220 APInt RunningGCD = APInt::getNullValue(BitWidth); 02221 02222 // Examine Src coefficients. 02223 // Compute running GCD and record source constant. 02224 // Because we're looking for the constant at the end of the chain, 02225 // we can't quit the loop just because the GCD == 1. 02226 const SCEV *Coefficients = Src; 02227 while (const SCEVAddRecExpr *AddRec = 02228 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 02229 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 02230 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff); 02231 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 02232 // If the coefficient is the product of a constant and other stuff, 02233 // we can use the constant in the GCD computation. 02234 Constant = getConstantPart(Product); 02235 if (!Constant) 02236 return false; 02237 APInt ConstCoeff = Constant->getValue()->getValue(); 02238 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 02239 Coefficients = AddRec->getStart(); 02240 } 02241 const SCEV *SrcConst = Coefficients; 02242 02243 // Examine Dst coefficients. 02244 // Compute running GCD and record destination constant. 02245 // Because we're looking for the constant at the end of the chain, 02246 // we can't quit the loop just because the GCD == 1. 02247 Coefficients = Dst; 02248 while (const SCEVAddRecExpr *AddRec = 02249 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 02250 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 02251 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff); 02252 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 02253 // If the coefficient is the product of a constant and other stuff, 02254 // we can use the constant in the GCD computation. 02255 Constant = getConstantPart(Product); 02256 if (!Constant) 02257 return false; 02258 APInt ConstCoeff = Constant->getValue()->getValue(); 02259 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 02260 Coefficients = AddRec->getStart(); 02261 } 02262 const SCEV *DstConst = Coefficients; 02263 02264 APInt ExtraGCD = APInt::getNullValue(BitWidth); 02265 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 02266 DEBUG(dbgs() << " Delta = " << *Delta << "\n"); 02267 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta); 02268 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) { 02269 // If Delta is a sum of products, we may be able to make further progress. 02270 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) { 02271 const SCEV *Operand = Sum->getOperand(Op); 02272 if (isa<SCEVConstant>(Operand)) { 02273 assert(!Constant && "Surprised to find multiple constants"); 02274 Constant = cast<SCEVConstant>(Operand); 02275 } 02276 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) { 02277 // Search for constant operand to participate in GCD; 02278 // If none found; return false. 02279 const SCEVConstant *ConstOp = getConstantPart(Product); 02280 if (!ConstOp) 02281 return false; 02282 APInt ConstOpValue = ConstOp->getValue()->getValue(); 02283 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, 02284 ConstOpValue.abs()); 02285 } 02286 else 02287 return false; 02288 } 02289 } 02290 if (!Constant) 02291 return false; 02292 APInt ConstDelta = cast<SCEVConstant>(Constant)->getValue()->getValue(); 02293 DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n"); 02294 if (ConstDelta == 0) 02295 return false; 02296 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD); 02297 DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n"); 02298 APInt Remainder = ConstDelta.srem(RunningGCD); 02299 if (Remainder != 0) { 02300 ++GCDindependence; 02301 return true; 02302 } 02303 02304 // Try to disprove equal directions. 02305 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1], 02306 // the code above can't disprove the dependence because the GCD = 1. 02307 // So we consider what happen if i = i' and what happens if j = j'. 02308 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1], 02309 // which is infeasible, so we can disallow the = direction for the i level. 02310 // Setting j = j' doesn't help matters, so we end up with a direction vector 02311 // of [<>, *] 02312 // 02313 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5], 02314 // we need to remember that the constant part is 5 and the RunningGCD should 02315 // be initialized to ExtraGCD = 30. 02316 DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n'); 02317 02318 bool Improved = false; 02319 Coefficients = Src; 02320 while (const SCEVAddRecExpr *AddRec = 02321 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 02322 Coefficients = AddRec->getStart(); 02323 const Loop *CurLoop = AddRec->getLoop(); 02324 RunningGCD = ExtraGCD; 02325 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE); 02326 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff); 02327 const SCEV *Inner = Src; 02328 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) { 02329 AddRec = cast<SCEVAddRecExpr>(Inner); 02330 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 02331 if (CurLoop == AddRec->getLoop()) 02332 ; // SrcCoeff == Coeff 02333 else { 02334 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 02335 // If the coefficient is the product of a constant and other stuff, 02336 // we can use the constant in the GCD computation. 02337 Constant = getConstantPart(Product); 02338 else 02339 Constant = cast<SCEVConstant>(Coeff); 02340 APInt ConstCoeff = Constant->getValue()->getValue(); 02341 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 02342 } 02343 Inner = AddRec->getStart(); 02344 } 02345 Inner = Dst; 02346 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) { 02347 AddRec = cast<SCEVAddRecExpr>(Inner); 02348 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 02349 if (CurLoop == AddRec->getLoop()) 02350 DstCoeff = Coeff; 02351 else { 02352 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 02353 // If the coefficient is the product of a constant and other stuff, 02354 // we can use the constant in the GCD computation. 02355 Constant = getConstantPart(Product); 02356 else 02357 Constant = cast<SCEVConstant>(Coeff); 02358 APInt ConstCoeff = Constant->getValue()->getValue(); 02359 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 02360 } 02361 Inner = AddRec->getStart(); 02362 } 02363 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff); 02364 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Delta)) 02365 // If the coefficient is the product of a constant and other stuff, 02366 // we can use the constant in the GCD computation. 02367 Constant = getConstantPart(Product); 02368 else if (isa<SCEVConstant>(Delta)) 02369 Constant = cast<SCEVConstant>(Delta); 02370 else { 02371 // The difference of the two coefficients might not be a product 02372 // or constant, in which case we give up on this direction. 02373 continue; 02374 } 02375 APInt ConstCoeff = Constant->getValue()->getValue(); 02376 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 02377 DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n"); 02378 if (RunningGCD != 0) { 02379 Remainder = ConstDelta.srem(RunningGCD); 02380 DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n"); 02381 if (Remainder != 0) { 02382 unsigned Level = mapSrcLoop(CurLoop); 02383 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ); 02384 Improved = true; 02385 } 02386 } 02387 } 02388 if (Improved) 02389 ++GCDsuccesses; 02390 DEBUG(dbgs() << "all done\n"); 02391 return false; 02392 } 02393 02394 02395 //===----------------------------------------------------------------------===// 02396 // banerjeeMIVtest - 02397 // Use Banerjee's Inequalities to test an MIV subscript pair. 02398 // (Wolfe, in the race-car book, calls this the Extreme Value Test.) 02399 // Generally follows the discussion in Section 2.5.2 of 02400 // 02401 // Optimizing Supercompilers for Supercomputers 02402 // Michael Wolfe 02403 // 02404 // The inequalities given on page 25 are simplified in that loops are 02405 // normalized so that the lower bound is always 0 and the stride is always 1. 02406 // For example, Wolfe gives 02407 // 02408 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 02409 // 02410 // where A_k is the coefficient of the kth index in the source subscript, 02411 // B_k is the coefficient of the kth index in the destination subscript, 02412 // U_k is the upper bound of the kth index, L_k is the lower bound of the Kth 02413 // index, and N_k is the stride of the kth index. Since all loops are normalized 02414 // by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the 02415 // equation to 02416 // 02417 // LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1 02418 // = (A^-_k - B_k)^- (U_k - 1) - B_k 02419 // 02420 // Similar simplifications are possible for the other equations. 02421 // 02422 // When we can't determine the number of iterations for a loop, 02423 // we use NULL as an indicator for the worst case, infinity. 02424 // When computing the upper bound, NULL denotes +inf; 02425 // for the lower bound, NULL denotes -inf. 02426 // 02427 // Return true if dependence disproved. 02428 bool DependenceAnalysis::banerjeeMIVtest(const SCEV *Src, 02429 const SCEV *Dst, 02430 const SmallBitVector &Loops, 02431 FullDependence &Result) const { 02432 DEBUG(dbgs() << "starting Banerjee\n"); 02433 ++BanerjeeApplications; 02434 DEBUG(dbgs() << " Src = " << *Src << '\n'); 02435 const SCEV *A0; 02436 CoefficientInfo *A = collectCoeffInfo(Src, true, A0); 02437 DEBUG(dbgs() << " Dst = " << *Dst << '\n'); 02438 const SCEV *B0; 02439 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0); 02440 BoundInfo *Bound = new BoundInfo[MaxLevels + 1]; 02441 const SCEV *Delta = SE->getMinusSCEV(B0, A0); 02442 DEBUG(dbgs() << "\tDelta = " << *Delta << '\n'); 02443 02444 // Compute bounds for all the * directions. 02445 DEBUG(dbgs() << "\tBounds[*]\n"); 02446 for (unsigned K = 1; K <= MaxLevels; ++K) { 02447 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations; 02448 Bound[K].Direction = Dependence::DVEntry::ALL; 02449 Bound[K].DirSet = Dependence::DVEntry::NONE; 02450 findBoundsALL(A, B, Bound, K); 02451 #ifndef NDEBUG 02452 DEBUG(dbgs() << "\t " << K << '\t'); 02453 if (Bound[K].Lower[Dependence::DVEntry::ALL]) 02454 DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t'); 02455 else 02456 DEBUG(dbgs() << "-inf\t"); 02457 if (Bound[K].Upper[Dependence::DVEntry::ALL]) 02458 DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n'); 02459 else 02460 DEBUG(dbgs() << "+inf\n"); 02461 #endif 02462 } 02463 02464 // Test the *, *, *, ... case. 02465 bool Disproved = false; 02466 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) { 02467 // Explore the direction vector hierarchy. 02468 unsigned DepthExpanded = 0; 02469 unsigned NewDeps = exploreDirections(1, A, B, Bound, 02470 Loops, DepthExpanded, Delta); 02471 if (NewDeps > 0) { 02472 bool Improved = false; 02473 for (unsigned K = 1; K <= CommonLevels; ++K) { 02474 if (Loops[K]) { 02475 unsigned Old = Result.DV[K - 1].Direction; 02476 Result.DV[K - 1].Direction = Old & Bound[K].DirSet; 02477 Improved |= Old != Result.DV[K - 1].Direction; 02478 if (!Result.DV[K - 1].Direction) { 02479 Improved = false; 02480 Disproved = true; 02481 break; 02482 } 02483 } 02484 } 02485 if (Improved) 02486 ++BanerjeeSuccesses; 02487 } 02488 else { 02489 ++BanerjeeIndependence; 02490 Disproved = true; 02491 } 02492 } 02493 else { 02494 ++BanerjeeIndependence; 02495 Disproved = true; 02496 } 02497 delete [] Bound; 02498 delete [] A; 02499 delete [] B; 02500 return Disproved; 02501 } 02502 02503 02504 // Hierarchically expands the direction vector 02505 // search space, combining the directions of discovered dependences 02506 // in the DirSet field of Bound. Returns the number of distinct 02507 // dependences discovered. If the dependence is disproved, 02508 // it will return 0. 02509 unsigned DependenceAnalysis::exploreDirections(unsigned Level, 02510 CoefficientInfo *A, 02511 CoefficientInfo *B, 02512 BoundInfo *Bound, 02513 const SmallBitVector &Loops, 02514 unsigned &DepthExpanded, 02515 const SCEV *Delta) const { 02516 if (Level > CommonLevels) { 02517 // record result 02518 DEBUG(dbgs() << "\t["); 02519 for (unsigned K = 1; K <= CommonLevels; ++K) { 02520 if (Loops[K]) { 02521 Bound[K].DirSet |= Bound[K].Direction; 02522 #ifndef NDEBUG 02523 switch (Bound[K].Direction) { 02524 case Dependence::DVEntry::LT: 02525 DEBUG(dbgs() << " <"); 02526 break; 02527 case Dependence::DVEntry::EQ: 02528 DEBUG(dbgs() << " ="); 02529 break; 02530 case Dependence::DVEntry::GT: 02531 DEBUG(dbgs() << " >"); 02532 break; 02533 case Dependence::DVEntry::ALL: 02534 DEBUG(dbgs() << " *"); 02535 break; 02536 default: 02537 llvm_unreachable("unexpected Bound[K].Direction"); 02538 } 02539 #endif 02540 } 02541 } 02542 DEBUG(dbgs() << " ]\n"); 02543 return 1; 02544 } 02545 if (Loops[Level]) { 02546 if (Level > DepthExpanded) { 02547 DepthExpanded = Level; 02548 // compute bounds for <, =, > at current level 02549 findBoundsLT(A, B, Bound, Level); 02550 findBoundsGT(A, B, Bound, Level); 02551 findBoundsEQ(A, B, Bound, Level); 02552 #ifndef NDEBUG 02553 DEBUG(dbgs() << "\tBound for level = " << Level << '\n'); 02554 DEBUG(dbgs() << "\t <\t"); 02555 if (Bound[Level].Lower[Dependence::DVEntry::LT]) 02556 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT] << '\t'); 02557 else 02558 DEBUG(dbgs() << "-inf\t"); 02559 if (Bound[Level].Upper[Dependence::DVEntry::LT]) 02560 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT] << '\n'); 02561 else 02562 DEBUG(dbgs() << "+inf\n"); 02563 DEBUG(dbgs() << "\t =\t"); 02564 if (Bound[Level].Lower[Dependence::DVEntry::EQ]) 02565 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ] << '\t'); 02566 else 02567 DEBUG(dbgs() << "-inf\t"); 02568 if (Bound[Level].Upper[Dependence::DVEntry::EQ]) 02569 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ] << '\n'); 02570 else 02571 DEBUG(dbgs() << "+inf\n"); 02572 DEBUG(dbgs() << "\t >\t"); 02573 if (Bound[Level].Lower[Dependence::DVEntry::GT]) 02574 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT] << '\t'); 02575 else 02576 DEBUG(dbgs() << "-inf\t"); 02577 if (Bound[Level].Upper[Dependence::DVEntry::GT]) 02578 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT] << '\n'); 02579 else 02580 DEBUG(dbgs() << "+inf\n"); 02581 #endif 02582 } 02583 02584 unsigned NewDeps = 0; 02585 02586 // test bounds for <, *, *, ... 02587 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta)) 02588 NewDeps += exploreDirections(Level + 1, A, B, Bound, 02589 Loops, DepthExpanded, Delta); 02590 02591 // Test bounds for =, *, *, ... 02592 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta)) 02593 NewDeps += exploreDirections(Level + 1, A, B, Bound, 02594 Loops, DepthExpanded, Delta); 02595 02596 // test bounds for >, *, *, ... 02597 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta)) 02598 NewDeps += exploreDirections(Level + 1, A, B, Bound, 02599 Loops, DepthExpanded, Delta); 02600 02601 Bound[Level].Direction = Dependence::DVEntry::ALL; 02602 return NewDeps; 02603 } 02604 else 02605 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta); 02606 } 02607 02608 02609 // Returns true iff the current bounds are plausible. 02610 bool DependenceAnalysis::testBounds(unsigned char DirKind, 02611 unsigned Level, 02612 BoundInfo *Bound, 02613 const SCEV *Delta) const { 02614 Bound[Level].Direction = DirKind; 02615 if (const SCEV *LowerBound = getLowerBound(Bound)) 02616 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta)) 02617 return false; 02618 if (const SCEV *UpperBound = getUpperBound(Bound)) 02619 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound)) 02620 return false; 02621 return true; 02622 } 02623 02624 02625 // Computes the upper and lower bounds for level K 02626 // using the * direction. Records them in Bound. 02627 // Wolfe gives the equations 02628 // 02629 // LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k 02630 // UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k 02631 // 02632 // Since we normalize loops, we can simplify these equations to 02633 // 02634 // LB^*_k = (A^-_k - B^+_k)U_k 02635 // UB^*_k = (A^+_k - B^-_k)U_k 02636 // 02637 // We must be careful to handle the case where the upper bound is unknown. 02638 // Note that the lower bound is always <= 0 02639 // and the upper bound is always >= 0. 02640 void DependenceAnalysis::findBoundsALL(CoefficientInfo *A, 02641 CoefficientInfo *B, 02642 BoundInfo *Bound, 02643 unsigned K) const { 02644 Bound[K].Lower[Dependence::DVEntry::ALL] = NULL; // Default value = -infinity. 02645 Bound[K].Upper[Dependence::DVEntry::ALL] = NULL; // Default value = +infinity. 02646 if (Bound[K].Iterations) { 02647 Bound[K].Lower[Dependence::DVEntry::ALL] = 02648 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), 02649 Bound[K].Iterations); 02650 Bound[K].Upper[Dependence::DVEntry::ALL] = 02651 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), 02652 Bound[K].Iterations); 02653 } 02654 else { 02655 // If the difference is 0, we won't need to know the number of iterations. 02656 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart)) 02657 Bound[K].Lower[Dependence::DVEntry::ALL] = 02658 SE->getConstant(A[K].Coeff->getType(), 0); 02659 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart)) 02660 Bound[K].Upper[Dependence::DVEntry::ALL] = 02661 SE->getConstant(A[K].Coeff->getType(), 0); 02662 } 02663 } 02664 02665 02666 // Computes the upper and lower bounds for level K 02667 // using the = direction. Records them in Bound. 02668 // Wolfe gives the equations 02669 // 02670 // LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k 02671 // UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k 02672 // 02673 // Since we normalize loops, we can simplify these equations to 02674 // 02675 // LB^=_k = (A_k - B_k)^- U_k 02676 // UB^=_k = (A_k - B_k)^+ U_k 02677 // 02678 // We must be careful to handle the case where the upper bound is unknown. 02679 // Note that the lower bound is always <= 0 02680 // and the upper bound is always >= 0. 02681 void DependenceAnalysis::findBoundsEQ(CoefficientInfo *A, 02682 CoefficientInfo *B, 02683 BoundInfo *Bound, 02684 unsigned K) const { 02685 Bound[K].Lower[Dependence::DVEntry::EQ] = NULL; // Default value = -infinity. 02686 Bound[K].Upper[Dependence::DVEntry::EQ] = NULL; // Default value = +infinity. 02687 if (Bound[K].Iterations) { 02688 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff); 02689 const SCEV *NegativePart = getNegativePart(Delta); 02690 Bound[K].Lower[Dependence::DVEntry::EQ] = 02691 SE->getMulExpr(NegativePart, Bound[K].Iterations); 02692 const SCEV *PositivePart = getPositivePart(Delta); 02693 Bound[K].Upper[Dependence::DVEntry::EQ] = 02694 SE->getMulExpr(PositivePart, Bound[K].Iterations); 02695 } 02696 else { 02697 // If the positive/negative part of the difference is 0, 02698 // we won't need to know the number of iterations. 02699 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff); 02700 const SCEV *NegativePart = getNegativePart(Delta); 02701 if (NegativePart->isZero()) 02702 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero 02703 const SCEV *PositivePart = getPositivePart(Delta); 02704 if (PositivePart->isZero()) 02705 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero 02706 } 02707 } 02708 02709 02710 // Computes the upper and lower bounds for level K 02711 // using the < direction. Records them in Bound. 02712 // Wolfe gives the equations 02713 // 02714 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 02715 // UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 02716 // 02717 // Since we normalize loops, we can simplify these equations to 02718 // 02719 // LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k 02720 // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k 02721 // 02722 // We must be careful to handle the case where the upper bound is unknown. 02723 void DependenceAnalysis::findBoundsLT(CoefficientInfo *A, 02724 CoefficientInfo *B, 02725 BoundInfo *Bound, 02726 unsigned K) const { 02727 Bound[K].Lower[Dependence::DVEntry::LT] = NULL; // Default value = -infinity. 02728 Bound[K].Upper[Dependence::DVEntry::LT] = NULL; // Default value = +infinity. 02729 if (Bound[K].Iterations) { 02730 const SCEV *Iter_1 = 02731 SE->getMinusSCEV(Bound[K].Iterations, 02732 SE->getConstant(Bound[K].Iterations->getType(), 1)); 02733 const SCEV *NegPart = 02734 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff)); 02735 Bound[K].Lower[Dependence::DVEntry::LT] = 02736 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff); 02737 const SCEV *PosPart = 02738 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff)); 02739 Bound[K].Upper[Dependence::DVEntry::LT] = 02740 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff); 02741 } 02742 else { 02743 // If the positive/negative part of the difference is 0, 02744 // we won't need to know the number of iterations. 02745 const SCEV *NegPart = 02746 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff)); 02747 if (NegPart->isZero()) 02748 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff); 02749 const SCEV *PosPart = 02750 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff)); 02751 if (PosPart->isZero()) 02752 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff); 02753 } 02754 } 02755 02756 02757 // Computes the upper and lower bounds for level K 02758 // using the > direction. Records them in Bound. 02759 // Wolfe gives the equations 02760 // 02761 // LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k 02762 // UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k 02763 // 02764 // Since we normalize loops, we can simplify these equations to 02765 // 02766 // LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k 02767 // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k 02768 // 02769 // We must be careful to handle the case where the upper bound is unknown. 02770 void DependenceAnalysis::findBoundsGT(CoefficientInfo *A, 02771 CoefficientInfo *B, 02772 BoundInfo *Bound, 02773 unsigned K) const { 02774 Bound[K].Lower[Dependence::DVEntry::GT] = NULL; // Default value = -infinity. 02775 Bound[K].Upper[Dependence::DVEntry::GT] = NULL; // Default value = +infinity. 02776 if (Bound[K].Iterations) { 02777 const SCEV *Iter_1 = 02778 SE->getMinusSCEV(Bound[K].Iterations, 02779 SE->getConstant(Bound[K].Iterations->getType(), 1)); 02780 const SCEV *NegPart = 02781 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart)); 02782 Bound[K].Lower[Dependence::DVEntry::GT] = 02783 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff); 02784 const SCEV *PosPart = 02785 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart)); 02786 Bound[K].Upper[Dependence::DVEntry::GT] = 02787 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff); 02788 } 02789 else { 02790 // If the positive/negative part of the difference is 0, 02791 // we won't need to know the number of iterations. 02792 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart)); 02793 if (NegPart->isZero()) 02794 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff; 02795 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart)); 02796 if (PosPart->isZero()) 02797 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff; 02798 } 02799 } 02800 02801 02802 // X^+ = max(X, 0) 02803 const SCEV *DependenceAnalysis::getPositivePart(const SCEV *X) const { 02804 return SE->getSMaxExpr(X, SE->getConstant(X->getType(), 0)); 02805 } 02806 02807 02808 // X^- = min(X, 0) 02809 const SCEV *DependenceAnalysis::getNegativePart(const SCEV *X) const { 02810 return SE->getSMinExpr(X, SE->getConstant(X->getType(), 0)); 02811 } 02812 02813 02814 // Walks through the subscript, 02815 // collecting each coefficient, the associated loop bounds, 02816 // and recording its positive and negative parts for later use. 02817 DependenceAnalysis::CoefficientInfo * 02818 DependenceAnalysis::collectCoeffInfo(const SCEV *Subscript, 02819 bool SrcFlag, 02820 const SCEV *&Constant) const { 02821 const SCEV *Zero = SE->getConstant(Subscript->getType(), 0); 02822 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1]; 02823 for (unsigned K = 1; K <= MaxLevels; ++K) { 02824 CI[K].Coeff = Zero; 02825 CI[K].PosPart = Zero; 02826 CI[K].NegPart = Zero; 02827 CI[K].Iterations = NULL; 02828 } 02829 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) { 02830 const Loop *L = AddRec->getLoop(); 02831 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L); 02832 CI[K].Coeff = AddRec->getStepRecurrence(*SE); 02833 CI[K].PosPart = getPositivePart(CI[K].Coeff); 02834 CI[K].NegPart = getNegativePart(CI[K].Coeff); 02835 CI[K].Iterations = collectUpperBound(L, Subscript->getType()); 02836 Subscript = AddRec->getStart(); 02837 } 02838 Constant = Subscript; 02839 #ifndef NDEBUG 02840 DEBUG(dbgs() << "\tCoefficient Info\n"); 02841 for (unsigned K = 1; K <= MaxLevels; ++K) { 02842 DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff); 02843 DEBUG(dbgs() << "\tPos Part = "); 02844 DEBUG(dbgs() << *CI[K].PosPart); 02845 DEBUG(dbgs() << "\tNeg Part = "); 02846 DEBUG(dbgs() << *CI[K].NegPart); 02847 DEBUG(dbgs() << "\tUpper Bound = "); 02848 if (CI[K].Iterations) 02849 DEBUG(dbgs() << *CI[K].Iterations); 02850 else 02851 DEBUG(dbgs() << "+inf"); 02852 DEBUG(dbgs() << '\n'); 02853 } 02854 DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n'); 02855 #endif 02856 return CI; 02857 } 02858 02859 02860 // Looks through all the bounds info and 02861 // computes the lower bound given the current direction settings 02862 // at each level. If the lower bound for any level is -inf, 02863 // the result is -inf. 02864 const SCEV *DependenceAnalysis::getLowerBound(BoundInfo *Bound) const { 02865 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction]; 02866 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { 02867 if (Bound[K].Lower[Bound[K].Direction]) 02868 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]); 02869 else 02870 Sum = NULL; 02871 } 02872 return Sum; 02873 } 02874 02875 02876 // Looks through all the bounds info and 02877 // computes the upper bound given the current direction settings 02878 // at each level. If the upper bound at any level is +inf, 02879 // the result is +inf. 02880 const SCEV *DependenceAnalysis::getUpperBound(BoundInfo *Bound) const { 02881 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction]; 02882 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { 02883 if (Bound[K].Upper[Bound[K].Direction]) 02884 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]); 02885 else 02886 Sum = NULL; 02887 } 02888 return Sum; 02889 } 02890 02891 02892 //===----------------------------------------------------------------------===// 02893 // Constraint manipulation for Delta test. 02894 02895 // Given a linear SCEV, 02896 // return the coefficient (the step) 02897 // corresponding to the specified loop. 02898 // If there isn't one, return 0. 02899 // For example, given a*i + b*j + c*k, zeroing the coefficient 02900 // corresponding to the j loop would yield b. 02901 const SCEV *DependenceAnalysis::findCoefficient(const SCEV *Expr, 02902 const Loop *TargetLoop) const { 02903 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 02904 if (!AddRec) 02905 return SE->getConstant(Expr->getType(), 0); 02906 if (AddRec->getLoop() == TargetLoop) 02907 return AddRec->getStepRecurrence(*SE); 02908 return findCoefficient(AddRec->getStart(), TargetLoop); 02909 } 02910 02911 02912 // Given a linear SCEV, 02913 // return the SCEV given by zeroing out the coefficient 02914 // corresponding to the specified loop. 02915 // For example, given a*i + b*j + c*k, zeroing the coefficient 02916 // corresponding to the j loop would yield a*i + c*k. 02917 const SCEV *DependenceAnalysis::zeroCoefficient(const SCEV *Expr, 02918 const Loop *TargetLoop) const { 02919 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 02920 if (!AddRec) 02921 return Expr; // ignore 02922 if (AddRec->getLoop() == TargetLoop) 02923 return AddRec->getStart(); 02924 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop), 02925 AddRec->getStepRecurrence(*SE), 02926 AddRec->getLoop(), 02927 AddRec->getNoWrapFlags()); 02928 } 02929 02930 02931 // Given a linear SCEV Expr, 02932 // return the SCEV given by adding some Value to the 02933 // coefficient corresponding to the specified TargetLoop. 02934 // For example, given a*i + b*j + c*k, adding 1 to the coefficient 02935 // corresponding to the j loop would yield a*i + (b+1)*j + c*k. 02936 const SCEV *DependenceAnalysis::addToCoefficient(const SCEV *Expr, 02937 const Loop *TargetLoop, 02938 const SCEV *Value) const { 02939 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 02940 if (!AddRec) // create a new addRec 02941 return SE->getAddRecExpr(Expr, 02942 Value, 02943 TargetLoop, 02944 SCEV::FlagAnyWrap); // Worst case, with no info. 02945 if (AddRec->getLoop() == TargetLoop) { 02946 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value); 02947 if (Sum->isZero()) 02948 return AddRec->getStart(); 02949 return SE->getAddRecExpr(AddRec->getStart(), 02950 Sum, 02951 AddRec->getLoop(), 02952 AddRec->getNoWrapFlags()); 02953 } 02954 return SE->getAddRecExpr(addToCoefficient(AddRec->getStart(), 02955 TargetLoop, Value), 02956 AddRec->getStepRecurrence(*SE), 02957 AddRec->getLoop(), 02958 AddRec->getNoWrapFlags()); 02959 } 02960 02961 02962 // Review the constraints, looking for opportunities 02963 // to simplify a subscript pair (Src and Dst). 02964 // Return true if some simplification occurs. 02965 // If the simplification isn't exact (that is, if it is conservative 02966 // in terms of dependence), set consistent to false. 02967 // Corresponds to Figure 5 from the paper 02968 // 02969 // Practical Dependence Testing 02970 // Goff, Kennedy, Tseng 02971 // PLDI 1991 02972 bool DependenceAnalysis::propagate(const SCEV *&Src, 02973 const SCEV *&Dst, 02974 SmallBitVector &Loops, 02975 SmallVector<Constraint, 4> &Constraints, 02976 bool &Consistent) { 02977 bool Result = false; 02978 for (int LI = Loops.find_first(); LI >= 0; LI = Loops.find_next(LI)) { 02979 DEBUG(dbgs() << "\t Constraint[" << LI << "] is"); 02980 DEBUG(Constraints[LI].dump(dbgs())); 02981 if (Constraints[LI].isDistance()) 02982 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent); 02983 else if (Constraints[LI].isLine()) 02984 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent); 02985 else if (Constraints[LI].isPoint()) 02986 Result |= propagatePoint(Src, Dst, Constraints[LI]); 02987 } 02988 return Result; 02989 } 02990 02991 02992 // Attempt to propagate a distance 02993 // constraint into a subscript pair (Src and Dst). 02994 // Return true if some simplification occurs. 02995 // If the simplification isn't exact (that is, if it is conservative 02996 // in terms of dependence), set consistent to false. 02997 bool DependenceAnalysis::propagateDistance(const SCEV *&Src, 02998 const SCEV *&Dst, 02999 Constraint &CurConstraint, 03000 bool &Consistent) { 03001 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 03002 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n"); 03003 const SCEV *A_K = findCoefficient(Src, CurLoop); 03004 if (A_K->isZero()) 03005 return false; 03006 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD()); 03007 Src = SE->getMinusSCEV(Src, DA_K); 03008 Src = zeroCoefficient(Src, CurLoop); 03009 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n"); 03010 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n"); 03011 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K)); 03012 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n"); 03013 if (!findCoefficient(Dst, CurLoop)->isZero()) 03014 Consistent = false; 03015 return true; 03016 } 03017 03018 03019 // Attempt to propagate a line 03020 // constraint into a subscript pair (Src and Dst). 03021 // Return true if some simplification occurs. 03022 // If the simplification isn't exact (that is, if it is conservative 03023 // in terms of dependence), set consistent to false. 03024 bool DependenceAnalysis::propagateLine(const SCEV *&Src, 03025 const SCEV *&Dst, 03026 Constraint &CurConstraint, 03027 bool &Consistent) { 03028 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 03029 const SCEV *A = CurConstraint.getA(); 03030 const SCEV *B = CurConstraint.getB(); 03031 const SCEV *C = CurConstraint.getC(); 03032 DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C << "\n"); 03033 DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n"); 03034 DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n"); 03035 if (A->isZero()) { 03036 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B); 03037 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 03038 if (!Bconst || !Cconst) return false; 03039 APInt Beta = Bconst->getValue()->getValue(); 03040 APInt Charlie = Cconst->getValue()->getValue(); 03041 APInt CdivB = Charlie.sdiv(Beta); 03042 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B"); 03043 const SCEV *AP_K = findCoefficient(Dst, CurLoop); 03044 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB))); 03045 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB))); 03046 Dst = zeroCoefficient(Dst, CurLoop); 03047 if (!findCoefficient(Src, CurLoop)->isZero()) 03048 Consistent = false; 03049 } 03050 else if (B->isZero()) { 03051 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A); 03052 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 03053 if (!Aconst || !Cconst) return false; 03054 APInt Alpha = Aconst->getValue()->getValue(); 03055 APInt Charlie = Cconst->getValue()->getValue(); 03056 APInt CdivA = Charlie.sdiv(Alpha); 03057 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A"); 03058 const SCEV *A_K = findCoefficient(Src, CurLoop); 03059 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA))); 03060 Src = zeroCoefficient(Src, CurLoop); 03061 if (!findCoefficient(Dst, CurLoop)->isZero()) 03062 Consistent = false; 03063 } 03064 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) { 03065 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A); 03066 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 03067 if (!Aconst || !Cconst) return false; 03068 APInt Alpha = Aconst->getValue()->getValue(); 03069 APInt Charlie = Cconst->getValue()->getValue(); 03070 APInt CdivA = Charlie.sdiv(Alpha); 03071 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A"); 03072 const SCEV *A_K = findCoefficient(Src, CurLoop); 03073 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA))); 03074 Src = zeroCoefficient(Src, CurLoop); 03075 Dst = addToCoefficient(Dst, CurLoop, A_K); 03076 if (!findCoefficient(Dst, CurLoop)->isZero()) 03077 Consistent = false; 03078 } 03079 else { 03080 // paper is incorrect here, or perhaps just misleading 03081 const SCEV *A_K = findCoefficient(Src, CurLoop); 03082 Src = SE->getMulExpr(Src, A); 03083 Dst = SE->getMulExpr(Dst, A); 03084 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C)); 03085 Src = zeroCoefficient(Src, CurLoop); 03086 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B)); 03087 if (!findCoefficient(Dst, CurLoop)->isZero()) 03088 Consistent = false; 03089 } 03090 DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n"); 03091 DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n"); 03092 return true; 03093 } 03094 03095 03096 // Attempt to propagate a point 03097 // constraint into a subscript pair (Src and Dst). 03098 // Return true if some simplification occurs. 03099 bool DependenceAnalysis::propagatePoint(const SCEV *&Src, 03100 const SCEV *&Dst, 03101 Constraint &CurConstraint) { 03102 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 03103 const SCEV *A_K = findCoefficient(Src, CurLoop); 03104 const SCEV *AP_K = findCoefficient(Dst, CurLoop); 03105 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX()); 03106 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY()); 03107 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n"); 03108 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K)); 03109 Src = zeroCoefficient(Src, CurLoop); 03110 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n"); 03111 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n"); 03112 Dst = zeroCoefficient(Dst, CurLoop); 03113 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n"); 03114 return true; 03115 } 03116 03117 03118 // Update direction vector entry based on the current constraint. 03119 void DependenceAnalysis::updateDirection(Dependence::DVEntry &Level, 03120 const Constraint &CurConstraint 03121 ) const { 03122 DEBUG(dbgs() << "\tUpdate direction, constraint ="); 03123 DEBUG(CurConstraint.dump(dbgs())); 03124 if (CurConstraint.isAny()) 03125 ; // use defaults 03126 else if (CurConstraint.isDistance()) { 03127 // this one is consistent, the others aren't 03128 Level.Scalar = false; 03129 Level.Distance = CurConstraint.getD(); 03130 unsigned NewDirection = Dependence::DVEntry::NONE; 03131 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero 03132 NewDirection = Dependence::DVEntry::EQ; 03133 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive 03134 NewDirection |= Dependence::DVEntry::LT; 03135 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative 03136 NewDirection |= Dependence::DVEntry::GT; 03137 Level.Direction &= NewDirection; 03138 } 03139 else if (CurConstraint.isLine()) { 03140 Level.Scalar = false; 03141 Level.Distance = NULL; 03142 // direction should be accurate 03143 } 03144 else if (CurConstraint.isPoint()) { 03145 Level.Scalar = false; 03146 Level.Distance = NULL; 03147 unsigned NewDirection = Dependence::DVEntry::NONE; 03148 if (!isKnownPredicate(CmpInst::ICMP_NE, 03149 CurConstraint.getY(), 03150 CurConstraint.getX())) 03151 // if X may be = Y 03152 NewDirection |= Dependence::DVEntry::EQ; 03153 if (!isKnownPredicate(CmpInst::ICMP_SLE, 03154 CurConstraint.getY(), 03155 CurConstraint.getX())) 03156 // if Y may be > X 03157 NewDirection |= Dependence::DVEntry::LT; 03158 if (!isKnownPredicate(CmpInst::ICMP_SGE, 03159 CurConstraint.getY(), 03160 CurConstraint.getX())) 03161 // if Y may be < X 03162 NewDirection |= Dependence::DVEntry::GT; 03163 Level.Direction &= NewDirection; 03164 } 03165 else 03166 llvm_unreachable("constraint has unexpected kind"); 03167 } 03168 03169 03170 //===----------------------------------------------------------------------===// 03171 03172 #ifndef NDEBUG 03173 // For debugging purposes, dump a small bit vector to dbgs(). 03174 static void dumpSmallBitVector(SmallBitVector &BV) { 03175 dbgs() << "{"; 03176 for (int VI = BV.find_first(); VI >= 0; VI = BV.find_next(VI)) { 03177 dbgs() << VI; 03178 if (BV.find_next(VI) >= 0) 03179 dbgs() << ' '; 03180 } 03181 dbgs() << "}\n"; 03182 } 03183 #endif 03184 03185 03186 // depends - 03187 // Returns NULL if there is no dependence. 03188 // Otherwise, return a Dependence with as many details as possible. 03189 // Corresponds to Section 3.1 in the paper 03190 // 03191 // Practical Dependence Testing 03192 // Goff, Kennedy, Tseng 03193 // PLDI 1991 03194 // 03195 // Care is required to keep the routine below, getSplitIteration(), 03196 // up to date with respect to this routine. 03197 Dependence *DependenceAnalysis::depends(Instruction *Src, 03198 Instruction *Dst, 03199 bool PossiblyLoopIndependent) { 03200 if (Src == Dst) 03201 PossiblyLoopIndependent = false; 03202 03203 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) || 03204 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory())) 03205 // if both instructions don't reference memory, there's no dependence 03206 return NULL; 03207 03208 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) { 03209 // can only analyze simple loads and stores, i.e., no calls, invokes, etc. 03210 DEBUG(dbgs() << "can only handle simple loads and stores\n"); 03211 return new Dependence(Src, Dst); 03212 } 03213 03214 Value *SrcPtr = getPointerOperand(Src); 03215 Value *DstPtr = getPointerOperand(Dst); 03216 03217 switch (underlyingObjectsAlias(AA, DstPtr, SrcPtr)) { 03218 case AliasAnalysis::MayAlias: 03219 case AliasAnalysis::PartialAlias: 03220 // cannot analyse objects if we don't understand their aliasing. 03221 DEBUG(dbgs() << "can't analyze may or partial alias\n"); 03222 return new Dependence(Src, Dst); 03223 case AliasAnalysis::NoAlias: 03224 // If the objects noalias, they are distinct, accesses are independent. 03225 DEBUG(dbgs() << "no alias\n"); 03226 return NULL; 03227 case AliasAnalysis::MustAlias: 03228 break; // The underlying objects alias; test accesses for dependence. 03229 } 03230 03231 // establish loop nesting levels 03232 establishNestingLevels(Src, Dst); 03233 DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n"); 03234 DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n"); 03235 03236 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels); 03237 ++TotalArrayPairs; 03238 03239 // See if there are GEPs we can use. 03240 bool UsefulGEP = false; 03241 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr); 03242 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr); 03243 if (SrcGEP && DstGEP && 03244 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) { 03245 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand()); 03246 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand()); 03247 DEBUG(dbgs() << " SrcPtrSCEV = " << *SrcPtrSCEV << "\n"); 03248 DEBUG(dbgs() << " DstPtrSCEV = " << *DstPtrSCEV << "\n"); 03249 03250 UsefulGEP = 03251 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) && 03252 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())); 03253 } 03254 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1; 03255 SmallVector<Subscript, 4> Pair(Pairs); 03256 if (UsefulGEP) { 03257 DEBUG(dbgs() << " using GEPs\n"); 03258 unsigned P = 0; 03259 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(), 03260 SrcEnd = SrcGEP->idx_end(), 03261 DstIdx = DstGEP->idx_begin(); 03262 SrcIdx != SrcEnd; 03263 ++SrcIdx, ++DstIdx, ++P) { 03264 Pair[P].Src = SE->getSCEV(*SrcIdx); 03265 Pair[P].Dst = SE->getSCEV(*DstIdx); 03266 } 03267 } 03268 else { 03269 DEBUG(dbgs() << " ignoring GEPs\n"); 03270 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr); 03271 const SCEV *DstSCEV = SE->getSCEV(DstPtr); 03272 DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n"); 03273 DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n"); 03274 Pair[0].Src = SrcSCEV; 03275 Pair[0].Dst = DstSCEV; 03276 } 03277 03278 for (unsigned P = 0; P < Pairs; ++P) { 03279 Pair[P].Loops.resize(MaxLevels + 1); 03280 Pair[P].GroupLoops.resize(MaxLevels + 1); 03281 Pair[P].Group.resize(Pairs); 03282 removeMatchingExtensions(&Pair[P]); 03283 Pair[P].Classification = 03284 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), 03285 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), 03286 Pair[P].Loops); 03287 Pair[P].GroupLoops = Pair[P].Loops; 03288 Pair[P].Group.set(P); 03289 DEBUG(dbgs() << " subscript " << P << "\n"); 03290 DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n"); 03291 DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n"); 03292 DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n"); 03293 DEBUG(dbgs() << "\tloops = "); 03294 DEBUG(dumpSmallBitVector(Pair[P].Loops)); 03295 } 03296 03297 SmallBitVector Separable(Pairs); 03298 SmallBitVector Coupled(Pairs); 03299 03300 // Partition subscripts into separable and minimally-coupled groups 03301 // Algorithm in paper is algorithmically better; 03302 // this may be faster in practice. Check someday. 03303 // 03304 // Here's an example of how it works. Consider this code: 03305 // 03306 // for (i = ...) { 03307 // for (j = ...) { 03308 // for (k = ...) { 03309 // for (l = ...) { 03310 // for (m = ...) { 03311 // A[i][j][k][m] = ...; 03312 // ... = A[0][j][l][i + j]; 03313 // } 03314 // } 03315 // } 03316 // } 03317 // } 03318 // 03319 // There are 4 subscripts here: 03320 // 0 [i] and [0] 03321 // 1 [j] and [j] 03322 // 2 [k] and [l] 03323 // 3 [m] and [i + j] 03324 // 03325 // We've already classified each subscript pair as ZIV, SIV, etc., 03326 // and collected all the loops mentioned by pair P in Pair[P].Loops. 03327 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops 03328 // and set Pair[P].Group = {P}. 03329 // 03330 // Src Dst Classification Loops GroupLoops Group 03331 // 0 [i] [0] SIV {1} {1} {0} 03332 // 1 [j] [j] SIV {2} {2} {1} 03333 // 2 [k] [l] RDIV {3,4} {3,4} {2} 03334 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3} 03335 // 03336 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ. 03337 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc. 03338 // 03339 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty. 03340 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty. 03341 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty, 03342 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added 03343 // to either Separable or Coupled). 03344 // 03345 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty. 03346 // Next, 1 and 3. The intersectionof their GroupLoops = {2}, not empty, 03347 // so Pair[3].Group = {0, 1, 3} and Done = false. 03348 // 03349 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty. 03350 // Since Done remains true, we add 2 to the set of Separable pairs. 03351 // 03352 // Finally, we consider 3. There's nothing to compare it with, 03353 // so Done remains true and we add it to the Coupled set. 03354 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}. 03355 // 03356 // In the end, we've got 1 separable subscript and 1 coupled group. 03357 for (unsigned SI = 0; SI < Pairs; ++SI) { 03358 if (Pair[SI].Classification == Subscript::NonLinear) { 03359 // ignore these, but collect loops for later 03360 ++NonlinearSubscriptPairs; 03361 collectCommonLoops(Pair[SI].Src, 03362 LI->getLoopFor(Src->getParent()), 03363 Pair[SI].Loops); 03364 collectCommonLoops(Pair[SI].Dst, 03365 LI->getLoopFor(Dst->getParent()), 03366 Pair[SI].Loops); 03367 Result.Consistent = false; 03368 } 03369 else if (Pair[SI].Classification == Subscript::ZIV) { 03370 // always separable 03371 Separable.set(SI); 03372 } 03373 else { 03374 // SIV, RDIV, or MIV, so check for coupled group 03375 bool Done = true; 03376 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) { 03377 SmallBitVector Intersection = Pair[SI].GroupLoops; 03378 Intersection &= Pair[SJ].GroupLoops; 03379 if (Intersection.any()) { 03380 // accumulate set of all the loops in group 03381 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops; 03382 // accumulate set of all subscripts in group 03383 Pair[SJ].Group |= Pair[SI].Group; 03384 Done = false; 03385 } 03386 } 03387 if (Done) { 03388 if (Pair[SI].Group.count() == 1) { 03389 Separable.set(SI); 03390 ++SeparableSubscriptPairs; 03391 } 03392 else { 03393 Coupled.set(SI); 03394 ++CoupledSubscriptPairs; 03395 } 03396 } 03397 } 03398 } 03399 03400 DEBUG(dbgs() << " Separable = "); 03401 DEBUG(dumpSmallBitVector(Separable)); 03402 DEBUG(dbgs() << " Coupled = "); 03403 DEBUG(dumpSmallBitVector(Coupled)); 03404 03405 Constraint NewConstraint; 03406 NewConstraint.setAny(SE); 03407 03408 // test separable subscripts 03409 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) { 03410 DEBUG(dbgs() << "testing subscript " << SI); 03411 switch (Pair[SI].Classification) { 03412 case Subscript::ZIV: 03413 DEBUG(dbgs() << ", ZIV\n"); 03414 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result)) 03415 return NULL; 03416 break; 03417 case Subscript::SIV: { 03418 DEBUG(dbgs() << ", SIV\n"); 03419 unsigned Level; 03420 const SCEV *SplitIter = NULL; 03421 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, 03422 Result, NewConstraint, SplitIter)) 03423 return NULL; 03424 break; 03425 } 03426 case Subscript::RDIV: 03427 DEBUG(dbgs() << ", RDIV\n"); 03428 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result)) 03429 return NULL; 03430 break; 03431 case Subscript::MIV: 03432 DEBUG(dbgs() << ", MIV\n"); 03433 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result)) 03434 return NULL; 03435 break; 03436 default: 03437 llvm_unreachable("subscript has unexpected classification"); 03438 } 03439 } 03440 03441 if (Coupled.count()) { 03442 // test coupled subscript groups 03443 DEBUG(dbgs() << "starting on coupled subscripts\n"); 03444 DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n"); 03445 SmallVector<Constraint, 4> Constraints(MaxLevels + 1); 03446 for (unsigned II = 0; II <= MaxLevels; ++II) 03447 Constraints[II].setAny(SE); 03448 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) { 03449 DEBUG(dbgs() << "testing subscript group " << SI << " { "); 03450 SmallBitVector Group(Pair[SI].Group); 03451 SmallBitVector Sivs(Pairs); 03452 SmallBitVector Mivs(Pairs); 03453 SmallBitVector ConstrainedLevels(MaxLevels + 1); 03454 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) { 03455 DEBUG(dbgs() << SJ << " "); 03456 if (Pair[SJ].Classification == Subscript::SIV) 03457 Sivs.set(SJ); 03458 else 03459 Mivs.set(SJ); 03460 } 03461 DEBUG(dbgs() << "}\n"); 03462 while (Sivs.any()) { 03463 bool Changed = false; 03464 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) { 03465 DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n"); 03466 // SJ is an SIV subscript that's part of the current coupled group 03467 unsigned Level; 03468 const SCEV *SplitIter = NULL; 03469 DEBUG(dbgs() << "SIV\n"); 03470 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, 03471 Result, NewConstraint, SplitIter)) 03472 return NULL; 03473 ConstrainedLevels.set(Level); 03474 if (intersectConstraints(&Constraints[Level], &NewConstraint)) { 03475 if (Constraints[Level].isEmpty()) { 03476 ++DeltaIndependence; 03477 return NULL; 03478 } 03479 Changed = true; 03480 } 03481 Sivs.reset(SJ); 03482 } 03483 if (Changed) { 03484 // propagate, possibly creating new SIVs and ZIVs 03485 DEBUG(dbgs() << " propagating\n"); 03486 DEBUG(dbgs() << "\tMivs = "); 03487 DEBUG(dumpSmallBitVector(Mivs)); 03488 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 03489 // SJ is an MIV subscript that's part of the current coupled group 03490 DEBUG(dbgs() << "\tSJ = " << SJ << "\n"); 03491 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, 03492 Constraints, Result.Consistent)) { 03493 DEBUG(dbgs() << "\t Changed\n"); 03494 ++DeltaPropagations; 03495 Pair[SJ].Classification = 03496 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()), 03497 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()), 03498 Pair[SJ].Loops); 03499 switch (Pair[SJ].Classification) { 03500 case Subscript::ZIV: 03501 DEBUG(dbgs() << "ZIV\n"); 03502 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result)) 03503 return NULL; 03504 Mivs.reset(SJ); 03505 break; 03506 case Subscript::SIV: 03507 Sivs.set(SJ); 03508 Mivs.reset(SJ); 03509 break; 03510 case Subscript::RDIV: 03511 case Subscript::MIV: 03512 break; 03513 default: 03514 llvm_unreachable("bad subscript classification"); 03515 } 03516 } 03517 } 03518 } 03519 } 03520 03521 // test & propagate remaining RDIVs 03522 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 03523 if (Pair[SJ].Classification == Subscript::RDIV) { 03524 DEBUG(dbgs() << "RDIV test\n"); 03525 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result)) 03526 return NULL; 03527 // I don't yet understand how to propagate RDIV results 03528 Mivs.reset(SJ); 03529 } 03530 } 03531 03532 // test remaining MIVs 03533 // This code is temporary. 03534 // Better to somehow test all remaining subscripts simultaneously. 03535 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 03536 if (Pair[SJ].Classification == Subscript::MIV) { 03537 DEBUG(dbgs() << "MIV test\n"); 03538 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result)) 03539 return NULL; 03540 } 03541 else 03542 llvm_unreachable("expected only MIV subscripts at this point"); 03543 } 03544 03545 // update Result.DV from constraint vector 03546 DEBUG(dbgs() << " updating\n"); 03547 for (int SJ = ConstrainedLevels.find_first(); 03548 SJ >= 0; SJ = ConstrainedLevels.find_next(SJ)) { 03549 updateDirection(Result.DV[SJ - 1], Constraints[SJ]); 03550 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE) 03551 return NULL; 03552 } 03553 } 03554 } 03555 03556 // Make sure the Scalar flags are set correctly. 03557 SmallBitVector CompleteLoops(MaxLevels + 1); 03558 for (unsigned SI = 0; SI < Pairs; ++SI) 03559 CompleteLoops |= Pair[SI].Loops; 03560 for (unsigned II = 1; II <= CommonLevels; ++II) 03561 if (CompleteLoops[II]) 03562 Result.DV[II - 1].Scalar = false; 03563 03564 if (PossiblyLoopIndependent) { 03565 // Make sure the LoopIndependent flag is set correctly. 03566 // All directions must include equal, otherwise no 03567 // loop-independent dependence is possible. 03568 for (unsigned II = 1; II <= CommonLevels; ++II) { 03569 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) { 03570 Result.LoopIndependent = false; 03571 break; 03572 } 03573 } 03574 } 03575 else { 03576 // On the other hand, if all directions are equal and there's no 03577 // loop-independent dependence possible, then no dependence exists. 03578 bool AllEqual = true; 03579 for (unsigned II = 1; II <= CommonLevels; ++II) { 03580 if (Result.getDirection(II) != Dependence::DVEntry::EQ) { 03581 AllEqual = false; 03582 break; 03583 } 03584 } 03585 if (AllEqual) 03586 return NULL; 03587 } 03588 03589 FullDependence *Final = new FullDependence(Result); 03590 Result.DV = NULL; 03591 return Final; 03592 } 03593 03594 03595 03596 //===----------------------------------------------------------------------===// 03597 // getSplitIteration - 03598 // Rather than spend rarely-used space recording the splitting iteration 03599 // during the Weak-Crossing SIV test, we re-compute it on demand. 03600 // The re-computation is basically a repeat of the entire dependence test, 03601 // though simplified since we know that the dependence exists. 03602 // It's tedious, since we must go through all propagations, etc. 03603 // 03604 // Care is required to keep this code up to date with respect to the routine 03605 // above, depends(). 03606 // 03607 // Generally, the dependence analyzer will be used to build 03608 // a dependence graph for a function (basically a map from instructions 03609 // to dependences). Looking for cycles in the graph shows us loops 03610 // that cannot be trivially vectorized/parallelized. 03611 // 03612 // We can try to improve the situation by examining all the dependences 03613 // that make up the cycle, looking for ones we can break. 03614 // Sometimes, peeling the first or last iteration of a loop will break 03615 // dependences, and we've got flags for those possibilities. 03616 // Sometimes, splitting a loop at some other iteration will do the trick, 03617 // and we've got a flag for that case. Rather than waste the space to 03618 // record the exact iteration (since we rarely know), we provide 03619 // a method that calculates the iteration. It's a drag that it must work 03620 // from scratch, but wonderful in that it's possible. 03621 // 03622 // Here's an example: 03623 // 03624 // for (i = 0; i < 10; i++) 03625 // A[i] = ... 03626 // ... = A[11 - i] 03627 // 03628 // There's a loop-carried flow dependence from the store to the load, 03629 // found by the weak-crossing SIV test. The dependence will have a flag, 03630 // indicating that the dependence can be broken by splitting the loop. 03631 // Calling getSplitIteration will return 5. 03632 // Splitting the loop breaks the dependence, like so: 03633 // 03634 // for (i = 0; i <= 5; i++) 03635 // A[i] = ... 03636 // ... = A[11 - i] 03637 // for (i = 6; i < 10; i++) 03638 // A[i] = ... 03639 // ... = A[11 - i] 03640 // 03641 // breaks the dependence and allows us to vectorize/parallelize 03642 // both loops. 03643 const SCEV *DependenceAnalysis::getSplitIteration(const Dependence *Dep, 03644 unsigned SplitLevel) { 03645 assert(Dep && "expected a pointer to a Dependence"); 03646 assert(Dep->isSplitable(SplitLevel) && 03647 "Dep should be splitable at SplitLevel"); 03648 Instruction *Src = Dep->getSrc(); 03649 Instruction *Dst = Dep->getDst(); 03650 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory()); 03651 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory()); 03652 assert(isLoadOrStore(Src)); 03653 assert(isLoadOrStore(Dst)); 03654 Value *SrcPtr = getPointerOperand(Src); 03655 Value *DstPtr = getPointerOperand(Dst); 03656 assert(underlyingObjectsAlias(AA, DstPtr, SrcPtr) == 03657 AliasAnalysis::MustAlias); 03658 03659 // establish loop nesting levels 03660 establishNestingLevels(Src, Dst); 03661 03662 FullDependence Result(Src, Dst, false, CommonLevels); 03663 03664 // See if there are GEPs we can use. 03665 bool UsefulGEP = false; 03666 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr); 03667 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr); 03668 if (SrcGEP && DstGEP && 03669 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) { 03670 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand()); 03671 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand()); 03672 UsefulGEP = 03673 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) && 03674 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())); 03675 } 03676 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1; 03677 SmallVector<Subscript, 4> Pair(Pairs); 03678 if (UsefulGEP) { 03679 unsigned P = 0; 03680 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(), 03681 SrcEnd = SrcGEP->idx_end(), 03682 DstIdx = DstGEP->idx_begin(); 03683 SrcIdx != SrcEnd; 03684 ++SrcIdx, ++DstIdx, ++P) { 03685 Pair[P].Src = SE->getSCEV(*SrcIdx); 03686 Pair[P].Dst = SE->getSCEV(*DstIdx); 03687 } 03688 } 03689 else { 03690 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr); 03691 const SCEV *DstSCEV = SE->getSCEV(DstPtr); 03692 Pair[0].Src = SrcSCEV; 03693 Pair[0].Dst = DstSCEV; 03694 } 03695 03696 for (unsigned P = 0; P < Pairs; ++P) { 03697 Pair[P].Loops.resize(MaxLevels + 1); 03698 Pair[P].GroupLoops.resize(MaxLevels + 1); 03699 Pair[P].Group.resize(Pairs); 03700 removeMatchingExtensions(&Pair[P]); 03701 Pair[P].Classification = 03702 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), 03703 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), 03704 Pair[P].Loops); 03705 Pair[P].GroupLoops = Pair[P].Loops; 03706 Pair[P].Group.set(P); 03707 } 03708 03709 SmallBitVector Separable(Pairs); 03710 SmallBitVector Coupled(Pairs); 03711 03712 // partition subscripts into separable and minimally-coupled groups 03713 for (unsigned SI = 0; SI < Pairs; ++SI) { 03714 if (Pair[SI].Classification == Subscript::NonLinear) { 03715 // ignore these, but collect loops for later 03716 collectCommonLoops(Pair[SI].Src, 03717 LI->getLoopFor(Src->getParent()), 03718 Pair[SI].Loops); 03719 collectCommonLoops(Pair[SI].Dst, 03720 LI->getLoopFor(Dst->getParent()), 03721 Pair[SI].Loops); 03722 Result.Consistent = false; 03723 } 03724 else if (Pair[SI].Classification == Subscript::ZIV) 03725 Separable.set(SI); 03726 else { 03727 // SIV, RDIV, or MIV, so check for coupled group 03728 bool Done = true; 03729 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) { 03730 SmallBitVector Intersection = Pair[SI].GroupLoops; 03731 Intersection &= Pair[SJ].GroupLoops; 03732 if (Intersection.any()) { 03733 // accumulate set of all the loops in group 03734 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops; 03735 // accumulate set of all subscripts in group 03736 Pair[SJ].Group |= Pair[SI].Group; 03737 Done = false; 03738 } 03739 } 03740 if (Done) { 03741 if (Pair[SI].Group.count() == 1) 03742 Separable.set(SI); 03743 else 03744 Coupled.set(SI); 03745 } 03746 } 03747 } 03748 03749 Constraint NewConstraint; 03750 NewConstraint.setAny(SE); 03751 03752 // test separable subscripts 03753 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) { 03754 switch (Pair[SI].Classification) { 03755 case Subscript::SIV: { 03756 unsigned Level; 03757 const SCEV *SplitIter = NULL; 03758 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level, 03759 Result, NewConstraint, SplitIter); 03760 if (Level == SplitLevel) { 03761 assert(SplitIter != NULL); 03762 return SplitIter; 03763 } 03764 break; 03765 } 03766 case Subscript::ZIV: 03767 case Subscript::RDIV: 03768 case Subscript::MIV: 03769 break; 03770 default: 03771 llvm_unreachable("subscript has unexpected classification"); 03772 } 03773 } 03774 03775 if (Coupled.count()) { 03776 // test coupled subscript groups 03777 SmallVector<Constraint, 4> Constraints(MaxLevels + 1); 03778 for (unsigned II = 0; II <= MaxLevels; ++II) 03779 Constraints[II].setAny(SE); 03780 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) { 03781 SmallBitVector Group(Pair[SI].Group); 03782 SmallBitVector Sivs(Pairs); 03783 SmallBitVector Mivs(Pairs); 03784 SmallBitVector ConstrainedLevels(MaxLevels + 1); 03785 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) { 03786 if (Pair[SJ].Classification == Subscript::SIV) 03787 Sivs.set(SJ); 03788 else 03789 Mivs.set(SJ); 03790 } 03791 while (Sivs.any()) { 03792 bool Changed = false; 03793 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) { 03794 // SJ is an SIV subscript that's part of the current coupled group 03795 unsigned Level; 03796 const SCEV *SplitIter = NULL; 03797 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, 03798 Result, NewConstraint, SplitIter); 03799 if (Level == SplitLevel && SplitIter) 03800 return SplitIter; 03801 ConstrainedLevels.set(Level); 03802 if (intersectConstraints(&Constraints[Level], &NewConstraint)) 03803 Changed = true; 03804 Sivs.reset(SJ); 03805 } 03806 if (Changed) { 03807 // propagate, possibly creating new SIVs and ZIVs 03808 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 03809 // SJ is an MIV subscript that's part of the current coupled group 03810 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, 03811 Pair[SJ].Loops, Constraints, Result.Consistent)) { 03812 Pair[SJ].Classification = 03813 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()), 03814 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()), 03815 Pair[SJ].Loops); 03816 switch (Pair[SJ].Classification) { 03817 case Subscript::ZIV: 03818 Mivs.reset(SJ); 03819 break; 03820 case Subscript::SIV: 03821 Sivs.set(SJ); 03822 Mivs.reset(SJ); 03823 break; 03824 case Subscript::RDIV: 03825 case Subscript::MIV: 03826 break; 03827 default: 03828 llvm_unreachable("bad subscript classification"); 03829 } 03830 } 03831 } 03832 } 03833 } 03834 } 03835 } 03836 llvm_unreachable("somehow reached end of routine"); 03837 return NULL; 03838 }