bdc880d651a3e7295900aac4222d71ac6b7377fc
[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 "xmalloc.h"
21
22 #include "beutil.h"
23 #include "besched.h"
24 #include "bera_t.h"
25 #include "benumb_t.h"
26 #include "besched_t.h"
27 #include "belive_t.h"
28
29 #define TEST_COLORS 2048
30
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. */
37 } border_t;
38
39 typedef struct _if_edge_t {
40         int src, tgt;
41 } if_edge_t;
42
43 #define IF_EDGE_HASH(e) ((e)->src)
44
45 static int if_edge_cmp(const void *p1, const void *p2, size_t size)
46 {
47         const if_edge_t *e1 = p1;
48         const if_edge_t *e2 = p2;
49
50         return !(e1->src == e2->src && e1->tgt == e2->tgt);
51 }
52
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. */
59 } env_t;
60
61 typedef struct _be_chordal_dump_params_t {
62         int x_dist;
63         int y_dist;
64         double font_scale;
65 } be_chordal_dump_params_t;
66
67 static const be_chordal_dump_params_t dump_params = {
68         30,
69         10,
70         4
71 };
72
73 static void draw_interval_graphs(ir_node *block,
74                 struct list_head *border_head,
75                 const be_chordal_dump_params_t *params)
76 {
77         int i;
78         int x_dist = params->x_dist;
79         int y_dist = params->y_dist;
80         ir_graph *irg = get_irn_irg(block);
81
82         FILE *f;
83         char buf[1024];
84
85         ir_snprintf(buf, sizeof(buf), "%s_bl%N.eps",
86                         get_entity_name(get_irg_entity(irg)), block);
87
88         if((f = fopen(buf, "wt")) != NULL) {
89                 border_t *b;
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;
92                 int max_col = 0;
93
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;
98                 }
99
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");
106
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");
112                 }
113                 fprintf(f, "0.5 setlinewidth\n");
114
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);
118
119                         if(b->is_def)
120                                 seen[nr] = b->step;
121                         else {
122                                 int col = get_irn_color(irn);
123
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;
129
130                                 int red = 0;
131                                 int green = live_out;
132                                 int blue = live_in;
133
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");
141                         }
142                 }
143
144                 free(seen);
145                 fclose(f);
146         }
147 }
148
149 static INLINE if_edge_t *edge_init(if_edge_t *edge, int src, int tgt)
150 {
151         /* Bring the smaller entry to src. */
152         if(src > tgt) {
153                 edge->src = tgt;
154                 edge->tgt = src;
155         } else {
156                 edge->src = src;
157                 edge->tgt = tgt;
158         }
159
160         return edge;
161 }
162
163 static INLINE void add_if(const env_t *env, int src, int tgt)
164 {
165         if_edge_t edge;
166         edge_init(&edge, src, tgt);
167         set_insert(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge));
168 }
169
170 static INLINE int are_connected(const env_t *env, int src, int tgt)
171 {
172         if_edge_t edge;
173         edge_init(&edge, src, tgt);
174         return set_find(env->phi_if, &edge, sizeof(edge), IF_EDGE_HASH(&edge)) != NULL;
175 }
176
177
178
179 static INLINE border_t *border_add(env_t *env, struct list_head *head,
180                         const ir_node *irn, int step, int is_def)
181 {
182         border_t *b = obstack_alloc(&env->obst, sizeof(*b));
183         b->is_def = is_def;
184         b->irn = irn;
185         b->step = step;
186         list_add_tail(&b->list, head);
187         return b;
188 }
189
190 static void block_alloc(ir_node *block, void *env_ptr)
191 {
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);
198
199         int i, n;
200         unsigned step = 0;
201         int block_nr = get_block_graph_nr(block);
202         const ir_node *irn;
203         border_t *b;
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);
208
209         /*
210          * Check, if this block has already been processed, if true, return
211          * immediately.
212          */
213         if(bitset_is_set(env->processed, block_nr))
214                 return;
215
216         /*
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.
221          */
222         if(idom)
223                 block_alloc(idom, env);
224
225         /* Clear the live and allocate the color bitset. */
226         bitset_clear_all(live);
227         bitset_clear_all(colors);
228
229         INIT_LIST_HEAD(&head);
230
231         /*
232          * Make final uses of all values live out of the block.
233          * They are neccessary to build up real intervals.
234          */
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);
239         }
240
241         ++step;
242
243         /*
244          * Determine the last uses of a value inside the block, since they are
245          * relevant for the interval borders.
246          */
247         sched_foreach_reverse(block, irn) {
248                 ir_debugf("insn: %n\n", irn);
249                 ir_debugf("live: %b\n", live);
250
251                 get_irn_ra_info(irn)->color = NO_COLOR;
252
253                 /*
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.
256                  */
257                 if(is_allocatable_irn(irn)) {
258                         unsigned long elm;
259                         int nr = get_irn_graph_nr(irn);
260
261                         bitset_clear(live, nr);
262                         border_add(env, &head, irn, step, 1);
263
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);
271                                         }
272                                 }
273                         }
274                 }
275
276                 /*
277                  * If the node is no phi node we can examine the uses.
278                  */
279                 if(!is_Phi(irn)) {
280                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
281                                 ir_node *op = get_irn_n(irn, i);
282
283                                 if(is_allocatable_irn(op)) {
284                                         int nr = get_irn_graph_nr(op);
285
286                                         ir_debugf("\t\tpos: %d, use: %n\n", i, op);
287
288                                         if(!bitset_is_set(live, nr)) {
289                                                 border_add(env, &head, op, step, 0);
290                                                 bitset_set(live, nr);
291                                         }
292                                 }
293                         }
294                 }
295
296                 ++step;
297         }
298
299         bitset_clear_all(live);
300
301         /*
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.
305          */
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);
309
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);
313
314                         /* Mark the value live in. */
315                         bitset_set(live, get_irn_graph_nr(irn));
316
317                         /* Add the def */
318                         border_add(env, &head, irn, step, 1);
319                 }
320         }
321
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));
325         }
326
327         /*
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
330          * will work.
331          */
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);
335
336                 /*
337                  * Assign a color, if it is a local def. Global defs already have a
338                  * color.
339                  */
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);
343
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");
346
347                         bitset_set(colors, col);
348                         bitset_set(live, nr);
349
350                         ri->color = col;
351                         ri->pressure = bitset_popcnt(colors);
352
353                         ir_debugf("\tassigning color %d to %n\n", col, irn);
354                 }
355
356                 /* Clear the color upon a use. */
357                 else if(!b->is_def) {
358                         int col = get_irn_ra_info(irn)->color;
359
360                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
361                         assert(is_color(col) && "A color must have been assigned");
362
363                         bitset_clear(colors, col);
364                         bitset_clear(live, nr);
365                 }
366         }
367
368         draw_interval_graphs(block, &head, &dump_params);
369
370         /* Mark this block has processed. */
371         bitset_set(env->processed, block_nr);
372
373         /* Reset the obstack to its initial level */
374         obstack_free(obst, obstack_level);
375 }
376
377
378 void be_ra_chordal(ir_graph *irg)
379 {
380         int node_count = get_graph_node_count(irg);
381         env_t *env = malloc(sizeof(*env));
382
383         if(get_irg_dom_state(irg) != dom_consistent)
384                 compute_doms(irg);
385
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);
391
392         irg_block_walk_graph(irg, block_alloc, NULL, env);
393         obstack_free(&env->obst, NULL);
394
395         set_irg_ra_link(irg, env);
396 }
397
398 void be_ra_chordal_done(ir_graph *irg)
399 {
400         env_t *env = get_irg_ra_link(irg);
401         free(env->phi_if);
402         free(env);
403 }
404
405 int phi_ops_interfere(const ir_node *a, const ir_node *b)
406 {
407         ir_graph *irg = get_irn_irg(a);
408         env_t *env = get_irg_ra_link(irg);
409
410         assert(irg == get_irn_irg(b) && "Both nodes must be in the same graph");
411
412         return are_connected(env, get_irn_graph_nr(a), get_irn_graph_nr(b));
413 }