2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Morgans spill algorithm.
23 * @author Matthias Braun
33 #include "irgraph_t.h"
38 #include "bespillmorgan.h"
39 #include "bechordal_t.h"
43 #include "bespillbelady.h"
45 #include "benodesets.h"
46 #include "bespilloptions.h"
54 #define DBG_PRESSURE 4
57 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
59 typedef struct morgan_env {
60 const arch_env_t *arch;
61 const arch_register_class_t *cls;
65 /** maximum safe register pressure */
66 int registers_available;
74 typedef struct loop_edge {
79 typedef struct loop_attr {
83 /** The set of all values that are live in the loop but not used in the loop */
84 bitset_t *livethrough_unused;
87 typedef struct morgan_block_attr {
89 /** set of all values that are live in the block but not used in the block */
90 bitset_t *livethrough_unused;
93 //---------------------------------------------------------------------------
95 static int loop_edge_cmp(const void* p1, const void* p2, size_t size) {
96 loop_edge_t *e1 = (loop_edge_t*) p1;
97 loop_edge_t *e2 = (loop_edge_t*) p2;
100 return e1->block != e2->block || e1->pos != e2->pos;
103 static int loop_attr_cmp(const void *e1, const void *e2, size_t size) {
104 loop_attr_t *la1 = (loop_attr_t*) e1;
105 loop_attr_t *la2 = (loop_attr_t*) e2;
108 return la1->loop != la2->loop;
111 static int block_attr_cmp(const void *e1, const void *e2, size_t size) {
112 block_attr_t *b1 = (block_attr_t*) e1;
113 block_attr_t *b2 = (block_attr_t*) e2;
116 return b1->block != b2->block;
119 static INLINE int loop_attr_hash(const loop_attr_t *a) {
121 return a->loop->loop_nr;
123 return HASH_PTR(a->loop);
127 static INLINE int block_attr_hash(const block_attr_t *b) {
128 return nodeset_hash(b->block);
131 static INLINE int loop_edge_hash(const loop_edge_t *e) {
132 return nodeset_hash(e->block) ^ (e->pos * 31);
135 static INLINE loop_attr_t *get_loop_attr(morgan_env_t *env, const ir_loop *loop) {
136 loop_attr_t l_attr, *res;
140 hash = loop_attr_hash(&l_attr);
141 res = set_find(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
143 // create new loop_attr if none exists yet
145 l_attr.out_edges = new_set(loop_edge_cmp, 1);
146 l_attr.in_edges = new_set(loop_edge_cmp, 1);
147 l_attr.livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
148 res = set_insert(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
154 static INLINE block_attr_t *get_block_attr(morgan_env_t *env, const ir_node *block) {
155 block_attr_t b_attr, *res;
157 b_attr.block = block;
159 hash = block_attr_hash(&b_attr);
160 res = set_find(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
163 b_attr.livethrough_unused = NULL;
164 res = set_insert(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
170 //---------------------------------------------------------------------------
172 static INLINE int consider_for_spilling(const arch_env_t *env, const arch_register_class_t *cls, const ir_node *node) {
173 if(!arch_irn_has_reg_class(env, node, -1, cls))
176 return !(arch_irn_get_flags(env, node) & (arch_irn_flags_ignore | arch_irn_flags_dont_spill));
180 * Determine edges going out of a loop (= edges that go to a block that is not
181 * inside the loop or one of its subloops)
183 static INLINE void construct_loop_edges(ir_node *block, void *data) {
184 morgan_env_t *env = data;
185 int n_cfgpreds = get_Block_n_cfgpreds(block);
187 ir_loop* loop = get_irn_loop(block);
188 DBG((dbg, DBG_LOOPANA, "Loop for %+F: %d (depth %d)\n", block, loop->loop_nr, loop->depth));
190 for(i = 0; i < n_cfgpreds; ++i) {
193 ir_node* cfgpred = get_Block_cfgpred(block, i);
194 ir_node* cfgpred_block = get_nodes_block(cfgpred);
195 ir_loop* cfgpred_loop = get_irn_loop(cfgpred_block);
197 if(cfgpred_loop == loop)
200 assert(get_loop_depth(cfgpred_loop) != get_loop_depth(loop));
204 hash = loop_edge_hash(&edge);
206 // edge out of a loop?
207 if(get_loop_depth(cfgpred_loop) > get_loop_depth(loop)) {
210 DBG((dbg, DBG_LOOPANA, "Loop out edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
211 cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
213 /* this might be a jump out of multiple loops, so add this to all
214 * needed outedge sets */
217 loop_attr_t *l_attr = get_loop_attr(env, l);
218 set_insert(l_attr->out_edges, &edge, sizeof(edge), hash);
220 l = get_loop_outer_loop(l);
227 DBG((dbg, DBG_LOOPANA, "Loop in edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
228 cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
232 loop_attr_t *l_attr = get_loop_attr(env, l);
233 set_insert(l_attr->in_edges, &edge, sizeof(edge), hash);
235 l = get_loop_outer_loop(l);
236 } while(l != cfgpred_loop);
241 static void free_loop_edges(morgan_env_t *env) {
244 for(l_attr = set_first(env->loop_attr_set); l_attr != NULL; l_attr = set_next(env->loop_attr_set)) {
245 del_set(l_attr->out_edges);
246 del_set(l_attr->in_edges);
252 * Debugging help, shows all nodes in a (node-)bitset
254 static void show_nodebitset(ir_graph* irg, const bitset_t* bitset) {
257 bitset_foreach(bitset, i) {
258 ir_node* node = get_idx_irn(irg, i);
259 ir_fprintf(stderr, " %+F", node);
261 fprintf(stderr, "\n");
265 static INLINE void init_livethrough_unuseds(block_attr_t *attr, morgan_env_t *env) {
266 const ir_node *block;
268 const be_lv_t *lv = env->lv;
270 if(attr->livethrough_unused != NULL)
275 attr->livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
277 // copy all live-outs into the livethrough_unused set
278 be_lv_foreach(lv, block, be_lv_state_in | be_lv_state_out, i) {
279 ir_node *irn = be_lv_get_irn(lv, block, i);
282 if(!consider_for_spilling(env->arch, env->cls, irn))
285 node_idx = get_irn_idx(irn);
286 bitset_set(attr->livethrough_unused, node_idx);
291 * Construct the livethrough unused set for a block
293 static void construct_block_livethrough_unused(ir_node *block, void *data) {
294 morgan_env_t* env = data;
295 block_attr_t *block_attr = get_block_attr(env, block);
298 block_attr_t **pred_attrs = NULL;
301 init_livethrough_unuseds(block_attr, env);
303 DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block)));
305 n_cfgpreds = get_Block_n_cfgpreds(block);
307 pred_attrs = alloca(sizeof(pred_attrs[0]) * n_cfgpreds);
308 for(i = 0; i < n_cfgpreds; ++i) {
309 ir_node *pred_block = get_Block_cfgpred_block(block, i);
310 pred_attrs[i] = get_block_attr(env, pred_block);
311 init_livethrough_unuseds(pred_attrs[i], env);
316 * All values that are used within the block are not unused (and therefore not
317 * livethrough_unused)
319 * TODO FIXME use block out edges and not schedule to find uses
321 panic("needs fixing");
322 sched_foreach(block, node) {
325 // phis are really uses in the pred block
328 for(j = 0; j < n_cfgpreds; ++j) {
329 ir_node *used_value = get_Phi_pred(node, j);
330 int idx = get_irn_idx(used_value);
331 block_attr_t *pred_attr = pred_attrs[j];
333 bitset_clear(pred_attr->livethrough_unused, idx);
336 // mark all used values as used
337 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
338 int idx = get_irn_idx(get_irn_n(node, i));
339 bitset_clear(block_attr->livethrough_unused, idx);
346 * Construct the livethrough unused set for a loop (and all its subloops+blocks)
348 static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, const ir_loop *loop) {
350 loop_attr_t* loop_attr = get_loop_attr(env, loop);
352 DBG((dbg, DBG_LIVE, "Processing Loop %d\n", loop->loop_nr));
353 assert(get_loop_n_elements(loop) > 0);
354 for(i = 0; i < get_loop_n_elements(loop); ++i) {
355 loop_element elem = get_loop_element(loop, i);
356 switch (*elem.kind) {
358 ir_node *block = elem.node;
359 block_attr_t *block_attr = get_block_attr(env, block);
360 bitset_t *livethrough_block_unused = block_attr->livethrough_unused;
362 assert(is_Block(elem.node));
363 assert(livethrough_block_unused != NULL);
366 bitset_copy(loop_attr->livethrough_unused, livethrough_block_unused);
368 bitset_and(loop_attr->livethrough_unused, livethrough_block_unused);
373 bitset_t *livethrough_son_unused;
375 livethrough_son_unused = construct_loop_livethrough_unused(env, elem.son);
377 bitset_copy(loop_attr->livethrough_unused, livethrough_son_unused);
379 bitset_and(loop_attr->livethrough_unused, livethrough_son_unused);
388 DBG((dbg, DBG_LIVE, "Done with loop %d\n", loop->loop_nr));
390 // remove all unused livethroughs that are remembered for this loop from child loops and blocks
391 for(i = 0; i < get_loop_n_elements(loop); ++i) {
392 const loop_element elem = get_loop_element(loop, i);
394 if(*elem.kind == k_ir_loop) {
395 loop_attr_t *son_attr = get_loop_attr(env, elem.son);
396 bitset_andnot(son_attr->livethrough_unused, loop_attr->livethrough_unused);
398 DBG((dbg, DBG_LIVE, "Livethroughs for loop %d:\n", loop->loop_nr));
399 } else if(*elem.kind == k_ir_node) {
400 block_attr_t *block_attr = get_block_attr(env, elem.node);
401 bitset_andnot(block_attr->livethrough_unused, loop_attr->livethrough_unused);
403 DBG((dbg, DBG_LIVE, "Livethroughs for block %+F\n", elem.node));
409 return loop_attr->livethrough_unused;
412 /*---------------------------------------------------------------------------*/
414 typedef struct _spillcandidate_t {
419 static int compare_spillcandidates(const void *d1, const void *d2) {
420 const spillcandidate_t *cand1 = d1;
421 const spillcandidate_t *cand2 = d2;
423 return cand1->cost - cand2->cost;
426 static void spill_values(morgan_env_t *env, const loop_attr_t *loop_attr, int spills) {
427 const bitset_t *cand_bitset = loop_attr->livethrough_unused;
428 int candidatecount = bitset_popcnt(cand_bitset);
429 spillcandidate_t *candidates;
434 assert(spills <= candidatecount);
436 candidates = alloca(sizeof(candidates[0]) * candidatecount);
438 DBG((dbg, DBG_CHOOSE, "Candidates for loop %d\n", get_loop_loop_nr(loop_attr->loop)));
439 // build candidiatelist
441 bitset_foreach(cand_bitset, idx) {
442 ir_node *node = get_idx_irn(env->irg, idx);
443 candidates[c].node = node;
444 candidates[c].cost = 0;
446 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
447 candidates[c].cost += be_get_reload_costs_on_edge(env->senv, node, edge->block, edge->pos);
449 DBG((dbg, DBG_CHOOSE, "%+F has costs %d\n", node, candidates[c].cost));
453 assert(c == candidatecount);
456 qsort(candidates, candidatecount, sizeof(candidates[0]), compare_spillcandidates);
459 for(i = 0; i < spills; ++i) {
460 ir_node *to_spill = candidates[i].node;
461 DBG((dbg, DBG_CHOOSE, "Spilling %+F ", to_spill));
463 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
464 be_add_reload_on_edge(env->senv, to_spill, edge->block, edge->pos, env->cls, 1);
469 static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* block, int loop_unused_spills_possible) {
472 int loop_unused_spills_needed;
473 ir_nodeset_t live_nodes;
474 const be_lv_t *lv = env->lv;
476 ir_nodeset_init(&live_nodes);
478 be_liveness_end_of_block(lv, env->arch, env->cls, block, &live_nodes);
479 max_pressure = ir_nodeset_size(&live_nodes);
481 DBG((dbg, DBG_LIVE, "Reduce pressure to %d In Block %+F:\n", env->registers_available, block));
484 * Determine register pressure in block
486 sched_foreach_reverse(block, node) {
492 be_liveness_transfer(env->arch, env->cls, node, &live_nodes);
493 pressure = ir_nodeset_size(&live_nodes);
494 if(pressure > max_pressure)
495 max_pressure = pressure;
497 ir_nodeset_destroy(&live_nodes);
499 loop_unused_spills_needed = max_pressure - env->registers_available;
501 if(loop_unused_spills_needed < 0) {
502 loop_unused_spills_needed = 0;
503 } else if(loop_unused_spills_needed > loop_unused_spills_possible) {
504 loop_unused_spills_needed = loop_unused_spills_possible;
507 DBG((dbg, DBG_PRESSURE, "Block %+F: max-pressure %d spills possible: %d spills used: %d\n",
508 block, max_pressure, loop_unused_spills_possible, loop_unused_spills_needed));
509 return loop_unused_spills_needed;
513 * Reduce register pressure in a loop
515 * @param unused_spills_possible Number of spills from livethrough_unused variables possible in outer loops
516 * @return Number of spills of livethrough_unused variables needed in outer loops
518 static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *loop, int outer_spills_possible) {
520 loop_attr_t* loop_attr = get_loop_attr(env, loop);
521 int spills_needed = 0;
522 int spills_possible = outer_spills_possible + bitset_popcnt(loop_attr->livethrough_unused);
523 int outer_spills_needed;
525 DBG((dbg, DBG_PRESSURE, "Reducing Pressure in loop %d\n", loop->loop_nr));
526 for(i = 0; i < get_loop_n_elements(loop); ++i) {
527 loop_element elem = get_loop_element(loop, i);
528 switch (*elem.kind) {
531 assert(is_Block(elem.node));
532 needed = reduce_register_pressure_in_block(env, elem.node, spills_possible);
534 assert(needed <= spills_possible);
535 if(needed > spills_needed)
536 spills_needed = needed;
540 int needed = reduce_register_pressure_in_loop(env, elem.son, spills_possible);
542 assert(needed <= spills_possible);
543 if(needed > spills_needed)
544 spills_needed = needed;
553 /* calculate number of spills needed in outer loop and spill
554 * unused livethrough nodes around this loop */
555 if(spills_needed > outer_spills_possible) {
557 outer_spills_needed = outer_spills_possible;
558 spills_needed -= outer_spills_possible;
560 spills_to_place = spills_needed;
562 DBG((dbg, DBG_SPILLS, "%d values unused in loop %d, spilling %d\n",
563 spills_possible - outer_spills_possible, loop->loop_nr, spills_to_place));
565 spill_values(env, loop_attr, spills_to_place);
567 outer_spills_needed = spills_needed;
570 return outer_spills_needed;
573 void be_spill_morgan(be_irg_t *birg, const arch_register_class_t *cls) {
574 ir_graph *irg = be_get_birg_irg(birg);
577 be_liveness_assure_sets(be_assure_liveness(birg));
579 env.arch = birg->main_env->arch_env;
582 env.lv = be_get_birg_liveness(birg);
583 env.senv = be_new_spill_env(birg);
585 obstack_init(&env.obst);
587 env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
589 env.loop_attr_set = new_set(loop_attr_cmp, 5);
590 env.block_attr_set = new_set(block_attr_cmp, 20);
592 /* -- Part1: Analysis -- */
594 /* construct control flow loop tree */
595 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
596 construct_cf_backedges(irg);
599 /* construct loop out edges and livethrough_unused sets for loops and blocks */
600 irg_block_walk_graph(irg, construct_block_livethrough_unused, construct_loop_edges, &env);
601 construct_loop_livethrough_unused(&env, get_irg_loop(irg));
603 /* -- Part2: Transformation -- */
605 /* spill unused livethrough values around loops and blocks where
606 * the pressure is too high
608 reduce_register_pressure_in_loop(&env, get_irg_loop(irg), 0);
610 /* Insert real spill/reload nodes and fix usages */
611 be_insert_spills_reloads(env.senv);
613 /* Verify the result */
614 if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
615 be_verify_schedule(birg);
616 } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
617 assert(be_verify_schedule(birg));
621 free_loop_edges(&env);
622 del_set(env.loop_attr_set);
623 del_set(env.block_attr_set);
625 /* fix the remaining places with too high register pressure with beladies algorithm */
626 be_spill_belady_spill_env(birg, cls, env.senv);
628 be_delete_spill_env(env.senv);
629 obstack_free(&env.obst, NULL);
632 void be_init_spillmorgan(void)
634 static be_spiller_t morgan_spiller = {
638 be_register_spiller("morgan", &morgan_spiller);
639 FIRM_DBG_REGISTER(dbg, "ir.be.spillmorgan");
642 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillmorgan);