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