]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c-family: implement -ffp-contract=on
authorAlexander Monakov <amonakov@ispras.ru>
Thu, 18 May 2023 20:47:47 +0000 (23:47 +0300)
committerAlexander Monakov <amonakov@ispras.ru>
Wed, 21 Jun 2023 18:31:25 +0000 (21:31 +0300)
Implement -ffp-contract=on for C and C++ without changing default
behavior (=off for -std=cNN, =fast for C++ and -std=gnuNN).

gcc/c-family/ChangeLog:

* c-gimplify.cc (fma_supported_p): New helper.
(c_gimplify_expr) [PLUS_EXPR, MINUS_EXPR]: Implement FMA
contraction.

gcc/ChangeLog:

* common.opt (fp_contract_mode) [on]: Remove fallback.
* config/sh/sh.md (*fmasf4): Correct flag_fp_contract_mode test.
* doc/invoke.texi (-ffp-contract): Update.
* trans-mem.cc (diagnose_tm_1): Skip internal function calls.

gcc/c-family/c-gimplify.cc
gcc/common.opt
gcc/config/sh/sh.md
gcc/doc/invoke.texi
gcc/trans-mem.cc

index ef5c7d919fcc678f2b8aac453c5d82d8ac76b4ef..17b0610a89f9c5f9e22127b1da2a39933f176ac5 100644 (file)
@@ -41,6 +41,8 @@ along with GCC; see the file COPYING3.  If not see
 #include "c-ubsan.h"
 #include "tree-nested.h"
 #include "context.h"
+#include "tree-pass.h"
+#include "internal-fn.h"
 
 /*  The gimplification pass converts the language-dependent trees
     (ld-trees) emitted by the parser into language-independent trees
@@ -686,6 +688,14 @@ c_build_bind_expr (location_t loc, tree block, tree body)
   return bind;
 }
 
+/* Helper for c_gimplify_expr: test if target supports fma-like FN.  */
+
+static bool
+fma_supported_p (enum internal_fn fn, tree type)
+{
+  return direct_internal_fn_supported_p (fn, type, OPTIMIZE_FOR_BOTH);
+}
+
 /* Gimplification of expression trees.  */
 
 /* Do C-specific gimplification on *EXPR_P.  PRE_P and POST_P are as in
@@ -739,6 +749,75 @@ c_gimplify_expr (tree *expr_p, gimple_seq *pre_p ATTRIBUTE_UNUSED,
        break;
       }
 
+    case PLUS_EXPR:
+    case MINUS_EXPR:
+      {
+       tree type = TREE_TYPE (*expr_p);
+       /* For -ffp-contract=on we need to attempt FMA contraction only
+          during initial gimplification.  Late contraction across statement
+          boundaries would violate language semantics.  */
+       if (SCALAR_FLOAT_TYPE_P (type)
+           && flag_fp_contract_mode == FP_CONTRACT_ON
+           && cfun && !(cfun->curr_properties & PROP_gimple_any)
+           && fma_supported_p (IFN_FMA, type))
+         {
+           bool neg_mul = false, neg_add = code == MINUS_EXPR;
+
+           tree *op0_p = &TREE_OPERAND (*expr_p, 0);
+           tree *op1_p = &TREE_OPERAND (*expr_p, 1);
+
+           /* Look for ±(x * y) ± z, swapping operands if necessary.  */
+           if (TREE_CODE (*op0_p) == NEGATE_EXPR
+               && TREE_CODE (TREE_OPERAND (*op0_p, 0)) == MULT_EXPR)
+             /* '*EXPR_P' is '-(x * y) ± z'.  This is fine.  */;
+           else if (TREE_CODE (*op0_p) != MULT_EXPR)
+             {
+               std::swap (op0_p, op1_p);
+               std::swap (neg_mul, neg_add);
+             }
+           if (TREE_CODE (*op0_p) == NEGATE_EXPR)
+             {
+               op0_p = &TREE_OPERAND (*op0_p, 0);
+               neg_mul = !neg_mul;
+             }
+           if (TREE_CODE (*op0_p) != MULT_EXPR)
+             break;
+           auto_vec<tree, 3> ops (3);
+           ops.quick_push (TREE_OPERAND (*op0_p, 0));
+           ops.quick_push (TREE_OPERAND (*op0_p, 1));
+           ops.quick_push (*op1_p);
+
+           enum internal_fn ifn = IFN_FMA;
+           if (neg_mul)
+             {
+               if (fma_supported_p (IFN_FNMA, type))
+                 ifn = IFN_FNMA;
+               else
+                 ops[0] = build1 (NEGATE_EXPR, type, ops[0]);
+             }
+           if (neg_add)
+             {
+               enum internal_fn ifn2 = ifn == IFN_FMA ? IFN_FMS : IFN_FNMS;
+               if (fma_supported_p (ifn2, type))
+                 ifn = ifn2;
+               else
+                 ops[2] = build1 (NEGATE_EXPR, type, ops[2]);
+             }
+           /* Avoid gimplify_arg: it emits all side effects into *PRE_P.  */
+           for (auto &&op : ops)
+             if (gimplify_expr (&op, pre_p, post_p, is_gimple_val, fb_rvalue)
+                 == GS_ERROR)
+               return GS_ERROR;
+
+           gcall *call = gimple_build_call_internal_vec (ifn, ops);
+           gimple_seq_add_stmt_without_update (pre_p, call);
+           *expr_p = create_tmp_var (type);
+           gimple_call_set_lhs (call, *expr_p);
+           return GS_ALL_DONE;
+         }
+       break;
+      }
+
     default:;
     }
 
