From: Kyrylo Tkachov <[email protected]>

The CRC loop pass accepts any iteration count up to the CRC width.  When
the CRC is xor-ed with the data before the loop, it builds a data argument
of a non-standard integer type with one bit per iteration.  The expander
for IFN_CRC and IFN_CRC_REV uses TYPE_MODE of the data type, so for a
24 iteration loop it processes 32 bits and returns the value after 32
shift steps instead of 24:

  for (int i = 0; i < 24; i++)
    crc = (crc & 1) ? (crc >> 1) ^ 0xEDB88320 : crc >> 1;

Only accept loops whose data size is the precision of an integer mode,
in validate_crc_and_data.  When the data is a PHI, also require its type
to have the precision of its mode.  Add an assert in expand_crc_optab_fn so
that such a type turns into an ICE rather than wrong code.

Bootstrapped and tested on aarch64-none-linux-gnu.
Ok for trunk?
Thanks,
Kyrill

gcc/ChangeLog:

        PR middle-end/127984
        * gimple-crc-optimization.cc (crc_optimization::validate_crc_and_data):
        Reject data sizes that are not the precision of an integer mode
        and data PHIs whose type does not have the precision of its mode.
        * internal-fn.cc (expand_crc_optab_fn): Assert that the data type
        precision matches its mode.

gcc/testsuite/ChangeLog:

        PR middle-end/127984
        * gcc.dg/pr127984.c: New test.

Signed-off-by: Kyrylo Tkachov <[email protected]>
---
 gcc/gimple-crc-optimization.cc  |  8 +++++-
 gcc/internal-fn.cc              |  1 +
 gcc/testsuite/gcc.dg/pr127984.c | 47 +++++++++++++++++++++++++++++++++
 3 files changed, 55 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr127984.c

diff --git a/gcc/gimple-crc-optimization.cc b/gcc/gimple-crc-optimization.cc
index ec985148e08..8bbae6c39ee 100644
--- a/gcc/gimple-crc-optimization.cc
+++ b/gcc/gimple-crc-optimization.cc
@@ -1124,6 +1124,11 @@ bool crc_optimization::validate_crc_and_data ()
   if (TYPE_PRECISION (TREE_TYPE (m_crc_arg)) < data_size)
     return false;
 
+  /* The expansion processes TYPE_MODE (data) bits, so the data size must
+     match the precision of an integer mode.  */
+  if (!int_mode_for_size (data_size, 0).exists ())
+    return false;
+
   /* Set m_data_arg if a PHI node for data exists,
      and check its size against loop iterations.
      This is the case when data and CRC are XOR-ed in the loop.  */
@@ -1136,7 +1141,8 @@ bool crc_optimization::validate_crc_and_data ()
       m_data_arg = PHI_ARG_DEF_FROM_EDGE (m_phi_for_data,
                                          loop_preheader_edge (m_crc_loop));
       gcc_assert (m_data_arg);
-      if (TYPE_PRECISION (TREE_TYPE (m_data_arg)) != data_size)
+      if (TYPE_PRECISION (TREE_TYPE (m_data_arg)) != data_size
+         || !type_has_mode_precision_p (TREE_TYPE (m_data_arg)))
        {
          if (dump_file && (dump_flags & TDF_DETAILS))
            fprintf (dump_file,
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index d277a07eaf7..da332bf3b3b 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -4232,6 +4232,7 @@ expand_crc_optab_fn (internal_fn fn, gcall *stmt, 
convert_optab optab)
   tree data_type = TREE_TYPE (rhs2);
 
   gcc_assert (TYPE_MODE (result_type) >= TYPE_MODE (data_type));
+  gcc_assert (type_has_mode_precision_p (data_type));
 
   rtx dest = expand_expr (lhs, NULL_RTX, VOIDmode, EXPAND_WRITE);
   rtx crc = expand_normal (rhs1);
diff --git a/gcc/testsuite/gcc.dg/pr127984.c b/gcc/testsuite/gcc.dg/pr127984.c
new file mode 100644
index 00000000000..1ce85f01dad
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr127984.c
@@ -0,0 +1,47 @@
+/* PR middle-end/127984 */
+/* { dg-do run } */
+/* { dg-options "-O2" } */
+
+#define CRC(NAME, N)                           \
+__attribute__((noipa)) unsigned                                \
+NAME (unsigned crc)                                    \
+{                                                      \
+  for (int i = 0; i < N; i++)                          \
+    {                                                  \
+      if (crc & 1)                                     \
+       crc = (crc >> 1) ^ 0xEDB88320;                  \
+      else                                             \
+       crc >>= 1;                                      \
+    }                                                  \
+  return crc;                                          \
+}
+
+CRC (f24, 24)
+CRC (f12, 12)
+CRC (f20, 20)
+
+/* Reference with the loop bound hidden from the optimizer.  */
+__attribute__((noipa)) unsigned
+ref (unsigned crc, int n)
+{
+  for (int i = 0; i < n; i++)
+    {
+      if (crc & 1)
+       crc = (crc >> 1) ^ 0xEDB88320;
+      else
+       crc >>= 1;
+    }
+  return crc;
+}
+
+int
+main (void)
+{
+  if (f24 (0xFFFFFFFF) != ref (0xFFFFFFFF, 24))
+    __builtin_abort ();
+  if (f12 (0xFFFFFFFF) != ref (0xFFFFFFFF, 12))
+    __builtin_abort ();
+  if (f20 (0xFFFFFFFF) != ref (0xFFFFFFFF, 20))
+    __builtin_abort ();
+  return 0;
+}
-- 
2.54.0 (Apple Git-157)

Reply via email to