[PATCH] D58877: [InstCombine] fold add(add(A, ~B), 1) -> sub(A, B)
Amaury SECHET via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Sat Mar 2 12:17:51 PST 2019
deadalnix created this revision.
deadalnix added reviewers: lebedev.ri, RKSimon.
Herald added a project: LLVM.
As per title.
Repository:
rL LLVM
https://reviews.llvm.org/D58877
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
@@ -950,9 +950,7 @@
; E = (A + ~B) + 1 = A - B
define i32 @add_to_sub(i32 %A, i32 %B) {
; CHECK-LABEL: @add_to_sub(
-; CHECK-NEXT: [[C:%.*]] = xor i32 [[B:%.*]], -1
-; CHECK-NEXT: [[D:%.*]] = add i32 [[C]], [[A:%.*]]
-; CHECK-NEXT: [[E:%.*]] = add i32 [[D]], 1
+; CHECK-NEXT: [[E:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[E]]
;
%C = xor i32 %B, -1
@@ -964,9 +962,7 @@
; E = (~B + A) + 1 = A - B
define i32 @add_to_sub2(i32 %A, i32 %B) {
; CHECK-LABEL: @add_to_sub2(
-; CHECK-NEXT: [[C:%.*]] = xor i32 [[B:%.*]], -1
-; CHECK-NEXT: [[D:%.*]] = add i32 [[C]], [[A:%.*]]
-; CHECK-NEXT: [[E:%.*]] = add i32 [[D]], 1
+; CHECK-NEXT: [[E:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
; CHECK-NEXT: ret i32 [[E]]
;
%C = xor i32 %B, -1
Index: lib/Transforms/InstCombine/InstCombineAddSub.cpp
===================================================================
--- lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -892,6 +892,10 @@
if (!match(Op1, m_APInt(C)))
return nullptr;
+ // (X + ~Y) + 1 --> X - Y
+ if (C->isOneValue() && match(Op0, m_c_Add(m_Value(X), m_Not(m_Value(Y)))))
+ return BinaryOperator::CreateSub(X, Y);
+
if (C->isSignMask()) {
// If wrapping is not allowed, then the addition must set the sign bit:
// X + (signmask) --> X | signmask
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58877.189056.patch
Type: text/x-patch
Size: 1573 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190302/22e7339a/attachment.bin>
More information about the llvm-commits
mailing list