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_spilled_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, size_t n) {
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_spilled_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_spilled_phi = is_spilled_phi;
114 env->chordal_env = chordal_env;
115 obstack_init(&env->obst);
119 void be_set_is_spilled_phi(spill_env_t *env, decide_irn_t is_spilled_phi, void *data) {
120 env->is_spilled_phi = is_spilled_phi;
124 /* Deletes a spill environment. */
125 void be_delete_spill_env(spill_env_t *senv) {
126 del_set(senv->spill_ctxs);
127 del_set(senv->spills);
128 obstack_free(&senv->obst, NULL);
133 * Returns a spill context. If the context did not exists, create one.
135 * @param sc the set containing all spill contexts
136 * @param to_spill the node that should be spilled
137 * @param ctx_irn an user of the spilled node
139 * @return a spill context.
141 static spill_ctx_t *be_get_spill_ctx(set *sc, ir_node *to_spill, ir_node *ctx_irn) {
144 templ.spilled = to_spill;
145 templ.user = ctx_irn;
148 return set_insert(sc, &templ, sizeof(templ), HASH_COMBINE(HASH_PTR(to_spill), HASH_PTR(ctx_irn)));
154 * @param senv the spill environment
155 * @param irn the node that should be spilled
156 * @param ctx_irn an user of the spilled node
158 * @return a be_Spill node
160 static ir_node *be_spill_irn(spill_env_t *senv, ir_node *irn, ir_node *ctx_irn) {
162 const be_main_env_t *env = senv->chordal_env->birg->main_env;
163 DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", irn, ctx_irn));
165 // Has the value already been spilled?
166 ctx = be_get_spill_ctx(senv->spill_ctxs, irn, ctx_irn);
170 /* Trying to spill an already spilled value, no need for a new spill
171 * node then, we can simply connect to the same one for this reload
173 if(be_is_Reload(irn)) {
174 return get_irn_n(irn, be_pos_Reload_mem);
177 ctx->spill = be_spill(env->arch_env, irn, ctx_irn);
182 * If the first usage of a Phi result would be out of memory
183 * there is no sense in allocating a register for it.
184 * Thus we spill it and all its operands to the same spill slot.
185 * Therefore the phi/dataB becomes a phi/Memory
187 * @param senv the spill environment
188 * @param phi the Phi node that should be spilled
189 * @param ctx_irn an user of the spilled node
191 * @return a be_Spill node
193 static ir_node *be_spill_phi(spill_env_t *senv, ir_node *phi, ir_node *ctx_irn, set *already_visited_phis, bitset_t *bs) {
194 int i, n = get_irn_arity(phi);
195 ir_graph *irg = senv->chordal_env->irg;
196 ir_node *bl = get_nodes_block(phi);
197 ir_node **ins, *phi_spill;
198 phi_spill_assoc_t key;
202 DBG((senv->dbg, LEVEL_1, "%+F in ctx %+F\n", phi, ctx_irn));
204 /* build a new PhiM */
205 NEW_ARR_A(ir_node *, ins, n);
206 for (i = 0; i < n; ++i) {
207 ins[i] = new_r_Bad(irg);
209 phi_spill = new_r_Phi(senv->chordal_env->irg, bl, n, ins, mode_M);
211 key.spill = phi_spill;
212 set_insert(already_visited_phis, &key, sizeof(key), HASH_PTR(phi));
213 bitset_set(bs, get_irn_idx(phi));
215 /* search an existing spill for this context */
216 ctx = be_get_spill_ctx(senv->spill_ctxs, phi, ctx_irn);
218 /* if not found spill the phi */
220 /* collect all arguments of the phi */
221 for (i = 0; i < n; ++i) {
222 ir_node *arg = get_irn_n(phi, i);
224 phi_spill_assoc_t *entry;
226 if(is_Phi(arg) && pset_find_ptr(senv->mem_phis, arg)) {
227 if (! bitset_is_set(bs, get_irn_idx(arg)))
228 sub_res = be_spill_phi(senv, arg, ctx_irn, already_visited_phis, bs);
230 /* we already visited the argument phi: get it's spill */
233 entry = set_find(already_visited_phis, &key, sizeof(key), HASH_PTR(arg));
234 assert(entry && "argument phi already visited, but no spill found?!?");
235 sub_res = entry->spill;
236 assert(sub_res && "spill missing?!?");
240 sub_res = be_spill_irn(senv, arg, ctx_irn);
242 set_irn_n(phi_spill, i, sub_res);
245 ctx->spill = phi_spill;
253 * @param senv the spill environment
254 * @param to_spill the node that should be spilled
256 * @return a be_Spill node
258 static ir_node *be_spill_node(spill_env_t *senv, ir_node *to_spill) {
259 ir_graph *irg = get_irn_irg(to_spill);
260 set *already_visited_phis = new_set(cmp_phi_spill_assoc, 10);
262 bitset_t *bs = bitset_alloca(get_irg_last_idx(irg));
264 if (pset_find_ptr(senv->mem_phis, to_spill))
265 res = be_spill_phi(senv, to_spill, to_spill, already_visited_phis, bs);
267 res = be_spill_irn(senv, to_spill, to_spill);
277 * Check if a spilled node could be rematerialized.
279 * @param senv the spill environment
280 * @param spill the Spill node
281 * @param spilled the node that was spilled
282 * @param reloader a irn that requires a reload
284 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
287 /* check for 'normal' spill and general remat condition */
288 if (!be_is_Spill(spill) || !arch_irn_is(senv->chordal_env->birg->main_env->arch_env, spilled, rematerializable))
291 /* check availability of original arguments */
292 if (is_Block(reloader)) {
294 /* we want to remat at the end of a block.
295 * thus all arguments must be alive at the end of the block
297 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
298 ir_node *arg = get_irn_n(spilled, pos);
299 if (!is_live_end(reloader, arg))
305 /* we want to remat before the insn reloader
306 * thus an arguments is alive if
307 * - it interferes with the reloaders result
309 * - or it is (last-) used by reloader itself
311 for (pos=0, max=get_irn_arity(spilled); pos<max; ++pos) {
312 ir_node *arg = get_irn_n(spilled, pos);
315 if (values_interfere(reloader, arg))
318 for (i=0, m=get_irn_arity(reloader); i<m; ++i) {
319 ir_node *rel_arg = get_irn_n(reloader, i);
324 /* arg is not alive before reloader */
336 #else /* BUGGY_REMAT */
339 * A very simple rematerialization checker.
341 * @param senv the spill environment
342 * @param spill the Spill node
343 * @param spilled the node that was spilled
344 * @param reloader a irn that requires a reload
346 static int check_remat_conditions(spill_env_t *senv, ir_node *spill, ir_node *spilled, ir_node *reloader) {
347 const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
349 return get_irn_arity(spilled) == 0 &&
350 be_is_Spill(spill) &&
351 arch_irn_is(aenv, spilled, rematerializable);
354 #endif /* BUGGY_REMAT */
359 * Re-materialize a node.
361 * @param senv the spill environment
362 * @param spilled the node that was spilled
363 * @param reloader a irn that requires a reload
365 static ir_node *do_remat(spill_env_t *senv, ir_node *spilled, ir_node *reloader) {
367 ir_node *bl = (is_Block(reloader)) ? reloader : get_nodes_block(reloader);
369 /* recompute the value */
370 res = new_ir_node(get_irn_dbg_info(spilled), senv->chordal_env->irg, bl,
372 get_irn_mode(spilled),
373 get_irn_arity(spilled),
374 get_irn_in(spilled) + 1);
375 copy_node_attr(spilled, res);
377 DBG((senv->dbg, LEVEL_1, "Insert remat %+F before reloader %+F\n", res, reloader));
379 /* insert in schedule */
380 if (is_Block(reloader)) {
381 ir_node *insert = sched_skip(reloader, 0, sched_skip_cf_predicator, (void *) senv->chordal_env->birg->main_env->arch_env);
382 sched_add_after(insert, res);
384 sched_add_before(reloader, res);
391 * Walker: fills the mem_phis set by evaluating Phi nodes
392 * using the is_spilled_phi() callback.
394 static void phi_walker(ir_node *irn, void *env) {
395 spill_env_t *senv = env;
398 const arch_env_t *arch = senv->chordal_env->birg->main_env->arch_env;
399 if (arch_irn_has_reg_class(arch, irn, 0, senv->cls) &&
400 senv->is_spilled_phi(irn, senv->data)) {
401 DBG((senv->dbg, LEVEL_1, " %+F\n", irn));
402 pset_insert_ptr(senv->mem_phis, irn);
407 void be_insert_spills_reloads(spill_env_t *senv) {
408 const arch_env_t *aenv = senv->chordal_env->birg->main_env->arch_env;
409 ir_graph *irg = senv->chordal_env->irg;
413 /* get all special spilled phis */
414 DBG((senv->dbg, LEVEL_1, "Mem-phis:\n"));
415 senv->mem_phis = pset_new_ptr_default();
416 irg_walk_graph(senv->chordal_env->irg, phi_walker, NULL, senv);
418 /* Add reloads for mem_phis */
419 /* BETTER: These reloads (1) should only be inserted, if they are really needed */
420 DBG((senv->dbg, LEVEL_1, "Reloads for mem-phis:\n"));
421 for(irn = pset_first(senv->mem_phis); irn; irn = pset_next(senv->mem_phis)) {
423 DBG((senv->dbg, LEVEL_1, " Mem-phi %+F\n", irn));
424 foreach_out_edge(irn, e) {
425 ir_node *user = e->src;
426 if (is_Phi(user) && !pset_find_ptr(senv->mem_phis, user)) {
427 ir_node *use_bl = get_nodes_block(user);
428 DBG((senv->dbg, LEVEL_1, " non-mem-phi user %+F\n", user));
429 be_add_reload_on_edge(senv, irn, use_bl, e->pos); /* (1) */
434 /* process each spilled node */
435 DBG((senv->dbg, LEVEL_1, "Insert spills and reloads:\n"));
436 for(si = set_first(senv->spills); si; si = set_next(senv->spills)) {
438 ir_mode *mode = get_irn_mode(si->spilled_node);
440 pset *values = pset_new_ptr(16);
442 /* go through all reloads for this spill */
443 for(rld = si->reloaders; rld; rld = rld->next) {
446 /* the spill for this reloader */
447 ir_node *spill = be_spill_node(senv, si->spilled_node);
450 if (check_remat_conditions(senv, spill, si->spilled_node, rld->reloader)) {
451 new_val = do_remat(senv, si->spilled_node, rld->reloader);
452 //pdeq_putl(possibly_dead, spill);
457 new_val = be_reload(aenv, senv->cls, rld->reloader, mode, spill);
459 DBG((senv->dbg, LEVEL_1, " %+F of %+F before %+F\n", new_val, si->spilled_node, rld->reloader));
460 pset_insert_ptr(values, new_val);
463 /* introduce copies, rewire the uses */
464 assert(pset_count(values) > 0 && "???");
465 pset_insert_ptr(values, si->spilled_node);
466 be_ssa_constr_set_ignore(senv->chordal_env->dom_front, values, senv->mem_phis);
471 del_pset(senv->mem_phis);
473 // reloads are placed now, but we might reuse the spill environment for further spilling decisions
474 del_set(senv->spills);
475 senv->spills = new_set(cmp_spillinfo, 1024);
478 void be_add_reload(spill_env_t *senv, ir_node *to_spill, ir_node *before) {
479 spill_info_t templ, *res;
482 templ.spilled_node = to_spill;
483 templ.reloaders = NULL;
484 res = set_insert(senv->spills, &templ, sizeof(templ), HASH_PTR(to_spill));
486 rel = obstack_alloc(&senv->obst, sizeof(rel[0]));
487 rel->reloader = before;
488 rel->next = res->reloaders;
489 res->reloaders = rel;
492 void be_add_reload_on_edge(spill_env_t *senv, ir_node *to_spill, ir_node *bl, int pos) {
493 ir_node *insert_bl = get_irn_arity(bl) == 1 ? sched_first(bl) : get_Block_cfgpred_block(bl, pos);
494 be_add_reload(senv, to_spill, insert_bl);
499 /****************************************
501 SPILL SLOT MANAGEMENT AND OPTS
503 ****************************************/
505 typedef struct _spill_slot_t {
509 ir_mode *largest_mode; /* the mode of all members with largest size */
512 typedef struct _ss_env_t {
514 be_chordal_env_t *cenv;
515 pmap *slots; /* maps spill_contexts to spill_slots */
516 pmap *types; /* maps modes to types */
517 DEBUG_ONLY(firm_dbg_module_t *dbg;)
522 * Walker: compute the spill slots
524 static void compute_spill_slots_walker(ir_node *spill, void *env) {
525 ss_env_t *ssenv = env;
530 if (!be_is_Spill(spill))
533 /* check, if this spill is for a context already known */
534 ctx = be_get_Spill_context(spill);
535 entry = pmap_find(ssenv->slots, ctx);
538 struct _arch_env_t *arch_env = ssenv->cenv->birg->main_env->arch_env;
539 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
540 ir_mode *largest_mode = arch_register_class_mode(cls);
542 /* this is a new spill context */
543 ss = obstack_alloc(&ssenv->ob, sizeof(*ss));
544 ss->members = pset_new_ptr(8);
545 ss->largest_mode = largest_mode;
546 ss->size = get_mode_size_bytes(ss->largest_mode);
547 ss->align = arch_isa_get_reg_class_alignment(arch_env->isa, cls);
548 pmap_insert(ssenv->slots, ctx, ss);
550 /* values with the same spill_ctx must go into the same spill slot */
554 /* ugly mega assert :-) */
557 struct _arch_env_t *arch_env = ssenv->cenv->birg->main_env->arch_env;
558 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, spill, be_pos_Spill_val);
559 int size = get_mode_size_bytes(arch_register_class_mode(cls));
560 assert((int) ss->size == size && "Different sizes for the same spill slot are not allowed.");
561 for (irn = pset_first(ss->members); irn; irn = pset_next(ss->members)) {
562 /* use values_interfere here, because it uses the dominance check,
563 which does work for values in memory */
564 assert(!values_interfere(spill, irn) && "Spills for the same spill slot must not interfere!");
570 pset_insert_ptr(ss->members, spill);
574 * qsort compare function, sort spill slots by size.
576 static int ss_sorter(const void *v1, const void *v2) {
577 const spill_slot_t **ss1 = (const spill_slot_t **)v1;
578 const spill_slot_t **ss2 = (const spill_slot_t **)v2;
579 return ((int) (*ss2)->size) - ((int) (*ss1)->size);
584 * This function should optimize the spill slots.
585 * - Coalescing of multiple slots
586 * - Ordering the slots
588 * Input slots are in @p ssenv->slots
589 * @p size The count of initial spill slots in @p ssenv->slots
590 * This also is the size of the preallocated array @p ass
592 * @return An array of spill slots @p ass in specific order
594 static void optimize_slots(ss_env_t *ssenv, int size, spill_slot_t *ass[]) {
595 int i, o, used_slots;
599 pmap_foreach(ssenv->slots, entr)
600 ass[i++] = entr->value;
602 /* Sort the array to minimize fragmentation and cache footprint.
603 Large slots come first */
604 qsort(ass, size, sizeof(ass[0]), ss_sorter);
606 /* For each spill slot:
607 - assign a new offset to this slot
608 - xor find another slot to coalesce with */
610 for (i=0; i<size; ++i) { /* for each spill slot */
614 DBG((ssenv->dbg, LEVEL_1, "Spill slot %d members:\n", i));
615 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
616 DBG((ssenv->dbg, LEVEL_1, " %+F\n", n1));
619 for (o=0; o < used_slots && tgt_slot == -1; ++o) { /* for each offset-assigned spill slot */
620 /* check inter-slot-pairs for interference */
622 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
623 for(n2 = pset_first(ass[o]->members); n2; n2 = pset_next(ass[o]->members))
624 if(values_interfere(n1, n2)) {
625 pset_break(ass[i]->members);
626 pset_break(ass[o]->members);
627 DBG((ssenv->dbg, LEVEL_1, " Interf %+F -- %+F\n", n1, n2));
628 goto interf_detected;
631 /* if we are here, there is no interference between ass[i] and ass[o] */
634 interf_detected: /*nothing*/ ;
637 /* now the members of ass[i] join the members of ass[tgt_slot] */
639 /* do we need a new slot? */
640 if (tgt_slot == -1) {
641 tgt_slot = used_slots;
646 ass[tgt_slot]->size = ass[i]->size;
647 del_pset(ass[tgt_slot]->members);
648 ass[tgt_slot]->members = pset_new_ptr(8);
652 /* copy the members to the target pset */
653 /* NOTE: If src and tgt pset are the same, inserting while iterating is not allowed */
655 for(n1 = pset_first(ass[i]->members); n1; n1 = pset_next(ass[i]->members))
656 pset_insert_ptr(ass[tgt_slot]->members, n1);
660 #define ALIGN_SPILL_AREA 16
661 #define pset_foreach(pset, elm) for(elm=pset_first(pset); elm; elm=pset_next(pset))
664 * Returns a spill type for a mode. Keep them in a map to reduce
665 * the number of types.
667 * @param types a map containing all created types
668 * @param ss the spill slot
670 * Note that type types should are identical for every mode.
671 * This rule might break if two different register classes return the same
672 * mode but different alignments.
674 static ir_type *get_spill_type(pmap *types, spill_slot_t *ss) {
675 pmap_entry *e = pmap_find(types, ss->largest_mode);
680 snprintf(buf, sizeof(buf), "spill_slot_type_%s", get_mode_name(ss->largest_mode));
681 res = new_type_primitive(new_id_from_str(buf), ss->largest_mode);
682 set_type_alignment_bytes(res, ss->align);
683 pmap_insert(types, ss->largest_mode, res);
687 assert(get_type_alignment_bytes(res) == (int)ss->align);
693 * Create spill slot entities on the frame type.
695 * @param ssenv the spill environment
696 * @param n number of spill slots
697 * @param ss array of spill slots
699 static void assign_entities(ss_env_t *ssenv, int n_slots, spill_slot_t *ss[]) {
700 int i, offset, frame_align;
701 ir_type *frame = get_irg_frame_type(ssenv->cenv->irg);
703 /* aligning by increasing frame size */
704 offset = get_type_size_bits(frame) / 8;
705 offset = round_up2(offset, ALIGN_SPILL_AREA);
706 set_type_size_bytes(frame, -1);
708 /* create entities and assign offsets according to size and alignment*/
709 for (i = 0; i < n_slots; ++i) {
716 snprintf(buf, sizeof(buf), "spill_slot_%d", i);
717 name = new_id_from_str(buf);
719 spill_ent = new_entity(frame, name, get_spill_type(ssenv->types, ss[i]));
722 offset = round_up2(offset, ss[i]->align);
724 set_entity_offset_bytes(spill_ent, offset);
725 /* next possible offset */
726 offset += round_up2(ss[i]->size, ss[i]->align);
728 pset_foreach(ss[i]->members, irn)
729 be_set_Spill_entity(irn, spill_ent);
732 /* set final size of stack frame */
733 frame_align = get_type_alignment_bytes(frame);
734 set_type_size_bytes(frame, round_up2(offset, frame_align));
737 void be_compute_spill_offsets(be_chordal_env_t *cenv) {
743 obstack_init(&ssenv.ob);
745 ssenv.slots = pmap_create();
746 ssenv.types = pmap_create();
747 FIRM_DBG_REGISTER(ssenv.dbg, "ir.be.spillslots");
749 /* Get initial spill slots */
750 irg_walk_graph(cenv->irg, NULL, compute_spill_slots_walker, &ssenv);
752 /* Build an empty array for optimized spill slots */
753 ss_size = pmap_count(ssenv.slots);
754 ss = obstack_alloc(&ssenv.ob, ss_size * sizeof(*ss));
755 optimize_slots(&ssenv, ss_size, ss);
757 /* Integrate slots into the stack frame entity */
758 assign_entities(&ssenv, ss_size, ss);
761 pmap_foreach(ssenv.slots, pme)
762 del_pset(((spill_slot_t *)pme->value)->members);
763 pmap_destroy(ssenv.slots);
764 pmap_destroy(ssenv.types);
765 obstack_free(&ssenv.ob, NULL);
767 be_copy_entities_to_reloads(cenv->irg);