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