backend

Unnamed repository; edit this file 'description' to name the repository.
git clone https://git.deepztream.com/backend
Log | Files | Refs | Submodules

commit a36698a857973e5e3095781281cf59d34af77c03
parent 6ebbddc790290d30c228c6ae10490e27d7d2de71
Author: William Djupström <william@deepztream.com>
Date:   Tue, 29 Sep 2026 20:01:55 +0200

Remove unused temporaries

Diffstat:
Msrc/x86_64-codegen.c | 117+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 93 insertions(+), 24 deletions(-)

diff --git a/src/x86_64-codegen.c b/src/x86_64-codegen.c @@ -1,6 +1,7 @@ #define SIZE_T_INVALID 0xfffffffffffffffful typedef enum { + x86_64_NOP, x86_64_MOV, x86_64_XCHG, x86_64_PUSH, @@ -37,6 +38,7 @@ typedef enum { } x86_64_Operand_encoding; typedef enum { + x86_64_RAND_NONE, x86_64_RAND_REGISTER, x86_64_RAND_MEMORY, x86_64_RAND_IMMEDIATE, @@ -202,6 +204,9 @@ x86_64_operand_to_cstr(x86_64_Operand rand) reg_names = x86_64_registers_err; } switch(rand.kind) { + case x86_64_RAND_NONE: + free(leak_buf); + return "(none)"; case x86_64_RAND_REGISTER: if (rand.reg.physical == -1) { snprintf(leak_buf, 32, "r_%zu", rand.reg.temp_id); @@ -460,6 +465,8 @@ x86_64_emit_instruction(x86_64_Instruction ins, Sb *sb) { size_t i; switch (ins.opcode) { + case x86_64_NOP: + break; case x86_64_MOV: switch (ins.encoding) { case x86_64_RI: @@ -1293,6 +1300,8 @@ x86_64_calculate_internal_liveness(x86_64_Basic_block *block) for (i = block_icount - 1; i != SIZE_T_INVALID; i--) { ins = block->instructions.items[i]; switch (ins.opcode) { + case x86_64_NOP: + continue; case x86_64_CALL: unreachable("live call"); case x86_64_SYSCALL: @@ -1496,15 +1505,17 @@ x86_64_calculate_internal_liveness(x86_64_Basic_block *block) if (ins.encoding == x86_64_R) { todo("indirect call"); } - if ((entry_index = x86_64_get_liveness_entry(&block->live, ins.ret.reg.temp_id)) == SIZE_T_INVALID) { - da_append(&block->live, ((x86_64_Liveness_entry) { - .start = i, - .end = i, - .temp_id = ins.ret.reg.temp_id, - })); - } else { - assert(block->live.items[entry_index].start == -1); - block->live.items[entry_index].start = i; + if (ins.ret.kind == x86_64_RAND_REGISTER) { + if ((entry_index = x86_64_get_liveness_entry(&block->live, ins.ret.reg.temp_id)) == SIZE_T_INVALID) { + da_append(&block->live, ((x86_64_Liveness_entry) { + .start = i, + .end = i, + .temp_id = ins.ret.reg.temp_id, + })); + } else { + assert(block->live.items[entry_index].start == -1); + block->live.items[entry_index].start = i; + } } if (ins.call_args.count) { for (j = 0; j < ins.call_args.count; j++) { @@ -1727,6 +1738,7 @@ x86_64_emit_call_graph_dotfmt(x86_64_Function fn) bool x86_64_is_pseudo_opcode(x86_64_Opcode opcode) { switch (opcode) { + case x86_64_NOP: case x86_64_MOV: case x86_64_XCHG: case x86_64_PUSH: @@ -1897,6 +1909,8 @@ x86_64_expand_pseudo_instructions(x86_64_Function *fn) } if (swap == -1) { switch (arg->kind) { + case x86_64_RAND_NONE: + break; case x86_64_RAND_REGISTER: assert(arg->reg.physical != -1); da_append(&new_instruction_list, ((x86_64_Instruction) { @@ -1944,6 +1958,8 @@ x86_64_expand_pseudo_instructions(x86_64_Function *fn) } } else { switch (arg->kind) { + case x86_64_RAND_NONE: + break; case x86_64_RAND_REGISTER: assert(arg->reg.physical != -1); break; @@ -2040,20 +2056,25 @@ x86_64_expand_pseudo_instructions(x86_64_Function *fn) } else { unreachable("opcode"); } - assert(ins->ret.kind == x86_64_RAND_REGISTER); - da_append(&new_instruction_list, ((x86_64_Instruction) { - .opcode = x86_64_MOV, - .encoding = x86_64_RR, - .rand1 = ins->ret, - .rand2 = { - .kind = x86_64_RAND_REGISTER, - .bit_width = ins->ret.bit_width, - .reg = { - .physical = 0, - .temp_id = SIZE_T_INVALID, + if (ins->ret.kind == x86_64_RAND_REGISTER) { + da_append(&new_instruction_list, ((x86_64_Instruction) { + .opcode = x86_64_MOV, + .encoding = x86_64_RR, + .rand1 = ins->ret, + .rand2 = { + .kind = x86_64_RAND_REGISTER, + .bit_width = ins->ret.bit_width, + .reg = { + .physical = 0, + .temp_id = SIZE_T_INVALID, + }, }, - }, - })); + })); + } else if (ins->ret.kind == x86_64_RAND_NONE) { + /* DO NOTHING */ + } else { + unreachable("return value operand"); + } while (saved.count) { saved.count--; da_append(&new_instruction_list, ((x86_64_Instruction) { @@ -2293,8 +2314,9 @@ x86_64_assign_registers(x86_64_Function *fn) } ins->rand1.reg.physical = mappings[ins->rand1.reg.temp_id]; - assert(ins->rand2.reg.physical == -1); - ins->rand2.reg.physical = mappings[ins->rand2.reg.temp_id]; + if (ins->rand2.reg.physical == -1) { + ins->rand2.reg.physical = mappings[ins->rand2.reg.temp_id]; + } break; case x86_64_M: @@ -2398,6 +2420,47 @@ x86_64_simplify_constant_operations(x86_64_Function *fn) } bool +x86_64_delete_unused_temporaries(x86_64_Function *fn) +{ + size_t i, j; + x86_64_Basic_block *block; + x86_64_Instruction *ins; + bool found_unused; + for (;;) { + found_unused = false; + for (i = 0; i < fn->blocks.count; i++) { + block = &fn->blocks.items[i]; + for (j = 0; j < block->live.count; j++) { + /* fprintf(stderr, " %%%zu: %i -> %i\n", block->live.items[j].temp_id, block->live.items[j].start, block->live.items[j].end); */ + if (block->live.items[j].start == block->live.items[j].end && block->live.items[j].start >= 0) { + ins = &block->instructions.items[block->live.items[j].start]; + if (ins->opcode == x86_64_MOV) { + ins->opcode = x86_64_NOP; + found_unused = true; + } else if (ins->opcode == x86_64_PSEUDO_CALL) { + ins->ret.kind = x86_64_RAND_NONE; + } else if (ins->opcode == x86_64_PSEUDO_SYSCALL) { + ins->ret.kind = x86_64_RAND_NONE; + } + } + } + } + if (!found_unused) { + break; + } + for (i = 0; i < fn->blocks.count; i++) { + block = &fn->blocks.items[i]; + block->live_status = x86_64_LIVE_NOT_STARTED; + block->live.count = 0; + } + if (!x86_64_calculate_liveness(fn)) { + return false; + } + } + return true; +} + +bool x86_64_delete_nop(x86_64_Function *fn) { bool is_nop; @@ -2472,6 +2535,12 @@ x86_64_compile_program(Ir_program prog, x86_64_Program *out_prog) break; } + if (!x86_64_delete_unused_temporaries(&func)) { + da_append(&out_prog->functions, func); + success = false; + break; + } + if (!x86_64_assign_registers(&func)) { da_append(&out_prog->functions, func); success = false;