[llvm] 7c57180 - [InstCombine] fold add+negate through select into sub
Sanjay Patel via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 7 05:23:52 PDT 2022
Author: Sanjay Patel
Date: 2022-09-07T08:23:35-04:00
New Revision: 7c57180900fb6d703c71351e67eaea0f1a25d2e1
URL: https://github.com/llvm/llvm-project/commit/7c57180900fb6d703c71351e67eaea0f1a25d2e1
DIFF: https://github.com/llvm/llvm-project/commit/7c57180900fb6d703c71351e67eaea0f1a25d2e1.diff
LOG: [InstCombine] fold add+negate through select into sub
This transform came up as a potential DAGCombine in D133282,
so I wanted to see how it escaped in IR too.
We do general folds in InstCombiner::SimplifySelectsFeedingBinaryOp()
by checking if either arm of a select simplifies when the trailing
binop is threaded into the select.
So as long as one side simplifies, it's a good fold to combine a
negate and add into 1 subtract.
This is an example with a zero arm in the select:
https://alive2.llvm.org/ce/z/Hgu_Tj
And this models the tests with a cancelling 'not' op:
https://alive2.llvm.org/ce/z/BuzVV_
Differential Revision: https://reviews.llvm.org/D133369
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/add.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 8dc89b57f51fa..dc6943dab645b 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -872,6 +872,28 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
SimplifyQuery Q = SQ.getWithInstruction(&I);
Value *Cond, *True = nullptr, *False = nullptr;
+
+ // Special-case for add/negate combination. Replace the zero in the negation
+ // with the trailing add operand:
+ // (Cond ? TVal : -N) + Z --> Cond ? True : (Z - N)
+ // (Cond ? -N : FVal) + Z --> Cond ? (Z - N) : False
+ auto foldAddNegate = [&](Value *TVal, Value *FVal, Value *Z) -> Value * {
+ // We need an 'add' and exactly 1 arm of the select to have been simplified.
+ if (Opcode != Instruction::Add || (!True && !False) || (True && False))
+ return nullptr;
+
+ Value *N;
+ if (True && match(FVal, m_Neg(m_Value(N)))) {
+ Value *Sub = Builder.CreateSub(Z, N);
+ return Builder.CreateSelect(Cond, True, Sub, I.getName());
+ }
+ if (False && match(TVal, m_Neg(m_Value(N)))) {
+ Value *Sub = Builder.CreateSub(Z, N);
+ return Builder.CreateSelect(Cond, Sub, False, I.getName());
+ }
+ return nullptr;
+ };
+
if (LHSIsSelect && RHSIsSelect && A == D) {
// (A ? B : C) op (A ? E : F) -> A ? (B op E) : (C op F)
Cond = A;
@@ -889,11 +911,15 @@ Value *InstCombinerImpl::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
Cond = A;
True = simplifyBinOp(Opcode, B, RHS, FMF, Q);
False = simplifyBinOp(Opcode, C, RHS, FMF, Q);
+ if (Value *NewSel = foldAddNegate(B, C, RHS))
+ return NewSel;
} else if (RHSIsSelect && RHS->hasOneUse()) {
// X op (D ? E : F) -> D ? (X op E) : (X op F)
Cond = D;
True = simplifyBinOp(Opcode, LHS, E, FMF, Q);
False = simplifyBinOp(Opcode, LHS, F, FMF, Q);
+ if (Value *NewSel = foldAddNegate(E, F, LHS))
+ return NewSel;
}
if (!True || !False)
diff --git a/llvm/test/Transforms/InstCombine/add.ll b/llvm/test/Transforms/InstCombine/add.ll
index 02c46eced9eb1..68c7a24c5f513 100644
--- a/llvm/test/Transforms/InstCombine/add.ll
+++ b/llvm/test/Transforms/InstCombine/add.ll
@@ -1963,10 +1963,9 @@ define i8 @full_ashr_not_inc(i8 %x) {
define i8 @select_negate_or_zero(i1 %b, i8 %x, i8 %y) {
; CHECK-LABEL: @select_negate_or_zero(
-; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[NEGX]]
-; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]]
-; CHECK-NEXT: ret i8 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 0, i8 [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y:%.*]], [[TMP1]]
+; CHECK-NEXT: ret i8 [[ADD1]]
;
%negx = sub i8 0, %x
%sel = select i1 %b, i8 0, i8 %negx
@@ -1974,13 +1973,14 @@ define i8 @select_negate_or_zero(i1 %b, i8 %x, i8 %y) {
ret i8 %add
}
+; commuted add operands - same result
+
define <2 x i8> @select_negate_or_zero_commute(<2 x i1> %b, <2 x i8> %x, <2 x i8> %p) {
; CHECK-LABEL: @select_negate_or_zero_commute(
; CHECK-NEXT: [[Y:%.*]] = mul <2 x i8> [[P:%.*]], [[P]]
-; CHECK-NEXT: [[NEGX:%.*]] = sub <2 x i8> <i8 poison, i8 0>, [[X:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select <2 x i1> [[B:%.*]], <2 x i8> <i8 poison, i8 0>, <2 x i8> [[NEGX]]
-; CHECK-NEXT: [[ADD:%.*]] = add <2 x i8> [[Y]], [[SEL]]
-; CHECK-NEXT: ret <2 x i8> [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[B:%.*]], <2 x i8> zeroinitializer, <2 x i8> [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = sub <2 x i8> [[Y]], [[TMP1]]
+; CHECK-NEXT: ret <2 x i8> [[ADD1]]
;
%y = mul <2 x i8> %p, %p ; thwart complexity-based canonicalization
%negx = sub <2 x i8> <i8 poison, i8 0>, %x
@@ -1989,13 +1989,15 @@ define <2 x i8> @select_negate_or_zero_commute(<2 x i1> %b, <2 x i8> %x, <2 x i8
ret <2 x i8> %add
}
+; swapped select operands and extra use are ok
+
define i8 @select_negate_or_zero_swap(i1 %b, i8 %x, i8 %y) {
; CHECK-LABEL: @select_negate_or_zero_swap(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
; CHECK-NEXT: call void @use(i8 [[NEGX]])
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 [[NEGX]], i8 0
-; CHECK-NEXT: [[ADD:%.*]] = add i8 [[SEL]], [[Y:%.*]]
-; CHECK-NEXT: ret i8 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 [[X]], i8 0
+; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y:%.*]], [[TMP1]]
+; CHECK-NEXT: ret i8 [[ADD1]]
;
%negx = sub i8 0, %x
call void @use(i8 %negx)
@@ -2004,13 +2006,14 @@ define i8 @select_negate_or_zero_swap(i1 %b, i8 %x, i8 %y) {
ret i8 %add
}
+; commuted add operands - same result
+
define i8 @select_negate_or_zero_swap_commute(i1 %b, i8 %x, i8 %p) {
; CHECK-LABEL: @select_negate_or_zero_swap_commute(
; CHECK-NEXT: [[Y:%.*]] = mul i8 [[P:%.*]], [[P]]
-; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i8 [[NEGX]], i8 0
-; CHECK-NEXT: [[ADD:%.*]] = add i8 [[Y]], [[SEL]]
-; CHECK-NEXT: ret i8 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[B:%.*]], i8 [[X:%.*]], i8 0
+; CHECK-NEXT: [[ADD1:%.*]] = sub i8 [[Y]], [[TMP1]]
+; CHECK-NEXT: ret i8 [[ADD1]]
;
%y = mul i8 %p, %p ; thwart complexity-based canonicalization
%negx = sub i8 0, %x
@@ -2019,6 +2022,8 @@ define i8 @select_negate_or_zero_swap_commute(i1 %b, i8 %x, i8 %p) {
ret i8 %add
}
+; negative test - one arm of the select must simplify
+
define i8 @select_negate_or_nonzero(i1 %b, i8 %x, i8 %y) {
; CHECK-LABEL: @select_negate_or_nonzero(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -2032,6 +2037,8 @@ define i8 @select_negate_or_nonzero(i1 %b, i8 %x, i8 %y) {
ret i8 %add
}
+; negative test - must have a negate, not any subtract
+
define i8 @select_nonnegate_or_zero(i1 %b, i8 %x, i8 %y) {
; CHECK-LABEL: @select_nonnegate_or_zero(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 42, [[X:%.*]]
@@ -2045,6 +2052,8 @@ define i8 @select_nonnegate_or_zero(i1 %b, i8 %x, i8 %y) {
ret i8 %add
}
+; negative test - don't create an extra instruction
+
define i8 @select_negate_or_nonzero_use(i1 %b, i8 %x, i8 %y) {
; CHECK-LABEL: @select_negate_or_nonzero_use(
; CHECK-NEXT: [[NEGX:%.*]] = sub i8 0, [[X:%.*]]
@@ -2060,13 +2069,13 @@ define i8 @select_negate_or_nonzero_use(i1 %b, i8 %x, i8 %y) {
ret i8 %add
}
+; extra reduction because y + ~y -> -1
+
define i5 @select_negate_not(i1 %b, i5 %x, i5 %y) {
; CHECK-LABEL: @select_negate_not(
-; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]]
-; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y:%.*]], -1
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NOTY]], i5 [[NEGX]]
-; CHECK-NEXT: [[ADD:%.*]] = add i5 [[SEL]], [[Y]]
-; CHECK-NEXT: ret i5 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 -1, i5 [[TMP1]]
+; CHECK-NEXT: ret i5 [[ADD1]]
;
%negx = sub i5 0, %x
%noty = xor i5 %y, -1
@@ -2078,11 +2087,9 @@ define i5 @select_negate_not(i1 %b, i5 %x, i5 %y) {
define i5 @select_negate_not_commute(i1 %b, i5 %x, i5 %p) {
; CHECK-LABEL: @select_negate_not_commute(
; CHECK-NEXT: [[Y:%.*]] = mul i5 [[P:%.*]], [[P]]
-; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]]
-; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y]], -1
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NOTY]], i5 [[NEGX]]
-; CHECK-NEXT: [[ADD:%.*]] = add i5 [[Y]], [[SEL]]
-; CHECK-NEXT: ret i5 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y]], [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 -1, i5 [[TMP1]]
+; CHECK-NEXT: ret i5 [[ADD1]]
;
%y = mul i5 %p, %p ; thwart complexity-based canonicalization
%negx = sub i5 0, %x
@@ -2094,11 +2101,9 @@ define i5 @select_negate_not_commute(i1 %b, i5 %x, i5 %p) {
define i5 @select_negate_not_swap(i1 %b, i5 %x, i5 %y) {
; CHECK-LABEL: @select_negate_not_swap(
-; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]]
-; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y:%.*]], -1
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NEGX]], i5 [[NOTY]]
-; CHECK-NEXT: [[ADD:%.*]] = add i5 [[SEL]], [[Y]]
-; CHECK-NEXT: ret i5 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y:%.*]], [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 [[TMP1]], i5 -1
+; CHECK-NEXT: ret i5 [[ADD1]]
;
%negx = sub i5 0, %x
%noty = xor i5 %y, -1
@@ -2110,11 +2115,9 @@ define i5 @select_negate_not_swap(i1 %b, i5 %x, i5 %y) {
define i5 @select_negate_not_swap_commute(i1 %b, i5 %x, i5 %p) {
; CHECK-LABEL: @select_negate_not_swap_commute(
; CHECK-NEXT: [[Y:%.*]] = mul i5 [[P:%.*]], [[P]]
-; CHECK-NEXT: [[NEGX:%.*]] = sub i5 0, [[X:%.*]]
-; CHECK-NEXT: [[NOTY:%.*]] = xor i5 [[Y]], -1
-; CHECK-NEXT: [[SEL:%.*]] = select i1 [[B:%.*]], i5 [[NEGX]], i5 [[NOTY]]
-; CHECK-NEXT: [[ADD:%.*]] = add i5 [[Y]], [[SEL]]
-; CHECK-NEXT: ret i5 [[ADD]]
+; CHECK-NEXT: [[TMP1:%.*]] = sub i5 [[Y]], [[X:%.*]]
+; CHECK-NEXT: [[ADD1:%.*]] = select i1 [[B:%.*]], i5 [[TMP1]], i5 -1
+; CHECK-NEXT: ret i5 [[ADD1]]
;
%y = mul i5 %p, %p ; thwart complexity-based canonicalization
%negx = sub i5 0, %x
More information about the llvm-commits
mailing list