@@ -1714,20 +1714,25 @@ bool IfConverter::IfConvertDiamondCommon(
17141714 }
17151715
17161716 // Remove the duplicated instructions at the beginnings of both paths.
1717- // Skip dbg_value instructions
1717+ // Skip dbg_value instructions.
17181718 MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr ();
17191719 MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr ();
17201720 BBI1->NonPredSize -= NumDups1;
17211721 BBI2->NonPredSize -= NumDups1;
17221722
17231723 // Skip past the dups on each side separately since there may be
1724- // differing dbg_value entries.
1724+ // differing dbg_value entries. NumDups1 can include a "return"
1725+ // instruction, if it's not marked as "branch".
17251726 for (unsigned i = 0 ; i < NumDups1; ++DI1) {
1727+ if (DI1 == MBB1.end ())
1728+ break ;
17261729 if (!DI1->isDebugValue ())
17271730 ++i;
17281731 }
17291732 while (NumDups1 != 0 ) {
17301733 ++DI2;
1734+ if (DI2 == MBB2.end ())
1735+ break ;
17311736 if (!DI2->isDebugValue ())
17321737 --NumDups1;
17331738 }
@@ -1738,11 +1743,16 @@ bool IfConverter::IfConvertDiamondCommon(
17381743 Redefs.stepForward (MI, Dummy);
17391744 }
17401745 }
1746+
17411747 BBI.BB ->splice (BBI.BB ->end (), &MBB1, MBB1.begin (), DI1);
17421748 MBB2.erase (MBB2.begin (), DI2);
17431749
1744- // The branches have been checked to match, so it is safe to remove the branch
1745- // in BB1 and rely on the copy in BB2
1750+ // The branches have been checked to match, so it is safe to remove the
1751+ // branch in BB1 and rely on the copy in BB2. The complication is that
1752+ // the blocks may end with a return instruction, which may or may not
1753+ // be marked as "branch". If it's not, then it could be included in
1754+ // "dups1", leaving the blocks potentially empty after moving the common
1755+ // duplicates.
17461756#ifndef NDEBUG
17471757 // Unanalyzable branches must match exactly. Check that now.
17481758 if (!BBI1->IsBrAnalyzable )
@@ -1768,11 +1778,14 @@ bool IfConverter::IfConvertDiamondCommon(
17681778 if (RemoveBranch)
17691779 BBI2->NonPredSize -= TII->removeBranch (*BBI2->BB );
17701780 else {
1771- do {
1772- assert (DI2 != MBB2.begin ());
1773- DI2--;
1774- } while (DI2->isBranch () || DI2->isDebugValue ());
1775- DI2++;
1781+ // Make DI2 point to the end of the range where the common "tail"
1782+ // instructions could be found.
1783+ while (DI2 != MBB2.begin ()) {
1784+ MachineBasicBlock::iterator Prev = std::prev (DI2);
1785+ if (!Prev->isBranch () && !Prev->isDebugValue ())
1786+ break ;
1787+ DI2 = Prev;
1788+ }
17761789 }
17771790 while (NumDups2 != 0 ) {
17781791 // NumDups2 only counted non-dbg_value instructions, so this won't
@@ -1833,11 +1846,15 @@ bool IfConverter::IfConvertDiamondCommon(
18331846 // a non-predicated in BBI2, then we don't want to predicate the one from
18341847 // BBI2. The reason is that if we merged these blocks, we would end up with
18351848 // two predicated terminators in the same block.
1849+ // Also, if the branches in MBB1 and MBB2 were non-analyzable, then don't
1850+ // predicate them either. They were checked to be identical, and so the
1851+ // same branch would happen regardless of which path was taken.
18361852 if (!MBB2.empty () && (DI2 == MBB2.end ())) {
18371853 MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator ();
18381854 MachineBasicBlock::iterator BBI2T = MBB2.getFirstTerminator ();
1839- if (BBI1T != MBB1.end () && TII->isPredicated (*BBI1T) &&
1840- BBI2T != MBB2.end () && !TII->isPredicated (*BBI2T))
1855+ bool BB1Predicated = BBI1T != MBB1.end () && TII->isPredicated (*BBI1T);
1856+ bool BB2NonPredicated = BBI2T != MBB2.end () && !TII->isPredicated (*BBI2T);
1857+ if (BB2NonPredicated && (BB1Predicated || !BBI2->IsBrAnalyzable ))
18411858 --DI2;
18421859 }
18431860
0 commit comments