RISC-V: Add -mpreferred-stack-boundary option.

Message ID 20180123230719.17443-1-jimw@sifive.com
State New
Headers show
Series
  • RISC-V: Add -mpreferred-stack-boundary option.
Related show

Commit Message

Jim Wilson Jan. 23, 2018, 11:07 p.m.
This adds a -mpreferred-stack-boundary option patterned after the one supported
by the x86 port.  This allows one to reduce the size of stack frames to reduce
memory usage.

This was tested with a rv32i/ilp32 target using -mpreferred-stack-boundary=3
to get 8-byte alignment instead of the default 16-byte alignment.  There were
no regressions other than gcc.dg/stack-usage-1.c which is expected to fail as
it checks for the default stack frame size.

Committed.

Jim

	2018-01-23  Andrew Waterman  <andrew@sifive.com>
	gcc/
	* config/riscv/riscv.c (riscv_stack_boundary): New.
	(riscv_option_override): Set riscv_stack_boundary.  Handle
	riscv_preferred_stack_boundary_arg.
	* config/riscv/riscv.h (MIN_STACK_BOUNDARY, ABI_STACK_BOUNDARY): New.
	(BIGGEST_ALIGNMENT): Set to STACK_BOUNDARY.
	(STACK_BOUNDARY): Set to riscv_stack_boundary.
	(RISCV_STACK_ALIGN): Use STACK_BOUNDARY.
	* config/riscv/riscv.opt (mpreferred-stack-boundary): New.
	* doc/invoke.tex (RISC-V Options): Add -mpreferred-stack-boundary.
---
 gcc/config/riscv/riscv.c   | 17 +++++++++++++++++
 gcc/config/riscv/riscv.h   | 17 ++++++++++++-----
 gcc/config/riscv/riscv.opt |  4 ++++
 gcc/doc/invoke.texi        | 11 +++++++++++
 4 files changed, 44 insertions(+), 5 deletions(-)

-- 
2.14.1

Patch

diff --git a/gcc/config/riscv/riscv.c b/gcc/config/riscv/riscv.c
index 20660a4061a..4ef7a1774c4 100644
--- a/gcc/config/riscv/riscv.c
+++ b/gcc/config/riscv/riscv.c
@@ -222,6 +222,9 @@  struct riscv_cpu_info {
 /* Whether unaligned accesses execute very slowly.  */
 bool riscv_slow_unaligned_access_p;
 
+/* Stack alignment to assume/maintain.  */
+unsigned riscv_stack_boundary;
+
 /* Which tuning parameters to use.  */
 static const struct riscv_tune_info *tune_info;
 
@@ -4111,6 +4114,20 @@  riscv_option_override (void)
   /* We do not yet support ILP32 on RV64.  */
   if (BITS_PER_WORD != POINTER_SIZE)
     error ("ABI requires -march=rv%d", POINTER_SIZE);
+
+  /* Validate -mpreferred-stack-boundary= value.  */
+  riscv_stack_boundary = ABI_STACK_BOUNDARY;
+  if (riscv_preferred_stack_boundary_arg)
+    {
+      int min = ctz_hwi (MIN_STACK_BOUNDARY / 8);
+      int max = 8;
+
+      if (!IN_RANGE (riscv_preferred_stack_boundary_arg, min, max))
+	error ("-mpreferred-stack-boundary=%d must be between %d and %d",
+	       riscv_preferred_stack_boundary_arg, min, max);
+
+      riscv_stack_boundary = 8 << riscv_preferred_stack_boundary_arg;
+    }
 }
 
 /* Implement TARGET_CONDITIONAL_REGISTER_USAGE.  */
diff --git a/gcc/config/riscv/riscv.h b/gcc/config/riscv/riscv.h
index cd37761b629..a002bff4480 100644
--- a/gcc/config/riscv/riscv.h
+++ b/gcc/config/riscv/riscv.h
@@ -123,8 +123,14 @@  along with GCC; see the file COPYING3.  If not see
 /* Allocation boundary (in *bits*) for the code of a function.  */
 #define FUNCTION_BOUNDARY (TARGET_RVC ? 16 : 32)
 
