public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Fei Gao <gaofei@eswincomputing.com>
To: gcc-patches@gcc.gnu.org
Cc: kito.cheng@gmail.com, palmer@dabbelt.com, jeffreyalaw@gmail.com,
	sinan.lin@linux.alibaba.com, jiawei@iscas.ac.cn,
	Fei Gao <gaofei@eswincomputing.com>
Subject: [PATCH 2/2] [RISC-V] resolve confilct between zcmp multi push/pop and shrink-wrap-separate
Date: Tue, 20 Jun 2023 09:40:52 +0000	[thread overview]
Message-ID: <20230620094052.35993-3-gaofei@eswincomputing.com> (raw)
In-Reply-To: <20230620094052.35993-1-gaofei@eswincomputing.com>

Disable zcmp multi push/pop if shrink-wrap-separate is active.

So in -Os that prefers smaller code size, by default shrink-wrap-separate
is disabled while zcmp multi push/pop is enabled.

And in -O2 and others that prefers speed, by default shrink-wrap-separate
is enabled while zcmp multi push/pop is disabled. To force enabling zcmp multi
push/pop in this case, -fno-shrink-wrap-separate has to be explictly given.

The following TC shows the issues in -O2 before this patch with both
shrink-wrap-separate and zcmp multi push/pop active.
1. duplicated store of s regs.
2. cm.push pushes ra, s0-s11 in reverse order than what normal
   prologue does, causing stack corruption and failure to resotre s regs.

TC: zcmp_shrink_wrap_separate.c included in this patch.

output asm before this patch:
calc_func:
	cm.push	{ra, s0-s3}, -32
	...
	beq	a5,zero,.L2
	...
.L2:
	...
	sw	s1,20(sp) //issue here
	sw	s3,12(sp) //issue here
	...
	sw	s2,16(sp) //issue here

output asm after this patch:
calc_func:
	addi	sp,sp,-32
	sw	s0,24(sp)
	...
	beq	a5,zero,.L2
	...
.L2:
	...
	sw	s1,20(sp)
	sw	s3,12(sp)
	...
	sw	s2,16(sp)

Signed-off-by: Fei Gao <gaofei@eswincomputing.com>
Co-Authored-By: Zhangjin Liao <liaozhangjin@eswincomputing.com>

gcc/ChangeLog:

        * config/riscv/riscv.cc
        (riscv_avoid_shrink_wrapping_separate): wrap the condition check in
          riscv_avoid_shrink_wrapping_separate.
        (riscv_avoid_multi_push): avoid multi push if shrink_wrapping_separate
          is active.
        (riscv_get_separate_components): call riscv_avoid_shrink_wrapping_separate
---
 gcc/config/riscv/riscv.cc | 19 ++++++++++++++++---
 1 file changed, 16 insertions(+), 3 deletions(-)

diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc
index 26405b5978b..2cca5fbb62d 100644
--- a/gcc/config/riscv/riscv.cc
+++ b/gcc/config/riscv/riscv.cc
@@ -64,6 +64,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "cfghooks.h"
 #include "cfgloop.h"
 #include "cfgrtl.h"
+#include "shrink-wrap.h"
 #include "sel-sched.h"
 #include "fold-const.h"
 #include "gimple-iterator.h"
@@ -389,6 +390,7 @@ static const struct riscv_tune_param optimize_size_tune_info = {
   false,					/* use_divmod_expansion */
 };
 
+static bool riscv_avoid_shrink_wrapping_separate ();
 static tree riscv_handle_fndecl_attribute (tree *, tree, tree, int, bool *);
 static tree riscv_handle_type_attribute (tree *, tree, tree, int, bool *);
 
@@ -5032,6 +5034,8 @@ riscv_avoid_multi_push(const struct riscv_frame_info *frame)
       || cfun->machine->interrupt_handler_p
       || cfun->machine->varargs_size != 0
       || crtl->args.pretend_args_size != 0
+      || (use_shrink_wrapping_separate ()
+          && !riscv_avoid_shrink_wrapping_separate ())
       || (frame->mask & ~ MULTI_PUSH_GPR_MASK))
     return true;
 
@@ -6199,6 +6203,17 @@ riscv_epilogue_uses (unsigned int regno)
   return false;
 }
 
+static bool
+riscv_avoid_shrink_wrapping_separate ()
+{
+  if (riscv_use_save_libcall (&cfun->machine->frame)
+      || cfun->machine->interrupt_handler_p
+      || !cfun->machine->frame.gp_sp_offset.is_constant ())
+    return true;
+
+  return false;
+}
+
 /* Implement TARGET_SHRINK_WRAP_GET_SEPARATE_COMPONENTS.  */
 
 static sbitmap
@@ -6208,9 +6223,7 @@ riscv_get_separate_components (void)
   sbitmap components = sbitmap_alloc (FIRST_PSEUDO_REGISTER);
   bitmap_clear (components);
 
-  if (riscv_use_save_libcall (&cfun->machine->frame)
-      || cfun->machine->interrupt_handler_p
-      || !cfun->machine->frame.gp_sp_offset.is_constant ())
+  if (riscv_avoid_shrink_wrapping_separate ())
     return components;
 
   offset = cfun->machine->frame.gp_sp_offset.to_constant ();
-- 
2.17.1


      parent reply	other threads:[~2023-06-20  9:41 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-06-20  9:40 [PATCH 0/2] resolve confilct between RISC-V zcmp " Fei Gao
2023-06-20  9:40 ` [PATCH 1/2] allow target to check shrink-wrap-separate enabled or not Fei Gao
2023-06-25 13:36   ` Jeff Law
2023-06-26  2:29     ` Fei Gao
2023-08-28 22:03       ` Jeff Law
2023-08-28 22:32       ` Jeff Law
2023-06-20  9:40 ` Fei Gao [this message]

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=20230620094052.35993-3-gaofei@eswincomputing.com \
    --to=gaofei@eswincomputing.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jeffreyalaw@gmail.com \
    --cc=jiawei@iscas.ac.cn \
    --cc=kito.cheng@gmail.com \
    --cc=palmer@dabbelt.com \
    --cc=sinan.lin@linux.alibaba.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).