Fix the folding of floating-point math library calls, like sin(infinity),
authorDan Gohman <gohman@apple.com>
Fri, 17 Sep 2010 01:38:06 +0000 (01:38 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 17 Sep 2010 01:38:06 +0000 (01:38 +0000)
so that it detects errors on platforms where libm doesn't set errno.
It's still subject to host libm details though.

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

lib/Analysis/ConstantFolding.cpp
test/Transforms/InstCombine/fold-calls.ll [new file with mode: 0644]

index 0bf7967e83b132679b81105e21951703606a571c..69581ba5ecd8a2ac5d37f9a95f9fabde3dd724bd 100644 (file)
@@ -32,6 +32,7 @@
 #include "llvm/Support/MathExtras.h"
 #include <cerrno>
 #include <cmath>
+#include <fenv.h>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -1039,9 +1040,12 @@ llvm::canConstantFoldCallTo(const Function *F) {
 
 static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, 
                                 const Type *Ty) {
+  feclearexcept(FE_ALL_EXCEPT);
   errno = 0;
   V = NativeFP(V);
-  if (errno != 0) {
+  if (errno != 0 ||
+      fetestexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)) {
+    feclearexcept(FE_ALL_EXCEPT);
     errno = 0;
     return 0;
   }
@@ -1056,9 +1060,12 @@ static Constant *ConstantFoldFP(double (*NativeFP)(double), double V,
 
 static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),
                                       double V, double W, const Type *Ty) {
+  feclearexcept(FE_ALL_EXCEPT);
   errno = 0;
   V = NativeFP(V, W);
-  if (errno != 0) {
+  if (errno != 0 ||
+      fetestexcept(FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW | FE_INVALID)) {
+    feclearexcept(FE_ALL_EXCEPT);
     errno = 0;
     return 0;
   }
diff --git a/test/Transforms/InstCombine/fold-calls.ll b/test/Transforms/InstCombine/fold-calls.ll
new file mode 100644 (file)
index 0000000..2b11771
--- /dev/null
@@ -0,0 +1,19 @@
+; RUN: opt -instcombine -S < %s | FileCheck %s
+
+; This shouldn't fold, because sin(inf) is invalid.
+; CHECK: @foo
+; CHECK:   %t = call double @sin(double 0x7FF0000000000000)
+define double @foo() {
+  %t = call double @sin(double 0x7FF0000000000000)
+  ret double %t
+}
+
+; This should fold.
+; CHECK: @bar
+; CHECK:   ret double 0x3FDA6026360C2F91
+define double @bar() {
+  %t = call double @sin(double 9.0)
+  ret double %t
+}
+
+declare double @sin(double)