Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 68ce932

Browse files
author
Kyle Butt
committed
[IfConversion] Use reverse_iterator to simplify. NFC
This simplifies skipping debug instructions and shrinking ranges. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@293202 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent eceaa2a commit 68ce932

File tree

1 file changed

+35
-70
lines changed

1 file changed

+35
-70
lines changed

lib/CodeGen/IfConversion.cpp

Lines changed: 35 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -588,19 +588,6 @@ bool IfConverter::ValidTriangle(BBInfo &TrueBBI, BBInfo &FalseBBI,
588588
return TExit && TExit == FalseBBI.BB;
589589
}
590590

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-
604591
/// Count duplicated instructions and move the iterators to show where they
605592
/// are.
606593
/// @param TIB True Iterator Begin
@@ -633,10 +620,8 @@ bool IfConverter::CountDuplicatedInstructions(
633620
while (TIB != TIE && FIB != FIE) {
634621
// Skip dbg_value instructions. These do not count.
635622
TIB = skipDebugInstructionsForward(TIB, TIE);
636-
if(TIB == TIE)
637-
break;
638623
FIB = skipDebugInstructionsForward(FIB, FIE);
639-
if(FIB == FIE)
624+
if (TIB == TIE || FIB == FIE)
640625
break;
641626
if (!TIB->isIdenticalTo(*FIB))
642627
break;
@@ -656,58 +641,42 @@ bool IfConverter::CountDuplicatedInstructions(
656641
if (TIB == TIE || FIB == FIE)
657642
return true;
658643
// 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());
678651

679652
if (!TBB.succ_empty() || !FBB.succ_empty()) {
680653
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;
685658
}
686659
}
687660

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-
693661
// Count duplicate instructions at the ends of the blocks.
694-
while (!TEmpty && !FEmpty) {
662+
while (RTIE != RTIB && RFIE != RFIB) {
695663
// 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)
701668
break;
702-
if (!TIE->isIdenticalTo(*FIE))
669+
if (!RTIE->isIdenticalTo(*RFIE))
703670
break;
704671
// We have to verify that any branch instructions are the same, and then we
705672
// don't count them toward the # of duplicate instructions.
706-
if (!TIE->isBranch())
673+
if (!RTIE->isBranch())
707674
++Dups2;
708-
shrinkInclusiveRange(TIB, TIE, TEmpty);
709-
shrinkInclusiveRange(FIB, FIE, FEmpty);
675+
++RTIE;
676+
++RFIE;
710677
}
678+
TIE = std::next(RTIE.getReverse());
679+
FIE = std::next(RFIE.getReverse());
711680
return true;
712681
}
713682

@@ -741,25 +710,21 @@ bool IfConverter::RescanInstructions(
741710
static void verifySameBranchInstructions(
742711
MachineBasicBlock *MBB1,
743712
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)
756721
break;
757722

758-
if (Empty1) {
723+
if (E1 == B1) {
759724
assert(!E2->isBranch() && "Branch mis-match, one block is empty.");
760725
break;
761726
}
762-
if (Empty2) {
727+
if (E2 == B2) {
763728
assert(!E1->isBranch() && "Branch mis-match, one block is empty.");
764729
break;
765730
}
@@ -769,8 +734,8 @@ static void verifySameBranchInstructions(
769734
"Branch mis-match, branch instructions don't match.");
770735
else
771736
break;
772-
shrinkInclusiveRange(B1, E1, Empty1);
773-
shrinkInclusiveRange(B2, E2, Empty2);
737+
++E1;
738+
++E2;
774739
}
775740
}
776741
#endif

0 commit comments

Comments
 (0)