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
48 #include "irnodeset.h"
50 #include "iredges_t.h"
53 #include "irbackedge_t.h"
54 #include "irprofile.h"
58 #include <lpp/lpp_net.h>
59 #include <lpp/lpp_cplex.h>
64 #include "besched_t.h"
65 #include "bessaconstr.h"
67 #include "beintlive_t.h"
71 #include "bespillremat.h"
73 #include "bepressurestat.h"
74 #include "bespilloptions.h"
75 #include "bechordal_t.h"
79 #include "lc_opts_enum.h"
81 #define DUMP_PROBLEM 1
83 #define DUMP_SOLUTION 4
85 #define DUMP_PRESSURE 16
87 #define KEEPALIVE_REMATS 1
88 #define KEEPALIVE_SPILLS 2
89 #define KEEPALIVE_RELOADS 4
91 #define VERIFY_MEMINTERF 1
92 #define VERIFY_DOMINANCE 2
95 #define REMATS_BRIGGS 1
96 #define REMATS_NOINVERSE 2
99 static unsigned opt_dump_flags = 0;
100 static int opt_log = 0;
101 static unsigned opt_keep_alive = 0;
102 static int opt_goodwin = 1;
103 static int opt_memcopies = 1;
104 static int opt_memoperands = 1;
105 static int opt_verify = VERIFY_MEMINTERF;
106 static unsigned opt_remats = REMATS_ALL;
107 static int opt_repair_schedule = 0;
108 static int opt_no_enlarge_liveness = 0;
109 static int opt_remat_while_live = 1;
110 static int opt_timeout = 300;
111 static double opt_cost_reload = 8.0;
112 static double opt_cost_memoperand = 7.0;
113 static double opt_cost_spill = 15.0;
114 static double opt_cost_remat = 1.0;
117 static const lc_opt_enum_mask_items_t dump_items[] = {
118 { "problem", DUMP_PROBLEM },
120 { "solution", DUMP_SOLUTION },
121 { "stats", DUMP_STATS },
122 { "pressure", DUMP_PRESSURE },
126 static lc_opt_enum_mask_var_t dump_var = {
127 &opt_dump_flags, dump_items
130 static const lc_opt_enum_mask_items_t keepalive_items[] = {
131 { "remats", KEEPALIVE_REMATS },
132 { "spills", KEEPALIVE_SPILLS },
133 { "reloads", KEEPALIVE_RELOADS },
137 static lc_opt_enum_mask_var_t keep_alive_var = {
138 &opt_keep_alive, keepalive_items
141 static const lc_opt_enum_mask_items_t remats_items[] = {
142 { "none", REMATS_NONE },
143 { "briggs", REMATS_BRIGGS },
144 { "noinverse", REMATS_NOINVERSE },
145 { "all", REMATS_ALL },
149 static lc_opt_enum_mask_var_t remats_var = {
150 &opt_remats, remats_items
153 static const lc_opt_table_entry_t options[] = {
154 LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var),
156 LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin),
157 LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies),
158 LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands),
159 LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var),
160 LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule),
161 LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
162 LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live),
164 LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var),
165 LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log),
166 LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout),
168 LC_OPT_ENT_DBL ("cost_reload", "cost of a reload", &opt_cost_reload),
169 LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand),
170 LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill),
171 LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat),
175 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
176 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
179 //#define SOLVE_LOCAL
180 #define LPP_SERVER "i44pc52"
181 #define LPP_SOLVER "cplex"
184 #define MAX_PATHS INT_MAX
187 typedef struct _spill_ilp_t {
188 const arch_register_class_t *cls;
193 struct obstack *obst;
195 pset *all_possible_remats;
198 set *values; /**< for collecting all definitions of values before running ssa-construction */
204 #ifndef SCHEDULE_PHIM
207 DEBUG_ONLY(firm_dbg_module_t * dbg);
210 typedef int ilp_var_t;
211 typedef int ilp_cst_t;
213 typedef struct _spill_bb_t {
218 typedef struct _remat_t {
219 const ir_node *op; /**< for copy_irn */
220 const ir_node *value; /**< the value which is being recomputed by this remat */
221 const ir_node *proj; /**< not NULL if the above op produces a tuple */
222 int cost; /**< cost of this remat */
223 int inverse; /**< nonzero if this is an inverse remat */
227 * Data to be attached to each IR node. For remats this contains the ilp_var
228 * for this remat and for normal ops this contains the ilp_vars for
229 * reloading each operand
231 typedef struct _op_t {
236 const remat_t *remat; /** the remat this op belongs to */
237 int pre; /** 1, if this is a pressure-increasing remat */
241 ir_node *op; /** the operation this live range belongs to */
250 typedef struct _defs_t {
251 const ir_node *value;
252 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
253 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
256 typedef struct _remat_info_t {
257 const ir_node *irn; /**< the irn to which these remats belong */
258 pset *remats; /**< possible remats for this value */
259 pset *remats_by_operand; /**< remats with this value as operand */
262 typedef struct _keyval_t {
267 typedef struct _spill_t {
276 typedef struct _memoperand_t {
277 ir_node *irn; /**< the irn */
278 unsigned int pos; /**< the position of the argument */
279 ilp_var_t ilp; /**< the ilp var for this memory operand */
283 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
285 return arch_irn_consider_in_reg_alloc(si->birg->main_env->arch_env,
291 cmp_remat(const void *a, const void *b)
293 const keyval_t *p = a;
294 const keyval_t *q = b;
295 const remat_t *r = p->val;
296 const remat_t *s = q->val;
300 return !(r == s || r->op == s->op);
304 cmp_remat(const void *a, const void *b)
306 const remat_t *r = a;
307 const remat_t *s = b;
309 return !(r == s || r->op == s->op);
313 cmp_spill(const void *a, const void *b, size_t size)
315 const spill_t *p = a;
316 const spill_t *q = b;
319 // return !(p->irn == q->irn && p->bb == q->bb);
320 return !(p->irn == q->irn);
324 cmp_memoperands(const void *a, const void *b, size_t size)
326 const memoperand_t *p = a;
327 const memoperand_t *q = b;
330 return !(p->irn == q->irn && p->pos == q->pos);
334 set_find_keyval(set * set, const void * key)
339 return set_find(set, &query, sizeof(query), HASH_PTR(key));
343 set_insert_keyval(set * set, void * key, void * val)
349 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
353 set_find_def(set * set, const ir_node * value)
358 return set_find(set, &query, sizeof(query), HASH_PTR(value));
362 set_insert_def(set * set, const ir_node * value)
369 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
372 static memoperand_t *
373 set_insert_memoperand(set * set, ir_node * irn, unsigned int pos, ilp_var_t ilp)
380 return set_insert(set, &query, sizeof(query), HASH_PTR(irn)+pos);
383 static memoperand_t *
384 set_find_memoperand(set * set, const ir_node * irn, unsigned int pos)
388 query.irn = (ir_node*)irn;
390 return set_find(set, &query, sizeof(query), HASH_PTR(irn)+pos);
395 set_find_spill(set * set, const ir_node * value)
399 query.irn = (ir_node*)value;
400 return set_find(set, &query, sizeof(query), HASH_PTR(value));
403 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
404 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
405 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
406 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
407 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
410 cmp_remat_info(const void *a, const void *b, size_t size)
412 const remat_info_t *p = a;
413 const remat_info_t *q = b;
416 return !(p->irn == q->irn);
420 cmp_defs(const void *a, const void *b, size_t size)
426 return !(p->value == q->value);
430 cmp_keyval(const void *a, const void *b, size_t size)
432 const keyval_t *p = a;
433 const keyval_t *q = b;
436 return !(p->key == q->key);
440 execution_frequency(const spill_ilp_t *si, const ir_node * irn)
443 if(ir_profile_has_data())
444 return ((double)ir_profile_get_block_execcount(get_block_const(irn))) + FUDGE;
446 #ifndef EXECFREQ_LOOPDEPH
447 return get_block_execfreq(si->birg->exec_freq, get_block_const(irn)) + FUDGE;
450 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
452 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
457 get_cost(const spill_ilp_t * si, const ir_node * irn)
459 if(be_is_Spill(irn)) {
460 return opt_cost_spill;
461 } else if(be_is_Reload(irn)){
462 return opt_cost_reload;
464 return arch_get_op_estimated_cost(si->birg->main_env->arch_env, irn);
469 * Checks, whether node and its operands have suitable reg classes
472 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
475 const arch_env_t *arch_env = si->birg->main_env->arch_env;
476 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
480 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
483 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
484 ir_node *op = get_irn_n(irn, n);
485 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
488 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
495 * Try to create a remat from @p op with destination value @p dest_value
497 static INLINE remat_t *
498 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
500 remat_t *remat = NULL;
502 // if(!mode_is_datab(get_irn_mode(dest_value)))
505 if(dest_value == op) {
506 const ir_node *proj = NULL;
508 if(is_Proj(dest_value)) {
509 op = get_Proj_pred(op);
513 if(!is_rematerializable(si, op))
516 remat = obstack_alloc(si->obst, sizeof(*remat));
518 remat->cost = (int)get_cost(si, op);
519 remat->value = dest_value;
523 arch_inverse_t inverse;
526 /* get the index of the operand we want to retrieve by the inverse op */
527 for (n = get_irn_arity(op)-1; n>=0; --n) {
528 ir_node *arg = get_irn_n(op, n);
530 if(arg == dest_value) break;
534 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
536 /* else ask the backend to give an inverse op */
537 if(arch_get_inverse(si->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
540 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
542 assert(inverse.n > 0 && "inverse op should have at least one node");
544 for(i=inverse.n-1; i>=0; --i) {
545 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
549 remat = obstack_alloc(si->obst, sizeof(*remat));
550 remat->op = inverse.nodes[0];
551 remat->cost = inverse.costs;
552 remat->value = dest_value;
553 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
556 // Matze: commented this out, this doesn't seem to be true if
557 // the inverse is a simple operation with only 1 result...
558 //assert(is_Proj(remat->proj));
560 assert(0 && "I can not handle remats with more than 2 nodes");
567 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
569 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
577 add_remat(const spill_ilp_t * si, const remat_t * remat)
579 remat_info_t *remat_info,
584 assert(remat->value);
586 query.irn = remat->value;
588 query.remats_by_operand = NULL;
589 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
591 if(remat_info->remats == NULL) {
592 remat_info->remats = new_pset(cmp_remat, 4096);
594 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
596 /* insert the remat into the remats_be_operand set of each argument of the remat op */
597 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
598 ir_node *arg = get_irn_n(remat->op, n);
602 query.remats_by_operand = NULL;
603 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
605 if(remat_info->remats_by_operand == NULL) {
606 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
608 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
613 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
615 const ir_edge_t *edge = get_irn_out_edge_first(irn);
619 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
622 edge = get_irn_out_edge_next(irn, edge);
629 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
635 irn = get_Proj_pred(irn);
637 for(n=get_irn_arity(irn)-1; n>=0; --n) {
638 const ir_node *arg = get_irn_n(irn, n);
640 if(has_reg_class(si, arg)) ++ret;
647 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
652 if( has_reg_class(si, op)
653 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
654 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
656 remat = get_remat_from_op(si, op, op);
658 add_remat(si, remat);
662 if(opt_remats == REMATS_ALL) {
663 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
665 for (n = get_irn_arity(op)-1; n>=0; --n) {
666 ir_node *arg = get_irn_n(op, n);
668 if(has_reg_class(si, arg)) {
669 /* try to get an inverse remat */
670 remat = get_remat_from_op(si, arg, op);
672 add_remat(si, remat);
680 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
683 ir_node *def_block = get_nodes_block(val);
690 /* if pos is at end of a basic block */
692 ret = (pos == def_block || block_dominates(def_block, pos));
693 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
697 /* else if this is a normal operation */
698 block = get_nodes_block(pos);
699 if(block == def_block) {
700 if(!sched_is_scheduled(val)) return 1;
702 ret = sched_comes_after(val, pos);
703 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
707 ret = block_dominates(def_block, block);
708 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
712 static INLINE ir_node *
713 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
715 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->birg->main_env->arch_env);
719 * Returns first non-Phi node of block @p bb
721 static INLINE ir_node *
722 sched_block_first_nonphi(const ir_node * bb)
724 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
728 sched_skip_proj_predicator(const ir_node * irn, void * data)
731 return (is_Proj(irn));
734 static INLINE ir_node *
735 sched_next_nonproj(const ir_node * irn, int forward)
737 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
741 * Returns next operation node (non-Proj) after @p irn
742 * or the basic block of this node
744 static INLINE ir_node *
745 sched_next_op(const ir_node * irn)
747 ir_node *next = sched_next(irn);
752 return sched_next_nonproj(next, 1);
756 * Returns previous operation node (non-Proj) before @p irn
757 * or the basic block of this node
759 static INLINE ir_node *
760 sched_prev_op(const ir_node * irn)
762 ir_node *prev = sched_prev(irn);
767 return sched_next_nonproj(prev, 0);
771 sched_put_after(ir_node * insert, ir_node * irn)
773 if(is_Block(insert)) {
774 insert = sched_block_first_nonphi(insert);
776 insert = sched_next_op(insert);
779 sched_add_before(insert, irn);
783 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
785 if(is_Block(insert)) {
786 insert = sched_block_last_noncf(si, insert);
788 insert = sched_next_nonproj(insert, 0);
789 insert = sched_prev(insert);
792 sched_add_after(insert, irn);
796 next_post_remat(const ir_node * irn)
802 next = sched_block_first_nonphi(irn);
804 next = sched_next_op(irn);
807 if(sched_is_end(next))
810 op = get_irn_link(next);
811 if(op->is_remat && !op->attr.remat.pre) {
820 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
826 ret = sched_block_last_noncf(si, irn);
827 ret = sched_next(ret);
828 ret = sched_prev_op(ret);
830 ret = sched_prev_op(irn);
833 if(sched_is_end(ret) || is_Phi(ret))
836 op = (op_t*)get_irn_link(ret);
837 if(op->is_remat && op->attr.remat.pre) {
845 * Tells you whether a @p remat can be placed before the irn @p pos
848 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
850 const ir_node *op = remat->op;
856 prev = sched_block_last_noncf(si, pos);
857 prev = sched_next_nonproj(prev, 0);
859 prev = sched_prev_op(pos);
861 /* do not remat if the rematted value is defined immediately before this op */
862 if(prev == remat->op) {
867 /* this should be just fine, the following OP will be using this value, right? */
869 /* only remat AFTER the real definition of a value (?) */
870 if(!value_is_defined_before(si, pos, remat->value)) {
871 // ir_fprintf(stderr, "error(not defined)");
876 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
877 const ir_node *arg = get_irn_n(op, n);
879 if(opt_no_enlarge_liveness) {
880 if(has_reg_class(si, arg) && live) {
881 res &= pset_find_ptr((pset*)live, arg)?1:0;
883 res &= value_is_defined_before(si, pos, arg);
886 res &= value_is_defined_before(si, pos, arg);
894 * Tells you whether a @p remat can be placed after the irn @p pos
897 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
900 pos = sched_block_first_nonphi(pos);
902 pos = sched_next_op(pos);
905 /* only remat AFTER the real definition of a value (?) */
906 if(!value_is_defined_before(si, pos, remat->value)) {
910 return can_remat_before(si, remat, pos, live);
914 * Collect potetially rematerializable OPs
917 walker_remat_collector(ir_node * irn, void * data)
919 spill_ilp_t *si = data;
921 if(!is_Block(irn) && !is_Phi(irn)) {
922 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
923 get_remats_from_op(si, irn);
928 * Inserts a copy of @p irn before @p pos
931 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
936 bb = is_Block(pos)?pos:get_nodes_block(pos);
937 copy = exact_copy(irn);
939 set_phi_class(si->pc, copy, NULL);
940 set_nodes_block(copy, bb);
941 sched_put_before(si, pos, copy);
947 * Inserts a copy of @p irn after @p pos
950 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
955 bb = is_Block(pos)?pos:get_nodes_block(pos);
956 copy = exact_copy(irn);
958 set_phi_class(si->pc, copy, NULL);
959 set_nodes_block(copy, bb);
960 sched_put_after(pos, copy);
966 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
970 if(can_remat_after(si, remat, pos, live)) {
975 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
977 copy = insert_copy_after(si, remat->op, pos);
979 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
980 op = obstack_alloc(si->obst, sizeof(*op));
982 op->attr.remat.remat = remat;
983 op->attr.remat.pre = 0;
984 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
986 set_irn_link(copy, op);
987 pset_insert_ptr(si->all_possible_remats, copy);
989 proj_copy = insert_copy_after(si, remat->proj, copy);
990 set_irn_n(proj_copy, 0, copy);
991 set_irn_link(proj_copy, op);
992 pset_insert_ptr(si->all_possible_remats, proj_copy);
1004 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
1008 if(can_remat_before(si, remat, pos, live)) {
1013 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
1015 copy = insert_copy_before(si, remat->op, pos);
1017 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
1018 op = obstack_alloc(si->obst, sizeof(*op));
1020 op->attr.remat.remat = remat;
1021 op->attr.remat.pre = 1;
1022 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1024 set_irn_link(copy, op);
1025 pset_insert_ptr(si->all_possible_remats, copy);
1027 proj_copy = insert_copy_after(si, remat->proj, copy);
1028 set_irn_n(proj_copy, 0, copy);
1029 set_irn_link(proj_copy, op);
1030 pset_insert_ptr(si->all_possible_remats, proj_copy);
1042 get_block_n_succs(const ir_node *block) {
1043 const ir_edge_t *edge;
1045 assert(edges_activated(current_ir_graph));
1047 edge = get_block_succ_first(block);
1051 edge = get_block_succ_next(block, edge);
1052 return edge ? 2 : 1;
1056 is_start_block(const ir_node * bb)
1058 return get_irg_start_block(get_irn_irg(bb)) == bb;
1062 is_merge_edge(const ir_node * bb)
1064 if(is_start_block(bb))
1068 return get_block_n_succs(bb) == 1;
1074 is_diverge_edge(const ir_node * bb)
1076 if(is_start_block(bb))
1080 return get_Block_n_cfgpreds(bb) == 1;
1086 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1091 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1092 irn = be_lv_get_irn(si->lv, bb, i);
1094 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1095 pset_insert_ptr(live, irn);
1099 irn = sched_last(bb);
1101 /* all values eaten by control flow operations are also live until the end of the block */
1102 sched_foreach_reverse(bb, irn) {
1105 if(!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break;
1107 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1108 ir_node *arg = get_irn_n(irn,i);
1110 if(has_reg_class(si, arg)) {
1111 pset_insert_ptr(live, arg);
1116 * find values that are used by remats at end of block
1117 * and insert them into live set
1119 foreach_pre_remat(si, bb, irn) {
1122 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1123 ir_node *remat_arg = get_irn_n(irn, n);
1125 if(!has_reg_class(si, remat_arg)) continue;
1127 /* if value is becoming live through use by remat */
1128 if(!pset_find_ptr(live, remat_arg)) {
1129 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1131 pset_insert_ptr(live, remat_arg);
1138 walker_regclass_copy_insertor(ir_node * irn, void * data)
1140 spill_ilp_t *si = data;
1142 if(is_Phi(irn) && has_reg_class(si, irn)) {
1145 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1146 ir_node *phi_arg = get_irn_n(irn, n);
1147 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1149 if(!has_reg_class(si, phi_arg)) {
1150 ir_node *copy = be_new_Copy(si->cls, si->birg->irg, bb, phi_arg);
1151 ir_node *pos = sched_block_last_noncf(si, bb);
1152 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1154 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1155 sched_add_after(pos, copy);
1156 set_irn_n(irn, n, copy);
1159 op->attr.live_range.args.reloads = NULL;
1160 op->attr.live_range.ilp = ILP_UNDEF;
1161 set_irn_link(copy, op);
1168 * Insert (so far unused) remats into the irg to
1169 * recompute the potential liveness of all values
1172 walker_remat_insertor(ir_node * bb, void * data)
1174 spill_ilp_t *si = data;
1181 /* skip start block, no remats to do there */
1182 if(is_start_block(bb)) return;
1184 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1186 live = pset_new_ptr_default();
1187 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1188 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1190 /* add remats at end of block */
1191 if (has_reg_class(si, value)) {
1192 pset_insert_ptr(live, value);
1196 irn = sched_last(bb);
1197 while(!sched_is_end(irn)) {
1203 next = sched_prev(irn);
1205 /* delete defined value from live set */
1206 if(has_reg_class(si, irn)) {
1207 pset_remove_ptr(live, irn);
1210 if(is_Phi(irn) || is_Proj(irn)) {
1215 args = pset_new_ptr_default();
1216 used = pset_new_ptr_default();
1218 /* collect arguments of op and set args of op already live in epilog */
1219 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1220 ir_node *arg = get_irn_n(irn, n);
1222 pset_insert_ptr(args, arg);
1223 if(has_reg_class(si, arg)) {
1224 pset_insert_ptr(live, arg);
1225 pset_insert_ptr(used, arg);
1229 /* insert all possible remats before irn */
1230 pset_foreach(args, arg) {
1231 remat_info_t *remat_info,
1234 /* continue if the operand has the wrong reg class */
1235 if(!has_reg_class(si, arg))
1239 query.remats = NULL;
1240 query.remats_by_operand = NULL;
1241 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1247 if(remat_info->remats) {
1248 pset_foreach(remat_info->remats, remat) {
1249 ir_node *remat_irn = NULL;
1251 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1252 remat_irn = insert_remat_before(si, remat, irn, live);
1255 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1256 ir_node *remat_arg = get_irn_n(remat_irn, n);
1258 /* collect args of remats which are not args of op */
1259 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1260 pset_insert_ptr(used, remat_arg);
1268 /* do not place post remats after jumps */
1269 if(sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) {
1275 /* insert all possible remats after irn */
1276 post_remats = pset_new_ptr_default();
1277 pset_foreach(used, arg) {
1278 remat_info_t *remat_info,
1281 /* continue if the operand has the wrong reg class */
1282 if(!has_reg_class(si, arg))
1286 query.remats = NULL;
1287 query.remats_by_operand = NULL;
1288 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1294 if(remat_info->remats_by_operand) {
1295 pset_foreach(remat_info->remats_by_operand, remat) {
1296 /* do not insert remats producing the same value as one of the operands */
1297 if(!pset_find_ptr(args, remat->value)) {
1298 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1300 /* only remat values that can be used by real ops */
1301 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1302 pset_insert_ptr(post_remats, remat);
1308 pset_foreach(post_remats, remat) {
1309 insert_remat_after(si, remat, irn, live);
1311 del_pset(post_remats);
1318 /* add remats at end if successor has multiple predecessors */
1319 if(is_merge_edge(bb)) {
1320 pset *live_out = pset_new_ptr_default();
1323 get_live_end(si, bb, live_out);
1325 /* add remats at end of block */
1326 pset_foreach(live_out, value) {
1327 remat_info_t *remat_info,
1331 query.remats = NULL;
1332 query.remats_by_operand = NULL;
1333 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1335 if(remat_info && remat_info->remats) {
1336 pset_foreach(remat_info->remats, remat) {
1337 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1339 insert_remat_before(si, remat, bb, live_out);
1346 if(is_diverge_edge(bb)) {
1347 pset *live_in = pset_new_ptr_default();
1350 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1351 value = be_lv_get_irn(si->lv, bb, i);
1353 if(has_reg_class(si, value)) {
1354 pset_insert_ptr(live_in, value);
1357 /* add phis to live_in */
1358 sched_foreach(bb, value) {
1359 if(!is_Phi(value)) break;
1361 if(has_reg_class(si, value)) {
1362 pset_insert_ptr(live_in, value);
1366 /* add remat2s at beginning of block */
1367 post_remats = pset_new_ptr_default();
1368 pset_foreach(live_in, value) {
1369 remat_info_t *remat_info,
1373 query.remats = NULL;
1374 query.remats_by_operand = NULL;
1375 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1377 if(remat_info && remat_info->remats_by_operand) {
1378 pset_foreach(remat_info->remats_by_operand, remat) {
1379 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1381 /* put the remat here if all its args are available and result is still live */
1382 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1383 pset_insert_ptr(post_remats, remat);
1388 pset_foreach(post_remats, remat) {
1389 insert_remat_after(si, remat, bb, live_in);
1391 del_pset(post_remats);
1397 can_be_copied(const ir_node * bb, const ir_node * irn)
1399 const ir_edge_t *edge = get_block_succ_first(bb);
1400 const ir_node *next_bb = edge->src;
1401 int pos = edge->pos;
1404 assert(is_merge_edge(bb));
1406 sched_foreach(next_bb, phi) {
1407 const ir_node *phi_arg;
1409 if(!is_Phi(phi)) break;
1411 phi_arg = get_irn_n(phi, pos);
1413 if(phi_arg == irn) {
1421 * Initialize additional node info
1424 luke_initializer(ir_node * bb, void * data)
1426 spill_ilp_t *si = (spill_ilp_t*)data;
1427 spill_bb_t *spill_bb;
1430 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1431 set_irn_link(bb, spill_bb);
1433 sched_foreach(bb, irn) {
1436 op = obstack_alloc(si->obst, sizeof(*op));
1438 op->attr.live_range.ilp = ILP_UNDEF;
1441 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1442 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1444 } else if(!is_Proj(irn)) {
1445 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1446 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1448 op->attr.live_range.args.reloads = NULL;
1450 set_irn_link(irn, op);
1456 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1459 luke_endwalker(ir_node * bb, void * data)
1461 spill_ilp_t *si = (spill_ilp_t*)data;
1467 spill_bb_t *spill_bb = get_irn_link(bb);
1470 live = pset_new_ptr_default();
1471 use_end = pset_new_ptr_default();
1473 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1474 irn = be_lv_get_irn(si->lv, bb, i);
1475 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1476 pset_insert_ptr(live, irn);
1480 * find values that are used by remats at end of block
1481 * and insert them into live set
1483 foreach_pre_remat(si, bb, irn) {
1486 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1487 ir_node *remat_arg = get_irn_n(irn, n);
1489 if(has_reg_class(si, remat_arg)) {
1490 pset_insert_ptr(live, remat_arg);
1495 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1496 /* their reg_out must always be set */
1497 sched_foreach_reverse(bb, irn) {
1500 if(!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break;
1502 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1503 ir_node *irn_arg = get_irn_n(irn, n);
1505 if(has_reg_class(si, irn_arg)) {
1506 pset_insert_ptr(use_end, irn_arg);
1511 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1512 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1513 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1515 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1517 /* if this is a merge edge we can reload at the end of this block */
1518 if(is_merge_edge(bb)) {
1519 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1520 } else if(pset_count(use_end)){
1521 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1523 spill_bb->reloads = NULL;
1526 pset_foreach(live,irn) {
1530 int default_spilled;
1533 /* handle values used by control flow nodes later separately */
1534 if(pset_find_ptr(use_end, irn)) continue;
1537 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1539 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1541 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1542 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1543 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1545 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1546 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1548 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1549 /* by default spill value right after definition */
1550 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1551 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1553 if(is_merge_edge(bb)) {
1557 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1558 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1559 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1561 /* reload <= mem_out */
1562 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1563 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1564 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1567 spill->reg_in = ILP_UNDEF;
1568 spill->mem_in = ILP_UNDEF;
1571 pset_foreach(use_end,irn) {
1575 ilp_cst_t end_use_req,
1578 int default_spilled;
1581 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1583 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1585 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1586 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1588 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1589 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1591 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1592 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1593 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1595 /* reload for use be control flow op */
1596 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1597 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1598 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1600 /* reload <= mem_out */
1601 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1602 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1603 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1605 spill->reg_in = ILP_UNDEF;
1606 spill->mem_in = ILP_UNDEF;
1608 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1609 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1610 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1619 * Find a remat of value @p value in the epilog of @p pos
1622 find_post_remat(const ir_node * value, const ir_node * pos)
1624 while((pos = next_post_remat(pos)) != NULL) {
1627 op = get_irn_link(pos);
1628 assert(op->is_remat && !op->attr.remat.pre);
1630 if(op->attr.remat.remat->value == value)
1631 return (ir_node*)pos;
1634 const ir_edge_t *edge;
1635 foreach_out_edge(pos, edge) {
1636 ir_node *proj = get_edge_src_irn(edge);
1637 assert(is_Proj(proj));
1648 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1650 spill_bb_t *spill_bb = get_irn_link(bb);
1654 int default_spilled;
1657 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1659 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1661 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1663 spill->reg_out = ILP_UNDEF;
1664 spill->reg_in = ILP_UNDEF;
1665 spill->mem_in = ILP_UNDEF;
1667 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1668 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1670 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1671 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1672 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1679 * Inserts ILP-constraints and variables for memory copying before the given position
1682 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1684 const ir_node *succ;
1685 const ir_edge_t *edge;
1686 spill_bb_t *spill_bb = get_irn_link(block);
1695 assert(edges_activated(current_ir_graph));
1697 edge = get_block_succ_first(block);
1703 edge = get_block_succ_next(block, edge);
1704 /* next block can only contain phis, if this is a merge edge */
1707 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1708 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1710 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1711 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1713 pset_foreach(live, tmp) {
1716 op_t *op = get_irn_link(irn);
1717 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1719 spill = set_find_spill(spill_bb->ilp, tmp);
1722 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1724 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1726 sched_foreach(succ, phi) {
1727 const ir_node *to_copy;
1729 spill_t *to_copy_spill;
1730 op_t *phi_op = get_irn_link(phi);
1731 ilp_var_t reload = ILP_UNDEF;
1734 if(!is_Phi(phi)) break;
1735 if(!has_reg_class(si, phi)) continue;
1737 to_copy = get_irn_n(phi, pos);
1738 to_copy_op = get_irn_link(to_copy);
1740 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1741 assert(to_copy_spill);
1743 if(spill_bb->reloads) {
1744 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1747 reload = PTR_TO_INT(keyval->val);
1751 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1752 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1754 /* copy - reg_out - reload - remat - live_range <= 0 */
1755 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1756 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1757 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1758 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1759 foreach_pre_remat(si, block, tmp) {
1760 op_t *remat_op = get_irn_link(tmp);
1761 if(remat_op->attr.remat.remat->value == to_copy) {
1762 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1766 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1767 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1769 /* copy - reg_out - copyreg <= 0 */
1770 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1771 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1772 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1778 * Walk all irg blocks and emit this ILP
1781 luke_blockwalker(ir_node * bb, void * data)
1783 spill_ilp_t *si = (spill_ilp_t*)data;
1788 spill_bb_t *spill_bb = get_irn_link(bb);
1791 pset *defs = pset_new_ptr_default();
1792 const arch_env_t *arch_env = si->birg->main_env->arch_env;
1794 live = pset_new_ptr_default();
1796 /****************************************
1797 * B A S I C B L O C K E N D
1798 ***************************************/
1801 /* init live values at end of block */
1802 get_live_end(si, bb, live);
1804 pset_foreach(live, irn) {
1806 ilp_var_t reload = ILP_UNDEF;
1808 spill = set_find_spill(spill_bb->ilp, irn);
1811 if(spill_bb->reloads) {
1812 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1815 reload = PTR_TO_INT(keyval->val);
1819 op = get_irn_link(irn);
1820 assert(!op->is_remat);
1822 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1823 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1824 op->attr.live_range.op = bb;
1826 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1827 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1829 /* reg_out - reload - remat - live_range <= 0 */
1830 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1831 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1832 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1833 foreach_pre_remat(si, bb, tmp) {
1834 op_t *remat_op = get_irn_link(tmp);
1835 if(remat_op->attr.remat.remat->value == irn) {
1836 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1839 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1840 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1842 /* value may only die at bb end if it is used for a mem copy */
1843 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1844 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1845 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1846 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1847 foreach_pre_remat(si, bb, tmp) {
1848 op_t *remat_op = get_irn_link(tmp);
1849 if(remat_op->attr.remat.remat->value == irn) {
1850 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1853 if(is_merge_edge(bb)) {
1854 const ir_edge_t *edge = get_block_succ_first(bb);
1855 const ir_node *next_bb = edge->src;
1856 int pos = edge->pos;
1859 sched_foreach(next_bb, phi) {
1860 const ir_node *phi_arg;
1862 if(!is_Phi(phi)) break;
1864 phi_arg = get_irn_n(phi, pos);
1866 if(phi_arg == irn) {
1867 op_t *phi_op = get_irn_link(phi);
1868 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1870 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1877 insert_mem_copy_position(si, live, bb);
1880 * assure the remat args are available
1882 foreach_pre_remat(si, bb, tmp) {
1883 op_t *remat_op = get_irn_link(tmp);
1886 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1887 ir_node *remat_arg = get_irn_n(tmp, n);
1888 op_t *arg_op = get_irn_link(remat_arg);
1890 if(!has_reg_class(si, remat_arg)) continue;
1892 spill = set_find_spill(spill_bb->ilp, remat_arg);
1895 /* arguments of remats have to be live until the very end of the block
1896 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1897 * no remats, they could be in wrong order
1900 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1901 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1903 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1904 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1905 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1907 /* use reload placed for this argument */
1908 if(spill_bb->reloads) {
1909 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1912 ilp_var_t reload = PTR_TO_INT(keyval->val);
1914 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1919 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1924 /**************************************
1925 * B A S I C B L O C K B O D Y
1926 **************************************/
1928 sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1934 ilp_cst_t check_pre,
1940 ilp_cst_t one_memoperand = -1;
1942 /* iterate only until first phi */
1946 op = get_irn_link(irn);
1948 if(op->is_remat) continue;
1950 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1952 /* collect defined values */
1953 if(has_reg_class(si, irn)) {
1954 pset_insert_ptr(defs, irn);
1958 if(is_Proj(irn)) continue;
1961 * init set of irn's arguments
1962 * and all possibly used values around this op
1963 * and values defined by post remats
1965 args = new_set(cmp_keyval, get_irn_arity(irn));
1966 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1967 remat_defs = pset_new_ptr(pset_count(live));
1969 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1970 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1971 ir_node *irn_arg = get_irn_n(irn, n);
1972 if(has_reg_class(si, irn_arg)) {
1973 set_insert_keyval(args, irn_arg, (void*)n);
1974 pset_insert_ptr(used, irn_arg);
1977 foreach_post_remat(irn, tmp) {
1978 op_t *remat_op = get_irn_link(tmp);
1980 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
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);
1989 foreach_pre_remat(si, irn, tmp) {
1990 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1991 ir_node *remat_arg = get_irn_n(tmp, n);
1992 if(has_reg_class(si, remat_arg)) {
1993 pset_insert_ptr(used, remat_arg);
1999 /**********************************
2000 * I N E P I L O G O F irn
2001 **********************************/
2003 /* ensure each dying value is used by only one post remat */
2004 pset_foreach(used, tmp) {
2005 ir_node *value = tmp;
2006 op_t *value_op = get_irn_link(value);
2011 foreach_post_remat(irn, remat) {
2012 op_t *remat_op = get_irn_link(remat);
2014 for(n=get_irn_arity(remat)-1; n>=0; --n) {
2015 ir_node *remat_arg = get_irn_n(remat, n);
2017 /* if value is used by this remat add it to constraint */
2018 if(remat_arg == value) {
2020 /* sum remat2s <= 1 + n_remats*live_range */
2021 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2022 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2026 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2032 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2033 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2037 /* ensure at least one value dies at post remat */
2038 foreach_post_remat(irn, tmp) {
2039 op_t *remat_op = get_irn_link(tmp);
2040 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2043 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2044 remat_arg = get_irn_n(tmp, n);
2046 if(has_reg_class(si, remat_arg)) {
2048 /* does arg always die at this op? */
2049 if(!pset_find_ptr(live, remat_arg))
2050 goto skip_one_must_die;
2052 pset_insert_ptr(remat_args, remat_arg);
2056 /* remat + \sum live_range(remat_arg) <= |args| */
2057 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2058 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2059 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2061 pset_foreach(remat_args, remat_arg) {
2062 op_t *arg_op = get_irn_link(remat_arg);
2064 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2068 del_pset(remat_args);
2071 /* new live ranges for values from L\U defined by post remats */
2072 pset_foreach(live, tmp) {
2073 ir_node *value = tmp;
2074 op_t *value_op = get_irn_link(value);
2076 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2077 ilp_var_t prev_lr = ILP_UNDEF;
2080 if(pset_find_ptr(remat_defs, value)) {
2082 /* next_live_range <= prev_live_range + sum remat2s */
2083 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2084 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2086 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2087 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2089 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2090 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2092 foreach_post_remat(irn, remat) {
2093 op_t *remat_op = get_irn_link(remat);
2095 /* if value is being rematerialized by this remat */
2096 if(value == remat_op->attr.remat.remat->value) {
2097 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2101 value_op->attr.live_range.ilp = prev_lr;
2102 value_op->attr.live_range.op = irn;
2107 /* requirements for post remats and start live ranges from L/U' for values dying here */
2108 foreach_post_remat(irn, tmp) {
2109 op_t *remat_op = get_irn_link(tmp);
2112 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2113 ir_node *remat_arg = get_irn_n(tmp, n);
2114 op_t *arg_op = get_irn_link(remat_arg);
2116 if(!has_reg_class(si, remat_arg)) continue;
2118 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2119 if(!pset_find_ptr(used, remat_arg)) {
2120 /* remat <= live_range(remat_arg) */
2121 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2122 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2124 /* if value is becoming live through use by remat2 */
2125 if(!pset_find_ptr(live, remat_arg)) {
2128 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2129 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2131 arg_op->attr.live_range.ilp = lr;
2132 arg_op->attr.live_range.op = irn;
2134 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2136 pset_insert_ptr(live, remat_arg);
2137 add_to_spill_bb(si, bb, remat_arg);
2140 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2141 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2146 d = pset_count(defs);
2147 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2149 /* count how many regs irn needs for arguments */
2150 u = set_count(args);
2153 /* check the register pressure in the epilog */
2154 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2155 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2156 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2158 /* add L\U' to check_post */
2159 pset_foreach(live, tmp) {
2160 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2161 /* if a live value is not used by irn */
2162 tmp_op = get_irn_link(tmp);
2163 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2167 /***********************************************************
2168 * 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
2169 **********************************************************/
2172 pset_foreach(used, tmp) {
2178 op_t *arg_op = get_irn_link(arg);
2181 spill = add_to_spill_bb(si, bb, arg);
2183 /* new live range for each used value */
2184 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2185 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2187 /* the epilog stuff - including post_use, check_post, check_post_remat */
2188 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2189 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2191 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2193 /* arg is live throughout epilog if the next live_range is in a register */
2194 if(pset_find_ptr(live, arg)) {
2195 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2197 /* post_use >= next_lr + remat */
2198 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2199 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2200 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2201 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2204 /* forall post remat which use arg add a similar cst */
2205 foreach_post_remat(irn, remat) {
2208 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2209 ir_node *remat_arg = get_irn_n(remat, n);
2210 op_t *remat_op = get_irn_link(remat);
2212 if(remat_arg == arg) {
2213 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2215 /* post_use >= remat */
2216 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2217 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2218 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2219 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2224 /* if value is not an arg of op and not possibly defined by post remat
2225 * then it may only die and not become live
2227 if(!set_find_keyval(args, arg)) {
2228 /* post_use <= prev_lr */
2229 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2230 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2231 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2232 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2234 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2235 /* next_lr <= prev_lr */
2236 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2237 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2238 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2239 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2243 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2244 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2245 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2246 ilp_var_t memoperand;
2248 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2249 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2250 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2252 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2253 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2255 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2256 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2261 /* new live range begins for each used value */
2262 arg_op->attr.live_range.ilp = prev_lr;
2263 arg_op->attr.live_range.op = irn;
2265 pset_insert_ptr(live, arg);
2268 /* just to be sure */
2269 check_post = ILP_UNDEF;
2271 /* allow original defintions to be removed */
2272 if(opt_repair_schedule) {
2273 pset_foreach(defs, tmp) {
2274 op_t *tmp_op = get_irn_link(tmp);
2275 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2280 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2281 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0*get_cost(si, irn)*execution_frequency(si, bb), 0.0);
2283 /* op may not be killed if its first live_range is 1 */
2284 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2285 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2286 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2287 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2289 /* op may not be killed if it is spilled after the definition */
2290 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2291 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2292 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2293 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2298 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2299 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(si, irn)*execution_frequency(si, bb), 1.0);
2301 /* op may not be killed if its first live_range is 1 */
2302 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2303 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2304 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2305 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2307 /* op may not be killed if it is spilled after the definition */
2308 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2309 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2310 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2311 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2316 pset_foreach(defs, tmp) {
2317 op_t *tmp_op = get_irn_link(tmp);
2318 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2321 /* live_range or spill should be 1
2322 TODO: lr should be live until first use */
2323 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2324 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2325 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2326 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2336 /* check the register pressure in the prolog */
2337 /* sum_{L\U} lr <= k - |U| */
2338 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2339 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2341 /* for the prolog remove defined values from the live set */
2342 pset_foreach(defs, tmp) {
2343 pset_remove_ptr(live, tmp);
2346 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2347 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2348 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2351 /***********************************************************
2352 * 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
2353 **********************************************************/
2356 set_foreach(args, keyval) {
2358 const ir_node *arg = keyval->key;
2359 int i = PTR_TO_INT(keyval->val);
2360 op_t *arg_op = get_irn_link(arg);
2361 ilp_cst_t requirements;
2364 spill = set_find_spill(spill_bb->ilp, arg);
2367 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2368 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);
2370 /* reload <= mem_out */
2371 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2372 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2373 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2374 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2376 /* requirement: arg must be in register for use */
2377 /* reload + remat + live_range == 1 */
2378 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2379 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2381 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2382 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2383 foreach_pre_remat(si, irn, tmp) {
2384 op_t *remat_op = get_irn_link(tmp);
2385 if(remat_op->attr.remat.remat->value == arg) {
2386 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2390 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2392 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2393 if(get_irn_n(irn, n) == arg) {
2397 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2398 if(get_irn_n(irn, n) == arg && arch_possible_memory_operand(arch_env, irn, n)) {
2399 memoperand_t *memoperand;
2400 memoperand = set_find_memoperand(si->memoperands, irn, n);
2402 /* memoperand <= mem_out */
2403 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2404 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2405 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2406 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2408 /* the memoperand is only sufficient if it is used once by the op */
2409 if(n_memoperands == 1)
2410 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2412 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2414 /* we have one more free register if we use a memory operand */
2415 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2421 /* iterate over L\U */
2422 pset_foreach(live, tmp) {
2423 if(!set_find_keyval(args, tmp)) {
2424 /* if a live value is not used by irn */
2425 tmp_op = get_irn_link(tmp);
2426 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2430 /* requirements for remats */
2431 foreach_pre_remat(si, irn, tmp) {
2432 op_t *remat_op = get_irn_link(tmp);
2435 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2436 ir_node *remat_arg = get_irn_n(tmp, n);
2437 op_t *arg_op = get_irn_link(remat_arg);
2439 if(!has_reg_class(si, remat_arg)) continue;
2441 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2442 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2443 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2445 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2446 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2448 /* if remat arg is also used by current op then we can use reload placed for this argument */
2449 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2450 int index = (int)keyval->val;
2452 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2460 /*************************
2461 * D O N E W I T H O P
2462 *************************/
2464 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2466 pset_foreach(live, tmp) {
2467 assert(has_reg_class(si, tmp));
2471 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2472 ir_node *arg = get_irn_n(irn, n);
2474 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2478 del_pset(remat_defs);
2482 defs = pset_new_ptr_default();
2484 /* skip everything above barrier in start block */
2485 if(is_start_block(bb) && be_is_Barrier(irn)) {
2486 assert(pset_count(live) == 0);
2495 /***************************************
2496 * B E G I N N I N G O F B L O C K
2497 ***************************************/
2500 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2501 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2503 pset_foreach(live, irn) {
2504 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2507 /* construct mem_outs for all values */
2508 set_foreach(spill_bb->ilp, spill) {
2509 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2510 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2512 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2513 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2515 if(pset_find_ptr(live, spill->irn)) {
2516 int default_spilled;
2517 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2519 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2520 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2521 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2522 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2524 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2526 op_t *op = get_irn_link(spill->irn);
2528 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2529 const ir_node *arg = get_irn_n(spill->irn, n);
2535 /* argument already done? */
2536 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2538 /* get sum of execution frequencies of blocks with the same phi argument */
2539 for(m=n; m>=0; --m) {
2540 const ir_node *arg2 = get_irn_n(spill->irn, m);
2543 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2547 /* copies are not for free */
2548 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2549 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2551 for(m=n; m>=0; --m) {
2552 const ir_node *arg2 = get_irn_n(spill->irn, m);
2555 op->attr.live_range.args.copies[m] = var;
2560 /* copy <= mem_in */
2561 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2562 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2563 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2564 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2571 foreach_post_remat(bb, tmp) {
2573 op_t *remat_op = get_irn_link(tmp);
2574 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2577 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2578 remat_arg = get_irn_n(tmp, n);
2580 if(has_reg_class(si, remat_arg)) {
2581 pset_insert_ptr(remat_args, remat_arg);
2585 /* remat + \sum live_range(remat_arg) <= |args| */
2586 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2587 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2588 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2590 pset_foreach(remat_args, remat_arg) {
2591 if(pset_find_ptr(live, remat_arg)) {
2592 op_t *remat_arg_op = get_irn_link(remat_arg);
2593 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2596 del_pset(remat_args);
2599 foreach_post_remat(bb, tmp) {
2602 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2603 ir_node *remat_arg = get_irn_n(tmp, n);
2605 /* if value is becoming live through use by remat2 */
2606 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2607 op_t *remat_arg_op = get_irn_link(remat_arg);
2610 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2612 pset_insert_ptr(live, remat_arg);
2613 spill = add_to_spill_bb(si, bb, remat_arg);
2614 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2616 /* we need reg_in and mem_in for this value; they will be referenced later */
2617 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2618 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2619 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2620 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2623 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2624 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2625 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2626 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2631 /* L\U is empty at bb start */
2632 /* arg is live throughout epilog if it is reg_in into this block */
2634 /* check the register pressure at the beginning of the block
2637 /* reg_in entspricht post_use */
2639 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2640 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2642 pset_foreach(live, irn) {
2645 spill = set_find_spill(spill_bb->ilp, irn);
2648 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2649 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2651 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2653 /* spill + mem_in <= 1 */
2654 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2655 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2657 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2658 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2660 } /* post_remats are NOT included in register pressure check because
2661 they do not increase regpressure */
2663 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2664 pset_foreach(live, irn) {
2668 spill = set_find_spill(spill_bb->ilp, irn);
2669 assert(spill && spill->irn == irn);
2671 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2672 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2675 ir_node *phi_arg = get_Phi_pred(irn, n);
2676 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2677 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2679 op_t *op = get_irn_link(irn);
2681 /* although the phi is in the right regclass one or more of
2682 * its arguments can be in a different one or at least to
2685 if(has_reg_class(si, phi_arg)) {
2686 /* mem_in < mem_out_arg + copy */
2687 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2688 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2690 /* reg_in < reg_out_arg */
2691 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2692 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2694 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2695 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2697 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2700 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2702 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2704 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2708 /* else assure the value arrives on all paths in the same resource */
2710 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2713 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2714 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2717 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2718 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2719 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2720 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2722 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2723 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2725 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2728 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2729 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2734 foreach_post_remat(bb, tmp) {
2737 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2738 ir_node *remat_arg = get_irn_n(tmp, n);
2739 op_t *remat_op = get_irn_link(tmp);
2741 if(!has_reg_class(si, remat_arg)) continue;
2743 spill = set_find_spill(spill_bb->ilp, remat_arg);
2746 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2747 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2748 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2749 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2753 pset_foreach(live, irn) {
2754 const op_t *op = get_irn_link(irn);
2755 const ir_node *remat;
2760 foreach_post_remat(bb, remat) {
2763 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2764 const ir_node *arg = get_irn_n(remat, n);
2767 const op_t *remat_op = get_irn_link(remat);
2769 if(cst == ILP_UNDEF) {
2770 /* sum remat2s <= 1 + n_remats*live_range */
2771 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2772 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2774 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2780 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2781 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2785 /* first live ranges from reg_ins */
2786 pset_foreach(live, irn) {
2787 op_t *op = get_irn_link(irn);
2789 if(op->attr.live_range.ilp != ILP_UNDEF) {
2791 spill = set_find_spill(spill_bb->ilp, irn);
2792 assert(spill && spill->irn == irn);
2794 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2795 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2796 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2797 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2799 foreach_post_remat(bb, tmp) {
2800 op_t *remat_op = get_irn_link(tmp);
2802 if(remat_op->attr.remat.remat->value == irn) {
2803 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2809 /* walk forward now and compute constraints for placing spills */
2810 /* this must only be done for values that are not defined in this block */
2811 pset_foreach(live, irn) {
2813 * if value is defined in this block we can anways place the spill directly after the def
2814 * -> no constraint necessary
2816 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2821 spill = set_find_spill(spill_bb->ilp, irn);
2824 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2825 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2827 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2828 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2831 sched_foreach_op(bb, tmp) {
2832 op_t *op = get_irn_link(tmp);
2834 if(is_Phi(tmp)) continue;
2835 assert(!is_Proj(tmp));
2838 const ir_node *value = op->attr.remat.remat->value;
2841 /* only collect remats up to the first real use of a value */
2842 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2847 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2848 ir_node *arg = get_irn_n(tmp, n);
2851 /* if a value is used stop collecting remats */
2864 typedef struct _irnlist_t {
2865 struct list_head list;
2869 typedef struct _interference_t {
2870 struct list_head blocklist;
2876 cmp_interference(const void *a, const void *b, size_t size)
2878 const interference_t *p = a;
2879 const interference_t *q = b;
2882 return !(p->a == q->a && p->b == q->b);
2885 static interference_t *
2886 set_find_interference(set * set, ir_node * a, ir_node * b)
2888 interference_t query;
2890 query.a = (a>b)?a:b;
2891 query.b = (a>b)?b:a;
2893 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2896 static interference_t *
2897 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2899 interference_t query,
2901 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2905 result = set_find_interference(set, a, b);
2908 list_add(&list->list, &result->blocklist);
2912 query.a = (a>b)?a:b;
2913 query.b = (a>b)?b:a;
2915 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2917 INIT_LIST_HEAD(&result->blocklist);
2918 list_add(&list->list, &result->blocklist);
2924 int values_interfere_in_block(const spill_ilp_t *si, const ir_node *bb, const ir_node *a, const ir_node *b)
2926 const ir_edge_t *edge;
2928 if (get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2929 /* both values are live in, so they interfere */
2933 /* ensure a dominates b */
2934 if (value_dominates(b, a)) {
2940 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2943 /* the following code is stolen from bera.c */
2944 if (be_is_live_end(si->lv, bb, a))
2947 foreach_out_edge(a, edge) {
2948 const ir_node *user = edge->src;
2949 if (get_nodes_block(user) == bb
2952 && ! pset_find_ptr(si->inverse_ops, user)
2953 && value_dominates(b, user))
2961 * Walk all irg blocks and collect interfering values inside of phi classes
2964 luke_interferencewalker(ir_node * bb, void * data)
2966 spill_ilp_t *si = (spill_ilp_t*)data;
2969 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2970 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2971 op_t *a_op = get_irn_link(a);
2974 /* a is only interesting if it is in my register class and if it is inside a phi class */
2975 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
2976 if (a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2979 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)) {
2980 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2981 op_t *b_op = get_irn_link(b);
2983 /* a and b are only interesting if they are in the same phi class */
2984 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
2985 if (b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2988 if (values_interfere_in_block(si, bb, a, b)) {
2989 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2990 set_insert_interference(si, si->interferences, a, b, bb);
2998 static unsigned int copy_path_id = 0;
3001 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
3008 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
3009 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3011 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
3013 pset_foreach(copies, ptr) {
3014 copy = PTR_TO_INT(ptr);
3015 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
3020 * @parameter copies contains a path of copies which lead us to irn
3021 * @parameter visited contains a set of nodes already visited on this path
3024 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3026 const ir_edge_t *edge;
3027 op_t *op = get_irn_link(irn);
3028 pset *visited_users = pset_new_ptr_default();
3031 if(op->is_remat) return 0;
3033 pset_insert_ptr(visited, irn);
3037 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3039 /* visit all operands */
3040 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3041 ir_node *arg = get_irn_n(irn, n);
3042 ilp_var_t copy = op->attr.live_range.args.copies[n];
3044 if(!has_reg_class(si, arg)) continue;
3045 if(pset_find_ptr(visited_operands, arg)) continue;
3046 pset_insert_ptr(visited_operands, arg);
3049 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3050 del_pset(visited_operands);
3051 del_pset(visited_users);
3052 pset_remove_ptr(visited, irn);
3055 pset_insert(copies, INT_TO_PTR(copy), copy);
3056 write_copy_path_cst(si, copies, any_interfere);
3057 pset_remove(copies, INT_TO_PTR(copy), copy);
3058 } else if(!pset_find_ptr(visited, arg)) {
3059 pset_insert(copies, INT_TO_PTR(copy), copy);
3060 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3061 pset_remove(copies, INT_TO_PTR(copy), copy);
3063 if(paths > MAX_PATHS) {
3064 if(pset_count(copies) == 0) {
3068 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3069 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3070 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3071 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3072 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3076 del_pset(visited_operands);
3077 del_pset(visited_users);
3078 pset_remove_ptr(visited, irn);
3081 } else if(pset_count(copies) == 0) {
3087 del_pset(visited_operands);
3090 /* visit all uses which are phis */
3091 foreach_out_edge(irn, edge) {
3092 ir_node *user = edge->src;
3093 int pos = edge->pos;
3094 op_t *op = get_irn_link(user);
3097 if(!is_Phi(user)) continue;
3098 if(!has_reg_class(si, user)) continue;
3099 if(pset_find_ptr(visited_users, user)) continue;
3100 pset_insert_ptr(visited_users, user);
3102 copy = op->attr.live_range.args.copies[pos];
3104 if(user == target) {
3105 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3106 del_pset(visited_users);
3107 pset_remove_ptr(visited, irn);
3110 pset_insert(copies, INT_TO_PTR(copy), copy);
3111 write_copy_path_cst(si, copies, any_interfere);
3112 pset_remove(copies, INT_TO_PTR(copy), copy);
3113 } else if(!pset_find_ptr(visited, user)) {
3114 pset_insert(copies, INT_TO_PTR(copy), copy);
3115 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3116 pset_remove(copies, INT_TO_PTR(copy), copy);
3118 if(paths > MAX_PATHS) {
3119 if(pset_count(copies) == 0) {
3123 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3124 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3125 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3126 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3127 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3131 del_pset(visited_users);
3132 pset_remove_ptr(visited, irn);
3135 } else if(pset_count(copies) == 0) {
3141 del_pset(visited_users);
3142 pset_remove_ptr(visited, irn);
3147 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3149 pset * copies = pset_new_ptr_default();
3150 pset * visited = pset_new_ptr_default();
3152 find_copy_path(si, a, b, any_interfere, copies, visited);
3160 memcopyhandler(spill_ilp_t * si)
3162 interference_t *interference;
3164 /* teste Speicherwerte auf Interferenz */
3166 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3167 irg_block_walk_graph(si->birg->irg, luke_interferencewalker, NULL, si);
3169 /* now lets emit the ILP unequations for the crap */
3170 set_foreach(si->interferences, interference) {
3172 ilp_var_t interfere, any_interfere;
3173 ilp_cst_t any_interfere_cst, cst;
3174 const ir_node *a = interference->a;
3175 const ir_node *b = interference->b;
3177 /* any_interf <= \sum interf */
3178 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3179 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3180 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3182 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3184 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3185 const ir_node *bb = irnlist->irn;
3186 spill_bb_t *spill_bb = get_irn_link(bb);
3191 spilla = set_find_spill(spill_bb->ilp, a);
3194 spillb = set_find_spill(spill_bb->ilp, b);
3197 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3198 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3199 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3200 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3201 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3202 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3204 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3205 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3207 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3208 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3209 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3210 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3211 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3213 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3214 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3216 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3217 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3218 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3220 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3221 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3223 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3224 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3225 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3228 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3230 /* any_interfere >= interf */
3231 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3232 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3234 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3235 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3238 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3239 gen_copy_constraints(si,a,b,any_interfere);
3247 return fabs(x) < 0.00001;
3250 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3252 spill_ilp_t *si = get_irg_link(current_ir_graph);
3255 if(pset_find_ptr(si->all_possible_remats, n)) {
3256 op_t *op = (op_t*)get_irn_link(n);
3257 assert(op && op->is_remat);
3259 if(!op->attr.remat.remat->inverse) {
3260 if(op->attr.remat.pre) {
3261 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3263 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3268 op_t *op = (op_t*)get_irn_link(n);
3269 assert(op && op->is_remat);
3271 if(op->attr.remat.pre) {
3272 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3274 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3285 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3287 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3288 be_dump(irg, suffix, dump_ir_block_graph_sched);
3289 set_dump_node_vcgattr_hook(NULL);
3293 * Edge hook to dump the schedule edges with annotated register pressure.
3296 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3298 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3299 ir_node *prev = sched_prev(irn);
3300 fprintf(F, "edge:{sourcename:\"");
3302 fprintf(F, "\" targetname:\"");
3304 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3305 fprintf(F, "\" color:magenta}\n");
3311 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3313 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3315 dump_consts_local(0);
3316 set_dump_node_edge_hook(sched_pressure_edge_hook);
3317 dump_ir_block_graph(irg, suffix);
3318 set_dump_node_edge_hook(old_edge_hook);
3322 walker_pressure_annotator(ir_node * bb, void * data)
3324 spill_ilp_t *si = data;
3327 pset *live = pset_new_ptr_default();
3330 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3331 irn = be_lv_get_irn(si->lv, bb, i);
3333 if (has_reg_class(si, irn)) {
3334 pset_insert_ptr(live, irn);
3338 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3340 sched_foreach_reverse(bb, irn) {
3342 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3346 if(has_reg_class(si, irn)) {
3347 pset_remove_ptr(live, irn);
3348 if(is_Proj(irn)) ++projs;
3351 if(!is_Proj(irn)) projs = 0;
3353 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3354 ir_node *arg = get_irn_n(irn, n);
3356 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3358 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3365 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3367 be_dump(si->birg->irg, suffix, dump_ir_block_graph_sched_pressure);
3371 connect_all_remats_with_keep(spill_ilp_t * si)
3379 n_remats = pset_count(si->all_possible_remats);
3381 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3384 pset_foreach(si->all_possible_remats, irn) {
3389 si->keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_remats, ins);
3391 obstack_free(si->obst, ins);
3396 connect_all_spills_with_keep(spill_ilp_t * si)
3405 n_spills = pset_count(si->spills);
3407 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3410 pset_foreach(si->spills, irn) {
3415 keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_spills, ins);
3417 obstack_free(si->obst, ins);
3421 /** insert a spill at an arbitrary position */
3422 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert)
3424 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
3425 ir_graph *irg = get_irn_irg(bl);
3426 ir_node *frame = get_irg_frame(irg);
3429 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
3430 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
3432 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
3435 * search the right insertion point. a spill of a phi cannot be put
3436 * directly after the phi, if there are some phis behind the one which
3437 * is spilled. Also, a spill of a Proj must be after all Projs of the
3440 * Here's one special case:
3441 * If the spill is in the start block, the spill must be after the frame
3442 * pointer is set up. This is done by setting insert to the end of the block
3443 * which is its default initialization (see above).
3446 if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3449 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3452 sched_add_after(insert, spill);
3457 delete_remat(spill_ilp_t * si, ir_node * remat) {
3459 ir_node *bad = get_irg_bad(si->birg->irg);
3461 sched_remove(remat);
3463 /* kill links to operands */
3464 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3465 set_irn_n(remat, n, bad);
3470 clean_remat_info(spill_ilp_t * si)
3474 remat_info_t *remat_info;
3475 ir_node *bad = get_irg_bad(si->birg->irg);
3477 set_foreach(si->remat_info, remat_info) {
3478 if(!remat_info->remats) continue;
3480 pset_foreach(remat_info->remats, remat)
3482 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3483 if(sched_is_scheduled(remat->proj)) {
3484 sched_remove((ir_node*)remat->proj);
3486 set_irn_n((ir_node*)remat->proj, -1, bad);
3487 set_irn_n((ir_node*)remat->proj, 0, bad);
3490 if(get_irn_n_edges(remat->op) == 0) {
3491 if(sched_is_scheduled(remat->op)) {
3492 sched_remove((ir_node*)remat->op);
3494 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3495 set_irn_n((ir_node*)remat->op, n, bad);
3500 if(remat_info->remats) del_pset(remat_info->remats);
3501 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3506 delete_unnecessary_remats(spill_ilp_t * si)
3508 if(opt_keep_alive & KEEPALIVE_REMATS) {
3510 ir_node *bad = get_irg_bad(si->birg->irg);
3513 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3514 ir_node *keep_arg = get_irn_n(si->keep, n);
3515 op_t *arg_op = get_irn_link(keep_arg);
3518 assert(arg_op->is_remat);
3520 name = si->lpp->vars[arg_op->attr.remat.ilp];
3522 if(is_zero(name->value)) {
3523 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3524 /* TODO check whether reload is preferred over remat (could be bug) */
3525 delete_remat(si, keep_arg);
3527 if(!arg_op->attr.remat.remat->inverse) {
3528 if(arg_op->attr.remat.pre) {
3529 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3531 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3534 if(arg_op->attr.remat.pre) {
3535 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3537 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3542 set_irn_n(si->keep, n, bad);
3545 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3550 pset_foreach(si->all_possible_remats, remat) {
3551 op_t *remat_op = get_irn_link(remat);
3552 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3554 if(is_zero(name->value)) {
3555 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3556 /* TODO check whether reload is preferred over remat (could be bug) */
3557 delete_remat(si, remat);
3559 if(!remat_op->attr.remat.remat->inverse) {
3560 if(remat_op->attr.remat.pre) {
3561 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3563 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3566 if(remat_op->attr.remat.pre) {
3567 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3569 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3578 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3580 pset *spills = pset_new_ptr_default();
3582 const ir_node *next;
3585 defs = set_find_def(si->values, value);
3587 if(defs && defs->spills) {
3588 for(next = defs->spills; next; next = get_irn_link(next)) {
3589 pset_insert_ptr(spills, next);
3597 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3601 assert( get_irn_arity(block) == arity );
3603 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3604 res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
3610 * @param before The node after which the spill will be placed in the schedule
3613 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3617 const arch_env_t *arch_env = si->birg->main_env->arch_env;
3619 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3621 spill = be_spill2(arch_env, irn, before);
3623 defs = set_insert_def(si->values, value);
3626 /* enter into the linked list */
3627 set_irn_link(spill, defs->spills);
3628 defs->spills = spill;
3630 if(opt_keep_alive & KEEPALIVE_SPILLS)
3631 pset_insert_ptr(si->spills, spill);
3637 * @param before The Phi node which has to be spilled
3640 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3647 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3649 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3650 ins[n] = si->m_unknown;
3653 mem_phi = new_r_PhiM_nokeep(si->birg->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3655 defs = set_insert_def(si->values, phi);
3658 /* enter into the linked list */
3659 set_irn_link(mem_phi, defs->spills);
3660 defs->spills = mem_phi;
3662 #ifdef SCHEDULE_PHIM
3663 sched_add_after(phi, mem_phi);
3665 pset_insert_ptr(si->phims, mem_phi);
3668 if(opt_keep_alive & KEEPALIVE_SPILLS)
3669 pset_insert_ptr(si->spills, mem_phi);
3676 * Add remat to list of defs, destroys link field!
3679 insert_remat(spill_ilp_t * si, ir_node * remat)
3682 op_t *remat_op = get_irn_link(remat);
3684 assert(remat_op->is_remat);
3686 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3689 /* enter into the linked list */
3690 set_irn_link(remat, defs->remats);
3691 defs->remats = remat;
3696 * Add reload before operation and add to list of defs
3699 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3704 const arch_env_t *arch_env = si->birg->main_env->arch_env;
3706 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3708 defs = set_find_def(si->values, value);
3710 spill = defs->spills;
3711 assert(spill && "no spill placed before reload");
3713 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3715 /* enter into the linked list */
3716 set_irn_link(reload, defs->remats);
3717 defs->remats = reload;
3722 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3725 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3727 const arch_env_t *arch_env = si->birg->main_env->arch_env;
3729 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3731 defs = set_find_def(si->values, value);
3733 spill = defs->spills;
3734 assert(spill && "no spill placed before reload");
3736 arch_perform_memory_operand(arch_env, memoperand->irn, spill, memoperand->pos);
3739 void insert_memoperands(spill_ilp_t * si)
3741 memoperand_t *memoperand;
3744 set_foreach(si->memoperands, memoperand) {
3745 name = si->lpp->vars[memoperand->ilp];
3746 if(!is_zero(name->value)) {
3747 perform_memory_operand(si, memoperand);
3753 walker_spill_placer(ir_node * bb, void * data) {
3754 spill_ilp_t *si = (spill_ilp_t*)data;
3756 spill_bb_t *spill_bb = get_irn_link(bb);
3757 pset *spills_to_do = pset_new_ptr_default();
3760 set_foreach(spill_bb->ilp, spill) {
3763 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3764 name = si->lpp->vars[spill->mem_in];
3765 if(!is_zero(name->value)) {
3768 mem_phi = insert_mem_phi(si, spill->irn);
3770 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3774 name = si->lpp->vars[spill->spill];
3775 if(!is_zero(name->value)) {
3776 /* place spill directly after definition */
3777 if(get_nodes_block(spill->irn) == bb) {
3778 insert_spill(si, spill->irn, spill->irn, spill->irn);
3782 /* place spill at bb start */
3783 if(spill->reg_in > 0) {
3784 name = si->lpp->vars[spill->reg_in];
3785 if(!is_zero(name->value)) {
3786 insert_spill(si, spill->irn, spill->irn, bb);
3790 /* place spill after a remat */
3791 pset_insert_ptr(spills_to_do, spill->irn);
3794 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3797 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3798 op_t *op = get_irn_link(irn);
3800 if(be_is_Spill(irn)) continue;
3803 /* TODO fix this if we want to support remats with more than two nodes */
3804 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3805 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3807 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3810 if(pset_find_ptr(spills_to_do, irn)) {
3811 pset_remove_ptr(spills_to_do, irn);
3813 insert_spill(si, irn, irn, irn);
3819 assert(pset_count(spills_to_do) == 0);
3821 /* afterwards free data in block */
3822 del_pset(spills_to_do);
3826 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3828 ir_node *insert_pos = bb;
3830 const arch_env_t *arch_env = si->birg->main_env->arch_env;
3832 /* find last definition of arg value in block */
3837 defs = set_find_def(si->values, value);
3839 if(defs && defs->remats) {
3840 for(next = defs->remats; next; next = get_irn_link(next)) {
3841 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3842 last = sched_get_time_step(next);
3848 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3849 last = sched_get_time_step(value);
3853 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3855 spill = be_spill2(arch_env, is_Block(insert_pos)?value:insert_pos, insert_pos);
3861 phim_fixer(spill_ilp_t *si) {
3864 set_foreach(si->values, defs) {
3865 const ir_node *phi = defs->value;
3866 op_t *op = get_irn_link(phi);
3867 ir_node *phi_m = NULL;
3868 ir_node *next = defs->spills;
3871 if(!is_Phi(phi)) continue;
3874 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3878 next = get_irn_link(next);
3881 if(!phi_m) continue;
3883 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3884 ir_node *value = get_irn_n(phi, n);
3885 defs_t *val_defs = set_find_def(si->values, value);
3887 /* a spill of this value */
3892 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3893 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3895 if(!is_zero(name->value)) {
3896 spill = insert_mem_copy(si, pred, value);
3898 spill = val_defs->spills;
3901 spill = val_defs->spills;
3904 assert(spill && "no spill placed before PhiM");
3905 set_irn_n(phi_m, n, spill);
3911 walker_reload_placer(ir_node * bb, void * data) {
3912 spill_ilp_t *si = (spill_ilp_t*)data;
3914 spill_bb_t *spill_bb = get_irn_link(bb);
3916 /* reloads at end of block */
3917 if(spill_bb->reloads) {
3920 set_foreach(spill_bb->reloads, keyval) {
3921 ir_node *irn = (ir_node*)keyval->key;
3922 ilp_var_t reload = PTR_TO_INT(keyval->val);
3925 name = si->lpp->vars[reload];
3926 if(!is_zero(name->value)) {
3928 ir_node *insert_pos = bb;
3929 ir_node *prev = sched_block_last_noncf(si, bb);
3930 op_t *prev_op = get_irn_link(prev);
3932 while(be_is_Spill(prev)) {
3933 prev = sched_prev(prev);
3936 prev_op = get_irn_link(prev);
3938 /* insert reload before pre-remats */
3939 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3940 && prev_op->is_remat && prev_op->attr.remat.pre) {
3944 prev = sched_prev(prev);
3945 } while(be_is_Spill(prev));
3947 prev_op = get_irn_link(prev);
3951 reload = insert_reload(si, irn, insert_pos);
3953 if(opt_keep_alive & KEEPALIVE_RELOADS)
3954 pset_insert_ptr(si->spills, reload);
3959 /* walk and insert more reloads and collect remats */
3960 sched_foreach_reverse(bb, irn) {
3961 op_t *op = get_irn_link(irn);
3963 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3964 if(is_Phi(irn)) break;
3967 if(get_irn_mode(irn) != mode_T) {
3968 insert_remat(si, irn);
3973 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3974 ir_node *arg = get_irn_n(irn, n);
3976 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3979 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3980 if(!is_zero(name->value)) {
3982 ir_node *insert_pos = irn;
3983 ir_node *prev = sched_prev(insert_pos);
3986 while(be_is_Spill(prev)) {
3987 prev = sched_prev(prev);
3990 prev_op = get_irn_link(prev);
3992 /* insert reload before pre-remats */
3993 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3994 && prev_op->is_remat && prev_op->attr.remat.pre) {
3998 prev = sched_prev(prev);
3999 } while(be_is_Spill(prev));
4001 prev_op = get_irn_link(prev);
4005 reload = insert_reload(si, arg, insert_pos);
4007 assert(reload && "no reload returned");
4008 set_irn_n(irn, n, reload);
4010 if(opt_keep_alive & KEEPALIVE_RELOADS)
4011 pset_insert_ptr(si->spills, reload);
4018 del_set(spill_bb->ilp);
4019 if(spill_bb->reloads) del_set(spill_bb->reloads);
4023 walker_collect_used(ir_node * irn, void * data)
4025 bitset_t *used = data;
4027 bitset_set(used, get_irn_idx(irn));
4030 struct kill_helper {
4036 walker_kill_unused(ir_node * bb, void * data)
4038 struct kill_helper *kh = data;
4039 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4043 for(irn=sched_first(bb); !sched_is_end(irn);) {
4044 ir_node *next = sched_next(irn);
4047 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4048 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4049 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(kh->si, bb)));
4051 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4057 set_nodes_block(irn, bad);
4058 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4059 set_irn_n(irn, n, bad);
4066 #ifndef SCHEDULE_PHIM
4068 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4071 ir_node *bad = get_irg_bad(si->birg->irg);
4074 pset_foreach(si->phims, phi) {
4075 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4077 set_nodes_block(phi, bad);
4078 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4079 set_irn_n(phi, n, bad);
4087 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4089 struct kill_helper kh;
4091 kh.used = bitset_malloc(get_irg_last_idx(si->birg->irg));
4094 irg_walk_graph(si->birg->irg, walker_collect_used, NULL, kh.used);
4095 #ifndef SCHEDULE_PHIM
4096 kill_unused_phims(si, &kh);
4098 irg_block_walk_graph(si->birg->irg, walker_kill_unused, NULL, &kh);
4100 bitset_free(kh.used);
4104 print_irn_pset(pset * p)
4108 pset_foreach(p, irn) {
4109 ir_printf("%+F\n", irn);
4114 dump_phi_class(spill_ilp_t *si, ir_node **phiclass, const char * file)
4116 FILE *f = fopen(file, "w");
4118 interference_t *interference;
4121 set_break(si->interferences);
4123 ir_fprintf(f, "digraph phiclass {\n");
4125 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4128 ir_fprintf(f, " %F%N [shape=box]\n", irn, irn);
4131 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4138 for (n = get_irn_arity(irn) - 1; n >= 0; --n) {
4139 ir_node *arg = get_irn_n(irn, n);
4141 ir_fprintf(f, " %F%N -> %F%N\n", irn, irn, arg, arg);
4145 set_foreach(si->interferences, interference) {
4146 const ir_node *a = interference->a;
4147 const ir_node *b = interference->b;
4148 if (get_phi_class(si->pc, (ir_node *)a) == phiclass) {
4149 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n", a, a, b, b);
4158 rewire_uses(spill_ilp_t * si)
4161 ir_nodeset_t ignore;
4163 ir_nodeset_init(&ignore);
4164 ir_nodeset_insert(&ignore, get_irg_end(si->birg->irg));
4166 /* then fix uses of spills */
4167 set_foreach(si->values, defs) {
4170 const ir_node *next = defs->remats;
4173 reloads = pset_new_ptr_default();
4176 if(be_is_Reload(next)) {
4177 pset_insert_ptr(reloads, next);
4181 next = get_irn_link(next);
4184 spills = get_spills_for_value(si, defs->value);
4185 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));
4186 if(pset_count(spills) > 1) {
4187 be_ssa_construction_env_t senv;
4189 //assert(pset_count(reloads) > 0);
4190 // print_irn_pset(spills);
4191 // print_irn_pset(reloads);
4193 be_ssa_construction_init(&senv, si->birg);
4194 be_ssa_construction_set_ignore_uses(&senv, &ignore);
4195 pset_foreach(spills, node) {
4196 be_ssa_construction_add_copy(&senv, node);
4198 pset_foreach(spills, node) {
4199 be_ssa_construction_fix_users(&senv, node);
4201 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4202 pset_foreach(spills, node) {
4203 be_liveness_update(si->lv, node);
4205 be_ssa_construction_destroy(&senv);
4212 /* first fix uses of remats and reloads */
4213 set_foreach(si->values, defs) {
4214 const ir_node *next = defs->remats;
4218 be_ssa_construction_env_t senv;
4220 be_ssa_construction_init(&senv, si->birg);
4222 if(sched_is_scheduled(defs->value)) {
4223 be_ssa_construction_add_copy(&senv, (ir_node*) defs->value);
4227 next = defs->remats;
4229 be_ssa_construction_add_copy(&senv, (ir_node*) next);
4230 next = get_irn_link(next);
4233 if(sched_is_scheduled(defs->value)) {
4234 be_ssa_construction_fix_users(&senv, (ir_node*) defs->value);
4237 next = defs->remats;
4239 be_ssa_construction_fix_users(&senv, (ir_node*) next);
4240 next = get_irn_link(next);
4243 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4244 if(sched_is_scheduled(defs->value)) {
4245 be_liveness_update(si->lv, (ir_node*) defs->value);
4248 next = defs->remats;
4250 be_liveness_update(si->lv, (ir_node*) next);
4251 next = get_irn_link(next);
4254 be_ssa_construction_destroy(&senv);
4258 ir_nodeset_destroy(&ignore);
4259 // remove_unused_defs(si);
4264 writeback_results(spill_ilp_t * si)
4266 /* walk through the graph and collect all spills, reloads and remats for a value */
4268 si->values = new_set(cmp_defs, 4096);
4270 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4271 delete_unnecessary_remats(si);
4272 si->m_unknown = new_r_Unknown(si->birg->irg, mode_M);
4273 irg_block_walk_graph(si->birg->irg, walker_spill_placer, NULL, si);
4274 irg_block_walk_graph(si->birg->irg, walker_reload_placer, NULL, si);
4276 insert_memoperands(si);
4279 /* clean the remat info! there are still back-edges leading there! */
4280 clean_remat_info(si);
4284 connect_all_spills_with_keep(si);
4286 del_set(si->values);
4290 get_n_regs(spill_ilp_t * si)
4292 int arch_n_regs = arch_register_class_n_regs(si->cls);
4294 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4295 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4297 arch_put_non_ignore_regs(si->birg->main_env->arch_env, si->cls, arch_regs);
4298 be_abi_put_ignore_regs(si->birg->abi, si->cls, abi_regs);
4300 bitset_andnot(arch_regs, abi_regs);
4301 arch_n_regs = bitset_popcnt(arch_regs);
4303 bitset_free(arch_regs);
4304 bitset_free(abi_regs);
4306 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4311 walker_reload_mover(ir_node * bb, void * data)
4313 spill_ilp_t *si = data;
4316 sched_foreach(bb, tmp) {
4317 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4318 ir_node *reload = tmp;
4321 /* move reload upwards */
4323 int pressure = (int)get_irn_link(reload);
4324 if(pressure < si->n_regs) {
4325 irn = sched_prev(reload);
4326 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4327 sched_remove(reload);
4328 pressure = (int)get_irn_link(irn);
4330 while(pressure < si->n_regs) {
4331 if( sched_is_end(irn) ||
4332 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4333 /* do not move reload before its spill */
4334 (irn == be_get_Reload_mem(reload)) ||
4335 /* do not move before phi */
4338 set_irn_link(irn, INT_TO_PTR(pressure+1));
4339 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4340 irn = sched_prev(irn);
4342 pressure = (int)get_irn_link(irn);
4345 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4346 sched_put_after(irn, reload);
4353 move_reloads_upward(spill_ilp_t * si)
4355 irg_block_walk_graph(si->birg->irg, walker_reload_mover, NULL, si);
4360 * Walk all irg blocks and check for interfering spills inside of phi classes
4363 luke_meminterferencechecker(ir_node * bb, void * data)
4365 spill_ilp_t *si = (spill_ilp_t*)data;
4368 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4369 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4371 if (! be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T))
4374 /* a is only interesting if it is in my register class and if it is inside a phi class */
4375 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
4376 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)) {
4377 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4379 if (! be_is_Spill(b) && (! is_Phi(b) || get_irn_mode(b) != mode_T))
4382 /* a and b are only interesting if they are in the same phi class */
4383 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
4384 if (values_interfere_in_block(si, bb, a, b)) {
4385 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4394 verify_phiclasses(spill_ilp_t * si)
4396 /* analyze phi classes */
4397 phi_class_free(si->pc);
4398 si->pc = phi_class_new_from_irg(si->birg->irg, 0);
4400 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4401 irg_block_walk_graph(si->birg->irg, luke_meminterferencechecker, NULL, si);
4405 be_spill_remat(be_irg_t *birg, const arch_register_class_t *cls)
4408 char problem_name[256];
4409 char dump_suffix[256];
4410 char dump_suffix2[256];
4411 struct obstack obst;
4413 ir_graph *irg = be_get_birg_irg(birg);
4415 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", irg, cls->name);
4416 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", cls->name);
4417 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", cls->name);
4419 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4420 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4422 if(opt_verify & VERIFY_DOMINANCE)
4423 be_check_dominance(irg);
4425 be_assure_dom_front(birg);
4426 be_assure_liveness(birg);
4428 obstack_init(&obst);
4432 si.lpp = new_lpp(problem_name, lpp_minimize);
4433 si.remat_info = new_set(cmp_remat_info, 4096);
4434 si.interferences = new_set(cmp_interference, 32);
4435 si.memoperands = new_set(cmp_memoperands, 128);
4436 si.all_possible_remats = pset_new_ptr_default();
4437 si.spills = pset_new_ptr_default();
4438 si.inverse_ops = pset_new_ptr_default();
4441 si.n_regs = get_n_regs(&si);
4443 set_irg_link(irg, &si);
4446 /* compute phi classes */
4447 // phi_class_compute(irg);
4449 if(opt_dump_flags & DUMP_STATS)
4450 be_analyze_regpressure(birg, cls, "-pre");
4452 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4453 irg_block_walk_graph(irg, luke_initializer, NULL, &si);
4456 /* collect remats */
4457 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4458 irg_walk_graph(irg, walker_remat_collector, NULL, &si);
4461 /* insert possible remats */
4462 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4463 irg_block_walk_graph(irg, walker_remat_insertor, NULL, &si);
4464 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4466 if(opt_keep_alive & KEEPALIVE_REMATS) {
4467 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4468 connect_all_remats_with_keep(&si);
4469 /* dump graph with inserted remats */
4470 dump_graph_with_remats(irg, dump_suffix);
4473 /* insert copies for phi arguments not in my regclass */
4474 irg_walk_graph(irg, walker_regclass_copy_insertor, NULL, &si);
4476 /* recompute liveness */
4477 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4478 be_liveness_recompute(si.lv);
4481 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4482 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4483 irg_block_walk_graph(irg, luke_endwalker, NULL, &si);
4485 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4486 irg_block_walk_graph(irg, luke_blockwalker, NULL, &si);
4488 si.pc = phi_class_new_from_irg(birg->irg, 0);
4489 if (opt_memcopies) {
4490 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4491 memcopyhandler(&si);
4494 if (opt_dump_flags & DUMP_PROBLEM) {
4496 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4497 if ((f = fopen(buf, "wt")) != NULL) {
4498 lpp_dump_plain(si.lpp, f);
4503 if (opt_dump_flags & DUMP_MPS) {
4506 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4507 if ((f = fopen(buf, "wt")) != NULL) {
4508 mps_write_mps(si.lpp, s_mps_fixed, f);
4512 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4513 if ((f = fopen(buf, "wt")) != NULL) {
4514 mps_write_mst(si.lpp, s_mps_fixed, f);
4519 lpp_check_startvals(si.lpp);
4522 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4523 lpp_set_time_limit(si.lpp, opt_timeout);
4526 lpp_set_log(si.lpp, stdout);
4529 lpp_solve_cplex(si.lpp);
4531 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4533 assert(lpp_is_sol_valid(si.lpp)
4534 && "solution of ILP must be valid");
4536 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));
4538 if(opt_dump_flags & DUMP_SOLUTION) {
4542 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4543 if ((f = fopen(buf, "wt")) != NULL) {
4545 for (i = 0; i < si.lpp->var_next; ++i) {
4546 lpp_name_t *name = si.lpp->vars[i];
4547 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4553 #ifndef SCHEDULE_PHIM
4554 si.phims = pset_new_ptr_default();
4556 writeback_results(&si);
4561 kill_all_unused_values_in_schedule(&si);
4563 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4567 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4568 be_dump(irg, "-spills-placed", dump_ir_block_graph);
4570 // move reloads upwards
4571 be_liveness_recompute(si.lv);
4572 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4573 move_reloads_upward(&si);
4576 verify_phiclasses(&si);
4579 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4581 if(opt_dump_flags & DUMP_PRESSURE)
4582 dump_pressure_graph(&si, dump_suffix2);
4584 if(opt_dump_flags & DUMP_STATS)
4585 be_analyze_regpressure(birg, cls, "-post");
4587 if(opt_verify & VERIFY_DOMINANCE)
4588 be_check_dominance(irg);
4591 del_set(si.interferences);
4592 del_pset(si.inverse_ops);
4593 del_pset(si.all_possible_remats);
4594 del_set(si.memoperands);
4595 del_pset(si.spills);
4597 phi_class_free(si.pc);
4598 obstack_free(&obst, NULL);
4599 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4602 void be_init_spillremat(void)
4604 static be_spiller_t remat_spiller = {
4607 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
4608 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
4609 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
4610 lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat");
4612 be_register_spiller("remat", &remat_spiller);
4613 lc_opt_add_table(remat_grp, options);
4616 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat);
4618 #else /* WITH_ILP */
4620 static __attribute__((unused))
4621 void only_that_you_can_compile_without_WITH_ILP_defined(void)
4625 #endif /* WITH_ILP */