@@ -674,6 +674,79 @@ BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
674674 return SplitBlock (BB, BB->getTerminator (), DT, LI, MSSAU, BBName);
675675}
676676
677+ // / Helper function to update the cycle or loop information after inserting a
678+ // / new block between a callbr instruction and one of its target blocks. Adds
679+ // / the new block to the innermost cycle or loop that the callbr instruction and
680+ // / the original target block share.
681+ // / \p LCI cycle or loop information to update
682+ // / \p CallBrBlock block containing the callbr instruction
683+ // / \p CallBrTarget new target block of the callbr instruction
684+ // / \p Succ original target block of the callbr instruction
685+ template <typename TI, typename T>
686+ static bool updateCycleLoopInfo (TI *LCI, BasicBlock *CallBrBlock,
687+ BasicBlock *CallBrTarget, BasicBlock *Succ) {
688+ static_assert (std::is_same_v<TI, CycleInfo> || std::is_same_v<TI, LoopInfo>,
689+ " type must be CycleInfo or LoopInfo" );
690+ if (!LCI)
691+ return false ;
692+
693+ T *LC;
694+ if constexpr (std::is_same_v<TI, CycleInfo>)
695+ LC = LCI->getSmallestCommonCycle (CallBrBlock, Succ);
696+ else
697+ LC = LCI->getSmallestCommonLoop (CallBrBlock, Succ);
698+ if (!LC)
699+ return false ;
700+
701+ if constexpr (std::is_same_v<TI, CycleInfo>)
702+ LCI->addBlockToCycle (CallBrTarget, LC);
703+ else
704+ LC->addBasicBlockToLoop (CallBrTarget, *LCI);
705+
706+ return true ;
707+ }
708+
709+ BasicBlock *llvm::SplitCallBrEdge (BasicBlock *CallBrBlock, BasicBlock *Succ,
710+ unsigned SuccIdx, DomTreeUpdater *DTU,
711+ CycleInfo *CI, LoopInfo *LI,
712+ bool *UpdatedLI) {
713+ CallBrInst *CallBr = dyn_cast<CallBrInst>(CallBrBlock->getTerminator ());
714+ assert (CallBr && " expected callbr terminator" );
715+ assert (SuccIdx < CallBr->getNumSuccessors () &&
716+ Succ == CallBr->getSuccessor (SuccIdx) && " invalid successor index" );
717+
718+ // Create a new block between callbr and the specified successor.
719+ // splitBlockBefore cannot be re-used here since it cannot split if the split
720+ // point is a PHI node (because BasicBlock::splitBasicBlockBefore cannot
721+ // handle that). But we don't need to rewire every part of a potential PHI
722+ // node. We only care about the edge between CallBrBlock and the original
723+ // successor.
724+ BasicBlock *CallBrTarget =
725+ BasicBlock::Create (CallBrBlock->getContext (),
726+ CallBrBlock->getName () + " .target." + Succ->getName (),
727+ CallBrBlock->getParent ());
728+ // Rewire control flow from the new target block to the original successor.
729+ Succ->replacePhiUsesWith (CallBrBlock, CallBrTarget);
730+ // Rewire control flow from callbr to the new target block.
731+ CallBr->setSuccessor (SuccIdx, CallBrTarget);
732+ // Jump from the new target block to the original successor.
733+ BranchInst::Create (Succ, CallBrTarget);
734+
735+ bool Updated =
736+ updateCycleLoopInfo<LoopInfo, Loop>(LI, CallBrBlock, CallBrTarget, Succ);
737+ if (UpdatedLI)
738+ *UpdatedLI = Updated;
739+ updateCycleLoopInfo<CycleInfo, Cycle>(CI, CallBrBlock, CallBrTarget, Succ);
740+ if (DTU) {
741+ DTU->applyUpdates ({{DominatorTree::Insert, CallBrBlock, CallBrTarget}});
742+ if (DTU->getDomTree ().dominates (CallBrBlock, Succ))
743+ DTU->applyUpdates ({{DominatorTree::Delete, CallBrBlock, Succ},
744+ {DominatorTree::Insert, CallBrTarget, Succ}});
745+ }
746+
747+ return CallBrTarget;
748+ }
749+
677750void llvm::setUnwindEdgeTo (Instruction *TI, BasicBlock *Succ) {
678751 if (auto *II = dyn_cast<InvokeInst>(TI))
679752 II->setUnwindDest (Succ);
0 commit comments