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"
57 #include "bespilloptions.h"
59 #include "bechordal_t.h"
62 #include <libcore/lc_opts.h>
63 #include <libcore/lc_opts_enum.h>
64 #endif /* WITH_LIBCORE */
66 #define DUMP_PROBLEM 1
68 #define DUMP_SOLUTION 4
70 #define DUMP_PRESSURE 16
72 #define KEEPALIVE_REMATS 1
73 #define KEEPALIVE_SPILLS 2
74 #define KEEPALIVE_RELOADS 4
76 #define VERIFY_MEMINTERF 1
77 #define VERIFY_DOMINANCE 2
80 #define REMATS_BRIGGS 1
81 #define REMATS_NOINVERSE 2
84 static int opt_dump_flags = 0;
85 static int opt_log = 0;
86 static int opt_keep_alive = 0;
87 static int opt_goodwin = 1;
88 static int opt_memcopies = 1;
89 static int opt_memoperands = 1;
90 static int opt_verify = VERIFY_MEMINTERF;
91 static int opt_remats = REMATS_ALL;
92 static int opt_repair_schedule = 0;
93 static int opt_no_enlarge_liveness = 0;
94 static int opt_remat_while_live = 1;
95 static int opt_timeout = 300;
96 static double opt_cost_reload = 8.0;
97 static double opt_cost_memoperand = 7.0;
98 static double opt_cost_spill = 15.0;
99 static double opt_cost_remat = 1.0;
103 static const lc_opt_enum_mask_items_t dump_items[] = {
104 { "problem", DUMP_PROBLEM },
106 { "solution", DUMP_SOLUTION },
107 { "stats", DUMP_STATS },
108 { "pressure", DUMP_PRESSURE },
112 static lc_opt_enum_mask_var_t dump_var = {
113 &opt_dump_flags, dump_items
116 static const lc_opt_enum_mask_items_t keepalive_items[] = {
117 { "remats", KEEPALIVE_REMATS },
118 { "spills", KEEPALIVE_SPILLS },
119 { "reloads", KEEPALIVE_RELOADS },
123 static lc_opt_enum_mask_var_t keep_alive_var = {
124 &opt_keep_alive, keepalive_items
127 static const lc_opt_enum_mask_items_t remats_items[] = {
128 { "none", REMATS_NONE },
129 { "briggs", REMATS_BRIGGS },
130 { "noinverse", REMATS_NOINVERSE },
131 { "all", REMATS_ALL },
135 static lc_opt_enum_mask_var_t remats_var = {
136 &opt_remats, remats_items
139 static const lc_opt_table_entry_t options[] = {
140 LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var),
142 LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin),
143 LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies),
144 LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands),
145 LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var),
146 LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule),
147 LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
148 LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live),
150 LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var),
151 LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log),
152 LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout),
154 LC_OPT_ENT_DBL ("cost_reload", "cost of a reload", &opt_cost_reload),
155 LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand),
156 LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill),
157 LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat),
164 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
165 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
168 //#define SOLVE_LOCAL
169 #define LPP_SERVER "i44pc52"
170 #define LPP_SOLVER "cplex"
173 #define MAX_PATHS INT_MAX
176 typedef struct _spill_ilp_t {
177 const arch_register_class_t *cls;
179 const be_chordal_env_t *chordal_env;
182 struct obstack *obst;
184 pset *all_possible_remats;
187 set *values; /**< for collecting all definitions of values before running ssa-construction */
192 #ifndef SCHEDULE_PHIM
195 DEBUG_ONLY(firm_dbg_module_t * dbg);
198 typedef int ilp_var_t;
199 typedef int ilp_cst_t;
201 typedef struct _spill_bb_t {
206 typedef struct _remat_t {
207 const ir_node *op; /**< for copy_irn */
208 const ir_node *value; /**< the value which is being recomputed by this remat */
209 const ir_node *proj; /**< not NULL if the above op produces a tuple */
210 int cost; /**< cost of this remat */
211 int inverse; /**< nonzero if this is an inverse remat */
215 * Data to be attached to each IR node. For remats this contains the ilp_var
216 * for this remat and for normal ops this contains the ilp_vars for
217 * reloading each operand
219 typedef struct _op_t {
224 const remat_t *remat; /** the remat this op belongs to */
225 int pre; /** 1, if this is a pressure-increasing remat */
229 ir_node *op; /** the operation this live range belongs to */
238 typedef struct _defs_t {
239 const ir_node *value;
240 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
241 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
244 typedef struct _remat_info_t {
245 const ir_node *irn; /**< the irn to which these remats belong */
246 pset *remats; /**< possible remats for this value */
247 pset *remats_by_operand; /**< remats with this value as operand */
250 typedef struct _keyval_t {
255 typedef struct _spill_t {
264 typedef struct _memoperand_t {
265 ir_node *irn; /**< the irn */
266 unsigned int pos; /**< the position of the argument */
267 ilp_var_t ilp; /**< the ilp var for this memory operand */
271 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
273 return chordal_has_class(si->chordal_env, irn);
278 cmp_remat(const void *a, const void *b)
280 const keyval_t *p = a;
281 const keyval_t *q = b;
282 const remat_t *r = p->val;
283 const remat_t *s = q->val;
287 return !(r == s || r->op == s->op);
291 cmp_remat(const void *a, const void *b)
293 const remat_t *r = a;
294 const remat_t *s = a;
296 return !(r == s || r->op == s->op);
300 cmp_spill(const void *a, const void *b, size_t size)
302 const spill_t *p = a;
303 const spill_t *q = b;
305 // return !(p->irn == q->irn && p->bb == q->bb);
306 return !(p->irn == q->irn);
310 cmp_memoperands(const void *a, const void *b, size_t size)
312 const memoperand_t *p = a;
313 const memoperand_t *q = b;
315 return !(p->irn == q->irn && p->pos == q->pos);
319 set_find_keyval(set * set, const void * key)
324 return set_find(set, &query, sizeof(query), HASH_PTR(key));
328 set_insert_keyval(set * set, void * key, void * val)
334 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
338 set_find_def(set * set, const ir_node * value)
343 return set_find(set, &query, sizeof(query), HASH_PTR(value));
347 set_insert_def(set * set, const ir_node * value)
354 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
357 static memoperand_t *
358 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
365 return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
368 static memoperand_t *
369 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
373 query.irn = (ir_node*)irn;
375 return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
380 set_find_spill(set * set, const ir_node * value)
384 query.irn = (ir_node*)value;
385 return set_find(set, &query, sizeof(query), HASH_PTR(value));
388 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
389 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
390 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
391 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
392 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
395 cmp_remat_info(const void *a, const void *b, size_t size)
397 const remat_info_t *p = a;
398 const remat_info_t *q = b;
400 return !(p->irn == q->irn);
404 cmp_defs(const void *a, const void *b, size_t size)
409 return !(p->value == q->value);
413 cmp_keyval(const void *a, const void *b, size_t size)
415 const keyval_t *p = a;
416 const keyval_t *q = b;
418 return !(p->key == q->key);
422 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
425 if(be_profile_has_data())
426 return ((double)be_profile_get_block_execcount(get_block(irn))) + FUDGE;
428 #ifndef EXECFREQ_LOOPDEPH
429 return get_block_execfreq(si->chordal_env->birg->exec_freq, get_block(irn)) + FUDGE;
432 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
434 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
439 get_cost(const spill_ilp_t * si, const ir_node * irn)
441 if(be_is_Spill(irn)) {
442 return opt_cost_spill;
443 } else if(be_is_Reload(irn)){
444 return opt_cost_reload;
446 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
451 * Checks, whether node and its operands have suitable reg classes
454 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
457 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
458 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
462 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
465 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
466 ir_node *op = get_irn_n(irn, n);
467 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
470 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
477 * Try to create a remat from @p op with destination value @p dest_value
479 static INLINE remat_t *
480 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
482 remat_t *remat = NULL;
484 // if(!mode_is_datab(get_irn_mode(dest_value)))
487 if(dest_value == op) {
488 const ir_node *proj = NULL;
490 if(is_Proj(dest_value)) {
491 op = get_Proj_pred(op);
495 if(!is_rematerializable(si, op))
498 remat = obstack_alloc(si->obst, sizeof(*remat));
500 remat->cost = (int)get_cost(si, op);
501 remat->value = dest_value;
505 arch_inverse_t inverse;
508 /* get the index of the operand we want to retrieve by the inverse op */
509 for (n = get_irn_arity(op)-1; n>=0; --n) {
510 ir_node *arg = get_irn_n(op, n);
512 if(arg == dest_value) break;
516 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
518 /* else ask the backend to give an inverse op */
519 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
522 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
524 assert(inverse.n > 0 && "inverse op should have at least one node");
526 for(i=inverse.n-1; i>=0; --i) {
527 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
531 remat = obstack_alloc(si->obst, sizeof(*remat));
532 remat->op = inverse.nodes[0];
533 remat->cost = inverse.costs;
534 remat->value = dest_value;
535 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
538 assert(is_Proj(remat->proj));
540 assert(0 && "I can not handle remats with more than 2 nodes");
547 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
549 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
557 add_remat(const spill_ilp_t * si, const remat_t * remat)
559 remat_info_t *remat_info,
564 assert(remat->value);
566 query.irn = remat->value;
568 query.remats_by_operand = NULL;
569 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
571 if(remat_info->remats == NULL) {
572 remat_info->remats = new_pset(cmp_remat, 4096);
574 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
576 /* insert the remat into the remats_be_operand set of each argument of the remat op */
577 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
578 ir_node *arg = get_irn_n(remat->op, n);
582 query.remats_by_operand = NULL;
583 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
585 if(remat_info->remats_by_operand == NULL) {
586 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
588 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
593 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
595 const ir_edge_t *edge = get_irn_out_edge_first(irn);
599 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
602 edge = get_irn_out_edge_next(irn, edge);
609 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
615 irn = get_Proj_pred(irn);
617 for(n=get_irn_arity(irn)-1; n>=0; --n) {
618 const ir_node *arg = get_irn_n(irn, n);
620 if(has_reg_class(si, arg)) ++ret;
627 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
632 if( has_reg_class(si, op)
633 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
634 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
636 remat = get_remat_from_op(si, op, op);
638 add_remat(si, remat);
642 if(opt_remats == REMATS_ALL) {
643 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
645 for (n = get_irn_arity(op)-1; n>=0; --n) {
646 ir_node *arg = get_irn_n(op, n);
648 if(has_reg_class(si, arg)) {
649 /* try to get an inverse remat */
650 remat = get_remat_from_op(si, arg, op);
652 add_remat(si, remat);
660 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
663 ir_node *def_block = get_nodes_block(val);
669 /* if pos is at end of a basic block */
671 ret = (pos == def_block || block_dominates(def_block, pos));
672 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
676 /* else if this is a normal operation */
677 block = get_nodes_block(pos);
678 if(block == def_block) {
679 if(!sched_is_scheduled(val)) return 1;
681 ret = sched_comes_after(val, pos);
682 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
686 ret = block_dominates(def_block, block);
687 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
691 static INLINE ir_node *
692 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
694 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
698 * Returns first non-Phi node of block @p bb
700 static INLINE ir_node *
701 sched_block_first_nonphi(const ir_node * bb)
703 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
707 sched_skip_proj_predicator(const ir_node * irn, void * data)
709 return (is_Proj(irn));
712 static INLINE ir_node *
713 sched_next_nonproj(const ir_node * irn, int forward)
715 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
719 * Returns next operation node (non-Proj) after @p irn
720 * or the basic block of this node
722 static INLINE ir_node *
723 sched_next_op(const ir_node * irn)
725 ir_node *next = sched_next(irn);
730 return sched_next_nonproj(next, 1);
734 * Returns previous operation node (non-Proj) before @p irn
735 * or the basic block of this node
737 static INLINE ir_node *
738 sched_prev_op(const ir_node * irn)
740 ir_node *prev = sched_prev(irn);
745 return sched_next_nonproj(prev, 0);
749 sched_put_after(ir_node * insert, ir_node * irn)
751 if(is_Block(insert)) {
752 insert = sched_block_first_nonphi(insert);
754 insert = sched_next_op(insert);
756 sched_add_before(insert, irn);
760 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
762 if(is_Block(insert)) {
763 insert = sched_block_last_noncf(si, insert);
765 insert = sched_next_nonproj(insert, 0);
766 insert = sched_prev(insert);
768 sched_add_after(insert, irn);
772 next_post_remat(const ir_node * irn)
778 next = sched_block_first_nonphi(irn);
780 next = sched_next_op(irn);
783 if(sched_is_end(next))
786 op = get_irn_link(next);
787 if(op->is_remat && !op->attr.remat.pre) {
796 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
802 ret = sched_block_last_noncf(si, irn);
803 ret = sched_next(ret);
804 ret = sched_prev_op(ret);
806 ret = sched_prev_op(irn);
809 if(sched_is_end(ret) || is_Phi(ret))
812 op = (op_t*)get_irn_link(ret);
813 if(op->is_remat && op->attr.remat.pre) {
821 * Tells you whether a @p remat can be placed before the irn @p pos
824 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
826 const ir_node *op = remat->op;
832 prev = sched_block_last_noncf(si, pos);
833 prev = sched_next_nonproj(prev, 0);
835 prev = sched_prev_op(pos);
837 /* do not remat if the rematted value is defined immediately before this op */
838 if(prev == remat->op) {
843 /* this should be just fine, the following OP will be using this value, right? */
845 /* only remat AFTER the real definition of a value (?) */
846 if(!value_is_defined_before(si, pos, remat->value)) {
847 // ir_fprintf(stderr, "error(not defined)");
852 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
853 const ir_node *arg = get_irn_n(op, n);
855 if(opt_no_enlarge_liveness) {
856 if(has_reg_class(si, arg) && live) {
857 res &= pset_find_ptr((pset*)live, arg)?1:0;
859 res &= value_is_defined_before(si, pos, arg);
862 res &= value_is_defined_before(si, pos, arg);
870 * Tells you whether a @p remat can be placed after the irn @p pos
873 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
876 pos = sched_block_first_nonphi(pos);
878 pos = sched_next_op(pos);
881 /* only remat AFTER the real definition of a value (?) */
882 if(!value_is_defined_before(si, pos, remat->value)) {
886 return can_remat_before(si, remat, pos, live);
890 * Collect potetially rematerializable OPs
893 walker_remat_collector(ir_node * irn, void * data)
895 spill_ilp_t *si = data;
897 if(!is_Block(irn) && !is_Phi(irn)) {
898 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
899 get_remats_from_op(si, irn);
904 * Inserts a copy of @p irn before @p pos
907 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
912 bb = is_Block(pos)?pos:get_nodes_block(pos);
913 copy = exact_copy(irn);
915 _set_phi_class(copy, NULL);
916 set_nodes_block(copy, bb);
917 sched_put_before(si, pos, copy);
923 * Inserts a copy of @p irn after @p pos
926 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
931 bb = is_Block(pos)?pos:get_nodes_block(pos);
932 copy = exact_copy(irn);
934 _set_phi_class(copy, NULL);
935 set_nodes_block(copy, bb);
936 sched_put_after(pos, copy);
942 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
946 if(can_remat_after(si, remat, pos, live)) {
951 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
953 copy = insert_copy_after(si, remat->op, pos);
955 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
956 op = obstack_alloc(si->obst, sizeof(*op));
958 op->attr.remat.remat = remat;
959 op->attr.remat.pre = 0;
960 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
962 set_irn_link(copy, op);
963 pset_insert_ptr(si->all_possible_remats, copy);
965 proj_copy = insert_copy_after(si, remat->proj, copy);
966 set_irn_n(proj_copy, 0, copy);
967 set_irn_link(proj_copy, op);
968 pset_insert_ptr(si->all_possible_remats, proj_copy);
980 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
984 if(can_remat_before(si, remat, pos, live)) {
989 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
991 copy = insert_copy_before(si, remat->op, pos);
993 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
994 op = obstack_alloc(si->obst, sizeof(*op));
996 op->attr.remat.remat = remat;
997 op->attr.remat.pre = 1;
998 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1000 set_irn_link(copy, op);
1001 pset_insert_ptr(si->all_possible_remats, copy);
1003 proj_copy = insert_copy_after(si, remat->proj, copy);
1004 set_irn_n(proj_copy, 0, copy);
1005 set_irn_link(proj_copy, op);
1006 pset_insert_ptr(si->all_possible_remats, proj_copy);
1018 get_block_n_succs(const ir_node *block) {
1019 const ir_edge_t *edge;
1021 assert(edges_activated(current_ir_graph));
1023 edge = get_block_succ_first(block);
1027 edge = get_block_succ_next(block, edge);
1028 return edge ? 2 : 1;
1032 is_start_block(const ir_node * bb)
1034 return get_irg_start_block(get_irn_irg(bb)) == bb;
1038 is_merge_edge(const ir_node * bb)
1040 if(is_start_block(bb))
1044 return get_block_n_succs(bb) == 1;
1050 is_diverge_edge(const ir_node * bb)
1052 if(is_start_block(bb))
1056 return get_Block_n_cfgpreds(bb) == 1;
1062 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1067 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1068 irn = be_lv_get_irn(si->lv, bb, i);
1070 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1071 pset_insert_ptr(live, irn);
1075 irn = sched_last(bb);
1077 /* all values eaten by control flow operations are also live until the end of the block */
1078 sched_foreach_reverse(bb, irn) {
1081 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1083 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1084 ir_node *arg = get_irn_n(irn,i);
1086 if(has_reg_class(si, arg)) {
1087 pset_insert_ptr(live, arg);
1092 * find values that are used by remats at end of block
1093 * and insert them into live set
1095 foreach_pre_remat(si, bb, irn) {
1098 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1099 ir_node *remat_arg = get_irn_n(irn, n);
1101 if(!has_reg_class(si, remat_arg)) continue;
1103 /* if value is becoming live through use by remat */
1104 if(!pset_find_ptr(live, remat_arg)) {
1105 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1107 pset_insert_ptr(live, remat_arg);
1114 walker_regclass_copy_insertor(ir_node * irn, void * data)
1116 spill_ilp_t *si = data;
1118 if(is_Phi(irn) && has_reg_class(si, irn)) {
1121 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1122 ir_node *phi_arg = get_irn_n(irn, n);
1123 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1125 if(!has_reg_class(si, phi_arg)) {
1126 ir_node *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg);
1127 ir_node *pos = sched_block_last_noncf(si, bb);
1128 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1130 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1131 sched_add_after(pos, copy);
1132 set_irn_n(irn, n, copy);
1135 op->attr.live_range.args.reloads = NULL;
1136 op->attr.live_range.ilp = ILP_UNDEF;
1137 set_irn_link(copy, op);
1144 * Insert (so far unused) remats into the irg to
1145 * recompute the potential liveness of all values
1148 walker_remat_insertor(ir_node * bb, void * data)
1150 spill_ilp_t *si = data;
1157 /* skip start block, no remats to do there */
1158 if(is_start_block(bb)) return;
1160 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1162 live = pset_new_ptr_default();
1163 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1164 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1166 /* add remats at end of block */
1167 if (has_reg_class(si, value)) {
1168 pset_insert_ptr(live, value);
1172 irn = sched_last(bb);
1173 while(!sched_is_end(irn)) {
1179 next = sched_prev(irn);
1181 /* delete defined value from live set */
1182 if(has_reg_class(si, irn)) {
1183 pset_remove_ptr(live, irn);
1186 if(is_Phi(irn) || is_Proj(irn)) {
1191 args = pset_new_ptr_default();
1192 used = pset_new_ptr_default();
1194 /* collect arguments of op and set args of op already live in epilog */
1195 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1196 ir_node *arg = get_irn_n(irn, n);
1198 pset_insert_ptr(args, arg);
1199 if(has_reg_class(si, arg)) {
1200 pset_insert_ptr(live, arg);
1201 pset_insert_ptr(used, arg);
1205 /* insert all possible remats before irn */
1206 pset_foreach(args, arg) {
1207 remat_info_t *remat_info,
1210 /* continue if the operand has the wrong reg class */
1211 if(!has_reg_class(si, arg))
1215 query.remats = NULL;
1216 query.remats_by_operand = NULL;
1217 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1223 if(remat_info->remats) {
1224 pset_foreach(remat_info->remats, remat) {
1225 ir_node *remat_irn = NULL;
1227 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1228 remat_irn = insert_remat_before(si, remat, irn, live);
1231 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1232 ir_node *remat_arg = get_irn_n(remat_irn, n);
1234 /* collect args of remats which are not args of op */
1235 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1236 pset_insert_ptr(used, remat_arg);
1244 /* do not place post remats after jumps */
1245 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) {
1251 /* insert all possible remats after irn */
1252 post_remats = pset_new_ptr_default();
1253 pset_foreach(used, arg) {
1254 remat_info_t *remat_info,
1257 /* continue if the operand has the wrong reg class */
1258 if(!has_reg_class(si, arg))
1262 query.remats = NULL;
1263 query.remats_by_operand = NULL;
1264 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1270 if(remat_info->remats_by_operand) {
1271 pset_foreach(remat_info->remats_by_operand, remat) {
1272 /* do not insert remats producing the same value as one of the operands */
1273 if(!pset_find_ptr(args, remat->value)) {
1274 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1276 /* only remat values that can be used by real ops */
1277 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1278 pset_insert_ptr(post_remats, remat);
1284 pset_foreach(post_remats, remat) {
1285 insert_remat_after(si, remat, irn, live);
1287 del_pset(post_remats);
1294 /* add remats at end if successor has multiple predecessors */
1295 if(is_merge_edge(bb)) {
1296 pset *live_out = pset_new_ptr_default();
1299 get_live_end(si, bb, live_out);
1301 /* add remats at end of block */
1302 pset_foreach(live_out, value) {
1303 remat_info_t *remat_info,
1307 query.remats = NULL;
1308 query.remats_by_operand = NULL;
1309 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1311 if(remat_info && remat_info->remats) {
1312 pset_foreach(remat_info->remats, remat) {
1313 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1315 insert_remat_before(si, remat, bb, live_out);
1322 if(is_diverge_edge(bb)) {
1323 pset *live_in = pset_new_ptr_default();
1326 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1327 value = be_lv_get_irn(si->lv, bb, i);
1329 if(has_reg_class(si, value)) {
1330 pset_insert_ptr(live_in, value);
1333 /* add phis to live_in */
1334 sched_foreach(bb, value) {
1335 if(!is_Phi(value)) break;
1337 if(has_reg_class(si, value)) {
1338 pset_insert_ptr(live_in, value);
1342 /* add remat2s at beginning of block */
1343 post_remats = pset_new_ptr_default();
1344 pset_foreach(live_in, value) {
1345 remat_info_t *remat_info,
1349 query.remats = NULL;
1350 query.remats_by_operand = NULL;
1351 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1353 if(remat_info && remat_info->remats_by_operand) {
1354 pset_foreach(remat_info->remats_by_operand, remat) {
1355 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1357 /* put the remat here if all its args are available and result is still live */
1358 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1359 pset_insert_ptr(post_remats, remat);
1364 pset_foreach(post_remats, remat) {
1365 insert_remat_after(si, remat, bb, live_in);
1367 del_pset(post_remats);
1373 can_be_copied(const ir_node * bb, const ir_node * irn)
1375 const ir_edge_t *edge = get_block_succ_first(bb);
1376 const ir_node *next_bb = edge->src;
1377 int pos = edge->pos;
1380 assert(is_merge_edge(bb));
1382 sched_foreach(next_bb, phi) {
1383 const ir_node *phi_arg;
1385 if(!is_Phi(phi)) break;
1387 phi_arg = get_irn_n(phi, pos);
1389 if(phi_arg == irn) {
1397 * Initialize additional node info
1400 luke_initializer(ir_node * bb, void * data)
1402 spill_ilp_t *si = (spill_ilp_t*)data;
1403 spill_bb_t *spill_bb;
1406 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1407 set_irn_link(bb, spill_bb);
1409 sched_foreach(bb, irn) {
1412 op = obstack_alloc(si->obst, sizeof(*op));
1414 op->attr.live_range.ilp = ILP_UNDEF;
1417 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1418 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1420 } else if(!is_Proj(irn)) {
1421 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1422 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1424 op->attr.live_range.args.reloads = NULL;
1426 set_irn_link(irn, op);
1432 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1435 luke_endwalker(ir_node * bb, void * data)
1437 spill_ilp_t *si = (spill_ilp_t*)data;
1443 spill_bb_t *spill_bb = get_irn_link(bb);
1446 live = pset_new_ptr_default();
1447 use_end = pset_new_ptr_default();
1449 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1450 irn = be_lv_get_irn(si->lv, bb, i);
1451 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1452 pset_insert_ptr(live, irn);
1456 * find values that are used by remats at end of block
1457 * and insert them into live set
1459 foreach_pre_remat(si, bb, irn) {
1462 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1463 ir_node *remat_arg = get_irn_n(irn, n);
1465 if(has_reg_class(si, remat_arg)) {
1466 pset_insert_ptr(live, remat_arg);
1471 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1472 /* their reg_out must always be set */
1473 sched_foreach_reverse(bb, irn) {
1476 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1478 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1479 ir_node *irn_arg = get_irn_n(irn, n);
1481 if(has_reg_class(si, irn_arg)) {
1482 pset_insert_ptr(use_end, irn_arg);
1487 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1488 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1489 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1491 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1493 /* if this is a merge edge we can reload at the end of this block */
1494 if(is_merge_edge(bb)) {
1495 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1496 } else if(pset_count(use_end)){
1497 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1499 spill_bb->reloads = NULL;
1502 pset_foreach(live,irn) {
1506 int default_spilled;
1509 /* handle values used by control flow nodes later separately */
1510 if(pset_find_ptr(use_end, irn)) continue;
1513 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1515 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1517 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1518 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1519 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1521 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1522 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1524 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1525 /* by default spill value right after definition */
1526 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1527 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1529 if(is_merge_edge(bb)) {
1533 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1534 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1535 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1537 /* reload <= mem_out */
1538 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1539 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1540 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1543 spill->reg_in = ILP_UNDEF;
1544 spill->mem_in = ILP_UNDEF;
1547 pset_foreach(use_end,irn) {
1551 ilp_cst_t end_use_req,
1554 int default_spilled;
1557 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1559 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1561 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1562 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1564 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1565 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1567 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1568 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1569 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1571 /* reload for use be control flow op */
1572 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1573 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1574 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1576 /* reload <= mem_out */
1577 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1578 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1579 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1581 spill->reg_in = ILP_UNDEF;
1582 spill->mem_in = ILP_UNDEF;
1584 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1585 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1586 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1594 * Find a remat of value @p value in the epilog of @p pos
1597 find_post_remat(const ir_node * value, const ir_node * pos)
1599 while((pos = next_post_remat(pos)) != NULL) {
1602 op = get_irn_link(pos);
1603 assert(op->is_remat && !op->attr.remat.pre);
1605 if(op->attr.remat.remat->value == value)
1606 return (ir_node*)pos;
1609 const ir_edge_t *edge;
1610 foreach_out_edge(pos, edge) {
1611 ir_node *proj = get_edge_src_irn(edge);
1612 assert(is_Proj(proj));
1622 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1624 spill_bb_t *spill_bb = get_irn_link(bb);
1628 int default_spilled;
1631 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1633 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1635 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1637 spill->reg_out = ILP_UNDEF;
1638 spill->reg_in = ILP_UNDEF;
1639 spill->mem_in = ILP_UNDEF;
1641 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1642 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1644 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1645 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1646 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1653 * Inserts ILP-constraints and variables for memory copying before the given position
1656 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1658 const ir_node *succ;
1659 const ir_edge_t *edge;
1660 spill_bb_t *spill_bb = get_irn_link(block);
1669 assert(edges_activated(current_ir_graph));
1671 edge = get_block_succ_first(block);
1677 edge = get_block_succ_next(block, edge);
1678 /* next block can only contain phis, if this is a merge edge */
1681 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1682 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1684 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1685 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1687 pset_foreach(live, tmp) {
1690 op_t *op = get_irn_link(irn);
1691 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1693 spill = set_find_spill(spill_bb->ilp, tmp);
1696 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1698 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1700 sched_foreach(succ, phi) {
1701 const ir_node *to_copy;
1703 spill_t *to_copy_spill;
1704 op_t *phi_op = get_irn_link(phi);
1705 ilp_var_t reload = ILP_UNDEF;
1708 if(!is_Phi(phi)) break;
1709 if(!has_reg_class(si, phi)) continue;
1711 to_copy = get_irn_n(phi, pos);
1712 to_copy_op = get_irn_link(to_copy);
1714 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1715 assert(to_copy_spill);
1717 if(spill_bb->reloads) {
1718 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1721 reload = PTR_TO_INT(keyval->val);
1725 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1726 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1728 /* copy - reg_out - reload - remat - live_range <= 0 */
1729 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1730 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1731 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1732 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1733 foreach_pre_remat(si, block, tmp) {
1734 op_t *remat_op = get_irn_link(tmp);
1735 if(remat_op->attr.remat.remat->value == to_copy) {
1736 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1740 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1741 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1743 /* copy - reg_out - copyreg <= 0 */
1744 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1745 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1746 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1752 * Walk all irg blocks and emit this ILP
1755 luke_blockwalker(ir_node * bb, void * data)
1757 spill_ilp_t *si = (spill_ilp_t*)data;
1762 spill_bb_t *spill_bb = get_irn_link(bb);
1765 pset *defs = pset_new_ptr_default();
1766 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
1768 live = pset_new_ptr_default();
1770 /****************************************
1771 * B A S I C B L O C K E N D
1772 ***************************************/
1775 /* init live values at end of block */
1776 get_live_end(si, bb, live);
1778 pset_foreach(live, irn) {
1780 ilp_var_t reload = ILP_UNDEF;
1782 spill = set_find_spill(spill_bb->ilp, irn);
1785 if(spill_bb->reloads) {
1786 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1789 reload = PTR_TO_INT(keyval->val);
1793 op = get_irn_link(irn);
1794 assert(!op->is_remat);
1796 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1797 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1798 op->attr.live_range.op = bb;
1800 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1801 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1803 /* reg_out - reload - remat - live_range <= 0 */
1804 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1805 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1806 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1807 foreach_pre_remat(si, bb, tmp) {
1808 op_t *remat_op = get_irn_link(tmp);
1809 if(remat_op->attr.remat.remat->value == irn) {
1810 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1813 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1814 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1816 /* value may only die at bb end if it is used for a mem copy */
1817 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1818 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1819 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1820 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1821 foreach_pre_remat(si, bb, tmp) {
1822 op_t *remat_op = get_irn_link(tmp);
1823 if(remat_op->attr.remat.remat->value == irn) {
1824 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1827 if(is_merge_edge(bb)) {
1828 const ir_edge_t *edge = get_block_succ_first(bb);
1829 const ir_node *next_bb = edge->src;
1830 int pos = edge->pos;
1833 sched_foreach(next_bb, phi) {
1834 const ir_node *phi_arg;
1836 if(!is_Phi(phi)) break;
1838 phi_arg = get_irn_n(phi, pos);
1840 if(phi_arg == irn) {
1841 op_t *phi_op = get_irn_link(phi);
1842 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1844 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1851 insert_mem_copy_position(si, live, bb);
1854 * assure the remat args are available
1856 foreach_pre_remat(si, bb, tmp) {
1857 op_t *remat_op = get_irn_link(tmp);
1860 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1861 ir_node *remat_arg = get_irn_n(tmp, n);
1862 op_t *arg_op = get_irn_link(remat_arg);
1864 if(!has_reg_class(si, remat_arg)) continue;
1866 spill = set_find_spill(spill_bb->ilp, remat_arg);
1869 /* arguments of remats have to be live until the very end of the block
1870 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1871 * no remats, they could be in wrong order
1874 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1875 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1877 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1878 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1879 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1881 /* use reload placed for this argument */
1882 if(spill_bb->reloads) {
1883 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1886 ilp_var_t reload = PTR_TO_INT(keyval->val);
1888 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1893 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1898 /**************************************
1899 * B A S I C B L O C K B O D Y
1900 **************************************/
1902 sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1908 ilp_cst_t check_pre,
1914 ilp_cst_t one_memoperand;
1916 /* iterate only until first phi */
1920 op = get_irn_link(irn);
1922 if(op->is_remat) continue;
1924 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1926 /* collect defined values */
1927 if(has_reg_class(si, irn)) {
1928 pset_insert_ptr(defs, irn);
1932 if(is_Proj(irn)) continue;
1935 * init set of irn's arguments
1936 * and all possibly used values around this op
1937 * and values defined by post remats
1939 args = new_set(cmp_keyval, get_irn_arity(irn));
1940 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1941 remat_defs = pset_new_ptr(pset_count(live));
1943 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1944 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1945 ir_node *irn_arg = get_irn_n(irn, n);
1946 if(has_reg_class(si, irn_arg)) {
1947 set_insert_keyval(args, irn_arg, (void*)n);
1948 pset_insert_ptr(used, irn_arg);
1951 foreach_post_remat(irn, tmp) {
1952 op_t *remat_op = get_irn_link(tmp);
1954 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1956 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1957 ir_node *remat_arg = get_irn_n(tmp, n);
1958 if(has_reg_class(si, remat_arg)) {
1959 pset_insert_ptr(used, remat_arg);
1963 foreach_pre_remat(si, irn, tmp) {
1964 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1965 ir_node *remat_arg = get_irn_n(tmp, n);
1966 if(has_reg_class(si, remat_arg)) {
1967 pset_insert_ptr(used, remat_arg);
1973 /**********************************
1974 * I N E P I L O G O F irn
1975 **********************************/
1977 /* ensure each dying value is used by only one post remat */
1978 pset_foreach(used, tmp) {
1979 ir_node *value = tmp;
1980 op_t *value_op = get_irn_link(value);
1985 foreach_post_remat(irn, remat) {
1986 op_t *remat_op = get_irn_link(remat);
1988 for(n=get_irn_arity(remat)-1; n>=0; --n) {
1989 ir_node *remat_arg = get_irn_n(remat, n);
1991 /* if value is used by this remat add it to constraint */
1992 if(remat_arg == value) {
1994 /* sum remat2s <= 1 + n_remats*live_range */
1995 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
1996 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2000 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2006 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2007 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2011 /* ensure at least one value dies at post remat */
2012 foreach_post_remat(irn, tmp) {
2013 op_t *remat_op = get_irn_link(tmp);
2014 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2017 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2018 remat_arg = get_irn_n(tmp, n);
2020 if(has_reg_class(si, remat_arg)) {
2022 /* does arg always die at this op? */
2023 if(!pset_find_ptr(live, remat_arg))
2024 goto skip_one_must_die;
2026 pset_insert_ptr(remat_args, remat_arg);
2030 /* remat + \sum live_range(remat_arg) <= |args| */
2031 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2032 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2033 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2035 pset_foreach(remat_args, remat_arg) {
2036 op_t *arg_op = get_irn_link(remat_arg);
2038 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2042 del_pset(remat_args);
2045 /* new live ranges for values from L\U defined by post remats */
2046 pset_foreach(live, tmp) {
2047 ir_node *value = tmp;
2048 op_t *value_op = get_irn_link(value);
2050 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2051 ilp_var_t prev_lr = ILP_UNDEF;
2054 if(pset_find_ptr(remat_defs, value)) {
2056 /* next_live_range <= prev_live_range + sum remat2s */
2057 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2058 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2060 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2061 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2063 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2064 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2066 foreach_post_remat(irn, remat) {
2067 op_t *remat_op = get_irn_link(remat);
2069 /* if value is being rematerialized by this remat */
2070 if(value == remat_op->attr.remat.remat->value) {
2071 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2075 value_op->attr.live_range.ilp = prev_lr;
2076 value_op->attr.live_range.op = irn;
2081 /* requirements for post remats and start live ranges from L/U' for values dying here */
2082 foreach_post_remat(irn, tmp) {
2083 op_t *remat_op = get_irn_link(tmp);
2086 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2087 ir_node *remat_arg = get_irn_n(tmp, n);
2088 op_t *arg_op = get_irn_link(remat_arg);
2090 if(!has_reg_class(si, remat_arg)) continue;
2092 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2093 if(!pset_find_ptr(used, remat_arg)) {
2094 /* remat <= live_range(remat_arg) */
2095 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2096 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2098 /* if value is becoming live through use by remat2 */
2099 if(!pset_find_ptr(live, remat_arg)) {
2102 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2103 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2105 arg_op->attr.live_range.ilp = lr;
2106 arg_op->attr.live_range.op = irn;
2108 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2110 pset_insert_ptr(live, remat_arg);
2111 add_to_spill_bb(si, bb, remat_arg);
2114 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2115 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2120 d = pset_count(defs);
2121 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2123 /* count how many regs irn needs for arguments */
2124 u = set_count(args);
2127 /* check the register pressure in the epilog */
2128 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2129 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2130 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2132 /* add L\U' to check_post */
2133 pset_foreach(live, tmp) {
2134 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2135 /* if a live value is not used by irn */
2136 tmp_op = get_irn_link(tmp);
2137 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2141 /***********************************************************
2142 * 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
2143 **********************************************************/
2146 pset_foreach(used, tmp) {
2152 op_t *arg_op = get_irn_link(arg);
2155 spill = add_to_spill_bb(si, bb, arg);
2157 /* new live range for each used value */
2158 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2159 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2161 /* the epilog stuff - including post_use, check_post, check_post_remat */
2162 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2163 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2165 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2167 /* arg is live throughout epilog if the next live_range is in a register */
2168 if(pset_find_ptr(live, arg)) {
2169 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2171 /* post_use >= next_lr + remat */
2172 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2173 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2174 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2175 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2178 /* forall post remat which use arg add a similar cst */
2179 foreach_post_remat(irn, remat) {
2182 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2183 ir_node *remat_arg = get_irn_n(remat, n);
2184 op_t *remat_op = get_irn_link(remat);
2186 if(remat_arg == arg) {
2187 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2189 /* post_use >= remat */
2190 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2191 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2192 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2193 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2198 /* if value is not an arg of op and not possibly defined by post remat
2199 * then it may only die and not become live
2201 if(!set_find_keyval(args, arg)) {
2202 /* post_use <= prev_lr */
2203 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2204 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2205 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2206 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2208 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2209 /* next_lr <= prev_lr */
2210 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2211 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2212 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2213 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2217 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2218 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2219 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2220 ilp_var_t memoperand;
2222 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2223 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2224 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2226 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2227 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2229 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2230 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2235 /* new live range begins for each used value */
2236 arg_op->attr.live_range.ilp = prev_lr;
2237 arg_op->attr.live_range.op = irn;
2239 pset_insert_ptr(live, arg);
2242 /* just to be sure */
2243 check_post = ILP_UNDEF;
2245 /* allow original defintions to be removed */
2246 if(opt_repair_schedule) {
2247 pset_foreach(defs, tmp) {
2248 op_t *tmp_op = get_irn_link(tmp);
2249 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2254 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2255 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0);
2257 /* op may not be killed if its first live_range is 1 */
2258 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2259 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2260 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2261 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2263 /* op may not be killed if it is spilled after the definition */
2264 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2265 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2266 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2267 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2272 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2273 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0);
2275 /* op may not be killed if its first live_range is 1 */
2276 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2277 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2278 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2279 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2281 /* op may not be killed if it is spilled after the definition */
2282 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2283 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2284 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2285 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2290 pset_foreach(defs, tmp) {
2291 op_t *tmp_op = get_irn_link(tmp);
2292 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2295 /* live_range or spill should be 1
2296 TODO: lr should be live until first use */
2297 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2298 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2299 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2300 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2310 /* check the register pressure in the prolog */
2311 /* sum_{L\U} lr <= k - |U| */
2312 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2313 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2315 /* for the prolog remove defined values from the live set */
2316 pset_foreach(defs, tmp) {
2317 pset_remove_ptr(live, tmp);
2320 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2321 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2322 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2325 /***********************************************************
2326 * 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
2327 **********************************************************/
2330 set_foreach(args, keyval) {
2332 const ir_node *arg = keyval->key;
2333 int i = PTR_TO_INT(keyval->val);
2334 op_t *arg_op = get_irn_link(arg);
2335 ilp_cst_t requirements;
2338 spill = set_find_spill(spill_bb->ilp, arg);
2341 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2342 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);
2344 /* reload <= mem_out */
2345 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2346 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2347 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2348 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2350 /* requirement: arg must be in register for use */
2351 /* reload + remat + live_range == 1 */
2352 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2353 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2355 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2356 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2357 foreach_pre_remat(si, irn, tmp) {
2358 op_t *remat_op = get_irn_link(tmp);
2359 if(remat_op->attr.remat.remat->value == arg) {
2360 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2364 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2366 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2367 if(get_irn_n(irn, n) == arg) {
2371 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2372 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2373 memoperand_t *memoperand;
2374 memoperand = set_find_memoperand(si->memoperands, irn, n);
2376 /* memoperand <= mem_out */
2377 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2378 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2379 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2380 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2382 /* the memoperand is only sufficient if it is used once by the op */
2383 if(n_memoperands == 1)
2384 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2386 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2388 /* we have one more free register if we use a memory operand */
2389 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2395 /* iterate over L\U */
2396 pset_foreach(live, tmp) {
2397 if(!set_find_keyval(args, tmp)) {
2398 /* if a live value is not used by irn */
2399 tmp_op = get_irn_link(tmp);
2400 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2404 /* requirements for remats */
2405 foreach_pre_remat(si, irn, tmp) {
2406 op_t *remat_op = get_irn_link(tmp);
2409 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2410 ir_node *remat_arg = get_irn_n(tmp, n);
2411 op_t *arg_op = get_irn_link(remat_arg);
2413 if(!has_reg_class(si, remat_arg)) continue;
2415 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2416 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2417 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2419 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2420 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2422 /* if remat arg is also used by current op then we can use reload placed for this argument */
2423 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2424 int index = (int)keyval->val;
2426 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2434 /*************************
2435 * D O N E W I T H O P
2436 *************************/
2438 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2440 pset_foreach(live, tmp) {
2441 assert(has_reg_class(si, tmp));
2444 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2445 ir_node *arg = get_irn_n(irn, n);
2447 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2450 del_pset(remat_defs);
2454 defs = pset_new_ptr_default();
2456 /* skip everything above barrier in start block */
2457 if(is_start_block(bb) && be_is_Barrier(irn)) {
2458 assert(pset_count(live) == 0);
2467 /***************************************
2468 * B E G I N N I N G O F B L O C K
2469 ***************************************/
2472 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2473 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2475 pset_foreach(live, irn) {
2476 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2479 /* construct mem_outs for all values */
2480 set_foreach(spill_bb->ilp, spill) {
2481 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2482 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2484 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2485 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2487 if(pset_find_ptr(live, spill->irn)) {
2488 int default_spilled;
2489 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2491 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2492 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2493 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2494 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2496 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2498 op_t *op = get_irn_link(spill->irn);
2500 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2501 const ir_node *arg = get_irn_n(spill->irn, n);
2507 /* argument already done? */
2508 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2510 /* get sum of execution frequencies of blocks with the same phi argument */
2511 for(m=n; m>=0; --m) {
2512 const ir_node *arg2 = get_irn_n(spill->irn, m);
2515 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2519 /* copies are not for free */
2520 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2521 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2523 for(m=n; m>=0; --m) {
2524 const ir_node *arg2 = get_irn_n(spill->irn, m);
2527 op->attr.live_range.args.copies[m] = var;
2532 /* copy <= mem_in */
2533 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2534 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2535 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2536 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2543 foreach_post_remat(bb, tmp) {
2545 op_t *remat_op = get_irn_link(tmp);
2546 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2549 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2550 remat_arg = get_irn_n(tmp, n);
2552 if(has_reg_class(si, remat_arg)) {
2553 pset_insert_ptr(remat_args, remat_arg);
2557 /* remat + \sum live_range(remat_arg) <= |args| */
2558 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2559 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2560 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2562 pset_foreach(remat_args, remat_arg) {
2563 if(pset_find_ptr(live, remat_arg)) {
2564 op_t *remat_arg_op = get_irn_link(remat_arg);
2565 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2568 del_pset(remat_args);
2571 foreach_post_remat(bb, tmp) {
2574 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2575 ir_node *remat_arg = get_irn_n(tmp, n);
2577 /* if value is becoming live through use by remat2 */
2578 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2579 op_t *remat_arg_op = get_irn_link(remat_arg);
2582 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2584 pset_insert_ptr(live, remat_arg);
2585 spill = add_to_spill_bb(si, bb, remat_arg);
2586 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2588 /* we need reg_in and mem_in for this value; they will be referenced later */
2589 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2590 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2591 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2592 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2595 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2596 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2597 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2598 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2603 /* L\U is empty at bb start */
2604 /* arg is live throughout epilog if it is reg_in into this block */
2606 /* check the register pressure at the beginning of the block
2609 /* reg_in entspricht post_use */
2611 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2612 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2614 pset_foreach(live, irn) {
2617 spill = set_find_spill(spill_bb->ilp, irn);
2620 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2621 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2623 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2625 /* spill + mem_in <= 1 */
2626 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2627 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2629 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2630 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2632 } /* post_remats are NOT included in register pressure check because
2633 they do not increase regpressure */
2635 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2636 pset_foreach(live, irn) {
2640 spill = set_find_spill(spill_bb->ilp, irn);
2641 assert(spill && spill->irn == irn);
2643 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2644 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2647 ir_node *phi_arg = get_Phi_pred(irn, n);
2648 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2649 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2651 op_t *op = get_irn_link(irn);
2653 /* although the phi is in the right regclass one or more of
2654 * its arguments can be in a different one or at least to
2657 if(has_reg_class(si, phi_arg)) {
2658 /* mem_in < mem_out_arg + copy */
2659 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2660 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2662 /* reg_in < reg_out_arg */
2663 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2664 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2666 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2667 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2669 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2672 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2674 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2676 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2680 /* else assure the value arrives on all paths in the same resource */
2682 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2685 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2686 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2689 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2690 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2691 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2692 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2694 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2695 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2697 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2700 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2701 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2706 foreach_post_remat(bb, tmp) {
2709 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2710 ir_node *remat_arg = get_irn_n(tmp, n);
2711 op_t *remat_op = get_irn_link(tmp);
2713 if(!has_reg_class(si, remat_arg)) continue;
2715 spill = set_find_spill(spill_bb->ilp, remat_arg);
2718 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2719 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2720 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2721 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2725 pset_foreach(live, irn) {
2726 const op_t *op = get_irn_link(irn);
2727 const ir_node *remat;
2732 foreach_post_remat(bb, remat) {
2735 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2736 const ir_node *arg = get_irn_n(remat, n);
2739 const op_t *remat_op = get_irn_link(remat);
2741 if(cst == ILP_UNDEF) {
2742 /* sum remat2s <= 1 + n_remats*live_range */
2743 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2744 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2746 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2752 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2753 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2757 /* first live ranges from reg_ins */
2758 pset_foreach(live, irn) {
2759 op_t *op = get_irn_link(irn);
2761 if(op->attr.live_range.ilp != ILP_UNDEF) {
2763 spill = set_find_spill(spill_bb->ilp, irn);
2764 assert(spill && spill->irn == irn);
2766 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2767 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2768 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2769 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2771 foreach_post_remat(bb, tmp) {
2772 op_t *remat_op = get_irn_link(tmp);
2774 if(remat_op->attr.remat.remat->value == irn) {
2775 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2781 /* walk forward now and compute constraints for placing spills */
2782 /* this must only be done for values that are not defined in this block */
2783 pset_foreach(live, irn) {
2785 * if value is defined in this block we can anways place the spill directly after the def
2786 * -> no constraint necessary
2788 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2793 spill = set_find_spill(spill_bb->ilp, irn);
2796 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2797 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2799 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2800 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2803 sched_foreach_op(bb, tmp) {
2804 op_t *op = get_irn_link(tmp);
2806 if(is_Phi(tmp)) continue;
2807 assert(!is_Proj(tmp));
2810 const ir_node *value = op->attr.remat.remat->value;
2813 /* only collect remats up to the first real use of a value */
2814 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2819 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2820 ir_node *arg = get_irn_n(tmp, n);
2823 /* if a value is used stop collecting remats */
2836 typedef struct _irnlist_t {
2837 struct list_head list;
2841 typedef struct _interference_t {
2842 struct list_head blocklist;
2848 cmp_interference(const void *a, const void *b, size_t size)
2850 const interference_t *p = a;
2851 const interference_t *q = b;
2853 return !(p->a == q->a && p->b == q->b);
2856 static interference_t *
2857 set_find_interference(set * set, ir_node * a, ir_node * b)
2859 interference_t query;
2861 query.a = (a>b)?a:b;
2862 query.b = (a>b)?b:a;
2864 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2867 static interference_t *
2868 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2870 interference_t query,
2872 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2876 result = set_find_interference(set, a, b);
2879 list_add(&list->list, &result->blocklist);
2883 query.a = (a>b)?a:b;
2884 query.b = (a>b)?b:a;
2886 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2888 INIT_LIST_HEAD(&result->blocklist);
2889 list_add(&list->list, &result->blocklist);
2895 values_interfere_in_block(const spill_ilp_t * si, const ir_node * bb, const ir_node * a, const ir_node * b)
2897 const ir_edge_t *edge;
2899 if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2900 /* both values are live in, so they interfere */
2904 /* ensure a dominates b */
2905 if(value_dominates(b,a)) {
2911 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2914 /* the following code is stolen from bera.c */
2915 if(be_is_live_end(si->lv, bb, a))
2918 foreach_out_edge(a, edge) {
2919 const ir_node *user = edge->src;
2920 if(get_nodes_block(user) == bb
2923 && !pset_find_ptr(si->inverse_ops, user)
2924 && value_dominates(b, user))
2932 * Walk all irg blocks and collect interfering values inside of phi classes
2935 luke_interferencewalker(ir_node * bb, void * data)
2937 spill_ilp_t *si = (spill_ilp_t*)data;
2940 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2941 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2942 op_t *a_op = get_irn_link(a);
2945 /* a is only interesting if it is in my register class and if it is inside a phi class */
2946 if (has_reg_class(si, a) && get_phi_class(a)) {
2947 if(a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2950 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)) {
2951 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2952 op_t *b_op = get_irn_link(b);
2955 /* a and b are only interesting if they are in the same phi class */
2956 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2957 if(b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2960 if(values_interfere_in_block(si, bb, a, b)) {
2961 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2962 set_insert_interference(si, si->interferences, a, b, bb);
2970 static unsigned int copy_path_id = 0;
2973 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2980 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2981 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
2983 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2985 pset_foreach(copies, ptr) {
2986 copy = PTR_TO_INT(ptr);
2987 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2992 * @parameter copies contains a path of copies which lead us to irn
2993 * @parameter visited contains a set of nodes already visited on this path
2996 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
2998 const ir_edge_t *edge;
2999 op_t *op = get_irn_link(irn);
3000 pset *visited_users = pset_new_ptr_default();
3003 if(op->is_remat) return 0;
3005 pset_insert_ptr(visited, irn);
3009 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3011 /* visit all operands */
3012 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3013 ir_node *arg = get_irn_n(irn, n);
3014 ilp_var_t copy = op->attr.live_range.args.copies[n];
3016 if(!has_reg_class(si, arg)) continue;
3017 if(pset_find_ptr(visited_operands, arg)) continue;
3018 pset_insert_ptr(visited_operands, arg);
3021 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3022 del_pset(visited_operands);
3023 del_pset(visited_users);
3024 pset_remove_ptr(visited, irn);
3027 pset_insert(copies, INT_TO_PTR(copy), copy);
3028 write_copy_path_cst(si, copies, any_interfere);
3029 pset_remove(copies, INT_TO_PTR(copy), copy);
3030 } else if(!pset_find_ptr(visited, arg)) {
3031 pset_insert(copies, INT_TO_PTR(copy), copy);
3032 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3033 pset_remove(copies, INT_TO_PTR(copy), copy);
3035 if(paths > MAX_PATHS) {
3036 if(pset_count(copies) == 0) {
3040 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3041 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3042 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3043 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3044 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3048 del_pset(visited_operands);
3049 del_pset(visited_users);
3050 pset_remove_ptr(visited, irn);
3053 } else if(pset_count(copies) == 0) {
3059 del_pset(visited_operands);
3062 /* visit all uses which are phis */
3063 foreach_out_edge(irn, edge) {
3064 ir_node *user = edge->src;
3065 int pos = edge->pos;
3066 op_t *op = get_irn_link(user);
3069 if(!is_Phi(user)) continue;
3070 if(!has_reg_class(si, user)) continue;
3071 if(pset_find_ptr(visited_users, user)) continue;
3072 pset_insert_ptr(visited_users, user);
3074 copy = op->attr.live_range.args.copies[pos];
3076 if(user == target) {
3077 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3078 del_pset(visited_users);
3079 pset_remove_ptr(visited, irn);
3082 pset_insert(copies, INT_TO_PTR(copy), copy);
3083 write_copy_path_cst(si, copies, any_interfere);
3084 pset_remove(copies, INT_TO_PTR(copy), copy);
3085 } else if(!pset_find_ptr(visited, user)) {
3086 pset_insert(copies, INT_TO_PTR(copy), copy);
3087 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3088 pset_remove(copies, INT_TO_PTR(copy), copy);
3090 if(paths > MAX_PATHS) {
3091 if(pset_count(copies) == 0) {
3095 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3096 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3097 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3098 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3099 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3103 del_pset(visited_users);
3104 pset_remove_ptr(visited, irn);
3107 } else if(pset_count(copies) == 0) {
3113 del_pset(visited_users);
3114 pset_remove_ptr(visited, irn);
3119 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3121 pset * copies = pset_new_ptr_default();
3122 pset * visited = pset_new_ptr_default();
3124 find_copy_path(si, a, b, any_interfere, copies, visited);
3132 memcopyhandler(spill_ilp_t * si)
3134 interference_t *interference;
3136 /* teste Speicherwerte auf Interferenz */
3138 /* analyze phi classes */
3139 phi_class_compute(si->chordal_env->irg);
3141 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3142 irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
3144 /* now lets emit the ILP unequations for the crap */
3145 set_foreach(si->interferences, interference) {
3147 ilp_var_t interfere,
3149 ilp_cst_t any_interfere_cst,
3151 const ir_node *a = interference->a;
3152 const ir_node *b = interference->b;
3154 /* any_interf <= \sum interf */
3155 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3156 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3157 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3159 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3161 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3162 const ir_node *bb = irnlist->irn;
3163 spill_bb_t *spill_bb = get_irn_link(bb);
3168 spilla = set_find_spill(spill_bb->ilp, a);
3171 spillb = set_find_spill(spill_bb->ilp, b);
3174 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3175 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3176 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3177 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3178 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3179 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3181 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3182 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3184 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3185 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3186 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3187 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3188 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3190 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3191 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3193 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3194 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3195 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3197 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3198 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3200 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3201 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3202 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3205 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3207 /* any_interfere >= interf */
3208 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3209 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3211 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3212 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3215 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3216 gen_copy_constraints(si,a,b,any_interfere);
3224 return fabs(x) < 0.00001;
3227 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3229 spill_ilp_t *si = get_irg_link(current_ir_graph);
3231 if(pset_find_ptr(si->all_possible_remats, n)) {
3232 op_t *op = (op_t*)get_irn_link(n);
3233 assert(op && op->is_remat);
3235 if(!op->attr.remat.remat->inverse) {
3236 if(op->attr.remat.pre) {
3237 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3239 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3244 op_t *op = (op_t*)get_irn_link(n);
3245 assert(op && op->is_remat);
3247 if(op->attr.remat.pre) {
3248 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3250 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3261 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3263 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3264 be_dump(irg, suffix, dump_ir_block_graph_sched);
3265 set_dump_node_vcgattr_hook(NULL);
3269 * Edge hook to dump the schedule edges with annotated register pressure.
3272 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3274 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3275 ir_node *prev = sched_prev(irn);
3276 fprintf(F, "edge:{sourcename:\"");
3278 fprintf(F, "\" targetname:\"");
3280 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3281 fprintf(F, "\" color:magenta}\n");
3287 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3289 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3291 dump_consts_local(0);
3292 set_dump_node_edge_hook(sched_pressure_edge_hook);
3293 dump_ir_block_graph(irg, suffix);
3294 set_dump_node_edge_hook(old_edge_hook);
3298 walker_pressure_annotator(ir_node * bb, void * data)
3300 spill_ilp_t *si = data;
3303 pset *live = pset_new_ptr_default();
3306 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3307 irn = be_lv_get_irn(si->lv, bb, i);
3309 if (has_reg_class(si, irn)) {
3310 pset_insert_ptr(live, irn);
3314 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3316 sched_foreach_reverse(bb, irn) {
3318 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3322 if(has_reg_class(si, irn)) {
3323 pset_remove_ptr(live, irn);
3324 if(is_Proj(irn)) ++projs;
3327 if(!is_Proj(irn)) projs = 0;
3329 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3330 ir_node *arg = get_irn_n(irn, n);
3332 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3334 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3341 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3343 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
3347 connect_all_remats_with_keep(spill_ilp_t * si)
3355 n_remats = pset_count(si->all_possible_remats);
3357 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3360 pset_foreach(si->all_possible_remats, irn) {
3365 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
3367 obstack_free(si->obst, ins);
3372 connect_all_spills_with_keep(spill_ilp_t * si)
3381 n_spills = pset_count(si->spills);
3383 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3386 pset_foreach(si->spills, irn) {
3391 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
3393 obstack_free(si->obst, ins);
3397 /** insert a spill at an arbitrary position */
3398 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert)
3400 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
3401 ir_graph *irg = get_irn_irg(bl);
3402 ir_node *frame = get_irg_frame(irg);
3405 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
3407 spill = be_new_Spill(cls, irg, bl, irn);
3410 * search the right insertion point. a spill of a phi cannot be put
3411 * directly after the phi, if there are some phis behind the one which
3412 * is spilled. Also, a spill of a Proj must be after all Projs of the
3415 * Here's one special case:
3416 * If the spill is in the start block, the spill must be after the frame
3417 * pointer is set up. This is done by setting insert to the end of the block
3418 * which is its default initialization (see above).
3421 if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3424 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3427 sched_add_after(insert, spill);
3432 delete_remat(spill_ilp_t * si, ir_node * remat) {
3434 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3436 sched_remove(remat);
3438 /* kill links to operands */
3439 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3440 set_irn_n(remat, n, bad);
3445 clean_remat_info(spill_ilp_t * si)
3449 remat_info_t *remat_info;
3450 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3452 set_foreach(si->remat_info, remat_info) {
3453 if(!remat_info->remats) continue;
3455 pset_foreach(remat_info->remats, remat)
3457 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3458 if(sched_is_scheduled(remat->proj)) {
3459 sched_remove((ir_node*)remat->proj);
3461 set_irn_n((ir_node*)remat->proj, -1, bad);
3462 set_irn_n((ir_node*)remat->proj, 0, bad);
3465 if(get_irn_n_edges(remat->op) == 0) {
3466 if(sched_is_scheduled(remat->op)) {
3467 sched_remove((ir_node*)remat->op);
3469 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3470 set_irn_n((ir_node*)remat->op, n, bad);
3475 if(remat_info->remats) del_pset(remat_info->remats);
3476 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3481 delete_unnecessary_remats(spill_ilp_t * si)
3483 if(opt_keep_alive & KEEPALIVE_REMATS) {
3485 ir_node *bad = get_irg_bad(si->chordal_env->irg);
3488 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3489 ir_node *keep_arg = get_irn_n(si->keep, n);
3490 op_t *arg_op = get_irn_link(keep_arg);
3493 assert(arg_op->is_remat);
3495 name = si->lpp->vars[arg_op->attr.remat.ilp];
3497 if(is_zero(name->value)) {
3498 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3499 /* TODO check whether reload is preferred over remat (could be bug) */
3500 delete_remat(si, keep_arg);
3502 if(!arg_op->attr.remat.remat->inverse) {
3503 if(arg_op->attr.remat.pre) {
3504 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3506 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3509 if(arg_op->attr.remat.pre) {
3510 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3512 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3517 set_irn_n(si->keep, n, bad);
3520 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3525 pset_foreach(si->all_possible_remats, remat) {
3526 op_t *remat_op = get_irn_link(remat);
3527 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3529 if(is_zero(name->value)) {
3530 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3531 /* TODO check whether reload is preferred over remat (could be bug) */
3532 delete_remat(si, remat);
3534 if(!remat_op->attr.remat.remat->inverse) {
3535 if(remat_op->attr.remat.pre) {
3536 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3538 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3541 if(remat_op->attr.remat.pre) {
3542 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3544 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3553 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3555 pset *spills = pset_new_ptr_default();
3557 const ir_node *next;
3560 defs = set_find_def(si->values, value);
3562 if(defs && defs->spills) {
3563 for(next = defs->spills; next; next = get_irn_link(next)) {
3564 pset_insert_ptr(spills, next);
3572 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3576 assert( get_irn_arity(block) == arity );
3578 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3579 res->attr.phi_backedge = new_backedge_arr(irg->obst, arity);
3585 * @param before The node after which the spill will be placed in the schedule
3588 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3592 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3594 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3596 spill = be_spill2(arch_env, irn, before);
3598 defs = set_insert_def(si->values, value);
3601 /* enter into the linked list */
3602 set_irn_link(spill, defs->spills);
3603 defs->spills = spill;
3605 if(opt_keep_alive & KEEPALIVE_SPILLS)
3606 pset_insert_ptr(si->spills, spill);
3612 * @param before The Phi node which has to be spilled
3615 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3622 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3624 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3625 ins[n] = si->m_unknown;
3628 mem_phi = new_r_PhiM_nokeep(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3630 defs = set_insert_def(si->values, phi);
3633 /* enter into the linked list */
3634 set_irn_link(mem_phi, defs->spills);
3635 defs->spills = mem_phi;
3637 #ifdef SCHEDULE_PHIM
3638 sched_add_after(phi, mem_phi);
3640 pset_insert_ptr(si->phims, mem_phi);
3643 if(opt_keep_alive & KEEPALIVE_SPILLS)
3644 pset_insert_ptr(si->spills, mem_phi);
3651 * Add remat to list of defs, destroys link field!
3654 insert_remat(spill_ilp_t * si, ir_node * remat)
3657 op_t *remat_op = get_irn_link(remat);
3659 assert(remat_op->is_remat);
3661 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3664 /* enter into the linked list */
3665 set_irn_link(remat, defs->remats);
3666 defs->remats = remat;
3671 * Add reload before operation and add to list of defs
3674 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3679 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3681 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3683 defs = set_find_def(si->values, value);
3685 spill = defs->spills;
3686 assert(spill && "no spill placed before reload");
3688 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3690 /* enter into the linked list */
3691 set_irn_link(reload, defs->remats);
3692 defs->remats = reload;
3697 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3700 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3702 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3704 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3706 defs = set_find_def(si->values, value);
3708 spill = defs->spills;
3709 assert(spill && "no spill placed before reload");
3711 arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos);
3714 void insert_memoperands(spill_ilp_t * si)
3716 memoperand_t *memoperand;
3719 set_foreach(si->memoperands, memoperand) {
3720 name = si->lpp->vars[memoperand->ilp];
3721 if(!is_zero(name->value)) {
3722 perform_memory_operand(si, memoperand);
3728 walker_spill_placer(ir_node * bb, void * data) {
3729 spill_ilp_t *si = (spill_ilp_t*)data;
3731 spill_bb_t *spill_bb = get_irn_link(bb);
3732 pset *spills_to_do = pset_new_ptr_default();
3735 set_foreach(spill_bb->ilp, spill) {
3738 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3739 name = si->lpp->vars[spill->mem_in];
3740 if(!is_zero(name->value)) {
3743 mem_phi = insert_mem_phi(si, spill->irn);
3745 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3749 name = si->lpp->vars[spill->spill];
3750 if(!is_zero(name->value)) {
3751 /* place spill directly after definition */
3752 if(get_nodes_block(spill->irn) == bb) {
3753 insert_spill(si, spill->irn, spill->irn, spill->irn);
3757 /* place spill at bb start */
3758 if(spill->reg_in > 0) {
3759 name = si->lpp->vars[spill->reg_in];
3760 if(!is_zero(name->value)) {
3761 insert_spill(si, spill->irn, spill->irn, bb);
3765 /* place spill after a remat */
3766 pset_insert_ptr(spills_to_do, spill->irn);
3769 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3772 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3773 op_t *op = get_irn_link(irn);
3775 if(be_is_Spill(irn)) continue;
3778 /* TODO fix this if we want to support remats with more than two nodes */
3779 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3780 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3782 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3785 if(pset_find_ptr(spills_to_do, irn)) {
3786 pset_remove_ptr(spills_to_do, irn);
3788 insert_spill(si, irn, irn, irn);
3794 assert(pset_count(spills_to_do) == 0);
3796 /* afterwards free data in block */
3797 del_pset(spills_to_do);
3801 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3803 ir_node *insert_pos = bb;
3805 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3807 /* find last definition of arg value in block */
3812 defs = set_find_def(si->values, value);
3814 if(defs && defs->remats) {
3815 for(next = defs->remats; next; next = get_irn_link(next)) {
3816 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3817 last = sched_get_time_step(next);
3823 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3824 last = sched_get_time_step(value);
3828 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3830 spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos);
3836 phim_fixer(spill_ilp_t *si) {
3839 set_foreach(si->values, defs) {
3840 const ir_node *phi = defs->value;
3841 op_t *op = get_irn_link(phi);
3842 ir_node *phi_m = NULL;
3843 ir_node *next = defs->spills;
3846 if(!is_Phi(phi)) continue;
3849 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3853 next = get_irn_link(next);
3856 if(!phi_m) continue;
3858 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3859 ir_node *value = get_irn_n(phi, n);
3860 defs_t *val_defs = set_find_def(si->values, value);
3862 /* a spill of this value */
3867 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3868 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3870 if(!is_zero(name->value)) {
3871 spill = insert_mem_copy(si, pred, value);
3873 spill = val_defs->spills;
3876 spill = val_defs->spills;
3879 assert(spill && "no spill placed before PhiM");
3880 set_irn_n(phi_m, n, spill);
3886 walker_reload_placer(ir_node * bb, void * data) {
3887 spill_ilp_t *si = (spill_ilp_t*)data;
3889 spill_bb_t *spill_bb = get_irn_link(bb);
3891 /* reloads at end of block */
3892 if(spill_bb->reloads) {
3895 set_foreach(spill_bb->reloads, keyval) {
3896 ir_node *irn = (ir_node*)keyval->key;
3897 ilp_var_t reload = PTR_TO_INT(keyval->val);
3900 name = si->lpp->vars[reload];
3901 if(!is_zero(name->value)) {
3903 ir_node *insert_pos = bb;
3904 ir_node *prev = sched_block_last_noncf(si, bb);
3905 op_t *prev_op = get_irn_link(prev);
3907 while(be_is_Spill(prev)) {
3908 prev = sched_prev(prev);
3911 prev_op = get_irn_link(prev);
3913 /* insert reload before pre-remats */
3914 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3915 && prev_op->is_remat && prev_op->attr.remat.pre) {
3919 prev = sched_prev(prev);
3920 } while(be_is_Spill(prev));
3922 prev_op = get_irn_link(prev);
3926 reload = insert_reload(si, irn, insert_pos);
3928 if(opt_keep_alive & KEEPALIVE_RELOADS)
3929 pset_insert_ptr(si->spills, reload);
3934 /* walk and insert more reloads and collect remats */
3935 sched_foreach_reverse(bb, irn) {
3936 op_t *op = get_irn_link(irn);
3938 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3939 if(is_Phi(irn)) break;
3942 if(get_irn_mode(irn) != mode_T) {
3943 insert_remat(si, irn);
3948 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3949 ir_node *arg = get_irn_n(irn, n);
3951 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3954 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3955 if(!is_zero(name->value)) {
3957 ir_node *insert_pos = irn;
3958 ir_node *prev = sched_prev(insert_pos);
3961 while(be_is_Spill(prev)) {
3962 prev = sched_prev(prev);
3965 prev_op = get_irn_link(prev);
3967 /* insert reload before pre-remats */
3968 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3969 && prev_op->is_remat && prev_op->attr.remat.pre) {
3973 prev = sched_prev(prev);
3974 } while(be_is_Spill(prev));
3976 prev_op = get_irn_link(prev);
3980 reload = insert_reload(si, arg, insert_pos);
3982 assert(reload && "no reload returned");
3983 set_irn_n(irn, n, reload);
3985 if(opt_keep_alive & KEEPALIVE_RELOADS)
3986 pset_insert_ptr(si->spills, reload);
3993 del_set(spill_bb->ilp);
3994 if(spill_bb->reloads) del_set(spill_bb->reloads);
3998 walker_collect_used(ir_node * irn, void * data)
4000 bitset_t *used = data;
4002 bitset_set(used, get_irn_idx(irn));
4005 struct kill_helper {
4011 walker_kill_unused(ir_node * bb, void * data)
4013 struct kill_helper *kh = data;
4014 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4018 for(irn=sched_first(bb); !sched_is_end(irn);) {
4019 ir_node *next = sched_next(irn);
4022 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4023 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4024 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)));
4026 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4032 set_nodes_block(irn, bad);
4033 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4034 set_irn_n(irn, n, bad);
4041 #ifndef SCHEDULE_PHIM
4043 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4046 ir_node *bad = get_irg_bad(si->chordal_env->irg);
4049 pset_foreach(si->phims, phi) {
4050 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4052 set_nodes_block(phi, bad);
4053 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4054 set_irn_n(phi, n, bad);
4062 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4064 struct kill_helper kh;
4066 kh.used = bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
4069 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
4070 #ifndef SCHEDULE_PHIM
4071 kill_unused_phims(si, &kh);
4073 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
4075 bitset_free(kh.used);
4079 print_irn_pset(pset * p)
4083 pset_foreach(p, irn) {
4084 ir_printf("%+F\n", irn);
4089 dump_phi_class(spill_ilp_t * si, pset * phiclass, const char * file)
4091 FILE *f = fopen(file, "w");
4093 interference_t *interference;
4095 pset_break(phiclass);
4096 set_break(si->interferences);
4098 ir_fprintf(f, "digraph phiclass {\n");
4100 pset_foreach(phiclass, irn) {
4102 ir_fprintf(f, " %F%N [shape=box]\n",irn,irn);
4105 pset_foreach(phiclass, irn) {
4108 if(!is_Phi(irn)) continue;
4110 for(n=get_irn_arity(irn)-1; n>=0; --n) {
4111 ir_node *arg = get_irn_n(irn, n);
4113 ir_fprintf(f, " %F%N -> %F%N\n",irn,irn,arg,arg);
4117 set_foreach(si->interferences, interference) {
4118 const ir_node *a = interference->a;
4119 const ir_node *b = interference->b;
4120 if(get_phi_class(a) == phiclass) {
4121 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n",a,a,b,b);
4130 rewire_uses(spill_ilp_t * si)
4133 pset *ignore = pset_new_ptr(1);
4134 be_dom_front_info_t *dom_front = si->chordal_env->birg->dom_front;
4136 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
4138 /* then fix uses of spills */
4139 set_foreach(si->values, defs) {
4142 const ir_node *next = defs->remats;
4145 reloads = pset_new_ptr_default();
4148 if(be_is_Reload(next)) {
4149 pset_insert_ptr(reloads, next);
4153 next = get_irn_link(next);
4156 spills = get_spills_for_value(si, defs->value);
4157 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));
4158 if(pset_count(spills) > 1) {
4159 //assert(pset_count(reloads) > 0);
4160 // print_irn_pset(spills);
4161 // print_irn_pset(reloads);
4163 be_ssa_constr_set_ignore(dom_front, si->lv, spills, ignore);
4170 /* first fix uses of remats and reloads */
4171 set_foreach(si->values, defs) {
4173 const ir_node *next = defs->remats;
4177 nodes = pset_new_ptr_default();
4178 if(sched_is_scheduled(defs->value)) {
4179 pset_insert_ptr(nodes, defs->value);
4184 pset_insert_ptr(nodes, next);
4185 next = get_irn_link(next);
4188 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-orig_kept, defs->value));
4189 be_ssa_constr_set(dom_front, si->lv, nodes);
4195 // remove_unused_defs(si);
4200 writeback_results(spill_ilp_t * si)
4202 /* walk through the graph and collect all spills, reloads and remats for a value */
4204 si->values = new_set(cmp_defs, 4096);
4206 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4207 delete_unnecessary_remats(si);
4208 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
4209 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
4210 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
4212 insert_memoperands(si);
4215 /* clean the remat info! there are still back-edges leading there! */
4216 clean_remat_info(si);
4220 connect_all_spills_with_keep(si);
4222 del_set(si->values);
4226 get_n_regs(spill_ilp_t * si)
4228 int arch_n_regs = arch_register_class_n_regs(si->cls);
4230 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4231 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4233 arch_put_non_ignore_regs(si->chordal_env->birg->main_env->arch_env, si->cls, arch_regs);
4234 be_abi_put_ignore_regs(si->chordal_env->birg->abi, si->cls, abi_regs);
4236 bitset_andnot(arch_regs, abi_regs);
4237 arch_n_regs = bitset_popcnt(arch_regs);
4239 bitset_free(arch_regs);
4240 bitset_free(abi_regs);
4242 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4247 walker_reload_mover(ir_node * bb, void * data)
4249 spill_ilp_t *si = data;
4252 sched_foreach(bb, tmp) {
4253 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4254 ir_node *reload = tmp;
4257 /* move reload upwards */
4259 int pressure = (int)get_irn_link(reload);
4260 if(pressure < si->n_regs) {
4261 irn = sched_prev(reload);
4262 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4263 sched_remove(reload);
4264 pressure = (int)get_irn_link(irn);
4266 while(pressure < si->n_regs) {
4267 if( sched_is_end(irn) ||
4268 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4269 /* do not move reload before its spill */
4270 (irn == be_get_Reload_mem(reload)) ||
4271 /* do not move before phi */
4274 set_irn_link(irn, INT_TO_PTR(pressure+1));
4275 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4276 irn = sched_prev(irn);
4278 pressure = (int)get_irn_link(irn);
4281 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4282 sched_put_after(irn, reload);
4289 move_reloads_upward(spill_ilp_t * si)
4291 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
4296 * Walk all irg blocks and check for interfering spills inside of phi classes
4299 luke_meminterferencechecker(ir_node * bb, void * data)
4301 spill_ilp_t *si = (spill_ilp_t*)data;
4304 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4305 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4307 if(!be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T)) continue;
4309 /* a is only interesting if it is in my register class and if it is inside a phi class */
4310 if (has_reg_class(si, a) && get_phi_class(a)) {
4311 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)) {
4312 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4314 if(!be_is_Spill(b) && (!is_Phi(b) || get_irn_mode(b) != mode_T)) continue;
4316 /* a and b are only interesting if they are in the same phi class */
4317 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
4318 if(values_interfere_in_block(si, bb, a, b)) {
4319 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4328 verify_phiclasses(spill_ilp_t * si)
4330 /* analyze phi classes */
4331 phi_class_compute(si->chordal_env->irg);
4333 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4334 irg_block_walk_graph(si->chordal_env->irg, luke_meminterferencechecker, NULL, si);
4338 be_spill_remat(const be_chordal_env_t * chordal_env)
4341 char problem_name[256];
4342 char dump_suffix[256];
4343 char dump_suffix2[256];
4344 struct obstack obst;
4346 be_irg_t *birg = chordal_env->birg;
4348 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
4349 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
4350 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
4352 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4353 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4355 if(opt_verify & VERIFY_DOMINANCE)
4356 be_check_dominance(chordal_env->irg);
4358 be_assure_dom_front(birg);
4359 be_assure_liveness(birg);
4361 obstack_init(&obst);
4362 si.chordal_env = chordal_env;
4364 si.cls = chordal_env->cls;
4365 si.lpp = new_lpp(problem_name, lpp_minimize);
4366 si.remat_info = new_set(cmp_remat_info, 4096);
4367 si.interferences = new_set(cmp_interference, 32);
4368 si.memoperands = new_set(cmp_memoperands, 128);
4369 si.all_possible_remats = pset_new_ptr_default();
4370 si.spills = pset_new_ptr_default();
4371 si.inverse_ops = pset_new_ptr_default();
4374 si.n_regs = get_n_regs(&si);
4376 set_irg_link(chordal_env->irg, &si);
4377 compute_doms(chordal_env->irg);
4379 /* compute phi classes */
4380 // phi_class_compute(chordal_env->irg);
4382 if(opt_dump_flags & DUMP_STATS)
4383 be_analyze_regpressure(chordal_env, "-pre");
4385 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4386 irg_block_walk_graph(chordal_env->irg, luke_initializer, NULL, &si);
4389 /* collect remats */
4390 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4391 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
4394 /* insert possible remats */
4395 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4396 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
4397 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4399 if(opt_keep_alive & KEEPALIVE_REMATS) {
4400 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4401 connect_all_remats_with_keep(&si);
4402 /* dump graph with inserted remats */
4403 dump_graph_with_remats(chordal_env->irg, dump_suffix);
4406 /* insert copies for phi arguments not in my regclass */
4407 irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si);
4409 /* recompute liveness */
4410 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4411 be_liveness_recompute(si.lv);
4414 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4415 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4416 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
4418 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4419 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
4422 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4423 memcopyhandler(&si);
4426 if(opt_dump_flags & DUMP_PROBLEM) {
4428 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4429 if ((f = fopen(buf, "wt")) != NULL) {
4430 lpp_dump_plain(si.lpp, f);
4435 if(opt_dump_flags & DUMP_MPS) {
4438 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4439 if((f = fopen(buf, "wt")) != NULL) {
4440 mps_write_mps(si.lpp, s_mps_fixed, f);
4444 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4445 if((f = fopen(buf, "wt")) != NULL) {
4446 mps_write_mst(si.lpp, s_mps_fixed, f);
4451 lpp_check_startvals(si.lpp);
4454 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4455 lpp_set_time_limit(si.lpp, opt_timeout);
4458 lpp_set_log(si.lpp, stdout);
4461 lpp_solve_cplex(si.lpp);
4463 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4465 assert(lpp_is_sol_valid(si.lpp)
4466 && "solution of ILP must be valid");
4468 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));
4470 if(opt_dump_flags & DUMP_SOLUTION) {
4474 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4475 if ((f = fopen(buf, "wt")) != NULL) {
4477 for (i = 0; i < si.lpp->var_next; ++i) {
4478 lpp_name_t *name = si.lpp->vars[i];
4479 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4485 #ifndef SCHEDULE_PHIM
4486 si.phims = pset_new_ptr_default();
4488 writeback_results(&si);
4493 kill_all_unused_values_in_schedule(&si);
4495 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4499 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4500 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
4502 // move reloads upwards
4503 be_liveness_recompute(si.lv);
4504 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4505 move_reloads_upward(&si);
4508 verify_phiclasses(&si);
4511 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
4513 if(opt_dump_flags & DUMP_PRESSURE)
4514 dump_pressure_graph(&si, dump_suffix2);
4516 if(opt_dump_flags & DUMP_STATS)
4517 be_analyze_regpressure(chordal_env, "-post");
4519 if(opt_verify & VERIFY_DOMINANCE)
4520 be_check_dominance(chordal_env->irg);
4522 free_dom(chordal_env->irg);
4523 del_set(si.interferences);
4524 del_pset(si.inverse_ops);
4525 del_pset(si.all_possible_remats);
4526 del_set(si.memoperands);
4527 del_pset(si.spills);
4529 obstack_free(&obst, NULL);
4530 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4533 void be_init_spillremat(void)
4535 static be_spiller_t remat_spiller = {
4538 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
4539 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
4540 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
4541 lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat");
4543 be_register_spiller("remat", &remat_spiller);
4544 lc_opt_add_table(remat_grp, options);
4547 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat);
4549 #else /* WITH_ILP */
4552 only_that_you_can_compile_without_WITH_ILP_defined(void)
4556 #endif /* WITH_ILP */