2 * Author: Daniel Grund, Sebastian Hack
4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #include "iredges_t.h"
26 #include "besched_t.h"
29 #include "bechordal_t.h"
32 /* This enables re-computation of values. Current state: Unfinished and buggy. */
35 typedef struct _reloader_t reloader_t;
36 typedef struct _spill_info_t spill_info_t;
43 struct _spill_info_t {
44 ir_node *spilled_node;
45 reloader_t *reloaders;
48 typedef struct _spill_ctx_t {
49 ir_node *spilled; /**< The spilled node. */
50 ir_node *user; /**< The node this spill is for. */
51 ir_node *spill; /**< The spill itself. */
55 const arch_register_class_t *cls;
56 const be_chordal_env_t *chordal_env;
59 set *spills; /**< all spill_info_t's, which must be placed */
60 pset *mem_phis; /**< set of all special spilled phis. allocated and freed separately */
61 decide_irn_t is_mem_phi; /**< callback func to decide if a phi needs special spilling */
62 void *data; /**< data passed to all callbacks */
63 DEBUG_ONLY(firm_dbg_module_t *dbg;)
66 /* associated Phi -> Spill*/
67 typedef struct _phi_spill_assoc_t {
73 * Compare two Phi->Spill associations.
75 static int cmp_phi_spill_assoc(const void *a, const void *b) {
76 const phi_spill_assoc_t *p1 = a;
77 const phi_spill_assoc_t *p2 = b;
78 return p1->phi != p2->phi;
82 * compare two spill contexts.
84 static int cmp_spillctx(const void *a, const void *b, size_t n) {
85 const spill_ctx_t *p = a;
86 const spill_ctx_t *q = b;
87 return p->user != q->user || p->spilled != q->spilled;
91 * Compare two spill infos.
93 static int cmp_spillinfo(const void *x, const void *y, size_t size) {
94 const spill_info_t *xx = x;
95 const spill_info_t *yy = y;
96 return xx->spilled_node != yy->spilled_node;
100 /* Sets the debug module of a spill environment. */
101 void be_set_spill_env_dbg_module(spill_env_t *env, firm_dbg_module_t *dbg) {
106 /* Creates a new spill environment. */
107 spill_env_t *be_new_spill_env(const be_chordal_env_t *chordal_env, decide_irn_t is_mem_phi, void *data) {
108 spill_env_t *env = xmalloc(sizeof(env[0]));
109 env->spill_ctxs = new_set(cmp_spillctx, 1024);
110 env->spills = new_set(cmp_spillinfo, 1024);
111 env->cls = chordal_env->cls;
112 env->is_mem_phi = is_mem_phi;
114 env->chordal_env = chordal_env;
115 obstack_init(&env->obst);
119 /* Deletes a spill environment. */
120 void be_delete_spill_env(spill_env_t *senv) {
121 del_set(senv->spill_ctxs);
122 del_set(senv->spills);
123 obstack_free(&senv->obst, NULL);
128 * Returns a spill context. If the context did not exists, create one.
130 * @param sc the set containing all spill contexts
131 * @param to_spill the node that should be spilled
132 * @param ctx_irn an user of the spilled node
134 * @return a spill context.
136 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
139 templ.spilled = to_spill;
140 templ.user = ctx_irn;
143 return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
149 * @param senv the spill environment
150 * @param irn the node that should be spilled
151 * @param ctx_irn an user of the spilled node
153 * @return a be_Spill node
155 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
157 DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", irn, ctx_irn));
159 ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
161 const be_main_env_t *env = senv->chordal_env->birg->main_env;
162 ctx->spill = be_spill(env->arch_env, irn, ctx_irn);
169 * If the first usage of a Phi result would be out of memory
170 * there is no sense in allocating a register for it.
171 * Thus we spill it and all its operands to the same spill slot.
172 * Therefore the phi/dataB becomes a phi/Memory
174 * @param senv the spill environment
175 * @param phi the Phi node that should be spilled
176 * @param ctx_irn an user of the spilled node
178 * @return a be_Spill node
180 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn, unsigned visited_nr, set *already_visited_phis) {
181 int i, n = get_irn_arity(phi);
182 ir_graph *irg = senv->chordal_env->irg;
183 ir_mode *phi_mode = get_irn_mode(phi);
184 ir_node *bl = get_nodes_block(phi);
185 ir_node **ins, *phi_spill;
186 phi_spill_assoc_t key;
190 DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", phi, ctx_irn));
192 /* build a new PhiM */
193 NEW_ARR_A(ir_node *, ins, n);
194 for (i = 0; i < n; ++i) {
195 ins[i] = new_rd_Unknown(irg, phi_mode);
197 phi_spill = new_r_Phi(senv->chordal_env->irg, bl, n, ins, mode_M);
199 key.spill = phi_spill;
200 set_insert(already_visited_phis, &key, sizeof(key), HASH_PTR(phi));
202 /* search an existing spill for this context */
203 ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
205 /* if not found spill the phi */
207 set_irn_visited(phi, visited_nr);
209 /* collect all arguments of the phi */
210 for (i = 0; i < n; ++i) {
211 ir_node *arg = get_irn_n(phi, i);
213 phi_spill_assoc_t *entry;
215 if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg)) {
216 if (get_irn_visited(arg) < visited_nr)
217 sub_res = be_spill_phi(senv, arg, ctx_irn, visited_nr, already_visited_phis);
219 /* we already visited the argument phi: get it's spill */
222 entry = set_find(already_visited_phis, &key, sizeof(key), HASH_PTR(arg));
223 assert(entry && "argument phi already visited, but no spill found?!?");
224 sub_res = entry->spill;
225 assert(sub_res && "spill missing?!?");
229 sub_res = be_spill_irn(senv, arg, ctx_irn);
231 set_irn_n(phi_spill, i, sub_res);
234 ctx->spill = phi_spill;
242 * @param senv the spill environment
243 * @param irn the node that should be spilled
244 * @param ctx_irn an user of the spilled node
246 * @return a be_Spill node
248 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill, unsigned visited_nr) {
249 ir_graph *irg = get_irn_irg(to_spill);
250 int save_optimize = get_optimize();
251 int save_normalize = get_opt_normalize();
252 pset *already_visited_phis = new_set(cmp_phi_spill_assoc, 10);
256 * Disable optimization so that the phi functions do not
260 set_opt_normalize(0);
262 if (pset_find_ptr(senv->mem_phis, to_spill))
263 res = be_spill_phi(senv, to_spill, to_spill, visited_nr, already_visited_phis);
265 res = be_spill_irn(senv, to_spill, to_spill);
267 del_set(already_visited_phis);
269 /* reset the optimizations */
270 set_optimize(save_optimize);
271 set_opt_normalize(save_normalize);
281 * Check if a spilled node could be rematerialized.
283 * @param senv the spill environment
284 * @param spill the Spill node
285 * @param spilled the node that was spilled
286 * @param reloader a irn that requires a reload
288 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
291 /* check for 'normal' spill and general remat condition */
292 if (!be_is_Spill(spill) || !arch_irn_is(senv->chordal_env->birg->main_env->arch_env, spilled, rematerializable))
295 /* check availability of original arguments */
296 if (is_Block(reloader)) {
298 /* we want to remat at the end of a block.
299 * thus all arguments must be alive at the end of the block
301 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
302 ir_node *arg = get_irn_n(spilled, pos);
303 if (!is_live_end(reloader, arg))
309 /* we want to remat before the insn reloader
310 * thus an arguments is alive if
311 * - it interferes with the reloaders result
313 * - or it is (last-) used by reloader itself
315 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
316 ir_node *arg = get_irn_n(spilled, pos);
319 if (values_interfere(reloader, arg))
322 for (i=0, m=get_irn_arity(reloader); i<m; ++i) {
323 ir_node *rel_arg = get_irn_n(reloader, i);
328 /* arg is not alive before reloader */
340 #else /* BUGGY_REMAT */
343 * A very simple rematerialization checker.
345 * @param senv the spill environment
346 * @param spill the Spill node
347 * @param spilled the node that was spilled
348 * @param reloader a irn that requires a reload
350 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
351 const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
353 return get_irn_arity(spilled) == 0 &&
354 be_is_Spill(spill) &&
355 arch_irn_is(aenv, spilled, rematerializable);
358 #endif /* BUGGY_REMAT */
363 * Re-materialize a node.
365 * @param senv the spill environment
366 * @param spilled the node that was spilled
367 * @param reloader a irn that requires a reload
369 static ir_node *do_remat(spill_env_t *senv, ir_node *spilled, ir_node *reloader) {
371 ir_node *bl = (is_Block(reloader)) ? reloader : get_nodes_block(reloader);
373 /* recompute the value */
374 res = new_ir_node(get_irn_dbg_info(spilled), senv->chordal_env->irg, bl,
376 get_irn_mode(spilled),
377 get_irn_arity(spilled),
378 get_irn_in(spilled) + 1);
379 copy_node_attr(spilled, res);
381 DBG((senv->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
383 /* insert in schedule */
384 if (is_Block(reloader)) {
385 ir_node *insert = sched_skip(reloader, 0, sched_skip_cf_predicator, (void *) senv->chordal_env->birg->main_env->arch_env);
386 sched_add_after(insert, res);
388 sched_add_before(reloader, res);
395 * Walker: fills the mem_phis set by evaluating Phi nodes
396 * using the is_mem_phi() callback.
398 static void phi_walker(ir_node *irn, void *env) {
399 spill_env_t *senv = env;
402 const arch_env_t *arch = senv->chordal_env->birg->main_env->arch_env;
403 if (arch_irn_has_reg_class(arch, irn, 0, senv->cls) &&
404 senv->is_mem_phi(irn, senv->data)) {
405 DBG((senv->dbg, LEVEL_1, " %+F\n", irn));
406 pset_insert_ptr(senv->mem_phis, irn);
411 void be_insert_spills_reloads(spill_env_t *senv, pset *reload_set) {
412 const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
413 ir_graph *irg = senv->chordal_env->irg;
419 /* get all special spilled phis */
420 DBG((senv->dbg, LEVEL_1, "Mem-phis:\n"));
421 senv->mem_phis = pset_new_ptr_default();
422 irg_walk_graph(senv->chordal_env->irg, phi_walker, NULL, senv);
424 /* Add reloads for mem_phis */
425 /* BETTER: These reloads (1) should only be inserted, if they are really needed */
426 DBG((senv->dbg, LEVEL_1, "Reloads for mem-phis:\n"));
427 for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
429 DBG((senv->dbg, LEVEL_1, " Mem-phi %+F\n", irn));
430 foreach_out_edge(irn, e) {
431 ir_node *user = e->src;
432 if (is_Phi(user) && !pset_find_ptr(senv->mem_phis, user)) {
433 ir_node *use_bl = get_nodes_block(user);
434 DBG((senv->dbg, LEVEL_1, " non-mem-phi user %+F\n", user));
435 be_add_reload_on_edge(senv, irn, use_bl, e->pos); /* (1) */
440 visited_nr = get_irg_visited(irg) + 1;
441 set_irg_visited(irg, visited_nr);
443 /* process each spilled node */
444 DBG((senv->dbg, LEVEL_1, "Insert spills and reloads:\n"));
445 possibly_dead = new_pdeq();
446 for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
448 ir_mode *mode = get_irn_mode(si->spilled_node);
449 pset *values = pset_new_ptr(16);
451 /* go through all reloads for this spill */
452 for(rld = si->reloaders; rld; rld = rld->next) {
455 /* the spill for this reloader */
456 ir_node *spill = be_spill_node(senv, si->spilled_node, visited_nr);
459 if (check_remat_conditions(senv, spill, si->spilled_node, rld->reloader)) {
460 new_val = do_remat(senv, si->spilled_node, rld->reloader);
461 pdeq_putl(possibly_dead, spill);
466 new_val = be_reload(aenv, senv->cls, rld->reloader, mode, spill);
468 DBG((senv->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
469 pset_insert_ptr(values, new_val);
471 pset_insert_ptr(reload_set, new_val);
474 /* introduce copies, rewire the uses */
475 assert(pset_count(values) > 0 && "???");
476 pset_insert_ptr(values, si->spilled_node);
477 be_ssa_constr_set_ignore(senv->chordal_env->dom_front, values, senv->mem_phis);
482 foreach_pset(senv->mem_phis, irn) {
484 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
485 pdeq_putl(possibly_dead, get_irn_n(irn, i));
486 set_irn_n(irn, i, new_r_Bad(senv->chordal_env->irg));
491 /* check if possibly dead nodes are really dead yet */
492 while (! pdeq_empty(possibly_dead)) {
493 ir_node *irn = pdeq_getr(possibly_dead);
494 const ir_edge_t *edge = get_irn_out_edge_first(irn);
498 for (i = get_irn_arity(irn) - 1; i >= 0; --i) {
499 pdeq_putl(possibly_dead, get_irn_n(irn, i));
500 set_irn_n(irn, i, new_r_Bad(senv->chordal_env->irg));
505 del_pdeq(possibly_dead);
506 del_pset(senv->mem_phis);
509 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
510 spill_info_t templ, *res;
513 templ.spilled_node = to_spill;
514 templ.reloaders = NULL;
515 res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
517 rel = obstack_alloc(&senv->obst, sizeof(rel[0]));
518 rel->reloader = before;
519 rel->next = res->reloaders;
520 res->reloaders = rel;
523 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
524 ir_node *insert_bl = get_irn_arity(bl) == 1 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
525 be_add_reload(senv, to_spill, insert_bl);
530 /****************************************
532 SPILL SLOT MANAGEMENT AND OPTS
534 ****************************************/
536 typedef struct _spill_slot_t {
540 ir_mode *largest_mode; /* the mode of all members with largest size */
543 typedef struct _ss_env_t {
545 be_chordal_env_t *cenv;
546 pmap *slots; /* maps spill_contexts to spill_slots */
547 pmap *types; /* maps modes to types */
548 DEBUG_ONLY(firm_dbg_module_t *dbg;)
553 * Walker: compute the spill slots
555 static void compute_spill_slots_walker(ir_node *spill, void *env) {
556 ss_env_t *ssenv = env;
561 if (!be_is_Spill(spill))
564 /* check, if this spill is for a context already known */
565 ctx = be_get_Spill_context(spill);
566 entry = pmap_find(ssenv->slots, ctx);
569 struct _arch_env_t *arch_env = ssenv->cenv->birg->main_env->arch_env;
570 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
571 ir_mode *largest_mode = arch_register_class_mode(cls);
573 /* this is a new spill context */
574 ss = obstack_alloc(&ssenv->ob, sizeof(*ss));
575 ss->members = pset_new_ptr(8);
576 ss->largest_mode = largest_mode;
577 ss->size = get_mode_size_bytes(ss->largest_mode);
578 ss->align = arch_isa_get_reg_class_alignment(arch_env->isa, cls);
579 pmap_insert(ssenv->slots, ctx, ss);
581 /* values with the same spill_ctx must go into the same spill slot */
585 /* ugly mega assert :-) */
588 struct _arch_env_t *arch_env = ssenv->cenv->birg->main_env->arch_env;
589 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
590 int size = get_mode_size_bytes(arch_register_class_mode(cls));
591 assert(ss->size == size && "Different sizes for the same spill slot are not allowed.");
592 for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
593 /* use values_interfere here, because it uses the dominance check,
594 which does work for values in memory */
595 assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
601 pset_insert_ptr(ss->members, spill);
605 * qsort compare function, sort spill slots by size.
607 static int ss_sorter(const void *v1, const void *v2) {
608 const spill_slot_t **ss1 = v1;
609 const spill_slot_t **ss2 = v2;
610 return ((int) (*ss2)->size) - ((int) (*ss1)->size);
615 * This function should optimize the spill slots.
616 * - Coalescing of multiple slots
617 * - Ordering the slots
619 * Input slots are in @p ssenv->slots
620 * @p size The count of initial spill slots in @p ssenv->slots
621 * This also is the size of the preallocated array @p ass
623 * @return An array of spill slots @p ass in specific order
625 static void optimize_slots(ss_env_t *ssenv, int size, spill_slot_t *ass[]) {
626 int i, o, used_slots;
630 pmap_foreach(ssenv->slots, entr)
631 ass[i++] = entr->value;
633 /* Sort the array to minimize fragmentation and cache footprint.
634 Large slots come first */
635 qsort(ass, size, sizeof(ass[0]), ss_sorter);
637 /* For each spill slot:
638 - assign a new offset to this slot
639 - xor find another slot to coalesce with */
641 for (i=0; i<size; ++i) { /* for each spill slot */
645 DBG((ssenv->dbg, LEVEL_1, "Spill slot %d members:\n", i));
646 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
647 DBG((ssenv->dbg, LEVEL_1, " %+F\n", n1));
650 for (o=0; o < used_slots && tgt_slot == -1; ++o) { /* for each offset-assigned spill slot */
651 /* check inter-slot-pairs for interference */
653 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
654 for(n2 = pset_first(ass[o]->members); n2; n2 = pset_next(ass[o]->members))
655 if(values_interfere(n1, n2)) {
656 pset_break(ass[i]->members);
657 pset_break(ass[o]->members);
658 DBG((ssenv->dbg, LEVEL_1, " Interf %+F -- %+F\n", n1, n2));
659 goto interf_detected;
662 /* if we are here, there is no interference between ass[i] and ass[o] */
665 interf_detected: /*nothing*/ ;
668 /* now the members of ass[i] join the members of ass[tgt_slot] */
670 /* do we need a new slot? */
671 if (tgt_slot == -1) {
672 tgt_slot = used_slots;
677 ass[tgt_slot]->size = ass[i]->size;
678 del_pset(ass[tgt_slot]->members);
679 ass[tgt_slot]->members = pset_new_ptr(8);
683 /* copy the members to the target pset */
684 /* NOTE: If src and tgt pset are the same, inserting while iterating is not allowed */
686 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
687 pset_insert_ptr(ass[tgt_slot]->members, n1);
691 #define ALIGN_SPILL_AREA 16
692 #define pset_foreach(pset, elm) for(elm=pset_first(pset); elm; elm=pset_next(pset))
695 * Returns a spill type for a mode. Keep them in a map to reduce
696 * the number of types.
698 * @param types a map containing all created types
699 * @param ss the spill slot
701 * Note that type types should are identical for every mode.
702 * This rule might break if two different register classes return the same
703 * mode but different alignments.
705 static ir_type *get_spill_type(pmap *types, spill_slot_t *ss) {
706 pmap_entry *e = pmap_find(types, ss->largest_mode);
711 snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(ss->largest_mode));
712 res = new_type_primitive(new_id_from_str(buf), ss->largest_mode);
713 set_type_alignment_bytes(res, ss->align);
714 pmap_insert(types, ss->largest_mode, res);
718 assert(get_type_alignment_bytes(res) == (int)ss->align);
724 * Create spill slot entities on the frame type.
726 * @param ssenv the spill environment
727 * @param n number of spill slots
728 * @param ss array of spill slots
730 static void assign_entities(ss_env_t *ssenv, int n_slots, spill_slot_t *ss[]) {
731 int i, offset, frame_align;
732 ir_type *frame = get_irg_frame_type(ssenv->cenv->irg);
734 /* aligning by increasing frame size */
735 offset = get_type_size_bits(frame) / 8;
736 offset = round_up2(offset, ALIGN_SPILL_AREA);
737 set_type_size_bytes(frame, -1);
739 /* create entities and assign offsets according to size and alignment*/
740 for (i = 0; i < n_slots; ++i) {
747 snprintf(buf, sizeof(buf), "spill_slot_%d", i);
748 name = new_id_from_str(buf);
750 spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]));
753 offset = round_up2(offset, ss[i]->align);
755 set_entity_offset_bytes(spill_ent, offset);
756 /* next possible offset */
757 offset += round_up2(ss[i]->size, ss[i]->align);
759 pset_foreach(ss[i]->members, irn)
760 be_set_Spill_entity(irn, spill_ent);
763 /* set final size of stack frame */
764 frame_align = get_type_alignment_bytes(frame);
765 set_type_size_bytes(frame, round_up2(offset, frame_align));
768 void be_compute_spill_offsets(be_chordal_env_t *cenv) {
774 obstack_init(&ssenv.ob);
776 ssenv.slots = pmap_create();
777 ssenv.types = pmap_create();
778 FIRM_DBG_REGISTER(ssenv.dbg, "ir.be.spillslots");
780 /* Get initial spill slots */
781 irg_walk_graph(cenv->irg, NULL, compute_spill_slots_walker, &ssenv);
783 /* Build an empty array for optimized spill slots */
784 ss_size = pmap_count(ssenv.slots);
785 ss = obstack_alloc(&ssenv.ob, ss_size * sizeof(*ss));
786 optimize_slots(&ssenv, ss_size, ss);
788 /* Integrate slots into the stack frame entity */
789 assign_entities(&ssenv, ss_size, ss);
792 pmap_foreach(ssenv.slots, pme)
793 del_pset(((spill_slot_t *)pme->value)->members);
794 pmap_destroy(ssenv.slots);
795 pmap_destroy(ssenv.types);
796 obstack_free(&ssenv.ob, NULL);
798 be_copy_entities_to_reloads(cenv->irg);