index a28ca13385a22f0b4f770257a76bdb86d8d317c6..3daec85aef9b9544b052b40026e1fd91d54e2754 100644 (file)
@@ -1662,9 +1662,8 @@ Name(fp_contract_mode) Type(enum fp_contract_mode) UnknownError(unknown floating
 EnumValue
 Enum(fp_contract_mode) String(off) Value(FP_CONTRACT_OFF)
 
-; Not implemented, fall back to conservative FP_CONTRACT_OFF.
 EnumValue
-Enum(fp_contract_mode) String(on) Value(FP_CONTRACT_OFF)
+Enum(fp_contract_mode) String(on) Value(FP_CONTRACT_ON)
 
 EnumValue
 Enum(fp_contract_mode) String(fast) Value(FP_CONTRACT_FAST)
index 4622dba0121d371e65453cf7b8a9040093381b8e..5cb179548256e2ea6c24b0f2db9062b4963bcc7c 100644 (file)
                 (match_operand:SF 3 "arith_reg_operand" "0")))
    (clobber (reg:SI FPSCR_STAT_REG))
    (use (reg:SI FPSCR_MODES_REG))]
-  "TARGET_SH2E && flag_fp_contract_mode != FP_CONTRACT_OFF"
+  "TARGET_SH2E && flag_fp_contract_mode == FP_CONTRACT_FAST"
   "fmac        %1,%2,%0"
   "&& can_create_pseudo_p ()"
   [(parallel [(set (match_dup 0)
index 215ab0dd05c3e712cf2503f870e174cc72a1664c..8c17a81c7d908b5f8cba927fd861e08cc971c15f 100644 (file)
@@ -12077,10 +12077,12 @@ This option is enabled by default at optimization levels @option{-O1},
 such as forming of fused multiply-add operations if the target has
 native support for them.
 @option{-ffp-contract=on} enables floating-point expression contraction
-if allowed by the language standard.  This is currently not implemented
-and treated equal to @option{-ffp-contract=off}.
+if allowed by the language standard.  This is implemented for C and C++,
+where it enables contraction within one expression, but not across
+different statements.
 
-The default is @option{-ffp-contract=fast}.
+The default is @option{-ffp-contract=off} for C in a standards compliant mode
+(@option{-std=c11} or similar), @option{-ffp-contract=fast} otherwise.
 
 @opindex fomit-frame-pointer
 @item -fomit-frame-pointer
index d036e4333dbe9f25e012afde137f533b56ce6aad..9c3d1121a03cafe57523e920502b9bcade0ed0ce 100644 (file)
@@ -637,6 +637,9 @@ diagnose_tm_1 (gimple_stmt_iterator *gsi, bool *handled_ops_p,
     {
     case GIMPLE_CALL:
       {
+       if (gimple_call_internal_p (stmt))
+         break;
+
        tree fn = gimple_call_fn (stmt);
 
        if ((d->summary_flags & DIAG_TM_OUTER) == 0