1 /** vim: set sw=4 ts=4:
4 * @author Adam M. Szalkowski & Sebastian Hack
6 * ILP based spilling & rematerialization
8 * Copyright (C) 2006 Universitaet Karlsruhe
9 * Released under the GPL
37 #include <lpp/lpp_net.h>
38 #include <lpp/lpp_cplex.h>
39 //#include <lc_pset.h>
40 #include <libcore/lc_bitset.h>
44 #include "besched_t.h"
49 #include "bespillremat.h"
51 #include "bepressurestat.h"
53 #include "bechordal_t.h"
59 //#define KEEPALIVE /* keep alive all inserted remats and dump graph with remats */
60 #define COLLECT_REMATS /* enable rematerialization */
61 #define COLLECT_INVERSE_REMATS /* enable placement of inverse remats */
62 #define REMAT_WHILE_LIVE /* only remat values that are live */
63 //#define NO_ENLARGE_L1V3N355 /* do not remat after the death of some operand */
64 //#define EXECFREQ_LOOPDEPH /* compute execution frequency from loop depth only */
65 #define MAY_DIE_AT_REMAT /* allow values to die after a pre remat */
66 #define NO_SINGLE_USE_REMATS /* do not repair schedule */
67 //#define KEEPALIVE_SPILLS
68 //#define KEEPALIVE_RELOADS
69 #define GOODWIN_REDUCTION
74 #define LPP_SERVER "i44pc52"
75 #define LPP_SOLVER "cplex"
81 #define ILP_TIMEOUT 120
85 typedef struct _spill_ilp_t {
86 const arch_register_class_t *cls;
88 const be_chordal_env_t *chordal_env;
92 pset *all_possible_remats;
97 set *values; /**< for collecting all definitions of values before running ssa-construction */
102 DEBUG_ONLY(firm_dbg_module_t * dbg);
105 typedef int ilp_var_t;
106 typedef int ilp_cst_t;
108 typedef struct _spill_bb_t {
113 typedef struct _remat_t {
114 const ir_node *op; /**< for copy_irn */
115 const ir_node *value; /**< the value which is being recomputed by this remat */
116 ir_node *proj; /**< not NULL if the above op produces a tuple */
117 int cost; /**< cost of this remat */
118 int inverse; /**< nonzero if this is an inverse remat */
122 * Data to be attached to each IR node. For remats this contains the ilp_var
123 * for this remat and for normal ops this contains the ilp_vars for
124 * reloading each operand
126 typedef struct _op_t {
131 remat_t *remat; /** the remat this op belongs to */
132 int pre; /** 1, if this is a pressure-increasing remat */
136 ir_node *op; /** the operation this live range belongs to */
145 typedef struct _defs_t {
147 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
148 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
151 typedef struct _remat_info_t {
152 const ir_node *irn; /**< the irn to which these remats belong */
153 pset *remats; /**< possible remats for this value */
154 pset *remats_by_operand; /**< remats with this value as operand */
157 typedef struct _keyval_t {
162 typedef struct _spill_t {
172 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
174 return chordal_has_class(si->chordal_env, irn);
179 cmp_remat(const void *a, const void *b)
181 const keyval_t *p = a;
182 const keyval_t *q = b;
183 const remat_t *r = p->val;
184 const remat_t *s = q->val;
188 return !(r == s || r->op == s->op);
192 cmp_remat(const void *a, const void *b)
194 const remat_t *r = a;
195 const remat_t *s = a;
197 return !(r == s || r->op == s->op);
201 cmp_spill(const void *a, const void *b, size_t size)
203 const spill_t *p = a;
204 const spill_t *q = b;
206 // return !(p->irn == q->irn && p->bb == q->bb);
207 return !(p->irn == q->irn);
211 set_find_keyval(set * set, void * key)
216 return set_find(set, &query, sizeof(query), HASH_PTR(key));
220 set_insert_keyval(set * set, void * key, void * val)
226 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
230 set_find_def(set * set, ir_node * value)
235 return set_find(set, &query, sizeof(query), HASH_PTR(value));
239 set_insert_def(set * set, ir_node * value)
246 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
250 set_find_spill(set * set, ir_node * value)
255 return set_find(set, &query, sizeof(query), HASH_PTR(value));
258 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
259 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
260 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
261 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
262 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
265 cmp_remat_info(const void *a, const void *b, size_t size)
267 const remat_info_t *p = a;
268 const remat_info_t *q = b;
270 return !(p->irn == q->irn);
274 cmp_defs(const void *a, const void *b, size_t size)
279 return !(p->value == q->value);
283 cmp_keyval(const void *a, const void *b, size_t size)
285 const keyval_t *p = a;
286 const keyval_t *q = b;
288 return !(p->key == q->key);
292 execution_frequency(const spill_ilp_t * si, const ir_node * irn)
297 return get_block_execfreq(si->execfreqs, irn) + FUDGE;
299 return get_block_execfreq(si->execfreqs, get_nodes_block(irn)) + FUDGE;
303 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
305 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
310 get_cost(const spill_ilp_t * si, const ir_node * irn)
312 if(be_is_Spill(irn)) {
314 } else if(be_is_Reload(irn)){
317 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
323 * Checks, whether node and its operands have suitable reg classes
326 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
329 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
330 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
334 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
337 for (n = get_irn_arity(irn)-1; n>=0 && remat; --n) {
338 ir_node *op = get_irn_n(irn, n);
339 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
342 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
349 * Try to create a remat from @p op with destination value @p dest_value
351 static INLINE remat_t *
352 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
354 remat_t *remat = NULL;
356 // if(!mode_is_datab(get_irn_mode(dest_value)))
359 if(dest_value == op) {
360 const ir_node *proj = NULL;
362 if(is_Proj(dest_value)) {
363 op = get_irn_n(op, 0);
367 if(!is_rematerializable(si, op))
370 remat = obstack_alloc(si->obst, sizeof(*remat));
372 remat->cost = get_cost(si, op);
373 remat->value = dest_value;
377 arch_inverse_t inverse;
380 /* get the index of the operand we want to retrieve by the inverse op */
381 for (n = get_irn_arity(op)-1; n>=0; --n) {
382 ir_node *arg = get_irn_n(op, n);
384 if(arg == dest_value) break;
388 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", n, op));
390 /* else ask the backend to give an inverse op */
391 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, n, &inverse, si->obst)) {
394 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
396 assert(inverse.n > 0 && "inverse op should have at least one node");
398 for(i=inverse.n-1; i>=0; --i) {
399 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
403 remat = obstack_alloc(si->obst, sizeof(*remat));
404 remat->op = inverse.nodes[0];
405 remat->cost = inverse.costs;
406 remat->value = dest_value;
407 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
410 assert(is_Proj(remat->proj));
412 assert(0 && "I can not handle remats with more than 2 nodes");
419 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
421 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
429 add_remat(const spill_ilp_t * si, const remat_t * remat)
431 remat_info_t *remat_info,
436 assert(remat->value);
438 query.irn = remat->value;
440 query.remats_by_operand = NULL;
441 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
443 if(remat_info->remats == NULL) {
444 remat_info->remats = new_pset(cmp_remat, 4096);
446 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
448 /* insert the remat into the remats_be_operand set of each argument of the remat op */
449 for (n = get_irn_arity(remat->op)-1; n>=0; --n) {
450 ir_node *arg = get_irn_n(remat->op, n);
454 query.remats_by_operand = NULL;
455 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
457 if(remat_info->remats_by_operand == NULL) {
458 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
460 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
465 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
467 const ir_edge_t *edge = get_irn_out_edge_first(irn);
471 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
474 edge = get_irn_out_edge_next(irn, edge);
481 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
486 #ifdef NO_SINGLE_USE_REMATS
487 if(has_reg_class(si, op) && (get_irn_n_nonremat_edges(si, op) > 1)) {
489 if(has_reg_class(si, op)) {
491 remat = get_remat_from_op(si, op, op);
493 add_remat(si, remat);
497 #ifdef COLLECT_INVERSE_REMATS
498 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
500 for (n = get_irn_arity(op)-1; n>=0; --n) {
501 ir_node *arg = get_irn_n(op, n);
503 if(has_reg_class(si, arg)) {
504 /* try to get an inverse remat */
505 remat = get_remat_from_op(si, arg, op);
507 add_remat(si, remat);
516 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
519 ir_node *def_block = get_nodes_block(val);
525 /* if pos is at end of a basic block */
527 ret = (pos == def_block || block_dominates(def_block, pos));
528 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
532 /* else if this is a normal operation */
533 block = get_nodes_block(pos);
534 if(block == def_block) {
535 if(!sched_is_scheduled(val)) return 1;
537 ret = sched_comes_after(val, pos);
538 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
542 ret = block_dominates(def_block, block);
543 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
547 static INLINE ir_node *
548 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
550 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
554 * Returns first non-Phi node of block @p bb
556 static INLINE ir_node *
557 sched_block_first_nonphi(const ir_node * bb)
559 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
563 sched_skip_proj_predicator(const ir_node * irn, void * data)
565 return (is_Proj(irn));
568 static INLINE ir_node *
569 sched_next_nonproj(const ir_node * irn, int forward)
571 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
575 * Returns next operation node (non-Proj) after @p irn
576 * or the basic block of this node
578 static INLINE ir_node *
579 sched_next_op(const ir_node * irn)
581 ir_node *next = sched_next(irn);
586 return sched_next_nonproj(next, 1);
590 * Returns previous operation node (non-Proj) before @p irn
591 * or the basic block of this node
593 static INLINE ir_node *
594 sched_prev_op(const ir_node * irn)
596 ir_node *prev = sched_prev(irn);
601 return sched_next_nonproj(prev, 0);
605 sched_put_after(ir_node * insert, ir_node * irn)
607 if(is_Block(insert)) {
608 insert = sched_block_first_nonphi(insert);
610 insert = sched_next_op(insert);
612 sched_add_before(insert, irn);
616 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
618 if(is_Block(insert)) {
619 insert = sched_block_last_noncf(si, insert);
621 insert = sched_next_nonproj(insert, 0);
622 insert = sched_prev(insert);
624 sched_add_after(insert, irn);
628 * Tells you whether a @p remat can be placed before the irn @p pos
631 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
633 const ir_node *op = remat->op;
639 prev = sched_block_last_noncf(si, pos);
640 prev = sched_next_nonproj(prev, 0);
642 prev = sched_prev_op(pos);
644 /* do not remat if the rematted value is defined immediately before this op */
645 if(prev == remat->op) {
650 /* this should be just fine, the following OP will be using this value, right? */
652 /* only remat AFTER the real definition of a value (?) */
653 if(!value_is_defined_before(si, pos, remat->value)) {
654 // ir_fprintf(stderr, "error(not defined)");
659 for(n=get_irn_arity(op)-1; n>=0 && res; --n) {
660 const ir_node *arg = get_irn_n(op, n);
662 #ifdef NO_ENLARGE_L1V3N355
663 if(has_reg_class(si, arg) && live) {
664 res &= pset_find_ptr(live, arg)?1:0;
666 res &= value_is_defined_before(si, pos, arg);
669 res &= value_is_defined_before(si, pos, arg);
677 * Tells you whether a @p remat can be placed after the irn @p pos
680 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
683 pos = sched_block_first_nonphi(pos);
685 pos = sched_next_op(pos);
688 /* only remat AFTER the real definition of a value (?) */
689 if(!value_is_defined_before(si, pos, remat->value)) {
693 return can_remat_before(si, remat, pos, live);
697 * Collect potetially rematerializable OPs
700 walker_remat_collector(ir_node * irn, void * data)
702 spill_ilp_t *si = data;
704 if(!is_Block(irn) && !is_Phi(irn)) {
705 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
706 get_remats_from_op(si, irn);
711 * Inserts a copy of @p irn before @p pos
714 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
719 bb = is_Block(pos)?pos:get_nodes_block(pos);
720 copy = exact_copy(irn);
721 set_nodes_block(copy, bb);
722 sched_put_before(si, pos, copy);
728 * Inserts a copy of @p irn after @p pos
731 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
736 bb = is_Block(pos)?pos:get_nodes_block(pos);
737 copy = exact_copy(irn);
738 set_nodes_block(copy, bb);
739 sched_put_after(pos, copy);
745 insert_remat_after(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
749 if(can_remat_after(si, remat, pos, live)) {
754 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
756 copy = insert_copy_after(si, remat->op, pos);
758 // ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", remat->value, pos);
759 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
760 op = obstack_alloc(si->obst, sizeof(*op));
762 op->attr.remat.remat = remat;
763 op->attr.remat.pre = 0;
764 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
766 set_irn_link(copy, op);
767 pset_insert_ptr(si->all_possible_remats, copy);
769 proj_copy = insert_copy_after(si, remat->proj, copy);
770 set_irn_n(proj_copy, 0, copy);
771 set_irn_link(proj_copy, op);
772 pset_insert_ptr(si->all_possible_remats, proj_copy);
784 insert_remat_before(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
788 if(can_remat_before(si, remat, pos, live)) {
793 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
795 copy = insert_copy_before(si, remat->op, pos);
797 // ir_snprintf(buf, sizeof(buf), "remat_%N_%N", remat->value, pos);
798 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
799 op = obstack_alloc(si->obst, sizeof(*op));
801 op->attr.remat.remat = remat;
802 op->attr.remat.pre = 1;
803 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
805 set_irn_link(copy, op);
806 pset_insert_ptr(si->all_possible_remats, copy);
808 proj_copy = insert_copy_after(si, remat->proj, copy);
809 set_irn_n(proj_copy, 0, copy);
810 set_irn_link(proj_copy, op);
811 pset_insert_ptr(si->all_possible_remats, proj_copy);
823 get_block_n_succs(const ir_node *block) {
824 const ir_edge_t *edge;
826 assert(edges_activated(current_ir_graph));
828 edge = get_block_succ_first(block);
832 edge = get_block_succ_next(block, edge);
837 is_merge_edge(const ir_node * bb)
839 #ifdef GOODWIN_REDUCTION
840 return get_block_n_succs(bb) == 1;
847 is_diverge_edge(const ir_node * bb)
849 #ifdef GOODWIN_REDUCTION
850 return get_Block_n_cfgpreds(bb) == 1;
857 walker_regclass_copy_insertor(ir_node * irn, void * data)
859 spill_ilp_t *si = data;
861 if(is_Phi(irn) && has_reg_class(si, irn)) {
864 for(n=get_irn_arity(irn)-1; n>=0; --n) {
865 ir_node *phi_arg = get_irn_n(irn, n);
866 ir_node *bb = get_Block_cfgpred_block(get_nodes_block(irn), n);
868 if(!has_reg_class(si, phi_arg)) {
869 ir_node *copy = be_new_Copy(si->cls, si->chordal_env->irg, bb, phi_arg);
870 ir_node *pos = sched_block_last_noncf(si, bb);
871 op_t *op = obstack_alloc(si->obst, sizeof(*op));
873 DBG((si->dbg, LEVEL_2, "\t copy to my regclass for arg %+F of %+F\n", phi_arg, irn));
874 sched_add_after(pos, copy);
875 set_irn_n(irn, n, copy);
878 op->attr.live_range.args.reloads = NULL;
879 op->attr.live_range.ilp = ILP_UNDEF;
880 set_irn_link(copy, op);
888 * Insert (so far unused) remats into the irg to
889 * recompute the potential liveness of all values
892 walker_remat_insertor(ir_node * bb, void * data)
894 spill_ilp_t *si = data;
895 spill_bb_t *spill_bb;
899 pset *live = pset_new_ptr_default();
901 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
903 live_foreach(bb, li) {
904 ir_node *value = (ir_node *) li->irn;
906 /* add remats at end of block */
907 if (live_is_end(li) && has_reg_class(si, value)) {
908 pset_insert_ptr(live, value);
912 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
913 set_irn_link(bb, spill_bb);
915 irn = sched_last(bb);
916 while(!sched_is_end(irn)) {
923 next = sched_prev(irn);
925 DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next));
927 if(is_Phi(irn) || is_Proj(irn)) {
930 if(has_reg_class(si, irn)) {
931 pset_remove_ptr(live, irn);
934 op = obstack_alloc(si->obst, sizeof(*op));
936 op->attr.live_range.args.reloads = NULL;
937 op->attr.live_range.ilp = ILP_UNDEF;
938 set_irn_link(irn, op);
944 op = obstack_alloc(si->obst, sizeof(*op));
946 op->attr.live_range.ilp = ILP_UNDEF;
947 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
948 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
949 set_irn_link(irn, op);
951 args = pset_new_ptr_default();
953 /* collect arguments of op */
954 for (n = get_irn_arity(irn)-1; n>=0; --n) {
955 ir_node *arg = get_irn_n(irn, n);
957 pset_insert_ptr(args, arg);
960 /* set args of op already live in epilog */
961 pset_foreach(args, arg) {
962 if(has_reg_class(si, arg)) {
963 pset_insert_ptr(live, arg);
967 remat_args = pset_new_ptr_default();
969 /* insert all possible remats before irn */
970 pset_foreach(args, arg) {
971 remat_info_t *remat_info,
975 /* continue if the operand has the wrong reg class
977 if(!has_reg_class(si, arg))
982 query.remats_by_operand = NULL;
983 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
989 if(remat_info->remats) {
990 pset_foreach(remat_info->remats, remat) {
991 ir_node *remat_irn = NULL;
993 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
994 #ifdef REMAT_WHILE_LIVE
995 if(pset_find_ptr(live, remat->value)) {
996 remat_irn = insert_remat_before(si, remat, irn, live);
999 remat_irn = insert_remat_before(si, remat, irn, live);
1002 for(n=get_irn_arity(remat_irn)-1; n>=0; --n) {
1003 ir_node *remat_arg = get_irn_n(remat_irn, n);
1005 if(!has_reg_class(si, remat_arg)) continue;
1007 pset_insert_ptr(remat_args, remat_arg);
1014 /* now we add remat args to op's args because they could also die at this op */
1015 pset_foreach(args,arg) {
1016 if(pset_find_ptr(remat_args, arg)) {
1017 pset_remove_ptr(remat_args, arg);
1020 pset_foreach(remat_args,arg) {
1021 pset_insert_ptr(args, arg);
1024 /* insert all possible remats after irn */
1025 pset_foreach(args, arg) {
1026 remat_info_t *remat_info,
1030 /* continue if the operand has the wrong reg class */
1031 if(!has_reg_class(si, arg))
1035 query.remats = NULL;
1036 query.remats_by_operand = NULL;
1037 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
1043 /* do not place post remats after jumps */
1044 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue;
1046 if(remat_info->remats_by_operand) {
1047 pset_foreach(remat_info->remats_by_operand, remat) {
1048 /* do not insert remats producing the same value as one of the operands */
1049 if(!pset_find_ptr(args, remat->value)) {
1050 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
1051 #ifdef REMAT_WHILE_LIVE
1052 if(pset_find_ptr(live, remat->value)) {
1053 insert_remat_after(si, remat, irn, live);
1056 insert_remat_after(si, remat, irn, live);
1063 /* delete defined value from live set */
1064 if(has_reg_class(si, irn)) {
1065 pset_remove_ptr(live, irn);
1068 del_pset(remat_args);
1073 live_foreach(bb, li) {
1074 ir_node *value = (ir_node *) li->irn;
1076 /* add remats at end if successor has multiple predecessors */
1077 if(is_merge_edge(bb)) {
1078 /* add remats at end of block */
1079 if (live_is_end(li) && has_reg_class(si, value)) {
1080 remat_info_t *remat_info,
1085 query.remats = NULL;
1086 query.remats_by_operand = NULL;
1087 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1089 if(remat_info && remat_info->remats) {
1090 pset_foreach(remat_info->remats, remat) {
1091 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1093 insert_remat_before(si, remat, bb, NULL);
1098 if(is_diverge_edge(bb)) {
1099 /* add remat2s at beginning of block */
1100 if ((live_is_in(li) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
1101 remat_info_t *remat_info,
1106 query.remats = NULL;
1107 query.remats_by_operand = NULL;
1108 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1110 if(remat_info && remat_info->remats) {
1111 pset_foreach(remat_info->remats, remat) {
1112 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at beginning of block %+F\n", remat->op, bb));
1114 /* put the remat here if all its args are available */
1115 insert_remat_after(si, remat, bb, NULL);
1125 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1128 luke_endwalker(ir_node * bb, void * data)
1130 spill_ilp_t *si = (spill_ilp_t*)data;
1137 spill_bb_t *spill_bb = get_irn_link(bb);
1140 live = pset_new_ptr_default();
1141 use_end = pset_new_ptr_default();
1143 live_foreach(bb, li) {
1144 irn = (ir_node *) li->irn;
1145 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1148 pset_insert_ptr(live, irn);
1149 op = get_irn_link(irn);
1150 assert(!op->is_remat);
1154 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1155 /* their reg_out must always be set */
1156 sched_foreach_reverse(bb, irn) {
1159 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1161 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1162 ir_node *irn_arg = get_irn_n(irn, n);
1164 if(has_reg_class(si, irn_arg)) {
1165 pset_insert_ptr(use_end, irn_arg);
1170 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1171 //cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1172 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1174 spill_bb->ilp = new_set(cmp_spill, pset_count(live)+pset_count(use_end));
1176 /* if this is a merge edge we can reload at the end of this block */
1177 if(is_merge_edge(bb)) {
1178 spill_bb->reloads = new_set(cmp_keyval, pset_count(live)+pset_count(use_end));
1179 } else if(pset_count(use_end)){
1180 spill_bb->reloads = new_set(cmp_keyval, pset_count(use_end));
1182 spill_bb->reloads = NULL;
1185 pset_foreach(live,irn) {
1191 /* handle values used by control flow nodes later separately */
1192 if(pset_find_ptr(use_end, irn)) continue;
1195 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1197 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1199 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1200 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1201 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1203 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1204 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1206 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1207 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1209 if(is_merge_edge(bb)) {
1213 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1214 reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1215 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1217 /* reload <= mem_out */
1218 rel_cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1219 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1220 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1223 spill->reg_in = ILP_UNDEF;
1224 spill->mem_in = ILP_UNDEF;
1227 pset_foreach(use_end,irn) {
1231 ilp_cst_t end_use_req,
1236 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1238 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1240 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1241 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1242 /* if irn is used at the end of the block, then it is live anyway */
1243 //lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1245 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1246 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1248 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1249 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1251 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1252 reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1253 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1255 /* reload <= mem_out */
1256 rel_cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1257 lpp_set_factor_fast(si->lpp, rel_cst, reload, 1.0);
1258 lpp_set_factor_fast(si->lpp, rel_cst, spill->mem_out, -1.0);
1260 spill->reg_in = ILP_UNDEF;
1261 spill->mem_in = ILP_UNDEF;
1263 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1264 end_use_req = lpp_add_cst(si->lpp, buf, lpp_equal, 1);
1265 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1273 next_post_remat(const ir_node * irn)
1278 irn = sched_block_first_nonphi(irn);
1280 irn = sched_next_op(irn);
1283 if(sched_is_end(irn))
1286 op = (op_t*)get_irn_link(irn);
1287 if(op->is_remat && !op->attr.remat.pre) {
1296 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
1302 ret = sched_block_last_noncf(si, irn);
1303 ret = sched_next(ret);
1304 ret = sched_prev_op(ret);
1306 ret = sched_prev_op(irn);
1309 if(sched_is_end(ret) || is_Phi(ret))
1312 op = (op_t*)get_irn_link(ret);
1313 if(op->is_remat && op->attr.remat.pre) {
1321 * Find a remat of value @p value in the epilog of @p pos
1324 find_post_remat(const ir_node * value, const ir_node * pos)
1326 while((pos = next_post_remat(pos)) != NULL) {
1329 op = get_irn_link(pos);
1330 assert(op->is_remat && !op->attr.remat.pre);
1332 if(op->attr.remat.remat->value == value)
1333 return (ir_node*)pos;
1336 const ir_edge_t *edge;
1337 foreach_out_edge(pos, edge) {
1338 ir_node *proj = get_edge_src_irn(edge);
1339 assert(is_Proj(proj));
1349 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1351 spill_bb_t *spill_bb = get_irn_link(bb);
1357 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1359 double spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1361 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1363 spill->reg_out = ILP_UNDEF;
1364 spill->reg_in = ILP_UNDEF;
1365 spill->mem_in = ILP_UNDEF;
1367 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1368 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1370 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1371 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1378 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1383 live_foreach(bb, li) {
1384 irn = (ir_node *) li->irn;
1386 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1387 pset_insert_ptr(live, irn);
1391 irn = sched_last(bb);
1393 /* all values eaten by control flow operations are also live until the end of the block */
1394 sched_foreach_reverse(bb, irn) {
1397 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1399 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1400 ir_node *arg = get_irn_n(irn,i);
1402 if(has_reg_class(si, arg)) {
1403 pset_insert_ptr(live, arg);
1410 * Walk all irg blocks and emit this ILP
1413 luke_blockwalker(ir_node * bb, void * data)
1415 spill_ilp_t *si = (spill_ilp_t*)data;
1420 spill_bb_t *spill_bb = get_irn_link(bb);
1423 pset *defs = pset_new_ptr_default();
1426 live = pset_new_ptr_default();
1428 /****************************************
1429 * B A S I C B L O C K E N D
1430 ***************************************/
1433 /* init live values at end of block */
1434 get_live_end(si, bb, live);
1436 pset_foreach(live, irn) {
1438 ilp_var_t reload = ILP_UNDEF;
1440 spill = set_find_spill(spill_bb->ilp, irn);
1443 if(spill_bb->reloads) {
1444 keyval_t *keyval = set_find_keyval(spill_bb->reloads, irn);
1447 reload = PTR_TO_INT(keyval->val);
1451 op = get_irn_link(irn);
1452 assert(!op->is_remat);
1454 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1455 op->attr.live_range.ilp = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1456 op->attr.live_range.op = bb;
1458 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1459 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1461 /* reg_out - reload - remat - live_range <= 0 */
1462 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1463 if(reload != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1464 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1465 foreach_pre_remat(si, bb, tmp) {
1466 op_t *remat_op = get_irn_link(tmp);
1467 if(remat_op->attr.remat.remat->value == irn) {
1468 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1471 /* maybe we should also assure that reg_out >= live_range etc. */
1475 * start new live ranges for values used by remats at end of block
1476 * and assure the remat args are available
1478 foreach_pre_remat(si, bb, tmp) {
1479 op_t *remat_op = get_irn_link(tmp);
1482 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1483 ir_node *remat_arg = get_irn_n(tmp, n);
1484 op_t *arg_op = get_irn_link(remat_arg);
1487 if(!has_reg_class(si, remat_arg)) continue;
1489 /* if value is becoming live through use by remat */
1490 if(!pset_find_ptr(live, remat_arg)) {
1491 ir_snprintf(buf, sizeof(buf), "lr_%N_end%N", remat_arg, bb);
1492 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1494 arg_op->attr.live_range.ilp = prev_lr;
1495 arg_op->attr.live_range.op = bb;
1497 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat at end of block %+F\n", remat_arg, tmp));
1499 pset_insert_ptr(live, remat_arg);
1500 add_to_spill_bb(si, bb, remat_arg);
1503 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1504 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1505 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1507 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1508 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1510 /* use reload placed for this argument */
1511 if(spill_bb->reloads) {
1512 keyval_t *keyval = set_find_keyval(spill_bb->reloads, remat_arg);
1515 ilp_var_t reload = PTR_TO_INT(keyval->val);
1517 lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1522 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1527 /**************************************
1528 * B A S I C B L O C K B O D Y
1529 **************************************/
1531 sched_foreach_reverse_from(sched_block_last_noncf(si, bb), irn) {
1537 ilp_cst_t check_pre,
1544 /* iterate only until first phi */
1548 op = get_irn_link(irn);
1550 if(op->is_remat) continue;
1551 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1553 /* collect defined values */
1554 if(has_reg_class(si, irn)) {
1555 pset_insert_ptr(defs, irn);
1559 if(is_Proj(irn)) continue;
1562 * init set of irn's arguments
1563 * and all possibly used values around this op
1564 * and values defined by post remats
1566 args = new_set(cmp_keyval, get_irn_arity(irn));
1567 used = pset_new_ptr(pset_count(live) + get_irn_arity(irn));
1568 remat_defs = pset_new_ptr(pset_count(live));
1570 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1571 ir_node *irn_arg = get_irn_n(irn, n);
1572 if(has_reg_class(si, irn_arg)) {
1573 set_insert_keyval(args, irn_arg, (void*)n);
1574 pset_insert_ptr(used, irn_arg);
1577 foreach_post_remat(irn, tmp) {
1578 op_t *remat_op = get_irn_link(tmp);
1580 pset_insert_ptr(remat_defs, remat_op->attr.remat.remat->value);
1582 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1583 ir_node *remat_arg = get_irn_n(tmp, n);
1584 if(has_reg_class(si, remat_arg)) {
1585 pset_insert_ptr(used, remat_arg);
1589 foreach_pre_remat(si, irn, tmp) {
1590 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1591 ir_node *remat_arg = get_irn_n(tmp, n);
1592 if(has_reg_class(si, remat_arg)) {
1593 pset_insert_ptr(used, remat_arg);
1598 /**********************************
1599 * I N E P I L O G O F irn
1600 **********************************/
1602 /* ensure each dying value is used by only one post remat */
1603 pset_foreach(live, tmp) {
1604 ir_node *value = tmp;
1605 op_t *value_op = get_irn_link(value);
1610 foreach_post_remat(irn, remat) {
1611 op_t *remat_op = get_irn_link(remat);
1613 for(n=get_irn_arity(remat)-1; n>=0; --n) {
1614 ir_node *remat_arg = get_irn_n(remat, n);
1616 /* if value is used by this remat add it to constraint */
1617 if(remat_arg == value) {
1619 /* sum remat2s <= 1 + n_remats*live_range */
1620 ir_snprintf(buf, sizeof(buf), "dying_lr_%N_%N", value, irn);
1621 cst = lpp_add_cst(si->lpp, buf, lpp_less, 1.0);
1625 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1631 if(value_op->attr.live_range.ilp != ILP_UNDEF && cst != ILP_UNDEF) {
1632 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, -n_remats);
1638 /* new live ranges for values from L\U defined by post remats */
1639 pset_foreach(live, tmp) {
1640 ir_node *value = tmp;
1641 op_t *value_op = get_irn_link(value);
1643 if(!set_find_keyval(args, value) && !pset_find_ptr(defs, value)) {
1644 ilp_var_t prev_lr = ILP_UNDEF;
1647 if(pset_find_ptr(remat_defs, value)) {
1649 /* next_live_range <= prev_live_range + sum remat2s */
1650 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1651 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1653 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1654 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1656 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1657 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1659 foreach_post_remat(irn, remat) {
1660 op_t *remat_op = get_irn_link(remat);
1662 /* if value is being rematerialized by this remat */
1663 if(value == remat_op->attr.remat.remat->value) {
1664 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1668 value_op->attr.live_range.ilp = prev_lr;
1669 value_op->attr.live_range.op = irn;
1674 /* requirements for post remats and start live ranges from L/U' for values dying here */
1675 foreach_post_remat(irn, tmp) {
1676 op_t *remat_op = get_irn_link(tmp);
1679 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1680 ir_node *remat_arg = get_irn_n(tmp, n);
1681 op_t *arg_op = get_irn_link(remat_arg);
1683 if(!has_reg_class(si, remat_arg)) continue;
1685 /* only for values in L\U (TODO and D?), the others are handled with post_use */
1686 if(!pset_find_ptr(used, remat_arg)) {
1687 /* remat <= live_rang(remat_arg) */
1688 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
1689 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1691 /* if value is becoming live through use by remat2 */
1692 if(!pset_find_ptr(live, remat_arg)) {
1695 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1696 lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1698 arg_op->attr.live_range.ilp = lr;
1699 arg_op->attr.live_range.op = irn;
1701 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
1703 pset_insert_ptr(live, remat_arg);
1704 add_to_spill_bb(si, bb, remat_arg);
1707 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1708 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1713 d = pset_count(defs);
1714 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
1716 /* count how many regs irn needs for arguments */
1717 u = set_count(args);
1720 /* check the register pressure in the epilog */
1721 /* sum_{L\U'} lr + sum_{U'} post_use <= k - |D| */
1722 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
1723 check_post = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - d);
1725 /* add L\U' to check_post */
1726 pset_foreach(live, tmp) {
1727 if(!pset_find_ptr(used, tmp) && !pset_find_ptr(defs, tmp)) {
1728 /* if a live value is not used by irn */
1729 tmp_op = get_irn_link(tmp);
1730 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
1734 /***********************************************************
1735 * 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
1736 **********************************************************/
1739 pset_foreach(used, tmp) {
1745 op_t *arg_op = get_irn_link(arg);
1748 spill = add_to_spill_bb(si, bb, arg);
1750 /* new live range for each used value */
1751 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
1752 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1754 /* the epilog stuff - including post_use, check_post, check_post_remat */
1755 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
1756 post_use = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1758 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
1760 /* arg is live throughout epilog if the next live_range is in a register */
1761 if(pset_find_ptr(live, arg)) {
1762 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
1764 /* post_use >= next_lr + remat */
1765 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1766 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1767 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
1768 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1772 /* if value is not an arg of op and not possibly defined by post remat
1773 * then it may only die and not become live
1775 if(!set_find_keyval(args, arg)) {
1776 /* post_use <= prev_lr */
1777 ir_snprintf(buf, sizeof(buf), "req_post_use_%N_%N", arg, irn);
1778 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1779 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1780 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1782 if(!pset_find_ptr(remat_defs, arg) && pset_find_ptr(live, arg)) {
1783 /* next_lr <= prev_lr */
1784 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", arg, irn);
1785 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1786 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1787 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1793 /* forall post remat which use arg add a similar cst */
1794 foreach_post_remat(irn, remat) {
1797 for (n=get_irn_arity(remat)-1; n>=0; --n) {
1798 ir_node *remat_arg = get_irn_n(remat, n);
1799 op_t *remat_op = get_irn_link(remat);
1801 if(remat_arg == arg) {
1802 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
1804 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1805 cst = lpp_add_cst(si->lpp, buf, lpp_greater, 0.0);
1806 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1807 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1812 /* new live range begins for each used value */
1813 arg_op->attr.live_range.ilp = prev_lr;
1814 arg_op->attr.live_range.op = irn;
1816 /*if(!pset_find_ptr(live, arg)) {
1817 pset_insert_ptr(live, arg);
1818 add_to_spill_bb(si, bb, arg);
1820 pset_insert_ptr(live, arg);
1824 /* just to be sure */
1825 check_post = ILP_UNDEF;
1834 /* check the register pressure in the prolog */
1835 /* sum_{L\U} lr <= k - |U| */
1836 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
1837 check_pre = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - u);
1839 /* for the prolog remove defined values from the live set */
1840 pset_foreach(defs, tmp) {
1841 pset_remove_ptr(live, tmp);
1844 /***********************************************************
1845 * 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
1846 **********************************************************/
1849 set_foreach(args, keyval) {
1851 ir_node *arg = keyval->key;
1852 int i = PTR_TO_INT(keyval->val);
1853 op_t *arg_op = get_irn_link(arg);
1855 spill = set_find_spill(spill_bb->ilp, arg);
1858 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
1859 op->attr.live_range.args.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1861 /* reload <= mem_out */
1862 ir_snprintf(buf, sizeof(buf), "req_reload_%N_%N", arg, irn);
1863 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1864 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1865 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1867 /* requirement: arg must be in register for use */
1868 /* reload + remat + live_range == 1 */
1869 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
1870 cst = lpp_add_cst(si->lpp, buf, lpp_equal, 1.0);
1872 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1873 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1874 foreach_pre_remat(si, irn, tmp) {
1875 op_t *remat_op = get_irn_link(tmp);
1876 if(remat_op->attr.remat.remat->value == arg) {
1877 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1882 /* iterate over L\U */
1883 pset_foreach(live, tmp) {
1884 if(!set_find_keyval(args, tmp)) {
1885 /* if a live value is not used by irn */
1886 tmp_op = get_irn_link(tmp);
1887 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
1892 /* requirements for remats */
1893 /* start new live ranges for values used by remats */
1894 foreach_pre_remat(si, irn, tmp) {
1895 op_t *remat_op = get_irn_link(tmp);
1898 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1899 ir_node *remat_arg = get_irn_n(tmp, n);
1900 op_t *arg_op = get_irn_link(remat_arg);
1903 if(!has_reg_class(si, remat_arg)) continue;
1905 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1906 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1907 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1909 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1910 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1912 /* if remat arg is also used by current op then we can use reload placed for this argument */
1913 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
1914 int index = (int)keyval->val;
1916 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
1924 /*************************
1925 * D O N E W I T H O P
1926 *************************/
1928 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
1930 pset_foreach(live, tmp) {
1931 assert(has_reg_class(si, tmp));
1934 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1935 ir_node *arg = get_irn_n(irn, n);
1937 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
1940 del_pset(remat_defs);
1944 defs = pset_new_ptr_default();
1949 /***************************************
1950 * B E G I N N I N G O F B L O C K
1951 ***************************************/
1954 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
1955 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
1957 pset_foreach(live, irn) {
1958 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
1961 /* construct mem_outs for all values */
1963 set_foreach(spill_bb->ilp, spill) {
1964 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
1965 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1967 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
1968 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
1970 if(pset_find_ptr(live, spill->irn)) {
1971 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
1973 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
1974 spill->mem_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1975 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1977 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
1979 op_t *op = get_irn_link(spill->irn);
1981 /* do we have to copy a phi argument? */
1982 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1983 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1985 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
1986 const ir_node *arg = get_irn_n(spill->irn, n);
1992 /* argument already done? */
1993 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
1995 /* get sum of execution frequencies of blocks with the same phi argument */
1996 for(m=n; m>=0; --m) {
1997 const ir_node *arg2 = get_irn_n(spill->irn, m);
2000 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
2004 /* copies are not for free */
2005 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
2006 var = lpp_add_var(si->lpp, buf, lpp_binary, COST_STORE * freq);
2008 for(m=n; m>=0; --m) {
2009 const ir_node *arg2 = get_irn_n(spill->irn, m);
2012 op->attr.live_range.args.copies[m] = var;
2016 /* copy <= mem_in */
2017 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
2018 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2019 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
2020 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
2027 /* L\U is empty at bb start */
2028 /* arg is live throughout epilog if it is reg_in into this block */
2030 /* check the register pressure at the beginning of the block
2033 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
2034 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
2036 pset_foreach(live, irn) {
2039 spill = set_find_spill(spill_bb->ilp, irn);
2042 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
2043 spill->reg_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2045 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
2047 /* spill + mem_in <= 1 */
2048 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
2049 nospill = lpp_add_cst(si->lpp, buf, lpp_less, 1);
2051 lpp_set_factor_fast(si->lpp, nospill, spill->mem_in, 1.0);
2052 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
2055 foreach_post_remat(bb, irn) {
2056 op_t *remat_op = get_irn_link(irn);
2058 DBG((si->dbg, LEVEL_4, "\t next post remat: %+F\n", irn));
2059 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
2061 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2064 /* forall post remats add requirements */
2065 foreach_post_remat(bb, tmp) {
2068 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2069 ir_node *remat_arg = get_irn_n(tmp, n);
2070 op_t *remat_op = get_irn_link(tmp);
2072 if(!has_reg_class(si, remat_arg)) continue;
2074 spill = set_find_spill(spill_bb->ilp, remat_arg);
2077 /* remat <= reg_in_argument */
2078 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
2079 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2080 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2081 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
2085 /* mem_in/reg_in for live_in values, especially phis and their arguments */
2086 pset_foreach(live, irn) {
2090 spill = set_find_spill(spill_bb->ilp, irn);
2091 assert(spill && spill->irn == irn);
2093 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
2094 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
2097 ir_node *phi_arg = get_Phi_pred(irn, n);
2098 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2099 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2102 /* although the phi is in the right regclass one or more of
2103 * its arguments can be in a different one or at least to
2106 if(has_reg_class(si, phi_arg)) {
2107 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2108 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2109 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2110 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2112 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2113 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2115 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
2118 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2119 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2123 /* else assure the value arrives on all paths in the same resource */
2125 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
2128 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
2129 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
2132 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
2133 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2134 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
2135 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2137 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
2138 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
2140 spill_p = set_find_spill(spill_bb_p->ilp, irn);
2143 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
2144 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2149 /* first live ranges from reg_ins */
2150 pset_foreach(live, irn) {
2151 op_t *op = get_irn_link(irn);
2153 spill = set_find_spill(spill_bb->ilp, irn);
2154 assert(spill && spill->irn == irn);
2156 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2157 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2158 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2159 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2161 foreach_post_remat(bb, tmp) {
2162 op_t *remat_op = get_irn_link(tmp);
2164 if(remat_op->attr.remat.remat->value == irn) {
2165 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2170 /* walk forward now and compute constraints for placing spills */
2171 /* this must only be done for values that are not defined in this block */
2172 /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */
2173 pset_foreach(live, irn) {
2175 * if value is defined in this block we can anways place the spill directly after the def
2176 * -> no constraint necessary
2178 if(!is_Phi(irn) && get_nodes_block(irn) == bb) continue;
2181 spill = set_find_spill(spill_bb->ilp, irn);
2184 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2185 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2187 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2188 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2191 sched_foreach_op(bb, tmp) {
2192 op_t *op = get_irn_link(tmp);
2194 if(is_Phi(tmp)) continue;
2195 assert(!is_Proj(tmp));
2198 ir_node *value = op->attr.remat.remat->value;
2201 /* only collect remats up to the first use of a value */
2202 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2207 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2208 ir_node *arg = get_irn_n(tmp, n);
2211 /* if a value is used stop collecting remats */
2217 if(cst == ILP_UNDEF) break;
2225 typedef struct _irnlist_t {
2226 struct list_head list;
2230 typedef struct _interference_t {
2231 struct list_head blocklist;
2237 cmp_interference(const void *a, const void *b, size_t size)
2239 const interference_t *p = a;
2240 const interference_t *q = b;
2242 return !(p->a == q->a && p->b == q->b);
2245 static interference_t *
2246 set_find_interference(set * set, ir_node * a, ir_node * b)
2248 interference_t query;
2250 query.a = (a>b)?a:b;
2251 query.b = (a>b)?b:a;
2253 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2256 static interference_t *
2257 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2259 interference_t query,
2261 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2265 result = set_find_interference(set, a, b);
2268 list_add(&list->list, &result->blocklist);
2272 query.a = (a>b)?a:b;
2273 query.b = (a>b)?b:a;
2275 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2277 INIT_LIST_HEAD(&result->blocklist);
2278 list_add(&list->list, &result->blocklist);
2284 values_interfere_in_block(ir_node * bb, ir_node * a, ir_node * b)
2286 const ir_edge_t *edge;
2288 if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2289 /* both values are live in, so they interfere */
2293 /* ensure a dominates b */
2294 if(value_dominates(b,a)) {
2300 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2303 /* the following code is stolen from bera.c */
2304 if(is_live_end(bb, a))
2307 foreach_out_edge(a, edge) {
2308 const ir_node *user = edge->src;
2309 if(get_nodes_block(user) == bb
2312 && value_dominates(b, user))
2320 * Walk all irg blocks and collect interfering values inside of phi classes
2323 luke_interferencewalker(ir_node * bb, void * data)
2325 spill_ilp_t *si = (spill_ilp_t*)data;
2329 live_foreach(bb, li1) {
2330 ir_node *a = (ir_node *) li1->irn;
2331 op_t *a_op = get_irn_link(a);
2333 if(a_op->is_remat) continue;
2335 /* a is only interesting if it is in my register class and if it is inside a phi class */
2336 if (has_reg_class(si, a) && get_phi_class(a)) {
2337 for(li2=li1->next; li2; li2 = li2->next) {
2338 ir_node *b = (ir_node *) li2->irn;
2339 op_t *b_op = get_irn_link(b);
2341 if(b_op->is_remat) continue;
2343 /* a and b are only interesting if they are in the same phi class */
2344 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2345 if(values_interfere_in_block(bb, a, b)) {
2346 //DBG((si->dbg, LEVEL_1, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2347 ir_fprintf(stderr, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b);
2348 set_insert_interference(si, si->interferences, a, b, bb);
2356 static unsigned int copy_path_id = 0;
2359 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2366 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2367 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2369 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2371 pset_foreach(copies, ptr) {
2372 copy = PTR_TO_INT(ptr);
2373 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2378 * @parameter copies contains a path of copies which lead us to irn
2379 * @parameter visited contains a set of nodes already visited on this path
2382 find_copy_path(spill_ilp_t * si, ir_node * irn, ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
2385 op_t *op = get_irn_link(irn);
2387 if(op->is_remat) return;
2389 pset_insert_ptr(visited, irn);
2394 /* visit all operands */
2395 for(n=get_irn_arity(irn)-1; n>=0; --n) {
2396 ir_node *arg = get_irn_n(irn, n);
2397 ilp_var_t copy = op->attr.live_range.args.copies[n];
2399 if(!has_reg_class(si, arg)) continue;
2402 pset_insert(copies, INT_TO_PTR(copy), copy);
2403 write_copy_path_cst(si, copies, any_interfere);
2404 pset_remove(copies, INT_TO_PTR(copy), copy);
2406 if(!pset_find_ptr(visited, arg)) {
2407 pset_insert(copies, INT_TO_PTR(copy), copy);
2408 find_copy_path(si, arg, target, any_interfere, copies, visited);
2409 pset_remove(copies, INT_TO_PTR(copy), copy);
2415 /* visit all uses which are phis */
2416 foreach_out_edge(irn, edge) {
2417 ir_node *user = edge->src;
2418 int pos = edge->pos;
2419 op_t *op = get_irn_link(user);
2422 if(!is_Phi(user)) continue;
2423 if(!has_reg_class(si, user)) continue;
2425 copy = op->attr.live_range.args.copies[pos];
2427 if(user == target) {
2428 pset_insert(copies, INT_TO_PTR(copy), copy);
2429 write_copy_path_cst(si, copies, any_interfere);
2430 pset_remove(copies, INT_TO_PTR(copy), copy);
2432 if(!pset_find_ptr(visited, user)) {
2433 pset_insert(copies, INT_TO_PTR(copy), copy);
2434 find_copy_path(si, user, target, any_interfere, copies, visited);
2435 pset_remove(copies, INT_TO_PTR(copy), copy);
2440 pset_remove_ptr(visited, irn);
2444 gen_copy_constraints(spill_ilp_t * si, ir_node * a, ir_node * b, ilp_var_t any_interfere)
2446 pset * copies = pset_new_ptr_default();
2447 pset * visited = pset_new_ptr_default();
2449 find_copy_path(si, a, b, any_interfere, copies, visited);
2457 memcopyhandler(spill_ilp_t * si)
2459 interference_t *interference;
2461 /* teste Speicherwerte auf Interferenz */
2463 /* analyze phi classes */
2464 phi_class_compute(si->chordal_env->irg);
2466 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
2467 irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
2469 // phi_class_free(si->chordal_env->irg);
2471 /* now lets emit the ILP unequations for the crap */
2472 set_foreach(si->interferences, interference) {
2474 ilp_var_t interfere,
2476 ilp_cst_t any_interfere_cst,
2478 const ir_node *a = interference->a;
2479 const ir_node *b = interference->b;
2481 /* any_interf <= \sum interf */
2482 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
2483 any_interfere_cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2484 any_interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2486 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
2488 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
2489 const ir_node *bb = irnlist->irn;
2490 spill_bb_t *spill_bb = get_irn_link(bb);
2497 spilla = set_find_spill(spill_bb->ilp, a);
2501 spillb = set_find_spill(spill_bb->ilp, b);
2504 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
2505 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
2506 /* 2: - mem_in_a - spill_a + interfere <= 0 */
2507 /* 3: - mem_in_b - spill_b + interfere <= 0 */
2508 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
2509 interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2511 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
2512 cst = lpp_add_cst(si->lpp, buf, lpp_less, 1);
2514 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
2515 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
2516 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
2517 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
2518 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
2520 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
2521 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2523 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2524 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
2525 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
2527 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
2528 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2530 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2531 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
2532 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
2535 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
2537 /* any_interfere >= interf */
2538 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
2539 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2541 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2542 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
2545 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
2546 gen_copy_constraints(si,a,b,any_interfere);
2553 memcopyinsertor(spill_ilp_t * si)
2555 /* weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
2556 * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
2572 return fabs(x) < 0.00001;
2576 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
2578 spill_ilp_t *si = get_irg_link(current_ir_graph);
2580 if(pset_find_ptr(si->all_possible_remats, n)) {
2581 op_t *op = (op_t*)get_irn_link(n);
2582 assert(op && op->is_remat);
2584 if(!op->attr.remat.remat->inverse) {
2585 if(op->attr.remat.pre) {
2586 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
2588 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
2593 op_t *op = (op_t*)get_irn_link(n);
2594 assert(op && op->is_remat);
2596 if(op->attr.remat.pre) {
2597 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
2599 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
2610 dump_graph_with_remats(ir_graph * irg, const char * suffix)
2612 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
2613 be_dump(irg, suffix, dump_ir_block_graph_sched);
2614 set_dump_node_vcgattr_hook(NULL);
2619 * Edge hook to dump the schedule edges with annotated register pressure.
2622 sched_pressure_edge_hook(FILE *F, ir_node *irn)
2624 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
2625 ir_node *prev = sched_prev(irn);
2626 fprintf(F, "edge:{sourcename:\"");
2628 fprintf(F, "\" targetname:\"");
2630 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
2631 fprintf(F, "\" color:magenta}\n");
2637 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
2639 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
2641 dump_consts_local(0);
2642 set_dump_node_edge_hook(sched_pressure_edge_hook);
2643 dump_ir_block_graph(irg, suffix);
2644 set_dump_node_edge_hook(old);
2648 walker_pressure_annotator(ir_node * bb, void * data)
2650 spill_ilp_t *si = data;
2654 pset *live = pset_new_ptr_default();
2657 live_foreach(bb, li) {
2658 irn = (ir_node *) li->irn;
2660 if (live_is_end(li) && has_reg_class(si, irn)) {
2661 pset_insert_ptr(live, irn);
2665 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
2667 sched_foreach_reverse(bb, irn) {
2669 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
2673 if(has_reg_class(si, irn)) {
2674 pset_remove_ptr(live, irn);
2675 if(is_Proj(irn)) ++projs;
2678 if(!is_Proj(irn)) projs = 0;
2680 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2681 ir_node *arg = get_irn_n(irn, n);
2683 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
2685 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
2692 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
2694 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
2699 connect_all_remats_with_keep(spill_ilp_t * si)
2707 n_remats = pset_count(si->all_possible_remats);
2709 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
2712 pset_foreach(si->all_possible_remats, irn) {
2717 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
2719 obstack_free(si->obst, ins);
2725 connect_all_spills_with_keep(spill_ilp_t * si)
2734 n_spills = pset_count(si->spills);
2736 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
2739 pset_foreach(si->spills, irn) {
2744 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
2746 obstack_free(si->obst, ins);
2750 /** insert a spill at an arbitrary position */
2751 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert, ir_node *ctx)
2753 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
2754 ir_graph *irg = get_irn_irg(bl);
2755 ir_node *frame = get_irg_frame(irg);
2759 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
2760 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
2762 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
2765 * search the right insertion point. a spill of a phi cannot be put
2766 * directly after the phi, if there are some phis behind the one which
2767 * is spilled. Also, a spill of a Proj must be after all Projs of the
2770 * Here's one special case:
2771 * If the spill is in the start block, the spill must be after the frame
2772 * pointer is set up. This is done by setting insert to the end of the block
2773 * which is its default initialization (see above).
2776 if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
2779 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
2782 sched_add_after(insert, spill);
2787 delete_remat(spill_ilp_t * si, ir_node * remat) {
2789 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2791 sched_remove(remat);
2793 /* kill links to operands */
2794 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
2795 set_irn_n(remat, n, bad);
2800 clean_remat_info(spill_ilp_t * si)
2804 remat_info_t *remat_info;
2805 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2807 set_foreach(si->remat_info, remat_info) {
2808 if(!remat_info->remats) continue;
2810 pset_foreach(remat_info->remats, remat)
2812 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
2813 set_irn_n(remat->proj, -1, bad);
2814 set_irn_n(remat->proj, 0, bad);
2817 if(get_irn_n_edges(remat->op) == 0) {
2818 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
2819 set_irn_n(remat->op, n, bad);
2824 if(remat_info->remats) del_pset(remat_info->remats);
2825 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
2830 delete_unnecessary_remats(spill_ilp_t * si)
2834 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2837 ir_node *end = get_irg_end(si->chordal_env->irg);
2840 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
2841 ir_node *keep_arg = get_irn_n(si->keep, n);
2842 op_t *arg_op = get_irn_link(keep_arg);
2845 assert(arg_op->is_remat);
2847 name = si->lpp->vars[arg_op->attr.remat.ilp];
2849 if(is_zero(name->value)) {
2850 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
2851 /* TODO check whether reload is preferred over remat (could be bug) */
2852 delete_remat(si, keep_arg);
2854 if(!arg_op->attr.remat.remat->inverse) {
2855 if(arg_op->attr.remat.pre) {
2856 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
2858 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
2861 if(arg_op->attr.remat.pre) {
2862 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
2864 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
2869 set_irn_n(si->keep, n, bad);
2872 for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
2873 ir_node *end_arg = get_End_keepalive(end, i);
2875 if(end_arg != si->keep) {
2876 obstack_grow(si->obst, &end_arg, sizeof(end_arg));
2879 keeps = obstack_finish(si->obst);
2880 set_End_keepalives(end, n-1, keeps);
2881 obstack_free(si->obst, keeps);
2884 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
2889 pset_foreach(si->all_possible_remats, remat) {
2890 op_t *remat_op = get_irn_link(remat);
2891 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
2893 if(is_zero(name->value)) {
2894 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
2895 /* TODO check whether reload is preferred over remat (could be bug) */
2896 delete_remat(si, remat);
2898 if(!remat_op->attr.remat.remat->inverse) {
2899 if(remat_op->attr.remat.pre) {
2900 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
2902 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
2905 if(remat_op->attr.remat.pre) {
2906 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
2908 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
2917 * @param before The node after which the spill will be placed in the schedule
2919 /* TODO set context properly */
2921 insert_spill(spill_ilp_t * si, ir_node * irn, ir_node * value, ir_node * before)
2925 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2927 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
2929 spill = be_spill2(arch_env, irn, before, irn);
2931 defs = set_insert_def(si->values, value);
2934 /* enter into the linked list */
2935 set_irn_link(spill, defs->spills);
2936 defs->spills = spill;
2938 #ifdef KEEPALIVE_SPILLS
2939 pset_insert_ptr(si->spills, spill);
2946 * @param before The Phi node which has to be spilled
2949 insert_mem_phi(spill_ilp_t * si, const ir_node * phi)
2956 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
2958 for(n=get_irn_arity(phi)-1; n>=0; --n) {
2959 ins[n] = si->m_unknown;
2962 mem_phi = new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
2964 defs = set_insert_def(si->values, phi);
2967 /* enter into the linked list */
2968 set_irn_link(mem_phi, defs->spills);
2969 defs->spills = mem_phi;
2971 sched_add_after(phi, mem_phi);
2973 #ifdef KEEPALIVE_SPILLS
2974 pset_insert_ptr(si->spills, mem_phi);
2981 * Add remat to list of defs, destroys link field!
2984 insert_remat(spill_ilp_t * si, ir_node * remat)
2987 op_t *remat_op = get_irn_link(remat);
2989 assert(remat_op->is_remat);
2991 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
2994 /* enter into the linked list */
2995 set_irn_link(remat, defs->remats);
2996 defs->remats = remat;
3001 collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
3006 defs = set_find_def(si->values, value);
3008 if(defs && defs->spills) {
3009 for(next = defs->spills; next; next = get_irn_link(next)) {
3010 pset_insert_ptr(spills, next);
3012 } else if (is_Phi(value)) {
3014 if(!pset_find_ptr(visited, value)) {
3018 pset_insert_ptr(visited, value);
3019 for(i=0, n=get_irn_arity(value); i<n; ++i) {
3020 ir_node *arg = get_irn_n(value, i);
3022 collect_spills(si, arg, spills, visited);
3026 // assert(0 && "Phi operand not spilled");
3032 get_spills_for_value(spill_ilp_t * si, ir_node * value)
3034 pset *spills = pset_new_ptr_default();
3035 // pset *visited = pset_new_ptr_default();
3037 // collect_spills(si, value, spills, visited);
3038 // del_pset(visited);
3042 defs = set_find_def(si->values, value);
3044 if(defs && defs->spills) {
3045 for(next = defs->spills; next; next = get_irn_link(next)) {
3046 pset_insert_ptr(spills, next);
3054 * Add reload before operation and add to list of defs
3057 insert_reload(spill_ilp_t * si, const ir_node * value, const ir_node * after)
3062 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
3064 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
3066 defs = set_find_def(si->values, value);
3067 /* get a spill of this value */
3069 if((!defs || !defs->spills) && is_Phi(value)) {
3072 spills = get_spills_for_value(si, value);
3074 spill = pset_first(spills);
3078 defs = set_insert_def(si->values, value);
3080 defs->spills = spill;
3081 set_irn_link(spill, NULL);
3083 spill = defs->spills;
3086 spill = defs->spills;
3087 assert(spill && "no spill placed before reload");
3089 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
3091 /* enter into the linked list */
3092 set_irn_link(reload, defs->remats);
3093 defs->remats = reload;
3099 walker_spill_placer(ir_node * bb, void * data) {
3100 spill_ilp_t *si = (spill_ilp_t*)data;
3102 spill_bb_t *spill_bb = get_irn_link(bb);
3103 pset *spills_to_do = pset_new_ptr_default();
3106 set_foreach(spill_bb->ilp, spill) {
3109 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
3110 name = si->lpp->vars[spill->mem_in];
3111 if(!is_zero(name->value)) {
3114 mem_phi = insert_mem_phi(si, spill->irn);
3116 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
3120 name = si->lpp->vars[spill->spill];
3121 if(!is_zero(name->value)) {
3122 /* place spill directly after definition */
3123 if(get_nodes_block(spill->irn) == bb) {
3124 insert_spill(si, spill->irn, spill->irn, spill->irn);
3128 /* place spill at bb start */
3129 if(spill->reg_in > 0) {
3130 name = si->lpp->vars[spill->reg_in];
3131 if(!is_zero(name->value)) {
3132 insert_spill(si, spill->irn, spill->irn, bb);
3136 /* place spill after a remat */
3137 pset_insert_ptr(spills_to_do, spill->irn);
3140 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
3143 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
3144 op_t *op = get_irn_link(irn);
3146 if(be_is_Spill(irn)) continue;
3149 /* TODO fix this if we want to support remats with more than two nodes */
3150 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3151 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3153 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3156 if(pset_find_ptr(spills_to_do, irn)) {
3157 pset_remove_ptr(spills_to_do, irn);
3159 insert_spill(si, irn, irn, irn);
3165 assert(pset_count(spills_to_do) == 0);
3167 /* afterwards free data in block */
3168 del_pset(spills_to_do);
3172 phim_fixer(spill_ilp_t *si) {
3175 set_foreach(si->values, defs) {
3176 const ir_node *phi = defs->value;
3177 ir_node *phi_m = NULL;
3178 ir_node *next = defs->spills;
3181 if(!is_Phi(phi)) continue;
3184 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3188 next = get_irn_link(next);
3191 if(!phi_m) continue;
3193 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3194 const ir_node *value = get_irn_n(phi, n);
3195 defs_t *val_defs = set_find_def(si->values, value);
3197 /* get a spill of this value */
3198 ir_node *spill = val_defs->spills;
3200 assert(spill && "no spill placed before PhiM");
3202 set_irn_n(phi_m, n, spill);
3208 walker_reload_placer(ir_node * bb, void * data) {
3209 spill_ilp_t *si = (spill_ilp_t*)data;
3211 spill_bb_t *spill_bb = get_irn_link(bb);
3215 /* reloads at end of block */
3216 if(spill_bb->reloads) {
3219 set_foreach(spill_bb->reloads, keyval) {
3220 ir_node *irn = (ir_node*)keyval->key;
3221 ilp_var_t reload = PTR_TO_INT(keyval->val);
3224 name = si->lpp->vars[reload];
3225 if(!is_zero(name->value)) {
3227 ir_node *insert_pos = bb;
3228 ir_node *prev = sched_block_last_noncf(si, bb);
3229 op_t *prev_op = get_irn_link(prev);
3231 /* insert reload before pre-remats */
3232 while(!sched_is_end(prev) && !be_is_Reload(prev) && !be_is_Spill(prev)
3233 && prev_op->is_remat && prev_op->attr.remat.pre) {
3236 prev = sched_prev(insert_pos);
3237 prev_op = get_irn_link(prev);
3240 reload = insert_reload(si, irn, insert_pos);
3242 #ifdef KEEPALIVE_RELOADS
3243 pset_insert_ptr(si->spills, reload);
3249 /* walk and insert more reloads and collect remats */
3250 sched_foreach_reverse(bb, irn) {
3251 op_t *op = get_irn_link(irn);
3253 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3254 if(is_Phi(irn)) break;
3257 if(get_irn_mode(irn) != mode_T) {
3258 insert_remat(si, irn);
3263 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3264 ir_node *arg = get_irn_n(irn, n);
3266 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3269 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3270 if(!is_zero(name->value)) {
3272 ir_node *insert_pos = irn;
3273 ir_node *prev = insert_pos;
3277 prev = sched_prev(prev);
3278 } while(be_is_Spill(prev));
3280 prev_op = get_irn_link(prev);
3282 /* insert reload before pre-remats */
3283 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3284 && prev_op->is_remat && prev_op->attr.remat.pre) {
3288 prev = sched_prev(prev);
3289 } while(be_is_Spill(prev));
3291 prev_op = get_irn_link(prev);
3295 reload = insert_reload(si, arg, insert_pos);
3297 set_irn_n(irn, n, reload);
3299 #ifdef KEEPALIVE_RELOADS
3300 pset_insert_ptr(si->spills, reload);
3308 del_set(spill_bb->ilp);
3309 if(spill_bb->reloads) del_set(spill_bb->reloads);
3313 walker_collect_used(ir_node * irn, void * data)
3315 lc_bitset_t *used = data;
3317 lc_bitset_set(used, get_irn_idx(irn));
3320 struct kill_helper {
3326 walker_kill_unused(ir_node * bb, void * data)
3328 struct kill_helper *kh = data;
3329 const ir_node *bad = get_irg_bad(get_irn_irg(bb));
3333 for(irn=sched_first(bb); !sched_is_end(irn);) {
3334 ir_node *next = sched_next(irn);
3337 if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) {
3338 if(be_is_Spill(irn) || be_is_Reload(irn)) {
3339 DBG((kh->si->dbg, LEVEL_1, "\t SUBOPTIMAL! %+F IS UNUSED (cost: %g)\n", irn, get_cost(kh->si, irn)*execution_frequency(kh->si, bb)));
3341 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
3347 set_nodes_block(irn, bad);
3348 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3349 set_irn_n(irn, n, bad);
3357 kill_all_unused_values_in_schedule(spill_ilp_t * si)
3359 struct kill_helper kh;
3361 kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
3364 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
3365 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
3367 lc_bitset_free(kh.used);
3371 print_irn_pset(pset * p)
3375 pset_foreach(p, irn) {
3376 ir_printf("%+F\n", irn);
3381 rewire_uses(spill_ilp_t * si)
3383 dom_front_info_t *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
3385 pset *ignore = pset_new_ptr(1);
3387 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
3389 /* then fix uses of spills */
3390 set_foreach(si->values, defs) {
3393 ir_node *next = defs->remats;
3396 reloads = pset_new_ptr_default();
3399 if(be_is_Reload(next)) {
3400 pset_insert_ptr(reloads, next);
3404 next = get_irn_link(next);
3407 spills = get_spills_for_value(si, defs->value);
3408 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));
3409 if(pset_count(spills) > 1) {
3410 //assert(pset_count(reloads) > 0);
3411 // print_irn_pset(spills);
3412 // print_irn_pset(reloads);
3414 be_ssa_constr_set_ignore(dfi, spills, ignore);
3421 /* first fix uses of remats and reloads */
3422 set_foreach(si->values, defs) {
3424 ir_node *next = defs->remats;
3427 nodes = pset_new_ptr_default();
3428 pset_insert_ptr(nodes, defs->value);
3431 pset_insert_ptr(nodes, next);
3432 next = get_irn_link(next);
3435 if(pset_count(nodes) > 1) {
3436 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value));
3437 be_ssa_constr_set(dfi, nodes);
3444 // remove_unused_defs(si);
3446 be_free_dominance_frontiers(dfi);
3450 writeback_results(spill_ilp_t * si)
3452 /* walk through the graph and collect all spills, reloads and remats for a value */
3454 si->values = new_set(cmp_defs, 4096);
3456 DBG((si->dbg, LEVEL_1, "Applying results\n"));
3457 delete_unnecessary_remats(si);
3458 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
3459 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
3461 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
3463 /* clean the remat info! there are still back-edges leading there! */
3464 clean_remat_info(si);
3468 connect_all_spills_with_keep(si);
3470 del_set(si->values);
3474 get_n_regs(spill_ilp_t * si)
3476 int arch_n_regs = arch_register_class_n_regs(si->cls);
3480 for(i=0; i<arch_n_regs; i++) {
3481 if(!arch_register_type_is(&si->cls->regs[i], ignore)) {
3486 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name));
3491 walker_reload_mover(ir_node * bb, void * data)
3493 spill_ilp_t *si = data;
3496 sched_foreach(bb, tmp) {
3497 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
3498 ir_node *reload = tmp;
3501 /* move reload upwards */
3503 int pressure = (int)get_irn_link(reload);
3504 if(pressure < si->n_regs) {
3505 irn = sched_prev(reload);
3506 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
3507 sched_remove(reload);
3508 pressure = (int)get_irn_link(irn);
3510 while(pressure < si->n_regs) {
3511 if(sched_is_end(irn) || (be_is_Reload(irn) && has_reg_class(si, irn))) break;
3513 set_irn_link(irn, INT_TO_PTR(pressure+1));
3514 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
3515 irn = sched_prev(irn);
3517 pressure = (int)get_irn_link(irn);
3520 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
3521 sched_put_after(irn, reload);
3528 move_reloads_upward(spill_ilp_t * si)
3530 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
3534 be_spill_remat(const be_chordal_env_t * chordal_env)
3536 char problem_name[256];
3537 char dump_suffix[256];
3538 char dump_suffix2[256];
3539 char dump_suffix3[256];
3540 struct obstack obst;
3543 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
3544 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
3545 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
3547 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
3548 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
3550 obstack_init(&obst);
3551 si.chordal_env = chordal_env;
3553 si.cls = chordal_env->cls;
3554 si.lpp = new_lpp(problem_name, lpp_minimize);
3555 si.remat_info = new_set(cmp_remat_info, 4096);
3556 si.interferences = new_set(cmp_interference, 4096);
3557 si.all_possible_remats = pset_new_ptr_default();
3558 si.spills = pset_new_ptr_default();
3559 si.inverse_ops = pset_new_ptr_default();
3560 #ifndef EXECFREQ_LOOPDEPH
3561 si.execfreqs = compute_execfreq(chordal_env->irg);
3563 si.execfreqs = NULL;
3568 si.n_regs = get_n_regs(&si);
3570 set_irg_link(chordal_env->irg, &si);
3571 compute_doms(chordal_env->irg);
3573 /* compute phi classes */
3574 // phi_class_compute(chordal_env->irg);
3576 be_analyze_regpressure(chordal_env, "-pre");
3578 #ifdef COLLECT_REMATS
3579 /* collect remats */
3580 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
3581 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
3584 /* insert possible remats */
3585 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
3586 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
3587 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
3590 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
3591 connect_all_remats_with_keep(&si);
3592 /* dump graph with inserted remats */
3593 dump_graph_with_remats(chordal_env->irg, dump_suffix);
3596 /* insert copies for phi arguments not in my regclass */
3597 irg_walk_graph(chordal_env->irg, walker_regclass_copy_insertor, NULL, &si);
3599 /* recompute liveness */
3600 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
3601 be_liveness(chordal_env->irg);
3605 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
3606 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
3607 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
3609 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
3610 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
3612 #ifndef NO_MEMCOPIES
3613 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
3614 memcopyhandler(&si);
3622 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
3623 if ((f = fopen(buf, "wt")) != NULL) {
3624 lpp_dump_plain(si.lpp, f);
3631 DBG((si.dbg, LEVEL_1, "\tSolving %F\n", chordal_env->irg));
3633 lpp_set_time_limit(si.lpp, ILP_TIMEOUT);
3637 lpp_solve_cplex(si.lpp);
3639 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
3641 assert(lpp_is_sol_valid(si.lpp)
3642 && "solution of ILP must be valid");
3644 DBG((si.dbg, LEVEL_1, "\t%s: iterations: %d, solution time: %g, objective function: %g\n", problem_name, si.lpp->iterations, si.lpp->sol_time, is_zero(si.lpp->objval)?0.0:si.lpp->objval));
3646 #ifdef DUMP_SOLUTION
3651 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
3652 if ((f = fopen(buf, "wt")) != NULL) {
3654 for (i = 0; i < si.lpp->var_next; ++i) {
3655 lpp_name_t *name = si.lpp->vars[i];
3656 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
3663 writeback_results(&si);
3667 kill_all_unused_values_in_schedule(&si);
3669 #if defined(KEEPALIVE_SPILLS) || defined(KEEPALIVE_RELOADS)
3670 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
3673 // move reloads upwards
3674 be_liveness(chordal_env->irg);
3675 //irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3676 //move_reloads_upward(&si);
3678 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3680 dump_pressure_graph(&si, dump_suffix2);
3682 // TODO fix temporarily exceeded regpressure due to remat2s
3684 // TODO insert copys to fix interferences in memory
3686 be_analyze_regpressure(chordal_env, "-post");
3688 free_dom(chordal_env->irg);
3689 del_set(si.interferences);
3690 del_pset(si.inverse_ops);
3691 del_pset(si.all_possible_remats);
3692 del_pset(si.spills);
3693 #ifndef EXECFREQ_LOOPDEPH
3694 free_execfreq(si.execfreqs);
3697 obstack_free(&obst, NULL);
3698 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
3701 #else /* WITH_ILP */
3704 only_that_you_can_compile_without_WITH_ILP_defined(void)
3708 #endif /* WITH_ILP */