Skip to content

Commit

Permalink
[InstCombine] fold ldexp(x, sext(i1 y)) to fmul x, (select y, 0.5, 1.…
Browse files Browse the repository at this point in the history
…0) (llvm#95073)

Follow up of llvm#94887.

Context:
llvm#94887 (review)
  • Loading branch information
c8ef authored and Lukacma committed Jun 12, 2024
1 parent 11f3846 commit 0c1d6e5
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
8 changes: 8 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 0c1d6e5

Please sign in to comment.