aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Ilseman <milseman@apple.com>2012-12-04 00:36:06 +0000
committerMichael Ilseman <milseman@apple.com>2012-12-04 00:36:06 +0000
commitcdeb7d57a4867c28e3e6a8c391cba6f13c781fa6 (patch)
tree9778cc6f00cc30896218cd0d75cfcf85dc41ec12
parent9dce4b3d9d14ba2be2aad5bd7fd8d381a0263c36 (diff)
Have clang use LLVM IR's fast-math flags when in FastMath or FiniteMathOnly modes. Test cases included.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@169191 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r--lib/CodeGen/CodeGenFunction.cpp10
-rw-r--r--test/CodeGen/fast-math.c13
-rw-r--r--test/CodeGen/finite-math.c13
3 files changed, 36 insertions, 0 deletions
diff --git a/lib/CodeGen/CodeGenFunction.cpp b/lib/CodeGen/CodeGenFunction.cpp
index 3d7a978258..c441403fb3 100644
--- a/lib/CodeGen/CodeGenFunction.cpp
+++ b/lib/CodeGen/CodeGenFunction.cpp
@@ -22,6 +22,7 @@
#include "clang/AST/DeclCXX.h"
#include "clang/AST/StmtCXX.h"
#include "clang/Frontend/CodeGenOptions.h"
+#include "llvm/Operator.h"
#include "llvm/Intrinsics.h"
#include "llvm/MDBuilder.h"
#include "llvm/DataLayout.h"
@@ -46,6 +47,15 @@ CodeGenFunction::CodeGenFunction(CodeGenModule &cgm, bool suppressNewContext)
TerminateHandler(0), TrapBB(0) {
if (!suppressNewContext)
CGM.getCXXABI().getMangleContext().startNewFunction();
+
+ llvm::FastMathFlags FMF;
+ if (CGM.getLangOpts().FastMath)
+ FMF.UnsafeAlgebra = true;
+ if (CGM.getLangOpts().FiniteMathOnly) {
+ FMF.NoNaNs = true;
+ FMF.NoInfs = true;
+ }
+ Builder.SetFastMathFlags(FMF);
}
CodeGenFunction::~CodeGenFunction() {
diff --git a/test/CodeGen/fast-math.c b/test/CodeGen/fast-math.c
new file mode 100644
index 0000000000..9f9a39269e
--- /dev/null
+++ b/test/CodeGen/fast-math.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -ffast-math -emit-llvm -o - %s | FileCheck %s
+typedef unsigned cond_t;
+
+volatile float f0, f1, f2;
+
+void foo(void) {
+ // CHECK: define void @foo()
+
+ // CHECK: fadd fast
+ f0 = f1 + f2;
+
+ // CHECK: ret
+}
diff --git a/test/CodeGen/finite-math.c b/test/CodeGen/finite-math.c
new file mode 100644
index 0000000000..a79ee0c257
--- /dev/null
+++ b/test/CodeGen/finite-math.c
@@ -0,0 +1,13 @@
+// RUN: %clang_cc1 -ffinite-math-only -emit-llvm -o - %s | FileCheck %s
+typedef unsigned cond_t;
+
+volatile float f0, f1, f2;
+
+void foo(void) {
+ // CHECK: define void @foo()
+
+ // CHECK: fadd nnan ninf
+ f0 = f1 + f2;
+
+ // CHECK: ret
+}