Skip to content

Commit

Permalink
remove if with empty body
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-s168 committed May 28, 2024
1 parent faebfbe commit ed6ff3a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
5 changes: 5 additions & 0 deletions ir/ir.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,11 @@ void vx_IrBlock_swap_out_at(vx_IrBlock *block, size_t a, size_t b);
void vx_IrBlock_remove_out_at(vx_IrBlock *block, size_t id);
size_t vx_IrBlock_insert_label_op(vx_IrBlock *block);

static bool vx_IrBlock_empty(vx_IrBlock *block) {
if (!block)
return true;
return block->ops_len == 0;
}
bool vx_IrBlock_var_used(const vx_IrBlock *block, vx_IrVar var);
bool vx_IrOp_var_used(const vx_IrOp *op, vx_IrVar var);

Expand Down
21 changes: 13 additions & 8 deletions ir/opt/reduce_if.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,19 @@ void vx_opt_reduce_if(vx_IrView view,
vx_IrBlock *cond = vx_IrOp_param(op, VX_IR_NAME_COND)->block;
const vx_IrVar condVar = cond->outs[0];

// TODO: remove if empty

vx_IrValue *pthen = vx_IrOp_param(op, VX_IR_NAME_COND_THEN);
if (pthen) {
vx_IrBlock *then = pthen->block;
vx_IrBlock *then = pthen ? pthen->block : NULL;

vx_IrValue *pelse = vx_IrOp_param(op, VX_IR_NAME_COND_ELSE);
vx_IrBlock *els = pelse ? pelse->block : NULL;

if (vx_IrBlock_empty(then) && vx_IrBlock_empty(els)) {
vx_IrOp_destroy(op);
vx_IrOp_init(op, VX_IR_OP_NOP, block);
goto cont;
}

if (then) {
// if it will never be 0 (not might be 0), it is always true => only execute then block
if (!vx_Irblock_mightbe_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) {
for (size_t i = 0; i < op->outs_len; i ++) {
Expand All @@ -30,10 +37,7 @@ void vx_opt_reduce_if(vx_IrView view,
}
}

vx_IrValue *pelse = vx_IrOp_param(op, VX_IR_NAME_COND_ELSE);
if (pelse) {
vx_IrBlock *els = pelse->block;

if (els) {
// if it will always we 0, only the else block will ever be executed
if (vx_Irblock_alwaysis_var(cond, condVar, (vx_IrValue) { .type = VX_IR_VAL_IMM_INT, .imm_int = 0 })) {
for (size_t i = 0; i < op->outs_len; i ++) {
Expand All @@ -58,6 +62,7 @@ void vx_opt_reduce_if(vx_IrView view,
}
}

cont:
view = vx_IrView_drop(view, 1);
}
}

0 comments on commit ed6ff3a

Please sign in to comment.