Skip to content

Commit e5bdb7a

Browse files
authored
[InstCombine] fold ldexp(x, sext(i1 y)) to fmul x, (select y, 0.5, 1.0) (#95073)
Follow up of #94887. Context: #94887 (review)
1 parent 995ba4a commit e5bdb7a

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -2619,6 +2619,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26192619
}
26202620

26212621
// ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0)
2622+
// ldexp(x, sext(i1 y)) -> fmul x, (select y, 0.5, 1.0)
26222623
Value *ExtSrc;
26232624
if (match(Exp, m_ZExt(m_Value(ExtSrc))) &&
26242625
ExtSrc->getType()->getScalarSizeInBits() == 1) {
@@ -2627,6 +2628,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
26272628
ConstantFP::get(II->getType(), 1.0));
26282629
return BinaryOperator::CreateFMulFMF(Src, Select, II);
26292630
}
2631+
if (match(Exp, m_SExt(m_Value(ExtSrc))) &&
2632+
ExtSrc->getType()->getScalarSizeInBits() == 1) {
2633+
Value *Select =
2634+
Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 0.5),
2635+
ConstantFP::get(II->getType(), 1.0));
2636+
return BinaryOperator::CreateFMulFMF(Src, Select, II);
2637+
}
26302638

26312639
break;
26322640
}

llvm/test/Transforms/InstCombine/ldexp-zext.ll renamed to llvm/test/Transforms/InstCombine/ldexp-ext.ll

+55
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,58 @@ define <2 x float> @ldexp_zext_float_vector(<2 x float> %x, <2 x i1> %bool) {
5555
%ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %zext)
5656
ret <2 x float> %ldexp
5757
}
58+
59+
define float @ldexp_sext_float(float %x, i1 %bool) {
60+
; CHECK-LABEL: @ldexp_sext_float(
61+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], float 5.000000e-01, float 1.000000e+00
62+
; CHECK-NEXT: [[LDEXP:%.*]] = fmul float [[TMP1]], [[X:%.*]]
63+
; CHECK-NEXT: ret float [[LDEXP]]
64+
;
65+
%sext = sext i1 %bool to i32
66+
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
67+
ret float %ldexp
68+
}
69+
70+
define float @ldexp_sext_float_negative(float %x, i8 %y) {
71+
; CHECK-LABEL: @ldexp_sext_float_negative(
72+
; CHECK-NEXT: [[SEXT:%.*]] = sext i8 [[Y:%.*]] to i32
73+
; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[SEXT]])
74+
; CHECK-NEXT: ret float [[LDEXP]]
75+
;
76+
%sext = sext i8 %y to i32
77+
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
78+
ret float %ldexp
79+
}
80+
81+
define double @ldexp_sext_double(double %x, i1 %bool) {
82+
; CHECK-LABEL: @ldexp_sext_double(
83+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
84+
; CHECK-NEXT: [[LDEXP:%.*]] = fmul double [[TMP1]], [[X:%.*]]
85+
; CHECK-NEXT: ret double [[LDEXP]]
86+
;
87+
%sext = sext i1 %bool to i32
88+
%ldexp = call double @llvm.ldexp.f64.i32(double %x, i32 %sext)
89+
ret double %ldexp
90+
}
91+
92+
define double @ldexp_sext_double_fast_math(double %x, i1 %bool) {
93+
; CHECK-LABEL: @ldexp_sext_double_fast_math(
94+
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
95+
; CHECK-NEXT: [[LDEXP:%.*]] = fmul reassoc double [[TMP1]], [[X:%.*]]
96+
; CHECK-NEXT: ret double [[LDEXP]]
97+
;
98+
%sext = sext i1 %bool to i32
99+
%ldexp = call reassoc double @llvm.ldexp.f64.i32(double %x, i32 %sext)
100+
ret double %ldexp
101+
}
102+
103+
define <2 x float> @ldexp_sext_float_vector(<2 x float> %x, <2 x i1> %bool) {
104+
; CHECK-LABEL: @ldexp_sext_float_vector(
105+
; CHECK-NEXT: [[TMP1:%.*]] = select <2 x i1> [[BOOL:%.*]], <2 x float> <float 5.000000e-01, float 5.000000e-01>, <2 x float> <float 1.000000e+00, float 1.000000e+00>
106+
; CHECK-NEXT: [[LDEXP:%.*]] = fmul <2 x float> [[TMP1]], [[X:%.*]]
107+
; CHECK-NEXT: ret <2 x float> [[LDEXP]]
108+
;
109+
%sext = sext <2 x i1> %bool to <2 x i32>
110+
%ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %sext)
111+
ret <2 x float> %ldexp
112+
}

0 commit comments

Comments
 (0)