Skip to content

Commit

Permalink
working on improving strcpy lowering
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander.nutz committed Oct 22, 2024
1 parent b903fbc commit 2d076dd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 11 deletions.
2 changes: 2 additions & 0 deletions ir/ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,8 @@ int vx_CU_compile(vx_CU * cu,

FOR_BLOCK({
vx_IrBlock_llir_preLower_loops(block);
vx_IrBlock_llir_preLower_ifs(block);
opt_preLower(block);
vx_IrBlock_llir_lower(block);
vx_IrBlock_llir_fix_decl(block);
});
Expand Down
1 change: 1 addition & 0 deletions ir/llir.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ OPT_PASS void vx_opt_ll_binary(vx_IrBlock *block);
OPT_PASS void vx_opt_ll_tailcall(vx_IrBlock *block);

void vx_IrBlock_llir_preLower_loops(vx_IrBlock *block);
void vx_IrBlock_llir_preLower_ifs(vx_IrBlock *block);

void vx_IrBlock_llir_lower(vx_IrBlock *block);

Expand Down
25 changes: 19 additions & 6 deletions ir/opt.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ static void opt_pre(vx_IrBlock *block) {
// place immediates into params
vx_opt_inline_vars(block);

if (block->is_root)
vx_opt_vars(block);
vx_opt_vars(block);

for (size_t i = 0; i < vx_g_optconfig.consteval_iterations; i ++) {
// evaluate constants
Expand All @@ -30,10 +29,12 @@ static void opt_pre(vx_IrBlock *block) {
void opt(vx_IrBlock *block) {
assert(block != NULL);

for (vx_IrOp *op = block->first; op; op = op->next)
for (size_t i = 0; i < op->params_len; i ++)
if (op->params[i].val.type == VX_IR_VAL_BLOCK)
opt(op->params[i].val.block);
for (vx_IrOp *op = block->first; op; op = op->next) {
FOR_INPUTS(op, input, ({
if (input.type == VX_IR_VAL_BLOCK)
opt(input.block);
}));
}

opt_pre(block);
vx_opt_join_compute(block);
Expand All @@ -50,6 +51,18 @@ void opt(vx_IrBlock *block) {
opt_pre(block);
}

void opt_preLower(vx_IrBlock *block)
{
for (vx_IrOp *op = block->first; op; op = op->next) {
FOR_INPUTS(op, input, ({
if (input.type == VX_IR_VAL_BLOCK)
opt_preLower(input.block);
}));
}

opt_pre(block);
}

void opt_ll(vx_IrBlock *block) {
vx_opt_ll_dce(block);
for (size_t i = 0; i < vx_g_optconfig.consteval_iterations; i ++) {
Expand Down
1 change: 1 addition & 0 deletions ir/opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ OPT_PASS void vx_opt_ll_sched(vx_IrBlock *block);
// TODO: rename to vx_opt

void opt(vx_IrBlock *block);
void opt_preLower(vx_IrBlock *block);
void opt_ll(vx_IrBlock *block);

#endif //OPT_H
43 changes: 38 additions & 5 deletions ir/transform/ssair_llir_lower.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,34 @@

#include "../llir.h"

void vx_IrBlock_llir_preLower_ifs(vx_IrBlock *block)
{
for (vx_IrOp* op = block->first; op; op = op->next)
{
FOR_INPUTS(op, inp, {
if (inp.type == VX_IR_VAL_BLOCK)
vx_IrBlock_llir_preLower_ifs(inp.block);
});

if (op->id != VX_IR_OP_IF)
continue;

vx_IrValue* cond = vx_IrOp_param(op, VX_IR_NAME_COND);

vx_IrBlock* condbl = cond->block;
*cond = VX_IR_VALUE_VAR(condbl->outs[0]);

if (condbl->first) {
vx_IrOp* pred = vx_IrOp_predecessor(op);
if (pred)
pred->next = condbl->first;
else
block->first = condbl->first;
vx_IrBlock_tail(condbl)->next = op;
}
}
}

static void lower_into(vx_IrBlock *old, vx_IrBlock *dest, vx_IrBlock* newParent, size_t continueLabel, size_t breakLabel, vx_IrOp* loopOP);

static void into(vx_IrBlock *src, vx_IrOp *parent, vx_IrBlock *dest, size_t continueLabel, size_t breakLabel, vx_IrOp* loopOP) {
Expand Down Expand Up @@ -81,8 +109,6 @@ static void lower_into(vx_IrBlock *old, vx_IrBlock *dest, vx_IrBlock *newParent,
});

if (op->id == VX_IR_OP_IF) {
vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block;

vx_IrBlock *then = NULL; {
vx_IrValue *val = vx_IrOp_param(op, VX_IR_NAME_COND_THEN);
if (val)
Expand All @@ -100,9 +126,10 @@ static void lower_into(vx_IrBlock *old, vx_IrBlock *dest, vx_IrBlock *newParent,
continue;
}

vx_IrVar cond_var = cond->outs[0];
vx_IrVar cond_var = vx_IrOp_param(op, VX_IR_NAME_COND)->var;
vx_IrType* cond_var_ty = vx_IrBlock_typeofVar(op->parent, cond_var);

lower_into(cond, dest, newParent, continueLabel, breakLabel, loopOP);
// lower_into(cond, dest, newParent, continueLabel, breakLabel, loopOP);

if (els && then) {
// cond .then COND
Expand Down Expand Up @@ -143,7 +170,7 @@ static void lower_into(vx_IrBlock *old, vx_IrBlock *dest, vx_IrBlock *newParent,
vx_IrOp *inv = vx_IrBlock_addOpBuilding(dest);
vx_IrVar new = vx_IrBlock_newVar(dest, inv);
vx_IrOp_init(inv, VX_IR_OP_NOT, dest);
vx_IrOp_addOut(inv, new, vx_IrBlock_typeofVar(cond, cond_var));
vx_IrOp_addOut(inv, new, cond_var_ty);
vx_IrOp_addParam_s(inv, VX_IR_NAME_COND, VX_IR_VALUE_VAR(cond_var));
cond_var = new;

Expand Down Expand Up @@ -278,11 +305,17 @@ static void lower_into(vx_IrBlock *old, vx_IrBlock *dest, vx_IrBlock *newParent,
}

void vx_IrBlock_llir_lower(vx_IrBlock *block) {
puts("pre lower:");
vx_IrBlock_dump(block, stdout, 0);

static vx_IrBlock copy;
copy = *block;
block->first = NULL;
lower_into(&copy, block, block, 0, 0, NULL);

for (vx_IrOp* op = block->first; op; op = op->next)
op->parent = block;

puts("post lower:");
vx_IrBlock_dump(block, stdout, 0);
}

0 comments on commit 2d076dd

Please sign in to comment.