For 24-iteration CRC loops, the CRC pass builds a 24-bit data type whose
mode is SImode. The table based expanders derived the data size from the
mode, so it processed 4 bytes instead of 3 and computed wrong result.
Modified the last argument of crc table expanders to take data_bit_size
instead of mode and passed the TYPE_PRECISION of data argument which is
already built using build_nonstandard_integer_type correctly.
PR middle-end/127984
gcc/ChangeLog:
* builtins.cc (expand_builtin_crc_table_based): Update call to
pass bit size instead of mode.
* config/aarch64/aarch64.md (crc_rev<ALLI:mode><ALLX:mode>4): Likewise.
* config/i386/i386.md (crc_rev<SWI1248x:mode><SWI248x:mode>4): Likewise.
* config/loongarch/loongarch.md (crc_rev<mode>si4): Likewise.
* config/riscv/bitmanip.md (crc_rev<ANY1:mode><ANYI:mode>4): Likewise.
* expr.cc (calculate_table_based_CRC): Modified API to take
bit size instead of mode.
(calculate_table_based_reversed_CRC): Likewise.
(expand_crc_table_based): Likewise.
(expand_reversed_crc_table_based): Likewise.
* expr.h (expand_crc_table_based): Update function declaration.
(expand_reversed_crc_table_based): Likewise.
* internal-fn.cc (expand_crc_optab_fn): Update call to pass bit
size instead of mode.
gcc/testsuite/ChangeLog:
* gcc.dg/pr127984.c: New test.
Signed-off-by: Shreesh Adiga <[email protected]>
---
Changes in v2:
Retain the 24-bit crc table optimization and pass the correct bitsize.
In v1 the optimization was being disabled. Now it is correctly enabled.
gcc/builtins.cc | 5 +++--
gcc/config/aarch64/aarch64.md | 3 ++-
gcc/config/i386/i386.md | 3 ++-
gcc/config/loongarch/loongarch.md | 3 ++-
gcc/config/riscv/bitmanip.md | 2 +-
gcc/expr.cc | 30 ++++++++++++++++--------------
gcc/expr.h | 5 +++--
gcc/internal-fn.cc | 4 ++--
gcc/testsuite/gcc.dg/pr127984.c | 15 +++++++++++++++
9 files changed, 46 insertions(+), 24 deletions(-)
create mode 100644 gcc/testsuite/gcc.dg/pr127984.c
diff --git a/gcc/builtins.cc b/gcc/builtins.cc
index 3127e0e82a1..263292adfdb 100644
--- a/gcc/builtins.cc
+++ b/gcc/builtins.cc
@@ -7852,11 +7852,12 @@ expand_builtin_crc_table_based (internal_fn fn,
scalar_mode crc_mode,
op2 = convert_to_mode (crc_mode, op2, 0);
if (fn == IFN_CRC)
- expand_crc_table_based (target, op1, op2, op3, data_mode);
+ expand_crc_table_based (target, op1, op2, op3,
+ GET_MODE_BITSIZE (data_mode));
else
/* If it's IFN_CRC_REV generate bit-reversed CRC. */
expand_reversed_crc_table_based (target, op1, op2, op3,
- data_mode);
+ GET_MODE_BITSIZE (data_mode));
return target;
}
diff --git a/gcc/config/aarch64/aarch64.md b/gcc/config/aarch64/aarch64.md
index 3e08d4dc3d1..3666ab4e535 100644
--- a/gcc/config/aarch64/aarch64.md
+++ b/gcc/config/aarch64/aarch64.md
@@ -5044,7 +5044,8 @@ (define_expand "crc_rev<ALLI:mode><ALLI_2:mode>4"
else
/* Otherwise, generate table-based CRC. */
expand_reversed_crc_table_based (operands[0], operands[1], operands[2],
- operands[3], <ALLI:MODE>mode);
+ operands[3],
+ GET_MODE_BITSIZE (<ALLI:MODE>mode));
DONE;
}
)
diff --git a/gcc/config/i386/i386.md b/gcc/config/i386/i386.md
index c7fcaf1739e..b3ee363ae12 100644
--- a/gcc/config/i386/i386.md
+++ b/gcc/config/i386/i386.md
@@ -30778,7 +30778,8 @@ (define_expand "crc_rev<SWI1248x:mode><SWI248x:mode>4"
<SWI1248x:MODE>mode, operands);
else
expand_reversed_crc_table_based (operands[0], operands[1], operands[2],
- operands[3], <SWI1248x:MODE>mode);
+ operands[3],
+ GET_MODE_BITSIZE (<SWI1248x:MODE>mode));
DONE;
})
diff --git a/gcc/config/loongarch/loongarch.md
b/gcc/config/loongarch/loongarch.md
index cccb0ed8612..30d9cb3f2c4 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -4958,7 +4958,8 @@ (define_expand "crc_rev<mode>si4"
else
{
expand_reversed_crc_table_based (operands[0], operands[1],
- msg, operands[3], <MODE>mode);
+ msg, operands[3],
+ GET_MODE_BITSIZE (<MODE>mode));
}
DONE;
})
diff --git a/gcc/config/riscv/bitmanip.md b/gcc/config/riscv/bitmanip.md
index b3550d9a0d0..57fbccf99ce 100644
--- a/gcc/config/riscv/bitmanip.md
+++ b/gcc/config/riscv/bitmanip.md
@@ -1404,7 +1404,7 @@ (define_expand "crc_rev<ANYI1:mode><ANYI:mode>4"
To reflect values use standard reflecting algorithm. */
expand_reversed_crc_table_based (operands[0], operands[1],
operands[2], operands[3],
- GET_MODE (operands[2]));
+ GET_MODE_BITSIZE (<ANYI1:MODE>mode));
DONE;
})
diff --git a/gcc/expr.cc b/gcc/expr.cc
index c654387ab01..2000b591793 100644
--- a/gcc/expr.cc
+++ b/gcc/expr.cc
@@ -14711,7 +14711,8 @@ generate_reversed_crc_table (unsigned HOST_WIDE_INT
polynom,
First, using POLYNOMIAL's value generates CRC table of 256 elements,
then generates the assembly for the following code,
- where crc_bit_size and data_bit_size may be 8, 16, 32, 64, depending on CRC:
+ where crc_bit_size may be 8, 16, 32, 64 and data_bit_size multiple of 8,
+ depending on CRC:
for (int i = 0; i < data_bit_size / 8; i++)
crc = (crc << 8) ^ crc_table[(crc >> (crc_bit_size - 8))
@@ -14725,11 +14726,12 @@ generate_reversed_crc_table (unsigned HOST_WIDE_INT
polynom,
static void
calculate_table_based_CRC (rtx *crc, const rtx &input_data,
const rtx &polynomial,
- machine_mode data_mode)
+ unsigned short data_bit_size)
{
machine_mode mode = GET_MODE (*crc);
unsigned short crc_bit_size = GET_MODE_BITSIZE (mode).to_constant ();
- unsigned short data_size = GET_MODE_SIZE (data_mode).to_constant ();
+ gcc_assert (data_bit_size % 8 == 0);
+ unsigned short data_size = data_bit_size / 8;
rtx tab = generate_crc_table (UINTVAL (polynomial), crc_bit_size);
for (unsigned short i = 0; i < data_size; i++)
@@ -14739,7 +14741,7 @@ calculate_table_based_CRC (rtx *crc, const rtx
&input_data,
rtx op1 = expand_shift (RSHIFT_EXPR, mode, *crc, crc_bit_size - 8,
NULL_RTX, 1);
- /* data >> (8 * (GET_MODE_SIZE (data_mode).to_constant () - i - 1)). */
+ /* data >> (8 * (data_size - i - 1)). */
unsigned range_8 = 8 * (data_size - i - 1);
/* CRC's mode is always at least as wide as INPUT_DATA. Convert
INPUT_DATA into CRC's mode. */
@@ -14747,8 +14749,7 @@ calculate_table_based_CRC (rtx *crc, const rtx
&input_data,
convert_move (data, input_data, 1);
data = expand_shift (RSHIFT_EXPR, mode, data, range_8, NULL_RTX, 1);
- /* data >> (8 * (GET_MODE_SIZE (mode)
- .to_constant () - i - 1)) & 0xFF. */
+ /* data >> (8 * (data_size - i - 1)) & 0xFF. */
rtx data_final = expand_and (mode, data,
gen_int_mode (255, mode), NULL_RTX);
@@ -14796,11 +14797,12 @@ calculate_table_based_CRC (rtx *crc, const rtx
&input_data,
static void
calculate_table_based_reversed_CRC (rtx *crc, const rtx &input_data,
const rtx &polynomial,
- machine_mode data_mode)
+ unsigned short data_bit_size)
{
machine_mode mode = GET_MODE (*crc);
unsigned short crc_bit_size = GET_MODE_BITSIZE (mode).to_constant ();
- unsigned short data_size = GET_MODE_SIZE (data_mode).to_constant ();
+ gcc_assert (data_bit_size % 8 == 0);
+ unsigned short data_size = data_bit_size / 8;
rtx tab = generate_reversed_crc_table (UINTVAL (polynomial), crc_bit_size);
/* CRC's mode is always at least as wide as INPUT_DATA. Convert
@@ -14855,7 +14857,7 @@ calculate_table_based_reversed_CRC (rtx *crc, const rtx
&input_data,
CRC is OP1, data is OP2 and the polynomial is OP3.
This must generate a CRC table and an assembly for the following code,
- where crc_bit_size and data_bit_size may be 8, 16, 32, 64:
+ where crc_bit_size may be 8, 16, 32, 64 and data_bit_size multiple of 8:
uint_crc_bit_size_t
crc_crc_bit_size (uint_crc_bit_size_t crc_init,
uint_data_bit_size_t data, size_t size)
@@ -14870,14 +14872,14 @@ calculate_table_based_reversed_CRC (rtx *crc, const
rtx &input_data,
void
expand_crc_table_based (rtx op0, rtx op1, rtx op2, rtx op3,
- machine_mode data_mode)
+ unsigned short data_bit_size)
{
gcc_assert (!CONST_INT_P (op0));
gcc_assert (CONST_INT_P (op3));
machine_mode crc_mode = GET_MODE (op0);
rtx crc = gen_reg_rtx (crc_mode);
convert_move (crc, op1, 0);
- calculate_table_based_CRC (&crc, op2, op3, data_mode);
+ calculate_table_based_CRC (&crc, op2, op3, data_bit_size);
convert_move (op0, crc, 0);
}
@@ -14886,7 +14888,7 @@ expand_crc_table_based (rtx op0, rtx op1, rtx op2, rtx
op3,
CRC is OP1, data is OP2 and the polynomial is OP3.
This generates a reversed CRC table and assembly for the following code,
- where crc_bit_size and data_bit_size may be 8, 16, 32, 64:
+ where crc_bit_size may be 8, 16, 32, 64 and data_bit_size multiple of 8:
uint_crc_bit_size_t
crc_crc_bit_size (uint_crc_bit_size_t crc_init,
uint_data_bit_size_t data)
@@ -14902,13 +14904,13 @@ expand_crc_table_based (rtx op0, rtx op1, rtx op2,
rtx op3,
void
expand_reversed_crc_table_based (rtx op0, rtx op1, rtx op2, rtx op3,
- machine_mode data_mode)
+ unsigned short data_bit_size)
{
gcc_assert (!CONST_INT_P (op0));
gcc_assert (CONST_INT_P (op3));
machine_mode crc_mode = GET_MODE (op0);
rtx crc = gen_reg_rtx (crc_mode);
convert_move (crc, op1, 0);
- calculate_table_based_reversed_CRC (&crc, op2, op3, data_mode);
+ calculate_table_based_reversed_CRC (&crc, op2, op3, data_bit_size);
convert_move (op0, crc, 0);
}
diff --git a/gcc/expr.h b/gcc/expr.h
index 55d72cb9510..49d303586cf 100644
--- a/gcc/expr.h
+++ b/gcc/expr.h
@@ -402,8 +402,9 @@ extern unsigned HOST_WIDE_INT
gf2n_poly_long_div_quotient (unsigned HOST_WIDE_INT, unsigned short);
/* Generate table-based CRC. */
-extern void expand_crc_table_based (rtx, rtx, rtx, rtx, machine_mode);
-extern void expand_reversed_crc_table_based (rtx, rtx, rtx, rtx, machine_mode);
+extern void expand_crc_table_based (rtx, rtx, rtx, rtx, unsigned short);
+extern void expand_reversed_crc_table_based (rtx, rtx, rtx, rtx,
+ unsigned short);
/* Cache of the "extended" flag in the target's _BitInt description
for use during expand. */
diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc
index 94a601d34d6..2003ddc738e 100644
--- a/gcc/internal-fn.cc
+++ b/gcc/internal-fn.cc
@@ -4223,11 +4223,11 @@ expand_crc_optab_fn (internal_fn fn, gcall *stmt,
convert_optab optab)
/* If it's IFN_CRC generate bit-forward CRC. */
if (fn == IFN_CRC)
expand_crc_table_based (dest, crc, data, polynomial,
- TYPE_MODE (data_type));
+ TYPE_PRECISION (data_type));
else
/* If it's IFN_CRC_REV generate bit-reversed CRC. */
expand_reversed_crc_table_based (dest, crc, data, polynomial,
- TYPE_MODE (data_type));
+ TYPE_PRECISION (data_type));
/* Now get the return value where it needs to be, taking care to
ensure it's promoted appropriately if the ABI demands it.
diff --git a/gcc/testsuite/gcc.dg/pr127984.c b/gcc/testsuite/gcc.dg/pr127984.c
new file mode 100644
index 00000000000..8f08185d612
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr127984.c
@@ -0,0 +1,15 @@
+/* PR middle-end/127984 */
+/* { dg-options "-O2" } */
+/* { dg-do run } */
+
+__attribute__((noipa)) unsigned f(unsigned crc) {
+ for (int i = 0; i < 24; i++) {
+ if (crc & 1) crc = (crc >> 1) ^ 0xEDB88320; else crc >>= 1;
+ }
+ return crc;
+}
+
+int main(void) {
+ if (f(0xFFFFFFFF) != 0xBE26ED)
+ __builtin_abort();
+}
--
2.56.1