This optimisation does not honour signed zeros, so should not be
enabled except with -fno-signed-zeros.

Cherry-pick of 7dd3b2b09cbeb6712ec680a0445cb0ad41070423.

Applies cleanly on releases/gcc-12. Regression-tested, only new
failure is in gcc/testsuite/c-c++-common/hwasan/large-aligned-1.c
which I believe is unrelated and known to be flaky.

OK for backport to GCC 12? If so, please commit for me as I do not
have commit rights in GCC.

Thanks,
Joe

gcc/ChangeLog:

        * match.pd: Fix truncation pattern for -fno-signed-zeroes

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/no_merge_trunc_signed_zero.c: New test.
---
 gcc/match.pd                                  |  1 +
 .../aarch64/no_merge_trunc_signed_zero.c      | 24 +++++++++++++++++++
 2 files changed, 25 insertions(+)
 create mode 100644 
gcc/testsuite/gcc.target/aarch64/no_merge_trunc_signed_zero.c

diff --git a/gcc/match.pd b/gcc/match.pd
index c5a4426e76b..0938d56fa45 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -3825,6 +3825,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 (simplify
    (float (fix_trunc @0))
    (if (!flag_trapping_math
+       && !HONOR_SIGNED_ZEROS (type)
        && types_match (type, TREE_TYPE (@0))
        && direct_internal_fn_supported_p (IFN_TRUNC, type,
                                          OPTIMIZE_FOR_BOTH))
diff --git a/gcc/testsuite/gcc.target/aarch64/no_merge_trunc_signed_zero.c 
b/gcc/testsuite/gcc.target/aarch64/no_merge_trunc_signed_zero.c
new file mode 100644
index 00000000000..b2c93e55567
--- /dev/null
+++ b/gcc/testsuite/gcc.target/aarch64/no_merge_trunc_signed_zero.c
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fno-trapping-math -fsigned-zeros" } */
+
+#include <math.h>
+
+float
+f1 (float x)
+{
+  return (int) rintf(x);
+}
+
+double
+f2 (double x)
+{
+  return (long) rint(x);
+}
+
+/* { dg-final { scan-assembler "frintx\\ts\[0-9\]+, s\[0-9\]+" } } */
+/* { dg-final { scan-assembler "cvtzs\\ts\[0-9\]+, s\[0-9\]+" } } */
+/* { dg-final { scan-assembler "scvtf\\ts\[0-9\]+, s\[0-9\]+" } } */
+/* { dg-final { scan-assembler "frintx\\td\[0-9\]+, d\[0-9\]+" } } */
+/* { dg-final { scan-assembler "cvtzs\\td\[0-9\]+, d\[0-9\]+" } } */
+/* { dg-final { scan-assembler "scvtf\\td\[0-9\]+, d\[0-9\]+" } } */
+
-- 
2.27.0

Reply via email to