]> gcc.gnu.org Git - gcc.git/commitdiff
Only allow (int)trunc(x) to (int)x simplification with -ffp-int-builtin-inexact ...
authorXi Ruoyao <xry111@xry111.site>
Fri, 24 Nov 2023 03:08:19 +0000 (11:08 +0800)
committerXi Ruoyao <xry111@xry111.site>
Tue, 12 Dec 2023 11:21:57 +0000 (19:21 +0800)
With -fno-fp-int-builtin-inexact, trunc is not allowed to raise
FE_INEXACT and it should produce an integral result (if the input is not
NaN or Inf).  Thus FE_INEXACT should not be raised.

But (int)x may raise FE_INEXACT when x is a non-integer, non-NaN, and
non-Inf value.  C23 recommends to do so in a footnote.

Thus we should not simplify (int)trunc(x) to (int)x if
-fno-fp-int-builtin-inexact is in-effect.

gcc/ChangeLog:

PR middle-end/107723
* convert.cc (convert_to_integer_1) [case BUILT_IN_TRUNC]: Break
early if !flag_fp_int_builtin_inexact and flag_trapping_math.

gcc/testsuite/ChangeLog:

PR middle-end/107723
* gcc.dg/torture/builtin-fp-int-inexact-trunc.c: New test.

gcc/convert.cc
gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c [new file with mode: 0644]

index 46c8bcb31f821f17cda9d6124cc2a55ce32e4ca0..f214b750188da1ba3486a394a74f491ac0ff170a 100644 (file)
@@ -591,7 +591,8 @@ convert_to_integer_1 (tree type, tree expr, bool dofold)
        CASE_FLT_FN (BUILT_IN_TRUNC):
        CASE_FLT_FN_FLOATN_NX (BUILT_IN_TRUNC):
          if (call_expr_nargs (s_expr) != 1
-             || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr, 0))))
+             || !SCALAR_FLOAT_TYPE_P (TREE_TYPE (CALL_EXPR_ARG (s_expr, 0)))
+             || (!flag_fp_int_builtin_inexact && flag_trapping_math))
            break;
          return convert_to_integer_1 (type, CALL_EXPR_ARG (s_expr, 0),
                                       dofold);
diff --git a/gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c b/gcc/testsuite/gcc.dg/torture/builtin-fp-int-inexact-trunc.c
new file mode 100644 (file)
index 0000000..0973118
--- /dev/null
@@ -0,0 +1,12 @@
+/* Test -fno-fp-int-builtin-inexact.  */
+/* { dg-do compile } */
+/* { dg-options "-fno-fp-int-builtin-inexact -fdump-tree-original" } */
+
+long
+x (double y)
+{
+  return __builtin_trunc (y);
+}
+
+/* Optimization should not discard the __builtin_trunc call.  */
+/* { dg-final { scan-tree-dump "__builtin_trunc" "original" } } */
This page took 0.077777 seconds and 5 git commands to generate.