[llvm] [InstCombine] Improve handling of `not` and free inversion. (PR #66787)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 19 12:05:47 PST 2023


================
@@ -2205,12 +2227,23 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
   // (~X) - (~Y) --> Y - X
   // This is placed after the other reassociations and explicitly excludes a
   // sub-of-sub pattern to avoid infinite looping.
-  if (isFreeToInvert(Op0, Op0->hasOneUse()) &&
-      isFreeToInvert(Op1, Op1->hasOneUse()) &&
-      !match(Op0, m_Sub(m_ImmConstant(), m_Value()))) {
-    Value *NotOp0 = Builder.CreateNot(Op0);
-    Value *NotOp1 = Builder.CreateNot(Op1);
-    return BinaryOperator::CreateSub(NotOp1, NotOp0);
+  {
+    auto m_AddSubInvertable = m_CombineOr(m_Add(m_Value(), m_ImmConstant()),
+                                          m_Sub(m_ImmConstant(), m_Value()));
----------------
goldsteinn wrote:

The `sub` is unnecessary (will drop). But we rely on this fold for simplifying things like `(A + C0) - (B + C1)`

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


More information about the llvm-commits mailing list