From 9159db87d32e69a229f4a547fe5ebfe62ff2d3d5 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Wed, 29 Nov 2023 16:34:35 +0800 Subject: [PATCH] fix: 1, ELF core dump caused by GROUP peephole for '../examples/recursive_pointer.c' 2, auto gc analysis failed for '../examples/recursive_pointer.c' --- core/scf_basic_block.c | 2 ++ core/scf_optimizer_auto_gc_find.c | 37 +++++++++++++++++++++++-------- core/scf_optimizer_group.c | 10 +++++++-- examples/recursive_pointer.c | 37 +++++++++++++++++++++++++++++++ util/scf_vector.h | 14 ++++++++++++ 5 files changed, 89 insertions(+), 11 deletions(-) create mode 100644 examples/recursive_pointer.c diff --git a/core/scf_basic_block.c b/core/scf_basic_block.c index dca0be5..2a5fdb5 100644 --- a/core/scf_basic_block.c +++ b/core/scf_basic_block.c @@ -284,6 +284,7 @@ void scf_basic_block_print_list(scf_list_t* h) if (bb->ds_malloced) { scf_dn_status_t* ds; + printf("auto gc: \n"); for (i = 0; i < bb->ds_malloced->size; i++) { ds = bb->ds_malloced->data[i]; @@ -291,6 +292,7 @@ void scf_basic_block_print_list(scf_list_t* h) continue; scf_dn_status_print(ds); } + printf("\n"); } #endif if (bb->entry_dn_actives) { diff --git a/core/scf_optimizer_auto_gc_find.c b/core/scf_optimizer_auto_gc_find.c index 60e7d06..23fb0da 100644 --- a/core/scf_optimizer_auto_gc_find.c +++ b/core/scf_optimizer_auto_gc_find.c @@ -76,6 +76,9 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s scf_dn_index_t* di; scf_dn_index_t* di2; + scf_variable_t* v; + scf_variable_t* v2; + int i; for (i = 0; i < bb2->ds_malloced->size; i++) { ds2 = bb2->ds_malloced->data[i]; @@ -101,7 +104,6 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s } } - int size = ds->dn_indexes->size; int j; for (j = 0; j < ds2->dn_indexes->size; j++) { di2 = ds2->dn_indexes->data[j]; @@ -119,25 +121,42 @@ static int _bb_add_ds_for_call(scf_basic_block_t* bb, scf_dn_status_t* ds_obj, s if (!di->dn) { scf_dn_status_free(ds); scf_dn_index_free (di); + return -ENOMEM; } } - if (scf_vector_add(ds->dn_indexes, di) < 0) { + if (scf_vector_add_front(ds->dn_indexes, di) < 0) { scf_dn_status_free(ds); scf_dn_index_free (di); return -ENOMEM; } } - int k; - for (k = 0; k < ds2->dn_indexes->size; k++) { + v = ds ->dag_node->var; + v2 = ds2->dag_node->var; - di = ds->dn_indexes->data[size + k]; + int m = v ->nb_pointers + v ->nb_dimentions + (v ->type >= SCF_STRUCT); + int n = v2->nb_pointers + v2->nb_dimentions + (v2->type >= SCF_STRUCT); - for (j = size + k - 1; j > k; j--) - ds->dn_indexes->data[j + 1] = ds->dn_indexes->data[j]; + for (j = 0; j + m < n; j++) { - ds->dn_indexes->data[k] = di; + di = ds->dn_indexes->data[ds->dn_indexes->size - 1]; + + if (di->member || 0 == di->index) { + assert(!di->dn); + + --ds->dn_indexes->size; + scf_dn_index_free(di); + di = NULL; + } else{ + scf_dn_status_free(ds); + return -ENOMEM; + } + } + + if (ds->dn_indexes->size <= 0) { + scf_vector_free(ds->dn_indexes); + ds->dn_indexes = NULL; } _bb_add_ds(bb, ds); @@ -454,7 +473,7 @@ static int _auto_gc_find_argv_out(scf_basic_block_t* cur_bb, scf_3ac_code_t* c) if (!v1->auto_gc_flag) continue; - scf_logd("f2: %s, v0: %s, v1: %s\n", f2->node.w->text->data, v0->w->text->data, v1->w->text->data); + scf_logd("i: %d, f2: %s, v0: %s, v1: %s\n", i, f2->node.w->text->data, v0->w->text->data, v1->w->text->data); ds_obj = NULL; if (SCF_OP_ADDRESS_OF == dn->type) diff --git a/core/scf_optimizer_group.c b/core/scf_optimizer_group.c index 332fa7a..8d6aece 100644 --- a/core/scf_optimizer_group.c +++ b/core/scf_optimizer_group.c @@ -66,6 +66,9 @@ int _optimize_peep_hole(scf_bb_group_t* bbg, scf_basic_block_t* bb) if (bb_prev->nexts->size != 1) return 0; + if (bb_prev->call_flag) + return 0; + if (!scf_vector_find(bbg->body, bb_prev)) return 0; @@ -82,14 +85,17 @@ int _optimize_peep_hole(scf_bb_group_t* bbg, scf_basic_block_t* bb) return 0; } + assert(0 == scf_vector_del(bb->dn_reloads, dn)); + + if (bb->call_flag) + continue; + for (j = 0; j < bb->prevs->size; j++) { bb_prev = bb->prevs->data[j]; assert(0 == scf_vector_del(bb_prev->dn_resaves, dn)); } - assert(0 == scf_vector_del(bb->dn_reloads, dn)); - int ret = scf_vector_add_unique(bb->dn_resaves, dn); if (ret < 0) return ret; diff --git a/examples/recursive_pointer.c b/examples/recursive_pointer.c new file mode 100644 index 0000000..2434b41 --- /dev/null +++ b/examples/recursive_pointer.c @@ -0,0 +1,37 @@ +include "../lib/scf_capi.c"; + +int f(int** t0, int **t1); + +int g(int** s0, int** s1) +{ + if (!*s1) + *s1 = scf__auto_malloc(sizeof(int)); + + if (!*s0) + f(s0, s1); + return 0; +} + +int f(int** t0, int **t1) +{ + if (!*t0) + *t0 = scf__auto_malloc(sizeof(int)); + + if (!*t1) + g(t0, t1); + return 0; +} + +int main() +{ + int* p0 = NULL; + int* p1 = NULL; + + f(&p0, &p1); + + *p0 = 1; + *p1 = 2; + + printf("%d, %d\n", *p0, *p1); + return 0; +} diff --git a/util/scf_vector.h b/util/scf_vector.h index d4249ca..d8abe21 100644 --- a/util/scf_vector.h +++ b/util/scf_vector.h @@ -89,6 +89,20 @@ static inline int scf_vector_add(scf_vector_t* v, void* node) return 0; } +static inline int scf_vector_add_front(scf_vector_t* v, void* node) +{ + int ret = scf_vector_add(v, node); + if (ret < 0) + return ret; + + int i; + for (i = v->size - 2; i >= 0; i--) + v->data[i + 1] = v->data[i]; + + v->data[0] = node; + return 0; +} + static inline int scf_vector_del(scf_vector_t* v, void* node) { if (!v || !v->data) -- 2.25.1