2 * Chordal register allocation.
3 * @author Sebastian Hack
6 * Copyright (C) Universitaet Karlsruhe
7 * Released under the GPL
23 #include "irgraph_t.h"
24 #include "irprintf_t.h"
34 #include "besched_t.h"
39 #include "bechordal_t.h"
40 #include "bechordal_draw.h"
42 #define DBG_LEVEL SET_LEVEL_0
43 #define DBG_LEVEL_CHECK SET_LEVEL_0
49 typedef struct _be_chordal_alloc_env_t {
50 be_chordal_env_t *chordal_env;
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;
60 /* Make a fourcc for border checking. */
61 #define BORDER_FOURCC FOURCC('B', 'O', 'R', 'D')
63 static void check_border_list(struct list_head *head)
66 list_for_each_entry(border_t, x, head, list) {
67 assert(x->magic == BORDER_FOURCC);
71 static void check_heads(be_chordal_env_t *env)
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);
82 * Add an interval border to the list of a block's list
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.
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)
102 b = obstack_alloc(&env->obst, sizeof(*b));
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));
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.
115 set_irn_link(irn, def);
117 b->magic = BORDER_FOURCC;
118 def->magic = BORDER_FOURCC;
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.
127 b = get_irn_link(irn);
129 assert(b && b->magic == BORDER_FOURCC && "Illegal border encountered");
132 b->pressure = pressure;
134 b->is_real = is_real;
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));
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.
150 static INLINE int has_reg_class(const be_chordal_env_t *env, const ir_node *irn)
152 return arch_irn_has_reg_class(env->main_env->arch_env, irn, -1, env->cls);
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.
161 static void pressure(ir_node *block, void *env_ptr)
163 /* Convenience macro for a def */
164 #define border_def(irn, step, real) \
165 border_add(env, head, irn, step, pressure--, 1, real)
167 /* Convenience macro for a use */
168 #define border_use(irn, step, real) \
169 border_add(env, head, irn, step, ++pressure, 0, real)
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;
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());
184 DBG((dbg, LEVEL_1, "Computing pressure in block %+F\n", block));
185 bitset_clear_all(live);
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);
194 * Make final uses of all values live out of the block.
195 * They are necessary to build up real intervals.
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);
207 * Determine the last uses of a value inside the block, since they are
208 * relevant for the interval borders.
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));
215 * If the node defines some value, which can put into a
216 * register of the current class, make a border for it.
218 if(has_reg_class(env, irn)) {
219 int nr = get_irn_graph_nr(irn);
221 bitset_clear(live, nr);
222 border_def(irn, step, 1);
226 * If the node is no phi node we can examine the uses.
229 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
230 ir_node *op = get_irn_n(irn, i);
232 if(has_reg_class(env, op)) {
233 int nr = get_irn_graph_nr(op);
235 DBG((dbg, LEVEL_4, "\t\tpos: %d, use: %+F\n", i, op));
237 if(!bitset_is_set(live, nr)) {
238 border_use(op, step, 1);
239 bitset_set(live, nr);
248 * Add initial defs for all values live in.
250 for(irn = pset_first(live_in); irn; irn = pset_next(live_in)) {
251 if(has_reg_class(env, irn)) {
253 /* Mark the value live in. */
254 bitset_set(live, get_irn_graph_nr(irn));
257 border_def(irn, step, 0);
266 static void assign(ir_node *block, void *env_ptr)
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;
278 struct list_head *head = get_block_border_head(env, block);
279 pset *live_in = put_live_in(block, pset_new_ptr_default());
281 bitset_clear_all(live);
282 bitset_clear_all(colors);
283 bitset_clear_all(in_colors);
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)));
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.
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);
302 assert(reg && "Node must have been assigned a register");
303 col = arch_register_get_index(reg);
305 /* Mark the color of the live in value as used. */
306 bitset_set(colors, col);
307 bitset_set(in_colors, col);
309 /* Mark the value live in. */
310 bitset_set(live, get_irn_graph_nr(irn));
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
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);
324 * Assign a color, if it is a local def. Global defs already have a
327 if(b->is_def && !is_live_in(block, irn)) {
328 const arch_register_t *reg;
331 DBG((dbg, LEVEL_4, "\tcolors in use: %b\n", colors));
333 col = bitset_next_clear(colors, 0);
334 reg = arch_register_for_index(env->cls, col);
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");
339 bitset_set(colors, col);
340 bitset_set(live, nr);
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));
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);
352 assert(reg && "Register must have been assigned");
354 col = arch_register_get_index(reg);
355 assert(bitset_is_set(live, nr) && "Cannot have a non live use");
357 bitset_clear(colors, col);
358 bitset_clear(live, nr);
365 void be_ra_chordal_color(be_chordal_env_t *chordal_env)
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);
372 be_chordal_alloc_env_t env;
374 if(get_irg_dom_state(irg) != dom_consistent)
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;
383 /* First, determine the pressure */
384 dom_tree_walk_irg(irg, pressure, NULL, &env);
386 /* Assign the colors */
387 dom_tree_walk_irg(irg, assign, NULL, &env);
389 #ifdef DUMP_INTERVALS
394 ir_snprintf(buf, sizeof(buf), "ifg_%s_%F.eps", cls->name, irg);
395 plotter = new_plotter_ps(buf);
397 draw_interval_tree(&draw_chordal_def_opts, chordal_env, plotter, env->arch_env, cls);
398 plotter_free(plotter);
402 obstack_free(&chordal_env->obst, base);