@@ -1714,20 +1714,25 @@ bool IfConverter::IfConvertDiamondCommon(
1714
1714
}
1715
1715
1716
1716
// Remove the duplicated instructions at the beginnings of both paths.
1717
- // Skip dbg_value instructions
1717
+ // Skip dbg_value instructions.
1718
1718
MachineBasicBlock::iterator DI1 = MBB1.getFirstNonDebugInstr ();
1719
1719
MachineBasicBlock::iterator DI2 = MBB2.getFirstNonDebugInstr ();
1720
1720
BBI1->NonPredSize -= NumDups1;
1721
1721
BBI2->NonPredSize -= NumDups1;
1722
1722
1723
1723
// 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".
1725
1726
for (unsigned i = 0 ; i < NumDups1; ++DI1) {
1727
+ if (DI1 == MBB1.end ())
1728
+ break ;
1726
1729
if (!DI1->isDebugValue ())
1727
1730
++i;
1728
1731
}
1729
1732
while (NumDups1 != 0 ) {
1730
1733
++DI2;
1734
+ if (DI2 == MBB2.end ())
1735
+ break ;
1731
1736
if (!DI2->isDebugValue ())
1732
1737
--NumDups1;
1733
1738
}
@@ -1738,11 +1743,16 @@ bool IfConverter::IfConvertDiamondCommon(
1738
1743
Redefs.stepForward (MI, Dummy);
1739
1744
}
1740
1745
}
1746
+
1741
1747
BBI.BB ->splice (BBI.BB ->end (), &MBB1, MBB1.begin (), DI1);
1742
1748
MBB2.erase (MBB2.begin (), DI2);
1743
1749
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.
1746
1756
#ifndef NDEBUG
1747
1757
// Unanalyzable branches must match exactly. Check that now.
1748
1758
if (!BBI1->IsBrAnalyzable )
@@ -1768,11 +1778,14 @@ bool IfConverter::IfConvertDiamondCommon(
1768
1778
if (RemoveBranch)
1769
1779
BBI2->NonPredSize -= TII->removeBranch (*BBI2->BB );
1770
1780
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
+ }
1776
1789
}
1777
1790
while (NumDups2 != 0 ) {
1778
1791
// NumDups2 only counted non-dbg_value instructions, so this won't
@@ -1833,11 +1846,15 @@ bool IfConverter::IfConvertDiamondCommon(
1833
1846
// a non-predicated in BBI2, then we don't want to predicate the one from
1834
1847
// BBI2. The reason is that if we merged these blocks, we would end up with
1835
1848
// 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.
1836
1852
if (!MBB2.empty () && (DI2 == MBB2.end ())) {
1837
1853
MachineBasicBlock::iterator BBI1T = MBB1.getFirstTerminator ();
1838
1854
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 ))
1841
1858
--DI2;
1842
1859
}
1843
1860
0 commit comments