2 * Chordal register allocation.
3 * @author Sebastian Hack
15 #include "irprintf_t.h"
26 #include "besched_t.h"
29 #define TEST_COLORS 2048
31 /** An interval border. */
32 typedef struct _border_t {
33 struct list_head list; /**< list head for queuing. */
34 const ir_node *irn; /**< The node. */
35 unsigned step; /**< The number equal to the interval border. */
36 unsigned is_def : 1; /**< Does this border denote a use or a def. */
39 typedef struct _if_edge_t {
43 #define IF_EDGE_HASH(e) ((e)->src)
45 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
47 const if_edge_t *e1 = p1;
48 const if_edge_t *e2 = p2;
50 return !(e1->src == e2->src && e1->tgt == e2->tgt);
53 typedef struct _env_t {
54 struct obstack obst; /**< An obstack for temporary storage. */
55 set *phi_if; /**< The phi interference map. */
56 bitset_t *live; /**< A live bitset to use in every block. */
57 bitset_t *processed; /**< A set marking processed blocks. */
58 bitset_t *colors; /**< The color mask. */
61 typedef struct _be_chordal_dump_params_t {
65 } be_chordal_dump_params_t;
67 static const be_chordal_dump_params_t dump_params = {
73 static void draw_interval_graphs(ir_node *block,
74 struct list_head *border_head,
75 const be_chordal_dump_params_t *params)
78 int x_dist = params->x_dist;
79 int y_dist = params->y_dist;
80 ir_graph *irg = get_irn_irg(block);
85 ir_snprintf(buf, sizeof(buf), "%s_bl%N.eps",
86 get_entity_name(get_irg_entity(irg)), block);
88 if((f = fopen(buf, "wt")) != NULL) {
90 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
91 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
94 list_for_each_entry_reverse(border_t, b, border_head, list) {
95 const ir_node *irn = b->irn;
96 int col = get_irn_color(irn);
97 max_col = max_col > col ? max_col : col;
100 fprintf(f, "%%!PS-Adobe-2.0\n");
101 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
102 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
103 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
104 fprintf(f, "mainfont setfont\n");
105 fprintf(f, "0.2 setlinewidth\n");
107 for(i = 0; i <= last_pos; ++i) {
108 fprintf(f, "0 0 0 setrgbcolor\n");
109 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
110 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
111 fprintf(f, "stroke\n");
113 fprintf(f, "0.5 setlinewidth\n");
115 list_for_each_entry_reverse(border_t, b, border_head, list) {
116 const ir_node *irn = b->irn;
117 int nr = get_irn_graph_nr(irn);
122 int col = get_irn_color(irn);
124 int pos = last_pos - seen[nr];
125 int end_pos = last_pos - b->step;
126 int live_in = is_live_in(block, irn);
127 int live_out = is_live_out(block, irn);
128 int y_val = y_dist * col;
131 int green = live_out;
134 fprintf(f, "0 0 0 setrgbcolor\n");
135 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
136 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
137 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
138 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
139 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
140 fprintf(f, "stroke\n");
149 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
151 /* Bring the smaller entry to src. */
163 static INLINE void add_if(const env_t *env, int src, int tgt)
166 edge_init(&edge, src, tgt);
167 set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
170 static INLINE int are_connected(const env_t *env, int src, int tgt)
173 edge_init(&edge, src, tgt);
174 return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
179 static INLINE border_t *border_add(env_t *env, struct list_head *head,
180 const ir_node *irn, int step, int is_def)
182 border_t *b = obstack_alloc(&env->obst, sizeof(*b));
186 list_add_tail(&b->list, head);
190 static void block_alloc(ir_node *block, void *env_ptr)
192 env_t *env = env_ptr;
193 struct obstack *obst = &env->obst;
194 void *obstack_level = obstack_base(obst);
195 bitset_t *live = env->live;
196 bitset_t *colors = env->colors;
197 ir_graph *irg = get_irn_irg(block);
201 int block_nr = get_block_graph_nr(block);
204 struct list_head head;
205 pset *live_in = get_live_in(block);
206 pset *live_out = get_live_out(block);
207 ir_node *idom = get_Block_idom(block);
210 * Check, if this block has already been processed, if true, return
213 if(bitset_is_set(env->processed, block_nr))
217 * Ensure, that the immediate dominator of this block is allocated
218 * before this block, since the values live in at this block are
219 * defined in the dominators of this block. Coloring the dominators
220 * thus is vital before coloring this block.
223 block_alloc(idom, env);
225 /* Clear the live and allocate the color bitset. */
226 bitset_clear_all(live);
227 bitset_clear_all(colors);
229 INIT_LIST_HEAD(&head);
232 * Make final uses of all values live out of the block.
233 * They are neccessary to build up real intervals.
235 for(irn = pset_first(live_out); irn; irn = pset_next(live_out)) {
236 bitset_set(live, get_irn_graph_nr(irn));
237 if(!is_Phi(irn) && is_allocatable_irn(irn))
238 border_add(env, &head, irn, step, 0);
244 * Determine the last uses of a value inside the block, since they are
245 * relevant for the interval borders.
247 sched_foreach_reverse(block, irn) {
248 ir_debugf("insn: %n\n", irn);
249 ir_debugf("live: %b\n", live);
251 get_irn_ra_info(irn)->color = NO_COLOR;
254 * If the node defines a datab value, i.e. something, registers must
255 * be allocated for, add a new def border to the border list.
257 if(is_allocatable_irn(irn)) {
259 int nr = get_irn_graph_nr(irn);
261 bitset_clear(live, nr);
262 border_add(env, &head, irn, step, 1);
264 if(is_phi_operand(irn)) {
265 bitset_foreach(live, elm) {
266 int live_nr = (int) elm;
267 ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
268 if(is_phi_operand(live_irn)) {
269 ir_debugf("\t\tinterfering phi operands: %n, %n\n", irn, live_irn);
270 add_if(env, nr, live_nr);
277 * If the node is no phi node we can examine the uses.
280 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
281 ir_node *op = get_irn_n(irn, i);
283 if(is_allocatable_irn(op)) {
284 int nr = get_irn_graph_nr(op);
286 ir_debugf("\t\tpos: %d, use: %n\n", i, op);
288 if(!bitset_is_set(live, nr)) {
289 border_add(env, &head, op, step, 0);
290 bitset_set(live, nr);
299 bitset_clear_all(live);
302 * Add initial defs for all values live in.
303 * Since their colors have already been assigned (The dominators were
304 * allocated before), we have to mark their colors as used also.
306 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
307 if(is_allocatable_irn(irn)) {
308 int col = get_irn_color(irn);
310 /* Mark the color of the live in value as used. */
311 assert(is_color(col) && "Node must have been assigned a color.");
312 bitset_set(colors, col);
314 /* Mark the value live in. */
315 bitset_set(live, get_irn_graph_nr(irn));
318 border_add(env, &head, irn, step, 1);
322 ir_debugf("usedef chain for block %n\n", block);
323 list_for_each_entry(border_t, b, &head, list) {
324 ir_debugf("\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn));
328 * Mind that the sequence of defs from back to front defines a perfect
329 * elimination order. So, coloring the definitions from first to last
332 list_for_each_entry_reverse(border_t, b, &head, list) {
333 const ir_node *irn = b->irn;
334 int nr = get_irn_graph_nr(irn);
337 * Assign a color, if it is a local def. Global defs already have a
340 if(b->is_def && !is_live_in(block, irn)) {
341 ra_info_t *ri = get_irn_ra_info(irn);
342 int col = bitset_next_clear(colors, 0);
344 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
345 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
347 bitset_set(colors, col);
348 bitset_set(live, nr);
351 ri->pressure = bitset_popcnt(colors);
353 ir_debugf("\tassigning color %d to %n\n", col, irn);
356 /* Clear the color upon a use. */
357 else if(!b->is_def) {
358 int col = get_irn_ra_info(irn)->color;
360 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
361 assert(is_color(col) && "A color must have been assigned");
363 bitset_clear(colors, col);
364 bitset_clear(live, nr);
368 draw_interval_graphs(block, &head, &dump_params);
370 /* Mark this block has processed. */
371 bitset_set(env->processed, block_nr);
373 /* Reset the obstack to its initial level */
374 obstack_free(obst, obstack_level);
378 void be_ra_chordal(ir_graph *irg)
380 int node_count = get_graph_node_count(irg);
381 env_t *env = malloc(sizeof(*env));
383 if(get_irg_dom_state(irg) != dom_consistent)
386 obstack_init(&env->obst);
387 env->phi_if = new_set(if_edge_cmp, node_count);
388 env->live = bitset_obstack_alloc(&env->obst, node_count);
389 env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
390 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
392 irg_block_walk_graph(irg, block_alloc, NULL, env);
393 obstack_free(&env->obst, NULL);
395 set_irg_ra_link(irg, env);
398 void be_ra_chordal_done(ir_graph *irg)
400 env_t *env = get_irg_ra_link(irg);
405 int phi_ops_interfere(const ir_node *a, const ir_node *b)
407 ir_graph *irg = get_irn_irg(a);
408 env_t *env = get_irg_ra_link(irg);
410 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
412 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));