[llvm] [InstCombine] Simplifiy `(-x * y * -x)` into `(x * y * x)` (PR #72953)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 11 05:57:39 PST 2023


================
@@ -351,19 +351,25 @@ Instruction *InstCombinerImpl::visitMul(BinaryOperator &I) {
   // X * -Y --> -(X * Y)
   if (match(&I, m_c_Mul(m_OneUse(m_Neg(m_Value(X))), m_Value(Y))))
     return BinaryOperator::CreateNeg(Builder.CreateMul(X, Y));
-
-  // -X * Y * -X --> X * Y * X
-  if (match(&I, m_Mul(m_c_Mul(m_Value(Y), m_Neg(m_Value(X))),
-                      m_Neg(m_Deferred(X))))) {
-    if (X == Y) {
-      Value *MulXY = Builder.CreateMul(X, Y);
-      bool BO0HasNSW = cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap();
-      cast<BinaryOperator>(MulXY)->setHasNoSignedWrap(BO0HasNSW);
-      auto *Mul = BinaryOperator::CreateMul(X, MulXY);
-      Mul->setHasNoSignedWrap(HasNSW);
-      return Mul;
+  // (-X * Y) * -X --> (X * Y) * X
+  // (-X << Y) * -X --> (X << Y) * X
+  {
+    bool IsShl = false;
+    if ((match(Op0, m_c_Mul(m_Neg(m_Value(X)), m_Value(Y))) ||
+         (match(Op0, m_Shl(m_Neg(m_Value(X)), m_Value(Y))) &&
+          (IsShl = true))) &&
----------------
nikic wrote:

We should only match the one m_Neg operand here, not how the other operand looks like. That will be handled by Negator::Negate.

If you're only doing this for the sake of the nsw handling, drop the flag preservation please.

https://github.com/llvm/llvm-project/pull/72953


More information about the llvm-commits mailing list