1 //===- SimpleLoopUnswitch.cpp - Hoist loop-invariant control flow ---------===// 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 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 11 #include "llvm/ADT/DenseMap.h" 12 #include "llvm/ADT/STLExtras.h" 13 #include "llvm/ADT/Sequence.h" 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/SmallVector.h" 17 #include "llvm/ADT/Statistic.h" 18 #include "llvm/ADT/Twine.h" 19 #include "llvm/Analysis/AssumptionCache.h" 20 #include "llvm/Analysis/CodeMetrics.h" 21 #include "llvm/Analysis/LoopAnalysisManager.h" 22 #include "llvm/Analysis/LoopInfo.h" 23 #include "llvm/Analysis/LoopPass.h" 24 #include "llvm/IR/BasicBlock.h" 25 #include "llvm/IR/Constant.h" 26 #include "llvm/IR/Constants.h" 27 #include "llvm/IR/Dominators.h" 28 #include "llvm/IR/Function.h" 29 #include "llvm/IR/InstrTypes.h" 30 #include "llvm/IR/Instruction.h" 31 #include "llvm/IR/Instructions.h" 32 #include "llvm/IR/IntrinsicInst.h" 33 #include "llvm/IR/Use.h" 34 #include "llvm/IR/Value.h" 35 #include "llvm/Pass.h" 36 #include "llvm/Support/Casting.h" 37 #include "llvm/Support/Debug.h" 38 #include "llvm/Support/ErrorHandling.h" 39 #include "llvm/Support/GenericDomTree.h" 40 #include "llvm/Support/raw_ostream.h" 41 #include "llvm/Transforms/Scalar/SimpleLoopUnswitch.h" 42 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 43 #include "llvm/Transforms/Utils/Cloning.h" 44 #include "llvm/Transforms/Utils/LoopUtils.h" 45 #include "llvm/Transforms/Utils/ValueMapper.h" 46 #include <algorithm> 47 #include <cassert> 48 #include <iterator> 49 #include <numeric> 50 #include <utility> 51 52 #define DEBUG_TYPE "simple-loop-unswitch" 53 54 using namespace llvm; 55 56 STATISTIC(NumBranches, "Number of branches unswitched"); 57 STATISTIC(NumSwitches, "Number of switches unswitched"); 58 STATISTIC(NumTrivial, "Number of unswitches that are trivial"); 59 60 static cl::opt<bool> EnableNonTrivialUnswitch( 61 "enable-nontrivial-unswitch", cl::init(false), cl::Hidden, 62 cl::desc("Forcibly enables non-trivial loop unswitching rather than " 63 "following the configuration passed into the pass.")); 64 65 static cl::opt<int> 66 UnswitchThreshold("unswitch-threshold", cl::init(50), cl::Hidden, 67 cl::desc("The cost threshold for unswitching a loop.")); 68 69 static void replaceLoopUsesWithConstant(Loop &L, Value &LIC, 70 Constant &Replacement) { 71 assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); 72 73 // Replace uses of LIC in the loop with the given constant. 74 for (auto UI = LIC.use_begin(), UE = LIC.use_end(); UI != UE;) { 75 // Grab the use and walk past it so we can clobber it in the use list. 76 Use *U = &*UI++; 77 Instruction *UserI = dyn_cast<Instruction>(U->getUser()); 78 if (!UserI || !L.contains(UserI)) 79 continue; 80 81 // Replace this use within the loop body. 82 *U = &Replacement; 83 } 84 } 85 86 /// Update the IDom for a basic block whose predecessor set has changed. 87 /// 88 /// This routine is designed to work when the domtree update is relatively 89 /// localized by leveraging a known common dominator, often a loop header. 90 /// 91 /// FIXME: Should consider hand-rolling a slightly more efficient non-DFS 92 /// approach here as we can do that easily by persisting the candidate IDom's 93 /// dominating set between each predecessor. 94 /// 95 /// FIXME: Longer term, many uses of this can be replaced by an incremental 96 /// domtree update strategy that starts from a known dominating block and 97 /// rebuilds that subtree. 98 static bool updateIDomWithKnownCommonDominator(BasicBlock *BB, 99 BasicBlock *KnownDominatingBB, 100 DominatorTree &DT) { 101 assert(pred_begin(BB) != pred_end(BB) && 102 "This routine does not handle unreachable blocks!"); 103 104 BasicBlock *OrigIDom = DT[BB]->getIDom()->getBlock(); 105 106 BasicBlock *IDom = *pred_begin(BB); 107 assert(DT.dominates(KnownDominatingBB, IDom) && 108 "Bad known dominating block!"); 109 110 // Walk all of the other predecessors finding the nearest common dominator 111 // until all predecessors are covered or we reach the loop header. The loop 112 // header necessarily dominates all loop exit blocks in loop simplified form 113 // so we can early-exit the moment we hit that block. 114 for (auto PI = std::next(pred_begin(BB)), PE = pred_end(BB); 115 PI != PE && IDom != KnownDominatingBB; ++PI) { 116 assert(DT.dominates(KnownDominatingBB, *PI) && 117 "Bad known dominating block!"); 118 IDom = DT.findNearestCommonDominator(IDom, *PI); 119 } 120 121 if (IDom == OrigIDom) 122 return false; 123 124 DT.changeImmediateDominator(BB, IDom); 125 return true; 126 } 127 128 // Note that we don't currently use the IDFCalculator here for two reasons: 129 // 1) It computes dominator tree levels for the entire function on each run 130 // of 'compute'. While this isn't terrible, given that we expect to update 131 // relatively small subtrees of the domtree, it isn't necessarily the right 132 // tradeoff. 133 // 2) The interface doesn't fit this usage well. It doesn't operate in 134 // append-only, and builds several sets that we don't need. 135 // 136 // FIXME: Neither of these issues are a big deal and could be addressed with 137 // some amount of refactoring of IDFCalculator. That would allow us to share 138 // the core logic here (which is solving the same core problem). 139 static void appendDomFrontier(DomTreeNode *Node, 140 SmallSetVector<BasicBlock *, 4> &Worklist, 141 SmallVectorImpl<DomTreeNode *> &DomNodes, 142 SmallPtrSetImpl<BasicBlock *> &DomSet) { 143 assert(DomNodes.empty() && "Must start with no dominator nodes."); 144 assert(DomSet.empty() && "Must start with an empty dominator set."); 145 146 // First flatten this subtree into sequence of nodes by doing a pre-order 147 // walk. 148 DomNodes.push_back(Node); 149 // We intentionally re-evaluate the size as each node can add new children. 150 // Because this is a tree walk, this cannot add any duplicates. 151 for (int i = 0; i < (int)DomNodes.size(); ++i) 152 DomNodes.insert(DomNodes.end(), DomNodes[i]->begin(), DomNodes[i]->end()); 153 154 // Now create a set of the basic blocks so we can quickly test for 155 // dominated successors. We could in theory use the DFS numbers of the 156 // dominator tree for this, but we want this to remain predictably fast 157 // even while we mutate the dominator tree in ways that would invalidate 158 // the DFS numbering. 159 for (DomTreeNode *InnerN : DomNodes) 160 DomSet.insert(InnerN->getBlock()); 161 162 // Now re-walk the nodes, appending every successor of every node that isn't 163 // in the set. Note that we don't append the node itself, even though if it 164 // is a successor it does not strictly dominate itself and thus it would be 165 // part of the dominance frontier. The reason we don't append it is that 166 // the node passed in came *from* the worklist and so it has already been 167 // processed. 168 for (DomTreeNode *InnerN : DomNodes) 169 for (BasicBlock *SuccBB : successors(InnerN->getBlock())) 170 if (!DomSet.count(SuccBB)) 171 Worklist.insert(SuccBB); 172 173 DomNodes.clear(); 174 DomSet.clear(); 175 } 176 177 /// Update the dominator tree after unswitching a particular former exit block. 178 /// 179 /// This handles the full update of the dominator tree after hoisting a block 180 /// that previously was an exit block (or split off of an exit block) up to be 181 /// reached from the new immediate dominator of the preheader. 182 /// 183 /// The common case is simple -- we just move the unswitched block to have an 184 /// immediate dominator of the old preheader. But in complex cases, there may 185 /// be other blocks reachable from the unswitched block that are immediately 186 /// dominated by some node between the unswitched one and the old preheader. 187 /// All of these also need to be hoisted in the dominator tree. We also want to 188 /// minimize queries to the dominator tree because each step of this 189 /// invalidates any DFS numbers that would make queries fast. 190 static void updateDTAfterUnswitch(BasicBlock *UnswitchedBB, BasicBlock *OldPH, 191 DominatorTree &DT) { 192 DomTreeNode *OldPHNode = DT[OldPH]; 193 DomTreeNode *UnswitchedNode = DT[UnswitchedBB]; 194 // If the dominator tree has already been updated for this unswitched node, 195 // we're done. This makes it easier to use this routine if there are multiple 196 // paths to the same unswitched destination. 197 if (UnswitchedNode->getIDom() == OldPHNode) 198 return; 199 200 // First collect the domtree nodes that we are hoisting over. These are the 201 // set of nodes which may have children that need to be hoisted as well. 202 SmallPtrSet<DomTreeNode *, 4> DomChain; 203 for (auto *IDom = UnswitchedNode->getIDom(); IDom != OldPHNode; 204 IDom = IDom->getIDom()) 205 DomChain.insert(IDom); 206 207 // The unswitched block ends up immediately dominated by the old preheader -- 208 // regardless of whether it is the loop exit block or split off of the loop 209 // exit block. 210 DT.changeImmediateDominator(UnswitchedNode, OldPHNode); 211 212 // For everything that moves up the dominator tree, we need to examine the 213 // dominator frontier to see if it additionally should move up the dominator 214 // tree. This lambda appends the dominator frontier for a node on the 215 // worklist. 216 SmallSetVector<BasicBlock *, 4> Worklist; 217 218 // Scratch data structures reused by domfrontier finding. 219 SmallVector<DomTreeNode *, 4> DomNodes; 220 SmallPtrSet<BasicBlock *, 4> DomSet; 221 222 // Append the initial dom frontier nodes. 223 appendDomFrontier(UnswitchedNode, Worklist, DomNodes, DomSet); 224 225 // Walk the worklist. We grow the list in the loop and so must recompute size. 226 for (int i = 0; i < (int)Worklist.size(); ++i) { 227 auto *BB = Worklist[i]; 228 229 DomTreeNode *Node = DT[BB]; 230 assert(!DomChain.count(Node) && 231 "Cannot be dominated by a block you can reach!"); 232 233 // If this block had an immediate dominator somewhere in the chain 234 // we hoisted over, then its position in the domtree needs to move as it is 235 // reachable from a node hoisted over this chain. 236 if (!DomChain.count(Node->getIDom())) 237 continue; 238 239 DT.changeImmediateDominator(Node, OldPHNode); 240 241 // Now add this node's dominator frontier to the worklist as well. 242 appendDomFrontier(Node, Worklist, DomNodes, DomSet); 243 } 244 } 245 246 /// Check that all the LCSSA PHI nodes in the loop exit block have trivial 247 /// incoming values along this edge. 248 static bool areLoopExitPHIsLoopInvariant(Loop &L, BasicBlock &ExitingBB, 249 BasicBlock &ExitBB) { 250 for (Instruction &I : ExitBB) { 251 auto *PN = dyn_cast<PHINode>(&I); 252 if (!PN) 253 // No more PHIs to check. 254 return true; 255 256 // If the incoming value for this edge isn't loop invariant the unswitch 257 // won't be trivial. 258 if (!L.isLoopInvariant(PN->getIncomingValueForBlock(&ExitingBB))) 259 return false; 260 } 261 llvm_unreachable("Basic blocks should never be empty!"); 262 } 263 264 /// Rewrite the PHI nodes in an unswitched loop exit basic block. 265 /// 266 /// Requires that the loop exit and unswitched basic block are the same, and 267 /// that the exiting block was a unique predecessor of that block. Rewrites the 268 /// PHI nodes in that block such that what were LCSSA PHI nodes become trivial 269 /// PHI nodes from the old preheader that now contains the unswitched 270 /// terminator. 271 static void rewritePHINodesForUnswitchedExitBlock(BasicBlock &UnswitchedBB, 272 BasicBlock &OldExitingBB, 273 BasicBlock &OldPH) { 274 for (PHINode &PN : UnswitchedBB.phis()) { 275 // When the loop exit is directly unswitched we just need to update the 276 // incoming basic block. We loop to handle weird cases with repeated 277 // incoming blocks, but expect to typically only have one operand here. 278 for (auto i : seq<int>(0, PN.getNumOperands())) { 279 assert(PN.getIncomingBlock(i) == &OldExitingBB && 280 "Found incoming block different from unique predecessor!"); 281 PN.setIncomingBlock(i, &OldPH); 282 } 283 } 284 } 285 286 /// Rewrite the PHI nodes in the loop exit basic block and the split off 287 /// unswitched block. 288 /// 289 /// Because the exit block remains an exit from the loop, this rewrites the 290 /// LCSSA PHI nodes in it to remove the unswitched edge and introduces PHI 291 /// nodes into the unswitched basic block to select between the value in the 292 /// old preheader and the loop exit. 293 static void rewritePHINodesForExitAndUnswitchedBlocks(BasicBlock &ExitBB, 294 BasicBlock &UnswitchedBB, 295 BasicBlock &OldExitingBB, 296 BasicBlock &OldPH) { 297 assert(&ExitBB != &UnswitchedBB && 298 "Must have different loop exit and unswitched blocks!"); 299 Instruction *InsertPt = &*UnswitchedBB.begin(); 300 for (PHINode &PN : ExitBB.phis()) { 301 auto *NewPN = PHINode::Create(PN.getType(), /*NumReservedValues*/ 2, 302 PN.getName() + ".split", InsertPt); 303 304 // Walk backwards over the old PHI node's inputs to minimize the cost of 305 // removing each one. We have to do this weird loop manually so that we 306 // create the same number of new incoming edges in the new PHI as we expect 307 // each case-based edge to be included in the unswitched switch in some 308 // cases. 309 // FIXME: This is really, really gross. It would be much cleaner if LLVM 310 // allowed us to create a single entry for a predecessor block without 311 // having separate entries for each "edge" even though these edges are 312 // required to produce identical results. 313 for (int i = PN.getNumIncomingValues() - 1; i >= 0; --i) { 314 if (PN.getIncomingBlock(i) != &OldExitingBB) 315 continue; 316 317 Value *Incoming = PN.removeIncomingValue(i); 318 NewPN->addIncoming(Incoming, &OldPH); 319 } 320 321 // Now replace the old PHI with the new one and wire the old one in as an 322 // input to the new one. 323 PN.replaceAllUsesWith(NewPN); 324 NewPN->addIncoming(&PN, &ExitBB); 325 } 326 } 327 328 /// Unswitch a trivial branch if the condition is loop invariant. 329 /// 330 /// This routine should only be called when loop code leading to the branch has 331 /// been validated as trivial (no side effects). This routine checks if the 332 /// condition is invariant and one of the successors is a loop exit. This 333 /// allows us to unswitch without duplicating the loop, making it trivial. 334 /// 335 /// If this routine fails to unswitch the branch it returns false. 336 /// 337 /// If the branch can be unswitched, this routine splits the preheader and 338 /// hoists the branch above that split. Preserves loop simplified form 339 /// (splitting the exit block as necessary). It simplifies the branch within 340 /// the loop to an unconditional branch but doesn't remove it entirely. Further 341 /// cleanup can be done with some simplify-cfg like pass. 342 static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT, 343 LoopInfo &LI) { 344 assert(BI.isConditional() && "Can only unswitch a conditional branch!"); 345 DEBUG(dbgs() << " Trying to unswitch branch: " << BI << "\n"); 346 347 Value *LoopCond = BI.getCondition(); 348 349 // Need a trivial loop condition to unswitch. 350 if (!L.isLoopInvariant(LoopCond)) 351 return false; 352 353 // FIXME: We should compute this once at the start and update it! 354 SmallVector<BasicBlock *, 16> ExitBlocks; 355 L.getExitBlocks(ExitBlocks); 356 SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(), 357 ExitBlocks.end()); 358 359 // Check to see if a successor of the branch is guaranteed to 360 // exit through a unique exit block without having any 361 // side-effects. If so, determine the value of Cond that causes 362 // it to do this. 363 ConstantInt *CondVal = ConstantInt::getTrue(BI.getContext()); 364 ConstantInt *Replacement = ConstantInt::getFalse(BI.getContext()); 365 int LoopExitSuccIdx = 0; 366 auto *LoopExitBB = BI.getSuccessor(0); 367 if (!ExitBlockSet.count(LoopExitBB)) { 368 std::swap(CondVal, Replacement); 369 LoopExitSuccIdx = 1; 370 LoopExitBB = BI.getSuccessor(1); 371 if (!ExitBlockSet.count(LoopExitBB)) 372 return false; 373 } 374 auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx); 375 assert(L.contains(ContinueBB) && 376 "Cannot have both successors exit and still be in the loop!"); 377 378 auto *ParentBB = BI.getParent(); 379 if (!areLoopExitPHIsLoopInvariant(L, *ParentBB, *LoopExitBB)) 380 return false; 381 382 DEBUG(dbgs() << " unswitching trivial branch when: " << CondVal 383 << " == " << LoopCond << "\n"); 384 385 // Split the preheader, so that we know that there is a safe place to insert 386 // the conditional branch. We will change the preheader to have a conditional 387 // branch on LoopCond. 388 BasicBlock *OldPH = L.getLoopPreheader(); 389 BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI); 390 391 // Now that we have a place to insert the conditional branch, create a place 392 // to branch to: this is the exit block out of the loop that we are 393 // unswitching. We need to split this if there are other loop predecessors. 394 // Because the loop is in simplified form, *any* other predecessor is enough. 395 BasicBlock *UnswitchedBB; 396 if (BasicBlock *PredBB = LoopExitBB->getUniquePredecessor()) { 397 (void)PredBB; 398 assert(PredBB == BI.getParent() && 399 "A branch's parent isn't a predecessor!"); 400 UnswitchedBB = LoopExitBB; 401 } else { 402 UnswitchedBB = SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI); 403 } 404 405 // Now splice the branch to gate reaching the new preheader and re-point its 406 // successors. 407 OldPH->getInstList().splice(std::prev(OldPH->end()), 408 BI.getParent()->getInstList(), BI); 409 OldPH->getTerminator()->eraseFromParent(); 410 BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB); 411 BI.setSuccessor(1 - LoopExitSuccIdx, NewPH); 412 413 // Create a new unconditional branch that will continue the loop as a new 414 // terminator. 415 BranchInst::Create(ContinueBB, ParentBB); 416 417 // Rewrite the relevant PHI nodes. 418 if (UnswitchedBB == LoopExitBB) 419 rewritePHINodesForUnswitchedExitBlock(*UnswitchedBB, *ParentBB, *OldPH); 420 else 421 rewritePHINodesForExitAndUnswitchedBlocks(*LoopExitBB, *UnswitchedBB, 422 *ParentBB, *OldPH); 423 424 // Now we need to update the dominator tree. 425 updateDTAfterUnswitch(UnswitchedBB, OldPH, DT); 426 // But if we split something off of the loop exit block then we also removed 427 // one of the predecessors for the loop exit block and may need to update its 428 // idom. 429 if (UnswitchedBB != LoopExitBB) 430 updateIDomWithKnownCommonDominator(LoopExitBB, L.getHeader(), DT); 431 432 // Since this is an i1 condition we can also trivially replace uses of it 433 // within the loop with a constant. 434 replaceLoopUsesWithConstant(L, *LoopCond, *Replacement); 435 436 ++NumTrivial; 437 ++NumBranches; 438 return true; 439 } 440 441 /// Unswitch a trivial switch if the condition is loop invariant. 442 /// 443 /// This routine should only be called when loop code leading to the switch has 444 /// been validated as trivial (no side effects). This routine checks if the 445 /// condition is invariant and that at least one of the successors is a loop 446 /// exit. This allows us to unswitch without duplicating the loop, making it 447 /// trivial. 448 /// 449 /// If this routine fails to unswitch the switch it returns false. 450 /// 451 /// If the switch can be unswitched, this routine splits the preheader and 452 /// copies the switch above that split. If the default case is one of the 453 /// exiting cases, it copies the non-exiting cases and points them at the new 454 /// preheader. If the default case is not exiting, it copies the exiting cases 455 /// and points the default at the preheader. It preserves loop simplified form 456 /// (splitting the exit blocks as necessary). It simplifies the switch within 457 /// the loop by removing now-dead cases. If the default case is one of those 458 /// unswitched, it replaces its destination with a new basic block containing 459 /// only unreachable. Such basic blocks, while technically loop exits, are not 460 /// considered for unswitching so this is a stable transform and the same 461 /// switch will not be revisited. If after unswitching there is only a single 462 /// in-loop successor, the switch is further simplified to an unconditional 463 /// branch. Still more cleanup can be done with some simplify-cfg like pass. 464 static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT, 465 LoopInfo &LI) { 466 DEBUG(dbgs() << " Trying to unswitch switch: " << SI << "\n"); 467 Value *LoopCond = SI.getCondition(); 468 469 // If this isn't switching on an invariant condition, we can't unswitch it. 470 if (!L.isLoopInvariant(LoopCond)) 471 return false; 472 473 auto *ParentBB = SI.getParent(); 474 475 // FIXME: We should compute this once at the start and update it! 476 SmallVector<BasicBlock *, 16> ExitBlocks; 477 L.getExitBlocks(ExitBlocks); 478 SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(), 479 ExitBlocks.end()); 480 481 SmallVector<int, 4> ExitCaseIndices; 482 for (auto Case : SI.cases()) { 483 auto *SuccBB = Case.getCaseSuccessor(); 484 if (ExitBlockSet.count(SuccBB) && 485 areLoopExitPHIsLoopInvariant(L, *ParentBB, *SuccBB)) 486 ExitCaseIndices.push_back(Case.getCaseIndex()); 487 } 488 BasicBlock *DefaultExitBB = nullptr; 489 if (ExitBlockSet.count(SI.getDefaultDest()) && 490 areLoopExitPHIsLoopInvariant(L, *ParentBB, *SI.getDefaultDest()) && 491 !isa<UnreachableInst>(SI.getDefaultDest()->getTerminator())) 492 DefaultExitBB = SI.getDefaultDest(); 493 else if (ExitCaseIndices.empty()) 494 return false; 495 496 DEBUG(dbgs() << " unswitching trivial cases...\n"); 497 498 SmallVector<std::pair<ConstantInt *, BasicBlock *>, 4> ExitCases; 499 ExitCases.reserve(ExitCaseIndices.size()); 500 // We walk the case indices backwards so that we remove the last case first 501 // and don't disrupt the earlier indices. 502 for (unsigned Index : reverse(ExitCaseIndices)) { 503 auto CaseI = SI.case_begin() + Index; 504 // Save the value of this case. 505 ExitCases.push_back({CaseI->getCaseValue(), CaseI->getCaseSuccessor()}); 506 // Delete the unswitched cases. 507 SI.removeCase(CaseI); 508 } 509 510 // Check if after this all of the remaining cases point at the same 511 // successor. 512 BasicBlock *CommonSuccBB = nullptr; 513 if (SI.getNumCases() > 0 && 514 std::all_of(std::next(SI.case_begin()), SI.case_end(), 515 [&SI](const SwitchInst::CaseHandle &Case) { 516 return Case.getCaseSuccessor() == 517 SI.case_begin()->getCaseSuccessor(); 518 })) 519 CommonSuccBB = SI.case_begin()->getCaseSuccessor(); 520 521 if (DefaultExitBB) { 522 // We can't remove the default edge so replace it with an edge to either 523 // the single common remaining successor (if we have one) or an unreachable 524 // block. 525 if (CommonSuccBB) { 526 SI.setDefaultDest(CommonSuccBB); 527 } else { 528 BasicBlock *UnreachableBB = BasicBlock::Create( 529 ParentBB->getContext(), 530 Twine(ParentBB->getName()) + ".unreachable_default", 531 ParentBB->getParent()); 532 new UnreachableInst(ParentBB->getContext(), UnreachableBB); 533 SI.setDefaultDest(UnreachableBB); 534 DT.addNewBlock(UnreachableBB, ParentBB); 535 } 536 } else { 537 // If we're not unswitching the default, we need it to match any cases to 538 // have a common successor or if we have no cases it is the common 539 // successor. 540 if (SI.getNumCases() == 0) 541 CommonSuccBB = SI.getDefaultDest(); 542 else if (SI.getDefaultDest() != CommonSuccBB) 543 CommonSuccBB = nullptr; 544 } 545 546 // Split the preheader, so that we know that there is a safe place to insert 547 // the switch. 548 BasicBlock *OldPH = L.getLoopPreheader(); 549 BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI); 550 OldPH->getTerminator()->eraseFromParent(); 551 552 // Now add the unswitched switch. 553 auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH); 554 555 // Rewrite the IR for the unswitched basic blocks. This requires two steps. 556 // First, we split any exit blocks with remaining in-loop predecessors. Then 557 // we update the PHIs in one of two ways depending on if there was a split. 558 // We walk in reverse so that we split in the same order as the cases 559 // appeared. This is purely for convenience of reading the resulting IR, but 560 // it doesn't cost anything really. 561 SmallPtrSet<BasicBlock *, 2> UnswitchedExitBBs; 562 SmallDenseMap<BasicBlock *, BasicBlock *, 2> SplitExitBBMap; 563 // Handle the default exit if necessary. 564 // FIXME: It'd be great if we could merge this with the loop below but LLVM's 565 // ranges aren't quite powerful enough yet. 566 if (DefaultExitBB) { 567 if (pred_empty(DefaultExitBB)) { 568 UnswitchedExitBBs.insert(DefaultExitBB); 569 rewritePHINodesForUnswitchedExitBlock(*DefaultExitBB, *ParentBB, *OldPH); 570 } else { 571 auto *SplitBB = 572 SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI); 573 rewritePHINodesForExitAndUnswitchedBlocks(*DefaultExitBB, *SplitBB, 574 *ParentBB, *OldPH); 575 updateIDomWithKnownCommonDominator(DefaultExitBB, L.getHeader(), DT); 576 DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB; 577 } 578 } 579 // Note that we must use a reference in the for loop so that we update the 580 // container. 581 for (auto &CasePair : reverse(ExitCases)) { 582 // Grab a reference to the exit block in the pair so that we can update it. 583 BasicBlock *ExitBB = CasePair.second; 584 585 // If this case is the last edge into the exit block, we can simply reuse it 586 // as it will no longer be a loop exit. No mapping necessary. 587 if (pred_empty(ExitBB)) { 588 // Only rewrite once. 589 if (UnswitchedExitBBs.insert(ExitBB).second) 590 rewritePHINodesForUnswitchedExitBlock(*ExitBB, *ParentBB, *OldPH); 591 continue; 592 } 593 594 // Otherwise we need to split the exit block so that we retain an exit 595 // block from the loop and a target for the unswitched condition. 596 BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB]; 597 if (!SplitExitBB) { 598 // If this is the first time we see this, do the split and remember it. 599 SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI); 600 rewritePHINodesForExitAndUnswitchedBlocks(*ExitBB, *SplitExitBB, 601 *ParentBB, *OldPH); 602 updateIDomWithKnownCommonDominator(ExitBB, L.getHeader(), DT); 603 } 604 // Update the case pair to point to the split block. 605 CasePair.second = SplitExitBB; 606 } 607 608 // Now add the unswitched cases. We do this in reverse order as we built them 609 // in reverse order. 610 for (auto CasePair : reverse(ExitCases)) { 611 ConstantInt *CaseVal = CasePair.first; 612 BasicBlock *UnswitchedBB = CasePair.second; 613 614 NewSI->addCase(CaseVal, UnswitchedBB); 615 updateDTAfterUnswitch(UnswitchedBB, OldPH, DT); 616 } 617 618 // If the default was unswitched, re-point it and add explicit cases for 619 // entering the loop. 620 if (DefaultExitBB) { 621 NewSI->setDefaultDest(DefaultExitBB); 622 updateDTAfterUnswitch(DefaultExitBB, OldPH, DT); 623 624 // We removed all the exit cases, so we just copy the cases to the 625 // unswitched switch. 626 for (auto Case : SI.cases()) 627 NewSI->addCase(Case.getCaseValue(), NewPH); 628 } 629 630 // If we ended up with a common successor for every path through the switch 631 // after unswitching, rewrite it to an unconditional branch to make it easy 632 // to recognize. Otherwise we potentially have to recognize the default case 633 // pointing at unreachable and other complexity. 634 if (CommonSuccBB) { 635 BasicBlock *BB = SI.getParent(); 636 SI.eraseFromParent(); 637 BranchInst::Create(CommonSuccBB, BB); 638 } 639 640 assert(DT.verify(DominatorTree::VerificationLevel::Fast)); 641 ++NumTrivial; 642 ++NumSwitches; 643 return true; 644 } 645 646 /// This routine scans the loop to find a branch or switch which occurs before 647 /// any side effects occur. These can potentially be unswitched without 648 /// duplicating the loop. If a branch or switch is successfully unswitched the 649 /// scanning continues to see if subsequent branches or switches have become 650 /// trivial. Once all trivial candidates have been unswitched, this routine 651 /// returns. 652 /// 653 /// The return value indicates whether anything was unswitched (and therefore 654 /// changed). 655 static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT, 656 LoopInfo &LI) { 657 bool Changed = false; 658 659 // If loop header has only one reachable successor we should keep looking for 660 // trivial condition candidates in the successor as well. An alternative is 661 // to constant fold conditions and merge successors into loop header (then we 662 // only need to check header's terminator). The reason for not doing this in 663 // LoopUnswitch pass is that it could potentially break LoopPassManager's 664 // invariants. Folding dead branches could either eliminate the current loop 665 // or make other loops unreachable. LCSSA form might also not be preserved 666 // after deleting branches. The following code keeps traversing loop header's 667 // successors until it finds the trivial condition candidate (condition that 668 // is not a constant). Since unswitching generates branches with constant 669 // conditions, this scenario could be very common in practice. 670 BasicBlock *CurrentBB = L.getHeader(); 671 SmallPtrSet<BasicBlock *, 8> Visited; 672 Visited.insert(CurrentBB); 673 do { 674 // Check if there are any side-effecting instructions (e.g. stores, calls, 675 // volatile loads) in the part of the loop that the code *would* execute 676 // without unswitching. 677 if (llvm::any_of(*CurrentBB, 678 [](Instruction &I) { return I.mayHaveSideEffects(); })) 679 return Changed; 680 681 TerminatorInst *CurrentTerm = CurrentBB->getTerminator(); 682 683 if (auto *SI = dyn_cast<SwitchInst>(CurrentTerm)) { 684 // Don't bother trying to unswitch past a switch with a constant 685 // condition. This should be removed prior to running this pass by 686 // simplify-cfg. 687 if (isa<Constant>(SI->getCondition())) 688 return Changed; 689 690 if (!unswitchTrivialSwitch(L, *SI, DT, LI)) 691 // Coludn't unswitch this one so we're done. 692 return Changed; 693 694 // Mark that we managed to unswitch something. 695 Changed = true; 696 697 // If unswitching turned the terminator into an unconditional branch then 698 // we can continue. The unswitching logic specifically works to fold any 699 // cases it can into an unconditional branch to make it easier to 700 // recognize here. 701 auto *BI = dyn_cast<BranchInst>(CurrentBB->getTerminator()); 702 if (!BI || BI->isConditional()) 703 return Changed; 704 705 CurrentBB = BI->getSuccessor(0); 706 continue; 707 } 708 709 auto *BI = dyn_cast<BranchInst>(CurrentTerm); 710 if (!BI) 711 // We do not understand other terminator instructions. 712 return Changed; 713 714 // Don't bother trying to unswitch past an unconditional branch or a branch 715 // with a constant value. These should be removed by simplify-cfg prior to 716 // running this pass. 717 if (!BI->isConditional() || isa<Constant>(BI->getCondition())) 718 return Changed; 719 720 // Found a trivial condition candidate: non-foldable conditional branch. If 721 // we fail to unswitch this, we can't do anything else that is trivial. 722 if (!unswitchTrivialBranch(L, *BI, DT, LI)) 723 return Changed; 724 725 // Mark that we managed to unswitch something. 726 Changed = true; 727 728 // We unswitched the branch. This should always leave us with an 729 // unconditional branch that we can follow now. 730 BI = cast<BranchInst>(CurrentBB->getTerminator()); 731 assert(!BI->isConditional() && 732 "Cannot form a conditional branch by unswitching1"); 733 CurrentBB = BI->getSuccessor(0); 734 735 // When continuing, if we exit the loop or reach a previous visited block, 736 // then we can not reach any trivial condition candidates (unfoldable 737 // branch instructions or switch instructions) and no unswitch can happen. 738 } while (L.contains(CurrentBB) && Visited.insert(CurrentBB).second); 739 740 return Changed; 741 } 742 743 /// Build the cloned blocks for an unswitched copy of the given loop. 744 /// 745 /// The cloned blocks are inserted before the loop preheader (`LoopPH`) and 746 /// after the split block (`SplitBB`) that will be used to select between the 747 /// cloned and original loop. 748 /// 749 /// This routine handles cloning all of the necessary loop blocks and exit 750 /// blocks including rewriting their instructions and the relevant PHI nodes. 751 /// It skips loop and exit blocks that are not necessary based on the provided 752 /// set. It also correctly creates the unconditional branch in the cloned 753 /// unswitched parent block to only point at the unswitched successor. 754 /// 755 /// This does not handle most of the necessary updates to `LoopInfo`. Only exit 756 /// block splitting is correctly reflected in `LoopInfo`, essentially all of 757 /// the cloned blocks (and their loops) are left without full `LoopInfo` 758 /// updates. This also doesn't fully update `DominatorTree`. It adds the cloned 759 /// blocks to them but doesn't create the cloned `DominatorTree` structure and 760 /// instead the caller must recompute an accurate DT. It *does* correctly 761 /// update the `AssumptionCache` provided in `AC`. 762 static BasicBlock *buildClonedLoopBlocks( 763 Loop &L, BasicBlock *LoopPH, BasicBlock *SplitBB, 764 ArrayRef<BasicBlock *> ExitBlocks, BasicBlock *ParentBB, 765 BasicBlock *UnswitchedSuccBB, BasicBlock *ContinueSuccBB, 766 const SmallPtrSetImpl<BasicBlock *> &SkippedLoopAndExitBlocks, 767 ValueToValueMapTy &VMap, AssumptionCache &AC, DominatorTree &DT, 768 LoopInfo &LI) { 769 SmallVector<BasicBlock *, 4> NewBlocks; 770 NewBlocks.reserve(L.getNumBlocks() + ExitBlocks.size()); 771 772 // We will need to clone a bunch of blocks, wrap up the clone operation in 773 // a helper. 774 auto CloneBlock = [&](BasicBlock *OldBB) { 775 // Clone the basic block and insert it before the new preheader. 776 BasicBlock *NewBB = CloneBasicBlock(OldBB, VMap, ".us", OldBB->getParent()); 777 NewBB->moveBefore(LoopPH); 778 779 // Record this block and the mapping. 780 NewBlocks.push_back(NewBB); 781 VMap[OldBB] = NewBB; 782 783 // Add the block to the domtree. We'll move it to the correct position 784 // below. 785 DT.addNewBlock(NewBB, SplitBB); 786 787 return NewBB; 788 }; 789 790 // First, clone the preheader. 791 auto *ClonedPH = CloneBlock(LoopPH); 792 793 // Then clone all the loop blocks, skipping the ones that aren't necessary. 794 for (auto *LoopBB : L.blocks()) 795 if (!SkippedLoopAndExitBlocks.count(LoopBB)) 796 CloneBlock(LoopBB); 797 798 // Split all the loop exit edges so that when we clone the exit blocks, if 799 // any of the exit blocks are *also* a preheader for some other loop, we 800 // don't create multiple predecessors entering the loop header. 801 for (auto *ExitBB : ExitBlocks) { 802 if (SkippedLoopAndExitBlocks.count(ExitBB)) 803 continue; 804 805 // When we are going to clone an exit, we don't need to clone all the 806 // instructions in the exit block and we want to ensure we have an easy 807 // place to merge the CFG, so split the exit first. This is always safe to 808 // do because there cannot be any non-loop predecessors of a loop exit in 809 // loop simplified form. 810 auto *MergeBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI); 811 812 // Rearrange the names to make it easier to write test cases by having the 813 // exit block carry the suffix rather than the merge block carrying the 814 // suffix. 815 MergeBB->takeName(ExitBB); 816 ExitBB->setName(Twine(MergeBB->getName()) + ".split"); 817 818 // Now clone the original exit block. 819 auto *ClonedExitBB = CloneBlock(ExitBB); 820 assert(ClonedExitBB->getTerminator()->getNumSuccessors() == 1 && 821 "Exit block should have been split to have one successor!"); 822 assert(ClonedExitBB->getTerminator()->getSuccessor(0) == MergeBB && 823 "Cloned exit block has the wrong successor!"); 824 825 // Move the merge block's idom to be the split point as one exit is 826 // dominated by one header, and the other by another, so we know the split 827 // point dominates both. While the dominator tree isn't fully accurate, we 828 // want sub-trees within the original loop to be correctly reflect 829 // dominance within that original loop (at least) and that requires moving 830 // the merge block out of that subtree. 831 // FIXME: This is very brittle as we essentially have a partial contract on 832 // the dominator tree. We really need to instead update it and keep it 833 // valid or stop relying on it. 834 DT.changeImmediateDominator(MergeBB, SplitBB); 835 836 // Remap any cloned instructions and create a merge phi node for them. 837 for (auto ZippedInsts : llvm::zip_first( 838 llvm::make_range(ExitBB->begin(), std::prev(ExitBB->end())), 839 llvm::make_range(ClonedExitBB->begin(), 840 std::prev(ClonedExitBB->end())))) { 841 Instruction &I = std::get<0>(ZippedInsts); 842 Instruction &ClonedI = std::get<1>(ZippedInsts); 843 844 // The only instructions in the exit block should be PHI nodes and 845 // potentially a landing pad. 846 assert( 847 (isa<PHINode>(I) || isa<LandingPadInst>(I) || isa<CatchPadInst>(I)) && 848 "Bad instruction in exit block!"); 849 // We should have a value map between the instruction and its clone. 850 assert(VMap.lookup(&I) == &ClonedI && "Mismatch in the value map!"); 851 852 auto *MergePN = 853 PHINode::Create(I.getType(), /*NumReservedValues*/ 2, ".us-phi", 854 &*MergeBB->getFirstInsertionPt()); 855 I.replaceAllUsesWith(MergePN); 856 MergePN->addIncoming(&I, ExitBB); 857 MergePN->addIncoming(&ClonedI, ClonedExitBB); 858 } 859 } 860 861 // Rewrite the instructions in the cloned blocks to refer to the instructions 862 // in the cloned blocks. We have to do this as a second pass so that we have 863 // everything available. Also, we have inserted new instructions which may 864 // include assume intrinsics, so we update the assumption cache while 865 // processing this. 866 for (auto *ClonedBB : NewBlocks) 867 for (Instruction &I : *ClonedBB) { 868 RemapInstruction(&I, VMap, 869 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals); 870 if (auto *II = dyn_cast<IntrinsicInst>(&I)) 871 if (II->getIntrinsicID() == Intrinsic::assume) 872 AC.registerAssumption(II); 873 } 874 875 // Remove the cloned parent as a predecessor of the cloned continue successor 876 // if we did in fact clone it. 877 auto *ClonedParentBB = cast<BasicBlock>(VMap.lookup(ParentBB)); 878 if (auto *ClonedContinueSuccBB = 879 cast_or_null<BasicBlock>(VMap.lookup(ContinueSuccBB))) 880 ClonedContinueSuccBB->removePredecessor(ClonedParentBB, 881 /*DontDeleteUselessPHIs*/ true); 882 // Replace the cloned branch with an unconditional branch to the cloneed 883 // unswitched successor. 884 auto *ClonedSuccBB = cast<BasicBlock>(VMap.lookup(UnswitchedSuccBB)); 885 ClonedParentBB->getTerminator()->eraseFromParent(); 886 BranchInst::Create(ClonedSuccBB, ClonedParentBB); 887 888 // Update any PHI nodes in the cloned successors of the skipped blocks to not 889 // have spurious incoming values. 890 for (auto *LoopBB : L.blocks()) 891 if (SkippedLoopAndExitBlocks.count(LoopBB)) 892 for (auto *SuccBB : successors(LoopBB)) 893 if (auto *ClonedSuccBB = cast_or_null<BasicBlock>(VMap.lookup(SuccBB))) 894 for (PHINode &PN : ClonedSuccBB->phis()) 895 PN.removeIncomingValue(LoopBB, /*DeletePHIIfEmpty*/ false); 896 897 return ClonedPH; 898 } 899 900 /// Recursively clone the specified loop and all of its children. 901 /// 902 /// The target parent loop for the clone should be provided, or can be null if 903 /// the clone is a top-level loop. While cloning, all the blocks are mapped 904 /// with the provided value map. The entire original loop must be present in 905 /// the value map. The cloned loop is returned. 906 static Loop *cloneLoopNest(Loop &OrigRootL, Loop *RootParentL, 907 const ValueToValueMapTy &VMap, LoopInfo &LI) { 908 auto AddClonedBlocksToLoop = [&](Loop &OrigL, Loop &ClonedL) { 909 assert(ClonedL.getBlocks().empty() && "Must start with an empty loop!"); 910 ClonedL.reserveBlocks(OrigL.getNumBlocks()); 911 for (auto *BB : OrigL.blocks()) { 912 auto *ClonedBB = cast<BasicBlock>(VMap.lookup(BB)); 913 ClonedL.addBlockEntry(ClonedBB); 914 if (LI.getLoopFor(BB) == &OrigL) { 915 assert(!LI.getLoopFor(ClonedBB) && 916 "Should not have an existing loop for this block!"); 917 LI.changeLoopFor(ClonedBB, &ClonedL); 918 } 919 } 920 }; 921 922 // We specially handle the first loop because it may get cloned into 923 // a different parent and because we most commonly are cloning leaf loops. 924 Loop *ClonedRootL = LI.AllocateLoop(); 925 if (RootParentL) 926 RootParentL->addChildLoop(ClonedRootL); 927 else 928 LI.addTopLevelLoop(ClonedRootL); 929 AddClonedBlocksToLoop(OrigRootL, *ClonedRootL); 930 931 if (OrigRootL.empty()) 932 return ClonedRootL; 933 934 // If we have a nest, we can quickly clone the entire loop nest using an 935 // iterative approach because it is a tree. We keep the cloned parent in the 936 // data structure to avoid repeatedly querying through a map to find it. 937 SmallVector<std::pair<Loop *, Loop *>, 16> LoopsToClone; 938 // Build up the loops to clone in reverse order as we'll clone them from the 939 // back. 940 for (Loop *ChildL : llvm::reverse(OrigRootL)) 941 LoopsToClone.push_back({ClonedRootL, ChildL}); 942 do { 943 Loop *ClonedParentL, *L; 944 std::tie(ClonedParentL, L) = LoopsToClone.pop_back_val(); 945 Loop *ClonedL = LI.AllocateLoop(); 946 ClonedParentL->addChildLoop(ClonedL); 947 AddClonedBlocksToLoop(*L, *ClonedL); 948 for (Loop *ChildL : llvm::reverse(*L)) 949 LoopsToClone.push_back({ClonedL, ChildL}); 950 } while (!LoopsToClone.empty()); 951 952 return ClonedRootL; 953 } 954 955 /// Build the cloned loops of an original loop from unswitching. 956 /// 957 /// Because unswitching simplifies the CFG of the loop, this isn't a trivial 958 /// operation. We need to re-verify that there even is a loop (as the backedge 959 /// may not have been cloned), and even if there are remaining backedges the 960 /// backedge set may be different. However, we know that each child loop is 961 /// undisturbed, we only need to find where to place each child loop within 962 /// either any parent loop or within a cloned version of the original loop. 963 /// 964 /// Because child loops may end up cloned outside of any cloned version of the 965 /// original loop, multiple cloned sibling loops may be created. All of them 966 /// are returned so that the newly introduced loop nest roots can be 967 /// identified. 968 static Loop *buildClonedLoops(Loop &OrigL, ArrayRef<BasicBlock *> ExitBlocks, 969 const ValueToValueMapTy &VMap, LoopInfo &LI, 970 SmallVectorImpl<Loop *> &NonChildClonedLoops) { 971 Loop *ClonedL = nullptr; 972 973 auto *OrigPH = OrigL.getLoopPreheader(); 974 auto *OrigHeader = OrigL.getHeader(); 975 976 auto *ClonedPH = cast<BasicBlock>(VMap.lookup(OrigPH)); 977 auto *ClonedHeader = cast<BasicBlock>(VMap.lookup(OrigHeader)); 978 979 // We need to know the loops of the cloned exit blocks to even compute the 980 // accurate parent loop. If we only clone exits to some parent of the 981 // original parent, we want to clone into that outer loop. We also keep track 982 // of the loops that our cloned exit blocks participate in. 983 Loop *ParentL = nullptr; 984 SmallVector<BasicBlock *, 4> ClonedExitsInLoops; 985 SmallDenseMap<BasicBlock *, Loop *, 16> ExitLoopMap; 986 ClonedExitsInLoops.reserve(ExitBlocks.size()); 987 for (auto *ExitBB : ExitBlocks) 988 if (auto *ClonedExitBB = cast_or_null<BasicBlock>(VMap.lookup(ExitBB))) 989 if (Loop *ExitL = LI.getLoopFor(ExitBB)) { 990 ExitLoopMap[ClonedExitBB] = ExitL; 991 ClonedExitsInLoops.push_back(ClonedExitBB); 992 if (!ParentL || (ParentL != ExitL && ParentL->contains(ExitL))) 993 ParentL = ExitL; 994 } 995 assert((!ParentL || ParentL == OrigL.getParentLoop() || 996 ParentL->contains(OrigL.getParentLoop())) && 997 "The computed parent loop should always contain (or be) the parent of " 998 "the original loop."); 999 1000 // We build the set of blocks dominated by the cloned header from the set of 1001 // cloned blocks out of the original loop. While not all of these will 1002 // necessarily be in the cloned loop, it is enough to establish that they 1003 // aren't in unreachable cycles, etc. 1004 SmallSetVector<BasicBlock *, 16> ClonedLoopBlocks; 1005 for (auto *BB : OrigL.blocks()) 1006 if (auto *ClonedBB = cast_or_null<BasicBlock>(VMap.lookup(BB))) 1007 ClonedLoopBlocks.insert(ClonedBB); 1008 1009 // Rebuild the set of blocks that will end up in the cloned loop. We may have 1010 // skipped cloning some region of this loop which can in turn skip some of 1011 // the backedges so we have to rebuild the blocks in the loop based on the 1012 // backedges that remain after cloning. 1013 SmallVector<BasicBlock *, 16> Worklist; 1014 SmallPtrSet<BasicBlock *, 16> BlocksInClonedLoop; 1015 for (auto *Pred : predecessors(ClonedHeader)) { 1016 // The only possible non-loop header predecessor is the preheader because 1017 // we know we cloned the loop in simplified form. 1018 if (Pred == ClonedPH) 1019 continue; 1020 1021 // Because the loop was in simplified form, the only non-loop predecessor 1022 // should be the preheader. 1023 assert(ClonedLoopBlocks.count(Pred) && "Found a predecessor of the loop " 1024 "header other than the preheader " 1025 "that is not part of the loop!"); 1026 1027 // Insert this block into the loop set and on the first visit (and if it 1028 // isn't the header we're currently walking) put it into the worklist to 1029 // recurse through. 1030 if (BlocksInClonedLoop.insert(Pred).second && Pred != ClonedHeader) 1031 Worklist.push_back(Pred); 1032 } 1033 1034 // If we had any backedges then there *is* a cloned loop. Put the header into 1035 // the loop set and then walk the worklist backwards to find all the blocks 1036 // that remain within the loop after cloning. 1037 if (!BlocksInClonedLoop.empty()) { 1038 BlocksInClonedLoop.insert(ClonedHeader); 1039 1040 while (!Worklist.empty()) { 1041 BasicBlock *BB = Worklist.pop_back_val(); 1042 assert(BlocksInClonedLoop.count(BB) && 1043 "Didn't put block into the loop set!"); 1044 1045 // Insert any predecessors that are in the possible set into the cloned 1046 // set, and if the insert is successful, add them to the worklist. Note 1047 // that we filter on the blocks that are definitely reachable via the 1048 // backedge to the loop header so we may prune out dead code within the 1049 // cloned loop. 1050 for (auto *Pred : predecessors(BB)) 1051 if (ClonedLoopBlocks.count(Pred) && 1052 BlocksInClonedLoop.insert(Pred).second) 1053 Worklist.push_back(Pred); 1054 } 1055 1056 ClonedL = LI.AllocateLoop(); 1057 if (ParentL) { 1058 ParentL->addBasicBlockToLoop(ClonedPH, LI); 1059 ParentL->addChildLoop(ClonedL); 1060 } else { 1061 LI.addTopLevelLoop(ClonedL); 1062 } 1063 1064 ClonedL->reserveBlocks(BlocksInClonedLoop.size()); 1065 // We don't want to just add the cloned loop blocks based on how we 1066 // discovered them. The original order of blocks was carefully built in 1067 // a way that doesn't rely on predecessor ordering. Rather than re-invent 1068 // that logic, we just re-walk the original blocks (and those of the child 1069 // loops) and filter them as we add them into the cloned loop. 1070 for (auto *BB : OrigL.blocks()) { 1071 auto *ClonedBB = cast_or_null<BasicBlock>(VMap.lookup(BB)); 1072 if (!ClonedBB || !BlocksInClonedLoop.count(ClonedBB)) 1073 continue; 1074 1075 // Directly add the blocks that are only in this loop. 1076 if (LI.getLoopFor(BB) == &OrigL) { 1077 ClonedL->addBasicBlockToLoop(ClonedBB, LI); 1078 continue; 1079 } 1080 1081 // We want to manually add it to this loop and parents. 1082 // Registering it with LoopInfo will happen when we clone the top 1083 // loop for this block. 1084 for (Loop *PL = ClonedL; PL; PL = PL->getParentLoop()) 1085 PL->addBlockEntry(ClonedBB); 1086 } 1087 1088 // Now add each child loop whose header remains within the cloned loop. All 1089 // of the blocks within the loop must satisfy the same constraints as the 1090 // header so once we pass the header checks we can just clone the entire 1091 // child loop nest. 1092 for (Loop *ChildL : OrigL) { 1093 auto *ClonedChildHeader = 1094 cast_or_null<BasicBlock>(VMap.lookup(ChildL->getHeader())); 1095 if (!ClonedChildHeader || !BlocksInClonedLoop.count(ClonedChildHeader)) 1096 continue; 1097 1098 #ifndef NDEBUG 1099 // We should never have a cloned child loop header but fail to have 1100 // all of the blocks for that child loop. 1101 for (auto *ChildLoopBB : ChildL->blocks()) 1102 assert(BlocksInClonedLoop.count( 1103 cast<BasicBlock>(VMap.lookup(ChildLoopBB))) && 1104 "Child cloned loop has a header within the cloned outer " 1105 "loop but not all of its blocks!"); 1106 #endif 1107 1108 cloneLoopNest(*ChildL, ClonedL, VMap, LI); 1109 } 1110 } 1111 1112 // Now that we've handled all the components of the original loop that were 1113 // cloned into a new loop, we still need to handle anything from the original 1114 // loop that wasn't in a cloned loop. 1115 1116 // Figure out what blocks are left to place within any loop nest containing 1117 // the unswitched loop. If we never formed a loop, the cloned PH is one of 1118 // them. 1119 SmallPtrSet<BasicBlock *, 16> UnloopedBlockSet; 1120 if (BlocksInClonedLoop.empty()) 1121 UnloopedBlockSet.insert(ClonedPH); 1122 for (auto *ClonedBB : ClonedLoopBlocks) 1123 if (!BlocksInClonedLoop.count(ClonedBB)) 1124 UnloopedBlockSet.insert(ClonedBB); 1125 1126 // Copy the cloned exits and sort them in ascending loop depth, we'll work 1127 // backwards across these to process them inside out. The order shouldn't 1128 // matter as we're just trying to build up the map from inside-out; we use 1129 // the map in a more stably ordered way below. 1130 auto OrderedClonedExitsInLoops = ClonedExitsInLoops; 1131 llvm::sort(OrderedClonedExitsInLoops.begin(), 1132 OrderedClonedExitsInLoops.end(), 1133 [&](BasicBlock *LHS, BasicBlock *RHS) { 1134 return ExitLoopMap.lookup(LHS)->getLoopDepth() < 1135 ExitLoopMap.lookup(RHS)->getLoopDepth(); 1136 }); 1137 1138 // Populate the existing ExitLoopMap with everything reachable from each 1139 // exit, starting from the inner most exit. 1140 while (!UnloopedBlockSet.empty() && !OrderedClonedExitsInLoops.empty()) { 1141 assert(Worklist.empty() && "Didn't clear worklist!"); 1142 1143 BasicBlock *ExitBB = OrderedClonedExitsInLoops.pop_back_val(); 1144 Loop *ExitL = ExitLoopMap.lookup(ExitBB); 1145 1146 // Walk the CFG back until we hit the cloned PH adding everything reachable 1147 // and in the unlooped set to this exit block's loop. 1148 Worklist.push_back(ExitBB); 1149 do { 1150 BasicBlock *BB = Worklist.pop_back_val(); 1151 // We can stop recursing at the cloned preheader (if we get there). 1152 if (BB == ClonedPH) 1153 continue; 1154 1155 for (BasicBlock *PredBB : predecessors(BB)) { 1156 // If this pred has already been moved to our set or is part of some 1157 // (inner) loop, no update needed. 1158 if (!UnloopedBlockSet.erase(PredBB)) { 1159 assert( 1160 (BlocksInClonedLoop.count(PredBB) || ExitLoopMap.count(PredBB)) && 1161 "Predecessor not mapped to a loop!"); 1162 continue; 1163 } 1164 1165 // We just insert into the loop set here. We'll add these blocks to the 1166 // exit loop after we build up the set in an order that doesn't rely on 1167 // predecessor order (which in turn relies on use list order). 1168 bool Inserted = ExitLoopMap.insert({PredBB, ExitL}).second; 1169 (void)Inserted; 1170 assert(Inserted && "Should only visit an unlooped block once!"); 1171 1172 // And recurse through to its predecessors. 1173 Worklist.push_back(PredBB); 1174 } 1175 } while (!Worklist.empty()); 1176 } 1177 1178 // Now that the ExitLoopMap gives as mapping for all the non-looping cloned 1179 // blocks to their outer loops, walk the cloned blocks and the cloned exits 1180 // in their original order adding them to the correct loop. 1181 1182 // We need a stable insertion order. We use the order of the original loop 1183 // order and map into the correct parent loop. 1184 for (auto *BB : llvm::concat<BasicBlock *const>( 1185 makeArrayRef(ClonedPH), ClonedLoopBlocks, ClonedExitsInLoops)) 1186 if (Loop *OuterL = ExitLoopMap.lookup(BB)) 1187 OuterL->addBasicBlockToLoop(BB, LI); 1188 1189 #ifndef NDEBUG 1190 for (auto &BBAndL : ExitLoopMap) { 1191 auto *BB = BBAndL.first; 1192 auto *OuterL = BBAndL.second; 1193 assert(LI.getLoopFor(BB) == OuterL && 1194 "Failed to put all blocks into outer loops!"); 1195 } 1196 #endif 1197 1198 // Now that all the blocks are placed into the correct containing loop in the 1199 // absence of child loops, find all the potentially cloned child loops and 1200 // clone them into whatever outer loop we placed their header into. 1201 for (Loop *ChildL : OrigL) { 1202 auto *ClonedChildHeader = 1203 cast_or_null<BasicBlock>(VMap.lookup(ChildL->getHeader())); 1204 if (!ClonedChildHeader || BlocksInClonedLoop.count(ClonedChildHeader)) 1205 continue; 1206 1207 #ifndef NDEBUG 1208 for (auto *ChildLoopBB : ChildL->blocks()) 1209 assert(VMap.count(ChildLoopBB) && 1210 "Cloned a child loop header but not all of that loops blocks!"); 1211 #endif 1212 1213 NonChildClonedLoops.push_back(cloneLoopNest( 1214 *ChildL, ExitLoopMap.lookup(ClonedChildHeader), VMap, LI)); 1215 } 1216 1217 // Return the main cloned loop if any. 1218 return ClonedL; 1219 } 1220 1221 static void deleteDeadBlocksFromLoop(Loop &L, BasicBlock *DeadSubtreeRoot, 1222 SmallVectorImpl<BasicBlock *> &ExitBlocks, 1223 DominatorTree &DT, LoopInfo &LI) { 1224 // Walk the dominator tree to build up the set of blocks we will delete here. 1225 // The order is designed to allow us to always delete bottom-up and avoid any 1226 // dangling uses. 1227 SmallSetVector<BasicBlock *, 16> DeadBlocks; 1228 DeadBlocks.insert(DeadSubtreeRoot); 1229 for (int i = 0; i < (int)DeadBlocks.size(); ++i) 1230 for (DomTreeNode *ChildN : *DT[DeadBlocks[i]]) { 1231 // FIXME: This assert should pass and that means we don't change nearly 1232 // as much below! Consider rewriting all of this to avoid deleting 1233 // blocks. They are always cloned before being deleted, and so instead 1234 // could just be moved. 1235 // FIXME: This in turn means that we might actually be more able to 1236 // update the domtree. 1237 assert((L.contains(ChildN->getBlock()) || 1238 llvm::find(ExitBlocks, ChildN->getBlock()) != ExitBlocks.end()) && 1239 "Should never reach beyond the loop and exits when deleting!"); 1240 DeadBlocks.insert(ChildN->getBlock()); 1241 } 1242 1243 // Filter out the dead blocks from the exit blocks list so that it can be 1244 // used in the caller. 1245 llvm::erase_if(ExitBlocks, 1246 [&](BasicBlock *BB) { return DeadBlocks.count(BB); }); 1247 1248 // Remove these blocks from their successors. 1249 for (auto *BB : DeadBlocks) 1250 for (BasicBlock *SuccBB : successors(BB)) 1251 SuccBB->removePredecessor(BB, /*DontDeleteUselessPHIs*/ true); 1252 1253 // Walk from this loop up through its parents removing all of the dead blocks. 1254 for (Loop *ParentL = &L; ParentL; ParentL = ParentL->getParentLoop()) { 1255 for (auto *BB : DeadBlocks) 1256 ParentL->getBlocksSet().erase(BB); 1257 llvm::erase_if(ParentL->getBlocksVector(), 1258 [&](BasicBlock *BB) { return DeadBlocks.count(BB); }); 1259 } 1260 1261 // Now delete the dead child loops. This raw delete will clear them 1262 // recursively. 1263 llvm::erase_if(L.getSubLoopsVector(), [&](Loop *ChildL) { 1264 if (!DeadBlocks.count(ChildL->getHeader())) 1265 return false; 1266 1267 assert(llvm::all_of(ChildL->blocks(), 1268 [&](BasicBlock *ChildBB) { 1269 return DeadBlocks.count(ChildBB); 1270 }) && 1271 "If the child loop header is dead all blocks in the child loop must " 1272 "be dead as well!"); 1273 LI.destroy(ChildL); 1274 return true; 1275 }); 1276 1277 // Remove the mappings for the dead blocks. 1278 for (auto *BB : DeadBlocks) 1279 LI.changeLoopFor(BB, nullptr); 1280 1281 // Drop all the references from these blocks to others to handle cyclic 1282 // references as we start deleting the blocks themselves. 1283 for (auto *BB : DeadBlocks) 1284 BB->dropAllReferences(); 1285 1286 for (auto *BB : llvm::reverse(DeadBlocks)) { 1287 DT.eraseNode(BB); 1288 BB->eraseFromParent(); 1289 } 1290 } 1291 1292 /// Recompute the set of blocks in a loop after unswitching. 1293 /// 1294 /// This walks from the original headers predecessors to rebuild the loop. We 1295 /// take advantage of the fact that new blocks can't have been added, and so we 1296 /// filter by the original loop's blocks. This also handles potentially 1297 /// unreachable code that we don't want to explore but might be found examining 1298 /// the predecessors of the header. 1299 /// 1300 /// If the original loop is no longer a loop, this will return an empty set. If 1301 /// it remains a loop, all the blocks within it will be added to the set 1302 /// (including those blocks in inner loops). 1303 static SmallPtrSet<const BasicBlock *, 16> recomputeLoopBlockSet(Loop &L, 1304 LoopInfo &LI) { 1305 SmallPtrSet<const BasicBlock *, 16> LoopBlockSet; 1306 1307 auto *PH = L.getLoopPreheader(); 1308 auto *Header = L.getHeader(); 1309 1310 // A worklist to use while walking backwards from the header. 1311 SmallVector<BasicBlock *, 16> Worklist; 1312 1313 // First walk the predecessors of the header to find the backedges. This will 1314 // form the basis of our walk. 1315 for (auto *Pred : predecessors(Header)) { 1316 // Skip the preheader. 1317 if (Pred == PH) 1318 continue; 1319 1320 // Because the loop was in simplified form, the only non-loop predecessor 1321 // is the preheader. 1322 assert(L.contains(Pred) && "Found a predecessor of the loop header other " 1323 "than the preheader that is not part of the " 1324 "loop!"); 1325 1326 // Insert this block into the loop set and on the first visit and, if it 1327 // isn't the header we're currently walking, put it into the worklist to 1328 // recurse through. 1329 if (LoopBlockSet.insert(Pred).second && Pred != Header) 1330 Worklist.push_back(Pred); 1331 } 1332 1333 // If no backedges were found, we're done. 1334 if (LoopBlockSet.empty()) 1335 return LoopBlockSet; 1336 1337 // Add the loop header to the set. 1338 LoopBlockSet.insert(Header); 1339 1340 // We found backedges, recurse through them to identify the loop blocks. 1341 while (!Worklist.empty()) { 1342 BasicBlock *BB = Worklist.pop_back_val(); 1343 assert(LoopBlockSet.count(BB) && "Didn't put block into the loop set!"); 1344 1345 // Because we know the inner loop structure remains valid we can use the 1346 // loop structure to jump immediately across the entire nested loop. 1347 // Further, because it is in loop simplified form, we can directly jump 1348 // to its preheader afterward. 1349 if (Loop *InnerL = LI.getLoopFor(BB)) 1350 if (InnerL != &L) { 1351 assert(L.contains(InnerL) && 1352 "Should not reach a loop *outside* this loop!"); 1353 // The preheader is the only possible predecessor of the loop so 1354 // insert it into the set and check whether it was already handled. 1355 auto *InnerPH = InnerL->getLoopPreheader(); 1356 assert(L.contains(InnerPH) && "Cannot contain an inner loop block " 1357 "but not contain the inner loop " 1358 "preheader!"); 1359 if (!LoopBlockSet.insert(InnerPH).second) 1360 // The only way to reach the preheader is through the loop body 1361 // itself so if it has been visited the loop is already handled. 1362 continue; 1363 1364 // Insert all of the blocks (other than those already present) into 1365 // the loop set. The only block we expect to already be in the set is 1366 // the one we used to find this loop as we immediately handle the 1367 // others the first time we encounter the loop. 1368 for (auto *InnerBB : InnerL->blocks()) { 1369 if (InnerBB == BB) { 1370 assert(LoopBlockSet.count(InnerBB) && 1371 "Block should already be in the set!"); 1372 continue; 1373 } 1374 1375 bool Inserted = LoopBlockSet.insert(InnerBB).second; 1376 (void)Inserted; 1377 assert(Inserted && "Should only insert an inner loop once!"); 1378 } 1379 1380 // Add the preheader to the worklist so we will continue past the 1381 // loop body. 1382 Worklist.push_back(InnerPH); 1383 continue; 1384 } 1385 1386 // Insert any predecessors that were in the original loop into the new 1387 // set, and if the insert is successful, add them to the worklist. 1388 for (auto *Pred : predecessors(BB)) 1389 if (L.contains(Pred) && LoopBlockSet.insert(Pred).second) 1390 Worklist.push_back(Pred); 1391 } 1392 1393 // We've found all the blocks participating in the loop, return our completed 1394 // set. 1395 return LoopBlockSet; 1396 } 1397 1398 /// Rebuild a loop after unswitching removes some subset of blocks and edges. 1399 /// 1400 /// The removal may have removed some child loops entirely but cannot have 1401 /// disturbed any remaining child loops. However, they may need to be hoisted 1402 /// to the parent loop (or to be top-level loops). The original loop may be 1403 /// completely removed. 1404 /// 1405 /// The sibling loops resulting from this update are returned. If the original 1406 /// loop remains a valid loop, it will be the first entry in this list with all 1407 /// of the newly sibling loops following it. 1408 /// 1409 /// Returns true if the loop remains a loop after unswitching, and false if it 1410 /// is no longer a loop after unswitching (and should not continue to be 1411 /// referenced). 1412 static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef<BasicBlock *> ExitBlocks, 1413 LoopInfo &LI, 1414 SmallVectorImpl<Loop *> &HoistedLoops) { 1415 auto *PH = L.getLoopPreheader(); 1416 1417 // Compute the actual parent loop from the exit blocks. Because we may have 1418 // pruned some exits the loop may be different from the original parent. 1419 Loop *ParentL = nullptr; 1420 SmallVector<Loop *, 4> ExitLoops; 1421 SmallVector<BasicBlock *, 4> ExitsInLoops; 1422 ExitsInLoops.reserve(ExitBlocks.size()); 1423 for (auto *ExitBB : ExitBlocks) 1424 if (Loop *ExitL = LI.getLoopFor(ExitBB)) { 1425 ExitLoops.push_back(ExitL); 1426 ExitsInLoops.push_back(ExitBB); 1427 if (!ParentL || (ParentL != ExitL && ParentL->contains(ExitL))) 1428 ParentL = ExitL; 1429 } 1430 1431 // Recompute the blocks participating in this loop. This may be empty if it 1432 // is no longer a loop. 1433 auto LoopBlockSet = recomputeLoopBlockSet(L, LI); 1434 1435 // If we still have a loop, we need to re-set the loop's parent as the exit 1436 // block set changing may have moved it within the loop nest. Note that this 1437 // can only happen when this loop has a parent as it can only hoist the loop 1438 // *up* the nest. 1439 if (!LoopBlockSet.empty() && L.getParentLoop() != ParentL) { 1440 // Remove this loop's (original) blocks from all of the intervening loops. 1441 for (Loop *IL = L.getParentLoop(); IL != ParentL; 1442 IL = IL->getParentLoop()) { 1443 IL->getBlocksSet().erase(PH); 1444 for (auto *BB : L.blocks()) 1445 IL->getBlocksSet().erase(BB); 1446 llvm::erase_if(IL->getBlocksVector(), [&](BasicBlock *BB) { 1447 return BB == PH || L.contains(BB); 1448 }); 1449 } 1450 1451 LI.changeLoopFor(PH, ParentL); 1452 L.getParentLoop()->removeChildLoop(&L); 1453 if (ParentL) 1454 ParentL->addChildLoop(&L); 1455 else 1456 LI.addTopLevelLoop(&L); 1457 } 1458 1459 // Now we update all the blocks which are no longer within the loop. 1460 auto &Blocks = L.getBlocksVector(); 1461 auto BlocksSplitI = 1462 LoopBlockSet.empty() 1463 ? Blocks.begin() 1464 : std::stable_partition( 1465 Blocks.begin(), Blocks.end(), 1466 [&](BasicBlock *BB) { return LoopBlockSet.count(BB); }); 1467 1468 // Before we erase the list of unlooped blocks, build a set of them. 1469 SmallPtrSet<BasicBlock *, 16> UnloopedBlocks(BlocksSplitI, Blocks.end()); 1470 if (LoopBlockSet.empty()) 1471 UnloopedBlocks.insert(PH); 1472 1473 // Now erase these blocks from the loop. 1474 for (auto *BB : make_range(BlocksSplitI, Blocks.end())) 1475 L.getBlocksSet().erase(BB); 1476 Blocks.erase(BlocksSplitI, Blocks.end()); 1477 1478 // Sort the exits in ascending loop depth, we'll work backwards across these 1479 // to process them inside out. 1480 std::stable_sort(ExitsInLoops.begin(), ExitsInLoops.end(), 1481 [&](BasicBlock *LHS, BasicBlock *RHS) { 1482 return LI.getLoopDepth(LHS) < LI.getLoopDepth(RHS); 1483 }); 1484 1485 // We'll build up a set for each exit loop. 1486 SmallPtrSet<BasicBlock *, 16> NewExitLoopBlocks; 1487 Loop *PrevExitL = L.getParentLoop(); // The deepest possible exit loop. 1488 1489 auto RemoveUnloopedBlocksFromLoop = 1490 [](Loop &L, SmallPtrSetImpl<BasicBlock *> &UnloopedBlocks) { 1491 for (auto *BB : UnloopedBlocks) 1492 L.getBlocksSet().erase(BB); 1493 llvm::erase_if(L.getBlocksVector(), [&](BasicBlock *BB) { 1494 return UnloopedBlocks.count(BB); 1495 }); 1496 }; 1497 1498 SmallVector<BasicBlock *, 16> Worklist; 1499 while (!UnloopedBlocks.empty() && !ExitsInLoops.empty()) { 1500 assert(Worklist.empty() && "Didn't clear worklist!"); 1501 assert(NewExitLoopBlocks.empty() && "Didn't clear loop set!"); 1502 1503 // Grab the next exit block, in decreasing loop depth order. 1504 BasicBlock *ExitBB = ExitsInLoops.pop_back_val(); 1505 Loop &ExitL = *LI.getLoopFor(ExitBB); 1506 assert(ExitL.contains(&L) && "Exit loop must contain the inner loop!"); 1507 1508 // Erase all of the unlooped blocks from the loops between the previous 1509 // exit loop and this exit loop. This works because the ExitInLoops list is 1510 // sorted in increasing order of loop depth and thus we visit loops in 1511 // decreasing order of loop depth. 1512 for (; PrevExitL != &ExitL; PrevExitL = PrevExitL->getParentLoop()) 1513 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks); 1514 1515 // Walk the CFG back until we hit the cloned PH adding everything reachable 1516 // and in the unlooped set to this exit block's loop. 1517 Worklist.push_back(ExitBB); 1518 do { 1519 BasicBlock *BB = Worklist.pop_back_val(); 1520 // We can stop recursing at the cloned preheader (if we get there). 1521 if (BB == PH) 1522 continue; 1523 1524 for (BasicBlock *PredBB : predecessors(BB)) { 1525 // If this pred has already been moved to our set or is part of some 1526 // (inner) loop, no update needed. 1527 if (!UnloopedBlocks.erase(PredBB)) { 1528 assert((NewExitLoopBlocks.count(PredBB) || 1529 ExitL.contains(LI.getLoopFor(PredBB))) && 1530 "Predecessor not in a nested loop (or already visited)!"); 1531 continue; 1532 } 1533 1534 // We just insert into the loop set here. We'll add these blocks to the 1535 // exit loop after we build up the set in a deterministic order rather 1536 // than the predecessor-influenced visit order. 1537 bool Inserted = NewExitLoopBlocks.insert(PredBB).second; 1538 (void)Inserted; 1539 assert(Inserted && "Should only visit an unlooped block once!"); 1540 1541 // And recurse through to its predecessors. 1542 Worklist.push_back(PredBB); 1543 } 1544 } while (!Worklist.empty()); 1545 1546 // If blocks in this exit loop were directly part of the original loop (as 1547 // opposed to a child loop) update the map to point to this exit loop. This 1548 // just updates a map and so the fact that the order is unstable is fine. 1549 for (auto *BB : NewExitLoopBlocks) 1550 if (Loop *BBL = LI.getLoopFor(BB)) 1551 if (BBL == &L || !L.contains(BBL)) 1552 LI.changeLoopFor(BB, &ExitL); 1553 1554 // We will remove the remaining unlooped blocks from this loop in the next 1555 // iteration or below. 1556 NewExitLoopBlocks.clear(); 1557 } 1558 1559 // Any remaining unlooped blocks are no longer part of any loop unless they 1560 // are part of some child loop. 1561 for (; PrevExitL; PrevExitL = PrevExitL->getParentLoop()) 1562 RemoveUnloopedBlocksFromLoop(*PrevExitL, UnloopedBlocks); 1563 for (auto *BB : UnloopedBlocks) 1564 if (Loop *BBL = LI.getLoopFor(BB)) 1565 if (BBL == &L || !L.contains(BBL)) 1566 LI.changeLoopFor(BB, nullptr); 1567 1568 // Sink all the child loops whose headers are no longer in the loop set to 1569 // the parent (or to be top level loops). We reach into the loop and directly 1570 // update its subloop vector to make this batch update efficient. 1571 auto &SubLoops = L.getSubLoopsVector(); 1572 auto SubLoopsSplitI = 1573 LoopBlockSet.empty() 1574 ? SubLoops.begin() 1575 : std::stable_partition( 1576 SubLoops.begin(), SubLoops.end(), [&](Loop *SubL) { 1577 return LoopBlockSet.count(SubL->getHeader()); 1578 }); 1579 for (auto *HoistedL : make_range(SubLoopsSplitI, SubLoops.end())) { 1580 HoistedLoops.push_back(HoistedL); 1581 HoistedL->setParentLoop(nullptr); 1582 1583 // To compute the new parent of this hoisted loop we look at where we 1584 // placed the preheader above. We can't lookup the header itself because we 1585 // retained the mapping from the header to the hoisted loop. But the 1586 // preheader and header should have the exact same new parent computed 1587 // based on the set of exit blocks from the original loop as the preheader 1588 // is a predecessor of the header and so reached in the reverse walk. And 1589 // because the loops were all in simplified form the preheader of the 1590 // hoisted loop can't be part of some *other* loop. 1591 if (auto *NewParentL = LI.getLoopFor(HoistedL->getLoopPreheader())) 1592 NewParentL->addChildLoop(HoistedL); 1593 else 1594 LI.addTopLevelLoop(HoistedL); 1595 } 1596 SubLoops.erase(SubLoopsSplitI, SubLoops.end()); 1597 1598 // Actually delete the loop if nothing remained within it. 1599 if (Blocks.empty()) { 1600 assert(SubLoops.empty() && 1601 "Failed to remove all subloops from the original loop!"); 1602 if (Loop *ParentL = L.getParentLoop()) 1603 ParentL->removeChildLoop(llvm::find(*ParentL, &L)); 1604 else 1605 LI.removeLoop(llvm::find(LI, &L)); 1606 LI.destroy(&L); 1607 return false; 1608 } 1609 1610 return true; 1611 } 1612 1613 /// Helper to visit a dominator subtree, invoking a callable on each node. 1614 /// 1615 /// Returning false at any point will stop walking past that node of the tree. 1616 template <typename CallableT> 1617 void visitDomSubTree(DominatorTree &DT, BasicBlock *BB, CallableT Callable) { 1618 SmallVector<DomTreeNode *, 4> DomWorklist; 1619 DomWorklist.push_back(DT[BB]); 1620 #ifndef NDEBUG 1621 SmallPtrSet<DomTreeNode *, 4> Visited; 1622 Visited.insert(DT[BB]); 1623 #endif 1624 do { 1625 DomTreeNode *N = DomWorklist.pop_back_val(); 1626 1627 // Visit this node. 1628 if (!Callable(N->getBlock())) 1629 continue; 1630 1631 // Accumulate the child nodes. 1632 for (DomTreeNode *ChildN : *N) { 1633 assert(Visited.insert(ChildN).second && 1634 "Cannot visit a node twice when walking a tree!"); 1635 DomWorklist.push_back(ChildN); 1636 } 1637 } while (!DomWorklist.empty()); 1638 } 1639 1640 /// Take an invariant branch that has been determined to be safe and worthwhile 1641 /// to unswitch despite being non-trivial to do so and perform the unswitch. 1642 /// 1643 /// This directly updates the CFG to hoist the predicate out of the loop, and 1644 /// clone the necessary parts of the loop to maintain behavior. 1645 /// 1646 /// It also updates both dominator tree and loopinfo based on the unswitching. 1647 /// 1648 /// Once unswitching has been performed it runs the provided callback to report 1649 /// the new loops and no-longer valid loops to the caller. 1650 static bool unswitchInvariantBranch( 1651 Loop &L, BranchInst &BI, DominatorTree &DT, LoopInfo &LI, 1652 AssumptionCache &AC, 1653 function_ref<void(bool, ArrayRef<Loop *>)> NonTrivialUnswitchCB) { 1654 assert(BI.isConditional() && "Can only unswitch a conditional branch!"); 1655 assert(L.isLoopInvariant(BI.getCondition()) && 1656 "Can only unswitch an invariant branch condition!"); 1657 1658 // Constant and BBs tracking the cloned and continuing successor. 1659 const int ClonedSucc = 0; 1660 auto *ParentBB = BI.getParent(); 1661 auto *UnswitchedSuccBB = BI.getSuccessor(ClonedSucc); 1662 auto *ContinueSuccBB = BI.getSuccessor(1 - ClonedSucc); 1663 1664 assert(UnswitchedSuccBB != ContinueSuccBB && 1665 "Should not unswitch a branch that always goes to the same place!"); 1666 1667 // The branch should be in this exact loop. Any inner loop's invariant branch 1668 // should be handled by unswitching that inner loop. The caller of this 1669 // routine should filter out any candidates that remain (but were skipped for 1670 // whatever reason). 1671 assert(LI.getLoopFor(ParentBB) == &L && "Branch in an inner loop!"); 1672 1673 SmallVector<BasicBlock *, 4> ExitBlocks; 1674 L.getUniqueExitBlocks(ExitBlocks); 1675 1676 // We cannot unswitch if exit blocks contain a cleanuppad instruction as we 1677 // don't know how to split those exit blocks. 1678 // FIXME: We should teach SplitBlock to handle this and remove this 1679 // restriction. 1680 for (auto *ExitBB : ExitBlocks) 1681 if (isa<CleanupPadInst>(ExitBB->getFirstNonPHI())) 1682 return false; 1683 1684 SmallPtrSet<BasicBlock *, 4> ExitBlockSet(ExitBlocks.begin(), 1685 ExitBlocks.end()); 1686 1687 // Compute the parent loop now before we start hacking on things. 1688 Loop *ParentL = L.getParentLoop(); 1689 1690 // Compute the outer-most loop containing one of our exit blocks. This is the 1691 // furthest up our loopnest which can be mutated, which we will use below to 1692 // update things. 1693 Loop *OuterExitL = &L; 1694 for (auto *ExitBB : ExitBlocks) { 1695 Loop *NewOuterExitL = LI.getLoopFor(ExitBB); 1696 if (!NewOuterExitL) { 1697 // We exited the entire nest with this block, so we're done. 1698 OuterExitL = nullptr; 1699 break; 1700 } 1701 if (NewOuterExitL != OuterExitL && NewOuterExitL->contains(OuterExitL)) 1702 OuterExitL = NewOuterExitL; 1703 } 1704 1705 // If the edge we *aren't* cloning in the unswitch (the continuing edge) 1706 // dominates its target, we can skip cloning the dominated region of the loop 1707 // and its exits. We compute this as a set of nodes to be skipped. 1708 SmallPtrSet<BasicBlock *, 4> SkippedLoopAndExitBlocks; 1709 if (ContinueSuccBB->getUniquePredecessor() || 1710 llvm::all_of(predecessors(ContinueSuccBB), [&](BasicBlock *PredBB) { 1711 return PredBB == ParentBB || DT.dominates(ContinueSuccBB, PredBB); 1712 })) { 1713 visitDomSubTree(DT, ContinueSuccBB, [&](BasicBlock *BB) { 1714 SkippedLoopAndExitBlocks.insert(BB); 1715 return true; 1716 }); 1717 } 1718 // Similarly, if the edge we *are* cloning in the unswitch (the unswitched 1719 // edge) dominates its target, we will end up with dead nodes in the original 1720 // loop and its exits that will need to be deleted. Here, we just retain that 1721 // the property holds and will compute the deleted set later. 1722 bool DeleteUnswitchedSucc = 1723 UnswitchedSuccBB->getUniquePredecessor() || 1724 llvm::all_of(predecessors(UnswitchedSuccBB), [&](BasicBlock *PredBB) { 1725 return PredBB == ParentBB || DT.dominates(UnswitchedSuccBB, PredBB); 1726 }); 1727 1728 // Split the preheader, so that we know that there is a safe place to insert 1729 // the conditional branch. We will change the preheader to have a conditional 1730 // branch on LoopCond. The original preheader will become the split point 1731 // between the unswitched versions, and we will have a new preheader for the 1732 // original loop. 1733 BasicBlock *SplitBB = L.getLoopPreheader(); 1734 BasicBlock *LoopPH = SplitEdge(SplitBB, L.getHeader(), &DT, &LI); 1735 1736 // Keep a mapping for the cloned values. 1737 ValueToValueMapTy VMap; 1738 1739 // Build the cloned blocks from the loop. 1740 auto *ClonedPH = buildClonedLoopBlocks( 1741 L, LoopPH, SplitBB, ExitBlocks, ParentBB, UnswitchedSuccBB, 1742 ContinueSuccBB, SkippedLoopAndExitBlocks, VMap, AC, DT, LI); 1743 1744 // Build the cloned loop structure itself. This may be substantially 1745 // different from the original structure due to the simplified CFG. This also 1746 // handles inserting all the cloned blocks into the correct loops. 1747 SmallVector<Loop *, 4> NonChildClonedLoops; 1748 Loop *ClonedL = 1749 buildClonedLoops(L, ExitBlocks, VMap, LI, NonChildClonedLoops); 1750 1751 // Remove the parent as a predecessor of the unswitched successor. 1752 UnswitchedSuccBB->removePredecessor(ParentBB, /*DontDeleteUselessPHIs*/ true); 1753 1754 // Now splice the branch from the original loop and use it to select between 1755 // the two loops. 1756 SplitBB->getTerminator()->eraseFromParent(); 1757 SplitBB->getInstList().splice(SplitBB->end(), ParentBB->getInstList(), BI); 1758 BI.setSuccessor(ClonedSucc, ClonedPH); 1759 BI.setSuccessor(1 - ClonedSucc, LoopPH); 1760 1761 // Create a new unconditional branch to the continuing block (as opposed to 1762 // the one cloned). 1763 BranchInst::Create(ContinueSuccBB, ParentBB); 1764 1765 // Delete anything that was made dead in the original loop due to 1766 // unswitching. 1767 if (DeleteUnswitchedSucc) 1768 deleteDeadBlocksFromLoop(L, UnswitchedSuccBB, ExitBlocks, DT, LI); 1769 1770 SmallVector<Loop *, 4> HoistedLoops; 1771 bool IsStillLoop = rebuildLoopAfterUnswitch(L, ExitBlocks, LI, HoistedLoops); 1772 1773 // This will have completely invalidated the dominator tree. We can't easily 1774 // bound how much is invalid because in some cases we will refine the 1775 // predecessor set of exit blocks of the loop which can move large unrelated 1776 // regions of code into a new subtree. 1777 // 1778 // FIXME: Eventually, we should use an incremental update utility that 1779 // leverages the existing information in the dominator tree (and potentially 1780 // the nature of the change) to more efficiently update things. 1781 DT.recalculate(*SplitBB->getParent()); 1782 1783 // We can change which blocks are exit blocks of all the cloned sibling 1784 // loops, the current loop, and any parent loops which shared exit blocks 1785 // with the current loop. As a consequence, we need to re-form LCSSA for 1786 // them. But we shouldn't need to re-form LCSSA for any child loops. 1787 // FIXME: This could be made more efficient by tracking which exit blocks are 1788 // new, and focusing on them, but that isn't likely to be necessary. 1789 // 1790 // In order to reasonably rebuild LCSSA we need to walk inside-out across the 1791 // loop nest and update every loop that could have had its exits changed. We 1792 // also need to cover any intervening loops. We add all of these loops to 1793 // a list and sort them by loop depth to achieve this without updating 1794 // unnecessary loops. 1795 auto UpdateLCSSA = [&](Loop &UpdateL) { 1796 #ifndef NDEBUG 1797 for (Loop *ChildL : UpdateL) 1798 assert(ChildL->isRecursivelyLCSSAForm(DT, LI) && 1799 "Perturbed a child loop's LCSSA form!"); 1800 #endif 1801 formLCSSA(UpdateL, DT, &LI, nullptr); 1802 }; 1803 1804 // For non-child cloned loops and hoisted loops, we just need to update LCSSA 1805 // and we can do it in any order as they don't nest relative to each other. 1806 for (Loop *UpdatedL : llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops)) 1807 UpdateLCSSA(*UpdatedL); 1808 1809 // If the original loop had exit blocks, walk up through the outer most loop 1810 // of those exit blocks to update LCSSA and form updated dedicated exits. 1811 if (OuterExitL != &L) { 1812 SmallVector<Loop *, 4> OuterLoops; 1813 // We start with the cloned loop and the current loop if they are loops and 1814 // move toward OuterExitL. Also, if either the cloned loop or the current 1815 // loop have become top level loops we need to walk all the way out. 1816 if (ClonedL) { 1817 OuterLoops.push_back(ClonedL); 1818 if (!ClonedL->getParentLoop()) 1819 OuterExitL = nullptr; 1820 } 1821 if (IsStillLoop) { 1822 OuterLoops.push_back(&L); 1823 if (!L.getParentLoop()) 1824 OuterExitL = nullptr; 1825 } 1826 // Grab all of the enclosing loops now. 1827 for (Loop *OuterL = ParentL; OuterL != OuterExitL; 1828 OuterL = OuterL->getParentLoop()) 1829 OuterLoops.push_back(OuterL); 1830 1831 // Finally, update our list of outer loops. This is nicely ordered to work 1832 // inside-out. 1833 for (Loop *OuterL : OuterLoops) { 1834 // First build LCSSA for this loop so that we can preserve it when 1835 // forming dedicated exits. We don't want to perturb some other loop's 1836 // LCSSA while doing that CFG edit. 1837 UpdateLCSSA(*OuterL); 1838 1839 // For loops reached by this loop's original exit blocks we may 1840 // introduced new, non-dedicated exits. At least try to re-form dedicated 1841 // exits for these loops. This may fail if they couldn't have dedicated 1842 // exits to start with. 1843 formDedicatedExitBlocks(OuterL, &DT, &LI, /*PreserveLCSSA*/ true); 1844 } 1845 } 1846 1847 #ifndef NDEBUG 1848 // Verify the entire loop structure to catch any incorrect updates before we 1849 // progress in the pass pipeline. 1850 LI.verify(DT); 1851 #endif 1852 1853 // Now that we've unswitched something, make callbacks to report the changes. 1854 // For that we need to merge together the updated loops and the cloned loops 1855 // and check whether the original loop survived. 1856 SmallVector<Loop *, 4> SibLoops; 1857 for (Loop *UpdatedL : llvm::concat<Loop *>(NonChildClonedLoops, HoistedLoops)) 1858 if (UpdatedL->getParentLoop() == ParentL) 1859 SibLoops.push_back(UpdatedL); 1860 NonTrivialUnswitchCB(IsStillLoop, SibLoops); 1861 1862 ++NumBranches; 1863 return true; 1864 } 1865 1866 /// Recursively compute the cost of a dominator subtree based on the per-block 1867 /// cost map provided. 1868 /// 1869 /// The recursive computation is memozied into the provided DT-indexed cost map 1870 /// to allow querying it for most nodes in the domtree without it becoming 1871 /// quadratic. 1872 static int 1873 computeDomSubtreeCost(DomTreeNode &N, 1874 const SmallDenseMap<BasicBlock *, int, 4> &BBCostMap, 1875 SmallDenseMap<DomTreeNode *, int, 4> &DTCostMap) { 1876 // Don't accumulate cost (or recurse through) blocks not in our block cost 1877 // map and thus not part of the duplication cost being considered. 1878 auto BBCostIt = BBCostMap.find(N.getBlock()); 1879 if (BBCostIt == BBCostMap.end()) 1880 return 0; 1881 1882 // Lookup this node to see if we already computed its cost. 1883 auto DTCostIt = DTCostMap.find(&N); 1884 if (DTCostIt != DTCostMap.end()) 1885 return DTCostIt->second; 1886 1887 // If not, we have to compute it. We can't use insert above and update 1888 // because computing the cost may insert more things into the map. 1889 int Cost = std::accumulate( 1890 N.begin(), N.end(), BBCostIt->second, [&](int Sum, DomTreeNode *ChildN) { 1891 return Sum + computeDomSubtreeCost(*ChildN, BBCostMap, DTCostMap); 1892 }); 1893 bool Inserted = DTCostMap.insert({&N, Cost}).second; 1894 (void)Inserted; 1895 assert(Inserted && "Should not insert a node while visiting children!"); 1896 return Cost; 1897 } 1898 1899 /// Unswitch control flow predicated on loop invariant conditions. 1900 /// 1901 /// This first hoists all branches or switches which are trivial (IE, do not 1902 /// require duplicating any part of the loop) out of the loop body. It then 1903 /// looks at other loop invariant control flows and tries to unswitch those as 1904 /// well by cloning the loop if the result is small enough. 1905 static bool 1906 unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC, 1907 TargetTransformInfo &TTI, bool NonTrivial, 1908 function_ref<void(bool, ArrayRef<Loop *>)> NonTrivialUnswitchCB) { 1909 assert(L.isRecursivelyLCSSAForm(DT, LI) && 1910 "Loops must be in LCSSA form before unswitching."); 1911 bool Changed = false; 1912 1913 // Must be in loop simplified form: we need a preheader and dedicated exits. 1914 if (!L.isLoopSimplifyForm()) 1915 return false; 1916 1917 // Try trivial unswitch first before loop over other basic blocks in the loop. 1918 Changed |= unswitchAllTrivialConditions(L, DT, LI); 1919 1920 // If we're not doing non-trivial unswitching, we're done. We both accept 1921 // a parameter but also check a local flag that can be used for testing 1922 // a debugging. 1923 if (!NonTrivial && !EnableNonTrivialUnswitch) 1924 return Changed; 1925 1926 // Collect all remaining invariant branch conditions within this loop (as 1927 // opposed to an inner loop which would be handled when visiting that inner 1928 // loop). 1929 SmallVector<TerminatorInst *, 4> UnswitchCandidates; 1930 for (auto *BB : L.blocks()) 1931 if (LI.getLoopFor(BB) == &L) 1932 if (auto *BI = dyn_cast<BranchInst>(BB->getTerminator())) 1933 if (BI->isConditional() && L.isLoopInvariant(BI->getCondition()) && 1934 BI->getSuccessor(0) != BI->getSuccessor(1)) 1935 UnswitchCandidates.push_back(BI); 1936 1937 // If we didn't find any candidates, we're done. 1938 if (UnswitchCandidates.empty()) 1939 return Changed; 1940 1941 DEBUG(dbgs() << "Considering " << UnswitchCandidates.size() 1942 << " non-trivial loop invariant conditions for unswitching.\n"); 1943 1944 // Given that unswitching these terminators will require duplicating parts of 1945 // the loop, so we need to be able to model that cost. Compute the ephemeral 1946 // values and set up a data structure to hold per-BB costs. We cache each 1947 // block's cost so that we don't recompute this when considering different 1948 // subsets of the loop for duplication during unswitching. 1949 SmallPtrSet<const Value *, 4> EphValues; 1950 CodeMetrics::collectEphemeralValues(&L, &AC, EphValues); 1951 SmallDenseMap<BasicBlock *, int, 4> BBCostMap; 1952 1953 // Compute the cost of each block, as well as the total loop cost. Also, bail 1954 // out if we see instructions which are incompatible with loop unswitching 1955 // (convergent, noduplicate, or cross-basic-block tokens). 1956 // FIXME: We might be able to safely handle some of these in non-duplicated 1957 // regions. 1958 int LoopCost = 0; 1959 for (auto *BB : L.blocks()) { 1960 int Cost = 0; 1961 for (auto &I : *BB) { 1962 if (EphValues.count(&I)) 1963 continue; 1964 1965 if (I.getType()->isTokenTy() && I.isUsedOutsideOfBlock(BB)) 1966 return Changed; 1967 if (auto CS = CallSite(&I)) 1968 if (CS.isConvergent() || CS.cannotDuplicate()) 1969 return Changed; 1970 1971 Cost += TTI.getUserCost(&I); 1972 } 1973 assert(Cost >= 0 && "Must not have negative costs!"); 1974 LoopCost += Cost; 1975 assert(LoopCost >= 0 && "Must not have negative loop costs!"); 1976 BBCostMap[BB] = Cost; 1977 } 1978 DEBUG(dbgs() << " Total loop cost: " << LoopCost << "\n"); 1979 1980 // Now we find the best candidate by searching for the one with the following 1981 // properties in order: 1982 // 1983 // 1) An unswitching cost below the threshold 1984 // 2) The smallest number of duplicated unswitch candidates (to avoid 1985 // creating redundant subsequent unswitching) 1986 // 3) The smallest cost after unswitching. 1987 // 1988 // We prioritize reducing fanout of unswitch candidates provided the cost 1989 // remains below the threshold because this has a multiplicative effect. 1990 // 1991 // This requires memoizing each dominator subtree to avoid redundant work. 1992 // 1993 // FIXME: Need to actually do the number of candidates part above. 1994 SmallDenseMap<DomTreeNode *, int, 4> DTCostMap; 1995 // Given a terminator which might be unswitched, computes the non-duplicated 1996 // cost for that terminator. 1997 auto ComputeUnswitchedCost = [&](TerminatorInst *TI) { 1998 BasicBlock &BB = *TI->getParent(); 1999 SmallPtrSet<BasicBlock *, 4> Visited; 2000 2001 int Cost = LoopCost; 2002 for (BasicBlock *SuccBB : successors(&BB)) { 2003 // Don't count successors more than once. 2004 if (!Visited.insert(SuccBB).second) 2005 continue; 2006 2007 // This successor's domtree will not need to be duplicated after 2008 // unswitching if the edge to the successor dominates it (and thus the 2009 // entire tree). This essentially means there is no other path into this 2010 // subtree and so it will end up live in only one clone of the loop. 2011 if (SuccBB->getUniquePredecessor() || 2012 llvm::all_of(predecessors(SuccBB), [&](BasicBlock *PredBB) { 2013 return PredBB == &BB || DT.dominates(SuccBB, PredBB); 2014 })) { 2015 Cost -= computeDomSubtreeCost(*DT[SuccBB], BBCostMap, DTCostMap); 2016 assert(Cost >= 0 && 2017 "Non-duplicated cost should never exceed total loop cost!"); 2018 } 2019 } 2020 2021 // Now scale the cost by the number of unique successors minus one. We 2022 // subtract one because there is already at least one copy of the entire 2023 // loop. This is computing the new cost of unswitching a condition. 2024 assert(Visited.size() > 1 && 2025 "Cannot unswitch a condition without multiple distinct successors!"); 2026 return Cost * (Visited.size() - 1); 2027 }; 2028 TerminatorInst *BestUnswitchTI = nullptr; 2029 int BestUnswitchCost; 2030 for (TerminatorInst *CandidateTI : UnswitchCandidates) { 2031 int CandidateCost = ComputeUnswitchedCost(CandidateTI); 2032 DEBUG(dbgs() << " Computed cost of " << CandidateCost 2033 << " for unswitch candidate: " << *CandidateTI << "\n"); 2034 if (!BestUnswitchTI || CandidateCost < BestUnswitchCost) { 2035 BestUnswitchTI = CandidateTI; 2036 BestUnswitchCost = CandidateCost; 2037 } 2038 } 2039 2040 if (BestUnswitchCost < UnswitchThreshold) { 2041 DEBUG(dbgs() << " Trying to unswitch non-trivial (cost = " 2042 << BestUnswitchCost << ") branch: " << *BestUnswitchTI 2043 << "\n"); 2044 Changed |= unswitchInvariantBranch(L, cast<BranchInst>(*BestUnswitchTI), DT, 2045 LI, AC, NonTrivialUnswitchCB); 2046 } else { 2047 DEBUG(dbgs() << "Cannot unswitch, lowest cost found: " << BestUnswitchCost 2048 << "\n"); 2049 } 2050 2051 return Changed; 2052 } 2053 2054 PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM, 2055 LoopStandardAnalysisResults &AR, 2056 LPMUpdater &U) { 2057 Function &F = *L.getHeader()->getParent(); 2058 (void)F; 2059 2060 DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L << "\n"); 2061 2062 // Save the current loop name in a variable so that we can report it even 2063 // after it has been deleted. 2064 std::string LoopName = L.getName(); 2065 2066 auto NonTrivialUnswitchCB = [&L, &U, &LoopName](bool CurrentLoopValid, 2067 ArrayRef<Loop *> NewLoops) { 2068 // If we did a non-trivial unswitch, we have added new (cloned) loops. 2069 U.addSiblingLoops(NewLoops); 2070 2071 // If the current loop remains valid, we should revisit it to catch any 2072 // other unswitch opportunities. Otherwise, we need to mark it as deleted. 2073 if (CurrentLoopValid) 2074 U.revisitCurrentLoop(); 2075 else 2076 U.markLoopAsDeleted(L, LoopName); 2077 }; 2078 2079 if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC, AR.TTI, NonTrivial, 2080 NonTrivialUnswitchCB)) 2081 return PreservedAnalyses::all(); 2082 2083 // Historically this pass has had issues with the dominator tree so verify it 2084 // in asserts builds. 2085 assert(AR.DT.verify(DominatorTree::VerificationLevel::Fast)); 2086 return getLoopPassPreservedAnalyses(); 2087 } 2088 2089 namespace { 2090 2091 class SimpleLoopUnswitchLegacyPass : public LoopPass { 2092 bool NonTrivial; 2093 2094 public: 2095 static char ID; // Pass ID, replacement for typeid 2096 2097 explicit SimpleLoopUnswitchLegacyPass(bool NonTrivial = false) 2098 : LoopPass(ID), NonTrivial(NonTrivial) { 2099 initializeSimpleLoopUnswitchLegacyPassPass( 2100 *PassRegistry::getPassRegistry()); 2101 } 2102 2103 bool runOnLoop(Loop *L, LPPassManager &LPM) override; 2104 2105 void getAnalysisUsage(AnalysisUsage &AU) const override { 2106 AU.addRequired<AssumptionCacheTracker>(); 2107 AU.addRequired<TargetTransformInfoWrapperPass>(); 2108 getLoopAnalysisUsage(AU); 2109 } 2110 }; 2111 2112 } // end anonymous namespace 2113 2114 bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) { 2115 if (skipLoop(L)) 2116 return false; 2117 2118 Function &F = *L->getHeader()->getParent(); 2119 2120 DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L << "\n"); 2121 2122 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 2123 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 2124 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 2125 auto &TTI = getAnalysis<TargetTransformInfoWrapperPass>().getTTI(F); 2126 2127 auto NonTrivialUnswitchCB = [&L, &LPM](bool CurrentLoopValid, 2128 ArrayRef<Loop *> NewLoops) { 2129 // If we did a non-trivial unswitch, we have added new (cloned) loops. 2130 for (auto *NewL : NewLoops) 2131 LPM.addLoop(*NewL); 2132 2133 // If the current loop remains valid, re-add it to the queue. This is 2134 // a little wasteful as we'll finish processing the current loop as well, 2135 // but it is the best we can do in the old PM. 2136 if (CurrentLoopValid) 2137 LPM.addLoop(*L); 2138 else 2139 LPM.markLoopAsDeleted(*L); 2140 }; 2141 2142 bool Changed = 2143 unswitchLoop(*L, DT, LI, AC, TTI, NonTrivial, NonTrivialUnswitchCB); 2144 2145 // If anything was unswitched, also clear any cached information about this 2146 // loop. 2147 LPM.deleteSimpleAnalysisLoop(L); 2148 2149 // Historically this pass has had issues with the dominator tree so verify it 2150 // in asserts builds. 2151 assert(DT.verify(DominatorTree::VerificationLevel::Fast)); 2152 2153 return Changed; 2154 } 2155 2156 char SimpleLoopUnswitchLegacyPass::ID = 0; 2157 INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", 2158 "Simple unswitch loops", false, false) 2159 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 2160 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 2161 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 2162 INITIALIZE_PASS_DEPENDENCY(LoopPass) 2163 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 2164 INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", 2165 "Simple unswitch loops", false, false) 2166 2167 Pass *llvm::createSimpleLoopUnswitchLegacyPass(bool NonTrivial) { 2168 return new SimpleLoopUnswitchLegacyPass(NonTrivial); 2169 } 2170