@@ -1629,30 +1629,41 @@ class BitPermutationSelector {
1629
1629
bool &Interesting = ValueEntry->first ;
1630
1630
SmallVector<ValueBit, 64 > &Bits = ValueEntry->second ;
1631
1631
Bits.resize (NumBits);
1632
+ SDValue LHS = V.getNumOperands () > 0 ? V.getOperand (0 ) : SDValue ();
1633
+ SDValue RHS = V.getNumOperands () > 1 ? V.getOperand (1 ) : SDValue ();
1632
1634
1633
1635
switch (V.getOpcode ()) {
1634
1636
default : break ;
1635
1637
case ISD::ROTL:
1636
- if (isa<ConstantSDNode>(V. getOperand ( 1 ) )) {
1638
+ if (isa<ConstantSDNode>(RHS )) {
1637
1639
unsigned RotAmt = V.getConstantOperandVal (1 );
1638
1640
1639
- const auto &LHSBits = *getValueBits (V.getOperand (0 ), NumBits).second ;
1640
-
1641
- for (unsigned i = 0 ; i < NumBits; ++i)
1642
- Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt];
1641
+ if (LHS.hasOneUse ()) {
1642
+ const auto &LHSBits = *getValueBits (LHS, NumBits).second ;
1643
+ for (unsigned i = 0 ; i < NumBits; ++i)
1644
+ Bits[i] = LHSBits[i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt];
1645
+ } else {
1646
+ for (unsigned i = 0 ; i < NumBits; ++i)
1647
+ Bits[i] =
1648
+ ValueBit (LHS, i < RotAmt ? i + (NumBits - RotAmt) : i - RotAmt);
1649
+ }
1643
1650
1644
1651
return std::make_pair (Interesting = true , &Bits);
1645
1652
}
1646
1653
break ;
1647
1654
case ISD::SHL:
1648
1655
case PPCISD::SHL:
1649
- if (isa<ConstantSDNode>(V. getOperand ( 1 ) )) {
1656
+ if (isa<ConstantSDNode>(RHS )) {
1650
1657
unsigned ShiftAmt = V.getConstantOperandVal (1 );
1651
1658
1652
- const auto &LHSBits = *getValueBits (V.getOperand (0 ), NumBits).second ;
1653
-
1654
- for (unsigned i = ShiftAmt; i < NumBits; ++i)
1655
- Bits[i] = LHSBits[i - ShiftAmt];
1659
+ if (LHS.hasOneUse ()) {
1660
+ const auto &LHSBits = *getValueBits (LHS, NumBits).second ;
1661
+ for (unsigned i = ShiftAmt; i < NumBits; ++i)
1662
+ Bits[i] = LHSBits[i - ShiftAmt];
1663
+ } else {
1664
+ for (unsigned i = ShiftAmt; i < NumBits; ++i)
1665
+ Bits[i] = ValueBit (LHS, i - ShiftAmt);
1666
+ }
1656
1667
1657
1668
for (unsigned i = 0 ; i < ShiftAmt; ++i)
1658
1669
Bits[i] = ValueBit (ValueBit::ConstZero);
@@ -1662,13 +1673,17 @@ class BitPermutationSelector {
1662
1673
break ;
1663
1674
case ISD::SRL:
1664
1675
case PPCISD::SRL:
1665
- if (isa<ConstantSDNode>(V. getOperand ( 1 ) )) {
1676
+ if (isa<ConstantSDNode>(RHS )) {
1666
1677
unsigned ShiftAmt = V.getConstantOperandVal (1 );
1667
1678
1668
- const auto &LHSBits = *getValueBits (V.getOperand (0 ), NumBits).second ;
1669
-
1670
- for (unsigned i = 0 ; i < NumBits - ShiftAmt; ++i)
1671
- Bits[i] = LHSBits[i + ShiftAmt];
1679
+ if (LHS.hasOneUse ()) {
1680
+ const auto &LHSBits = *getValueBits (LHS, NumBits).second ;
1681
+ for (unsigned i = 0 ; i < NumBits - ShiftAmt; ++i)
1682
+ Bits[i] = LHSBits[i + ShiftAmt];
1683
+ } else {
1684
+ for (unsigned i = 0 ; i < NumBits - ShiftAmt; ++i)
1685
+ Bits[i] = ValueBit (LHS, i + ShiftAmt);
1686
+ }
1672
1687
1673
1688
for (unsigned i = NumBits - ShiftAmt; i < NumBits; ++i)
1674
1689
Bits[i] = ValueBit (ValueBit::ConstZero);
@@ -1677,23 +1692,27 @@ class BitPermutationSelector {
1677
1692
}
1678
1693
break ;
1679
1694
case ISD::AND:
1680
- if (isa<ConstantSDNode>(V. getOperand ( 1 ) )) {
1695
+ if (isa<ConstantSDNode>(RHS )) {
1681
1696
uint64_t Mask = V.getConstantOperandVal (1 );
1682
1697
1683
- const SmallVector<ValueBit, 64 > *LHSBits;
1698
+ const SmallVector<ValueBit, 64 > *LHSBits = nullptr ;
1684
1699
// Mark this as interesting, only if the LHS was also interesting. This
1685
1700
// prevents the overall procedure from matching a single immediate 'and'
1686
1701
// (which is non-optimal because such an and might be folded with other
1687
1702
// things if we don't select it here).
1688
- std::tie (Interesting, LHSBits) = getValueBits (V.getOperand (0 ), NumBits);
1703
+ if (LHS.hasOneUse ())
1704
+ std::tie (Interesting, LHSBits) = getValueBits (LHS, NumBits);
1689
1705
1690
1706
for (unsigned i = 0 ; i < NumBits; ++i)
1691
- if (((Mask >> i) & 1 ) == 1 )
1692
- Bits[i] = (*LHSBits)[i];
1693
- else {
1707
+ if (((Mask >> i) & 1 ) == 1 ) {
1708
+ if (LHS.hasOneUse ())
1709
+ Bits[i] = (*LHSBits)[i];
1710
+ else
1711
+ Bits[i] = ValueBit (LHS, i);
1712
+ } else {
1694
1713
// AND instruction masks this bit. If the input is already zero,
1695
1714
// we have nothing to do here. Otherwise, make the bit ConstZero.
1696
- if ((*LHSBits)[i].isZero ())
1715
+ if (LHS. hasOneUse () && (*LHSBits)[i].isZero ())
1697
1716
Bits[i] = (*LHSBits)[i];
1698
1717
else
1699
1718
Bits[i] = ValueBit (ValueBit::ConstZero);
@@ -1703,44 +1722,54 @@ class BitPermutationSelector {
1703
1722
}
1704
1723
break ;
1705
1724
case ISD::OR: {
1706
- const auto &LHSBits = *getValueBits (V.getOperand (0 ), NumBits).second ;
1707
- const auto &RHSBits = *getValueBits (V.getOperand (1 ), NumBits).second ;
1725
+ const auto *LHSBits =
1726
+ LHS.hasOneUse () ? getValueBits (LHS, NumBits).second : nullptr ;
1727
+ const auto *RHSBits =
1728
+ RHS.hasOneUse () ? getValueBits (RHS, NumBits).second : nullptr ;
1708
1729
1709
1730
bool AllDisjoint = true ;
1710
1731
SDValue LastVal = SDValue ();
1711
1732
unsigned LastIdx = 0 ;
1712
1733
for (unsigned i = 0 ; i < NumBits; ++i) {
1713
- if (LHSBits[i].isZero () && RHSBits[i].isZero ()) {
1734
+ if (LHSBits && RHSBits && (*LHSBits)[i].isZero () &&
1735
+ (*RHSBits)[i].isZero ()) {
1714
1736
// If both inputs are known to be zero and one is ConstZero and
1715
1737
// another is VariableKnownToBeZero, we can select whichever
1716
1738
// we like. To minimize the number of bit groups, we select
1717
1739
// VariableKnownToBeZero if this bit is the next bit of the same
1718
1740
// input variable from the previous bit. Otherwise, we select
1719
1741
// ConstZero.
1720
- if (LHSBits[i].hasValue () && LHSBits[i].getValue () == LastVal &&
1721
- LHSBits[i].getValueBitIndex () == LastIdx + 1 )
1722
- Bits[i] = LHSBits[i];
1723
- else if (RHSBits[i].hasValue () && RHSBits[i].getValue () == LastVal &&
1724
- RHSBits[i].getValueBitIndex () == LastIdx + 1 )
1725
- Bits[i] = RHSBits[i];
1742
+ const auto &LBits = *LHSBits;
1743
+ const auto &RBits = *RHSBits;
1744
+ if (LBits[i].hasValue () && LBits[i].getValue () == LastVal &&
1745
+ LBits[i].getValueBitIndex () == LastIdx + 1 )
1746
+ Bits[i] = LBits[i];
1747
+ else if (RBits[i].hasValue () && RBits[i].getValue () == LastVal &&
1748
+ RBits[i].getValueBitIndex () == LastIdx + 1 )
1749
+ Bits[i] = RBits[i];
1726
1750
else
1727
1751
Bits[i] = ValueBit (ValueBit::ConstZero);
1728
- }
1729
- else if (LHSBits[i].isZero ())
1730
- Bits[i] = RHSBits[i];
1731
- else if (RHSBits[i].isZero ())
1732
- Bits[i] = LHSBits[i];
1733
- else {
1752
+ } else if (LHSBits && (*LHSBits)[i].isZero ()) {
1753
+ if (RHSBits)
1754
+ Bits[i] = (*RHSBits)[i];
1755
+ else
1756
+ Bits[i] = ValueBit (RHS, i);
1757
+ } else if (RHSBits && (*RHSBits)[i].isZero ()) {
1758
+ if (LHSBits)
1759
+ Bits[i] = (*LHSBits)[i];
1760
+ else
1761
+ Bits[i] = ValueBit (LHS, i);
1762
+ } else {
1734
1763
AllDisjoint = false ;
1735
1764
break ;
1736
1765
}
1737
1766
// We remember the value and bit index of this bit.
1738
1767
if (Bits[i].hasValue ()) {
1739
1768
LastVal = Bits[i].getValue ();
1740
1769
LastIdx = Bits[i].getValueBitIndex ();
1741
- }
1742
- else {
1743
- if (LastVal) LastVal = SDValue ();
1770
+ } else {
1771
+ if (LastVal)
1772
+ LastVal = SDValue ();
1744
1773
LastIdx = 0 ;
1745
1774
}
1746
1775
}
@@ -1752,33 +1781,34 @@ class BitPermutationSelector {
1752
1781
}
1753
1782
case ISD::ZERO_EXTEND: {
1754
1783
// We support only the case with zero extension from i32 to i64 so far.
1755
- if (V.getValueType () != MVT::i64 ||
1756
- V.getOperand (0 ).getValueType () != MVT::i32 )
1784
+ if (V.getValueType () != MVT::i64 || LHS.getValueType () != MVT::i32 )
1757
1785
break ;
1758
1786
1759
- const SmallVector<ValueBit, 64 > *LHSBits;
1760
1787
const unsigned NumOperandBits = 32 ;
1761
- std::tie (Interesting, LHSBits) = getValueBits (V.getOperand (0 ),
1762
- NumOperandBits);
1763
-
1764
- for (unsigned i = 0 ; i < NumOperandBits; ++i)
1765
- Bits[i] = (*LHSBits)[i];
1788
+ if (LHS.hasOneUse ()) {
1789
+ const SmallVector<ValueBit, 64 > *LHSBits;
1790
+ std::tie (Interesting, LHSBits) = getValueBits (LHS, NumOperandBits);
1791
+ for (unsigned i = 0 ; i < NumOperandBits; ++i)
1792
+ Bits[i] = (*LHSBits)[i];
1793
+ } else {
1794
+ for (unsigned i = 0 ; i < NumOperandBits; ++i)
1795
+ Bits[i] = ValueBit (LHS, i);
1796
+ }
1766
1797
1767
1798
for (unsigned i = NumOperandBits; i < NumBits; ++i)
1768
1799
Bits[i] = ValueBit (ValueBit::ConstZero);
1769
1800
1770
1801
return std::make_pair (Interesting, &Bits);
1771
1802
}
1772
1803
case ISD::TRUNCATE: {
1773
- EVT FromType = V. getOperand ( 0 ) .getValueType ();
1804
+ EVT FromType = LHS .getValueType ();
1774
1805
EVT ToType = V.getValueType ();
1775
1806
// We support only the case with truncate from i64 to i32.
1776
- if (FromType != MVT::i64 || ToType != MVT::i32 )
1807
+ if (FromType != MVT::i64 || ToType != MVT::i32 || !LHS. hasOneUse () )
1777
1808
break ;
1778
1809
const unsigned NumAllBits = FromType.getSizeInBits ();
1779
1810
SmallVector<ValueBit, 64 > *InBits;
1780
- std::tie (Interesting, InBits) = getValueBits (V.getOperand (0 ),
1781
- NumAllBits);
1811
+ std::tie (Interesting, InBits) = getValueBits (LHS, NumAllBits);
1782
1812
const unsigned NumValidBits = ToType.getSizeInBits ();
1783
1813
1784
1814
// A 32-bit instruction cannot touch upper 32-bit part of 64-bit value.
@@ -1801,22 +1831,28 @@ class BitPermutationSelector {
1801
1831
// For AssertZext, we look through the operand and
1802
1832
// mark the bits known to be zero.
1803
1833
const SmallVector<ValueBit, 64 > *LHSBits;
1804
- std::tie (Interesting, LHSBits) = getValueBits (V.getOperand (0 ),
1805
- NumBits);
1806
1834
1807
- EVT FromType = cast<VTSDNode>(V. getOperand ( 1 ) )->getVT ();
1835
+ EVT FromType = cast<VTSDNode>(RHS )->getVT ();
1808
1836
const unsigned NumValidBits = FromType.getSizeInBits ();
1809
- for (unsigned i = 0 ; i < NumValidBits; ++i)
1810
- Bits[i] = (*LHSBits)[i];
1811
1837
1812
1838
// These bits are known to be zero but the AssertZext may be from a value
1813
1839
// that already has some constant zero bits (i.e. from a masking and).
1814
- for (unsigned i = NumValidBits; i < NumBits; ++i)
1815
- Bits[i] = (*LHSBits)[i].hasValue ()
1816
- ? ValueBit ((*LHSBits)[i].getValue (),
1817
- (*LHSBits)[i].getValueBitIndex (),
1818
- ValueBit::VariableKnownToBeZero)
1819
- : ValueBit (ValueBit::ConstZero);
1840
+ if (LHS.hasOneUse ()) {
1841
+ std::tie (Interesting, LHSBits) = getValueBits (LHS, NumBits);
1842
+ for (unsigned i = 0 ; i < NumValidBits; ++i)
1843
+ Bits[i] = (*LHSBits)[i];
1844
+ for (unsigned i = NumValidBits; i < NumBits; ++i)
1845
+ Bits[i] = (*LHSBits)[i].hasValue ()
1846
+ ? ValueBit ((*LHSBits)[i].getValue (),
1847
+ (*LHSBits)[i].getValueBitIndex (),
1848
+ ValueBit::VariableKnownToBeZero)
1849
+ : ValueBit (ValueBit::ConstZero);
1850
+ } else {
1851
+ for (unsigned i = 0 ; i < NumValidBits; ++i)
1852
+ Bits[i] = ValueBit (LHS, i);
1853
+ for (unsigned i = NumValidBits; i < NumBits; ++i)
1854
+ Bits[i] = ValueBit (LHS, i, ValueBit::VariableKnownToBeZero);
1855
+ }
1820
1856
1821
1857
return std::make_pair (Interesting, &Bits);
1822
1858
}
0 commit comments