1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-23 03:02:36 +01:00

[InstCombine] Simplify binary op when only one operand is a select

Summary:
SimplifySelectsFeedingBinaryOp simplified binary ops when both operands
were selects with the same condition. This patch extends it to handle
these cases where only one operand is a select:

X op (C ? P : Q) -> C ? (X op P) : (X op Q)
  // if X op P and X op Q both simplify
(C ? P : Q) op Y -> C ? (P op Y) : (Q op Y)
  // if P op Y and Q op Y both simplify

For example: X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0

Reviewers: mcberg2017, majnemer, craig.topper, qcolombet, mcrosier

Subscribers: hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D64713
This commit is contained in:
Jay Foad 2019-07-12 17:33:54 +01:00
parent 25ed2dfb26
commit f1b4e54b77
3 changed files with 14 additions and 9 deletions

View File

@ -788,6 +788,16 @@ Value *InstCombiner::SimplifySelectsFeedingBinaryOp(BinaryOperator &I,
else if (True && !False)
False = Builder.CreateBinOp(Opcode, C, F);
}
} else if (LHSIsSelect && LHS->hasOneUse()) {
// (A ? B : C) op Y -> A ? (B op Y) : (C op Y)
Cond = A;
True = SimplifyBinOp(Opcode, B, RHS, FMF, Q);
False = SimplifyBinOp(Opcode, C, RHS, FMF, Q);
} 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 (!True || !False)

View File

@ -1073,8 +1073,7 @@ define <2 x double> @negate_if_true_wrong_constant(<2 x double> %px, i1 %cond) {
; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
define float @fmul_select(float %x, i1 %c) {
; CHECK-LABEL: @fmul_select(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], float 1.000000e+00, float 0.000000e+00
; CHECK-NEXT: [[MUL:%.*]] = fmul fast float [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], float [[X:%.*]], float 0.000000e+00
; CHECK-NEXT: ret float [[MUL]]
;
%sel = select i1 %c, float 1.0, float 0.0
@ -1085,8 +1084,7 @@ define float @fmul_select(float %x, i1 %c) {
; X *fast (C ? 1.0 : 0.0) -> C ? X : 0.0
define <2 x float> @fmul_select_vec(<2 x float> %x, i1 %c) {
; CHECK-LABEL: @fmul_select_vec(
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], <2 x float> <float 1.000000e+00, float 1.000000e+00>, <2 x float> zeroinitializer
; CHECK-NEXT: [[MUL:%.*]] = fmul fast <2 x float> [[SEL]], [[X:%.*]]
; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], <2 x float> [[X:%.*]], <2 x float> zeroinitializer
; CHECK-NEXT: ret <2 x float> [[MUL]]
;
%sel = select i1 %c, <2 x float> <float 1.0, float 1.0>, <2 x float> zeroinitializer
@ -1110,8 +1108,7 @@ define float @fmul_select_strict(float %x, i1 %c) {
define double @fmul_sqrt_select(double %x, i1 %c) {
; CHECK-LABEL: @fmul_sqrt_select(
; CHECK-NEXT: [[SQR:%.*]] = call double @llvm.sqrt.f64(double [[X:%.*]])
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], double [[SQR]], double 1.000000e+00
; CHECK-NEXT: [[MUL:%.*]] = fmul fast double [[SQR]], [[SEL]]
; CHECK-NEXT: [[MUL:%.*]] = select fast i1 [[C:%.*]], double [[X]], double [[SQR]]
; CHECK-NEXT: ret double [[MUL]]
;
%sqr = call double @llvm.sqrt.f64(double %x)

View File

@ -599,9 +599,7 @@ define <2 x i8> @negate_if_true_wrong_constant(<2 x i8> %px, i1 %cond) {
; (C ? (X /exact Y) : 1) * Y -> C ? X : Y
define i32 @mul_div_select(i32 %x, i32 %y, i1 %c) {
; CHECK-LABEL: @mul_div_select(
; CHECK-NEXT: [[DIV:%.*]] = udiv exact i32 [[X:%.*]], [[Y:%.*]]
; CHECK-NEXT: [[SEL:%.*]] = select i1 [[C:%.*]], i32 [[DIV]], i32 1
; CHECK-NEXT: [[MUL:%.*]] = mul i32 [[SEL]], [[Y]]
; CHECK-NEXT: [[MUL:%.*]] = select i1 [[C:%.*]], i32 [[X:%.*]], i32 [[Y:%.*]]
; CHECK-NEXT: ret i32 [[MUL]]
;
%div = udiv exact i32 %x, %y