2012-05-08 02:44:16 +00:00
|
|
|
/*
|
|
|
|
dags.c
|
|
|
|
|
|
|
|
DAG representation of basic blocks
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/05/08
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
2012-12-13 00:51:48 +00:00
|
|
|
#include <ctype.h>
|
2012-05-08 02:44:16 +00:00
|
|
|
|
2013-01-22 05:09:41 +00:00
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/alloc.h"
|
2012-10-30 03:28:22 +00:00
|
|
|
#include "QF/dstring.h"
|
2012-11-07 05:11:26 +00:00
|
|
|
#include "QF/mathlib.h"
|
2012-12-06 11:52:53 +00:00
|
|
|
#include "QF/set.h"
|
2012-10-30 03:28:22 +00:00
|
|
|
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "dags.h"
|
2012-05-08 23:43:48 +00:00
|
|
|
#include "diagnostic.h"
|
2012-11-05 10:00:57 +00:00
|
|
|
#include "flow.h"
|
2012-12-13 00:51:48 +00:00
|
|
|
#include "function.h"
|
2012-05-08 23:43:48 +00:00
|
|
|
#include "qfcc.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "statements.h"
|
2012-10-30 03:28:22 +00:00
|
|
|
#include "strpool.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
#include "symtab.h"
|
2012-11-19 04:49:34 +00:00
|
|
|
#include "type.h"
|
2012-12-20 12:01:46 +00:00
|
|
|
#include "value.h"
|
2012-05-08 02:44:16 +00:00
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
static daglabel_t *labels_freelist;
|
|
|
|
static dagnode_t *nodes_freelist;
|
|
|
|
static dag_t *dags_freelist;
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-07-19 02:38:02 +00:00
|
|
|
static daglabel_t *daglabel_chain;
|
|
|
|
|
|
|
|
static void
|
|
|
|
flush_daglabels (void)
|
|
|
|
{
|
|
|
|
while (daglabel_chain) {
|
|
|
|
operand_t *op;
|
|
|
|
|
|
|
|
if ((op = daglabel_chain->op)) {
|
2012-12-05 10:47:22 +00:00
|
|
|
if (op->op_type == op_def)
|
|
|
|
op->o.def->daglabel = 0;
|
2012-07-19 02:38:02 +00:00
|
|
|
else if (op->op_type == op_temp)
|
|
|
|
op->o.tempop.daglabel = 0;
|
2012-12-05 10:47:22 +00:00
|
|
|
else if (op->op_type == op_value)
|
2012-07-19 02:38:02 +00:00
|
|
|
op->o.value->daglabel = 0;
|
2012-11-16 11:12:13 +00:00
|
|
|
else if (op->op_type == op_label)
|
|
|
|
op->o.label->daglabel = 0;
|
2012-07-19 02:38:02 +00:00
|
|
|
else
|
|
|
|
internal_error (0, "unexpected operand type");
|
|
|
|
}
|
|
|
|
daglabel_chain = daglabel_chain->daglabel_chain;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-17 12:14:42 +00:00
|
|
|
static dag_t *
|
|
|
|
new_dag (void)
|
|
|
|
{
|
|
|
|
dag_t *dag;
|
|
|
|
ALLOC (256, dag_t, dags, dag);
|
|
|
|
return dag;
|
|
|
|
}
|
|
|
|
|
2012-05-08 23:43:48 +00:00
|
|
|
static daglabel_t *
|
2012-11-17 12:55:59 +00:00
|
|
|
new_label (dag_t *dag)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
|
|
|
daglabel_t *label;
|
|
|
|
ALLOC (256, daglabel_t, labels, label);
|
2012-07-19 02:38:02 +00:00
|
|
|
label->daglabel_chain = daglabel_chain;
|
|
|
|
daglabel_chain = label;
|
2012-11-17 12:55:59 +00:00
|
|
|
label->number = dag->num_labels;
|
|
|
|
dag->labels[dag->num_labels++] = label;
|
2012-05-08 23:43:48 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dagnode_t *
|
2012-11-17 12:55:59 +00:00
|
|
|
new_node (dag_t *dag)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
|
|
|
dagnode_t *node;
|
|
|
|
ALLOC (256, dagnode_t, nodes, node);
|
2012-11-17 12:14:42 +00:00
|
|
|
node->parents = set_new ();
|
|
|
|
node->edges = set_new ();
|
|
|
|
node->identifiers = set_new ();
|
2012-11-17 12:55:59 +00:00
|
|
|
node->number = dag->num_nodes;
|
2012-11-21 07:11:37 +00:00
|
|
|
set_add (dag->roots, node->number); // nodes start out as root nodes
|
2012-11-17 12:55:59 +00:00
|
|
|
dag->nodes[dag->num_nodes++] = node;
|
2012-05-08 23:43:48 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2012-05-08 02:44:16 +00:00
|
|
|
const char *
|
|
|
|
daglabel_string (daglabel_t *label)
|
|
|
|
{
|
2012-10-30 03:28:22 +00:00
|
|
|
static dstring_t *str;
|
2012-07-15 12:10:51 +00:00
|
|
|
if ((label->opcode && label->op) || (!label->opcode && !label->op))
|
2012-05-08 02:44:16 +00:00
|
|
|
return "bad label";
|
|
|
|
if (label->opcode)
|
|
|
|
return label->opcode;
|
2012-10-30 03:28:22 +00:00
|
|
|
if (!str)
|
|
|
|
str = dstring_new ();
|
|
|
|
// operand_string might use quote_string, which returns a pointer to
|
|
|
|
// a static variable.
|
|
|
|
dstring_copystr (str, operand_string (label->op));
|
|
|
|
return quote_string (str->str);
|
2012-05-08 02:44:16 +00:00
|
|
|
}
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
static daglabel_t *
|
2012-11-17 12:55:59 +00:00
|
|
|
opcode_label (dag_t *dag, const char *opcode, expr_t *expr)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
|
|
|
daglabel_t *label;
|
2012-07-15 12:10:51 +00:00
|
|
|
|
2012-11-17 12:55:59 +00:00
|
|
|
label = new_label (dag);
|
2012-07-15 12:10:51 +00:00
|
|
|
label->opcode = opcode;
|
2012-11-17 03:07:27 +00:00
|
|
|
label->expr = expr;
|
2012-07-15 12:10:51 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
2012-11-06 23:50:28 +00:00
|
|
|
static daglabel_t *
|
2012-11-17 12:55:59 +00:00
|
|
|
operand_label (dag_t *dag, operand_t *op)
|
2012-07-15 12:10:51 +00:00
|
|
|
{
|
2012-12-05 10:47:22 +00:00
|
|
|
def_t *def = 0;
|
2012-07-19 01:42:05 +00:00
|
|
|
ex_value_t *val = 0;
|
2012-07-15 12:10:51 +00:00
|
|
|
daglabel_t *label;
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-11-15 06:53:51 +00:00
|
|
|
if (op->op_type == op_temp) {
|
2019-06-11 15:37:02 +00:00
|
|
|
//while (op->o.tempop.alias)
|
|
|
|
// op = op->o.tempop.alias;
|
2012-11-15 06:53:51 +00:00
|
|
|
if (op->o.tempop.daglabel)
|
|
|
|
return op->o.tempop.daglabel;
|
2012-11-17 12:55:59 +00:00
|
|
|
label = new_label (dag);
|
2012-07-16 08:37:13 +00:00
|
|
|
label->op = op;
|
2012-11-15 06:53:51 +00:00
|
|
|
op->o.tempop.daglabel = label;
|
2012-12-05 10:47:22 +00:00
|
|
|
} else if (op->op_type == op_def) {
|
|
|
|
def = op->o.def;
|
|
|
|
if (def->daglabel)
|
|
|
|
return def->daglabel;
|
2012-11-17 12:55:59 +00:00
|
|
|
label = new_label (dag);
|
2012-07-16 08:37:13 +00:00
|
|
|
label->op = op;
|
2012-12-05 10:47:22 +00:00
|
|
|
def->daglabel = label;
|
|
|
|
} else if (op->op_type == op_value) {
|
2012-11-15 06:53:51 +00:00
|
|
|
val = op->o.value;
|
2012-07-19 01:42:05 +00:00
|
|
|
if (val->daglabel)
|
|
|
|
return val->daglabel;
|
2012-11-17 12:55:59 +00:00
|
|
|
label = new_label (dag);
|
2012-07-19 01:42:05 +00:00
|
|
|
label->op = op;
|
|
|
|
val->daglabel = label;
|
2012-11-16 11:12:13 +00:00
|
|
|
} else if (op->op_type == op_label) {
|
|
|
|
if (op->o.label->daglabel)
|
|
|
|
return op->o.label->daglabel;
|
2012-11-17 12:55:59 +00:00
|
|
|
label = new_label (dag);
|
2012-11-16 11:12:13 +00:00
|
|
|
label->op = op;
|
|
|
|
op->o.label->daglabel = label;
|
2012-07-19 01:42:05 +00:00
|
|
|
} else {
|
2012-11-16 11:12:13 +00:00
|
|
|
internal_error (0, "unexpected operand type: %d", op->op_type);
|
2012-07-16 08:37:13 +00:00
|
|
|
}
|
2012-07-15 12:10:51 +00:00
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static dagnode_t *
|
2012-11-17 12:55:59 +00:00
|
|
|
leaf_node (dag_t *dag, operand_t *op, expr_t *expr)
|
2012-07-15 12:10:51 +00:00
|
|
|
{
|
|
|
|
daglabel_t *label;
|
|
|
|
dagnode_t *node;
|
|
|
|
|
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-11-17 12:55:59 +00:00
|
|
|
node = new_node (dag);
|
2012-11-15 08:58:33 +00:00
|
|
|
node->tl = op->type;
|
2012-11-17 12:55:59 +00:00
|
|
|
label = operand_label (dag, op);
|
2012-07-15 12:10:51 +00:00
|
|
|
label->dagnode = node;
|
2012-11-17 03:07:27 +00:00
|
|
|
label->expr = expr;
|
2012-11-15 08:58:33 +00:00
|
|
|
node->label = label;
|
2012-05-08 23:43:48 +00:00
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2018-10-09 03:35:01 +00:00
|
|
|
static __attribute__((pure)) dagnode_t *
|
2012-11-21 07:11:37 +00:00
|
|
|
dag_node (operand_t *op)
|
2012-07-15 12:10:51 +00:00
|
|
|
{
|
2012-12-05 10:47:22 +00:00
|
|
|
def_t *def;
|
2012-11-20 05:23:43 +00:00
|
|
|
dagnode_t *node = 0;
|
2012-07-15 12:10:51 +00:00
|
|
|
|
|
|
|
if (!op)
|
|
|
|
return 0;
|
2012-12-05 10:47:22 +00:00
|
|
|
if (op->op_type == op_def) {
|
|
|
|
def = op->o.def;
|
|
|
|
if (def->daglabel)
|
|
|
|
node = def->daglabel->dagnode;
|
2012-11-15 06:53:51 +00:00
|
|
|
} else if (op->op_type == op_temp) {
|
2012-12-11 06:20:52 +00:00
|
|
|
while (op->o.tempop.alias)
|
|
|
|
op = op->o.tempop.alias;
|
2012-11-15 06:53:51 +00:00
|
|
|
if (op->o.tempop.daglabel)
|
2012-11-20 05:23:43 +00:00
|
|
|
node = op->o.tempop.daglabel->dagnode;
|
2012-12-05 10:47:22 +00:00
|
|
|
} else if (op->op_type == op_value) {
|
2012-11-20 05:23:43 +00:00
|
|
|
if (op->o.value->daglabel)
|
|
|
|
node = op->o.value->daglabel->dagnode;
|
|
|
|
} else if (op->op_type == op_label) {
|
|
|
|
if (op->o.label->daglabel)
|
|
|
|
node = op->o.label->daglabel->dagnode;
|
2012-07-16 08:37:13 +00:00
|
|
|
}
|
2012-11-20 05:30:42 +00:00
|
|
|
if (node && node->killed)
|
|
|
|
node = 0;
|
2012-11-20 05:23:43 +00:00
|
|
|
return node;
|
2012-07-15 12:10:51 +00:00
|
|
|
}
|
|
|
|
|
2012-11-21 07:11:37 +00:00
|
|
|
static void
|
|
|
|
dag_make_children (dag_t *dag, statement_t *s, operand_t *operands[4],
|
|
|
|
dagnode_t *children[3])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
flow_analyze_statement (s, 0, 0, 0, operands);
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if (!(children[i] = dag_node (operands[i + 1])))
|
|
|
|
children[i] = leaf_node (dag, operands[i + 1], s->expr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-20 06:22:05 +00:00
|
|
|
static int
|
|
|
|
dagnode_deref_match (const dagnode_t *n, const daglabel_t *op,
|
2012-11-21 07:11:37 +00:00
|
|
|
dagnode_t *children[3])
|
2012-11-20 06:22:05 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2; i++) {
|
2012-12-20 07:33:01 +00:00
|
|
|
if (n->children[i] != children[i + 1])
|
2012-11-20 06:22:05 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-07-15 12:10:51 +00:00
|
|
|
static int
|
|
|
|
dagnode_match (const dagnode_t *n, const daglabel_t *op,
|
2012-11-21 07:11:37 +00:00
|
|
|
dagnode_t *children[3])
|
2012-07-15 12:10:51 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
int i;
|
|
|
|
|
2012-11-20 05:30:42 +00:00
|
|
|
if (n->killed)
|
|
|
|
return 0;
|
2012-11-20 06:22:05 +00:00
|
|
|
if (!strcmp (op->opcode, ".")
|
|
|
|
&& n->label->opcode && !strcmp (n->label->opcode, ".="))
|
2012-11-21 07:11:37 +00:00
|
|
|
return dagnode_deref_match (n, op, children);
|
2012-07-15 12:10:51 +00:00
|
|
|
if (n->label->opcode != op->opcode)
|
|
|
|
return 0;
|
2012-11-17 12:14:42 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
2012-11-22 12:23:46 +00:00
|
|
|
if (n->children[i] != children[i])
|
2012-11-17 12:14:42 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2012-07-15 12:10:51 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-11-21 07:11:37 +00:00
|
|
|
static dagnode_t *
|
|
|
|
dagnode_search (dag_t *dag, daglabel_t *op, dagnode_t *children[3])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < dag->num_nodes; i++)
|
|
|
|
if (dagnode_match (dag->nodes[i], op, children))
|
|
|
|
return dag->nodes[i];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dagnode_add_children (dag_t *dag, dagnode_t *n, operand_t *operands[4],
|
|
|
|
dagnode_t *children[3])
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
dagnode_t *child = children[i];
|
|
|
|
if ((n->children[i] = child)) {
|
|
|
|
n->types[i] = operands[i + 1]->type;
|
|
|
|
set_remove (dag->roots, child->number);
|
|
|
|
set_add (child->parents, n->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-12 02:46:33 +00:00
|
|
|
static int
|
2019-06-16 07:56:39 +00:00
|
|
|
dagnode_tempop_set_edges_visit (tempop_t *tempop, void *_node)
|
|
|
|
{
|
|
|
|
dagnode_t *node = (dagnode_t *) _node;
|
|
|
|
daglabel_t *label;
|
|
|
|
|
|
|
|
label = tempop->daglabel;
|
|
|
|
if (label && label->dagnode) {
|
|
|
|
set_add (node->edges, label->dagnode->number);
|
|
|
|
label->live = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dagnode_def_set_edges_visit (def_t *def, void *_node)
|
2012-12-12 02:46:33 +00:00
|
|
|
{
|
|
|
|
dagnode_t *node = (dagnode_t *) _node;
|
|
|
|
daglabel_t *label;
|
|
|
|
|
|
|
|
label = def->daglabel;
|
|
|
|
if (label && label->dagnode) {
|
|
|
|
set_add (node->edges, label->dagnode->number);
|
|
|
|
label->live = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-12-13 00:51:48 +00:00
|
|
|
static int
|
2012-12-22 05:38:38 +00:00
|
|
|
dag_find_node (def_t *def, void *_daglabel)
|
2012-12-13 00:51:48 +00:00
|
|
|
{
|
2012-12-22 05:38:38 +00:00
|
|
|
daglabel_t **daglabel = (daglabel_t **) _daglabel;
|
|
|
|
if (def->daglabel && def->daglabel->dagnode) {
|
|
|
|
*daglabel = def->daglabel;
|
2012-12-13 00:51:48 +00:00
|
|
|
return def->daglabel->dagnode->number + 1; // ensure never 0
|
2012-12-22 05:38:38 +00:00
|
|
|
}
|
2012-12-13 00:51:48 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-11-21 07:11:37 +00:00
|
|
|
static void
|
2012-12-13 00:51:48 +00:00
|
|
|
dagnode_set_edges (dag_t *dag, dagnode_t *n)
|
2012-11-21 07:11:37 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2012-11-24 03:53:51 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
dagnode_t *child = n->children[i];
|
|
|
|
if (child && n != child)
|
|
|
|
set_add (n->edges, child->number);
|
|
|
|
}
|
|
|
|
if (n->type == st_flow)
|
|
|
|
return;
|
2012-11-21 07:11:37 +00:00
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
dagnode_t *child = n->children[i];
|
|
|
|
if (child) {
|
|
|
|
if (child->label->op) {
|
|
|
|
dagnode_t *node = child->label->dagnode;
|
2012-12-11 10:52:58 +00:00
|
|
|
operand_t *op = child->label->op;
|
2019-06-16 07:55:54 +00:00
|
|
|
if (node != child && node != n) {
|
2012-11-21 07:11:37 +00:00
|
|
|
set_add (node->edges, n->number);
|
2019-06-16 07:55:54 +00:00
|
|
|
}
|
2012-12-12 03:55:17 +00:00
|
|
|
if (op->op_type == op_value
|
2018-10-12 13:05:17 +00:00
|
|
|
&& op->o.value->lltype == ev_pointer
|
2019-06-16 07:55:54 +00:00
|
|
|
&& op->o.value->v.pointer.def) {
|
2012-12-12 03:55:17 +00:00
|
|
|
def_visit_all (op->o.value->v.pointer.def, 1,
|
2019-06-16 07:56:39 +00:00
|
|
|
dagnode_def_set_edges_visit, n);
|
2019-06-16 07:55:54 +00:00
|
|
|
}
|
2012-12-12 03:52:01 +00:00
|
|
|
if (op->op_type == op_def
|
2019-06-16 07:55:54 +00:00
|
|
|
&& (op->o.def->alias || op->o.def->alias_defs)) {
|
2019-06-16 07:56:39 +00:00
|
|
|
def_visit_all (op->o.def, 1,
|
|
|
|
dagnode_def_set_edges_visit, n);
|
|
|
|
}
|
2019-06-16 10:17:01 +00:00
|
|
|
if (op->op_type == op_temp
|
|
|
|
&& (op->o.tempop.alias || op->o.tempop.alias_ops)) {
|
2019-06-16 07:56:39 +00:00
|
|
|
tempop_visit_all (&op->o.tempop, 1,
|
|
|
|
dagnode_tempop_set_edges_visit, n);
|
|
|
|
}
|
2012-11-21 07:11:37 +00:00
|
|
|
}
|
|
|
|
if (n != child)
|
|
|
|
set_add (n->edges, child->number);
|
|
|
|
}
|
|
|
|
}
|
2012-12-13 00:51:48 +00:00
|
|
|
if (n->type == st_func) {
|
|
|
|
const char *num_params = 0;
|
|
|
|
int first_param = 0;
|
|
|
|
flowvar_t **flowvars = dag->flownode->graph->func->vars;
|
|
|
|
|
|
|
|
if (!strncmp (n->label->opcode, "<RCALL", 6)) {
|
|
|
|
num_params = n->label->opcode + 6;
|
|
|
|
first_param = 2;
|
|
|
|
} else if (!strncmp (n->label->opcode, "<CALL", 5)) {
|
|
|
|
num_params = n->label->opcode + 5;
|
2012-12-20 12:01:46 +00:00
|
|
|
} else if (!strcmp (n->label->opcode, "<RETURN>")) {
|
|
|
|
daglabel_t *label = n->children[0]->label;
|
|
|
|
if (!label->op) {
|
|
|
|
set_iter_t *lab_i;
|
|
|
|
for (lab_i = set_first (n->children[0]->identifiers); lab_i;
|
|
|
|
lab_i = set_next (lab_i)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
label = dag->labels[lab_i->element];
|
2012-12-20 12:01:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
label->live = 1;
|
2012-12-13 00:51:48 +00:00
|
|
|
}
|
|
|
|
if (num_params && isdigit (*num_params)) {
|
|
|
|
for (i = first_param; i < *num_params - '0'; i++) {
|
|
|
|
flowvar_t *var = flowvars[i + 1];
|
|
|
|
def_t *param_def = var->op->o.def;
|
2012-12-22 05:38:38 +00:00
|
|
|
daglabel_t *daglabel;
|
2012-12-13 00:51:48 +00:00
|
|
|
int param_node;
|
|
|
|
|
|
|
|
// FIXME hopefully only the one alias :P
|
2012-12-22 05:38:38 +00:00
|
|
|
param_node = def_visit_all (param_def, 0, dag_find_node,
|
|
|
|
&daglabel);
|
2012-12-13 00:51:48 +00:00
|
|
|
if (!param_node) {
|
|
|
|
bug (0, ".param_%d not set for %s", i, n->label->opcode);
|
|
|
|
continue;
|
|
|
|
}
|
2012-12-22 05:38:38 +00:00
|
|
|
daglabel->live = 1;
|
2012-12-13 00:51:48 +00:00
|
|
|
set_add (n->edges, param_node - 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-11-21 07:11:37 +00:00
|
|
|
}
|
|
|
|
|
2012-07-19 00:59:17 +00:00
|
|
|
static int
|
2012-11-20 06:19:32 +00:00
|
|
|
op_is_identifier (operand_t *op)
|
2012-07-19 00:59:17 +00:00
|
|
|
{
|
2012-11-20 05:23:43 +00:00
|
|
|
if (op->op_type == op_label)
|
|
|
|
return 0;
|
|
|
|
if (op->op_type == op_value)
|
|
|
|
return 0;
|
2012-07-19 00:59:17 +00:00
|
|
|
if (op->op_type == op_temp)
|
|
|
|
return 1;
|
2012-12-05 10:47:22 +00:00
|
|
|
if (op->op_type != op_def)
|
2012-07-19 00:59:17 +00:00
|
|
|
return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-12-13 05:43:43 +00:00
|
|
|
static int
|
2019-06-16 07:56:39 +00:00
|
|
|
dag_tempop_kill_aliases_visit (tempop_t *tempop, void *_l)
|
|
|
|
{
|
|
|
|
daglabel_t *l = (daglabel_t *) _l;
|
|
|
|
dagnode_t *node = l->dagnode;;
|
|
|
|
daglabel_t *label;
|
|
|
|
|
|
|
|
if (tempop == &l->op->o.tempop)
|
|
|
|
return 0;
|
|
|
|
label = tempop->daglabel;
|
|
|
|
if (label && label->dagnode) {
|
|
|
|
set_add (node->edges, label->dagnode->number);
|
|
|
|
set_remove (node->edges, node->number);
|
|
|
|
label->dagnode->killed = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dag_def_kill_aliases_visit (def_t *def, void *_l)
|
2012-12-13 05:43:43 +00:00
|
|
|
{
|
2012-12-13 12:06:04 +00:00
|
|
|
daglabel_t *l = (daglabel_t *) _l;
|
|
|
|
dagnode_t *node = l->dagnode;;
|
2012-12-13 05:43:43 +00:00
|
|
|
daglabel_t *label;
|
|
|
|
|
2012-12-13 12:06:04 +00:00
|
|
|
if (def == l->op->o.def)
|
|
|
|
return 0;
|
2012-12-13 05:43:43 +00:00
|
|
|
label = def->daglabel;
|
|
|
|
if (label && label->dagnode) {
|
|
|
|
set_add (node->edges, label->dagnode->number);
|
|
|
|
set_remove (node->edges, node->number);
|
|
|
|
label->dagnode->killed = 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dag_kill_aliases (daglabel_t *l)
|
|
|
|
{
|
|
|
|
operand_t *op = l->op;
|
|
|
|
|
|
|
|
if (op->op_type == op_temp) {
|
2019-06-16 10:17:01 +00:00
|
|
|
if (op->o.tempop.alias || op->o.tempop.alias_ops) {
|
|
|
|
tempop_visit_all (&op->o.tempop, 1,
|
|
|
|
dag_tempop_kill_aliases_visit, l);
|
|
|
|
}
|
2012-12-13 05:43:43 +00:00
|
|
|
} else if (op->op_type == op_def) {
|
2019-06-16 07:55:54 +00:00
|
|
|
if (op->o.def->alias || op->o.def->alias_defs) {
|
2019-06-16 07:56:39 +00:00
|
|
|
def_visit_all (op->o.def, 1, dag_def_kill_aliases_visit, l);
|
2019-06-16 07:55:54 +00:00
|
|
|
}
|
2012-12-13 05:43:43 +00:00
|
|
|
} else {
|
|
|
|
internal_error (0, "rvalue assignment?");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-13 12:20:21 +00:00
|
|
|
static int
|
2019-06-16 07:56:39 +00:00
|
|
|
dag_tempop_live_aliases (tempop_t *tempop, void *_t)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (tempop != _t && tempop->daglabel)
|
|
|
|
tempop->daglabel->live = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dag_def_live_aliases (def_t *def, void *_d)
|
2012-12-13 12:20:21 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
if (def != _d && def->daglabel)
|
|
|
|
def->daglabel->live = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-06-16 10:17:01 +00:00
|
|
|
static void
|
|
|
|
dag_live_aliases(operand_t *op)
|
|
|
|
{
|
|
|
|
// FIXME it would be better to propogate the aliasing
|
|
|
|
if (op->op_type == op_temp
|
|
|
|
&& (op->o.tempop.alias || op->o.tempop.alias_ops)) {
|
|
|
|
tempop_visit_all (&op->o.tempop, 1, dag_tempop_live_aliases,
|
|
|
|
&op->o.tempop);
|
|
|
|
}
|
|
|
|
if (op->op_type == op_def
|
|
|
|
&& (op->o.def->alias || op->o.def->alias_defs)) {
|
|
|
|
def_visit_all (op->o.def, 1, dag_def_live_aliases, op->o.def);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-16 10:17:45 +00:00
|
|
|
static void
|
|
|
|
dag_make_var_live (set_t *live_vars, operand_t *op)
|
|
|
|
{
|
|
|
|
flowvar_t *var = 0;
|
|
|
|
|
|
|
|
if (op) {
|
|
|
|
dag_live_aliases (op);
|
|
|
|
var = flow_get_var (op);
|
|
|
|
}
|
|
|
|
if (var)
|
|
|
|
set_add (live_vars, var->number);
|
|
|
|
}
|
|
|
|
|
2012-07-19 00:27:42 +00:00
|
|
|
static void
|
|
|
|
dagnode_attach_label (dagnode_t *n, daglabel_t *l)
|
|
|
|
{
|
2012-07-19 00:59:17 +00:00
|
|
|
if (!l->op)
|
|
|
|
internal_error (0, "attempt to attach operator label to dagnode "
|
2012-12-20 11:55:25 +00:00
|
|
|
"identifiers");
|
2012-11-20 06:19:32 +00:00
|
|
|
if (!op_is_identifier (l->op))
|
2012-07-19 00:59:17 +00:00
|
|
|
internal_error (0, "attempt to attach non-identifer label to dagnode "
|
2012-12-20 11:55:25 +00:00
|
|
|
"identifiers");
|
2012-11-18 03:56:27 +00:00
|
|
|
if (l->dagnode) {
|
2018-08-23 11:07:22 +00:00
|
|
|
// if the node is a leaf, then kill its value so no attempt is made
|
|
|
|
// to reuse it.
|
|
|
|
if (l->dagnode->type == st_none) {
|
|
|
|
l->dagnode->killed = 1;
|
|
|
|
}
|
2012-11-18 03:56:27 +00:00
|
|
|
dagnode_t *node = l->dagnode;
|
|
|
|
set_union (n->edges, node->parents);
|
|
|
|
set_remove (n->edges, n->number);
|
|
|
|
set_remove (node->identifiers, l->number);
|
|
|
|
}
|
2012-12-11 10:52:58 +00:00
|
|
|
l->live = 0; // remove live forcing on assignment
|
2012-07-19 00:27:42 +00:00
|
|
|
l->dagnode = n;
|
2012-11-17 12:14:42 +00:00
|
|
|
set_add (n->identifiers, l->number);
|
2012-12-13 05:43:43 +00:00
|
|
|
dag_kill_aliases (l);
|
2012-12-13 12:20:21 +00:00
|
|
|
if (n->label->op) {
|
2019-06-16 10:17:01 +00:00
|
|
|
dag_live_aliases (n->label->op);
|
2012-12-13 12:20:21 +00:00
|
|
|
}
|
2012-07-19 00:27:42 +00:00
|
|
|
}
|
|
|
|
|
2012-12-12 14:15:55 +00:00
|
|
|
static int
|
2019-06-16 07:56:39 +00:00
|
|
|
dag_tempop_alias_live (tempop_t *tempop, void *_live_vars)
|
|
|
|
{
|
|
|
|
set_t *live_vars = (set_t *) _live_vars;
|
|
|
|
if (!tempop->flowvar)
|
|
|
|
return 0;
|
|
|
|
return set_is_member (live_vars, tempop->flowvar->number);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
dag_def_alias_live (def_t *def, void *_live_vars)
|
2012-12-12 14:15:55 +00:00
|
|
|
{
|
|
|
|
set_t *live_vars = (set_t *) _live_vars;
|
|
|
|
if (!def->flowvar)
|
|
|
|
return 0;
|
|
|
|
return set_is_member (live_vars, def->flowvar->number);
|
|
|
|
}
|
|
|
|
|
2012-11-18 00:23:01 +00:00
|
|
|
static void
|
2012-11-19 06:18:31 +00:00
|
|
|
dag_remove_dead_vars (dag_t *dag, set_t *live_vars)
|
2012-11-18 00:23:01 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < dag->num_labels; i++) {
|
|
|
|
daglabel_t *l = dag->labels[i];
|
|
|
|
flowvar_t *var;
|
|
|
|
|
|
|
|
if (!l->op || !l->dagnode)
|
|
|
|
continue;
|
2012-12-11 10:52:58 +00:00
|
|
|
if (l->live) // label forced live (probably via an alias)
|
|
|
|
continue;
|
2012-11-18 00:23:01 +00:00
|
|
|
var = flow_get_var (l->op);
|
|
|
|
if (!var)
|
|
|
|
continue;
|
2012-11-24 05:56:30 +00:00
|
|
|
if (set_is_member (dag->flownode->global_vars, var->number))
|
|
|
|
continue;
|
2012-12-12 14:15:55 +00:00
|
|
|
if (l->op->op_type == op_def
|
2019-06-16 07:56:39 +00:00
|
|
|
&& def_visit_all (l->op->o.def, 1, dag_def_alias_live, live_vars))
|
|
|
|
continue;
|
|
|
|
if (l->op->op_type == op_temp
|
|
|
|
&& tempop_visit_all (&l->op->o.tempop, 1, dag_tempop_alias_live,
|
|
|
|
live_vars))
|
2012-12-12 14:15:55 +00:00
|
|
|
continue;
|
2012-11-19 06:18:31 +00:00
|
|
|
if (!set_is_member (live_vars, var->number))
|
2012-11-18 00:23:01 +00:00
|
|
|
set_remove (l->dagnode->identifiers, l->number);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dag_sort_visit (dag_t *dag, set_t *visited, int node_index, int *topo)
|
|
|
|
{
|
|
|
|
set_iter_t *node_iter;
|
|
|
|
dagnode_t *node;
|
|
|
|
|
|
|
|
if (set_is_member (visited, node_index))
|
|
|
|
return;
|
|
|
|
set_add (visited, node_index);
|
|
|
|
node = dag->nodes[node_index];
|
|
|
|
for (node_iter = set_first (node->edges); node_iter;
|
|
|
|
node_iter = set_next (node_iter))
|
2012-12-21 05:09:00 +00:00
|
|
|
dag_sort_visit (dag, visited, node_iter->element, topo);
|
2012-11-18 00:23:01 +00:00
|
|
|
node->topo = *topo;
|
|
|
|
dag->topo[(*topo)++] = node_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
dag_sort_nodes (dag_t *dag)
|
|
|
|
{
|
|
|
|
set_iter_t *root_iter;
|
|
|
|
set_t *visited = set_new ();
|
|
|
|
int topo = 0;
|
2012-12-25 08:07:58 +00:00
|
|
|
int *tmp_topo;
|
2012-11-18 00:23:01 +00:00
|
|
|
|
2012-12-25 08:07:58 +00:00
|
|
|
if (dag->topo)
|
|
|
|
free (dag->topo);
|
|
|
|
dag->topo = alloca (dag->num_nodes * sizeof (int));
|
2012-11-18 00:23:01 +00:00
|
|
|
for (root_iter = set_first (dag->roots); root_iter;
|
|
|
|
root_iter = set_next (root_iter))
|
2012-12-21 05:09:00 +00:00
|
|
|
dag_sort_visit (dag, visited, root_iter->element, &topo);
|
2012-11-18 00:23:01 +00:00
|
|
|
set_delete (visited);
|
2012-12-25 08:07:58 +00:00
|
|
|
tmp_topo = malloc (topo * sizeof (int));
|
|
|
|
memcpy (tmp_topo, dag->topo, topo * sizeof (int));
|
|
|
|
dag->topo = tmp_topo;
|
|
|
|
dag->num_topo = topo;
|
2012-11-18 00:23:01 +00:00
|
|
|
}
|
|
|
|
|
2012-11-20 05:30:42 +00:00
|
|
|
static void
|
|
|
|
dag_kill_nodes (dag_t *dag, dagnode_t *n)
|
|
|
|
{
|
|
|
|
int i;
|
2012-11-20 06:28:57 +00:00
|
|
|
dagnode_t *node;
|
2012-11-20 05:30:42 +00:00
|
|
|
|
2012-11-20 06:28:57 +00:00
|
|
|
for (i = 0; i < dag->num_nodes; i++) {
|
|
|
|
node = dag->nodes[i];
|
|
|
|
if (node == n->children[1]) {
|
|
|
|
// assume the pointer does not point to itself. This should be
|
|
|
|
// reasonable because without casting, only a void pointer can
|
|
|
|
// point to itself (the required type is recursive).
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (node->label->op && !op_is_identifier (node->label->op)) {
|
|
|
|
// While constants in the Quake VM can be changed via a pointer,
|
|
|
|
// doing so would cause much more fun than a simple
|
|
|
|
// mis-optimization would, so consider them safe from pointer
|
|
|
|
// operations.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
node->killed = 1;
|
|
|
|
}
|
2012-11-20 05:30:42 +00:00
|
|
|
n->killed = 0;
|
|
|
|
}
|
|
|
|
|
2012-11-17 12:14:42 +00:00
|
|
|
dag_t *
|
2012-11-18 00:23:01 +00:00
|
|
|
dag_create (flownode_t *flownode)
|
2012-05-08 23:43:48 +00:00
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
dag_t *dag;
|
|
|
|
sblock_t *block = flownode->sblock;
|
2012-05-08 23:43:48 +00:00
|
|
|
statement_t *s;
|
2012-11-17 12:55:59 +00:00
|
|
|
dagnode_t **nodes;
|
2012-11-17 12:14:42 +00:00
|
|
|
daglabel_t **labels;
|
|
|
|
int num_statements = 0;
|
2012-11-19 06:18:31 +00:00
|
|
|
set_t *live_vars = set_new ();
|
2012-07-19 02:38:02 +00:00
|
|
|
|
|
|
|
flush_daglabels ();
|
2012-05-08 23:43:48 +00:00
|
|
|
|
2012-11-24 03:53:51 +00:00
|
|
|
// count the number of statements so the number of nodes and labels can be
|
|
|
|
// guessed
|
2012-11-17 12:14:42 +00:00
|
|
|
for (s = block->statements; s; s = s->next)
|
|
|
|
num_statements++;
|
|
|
|
|
2012-11-19 06:18:31 +00:00
|
|
|
set_assign (live_vars, flownode->live_vars.out);
|
|
|
|
|
2012-11-17 12:55:59 +00:00
|
|
|
dag = new_dag ();
|
2012-11-18 00:23:01 +00:00
|
|
|
dag->flownode = flownode;
|
2012-11-17 12:14:42 +00:00
|
|
|
// at most 4 per statement
|
2012-11-17 12:55:59 +00:00
|
|
|
dag->nodes = alloca (num_statements * 4 * sizeof (dagnode_t));
|
2012-12-13 03:48:07 +00:00
|
|
|
// at most 4 per statement, + return + params
|
|
|
|
dag->labels = alloca (num_statements * (4 + 1 + 8) * sizeof (daglabel_t));
|
2012-11-18 01:50:10 +00:00
|
|
|
dag->roots = set_new ();
|
2012-11-17 12:14:42 +00:00
|
|
|
|
2012-05-08 23:43:48 +00:00
|
|
|
for (s = block->statements; s; s = s->next) {
|
2012-11-16 11:03:30 +00:00
|
|
|
operand_t *operands[4];
|
2012-11-17 12:14:42 +00:00
|
|
|
dagnode_t *n = 0, *children[3] = {0, 0, 0};
|
2012-07-17 05:03:04 +00:00
|
|
|
daglabel_t *op, *lx;
|
2012-11-24 03:53:51 +00:00
|
|
|
int i;
|
2012-11-21 07:11:37 +00:00
|
|
|
|
|
|
|
dag_make_children (dag, s, operands, children);
|
2019-06-16 10:19:03 +00:00
|
|
|
if (s->type == st_flow || s->type == st_func)
|
2012-11-24 03:53:51 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (children[i])
|
2019-06-16 10:21:02 +00:00
|
|
|
dag_make_var_live (live_vars, operands[i + 1]);
|
2012-11-17 12:55:59 +00:00
|
|
|
op = opcode_label (dag, s->opcode, s->expr);
|
2012-11-21 07:11:37 +00:00
|
|
|
n = children[0];
|
|
|
|
if (s->type != st_assign
|
|
|
|
&& !(n = dagnode_search (dag, op, children))) {
|
2012-11-17 12:55:59 +00:00
|
|
|
n = new_node (dag);
|
2012-11-16 13:09:49 +00:00
|
|
|
n->type = s->type;
|
2012-07-15 12:10:51 +00:00
|
|
|
n->label = op;
|
2012-11-21 07:11:37 +00:00
|
|
|
dagnode_add_children (dag, n, operands, children);
|
2012-12-13 00:51:48 +00:00
|
|
|
dagnode_set_edges (dag, n);
|
2012-07-15 12:10:51 +00:00
|
|
|
}
|
2012-11-17 12:55:59 +00:00
|
|
|
lx = operand_label (dag, operands[0]);
|
2012-11-21 03:16:10 +00:00
|
|
|
if (lx && lx->dagnode != n) {
|
2012-11-17 03:07:27 +00:00
|
|
|
lx->expr = s->expr;
|
2012-11-17 13:08:46 +00:00
|
|
|
dagnode_attach_label (n, lx);
|
2012-11-07 02:49:17 +00:00
|
|
|
}
|
2012-11-20 05:30:42 +00:00
|
|
|
if (n->type == st_ptrassign)
|
|
|
|
dag_kill_nodes (dag, n);
|
2012-11-07 02:49:17 +00:00
|
|
|
}
|
2012-11-18 01:50:10 +00:00
|
|
|
|
2012-11-17 12:55:59 +00:00
|
|
|
nodes = malloc (dag->num_nodes * sizeof (dagnode_t *));
|
|
|
|
memcpy (nodes, dag->nodes, dag->num_nodes * sizeof (dagnode_t *));
|
|
|
|
dag->nodes = nodes;
|
|
|
|
labels = malloc (dag->num_labels * sizeof (daglabel_t *));
|
|
|
|
memcpy (labels, dag->labels, dag->num_labels * sizeof (daglabel_t *));
|
|
|
|
dag->labels = labels;
|
2012-11-18 01:50:10 +00:00
|
|
|
|
2012-11-19 06:18:31 +00:00
|
|
|
dag_remove_dead_vars (dag, live_vars);
|
2012-11-18 00:23:01 +00:00
|
|
|
dag_sort_nodes (dag);
|
2012-11-19 06:18:31 +00:00
|
|
|
set_delete (live_vars);
|
2012-11-17 12:14:42 +00:00
|
|
|
return dag;
|
2012-05-08 23:43:48 +00:00
|
|
|
}
|
2012-11-07 05:11:26 +00:00
|
|
|
|
2012-11-17 12:14:42 +00:00
|
|
|
static statement_t *
|
|
|
|
build_statement (const char *opcode, operand_t **operands, expr_t *expr)
|
|
|
|
{
|
2012-11-18 11:27:26 +00:00
|
|
|
int i;
|
|
|
|
operand_t *op;
|
2012-11-17 12:14:42 +00:00
|
|
|
statement_t *st = new_statement (st_none, opcode, expr);
|
2012-11-18 11:27:26 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 3; i++) {
|
|
|
|
if ((op = operands[i])) {
|
2012-12-11 06:52:37 +00:00
|
|
|
while (op->op_type == op_alias)
|
|
|
|
op = op->o.alias;
|
2012-12-05 10:47:22 +00:00
|
|
|
if (op->op_type == op_temp) {
|
|
|
|
while (op->o.tempop.alias)
|
|
|
|
op = op->o.tempop.alias;
|
2012-11-18 11:27:26 +00:00
|
|
|
op->o.tempop.users++;
|
2012-12-05 10:47:22 +00:00
|
|
|
}
|
2012-11-18 11:27:26 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 12:14:42 +00:00
|
|
|
st->opa = operands[0];
|
|
|
|
st->opb = operands[1];
|
|
|
|
st->opc = operands[2];
|
|
|
|
return st;
|
|
|
|
}
|
|
|
|
#if 0
|
2012-11-07 05:11:26 +00:00
|
|
|
static void
|
|
|
|
dag_calc_node_costs (dagnode_t *dagnode)
|
|
|
|
{
|
2012-11-17 12:14:42 +00:00
|
|
|
int i;
|
2012-11-07 05:11:26 +00:00
|
|
|
|
2012-11-17 12:14:42 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (dagnode->children[i])
|
|
|
|
dag_calc_node_costs (dagnode->children[i]);
|
2012-11-07 05:11:26 +00:00
|
|
|
|
|
|
|
// if dagnode->a is null, then this is a leaf (as b and c are guaranted to
|
|
|
|
// be null)
|
2012-11-17 12:14:42 +00:00
|
|
|
if (!dagnode->children[0]) {
|
2012-11-07 05:11:26 +00:00
|
|
|
// Because qc vm statements don't mix source and destination operands,
|
|
|
|
// leaves never need temporary variables.
|
|
|
|
dagnode->cost = 0;
|
|
|
|
} else {
|
|
|
|
int different = 0;
|
|
|
|
|
2012-11-17 12:14:42 +00:00
|
|
|
// a non-leaf is guaranteed to have a valid first child
|
|
|
|
dagnode->cost = dagnode->children[0]->cost;
|
|
|
|
for (i = 1; i < 3; i++) {
|
|
|
|
if (dagnode->children[i]
|
|
|
|
&& dagnode->children[i]->cost != dagnode->cost) {
|
|
|
|
dagnode->cost = max (dagnode->cost,
|
|
|
|
dagnode->children[i]->cost);
|
|
|
|
different = 1;
|
|
|
|
}
|
2012-11-07 05:11:26 +00:00
|
|
|
}
|
|
|
|
if (!different)
|
|
|
|
dagnode->cost += 1;
|
|
|
|
}
|
|
|
|
}
|
2012-11-17 12:14:42 +00:00
|
|
|
#endif
|
2012-12-11 06:52:37 +00:00
|
|
|
static operand_t *
|
2019-06-17 14:38:34 +00:00
|
|
|
fix_op_type (operand_t *op, type_t *type)
|
2012-12-11 06:52:37 +00:00
|
|
|
{
|
|
|
|
if (op && op->op_type != op_label && op->type != type)
|
|
|
|
op = alias_operand (type, op);
|
|
|
|
return op;
|
|
|
|
}
|
2012-11-15 08:15:34 +00:00
|
|
|
|
2012-12-13 03:48:07 +00:00
|
|
|
static operand_t *
|
|
|
|
make_operand (dag_t *dag, sblock_t *block, const dagnode_t *dagnode, int index)
|
|
|
|
{
|
|
|
|
operand_t *op;
|
|
|
|
|
|
|
|
op = dagnode->children[index]->value;
|
|
|
|
op = fix_op_type (op, dagnode->types[index]);
|
|
|
|
return op;
|
|
|
|
}
|
|
|
|
|
|
|
|
static operand_t *
|
|
|
|
generate_moves (dag_t *dag, sblock_t *block, dagnode_t *dagnode)
|
|
|
|
{
|
|
|
|
set_iter_t *var_iter;
|
|
|
|
daglabel_t *var;
|
|
|
|
operand_t *operands[3] = {0, 0, 0};
|
|
|
|
statement_t *st;
|
|
|
|
operand_t *dst;
|
|
|
|
|
|
|
|
operands[0] = make_operand (dag, block, dagnode, 0);
|
|
|
|
operands[1] = make_operand (dag, block, dagnode, 1);
|
|
|
|
dst = operands[0];
|
|
|
|
for (var_iter = set_first (dagnode->identifiers); var_iter;
|
|
|
|
var_iter = set_next (var_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
var = dag->labels[var_iter->element];
|
2012-12-13 03:48:07 +00:00
|
|
|
operands[2] = var->op;
|
|
|
|
dst = operands[2];
|
|
|
|
st = build_statement ("<MOVE>", operands, var->expr);
|
|
|
|
sblock_add_statement (block, st);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-12-20 11:58:19 +00:00
|
|
|
static operand_t *
|
|
|
|
generate_moveps (dag_t *dag, sblock_t *block, dagnode_t *dagnode)
|
|
|
|
{
|
|
|
|
set_iter_t *var_iter;
|
|
|
|
daglabel_t *var;
|
|
|
|
operand_t *operands[3] = {0, 0, 0};
|
|
|
|
statement_t *st;
|
|
|
|
operand_t *dst = 0;
|
2019-06-10 14:52:39 +00:00
|
|
|
type_t *type;
|
2012-12-20 11:58:19 +00:00
|
|
|
|
|
|
|
operands[0] = make_operand (dag, block, dagnode, 0);
|
|
|
|
operands[1] = make_operand (dag, block, dagnode, 1);
|
|
|
|
for (var_iter = set_first (dagnode->identifiers); var_iter;
|
|
|
|
var_iter = set_next (var_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
var = dag->labels[var_iter->element];
|
2012-12-20 11:58:19 +00:00
|
|
|
dst = var->op;
|
2019-06-10 14:52:39 +00:00
|
|
|
type = dst->o.def->type;
|
|
|
|
operands[2] = value_operand (new_pointer_val (0, type, dst->o.def));
|
2012-12-20 11:58:19 +00:00
|
|
|
st = build_statement ("<MOVEP>", operands, var->expr);
|
|
|
|
sblock_add_statement (block, st);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:11:26 +00:00
|
|
|
static operand_t *
|
2012-11-17 12:14:42 +00:00
|
|
|
generate_assignments (dag_t *dag, sblock_t *block, operand_t *src,
|
2019-06-17 14:38:34 +00:00
|
|
|
set_iter_t *var_iter, type_t *type)
|
2012-11-07 05:11:26 +00:00
|
|
|
{
|
2012-11-16 13:09:49 +00:00
|
|
|
statement_t *st;
|
|
|
|
operand_t *dst = 0;
|
2012-11-19 02:00:04 +00:00
|
|
|
operand_t *operands[3] = {0, 0, 0};
|
2012-11-17 12:14:42 +00:00
|
|
|
daglabel_t *var;
|
2012-11-16 13:09:49 +00:00
|
|
|
|
2012-12-11 06:52:37 +00:00
|
|
|
operands[0] = fix_op_type (src, type);
|
2012-11-17 12:14:42 +00:00
|
|
|
for ( ; var_iter; var_iter = set_next (var_iter)) {
|
2012-12-21 05:09:00 +00:00
|
|
|
var = dag->labels[var_iter->element];
|
2012-12-11 06:52:37 +00:00
|
|
|
operands[1] = fix_op_type (var->op, type);
|
2012-12-05 10:47:22 +00:00
|
|
|
if (!dst)
|
2012-11-17 12:14:42 +00:00
|
|
|
dst = operands[1];
|
|
|
|
|
|
|
|
st = build_statement ("=", operands, var->expr);
|
2012-11-16 13:09:49 +00:00
|
|
|
sblock_add_statement (block, st);
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
2012-11-18 04:10:19 +00:00
|
|
|
static void
|
|
|
|
dag_gencode (dag_t *dag, sblock_t *block, dagnode_t *dagnode)
|
2012-11-17 12:14:42 +00:00
|
|
|
{
|
|
|
|
operand_t *operands[3] = {0, 0, 0};
|
2012-11-16 13:09:49 +00:00
|
|
|
operand_t *dst = 0;
|
|
|
|
statement_t *st;
|
2012-11-17 12:14:42 +00:00
|
|
|
set_iter_t *var_iter;
|
|
|
|
int i;
|
2019-06-17 14:38:34 +00:00
|
|
|
type_t *type;
|
2012-11-16 13:09:49 +00:00
|
|
|
|
|
|
|
switch (dagnode->type) {
|
|
|
|
case st_none:
|
|
|
|
if (!dagnode->label->op)
|
|
|
|
internal_error (0, "non-leaf label in leaf node");
|
|
|
|
dst = dagnode->label->op;
|
2012-11-19 06:21:01 +00:00
|
|
|
if ((var_iter = set_first (dagnode->identifiers))) {
|
2012-11-30 11:47:36 +00:00
|
|
|
type = dst->type;
|
2012-11-19 06:21:01 +00:00
|
|
|
dst = generate_assignments (dag, block, dst, var_iter, type);
|
|
|
|
}
|
2012-11-16 13:09:49 +00:00
|
|
|
break;
|
|
|
|
case st_expr:
|
2012-11-17 12:14:42 +00:00
|
|
|
operands[0] = make_operand (dag, block, dagnode, 0);
|
|
|
|
if (dagnode->children[1])
|
|
|
|
operands[1] = make_operand (dag, block, dagnode, 1);
|
2019-06-17 14:38:34 +00:00
|
|
|
type = get_type (dagnode->label->expr);
|
2012-11-17 12:14:42 +00:00
|
|
|
if (!(var_iter = set_first (dagnode->identifiers))) {
|
|
|
|
operands[2] = temp_operand (get_type (dagnode->label->expr));
|
2012-11-15 06:53:51 +00:00
|
|
|
} else {
|
2012-12-21 05:09:00 +00:00
|
|
|
daglabel_t *var = dag->labels[var_iter->element];
|
2012-11-19 04:49:34 +00:00
|
|
|
|
2012-12-11 06:52:37 +00:00
|
|
|
operands[2] = fix_op_type (var->op, type);
|
2012-11-17 12:14:42 +00:00
|
|
|
var_iter = set_next (var_iter);
|
2012-11-15 06:53:51 +00:00
|
|
|
}
|
2012-11-17 12:14:42 +00:00
|
|
|
dst = operands[2];
|
|
|
|
st = build_statement (dagnode->label->opcode, operands,
|
2012-11-17 03:07:27 +00:00
|
|
|
dagnode->label->expr);
|
2012-11-15 06:53:51 +00:00
|
|
|
sblock_add_statement (block, st);
|
2012-11-19 06:21:01 +00:00
|
|
|
generate_assignments (dag, block, operands[2], var_iter, type);
|
2012-11-16 13:09:49 +00:00
|
|
|
break;
|
|
|
|
case st_assign:
|
|
|
|
internal_error (0, "unexpected assignment node");
|
|
|
|
case st_ptrassign:
|
2012-11-17 12:14:42 +00:00
|
|
|
operands[0] = make_operand (dag, block, dagnode, 0);
|
|
|
|
operands[1] = make_operand (dag, block, dagnode, 1);
|
|
|
|
if (dagnode->children[2])
|
|
|
|
operands[2] = make_operand (dag, block, dagnode, 2);
|
|
|
|
st = build_statement (dagnode->label->opcode, operands,
|
2012-11-17 03:07:27 +00:00
|
|
|
dagnode->label->expr);
|
2012-11-16 13:09:49 +00:00
|
|
|
sblock_add_statement (block, st);
|
2012-11-20 06:22:05 +00:00
|
|
|
// the source location is suitable for use in other nodes
|
|
|
|
dst = operands[0];
|
2012-11-16 13:09:49 +00:00
|
|
|
break;
|
|
|
|
case st_move:
|
2012-12-20 12:28:34 +00:00
|
|
|
if (!strcmp (dagnode->label->opcode, "<MOVE>")) {
|
2012-12-13 03:48:07 +00:00
|
|
|
dst = generate_moves (dag, block, dagnode);
|
2012-12-20 12:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!strcmp (dagnode->label->opcode, "<MOVEP>")
|
|
|
|
&& !dagnode->children[2]) {
|
2012-12-20 11:58:19 +00:00
|
|
|
dst = generate_moveps (dag, block, dagnode);
|
2012-12-20 12:28:34 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//fall through
|
2012-11-16 13:09:49 +00:00
|
|
|
case st_state:
|
|
|
|
case st_func:
|
2012-11-17 12:14:42 +00:00
|
|
|
for (i = 0; i < 3; i++)
|
|
|
|
if (dagnode->children[i])
|
|
|
|
operands[i] = make_operand (dag, block, dagnode, i);
|
|
|
|
st = build_statement (dagnode->label->opcode, operands,
|
2012-11-17 03:07:27 +00:00
|
|
|
dagnode->label->expr);
|
2012-11-16 13:09:49 +00:00
|
|
|
sblock_add_statement (block, st);
|
|
|
|
break;
|
|
|
|
case st_flow:
|
2012-11-17 12:14:42 +00:00
|
|
|
operands[0] = make_operand (dag, block, dagnode, 0);
|
|
|
|
if (dagnode->children[1])
|
|
|
|
operands[1] = make_operand (dag, block, dagnode, 1);
|
|
|
|
st = build_statement (dagnode->label->opcode, operands,
|
2012-11-17 03:07:27 +00:00
|
|
|
dagnode->label->expr);
|
2012-11-16 13:09:49 +00:00
|
|
|
sblock_add_statement (block, st);
|
|
|
|
break;
|
2012-11-15 06:53:51 +00:00
|
|
|
}
|
2012-11-18 04:10:19 +00:00
|
|
|
dagnode->value = dst;
|
2012-11-07 05:11:26 +00:00
|
|
|
}
|
|
|
|
|
2012-12-25 08:07:58 +00:00
|
|
|
void
|
|
|
|
dag_remove_dead_nodes (dag_t *dag)
|
|
|
|
{
|
|
|
|
int added_root;
|
|
|
|
set_iter_t *root_i, *child_i;
|
|
|
|
dagnode_t *node, *child;
|
|
|
|
|
|
|
|
do {
|
|
|
|
added_root = 0;
|
|
|
|
for (root_i = set_first (dag->roots); root_i;
|
|
|
|
root_i = set_next (root_i)) {
|
|
|
|
node = dag->nodes[root_i->element];
|
|
|
|
// only st_none (leaf nodes), st_expr and st_move can become
|
|
|
|
// dead nodes (no live vars attached).
|
|
|
|
if (node->type != st_none && node->type != st_expr
|
|
|
|
&& node->type != st_move)
|
|
|
|
continue;
|
|
|
|
if (!set_is_empty (node->identifiers))
|
|
|
|
continue;
|
|
|
|
// MOVEP with a variable destination pointer is never dead
|
2012-12-26 02:51:40 +00:00
|
|
|
if (node->type == st_move && node->children[2])
|
2012-12-25 08:07:58 +00:00
|
|
|
continue;
|
|
|
|
set_remove (dag->roots, node->number);
|
|
|
|
for (child_i = set_first (node->edges); child_i;
|
|
|
|
child_i = set_next (child_i)) {
|
|
|
|
child = dag->nodes[child_i->element];
|
|
|
|
if (!set_is_member (child->parents, node->number))
|
|
|
|
continue; // not really a child (dependency edge)
|
|
|
|
set_remove (child->parents, node->number);
|
|
|
|
if (set_is_empty (child->parents)) {
|
|
|
|
set_add (dag->roots, child->number);
|
|
|
|
added_root = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (added_root);
|
|
|
|
dag_sort_nodes (dag);
|
|
|
|
}
|
|
|
|
|
2012-11-07 05:11:26 +00:00
|
|
|
void
|
2012-11-17 12:14:42 +00:00
|
|
|
dag_generate (dag_t *dag, sblock_t *block)
|
2012-11-07 05:11:26 +00:00
|
|
|
{
|
2012-11-18 04:10:19 +00:00
|
|
|
int i;
|
2012-11-07 05:11:26 +00:00
|
|
|
|
2012-12-25 08:07:58 +00:00
|
|
|
for (i = 0; i < dag->num_topo; i++)
|
2012-11-18 04:10:19 +00:00
|
|
|
dag_gencode (dag, block, dag->nodes[dag->topo[i]]);
|
2012-11-07 05:11:26 +00:00
|
|
|
}
|