@@ -501,13 +501,20 @@ BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
501
501
const Twine &BBName) {
502
502
unsigned SuccNum = GetSuccessorNumber (BB, Succ);
503
503
504
- // If this is a critical edge, let SplitCriticalEdge do it.
505
504
Instruction *LatchTerm = BB->getTerminator ();
506
- if (SplitCriticalEdge (
507
- LatchTerm, SuccNum,
508
- CriticalEdgeSplittingOptions (DT, LI, MSSAU).setPreserveLCSSA (),
509
- BBName))
510
- return LatchTerm->getSuccessor (SuccNum);
505
+
506
+ CriticalEdgeSplittingOptions Options =
507
+ CriticalEdgeSplittingOptions (DT, LI, MSSAU).setPreserveLCSSA ();
508
+
509
+ if ((isCriticalEdge (LatchTerm, SuccNum, Options.MergeIdenticalEdges ))) {
510
+ // If it is a critical edge, and the succesor is an exception block, handle
511
+ // the split edge logic in this specific function
512
+ if (Succ->isEHPad ())
513
+ return ehAwareSplitEdge (BB, Succ, nullptr , nullptr , Options, BBName);
514
+
515
+ // If this is a critical edge, let SplitKnownCriticalEdge do it.
516
+ return SplitKnownCriticalEdge (LatchTerm, SuccNum, Options, BBName);
517
+ }
511
518
512
519
// If the edge isn't critical, then BB has a single successor or Succ has a
513
520
// single pred. Split the block.
@@ -527,6 +534,218 @@ BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
527
534
return SplitBlock (BB, BB->getTerminator (), DT, LI, MSSAU, BBName);
528
535
}
529
536
537
+ void llvm::setUnwindEdgeTo (Instruction *TI, BasicBlock *Succ) {
538
+ if (auto *II = dyn_cast<InvokeInst>(TI))
539
+ II->setUnwindDest (Succ);
540
+ else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
541
+ CS->setUnwindDest (Succ);
542
+ else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
543
+ CR->setUnwindDest (Succ);
544
+ else
545
+ llvm_unreachable (" unexpected terminator instruction" );
546
+ }
547
+
548
+ void llvm::updatePhiNodes (BasicBlock *DestBB, BasicBlock *OldPred,
549
+ BasicBlock *NewPred, PHINode *Until) {
550
+ int BBIdx = 0 ;
551
+ for (PHINode &PN : DestBB->phis ()) {
552
+ // We manually update the LandingPadReplacement PHINode and it is the last
553
+ // PHI Node. So, if we find it, we are done.
554
+ if (Until == &PN)
555
+ break ;
556
+
557
+ // Reuse the previous value of BBIdx if it lines up. In cases where we
558
+ // have multiple phi nodes with *lots* of predecessors, this is a speed
559
+ // win because we don't have to scan the PHI looking for TIBB. This
560
+ // happens because the BB list of PHI nodes are usually in the same
561
+ // order.
562
+ if (PN.getIncomingBlock (BBIdx) != OldPred)
563
+ BBIdx = PN.getBasicBlockIndex (OldPred);
564
+
565
+ assert (BBIdx != -1 && " Invalid PHI Index!" );
566
+ PN.setIncomingBlock (BBIdx, NewPred);
567
+ }
568
+ }
569
+
570
+ BasicBlock *llvm::ehAwareSplitEdge (BasicBlock *BB, BasicBlock *Succ,
571
+ LandingPadInst *OriginalPad,
572
+ PHINode *LandingPadReplacement,
573
+ const CriticalEdgeSplittingOptions &Options,
574
+ const Twine &BBName) {
575
+
576
+ auto *PadInst = Succ->getFirstNonPHI ();
577
+ if (!LandingPadReplacement && !PadInst->isEHPad ())
578
+ return SplitEdge (BB, Succ, Options.DT , Options.LI , Options.MSSAU , BBName);
579
+
580
+ auto *LI = Options.LI ;
581
+ SmallVector<BasicBlock *, 4 > LoopPreds;
582
+ // Check if extra modifications will be required to preserve loop-simplify
583
+ // form after splitting. If it would require splitting blocks with IndirectBr
584
+ // terminators, bail out if preserving loop-simplify form is requested.
585
+ if (Options.PreserveLoopSimplify && LI) {
586
+ if (Loop *BBLoop = LI->getLoopFor (BB)) {
587
+
588
+ // The only way that we can break LoopSimplify form by splitting a
589
+ // critical edge is when there exists some edge from BBLoop to Succ *and*
590
+ // the only edge into Succ from outside of BBLoop is that of NewBB after
591
+ // the split. If the first isn't true, then LoopSimplify still holds,
592
+ // NewBB is the new exit block and it has no non-loop predecessors. If the
593
+ // second isn't true, then Succ was not in LoopSimplify form prior to
594
+ // the split as it had a non-loop predecessor. In both of these cases,
595
+ // the predecessor must be directly in BBLoop, not in a subloop, or again
596
+ // LoopSimplify doesn't hold.
597
+ for (BasicBlock *P : predecessors (Succ)) {
598
+ if (P == BB)
599
+ continue ; // The new block is known.
600
+ if (LI->getLoopFor (P) != BBLoop) {
601
+ // Loop is not in LoopSimplify form, no need to re simplify after
602
+ // splitting edge.
603
+ LoopPreds.clear ();
604
+ break ;
605
+ }
606
+ LoopPreds.push_back (P);
607
+ }
608
+ // Loop-simplify form can be preserved, if we can split all in-loop
609
+ // predecessors.
610
+ if (any_of (LoopPreds, [](BasicBlock *Pred) {
611
+ return isa<IndirectBrInst>(Pred->getTerminator ());
612
+ })) {
613
+ return nullptr ;
614
+ }
615
+ }
616
+ }
617
+
618
+ auto *NewBB =
619
+ BasicBlock::Create (BB->getContext (), BBName, BB->getParent (), Succ);
620
+ setUnwindEdgeTo (BB->getTerminator (), NewBB);
621
+ updatePhiNodes (Succ, BB, NewBB, LandingPadReplacement);
622
+
623
+ if (LandingPadReplacement) {
624
+ auto *NewLP = OriginalPad->clone ();
625
+ auto *Terminator = BranchInst::Create (Succ, NewBB);
626
+ NewLP->insertBefore (Terminator);
627
+ LandingPadReplacement->addIncoming (NewLP, NewBB);
628
+ } else {
629
+ Value *ParentPad = nullptr ;
630
+ if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
631
+ ParentPad = FuncletPad->getParentPad ();
632
+ else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
633
+ ParentPad = CatchSwitch->getParentPad ();
634
+ else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))
635
+ ParentPad = CleanupPad->getParentPad ();
636
+ else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))
637
+ ParentPad = LandingPad->getParent ();
638
+ else
639
+ llvm_unreachable (" handling for other EHPads not implemented yet" );
640
+
641
+ auto *NewCleanupPad = CleanupPadInst::Create (ParentPad, {}, BBName, NewBB);
642
+ CleanupReturnInst::Create (NewCleanupPad, Succ, NewBB);
643
+ }
644
+
645
+ auto *DT = Options.DT ;
646
+ auto *MSSAU = Options.MSSAU ;
647
+ if (!DT && !LI)
648
+ return NewBB;
649
+
650
+ if (DT) {
651
+ DomTreeUpdater DTU (DT, DomTreeUpdater::UpdateStrategy::Lazy);
652
+ SmallVector<DominatorTree::UpdateType, 3 > Updates;
653
+
654
+ Updates.push_back ({DominatorTree::Insert, BB, NewBB});
655
+ Updates.push_back ({DominatorTree::Insert, NewBB, Succ});
656
+ Updates.push_back ({DominatorTree::Delete, BB, Succ});
657
+
658
+ DTU.applyUpdates (Updates);
659
+ DTU.flush ();
660
+
661
+ if (MSSAU) {
662
+ MSSAU->applyUpdates (Updates, *DT);
663
+ if (VerifyMemorySSA)
664
+ MSSAU->getMemorySSA ()->verifyMemorySSA ();
665
+ }
666
+ }
667
+
668
+ if (LI) {
669
+ if (Loop *BBLoop = LI->getLoopFor (BB)) {
670
+ // If one or the other blocks were not in a loop, the new block is not
671
+ // either, and thus LI doesn't need to be updated.
672
+ if (Loop *SuccLoop = LI->getLoopFor (Succ)) {
673
+ if (BBLoop == SuccLoop) {
674
+ // Both in the same loop, the NewBB joins loop.
675
+ SuccLoop->addBasicBlockToLoop (NewBB, *LI);
676
+ } else if (BBLoop->contains (SuccLoop)) {
677
+ // Edge from an outer loop to an inner loop. Add to the outer loop.
678
+ BBLoop->addBasicBlockToLoop (NewBB, *LI);
679
+ } else if (SuccLoop->contains (BBLoop)) {
680
+ // Edge from an inner loop to an outer loop. Add to the outer loop.
681
+ SuccLoop->addBasicBlockToLoop (NewBB, *LI);
682
+ } else {
683
+ // Edge from two loops with no containment relation. Because these
684
+ // are natural loops, we know that the destination block must be the
685
+ // header of its loop (adding a branch into a loop elsewhere would
686
+ // create an irreducible loop).
687
+ assert (SuccLoop->getHeader () == Succ &&
688
+ " Should not create irreducible loops!" );
689
+ if (Loop *P = SuccLoop->getParentLoop ())
690
+ P->addBasicBlockToLoop (NewBB, *LI);
691
+ }
692
+ }
693
+
694
+ // If BB is in a loop and Succ is outside of that loop, we may need to
695
+ // update LoopSimplify form and LCSSA form.
696
+ if (!BBLoop->contains (Succ)) {
697
+ assert (!BBLoop->contains (NewBB) &&
698
+ " Split point for loop exit is contained in loop!" );
699
+
700
+ // Update LCSSA form in the newly created exit block.
701
+ if (Options.PreserveLCSSA ) {
702
+ createPHIsForSplitLoopExit (BB, NewBB, Succ);
703
+ }
704
+
705
+ if (!LoopPreds.empty ()) {
706
+ BasicBlock *NewExitBB = SplitBlockPredecessors (
707
+ Succ, LoopPreds, " split" , DT, LI, MSSAU, Options.PreserveLCSSA );
708
+ if (Options.PreserveLCSSA )
709
+ createPHIsForSplitLoopExit (LoopPreds, NewExitBB, Succ);
710
+ }
711
+ }
712
+ }
713
+ }
714
+
715
+ return NewBB;
716
+ }
717
+
718
+ void llvm::createPHIsForSplitLoopExit (ArrayRef<BasicBlock *> Preds,
719
+ BasicBlock *SplitBB, BasicBlock *DestBB) {
720
+ // SplitBB shouldn't have anything non-trivial in it yet.
721
+ assert ((SplitBB->getFirstNonPHI () == SplitBB->getTerminator () ||
722
+ SplitBB->isLandingPad ()) &&
723
+ " SplitBB has non-PHI nodes!" );
724
+
725
+ // For each PHI in the destination block.
726
+ for (PHINode &PN : DestBB->phis ()) {
727
+ int Idx = PN.getBasicBlockIndex (SplitBB);
728
+ assert (Idx >= 0 && " Invalid Block Index" );
729
+ Value *V = PN.getIncomingValue (Idx);
730
+
731
+ // If the input is a PHI which already satisfies LCSSA, don't create
732
+ // a new one.
733
+ if (const PHINode *VP = dyn_cast<PHINode>(V))
734
+ if (VP->getParent () == SplitBB)
735
+ continue ;
736
+
737
+ // Otherwise a new PHI is needed. Create one and populate it.
738
+ PHINode *NewPN = PHINode::Create (
739
+ PN.getType (), Preds.size (), " split" ,
740
+ SplitBB->isLandingPad () ? &SplitBB->front () : SplitBB->getTerminator ());
741
+ for (BasicBlock *BB : Preds)
742
+ NewPN->addIncoming (V, BB);
743
+
744
+ // Update the original PHI.
745
+ PN.setIncomingValue (Idx, NewPN);
746
+ }
747
+ }
748
+
530
749
unsigned
531
750
llvm::SplitAllCriticalEdges (Function &F,
532
751
const CriticalEdgeSplittingOptions &Options) {
0 commit comments