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
49 #include "irnodeset.h"
51 #include "iredges_t.h"
54 #include "irbackedge_t.h"
55 #include "irprofile.h"
59 #include <lpp/lpp_net.h>
60 #include <lpp/lpp_cplex.h>
65 #include "besched_t.h"
66 #include "bessaconstr.h"
68 #include "beintlive_t.h"
72 #include "bespillremat.h"
74 #include "bepressurestat.h"
75 #include "bespilloptions.h"
76 #include "bechordal_t.h"
80 #include "lc_opts_enum.h"
82 #define DUMP_PROBLEM 1
84 #define DUMP_SOLUTION 4
86 #define DUMP_PRESSURE 16
88 #define KEEPALIVE_REMATS 1
89 #define KEEPALIVE_SPILLS 2
90 #define KEEPALIVE_RELOADS 4
92 #define VERIFY_MEMINTERF 1
93 #define VERIFY_DOMINANCE 2
96 #define REMATS_BRIGGS 1
97 #define REMATS_NOINVERSE 2
100 static unsigned opt_dump_flags = 0;
101 static int opt_log = 0;
102 static unsigned opt_keep_alive = 0;
103 static int opt_goodwin = 1;
104 static int opt_memcopies = 1;
105 static int opt_memoperands = 1;
106 static int opt_verify = VERIFY_MEMINTERF;
107 static unsigned opt_remats = REMATS_ALL;
108 static int opt_repair_schedule = 0;
109 static int opt_no_enlarge_liveness = 0;
110 static int opt_remat_while_live = 1;
111 static int opt_timeout = 300;
112 static double opt_cost_reload = 8.0;
113 static double opt_cost_memoperand = 7.0;
114 static double opt_cost_spill = 15.0;
115 static double opt_cost_remat = 1.0;
118 static const lc_opt_enum_mask_items_t dump_items[] = {
119 { "problem", DUMP_PROBLEM },
121 { "solution", DUMP_SOLUTION },
122 { "stats", DUMP_STATS },
123 { "pressure", DUMP_PRESSURE },
127 static lc_opt_enum_mask_var_t dump_var = {
128 &opt_dump_flags, dump_items
131 static const lc_opt_enum_mask_items_t keepalive_items[] = {
132 { "remats", KEEPALIVE_REMATS },
133 { "spills", KEEPALIVE_SPILLS },
134 { "reloads", KEEPALIVE_RELOADS },
138 static lc_opt_enum_mask_var_t keep_alive_var = {
139 &opt_keep_alive, keepalive_items
142 static const lc_opt_enum_mask_items_t remats_items[] = {
143 { "none", REMATS_NONE },
144 { "briggs", REMATS_BRIGGS },
145 { "noinverse", REMATS_NOINVERSE },
146 { "all", REMATS_ALL },
150 static lc_opt_enum_mask_var_t remats_var = {
151 &opt_remats, remats_items
154 static const lc_opt_table_entry_t options[] = {
155 LC_OPT_ENT_ENUM_MASK("keepalive", "keep alive inserted nodes", &keep_alive_var),
157 LC_OPT_ENT_BOOL ("goodwin", "activate goodwin reduction", &opt_goodwin),
158 LC_OPT_ENT_BOOL ("memcopies", "activate memcopy handling", &opt_memcopies),
159 LC_OPT_ENT_BOOL ("memoperands", "activate memoperands", &opt_memoperands),
160 LC_OPT_ENT_ENUM_INT ("remats", "type of remats to insert", &remats_var),
161 LC_OPT_ENT_BOOL ("repair_schedule", "repair the schedule by rematting once used nodes",&opt_repair_schedule),
162 LC_OPT_ENT_BOOL ("no_enlage_liveness", "do not enlarge liveness of operands of remats",&opt_no_enlarge_liveness),
163 LC_OPT_ENT_BOOL ("remat_while_live", "only remat where rematted value was live", &opt_remat_while_live),
165 LC_OPT_ENT_ENUM_MASK("dump", "dump problem, solution or statistical data", &dump_var),
166 LC_OPT_ENT_BOOL ("log", "activate the lpp log", &opt_log),
167 LC_OPT_ENT_INT ("timeout", "ILP solver timeout", &opt_timeout),
169 LC_OPT_ENT_DBL ("cost_reload", "cost of a reload", &opt_cost_reload),
170 LC_OPT_ENT_DBL ("cost_memoperand", "cost of a memory operand", &opt_cost_memoperand),
171 LC_OPT_ENT_DBL ("cost_spill", "cost of a spill instruction", &opt_cost_spill),
172 LC_OPT_ENT_DBL ("cost_remat", "cost of a rematerialization", &opt_cost_remat),
176 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
177 //#define SCHEDULE_PHIM /* insert phim nodes into schedule */
180 //#define SOLVE_LOCAL
181 #define LPP_SERVER "i44pc52"
182 #define LPP_SOLVER "cplex"
185 #define MAX_PATHS INT_MAX
188 typedef struct _spill_ilp_t {
189 const arch_register_class_t *cls;
194 struct obstack *obst;
196 pset *all_possible_remats;
199 set *values; /**< for collecting all definitions of values before running ssa-construction */
205 #ifndef SCHEDULE_PHIM
208 DEBUG_ONLY(firm_dbg_module_t * dbg);
211 typedef int ilp_var_t;
212 typedef int ilp_cst_t;
214 typedef struct _spill_bb_t {
219 typedef struct _remat_t {
220 const ir_node *op; /**< for copy_irn */
221 const ir_node *value; /**< the value which is being recomputed by this remat */
222 const ir_node *proj; /**< not NULL if the above op produces a tuple */
223 int cost; /**< cost of this remat */
224 int inverse; /**< nonzero if this is an inverse remat */
228 * Data to be attached to each IR node. For remats this contains the ilp_var
229 * for this remat and for normal ops this contains the ilp_vars for
230 * reloading each operand
232 typedef struct _op_t {
237 const remat_t *remat; /** the remat this op belongs to */
238 int pre; /** 1, if this is a pressure-increasing remat */
242 ir_node *op; /** the operation this live range belongs to */
251 typedef struct _defs_t {
252 const ir_node *value;
253 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
254 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
257 typedef struct _remat_info_t {
258 const ir_node *irn; /**< the irn to which these remats belong */
259 pset *remats; /**< possible remats for this value */
260 pset *remats_by_operand; /**< remats with this value as operand */
263 typedef struct _keyval_t {
268 typedef struct _spill_t {
277 typedef struct _memoperand_t {
278 ir_node *irn; /**< the irn */
279 unsigned int pos; /**< the position of the argument */
280 ilp_var_t ilp; /**< the ilp var for this memory operand */
284 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
286 return arch_irn_consider_in_reg_alloc(si->cls, irn);
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(s,i) for((i)=next_pre_remat((s)); (i); (i)=next_pre_remat((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;
456 static double get_cost(const ir_node *irn)
458 if(be_is_Spill(irn)) {
459 return opt_cost_spill;
460 } else if(be_is_Reload(irn)){
461 return opt_cost_reload;
463 return arch_get_op_estimated_cost(irn);
468 * Checks, whether node and its operands have suitable reg classes
471 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
474 int remat = (arch_irn_get_flags(irn) & arch_irn_flags_rematerializable) != 0;
478 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
481 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
482 ir_node *op = get_irn_n(irn, n);
483 remat &= has_reg_class(si, op) || arch_irn_get_flags(op) & arch_irn_flags_ignore || is_NoMem(op);
486 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
493 * Try to create a remat from @p op with destination value @p dest_value
495 static INLINE remat_t *
496 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
498 remat_t *remat = NULL;
500 // if(!mode_is_datab(get_irn_mode(dest_value)))
503 if(dest_value == op) {
504 const ir_node *proj = NULL;
506 if(is_Proj(dest_value)) {
507 op = get_Proj_pred(op);
511 if(!is_rematerializable(si, op))
514 remat = obstack_alloc(si->obst, sizeof(*remat));
516 remat->cost = (int)get_cost(op);
517 remat->value = dest_value;
521 arch_inverse_t inverse;
524 /* get the index of the operand we want to retrieve by the inverse op */
525 for (n = get_irn_arity(op)-1; n>=0; --n) {
526 ir_node *arg = get_irn_n(op, n);
528 if(arg == dest_value) break;
532 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
534 /* else ask the backend to give an inverse op */
535 if(arch_get_inverse(op, n, &inverse, si->obst)) {
538 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
540 assert(inverse.n > 0 && "inverse op should have at least one node");
542 for(i=inverse.n-1; i>=0; --i) {
543 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
547 remat = obstack_alloc(si->obst, sizeof(*remat));
548 remat->op = inverse.nodes[0];
549 remat->cost = inverse.costs;
550 remat->value = dest_value;
551 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
554 // Matze: commented this out, this doesn't seem to be true if
555 // the inverse is a simple operation with only 1 result...
556 //assert(is_Proj(remat->proj));
558 assert(0 && "I can not handle remats with more than 2 nodes");
565 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
567 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
575 add_remat(const spill_ilp_t * si, const remat_t * remat)
577 remat_info_t *remat_info,
582 assert(remat->value);
584 query.irn = remat->value;
586 query.remats_by_operand = NULL;
587 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
589 if(remat_info->remats == NULL) {
590 remat_info->remats = new_pset(cmp_remat, 4096);
592 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
594 /* insert the remat into the remats_be_operand set of each argument of the remat op */
595 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
596 ir_node *arg = get_irn_n(remat->op, n);
600 query.remats_by_operand = NULL;
601 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
603 if(remat_info->remats_by_operand == NULL) {
604 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
606 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
611 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
613 const ir_edge_t *edge = get_irn_out_edge_first(irn);
617 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
620 edge = get_irn_out_edge_next(irn, edge);
627 get_irn_n_nonignore_args(const spill_ilp_t * si, const ir_node * irn)
633 irn = get_Proj_pred(irn);
635 for(n=get_irn_arity(irn)-1; n>=0; --n) {
636 const ir_node *arg = get_irn_n(irn, n);
638 if(has_reg_class(si, arg)) ++ret;
645 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
650 if( has_reg_class(si, op)
651 && (opt_repair_schedule || get_irn_n_nonremat_edges(si, op) > 1)
652 && (opt_remats != REMATS_BRIGGS || get_irn_n_nonignore_args(si, op) == 0)
654 remat = get_remat_from_op(si, op, op);
656 add_remat(si, remat);
660 if(opt_remats == REMATS_ALL) {
661 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
663 for (n = get_irn_arity(op)-1; n>=0; --n) {
664 ir_node *arg = get_irn_n(op, n);
666 if(has_reg_class(si, arg)) {
667 /* try to get an inverse remat */
668 remat = get_remat_from_op(si, arg, op);
670 add_remat(si, remat);
678 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
681 ir_node *def_block = get_nodes_block(val);
688 /* if pos is at end of a basic block */
690 ret = (pos == def_block || block_dominates(def_block, pos));
691 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
695 /* else if this is a normal operation */
696 block = get_nodes_block(pos);
697 if(block == def_block) {
698 if(!sched_is_scheduled(val)) return 1;
700 ret = sched_comes_after(val, pos);
701 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
705 ret = block_dominates(def_block, block);
706 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
710 static INLINE ir_node *sched_block_last_noncf(const ir_node * bb)
712 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, NULL);
716 * Returns first non-Phi node of block @p bb
718 static INLINE ir_node *
719 sched_block_first_nonphi(const ir_node * bb)
721 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
725 sched_skip_proj_predicator(const ir_node * irn, void * data)
728 return (is_Proj(irn));
731 static INLINE ir_node *
732 sched_next_nonproj(const ir_node * irn, int forward)
734 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
738 * Returns next operation node (non-Proj) after @p irn
739 * or the basic block of this node
741 static INLINE ir_node *
742 sched_next_op(const ir_node * irn)
744 ir_node *next = sched_next(irn);
749 return sched_next_nonproj(next, 1);
753 * Returns previous operation node (non-Proj) before @p irn
754 * or the basic block of this node
756 static INLINE ir_node *
757 sched_prev_op(const ir_node * irn)
759 ir_node *prev = sched_prev(irn);
764 return sched_next_nonproj(prev, 0);
768 sched_put_after(ir_node * insert, ir_node * irn)
770 if(is_Block(insert)) {
771 insert = sched_block_first_nonphi(insert);
773 insert = sched_next_op(insert);
776 sched_add_before(insert, irn);
779 static void sched_put_before(ir_node * insert, ir_node * irn)
781 if(is_Block(insert)) {
782 insert = sched_block_last_noncf(insert);
784 insert = sched_next_nonproj(insert, 0);
785 insert = sched_prev(insert);
788 sched_add_after(insert, irn);
792 next_post_remat(const ir_node * irn)
798 next = sched_block_first_nonphi(irn);
800 next = sched_next_op(irn);
803 if(sched_is_end(next))
806 op = get_irn_link(next);
807 if(op->is_remat && !op->attr.remat.pre) {
815 static ir_node *next_pre_remat(const ir_node * irn)
821 ret = sched_block_last_noncf(irn);
822 ret = sched_next(ret);
823 ret = sched_prev_op(ret);
825 ret = sched_prev_op(irn);
828 if(sched_is_end(ret) || is_Phi(ret))
831 op = (op_t*)get_irn_link(ret);
832 if(op->is_remat && op->attr.remat.pre) {
840 * Tells you whether a @p remat can be placed before the irn @p pos
843 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
845 const ir_node *op = remat->op;
851 prev = sched_block_last_noncf(pos);
852 prev = sched_next_nonproj(prev, 0);
854 prev = sched_prev_op(pos);
856 /* do not remat if the rematted value is defined immediately before this op */
857 if(prev == remat->op) {
862 /* this should be just fine, the following OP will be using this value, right? */
864 /* only remat AFTER the real definition of a value (?) */
865 if(!value_is_defined_before(si, pos, remat->value)) {
866 // ir_fprintf(stderr, "error(not defined)");
871 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
872 const ir_node *arg = get_irn_n(op, n);
874 if(opt_no_enlarge_liveness) {
875 if(has_reg_class(si, arg) && live) {
876 res &= pset_find_ptr((pset*)live, arg)?1:0;
878 res &= value_is_defined_before(si, pos, arg);
881 res &= value_is_defined_before(si, pos, arg);
889 * Tells you whether a @p remat can be placed after the irn @p pos
892 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
895 pos = sched_block_first_nonphi(pos);
897 pos = sched_next_op(pos);
900 /* only remat AFTER the real definition of a value (?) */
901 if(!value_is_defined_before(si, pos, remat->value)) {
905 return can_remat_before(si, remat, pos, live);
909 * Collect potetially rematerializable OPs
912 walker_remat_collector(ir_node * irn, void * data)
914 spill_ilp_t *si = data;
916 if(!is_Block(irn) && !is_Phi(irn)) {
917 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
918 get_remats_from_op(si, irn);
923 * Inserts a copy of @p irn before @p pos
926 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
931 bb = is_Block(pos)?pos:get_nodes_block(pos);
932 copy = exact_copy(irn);
934 set_phi_class(si->pc, copy, NULL);
935 set_nodes_block(copy, bb);
936 sched_put_before(pos, copy);
942 * Inserts a copy of @p irn after @p pos
945 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
950 bb = is_Block(pos)?pos:get_nodes_block(pos);
951 copy = exact_copy(irn);
953 set_phi_class(si->pc, copy, NULL);
954 set_nodes_block(copy, bb);
955 sched_put_after(pos, copy);
961 insert_remat_after(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
965 if(can_remat_after(si, remat, pos, live)) {
970 DBG((si->dbg, LEVEL_3, "\t >inserting remat2 %+F\n", remat->op));
972 copy = insert_copy_after(si, remat->op, pos);
974 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
975 op = obstack_alloc(si->obst, sizeof(*op));
977 op->attr.remat.remat = remat;
978 op->attr.remat.pre = 0;
979 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
981 set_irn_link(copy, op);
982 pset_insert_ptr(si->all_possible_remats, copy);
984 proj_copy = insert_copy_after(si, remat->proj, copy);
985 set_irn_n(proj_copy, 0, copy);
986 set_irn_link(proj_copy, op);
987 pset_insert_ptr(si->all_possible_remats, proj_copy);
999 insert_remat_before(spill_ilp_t * si, const remat_t * remat, ir_node * pos, const pset * live)
1003 if(can_remat_before(si, remat, pos, live)) {
1008 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
1010 copy = insert_copy_before(si, remat->op, pos);
1012 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
1013 op = obstack_alloc(si->obst, sizeof(*op));
1015 op->attr.remat.remat = remat;
1016 op->attr.remat.pre = 1;
1017 op->attr.remat.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos), 0.0);
1019 set_irn_link(copy, op);
1020 pset_insert_ptr(si->all_possible_remats, copy);
1022 proj_copy = insert_copy_after(si, remat->proj, copy);
1023 set_irn_n(proj_copy, 0, copy);
1024 set_irn_link(proj_copy, op);
1025 pset_insert_ptr(si->all_possible_remats, proj_copy);
1037 get_block_n_succs(const ir_node *block) {
1038 const ir_edge_t *edge;
1040 assert(edges_activated(current_ir_graph));
1042 edge = get_block_succ_first(block);
1046 edge = get_block_succ_next(block, edge);
1047 return edge ? 2 : 1;
1051 is_start_block(const ir_node * bb)
1053 return get_irg_start_block(get_irn_irg(bb)) == bb;
1057 is_merge_edge(const ir_node * bb)
1059 if(is_start_block(bb))
1063 return get_block_n_succs(bb) == 1;
1069 is_diverge_edge(const ir_node * bb)
1071 if(is_start_block(bb))
1075 return get_Block_n_cfgpreds(bb) == 1;
1081 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1086 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1087 irn = be_lv_get_irn(si->lv, bb, i);
1089 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1090 pset_insert_ptr(live, irn);
1094 irn = sched_last(bb);
1096 /* all values eaten by control flow operations are also live until the end of the block */
1097 sched_foreach_reverse(bb, irn) {
1100 if (!sched_skip_cf_predicator(irn, NULL)) break;
1102 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1103 ir_node *arg = get_irn_n(irn,i);
1105 if(has_reg_class(si, arg)) {
1106 pset_insert_ptr(live, arg);
1111 * find values that are used by remats at end of block
1112 * and insert them into live set
1114 foreach_pre_remat(bb, irn) {
1117 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1118 ir_node *remat_arg = get_irn_n(irn, n);
1120 if(!has_reg_class(si, remat_arg)) continue;
1122 /* if value is becoming live through use by remat */
1123 if(!pset_find_ptr(live, remat_arg)) {
1124 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, irn));
1126 pset_insert_ptr(live, remat_arg);
1133 walker_regclass_copy_insertor(ir_node * irn, void * data)
1135 spill_ilp_t *si = data;
1137 if(is_Phi(irn) && has_reg_class(si, irn)) {
1140 for(n=get_irn_arity(irn)-1; n>=0; --n) {
1141 ir_node *phi_arg = get_irn_n(irn, n);
1142 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
1144 if(!has_reg_class(si, phi_arg)) {
1145 ir_node *copy = be_new_Copy(si->cls, si->birg->irg, bb, phi_arg);
1146 ir_node *pos = sched_block_last_noncf(bb);
1147 op_t *op = obstack_alloc(si->obst, sizeof(*op));
1149 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
1150 sched_add_after(pos, copy);
1151 set_irn_n(irn, n, copy);
1154 op->attr.live_range.args.reloads = NULL;
1155 op->attr.live_range.ilp = ILP_UNDEF;
1156 set_irn_link(copy, op);
1163 * Insert (so far unused) remats into the irg to
1164 * recompute the potential liveness of all values
1167 walker_remat_insertor(ir_node * bb, void * data)
1169 spill_ilp_t *si = data;
1176 /* skip start block, no remats to do there */
1177 if(is_start_block(bb)) return;
1179 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
1181 live = pset_new_ptr_default();
1182 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1183 ir_node *value = be_lv_get_irn(si->lv, bb, i);
1185 /* add remats at end of block */
1186 if (has_reg_class(si, value)) {
1187 pset_insert_ptr(live, value);
1191 irn = sched_last(bb);
1192 while(!sched_is_end(irn)) {
1198 next = sched_prev(irn);
1200 /* delete defined value from live set */
1201 if(has_reg_class(si, irn)) {
1202 pset_remove_ptr(live, irn);
1205 if(is_Phi(irn) || is_Proj(irn)) {
1210 args = pset_new_ptr_default();
1211 used = pset_new_ptr_default();
1213 /* collect arguments of op and set args of op already live in epilog */
1214 for (n = get_irn_arity(irn)-1; n>=0; --n) {
1215 ir_node *arg = get_irn_n(irn, n);
1217 pset_insert_ptr(args, arg);
1218 if(has_reg_class(si, arg)) {
1219 pset_insert_ptr(live, arg);
1220 pset_insert_ptr(used, arg);
1224 /* insert all possible remats before irn */
1225 pset_foreach(args, arg) {
1226 remat_info_t *remat_info,
1229 /* continue if the operand has the wrong reg class */
1230 if(!has_reg_class(si, arg))
1234 query.remats = NULL;
1235 query.remats_by_operand = NULL;
1236 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1242 if(remat_info->remats) {
1243 pset_foreach(remat_info->remats, remat) {
1244 ir_node *remat_irn = NULL;
1246 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1247 remat_irn = insert_remat_before(si, remat, irn, live);
1250 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1251 ir_node *remat_arg = get_irn_n(remat_irn, n);
1253 /* collect args of remats which are not args of op */
1254 if(has_reg_class(si, remat_arg) && !pset_find_ptr(args, remat_arg)) {
1255 pset_insert_ptr(used, remat_arg);
1263 /* do not place post remats after jumps */
1264 if (sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) {
1270 /* insert all possible remats after irn */
1271 post_remats = pset_new_ptr_default();
1272 pset_foreach(used, arg) {
1273 remat_info_t *remat_info,
1276 /* continue if the operand has the wrong reg class */
1277 if(!has_reg_class(si, arg))
1281 query.remats = NULL;
1282 query.remats_by_operand = NULL;
1283 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1289 if(remat_info->remats_by_operand) {
1290 pset_foreach(remat_info->remats_by_operand, remat) {
1291 /* do not insert remats producing the same value as one of the operands */
1292 if(!pset_find_ptr(args, remat->value)) {
1293 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1295 /* only remat values that can be used by real ops */
1296 if(!opt_remat_while_live || pset_find_ptr(live, remat->value)) {
1297 pset_insert_ptr(post_remats, remat);
1303 pset_foreach(post_remats, remat) {
1304 insert_remat_after(si, remat, irn, live);
1306 del_pset(post_remats);
1313 /* add remats at end if successor has multiple predecessors */
1314 if(is_merge_edge(bb)) {
1315 pset *live_out = pset_new_ptr_default();
1318 get_live_end(si, bb, live_out);
1320 /* add remats at end of block */
1321 pset_foreach(live_out, value) {
1322 remat_info_t *remat_info,
1326 query.remats = NULL;
1327 query.remats_by_operand = NULL;
1328 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1330 if(remat_info && remat_info->remats) {
1331 pset_foreach(remat_info->remats, remat) {
1332 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1334 insert_remat_before(si, remat, bb, live_out);
1341 if(is_diverge_edge(bb)) {
1342 pset *live_in = pset_new_ptr_default();
1345 be_lv_foreach(si->lv, bb, be_lv_state_in, i) {
1346 value = be_lv_get_irn(si->lv, bb, i);
1348 if(has_reg_class(si, value)) {
1349 pset_insert_ptr(live_in, value);
1352 /* add phis to live_in */
1353 sched_foreach(bb, value) {
1354 if(!is_Phi(value)) break;
1356 if(has_reg_class(si, value)) {
1357 pset_insert_ptr(live_in, value);
1361 /* add remat2s at beginning of block */
1362 post_remats = pset_new_ptr_default();
1363 pset_foreach(live_in, value) {
1364 remat_info_t *remat_info,
1368 query.remats = NULL;
1369 query.remats_by_operand = NULL;
1370 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1372 if(remat_info && remat_info->remats_by_operand) {
1373 pset_foreach(remat_info->remats_by_operand, remat) {
1374 DBG((si->dbg, LEVEL_4, "\t considering remat2 %+F at beginning of block %+F\n", remat->op, bb));
1376 /* put the remat here if all its args are available and result is still live */
1377 if(!opt_remat_while_live || pset_find_ptr(live_in, remat->value)) {
1378 pset_insert_ptr(post_remats, remat);
1383 pset_foreach(post_remats, remat) {
1384 insert_remat_after(si, remat, bb, live_in);
1386 del_pset(post_remats);
1392 can_be_copied(const ir_node * bb, const ir_node * irn)
1394 const ir_edge_t *edge = get_block_succ_first(bb);
1395 const ir_node *next_bb = edge->src;
1396 int pos = edge->pos;
1399 assert(is_merge_edge(bb));
1401 sched_foreach(next_bb, phi) {
1402 const ir_node *phi_arg;
1404 if(!is_Phi(phi)) break;
1406 phi_arg = get_irn_n(phi, pos);
1408 if(phi_arg == irn) {
1416 * Initialize additional node info
1419 luke_initializer(ir_node * bb, void * data)
1421 spill_ilp_t *si = (spill_ilp_t*)data;
1422 spill_bb_t *spill_bb;
1425 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
1426 set_irn_link(bb, spill_bb);
1428 sched_foreach(bb, irn) {
1431 op = obstack_alloc(si->obst, sizeof(*op));
1433 op->attr.live_range.ilp = ILP_UNDEF;
1436 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1437 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(irn));
1439 } else if(!is_Proj(irn)) {
1440 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1441 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
1443 op->attr.live_range.args.reloads = NULL;
1445 set_irn_link(irn, op);
1451 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1454 luke_endwalker(ir_node * bb, void * data)
1456 spill_ilp_t *si = (spill_ilp_t*)data;
1462 spill_bb_t *spill_bb = get_irn_link(bb);
1465 live = pset_new_ptr_default();
1466 use_end = pset_new_ptr_default();
1468 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
1469 irn = be_lv_get_irn(si->lv, bb, i);
1470 if (has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1471 pset_insert_ptr(live, irn);
1475 * find values that are used by remats at end of block
1476 * and insert them into live set
1478 foreach_pre_remat(bb, irn) {
1481 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1482 ir_node *remat_arg = get_irn_n(irn, n);
1484 if(has_reg_class(si, remat_arg)) {
1485 pset_insert_ptr(live, remat_arg);
1490 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1491 /* their reg_out must always be set */
1492 sched_foreach_reverse(bb, irn) {
1495 if (!sched_skip_cf_predicator(irn, si->birg->main_env->arch_env)) break;
1497 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1498 ir_node *irn_arg = get_irn_n(irn, n);
1500 if(has_reg_class(si, irn_arg)) {
1501 pset_insert_ptr(use_end, irn_arg);
1506 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1507 //cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1508 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1510 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1512 /* if this is a merge edge we can reload at the end of this block */
1513 if(is_merge_edge(bb)) {
1514 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1515 } else if(pset_count(use_end)){
1516 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1518 spill_bb->reloads = NULL;
1521 pset_foreach(live,irn) {
1525 int default_spilled;
1528 /* handle values used by control flow nodes later separately */
1529 if(pset_find_ptr(use_end, irn)) continue;
1532 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1534 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1536 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1537 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1538 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1540 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1541 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1543 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1544 /* by default spill value right after definition */
1545 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1546 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1548 if(is_merge_edge(bb)) {
1552 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1553 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), can_be_copied(bb, irn));
1554 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1556 /* reload <= mem_out */
1557 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1558 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1559 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1562 spill->reg_in = ILP_UNDEF;
1563 spill->mem_in = ILP_UNDEF;
1566 pset_foreach(use_end,irn) {
1570 ilp_cst_t end_use_req,
1573 int default_spilled;
1576 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1578 spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1580 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1581 spill->reg_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1583 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1584 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1586 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1587 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1588 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1590 /* reload for use be control flow op */
1591 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1592 reload = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_reload*execution_frequency(si, bb), 1.0);
1593 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1595 /* reload <= mem_out */
1596 rel_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1597 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1598 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1600 spill->reg_in = ILP_UNDEF;
1601 spill->mem_in = ILP_UNDEF;
1603 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1604 end_use_req = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1);
1605 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1614 * Find a remat of value @p value in the epilog of @p pos
1617 find_post_remat(const ir_node * value, const ir_node * pos)
1619 while((pos = next_post_remat(pos)) != NULL) {
1622 op = get_irn_link(pos);
1623 assert(op->is_remat && !op->attr.remat.pre);
1625 if(op->attr.remat.remat->value == value)
1626 return (ir_node*)pos;
1629 const ir_edge_t *edge;
1630 foreach_out_edge(pos, edge) {
1631 ir_node *proj = get_edge_src_irn(edge);
1632 assert(is_Proj(proj));
1643 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1645 spill_bb_t *spill_bb = get_irn_link(bb);
1649 int default_spilled;
1652 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1654 double spill_cost = is_Unknown(irn)?0.0001:opt_cost_spill*execution_frequency(si, bb);
1656 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1658 spill->reg_out = ILP_UNDEF;
1659 spill->reg_in = ILP_UNDEF;
1660 spill->mem_in = ILP_UNDEF;
1662 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1663 spill->mem_out = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1665 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1666 default_spilled = be_is_live_in(si->lv, bb, irn) || is_Phi(irn);
1667 spill->spill = lpp_add_var_default(si->lpp, buf, lpp_binary, spill_cost, !default_spilled);
1674 * Inserts ILP-constraints and variables for memory copying before the given position
1677 insert_mem_copy_position(spill_ilp_t * si, pset * live, const ir_node * block)
1679 const ir_node *succ;
1680 const ir_edge_t *edge;
1681 spill_bb_t *spill_bb = get_irn_link(block);
1690 assert(edges_activated(current_ir_graph));
1692 edge = get_block_succ_first(block);
1698 edge = get_block_succ_next(block, edge);
1699 /* next block can only contain phis, if this is a merge edge */
1702 ir_snprintf(buf, sizeof(buf), "copyreg_%N", block);
1703 copyreg = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
1705 ir_snprintf(buf, sizeof(buf), "check_copyreg_%N", block);
1706 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
1708 pset_foreach(live, tmp) {
1711 op_t *op = get_irn_link(irn);
1712 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1714 spill = set_find_spill(spill_bb->ilp, tmp);
1717 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1719 lpp_set_factor_fast(si->lpp, cst, copyreg, 1.0);
1721 sched_foreach(succ, phi) {
1722 const ir_node *to_copy;
1724 spill_t *to_copy_spill;
1725 op_t *phi_op = get_irn_link(phi);
1726 ilp_var_t reload = ILP_UNDEF;
1729 if(!is_Phi(phi)) break;
1730 if(!has_reg_class(si, phi)) continue;
1732 to_copy = get_irn_n(phi, pos);
1733 to_copy_op = get_irn_link(to_copy);
1735 to_copy_spill = set_find_spill(spill_bb->ilp, to_copy);
1736 assert(to_copy_spill);
1738 if(spill_bb->reloads) {
1739 keyval_t *keyval = set_find_keyval(spill_bb->reloads, to_copy);
1742 reload = PTR_TO_INT(keyval->val);
1746 ir_snprintf(buf, sizeof(buf), "req_copy_%N_%N_%N", block, phi, to_copy);
1747 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1749 /* copy - reg_out - reload - remat - live_range <= 0 */
1750 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1751 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1752 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1753 lpp_set_factor_fast(si->lpp, cst, to_copy_op->attr.live_range.ilp, -1.0);
1754 foreach_pre_remat(block, tmp) {
1755 op_t *remat_op = get_irn_link(tmp);
1756 if(remat_op->attr.remat.remat->value == to_copy) {
1757 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1761 ir_snprintf(buf, sizeof(buf), "copyreg_%N_%N_%N", block, phi, to_copy);
1762 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1764 /* copy - reg_out - copyreg <= 0 */
1765 lpp_set_factor_fast(si->lpp, cst, phi_op->attr.live_range.args.copies[pos], 1.0);
1766 lpp_set_factor_fast(si->lpp, cst, to_copy_spill->reg_out, -1.0);
1767 lpp_set_factor_fast(si->lpp, cst, copyreg, -1.0);
1773 * Walk all irg blocks and emit this ILP
1776 luke_blockwalker(ir_node * bb, void * data)
1778 spill_ilp_t *si = (spill_ilp_t*)data;
1783 spill_bb_t *spill_bb = get_irn_link(bb);
1786 pset *defs = pset_new_ptr_default();
1788 live = pset_new_ptr_default();
1790 /****************************************
1791 * B A S I C B L O C K E N D
1792 ***************************************/
1795 /* init live values at end of block */
1796 get_live_end(si, bb, live);
1798 pset_foreach(live, irn) {
1800 ilp_var_t reload = ILP_UNDEF;
1802 spill = set_find_spill(spill_bb->ilp, irn);
1805 if(spill_bb->reloads) {
1806 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1809 reload = PTR_TO_INT(keyval->val);
1813 op = get_irn_link(irn);
1814 assert(!op->is_remat);
1816 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1817 op->attr.live_range.ilp = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
1818 op->attr.live_range.op = bb;
1820 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1821 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
1823 /* reg_out - reload - remat - live_range <= 0 */
1824 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1825 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1826 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1827 foreach_pre_remat(bb, tmp) {
1828 op_t *remat_op = get_irn_link(tmp);
1829 if(remat_op->attr.remat.remat->value == irn) {
1830 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1833 ir_snprintf(buf, sizeof(buf), "reg_out2_%N_%N", bb, irn);
1834 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
1836 /* value may only die at bb end if it is used for a mem copy */
1837 /* reg_out + \sum copy - reload - remat - live_range >= 0 */
1838 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1839 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1840 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1841 foreach_pre_remat(bb, tmp) {
1842 op_t *remat_op = get_irn_link(tmp);
1843 if(remat_op->attr.remat.remat->value == irn) {
1844 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1847 if(is_merge_edge(bb)) {
1848 const ir_edge_t *edge = get_block_succ_first(bb);
1849 const ir_node *next_bb = edge->src;
1850 int pos = edge->pos;
1853 sched_foreach(next_bb, phi) {
1854 const ir_node *phi_arg;
1856 if(!is_Phi(phi)) break;
1858 phi_arg = get_irn_n(phi, pos);
1860 if(phi_arg == irn) {
1861 op_t *phi_op = get_irn_link(phi);
1862 ilp_var_t copy = phi_op->attr.live_range.args.copies[pos];
1864 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
1871 insert_mem_copy_position(si, live, bb);
1874 * assure the remat args are available
1876 foreach_pre_remat(bb, tmp) {
1877 op_t *remat_op = get_irn_link(tmp);
1880 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1881 ir_node *remat_arg = get_irn_n(tmp, n);
1882 op_t *arg_op = get_irn_link(remat_arg);
1884 if(!has_reg_class(si, remat_arg)) continue;
1886 spill = set_find_spill(spill_bb->ilp, remat_arg);
1889 /* arguments of remats have to be live until the very end of the block
1890 * remat = reg_out(remat_arg) and (reload(remat_arg) or live_range(remat_arg)),
1891 * no remats, they could be in wrong order
1894 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1895 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1897 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 3.0);
1898 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, -2.0);
1899 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1901 /* use reload placed for this argument */
1902 if(spill_bb->reloads) {
1903 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1906 ilp_var_t reload = PTR_TO_INT(keyval->val);
1908 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1913 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1918 /**************************************
1919 * B A S I C B L O C K B O D Y
1920 **************************************/
1922 sched_foreach_reverse_from(sched_block_last_noncf(bb), irn) {
1928 ilp_cst_t check_pre,
1934 ilp_cst_t one_memoperand = -1;
1936 /* iterate only until first phi */
1940 op = get_irn_link(irn);
1942 if(op->is_remat) continue;
1944 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1946 /* collect defined values */
1947 if(has_reg_class(si, irn)) {
1948 pset_insert_ptr(defs, irn);
1952 if(is_Proj(irn)) continue;
1955 * init set of irn's arguments
1956 * and all possibly used values around this op
1957 * and values defined by post remats
1959 args = new_set(cmp_keyval, get_irn_arity(irn));
1960 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1961 remat_defs = pset_new_ptr(pset_count(live));
1963 if(!is_start_block(bb) || !be_is_Barrier(irn)) {
1964 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1965 ir_node *irn_arg = get_irn_n(irn, n);
1966 if(has_reg_class(si, irn_arg)) {
1967 set_insert_keyval(args, irn_arg, (void*)n);
1968 pset_insert_ptr(used, irn_arg);
1971 foreach_post_remat(irn, tmp) {
1972 op_t *remat_op = get_irn_link(tmp);
1974 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1976 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1977 ir_node *remat_arg = get_irn_n(tmp, n);
1978 if(has_reg_class(si, remat_arg)) {
1979 pset_insert_ptr(used, remat_arg);
1983 foreach_pre_remat(irn, tmp) {
1984 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1985 ir_node *remat_arg = get_irn_n(tmp, n);
1986 if(has_reg_class(si, remat_arg)) {
1987 pset_insert_ptr(used, remat_arg);
1993 /**********************************
1994 * I N E P I L O G O F irn
1995 **********************************/
1997 /* ensure each dying value is used by only one post remat */
1998 pset_foreach(used, tmp) {
1999 ir_node *value = tmp;
2000 op_t *value_op = get_irn_link(value);
2005 foreach_post_remat(irn, remat) {
2006 op_t *remat_op = get_irn_link(remat);
2008 for(n=get_irn_arity(remat)-1; n>=0; --n) {
2009 ir_node *remat_arg = get_irn_n(remat, n);
2011 /* if value is used by this remat add it to constraint */
2012 if(remat_arg == value) {
2014 /* sum remat2s <= 1 + n_remats*live_range */
2015 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
2016 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2020 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2026 if(pset_find_ptr(live, value) && cst != ILP_UNDEF) {
2027 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
2031 /* ensure at least one value dies at post remat */
2032 foreach_post_remat(irn, tmp) {
2033 op_t *remat_op = get_irn_link(tmp);
2034 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2037 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2038 remat_arg = get_irn_n(tmp, n);
2040 if(has_reg_class(si, remat_arg)) {
2042 /* does arg always die at this op? */
2043 if(!pset_find_ptr(live, remat_arg))
2044 goto skip_one_must_die;
2046 pset_insert_ptr(remat_args, remat_arg);
2050 /* remat + \sum live_range(remat_arg) <= |args| */
2051 ir_snprintf(buf, sizeof(buf), "one_must_die_%+F", tmp);
2052 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2053 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2055 pset_foreach(remat_args, remat_arg) {
2056 op_t *arg_op = get_irn_link(remat_arg);
2058 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2062 del_pset(remat_args);
2065 /* new live ranges for values from L\U defined by post remats */
2066 pset_foreach(live, tmp) {
2067 ir_node *value = tmp;
2068 op_t *value_op = get_irn_link(value);
2070 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
2071 ilp_var_t prev_lr = ILP_UNDEF;
2074 if(pset_find_ptr(remat_defs, value)) {
2076 /* next_live_range <= prev_live_range + sum remat2s */
2077 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
2078 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2080 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
2081 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2083 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
2084 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2086 foreach_post_remat(irn, remat) {
2087 op_t *remat_op = get_irn_link(remat);
2089 /* if value is being rematerialized by this remat */
2090 if(value == remat_op->attr.remat.remat->value) {
2091 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2095 value_op->attr.live_range.ilp = prev_lr;
2096 value_op->attr.live_range.op = irn;
2101 /* requirements for post remats and start live ranges from L/U' for values dying here */
2102 foreach_post_remat(irn, tmp) {
2103 op_t *remat_op = get_irn_link(tmp);
2106 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2107 ir_node *remat_arg = get_irn_n(tmp, n);
2108 op_t *arg_op = get_irn_link(remat_arg);
2110 if(!has_reg_class(si, remat_arg)) continue;
2112 /* only for values in L\U (TODO and D?), the others are handled with post_use */
2113 if(!pset_find_ptr(used, remat_arg)) {
2114 /* remat <= live_range(remat_arg) */
2115 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
2116 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2118 /* if value is becoming live through use by remat2 */
2119 if(!pset_find_ptr(live, remat_arg)) {
2122 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
2123 lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2125 arg_op->attr.live_range.ilp = lr;
2126 arg_op->attr.live_range.op = irn;
2128 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
2130 pset_insert_ptr(live, remat_arg);
2131 add_to_spill_bb(si, bb, remat_arg);
2134 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2135 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2140 d = pset_count(defs);
2141 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
2143 /* count how many regs irn needs for arguments */
2144 u = set_count(args);
2147 /* check the register pressure in the epilog */
2148 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
2149 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
2150 check_post = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - d);
2152 /* add L\U' to check_post */
2153 pset_foreach(live, tmp) {
2154 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
2155 /* if a live value is not used by irn */
2156 tmp_op = get_irn_link(tmp);
2157 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
2161 /***********************************************************
2162 * 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
2163 **********************************************************/
2166 pset_foreach(used, tmp) {
2172 op_t *arg_op = get_irn_link(arg);
2175 spill = add_to_spill_bb(si, bb, arg);
2177 /* new live range for each used value */
2178 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
2179 prev_lr = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2181 /* the epilog stuff - including post_use, check_post, check_post_remat */
2182 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
2183 post_use = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2185 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
2187 /* arg is live throughout epilog if the next live_range is in a register */
2188 if(pset_find_ptr(live, arg)) {
2189 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
2191 /* post_use >= next_lr + remat */
2192 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2193 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2194 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2195 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2198 /* forall post remat which use arg add a similar cst */
2199 foreach_post_remat(irn, remat) {
2202 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2203 ir_node *remat_arg = get_irn_n(remat, n);
2204 op_t *remat_op = get_irn_link(remat);
2206 if(remat_arg == arg) {
2207 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
2209 /* post_use >= remat */
2210 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
2211 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2212 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
2213 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2218 /* if value is not an arg of op and not possibly defined by post remat
2219 * then it may only die and not become live
2221 if(!set_find_keyval(args, arg)) {
2222 /* post_use <= prev_lr */
2223 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
2224 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2225 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2226 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2228 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
2229 /* next_lr <= prev_lr */
2230 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
2231 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2232 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
2233 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
2237 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2238 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2239 if (get_irn_n(irn, n) == arg &&
2240 arch_possible_memory_operand(irn, n)) {
2241 ilp_var_t memoperand;
2243 ir_snprintf(buf, sizeof(buf), "memoperand_%N_%d", irn, n);
2244 memoperand = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_memoperand*execution_frequency(si, bb), 0.0);
2245 set_insert_memoperand(si->memoperands, irn, n, memoperand);
2247 ir_snprintf(buf, sizeof(buf), "nolivepost_%N_%d", irn, n);
2248 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2250 lpp_set_factor_fast(si->lpp, cst, memoperand, 1.0);
2251 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
2256 /* new live range begins for each used value */
2257 arg_op->attr.live_range.ilp = prev_lr;
2258 arg_op->attr.live_range.op = irn;
2260 pset_insert_ptr(live, arg);
2263 /* just to be sure */
2264 check_post = ILP_UNDEF;
2266 /* allow original defintions to be removed */
2267 if(opt_repair_schedule) {
2268 pset_foreach(defs, tmp) {
2269 op_t *tmp_op = get_irn_link(tmp);
2270 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2275 ir_snprintf(buf, sizeof(buf), "delete_%N", tmp);
2276 delete = lpp_add_var_default(si->lpp, buf, lpp_binary, -1.0 * get_cost(irn) * execution_frequency(si, bb), 0.0);
2278 /* op may not be killed if its first live_range is 1 */
2279 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2280 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2281 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2282 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2284 /* op may not be killed if it is spilled after the definition */
2285 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2286 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2287 lpp_set_factor_fast(si->lpp, cst, delete, 1.0);
2288 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2293 ir_snprintf(buf, sizeof(buf), "keep_%N", tmp);
2294 keep = lpp_add_var_default(si->lpp, buf, lpp_binary, get_cost(irn) * execution_frequency(si, bb), 1.0);
2296 /* op may not be killed if its first live_range is 1 */
2297 ir_snprintf(buf, sizeof(buf), "killorig-lr_%N", tmp);
2298 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2299 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2300 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, -1.0);
2302 /* op may not be killed if it is spilled after the definition */
2303 ir_snprintf(buf, sizeof(buf), "killorig-spill_%N", tmp);
2304 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 0.0);
2305 lpp_set_factor_fast(si->lpp, cst, keep, 1.0);
2306 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2311 pset_foreach(defs, tmp) {
2312 op_t *tmp_op = get_irn_link(tmp);
2313 spill_t *spill = set_find_spill(spill_bb->ilp, tmp);
2316 /* live_range or spill should be 1
2317 TODO: lr should be live until first use */
2318 ir_snprintf(buf, sizeof(buf), "nokillorig_%N", tmp);
2319 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_greater, 1.0);
2320 lpp_set_factor_fast(si->lpp, cst, tmp_op->attr.live_range.ilp, 1.0);
2321 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2331 /* check the register pressure in the prolog */
2332 /* sum_{L\U} lr <= k - |U| */
2333 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
2334 check_pre = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs - u);
2336 /* for the prolog remove defined values from the live set */
2337 pset_foreach(defs, tmp) {
2338 pset_remove_ptr(live, tmp);
2341 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2342 ir_snprintf(buf, sizeof(buf), "one_memoperand_%N", irn);
2343 one_memoperand = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2346 /***********************************************************
2347 * 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
2348 **********************************************************/
2351 set_foreach(args, keyval) {
2353 const ir_node *arg = keyval->key;
2354 int i = PTR_TO_INT(keyval->val);
2355 op_t *arg_op = get_irn_link(arg);
2356 ilp_cst_t requirements;
2359 spill = set_find_spill(spill_bb->ilp, arg);
2362 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
2363 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);
2365 /* reload <= mem_out */
2366 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
2367 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2368 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
2369 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2371 /* requirement: arg must be in register for use */
2372 /* reload + remat + live_range == 1 */
2373 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
2374 requirements = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 1.0);
2376 lpp_set_factor_fast(si->lpp, requirements, arg_op->attr.live_range.ilp, 1.0);
2377 lpp_set_factor_fast(si->lpp, requirements, op->attr.live_range.args.reloads[i], 1.0);
2378 foreach_pre_remat(irn, tmp) {
2379 op_t *remat_op = get_irn_link(tmp);
2380 if(remat_op->attr.remat.remat->value == arg) {
2381 lpp_set_factor_fast(si->lpp, requirements, remat_op->attr.remat.ilp, 1.0);
2385 if(opt_memoperands && (!is_start_block(bb) || be_is_Barrier(irn))) {
2387 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2388 if(get_irn_n(irn, n) == arg) {
2392 for(n = get_irn_arity(irn)-1; n>=0; --n) {
2393 if (get_irn_n(irn, n) == arg &&
2394 arch_possible_memory_operand(irn, n)) {
2395 memoperand_t *memoperand;
2396 memoperand = set_find_memoperand(si->memoperands, irn, n);
2398 /* memoperand <= mem_out */
2399 ir_snprintf(buf, sizeof(buf), "req_memoperand_%N_%d", irn, n);
2400 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2401 lpp_set_factor_fast(si->lpp, cst, memoperand->ilp, 1.0);
2402 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
2404 /* the memoperand is only sufficient if it is used once by the op */
2405 if(n_memoperands == 1)
2406 lpp_set_factor_fast(si->lpp, requirements, memoperand->ilp, 1.0);
2408 lpp_set_factor_fast(si->lpp, one_memoperand, memoperand->ilp, 1.0);
2410 /* we have one more free register if we use a memory operand */
2411 lpp_set_factor_fast(si->lpp, check_pre, memoperand->ilp, -1.0);
2417 /* iterate over L\U */
2418 pset_foreach(live, tmp) {
2419 if(!set_find_keyval(args, tmp)) {
2420 /* if a live value is not used by irn */
2421 tmp_op = get_irn_link(tmp);
2422 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
2426 /* requirements for remats */
2427 foreach_pre_remat(irn, tmp) {
2428 op_t *remat_op = get_irn_link(tmp);
2431 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2432 ir_node *remat_arg = get_irn_n(tmp, n);
2433 op_t *arg_op = get_irn_link(remat_arg);
2435 if(!has_reg_class(si, remat_arg)) continue;
2437 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
2438 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
2439 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2441 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2442 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
2444 /* if remat arg is also used by current op then we can use reload placed for this argument */
2445 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
2446 int index = (int)keyval->val;
2448 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
2456 /*************************
2457 * D O N E W I T H O P
2458 *************************/
2460 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
2462 pset_foreach(live, tmp) {
2463 assert(has_reg_class(si, tmp));
2467 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2468 ir_node *arg = get_irn_n(irn, n);
2470 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
2474 del_pset(remat_defs);
2478 defs = pset_new_ptr_default();
2480 /* skip everything above barrier in start block */
2481 if(is_start_block(bb) && be_is_Barrier(irn)) {
2482 assert(pset_count(live) == 0);
2491 /***************************************
2492 * B E G I N N I N G O F B L O C K
2493 ***************************************/
2496 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
2497 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
2499 pset_foreach(live, irn) {
2500 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
2503 /* construct mem_outs for all values */
2504 set_foreach(spill_bb->ilp, spill) {
2505 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
2506 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2508 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
2509 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
2511 if(pset_find_ptr(live, spill->irn)) {
2512 int default_spilled;
2513 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
2515 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
2516 default_spilled = be_is_live_in(si->lv, bb, spill->irn) || is_Phi(spill->irn);
2517 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, default_spilled);
2518 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2520 if(opt_memcopies && is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2522 op_t *op = get_irn_link(spill->irn);
2524 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
2525 const ir_node *arg = get_irn_n(spill->irn, n);
2531 /* argument already done? */
2532 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
2534 /* get sum of execution frequencies of blocks with the same phi argument */
2535 for(m=n; m>=0; --m) {
2536 const ir_node *arg2 = get_irn_n(spill->irn, m);
2539 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2543 /* copies are not for free */
2544 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2545 var = lpp_add_var_default(si->lpp, buf, lpp_binary, opt_cost_spill * freq, 1.0);
2547 for(m=n; m>=0; --m) {
2548 const ir_node *arg2 = get_irn_n(spill->irn, m);
2551 op->attr.live_range.args.copies[m] = var;
2556 /* copy <= mem_in */
2557 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2558 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2559 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2560 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2567 foreach_post_remat(bb, tmp) {
2569 op_t *remat_op = get_irn_link(tmp);
2570 pset *remat_args = pset_new_ptr(get_irn_arity(tmp));
2573 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2574 remat_arg = get_irn_n(tmp, n);
2576 if(has_reg_class(si, remat_arg)) {
2577 pset_insert_ptr(remat_args, remat_arg);
2581 /* remat + \sum live_range(remat_arg) <= |args| */
2582 ir_snprintf(buf, sizeof(buf), "one_must_die_%N", tmp);
2583 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, pset_count(remat_args));
2584 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2586 pset_foreach(remat_args, remat_arg) {
2587 if(pset_find_ptr(live, remat_arg)) {
2588 op_t *remat_arg_op = get_irn_link(remat_arg);
2589 lpp_set_factor_fast(si->lpp, cst, remat_arg_op->attr.live_range.ilp, 1.0);
2592 del_pset(remat_args);
2595 foreach_post_remat(bb, tmp) {
2598 for(n=get_irn_arity(tmp)-1; n>=0; --n) {
2599 ir_node *remat_arg = get_irn_n(tmp, n);
2601 /* if value is becoming live through use by remat2 */
2602 if(has_reg_class(si, remat_arg) && !pset_find_ptr(live, remat_arg)) {
2603 op_t *remat_arg_op = get_irn_link(remat_arg);
2606 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 at bb start %+F\n", remat_arg, tmp));
2608 pset_insert_ptr(live, remat_arg);
2609 spill = add_to_spill_bb(si, bb, remat_arg);
2610 remat_arg_op->attr.live_range.ilp = ILP_UNDEF;
2612 /* we need reg_in and mem_in for this value; they will be referenced later */
2613 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", remat_arg, bb);
2614 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2615 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", remat_arg, bb);
2616 spill->mem_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
2619 /* optimization: all memory stuff should be 0, for we do not want to insert reloads for remats */
2620 ir_snprintf(buf, sizeof(buf), "nomem_%N_%N", remat_arg, bb);
2621 nomem = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0.0);
2622 lpp_set_factor_fast(si->lpp, nomem, spill->spill, 1.0);
2627 /* L\U is empty at bb start */
2628 /* arg is live throughout epilog if it is reg_in into this block */
2630 /* check the register pressure at the beginning of the block
2633 /* reg_in entspricht post_use */
2635 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2636 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, si->n_regs);
2638 pset_foreach(live, irn) {
2641 spill = set_find_spill(spill_bb->ilp, irn);
2644 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2645 spill->reg_in = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 0.0);
2647 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2649 /* spill + mem_in <= 1 */
2650 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2651 nospill = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
2653 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2654 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2656 } /* post_remats are NOT included in register pressure check because
2657 they do not increase regpressure */
2659 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2660 pset_foreach(live, irn) {
2664 spill = set_find_spill(spill_bb->ilp, irn);
2665 assert(spill && spill->irn == irn);
2667 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2668 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2671 ir_node *phi_arg = get_Phi_pred(irn, n);
2672 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2673 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2675 op_t *op = get_irn_link(irn);
2677 /* although the phi is in the right regclass one or more of
2678 * its arguments can be in a different one or at least to
2681 if(has_reg_class(si, phi_arg)) {
2682 /* mem_in < mem_out_arg + copy */
2683 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2684 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2686 /* reg_in < reg_out_arg */
2687 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2688 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2690 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2691 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2693 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2696 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2698 lpp_set_factor_fast(si->lpp, mem_in, op->attr.live_range.args.copies[n], -1.0);
2700 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2704 /* else assure the value arrives on all paths in the same resource */
2706 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2709 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2710 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2713 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2714 mem_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2715 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2716 reg_in = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2718 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2719 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2721 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2724 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2725 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2730 foreach_post_remat(bb, tmp) {
2733 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2734 ir_node *remat_arg = get_irn_n(tmp, n);
2735 op_t *remat_op = get_irn_link(tmp);
2737 if(!has_reg_class(si, remat_arg)) continue;
2739 spill = set_find_spill(spill_bb->ilp, remat_arg);
2742 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2743 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2744 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2745 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2749 pset_foreach(live, irn) {
2750 const op_t *op = get_irn_link(irn);
2751 const ir_node *remat;
2756 foreach_post_remat(bb, remat) {
2759 for (n=get_irn_arity(remat)-1; n>=0; --n) {
2760 const ir_node *arg = get_irn_n(remat, n);
2763 const op_t *remat_op = get_irn_link(remat);
2765 if(cst == ILP_UNDEF) {
2766 /* sum remat2s <= 1 + n_remats*live_range */
2767 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", irn, bb);
2768 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1.0);
2770 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2776 if(cst != ILP_UNDEF && op->attr.live_range.ilp != ILP_UNDEF) {
2777 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -n_remats);
2781 /* first live ranges from reg_ins */
2782 pset_foreach(live, irn) {
2783 op_t *op = get_irn_link(irn);
2785 if(op->attr.live_range.ilp != ILP_UNDEF) {
2787 spill = set_find_spill(spill_bb->ilp, irn);
2788 assert(spill && spill->irn == irn);
2790 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2791 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2792 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2793 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2795 foreach_post_remat(bb, tmp) {
2796 op_t *remat_op = get_irn_link(tmp);
2798 if(remat_op->attr.remat.remat->value == irn) {
2799 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2805 /* walk forward now and compute constraints for placing spills */
2806 /* this must only be done for values that are not defined in this block */
2807 pset_foreach(live, irn) {
2809 * if value is defined in this block we can anways place the spill directly after the def
2810 * -> no constraint necessary
2812 if(!is_Phi(irn) && get_nodes_block(irn) == bb) {
2817 spill = set_find_spill(spill_bb->ilp, irn);
2820 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2821 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0.0);
2823 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2824 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2827 sched_foreach_op(bb, tmp) {
2828 op_t *op = get_irn_link(tmp);
2830 if(is_Phi(tmp)) continue;
2831 assert(!is_Proj(tmp));
2834 const ir_node *value = op->attr.remat.remat->value;
2837 /* only collect remats up to the first real use of a value */
2838 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2843 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2844 ir_node *arg = get_irn_n(tmp, n);
2847 /* if a value is used stop collecting remats */
2860 typedef struct _irnlist_t {
2861 struct list_head list;
2865 typedef struct _interference_t {
2866 struct list_head blocklist;
2872 cmp_interference(const void *a, const void *b, size_t size)
2874 const interference_t *p = a;
2875 const interference_t *q = b;
2878 return !(p->a == q->a && p->b == q->b);
2881 static interference_t *
2882 set_find_interference(set * set, ir_node * a, ir_node * b)
2884 interference_t query;
2886 query.a = (a>b)?a:b;
2887 query.b = (a>b)?b:a;
2889 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2892 static interference_t *
2893 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2895 interference_t query,
2897 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2901 result = set_find_interference(set, a, b);
2904 list_add(&list->list, &result->blocklist);
2908 query.a = (a>b)?a:b;
2909 query.b = (a>b)?b:a;
2911 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2913 INIT_LIST_HEAD(&result->blocklist);
2914 list_add(&list->list, &result->blocklist);
2920 int values_interfere_in_block(const spill_ilp_t *si, const ir_node *bb, const ir_node *a, const ir_node *b)
2922 const ir_edge_t *edge;
2924 if (get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2925 /* both values are live in, so they interfere */
2929 /* ensure a dominates b */
2930 if (value_dominates(b, a)) {
2936 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2939 /* the following code is stolen from bera.c */
2940 if (be_is_live_end(si->lv, bb, a))
2943 foreach_out_edge(a, edge) {
2944 const ir_node *user = edge->src;
2945 if (get_nodes_block(user) == bb
2948 && ! pset_find_ptr(si->inverse_ops, user)
2949 && value_dominates(b, user))
2957 * Walk all irg blocks and collect interfering values inside of phi classes
2960 luke_interferencewalker(ir_node * bb, void * data)
2962 spill_ilp_t *si = (spill_ilp_t*)data;
2965 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
2966 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
2967 op_t *a_op = get_irn_link(a);
2970 /* a is only interesting if it is in my register class and if it is inside a phi class */
2971 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
2972 if (a_op->is_remat || pset_find_ptr(si->inverse_ops, a))
2975 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)) {
2976 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
2977 op_t *b_op = get_irn_link(b);
2979 /* a and b are only interesting if they are in the same phi class */
2980 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
2981 if (b_op->is_remat || pset_find_ptr(si->inverse_ops, b))
2984 if (values_interfere_in_block(si, bb, a, b)) {
2985 DBG((si->dbg, LEVEL_4, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2986 set_insert_interference(si, si->interferences, a, b, bb);
2994 static unsigned int copy_path_id = 0;
2997 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
3004 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
3005 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3007 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
3009 pset_foreach(copies, ptr) {
3010 copy = PTR_TO_INT(ptr);
3011 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
3016 * @parameter copies contains a path of copies which lead us to irn
3017 * @parameter visited contains a set of nodes already visited on this path
3020 find_copy_path(spill_ilp_t * si, const ir_node * irn, const ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
3022 const ir_edge_t *edge;
3023 op_t *op = get_irn_link(irn);
3024 pset *visited_users = pset_new_ptr_default();
3027 if(op->is_remat) return 0;
3029 pset_insert_ptr(visited, irn);
3033 pset *visited_operands = pset_new_ptr(get_irn_arity(irn));
3035 /* visit all operands */
3036 for(n=get_irn_arity(irn)-1; n>=0; --n) {
3037 ir_node *arg = get_irn_n(irn, n);
3038 ilp_var_t copy = op->attr.live_range.args.copies[n];
3040 if(!has_reg_class(si, arg)) continue;
3041 if(pset_find_ptr(visited_operands, arg)) continue;
3042 pset_insert_ptr(visited_operands, arg);
3045 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3046 del_pset(visited_operands);
3047 del_pset(visited_users);
3048 pset_remove_ptr(visited, irn);
3051 pset_insert(copies, INT_TO_PTR(copy), copy);
3052 write_copy_path_cst(si, copies, any_interfere);
3053 pset_remove(copies, INT_TO_PTR(copy), copy);
3054 } else if(!pset_find_ptr(visited, arg)) {
3055 pset_insert(copies, INT_TO_PTR(copy), copy);
3056 paths += find_copy_path(si, arg, target, any_interfere, copies, visited);
3057 pset_remove(copies, INT_TO_PTR(copy), copy);
3059 if(paths > MAX_PATHS) {
3060 if(pset_count(copies) == 0) {
3064 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3065 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3066 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3067 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3068 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3072 del_pset(visited_operands);
3073 del_pset(visited_users);
3074 pset_remove_ptr(visited, irn);
3077 } else if(pset_count(copies) == 0) {
3083 del_pset(visited_operands);
3086 /* visit all uses which are phis */
3087 foreach_out_edge(irn, edge) {
3088 ir_node *user = edge->src;
3089 int pos = edge->pos;
3090 op_t *op = get_irn_link(user);
3093 if(!is_Phi(user)) continue;
3094 if(!has_reg_class(si, user)) continue;
3095 if(pset_find_ptr(visited_users, user)) continue;
3096 pset_insert_ptr(visited_users, user);
3098 copy = op->attr.live_range.args.copies[pos];
3100 if(user == target) {
3101 if(++paths > MAX_PATHS && pset_count(copies) != 0) {
3102 del_pset(visited_users);
3103 pset_remove_ptr(visited, irn);
3106 pset_insert(copies, INT_TO_PTR(copy), copy);
3107 write_copy_path_cst(si, copies, any_interfere);
3108 pset_remove(copies, INT_TO_PTR(copy), copy);
3109 } else if(!pset_find_ptr(visited, user)) {
3110 pset_insert(copies, INT_TO_PTR(copy), copy);
3111 paths += find_copy_path(si, user, target, any_interfere, copies, visited);
3112 pset_remove(copies, INT_TO_PTR(copy), copy);
3114 if(paths > MAX_PATHS) {
3115 if(pset_count(copies) == 0) {
3119 ir_snprintf(buf, sizeof(buf), "always_copy-%d-%d", any_interfere, copy);
3120 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_equal, 0);
3121 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3122 lpp_set_factor_fast(si->lpp, cst, copy, 1.0);
3123 DBG((si->dbg, LEVEL_1, "ALWAYS COPYING %d FOR INTERFERENCE %d\n", copy, any_interfere));
3127 del_pset(visited_users);
3128 pset_remove_ptr(visited, irn);
3131 } else if(pset_count(copies) == 0) {
3137 del_pset(visited_users);
3138 pset_remove_ptr(visited, irn);
3143 gen_copy_constraints(spill_ilp_t * si, const ir_node * a, const ir_node * b, ilp_var_t any_interfere)
3145 pset * copies = pset_new_ptr_default();
3146 pset * visited = pset_new_ptr_default();
3148 find_copy_path(si, a, b, any_interfere, copies, visited);
3156 memcopyhandler(spill_ilp_t * si)
3158 interference_t *interference;
3160 /* teste Speicherwerte auf Interferenz */
3162 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
3163 irg_block_walk_graph(si->birg->irg, luke_interferencewalker, NULL, si);
3165 /* now lets emit the ILP unequations for the crap */
3166 set_foreach(si->interferences, interference) {
3168 ilp_var_t interfere, any_interfere;
3169 ilp_cst_t any_interfere_cst, cst;
3170 const ir_node *a = interference->a;
3171 const ir_node *b = interference->b;
3173 /* any_interf <= \sum interf */
3174 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
3175 any_interfere_cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3176 any_interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3178 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
3180 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
3181 const ir_node *bb = irnlist->irn;
3182 spill_bb_t *spill_bb = get_irn_link(bb);
3187 spilla = set_find_spill(spill_bb->ilp, a);
3190 spillb = set_find_spill(spill_bb->ilp, b);
3193 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
3194 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
3195 /* 2: - mem_in_a - spill_a + interfere <= 0 */
3196 /* 3: - mem_in_b - spill_b + interfere <= 0 */
3197 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
3198 interfere = lpp_add_var_default(si->lpp, buf, lpp_binary, 0.0, 1.0);
3200 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
3201 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 1);
3203 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
3204 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
3205 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
3206 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
3207 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
3209 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
3210 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3212 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3213 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
3214 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
3216 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
3217 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3219 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3220 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
3221 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
3224 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
3226 /* any_interfere >= interf */
3227 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
3228 cst = lpp_add_cst_uniq(si->lpp, buf, lpp_less, 0);
3230 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
3231 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
3234 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
3235 gen_copy_constraints(si,a,b,any_interfere);
3243 return fabs(x) < 0.00001;
3247 * node attribute hook for changing colors
3249 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
3251 spill_ilp_t *si = get_irg_link(current_ir_graph);
3254 if(pset_find_ptr(si->all_possible_remats, n)) {
3255 op_t *op = (op_t*)get_irn_link(n);
3256 assert(op && op->is_remat);
3258 if(!op->attr.remat.remat->inverse) {
3259 if(op->attr.remat.pre) {
3260 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
3262 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
3267 op_t *op = (op_t*)get_irn_link(n);
3268 assert(op && op->is_remat);
3270 if(op->attr.remat.pre) {
3271 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
3273 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
3284 dump_graph_with_remats(ir_graph * irg, const char * suffix)
3286 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
3287 be_dump(irg, suffix, dump_ir_block_graph_sched);
3288 set_dump_node_vcgattr_hook(NULL);
3292 * Edge hook to dump the schedule edges with annotated register pressure.
3295 sched_pressure_edge_hook(FILE *F, ir_node *irn)
3297 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
3298 ir_node *prev = sched_prev(irn);
3299 fprintf(F, "edge:{sourcename:\"");
3301 fprintf(F, "\" targetname:\"");
3303 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
3304 fprintf(F, "\" color:magenta}\n");
3310 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
3312 DUMP_NODE_EDGE_FUNC old_edge_hook = get_dump_node_edge_hook();
3314 dump_consts_local(0);
3315 set_dump_node_edge_hook(sched_pressure_edge_hook);
3316 dump_ir_block_graph(irg, suffix);
3317 set_dump_node_edge_hook(old_edge_hook);
3321 walker_pressure_annotator(ir_node * bb, void * data)
3323 spill_ilp_t *si = data;
3326 pset *live = pset_new_ptr_default();
3329 be_lv_foreach(si->lv, bb, be_lv_state_end, i) {
3330 irn = be_lv_get_irn(si->lv, bb, i);
3332 if (has_reg_class(si, irn)) {
3333 pset_insert_ptr(live, irn);
3337 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
3339 sched_foreach_reverse(bb, irn) {
3341 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
3345 if(has_reg_class(si, irn)) {
3346 pset_remove_ptr(live, irn);
3347 if(is_Proj(irn)) ++projs;
3350 if(!is_Proj(irn)) projs = 0;
3352 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3353 ir_node *arg = get_irn_n(irn, n);
3355 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
3357 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
3364 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
3366 be_dump(si->birg->irg, suffix, dump_ir_block_graph_sched_pressure);
3370 connect_all_remats_with_keep(spill_ilp_t * si)
3378 n_remats = pset_count(si->all_possible_remats);
3380 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
3383 pset_foreach(si->all_possible_remats, irn) {
3388 si->keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_remats, ins);
3390 obstack_free(si->obst, ins);
3395 connect_all_spills_with_keep(spill_ilp_t * si)
3404 n_spills = pset_count(si->spills);
3406 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
3409 pset_foreach(si->spills, irn) {
3414 keep = be_new_Keep(si->cls, si->birg->irg, get_irg_end_block(si->birg->irg), n_spills, ins);
3416 obstack_free(si->obst, ins);
3420 /** insert a spill at an arbitrary position */
3421 static ir_node *be_spill2(ir_node *irn, ir_node *insert)
3423 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
3424 ir_graph *irg = get_irn_irg(bl);
3425 ir_node *frame = get_irg_frame(irg);
3428 const arch_register_class_t *cls = arch_get_irn_reg_class(irn, -1);
3429 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(frame, -1);
3431 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
3434 * search the right insertion point. a spill of a phi cannot be put
3435 * directly after the phi, if there are some phis behind the one which
3436 * is spilled. Also, a spill of a Proj must be after all Projs of the
3439 * Here's one special case:
3440 * If the spill is in the start block, the spill must be after the frame
3441 * pointer is set up. This is done by setting insert to the end of the block
3442 * which is its default initialization (see above).
3445 if (bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
3448 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
3451 sched_add_after(insert, spill);
3456 delete_remat(spill_ilp_t * si, ir_node * remat) {
3458 ir_node *bad = get_irg_bad(si->birg->irg);
3460 sched_remove(remat);
3462 /* kill links to operands */
3463 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
3464 set_irn_n(remat, n, bad);
3469 clean_remat_info(spill_ilp_t * si)
3473 remat_info_t *remat_info;
3474 ir_node *bad = get_irg_bad(si->birg->irg);
3476 set_foreach(si->remat_info, remat_info) {
3477 if(!remat_info->remats) continue;
3479 pset_foreach(remat_info->remats, remat)
3481 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
3482 if(sched_is_scheduled(remat->proj)) {
3483 sched_remove((ir_node*)remat->proj);
3485 set_irn_n((ir_node*)remat->proj, -1, bad);
3486 set_irn_n((ir_node*)remat->proj, 0, bad);
3489 if(get_irn_n_edges(remat->op) == 0) {
3490 if(sched_is_scheduled(remat->op)) {
3491 sched_remove((ir_node*)remat->op);
3493 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
3494 set_irn_n((ir_node*)remat->op, n, bad);
3499 if(remat_info->remats) del_pset(remat_info->remats);
3500 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
3505 delete_unnecessary_remats(spill_ilp_t * si)
3507 if(opt_keep_alive & KEEPALIVE_REMATS) {
3509 ir_node *bad = get_irg_bad(si->birg->irg);
3512 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
3513 ir_node *keep_arg = get_irn_n(si->keep, n);
3514 op_t *arg_op = get_irn_link(keep_arg);
3517 assert(arg_op->is_remat);
3519 name = si->lpp->vars[arg_op->attr.remat.ilp];
3521 if(is_zero(name->value)) {
3522 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
3523 /* TODO check whether reload is preferred over remat (could be bug) */
3524 delete_remat(si, keep_arg);
3526 if(!arg_op->attr.remat.remat->inverse) {
3527 if(arg_op->attr.remat.pre) {
3528 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
3530 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
3533 if(arg_op->attr.remat.pre) {
3534 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
3536 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
3541 set_irn_n(si->keep, n, bad);
3544 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
3549 pset_foreach(si->all_possible_remats, remat) {
3550 op_t *remat_op = get_irn_link(remat);
3551 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
3553 if(is_zero(name->value)) {
3554 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
3555 /* TODO check whether reload is preferred over remat (could be bug) */
3556 delete_remat(si, remat);
3558 if(!remat_op->attr.remat.remat->inverse) {
3559 if(remat_op->attr.remat.pre) {
3560 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
3562 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
3565 if(remat_op->attr.remat.pre) {
3566 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
3568 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
3577 get_spills_for_value(spill_ilp_t * si, const ir_node * value)
3579 pset *spills = pset_new_ptr_default();
3581 const ir_node *next;
3584 defs = set_find_def(si->values, value);
3586 if(defs && defs->spills) {
3587 for(next = defs->spills; next; next = get_irn_link(next)) {
3588 pset_insert_ptr(spills, next);
3596 new_r_PhiM_nokeep(ir_graph * irg, ir_node *block, int arity, ir_node **in)
3600 assert( get_irn_arity(block) == arity );
3602 res = new_ir_node(NULL, irg, block, op_Phi, mode_M, arity, in);
3603 res->attr.phi.u.backedge = new_backedge_arr(irg->obst, arity);
3609 * @param before The node after which the spill will be placed in the schedule
3612 insert_spill(spill_ilp_t * si, ir_node * irn, const ir_node * value, ir_node * before)
3617 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
3619 spill = be_spill2(irn, before);
3621 defs = set_insert_def(si->values, value);
3624 /* enter into the linked list */
3625 set_irn_link(spill, defs->spills);
3626 defs->spills = spill;
3628 if(opt_keep_alive & KEEPALIVE_SPILLS)
3629 pset_insert_ptr(si->spills, spill);
3635 * @param before The Phi node which has to be spilled
3638 insert_mem_phi(spill_ilp_t * si, ir_node * phi)
3645 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
3647 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3648 ins[n] = si->m_unknown;
3651 mem_phi = new_r_PhiM_nokeep(si->birg->irg, get_nodes_block(phi), get_irn_arity(phi), ins);
3653 defs = set_insert_def(si->values, phi);
3656 /* enter into the linked list */
3657 set_irn_link(mem_phi, defs->spills);
3658 defs->spills = mem_phi;
3660 #ifdef SCHEDULE_PHIM
3661 sched_add_after(phi, mem_phi);
3663 pset_insert_ptr(si->phims, mem_phi);
3666 if(opt_keep_alive & KEEPALIVE_SPILLS)
3667 pset_insert_ptr(si->spills, mem_phi);
3674 * Add remat to list of defs, destroys link field!
3677 insert_remat(spill_ilp_t * si, ir_node * remat)
3680 op_t *remat_op = get_irn_link(remat);
3682 assert(remat_op->is_remat);
3684 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
3687 /* enter into the linked list */
3688 set_irn_link(remat, defs->remats);
3689 defs->remats = remat;
3694 * Add reload before operation and add to list of defs
3697 insert_reload(spill_ilp_t * si, const ir_node * value, ir_node * after)
3703 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3705 defs = set_find_def(si->values, value);
3707 spill = defs->spills;
3708 assert(spill && "no spill placed before reload");
3710 reload = be_reload(si->cls, after, get_irn_mode(value), spill);
3712 /* enter into the linked list */
3713 set_irn_link(reload, defs->remats);
3714 defs->remats = reload;
3719 void perform_memory_operand(spill_ilp_t * si, memoperand_t * memoperand)
3722 ir_node *value = get_irn_n(memoperand->irn, memoperand->pos);
3725 DBG((si->dbg, LEVEL_2, "\t inserting memory operand for value %+F at %+F\n", value, memoperand->irn));
3727 defs = set_find_def(si->values, value);
3729 spill = defs->spills;
3730 assert(spill && "no spill placed before reload");
3732 arch_perform_memory_operand(memoperand->irn, spill, memoperand->pos);
3735 void insert_memoperands(spill_ilp_t * si)
3737 memoperand_t *memoperand;
3740 set_foreach(si->memoperands, memoperand) {
3741 name = si->lpp->vars[memoperand->ilp];
3742 if(!is_zero(name->value)) {
3743 perform_memory_operand(si, memoperand);
3749 walker_spill_placer(ir_node * bb, void * data) {
3750 spill_ilp_t *si = (spill_ilp_t*)data;
3752 spill_bb_t *spill_bb = get_irn_link(bb);
3753 pset *spills_to_do = pset_new_ptr_default();
3756 set_foreach(spill_bb->ilp, spill) {
3759 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3760 name = si->lpp->vars[spill->mem_in];
3761 if(!is_zero(name->value)) {
3764 mem_phi = insert_mem_phi(si, spill->irn);
3766 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3770 name = si->lpp->vars[spill->spill];
3771 if(!is_zero(name->value)) {
3772 /* place spill directly after definition */
3773 if(get_nodes_block(spill->irn) == bb) {
3774 insert_spill(si, spill->irn, spill->irn, spill->irn);
3778 /* place spill at bb start */
3779 if(spill->reg_in > 0) {
3780 name = si->lpp->vars[spill->reg_in];
3781 if(!is_zero(name->value)) {
3782 insert_spill(si, spill->irn, spill->irn, bb);
3786 /* place spill after a remat */
3787 pset_insert_ptr(spills_to_do, spill->irn);
3790 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3793 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3794 op_t *op = get_irn_link(irn);
3796 if(be_is_Spill(irn)) continue;
3799 /* TODO fix this if we want to support remats with more than two nodes */
3800 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3801 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3803 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3806 if(pset_find_ptr(spills_to_do, irn)) {
3807 pset_remove_ptr(spills_to_do, irn);
3809 insert_spill(si, irn, irn, irn);
3815 assert(pset_count(spills_to_do) == 0);
3817 /* afterwards free data in block */
3818 del_pset(spills_to_do);
3822 insert_mem_copy(spill_ilp_t * si, ir_node * bb, ir_node * value)
3824 ir_node *insert_pos = bb;
3827 /* find last definition of arg value in block */
3832 defs = set_find_def(si->values, value);
3834 if(defs && defs->remats) {
3835 for(next = defs->remats; next; next = get_irn_link(next)) {
3836 if(get_nodes_block(next) == bb && sched_get_time_step(next) > last) {
3837 last = sched_get_time_step(next);
3843 if(get_nodes_block(value) == bb && sched_get_time_step(value) > last) {
3844 last = sched_get_time_step(value);
3848 DBG((si->dbg, LEVEL_2, "\t inserting mem copy for value %+F after %+F\n", value, insert_pos));
3850 spill = be_spill2(is_Block(insert_pos)?value:insert_pos, insert_pos);
3856 phim_fixer(spill_ilp_t *si) {
3859 set_foreach(si->values, defs) {
3860 const ir_node *phi = defs->value;
3861 op_t *op = get_irn_link(phi);
3862 ir_node *phi_m = NULL;
3863 ir_node *next = defs->spills;
3866 if(!is_Phi(phi)) continue;
3869 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3873 next = get_irn_link(next);
3876 if(!phi_m) continue;
3878 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3879 ir_node *value = get_irn_n(phi, n);
3880 defs_t *val_defs = set_find_def(si->values, value);
3882 /* a spill of this value */
3887 ir_node *pred = get_Block_cfgpred_block(get_nodes_block(phi), n);
3888 lpp_name_t *name = si->lpp->vars[op->attr.live_range.args.copies[n]];
3890 if(!is_zero(name->value)) {
3891 spill = insert_mem_copy(si, pred, value);
3893 spill = val_defs->spills;
3896 spill = val_defs->spills;
3899 assert(spill && "no spill placed before PhiM");
3900 set_irn_n(phi_m, n, spill);
3906 walker_reload_placer(ir_node * bb, void * data) {
3907 spill_ilp_t *si = (spill_ilp_t*)data;
3909 spill_bb_t *spill_bb = get_irn_link(bb);
3911 /* reloads at end of block */
3912 if(spill_bb->reloads) {
3915 set_foreach(spill_bb->reloads, keyval) {
3916 ir_node *irn = (ir_node*)keyval->key;
3917 ilp_var_t reload = PTR_TO_INT(keyval->val);
3920 name = si->lpp->vars[reload];
3921 if(!is_zero(name->value)) {
3923 ir_node *insert_pos = bb;
3924 ir_node *prev = sched_block_last_noncf(bb);
3925 op_t *prev_op = get_irn_link(prev);
3927 while(be_is_Spill(prev)) {
3928 prev = sched_prev(prev);
3931 prev_op = get_irn_link(prev);
3933 /* insert reload before pre-remats */
3934 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3935 && prev_op->is_remat && prev_op->attr.remat.pre) {
3939 prev = sched_prev(prev);
3940 } while(be_is_Spill(prev));
3942 prev_op = get_irn_link(prev);
3946 reload = insert_reload(si, irn, insert_pos);
3948 if(opt_keep_alive & KEEPALIVE_RELOADS)
3949 pset_insert_ptr(si->spills, reload);
3954 /* walk and insert more reloads and collect remats */
3955 sched_foreach_reverse(bb, irn) {
3956 op_t *op = get_irn_link(irn);
3958 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3959 if(is_Phi(irn)) break;
3962 if(get_irn_mode(irn) != mode_T) {
3963 insert_remat(si, irn);
3968 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3969 ir_node *arg = get_irn_n(irn, n);
3971 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3974 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3975 if(!is_zero(name->value)) {
3977 ir_node *insert_pos = irn;
3978 ir_node *prev = sched_prev(insert_pos);
3981 while(be_is_Spill(prev)) {
3982 prev = sched_prev(prev);
3985 prev_op = get_irn_link(prev);
3987 /* insert reload before pre-remats */
3988 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3989 && prev_op->is_remat && prev_op->attr.remat.pre) {
3993 prev = sched_prev(prev);
3994 } while(be_is_Spill(prev));
3996 prev_op = get_irn_link(prev);
4000 reload = insert_reload(si, arg, insert_pos);
4002 assert(reload && "no reload returned");
4003 set_irn_n(irn, n, reload);
4005 if(opt_keep_alive & KEEPALIVE_RELOADS)
4006 pset_insert_ptr(si->spills, reload);
4013 del_set(spill_bb->ilp);
4014 if(spill_bb->reloads) del_set(spill_bb->reloads);
4018 walker_collect_used(ir_node * irn, void * data)
4020 bitset_t *used = data;
4022 bitset_set(used, get_irn_idx(irn));
4025 struct kill_helper {
4031 walker_kill_unused(ir_node * bb, void * data)
4033 struct kill_helper *kh = data;
4034 ir_node *bad = get_irg_bad(get_irn_irg(bb));
4038 for(irn=sched_first(bb); !sched_is_end(irn);) {
4039 ir_node *next = sched_next(irn);
4042 if(!bitset_is_set(kh->used, get_irn_idx(irn))) {
4043 if(be_is_Spill(irn) || be_is_Reload(irn)) {
4044 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(irn) * execution_frequency(kh->si, bb)));
4046 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
4052 set_nodes_block(irn, bad);
4053 for (n=get_irn_arity(irn)-1; n>=0; --n) {
4054 set_irn_n(irn, n, bad);
4061 #ifndef SCHEDULE_PHIM
4063 kill_unused_phims(spill_ilp_t * si, struct kill_helper * kh)
4066 ir_node *bad = get_irg_bad(si->birg->irg);
4069 pset_foreach(si->phims, phi) {
4070 if(!bitset_is_set(kh->used, get_irn_idx(phi))) {
4072 set_nodes_block(phi, bad);
4073 for (n=get_irn_arity(phi)-1; n>=0; --n) {
4074 set_irn_n(phi, n, bad);
4082 kill_all_unused_values_in_schedule(spill_ilp_t * si)
4084 struct kill_helper kh;
4086 kh.used = bitset_malloc(get_irg_last_idx(si->birg->irg));
4089 irg_walk_graph(si->birg->irg, walker_collect_used, NULL, kh.used);
4090 #ifndef SCHEDULE_PHIM
4091 kill_unused_phims(si, &kh);
4093 irg_block_walk_graph(si->birg->irg, walker_kill_unused, NULL, &kh);
4095 bitset_free(kh.used);
4099 print_irn_pset(pset * p)
4103 pset_foreach(p, irn) {
4104 ir_printf("%+F\n", irn);
4109 dump_phi_class(spill_ilp_t *si, ir_node **phiclass, const char * file)
4111 FILE *f = fopen(file, "w");
4113 interference_t *interference;
4116 set_break(si->interferences);
4118 ir_fprintf(f, "digraph phiclass {\n");
4120 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4123 ir_fprintf(f, " %F%N [shape=box]\n", irn, irn);
4126 for (i = ARR_LEN(phiclass) - 1; i >= 0; --i) {
4133 for (n = get_irn_arity(irn) - 1; n >= 0; --n) {
4134 ir_node *arg = get_irn_n(irn, n);
4136 ir_fprintf(f, " %F%N -> %F%N\n", irn, irn, arg, arg);
4140 set_foreach(si->interferences, interference) {
4141 const ir_node *a = interference->a;
4142 const ir_node *b = interference->b;
4143 if (get_phi_class(si->pc, (ir_node *)a) == phiclass) {
4144 ir_fprintf(f, " %F%N -> %F%N [color=red,dir=none,style=bold]\n", a, a, b, b);
4153 rewire_uses(spill_ilp_t * si)
4156 ir_nodeset_t ignore;
4158 ir_nodeset_init(&ignore);
4159 ir_nodeset_insert(&ignore, get_irg_end(si->birg->irg));
4161 /* then fix uses of spills */
4162 set_foreach(si->values, defs) {
4165 const ir_node *next = defs->remats;
4168 reloads = pset_new_ptr_default();
4171 if(be_is_Reload(next)) {
4172 pset_insert_ptr(reloads, next);
4176 next = get_irn_link(next);
4179 spills = get_spills_for_value(si, defs->value);
4180 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));
4181 if(pset_count(spills) > 1) {
4182 be_ssa_construction_env_t senv;
4184 //assert(pset_count(reloads) > 0);
4185 // print_irn_pset(spills);
4186 // print_irn_pset(reloads);
4188 be_ssa_construction_init(&senv, si->birg);
4189 be_ssa_construction_set_ignore_uses(&senv, &ignore);
4190 pset_foreach(spills, node) {
4191 be_ssa_construction_add_copy(&senv, node);
4193 pset_foreach(spills, node) {
4194 be_ssa_construction_fix_users(&senv, node);
4196 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4197 pset_foreach(spills, node) {
4198 be_liveness_update(si->lv, node);
4200 be_ssa_construction_destroy(&senv);
4207 /* first fix uses of remats and reloads */
4208 set_foreach(si->values, defs) {
4209 const ir_node *next = defs->remats;
4213 be_ssa_construction_env_t senv;
4215 be_ssa_construction_init(&senv, si->birg);
4217 if(sched_is_scheduled(defs->value)) {
4218 be_ssa_construction_add_copy(&senv, (ir_node*) defs->value);
4222 next = defs->remats;
4224 be_ssa_construction_add_copy(&senv, (ir_node*) next);
4225 next = get_irn_link(next);
4228 if(sched_is_scheduled(defs->value)) {
4229 be_ssa_construction_fix_users(&senv, (ir_node*) defs->value);
4232 next = defs->remats;
4234 be_ssa_construction_fix_users(&senv, (ir_node*) next);
4235 next = get_irn_link(next);
4238 be_ssa_construction_update_liveness_phis(&senv, si->lv);
4239 if(sched_is_scheduled(defs->value)) {
4240 be_liveness_update(si->lv, (ir_node*) defs->value);
4243 next = defs->remats;
4245 be_liveness_update(si->lv, (ir_node*) next);
4246 next = get_irn_link(next);
4249 be_ssa_construction_destroy(&senv);
4253 ir_nodeset_destroy(&ignore);
4254 // remove_unused_defs(si);
4259 writeback_results(spill_ilp_t * si)
4261 /* walk through the graph and collect all spills, reloads and remats for a value */
4263 si->values = new_set(cmp_defs, 4096);
4265 DBG((si->dbg, LEVEL_1, "Applying results\n"));
4266 delete_unnecessary_remats(si);
4267 si->m_unknown = new_r_Unknown(si->birg->irg, mode_M);
4268 irg_block_walk_graph(si->birg->irg, walker_spill_placer, NULL, si);
4269 irg_block_walk_graph(si->birg->irg, walker_reload_placer, NULL, si);
4271 insert_memoperands(si);
4274 /* clean the remat info! there are still back-edges leading there! */
4275 clean_remat_info(si);
4279 connect_all_spills_with_keep(si);
4281 del_set(si->values);
4285 get_n_regs(spill_ilp_t * si)
4287 int arch_n_regs = arch_register_class_n_regs(si->cls);
4289 bitset_t *arch_regs = bitset_malloc(arch_n_regs);
4290 bitset_t *abi_regs = bitset_malloc(arch_n_regs);
4292 arch_put_non_ignore_regs(si->cls, arch_regs);
4293 be_abi_put_ignore_regs(si->birg->abi, si->cls, abi_regs);
4295 bitset_andnot(arch_regs, abi_regs);
4296 arch_n_regs = bitset_popcnt(arch_regs);
4298 bitset_free(arch_regs);
4299 bitset_free(abi_regs);
4301 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", arch_n_regs, si->cls->name));
4306 walker_reload_mover(ir_node * bb, void * data)
4308 spill_ilp_t *si = data;
4311 sched_foreach(bb, tmp) {
4312 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
4313 ir_node *reload = tmp;
4316 /* move reload upwards */
4318 int pressure = (int)get_irn_link(reload);
4319 if(pressure < si->n_regs) {
4320 irn = sched_prev(reload);
4321 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
4322 sched_remove(reload);
4323 pressure = (int)get_irn_link(irn);
4325 while(pressure < si->n_regs) {
4326 if( sched_is_end(irn) ||
4327 (be_is_Reload(irn) && has_reg_class(si, irn)) ||
4328 /* do not move reload before its spill */
4329 (irn == be_get_Reload_mem(reload)) ||
4330 /* do not move before phi */
4333 set_irn_link(irn, INT_TO_PTR(pressure+1));
4334 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
4335 irn = sched_prev(irn);
4337 pressure = (int)get_irn_link(irn);
4340 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
4341 sched_put_after(irn, reload);
4348 move_reloads_upward(spill_ilp_t * si)
4350 irg_block_walk_graph(si->birg->irg, walker_reload_mover, NULL, si);
4355 * Walk all irg blocks and check for interfering spills inside of phi classes
4358 luke_meminterferencechecker(ir_node * bb, void * data)
4360 spill_ilp_t *si = (spill_ilp_t*)data;
4363 be_lv_foreach(si->lv, bb, be_lv_state_end | be_lv_state_out | be_lv_state_in, l1) {
4364 ir_node *a = be_lv_get_irn(si->lv, bb, l1);
4366 if (! be_is_Spill(a) && (!is_Phi(a) || get_irn_mode(a) != mode_T))
4369 /* a is only interesting if it is in my register class and if it is inside a phi class */
4370 if (has_reg_class(si, a) && get_phi_class(si->pc, a)) {
4371 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)) {
4372 ir_node *b = be_lv_get_irn(si->lv, bb, l2);
4374 if (! be_is_Spill(b) && (! is_Phi(b) || get_irn_mode(b) != mode_T))
4377 /* a and b are only interesting if they are in the same phi class */
4378 if (has_reg_class(si, b) && get_phi_class(si->pc, a) == get_phi_class(si->pc, b)) {
4379 if (values_interfere_in_block(si, bb, a, b)) {
4380 ir_fprintf(stderr, "$$ Spills interfere in %+F: %+F, %+F \t$$\n", bb, a, b);
4389 verify_phiclasses(spill_ilp_t * si)
4391 /* analyze phi classes */
4392 phi_class_free(si->pc);
4393 si->pc = phi_class_new_from_irg(si->birg->irg, 0);
4395 DBG((si->dbg, LEVEL_2, "\t calling memory interference checker\n"));
4396 irg_block_walk_graph(si->birg->irg, luke_meminterferencechecker, NULL, si);
4400 be_spill_remat(be_irg_t *birg, const arch_register_class_t *cls)
4403 char problem_name[256];
4404 char dump_suffix[256];
4405 char dump_suffix2[256];
4406 struct obstack obst;
4408 ir_graph *irg = be_get_birg_irg(birg);
4410 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", irg, cls->name);
4411 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", cls->name);
4412 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", cls->name);
4414 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
4415 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
4417 if(opt_verify & VERIFY_DOMINANCE)
4418 be_check_dominance(irg);
4420 be_assure_liveness(birg);
4422 obstack_init(&obst);
4426 si.lpp = new_lpp(problem_name, lpp_minimize);
4427 si.remat_info = new_set(cmp_remat_info, 4096);
4428 si.interferences = new_set(cmp_interference, 32);
4429 si.memoperands = new_set(cmp_memoperands, 128);
4430 si.all_possible_remats = pset_new_ptr_default();
4431 si.spills = pset_new_ptr_default();
4432 si.inverse_ops = pset_new_ptr_default();
4435 si.n_regs = get_n_regs(&si);
4437 set_irg_link(irg, &si);
4440 /* compute phi classes */
4441 // phi_class_compute(irg);
4443 if(opt_dump_flags & DUMP_STATS)
4444 be_analyze_regpressure(birg, cls, "-pre");
4446 DBG((si.dbg, LEVEL_2, "\t initializing\n"));
4447 irg_block_walk_graph(irg, luke_initializer, NULL, &si);
4450 /* collect remats */
4451 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
4452 irg_walk_graph(irg, walker_remat_collector, NULL, &si);
4455 /* insert possible remats */
4456 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
4457 irg_block_walk_graph(irg, walker_remat_insertor, NULL, &si);
4458 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
4460 if(opt_keep_alive & KEEPALIVE_REMATS) {
4461 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
4462 connect_all_remats_with_keep(&si);
4463 /* dump graph with inserted remats */
4464 dump_graph_with_remats(irg, dump_suffix);
4467 /* insert copies for phi arguments not in my regclass */
4468 irg_walk_graph(irg, walker_regclass_copy_insertor, NULL, &si);
4470 /* recompute liveness */
4471 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
4472 be_liveness_recompute(si.lv);
4475 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
4476 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
4477 irg_block_walk_graph(irg, luke_endwalker, NULL, &si);
4479 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
4480 irg_block_walk_graph(irg, luke_blockwalker, NULL, &si);
4482 si.pc = phi_class_new_from_irg(birg->irg, 0);
4483 if (opt_memcopies) {
4484 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
4485 memcopyhandler(&si);
4488 if (opt_dump_flags & DUMP_PROBLEM) {
4490 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
4491 if ((f = fopen(buf, "wt")) != NULL) {
4492 lpp_dump_plain(si.lpp, f);
4497 if (opt_dump_flags & DUMP_MPS) {
4500 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mps", problem_name);
4501 if ((f = fopen(buf, "wt")) != NULL) {
4502 mps_write_mps(si.lpp, s_mps_fixed, f);
4506 ir_snprintf(buf, sizeof(buf), "%s-spillremat.mst", problem_name);
4507 if ((f = fopen(buf, "wt")) != NULL) {
4508 mps_write_mst(si.lpp, s_mps_fixed, f);
4513 lpp_check_startvals(si.lpp);
4516 DBG((si.dbg, LEVEL_1, "\tSolving %s (%d variables, %d constraints)\n", problem_name, si.lpp->var_next, si.lpp->cst_next));
4517 lpp_set_time_limit(si.lpp, opt_timeout);
4520 lpp_set_log(si.lpp, stdout);
4523 lpp_solve_cplex(si.lpp);
4525 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
4527 assert(lpp_is_sol_valid(si.lpp)
4528 && "solution of ILP must be valid");
4530 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));
4532 if(opt_dump_flags & DUMP_SOLUTION) {
4536 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
4537 if ((f = fopen(buf, "wt")) != NULL) {
4539 for (i = 0; i < si.lpp->var_next; ++i) {
4540 lpp_name_t *name = si.lpp->vars[i];
4541 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
4547 #ifndef SCHEDULE_PHIM
4548 si.phims = pset_new_ptr_default();
4550 writeback_results(&si);
4555 kill_all_unused_values_in_schedule(&si);
4557 #if !defined(SCHEDULE_PHIM) && defined(SOLVE)
4561 if(opt_keep_alive & (KEEPALIVE_SPILLS | KEEPALIVE_RELOADS))
4562 be_dump(irg, "-spills-placed", dump_ir_block_graph);
4564 // move reloads upwards
4565 be_liveness_recompute(si.lv);
4566 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4567 move_reloads_upward(&si);
4570 verify_phiclasses(&si);
4573 irg_block_walk_graph(irg, walker_pressure_annotator, NULL, &si);
4575 if(opt_dump_flags & DUMP_PRESSURE)
4576 dump_pressure_graph(&si, dump_suffix2);
4578 if(opt_dump_flags & DUMP_STATS)
4579 be_analyze_regpressure(birg, cls, "-post");
4581 if(opt_verify & VERIFY_DOMINANCE)
4582 be_check_dominance(irg);
4585 del_set(si.interferences);
4586 del_pset(si.inverse_ops);
4587 del_pset(si.all_possible_remats);
4588 del_set(si.memoperands);
4589 del_pset(si.spills);
4591 phi_class_free(si.pc);
4592 obstack_free(&obst, NULL);
4593 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
4596 void be_init_spillremat(void)
4598 static be_spiller_t remat_spiller = {
4601 lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
4602 lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
4603 lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
4604 lc_opt_entry_t *remat_grp = lc_opt_get_grp(chordal_grp, "remat");
4606 be_register_spiller("remat", &remat_spiller);
4607 lc_opt_add_table(remat_grp, options);
4610 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillremat);
4612 #else /* WITH_ILP */
4614 static __attribute__((unused))
4615 void only_that_you_can_compile_without_WITH_ILP_defined(void)
4619 #endif /* WITH_ILP */