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;
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)
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
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
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)
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
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:%.*]]
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:%.*]]
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:%.*]]
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
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
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
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