Made everything really kaputt
[libfirm] / ir / be / bechordal.c
1 /**
2  * Chordal register allocation.
3  * @author Sebastian Hack
4  * @date 8.12.2004
5  *
6  * Copyright (C) Universitaet Karlsruhe
7  * Released under the GPL
8  */
9
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
13
14 #include <ctype.h>
15
16 #include "obst.h"
17 #include "pset.h"
18 #include "list.h"
19 #include "bitset.h"
20 #include "iterator.h"
21
22 #include "irmode_t.h"
23 #include "irgraph_t.h"
24 #include "irprintf_t.h"
25 #include "irgwalk.h"
26 #include "irdump.h"
27 #include "irdom.h"
28 #include "debug.h"
29 #include "xmalloc.h"
30
31 #include "beutil.h"
32 #include "besched.h"
33 #include "benumb_t.h"
34 #include "besched_t.h"
35 #include "belive_t.h"
36 #include "bearch.h"
37 #include "beifg.h"
38
39 #include "bechordal_t.h"
40 #include "bechordal_draw.h"
41
42 #define DBG_LEVEL SET_LEVEL_0
43 #define DBG_LEVEL_CHECK SET_LEVEL_0
44
45 #define NO_COLOR (-1)
46
47 #undef DUMP_INTERVALS
48
49 typedef struct _be_chordal_alloc_env_t {
50         be_chordal_env_t *chordal_env;
51
52         bitset_t *live;                         /**< A liveness bitset. */
53         bitset_t *colors;                       /**< The color mask. */
54         bitset_t *in_colors;        /**< Colors used by live in values. */
55         int colors_n;               /**< The number of colors. */
56 } be_chordal_alloc_env_t;
57
58 #include "fourcc.h"
59
60 /* Make a fourcc for border checking. */
61 #define BORDER_FOURCC                           FOURCC('B', 'O', 'R', 'D')
62
63 static void check_border_list(struct list_head *head)
64 {
65   border_t *x;
66   list_for_each_entry(border_t, x, head, list) {
67     assert(x->magic == BORDER_FOURCC);
68   }
69 }
70
71 static void check_heads(be_chordal_env_t *env)
72 {
73   pmap_entry *ent;
74   for(ent = pmap_first(env->border_heads); ent; ent = pmap_next(env->border_heads)) {
75     /* ir_printf("checking border list of block %+F\n", ent->key); */
76     check_border_list(ent->value);
77   }
78 }
79
80
81 /**
82  * Add an interval border to the list of a block's list
83  * of interval border.
84  * @note You always have to create the use before the def.
85  * @param env The environment.
86  * @param head The list head to enqueue the borders.
87  * @param irn The node (value) the border belongs to.
88  * @param pressure The pressure at this point in time.
89  * @param step A time step for the border.
90  * @param is_def Is the border a use or a def.
91  * @return The created border.
92  */
93 static INLINE border_t *border_add(be_chordal_env_t *env, struct list_head *head,
94                         ir_node *irn, unsigned step, unsigned pressure,
95                         unsigned is_def, unsigned is_real)
96 {
97         border_t *b;
98
99         if(!is_def) {
100                 border_t *def;
101
102                 b = obstack_alloc(&env->obst, sizeof(*b));
103
104                 /* also allocate the def and tie it to the use. */
105                 def = obstack_alloc(&env->obst, sizeof(*def));
106                 memset(def, 0, sizeof(*def));
107                 b->other_end = def;
108                 def->other_end = b;
109
110                 /*
111                  * Set the link field of the irn to the def.
112                  * This strongly relies on the fact, that the use is always
113                  * made before the def.
114                  */
115                 set_irn_link(irn, def);
116
117                 b->magic = BORDER_FOURCC;
118                 def->magic = BORDER_FOURCC;
119         }
120
121         /*
122          * If the def is encountered, the use was made and so was the
123          * the def node (see the code above). It was placed into the
124          * link field of the irn, so we can get it there.
125          */
126         else {
127                 b = get_irn_link(irn);
128
129                 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
130         }
131
132         b->pressure = pressure;
133         b->is_def = is_def;
134         b->is_real = is_real;
135         b->irn = irn;
136         b->step = step;
137         list_add_tail(&b->list, head);
138         DBG((env->dbg, LEVEL_5, "\t\t%s adding %+F, step: %d\n", is_def ? "def" : "use", irn, step));
139
140
141         return b;
142 }
143
144 /**
145  * Check, if an irn is of the register class currently under processing.
146  * @param env The chordal environment.
147  * @param irn The node.
148  * @return 1, if the node is of that register class, 0 if not.
149  */
150 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
151 {
152   return arch_irn_has_reg_class(env->main_env->arch_env, irn, -1, env->cls);
153 }
154
155 /**
156  * Annotate the register pressure to the nodes and compute
157  * the liveness intervals.
158  * @param block The block to do it for.
159  * @param env_ptr The environment.
160  */
161 static void pressure(ir_node *block, void *env_ptr)
162 {
163 /* Convenience macro for a def */
164 #define border_def(irn, step, real) \
165         border_add(env, head, irn, step, pressure--, 1, real)
166
167 /* Convenience macro for a use */
168 #define border_use(irn, step, real) \
169         border_add(env, head, irn, step, ++pressure, 0, real)
170
171         be_chordal_alloc_env_t *alloc_env = env_ptr;
172         be_chordal_env_t *env             = alloc_env->chordal_env;
173         bitset_t *live                    = alloc_env->live;
174         firm_dbg_module_t *dbg            = env->dbg;
175         ir_node *irn;
176
177         int i, n;
178         unsigned step = 0;
179         unsigned pressure = 0;
180         struct list_head *head;
181         pset *live_in = put_live_in(block, pset_new_ptr_default());
182         pset *live_end = put_live_end(block, pset_new_ptr_default());
183
184         DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
185         bitset_clear_all(live);
186
187         /* Set up the border list in the block info */
188         head = obstack_alloc(&env->obst, sizeof(*head));
189         INIT_LIST_HEAD(head);
190         assert(pmap_get(env->border_heads, block) == NULL);
191         pmap_insert(env->border_heads, block, head);
192
193         /*
194          * Make final uses of all values live out of the block.
195          * They are necessary to build up real intervals.
196          */
197         for(irn = pset_first(live_end); irn; irn = pset_next(live_end)) {
198                 if(has_reg_class(env, irn)) {
199                         DBG((dbg, LEVEL_3, "\tMaking live: %+F/%d\n", irn, get_irn_graph_nr(irn)));
200                         bitset_set(live, get_irn_graph_nr(irn));
201                         border_use(irn, step, 0);
202                 }
203         }
204         ++step;
205
206         /*
207          * Determine the last uses of a value inside the block, since they are
208          * relevant for the interval borders.
209          */
210         sched_foreach_reverse(block, irn) {
211                 DBG((dbg, LEVEL_1, "\tinsn: %+F, pressure: %d\n", irn, pressure));
212                 DBG((dbg, LEVEL_2, "\tlive: %b\n", live));
213
214             /*
215              * If the node defines some value, which can put into a
216              * register of the current class, make a border for it.
217              */
218                 if(has_reg_class(env, irn)) {
219                         int nr = get_irn_graph_nr(irn);
220
221                         bitset_clear(live, nr);
222                         border_def(irn, step, 1);
223                 }
224
225                 /*
226                  * If the node is no phi node we can examine the uses.
227                  */
228                 if(!is_Phi(irn)) {
229                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
230                                 ir_node *op = get_irn_n(irn, i);
231
232                                 if(has_reg_class(env, op)) {
233                                         int nr = get_irn_graph_nr(op);
234
235                                         DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
236
237                                         if(!bitset_is_set(live, nr)) {
238                                                 border_use(op, step, 1);
239                                                 bitset_set(live, nr);
240                                         }
241                                 }
242                         }
243                 }
244                 ++step;
245         }
246
247         /*
248          * Add initial defs for all values live in.
249          */
250         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
251                 if(has_reg_class(env, irn)) {
252
253                         /* Mark the value live in. */
254                         bitset_set(live, get_irn_graph_nr(irn));
255
256                         /* Add the def */
257                         border_def(irn, step, 0);
258                 }
259         }
260
261
262   del_pset(live_in);
263   del_pset(live_end);
264 }
265
266 static void assign(ir_node *block, void *env_ptr)
267 {
268         be_chordal_alloc_env_t *alloc_env = env_ptr;
269         be_chordal_env_t *env       = alloc_env->chordal_env;
270         firm_dbg_module_t *dbg      = env->dbg;
271         bitset_t *live              = alloc_env->live;
272         bitset_t *colors            = alloc_env->colors;
273         bitset_t *in_colors         = alloc_env->in_colors;
274         const arch_env_t *arch_env  = env->main_env->arch_env;
275
276         const ir_node *irn;
277         border_t *b;
278         struct list_head *head = get_block_border_head(env, block);
279         pset *live_in = put_live_in(block, pset_new_ptr_default());
280
281         bitset_clear_all(live);
282         bitset_clear_all(colors);
283         bitset_clear_all(in_colors);
284
285         DBG((dbg, LEVEL_4, "Assigning colors for block %+F\n", block));
286         DBG((dbg, LEVEL_4, "\tusedef chain for block\n"));
287         list_for_each_entry(border_t, b, head, list) {
288                 DBG((dbg, LEVEL_4, "\t%s %+F/%d\n", b->is_def ? "def" : "use",
289                                         b->irn, get_irn_graph_nr(b->irn)));
290         }
291
292         /*
293          * Add initial defs for all values live in.
294          * Since their colors have already been assigned (The dominators were
295          * allocated before), we have to mark their colors as used also.
296          */
297         for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
298                 if(has_reg_class(env, irn)) {
299                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
300                         int col;
301
302                         assert(reg && "Node must have been assigned a register");
303                         col = arch_register_get_index(reg);
304
305                         /* Mark the color of the live in value as used. */
306                         bitset_set(colors, col);
307                         bitset_set(in_colors, col);
308
309                         /* Mark the value live in. */
310                         bitset_set(live, get_irn_graph_nr(irn));
311                 }
312         }
313
314         /*
315          * Mind that the sequence of defs from back to front defines a perfect
316          * elimination order. So, coloring the definitions from first to last
317          * will work.
318          */
319         list_for_each_entry_reverse(border_t, b, head, list) {
320                 ir_node *irn = b->irn;
321                 int nr = get_irn_graph_nr(irn);
322
323                 /*
324                  * Assign a color, if it is a local def. Global defs already have a
325                  * color.
326                  */
327                 if(b->is_def && !is_live_in(block, irn)) {
328                         const arch_register_t *reg;
329                         int col = NO_COLOR;
330
331                         DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
332
333                         col = bitset_next_clear(colors, 0);
334                         reg = arch_register_for_index(env->cls, col);
335
336                         assert(arch_get_irn_register(arch_env, irn) == NULL && "This node must not have been assigned a register yet");
337                         assert(!bitset_is_set(live, nr) && "Value's definition must not have been encountered");
338
339                         bitset_set(colors, col);
340                         bitset_set(live, nr);
341
342                         arch_set_irn_register(arch_env, irn, reg);
343                         DBG((dbg, LEVEL_1, "\tassigning register %s(%d) to %+F\n",
344             arch_register_get_name(reg), col, irn));
345                 }
346
347                 /* Clear the color upon a use. */
348                 else if(!b->is_def) {
349                         const arch_register_t *reg = arch_get_irn_register(arch_env, irn);
350                         int col;
351
352                         assert(reg && "Register must have been assigned");
353
354                         col = arch_register_get_index(reg);
355                         assert(bitset_is_set(live, nr) && "Cannot have a non live use");
356
357                         bitset_clear(colors, col);
358                         bitset_clear(live, nr);
359                 }
360         }
361
362         del_pset(live_in);
363 }
364
365 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
366 {
367         int node_count        = get_graph_node_count(chordal_env->irg);
368         int colors_n          = arch_register_class_n_regs(chordal_env->cls);
369         ir_graph *irg         = chordal_env->irg;
370         void *base            = obstack_base(&chordal_env->obst);
371
372         be_chordal_alloc_env_t env;
373
374         if(get_irg_dom_state(irg) != dom_consistent)
375                 compute_doms(irg);
376
377         env.chordal_env  = chordal_env;
378         env.live         = bitset_obstack_alloc(&chordal_env->obst, node_count);
379         env.colors       = bitset_obstack_alloc(&chordal_env->obst, colors_n);
380         env.in_colors    = bitset_obstack_alloc(&chordal_env->obst, colors_n);
381         env.colors_n     = colors_n;
382
383         /* First, determine the pressure */
384         dom_tree_walk_irg(irg, pressure, NULL, &env);
385
386         /* Assign the colors */
387         dom_tree_walk_irg(irg, assign, NULL, &env);
388
389 #ifdef DUMP_INTERVALS
390         {
391                 char buf[128];
392         plotter_t *plotter;
393
394                 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
395         plotter = new_plotter_ps(buf);
396
397         draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter, env->arch_env, cls);
398         plotter_free(plotter);
399         }
400 #endif
401
402         obstack_free(&chordal_env->obst, base);
403 }