1 //===-- DependenceAnalysis.cpp - DA Implementation --------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // DependenceAnalysis is an LLVM pass that analyses dependences between memory 11 // accesses. Currently, it is an (incomplete) implementation of the approach 12 // described in 13 // 14 // Practical Dependence Testing 15 // Goff, Kennedy, Tseng 16 // PLDI 1991 17 // 18 // There's a single entry point that analyzes the dependence between a pair 19 // of memory references in a function, returning either NULL, for no dependence, 20 // or a more-or-less detailed description of the dependence between them. 21 // 22 // Currently, the implementation cannot propagate constraints between 23 // coupled RDIV subscripts and lacks a multi-subscript MIV test. 24 // Both of these are conservative weaknesses; 25 // that is, not a source of correctness problems. 26 // 27 // The implementation depends on the GEP instruction to differentiate 28 // subscripts. Since Clang linearizes some array subscripts, the dependence 29 // analysis is using SCEV->delinearize to recover the representation of multiple 30 // subscripts, and thus avoid the more expensive and less precise MIV tests. The 31 // delinearization is controlled by the flag -da-delinearize. 32 // 33 // We should pay some careful attention to the possibility of integer overflow 34 // in the implementation of the various tests. This could happen with Add, 35 // Subtract, or Multiply, with both APInt's and SCEV's. 36 // 37 // Some non-linear subscript pairs can be handled by the GCD test 38 // (and perhaps other tests). 39 // Should explore how often these things occur. 40 // 41 // Finally, it seems like certain test cases expose weaknesses in the SCEV 42 // simplification, especially in the handling of sign and zero extensions. 43 // It could be useful to spend time exploring these. 44 // 45 // Please note that this is work in progress and the interface is subject to 46 // change. 47 // 48 //===----------------------------------------------------------------------===// 49 // // 50 // In memory of Ken Kennedy, 1945 - 2007 // 51 // // 52 //===----------------------------------------------------------------------===// 53 54 #include "llvm/Analysis/DependenceAnalysis.h" 55 #include "llvm/ADT/Statistic.h" 56 #include "llvm/Analysis/AliasAnalysis.h" 57 #include "llvm/Analysis/LoopInfo.h" 58 #include "llvm/Analysis/ScalarEvolution.h" 59 #include "llvm/Analysis/ScalarEvolutionExpressions.h" 60 #include "llvm/Analysis/ValueTracking.h" 61 #include "llvm/IR/InstIterator.h" 62 #include "llvm/IR/Operator.h" 63 #include "llvm/Support/CommandLine.h" 64 #include "llvm/Support/Debug.h" 65 #include "llvm/Support/ErrorHandling.h" 66 #include "llvm/Support/raw_ostream.h" 67 68 using namespace llvm; 69 70 #define DEBUG_TYPE "da" 71 72 //===----------------------------------------------------------------------===// 73 // statistics 74 75 STATISTIC(TotalArrayPairs, "Array pairs tested"); 76 STATISTIC(SeparableSubscriptPairs, "Separable subscript pairs"); 77 STATISTIC(CoupledSubscriptPairs, "Coupled subscript pairs"); 78 STATISTIC(NonlinearSubscriptPairs, "Nonlinear subscript pairs"); 79 STATISTIC(ZIVapplications, "ZIV applications"); 80 STATISTIC(ZIVindependence, "ZIV independence"); 81 STATISTIC(StrongSIVapplications, "Strong SIV applications"); 82 STATISTIC(StrongSIVsuccesses, "Strong SIV successes"); 83 STATISTIC(StrongSIVindependence, "Strong SIV independence"); 84 STATISTIC(WeakCrossingSIVapplications, "Weak-Crossing SIV applications"); 85 STATISTIC(WeakCrossingSIVsuccesses, "Weak-Crossing SIV successes"); 86 STATISTIC(WeakCrossingSIVindependence, "Weak-Crossing SIV independence"); 87 STATISTIC(ExactSIVapplications, "Exact SIV applications"); 88 STATISTIC(ExactSIVsuccesses, "Exact SIV successes"); 89 STATISTIC(ExactSIVindependence, "Exact SIV independence"); 90 STATISTIC(WeakZeroSIVapplications, "Weak-Zero SIV applications"); 91 STATISTIC(WeakZeroSIVsuccesses, "Weak-Zero SIV successes"); 92 STATISTIC(WeakZeroSIVindependence, "Weak-Zero SIV independence"); 93 STATISTIC(ExactRDIVapplications, "Exact RDIV applications"); 94 STATISTIC(ExactRDIVindependence, "Exact RDIV independence"); 95 STATISTIC(SymbolicRDIVapplications, "Symbolic RDIV applications"); 96 STATISTIC(SymbolicRDIVindependence, "Symbolic RDIV independence"); 97 STATISTIC(DeltaApplications, "Delta applications"); 98 STATISTIC(DeltaSuccesses, "Delta successes"); 99 STATISTIC(DeltaIndependence, "Delta independence"); 100 STATISTIC(DeltaPropagations, "Delta propagations"); 101 STATISTIC(GCDapplications, "GCD applications"); 102 STATISTIC(GCDsuccesses, "GCD successes"); 103 STATISTIC(GCDindependence, "GCD independence"); 104 STATISTIC(BanerjeeApplications, "Banerjee applications"); 105 STATISTIC(BanerjeeIndependence, "Banerjee independence"); 106 STATISTIC(BanerjeeSuccesses, "Banerjee successes"); 107 108 static cl::opt<bool> 109 Delinearize("da-delinearize", cl::init(false), cl::Hidden, cl::ZeroOrMore, 110 cl::desc("Try to delinearize array references.")); 111 112 //===----------------------------------------------------------------------===// 113 // basics 114 115 INITIALIZE_PASS_BEGIN(DependenceAnalysis, "da", 116 "Dependence Analysis", true, true) 117 INITIALIZE_PASS_DEPENDENCY(LoopInfo) 118 INITIALIZE_PASS_DEPENDENCY(ScalarEvolution) 119 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 120 INITIALIZE_PASS_END(DependenceAnalysis, "da", 121 "Dependence Analysis", true, true) 122 123 char DependenceAnalysis::ID = 0; 124 125 126 FunctionPass *llvm::createDependenceAnalysisPass() { 127 return new DependenceAnalysis(); 128 } 129 130 131 bool DependenceAnalysis::runOnFunction(Function &F) { 132 this->F = &F; 133 AA = &getAnalysis<AliasAnalysis>(); 134 SE = &getAnalysis<ScalarEvolution>(); 135 LI = &getAnalysis<LoopInfo>(); 136 return false; 137 } 138 139 140 void DependenceAnalysis::releaseMemory() { 141 } 142 143 144 void DependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const { 145 AU.setPreservesAll(); 146 AU.addRequiredTransitive<AliasAnalysis>(); 147 AU.addRequiredTransitive<ScalarEvolution>(); 148 AU.addRequiredTransitive<LoopInfo>(); 149 } 150 151 152 // Used to test the dependence analyzer. 153 // Looks through the function, noting loads and stores. 154 // Calls depends() on every possible pair and prints out the result. 155 // Ignores all other instructions. 156 static 157 void dumpExampleDependence(raw_ostream &OS, Function *F, 158 DependenceAnalysis *DA) { 159 for (inst_iterator SrcI = inst_begin(F), SrcE = inst_end(F); 160 SrcI != SrcE; ++SrcI) { 161 if (isa<StoreInst>(*SrcI) || isa<LoadInst>(*SrcI)) { 162 for (inst_iterator DstI = SrcI, DstE = inst_end(F); 163 DstI != DstE; ++DstI) { 164 if (isa<StoreInst>(*DstI) || isa<LoadInst>(*DstI)) { 165 OS << "da analyze - "; 166 if (auto D = DA->depends(&*SrcI, &*DstI, true)) { 167 D->dump(OS); 168 for (unsigned Level = 1; Level <= D->getLevels(); Level++) { 169 if (D->isSplitable(Level)) { 170 OS << "da analyze - split level = " << Level; 171 OS << ", iteration = " << *DA->getSplitIteration(*D, Level); 172 OS << "!\n"; 173 } 174 } 175 } 176 else 177 OS << "none!\n"; 178 } 179 } 180 } 181 } 182 } 183 184 185 void DependenceAnalysis::print(raw_ostream &OS, const Module*) const { 186 dumpExampleDependence(OS, F, const_cast<DependenceAnalysis *>(this)); 187 } 188 189 //===----------------------------------------------------------------------===// 190 // Dependence methods 191 192 // Returns true if this is an input dependence. 193 bool Dependence::isInput() const { 194 return Src->mayReadFromMemory() && Dst->mayReadFromMemory(); 195 } 196 197 198 // Returns true if this is an output dependence. 199 bool Dependence::isOutput() const { 200 return Src->mayWriteToMemory() && Dst->mayWriteToMemory(); 201 } 202 203 204 // Returns true if this is an flow (aka true) dependence. 205 bool Dependence::isFlow() const { 206 return Src->mayWriteToMemory() && Dst->mayReadFromMemory(); 207 } 208 209 210 // Returns true if this is an anti dependence. 211 bool Dependence::isAnti() const { 212 return Src->mayReadFromMemory() && Dst->mayWriteToMemory(); 213 } 214 215 216 // Returns true if a particular level is scalar; that is, 217 // if no subscript in the source or destination mention the induction 218 // variable associated with the loop at this level. 219 // Leave this out of line, so it will serve as a virtual method anchor 220 bool Dependence::isScalar(unsigned level) const { 221 return false; 222 } 223 224 225 //===----------------------------------------------------------------------===// 226 // FullDependence methods 227 228 FullDependence::FullDependence(Instruction *Source, 229 Instruction *Destination, 230 bool PossiblyLoopIndependent, 231 unsigned CommonLevels) : 232 Dependence(Source, Destination), 233 Levels(CommonLevels), 234 LoopIndependent(PossiblyLoopIndependent) { 235 Consistent = true; 236 DV = CommonLevels ? new DVEntry[CommonLevels] : nullptr; 237 } 238 239 // The rest are simple getters that hide the implementation. 240 241 // getDirection - Returns the direction associated with a particular level. 242 unsigned FullDependence::getDirection(unsigned Level) const { 243 assert(0 < Level && Level <= Levels && "Level out of range"); 244 return DV[Level - 1].Direction; 245 } 246 247 248 // Returns the distance (or NULL) associated with a particular level. 249 const SCEV *FullDependence::getDistance(unsigned Level) const { 250 assert(0 < Level && Level <= Levels && "Level out of range"); 251 return DV[Level - 1].Distance; 252 } 253 254 255 // Returns true if a particular level is scalar; that is, 256 // if no subscript in the source or destination mention the induction 257 // variable associated with the loop at this level. 258 bool FullDependence::isScalar(unsigned Level) const { 259 assert(0 < Level && Level <= Levels && "Level out of range"); 260 return DV[Level - 1].Scalar; 261 } 262 263 264 // Returns true if peeling the first iteration from this loop 265 // will break this dependence. 266 bool FullDependence::isPeelFirst(unsigned Level) const { 267 assert(0 < Level && Level <= Levels && "Level out of range"); 268 return DV[Level - 1].PeelFirst; 269 } 270 271 272 // Returns true if peeling the last iteration from this loop 273 // will break this dependence. 274 bool FullDependence::isPeelLast(unsigned Level) const { 275 assert(0 < Level && Level <= Levels && "Level out of range"); 276 return DV[Level - 1].PeelLast; 277 } 278 279 280 // Returns true if splitting this loop will break the dependence. 281 bool FullDependence::isSplitable(unsigned Level) const { 282 assert(0 < Level && Level <= Levels && "Level out of range"); 283 return DV[Level - 1].Splitable; 284 } 285 286 287 //===----------------------------------------------------------------------===// 288 // DependenceAnalysis::Constraint methods 289 290 // If constraint is a point <X, Y>, returns X. 291 // Otherwise assert. 292 const SCEV *DependenceAnalysis::Constraint::getX() const { 293 assert(Kind == Point && "Kind should be Point"); 294 return A; 295 } 296 297 298 // If constraint is a point <X, Y>, returns Y. 299 // Otherwise assert. 300 const SCEV *DependenceAnalysis::Constraint::getY() const { 301 assert(Kind == Point && "Kind should be Point"); 302 return B; 303 } 304 305 306 // If constraint is a line AX + BY = C, returns A. 307 // Otherwise assert. 308 const SCEV *DependenceAnalysis::Constraint::getA() const { 309 assert((Kind == Line || Kind == Distance) && 310 "Kind should be Line (or Distance)"); 311 return A; 312 } 313 314 315 // If constraint is a line AX + BY = C, returns B. 316 // Otherwise assert. 317 const SCEV *DependenceAnalysis::Constraint::getB() const { 318 assert((Kind == Line || Kind == Distance) && 319 "Kind should be Line (or Distance)"); 320 return B; 321 } 322 323 324 // If constraint is a line AX + BY = C, returns C. 325 // Otherwise assert. 326 const SCEV *DependenceAnalysis::Constraint::getC() const { 327 assert((Kind == Line || Kind == Distance) && 328 "Kind should be Line (or Distance)"); 329 return C; 330 } 331 332 333 // If constraint is a distance, returns D. 334 // Otherwise assert. 335 const SCEV *DependenceAnalysis::Constraint::getD() const { 336 assert(Kind == Distance && "Kind should be Distance"); 337 return SE->getNegativeSCEV(C); 338 } 339 340 341 // Returns the loop associated with this constraint. 342 const Loop *DependenceAnalysis::Constraint::getAssociatedLoop() const { 343 assert((Kind == Distance || Kind == Line || Kind == Point) && 344 "Kind should be Distance, Line, or Point"); 345 return AssociatedLoop; 346 } 347 348 349 void DependenceAnalysis::Constraint::setPoint(const SCEV *X, 350 const SCEV *Y, 351 const Loop *CurLoop) { 352 Kind = Point; 353 A = X; 354 B = Y; 355 AssociatedLoop = CurLoop; 356 } 357 358 359 void DependenceAnalysis::Constraint::setLine(const SCEV *AA, 360 const SCEV *BB, 361 const SCEV *CC, 362 const Loop *CurLoop) { 363 Kind = Line; 364 A = AA; 365 B = BB; 366 C = CC; 367 AssociatedLoop = CurLoop; 368 } 369 370 371 void DependenceAnalysis::Constraint::setDistance(const SCEV *D, 372 const Loop *CurLoop) { 373 Kind = Distance; 374 A = SE->getConstant(D->getType(), 1); 375 B = SE->getNegativeSCEV(A); 376 C = SE->getNegativeSCEV(D); 377 AssociatedLoop = CurLoop; 378 } 379 380 381 void DependenceAnalysis::Constraint::setEmpty() { 382 Kind = Empty; 383 } 384 385 386 void DependenceAnalysis::Constraint::setAny(ScalarEvolution *NewSE) { 387 SE = NewSE; 388 Kind = Any; 389 } 390 391 392 // For debugging purposes. Dumps the constraint out to OS. 393 void DependenceAnalysis::Constraint::dump(raw_ostream &OS) const { 394 if (isEmpty()) 395 OS << " Empty\n"; 396 else if (isAny()) 397 OS << " Any\n"; 398 else if (isPoint()) 399 OS << " Point is <" << *getX() << ", " << *getY() << ">\n"; 400 else if (isDistance()) 401 OS << " Distance is " << *getD() << 402 " (" << *getA() << "*X + " << *getB() << "*Y = " << *getC() << ")\n"; 403 else if (isLine()) 404 OS << " Line is " << *getA() << "*X + " << 405 *getB() << "*Y = " << *getC() << "\n"; 406 else 407 llvm_unreachable("unknown constraint type in Constraint::dump"); 408 } 409 410 411 // Updates X with the intersection 412 // of the Constraints X and Y. Returns true if X has changed. 413 // Corresponds to Figure 4 from the paper 414 // 415 // Practical Dependence Testing 416 // Goff, Kennedy, Tseng 417 // PLDI 1991 418 bool DependenceAnalysis::intersectConstraints(Constraint *X, 419 const Constraint *Y) { 420 ++DeltaApplications; 421 DEBUG(dbgs() << "\tintersect constraints\n"); 422 DEBUG(dbgs() << "\t X ="; X->dump(dbgs())); 423 DEBUG(dbgs() << "\t Y ="; Y->dump(dbgs())); 424 assert(!Y->isPoint() && "Y must not be a Point"); 425 if (X->isAny()) { 426 if (Y->isAny()) 427 return false; 428 *X = *Y; 429 return true; 430 } 431 if (X->isEmpty()) 432 return false; 433 if (Y->isEmpty()) { 434 X->setEmpty(); 435 return true; 436 } 437 438 if (X->isDistance() && Y->isDistance()) { 439 DEBUG(dbgs() << "\t intersect 2 distances\n"); 440 if (isKnownPredicate(CmpInst::ICMP_EQ, X->getD(), Y->getD())) 441 return false; 442 if (isKnownPredicate(CmpInst::ICMP_NE, X->getD(), Y->getD())) { 443 X->setEmpty(); 444 ++DeltaSuccesses; 445 return true; 446 } 447 // Hmmm, interesting situation. 448 // I guess if either is constant, keep it and ignore the other. 449 if (isa<SCEVConstant>(Y->getD())) { 450 *X = *Y; 451 return true; 452 } 453 return false; 454 } 455 456 // At this point, the pseudo-code in Figure 4 of the paper 457 // checks if (X->isPoint() && Y->isPoint()). 458 // This case can't occur in our implementation, 459 // since a Point can only arise as the result of intersecting 460 // two Line constraints, and the right-hand value, Y, is never 461 // the result of an intersection. 462 assert(!(X->isPoint() && Y->isPoint()) && 463 "We shouldn't ever see X->isPoint() && Y->isPoint()"); 464 465 if (X->isLine() && Y->isLine()) { 466 DEBUG(dbgs() << "\t intersect 2 lines\n"); 467 const SCEV *Prod1 = SE->getMulExpr(X->getA(), Y->getB()); 468 const SCEV *Prod2 = SE->getMulExpr(X->getB(), Y->getA()); 469 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) { 470 // slopes are equal, so lines are parallel 471 DEBUG(dbgs() << "\t\tsame slope\n"); 472 Prod1 = SE->getMulExpr(X->getC(), Y->getB()); 473 Prod2 = SE->getMulExpr(X->getB(), Y->getC()); 474 if (isKnownPredicate(CmpInst::ICMP_EQ, Prod1, Prod2)) 475 return false; 476 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) { 477 X->setEmpty(); 478 ++DeltaSuccesses; 479 return true; 480 } 481 return false; 482 } 483 if (isKnownPredicate(CmpInst::ICMP_NE, Prod1, Prod2)) { 484 // slopes differ, so lines intersect 485 DEBUG(dbgs() << "\t\tdifferent slopes\n"); 486 const SCEV *C1B2 = SE->getMulExpr(X->getC(), Y->getB()); 487 const SCEV *C1A2 = SE->getMulExpr(X->getC(), Y->getA()); 488 const SCEV *C2B1 = SE->getMulExpr(Y->getC(), X->getB()); 489 const SCEV *C2A1 = SE->getMulExpr(Y->getC(), X->getA()); 490 const SCEV *A1B2 = SE->getMulExpr(X->getA(), Y->getB()); 491 const SCEV *A2B1 = SE->getMulExpr(Y->getA(), X->getB()); 492 const SCEVConstant *C1A2_C2A1 = 493 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1A2, C2A1)); 494 const SCEVConstant *C1B2_C2B1 = 495 dyn_cast<SCEVConstant>(SE->getMinusSCEV(C1B2, C2B1)); 496 const SCEVConstant *A1B2_A2B1 = 497 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A1B2, A2B1)); 498 const SCEVConstant *A2B1_A1B2 = 499 dyn_cast<SCEVConstant>(SE->getMinusSCEV(A2B1, A1B2)); 500 if (!C1B2_C2B1 || !C1A2_C2A1 || 501 !A1B2_A2B1 || !A2B1_A1B2) 502 return false; 503 APInt Xtop = C1B2_C2B1->getValue()->getValue(); 504 APInt Xbot = A1B2_A2B1->getValue()->getValue(); 505 APInt Ytop = C1A2_C2A1->getValue()->getValue(); 506 APInt Ybot = A2B1_A1B2->getValue()->getValue(); 507 DEBUG(dbgs() << "\t\tXtop = " << Xtop << "\n"); 508 DEBUG(dbgs() << "\t\tXbot = " << Xbot << "\n"); 509 DEBUG(dbgs() << "\t\tYtop = " << Ytop << "\n"); 510 DEBUG(dbgs() << "\t\tYbot = " << Ybot << "\n"); 511 APInt Xq = Xtop; // these need to be initialized, even 512 APInt Xr = Xtop; // though they're just going to be overwritten 513 APInt::sdivrem(Xtop, Xbot, Xq, Xr); 514 APInt Yq = Ytop; 515 APInt Yr = Ytop; 516 APInt::sdivrem(Ytop, Ybot, Yq, Yr); 517 if (Xr != 0 || Yr != 0) { 518 X->setEmpty(); 519 ++DeltaSuccesses; 520 return true; 521 } 522 DEBUG(dbgs() << "\t\tX = " << Xq << ", Y = " << Yq << "\n"); 523 if (Xq.slt(0) || Yq.slt(0)) { 524 X->setEmpty(); 525 ++DeltaSuccesses; 526 return true; 527 } 528 if (const SCEVConstant *CUB = 529 collectConstantUpperBound(X->getAssociatedLoop(), Prod1->getType())) { 530 APInt UpperBound = CUB->getValue()->getValue(); 531 DEBUG(dbgs() << "\t\tupper bound = " << UpperBound << "\n"); 532 if (Xq.sgt(UpperBound) || Yq.sgt(UpperBound)) { 533 X->setEmpty(); 534 ++DeltaSuccesses; 535 return true; 536 } 537 } 538 X->setPoint(SE->getConstant(Xq), 539 SE->getConstant(Yq), 540 X->getAssociatedLoop()); 541 ++DeltaSuccesses; 542 return true; 543 } 544 return false; 545 } 546 547 // if (X->isLine() && Y->isPoint()) This case can't occur. 548 assert(!(X->isLine() && Y->isPoint()) && "This case should never occur"); 549 550 if (X->isPoint() && Y->isLine()) { 551 DEBUG(dbgs() << "\t intersect Point and Line\n"); 552 const SCEV *A1X1 = SE->getMulExpr(Y->getA(), X->getX()); 553 const SCEV *B1Y1 = SE->getMulExpr(Y->getB(), X->getY()); 554 const SCEV *Sum = SE->getAddExpr(A1X1, B1Y1); 555 if (isKnownPredicate(CmpInst::ICMP_EQ, Sum, Y->getC())) 556 return false; 557 if (isKnownPredicate(CmpInst::ICMP_NE, Sum, Y->getC())) { 558 X->setEmpty(); 559 ++DeltaSuccesses; 560 return true; 561 } 562 return false; 563 } 564 565 llvm_unreachable("shouldn't reach the end of Constraint intersection"); 566 return false; 567 } 568 569 570 //===----------------------------------------------------------------------===// 571 // DependenceAnalysis methods 572 573 // For debugging purposes. Dumps a dependence to OS. 574 void Dependence::dump(raw_ostream &OS) const { 575 bool Splitable = false; 576 if (isConfused()) 577 OS << "confused"; 578 else { 579 if (isConsistent()) 580 OS << "consistent "; 581 if (isFlow()) 582 OS << "flow"; 583 else if (isOutput()) 584 OS << "output"; 585 else if (isAnti()) 586 OS << "anti"; 587 else if (isInput()) 588 OS << "input"; 589 unsigned Levels = getLevels(); 590 OS << " ["; 591 for (unsigned II = 1; II <= Levels; ++II) { 592 if (isSplitable(II)) 593 Splitable = true; 594 if (isPeelFirst(II)) 595 OS << 'p'; 596 const SCEV *Distance = getDistance(II); 597 if (Distance) 598 OS << *Distance; 599 else if (isScalar(II)) 600 OS << "S"; 601 else { 602 unsigned Direction = getDirection(II); 603 if (Direction == DVEntry::ALL) 604 OS << "*"; 605 else { 606 if (Direction & DVEntry::LT) 607 OS << "<"; 608 if (Direction & DVEntry::EQ) 609 OS << "="; 610 if (Direction & DVEntry::GT) 611 OS << ">"; 612 } 613 } 614 if (isPeelLast(II)) 615 OS << 'p'; 616 if (II < Levels) 617 OS << " "; 618 } 619 if (isLoopIndependent()) 620 OS << "|<"; 621 OS << "]"; 622 if (Splitable) 623 OS << " splitable"; 624 } 625 OS << "!\n"; 626 } 627 628 629 630 static 631 AliasAnalysis::AliasResult underlyingObjectsAlias(AliasAnalysis *AA, 632 const Value *A, 633 const Value *B) { 634 const Value *AObj = GetUnderlyingObject(A); 635 const Value *BObj = GetUnderlyingObject(B); 636 return AA->alias(AObj, AA->getTypeStoreSize(AObj->getType()), 637 BObj, AA->getTypeStoreSize(BObj->getType())); 638 } 639 640 641 // Returns true if the load or store can be analyzed. Atomic and volatile 642 // operations have properties which this analysis does not understand. 643 static 644 bool isLoadOrStore(const Instruction *I) { 645 if (const LoadInst *LI = dyn_cast<LoadInst>(I)) 646 return LI->isUnordered(); 647 else if (const StoreInst *SI = dyn_cast<StoreInst>(I)) 648 return SI->isUnordered(); 649 return false; 650 } 651 652 653 static 654 Value *getPointerOperand(Instruction *I) { 655 if (LoadInst *LI = dyn_cast<LoadInst>(I)) 656 return LI->getPointerOperand(); 657 if (StoreInst *SI = dyn_cast<StoreInst>(I)) 658 return SI->getPointerOperand(); 659 llvm_unreachable("Value is not load or store instruction"); 660 return nullptr; 661 } 662 663 664 // Examines the loop nesting of the Src and Dst 665 // instructions and establishes their shared loops. Sets the variables 666 // CommonLevels, SrcLevels, and MaxLevels. 667 // The source and destination instructions needn't be contained in the same 668 // loop. The routine establishNestingLevels finds the level of most deeply 669 // nested loop that contains them both, CommonLevels. An instruction that's 670 // not contained in a loop is at level = 0. MaxLevels is equal to the level 671 // of the source plus the level of the destination, minus CommonLevels. 672 // This lets us allocate vectors MaxLevels in length, with room for every 673 // distinct loop referenced in both the source and destination subscripts. 674 // The variable SrcLevels is the nesting depth of the source instruction. 675 // It's used to help calculate distinct loops referenced by the destination. 676 // Here's the map from loops to levels: 677 // 0 - unused 678 // 1 - outermost common loop 679 // ... - other common loops 680 // CommonLevels - innermost common loop 681 // ... - loops containing Src but not Dst 682 // SrcLevels - innermost loop containing Src but not Dst 683 // ... - loops containing Dst but not Src 684 // MaxLevels - innermost loops containing Dst but not Src 685 // Consider the follow code fragment: 686 // for (a = ...) { 687 // for (b = ...) { 688 // for (c = ...) { 689 // for (d = ...) { 690 // A[] = ...; 691 // } 692 // } 693 // for (e = ...) { 694 // for (f = ...) { 695 // for (g = ...) { 696 // ... = A[]; 697 // } 698 // } 699 // } 700 // } 701 // } 702 // If we're looking at the possibility of a dependence between the store 703 // to A (the Src) and the load from A (the Dst), we'll note that they 704 // have 2 loops in common, so CommonLevels will equal 2 and the direction 705 // vector for Result will have 2 entries. SrcLevels = 4 and MaxLevels = 7. 706 // A map from loop names to loop numbers would look like 707 // a - 1 708 // b - 2 = CommonLevels 709 // c - 3 710 // d - 4 = SrcLevels 711 // e - 5 712 // f - 6 713 // g - 7 = MaxLevels 714 void DependenceAnalysis::establishNestingLevels(const Instruction *Src, 715 const Instruction *Dst) { 716 const BasicBlock *SrcBlock = Src->getParent(); 717 const BasicBlock *DstBlock = Dst->getParent(); 718 unsigned SrcLevel = LI->getLoopDepth(SrcBlock); 719 unsigned DstLevel = LI->getLoopDepth(DstBlock); 720 const Loop *SrcLoop = LI->getLoopFor(SrcBlock); 721 const Loop *DstLoop = LI->getLoopFor(DstBlock); 722 SrcLevels = SrcLevel; 723 MaxLevels = SrcLevel + DstLevel; 724 while (SrcLevel > DstLevel) { 725 SrcLoop = SrcLoop->getParentLoop(); 726 SrcLevel--; 727 } 728 while (DstLevel > SrcLevel) { 729 DstLoop = DstLoop->getParentLoop(); 730 DstLevel--; 731 } 732 while (SrcLoop != DstLoop) { 733 SrcLoop = SrcLoop->getParentLoop(); 734 DstLoop = DstLoop->getParentLoop(); 735 SrcLevel--; 736 } 737 CommonLevels = SrcLevel; 738 MaxLevels -= CommonLevels; 739 } 740 741 742 // Given one of the loops containing the source, return 743 // its level index in our numbering scheme. 744 unsigned DependenceAnalysis::mapSrcLoop(const Loop *SrcLoop) const { 745 return SrcLoop->getLoopDepth(); 746 } 747 748 749 // Given one of the loops containing the destination, 750 // return its level index in our numbering scheme. 751 unsigned DependenceAnalysis::mapDstLoop(const Loop *DstLoop) const { 752 unsigned D = DstLoop->getLoopDepth(); 753 if (D > CommonLevels) 754 return D - CommonLevels + SrcLevels; 755 else 756 return D; 757 } 758 759 760 // Returns true if Expression is loop invariant in LoopNest. 761 bool DependenceAnalysis::isLoopInvariant(const SCEV *Expression, 762 const Loop *LoopNest) const { 763 if (!LoopNest) 764 return true; 765 return SE->isLoopInvariant(Expression, LoopNest) && 766 isLoopInvariant(Expression, LoopNest->getParentLoop()); 767 } 768 769 770 771 // Finds the set of loops from the LoopNest that 772 // have a level <= CommonLevels and are referred to by the SCEV Expression. 773 void DependenceAnalysis::collectCommonLoops(const SCEV *Expression, 774 const Loop *LoopNest, 775 SmallBitVector &Loops) const { 776 while (LoopNest) { 777 unsigned Level = LoopNest->getLoopDepth(); 778 if (Level <= CommonLevels && !SE->isLoopInvariant(Expression, LoopNest)) 779 Loops.set(Level); 780 LoopNest = LoopNest->getParentLoop(); 781 } 782 } 783 784 785 // removeMatchingExtensions - Examines a subscript pair. 786 // If the source and destination are identically sign (or zero) 787 // extended, it strips off the extension in an effect to simplify 788 // the actual analysis. 789 void DependenceAnalysis::removeMatchingExtensions(Subscript *Pair) { 790 const SCEV *Src = Pair->Src; 791 const SCEV *Dst = Pair->Dst; 792 if ((isa<SCEVZeroExtendExpr>(Src) && isa<SCEVZeroExtendExpr>(Dst)) || 793 (isa<SCEVSignExtendExpr>(Src) && isa<SCEVSignExtendExpr>(Dst))) { 794 const SCEVCastExpr *SrcCast = cast<SCEVCastExpr>(Src); 795 const SCEVCastExpr *DstCast = cast<SCEVCastExpr>(Dst); 796 if (SrcCast->getType() == DstCast->getType()) { 797 Pair->Src = SrcCast->getOperand(); 798 Pair->Dst = DstCast->getOperand(); 799 } 800 } 801 } 802 803 804 // Examine the scev and return true iff it's linear. 805 // Collect any loops mentioned in the set of "Loops". 806 bool DependenceAnalysis::checkSrcSubscript(const SCEV *Src, 807 const Loop *LoopNest, 808 SmallBitVector &Loops) { 809 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Src); 810 if (!AddRec) 811 return isLoopInvariant(Src, LoopNest); 812 const SCEV *Start = AddRec->getStart(); 813 const SCEV *Step = AddRec->getStepRecurrence(*SE); 814 if (!isLoopInvariant(Step, LoopNest)) 815 return false; 816 Loops.set(mapSrcLoop(AddRec->getLoop())); 817 return checkSrcSubscript(Start, LoopNest, Loops); 818 } 819 820 821 822 // Examine the scev and return true iff it's linear. 823 // Collect any loops mentioned in the set of "Loops". 824 bool DependenceAnalysis::checkDstSubscript(const SCEV *Dst, 825 const Loop *LoopNest, 826 SmallBitVector &Loops) { 827 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Dst); 828 if (!AddRec) 829 return isLoopInvariant(Dst, LoopNest); 830 const SCEV *Start = AddRec->getStart(); 831 const SCEV *Step = AddRec->getStepRecurrence(*SE); 832 if (!isLoopInvariant(Step, LoopNest)) 833 return false; 834 Loops.set(mapDstLoop(AddRec->getLoop())); 835 return checkDstSubscript(Start, LoopNest, Loops); 836 } 837 838 839 // Examines the subscript pair (the Src and Dst SCEVs) 840 // and classifies it as either ZIV, SIV, RDIV, MIV, or Nonlinear. 841 // Collects the associated loops in a set. 842 DependenceAnalysis::Subscript::ClassificationKind 843 DependenceAnalysis::classifyPair(const SCEV *Src, const Loop *SrcLoopNest, 844 const SCEV *Dst, const Loop *DstLoopNest, 845 SmallBitVector &Loops) { 846 SmallBitVector SrcLoops(MaxLevels + 1); 847 SmallBitVector DstLoops(MaxLevels + 1); 848 if (!checkSrcSubscript(Src, SrcLoopNest, SrcLoops)) 849 return Subscript::NonLinear; 850 if (!checkDstSubscript(Dst, DstLoopNest, DstLoops)) 851 return Subscript::NonLinear; 852 Loops = SrcLoops; 853 Loops |= DstLoops; 854 unsigned N = Loops.count(); 855 if (N == 0) 856 return Subscript::ZIV; 857 if (N == 1) 858 return Subscript::SIV; 859 if (N == 2 && (SrcLoops.count() == 0 || 860 DstLoops.count() == 0 || 861 (SrcLoops.count() == 1 && DstLoops.count() == 1))) 862 return Subscript::RDIV; 863 return Subscript::MIV; 864 } 865 866 867 // A wrapper around SCEV::isKnownPredicate. 868 // Looks for cases where we're interested in comparing for equality. 869 // If both X and Y have been identically sign or zero extended, 870 // it strips off the (confusing) extensions before invoking 871 // SCEV::isKnownPredicate. Perhaps, someday, the ScalarEvolution package 872 // will be similarly updated. 873 // 874 // If SCEV::isKnownPredicate can't prove the predicate, 875 // we try simple subtraction, which seems to help in some cases 876 // involving symbolics. 877 bool DependenceAnalysis::isKnownPredicate(ICmpInst::Predicate Pred, 878 const SCEV *X, 879 const SCEV *Y) const { 880 if (Pred == CmpInst::ICMP_EQ || 881 Pred == CmpInst::ICMP_NE) { 882 if ((isa<SCEVSignExtendExpr>(X) && 883 isa<SCEVSignExtendExpr>(Y)) || 884 (isa<SCEVZeroExtendExpr>(X) && 885 isa<SCEVZeroExtendExpr>(Y))) { 886 const SCEVCastExpr *CX = cast<SCEVCastExpr>(X); 887 const SCEVCastExpr *CY = cast<SCEVCastExpr>(Y); 888 const SCEV *Xop = CX->getOperand(); 889 const SCEV *Yop = CY->getOperand(); 890 if (Xop->getType() == Yop->getType()) { 891 X = Xop; 892 Y = Yop; 893 } 894 } 895 } 896 if (SE->isKnownPredicate(Pred, X, Y)) 897 return true; 898 // If SE->isKnownPredicate can't prove the condition, 899 // we try the brute-force approach of subtracting 900 // and testing the difference. 901 // By testing with SE->isKnownPredicate first, we avoid 902 // the possibility of overflow when the arguments are constants. 903 const SCEV *Delta = SE->getMinusSCEV(X, Y); 904 switch (Pred) { 905 case CmpInst::ICMP_EQ: 906 return Delta->isZero(); 907 case CmpInst::ICMP_NE: 908 return SE->isKnownNonZero(Delta); 909 case CmpInst::ICMP_SGE: 910 return SE->isKnownNonNegative(Delta); 911 case CmpInst::ICMP_SLE: 912 return SE->isKnownNonPositive(Delta); 913 case CmpInst::ICMP_SGT: 914 return SE->isKnownPositive(Delta); 915 case CmpInst::ICMP_SLT: 916 return SE->isKnownNegative(Delta); 917 default: 918 llvm_unreachable("unexpected predicate in isKnownPredicate"); 919 } 920 } 921 922 923 // All subscripts are all the same type. 924 // Loop bound may be smaller (e.g., a char). 925 // Should zero extend loop bound, since it's always >= 0. 926 // This routine collects upper bound and extends if needed. 927 // Return null if no bound available. 928 const SCEV *DependenceAnalysis::collectUpperBound(const Loop *L, 929 Type *T) const { 930 if (SE->hasLoopInvariantBackedgeTakenCount(L)) { 931 const SCEV *UB = SE->getBackedgeTakenCount(L); 932 return SE->getNoopOrZeroExtend(UB, T); 933 } 934 return nullptr; 935 } 936 937 938 // Calls collectUpperBound(), then attempts to cast it to SCEVConstant. 939 // If the cast fails, returns NULL. 940 const SCEVConstant *DependenceAnalysis::collectConstantUpperBound(const Loop *L, 941 Type *T 942 ) const { 943 if (const SCEV *UB = collectUpperBound(L, T)) 944 return dyn_cast<SCEVConstant>(UB); 945 return nullptr; 946 } 947 948 949 // testZIV - 950 // When we have a pair of subscripts of the form [c1] and [c2], 951 // where c1 and c2 are both loop invariant, we attack it using 952 // the ZIV test. Basically, we test by comparing the two values, 953 // but there are actually three possible results: 954 // 1) the values are equal, so there's a dependence 955 // 2) the values are different, so there's no dependence 956 // 3) the values might be equal, so we have to assume a dependence. 957 // 958 // Return true if dependence disproved. 959 bool DependenceAnalysis::testZIV(const SCEV *Src, 960 const SCEV *Dst, 961 FullDependence &Result) const { 962 DEBUG(dbgs() << " src = " << *Src << "\n"); 963 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 964 ++ZIVapplications; 965 if (isKnownPredicate(CmpInst::ICMP_EQ, Src, Dst)) { 966 DEBUG(dbgs() << " provably dependent\n"); 967 return false; // provably dependent 968 } 969 if (isKnownPredicate(CmpInst::ICMP_NE, Src, Dst)) { 970 DEBUG(dbgs() << " provably independent\n"); 971 ++ZIVindependence; 972 return true; // provably independent 973 } 974 DEBUG(dbgs() << " possibly dependent\n"); 975 Result.Consistent = false; 976 return false; // possibly dependent 977 } 978 979 980 // strongSIVtest - 981 // From the paper, Practical Dependence Testing, Section 4.2.1 982 // 983 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 + a*i], 984 // where i is an induction variable, c1 and c2 are loop invariant, 985 // and a is a constant, we can solve it exactly using the Strong SIV test. 986 // 987 // Can prove independence. Failing that, can compute distance (and direction). 988 // In the presence of symbolic terms, we can sometimes make progress. 989 // 990 // If there's a dependence, 991 // 992 // c1 + a*i = c2 + a*i' 993 // 994 // The dependence distance is 995 // 996 // d = i' - i = (c1 - c2)/a 997 // 998 // A dependence only exists if d is an integer and abs(d) <= U, where U is the 999 // loop's upper bound. If a dependence exists, the dependence direction is 1000 // defined as 1001 // 1002 // { < if d > 0 1003 // direction = { = if d = 0 1004 // { > if d < 0 1005 // 1006 // Return true if dependence disproved. 1007 bool DependenceAnalysis::strongSIVtest(const SCEV *Coeff, 1008 const SCEV *SrcConst, 1009 const SCEV *DstConst, 1010 const Loop *CurLoop, 1011 unsigned Level, 1012 FullDependence &Result, 1013 Constraint &NewConstraint) const { 1014 DEBUG(dbgs() << "\tStrong SIV test\n"); 1015 DEBUG(dbgs() << "\t Coeff = " << *Coeff); 1016 DEBUG(dbgs() << ", " << *Coeff->getType() << "\n"); 1017 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst); 1018 DEBUG(dbgs() << ", " << *SrcConst->getType() << "\n"); 1019 DEBUG(dbgs() << "\t DstConst = " << *DstConst); 1020 DEBUG(dbgs() << ", " << *DstConst->getType() << "\n"); 1021 ++StrongSIVapplications; 1022 assert(0 < Level && Level <= CommonLevels && "level out of range"); 1023 Level--; 1024 1025 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst); 1026 DEBUG(dbgs() << "\t Delta = " << *Delta); 1027 DEBUG(dbgs() << ", " << *Delta->getType() << "\n"); 1028 1029 // check that |Delta| < iteration count 1030 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 1031 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound); 1032 DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n"); 1033 const SCEV *AbsDelta = 1034 SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta); 1035 const SCEV *AbsCoeff = 1036 SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff); 1037 const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff); 1038 if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) { 1039 // Distance greater than trip count - no dependence 1040 ++StrongSIVindependence; 1041 ++StrongSIVsuccesses; 1042 return true; 1043 } 1044 } 1045 1046 // Can we compute distance? 1047 if (isa<SCEVConstant>(Delta) && isa<SCEVConstant>(Coeff)) { 1048 APInt ConstDelta = cast<SCEVConstant>(Delta)->getValue()->getValue(); 1049 APInt ConstCoeff = cast<SCEVConstant>(Coeff)->getValue()->getValue(); 1050 APInt Distance = ConstDelta; // these need to be initialized 1051 APInt Remainder = ConstDelta; 1052 APInt::sdivrem(ConstDelta, ConstCoeff, Distance, Remainder); 1053 DEBUG(dbgs() << "\t Distance = " << Distance << "\n"); 1054 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 1055 // Make sure Coeff divides Delta exactly 1056 if (Remainder != 0) { 1057 // Coeff doesn't divide Distance, no dependence 1058 ++StrongSIVindependence; 1059 ++StrongSIVsuccesses; 1060 return true; 1061 } 1062 Result.DV[Level].Distance = SE->getConstant(Distance); 1063 NewConstraint.setDistance(SE->getConstant(Distance), CurLoop); 1064 if (Distance.sgt(0)) 1065 Result.DV[Level].Direction &= Dependence::DVEntry::LT; 1066 else if (Distance.slt(0)) 1067 Result.DV[Level].Direction &= Dependence::DVEntry::GT; 1068 else 1069 Result.DV[Level].Direction &= Dependence::DVEntry::EQ; 1070 ++StrongSIVsuccesses; 1071 } 1072 else if (Delta->isZero()) { 1073 // since 0/X == 0 1074 Result.DV[Level].Distance = Delta; 1075 NewConstraint.setDistance(Delta, CurLoop); 1076 Result.DV[Level].Direction &= Dependence::DVEntry::EQ; 1077 ++StrongSIVsuccesses; 1078 } 1079 else { 1080 if (Coeff->isOne()) { 1081 DEBUG(dbgs() << "\t Distance = " << *Delta << "\n"); 1082 Result.DV[Level].Distance = Delta; // since X/1 == X 1083 NewConstraint.setDistance(Delta, CurLoop); 1084 } 1085 else { 1086 Result.Consistent = false; 1087 NewConstraint.setLine(Coeff, 1088 SE->getNegativeSCEV(Coeff), 1089 SE->getNegativeSCEV(Delta), CurLoop); 1090 } 1091 1092 // maybe we can get a useful direction 1093 bool DeltaMaybeZero = !SE->isKnownNonZero(Delta); 1094 bool DeltaMaybePositive = !SE->isKnownNonPositive(Delta); 1095 bool DeltaMaybeNegative = !SE->isKnownNonNegative(Delta); 1096 bool CoeffMaybePositive = !SE->isKnownNonPositive(Coeff); 1097 bool CoeffMaybeNegative = !SE->isKnownNonNegative(Coeff); 1098 // The double negatives above are confusing. 1099 // It helps to read !SE->isKnownNonZero(Delta) 1100 // as "Delta might be Zero" 1101 unsigned NewDirection = Dependence::DVEntry::NONE; 1102 if ((DeltaMaybePositive && CoeffMaybePositive) || 1103 (DeltaMaybeNegative && CoeffMaybeNegative)) 1104 NewDirection = Dependence::DVEntry::LT; 1105 if (DeltaMaybeZero) 1106 NewDirection |= Dependence::DVEntry::EQ; 1107 if ((DeltaMaybeNegative && CoeffMaybePositive) || 1108 (DeltaMaybePositive && CoeffMaybeNegative)) 1109 NewDirection |= Dependence::DVEntry::GT; 1110 if (NewDirection < Result.DV[Level].Direction) 1111 ++StrongSIVsuccesses; 1112 Result.DV[Level].Direction &= NewDirection; 1113 } 1114 return false; 1115 } 1116 1117 1118 // weakCrossingSIVtest - 1119 // From the paper, Practical Dependence Testing, Section 4.2.2 1120 // 1121 // When we have a pair of subscripts of the form [c1 + a*i] and [c2 - a*i], 1122 // where i is an induction variable, c1 and c2 are loop invariant, 1123 // and a is a constant, we can solve it exactly using the 1124 // Weak-Crossing SIV test. 1125 // 1126 // Given c1 + a*i = c2 - a*i', we can look for the intersection of 1127 // the two lines, where i = i', yielding 1128 // 1129 // c1 + a*i = c2 - a*i 1130 // 2a*i = c2 - c1 1131 // i = (c2 - c1)/2a 1132 // 1133 // If i < 0, there is no dependence. 1134 // If i > upperbound, there is no dependence. 1135 // If i = 0 (i.e., if c1 = c2), there's a dependence with distance = 0. 1136 // If i = upperbound, there's a dependence with distance = 0. 1137 // If i is integral, there's a dependence (all directions). 1138 // If the non-integer part = 1/2, there's a dependence (<> directions). 1139 // Otherwise, there's no dependence. 1140 // 1141 // Can prove independence. Failing that, 1142 // can sometimes refine the directions. 1143 // Can determine iteration for splitting. 1144 // 1145 // Return true if dependence disproved. 1146 bool DependenceAnalysis::weakCrossingSIVtest(const SCEV *Coeff, 1147 const SCEV *SrcConst, 1148 const SCEV *DstConst, 1149 const Loop *CurLoop, 1150 unsigned Level, 1151 FullDependence &Result, 1152 Constraint &NewConstraint, 1153 const SCEV *&SplitIter) const { 1154 DEBUG(dbgs() << "\tWeak-Crossing SIV test\n"); 1155 DEBUG(dbgs() << "\t Coeff = " << *Coeff << "\n"); 1156 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 1157 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 1158 ++WeakCrossingSIVapplications; 1159 assert(0 < Level && Level <= CommonLevels && "Level out of range"); 1160 Level--; 1161 Result.Consistent = false; 1162 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 1163 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1164 NewConstraint.setLine(Coeff, Coeff, Delta, CurLoop); 1165 if (Delta->isZero()) { 1166 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT); 1167 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT); 1168 ++WeakCrossingSIVsuccesses; 1169 if (!Result.DV[Level].Direction) { 1170 ++WeakCrossingSIVindependence; 1171 return true; 1172 } 1173 Result.DV[Level].Distance = Delta; // = 0 1174 return false; 1175 } 1176 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(Coeff); 1177 if (!ConstCoeff) 1178 return false; 1179 1180 Result.DV[Level].Splitable = true; 1181 if (SE->isKnownNegative(ConstCoeff)) { 1182 ConstCoeff = dyn_cast<SCEVConstant>(SE->getNegativeSCEV(ConstCoeff)); 1183 assert(ConstCoeff && 1184 "dynamic cast of negative of ConstCoeff should yield constant"); 1185 Delta = SE->getNegativeSCEV(Delta); 1186 } 1187 assert(SE->isKnownPositive(ConstCoeff) && "ConstCoeff should be positive"); 1188 1189 // compute SplitIter for use by DependenceAnalysis::getSplitIteration() 1190 SplitIter = 1191 SE->getUDivExpr(SE->getSMaxExpr(SE->getConstant(Delta->getType(), 0), 1192 Delta), 1193 SE->getMulExpr(SE->getConstant(Delta->getType(), 2), 1194 ConstCoeff)); 1195 DEBUG(dbgs() << "\t Split iter = " << *SplitIter << "\n"); 1196 1197 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 1198 if (!ConstDelta) 1199 return false; 1200 1201 // We're certain that ConstCoeff > 0; therefore, 1202 // if Delta < 0, then no dependence. 1203 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1204 DEBUG(dbgs() << "\t ConstCoeff = " << *ConstCoeff << "\n"); 1205 if (SE->isKnownNegative(Delta)) { 1206 // No dependence, Delta < 0 1207 ++WeakCrossingSIVindependence; 1208 ++WeakCrossingSIVsuccesses; 1209 return true; 1210 } 1211 1212 // We're certain that Delta > 0 and ConstCoeff > 0. 1213 // Check Delta/(2*ConstCoeff) against upper loop bound 1214 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 1215 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 1216 const SCEV *ConstantTwo = SE->getConstant(UpperBound->getType(), 2); 1217 const SCEV *ML = SE->getMulExpr(SE->getMulExpr(ConstCoeff, UpperBound), 1218 ConstantTwo); 1219 DEBUG(dbgs() << "\t ML = " << *ML << "\n"); 1220 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, ML)) { 1221 // Delta too big, no dependence 1222 ++WeakCrossingSIVindependence; 1223 ++WeakCrossingSIVsuccesses; 1224 return true; 1225 } 1226 if (isKnownPredicate(CmpInst::ICMP_EQ, Delta, ML)) { 1227 // i = i' = UB 1228 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::LT); 1229 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::GT); 1230 ++WeakCrossingSIVsuccesses; 1231 if (!Result.DV[Level].Direction) { 1232 ++WeakCrossingSIVindependence; 1233 return true; 1234 } 1235 Result.DV[Level].Splitable = false; 1236 Result.DV[Level].Distance = SE->getConstant(Delta->getType(), 0); 1237 return false; 1238 } 1239 } 1240 1241 // check that Coeff divides Delta 1242 APInt APDelta = ConstDelta->getValue()->getValue(); 1243 APInt APCoeff = ConstCoeff->getValue()->getValue(); 1244 APInt Distance = APDelta; // these need to be initialzed 1245 APInt Remainder = APDelta; 1246 APInt::sdivrem(APDelta, APCoeff, Distance, Remainder); 1247 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 1248 if (Remainder != 0) { 1249 // Coeff doesn't divide Delta, no dependence 1250 ++WeakCrossingSIVindependence; 1251 ++WeakCrossingSIVsuccesses; 1252 return true; 1253 } 1254 DEBUG(dbgs() << "\t Distance = " << Distance << "\n"); 1255 1256 // if 2*Coeff doesn't divide Delta, then the equal direction isn't possible 1257 APInt Two = APInt(Distance.getBitWidth(), 2, true); 1258 Remainder = Distance.srem(Two); 1259 DEBUG(dbgs() << "\t Remainder = " << Remainder << "\n"); 1260 if (Remainder != 0) { 1261 // Equal direction isn't possible 1262 Result.DV[Level].Direction &= unsigned(~Dependence::DVEntry::EQ); 1263 ++WeakCrossingSIVsuccesses; 1264 } 1265 return false; 1266 } 1267 1268 1269 // Kirch's algorithm, from 1270 // 1271 // Optimizing Supercompilers for Supercomputers 1272 // Michael Wolfe 1273 // MIT Press, 1989 1274 // 1275 // Program 2.1, page 29. 1276 // Computes the GCD of AM and BM. 1277 // Also finds a solution to the equation ax - by = gcd(a, b). 1278 // Returns true if dependence disproved; i.e., gcd does not divide Delta. 1279 static 1280 bool findGCD(unsigned Bits, APInt AM, APInt BM, APInt Delta, 1281 APInt &G, APInt &X, APInt &Y) { 1282 APInt A0(Bits, 1, true), A1(Bits, 0, true); 1283 APInt B0(Bits, 0, true), B1(Bits, 1, true); 1284 APInt G0 = AM.abs(); 1285 APInt G1 = BM.abs(); 1286 APInt Q = G0; // these need to be initialized 1287 APInt R = G0; 1288 APInt::sdivrem(G0, G1, Q, R); 1289 while (R != 0) { 1290 APInt A2 = A0 - Q*A1; A0 = A1; A1 = A2; 1291 APInt B2 = B0 - Q*B1; B0 = B1; B1 = B2; 1292 G0 = G1; G1 = R; 1293 APInt::sdivrem(G0, G1, Q, R); 1294 } 1295 G = G1; 1296 DEBUG(dbgs() << "\t GCD = " << G << "\n"); 1297 X = AM.slt(0) ? -A1 : A1; 1298 Y = BM.slt(0) ? B1 : -B1; 1299 1300 // make sure gcd divides Delta 1301 R = Delta.srem(G); 1302 if (R != 0) 1303 return true; // gcd doesn't divide Delta, no dependence 1304 Q = Delta.sdiv(G); 1305 X *= Q; 1306 Y *= Q; 1307 return false; 1308 } 1309 1310 1311 static 1312 APInt floorOfQuotient(APInt A, APInt B) { 1313 APInt Q = A; // these need to be initialized 1314 APInt R = A; 1315 APInt::sdivrem(A, B, Q, R); 1316 if (R == 0) 1317 return Q; 1318 if ((A.sgt(0) && B.sgt(0)) || 1319 (A.slt(0) && B.slt(0))) 1320 return Q; 1321 else 1322 return Q - 1; 1323 } 1324 1325 1326 static 1327 APInt ceilingOfQuotient(APInt A, APInt B) { 1328 APInt Q = A; // these need to be initialized 1329 APInt R = A; 1330 APInt::sdivrem(A, B, Q, R); 1331 if (R == 0) 1332 return Q; 1333 if ((A.sgt(0) && B.sgt(0)) || 1334 (A.slt(0) && B.slt(0))) 1335 return Q + 1; 1336 else 1337 return Q; 1338 } 1339 1340 1341 static 1342 APInt maxAPInt(APInt A, APInt B) { 1343 return A.sgt(B) ? A : B; 1344 } 1345 1346 1347 static 1348 APInt minAPInt(APInt A, APInt B) { 1349 return A.slt(B) ? A : B; 1350 } 1351 1352 1353 // exactSIVtest - 1354 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*i], 1355 // where i is an induction variable, c1 and c2 are loop invariant, and a1 1356 // and a2 are constant, we can solve it exactly using an algorithm developed 1357 // by Banerjee and Wolfe. See Section 2.5.3 in 1358 // 1359 // Optimizing Supercompilers for Supercomputers 1360 // Michael Wolfe 1361 // MIT Press, 1989 1362 // 1363 // It's slower than the specialized tests (strong SIV, weak-zero SIV, etc), 1364 // so use them if possible. They're also a bit better with symbolics and, 1365 // in the case of the strong SIV test, can compute Distances. 1366 // 1367 // Return true if dependence disproved. 1368 bool DependenceAnalysis::exactSIVtest(const SCEV *SrcCoeff, 1369 const SCEV *DstCoeff, 1370 const SCEV *SrcConst, 1371 const SCEV *DstConst, 1372 const Loop *CurLoop, 1373 unsigned Level, 1374 FullDependence &Result, 1375 Constraint &NewConstraint) const { 1376 DEBUG(dbgs() << "\tExact SIV test\n"); 1377 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n"); 1378 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n"); 1379 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 1380 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 1381 ++ExactSIVapplications; 1382 assert(0 < Level && Level <= CommonLevels && "Level out of range"); 1383 Level--; 1384 Result.Consistent = false; 1385 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 1386 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1387 NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), 1388 Delta, CurLoop); 1389 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 1390 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 1391 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 1392 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff) 1393 return false; 1394 1395 // find gcd 1396 APInt G, X, Y; 1397 APInt AM = ConstSrcCoeff->getValue()->getValue(); 1398 APInt BM = ConstDstCoeff->getValue()->getValue(); 1399 unsigned Bits = AM.getBitWidth(); 1400 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) { 1401 // gcd doesn't divide Delta, no dependence 1402 ++ExactSIVindependence; 1403 ++ExactSIVsuccesses; 1404 return true; 1405 } 1406 1407 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); 1408 1409 // since SCEV construction normalizes, LM = 0 1410 APInt UM(Bits, 1, true); 1411 bool UMvalid = false; 1412 // UM is perhaps unavailable, let's check 1413 if (const SCEVConstant *CUB = 1414 collectConstantUpperBound(CurLoop, Delta->getType())) { 1415 UM = CUB->getValue()->getValue(); 1416 DEBUG(dbgs() << "\t UM = " << UM << "\n"); 1417 UMvalid = true; 1418 } 1419 1420 APInt TU(APInt::getSignedMaxValue(Bits)); 1421 APInt TL(APInt::getSignedMinValue(Bits)); 1422 1423 // test(BM/G, LM-X) and test(-BM/G, X-UM) 1424 APInt TMUL = BM.sdiv(G); 1425 if (TMUL.sgt(0)) { 1426 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL)); 1427 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1428 if (UMvalid) { 1429 TU = minAPInt(TU, floorOfQuotient(UM - X, TMUL)); 1430 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1431 } 1432 } 1433 else { 1434 TU = minAPInt(TU, floorOfQuotient(-X, TMUL)); 1435 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1436 if (UMvalid) { 1437 TL = maxAPInt(TL, ceilingOfQuotient(UM - X, TMUL)); 1438 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1439 } 1440 } 1441 1442 // test(AM/G, LM-Y) and test(-AM/G, Y-UM) 1443 TMUL = AM.sdiv(G); 1444 if (TMUL.sgt(0)) { 1445 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL)); 1446 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1447 if (UMvalid) { 1448 TU = minAPInt(TU, floorOfQuotient(UM - Y, TMUL)); 1449 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1450 } 1451 } 1452 else { 1453 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL)); 1454 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1455 if (UMvalid) { 1456 TL = maxAPInt(TL, ceilingOfQuotient(UM - Y, TMUL)); 1457 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1458 } 1459 } 1460 if (TL.sgt(TU)) { 1461 ++ExactSIVindependence; 1462 ++ExactSIVsuccesses; 1463 return true; 1464 } 1465 1466 // explore directions 1467 unsigned NewDirection = Dependence::DVEntry::NONE; 1468 1469 // less than 1470 APInt SaveTU(TU); // save these 1471 APInt SaveTL(TL); 1472 DEBUG(dbgs() << "\t exploring LT direction\n"); 1473 TMUL = AM - BM; 1474 if (TMUL.sgt(0)) { 1475 TL = maxAPInt(TL, ceilingOfQuotient(X - Y + 1, TMUL)); 1476 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 1477 } 1478 else { 1479 TU = minAPInt(TU, floorOfQuotient(X - Y + 1, TMUL)); 1480 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 1481 } 1482 if (TL.sle(TU)) { 1483 NewDirection |= Dependence::DVEntry::LT; 1484 ++ExactSIVsuccesses; 1485 } 1486 1487 // equal 1488 TU = SaveTU; // restore 1489 TL = SaveTL; 1490 DEBUG(dbgs() << "\t exploring EQ direction\n"); 1491 if (TMUL.sgt(0)) { 1492 TL = maxAPInt(TL, ceilingOfQuotient(X - Y, TMUL)); 1493 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 1494 } 1495 else { 1496 TU = minAPInt(TU, floorOfQuotient(X - Y, TMUL)); 1497 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 1498 } 1499 TMUL = BM - AM; 1500 if (TMUL.sgt(0)) { 1501 TL = maxAPInt(TL, ceilingOfQuotient(Y - X, TMUL)); 1502 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 1503 } 1504 else { 1505 TU = minAPInt(TU, floorOfQuotient(Y - X, TMUL)); 1506 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 1507 } 1508 if (TL.sle(TU)) { 1509 NewDirection |= Dependence::DVEntry::EQ; 1510 ++ExactSIVsuccesses; 1511 } 1512 1513 // greater than 1514 TU = SaveTU; // restore 1515 TL = SaveTL; 1516 DEBUG(dbgs() << "\t exploring GT direction\n"); 1517 if (TMUL.sgt(0)) { 1518 TL = maxAPInt(TL, ceilingOfQuotient(Y - X + 1, TMUL)); 1519 DEBUG(dbgs() << "\t\t TL = " << TL << "\n"); 1520 } 1521 else { 1522 TU = minAPInt(TU, floorOfQuotient(Y - X + 1, TMUL)); 1523 DEBUG(dbgs() << "\t\t TU = " << TU << "\n"); 1524 } 1525 if (TL.sle(TU)) { 1526 NewDirection |= Dependence::DVEntry::GT; 1527 ++ExactSIVsuccesses; 1528 } 1529 1530 // finished 1531 Result.DV[Level].Direction &= NewDirection; 1532 if (Result.DV[Level].Direction == Dependence::DVEntry::NONE) 1533 ++ExactSIVindependence; 1534 return Result.DV[Level].Direction == Dependence::DVEntry::NONE; 1535 } 1536 1537 1538 1539 // Return true if the divisor evenly divides the dividend. 1540 static 1541 bool isRemainderZero(const SCEVConstant *Dividend, 1542 const SCEVConstant *Divisor) { 1543 APInt ConstDividend = Dividend->getValue()->getValue(); 1544 APInt ConstDivisor = Divisor->getValue()->getValue(); 1545 return ConstDividend.srem(ConstDivisor) == 0; 1546 } 1547 1548 1549 // weakZeroSrcSIVtest - 1550 // From the paper, Practical Dependence Testing, Section 4.2.2 1551 // 1552 // When we have a pair of subscripts of the form [c1] and [c2 + a*i], 1553 // where i is an induction variable, c1 and c2 are loop invariant, 1554 // and a is a constant, we can solve it exactly using the 1555 // Weak-Zero SIV test. 1556 // 1557 // Given 1558 // 1559 // c1 = c2 + a*i 1560 // 1561 // we get 1562 // 1563 // (c1 - c2)/a = i 1564 // 1565 // If i is not an integer, there's no dependence. 1566 // If i < 0 or > UB, there's no dependence. 1567 // If i = 0, the direction is <= and peeling the 1568 // 1st iteration will break the dependence. 1569 // If i = UB, the direction is >= and peeling the 1570 // last iteration will break the dependence. 1571 // Otherwise, the direction is *. 1572 // 1573 // Can prove independence. Failing that, we can sometimes refine 1574 // the directions. Can sometimes show that first or last 1575 // iteration carries all the dependences (so worth peeling). 1576 // 1577 // (see also weakZeroDstSIVtest) 1578 // 1579 // Return true if dependence disproved. 1580 bool DependenceAnalysis::weakZeroSrcSIVtest(const SCEV *DstCoeff, 1581 const SCEV *SrcConst, 1582 const SCEV *DstConst, 1583 const Loop *CurLoop, 1584 unsigned Level, 1585 FullDependence &Result, 1586 Constraint &NewConstraint) const { 1587 // For the WeakSIV test, it's possible the loop isn't common to 1588 // the Src and Dst loops. If it isn't, then there's no need to 1589 // record a direction. 1590 DEBUG(dbgs() << "\tWeak-Zero (src) SIV test\n"); 1591 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << "\n"); 1592 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 1593 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 1594 ++WeakZeroSIVapplications; 1595 assert(0 < Level && Level <= MaxLevels && "Level out of range"); 1596 Level--; 1597 Result.Consistent = false; 1598 const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst); 1599 NewConstraint.setLine(SE->getConstant(Delta->getType(), 0), 1600 DstCoeff, Delta, CurLoop); 1601 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1602 if (isKnownPredicate(CmpInst::ICMP_EQ, SrcConst, DstConst)) { 1603 if (Level < CommonLevels) { 1604 Result.DV[Level].Direction &= Dependence::DVEntry::LE; 1605 Result.DV[Level].PeelFirst = true; 1606 ++WeakZeroSIVsuccesses; 1607 } 1608 return false; // dependences caused by first iteration 1609 } 1610 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 1611 if (!ConstCoeff) 1612 return false; 1613 const SCEV *AbsCoeff = 1614 SE->isKnownNegative(ConstCoeff) ? 1615 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff; 1616 const SCEV *NewDelta = 1617 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta; 1618 1619 // check that Delta/SrcCoeff < iteration count 1620 // really check NewDelta < count*AbsCoeff 1621 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 1622 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 1623 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound); 1624 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) { 1625 ++WeakZeroSIVindependence; 1626 ++WeakZeroSIVsuccesses; 1627 return true; 1628 } 1629 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) { 1630 // dependences caused by last iteration 1631 if (Level < CommonLevels) { 1632 Result.DV[Level].Direction &= Dependence::DVEntry::GE; 1633 Result.DV[Level].PeelLast = true; 1634 ++WeakZeroSIVsuccesses; 1635 } 1636 return false; 1637 } 1638 } 1639 1640 // check that Delta/SrcCoeff >= 0 1641 // really check that NewDelta >= 0 1642 if (SE->isKnownNegative(NewDelta)) { 1643 // No dependence, newDelta < 0 1644 ++WeakZeroSIVindependence; 1645 ++WeakZeroSIVsuccesses; 1646 return true; 1647 } 1648 1649 // if SrcCoeff doesn't divide Delta, then no dependence 1650 if (isa<SCEVConstant>(Delta) && 1651 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) { 1652 ++WeakZeroSIVindependence; 1653 ++WeakZeroSIVsuccesses; 1654 return true; 1655 } 1656 return false; 1657 } 1658 1659 1660 // weakZeroDstSIVtest - 1661 // From the paper, Practical Dependence Testing, Section 4.2.2 1662 // 1663 // When we have a pair of subscripts of the form [c1 + a*i] and [c2], 1664 // where i is an induction variable, c1 and c2 are loop invariant, 1665 // and a is a constant, we can solve it exactly using the 1666 // Weak-Zero SIV test. 1667 // 1668 // Given 1669 // 1670 // c1 + a*i = c2 1671 // 1672 // we get 1673 // 1674 // i = (c2 - c1)/a 1675 // 1676 // If i is not an integer, there's no dependence. 1677 // If i < 0 or > UB, there's no dependence. 1678 // If i = 0, the direction is <= and peeling the 1679 // 1st iteration will break the dependence. 1680 // If i = UB, the direction is >= and peeling the 1681 // last iteration will break the dependence. 1682 // Otherwise, the direction is *. 1683 // 1684 // Can prove independence. Failing that, we can sometimes refine 1685 // the directions. Can sometimes show that first or last 1686 // iteration carries all the dependences (so worth peeling). 1687 // 1688 // (see also weakZeroSrcSIVtest) 1689 // 1690 // Return true if dependence disproved. 1691 bool DependenceAnalysis::weakZeroDstSIVtest(const SCEV *SrcCoeff, 1692 const SCEV *SrcConst, 1693 const SCEV *DstConst, 1694 const Loop *CurLoop, 1695 unsigned Level, 1696 FullDependence &Result, 1697 Constraint &NewConstraint) const { 1698 // For the WeakSIV test, it's possible the loop isn't common to the 1699 // Src and Dst loops. If it isn't, then there's no need to record a direction. 1700 DEBUG(dbgs() << "\tWeak-Zero (dst) SIV test\n"); 1701 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << "\n"); 1702 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 1703 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 1704 ++WeakZeroSIVapplications; 1705 assert(0 < Level && Level <= SrcLevels && "Level out of range"); 1706 Level--; 1707 Result.Consistent = false; 1708 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 1709 NewConstraint.setLine(SrcCoeff, SE->getConstant(Delta->getType(), 0), 1710 Delta, CurLoop); 1711 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1712 if (isKnownPredicate(CmpInst::ICMP_EQ, DstConst, SrcConst)) { 1713 if (Level < CommonLevels) { 1714 Result.DV[Level].Direction &= Dependence::DVEntry::LE; 1715 Result.DV[Level].PeelFirst = true; 1716 ++WeakZeroSIVsuccesses; 1717 } 1718 return false; // dependences caused by first iteration 1719 } 1720 const SCEVConstant *ConstCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 1721 if (!ConstCoeff) 1722 return false; 1723 const SCEV *AbsCoeff = 1724 SE->isKnownNegative(ConstCoeff) ? 1725 SE->getNegativeSCEV(ConstCoeff) : ConstCoeff; 1726 const SCEV *NewDelta = 1727 SE->isKnownNegative(ConstCoeff) ? SE->getNegativeSCEV(Delta) : Delta; 1728 1729 // check that Delta/SrcCoeff < iteration count 1730 // really check NewDelta < count*AbsCoeff 1731 if (const SCEV *UpperBound = collectUpperBound(CurLoop, Delta->getType())) { 1732 DEBUG(dbgs() << "\t UpperBound = " << *UpperBound << "\n"); 1733 const SCEV *Product = SE->getMulExpr(AbsCoeff, UpperBound); 1734 if (isKnownPredicate(CmpInst::ICMP_SGT, NewDelta, Product)) { 1735 ++WeakZeroSIVindependence; 1736 ++WeakZeroSIVsuccesses; 1737 return true; 1738 } 1739 if (isKnownPredicate(CmpInst::ICMP_EQ, NewDelta, Product)) { 1740 // dependences caused by last iteration 1741 if (Level < CommonLevels) { 1742 Result.DV[Level].Direction &= Dependence::DVEntry::GE; 1743 Result.DV[Level].PeelLast = true; 1744 ++WeakZeroSIVsuccesses; 1745 } 1746 return false; 1747 } 1748 } 1749 1750 // check that Delta/SrcCoeff >= 0 1751 // really check that NewDelta >= 0 1752 if (SE->isKnownNegative(NewDelta)) { 1753 // No dependence, newDelta < 0 1754 ++WeakZeroSIVindependence; 1755 ++WeakZeroSIVsuccesses; 1756 return true; 1757 } 1758 1759 // if SrcCoeff doesn't divide Delta, then no dependence 1760 if (isa<SCEVConstant>(Delta) && 1761 !isRemainderZero(cast<SCEVConstant>(Delta), ConstCoeff)) { 1762 ++WeakZeroSIVindependence; 1763 ++WeakZeroSIVsuccesses; 1764 return true; 1765 } 1766 return false; 1767 } 1768 1769 1770 // exactRDIVtest - Tests the RDIV subscript pair for dependence. 1771 // Things of the form [c1 + a*i] and [c2 + b*j], 1772 // where i and j are induction variable, c1 and c2 are loop invariant, 1773 // and a and b are constants. 1774 // Returns true if any possible dependence is disproved. 1775 // Marks the result as inconsistent. 1776 // Works in some cases that symbolicRDIVtest doesn't, and vice versa. 1777 bool DependenceAnalysis::exactRDIVtest(const SCEV *SrcCoeff, 1778 const SCEV *DstCoeff, 1779 const SCEV *SrcConst, 1780 const SCEV *DstConst, 1781 const Loop *SrcLoop, 1782 const Loop *DstLoop, 1783 FullDependence &Result) const { 1784 DEBUG(dbgs() << "\tExact RDIV test\n"); 1785 DEBUG(dbgs() << "\t SrcCoeff = " << *SrcCoeff << " = AM\n"); 1786 DEBUG(dbgs() << "\t DstCoeff = " << *DstCoeff << " = BM\n"); 1787 DEBUG(dbgs() << "\t SrcConst = " << *SrcConst << "\n"); 1788 DEBUG(dbgs() << "\t DstConst = " << *DstConst << "\n"); 1789 ++ExactRDIVapplications; 1790 Result.Consistent = false; 1791 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 1792 DEBUG(dbgs() << "\t Delta = " << *Delta << "\n"); 1793 const SCEVConstant *ConstDelta = dyn_cast<SCEVConstant>(Delta); 1794 const SCEVConstant *ConstSrcCoeff = dyn_cast<SCEVConstant>(SrcCoeff); 1795 const SCEVConstant *ConstDstCoeff = dyn_cast<SCEVConstant>(DstCoeff); 1796 if (!ConstDelta || !ConstSrcCoeff || !ConstDstCoeff) 1797 return false; 1798 1799 // find gcd 1800 APInt G, X, Y; 1801 APInt AM = ConstSrcCoeff->getValue()->getValue(); 1802 APInt BM = ConstDstCoeff->getValue()->getValue(); 1803 unsigned Bits = AM.getBitWidth(); 1804 if (findGCD(Bits, AM, BM, ConstDelta->getValue()->getValue(), G, X, Y)) { 1805 // gcd doesn't divide Delta, no dependence 1806 ++ExactRDIVindependence; 1807 return true; 1808 } 1809 1810 DEBUG(dbgs() << "\t X = " << X << ", Y = " << Y << "\n"); 1811 1812 // since SCEV construction seems to normalize, LM = 0 1813 APInt SrcUM(Bits, 1, true); 1814 bool SrcUMvalid = false; 1815 // SrcUM is perhaps unavailable, let's check 1816 if (const SCEVConstant *UpperBound = 1817 collectConstantUpperBound(SrcLoop, Delta->getType())) { 1818 SrcUM = UpperBound->getValue()->getValue(); 1819 DEBUG(dbgs() << "\t SrcUM = " << SrcUM << "\n"); 1820 SrcUMvalid = true; 1821 } 1822 1823 APInt DstUM(Bits, 1, true); 1824 bool DstUMvalid = false; 1825 // UM is perhaps unavailable, let's check 1826 if (const SCEVConstant *UpperBound = 1827 collectConstantUpperBound(DstLoop, Delta->getType())) { 1828 DstUM = UpperBound->getValue()->getValue(); 1829 DEBUG(dbgs() << "\t DstUM = " << DstUM << "\n"); 1830 DstUMvalid = true; 1831 } 1832 1833 APInt TU(APInt::getSignedMaxValue(Bits)); 1834 APInt TL(APInt::getSignedMinValue(Bits)); 1835 1836 // test(BM/G, LM-X) and test(-BM/G, X-UM) 1837 APInt TMUL = BM.sdiv(G); 1838 if (TMUL.sgt(0)) { 1839 TL = maxAPInt(TL, ceilingOfQuotient(-X, TMUL)); 1840 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1841 if (SrcUMvalid) { 1842 TU = minAPInt(TU, floorOfQuotient(SrcUM - X, TMUL)); 1843 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1844 } 1845 } 1846 else { 1847 TU = minAPInt(TU, floorOfQuotient(-X, TMUL)); 1848 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1849 if (SrcUMvalid) { 1850 TL = maxAPInt(TL, ceilingOfQuotient(SrcUM - X, TMUL)); 1851 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1852 } 1853 } 1854 1855 // test(AM/G, LM-Y) and test(-AM/G, Y-UM) 1856 TMUL = AM.sdiv(G); 1857 if (TMUL.sgt(0)) { 1858 TL = maxAPInt(TL, ceilingOfQuotient(-Y, TMUL)); 1859 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1860 if (DstUMvalid) { 1861 TU = minAPInt(TU, floorOfQuotient(DstUM - Y, TMUL)); 1862 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1863 } 1864 } 1865 else { 1866 TU = minAPInt(TU, floorOfQuotient(-Y, TMUL)); 1867 DEBUG(dbgs() << "\t TU = " << TU << "\n"); 1868 if (DstUMvalid) { 1869 TL = maxAPInt(TL, ceilingOfQuotient(DstUM - Y, TMUL)); 1870 DEBUG(dbgs() << "\t TL = " << TL << "\n"); 1871 } 1872 } 1873 if (TL.sgt(TU)) 1874 ++ExactRDIVindependence; 1875 return TL.sgt(TU); 1876 } 1877 1878 1879 // symbolicRDIVtest - 1880 // In Section 4.5 of the Practical Dependence Testing paper,the authors 1881 // introduce a special case of Banerjee's Inequalities (also called the 1882 // Extreme-Value Test) that can handle some of the SIV and RDIV cases, 1883 // particularly cases with symbolics. Since it's only able to disprove 1884 // dependence (not compute distances or directions), we'll use it as a 1885 // fall back for the other tests. 1886 // 1887 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j] 1888 // where i and j are induction variables and c1 and c2 are loop invariants, 1889 // we can use the symbolic tests to disprove some dependences, serving as a 1890 // backup for the RDIV test. Note that i and j can be the same variable, 1891 // letting this test serve as a backup for the various SIV tests. 1892 // 1893 // For a dependence to exist, c1 + a1*i must equal c2 + a2*j for some 1894 // 0 <= i <= N1 and some 0 <= j <= N2, where N1 and N2 are the (normalized) 1895 // loop bounds for the i and j loops, respectively. So, ... 1896 // 1897 // c1 + a1*i = c2 + a2*j 1898 // a1*i - a2*j = c2 - c1 1899 // 1900 // To test for a dependence, we compute c2 - c1 and make sure it's in the 1901 // range of the maximum and minimum possible values of a1*i - a2*j. 1902 // Considering the signs of a1 and a2, we have 4 possible cases: 1903 // 1904 // 1) If a1 >= 0 and a2 >= 0, then 1905 // a1*0 - a2*N2 <= c2 - c1 <= a1*N1 - a2*0 1906 // -a2*N2 <= c2 - c1 <= a1*N1 1907 // 1908 // 2) If a1 >= 0 and a2 <= 0, then 1909 // a1*0 - a2*0 <= c2 - c1 <= a1*N1 - a2*N2 1910 // 0 <= c2 - c1 <= a1*N1 - a2*N2 1911 // 1912 // 3) If a1 <= 0 and a2 >= 0, then 1913 // a1*N1 - a2*N2 <= c2 - c1 <= a1*0 - a2*0 1914 // a1*N1 - a2*N2 <= c2 - c1 <= 0 1915 // 1916 // 4) If a1 <= 0 and a2 <= 0, then 1917 // a1*N1 - a2*0 <= c2 - c1 <= a1*0 - a2*N2 1918 // a1*N1 <= c2 - c1 <= -a2*N2 1919 // 1920 // return true if dependence disproved 1921 bool DependenceAnalysis::symbolicRDIVtest(const SCEV *A1, 1922 const SCEV *A2, 1923 const SCEV *C1, 1924 const SCEV *C2, 1925 const Loop *Loop1, 1926 const Loop *Loop2) const { 1927 ++SymbolicRDIVapplications; 1928 DEBUG(dbgs() << "\ttry symbolic RDIV test\n"); 1929 DEBUG(dbgs() << "\t A1 = " << *A1); 1930 DEBUG(dbgs() << ", type = " << *A1->getType() << "\n"); 1931 DEBUG(dbgs() << "\t A2 = " << *A2 << "\n"); 1932 DEBUG(dbgs() << "\t C1 = " << *C1 << "\n"); 1933 DEBUG(dbgs() << "\t C2 = " << *C2 << "\n"); 1934 const SCEV *N1 = collectUpperBound(Loop1, A1->getType()); 1935 const SCEV *N2 = collectUpperBound(Loop2, A1->getType()); 1936 DEBUG(if (N1) dbgs() << "\t N1 = " << *N1 << "\n"); 1937 DEBUG(if (N2) dbgs() << "\t N2 = " << *N2 << "\n"); 1938 const SCEV *C2_C1 = SE->getMinusSCEV(C2, C1); 1939 const SCEV *C1_C2 = SE->getMinusSCEV(C1, C2); 1940 DEBUG(dbgs() << "\t C2 - C1 = " << *C2_C1 << "\n"); 1941 DEBUG(dbgs() << "\t C1 - C2 = " << *C1_C2 << "\n"); 1942 if (SE->isKnownNonNegative(A1)) { 1943 if (SE->isKnownNonNegative(A2)) { 1944 // A1 >= 0 && A2 >= 0 1945 if (N1) { 1946 // make sure that c2 - c1 <= a1*N1 1947 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 1948 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n"); 1949 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1)) { 1950 ++SymbolicRDIVindependence; 1951 return true; 1952 } 1953 } 1954 if (N2) { 1955 // make sure that -a2*N2 <= c2 - c1, or a2*N2 >= c1 - c2 1956 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 1957 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n"); 1958 if (isKnownPredicate(CmpInst::ICMP_SLT, A2N2, C1_C2)) { 1959 ++SymbolicRDIVindependence; 1960 return true; 1961 } 1962 } 1963 } 1964 else if (SE->isKnownNonPositive(A2)) { 1965 // a1 >= 0 && a2 <= 0 1966 if (N1 && N2) { 1967 // make sure that c2 - c1 <= a1*N1 - a2*N2 1968 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 1969 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 1970 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2); 1971 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n"); 1972 if (isKnownPredicate(CmpInst::ICMP_SGT, C2_C1, A1N1_A2N2)) { 1973 ++SymbolicRDIVindependence; 1974 return true; 1975 } 1976 } 1977 // make sure that 0 <= c2 - c1 1978 if (SE->isKnownNegative(C2_C1)) { 1979 ++SymbolicRDIVindependence; 1980 return true; 1981 } 1982 } 1983 } 1984 else if (SE->isKnownNonPositive(A1)) { 1985 if (SE->isKnownNonNegative(A2)) { 1986 // a1 <= 0 && a2 >= 0 1987 if (N1 && N2) { 1988 // make sure that a1*N1 - a2*N2 <= c2 - c1 1989 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 1990 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 1991 const SCEV *A1N1_A2N2 = SE->getMinusSCEV(A1N1, A2N2); 1992 DEBUG(dbgs() << "\t A1*N1 - A2*N2 = " << *A1N1_A2N2 << "\n"); 1993 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1_A2N2, C2_C1)) { 1994 ++SymbolicRDIVindependence; 1995 return true; 1996 } 1997 } 1998 // make sure that c2 - c1 <= 0 1999 if (SE->isKnownPositive(C2_C1)) { 2000 ++SymbolicRDIVindependence; 2001 return true; 2002 } 2003 } 2004 else if (SE->isKnownNonPositive(A2)) { 2005 // a1 <= 0 && a2 <= 0 2006 if (N1) { 2007 // make sure that a1*N1 <= c2 - c1 2008 const SCEV *A1N1 = SE->getMulExpr(A1, N1); 2009 DEBUG(dbgs() << "\t A1*N1 = " << *A1N1 << "\n"); 2010 if (isKnownPredicate(CmpInst::ICMP_SGT, A1N1, C2_C1)) { 2011 ++SymbolicRDIVindependence; 2012 return true; 2013 } 2014 } 2015 if (N2) { 2016 // make sure that c2 - c1 <= -a2*N2, or c1 - c2 >= a2*N2 2017 const SCEV *A2N2 = SE->getMulExpr(A2, N2); 2018 DEBUG(dbgs() << "\t A2*N2 = " << *A2N2 << "\n"); 2019 if (isKnownPredicate(CmpInst::ICMP_SLT, C1_C2, A2N2)) { 2020 ++SymbolicRDIVindependence; 2021 return true; 2022 } 2023 } 2024 } 2025 } 2026 return false; 2027 } 2028 2029 2030 // testSIV - 2031 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 - a2*i] 2032 // where i is an induction variable, c1 and c2 are loop invariant, and a1 and 2033 // a2 are constant, we attack it with an SIV test. While they can all be 2034 // solved with the Exact SIV test, it's worthwhile to use simpler tests when 2035 // they apply; they're cheaper and sometimes more precise. 2036 // 2037 // Return true if dependence disproved. 2038 bool DependenceAnalysis::testSIV(const SCEV *Src, 2039 const SCEV *Dst, 2040 unsigned &Level, 2041 FullDependence &Result, 2042 Constraint &NewConstraint, 2043 const SCEV *&SplitIter) const { 2044 DEBUG(dbgs() << " src = " << *Src << "\n"); 2045 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 2046 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src); 2047 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst); 2048 if (SrcAddRec && DstAddRec) { 2049 const SCEV *SrcConst = SrcAddRec->getStart(); 2050 const SCEV *DstConst = DstAddRec->getStart(); 2051 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 2052 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE); 2053 const Loop *CurLoop = SrcAddRec->getLoop(); 2054 assert(CurLoop == DstAddRec->getLoop() && 2055 "both loops in SIV should be same"); 2056 Level = mapSrcLoop(CurLoop); 2057 bool disproven; 2058 if (SrcCoeff == DstCoeff) 2059 disproven = strongSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 2060 Level, Result, NewConstraint); 2061 else if (SrcCoeff == SE->getNegativeSCEV(DstCoeff)) 2062 disproven = weakCrossingSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 2063 Level, Result, NewConstraint, SplitIter); 2064 else 2065 disproven = exactSIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, 2066 Level, Result, NewConstraint); 2067 return disproven || 2068 gcdMIVtest(Src, Dst, Result) || 2069 symbolicRDIVtest(SrcCoeff, DstCoeff, SrcConst, DstConst, CurLoop, CurLoop); 2070 } 2071 if (SrcAddRec) { 2072 const SCEV *SrcConst = SrcAddRec->getStart(); 2073 const SCEV *SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 2074 const SCEV *DstConst = Dst; 2075 const Loop *CurLoop = SrcAddRec->getLoop(); 2076 Level = mapSrcLoop(CurLoop); 2077 return weakZeroDstSIVtest(SrcCoeff, SrcConst, DstConst, CurLoop, 2078 Level, Result, NewConstraint) || 2079 gcdMIVtest(Src, Dst, Result); 2080 } 2081 if (DstAddRec) { 2082 const SCEV *DstConst = DstAddRec->getStart(); 2083 const SCEV *DstCoeff = DstAddRec->getStepRecurrence(*SE); 2084 const SCEV *SrcConst = Src; 2085 const Loop *CurLoop = DstAddRec->getLoop(); 2086 Level = mapDstLoop(CurLoop); 2087 return weakZeroSrcSIVtest(DstCoeff, SrcConst, DstConst, 2088 CurLoop, Level, Result, NewConstraint) || 2089 gcdMIVtest(Src, Dst, Result); 2090 } 2091 llvm_unreachable("SIV test expected at least one AddRec"); 2092 return false; 2093 } 2094 2095 2096 // testRDIV - 2097 // When we have a pair of subscripts of the form [c1 + a1*i] and [c2 + a2*j] 2098 // where i and j are induction variables, c1 and c2 are loop invariant, 2099 // and a1 and a2 are constant, we can solve it exactly with an easy adaptation 2100 // of the Exact SIV test, the Restricted Double Index Variable (RDIV) test. 2101 // It doesn't make sense to talk about distance or direction in this case, 2102 // so there's no point in making special versions of the Strong SIV test or 2103 // the Weak-crossing SIV test. 2104 // 2105 // With minor algebra, this test can also be used for things like 2106 // [c1 + a1*i + a2*j][c2]. 2107 // 2108 // Return true if dependence disproved. 2109 bool DependenceAnalysis::testRDIV(const SCEV *Src, 2110 const SCEV *Dst, 2111 FullDependence &Result) const { 2112 // we have 3 possible situations here: 2113 // 1) [a*i + b] and [c*j + d] 2114 // 2) [a*i + c*j + b] and [d] 2115 // 3) [b] and [a*i + c*j + d] 2116 // We need to find what we've got and get organized 2117 2118 const SCEV *SrcConst, *DstConst; 2119 const SCEV *SrcCoeff, *DstCoeff; 2120 const Loop *SrcLoop, *DstLoop; 2121 2122 DEBUG(dbgs() << " src = " << *Src << "\n"); 2123 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 2124 const SCEVAddRecExpr *SrcAddRec = dyn_cast<SCEVAddRecExpr>(Src); 2125 const SCEVAddRecExpr *DstAddRec = dyn_cast<SCEVAddRecExpr>(Dst); 2126 if (SrcAddRec && DstAddRec) { 2127 SrcConst = SrcAddRec->getStart(); 2128 SrcCoeff = SrcAddRec->getStepRecurrence(*SE); 2129 SrcLoop = SrcAddRec->getLoop(); 2130 DstConst = DstAddRec->getStart(); 2131 DstCoeff = DstAddRec->getStepRecurrence(*SE); 2132 DstLoop = DstAddRec->getLoop(); 2133 } 2134 else if (SrcAddRec) { 2135 if (const SCEVAddRecExpr *tmpAddRec = 2136 dyn_cast<SCEVAddRecExpr>(SrcAddRec->getStart())) { 2137 SrcConst = tmpAddRec->getStart(); 2138 SrcCoeff = tmpAddRec->getStepRecurrence(*SE); 2139 SrcLoop = tmpAddRec->getLoop(); 2140 DstConst = Dst; 2141 DstCoeff = SE->getNegativeSCEV(SrcAddRec->getStepRecurrence(*SE)); 2142 DstLoop = SrcAddRec->getLoop(); 2143 } 2144 else 2145 llvm_unreachable("RDIV reached by surprising SCEVs"); 2146 } 2147 else if (DstAddRec) { 2148 if (const SCEVAddRecExpr *tmpAddRec = 2149 dyn_cast<SCEVAddRecExpr>(DstAddRec->getStart())) { 2150 DstConst = tmpAddRec->getStart(); 2151 DstCoeff = tmpAddRec->getStepRecurrence(*SE); 2152 DstLoop = tmpAddRec->getLoop(); 2153 SrcConst = Src; 2154 SrcCoeff = SE->getNegativeSCEV(DstAddRec->getStepRecurrence(*SE)); 2155 SrcLoop = DstAddRec->getLoop(); 2156 } 2157 else 2158 llvm_unreachable("RDIV reached by surprising SCEVs"); 2159 } 2160 else 2161 llvm_unreachable("RDIV expected at least one AddRec"); 2162 return exactRDIVtest(SrcCoeff, DstCoeff, 2163 SrcConst, DstConst, 2164 SrcLoop, DstLoop, 2165 Result) || 2166 gcdMIVtest(Src, Dst, Result) || 2167 symbolicRDIVtest(SrcCoeff, DstCoeff, 2168 SrcConst, DstConst, 2169 SrcLoop, DstLoop); 2170 } 2171 2172 2173 // Tests the single-subscript MIV pair (Src and Dst) for dependence. 2174 // Return true if dependence disproved. 2175 // Can sometimes refine direction vectors. 2176 bool DependenceAnalysis::testMIV(const SCEV *Src, 2177 const SCEV *Dst, 2178 const SmallBitVector &Loops, 2179 FullDependence &Result) const { 2180 DEBUG(dbgs() << " src = " << *Src << "\n"); 2181 DEBUG(dbgs() << " dst = " << *Dst << "\n"); 2182 Result.Consistent = false; 2183 return gcdMIVtest(Src, Dst, Result) || 2184 banerjeeMIVtest(Src, Dst, Loops, Result); 2185 } 2186 2187 2188 // Given a product, e.g., 10*X*Y, returns the first constant operand, 2189 // in this case 10. If there is no constant part, returns NULL. 2190 static 2191 const SCEVConstant *getConstantPart(const SCEVMulExpr *Product) { 2192 for (unsigned Op = 0, Ops = Product->getNumOperands(); Op < Ops; Op++) { 2193 if (const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Product->getOperand(Op))) 2194 return Constant; 2195 } 2196 return nullptr; 2197 } 2198 2199 2200 //===----------------------------------------------------------------------===// 2201 // gcdMIVtest - 2202 // Tests an MIV subscript pair for dependence. 2203 // Returns true if any possible dependence is disproved. 2204 // Marks the result as inconsistent. 2205 // Can sometimes disprove the equal direction for 1 or more loops, 2206 // as discussed in Michael Wolfe's book, 2207 // High Performance Compilers for Parallel Computing, page 235. 2208 // 2209 // We spend some effort (code!) to handle cases like 2210 // [10*i + 5*N*j + 15*M + 6], where i and j are induction variables, 2211 // but M and N are just loop-invariant variables. 2212 // This should help us handle linearized subscripts; 2213 // also makes this test a useful backup to the various SIV tests. 2214 // 2215 // It occurs to me that the presence of loop-invariant variables 2216 // changes the nature of the test from "greatest common divisor" 2217 // to "a common divisor". 2218 bool DependenceAnalysis::gcdMIVtest(const SCEV *Src, 2219 const SCEV *Dst, 2220 FullDependence &Result) const { 2221 DEBUG(dbgs() << "starting gcd\n"); 2222 ++GCDapplications; 2223 unsigned BitWidth = SE->getTypeSizeInBits(Src->getType()); 2224 APInt RunningGCD = APInt::getNullValue(BitWidth); 2225 2226 // Examine Src coefficients. 2227 // Compute running GCD and record source constant. 2228 // Because we're looking for the constant at the end of the chain, 2229 // we can't quit the loop just because the GCD == 1. 2230 const SCEV *Coefficients = Src; 2231 while (const SCEVAddRecExpr *AddRec = 2232 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 2233 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 2234 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff); 2235 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 2236 // If the coefficient is the product of a constant and other stuff, 2237 // we can use the constant in the GCD computation. 2238 Constant = getConstantPart(Product); 2239 if (!Constant) 2240 return false; 2241 APInt ConstCoeff = Constant->getValue()->getValue(); 2242 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 2243 Coefficients = AddRec->getStart(); 2244 } 2245 const SCEV *SrcConst = Coefficients; 2246 2247 // Examine Dst coefficients. 2248 // Compute running GCD and record destination constant. 2249 // Because we're looking for the constant at the end of the chain, 2250 // we can't quit the loop just because the GCD == 1. 2251 Coefficients = Dst; 2252 while (const SCEVAddRecExpr *AddRec = 2253 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 2254 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 2255 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Coeff); 2256 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 2257 // If the coefficient is the product of a constant and other stuff, 2258 // we can use the constant in the GCD computation. 2259 Constant = getConstantPart(Product); 2260 if (!Constant) 2261 return false; 2262 APInt ConstCoeff = Constant->getValue()->getValue(); 2263 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 2264 Coefficients = AddRec->getStart(); 2265 } 2266 const SCEV *DstConst = Coefficients; 2267 2268 APInt ExtraGCD = APInt::getNullValue(BitWidth); 2269 const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst); 2270 DEBUG(dbgs() << " Delta = " << *Delta << "\n"); 2271 const SCEVConstant *Constant = dyn_cast<SCEVConstant>(Delta); 2272 if (const SCEVAddExpr *Sum = dyn_cast<SCEVAddExpr>(Delta)) { 2273 // If Delta is a sum of products, we may be able to make further progress. 2274 for (unsigned Op = 0, Ops = Sum->getNumOperands(); Op < Ops; Op++) { 2275 const SCEV *Operand = Sum->getOperand(Op); 2276 if (isa<SCEVConstant>(Operand)) { 2277 assert(!Constant && "Surprised to find multiple constants"); 2278 Constant = cast<SCEVConstant>(Operand); 2279 } 2280 else if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Operand)) { 2281 // Search for constant operand to participate in GCD; 2282 // If none found; return false. 2283 const SCEVConstant *ConstOp = getConstantPart(Product); 2284 if (!ConstOp) 2285 return false; 2286 APInt ConstOpValue = ConstOp->getValue()->getValue(); 2287 ExtraGCD = APIntOps::GreatestCommonDivisor(ExtraGCD, 2288 ConstOpValue.abs()); 2289 } 2290 else 2291 return false; 2292 } 2293 } 2294 if (!Constant) 2295 return false; 2296 APInt ConstDelta = cast<SCEVConstant>(Constant)->getValue()->getValue(); 2297 DEBUG(dbgs() << " ConstDelta = " << ConstDelta << "\n"); 2298 if (ConstDelta == 0) 2299 return false; 2300 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ExtraGCD); 2301 DEBUG(dbgs() << " RunningGCD = " << RunningGCD << "\n"); 2302 APInt Remainder = ConstDelta.srem(RunningGCD); 2303 if (Remainder != 0) { 2304 ++GCDindependence; 2305 return true; 2306 } 2307 2308 // Try to disprove equal directions. 2309 // For example, given a subscript pair [3*i + 2*j] and [i' + 2*j' - 1], 2310 // the code above can't disprove the dependence because the GCD = 1. 2311 // So we consider what happen if i = i' and what happens if j = j'. 2312 // If i = i', we can simplify the subscript to [2*i + 2*j] and [2*j' - 1], 2313 // which is infeasible, so we can disallow the = direction for the i level. 2314 // Setting j = j' doesn't help matters, so we end up with a direction vector 2315 // of [<>, *] 2316 // 2317 // Given A[5*i + 10*j*M + 9*M*N] and A[15*i + 20*j*M - 21*N*M + 5], 2318 // we need to remember that the constant part is 5 and the RunningGCD should 2319 // be initialized to ExtraGCD = 30. 2320 DEBUG(dbgs() << " ExtraGCD = " << ExtraGCD << '\n'); 2321 2322 bool Improved = false; 2323 Coefficients = Src; 2324 while (const SCEVAddRecExpr *AddRec = 2325 dyn_cast<SCEVAddRecExpr>(Coefficients)) { 2326 Coefficients = AddRec->getStart(); 2327 const Loop *CurLoop = AddRec->getLoop(); 2328 RunningGCD = ExtraGCD; 2329 const SCEV *SrcCoeff = AddRec->getStepRecurrence(*SE); 2330 const SCEV *DstCoeff = SE->getMinusSCEV(SrcCoeff, SrcCoeff); 2331 const SCEV *Inner = Src; 2332 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) { 2333 AddRec = cast<SCEVAddRecExpr>(Inner); 2334 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 2335 if (CurLoop == AddRec->getLoop()) 2336 ; // SrcCoeff == Coeff 2337 else { 2338 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 2339 // If the coefficient is the product of a constant and other stuff, 2340 // we can use the constant in the GCD computation. 2341 Constant = getConstantPart(Product); 2342 else 2343 Constant = cast<SCEVConstant>(Coeff); 2344 APInt ConstCoeff = Constant->getValue()->getValue(); 2345 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 2346 } 2347 Inner = AddRec->getStart(); 2348 } 2349 Inner = Dst; 2350 while (RunningGCD != 1 && isa<SCEVAddRecExpr>(Inner)) { 2351 AddRec = cast<SCEVAddRecExpr>(Inner); 2352 const SCEV *Coeff = AddRec->getStepRecurrence(*SE); 2353 if (CurLoop == AddRec->getLoop()) 2354 DstCoeff = Coeff; 2355 else { 2356 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Coeff)) 2357 // If the coefficient is the product of a constant and other stuff, 2358 // we can use the constant in the GCD computation. 2359 Constant = getConstantPart(Product); 2360 else 2361 Constant = cast<SCEVConstant>(Coeff); 2362 APInt ConstCoeff = Constant->getValue()->getValue(); 2363 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 2364 } 2365 Inner = AddRec->getStart(); 2366 } 2367 Delta = SE->getMinusSCEV(SrcCoeff, DstCoeff); 2368 if (const SCEVMulExpr *Product = dyn_cast<SCEVMulExpr>(Delta)) 2369 // If the coefficient is the product of a constant and other stuff, 2370 // we can use the constant in the GCD computation. 2371 Constant = getConstantPart(Product); 2372 else if (isa<SCEVConstant>(Delta)) 2373 Constant = cast<SCEVConstant>(Delta); 2374 else { 2375 // The difference of the two coefficients might not be a product 2376 // or constant, in which case we give up on this direction. 2377 continue; 2378 } 2379 APInt ConstCoeff = Constant->getValue()->getValue(); 2380 RunningGCD = APIntOps::GreatestCommonDivisor(RunningGCD, ConstCoeff.abs()); 2381 DEBUG(dbgs() << "\tRunningGCD = " << RunningGCD << "\n"); 2382 if (RunningGCD != 0) { 2383 Remainder = ConstDelta.srem(RunningGCD); 2384 DEBUG(dbgs() << "\tRemainder = " << Remainder << "\n"); 2385 if (Remainder != 0) { 2386 unsigned Level = mapSrcLoop(CurLoop); 2387 Result.DV[Level - 1].Direction &= unsigned(~Dependence::DVEntry::EQ); 2388 Improved = true; 2389 } 2390 } 2391 } 2392 if (Improved) 2393 ++GCDsuccesses; 2394 DEBUG(dbgs() << "all done\n"); 2395 return false; 2396 } 2397 2398 2399 //===----------------------------------------------------------------------===// 2400 // banerjeeMIVtest - 2401 // Use Banerjee's Inequalities to test an MIV subscript pair. 2402 // (Wolfe, in the race-car book, calls this the Extreme Value Test.) 2403 // Generally follows the discussion in Section 2.5.2 of 2404 // 2405 // Optimizing Supercompilers for Supercomputers 2406 // Michael Wolfe 2407 // 2408 // The inequalities given on page 25 are simplified in that loops are 2409 // normalized so that the lower bound is always 0 and the stride is always 1. 2410 // For example, Wolfe gives 2411 // 2412 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 2413 // 2414 // where A_k is the coefficient of the kth index in the source subscript, 2415 // B_k is the coefficient of the kth index in the destination subscript, 2416 // U_k is the upper bound of the kth index, L_k is the lower bound of the Kth 2417 // index, and N_k is the stride of the kth index. Since all loops are normalized 2418 // by the SCEV package, N_k = 1 and L_k = 0, allowing us to simplify the 2419 // equation to 2420 // 2421 // LB^<_k = (A^-_k - B_k)^- (U_k - 0 - 1) + (A_k - B_k)0 - B_k 1 2422 // = (A^-_k - B_k)^- (U_k - 1) - B_k 2423 // 2424 // Similar simplifications are possible for the other equations. 2425 // 2426 // When we can't determine the number of iterations for a loop, 2427 // we use NULL as an indicator for the worst case, infinity. 2428 // When computing the upper bound, NULL denotes +inf; 2429 // for the lower bound, NULL denotes -inf. 2430 // 2431 // Return true if dependence disproved. 2432 bool DependenceAnalysis::banerjeeMIVtest(const SCEV *Src, 2433 const SCEV *Dst, 2434 const SmallBitVector &Loops, 2435 FullDependence &Result) const { 2436 DEBUG(dbgs() << "starting Banerjee\n"); 2437 ++BanerjeeApplications; 2438 DEBUG(dbgs() << " Src = " << *Src << '\n'); 2439 const SCEV *A0; 2440 CoefficientInfo *A = collectCoeffInfo(Src, true, A0); 2441 DEBUG(dbgs() << " Dst = " << *Dst << '\n'); 2442 const SCEV *B0; 2443 CoefficientInfo *B = collectCoeffInfo(Dst, false, B0); 2444 BoundInfo *Bound = new BoundInfo[MaxLevels + 1]; 2445 const SCEV *Delta = SE->getMinusSCEV(B0, A0); 2446 DEBUG(dbgs() << "\tDelta = " << *Delta << '\n'); 2447 2448 // Compute bounds for all the * directions. 2449 DEBUG(dbgs() << "\tBounds[*]\n"); 2450 for (unsigned K = 1; K <= MaxLevels; ++K) { 2451 Bound[K].Iterations = A[K].Iterations ? A[K].Iterations : B[K].Iterations; 2452 Bound[K].Direction = Dependence::DVEntry::ALL; 2453 Bound[K].DirSet = Dependence::DVEntry::NONE; 2454 findBoundsALL(A, B, Bound, K); 2455 #ifndef NDEBUG 2456 DEBUG(dbgs() << "\t " << K << '\t'); 2457 if (Bound[K].Lower[Dependence::DVEntry::ALL]) 2458 DEBUG(dbgs() << *Bound[K].Lower[Dependence::DVEntry::ALL] << '\t'); 2459 else 2460 DEBUG(dbgs() << "-inf\t"); 2461 if (Bound[K].Upper[Dependence::DVEntry::ALL]) 2462 DEBUG(dbgs() << *Bound[K].Upper[Dependence::DVEntry::ALL] << '\n'); 2463 else 2464 DEBUG(dbgs() << "+inf\n"); 2465 #endif 2466 } 2467 2468 // Test the *, *, *, ... case. 2469 bool Disproved = false; 2470 if (testBounds(Dependence::DVEntry::ALL, 0, Bound, Delta)) { 2471 // Explore the direction vector hierarchy. 2472 unsigned DepthExpanded = 0; 2473 unsigned NewDeps = exploreDirections(1, A, B, Bound, 2474 Loops, DepthExpanded, Delta); 2475 if (NewDeps > 0) { 2476 bool Improved = false; 2477 for (unsigned K = 1; K <= CommonLevels; ++K) { 2478 if (Loops[K]) { 2479 unsigned Old = Result.DV[K - 1].Direction; 2480 Result.DV[K - 1].Direction = Old & Bound[K].DirSet; 2481 Improved |= Old != Result.DV[K - 1].Direction; 2482 if (!Result.DV[K - 1].Direction) { 2483 Improved = false; 2484 Disproved = true; 2485 break; 2486 } 2487 } 2488 } 2489 if (Improved) 2490 ++BanerjeeSuccesses; 2491 } 2492 else { 2493 ++BanerjeeIndependence; 2494 Disproved = true; 2495 } 2496 } 2497 else { 2498 ++BanerjeeIndependence; 2499 Disproved = true; 2500 } 2501 delete [] Bound; 2502 delete [] A; 2503 delete [] B; 2504 return Disproved; 2505 } 2506 2507 2508 // Hierarchically expands the direction vector 2509 // search space, combining the directions of discovered dependences 2510 // in the DirSet field of Bound. Returns the number of distinct 2511 // dependences discovered. If the dependence is disproved, 2512 // it will return 0. 2513 unsigned DependenceAnalysis::exploreDirections(unsigned Level, 2514 CoefficientInfo *A, 2515 CoefficientInfo *B, 2516 BoundInfo *Bound, 2517 const SmallBitVector &Loops, 2518 unsigned &DepthExpanded, 2519 const SCEV *Delta) const { 2520 if (Level > CommonLevels) { 2521 // record result 2522 DEBUG(dbgs() << "\t["); 2523 for (unsigned K = 1; K <= CommonLevels; ++K) { 2524 if (Loops[K]) { 2525 Bound[K].DirSet |= Bound[K].Direction; 2526 #ifndef NDEBUG 2527 switch (Bound[K].Direction) { 2528 case Dependence::DVEntry::LT: 2529 DEBUG(dbgs() << " <"); 2530 break; 2531 case Dependence::DVEntry::EQ: 2532 DEBUG(dbgs() << " ="); 2533 break; 2534 case Dependence::DVEntry::GT: 2535 DEBUG(dbgs() << " >"); 2536 break; 2537 case Dependence::DVEntry::ALL: 2538 DEBUG(dbgs() << " *"); 2539 break; 2540 default: 2541 llvm_unreachable("unexpected Bound[K].Direction"); 2542 } 2543 #endif 2544 } 2545 } 2546 DEBUG(dbgs() << " ]\n"); 2547 return 1; 2548 } 2549 if (Loops[Level]) { 2550 if (Level > DepthExpanded) { 2551 DepthExpanded = Level; 2552 // compute bounds for <, =, > at current level 2553 findBoundsLT(A, B, Bound, Level); 2554 findBoundsGT(A, B, Bound, Level); 2555 findBoundsEQ(A, B, Bound, Level); 2556 #ifndef NDEBUG 2557 DEBUG(dbgs() << "\tBound for level = " << Level << '\n'); 2558 DEBUG(dbgs() << "\t <\t"); 2559 if (Bound[Level].Lower[Dependence::DVEntry::LT]) 2560 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::LT] << '\t'); 2561 else 2562 DEBUG(dbgs() << "-inf\t"); 2563 if (Bound[Level].Upper[Dependence::DVEntry::LT]) 2564 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::LT] << '\n'); 2565 else 2566 DEBUG(dbgs() << "+inf\n"); 2567 DEBUG(dbgs() << "\t =\t"); 2568 if (Bound[Level].Lower[Dependence::DVEntry::EQ]) 2569 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::EQ] << '\t'); 2570 else 2571 DEBUG(dbgs() << "-inf\t"); 2572 if (Bound[Level].Upper[Dependence::DVEntry::EQ]) 2573 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::EQ] << '\n'); 2574 else 2575 DEBUG(dbgs() << "+inf\n"); 2576 DEBUG(dbgs() << "\t >\t"); 2577 if (Bound[Level].Lower[Dependence::DVEntry::GT]) 2578 DEBUG(dbgs() << *Bound[Level].Lower[Dependence::DVEntry::GT] << '\t'); 2579 else 2580 DEBUG(dbgs() << "-inf\t"); 2581 if (Bound[Level].Upper[Dependence::DVEntry::GT]) 2582 DEBUG(dbgs() << *Bound[Level].Upper[Dependence::DVEntry::GT] << '\n'); 2583 else 2584 DEBUG(dbgs() << "+inf\n"); 2585 #endif 2586 } 2587 2588 unsigned NewDeps = 0; 2589 2590 // test bounds for <, *, *, ... 2591 if (testBounds(Dependence::DVEntry::LT, Level, Bound, Delta)) 2592 NewDeps += exploreDirections(Level + 1, A, B, Bound, 2593 Loops, DepthExpanded, Delta); 2594 2595 // Test bounds for =, *, *, ... 2596 if (testBounds(Dependence::DVEntry::EQ, Level, Bound, Delta)) 2597 NewDeps += exploreDirections(Level + 1, A, B, Bound, 2598 Loops, DepthExpanded, Delta); 2599 2600 // test bounds for >, *, *, ... 2601 if (testBounds(Dependence::DVEntry::GT, Level, Bound, Delta)) 2602 NewDeps += exploreDirections(Level + 1, A, B, Bound, 2603 Loops, DepthExpanded, Delta); 2604 2605 Bound[Level].Direction = Dependence::DVEntry::ALL; 2606 return NewDeps; 2607 } 2608 else 2609 return exploreDirections(Level + 1, A, B, Bound, Loops, DepthExpanded, Delta); 2610 } 2611 2612 2613 // Returns true iff the current bounds are plausible. 2614 bool DependenceAnalysis::testBounds(unsigned char DirKind, 2615 unsigned Level, 2616 BoundInfo *Bound, 2617 const SCEV *Delta) const { 2618 Bound[Level].Direction = DirKind; 2619 if (const SCEV *LowerBound = getLowerBound(Bound)) 2620 if (isKnownPredicate(CmpInst::ICMP_SGT, LowerBound, Delta)) 2621 return false; 2622 if (const SCEV *UpperBound = getUpperBound(Bound)) 2623 if (isKnownPredicate(CmpInst::ICMP_SGT, Delta, UpperBound)) 2624 return false; 2625 return true; 2626 } 2627 2628 2629 // Computes the upper and lower bounds for level K 2630 // using the * direction. Records them in Bound. 2631 // Wolfe gives the equations 2632 // 2633 // LB^*_k = (A^-_k - B^+_k)(U_k - L_k) + (A_k - B_k)L_k 2634 // UB^*_k = (A^+_k - B^-_k)(U_k - L_k) + (A_k - B_k)L_k 2635 // 2636 // Since we normalize loops, we can simplify these equations to 2637 // 2638 // LB^*_k = (A^-_k - B^+_k)U_k 2639 // UB^*_k = (A^+_k - B^-_k)U_k 2640 // 2641 // We must be careful to handle the case where the upper bound is unknown. 2642 // Note that the lower bound is always <= 0 2643 // and the upper bound is always >= 0. 2644 void DependenceAnalysis::findBoundsALL(CoefficientInfo *A, 2645 CoefficientInfo *B, 2646 BoundInfo *Bound, 2647 unsigned K) const { 2648 Bound[K].Lower[Dependence::DVEntry::ALL] = nullptr; // Default value = -infinity. 2649 Bound[K].Upper[Dependence::DVEntry::ALL] = nullptr; // Default value = +infinity. 2650 if (Bound[K].Iterations) { 2651 Bound[K].Lower[Dependence::DVEntry::ALL] = 2652 SE->getMulExpr(SE->getMinusSCEV(A[K].NegPart, B[K].PosPart), 2653 Bound[K].Iterations); 2654 Bound[K].Upper[Dependence::DVEntry::ALL] = 2655 SE->getMulExpr(SE->getMinusSCEV(A[K].PosPart, B[K].NegPart), 2656 Bound[K].Iterations); 2657 } 2658 else { 2659 // If the difference is 0, we won't need to know the number of iterations. 2660 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].NegPart, B[K].PosPart)) 2661 Bound[K].Lower[Dependence::DVEntry::ALL] = 2662 SE->getConstant(A[K].Coeff->getType(), 0); 2663 if (isKnownPredicate(CmpInst::ICMP_EQ, A[K].PosPart, B[K].NegPart)) 2664 Bound[K].Upper[Dependence::DVEntry::ALL] = 2665 SE->getConstant(A[K].Coeff->getType(), 0); 2666 } 2667 } 2668 2669 2670 // Computes the upper and lower bounds for level K 2671 // using the = direction. Records them in Bound. 2672 // Wolfe gives the equations 2673 // 2674 // LB^=_k = (A_k - B_k)^- (U_k - L_k) + (A_k - B_k)L_k 2675 // UB^=_k = (A_k - B_k)^+ (U_k - L_k) + (A_k - B_k)L_k 2676 // 2677 // Since we normalize loops, we can simplify these equations to 2678 // 2679 // LB^=_k = (A_k - B_k)^- U_k 2680 // UB^=_k = (A_k - B_k)^+ U_k 2681 // 2682 // We must be careful to handle the case where the upper bound is unknown. 2683 // Note that the lower bound is always <= 0 2684 // and the upper bound is always >= 0. 2685 void DependenceAnalysis::findBoundsEQ(CoefficientInfo *A, 2686 CoefficientInfo *B, 2687 BoundInfo *Bound, 2688 unsigned K) const { 2689 Bound[K].Lower[Dependence::DVEntry::EQ] = nullptr; // Default value = -infinity. 2690 Bound[K].Upper[Dependence::DVEntry::EQ] = nullptr; // Default value = +infinity. 2691 if (Bound[K].Iterations) { 2692 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff); 2693 const SCEV *NegativePart = getNegativePart(Delta); 2694 Bound[K].Lower[Dependence::DVEntry::EQ] = 2695 SE->getMulExpr(NegativePart, Bound[K].Iterations); 2696 const SCEV *PositivePart = getPositivePart(Delta); 2697 Bound[K].Upper[Dependence::DVEntry::EQ] = 2698 SE->getMulExpr(PositivePart, Bound[K].Iterations); 2699 } 2700 else { 2701 // If the positive/negative part of the difference is 0, 2702 // we won't need to know the number of iterations. 2703 const SCEV *Delta = SE->getMinusSCEV(A[K].Coeff, B[K].Coeff); 2704 const SCEV *NegativePart = getNegativePart(Delta); 2705 if (NegativePart->isZero()) 2706 Bound[K].Lower[Dependence::DVEntry::EQ] = NegativePart; // Zero 2707 const SCEV *PositivePart = getPositivePart(Delta); 2708 if (PositivePart->isZero()) 2709 Bound[K].Upper[Dependence::DVEntry::EQ] = PositivePart; // Zero 2710 } 2711 } 2712 2713 2714 // Computes the upper and lower bounds for level K 2715 // using the < direction. Records them in Bound. 2716 // Wolfe gives the equations 2717 // 2718 // LB^<_k = (A^-_k - B_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 2719 // UB^<_k = (A^+_k - B_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k - B_k N_k 2720 // 2721 // Since we normalize loops, we can simplify these equations to 2722 // 2723 // LB^<_k = (A^-_k - B_k)^- (U_k - 1) - B_k 2724 // UB^<_k = (A^+_k - B_k)^+ (U_k - 1) - B_k 2725 // 2726 // We must be careful to handle the case where the upper bound is unknown. 2727 void DependenceAnalysis::findBoundsLT(CoefficientInfo *A, 2728 CoefficientInfo *B, 2729 BoundInfo *Bound, 2730 unsigned K) const { 2731 Bound[K].Lower[Dependence::DVEntry::LT] = nullptr; // Default value = -infinity. 2732 Bound[K].Upper[Dependence::DVEntry::LT] = nullptr; // Default value = +infinity. 2733 if (Bound[K].Iterations) { 2734 const SCEV *Iter_1 = 2735 SE->getMinusSCEV(Bound[K].Iterations, 2736 SE->getConstant(Bound[K].Iterations->getType(), 1)); 2737 const SCEV *NegPart = 2738 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff)); 2739 Bound[K].Lower[Dependence::DVEntry::LT] = 2740 SE->getMinusSCEV(SE->getMulExpr(NegPart, Iter_1), B[K].Coeff); 2741 const SCEV *PosPart = 2742 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff)); 2743 Bound[K].Upper[Dependence::DVEntry::LT] = 2744 SE->getMinusSCEV(SE->getMulExpr(PosPart, Iter_1), B[K].Coeff); 2745 } 2746 else { 2747 // If the positive/negative part of the difference is 0, 2748 // we won't need to know the number of iterations. 2749 const SCEV *NegPart = 2750 getNegativePart(SE->getMinusSCEV(A[K].NegPart, B[K].Coeff)); 2751 if (NegPart->isZero()) 2752 Bound[K].Lower[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff); 2753 const SCEV *PosPart = 2754 getPositivePart(SE->getMinusSCEV(A[K].PosPart, B[K].Coeff)); 2755 if (PosPart->isZero()) 2756 Bound[K].Upper[Dependence::DVEntry::LT] = SE->getNegativeSCEV(B[K].Coeff); 2757 } 2758 } 2759 2760 2761 // Computes the upper and lower bounds for level K 2762 // using the > direction. Records them in Bound. 2763 // Wolfe gives the equations 2764 // 2765 // LB^>_k = (A_k - B^+_k)^- (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k 2766 // UB^>_k = (A_k - B^-_k)^+ (U_k - L_k - N_k) + (A_k - B_k)L_k + A_k N_k 2767 // 2768 // Since we normalize loops, we can simplify these equations to 2769 // 2770 // LB^>_k = (A_k - B^+_k)^- (U_k - 1) + A_k 2771 // UB^>_k = (A_k - B^-_k)^+ (U_k - 1) + A_k 2772 // 2773 // We must be careful to handle the case where the upper bound is unknown. 2774 void DependenceAnalysis::findBoundsGT(CoefficientInfo *A, 2775 CoefficientInfo *B, 2776 BoundInfo *Bound, 2777 unsigned K) const { 2778 Bound[K].Lower[Dependence::DVEntry::GT] = nullptr; // Default value = -infinity. 2779 Bound[K].Upper[Dependence::DVEntry::GT] = nullptr; // Default value = +infinity. 2780 if (Bound[K].Iterations) { 2781 const SCEV *Iter_1 = 2782 SE->getMinusSCEV(Bound[K].Iterations, 2783 SE->getConstant(Bound[K].Iterations->getType(), 1)); 2784 const SCEV *NegPart = 2785 getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart)); 2786 Bound[K].Lower[Dependence::DVEntry::GT] = 2787 SE->getAddExpr(SE->getMulExpr(NegPart, Iter_1), A[K].Coeff); 2788 const SCEV *PosPart = 2789 getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart)); 2790 Bound[K].Upper[Dependence::DVEntry::GT] = 2791 SE->getAddExpr(SE->getMulExpr(PosPart, Iter_1), A[K].Coeff); 2792 } 2793 else { 2794 // If the positive/negative part of the difference is 0, 2795 // we won't need to know the number of iterations. 2796 const SCEV *NegPart = getNegativePart(SE->getMinusSCEV(A[K].Coeff, B[K].PosPart)); 2797 if (NegPart->isZero()) 2798 Bound[K].Lower[Dependence::DVEntry::GT] = A[K].Coeff; 2799 const SCEV *PosPart = getPositivePart(SE->getMinusSCEV(A[K].Coeff, B[K].NegPart)); 2800 if (PosPart->isZero()) 2801 Bound[K].Upper[Dependence::DVEntry::GT] = A[K].Coeff; 2802 } 2803 } 2804 2805 2806 // X^+ = max(X, 0) 2807 const SCEV *DependenceAnalysis::getPositivePart(const SCEV *X) const { 2808 return SE->getSMaxExpr(X, SE->getConstant(X->getType(), 0)); 2809 } 2810 2811 2812 // X^- = min(X, 0) 2813 const SCEV *DependenceAnalysis::getNegativePart(const SCEV *X) const { 2814 return SE->getSMinExpr(X, SE->getConstant(X->getType(), 0)); 2815 } 2816 2817 2818 // Walks through the subscript, 2819 // collecting each coefficient, the associated loop bounds, 2820 // and recording its positive and negative parts for later use. 2821 DependenceAnalysis::CoefficientInfo * 2822 DependenceAnalysis::collectCoeffInfo(const SCEV *Subscript, 2823 bool SrcFlag, 2824 const SCEV *&Constant) const { 2825 const SCEV *Zero = SE->getConstant(Subscript->getType(), 0); 2826 CoefficientInfo *CI = new CoefficientInfo[MaxLevels + 1]; 2827 for (unsigned K = 1; K <= MaxLevels; ++K) { 2828 CI[K].Coeff = Zero; 2829 CI[K].PosPart = Zero; 2830 CI[K].NegPart = Zero; 2831 CI[K].Iterations = nullptr; 2832 } 2833 while (const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Subscript)) { 2834 const Loop *L = AddRec->getLoop(); 2835 unsigned K = SrcFlag ? mapSrcLoop(L) : mapDstLoop(L); 2836 CI[K].Coeff = AddRec->getStepRecurrence(*SE); 2837 CI[K].PosPart = getPositivePart(CI[K].Coeff); 2838 CI[K].NegPart = getNegativePart(CI[K].Coeff); 2839 CI[K].Iterations = collectUpperBound(L, Subscript->getType()); 2840 Subscript = AddRec->getStart(); 2841 } 2842 Constant = Subscript; 2843 #ifndef NDEBUG 2844 DEBUG(dbgs() << "\tCoefficient Info\n"); 2845 for (unsigned K = 1; K <= MaxLevels; ++K) { 2846 DEBUG(dbgs() << "\t " << K << "\t" << *CI[K].Coeff); 2847 DEBUG(dbgs() << "\tPos Part = "); 2848 DEBUG(dbgs() << *CI[K].PosPart); 2849 DEBUG(dbgs() << "\tNeg Part = "); 2850 DEBUG(dbgs() << *CI[K].NegPart); 2851 DEBUG(dbgs() << "\tUpper Bound = "); 2852 if (CI[K].Iterations) 2853 DEBUG(dbgs() << *CI[K].Iterations); 2854 else 2855 DEBUG(dbgs() << "+inf"); 2856 DEBUG(dbgs() << '\n'); 2857 } 2858 DEBUG(dbgs() << "\t Constant = " << *Subscript << '\n'); 2859 #endif 2860 return CI; 2861 } 2862 2863 2864 // Looks through all the bounds info and 2865 // computes the lower bound given the current direction settings 2866 // at each level. If the lower bound for any level is -inf, 2867 // the result is -inf. 2868 const SCEV *DependenceAnalysis::getLowerBound(BoundInfo *Bound) const { 2869 const SCEV *Sum = Bound[1].Lower[Bound[1].Direction]; 2870 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { 2871 if (Bound[K].Lower[Bound[K].Direction]) 2872 Sum = SE->getAddExpr(Sum, Bound[K].Lower[Bound[K].Direction]); 2873 else 2874 Sum = nullptr; 2875 } 2876 return Sum; 2877 } 2878 2879 2880 // Looks through all the bounds info and 2881 // computes the upper bound given the current direction settings 2882 // at each level. If the upper bound at any level is +inf, 2883 // the result is +inf. 2884 const SCEV *DependenceAnalysis::getUpperBound(BoundInfo *Bound) const { 2885 const SCEV *Sum = Bound[1].Upper[Bound[1].Direction]; 2886 for (unsigned K = 2; Sum && K <= MaxLevels; ++K) { 2887 if (Bound[K].Upper[Bound[K].Direction]) 2888 Sum = SE->getAddExpr(Sum, Bound[K].Upper[Bound[K].Direction]); 2889 else 2890 Sum = nullptr; 2891 } 2892 return Sum; 2893 } 2894 2895 2896 //===----------------------------------------------------------------------===// 2897 // Constraint manipulation for Delta test. 2898 2899 // Given a linear SCEV, 2900 // return the coefficient (the step) 2901 // corresponding to the specified loop. 2902 // If there isn't one, return 0. 2903 // For example, given a*i + b*j + c*k, zeroing the coefficient 2904 // corresponding to the j loop would yield b. 2905 const SCEV *DependenceAnalysis::findCoefficient(const SCEV *Expr, 2906 const Loop *TargetLoop) const { 2907 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 2908 if (!AddRec) 2909 return SE->getConstant(Expr->getType(), 0); 2910 if (AddRec->getLoop() == TargetLoop) 2911 return AddRec->getStepRecurrence(*SE); 2912 return findCoefficient(AddRec->getStart(), TargetLoop); 2913 } 2914 2915 2916 // Given a linear SCEV, 2917 // return the SCEV given by zeroing out the coefficient 2918 // corresponding to the specified loop. 2919 // For example, given a*i + b*j + c*k, zeroing the coefficient 2920 // corresponding to the j loop would yield a*i + c*k. 2921 const SCEV *DependenceAnalysis::zeroCoefficient(const SCEV *Expr, 2922 const Loop *TargetLoop) const { 2923 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 2924 if (!AddRec) 2925 return Expr; // ignore 2926 if (AddRec->getLoop() == TargetLoop) 2927 return AddRec->getStart(); 2928 return SE->getAddRecExpr(zeroCoefficient(AddRec->getStart(), TargetLoop), 2929 AddRec->getStepRecurrence(*SE), 2930 AddRec->getLoop(), 2931 AddRec->getNoWrapFlags()); 2932 } 2933 2934 2935 // Given a linear SCEV Expr, 2936 // return the SCEV given by adding some Value to the 2937 // coefficient corresponding to the specified TargetLoop. 2938 // For example, given a*i + b*j + c*k, adding 1 to the coefficient 2939 // corresponding to the j loop would yield a*i + (b+1)*j + c*k. 2940 const SCEV *DependenceAnalysis::addToCoefficient(const SCEV *Expr, 2941 const Loop *TargetLoop, 2942 const SCEV *Value) const { 2943 const SCEVAddRecExpr *AddRec = dyn_cast<SCEVAddRecExpr>(Expr); 2944 if (!AddRec) // create a new addRec 2945 return SE->getAddRecExpr(Expr, 2946 Value, 2947 TargetLoop, 2948 SCEV::FlagAnyWrap); // Worst case, with no info. 2949 if (AddRec->getLoop() == TargetLoop) { 2950 const SCEV *Sum = SE->getAddExpr(AddRec->getStepRecurrence(*SE), Value); 2951 if (Sum->isZero()) 2952 return AddRec->getStart(); 2953 return SE->getAddRecExpr(AddRec->getStart(), 2954 Sum, 2955 AddRec->getLoop(), 2956 AddRec->getNoWrapFlags()); 2957 } 2958 if (SE->isLoopInvariant(AddRec, TargetLoop)) 2959 return SE->getAddRecExpr(AddRec, 2960 Value, 2961 TargetLoop, 2962 SCEV::FlagAnyWrap); 2963 return SE->getAddRecExpr(addToCoefficient(AddRec->getStart(), 2964 TargetLoop, Value), 2965 AddRec->getStepRecurrence(*SE), 2966 AddRec->getLoop(), 2967 AddRec->getNoWrapFlags()); 2968 } 2969 2970 2971 // Review the constraints, looking for opportunities 2972 // to simplify a subscript pair (Src and Dst). 2973 // Return true if some simplification occurs. 2974 // If the simplification isn't exact (that is, if it is conservative 2975 // in terms of dependence), set consistent to false. 2976 // Corresponds to Figure 5 from the paper 2977 // 2978 // Practical Dependence Testing 2979 // Goff, Kennedy, Tseng 2980 // PLDI 1991 2981 bool DependenceAnalysis::propagate(const SCEV *&Src, 2982 const SCEV *&Dst, 2983 SmallBitVector &Loops, 2984 SmallVectorImpl<Constraint> &Constraints, 2985 bool &Consistent) { 2986 bool Result = false; 2987 for (int LI = Loops.find_first(); LI >= 0; LI = Loops.find_next(LI)) { 2988 DEBUG(dbgs() << "\t Constraint[" << LI << "] is"); 2989 DEBUG(Constraints[LI].dump(dbgs())); 2990 if (Constraints[LI].isDistance()) 2991 Result |= propagateDistance(Src, Dst, Constraints[LI], Consistent); 2992 else if (Constraints[LI].isLine()) 2993 Result |= propagateLine(Src, Dst, Constraints[LI], Consistent); 2994 else if (Constraints[LI].isPoint()) 2995 Result |= propagatePoint(Src, Dst, Constraints[LI]); 2996 } 2997 return Result; 2998 } 2999 3000 3001 // Attempt to propagate a distance 3002 // constraint into a subscript pair (Src and Dst). 3003 // Return true if some simplification occurs. 3004 // If the simplification isn't exact (that is, if it is conservative 3005 // in terms of dependence), set consistent to false. 3006 bool DependenceAnalysis::propagateDistance(const SCEV *&Src, 3007 const SCEV *&Dst, 3008 Constraint &CurConstraint, 3009 bool &Consistent) { 3010 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 3011 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n"); 3012 const SCEV *A_K = findCoefficient(Src, CurLoop); 3013 if (A_K->isZero()) 3014 return false; 3015 const SCEV *DA_K = SE->getMulExpr(A_K, CurConstraint.getD()); 3016 Src = SE->getMinusSCEV(Src, DA_K); 3017 Src = zeroCoefficient(Src, CurLoop); 3018 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n"); 3019 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n"); 3020 Dst = addToCoefficient(Dst, CurLoop, SE->getNegativeSCEV(A_K)); 3021 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n"); 3022 if (!findCoefficient(Dst, CurLoop)->isZero()) 3023 Consistent = false; 3024 return true; 3025 } 3026 3027 3028 // Attempt to propagate a line 3029 // constraint into a subscript pair (Src and Dst). 3030 // Return true if some simplification occurs. 3031 // If the simplification isn't exact (that is, if it is conservative 3032 // in terms of dependence), set consistent to false. 3033 bool DependenceAnalysis::propagateLine(const SCEV *&Src, 3034 const SCEV *&Dst, 3035 Constraint &CurConstraint, 3036 bool &Consistent) { 3037 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 3038 const SCEV *A = CurConstraint.getA(); 3039 const SCEV *B = CurConstraint.getB(); 3040 const SCEV *C = CurConstraint.getC(); 3041 DEBUG(dbgs() << "\t\tA = " << *A << ", B = " << *B << ", C = " << *C << "\n"); 3042 DEBUG(dbgs() << "\t\tSrc = " << *Src << "\n"); 3043 DEBUG(dbgs() << "\t\tDst = " << *Dst << "\n"); 3044 if (A->isZero()) { 3045 const SCEVConstant *Bconst = dyn_cast<SCEVConstant>(B); 3046 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 3047 if (!Bconst || !Cconst) return false; 3048 APInt Beta = Bconst->getValue()->getValue(); 3049 APInt Charlie = Cconst->getValue()->getValue(); 3050 APInt CdivB = Charlie.sdiv(Beta); 3051 assert(Charlie.srem(Beta) == 0 && "C should be evenly divisible by B"); 3052 const SCEV *AP_K = findCoefficient(Dst, CurLoop); 3053 // Src = SE->getAddExpr(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB))); 3054 Src = SE->getMinusSCEV(Src, SE->getMulExpr(AP_K, SE->getConstant(CdivB))); 3055 Dst = zeroCoefficient(Dst, CurLoop); 3056 if (!findCoefficient(Src, CurLoop)->isZero()) 3057 Consistent = false; 3058 } 3059 else if (B->isZero()) { 3060 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A); 3061 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 3062 if (!Aconst || !Cconst) return false; 3063 APInt Alpha = Aconst->getValue()->getValue(); 3064 APInt Charlie = Cconst->getValue()->getValue(); 3065 APInt CdivA = Charlie.sdiv(Alpha); 3066 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A"); 3067 const SCEV *A_K = findCoefficient(Src, CurLoop); 3068 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA))); 3069 Src = zeroCoefficient(Src, CurLoop); 3070 if (!findCoefficient(Dst, CurLoop)->isZero()) 3071 Consistent = false; 3072 } 3073 else if (isKnownPredicate(CmpInst::ICMP_EQ, A, B)) { 3074 const SCEVConstant *Aconst = dyn_cast<SCEVConstant>(A); 3075 const SCEVConstant *Cconst = dyn_cast<SCEVConstant>(C); 3076 if (!Aconst || !Cconst) return false; 3077 APInt Alpha = Aconst->getValue()->getValue(); 3078 APInt Charlie = Cconst->getValue()->getValue(); 3079 APInt CdivA = Charlie.sdiv(Alpha); 3080 assert(Charlie.srem(Alpha) == 0 && "C should be evenly divisible by A"); 3081 const SCEV *A_K = findCoefficient(Src, CurLoop); 3082 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, SE->getConstant(CdivA))); 3083 Src = zeroCoefficient(Src, CurLoop); 3084 Dst = addToCoefficient(Dst, CurLoop, A_K); 3085 if (!findCoefficient(Dst, CurLoop)->isZero()) 3086 Consistent = false; 3087 } 3088 else { 3089 // paper is incorrect here, or perhaps just misleading 3090 const SCEV *A_K = findCoefficient(Src, CurLoop); 3091 Src = SE->getMulExpr(Src, A); 3092 Dst = SE->getMulExpr(Dst, A); 3093 Src = SE->getAddExpr(Src, SE->getMulExpr(A_K, C)); 3094 Src = zeroCoefficient(Src, CurLoop); 3095 Dst = addToCoefficient(Dst, CurLoop, SE->getMulExpr(A_K, B)); 3096 if (!findCoefficient(Dst, CurLoop)->isZero()) 3097 Consistent = false; 3098 } 3099 DEBUG(dbgs() << "\t\tnew Src = " << *Src << "\n"); 3100 DEBUG(dbgs() << "\t\tnew Dst = " << *Dst << "\n"); 3101 return true; 3102 } 3103 3104 3105 // Attempt to propagate a point 3106 // constraint into a subscript pair (Src and Dst). 3107 // Return true if some simplification occurs. 3108 bool DependenceAnalysis::propagatePoint(const SCEV *&Src, 3109 const SCEV *&Dst, 3110 Constraint &CurConstraint) { 3111 const Loop *CurLoop = CurConstraint.getAssociatedLoop(); 3112 const SCEV *A_K = findCoefficient(Src, CurLoop); 3113 const SCEV *AP_K = findCoefficient(Dst, CurLoop); 3114 const SCEV *XA_K = SE->getMulExpr(A_K, CurConstraint.getX()); 3115 const SCEV *YAP_K = SE->getMulExpr(AP_K, CurConstraint.getY()); 3116 DEBUG(dbgs() << "\t\tSrc is " << *Src << "\n"); 3117 Src = SE->getAddExpr(Src, SE->getMinusSCEV(XA_K, YAP_K)); 3118 Src = zeroCoefficient(Src, CurLoop); 3119 DEBUG(dbgs() << "\t\tnew Src is " << *Src << "\n"); 3120 DEBUG(dbgs() << "\t\tDst is " << *Dst << "\n"); 3121 Dst = zeroCoefficient(Dst, CurLoop); 3122 DEBUG(dbgs() << "\t\tnew Dst is " << *Dst << "\n"); 3123 return true; 3124 } 3125 3126 3127 // Update direction vector entry based on the current constraint. 3128 void DependenceAnalysis::updateDirection(Dependence::DVEntry &Level, 3129 const Constraint &CurConstraint 3130 ) const { 3131 DEBUG(dbgs() << "\tUpdate direction, constraint ="); 3132 DEBUG(CurConstraint.dump(dbgs())); 3133 if (CurConstraint.isAny()) 3134 ; // use defaults 3135 else if (CurConstraint.isDistance()) { 3136 // this one is consistent, the others aren't 3137 Level.Scalar = false; 3138 Level.Distance = CurConstraint.getD(); 3139 unsigned NewDirection = Dependence::DVEntry::NONE; 3140 if (!SE->isKnownNonZero(Level.Distance)) // if may be zero 3141 NewDirection = Dependence::DVEntry::EQ; 3142 if (!SE->isKnownNonPositive(Level.Distance)) // if may be positive 3143 NewDirection |= Dependence::DVEntry::LT; 3144 if (!SE->isKnownNonNegative(Level.Distance)) // if may be negative 3145 NewDirection |= Dependence::DVEntry::GT; 3146 Level.Direction &= NewDirection; 3147 } 3148 else if (CurConstraint.isLine()) { 3149 Level.Scalar = false; 3150 Level.Distance = nullptr; 3151 // direction should be accurate 3152 } 3153 else if (CurConstraint.isPoint()) { 3154 Level.Scalar = false; 3155 Level.Distance = nullptr; 3156 unsigned NewDirection = Dependence::DVEntry::NONE; 3157 if (!isKnownPredicate(CmpInst::ICMP_NE, 3158 CurConstraint.getY(), 3159 CurConstraint.getX())) 3160 // if X may be = Y 3161 NewDirection |= Dependence::DVEntry::EQ; 3162 if (!isKnownPredicate(CmpInst::ICMP_SLE, 3163 CurConstraint.getY(), 3164 CurConstraint.getX())) 3165 // if Y may be > X 3166 NewDirection |= Dependence::DVEntry::LT; 3167 if (!isKnownPredicate(CmpInst::ICMP_SGE, 3168 CurConstraint.getY(), 3169 CurConstraint.getX())) 3170 // if Y may be < X 3171 NewDirection |= Dependence::DVEntry::GT; 3172 Level.Direction &= NewDirection; 3173 } 3174 else 3175 llvm_unreachable("constraint has unexpected kind"); 3176 } 3177 3178 /// Check if we can delinearize the subscripts. If the SCEVs representing the 3179 /// source and destination array references are recurrences on a nested loop, 3180 /// this function flattens the nested recurrences into separate recurrences 3181 /// for each loop level. 3182 bool DependenceAnalysis::tryDelinearize(const SCEV *SrcSCEV, 3183 const SCEV *DstSCEV, 3184 SmallVectorImpl<Subscript> &Pair, 3185 const SCEV *ElementSize) const { 3186 const SCEVUnknown *SrcBase = 3187 dyn_cast<SCEVUnknown>(SE->getPointerBase(SrcSCEV)); 3188 const SCEVUnknown *DstBase = 3189 dyn_cast<SCEVUnknown>(SE->getPointerBase(DstSCEV)); 3190 3191 if (!SrcBase || !DstBase || SrcBase != DstBase) 3192 return false; 3193 3194 SrcSCEV = SE->getMinusSCEV(SrcSCEV, SrcBase); 3195 DstSCEV = SE->getMinusSCEV(DstSCEV, DstBase); 3196 3197 const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV); 3198 const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV); 3199 if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine()) 3200 return false; 3201 3202 // First step: collect parametric terms in both array references. 3203 SmallVector<const SCEV *, 4> Terms; 3204 SrcAR->collectParametricTerms(*SE, Terms); 3205 DstAR->collectParametricTerms(*SE, Terms); 3206 3207 // Second step: find subscript sizes. 3208 SmallVector<const SCEV *, 4> Sizes; 3209 SE->findArrayDimensions(Terms, Sizes, ElementSize); 3210 3211 // Third step: compute the access functions for each subscript. 3212 SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts; 3213 SrcAR->computeAccessFunctions(*SE, SrcSubscripts, Sizes); 3214 DstAR->computeAccessFunctions(*SE, DstSubscripts, Sizes); 3215 3216 // Fail when there is only a subscript: that's a linearized access function. 3217 if (SrcSubscripts.size() < 2 || DstSubscripts.size() < 2 || 3218 SrcSubscripts.size() != DstSubscripts.size()) 3219 return false; 3220 3221 int size = SrcSubscripts.size(); 3222 3223 DEBUG({ 3224 dbgs() << "\nSrcSubscripts: "; 3225 for (int i = 0; i < size; i++) 3226 dbgs() << *SrcSubscripts[i]; 3227 dbgs() << "\nDstSubscripts: "; 3228 for (int i = 0; i < size; i++) 3229 dbgs() << *DstSubscripts[i]; 3230 }); 3231 3232 // The delinearization transforms a single-subscript MIV dependence test into 3233 // a multi-subscript SIV dependence test that is easier to compute. So we 3234 // resize Pair to contain as many pairs of subscripts as the delinearization 3235 // has found, and then initialize the pairs following the delinearization. 3236 Pair.resize(size); 3237 for (int i = 0; i < size; ++i) { 3238 Pair[i].Src = SrcSubscripts[i]; 3239 Pair[i].Dst = DstSubscripts[i]; 3240 3241 // FIXME: we should record the bounds SrcSizes[i] and DstSizes[i] that the 3242 // delinearization has found, and add these constraints to the dependence 3243 // check to avoid memory accesses overflow from one dimension into another. 3244 // This is related to the problem of determining the existence of data 3245 // dependences in array accesses using a different number of subscripts: in 3246 // C one can access an array A[100][100]; as A[0][9999], *A[9999], etc. 3247 } 3248 3249 return true; 3250 } 3251 3252 //===----------------------------------------------------------------------===// 3253 3254 #ifndef NDEBUG 3255 // For debugging purposes, dump a small bit vector to dbgs(). 3256 static void dumpSmallBitVector(SmallBitVector &BV) { 3257 dbgs() << "{"; 3258 for (int VI = BV.find_first(); VI >= 0; VI = BV.find_next(VI)) { 3259 dbgs() << VI; 3260 if (BV.find_next(VI) >= 0) 3261 dbgs() << ' '; 3262 } 3263 dbgs() << "}\n"; 3264 } 3265 #endif 3266 3267 3268 // depends - 3269 // Returns NULL if there is no dependence. 3270 // Otherwise, return a Dependence with as many details as possible. 3271 // Corresponds to Section 3.1 in the paper 3272 // 3273 // Practical Dependence Testing 3274 // Goff, Kennedy, Tseng 3275 // PLDI 1991 3276 // 3277 // Care is required to keep the routine below, getSplitIteration(), 3278 // up to date with respect to this routine. 3279 std::unique_ptr<Dependence> 3280 DependenceAnalysis::depends(Instruction *Src, Instruction *Dst, 3281 bool PossiblyLoopIndependent) { 3282 if (Src == Dst) 3283 PossiblyLoopIndependent = false; 3284 3285 if ((!Src->mayReadFromMemory() && !Src->mayWriteToMemory()) || 3286 (!Dst->mayReadFromMemory() && !Dst->mayWriteToMemory())) 3287 // if both instructions don't reference memory, there's no dependence 3288 return nullptr; 3289 3290 if (!isLoadOrStore(Src) || !isLoadOrStore(Dst)) { 3291 // can only analyze simple loads and stores, i.e., no calls, invokes, etc. 3292 DEBUG(dbgs() << "can only handle simple loads and stores\n"); 3293 return make_unique<Dependence>(Src, Dst); 3294 } 3295 3296 Value *SrcPtr = getPointerOperand(Src); 3297 Value *DstPtr = getPointerOperand(Dst); 3298 3299 switch (underlyingObjectsAlias(AA, DstPtr, SrcPtr)) { 3300 case AliasAnalysis::MayAlias: 3301 case AliasAnalysis::PartialAlias: 3302 // cannot analyse objects if we don't understand their aliasing. 3303 DEBUG(dbgs() << "can't analyze may or partial alias\n"); 3304 return make_unique<Dependence>(Src, Dst); 3305 case AliasAnalysis::NoAlias: 3306 // If the objects noalias, they are distinct, accesses are independent. 3307 DEBUG(dbgs() << "no alias\n"); 3308 return nullptr; 3309 case AliasAnalysis::MustAlias: 3310 break; // The underlying objects alias; test accesses for dependence. 3311 } 3312 3313 // establish loop nesting levels 3314 establishNestingLevels(Src, Dst); 3315 DEBUG(dbgs() << " common nesting levels = " << CommonLevels << "\n"); 3316 DEBUG(dbgs() << " maximum nesting levels = " << MaxLevels << "\n"); 3317 3318 FullDependence Result(Src, Dst, PossiblyLoopIndependent, CommonLevels); 3319 ++TotalArrayPairs; 3320 3321 // See if there are GEPs we can use. 3322 bool UsefulGEP = false; 3323 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr); 3324 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr); 3325 if (SrcGEP && DstGEP && 3326 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) { 3327 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand()); 3328 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand()); 3329 DEBUG(dbgs() << " SrcPtrSCEV = " << *SrcPtrSCEV << "\n"); 3330 DEBUG(dbgs() << " DstPtrSCEV = " << *DstPtrSCEV << "\n"); 3331 3332 UsefulGEP = 3333 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) && 3334 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())); 3335 } 3336 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1; 3337 SmallVector<Subscript, 4> Pair(Pairs); 3338 if (UsefulGEP) { 3339 DEBUG(dbgs() << " using GEPs\n"); 3340 unsigned P = 0; 3341 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(), 3342 SrcEnd = SrcGEP->idx_end(), 3343 DstIdx = DstGEP->idx_begin(); 3344 SrcIdx != SrcEnd; 3345 ++SrcIdx, ++DstIdx, ++P) { 3346 Pair[P].Src = SE->getSCEV(*SrcIdx); 3347 Pair[P].Dst = SE->getSCEV(*DstIdx); 3348 } 3349 } 3350 else { 3351 DEBUG(dbgs() << " ignoring GEPs\n"); 3352 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr); 3353 const SCEV *DstSCEV = SE->getSCEV(DstPtr); 3354 DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n"); 3355 DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n"); 3356 Pair[0].Src = SrcSCEV; 3357 Pair[0].Dst = DstSCEV; 3358 } 3359 3360 if (Delinearize && Pairs == 1 && CommonLevels > 1 && 3361 tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) { 3362 DEBUG(dbgs() << " delinerized GEP\n"); 3363 Pairs = Pair.size(); 3364 } 3365 3366 for (unsigned P = 0; P < Pairs; ++P) { 3367 Pair[P].Loops.resize(MaxLevels + 1); 3368 Pair[P].GroupLoops.resize(MaxLevels + 1); 3369 Pair[P].Group.resize(Pairs); 3370 removeMatchingExtensions(&Pair[P]); 3371 Pair[P].Classification = 3372 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), 3373 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), 3374 Pair[P].Loops); 3375 Pair[P].GroupLoops = Pair[P].Loops; 3376 Pair[P].Group.set(P); 3377 DEBUG(dbgs() << " subscript " << P << "\n"); 3378 DEBUG(dbgs() << "\tsrc = " << *Pair[P].Src << "\n"); 3379 DEBUG(dbgs() << "\tdst = " << *Pair[P].Dst << "\n"); 3380 DEBUG(dbgs() << "\tclass = " << Pair[P].Classification << "\n"); 3381 DEBUG(dbgs() << "\tloops = "); 3382 DEBUG(dumpSmallBitVector(Pair[P].Loops)); 3383 } 3384 3385 SmallBitVector Separable(Pairs); 3386 SmallBitVector Coupled(Pairs); 3387 3388 // Partition subscripts into separable and minimally-coupled groups 3389 // Algorithm in paper is algorithmically better; 3390 // this may be faster in practice. Check someday. 3391 // 3392 // Here's an example of how it works. Consider this code: 3393 // 3394 // for (i = ...) { 3395 // for (j = ...) { 3396 // for (k = ...) { 3397 // for (l = ...) { 3398 // for (m = ...) { 3399 // A[i][j][k][m] = ...; 3400 // ... = A[0][j][l][i + j]; 3401 // } 3402 // } 3403 // } 3404 // } 3405 // } 3406 // 3407 // There are 4 subscripts here: 3408 // 0 [i] and [0] 3409 // 1 [j] and [j] 3410 // 2 [k] and [l] 3411 // 3 [m] and [i + j] 3412 // 3413 // We've already classified each subscript pair as ZIV, SIV, etc., 3414 // and collected all the loops mentioned by pair P in Pair[P].Loops. 3415 // In addition, we've initialized Pair[P].GroupLoops to Pair[P].Loops 3416 // and set Pair[P].Group = {P}. 3417 // 3418 // Src Dst Classification Loops GroupLoops Group 3419 // 0 [i] [0] SIV {1} {1} {0} 3420 // 1 [j] [j] SIV {2} {2} {1} 3421 // 2 [k] [l] RDIV {3,4} {3,4} {2} 3422 // 3 [m] [i + j] MIV {1,2,5} {1,2,5} {3} 3423 // 3424 // For each subscript SI 0 .. 3, we consider each remaining subscript, SJ. 3425 // So, 0 is compared against 1, 2, and 3; 1 is compared against 2 and 3, etc. 3426 // 3427 // We begin by comparing 0 and 1. The intersection of the GroupLoops is empty. 3428 // Next, 0 and 2. Again, the intersection of their GroupLoops is empty. 3429 // Next 0 and 3. The intersection of their GroupLoop = {1}, not empty, 3430 // so Pair[3].Group = {0,3} and Done = false (that is, 0 will not be added 3431 // to either Separable or Coupled). 3432 // 3433 // Next, we consider 1 and 2. The intersection of the GroupLoops is empty. 3434 // Next, 1 and 3. The intersectionof their GroupLoops = {2}, not empty, 3435 // so Pair[3].Group = {0, 1, 3} and Done = false. 3436 // 3437 // Next, we compare 2 against 3. The intersection of the GroupLoops is empty. 3438 // Since Done remains true, we add 2 to the set of Separable pairs. 3439 // 3440 // Finally, we consider 3. There's nothing to compare it with, 3441 // so Done remains true and we add it to the Coupled set. 3442 // Pair[3].Group = {0, 1, 3} and GroupLoops = {1, 2, 5}. 3443 // 3444 // In the end, we've got 1 separable subscript and 1 coupled group. 3445 for (unsigned SI = 0; SI < Pairs; ++SI) { 3446 if (Pair[SI].Classification == Subscript::NonLinear) { 3447 // ignore these, but collect loops for later 3448 ++NonlinearSubscriptPairs; 3449 collectCommonLoops(Pair[SI].Src, 3450 LI->getLoopFor(Src->getParent()), 3451 Pair[SI].Loops); 3452 collectCommonLoops(Pair[SI].Dst, 3453 LI->getLoopFor(Dst->getParent()), 3454 Pair[SI].Loops); 3455 Result.Consistent = false; 3456 } 3457 else if (Pair[SI].Classification == Subscript::ZIV) { 3458 // always separable 3459 Separable.set(SI); 3460 } 3461 else { 3462 // SIV, RDIV, or MIV, so check for coupled group 3463 bool Done = true; 3464 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) { 3465 SmallBitVector Intersection = Pair[SI].GroupLoops; 3466 Intersection &= Pair[SJ].GroupLoops; 3467 if (Intersection.any()) { 3468 // accumulate set of all the loops in group 3469 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops; 3470 // accumulate set of all subscripts in group 3471 Pair[SJ].Group |= Pair[SI].Group; 3472 Done = false; 3473 } 3474 } 3475 if (Done) { 3476 if (Pair[SI].Group.count() == 1) { 3477 Separable.set(SI); 3478 ++SeparableSubscriptPairs; 3479 } 3480 else { 3481 Coupled.set(SI); 3482 ++CoupledSubscriptPairs; 3483 } 3484 } 3485 } 3486 } 3487 3488 DEBUG(dbgs() << " Separable = "); 3489 DEBUG(dumpSmallBitVector(Separable)); 3490 DEBUG(dbgs() << " Coupled = "); 3491 DEBUG(dumpSmallBitVector(Coupled)); 3492 3493 Constraint NewConstraint; 3494 NewConstraint.setAny(SE); 3495 3496 // test separable subscripts 3497 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) { 3498 DEBUG(dbgs() << "testing subscript " << SI); 3499 switch (Pair[SI].Classification) { 3500 case Subscript::ZIV: 3501 DEBUG(dbgs() << ", ZIV\n"); 3502 if (testZIV(Pair[SI].Src, Pair[SI].Dst, Result)) 3503 return nullptr; 3504 break; 3505 case Subscript::SIV: { 3506 DEBUG(dbgs() << ", SIV\n"); 3507 unsigned Level; 3508 const SCEV *SplitIter = nullptr; 3509 if (testSIV(Pair[SI].Src, Pair[SI].Dst, Level, 3510 Result, NewConstraint, SplitIter)) 3511 return nullptr; 3512 break; 3513 } 3514 case Subscript::RDIV: 3515 DEBUG(dbgs() << ", RDIV\n"); 3516 if (testRDIV(Pair[SI].Src, Pair[SI].Dst, Result)) 3517 return nullptr; 3518 break; 3519 case Subscript::MIV: 3520 DEBUG(dbgs() << ", MIV\n"); 3521 if (testMIV(Pair[SI].Src, Pair[SI].Dst, Pair[SI].Loops, Result)) 3522 return nullptr; 3523 break; 3524 default: 3525 llvm_unreachable("subscript has unexpected classification"); 3526 } 3527 } 3528 3529 if (Coupled.count()) { 3530 // test coupled subscript groups 3531 DEBUG(dbgs() << "starting on coupled subscripts\n"); 3532 DEBUG(dbgs() << "MaxLevels + 1 = " << MaxLevels + 1 << "\n"); 3533 SmallVector<Constraint, 4> Constraints(MaxLevels + 1); 3534 for (unsigned II = 0; II <= MaxLevels; ++II) 3535 Constraints[II].setAny(SE); 3536 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) { 3537 DEBUG(dbgs() << "testing subscript group " << SI << " { "); 3538 SmallBitVector Group(Pair[SI].Group); 3539 SmallBitVector Sivs(Pairs); 3540 SmallBitVector Mivs(Pairs); 3541 SmallBitVector ConstrainedLevels(MaxLevels + 1); 3542 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) { 3543 DEBUG(dbgs() << SJ << " "); 3544 if (Pair[SJ].Classification == Subscript::SIV) 3545 Sivs.set(SJ); 3546 else 3547 Mivs.set(SJ); 3548 } 3549 DEBUG(dbgs() << "}\n"); 3550 while (Sivs.any()) { 3551 bool Changed = false; 3552 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) { 3553 DEBUG(dbgs() << "testing subscript " << SJ << ", SIV\n"); 3554 // SJ is an SIV subscript that's part of the current coupled group 3555 unsigned Level; 3556 const SCEV *SplitIter = nullptr; 3557 DEBUG(dbgs() << "SIV\n"); 3558 if (testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, 3559 Result, NewConstraint, SplitIter)) 3560 return nullptr; 3561 ConstrainedLevels.set(Level); 3562 if (intersectConstraints(&Constraints[Level], &NewConstraint)) { 3563 if (Constraints[Level].isEmpty()) { 3564 ++DeltaIndependence; 3565 return nullptr; 3566 } 3567 Changed = true; 3568 } 3569 Sivs.reset(SJ); 3570 } 3571 if (Changed) { 3572 // propagate, possibly creating new SIVs and ZIVs 3573 DEBUG(dbgs() << " propagating\n"); 3574 DEBUG(dbgs() << "\tMivs = "); 3575 DEBUG(dumpSmallBitVector(Mivs)); 3576 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 3577 // SJ is an MIV subscript that's part of the current coupled group 3578 DEBUG(dbgs() << "\tSJ = " << SJ << "\n"); 3579 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, 3580 Constraints, Result.Consistent)) { 3581 DEBUG(dbgs() << "\t Changed\n"); 3582 ++DeltaPropagations; 3583 Pair[SJ].Classification = 3584 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()), 3585 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()), 3586 Pair[SJ].Loops); 3587 switch (Pair[SJ].Classification) { 3588 case Subscript::ZIV: 3589 DEBUG(dbgs() << "ZIV\n"); 3590 if (testZIV(Pair[SJ].Src, Pair[SJ].Dst, Result)) 3591 return nullptr; 3592 Mivs.reset(SJ); 3593 break; 3594 case Subscript::SIV: 3595 Sivs.set(SJ); 3596 Mivs.reset(SJ); 3597 break; 3598 case Subscript::RDIV: 3599 case Subscript::MIV: 3600 break; 3601 default: 3602 llvm_unreachable("bad subscript classification"); 3603 } 3604 } 3605 } 3606 } 3607 } 3608 3609 // test & propagate remaining RDIVs 3610 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 3611 if (Pair[SJ].Classification == Subscript::RDIV) { 3612 DEBUG(dbgs() << "RDIV test\n"); 3613 if (testRDIV(Pair[SJ].Src, Pair[SJ].Dst, Result)) 3614 return nullptr; 3615 // I don't yet understand how to propagate RDIV results 3616 Mivs.reset(SJ); 3617 } 3618 } 3619 3620 // test remaining MIVs 3621 // This code is temporary. 3622 // Better to somehow test all remaining subscripts simultaneously. 3623 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 3624 if (Pair[SJ].Classification == Subscript::MIV) { 3625 DEBUG(dbgs() << "MIV test\n"); 3626 if (testMIV(Pair[SJ].Src, Pair[SJ].Dst, Pair[SJ].Loops, Result)) 3627 return nullptr; 3628 } 3629 else 3630 llvm_unreachable("expected only MIV subscripts at this point"); 3631 } 3632 3633 // update Result.DV from constraint vector 3634 DEBUG(dbgs() << " updating\n"); 3635 for (int SJ = ConstrainedLevels.find_first(); 3636 SJ >= 0; SJ = ConstrainedLevels.find_next(SJ)) { 3637 updateDirection(Result.DV[SJ - 1], Constraints[SJ]); 3638 if (Result.DV[SJ - 1].Direction == Dependence::DVEntry::NONE) 3639 return nullptr; 3640 } 3641 } 3642 } 3643 3644 // Make sure the Scalar flags are set correctly. 3645 SmallBitVector CompleteLoops(MaxLevels + 1); 3646 for (unsigned SI = 0; SI < Pairs; ++SI) 3647 CompleteLoops |= Pair[SI].Loops; 3648 for (unsigned II = 1; II <= CommonLevels; ++II) 3649 if (CompleteLoops[II]) 3650 Result.DV[II - 1].Scalar = false; 3651 3652 if (PossiblyLoopIndependent) { 3653 // Make sure the LoopIndependent flag is set correctly. 3654 // All directions must include equal, otherwise no 3655 // loop-independent dependence is possible. 3656 for (unsigned II = 1; II <= CommonLevels; ++II) { 3657 if (!(Result.getDirection(II) & Dependence::DVEntry::EQ)) { 3658 Result.LoopIndependent = false; 3659 break; 3660 } 3661 } 3662 } 3663 else { 3664 // On the other hand, if all directions are equal and there's no 3665 // loop-independent dependence possible, then no dependence exists. 3666 bool AllEqual = true; 3667 for (unsigned II = 1; II <= CommonLevels; ++II) { 3668 if (Result.getDirection(II) != Dependence::DVEntry::EQ) { 3669 AllEqual = false; 3670 break; 3671 } 3672 } 3673 if (AllEqual) 3674 return nullptr; 3675 } 3676 3677 auto Final = make_unique<FullDependence>(Result); 3678 Result.DV = nullptr; 3679 return std::move(Final); 3680 } 3681 3682 3683 3684 //===----------------------------------------------------------------------===// 3685 // getSplitIteration - 3686 // Rather than spend rarely-used space recording the splitting iteration 3687 // during the Weak-Crossing SIV test, we re-compute it on demand. 3688 // The re-computation is basically a repeat of the entire dependence test, 3689 // though simplified since we know that the dependence exists. 3690 // It's tedious, since we must go through all propagations, etc. 3691 // 3692 // Care is required to keep this code up to date with respect to the routine 3693 // above, depends(). 3694 // 3695 // Generally, the dependence analyzer will be used to build 3696 // a dependence graph for a function (basically a map from instructions 3697 // to dependences). Looking for cycles in the graph shows us loops 3698 // that cannot be trivially vectorized/parallelized. 3699 // 3700 // We can try to improve the situation by examining all the dependences 3701 // that make up the cycle, looking for ones we can break. 3702 // Sometimes, peeling the first or last iteration of a loop will break 3703 // dependences, and we've got flags for those possibilities. 3704 // Sometimes, splitting a loop at some other iteration will do the trick, 3705 // and we've got a flag for that case. Rather than waste the space to 3706 // record the exact iteration (since we rarely know), we provide 3707 // a method that calculates the iteration. It's a drag that it must work 3708 // from scratch, but wonderful in that it's possible. 3709 // 3710 // Here's an example: 3711 // 3712 // for (i = 0; i < 10; i++) 3713 // A[i] = ... 3714 // ... = A[11 - i] 3715 // 3716 // There's a loop-carried flow dependence from the store to the load, 3717 // found by the weak-crossing SIV test. The dependence will have a flag, 3718 // indicating that the dependence can be broken by splitting the loop. 3719 // Calling getSplitIteration will return 5. 3720 // Splitting the loop breaks the dependence, like so: 3721 // 3722 // for (i = 0; i <= 5; i++) 3723 // A[i] = ... 3724 // ... = A[11 - i] 3725 // for (i = 6; i < 10; i++) 3726 // A[i] = ... 3727 // ... = A[11 - i] 3728 // 3729 // breaks the dependence and allows us to vectorize/parallelize 3730 // both loops. 3731 const SCEV *DependenceAnalysis::getSplitIteration(const Dependence &Dep, 3732 unsigned SplitLevel) { 3733 assert(Dep.isSplitable(SplitLevel) && 3734 "Dep should be splitable at SplitLevel"); 3735 Instruction *Src = Dep.getSrc(); 3736 Instruction *Dst = Dep.getDst(); 3737 assert(Src->mayReadFromMemory() || Src->mayWriteToMemory()); 3738 assert(Dst->mayReadFromMemory() || Dst->mayWriteToMemory()); 3739 assert(isLoadOrStore(Src)); 3740 assert(isLoadOrStore(Dst)); 3741 Value *SrcPtr = getPointerOperand(Src); 3742 Value *DstPtr = getPointerOperand(Dst); 3743 assert(underlyingObjectsAlias(AA, DstPtr, SrcPtr) == 3744 AliasAnalysis::MustAlias); 3745 3746 // establish loop nesting levels 3747 establishNestingLevels(Src, Dst); 3748 3749 FullDependence Result(Src, Dst, false, CommonLevels); 3750 3751 // See if there are GEPs we can use. 3752 bool UsefulGEP = false; 3753 GEPOperator *SrcGEP = dyn_cast<GEPOperator>(SrcPtr); 3754 GEPOperator *DstGEP = dyn_cast<GEPOperator>(DstPtr); 3755 if (SrcGEP && DstGEP && 3756 SrcGEP->getPointerOperandType() == DstGEP->getPointerOperandType()) { 3757 const SCEV *SrcPtrSCEV = SE->getSCEV(SrcGEP->getPointerOperand()); 3758 const SCEV *DstPtrSCEV = SE->getSCEV(DstGEP->getPointerOperand()); 3759 UsefulGEP = 3760 isLoopInvariant(SrcPtrSCEV, LI->getLoopFor(Src->getParent())) && 3761 isLoopInvariant(DstPtrSCEV, LI->getLoopFor(Dst->getParent())); 3762 } 3763 unsigned Pairs = UsefulGEP ? SrcGEP->idx_end() - SrcGEP->idx_begin() : 1; 3764 SmallVector<Subscript, 4> Pair(Pairs); 3765 if (UsefulGEP) { 3766 unsigned P = 0; 3767 for (GEPOperator::const_op_iterator SrcIdx = SrcGEP->idx_begin(), 3768 SrcEnd = SrcGEP->idx_end(), 3769 DstIdx = DstGEP->idx_begin(); 3770 SrcIdx != SrcEnd; 3771 ++SrcIdx, ++DstIdx, ++P) { 3772 Pair[P].Src = SE->getSCEV(*SrcIdx); 3773 Pair[P].Dst = SE->getSCEV(*DstIdx); 3774 } 3775 } 3776 else { 3777 const SCEV *SrcSCEV = SE->getSCEV(SrcPtr); 3778 const SCEV *DstSCEV = SE->getSCEV(DstPtr); 3779 Pair[0].Src = SrcSCEV; 3780 Pair[0].Dst = DstSCEV; 3781 } 3782 3783 if (Delinearize && Pairs == 1 && CommonLevels > 1 && 3784 tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) { 3785 DEBUG(dbgs() << " delinerized GEP\n"); 3786 Pairs = Pair.size(); 3787 } 3788 3789 for (unsigned P = 0; P < Pairs; ++P) { 3790 Pair[P].Loops.resize(MaxLevels + 1); 3791 Pair[P].GroupLoops.resize(MaxLevels + 1); 3792 Pair[P].Group.resize(Pairs); 3793 removeMatchingExtensions(&Pair[P]); 3794 Pair[P].Classification = 3795 classifyPair(Pair[P].Src, LI->getLoopFor(Src->getParent()), 3796 Pair[P].Dst, LI->getLoopFor(Dst->getParent()), 3797 Pair[P].Loops); 3798 Pair[P].GroupLoops = Pair[P].Loops; 3799 Pair[P].Group.set(P); 3800 } 3801 3802 SmallBitVector Separable(Pairs); 3803 SmallBitVector Coupled(Pairs); 3804 3805 // partition subscripts into separable and minimally-coupled groups 3806 for (unsigned SI = 0; SI < Pairs; ++SI) { 3807 if (Pair[SI].Classification == Subscript::NonLinear) { 3808 // ignore these, but collect loops for later 3809 collectCommonLoops(Pair[SI].Src, 3810 LI->getLoopFor(Src->getParent()), 3811 Pair[SI].Loops); 3812 collectCommonLoops(Pair[SI].Dst, 3813 LI->getLoopFor(Dst->getParent()), 3814 Pair[SI].Loops); 3815 Result.Consistent = false; 3816 } 3817 else if (Pair[SI].Classification == Subscript::ZIV) 3818 Separable.set(SI); 3819 else { 3820 // SIV, RDIV, or MIV, so check for coupled group 3821 bool Done = true; 3822 for (unsigned SJ = SI + 1; SJ < Pairs; ++SJ) { 3823 SmallBitVector Intersection = Pair[SI].GroupLoops; 3824 Intersection &= Pair[SJ].GroupLoops; 3825 if (Intersection.any()) { 3826 // accumulate set of all the loops in group 3827 Pair[SJ].GroupLoops |= Pair[SI].GroupLoops; 3828 // accumulate set of all subscripts in group 3829 Pair[SJ].Group |= Pair[SI].Group; 3830 Done = false; 3831 } 3832 } 3833 if (Done) { 3834 if (Pair[SI].Group.count() == 1) 3835 Separable.set(SI); 3836 else 3837 Coupled.set(SI); 3838 } 3839 } 3840 } 3841 3842 Constraint NewConstraint; 3843 NewConstraint.setAny(SE); 3844 3845 // test separable subscripts 3846 for (int SI = Separable.find_first(); SI >= 0; SI = Separable.find_next(SI)) { 3847 switch (Pair[SI].Classification) { 3848 case Subscript::SIV: { 3849 unsigned Level; 3850 const SCEV *SplitIter = nullptr; 3851 (void) testSIV(Pair[SI].Src, Pair[SI].Dst, Level, 3852 Result, NewConstraint, SplitIter); 3853 if (Level == SplitLevel) { 3854 assert(SplitIter != nullptr); 3855 return SplitIter; 3856 } 3857 break; 3858 } 3859 case Subscript::ZIV: 3860 case Subscript::RDIV: 3861 case Subscript::MIV: 3862 break; 3863 default: 3864 llvm_unreachable("subscript has unexpected classification"); 3865 } 3866 } 3867 3868 if (Coupled.count()) { 3869 // test coupled subscript groups 3870 SmallVector<Constraint, 4> Constraints(MaxLevels + 1); 3871 for (unsigned II = 0; II <= MaxLevels; ++II) 3872 Constraints[II].setAny(SE); 3873 for (int SI = Coupled.find_first(); SI >= 0; SI = Coupled.find_next(SI)) { 3874 SmallBitVector Group(Pair[SI].Group); 3875 SmallBitVector Sivs(Pairs); 3876 SmallBitVector Mivs(Pairs); 3877 SmallBitVector ConstrainedLevels(MaxLevels + 1); 3878 for (int SJ = Group.find_first(); SJ >= 0; SJ = Group.find_next(SJ)) { 3879 if (Pair[SJ].Classification == Subscript::SIV) 3880 Sivs.set(SJ); 3881 else 3882 Mivs.set(SJ); 3883 } 3884 while (Sivs.any()) { 3885 bool Changed = false; 3886 for (int SJ = Sivs.find_first(); SJ >= 0; SJ = Sivs.find_next(SJ)) { 3887 // SJ is an SIV subscript that's part of the current coupled group 3888 unsigned Level; 3889 const SCEV *SplitIter = nullptr; 3890 (void) testSIV(Pair[SJ].Src, Pair[SJ].Dst, Level, 3891 Result, NewConstraint, SplitIter); 3892 if (Level == SplitLevel && SplitIter) 3893 return SplitIter; 3894 ConstrainedLevels.set(Level); 3895 if (intersectConstraints(&Constraints[Level], &NewConstraint)) 3896 Changed = true; 3897 Sivs.reset(SJ); 3898 } 3899 if (Changed) { 3900 // propagate, possibly creating new SIVs and ZIVs 3901 for (int SJ = Mivs.find_first(); SJ >= 0; SJ = Mivs.find_next(SJ)) { 3902 // SJ is an MIV subscript that's part of the current coupled group 3903 if (propagate(Pair[SJ].Src, Pair[SJ].Dst, 3904 Pair[SJ].Loops, Constraints, Result.Consistent)) { 3905 Pair[SJ].Classification = 3906 classifyPair(Pair[SJ].Src, LI->getLoopFor(Src->getParent()), 3907 Pair[SJ].Dst, LI->getLoopFor(Dst->getParent()), 3908 Pair[SJ].Loops); 3909 switch (Pair[SJ].Classification) { 3910 case Subscript::ZIV: 3911 Mivs.reset(SJ); 3912 break; 3913 case Subscript::SIV: 3914 Sivs.set(SJ); 3915 Mivs.reset(SJ); 3916 break; 3917 case Subscript::RDIV: 3918 case Subscript::MIV: 3919 break; 3920 default: 3921 llvm_unreachable("bad subscript classification"); 3922 } 3923 } 3924 } 3925 } 3926 } 3927 } 3928 } 3929 llvm_unreachable("somehow reached end of routine"); 3930 return nullptr; 3931 } 3932