optimize 3ac code of pointer alias
authoryu.dongliang <18588496441@163.com>
Sun, 26 Nov 2023 17:15:49 +0000 (01:15 +0800)
committeryu.dongliang <18588496441@163.com>
Sun, 26 Nov 2023 17:15:49 +0000 (01:15 +0800)
core/scf_basic_block.c
core/scf_optimizer_call.c
core/scf_optimizer_pointer_alias.c

index a41f99bbdd153cc3f4ed39ec7833e0b887575c43..36e76a7f87d7715769d38e1cdbba11d9ad242ec5 100644 (file)
@@ -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);
 
index be0045ff22ec5a4c856848b8baa61fd2f19a9476..06fd0d5f333a8cc655e6a32be4cf20fe0faf0965 100644 (file)
@@ -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;
index ae24616668dbd9f7e78d46e29b7de52952a49dc5..31907fd7c01e1709b22a79b1d0263cb1b4a8b393 100644 (file)
@@ -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;