1 /** vim: set sw=4 ts=4:
4 * @author Adam M. Szalkowski & Sebastian Hack
6 * ILP based spilling & rematerialization
8 * Copyright (C) 2006 Universitaet Karlsruhe
9 * Released under the GPL
32 #include "phiclass_t.h"
36 #include "irbackedge_t.h"
40 #include <lpp/lpp_net.h>
41 #include <lpp/lpp_cplex.h>
42 //#include <lc_pset.h>
43 //#include <libcore/lc_bitset.h>
47 #include "besched_t.h"
53 #include "bespillremat.h"
55 #include "bepressurestat.h"
56 #include "beprofile.h"
58 #include "bechordal_t.h"
61 #include <libcore/lc_opts.h>
62 #include <libcore/lc_opts_enum.h>
63 #endif /* WITH_LIBCORE */
65 #define DUMP_PROBLEM 1
67 #define DUMP_SOLUTION 4
69 #define DUMP_PRESSURE 16
71 #define KEEPALIVE_REMATS 1
72 #define KEEPALIVE_SPILLS 2
73 #define KEEPALIVE_RELOADS 4
75 #define VERIFY_MEMINTERF 1
76 #define VERIFY_DOMINANCE 2
79 #define REMATS_BRIGGS 1
80 #define REMATS_NOINVERSE 2
83 static int opt_dump_flags = 0;
84 static int opt_log = 0;
85 static int opt_keep_alive = 0;
86 static int opt_goodwin = 1;
87 static int opt_memcopies = 1;
88 static int opt_memoperands = 1;
89 static int opt_verify = VERIFY_MEMINTERF;
90 static int opt_remats = REMATS_ALL;
91 static int opt_repair_schedule = 0;
92 static int opt_no_enlarge_liveness = 0;
93 static int opt_remat_while_live = 1;
94 static int opt_timeout = 300;
95 static double opt_cost_reload = 8.0;
96 static double opt_cost_memoperand = 7.0;
97 static double opt_cost_spill = 15.0;
98 static double opt_cost_remat = 1.0;
102 static const lc_opt_enum_mask_items_t dump_items[] = {
103 { "problem", DUMP_PROBLEM },
105 { "solution", DUMP_SOLUTION },
106 { "stats", DUMP_STATS },
107 { "pressure", DUMP_PRESSURE },
111 static lc_opt_enum_mask_var_t dump_var = {
112 &opt_dump_flags, dump_items
115 static const lc_opt_enum_mask_items_t keepalive_items[] = {
116 { "remats", KEEPALIVE_REMATS },
117 { "spills", KEEPALIVE_SPILLS },
118 { "reloads", KEEPALIVE_RELOADS },
122 static lc_opt_enum_mask_var_t keep_alive_var = {
123 &opt_keep_alive, keepalive_items
126 static const lc_opt_enum_mask_items_t remats_items[] = {
127 { "none", REMATS_NONE },
128 { "briggs", REMATS_BRIGGS },
129 { "noinverse", REMATS_NOINVERSE },
130 { "all", REMATS_ALL },
134 static lc_opt_enum_mask_var_t remats_var = {
135 &opt_remats, remats_items
138 static const lc_opt_table_entry_t options[] = {
139 LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var),
141 LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin),
142 LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies),
143 LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands),
144 LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var),
145 LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule),
146 LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
147 LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live),
149 LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var),
150 LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log),
151 LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout),
153 LC_OPT_ENT_DBL ("cost_reload", "cost of a reload", &opt_cost_reload),
154 LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand),
155 LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill),
156 LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat),
160 void be_spill_remat_register_options(lc_opt_entry_t *grp)
162 lc_opt_entry_t *my_grp = lc_opt_get_grp(grp, "remat");
163 lc_opt_add_table(my_grp, options);
168 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
169 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
172 //#define SOLVE_LOCAL
173 #define LPP_SERVER "i44pc52"
174 #define LPP_SOLVER "cplex"
177 #define MAX_PATHS INT_MAX
180 typedef struct _spill_ilp_t {
181 const arch_register_class_t *cls;
183 const be_chordal_env_t *chordal_env;
186 struct obstack *obst;
188 pset *all_possible_remats;
191 set *values; /**< for collecting all definitions of values before running ssa-construction */
196 #ifndef SCHEDULE_PHIM
199 DEBUG_ONLY(firm_dbg_module_t * dbg);
202 typedef int ilp_var_t;
203 typedef int ilp_cst_t;
205 typedef struct _spill_bb_t {
210 typedef struct _remat_t {
211 const ir_node *op; /**< for copy_irn */
212 const ir_node *value; /**< the value which is being recomputed by this remat */
213 const ir_node *proj; /**< not NULL if the above op produces a tuple */
214 int cost; /**< cost of this remat */
215 int inverse; /**< nonzero if this is an inverse remat */
219 * Data to be attached to each IR node. For remats this contains the ilp_var
220 * for this remat and for normal ops this contains the ilp_vars for
221 * reloading each operand
223 typedef struct _op_t {
228 const remat_t *remat; /** the remat this op belongs to */
229 int pre; /** 1, if this is a pressure-increasing remat */
233 ir_node *op; /** the operation this live range belongs to */
242 typedef struct _defs_t {
243 const ir_node *value;
244 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
245 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
248 typedef struct _remat_info_t {
249 const ir_node *irn; /**< the irn to which these remats belong */
250 pset *remats; /**< possible remats for this value */
251 pset *remats_by_operand; /**< remats with this value as operand */
254 typedef struct _keyval_t {
259 typedef struct _spill_t {
268 typedef struct _memoperand_t {
269 ir_node *irn; /**< the irn */
270 unsigned int pos; /**< the position of the argument */
271 ilp_var_t ilp; /**< the ilp var for this memory operand */
275 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
277 return chordal_has_class(si->chordal_env, irn);
282 cmp_remat(const void *a, const void *b)
284 const keyval_t *p = a;
285 const keyval_t *q = b;
286 const remat_t *r = p->val;
287 const remat_t *s = q->val;
291 return !(r == s || r->op == s->op);
295 cmp_remat(const void *a, const void *b)
297 const remat_t *r = a;
298 const remat_t *s = a;
300 return !(r == s || r->op == s->op);
304 cmp_spill(const void *a, const void *b, size_t size)
306 const spill_t *p = a;
307 const spill_t *q = b;
309 // return !(p->irn == q->irn && p->bb == q->bb);
310 return !(p->irn == q->irn);
314 cmp_memoperands(const void *a, const void *b, size_t size)
316 const memoperand_t *p = a;
317 const memoperand_t *q = b;
319 return !(p->irn == q->irn && p->pos == q->pos);
323 set_find_keyval(set * set, const void * key)
328 return set_find(set, &query, sizeof(query), HASH_PTR(key));
332 set_insert_keyval(set * set, void * key, void * val)
338 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
342 set_find_def(set * set, const ir_node * value)
347 return set_find(set, &query, sizeof(query), HASH_PTR(value));
351 set_insert_def(set * set, const ir_node * value)
358 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
361 static memoperand_t *
362 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
369 return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
372 static memoperand_t *
373 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
377 query.irn = (ir_node*)irn;
379 return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
384 set_find_spill(set * set, const ir_node * value)
388 query.irn = (ir_node*)value;
389 return set_find(set, &query, sizeof(query), HASH_PTR(value));
392 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
393 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
394 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
395 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
396 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
399 cmp_remat_info(const void *a, const void *b, size_t size)
401 const remat_info_t *p = a;
402 const remat_info_t *q = b;
404 return !(p->irn == q->irn);
408 cmp_defs(const void *a, const void *b, size_t size)
413 return !(p->value == q->value);
417 cmp_keyval(const void *a, const void *b, size_t size)
419 const keyval_t *p = a;
420 const keyval_t *q = b;
422 return !(p->key == q->key);
426 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
429 if(be_profile_has_data())
430 return ((double)be_profile_get_block_execcount(get_block(irn))) + FUDGE;
432 #ifndef EXECFREQ_LOOPDEPH
433 return get_block_execfreq(si->chordal_env->exec_freq, get_block(irn)) + FUDGE;
436 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
438 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
443 get_cost(const spill_ilp_t * si, const ir_node * irn)
445 if(be_is_Spill(irn)) {
446 return opt_cost_spill;
447 } else if(be_is_Reload(irn)){
448 return opt_cost_reload;
450 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
455 * Checks, whether node and its operands have suitable reg classes
458 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
461 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
462 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
466 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
469 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
470 ir_node *op = get_irn_n(irn, n);
471 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
474 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
481 * Try to create a remat from @p op with destination value @p dest_value
483 static INLINE remat_t *
484 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
486 remat_t *remat = NULL;
488 // if(!mode_is_datab(get_irn_mode(dest_value)))
491 if(dest_value == op) {
492 const ir_node *proj = NULL;
494 if(is_Proj(dest_value)) {
495 op = get_Proj_pred(op);
499 if(!is_rematerializable(si, op))
502 remat = obstack_alloc(si->obst, sizeof(*remat));
504 remat->cost = (int)get_cost(si, op);
505 remat->value = dest_value;
509 arch_inverse_t inverse;
512 /* get the index of the operand we want to retrieve by the inverse op */
513 for (n = get_irn_arity(op)-1; n>=0; --n) {
514 ir_node *arg = get_irn_n(op, n);
516 if(arg == dest_value) break;
520 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
522 /* else ask the backend to give an inverse op */
523 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
526 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
528 assert(inverse.n > 0 && "inverse op should have at least one node");
530 for(i=inverse.n-1; i>=0; --i) {
531 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
535 remat = obstack_alloc(si->obst, sizeof(*remat));
536 remat->op = inverse.nodes[0];
537 remat->cost = inverse.costs;
538 remat->value = dest_value;
539 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
542 assert(is_Proj(remat->proj));
544 assert(0 && "I can not handle remats with more than 2 nodes");
551 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
553 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
561 add_remat(const spill_ilp_t * si, const remat_t * remat)
563 remat_info_t *remat_info,
568 assert(remat->value);
570 query.irn = remat->value;
572 query.remats_by_operand = NULL;
573 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
575 if(remat_info->remats == NULL) {
576 remat_info->remats = new_pset(cmp_remat, 4096);
578 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
580 /* insert the remat into the remats_be_operand set of each argument of the remat op */
581 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
582 ir_node *arg = get_irn_n(remat->op, n);
586 query.remats_by_operand = NULL;
587 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
589 if(remat_info->remats_by_operand == NULL) {
590 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
592 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
597 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
599 const ir_edge_t *edge = get_irn_out_edge_first(irn);
603 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
606 edge = get_irn_out_edge_next(irn, edge);
613 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
619 irn = get_Proj_pred(irn);
621 for(n=get_irn_arity(irn)-1; n>=0; --n) {
622 const ir_node *arg = get_irn_n(irn, n);
624 if(has_reg_class(si, arg)) ++ret;
631 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
636 if( has_reg_class(si, op)
637 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
638 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
640 remat = get_remat_from_op(si, op, op);
642 add_remat(si, remat);
646 if(opt_remats == REMATS_ALL) {
647 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
649 for (n = get_irn_arity(op)-1; n>=0; --n) {
650 ir_node *arg = get_irn_n(op, n);
652 if(has_reg_class(si, arg)) {
653 /* try to get an inverse remat */
654 remat = get_remat_from_op(si, arg, op);
656 add_remat(si, remat);
664 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
667 ir_node *def_block = get_nodes_block(val);
673 /* if pos is at end of a basic block */
675 ret = (pos == def_block || block_dominates(def_block, pos));
676 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
680 /* else if this is a normal operation */
681 block = get_nodes_block(pos);
682 if(block == def_block) {
683 if(!sched_is_scheduled(val)) return 1;
685 ret = sched_comes_after(val, pos);
686 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
690 ret = block_dominates(def_block, block);
691 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
695 static INLINE ir_node *
696 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
698 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
702 * Returns first non-Phi node of block @p bb
704 static INLINE ir_node *
705 sched_block_first_nonphi(const ir_node * bb)
707 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
711 sched_skip_proj_predicator(const ir_node * irn, void * data)
713 return (is_Proj(irn));
716 static INLINE ir_node *
717 sched_next_nonproj(const ir_node * irn, int forward)
719 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
723 * Returns next operation node (non-Proj) after @p irn
724 * or the basic block of this node
726 static INLINE ir_node *
727 sched_next_op(const ir_node * irn)
729 ir_node *next = sched_next(irn);
734 return sched_next_nonproj(next, 1);
738 * Returns previous operation node (non-Proj) before @p irn
739 * or the basic block of this node
741 static INLINE ir_node *
742 sched_prev_op(const ir_node * irn)
744 ir_node *prev = sched_prev(irn);
749 return sched_next_nonproj(prev, 0);
753 sched_put_after(ir_node * insert, ir_node * irn)
755 if(is_Block(insert)) {
756 insert = sched_block_first_nonphi(insert);
758 insert = sched_next_op(insert);
760 sched_add_before(insert, irn);
764 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
766 if(is_Block(insert)) {
767 insert = sched_block_last_noncf(si, insert);
769 insert = sched_next_nonproj(insert, 0);
770 insert = sched_prev(insert);
772 sched_add_after(insert, irn);
776 next_post_remat(const ir_node * irn)
782 next = sched_block_first_nonphi(irn);
784 next = sched_next_op(irn);
787 if(sched_is_end(next))
790 op = get_irn_link(next);
791 if(op->is_remat && !op->attr.remat.pre) {
800 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
806 ret = sched_block_last_noncf(si, irn);
807 ret = sched_next(ret);
808 ret = sched_prev_op(ret);
810 ret = sched_prev_op(irn);
813 if(sched_is_end(ret) || is_Phi(ret))
816 op = (op_t*)get_irn_link(ret);
817 if(op->is_remat && op->attr.remat.pre) {
825 * Tells you whether a @p remat can be placed before the irn @p pos
828 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
830 const ir_node *op = remat->op;
836 prev = sched_block_last_noncf(si, pos);
837 prev = sched_next_nonproj(prev, 0);
839 prev = sched_prev_op(pos);
841 /* do not remat if the rematted value is defined immediately before this op */
842 if(prev == remat->op) {
847 /* this should be just fine, the following OP will be using this value, right? */
849 /* only remat AFTER the real definition of a value (?) */
850 if(!value_is_defined_before(si, pos, remat->value)) {
851 // ir_fprintf(stderr, "error(not defined)");
856 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
857 const ir_node *arg = get_irn_n(op, n);
859 if(opt_no_enlarge_liveness) {
860 if(has_reg_class(si, arg) && live) {
861 res &= pset_find_ptr((pset*)live, arg)?1:0;
863 res &= value_is_defined_before(si, pos, arg);
866 res &= value_is_defined_before(si, pos, arg);
874 * Tells you whether a @p remat can be placed after the irn @p pos
877 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
880 pos = sched_block_first_nonphi(pos);
882 pos = sched_next_op(pos);
885 /* only remat AFTER the real definition of a value (?) */
886 if(!value_is_defined_before(si, pos, remat->value)) {
890 return can_remat_before(si, remat, pos, live);
894 * Collect potetially rematerializable OPs
897 walker_remat_collector(ir_node * irn, void * data)
899 spill_ilp_t *si = data;
901 if(!is_Block(irn) && !is_Phi(irn)) {
902 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
903 get_remats_from_op(si, irn);
908 * Inserts a copy of @p irn before @p pos
911 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
916 bb = is_Block(pos)?pos:get_nodes_block(pos);
917 copy = exact_copy(irn);
919 _set_phi_class(copy, NULL);
920 set_nodes_block(copy, bb);
921 sched_put_before(si, pos, copy);
927 * Inserts a copy of @p irn after @p pos
930 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
935 bb = is_Block(pos)?pos:get_nodes_block(pos);
936 copy = exact_copy(irn);
938 _set_phi_class(copy, NULL);
939 set_nodes_block(copy, bb);
940 sched_put_after(pos, copy);
946 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
950 if(can_remat_after(si, remat, pos, live)) {
955 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
957 copy = insert_copy_after(si, remat->op, pos);
959 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
960 op = obstack_alloc(si->obst, sizeof(*op));
962 op->attr.remat.remat = remat;
963 op->attr.remat.pre = 0;
964 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
966 set_irn_link(copy, op);
967 pset_insert_ptr(si->all_possible_remats, copy);
969 proj_copy = insert_copy_after(si, remat->proj, copy);
970 set_irn_n(proj_copy, 0, copy);
971 set_irn_link(proj_copy, op);
972 pset_insert_ptr(si->all_possible_remats, proj_copy);
984 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
988 if(can_remat_before(si, remat, pos, live)) {
993 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
995 copy = insert_copy_before(si, remat->op, pos);
997 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
998 op = obstack_alloc(si->obst, sizeof(*op));
1000 op->attr.remat.remat = remat;
1001 op->attr.remat.pre = 1;
1002 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1004 set_irn_link(copy, op);
1005 pset_insert_ptr(si->all_possible_remats, copy);
1007 proj_copy = insert_copy_after(si, remat->proj, copy);
1008 set_irn_n(proj_copy, 0, copy);
1009 set_irn_link(proj_copy, op);
1010 pset_insert_ptr(si->all_possible_remats, proj_copy);
1022 get_block_n_succs(const ir_node *block) {
1023 const ir_edge_t *edge;
1025 assert(edges_activated(current_ir_graph));
1027 edge = get_block_succ_first(block);
1031 edge = get_block_succ_next(block, edge);
1032 return edge ? 2 : 1;
1036 is_start_block(const ir_node * bb)
1038 return get_irg_start_block(get_irn_irg(bb)) == bb;
1042 is_merge_edge(const ir_node * bb)
1044 if(is_start_block(bb))
1048 return get_block_n_succs(bb) == 1;
1054 is_diverge_edge(const ir_node * bb)
1056 if(is_start_block(bb))
1060 return get_Block_n_cfgpreds(bb) == 1;
1066 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1071 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1072 irn = be_lv_get_irn(si->lv, bb, i);
1074 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1075 pset_insert_ptr(live, irn);
1079 irn = sched_last(bb);
1081 /* all values eaten by control flow operations are also live until the end of the block */
1082 sched_foreach_reverse(bb, irn) {
1085 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1087 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1088 ir_node *arg = get_irn_n(irn,i);
1090 if(has_reg_class(si, arg)) {
1091 pset_insert_ptr(live, arg);
1096 * find values that are used by remats at end of block
1097 * and insert them into live set
1099 foreach_pre_remat(si, bb, irn) {
1102 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1103 ir_node *remat_arg = get_irn_n(irn, n);
1105 if(!has_reg_class(si, remat_arg)) continue;
1107 /* if value is becoming live through use by remat */
1108 if(!pset_find_ptr(live, remat_arg)) {
1109 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1111 pset_insert_ptr(live, remat_arg);
1118 walker_regclass_copy_insertor(ir_node * irn, void * data)
1120 spill_ilp_t *si = data;
1122 if(is_Phi(irn) && has_reg_class(si, irn)) {
1125 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1126 ir_node *phi_arg = get_irn_n(irn, n);
1127 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1129 if(!has_reg_class(si, phi_arg)) {
1130 ir_node *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg);
1131 ir_node *pos = sched_block_last_noncf(si, bb);
1132 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1134 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1135 sched_add_after(pos, copy);
1136 set_irn_n(irn, n, copy);
1139 op->attr.live_range.args.reloads = NULL;
1140 op->attr.live_range.ilp = ILP_UNDEF;
1141 set_irn_link(copy, op);
1148 * Insert (so far unused) remats into the irg to
1149 * recompute the potential liveness of all values
1152 walker_remat_insertor(ir_node * bb, void * data)
1154 spill_ilp_t *si = data;
1161 /* skip start block, no remats to do there */
1162 if(is_start_block(bb)) return;
1164 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1166 live = pset_new_ptr_default();
1167 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1168 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1170 /* add remats at end of block */
1171 if (has_reg_class(si, value)) {
1172 pset_insert_ptr(live, value);
1176 irn = sched_last(bb);
1177 while(!sched_is_end(irn)) {
1183 next = sched_prev(irn);
1185 /* delete defined value from live set */
1186 if(has_reg_class(si, irn)) {
1187 pset_remove_ptr(live, irn);
1190 if(is_Phi(irn) || is_Proj(irn)) {
1195 args = pset_new_ptr_default();
1196 used = pset_new_ptr_default();
1198 /* collect arguments of op and set args of op already live in epilog */
1199 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1200 ir_node *arg = get_irn_n(irn, n);
1202 pset_insert_ptr(args, arg);
1203 if(has_reg_class(si, arg)) {
1204 pset_insert_ptr(live, arg);
1205 pset_insert_ptr(used, arg);
1209 /* insert all possible remats before irn */
1210 pset_foreach(args, arg) {
1211 remat_info_t *remat_info,
1214 /* continue if the operand has the wrong reg class */
1215 if(!has_reg_class(si, arg))
1219 query.remats = NULL;
1220 query.remats_by_operand = NULL;
1221 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1227 if(remat_info->remats) {
1228 pset_foreach(remat_info->remats, remat) {
1229 ir_node *remat_irn = NULL;
1231 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1232 remat_irn = insert_remat_before(si, remat, irn, live);
1235 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1236 ir_node *remat_arg = get_irn_n(remat_irn, n);
1238 /* collect args of remats which are not args of op */
1239 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1240 pset_insert_ptr(used, remat_arg);
1248 /* do not place post remats after jumps */
1249 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) {
1255 /* insert all possible remats after irn */
1256 post_remats = pset_new_ptr_default();
1257 pset_foreach(used, arg) {
1258 remat_info_t *remat_info,
1261 /* continue if the operand has the wrong reg class */
1262 if(!has_reg_class(si, arg))
1266 query.remats = NULL;
1267 query.remats_by_operand = NULL;
1268 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1274 if(remat_info->remats_by_operand) {
1275 pset_foreach(remat_info->remats_by_operand, remat) {
1276 /* do not insert remats producing the same value as one of the operands */
1277 if(!pset_find_ptr(args, remat->value)) {
1278 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1280 /* only remat values that can be used by real ops */
1281 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1282 pset_insert_ptr(post_remats, remat);
1288 pset_foreach(post_remats, remat) {
1289 insert_remat_after(si, remat, irn, live);
1291 del_pset(post_remats);
1298 /* add remats at end if successor has multiple predecessors */
1299 if(is_merge_edge(bb)) {
1300 pset *live_out = pset_new_ptr_default();
1303 get_live_end(si, bb, live_out);
1305 /* add remats at end of block */
1306 pset_foreach(live_out, value) {
1307 remat_info_t *remat_info,
1311 query.remats = NULL;
1312 query.remats_by_operand = NULL;
1313 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1315 if(remat_info && remat_info->remats) {
1316 pset_foreach(remat_info->remats, remat) {
1317 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1319 insert_remat_before(si, remat, bb, live_out);
1326 if(is_diverge_edge(bb)) {
1327 pset *live_in = pset_new_ptr_default();
1330 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1331 value = be_lv_get_irn(si->lv, bb, i);
1333 if(has_reg_class(si, value)) {
1334 pset_insert_ptr(live_in, value);
1337 /* add phis to live_in */
1338 sched_foreach(bb, value) {
1339 if(!is_Phi(value)) break;
1341 if(has_reg_class(si, value)) {
1342 pset_insert_ptr(live_in, value);
1346 /* add remat2s at beginning of block */
1347 post_remats = pset_new_ptr_default();
1348 pset_foreach(live_in, value) {
1349 remat_info_t *remat_info,
1353 query.remats = NULL;
1354 query.remats_by_operand = NULL;
1355 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1357 if(remat_info && remat_info->remats_by_operand) {
1358 pset_foreach(remat_info->remats_by_operand, remat) {
1359 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1361 /* put the remat here if all its args are available and result is still live */
1362 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1363 pset_insert_ptr(post_remats, remat);
1368 pset_foreach(post_remats, remat) {
1369 insert_remat_after(si, remat, bb, live_in);
1371 del_pset(post_remats);
1377 can_be_copied(const ir_node * bb, const ir_node * irn)
1379 const ir_edge_t *edge = get_block_succ_first(bb);
1380 const ir_node *next_bb = edge->src;
1381 int pos = edge->pos;
1384 assert(is_merge_edge(bb));
1386 sched_foreach(next_bb, phi) {
1387 const ir_node *phi_arg;
1389 if(!is_Phi(phi)) break;
1391 phi_arg = get_irn_n(phi, pos);
1393 if(phi_arg == irn) {
1401 * Initialize additional node info
1404 luke_initializer(ir_node * bb, void * data)
1406 spill_ilp_t *si = (spill_ilp_t*)data;
1407 spill_bb_t *spill_bb;
1410 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1411 set_irn_link(bb, spill_bb);
1413 sched_foreach(bb, irn) {
1416 op = obstack_alloc(si->obst, sizeof(*op));
1418 op->attr.live_range.ilp = ILP_UNDEF;
1421 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1422 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1424 } else if(!is_Proj(irn)) {
1425 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1426 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1428 op->attr.live_range.args.reloads = NULL;
1430 set_irn_link(irn, op);
1436 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1439 luke_endwalker(ir_node * bb, void * data)
1441 spill_ilp_t *si = (spill_ilp_t*)data;
1447 spill_bb_t *spill_bb = get_irn_link(bb);
1450 live = pset_new_ptr_default();
1451 use_end = pset_new_ptr_default();
1453 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1454 irn = be_lv_get_irn(si->lv, bb, i);
1455 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1456 pset_insert_ptr(live, irn);
1460 * find values that are used by remats at end of block
1461 * and insert them into live set
1463 foreach_pre_remat(si, bb, irn) {
1466 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1467 ir_node *remat_arg = get_irn_n(irn, n);
1469 if(has_reg_class(si, remat_arg)) {
1470 pset_insert_ptr(live, remat_arg);
1475 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1476 /* their reg_out must always be set */
1477 sched_foreach_reverse(bb, irn) {
1480 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1482 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1483 ir_node *irn_arg = get_irn_n(irn, n);
1485 if(has_reg_class(si, irn_arg)) {
1486 pset_insert_ptr(use_end, irn_arg);
1491 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1492 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1493 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1495 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1497 /* if this is a merge edge we can reload at the end of this block */
1498 if(is_merge_edge(bb)) {
1499 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1500 } else if(pset_count(use_end)){
1501 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1503 spill_bb->reloads = NULL;
1506 pset_foreach(live,irn) {
1510 int default_spilled;
1513 /* handle values used by control flow nodes later separately */
1514 if(pset_find_ptr(use_end, irn)) continue;
1517 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1519 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1521 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1522 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1523 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1525 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1526 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1528 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1529 /* by default spill value right after definition */
1530 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1531 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1533 if(is_merge_edge(bb)) {
1537 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1538 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1539 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1541 /* reload <= mem_out */
1542 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1543 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1544 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1547 spill->reg_in = ILP_UNDEF;
1548 spill->mem_in = ILP_UNDEF;
1551 pset_foreach(use_end,irn) {
1555 ilp_cst_t end_use_req,
1558 int default_spilled;
1561 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1563 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1565 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1566 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1568 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1569 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1571 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1572 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1573 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1575 /* reload for use be control flow op */
1576 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1577 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1578 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1580 /* reload <= mem_out */
1581 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1582 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1583 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1585 spill->reg_in = ILP_UNDEF;
1586 spill->mem_in = ILP_UNDEF;
1588 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1589 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1590 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1598 * Find a remat of value @p value in the epilog of @p pos
1601 find_post_remat(const ir_node * value, const ir_node * pos)
1603 while((pos = next_post_remat(pos)) != NULL) {
1606 op = get_irn_link(pos);
1607 assert(op->is_remat && !op->attr.remat.pre);
1609 if(op->attr.remat.remat->value == value)
1610 return (ir_node*)pos;
1613 const ir_edge_t *edge;
1614 foreach_out_edge(pos, edge) {
1615 ir_node *proj = get_edge_src_irn(edge);
1616 assert(is_Proj(proj));
1626 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1628 spill_bb_t *spill_bb = get_irn_link(bb);
1632 int default_spilled;
1635 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1637 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1639 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1641 spill->reg_out = ILP_UNDEF;
1642 spill->reg_in = ILP_UNDEF;
1643 spill->mem_in = ILP_UNDEF;
1645 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1646 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1648 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1649 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1650 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1657 * Inserts ILP-constraints and variables for memory copying before the given position
1660 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1662 const ir_node *succ;
1663 const ir_edge_t *edge;
1664 spill_bb_t *spill_bb = get_irn_link(block);
1673 assert(edges_activated(current_ir_graph));
1675 edge = get_block_succ_first(block);
1681 edge = get_block_succ_next(block, edge);
1682 /* next block can only contain phis, if this is a merge edge */
1685 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1686 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1688 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1689 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1691 pset_foreach(live, tmp) {
1694 op_t *op = get_irn_link(irn);
1695 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1697 spill = set_find_spill(spill_bb->ilp, tmp);
1700 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1702 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1704 sched_foreach(succ, phi) {
1705 const ir_node *to_copy;
1707 spill_t *to_copy_spill;
1708 op_t *phi_op = get_irn_link(phi);
1709 ilp_var_t reload = ILP_UNDEF;
1712 if(!is_Phi(phi)) break;
1713 if(!has_reg_class(si, phi)) continue;
1715 to_copy = get_irn_n(phi, pos);
1716 to_copy_op = get_irn_link(to_copy);
1718 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1719 assert(to_copy_spill);
1721 if(spill_bb->reloads) {
1722 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1725 reload = PTR_TO_INT(keyval->val);
1729 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1730 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1732 /* copy - reg_out - reload - remat - live_range <= 0 */
1733 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1734 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1735 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1736 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1737 foreach_pre_remat(si, block, tmp) {
1738 op_t *remat_op = get_irn_link(tmp);
1739 if(remat_op->attr.remat.remat->value == to_copy) {
1740 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1744 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1745 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1747 /* copy - reg_out - copyreg <= 0 */
1748 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1749 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1750 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1756 * Walk all irg blocks and emit this ILP
1759 luke_blockwalker(ir_node * bb, void * data)
1761 spill_ilp_t *si = (spill_ilp_t*)data;
1766 spill_bb_t *spill_bb = get_irn_link(bb);
1769 pset *defs = pset_new_ptr_default();
1770 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
1772 live = pset_new_ptr_default();
1774 /****************************************
1775 * B A S I C B L O C K E N D
1776 ***************************************/
1779 /* init live values at end of block */
1780 get_live_end(si, bb, live);
1782 pset_foreach(live, irn) {
1784 ilp_var_t reload = ILP_UNDEF;
1786 spill = set_find_spill(spill_bb->ilp, irn);
1789 if(spill_bb->reloads) {
1790 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1793 reload = PTR_TO_INT(keyval->val);
1797 op = get_irn_link(irn);
1798 assert(!op->is_remat);
1800 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1801 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1802 op->attr.live_range.op = bb;
1804 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1805 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1807 /* reg_out - reload - remat - live_range <= 0 */
1808 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1809 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1810 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1811 foreach_pre_remat(si, bb, tmp) {
1812 op_t *remat_op = get_irn_link(tmp);
1813 if(remat_op->attr.remat.remat->value == irn) {
1814 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1817 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1818 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1820 /* value may only die at bb end if it is used for a mem copy */
1821 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1822 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1823 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1824 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1825 foreach_pre_remat(si, bb, tmp) {
1826 op_t *remat_op = get_irn_link(tmp);
1827 if(remat_op->attr.remat.remat->value == irn) {
1828 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1831 if(is_merge_edge(bb)) {
1832 const ir_edge_t *edge = get_block_succ_first(bb);
1833 const ir_node *next_bb = edge->src;
1834 int pos = edge->pos;
1837 sched_foreach(next_bb, phi) {
1838 const ir_node *phi_arg;
1840 if(!is_Phi(phi)) break;
1842 phi_arg = get_irn_n(phi, pos);
1844 if(phi_arg == irn) {
1845 op_t *phi_op = get_irn_link(phi);
1846 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1848 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1855 insert_mem_copy_position(si, live, bb);
1858 * assure the remat args are available
1860 foreach_pre_remat(si, bb, tmp) {
1861 op_t *remat_op = get_irn_link(tmp);
1864 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1865 ir_node *remat_arg = get_irn_n(tmp, n);
1866 op_t *arg_op = get_irn_link(remat_arg);
1868 if(!has_reg_class(si, remat_arg)) continue;
1870 spill = set_find_spill(spill_bb->ilp, remat_arg);
1873 /* arguments of remats have to be live until the very end of the block
1874 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1875 * no remats, they could be in wrong order
1878 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1879 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1881 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1882 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1883 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1885 /* use reload placed for this argument */
1886 if(spill_bb->reloads) {
1887 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1890 ilp_var_t reload = PTR_TO_INT(keyval->val);
1892 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1897 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1902 /**************************************
1903 * B A S I C B L O C K B O D Y
1904 **************************************/
1906 sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1912 ilp_cst_t check_pre,
1918 ilp_cst_t one_memoperand;
1920 /* iterate only until first phi */
1924 op = get_irn_link(irn);
1926 if(op->is_remat) continue;
1928 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1930 /* collect defined values */
1931 if(has_reg_class(si, irn)) {
1932 pset_insert_ptr(defs, irn);
1936 if(is_Proj(irn)) continue;
1939 * init set of irn's arguments
1940 * and all possibly used values around this op
1941 * and values defined by post remats
1943 args = new_set(cmp_keyval, get_irn_arity(irn));
1944 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1945 remat_defs = pset_new_ptr(pset_count(live));
1947 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1948 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1949 ir_node *irn_arg = get_irn_n(irn, n);
1950 if(has_reg_class(si, irn_arg)) {
1951 set_insert_keyval(args, irn_arg, (void*)n);
1952 pset_insert_ptr(used, irn_arg);
1955 foreach_post_remat(irn, tmp) {
1956 op_t *remat_op = get_irn_link(tmp);
1958 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1960 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1961 ir_node *remat_arg = get_irn_n(tmp, n);
1962 if(has_reg_class(si, remat_arg)) {
1963 pset_insert_ptr(used, remat_arg);
1967 foreach_pre_remat(si, irn, tmp) {
1968 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1969 ir_node *remat_arg = get_irn_n(tmp, n);
1970 if(has_reg_class(si, remat_arg)) {
1971 pset_insert_ptr(used, remat_arg);
1977 /**********************************
1978 * I N E P I L O G O F irn
1979 **********************************/
1981 /* ensure each dying value is used by only one post remat */
1982 pset_foreach(used, tmp) {
1983 ir_node *value = tmp;
1984 op_t *value_op = get_irn_link(value);
1989 foreach_post_remat(irn, remat) {
1990 op_t *remat_op = get_irn_link(remat);
1992 for(n=get_irn_arity(remat)-1; n>=0; --n) {
1993 ir_node *remat_arg = get_irn_n(remat, n);
1995 /* if value is used by this remat add it to constraint */
1996 if(remat_arg == value) {
1998 /* sum remat2s <= 1 + n_remats*live_range */
1999 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2000 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2004 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2010 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2011 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2015 /* ensure at least one value dies at post remat */
2016 foreach_post_remat(irn, tmp) {
2017 op_t *remat_op = get_irn_link(tmp);
2018 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2021 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2022 remat_arg = get_irn_n(tmp, n);
2024 if(has_reg_class(si, remat_arg)) {
2026 /* does arg always die at this op? */
2027 if(!pset_find_ptr(live, remat_arg))
2028 goto skip_one_must_die;
2030 pset_insert_ptr(remat_args, remat_arg);
2034 /* remat + \sum live_range(remat_arg) <= |args| */
2035 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2036 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2037 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2039 pset_foreach(remat_args, remat_arg) {
2040 op_t *arg_op = get_irn_link(remat_arg);
2042 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2046 del_pset(remat_args);
2049 /* new live ranges for values from L\U defined by post remats */
2050 pset_foreach(live, tmp) {
2051 ir_node *value = tmp;
2052 op_t *value_op = get_irn_link(value);
2054 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2055 ilp_var_t prev_lr = ILP_UNDEF;
2058 if(pset_find_ptr(remat_defs, value)) {
2060 /* next_live_range <= prev_live_range + sum remat2s */
2061 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2062 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2064 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2065 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2067 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2068 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2070 foreach_post_remat(irn, remat) {
2071 op_t *remat_op = get_irn_link(remat);
2073 /* if value is being rematerialized by this remat */
2074 if(value == remat_op->attr.remat.remat->value) {
2075 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2079 value_op->attr.live_range.ilp = prev_lr;
2080 value_op->attr.live_range.op = irn;
2085 /* requirements for post remats and start live ranges from L/U' for values dying here */
2086 foreach_post_remat(irn, tmp) {
2087 op_t *remat_op = get_irn_link(tmp);
2090 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2091 ir_node *remat_arg = get_irn_n(tmp, n);
2092 op_t *arg_op = get_irn_link(remat_arg);
2094 if(!has_reg_class(si, remat_arg)) continue;
2096 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2097 if(!pset_find_ptr(used, remat_arg)) {
2098 /* remat <= live_range(remat_arg) */
2099 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2100 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2102 /* if value is becoming live through use by remat2 */
2103 if(!pset_find_ptr(live, remat_arg)) {
2106 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2107 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2109 arg_op->attr.live_range.ilp = lr;
2110 arg_op->attr.live_range.op = irn;
2112 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2114 pset_insert_ptr(live, remat_arg);
2115 add_to_spill_bb(si, bb, remat_arg);
2118 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2119 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2124 d = pset_count(defs);
2125 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2127 /* count how many regs irn needs for arguments */
2128 u = set_count(args);
2131 /* check the register pressure in the epilog */
2132 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2133 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2134 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2136 /* add L\U' to check_post */
2137 pset_foreach(live, tmp) {
2138 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2139 /* if a live value is not used by irn */
2140 tmp_op = get_irn_link(tmp);
2141 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2145 /***********************************************************
2146 * I T E R A T I O N O V E R U S E S F O R E P I L O G
2147 **********************************************************/
2150 pset_foreach(used, tmp) {
2156 op_t *arg_op = get_irn_link(arg);
2159 spill = add_to_spill_bb(si, bb, arg);
2161 /* new live range for each used value */
2162 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2163 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2165 /* the epilog stuff - including post_use, check_post, check_post_remat */
2166 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2167 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2169 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2171 /* arg is live throughout epilog if the next live_range is in a register */
2172 if(pset_find_ptr(live, arg)) {
2173 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2175 /* post_use >= next_lr + remat */
2176 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2177 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2178 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2179 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2182 /* forall post remat which use arg add a similar cst */
2183 foreach_post_remat(irn, remat) {
2186 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2187 ir_node *remat_arg = get_irn_n(remat, n);
2188 op_t *remat_op = get_irn_link(remat);
2190 if(remat_arg == arg) {
2191 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2193 /* post_use >= remat */
2194 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2195 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2196 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2197 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2202 /* if value is not an arg of op and not possibly defined by post remat
2203 * then it may only die and not become live
2205 if(!set_find_keyval(args, arg)) {
2206 /* post_use <= prev_lr */
2207 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2208 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2209 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2210 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2212 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2213 /* next_lr <= prev_lr */
2214 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2215 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2216 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2217 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2221 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2222 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2223 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2224 ilp_var_t memoperand;
2226 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2227 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2228 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2230 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2231 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2233 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2234 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2239 /* new live range begins for each used value */
2240 arg_op->attr.live_range.ilp = prev_lr;
2241 arg_op->attr.live_range.op = irn;
2243 pset_insert_ptr(live, arg);
2246 /* just to be sure */
2247 check_post = ILP_UNDEF;
2249 /* allow original defintions to be removed */
2250 if(opt_repair_schedule) {
2251 pset_foreach(defs, tmp) {
2252 op_t *tmp_op = get_irn_link(tmp);
2253 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2258 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2259 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0);
2261 /* op may not be killed if its first live_range is 1 */
2262 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2263 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2264 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2265 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2267 /* op may not be killed if it is spilled after the definition */
2268 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2269 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2270 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2271 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2276 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2277 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0);
2279 /* op may not be killed if its first live_range is 1 */
2280 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2281 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2282 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2283 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2285 /* op may not be killed if it is spilled after the definition */
2286 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2287 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2288 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2289 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2294 pset_foreach(defs, tmp) {
2295 op_t *tmp_op = get_irn_link(tmp);
2296 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2299 /* live_range or spill should be 1
2300 TODO: lr should be live until first use */
2301 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2302 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2303 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2304 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2314 /* check the register pressure in the prolog */
2315 /* sum_{L\U} lr <= k - |U| */
2316 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2317 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2319 /* for the prolog remove defined values from the live set */
2320 pset_foreach(defs, tmp) {
2321 pset_remove_ptr(live, tmp);
2324 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2325 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2326 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2329 /***********************************************************
2330 * I T E R A T I O N O V E R A R G S F O R P R O L O G
2331 **********************************************************/
2334 set_foreach(args, keyval) {
2336 const ir_node *arg = keyval->key;
2337 int i = PTR_TO_INT(keyval->val);
2338 op_t *arg_op = get_irn_link(arg);
2339 ilp_cst_t requirements;
2342 spill = set_find_spill(spill_bb->ilp, arg);
2345 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2346 op->attr.live_range.args.reloads[i] = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
2348 /* reload <= mem_out */
2349 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2350 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2351 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2352 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2354 /* requirement: arg must be in register for use */
2355 /* reload + remat + live_range == 1 */
2356 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2357 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2359 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2360 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2361 foreach_pre_remat(si, irn, tmp) {
2362 op_t *remat_op = get_irn_link(tmp);
2363 if(remat_op->attr.remat.remat->value == arg) {
2364 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2368 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2370 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2371 if(get_irn_n(irn, n) == arg) {
2375 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2376 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2377 memoperand_t *memoperand;
2378 memoperand = set_find_memoperand(si->memoperands, irn, n);
2380 /* memoperand <= mem_out */
2381 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2382 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2383 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2384 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2386 /* the memoperand is only sufficient if it is used once by the op */
2387 if(n_memoperands == 1)
2388 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2390 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2392 /* we have one more free register if we use a memory operand */
2393 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2399 /* iterate over L\U */
2400 pset_foreach(live, tmp) {
2401 if(!set_find_keyval(args, tmp)) {
2402 /* if a live value is not used by irn */
2403 tmp_op = get_irn_link(tmp);
2404 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2408 /* requirements for remats */
2409 foreach_pre_remat(si, irn, tmp) {
2410 op_t *remat_op = get_irn_link(tmp);
2413 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2414 ir_node *remat_arg = get_irn_n(tmp, n);
2415 op_t *arg_op = get_irn_link(remat_arg);
2417 if(!has_reg_class(si, remat_arg)) continue;
2419 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2420 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2421 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2423 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2424 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2426 /* if remat arg is also used by current op then we can use reload placed for this argument */
2427 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2428 int index = (int)keyval->val;
2430 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2438 /*************************
2439 * D O N E W I T H O P
2440 *************************/
2442 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2444 pset_foreach(live, tmp) {
2445 assert(has_reg_class(si, tmp));
2448 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2449 ir_node *arg = get_irn_n(irn, n);
2451 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2454 del_pset(remat_defs);
2458 defs = pset_new_ptr_default();
2460 /* skip everything above barrier in start block */
2461 if(is_start_block(bb) && be_is_Barrier(irn)) {
2462 assert(pset_count(live) == 0);
2471 /***************************************
2472 * B E G I N N I N G O F B L O C K
2473 ***************************************/
2476 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2477 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2479 pset_foreach(live, irn) {
2480 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2483 /* construct mem_outs for all values */
2484 set_foreach(spill_bb->ilp, spill) {
2485 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2486 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2488 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2489 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2491 if(pset_find_ptr(live, spill->irn)) {
2492 int default_spilled;
2493 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2495 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2496 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2497 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2498 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2500 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2502 op_t *op = get_irn_link(spill->irn);
2504 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2505 const ir_node *arg = get_irn_n(spill->irn, n);
2511 /* argument already done? */
2512 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2514 /* get sum of execution frequencies of blocks with the same phi argument */
2515 for(m=n; m>=0; --m) {
2516 const ir_node *arg2 = get_irn_n(spill->irn, m);
2519 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2523 /* copies are not for free */
2524 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2525 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2527 for(m=n; m>=0; --m) {
2528 const ir_node *arg2 = get_irn_n(spill->irn, m);
2531 op->attr.live_range.args.copies[m] = var;
2536 /* copy <= mem_in */
2537 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2538 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2539 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2540 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2547 foreach_post_remat(bb, tmp) {
2549 op_t *remat_op = get_irn_link(tmp);
2550 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2553 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2554 remat_arg = get_irn_n(tmp, n);
2556 if(has_reg_class(si, remat_arg)) {
2557 pset_insert_ptr(remat_args, remat_arg);
2561 /* remat + \sum live_range(remat_arg) <= |args| */
2562 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2563 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2564 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2566 pset_foreach(remat_args, remat_arg) {
2567 if(pset_find_ptr(live, remat_arg)) {
2568 op_t *remat_arg_op = get_irn_link(remat_arg);
2569 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2572 del_pset(remat_args);
2575 foreach_post_remat(bb, tmp) {
2578 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2579 ir_node *remat_arg = get_irn_n(tmp, n);
2581 /* if value is becoming live through use by remat2 */
2582 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2583 op_t *remat_arg_op = get_irn_link(remat_arg);
2586 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2588 pset_insert_ptr(live, remat_arg);
2589 spill = add_to_spill_bb(si, bb, remat_arg);
2590 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2592 /* we need reg_in and mem_in for this value; they will be referenced later */
2593 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2594 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2595 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2596 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2599 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2600 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2601 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2602 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2607 /* L\U is empty at bb start */
2608 /* arg is live throughout epilog if it is reg_in into this block */
2610 /* check the register pressure at the beginning of the block
2613 /* reg_in entspricht post_use */
2615 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2616 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2618 pset_foreach(live, irn) {
2621 spill = set_find_spill(spill_bb->ilp, irn);
2624 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2625 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2627 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2629 /* spill + mem_in <= 1 */
2630 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2631 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2633 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2634 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2636 } /* post_remats are NOT included in register pressure check because
2637 they do not increase regpressure */
2639 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2640 pset_foreach(live, irn) {
2644 spill = set_find_spill(spill_bb->ilp, irn);
2645 assert(spill && spill->irn == irn);
2647 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2648 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2651 ir_node *phi_arg = get_Phi_pred(irn, n);
2652 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2653 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2655 op_t *op = get_irn_link(irn);
2657 /* although the phi is in the right regclass one or more of
2658 * its arguments can be in a different one or at least to
2661 if(has_reg_class(si, phi_arg)) {
2662 /* mem_in < mem_out_arg + copy */
2663 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2664 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2666 /* reg_in < reg_out_arg */
2667 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2668 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2670 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2671 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2673 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2676 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2678 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2680 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2684 /* else assure the value arrives on all paths in the same resource */
2686 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2689 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2690 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2693 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2694 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2695 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2696 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2698 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2699 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2701 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2704 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2705 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2710 foreach_post_remat(bb, tmp) {
2713 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2714 ir_node *remat_arg = get_irn_n(tmp, n);
2715 op_t *remat_op = get_irn_link(tmp);
2717 if(!has_reg_class(si, remat_arg)) continue;
2719 spill = set_find_spill(spill_bb->ilp, remat_arg);
2722 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2723 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2724 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2725 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2729 pset_foreach(live, irn) {
2730 const op_t *op = get_irn_link(irn);
2731 const ir_node *remat;
2736 foreach_post_remat(bb, remat) {
2739 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2740 const ir_node *arg = get_irn_n(remat, n);
2743 const op_t *remat_op = get_irn_link(remat);
2745 if(cst == ILP_UNDEF) {
2746 /* sum remat2s <= 1 + n_remats*live_range */
2747 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2748 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2750 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2756 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2757 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2761 /* first live ranges from reg_ins */
2762 pset_foreach(live, irn) {
2763 op_t *op = get_irn_link(irn);
2765 if(op->attr.live_range.ilp != ILP_UNDEF) {
2767 spill = set_find_spill(spill_bb->ilp, irn);
2768 assert(spill && spill->irn == irn);
2770 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2771 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2772 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2773 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2775 foreach_post_remat(bb, tmp) {
2776 op_t *remat_op = get_irn_link(tmp);
2778 if(remat_op->attr.remat.remat->value == irn) {
2779 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2785 /* walk forward now and compute constraints for placing spills */
2786 /* this must only be done for values that are not defined in this block */
2787 pset_foreach(live, irn) {
2789 * if value is defined in this block we can anways place the spill directly after the def
2790 * -> no constraint necessary
2792 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2797 spill = set_find_spill(spill_bb->ilp, irn);
2800 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2801 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2803 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2804 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2807 sched_foreach_op(bb, tmp) {
2808 op_t *op = get_irn_link(tmp);
2810 if(is_Phi(tmp)) continue;
2811 assert(!is_Proj(tmp));
2814 const ir_node *value = op->attr.remat.remat->value;
2817 /* only collect remats up to the first real use of a value */
2818 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2823 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2824 ir_node *arg = get_irn_n(tmp, n);
2827 /* if a value is used stop collecting remats */
2840 typedef struct _irnlist_t {
2841 struct list_head list;
2845 typedef struct _interference_t {
2846 struct list_head blocklist;
2852 cmp_interference(const void *a, const void *b, size_t size)
2854 const interference_t *p = a;
2855 const interference_t *q = b;
2857 return !(p->a == q->a && p->b == q->b);
2860 static interference_t *
2861 set_find_interference(set * set, ir_node * a, ir_node * b)
2863 interference_t query;
2865 query.a = (a>b)?a:b;
2866 query.b = (a>b)?b:a;
2868 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2871 static interference_t *
2872 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2874 interference_t query,
2876 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2880 result = set_find_interference(set, a, b);
2883 list_add(&list->list, &result->blocklist);
2887 query.a = (a>b)?a:b;
2888 query.b = (a>b)?b:a;
2890 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2892 INIT_LIST_HEAD(&result->blocklist);
2893 list_add(&list->list, &result->blocklist);
2899 values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_node * a, const ir_node * b)
2901 const ir_edge_t *edge;
2903 if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2904 /* both values are live in, so they interfere */
2908 /* ensure a dominates b */
2909 if(value_dominates(b,a)) {
2915 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2918 /* the following code is stolen from bera.c */
2919 if(be_is_live_end(si->lv, bb, a))
2922 foreach_out_edge(a, edge) {
2923 const ir_node *user = edge->src;
2924 if(get_nodes_block(user) == bb
2927 && !pset_find_ptr(si->inverse_ops, user)
2928 && value_dominates(b, user))
2936 * Walk all irg blocks and collect interfering values inside of phi classes
2939 luke_interferencewalker(ir_node * bb, void * data)
2941 spill_ilp_t *si = (spill_ilp_t*)data;
2944 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2945 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2946 op_t *a_op = get_irn_link(a);
2949 /* a is only interesting if it is in my register class and if it is inside a phi class */
2950 if (has_reg_class(si, a) && get_phi_class(a)) {
2951 if(a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2954 for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) {
2955 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2956 op_t *b_op = get_irn_link(b);
2959 /* a and b are only interesting if they are in the same phi class */
2960 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2961 if(b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2964 if(values_interfere_in_block(si, bb, a, b)) {
2965 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2966 set_insert_interference(si, si->interferences, a, b, bb);
2974 static unsigned int copy_path_id = 0;
2977 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2984 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2985 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
2987 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2989 pset_foreach(copies, ptr) {
2990 copy = PTR_TO_INT(ptr);
2991 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2996 * @parameter copies contains a path of copies which lead us to irn
2997 * @parameter visited contains a set of nodes already visited on this path
3000 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3002 const ir_edge_t *edge;
3003 op_t *op = get_irn_link(irn);
3004 pset *visited_users = pset_new_ptr_default();
3007 if(op->is_remat) return 0;
3009 pset_insert_ptr(visited, irn);
3013 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3015 /* visit all operands */
3016 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3017 ir_node *arg = get_irn_n(irn, n);
3018 ilp_var_t copy = op->attr.live_range.args.copies[n];
3020 if(!has_reg_class(si, arg)) continue;
3021 if(pset_find_ptr(visited_operands, arg)) continue;
3022 pset_insert_ptr(visited_operands, arg);
3025 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3026 del_pset(visited_operands);
3027 del_pset(visited_users);
3028 pset_remove_ptr(visited, irn);
3031 pset_insert(copies, INT_TO_PTR(copy), copy);
3032 write_copy_path_cst(si, copies, any_interfere);
3033 pset_remove(copies, INT_TO_PTR(copy), copy);
3034 } else if(!pset_find_ptr(visited, arg)) {
3035 pset_insert(copies, INT_TO_PTR(copy), copy);
3036 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3037 pset_remove(copies, INT_TO_PTR(copy), copy);
3039 if(paths > MAX_PATHS) {
3040 if(pset_count(copies) == 0) {
3044 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3045 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3046 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3047 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3048 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3052 del_pset(visited_operands);
3053 del_pset(visited_users);
3054 pset_remove_ptr(visited, irn);
3057 } else if(pset_count(copies) == 0) {
3063 del_pset(visited_operands);
3066 /* visit all uses which are phis */
3067 foreach_out_edge(irn, edge) {
3068 ir_node *user = edge->src;
3069 int pos = edge->pos;
3070 op_t *op = get_irn_link(user);
3073 if(!is_Phi(user)) continue;
3074 if(!has_reg_class(si, user)) continue;
3075 if(pset_find_ptr(visited_users, user)) continue;
3076 pset_insert_ptr(visited_users, user);
3078 copy = op->attr.live_range.args.copies[pos];
3080 if(user == target) {
3081 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3082 del_pset(visited_users);
3083 pset_remove_ptr(visited, irn);
3086 pset_insert(copies, INT_TO_PTR(copy), copy);
3087 write_copy_path_cst(si, copies, any_interfere);
3088 pset_remove(copies, INT_TO_PTR(copy), copy);
3089 } else if(!pset_find_ptr(visited, user)) {
3090 pset_insert(copies, INT_TO_PTR(copy), copy);
3091 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3092 pset_remove(copies, INT_TO_PTR(copy), copy);
3094 if(paths > MAX_PATHS) {
3095 if(pset_count(copies) == 0) {
3099 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3100 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3101 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3102 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3103 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3107 del_pset(visited_users);
3108 pset_remove_ptr(visited, irn);
3111 } else if(pset_count(copies) == 0) {
3117 del_pset(visited_users);
3118 pset_remove_ptr(visited, irn);
3123 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3125 pset * copies = pset_new_ptr_default();
3126 pset * visited = pset_new_ptr_default();
3128 find_copy_path(si, a, b, any_interfere, copies, visited);
3136 memcopyhandler(spill_ilp_t * si)
3138 interference_t *interference;
3140 /* teste Speicherwerte auf Interferenz */
3142 /* analyze phi classes */
3143 phi_class_compute(si->chordal_env->irg);
3145 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3146 irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
3148 /* now lets emit the ILP unequations for the crap */
3149 set_foreach(si->interferences, interference) {
3151 ilp_var_t interfere,
3153 ilp_cst_t any_interfere_cst,
3155 const ir_node *a = interference->a;
3156 const ir_node *b = interference->b;
3158 /* any_interf <= \sum interf */
3159 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3160 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3161 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3163 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3165 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3166 const ir_node *bb = irnlist->irn;
3167 spill_bb_t *spill_bb = get_irn_link(bb);
3172 spilla = set_find_spill(spill_bb->ilp, a);
3175 spillb = set_find_spill(spill_bb->ilp, b);
3178 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3179 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3180 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3181 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3182 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3183 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3185 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3186 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3188 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3189 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3190 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3191 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3192 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3194 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3195 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3197 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3198 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3199 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3201 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3202 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3204 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3205 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3206 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3209 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3211 /* any_interfere >= interf */
3212 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3213 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3215 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3216 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3219 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3220 gen_copy_constraints(si,a,b,any_interfere);
3228 return fabs(x) < 0.00001;
3231 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3233 spill_ilp_t *si = get_irg_link(current_ir_graph);
3235 if(pset_find_ptr(si->all_possible_remats, n)) {
3236 op_t *op = (op_t*)get_irn_link(n);
3237 assert(op && op->is_remat);
3239 if(!op->attr.remat.remat->inverse) {
3240 if(op->attr.remat.pre) {
3241 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3243 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3248 op_t *op = (op_t*)get_irn_link(n);
3249 assert(op && op->is_remat);
3251 if(op->attr.remat.pre) {
3252 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3254 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3265 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3267 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3268 be_dump(irg, suffix, dump_ir_block_graph_sched);
3269 set_dump_node_vcgattr_hook(NULL);
3273 * Edge hook to dump the schedule edges with annotated register pressure.
3276 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3278 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3279 ir_node *prev = sched_prev(irn);
3280 fprintf(F, "edge:{sourcename:\"");
3282 fprintf(F, "\" targetname:\"");
3284 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3285 fprintf(F, "\" color:magenta}\n");
3291 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3293 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3295 dump_consts_local(0);
3296 set_dump_node_edge_hook(sched_pressure_edge_hook);
3297 dump_ir_block_graph(irg, suffix);
3298 set_dump_node_edge_hook(old_edge_hook);
3302 walker_pressure_annotator(ir_node * bb, void * data)
3304 spill_ilp_t *si = data;
3307 pset *live = pset_new_ptr_default();
3310 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3311 irn = be_lv_get_irn(si->lv, bb, i);
3313 if (has_reg_class(si, irn)) {
3314 pset_insert_ptr(live, irn);
3318 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3320 sched_foreach_reverse(bb, irn) {
3322 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3326 if(has_reg_class(si, irn)) {
3327 pset_remove_ptr(live, irn);
3328 if(is_Proj(irn)) ++projs;
3331 if(!is_Proj(irn)) projs = 0;
3333 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3334 ir_node *arg = get_irn_n(irn, n);
3336 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3338 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3345 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3347 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
3351 connect_all_remats_with_keep(spill_ilp_t * si)
3359 n_remats = pset_count(si->all_possible_remats);
3361 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3364 pset_foreach(si->all_possible_remats, irn) {
3369 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
3371 obstack_free(si->obst, ins);
3376 connect_all_spills_with_keep(spill_ilp_t * si)
3385 n_spills = pset_count(si->spills);
3387 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3390 pset_foreach(si->spills, irn) {
3395 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
3397 obstack_free(si->obst, ins);
3401 /** insert a spill at an arbitrary position */
3402 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert)
3404 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
3405 ir_graph *irg = get_irn_irg(bl);
3406 ir_node *frame = get_irg_frame(irg);
3410 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
3411 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
3413 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
3416 * search the right insertion point. a spill of a phi cannot be put
3417 * directly after the phi, if there are some phis behind the one which
3418 * is spilled. Also, a spill of a Proj must be after all Projs of the
3421 * Here's one special case:
3422 * If the spill is in the start block, the spill must be after the frame
3423 * pointer is set up. This is done by setting insert to the end of the block
3424 * which is its default initialization (see above).
3427 if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3430 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3433 sched_add_after(insert, spill);
3438 delete_remat(spill_ilp_t * si, ir_node * remat) {
3440 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3442 sched_remove(remat);
3444 /* kill links to operands */
3445 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3446 set_irn_n(remat, n, bad);
3451 clean_remat_info(spill_ilp_t * si)
3455 remat_info_t *remat_info;
3456 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3458 set_foreach(si->remat_info, remat_info) {
3459 if(!remat_info->remats) continue;
3461 pset_foreach(remat_info->remats, remat)
3463 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3464 if(sched_is_scheduled(remat->proj)) {
3465 sched_remove((ir_node*)remat->proj);
3467 set_irn_n((ir_node*)remat->proj, -1, bad);
3468 set_irn_n((ir_node*)remat->proj, 0, bad);
3471 if(get_irn_n_edges(remat->op) == 0) {
3472 if(sched_is_scheduled(remat->op)) {
3473 sched_remove((ir_node*)remat->op);
3475 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3476 set_irn_n((ir_node*)remat->op, n, bad);
3481 if(remat_info->remats) del_pset(remat_info->remats);
3482 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3487 delete_unnecessary_remats(spill_ilp_t * si)
3489 if(opt_keep_alive & KEEPALIVE_REMATS) {
3491 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3494 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3495 ir_node *keep_arg = get_irn_n(si->keep, n);
3496 op_t *arg_op = get_irn_link(keep_arg);
3499 assert(arg_op->is_remat);
3501 name = si->lpp->vars[arg_op->attr.remat.ilp];
3503 if(is_zero(name->value)) {
3504 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3505 /* TODO check whether reload is preferred over remat (could be bug) */
3506 delete_remat(si, keep_arg);
3508 if(!arg_op->attr.remat.remat->inverse) {
3509 if(arg_op->attr.remat.pre) {
3510 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3512 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3515 if(arg_op->attr.remat.pre) {
3516 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3518 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3523 set_irn_n(si->keep, n, bad);
3526 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3531 pset_foreach(si->all_possible_remats, remat) {
3532 op_t *remat_op = get_irn_link(remat);
3533 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3535 if(is_zero(name->value)) {
3536 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3537 /* TODO check whether reload is preferred over remat (could be bug) */
3538 delete_remat(si, remat);
3540 if(!remat_op->attr.remat.remat->inverse) {
3541 if(remat_op->attr.remat.pre) {
3542 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3544 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3547 if(remat_op->attr.remat.pre) {
3548 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3550 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3559 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3561 pset *spills = pset_new_ptr_default();
3563 const ir_node *next;
3566 defs = set_find_def(si->values, value);
3568 if(defs && defs->spills) {
3569 for(next = defs->spills; next; next = get_irn_link(next)) {
3570 pset_insert_ptr(spills, next);
3578 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3582 assert( get_irn_arity(block) == arity );
3584 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3585 res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
3591 * @param before The node after which the spill will be placed in the schedule
3594 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3598 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3600 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3602 spill = be_spill2(arch_env, irn, before);
3604 defs = set_insert_def(si->values, value);
3607 /* enter into the linked list */
3608 set_irn_link(spill, defs->spills);
3609 defs->spills = spill;
3611 if(opt_keep_alive & KEEPALIVE_SPILLS)
3612 pset_insert_ptr(si->spills, spill);
3618 * @param before The Phi node which has to be spilled
3621 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3628 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3630 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3631 ins[n] = si->m_unknown;
3634 mem_phi = new_r_PhiM_nokeep(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3636 defs = set_insert_def(si->values, phi);
3639 /* enter into the linked list */
3640 set_irn_link(mem_phi, defs->spills);
3641 defs->spills = mem_phi;
3643 #ifdef SCHEDULE_PHIM
3644 sched_add_after(phi, mem_phi);
3646 pset_insert_ptr(si->phims, mem_phi);
3649 if(opt_keep_alive & KEEPALIVE_SPILLS)
3650 pset_insert_ptr(si->spills, mem_phi);
3657 * Add remat to list of defs, destroys link field!
3660 insert_remat(spill_ilp_t * si, ir_node * remat)
3663 op_t *remat_op = get_irn_link(remat);
3665 assert(remat_op->is_remat);
3667 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3670 /* enter into the linked list */
3671 set_irn_link(remat, defs->remats);
3672 defs->remats = remat;
3677 * Add reload before operation and add to list of defs
3680 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3685 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3687 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3689 defs = set_find_def(si->values, value);
3691 spill = defs->spills;
3692 assert(spill && "no spill placed before reload");
3694 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3696 /* enter into the linked list */
3697 set_irn_link(reload, defs->remats);
3698 defs->remats = reload;
3703 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3706 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3708 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3710 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3712 defs = set_find_def(si->values, value);
3714 spill = defs->spills;
3715 assert(spill && "no spill placed before reload");
3717 arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos);
3720 void insert_memoperands(spill_ilp_t * si)
3722 memoperand_t *memoperand;
3725 set_foreach(si->memoperands, memoperand) {
3726 name = si->lpp->vars[memoperand->ilp];
3727 if(!is_zero(name->value)) {
3728 perform_memory_operand(si, memoperand);
3734 walker_spill_placer(ir_node * bb, void * data) {
3735 spill_ilp_t *si = (spill_ilp_t*)data;
3737 spill_bb_t *spill_bb = get_irn_link(bb);
3738 pset *spills_to_do = pset_new_ptr_default();
3741 set_foreach(spill_bb->ilp, spill) {
3744 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3745 name = si->lpp->vars[spill->mem_in];
3746 if(!is_zero(name->value)) {
3749 mem_phi = insert_mem_phi(si, spill->irn);
3751 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3755 name = si->lpp->vars[spill->spill];
3756 if(!is_zero(name->value)) {
3757 /* place spill directly after definition */
3758 if(get_nodes_block(spill->irn) == bb) {
3759 insert_spill(si, spill->irn, spill->irn, spill->irn);
3763 /* place spill at bb start */
3764 if(spill->reg_in > 0) {
3765 name = si->lpp->vars[spill->reg_in];
3766 if(!is_zero(name->value)) {
3767 insert_spill(si, spill->irn, spill->irn, bb);
3771 /* place spill after a remat */
3772 pset_insert_ptr(spills_to_do, spill->irn);
3775 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3778 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3779 op_t *op = get_irn_link(irn);
3781 if(be_is_Spill(irn)) continue;
3784 /* TODO fix this if we want to support remats with more than two nodes */
3785 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3786 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3788 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3791 if(pset_find_ptr(spills_to_do, irn)) {
3792 pset_remove_ptr(spills_to_do, irn);
3794 insert_spill(si, irn, irn, irn);
3800 assert(pset_count(spills_to_do) == 0);
3802 /* afterwards free data in block */
3803 del_pset(spills_to_do);
3807 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3809 ir_node *insert_pos = bb;
3811 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3813 /* find last definition of arg value in block */
3818 defs = set_find_def(si->values, value);
3820 if(defs && defs->remats) {
3821 for(next = defs->remats; next; next = get_irn_link(next)) {
3822 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3823 last = sched_get_time_step(next);
3829 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3830 last = sched_get_time_step(value);
3834 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3836 spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos);
3842 phim_fixer(spill_ilp_t *si) {
3845 set_foreach(si->values, defs) {
3846 const ir_node *phi = defs->value;
3847 op_t *op = get_irn_link(phi);
3848 ir_node *phi_m = NULL;
3849 ir_node *next = defs->spills;
3852 if(!is_Phi(phi)) continue;
3855 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3859 next = get_irn_link(next);
3862 if(!phi_m) continue;
3864 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3865 ir_node *value = get_irn_n(phi, n);
3866 defs_t *val_defs = set_find_def(si->values, value);
3868 /* a spill of this value */
3873 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3874 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3876 if(!is_zero(name->value)) {
3877 spill = insert_mem_copy(si, pred, value);
3879 spill = val_defs->spills;
3882 spill = val_defs->spills;
3885 assert(spill && "no spill placed before PhiM");
3886 set_irn_n(phi_m, n, spill);
3892 walker_reload_placer(ir_node * bb, void * data) {
3893 spill_ilp_t *si = (spill_ilp_t*)data;
3895 spill_bb_t *spill_bb = get_irn_link(bb);
3897 /* reloads at end of block */
3898 if(spill_bb->reloads) {
3901 set_foreach(spill_bb->reloads, keyval) {
3902 ir_node *irn = (ir_node*)keyval->key;
3903 ilp_var_t reload = PTR_TO_INT(keyval->val);
3906 name = si->lpp->vars[reload];
3907 if(!is_zero(name->value)) {
3909 ir_node *insert_pos = bb;
3910 ir_node *prev = sched_block_last_noncf(si, bb);
3911 op_t *prev_op = get_irn_link(prev);
3913 while(be_is_Spill(prev)) {
3914 prev = sched_prev(prev);
3917 prev_op = get_irn_link(prev);
3919 /* insert reload before pre-remats */
3920 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3921 && prev_op->is_remat && prev_op->attr.remat.pre) {
3925 prev = sched_prev(prev);
3926 } while(be_is_Spill(prev));
3928 prev_op = get_irn_link(prev);
3932 reload = insert_reload(si, irn, insert_pos);
3934 if(opt_keep_alive & KEEPALIVE_RELOADS)
3935 pset_insert_ptr(si->spills, reload);
3940 /* walk and insert more reloads and collect remats */
3941 sched_foreach_reverse(bb, irn) {
3942 op_t *op = get_irn_link(irn);
3944 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3945 if(is_Phi(irn)) break;
3948 if(get_irn_mode(irn) != mode_T) {
3949 insert_remat(si, irn);
3954 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3955 ir_node *arg = get_irn_n(irn, n);
3957 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3960 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3961 if(!is_zero(name->value)) {
3963 ir_node *insert_pos = irn;
3964 ir_node *prev = sched_prev(insert_pos);
3967 while(be_is_Spill(prev)) {
3968 prev = sched_prev(prev);
3971 prev_op = get_irn_link(prev);
3973 /* insert reload before pre-remats */
3974 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3975 && prev_op->is_remat && prev_op->attr.remat.pre) {
3979 prev = sched_prev(prev);
3980 } while(be_is_Spill(prev));
3982 prev_op = get_irn_link(prev);
3986 reload = insert_reload(si, arg, insert_pos);
3988 assert(reload && "no reload returned");
3989 set_irn_n(irn, n, reload);
3991 if(opt_keep_alive & KEEPALIVE_RELOADS)
3992 pset_insert_ptr(si->spills, reload);
3999 del_set(spill_bb->ilp);
4000 if(spill_bb->reloads) del_set(spill_bb->reloads);
4004 walker_collect_used(ir_node * irn, void * data)
4006 bitset_t *used = data;
4008 bitset_set(used, get_irn_idx(irn));
4011 struct kill_helper {
4017 walker_kill_unused(ir_node * bb, void * data)
4019 struct kill_helper *kh = data;
4020 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4024 for(irn=sched_first(bb); !sched_is_end(irn);) {
4025 ir_node *next = sched_next(irn);
4028 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4029 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4030 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(kh->si, bb)));
4032 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4038 set_nodes_block(irn, bad);
4039 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4040 set_irn_n(irn, n, bad);
4047 #ifndef SCHEDULE_PHIM
4049 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4052 ir_node *bad = get_irg_bad(si->chordal_env->irg);
4055 pset_foreach(si->phims, phi) {
4056 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4058 set_nodes_block(phi, bad);
4059 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4060 set_irn_n(phi, n, bad);
4068 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4070 struct kill_helper kh;
4072 kh.used = bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
4075 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
4076 #ifndef SCHEDULE_PHIM
4077 kill_unused_phims(si, &kh);
4079 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
4081 bitset_free(kh.used);
4085 print_irn_pset(pset * p)
4089 pset_foreach(p, irn) {
4090 ir_printf("%+F\n", irn);
4095 dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file)
4097 FILE *f = fopen(file, "w");
4099 interference_t *interference;
4101 pset_break(phiclass);
4102 set_break(si->interferences);
4104 ir_fprintf(f, "digraph phiclass {\n");
4106 pset_foreach(phiclass, irn) {
4108 ir_fprintf(f, " %F%N [shape=box]\n",irn,irn);
4111 pset_foreach(phiclass, irn) {
4114 if(!is_Phi(irn)) continue;
4116 for(n=get_irn_arity(irn)-1; n>=0; --n) {
4117 ir_node *arg = get_irn_n(irn, n);
4119 ir_fprintf(f, " %F%N -> %F%N\n",irn,irn,arg,arg);
4123 set_foreach(si->interferences, interference) {
4124 const ir_node *a = interference->a;
4125 const ir_node *b = interference->b;
4126 if(get_phi_class(a) == phiclass) {
4127 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n",a,a,b,b);
4136 rewire_uses(spill_ilp_t * si)
4138 dom_front_info_t *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
4140 pset *ignore = pset_new_ptr(1);
4142 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
4144 /* then fix uses of spills */
4145 set_foreach(si->values, defs) {
4148 const ir_node *next = defs->remats;
4151 reloads = pset_new_ptr_default();
4154 if(be_is_Reload(next)) {
4155 pset_insert_ptr(reloads, next);
4159 next = get_irn_link(next);
4162 spills = get_spills_for_value(si, defs->value);
4163 DBG((si->dbg, LEVEL_2, "\t %d remats, %d reloads, and %d spills for value %+F\n", remats, pset_count(reloads), pset_count(spills), defs->value));
4164 if(pset_count(spills) > 1) {
4165 //assert(pset_count(reloads) > 0);
4166 // print_irn_pset(spills);
4167 // print_irn_pset(reloads);
4169 be_ssa_constr_set_ignore(dfi, si->lv, spills, ignore);
4176 /* first fix uses of remats and reloads */
4177 set_foreach(si->values, defs) {
4179 const ir_node *next = defs->remats;
4183 nodes = pset_new_ptr_default();
4184 if(sched_is_scheduled(defs->value)) {
4185 pset_insert_ptr(nodes, defs->value);
4190 pset_insert_ptr(nodes, next);
4191 next = get_irn_link(next);
4194 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-orig_kept, defs->value));
4195 be_ssa_constr_set(dfi, si->lv, nodes);
4201 // remove_unused_defs(si);
4203 be_free_dominance_frontiers(dfi);
4208 writeback_results(spill_ilp_t * si)
4210 /* walk through the graph and collect all spills, reloads and remats for a value */
4212 si->values = new_set(cmp_defs, 4096);
4214 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4215 delete_unnecessary_remats(si);
4216 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
4217 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
4218 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
4220 insert_memoperands(si);
4223 /* clean the remat info! there are still back-edges leading there! */
4224 clean_remat_info(si);
4228 connect_all_spills_with_keep(si);
4230 del_set(si->values);
4234 get_n_regs(spill_ilp_t * si)
4236 int arch_n_regs = arch_register_class_n_regs(si->cls);
4238 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4239 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4241 arch_put_non_ignore_regs(si->chordal_env->birg->main_env->arch_env, si->cls, arch_regs);
4242 be_abi_put_ignore_regs(si->chordal_env->birg->abi, si->cls, abi_regs);
4244 bitset_andnot(arch_regs, abi_regs);
4245 arch_n_regs = bitset_popcnt(arch_regs);
4247 bitset_free(arch_regs);
4248 bitset_free(abi_regs);
4250 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4255 walker_reload_mover(ir_node * bb, void * data)
4257 spill_ilp_t *si = data;
4260 sched_foreach(bb, tmp) {
4261 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4262 ir_node *reload = tmp;
4265 /* move reload upwards */
4267 int pressure = (int)get_irn_link(reload);
4268 if(pressure < si->n_regs) {
4269 irn = sched_prev(reload);
4270 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4271 sched_remove(reload);
4272 pressure = (int)get_irn_link(irn);
4274 while(pressure < si->n_regs) {
4275 if( sched_is_end(irn) ||
4276 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4277 /* do not move reload before its spill */
4278 (irn == be_get_Reload_mem(reload)) ||
4279 /* do not move before phi */
4282 set_irn_link(irn, INT_TO_PTR(pressure+1));
4283 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4284 irn = sched_prev(irn);
4286 pressure = (int)get_irn_link(irn);
4289 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4290 sched_put_after(irn, reload);
4297 move_reloads_upward(spill_ilp_t * si)
4299 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
4304 * Walk all irg blocks and check for interfering spills inside of phi classes
4307 luke_meminterferencechecker(ir_node * bb, void * data)
4309 spill_ilp_t *si = (spill_ilp_t*)data;
4312 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4313 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4315 if(!be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) continue;
4317 /* a is only interesting if it is in my register class and if it is inside a phi class */
4318 if (has_reg_class(si, a) && get_phi_class(a)) {
4319 for(l2=_be_lv_next_irn(si->lv, bb, 0xff, l1+1); l2>=0; l2=_be_lv_next_irn(si->lv, bb, 0xff, l2+1)) {
4320 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4322 if(!be_is_Spill(b) && (!is_Phi(b) || get_irn_mode(b) != mode_T)) continue;
4324 /* a and b are only interesting if they are in the same phi class */
4325 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
4326 if(values_interfere_in_block(si, bb, a, b)) {
4327 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4336 verify_phiclasses(spill_ilp_t * si)
4338 /* analyze phi classes */
4339 phi_class_compute(si->chordal_env->irg);
4341 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4342 irg_block_walk_graph(si->chordal_env->irg, luke_meminterferencechecker, NULL, si);
4346 be_spill_remat(const be_chordal_env_t * chordal_env)
4349 char problem_name[256];
4350 char dump_suffix[256];
4351 char dump_suffix2[256];
4352 struct obstack obst;
4355 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
4356 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
4357 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
4359 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4360 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4362 if(opt_verify & VERIFY_DOMINANCE)
4363 be_check_dominance(chordal_env->irg);
4365 obstack_init(&obst);
4366 si.chordal_env = chordal_env;
4368 si.cls = chordal_env->cls;
4369 si.lpp = new_lpp(problem_name, lpp_minimize);
4370 si.remat_info = new_set(cmp_remat_info, 4096);
4371 si.interferences = new_set(cmp_interference, 32);
4372 si.memoperands = new_set(cmp_memoperands, 128);
4373 si.all_possible_remats = pset_new_ptr_default();
4374 si.spills = pset_new_ptr_default();
4375 si.inverse_ops = pset_new_ptr_default();
4376 si.lv = chordal_env->lv;
4378 si.n_regs = get_n_regs(&si);
4380 set_irg_link(chordal_env->irg, &si);
4381 compute_doms(chordal_env->irg);
4383 /* compute phi classes */
4384 // phi_class_compute(chordal_env->irg);
4386 if(opt_dump_flags & DUMP_STATS)
4387 be_analyze_regpressure(chordal_env, "-pre");
4389 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4390 irg_block_walk_graph(chordal_env->irg, luke_initializer, NULL, &si);
4393 /* collect remats */
4394 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4395 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
4398 /* insert possible remats */
4399 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4400 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
4401 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4403 if(opt_keep_alive & KEEPALIVE_REMATS) {
4404 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4405 connect_all_remats_with_keep(&si);
4406 /* dump graph with inserted remats */
4407 dump_graph_with_remats(chordal_env->irg, dump_suffix);
4410 /* insert copies for phi arguments not in my regclass */
4411 irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si);
4413 /* recompute liveness */
4414 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4415 be_liveness_recompute(si.lv);
4418 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4419 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4420 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
4422 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4423 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
4426 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4427 memcopyhandler(&si);
4430 if(opt_dump_flags & DUMP_PROBLEM) {
4432 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4433 if ((f = fopen(buf, "wt")) != NULL) {
4434 lpp_dump_plain(si.lpp, f);
4439 if(opt_dump_flags & DUMP_MPS) {
4442 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4443 if((f = fopen(buf, "wt")) != NULL) {
4444 mps_write_mps(si.lpp, s_mps_fixed, f);
4448 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4449 if((f = fopen(buf, "wt")) != NULL) {
4450 mps_write_mst(si.lpp, s_mps_fixed, f);
4455 lpp_check_startvals(si.lpp);
4458 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4459 lpp_set_time_limit(si.lpp, opt_timeout);
4462 lpp_set_log(si.lpp, stdout);
4465 lpp_solve_cplex(si.lpp);
4467 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4469 assert(lpp_is_sol_valid(si.lpp)
4470 && "solution of ILP must be valid");
4472 DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g, best bound: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval, is_zero(si.lpp->best_bound)?0.0:si.lpp->best_bound));
4474 if(opt_dump_flags & DUMP_SOLUTION) {
4478 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4479 if ((f = fopen(buf, "wt")) != NULL) {
4481 for (i = 0; i < si.lpp->var_next; ++i) {
4482 lpp_name_t *name = si.lpp->vars[i];
4483 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4489 #ifndef SCHEDULE_PHIM
4490 si.phims = pset_new_ptr_default();
4492 writeback_results(&si);
4497 kill_all_unused_values_in_schedule(&si);
4499 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4503 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4504 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
4506 // move reloads upwards
4507 be_liveness_recompute(si.lv);
4508 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4509 move_reloads_upward(&si);
4512 verify_phiclasses(&si);
4515 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4517 if(opt_dump_flags & DUMP_PRESSURE)
4518 dump_pressure_graph(&si, dump_suffix2);
4520 if(opt_dump_flags & DUMP_STATS)
4521 be_analyze_regpressure(chordal_env, "-post");
4523 if(opt_verify & VERIFY_DOMINANCE)
4524 be_check_dominance(chordal_env->irg);
4526 free_dom(chordal_env->irg);
4527 del_set(si.interferences);
4528 del_pset(si.inverse_ops);
4529 del_pset(si.all_possible_remats);
4530 del_set(si.memoperands);
4531 del_pset(si.spills);
4533 obstack_free(&obst, NULL);
4534 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4537 #else /* WITH_ILP */
4540 only_that_you_can_compile_without_WITH_ILP_defined(void)
4544 #endif /* WITH_ILP */