1 //===-- UnrollLoop.cpp - Loop unrolling utilities -------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file implements some loop unrolling utilities. It does not define any 11 // actual pass or policy, but provides a single function to perform loop 12 // unrolling. 13 // 14 // The process of unrolling can produce extraneous basic blocks linked with 15 // unconditional branches. This will be corrected in the future. 16 // 17 //===----------------------------------------------------------------------===// 18 19 #include "llvm/ADT/SmallPtrSet.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/Analysis/AssumptionCache.h" 22 #include "llvm/Analysis/InstructionSimplify.h" 23 #include "llvm/Analysis/LoopIterator.h" 24 #include "llvm/Analysis/LoopPass.h" 25 #include "llvm/Analysis/OptimizationRemarkEmitter.h" 26 #include "llvm/Analysis/ScalarEvolution.h" 27 #include "llvm/IR/BasicBlock.h" 28 #include "llvm/IR/DataLayout.h" 29 #include "llvm/IR/DebugInfoMetadata.h" 30 #include "llvm/IR/Dominators.h" 31 #include "llvm/IR/IntrinsicInst.h" 32 #include "llvm/IR/LLVMContext.h" 33 #include "llvm/Support/Debug.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 36 #include "llvm/Transforms/Utils/Cloning.h" 37 #include "llvm/Transforms/Utils/Local.h" 38 #include "llvm/Transforms/Utils/LoopSimplify.h" 39 #include "llvm/Transforms/Utils/LoopUtils.h" 40 #include "llvm/Transforms/Utils/SimplifyIndVar.h" 41 #include "llvm/Transforms/Utils/UnrollLoop.h" 42 using namespace llvm; 43 44 #define DEBUG_TYPE "loop-unroll" 45 46 // TODO: Should these be here or in LoopUnroll? 47 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled"); 48 STATISTIC(NumUnrolled, "Number of loops unrolled (completely or otherwise)"); 49 50 static cl::opt<bool> 51 UnrollRuntimeEpilog("unroll-runtime-epilog", cl::init(false), cl::Hidden, 52 cl::desc("Allow runtime unrolled loops to be unrolled " 53 "with epilog instead of prolog.")); 54 55 static cl::opt<bool> 56 UnrollVerifyDomtree("unroll-verify-domtree", cl::Hidden, 57 cl::desc("Verify domtree after unrolling"), 58 #ifdef NDEBUG 59 cl::init(false) 60 #else 61 cl::init(true) 62 #endif 63 ); 64 65 /// Convert the instruction operands from referencing the current values into 66 /// those specified by VMap. 67 static inline void remapInstruction(Instruction *I, 68 ValueToValueMapTy &VMap) { 69 for (unsigned op = 0, E = I->getNumOperands(); op != E; ++op) { 70 Value *Op = I->getOperand(op); 71 72 // Unwrap arguments of dbg.value intrinsics. 73 bool Wrapped = false; 74 if (auto *V = dyn_cast<MetadataAsValue>(Op)) 75 if (auto *Unwrapped = dyn_cast<ValueAsMetadata>(V->getMetadata())) { 76 Op = Unwrapped->getValue(); 77 Wrapped = true; 78 } 79 80 auto wrap = [&](Value *V) { 81 auto &C = I->getContext(); 82 return Wrapped ? MetadataAsValue::get(C, ValueAsMetadata::get(V)) : V; 83 }; 84 85 ValueToValueMapTy::iterator It = VMap.find(Op); 86 if (It != VMap.end()) 87 I->setOperand(op, wrap(It->second)); 88 } 89 90 if (PHINode *PN = dyn_cast<PHINode>(I)) { 91 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { 92 ValueToValueMapTy::iterator It = VMap.find(PN->getIncomingBlock(i)); 93 if (It != VMap.end()) 94 PN->setIncomingBlock(i, cast<BasicBlock>(It->second)); 95 } 96 } 97 } 98 99 /// Folds a basic block into its predecessor if it only has one predecessor, and 100 /// that predecessor only has one successor. 101 /// The LoopInfo Analysis that is passed will be kept consistent. If folding is 102 /// successful references to the containing loop must be removed from 103 /// ScalarEvolution by calling ScalarEvolution::forgetLoop because SE may have 104 /// references to the eliminated BB. The argument ForgottenLoops contains a set 105 /// of loops that have already been forgotten to prevent redundant, expensive 106 /// calls to ScalarEvolution::forgetLoop. Returns the new combined block. 107 static BasicBlock * 108 foldBlockIntoPredecessor(BasicBlock *BB, LoopInfo *LI, ScalarEvolution *SE, 109 SmallPtrSetImpl<Loop *> &ForgottenLoops, 110 DominatorTree *DT) { 111 // Merge basic blocks into their predecessor if there is only one distinct 112 // pred, and if there is only one distinct successor of the predecessor, and 113 // if there are no PHI nodes. 114 BasicBlock *OnlyPred = BB->getSinglePredecessor(); 115 if (!OnlyPred) return nullptr; 116 117 if (OnlyPred->getTerminator()->getNumSuccessors() != 1) 118 return nullptr; 119 120 DEBUG(dbgs() << "Merging: " << *BB << "into: " << *OnlyPred); 121 122 // Resolve any PHI nodes at the start of the block. They are all 123 // guaranteed to have exactly one entry if they exist, unless there are 124 // multiple duplicate (but guaranteed to be equal) entries for the 125 // incoming edges. This occurs when there are multiple edges from 126 // OnlyPred to OnlySucc. 127 FoldSingleEntryPHINodes(BB); 128 129 // Delete the unconditional branch from the predecessor... 130 OnlyPred->getInstList().pop_back(); 131 132 // Make all PHI nodes that referred to BB now refer to Pred as their 133 // source... 134 BB->replaceAllUsesWith(OnlyPred); 135 136 // Move all definitions in the successor to the predecessor... 137 OnlyPred->getInstList().splice(OnlyPred->end(), BB->getInstList()); 138 139 // OldName will be valid until erased. 140 StringRef OldName = BB->getName(); 141 142 // Erase the old block and update dominator info. 143 if (DT) 144 if (DomTreeNode *DTN = DT->getNode(BB)) { 145 DomTreeNode *PredDTN = DT->getNode(OnlyPred); 146 SmallVector<DomTreeNode *, 8> Children(DTN->begin(), DTN->end()); 147 for (auto *DI : Children) 148 DT->changeImmediateDominator(DI, PredDTN); 149 150 DT->eraseNode(BB); 151 } 152 153 // ScalarEvolution holds references to loop exit blocks. 154 if (SE) { 155 if (Loop *L = LI->getLoopFor(BB)) { 156 if (ForgottenLoops.insert(L).second) 157 SE->forgetLoop(L); 158 } 159 } 160 LI->removeBlock(BB); 161 162 // Inherit predecessor's name if it exists... 163 if (!OldName.empty() && !OnlyPred->hasName()) 164 OnlyPred->setName(OldName); 165 166 BB->eraseFromParent(); 167 168 return OnlyPred; 169 } 170 171 /// Check if unrolling created a situation where we need to insert phi nodes to 172 /// preserve LCSSA form. 173 /// \param Blocks is a vector of basic blocks representing unrolled loop. 174 /// \param L is the outer loop. 175 /// It's possible that some of the blocks are in L, and some are not. In this 176 /// case, if there is a use is outside L, and definition is inside L, we need to 177 /// insert a phi-node, otherwise LCSSA will be broken. 178 /// The function is just a helper function for llvm::UnrollLoop that returns 179 /// true if this situation occurs, indicating that LCSSA needs to be fixed. 180 static bool needToInsertPhisForLCSSA(Loop *L, std::vector<BasicBlock *> Blocks, 181 LoopInfo *LI) { 182 for (BasicBlock *BB : Blocks) { 183 if (LI->getLoopFor(BB) == L) 184 continue; 185 for (Instruction &I : *BB) { 186 for (Use &U : I.operands()) { 187 if (auto Def = dyn_cast<Instruction>(U)) { 188 Loop *DefLoop = LI->getLoopFor(Def->getParent()); 189 if (!DefLoop) 190 continue; 191 if (DefLoop->contains(L)) 192 return true; 193 } 194 } 195 } 196 } 197 return false; 198 } 199 200 /// Adds ClonedBB to LoopInfo, creates a new loop for ClonedBB if necessary 201 /// and adds a mapping from the original loop to the new loop to NewLoops. 202 /// Returns nullptr if no new loop was created and a pointer to the 203 /// original loop OriginalBB was part of otherwise. 204 const Loop* llvm::addClonedBlockToLoopInfo(BasicBlock *OriginalBB, 205 BasicBlock *ClonedBB, LoopInfo *LI, 206 NewLoopsMap &NewLoops) { 207 // Figure out which loop New is in. 208 const Loop *OldLoop = LI->getLoopFor(OriginalBB); 209 assert(OldLoop && "Should (at least) be in the loop being unrolled!"); 210 211 Loop *&NewLoop = NewLoops[OldLoop]; 212 if (!NewLoop) { 213 // Found a new sub-loop. 214 assert(OriginalBB == OldLoop->getHeader() && 215 "Header should be first in RPO"); 216 217 NewLoop = LI->AllocateLoop(); 218 Loop *NewLoopParent = NewLoops.lookup(OldLoop->getParentLoop()); 219 220 if (NewLoopParent) 221 NewLoopParent->addChildLoop(NewLoop); 222 else 223 LI->addTopLevelLoop(NewLoop); 224 225 NewLoop->addBasicBlockToLoop(ClonedBB, *LI); 226 return OldLoop; 227 } else { 228 NewLoop->addBasicBlockToLoop(ClonedBB, *LI); 229 return nullptr; 230 } 231 } 232 233 /// The function chooses which type of unroll (epilog or prolog) is more 234 /// profitabale. 235 /// Epilog unroll is more profitable when there is PHI that starts from 236 /// constant. In this case epilog will leave PHI start from constant, 237 /// but prolog will convert it to non-constant. 238 /// 239 /// loop: 240 /// PN = PHI [I, Latch], [CI, PreHeader] 241 /// I = foo(PN) 242 /// ... 243 /// 244 /// Epilog unroll case. 245 /// loop: 246 /// PN = PHI [I2, Latch], [CI, PreHeader] 247 /// I1 = foo(PN) 248 /// I2 = foo(I1) 249 /// ... 250 /// Prolog unroll case. 251 /// NewPN = PHI [PrologI, Prolog], [CI, PreHeader] 252 /// loop: 253 /// PN = PHI [I2, Latch], [NewPN, PreHeader] 254 /// I1 = foo(PN) 255 /// I2 = foo(I1) 256 /// ... 257 /// 258 static bool isEpilogProfitable(Loop *L) { 259 BasicBlock *PreHeader = L->getLoopPreheader(); 260 BasicBlock *Header = L->getHeader(); 261 assert(PreHeader && Header); 262 for (Instruction &BBI : *Header) { 263 PHINode *PN = dyn_cast<PHINode>(&BBI); 264 if (!PN) 265 break; 266 if (isa<ConstantInt>(PN->getIncomingValueForBlock(PreHeader))) 267 return true; 268 } 269 return false; 270 } 271 272 /// Unroll the given loop by Count. The loop must be in LCSSA form. Unrolling 273 /// can only fail when the loop's latch block is not terminated by a conditional 274 /// branch instruction. However, if the trip count (and multiple) are not known, 275 /// loop unrolling will mostly produce more code that is no faster. 276 /// 277 /// TripCount is the upper bound of the iteration on which control exits 278 /// LatchBlock. Control may exit the loop prior to TripCount iterations either 279 /// via an early branch in other loop block or via LatchBlock terminator. This 280 /// is relaxed from the general definition of trip count which is the number of 281 /// times the loop header executes. Note that UnrollLoop assumes that the loop 282 /// counter test is in LatchBlock in order to remove unnecesssary instances of 283 /// the test. If control can exit the loop from the LatchBlock's terminator 284 /// prior to TripCount iterations, flag PreserveCondBr needs to be set. 285 /// 286 /// PreserveCondBr indicates whether the conditional branch of the LatchBlock 287 /// needs to be preserved. It is needed when we use trip count upper bound to 288 /// fully unroll the loop. If PreserveOnlyFirst is also set then only the first 289 /// conditional branch needs to be preserved. 290 /// 291 /// Similarly, TripMultiple divides the number of times that the LatchBlock may 292 /// execute without exiting the loop. 293 /// 294 /// If AllowRuntime is true then UnrollLoop will consider unrolling loops that 295 /// have a runtime (i.e. not compile time constant) trip count. Unrolling these 296 /// loops require a unroll "prologue" that runs "RuntimeTripCount % Count" 297 /// iterations before branching into the unrolled loop. UnrollLoop will not 298 /// runtime-unroll the loop if computing RuntimeTripCount will be expensive and 299 /// AllowExpensiveTripCount is false. 300 /// 301 /// If we want to perform PGO-based loop peeling, PeelCount is set to the 302 /// number of iterations we want to peel off. 303 /// 304 /// The LoopInfo Analysis that is passed will be kept consistent. 305 /// 306 /// This utility preserves LoopInfo. It will also preserve ScalarEvolution and 307 /// DominatorTree if they are non-null. 308 LoopUnrollResult llvm::UnrollLoop( 309 Loop *L, unsigned Count, unsigned TripCount, bool Force, bool AllowRuntime, 310 bool AllowExpensiveTripCount, bool PreserveCondBr, bool PreserveOnlyFirst, 311 unsigned TripMultiple, unsigned PeelCount, bool UnrollRemainder, 312 LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC, 313 OptimizationRemarkEmitter *ORE, bool PreserveLCSSA) { 314 315 BasicBlock *Preheader = L->getLoopPreheader(); 316 if (!Preheader) { 317 DEBUG(dbgs() << " Can't unroll; loop preheader-insertion failed.\n"); 318 return LoopUnrollResult::Unmodified; 319 } 320 321 BasicBlock *LatchBlock = L->getLoopLatch(); 322 if (!LatchBlock) { 323 DEBUG(dbgs() << " Can't unroll; loop exit-block-insertion failed.\n"); 324 return LoopUnrollResult::Unmodified; 325 } 326 327 // Loops with indirectbr cannot be cloned. 328 if (!L->isSafeToClone()) { 329 DEBUG(dbgs() << " Can't unroll; Loop body cannot be cloned.\n"); 330 return LoopUnrollResult::Unmodified; 331 } 332 333 // The current loop unroll pass can only unroll loops with a single latch 334 // that's a conditional branch exiting the loop. 335 // FIXME: The implementation can be extended to work with more complicated 336 // cases, e.g. loops with multiple latches. 337 BasicBlock *Header = L->getHeader(); 338 BranchInst *BI = dyn_cast<BranchInst>(LatchBlock->getTerminator()); 339 340 if (!BI || BI->isUnconditional()) { 341 // The loop-rotate pass can be helpful to avoid this in many cases. 342 DEBUG(dbgs() << 343 " Can't unroll; loop not terminated by a conditional branch.\n"); 344 return LoopUnrollResult::Unmodified; 345 } 346 347 auto CheckSuccessors = [&](unsigned S1, unsigned S2) { 348 return BI->getSuccessor(S1) == Header && !L->contains(BI->getSuccessor(S2)); 349 }; 350 351 if (!CheckSuccessors(0, 1) && !CheckSuccessors(1, 0)) { 352 DEBUG(dbgs() << "Can't unroll; only loops with one conditional latch" 353 " exiting the loop can be unrolled\n"); 354 return LoopUnrollResult::Unmodified; 355 } 356 357 if (Header->hasAddressTaken()) { 358 // The loop-rotate pass can be helpful to avoid this in many cases. 359 DEBUG(dbgs() << 360 " Won't unroll loop: address of header block is taken.\n"); 361 return LoopUnrollResult::Unmodified; 362 } 363 364 if (TripCount != 0) 365 DEBUG(dbgs() << " Trip Count = " << TripCount << "\n"); 366 if (TripMultiple != 1) 367 DEBUG(dbgs() << " Trip Multiple = " << TripMultiple << "\n"); 368 369 // Effectively "DCE" unrolled iterations that are beyond the tripcount 370 // and will never be executed. 371 if (TripCount != 0 && Count > TripCount) 372 Count = TripCount; 373 374 // Don't enter the unroll code if there is nothing to do. 375 if (TripCount == 0 && Count < 2 && PeelCount == 0) { 376 DEBUG(dbgs() << "Won't unroll; almost nothing to do\n"); 377 return LoopUnrollResult::Unmodified; 378 } 379 380 assert(Count > 0); 381 assert(TripMultiple > 0); 382 assert(TripCount == 0 || TripCount % TripMultiple == 0); 383 384 // Are we eliminating the loop control altogether? 385 bool CompletelyUnroll = Count == TripCount; 386 SmallVector<BasicBlock *, 4> ExitBlocks; 387 L->getExitBlocks(ExitBlocks); 388 std::vector<BasicBlock*> OriginalLoopBlocks = L->getBlocks(); 389 390 // Go through all exits of L and see if there are any phi-nodes there. We just 391 // conservatively assume that they're inserted to preserve LCSSA form, which 392 // means that complete unrolling might break this form. We need to either fix 393 // it in-place after the transformation, or entirely rebuild LCSSA. TODO: For 394 // now we just recompute LCSSA for the outer loop, but it should be possible 395 // to fix it in-place. 396 bool NeedToFixLCSSA = PreserveLCSSA && CompletelyUnroll && 397 any_of(ExitBlocks, [](const BasicBlock *BB) { 398 return isa<PHINode>(BB->begin()); 399 }); 400 401 // We assume a run-time trip count if the compiler cannot 402 // figure out the loop trip count and the unroll-runtime 403 // flag is specified. 404 bool RuntimeTripCount = (TripCount == 0 && Count > 0 && AllowRuntime); 405 406 assert((!RuntimeTripCount || !PeelCount) && 407 "Did not expect runtime trip-count unrolling " 408 "and peeling for the same loop"); 409 410 if (PeelCount) { 411 bool Peeled = peelLoop(L, PeelCount, LI, SE, DT, AC, PreserveLCSSA); 412 413 // Successful peeling may result in a change in the loop preheader/trip 414 // counts. If we later unroll the loop, we want these to be updated. 415 if (Peeled) { 416 BasicBlock *ExitingBlock = L->getExitingBlock(); 417 assert(ExitingBlock && "Loop without exiting block?"); 418 Preheader = L->getLoopPreheader(); 419 TripCount = SE->getSmallConstantTripCount(L, ExitingBlock); 420 TripMultiple = SE->getSmallConstantTripMultiple(L, ExitingBlock); 421 } 422 } 423 424 // Loops containing convergent instructions must have a count that divides 425 // their TripMultiple. 426 DEBUG( 427 { 428 bool HasConvergent = false; 429 for (auto &BB : L->blocks()) 430 for (auto &I : *BB) 431 if (auto CS = CallSite(&I)) 432 HasConvergent |= CS.isConvergent(); 433 assert((!HasConvergent || TripMultiple % Count == 0) && 434 "Unroll count must divide trip multiple if loop contains a " 435 "convergent operation."); 436 }); 437 438 bool EpilogProfitability = 439 UnrollRuntimeEpilog.getNumOccurrences() ? UnrollRuntimeEpilog 440 : isEpilogProfitable(L); 441 442 if (RuntimeTripCount && TripMultiple % Count != 0 && 443 !UnrollRuntimeLoopRemainder(L, Count, AllowExpensiveTripCount, 444 EpilogProfitability, UnrollRemainder, LI, SE, 445 DT, AC, PreserveLCSSA)) { 446 if (Force) 447 RuntimeTripCount = false; 448 else { 449 DEBUG( 450 dbgs() << "Wont unroll; remainder loop could not be generated" 451 "when assuming runtime trip count\n"); 452 return LoopUnrollResult::Unmodified; 453 } 454 } 455 456 // Notify ScalarEvolution that the loop will be substantially changed, 457 // if not outright eliminated. 458 if (SE) 459 SE->forgetLoop(L); 460 461 // If we know the trip count, we know the multiple... 462 unsigned BreakoutTrip = 0; 463 if (TripCount != 0) { 464 BreakoutTrip = TripCount % Count; 465 TripMultiple = 0; 466 } else { 467 // Figure out what multiple to use. 468 BreakoutTrip = TripMultiple = 469 (unsigned)GreatestCommonDivisor64(Count, TripMultiple); 470 } 471 472 using namespace ore; 473 // Report the unrolling decision. 474 if (CompletelyUnroll) { 475 DEBUG(dbgs() << "COMPLETELY UNROLLING loop %" << Header->getName() 476 << " with trip count " << TripCount << "!\n"); 477 if (ORE) 478 ORE->emit([&]() { 479 return OptimizationRemark(DEBUG_TYPE, "FullyUnrolled", L->getStartLoc(), 480 L->getHeader()) 481 << "completely unrolled loop with " 482 << NV("UnrollCount", TripCount) << " iterations"; 483 }); 484 } else if (PeelCount) { 485 DEBUG(dbgs() << "PEELING loop %" << Header->getName() 486 << " with iteration count " << PeelCount << "!\n"); 487 if (ORE) 488 ORE->emit([&]() { 489 return OptimizationRemark(DEBUG_TYPE, "Peeled", L->getStartLoc(), 490 L->getHeader()) 491 << " peeled loop by " << NV("PeelCount", PeelCount) 492 << " iterations"; 493 }); 494 } else { 495 auto DiagBuilder = [&]() { 496 OptimizationRemark Diag(DEBUG_TYPE, "PartialUnrolled", L->getStartLoc(), 497 L->getHeader()); 498 return Diag << "unrolled loop by a factor of " 499 << NV("UnrollCount", Count); 500 }; 501 502 DEBUG(dbgs() << "UNROLLING loop %" << Header->getName() 503 << " by " << Count); 504 if (TripMultiple == 0 || BreakoutTrip != TripMultiple) { 505 DEBUG(dbgs() << " with a breakout at trip " << BreakoutTrip); 506 if (ORE) 507 ORE->emit([&]() { 508 return DiagBuilder() << " with a breakout at trip " 509 << NV("BreakoutTrip", BreakoutTrip); 510 }); 511 } else if (TripMultiple != 1) { 512 DEBUG(dbgs() << " with " << TripMultiple << " trips per branch"); 513 if (ORE) 514 ORE->emit([&]() { 515 return DiagBuilder() << " with " << NV("TripMultiple", TripMultiple) 516 << " trips per branch"; 517 }); 518 } else if (RuntimeTripCount) { 519 DEBUG(dbgs() << " with run-time trip count"); 520 if (ORE) 521 ORE->emit( 522 [&]() { return DiagBuilder() << " with run-time trip count"; }); 523 } 524 DEBUG(dbgs() << "!\n"); 525 } 526 527 bool ContinueOnTrue = L->contains(BI->getSuccessor(0)); 528 BasicBlock *LoopExit = BI->getSuccessor(ContinueOnTrue); 529 530 // For the first iteration of the loop, we should use the precloned values for 531 // PHI nodes. Insert associations now. 532 ValueToValueMapTy LastValueMap; 533 std::vector<PHINode*> OrigPHINode; 534 for (BasicBlock::iterator I = Header->begin(); isa<PHINode>(I); ++I) { 535 OrigPHINode.push_back(cast<PHINode>(I)); 536 } 537 538 std::vector<BasicBlock*> Headers; 539 std::vector<BasicBlock*> Latches; 540 Headers.push_back(Header); 541 Latches.push_back(LatchBlock); 542 543 // The current on-the-fly SSA update requires blocks to be processed in 544 // reverse postorder so that LastValueMap contains the correct value at each 545 // exit. 546 LoopBlocksDFS DFS(L); 547 DFS.perform(LI); 548 549 // Stash the DFS iterators before adding blocks to the loop. 550 LoopBlocksDFS::RPOIterator BlockBegin = DFS.beginRPO(); 551 LoopBlocksDFS::RPOIterator BlockEnd = DFS.endRPO(); 552 553 std::vector<BasicBlock*> UnrolledLoopBlocks = L->getBlocks(); 554 555 // Loop Unrolling might create new loops. While we do preserve LoopInfo, we 556 // might break loop-simplified form for these loops (as they, e.g., would 557 // share the same exit blocks). We'll keep track of loops for which we can 558 // break this so that later we can re-simplify them. 559 SmallSetVector<Loop *, 4> LoopsToSimplify; 560 for (Loop *SubLoop : *L) 561 LoopsToSimplify.insert(SubLoop); 562 563 if (Header->getParent()->isDebugInfoForProfiling()) 564 for (BasicBlock *BB : L->getBlocks()) 565 for (Instruction &I : *BB) 566 if (!isa<DbgInfoIntrinsic>(&I)) 567 if (const DILocation *DIL = I.getDebugLoc()) 568 I.setDebugLoc(DIL->cloneWithDuplicationFactor(Count)); 569 570 for (unsigned It = 1; It != Count; ++It) { 571 std::vector<BasicBlock*> NewBlocks; 572 SmallDenseMap<const Loop *, Loop *, 4> NewLoops; 573 NewLoops[L] = L; 574 575 for (LoopBlocksDFS::RPOIterator BB = BlockBegin; BB != BlockEnd; ++BB) { 576 ValueToValueMapTy VMap; 577 BasicBlock *New = CloneBasicBlock(*BB, VMap, "." + Twine(It)); 578 Header->getParent()->getBasicBlockList().push_back(New); 579 580 assert((*BB != Header || LI->getLoopFor(*BB) == L) && 581 "Header should not be in a sub-loop"); 582 // Tell LI about New. 583 const Loop *OldLoop = addClonedBlockToLoopInfo(*BB, New, LI, NewLoops); 584 if (OldLoop) { 585 LoopsToSimplify.insert(NewLoops[OldLoop]); 586 587 // Forget the old loop, since its inputs may have changed. 588 if (SE) 589 SE->forgetLoop(OldLoop); 590 } 591 592 if (*BB == Header) 593 // Loop over all of the PHI nodes in the block, changing them to use 594 // the incoming values from the previous block. 595 for (PHINode *OrigPHI : OrigPHINode) { 596 PHINode *NewPHI = cast<PHINode>(VMap[OrigPHI]); 597 Value *InVal = NewPHI->getIncomingValueForBlock(LatchBlock); 598 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) 599 if (It > 1 && L->contains(InValI)) 600 InVal = LastValueMap[InValI]; 601 VMap[OrigPHI] = InVal; 602 New->getInstList().erase(NewPHI); 603 } 604 605 // Update our running map of newest clones 606 LastValueMap[*BB] = New; 607 for (ValueToValueMapTy::iterator VI = VMap.begin(), VE = VMap.end(); 608 VI != VE; ++VI) 609 LastValueMap[VI->first] = VI->second; 610 611 // Add phi entries for newly created values to all exit blocks. 612 for (BasicBlock *Succ : successors(*BB)) { 613 if (L->contains(Succ)) 614 continue; 615 for (BasicBlock::iterator BBI = Succ->begin(); 616 PHINode *phi = dyn_cast<PHINode>(BBI); ++BBI) { 617 Value *Incoming = phi->getIncomingValueForBlock(*BB); 618 ValueToValueMapTy::iterator It = LastValueMap.find(Incoming); 619 if (It != LastValueMap.end()) 620 Incoming = It->second; 621 phi->addIncoming(Incoming, New); 622 } 623 } 624 // Keep track of new headers and latches as we create them, so that 625 // we can insert the proper branches later. 626 if (*BB == Header) 627 Headers.push_back(New); 628 if (*BB == LatchBlock) 629 Latches.push_back(New); 630 631 NewBlocks.push_back(New); 632 UnrolledLoopBlocks.push_back(New); 633 634 // Update DomTree: since we just copy the loop body, and each copy has a 635 // dedicated entry block (copy of the header block), this header's copy 636 // dominates all copied blocks. That means, dominance relations in the 637 // copied body are the same as in the original body. 638 if (DT) { 639 if (*BB == Header) 640 DT->addNewBlock(New, Latches[It - 1]); 641 else { 642 auto BBDomNode = DT->getNode(*BB); 643 auto BBIDom = BBDomNode->getIDom(); 644 BasicBlock *OriginalBBIDom = BBIDom->getBlock(); 645 DT->addNewBlock( 646 New, cast<BasicBlock>(LastValueMap[cast<Value>(OriginalBBIDom)])); 647 } 648 } 649 } 650 651 // Remap all instructions in the most recent iteration 652 for (BasicBlock *NewBlock : NewBlocks) { 653 for (Instruction &I : *NewBlock) { 654 ::remapInstruction(&I, LastValueMap); 655 if (auto *II = dyn_cast<IntrinsicInst>(&I)) 656 if (II->getIntrinsicID() == Intrinsic::assume) 657 AC->registerAssumption(II); 658 } 659 } 660 } 661 662 // Loop over the PHI nodes in the original block, setting incoming values. 663 for (PHINode *PN : OrigPHINode) { 664 if (CompletelyUnroll) { 665 PN->replaceAllUsesWith(PN->getIncomingValueForBlock(Preheader)); 666 Header->getInstList().erase(PN); 667 } 668 else if (Count > 1) { 669 Value *InVal = PN->removeIncomingValue(LatchBlock, false); 670 // If this value was defined in the loop, take the value defined by the 671 // last iteration of the loop. 672 if (Instruction *InValI = dyn_cast<Instruction>(InVal)) { 673 if (L->contains(InValI)) 674 InVal = LastValueMap[InVal]; 675 } 676 assert(Latches.back() == LastValueMap[LatchBlock] && "bad last latch"); 677 PN->addIncoming(InVal, Latches.back()); 678 } 679 } 680 681 // Now that all the basic blocks for the unrolled iterations are in place, 682 // set up the branches to connect them. 683 for (unsigned i = 0, e = Latches.size(); i != e; ++i) { 684 // The original branch was replicated in each unrolled iteration. 685 BranchInst *Term = cast<BranchInst>(Latches[i]->getTerminator()); 686 687 // The branch destination. 688 unsigned j = (i + 1) % e; 689 BasicBlock *Dest = Headers[j]; 690 bool NeedConditional = true; 691 692 if (RuntimeTripCount && j != 0) { 693 NeedConditional = false; 694 } 695 696 // For a complete unroll, make the last iteration end with a branch 697 // to the exit block. 698 if (CompletelyUnroll) { 699 if (j == 0) 700 Dest = LoopExit; 701 // If using trip count upper bound to completely unroll, we need to keep 702 // the conditional branch except the last one because the loop may exit 703 // after any iteration. 704 assert(NeedConditional && 705 "NeedCondition cannot be modified by both complete " 706 "unrolling and runtime unrolling"); 707 NeedConditional = (PreserveCondBr && j && !(PreserveOnlyFirst && i != 0)); 708 } else if (j != BreakoutTrip && (TripMultiple == 0 || j % TripMultiple != 0)) { 709 // If we know the trip count or a multiple of it, we can safely use an 710 // unconditional branch for some iterations. 711 NeedConditional = false; 712 } 713 714 if (NeedConditional) { 715 // Update the conditional branch's successor for the following 716 // iteration. 717 Term->setSuccessor(!ContinueOnTrue, Dest); 718 } else { 719 // Remove phi operands at this loop exit 720 if (Dest != LoopExit) { 721 BasicBlock *BB = Latches[i]; 722 for (BasicBlock *Succ: successors(BB)) { 723 if (Succ == Headers[i]) 724 continue; 725 for (BasicBlock::iterator BBI = Succ->begin(); 726 PHINode *Phi = dyn_cast<PHINode>(BBI); ++BBI) { 727 Phi->removeIncomingValue(BB, false); 728 } 729 } 730 } 731 // Replace the conditional branch with an unconditional one. 732 BranchInst::Create(Dest, Term); 733 Term->eraseFromParent(); 734 } 735 } 736 737 // Update dominators of blocks we might reach through exits. 738 // Immediate dominator of such block might change, because we add more 739 // routes which can lead to the exit: we can now reach it from the copied 740 // iterations too. 741 if (DT && Count > 1) { 742 for (auto *BB : OriginalLoopBlocks) { 743 auto *BBDomNode = DT->getNode(BB); 744 SmallVector<BasicBlock *, 16> ChildrenToUpdate; 745 for (auto *ChildDomNode : BBDomNode->getChildren()) { 746 auto *ChildBB = ChildDomNode->getBlock(); 747 if (!L->contains(ChildBB)) 748 ChildrenToUpdate.push_back(ChildBB); 749 } 750 BasicBlock *NewIDom; 751 if (BB == LatchBlock) { 752 // The latch is special because we emit unconditional branches in 753 // some cases where the original loop contained a conditional branch. 754 // Since the latch is always at the bottom of the loop, if the latch 755 // dominated an exit before unrolling, the new dominator of that exit 756 // must also be a latch. Specifically, the dominator is the first 757 // latch which ends in a conditional branch, or the last latch if 758 // there is no such latch. 759 NewIDom = Latches.back(); 760 for (BasicBlock *IterLatch : Latches) { 761 TerminatorInst *Term = IterLatch->getTerminator(); 762 if (isa<BranchInst>(Term) && cast<BranchInst>(Term)->isConditional()) { 763 NewIDom = IterLatch; 764 break; 765 } 766 } 767 } else { 768 // The new idom of the block will be the nearest common dominator 769 // of all copies of the previous idom. This is equivalent to the 770 // nearest common dominator of the previous idom and the first latch, 771 // which dominates all copies of the previous idom. 772 NewIDom = DT->findNearestCommonDominator(BB, LatchBlock); 773 } 774 for (auto *ChildBB : ChildrenToUpdate) 775 DT->changeImmediateDominator(ChildBB, NewIDom); 776 } 777 } 778 779 if (DT && UnrollVerifyDomtree) 780 DT->verifyDomTree(); 781 782 // Merge adjacent basic blocks, if possible. 783 SmallPtrSet<Loop *, 4> ForgottenLoops; 784 for (BasicBlock *Latch : Latches) { 785 BranchInst *Term = cast<BranchInst>(Latch->getTerminator()); 786 if (Term->isUnconditional()) { 787 BasicBlock *Dest = Term->getSuccessor(0); 788 if (BasicBlock *Fold = 789 foldBlockIntoPredecessor(Dest, LI, SE, ForgottenLoops, DT)) { 790 // Dest has been folded into Fold. Update our worklists accordingly. 791 std::replace(Latches.begin(), Latches.end(), Dest, Fold); 792 UnrolledLoopBlocks.erase(std::remove(UnrolledLoopBlocks.begin(), 793 UnrolledLoopBlocks.end(), Dest), 794 UnrolledLoopBlocks.end()); 795 } 796 } 797 } 798 799 // Simplify any new induction variables in the partially unrolled loop. 800 if (SE && !CompletelyUnroll && Count > 1) { 801 SmallVector<WeakTrackingVH, 16> DeadInsts; 802 simplifyLoopIVs(L, SE, DT, LI, DeadInsts); 803 804 // Aggressively clean up dead instructions that simplifyLoopIVs already 805 // identified. Any remaining should be cleaned up below. 806 while (!DeadInsts.empty()) 807 if (Instruction *Inst = 808 dyn_cast_or_null<Instruction>(&*DeadInsts.pop_back_val())) 809 RecursivelyDeleteTriviallyDeadInstructions(Inst); 810 } 811 812 // At this point, the code is well formed. We now do a quick sweep over the 813 // inserted code, doing constant propagation and dead code elimination as we 814 // go. 815 const DataLayout &DL = Header->getModule()->getDataLayout(); 816 const std::vector<BasicBlock*> &NewLoopBlocks = L->getBlocks(); 817 for (BasicBlock *BB : NewLoopBlocks) { 818 for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ) { 819 Instruction *Inst = &*I++; 820 821 if (Value *V = SimplifyInstruction(Inst, {DL, nullptr, DT, AC})) 822 if (LI->replacementPreservesLCSSAForm(Inst, V)) 823 Inst->replaceAllUsesWith(V); 824 if (isInstructionTriviallyDead(Inst)) 825 BB->getInstList().erase(Inst); 826 } 827 } 828 829 // TODO: after peeling or unrolling, previously loop variant conditions are 830 // likely to fold to constants, eagerly propagating those here will require 831 // fewer cleanup passes to be run. Alternatively, a LoopEarlyCSE might be 832 // appropriate. 833 834 NumCompletelyUnrolled += CompletelyUnroll; 835 ++NumUnrolled; 836 837 Loop *OuterL = L->getParentLoop(); 838 // Update LoopInfo if the loop is completely removed. 839 if (CompletelyUnroll) 840 LI->erase(L); 841 842 // After complete unrolling most of the blocks should be contained in OuterL. 843 // However, some of them might happen to be out of OuterL (e.g. if they 844 // precede a loop exit). In this case we might need to insert PHI nodes in 845 // order to preserve LCSSA form. 846 // We don't need to check this if we already know that we need to fix LCSSA 847 // form. 848 // TODO: For now we just recompute LCSSA for the outer loop in this case, but 849 // it should be possible to fix it in-place. 850 if (PreserveLCSSA && OuterL && CompletelyUnroll && !NeedToFixLCSSA) 851 NeedToFixLCSSA |= ::needToInsertPhisForLCSSA(OuterL, UnrolledLoopBlocks, LI); 852 853 // If we have a pass and a DominatorTree we should re-simplify impacted loops 854 // to ensure subsequent analyses can rely on this form. We want to simplify 855 // at least one layer outside of the loop that was unrolled so that any 856 // changes to the parent loop exposed by the unrolling are considered. 857 if (DT) { 858 if (OuterL) { 859 // OuterL includes all loops for which we can break loop-simplify, so 860 // it's sufficient to simplify only it (it'll recursively simplify inner 861 // loops too). 862 if (NeedToFixLCSSA) { 863 // LCSSA must be performed on the outermost affected loop. The unrolled 864 // loop's last loop latch is guaranteed to be in the outermost loop 865 // after LoopInfo's been updated by LoopInfo::erase. 866 Loop *LatchLoop = LI->getLoopFor(Latches.back()); 867 Loop *FixLCSSALoop = OuterL; 868 if (!FixLCSSALoop->contains(LatchLoop)) 869 while (FixLCSSALoop->getParentLoop() != LatchLoop) 870 FixLCSSALoop = FixLCSSALoop->getParentLoop(); 871 872 formLCSSARecursively(*FixLCSSALoop, *DT, LI, SE); 873 } else if (PreserveLCSSA) { 874 assert(OuterL->isLCSSAForm(*DT) && 875 "Loops should be in LCSSA form after loop-unroll."); 876 } 877 878 // TODO: That potentially might be compile-time expensive. We should try 879 // to fix the loop-simplified form incrementally. 880 simplifyLoop(OuterL, DT, LI, SE, AC, PreserveLCSSA); 881 } else { 882 // Simplify loops for which we might've broken loop-simplify form. 883 for (Loop *SubLoop : LoopsToSimplify) 884 simplifyLoop(SubLoop, DT, LI, SE, AC, PreserveLCSSA); 885 } 886 } 887 888 return CompletelyUnroll ? LoopUnrollResult::FullyUnrolled 889 : LoopUnrollResult::PartiallyUnrolled; 890 } 891 892 /// Given an llvm.loop loop id metadata node, returns the loop hint metadata 893 /// node with the given name (for example, "llvm.loop.unroll.count"). If no 894 /// such metadata node exists, then nullptr is returned. 895 MDNode *llvm::GetUnrollMetadata(MDNode *LoopID, StringRef Name) { 896 // First operand should refer to the loop id itself. 897 assert(LoopID->getNumOperands() > 0 && "requires at least one operand"); 898 assert(LoopID->getOperand(0) == LoopID && "invalid loop id"); 899 900 for (unsigned i = 1, e = LoopID->getNumOperands(); i < e; ++i) { 901 MDNode *MD = dyn_cast<MDNode>(LoopID->getOperand(i)); 902 if (!MD) 903 continue; 904 905 MDString *S = dyn_cast<MDString>(MD->getOperand(0)); 906 if (!S) 907 continue; 908 909 if (Name.equals(S->getString())) 910 return MD; 911 } 912 return nullptr; 913 } 914