[llvm] 31d219d - [InstCombine] Fold `((X - Y) - Z)` to `X - (Y + Z)` (PR49858)
Roman Lebedev via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 6 05:59:31 PDT 2021
Author: Roman Lebedev
Date: 2021-04-06T15:58:14+03:00
New Revision: 31d219d2997fed1b7dc97e0adf170d5aaf65883e
URL: https://github.com/llvm/llvm-project/commit/31d219d2997fed1b7dc97e0adf170d5aaf65883e
DIFF: https://github.com/llvm/llvm-project/commit/31d219d2997fed1b7dc97e0adf170d5aaf65883e.diff
LOG: [InstCombine] Fold `((X - Y) - Z)` to `X - (Y + Z)` (PR49858)
https://alive2.llvm.org/ce/z/67w-wQ
We prefer `add`s over `sub`, and this particular xform
allows further folds to happen:
Fixes https://bugs.llvm.org/show_bug.cgi?id=49858
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
llvm/test/Transforms/InstCombine/abs-1.ll
llvm/test/Transforms/InstCombine/sub-from-sub.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index bacb8689892a9..4ced5994ffa60 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -1801,6 +1801,12 @@ Instruction *InstCombinerImpl::visitSub(BinaryOperator &I) {
return BinaryOperator::CreateSub(XZ, YW);
}
+ // ((X - Y) - Op1) --> X - (Y + Op1)
+ if (match(Op0, m_OneUse(m_Sub(m_Value(X), m_Value(Y))))) {
+ Value *Add = Builder.CreateAdd(Y, Op1);
+ return BinaryOperator::CreateSub(X, Add);
+ }
+
auto m_AddRdx = [](Value *&Vec) {
return m_OneUse(m_Intrinsic<Intrinsic::vector_reduce_add>(m_Value(Vec)));
};
diff --git a/llvm/test/Transforms/InstCombine/abs-1.ll b/llvm/test/Transforms/InstCombine/abs-1.ll
index 7452798ead77a..ea77dfb5fef4d 100644
--- a/llvm/test/Transforms/InstCombine/abs-1.ll
+++ b/llvm/test/Transforms/InstCombine/abs-1.ll
@@ -305,9 +305,9 @@ define i32 @nabs_canonical_8(i32 %a) {
define i32 @nabs_canonical_9(i32 %a, i32 %b) {
; CHECK-LABEL: @nabs_canonical_9(
; CHECK-NEXT: [[T1:%.*]] = sub i32 [[A:%.*]], [[B:%.*]]
-; CHECK-NEXT: [[T2:%.*]] = sub i32 [[B]], [[A]]
; CHECK-NEXT: [[TMP1:%.*]] = call i32 @llvm.abs.i32(i32 [[T1]], i1 false)
-; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[T2]], [[TMP1]]
+; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[TMP1]], [[A]]
+; CHECK-NEXT: [[ADD:%.*]] = sub i32 [[B]], [[TMP2]]
; CHECK-NEXT: ret i32 [[ADD]]
;
%t1 = sub i32 %a, %b
diff --git a/llvm/test/Transforms/InstCombine/sub-from-sub.ll b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
index 202dbd1072a4d..aaa8b1db32de6 100644
--- a/llvm/test/Transforms/InstCombine/sub-from-sub.ll
+++ b/llvm/test/Transforms/InstCombine/sub-from-sub.ll
@@ -8,8 +8,8 @@ declare void @use8(i8)
; Basic test
define i8 @t0(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @t0(
-; CHECK-NEXT: [[I0:%.*]] = sub i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%i0 = sub i8 %x, %y
@@ -20,8 +20,8 @@ define i8 @t0(i8 %x, i8 %y, i8 %z) {
; No flags are propagated
define i8 @t1_flags(i8 %x, i8 %y, i8 %z) {
; CHECK-LABEL: @t1_flags(
-; CHECK-NEXT: [[O0:%.*]] = sub nuw nsw i8 [[X:%.*]], [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sub nuw nsw i8 [[O0]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sub i8 [[X:%.*]], [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%o0 = sub nuw nsw i8 %x, %y
@@ -47,8 +47,8 @@ define i8 @n2(i8 %x, i8 %y, i8 %z) {
define i8 @t3_c0(i8 %y, i8 %z) {
; CHECK-LABEL: @t3_c0(
-; CHECK-NEXT: [[I0:%.*]] = sub i8 42, [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = sub i8 [[I0]], [[Z:%.*]]
+; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[Y:%.*]], [[Z:%.*]]
+; CHECK-NEXT: [[R:%.*]] = sub i8 42, [[TMP1]]
; CHECK-NEXT: ret i8 [[R]]
;
%i0 = sub i8 42, %y
More information about the llvm-commits
mailing list