Skip to content

Commit

Permalink
fix cg & tiny opt pass
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed Aug 6, 2024
1 parent 5c68ec5 commit 5d6b1e7
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
1 change: 1 addition & 0 deletions build.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ struct CompileData target_lib_files[] = {
SP(CT_C, "ir/opt/dce.c"),
SP(CT_C, "ir/opt/join_compute.c"),
SP(CT_C, "ir/opt/ll_jumps.c"),
SP(CT_C, "ir/opt/ll_binary.c"),

DIR("build/ir/transform"),
SP(CT_C, "ir/transform/single_assign_conditional.c"),
Expand Down
29 changes: 22 additions & 7 deletions cg/x86_stupid/cg.c
Original file line number Diff line number Diff line change
Expand Up @@ -452,18 +452,16 @@ static void emiti_binary(Location* a, Location* b, Location* o, const char * bin

if (a != o) {
if (b == o) {
Location* scratch = start_scratch_reg(a->bytesWidth, out);
Location* scratch = start_scratch_reg(size, out);
emiti_move(a, scratch, false, out);
emiti_binary(scratch, b, scratch, binary, out);
emiti_move(scratch, o, false, out);
end_scratch_reg(scratch, out);
return;
} else {
emiti_move(a, o, false, out);
emiti_binary(o, b, o, binary, out);
return;
}
assert(false);
return;
}

fprintf(out, "%s ", binary);
Expand Down Expand Up @@ -583,6 +581,23 @@ static void emit_condmove(vx_IrOp* op, const char *cc, FILE* file) {
emiti_cmove(as_loc(out->bytesWidth, vthen), out, cc, file);
}

static size_t var_used_after_and_before_overwrite(vx_IrOp* after, vx_IrVar var) {
size_t i = 0;
for (vx_IrOp *op = after->next; op; op = op->next) {
for (size_t o = 0; o < op->outs_len; o ++)
if (op->outs[o].var == var)
break;

if (vx_IrOp_var_used(op, var))
i ++;
}
return i;
}

static bool ops_after_and_before_usage_or_overwrite(vx_IrOp* after, vx_IrVar var) {
return after->next && !vx_IrOp_var_used(after->next, var);
}

static vx_IrOp* emiti(vx_IrOp *prev, vx_IrOp* op, FILE* file) {
switch (op->id) {
case VX_IR_OP_REINTERPRET: // "val"
Expand Down Expand Up @@ -714,7 +729,7 @@ static vx_IrOp* emiti(vx_IrOp *prev, vx_IrOp* op, FILE* file) {
default: assert(false); break;
}

if (varData[ov].heat > 2) {
if (!(op->next && vx_IrOp_var_used(op->next, ov) && (op->next->id == VX_IR_OP_CMOV || op->next->id == VX_IR_OP_CONDTAILCALL || op->next->id == VX_LIR_COND))) {
emiti_storecond(o, cc, file);
}

Expand Down Expand Up @@ -999,7 +1014,7 @@ void vx_cg_x86stupid_gen(vx_IrBlock* block, FILE* out) {
break;
}
}
printf("var %zu: type %p , loc %s , size %zu\n", i, varData[i].type, str, loc ? loc->bytesWidth : 0);
printf("var %zu: type %p , loc %s , size %zu , heat %zu\n", i, varData[i].type, str, loc ? loc->bytesWidth : 0, varData[i].heat);
}

if (anyPlaced)
Expand All @@ -1024,7 +1039,7 @@ void vx_cg_x86stupid_gen(vx_IrBlock* block, FILE* out) {

static Location* start_scratch_reg(size_t size, FILE* out) {
assert(RegLut[SCRATCH_REG]->stored == NULL); // TODO
Location* loc = gen_reg_var(8, SCRATCH_REG);
Location* loc = gen_reg_var(size, SCRATCH_REG);
RegLut[SCRATCH_REG]->stored = loc;
return loc;
}
Expand Down
3 changes: 3 additions & 0 deletions ir/llir.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "ir.h"

OPT_PASS void vx_opt_ll_binary(vx_IrBlock *block);

void vx_IrBlock_llir_lower(vx_IrBlock *block);

void vx_IrBlock_llir_fix_decl(vx_IrBlock *root);
Expand All @@ -17,4 +19,5 @@ static void llir_prep_lower(vx_IrBlock *block) {
vx_IrBlock_llir_compact(block);
vx_IrBlock_lifetimes(block);
vx_IrBlock_ll_share_slots(block);
vx_opt_ll_binary(block);
}
2 changes: 1 addition & 1 deletion ir/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ OPT_PASS void vx_opt_reduce_loops(vx_IrBlock *block);
OPT_PASS void vx_opt_cmov(vx_IrBlock *block);
OPT_PASS void vx_opt_tailcall(vx_IrBlock *block);
OPT_PASS void vx_opt_vars(vx_IrBlock *block);
OPT_PASS void vx_opt_ll_jumps(vx_IrBlock *block);

OPT_PASS void vx_opt_ll_dce(vx_IrBlock *block);
OPT_PASS void vx_opt_ll_condtailcall(vx_IrBlock *block);
OPT_PASS void vx_opt_ll_jumps(vx_IrBlock *block);

void opt(vx_IrBlock *block);
void opt_ll(vx_IrBlock *block);
Expand Down
31 changes: 31 additions & 0 deletions ir/opt/ll_binary.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include "../opt.h"

OPT_PASS void vx_opt_ll_binary(vx_IrBlock *block) {
for (vx_IrOp* op = block->first; op; op = op->next) {
switch (op->id) {
case VX_IR_OP_EQ:
case VX_IR_OP_NEQ:
case VX_IR_OP_AND:
case VX_IR_OP_OR:
case VX_IR_OP_BITWISE_AND:
case VX_IR_OP_BITIWSE_OR:

case VX_IR_OP_ADD: // TODO: this might break programs ; add safeadd and safemul that don't have this problem
case VX_IR_OP_MUL:
{
vx_IrValue *a = vx_IrOp_param(op, VX_IR_NAME_OPERAND_A);
vx_IrValue *b = vx_IrOp_param(op, VX_IR_NAME_OPERAND_B);
vx_IrVar ov = op->outs[0].var;

if (b->type == VX_IR_VAL_VAR && b->var == ov) {
vx_IrValue temp = *b;
*b = *a;
*a = temp;
}
} break;

default:
break;
}
}
}

0 comments on commit 5d6b1e7

Please sign in to comment.