summaryrefslogtreecommitdiff
path: root/gcc/function.c
diff options
context:
space:
mode:
authorRenlin Li <renlin.li@arm.com>2018-11-21 14:29:19 +0000
committerRenlin Li <renlin@gcc.gnu.org>2018-11-21 14:29:19 +0000
commit0358d788d238ba7407648962f40026bd8c190308 (patch)
treebcd09aa19909fcbc6ad37c8776a9914f1d3d98ac /gcc/function.c
parent566422e03b34c8c934b878bf7b0af2fab3b1fb5f (diff)
[PATCH][PR84877]Dynamically align the address for local parameter copy on the stack when required alignment is larger than MAX_SUPPORTED_STACK_ALIGNMENT
As described in PR84877. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84877 The local copy of parameter on stack is not aligned. For BLKmode paramters, a local copy on the stack will be saved. There are three cases: 1) arguments passed partially on the stack, partially via registers. 2) arguments passed fully on the stack. 3) arguments passed via registers. After the change here, in all three cases, the stack slot for the local parameter copy is aligned by the data type. The stack slot is the DECL_RTL of the parameter. All the references thereafter in the function will refer to this RTL. To populate the local copy on the stack, For case 1) and 2), there are operations to move data from the caller's stack (from incoming rtl) into callee's stack. For case 3), the registers are directly saved into the stack slot. In all cases, the destination address is properly aligned. But for case 1) and case 2), the source address is not aligned by the type. It is defined by the PCS how the arguments are prepared. The block move operation is fulfilled by emit_block_move (). As far as I can see, it will use the smaller alignment of source and destination. This looks fine as long as we don't use instructions which requires a strict larger alignment than the address actually has. Here, it only changes receiving parameters. The function assign_stack_local_1 will be called in various places. Usually, the caller will constraint the ALIGN parameter. For example via STACK_SLOT_ALIGNMENT macro. assign_parm_setup_block will call assign_stack_local () with alignment from the parameter type which in this case could be larger than MAX_SUPPORTED_STACK_ALIGNMENT. The alignment operation for parameter copy on the stack is similar to stack vars. First, enough space is reserved on the stack. The size is fixed at compile time. Instructions are emitted to dynamically get an aligned address at runtime within this piece of memory. This will unavoidably increase the usage of stack. However, it really depends on how many over-aligned parameters are passed by value. gcc/ 2018-11-21 Renlin Li <renlin.li@arm.com> PR middle-end/84877 * explow.h (get_dynamic_stack_size): Declare it as external. * explow.c (record_new_stack_level): Remove function static attribute. * function.c (assign_stack_local_1): Dynamically align the stack slot addr for parameter copy on the stack. gcc/testsuite/ 2018-11-21 Renlin Li <renlin.li@arm.com> PR middle-end/84877 * gcc.dg/pr84877.c: New. From-SVN: r266345
Diffstat (limited to 'gcc/function.c')
-rw-r--r--gcc/function.c61
1 files changed, 53 insertions, 8 deletions
diff --git a/gcc/function.c b/gcc/function.c
index 302438323c8..954e9468f01 100644
--- a/gcc/function.c
+++ b/gcc/function.c
@@ -377,6 +377,7 @@ assign_stack_local_1 (machine_mode mode, poly_int64 size,
poly_int64 bigend_correction = 0;
poly_int64 slot_offset = 0, old_frame_offset;
unsigned int alignment, alignment_in_bits;
+ bool dynamic_align_addr = false;
if (align == 0)
{
@@ -395,14 +396,20 @@ assign_stack_local_1 (machine_mode mode, poly_int64 size,
alignment_in_bits = alignment * BITS_PER_UNIT;
- /* Ignore alignment if it exceeds MAX_SUPPORTED_STACK_ALIGNMENT. */
if (alignment_in_bits > MAX_SUPPORTED_STACK_ALIGNMENT)
{
- alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
- alignment = alignment_in_bits / BITS_PER_UNIT;
+ /* If the required alignment exceeds MAX_SUPPORTED_STACK_ALIGNMENT and
+ it is not OK to reduce it. Align the slot dynamically. */
+ if (mode == BLKmode && (kind & ASLK_REDUCE_ALIGN) == 0)
+ dynamic_align_addr = true;
+ else
+ {
+ alignment_in_bits = MAX_SUPPORTED_STACK_ALIGNMENT;
+ alignment = MAX_SUPPORTED_STACK_ALIGNMENT / BITS_PER_UNIT;
+ }
}
- if (SUPPORTS_STACK_ALIGNMENT)
+ if (SUPPORTS_STACK_ALIGNMENT && !dynamic_align_addr)
{
if (crtl->stack_alignment_estimated < alignment_in_bits)
{
@@ -432,10 +439,42 @@ assign_stack_local_1 (machine_mode mode, poly_int64 size,
}
}
- if (crtl->stack_alignment_needed < alignment_in_bits)
- crtl->stack_alignment_needed = alignment_in_bits;
- if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
- crtl->max_used_stack_slot_alignment = alignment_in_bits;
+ /* Handle overalignment here for parameter copy on the stack.
+ Reserved enough space for it and dynamically align the address.
+ No free frame_space is added here. */
+ if (dynamic_align_addr)
+ {
+ rtx allocsize = gen_int_mode (size, Pmode);
+ get_dynamic_stack_size (&allocsize, 0, alignment_in_bits, NULL);
+
+ /* This is the size of space needed to accommodate required size of data
+ with given alignment. */
+ poly_int64 len = rtx_to_poly_int64 (allocsize);
+ old_frame_offset = frame_offset;
+
+ if (FRAME_GROWS_DOWNWARD)
+ {
+ frame_offset -= len;
+ try_fit_stack_local (frame_offset, len, len,
+ PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT,
+ &slot_offset);
+ }
+ else
+ {
+ frame_offset += len;
+ try_fit_stack_local (old_frame_offset, len, len,
+ PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT,
+ &slot_offset);
+ }
+ goto found_space;
+ }
+ else
+ {
+ if (crtl->stack_alignment_needed < alignment_in_bits)
+ crtl->stack_alignment_needed = alignment_in_bits;
+ if (crtl->max_used_stack_slot_alignment < alignment_in_bits)
+ crtl->max_used_stack_slot_alignment = alignment_in_bits;
+ }
if (mode != BLKmode || maybe_ne (size, 0))
{
@@ -522,6 +561,12 @@ assign_stack_local_1 (machine_mode mode, poly_int64 size,
(slot_offset + bigend_correction,
Pmode));
+ if (dynamic_align_addr)
+ {
+ addr = align_dynamic_address (addr, alignment_in_bits);
+ mark_reg_pointer (addr, alignment_in_bits);
+ }
+
x = gen_rtx_MEM (mode, addr);
set_mem_align (x, alignment_in_bits);
MEM_NOTRAP_P (x) = 1;