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/STLExtras.h" 12 #include "llvm/ADT/SmallPtrSet.h" 13 #include "llvm/ADT/Statistic.h" 14 #include "llvm/Analysis/AssumptionCache.h" 15 #include "llvm/Analysis/LoopInfo.h" 16 #include "llvm/Analysis/LoopPass.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/Dominators.h" 19 #include "llvm/IR/Function.h" 20 #include "llvm/IR/Instructions.h" 21 #include "llvm/Support/CommandLine.h" 22 #include "llvm/Support/Debug.h" 23 #include "llvm/Support/raw_ostream.h" 24 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 25 #include "llvm/Transforms/Utils/Cloning.h" 26 #include "llvm/Transforms/Utils/Local.h" 27 #include "llvm/Transforms/Scalar/LoopPassManager.h" 28 #include "llvm/Transforms/Utils/LoopUtils.h" 29 30 #define DEBUG_TYPE "simple-loop-unswitch" 31 32 using namespace llvm; 33 34 STATISTIC(NumBranches, "Number of branches unswitched"); 35 STATISTIC(NumSwitches, "Number of switches unswitched"); 36 STATISTIC(NumTrivial, "Number of unswitches that are trivial"); 37 38 static void replaceLoopUsesWithConstant(Loop &L, Value &LIC, 39 Constant &Replacement) { 40 assert(!isa<Constant>(LIC) && "Why are we unswitching on a constant?"); 41 42 // Replace uses of LIC in the loop with the given constant. 43 for (auto UI = LIC.use_begin(), UE = LIC.use_end(); UI != UE;) { 44 // Grab the use and walk past it so we can clobber it in the use list. 45 Use *U = &*UI++; 46 Instruction *UserI = dyn_cast<Instruction>(U->getUser()); 47 if (!UserI || !L.contains(UserI)) 48 continue; 49 50 // Replace this use within the loop body. 51 *U = &Replacement; 52 } 53 } 54 55 /// Update the dominator tree after removing one exiting predecessor of a loop 56 /// exit block. 57 static void updateLoopExitIDom(BasicBlock *LoopExitBB, Loop &L, 58 DominatorTree &DT) { 59 assert(pred_begin(LoopExitBB) != pred_end(LoopExitBB) && 60 "Cannot have empty predecessors of the loop exit block if we split " 61 "off a block to unswitch!"); 62 63 BasicBlock *IDom = *pred_begin(LoopExitBB); 64 // Walk all of the other predecessors finding the nearest common dominator 65 // until all predecessors are covered or we reach the loop header. The loop 66 // header necessarily dominates all loop exit blocks in loop simplified form 67 // so we can early-exit the moment we hit that block. 68 for (auto PI = std::next(pred_begin(LoopExitBB)), PE = pred_end(LoopExitBB); 69 PI != PE && IDom != L.getHeader(); ++PI) 70 IDom = DT.findNearestCommonDominator(IDom, *PI); 71 72 DT.changeImmediateDominator(LoopExitBB, IDom); 73 } 74 75 /// Update the dominator tree after unswitching a particular former exit block. 76 /// 77 /// This handles the full update of the dominator tree after hoisting a block 78 /// that previously was an exit block (or split off of an exit block) up to be 79 /// reached from the new immediate dominator of the preheader. 80 /// 81 /// The common case is simple -- we just move the unswitched block to have an 82 /// immediate dominator of the old preheader. But in complex cases, there may 83 /// be other blocks reachable from the unswitched block that are immediately 84 /// dominated by some node between the unswitched one and the old preheader. 85 /// All of these also need to be hoisted in the dominator tree. We also want to 86 /// minimize queries to the dominator tree because each step of this 87 /// invalidates any DFS numbers that would make queries fast. 88 static void updateDTAfterUnswitch(BasicBlock *UnswitchedBB, BasicBlock *OldPH, 89 DominatorTree &DT) { 90 DomTreeNode *OldPHNode = DT[OldPH]; 91 DomTreeNode *UnswitchedNode = DT[UnswitchedBB]; 92 // If the dominator tree has already been updated for this unswitched node, 93 // we're done. This makes it easier to use this routine if there are multiple 94 // paths to the same unswitched destination. 95 if (UnswitchedNode->getIDom() == OldPHNode) 96 return; 97 98 // First collect the domtree nodes that we are hoisting over. These are the 99 // set of nodes which may have children that need to be hoisted as well. 100 SmallPtrSet<DomTreeNode *, 4> DomChain; 101 for (auto *IDom = UnswitchedNode->getIDom(); IDom != OldPHNode; 102 IDom = IDom->getIDom()) 103 DomChain.insert(IDom); 104 105 // The unswitched block ends up immediately dominated by the old preheader -- 106 // regardless of whether it is the loop exit block or split off of the loop 107 // exit block. 108 DT.changeImmediateDominator(UnswitchedNode, OldPHNode); 109 110 // Blocks reachable from the unswitched block may need to change their IDom 111 // as well. 112 SmallSetVector<BasicBlock *, 4> Worklist; 113 for (auto *SuccBB : successors(UnswitchedBB)) 114 Worklist.insert(SuccBB); 115 116 // Walk the worklist. We grow the list in the loop and so must recompute size. 117 for (int i = 0; i < (int)Worklist.size(); ++i) { 118 auto *BB = Worklist[i]; 119 120 DomTreeNode *Node = DT[BB]; 121 assert(!DomChain.count(Node) && 122 "Cannot be dominated by a block you can reach!"); 123 // If this block doesn't have an immediate dominator somewhere in the chain 124 // we hoisted over, then its position in the domtree hasn't changed. Either 125 // it is above the region hoisted and still valid, or it is below the 126 // hoisted block and so was trivially updated. This also applies to 127 // everything reachable from this block so we're completely done with the 128 // it. 129 if (!DomChain.count(Node->getIDom())) 130 continue; 131 132 // We need to change the IDom for this node but also walk its successors 133 // which could have similar dominance position. 134 DT.changeImmediateDominator(Node, OldPHNode); 135 for (auto *SuccBB : successors(BB)) 136 Worklist.insert(SuccBB); 137 } 138 } 139 140 /// Unswitch a trivial branch if the condition is loop invariant. 141 /// 142 /// This routine should only be called when loop code leading to the branch has 143 /// been validated as trivial (no side effects). This routine checks if the 144 /// condition is invariant and one of the successors is a loop exit. This 145 /// allows us to unswitch without duplicating the loop, making it trivial. 146 /// 147 /// If this routine fails to unswitch the branch it returns false. 148 /// 149 /// If the branch can be unswitched, this routine splits the preheader and 150 /// hoists the branch above that split. Preserves loop simplified form 151 /// (splitting the exit block as necessary). It simplifies the branch within 152 /// the loop to an unconditional branch but doesn't remove it entirely. Further 153 /// cleanup can be done with some simplify-cfg like pass. 154 static bool unswitchTrivialBranch(Loop &L, BranchInst &BI, DominatorTree &DT, 155 LoopInfo &LI) { 156 assert(BI.isConditional() && "Can only unswitch a conditional branch!"); 157 DEBUG(dbgs() << " Trying to unswitch branch: " << BI << "\n"); 158 159 Value *LoopCond = BI.getCondition(); 160 161 // Need a trivial loop condition to unswitch. 162 if (!L.isLoopInvariant(LoopCond)) 163 return false; 164 165 // FIXME: We should compute this once at the start and update it! 166 SmallVector<BasicBlock *, 16> ExitBlocks; 167 L.getExitBlocks(ExitBlocks); 168 SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(), 169 ExitBlocks.end()); 170 171 // Check to see if a successor of the branch is guaranteed to 172 // exit through a unique exit block without having any 173 // side-effects. If so, determine the value of Cond that causes 174 // it to do this. 175 ConstantInt *CondVal = ConstantInt::getTrue(BI.getContext()); 176 ConstantInt *Replacement = ConstantInt::getFalse(BI.getContext()); 177 int LoopExitSuccIdx = 0; 178 auto *LoopExitBB = BI.getSuccessor(0); 179 if (!ExitBlockSet.count(LoopExitBB)) { 180 std::swap(CondVal, Replacement); 181 LoopExitSuccIdx = 1; 182 LoopExitBB = BI.getSuccessor(1); 183 if (!ExitBlockSet.count(LoopExitBB)) 184 return false; 185 } 186 auto *ContinueBB = BI.getSuccessor(1 - LoopExitSuccIdx); 187 assert(L.contains(ContinueBB) && 188 "Cannot have both successors exit and still be in the loop!"); 189 190 // If the loop exit block contains phi nodes, this isn't trivial. 191 // FIXME: We should examine the PHI to determine whether or not we can handle 192 // it trivially. 193 if (isa<PHINode>(LoopExitBB->begin())) 194 return false; 195 196 DEBUG(dbgs() << " unswitching trivial branch when: " << CondVal 197 << " == " << LoopCond << "\n"); 198 199 // Split the preheader, so that we know that there is a safe place to insert 200 // the conditional branch. We will change the preheader to have a conditional 201 // branch on LoopCond. 202 BasicBlock *OldPH = L.getLoopPreheader(); 203 BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI); 204 205 // Now that we have a place to insert the conditional branch, create a place 206 // to branch to: this is the exit block out of the loop that we are 207 // unswitching. We need to split this if there are other loop predecessors. 208 // Because the loop is in simplified form, *any* other predecessor is enough. 209 BasicBlock *UnswitchedBB; 210 if (BasicBlock *PredBB = LoopExitBB->getUniquePredecessor()) { 211 (void)PredBB; 212 assert(PredBB == BI.getParent() && "A branch's parent is't a predecessor!"); 213 UnswitchedBB = LoopExitBB; 214 } else { 215 UnswitchedBB = SplitBlock(LoopExitBB, &LoopExitBB->front(), &DT, &LI); 216 } 217 218 BasicBlock *ParentBB = BI.getParent(); 219 220 // Now splice the branch to gate reaching the new preheader and re-point its 221 // successors. 222 OldPH->getInstList().splice(std::prev(OldPH->end()), 223 BI.getParent()->getInstList(), BI); 224 OldPH->getTerminator()->eraseFromParent(); 225 BI.setSuccessor(LoopExitSuccIdx, UnswitchedBB); 226 BI.setSuccessor(1 - LoopExitSuccIdx, NewPH); 227 228 // Create a new unconditional branch that will continue the loop as a new 229 // terminator. 230 BranchInst::Create(ContinueBB, ParentBB); 231 232 // Now we need to update the dominator tree. 233 updateDTAfterUnswitch(UnswitchedBB, OldPH, DT); 234 // But if we split something off of the loop exit block then we also removed 235 // one of the predecessors for the loop exit block and may need to update its 236 // idom. 237 if (UnswitchedBB != LoopExitBB) 238 updateLoopExitIDom(LoopExitBB, L, DT); 239 240 // Since this is an i1 condition we can also trivially replace uses of it 241 // within the loop with a constant. 242 replaceLoopUsesWithConstant(L, *LoopCond, *Replacement); 243 244 ++NumTrivial; 245 ++NumBranches; 246 return true; 247 } 248 249 /// Unswitch a trivial switch if the condition is loop invariant. 250 /// 251 /// This routine should only be called when loop code leading to the switch has 252 /// been validated as trivial (no side effects). This routine checks if the 253 /// condition is invariant and that at least one of the successors is a loop 254 /// exit. This allows us to unswitch without duplicating the loop, making it 255 /// trivial. 256 /// 257 /// If this routine fails to unswitch the switch it returns false. 258 /// 259 /// If the switch can be unswitched, this routine splits the preheader and 260 /// copies the switch above that split. If the default case is one of the 261 /// exiting cases, it copies the non-exiting cases and points them at the new 262 /// preheader. If the default case is not exiting, it copies the exiting cases 263 /// and points the default at the preheader. It preserves loop simplified form 264 /// (splitting the exit blocks as necessary). It simplifies the switch within 265 /// the loop by removing now-dead cases. If the default case is one of those 266 /// unswitched, it replaces its destination with a new basic block containing 267 /// only unreachable. Such basic blocks, while technically loop exits, are not 268 /// considered for unswitching so this is a stable transform and the same 269 /// switch will not be revisited. If after unswitching there is only a single 270 /// in-loop successor, the switch is further simplified to an unconditional 271 /// branch. Still more cleanup can be done with some simplify-cfg like pass. 272 static bool unswitchTrivialSwitch(Loop &L, SwitchInst &SI, DominatorTree &DT, 273 LoopInfo &LI) { 274 DEBUG(dbgs() << " Trying to unswitch switch: " << SI << "\n"); 275 Value *LoopCond = SI.getCondition(); 276 277 // If this isn't switching on an invariant condition, we can't unswitch it. 278 if (!L.isLoopInvariant(LoopCond)) 279 return false; 280 281 // FIXME: We should compute this once at the start and update it! 282 SmallVector<BasicBlock *, 16> ExitBlocks; 283 L.getExitBlocks(ExitBlocks); 284 SmallPtrSet<BasicBlock *, 16> ExitBlockSet(ExitBlocks.begin(), 285 ExitBlocks.end()); 286 287 SmallVector<int, 4> ExitCaseIndices; 288 for (auto Case : SI.cases()) { 289 auto *SuccBB = Case.getCaseSuccessor(); 290 if (ExitBlockSet.count(SuccBB) && !isa<PHINode>(SuccBB->begin())) 291 ExitCaseIndices.push_back(Case.getCaseIndex()); 292 } 293 BasicBlock *DefaultExitBB = nullptr; 294 if (ExitBlockSet.count(SI.getDefaultDest()) && 295 !isa<PHINode>(SI.getDefaultDest()->begin()) && 296 !isa<UnreachableInst>(SI.getDefaultDest()->getTerminator())) 297 DefaultExitBB = SI.getDefaultDest(); 298 else if (ExitCaseIndices.empty()) 299 return false; 300 301 DEBUG(dbgs() << " unswitching trivial cases...\n"); 302 303 SmallVector<std::pair<ConstantInt *, BasicBlock *>, 4> ExitCases; 304 ExitCases.reserve(ExitCaseIndices.size()); 305 // We walk the case indices backwards so that we remove the last case first 306 // and don't disrupt the earlier indices. 307 for (unsigned Index : reverse(ExitCaseIndices)) { 308 auto CaseI = SI.case_begin() + Index; 309 // Save the value of this case. 310 ExitCases.push_back({CaseI->getCaseValue(), CaseI->getCaseSuccessor()}); 311 // Delete the unswitched cases. 312 SI.removeCase(CaseI); 313 } 314 315 // Check if after this all of the remaining cases point at the same 316 // successor. 317 BasicBlock *CommonSuccBB = nullptr; 318 if (SI.getNumCases() > 0 && 319 std::all_of(std::next(SI.case_begin()), SI.case_end(), 320 [&SI](const SwitchInst::CaseHandle &Case) { 321 return Case.getCaseSuccessor() == 322 SI.case_begin()->getCaseSuccessor(); 323 })) 324 CommonSuccBB = SI.case_begin()->getCaseSuccessor(); 325 326 if (DefaultExitBB) { 327 // We can't remove the default edge so replace it with an edge to either 328 // the single common remaining successor (if we have one) or an unreachable 329 // block. 330 if (CommonSuccBB) { 331 SI.setDefaultDest(CommonSuccBB); 332 } else { 333 BasicBlock *ParentBB = SI.getParent(); 334 BasicBlock *UnreachableBB = BasicBlock::Create( 335 ParentBB->getContext(), 336 Twine(ParentBB->getName()) + ".unreachable_default", 337 ParentBB->getParent()); 338 new UnreachableInst(ParentBB->getContext(), UnreachableBB); 339 SI.setDefaultDest(UnreachableBB); 340 DT.addNewBlock(UnreachableBB, ParentBB); 341 } 342 } else { 343 // If we're not unswitching the default, we need it to match any cases to 344 // have a common successor or if we have no cases it is the common 345 // successor. 346 if (SI.getNumCases() == 0) 347 CommonSuccBB = SI.getDefaultDest(); 348 else if (SI.getDefaultDest() != CommonSuccBB) 349 CommonSuccBB = nullptr; 350 } 351 352 // Split the preheader, so that we know that there is a safe place to insert 353 // the switch. 354 BasicBlock *OldPH = L.getLoopPreheader(); 355 BasicBlock *NewPH = SplitEdge(OldPH, L.getHeader(), &DT, &LI); 356 OldPH->getTerminator()->eraseFromParent(); 357 358 // Now add the unswitched switch. 359 auto *NewSI = SwitchInst::Create(LoopCond, NewPH, ExitCases.size(), OldPH); 360 361 // Split any exit blocks with remaining in-loop predecessors. We walk in 362 // reverse so that we split in the same order as the cases appeared. This is 363 // purely for convenience of reading the resulting IR, but it doesn't cost 364 // anything really. 365 SmallDenseMap<BasicBlock *, BasicBlock *, 2> SplitExitBBMap; 366 // Handle the default exit if necessary. 367 // FIXME: It'd be great if we could merge this with the loop below but LLVM's 368 // ranges aren't quite powerful enough yet. 369 if (DefaultExitBB && !pred_empty(DefaultExitBB)) { 370 auto *SplitBB = 371 SplitBlock(DefaultExitBB, &DefaultExitBB->front(), &DT, &LI); 372 updateLoopExitIDom(DefaultExitBB, L, DT); 373 DefaultExitBB = SplitExitBBMap[DefaultExitBB] = SplitBB; 374 } 375 // Note that we must use a reference in the for loop so that we update the 376 // container. 377 for (auto &CasePair : reverse(ExitCases)) { 378 // Grab a reference to the exit block in the pair so that we can update it. 379 BasicBlock *&ExitBB = CasePair.second; 380 381 // If this case is the last edge into the exit block, we can simply reuse it 382 // as it will no longer be a loop exit. No mapping necessary. 383 if (pred_empty(ExitBB)) 384 continue; 385 386 // Otherwise we need to split the exit block so that we retain an exit 387 // block from the loop and a target for the unswitched condition. 388 BasicBlock *&SplitExitBB = SplitExitBBMap[ExitBB]; 389 if (!SplitExitBB) { 390 // If this is the first time we see this, do the split and remember it. 391 SplitExitBB = SplitBlock(ExitBB, &ExitBB->front(), &DT, &LI); 392 updateLoopExitIDom(ExitBB, L, DT); 393 } 394 ExitBB = SplitExitBB; 395 } 396 397 // Now add the unswitched cases. We do this in reverse order as we built them 398 // in reverse order. 399 for (auto CasePair : reverse(ExitCases)) { 400 ConstantInt *CaseVal = CasePair.first; 401 BasicBlock *UnswitchedBB = CasePair.second; 402 403 NewSI->addCase(CaseVal, UnswitchedBB); 404 updateDTAfterUnswitch(UnswitchedBB, OldPH, DT); 405 } 406 407 // If the default was unswitched, re-point it and add explicit cases for 408 // entering the loop. 409 if (DefaultExitBB) { 410 NewSI->setDefaultDest(DefaultExitBB); 411 updateDTAfterUnswitch(DefaultExitBB, OldPH, DT); 412 413 // We removed all the exit cases, so we just copy the cases to the 414 // unswitched switch. 415 for (auto Case : SI.cases()) 416 NewSI->addCase(Case.getCaseValue(), NewPH); 417 } 418 419 // If we ended up with a common successor for every path through the switch 420 // after unswitching, rewrite it to an unconditional branch to make it easy 421 // to recognize. Otherwise we potentially have to recognize the default case 422 // pointing at unreachable and other complexity. 423 if (CommonSuccBB) { 424 BasicBlock *BB = SI.getParent(); 425 SI.eraseFromParent(); 426 BranchInst::Create(CommonSuccBB, BB); 427 } 428 429 DT.verifyDomTree(); 430 ++NumTrivial; 431 ++NumSwitches; 432 return true; 433 } 434 435 /// This routine scans the loop to find a branch or switch which occurs before 436 /// any side effects occur. These can potentially be unswitched without 437 /// duplicating the loop. If a branch or switch is successfully unswitched the 438 /// scanning continues to see if subsequent branches or switches have become 439 /// trivial. Once all trivial candidates have been unswitched, this routine 440 /// returns. 441 /// 442 /// The return value indicates whether anything was unswitched (and therefore 443 /// changed). 444 static bool unswitchAllTrivialConditions(Loop &L, DominatorTree &DT, 445 LoopInfo &LI) { 446 bool Changed = false; 447 448 // If loop header has only one reachable successor we should keep looking for 449 // trivial condition candidates in the successor as well. An alternative is 450 // to constant fold conditions and merge successors into loop header (then we 451 // only need to check header's terminator). The reason for not doing this in 452 // LoopUnswitch pass is that it could potentially break LoopPassManager's 453 // invariants. Folding dead branches could either eliminate the current loop 454 // or make other loops unreachable. LCSSA form might also not be preserved 455 // after deleting branches. The following code keeps traversing loop header's 456 // successors until it finds the trivial condition candidate (condition that 457 // is not a constant). Since unswitching generates branches with constant 458 // conditions, this scenario could be very common in practice. 459 BasicBlock *CurrentBB = L.getHeader(); 460 SmallPtrSet<BasicBlock *, 8> Visited; 461 Visited.insert(CurrentBB); 462 do { 463 // Check if there are any side-effecting instructions (e.g. stores, calls, 464 // volatile loads) in the part of the loop that the code *would* execute 465 // without unswitching. 466 if (llvm::any_of(*CurrentBB, 467 [](Instruction &I) { return I.mayHaveSideEffects(); })) 468 return Changed; 469 470 TerminatorInst *CurrentTerm = CurrentBB->getTerminator(); 471 472 if (auto *SI = dyn_cast<SwitchInst>(CurrentTerm)) { 473 // Don't bother trying to unswitch past a switch with a constant 474 // condition. This should be removed prior to running this pass by 475 // simplify-cfg. 476 if (isa<Constant>(SI->getCondition())) 477 return Changed; 478 479 if (!unswitchTrivialSwitch(L, *SI, DT, LI)) 480 // Coludn't unswitch this one so we're done. 481 return Changed; 482 483 // Mark that we managed to unswitch something. 484 Changed = true; 485 486 // If unswitching turned the terminator into an unconditional branch then 487 // we can continue. The unswitching logic specifically works to fold any 488 // cases it can into an unconditional branch to make it easier to 489 // recognize here. 490 auto *BI = dyn_cast<BranchInst>(CurrentBB->getTerminator()); 491 if (!BI || BI->isConditional()) 492 return Changed; 493 494 CurrentBB = BI->getSuccessor(0); 495 continue; 496 } 497 498 auto *BI = dyn_cast<BranchInst>(CurrentTerm); 499 if (!BI) 500 // We do not understand other terminator instructions. 501 return Changed; 502 503 // Don't bother trying to unswitch past an unconditional branch or a branch 504 // with a constant value. These should be removed by simplify-cfg prior to 505 // running this pass. 506 if (!BI->isConditional() || isa<Constant>(BI->getCondition())) 507 return Changed; 508 509 // Found a trivial condition candidate: non-foldable conditional branch. If 510 // we fail to unswitch this, we can't do anything else that is trivial. 511 if (!unswitchTrivialBranch(L, *BI, DT, LI)) 512 return Changed; 513 514 // Mark that we managed to unswitch something. 515 Changed = true; 516 517 // We unswitched the branch. This should always leave us with an 518 // unconditional branch that we can follow now. 519 BI = cast<BranchInst>(CurrentBB->getTerminator()); 520 assert(!BI->isConditional() && 521 "Cannot form a conditional branch by unswitching1"); 522 CurrentBB = BI->getSuccessor(0); 523 524 // When continuing, if we exit the loop or reach a previous visited block, 525 // then we can not reach any trivial condition candidates (unfoldable 526 // branch instructions or switch instructions) and no unswitch can happen. 527 } while (L.contains(CurrentBB) && Visited.insert(CurrentBB).second); 528 529 return Changed; 530 } 531 532 /// Unswitch control flow predicated on loop invariant conditions. 533 /// 534 /// This first hoists all branches or switches which are trivial (IE, do not 535 /// require duplicating any part of the loop) out of the loop body. It then 536 /// looks at other loop invariant control flows and tries to unswitch those as 537 /// well by cloning the loop if the result is small enough. 538 static bool unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, 539 AssumptionCache &AC) { 540 assert(L.isLCSSAForm(DT) && 541 "Loops must be in LCSSA form before unswitching."); 542 bool Changed = false; 543 544 // Must be in loop simplified form: we need a preheader and dedicated exits. 545 if (!L.isLoopSimplifyForm()) 546 return false; 547 548 // Try trivial unswitch first before loop over other basic blocks in the loop. 549 Changed |= unswitchAllTrivialConditions(L, DT, LI); 550 551 // FIXME: Add support for non-trivial unswitching by cloning the loop. 552 553 return Changed; 554 } 555 556 PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM, 557 LoopStandardAnalysisResults &AR, 558 LPMUpdater &U) { 559 Function &F = *L.getHeader()->getParent(); 560 (void)F; 561 562 DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << L << "\n"); 563 564 if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC)) 565 return PreservedAnalyses::all(); 566 567 #ifndef NDEBUG 568 // Historically this pass has had issues with the dominator tree so verify it 569 // in asserts builds. 570 AR.DT.verifyDomTree(); 571 #endif 572 return getLoopPassPreservedAnalyses(); 573 } 574 575 namespace { 576 class SimpleLoopUnswitchLegacyPass : public LoopPass { 577 public: 578 static char ID; // Pass ID, replacement for typeid 579 explicit SimpleLoopUnswitchLegacyPass() : LoopPass(ID) { 580 initializeSimpleLoopUnswitchLegacyPassPass( 581 *PassRegistry::getPassRegistry()); 582 } 583 584 bool runOnLoop(Loop *L, LPPassManager &LPM) override; 585 586 void getAnalysisUsage(AnalysisUsage &AU) const override { 587 AU.addRequired<AssumptionCacheTracker>(); 588 getLoopAnalysisUsage(AU); 589 } 590 }; 591 } // namespace 592 593 bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) { 594 if (skipLoop(L)) 595 return false; 596 597 Function &F = *L->getHeader()->getParent(); 598 599 DEBUG(dbgs() << "Unswitching loop in " << F.getName() << ": " << *L << "\n"); 600 601 auto &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 602 auto &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 603 auto &AC = getAnalysis<AssumptionCacheTracker>().getAssumptionCache(F); 604 605 bool Changed = unswitchLoop(*L, DT, LI, AC); 606 607 #ifndef NDEBUG 608 // Historically this pass has had issues with the dominator tree so verify it 609 // in asserts builds. 610 DT.verifyDomTree(); 611 #endif 612 return Changed; 613 } 614 615 char SimpleLoopUnswitchLegacyPass::ID = 0; 616 INITIALIZE_PASS_BEGIN(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", 617 "Simple unswitch loops", false, false) 618 INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker) 619 INITIALIZE_PASS_DEPENDENCY(LoopPass) 620 INITIALIZE_PASS_DEPENDENCY(TargetTransformInfoWrapperPass) 621 INITIALIZE_PASS_END(SimpleLoopUnswitchLegacyPass, "simple-loop-unswitch", 622 "Simple unswitch loops", false, false) 623 624 Pass *llvm::createSimpleLoopUnswitchLegacyPass() { 625 return new SimpleLoopUnswitchLegacyPass(); 626 } 627