]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[RISC-V][PR middle-end/118084] Fix brev based reflection code
authorJeff Law <jlaw@ventanamicro.com>
Sat, 21 Dec 2024 15:33:36 +0000 (08:33 -0700)
committerJeff Law <jlaw@ventanamicro.com>
Sat, 21 Dec 2024 15:35:26 +0000 (08:35 -0700)
The fuzzer tripped over a risc-v target issue in the expansion of CRCs.
In particular we want to use brev instruction to improve the reflection
code.

In the case where the item to be reflected is smaller than a word we
would end up triggering an ICE due to mode mismatching since the
expansion code asks for the operation in word_mode.

I was briefly confused by the multiple calls into this code, but we have
to reflect multiple values and those calls may be reflecting different
sized items.  So seeing one in SI, then another in QI is sensible.

The fix is pretty simple.  In theory the item being reflected should
always be word_size or smaller.  So an assertion is added to verify
that.  If the item's size is smaller than a word, we can use a
paradoxical subreg.  The logical right shift after the brev should zero
out any extraneous bits.

It's unclear why we're passing a pointer to an RTX in this code.  I left
that as-is, but we can simplify the code a little bit by doing the
dereference early and using the dereferenced value.

PR middle-end/118084
gcc/
* config/riscv/riscv.cc (generate_reflecting_code_using_brev): Handle
sub-word sized objects correctly.

gcc/testsuite/
* gcc.target/riscv/pr118084.c: New test.

gcc/config/riscv/riscv.cc
gcc/testsuite/gcc.target/riscv/pr118084.c [new file with mode: 0644]

index 4f1f9defc8017226ce9d501127762b0c30910ae3..1374868eddfbc63ed2a9849eb18aafc7bff5a10c 100644 (file)
@@ -13602,18 +13602,27 @@ riscv_use_by_pieces_infrastructure_p (unsigned HOST_WIDE_INT size,
    we will get the desired one: 1000 1111.  */
 
 void
-generate_reflecting_code_using_brev (rtx *op)
+generate_reflecting_code_using_brev (rtx *op_p)
 {
-  machine_mode op_mode = GET_MODE (*op);
+  rtx op = *op_p;
+  machine_mode op_mode = GET_MODE (op);
+
+  /* OP may be smaller than a word.  We can use a paradoxical subreg
+     to compensate for that.  It should never be larger than a word
+     for RISC-V.  */
+  gcc_assert (op_mode <= word_mode);
+  if (op_mode != word_mode)
+    op = gen_lowpart (word_mode, op);
+
   HOST_WIDE_INT shift_val = (BITS_PER_WORD
                             - GET_MODE_BITSIZE (op_mode).to_constant ());
-  riscv_expand_op (BSWAP, word_mode, *op, *op, *op);
-  riscv_expand_op (LSHIFTRT, word_mode, *op, *op,
+  riscv_expand_op (BSWAP, word_mode, op, op, op);
+  riscv_expand_op (LSHIFTRT, word_mode, op, op,
                   gen_int_mode (shift_val, word_mode));
   if (TARGET_64BIT)
-    emit_insn (gen_riscv_brev8_di (*op, *op));
+    emit_insn (gen_riscv_brev8_di (op, op));
   else
-    emit_insn (gen_riscv_brev8_si (*op, *op));
+    emit_insn (gen_riscv_brev8_si (op, op));
 }
 
 
diff --git a/gcc/testsuite/gcc.target/riscv/pr118084.c b/gcc/testsuite/gcc.target/riscv/pr118084.c
new file mode 100644 (file)
index 0000000..2ffa1d7
--- /dev/null
@@ -0,0 +1,13 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -march=rv32izk -mabi=ilp32 -Os" } */
+unsigned a;
+int main() {
+  int b = 8;
+  for (; b; b--)
+    if (a & 1)
+      a = a >> 1 ^ 30196000;
+    else
+      a >>= 1;
+}
+
+