[llvm] [InstCombine] Simplification for (-a * b) / (a * b) and (a - b) / (b - a). (PR #71768)

via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 8 21:45:54 PST 2023


================
@@ -1544,6 +1544,20 @@ Instruction *InstCombinerImpl::visitSDiv(BinaryOperator &I) {
     }
   }
 
+  // -(X * Y) / (X * Y) --> -1
+  if ((match(Op0, m_Neg(m_c_Mul(m_Value(X), m_Value(Y)))) &&
+       match(Op1, m_c_Mul(m_Specific(X), m_Specific(Y)))) ||
+      (match(Op0, m_c_Mul(m_Value(X), m_Value(Y))) &&
+       match(Op1, m_Neg(m_c_Mul(m_Specific(X), m_Specific(Y)))))) {
----------------
goldsteinn wrote:

You could also:
1) check knownbits to see that X/Y are not INT_MIN
2) You could also handle `((-a) * b) / (a * b)` (which doesn't get canonicalized to `(-(a * b)) / (a * b)`)

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


More information about the llvm-commits mailing list