-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
alexander.nutz
committed
May 29, 2024
1 parent
f0358ae
commit 4bf833b
Showing
13 changed files
with
98 additions
and
164 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,17 @@ | ||
#include "../opt.h" | ||
|
||
static void rmcode_before_label(vx_IrBlock *block, size_t first) { | ||
for (size_t i = first; i < block->ops_len; i ++) { | ||
vx_IrOp *op = &block->ops[i]; | ||
|
||
static void rmcode_before_label(vx_IrOp *op) { | ||
for (; op; op = op->next) { | ||
if (op->id == VX_LIR_OP_LABEL) | ||
break; | ||
|
||
vx_IrOp_destroy(op); | ||
vx_IrOp_init(op, VX_IR_OP_NOP, block); | ||
vx_IrOp_remove(op); | ||
} | ||
} | ||
|
||
void vx_opt_ll_dce(vx_IrBlock *block) { | ||
for (size_t i = 0; i < block->ops_len; i ++) { | ||
vx_IrOp *op = &block->ops[i]; | ||
if (vx_IrOp_ends_flow(op)) { | ||
rmcode_before_label(block, i + 1); | ||
} | ||
} | ||
for (vx_IrOp *op = block->first; op; op = op->next) | ||
if (vx_IrOp_ends_flow(op)) | ||
rmcode_before_label(op->next); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,70 +1,49 @@ | ||
#include "../opt.h" | ||
|
||
void vx_opt_loop_simplify(vx_IrView view, | ||
vx_IrBlock *block) | ||
void vx_opt_loop_simplify(vx_IrBlock *block) | ||
{ | ||
while (vx_IrView_find(&view, VX_IR_OP_FOR)) { | ||
vx_IrOp *op = (vx_IrOp *) vx_IrView_take(view); | ||
for (vx_IrOp *op = block->first; op; op = op->next) { | ||
if (op->id != VX_IR_OP_FOR) | ||
continue; | ||
|
||
vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block; | ||
const vx_IrVar condVar = cond->outs[0]; | ||
|
||
// if it will always we 0, we optimize it out | ||
if (vx_Irblock_alwaysis_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) { | ||
vx_IrView_replace(block, view, NULL, 0); | ||
|
||
goto next; | ||
vx_IrOp_remove(op); | ||
continue; | ||
} | ||
|
||
// if it will never be 0 (not might be 0), it is always true => infinite loop | ||
if (!vx_Irblock_mightbe_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) { | ||
vx_IrOp new; | ||
vx_IrOp_init(&new, VX_IR_OP_INFINITE, block); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE); | ||
vx_IrOp_steal_states(&new, op); | ||
vx_IrOp_steal_outs(&new, op); | ||
|
||
vx_IrView_replace(block, view, &new, 1); | ||
|
||
goto next; | ||
op->id = VX_IR_OP_INFINITE; // we could remove cond param | ||
continue; | ||
} | ||
|
||
// if it is a less than, we can do a repeat | ||
if (cond->ops_len > 0 && cond->ops[0].id == VX_IR_OP_LT) { | ||
if (cond->first && cond->first->id == VX_IR_OP_LT) { | ||
const vx_IrValue *a = vx_IrOp_param(cond->first, VX_IR_NAME_OPERAND_A); | ||
// require it to be the counter | ||
if (a->type != VX_IR_VAL_VAR) | ||
continue; | ||
if (a->var != cond->ins[0].var) | ||
continue; | ||
if (cond->first->outs[0].var != cond->outs[0]) | ||
continue; | ||
|
||
const vx_IrValue b = *vx_IrOp_param(cond->first, VX_IR_NAME_OPERAND_B); | ||
|
||
bool break2 = false; | ||
do { | ||
const vx_IrValue *a = vx_IrOp_param(&cond->ops[0], VX_IR_NAME_OPERAND_A); | ||
// require it to be the counter | ||
if (a->type != VX_IR_VAL_VAR) | ||
break; | ||
if (a->var != cond->ins[0].var) | ||
break; | ||
if (cond->ops[0].outs[0].var != cond->outs[0]) | ||
break; | ||
|
||
const vx_IrValue b = *vx_IrOp_param(&cond->ops[0], VX_IR_NAME_OPERAND_B); | ||
|
||
vx_IrOp new; | ||
vx_IrOp_init(&new, VX_IR_OP_REPEAT, block); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START); | ||
vx_IrOp_add_param_s(&new, VX_IR_NAME_LOOP_ENDEX, b); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE); | ||
vx_IrOp_steal_states(&new, op); // steal all state init params | ||
vx_IrOp_steal_outs(&new, op); | ||
// we probably do a bit of memory leaking here... | ||
*op = new; | ||
|
||
break2 = true; | ||
} while(0); | ||
if (break2) | ||
goto next; // we optimized already | ||
vx_IrOp new; | ||
vx_IrOp_init(&new, VX_IR_OP_REPEAT, block); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_DO); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_START); | ||
vx_IrOp_add_param_s(&new, VX_IR_NAME_LOOP_ENDEX, b); | ||
vx_IrOp_steal_param(&new, op, VX_IR_NAME_LOOP_STRIDE); | ||
vx_IrOp_steal_states(&new, op); // steal all state init params | ||
vx_IrOp_steal_outs(&new, op); | ||
// we probably do a bit of memory leaking here... | ||
*op = new; | ||
} | ||
|
||
next: | ||
view = vx_IrView_drop(view, 1); | ||
} | ||
} |
Oops, something went wrong.