summaryrefslogtreecommitdiffstats
path: root/llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll
diff options
context:
space:
mode:
authorAndrew Ng <anng.sw@gmail.com>2017-04-25 12:36:14 +0000
committerAndrew Ng <anng.sw@gmail.com>2017-04-25 12:36:14 +0000
commit1606fc0bf90fa984f335c7d9d7b56761afa0e67a (patch)
tree8afed9a011b2868979f7a280f8dd96087b00c178 /llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll
parentab0446332e7b50328499e138b9856625a6577109 (diff)
downloadbcm5719-llvm-1606fc0bf90fa984f335c7d9d7b56761afa0e67a.tar.gz
bcm5719-llvm-1606fc0bf90fa984f335c7d9d7b56761afa0e67a.zip
[SimplifyLibCalls] Fix infinite loop with fast-math optimization.
One of the fast-math optimizations is to replace calls to standard double functions with their float equivalents, e.g. exp -> expf. However, this can cause infinite loops for the following: float expf(float val) { return (float) exp((double) val); } A similar inline declaration exists in the MinGW-w64 math.h header file which when compiled with -O2/3 and fast-math generates infinite loops. So this fix checks that the calling function to the standard double function that is being replaced does not match the float equivalent. Differential Revision: https://reviews.llvm.org/D31806 llvm-svn: 301304
Diffstat (limited to 'llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll')
-rw-r--r--llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll60
1 files changed, 60 insertions, 0 deletions
diff --git a/llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll b/llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll
new file mode 100644
index 00000000000..a351fe82173
--- /dev/null
+++ b/llvm/test/Transforms/Util/libcalls-fast-math-inf-loop.ll
@@ -0,0 +1,60 @@
+; RUN: opt -S -instcombine -o - %s | FileCheck %s
+
+; Test that fast math lib call simplification of double math function to float
+; equivalent doesn't occur when the calling function matches the float
+; equivalent math function. Otherwise this can cause the generation of infinite
+; loops when compiled with -O2/3 and fast math.
+
+; Test case C source:
+;
+; extern double exp(double x);
+; inline float expf(float x) { return (float) exp((double) x); }
+; float fn(float f) { return expf(f); }
+;
+; IR generated with command:
+;
+; clang -cc1 -O2 -ffast-math -emit-llvm -disable-llvm-passes -triple x86_64-unknown-unknown -o - <srcfile>
+
+target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-unknown"
+
+; Function Attrs: nounwind
+define float @fn(float %f) #0 {
+; CHECK: define float @fn(
+; CHECK: call fast float @expf(
+ %f.addr = alloca float, align 4
+ store float %f, float* %f.addr, align 4, !tbaa !1
+ %1 = load float, float* %f.addr, align 4, !tbaa !1
+ %call = call fast float @expf(float %1) #3
+ ret float %call
+}
+
+; Function Attrs: inlinehint nounwind readnone
+define available_externally float @expf(float %x) #1 {
+; CHECK: define available_externally float @expf(
+; CHECK: fpext float
+; CHECK: call fast double @exp(
+; CHECK: fptrunc double
+ %x.addr = alloca float, align 4
+ store float %x, float* %x.addr, align 4, !tbaa !1
+ %1 = load float, float* %x.addr, align 4, !tbaa !1
+ %conv = fpext float %1 to double
+ %call = call fast double @exp(double %conv) #3
+ %conv1 = fptrunc double %call to float
+ ret float %conv1
+}
+
+; Function Attrs: nounwind readnone
+declare double @exp(double) #2
+
+attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { inlinehint nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #2 = { nounwind readnone }
+
+!llvm.ident = !{!0}
+
+!0 = !{!"clang version 5.0.0"}
+!1 = !{!2, !2, i64 0}
+!2 = !{!"float", !3, i64 0}
+!3 = !{!"omnipotent char", !4, i64 0}
+!4 = !{!"Simple C/C++ TBAA"}
OpenPOWER on IntegriCloud