[llvm-commits] [llvm] r114148 - in /llvm/trunk: lib/Analysis/ConstantFolding.cpp test/Transforms/InstCombine/fold-calls.ll

Dan Gohman gohman at apple.com
Thu Sep 16 18:38:06 PDT 2010


Author: djg
Date: Thu Sep 16 20:38:06 2010
New Revision: 114148

URL: http://llvm.org/viewvc/llvm-project?rev=114148&view=rev
Log:
Fix the folding of floating-point math library calls, like sin(infinity),
so that it detects errors on platforms where libm doesn't set errno.
It's still subject to host libm details though.

Added:
    llvm/trunk/test/Transforms/InstCombine/fold-calls.ll
Modified:
    llvm/trunk/lib/Analysis/ConstantFolding.cpp

Modified: llvm/trunk/lib/Analysis/ConstantFolding.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ConstantFolding.cpp?rev=114148&r1=114147&r2=114148&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ConstantFolding.cpp (original)
+++ llvm/trunk/lib/Analysis/ConstantFolding.cpp Thu Sep 16 20:38:06 2010
@@ -32,6 +32,7 @@
 #include "llvm/Support/MathExtras.h"
 #include <cerrno>
 #include <cmath>
+#include <fenv.h>
 using namespace llvm;
 
 //===----------------------------------------------------------------------===//
@@ -1039,9 +1040,12 @@
 
 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 *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;
   }

Added: llvm/trunk/test/Transforms/InstCombine/fold-calls.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/fold-calls.ll?rev=114148&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/fold-calls.ll (added)
+++ llvm/trunk/test/Transforms/InstCombine/fold-calls.ll Thu Sep 16 20:38:06 2010
@@ -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)





More information about the llvm-commits mailing list