@@ -655,22 +655,38 @@ static std::pair<VPBlockBase *, VPBlockBase *> cloneSESE(VPBlockBase *Entry);
655655// cloned region.
656656static std::pair<VPBlockBase *, VPBlockBase *> cloneSESE (VPBlockBase *Entry) {
657657 DenseMap<VPBlockBase *, VPBlockBase *> Old2NewVPBlocks;
658- ReversePostOrderTraversal<VPBlockShallowTraversalWrapper< VPBlockBase *>> RPOT (
659- Entry);
660- for (VPBlockBase *BB : RPOT ) {
658+ VPBlockBase *Exiting = nullptr ;
659+ // First, clone blocks reachable from Entry.
660+ for (VPBlockBase *BB : vp_depth_first_shallow (Entry) ) {
661661 VPBlockBase *NewBB = BB->clone ();
662- for (VPBlockBase *Pred : BB->getPredecessors ())
663- VPBlockUtils::connectBlocks (Old2NewVPBlocks[Pred], NewBB);
664-
665662 Old2NewVPBlocks[BB] = NewBB;
663+ if (BB->getNumSuccessors () == 0 ) {
664+ assert (!Exiting && " Multiple exiting blocks?" );
665+ Exiting = BB;
666+ }
667+ }
668+
669+ // Second, update the predecessors & successors of the cloned blocks.
670+ for (VPBlockBase *BB : vp_depth_first_shallow (Entry)) {
671+ VPBlockBase *NewBB = Old2NewVPBlocks[BB];
672+ SmallVector<VPBlockBase *> NewPreds;
673+ for (VPBlockBase *Pred : BB->getPredecessors ()) {
674+ NewPreds.push_back (Old2NewVPBlocks[Pred]);
675+ }
676+ NewBB->setPredecessors (NewPreds);
677+ SmallVector<VPBlockBase *> NewSuccs;
678+ for (VPBlockBase *Succ : BB->successors ()) {
679+ NewSuccs.push_back (Old2NewVPBlocks[Succ]);
680+ }
681+ NewBB->setSuccessors (NewSuccs);
666682 }
667683
668684#if !defined(NDEBUG)
669685 // Verify that the order of predecessors and successors matches in the cloned
670686 // version.
671- ReversePostOrderTraversal<VPBlockShallowTraversalWrapper<VPBlockBase *>>
672- NewRPOT (Old2NewVPBlocks[ Entry]);
673- for ( const auto &[OldBB, NewBB] : zip (RPOT, NewRPOT )) {
687+ for ( const auto &[OldBB, NewBB] :
688+ zip ( vp_depth_first_shallow ( Entry),
689+ vp_depth_first_shallow (Old2NewVPBlocks[Entry]) )) {
674690 for (const auto &[OldPred, NewPred] :
675691 zip (OldBB->getPredecessors (), NewBB->getPredecessors ()))
676692 assert (NewPred == Old2NewVPBlocks[OldPred] && " Different predecessors" );
@@ -681,8 +697,7 @@ static std::pair<VPBlockBase *, VPBlockBase *> cloneSESE(VPBlockBase *Entry) {
681697 }
682698#endif
683699
684- return std::make_pair (Old2NewVPBlocks[Entry],
685- Old2NewVPBlocks[*reverse (RPOT).begin ()]);
700+ return std::make_pair (Old2NewVPBlocks[Entry], Old2NewVPBlocks[Exiting]);
686701}
687702
688703VPRegionBlock *VPRegionBlock::clone () {
0 commit comments