RISC-V: Disallow regrenme if the TO register never used before for interrupt functions
authorKito Cheng <kito.cheng@sifive.com>
Fri, 17 Jan 2020 11:49:15 +0000 (19:49 +0800)
committerKito Cheng <kito.cheng@sifive.com>
Tue, 21 Jan 2020 02:40:47 +0000 (10:40 +0800)
gcc/ChangeLog

PR target/93304
* config/riscv/riscv-protos.h (riscv_hard_regno_rename_ok): New.
* config/riscv/riscv.c (riscv_hard_regno_rename_ok): New.
* config/riscv/riscv.h (HARD_REGNO_RENAME_OK): Defined.

gcc/testsuite/ChangeLog

PR target/93304
* gcc.target/riscv/pr93304.c: New test.

gcc/ChangeLog
gcc/config/riscv/riscv-protos.h
gcc/config/riscv/riscv.c
gcc/config/riscv/riscv.h
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.target/riscv/pr93304.c [new file with mode: 0644]

index 31b7305b5f46ce8a2780f037ff4d28f6e67aef44..0581561f63ddcd1184e3ce5225ac0ab7da08249c 100644 (file)
@@ -1,3 +1,10 @@
+2020-01-21  Kito Cheng  <kito.cheng@sifive.com>
+
+       PR target/93304
+       * config/riscv/riscv-protos.h (riscv_hard_regno_rename_ok): New.
+       * config/riscv/riscv.c (riscv_hard_regno_rename_ok): New.
+       * config/riscv/riscv.h (HARD_REGNO_RENAME_OK): Defined.
+
 2020-01-20  Wilco Dijkstra  <wdijkstr@arm.com>
 
        * config/aarch64/aarch64.c (neoversen1_tunings): Set jump_align to 4.
index 26b811007a99932ce5209a5e23970c9b441aa0ea..8cf9137b5e7e9f985abc44168b67ba8f153a2f2e 100644 (file)
@@ -89,4 +89,6 @@ extern void riscv_init_builtins (void);
 /* Routines implemented in riscv-common.c.  */
 extern std::string riscv_arch_str ();
 
+extern bool riscv_hard_regno_rename_ok (unsigned, unsigned);
+
 #endif /* ! GCC_RISCV_PROTOS_H */
index 320a70b9c863625d50da2571d322bdb0f4ab1cc9..573024074e3f8d606cb1162505d4cc072950d00b 100644 (file)
@@ -5021,6 +5021,19 @@ riscv_reorg (void)
     riscv_remove_unneeded_save_restore_calls ();
 }
 
+/* Return nonzero if register FROM_REGNO can be renamed to register
+   TO_REGNO.  */
+
+bool
+riscv_hard_regno_rename_ok (unsigned from_regno ATTRIBUTE_UNUSED,
+                           unsigned to_regno)
+{
+  /* Interrupt functions can only use registers that have already been
+     saved by the prologue, even if they would normally be
+     call-clobbered.  */
+  return !cfun->machine->interrupt_handler_p || df_regs_ever_live_p (to_regno);
+}
+
 /* Initialize the GCC target structure.  */
 #undef TARGET_ASM_ALIGNED_HI_OP
 #define TARGET_ASM_ALIGNED_HI_OP "\t.half\t"
index 0bf3d2f81a81696e25e1f6335dbd396db758f0b4..19438e28fe88b92d91b610e9a1760c13f1d88be5 100644 (file)
@@ -926,4 +926,6 @@ extern unsigned riscv_stack_boundary;
 
 extern void riscv_remove_unneeded_save_restore_calls (void);
 
+#define HARD_REGNO_RENAME_OK(FROM, TO) riscv_hard_regno_rename_ok (FROM, TO)
+
 #endif /* ! GCC_RISCV_H */
index 452c16eb8c5ed7b4181a02800bb3dbc7b55e38cd..fa457c86879e137f3886e6dc6b84422447e32ce7 100644 (file)
@@ -1,3 +1,8 @@
+2020-01-21  Kito Cheng  <kito.cheng@sifive.com>
+
+       PR target/93304
+       * gcc.target/riscv/pr93304.c: New test.
+
 2020-01-20  Martin Sebor  <msebor@redhat.com>
 
        PR testsuite/92829
diff --git a/gcc/testsuite/gcc.target/riscv/pr93304.c b/gcc/testsuite/gcc.target/riscv/pr93304.c
new file mode 100644 (file)
index 0000000..f771e48
--- /dev/null
@@ -0,0 +1,19 @@
+/* Verify the regrename won't rename registers to register which never used
+   before.  */
+/* { dg-do compile } */
+/* { dg-options "-O -frename-registers" } */
+
+static unsigned _t = 0;
+
+void __attribute__ ((interrupt))
+foo (void)
+{
+  _t++;
+}
+
+/* Register rename will try to use registers from the lower register
+   regradless of the REG_ALLOC_ORDER.
+   In theory, t0-t6 should not used in such small program if regrename
+   not executed incorrectly, because a5-a0 has higher priority in
+   REG_ALLOC_ORDER.  */
+/* { dg-final { scan-assembler-not "t\[0-6\]" } } */