]> granicus.if.org Git - clang/commitdiff
Restore the sqrt -> llvm.sqrt mapping in fast-math mode
authorHal Finkel <hfinkel@anl.gov>
Thu, 12 Sep 2013 23:57:55 +0000 (23:57 +0000)
committerHal Finkel <hfinkel@anl.gov>
Thu, 12 Sep 2013 23:57:55 +0000 (23:57 +0000)
This restores the sqrt -> llvm.sqrt mapping, but only in fast-math mode
(specifically, when the UnsafeFPMath or NoNaNsFPMath CodeGen options are
enabled). The @llvm.sqrt* intrinsics have slightly different semantics from the
libm call, specifically, they are undefined when given a non-zero negative
number (the libm calls will always return NaN for any negative number).

This mapping was removed in r100613, and replaced with a TODO, but at that time
the fast-math flags were not yet implemented. Now that we have these, restoring
this mapping is important because it will enable autovectorization of sqrt
calls in loops (at least in fast-math mode).

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@190646 91177308-0d34-0410-b5e6-96231b3b80d8

lib/CodeGen/CGBuiltin.cpp
test/CodeGen/libcalls.c

index 5b5b39f5e34b482c97fcef9947bcd3ce221c91f3..e6cfe64471eb48e5c58c94cf79d546283c9955d8 100644 (file)
@@ -1282,12 +1282,19 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
   case Builtin::BIsqrt:
   case Builtin::BIsqrtf:
   case Builtin::BIsqrtl: {
-    // TODO: there is currently no set of optimizer flags
-    // sufficient for us to rewrite sqrt to @llvm.sqrt.
-    // -fmath-errno=0 is not good enough; we need finiteness.
-    // We could probably precondition the call with an ult
-    // against 0, but is that worth the complexity?
-    break;
+    // Transform a call to sqrt* into a @llvm.sqrt.* intrinsic call, but only
+    // in finite- or unsafe-math mode (the intrinsic has different semantics
+    // for handling negative numbers compared to the library function, so
+    // -fmath-errno=0 is not enough).
+    if (!FD->hasAttr<ConstAttr>())
+      break;
+    if (!(CGM.getCodeGenOpts().UnsafeFPMath ||
+          CGM.getCodeGenOpts().NoNaNsFPMath))
+      break;
+    Value *Arg0 = EmitScalarExpr(E->getArg(0));
+    llvm::Type *ArgType = Arg0->getType();
+    Value *F = CGM.getIntrinsic(Intrinsic::sqrt, ArgType);
+    return RValue::get(Builder.CreateCall(F, Arg0));
   }
 
   case Builtin::BIpow:
index 520b79b146a179579f18a60a2796ea91902dd007..3112c8757343ad057405b9955c12049c2441da6b 100644 (file)
@@ -1,8 +1,10 @@
 // RUN: %clang_cc1 -fmath-errno -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-YES %s
 // RUN: %clang_cc1 -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-NO %s
+// RUN: %clang_cc1 -menable-unsafe-fp-math -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix CHECK-FAST %s
 
 // CHECK-YES-LABEL: define void @test_sqrt
 // CHECK-NO-LABEL: define void @test_sqrt
+// CHECK-FAST-LABEL: define void @test_sqrt
 void test_sqrt(float a0, double a1, long double a2) {
   // Following llvm-gcc's lead, we never emit these as intrinsics;
   // no-math-errno isn't good enough.  We could probably use intrinsics
@@ -27,6 +29,9 @@ void test_sqrt(float a0, double a1, long double a2) {
 // CHECK-NO: declare float @sqrtf(float) [[NUW_RN:#[0-9]+]]
 // CHECK-NO: declare double @sqrt(double) [[NUW_RN]]
 // CHECK-NO: declare x86_fp80 @sqrtl(x86_fp80) [[NUW_RN]]
+// CHECK-FAST: declare float @llvm.sqrt.f32(float)
+// CHECK-FAST: declare double @llvm.sqrt.f64(double)
+// CHECK-FAST: declare x86_fp80 @llvm.sqrt.f80(x86_fp80)
 
 // CHECK-YES-LABEL: define void @test_pow
 // CHECK-NO-LABEL: define void @test_pow