[PATCH] D48535: [InstCombine] (A + 1) + (B ^ -1) --> A - B

Gil Rapaport via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 25 01:41:45 PDT 2018


gilr created this revision.
gilr added reviewers: spatel, craig.topper, sunfish.
Herald added a subscriber: llvm-commits.

Turn canonicalized subtraction back into (-1 - B) and combine it with (A + 1) into (A - B).
This is similar to the folding already done for (B ^ -1) + Const into (-1 + Const) - B.


Repository:
  rL LLVM

https://reviews.llvm.org/D48535

Files:
  lib/Transforms/InstCombine/InstCombineAddSub.cpp
  test/Transforms/InstCombine/add.ll


Index: test/Transforms/InstCombine/add.ll
===================================================================
--- test/Transforms/InstCombine/add.ll
+++ test/Transforms/InstCombine/add.ll
@@ -780,3 +780,28 @@
   %value = add <2 x i32> <i32 123, i32 333>, %A
   ret <2 x i32> %value
 }
+
+define i32 @test44(i32 %A, i32 %B) {
+; CHECK-LABEL: @test44(
+; CHECK-NEXT:    [[E:%.*]] = sub i32 %A, %B
+; CHECK-NEXT:    ret i32 [[E]]
+;
+  %C = xor i32 %B, -1
+  %D = add i32 %A, 1
+  ; E = (A + 1) + ~B = A - B
+  %E = add i32 %D, %C
+  ret i32 %E
+}
+
+define i32 @test45(i32 %A, i32 %B) {
+; CHECK-LABEL: @test45(
+; CHECK-NEXT:    [[E:%.*]] = sub i32 %A, %B
+; CHECK-NEXT:    ret i32 [[E]]
+;
+  %C = xor i32 -1, %B
+  %D = add i32 1, %A
+  ; E = ~B + (1 + A) = A - B
+  %E = add i32 %C, %D
+  ret i32 %E
+}
+
Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1212,6 +1212,11 @@
   if (Value *V = checkForNegativeOperand(I, Builder))
     return replaceInstUsesWith(I, V);
 
+  // (A + 1) + ~B --> A - B
+  // ~B + (A + 1) --> A - B
+  if (match(&I, m_c_BinOp(m_c_Add(m_Value(A), m_One()), m_Not(m_Value(B)))))
+    return BinaryOperator::CreateSub(A, B);
+
   // X % C0 + (( X / C0 ) % C1) * C0 => X % (C0 * C1)
   if (Value *V = SimplifyAddWithRemainder(I)) return replaceInstUsesWith(I, V);
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D48535.152611.patch
Type: text/x-patch
Size: 1477 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20180625/9ad0a292/attachment.bin>


More information about the llvm-commits mailing list