Skip to content

Commit 8586bfd

Browse files
committed
@llvm.sqrt isn't really close enough to C's sqrt to justify emitting calls
to the intrinsic, even when math-errno is off. Fixes rdar://problem/7828230 by falling back on the library function. llvm-svn: 100613
1 parent 4948793 commit 8586bfd

File tree

2 files changed

+46
-17
lines changed

2 files changed

+46
-17
lines changed

clang/lib/CodeGen/CGBuiltin.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -682,13 +682,12 @@ RValue CodeGenFunction::EmitBuiltinExpr(const FunctionDecl *FD,
682682
case Builtin::BIsqrt:
683683
case Builtin::BIsqrtf:
684684
case Builtin::BIsqrtl: {
685-
// Rewrite sqrt to intrinsic if allowed.
686-
if (!FD->hasAttr<ConstAttr>())
687-
break;
688-
Value *Arg0 = EmitScalarExpr(E->getArg(0));
689-
const llvm::Type *ArgType = Arg0->getType();
690-
Value *F = CGM.getIntrinsic(Intrinsic::sqrt, &ArgType, 1);
691-
return RValue::get(Builder.CreateCall(F, Arg0, "tmp"));
685+
// TODO: there is currently no set of optimizer flags
686+
// sufficient for us to rewrite sqrt to @llvm.sqrt.
687+
// -fmath-errno=0 is not good enough; we need finiteness.
688+
// We could probably precondition the call with an ult
689+
// against 0, but is that worth the complexity?
690+
break;
692691
}
693692

694693
case Builtin::BIpow:

clang/test/CodeGen/libcalls.c

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,52 @@
1-
// RUN: %clang_cc1 -fmath-errno -emit-llvm -o %t %s -triple i386-unknown-unknown
2-
// RUN: grep "declare " %t | count 6
3-
// RUN: grep "declare " %t | grep "@llvm." | count 1
4-
// RUN: %clang_cc1 -emit-llvm -o %t %s -triple i386-unknown-unknown
5-
// RUN: grep "declare " %t | count 6
6-
// RUN: grep "declare " %t | grep -v "@llvm." | count 0
7-
8-
// IRgen only pays attention to const; it should always call llvm for
9-
// this.
10-
float sqrtf(float) __attribute__((const));
1+
// RUN: %clang_cc1 -fmath-errno -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix YES %s
2+
// RUN: %clang_cc1 -emit-llvm -o - %s -triple i386-unknown-unknown | FileCheck -check-prefix NO %s
113

4+
// CHECK-YES: define void @test_sqrt
5+
// CHECK-NO: define void @test_sqrt
126
void test_sqrt(float a0, double a1, long double a2) {
7+
// Following llvm-gcc's lead, we never emit these as intrinsics;
8+
// no-math-errno isn't good enough. We could probably use intrinsics
9+
// with appropriate guards if it proves worthwhile.
10+
11+
// CHECK-YES: call float @sqrtf
12+
// CHECK-NO: call float @sqrtf
1313
float l0 = sqrtf(a0);
14+
15+
// CHECK-YES: call double @sqrt
16+
// CHECK-NO: call double @sqrt
1417
double l1 = sqrt(a1);
18+
19+
// CHECK-YES: call x86_fp80 @sqrtl
20+
// CHECK-NO: call x86_fp80 @sqrtl
1521
long double l2 = sqrtl(a2);
1622
}
1723

24+
// CHECK-YES: declare float @sqrtf(float)
25+
// CHECK-YES: declare double @sqrt(double)
26+
// CHECK-YES: declare x86_fp80 @sqrtl(x86_fp80)
27+
// CHECK-NO: declare float @sqrtf(float) readnone
28+
// CHECK-NO: declare double @sqrt(double) readnone
29+
// CHECK-NO: declare x86_fp80 @sqrtl(x86_fp80) readnone
30+
31+
// CHECK-YES: define void @test_pow
32+
// CHECK-NO: define void @test_pow
1833
void test_pow(float a0, double a1, long double a2) {
34+
// CHECK-YES: call float @powf
35+
// CHECK-NO: call float @llvm.pow.f32
1936
float l0 = powf(a0, a0);
37+
38+
// CHECK-YES: call double @pow
39+
// CHECK-NO: call double @llvm.pow.f64
2040
double l1 = pow(a1, a1);
41+
42+
// CHECK-YES: call x86_fp80 @powl
43+
// CHECK-NO: call x86_fp80 @llvm.pow.f80
2144
long double l2 = powl(a2, a2);
2245
}
46+
47+
// CHECK-YES: declare float @powf(float, float)
48+
// CHECK-YES: declare double @pow(double, double)
49+
// CHECK-YES: declare x86_fp80 @powl(x86_fp80, x86_fp80)
50+
// CHECK-NO: declare float @llvm.pow.f32(float, float) nounwind readonly
51+
// CHECK-NO: declare double @llvm.pow.f64(double, double) nounwind readonly
52+
// CHECK-NO: declare x86_fp80 @llvm.pow.f80(x86_fp80, x86_fp80) nounwind readonly

0 commit comments

Comments
 (0)