@@ -1734,22 +1734,18 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1734
1734
}
1735
1735
1736
1736
if (Constant *C = dyn_cast<Constant>(Op0)) {
1737
- bool IsNegate = match (C, m_ZeroInt ());
1737
+ // -f(x) -> f(-x) if possible.
1738
+ if (match (C, m_Zero ()))
1739
+ if (Value *Neg = freelyNegateValue (Op1))
1740
+ return replaceInstUsesWith (I, Neg);
1741
+
1738
1742
Value *X;
1739
- if (match (Op1, m_ZExt (m_Value (X))) && X->getType ()->isIntOrIntVectorTy (1 )) {
1740
- // 0 - (zext bool) --> sext bool
1743
+ if (match (Op1, m_ZExt (m_Value (X))) && X->getType ()->isIntOrIntVectorTy (1 ))
1741
1744
// C - (zext bool) --> bool ? C - 1 : C
1742
- if (IsNegate)
1743
- return CastInst::CreateSExtOrBitCast (X, I.getType ());
1744
1745
return SelectInst::Create (X, SubOne (C), C);
1745
- }
1746
- if (match (Op1, m_SExt (m_Value (X))) && X->getType ()->isIntOrIntVectorTy (1 )) {
1747
- // 0 - (sext bool) --> zext bool
1746
+ if (match (Op1, m_SExt (m_Value (X))) && X->getType ()->isIntOrIntVectorTy (1 ))
1748
1747
// C - (sext bool) --> bool ? C + 1 : C
1749
- if (IsNegate)
1750
- return CastInst::CreateZExtOrBitCast (X, I.getType ());
1751
1748
return SelectInst::Create (X, AddOne (C), C);
1752
- }
1753
1749
1754
1750
// C - ~X == X + (1+C)
1755
1751
if (match (Op1, m_Not (m_Value (X))))
@@ -1778,51 +1774,15 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1778
1774
1779
1775
const APInt *Op0C;
1780
1776
if (match (Op0, m_APInt (Op0C))) {
1781
-
1782
- if (Op0C->isNullValue ()) {
1783
- Value *Op1Wide;
1784
- match (Op1, m_TruncOrSelf (m_Value (Op1Wide)));
1785
- bool HadTrunc = Op1Wide != Op1;
1786
- bool NoTruncOrTruncIsOneUse = !HadTrunc || Op1->hasOneUse ();
1787
- unsigned BitWidth = Op1Wide->getType ()->getScalarSizeInBits ();
1788
-
1789
- Value *X;
1790
- const APInt *ShAmt;
1791
- // -(X >>u 31) -> (X >>s 31)
1792
- if (NoTruncOrTruncIsOneUse &&
1793
- match (Op1Wide, m_LShr (m_Value (X), m_APInt (ShAmt))) &&
1794
- *ShAmt == BitWidth - 1 ) {
1795
- Value *ShAmtOp = cast<Instruction>(Op1Wide)->getOperand (1 );
1796
- Instruction *NewShift = BinaryOperator::CreateAShr (X, ShAmtOp);
1797
- NewShift->copyIRFlags (Op1Wide);
1798
- if (!HadTrunc)
1799
- return NewShift;
1800
- Builder.Insert (NewShift);
1801
- return TruncInst::CreateTruncOrBitCast (NewShift, Op1->getType ());
1802
- }
1803
- // -(X >>s 31) -> (X >>u 31)
1804
- if (NoTruncOrTruncIsOneUse &&
1805
- match (Op1Wide, m_AShr (m_Value (X), m_APInt (ShAmt))) &&
1806
- *ShAmt == BitWidth - 1 ) {
1807
- Value *ShAmtOp = cast<Instruction>(Op1Wide)->getOperand (1 );
1808
- Instruction *NewShift = BinaryOperator::CreateLShr (X, ShAmtOp);
1809
- NewShift->copyIRFlags (Op1Wide);
1810
- if (!HadTrunc)
1811
- return NewShift;
1812
- Builder.Insert (NewShift);
1813
- return TruncInst::CreateTruncOrBitCast (NewShift, Op1->getType ());
1814
- }
1815
-
1816
- if (!HadTrunc && Op1->hasOneUse ()) {
1817
- Value *LHS, *RHS;
1818
- SelectPatternFlavor SPF = matchSelectPattern (Op1, LHS, RHS).Flavor ;
1819
- if (SPF == SPF_ABS || SPF == SPF_NABS) {
1820
- // This is a negate of an ABS/NABS pattern. Just swap the operands
1821
- // of the select.
1822
- cast<SelectInst>(Op1)->swapValues ();
1823
- // Don't swap prof metadata, we didn't change the branch behavior.
1824
- return replaceInstUsesWith (I, Op1);
1825
- }
1777
+ if (Op0C->isNullValue () && Op1->hasOneUse ()) {
1778
+ Value *LHS, *RHS;
1779
+ SelectPatternFlavor SPF = matchSelectPattern (Op1, LHS, RHS).Flavor ;
1780
+ if (SPF == SPF_ABS || SPF == SPF_NABS) {
1781
+ // This is a negate of an ABS/NABS pattern. Just swap the operands
1782
+ // of the select.
1783
+ cast<SelectInst>(Op1)->swapValues ();
1784
+ // Don't swap prof metadata, we didn't change the branch behavior.
1785
+ return replaceInstUsesWith (I, Op1);
1826
1786
}
1827
1787
}
1828
1788
@@ -1957,7 +1917,7 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1957
1917
}
1958
1918
1959
1919
if (Op1->hasOneUse ()) {
1960
- Value *X = nullptr , * Y = nullptr , *Z = nullptr ;
1920
+ Value *Y = nullptr , *Z = nullptr ;
1961
1921
Constant *C = nullptr ;
1962
1922
1963
1923
// (X - (Y - Z)) --> (X + (Z - Y)).
@@ -1970,24 +1930,6 @@ Instruction *InstCombiner::visitSub(BinaryOperator &I) {
1970
1930
return BinaryOperator::CreateAnd (Op0,
1971
1931
Builder.CreateNot (Y, Y->getName () + " .not" ));
1972
1932
1973
- // 0 - (X sdiv C) -> (X sdiv -C) provided the negation doesn't overflow.
1974
- if (match (Op0, m_Zero ())) {
1975
- Constant *Op11C;
1976
- if (match (Op1, m_SDiv (m_Value (X), m_Constant (Op11C))) &&
1977
- !Op11C->containsUndefElement () && Op11C->isNotMinSignedValue () &&
1978
- Op11C->isNotOneValue ()) {
1979
- Instruction *BO =
1980
- BinaryOperator::CreateSDiv (X, ConstantExpr::getNeg (Op11C));
1981
- BO->setIsExact (cast<BinaryOperator>(Op1)->isExact ());
1982
- return BO;
1983
- }
1984
- }
1985
-
1986
- // 0 - (X << Y) -> (-X << Y) when X is freely negatable.
1987
- if (match (Op1, m_Shl (m_Value (X), m_Value (Y))) && match (Op0, m_Zero ()))
1988
- if (Value *XNeg = freelyNegateValue (X))
1989
- return BinaryOperator::CreateShl (XNeg, Y);
1990
-
1991
1933
// Subtracting -1/0 is the same as adding 1/0:
1992
1934
// sub [nsw] Op0, sext(bool Y) -> add [nsw] Op0, zext(bool Y)
1993
1935
// 'nuw' is dropped in favor of the canonical form.
0 commit comments