From 97a878dc6c7b95dd8750bb8ef843f983ded01e95 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Mon, 27 Nov 2023 01:15:49 +0800 Subject: [PATCH] optimize 3ac code of pointer alias --- core/scf_basic_block.c | 4 +- core/scf_optimizer_call.c | 6 ++- core/scf_optimizer_pointer_alias.c | 61 +++++++++++++++++++++++++++++- 3 files changed, 65 insertions(+), 6 deletions(-) diff --git a/core/scf_basic_block.c b/core/scf_basic_block.c index a41f99b..36e76a7 100644 --- a/core/scf_basic_block.c +++ b/core/scf_basic_block.c @@ -243,8 +243,8 @@ void scf_basic_block_print_list(scf_list_t* h) scf_basic_block_t* bb = scf_list_data(l, scf_basic_block_t, list); - printf("\033[33mbasic_block: %p, index: %d, dfo_normal: %d, cmp_flag: %d, group: %d, loop: %d, dereference_flag: %d, ret_flag: %d\033[0m\n", - bb, bb->index, bb->dfo_normal, bb->cmp_flag, bb->group_flag, bb->loop_flag, bb->dereference_flag, bb->ret_flag); + printf("\033[33mbasic_block: %p, index: %d, dfo_normal: %d, cmp_flag: %d, call_flag: %d, group: %d, loop: %d, dereference_flag: %d, ret_flag: %d\033[0m\n", + bb, bb->index, bb->dfo_normal, bb->cmp_flag, bb->call_flag, bb->group_flag, bb->loop_flag, bb->dereference_flag, bb->ret_flag); scf_basic_block_print(bb, sentinel); diff --git a/core/scf_optimizer_call.c b/core/scf_optimizer_call.c index be0045f..06fd0d5 100644 --- a/core/scf_optimizer_call.c +++ b/core/scf_optimizer_call.c @@ -169,8 +169,9 @@ static int __optimize_call_bb(scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list ret = scf_basic_block_split(bb0, &bb1); \ if (ret < 0) \ return ret; \ - bb1->call_flag = 1; \ bb1->dereference_flag = bb0->dereference_flag; \ + bb1->ret_flag = bb0->ret_flag; \ + bb0->ret_flag = 0; \ scf_list_add_front(&bb0->list, &bb1->list); \ scf_basic_block_mov_code(start, bb1, bb0); \ } while (0) @@ -200,9 +201,11 @@ static int _optimize_call_bb(scf_basic_block_t* bb, scf_list_t* bb_list_head) break; if (l != start) { + bb->call_flag = 0; SCF_BB_SPLIT_MOV_CODE(l, bb, bb_child); bb = bb_child; } + bb->call_flag = 1; ret = __optimize_call_bb(c, bb, bb_list_head); if (ret < 0) { @@ -254,7 +257,6 @@ static int _optimize_call(scf_ast_t* ast, scf_function_t* f, scf_list_t* bb_list return ret; } -// scf_basic_block_print_list(bb_list_head); ret = 0; error: return ret; diff --git a/core/scf_optimizer_pointer_alias.c b/core/scf_optimizer_pointer_alias.c index ae24616..31907fd 100644 --- a/core/scf_optimizer_pointer_alias.c +++ b/core/scf_optimizer_pointer_alias.c @@ -171,9 +171,46 @@ static int _3ac_pointer_alias(scf_dag_node_t* alias, scf_3ac_code_t* c, scf_basi return 0; } +static int _alias_dereference_filter_3ac(scf_dag_node_t* dn, scf_dn_status_t* ds, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head) +{ + scf_3ac_operand_t* dst; + scf_3ac_operand_t* src; + scf_3ac_code_t* c2; + scf_list_t* l; + + int i; + + for (l = scf_list_next(&c->list); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_next(l)) { + c2 = scf_list_data(l, scf_3ac_code_t, list); + + if (c2->srcs) { + for (i = 0; i < c2->srcs->size; i++) { + src = c2->srcs->data[i]; + + if (src->dag_node == dn) + src->dag_node = ds->alias; + } + } + + if (c2->dsts) { + for (i = 0; i < c2->dsts->size; i++) { + dst = c2->dsts->data[i]; + + if (dst->dag_node == dn) + dst->dag_node = ds->alias; + } + } + } + + return 0; +} + static int _alias_dereference(scf_vector_t** paliases, scf_dag_node_t* dn_pointer, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head) { + scf_3ac_operand_t* dst; + scf_dn_status_t* ds; scf_vector_t* aliases; + int ret; aliases = scf_vector_alloc(); @@ -187,6 +224,24 @@ static int _alias_dereference(scf_vector_t** paliases, scf_dag_node_t* dn_pointe return ret; } + if (1 == aliases->size) { + ds = aliases->data[0]; + + if (SCF_DN_ALIAS_VAR == ds->alias_type) { + + dst = c->dsts->data[0]; + + _alias_dereference_filter_3ac(dst->dag_node, ds, c, bb, bb_list_head); + + scf_vector_free(aliases); + aliases = NULL; + + if (ret < 0) + return ret; + return scf_basic_block_inited_vars(bb, bb_list_head); + } + } + *paliases = aliases; return 0; } @@ -314,9 +369,10 @@ static int __optimize_alias_bb(scf_list_t** pend, scf_list_t* start, scf_basic_b int ret = 0; - for (l = start; l != *pend; l = scf_list_next(l)) { + for (l = start; l != *pend; ) { c = scf_list_data(l, scf_3ac_code_t, list); + l = scf_list_next(l); if (!scf_type_is_assign_dereference(c->op->type) && SCF_OP_DEREFERENCE != c->op->type @@ -411,7 +467,7 @@ static int __optimize_alias_bb(scf_list_t** pend, scf_list_t* start, scf_basic_b } if (flag) { - *pend = l; + *pend = &c->list; break; } } @@ -493,6 +549,7 @@ static int _optimize_pointer_alias(scf_ast_t* ast, scf_function_t* f, scf_list_t return ret; } +// scf_basic_block_print_list(bb_list_head); ret = 0; error: return ret; -- 2.25.1