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_init_spillremat(void)
162 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
163 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
164 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
165 lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat");
167 lc_opt_add_table(remat_grp, options);
170 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat);
174 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
175 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
178 //#define SOLVE_LOCAL
179 #define LPP_SERVER "i44pc52"
180 #define LPP_SOLVER "cplex"
183 #define MAX_PATHS INT_MAX
186 typedef struct _spill_ilp_t {
187 const arch_register_class_t *cls;
189 const be_chordal_env_t *chordal_env;
192 struct obstack *obst;
194 pset *all_possible_remats;
197 set *values; /**< for collecting all definitions of values before running ssa-construction */
202 #ifndef SCHEDULE_PHIM
205 DEBUG_ONLY(firm_dbg_module_t * dbg);
208 typedef int ilp_var_t;
209 typedef int ilp_cst_t;
211 typedef struct _spill_bb_t {
216 typedef struct _remat_t {
217 const ir_node *op; /**< for copy_irn */
218 const ir_node *value; /**< the value which is being recomputed by this remat */
219 const ir_node *proj; /**< not NULL if the above op produces a tuple */
220 int cost; /**< cost of this remat */
221 int inverse; /**< nonzero if this is an inverse remat */
225 * Data to be attached to each IR node. For remats this contains the ilp_var
226 * for this remat and for normal ops this contains the ilp_vars for
227 * reloading each operand
229 typedef struct _op_t {
234 const remat_t *remat; /** the remat this op belongs to */
235 int pre; /** 1, if this is a pressure-increasing remat */
239 ir_node *op; /** the operation this live range belongs to */
248 typedef struct _defs_t {
249 const ir_node *value;
250 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
251 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
254 typedef struct _remat_info_t {
255 const ir_node *irn; /**< the irn to which these remats belong */
256 pset *remats; /**< possible remats for this value */
257 pset *remats_by_operand; /**< remats with this value as operand */
260 typedef struct _keyval_t {
265 typedef struct _spill_t {
274 typedef struct _memoperand_t {
275 ir_node *irn; /**< the irn */
276 unsigned int pos; /**< the position of the argument */
277 ilp_var_t ilp; /**< the ilp var for this memory operand */
281 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
283 return chordal_has_class(si->chordal_env, irn);
288 cmp_remat(const void *a, const void *b)
290 const keyval_t *p = a;
291 const keyval_t *q = b;
292 const remat_t *r = p->val;
293 const remat_t *s = q->val;
297 return !(r == s || r->op == s->op);
301 cmp_remat(const void *a, const void *b)
303 const remat_t *r = a;
304 const remat_t *s = a;
306 return !(r == s || r->op == s->op);
310 cmp_spill(const void *a, const void *b, size_t size)
312 const spill_t *p = a;
313 const spill_t *q = b;
315 // return !(p->irn == q->irn && p->bb == q->bb);
316 return !(p->irn == q->irn);
320 cmp_memoperands(const void *a, const void *b, size_t size)
322 const memoperand_t *p = a;
323 const memoperand_t *q = b;
325 return !(p->irn == q->irn && p->pos == q->pos);
329 set_find_keyval(set * set, const void * key)
334 return set_find(set, &query, sizeof(query), HASH_PTR(key));
338 set_insert_keyval(set * set, void * key, void * val)
344 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
348 set_find_def(set * set, const ir_node * value)
353 return set_find(set, &query, sizeof(query), HASH_PTR(value));
357 set_insert_def(set * set, const ir_node * value)
364 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
367 static memoperand_t *
368 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
375 return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
378 static memoperand_t *
379 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
383 query.irn = (ir_node*)irn;
385 return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
390 set_find_spill(set * set, const ir_node * value)
394 query.irn = (ir_node*)value;
395 return set_find(set, &query, sizeof(query), HASH_PTR(value));
398 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
399 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
400 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
401 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
402 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
405 cmp_remat_info(const void *a, const void *b, size_t size)
407 const remat_info_t *p = a;
408 const remat_info_t *q = b;
410 return !(p->irn == q->irn);
414 cmp_defs(const void *a, const void *b, size_t size)
419 return !(p->value == q->value);
423 cmp_keyval(const void *a, const void *b, size_t size)
425 const keyval_t *p = a;
426 const keyval_t *q = b;
428 return !(p->key == q->key);
432 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
435 if(be_profile_has_data())
436 return ((double)be_profile_get_block_execcount(get_block(irn))) + FUDGE;
438 #ifndef EXECFREQ_LOOPDEPH
439 return get_block_execfreq(si->chordal_env->birg->exec_freq, get_block(irn)) + FUDGE;
442 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
444 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
449 get_cost(const spill_ilp_t * si, const ir_node * irn)
451 if(be_is_Spill(irn)) {
452 return opt_cost_spill;
453 } else if(be_is_Reload(irn)){
454 return opt_cost_reload;
456 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
461 * Checks, whether node and its operands have suitable reg classes
464 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
467 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
468 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
472 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
475 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
476 ir_node *op = get_irn_n(irn, n);
477 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
480 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
487 * Try to create a remat from @p op with destination value @p dest_value
489 static INLINE remat_t *
490 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
492 remat_t *remat = NULL;
494 // if(!mode_is_datab(get_irn_mode(dest_value)))
497 if(dest_value == op) {
498 const ir_node *proj = NULL;
500 if(is_Proj(dest_value)) {
501 op = get_Proj_pred(op);
505 if(!is_rematerializable(si, op))
508 remat = obstack_alloc(si->obst, sizeof(*remat));
510 remat->cost = (int)get_cost(si, op);
511 remat->value = dest_value;
515 arch_inverse_t inverse;
518 /* get the index of the operand we want to retrieve by the inverse op */
519 for (n = get_irn_arity(op)-1; n>=0; --n) {
520 ir_node *arg = get_irn_n(op, n);
522 if(arg == dest_value) break;
526 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
528 /* else ask the backend to give an inverse op */
529 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
532 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
534 assert(inverse.n > 0 && "inverse op should have at least one node");
536 for(i=inverse.n-1; i>=0; --i) {
537 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
541 remat = obstack_alloc(si->obst, sizeof(*remat));
542 remat->op = inverse.nodes[0];
543 remat->cost = inverse.costs;
544 remat->value = dest_value;
545 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
548 assert(is_Proj(remat->proj));
550 assert(0 && "I can not handle remats with more than 2 nodes");
557 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
559 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
567 add_remat(const spill_ilp_t * si, const remat_t * remat)
569 remat_info_t *remat_info,
574 assert(remat->value);
576 query.irn = remat->value;
578 query.remats_by_operand = NULL;
579 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
581 if(remat_info->remats == NULL) {
582 remat_info->remats = new_pset(cmp_remat, 4096);
584 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
586 /* insert the remat into the remats_be_operand set of each argument of the remat op */
587 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
588 ir_node *arg = get_irn_n(remat->op, n);
592 query.remats_by_operand = NULL;
593 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
595 if(remat_info->remats_by_operand == NULL) {
596 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
598 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
603 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
605 const ir_edge_t *edge = get_irn_out_edge_first(irn);
609 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
612 edge = get_irn_out_edge_next(irn, edge);
619 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
625 irn = get_Proj_pred(irn);
627 for(n=get_irn_arity(irn)-1; n>=0; --n) {
628 const ir_node *arg = get_irn_n(irn, n);
630 if(has_reg_class(si, arg)) ++ret;
637 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
642 if( has_reg_class(si, op)
643 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
644 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
646 remat = get_remat_from_op(si, op, op);
648 add_remat(si, remat);
652 if(opt_remats == REMATS_ALL) {
653 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
655 for (n = get_irn_arity(op)-1; n>=0; --n) {
656 ir_node *arg = get_irn_n(op, n);
658 if(has_reg_class(si, arg)) {
659 /* try to get an inverse remat */
660 remat = get_remat_from_op(si, arg, op);
662 add_remat(si, remat);
670 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
673 ir_node *def_block = get_nodes_block(val);
679 /* if pos is at end of a basic block */
681 ret = (pos == def_block || block_dominates(def_block, pos));
682 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
686 /* else if this is a normal operation */
687 block = get_nodes_block(pos);
688 if(block == def_block) {
689 if(!sched_is_scheduled(val)) return 1;
691 ret = sched_comes_after(val, pos);
692 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
696 ret = block_dominates(def_block, block);
697 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
701 static INLINE ir_node *
702 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
704 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
708 * Returns first non-Phi node of block @p bb
710 static INLINE ir_node *
711 sched_block_first_nonphi(const ir_node * bb)
713 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
717 sched_skip_proj_predicator(const ir_node * irn, void * data)
719 return (is_Proj(irn));
722 static INLINE ir_node *
723 sched_next_nonproj(const ir_node * irn, int forward)
725 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
729 * Returns next operation node (non-Proj) after @p irn
730 * or the basic block of this node
732 static INLINE ir_node *
733 sched_next_op(const ir_node * irn)
735 ir_node *next = sched_next(irn);
740 return sched_next_nonproj(next, 1);
744 * Returns previous operation node (non-Proj) before @p irn
745 * or the basic block of this node
747 static INLINE ir_node *
748 sched_prev_op(const ir_node * irn)
750 ir_node *prev = sched_prev(irn);
755 return sched_next_nonproj(prev, 0);
759 sched_put_after(ir_node * insert, ir_node * irn)
761 if(is_Block(insert)) {
762 insert = sched_block_first_nonphi(insert);
764 insert = sched_next_op(insert);
766 sched_add_before(insert, irn);
770 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
772 if(is_Block(insert)) {
773 insert = sched_block_last_noncf(si, insert);
775 insert = sched_next_nonproj(insert, 0);
776 insert = sched_prev(insert);
778 sched_add_after(insert, irn);
782 next_post_remat(const ir_node * irn)
788 next = sched_block_first_nonphi(irn);
790 next = sched_next_op(irn);
793 if(sched_is_end(next))
796 op = get_irn_link(next);
797 if(op->is_remat && !op->attr.remat.pre) {
806 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
812 ret = sched_block_last_noncf(si, irn);
813 ret = sched_next(ret);
814 ret = sched_prev_op(ret);
816 ret = sched_prev_op(irn);
819 if(sched_is_end(ret) || is_Phi(ret))
822 op = (op_t*)get_irn_link(ret);
823 if(op->is_remat && op->attr.remat.pre) {
831 * Tells you whether a @p remat can be placed before the irn @p pos
834 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
836 const ir_node *op = remat->op;
842 prev = sched_block_last_noncf(si, pos);
843 prev = sched_next_nonproj(prev, 0);
845 prev = sched_prev_op(pos);
847 /* do not remat if the rematted value is defined immediately before this op */
848 if(prev == remat->op) {
853 /* this should be just fine, the following OP will be using this value, right? */
855 /* only remat AFTER the real definition of a value (?) */
856 if(!value_is_defined_before(si, pos, remat->value)) {
857 // ir_fprintf(stderr, "error(not defined)");
862 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
863 const ir_node *arg = get_irn_n(op, n);
865 if(opt_no_enlarge_liveness) {
866 if(has_reg_class(si, arg) && live) {
867 res &= pset_find_ptr((pset*)live, arg)?1:0;
869 res &= value_is_defined_before(si, pos, arg);
872 res &= value_is_defined_before(si, pos, arg);
880 * Tells you whether a @p remat can be placed after the irn @p pos
883 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
886 pos = sched_block_first_nonphi(pos);
888 pos = sched_next_op(pos);
891 /* only remat AFTER the real definition of a value (?) */
892 if(!value_is_defined_before(si, pos, remat->value)) {
896 return can_remat_before(si, remat, pos, live);
900 * Collect potetially rematerializable OPs
903 walker_remat_collector(ir_node * irn, void * data)
905 spill_ilp_t *si = data;
907 if(!is_Block(irn) && !is_Phi(irn)) {
908 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
909 get_remats_from_op(si, irn);
914 * Inserts a copy of @p irn before @p pos
917 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
922 bb = is_Block(pos)?pos:get_nodes_block(pos);
923 copy = exact_copy(irn);
925 _set_phi_class(copy, NULL);
926 set_nodes_block(copy, bb);
927 sched_put_before(si, pos, copy);
933 * Inserts a copy of @p irn after @p pos
936 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
941 bb = is_Block(pos)?pos:get_nodes_block(pos);
942 copy = exact_copy(irn);
944 _set_phi_class(copy, NULL);
945 set_nodes_block(copy, bb);
946 sched_put_after(pos, copy);
952 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
956 if(can_remat_after(si, remat, pos, live)) {
961 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
963 copy = insert_copy_after(si, remat->op, pos);
965 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
966 op = obstack_alloc(si->obst, sizeof(*op));
968 op->attr.remat.remat = remat;
969 op->attr.remat.pre = 0;
970 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
972 set_irn_link(copy, op);
973 pset_insert_ptr(si->all_possible_remats, copy);
975 proj_copy = insert_copy_after(si, remat->proj, copy);
976 set_irn_n(proj_copy, 0, copy);
977 set_irn_link(proj_copy, op);
978 pset_insert_ptr(si->all_possible_remats, proj_copy);
990 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
994 if(can_remat_before(si, remat, pos, live)) {
999 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
1001 copy = insert_copy_before(si, remat->op, pos);
1003 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
1004 op = obstack_alloc(si->obst, sizeof(*op));
1006 op->attr.remat.remat = remat;
1007 op->attr.remat.pre = 1;
1008 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1010 set_irn_link(copy, op);
1011 pset_insert_ptr(si->all_possible_remats, copy);
1013 proj_copy = insert_copy_after(si, remat->proj, copy);
1014 set_irn_n(proj_copy, 0, copy);
1015 set_irn_link(proj_copy, op);
1016 pset_insert_ptr(si->all_possible_remats, proj_copy);
1028 get_block_n_succs(const ir_node *block) {
1029 const ir_edge_t *edge;
1031 assert(edges_activated(current_ir_graph));
1033 edge = get_block_succ_first(block);
1037 edge = get_block_succ_next(block, edge);
1038 return edge ? 2 : 1;
1042 is_start_block(const ir_node * bb)
1044 return get_irg_start_block(get_irn_irg(bb)) == bb;
1048 is_merge_edge(const ir_node * bb)
1050 if(is_start_block(bb))
1054 return get_block_n_succs(bb) == 1;
1060 is_diverge_edge(const ir_node * bb)
1062 if(is_start_block(bb))
1066 return get_Block_n_cfgpreds(bb) == 1;
1072 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1077 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1078 irn = be_lv_get_irn(si->lv, bb, i);
1080 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1081 pset_insert_ptr(live, irn);
1085 irn = sched_last(bb);
1087 /* all values eaten by control flow operations are also live until the end of the block */
1088 sched_foreach_reverse(bb, irn) {
1091 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1093 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1094 ir_node *arg = get_irn_n(irn,i);
1096 if(has_reg_class(si, arg)) {
1097 pset_insert_ptr(live, arg);
1102 * find values that are used by remats at end of block
1103 * and insert them into live set
1105 foreach_pre_remat(si, bb, irn) {
1108 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1109 ir_node *remat_arg = get_irn_n(irn, n);
1111 if(!has_reg_class(si, remat_arg)) continue;
1113 /* if value is becoming live through use by remat */
1114 if(!pset_find_ptr(live, remat_arg)) {
1115 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1117 pset_insert_ptr(live, remat_arg);
1124 walker_regclass_copy_insertor(ir_node * irn, void * data)
1126 spill_ilp_t *si = data;
1128 if(is_Phi(irn) && has_reg_class(si, irn)) {
1131 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1132 ir_node *phi_arg = get_irn_n(irn, n);
1133 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1135 if(!has_reg_class(si, phi_arg)) {
1136 ir_node *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg);
1137 ir_node *pos = sched_block_last_noncf(si, bb);
1138 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1140 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1141 sched_add_after(pos, copy);
1142 set_irn_n(irn, n, copy);
1145 op->attr.live_range.args.reloads = NULL;
1146 op->attr.live_range.ilp = ILP_UNDEF;
1147 set_irn_link(copy, op);
1154 * Insert (so far unused) remats into the irg to
1155 * recompute the potential liveness of all values
1158 walker_remat_insertor(ir_node * bb, void * data)
1160 spill_ilp_t *si = data;
1167 /* skip start block, no remats to do there */
1168 if(is_start_block(bb)) return;
1170 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1172 live = pset_new_ptr_default();
1173 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1174 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1176 /* add remats at end of block */
1177 if (has_reg_class(si, value)) {
1178 pset_insert_ptr(live, value);
1182 irn = sched_last(bb);
1183 while(!sched_is_end(irn)) {
1189 next = sched_prev(irn);
1191 /* delete defined value from live set */
1192 if(has_reg_class(si, irn)) {
1193 pset_remove_ptr(live, irn);
1196 if(is_Phi(irn) || is_Proj(irn)) {
1201 args = pset_new_ptr_default();
1202 used = pset_new_ptr_default();
1204 /* collect arguments of op and set args of op already live in epilog */
1205 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1206 ir_node *arg = get_irn_n(irn, n);
1208 pset_insert_ptr(args, arg);
1209 if(has_reg_class(si, arg)) {
1210 pset_insert_ptr(live, arg);
1211 pset_insert_ptr(used, arg);
1215 /* insert all possible remats before irn */
1216 pset_foreach(args, arg) {
1217 remat_info_t *remat_info,
1220 /* continue if the operand has the wrong reg class */
1221 if(!has_reg_class(si, arg))
1225 query.remats = NULL;
1226 query.remats_by_operand = NULL;
1227 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1233 if(remat_info->remats) {
1234 pset_foreach(remat_info->remats, remat) {
1235 ir_node *remat_irn = NULL;
1237 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1238 remat_irn = insert_remat_before(si, remat, irn, live);
1241 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1242 ir_node *remat_arg = get_irn_n(remat_irn, n);
1244 /* collect args of remats which are not args of op */
1245 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1246 pset_insert_ptr(used, remat_arg);
1254 /* do not place post remats after jumps */
1255 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) {
1261 /* insert all possible remats after irn */
1262 post_remats = pset_new_ptr_default();
1263 pset_foreach(used, arg) {
1264 remat_info_t *remat_info,
1267 /* continue if the operand has the wrong reg class */
1268 if(!has_reg_class(si, arg))
1272 query.remats = NULL;
1273 query.remats_by_operand = NULL;
1274 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1280 if(remat_info->remats_by_operand) {
1281 pset_foreach(remat_info->remats_by_operand, remat) {
1282 /* do not insert remats producing the same value as one of the operands */
1283 if(!pset_find_ptr(args, remat->value)) {
1284 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1286 /* only remat values that can be used by real ops */
1287 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1288 pset_insert_ptr(post_remats, remat);
1294 pset_foreach(post_remats, remat) {
1295 insert_remat_after(si, remat, irn, live);
1297 del_pset(post_remats);
1304 /* add remats at end if successor has multiple predecessors */
1305 if(is_merge_edge(bb)) {
1306 pset *live_out = pset_new_ptr_default();
1309 get_live_end(si, bb, live_out);
1311 /* add remats at end of block */
1312 pset_foreach(live_out, value) {
1313 remat_info_t *remat_info,
1317 query.remats = NULL;
1318 query.remats_by_operand = NULL;
1319 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1321 if(remat_info && remat_info->remats) {
1322 pset_foreach(remat_info->remats, remat) {
1323 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1325 insert_remat_before(si, remat, bb, live_out);
1332 if(is_diverge_edge(bb)) {
1333 pset *live_in = pset_new_ptr_default();
1336 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1337 value = be_lv_get_irn(si->lv, bb, i);
1339 if(has_reg_class(si, value)) {
1340 pset_insert_ptr(live_in, value);
1343 /* add phis to live_in */
1344 sched_foreach(bb, value) {
1345 if(!is_Phi(value)) break;
1347 if(has_reg_class(si, value)) {
1348 pset_insert_ptr(live_in, value);
1352 /* add remat2s at beginning of block */
1353 post_remats = pset_new_ptr_default();
1354 pset_foreach(live_in, value) {
1355 remat_info_t *remat_info,
1359 query.remats = NULL;
1360 query.remats_by_operand = NULL;
1361 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1363 if(remat_info && remat_info->remats_by_operand) {
1364 pset_foreach(remat_info->remats_by_operand, remat) {
1365 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1367 /* put the remat here if all its args are available and result is still live */
1368 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1369 pset_insert_ptr(post_remats, remat);
1374 pset_foreach(post_remats, remat) {
1375 insert_remat_after(si, remat, bb, live_in);
1377 del_pset(post_remats);
1383 can_be_copied(const ir_node * bb, const ir_node * irn)
1385 const ir_edge_t *edge = get_block_succ_first(bb);
1386 const ir_node *next_bb = edge->src;
1387 int pos = edge->pos;
1390 assert(is_merge_edge(bb));
1392 sched_foreach(next_bb, phi) {
1393 const ir_node *phi_arg;
1395 if(!is_Phi(phi)) break;
1397 phi_arg = get_irn_n(phi, pos);
1399 if(phi_arg == irn) {
1407 * Initialize additional node info
1410 luke_initializer(ir_node * bb, void * data)
1412 spill_ilp_t *si = (spill_ilp_t*)data;
1413 spill_bb_t *spill_bb;
1416 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1417 set_irn_link(bb, spill_bb);
1419 sched_foreach(bb, irn) {
1422 op = obstack_alloc(si->obst, sizeof(*op));
1424 op->attr.live_range.ilp = ILP_UNDEF;
1427 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1428 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1430 } else if(!is_Proj(irn)) {
1431 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1432 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1434 op->attr.live_range.args.reloads = NULL;
1436 set_irn_link(irn, op);
1442 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1445 luke_endwalker(ir_node * bb, void * data)
1447 spill_ilp_t *si = (spill_ilp_t*)data;
1453 spill_bb_t *spill_bb = get_irn_link(bb);
1456 live = pset_new_ptr_default();
1457 use_end = pset_new_ptr_default();
1459 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1460 irn = be_lv_get_irn(si->lv, bb, i);
1461 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1462 pset_insert_ptr(live, irn);
1466 * find values that are used by remats at end of block
1467 * and insert them into live set
1469 foreach_pre_remat(si, bb, irn) {
1472 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1473 ir_node *remat_arg = get_irn_n(irn, n);
1475 if(has_reg_class(si, remat_arg)) {
1476 pset_insert_ptr(live, remat_arg);
1481 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1482 /* their reg_out must always be set */
1483 sched_foreach_reverse(bb, irn) {
1486 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1488 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1489 ir_node *irn_arg = get_irn_n(irn, n);
1491 if(has_reg_class(si, irn_arg)) {
1492 pset_insert_ptr(use_end, irn_arg);
1497 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1498 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1499 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1501 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1503 /* if this is a merge edge we can reload at the end of this block */
1504 if(is_merge_edge(bb)) {
1505 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1506 } else if(pset_count(use_end)){
1507 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1509 spill_bb->reloads = NULL;
1512 pset_foreach(live,irn) {
1516 int default_spilled;
1519 /* handle values used by control flow nodes later separately */
1520 if(pset_find_ptr(use_end, irn)) continue;
1523 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1525 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1527 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1528 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1529 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1531 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1532 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1534 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1535 /* by default spill value right after definition */
1536 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1537 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1539 if(is_merge_edge(bb)) {
1543 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1544 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1545 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1547 /* reload <= mem_out */
1548 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1549 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1550 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1553 spill->reg_in = ILP_UNDEF;
1554 spill->mem_in = ILP_UNDEF;
1557 pset_foreach(use_end,irn) {
1561 ilp_cst_t end_use_req,
1564 int default_spilled;
1567 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1569 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1571 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1572 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1574 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1575 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1577 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1578 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1579 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1581 /* reload for use be control flow op */
1582 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1583 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1584 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1586 /* reload <= mem_out */
1587 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1588 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1589 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1591 spill->reg_in = ILP_UNDEF;
1592 spill->mem_in = ILP_UNDEF;
1594 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1595 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1596 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1604 * Find a remat of value @p value in the epilog of @p pos
1607 find_post_remat(const ir_node * value, const ir_node * pos)
1609 while((pos = next_post_remat(pos)) != NULL) {
1612 op = get_irn_link(pos);
1613 assert(op->is_remat && !op->attr.remat.pre);
1615 if(op->attr.remat.remat->value == value)
1616 return (ir_node*)pos;
1619 const ir_edge_t *edge;
1620 foreach_out_edge(pos, edge) {
1621 ir_node *proj = get_edge_src_irn(edge);
1622 assert(is_Proj(proj));
1632 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1634 spill_bb_t *spill_bb = get_irn_link(bb);
1638 int default_spilled;
1641 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1643 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1645 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1647 spill->reg_out = ILP_UNDEF;
1648 spill->reg_in = ILP_UNDEF;
1649 spill->mem_in = ILP_UNDEF;
1651 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1652 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1654 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1655 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1656 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1663 * Inserts ILP-constraints and variables for memory copying before the given position
1666 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1668 const ir_node *succ;
1669 const ir_edge_t *edge;
1670 spill_bb_t *spill_bb = get_irn_link(block);
1679 assert(edges_activated(current_ir_graph));
1681 edge = get_block_succ_first(block);
1687 edge = get_block_succ_next(block, edge);
1688 /* next block can only contain phis, if this is a merge edge */
1691 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1692 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1694 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1695 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1697 pset_foreach(live, tmp) {
1700 op_t *op = get_irn_link(irn);
1701 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1703 spill = set_find_spill(spill_bb->ilp, tmp);
1706 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1708 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1710 sched_foreach(succ, phi) {
1711 const ir_node *to_copy;
1713 spill_t *to_copy_spill;
1714 op_t *phi_op = get_irn_link(phi);
1715 ilp_var_t reload = ILP_UNDEF;
1718 if(!is_Phi(phi)) break;
1719 if(!has_reg_class(si, phi)) continue;
1721 to_copy = get_irn_n(phi, pos);
1722 to_copy_op = get_irn_link(to_copy);
1724 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1725 assert(to_copy_spill);
1727 if(spill_bb->reloads) {
1728 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1731 reload = PTR_TO_INT(keyval->val);
1735 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1736 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1738 /* copy - reg_out - reload - remat - live_range <= 0 */
1739 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1740 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1741 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1742 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1743 foreach_pre_remat(si, block, tmp) {
1744 op_t *remat_op = get_irn_link(tmp);
1745 if(remat_op->attr.remat.remat->value == to_copy) {
1746 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1750 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1751 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1753 /* copy - reg_out - copyreg <= 0 */
1754 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1755 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1756 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1762 * Walk all irg blocks and emit this ILP
1765 luke_blockwalker(ir_node * bb, void * data)
1767 spill_ilp_t *si = (spill_ilp_t*)data;
1772 spill_bb_t *spill_bb = get_irn_link(bb);
1775 pset *defs = pset_new_ptr_default();
1776 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
1778 live = pset_new_ptr_default();
1780 /****************************************
1781 * B A S I C B L O C K E N D
1782 ***************************************/
1785 /* init live values at end of block */
1786 get_live_end(si, bb, live);
1788 pset_foreach(live, irn) {
1790 ilp_var_t reload = ILP_UNDEF;
1792 spill = set_find_spill(spill_bb->ilp, irn);
1795 if(spill_bb->reloads) {
1796 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1799 reload = PTR_TO_INT(keyval->val);
1803 op = get_irn_link(irn);
1804 assert(!op->is_remat);
1806 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1807 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1808 op->attr.live_range.op = bb;
1810 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1811 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1813 /* reg_out - reload - remat - live_range <= 0 */
1814 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1815 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1816 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1817 foreach_pre_remat(si, bb, tmp) {
1818 op_t *remat_op = get_irn_link(tmp);
1819 if(remat_op->attr.remat.remat->value == irn) {
1820 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1823 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1824 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1826 /* value may only die at bb end if it is used for a mem copy */
1827 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1828 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1829 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1830 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1831 foreach_pre_remat(si, bb, tmp) {
1832 op_t *remat_op = get_irn_link(tmp);
1833 if(remat_op->attr.remat.remat->value == irn) {
1834 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1837 if(is_merge_edge(bb)) {
1838 const ir_edge_t *edge = get_block_succ_first(bb);
1839 const ir_node *next_bb = edge->src;
1840 int pos = edge->pos;
1843 sched_foreach(next_bb, phi) {
1844 const ir_node *phi_arg;
1846 if(!is_Phi(phi)) break;
1848 phi_arg = get_irn_n(phi, pos);
1850 if(phi_arg == irn) {
1851 op_t *phi_op = get_irn_link(phi);
1852 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1854 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1861 insert_mem_copy_position(si, live, bb);
1864 * assure the remat args are available
1866 foreach_pre_remat(si, bb, tmp) {
1867 op_t *remat_op = get_irn_link(tmp);
1870 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1871 ir_node *remat_arg = get_irn_n(tmp, n);
1872 op_t *arg_op = get_irn_link(remat_arg);
1874 if(!has_reg_class(si, remat_arg)) continue;
1876 spill = set_find_spill(spill_bb->ilp, remat_arg);
1879 /* arguments of remats have to be live until the very end of the block
1880 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1881 * no remats, they could be in wrong order
1884 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1885 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1887 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1888 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1889 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1891 /* use reload placed for this argument */
1892 if(spill_bb->reloads) {
1893 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1896 ilp_var_t reload = PTR_TO_INT(keyval->val);
1898 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1903 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1908 /**************************************
1909 * B A S I C B L O C K B O D Y
1910 **************************************/
1912 sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1918 ilp_cst_t check_pre,
1924 ilp_cst_t one_memoperand;
1926 /* iterate only until first phi */
1930 op = get_irn_link(irn);
1932 if(op->is_remat) continue;
1934 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1936 /* collect defined values */
1937 if(has_reg_class(si, irn)) {
1938 pset_insert_ptr(defs, irn);
1942 if(is_Proj(irn)) continue;
1945 * init set of irn's arguments
1946 * and all possibly used values around this op
1947 * and values defined by post remats
1949 args = new_set(cmp_keyval, get_irn_arity(irn));
1950 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1951 remat_defs = pset_new_ptr(pset_count(live));
1953 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1954 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1955 ir_node *irn_arg = get_irn_n(irn, n);
1956 if(has_reg_class(si, irn_arg)) {
1957 set_insert_keyval(args, irn_arg, (void*)n);
1958 pset_insert_ptr(used, irn_arg);
1961 foreach_post_remat(irn, tmp) {
1962 op_t *remat_op = get_irn_link(tmp);
1964 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1966 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1967 ir_node *remat_arg = get_irn_n(tmp, n);
1968 if(has_reg_class(si, remat_arg)) {
1969 pset_insert_ptr(used, remat_arg);
1973 foreach_pre_remat(si, irn, tmp) {
1974 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1975 ir_node *remat_arg = get_irn_n(tmp, n);
1976 if(has_reg_class(si, remat_arg)) {
1977 pset_insert_ptr(used, remat_arg);
1983 /**********************************
1984 * I N E P I L O G O F irn
1985 **********************************/
1987 /* ensure each dying value is used by only one post remat */
1988 pset_foreach(used, tmp) {
1989 ir_node *value = tmp;
1990 op_t *value_op = get_irn_link(value);
1995 foreach_post_remat(irn, remat) {
1996 op_t *remat_op = get_irn_link(remat);
1998 for(n=get_irn_arity(remat)-1; n>=0; --n) {
1999 ir_node *remat_arg = get_irn_n(remat, n);
2001 /* if value is used by this remat add it to constraint */
2002 if(remat_arg == value) {
2004 /* sum remat2s <= 1 + n_remats*live_range */
2005 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2006 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2010 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2016 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2017 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2021 /* ensure at least one value dies at post remat */
2022 foreach_post_remat(irn, tmp) {
2023 op_t *remat_op = get_irn_link(tmp);
2024 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2027 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2028 remat_arg = get_irn_n(tmp, n);
2030 if(has_reg_class(si, remat_arg)) {
2032 /* does arg always die at this op? */
2033 if(!pset_find_ptr(live, remat_arg))
2034 goto skip_one_must_die;
2036 pset_insert_ptr(remat_args, remat_arg);
2040 /* remat + \sum live_range(remat_arg) <= |args| */
2041 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2042 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2043 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2045 pset_foreach(remat_args, remat_arg) {
2046 op_t *arg_op = get_irn_link(remat_arg);
2048 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2052 del_pset(remat_args);
2055 /* new live ranges for values from L\U defined by post remats */
2056 pset_foreach(live, tmp) {
2057 ir_node *value = tmp;
2058 op_t *value_op = get_irn_link(value);
2060 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2061 ilp_var_t prev_lr = ILP_UNDEF;
2064 if(pset_find_ptr(remat_defs, value)) {
2066 /* next_live_range <= prev_live_range + sum remat2s */
2067 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2068 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2070 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2071 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2073 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2074 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2076 foreach_post_remat(irn, remat) {
2077 op_t *remat_op = get_irn_link(remat);
2079 /* if value is being rematerialized by this remat */
2080 if(value == remat_op->attr.remat.remat->value) {
2081 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2085 value_op->attr.live_range.ilp = prev_lr;
2086 value_op->attr.live_range.op = irn;
2091 /* requirements for post remats and start live ranges from L/U' for values dying here */
2092 foreach_post_remat(irn, tmp) {
2093 op_t *remat_op = get_irn_link(tmp);
2096 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2097 ir_node *remat_arg = get_irn_n(tmp, n);
2098 op_t *arg_op = get_irn_link(remat_arg);
2100 if(!has_reg_class(si, remat_arg)) continue;
2102 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2103 if(!pset_find_ptr(used, remat_arg)) {
2104 /* remat <= live_range(remat_arg) */
2105 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2106 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2108 /* if value is becoming live through use by remat2 */
2109 if(!pset_find_ptr(live, remat_arg)) {
2112 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2113 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2115 arg_op->attr.live_range.ilp = lr;
2116 arg_op->attr.live_range.op = irn;
2118 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2120 pset_insert_ptr(live, remat_arg);
2121 add_to_spill_bb(si, bb, remat_arg);
2124 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2125 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2130 d = pset_count(defs);
2131 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2133 /* count how many regs irn needs for arguments */
2134 u = set_count(args);
2137 /* check the register pressure in the epilog */
2138 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2139 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2140 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2142 /* add L\U' to check_post */
2143 pset_foreach(live, tmp) {
2144 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2145 /* if a live value is not used by irn */
2146 tmp_op = get_irn_link(tmp);
2147 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2151 /***********************************************************
2152 * 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
2153 **********************************************************/
2156 pset_foreach(used, tmp) {
2162 op_t *arg_op = get_irn_link(arg);
2165 spill = add_to_spill_bb(si, bb, arg);
2167 /* new live range for each used value */
2168 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2169 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2171 /* the epilog stuff - including post_use, check_post, check_post_remat */
2172 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2173 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2175 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2177 /* arg is live throughout epilog if the next live_range is in a register */
2178 if(pset_find_ptr(live, arg)) {
2179 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2181 /* post_use >= next_lr + remat */
2182 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2183 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2184 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2185 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2188 /* forall post remat which use arg add a similar cst */
2189 foreach_post_remat(irn, remat) {
2192 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2193 ir_node *remat_arg = get_irn_n(remat, n);
2194 op_t *remat_op = get_irn_link(remat);
2196 if(remat_arg == arg) {
2197 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2199 /* post_use >= remat */
2200 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2201 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2202 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2203 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2208 /* if value is not an arg of op and not possibly defined by post remat
2209 * then it may only die and not become live
2211 if(!set_find_keyval(args, arg)) {
2212 /* post_use <= prev_lr */
2213 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2214 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2215 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2216 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2218 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2219 /* next_lr <= prev_lr */
2220 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2221 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2222 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2223 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2227 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2228 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2229 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2230 ilp_var_t memoperand;
2232 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2233 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2234 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2236 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2237 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2239 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2240 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2245 /* new live range begins for each used value */
2246 arg_op->attr.live_range.ilp = prev_lr;
2247 arg_op->attr.live_range.op = irn;
2249 pset_insert_ptr(live, arg);
2252 /* just to be sure */
2253 check_post = ILP_UNDEF;
2255 /* allow original defintions to be removed */
2256 if(opt_repair_schedule) {
2257 pset_foreach(defs, tmp) {
2258 op_t *tmp_op = get_irn_link(tmp);
2259 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2264 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2265 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0);
2267 /* op may not be killed if its first live_range is 1 */
2268 ir_snprintf(buf, sizeof(buf), "killorig-lr_%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, tmp_op->attr.live_range.ilp, 1.0);
2273 /* op may not be killed if it is spilled after the definition */
2274 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2275 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2276 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2277 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2282 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2283 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0);
2285 /* op may not be killed if its first live_range is 1 */
2286 ir_snprintf(buf, sizeof(buf), "killorig-lr_%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, tmp_op->attr.live_range.ilp, -1.0);
2291 /* op may not be killed if it is spilled after the definition */
2292 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2293 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2294 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2295 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2300 pset_foreach(defs, tmp) {
2301 op_t *tmp_op = get_irn_link(tmp);
2302 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2305 /* live_range or spill should be 1
2306 TODO: lr should be live until first use */
2307 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2308 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2309 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2310 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2320 /* check the register pressure in the prolog */
2321 /* sum_{L\U} lr <= k - |U| */
2322 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2323 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2325 /* for the prolog remove defined values from the live set */
2326 pset_foreach(defs, tmp) {
2327 pset_remove_ptr(live, tmp);
2330 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2331 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2332 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2335 /***********************************************************
2336 * 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
2337 **********************************************************/
2340 set_foreach(args, keyval) {
2342 const ir_node *arg = keyval->key;
2343 int i = PTR_TO_INT(keyval->val);
2344 op_t *arg_op = get_irn_link(arg);
2345 ilp_cst_t requirements;
2348 spill = set_find_spill(spill_bb->ilp, arg);
2351 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2352 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);
2354 /* reload <= mem_out */
2355 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2356 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2357 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2358 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2360 /* requirement: arg must be in register for use */
2361 /* reload + remat + live_range == 1 */
2362 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2363 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2365 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2366 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2367 foreach_pre_remat(si, irn, tmp) {
2368 op_t *remat_op = get_irn_link(tmp);
2369 if(remat_op->attr.remat.remat->value == arg) {
2370 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2374 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2376 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2377 if(get_irn_n(irn, n) == arg) {
2381 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2382 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2383 memoperand_t *memoperand;
2384 memoperand = set_find_memoperand(si->memoperands, irn, n);
2386 /* memoperand <= mem_out */
2387 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2388 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2389 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2390 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2392 /* the memoperand is only sufficient if it is used once by the op */
2393 if(n_memoperands == 1)
2394 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2396 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2398 /* we have one more free register if we use a memory operand */
2399 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2405 /* iterate over L\U */
2406 pset_foreach(live, tmp) {
2407 if(!set_find_keyval(args, tmp)) {
2408 /* if a live value is not used by irn */
2409 tmp_op = get_irn_link(tmp);
2410 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2414 /* requirements for remats */
2415 foreach_pre_remat(si, irn, tmp) {
2416 op_t *remat_op = get_irn_link(tmp);
2419 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2420 ir_node *remat_arg = get_irn_n(tmp, n);
2421 op_t *arg_op = get_irn_link(remat_arg);
2423 if(!has_reg_class(si, remat_arg)) continue;
2425 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2426 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2427 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2429 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2430 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2432 /* if remat arg is also used by current op then we can use reload placed for this argument */
2433 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2434 int index = (int)keyval->val;
2436 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2444 /*************************
2445 * D O N E W I T H O P
2446 *************************/
2448 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2450 pset_foreach(live, tmp) {
2451 assert(has_reg_class(si, tmp));
2454 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2455 ir_node *arg = get_irn_n(irn, n);
2457 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2460 del_pset(remat_defs);
2464 defs = pset_new_ptr_default();
2466 /* skip everything above barrier in start block */
2467 if(is_start_block(bb) && be_is_Barrier(irn)) {
2468 assert(pset_count(live) == 0);
2477 /***************************************
2478 * B E G I N N I N G O F B L O C K
2479 ***************************************/
2482 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2483 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2485 pset_foreach(live, irn) {
2486 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2489 /* construct mem_outs for all values */
2490 set_foreach(spill_bb->ilp, spill) {
2491 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2492 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2494 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2495 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2497 if(pset_find_ptr(live, spill->irn)) {
2498 int default_spilled;
2499 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2501 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2502 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2503 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2504 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2506 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2508 op_t *op = get_irn_link(spill->irn);
2510 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2511 const ir_node *arg = get_irn_n(spill->irn, n);
2517 /* argument already done? */
2518 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2520 /* get sum of execution frequencies of blocks with the same phi argument */
2521 for(m=n; m>=0; --m) {
2522 const ir_node *arg2 = get_irn_n(spill->irn, m);
2525 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2529 /* copies are not for free */
2530 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2531 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2533 for(m=n; m>=0; --m) {
2534 const ir_node *arg2 = get_irn_n(spill->irn, m);
2537 op->attr.live_range.args.copies[m] = var;
2542 /* copy <= mem_in */
2543 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2544 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2545 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2546 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2553 foreach_post_remat(bb, tmp) {
2555 op_t *remat_op = get_irn_link(tmp);
2556 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2559 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2560 remat_arg = get_irn_n(tmp, n);
2562 if(has_reg_class(si, remat_arg)) {
2563 pset_insert_ptr(remat_args, remat_arg);
2567 /* remat + \sum live_range(remat_arg) <= |args| */
2568 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2569 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2570 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2572 pset_foreach(remat_args, remat_arg) {
2573 if(pset_find_ptr(live, remat_arg)) {
2574 op_t *remat_arg_op = get_irn_link(remat_arg);
2575 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2578 del_pset(remat_args);
2581 foreach_post_remat(bb, tmp) {
2584 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2585 ir_node *remat_arg = get_irn_n(tmp, n);
2587 /* if value is becoming live through use by remat2 */
2588 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2589 op_t *remat_arg_op = get_irn_link(remat_arg);
2592 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2594 pset_insert_ptr(live, remat_arg);
2595 spill = add_to_spill_bb(si, bb, remat_arg);
2596 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2598 /* we need reg_in and mem_in for this value; they will be referenced later */
2599 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2600 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2601 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2602 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2605 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2606 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2607 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2608 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2613 /* L\U is empty at bb start */
2614 /* arg is live throughout epilog if it is reg_in into this block */
2616 /* check the register pressure at the beginning of the block
2619 /* reg_in entspricht post_use */
2621 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2622 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2624 pset_foreach(live, irn) {
2627 spill = set_find_spill(spill_bb->ilp, irn);
2630 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2631 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2633 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2635 /* spill + mem_in <= 1 */
2636 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2637 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2639 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2640 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2642 } /* post_remats are NOT included in register pressure check because
2643 they do not increase regpressure */
2645 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2646 pset_foreach(live, irn) {
2650 spill = set_find_spill(spill_bb->ilp, irn);
2651 assert(spill && spill->irn == irn);
2653 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2654 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2657 ir_node *phi_arg = get_Phi_pred(irn, n);
2658 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2659 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2661 op_t *op = get_irn_link(irn);
2663 /* although the phi is in the right regclass one or more of
2664 * its arguments can be in a different one or at least to
2667 if(has_reg_class(si, phi_arg)) {
2668 /* mem_in < mem_out_arg + copy */
2669 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2670 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2672 /* reg_in < reg_out_arg */
2673 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2674 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2676 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2677 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2679 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2682 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2684 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2686 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2690 /* else assure the value arrives on all paths in the same resource */
2692 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2695 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2696 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2699 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2700 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2701 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2702 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2704 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2705 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2707 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2710 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2711 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2716 foreach_post_remat(bb, tmp) {
2719 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2720 ir_node *remat_arg = get_irn_n(tmp, n);
2721 op_t *remat_op = get_irn_link(tmp);
2723 if(!has_reg_class(si, remat_arg)) continue;
2725 spill = set_find_spill(spill_bb->ilp, remat_arg);
2728 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2729 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2730 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2731 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2735 pset_foreach(live, irn) {
2736 const op_t *op = get_irn_link(irn);
2737 const ir_node *remat;
2742 foreach_post_remat(bb, remat) {
2745 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2746 const ir_node *arg = get_irn_n(remat, n);
2749 const op_t *remat_op = get_irn_link(remat);
2751 if(cst == ILP_UNDEF) {
2752 /* sum remat2s <= 1 + n_remats*live_range */
2753 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2754 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2756 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2762 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2763 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2767 /* first live ranges from reg_ins */
2768 pset_foreach(live, irn) {
2769 op_t *op = get_irn_link(irn);
2771 if(op->attr.live_range.ilp != ILP_UNDEF) {
2773 spill = set_find_spill(spill_bb->ilp, irn);
2774 assert(spill && spill->irn == irn);
2776 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2777 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2778 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2779 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2781 foreach_post_remat(bb, tmp) {
2782 op_t *remat_op = get_irn_link(tmp);
2784 if(remat_op->attr.remat.remat->value == irn) {
2785 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2791 /* walk forward now and compute constraints for placing spills */
2792 /* this must only be done for values that are not defined in this block */
2793 pset_foreach(live, irn) {
2795 * if value is defined in this block we can anways place the spill directly after the def
2796 * -> no constraint necessary
2798 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2803 spill = set_find_spill(spill_bb->ilp, irn);
2806 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2807 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2809 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2810 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2813 sched_foreach_op(bb, tmp) {
2814 op_t *op = get_irn_link(tmp);
2816 if(is_Phi(tmp)) continue;
2817 assert(!is_Proj(tmp));
2820 const ir_node *value = op->attr.remat.remat->value;
2823 /* only collect remats up to the first real use of a value */
2824 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2829 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2830 ir_node *arg = get_irn_n(tmp, n);
2833 /* if a value is used stop collecting remats */
2846 typedef struct _irnlist_t {
2847 struct list_head list;
2851 typedef struct _interference_t {
2852 struct list_head blocklist;
2858 cmp_interference(const void *a, const void *b, size_t size)
2860 const interference_t *p = a;
2861 const interference_t *q = b;
2863 return !(p->a == q->a && p->b == q->b);
2866 static interference_t *
2867 set_find_interference(set * set, ir_node * a, ir_node * b)
2869 interference_t query;
2871 query.a = (a>b)?a:b;
2872 query.b = (a>b)?b:a;
2874 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2877 static interference_t *
2878 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2880 interference_t query,
2882 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2886 result = set_find_interference(set, a, b);
2889 list_add(&list->list, &result->blocklist);
2893 query.a = (a>b)?a:b;
2894 query.b = (a>b)?b:a;
2896 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2898 INIT_LIST_HEAD(&result->blocklist);
2899 list_add(&list->list, &result->blocklist);
2905 values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_node * a, const ir_node * b)
2907 const ir_edge_t *edge;
2909 if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2910 /* both values are live in, so they interfere */
2914 /* ensure a dominates b */
2915 if(value_dominates(b,a)) {
2921 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2924 /* the following code is stolen from bera.c */
2925 if(be_is_live_end(si->lv, bb, a))
2928 foreach_out_edge(a, edge) {
2929 const ir_node *user = edge->src;
2930 if(get_nodes_block(user) == bb
2933 && !pset_find_ptr(si->inverse_ops, user)
2934 && value_dominates(b, user))
2942 * Walk all irg blocks and collect interfering values inside of phi classes
2945 luke_interferencewalker(ir_node * bb, void * data)
2947 spill_ilp_t *si = (spill_ilp_t*)data;
2950 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2951 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2952 op_t *a_op = get_irn_link(a);
2955 /* a is only interesting if it is in my register class and if it is inside a phi class */
2956 if (has_reg_class(si, a) && get_phi_class(a)) {
2957 if(a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2960 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)) {
2961 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2962 op_t *b_op = get_irn_link(b);
2965 /* a and b are only interesting if they are in the same phi class */
2966 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2967 if(b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2970 if(values_interfere_in_block(si, bb, a, b)) {
2971 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2972 set_insert_interference(si, si->interferences, a, b, bb);
2980 static unsigned int copy_path_id = 0;
2983 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2990 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2991 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
2993 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2995 pset_foreach(copies, ptr) {
2996 copy = PTR_TO_INT(ptr);
2997 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
3002 * @parameter copies contains a path of copies which lead us to irn
3003 * @parameter visited contains a set of nodes already visited on this path
3006 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3008 const ir_edge_t *edge;
3009 op_t *op = get_irn_link(irn);
3010 pset *visited_users = pset_new_ptr_default();
3013 if(op->is_remat) return 0;
3015 pset_insert_ptr(visited, irn);
3019 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3021 /* visit all operands */
3022 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3023 ir_node *arg = get_irn_n(irn, n);
3024 ilp_var_t copy = op->attr.live_range.args.copies[n];
3026 if(!has_reg_class(si, arg)) continue;
3027 if(pset_find_ptr(visited_operands, arg)) continue;
3028 pset_insert_ptr(visited_operands, arg);
3031 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3032 del_pset(visited_operands);
3033 del_pset(visited_users);
3034 pset_remove_ptr(visited, irn);
3037 pset_insert(copies, INT_TO_PTR(copy), copy);
3038 write_copy_path_cst(si, copies, any_interfere);
3039 pset_remove(copies, INT_TO_PTR(copy), copy);
3040 } else if(!pset_find_ptr(visited, arg)) {
3041 pset_insert(copies, INT_TO_PTR(copy), copy);
3042 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3043 pset_remove(copies, INT_TO_PTR(copy), copy);
3045 if(paths > MAX_PATHS) {
3046 if(pset_count(copies) == 0) {
3050 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3051 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3052 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3053 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3054 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3058 del_pset(visited_operands);
3059 del_pset(visited_users);
3060 pset_remove_ptr(visited, irn);
3063 } else if(pset_count(copies) == 0) {
3069 del_pset(visited_operands);
3072 /* visit all uses which are phis */
3073 foreach_out_edge(irn, edge) {
3074 ir_node *user = edge->src;
3075 int pos = edge->pos;
3076 op_t *op = get_irn_link(user);
3079 if(!is_Phi(user)) continue;
3080 if(!has_reg_class(si, user)) continue;
3081 if(pset_find_ptr(visited_users, user)) continue;
3082 pset_insert_ptr(visited_users, user);
3084 copy = op->attr.live_range.args.copies[pos];
3086 if(user == target) {
3087 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3088 del_pset(visited_users);
3089 pset_remove_ptr(visited, irn);
3092 pset_insert(copies, INT_TO_PTR(copy), copy);
3093 write_copy_path_cst(si, copies, any_interfere);
3094 pset_remove(copies, INT_TO_PTR(copy), copy);
3095 } else if(!pset_find_ptr(visited, user)) {
3096 pset_insert(copies, INT_TO_PTR(copy), copy);
3097 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3098 pset_remove(copies, INT_TO_PTR(copy), copy);
3100 if(paths > MAX_PATHS) {
3101 if(pset_count(copies) == 0) {
3105 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3106 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3107 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3108 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3109 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3113 del_pset(visited_users);
3114 pset_remove_ptr(visited, irn);
3117 } else if(pset_count(copies) == 0) {
3123 del_pset(visited_users);
3124 pset_remove_ptr(visited, irn);
3129 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3131 pset * copies = pset_new_ptr_default();
3132 pset * visited = pset_new_ptr_default();
3134 find_copy_path(si, a, b, any_interfere, copies, visited);
3142 memcopyhandler(spill_ilp_t * si)
3144 interference_t *interference;
3146 /* teste Speicherwerte auf Interferenz */
3148 /* analyze phi classes */
3149 phi_class_compute(si->chordal_env->irg);
3151 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3152 irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
3154 /* now lets emit the ILP unequations for the crap */
3155 set_foreach(si->interferences, interference) {
3157 ilp_var_t interfere,
3159 ilp_cst_t any_interfere_cst,
3161 const ir_node *a = interference->a;
3162 const ir_node *b = interference->b;
3164 /* any_interf <= \sum interf */
3165 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3166 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3167 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3169 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3171 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3172 const ir_node *bb = irnlist->irn;
3173 spill_bb_t *spill_bb = get_irn_link(bb);
3178 spilla = set_find_spill(spill_bb->ilp, a);
3181 spillb = set_find_spill(spill_bb->ilp, b);
3184 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3185 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3186 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3187 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3188 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3189 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3191 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3192 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3194 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3195 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3196 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3197 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3198 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3200 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3201 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3203 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3204 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3205 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3207 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3208 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3210 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3211 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3212 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3215 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3217 /* any_interfere >= interf */
3218 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3219 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3221 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3222 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3225 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3226 gen_copy_constraints(si,a,b,any_interfere);
3234 return fabs(x) < 0.00001;
3237 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3239 spill_ilp_t *si = get_irg_link(current_ir_graph);
3241 if(pset_find_ptr(si->all_possible_remats, n)) {
3242 op_t *op = (op_t*)get_irn_link(n);
3243 assert(op && op->is_remat);
3245 if(!op->attr.remat.remat->inverse) {
3246 if(op->attr.remat.pre) {
3247 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3249 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3254 op_t *op = (op_t*)get_irn_link(n);
3255 assert(op && op->is_remat);
3257 if(op->attr.remat.pre) {
3258 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3260 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3271 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3273 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3274 be_dump(irg, suffix, dump_ir_block_graph_sched);
3275 set_dump_node_vcgattr_hook(NULL);
3279 * Edge hook to dump the schedule edges with annotated register pressure.
3282 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3284 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3285 ir_node *prev = sched_prev(irn);
3286 fprintf(F, "edge:{sourcename:\"");
3288 fprintf(F, "\" targetname:\"");
3290 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3291 fprintf(F, "\" color:magenta}\n");
3297 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3299 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3301 dump_consts_local(0);
3302 set_dump_node_edge_hook(sched_pressure_edge_hook);
3303 dump_ir_block_graph(irg, suffix);
3304 set_dump_node_edge_hook(old_edge_hook);
3308 walker_pressure_annotator(ir_node * bb, void * data)
3310 spill_ilp_t *si = data;
3313 pset *live = pset_new_ptr_default();
3316 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3317 irn = be_lv_get_irn(si->lv, bb, i);
3319 if (has_reg_class(si, irn)) {
3320 pset_insert_ptr(live, irn);
3324 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3326 sched_foreach_reverse(bb, irn) {
3328 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3332 if(has_reg_class(si, irn)) {
3333 pset_remove_ptr(live, irn);
3334 if(is_Proj(irn)) ++projs;
3337 if(!is_Proj(irn)) projs = 0;
3339 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3340 ir_node *arg = get_irn_n(irn, n);
3342 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3344 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3351 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3353 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
3357 connect_all_remats_with_keep(spill_ilp_t * si)
3365 n_remats = pset_count(si->all_possible_remats);
3367 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3370 pset_foreach(si->all_possible_remats, irn) {
3375 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
3377 obstack_free(si->obst, ins);
3382 connect_all_spills_with_keep(spill_ilp_t * si)
3391 n_spills = pset_count(si->spills);
3393 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3396 pset_foreach(si->spills, irn) {
3401 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
3403 obstack_free(si->obst, ins);
3407 /** insert a spill at an arbitrary position */
3408 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert)
3410 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
3411 ir_graph *irg = get_irn_irg(bl);
3412 ir_node *frame = get_irg_frame(irg);
3415 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
3417 spill = be_new_Spill(cls, irg, bl, irn);
3420 * search the right insertion point. a spill of a phi cannot be put
3421 * directly after the phi, if there are some phis behind the one which
3422 * is spilled. Also, a spill of a Proj must be after all Projs of the
3425 * Here's one special case:
3426 * If the spill is in the start block, the spill must be after the frame
3427 * pointer is set up. This is done by setting insert to the end of the block
3428 * which is its default initialization (see above).
3431 if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3434 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3437 sched_add_after(insert, spill);
3442 delete_remat(spill_ilp_t * si, ir_node * remat) {
3444 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3446 sched_remove(remat);
3448 /* kill links to operands */
3449 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3450 set_irn_n(remat, n, bad);
3455 clean_remat_info(spill_ilp_t * si)
3459 remat_info_t *remat_info;
3460 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3462 set_foreach(si->remat_info, remat_info) {
3463 if(!remat_info->remats) continue;
3465 pset_foreach(remat_info->remats, remat)
3467 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3468 if(sched_is_scheduled(remat->proj)) {
3469 sched_remove((ir_node*)remat->proj);
3471 set_irn_n((ir_node*)remat->proj, -1, bad);
3472 set_irn_n((ir_node*)remat->proj, 0, bad);
3475 if(get_irn_n_edges(remat->op) == 0) {
3476 if(sched_is_scheduled(remat->op)) {
3477 sched_remove((ir_node*)remat->op);
3479 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3480 set_irn_n((ir_node*)remat->op, n, bad);
3485 if(remat_info->remats) del_pset(remat_info->remats);
3486 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3491 delete_unnecessary_remats(spill_ilp_t * si)
3493 if(opt_keep_alive & KEEPALIVE_REMATS) {
3495 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3498 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3499 ir_node *keep_arg = get_irn_n(si->keep, n);
3500 op_t *arg_op = get_irn_link(keep_arg);
3503 assert(arg_op->is_remat);
3505 name = si->lpp->vars[arg_op->attr.remat.ilp];
3507 if(is_zero(name->value)) {
3508 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3509 /* TODO check whether reload is preferred over remat (could be bug) */
3510 delete_remat(si, keep_arg);
3512 if(!arg_op->attr.remat.remat->inverse) {
3513 if(arg_op->attr.remat.pre) {
3514 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3516 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3519 if(arg_op->attr.remat.pre) {
3520 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3522 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3527 set_irn_n(si->keep, n, bad);
3530 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3535 pset_foreach(si->all_possible_remats, remat) {
3536 op_t *remat_op = get_irn_link(remat);
3537 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3539 if(is_zero(name->value)) {
3540 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3541 /* TODO check whether reload is preferred over remat (could be bug) */
3542 delete_remat(si, remat);
3544 if(!remat_op->attr.remat.remat->inverse) {
3545 if(remat_op->attr.remat.pre) {
3546 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3548 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3551 if(remat_op->attr.remat.pre) {
3552 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3554 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3563 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3565 pset *spills = pset_new_ptr_default();
3567 const ir_node *next;
3570 defs = set_find_def(si->values, value);
3572 if(defs && defs->spills) {
3573 for(next = defs->spills; next; next = get_irn_link(next)) {
3574 pset_insert_ptr(spills, next);
3582 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3586 assert( get_irn_arity(block) == arity );
3588 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3589 res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
3595 * @param before The node after which the spill will be placed in the schedule
3598 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3602 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3604 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3606 spill = be_spill2(arch_env, irn, before);
3608 defs = set_insert_def(si->values, value);
3611 /* enter into the linked list */
3612 set_irn_link(spill, defs->spills);
3613 defs->spills = spill;
3615 if(opt_keep_alive & KEEPALIVE_SPILLS)
3616 pset_insert_ptr(si->spills, spill);
3622 * @param before The Phi node which has to be spilled
3625 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3632 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3634 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3635 ins[n] = si->m_unknown;
3638 mem_phi = new_r_PhiM_nokeep(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3640 defs = set_insert_def(si->values, phi);
3643 /* enter into the linked list */
3644 set_irn_link(mem_phi, defs->spills);
3645 defs->spills = mem_phi;
3647 #ifdef SCHEDULE_PHIM
3648 sched_add_after(phi, mem_phi);
3650 pset_insert_ptr(si->phims, mem_phi);
3653 if(opt_keep_alive & KEEPALIVE_SPILLS)
3654 pset_insert_ptr(si->spills, mem_phi);
3661 * Add remat to list of defs, destroys link field!
3664 insert_remat(spill_ilp_t * si, ir_node * remat)
3667 op_t *remat_op = get_irn_link(remat);
3669 assert(remat_op->is_remat);
3671 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3674 /* enter into the linked list */
3675 set_irn_link(remat, defs->remats);
3676 defs->remats = remat;
3681 * Add reload before operation and add to list of defs
3684 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3689 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3691 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3693 defs = set_find_def(si->values, value);
3695 spill = defs->spills;
3696 assert(spill && "no spill placed before reload");
3698 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3700 /* enter into the linked list */
3701 set_irn_link(reload, defs->remats);
3702 defs->remats = reload;
3707 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3710 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3712 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3714 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3716 defs = set_find_def(si->values, value);
3718 spill = defs->spills;
3719 assert(spill && "no spill placed before reload");
3721 arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos);
3724 void insert_memoperands(spill_ilp_t * si)
3726 memoperand_t *memoperand;
3729 set_foreach(si->memoperands, memoperand) {
3730 name = si->lpp->vars[memoperand->ilp];
3731 if(!is_zero(name->value)) {
3732 perform_memory_operand(si, memoperand);
3738 walker_spill_placer(ir_node * bb, void * data) {
3739 spill_ilp_t *si = (spill_ilp_t*)data;
3741 spill_bb_t *spill_bb = get_irn_link(bb);
3742 pset *spills_to_do = pset_new_ptr_default();
3745 set_foreach(spill_bb->ilp, spill) {
3748 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3749 name = si->lpp->vars[spill->mem_in];
3750 if(!is_zero(name->value)) {
3753 mem_phi = insert_mem_phi(si, spill->irn);
3755 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3759 name = si->lpp->vars[spill->spill];
3760 if(!is_zero(name->value)) {
3761 /* place spill directly after definition */
3762 if(get_nodes_block(spill->irn) == bb) {
3763 insert_spill(si, spill->irn, spill->irn, spill->irn);
3767 /* place spill at bb start */
3768 if(spill->reg_in > 0) {
3769 name = si->lpp->vars[spill->reg_in];
3770 if(!is_zero(name->value)) {
3771 insert_spill(si, spill->irn, spill->irn, bb);
3775 /* place spill after a remat */
3776 pset_insert_ptr(spills_to_do, spill->irn);
3779 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3782 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3783 op_t *op = get_irn_link(irn);
3785 if(be_is_Spill(irn)) continue;
3788 /* TODO fix this if we want to support remats with more than two nodes */
3789 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3790 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3792 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3795 if(pset_find_ptr(spills_to_do, irn)) {
3796 pset_remove_ptr(spills_to_do, irn);
3798 insert_spill(si, irn, irn, irn);
3804 assert(pset_count(spills_to_do) == 0);
3806 /* afterwards free data in block */
3807 del_pset(spills_to_do);
3811 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3813 ir_node *insert_pos = bb;
3815 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3817 /* find last definition of arg value in block */
3822 defs = set_find_def(si->values, value);
3824 if(defs && defs->remats) {
3825 for(next = defs->remats; next; next = get_irn_link(next)) {
3826 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3827 last = sched_get_time_step(next);
3833 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3834 last = sched_get_time_step(value);
3838 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3840 spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos);
3846 phim_fixer(spill_ilp_t *si) {
3849 set_foreach(si->values, defs) {
3850 const ir_node *phi = defs->value;
3851 op_t *op = get_irn_link(phi);
3852 ir_node *phi_m = NULL;
3853 ir_node *next = defs->spills;
3856 if(!is_Phi(phi)) continue;
3859 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3863 next = get_irn_link(next);
3866 if(!phi_m) continue;
3868 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3869 ir_node *value = get_irn_n(phi, n);
3870 defs_t *val_defs = set_find_def(si->values, value);
3872 /* a spill of this value */
3877 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3878 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3880 if(!is_zero(name->value)) {
3881 spill = insert_mem_copy(si, pred, value);
3883 spill = val_defs->spills;
3886 spill = val_defs->spills;
3889 assert(spill && "no spill placed before PhiM");
3890 set_irn_n(phi_m, n, spill);
3896 walker_reload_placer(ir_node * bb, void * data) {
3897 spill_ilp_t *si = (spill_ilp_t*)data;
3899 spill_bb_t *spill_bb = get_irn_link(bb);
3901 /* reloads at end of block */
3902 if(spill_bb->reloads) {
3905 set_foreach(spill_bb->reloads, keyval) {
3906 ir_node *irn = (ir_node*)keyval->key;
3907 ilp_var_t reload = PTR_TO_INT(keyval->val);
3910 name = si->lpp->vars[reload];
3911 if(!is_zero(name->value)) {
3913 ir_node *insert_pos = bb;
3914 ir_node *prev = sched_block_last_noncf(si, bb);
3915 op_t *prev_op = get_irn_link(prev);
3917 while(be_is_Spill(prev)) {
3918 prev = sched_prev(prev);
3921 prev_op = get_irn_link(prev);
3923 /* insert reload before pre-remats */
3924 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3925 && prev_op->is_remat && prev_op->attr.remat.pre) {
3929 prev = sched_prev(prev);
3930 } while(be_is_Spill(prev));
3932 prev_op = get_irn_link(prev);
3936 reload = insert_reload(si, irn, insert_pos);
3938 if(opt_keep_alive & KEEPALIVE_RELOADS)
3939 pset_insert_ptr(si->spills, reload);
3944 /* walk and insert more reloads and collect remats */
3945 sched_foreach_reverse(bb, irn) {
3946 op_t *op = get_irn_link(irn);
3948 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3949 if(is_Phi(irn)) break;
3952 if(get_irn_mode(irn) != mode_T) {
3953 insert_remat(si, irn);
3958 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3959 ir_node *arg = get_irn_n(irn, n);
3961 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3964 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3965 if(!is_zero(name->value)) {
3967 ir_node *insert_pos = irn;
3968 ir_node *prev = sched_prev(insert_pos);
3971 while(be_is_Spill(prev)) {
3972 prev = sched_prev(prev);
3975 prev_op = get_irn_link(prev);
3977 /* insert reload before pre-remats */
3978 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3979 && prev_op->is_remat && prev_op->attr.remat.pre) {
3983 prev = sched_prev(prev);
3984 } while(be_is_Spill(prev));
3986 prev_op = get_irn_link(prev);
3990 reload = insert_reload(si, arg, insert_pos);
3992 assert(reload && "no reload returned");
3993 set_irn_n(irn, n, reload);
3995 if(opt_keep_alive & KEEPALIVE_RELOADS)
3996 pset_insert_ptr(si->spills, reload);
4003 del_set(spill_bb->ilp);
4004 if(spill_bb->reloads) del_set(spill_bb->reloads);
4008 walker_collect_used(ir_node * irn, void * data)
4010 bitset_t *used = data;
4012 bitset_set(used, get_irn_idx(irn));
4015 struct kill_helper {
4021 walker_kill_unused(ir_node * bb, void * data)
4023 struct kill_helper *kh = data;
4024 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4028 for(irn=sched_first(bb); !sched_is_end(irn);) {
4029 ir_node *next = sched_next(irn);
4032 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4033 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4034 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)));
4036 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4042 set_nodes_block(irn, bad);
4043 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4044 set_irn_n(irn, n, bad);
4051 #ifndef SCHEDULE_PHIM
4053 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4056 ir_node *bad = get_irg_bad(si->chordal_env->irg);
4059 pset_foreach(si->phims, phi) {
4060 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4062 set_nodes_block(phi, bad);
4063 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4064 set_irn_n(phi, n, bad);
4072 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4074 struct kill_helper kh;
4076 kh.used = bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
4079 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
4080 #ifndef SCHEDULE_PHIM
4081 kill_unused_phims(si, &kh);
4083 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
4085 bitset_free(kh.used);
4089 print_irn_pset(pset * p)
4093 pset_foreach(p, irn) {
4094 ir_printf("%+F\n", irn);
4099 dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file)
4101 FILE *f = fopen(file, "w");
4103 interference_t *interference;
4105 pset_break(phiclass);
4106 set_break(si->interferences);
4108 ir_fprintf(f, "digraph phiclass {\n");
4110 pset_foreach(phiclass, irn) {
4112 ir_fprintf(f, " %F%N [shape=box]\n",irn,irn);
4115 pset_foreach(phiclass, irn) {
4118 if(!is_Phi(irn)) continue;
4120 for(n=get_irn_arity(irn)-1; n>=0; --n) {
4121 ir_node *arg = get_irn_n(irn, n);
4123 ir_fprintf(f, " %F%N -> %F%N\n",irn,irn,arg,arg);
4127 set_foreach(si->interferences, interference) {
4128 const ir_node *a = interference->a;
4129 const ir_node *b = interference->b;
4130 if(get_phi_class(a) == phiclass) {
4131 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n",a,a,b,b);
4140 rewire_uses(spill_ilp_t * si)
4143 pset *ignore = pset_new_ptr(1);
4144 be_dom_front_info_t *dom_front = si->chordal_env->birg->dom_front;
4146 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
4148 /* then fix uses of spills */
4149 set_foreach(si->values, defs) {
4152 const ir_node *next = defs->remats;
4155 reloads = pset_new_ptr_default();
4158 if(be_is_Reload(next)) {
4159 pset_insert_ptr(reloads, next);
4163 next = get_irn_link(next);
4166 spills = get_spills_for_value(si, defs->value);
4167 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));
4168 if(pset_count(spills) > 1) {
4169 //assert(pset_count(reloads) > 0);
4170 // print_irn_pset(spills);
4171 // print_irn_pset(reloads);
4173 be_ssa_constr_set_ignore(dom_front, si->lv, spills, ignore);
4180 /* first fix uses of remats and reloads */
4181 set_foreach(si->values, defs) {
4183 const ir_node *next = defs->remats;
4187 nodes = pset_new_ptr_default();
4188 if(sched_is_scheduled(defs->value)) {
4189 pset_insert_ptr(nodes, defs->value);
4194 pset_insert_ptr(nodes, next);
4195 next = get_irn_link(next);
4198 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-orig_kept, defs->value));
4199 be_ssa_constr_set(dom_front, si->lv, nodes);
4205 // remove_unused_defs(si);
4210 writeback_results(spill_ilp_t * si)
4212 /* walk through the graph and collect all spills, reloads and remats for a value */
4214 si->values = new_set(cmp_defs, 4096);
4216 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4217 delete_unnecessary_remats(si);
4218 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
4219 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
4220 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
4222 insert_memoperands(si);
4225 /* clean the remat info! there are still back-edges leading there! */
4226 clean_remat_info(si);
4230 connect_all_spills_with_keep(si);
4232 del_set(si->values);
4236 get_n_regs(spill_ilp_t * si)
4238 int arch_n_regs = arch_register_class_n_regs(si->cls);
4240 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4241 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4243 arch_put_non_ignore_regs(si->chordal_env->birg->main_env->arch_env, si->cls, arch_regs);
4244 be_abi_put_ignore_regs(si->chordal_env->birg->abi, si->cls, abi_regs);
4246 bitset_andnot(arch_regs, abi_regs);
4247 arch_n_regs = bitset_popcnt(arch_regs);
4249 bitset_free(arch_regs);
4250 bitset_free(abi_regs);
4252 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4257 walker_reload_mover(ir_node * bb, void * data)
4259 spill_ilp_t *si = data;
4262 sched_foreach(bb, tmp) {
4263 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4264 ir_node *reload = tmp;
4267 /* move reload upwards */
4269 int pressure = (int)get_irn_link(reload);
4270 if(pressure < si->n_regs) {
4271 irn = sched_prev(reload);
4272 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4273 sched_remove(reload);
4274 pressure = (int)get_irn_link(irn);
4276 while(pressure < si->n_regs) {
4277 if( sched_is_end(irn) ||
4278 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4279 /* do not move reload before its spill */
4280 (irn == be_get_Reload_mem(reload)) ||
4281 /* do not move before phi */
4284 set_irn_link(irn, INT_TO_PTR(pressure+1));
4285 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4286 irn = sched_prev(irn);
4288 pressure = (int)get_irn_link(irn);
4291 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4292 sched_put_after(irn, reload);
4299 move_reloads_upward(spill_ilp_t * si)
4301 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
4306 * Walk all irg blocks and check for interfering spills inside of phi classes
4309 luke_meminterferencechecker(ir_node * bb, void * data)
4311 spill_ilp_t *si = (spill_ilp_t*)data;
4314 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4315 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4317 if(!be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) continue;
4319 /* a is only interesting if it is in my register class and if it is inside a phi class */
4320 if (has_reg_class(si, a) && get_phi_class(a)) {
4321 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)) {
4322 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4324 if(!be_is_Spill(b) && (!is_Phi(b) || get_irn_mode(b) != mode_T)) continue;
4326 /* a and b are only interesting if they are in the same phi class */
4327 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
4328 if(values_interfere_in_block(si, bb, a, b)) {
4329 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4338 verify_phiclasses(spill_ilp_t * si)
4340 /* analyze phi classes */
4341 phi_class_compute(si->chordal_env->irg);
4343 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4344 irg_block_walk_graph(si->chordal_env->irg, luke_meminterferencechecker, NULL, si);
4348 be_spill_remat(const be_chordal_env_t * chordal_env)
4351 char problem_name[256];
4352 char dump_suffix[256];
4353 char dump_suffix2[256];
4354 struct obstack obst;
4356 be_irg_t *birg = chordal_env->birg;
4358 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
4359 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
4360 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
4362 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4363 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4365 if(opt_verify & VERIFY_DOMINANCE)
4366 be_check_dominance(chordal_env->irg);
4368 be_assure_dom_front(birg);
4369 be_assure_liveness(birg);
4371 obstack_init(&obst);
4372 si.chordal_env = chordal_env;
4374 si.cls = chordal_env->cls;
4375 si.lpp = new_lpp(problem_name, lpp_minimize);
4376 si.remat_info = new_set(cmp_remat_info, 4096);
4377 si.interferences = new_set(cmp_interference, 32);
4378 si.memoperands = new_set(cmp_memoperands, 128);
4379 si.all_possible_remats = pset_new_ptr_default();
4380 si.spills = pset_new_ptr_default();
4381 si.inverse_ops = pset_new_ptr_default();
4384 si.n_regs = get_n_regs(&si);
4386 set_irg_link(chordal_env->irg, &si);
4387 compute_doms(chordal_env->irg);
4389 /* compute phi classes */
4390 // phi_class_compute(chordal_env->irg);
4392 if(opt_dump_flags & DUMP_STATS)
4393 be_analyze_regpressure(chordal_env, "-pre");
4395 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4396 irg_block_walk_graph(chordal_env->irg, luke_initializer, NULL, &si);
4399 /* collect remats */
4400 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4401 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
4404 /* insert possible remats */
4405 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4406 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
4407 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4409 if(opt_keep_alive & KEEPALIVE_REMATS) {
4410 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4411 connect_all_remats_with_keep(&si);
4412 /* dump graph with inserted remats */
4413 dump_graph_with_remats(chordal_env->irg, dump_suffix);
4416 /* insert copies for phi arguments not in my regclass */
4417 irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si);
4419 /* recompute liveness */
4420 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4421 be_liveness_recompute(si.lv);
4424 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4425 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4426 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
4428 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4429 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
4432 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4433 memcopyhandler(&si);
4436 if(opt_dump_flags & DUMP_PROBLEM) {
4438 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4439 if ((f = fopen(buf, "wt")) != NULL) {
4440 lpp_dump_plain(si.lpp, f);
4445 if(opt_dump_flags & DUMP_MPS) {
4448 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4449 if((f = fopen(buf, "wt")) != NULL) {
4450 mps_write_mps(si.lpp, s_mps_fixed, f);
4454 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4455 if((f = fopen(buf, "wt")) != NULL) {
4456 mps_write_mst(si.lpp, s_mps_fixed, f);
4461 lpp_check_startvals(si.lpp);
4464 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4465 lpp_set_time_limit(si.lpp, opt_timeout);
4468 lpp_set_log(si.lpp, stdout);
4471 lpp_solve_cplex(si.lpp);
4473 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4475 assert(lpp_is_sol_valid(si.lpp)
4476 && "solution of ILP must be valid");
4478 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));
4480 if(opt_dump_flags & DUMP_SOLUTION) {
4484 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4485 if ((f = fopen(buf, "wt")) != NULL) {
4487 for (i = 0; i < si.lpp->var_next; ++i) {
4488 lpp_name_t *name = si.lpp->vars[i];
4489 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4495 #ifndef SCHEDULE_PHIM
4496 si.phims = pset_new_ptr_default();
4498 writeback_results(&si);
4503 kill_all_unused_values_in_schedule(&si);
4505 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4509 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4510 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
4512 // move reloads upwards
4513 be_liveness_recompute(si.lv);
4514 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4515 move_reloads_upward(&si);
4518 verify_phiclasses(&si);
4521 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4523 if(opt_dump_flags & DUMP_PRESSURE)
4524 dump_pressure_graph(&si, dump_suffix2);
4526 if(opt_dump_flags & DUMP_STATS)
4527 be_analyze_regpressure(chordal_env, "-post");
4529 if(opt_verify & VERIFY_DOMINANCE)
4530 be_check_dominance(chordal_env->irg);
4532 free_dom(chordal_env->irg);
4533 del_set(si.interferences);
4534 del_pset(si.inverse_ops);
4535 del_pset(si.all_possible_remats);
4536 del_set(si.memoperands);
4537 del_pset(si.spills);
4539 obstack_free(&obst, NULL);
4540 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4543 #else /* WITH_ILP */
4546 only_that_you_can_compile_without_WITH_ILP_defined(void)
4550 #endif /* WITH_ILP */