[llvm] r182739 - Convert sqrt functions into sqrt instructions when -ffast-math is in effect.
Preston Gurd
preston.gurd at intel.com
Mon May 27 08:44:35 PDT 2013
Author: pgurd
Date: Mon May 27 10:44:35 2013
New Revision: 182739
URL: http://llvm.org/viewvc/llvm-project?rev=182739&view=rev
Log:
Convert sqrt functions into sqrt instructions when -ffast-math is in effect.
When -ffast-math is in effect (on Linux, at least), clang defines
__FINITE_MATH_ONLY__ > 0 when including <math.h>. This causes the
preprocessor to include <bits/math-finite.h>, which renames the sqrt functions.
For instance, "sqrt" is renamed as "__sqrt_finite".
This patch adds the 3 new names in such a way that they will be treated
as equivalent to their respective original names.
Added:
llvm/trunk/test/CodeGen/X86/sqrt-fastmath.ll
Modified:
llvm/trunk/include/llvm/Target/TargetLibraryInfo.h
llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
llvm/trunk/lib/Target/TargetLibraryInfo.cpp
Modified: llvm/trunk/include/llvm/Target/TargetLibraryInfo.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Target/TargetLibraryInfo.h?rev=182739&r1=182738&r2=182739&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Target/TargetLibraryInfo.h (original)
+++ llvm/trunk/include/llvm/Target/TargetLibraryInfo.h Mon May 27 10:44:35 2013
@@ -57,6 +57,12 @@ namespace llvm {
dunder_isoc99_sscanf,
/// void *__memcpy_chk(void *s1, const void *s2, size_t n, size_t s1size);
memcpy_chk,
+ /// double __sqrt_finite(double x);
+ sqrt_finite,
+ /// float __sqrt_finite(float x);
+ sqrtf_finite,
+ /// long double __sqrt_finite(long double x);
+ sqrtl_finite,
/// char * __strdup(const char *s);
dunder_strdup,
/// char *__strndup(const char *s, size_t n);
@@ -677,6 +683,8 @@ public:
case LibFunc::sin: case LibFunc::sinf: case LibFunc::sinl:
case LibFunc::cos: case LibFunc::cosf: case LibFunc::cosl:
case LibFunc::sqrt: case LibFunc::sqrtf: case LibFunc::sqrtl:
+ case LibFunc::sqrt_finite: case LibFunc::sqrtf_finite:
+ case LibFunc::sqrtl_finite:
case LibFunc::floor: case LibFunc::floorf: case LibFunc::floorl:
case LibFunc::nearbyint: case LibFunc::nearbyintf: case LibFunc::nearbyintl:
case LibFunc::ceil: case LibFunc::ceilf: case LibFunc::ceill:
Modified: llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp?rev=182739&r1=182738&r2=182739&view=diff
==============================================================================
--- llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp (original)
+++ llvm/trunk/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp Mon May 27 10:44:35 2013
@@ -5562,6 +5562,9 @@ void SelectionDAGBuilder::visitCall(cons
case LibFunc::sqrt:
case LibFunc::sqrtf:
case LibFunc::sqrtl:
+ case LibFunc::sqrt_finite:
+ case LibFunc::sqrtf_finite:
+ case LibFunc::sqrtl_finite:
if (visitUnaryFloatCall(I, ISD::FSQRT))
return;
break;
Modified: llvm/trunk/lib/Target/TargetLibraryInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Target/TargetLibraryInfo.cpp?rev=182739&r1=182738&r2=182739&view=diff
==============================================================================
--- llvm/trunk/lib/Target/TargetLibraryInfo.cpp (original)
+++ llvm/trunk/lib/Target/TargetLibraryInfo.cpp Mon May 27 10:44:35 2013
@@ -43,6 +43,9 @@ const char* TargetLibraryInfo::StandardN
"__isoc99_scanf",
"__isoc99_sscanf",
"__memcpy_chk",
+ "__sqrt_finite",
+ "__sqrtf_finite",
+ "__sqrtl_finite",
"__strdup",
"__strndup",
"__strtok_r",
Added: llvm/trunk/test/CodeGen/X86/sqrt-fastmath.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/CodeGen/X86/sqrt-fastmath.ll?rev=182739&view=auto
==============================================================================
--- llvm/trunk/test/CodeGen/X86/sqrt-fastmath.ll (added)
+++ llvm/trunk/test/CodeGen/X86/sqrt-fastmath.ll Mon May 27 10:44:35 2013
@@ -0,0 +1,60 @@
+; RUN: llc < %s -mcpu=core2 | FileCheck %s
+
+; generated using "clang -S -O2 -ffast-math -emit-llvm sqrt.c" from
+; #include <math.h>
+;
+; double fd(double d){
+; return sqrt(d);
+; }
+;
+; float ff(float f){
+; return sqrtf(f);
+; }
+;
+; long double fld(long double ld){
+; return sqrtl(ld);
+; }
+;
+; Tests conversion of sqrt function calls into sqrt instructions when
+; -ffast-math is in effect.
+
+; ModuleID = 'sqrt.c'
+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"
+
+; Function Attrs: nounwind readnone uwtable
+define double @fd(double %d) #0 {
+entry:
+; CHECK: sqrtsd
+ %call = tail call double @__sqrt_finite(double %d) #2
+ ret double %call
+}
+
+; Function Attrs: nounwind readnone
+declare double @__sqrt_finite(double) #1
+
+; Function Attrs: nounwind readnone uwtable
+define float @ff(float %f) #0 {
+entry:
+; CHECK: sqrtss
+ %call = tail call float @__sqrtf_finite(float %f) #2
+ ret float %call
+}
+
+; Function Attrs: nounwind readnone
+declare float @__sqrtf_finite(float) #1
+
+; Function Attrs: nounwind readnone uwtable
+define x86_fp80 @fld(x86_fp80 %ld) #0 {
+entry:
+; CHECK: fsqrt
+ %call = tail call x86_fp80 @__sqrtl_finite(x86_fp80 %ld) #2
+ ret x86_fp80 %call
+}
+
+; Function Attrs: nounwind readnone
+declare x86_fp80 @__sqrtl_finite(x86_fp80) #1
+
+attributes #0 = { nounwind readnone uwtable "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "unsafe-fp-math"="true" "use-soft-float"="false" }
+attributes #1 = { nounwind readnone "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-frame-pointer-elim-non-leaf"="false" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "unsafe-fp-math"="true" "use-soft-float"="false" }
+attributes #2 = { nounwind readnone }
More information about the llvm-commits
mailing list