+/**
+ * Annotate the register pressure to the nodes and compute
+ * the liveness intervals.
+ * @param block The block to do it for.
+ * @param env_ptr The environment.
+ */
+static void pressure(ir_node *block, void *env_ptr)
+{
+/* Convenience macro for a def */
+#define border_def(irn, step, real) \
+ border_add(env, head, irn, step, pressure--, 1, real)
+
+/* Convenience macro for a use */
+#define border_use(irn, step, real) \
+ border_add(env, head, irn, step, ++pressure, 0, real)
+
+ env_t *env = env_ptr;
+ bitset_t *live = env->live;
+ ir_node *irn;
+
+ int i, n;
+ unsigned step = 0;
+ unsigned pressure = 0;
+ struct list_head *head;
+ pset *live_in = get_live_in(block);
+ pset *live_end = get_live_end(block);
+
+ DBG((dbg, LEVEL_1, "Computing pressure in block %n\n", block));
+ bitset_clear_all(live);
+
+ /* Set up the border list in the block info */
+ head = &get_ra_block_info(block)->border_head;
+ 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_end); irn; irn = pset_next(live_end)) {
+ DBG((dbg, LEVEL_3, "\tMaking 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_use(irn, step, 0);
+ }
+
+ ++step;
+
+ /*
+ * Determine the last uses of a value inside the block, since they are
+ * relevant for the interval borders.
+ */
+ sched_foreach_reverse(block, irn) {
+ DBG((dbg, LEVEL_1, "\tinsn: %n, pressure: %d\n", irn, pressure));
+ DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
+
+ /* Erase the color of each node encountered. */
+ 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)) {
+ int nr = get_irn_graph_nr(irn);
+
+ bitset_clear(live, nr);
+ border_def(irn, step, 1);
+
+#ifdef BUILD_GRAPH
+ {
+ unsigned long elm;
+ bitset_foreach(live, elm) {
+ int live_nr = (int) elm;
+ add_if(env, nr, live_nr);
+ }
+ }
+#endif
+ }
+
+ /*
+ * If the node is no phi node we can examine the uses.
+ */
+ if(!is_Phi(irn)) {
+ for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
+ ir_node *op = get_irn_n(irn, i);
+
+ if(is_allocatable_irn(op)) {
+ int nr = get_irn_graph_nr(op);
+
+ DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
+
+ if(!bitset_is_set(live, nr)) {
+ border_use(op, step, 1);
+ bitset_set(live, nr);
+ }
+ }
+ }
+ }
+
+ ++step;
+ }
+
+ /*
+ * Add initial defs for all values live in.
+ */
+ for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
+ if(is_allocatable_irn(irn)) {
+
+ /* Mark the value live in. */
+ bitset_set(live, get_irn_graph_nr(irn));
+
+ /* Add the def */
+ border_def(irn, step, 0);
+ }
+ }
+}
+
+static void assign(ir_node *block, void *env_ptr)
+{
+ env_t *env = env_ptr;
+ struct obstack *obst = &env->obst;
+ bitset_t *live = env->live;
+ bitset_t *colors = env->colors;
+ bitset_t *in_colors = env->in_colors;
+
+ /* The used colors will remain on the obstack. */
+ bitset_t *used_colors = bitset_obstack_alloc(obst, env->colors_n);
+
+ /* Mark the obstack level and allocate the temporary tmp_colors */
+ void *obstack_level = obstack_base(obst);
+ bitset_t *tmp_colors = bitset_obstack_alloc(obst, env->colors_n);
+
+ const ir_node *irn;
+ border_t *b;
+ struct list_head *head = &get_ra_block_info(block)->border_head;
+ pset *live_in = get_live_in(block);
+
+ bitset_clear_all(live);
+ bitset_clear_all(colors);
+ bitset_clear_all(in_colors);
+
+ DBG((dbg, LEVEL_4, "Assigning colors for block %n\n", block));
+ DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
+ list_for_each_entry(border_t, b, head, list) {
+ DBG((dbg, LEVEL_4, "\t%s %n/%d\n", b->is_def ? "def" : "use",
+ b->irn, get_irn_graph_nr(b->irn)));
+ }
+
+ /*
+ * Add initial defs for all values live in.
+ * Since their colors have already been assigned (The dominators were
+ * allocated before), we have to mark their colors as used also.
+ */
+ for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
+ if(is_allocatable_irn(irn)) {
+ int col = get_irn_color(irn);
+
+ /* 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));
+ }
+ }
+
+ /*
+ * Mind that the sequence of defs from back to front defines a perfect
+ * elimination order. So, coloring the definitions from first to last
+ * will work.
+ */
+ list_for_each_entry_reverse(border_t, b, head, list) {
+ const ir_node *irn = b->irn;
+ int nr = get_irn_graph_nr(irn);
+
+ /*
+ * Assign a color, if it is a local def. Global defs already have a
+ * color.
+ */
+ if(b->is_def && !is_live_in(block, irn)) {
+ ra_node_info_t *ri = get_ra_node_info(irn);
+ int col = NO_COLOR;
+
+ DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
+
+ /*
+ * Try to assign live out values colors which are not used by live
+ * in values.
+ */
+#if 0
+ if(is_live_out(block, irn)) {
+ int next_clear;
+
+ bitset_copy(tmp_colors, colors);
+ bitset_or(tmp_colors, in_colors);
+ next_clear = bitset_next_clear(tmp_colors, 0);
+ col = next_clear != -1 ? next_clear : NO_COLOR;
+
+ DBG((dbg, LEVEL_5, "next clear in only outs %b: %d\n", tmp_colors, col));
+ }
+#endif
+
+ /* 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;
+
+ 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_color(irn);
+
+ assert(bitset_is_set(live, nr) && "Cannot have a non live use");
+ assert(is_color(col) && "A color must have been assigned");
+
+ bitset_clear(colors, col);
+ bitset_clear(live, nr);
+ }
+ }
+
+#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;
+
+ /* Free the auxillary data on the obstack. */
+ obstack_free(obst, obstack_level);
+}
+
+
+#if 0