diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp index 4346a07e3a2cb..436cdbff75669 100644 --- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp +++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp @@ -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; } diff --git a/llvm/test/Transforms/InstCombine/ldexp-zext.ll b/llvm/test/Transforms/InstCombine/ldexp-ext.ll similarity index 51% rename from llvm/test/Transforms/InstCombine/ldexp-zext.ll rename to llvm/test/Transforms/InstCombine/ldexp-ext.ll index b6e4f12494059..4608553eb8874 100644 --- a/llvm/test/Transforms/InstCombine/ldexp-zext.ll +++ b/llvm/test/Transforms/InstCombine/ldexp-ext.ll @@ -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> , <2 x float> +; 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 +}