public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Kito Cheng <kito.cheng@sifive.com>
To: gcc-patches@gcc.gnu.org, kito.cheng@gmail.com,
	palmer@dabbelt.com, jeffreyalaw@gmail.com
Cc: Kito Cheng <kito.cheng@sifive.com>
Subject: [PATCH] RISC-V: Save/restore ra register correctly [PR112478]
Date: Tue, 14 Nov 2023 22:14:55 +0800	[thread overview]
Message-ID: <20231114141455.24465-1-kito.cheng@sifive.com> (raw)

We set ra to fixed register now, but we still need to save/restore that at
prologue/epilogue if that has used.

gcc/ChangeLog:

	PR target/112478
	* config/riscv/riscv.cc (riscv_save_return_addr_reg_p): Check ra
	is ever lived.

gcc/testsuite/gcc/ChangeLog:

	PR target/112478
	* riscv/pr112478.c: New.
---
 gcc/config/riscv/riscv.cc                 | 4 ++++
 gcc/testsuite/gcc.target/riscv/pr112478.c | 8 ++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr112478.c

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index ecee7eb4727..f09c4066903 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -5802,6 +5802,10 @@ riscv_save_return_addr_reg_p (void)
   if (riscv_far_jump_used_p ())
     return true;
 
+  /* We need to save it if anyone has used that.  */
+  if (df_regs_ever_live_p (RETURN_ADDR_REGNUM))
+    return true;
+
   /* Need not to use ra for leaf when frame pointer is turned off by
      option whatever the omit-leaf-frame's value.  */
   if (frame_pointer_needed && crtl->is_leaf
diff --git a/gcc/testsuite/gcc.target/riscv/pr112478.c b/gcc/testsuite/gcc.target/riscv/pr112478.c
new file mode 100644
index 00000000000..0bbde20b71b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr112478.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-ffat-lto-objects" } */
+
+void foo() {
+    asm volatile("# " : ::"ra");
+}
+
+/* { dg-final { scan-assembler "s(w|d)\[ \t\]*ra" } } */
-- 
2.40.1


             reply	other threads:[~2023-11-14 14:15 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-14 14:14 Kito Cheng [this message]
2023-11-15 23:19 ` Christoph Müllner
2023-11-16 11:39   ` Kito Cheng

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231114141455.24465-1-kito.cheng@sifive.com \
    --to=kito.cheng@sifive.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=kito.cheng@gmail.com \
    --cc=palmer@dabbelt.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).