Align stack once

This commit is contained in:
Dmitry Stogov 2022-05-17 23:01:37 +03:00
parent e794451451
commit 5319951060
2 changed files with 11 additions and 10 deletions

View File

@ -1611,7 +1611,7 @@ static void ir_emit_prologue(ir_ctx *ctx)
| push rbp | push rbp
| mov rbp, rsp | mov rbp, rsp
} }
if (data->stack_frame_size) { if (data->stack_frame_size + data->call_stack_size) {
| sub rsp, (data->stack_frame_size + data->call_stack_size) | sub rsp, (data->stack_frame_size + data->call_stack_size)
} }
if (data->used_preserved_regs) { if (data->used_preserved_regs) {
@ -1671,7 +1671,7 @@ static void ir_emit_epilogue(ir_ctx *ctx)
if (ctx->flags & IR_USE_FRAME_POINTER) { if (ctx->flags & IR_USE_FRAME_POINTER) {
| mov rsp, rbp | mov rsp, rbp
| pop rbp | pop rbp
} else if (data->stack_frame_size) { } else if (data->stack_frame_size + data->call_stack_size) {
| add rsp, (data->stack_frame_size + data->call_stack_size) | add rsp, (data->stack_frame_size + data->call_stack_size)
} }
} }
@ -3565,7 +3565,7 @@ static int ir_parallel_copy(ir_ctx *ctx, ir_copy *copies, int count, ir_reg tmp_
return 1; return 1;
} }
static int32_t ir_call_used_stack(ir_ctx *ctx, ir_insn *insn) static int32_t ir_call_used_stack(ir_ctx *ctx, ir_insn *insn, bool align)
{ {
int j, n; int j, n;
ir_type type; ir_type type;
@ -3593,7 +3593,7 @@ static int32_t ir_call_used_stack(ir_ctx *ctx, ir_insn *insn)
} }
} }
if (used_stack % 16 != 0) { if (align && used_stack % 16 != 0) {
/* Stack must be 16 byte aligned */ /* Stack must be 16 byte aligned */
used_stack += 8; used_stack += 8;
} }
@ -3626,7 +3626,7 @@ static int32_t ir_emit_arguments(ir_ctx *ctx, ir_ref def, ir_insn *insn, ir_reg
// TODO: support for preallocated stack // TODO: support for preallocated stack
used_stack = 0; used_stack = 0;
} else { } else {
used_stack = ir_call_used_stack(ctx, insn); used_stack = ir_call_used_stack(ctx, insn, 1);
data->call_stack_size += used_stack; data->call_stack_size += used_stack;
if (used_stack) { if (used_stack) {
| sub rsp, used_stack | sub rsp, used_stack
@ -4395,7 +4395,7 @@ static void ir_preallocate_call_stack(ir_ctx *ctx, ir_backend_data *data)
for (i = 1, insn = ctx->ir_base + 1; i < ctx->insns_count;) { for (i = 1, insn = ctx->ir_base + 1; i < ctx->insns_count;) {
if (insn->op == IR_CALL) { if (insn->op == IR_CALL) {
call_stack_size = ir_call_used_stack(ctx, insn); call_stack_size = ir_call_used_stack(ctx, insn, 0);
if (call_stack_size > peak_call_stack_size) { if (call_stack_size > peak_call_stack_size) {
peak_call_stack_size = call_stack_size; peak_call_stack_size = call_stack_size;
} }
@ -4443,11 +4443,12 @@ static void ir_calc_stack_frame_size(ir_ctx *ctx, ir_backend_data *data)
data->stack_frame_alignment += 8; data->stack_frame_alignment += 8;
} }
} else { } else {
while (IR_ALIGNED_SIZE(data->stack_frame_size + sizeof(void*), 16) != data->stack_frame_size + sizeof(void*)) { ir_preallocate_call_stack(ctx, data);
while (IR_ALIGNED_SIZE(data->stack_frame_size + data->call_stack_size + sizeof(void*), 16) !=
data->stack_frame_size + data->call_stack_size + sizeof(void*)) {
data->stack_frame_size += 8; data->stack_frame_size += 8;
data->stack_frame_alignment += 8; data->stack_frame_alignment += 8;
} }
ir_preallocate_call_stack(ctx, data);
} }
} }
} }

View File

@ -22,7 +22,7 @@
} }
--EXPECT-- --EXPECT--
test: test:
subq $0x38, %rsp subq $0x28, %rsp
movl $6, (%rsp) movl $6, (%rsp)
movl $7, 8(%rsp) movl $7, 8(%rsp)
movl $8, 0x10(%rsp) movl $8, 0x10(%rsp)
@ -37,7 +37,7 @@ test:
movabsq $0x100000000, %r9 movabsq $0x100000000, %r9
movabsq $_IO_printf, %rax movabsq $_IO_printf, %rax
callq *%rax callq *%rax
addq $0x38, %rsp addq $0x28, %rsp
retq retq
1 2 3 4 0x100000000 6 7 8 9 0x100000000 1 2 3 4 0x100000000 6 7 8 9 0x100000000