[PATCH] D31168: Set FMF for -ffp-contract=fast rather than a TargetConfig

Adam Nemet via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 21 16:03:29 PDT 2017


anemet updated this revision to Diff 92564.
anemet added a comment.

Address Akira's comments


https://reviews.llvm.org/D31168

Files:
  lib/CodeGen/CGExprScalar.cpp
  test/CodeGen/ffp-contract-fast-option.c


Index: test/CodeGen/ffp-contract-fast-option.c
===================================================================
--- /dev/null
+++ test/CodeGen/ffp-contract-fast-option.c
@@ -0,0 +1,15 @@
+// RUN: %clang_cc1 -O3 -ffp-contract=fast -triple %itanium_abi_triple -emit-llvm -o - %s | FileCheck %s
+
+float fp_contract_1(float a, float b, float c) {
+  // CHECK-LABEL: @fp_contract_1(
+  // CHECK: fmul contract float
+  // CHECK: fadd contract float
+  return a * b + c;
+}
+
+float fp_contract_2(float a, float b, float c) {
+  // CHECK-LABEL: @fp_contract_2(
+  // CHECK: fmul contract float
+  // CHECK: fsub contract float
+  return a * b - c;
+}
Index: lib/CodeGen/CGExprScalar.cpp
===================================================================
--- lib/CodeGen/CGExprScalar.cpp
+++ lib/CodeGen/CGExprScalar.cpp
@@ -252,12 +252,33 @@
     return Builder.CreateIsNotNull(V, "tobool");
   }
 
+  static Optional<FPOptions> getFPFeatures(Expr *E) {
+    if (const auto *BO = dyn_cast<BinaryOperator>(E))
+      return BO->getFPFeatures();
+    else if (const auto *CE = dyn_cast<CXXOperatorCallExpr>(E))
+      return CE->getFPFeatures();
+    return None;
+  }
+
+  /// A RAII to apply the FP features from the expression to the IRBuilder.
+  struct ApplyFPFeatures : public CGBuilderTy::FastMathFlagGuard {
+    ApplyFPFeatures(llvm::IRBuilderBase &Builder, Expr *E)
+        : CGBuilderTy::FastMathFlagGuard(Builder) {
+      if (Optional<FPOptions> FPFeatures = getFPFeatures(E)) {
+        llvm::FastMathFlags FMF = Builder.getFastMathFlags();
+        FMF.setAllowContract(FPFeatures->allowFPContractAcrossStatement());
+        Builder.setFastMathFlags(FMF);
+      }
+    }
+  };
+
   //===--------------------------------------------------------------------===//
   //                            Visitor Methods
   //===--------------------------------------------------------------------===//
 
   Value *Visit(Expr *E) {
     ApplyDebugLocation DL(CGF, E);
+    ApplyFPFeatures FPF(Builder, E);
     return StmtVisitor<ScalarExprEmitter, Value*>::Visit(E);
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D31168.92564.patch
Type: text/x-patch
Size: 2079 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170321/ad9ac0e6/attachment.bin>


More information about the cfe-commits mailing list