From e4256dcf5d2aa464f9458636c6dbd22a2ef6c48d Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Fri, 10 Jul 2026 22:29:01 +0800 Subject: [PATCH] 1, fix: the order of 'post ++/--' not right when make 3ac in SCF_OP_UNARY_ASSIGN(), 2, optimize pointer dereference with ++/--. --- core/scf_operator_handler_3ac.c | 67 +++++++++++---------------------- core/scf_pointer_alias.c | 25 ++++++------ native/x64/scf_x64_peephole.c | 10 ++++- 3 files changed, 46 insertions(+), 56 deletions(-) diff --git a/core/scf_operator_handler_3ac.c b/core/scf_operator_handler_3ac.c index a2b048e..b93a3a1 100644 --- a/core/scf_operator_handler_3ac.c +++ b/core/scf_operator_handler_3ac.c @@ -2122,13 +2122,14 @@ SCF_OP_BINARY_ASSIGN(mul, MUL) SCF_OP_BINARY_ASSIGN(div, DIV) SCF_OP_BINARY_ASSIGN(mod, MOD) -#define SCF_OP_UNARY_ASSIGN(name, op) \ -static int __scf_op_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data) \ +#define SCF_OP_UNARY_ASSIGN(name, op, post_flag) \ +static int _scf_op_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data) \ { \ assert(1 == nb_nodes); \ scf_handler_data_t* d = data; \ \ - scf_node_t* node0 = nodes[0]; \ + scf_node_t* node0 = nodes[0]; \ + scf_node_t* parent = nodes[0]->parent; \ \ while (SCF_OP_EXPR == node0->type) \ node0 = node0->nodes[0]; \ @@ -2143,6 +2144,21 @@ static int __scf_op_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, voi return -1; \ } \ \ + scf_list_t* l = scf_list_tail(d->_3ac_list_head); \ + scf_3ac_code_t* c = scf_list_data(l, scf_3ac_code_t, list); \ + \ + int ret = _scf_3ac_code_2(d->_3ac_list_head, SCF_OP_ASSIGN, parent, node0); \ + if (ret < 0) { \ + scf_loge("\n"); \ + return -1; \ + } \ + _scf_operand_get(parent)->tmp_flag = 1; \ + \ + if (post_flag) { \ + scf_list_del(&c->list); \ + scf_list_add_tail(d->_3ac_list_head, &c->list); \ + } \ + \ switch (node0->type) { \ case SCF_OP_DEREFERENCE: \ return _scf_op_left_value(ast, SCF_OP_3AC_ASSIGN_DEREFERENCE, node0, node0, d); \ @@ -2159,47 +2175,10 @@ static int __scf_op_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, voi \ return 0; \ } -SCF_OP_UNARY_ASSIGN(inc, INC) -SCF_OP_UNARY_ASSIGN(dec, DEC) - -#define SCF_OP_UNARY_ASSIGN2(name0, name1, op_type, post_flag) \ -static int _scf_op_##name0(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data) \ -{ \ - assert(1 == nb_nodes); \ - scf_handler_data_t* d = data; \ - \ - scf_node_t* node0 = nodes[0]; \ - scf_node_t* parent = nodes[0]->parent; \ - \ - int ret = __scf_op_##name1(ast, nodes, nb_nodes, data); \ - if (ret < 0) { \ - scf_loge("\n"); \ - return -1; \ - } \ - \ - while (SCF_OP_EXPR == node0->type) \ - node0 = node0->nodes[0]; \ - \ - scf_list_t* l = scf_list_tail(d->_3ac_list_head); \ - scf_3ac_code_t* c = scf_list_data(l, scf_3ac_code_t, list); \ - \ - ret = _scf_3ac_code_2(d->_3ac_list_head, SCF_OP_ASSIGN, parent, node0); \ - if (ret < 0) { \ - scf_loge("\n"); \ - return -1; \ - } \ - _scf_operand_get(parent)->tmp_flag = 1; \ - \ - if (post_flag) { \ - scf_list_del(&c->list); \ - scf_list_add_tail(d->_3ac_list_head, &c->list); \ - } \ - return 0; \ -} -SCF_OP_UNARY_ASSIGN2(inc, inc, INC, 0) -SCF_OP_UNARY_ASSIGN2(dec, dec, DEC, 0) -SCF_OP_UNARY_ASSIGN2(inc_post, inc, INC, 1) -SCF_OP_UNARY_ASSIGN2(dec_post, dec, DEC, 1) +SCF_OP_UNARY_ASSIGN(inc, INC, 0) +SCF_OP_UNARY_ASSIGN(dec, DEC, 0) +SCF_OP_UNARY_ASSIGN(inc_post, INC, 1) +SCF_OP_UNARY_ASSIGN(dec_post, DEC, 1) #define SCF_OP_CMP(name, op_type) \ static int _scf_op_##name(scf_ast_t* ast, scf_node_t** nodes, int nb_nodes, void* data) \ diff --git a/core/scf_pointer_alias.c b/core/scf_pointer_alias.c index 91b104b..aa4ea3e 100644 --- a/core/scf_pointer_alias.c +++ b/core/scf_pointer_alias.c @@ -407,7 +407,7 @@ int scf_pointer_alias_ds_leak(scf_vector_t* aliases, scf_dn_status_t* ds_pointer int _bb_copy_aliases(scf_basic_block_t* bb, scf_dag_node_t* dn_pointer, scf_dag_node_t* dn_dereference, scf_vector_t* aliases) { - scf_dag_node_t* dn_alias; + scf_dag_node_t* dn; scf_dn_status_t* ds; scf_dn_status_t* ds2; @@ -417,16 +417,21 @@ int _bb_copy_aliases(scf_basic_block_t* bb, scf_dag_node_t* dn_pointer, scf_dag_ for (i = 0; i < aliases->size; i++) { ds = aliases->data[i]; - dn_alias = ds->alias; + if (SCF_DN_ALIAS_VAR == ds->alias_type) { + dn = ds->dag_node; -// scf_variable_t* v = dn_alias->var; -// scf_logw("dn_alias: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data); +// scf_variable_t* v = ds->alias->var; +// scf_logw("dn_alias: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data); - if (SCF_DN_ALIAS_VAR == ds->alias_type) { + if (scf_dn_through_bb(ds->alias)) { - if (scf_dn_through_bb(dn_alias)) { + if (SCF_OP_INC == dn->type + || SCF_OP_DEC == dn->type + || SCF_OP_DEC_POST == dn->type + || SCF_OP_INC_POST == dn->type) + continue; - ret = scf_vector_add_unique(bb->entry_dn_aliases, dn_alias); + ret = scf_vector_add_unique(bb->entry_dn_aliases, ds->alias); if (ret < 0) return ret; } @@ -456,7 +461,6 @@ int _bb_copy_aliases(scf_basic_block_t* bb, scf_dag_node_t* dn_pointer, scf_dag_ int _bb_copy_aliases2(scf_basic_block_t* bb, scf_vector_t* aliases) { - scf_dag_node_t* dn_alias; scf_dn_status_t* ds; scf_dn_status_t* ds2; @@ -465,16 +469,15 @@ int _bb_copy_aliases2(scf_basic_block_t* bb, scf_vector_t* aliases) for (i = 0; i < aliases->size; i++) { ds = aliases->data[i]; - dn_alias = ds->alias; scf_variable_t* v = ds->dag_node->var; scf_logd("dn_pointer: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data); if (SCF_DN_ALIAS_VAR == ds->alias_type) { - if (scf_dn_through_bb(dn_alias)) { + if (scf_dn_through_bb(ds->alias)) { - ret = scf_vector_add_unique(bb->entry_dn_aliases, dn_alias); + ret = scf_vector_add_unique(bb->entry_dn_aliases, ds->alias); if (ret < 0) return ret; } diff --git a/native/x64/scf_x64_peephole.c b/native/x64/scf_x64_peephole.c index 394e96a..75baa7b 100644 --- a/native/x64/scf_x64_peephole.c +++ b/native/x64/scf_x64_peephole.c @@ -401,7 +401,15 @@ static int x64_inst_is_useful(x64_peep_ctx_t* ctx, scf_instruction_t* std) || X64_COLOR_CONFLICT(r0->color, x64_find_register("rdx")->color) || X64_COLOR_CONFLICT(r0->color, x64_find_register("rcx")->color) || X64_COLOR_CONFLICT(r0->color, x64_find_register("r8")->color) - || X64_COLOR_CONFLICT(r0->color, x64_find_register("r9")->color)) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("r9")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm0")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm1")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm2")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm3")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm4")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm5")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm6")->color) + || X64_COLOR_CONFLICT(r0->color, x64_find_register("xmm7")->color)) return 1; } else { -- 2.25.1