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_PRE_REMAT /* allow values to die after a pre remat */
66 #define CHECK_POST_REMAT /* check pressure after post remats (conservative but otherwise we can temporarily exceed the register pressure) */
67 #define NO_SINGLE_USE_REMATS /* do not repair schedule */
68 //#define KEEPALIVE_SPILLS
69 //#define KEEPALIVE_RELOADS
70 #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 */
101 DEBUG_ONLY(firm_dbg_module_t * dbg);
104 typedef int ilp_var_t;
105 typedef int ilp_cst_t;
107 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 */
142 typedef struct _defs_t {
144 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
145 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
148 typedef struct _remat_info_t {
149 const ir_node *irn; /**< the irn to which these remats belong */
150 pset *remats; /**< possible remats for this value */
151 pset *remats_by_operand; /**< remats with this value as operand */
154 typedef struct _keyval_t {
159 typedef struct _spill_t {
169 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
171 return chordal_has_class(si->chordal_env, irn);
176 cmp_remat(const void *a, const void *b)
178 const keyval_t *p = a;
179 const keyval_t *q = b;
180 const remat_t *r = p->val;
181 const remat_t *s = q->val;
185 return !(r == s || r->op == s->op);
189 cmp_remat(const void *a, const void *b)
191 const remat_t *r = a;
192 const remat_t *s = a;
194 return !(r == s || r->op == s->op);
198 cmp_spill(const void *a, const void *b, size_t size)
200 const spill_t *p = a;
201 const spill_t *q = b;
203 // return !(p->irn == q->irn && p->bb == q->bb);
204 return !(p->irn == q->irn);
208 set_find_keyval(set * set, void * key)
213 return set_find(set, &query, sizeof(query), HASH_PTR(key));
217 set_insert_keyval(set * set, void * key, void * val)
223 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
227 set_find_def(set * set, ir_node * value)
232 return set_find(set, &query, sizeof(query), HASH_PTR(value));
236 set_insert_def(set * set, ir_node * value)
243 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
247 set_find_spill(set * set, ir_node * value)
252 return set_find(set, &query, sizeof(query), HASH_PTR(value));
255 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
256 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
257 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
258 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
259 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
262 cmp_remat_info(const void *a, const void *b, size_t size)
264 const remat_info_t *p = a;
265 const remat_info_t *q = b;
267 return !(p->irn == q->irn);
271 cmp_defs(const void *a, const void *b, size_t size)
276 return !(p->value == q->value);
280 cmp_keyval(const void *a, const void *b, size_t size)
282 const keyval_t *p = a;
283 const keyval_t *q = b;
285 return !(p->key == q->key);
289 execution_frequency(const spill_ilp_t * si, const ir_node * irn)
294 return get_block_execfreq(si->execfreqs, irn) + FUDGE;
296 return get_block_execfreq(si->execfreqs, get_nodes_block(irn)) + FUDGE;
300 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
302 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
307 get_cost(const spill_ilp_t * si, const ir_node * irn)
309 if(be_is_Spill(irn)) {
311 } else if(be_is_Reload(irn)){
314 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
320 * Checks, whether node and its operands have suitable reg classes
323 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
327 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
328 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
332 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
335 for (i = 0, n = get_irn_arity(irn); i < n && remat; ++i) {
336 ir_node *op = get_irn_n(irn, i);
337 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
340 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
347 * Try to create a remat from @p op with destination value @p dest_value
349 static INLINE remat_t *
350 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
352 remat_t *remat = NULL;
354 // if(!mode_is_datab(get_irn_mode(dest_value)))
357 if(dest_value == op) {
358 const ir_node *proj = NULL;
360 if(is_Proj(dest_value)) {
361 op = get_irn_n(op, 0);
365 if(!is_rematerializable(si, op))
368 remat = obstack_alloc(si->obst, sizeof(*remat));
370 remat->cost = get_cost(si, op);
371 remat->value = dest_value;
375 arch_inverse_t inverse;
379 /* get the index of the operand we want to retrieve by the inverse op */
380 for (i = 0, n = get_irn_arity(op); i < n; ++i) {
381 ir_node *arg = get_irn_n(op, i);
383 if(arg == dest_value) break;
385 if(i == n) return NULL;
387 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", i, op));
389 /* else ask the backend to give an inverse op */
390 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, i, &inverse, si->obst)) {
393 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
395 assert(inverse.n > 0 && "inverse op should have at least one node");
397 for(i=0; i<inverse.n; ++i) {
398 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
402 remat = obstack_alloc(si->obst, sizeof(*remat));
403 remat->op = inverse.nodes[0];
404 remat->cost = inverse.costs;
405 remat->value = dest_value;
406 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
409 assert(is_Proj(remat->proj));
411 assert(0 && "I can not handle remats with more than 2 nodes");
418 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
420 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
428 add_remat(const spill_ilp_t * si, const remat_t * remat)
430 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 (i = 0, n = get_irn_arity(remat->op); i < n; ++i) {
450 ir_node *arg = get_irn_n(remat->op, i);
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)
487 #ifdef NO_SINGLE_USE_REMATS
488 if(has_reg_class(si, op) && (get_irn_n_nonremat_edges(si, op) > 1)) {
490 if(has_reg_class(si, op)) {
492 remat = get_remat_from_op(si, op, op);
494 add_remat(si, remat);
498 #ifdef COLLECT_INVERSE_REMATS
499 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
501 for (i = 0, n = get_irn_arity(op); i < n; ++i) {
502 ir_node *arg = get_irn_n(op, i);
504 if(has_reg_class(si, arg)) {
505 /* try to get an inverse remat */
506 remat = get_remat_from_op(si, arg, op);
508 add_remat(si, remat);
517 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
520 ir_node *def_block = get_nodes_block(val);
526 /* if pos is at end of a basic block */
528 ret = (pos == def_block || block_dominates(def_block, pos));
529 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
533 /* else if this is a normal operation */
534 block = get_nodes_block(pos);
535 if(block == def_block) {
536 if(!sched_is_scheduled(val)) return 1;
538 ret = sched_comes_after(val, pos);
539 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
543 ret = block_dominates(def_block, block);
544 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
548 static INLINE ir_node *
549 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
551 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
555 * Returns first non-Phi node of block @p bb
557 static INLINE ir_node *
558 sched_block_first_nonphi(const ir_node * bb)
560 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
564 sched_skip_proj_predicator(const ir_node * irn, void * data)
566 return (is_Proj(irn));
569 static INLINE ir_node *
570 sched_next_nonproj(const ir_node * irn, int forward)
572 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
576 * Returns next operation node (non-Proj) after @p irn
577 * or the basic block of this node
579 static INLINE ir_node *
580 sched_next_op(const ir_node * irn)
582 ir_node *next = sched_next(irn);
587 return sched_next_nonproj(next, 1);
591 * Returns previous operation node (non-Proj) before @p irn
592 * or the basic block of this node
594 static INLINE ir_node *
595 sched_prev_op(const ir_node * irn)
597 ir_node *prev = sched_prev(irn);
602 return sched_next_nonproj(prev, 0);
606 sched_put_after(ir_node * insert, ir_node * irn)
608 if(is_Block(insert)) {
609 insert = sched_block_first_nonphi(insert);
611 insert = sched_next_op(insert);
613 sched_add_before(insert, irn);
617 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
619 if(is_Block(insert)) {
620 insert = sched_block_last_noncf(si, insert);
622 insert = sched_next_nonproj(insert, 0);
623 insert = sched_prev(insert);
625 sched_add_after(insert, irn);
629 * Tells you whether a @p remat can be placed before the irn @p pos
632 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
634 const ir_node *op = remat->op;
641 prev = sched_block_last_noncf(si, pos);
642 prev = sched_next_nonproj(prev, 0);
644 prev = sched_prev_op(pos);
646 /* do not remat if the rematted value is defined immediately before this op */
647 if(prev == remat->op) {
652 /* this should be just fine, the following OP will be using this value, right? */
654 /* only remat AFTER the real definition of a value (?) */
655 if(!value_is_defined_before(si, pos, remat->value)) {
656 // ir_fprintf(stderr, "error(not defined)");
661 for(i=0, n=get_irn_arity(op); i<n && res; ++i) {
662 const ir_node *arg = get_irn_n(op, i);
664 #ifdef NO_ENLARGE_L1V3N355
665 if(has_reg_class(si, arg) && live) {
666 res &= pset_find_ptr(live, arg)?1:0;
668 res &= value_is_defined_before(si, pos, arg);
671 res &= value_is_defined_before(si, pos, arg);
679 * Tells you whether a @p remat can be placed after the irn @p pos
682 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
685 pos = sched_block_first_nonphi(pos);
687 pos = sched_next_op(pos);
690 /* only remat AFTER the real definition of a value (?) */
691 if(!value_is_defined_before(si, pos, remat->value)) {
695 return can_remat_before(si, remat, pos, live);
699 * Collect potetially rematerializable OPs
702 walker_remat_collector(ir_node * irn, void * data)
704 spill_ilp_t *si = data;
706 if(!is_Block(irn) && !is_Phi(irn)) {
707 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
708 get_remats_from_op(si, irn);
713 * Inserts a copy of @p irn before @p pos
716 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
721 bb = is_Block(pos)?pos:get_nodes_block(pos);
722 copy = exact_copy(irn);
723 set_nodes_block(copy, bb);
724 sched_put_before(si, pos, copy);
730 * Inserts a copy of @p irn after @p pos
733 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
738 bb = is_Block(pos)?pos:get_nodes_block(pos);
739 copy = exact_copy(irn);
740 set_nodes_block(copy, bb);
741 sched_put_after(pos, copy);
747 insert_remat_after(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
751 if(can_remat_after(si, remat, pos, live)) {
756 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
758 copy = insert_copy_after(si, remat->op, pos);
760 // ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", remat->value, pos);
761 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
762 op = obstack_alloc(si->obst, sizeof(*op));
764 op->attr.remat.remat = remat;
765 op->attr.remat.pre = 0;
766 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
768 set_irn_link(copy, op);
769 pset_insert_ptr(si->all_possible_remats, copy);
771 proj_copy = insert_copy_after(si, remat->proj, copy);
772 set_irn_n(proj_copy, 0, copy);
773 set_irn_link(proj_copy, op);
774 pset_insert_ptr(si->all_possible_remats, proj_copy);
782 insert_remat_before(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
786 if(can_remat_before(si, remat, pos, live)) {
791 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
793 copy = insert_copy_before(si, remat->op, pos);
795 // ir_snprintf(buf, sizeof(buf), "remat_%N_%N", remat->value, pos);
796 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
797 op = obstack_alloc(si->obst, sizeof(*op));
799 op->attr.remat.remat = remat;
800 op->attr.remat.pre = 1;
801 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
803 set_irn_link(copy, op);
804 pset_insert_ptr(si->all_possible_remats, copy);
806 proj_copy = insert_copy_after(si, remat->proj, copy);
807 set_irn_n(proj_copy, 0, copy);
808 set_irn_link(proj_copy, op);
809 pset_insert_ptr(si->all_possible_remats, proj_copy);
817 get_block_n_succs(const ir_node *block) {
818 const ir_edge_t *edge;
820 assert(edges_activated(current_ir_graph));
822 edge = get_block_succ_first(block);
826 edge = get_block_succ_next(block, edge);
831 is_merge_edge(const ir_node * bb)
833 #ifdef GOODWIN_REDUCTION
834 return get_block_n_succs(bb) == 1;
841 is_diverge_edge(const ir_node * bb)
843 #ifdef GOODWIN_REDUCTION
844 return get_Block_n_cfgpreds(bb) == 1;
851 * Insert (so far unused) remats into the irg to
852 * recompute the potential liveness of all values
855 walker_remat_insertor(ir_node * bb, void * data)
857 spill_ilp_t *si = data;
858 spill_bb_t *spill_bb;
863 pset *live = pset_new_ptr_default();
865 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
867 live_foreach(bb, li) {
868 ir_node *value = (ir_node *) li->irn;
870 /* add remats at end of block */
871 if (live_is_end(li) && has_reg_class(si, value)) {
872 pset_insert_ptr(live, value);
876 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
877 set_irn_link(bb, spill_bb);
879 irn = sched_last(bb);
880 while(!sched_is_end(irn)) {
886 next = sched_prev(irn);
888 DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next));
890 if(is_Phi(irn) || is_Proj(irn)) {
893 if(has_reg_class(si, irn)) {
894 pset_remove_ptr(live, irn);
897 op = obstack_alloc(si->obst, sizeof(*op));
899 op->attr.live_range.reloads = NULL;
900 op->attr.live_range.ilp = ILP_UNDEF;
901 set_irn_link(irn, op);
907 op = obstack_alloc(si->obst, sizeof(*op));
909 op->attr.live_range.ilp = ILP_UNDEF;
910 op->attr.live_range.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.reloads) * get_irn_arity(irn));
911 memset(op->attr.live_range.reloads, 0xFF, sizeof(*op->attr.live_range.reloads) * get_irn_arity(irn));
912 set_irn_link(irn, op);
914 args = pset_new_ptr_default();
916 /* collect arguments of op */
917 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
918 ir_node *arg = get_irn_n(irn, i);
920 pset_insert_ptr(args, arg);
923 /* set args of op live in epilog */
924 pset_foreach(args, arg) {
925 if(has_reg_class(si, arg)) {
926 pset_insert_ptr(live, arg);
930 /* insert all possible remats after irn */
931 pset_foreach(args, arg) {
932 remat_info_t *remat_info,
936 /* continue if the operand has the wrong reg class
938 if(!has_reg_class(si, arg))
943 query.remats_by_operand = NULL;
944 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
950 /* do not place post remats after jumps */
951 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue;
953 if(remat_info->remats_by_operand) {
954 pset_foreach(remat_info->remats_by_operand, remat) {
955 /* do not insert remats producing the same value as one of the operands */
956 if(!pset_find_ptr(args, remat->value)) {
957 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
958 #ifdef REMAT_WHILE_LIVE
959 if(pset_find_ptr(live, remat->value)) {
960 insert_remat_after(si, remat, irn, live);
963 insert_remat_after(si, remat, irn, live);
970 /* delete defined value from live set */
971 if(has_reg_class(si, irn)) {
972 pset_remove_ptr(live, irn);
975 /* insert all possible remats before irn */
976 pset_foreach(args, arg) {
977 remat_info_t *remat_info,
981 /* continue if the operand has the wrong reg class
983 if(!has_reg_class(si, arg))
988 query.remats_by_operand = NULL;
989 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
995 if(remat_info->remats) {
996 pset_foreach(remat_info->remats, remat) {
997 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
998 #ifdef REMAT_WHILE_LIVE
999 if(pset_find_ptr(live, remat->value)) {
1000 insert_remat_before(si, remat, irn, live);
1003 insert_remat_before(si, remat, irn, live);
1013 live_foreach(bb, li) {
1014 ir_node *value = (ir_node *) li->irn;
1016 /* add remats at end if successor has multiple predecessors */
1017 if(is_merge_edge(bb)) {
1018 /* add remats at end of block */
1019 if (live_is_end(li) && has_reg_class(si, value)) {
1020 remat_info_t *remat_info,
1025 query.remats = NULL;
1026 query.remats_by_operand = NULL;
1027 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1029 if(remat_info && remat_info->remats) {
1030 pset_foreach(remat_info->remats, remat) {
1031 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1033 insert_remat_before(si, remat, bb, NULL);
1038 if(is_diverge_edge(bb)) {
1039 /* add remat2s at beginning of block */
1040 if ((live_is_in(li) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
1041 remat_info_t *remat_info,
1046 query.remats = NULL;
1047 query.remats_by_operand = NULL;
1048 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1050 if(remat_info && remat_info->remats) {
1051 pset_foreach(remat_info->remats, remat) {
1052 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at beginning of block %+F\n", remat->op, bb));
1054 /* put the remat here if all its args are available */
1055 insert_remat_after(si, remat, bb, NULL);
1065 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1068 luke_endwalker(ir_node * bb, void * data)
1070 spill_ilp_t *si = (spill_ilp_t*)data;
1077 spill_bb_t *spill_bb = get_irn_link(bb);
1080 live = pset_new_ptr_default();
1081 use_end = pset_new_ptr_default();
1083 live_foreach(bb, li) {
1084 irn = (ir_node *) li->irn;
1085 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1088 pset_insert_ptr(live, irn);
1089 op = get_irn_link(irn);
1090 assert(!op->is_remat);
1094 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1095 /* their reg_out is unimportant because it can always be set */
1096 sched_foreach_reverse(bb, irn) {
1100 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1102 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1103 ir_node *irn_arg = get_irn_n(irn, i);
1104 if(has_reg_class(si, irn_arg)) {
1105 pset_insert_ptr(use_end, irn);
1110 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1111 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1113 spill_bb->ilp = new_set(cmp_spill, 16);
1115 live_foreach(bb, li) {
1116 irn = (ir_node *) li->irn;
1117 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1123 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1125 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1127 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1128 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1129 /* if irn is used at the end of the block, then it is live anyway */
1130 if(!pset_find_ptr(use_end, irn))
1131 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1133 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1134 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1136 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1137 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1139 spill->reg_in = ILP_UNDEF;
1140 spill->mem_in = ILP_UNDEF;
1149 next_post_remat(const ir_node * irn)
1154 irn = sched_block_first_nonphi(irn);
1156 irn = sched_next_op(irn);
1159 if(sched_is_end(irn))
1162 op = (op_t*)get_irn_link(irn);
1163 if(op->is_remat && !op->attr.remat.pre) {
1172 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
1178 ret = sched_block_last_noncf(si, irn);
1179 ret = sched_next(ret);
1180 ret = sched_prev_op(ret);
1182 ret = sched_prev_op(irn);
1185 if(sched_is_end(ret) || is_Phi(ret))
1188 op = (op_t*)get_irn_link(ret);
1189 if(op->is_remat && op->attr.remat.pre) {
1197 * Find a remat of value @p value in the epilog of @p pos
1200 find_post_remat(const ir_node * value, const ir_node * pos)
1202 while((pos = next_post_remat(pos)) != NULL) {
1205 op = get_irn_link(pos);
1206 assert(op->is_remat && !op->attr.remat.pre);
1208 if(op->attr.remat.remat->value == value)
1209 return (ir_node*)pos;
1212 const ir_edge_t *edge;
1213 foreach_out_edge(pos, edge) {
1214 ir_node *proj = get_edge_src_irn(edge);
1215 assert(is_Proj(proj));
1225 * Find a remat of value @p value in the prolog of @p pos
1228 find_pre_remat(const spill_ilp_t * si, const ir_node * value, const ir_node * pos)
1230 while((pos = next_pre_remat(si,pos)) != NULL) {
1233 op = get_irn_link(pos);
1234 assert(op->is_remat && op->attr.remat.pre);
1236 if(op->attr.remat.remat->value == value)
1237 return (ir_node*)pos;
1244 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1246 spill_bb_t *spill_bb = get_irn_link(bb);
1252 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1254 double spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1256 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1258 spill->reg_out = ILP_UNDEF;
1259 spill->reg_in = ILP_UNDEF;
1260 spill->mem_in = ILP_UNDEF;
1262 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1263 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1265 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1266 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1273 * Walk all irg blocks and emit this ILP
1276 luke_blockwalker(ir_node * bb, void * data)
1278 spill_ilp_t *si = (spill_ilp_t*)data;
1284 spill_bb_t *spill_bb = get_irn_link(bb);
1290 live = pset_new_ptr_default();
1292 /* do something at the end of the block */
1294 /* init live values at end of block */
1295 live_foreach(bb, li) {
1296 ir_node *irn = (ir_node *) li->irn;
1298 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1299 pset_insert_ptr(live, irn);
1303 if(is_merge_edge(bb)) {
1304 spill_bb->reloads = obstack_alloc(si->obst, pset_count(live) * sizeof(*spill_bb->reloads));
1305 memset(spill_bb->reloads, 0xFF, pset_count(live) * sizeof(*spill_bb->reloads));
1307 spill_bb->reloads = NULL;
1311 live_foreach(bb, li) {
1312 ir_node *irn = (ir_node *) li->irn;
1315 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1316 spill = set_find_spill(spill_bb->ilp, irn);
1319 if(spill_bb->reloads) {
1320 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1321 spill_bb->reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1323 /* reload <= mem_out */
1324 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1325 lpp_set_factor_fast(si->lpp, cst, spill_bb->reloads[i], 1.0);
1326 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1329 op = get_irn_link(irn);
1330 assert(!op->is_remat);
1332 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1333 op->attr.live_range.ilp = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1334 op->attr.live_range.op = bb;
1336 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1337 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1339 /* reg_out - reload - remat - live_range <= 0 */
1340 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1341 if(spill_bb->reloads) lpp_set_factor_fast(si->lpp, cst, spill_bb->reloads[i], -1.0);
1342 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1343 foreach_pre_remat(si, bb, tmp) {
1344 op_t *remat_op = get_irn_link(tmp);
1345 if(remat_op->attr.remat.remat->value == irn) {
1346 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1353 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1355 sched_foreach_reverse(bb, irn) {
1361 ilp_cst_t check_pre,
1363 #ifdef CHECK_POST_REMAT
1364 ilp_cst_t check_post_remat;
1366 set *args = new_set(cmp_keyval, get_irn_arity(irn));
1372 op = get_irn_link(irn);
1374 if(op->is_remat) continue;
1375 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1377 if(has_reg_class(si, irn)) {
1378 assert(pset_find_ptr(live, irn));
1379 pset_remove_ptr(live, irn);
1382 /* init set of irn's arguments */
1383 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1384 ir_node *irn_arg = get_irn_n(irn, i);
1385 if(has_reg_class(si, irn_arg)) {
1386 set_insert_keyval(args, irn_arg, (void*)i);
1390 #ifdef CHECK_POST_REMAT
1391 /* check the register pressure after the epilog */
1392 ir_snprintf(buf, sizeof(buf), "check_post_remat_%N", irn);
1393 check_post_remat = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1395 /* iterate over L\U */
1396 pset_foreach(live, tmp) {
1397 if(!set_find_keyval(args, tmp)) {
1398 /* if a live value is not used by irn */
1399 tmp_op = get_irn_link(tmp);
1400 // assert(tmp_op->attr.live_range.op != irn);
1401 lpp_set_factor_fast(si->lpp, check_post_remat, tmp_op->attr.live_range.ilp, 1.0);
1404 /* iterate over following remats and remove possibly defined values again from check_post_remat */
1405 foreach_post_remat(irn, tmp) {
1406 op_t *remat_op = get_irn_link(tmp);
1407 const ir_node *value = remat_op->attr.remat.remat->value;
1408 op_t *val_op = get_irn_link(value);
1410 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1412 /* values that are defined by remat2s are not counted */
1413 #ifdef REMAT_WHILE_LIVE
1414 assert(val_op->attr.live_range.ilp);
1415 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1417 if(val_op->attr.live_range.ilp != ILP_UNDEF) {
1418 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1420 #endif /* REMAT_WHILE_LIVE */
1422 #endif /* CHECK_POST_REMAT */
1425 /* new live ranges for values from L\U defined by remat2s or used by remats */
1426 pset_foreach(live, tmp) {
1427 ir_node *value = tmp;//remat_op->attr.remat.remat->value;
1428 op_t *value_op = get_irn_link(value);
1430 if(!set_find_keyval(args, value)) {
1431 ilp_var_t prev_lr = ILP_UNDEF;
1435 foreach_post_remat(irn, remat) {
1436 op_t *remat_op = get_irn_link(remat);
1438 /* if value is being rematerialized by this remat */
1439 if(value == remat_op->attr.remat.remat->value) {
1440 if(cst == ILP_UNDEF) {
1441 /* next_live_range <= prev_live_range + sum remat2s */
1442 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1443 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1444 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1445 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1446 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1447 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1450 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1454 #ifdef MAY_DIE_AT_PRE_REMAT
1455 if(cst == ILP_UNDEF) {
1456 foreach_pre_remat(si, irn, remat) {
1460 for (i = 0, n = get_irn_arity(remat); i < n; ++i) {
1461 ir_node *remat_arg = get_irn_n(remat, i);
1463 /* if value is being used by this remat */
1464 if(value == remat_arg) {
1465 /* next_live_range <= prev_live_range */
1466 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1467 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1469 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1470 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1471 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1472 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1475 /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1482 if(prev_lr != ILP_UNDEF) {
1483 value_op->attr.live_range.ilp = prev_lr;
1484 value_op->attr.live_range.op = irn;
1489 /* get count of values in my register class defined by irn */
1490 /* also add defined values to check_post_remat; do this before iterating over args */
1491 if(get_irn_mode(irn) == mode_T) {
1492 ir_node *proj = sched_next(irn);
1493 op_t *proj_op = get_irn_link(proj);
1495 while(is_Proj(proj)) {
1496 if(has_reg_class(si, proj)) {
1498 #ifdef CHECK_POST_REMAT
1499 lpp_set_factor_fast(si->lpp, check_post_remat, proj_op->attr.live_range.ilp, 1.0);
1502 proj = sched_next(proj);
1503 proj_op = get_irn_link(proj);
1506 if(has_reg_class(si, irn)) {
1508 #ifdef CHECK_POST_REMAT
1509 lpp_set_factor_fast(si->lpp, check_post_remat, op->attr.live_range.ilp, 1.0);
1513 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
1515 /* count how many regs irn needs for arguments */
1516 k = set_count(args);
1518 /* check the register pressure in the prolog */
1519 /* sum_{L\U} lr <= n - |U| */
1520 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
1521 check_pre = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - k);
1523 /* check the register pressure in the epilog */
1524 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
1525 check_post = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - d);
1527 set_foreach(args, keyval) {
1533 ir_node *arg = keyval->key;
1535 spill = add_to_spill_bb(si, bb, arg);
1537 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
1538 next_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1540 i = (int)keyval->val;
1543 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
1544 op->attr.live_range.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1546 /* reload <= mem_out */
1547 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1548 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[i], 1.0);
1549 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1551 arg_op = get_irn_link(arg);
1553 /* requirement: arg must be in register for use */
1554 /* reload + remat + live_range == 1 */
1555 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
1556 cst = lpp_add_cst(si->lpp, buf, lpp_equal, 1.0);
1558 lpp_set_factor_fast(si->lpp, cst, next_lr, 1.0);
1559 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[i], 1.0);
1560 foreach_pre_remat(si, irn, tmp) {
1561 op_t *remat_op = get_irn_link(tmp);
1562 if(remat_op->attr.remat.remat->value == arg) {
1563 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1567 /* the epilog stuff - including post_use, post, post_remat */
1568 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
1569 post_use = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1571 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
1573 /* arg is live throughout epilog if the next live_range is in a register */
1574 if(pset_find_ptr(live, arg)) {
1575 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
1577 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1578 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1579 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
1580 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1582 #ifdef CHECK_POST_REMAT
1583 lpp_set_factor_fast(si->lpp, check_post_remat, arg_op->attr.live_range.ilp, 1.0);
1587 /*forall remat2 which use arg add a similar cst*/
1588 foreach_post_remat(irn, tmp) {
1592 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1593 ir_node *remat_arg = get_irn_n(tmp, i);
1594 op_t *remat_op = get_irn_link(tmp);
1596 if(remat_arg == arg) {
1597 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
1599 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1600 cst = lpp_add_cst(si->lpp, buf, lpp_greater, 0.0);
1601 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1602 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1607 /* new live range begins for each argument */
1608 arg_op->attr.live_range.ilp = next_lr;
1609 arg_op->attr.live_range.op = irn;
1611 pset_insert_ptr(live, arg);
1614 /* start new live ranges for values used by remats */
1615 foreach_pre_remat(si, irn, tmp) {
1619 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1620 ir_node *remat_arg = get_irn_n(tmp, i);
1621 op_t *arg_op = get_irn_link(remat_arg);
1624 if(!has_reg_class(si, remat_arg)) continue;
1626 /* if value is becoming live through use by remat */
1627 if(!pset_find_ptr(live, remat_arg)) {
1628 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1629 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1631 arg_op->attr.live_range.ilp = prev_lr;
1632 arg_op->attr.live_range.op = irn;
1634 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat %+F\n", remat_arg, tmp));
1636 /* TODO ist das hier die richtige Stelle???? */
1637 pset_insert_ptr(live, remat_arg);
1638 add_to_spill_bb(si, bb, remat_arg);
1640 /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1644 /* iterate over L\U */
1645 pset_foreach(live, tmp) {
1646 if(!set_find_keyval(args, tmp)) {
1647 /* if a live value is not used by irn */
1648 tmp_op = get_irn_link(tmp);
1649 // assert(tmp_op->attr.live_range.op != irn);
1650 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
1651 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
1655 /* requirements for remats */
1656 foreach_pre_remat(si, irn, tmp) {
1657 op_t *remat_op = get_irn_link(tmp);
1661 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1662 ir_node *remat_arg = get_irn_n(tmp, i);
1663 op_t *arg_op = get_irn_link(remat_arg);
1665 if(!has_reg_class(si, remat_arg)) continue;
1667 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1668 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1669 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1671 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1672 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1674 /* if remat arg is also used by current op then we can use reload placed for this argument */
1675 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
1676 int index = (int)keyval->val;
1678 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.reloads[index], -1.0);
1683 /* requirements for remats2
1685 * TODO unsure if this does the right thing.
1686 * should insert values into set if they do not become live through remat and
1689 foreach_post_remat(irn, tmp) {
1690 op_t *remat_op = get_irn_link(tmp);
1694 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1695 ir_node *remat_arg = get_irn_n(tmp, i);
1696 op_t *arg_op = get_irn_link(remat_arg);
1698 if(!has_reg_class(si, remat_arg)) continue;
1700 /* only for values in L\U, the others are handled with post_use */
1701 if(!set_find_keyval(args, remat_arg)) {
1702 /* remat <= live_rang(remat_arg) */
1703 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
1704 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1706 /* if value is becoming live through use by remat2 */
1707 if(!pset_find_ptr(live, remat_arg)) {
1710 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1711 lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1713 arg_op->attr.live_range.ilp = lr;
1714 arg_op->attr.live_range.op = irn;
1716 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
1718 pset_insert_ptr(live, remat_arg);
1719 add_to_spill_bb(si, bb, remat_arg);
1722 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1723 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1728 #ifdef CHECK_POST_REMAT
1729 /* iterate over following remats and add them to check_post_remat */
1730 foreach_post_remat(irn, tmp) {
1731 op_t *remat_op = get_irn_link(tmp);
1733 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1735 lpp_set_factor_fast(si->lpp, check_post_remat, remat_op->attr.remat.ilp, 1.0);
1741 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
1743 pset_foreach(live, tmp) {
1744 assert(has_reg_class(si, tmp));
1747 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1748 ir_node *arg = get_irn_n(irn, i);
1750 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
1758 /* do something at the beginning of the block */
1760 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
1761 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
1763 pset_foreach(live, irn) {
1764 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
1767 /* construct mem_outs for all values */
1769 set_foreach(spill_bb->ilp, spill) {
1770 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
1771 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1773 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
1774 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
1776 if(pset_find_ptr(live, spill->irn)) {
1777 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
1779 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
1780 spill->mem_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1782 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1787 /* L\U is empty at bb start */
1788 /* arg is live throughout epilog if it is reg_in into this block */
1790 /* check the register pressure at the beginning of the block
1793 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
1794 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1796 pset_foreach(live, irn) {
1797 spill = set_find_spill(spill_bb->ilp, irn);
1800 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
1801 spill->reg_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1803 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
1805 foreach_post_remat(bb, irn) {
1806 op_t *remat_op = get_irn_link(irn);
1808 DBG((si->dbg, LEVEL_4, "\t next post remat: %+F\n", irn));
1809 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1811 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1814 /* forall remat2 add requirements */
1815 foreach_post_remat(bb, tmp) {
1819 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1820 ir_node *remat_arg = get_irn_n(tmp, i);
1821 op_t *remat_op = get_irn_link(tmp);
1823 if(!has_reg_class(si, remat_arg)) continue;
1825 spill = set_find_spill(spill_bb->ilp, remat_arg);
1828 /* TODO verify this is placed correctly */
1829 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
1830 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1831 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1832 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1836 /* mem_in/reg_in for live_in values, especially phis and their arguments */
1837 pset_foreach(live, irn) {
1842 spill = set_find_spill(spill_bb->ilp, irn);
1843 assert(spill && spill->irn == irn);
1845 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
1846 for (i = 0, n = get_Phi_n_preds(irn); i < n; ++i) {
1849 ir_node *phi_arg = get_Phi_pred(irn, i);
1850 ir_node *bb_p = get_Block_cfgpred_block(bb, i);
1851 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
1854 /* although the phi is in the right regclass one or more of
1855 * its arguments can be in a different one or at least to
1858 if(has_reg_class(si, phi_arg)) {
1859 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1860 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1861 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1862 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1864 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1865 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1867 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
1870 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1871 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
1875 /* else assure the value arrives on all paths in the same resource */
1877 for (i = 0, n = get_Block_n_cfgpreds(bb); i < n; ++i) {
1880 ir_node *bb_p = get_Block_cfgpred_block(bb, i);
1881 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
1884 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1885 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1886 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1887 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1889 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1890 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1892 spill_p = set_find_spill(spill_bb_p->ilp, irn);
1895 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1896 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
1901 /* first live ranges from reg_ins */
1902 pset_foreach(live, irn) {
1903 op_t *op = get_irn_link(irn);
1905 spill = set_find_spill(spill_bb->ilp, irn);
1906 assert(spill && spill->irn == irn);
1908 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
1909 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1910 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
1911 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1913 foreach_post_remat(bb, tmp) {
1914 op_t *remat_op = get_irn_link(tmp);
1916 if(remat_op->attr.remat.remat->value == irn) {
1917 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1922 /* walk forward now and compute constraints for placing spills */
1923 /* this must only be done for values that are not defined in this block */
1924 /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */
1925 pset_foreach(live, irn) {
1926 spill = set_find_spill(spill_bb->ilp, irn);
1929 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
1930 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1932 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
1933 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1935 sched_foreach_op(bb, tmp) {
1936 op_t *op = get_irn_link(tmp);
1938 if(is_Phi(tmp)) continue;
1939 assert(!is_Proj(tmp));
1942 ir_node *value = op->attr.remat.remat->value;
1945 /* only collect remats up to the first use of a value */
1946 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
1952 for (i = 0, n = get_irn_arity(tmp); i < n; ++i) {
1953 ir_node *arg = get_irn_n(tmp, i);
1956 /* if a value is used stop collecting remats */
1962 if(cst == ILP_UNDEF) break;
1967 /* if a value is used by a mem-phi, then mem_in of this value is 0 (has to be spilled again into a different slot)
1968 mem_in(phi) -> not mem_in(orig_value) TODO: how does this depend on a certain predecessor?
1971 /* mem_in of mem-phi has associated costs (but first one is free) */
1972 /* define n_mem_copies as positive integer in each predecessor block,
1973 #mem_in into this block from predecessor block - 1 weighted with SPILL_COST*execfreq(predecessor)
1983 * Speicherkopienminimierung: teste Speicherwerte auf Interferenz
1984 * und weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
1985 * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
1992 return fabs(x) < 0.00001;
1997 is_spilled(const spill_ilp_t * si, const live_range_t * lr)
1999 return !is_zero(lpp_get_var_sol(si->lpp, lr->in_mem_var));
2004 is_mem_phi(const ir_node * phi, void *data)
2006 spill_ilp_t *si = data;
2007 // return is_spilled(si, get_use_head(si, phi)->closest_use);
2012 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
2014 spill_ilp_t *si = get_irg_link(current_ir_graph);
2016 if(pset_find_ptr(si->all_possible_remats, n)) {
2017 op_t *op = (op_t*)get_irn_link(n);
2018 assert(op && op->is_remat);
2020 if(!op->attr.remat.remat->inverse) {
2021 if(op->attr.remat.pre) {
2022 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
2024 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
2029 op_t *op = (op_t*)get_irn_link(n);
2030 assert(op && op->is_remat);
2032 if(op->attr.remat.pre) {
2033 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
2035 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
2046 dump_graph_with_remats(ir_graph * irg, const char * suffix)
2048 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
2049 be_dump(irg, suffix, dump_ir_block_graph_sched);
2050 set_dump_node_vcgattr_hook(NULL);
2055 * Edge hook to dump the schedule edges with annotated register pressure.
2058 sched_pressure_edge_hook(FILE *F, ir_node *irn)
2060 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
2061 ir_node *prev = sched_prev(irn);
2062 fprintf(F, "edge:{sourcename:\"");
2064 fprintf(F, "\" targetname:\"");
2066 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
2067 fprintf(F, "\" color:magenta}\n");
2073 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
2075 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
2077 dump_consts_local(0);
2078 set_dump_node_edge_hook(sched_pressure_edge_hook);
2079 dump_ir_block_graph(irg, suffix);
2080 set_dump_node_edge_hook(old);
2084 walker_pressure_annotator(ir_node * bb, void * data)
2086 spill_ilp_t *si = data;
2091 pset *live = pset_new_ptr_default();
2094 live_foreach(bb, li) {
2095 irn = (ir_node *) li->irn;
2097 if (live_is_end(li) && has_reg_class(si, irn)) {
2098 pset_insert_ptr(live, irn);
2102 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
2104 sched_foreach_reverse(bb, irn) {
2106 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
2110 if(has_reg_class(si, irn)) {
2111 pset_remove_ptr(live, irn);
2112 if(is_Proj(irn)) ++projs;
2115 if(!is_Proj(irn)) projs = 0;
2117 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2118 ir_node *arg = get_irn_n(irn, i);
2120 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
2122 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
2129 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
2131 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
2136 connect_all_remats_with_keep(spill_ilp_t * si)
2144 n_remats = pset_count(si->all_possible_remats);
2146 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
2149 pset_foreach(si->all_possible_remats, irn) {
2154 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
2156 obstack_free(si->obst, ins);
2162 connect_all_spills_with_keep(spill_ilp_t * si)
2171 n_spills = pset_count(si->spills);
2173 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
2176 pset_foreach(si->spills, irn) {
2181 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
2183 obstack_free(si->obst, ins);
2187 /** insert a spill at an arbitrary position */
2188 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert, ir_node *ctx)
2190 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
2191 ir_graph *irg = get_irn_irg(bl);
2192 ir_node *frame = get_irg_frame(irg);
2196 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
2197 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
2199 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
2202 * search the right insertion point. a spill of a phi cannot be put
2203 * directly after the phi, if there are some phis behind the one which
2204 * is spilled. Also, a spill of a Proj must be after all Projs of the
2207 * Here's one special case:
2208 * If the spill is in the start block, the spill must be after the frame
2209 * pointer is set up. This is done by setting insert to the end of the block
2210 * which is its default initialization (see above).
2213 if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
2216 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
2219 sched_add_after(insert, spill);
2224 delete_remat(spill_ilp_t * si, ir_node * remat) {
2227 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2229 sched_remove(remat);
2231 /* kill links to operands */
2232 for (i = -1, n = get_irn_arity(remat); i < n; ++i) {
2233 set_irn_n(remat, i, bad);
2238 clean_remat_info(spill_ilp_t * si)
2243 remat_info_t *remat_info;
2244 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2246 set_foreach(si->remat_info, remat_info) {
2247 if(!remat_info->remats) continue;
2249 pset_foreach(remat_info->remats, remat)
2251 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
2252 set_irn_n(remat->proj, -1, bad);
2253 set_irn_n(remat->proj, 0, bad);
2256 if(get_irn_n_edges(remat->op) == 0) {
2257 for (i = -1, n = get_irn_arity(remat->op); i < n; ++i) {
2258 set_irn_n(remat->op, i, bad);
2263 if(remat_info->remats) del_pset(remat_info->remats);
2264 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
2269 delete_unnecessary_remats(spill_ilp_t * si)
2274 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2277 ir_node *end = get_irg_end(si->chordal_env->irg);
2280 for (i = 0, n = get_irn_arity(si->keep); i < n; ++i) {
2281 ir_node *keep_arg = get_irn_n(si->keep, i);
2282 op_t *arg_op = get_irn_link(keep_arg);
2285 assert(arg_op->is_remat);
2287 name = si->lpp->vars[arg_op->attr.remat.ilp];
2289 if(is_zero(name->value)) {
2290 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
2291 /* TODO check whether reload is preferred over remat (could be bug) */
2292 delete_remat(si, keep_arg);
2294 if(!arg_op->attr.remat.remat->inverse) {
2295 if(arg_op->attr.remat.pre) {
2296 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
2298 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
2301 if(arg_op->attr.remat.pre) {
2302 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
2304 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
2309 set_irn_n(si->keep, i, bad);
2312 for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
2313 ir_node *end_arg = get_End_keepalive(end, i);
2315 if(end_arg != si->keep) {
2316 obstack_grow(si->obst, &end_arg, sizeof(end_arg));
2319 keeps = obstack_finish(si->obst);
2320 set_End_keepalives(end, n-1, keeps);
2321 obstack_free(si->obst, keeps);
2324 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
2329 pset_foreach(si->all_possible_remats, remat) {
2330 op_t *remat_op = get_irn_link(remat);
2331 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
2333 if(is_zero(name->value)) {
2334 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
2335 /* TODO check whether reload is preferred over remat (could be bug) */
2336 delete_remat(si, remat);
2338 if(!remat_op->attr.remat.remat->inverse) {
2339 if(remat_op->attr.remat.pre) {
2340 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
2342 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
2345 if(remat_op->attr.remat.pre) {
2346 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
2348 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
2357 * @param before The node after which the spill will be placed in the schedule
2359 /* TODO set context properly */
2361 insert_spill(spill_ilp_t * si, ir_node * irn, ir_node * value, ir_node * before)
2365 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2367 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
2369 spill = be_spill2(arch_env, irn, before, irn);
2371 defs = set_insert_def(si->values, value);
2374 /* enter into the linked list */
2375 set_irn_link(spill, defs->spills);
2376 defs->spills = spill;
2378 #ifdef KEEPALIVE_SPILLS
2379 pset_insert_ptr(si->spills, spill);
2386 * @param before The Phi node which has to be spilled
2389 insert_mem_phi(spill_ilp_t * si, const ir_node * phi)
2397 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
2399 for(i=0,n=get_irn_arity(phi); i<n; ++i) {
2400 ins[i] = si->m_unknown;
2403 mem_phi = new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
2405 defs = set_insert_def(si->values, phi);
2408 /* enter into the linked list */
2409 set_irn_link(mem_phi, defs->spills);
2410 defs->spills = mem_phi;
2412 sched_add_after(phi, mem_phi);
2414 #ifdef KEEPALIVE_SPILLS
2415 pset_insert_ptr(si->spills, mem_phi);
2422 * Add remat to list of defs, destroys link field!
2425 insert_remat(spill_ilp_t * si, ir_node * remat)
2428 op_t *remat_op = get_irn_link(remat);
2430 assert(remat_op->is_remat);
2432 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
2435 /* enter into the linked list */
2436 set_irn_link(remat, defs->remats);
2437 defs->remats = remat;
2442 collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
2447 defs = set_find_def(si->values, value);
2449 if(defs && defs->spills) {
2450 for(next = defs->spills; next; next = get_irn_link(next)) {
2451 pset_insert_ptr(spills, next);
2453 } else if (is_Phi(value)) {
2455 if(!pset_find_ptr(visited, value)) {
2459 pset_insert_ptr(visited, value);
2460 for(i=0, n=get_irn_arity(value); i<n; ++i) {
2461 ir_node *arg = get_irn_n(value, i);
2463 collect_spills(si, arg, spills, visited);
2467 // assert(0 && "Phi operand not spilled");
2473 get_spills_for_value(spill_ilp_t * si, ir_node * value)
2475 pset *spills = pset_new_ptr_default();
2476 // pset *visited = pset_new_ptr_default();
2478 // collect_spills(si, value, spills, visited);
2479 // del_pset(visited);
2483 defs = set_find_def(si->values, value);
2485 if(defs && defs->spills) {
2486 for(next = defs->spills; next; next = get_irn_link(next)) {
2487 pset_insert_ptr(spills, next);
2495 * Add reload before operation and add to list of defs
2498 insert_reload(spill_ilp_t * si, const ir_node * value, const ir_node * after)
2503 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2505 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
2507 defs = set_find_def(si->values, value);
2508 /* get a spill of this value */
2510 if((!defs || !defs->spills) && is_Phi(value)) {
2513 spills = get_spills_for_value(si, value);
2515 spill = pset_first(spills);
2519 defs = set_insert_def(si->values, value);
2521 defs->spills = spill;
2522 set_irn_link(spill, NULL);
2524 spill = defs->spills;
2527 spill = defs->spills;
2528 assert(spill && "no spill placed before reload");
2530 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
2532 /* enter into the linked list */
2533 set_irn_link(reload, defs->remats);
2534 defs->remats = reload;
2540 walker_spill_placer(ir_node * bb, void * data) {
2541 spill_ilp_t *si = (spill_ilp_t*)data;
2543 spill_bb_t *spill_bb = get_irn_link(bb);
2544 pset *spills_to_do = pset_new_ptr_default();
2547 set_foreach(spill_bb->ilp, spill) {
2550 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2551 name = si->lpp->vars[spill->mem_in];
2552 if(!is_zero(name->value)) {
2555 mem_phi = insert_mem_phi(si, spill->irn);
2557 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
2561 name = si->lpp->vars[spill->spill];
2562 if(!is_zero(name->value)) {
2563 if(spill->reg_in > 0) {
2564 name = si->lpp->vars[spill->reg_in];
2565 if(!is_zero(name->value)) {
2566 insert_spill(si, spill->irn, spill->irn, bb);
2570 pset_insert_ptr(spills_to_do, spill->irn);
2573 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
2576 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
2577 op_t *op = get_irn_link(irn);
2579 if(be_is_Spill(irn)) continue;
2582 /* TODO fix this if we want to support remats with more than two nodes */
2583 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
2584 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
2586 insert_spill(si, irn, op->attr.remat.remat->value, irn);
2589 if(pset_find_ptr(spills_to_do, irn)) {
2590 pset_remove_ptr(spills_to_do, irn);
2592 insert_spill(si, irn, irn, irn);
2598 assert(pset_count(spills_to_do) == 0);
2600 /* afterwards free data in block */
2601 del_pset(spills_to_do);
2605 phim_fixer(spill_ilp_t *si) {
2608 set_foreach(si->values, defs) {
2609 const ir_node *phi = defs->value;
2610 ir_node *phi_m = NULL;
2611 ir_node *next = defs->spills;
2615 if(!is_Phi(phi)) continue;
2618 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
2622 next = get_irn_link(next);
2625 if(!phi_m) continue;
2627 for(i=0,n=get_irn_arity(phi); i<n; ++i) {
2628 const ir_node *value = get_irn_n(phi, i);
2629 defs_t *val_defs = set_find_def(si->values, value);
2631 /* get a spill of this value */
2632 ir_node *spill = val_defs->spills;
2634 assert(spill && "no spill placed before PhiM");
2636 set_irn_n(phi_m, i, spill);
2642 walker_reload_placer(ir_node * bb, void * data) {
2643 spill_ilp_t *si = (spill_ilp_t*)data;
2645 spill_bb_t *spill_bb = get_irn_link(bb);
2649 sched_foreach_reverse(bb, irn) {
2650 op_t *op = get_irn_link(irn);
2652 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
2653 if(is_Phi(irn)) break;
2656 if(get_irn_mode(irn) != mode_T) {
2657 insert_remat(si, irn);
2662 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2663 ir_node *arg = get_irn_n(irn, i);
2665 if(op->attr.live_range.reloads && op->attr.live_range.reloads[i] != ILP_UNDEF) {
2668 name = si->lpp->vars[op->attr.live_range.reloads[i]];
2669 if(!is_zero(name->value)) {
2671 ir_node *insert_pos = irn;
2672 ir_node *prev = insert_pos;
2676 prev = sched_prev(prev);
2677 } while(be_is_Spill(prev));
2679 prev_op = get_irn_link(prev);
2681 /* insert reload before pre-remats */
2682 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
2683 && prev_op->is_remat && prev_op->attr.remat.pre) {
2687 prev = sched_prev(prev);
2688 } while(be_is_Spill(prev));
2690 prev_op = get_irn_link(prev);
2694 reload = insert_reload(si, arg, insert_pos);
2696 set_irn_n(irn, i, reload);
2698 #ifdef KEEPALIVE_RELOADS
2699 pset_insert_ptr(si->spills, reload);
2707 /* reloads at end of block */
2708 if(spill_bb->reloads) {
2710 live_foreach(bb, li) {
2711 ir_node *irn = (ir_node *) li->irn;
2713 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
2716 name = si->lpp->vars[spill_bb->reloads[i]];
2717 if(!is_zero(name->value)) {
2719 ir_node *insert_pos = bb;
2720 ir_node *prev = sched_prev(insert_pos);
2721 op_t *prev_op = get_irn_link(prev);
2723 /* insert reload before pre-remats */
2724 while(!sched_is_end(prev) && !be_is_Reload(prev) && !be_is_Spill(prev)
2725 && prev_op->is_remat && prev_op->attr.remat.pre) {
2728 prev = sched_prev(insert_pos);
2729 prev_op = get_irn_link(prev);
2732 reload = insert_reload(si, irn, insert_pos);
2734 #ifdef KEEPALIVE_RELOADS
2735 pset_insert_ptr(si->spills, reload);
2743 del_set(spill_bb->ilp);
2747 walker_collect_used(ir_node * irn, void * data)
2749 lc_bitset_t *used = data;
2751 lc_bitset_set(used, get_irn_idx(irn));
2754 struct kill_helper {
2760 walker_kill_unused(ir_node * bb, void * data)
2762 struct kill_helper *kh = data;
2763 const ir_node *bad = get_irg_bad(get_irn_irg(bb));
2767 for(irn=sched_first(bb); !sched_is_end(irn);) {
2768 ir_node *next = sched_next(irn);
2772 if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) {
2773 if(be_is_Spill(irn) || be_is_Reload(irn)) {
2774 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)));
2775 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
2780 set_nodes_block(irn, bad);
2781 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
2782 set_irn_n(irn, i, bad);
2790 kill_all_unused_values_in_schedule(spill_ilp_t * si)
2792 struct kill_helper kh;
2794 kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
2797 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
2798 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
2800 lc_bitset_free(kh.used);
2804 print_irn_pset(pset * p)
2808 pset_foreach(p, irn) {
2809 ir_printf("%+F\n", irn);
2814 rewire_uses(spill_ilp_t * si)
2816 dom_front_info_t *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
2818 pset *ignore = pset_new_ptr(1);
2820 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
2822 /* then fix uses of spills */
2823 set_foreach(si->values, defs) {
2826 ir_node *next = defs->remats;
2829 reloads = pset_new_ptr_default();
2832 if(be_is_Reload(next)) {
2833 pset_insert_ptr(reloads, next);
2837 next = get_irn_link(next);
2840 spills = get_spills_for_value(si, defs->value);
2841 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));
2842 if(pset_count(spills) > 1) {
2843 //assert(pset_count(reloads) > 0);
2844 // print_irn_pset(spills);
2845 // print_irn_pset(reloads);
2847 be_ssa_constr_set_ignore(dfi, spills, ignore);
2854 /* first fix uses of remats and reloads */
2855 set_foreach(si->values, defs) {
2857 ir_node *next = defs->remats;
2860 nodes = pset_new_ptr_default();
2861 pset_insert_ptr(nodes, defs->value);
2864 pset_insert_ptr(nodes, next);
2865 next = get_irn_link(next);
2868 if(pset_count(nodes) > 1) {
2869 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value));
2870 be_ssa_constr_set(dfi, nodes);
2877 // remove_unused_defs(si);
2879 be_free_dominance_frontiers(dfi);
2883 writeback_results(spill_ilp_t * si)
2885 /* walk through the graph and collect all spills, reloads and remats for a value */
2887 si->values = new_set(cmp_defs, 4096);
2889 DBG((si->dbg, LEVEL_1, "Applying results\n"));
2890 delete_unnecessary_remats(si);
2891 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
2892 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
2894 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
2896 /* clean the remat info! there are still back-edges leading there! */
2897 clean_remat_info(si);
2901 connect_all_spills_with_keep(si);
2903 del_set(si->values);
2907 get_n_regs(spill_ilp_t * si)
2909 int arch_n_regs = arch_register_class_n_regs(si->cls);
2913 for(i=0; i<arch_n_regs; i++) {
2914 if(!arch_register_type_is(&si->cls->regs[i], ignore)) {
2919 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name));
2924 walker_reload_mover(ir_node * bb, void * data)
2926 spill_ilp_t *si = data;
2929 sched_foreach(bb, tmp) {
2930 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
2931 ir_node *reload = tmp;
2934 /* move reload upwards */
2936 int pressure = (int)get_irn_link(reload);
2937 if(pressure < si->n_regs) {
2938 irn = sched_prev(reload);
2939 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
2940 sched_remove(reload);
2941 pressure = (int)get_irn_link(irn);
2943 while(pressure < si->n_regs) {
2944 if(sched_is_end(irn) || (be_is_Reload(irn) && has_reg_class(si, irn))) break;
2946 set_irn_link(irn, INT_TO_PTR(pressure+1));
2947 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
2948 irn = sched_prev(irn);
2950 pressure = (int)get_irn_link(irn);
2953 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
2954 sched_put_after(irn, reload);
2961 move_reloads_upward(spill_ilp_t * si)
2963 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
2967 be_spill_remat(const be_chordal_env_t * chordal_env)
2969 char problem_name[256];
2970 char dump_suffix[256];
2971 char dump_suffix2[256];
2972 char dump_suffix3[256];
2973 struct obstack obst;
2976 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
2977 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
2978 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
2979 ir_snprintf(dump_suffix3, sizeof(dump_suffix3), "-%s-reloads_moved", chordal_env->cls->name);
2981 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
2982 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
2984 obstack_init(&obst);
2985 si.chordal_env = chordal_env;
2987 si.cls = chordal_env->cls;
2988 si.lpp = new_lpp(problem_name, lpp_minimize);
2989 si.remat_info = new_set(cmp_remat_info, 4096);
2990 si.all_possible_remats = pset_new_ptr_default();
2991 si.spills = pset_new_ptr_default();
2992 si.inverse_ops = pset_new_ptr_default();
2993 #ifndef EXECFREQ_LOOPDEPH
2994 si.execfreqs = compute_execfreq(chordal_env->irg);
2996 si.execfreqs = NULL;
3001 si.n_regs = get_n_regs(&si);
3003 set_irg_link(chordal_env->irg, &si);
3004 compute_doms(chordal_env->irg);
3006 be_analyze_regpressure(chordal_env, "-pre");
3008 #ifdef COLLECT_REMATS
3009 /* collect remats */
3010 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
3011 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
3014 /* insert possible remats */
3015 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
3016 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
3017 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
3020 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
3021 connect_all_remats_with_keep(&si);
3022 /* dump graph with inserted remats */
3023 dump_graph_with_remats(chordal_env->irg, dump_suffix);
3027 /* recompute liveness */
3028 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
3029 be_liveness(chordal_env->irg);
3033 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
3034 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
3035 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
3037 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
3038 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
3045 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
3046 if ((f = fopen(buf, "wt")) != NULL) {
3047 lpp_dump_plain(si.lpp, f);
3054 DBG((si.dbg, LEVEL_1, "\tSolving %F\n", chordal_env->irg));
3056 lpp_set_time_limit(si.lpp, ILP_TIMEOUT);
3060 lpp_solve_cplex(si.lpp);
3062 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
3064 assert(lpp_is_sol_valid(si.lpp)
3065 && "solution of ILP must be valid");
3067 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));
3069 #ifdef DUMP_SOLUTION
3074 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
3075 if ((f = fopen(buf, "wt")) != NULL) {
3077 for (i = 0; i < si.lpp->var_next; ++i) {
3078 lpp_name_t *name = si.lpp->vars[i];
3079 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
3086 writeback_results(&si);
3090 kill_all_unused_values_in_schedule(&si);
3092 #if defined(KEEPALIVE_SPILLS) || defined(KEEPALIVE_RELOADS)
3093 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
3096 be_liveness(chordal_env->irg);
3097 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3099 dump_pressure_graph(&si, dump_suffix2);
3101 // TODO fix temporarily exceeded regpressure due to remat2s
3103 // TODO insert copys to fix interferences in memory
3105 // move reloads upwards
3106 move_reloads_upward(&si);
3107 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3108 dump_pressure_graph(&si, dump_suffix3);
3110 be_analyze_regpressure(chordal_env, "-post");
3112 free_dom(chordal_env->irg);
3113 del_pset(si.inverse_ops);
3114 del_pset(si.all_possible_remats);
3115 del_pset(si.spills);
3116 #ifndef EXECFREQ_LOOPDEPH
3117 free_execfreq(si.execfreqs);
3120 obstack_free(&obst, NULL);
3124 #else /* WITH_ILP */
3127 only_that_you_can_compile_without_WITH_ILP_defined(void)
3131 #endif /* WITH_ILP */