Skip to content

Commit

Permalink
Improve instruction replacing code
Browse files Browse the repository at this point in the history
  • Loading branch information
dstogov committed Jan 10, 2025
1 parent e445f57 commit 65be1d8
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 44 deletions.
46 changes: 35 additions & 11 deletions ir.c
Original file line number Diff line number Diff line change
Expand Up @@ -1386,6 +1386,11 @@ bool ir_use_list_add(ir_ctx *ctx, ir_ref to, ir_ref ref)
if (old_size < new_size) {
/* Reallocate the whole edges buffer (this is inefficient) */
ctx->use_edges = ir_mem_realloc(ctx->use_edges, new_size);
} else if (n == ctx->use_edges_count) {
ctx->use_edges[n] = ref;
use_list->count++;
ctx->use_edges_count++;
return 0;
}
memcpy(ctx->use_edges + ctx->use_edges_count, ctx->use_edges + use_list->refs, use_list->count * sizeof(ir_ref));
use_list->refs = ctx->use_edges_count;
Expand Down Expand Up @@ -1416,20 +1421,39 @@ void ir_use_list_sort(ir_ctx *ctx, ir_ref ref)

void ir_replace(ir_ctx *ctx, ir_ref ref, ir_ref new_ref)
{
int i, j, n, use;
int i, j, n, *p, use;
ir_insn *insn;
ir_use_list *use_list;

IR_ASSERT(ref != new_ref);
n = ctx->use_lists[ref].count;
for (i = 0; i < n; i++) {
use = ctx->use_edges[ctx->use_lists[ref].refs + i];
IR_ASSERT(use != ref);
insn = &ctx->ir_base[use];
j = ir_insn_find_op(insn, ref);
IR_ASSERT(j > 0);
ir_insn_set_op(insn, j, new_ref);
if (!IR_IS_CONST_REF(new_ref)) {
ir_use_list_add(ctx, new_ref, use);
use_list = &ctx->use_lists[ref];
n = use_list->count;
p = ctx->use_edges + use_list->refs;

if (new_ref < 0) {
/* constant or IR_UNUSED */
for (; n; p++, n--) {
use = *p;
IR_ASSERT(use != ref);
insn = &ctx->ir_base[use];
j = ir_insn_find_op(insn, ref);
IR_ASSERT(j > 0);
ir_insn_set_op(insn, j, new_ref);
}
} else {
for (i = 0; i < n; p++, i++) {
use = *p;
IR_ASSERT(use != ref);
insn = &ctx->ir_base[use];
j = ir_insn_find_op(insn, ref);
IR_ASSERT(j > 0);
ir_insn_set_op(insn, j, new_ref);
if (ir_use_list_add(ctx, new_ref, use)) {
/* restore after reallocation */
use_list = &ctx->use_lists[ref];
n = use_list->count;
p = &ctx->use_edges[use_list->refs + i];
}
}
}
}
Expand Down
84 changes: 51 additions & 33 deletions ir_sccp.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ static void ir_sccp_remove_insn2(ir_ctx *ctx, ir_ref ref, ir_bitqueue *worklist)

static void ir_sccp_replace_insn(ir_ctx *ctx, ir_insn *_values, ir_ref ref, ir_ref new_ref, ir_bitqueue *worklist)
{
ir_ref j, n, *p, use, k, l;
ir_ref j, n, *p, use, i;
ir_insn *insn;
ir_use_list *use_list;

Expand All @@ -409,40 +409,48 @@ static void ir_sccp_replace_insn(ir_ctx *ctx, ir_insn *_values, ir_ref ref, ir_r

use_list = &ctx->use_lists[ref];
n = use_list->count;
for (j = 0, p = &ctx->use_edges[use_list->refs]; j < n; j++, p++) {
use = *p;
if (IR_IS_FEASIBLE(use)) {
insn = &ctx->ir_base[use];
l = insn->inputs_count;
for (k = 1; k <= l; k++) {
if (ir_insn_op(insn, k) == ref) {
ir_insn_set_op(insn, k, new_ref);
}
p = &ctx->use_edges[use_list->refs];
if (new_ref <= 0) {
/* constant or IR_UNUSED */
for (; n; p++, n--) {
use = *p;
/* we may skip nodes that are going to be removed by SCCP (TOP, CONST and COPY) */
if (_values[use].op > IR_COPY) {
insn = &ctx->ir_base[use];
i = ir_insn_find_op(insn, ref);
if (!i) continue;
IR_ASSERT(i > 0);
ir_insn_set_op(insn, i, new_ref);
/* schedule folding */
ir_bitqueue_add(worklist, use);
}
#if IR_COMBO_COPY_PROPAGATION
if (new_ref > 0 && IR_IS_BOTTOM(use)) {
}
} else {
for (j = 0; j < n; j++, p++) {
use = *p;
/* we may skip nodes that are going to be removed by SCCP (TOP, CONST and COPY) */
if (_values[use].optx == IR_BOTTOM) {
insn = &ctx->ir_base[use];
i = ir_insn_find_op(insn, ref);
IR_ASSERT(i > 0);
ir_insn_set_op(insn, i, new_ref);
if (ir_use_list_add(ctx, new_ref, use)) {
/* restore after reallocation */
use_list = &ctx->use_lists[ref];
n = use_list->count;
p = &ctx->use_edges[use_list->refs + j];
}
}
#endif
/* we may skip nodes that are going to be removed by SCCP (TOP, CONST and COPY) */
if (worklist && _values[use].op > IR_COPY) {
/* schedule folding */
ir_bitqueue_add(worklist, use);
}
}
}

CLEAR_USES(ref);
}

static void ir_sccp_replace_insn2(ir_ctx *ctx, ir_ref ref, ir_ref new_ref, ir_bitqueue *worklist)
{
ir_ref j, n, *p, use, k, l;
ir_ref i, j, n, *p, use;
ir_insn *insn;
ir_use_list *use_list;

Expand All @@ -468,29 +476,37 @@ static void ir_sccp_replace_insn2(ir_ctx *ctx, ir_ref ref, ir_ref new_ref, ir_bi

use_list = &ctx->use_lists[ref];
n = use_list->count;
for (j = 0, p = &ctx->use_edges[use_list->refs]; j < n; j++, p++) {
use = *p;
insn = &ctx->ir_base[use];
l = insn->inputs_count;
for (k = 1; k <= l; k++) {
if (ir_insn_op(insn, k) == ref) {
ir_insn_set_op(insn, k, new_ref);
}
p = &ctx->use_edges[use_list->refs];
if (new_ref <= 0) {
/* constant or IR_UNUSED */
for (; n; p++, n--) {
use = *p;
IR_ASSERT(use != ref);
insn = &ctx->ir_base[use];
i = ir_insn_find_op(insn, ref);
IR_ASSERT(i > 0);
ir_insn_set_op(insn, i, new_ref);
/* schedule folding */
ir_bitqueue_add(worklist, use);
}
#if IR_COMBO_COPY_PROPAGATION
if (new_ref > 0) {
} else {
for (j = 0; j < n; j++, p++) {
use = *p;
IR_ASSERT(use != ref);
insn = &ctx->ir_base[use];
i = ir_insn_find_op(insn, ref);
IR_ASSERT(i > 0);
ir_insn_set_op(insn, i, new_ref);
if (ir_use_list_add(ctx, new_ref, use)) {
/* restore after reallocation */
use_list = &ctx->use_lists[ref];
n = use_list->count;
p = &ctx->use_edges[use_list->refs + j];
}
/* schedule folding */
ir_bitqueue_add(worklist, use);
}
#endif
/* schedule folding */
ir_bitqueue_add(worklist, use);
}

CLEAR_USES(ref);
}

Expand Down Expand Up @@ -2483,7 +2499,9 @@ int ir_sccp(ir_ctx *ctx)
} else if (value->op == IR_TOP) {
/* remove unreachable instruction */
insn = &ctx->ir_base[i];
if (ir_op_flags[insn->op] & (IR_OP_FLAG_DATA|IR_OP_FLAG_MEM)) {
if (insn->op == IR_NOP) {
/* already removed */
} else if (ir_op_flags[insn->op] & (IR_OP_FLAG_DATA|IR_OP_FLAG_MEM)) {
if (insn->op != IR_PARAM && (insn->op != IR_VAR || _values[insn->op1].op == IR_TOP)) {
ir_sccp_remove_insn(ctx, _values, i, &worklist2);
}
Expand Down

0 comments on commit 65be1d8

Please sign in to comment.