From dc18ebc4b12d64ada969a5879cff162933b029ba Mon Sep 17 00:00:00 2001 From: Sanjay Patel Date: Wed, 22 Oct 2014 15:29:23 +0000 Subject: [PATCH] Shrinkify libcalls: use float versions of double libm functions with fast-math (bug 17850) When a call to a double-precision libm function has fast-math semantics (via function attribute for now because there is no IR-level FMF on calls), we can avoid fpext/fptrunc operations and use the float version of the call if the input and output are both float. We already do this optimization using a command-line option; this patch just adds the ability for fast-math to use the existing functionality. I moved the cl::opt from InstructionCombining into SimplifyLibCalls because it's only ever used internally to that class. Modified the existing test cases to use the unsafe-fp-math attribute rather than repeating all tests. This patch should solve: http://llvm.org/bugs/show_bug.cgi?id=17850 Differential Revision: http://reviews.llvm.org/D5893 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@220390 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../llvm/Transforms/Utils/SimplifyLibCalls.h | 3 +- .../InstCombine/InstructionCombining.cpp | 8 +- lib/Transforms/Utils/SimplifyLibCalls.cpp | 26 ++- .../InstCombine/double-float-shrink-1.ll | 208 +++++++++--------- 4 files changed, 131 insertions(+), 114 deletions(-) diff --git a/include/llvm/Transforms/Utils/SimplifyLibCalls.h b/include/llvm/Transforms/Utils/SimplifyLibCalls.h index 7087f2597ef..6765ac1e9d1 100644 --- a/include/llvm/Transforms/Utils/SimplifyLibCalls.h +++ b/include/llvm/Transforms/Utils/SimplifyLibCalls.h @@ -40,8 +40,7 @@ protected: ~LibCallSimplifier() {} public: - LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI, - bool UnsafeFPShrink); + LibCallSimplifier(const DataLayout *TD, const TargetLibraryInfo *TLI); /// optimizeCall - Take the given call instruction and return a more /// optimal value to replace the instruction with or 0 if a more diff --git a/lib/Transforms/InstCombine/InstructionCombining.cpp b/lib/Transforms/InstCombine/InstructionCombining.cpp index 8d74976cb18..c32294f51fb 100644 --- a/lib/Transforms/InstCombine/InstructionCombining.cpp +++ b/lib/Transforms/InstCombine/InstructionCombining.cpp @@ -70,12 +70,6 @@ STATISTIC(NumExpand, "Number of expansions"); STATISTIC(NumFactor , "Number of factorizations"); STATISTIC(NumReassoc , "Number of reassociations"); -static cl::opt - EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden, - cl::init(false), - cl::desc("Enable unsafe double to float " - "shrinking for math lib calls")); - // Initialization Routines void llvm::initializeInstCombine(PassRegistry &Registry) { initializeInstCombinerPass(Registry); @@ -2945,7 +2939,7 @@ public: InstCombinerLibCallSimplifier(const DataLayout *DL, const TargetLibraryInfo *TLI, InstCombiner *IC) - : LibCallSimplifier(DL, TLI, EnableUnsafeFPShrink) { + : LibCallSimplifier(DL, TLI) { this->IC = IC; } diff --git a/lib/Transforms/Utils/SimplifyLibCalls.cpp b/lib/Transforms/Utils/SimplifyLibCalls.cpp index c3e2f3aec00..caae06c4211 100644 --- a/lib/Transforms/Utils/SimplifyLibCalls.cpp +++ b/lib/Transforms/Utils/SimplifyLibCalls.cpp @@ -40,6 +40,13 @@ static cl::opt ColdErrorCalls("error-reporting-is-cold", cl::init(true), cl::Hidden, cl::desc("Treat error-reporting calls as cold")); +static cl::opt + EnableUnsafeFPShrink("enable-double-float-shrink", cl::Hidden, + cl::init(false), + cl::desc("Enable unsafe double to float " + "shrinking for math lib calls")); + + //===----------------------------------------------------------------------===// // Helper Functions //===----------------------------------------------------------------------===// @@ -1989,6 +1996,20 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { IRBuilder<> Builder(CI); bool isCallingConvC = CI->getCallingConv() == llvm::CallingConv::C; + // Command-line parameter overrides function attribute. + if (EnableUnsafeFPShrink.getNumOccurrences() > 0) + UnsafeFPShrink = EnableUnsafeFPShrink; + else if (Callee->hasFnAttribute("unsafe-fp-math")) { + // FIXME: This is the same problem as described in optimizeSqrt(). + // If calls gain access to IR-level FMF, then use that instead of a + // function attribute. + + // Check for unsafe-fp-math = true. + Attribute Attr = Callee->getFnAttribute("unsafe-fp-math"); + if (Attr.getValueAsString() == "true") + UnsafeFPShrink = true; + } + // Next check for intrinsics. if (IntrinsicInst *II = dyn_cast(CI)) { if (!isCallingConvC) @@ -2182,11 +2203,10 @@ Value *LibCallSimplifier::optimizeCall(CallInst *CI) { } LibCallSimplifier::LibCallSimplifier(const DataLayout *DL, - const TargetLibraryInfo *TLI, - bool UnsafeFPShrink) : + const TargetLibraryInfo *TLI) : DL(DL), TLI(TLI), - UnsafeFPShrink(UnsafeFPShrink) { + UnsafeFPShrink(false) { } void LibCallSimplifier::replaceAllUsesWith(Instruction *I, Value *With) const { diff --git a/test/Transforms/InstCombine/double-float-shrink-1.ll b/test/Transforms/InstCombine/double-float-shrink-1.ll index d958470f1ba..9b422b77296 100644 --- a/test/Transforms/InstCombine/double-float-shrink-1.ll +++ b/test/Transforms/InstCombine/double-float-shrink-1.ll @@ -1,349 +1,353 @@ -; RUN: opt < %s -instcombine -enable-double-float-shrink -S | FileCheck %s +; RUN: opt < %s -instcombine -S | FileCheck %s target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu" -define float @acos_test(float %f) nounwind readnone { -; CHECK: acos_test +; Check for and against shrinkage when using the +; unsafe-fp-math function attribute on a math lib +; function. This optimization may be overridden by +; the -enable-double-float-shrink option. +; PR17850: http://llvm.org/bugs/show_bug.cgi?id=17850 + +define float @acos_test(float %f) { %conv = fpext float %f to double %call = call double @acos(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: acos_test ; CHECK: call float @acosf(float %f) } -define double @acos_test2(float %f) nounwind readnone { -; CHECK: acos_test2 +define double @acos_test2(float %f) { %conv = fpext float %f to double %call = call double @acos(double %conv) ret double %call +; CHECK-LABEL: acos_test2 ; CHECK: call double @acos(double %conv) } -define float @acosh_test(float %f) nounwind readnone { -; CHECK: acosh_test +define float @acosh_test(float %f) { %conv = fpext float %f to double %call = call double @acosh(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: acosh_test ; CHECK: call float @acoshf(float %f) } -define double @acosh_test2(float %f) nounwind readnone { -; CHECK: acosh_test2 +define double @acosh_test2(float %f) { %conv = fpext float %f to double %call = call double @acosh(double %conv) ret double %call +; CHECK-LABEL: acosh_test2 ; CHECK: call double @acosh(double %conv) } -define float @asin_test(float %f) nounwind readnone { -; CHECK: asin_test +define float @asin_test(float %f) { %conv = fpext float %f to double %call = call double @asin(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: asin_test ; CHECK: call float @asinf(float %f) } -define double @asin_test2(float %f) nounwind readnone { -; CHECK: asin_test2 +define double @asin_test2(float %f) { %conv = fpext float %f to double %call = call double @asin(double %conv) ret double %call +; CHECK-LABEL: asin_test2 ; CHECK: call double @asin(double %conv) } -define float @asinh_test(float %f) nounwind readnone { -; CHECK: asinh_test +define float @asinh_test(float %f) { %conv = fpext float %f to double %call = call double @asinh(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: asinh_test ; CHECK: call float @asinhf(float %f) } -define double @asinh_test2(float %f) nounwind readnone { -; CHECK: asinh_test2 +define double @asinh_test2(float %f) { %conv = fpext float %f to double %call = call double @asinh(double %conv) ret double %call +; CHECK-LABEL: asinh_test2 ; CHECK: call double @asinh(double %conv) } -define float @atan_test(float %f) nounwind readnone { -; CHECK: atan_test +define float @atan_test(float %f) { %conv = fpext float %f to double %call = call double @atan(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: atan_test ; CHECK: call float @atanf(float %f) } -define double @atan_test2(float %f) nounwind readnone { -; CHECK: atan_test2 +define double @atan_test2(float %f) { %conv = fpext float %f to double %call = call double @atan(double %conv) ret double %call +; CHECK-LABEL: atan_test2 ; CHECK: call double @atan(double %conv) } -define float @atanh_test(float %f) nounwind readnone { -; CHECK: atanh_test +define float @atanh_test(float %f) { %conv = fpext float %f to double %call = call double @atanh(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: atanh_test ; CHECK: call float @atanhf(float %f) } -define double @atanh_test2(float %f) nounwind readnone { -; CHECK: atanh_test2 +define double @atanh_test2(float %f) { %conv = fpext float %f to double %call = call double @atanh(double %conv) ret double %call +; CHECK-LABEL: atanh_test2 ; CHECK: call double @atanh(double %conv) } -define float @cbrt_test(float %f) nounwind readnone { -; CHECK: cbrt_test +define float @cbrt_test(float %f) { %conv = fpext float %f to double %call = call double @cbrt(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: cbrt_test ; CHECK: call float @cbrtf(float %f) } -define double @cbrt_test2(float %f) nounwind readnone { -; CHECK: cbrt_test2 +define double @cbrt_test2(float %f) { %conv = fpext float %f to double %call = call double @cbrt(double %conv) ret double %call +; CHECK-LABEL: cbrt_test2 ; CHECK: call double @cbrt(double %conv) } -define float @exp_test(float %f) nounwind readnone { -; CHECK: exp_test +define float @exp_test(float %f) { %conv = fpext float %f to double %call = call double @exp(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: exp_test ; CHECK: call float @expf(float %f) } -define double @exp_test2(float %f) nounwind readnone { -; CHECK: exp_test2 +define double @exp_test2(float %f) { %conv = fpext float %f to double %call = call double @exp(double %conv) ret double %call +; CHECK-LABEL: exp_test2 ; CHECK: call double @exp(double %conv) } -define float @expm1_test(float %f) nounwind readnone { -; CHECK: expm1_test +define float @expm1_test(float %f) { %conv = fpext float %f to double %call = call double @expm1(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: expm1_test ; CHECK: call float @expm1f(float %f) } -define double @expm1_test2(float %f) nounwind readnone { -; CHECK: expm1_test2 +define double @expm1_test2(float %f) { %conv = fpext float %f to double %call = call double @expm1(double %conv) ret double %call +; CHECK-LABEL: expm1_test2 ; CHECK: call double @expm1(double %conv) } -define float @exp10_test(float %f) nounwind readnone { -; CHECK: exp10_test +define float @exp10_test(float %f) { %conv = fpext float %f to double %call = call double @exp10(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 -; FIXME: Re-enable this when Linux allows transforming this again, or when we -; can use builtin attributes to test the transform regardless of OS. -; DISABLED-CHECK: call float @exp10f(float %f) +; CHECK-LABEL: exp10_test ; CHECK: call double @exp10(double %conv) } -define double @exp10_test2(float %f) nounwind readnone { -; CHECK: exp10_test2 +define double @exp10_test2(float %f) { %conv = fpext float %f to double %call = call double @exp10(double %conv) ret double %call +; CHECK-LABEL: exp10_test2 ; CHECK: call double @exp10(double %conv) } -define float @log_test(float %f) nounwind readnone { -; CHECK: log_test +define float @log_test(float %f) { %conv = fpext float %f to double %call = call double @log(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: log_test ; CHECK: call float @logf(float %f) } -define double @log_test2(float %f) nounwind readnone { -; CHECK: log_test2 +define double @log_test2(float %f) { %conv = fpext float %f to double %call = call double @log(double %conv) ret double %call +; CHECK-LABEL: log_test2 ; CHECK: call double @log(double %conv) } -define float @log10_test(float %f) nounwind readnone { -; CHECK: log10_test +define float @log10_test(float %f) { %conv = fpext float %f to double %call = call double @log10(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: log10_test ; CHECK: call float @log10f(float %f) } -define double @log10_test2(float %f) nounwind readnone { -; CHECK: log10_test2 +define double @log10_test2(float %f) { %conv = fpext float %f to double %call = call double @log10(double %conv) ret double %call +; CHECK-LABEL: log10_test2 ; CHECK: call double @log10(double %conv) } -define float @log1p_test(float %f) nounwind readnone { -; CHECK: log1p_test +define float @log1p_test(float %f) { %conv = fpext float %f to double %call = call double @log1p(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: log1p_test ; CHECK: call float @log1pf(float %f) } -define double @log1p_test2(float %f) nounwind readnone { -; CHECK: log1p_test2 +define double @log1p_test2(float %f) { %conv = fpext float %f to double %call = call double @log1p(double %conv) ret double %call +; CHECK-LABEL: log1p_test2 ; CHECK: call double @log1p(double %conv) } -define float @log2_test(float %f) nounwind readnone { -; CHECK: log2_test +define float @log2_test(float %f) { %conv = fpext float %f to double %call = call double @log2(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: log2_test ; CHECK: call float @log2f(float %f) } -define double @log2_test2(float %f) nounwind readnone { -; CHECK: log2_test2 +define double @log2_test2(float %f) { %conv = fpext float %f to double %call = call double @log2(double %conv) ret double %call +; CHECK-LABEL: log2_test2 ; CHECK: call double @log2(double %conv) } -define float @logb_test(float %f) nounwind readnone { -; CHECK: logb_test +define float @logb_test(float %f) { %conv = fpext float %f to double %call = call double @logb(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: logb_test ; CHECK: call float @logbf(float %f) } -define double @logb_test2(float %f) nounwind readnone { -; CHECK: logb_test2 +define double @logb_test2(float %f) { %conv = fpext float %f to double %call = call double @logb(double %conv) ret double %call +; CHECK-LABEL: logb_test2 ; CHECK: call double @logb(double %conv) } -define float @sin_test(float %f) nounwind readnone { -; CHECK: sin_test +define float @sin_test(float %f) { %conv = fpext float %f to double %call = call double @sin(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: sin_test ; CHECK: call float @sinf(float %f) } -define double @sin_test2(float %f) nounwind readnone { -; CHECK: sin_test2 +define double @sin_test2(float %f) { %conv = fpext float %f to double %call = call double @sin(double %conv) ret double %call +; CHECK-LABEL: sin_test2 ; CHECK: call double @sin(double %conv) } -define float @sqrt_test(float %f) nounwind readnone { -; CHECK: sqrt_test +define float @sqrt_test(float %f) { %conv = fpext float %f to double %call = call double @sqrt(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: sqrt_test ; CHECK: call float @sqrtf(float %f) } -define float @sqrt_int_test(float %f) nounwind readnone { -; CHECK: sqrt_int_test +define float @sqrt_int_test(float %f) { %conv = fpext float %f to double %call = call double @llvm.sqrt.f64(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: sqrt_int_test ; CHECK: call float @llvm.sqrt.f32(float %f) } -define double @sqrt_test2(float %f) nounwind readnone { -; CHECK: sqrt_test2 +define double @sqrt_test2(float %f) { %conv = fpext float %f to double %call = call double @sqrt(double %conv) ret double %call +; CHECK-LABEL: sqrt_test2 ; CHECK: call double @sqrt(double %conv) } -define float @tan_test(float %f) nounwind readnone { -; CHECK: tan_test +define float @tan_test(float %f) { %conv = fpext float %f to double %call = call double @tan(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: tan_test ; CHECK: call float @tanf(float %f) } -define double @tan_test2(float %f) nounwind readnone { -; CHECK: tan_test2 +define double @tan_test2(float %f) { %conv = fpext float %f to double %call = call double @tan(double %conv) ret double %call +; CHECK-LABEL: tan_test2 ; CHECK: call double @tan(double %conv) } -define float @tanh_test(float %f) nounwind readnone { -; CHECK: tanh_test +define float @tanh_test(float %f) { %conv = fpext float %f to double %call = call double @tanh(double %conv) %conv1 = fptrunc double %call to float ret float %conv1 +; CHECK-LABEL: tanh_test ; CHECK: call float @tanhf(float %f) } -define double @tanh_test2(float %f) nounwind readnone { -; CHECK: tanh_test2 +define double @tanh_test2(float %f) { %conv = fpext float %f to double %call = call double @tanh(double %conv) ret double %call +; CHECK-LABEL: tanh_test2 ; CHECK: call double @tanh(double %conv) } -declare double @tanh(double) nounwind readnone -declare double @tan(double) nounwind readnone -declare double @sqrt(double) nounwind readnone -declare double @sin(double) nounwind readnone -declare double @log2(double) nounwind readnone -declare double @log1p(double) nounwind readnone -declare double @log10(double) nounwind readnone -declare double @log(double) nounwind readnone -declare double @logb(double) nounwind readnone -declare double @exp10(double) nounwind readnone -declare double @expm1(double) nounwind readnone -declare double @exp(double) nounwind readnone -declare double @cbrt(double) nounwind readnone -declare double @atanh(double) nounwind readnone -declare double @atan(double) nounwind readnone -declare double @acos(double) nounwind readnone -declare double @acosh(double) nounwind readnone -declare double @asin(double) nounwind readnone -declare double @asinh(double) nounwind readnone +declare double @tanh(double) #1 +declare double @tan(double) #1 +declare double @sqrt(double) #1 +declare double @sin(double) #1 +declare double @log2(double) #1 +declare double @log1p(double) #1 +declare double @log10(double) #1 +declare double @log(double) #1 +declare double @logb(double) #1 +declare double @exp10(double) #1 +declare double @expm1(double) #1 +declare double @exp(double) #1 +declare double @cbrt(double) #1 +declare double @atanh(double) #1 +declare double @atan(double) #1 +declare double @acos(double) #1 +declare double @acosh(double) #1 +declare double @asin(double) #1 +declare double @asinh(double) #1 -declare double @llvm.sqrt.f64(double) nounwind readnone +declare double @llvm.sqrt.f64(double) #1 +attributes #1 = { "unsafe-fp-math"="true" } -- 2.34.1