1 //===- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils -----*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This family of functions perform manipulations on basic blocks, and 10 // instructions contained within basic blocks. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 15 #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 16 17 // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock 18 19 #include "llvm/ADT/ArrayRef.h" 20 #include "llvm/ADT/SetVector.h" 21 #include "llvm/Analysis/DomTreeUpdater.h" 22 #include "llvm/Analysis/LoopInfo.h" 23 #include "llvm/IR/BasicBlock.h" 24 #include "llvm/IR/CFG.h" 25 #include "llvm/IR/InstrTypes.h" 26 #include <cassert> 27 28 namespace llvm { 29 30 class BlockFrequencyInfo; 31 class BranchProbabilityInfo; 32 class DominatorTree; 33 class DomTreeUpdater; 34 class Function; 35 class Instruction; 36 class LoopInfo; 37 class MDNode; 38 class MemoryDependenceResults; 39 class MemorySSAUpdater; 40 class PostDominatorTree; 41 class ReturnInst; 42 class TargetLibraryInfo; 43 class Value; 44 45 /// Replace contents of every block in \p BBs with single unreachable 46 /// instruction. If \p Updates is specified, collect all necessary DT updates 47 /// into this vector. If \p KeepOneInputPHIs is true, one-input Phis in 48 /// successors of blocks being deleted will be preserved. 49 void detachDeadBlocks(ArrayRef <BasicBlock *> BBs, 50 SmallVectorImpl<DominatorTree::UpdateType> *Updates, 51 bool KeepOneInputPHIs = false); 52 53 /// Delete the specified block, which must have no predecessors. 54 void DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU = nullptr, 55 bool KeepOneInputPHIs = false); 56 57 /// Delete the specified blocks from \p BB. The set of deleted blocks must have 58 /// no predecessors that are not being deleted themselves. \p BBs must have no 59 /// duplicating blocks. If there are loops among this set of blocks, all 60 /// relevant loop info updates should be done before this function is called. 61 /// If \p KeepOneInputPHIs is true, one-input Phis in successors of blocks 62 /// being deleted will be preserved. 63 void DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, 64 DomTreeUpdater *DTU = nullptr, 65 bool KeepOneInputPHIs = false); 66 67 /// Delete all basic blocks from \p F that are not reachable from its entry 68 /// node. If \p KeepOneInputPHIs is true, one-input Phis in successors of 69 /// blocks being deleted will be preserved. 70 bool EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU = nullptr, 71 bool KeepOneInputPHIs = false); 72 73 /// We know that BB has one predecessor. If there are any single-entry PHI nodes 74 /// in it, fold them away. This handles the case when all entries to the PHI 75 /// nodes in a block are guaranteed equal, such as when the block has exactly 76 /// one predecessor. 77 bool FoldSingleEntryPHINodes(BasicBlock *BB, 78 MemoryDependenceResults *MemDep = nullptr); 79 80 /// Examine each PHI in the given block and delete it if it is dead. Also 81 /// recursively delete any operands that become dead as a result. This includes 82 /// tracing the def-use list from the PHI to see if it is ultimately unused or 83 /// if it reaches an unused cycle. Return true if any PHIs were deleted. 84 bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr, 85 MemorySSAUpdater *MSSAU = nullptr); 86 87 /// Attempts to merge a block into its predecessor, if possible. The return 88 /// value indicates success or failure. 89 /// By default do not merge blocks if BB's predecessor has multiple successors. 90 /// If PredecessorWithTwoSuccessors = true, the blocks can only be merged 91 /// if BB's Pred has a branch to BB and to AnotherBB, and BB has a single 92 /// successor Sing. In this case the branch will be updated with Sing instead of 93 /// BB, and BB will still be merged into its predecessor and removed. 94 bool MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU = nullptr, 95 LoopInfo *LI = nullptr, 96 MemorySSAUpdater *MSSAU = nullptr, 97 MemoryDependenceResults *MemDep = nullptr, 98 bool PredecessorWithTwoSuccessors = false); 99 100 /// Merge block(s) sucessors, if possible. Return true if at least two 101 /// of the blocks were merged together. 102 /// In order to merge, each block must be terminated by an unconditional 103 /// branch. If L is provided, then the blocks merged into their predecessors 104 /// must be in L. In addition, This utility calls on another utility: 105 /// MergeBlockIntoPredecessor. Blocks are successfully merged when the call to 106 /// MergeBlockIntoPredecessor returns true. 107 bool MergeBlockSuccessorsIntoGivenBlocks( 108 SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L = nullptr, 109 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr); 110 111 /// Try to remove redundant dbg.value instructions from given basic block. 112 /// Returns true if at least one instruction was removed. Remove redundant 113 /// pseudo ops when RemovePseudoOp is true. 114 bool RemoveRedundantDbgInstrs(BasicBlock *BB); 115 116 /// Replace all uses of an instruction (specified by BI) with a value, then 117 /// remove and delete the original instruction. 118 void ReplaceInstWithValue(BasicBlock::InstListType &BIL, 119 BasicBlock::iterator &BI, Value *V); 120 121 /// Replace the instruction specified by BI with the instruction specified by I. 122 /// Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. The 123 /// original instruction is deleted and BI is updated to point to the new 124 /// instruction. 125 void ReplaceInstWithInst(BasicBlock::InstListType &BIL, 126 BasicBlock::iterator &BI, Instruction *I); 127 128 /// Replace the instruction specified by From with the instruction specified by 129 /// To. Copies DebugLoc from BI to I, if I doesn't already have a DebugLoc. 130 void ReplaceInstWithInst(Instruction *From, Instruction *To); 131 132 /// Check if we can prove that all paths starting from this block converge 133 /// to a block that either has a @llvm.experimental.deoptimize call 134 /// prior to its terminating return instruction or is terminated by unreachable. 135 /// All blocks in the traversed sequence must have an unique successor, maybe 136 /// except for the last one. 137 bool IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB); 138 139 /// Option class for critical edge splitting. 140 /// 141 /// This provides a builder interface for overriding the default options used 142 /// during critical edge splitting. 143 struct CriticalEdgeSplittingOptions { 144 DominatorTree *DT; 145 PostDominatorTree *PDT; 146 LoopInfo *LI; 147 MemorySSAUpdater *MSSAU; 148 bool MergeIdenticalEdges = false; 149 bool KeepOneInputPHIs = false; 150 bool PreserveLCSSA = false; 151 bool IgnoreUnreachableDests = false; 152 /// SplitCriticalEdge is guaranteed to preserve loop-simplify form if LI is 153 /// provided. If it cannot be preserved, no splitting will take place. If it 154 /// is not set, preserve loop-simplify form if possible. 155 bool PreserveLoopSimplify = true; 156 157 CriticalEdgeSplittingOptions(DominatorTree *DT = nullptr, 158 LoopInfo *LI = nullptr, 159 MemorySSAUpdater *MSSAU = nullptr, 160 PostDominatorTree *PDT = nullptr) 161 : DT(DT), PDT(PDT), LI(LI), MSSAU(MSSAU) {} 162 163 CriticalEdgeSplittingOptions &setMergeIdenticalEdges() { 164 MergeIdenticalEdges = true; 165 return *this; 166 } 167 168 CriticalEdgeSplittingOptions &setKeepOneInputPHIs() { 169 KeepOneInputPHIs = true; 170 return *this; 171 } 172 173 CriticalEdgeSplittingOptions &setPreserveLCSSA() { 174 PreserveLCSSA = true; 175 return *this; 176 } 177 178 CriticalEdgeSplittingOptions &setIgnoreUnreachableDests() { 179 IgnoreUnreachableDests = true; 180 return *this; 181 } 182 183 CriticalEdgeSplittingOptions &unsetPreserveLoopSimplify() { 184 PreserveLoopSimplify = false; 185 return *this; 186 } 187 }; 188 189 /// When a loop exit edge is split, LCSSA form may require new PHIs in the new 190 /// exit block. This function inserts the new PHIs, as needed. Preds is a list 191 /// of preds inside the loop, SplitBB is the new loop exit block, and DestBB is 192 /// the old loop exit, now the successor of SplitBB. 193 void createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds, 194 BasicBlock *SplitBB, BasicBlock *DestBB); 195 196 /// If this edge is a critical edge, insert a new node to split the critical 197 /// edge. This will update the analyses passed in through the option struct. 198 /// This returns the new block if the edge was split, null otherwise. 199 /// 200 /// If MergeIdenticalEdges in the options struct is true (not the default), 201 /// *all* edges from TI to the specified successor will be merged into the same 202 /// critical edge block. This is most commonly interesting with switch 203 /// instructions, which may have many edges to any one destination. This 204 /// ensures that all edges to that dest go to one block instead of each going 205 /// to a different block, but isn't the standard definition of a "critical 206 /// edge". 207 /// 208 /// It is invalid to call this function on a critical edge that starts at an 209 /// IndirectBrInst. Splitting these edges will almost always create an invalid 210 /// program because the address of the new block won't be the one that is jumped 211 /// to. 212 BasicBlock *SplitCriticalEdge(Instruction *TI, unsigned SuccNum, 213 const CriticalEdgeSplittingOptions &Options = 214 CriticalEdgeSplittingOptions(), 215 const Twine &BBName = ""); 216 217 /// If it is known that an edge is critical, SplitKnownCriticalEdge can be 218 /// called directly, rather than calling SplitCriticalEdge first. 219 BasicBlock *SplitKnownCriticalEdge(Instruction *TI, unsigned SuccNum, 220 const CriticalEdgeSplittingOptions &Options = 221 CriticalEdgeSplittingOptions(), 222 const Twine &BBName = ""); 223 224 /// If an edge from Src to Dst is critical, split the edge and return true, 225 /// otherwise return false. This method requires that there be an edge between 226 /// the two blocks. It updates the analyses passed in the options struct 227 inline BasicBlock * 228 SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst, 229 const CriticalEdgeSplittingOptions &Options = 230 CriticalEdgeSplittingOptions()) { 231 Instruction *TI = Src->getTerminator(); 232 unsigned i = 0; 233 while (true) { 234 assert(i != TI->getNumSuccessors() && "Edge doesn't exist!"); 235 if (TI->getSuccessor(i) == Dst) 236 return SplitCriticalEdge(TI, i, Options); 237 ++i; 238 } 239 } 240 241 /// Loop over all of the edges in the CFG, breaking critical edges as they are 242 /// found. Returns the number of broken edges. 243 unsigned SplitAllCriticalEdges(Function &F, 244 const CriticalEdgeSplittingOptions &Options = 245 CriticalEdgeSplittingOptions()); 246 247 /// Split the edge connecting the specified blocks, and return the newly created 248 /// basic block between \p From and \p To. 249 BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To, 250 DominatorTree *DT = nullptr, LoopInfo *LI = nullptr, 251 MemorySSAUpdater *MSSAU = nullptr, 252 const Twine &BBName = ""); 253 254 /// Sets the unwind edge of an instruction to a particular successor. 255 void setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ); 256 257 /// Replaces all uses of OldPred with the NewPred block in all PHINodes in a 258 /// block. 259 void updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred, 260 BasicBlock *NewPred, PHINode *Until = nullptr); 261 262 /// Split the edge connect the specficed blocks in the case that \p Succ is an 263 /// Exception Handling Block 264 BasicBlock *ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ, 265 LandingPadInst *OriginalPad = nullptr, 266 PHINode *LandingPadReplacement = nullptr, 267 const CriticalEdgeSplittingOptions &Options = 268 CriticalEdgeSplittingOptions(), 269 const Twine &BBName = ""); 270 271 /// Split the specified block at the specified instruction. 272 /// 273 /// If \p Before is true, splitBlockBefore handles the block 274 /// splitting. Otherwise, execution proceeds as described below. 275 /// 276 /// Everything before \p SplitPt stays in \p Old and everything starting with \p 277 /// SplitPt moves to a new block. The two blocks are joined by an unconditional 278 /// branch. The new block with name \p BBName is returned. 279 /// 280 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 281 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, DominatorTree *DT, 282 LoopInfo *LI = nullptr, 283 MemorySSAUpdater *MSSAU = nullptr, 284 const Twine &BBName = "", bool Before = false); 285 286 /// Split the specified block at the specified instruction. 287 /// 288 /// If \p Before is true, splitBlockBefore handles the block 289 /// splitting. Otherwise, execution proceeds as described below. 290 /// 291 /// Everything before \p SplitPt stays in \p Old and everything starting with \p 292 /// SplitPt moves to a new block. The two blocks are joined by an unconditional 293 /// branch. The new block with name \p BBName is returned. 294 BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt, 295 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, 296 MemorySSAUpdater *MSSAU = nullptr, 297 const Twine &BBName = "", bool Before = false); 298 299 /// Split the specified block at the specified instruction \p SplitPt. 300 /// All instructions before \p SplitPt are moved to a new block and all 301 /// instructions after \p SplitPt stay in the old block. The new block and the 302 /// old block are joined by inserting an unconditional branch to the end of the 303 /// new block. The new block with name \p BBName is returned. 304 BasicBlock *splitBlockBefore(BasicBlock *Old, Instruction *SplitPt, 305 DomTreeUpdater *DTU, LoopInfo *LI, 306 MemorySSAUpdater *MSSAU, const Twine &BBName = ""); 307 308 /// This method introduces at least one new basic block into the function and 309 /// moves some of the predecessors of BB to be predecessors of the new block. 310 /// The new predecessors are indicated by the Preds array. The new block is 311 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors 312 /// from Preds are now pointing. 313 /// 314 /// If BB is a landingpad block then additional basicblock might be introduced. 315 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 316 /// details on this case. 317 /// 318 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 319 /// no other analyses. In particular, it does not preserve LoopSimplify 320 /// (because it's complicated to handle the case where one of the edges being 321 /// split is an exit of a loop with other exits). 322 /// 323 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 324 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 325 const char *Suffix, DominatorTree *DT, 326 LoopInfo *LI = nullptr, 327 MemorySSAUpdater *MSSAU = nullptr, 328 bool PreserveLCSSA = false); 329 330 /// This method introduces at least one new basic block into the function and 331 /// moves some of the predecessors of BB to be predecessors of the new block. 332 /// The new predecessors are indicated by the Preds array. The new block is 333 /// given a suffix of 'Suffix'. Returns new basic block to which predecessors 334 /// from Preds are now pointing. 335 /// 336 /// If BB is a landingpad block then additional basicblock might be introduced. 337 /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more 338 /// details on this case. 339 /// 340 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 341 /// no other analyses. In particular, it does not preserve LoopSimplify 342 /// (because it's complicated to handle the case where one of the edges being 343 /// split is an exit of a loop with other exits). 344 BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds, 345 const char *Suffix, 346 DomTreeUpdater *DTU = nullptr, 347 LoopInfo *LI = nullptr, 348 MemorySSAUpdater *MSSAU = nullptr, 349 bool PreserveLCSSA = false); 350 351 /// This method transforms the landing pad, OrigBB, by introducing two new basic 352 /// blocks into the function. One of those new basic blocks gets the 353 /// predecessors listed in Preds. The other basic block gets the remaining 354 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both 355 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and 356 /// 'Suffix2', and are returned in the NewBBs vector. 357 /// 358 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 359 /// no other analyses. In particular, it does not preserve LoopSimplify 360 /// (because it's complicated to handle the case where one of the edges being 361 /// split is an exit of a loop with other exits). 362 /// 363 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 364 void SplitLandingPadPredecessors(BasicBlock *OrigBB, 365 ArrayRef<BasicBlock *> Preds, 366 const char *Suffix, const char *Suffix2, 367 SmallVectorImpl<BasicBlock *> &NewBBs, 368 DominatorTree *DT, LoopInfo *LI = nullptr, 369 MemorySSAUpdater *MSSAU = nullptr, 370 bool PreserveLCSSA = false); 371 372 /// This method transforms the landing pad, OrigBB, by introducing two new basic 373 /// blocks into the function. One of those new basic blocks gets the 374 /// predecessors listed in Preds. The other basic block gets the remaining 375 /// predecessors of OrigBB. The landingpad instruction OrigBB is clone into both 376 /// of the new basic blocks. The new blocks are given the suffixes 'Suffix1' and 377 /// 'Suffix2', and are returned in the NewBBs vector. 378 /// 379 /// This currently updates the LLVM IR, DominatorTree, LoopInfo, and LCCSA but 380 /// no other analyses. In particular, it does not preserve LoopSimplify 381 /// (because it's complicated to handle the case where one of the edges being 382 /// split is an exit of a loop with other exits). 383 void SplitLandingPadPredecessors( 384 BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix, 385 const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs, 386 DomTreeUpdater *DTU = nullptr, LoopInfo *LI = nullptr, 387 MemorySSAUpdater *MSSAU = nullptr, bool PreserveLCSSA = false); 388 389 /// This method duplicates the specified return instruction into a predecessor 390 /// which ends in an unconditional branch. If the return instruction returns a 391 /// value defined by a PHI, propagate the right value into the return. It 392 /// returns the new return instruction in the predecessor. 393 ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB, 394 BasicBlock *Pred, 395 DomTreeUpdater *DTU = nullptr); 396 397 /// Split the containing block at the specified instruction - everything before 398 /// SplitBefore stays in the old basic block, and the rest of the instructions 399 /// in the BB are moved to a new block. The two blocks are connected by a 400 /// conditional branch (with value of Cmp being the condition). 401 /// Before: 402 /// Head 403 /// SplitBefore 404 /// Tail 405 /// After: 406 /// Head 407 /// if (Cond) 408 /// ThenBlock 409 /// SplitBefore 410 /// Tail 411 /// 412 /// If \p ThenBlock is not specified, a new block will be created for it. 413 /// If \p Unreachable is true, the newly created block will end with 414 /// UnreachableInst, otherwise it branches to Tail. 415 /// Returns the NewBasicBlock's terminator. 416 /// 417 /// Updates DT and LI if given. 418 /// 419 /// FIXME: deprecated, switch to the DomTreeUpdater-based one. 420 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 421 bool Unreachable, MDNode *BranchWeights, 422 DominatorTree *DT, 423 LoopInfo *LI = nullptr, 424 BasicBlock *ThenBlock = nullptr); 425 426 /// Split the containing block at the specified instruction - everything before 427 /// SplitBefore stays in the old basic block, and the rest of the instructions 428 /// in the BB are moved to a new block. The two blocks are connected by a 429 /// conditional branch (with value of Cmp being the condition). 430 /// Before: 431 /// Head 432 /// SplitBefore 433 /// Tail 434 /// After: 435 /// Head 436 /// if (Cond) 437 /// ThenBlock 438 /// SplitBefore 439 /// Tail 440 /// 441 /// If \p ThenBlock is not specified, a new block will be created for it. 442 /// If \p Unreachable is true, the newly created block will end with 443 /// UnreachableInst, otherwise it branches to Tail. 444 /// Returns the NewBasicBlock's terminator. 445 /// 446 /// Updates DT and LI if given. 447 Instruction *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore, 448 bool Unreachable, 449 MDNode *BranchWeights = nullptr, 450 DomTreeUpdater *DTU = nullptr, 451 LoopInfo *LI = nullptr, 452 BasicBlock *ThenBlock = nullptr); 453 454 /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen, 455 /// but also creates the ElseBlock. 456 /// Before: 457 /// Head 458 /// SplitBefore 459 /// Tail 460 /// After: 461 /// Head 462 /// if (Cond) 463 /// ThenBlock 464 /// else 465 /// ElseBlock 466 /// SplitBefore 467 /// Tail 468 void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore, 469 Instruction **ThenTerm, 470 Instruction **ElseTerm, 471 MDNode *BranchWeights = nullptr); 472 473 /// Check whether BB is the merge point of a if-region. 474 /// If so, return the branch instruction that determines which entry into 475 /// BB will be taken. Also, return by references the block that will be 476 /// entered from if the condition is true, and the block that will be 477 /// entered if the condition is false. 478 /// 479 /// This does no checking to see if the true/false blocks have large or unsavory 480 /// instructions in them. 481 BranchInst *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue, 482 BasicBlock *&IfFalse); 483 484 // Split critical edges where the source of the edge is an indirectbr 485 // instruction. This isn't always possible, but we can handle some easy cases. 486 // This is useful because MI is unable to split such critical edges, 487 // which means it will not be able to sink instructions along those edges. 488 // This is especially painful for indirect branches with many successors, where 489 // we end up having to prepare all outgoing values in the origin block. 490 // 491 // Our normal algorithm for splitting critical edges requires us to update 492 // the outgoing edges of the edge origin block, but for an indirectbr this 493 // is hard, since it would require finding and updating the block addresses 494 // the indirect branch uses. But if a block only has a single indirectbr 495 // predecessor, with the others being regular branches, we can do it in a 496 // different way. 497 // Say we have A -> D, B -> D, I -> D where only I -> D is an indirectbr. 498 // We can split D into D0 and D1, where D0 contains only the PHIs from D, 499 // and D1 is the D block body. We can then duplicate D0 as D0A and D0B, and 500 // create the following structure: 501 // A -> D0A, B -> D0A, I -> D0B, D0A -> D1, D0B -> D1 502 // If BPI and BFI aren't non-null, BPI/BFI will be updated accordingly. 503 bool SplitIndirectBrCriticalEdges(Function &F, 504 BranchProbabilityInfo *BPI = nullptr, 505 BlockFrequencyInfo *BFI = nullptr); 506 507 /// Given a set of incoming and outgoing blocks, create a "hub" such that every 508 /// edge from an incoming block InBB to an outgoing block OutBB is now split 509 /// into two edges, one from InBB to the hub and another from the hub to 510 /// OutBB. The hub consists of a series of guard blocks, one for each outgoing 511 /// block. Each guard block conditionally branches to the corresponding outgoing 512 /// block, or the next guard block in the chain. These guard blocks are returned 513 /// in the argument vector. 514 /// 515 /// Since the control flow edges from InBB to OutBB have now been replaced, the 516 /// function also updates any PHINodes in OutBB. For each such PHINode, the 517 /// operands corresponding to incoming blocks are moved to a new PHINode in the 518 /// hub, and the hub is made an operand of the original PHINode. 519 /// 520 /// Input CFG: 521 /// ---------- 522 /// 523 /// Def 524 /// | 525 /// v 526 /// In1 In2 527 /// | | 528 /// | | 529 /// v v 530 /// Foo ---> Out1 Out2 531 /// | 532 /// v 533 /// Use 534 /// 535 /// 536 /// Create hub: Incoming = {In1, In2}, Outgoing = {Out1, Out2} 537 /// ---------------------------------------------------------- 538 /// 539 /// Def 540 /// | 541 /// v 542 /// In1 In2 Foo 543 /// | Hub | | 544 /// | + - - | - - + | 545 /// | ' v ' V 546 /// +------> Guard1 -----> Out1 547 /// ' | ' 548 /// ' v ' 549 /// ' Guard2 -----> Out2 550 /// ' ' | 551 /// + - - - - - + | 552 /// v 553 /// Use 554 /// 555 /// Limitations: 556 /// ----------- 557 /// 1. This assumes that all terminators in the CFG are direct branches (the 558 /// "br" instruction). The presence of any other control flow such as 559 /// indirectbr, switch or callbr will cause an assert. 560 /// 561 /// 2. The updates to the PHINodes are not sufficient to restore SSA 562 /// form. Consider a definition Def, its use Use, incoming block In2 and 563 /// outgoing block Out2, such that: 564 /// a. In2 is reachable from D or contains D. 565 /// b. U is reachable from Out2 or is contained in Out2. 566 /// c. U is not a PHINode if U is contained in Out2. 567 /// 568 /// Clearly, Def dominates Out2 since the program is valid SSA. But when the 569 /// hub is introduced, there is a new path through the hub along which Use is 570 /// reachable from entry without passing through Def, and SSA is no longer 571 /// valid. To fix this, we need to look at all the blocks post-dominated by 572 /// the hub on the one hand, and dominated by Out2 on the other. This is left 573 /// for the caller to accomplish, since each specific use of this function 574 /// may have additional information which simplifies this fixup. For example, 575 /// see restoreSSA() in the UnifyLoopExits pass. 576 BasicBlock *CreateControlFlowHub(DomTreeUpdater *DTU, 577 SmallVectorImpl<BasicBlock *> &GuardBlocks, 578 const SetVector<BasicBlock *> &Predecessors, 579 const SetVector<BasicBlock *> &Successors, 580 const StringRef Prefix); 581 582 } // end namespace llvm 583 584 #endif // LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H 585