1 //===- BreakCriticalEdges.cpp - Critical Edge Elimination Pass ------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // BreakCriticalEdges pass - Break all of the critical edges in the CFG by 10 // inserting a dummy basic block. This pass may be "required" by passes that 11 // cannot deal with critical edges. For this usage, the structure type is 12 // forward declared. This pass obviously invalidates the CFG, but can update 13 // dominator trees. 14 // 15 //===----------------------------------------------------------------------===// 16 17 #include "llvm/Transforms/Utils/BreakCriticalEdges.h" 18 #include "llvm/ADT/SetVector.h" 19 #include "llvm/ADT/SmallVector.h" 20 #include "llvm/ADT/Statistic.h" 21 #include "llvm/Analysis/BlockFrequencyInfo.h" 22 #include "llvm/Analysis/BranchProbabilityInfo.h" 23 #include "llvm/Analysis/CFG.h" 24 #include "llvm/Analysis/LoopInfo.h" 25 #include "llvm/Analysis/MemorySSAUpdater.h" 26 #include "llvm/Analysis/PostDominators.h" 27 #include "llvm/IR/CFG.h" 28 #include "llvm/IR/Dominators.h" 29 #include "llvm/IR/Instructions.h" 30 #include "llvm/IR/Type.h" 31 #include "llvm/Support/ErrorHandling.h" 32 #include "llvm/Transforms/Utils.h" 33 #include "llvm/Transforms/Utils/BasicBlockUtils.h" 34 #include "llvm/Transforms/Utils/Cloning.h" 35 #include "llvm/Transforms/Utils/ValueMapper.h" 36 using namespace llvm; 37 38 #define DEBUG_TYPE "break-crit-edges" 39 40 STATISTIC(NumBroken, "Number of blocks inserted"); 41 42 namespace { 43 struct BreakCriticalEdges : public FunctionPass { 44 static char ID; // Pass identification, replacement for typeid 45 BreakCriticalEdges() : FunctionPass(ID) { 46 initializeBreakCriticalEdgesPass(*PassRegistry::getPassRegistry()); 47 } 48 49 bool runOnFunction(Function &F) override { 50 auto *DTWP = getAnalysisIfAvailable<DominatorTreeWrapperPass>(); 51 auto *DT = DTWP ? &DTWP->getDomTree() : nullptr; 52 53 auto *PDTWP = getAnalysisIfAvailable<PostDominatorTreeWrapperPass>(); 54 auto *PDT = PDTWP ? &PDTWP->getPostDomTree() : nullptr; 55 56 auto *LIWP = getAnalysisIfAvailable<LoopInfoWrapperPass>(); 57 auto *LI = LIWP ? &LIWP->getLoopInfo() : nullptr; 58 unsigned N = 59 SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI, nullptr, PDT)); 60 NumBroken += N; 61 return N > 0; 62 } 63 64 void getAnalysisUsage(AnalysisUsage &AU) const override { 65 AU.addPreserved<DominatorTreeWrapperPass>(); 66 AU.addPreserved<LoopInfoWrapperPass>(); 67 68 // No loop canonicalization guarantees are broken by this pass. 69 AU.addPreservedID(LoopSimplifyID); 70 } 71 }; 72 } 73 74 char BreakCriticalEdges::ID = 0; 75 INITIALIZE_PASS(BreakCriticalEdges, "break-crit-edges", 76 "Break critical edges in CFG", false, false) 77 78 // Publicly exposed interface to pass... 79 char &llvm::BreakCriticalEdgesID = BreakCriticalEdges::ID; 80 FunctionPass *llvm::createBreakCriticalEdgesPass() { 81 return new BreakCriticalEdges(); 82 } 83 84 PreservedAnalyses BreakCriticalEdgesPass::run(Function &F, 85 FunctionAnalysisManager &AM) { 86 auto *DT = AM.getCachedResult<DominatorTreeAnalysis>(F); 87 auto *LI = AM.getCachedResult<LoopAnalysis>(F); 88 unsigned N = SplitAllCriticalEdges(F, CriticalEdgeSplittingOptions(DT, LI)); 89 NumBroken += N; 90 if (N == 0) 91 return PreservedAnalyses::all(); 92 PreservedAnalyses PA; 93 PA.preserve<DominatorTreeAnalysis>(); 94 PA.preserve<LoopAnalysis>(); 95 return PA; 96 } 97 98 //===----------------------------------------------------------------------===// 99 // Implementation of the external critical edge manipulation functions 100 //===----------------------------------------------------------------------===// 101 102 /// When a loop exit edge is split, LCSSA form may require new PHIs in the new 103 /// exit block. This function inserts the new PHIs, as needed. Preds is a list 104 /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is 105 /// the old loop exit, now the successor of SplitBB. 106 static void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds, 107 BasicBlock *SplitBB, 108 BasicBlock *DestBB) { 109 // SplitBB shouldn't have anything non-trivial in it yet. 110 assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() || 111 SplitBB->isLandingPad()) && "SplitBB has non-PHI nodes!"); 112 113 // For each PHI in the destination block. 114 for (PHINode &PN : DestBB->phis()) { 115 unsigned Idx = PN.getBasicBlockIndex(SplitBB); 116 Value *V = PN.getIncomingValue(Idx); 117 118 // If the input is a PHI which already satisfies LCSSA, don't create 119 // a new one. 120 if (const PHINode *VP = dyn_cast<PHINode>(V)) 121 if (VP->getParent() == SplitBB) 122 continue; 123 124 // Otherwise a new PHI is needed. Create one and populate it. 125 PHINode *NewPN = PHINode::Create( 126 PN.getType(), Preds.size(), "split", 127 SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator()); 128 for (unsigned i = 0, e = Preds.size(); i != e; ++i) 129 NewPN->addIncoming(V, Preds[i]); 130 131 // Update the original PHI. 132 PN.setIncomingValue(Idx, NewPN); 133 } 134 } 135 136 BasicBlock * 137 llvm::SplitCriticalEdge(Instruction *TI, unsigned SuccNum, 138 const CriticalEdgeSplittingOptions &Options) { 139 if (!isCriticalEdge(TI, SuccNum, Options.MergeIdenticalEdges)) 140 return nullptr; 141 142 assert(!isa<IndirectBrInst>(TI) && 143 "Cannot split critical edge from IndirectBrInst"); 144 145 BasicBlock *TIBB = TI->getParent(); 146 BasicBlock *DestBB = TI->getSuccessor(SuccNum); 147 148 // Splitting the critical edge to a pad block is non-trivial. Don't do 149 // it in this generic function. 150 if (DestBB->isEHPad()) return nullptr; 151 152 // Don't split the non-fallthrough edge from a callbr. 153 if (isa<CallBrInst>(TI) && SuccNum > 0) 154 return nullptr; 155 156 if (Options.IgnoreUnreachableDests && 157 isa<UnreachableInst>(DestBB->getFirstNonPHIOrDbgOrLifetime())) 158 return nullptr; 159 160 // Create a new basic block, linking it into the CFG. 161 BasicBlock *NewBB = BasicBlock::Create(TI->getContext(), 162 TIBB->getName() + "." + DestBB->getName() + "_crit_edge"); 163 // Create our unconditional branch. 164 BranchInst *NewBI = BranchInst::Create(DestBB, NewBB); 165 NewBI->setDebugLoc(TI->getDebugLoc()); 166 167 // Branch to the new block, breaking the edge. 168 TI->setSuccessor(SuccNum, NewBB); 169 170 // Insert the block into the function... right after the block TI lives in. 171 Function &F = *TIBB->getParent(); 172 Function::iterator FBBI = TIBB->getIterator(); 173 F.getBasicBlockList().insert(++FBBI, NewBB); 174 175 // If there are any PHI nodes in DestBB, we need to update them so that they 176 // merge incoming values from NewBB instead of from TIBB. 177 { 178 unsigned BBIdx = 0; 179 for (BasicBlock::iterator I = DestBB->begin(); isa<PHINode>(I); ++I) { 180 // We no longer enter through TIBB, now we come in through NewBB. 181 // Revector exactly one entry in the PHI node that used to come from 182 // TIBB to come from NewBB. 183 PHINode *PN = cast<PHINode>(I); 184 185 // Reuse the previous value of BBIdx if it lines up. In cases where we 186 // have multiple phi nodes with *lots* of predecessors, this is a speed 187 // win because we don't have to scan the PHI looking for TIBB. This 188 // happens because the BB list of PHI nodes are usually in the same 189 // order. 190 if (PN->getIncomingBlock(BBIdx) != TIBB) 191 BBIdx = PN->getBasicBlockIndex(TIBB); 192 PN->setIncomingBlock(BBIdx, NewBB); 193 } 194 } 195 196 // If there are any other edges from TIBB to DestBB, update those to go 197 // through the split block, making those edges non-critical as well (and 198 // reducing the number of phi entries in the DestBB if relevant). 199 if (Options.MergeIdenticalEdges) { 200 for (unsigned i = SuccNum+1, e = TI->getNumSuccessors(); i != e; ++i) { 201 if (TI->getSuccessor(i) != DestBB) continue; 202 203 // Remove an entry for TIBB from DestBB phi nodes. 204 DestBB->removePredecessor(TIBB, Options.KeepOneInputPHIs); 205 206 // We found another edge to DestBB, go to NewBB instead. 207 TI->setSuccessor(i, NewBB); 208 } 209 } 210 211 // If we have nothing to update, just return. 212 auto *DT = Options.DT; 213 auto *PDT = Options.PDT; 214 auto *LI = Options.LI; 215 auto *MSSAU = Options.MSSAU; 216 if (MSSAU) 217 MSSAU->wireOldPredecessorsToNewImmediatePredecessor( 218 DestBB, NewBB, {TIBB}, Options.MergeIdenticalEdges); 219 220 if (!DT && !PDT && !LI) 221 return NewBB; 222 223 if (DT || PDT) { 224 // Update the DominatorTree. 225 // ---> NewBB -----\ 226 // / V 227 // TIBB -------\\------> DestBB 228 // 229 // First, inform the DT about the new path from TIBB to DestBB via NewBB, 230 // then delete the old edge from TIBB to DestBB. By doing this in that order 231 // DestBB stays reachable in the DT the whole time and its subtree doesn't 232 // get disconnected. 233 SmallVector<DominatorTree::UpdateType, 3> Updates; 234 Updates.push_back({DominatorTree::Insert, TIBB, NewBB}); 235 Updates.push_back({DominatorTree::Insert, NewBB, DestBB}); 236 if (llvm::find(successors(TIBB), DestBB) == succ_end(TIBB)) 237 Updates.push_back({DominatorTree::Delete, TIBB, DestBB}); 238 239 if (DT) 240 DT->applyUpdates(Updates); 241 if (PDT) 242 PDT->applyUpdates(Updates); 243 } 244 245 // Update LoopInfo if it is around. 246 if (LI) { 247 if (Loop *TIL = LI->getLoopFor(TIBB)) { 248 // If one or the other blocks were not in a loop, the new block is not 249 // either, and thus LI doesn't need to be updated. 250 if (Loop *DestLoop = LI->getLoopFor(DestBB)) { 251 if (TIL == DestLoop) { 252 // Both in the same loop, the NewBB joins loop. 253 DestLoop->addBasicBlockToLoop(NewBB, *LI); 254 } else if (TIL->contains(DestLoop)) { 255 // Edge from an outer loop to an inner loop. Add to the outer loop. 256 TIL->addBasicBlockToLoop(NewBB, *LI); 257 } else if (DestLoop->contains(TIL)) { 258 // Edge from an inner loop to an outer loop. Add to the outer loop. 259 DestLoop->addBasicBlockToLoop(NewBB, *LI); 260 } else { 261 // Edge from two loops with no containment relation. Because these 262 // are natural loops, we know that the destination block must be the 263 // header of its loop (adding a branch into a loop elsewhere would 264 // create an irreducible loop). 265 assert(DestLoop->getHeader() == DestBB && 266 "Should not create irreducible loops!"); 267 if (Loop *P = DestLoop->getParentLoop()) 268 P->addBasicBlockToLoop(NewBB, *LI); 269 } 270 } 271 272 // If TIBB is in a loop and DestBB is outside of that loop, we may need 273 // to update LoopSimplify form and LCSSA form. 274 if (!TIL->contains(DestBB)) { 275 assert(!TIL->contains(NewBB) && 276 "Split point for loop exit is contained in loop!"); 277 278 // Update LCSSA form in the newly created exit block. 279 if (Options.PreserveLCSSA) { 280 createPHIsForSplitLoopExit(TIBB, NewBB, DestBB); 281 } 282 283 // The only that we can break LoopSimplify form by splitting a critical 284 // edge is if after the split there exists some edge from TIL to DestBB 285 // *and* the only edge into DestBB from outside of TIL is that of 286 // NewBB. If the first isn't true, then LoopSimplify still holds, NewBB 287 // is the new exit block and it has no non-loop predecessors. If the 288 // second isn't true, then DestBB was not in LoopSimplify form prior to 289 // the split as it had a non-loop predecessor. In both of these cases, 290 // the predecessor must be directly in TIL, not in a subloop, or again 291 // LoopSimplify doesn't hold. 292 SmallVector<BasicBlock *, 4> LoopPreds; 293 for (pred_iterator I = pred_begin(DestBB), E = pred_end(DestBB); I != E; 294 ++I) { 295 BasicBlock *P = *I; 296 if (P == NewBB) 297 continue; // The new block is known. 298 if (LI->getLoopFor(P) != TIL) { 299 // No need to re-simplify, it wasn't to start with. 300 LoopPreds.clear(); 301 break; 302 } 303 LoopPreds.push_back(P); 304 } 305 if (!LoopPreds.empty()) { 306 assert(!DestBB->isEHPad() && "We don't split edges to EH pads!"); 307 BasicBlock *NewExitBB = SplitBlockPredecessors( 308 DestBB, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA); 309 if (Options.PreserveLCSSA) 310 createPHIsForSplitLoopExit(LoopPreds, NewExitBB, DestBB); 311 } 312 } 313 } 314 } 315 316 return NewBB; 317 } 318 319 // Return the unique indirectbr predecessor of a block. This may return null 320 // even if such a predecessor exists, if it's not useful for splitting. 321 // If a predecessor is found, OtherPreds will contain all other (non-indirectbr) 322 // predecessors of BB. 323 static BasicBlock * 324 findIBRPredecessor(BasicBlock *BB, SmallVectorImpl<BasicBlock *> &OtherPreds) { 325 // If the block doesn't have any PHIs, we don't care about it, since there's 326 // no point in splitting it. 327 PHINode *PN = dyn_cast<PHINode>(BB->begin()); 328 if (!PN) 329 return nullptr; 330 331 // Verify we have exactly one IBR predecessor. 332 // Conservatively bail out if one of the other predecessors is not a "regular" 333 // terminator (that is, not a switch or a br). 334 BasicBlock *IBB = nullptr; 335 for (unsigned Pred = 0, E = PN->getNumIncomingValues(); Pred != E; ++Pred) { 336 BasicBlock *PredBB = PN->getIncomingBlock(Pred); 337 Instruction *PredTerm = PredBB->getTerminator(); 338 switch (PredTerm->getOpcode()) { 339 case Instruction::IndirectBr: 340 if (IBB) 341 return nullptr; 342 IBB = PredBB; 343 break; 344 case Instruction::Br: 345 case Instruction::Switch: 346 OtherPreds.push_back(PredBB); 347 continue; 348 default: 349 return nullptr; 350 } 351 } 352 353 return IBB; 354 } 355 356 bool llvm::SplitIndirectBrCriticalEdges(Function &F, 357 BranchProbabilityInfo *BPI, 358 BlockFrequencyInfo *BFI) { 359 // Check whether the function has any indirectbrs, and collect which blocks 360 // they may jump to. Since most functions don't have indirect branches, 361 // this lowers the common case's overhead to O(Blocks) instead of O(Edges). 362 SmallSetVector<BasicBlock *, 16> Targets; 363 for (auto &BB : F) { 364 auto *IBI = dyn_cast<IndirectBrInst>(BB.getTerminator()); 365 if (!IBI) 366 continue; 367 368 for (unsigned Succ = 0, E = IBI->getNumSuccessors(); Succ != E; ++Succ) 369 Targets.insert(IBI->getSuccessor(Succ)); 370 } 371 372 if (Targets.empty()) 373 return false; 374 375 bool ShouldUpdateAnalysis = BPI && BFI; 376 bool Changed = false; 377 for (BasicBlock *Target : Targets) { 378 SmallVector<BasicBlock *, 16> OtherPreds; 379 BasicBlock *IBRPred = findIBRPredecessor(Target, OtherPreds); 380 // If we did not found an indirectbr, or the indirectbr is the only 381 // incoming edge, this isn't the kind of edge we're looking for. 382 if (!IBRPred || OtherPreds.empty()) 383 continue; 384 385 // Don't even think about ehpads/landingpads. 386 Instruction *FirstNonPHI = Target->getFirstNonPHI(); 387 if (FirstNonPHI->isEHPad() || Target->isLandingPad()) 388 continue; 389 390 BasicBlock *BodyBlock = Target->splitBasicBlock(FirstNonPHI, ".split"); 391 if (ShouldUpdateAnalysis) { 392 // Copy the BFI/BPI from Target to BodyBlock. 393 for (unsigned I = 0, E = BodyBlock->getTerminator()->getNumSuccessors(); 394 I < E; ++I) 395 BPI->setEdgeProbability(BodyBlock, I, 396 BPI->getEdgeProbability(Target, I)); 397 BFI->setBlockFreq(BodyBlock, BFI->getBlockFreq(Target).getFrequency()); 398 } 399 // It's possible Target was its own successor through an indirectbr. 400 // In this case, the indirectbr now comes from BodyBlock. 401 if (IBRPred == Target) 402 IBRPred = BodyBlock; 403 404 // At this point Target only has PHIs, and BodyBlock has the rest of the 405 // block's body. Create a copy of Target that will be used by the "direct" 406 // preds. 407 ValueToValueMapTy VMap; 408 BasicBlock *DirectSucc = CloneBasicBlock(Target, VMap, ".clone", &F); 409 410 BlockFrequency BlockFreqForDirectSucc; 411 for (BasicBlock *Pred : OtherPreds) { 412 // If the target is a loop to itself, then the terminator of the split 413 // block (BodyBlock) needs to be updated. 414 BasicBlock *Src = Pred != Target ? Pred : BodyBlock; 415 Src->getTerminator()->replaceUsesOfWith(Target, DirectSucc); 416 if (ShouldUpdateAnalysis) 417 BlockFreqForDirectSucc += BFI->getBlockFreq(Src) * 418 BPI->getEdgeProbability(Src, DirectSucc); 419 } 420 if (ShouldUpdateAnalysis) { 421 BFI->setBlockFreq(DirectSucc, BlockFreqForDirectSucc.getFrequency()); 422 BlockFrequency NewBlockFreqForTarget = 423 BFI->getBlockFreq(Target) - BlockFreqForDirectSucc; 424 BFI->setBlockFreq(Target, NewBlockFreqForTarget.getFrequency()); 425 BPI->eraseBlock(Target); 426 } 427 428 // Ok, now fix up the PHIs. We know the two blocks only have PHIs, and that 429 // they are clones, so the number of PHIs are the same. 430 // (a) Remove the edge coming from IBRPred from the "Direct" PHI 431 // (b) Leave that as the only edge in the "Indirect" PHI. 432 // (c) Merge the two in the body block. 433 BasicBlock::iterator Indirect = Target->begin(), 434 End = Target->getFirstNonPHI()->getIterator(); 435 BasicBlock::iterator Direct = DirectSucc->begin(); 436 BasicBlock::iterator MergeInsert = BodyBlock->getFirstInsertionPt(); 437 438 assert(&*End == Target->getTerminator() && 439 "Block was expected to only contain PHIs"); 440 441 while (Indirect != End) { 442 PHINode *DirPHI = cast<PHINode>(Direct); 443 PHINode *IndPHI = cast<PHINode>(Indirect); 444 445 // Now, clean up - the direct block shouldn't get the indirect value, 446 // and vice versa. 447 DirPHI->removeIncomingValue(IBRPred); 448 Direct++; 449 450 // Advance the pointer here, to avoid invalidation issues when the old 451 // PHI is erased. 452 Indirect++; 453 454 PHINode *NewIndPHI = PHINode::Create(IndPHI->getType(), 1, "ind", IndPHI); 455 NewIndPHI->addIncoming(IndPHI->getIncomingValueForBlock(IBRPred), 456 IBRPred); 457 458 // Create a PHI in the body block, to merge the direct and indirect 459 // predecessors. 460 PHINode *MergePHI = 461 PHINode::Create(IndPHI->getType(), 2, "merge", &*MergeInsert); 462 MergePHI->addIncoming(NewIndPHI, Target); 463 MergePHI->addIncoming(DirPHI, DirectSucc); 464 465 IndPHI->replaceAllUsesWith(MergePHI); 466 IndPHI->eraseFromParent(); 467 } 468 469 Changed = true; 470 } 471 472 return Changed; 473 } 474