Index: lib/Transforms/Utils/SimplifyLibCalls.cpp =================================================================== --- lib/Transforms/Utils/SimplifyLibCalls.cpp +++ lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -1347,6 +1347,27 @@ return emitUnaryFloatFnCall(Expo, TLI, LibFunc_exp10, LibFunc_exp10f, LibFunc_exp10l, B, Attrs); + // pow(C,x) -> exp2(log2(C)*x) if C > 0, C != inf, C != NaN + if (Pow->hasOneUse() && Pow->hasApproxFunc() && BaseF->isNormal() && + !BaseF->isNegative()) { + Value *Log = nullptr; + if (Ty->isFloatTy()) + Log = ConstantFP::get(Ty, std::log(BaseF->convertToFloat())); + else if (Ty->isDoubleTy()) + Log = ConstantFP::get(Ty, std::log(BaseF->convertToDouble())); + if (Log) { + Value *FMul = B.CreateFMul(Log, Expo, "logmul"); + if (Pow->doesNotAccessMemory()) { + return B.CreateCall(Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty), + FMul, "exp2"); + } else { + if (hasUnaryFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, + LibFunc_exp2l)) + return emitUnaryFloatFnCall(FMul, TLI, LibFunc_exp2, LibFunc_exp2f, + LibFunc_exp2l, B, Attrs); + } + } + } return nullptr; } Index: test/Transforms/InstCombine/pow-exp.ll =================================================================== --- test/Transforms/InstCombine/pow-exp.ll +++ test/Transforms/InstCombine/pow-exp.ll @@ -205,6 +205,228 @@ ret double %pow } +declare void @use_d(double) +declare void @use_f(float) + +define double @pow_ok_base(double %e) { +; CHECK-LABEL: @pow_ok_base( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn double [[E:%.*]], 0xBFD6D3C324E13F50 +; CHECK-NEXT: [[EXP2:%.*]] = call afn double @exp2(double [[LOGMUL]]) +; CHECK-NEXT: ret double [[EXP2]] +; + %call = tail call afn double @pow(double 0x3FE6666666666666, double %e) + ret double %call +} + +define double @pow_ok_base2(double %e) { +; CHECK-LABEL: @pow_ok_base2( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn double [[E:%.*]], 0x4006FD0F752F7F61 +; CHECK-NEXT: [[EXP2:%.*]] = call afn double @exp2(double [[LOGMUL]]) +; CHECK-NEXT: ret double [[EXP2]] +; + %call = tail call afn double @pow(double 1.770000e+01, double %e) + ret double %call +} + +define double @pow_ok_base3(double %e) { +; CHECK-LABEL: @pow_ok_base3( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn double [[E:%.*]], 0x4002801292821F4C +; CHECK-NEXT: [[EXP2:%.*]] = call afn double @exp2(double [[LOGMUL]]) +; CHECK-NEXT: ret double [[EXP2]] +; + %call = tail call afn double @pow(double 1.010000e+01, double %e) + ret double %call +} + +define double @pow_ok_ten_base(double %e) { +; CHECK-LABEL: @pow_ok_ten_base( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn double [[E:%.*]], 0x40026BB1BBB55516 +; CHECK-NEXT: [[EXP2:%.*]] = call afn double @exp2(double [[LOGMUL]]) +; CHECK-NEXT: ret double [[EXP2]] +; + %call = tail call afn double @pow(double 1.000000e+01, double %e) + ret double %call +} + +define float @powf_ok_base(float %e) { +; CHECK-LABEL: @powf_ok_base( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn float [[E:%.*]], 0xBFD6D3C340000000 +; CHECK-NEXT: [[EXP2F:%.*]] = call afn float @exp2f(float [[LOGMUL]]) +; CHECK-NEXT: ret float [[EXP2F]] +; + %call = tail call afn float @powf(float 0x3FE6666660000000, float %e) + ret float %call +} + +define float @powf_ok_base2(float %e) { +; CHECK-LABEL: @powf_ok_base2( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn float [[E:%.*]], 0x4006FD0F80000000 +; CHECK-NEXT: [[EXP2F:%.*]] = call afn float @exp2f(float [[LOGMUL]]) +; CHECK-NEXT: ret float [[EXP2F]] +; + %call = tail call afn float @powf(float 0x4031B33340000000, float %e) + ret float %call +} + +define float @powf_ok_base3(float %e) { +; CHECK-LABEL: @powf_ok_base3( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn float [[E:%.*]], 0x40028012A0000000 +; CHECK-NEXT: [[EXP2F:%.*]] = call afn float @exp2f(float [[LOGMUL]]) +; CHECK-NEXT: ret float [[EXP2F]] +; + %call = tail call afn float @powf(float 0x4024333340000000, float %e) + ret float %call +} + +define float @powf_ok_ten_base(float %e) { +; CHECK-LABEL: @powf_ok_ten_base( +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul afn float [[E:%.*]], 0x40026BB1C0000000 +; CHECK-NEXT: [[EXP2F:%.*]] = call afn float @exp2f(float [[LOGMUL]]) +; CHECK-NEXT: ret float [[EXP2F]] +; + %call = tail call afn float @powf(float 1.000000e+01, float %e) + ret float %call +} + +define double @pow_zero_base(double %e) { +; CHECK-LABEL: @pow_zero_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double 0.000000e+00, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double 0.000000e+00, double %e) + ret double %call +} + +define double @pow_zero_base2(double %e) { +; CHECK-LABEL: @pow_zero_base2( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double -0.000000e+00, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double -0.000000e+00, double %e) + ret double %call +} + +define double @pow_inf_base(double %e) { +; CHECK-LABEL: @pow_inf_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double 0x7FF0000000000000, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double 0x7FF0000000000000, double %e) + ret double %call +} + +define double @pow_nan_base(double %e) { +; CHECK-LABEL: @pow_nan_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double 0x7FF8000000000000, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double 0x7FF8000000000000, double %e) + ret double %call +} + +define double @pow_negative_base(double %e) { +; CHECK-LABEL: @pow_negative_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double -4.000000e+00, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double -4.000000e+00, double %e) + ret double %call +} + +define double @pow_multiuse(double %e) { +; CHECK-LABEL: @pow_multiuse( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn double @pow(double 5.000000e+00, double [[E:%.*]]) +; CHECK-NEXT: tail call void @use_d(double [[CALL]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call afn double @pow(double 5.000000e+00, double %e) + tail call void @use_d(double %call) + ret double %call +} + +define double @pow_ok_base_no_afn(double %e) { +; CHECK-LABEL: @pow_ok_base_no_afn( +; CHECK-NEXT: [[CALL:%.*]] = tail call double @pow(double 0x3FE6666666666666, double [[E:%.*]]) +; CHECK-NEXT: ret double [[CALL]] +; + %call = tail call double @pow(double 0x3FE6666666666666, double %e) + ret double %call +} + +define float @powf_zero_base(float %e) { +; CHECK-LABEL: @powf_zero_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float 0.000000e+00, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float 0.000000e+00, float %e) + ret float %call +} + +define float @powf_zero_base2(float %e) { +; CHECK-LABEL: @powf_zero_base2( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float -0.000000e+00, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float -0.000000e+00, float %e) + ret float %call +} + +define float @powf_inf_base(float %e) { +; CHECK-LABEL: @powf_inf_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float 0x7FF0000000000000, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float 0x7FF0000000000000, float %e) + ret float %call +} + +define float @powf_nan_base(float %e) { +; CHECK-LABEL: @powf_nan_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float 0x7FF8000000000000, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float 0x7FF8000000000000, float %e) + ret float %call +} + +define float @powf_negative_base(float %e) { +; CHECK-LABEL: @powf_negative_base( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float -4.000000e+00, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float -4.000000e+00, float %e) + ret float %call +} + +define float @powf_multiuse(float %e) { +; CHECK-LABEL: @powf_multiuse( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn float @powf(float 5.000000e+00, float [[E:%.*]]) +; CHECK-NEXT: tail call void @use_f(float [[CALL]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call afn float @powf(float 5.000000e+00, float %e) + tail call void @use_f(float %call) + ret float %call +} + +define float @powf_ok_base_no_afn(float %e) { +; CHECK-LABEL: @powf_ok_base_no_afn( +; CHECK-NEXT: [[CALL:%.*]] = tail call float @powf(float 0x3FE6666660000000, float [[E:%.*]]) +; CHECK-NEXT: ret float [[CALL]] +; + %call = tail call float @powf(float 0x3FE6666660000000, float %e) + ret float %call +} + +define fp128 @powl_long_dbl_no_fold(fp128 %e) { +; CHECK-LABEL: @powl_long_dbl_no_fold( +; CHECK-NEXT: [[CALL:%.*]] = tail call afn fp128 @powl(fp128 0xL00000000000000005001000000000000, fp128 [[E:%.*]]) +; CHECK-NEXT: ret fp128 [[CALL]] +; + %call = tail call afn fp128 @powl(fp128 0xL00000000000000005001000000000000, fp128 %e) + ret fp128 %call +} + declare float @expf(float) declare double @exp(double) declare fp128 @expl(fp128) Index: test/Transforms/InstCombine/pow_fp_int.ll =================================================================== --- test/Transforms/InstCombine/pow_fp_int.ll +++ test/Transforms/InstCombine/pow_fp_int.ll @@ -196,8 +196,9 @@ define double @pow_uitofp_const_base_fast_i32(i32 %x) { ; CHECK-LABEL: @pow_uitofp_const_base_fast_i32( ; CHECK-NEXT: [[SUBFP:%.*]] = uitofp i32 [[X:%.*]] to float -; CHECK-NEXT: [[POW:%.*]] = tail call fast float @llvm.pow.f32(float 7.000000e+00, float [[SUBFP]]) -; CHECK-NEXT: [[RES:%.*]] = fpext float [[POW]] to double +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul fast float [[SUBFP]], 0x3FFF2272A0000000 +; CHECK-NEXT: [[EXP2:%.*]] = call fast float @llvm.exp2.f32(float [[LOGMUL]]) +; CHECK-NEXT: [[RES:%.*]] = fpext float [[EXP2]] to double ; CHECK-NEXT: ret double [[RES]] ; %subfp = uitofp i32 %x to float @@ -247,8 +248,9 @@ define double @pow_sitofp_const_base_fast_i64(i64 %x) { ; CHECK-LABEL: @pow_sitofp_const_base_fast_i64( ; CHECK-NEXT: [[SUBFP:%.*]] = sitofp i64 [[X:%.*]] to float -; CHECK-NEXT: [[POW:%.*]] = tail call fast float @llvm.pow.f32(float 7.000000e+00, float [[SUBFP]]) -; CHECK-NEXT: [[RES:%.*]] = fpext float [[POW]] to double +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul fast float [[SUBFP]], 0x3FFF2272A0000000 +; CHECK-NEXT: [[EXP2:%.*]] = call fast float @llvm.exp2.f32(float [[LOGMUL]]) +; CHECK-NEXT: [[RES:%.*]] = fpext float [[EXP2]] to double ; CHECK-NEXT: ret double [[RES]] ; %subfp = sitofp i64 %x to float @@ -260,8 +262,9 @@ define double @pow_uitofp_const_base_fast_i64(i64 %x) { ; CHECK-LABEL: @pow_uitofp_const_base_fast_i64( ; CHECK-NEXT: [[SUBFP:%.*]] = uitofp i64 [[X:%.*]] to float -; CHECK-NEXT: [[POW:%.*]] = tail call fast float @llvm.pow.f32(float 7.000000e+00, float [[SUBFP]]) -; CHECK-NEXT: [[RES:%.*]] = fpext float [[POW]] to double +; CHECK-NEXT: [[LOGMUL:%.*]] = fmul fast float [[SUBFP]], 0x3FFF2272A0000000 +; CHECK-NEXT: [[EXP2:%.*]] = call fast float @llvm.exp2.f32(float [[LOGMUL]]) +; CHECK-NEXT: [[RES:%.*]] = fpext float [[EXP2]] to double ; CHECK-NEXT: ret double [[RES]] ; %subfp = uitofp i64 %x to float