@@ -588,19 +588,6 @@ bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
588
588
return TExit && TExit == FalseBBI.BB ;
589
589
}
590
590
591
- // / Shrink the provided inclusive range by one instruction.
592
- // / If the range was one instruction (\p It == \p Begin), It is not modified,
593
- // / but \p Empty is set to true.
594
- static inline void shrinkInclusiveRange (
595
- MachineBasicBlock::iterator &Begin,
596
- MachineBasicBlock::iterator &It,
597
- bool &Empty) {
598
- if (It == Begin)
599
- Empty = true ;
600
- else
601
- It--;
602
- }
603
-
604
591
// / Count duplicated instructions and move the iterators to show where they
605
592
// / are.
606
593
// / @param TIB True Iterator Begin
@@ -633,10 +620,8 @@ bool IfConverter::CountDuplicatedInstructions(
633
620
while (TIB != TIE && FIB != FIE) {
634
621
// Skip dbg_value instructions. These do not count.
635
622
TIB = skipDebugInstructionsForward (TIB, TIE);
636
- if (TIB == TIE)
637
- break ;
638
623
FIB = skipDebugInstructionsForward (FIB, FIE);
639
- if ( FIB == FIE)
624
+ if (TIB == TIE || FIB == FIE)
640
625
break ;
641
626
if (!TIB->isIdenticalTo (*FIB))
642
627
break ;
@@ -656,58 +641,42 @@ bool IfConverter::CountDuplicatedInstructions(
656
641
if (TIB == TIE || FIB == FIE)
657
642
return true ;
658
643
// Now, in preparation for counting duplicate instructions at the ends of the
659
- // blocks, move the end iterators up past any branch instructions.
660
- --TIE;
661
- --FIE;
662
-
663
- // After this point TIB and TIE define an inclusive range, which means that
664
- // TIB == TIE is true when there is one more instruction to consider, not at
665
- // the end. Because we may not be able to go before TIB, we need a flag to
666
- // indicate a completely empty range.
667
- bool TEmpty = false , FEmpty = false ;
668
-
669
- // Upon exit TIE and FIE will both point at the last non-shared instruction.
670
- // They need to be moved forward to point past the last non-shared
671
- // instruction if the range they delimit is non-empty.
672
- auto IncrementEndIteratorsOnExit = make_scope_exit ([&]() {
673
- if (!TEmpty)
674
- ++TIE;
675
- if (!FEmpty)
676
- ++FIE;
677
- });
644
+ // blocks, switch to reverse_iterators. Note that getReverse() returns an
645
+ // iterator that points to the same instruction, unlike std::reverse_iterator.
646
+ // We have to do our own shifting so that we get the same range.
647
+ MachineBasicBlock::reverse_iterator RTIE = std::next (TIE.getReverse ());
648
+ MachineBasicBlock::reverse_iterator RFIE = std::next (FIE.getReverse ());
649
+ const MachineBasicBlock::reverse_iterator RTIB = std::next (TIB.getReverse ());
650
+ const MachineBasicBlock::reverse_iterator RFIB = std::next (FIB.getReverse ());
678
651
679
652
if (!TBB.succ_empty () || !FBB.succ_empty ()) {
680
653
if (SkipUnconditionalBranches) {
681
- while (!TEmpty && TIE ->isUnconditionalBranch ())
682
- shrinkInclusiveRange (TIB, TIE, TEmpty) ;
683
- while (!FEmpty && FIE ->isUnconditionalBranch ())
684
- shrinkInclusiveRange (FIB, FIE, FEmpty) ;
654
+ while (RTIE != RTIB && RTIE ->isUnconditionalBranch ())
655
+ ++RTIE ;
656
+ while (RFIE != RFIB && RFIE ->isUnconditionalBranch ())
657
+ ++RFIE ;
685
658
}
686
659
}
687
660
688
- // If Dups1 includes all of a block, then don't count duplicate
689
- // instructions at the end of the blocks.
690
- if (TEmpty || FEmpty)
691
- return true ;
692
-
693
661
// Count duplicate instructions at the ends of the blocks.
694
- while (!TEmpty && !FEmpty ) {
662
+ while (RTIE != RTIB && RFIE != RFIB ) {
695
663
// Skip dbg_value instructions. These do not count.
696
- TIE = skipDebugInstructionsBackward (TIE, TIB);
697
- FIE = skipDebugInstructionsBackward (FIE, FIB);
698
- TEmpty = TIE == TIB && TIE->isDebugValue ();
699
- FEmpty = FIE == FIB && FIE->isDebugValue ();
700
- if (TEmpty || FEmpty)
664
+ // Note that these are reverse iterators going forward.
665
+ RTIE = skipDebugInstructionsForward (RTIE, RTIB);
666
+ RFIE = skipDebugInstructionsForward (RFIE, RFIB);
667
+ if (RTIE == RTIB || RFIE == RFIB)
701
668
break ;
702
- if (!TIE ->isIdenticalTo (*FIE ))
669
+ if (!RTIE ->isIdenticalTo (*RFIE ))
703
670
break ;
704
671
// We have to verify that any branch instructions are the same, and then we
705
672
// don't count them toward the # of duplicate instructions.
706
- if (!TIE ->isBranch ())
673
+ if (!RTIE ->isBranch ())
707
674
++Dups2;
708
- shrinkInclusiveRange (TIB, TIE, TEmpty) ;
709
- shrinkInclusiveRange (FIB, FIE, FEmpty) ;
675
+ ++RTIE ;
676
+ ++RFIE ;
710
677
}
678
+ TIE = std::next (RTIE.getReverse ());
679
+ FIE = std::next (RFIE.getReverse ());
711
680
return true ;
712
681
}
713
682
@@ -741,25 +710,21 @@ bool IfConverter::RescanInstructions(
741
710
static void verifySameBranchInstructions (
742
711
MachineBasicBlock *MBB1,
743
712
MachineBasicBlock *MBB2) {
744
- MachineBasicBlock::iterator B1 = MBB1->begin ();
745
- MachineBasicBlock::iterator B2 = MBB2->begin ();
746
- MachineBasicBlock::iterator E1 = std::prev (MBB1->end ());
747
- MachineBasicBlock::iterator E2 = std::prev (MBB2->end ());
748
- bool Empty1 = false , Empty2 = false ;
749
- while (!Empty1 && !Empty2) {
750
- E1 = skipDebugInstructionsBackward (E1 , B1);
751
- E2 = skipDebugInstructionsBackward (E2 , B2);
752
- Empty1 = E1 == B1 && E1 ->isDebugValue ();
753
- Empty2 = E2 == B2 && E2 ->isDebugValue ();
754
-
755
- if (Empty1 && Empty2)
713
+ const MachineBasicBlock::reverse_iterator B1 = MBB1->rend ();
714
+ const MachineBasicBlock::reverse_iterator B2 = MBB2->rend ();
715
+ MachineBasicBlock::reverse_iterator E1 = MBB1->rbegin ();
716
+ MachineBasicBlock::reverse_iterator E2 = MBB2->rbegin ();
717
+ while (E1 != B1 && E2 != B2) {
718
+ skipDebugInstructionsForward (E1 , B1);
719
+ skipDebugInstructionsForward (E2 , B2);
720
+ if (E1 == B1 && E2 == B2)
756
721
break ;
757
722
758
- if (Empty1 ) {
723
+ if (E1 == B1 ) {
759
724
assert (!E2 ->isBranch () && " Branch mis-match, one block is empty." );
760
725
break ;
761
726
}
762
- if (Empty2 ) {
727
+ if (E2 == B2 ) {
763
728
assert (!E1 ->isBranch () && " Branch mis-match, one block is empty." );
764
729
break ;
765
730
}
@@ -769,8 +734,8 @@ static void verifySameBranchInstructions(
769
734
" Branch mis-match, branch instructions don't match." );
770
735
else
771
736
break ;
772
- shrinkInclusiveRange (B1, E1 , Empty1) ;
773
- shrinkInclusiveRange (B2, E2 , Empty2) ;
737
+ ++ E1 ;
738
+ ++ E2 ;
774
739
}
775
740
}
776
741
#endif
0 commit comments