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