+/* The smallest supported stack boundary the calling convention supports.  */
+#define MIN_STACK_BOUNDARY (2 * BITS_PER_WORD)
+
+/* The ABI stack alignment.  */
+#define ABI_STACK_BOUNDARY 128
+
 /* There is no point aligning anything to a rounder boundary than this.  */
-#define BIGGEST_ALIGNMENT 128
+#define BIGGEST_ALIGNMENT STACK_BOUNDARY
 
 /* The user-level ISA permits unaligned accesses, but they are not required
    of the privileged architecture.  */
@@ -472,8 +478,8 @@  enum reg_class
    `crtl->outgoing_args_size'.  */
 #define OUTGOING_REG_PARM_STACK_SPACE(FNTYPE) 1
 
-#define STACK_BOUNDARY 128
-
+#define STACK_BOUNDARY riscv_stack_boundary
+
 /* Symbolic macros for the registers used to return integer and floating
    point values.  */
 
@@ -528,8 +534,9 @@  typedef struct {
 
 #define EPILOGUE_USES(REGNO)	((REGNO) == RETURN_ADDR_REGNUM)
 
-/* ABI requires 16-byte alignment, even on RV32. */
-#define RISCV_STACK_ALIGN(LOC) (((LOC) + 15) & -16)
+/* Align based on stack boundary, which might have been set by the user.  */
+#define RISCV_STACK_ALIGN(LOC) \
+  (((LOC) + ((STACK_BOUNDARY/8)-1)) & -(STACK_BOUNDARY/8))
 
 /* EXIT_IGNORE_STACK should be nonzero if, when returning from a function,
    the stack pointer does not matter.  The value is tested only in
diff --git a/gcc/config/riscv/riscv.opt b/gcc/config/riscv/riscv.opt
index 50df851afdf..581a26bb5c1 100644
--- a/gcc/config/riscv/riscv.opt
+++ b/gcc/config/riscv/riscv.opt
@@ -33,6 +33,10 @@  mabi=
 Target Report RejectNegative Joined Enum(abi_type) Var(riscv_abi) Init(ABI_ILP32)
 Specify integer and floating-point calling convention.
 
+mpreferred-stack-boundary=
+Target RejectNegative Joined UInteger Var(riscv_preferred_stack_boundary_arg)
+Attempt to keep stack aligned to this power of 2.
+
 Enum
 Name(abi_type) Type(enum riscv_abi_type)
 Supported ABIs (for use with the -mabi= option):
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 891de733160..f066349c2ff 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -992,6 +992,7 @@  See RS/6000 and PowerPC Options.
 -mdiv  -mno-div @gol
 -march=@var{ISA-string} @gol
 -mtune=@var{processor-string} @gol
+-mpreferred-stack-boundary=@var{num} @gol
 -msmall-data-limit=@var{N-bytes} @gol
 -msave-restore  -mno-save-restore @gol
 -mstrict-align -mno-strict-align @gol
@@ -22072,6 +22073,16 @@  lower-case.  Examples include @samp{rv64i}, @samp{rv32g}, and @samp{rv32imaf}.
 Optimize the output for the given processor, specified by microarchitecture
 name.
 
+@item -mpreferred-stack-boundary=@var{num}
+@opindex mpreferred-stack-boundary
+Attempt to keep the stack boundary aligned to a 2 raised to @var{num}
+byte boundary.  If @option{-mpreferred-stack-boundary} is not specified,
+the default is 4 (16 bytes or 128-bits).
+
+@strong{Warning:} If you use this switch, then you must build all modules with
+the same value, including any libraries.  This includes the system libraries
+and startup modules.
+
 @item -msmall-data-limit=@var{n}
 @opindex msmall-data-limit
 Put global and static data smaller than @var{n} bytes into a special section