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"
37 #include "bespillmorgan.h"
38 #include "bechordal_t.h"
42 #include "bespillbelady.h"
44 #include "benodesets.h"
45 #include "bespilloptions.h"
53 #define DBG_PRESSURE 4
56 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
58 typedef struct morgan_env {
59 const arch_env_t *arch;
60 const arch_register_class_t *cls;
64 /** maximum safe register pressure */
65 int registers_available;
73 typedef struct loop_edge {
78 typedef struct loop_attr {
82 /** The set of all values that are live in the loop but not used in the loop */
83 bitset_t *livethrough_unused;
86 typedef struct morgan_block_attr {
88 /** set of all values that are live in the block but not used in the block */
89 bitset_t *livethrough_unused;
92 //---------------------------------------------------------------------------
94 static int loop_edge_cmp(const void* p1, const void* p2, size_t size) {
95 loop_edge_t *e1 = (loop_edge_t*) p1;
96 loop_edge_t *e2 = (loop_edge_t*) p2;
99 return e1->block != e2->block || e1->pos != e2->pos;
102 static int loop_attr_cmp(const void *e1, const void *e2, size_t size) {
103 loop_attr_t *la1 = (loop_attr_t*) e1;
104 loop_attr_t *la2 = (loop_attr_t*) e2;
107 return la1->loop != la2->loop;
110 static int block_attr_cmp(const void *e1, const void *e2, size_t size) {
111 block_attr_t *b1 = (block_attr_t*) e1;
112 block_attr_t *b2 = (block_attr_t*) e2;
115 return b1->block != b2->block;
118 static INLINE int loop_attr_hash(const loop_attr_t *a) {
120 return a->loop->loop_nr;
122 return HASH_PTR(a->loop);
126 static INLINE int block_attr_hash(const block_attr_t *b) {
127 return nodeset_hash(b->block);
130 static INLINE int loop_edge_hash(const loop_edge_t *e) {
131 return nodeset_hash(e->block) ^ (e->pos * 31);
134 static INLINE loop_attr_t *get_loop_attr(morgan_env_t *env, const ir_loop *loop) {
135 loop_attr_t l_attr, *res;
139 hash = loop_attr_hash(&l_attr);
140 res = set_find(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
142 // create new loop_attr if none exists yet
144 l_attr.out_edges = new_set(loop_edge_cmp, 1);
145 l_attr.in_edges = new_set(loop_edge_cmp, 1);
146 l_attr.livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
147 res = set_insert(env->loop_attr_set, &l_attr, sizeof(l_attr), hash);
153 static INLINE block_attr_t *get_block_attr(morgan_env_t *env, const ir_node *block) {
154 block_attr_t b_attr, *res;
156 b_attr.block = block;
158 hash = block_attr_hash(&b_attr);
159 res = set_find(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
162 b_attr.livethrough_unused = NULL;
163 res = set_insert(env->block_attr_set, &b_attr, sizeof(b_attr), hash);
169 //---------------------------------------------------------------------------
171 static INLINE int consider_for_spilling(const arch_env_t *env, const arch_register_class_t *cls, const ir_node *node) {
172 if(!arch_irn_has_reg_class(env, node, -1, cls))
175 return !(arch_irn_get_flags(env, node) & (arch_irn_flags_ignore | arch_irn_flags_dont_spill));
179 * Determine edges going out of a loop (= edges that go to a block that is not
180 * inside the loop or one of its subloops)
182 static INLINE void construct_loop_edges(ir_node *block, void *data) {
183 morgan_env_t *env = data;
184 int n_cfgpreds = get_Block_n_cfgpreds(block);
186 ir_loop* loop = get_irn_loop(block);
187 DBG((dbg, DBG_LOOPANA, "Loop for %+F: %d (depth %d)\n", block, loop->loop_nr, loop->depth));
189 for(i = 0; i < n_cfgpreds; ++i) {
192 ir_node* cfgpred = get_Block_cfgpred(block, i);
193 ir_node* cfgpred_block = get_nodes_block(cfgpred);
194 ir_loop* cfgpred_loop = get_irn_loop(cfgpred_block);
196 if(cfgpred_loop == loop)
199 assert(get_loop_depth(cfgpred_loop) != get_loop_depth(loop));
203 hash = loop_edge_hash(&edge);
205 // edge out of a loop?
206 if(get_loop_depth(cfgpred_loop) > get_loop_depth(loop)) {
209 DBG((dbg, DBG_LOOPANA, "Loop out edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
210 cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
212 /* this might be a jump out of multiple loops, so add this to all
213 * needed outedge sets */
216 loop_attr_t *l_attr = get_loop_attr(env, l);
217 set_insert(l_attr->out_edges, &edge, sizeof(edge), hash);
219 l = get_loop_outer_loop(l);
226 DBG((dbg, DBG_LOOPANA, "Loop in edge from %+F (loop %d) to %+F (loop %d)\n", block, get_loop_loop_nr(loop),
227 cfgpred_block, get_loop_loop_nr(cfgpred_loop)));
231 loop_attr_t *l_attr = get_loop_attr(env, l);
232 set_insert(l_attr->in_edges, &edge, sizeof(edge), hash);
234 l = get_loop_outer_loop(l);
235 } while(l != cfgpred_loop);
240 static void free_loop_edges(morgan_env_t *env) {
243 for(l_attr = set_first(env->loop_attr_set); l_attr != NULL; l_attr = set_next(env->loop_attr_set)) {
244 del_set(l_attr->out_edges);
245 del_set(l_attr->in_edges);
251 * Debugging help, shows all nodes in a (node-)bitset
253 static void show_nodebitset(ir_graph* irg, const bitset_t* bitset) {
256 bitset_foreach(bitset, i) {
257 ir_node* node = get_idx_irn(irg, i);
258 ir_fprintf(stderr, " %+F", node);
260 fprintf(stderr, "\n");
264 static INLINE void init_livethrough_unuseds(block_attr_t *attr, morgan_env_t *env) {
265 const ir_node *block;
267 const be_lv_t *lv = env->lv;
269 if(attr->livethrough_unused != NULL)
274 attr->livethrough_unused = bitset_obstack_alloc(&env->obst, get_irg_last_idx(env->irg));
276 // copy all live-outs into the livethrough_unused set
277 be_lv_foreach(lv, block, be_lv_state_in | be_lv_state_out, i) {
278 ir_node *irn = be_lv_get_irn(lv, block, i);
281 if(!consider_for_spilling(env->arch, env->cls, irn))
284 node_idx = get_irn_idx(irn);
285 bitset_set(attr->livethrough_unused, node_idx);
290 * Construct the livethrough unused set for a block
292 static void construct_block_livethrough_unused(ir_node *block, void *data) {
293 morgan_env_t* env = data;
294 block_attr_t *block_attr = get_block_attr(env, block);
297 block_attr_t **pred_attrs = NULL;
300 init_livethrough_unuseds(block_attr, env);
302 DBG((dbg, DBG_LIVE, "Processing block %d\n", get_irn_node_nr(block)));
304 n_cfgpreds = get_Block_n_cfgpreds(block);
306 pred_attrs = alloca(sizeof(pred_attrs[0]) * n_cfgpreds);
307 for(i = 0; i < n_cfgpreds; ++i) {
308 ir_node *pred_block = get_Block_cfgpred_block(block, i);
309 pred_attrs[i] = get_block_attr(env, pred_block);
310 init_livethrough_unuseds(pred_attrs[i], env);
315 * All values that are used within the block are not unused (and therefore not
316 * livethrough_unused)
318 sched_foreach(block, node) {
321 // phis are really uses in the pred block
324 for(j = 0; j < n_cfgpreds; ++j) {
325 ir_node *used_value = get_Phi_pred(node, j);
326 int idx = get_irn_idx(used_value);
327 block_attr_t *pred_attr = pred_attrs[j];
329 bitset_clear(pred_attr->livethrough_unused, idx);
332 // mark all used values as used
333 for(i = 0, arity = get_irn_arity(node); i < arity; ++i) {
334 int idx = get_irn_idx(get_irn_n(node, i));
335 bitset_clear(block_attr->livethrough_unused, idx);
342 * Construct the livethrough unused set for a loop (and all its subloops+blocks)
344 static bitset_t *construct_loop_livethrough_unused(morgan_env_t *env, const ir_loop *loop) {
346 loop_attr_t* loop_attr = get_loop_attr(env, loop);
348 DBG((dbg, DBG_LIVE, "Processing Loop %d\n", loop->loop_nr));
349 assert(get_loop_n_elements(loop) > 0);
350 for(i = 0; i < get_loop_n_elements(loop); ++i) {
351 loop_element elem = get_loop_element(loop, i);
352 switch (*elem.kind) {
354 ir_node *block = elem.node;
355 block_attr_t *block_attr = get_block_attr(env, block);
356 bitset_t *livethrough_block_unused = block_attr->livethrough_unused;
358 assert(is_Block(elem.node));
359 assert(livethrough_block_unused != NULL);
362 bitset_copy(loop_attr->livethrough_unused, livethrough_block_unused);
364 bitset_and(loop_attr->livethrough_unused, livethrough_block_unused);
369 bitset_t *livethrough_son_unused;
371 livethrough_son_unused = construct_loop_livethrough_unused(env, elem.son);
373 bitset_copy(loop_attr->livethrough_unused, livethrough_son_unused);
375 bitset_and(loop_attr->livethrough_unused, livethrough_son_unused);
384 DBG((dbg, DBG_LIVE, "Done with loop %d\n", loop->loop_nr));
386 // remove all unused livethroughs that are remembered for this loop from child loops and blocks
387 for(i = 0; i < get_loop_n_elements(loop); ++i) {
388 const loop_element elem = get_loop_element(loop, i);
390 if(*elem.kind == k_ir_loop) {
391 loop_attr_t *son_attr = get_loop_attr(env, elem.son);
392 bitset_andnot(son_attr->livethrough_unused, loop_attr->livethrough_unused);
394 DBG((dbg, DBG_LIVE, "Livethroughs for loop %d:\n", loop->loop_nr));
395 } else if(*elem.kind == k_ir_node) {
396 block_attr_t *block_attr = get_block_attr(env, elem.node);
397 bitset_andnot(block_attr->livethrough_unused, loop_attr->livethrough_unused);
399 DBG((dbg, DBG_LIVE, "Livethroughs for block %+F\n", elem.node));
405 return loop_attr->livethrough_unused;
408 /*---------------------------------------------------------------------------*/
410 typedef struct _spillcandidate_t {
415 static int compare_spillcandidates(const void *d1, const void *d2) {
416 const spillcandidate_t *cand1 = d1;
417 const spillcandidate_t *cand2 = d2;
419 return cand1->cost - cand2->cost;
422 static void spill_values(morgan_env_t *env, const loop_attr_t *loop_attr, int spills) {
423 const bitset_t *cand_bitset = loop_attr->livethrough_unused;
424 int candidatecount = bitset_popcnt(cand_bitset);
425 spillcandidate_t *candidates;
430 assert(spills <= candidatecount);
432 candidates = alloca(sizeof(candidates[0]) * candidatecount);
434 DBG((dbg, DBG_CHOOSE, "Candidates for loop %d\n", get_loop_loop_nr(loop_attr->loop)));
435 // build candidiatelist
437 bitset_foreach(cand_bitset, idx) {
438 ir_node *node = get_idx_irn(env->irg, idx);
439 candidates[c].node = node;
440 candidates[c].cost = 0;
442 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
443 candidates[c].cost += be_get_reload_costs_on_edge(env->senv, node, edge->block, edge->pos);
445 DBG((dbg, DBG_CHOOSE, "%+F has costs %d\n", node, candidates[c].cost));
449 assert(c == candidatecount);
452 qsort(candidates, candidatecount, sizeof(candidates[0]), compare_spillcandidates);
455 for(i = 0; i < spills; ++i) {
456 ir_node *to_spill = candidates[i].node;
457 DBG((dbg, DBG_CHOOSE, "Spilling %+F ", to_spill));
459 for(edge = set_first(loop_attr->out_edges); edge != NULL; edge = set_next(loop_attr->out_edges)) {
460 be_add_reload_on_edge(env->senv, to_spill, edge->block, edge->pos, env->cls, 1);
465 static int reduce_register_pressure_in_block(morgan_env_t *env, const ir_node* block, int loop_unused_spills_possible) {
468 int loop_unused_spills_needed;
469 ir_nodeset_t live_nodes;
470 const be_lv_t *lv = env->lv;
472 ir_nodeset_init(&live_nodes);
474 be_liveness_end_of_block(lv, env->arch, env->cls, block, &live_nodes);
475 max_pressure = ir_nodeset_size(&live_nodes);
477 DBG((dbg, DBG_LIVE, "Reduce pressure to %d In Block %+F:\n", env->registers_available, block));
480 * Determine register pressure in block
482 sched_foreach_reverse(block, node) {
488 be_liveness_transfer(env->arch, env->cls, node, &live_nodes);
489 pressure = ir_nodeset_size(&live_nodes);
490 if(pressure > max_pressure)
491 max_pressure = pressure;
493 ir_nodeset_destroy(&live_nodes);
495 loop_unused_spills_needed = max_pressure - env->registers_available;
497 if(loop_unused_spills_needed < 0) {
498 loop_unused_spills_needed = 0;
499 } else if(loop_unused_spills_needed > loop_unused_spills_possible) {
500 loop_unused_spills_needed = loop_unused_spills_possible;
503 DBG((dbg, DBG_PRESSURE, "Block %+F: max-pressure %d spills possible: %d spills used: %d\n",
504 block, max_pressure, loop_unused_spills_possible, loop_unused_spills_needed));
505 return loop_unused_spills_needed;
509 * Reduce register pressure in a loop
511 * @param unused_spills_possible Number of spills from livethrough_unused variables possible in outer loops
512 * @return Number of spills of livethrough_unused variables needed in outer loops
514 static int reduce_register_pressure_in_loop(morgan_env_t *env, const ir_loop *loop, int outer_spills_possible) {
516 loop_attr_t* loop_attr = get_loop_attr(env, loop);
517 int spills_needed = 0;
518 int spills_possible = outer_spills_possible + bitset_popcnt(loop_attr->livethrough_unused);
519 int outer_spills_needed;
521 DBG((dbg, DBG_PRESSURE, "Reducing Pressure in loop %d\n", loop->loop_nr));
522 for(i = 0; i < get_loop_n_elements(loop); ++i) {
523 loop_element elem = get_loop_element(loop, i);
524 switch (*elem.kind) {
527 assert(is_Block(elem.node));
528 needed = reduce_register_pressure_in_block(env, elem.node, spills_possible);
530 assert(needed <= spills_possible);
531 if(needed > spills_needed)
532 spills_needed = needed;
536 int needed = reduce_register_pressure_in_loop(env, elem.son, spills_possible);
538 assert(needed <= spills_possible);
539 if(needed > spills_needed)
540 spills_needed = needed;
549 /* calculate number of spills needed in outer loop and spill
550 * unused livethrough nodes around this loop */
551 if(spills_needed > outer_spills_possible) {
553 outer_spills_needed = outer_spills_possible;
554 spills_needed -= outer_spills_possible;
556 spills_to_place = spills_needed;
558 DBG((dbg, DBG_SPILLS, "%d values unused in loop %d, spilling %d\n",
559 spills_possible - outer_spills_possible, loop->loop_nr, spills_to_place));
561 spill_values(env, loop_attr, spills_to_place);
563 outer_spills_needed = spills_needed;
566 return outer_spills_needed;
569 void be_spill_morgan(be_irg_t *birg, const arch_register_class_t *cls) {
570 ir_graph *irg = be_get_birg_irg(birg);
573 be_liveness_assure_sets(be_assure_liveness(birg));
575 env.arch = birg->main_env->arch_env;
578 env.lv = be_get_birg_liveness(birg);
579 env.senv = be_new_spill_env(birg);
581 obstack_init(&env.obst);
583 env.registers_available = env.cls->n_regs - be_put_ignore_regs(birg, env.cls, NULL);
585 env.loop_attr_set = new_set(loop_attr_cmp, 5);
586 env.block_attr_set = new_set(block_attr_cmp, 20);
588 /* -- Part1: Analysis -- */
590 /* construct control flow loop tree */
591 if (! (get_irg_loopinfo_state(irg) & loopinfo_cf_consistent)) {
592 construct_cf_backedges(irg);
595 /* construct loop out edges and livethrough_unused sets for loops and blocks */
596 irg_block_walk_graph(irg, construct_block_livethrough_unused, construct_loop_edges, &env);
597 construct_loop_livethrough_unused(&env, get_irg_loop(irg));
599 /* -- Part2: Transformation -- */
601 /* spill unused livethrough values around loops and blocks where
602 * the pressure is too high
604 reduce_register_pressure_in_loop(&env, get_irg_loop(irg), 0);
606 /* Insert real spill/reload nodes and fix usages */
607 be_insert_spills_reloads(env.senv);
609 /* Verify the result */
610 if (birg->main_env->options->vrfy_option == BE_VRFY_WARN) {
611 be_verify_schedule(birg);
612 } else if (birg->main_env->options->vrfy_option == BE_VRFY_ASSERT) {
613 assert(be_verify_schedule(birg));
617 free_loop_edges(&env);
618 del_set(env.loop_attr_set);
619 del_set(env.block_attr_set);
621 /* fix the remaining places with too high register pressure with beladies algorithm */
622 be_spill_belady_spill_env(birg, cls, env.senv);
624 be_delete_spill_env(env.senv);
625 obstack_free(&env.obst, NULL);
628 void be_init_spillmorgan(void)
630 static be_spiller_t morgan_spiller = {
634 be_register_spiller("morgan", &morgan_spiller);
635 FIRM_DBG_REGISTER(dbg, "ir.be.spillmorgan");
638 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillmorgan);