diff mbox series

[v2] RISC-V: fix zcmp popretz [PR113715]

Message ID 20240709100029.12904-1-gaofei@eswincomputing.com
State New
Headers show
Series [v2] RISC-V: fix zcmp popretz [PR113715] | expand

Commit Message

Fei Gao July 9, 2024, 10 a.m. UTC
No functional changes compared with V1, just spaces to table conversion
in testcases to pass check-function-bodies.

V1 passed regression locally but suprisingly failed in pre-commit CI, after
picking the patch from patchwork, I realize table got coverted to spaces
before sending the patch.

Root cause:
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b27d323a368033f0b37e93c57a57a35fd9997864
Commit above tries in targetm.gen_epilogue () to detect if
there's li a0,0 insn at the end of insn chain, if so, cm.popret
is replaced by cm.popretz and li a0,0 insn is deleted.

Insertion of the generated epilogue sequence
into the insn chain doesn't happen at this moment.
If later shrink-wrap decides NOT to insert the epilogue sequence at the end
of insn chain, then the li a0,0 insn has already been mistakeny removed.

Fix this issue by removing generation of cm.popretz in epilogue,
leaving the assignment to a0 and use insn with cm.popret.

That's likely going to result in some kind of code size regression,
but not a correctness regression.

Optimization can be done in future.

Signed-off-by: Fei Gao <gaofei@eswincomputing.com>
gcc/ChangeLog:

	* config/riscv/riscv.cc (riscv_zcmp_can_use_popretz): Removed.
	(riscv_gen_multi_pop_insn): Remove generation of cm.popretz.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/rv32e_zcmp.c: Adapt TC.
	* gcc.target/riscv/rv32i_zcmp.c: Likewise.
---
 gcc/config/riscv/riscv.cc                   | 53 ---------------------
 gcc/testsuite/gcc.target/riscv/rv32e_zcmp.c |  3 +-
 gcc/testsuite/gcc.target/riscv/rv32i_zcmp.c |  3 +-
 3 files changed, 4 insertions(+), 55 deletions(-)

Comments

Jeff Law July 9, 2024, 3:20 p.m. UTC | #1
On 7/9/24 4:00 AM, Fei Gao wrote:
> No functional changes compared with V1, just spaces to table conversion
> in testcases to pass check-function-bodies.
> 
> V1 passed regression locally but suprisingly failed in pre-commit CI, after
> picking the patch from patchwork, I realize table got coverted to spaces
> before sending the patch.
Thanks for chasing this down!

> 
> Root cause:
> https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=b27d323a368033f0b37e93c57a57a35fd9997864
> Commit above tries in targetm.gen_epilogue () to detect if
> there's li a0,0 insn at the end of insn chain, if so, cm.popret
> is replaced by cm.popretz and li a0,0 insn is deleted.
> 
> Insertion of the generated epilogue sequence
> into the insn chain doesn't happen at this moment.
> If later shrink-wrap decides NOT to insert the epilogue sequence at the end
> of insn chain, then the li a0,0 insn has already been mistakeny removed.
> 
> Fix this issue by removing generation of cm.popretz in epilogue,
> leaving the assignment to a0 and use insn with cm.popret.
> 
> That's likely going to result in some kind of code size regression,
> but not a correctness regression.
> 
> Optimization can be done in future.
> 
> Signed-off-by: Fei Gao <gaofei@eswincomputing.com>
> gcc/ChangeLog:
> 
> 	* config/riscv/riscv.cc (riscv_zcmp_can_use_popretz): Removed.
> 	(riscv_gen_multi_pop_insn): Remove generation of cm.popretz.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/riscv/rv32e_zcmp.c: Adapt TC.
> 	* gcc.target/riscv/rv32i_zcmp.c: Likewise.
This is OK for the trunk as well.  It looks like CI is almost done.


jeff
diff mbox series

Patch

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 38ed773c222..61fa74e9322 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -8167,52 +8167,6 @@  riscv_adjust_libcall_cfi_epilogue ()
   return dwarf;
 }
 
