#include "irgraph.h"
#include "irdump.h"
#include "irdom.h"
+#include "debug.h"
+#include "xmalloc.h"
#include "beutil.h"
#include "besched.h"
#include "besched_t.h"
#include "belive_t.h"
+
+
+#undef DUMP_INTERVALS
+#undef DUMP_PRESSURE
+#define DUMP_IFG
+
+#define BUILD_GRAPH
+
+#ifdef USE_OLD_PHI_INTERFERENCE
+#undef BUILD_GRAPH
+#define BUILD_GRAPH
+#endif
+
+
#define TEST_COLORS 2048
+static firm_dbg_module_t *dbg;
+
/** An interval border. */
typedef struct _border_t {
struct list_head list; /**< list head for queuing. */
unsigned is_def : 1; /**< Does this border denote a use or a def. */
} border_t;
-typedef struct _if_edge_t {
- int src, tgt;
-} if_edge_t;
-
-#define IF_EDGE_HASH(e) ((e)->src)
-
-static int if_edge_cmp(const void *p1, const void *p2, size_t size)
-{
- const if_edge_t *e1 = p1;
- const if_edge_t *e2 = p2;
-
- return !(e1->src == e2->src && e1->tgt == e2->tgt);
-}
-
typedef struct _env_t {
struct obstack obst; /**< An obstack for temporary storage. */
- set *phi_if; /**< The phi interference map. */
+ set *graph; /**< The interference graph. */
bitset_t *live; /**< A live bitset to use in every block. */
bitset_t *processed; /**< A set marking processed blocks. */
bitset_t *colors; /**< The color mask. */
+ bitset_t *in_colors; /**< Colors used by live in values. */
+ int colors_n; /**< The number of colors. */
} env_t;
typedef struct _be_chordal_dump_params_t {
FILE *f;
char buf[1024];
- ir_snprintf(buf, sizeof(buf), "%s_bl%N.eps",
+ ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
get_entity_name(get_irg_entity(irg)), block);
if((f = fopen(buf, "wt")) != NULL) {
border_t *b;
- int *seen = calloc(get_graph_node_count(irg), sizeof(*seen));
+ int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
int max_col = 0;
int pos = last_pos - seen[nr];
int end_pos = last_pos - b->step;
int live_in = is_live_in(block, irn);
- int live_out = is_live_out(block, irn);
+ int live_end = is_live_end(block, irn);
int y_val = y_dist * col;
int red = 0;
- int green = live_out;
+ int green = live_end;
int blue = live_in;
fprintf(f, "0 0 0 setrgbcolor\n");
}
}
+#ifdef BUILD_GRAPH
+
+typedef struct _if_edge_t {
+ int src, tgt;
+} if_edge_t;
+
+#define IF_EDGE_HASH(e) ((e)->src)
+
+static int if_edge_cmp(const void *p1, const void *p2, size_t size)
+{
+ const if_edge_t *e1 = p1;
+ const if_edge_t *e2 = p2;
+
+ return !(e1->src == e2->src && e1->tgt == e2->tgt);
+}
+
static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
{
/* Bring the smaller entry to src. */
{
if_edge_t edge;
edge_init(&edge, src, tgt);
- set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
+ set_insert(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
}
static INLINE int are_connected(const env_t *env, int src, int tgt)
{
if_edge_t edge;
edge_init(&edge, src, tgt);
- return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
+ return set_find(env->graph, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
}
+static void dump_ifg(set *edges, const char *filename)
+{
+ FILE *f;
+
+ if((f = fopen(filename, "wt")) != NULL) {
+ if_edge_t *edge;
+
+ fprintf(f, "graph G {\n");
+ for(edge = set_first(edges); edge; edge = set_next(edges)) {
+ fprintf(f, "i\tn%d -- n%d\n", edge->src, edge->tgt);
+ }
+
+ fprintf(f, "}\n");
+ fclose(f);
+ }
+
+}
+
+#endif /* USE_OLD_PHI_INTERFERENCE || BUILD_GRAPH */
static INLINE border_t *border_add(env_t *env, struct list_head *head,
const ir_node *irn, int step, int is_def)
void *obstack_level = obstack_base(obst);
bitset_t *live = env->live;
bitset_t *colors = env->colors;
+ bitset_t *in_colors = env->in_colors;
+ bitset_t *used_colors = bitset_malloc(env->colors_n);
+ bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
ir_graph *irg = get_irn_irg(block);
int i, n;
border_t *b;
struct list_head head;
pset *live_in = get_live_in(block);
- pset *live_out = get_live_out(block);
+ pset *live_end = get_live_end(block);
ir_node *idom = get_Block_idom(block);
/*
/* Clear the live and allocate the color bitset. */
bitset_clear_all(live);
bitset_clear_all(colors);
+ bitset_clear_all(in_colors);
INIT_LIST_HEAD(&head);
* Make final uses of all values live out of the block.
* They are neccessary to build up real intervals.
*/
- for(irn = pset_first(live_out); irn; irn = pset_next(live_out)) {
+ for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
+ DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
bitset_set(live, get_irn_graph_nr(irn));
if(!is_Phi(irn) && is_allocatable_irn(irn))
border_add(env, &head, irn, step, 0);
* relevant for the interval borders.
*/
sched_foreach_reverse(block, irn) {
- ir_debugf("insn: %n\n", irn);
- ir_debugf("live: %b\n", live);
+ DBG((dbg, LEVEL_1, "insn: %n\n", irn));
+ DBG((dbg, LEVEL_2, "live: %b\n", live));
- get_irn_ra_info(irn)->color = NO_COLOR;
+ set_irn_color(irn, NO_COLOR);
/*
* If the node defines a datab value, i.e. something, registers must
* be allocated for, add a new def border to the border list.
*/
if(is_allocatable_irn(irn)) {
- unsigned long elm;
int nr = get_irn_graph_nr(irn);
bitset_clear(live, nr);
border_add(env, &head, irn, step, 1);
- if(is_phi_operand(irn)) {
+#ifdef BUILD_GRAPH
+ {
+ unsigned long elm;
bitset_foreach(live, elm) {
int live_nr = (int) elm;
ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
if(is_phi_operand(live_irn)) {
- ir_debugf("\t\tinterfering phi operands: %n, %n\n", irn, live_irn);
add_if(env, nr, live_nr);
}
}
}
+#endif
}
/*
if(is_allocatable_irn(op)) {
int nr = get_irn_graph_nr(op);
- ir_debugf("\t\tpos: %d, use: %n\n", i, op);
+ DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
if(!bitset_is_set(live, nr)) {
border_add(env, &head, op, step, 0);
/* Mark the color of the live in value as used. */
assert(is_color(col) && "Node must have been assigned a color.");
bitset_set(colors, col);
+ bitset_set(in_colors, col);
+ bitset_set(used_colors, col);
/* Mark the value live in. */
bitset_set(live, get_irn_graph_nr(irn));
}
}
- ir_debugf("usedef chain for block %n\n", block);
+ DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
list_for_each_entry(border_t, b, &head, list) {
- ir_debugf("\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn));
+ DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
}
/*
* color.
*/
if(b->is_def && !is_live_in(block, irn)) {
- ra_info_t *ri = get_irn_ra_info(irn);
- int col = bitset_next_clear(colors, 0);
+ ra_node_info_t *ri = get_ra_node_info(irn);
+ int col = NO_COLOR;
+
+ /*
+ * Try to assign live out values colors which are not used by live
+ * in values.
+ */
+ if(is_live_out(block, irn)) {
+ bitset_copy(tmp_colors, colors);
+ bitset_andnot(tmp_colors, in_colors);
+ col = bitset_next_clear(tmp_colors, 0);
+ }
+
+ /* If a color is not yet assigned, do it now. */
+ if(!is_color(col))
+ col = bitset_next_clear(colors, 0);
assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
bitset_set(colors, col);
+ bitset_set(used_colors, col);
bitset_set(live, nr);
ri->color = col;
ri->pressure = bitset_popcnt(colors);
- ir_debugf("\tassigning color %d to %n\n", col, irn);
+ DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
}
/* Clear the color upon a use. */
else if(!b->is_def) {
- int col = get_irn_ra_info(irn)->color;
+ int col = get_irn_color(irn);
assert(bitset_is_set(live, nr) && "Cannot have a non live use");
assert(is_color(col) && "A color must have been assigned");
}
}
+#ifdef DUMP_INTERVALS
draw_interval_graphs(block, &head, &dump_params);
+#endif
+
+#ifdef DUMP_PRESSURE
+ {
+ char buf[128];
+ FILE *f;
+
+ ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
+ get_entity_name(get_irg_entity(irg)), block);
+
+ if((f = fopen(buf, "wt")) != NULL) {
+ sched_foreach_reverse(block, irn) {
+ if(is_allocatable_irn(irn))
+ ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
+ get_ra_node_info(irn)->pressure);
+
+ }
+ fclose(f);
+ }
+ }
+#endif
+
+
+ /*
+ * Allocate the used colors array in the blocks ra info structure and
+ * fill it.
+ */
+ get_ra_block_info(block)->used_colors = used_colors;
/* Mark this block has processed. */
bitset_set(env->processed, block_nr);
obstack_free(obst, obstack_level);
}
+void be_ra_chordal_init(void)
+{
+ dbg = firm_dbg_register(DBG_BERA);
+ firm_dbg_set_mask(dbg, -1);
+}
void be_ra_chordal(ir_graph *irg)
{
compute_doms(irg);
obstack_init(&env->obst);
- env->phi_if = new_set(if_edge_cmp, node_count);
+
+#ifdef BUILD_GRAPH
+ env->graph = new_set(if_edge_cmp, node_count);
+#endif
+
env->live = bitset_obstack_alloc(&env->obst, node_count);
env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
+ env->in_colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
+ env->colors_n = TEST_COLORS;
irg_block_walk_graph(irg, block_alloc, NULL, env);
obstack_free(&env->obst, NULL);
+#ifdef DUMP_IFG
+ {
+ char buf[128];
+
+ ir_snprintf(buf, sizeof(buf), "ifg_%s.dot", get_entity_name(get_irg_entity(irg)));
+ dump_ifg(env->graph, buf);
+ }
+#endif
+
set_irg_ra_link(irg, env);
}
void be_ra_chordal_done(ir_graph *irg)
{
env_t *env = get_irg_ra_link(irg);
- free(env->phi_if);
+
+#ifdef BUILD_GRAPH
+ free(env->graph);
+#endif
free(env);
}
int phi_ops_interfere(const ir_node *a, const ir_node *b)
{
+#ifdef USE_OLD_PHI_INTERFERENCE
ir_graph *irg = get_irn_irg(a);
env_t *env = get_irg_ra_link(irg);
assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
+#else
+ return values_interfere(a, b);
+#endif /* USE_OLD_PHI_INTERFERENCE */
}