[InstCombine] fold ldexp(x, sext(i1 y)) to fmul x, (select y, 0.5, 1.0) (#95073)

Follow up of #94887.

Context:
https://github.com/llvm/llvm-project/pull/94887#pullrequestreview-2106213891
This commit is contained in:
c8ef
2024-06-11 17:42:19 +08:00
committed by GitHub
parent 995ba4afcd
commit e5bdb7af86
2 changed files with 63 additions and 0 deletions

View File

@@ -2619,6 +2619,7 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
// ldexp(x, zext(i1 y)) -> fmul x, (select y, 2.0, 1.0)
// ldexp(x, sext(i1 y)) -> fmul x, (select y, 0.5, 1.0)
Value *ExtSrc;
if (match(Exp, m_ZExt(m_Value(ExtSrc))) &&
ExtSrc->getType()->getScalarSizeInBits() == 1) {
@@ -2627,6 +2628,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
ConstantFP::get(II->getType(), 1.0));
return BinaryOperator::CreateFMulFMF(Src, Select, II);
}
if (match(Exp, m_SExt(m_Value(ExtSrc))) &&
ExtSrc->getType()->getScalarSizeInBits() == 1) {
Value *Select =
Builder.CreateSelect(ExtSrc, ConstantFP::get(II->getType(), 0.5),
ConstantFP::get(II->getType(), 1.0));
return BinaryOperator::CreateFMulFMF(Src, Select, II);
}
break;
}

View File

@@ -55,3 +55,58 @@ define <2 x float> @ldexp_zext_float_vector(<2 x float> %x, <2 x i1> %bool) {
%ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %zext)
ret <2 x float> %ldexp
}
define float @ldexp_sext_float(float %x, i1 %bool) {
; CHECK-LABEL: @ldexp_sext_float(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], float 5.000000e-01, float 1.000000e+00
; CHECK-NEXT: [[LDEXP:%.*]] = fmul float [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret float [[LDEXP]]
;
%sext = sext i1 %bool to i32
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
ret float %ldexp
}
define float @ldexp_sext_float_negative(float %x, i8 %y) {
; CHECK-LABEL: @ldexp_sext_float_negative(
; CHECK-NEXT: [[SEXT:%.*]] = sext i8 [[Y:%.*]] to i32
; CHECK-NEXT: [[LDEXP:%.*]] = call float @llvm.ldexp.f32.i32(float [[X:%.*]], i32 [[SEXT]])
; CHECK-NEXT: ret float [[LDEXP]]
;
%sext = sext i8 %y to i32
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %sext)
ret float %ldexp
}
define double @ldexp_sext_double(double %x, i1 %bool) {
; CHECK-LABEL: @ldexp_sext_double(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
; CHECK-NEXT: [[LDEXP:%.*]] = fmul double [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret double [[LDEXP]]
;
%sext = sext i1 %bool to i32
%ldexp = call double @llvm.ldexp.f64.i32(double %x, i32 %sext)
ret double %ldexp
}
define double @ldexp_sext_double_fast_math(double %x, i1 %bool) {
; CHECK-LABEL: @ldexp_sext_double_fast_math(
; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[BOOL:%.*]], double 5.000000e-01, double 1.000000e+00
; CHECK-NEXT: [[LDEXP:%.*]] = fmul reassoc double [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret double [[LDEXP]]
;
%sext = sext i1 %bool to i32
%ldexp = call reassoc double @llvm.ldexp.f64.i32(double %x, i32 %sext)
ret double %ldexp
}
define <2 x float> @ldexp_sext_float_vector(<2 x float> %x, <2 x i1> %bool) {
; CHECK-LABEL: @ldexp_sext_float_vector(
; 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>
; CHECK-NEXT: [[LDEXP:%.*]] = fmul <2 x float> [[TMP1]], [[X:%.*]]
; CHECK-NEXT: ret <2 x float> [[LDEXP]]
;
%sext = sext <2 x i1> %bool to <2 x i32>
%ldexp = call <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float> %x, <2 x i32> %sext)
ret <2 x float> %ldexp
}