[llvm] [InstCombine] Fold -X / -Y -> X / Y (PR #88422)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat Apr 13 01:14:45 PDT 2024


================
@@ -1581,10 +1581,24 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
   }
 
   // -X / Y --> -(X / Y)
-  Value *Y;
-  if (match(&I, m_SDiv(m_OneUse(m_NSWNeg(m_Value(X))), m_Value(Y))))
+  if (match(Op0, m_OneUse(m_NSWNeg(m_Value(X)))))
     return BinaryOperator::CreateNSWNeg(
-        Builder.CreateSDiv(X, Y, I.getName(), I.isExact()));
+        Builder.CreateSDiv(X, Op1, I.getName(), I.isExact()));
+
+  // X / -Y --> -(X / Y), if X is known to not be INT_MIN,
+  // or -Y is known to not be -1.
+  Value *Y;
+  KnownBits KnownDividend = computeKnownBits(Op0, 0, &I);
+  if (match(Op1, m_OneUse(m_NSWNeg(m_Value(Y))))) {
+    KnownBits KnownDivisor = computeKnownBits(Op1, 0, &I);
+    if (!KnownDividend.getSignedMinValue().isMinSignedValue() ||
+        // FIXME: Hack for checking if is -1, which is just all ones
----------------
dtcxzyw wrote:

This FIXME may be misleading. Can we improve it in the future? If not, please drop this line.


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


More information about the llvm-commits mailing list