Move the node/label numbering into the alloc funcs.

I should have done it this way in the first place. much cleaner.
This commit is contained in:
Bill Currie 2012-11-17 21:55:59 +09:00
parent 12133bc02f
commit deb5484c7e

View file

@ -90,24 +90,27 @@ new_dag (void)
} }
static daglabel_t * static daglabel_t *
new_label (void) new_label (dag_t *dag)
{ {
daglabel_t *label; daglabel_t *label;
ALLOC (256, daglabel_t, labels, label); ALLOC (256, daglabel_t, labels, label);
label->daglabel_chain = daglabel_chain; label->daglabel_chain = daglabel_chain;
daglabel_chain = label; daglabel_chain = label;
label->number = -1; label->number = dag->num_labels;
dag->labels[dag->num_labels++] = label;
return label; return label;
} }
static dagnode_t * static dagnode_t *
new_node (void) new_node (dag_t *dag)
{ {
dagnode_t *node; dagnode_t *node;
ALLOC (256, dagnode_t, nodes, node); ALLOC (256, dagnode_t, nodes, node);
node->parents = set_new (); node->parents = set_new ();
node->edges = set_new (); node->edges = set_new ();
node->identifiers = set_new (); node->identifiers = set_new ();
node->number = dag->num_nodes;
dag->nodes[dag->num_nodes++] = node;
return node; return node;
} }
@ -128,18 +131,18 @@ daglabel_string (daglabel_t *label)
} }
static daglabel_t * static daglabel_t *
opcode_label (const char *opcode, expr_t *expr) opcode_label (dag_t *dag, const char *opcode, expr_t *expr)
{ {
daglabel_t *label; daglabel_t *label;
label = new_label (); label = new_label (dag);
label->opcode = opcode; label->opcode = opcode;
label->expr = expr; label->expr = expr;
return label; return label;
} }
static daglabel_t * static daglabel_t *
operand_label (operand_t *op) operand_label (dag_t *dag, operand_t *op)
{ {
symbol_t *sym = 0; symbol_t *sym = 0;
ex_value_t *val = 0; ex_value_t *val = 0;
@ -153,27 +156,27 @@ operand_label (operand_t *op)
if (op->op_type == op_temp) { if (op->op_type == op_temp) {
if (op->o.tempop.daglabel) if (op->o.tempop.daglabel)
return op->o.tempop.daglabel; return op->o.tempop.daglabel;
label = new_label (); label = new_label (dag);
label->op = op; label->op = op;
op->o.tempop.daglabel = label; op->o.tempop.daglabel = label;
} else if (op->op_type == op_symbol) { } else if (op->op_type == op_symbol) {
sym = op->o.symbol; sym = op->o.symbol;
if (sym->daglabel) if (sym->daglabel)
return sym->daglabel; return sym->daglabel;
label = new_label (); label = new_label (dag);
label->op = op; label->op = op;
sym->daglabel = label; sym->daglabel = label;
} else if (op->op_type == op_value || op->op_type == op_pointer) { } else if (op->op_type == op_value || op->op_type == op_pointer) {
val = op->o.value; val = op->o.value;
if (val->daglabel) if (val->daglabel)
return val->daglabel; return val->daglabel;
label = new_label (); label = new_label (dag);
label->op = op; label->op = op;
val->daglabel = label; val->daglabel = label;
} else if (op->op_type == op_label) { } else if (op->op_type == op_label) {
if (op->o.label->daglabel) if (op->o.label->daglabel)
return op->o.label->daglabel; return op->o.label->daglabel;
label = new_label (); label = new_label (dag);
label->op = op; label->op = op;
op->o.label->daglabel = label; op->o.label->daglabel = label;
} else { } else {
@ -183,16 +186,16 @@ operand_label (operand_t *op)
} }
static dagnode_t * static dagnode_t *
leaf_node (operand_t *op, expr_t *expr) leaf_node (dag_t *dag, operand_t *op, expr_t *expr)
{ {
daglabel_t *label; daglabel_t *label;
dagnode_t *node; dagnode_t *node;
if (!op) if (!op)
return 0; return 0;
node = new_node (); node = new_node (dag);
node->tl = op->type; node->tl = op->type;
label = operand_label (op); label = operand_label (dag, op);
label->dagnode = node; label->dagnode = node;
label->expr = expr; label->expr = expr;
node->label = label; node->label = label;
@ -276,11 +279,9 @@ dag_create (const flownode_t *flownode)
dag_t *dag; dag_t *dag;
sblock_t *block = flownode->sblock; sblock_t *block = flownode->sblock;
statement_t *s; statement_t *s;
dagnode_t **nodes; dagnode_t **nodes;
daglabel_t **labels; daglabel_t **labels;
int num_statements = 0; int num_statements = 0;
int num_nodes = 0;
int num_labels = 0;
int i; int i;
flush_daglabels (); flush_daglabels ();
@ -288,10 +289,11 @@ dag_create (const flownode_t *flownode)
for (s = block->statements; s; s = s->next) for (s = block->statements; s; s = s->next)
num_statements++; num_statements++;
dag = new_dag ();
// at most 4 per statement // at most 4 per statement
nodes = alloca (num_statements * 4 * sizeof (dagnode_t)); dag->nodes = alloca (num_statements * 4 * sizeof (dagnode_t));
// at most 3 per statement // at most 3 per statement
labels = alloca (num_statements * 3 * sizeof (daglabel_t)); dag->labels = alloca (num_statements * 3 * sizeof (daglabel_t));
for (s = block->statements; s; s = s->next) { for (s = block->statements; s; s = s->next) {
operand_t *operands[4]; operand_t *operands[4];
@ -301,38 +303,25 @@ dag_create (const flownode_t *flownode)
flow_analyze_statement (s, 0, 0, 0, operands); flow_analyze_statement (s, 0, 0, 0, operands);
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
if (!(children[i] = node (operands[i + 1]))) { if (!(children[i] = node (operands[i + 1])))
children[i] = leaf_node (operands[i + 1], s->expr); children[i] = leaf_node (dag, operands[i + 1], s->expr);
if (children[i]) {
children[i]->number = num_nodes;
nodes[num_nodes++] = children[i];
if (children[i]->label->number == -1) {
children[i]->label->number = num_labels;
labels[num_labels++] = children[i]->label;
}
}
}
} }
op = opcode_label (s->opcode, s->expr); op = opcode_label (dag, s->opcode, s->expr);
op->number = num_labels;
labels[num_labels++] = op;
if (s->type == st_assign) { if (s->type == st_assign) {
n = children[0]; n = children[0];
} else { } else {
n = 0; n = 0;
for (i = 0; i < num_nodes; i++) { for (i = 0; i < dag->num_nodes; i++) {
if (dagnode_match (nodes[i], op, children)) { if (dagnode_match (dag->nodes[i], op, children)) {
n = nodes[i]; n = dag->nodes[i];
break; break;
} }
} }
} }
if (!n) { if (!n) {
n = new_node (); n = new_node (dag);
n->type = s->type; n->type = s->type;
n->label = op; n->label = op;
n->number = num_nodes;
nodes[num_nodes++] = n;
for (i = 0; i < 3; i++) { for (i = 0; i < 3; i++) {
n->children[i] = children[i]; n->children[i] = children[i];
if (n->children[i]) { if (n->children[i]) {
@ -341,27 +330,22 @@ dag_create (const flownode_t *flownode)
} }
} }
} }
lx = operand_label (operands[0]); lx = operand_label (dag, operands[0]);
if (lx) { if (lx) {
flowvar_t *var = flow_get_var (lx->op); flowvar_t *var = flow_get_var (lx->op);
lx->expr = s->expr; lx->expr = s->expr;
if (lx->number == -1) {
lx->number = num_labels;
labels[num_labels++] = lx;
}
if (set_is_member (flownode->live_vars.out, var->number)) if (set_is_member (flownode->live_vars.out, var->number))
dagnode_attach_label (n, lx); dagnode_attach_label (n, lx);
} }
} }
dag = new_dag (); nodes = malloc (dag->num_nodes * sizeof (dagnode_t *));
dag->nodes = malloc (num_nodes * sizeof (dagnode_t *)); memcpy (nodes, dag->nodes, dag->num_nodes * sizeof (dagnode_t *));
memcpy (dag->nodes, nodes, num_nodes * sizeof (dagnode_t *)); dag->nodes = nodes;
dag->num_nodes = num_nodes; labels = malloc (dag->num_labels * sizeof (daglabel_t *));
dag->labels = malloc (num_labels * sizeof (daglabel_t *)); memcpy (labels, dag->labels, dag->num_labels * sizeof (daglabel_t *));
memcpy (dag->labels, labels, num_labels * sizeof (daglabel_t *)); dag->labels = labels;
dag->num_labels = num_labels;
dag->roots = set_new (); dag->roots = set_new ();
for (i = 0; i < num_nodes; i++) { for (i = 0; i < dag->num_nodes; i++) {
if (set_is_empty (dag->nodes[i]->parents)) if (set_is_empty (dag->nodes[i]->parents))
set_add (dag->roots, dag->nodes[i]->number); set_add (dag->roots, dag->nodes[i]->number);
} }