2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief ILP based spilling & rematerialization
23 * @author Adam M. Szalkowski
47 #include "irnodeset.h"
49 #include "iredges_t.h"
52 #include "irbackedge_t.h"
53 #include "irprofile.h"
57 #include <lpp/lpp_net.h>
58 #include <lpp/lpp_cplex.h>
63 #include "besched_t.h"
64 #include "bessaconstr.h"
66 #include "beintlive_t.h"
70 #include "bespillremat.h"
72 #include "bepressurestat.h"
73 #include "bespilloptions.h"
74 #include "bechordal_t.h"
78 #include "lc_opts_enum.h"
80 #define DUMP_PROBLEM 1
82 #define DUMP_SOLUTION 4
84 #define DUMP_PRESSURE 16
86 #define KEEPALIVE_REMATS 1
87 #define KEEPALIVE_SPILLS 2
88 #define KEEPALIVE_RELOADS 4
90 #define VERIFY_MEMINTERF 1
91 #define VERIFY_DOMINANCE 2
94 #define REMATS_BRIGGS 1
95 #define REMATS_NOINVERSE 2
98 static unsigned opt_dump_flags = 0;
99 static int opt_log = 0;
100 static unsigned opt_keep_alive = 0;
101 static int opt_goodwin = 1;
102 static int opt_memcopies = 1;
103 static int opt_memoperands = 1;
104 static int opt_verify = VERIFY_MEMINTERF;
105 static unsigned opt_remats = REMATS_ALL;
106 static int opt_repair_schedule = 0;
107 static int opt_no_enlarge_liveness = 0;
108 static int opt_remat_while_live = 1;
109 static int opt_timeout = 300;
110 static double opt_cost_reload = 8.0;
111 static double opt_cost_memoperand = 7.0;
112 static double opt_cost_spill = 15.0;
113 static double opt_cost_remat = 1.0;
116 static const lc_opt_enum_mask_items_t dump_items[] = {
117 { "problem", DUMP_PROBLEM },
119 { "solution", DUMP_SOLUTION },
120 { "stats", DUMP_STATS },
121 { "pressure", DUMP_PRESSURE },
125 static lc_opt_enum_mask_var_t dump_var = {
126 &opt_dump_flags, dump_items
129 static const lc_opt_enum_mask_items_t keepalive_items[] = {
130 { "remats", KEEPALIVE_REMATS },
131 { "spills", KEEPALIVE_SPILLS },
132 { "reloads", KEEPALIVE_RELOADS },
136 static lc_opt_enum_mask_var_t keep_alive_var = {
137 &opt_keep_alive, keepalive_items
140 static const lc_opt_enum_mask_items_t remats_items[] = {
141 { "none", REMATS_NONE },
142 { "briggs", REMATS_BRIGGS },
143 { "noinverse", REMATS_NOINVERSE },
144 { "all", REMATS_ALL },
148 static lc_opt_enum_mask_var_t remats_var = {
149 &opt_remats, remats_items
152 static const lc_opt_table_entry_t options[] = {
153 LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var),
155 LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin),
156 LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies),
157 LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands),
158 LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var),
159 LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule),
160 LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
161 LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live),
163 LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var),
164 LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log),
165 LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout),
167 LC_OPT_ENT_DBL ("cost_reload", "cost of a reload", &opt_cost_reload),
168 LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand),
169 LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill),
170 LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat),
174 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
175 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
178 //#define SOLVE_LOCAL
179 #define LPP_SERVER "i44pc52"
180 #define LPP_SOLVER "cplex"
183 #define MAX_PATHS INT_MAX
186 typedef struct _spill_ilp_t {
187 const arch_register_class_t *cls;
192 struct obstack *obst;
194 pset *all_possible_remats;
197 set *values; /**< for collecting all definitions of values before running ssa-construction */
203 #ifndef SCHEDULE_PHIM
206 DEBUG_ONLY(firm_dbg_module_t * dbg);
209 typedef int ilp_var_t;
210 typedef int ilp_cst_t;
212 typedef struct _spill_bb_t {
217 typedef struct _remat_t {
218 const ir_node *op; /**< for copy_irn */
219 const ir_node *value; /**< the value which is being recomputed by this remat */
220 const ir_node *proj; /**< not NULL if the above op produces a tuple */
221 int cost; /**< cost of this remat */
222 int inverse; /**< nonzero if this is an inverse remat */
226 * Data to be attached to each IR node. For remats this contains the ilp_var
227 * for this remat and for normal ops this contains the ilp_vars for
228 * reloading each operand
230 typedef struct _op_t {
235 const remat_t *remat; /** the remat this op belongs to */
236 int pre; /** 1, if this is a pressure-increasing remat */
240 ir_node *op; /** the operation this live range belongs to */
249 typedef struct _defs_t {
250 const ir_node *value;
251 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
252 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
255 typedef struct _remat_info_t {
256 const ir_node *irn; /**< the irn to which these remats belong */
257 pset *remats; /**< possible remats for this value */
258 pset *remats_by_operand; /**< remats with this value as operand */
261 typedef struct _keyval_t {
266 typedef struct _spill_t {
275 typedef struct _memoperand_t {
276 ir_node *irn; /**< the irn */
277 unsigned int pos; /**< the position of the argument */
278 ilp_var_t ilp; /**< the ilp var for this memory operand */
282 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
284 return arch_irn_consider_in_reg_alloc(si->cls, irn);
289 cmp_remat(const void *a, const void *b)
291 const keyval_t *p = a;
292 const keyval_t *q = b;
293 const remat_t *r = p->val;
294 const remat_t *s = q->val;
298 return !(r == s || r->op == s->op);
302 cmp_remat(const void *a, const void *b)
304 const remat_t *r = a;
305 const remat_t *s = b;
307 return !(r == s || r->op == s->op);
311 cmp_spill(const void *a, const void *b, size_t size)
313 const spill_t *p = a;
314 const spill_t *q = b;
317 // return !(p->irn == q->irn && p->bb == q->bb);
318 return !(p->irn == q->irn);
322 cmp_memoperands(const void *a, const void *b, size_t size)
324 const memoperand_t *p = a;
325 const memoperand_t *q = b;
328 return !(p->irn == q->irn && p->pos == q->pos);
332 set_find_keyval(set * set, const void * key)
337 return set_find(set, &query, sizeof(query), HASH_PTR(key));
341 set_insert_keyval(set * set, void * key, void * val)
347 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
351 set_find_def(set * set, const ir_node * value)
356 return set_find(set, &query, sizeof(query), HASH_PTR(value));
360 set_insert_def(set * set, const ir_node * value)
367 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
370 static memoperand_t *
371 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
378 return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
381 static memoperand_t *
382 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
386 query.irn = (ir_node*)irn;
388 return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
393 set_find_spill(set * set, const ir_node * value)
397 query.irn = (ir_node*)value;
398 return set_find(set, &query, sizeof(query), HASH_PTR(value));
401 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
402 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
403 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
404 #define foreach_pre_remat(s,i) for((i)=next_pre_remat((s)); (i); (i)=next_pre_remat((i)))
405 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
408 cmp_remat_info(const void *a, const void *b, size_t size)
410 const remat_info_t *p = a;
411 const remat_info_t *q = b;
414 return !(p->irn == q->irn);
418 cmp_defs(const void *a, const void *b, size_t size)
424 return !(p->value == q->value);
428 cmp_keyval(const void *a, const void *b, size_t size)
430 const keyval_t *p = a;
431 const keyval_t *q = b;
434 return !(p->key == q->key);
438 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
441 if(ir_profile_has_data())
442 return ((double)ir_profile_get_block_execcount(get_block_const(irn))) + FUDGE;
444 #ifndef EXECFREQ_LOOPDEPH
445 return get_block_execfreq(si->birg->exec_freq, get_block_const(irn)) + FUDGE;
448 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
450 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
454 static double get_cost(const ir_node *irn)
456 if(be_is_Spill(irn)) {
457 return opt_cost_spill;
458 } else if(be_is_Reload(irn)){
459 return opt_cost_reload;
461 return arch_get_op_estimated_cost(irn);
466 * Checks, whether node and its operands have suitable reg classes
469 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
472 int remat = (arch_irn_get_flags(irn) & arch_irn_flags_rematerializable) != 0;
476 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
479 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
480 ir_node *op = get_irn_n(irn, n);
481 remat &= has_reg_class(si, op) || arch_irn_get_flags(op) & arch_irn_flags_ignore || is_NoMem(op);
484 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
491 * Try to create a remat from @p op with destination value @p dest_value
493 static INLINE remat_t *
494 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
496 remat_t *remat = NULL;
498 // if(!mode_is_datab(get_irn_mode(dest_value)))
501 if(dest_value == op) {
502 const ir_node *proj = NULL;
504 if(is_Proj(dest_value)) {
505 op = get_Proj_pred(op);
509 if(!is_rematerializable(si, op))
512 remat = obstack_alloc(si->obst, sizeof(*remat));
514 remat->cost = (int)get_cost(op);
515 remat->value = dest_value;
519 arch_inverse_t inverse;
522 /* get the index of the operand we want to retrieve by the inverse op */
523 for (n = get_irn_arity(op)-1; n>=0; --n) {
524 ir_node *arg = get_irn_n(op, n);
526 if(arg == dest_value) break;
530 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
532 /* else ask the backend to give an inverse op */
533 if(arch_get_inverse(op, n, &inverse, si->obst)) {
536 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
538 assert(inverse.n > 0 && "inverse op should have at least one node");
540 for(i=inverse.n-1; i>=0; --i) {
541 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
545 remat = obstack_alloc(si->obst, sizeof(*remat));
546 remat->op = inverse.nodes[0];
547 remat->cost = inverse.costs;
548 remat->value = dest_value;
549 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
552 // Matze: commented this out, this doesn't seem to be true if
553 // the inverse is a simple operation with only 1 result...
554 //assert(is_Proj(remat->proj));
556 assert(0 && "I can not handle remats with more than 2 nodes");
563 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
565 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
573 add_remat(const spill_ilp_t * si, const remat_t * remat)
575 remat_info_t *remat_info,
580 assert(remat->value);
582 query.irn = remat->value;
584 query.remats_by_operand = NULL;
585 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
587 if(remat_info->remats == NULL) {
588 remat_info->remats = new_pset(cmp_remat, 4096);
590 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
592 /* insert the remat into the remats_be_operand set of each argument of the remat op */
593 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
594 ir_node *arg = get_irn_n(remat->op, n);
598 query.remats_by_operand = NULL;
599 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
601 if(remat_info->remats_by_operand == NULL) {
602 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
604 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
609 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
611 const ir_edge_t *edge = get_irn_out_edge_first(irn);
615 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
618 edge = get_irn_out_edge_next(irn, edge);
625 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
631 irn = get_Proj_pred(irn);
633 for(n=get_irn_arity(irn)-1; n>=0; --n) {
634 const ir_node *arg = get_irn_n(irn, n);
636 if(has_reg_class(si, arg)) ++ret;
643 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
648 if( has_reg_class(si, op)
649 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
650 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
652 remat = get_remat_from_op(si, op, op);
654 add_remat(si, remat);
658 if(opt_remats == REMATS_ALL) {
659 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
661 for (n = get_irn_arity(op)-1; n>=0; --n) {
662 ir_node *arg = get_irn_n(op, n);
664 if(has_reg_class(si, arg)) {
665 /* try to get an inverse remat */
666 remat = get_remat_from_op(si, arg, op);
668 add_remat(si, remat);
676 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
679 ir_node *def_block = get_nodes_block(val);
686 /* if pos is at end of a basic block */
688 ret = (pos == def_block || block_dominates(def_block, pos));
689 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
693 /* else if this is a normal operation */
694 block = get_nodes_block(pos);
695 if(block == def_block) {
696 if(!sched_is_scheduled(val)) return 1;
698 ret = sched_comes_after(val, pos);
699 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
703 ret = block_dominates(def_block, block);
704 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
708 static INLINE ir_node *sched_block_last_noncf(const ir_node * bb)
710 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, NULL);
714 * Returns first non-Phi node of block @p bb
716 static INLINE ir_node *
717 sched_block_first_nonphi(const ir_node * bb)
719 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
723 sched_skip_proj_predicator(const ir_node * irn, void * data)
726 return (is_Proj(irn));
729 static INLINE ir_node *
730 sched_next_nonproj(const ir_node * irn, int forward)
732 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
736 * Returns next operation node (non-Proj) after @p irn
737 * or the basic block of this node
739 static INLINE ir_node *
740 sched_next_op(const ir_node * irn)
742 ir_node *next = sched_next(irn);
747 return sched_next_nonproj(next, 1);
751 * Returns previous operation node (non-Proj) before @p irn
752 * or the basic block of this node
754 static INLINE ir_node *
755 sched_prev_op(const ir_node * irn)
757 ir_node *prev = sched_prev(irn);
762 return sched_next_nonproj(prev, 0);
766 sched_put_after(ir_node * insert, ir_node * irn)
768 if(is_Block(insert)) {
769 insert = sched_block_first_nonphi(insert);
771 insert = sched_next_op(insert);
774 sched_add_before(insert, irn);
777 static void sched_put_before(ir_node * insert, ir_node * irn)
779 if(is_Block(insert)) {
780 insert = sched_block_last_noncf(insert);
782 insert = sched_next_nonproj(insert, 0);
783 insert = sched_prev(insert);
786 sched_add_after(insert, irn);
790 next_post_remat(const ir_node * irn)
796 next = sched_block_first_nonphi(irn);
798 next = sched_next_op(irn);
801 if(sched_is_end(next))
804 op = get_irn_link(next);
805 if(op->is_remat && !op->attr.remat.pre) {
813 static ir_node *next_pre_remat(const ir_node * irn)
819 ret = sched_block_last_noncf(irn);
820 ret = sched_next(ret);
821 ret = sched_prev_op(ret);
823 ret = sched_prev_op(irn);
826 if(sched_is_end(ret) || is_Phi(ret))
829 op = (op_t*)get_irn_link(ret);
830 if(op->is_remat && op->attr.remat.pre) {
838 * Tells you whether a @p remat can be placed before the irn @p pos
841 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
843 const ir_node *op = remat->op;
849 prev = sched_block_last_noncf(pos);
850 prev = sched_next_nonproj(prev, 0);
852 prev = sched_prev_op(pos);
854 /* do not remat if the rematted value is defined immediately before this op */
855 if(prev == remat->op) {
860 /* this should be just fine, the following OP will be using this value, right? */
862 /* only remat AFTER the real definition of a value (?) */
863 if(!value_is_defined_before(si, pos, remat->value)) {
864 // ir_fprintf(stderr, "error(not defined)");
869 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
870 const ir_node *arg = get_irn_n(op, n);
872 if(opt_no_enlarge_liveness) {
873 if(has_reg_class(si, arg) && live) {
874 res &= pset_find_ptr((pset*)live, arg)?1:0;
876 res &= value_is_defined_before(si, pos, arg);
879 res &= value_is_defined_before(si, pos, arg);
887 * Tells you whether a @p remat can be placed after the irn @p pos
890 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
893 pos = sched_block_first_nonphi(pos);
895 pos = sched_next_op(pos);
898 /* only remat AFTER the real definition of a value (?) */
899 if(!value_is_defined_before(si, pos, remat->value)) {
903 return can_remat_before(si, remat, pos, live);
907 * Collect potetially rematerializable OPs
910 walker_remat_collector(ir_node * irn, void * data)
912 spill_ilp_t *si = data;
914 if(!is_Block(irn) && !is_Phi(irn)) {
915 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
916 get_remats_from_op(si, irn);
921 * Inserts a copy of @p irn before @p pos
924 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
929 bb = is_Block(pos)?pos:get_nodes_block(pos);
930 copy = exact_copy(irn);
932 set_phi_class(si->pc, copy, NULL);
933 set_nodes_block(copy, bb);
934 sched_put_before(pos, copy);
940 * Inserts a copy of @p irn after @p pos
943 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
948 bb = is_Block(pos)?pos:get_nodes_block(pos);
949 copy = exact_copy(irn);
951 set_phi_class(si->pc, copy, NULL);
952 set_nodes_block(copy, bb);
953 sched_put_after(pos, copy);
959 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
963 if(can_remat_after(si, remat, pos, live)) {
968 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
970 copy = insert_copy_after(si, remat->op, pos);
972 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
973 op = obstack_alloc(si->obst, sizeof(*op));
975 op->attr.remat.remat = remat;
976 op->attr.remat.pre = 0;
977 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
979 set_irn_link(copy, op);
980 pset_insert_ptr(si->all_possible_remats, copy);
982 proj_copy = insert_copy_after(si, remat->proj, copy);
983 set_irn_n(proj_copy, 0, copy);
984 set_irn_link(proj_copy, op);
985 pset_insert_ptr(si->all_possible_remats, proj_copy);
997 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
1001 if(can_remat_before(si, remat, pos, live)) {
1006 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
1008 copy = insert_copy_before(si, remat->op, pos);
1010 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
1011 op = obstack_alloc(si->obst, sizeof(*op));
1013 op->attr.remat.remat = remat;
1014 op->attr.remat.pre = 1;
1015 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1017 set_irn_link(copy, op);
1018 pset_insert_ptr(si->all_possible_remats, copy);
1020 proj_copy = insert_copy_after(si, remat->proj, copy);
1021 set_irn_n(proj_copy, 0, copy);
1022 set_irn_link(proj_copy, op);
1023 pset_insert_ptr(si->all_possible_remats, proj_copy);
1035 get_block_n_succs(const ir_node *block) {
1036 const ir_edge_t *edge;
1038 assert(edges_activated(current_ir_graph));
1040 edge = get_block_succ_first(block);
1044 edge = get_block_succ_next(block, edge);
1045 return edge ? 2 : 1;
1049 is_start_block(const ir_node * bb)
1051 return get_irg_start_block(get_irn_irg(bb)) == bb;
1055 is_merge_edge(const ir_node * bb)
1057 if(is_start_block(bb))
1061 return get_block_n_succs(bb) == 1;
1067 is_diverge_edge(const ir_node * bb)
1069 if(is_start_block(bb))
1073 return get_Block_n_cfgpreds(bb) == 1;
1079 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1084 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1085 irn = be_lv_get_irn(si->lv, bb, i);
1087 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1088 pset_insert_ptr(live, irn);
1092 irn = sched_last(bb);
1094 /* all values eaten by control flow operations are also live until the end of the block */
1095 sched_foreach_reverse(bb, irn) {
1098 if (!sched_skip_cf_predicator(irn, NULL)) break;
1100 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1101 ir_node *arg = get_irn_n(irn,i);
1103 if(has_reg_class(si, arg)) {
1104 pset_insert_ptr(live, arg);
1109 * find values that are used by remats at end of block
1110 * and insert them into live set
1112 foreach_pre_remat(bb, irn) {
1115 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1116 ir_node *remat_arg = get_irn_n(irn, n);
1118 if(!has_reg_class(si, remat_arg)) continue;
1120 /* if value is becoming live through use by remat */
1121 if(!pset_find_ptr(live, remat_arg)) {
1122 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1124 pset_insert_ptr(live, remat_arg);
1131 walker_regclass_copy_insertor(ir_node * irn, void * data)
1133 spill_ilp_t *si = data;
1135 if(is_Phi(irn) && has_reg_class(si, irn)) {
1138 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1139 ir_node *phi_arg = get_irn_n(irn, n);
1140 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1142 if(!has_reg_class(si, phi_arg)) {
1143 ir_node *copy = be_new_Copy(si->cls, si->birg->irg, bb, phi_arg);
1144 ir_node *pos = sched_block_last_noncf(bb);
1145 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1147 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1148 sched_add_after(pos, copy);
1149 set_irn_n(irn, n, copy);
1152 op->attr.live_range.args.reloads = NULL;
1153 op->attr.live_range.ilp = ILP_UNDEF;
1154 set_irn_link(copy, op);
1161 * Insert (so far unused) remats into the irg to
1162 * recompute the potential liveness of all values
1165 walker_remat_insertor(ir_node * bb, void * data)
1167 spill_ilp_t *si = data;
1174 /* skip start block, no remats to do there */
1175 if(is_start_block(bb)) return;
1177 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1179 live = pset_new_ptr_default();
1180 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1181 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1183 /* add remats at end of block */
1184 if (has_reg_class(si, value)) {
1185 pset_insert_ptr(live, value);
1189 irn = sched_last(bb);
1190 while(!sched_is_end(irn)) {
1196 next = sched_prev(irn);
1198 /* delete defined value from live set */
1199 if(has_reg_class(si, irn)) {
1200 pset_remove_ptr(live, irn);
1203 if(is_Phi(irn) || is_Proj(irn)) {
1208 args = pset_new_ptr_default();
1209 used = pset_new_ptr_default();
1211 /* collect arguments of op and set args of op already live in epilog */
1212 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1213 ir_node *arg = get_irn_n(irn, n);
1215 pset_insert_ptr(args, arg);
1216 if(has_reg_class(si, arg)) {
1217 pset_insert_ptr(live, arg);
1218 pset_insert_ptr(used, arg);
1222 /* insert all possible remats before irn */
1223 pset_foreach(args, arg) {
1224 remat_info_t *remat_info,
1227 /* continue if the operand has the wrong reg class */
1228 if(!has_reg_class(si, arg))
1232 query.remats = NULL;
1233 query.remats_by_operand = NULL;
1234 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1240 if(remat_info->remats) {
1241 pset_foreach(remat_info->remats, remat) {
1242 ir_node *remat_irn = NULL;
1244 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1245 remat_irn = insert_remat_before(si, remat, irn, live);
1248 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1249 ir_node *remat_arg = get_irn_n(remat_irn, n);
1251 /* collect args of remats which are not args of op */
1252 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1253 pset_insert_ptr(used, remat_arg);
1261 /* do not place post remats after jumps */
1262 if (sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) {
1268 /* insert all possible remats after irn */
1269 post_remats = pset_new_ptr_default();
1270 pset_foreach(used, arg) {
1271 remat_info_t *remat_info,
1274 /* continue if the operand has the wrong reg class */
1275 if(!has_reg_class(si, arg))
1279 query.remats = NULL;
1280 query.remats_by_operand = NULL;
1281 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1287 if(remat_info->remats_by_operand) {
1288 pset_foreach(remat_info->remats_by_operand, remat) {
1289 /* do not insert remats producing the same value as one of the operands */
1290 if(!pset_find_ptr(args, remat->value)) {
1291 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1293 /* only remat values that can be used by real ops */
1294 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1295 pset_insert_ptr(post_remats, remat);
1301 pset_foreach(post_remats, remat) {
1302 insert_remat_after(si, remat, irn, live);
1304 del_pset(post_remats);
1311 /* add remats at end if successor has multiple predecessors */
1312 if(is_merge_edge(bb)) {
1313 pset *live_out = pset_new_ptr_default();
1316 get_live_end(si, bb, live_out);
1318 /* add remats at end of block */
1319 pset_foreach(live_out, value) {
1320 remat_info_t *remat_info,
1324 query.remats = NULL;
1325 query.remats_by_operand = NULL;
1326 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1328 if(remat_info && remat_info->remats) {
1329 pset_foreach(remat_info->remats, remat) {
1330 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1332 insert_remat_before(si, remat, bb, live_out);
1339 if(is_diverge_edge(bb)) {
1340 pset *live_in = pset_new_ptr_default();
1343 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1344 value = be_lv_get_irn(si->lv, bb, i);
1346 if(has_reg_class(si, value)) {
1347 pset_insert_ptr(live_in, value);
1350 /* add phis to live_in */
1351 sched_foreach(bb, value) {
1352 if(!is_Phi(value)) break;
1354 if(has_reg_class(si, value)) {
1355 pset_insert_ptr(live_in, value);
1359 /* add remat2s at beginning of block */
1360 post_remats = pset_new_ptr_default();
1361 pset_foreach(live_in, value) {
1362 remat_info_t *remat_info,
1366 query.remats = NULL;
1367 query.remats_by_operand = NULL;
1368 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1370 if(remat_info && remat_info->remats_by_operand) {
1371 pset_foreach(remat_info->remats_by_operand, remat) {
1372 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1374 /* put the remat here if all its args are available and result is still live */
1375 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1376 pset_insert_ptr(post_remats, remat);
1381 pset_foreach(post_remats, remat) {
1382 insert_remat_after(si, remat, bb, live_in);
1384 del_pset(post_remats);
1390 can_be_copied(const ir_node * bb, const ir_node * irn)
1392 const ir_edge_t *edge = get_block_succ_first(bb);
1393 const ir_node *next_bb = edge->src;
1394 int pos = edge->pos;
1397 assert(is_merge_edge(bb));
1399 sched_foreach(next_bb, phi) {
1400 const ir_node *phi_arg;
1402 if(!is_Phi(phi)) break;
1404 phi_arg = get_irn_n(phi, pos);
1406 if(phi_arg == irn) {
1414 * Initialize additional node info
1417 luke_initializer(ir_node * bb, void * data)
1419 spill_ilp_t *si = (spill_ilp_t*)data;
1420 spill_bb_t *spill_bb;
1423 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1424 set_irn_link(bb, spill_bb);
1426 sched_foreach(bb, irn) {
1429 op = obstack_alloc(si->obst, sizeof(*op));
1431 op->attr.live_range.ilp = ILP_UNDEF;
1434 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1435 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1437 } else if(!is_Proj(irn)) {
1438 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1439 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1441 op->attr.live_range.args.reloads = NULL;
1443 set_irn_link(irn, op);
1449 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1452 luke_endwalker(ir_node * bb, void * data)
1454 spill_ilp_t *si = (spill_ilp_t*)data;
1460 spill_bb_t *spill_bb = get_irn_link(bb);
1463 live = pset_new_ptr_default();
1464 use_end = pset_new_ptr_default();
1466 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1467 irn = be_lv_get_irn(si->lv, bb, i);
1468 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1469 pset_insert_ptr(live, irn);
1473 * find values that are used by remats at end of block
1474 * and insert them into live set
1476 foreach_pre_remat(bb, irn) {
1479 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1480 ir_node *remat_arg = get_irn_n(irn, n);
1482 if(has_reg_class(si, remat_arg)) {
1483 pset_insert_ptr(live, remat_arg);
1488 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1489 /* their reg_out must always be set */
1490 sched_foreach_reverse(bb, irn) {
1493 if (!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break;
1495 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1496 ir_node *irn_arg = get_irn_n(irn, n);
1498 if(has_reg_class(si, irn_arg)) {
1499 pset_insert_ptr(use_end, irn_arg);
1504 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1505 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1506 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1508 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1510 /* if this is a merge edge we can reload at the end of this block */
1511 if(is_merge_edge(bb)) {
1512 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1513 } else if(pset_count(use_end)){
1514 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1516 spill_bb->reloads = NULL;
1519 pset_foreach(live,irn) {
1523 int default_spilled;
1526 /* handle values used by control flow nodes later separately */
1527 if(pset_find_ptr(use_end, irn)) continue;
1530 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1532 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1534 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1535 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1536 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1538 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1539 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1541 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1542 /* by default spill value right after definition */
1543 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1544 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1546 if(is_merge_edge(bb)) {
1550 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1551 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1552 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1554 /* reload <= mem_out */
1555 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1556 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1557 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1560 spill->reg_in = ILP_UNDEF;
1561 spill->mem_in = ILP_UNDEF;
1564 pset_foreach(use_end,irn) {
1568 ilp_cst_t end_use_req,
1571 int default_spilled;
1574 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1576 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1578 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1579 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1581 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1582 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1584 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1585 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1586 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1588 /* reload for use be control flow op */
1589 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1590 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1591 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1593 /* reload <= mem_out */
1594 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1595 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1596 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1598 spill->reg_in = ILP_UNDEF;
1599 spill->mem_in = ILP_UNDEF;
1601 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1602 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1603 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1612 * Find a remat of value @p value in the epilog of @p pos
1615 find_post_remat(const ir_node * value, const ir_node * pos)
1617 while((pos = next_post_remat(pos)) != NULL) {
1620 op = get_irn_link(pos);
1621 assert(op->is_remat && !op->attr.remat.pre);
1623 if(op->attr.remat.remat->value == value)
1624 return (ir_node*)pos;
1627 const ir_edge_t *edge;
1628 foreach_out_edge(pos, edge) {
1629 ir_node *proj = get_edge_src_irn(edge);
1630 assert(is_Proj(proj));
1641 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1643 spill_bb_t *spill_bb = get_irn_link(bb);
1647 int default_spilled;
1650 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1652 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1654 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1656 spill->reg_out = ILP_UNDEF;
1657 spill->reg_in = ILP_UNDEF;
1658 spill->mem_in = ILP_UNDEF;
1660 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1661 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1663 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1664 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1665 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1672 * Inserts ILP-constraints and variables for memory copying before the given position
1675 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1677 const ir_node *succ;
1678 const ir_edge_t *edge;
1679 spill_bb_t *spill_bb = get_irn_link(block);
1688 assert(edges_activated(current_ir_graph));
1690 edge = get_block_succ_first(block);
1696 edge = get_block_succ_next(block, edge);
1697 /* next block can only contain phis, if this is a merge edge */
1700 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1701 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1703 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1704 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1706 pset_foreach(live, tmp) {
1709 op_t *op = get_irn_link(irn);
1710 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1712 spill = set_find_spill(spill_bb->ilp, tmp);
1715 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1717 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1719 sched_foreach(succ, phi) {
1720 const ir_node *to_copy;
1722 spill_t *to_copy_spill;
1723 op_t *phi_op = get_irn_link(phi);
1724 ilp_var_t reload = ILP_UNDEF;
1727 if(!is_Phi(phi)) break;
1728 if(!has_reg_class(si, phi)) continue;
1730 to_copy = get_irn_n(phi, pos);
1731 to_copy_op = get_irn_link(to_copy);
1733 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1734 assert(to_copy_spill);
1736 if(spill_bb->reloads) {
1737 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1740 reload = PTR_TO_INT(keyval->val);
1744 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1745 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1747 /* copy - reg_out - reload - remat - live_range <= 0 */
1748 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1749 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1750 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1751 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1752 foreach_pre_remat(block, tmp) {
1753 op_t *remat_op = get_irn_link(tmp);
1754 if(remat_op->attr.remat.remat->value == to_copy) {
1755 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1759 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1760 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1762 /* copy - reg_out - copyreg <= 0 */
1763 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1764 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1765 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1771 * Walk all irg blocks and emit this ILP
1774 luke_blockwalker(ir_node * bb, void * data)
1776 spill_ilp_t *si = (spill_ilp_t*)data;
1781 spill_bb_t *spill_bb = get_irn_link(bb);
1784 pset *defs = pset_new_ptr_default();
1786 live = pset_new_ptr_default();
1788 /****************************************
1789 * B A S I C B L O C K E N D
1790 ***************************************/
1793 /* init live values at end of block */
1794 get_live_end(si, bb, live);
1796 pset_foreach(live, irn) {
1798 ilp_var_t reload = ILP_UNDEF;
1800 spill = set_find_spill(spill_bb->ilp, irn);
1803 if(spill_bb->reloads) {
1804 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1807 reload = PTR_TO_INT(keyval->val);
1811 op = get_irn_link(irn);
1812 assert(!op->is_remat);
1814 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1815 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1816 op->attr.live_range.op = bb;
1818 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1819 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1821 /* reg_out - reload - remat - live_range <= 0 */
1822 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1823 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1824 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1825 foreach_pre_remat(bb, tmp) {
1826 op_t *remat_op = get_irn_link(tmp);
1827 if(remat_op->attr.remat.remat->value == irn) {
1828 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1831 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1832 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1834 /* value may only die at bb end if it is used for a mem copy */
1835 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1836 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1837 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1838 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1839 foreach_pre_remat(bb, tmp) {
1840 op_t *remat_op = get_irn_link(tmp);
1841 if(remat_op->attr.remat.remat->value == irn) {
1842 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1845 if(is_merge_edge(bb)) {
1846 const ir_edge_t *edge = get_block_succ_first(bb);
1847 const ir_node *next_bb = edge->src;
1848 int pos = edge->pos;
1851 sched_foreach(next_bb, phi) {
1852 const ir_node *phi_arg;
1854 if(!is_Phi(phi)) break;
1856 phi_arg = get_irn_n(phi, pos);
1858 if(phi_arg == irn) {
1859 op_t *phi_op = get_irn_link(phi);
1860 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1862 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1869 insert_mem_copy_position(si, live, bb);
1872 * assure the remat args are available
1874 foreach_pre_remat(bb, tmp) {
1875 op_t *remat_op = get_irn_link(tmp);
1878 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1879 ir_node *remat_arg = get_irn_n(tmp, n);
1880 op_t *arg_op = get_irn_link(remat_arg);
1882 if(!has_reg_class(si, remat_arg)) continue;
1884 spill = set_find_spill(spill_bb->ilp, remat_arg);
1887 /* arguments of remats have to be live until the very end of the block
1888 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1889 * no remats, they could be in wrong order
1892 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1893 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1895 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1896 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1897 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1899 /* use reload placed for this argument */
1900 if(spill_bb->reloads) {
1901 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1904 ilp_var_t reload = PTR_TO_INT(keyval->val);
1906 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1911 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1916 /**************************************
1917 * B A S I C B L O C K B O D Y
1918 **************************************/
1920 sched_foreach_reverse_from(sched_block_last_noncf(bb), irn) {
1926 ilp_cst_t check_pre,
1932 ilp_cst_t one_memoperand = -1;
1934 /* iterate only until first phi */
1938 op = get_irn_link(irn);
1940 if(op->is_remat) continue;
1942 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1944 /* collect defined values */
1945 if(has_reg_class(si, irn)) {
1946 pset_insert_ptr(defs, irn);
1950 if(is_Proj(irn)) continue;
1953 * init set of irn's arguments
1954 * and all possibly used values around this op
1955 * and values defined by post remats
1957 args = new_set(cmp_keyval, get_irn_arity(irn));
1958 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1959 remat_defs = pset_new_ptr(pset_count(live));
1961 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1962 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1963 ir_node *irn_arg = get_irn_n(irn, n);
1964 if(has_reg_class(si, irn_arg)) {
1965 set_insert_keyval(args, irn_arg, (void*)n);
1966 pset_insert_ptr(used, irn_arg);
1969 foreach_post_remat(irn, tmp) {
1970 op_t *remat_op = get_irn_link(tmp);
1972 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1974 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1975 ir_node *remat_arg = get_irn_n(tmp, n);
1976 if(has_reg_class(si, remat_arg)) {
1977 pset_insert_ptr(used, remat_arg);
1981 foreach_pre_remat(irn, tmp) {
1982 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1983 ir_node *remat_arg = get_irn_n(tmp, n);
1984 if(has_reg_class(si, remat_arg)) {
1985 pset_insert_ptr(used, remat_arg);
1991 /**********************************
1992 * I N E P I L O G O F irn
1993 **********************************/
1995 /* ensure each dying value is used by only one post remat */
1996 pset_foreach(used, tmp) {
1997 ir_node *value = tmp;
1998 op_t *value_op = get_irn_link(value);
2003 foreach_post_remat(irn, remat) {
2004 op_t *remat_op = get_irn_link(remat);
2006 for(n=get_irn_arity(remat)-1; n>=0; --n) {
2007 ir_node *remat_arg = get_irn_n(remat, n);
2009 /* if value is used by this remat add it to constraint */
2010 if(remat_arg == value) {
2012 /* sum remat2s <= 1 + n_remats*live_range */
2013 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2014 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2018 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2024 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2025 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2029 /* ensure at least one value dies at post remat */
2030 foreach_post_remat(irn, tmp) {
2031 op_t *remat_op = get_irn_link(tmp);
2032 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2035 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2036 remat_arg = get_irn_n(tmp, n);
2038 if(has_reg_class(si, remat_arg)) {
2040 /* does arg always die at this op? */
2041 if(!pset_find_ptr(live, remat_arg))
2042 goto skip_one_must_die;
2044 pset_insert_ptr(remat_args, remat_arg);
2048 /* remat + \sum live_range(remat_arg) <= |args| */
2049 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2050 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2051 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2053 pset_foreach(remat_args, remat_arg) {
2054 op_t *arg_op = get_irn_link(remat_arg);
2056 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2060 del_pset(remat_args);
2063 /* new live ranges for values from L\U defined by post remats */
2064 pset_foreach(live, tmp) {
2065 ir_node *value = tmp;
2066 op_t *value_op = get_irn_link(value);
2068 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2069 ilp_var_t prev_lr = ILP_UNDEF;
2072 if(pset_find_ptr(remat_defs, value)) {
2074 /* next_live_range <= prev_live_range + sum remat2s */
2075 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2076 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2078 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2079 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2081 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2082 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2084 foreach_post_remat(irn, remat) {
2085 op_t *remat_op = get_irn_link(remat);
2087 /* if value is being rematerialized by this remat */
2088 if(value == remat_op->attr.remat.remat->value) {
2089 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2093 value_op->attr.live_range.ilp = prev_lr;
2094 value_op->attr.live_range.op = irn;
2099 /* requirements for post remats and start live ranges from L/U' for values dying here */
2100 foreach_post_remat(irn, tmp) {
2101 op_t *remat_op = get_irn_link(tmp);
2104 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2105 ir_node *remat_arg = get_irn_n(tmp, n);
2106 op_t *arg_op = get_irn_link(remat_arg);
2108 if(!has_reg_class(si, remat_arg)) continue;
2110 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2111 if(!pset_find_ptr(used, remat_arg)) {
2112 /* remat <= live_range(remat_arg) */
2113 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2114 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2116 /* if value is becoming live through use by remat2 */
2117 if(!pset_find_ptr(live, remat_arg)) {
2120 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2121 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2123 arg_op->attr.live_range.ilp = lr;
2124 arg_op->attr.live_range.op = irn;
2126 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2128 pset_insert_ptr(live, remat_arg);
2129 add_to_spill_bb(si, bb, remat_arg);
2132 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2133 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2138 d = pset_count(defs);
2139 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2141 /* count how many regs irn needs for arguments */
2142 u = set_count(args);
2145 /* check the register pressure in the epilog */
2146 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2147 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2148 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2150 /* add L\U' to check_post */
2151 pset_foreach(live, tmp) {
2152 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2153 /* if a live value is not used by irn */
2154 tmp_op = get_irn_link(tmp);
2155 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2159 /***********************************************************
2160 * 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
2161 **********************************************************/
2164 pset_foreach(used, tmp) {
2170 op_t *arg_op = get_irn_link(arg);
2173 spill = add_to_spill_bb(si, bb, arg);
2175 /* new live range for each used value */
2176 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2177 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2179 /* the epilog stuff - including post_use, check_post, check_post_remat */
2180 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2181 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2183 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2185 /* arg is live throughout epilog if the next live_range is in a register */
2186 if(pset_find_ptr(live, arg)) {
2187 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2189 /* post_use >= next_lr + 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, arg_op->attr.live_range.ilp, 1.0);
2196 /* forall post remat which use arg add a similar cst */
2197 foreach_post_remat(irn, remat) {
2200 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2201 ir_node *remat_arg = get_irn_n(remat, n);
2202 op_t *remat_op = get_irn_link(remat);
2204 if(remat_arg == arg) {
2205 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2207 /* post_use >= remat */
2208 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2209 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2210 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2211 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2216 /* if value is not an arg of op and not possibly defined by post remat
2217 * then it may only die and not become live
2219 if(!set_find_keyval(args, arg)) {
2220 /* post_use <= prev_lr */
2221 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2222 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2223 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2224 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2226 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2227 /* next_lr <= prev_lr */
2228 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2229 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2230 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2231 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2235 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2236 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2237 if (get_irn_n(irn, n) == arg &&
2238 arch_possible_memory_operand(irn, n)) {
2239 ilp_var_t memoperand;
2241 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2242 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2243 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2245 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2246 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2248 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2249 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2254 /* new live range begins for each used value */
2255 arg_op->attr.live_range.ilp = prev_lr;
2256 arg_op->attr.live_range.op = irn;
2258 pset_insert_ptr(live, arg);
2261 /* just to be sure */
2262 check_post = ILP_UNDEF;
2264 /* allow original defintions to be removed */
2265 if(opt_repair_schedule) {
2266 pset_foreach(defs, tmp) {
2267 op_t *tmp_op = get_irn_link(tmp);
2268 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2273 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2274 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0 * get_cost(irn) * execution_frequency(si, bb), 0.0);
2276 /* op may not be killed if its first live_range is 1 */
2277 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2278 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2279 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2280 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2282 /* op may not be killed if it is spilled after the definition */
2283 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2284 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2285 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2286 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2291 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2292 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(irn) * execution_frequency(si, bb), 1.0);
2294 /* op may not be killed if its first live_range is 1 */
2295 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2296 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2297 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2298 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2300 /* op may not be killed if it is spilled after the definition */
2301 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2302 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2303 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2304 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2309 pset_foreach(defs, tmp) {
2310 op_t *tmp_op = get_irn_link(tmp);
2311 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2314 /* live_range or spill should be 1
2315 TODO: lr should be live until first use */
2316 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2317 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2318 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2319 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2329 /* check the register pressure in the prolog */
2330 /* sum_{L\U} lr <= k - |U| */
2331 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2332 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2334 /* for the prolog remove defined values from the live set */
2335 pset_foreach(defs, tmp) {
2336 pset_remove_ptr(live, tmp);
2339 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2340 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2341 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2344 /***********************************************************
2345 * 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
2346 **********************************************************/
2349 set_foreach(args, keyval) {
2351 const ir_node *arg = keyval->key;
2352 int i = PTR_TO_INT(keyval->val);
2353 op_t *arg_op = get_irn_link(arg);
2354 ilp_cst_t requirements;
2357 spill = set_find_spill(spill_bb->ilp, arg);
2360 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2361 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);
2363 /* reload <= mem_out */
2364 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2365 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2366 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2367 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2369 /* requirement: arg must be in register for use */
2370 /* reload + remat + live_range == 1 */
2371 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2372 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2374 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2375 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2376 foreach_pre_remat(irn, tmp) {
2377 op_t *remat_op = get_irn_link(tmp);
2378 if(remat_op->attr.remat.remat->value == arg) {
2379 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2383 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2385 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2386 if(get_irn_n(irn, n) == arg) {
2390 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2391 if (get_irn_n(irn, n) == arg &&
2392 arch_possible_memory_operand(irn, n)) {
2393 memoperand_t *memoperand;
2394 memoperand = set_find_memoperand(si->memoperands, irn, n);
2396 /* memoperand <= mem_out */
2397 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2398 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2399 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2400 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2402 /* the memoperand is only sufficient if it is used once by the op */
2403 if(n_memoperands == 1)
2404 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2406 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2408 /* we have one more free register if we use a memory operand */
2409 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2415 /* iterate over L\U */
2416 pset_foreach(live, tmp) {
2417 if(!set_find_keyval(args, tmp)) {
2418 /* if a live value is not used by irn */
2419 tmp_op = get_irn_link(tmp);
2420 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2424 /* requirements for remats */
2425 foreach_pre_remat(irn, tmp) {
2426 op_t *remat_op = get_irn_link(tmp);
2429 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2430 ir_node *remat_arg = get_irn_n(tmp, n);
2431 op_t *arg_op = get_irn_link(remat_arg);
2433 if(!has_reg_class(si, remat_arg)) continue;
2435 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2436 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2437 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2439 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2440 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2442 /* if remat arg is also used by current op then we can use reload placed for this argument */
2443 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2444 int index = (int)keyval->val;
2446 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2454 /*************************
2455 * D O N E W I T H O P
2456 *************************/
2458 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2460 pset_foreach(live, tmp) {
2461 assert(has_reg_class(si, tmp));
2465 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2466 ir_node *arg = get_irn_n(irn, n);
2468 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2472 del_pset(remat_defs);
2476 defs = pset_new_ptr_default();
2478 /* skip everything above barrier in start block */
2479 if(is_start_block(bb) && be_is_Barrier(irn)) {
2480 assert(pset_count(live) == 0);
2489 /***************************************
2490 * B E G I N N I N G O F B L O C K
2491 ***************************************/
2494 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2495 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2497 pset_foreach(live, irn) {
2498 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2501 /* construct mem_outs for all values */
2502 set_foreach(spill_bb->ilp, spill) {
2503 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2504 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2506 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2507 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2509 if(pset_find_ptr(live, spill->irn)) {
2510 int default_spilled;
2511 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2513 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2514 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2515 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2516 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2518 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2520 op_t *op = get_irn_link(spill->irn);
2522 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2523 const ir_node *arg = get_irn_n(spill->irn, n);
2529 /* argument already done? */
2530 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2532 /* get sum of execution frequencies of blocks with the same phi argument */
2533 for(m=n; m>=0; --m) {
2534 const ir_node *arg2 = get_irn_n(spill->irn, m);
2537 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2541 /* copies are not for free */
2542 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2543 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2545 for(m=n; m>=0; --m) {
2546 const ir_node *arg2 = get_irn_n(spill->irn, m);
2549 op->attr.live_range.args.copies[m] = var;
2554 /* copy <= mem_in */
2555 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2556 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2557 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2558 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2565 foreach_post_remat(bb, tmp) {
2567 op_t *remat_op = get_irn_link(tmp);
2568 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2571 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2572 remat_arg = get_irn_n(tmp, n);
2574 if(has_reg_class(si, remat_arg)) {
2575 pset_insert_ptr(remat_args, remat_arg);
2579 /* remat + \sum live_range(remat_arg) <= |args| */
2580 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2581 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2582 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2584 pset_foreach(remat_args, remat_arg) {
2585 if(pset_find_ptr(live, remat_arg)) {
2586 op_t *remat_arg_op = get_irn_link(remat_arg);
2587 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2590 del_pset(remat_args);
2593 foreach_post_remat(bb, tmp) {
2596 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2597 ir_node *remat_arg = get_irn_n(tmp, n);
2599 /* if value is becoming live through use by remat2 */
2600 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2601 op_t *remat_arg_op = get_irn_link(remat_arg);
2604 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2606 pset_insert_ptr(live, remat_arg);
2607 spill = add_to_spill_bb(si, bb, remat_arg);
2608 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2610 /* we need reg_in and mem_in for this value; they will be referenced later */
2611 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2612 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2613 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2614 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2617 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2618 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2619 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2620 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2625 /* L\U is empty at bb start */
2626 /* arg is live throughout epilog if it is reg_in into this block */
2628 /* check the register pressure at the beginning of the block
2631 /* reg_in entspricht post_use */
2633 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2634 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2636 pset_foreach(live, irn) {
2639 spill = set_find_spill(spill_bb->ilp, irn);
2642 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2643 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2645 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2647 /* spill + mem_in <= 1 */
2648 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2649 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2651 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2652 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2654 } /* post_remats are NOT included in register pressure check because
2655 they do not increase regpressure */
2657 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2658 pset_foreach(live, irn) {
2662 spill = set_find_spill(spill_bb->ilp, irn);
2663 assert(spill && spill->irn == irn);
2665 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2666 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2669 ir_node *phi_arg = get_Phi_pred(irn, n);
2670 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2671 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2673 op_t *op = get_irn_link(irn);
2675 /* although the phi is in the right regclass one or more of
2676 * its arguments can be in a different one or at least to
2679 if(has_reg_class(si, phi_arg)) {
2680 /* mem_in < mem_out_arg + copy */
2681 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2682 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2684 /* reg_in < reg_out_arg */
2685 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2686 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2688 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2689 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2691 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2694 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2696 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2698 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2702 /* else assure the value arrives on all paths in the same resource */
2704 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2707 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2708 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2711 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2712 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2713 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2714 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2716 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2717 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2719 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2722 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2723 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2728 foreach_post_remat(bb, tmp) {
2731 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2732 ir_node *remat_arg = get_irn_n(tmp, n);
2733 op_t *remat_op = get_irn_link(tmp);
2735 if(!has_reg_class(si, remat_arg)) continue;
2737 spill = set_find_spill(spill_bb->ilp, remat_arg);
2740 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2741 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2742 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2743 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2747 pset_foreach(live, irn) {
2748 const op_t *op = get_irn_link(irn);
2749 const ir_node *remat;
2754 foreach_post_remat(bb, remat) {
2757 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2758 const ir_node *arg = get_irn_n(remat, n);
2761 const op_t *remat_op = get_irn_link(remat);
2763 if(cst == ILP_UNDEF) {
2764 /* sum remat2s <= 1 + n_remats*live_range */
2765 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2766 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2768 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2774 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2775 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2779 /* first live ranges from reg_ins */
2780 pset_foreach(live, irn) {
2781 op_t *op = get_irn_link(irn);
2783 if(op->attr.live_range.ilp != ILP_UNDEF) {
2785 spill = set_find_spill(spill_bb->ilp, irn);
2786 assert(spill && spill->irn == irn);
2788 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2789 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2790 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2791 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2793 foreach_post_remat(bb, tmp) {
2794 op_t *remat_op = get_irn_link(tmp);
2796 if(remat_op->attr.remat.remat->value == irn) {
2797 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2803 /* walk forward now and compute constraints for placing spills */
2804 /* this must only be done for values that are not defined in this block */
2805 pset_foreach(live, irn) {
2807 * if value is defined in this block we can anways place the spill directly after the def
2808 * -> no constraint necessary
2810 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2815 spill = set_find_spill(spill_bb->ilp, irn);
2818 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2819 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2821 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2822 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2825 sched_foreach_op(bb, tmp) {
2826 op_t *op = get_irn_link(tmp);
2828 if(is_Phi(tmp)) continue;
2829 assert(!is_Proj(tmp));
2832 const ir_node *value = op->attr.remat.remat->value;
2835 /* only collect remats up to the first real use of a value */
2836 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2841 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2842 ir_node *arg = get_irn_n(tmp, n);
2845 /* if a value is used stop collecting remats */
2858 typedef struct _irnlist_t {
2859 struct list_head list;
2863 typedef struct _interference_t {
2864 struct list_head blocklist;
2870 cmp_interference(const void *a, const void *b, size_t size)
2872 const interference_t *p = a;
2873 const interference_t *q = b;
2876 return !(p->a == q->a && p->b == q->b);
2879 static interference_t *
2880 set_find_interference(set * set, ir_node * a, ir_node * b)
2882 interference_t query;
2884 query.a = (a>b)?a:b;
2885 query.b = (a>b)?b:a;
2887 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2890 static interference_t *
2891 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2893 interference_t query,
2895 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2899 result = set_find_interference(set, a, b);
2902 list_add(&list->list, &result->blocklist);
2906 query.a = (a>b)?a:b;
2907 query.b = (a>b)?b:a;
2909 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2911 INIT_LIST_HEAD(&result->blocklist);
2912 list_add(&list->list, &result->blocklist);
2918 int values_interfere_in_block(const spill_ilp_t *si, const ir_node *bb, const ir_node *a, const ir_node *b)
2920 const ir_edge_t *edge;
2922 if (get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2923 /* both values are live in, so they interfere */
2927 /* ensure a dominates b */
2928 if (value_dominates(b, a)) {
2934 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2937 /* the following code is stolen from bera.c */
2938 if (be_is_live_end(si->lv, bb, a))
2941 foreach_out_edge(a, edge) {
2942 const ir_node *user = edge->src;
2943 if (get_nodes_block(user) == bb
2946 && ! pset_find_ptr(si->inverse_ops, user)
2947 && value_dominates(b, user))
2955 * Walk all irg blocks and collect interfering values inside of phi classes
2958 luke_interferencewalker(ir_node * bb, void * data)
2960 spill_ilp_t *si = (spill_ilp_t*)data;
2963 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2964 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2965 op_t *a_op = get_irn_link(a);
2968 /* a is only interesting if it is in my register class and if it is inside a phi class */
2969 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
2970 if (a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2973 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)) {
2974 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2975 op_t *b_op = get_irn_link(b);
2977 /* a and b are only interesting if they are in the same phi class */
2978 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
2979 if (b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2982 if (values_interfere_in_block(si, bb, a, b)) {
2983 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2984 set_insert_interference(si, si->interferences, a, b, bb);
2992 static unsigned int copy_path_id = 0;
2995 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
3002 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
3003 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3005 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
3007 pset_foreach(copies, ptr) {
3008 copy = PTR_TO_INT(ptr);
3009 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
3014 * @parameter copies contains a path of copies which lead us to irn
3015 * @parameter visited contains a set of nodes already visited on this path
3018 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3020 const ir_edge_t *edge;
3021 op_t *op = get_irn_link(irn);
3022 pset *visited_users = pset_new_ptr_default();
3025 if(op->is_remat) return 0;
3027 pset_insert_ptr(visited, irn);
3031 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3033 /* visit all operands */
3034 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3035 ir_node *arg = get_irn_n(irn, n);
3036 ilp_var_t copy = op->attr.live_range.args.copies[n];
3038 if(!has_reg_class(si, arg)) continue;
3039 if(pset_find_ptr(visited_operands, arg)) continue;
3040 pset_insert_ptr(visited_operands, arg);
3043 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3044 del_pset(visited_operands);
3045 del_pset(visited_users);
3046 pset_remove_ptr(visited, irn);
3049 pset_insert(copies, INT_TO_PTR(copy), copy);
3050 write_copy_path_cst(si, copies, any_interfere);
3051 pset_remove(copies, INT_TO_PTR(copy), copy);
3052 } else if(!pset_find_ptr(visited, arg)) {
3053 pset_insert(copies, INT_TO_PTR(copy), copy);
3054 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3055 pset_remove(copies, INT_TO_PTR(copy), copy);
3057 if(paths > MAX_PATHS) {
3058 if(pset_count(copies) == 0) {
3062 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3063 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3064 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3065 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3066 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3070 del_pset(visited_operands);
3071 del_pset(visited_users);
3072 pset_remove_ptr(visited, irn);
3075 } else if(pset_count(copies) == 0) {
3081 del_pset(visited_operands);
3084 /* visit all uses which are phis */
3085 foreach_out_edge(irn, edge) {
3086 ir_node *user = edge->src;
3087 int pos = edge->pos;
3088 op_t *op = get_irn_link(user);
3091 if(!is_Phi(user)) continue;
3092 if(!has_reg_class(si, user)) continue;
3093 if(pset_find_ptr(visited_users, user)) continue;
3094 pset_insert_ptr(visited_users, user);
3096 copy = op->attr.live_range.args.copies[pos];
3098 if(user == target) {
3099 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3100 del_pset(visited_users);
3101 pset_remove_ptr(visited, irn);
3104 pset_insert(copies, INT_TO_PTR(copy), copy);
3105 write_copy_path_cst(si, copies, any_interfere);
3106 pset_remove(copies, INT_TO_PTR(copy), copy);
3107 } else if(!pset_find_ptr(visited, user)) {
3108 pset_insert(copies, INT_TO_PTR(copy), copy);
3109 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3110 pset_remove(copies, INT_TO_PTR(copy), copy);
3112 if(paths > MAX_PATHS) {
3113 if(pset_count(copies) == 0) {
3117 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3118 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3119 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3120 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3121 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3125 del_pset(visited_users);
3126 pset_remove_ptr(visited, irn);
3129 } else if(pset_count(copies) == 0) {
3135 del_pset(visited_users);
3136 pset_remove_ptr(visited, irn);
3141 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3143 pset * copies = pset_new_ptr_default();
3144 pset * visited = pset_new_ptr_default();
3146 find_copy_path(si, a, b, any_interfere, copies, visited);
3154 memcopyhandler(spill_ilp_t * si)
3156 interference_t *interference;
3158 /* teste Speicherwerte auf Interferenz */
3160 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3161 irg_block_walk_graph(si->birg->irg, luke_interferencewalker, NULL, si);
3163 /* now lets emit the ILP unequations for the crap */
3164 set_foreach(si->interferences, interference) {
3166 ilp_var_t interfere, any_interfere;
3167 ilp_cst_t any_interfere_cst, cst;
3168 const ir_node *a = interference->a;
3169 const ir_node *b = interference->b;
3171 /* any_interf <= \sum interf */
3172 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3173 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3174 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3176 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3178 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3179 const ir_node *bb = irnlist->irn;
3180 spill_bb_t *spill_bb = get_irn_link(bb);
3185 spilla = set_find_spill(spill_bb->ilp, a);
3188 spillb = set_find_spill(spill_bb->ilp, b);
3191 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3192 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3193 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3194 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3195 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3196 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3198 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3199 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3201 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3202 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3203 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3204 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3205 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3207 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3208 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3210 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3211 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3212 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3214 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3215 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3217 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3218 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3219 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3222 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3224 /* any_interfere >= interf */
3225 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3226 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3228 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3229 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3232 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3233 gen_copy_constraints(si,a,b,any_interfere);
3241 return fabs(x) < 0.00001;
3245 * node attribute hook for changing colors
3247 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3249 spill_ilp_t *si = get_irg_link(current_ir_graph);
3252 if(pset_find_ptr(si->all_possible_remats, n)) {
3253 op_t *op = (op_t*)get_irn_link(n);
3254 assert(op && op->is_remat);
3256 if(!op->attr.remat.remat->inverse) {
3257 if(op->attr.remat.pre) {
3258 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3260 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3265 op_t *op = (op_t*)get_irn_link(n);
3266 assert(op && op->is_remat);
3268 if(op->attr.remat.pre) {
3269 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3271 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3282 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3284 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3285 be_dump(irg, suffix, dump_ir_block_graph_sched);
3286 set_dump_node_vcgattr_hook(NULL);
3290 * Edge hook to dump the schedule edges with annotated register pressure.
3293 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3295 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3296 ir_node *prev = sched_prev(irn);
3297 fprintf(F, "edge:{sourcename:\"");
3299 fprintf(F, "\" targetname:\"");
3301 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3302 fprintf(F, "\" color:magenta}\n");
3308 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3310 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3312 dump_consts_local(0);
3313 set_dump_node_edge_hook(sched_pressure_edge_hook);
3314 dump_ir_block_graph(irg, suffix);
3315 set_dump_node_edge_hook(old_edge_hook);
3319 walker_pressure_annotator(ir_node * bb, void * data)
3321 spill_ilp_t *si = data;
3324 pset *live = pset_new_ptr_default();
3327 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3328 irn = be_lv_get_irn(si->lv, bb, i);
3330 if (has_reg_class(si, irn)) {
3331 pset_insert_ptr(live, irn);
3335 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3337 sched_foreach_reverse(bb, irn) {
3339 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3343 if(has_reg_class(si, irn)) {
3344 pset_remove_ptr(live, irn);
3345 if(is_Proj(irn)) ++projs;
3348 if(!is_Proj(irn)) projs = 0;
3350 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3351 ir_node *arg = get_irn_n(irn, n);
3353 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3355 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3362 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3364 be_dump(si->birg->irg, suffix, dump_ir_block_graph_sched_pressure);
3368 connect_all_remats_with_keep(spill_ilp_t * si)
3376 n_remats = pset_count(si->all_possible_remats);
3378 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3381 pset_foreach(si->all_possible_remats, irn) {
3386 si->keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_remats, ins);
3388 obstack_free(si->obst, ins);
3393 connect_all_spills_with_keep(spill_ilp_t * si)
3402 n_spills = pset_count(si->spills);
3404 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3407 pset_foreach(si->spills, irn) {
3412 keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_spills, ins);
3414 obstack_free(si->obst, ins);
3418 /** insert a spill at an arbitrary position */
3419 static ir_node *be_spill2(ir_node *irn, ir_node *insert)
3421 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
3422 ir_graph *irg = get_irn_irg(bl);
3423 ir_node *frame = get_irg_frame(irg);
3426 const arch_register_class_t *cls = arch_get_irn_reg_class(irn, -1);
3427 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame, -1);
3429 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
3432 * search the right insertion point. a spill of a phi cannot be put
3433 * directly after the phi, if there are some phis behind the one which
3434 * is spilled. Also, a spill of a Proj must be after all Projs of the
3437 * Here's one special case:
3438 * If the spill is in the start block, the spill must be after the frame
3439 * pointer is set up. This is done by setting insert to the end of the block
3440 * which is its default initialization (see above).
3443 if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3446 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3449 sched_add_after(insert, spill);
3454 delete_remat(spill_ilp_t * si, ir_node * remat) {
3456 ir_node *bad = get_irg_bad(si->birg->irg);
3458 sched_remove(remat);
3460 /* kill links to operands */
3461 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3462 set_irn_n(remat, n, bad);
3467 clean_remat_info(spill_ilp_t * si)
3471 remat_info_t *remat_info;
3472 ir_node *bad = get_irg_bad(si->birg->irg);
3474 set_foreach(si->remat_info, remat_info) {
3475 if(!remat_info->remats) continue;
3477 pset_foreach(remat_info->remats, remat)
3479 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3480 if(sched_is_scheduled(remat->proj)) {
3481 sched_remove((ir_node*)remat->proj);
3483 set_irn_n((ir_node*)remat->proj, -1, bad);
3484 set_irn_n((ir_node*)remat->proj, 0, bad);
3487 if(get_irn_n_edges(remat->op) == 0) {
3488 if(sched_is_scheduled(remat->op)) {
3489 sched_remove((ir_node*)remat->op);
3491 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3492 set_irn_n((ir_node*)remat->op, n, bad);
3497 if(remat_info->remats) del_pset(remat_info->remats);
3498 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3503 delete_unnecessary_remats(spill_ilp_t * si)
3505 if(opt_keep_alive & KEEPALIVE_REMATS) {
3507 ir_node *bad = get_irg_bad(si->birg->irg);
3510 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3511 ir_node *keep_arg = get_irn_n(si->keep, n);
3512 op_t *arg_op = get_irn_link(keep_arg);
3515 assert(arg_op->is_remat);
3517 name = si->lpp->vars[arg_op->attr.remat.ilp];
3519 if(is_zero(name->value)) {
3520 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3521 /* TODO check whether reload is preferred over remat (could be bug) */
3522 delete_remat(si, keep_arg);
3524 if(!arg_op->attr.remat.remat->inverse) {
3525 if(arg_op->attr.remat.pre) {
3526 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3528 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3531 if(arg_op->attr.remat.pre) {
3532 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3534 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3539 set_irn_n(si->keep, n, bad);
3542 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3547 pset_foreach(si->all_possible_remats, remat) {
3548 op_t *remat_op = get_irn_link(remat);
3549 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3551 if(is_zero(name->value)) {
3552 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3553 /* TODO check whether reload is preferred over remat (could be bug) */
3554 delete_remat(si, remat);
3556 if(!remat_op->attr.remat.remat->inverse) {
3557 if(remat_op->attr.remat.pre) {
3558 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3560 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3563 if(remat_op->attr.remat.pre) {
3564 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3566 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3575 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3577 pset *spills = pset_new_ptr_default();
3579 const ir_node *next;
3582 defs = set_find_def(si->values, value);
3584 if(defs && defs->spills) {
3585 for(next = defs->spills; next; next = get_irn_link(next)) {
3586 pset_insert_ptr(spills, next);
3594 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3598 assert( get_irn_arity(block) == arity );
3600 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3601 res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
3607 * @param before The node after which the spill will be placed in the schedule
3610 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3615 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3617 spill = be_spill2(irn, before);
3619 defs = set_insert_def(si->values, value);
3622 /* enter into the linked list */
3623 set_irn_link(spill, defs->spills);
3624 defs->spills = spill;
3626 if(opt_keep_alive & KEEPALIVE_SPILLS)
3627 pset_insert_ptr(si->spills, spill);
3633 * @param before The Phi node which has to be spilled
3636 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3643 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3645 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3646 ins[n] = si->m_unknown;
3649 mem_phi = new_r_PhiM_nokeep(si->birg->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3651 defs = set_insert_def(si->values, phi);
3654 /* enter into the linked list */
3655 set_irn_link(mem_phi, defs->spills);
3656 defs->spills = mem_phi;
3658 #ifdef SCHEDULE_PHIM
3659 sched_add_after(phi, mem_phi);
3661 pset_insert_ptr(si->phims, mem_phi);
3664 if(opt_keep_alive & KEEPALIVE_SPILLS)
3665 pset_insert_ptr(si->spills, mem_phi);
3672 * Add remat to list of defs, destroys link field!
3675 insert_remat(spill_ilp_t * si, ir_node * remat)
3678 op_t *remat_op = get_irn_link(remat);
3680 assert(remat_op->is_remat);
3682 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3685 /* enter into the linked list */
3686 set_irn_link(remat, defs->remats);
3687 defs->remats = remat;
3692 * Add reload before operation and add to list of defs
3695 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3701 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3703 defs = set_find_def(si->values, value);
3705 spill = defs->spills;
3706 assert(spill && "no spill placed before reload");
3708 reload = be_reload(si->cls, after, get_irn_mode(value), spill);
3710 /* enter into the linked list */
3711 set_irn_link(reload, defs->remats);
3712 defs->remats = reload;
3717 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3720 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3723 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3725 defs = set_find_def(si->values, value);
3727 spill = defs->spills;
3728 assert(spill && "no spill placed before reload");
3730 arch_perform_memory_operand(memoperand->irn, spill, memoperand->pos);
3733 void insert_memoperands(spill_ilp_t * si)
3735 memoperand_t *memoperand;
3738 set_foreach(si->memoperands, memoperand) {
3739 name = si->lpp->vars[memoperand->ilp];
3740 if(!is_zero(name->value)) {
3741 perform_memory_operand(si, memoperand);
3747 walker_spill_placer(ir_node * bb, void * data) {
3748 spill_ilp_t *si = (spill_ilp_t*)data;
3750 spill_bb_t *spill_bb = get_irn_link(bb);
3751 pset *spills_to_do = pset_new_ptr_default();
3754 set_foreach(spill_bb->ilp, spill) {
3757 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3758 name = si->lpp->vars[spill->mem_in];
3759 if(!is_zero(name->value)) {
3762 mem_phi = insert_mem_phi(si, spill->irn);
3764 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3768 name = si->lpp->vars[spill->spill];
3769 if(!is_zero(name->value)) {
3770 /* place spill directly after definition */
3771 if(get_nodes_block(spill->irn) == bb) {
3772 insert_spill(si, spill->irn, spill->irn, spill->irn);
3776 /* place spill at bb start */
3777 if(spill->reg_in > 0) {
3778 name = si->lpp->vars[spill->reg_in];
3779 if(!is_zero(name->value)) {
3780 insert_spill(si, spill->irn, spill->irn, bb);
3784 /* place spill after a remat */
3785 pset_insert_ptr(spills_to_do, spill->irn);
3788 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3791 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3792 op_t *op = get_irn_link(irn);
3794 if(be_is_Spill(irn)) continue;
3797 /* TODO fix this if we want to support remats with more than two nodes */
3798 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3799 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3801 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3804 if(pset_find_ptr(spills_to_do, irn)) {
3805 pset_remove_ptr(spills_to_do, irn);
3807 insert_spill(si, irn, irn, irn);
3813 assert(pset_count(spills_to_do) == 0);
3815 /* afterwards free data in block */
3816 del_pset(spills_to_do);
3820 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3822 ir_node *insert_pos = bb;
3825 /* find last definition of arg value in block */
3830 defs = set_find_def(si->values, value);
3832 if(defs && defs->remats) {
3833 for(next = defs->remats; next; next = get_irn_link(next)) {
3834 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3835 last = sched_get_time_step(next);
3841 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3842 last = sched_get_time_step(value);
3846 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3848 spill = be_spill2(is_Block(insert_pos)?value:insert_pos, insert_pos);
3854 phim_fixer(spill_ilp_t *si) {
3857 set_foreach(si->values, defs) {
3858 const ir_node *phi = defs->value;
3859 op_t *op = get_irn_link(phi);
3860 ir_node *phi_m = NULL;
3861 ir_node *next = defs->spills;
3864 if(!is_Phi(phi)) continue;
3867 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3871 next = get_irn_link(next);
3874 if(!phi_m) continue;
3876 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3877 ir_node *value = get_irn_n(phi, n);
3878 defs_t *val_defs = set_find_def(si->values, value);
3880 /* a spill of this value */
3885 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3886 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3888 if(!is_zero(name->value)) {
3889 spill = insert_mem_copy(si, pred, value);
3891 spill = val_defs->spills;
3894 spill = val_defs->spills;
3897 assert(spill && "no spill placed before PhiM");
3898 set_irn_n(phi_m, n, spill);
3904 walker_reload_placer(ir_node * bb, void * data) {
3905 spill_ilp_t *si = (spill_ilp_t*)data;
3907 spill_bb_t *spill_bb = get_irn_link(bb);
3909 /* reloads at end of block */
3910 if(spill_bb->reloads) {
3913 set_foreach(spill_bb->reloads, keyval) {
3914 ir_node *irn = (ir_node*)keyval->key;
3915 ilp_var_t reload = PTR_TO_INT(keyval->val);
3918 name = si->lpp->vars[reload];
3919 if(!is_zero(name->value)) {
3921 ir_node *insert_pos = bb;
3922 ir_node *prev = sched_block_last_noncf(bb);
3923 op_t *prev_op = get_irn_link(prev);
3925 while(be_is_Spill(prev)) {
3926 prev = sched_prev(prev);
3929 prev_op = get_irn_link(prev);
3931 /* insert reload before pre-remats */
3932 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3933 && prev_op->is_remat && prev_op->attr.remat.pre) {
3937 prev = sched_prev(prev);
3938 } while(be_is_Spill(prev));
3940 prev_op = get_irn_link(prev);
3944 reload = insert_reload(si, irn, insert_pos);
3946 if(opt_keep_alive & KEEPALIVE_RELOADS)
3947 pset_insert_ptr(si->spills, reload);
3952 /* walk and insert more reloads and collect remats */
3953 sched_foreach_reverse(bb, irn) {
3954 op_t *op = get_irn_link(irn);
3956 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3957 if(is_Phi(irn)) break;
3960 if(get_irn_mode(irn) != mode_T) {
3961 insert_remat(si, irn);
3966 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3967 ir_node *arg = get_irn_n(irn, n);
3969 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3972 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3973 if(!is_zero(name->value)) {
3975 ir_node *insert_pos = irn;
3976 ir_node *prev = sched_prev(insert_pos);
3979 while(be_is_Spill(prev)) {
3980 prev = sched_prev(prev);
3983 prev_op = get_irn_link(prev);
3985 /* insert reload before pre-remats */
3986 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3987 && prev_op->is_remat && prev_op->attr.remat.pre) {
3991 prev = sched_prev(prev);
3992 } while(be_is_Spill(prev));
3994 prev_op = get_irn_link(prev);
3998 reload = insert_reload(si, arg, insert_pos);
4000 assert(reload && "no reload returned");
4001 set_irn_n(irn, n, reload);
4003 if(opt_keep_alive & KEEPALIVE_RELOADS)
4004 pset_insert_ptr(si->spills, reload);
4011 del_set(spill_bb->ilp);
4012 if(spill_bb->reloads) del_set(spill_bb->reloads);
4016 walker_collect_used(ir_node * irn, void * data)
4018 bitset_t *used = data;
4020 bitset_set(used, get_irn_idx(irn));
4023 struct kill_helper {
4029 walker_kill_unused(ir_node * bb, void * data)
4031 struct kill_helper *kh = data;
4032 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4036 for(irn=sched_first(bb); !sched_is_end(irn);) {
4037 ir_node *next = sched_next(irn);
4040 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4041 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4042 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(irn) * execution_frequency(kh->si, bb)));
4044 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4050 set_nodes_block(irn, bad);
4051 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4052 set_irn_n(irn, n, bad);
4059 #ifndef SCHEDULE_PHIM
4061 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4064 ir_node *bad = get_irg_bad(si->birg->irg);
4067 pset_foreach(si->phims, phi) {
4068 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4070 set_nodes_block(phi, bad);
4071 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4072 set_irn_n(phi, n, bad);
4080 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4082 struct kill_helper kh;
4084 kh.used = bitset_malloc(get_irg_last_idx(si->birg->irg));
4087 irg_walk_graph(si->birg->irg, walker_collect_used, NULL, kh.used);
4088 #ifndef SCHEDULE_PHIM
4089 kill_unused_phims(si, &kh);
4091 irg_block_walk_graph(si->birg->irg, walker_kill_unused, NULL, &kh);
4093 bitset_free(kh.used);
4097 print_irn_pset(pset * p)
4101 pset_foreach(p, irn) {
4102 ir_printf("%+F\n", irn);
4107 dump_phi_class(spill_ilp_t *si, ir_node **phiclass, const char * file)
4109 FILE *f = fopen(file, "w");
4111 interference_t *interference;
4114 set_break(si->interferences);
4116 ir_fprintf(f, "digraph phiclass {\n");
4118 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4121 ir_fprintf(f, " %F%N [shape=box]\n", irn, irn);
4124 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4131 for (n = get_irn_arity(irn) - 1; n >= 0; --n) {
4132 ir_node *arg = get_irn_n(irn, n);
4134 ir_fprintf(f, " %F%N -> %F%N\n", irn, irn, arg, arg);
4138 set_foreach(si->interferences, interference) {
4139 const ir_node *a = interference->a;
4140 const ir_node *b = interference->b;
4141 if (get_phi_class(si->pc, (ir_node *)a) == phiclass) {
4142 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n", a, a, b, b);
4151 rewire_uses(spill_ilp_t * si)
4154 ir_nodeset_t ignore;
4156 ir_nodeset_init(&ignore);
4157 ir_nodeset_insert(&ignore, get_irg_end(si->birg->irg));
4159 /* then fix uses of spills */
4160 set_foreach(si->values, defs) {
4163 const ir_node *next = defs->remats;
4166 reloads = pset_new_ptr_default();
4169 if(be_is_Reload(next)) {
4170 pset_insert_ptr(reloads, next);
4174 next = get_irn_link(next);
4177 spills = get_spills_for_value(si, defs->value);
4178 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));
4179 if(pset_count(spills) > 1) {
4180 be_ssa_construction_env_t senv;
4182 //assert(pset_count(reloads) > 0);
4183 // print_irn_pset(spills);
4184 // print_irn_pset(reloads);
4186 be_ssa_construction_init(&senv, si->birg);
4187 be_ssa_construction_set_ignore_uses(&senv, &ignore);
4188 pset_foreach(spills, node) {
4189 be_ssa_construction_add_copy(&senv, node);
4191 pset_foreach(spills, node) {
4192 be_ssa_construction_fix_users(&senv, node);
4194 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4195 pset_foreach(spills, node) {
4196 be_liveness_update(si->lv, node);
4198 be_ssa_construction_destroy(&senv);
4205 /* first fix uses of remats and reloads */
4206 set_foreach(si->values, defs) {
4207 const ir_node *next = defs->remats;
4211 be_ssa_construction_env_t senv;
4213 be_ssa_construction_init(&senv, si->birg);
4215 if(sched_is_scheduled(defs->value)) {
4216 be_ssa_construction_add_copy(&senv, (ir_node*) defs->value);
4220 next = defs->remats;
4222 be_ssa_construction_add_copy(&senv, (ir_node*) next);
4223 next = get_irn_link(next);
4226 if(sched_is_scheduled(defs->value)) {
4227 be_ssa_construction_fix_users(&senv, (ir_node*) defs->value);
4230 next = defs->remats;
4232 be_ssa_construction_fix_users(&senv, (ir_node*) next);
4233 next = get_irn_link(next);
4236 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4237 if(sched_is_scheduled(defs->value)) {
4238 be_liveness_update(si->lv, (ir_node*) defs->value);
4241 next = defs->remats;
4243 be_liveness_update(si->lv, (ir_node*) next);
4244 next = get_irn_link(next);
4247 be_ssa_construction_destroy(&senv);
4251 ir_nodeset_destroy(&ignore);
4252 // remove_unused_defs(si);
4257 writeback_results(spill_ilp_t * si)
4259 /* walk through the graph and collect all spills, reloads and remats for a value */
4261 si->values = new_set(cmp_defs, 4096);
4263 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4264 delete_unnecessary_remats(si);
4265 si->m_unknown = new_r_Unknown(si->birg->irg, mode_M);
4266 irg_block_walk_graph(si->birg->irg, walker_spill_placer, NULL, si);
4267 irg_block_walk_graph(si->birg->irg, walker_reload_placer, NULL, si);
4269 insert_memoperands(si);
4272 /* clean the remat info! there are still back-edges leading there! */
4273 clean_remat_info(si);
4277 connect_all_spills_with_keep(si);
4279 del_set(si->values);
4283 get_n_regs(spill_ilp_t * si)
4285 int arch_n_regs = arch_register_class_n_regs(si->cls);
4287 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4288 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4290 arch_put_non_ignore_regs(si->cls, arch_regs);
4291 be_abi_put_ignore_regs(si->birg->abi, si->cls, abi_regs);
4293 bitset_andnot(arch_regs, abi_regs);
4294 arch_n_regs = bitset_popcnt(arch_regs);
4296 bitset_free(arch_regs);
4297 bitset_free(abi_regs);
4299 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4304 walker_reload_mover(ir_node * bb, void * data)
4306 spill_ilp_t *si = data;
4309 sched_foreach(bb, tmp) {
4310 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4311 ir_node *reload = tmp;
4314 /* move reload upwards */
4316 int pressure = (int)get_irn_link(reload);
4317 if(pressure < si->n_regs) {
4318 irn = sched_prev(reload);
4319 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4320 sched_remove(reload);
4321 pressure = (int)get_irn_link(irn);
4323 while(pressure < si->n_regs) {
4324 if( sched_is_end(irn) ||
4325 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4326 /* do not move reload before its spill */
4327 (irn == be_get_Reload_mem(reload)) ||
4328 /* do not move before phi */
4331 set_irn_link(irn, INT_TO_PTR(pressure+1));
4332 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4333 irn = sched_prev(irn);
4335 pressure = (int)get_irn_link(irn);
4338 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4339 sched_put_after(irn, reload);
4346 move_reloads_upward(spill_ilp_t * si)
4348 irg_block_walk_graph(si->birg->irg, walker_reload_mover, NULL, si);
4353 * Walk all irg blocks and check for interfering spills inside of phi classes
4356 luke_meminterferencechecker(ir_node * bb, void * data)
4358 spill_ilp_t *si = (spill_ilp_t*)data;
4361 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4362 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4364 if (! be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T))
4367 /* a is only interesting if it is in my register class and if it is inside a phi class */
4368 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
4369 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)) {
4370 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4372 if (! be_is_Spill(b) && (! is_Phi(b) || get_irn_mode(b) != mode_T))
4375 /* a and b are only interesting if they are in the same phi class */
4376 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
4377 if (values_interfere_in_block(si, bb, a, b)) {
4378 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4387 verify_phiclasses(spill_ilp_t * si)
4389 /* analyze phi classes */
4390 phi_class_free(si->pc);
4391 si->pc = phi_class_new_from_irg(si->birg->irg, 0);
4393 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4394 irg_block_walk_graph(si->birg->irg, luke_meminterferencechecker, NULL, si);
4398 be_spill_remat(be_irg_t *birg, const arch_register_class_t *cls)
4401 char problem_name[256];
4402 char dump_suffix[256];
4403 char dump_suffix2[256];
4404 struct obstack obst;
4406 ir_graph *irg = be_get_birg_irg(birg);
4408 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", irg, cls->name);
4409 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", cls->name);
4410 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", cls->name);
4412 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4413 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4415 if(opt_verify & VERIFY_DOMINANCE)
4416 be_check_dominance(irg);
4418 be_assure_liveness(birg);
4420 obstack_init(&obst);
4424 si.lpp = new_lpp(problem_name, lpp_minimize);
4425 si.remat_info = new_set(cmp_remat_info, 4096);
4426 si.interferences = new_set(cmp_interference, 32);
4427 si.memoperands = new_set(cmp_memoperands, 128);
4428 si.all_possible_remats = pset_new_ptr_default();
4429 si.spills = pset_new_ptr_default();
4430 si.inverse_ops = pset_new_ptr_default();
4433 si.n_regs = get_n_regs(&si);
4435 set_irg_link(irg, &si);
4438 /* compute phi classes */
4439 // phi_class_compute(irg);
4441 if(opt_dump_flags & DUMP_STATS)
4442 be_analyze_regpressure(birg, cls, "-pre");
4444 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4445 irg_block_walk_graph(irg, luke_initializer, NULL, &si);
4448 /* collect remats */
4449 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4450 irg_walk_graph(irg, walker_remat_collector, NULL, &si);
4453 /* insert possible remats */
4454 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4455 irg_block_walk_graph(irg, walker_remat_insertor, NULL, &si);
4456 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4458 if(opt_keep_alive & KEEPALIVE_REMATS) {
4459 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4460 connect_all_remats_with_keep(&si);
4461 /* dump graph with inserted remats */
4462 dump_graph_with_remats(irg, dump_suffix);
4465 /* insert copies for phi arguments not in my regclass */
4466 irg_walk_graph(irg, walker_regclass_copy_insertor, NULL, &si);
4468 /* recompute liveness */
4469 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4470 be_liveness_recompute(si.lv);
4473 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4474 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4475 irg_block_walk_graph(irg, luke_endwalker, NULL, &si);
4477 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4478 irg_block_walk_graph(irg, luke_blockwalker, NULL, &si);
4480 si.pc = phi_class_new_from_irg(birg->irg, 0);
4481 if (opt_memcopies) {
4482 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4483 memcopyhandler(&si);
4486 if (opt_dump_flags & DUMP_PROBLEM) {
4488 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4489 if ((f = fopen(buf, "wt")) != NULL) {
4490 lpp_dump_plain(si.lpp, f);
4495 if (opt_dump_flags & DUMP_MPS) {
4498 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4499 if ((f = fopen(buf, "wt")) != NULL) {
4500 mps_write_mps(si.lpp, s_mps_fixed, f);
4504 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4505 if ((f = fopen(buf, "wt")) != NULL) {
4506 mps_write_mst(si.lpp, s_mps_fixed, f);
4511 lpp_check_startvals(si.lpp);
4514 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4515 lpp_set_time_limit(si.lpp, opt_timeout);
4518 lpp_set_log(si.lpp, stdout);
4521 lpp_solve_cplex(si.lpp);
4523 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4525 assert(lpp_is_sol_valid(si.lpp)
4526 && "solution of ILP must be valid");
4528 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));
4530 if(opt_dump_flags & DUMP_SOLUTION) {
4534 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4535 if ((f = fopen(buf, "wt")) != NULL) {
4537 for (i = 0; i < si.lpp->var_next; ++i) {
4538 lpp_name_t *name = si.lpp->vars[i];
4539 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4545 #ifndef SCHEDULE_PHIM
4546 si.phims = pset_new_ptr_default();
4548 writeback_results(&si);
4553 kill_all_unused_values_in_schedule(&si);
4555 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4559 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4560 be_dump(irg, "-spills-placed", dump_ir_block_graph);
4562 // move reloads upwards
4563 be_liveness_recompute(si.lv);
4564 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4565 move_reloads_upward(&si);
4568 verify_phiclasses(&si);
4571 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4573 if(opt_dump_flags & DUMP_PRESSURE)
4574 dump_pressure_graph(&si, dump_suffix2);
4576 if(opt_dump_flags & DUMP_STATS)
4577 be_analyze_regpressure(birg, cls, "-post");
4579 if(opt_verify & VERIFY_DOMINANCE)
4580 be_check_dominance(irg);
4583 del_set(si.interferences);
4584 del_pset(si.inverse_ops);
4585 del_pset(si.all_possible_remats);
4586 del_set(si.memoperands);
4587 del_pset(si.spills);
4589 phi_class_free(si.pc);
4590 obstack_free(&obst, NULL);
4591 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4594 void be_init_spillremat(void)
4596 static be_spiller_t remat_spiller = {
4599 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
4600 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
4601 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
4602 lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat");
4604 be_register_spiller("remat", &remat_spiller);
4605 lc_opt_add_table(remat_grp, options);
4608 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat);
4610 #else /* WITH_ILP */
4612 static __attribute__((unused))
4613 void only_that_you_can_compile_without_WITH_ILP_defined(void)
4617 #endif /* WITH_ILP */