void scf_basic_block_print_list(scf_list_t* h)
{
- scf_list_t* l;
+ if (scf_list_empty(h))
+ return;
- if (!scf_list_empty(h)) {
-
- scf_basic_block_t* bb2;
- scf_basic_block_t* last_bb = scf_list_data(scf_list_tail(h), scf_basic_block_t, list);
- scf_list_t* sentinel = scf_list_sentinel(&last_bb->code_list_head);
+ scf_dn_status_t* ds;
+ scf_dag_node_t* dn;
+ scf_variable_t* v;
- for (l = scf_list_head(h); l != scf_list_sentinel(h); l = scf_list_next(l)) {
+ scf_basic_block_t* bb;
+ scf_basic_block_t* bb2;
+ scf_basic_block_t* last_bb = scf_list_data(scf_list_tail(h), scf_basic_block_t, list);
+ scf_list_t* sentinel = scf_list_sentinel(&last_bb->code_list_head);
+ scf_list_t* l;
- scf_basic_block_t* bb = scf_list_data(l, scf_basic_block_t, list);
+ for (l = scf_list_head(h); l != scf_list_sentinel(h); l = scf_list_next(l)) {
+ bb = scf_list_data(l, scf_basic_block_t, list);
- 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);
+ printf("\033[33mbasic_block: %p, index: %d, dfo: %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, bb->cmp_flag, bb->call_flag, bb->group_flag, bb->loop_flag, bb->dereference_flag, bb->ret_flag);
- scf_basic_block_print(bb, sentinel);
+ scf_basic_block_print(bb, sentinel);
- printf("\n");
- int i;
- if (bb->prevs) {
- for (i = 0; i < bb->prevs->size; i++) {
- bb2 = bb->prevs->data[i];
+ printf("\n");
+ int i;
+ if (bb->prevs) {
+ for (i = 0; i < bb->prevs->size; i++) {
+ bb2 = bb->prevs->data[i];
- printf("prev : %p, index: %d\n", bb2, bb2->index);
- }
+ printf("prev : %p, index: %d\n", bb2, bb2->index);
}
- if (bb->nexts) {
- for (i = 0; i < bb->nexts->size; i++) {
- bb2 = bb->nexts->data[i];
+ }
+ if (bb->nexts) {
+ for (i = 0; i < bb->nexts->size; i++) {
+ bb2 = bb->nexts->data[i];
- printf("next : %p, index: %d\n", bb2, bb2->index);
- }
+ printf("next : %p, index: %d\n", bb2, bb2->index);
}
+ }
#if 0
- if (bb->dominators_normal) {
- for (i = 0; i < bb->dominators_normal->size; i++)
- printf("dominator: %p\n", bb->dominators_normal->data[i]);
- }
+ if (bb->dominators_normal) {
+ for (i = 0; i < bb->dominators_normal->size; i++)
+ printf("dominator: %p\n", bb->dominators_normal->data[i]);
+ }
#endif
- if (bb->dn_status_initeds) {
- printf("inited: \n");
- for (i = 0; i < bb->dn_status_initeds->size; i++) {
+ if (bb->dn_status_initeds) {
+ printf("inited: \n");
+ for (i = 0; i < bb->dn_status_initeds->size; i++) {
+ ds = bb->dn_status_initeds->data[i];
- scf_dn_status_t* ds = bb->dn_status_initeds->data[i];
-
- scf_dn_status_print(ds);
- }
- printf("\n");
+ scf_dn_status_print(ds);
}
+ printf("\n");
+ }
#if 1
- 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];
+ if (bb->ds_malloced) {
+ printf("auto gc: \n");
+ for (i = 0; i < bb->ds_malloced->size; i++) {
+ ds = bb->ds_malloced->data[i];
- if (scf_vector_find_cmp(bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
- continue;
- scf_dn_status_print(ds);
- }
- printf("\n");
+ if (scf_vector_find_cmp(bb->ds_freed, ds, scf_dn_status_cmp_same_dn_indexes))
+ continue;
+ scf_dn_status_print(ds);
}
+ printf("\n");
+ }
#endif
- if (bb->entry_dn_actives) {
- for (i = 0; i < bb->entry_dn_actives->size; i++) {
-
- scf_dag_node_t* dn = bb->entry_dn_actives->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->entry_dn_actives) {
+ for (i = 0; i < bb->entry_dn_actives->size; i++) {
+ dn = bb->entry_dn_actives->data[i];
- if (v && v->w)
- printf("entry active: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("entry active: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
}
+ }
- if (bb->exit_dn_actives) {
- for (i = 0; i < bb->exit_dn_actives->size; i++) {
-
- scf_dag_node_t* dn = bb->exit_dn_actives->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->exit_dn_actives) {
+ for (i = 0; i < bb->exit_dn_actives->size; i++) {
+ dn = bb->exit_dn_actives->data[i];
- if (v && v->w)
- printf("exit active: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("exit active: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
}
+ }
- if (bb->entry_dn_aliases) {
- for (i = 0; i < bb->entry_dn_aliases->size; i++) {
-
- scf_dag_node_t* dn = bb->entry_dn_aliases->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->entry_dn_aliases) {
+ for (i = 0; i < bb->entry_dn_aliases->size; i++) {
+ dn = bb->entry_dn_aliases->data[i];
- if (v && v->w)
- printf("entry alias: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("entry alias: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
}
- if (bb->exit_dn_aliases) {
- for (i = 0; i < bb->exit_dn_aliases->size; i++) {
-
- scf_dag_node_t* dn = bb->exit_dn_aliases->data[i];
- scf_variable_t* v = dn->var;
+ }
+ if (bb->exit_dn_aliases) {
+ for (i = 0; i < bb->exit_dn_aliases->size; i++) {
+ dn = bb->exit_dn_aliases->data[i];
- if (v && v->w)
- printf("exit alias: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("exit alias: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
}
+ }
- if (bb->dn_updateds) {
- for (i = 0; i < bb->dn_updateds->size; i++) {
-
- scf_dag_node_t* dn = bb->dn_updateds->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->dn_updateds) {
+ for (i = 0; i < bb->dn_updateds->size; i++) {
+ dn = bb->dn_updateds->data[i];
- if (v && v->w)
- printf("updated: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("updated: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
}
+ }
- if (bb->dn_loads) {
- for (i = 0; i < bb->dn_loads->size; i++) {
-
- scf_dag_node_t* dn = bb->dn_loads->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->dn_loads) {
+ for (i = 0; i < bb->dn_loads->size; i++) {
+ dn = bb->dn_loads->data[i];
- if (v && v->w)
- printf("loads: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("loads: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
}
+ }
- if (bb->dn_reloads) {
- for (i = 0; i < bb->dn_reloads->size; i++) {
-
- scf_dag_node_t* dn = bb->dn_reloads->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->dn_reloads) {
+ for (i = 0; i < bb->dn_reloads->size; i++) {
+ dn = bb->dn_reloads->data[i];
- if (v && v->w)
- printf("reloads: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("reloads: v_%d_%d/%s\n", v->w->line, v->w->pos, v->w->text->data);
}
+ }
- if (bb->dn_saves) {
- for (i = 0; i < bb->dn_saves->size; i++) {
-
- scf_dag_node_t* dn = bb->dn_saves->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->dn_saves) {
+ for (i = 0; i < bb->dn_saves->size; i++) {
+ dn = bb->dn_saves->data[i];
- if (v && v->w)
- printf("saves: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("saves: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
}
+ }
- if (bb->dn_resaves) {
- for (i = 0; i < bb->dn_resaves->size; i++) {
-
- scf_dag_node_t* dn = bb->dn_resaves->data[i];
- scf_variable_t* v = dn->var;
+ if (bb->dn_resaves) {
+ for (i = 0; i < bb->dn_resaves->size; i++) {
+ dn = bb->dn_resaves->data[i];
- if (v && v->w)
- printf("resaves: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
- }
+ v = dn->var;
+ if (v && v->w)
+ printf("resaves: v_%d_%d/%s, dn: %#lx\n", v->w->line, v->w->pos, v->w->text->data, 0xffff & (uintptr_t)dn);
}
-
- printf("\n");
}
+
+ printf("\n");
}
}
{
scf_dag_node_t* dn;
scf_dn_status_t* ds;
-
int i;
for (i = 0; i < dag_nodes->size; i++) {
scf_dn_status_t* ds;
scf_dag_node_t* dn;
-
int j;
for (j = 0; j < ds_vars->size; j++) {
scf_dn_status_t* ds;
scf_dag_node_t* dn;
-
int j;
for (j = 0; j < ds_vars->size; j++) {
static int _bb_vars(scf_basic_block_t* bb)
{
- int ret = 0;
-
- scf_list_t* l;
+ scf_3ac_code_t* c;
scf_dag_node_t* dn;
+ scf_list_t* l;
+
+ int ret = 0;
if (!bb->var_dag_nodes) {
bb->var_dag_nodes = scf_vector_alloc();
} else
scf_vector_clear(bb->var_dag_nodes, NULL);
- for (l = scf_list_tail(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
- l = scf_list_prev(l)) {
+ for (l = scf_list_tail(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_prev(l)) {
- scf_3ac_code_t* c = scf_list_data(l, scf_3ac_code_t, list);
+ c = scf_list_data(l, scf_3ac_code_t, list);
if (scf_type_is_jmp(c->op->type))
continue;
- if (c->dsts) {
- scf_3ac_operand_t* dst;
- int j;
+ scf_3ac_operand_t* src;
+ scf_3ac_operand_t* dst;
+ scf_variable_t* v;
+ int j;
+ if (c->dsts) {
for (j = 0; j < c->dsts->size; j++) {
dst = c->dsts->data[j];
}
if (c->srcs) {
- scf_3ac_operand_t* src;
- scf_variable_t* v;
- int j;
-
for (j = 0; j < c->srcs->size; j++) {
src = c->srcs->data[j];
int scf_basic_block_dag2(scf_basic_block_t* bb, scf_list_t* dag_list_head)
{
- scf_list_t* l;
-
- for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
- l = scf_list_next(l)) {
+ scf_list_t* l;
+ scf_3ac_code_t* c;
- scf_3ac_code_t* c = scf_list_data(l, scf_3ac_code_t, list);
+ for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_next(l)) {
+ c = scf_list_data(l, scf_3ac_code_t, list);
int ret = scf_3ac_code_to_dag(c, dag_list_head);
if (ret < 0)
return ret;
}
- for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
- l = scf_list_next(l)) {
-
- c = scf_list_data(l, scf_3ac_code_t, list);
+ for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_next(l)) {
+ c = scf_list_data(l, scf_3ac_code_t, list);
if (c->active_vars)
scf_vector_clear(c->active_vars, (void (*)(void*))scf_dn_status_free);
scf_dn_status_t* ds;
scf_dn_status_t* ds2;
scf_dn_status_t* ds3;
- scf_vector_t* aliases;
+ scf_vector_t* aliases;
int ret;
int i;
static int _bb_init_var(scf_dag_node_t* dn, scf_3ac_code_t* c, scf_basic_block_t* bb, scf_list_t* bb_list_head)
{
- scf_dn_status_t* ds;
- scf_dn_status_t* ds2;
+ scf_dn_status_t* ds;
+ scf_dn_status_t* ds2;
scf_3ac_operand_t* src;
if (!c->dn_status_initeds) {
scf_dag_node_t* dn_index;
scf_dag_node_t* dn_src;
- scf_dn_status_t* ds;
- scf_dn_status_t* ds2;
+ scf_dn_status_t* ds;
+ scf_dn_status_t* ds2;
if (!c->dn_status_initeds) {
c ->dn_status_initeds = scf_vector_alloc();
int ret = 0;
- for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
- l = scf_list_next(l)) {
+ for (l = scf_list_head(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_next(l)) {
- c = scf_list_data(l, scf_3ac_code_t, list);
+ c = scf_list_data(l, scf_3ac_code_t, list);
if (!c->active_vars)
continue;
dn->updated = 0;
}
- for (l = scf_list_tail(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head);
- l = scf_list_prev(l)) {
+ for (l = scf_list_tail(&bb->code_list_head); l != scf_list_sentinel(&bb->code_list_head); l = scf_list_prev(l)) {
- c = scf_list_data(l, scf_3ac_code_t, list);
+ c = scf_list_data(l, scf_3ac_code_t, list);
- if (scf_type_is_jmp(c->op->type)
- || SCF_OP_3AC_END == c->op->type)
+ if (scf_type_is_jmp(c->op->type) || SCF_OP_3AC_END == c->op->type)
continue;
- if (c->dsts) {
- scf_3ac_operand_t* dst;
+ scf_3ac_operand_t* src;
+ scf_3ac_operand_t* dst;
+ if (c->dsts) {
for (j = 0; j < c->dsts->size; j++) {
dst = c->dsts->data[j];
}
if (c->srcs) {
- scf_3ac_operand_t* src;
-
for (j = 0; j < c->srcs->size; j++) {
src = c->srcs->data[j];
if (!root)
return -EINVAL;
- scf_vector_t* queue = scf_vector_alloc();
+ scf_basic_block_t* bb;
+ scf_vector_t* queue;
+ scf_vector_t* checked;
+
+ queue = scf_vector_alloc();
if (!queue)
return -ENOMEM;
- scf_vector_t* checked = scf_vector_alloc();
+ checked = scf_vector_alloc();
if (!queue) {
scf_vector_free(queue);
return -ENOMEM;
int i = 0;
while (i < queue->size) {
- scf_basic_block_t* bb = queue->data[i];
+ bb = queue->data[i];
int j;
for (j = 0; j < checked->size; j++) {
- if (bb == checked->data[j])
+ if (bb == checked->data[j])
goto next;
}
failed:
scf_vector_free(queue);
scf_vector_free(checked);
- queue = NULL;
- checked = NULL;
return ret;
}
root->visited_flag = 1;
for (i = 0; i < root->prevs->size; ++i) {
-
- bb = root->prevs->data[i];
+ bb = root->prevs->data[i];
if (bb->visited_flag)
continue;
int i;
for (i = 0; i < bb->entry_dn_actives->size; i++) {
-
- dn = bb->entry_dn_actives->data[i];
+ dn = bb->entry_dn_actives->data[i];
if (dn->var->extra_flag)
continue;
}
for (i = 0; i < bb->exit_dn_actives->size; i++) {
-
- dn = bb->exit_dn_actives->data[i];
+ dn = bb->exit_dn_actives->data[i];
if (!scf_vector_find(bb->dn_updateds, dn)) {
+
if (l != scf_list_head(bb_list_head) || !dn->var->arg_flag)
continue;
}
}
for (i = 0; i < bb->exit_dn_aliases->size; i++) {
-
- dn = bb->exit_dn_aliases->data[i];
+ dn = bb->exit_dn_aliases->data[i];
if (!scf_vector_find(bb->dn_updateds, dn)) {
+
if (l != scf_list_head(bb_list_head) || !dn->var->arg_flag)
continue;
}
return 0;
}
-
scf_vector_t* prevs; // prev basic blocks
scf_vector_t* nexts; // next basic blocks
- scf_vector_t* dominators_normal;
- scf_vector_t* dominators_reverse;
- int dfo_normal;
- int dfo_reverse;
+ scf_vector_t* dominators;
+ int dfo;
scf_vector_t* entry_dn_delivery;
scf_vector_t* entry_dn_inactives;
typedef int (*scf_basic_block_bfs_pt)(scf_basic_block_t* bb, void* data, scf_vector_t* queue);
typedef int (*scf_basic_block_dfs_pt)(scf_basic_block_t* bb, void* data);
-int scf_basic_block_search_bfs(scf_basic_block_t* root, scf_basic_block_bfs_pt find, void* data);
+int scf_basic_block_search_bfs (scf_basic_block_t* root, scf_basic_block_bfs_pt find, void* data);
int scf_basic_block_search_dfs_prev(scf_basic_block_t* root, scf_basic_block_dfs_pt find, void* data, scf_vector_t* results);
int scf_basic_block_inited_by3ac(scf_basic_block_t* bb);
#endif
-
scf_vector_t* bb_loops;
scf_vector_t* bb_groups;
int max_dfo;
- int max_dfo_reverse;
scf_vector_t* text_relas; // re-localtions in .text segment
scf_vector_t* data_relas; // re-localtions in .data segment
int scf_function_signature(scf_function_t* f);
#endif
-
extern scf_optimizer_t scf_optimizer_pointer_aliases;
extern scf_optimizer_t scf_optimizer_loads_saves;
-extern scf_optimizer_t scf_optimizer_dominators_normal;
-extern scf_optimizer_t scf_optimizer_dominators_reverse;
+extern scf_optimizer_t scf_optimizer_dominators;
extern scf_optimizer_t scf_optimizer_auto_gc_find;
extern scf_optimizer_t scf_optimizer_auto_gc;
extern scf_optimizer_t scf_optimizer_loop;
extern scf_optimizer_t scf_optimizer_group;
-
extern scf_optimizer_t scf_optimizer_generate_loads_saves;
static scf_optimizer_t* scf_optimizers[] =
{
- &scf_optimizer_inline, // global optimizer
+ &scf_optimizer_inline, // global optimizer
&scf_optimizer_dag,
&scf_optimizer_pointer_aliases,
&scf_optimizer_loads_saves,
- &scf_optimizer_auto_gc_find, // global optimizer
+ &scf_optimizer_auto_gc_find, // global optimizer
- &scf_optimizer_dominators_normal,
+ &scf_optimizer_dominators,
&scf_optimizer_auto_gc,
&scf_optimizer_basic_block,
&scf_optimizer_const_teq,
- &scf_optimizer_dominators_normal,
+ &scf_optimizer_dominators,
&scf_optimizer_loop,
&scf_optimizer_group,
if (loop->loop_childs) {
printf("childs: %d\n", loop->loop_childs->size);
+
for (k = 0; k < loop->loop_childs->size; k++)
printf("%p ", loop->loop_childs->data[k]);
printf("\n");
}
+
if (loop->loop_parent)
printf("parent: %p\n", loop->loop_parent);
+
printf("loop_layers: %d\n\n", loop->loop_layers);
}
int j;
for (i = 0; i < n; i++) {
-
opt = scf_optimizers[i];
if (SCF_OPTIMIZER_GLOBAL == opt->flags) {
#if 1
for (i = 0; i < functions->size; i++) {
-
- scf_function_t* f = functions->data[i];
+ f = functions->data[i];
if (!f->node.define_flag)
continue;
scf_logi("------- %s() ------\n", f->node.w->text->data);
scf_basic_block_print_list(&f->basic_block_list_head);
- scf_loops_print(f->bb_loops);
+
+ scf_loops_print (f->bb_loops);
scf_groups_print(f->bb_groups);
}
#endif
return 0;
}
-
scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
scf_basic_block_t* bb;
+ scf_dn_status_t* ds;
+ scf_dag_node_t* dn;
+ scf_variable_t* v;
scf_vector_t* local_arrays;
l = scf_list_tail(bb_list_head);
int ret;
int i;
for (i = 0; i < bb->ds_malloced->size; ) {
+ ds = bb->ds_malloced->data[i];
- scf_dn_status_t* ds = bb->ds_malloced->data[i];
- scf_dag_node_t* dn = ds->dag_node;
- scf_variable_t* v = dn->var;
+ dn = ds->dag_node;
+ v = dn->var;
scf_loge("f: %s, last free: v_%d_%d/%s, ds->ret: %u\n",
f->node.w->text->data, v->w->line, v->w->pos, v->w->text->data, ds->ret);
for (j = 0; j < vec->size; j++) { \
bb1 = vec->data[j]; \
\
- if (bb1->dfo_normal > dfo) \
- dfo = bb1->dfo_normal; \
+ if (bb1->dfo > dfo) \
+ dfo = bb1->dfo; \
} \
} while (0)
AUTO_GC_FIND_MAX_DFO();
}
AUTO_GC_FIND_MAX_DFO();
- for (j = 0; j < bb->dominators_normal->size; j++) {
- bb_dominator = bb->dominators_normal->data[j];
+ for (j = 0; j < bb->dominators->size; j++) {
+ bb_dominator = bb->dominators->data[j];
- if (bb_dominator->dfo_normal > dfo)
+ if (bb_dominator->dfo > dfo)
break;
}
i++;
}
- scf_dag_node_t* dn;
- scf_variable_t* v;
-
for (i = 0; i < local_arrays->size; i++) {
dn = local_arrays->data[i];
return ret;
}
- root->dfo_normal = --*total;
+ root->dfo = --*total;
return 0;
}
scf_basic_block_t* bb0 = *(scf_basic_block_t**)p0;
scf_basic_block_t* bb1 = *(scf_basic_block_t**)p1;
- if (bb0->dfo_normal < bb1->dfo_normal)
+ if (bb0->dfo < bb1->dfo)
return -1;
- if (bb0->dfo_normal > bb1->dfo_normal)
+ if (bb0->dfo > bb1->dfo)
return 1;
return 0;
}
scf_basic_block_t* bb0 = dst->data[k0];
scf_basic_block_t* bb1 = src->data[k1];
- if (bb0->dfo_normal < bb1->dfo_normal) {
+ if (bb0->dfo < bb1->dfo) {
int ret = scf_vector_del(dst, bb0);
if (ret < 0)
continue;
}
- if (bb0->dfo_normal > bb1->dfo_normal) {
+ if (bb0->dfo > bb1->dfo) {
++k1;
continue;
}
return 0;
}
-static int _bb_find_dominators_normal(scf_list_t* bb_list_head)
+static int _bb_find_dominators(scf_list_t* bb_list_head)
{
if (!bb_list_head)
return -EINVAL;
scf_vector_qsort(bb->prevs, _bb_cmp_dfo);
scf_vector_qsort(bb->nexts, _bb_cmp_dfo);
- if (0 == bb->dfo_normal) {
+ if (0 == bb->dfo) {
- if (!bb->dominators_normal) {
- bb->dominators_normal = scf_vector_alloc();
- if (!bb->dominators_normal) {
+ if (!bb->dominators) {
+ bb->dominators = scf_vector_alloc();
+ if (!bb->dominators) {
ret = -ENOMEM;
goto error;
}
} else
- scf_vector_clear(bb->dominators_normal, NULL);
+ scf_vector_clear(bb->dominators, NULL);
- ret = scf_vector_add(bb->dominators_normal, bb);
+ ret = scf_vector_add(bb->dominators, bb);
if (ret < 0)
goto error;
- scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo_normal, bb->dominators_normal->size);
+ scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo, bb->dominators->size);
continue;
}
- if (bb->dominators_normal)
- scf_vector_free(bb->dominators_normal);
+ if (bb->dominators)
+ scf_vector_free(bb->dominators);
- bb->dominators_normal = scf_vector_clone(all);
- if (!bb->dominators_normal) {
+ bb->dominators = scf_vector_clone(all);
+ if (!bb->dominators) {
ret = -ENOMEM;
goto error;
}
- scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo_normal, bb->dominators_normal->size);
+ scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo, bb->dominators->size);
}
do {
for (i = 1; i < all->size; i++) {
bb = all->data[i];
- scf_vector_t* dominators_normal = NULL;
+ scf_vector_t* dominators = NULL;
for (j = 0; j < bb->prevs->size; j++) {
prev = bb->prevs->data[j];
- scf_logd("bb: %p_%d, prev: %p_%d\n", bb, bb->dfo_normal, prev, prev->dfo_normal);
+ scf_logd("bb: %p_%d, prev: %p_%d\n", bb, bb->dfo, prev, prev->dfo);
- if (!dominators_normal) {
- dominators_normal = scf_vector_clone(prev->dominators_normal);
- if (!dominators_normal) {
+ if (!dominators) {
+ dominators = scf_vector_clone(prev->dominators);
+ if (!dominators) {
ret = -ENOMEM;
goto error;
}
continue;
}
- ret = _bb_intersection(dominators_normal, prev->dominators_normal);
+ ret = _bb_intersection(dominators, prev->dominators);
if (ret < 0) {
- scf_vector_free(dominators_normal);
+ scf_vector_free(dominators);
goto error;
}
}
scf_basic_block_t* dom = NULL;
scf_basic_block_t* dom1 = bb;
- for (j = 0; j < dominators_normal->size; j++) {
- dom = dominators_normal->data[j];
+ for (j = 0; j < dominators->size; j++) {
+ dom = dominators->data[j];
- if (bb->dfo_normal == dom->dfo_normal)
+ if (bb->dfo == dom->dfo)
break;
- if (bb->dfo_normal < dom->dfo_normal)
+ if (bb->dfo < dom->dfo)
break;
}
- if (bb->dfo_normal < dom->dfo_normal) {
+ if (bb->dfo < dom->dfo) {
- for (; j < dominators_normal->size; j++) {
- dom = dominators_normal->data[j];
- dominators_normal->data[j] = dom1;
+ for (; j < dominators->size; j++) {
+ dom = dominators->data[j];
+ dominators->data[j] = dom1;
dom1 = dom;
}
}
- if (j == dominators_normal->size) {
- ret = scf_vector_add(dominators_normal, dom1);
+ if (j == dominators->size) {
+ ret = scf_vector_add(dominators, dom1);
if (ret < 0) {
- scf_vector_free(dominators_normal);
+ scf_vector_free(dominators);
goto error;
}
}
- if (dominators_normal->size != bb->dominators_normal->size)
+ if (dominators->size != bb->dominators->size)
++changed;
else {
int k0 = 0;
int k1 = 0;
- while (k0 < dominators_normal->size && k1 < bb->dominators_normal->size) {
+ while (k0 < dominators->size && k1 < bb->dominators->size) {
- scf_basic_block_t* dom0 = dominators_normal->data[k0];
- scf_basic_block_t* dom1 = bb->dominators_normal->data[k1];
+ scf_basic_block_t* dom0 = dominators->data[k0];
+ scf_basic_block_t* dom1 = bb->dominators->data[k1];
- if (dom0->dfo_normal < dom1->dfo_normal) {
+ if (dom0->dfo < dom1->dfo) {
++changed;
break;
- } else if (dom0->dfo_normal > dom1->dfo_normal) {
+ } else if (dom0->dfo > dom1->dfo) {
++changed;
break;
} else {
}
}
- scf_vector_free(bb->dominators_normal);
- bb->dominators_normal = dominators_normal;
- dominators_normal = NULL;
+ scf_vector_free(bb->dominators);
+ bb->dominators = dominators;
+ dominators = NULL;
}
} while (changed > 0);
#if 0
bb = all->data[i];
int j;
- for (j = 0; j < bb->dominators_normal->size; j++) {
+ for (j = 0; j < bb->dominators->size; j++) {
- scf_basic_block_t* dom = bb->dominators_normal->data[j];
- scf_logw("bb: %p_%d, dom: %p_%d\n", bb, bb->dfo_normal, dom, dom->dfo_normal);
+ scf_basic_block_t* dom = bb->dominators->data[j];
+ scf_logw("bb: %p_%d, dom: %p_%d\n", bb, bb->dfo, dom, dom->dfo);
}
printf("\n");
}
return ret;
}
-static int _optimize_dominators_normal(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
+static int _optimize_dominators(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
{
if (!f)
return -EINVAL;
scf_list_t* bb_list_head = &f->basic_block_list_head;
scf_list_t* l;
+ scf_bb_edge_t* edge;
scf_basic_block_t* bb;
int ret;
return ret;
for (i = 0; i < f->dfs_tree->size; i++) {
- scf_bb_edge_t* edge = f->dfs_tree->data[i];
+ edge = f->dfs_tree->data[i];
scf_logd("bb_%p_%d --> bb_%p_%d\n",
- edge->start, edge->start->dfo_normal,
- edge->end, edge->end->dfo_normal);
+ edge->start, edge->start->dfo,
+ edge->end, edge->end->dfo);
}
- ret = _bb_find_dominators_normal(bb_list_head);
+ ret = _bb_find_dominators(bb_list_head);
if (ret < 0) {
scf_loge("\n");
return ret;
return 0;
}
-scf_optimizer_t scf_optimizer_dominators_normal =
+scf_optimizer_t scf_optimizer_dominators =
{
- .name = "dominators_normal",
+ .name = "dominators",
- .optimize = _optimize_dominators_normal,
+ .optimize = _optimize_dominators,
.flags = SCF_OPTIMIZER_LOCAL,
};
-
+++ /dev/null
-#include"scf_optimizer.h"
-
-static int __bb_dfs_tree(scf_basic_block_t* root, scf_vector_t* edges, int* total)
-{
- scf_basic_block_t* bb;
- scf_bb_edge_t* edge;
-
- int i;
- int ret;
-
- assert(!root->jmp_flag);
-
- root->visited_flag = 1;
-
- for (i = 0; i < root->prevs->size; ++i) {
-
- bb = root->prevs->data[i];
-
- if (bb->visited_flag)
- continue;
-
- edge = malloc(sizeof(scf_bb_edge_t));
- if (!edge)
- return -ENOMEM;
-
- edge->start = root;
- edge->end = bb;
-
- ret = scf_vector_add(edges, edge);
- if ( ret < 0)
- return ret;
-
- ret = __bb_dfs_tree(bb, edges, total);
- if ( ret < 0)
- return ret;
- }
-
- root->dfo_reverse = --*total;
- return 0;
-}
-
-static int _bb_dfs_tree(scf_list_t* bb_list_head, scf_function_t* f)
-{
- if (!bb_list_head)
- return -EINVAL;
-
- if (scf_list_empty(bb_list_head))
- return 0;
-
- scf_list_t* l;
- scf_basic_block_t* bb;
-
- int total = 0;
-
- for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_next(l)) {
-
- bb = scf_list_data(l, scf_basic_block_t, list);
-
- bb->visited_flag = 0;
-
- if (!bb->jmp_flag)
- ++total;
- }
- assert(&bb->list == scf_list_tail(bb_list_head));
-
- f->max_dfo_reverse = total - 1;
-
- return __bb_dfs_tree(bb, f->dfs_tree, &total);
-}
-
-static int _bb_cmp_dfo(const void* p0, const void* p1)
-{
- scf_basic_block_t* bb0 = *(scf_basic_block_t**)p0;
- scf_basic_block_t* bb1 = *(scf_basic_block_t**)p1;
-
- if (bb0->dfo_reverse < bb1->dfo_reverse)
- return -1;
- if (bb0->dfo_reverse > bb1->dfo_reverse)
- return 1;
- return 0;
-}
-
-static int _bb_intersection(scf_vector_t* dst, scf_vector_t* src)
-{
- int k0 = 0;
- int k1 = 0;
-
- while (k0 < dst->size && k1 < src->size) {
-
- scf_basic_block_t* bb0 = dst->data[k0];
- scf_basic_block_t* bb1 = src->data[k1];
-
- if (bb0->dfo_reverse < bb1->dfo_reverse) {
-
- int ret = scf_vector_del(dst, bb0);
- if (ret < 0)
- return ret;
- continue;
- }
-
- if (bb0->dfo_reverse > bb1->dfo_reverse) {
- ++k1;
- continue;
- }
-
- ++k0;
- ++k1;
- }
-
- dst->size = k0;
-
- return 0;
-}
-
-static int _bb_find_dominators_reverse(scf_list_t* bb_list_head)
-{
- if (!bb_list_head)
- return -EINVAL;
-
- if (scf_list_empty(bb_list_head))
- return 0;
-
- scf_list_t* l;
- scf_basic_block_t* bb;
- scf_vector_t* all;
-
- int i;
- int j;
- int ret;
- int changed;
-
- all = scf_vector_alloc();
- if (!all)
- return -ENOMEM;
-
- for (l = scf_list_tail(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_prev(l)) {
-
- bb = scf_list_data(l, scf_basic_block_t, list);
- if (bb->jmp_flag)
- continue;
-
- ret = scf_vector_add(all, bb);
- if (ret < 0)
- goto error;
- }
-
- scf_vector_qsort(all, _bb_cmp_dfo);
-
- for (i = 0; i < all->size; i++) {
- bb = all->data[i];
-
- scf_vector_qsort(bb->prevs, _bb_cmp_dfo);
- scf_vector_qsort(bb->nexts, _bb_cmp_dfo);
-
- if (0 == bb->dfo_reverse) {
-
- if (!bb->dominators_reverse) {
- bb->dominators_reverse = scf_vector_alloc();
- if (!bb->dominators_reverse) {
- ret = -ENOMEM;
- goto error;
- }
- } else
- scf_vector_clear(bb->dominators_reverse, NULL);
-
- ret = scf_vector_add(bb->dominators_reverse, bb);
- if (ret < 0)
- goto error;
-
- scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo_reverse, bb->dominators_reverse->size);
- continue;
- }
-
- if (bb->dominators_reverse)
- scf_vector_free(bb->dominators_reverse);
-
- bb->dominators_reverse = scf_vector_clone(all);
- if (!bb->dominators_reverse) {
- ret = -ENOMEM;
- goto error;
- }
-
- scf_logd("bb: %p_%d, dom size: %d\n", bb, bb->dfo_reverse, bb->dominators_reverse->size);
- }
-
- do {
- changed = 0;
-
- for (i = 1; i < all->size; i++) {
- bb = all->data[i];
-
- scf_vector_t* dominators_reverse = NULL;
-
- for (j = 0; j < bb->nexts->size; j++) {
- scf_basic_block_t* next = bb->nexts->data[j];
- scf_logd("bb: %p_%d, next: %p_%d\n", bb, bb->dfo_normal, next, next->dfo_normal);
-
- if (!dominators_reverse) {
- dominators_reverse = scf_vector_clone(next->dominators_reverse);
- if (!dominators_reverse) {
- ret = -ENOMEM;
- goto error;
- }
- continue;
- }
-
- ret = _bb_intersection(dominators_reverse, next->dominators_reverse);
- if (ret < 0) {
- scf_vector_free(dominators_reverse);
- goto error;
- }
- }
-
- scf_logd("bb: %p, dominators_reverse: %p, bb->nexts->size: %d\n",
- bb, dominators_reverse, bb->nexts->size);
-
- scf_basic_block_t* dom = NULL;
- scf_basic_block_t* dom1 = bb;
-
- for (j = 0; j < dominators_reverse->size; j++) {
- dom = dominators_reverse->data[j];
-
- if (bb->dfo_reverse == dom->dfo_reverse)
- break;
-
- if (bb->dfo_reverse < dom->dfo_reverse)
- break;
- }
- if (bb->dfo_reverse < dom->dfo_reverse) {
-
- for (; j < dominators_reverse->size; j++) {
- dom = dominators_reverse->data[j];
- dominators_reverse->data[j] = dom1;
- dom1 = dom;
- }
- }
- if (j == dominators_reverse->size) {
- ret = scf_vector_add(dominators_reverse, dom1);
- if (ret < 0) {
- scf_vector_free(dominators_reverse);
- goto error;
- }
- }
-
- if (dominators_reverse->size != bb->dominators_reverse->size) {
- scf_vector_free(bb->dominators_reverse);
- bb->dominators_reverse = dominators_reverse;
- dominators_reverse = NULL;
- ++changed;
- } else {
- int k0 = 0;
- int k1 = 0;
-
- while (k0 < dominators_reverse->size && k1 < bb->dominators_reverse->size) {
-
- scf_basic_block_t* dom0 = dominators_reverse->data[k0];
- scf_basic_block_t* dom1 = bb->dominators_reverse->data[k1];
-
- if (dom0->dfo_reverse < dom1->dfo_reverse)
- ++k0;
- else if (dom0->dfo_reverse > dom1->dfo_reverse)
- ++k1;
- else {
- ++k0;
- ++k1;
- }
- }
-
- if (k0 == k1) {
- scf_vector_free(dominators_reverse);
- dominators_reverse = NULL;
- } else {
- scf_vector_free(bb->dominators_reverse);
- bb->dominators_reverse = dominators_reverse;
- dominators_reverse = NULL;
- ++changed;
- }
- }
- }
- } while (changed > 0);
-#if 0
- for (i = 0; i < all->size; i++) {
- bb = all->data[i];
-
- int j;
- for (j = 0; j < bb->dominators_reverse->size; j++) {
-
- scf_basic_block_t* dom = bb->dominators_reverse->data[j];
- scf_logd("bb: %p_%d, dom: %p_%d\n", bb, bb->dfo_reverse, dom, dom->dfo_reverse);
- }
- printf("\n");
- }
-#endif
-
- ret = 0;
-error:
- scf_vector_free(all);
- return ret;
-}
-
-static int _optimize_dominators_reverse(scf_ast_t* ast, scf_function_t* f, scf_vector_t* functions)
-{
- if (!f)
- return -EINVAL;
-
- scf_list_t* bb_list_head = &f->basic_block_list_head;
- scf_list_t* l;
- scf_basic_block_t* bb;
-
- int ret;
- int i;
-
- if (scf_list_empty(bb_list_head))
- return 0;
-
- scf_vector_clear(f->dfs_tree, free);
-
- ret = _bb_dfs_tree(bb_list_head, f);
- if (ret < 0)
- return ret;
-
- for (i = 0; i < f->dfs_tree->size; i++) {
- scf_bb_edge_t* edge = f->dfs_tree->data[i];
-
- scf_logd("bb_%p_%d --> bb_%p_%d\n",
- edge->start, edge->start->dfo_reverse,
- edge->end, edge->end->dfo_reverse);
- }
-
- ret = _bb_find_dominators_reverse(bb_list_head);
- if (ret < 0) {
- scf_loge("\n");
- return ret;
- }
-
-// scf_basic_block_print_list(bb_list_head);
- return 0;
-}
-
-scf_optimizer_t scf_optimizer_dominators_reverse =
-{
- .name = "dominators_reverse",
-
- .optimize = _optimize_dominators_reverse,
-};
-
static int __bb_dfs_loop2(scf_basic_block_t* root, scf_vector_t* loop)
{
scf_basic_block_t* bb;
-
int i;
assert(!root->jmp_flag);
root->visited_flag = 1;
for (i = 0; i < root->prevs->size; ++i) {
-
- bb = root->prevs->data[i];
+ bb = root->prevs->data[i];
if (bb->visited_flag)
continue;
if (ret < 0)
return ret;
- for (l = scf_list_tail(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_prev(l)) {
-
+ for (l = scf_list_tail(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_prev(l)) {
bb2 = scf_list_data(l, scf_basic_block_t, list);
if (bb2->jmp_flag)
int nblocks = 0;
- for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head);
- l = scf_list_next(l)) {
+ for (l = scf_list_head(bb_list_head); l != scf_list_sentinel(bb_list_head); l = scf_list_next(l)) {
bb = scf_list_data(l, scf_basic_block_t, list);
int i = 0;
int j = 0;
- while (i < bb->nexts->size && j < bb->dominators_normal->size) {
+ while (i < bb->nexts->size && j < bb->dominators->size) {
scf_basic_block_t* next = bb->nexts->data[i];
- scf_basic_block_t* dom = bb->dominators_normal->data[j];
+ scf_basic_block_t* dom = bb->dominators->data[j];
- if (next->dfo_normal < dom->dfo_normal) {
+ if (next->dfo < dom->dfo) {
++i;
continue;
}
- if (next->dfo_normal > dom->dfo_normal) {
+ if (next->dfo > dom->dfo) {
++j;
continue;
}
ret = __bb_dfs_loop(bb_list_head, bb, dom, loop);
if (ret < 0) {
- scf_loge("\n");
scf_vector_free(loop);
return ret;
}
static int _bb_loop_merge(scf_vector_t* loops)
{
scf_bb_group_t* loop;
-
- int ret;
int i;
for (i = 0; i < loops->size; i++) {
loop = loops->data[i];
if (loop->loop_childs) {
-
- ret = _bb_loop_merge(loop->loop_childs);
+ int ret = _bb_loop_merge(loop->loop_childs);
if (ret < 0)
return ret;
}
static void _bb_loop_sort(scf_vector_t* loops)
{
scf_bb_group_t* loop;
-
- int ret;
int i;
for (i = 0; i < loops->size; i++) {
}
assert(n < exit->prevs->size);
- bb ->nexts->data[k] = post;
+ bb->nexts->data[k] = post;
if (!scf_vector_find(exit->prevs, post))
exit->prevs->data[n] = post;
.flags = SCF_OPTIMIZER_LOCAL,
};
-
CFILES += ../core/scf_optimizer_auto_gc_find.c
CFILES += ../core/scf_optimizer_auto_gc.c
CFILES += ../core/scf_optimizer_dominators.c
-CFILES += ../core/scf_optimizer_dominators_reverse.c
CFILES += ../core/scf_optimizer_basic_block.c
CFILES += ../core/scf_optimizer_const_teq.c
CFILES += ../core/scf_optimizer_loop.c