#include "llvm/Support/MathExtras.h"
#include <cerrno>
#include <cmath>
+#include <fenv.h>
using namespace llvm;
//===----------------------------------------------------------------------===//
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;
}
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;
}
--- /dev/null
+; 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)