8338767ea87a3745b2d315030bcf80af071c7b62
[libfirm] / ir / be / bechordal.c
1 /**
2  * Chordal register allocation.
3  * @author Sebastian Hack
4  * @date 8.12.2004
5  */
6
7 #include <ctype.h>
8
9 #include "obst.h"
10 #include "list.h"
11 #include "bitset.h"
12 #include "iterator.h"
13
14 #include "irmode_t.h"
15 #include "irprintf_t.h"
16 #include "irgwalk.h"
17 #include "irgraph.h"
18 #include "irdump.h"
19 #include "irdom.h"
20 #include "debug.h"
21 #include "xmalloc.h"
22
23 #include "beutil.h"
24 #include "besched.h"
25 #include "bera_t.h"
26 #include "benumb_t.h"
27 #include "besched_t.h"
28 #include "belive_t.h"
29
30 #undef DUMP_INTERVALS
31 #undef DUMP_PRESSURE
32
33 #define TEST_COLORS 2048
34
35 static firm_dbg_module_t *dbg;
36
37 /** An interval border. */
38 typedef struct _border_t {
39         struct list_head list;          /**< list head for queuing. */
40         const ir_node *irn;                             /**< The node. */
41         unsigned step;                                          /**< The number equal to the interval border. */
42         unsigned is_def : 1;                    /**< Does this border denote a use or a def. */
43 } border_t;
44
45 typedef struct _if_edge_t {
46         int src, tgt;
47 } if_edge_t;
48
49 #define IF_EDGE_HASH(e) ((e)->src)
50
51 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
52 {
53         const if_edge_t *e1 = p1;
54         const if_edge_t *e2 = p2;
55
56         return !(e1->src == e2->src && e1->tgt == e2->tgt);
57 }
58
59 typedef struct _env_t {
60         struct obstack obst;    /**< An obstack for temporary storage. */
61         set *phi_if;                                    /**< The phi interference map. */
62         bitset_t *live;                 /**< A live bitset to use in every block. */
63         bitset_t *processed;    /**< A set marking processed blocks. */
64         bitset_t *colors;                       /**< The color mask. */
65         int colors_n;                                   /**< The number of colors. */
66 } env_t;
67
68 typedef struct _be_chordal_dump_params_t {
69         int x_dist;
70         int y_dist;
71         double font_scale;
72 } be_chordal_dump_params_t;
73
74 static const be_chordal_dump_params_t dump_params = {
75         30,
76         10,
77         4
78 };
79
80 static void draw_interval_graphs(ir_node *block,
81                 struct list_head *border_head,
82                 const be_chordal_dump_params_t *params)
83 {
84         int i;
85         int x_dist = params->x_dist;
86         int y_dist = params->y_dist;
87         ir_graph *irg = get_irn_irg(block);
88
89         FILE *f;
90         char buf[1024];
91
92         ir_snprintf(buf, sizeof(buf), "intv_%s_bl%N.eps",
93                         get_entity_name(get_irg_entity(irg)), block);
94
95         if((f = fopen(buf, "wt")) != NULL) {
96                 border_t *b;
97                 int *seen = xcalloc(get_graph_node_count(irg), sizeof(seen[0]));
98                 int last_pos = list_empty(border_head) ? 0 : list_entry(border_head->prev, border_t, list)->step;
99                 int max_col = 0;
100
101                 list_for_each_entry_reverse(border_t, b, border_head, list) {
102                         const ir_node *irn = b->irn;
103                         int col = get_irn_color(irn);
104                         max_col = max_col > col ? max_col : col;
105                 }
106
107                 fprintf(f, "%%!PS-Adobe-2.0\n");
108                 fprintf(f, "%%%%BoundingBox: -10 -10 %d %d\n",
109                                 x_dist * last_pos + x_dist, y_dist * max_col + y_dist);
110                 fprintf(f, "/mainfont /Courier findfont %f scalefont def\n", params->font_scale);
111                 fprintf(f, "mainfont setfont\n");
112                 fprintf(f, "0.2 setlinewidth\n");
113
114                 for(i = 0; i <= last_pos; ++i) {
115                         fprintf(f, "0 0 0 setrgbcolor\n");
116                         fprintf(f, "%d %d moveto\n", i * x_dist, -2);
117                         fprintf(f, "%d %d lineto\n", i * x_dist, max_col * y_dist + 2);
118                         fprintf(f, "stroke\n");
119                 }
120                 fprintf(f, "0.5 setlinewidth\n");
121
122                 list_for_each_entry_reverse(border_t, b, border_head, list) {
123                         const ir_node *irn = b->irn;
124                         int nr = get_irn_graph_nr(irn);
125
126                         if(b->is_def)
127                                 seen[nr] = b->step;
128                         else {
129                                 int col = get_irn_color(irn);
130
131                                 int pos = last_pos - seen[nr];
132                                 int end_pos = last_pos - b->step;
133                                 int live_in = is_live_in(block, irn);
134                                 int live_end = is_live_end(block, irn);
135                                 int y_val = y_dist * col;
136
137                                 int red = 0;
138                                 int green = live_end;
139                                 int blue = live_in;
140
141                                 fprintf(f, "0 0 0 setrgbcolor\n");
142                                 fprintf(f, "%d %d moveto\n", x_dist * pos + 2, y_val + 2);
143                                 ir_fprintf(f, "(%n/%d%s) show\n", irn, nr, is_phi_operand(irn) ? "*" : "");
144                                 fprintf(f, "%d %d %d setrgbcolor\n", red, green, blue);
145                                 fprintf(f, "%d %d moveto\n", x_dist * pos, y_val);
146                                 fprintf(f, "%d %d lineto\n", (x_dist * end_pos) - 5, y_val);
147                                 fprintf(f, "stroke\n");
148                         }
149                 }
150
151                 free(seen);
152                 fclose(f);
153         }
154 }
155
156 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
157 {
158         /* Bring the smaller entry to src. */
159         if(src > tgt) {
160                 edge->src = tgt;
161                 edge->tgt = src;
162         } else {
163                 edge->src = src;
164                 edge->tgt = tgt;
165         }
166
167         return edge;
168 }
169
170 static INLINE void add_if(const env_t *env, int src, int tgt)
171 {
172         if_edge_t edge;
173         edge_init(&edge, src, tgt);
174         set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
175 }
176
177 static INLINE int are_connected(const env_t *env, int src, int tgt)
178 {
179         if_edge_t edge;
180         edge_init(&edge, src, tgt);
181         return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
182 }
183
184
185
186 static INLINE border_t *border_add(env_t *env, struct list_head *head,
187                         const ir_node *irn, int step, int is_def)
188 {
189         border_t *b = obstack_alloc(&env->obst, sizeof(*b));
190         b->is_def = is_def;
191         b->irn = irn;
192         b->step = step;
193         list_add_tail(&b->list, head);
194         return b;
195 }
196
197 static void block_alloc(ir_node *block, void *env_ptr)
198 {
199         env_t *env = env_ptr;
200         struct obstack *obst = &env->obst;
201         void *obstack_level = obstack_base(obst);
202         bitset_t *live = env->live;
203         bitset_t *colors = env->colors;
204         bitset_t *used_colors = bitset_malloc(env->colors_n);
205         ir_graph *irg = get_irn_irg(block);
206
207         int i, n;
208         unsigned step = 0;
209         int block_nr = get_block_graph_nr(block);
210         const ir_node *irn;
211         border_t *b;
212         struct list_head head;
213         pset *live_in = get_live_in(block);
214         pset *live_end = get_live_end(block);
215         ir_node *idom = get_Block_idom(block);
216
217         /*
218          * Check, if this block has already been processed, if true, return
219          * immediately.
220          */
221         if(bitset_is_set(env->processed, block_nr))
222                 return;
223
224         /*
225          * Ensure, that the immediate dominator of this block is allocated
226          * before this block, since the values live in at this block are
227          * defined in the dominators of this block. Coloring the dominators
228          * thus is vital before coloring this block.
229          */
230         if(idom)
231                 block_alloc(idom, env);
232
233         /* Clear the live and allocate the color bitset. */
234         bitset_clear_all(live);
235         bitset_clear_all(colors);
236
237         INIT_LIST_HEAD(&head);
238
239         /*
240          * Make final uses of all values live out of the block.
241          * They are neccessary to build up real intervals.
242          */
243         for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
244                 DBG((dbg, LEVEL_3, "Making live: %n/%d\n", irn, get_irn_graph_nr(irn)));
245                 bitset_set(live, get_irn_graph_nr(irn));
246                 if(!is_Phi(irn) && is_allocatable_irn(irn))
247                         border_add(env, &head, irn, step, 0);
248         }
249
250         ++step;
251
252         /*
253          * Determine the last uses of a value inside the block, since they are
254          * relevant for the interval borders.
255          */
256         sched_foreach_reverse(block, irn) {
257                 DBG((dbg, LEVEL_1, "insn: %n\n", irn));
258                 DBG((dbg, LEVEL_2, "live: %b\n", live));
259
260                 set_irn_color(irn, NO_COLOR);
261
262                 /*
263                  * If the node defines a datab value, i.e. something, registers must
264                  * be allocated for, add a new def border to the border list.
265                  */
266                 if(is_allocatable_irn(irn)) {
267                         int nr = get_irn_graph_nr(irn);
268
269                         bitset_clear(live, nr);
270                         border_add(env, &head, irn, step, 1);
271
272                         if(is_phi_operand(irn)) {
273                                 unsigned long elm;
274                                 bitset_foreach(live, elm) {
275                                         int live_nr = (int) elm;
276                                         ir_node *live_irn = get_irn_for_graph_nr(irg, live_nr);
277                                         if(is_phi_operand(live_irn)) {
278                                                 DBG((dbg, LEVEL_3, "\t\tinterfering phi operands: %n, %n\n", irn, live_irn));
279                                                 add_if(env, nr, live_nr);
280                                         }
281                                 }
282                         }
283                 }
284
285                 /*
286                  * If the node is no phi node we can examine the uses.
287                  */
288                 if(!is_Phi(irn)) {
289                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
290                                 ir_node *op = get_irn_n(irn, i);
291
292                                 if(is_allocatable_irn(op)) {
293                                         int nr = get_irn_graph_nr(op);
294
295                                         DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %n\n", i, op));
296
297                                         if(!bitset_is_set(live, nr)) {
298                                                 border_add(env, &head, op, step, 0);
299                                                 bitset_set(live, nr);
300                                         }
301                                 }
302                         }
303                 }
304
305                 ++step;
306         }
307
308         bitset_clear_all(live);
309
310         /*
311          * Add initial defs for all values live in.
312          * Since their colors have already been assigned (The dominators were
313          * allocated before), we have to mark their colors as used also.
314          */
315         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
316                 if(is_allocatable_irn(irn)) {
317                         int col = get_irn_color(irn);
318
319                         /* Mark the color of the live in value as used. */
320                         assert(is_color(col) && "Node must have been assigned a color.");
321                         bitset_set(colors, col);
322                         bitset_set(used_colors, col);
323
324                         /* Mark the value live in. */
325                         bitset_set(live, get_irn_graph_nr(irn));
326
327                         /* Add the def */
328                         border_add(env, &head, irn, step, 1);
329                 }
330         }
331
332         DBG((dbg, LEVEL_4, "usedef chain for block %n\n", block));
333         list_for_each_entry(border_t, b, &head, list) {
334                 DBG((dbg, LEVEL_4, "\t%s %n %d\n", b->is_def ? "def" : "use", b->irn, get_irn_graph_nr(b->irn)));
335         }
336
337         /*
338          * Mind that the sequence of defs from back to front defines a perfect
339          * elimination order. So, coloring the definitions from first to last
340          * will work.
341          */
342         list_for_each_entry_reverse(border_t, b, &head, list) {
343                 const ir_node *irn = b->irn;
344                 int nr = get_irn_graph_nr(irn);
345
346                 /*
347                  * Assign a color, if it is a local def. Global defs already have a
348                  * color.
349                  */
350                 if(b->is_def && !is_live_in(block, irn)) {
351                         ra_node_info_t *ri = get_ra_node_info(irn);
352                         int col = bitset_next_clear(colors, 0);
353
354                         assert(!is_color(get_irn_color(irn)) && "Color must not have assigned");
355                         assert(!bitset_is_set(live, nr) && "Value def must not have been encountered");
356
357                         bitset_set(colors, col);
358                         bitset_set(used_colors, col);
359                         bitset_set(live, nr);
360
361                         ri->color = col;
362                         ri->pressure = bitset_popcnt(colors);
363
364                         DBG((dbg, LEVEL_1, "\tassigning color %d to %n\n", col, irn));
365                 }
366
367                 /* Clear the color upon a use. */
368                 else if(!b->is_def) {
369                         int col = get_irn_color(irn);
370
371                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
372                         assert(is_color(col) && "A color must have been assigned");
373
374                         bitset_clear(colors, col);
375                         bitset_clear(live, nr);
376                 }
377         }
378
379 #ifdef DUMP_INTERVALS
380         draw_interval_graphs(block, &head, &dump_params);
381 #endif
382
383 #ifdef DUMP_PRESSURE
384         {
385                 char buf[128];
386                 FILE *f;
387
388                 ir_snprintf(buf, sizeof(buf), "pres_%s_bl_%N.txt",
389                                 get_entity_name(get_irg_entity(irg)), block);
390
391                 if((f = fopen(buf, "wt")) != NULL) {
392                         sched_foreach_reverse(block, irn) {
393                                 if(is_allocatable_irn(irn))
394                                         ir_fprintf(f, "\"%n\" %d %d\n", irn, sched_get_time_step(irn),
395                                                         get_ra_node_info(irn)->pressure);
396
397                         }
398                         fclose(f);
399                 }
400         }
401 #endif
402
403         /*
404          * Allocate the used colors array in the blocks ra info structure and
405          * fill it.
406          */
407         get_ra_block_info(block)->used_colors = used_colors;
408
409         /* Mark this block has processed. */
410         bitset_set(env->processed, block_nr);
411
412         /* Reset the obstack to its initial level */
413         obstack_free(obst, obstack_level);
414 }
415
416 void be_ra_chordal_init(void)
417 {
418         dbg = firm_dbg_register(DBG_BERA);
419         /* firm_dbg_set_mask(dbg, -1); */
420 }
421
422 void be_ra_chordal(ir_graph *irg)
423 {
424         int node_count = get_graph_node_count(irg);
425         env_t *env = malloc(sizeof(*env));
426
427         if(get_irg_dom_state(irg) != dom_consistent)
428                 compute_doms(irg);
429
430         obstack_init(&env->obst);
431         env->phi_if = new_set(if_edge_cmp, node_count);
432         env->live = bitset_obstack_alloc(&env->obst, node_count);
433         env->processed = bitset_obstack_alloc(&env->obst, get_graph_block_count(irg));
434         env->colors = bitset_obstack_alloc(&env->obst, TEST_COLORS);
435         env->colors_n = TEST_COLORS;
436
437         irg_block_walk_graph(irg, block_alloc, NULL, env);
438         obstack_free(&env->obst, NULL);
439
440         set_irg_ra_link(irg, env);
441 }
442
443 void be_ra_chordal_done(ir_graph *irg)
444 {
445         env_t *env = get_irg_ra_link(irg);
446         free(env->phi_if);
447         free(env);
448 }
449
450 int phi_ops_interfere(const ir_node *a, const ir_node *b)
451 {
452         return values_interfere(a, b);
453 }
454
455 #if 0
456 int phi_ops_interfere(const ir_node *a, const ir_node *b)
457 {
458         ir_graph *irg = get_irn_irg(a);
459         env_t *env = get_irg_ra_link(irg);
460
461         assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
462
463         return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
464 }
465 #endif