-/* return true if popretz pattern can be matched.
-   set (reg 10 a0) (const_int 0)
-   use (reg 10 a0)
-   NOTE_INSN_EPILOGUE_BEG  */
-static rtx_insn *
-riscv_zcmp_can_use_popretz (void)
-{
-  rtx_insn *insn = NULL, *use = NULL, *clear = NULL;
-
-  /* sequence stack for NOTE_INSN_EPILOGUE_BEG*/
-  struct sequence_stack *outer_seq = get_current_sequence ()->next;
-  if (!outer_seq)
-    return NULL;
-  insn = outer_seq->first;
-  if (!insn || !NOTE_P (insn) || NOTE_KIND (insn) != NOTE_INSN_EPILOGUE_BEG)
-    return NULL;
-
-  /* sequence stack for the insn before NOTE_INSN_EPILOGUE_BEG*/
-  outer_seq = outer_seq->next;
-  if (outer_seq)
-    insn = outer_seq->last;
-
-  /* skip notes  */
-  while (insn && NOTE_P (insn))
-    {
-      insn = PREV_INSN (insn);
-    }
-  use = insn;
-
-  /* match use (reg 10 a0)  */
-  if (use == NULL || !INSN_P (use) || GET_CODE (PATTERN (use)) != USE
-      || !REG_P (XEXP (PATTERN (use), 0))
-      || REGNO (XEXP (PATTERN (use), 0)) != A0_REGNUM)
-    return NULL;
-
-  /* match set (reg 10 a0) (const_int 0 [0])  */
-  clear = PREV_INSN (use);
-  if (clear != NULL && INSN_P (clear) && GET_CODE (PATTERN (clear)) == SET
-      && REG_P (SET_DEST (PATTERN (clear)))
-      && REGNO (SET_DEST (PATTERN (clear))) == A0_REGNUM
-      && SET_SRC (PATTERN (clear)) == const0_rtx)
-    return clear;
-
-  return NULL;
-}
-
 static void
 riscv_gen_multi_pop_insn (bool use_multi_pop_normal, unsigned mask,
 			  unsigned multipop_size)
@@ -8223,13 +8177,6 @@  riscv_gen_multi_pop_insn (bool use_multi_pop_normal, unsigned mask,
   if (!use_multi_pop_normal)
     insn = emit_insn (
       riscv_gen_multi_push_pop_insn (POP_IDX, multipop_size, regs_count));
-  else if (rtx_insn *clear_a0_insn = riscv_zcmp_can_use_popretz ())
-    {
-      delete_insn (NEXT_INSN (clear_a0_insn));
-      delete_insn (clear_a0_insn);
-      insn = emit_jump_insn (
-	riscv_gen_multi_push_pop_insn (POPRETZ_IDX, multipop_size, regs_count));
-    }
   else
     insn = emit_jump_insn (
       riscv_gen_multi_push_pop_insn (POPRET_IDX, multipop_size, regs_count));
diff --git a/gcc/testsuite/gcc.target/riscv/rv32e_zcmp.c b/gcc/testsuite/gcc.target/riscv/rv32e_zcmp.c
index 50e443573ad..0af4d7199f6 100644
--- a/gcc/testsuite/gcc.target/riscv/rv32e_zcmp.c
+++ b/gcc/testsuite/gcc.target/riscv/rv32e_zcmp.c
@@ -259,7 +259,8 @@  foo (void)
 **test_popretz:
 **	cm.push	{ra}, -16
 **	call	f1
-**	cm.popretz	{ra}, 16
+**	li	a0,0
+**	cm.popret	{ra}, 16
 */
 long
 test_popretz ()
diff --git a/gcc/testsuite/gcc.target/riscv/rv32i_zcmp.c b/gcc/testsuite/gcc.target/riscv/rv32i_zcmp.c
index 1e1a8be8705..723889f49df 100644
--- a/gcc/testsuite/gcc.target/riscv/rv32i_zcmp.c
+++ b/gcc/testsuite/gcc.target/riscv/rv32i_zcmp.c
@@ -259,7 +259,8 @@  foo (void)
 **test_popretz:
 **	cm.push	{ra}, -16
 **	call	f1
-**	cm.popretz	{ra}, 16
+**	li	a0,0
+**	cm.popret	{ra}, 16
 */
 long
 test_popretz ()