2 * Chordal register allocation.
3 * @author Sebastian Hack
15 #include "irprintf_t.h"
25 #include "besched_t.h"
28 #define TEST_COLORS 2048
30 /** An interval border. */
31 typedef struct _border_t {
32 struct list_head list; /**< list head for queuing. */
33 const ir_node *irn; /**< The node. */
34 unsigned step; /**< The number equal to the interval border. */
35 unsigned is_def : 1; /**< Does this border denote a use or a def. */
38 typedef struct _if_edge_t {
42 #define IF_EDGE_HASH(e) ((e)->src)
44 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
46 const if_edge_t *e1 = p1;
47 const if_edge_t *e2 = p2;
49 return !(e1->src == e2->src && e1->tgt == e2->tgt);
52 typedef struct _env_t {
53 struct obstack obst; /**< An obstack for temporary storage. */
54 set *phi_if; /**< The phi interference map. */
55 bitset_t *live; /**< A live bitset to use in every block. */
56 bitset_t *processed; /**< A set marking processed blocks. */
57 bitset_t *colors; /**< The color mask. */
60 typedef struct _be_chordal_dump_params_t {
64 } be_chordal_dump_params_t;
66 static const be_chordal_dump_params_t dump_params = {
72 static void draw_interval_graphs(ir_node *block,
73 struct list_head *border_head,
74 const be_chordal_dump_params_t *params)
77 int x_dist = params->x_dist;
78 int y_dist = params->y_dist;
79 ir_graph *irg = get_irn_irg(block);
84 ir_snprintf(buf, sizeof(buf), "%s_bl%N.eps",
85 get_entity_name(get_irg_entity(irg)), block);
87 if((f = fopen(buf, "wt")) != NULL) {
89 int *seen = calloc(get_graph_node_count(irg), sizeof(*seen));
90 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
93 list_for_each_entry_reverse(border_t, b, border_head, list) {
94 const ir_node *irn = b->irn;
95 int col = get_irn_color(irn);
96 max_col = max_col > col ? max_col : col;
99 fprintf(f, "%%!PS-Adobe-2.0\n");
100 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
101 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
102 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
103 fprintf(f, "mainfont setfont\n");
104 fprintf(f, "0.2 setlinewidth\n");
106 for(i = 0; i <= last_pos; ++i) {
107 fprintf(f, "0 0 0 setrgbcolor\n");
108 fprintf(f, "%d %d moveto\n", i * x_dist, -2);
109 fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
110 fprintf(f, "stroke\n");
112 fprintf(f, "0.5 setlinewidth\n");
114 list_for_each_entry_reverse(border_t, b, border_head, list) {
115 const ir_node *irn = b->irn;
116 int nr = get_irn_graph_nr(irn);
121 int col = get_irn_color(irn);
123 int pos = last_pos - seen[nr];
124 int end_pos = last_pos - b->step;
125 int live_in = is_live_in(block, irn);
126 int live_out = is_live_out(block, irn);
127 int y_val = y_dist * col;
130 int green = live_out;
133 fprintf(f, "0 0 0 setrgbcolor\n");
134 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
135 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
136 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
137 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
138 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
139 fprintf(f, "stroke\n");
148 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
150 /* Bring the smaller entry to src. */
162 static INLINE void add_if(const env_t *env, int src, int tgt)
165 edge_init(&edge, src, tgt);
166 set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
169 static INLINE int are_connected(const env_t *env, int src, int tgt)
172 edge_init(&edge, src, tgt);
173 return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
178 static INLINE border_t *border_add(env_t *env, struct list_head *head,
179 const ir_node *irn, int step, int is_def)
181 border_t *b = obstack_alloc(&env->obst, sizeof(*b));
185 list_add_tail(&b->list, head);
189 static void block_alloc(ir_node *block, void *env_ptr)
191 env_t *env = env_ptr;
192 struct obstack *obst = &env->obst;
193 void *obstack_level = obstack_base(obst);
194 bitset_t *live = env->live;
195 bitset_t *colors = env->colors;
196 ir_graph *irg = get_irn_irg(block);
200 int block_nr = get_block_graph_nr(block);
203 struct list_head head;
204 pset *live_in = get_live_in(block);
205 pset *live_out = get_live_out(block);
206 ir_node *idom = get_Block_idom(block);
209 * Check, if this block has already been processed, if true, return
212 if(bitset_is_set(env->processed, block_nr))
216 * Ensure, that the immediate dominator of this block is allocated
217 * before this block, since the values live in at this block are
218 * defined in the dominators of this block. Coloring the dominators
219 * thus is vital before coloring this block.
222 block_alloc(idom, env);
224 /* Clear the live and allocate the color bitset. */
225 bitset_clear_all(live);
226 bitset_clear_all(colors);
228 INIT_LIST_HEAD(&head);
231 * Make final uses of all values live out of the block.
232 * They are neccessary to build up real intervals.
234 for(irn = pset_first(live_out); irn; irn = pset_next(live_out)) {
235 bitset_set(live, get_irn_graph_nr(irn));
236 if(!is_Phi(irn) && is_allocatable_irn(irn))
237 border_add(env, &head, irn, step, 0);
243 * Determine the last uses of a value inside the block, since they are
244 * relevant for the interval borders.
246 sched_foreach_reverse(block, irn) {
247 ir_debugf("insn: %n\n", irn);
248 ir_debugf("live: %b\n", live);
250 get_irn_ra_info(irn)->color = NO_COLOR;
253 * If the node defines a datab value, i.e. something, registers must
254 * be allocated for, add a new def border to the border list.
256 if(is_allocatable_irn(irn)) {
258 int nr = get_irn_graph_nr(irn);
260 bitset_clear(live, nr);
261 border_add(env, &head, irn, step, 1);
263 if(is_phi_operand(irn)) {
264 bitset_foreach(live, elm) {
265 int live_nr = (int) elm;
266 ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
267 if(is_phi_operand(live_irn)) {
268 ir_debugf("\t\tinterfering phi operands: %n, %n\n", irn, live_irn);
269 add_if(env, nr, live_nr);
276 * If the node is no phi node we can examine the uses.
279 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
280 ir_node *op = get_irn_n(irn, i);
282 if(is_allocatable_irn(op)) {
283 int nr = get_irn_graph_nr(op);
285 ir_debugf("\t\tpos: %d, use: %n\n", i, op);
287 if(!bitset_is_set(live, nr)) {
288 border_add(env, &head, op, step, 0);
289 bitset_set(live, nr);
298 bitset_clear_all(live);
301 * Add initial defs for all values live in.
302 * Since their colors have already been assigned (The dominators were
303 * allocated before), we have to mark their colors as used also.
305 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
306 if(is_allocatable_irn(irn)) {
307 int col = get_irn_color(irn);
309 /* Mark the color of the live in value as used. */
310 assert(is_color(col) && "Node must have been assigned a color.");
311 bitset_set(colors, col);
313 /* Mark the value live in. */
314 bitset_set(live, get_irn_graph_nr(irn));
317 border_add(env, &head, irn, step, 1);
321 ir_debugf("usedef chain for block %n\n", block);
322 list_for_each_entry(border_t, b, &head, list) {
323 ir_debugf("\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn));
327 * Mind that the sequence of defs from back to front defines a perfect
328 * elimination order. So, coloring the definitions from first to last
331 list_for_each_entry_reverse(border_t, b, &head, list) {
332 const ir_node *irn = b->irn;
333 int nr = get_irn_graph_nr(irn);
336 * Assign a color, if it is a local def. Global defs already have a
339 if(b->is_def && !is_live_in(block, irn)) {
340 ra_info_t *ri = get_irn_ra_info(irn);
341 int col = bitset_next_clear(colors, 0);
343 assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
344 assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
346 bitset_set(colors, col);
347 bitset_set(live, nr);
350 ri->pressure = bitset_popcnt(colors);
352 ir_debugf("\tassigning color %d to %n\n", col, irn);
355 /* Clear the color upon a use. */
356 else if(!b->is_def) {
357 int col = get_irn_ra_info(irn)->color;
359 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
360 assert(is_color(col) && "A color must have been assigned");
362 bitset_clear(colors, col);
363 bitset_clear(live, nr);
367 draw_interval_graphs(block, &head, &dump_params);
369 /* Mark this block has processed. */
370 bitset_set(env->processed, block_nr);
372 /* Reset the obstack to its initial level */
373 obstack_free(obst, obstack_level);
377 void be_ra_chordal(ir_graph *irg)
379 int node_count = get_graph_node_count(irg);
380 env_t *env = malloc(sizeof(*env));
382 if(get_irg_dom_state(irg) != dom_consistent)
385 obstack_init(&env->obst);
386 env->phi_if = new_set(if_edge_cmp, node_count);
387 env->live = bitset_obstack_alloc(&env->obst, node_count);
388 env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
389 env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
391 irg_block_walk_graph(irg, block_alloc, NULL, env);
392 obstack_free(&env->obst, NULL);
394 set_irg_ra_link(irg, env);
397 void be_ra_chordal_done(ir_graph *irg)
399 env_t *env = get_irg_ra_link(irg);
404 int phi_ops_interfere(const ir_node *a, const ir_node *b)
406 ir_graph *irg = get_irn_irg(a);
407 env_t *env = get_irg_ra_link(irg);
409 assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
411 return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));