https://gcc.gnu.org/g:f97104c61a6cf4770f4d397f91a6e78426720e28
commit r17-6511-gf97104c61a6cf4770f4d397f91a6e78426720e28 Author: Shreesh Adiga <[email protected]> Date: Fri Nov 20 16:56:37 2026 -0700 [PATCH v2] middle-end: Fix CRC computation for 24-iteration loops [PR127984] 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]> Diff: --- 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(-) diff --git a/gcc/builtins.cc b/gcc/builtins.cc index 128a12a898fc..2a6d04026d34 100644 --- a/gcc/builtins.cc +++ b/gcc/builtins.cc @@ -7851,11 +7851,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 951f1e008045..bafccafdb53b 100644 --- a/gcc/config/aarch64/aarch64.md +++ b/gcc/config/aarch64/aarch64.md @@ -5068,7 +5068,8 @@ 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 048998566aed..ef8e9a21348f 100644 --- a/gcc/config/i386/i386.md +++ b/gcc/config/i386/i386.md @@ -30997,7 +30997,8 @@ <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 de18494af0b0..ade2383ba93d 100644 --- a/gcc/config/loongarch/loongarch.md +++ b/gcc/config/loongarch/loongarch.md @@ -4928,7 +4928,8 @@ 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 c1feb703e95e..5635c9911e48 100644 --- a/gcc/config/riscv/bitmanip.md +++ b/gcc/config/riscv/bitmanip.md @@ -1493,7 +1493,7 @@ 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 f0fe68dee1b7..2148c03194b8 100644 --- a/gcc/expr.cc +++ b/gcc/expr.cc @@ -14734,7 +14734,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)) @@ -14748,11 +14749,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++) @@ -14762,7 +14764,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. */ @@ -14770,8 +14772,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); @@ -14819,11 +14820,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 @@ -14878,7 +14880,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) @@ -14893,14 +14895,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); } @@ -14909,7 +14911,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) @@ -14925,13 +14927,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 a0194c96591d..7609754c6352 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 1d3163d1cac1..fac89c542c59 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -4288,11 +4288,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 000000000000..8f08185d6128 --- /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(); +}
