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
71 //#define NO_MEMCOPIES
75 #define LPP_SERVER "i44pc52"
76 #define LPP_SOLVER "cplex"
82 #define ILP_TIMEOUT 120
86 typedef struct _spill_ilp_t {
87 const arch_register_class_t *cls;
89 const be_chordal_env_t *chordal_env;
93 pset *all_possible_remats;
98 set *values; /**< for collecting all definitions of values before running ssa-construction */
103 DEBUG_ONLY(firm_dbg_module_t * dbg);
106 typedef int ilp_var_t;
107 typedef int ilp_cst_t;
109 typedef struct _spill_bb_t {
114 typedef struct _remat_t {
115 const ir_node *op; /**< for copy_irn */
116 const ir_node *value; /**< the value which is being recomputed by this remat */
117 ir_node *proj; /**< not NULL if the above op produces a tuple */
118 int cost; /**< cost of this remat */
119 int inverse; /**< nonzero if this is an inverse remat */
123 * Data to be attached to each IR node. For remats this contains the ilp_var
124 * for this remat and for normal ops this contains the ilp_vars for
125 * reloading each operand
127 typedef struct _op_t {
132 remat_t *remat; /** the remat this op belongs to */
133 int pre; /** 1, if this is a pressure-increasing remat */
137 ir_node *op; /** the operation this live range belongs to */
146 typedef struct _defs_t {
148 ir_node *spills; /**< points to the first spill for this value (linked by link field) */
149 ir_node *remats; /**< points to the first definition for this value (linked by link field) */
152 typedef struct _remat_info_t {
153 const ir_node *irn; /**< the irn to which these remats belong */
154 pset *remats; /**< possible remats for this value */
155 pset *remats_by_operand; /**< remats with this value as operand */
158 typedef struct _keyval_t {
163 typedef struct _spill_t {
173 has_reg_class(const spill_ilp_t * si, const ir_node * irn)
175 return chordal_has_class(si->chordal_env, irn);
180 cmp_remat(const void *a, const void *b)
182 const keyval_t *p = a;
183 const keyval_t *q = b;
184 const remat_t *r = p->val;
185 const remat_t *s = q->val;
189 return !(r == s || r->op == s->op);
193 cmp_remat(const void *a, const void *b)
195 const remat_t *r = a;
196 const remat_t *s = a;
198 return !(r == s || r->op == s->op);
202 cmp_spill(const void *a, const void *b, size_t size)
204 const spill_t *p = a;
205 const spill_t *q = b;
207 // return !(p->irn == q->irn && p->bb == q->bb);
208 return !(p->irn == q->irn);
212 set_find_keyval(set * set, void * key)
217 return set_find(set, &query, sizeof(query), HASH_PTR(key));
221 set_insert_keyval(set * set, void * key, void * val)
227 return set_insert(set, &query, sizeof(query), HASH_PTR(key));
231 set_find_def(set * set, ir_node * value)
236 return set_find(set, &query, sizeof(query), HASH_PTR(value));
240 set_insert_def(set * set, ir_node * value)
247 return set_insert(set, &query, sizeof(query), HASH_PTR(value));
251 set_find_spill(set * set, ir_node * value)
256 return set_find(set, &query, sizeof(query), HASH_PTR(value));
259 #define pset_foreach(s,i) for((i)=pset_first((s)); (i); (i)=pset_next((s)))
260 #define set_foreach(s,i) for((i)=set_first((s)); (i); (i)=set_next((s)))
261 #define foreach_post_remat(s,i) for((i)=next_post_remat((s)); (i); (i)=next_post_remat((i)))
262 #define foreach_pre_remat(si,s,i) for((i)=next_pre_remat((si),(s)); (i); (i)=next_pre_remat((si),(i)))
263 #define sched_foreach_op(s,i) for((i)=sched_next_op((s));!sched_is_end((i));(i)=sched_next_op((i)))
266 cmp_remat_info(const void *a, const void *b, size_t size)
268 const remat_info_t *p = a;
269 const remat_info_t *q = b;
271 return !(p->irn == q->irn);
275 cmp_defs(const void *a, const void *b, size_t size)
280 return !(p->value == q->value);
284 cmp_keyval(const void *a, const void *b, size_t size)
286 const keyval_t *p = a;
287 const keyval_t *q = b;
289 return !(p->key == q->key);
293 execution_frequency(const spill_ilp_t * si, const ir_node * irn)
298 return get_block_execfreq(si->execfreqs, irn) + FUDGE;
300 return get_block_execfreq(si->execfreqs, get_nodes_block(irn)) + FUDGE;
304 return exp(get_loop_depth(get_irn_loop(irn)) * log(10)) + FUDGE;
306 return exp(get_loop_depth(get_irn_loop(get_nodes_block(irn))) * log(10)) + FUDGE;
311 get_cost(const spill_ilp_t * si, const ir_node * irn)
313 if(be_is_Spill(irn)) {
315 } else if(be_is_Reload(irn)){
318 return arch_get_op_estimated_cost(si->chordal_env->birg->main_env->arch_env, irn);
324 * Checks, whether node and its operands have suitable reg classes
327 is_rematerializable(const spill_ilp_t * si, const ir_node * irn)
331 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
332 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
336 ir_fprintf(stderr, " Node %+F is not rematerializable\n", irn);
339 for (i = 0, n = get_irn_arity(irn); i < n && remat; ++i) {
340 ir_node *op = get_irn_n(irn, i);
341 remat &= has_reg_class(si, op) || arch_irn_get_flags(arch_env, op) & arch_irn_flags_ignore || (get_irn_op(op) == op_NoMem);
344 // ir_fprintf(stderr, " Argument %d (%+F) of Node %+F has wrong regclass\n", i, op, irn);
351 * Try to create a remat from @p op with destination value @p dest_value
353 static INLINE remat_t *
354 get_remat_from_op(spill_ilp_t * si, const ir_node * dest_value, const ir_node * op)
356 remat_t *remat = NULL;
358 // if(!mode_is_datab(get_irn_mode(dest_value)))
361 if(dest_value == op) {
362 const ir_node *proj = NULL;
364 if(is_Proj(dest_value)) {
365 op = get_irn_n(op, 0);
369 if(!is_rematerializable(si, op))
372 remat = obstack_alloc(si->obst, sizeof(*remat));
374 remat->cost = get_cost(si, op);
375 remat->value = dest_value;
379 arch_inverse_t inverse;
383 /* get the index of the operand we want to retrieve by the inverse op */
384 for (i = 0, n = get_irn_arity(op); i < n; ++i) {
385 ir_node *arg = get_irn_n(op, i);
387 if(arg == dest_value) break;
389 if(i == n) return NULL;
391 DBG((si->dbg, LEVEL_5, "\t requesting inverse op for argument %d of op %+F\n", i, op));
393 /* else ask the backend to give an inverse op */
394 if(arch_get_inverse(si->chordal_env->birg->main_env->arch_env, op, i, &inverse, si->obst)) {
397 DBG((si->dbg, LEVEL_4, "\t backend gave us an inverse op with %d nodes and cost %d\n", inverse.n, inverse.costs));
399 assert(inverse.n > 0 && "inverse op should have at least one node");
401 for(i=0; i<inverse.n; ++i) {
402 pset_insert_ptr(si->inverse_ops, inverse.nodes[i]);
406 remat = obstack_alloc(si->obst, sizeof(*remat));
407 remat->op = inverse.nodes[0];
408 remat->cost = inverse.costs;
409 remat->value = dest_value;
410 remat->proj = (inverse.n==2)?inverse.nodes[1]:NULL;
413 assert(is_Proj(remat->proj));
415 assert(0 && "I can not handle remats with more than 2 nodes");
422 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F with %+F\n", remat->op, dest_value, op, remat->proj));
424 DBG((si->dbg, LEVEL_3, "\t >Found remat %+F for %+F from %+F\n", remat->op, dest_value, op));
432 add_remat(const spill_ilp_t * si, const remat_t * remat)
434 remat_info_t *remat_info,
440 assert(remat->value);
442 query.irn = remat->value;
444 query.remats_by_operand = NULL;
445 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(remat->value));
447 if(remat_info->remats == NULL) {
448 remat_info->remats = new_pset(cmp_remat, 4096);
450 pset_insert(remat_info->remats, remat, HASH_PTR(remat->op));
452 /* insert the remat into the remats_be_operand set of each argument of the remat op */
453 for (i = 0, n = get_irn_arity(remat->op); i < n; ++i) {
454 ir_node *arg = get_irn_n(remat->op, i);
458 query.remats_by_operand = NULL;
459 remat_info = set_insert(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
461 if(remat_info->remats_by_operand == NULL) {
462 remat_info->remats_by_operand = new_pset(cmp_remat, 4096);
464 pset_insert(remat_info->remats_by_operand, remat, HASH_PTR(remat->op));
469 get_irn_n_nonremat_edges(const spill_ilp_t * si, const ir_node * irn)
471 const ir_edge_t *edge = get_irn_out_edge_first(irn);
475 if(!pset_find_ptr(si->inverse_ops, edge->src)) {
478 edge = get_irn_out_edge_next(irn, edge);
485 get_remats_from_op(spill_ilp_t * si, const ir_node * op)
491 #ifdef NO_SINGLE_USE_REMATS
492 if(has_reg_class(si, op) && (get_irn_n_nonremat_edges(si, op) > 1)) {
494 if(has_reg_class(si, op)) {
496 remat = get_remat_from_op(si, op, op);
498 add_remat(si, remat);
502 #ifdef COLLECT_INVERSE_REMATS
503 /* repeat the whole stuff for each remat retrieved by get_remat_from_op(op, arg)
505 for (i = 0, n = get_irn_arity(op); i < n; ++i) {
506 ir_node *arg = get_irn_n(op, i);
508 if(has_reg_class(si, arg)) {
509 /* try to get an inverse remat */
510 remat = get_remat_from_op(si, arg, op);
512 add_remat(si, remat);
521 value_is_defined_before(const spill_ilp_t * si, const ir_node * pos, const ir_node * val)
524 ir_node *def_block = get_nodes_block(val);
530 /* if pos is at end of a basic block */
532 ret = (pos == def_block || block_dominates(def_block, pos));
533 // ir_fprintf(stderr, "(def(bb)=%d) ", ret);
537 /* else if this is a normal operation */
538 block = get_nodes_block(pos);
539 if(block == def_block) {
540 if(!sched_is_scheduled(val)) return 1;
542 ret = sched_comes_after(val, pos);
543 // ir_fprintf(stderr, "(def(same block)=%d) ",ret);
547 ret = block_dominates(def_block, block);
548 // ir_fprintf(stderr, "(def(other block)=%d) ", ret);
552 static INLINE ir_node *
553 sched_block_last_noncf(const spill_ilp_t * si, const ir_node * bb)
555 return sched_skip((ir_node*)bb, 0, sched_skip_cf_predicator, (void *) si->chordal_env->birg->main_env->arch_env);
559 * Returns first non-Phi node of block @p bb
561 static INLINE ir_node *
562 sched_block_first_nonphi(const ir_node * bb)
564 return sched_skip((ir_node*)bb, 1, sched_skip_phi_predicator, NULL);
568 sched_skip_proj_predicator(const ir_node * irn, void * data)
570 return (is_Proj(irn));
573 static INLINE ir_node *
574 sched_next_nonproj(const ir_node * irn, int forward)
576 return sched_skip((ir_node*)irn, forward, sched_skip_proj_predicator, NULL);
580 * Returns next operation node (non-Proj) after @p irn
581 * or the basic block of this node
583 static INLINE ir_node *
584 sched_next_op(const ir_node * irn)
586 ir_node *next = sched_next(irn);
591 return sched_next_nonproj(next, 1);
595 * Returns previous operation node (non-Proj) before @p irn
596 * or the basic block of this node
598 static INLINE ir_node *
599 sched_prev_op(const ir_node * irn)
601 ir_node *prev = sched_prev(irn);
606 return sched_next_nonproj(prev, 0);
610 sched_put_after(ir_node * insert, ir_node * irn)
612 if(is_Block(insert)) {
613 insert = sched_block_first_nonphi(insert);
615 insert = sched_next_op(insert);
617 sched_add_before(insert, irn);
621 sched_put_before(const spill_ilp_t * si, ir_node * insert, ir_node * irn)
623 if(is_Block(insert)) {
624 insert = sched_block_last_noncf(si, insert);
626 insert = sched_next_nonproj(insert, 0);
627 insert = sched_prev(insert);
629 sched_add_after(insert, irn);
633 * Tells you whether a @p remat can be placed before the irn @p pos
636 can_remat_before(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
638 const ir_node *op = remat->op;
645 prev = sched_block_last_noncf(si, pos);
646 prev = sched_next_nonproj(prev, 0);
648 prev = sched_prev_op(pos);
650 /* do not remat if the rematted value is defined immediately before this op */
651 if(prev == remat->op) {
656 /* this should be just fine, the following OP will be using this value, right? */
658 /* only remat AFTER the real definition of a value (?) */
659 if(!value_is_defined_before(si, pos, remat->value)) {
660 // ir_fprintf(stderr, "error(not defined)");
665 for(i=0, n=get_irn_arity(op); i<n && res; ++i) {
666 const ir_node *arg = get_irn_n(op, i);
668 #ifdef NO_ENLARGE_L1V3N355
669 if(has_reg_class(si, arg) && live) {
670 res &= pset_find_ptr(live, arg)?1:0;
672 res &= value_is_defined_before(si, pos, arg);
675 res &= value_is_defined_before(si, pos, arg);
683 * Tells you whether a @p remat can be placed after the irn @p pos
686 can_remat_after(const spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
689 pos = sched_block_first_nonphi(pos);
691 pos = sched_next_op(pos);
694 /* only remat AFTER the real definition of a value (?) */
695 if(!value_is_defined_before(si, pos, remat->value)) {
699 return can_remat_before(si, remat, pos, live);
703 * Collect potetially rematerializable OPs
706 walker_remat_collector(ir_node * irn, void * data)
708 spill_ilp_t *si = data;
710 if(!is_Block(irn) && !is_Phi(irn)) {
711 DBG((si->dbg, LEVEL_4, "\t Processing %+F\n", irn));
712 get_remats_from_op(si, irn);
717 * Inserts a copy of @p irn before @p pos
720 insert_copy_before(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
725 bb = is_Block(pos)?pos:get_nodes_block(pos);
726 copy = exact_copy(irn);
727 set_nodes_block(copy, bb);
728 sched_put_before(si, pos, copy);
734 * Inserts a copy of @p irn after @p pos
737 insert_copy_after(const spill_ilp_t * si, const ir_node * irn, ir_node * pos)
742 bb = is_Block(pos)?pos:get_nodes_block(pos);
743 copy = exact_copy(irn);
744 set_nodes_block(copy, bb);
745 sched_put_after(pos, copy);
751 insert_remat_after(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
755 if(can_remat_after(si, remat, pos, live)) {
760 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
762 copy = insert_copy_after(si, remat->op, pos);
764 // ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", remat->value, pos);
765 ir_snprintf(buf, sizeof(buf), "remat2_%N_%N", copy, pos);
766 op = obstack_alloc(si->obst, sizeof(*op));
768 op->attr.remat.remat = remat;
769 op->attr.remat.pre = 0;
770 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
772 set_irn_link(copy, op);
773 pset_insert_ptr(si->all_possible_remats, copy);
775 proj_copy = insert_copy_after(si, remat->proj, copy);
776 set_irn_n(proj_copy, 0, copy);
777 set_irn_link(proj_copy, op);
778 pset_insert_ptr(si->all_possible_remats, proj_copy);
786 insert_remat_before(spill_ilp_t * si, const remat_t * remat, const ir_node * pos, const pset * live)
790 if(can_remat_before(si, remat, pos, live)) {
795 DBG((si->dbg, LEVEL_3, "\t >inserting remat %+F\n", remat->op));
797 copy = insert_copy_before(si, remat->op, pos);
799 // ir_snprintf(buf, sizeof(buf), "remat_%N_%N", remat->value, pos);
800 ir_snprintf(buf, sizeof(buf), "remat_%N_%N", copy, pos);
801 op = obstack_alloc(si->obst, sizeof(*op));
803 op->attr.remat.remat = remat;
804 op->attr.remat.pre = 1;
805 op->attr.remat.ilp = lpp_add_var(si->lpp, buf, lpp_binary, remat->cost*execution_frequency(si, pos));
807 set_irn_link(copy, op);
808 pset_insert_ptr(si->all_possible_remats, copy);
810 proj_copy = insert_copy_after(si, remat->proj, copy);
811 set_irn_n(proj_copy, 0, copy);
812 set_irn_link(proj_copy, op);
813 pset_insert_ptr(si->all_possible_remats, proj_copy);
821 get_block_n_succs(const ir_node *block) {
822 const ir_edge_t *edge;
824 assert(edges_activated(current_ir_graph));
826 edge = get_block_succ_first(block);
830 edge = get_block_succ_next(block, edge);
835 is_merge_edge(const ir_node * bb)
837 #ifdef GOODWIN_REDUCTION
838 return get_block_n_succs(bb) == 1;
845 is_diverge_edge(const ir_node * bb)
847 #ifdef GOODWIN_REDUCTION
848 return get_Block_n_cfgpreds(bb) == 1;
855 * Insert (so far unused) remats into the irg to
856 * recompute the potential liveness of all values
859 walker_remat_insertor(ir_node * bb, void * data)
861 spill_ilp_t *si = data;
862 spill_bb_t *spill_bb;
866 pset *live = pset_new_ptr_default();
868 DBG((si->dbg, LEVEL_3, "\t Entering %+F\n\n", bb));
870 live_foreach(bb, li) {
871 ir_node *value = (ir_node *) li->irn;
873 /* add remats at end of block */
874 if (live_is_end(li) && has_reg_class(si, value)) {
875 pset_insert_ptr(live, value);
879 spill_bb = obstack_alloc(si->obst, sizeof(*spill_bb));
880 set_irn_link(bb, spill_bb);
882 irn = sched_last(bb);
883 while(!sched_is_end(irn)) {
889 next = sched_prev(irn);
891 DBG((si->dbg, LEVEL_5, "\t at %+F (next: %+F)\n", irn, next));
893 if(is_Phi(irn) || is_Proj(irn)) {
896 if(has_reg_class(si, irn)) {
897 pset_remove_ptr(live, irn);
900 op = obstack_alloc(si->obst, sizeof(*op));
902 op->attr.live_range.args.reloads = NULL;
903 op->attr.live_range.ilp = ILP_UNDEF;
904 set_irn_link(irn, op);
910 op = obstack_alloc(si->obst, sizeof(*op));
912 op->attr.live_range.ilp = ILP_UNDEF;
913 op->attr.live_range.args.reloads = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
914 memset(op->attr.live_range.args.reloads, 0xFF, sizeof(*op->attr.live_range.args.reloads) * get_irn_arity(irn));
915 set_irn_link(irn, op);
917 args = pset_new_ptr_default();
919 /* collect arguments of op */
920 for (n = get_irn_arity(irn)-1; n>=0; --n) {
921 ir_node *arg = get_irn_n(irn, n);
923 pset_insert_ptr(args, arg);
926 /* set args of op live in epilog */
927 pset_foreach(args, arg) {
928 if(has_reg_class(si, arg)) {
929 pset_insert_ptr(live, arg);
933 /* insert all possible remats after irn */
934 pset_foreach(args, arg) {
935 remat_info_t *remat_info,
939 /* continue if the operand has the wrong reg class
941 if(!has_reg_class(si, arg))
946 query.remats_by_operand = NULL;
947 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
953 /* do not place post remats after jumps */
954 if(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) continue;
956 if(remat_info->remats_by_operand) {
957 pset_foreach(remat_info->remats_by_operand, remat) {
958 /* do not insert remats producing the same value as one of the operands */
959 if(!pset_find_ptr(args, remat->value)) {
960 DBG((si->dbg, LEVEL_4, "\t considering remat %+F with arg %+F\n", remat->op, arg));
961 #ifdef REMAT_WHILE_LIVE
962 if(pset_find_ptr(live, remat->value)) {
963 insert_remat_after(si, remat, irn, live);
966 insert_remat_after(si, remat, irn, live);
973 /* delete defined value from live set */
974 if(has_reg_class(si, irn)) {
975 pset_remove_ptr(live, irn);
978 /* insert all possible remats before irn */
979 pset_foreach(args, arg) {
980 remat_info_t *remat_info,
984 /* continue if the operand has the wrong reg class
986 if(!has_reg_class(si, arg))
991 query.remats_by_operand = NULL;
992 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(arg));
998 if(remat_info->remats) {
999 pset_foreach(remat_info->remats, remat) {
1000 DBG((si->dbg, LEVEL_4, "\t considering remat %+F for arg %+F\n", remat->op, arg));
1001 #ifdef REMAT_WHILE_LIVE
1002 if(pset_find_ptr(live, remat->value)) {
1003 insert_remat_before(si, remat, irn, live);
1006 insert_remat_before(si, remat, irn, live);
1016 live_foreach(bb, li) {
1017 ir_node *value = (ir_node *) li->irn;
1019 /* add remats at end if successor has multiple predecessors */
1020 if(is_merge_edge(bb)) {
1021 /* add remats at end of block */
1022 if (live_is_end(li) && has_reg_class(si, value)) {
1023 remat_info_t *remat_info,
1028 query.remats = NULL;
1029 query.remats_by_operand = NULL;
1030 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1032 if(remat_info && remat_info->remats) {
1033 pset_foreach(remat_info->remats, remat) {
1034 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at end of block %+F\n", remat->op, bb));
1036 insert_remat_before(si, remat, bb, NULL);
1041 if(is_diverge_edge(bb)) {
1042 /* add remat2s at beginning of block */
1043 if ((live_is_in(li) || (is_Phi(value) && get_nodes_block(value)==bb)) && has_reg_class(si, value)) {
1044 remat_info_t *remat_info,
1049 query.remats = NULL;
1050 query.remats_by_operand = NULL;
1051 remat_info = set_find(si->remat_info, &query, sizeof(query), HASH_PTR(value));
1053 if(remat_info && remat_info->remats) {
1054 pset_foreach(remat_info->remats, remat) {
1055 DBG((si->dbg, LEVEL_4, "\t considering remat %+F at beginning of block %+F\n", remat->op, bb));
1057 /* put the remat here if all its args are available */
1058 insert_remat_after(si, remat, bb, NULL);
1068 * Preparation of blocks' ends for Luke Blockwalker(tm)(R)
1071 luke_endwalker(ir_node * bb, void * data)
1073 spill_ilp_t *si = (spill_ilp_t*)data;
1080 spill_bb_t *spill_bb = get_irn_link(bb);
1083 live = pset_new_ptr_default();
1084 use_end = pset_new_ptr_default();
1086 live_foreach(bb, li) {
1087 irn = (ir_node *) li->irn;
1088 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1091 pset_insert_ptr(live, irn);
1092 op = get_irn_link(irn);
1093 assert(!op->is_remat);
1097 /* collect values used by cond jumps etc. at bb end (use_end) -> always live */
1098 /* their reg_out must always be set */
1099 sched_foreach_reverse(bb, irn) {
1103 if(!sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) break;
1105 for (i = 0, n = get_irn_arity(irn); i < n; ++i) {
1106 ir_node *irn_arg = get_irn_n(irn, i);
1107 if(has_reg_class(si, irn_arg)) {
1108 pset_insert_ptr(use_end, irn_arg);
1113 ir_snprintf(buf, sizeof(buf), "check_end_%N", bb);
1114 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - pset_count(use_end));
1116 spill_bb->ilp = new_set(cmp_spill, 16);
1118 pset_foreach(live,irn) {
1124 /* handle values used by control flow nodes later separately */
1125 if(pset_find_ptr(use_end, irn)) continue;
1128 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1130 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1132 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1133 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1134 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1136 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1137 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1139 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1140 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1142 spill->reg_in = ILP_UNDEF;
1143 spill->mem_in = ILP_UNDEF;
1146 pset_foreach(use_end,irn) {
1150 ilp_cst_t end_use_req;
1153 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1155 spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1157 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", irn, bb);
1158 spill->reg_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1159 /* if irn is used at the end of the block, then it is live anyway */
1160 //lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1162 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1163 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1165 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1166 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1168 spill->reg_in = ILP_UNDEF;
1169 spill->mem_in = ILP_UNDEF;
1171 ir_snprintf(buf, sizeof(buf), "req_cf_end_%N_%N", irn, bb);
1172 end_use_req = lpp_add_cst(si->lpp, buf, lpp_equal, 1);
1173 lpp_set_factor_fast(si->lpp, end_use_req, spill->reg_out, 1.0);
1181 next_post_remat(const ir_node * irn)
1186 irn = sched_block_first_nonphi(irn);
1188 irn = sched_next_op(irn);
1191 if(sched_is_end(irn))
1194 op = (op_t*)get_irn_link(irn);
1195 if(op->is_remat && !op->attr.remat.pre) {
1204 next_pre_remat(const spill_ilp_t * si, const ir_node * irn)
1210 ret = sched_block_last_noncf(si, irn);
1211 ret = sched_next(ret);
1212 ret = sched_prev_op(ret);
1214 ret = sched_prev_op(irn);
1217 if(sched_is_end(ret) || is_Phi(ret))
1220 op = (op_t*)get_irn_link(ret);
1221 if(op->is_remat && op->attr.remat.pre) {
1229 * Find a remat of value @p value in the epilog of @p pos
1232 find_post_remat(const ir_node * value, const ir_node * pos)
1234 while((pos = next_post_remat(pos)) != NULL) {
1237 op = get_irn_link(pos);
1238 assert(op->is_remat && !op->attr.remat.pre);
1240 if(op->attr.remat.remat->value == value)
1241 return (ir_node*)pos;
1244 const ir_edge_t *edge;
1245 foreach_out_edge(pos, edge) {
1246 ir_node *proj = get_edge_src_irn(edge);
1247 assert(is_Proj(proj));
1257 * Find a remat of value @p value in the prolog of @p pos
1260 find_pre_remat(const spill_ilp_t * si, const ir_node * value, const ir_node * pos)
1262 while((pos = next_pre_remat(si,pos)) != NULL) {
1265 op = get_irn_link(pos);
1266 assert(op->is_remat && op->attr.remat.pre);
1268 if(op->attr.remat.remat->value == value)
1269 return (ir_node*)pos;
1276 add_to_spill_bb(spill_ilp_t * si, ir_node * bb, ir_node * irn)
1278 spill_bb_t *spill_bb = get_irn_link(bb);
1284 spill = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1286 double spill_cost = is_Unknown(irn)?0.0001:COST_STORE*execution_frequency(si, bb);
1288 spill = set_insert(spill_bb->ilp, &query, sizeof(query), HASH_PTR(irn));
1290 spill->reg_out = ILP_UNDEF;
1291 spill->reg_in = ILP_UNDEF;
1292 spill->mem_in = ILP_UNDEF;
1294 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", irn, bb);
1295 spill->mem_out = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1297 ir_snprintf(buf, sizeof(buf), "spill_%N_%N", irn, bb);
1298 spill->spill = lpp_add_var(si->lpp, buf, lpp_binary, spill_cost);
1305 get_live_end(spill_ilp_t * si, ir_node * bb, pset * live)
1310 live_foreach(bb, li) {
1311 irn = (ir_node *) li->irn;
1313 if (live_is_end(li) && has_reg_class(si, irn) && !pset_find_ptr(si->all_possible_remats, irn)) {
1314 pset_insert_ptr(live, irn);
1318 irn = sched_last(bb);
1320 /* all values eaten by control flow operations are also live until the end of the block */
1321 while(sched_skip_cf_predicator(irn, si->chordal_env->birg->main_env->arch_env)) {
1323 for(i=get_irn_arity(irn)-1; i>=0; --i) {
1324 ir_node *arg = get_irn_n(irn,i);
1326 if(has_reg_class(si, arg)) {
1327 pset_insert_ptr(live, arg);
1331 irn = sched_prev(irn);
1336 * Walk all irg blocks and emit this ILP
1339 luke_blockwalker(ir_node * bb, void * data)
1341 spill_ilp_t *si = (spill_ilp_t*)data;
1346 spill_bb_t *spill_bb = get_irn_link(bb);
1353 live = pset_new_ptr_default();
1355 /* do something at the end of the block */
1357 /* init live values at end of block */
1358 get_live_end(si, bb, live);
1360 if(is_merge_edge(bb)) {
1361 spill_bb->reloads = new_set(cmp_keyval, pset_count(live));
1363 spill_bb->reloads = NULL;
1366 pset_foreach(live, irn) {
1370 spill = set_find_spill(spill_bb->ilp, irn);
1373 if(spill_bb->reloads) {
1374 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", bb, irn);
1375 reload = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1377 set_insert_keyval(spill_bb->reloads, irn, INT_TO_PTR(reload));
1379 /* reload <= mem_out */
1380 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1381 lpp_set_factor_fast(si->lpp, cst, reload, 1.0);
1382 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1385 op = get_irn_link(irn);
1386 assert(!op->is_remat);
1388 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", irn, bb);
1389 op->attr.live_range.ilp = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1390 op->attr.live_range.op = bb;
1392 ir_snprintf(buf, sizeof(buf), "reg_out_%N_%N", bb, irn);
1393 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1395 /* reg_out - reload - remat - live_range <= 0 */
1396 lpp_set_factor_fast(si->lpp, cst, spill->reg_out, 1.0);
1397 if(spill_bb->reloads) lpp_set_factor_fast(si->lpp, cst, reload, -1.0);
1398 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, -1.0);
1399 foreach_pre_remat(si, bb, tmp) {
1400 op_t *remat_op = get_irn_link(tmp);
1401 if(remat_op->attr.remat.remat->value == irn) {
1402 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1406 DBG((si->dbg, LEVEL_4, "\t %d values live at end of block %+F\n", pset_count(live), bb));
1408 sched_foreach_reverse(bb, irn) {
1414 ilp_cst_t check_pre,
1416 #ifdef CHECK_POST_REMAT
1417 ilp_cst_t check_post_remat;
1419 set *args = new_set(cmp_keyval, get_irn_arity(irn));
1425 op = get_irn_link(irn);
1427 if(op->is_remat) continue;
1428 DBG((si->dbg, LEVEL_4, "\t at node %+F\n", irn));
1430 if(has_reg_class(si, irn)) {
1431 assert(pset_find_ptr(live, irn));
1432 pset_remove_ptr(live, irn);
1435 if(is_Proj(irn)) continue;
1437 /* init set of irn's arguments */
1438 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1439 ir_node *irn_arg = get_irn_n(irn, n);
1440 if(has_reg_class(si, irn_arg)) {
1441 set_insert_keyval(args, irn_arg, (void*)n);
1445 #ifdef CHECK_POST_REMAT
1446 /* check the register pressure after the epilog */
1447 ir_snprintf(buf, sizeof(buf), "check_post_remat_%N", irn);
1448 check_post_remat = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1450 /* iterate over L\U */
1451 pset_foreach(live, tmp) {
1452 if(!set_find_keyval(args, tmp)) {
1453 /* if a live value is not used by irn */
1454 tmp_op = get_irn_link(tmp);
1455 // assert(tmp_op->attr.live_range.op != irn);
1456 lpp_set_factor_fast(si->lpp, check_post_remat, tmp_op->attr.live_range.ilp, 1.0);
1459 /* iterate over following remats and remove possibly defined values again from check_post_remat */
1460 foreach_post_remat(irn, tmp) {
1461 op_t *remat_op = get_irn_link(tmp);
1462 const ir_node *value = remat_op->attr.remat.remat->value;
1463 op_t *val_op = get_irn_link(value);
1465 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1467 /* values that are defined by remat2s are not counted */
1468 #ifdef REMAT_WHILE_LIVE
1469 assert(val_op->attr.live_range.ilp);
1470 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1472 if(val_op->attr.live_range.ilp != ILP_UNDEF) {
1473 lpp_set_factor_fast(si->lpp, check_post_remat, val_op->attr.live_range.ilp, 0.0);
1475 #endif /* REMAT_WHILE_LIVE */
1477 #endif /* CHECK_POST_REMAT */
1480 /* new live ranges for values from L\U defined by remat2s or used by remats */
1481 pset_foreach(live, tmp) {
1482 ir_node *value = tmp;//remat_op->attr.remat.remat->value;
1483 op_t *value_op = get_irn_link(value);
1485 if(!set_find_keyval(args, value)) {
1486 ilp_var_t prev_lr = ILP_UNDEF;
1490 foreach_post_remat(irn, remat) {
1491 op_t *remat_op = get_irn_link(remat);
1493 /* if value is being rematerialized by this remat */
1494 if(value == remat_op->attr.remat.remat->value) {
1495 if(cst == ILP_UNDEF) {
1496 /* next_live_range <= prev_live_range + sum remat2s */
1497 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1498 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1499 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1500 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1501 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1502 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1505 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1509 #ifdef MAY_DIE_AT_PRE_REMAT
1510 if(cst == ILP_UNDEF) {
1511 foreach_pre_remat(si, irn, remat) {
1514 for (n=get_irn_arity(remat)-1; n>=0; --n) {
1515 ir_node *remat_arg = get_irn_n(remat, n);
1517 /* if value is being used by this remat */
1518 if(value == remat_arg) {
1519 /* next_live_range <= prev_live_range */
1520 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", value, irn);
1521 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1523 ir_snprintf(buf, sizeof(buf), "next_lr_%N_%N", value, irn);
1524 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1525 lpp_set_factor_fast(si->lpp, cst, value_op->attr.live_range.ilp, 1.0);
1526 lpp_set_factor_fast(si->lpp, cst, prev_lr, -1.0);
1529 /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1536 if(prev_lr != ILP_UNDEF) {
1537 value_op->attr.live_range.ilp = prev_lr;
1538 value_op->attr.live_range.op = irn;
1543 /* get count of values in my register class defined by irn */
1544 /* also add defined values to check_post_remat; do this before iterating over args */
1545 if(get_irn_mode(irn) == mode_T) {
1546 ir_node *proj = sched_next(irn);
1547 op_t *proj_op = get_irn_link(proj);
1549 while(is_Proj(proj)) {
1550 if(has_reg_class(si, proj)) {
1552 #ifdef CHECK_POST_REMAT
1553 lpp_set_factor_fast(si->lpp, check_post_remat, proj_op->attr.live_range.ilp, 1.0);
1556 proj = sched_next(proj);
1557 proj_op = get_irn_link(proj);
1560 if(has_reg_class(si, irn)) {
1562 #ifdef CHECK_POST_REMAT
1563 lpp_set_factor_fast(si->lpp, check_post_remat, op->attr.live_range.ilp, 1.0);
1567 DBG((si->dbg, LEVEL_4, "\t %+F produces %d values in my register class\n", irn, d));
1569 /* count how many regs irn needs for arguments */
1570 k = set_count(args);
1572 /* check the register pressure in the prolog */
1573 /* sum_{L\U} lr <= n - |U| */
1574 ir_snprintf(buf, sizeof(buf), "check_pre_%N", irn);
1575 check_pre = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - k);
1577 /* check the register pressure in the epilog */
1578 ir_snprintf(buf, sizeof(buf), "check_post_%N", irn);
1579 check_post = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs - d);
1581 set_foreach(args, keyval) {
1587 ir_node *arg = keyval->key;
1589 spill = add_to_spill_bb(si, bb, arg);
1591 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", arg, irn);
1592 next_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1594 i = (int)keyval->val;
1596 ir_snprintf(buf, sizeof(buf), "reload_%N_%N", arg, irn);
1597 op->attr.live_range.args.reloads[i] = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD*execution_frequency(si, bb));
1599 /* reload <= mem_out */
1600 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1601 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1602 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, -1.0);
1604 arg_op = get_irn_link(arg);
1606 /* requirement: arg must be in register for use */
1607 /* reload + remat + live_range == 1 */
1608 ir_snprintf(buf, sizeof(buf), "req_%N_%N", irn, arg);
1609 cst = lpp_add_cst(si->lpp, buf, lpp_equal, 1.0);
1611 lpp_set_factor_fast(si->lpp, cst, next_lr, 1.0);
1612 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[i], 1.0);
1613 foreach_pre_remat(si, irn, tmp) {
1614 op_t *remat_op = get_irn_link(tmp);
1615 if(remat_op->attr.remat.remat->value == arg) {
1616 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1620 /* the epilog stuff - including post_use, post, post_remat */
1621 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N", arg, irn);
1622 post_use = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1624 lpp_set_factor_fast(si->lpp, check_post, post_use, 1.0);
1626 /* arg is live throughout epilog if the next live_range is in a register */
1627 if(pset_find_ptr(live, arg)) {
1628 DBG((si->dbg, LEVEL_3, "\t arg %+F is possibly live in epilog of %+F\n", arg, irn));
1630 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1631 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1632 lpp_set_factor_fast(si->lpp, cst, post_use, -1.0);
1633 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, 1.0);
1635 #ifdef CHECK_POST_REMAT
1636 lpp_set_factor_fast(si->lpp, check_post_remat, arg_op->attr.live_range.ilp, 1.0);
1640 /*forall remat2 which use arg add a similar cst*/
1641 foreach_post_remat(irn, tmp) {
1644 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1645 ir_node *remat_arg = get_irn_n(tmp, n);
1646 op_t *remat_op = get_irn_link(tmp);
1648 if(remat_arg == arg) {
1649 DBG((si->dbg, LEVEL_3, "\t found remat with arg %+F in epilog of %+F\n", arg, irn));
1651 ir_snprintf(buf, sizeof(buf), "post_use_%N_%N-%d", arg, irn, p++);
1652 cst = lpp_add_cst(si->lpp, buf, lpp_greater, 0.0);
1653 lpp_set_factor_fast(si->lpp, cst, post_use, 1.0);
1654 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
1659 /* new live range begins for each argument */
1660 arg_op->attr.live_range.ilp = next_lr;
1661 arg_op->attr.live_range.op = irn;
1663 pset_insert_ptr(live, arg);
1666 /* start new live ranges for values used by remats */
1667 foreach_pre_remat(si, irn, tmp) {
1670 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1671 ir_node *remat_arg = get_irn_n(tmp, n);
1672 op_t *arg_op = get_irn_link(remat_arg);
1675 if(!has_reg_class(si, remat_arg)) continue;
1677 /* if value is becoming live through use by remat */
1678 if(!pset_find_ptr(live, remat_arg)) {
1679 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1680 prev_lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1682 arg_op->attr.live_range.ilp = prev_lr;
1683 arg_op->attr.live_range.op = irn;
1685 DBG((si->dbg, LEVEL_4, " value %+F becoming live through use by remat %+F\n", remat_arg, tmp));
1687 /* TODO ist das hier die richtige Stelle???? */
1688 pset_insert_ptr(live, remat_arg);
1689 add_to_spill_bb(si, bb, remat_arg);
1691 /* TODO check afterwards whether lr dies after a pre-remat (should not happen) */
1695 /* iterate over L\U */
1696 pset_foreach(live, tmp) {
1697 if(!set_find_keyval(args, tmp)) {
1698 /* if a live value is not used by irn */
1699 tmp_op = get_irn_link(tmp);
1700 // assert(tmp_op->attr.live_range.op != irn);
1701 lpp_set_factor_fast(si->lpp, check_pre, tmp_op->attr.live_range.ilp, 1.0);
1702 lpp_set_factor_fast(si->lpp, check_post, tmp_op->attr.live_range.ilp, 1.0);
1706 /* requirements for remats */
1707 foreach_pre_remat(si, irn, tmp) {
1708 op_t *remat_op = get_irn_link(tmp);
1711 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1712 ir_node *remat_arg = get_irn_n(tmp, n);
1713 op_t *arg_op = get_irn_link(remat_arg);
1715 if(!has_reg_class(si, remat_arg)) continue;
1717 /* remat <= live_rang(remat_arg) [ + reload(remat_arg) ] */
1718 ir_snprintf(buf, sizeof(buf), "req_remat_%N_arg_%N", tmp, remat_arg);
1719 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1721 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1722 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1724 /* if remat arg is also used by current op then we can use reload placed for this argument */
1725 if((keyval = set_find_keyval(args, remat_arg)) != NULL) {
1726 int index = (int)keyval->val;
1728 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.args.reloads[index], -1.0);
1733 /* requirements for remats2
1735 * TODO unsure if this does the right thing.
1736 * should insert values into set if they do not become live through remat and
1739 foreach_post_remat(irn, tmp) {
1740 op_t *remat_op = get_irn_link(tmp);
1743 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1744 ir_node *remat_arg = get_irn_n(tmp, n);
1745 op_t *arg_op = get_irn_link(remat_arg);
1747 if(!has_reg_class(si, remat_arg)) continue;
1749 /* only for values in L\U, the others are handled with post_use */
1750 if(!set_find_keyval(args, remat_arg)) {
1751 /* remat <= live_rang(remat_arg) */
1752 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_arg_%N", tmp, remat_arg);
1753 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1755 /* if value is becoming live through use by remat2 */
1756 if(!pset_find_ptr(live, remat_arg)) {
1759 ir_snprintf(buf, sizeof(buf), "lr_%N_%N", remat_arg, irn);
1760 lr = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1762 arg_op->attr.live_range.ilp = lr;
1763 arg_op->attr.live_range.op = irn;
1765 DBG((si->dbg, LEVEL_3, " value %+F becoming live through use by remat2 %+F\n", remat_arg, tmp));
1767 pset_insert_ptr(live, remat_arg);
1768 add_to_spill_bb(si, bb, remat_arg);
1771 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1772 lpp_set_factor_fast(si->lpp, cst, arg_op->attr.live_range.ilp, -1.0);
1777 #ifdef CHECK_POST_REMAT
1778 /* iterate over following remats and add them to check_post_remat */
1779 foreach_post_remat(irn, tmp) {
1780 op_t *remat_op = get_irn_link(tmp);
1782 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1784 lpp_set_factor_fast(si->lpp, check_post_remat, remat_op->attr.remat.ilp, 1.0);
1790 DBG((si->dbg, LEVEL_4, "\t %d values live at %+F\n", pset_count(live), irn));
1792 pset_foreach(live, tmp) {
1793 assert(has_reg_class(si, tmp));
1796 for (n=get_irn_arity(irn)-1; n>=0; --n) {
1797 ir_node *arg = get_irn_n(irn, n);
1799 assert(!find_post_remat(arg, irn) && "there should be no post remat for an argument of an op");
1807 /* do something at the beginning of the block */
1809 /* we are now at the beginning of the basic block, there are only \Phis in front of us */
1810 DBG((si->dbg, LEVEL_3, "\t %d values live at beginning of block %+F\n", pset_count(live), bb));
1812 pset_foreach(live, irn) {
1813 assert(is_Phi(irn) || get_nodes_block(irn) != bb);
1816 /* construct mem_outs for all values */
1818 set_foreach(spill_bb->ilp, spill) {
1819 ir_snprintf(buf, sizeof(buf), "mem_out_%N_%N", spill->irn, bb);
1820 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1822 lpp_set_factor_fast(si->lpp, cst, spill->mem_out, 1.0);
1823 lpp_set_factor_fast(si->lpp, cst, spill->spill, -1.0);
1825 if(pset_find_ptr(live, spill->irn)) {
1826 DBG((si->dbg, LEVEL_5, "\t %+F live at beginning of block %+F\n", spill->irn, bb));
1828 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N", spill->irn, bb);
1829 spill->mem_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1830 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1832 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
1834 op_t *op = get_irn_link(spill->irn);
1836 /* do we have to copy a phi argument? */
1837 op->attr.live_range.args.copies = obstack_alloc(si->obst, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1838 memset(op->attr.live_range.args.copies, 0xFF, sizeof(*op->attr.live_range.args.copies) * get_irn_arity(spill->irn));
1840 for(n=get_irn_arity(spill->irn)-1; n>=0; --n) {
1841 const ir_node *arg = get_irn_n(spill->irn, n);
1847 /* argument already done? */
1848 if(op->attr.live_range.args.copies[n] != ILP_UNDEF) continue;
1850 /* get sum of execution frequencies of blocks with the same phi argument */
1851 for(m=n; m>=0; --m) {
1852 const ir_node *arg2 = get_irn_n(spill->irn, m);
1855 freq += execution_frequency(si, get_Block_cfgpred_block(bb, m));
1859 /* copies are not for free */
1860 ir_snprintf(buf, sizeof(buf), "copy_%N_%N", arg, spill->irn);
1861 var = lpp_add_var(si->lpp, buf, lpp_binary, COST_STORE * freq);
1863 for(m=n; m>=0; --m) {
1864 const ir_node *arg2 = get_irn_n(spill->irn, m);
1867 op->attr.live_range.args.copies[m] = var;
1871 /* copy <= mem_in */
1872 ir_snprintf(buf, sizeof(buf), "nocopy_%N_%N", arg, spill->irn);
1873 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1874 lpp_set_factor_fast(si->lpp, cst, var, 1.0);
1875 lpp_set_factor_fast(si->lpp, cst, spill->mem_in, -1.0);
1882 /* L\U is empty at bb start */
1883 /* arg is live throughout epilog if it is reg_in into this block */
1885 /* check the register pressure at the beginning of the block
1888 ir_snprintf(buf, sizeof(buf), "check_start_%N", bb);
1889 cst = lpp_add_cst(si->lpp, buf, lpp_less, si->n_regs);
1891 pset_foreach(live, irn) {
1894 spill = set_find_spill(spill_bb->ilp, irn);
1897 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N", irn, bb);
1898 spill->reg_in = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
1900 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, 1.0);
1902 /* spill + reg_in <= 1 */
1903 ir_snprintf(buf, sizeof(buf), "nospill_%N_%N", irn, bb);
1904 nospill = lpp_add_cst(si->lpp, buf, lpp_less, 1);
1906 lpp_set_factor_fast(si->lpp, nospill, spill->reg_in, 1.0);
1907 lpp_set_factor_fast(si->lpp, nospill, spill->spill, 1.0);
1910 foreach_post_remat(bb, irn) {
1911 op_t *remat_op = get_irn_link(irn);
1913 DBG((si->dbg, LEVEL_4, "\t next post remat: %+F\n", irn));
1914 assert(remat_op->is_remat && !remat_op->attr.remat.pre);
1916 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1919 /* forall remat2 add requirements */
1920 foreach_post_remat(bb, tmp) {
1923 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
1924 ir_node *remat_arg = get_irn_n(tmp, n);
1925 op_t *remat_op = get_irn_link(tmp);
1927 if(!has_reg_class(si, remat_arg)) continue;
1929 spill = set_find_spill(spill_bb->ilp, remat_arg);
1932 /* TODO verify this is placed correctly */
1933 ir_snprintf(buf, sizeof(buf), "req_remat2_%N_%N_arg_%N", tmp, bb, remat_arg);
1934 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1935 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
1936 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, 1.0);
1940 /* mem_in/reg_in for live_in values, especially phis and their arguments */
1941 pset_foreach(live, irn) {
1945 spill = set_find_spill(spill_bb->ilp, irn);
1946 assert(spill && spill->irn == irn);
1948 if(is_Phi(irn) && get_nodes_block(irn) == bb) {
1949 for (n=get_Phi_n_preds(irn)-1; n>=0; --n) {
1952 ir_node *phi_arg = get_Phi_pred(irn, n);
1953 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
1954 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
1957 /* although the phi is in the right regclass one or more of
1958 * its arguments can be in a different one or at least to
1961 if(has_reg_class(si, phi_arg)) {
1962 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1963 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1964 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1965 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1967 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1968 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1970 spill_p = set_find_spill(spill_bb_p->ilp, phi_arg);
1973 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1974 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
1978 /* else assure the value arrives on all paths in the same resource */
1980 for (n=get_Block_n_cfgpreds(bb)-1; n>=0; --n) {
1983 ir_node *bb_p = get_Block_cfgpred_block(bb, n);
1984 spill_bb_t *spill_bb_p = get_irn_link(bb_p);
1987 ir_snprintf(buf, sizeof(buf), "mem_in_%N_%N-%d", irn, bb, p);
1988 mem_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1989 ir_snprintf(buf, sizeof(buf), "reg_in_%N_%N-%d", irn, bb, p++);
1990 reg_in = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
1992 lpp_set_factor_fast(si->lpp, mem_in, spill->mem_in, 1.0);
1993 lpp_set_factor_fast(si->lpp, reg_in, spill->reg_in, 1.0);
1995 spill_p = set_find_spill(spill_bb_p->ilp, irn);
1998 lpp_set_factor_fast(si->lpp, mem_in, spill_p->mem_out, -1.0);
1999 lpp_set_factor_fast(si->lpp, reg_in, spill_p->reg_out, -1.0);
2004 /* first live ranges from reg_ins */
2005 pset_foreach(live, irn) {
2006 op_t *op = get_irn_link(irn);
2008 spill = set_find_spill(spill_bb->ilp, irn);
2009 assert(spill && spill->irn == irn);
2011 ir_snprintf(buf, sizeof(buf), "first_lr_%N_%N", irn, bb);
2012 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2013 lpp_set_factor_fast(si->lpp, cst, op->attr.live_range.ilp, 1.0);
2014 lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2016 foreach_post_remat(bb, tmp) {
2017 op_t *remat_op = get_irn_link(tmp);
2019 if(remat_op->attr.remat.remat->value == irn) {
2020 lpp_set_factor_fast(si->lpp, cst, remat_op->attr.remat.ilp, -1.0);
2025 /* walk forward now and compute constraints for placing spills */
2026 /* this must only be done for values that are not defined in this block */
2027 /* TODO are these values at start of block? if yes, just check whether this is a diverge edge and skip the loop */
2028 pset_foreach(live, irn) {
2029 spill = set_find_spill(spill_bb->ilp, irn);
2032 ir_snprintf(buf, sizeof(buf), "req_spill_%N_%N", irn, bb);
2033 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
2035 lpp_set_factor_fast(si->lpp, cst, spill->spill, 1.0);
2036 if(is_diverge_edge(bb)) lpp_set_factor_fast(si->lpp, cst, spill->reg_in, -1.0);
2038 sched_foreach_op(bb, tmp) {
2039 op_t *op = get_irn_link(tmp);
2041 if(is_Phi(tmp)) continue;
2042 assert(!is_Proj(tmp));
2045 ir_node *value = op->attr.remat.remat->value;
2048 /* only collect remats up to the first use of a value */
2049 lpp_set_factor_fast(si->lpp, cst, op->attr.remat.ilp, -1.0);
2054 for (n=get_irn_arity(tmp)-1; n>=0; --n) {
2055 ir_node *arg = get_irn_n(tmp, n);
2058 /* if a value is used stop collecting remats */
2064 if(cst == ILP_UNDEF) break;
2069 /* 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)
2070 mem_in(phi) -> not mem_in(orig_value) TODO: how does this depend on a certain predecessor?
2073 /* mem_in of mem-phi has associated costs (but first one is free) */
2074 /* define n_mem_copies as positive integer in each predecessor block,
2075 #mem_in into this block from predecessor block - 1 weighted with SPILL_COST*execfreq(predecessor)
2083 typedef struct _irnlist_t {
2084 struct list_head list;
2088 typedef struct _interference_t {
2089 struct list_head blocklist;
2095 cmp_interference(const void *a, const void *b, size_t size)
2097 const interference_t *p = a;
2098 const interference_t *q = b;
2100 return !(p->a == q->a && p->b == q->b);
2103 static interference_t *
2104 set_find_interference(set * set, ir_node * a, ir_node * b)
2106 interference_t query;
2108 query.a = (a>b)?a:b;
2109 query.b = (a>b)?b:a;
2111 return set_find(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2114 static interference_t *
2115 set_insert_interference(spill_ilp_t * si, set * set, ir_node * a, ir_node * b, ir_node * bb)
2117 interference_t query,
2119 irnlist_t *list = obstack_alloc(si->obst, sizeof(*list));
2123 result = set_find_interference(set, a, b);
2126 list_add(&list->list, &result->blocklist);
2130 query.a = (a>b)?a:b;
2131 query.b = (a>b)?b:a;
2133 result = set_insert(set, &query, sizeof(query), HASH_PTR(PTR_TO_INT(a) ^ PTR_TO_INT(b)));
2135 INIT_LIST_HEAD(&result->blocklist);
2136 list_add(&list->list, &result->blocklist);
2142 values_interfere_in_block(ir_node * bb, ir_node * a, ir_node * b)
2144 const ir_edge_t *edge;
2146 if(get_nodes_block(a) != bb && get_nodes_block(b) != bb) {
2147 /* both values are live in, so they interfere */
2151 /* ensure a dominates b */
2152 if(value_dominates(b,a)) {
2158 assert(get_nodes_block(b) == bb && "at least b should be defined here in this block");
2161 /* the following code is stolen from bera.c */
2162 if(is_live_end(bb, a))
2165 foreach_out_edge(a, edge) {
2166 const ir_node *user = edge->src;
2167 if(get_nodes_block(user) == bb
2170 && value_dominates(b, user))
2178 * Walk all irg blocks and collect interfering values inside of phi classes
2181 luke_interferencewalker(ir_node * bb, void * data)
2183 spill_ilp_t *si = (spill_ilp_t*)data;
2187 live_foreach(bb, li1) {
2188 ir_node *a = (ir_node *) li1->irn;
2189 op_t *a_op = get_irn_link(a);
2191 if(a_op->is_remat) continue;
2193 /* a is only interesting if it is in my register class and if it is inside a phi class */
2194 if (has_reg_class(si, a) && get_phi_class(a)) {
2195 for(li2=li1->next; li2; li2 = li2->next) {
2196 ir_node *b = (ir_node *) li2->irn;
2197 op_t *b_op = get_irn_link(b);
2199 if(b_op->is_remat) continue;
2201 /* a and b are only interesting if they are in the same phi class */
2202 if(has_reg_class(si, b) && get_phi_class(a) == get_phi_class(b)) {
2203 if(values_interfere_in_block(bb, a, b)) {
2204 //DBG((si->dbg, LEVEL_1, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b));
2205 ir_fprintf(stderr, "\tvalues interfere in %+F: %+F, %+F\n", bb, a, b);
2206 set_insert_interference(si, si->interferences, a, b, bb);
2214 static unsigned int copy_path_id = 0;
2217 write_copy_path_cst(spill_ilp_t *si, pset * copies, ilp_var_t any_interfere)
2224 ir_snprintf(buf, sizeof(buf), "copy_path-%d", copy_path_id++);
2225 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2227 lpp_set_factor_fast(si->lpp, cst, any_interfere, 1.0);
2229 pset_foreach(copies, ptr) {
2230 copy = PTR_TO_INT(ptr);
2231 lpp_set_factor_fast(si->lpp, cst, copy, -1.0);
2236 * @parameter copies contains a path of copies which lead us to irn
2237 * @parameter visited contains a set of nodes already visited on this path
2240 find_copy_path(spill_ilp_t * si, ir_node * irn, ir_node * target, ilp_var_t any_interfere, pset * copies, pset * visited)
2243 op_t *op = get_irn_link(irn);
2245 if(op->is_remat) return;
2247 pset_insert_ptr(visited, irn);
2252 /* visit all operands */
2253 for(n=get_irn_arity(irn)-1; n>=0; --n) {
2254 ir_node *arg = get_irn_n(irn, n);
2255 ilp_var_t copy = op->attr.live_range.args.copies[n];
2257 if(!has_reg_class(si, arg)) continue;
2260 pset_insert(copies, INT_TO_PTR(copy), copy);
2261 write_copy_path_cst(si, copies, any_interfere);
2262 pset_remove(copies, INT_TO_PTR(copy), copy);
2264 if(!pset_find_ptr(visited, arg)) {
2265 pset_insert(copies, INT_TO_PTR(copy), copy);
2266 find_copy_path(si, arg, target, any_interfere, copies, visited);
2267 pset_remove(copies, INT_TO_PTR(copy), copy);
2273 /* visit all uses which are phis */
2274 foreach_out_edge(irn, edge) {
2275 ir_node *user = edge->src;
2276 int pos = edge->pos;
2277 op_t *op = get_irn_link(user);
2280 if(!is_Phi(user)) continue;
2281 if(!has_reg_class(si, user)) continue;
2283 copy = op->attr.live_range.args.copies[pos];
2285 if(user == target) {
2286 pset_insert(copies, INT_TO_PTR(copy), copy);
2287 write_copy_path_cst(si, copies, any_interfere);
2288 pset_remove(copies, INT_TO_PTR(copy), copy);
2290 if(!pset_find_ptr(visited, user)) {
2291 pset_insert(copies, INT_TO_PTR(copy), copy);
2292 find_copy_path(si, user, target, any_interfere, copies, visited);
2293 pset_remove(copies, INT_TO_PTR(copy), copy);
2298 pset_remove_ptr(visited, irn);
2302 gen_copy_constraints(spill_ilp_t * si, ir_node * a, ir_node * b, ilp_var_t any_interfere)
2304 pset * copies = pset_new_ptr_default();
2305 pset * visited = pset_new_ptr_default();
2307 find_copy_path(si, a, b, any_interfere, copies, visited);
2315 memcopyhandler(spill_ilp_t * si)
2317 interference_t *interference;
2319 /* teste Speicherwerte auf Interferenz */
2321 /* analyze phi classes */
2322 phi_class_compute(si->chordal_env->irg);
2324 DBG((si->dbg, LEVEL_2, "\t calling interferencewalker\n"));
2325 irg_block_walk_graph(si->chordal_env->irg, luke_interferencewalker, NULL, si);
2327 // phi_class_free(si->chordal_env->irg);
2329 /* now lets emit the ILP unequations for the crap */
2330 set_foreach(si->interferences, interference) {
2332 ilp_var_t interfere,
2334 ilp_cst_t any_interfere_cst,
2336 const ir_node *a = interference->a;
2337 const ir_node *b = interference->b;
2338 struct list_head *pos;
2340 /* any_interf <= \sum interf */
2341 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N", a, b);
2342 any_interfere_cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2343 any_interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2345 lpp_set_factor_fast(si->lpp, any_interfere_cst, any_interfere, 1.0);
2347 list_for_each_entry(irnlist_t, irnlist, &interference->blocklist, list) {
2348 const ir_node *bb = irnlist->irn;
2349 spill_bb_t *spill_bb = get_irn_link(bb);
2356 spilla = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(a));
2360 spillb = set_find(spill_bb->ilp, &query, sizeof(query), HASH_PTR(b));
2363 /* interfere <-> (mem_in_a or spill_a) and (mem_in_b or spill_b): */
2364 /* 1: mem_in_a + mem_in_b + spill_a + spill_b - interfere <= 1 */
2365 /* 2: - mem_in_a - spill_a + interfere <= 0 */
2366 /* 3: - mem_in_b - spill_b + interfere <= 0 */
2367 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N", bb, a, b);
2368 interfere = lpp_add_var(si->lpp, buf, lpp_binary, 0.0);
2370 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-1", bb, a, b);
2371 cst = lpp_add_cst(si->lpp, buf, lpp_less, 1);
2373 lpp_set_factor_fast(si->lpp, cst, interfere, -1.0);
2374 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, 1.0);
2375 lpp_set_factor_fast(si->lpp, cst, spilla->spill, 1.0);
2376 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, 1.0);
2377 lpp_set_factor_fast(si->lpp, cst, spillb->spill, 1.0);
2379 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-2", bb, a, b);
2380 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2382 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2383 if(spilla->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spilla->mem_in, -1.0);
2384 lpp_set_factor_fast(si->lpp, cst, spilla->spill, -1.0);
2386 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N_%N-3", bb, a, b);
2387 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2389 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2390 if(spillb->mem_in != ILP_UNDEF) lpp_set_factor_fast(si->lpp, cst, spillb->mem_in, -1.0);
2391 lpp_set_factor_fast(si->lpp, cst, spillb->spill, -1.0);
2394 lpp_set_factor_fast(si->lpp, any_interfere_cst, interfere, -1.0);
2396 /* any_interfere >= interf */
2397 ir_snprintf(buf, sizeof(buf), "interfere_%N_%N-%N", a, b, bb);
2398 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
2400 lpp_set_factor_fast(si->lpp, cst, interfere, 1.0);
2401 lpp_set_factor_fast(si->lpp, cst, any_interfere, -1.0);
2404 /* now that we know whether the two values interfere in memory we can drop constraints to enforce copies */
2405 gen_copy_constraints(si,a,b,any_interfere);
2412 memcopyinsertor(spill_ilp_t * si)
2414 /* weise Spillkontexte zu. Sorge bei Phis dafuer, dass gleiche
2415 * Kontexte zusammenfliessen (Operanden und Ergebnis hat gleichen
2431 return fabs(x) < 0.00001;
2435 static int mark_remat_nodes_hook(FILE *F, ir_node *n, ir_node *l)
2437 spill_ilp_t *si = get_irg_link(current_ir_graph);
2439 if(pset_find_ptr(si->all_possible_remats, n)) {
2440 op_t *op = (op_t*)get_irn_link(n);
2441 assert(op && op->is_remat);
2443 if(!op->attr.remat.remat->inverse) {
2444 if(op->attr.remat.pre) {
2445 ir_fprintf(F, "color:red info3:\"remat value: %+F\"", op->attr.remat.remat->value);
2447 ir_fprintf(F, "color:orange info3:\"remat2 value: %+F\"", op->attr.remat.remat->value);
2452 op_t *op = (op_t*)get_irn_link(n);
2453 assert(op && op->is_remat);
2455 if(op->attr.remat.pre) {
2456 ir_fprintf(F, "color:cyan info3:\"remat inverse value: %+F\"", op->attr.remat.remat->value);
2458 ir_fprintf(F, "color:lightcyan info3:\"remat2 inverse value: %+F\"", op->attr.remat.remat->value);
2469 dump_graph_with_remats(ir_graph * irg, const char * suffix)
2471 set_dump_node_vcgattr_hook(mark_remat_nodes_hook);
2472 be_dump(irg, suffix, dump_ir_block_graph_sched);
2473 set_dump_node_vcgattr_hook(NULL);
2478 * Edge hook to dump the schedule edges with annotated register pressure.
2481 sched_pressure_edge_hook(FILE *F, ir_node *irn)
2483 if(sched_is_scheduled(irn) && sched_has_prev(irn)) {
2484 ir_node *prev = sched_prev(irn);
2485 fprintf(F, "edge:{sourcename:\"");
2487 fprintf(F, "\" targetname:\"");
2489 fprintf(F, "\" label:\"%d", (int)get_irn_link(irn));
2490 fprintf(F, "\" color:magenta}\n");
2496 dump_ir_block_graph_sched_pressure(ir_graph *irg, const char *suffix)
2498 DUMP_NODE_EDGE_FUNC old = get_dump_node_edge_hook();
2500 dump_consts_local(0);
2501 set_dump_node_edge_hook(sched_pressure_edge_hook);
2502 dump_ir_block_graph(irg, suffix);
2503 set_dump_node_edge_hook(old);
2507 walker_pressure_annotator(ir_node * bb, void * data)
2509 spill_ilp_t *si = data;
2513 pset *live = pset_new_ptr_default();
2516 live_foreach(bb, li) {
2517 irn = (ir_node *) li->irn;
2519 if (live_is_end(li) && has_reg_class(si, irn)) {
2520 pset_insert_ptr(live, irn);
2524 set_irn_link(bb, INT_TO_PTR(pset_count(live)));
2526 sched_foreach_reverse(bb, irn) {
2528 set_irn_link(irn, INT_TO_PTR(pset_count(live)));
2532 if(has_reg_class(si, irn)) {
2533 pset_remove_ptr(live, irn);
2534 if(is_Proj(irn)) ++projs;
2537 if(!is_Proj(irn)) projs = 0;
2539 for (n=get_irn_arity(irn)-1; n>=0; --n) {
2540 ir_node *arg = get_irn_n(irn, n);
2542 if(has_reg_class(si, arg)) pset_insert_ptr(live, arg);
2544 set_irn_link(irn, INT_TO_PTR(pset_count(live)+projs));
2551 dump_pressure_graph(spill_ilp_t * si, const char *suffix)
2553 be_dump(si->chordal_env->irg, suffix, dump_ir_block_graph_sched_pressure);
2558 connect_all_remats_with_keep(spill_ilp_t * si)
2566 n_remats = pset_count(si->all_possible_remats);
2568 ins = obstack_alloc(si->obst, n_remats * sizeof(*ins));
2571 pset_foreach(si->all_possible_remats, irn) {
2576 si->keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_remats, ins);
2578 obstack_free(si->obst, ins);
2584 connect_all_spills_with_keep(spill_ilp_t * si)
2593 n_spills = pset_count(si->spills);
2595 ins = obstack_alloc(si->obst, n_spills * sizeof(*ins));
2598 pset_foreach(si->spills, irn) {
2603 keep = be_new_Keep(si->chordal_env->cls, si->chordal_env->irg, get_irg_end_block(si->chordal_env->irg), n_spills, ins);
2605 obstack_free(si->obst, ins);
2609 /** insert a spill at an arbitrary position */
2610 ir_node *be_spill2(const arch_env_t *arch_env, ir_node *irn, ir_node *insert, ir_node *ctx)
2612 ir_node *bl = is_Block(insert)?insert:get_nodes_block(insert);
2613 ir_graph *irg = get_irn_irg(bl);
2614 ir_node *frame = get_irg_frame(irg);
2618 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
2619 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
2621 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
2624 * search the right insertion point. a spill of a phi cannot be put
2625 * directly after the phi, if there are some phis behind the one which
2626 * is spilled. Also, a spill of a Proj must be after all Projs of the
2629 * Here's one special case:
2630 * If the spill is in the start block, the spill must be after the frame
2631 * pointer is set up. This is done by setting insert to the end of the block
2632 * which is its default initialization (see above).
2635 if(bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
2638 for (next = sched_next(insert); is_Phi(next) || is_Proj(next); next = sched_next(insert))
2641 sched_add_after(insert, spill);
2646 delete_remat(spill_ilp_t * si, ir_node * remat) {
2648 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2650 sched_remove(remat);
2652 /* kill links to operands */
2653 for (n=get_irn_arity(remat)-1; n>=-1; --n) {
2654 set_irn_n(remat, n, bad);
2659 clean_remat_info(spill_ilp_t * si)
2663 remat_info_t *remat_info;
2664 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2666 set_foreach(si->remat_info, remat_info) {
2667 if(!remat_info->remats) continue;
2669 pset_foreach(remat_info->remats, remat)
2671 if(remat->proj && get_irn_n_edges(remat->proj) == 0) {
2672 set_irn_n(remat->proj, -1, bad);
2673 set_irn_n(remat->proj, 0, bad);
2676 if(get_irn_n_edges(remat->op) == 0) {
2677 for (n=get_irn_arity(remat->op)-1; n>=-1; --n) {
2678 set_irn_n(remat->op, n, bad);
2683 if(remat_info->remats) del_pset(remat_info->remats);
2684 if(remat_info->remats_by_operand) del_pset(remat_info->remats_by_operand);
2689 delete_unnecessary_remats(spill_ilp_t * si)
2693 ir_node *bad = get_irg_bad(si->chordal_env->irg);
2696 ir_node *end = get_irg_end(si->chordal_env->irg);
2699 for (n=get_irn_arity(si->keep)-1; n>=0; --n) {
2700 ir_node *keep_arg = get_irn_n(si->keep, n);
2701 op_t *arg_op = get_irn_link(keep_arg);
2704 assert(arg_op->is_remat);
2706 name = si->lpp->vars[arg_op->attr.remat.ilp];
2708 if(is_zero(name->value)) {
2709 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", keep_arg));
2710 /* TODO check whether reload is preferred over remat (could be bug) */
2711 delete_remat(si, keep_arg);
2713 if(!arg_op->attr.remat.remat->inverse) {
2714 if(arg_op->attr.remat.pre) {
2715 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", keep_arg));
2717 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", keep_arg));
2720 if(arg_op->attr.remat.pre) {
2721 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", keep_arg));
2723 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", keep_arg));
2728 set_irn_n(si->keep, n, bad);
2731 for (i = 0, n = get_End_n_keepalives(end); i < n; ++i) {
2732 ir_node *end_arg = get_End_keepalive(end, i);
2734 if(end_arg != si->keep) {
2735 obstack_grow(si->obst, &end_arg, sizeof(end_arg));
2738 keeps = obstack_finish(si->obst);
2739 set_End_keepalives(end, n-1, keeps);
2740 obstack_free(si->obst, keeps);
2743 DBG((si->dbg, LEVEL_2, "\t no remats to delete (none have been inserted)\n"));
2748 pset_foreach(si->all_possible_remats, remat) {
2749 op_t *remat_op = get_irn_link(remat);
2750 lpp_name_t *name = si->lpp->vars[remat_op->attr.remat.ilp];
2752 if(is_zero(name->value)) {
2753 DBG((si->dbg, LEVEL_3, "\t deleting remat %+F\n", remat));
2754 /* TODO check whether reload is preferred over remat (could be bug) */
2755 delete_remat(si, remat);
2757 if(!remat_op->attr.remat.remat->inverse) {
2758 if(remat_op->attr.remat.pre) {
2759 DBG((si->dbg, LEVEL_2, "\t**remat kept: %+F\n", remat));
2761 DBG((si->dbg, LEVEL_2, "\t%%%%remat2 kept: %+F\n", remat));
2764 if(remat_op->attr.remat.pre) {
2765 DBG((si->dbg, LEVEL_2, "\t**INVERSE remat kept: %+F\n", remat));
2767 DBG((si->dbg, LEVEL_2, "\t%%%%INVERSE remat2 kept: %+F\n", remat));
2776 * @param before The node after which the spill will be placed in the schedule
2778 /* TODO set context properly */
2780 insert_spill(spill_ilp_t * si, ir_node * irn, ir_node * value, ir_node * before)
2784 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2786 DBG((si->dbg, LEVEL_3, "\t inserting spill for value %+F after %+F\n", irn, before));
2788 spill = be_spill2(arch_env, irn, before, irn);
2790 defs = set_insert_def(si->values, value);
2793 /* enter into the linked list */
2794 set_irn_link(spill, defs->spills);
2795 defs->spills = spill;
2797 #ifdef KEEPALIVE_SPILLS
2798 pset_insert_ptr(si->spills, spill);
2805 * @param before The Phi node which has to be spilled
2808 insert_mem_phi(spill_ilp_t * si, const ir_node * phi)
2815 NEW_ARR_A(ir_node*, ins, get_irn_arity(phi));
2817 for(n=get_irn_arity(phi)-1; n>=0; --n) {
2818 ins[n] = si->m_unknown;
2821 mem_phi = new_r_Phi(si->chordal_env->irg, get_nodes_block(phi), get_irn_arity(phi), ins, mode_M);
2823 defs = set_insert_def(si->values, phi);
2826 /* enter into the linked list */
2827 set_irn_link(mem_phi, defs->spills);
2828 defs->spills = mem_phi;
2830 sched_add_after(phi, mem_phi);
2832 #ifdef KEEPALIVE_SPILLS
2833 pset_insert_ptr(si->spills, mem_phi);
2840 * Add remat to list of defs, destroys link field!
2843 insert_remat(spill_ilp_t * si, ir_node * remat)
2846 op_t *remat_op = get_irn_link(remat);
2848 assert(remat_op->is_remat);
2850 defs = set_insert_def(si->values, remat_op->attr.remat.remat->value);
2853 /* enter into the linked list */
2854 set_irn_link(remat, defs->remats);
2855 defs->remats = remat;
2860 collect_spills(spill_ilp_t * si, ir_node * value, pset * spills, pset * visited)
2865 defs = set_find_def(si->values, value);
2867 if(defs && defs->spills) {
2868 for(next = defs->spills; next; next = get_irn_link(next)) {
2869 pset_insert_ptr(spills, next);
2871 } else if (is_Phi(value)) {
2873 if(!pset_find_ptr(visited, value)) {
2877 pset_insert_ptr(visited, value);
2878 for(i=0, n=get_irn_arity(value); i<n; ++i) {
2879 ir_node *arg = get_irn_n(value, i);
2881 collect_spills(si, arg, spills, visited);
2885 // assert(0 && "Phi operand not spilled");
2891 get_spills_for_value(spill_ilp_t * si, ir_node * value)
2893 pset *spills = pset_new_ptr_default();
2894 // pset *visited = pset_new_ptr_default();
2896 // collect_spills(si, value, spills, visited);
2897 // del_pset(visited);
2901 defs = set_find_def(si->values, value);
2903 if(defs && defs->spills) {
2904 for(next = defs->spills; next; next = get_irn_link(next)) {
2905 pset_insert_ptr(spills, next);
2913 * Add reload before operation and add to list of defs
2916 insert_reload(spill_ilp_t * si, const ir_node * value, const ir_node * after)
2921 const arch_env_t *arch_env = si->chordal_env->birg->main_env->arch_env;
2923 DBG((si->dbg, LEVEL_3, "\t inserting reload for value %+F before %+F\n", value, after));
2925 defs = set_find_def(si->values, value);
2926 /* get a spill of this value */
2928 if((!defs || !defs->spills) && is_Phi(value)) {
2931 spills = get_spills_for_value(si, value);
2933 spill = pset_first(spills);
2937 defs = set_insert_def(si->values, value);
2939 defs->spills = spill;
2940 set_irn_link(spill, NULL);
2942 spill = defs->spills;
2945 spill = defs->spills;
2946 assert(spill && "no spill placed before reload");
2948 reload = be_reload(arch_env, si->cls, after, get_irn_mode(value), spill);
2950 /* enter into the linked list */
2951 set_irn_link(reload, defs->remats);
2952 defs->remats = reload;
2958 walker_spill_placer(ir_node * bb, void * data) {
2959 spill_ilp_t *si = (spill_ilp_t*)data;
2961 spill_bb_t *spill_bb = get_irn_link(bb);
2962 pset *spills_to_do = pset_new_ptr_default();
2965 set_foreach(spill_bb->ilp, spill) {
2968 if(is_Phi(spill->irn) && get_nodes_block(spill->irn) == bb) {
2969 name = si->lpp->vars[spill->mem_in];
2970 if(!is_zero(name->value)) {
2973 mem_phi = insert_mem_phi(si, spill->irn);
2975 DBG((si->dbg, LEVEL_2, "\t >>spilled Phi %+F -> %+F\n", spill->irn, mem_phi));
2979 name = si->lpp->vars[spill->spill];
2980 if(!is_zero(name->value)) {
2981 if(spill->reg_in > 0) {
2982 name = si->lpp->vars[spill->reg_in];
2983 if(!is_zero(name->value)) {
2984 insert_spill(si, spill->irn, spill->irn, bb);
2988 pset_insert_ptr(spills_to_do, spill->irn);
2991 DBG((si->dbg, LEVEL_3, "\t %d spills to do in block %+F\n", pset_count(spills_to_do), bb));
2994 for(irn = sched_block_first_nonphi(bb); !sched_is_end(irn); irn = sched_next(irn)) {
2995 op_t *op = get_irn_link(irn);
2997 if(be_is_Spill(irn)) continue;
3000 /* TODO fix this if we want to support remats with more than two nodes */
3001 if(get_irn_mode(irn) != mode_T && pset_find_ptr(spills_to_do, op->attr.remat.remat->value)) {
3002 pset_remove_ptr(spills_to_do, op->attr.remat.remat->value);
3004 insert_spill(si, irn, op->attr.remat.remat->value, irn);
3007 if(pset_find_ptr(spills_to_do, irn)) {
3008 pset_remove_ptr(spills_to_do, irn);
3010 insert_spill(si, irn, irn, irn);
3016 assert(pset_count(spills_to_do) == 0);
3018 /* afterwards free data in block */
3019 del_pset(spills_to_do);
3023 phim_fixer(spill_ilp_t *si) {
3026 set_foreach(si->values, defs) {
3027 const ir_node *phi = defs->value;
3028 ir_node *phi_m = NULL;
3029 ir_node *next = defs->spills;
3032 if(!is_Phi(phi)) continue;
3035 if(is_Phi(next) && get_irn_mode(next) == mode_M) {
3039 next = get_irn_link(next);
3042 if(!phi_m) continue;
3044 for(n=get_irn_arity(phi)-1; n>=0; --n) {
3045 const ir_node *value = get_irn_n(phi, n);
3046 defs_t *val_defs = set_find_def(si->values, value);
3048 /* get a spill of this value */
3049 ir_node *spill = val_defs->spills;
3051 assert(spill && "no spill placed before PhiM");
3053 set_irn_n(phi_m, n, spill);
3059 walker_reload_placer(ir_node * bb, void * data) {
3060 spill_ilp_t *si = (spill_ilp_t*)data;
3062 spill_bb_t *spill_bb = get_irn_link(bb);
3066 sched_foreach_reverse(bb, irn) {
3067 op_t *op = get_irn_link(irn);
3069 if(be_is_Reload(irn) || be_is_Spill(irn)) continue;
3070 if(is_Phi(irn)) break;
3073 if(get_irn_mode(irn) != mode_T) {
3074 insert_remat(si, irn);
3079 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3080 ir_node *arg = get_irn_n(irn, n);
3082 if(op->attr.live_range.args.reloads && op->attr.live_range.args.reloads[n] != ILP_UNDEF) {
3085 name = si->lpp->vars[op->attr.live_range.args.reloads[n]];
3086 if(!is_zero(name->value)) {
3088 ir_node *insert_pos = irn;
3089 ir_node *prev = insert_pos;
3093 prev = sched_prev(prev);
3094 } while(be_is_Spill(prev));
3096 prev_op = get_irn_link(prev);
3098 /* insert reload before pre-remats */
3099 while(!sched_is_end(prev) && !be_is_Reload(prev) && !is_Phi(prev)
3100 && prev_op->is_remat && prev_op->attr.remat.pre) {
3104 prev = sched_prev(prev);
3105 } while(be_is_Spill(prev));
3107 prev_op = get_irn_link(prev);
3111 reload = insert_reload(si, arg, insert_pos);
3113 set_irn_n(irn, n, reload);
3115 #ifdef KEEPALIVE_RELOADS
3116 pset_insert_ptr(si->spills, reload);
3124 /* reloads at end of block */
3125 if(spill_bb->reloads) {
3128 set_foreach(spill_bb->reloads, keyval) {
3129 ir_node *irn = (ir_node*)keyval->key;
3130 ilp_var_t reload = PTR_TO_INT(keyval->val);
3133 name = si->lpp->vars[reload];
3134 if(!is_zero(name->value)) {
3136 ir_node *insert_pos = bb;
3137 ir_node *prev = sched_prev(insert_pos);
3138 op_t *prev_op = get_irn_link(prev);
3140 /* insert reload before pre-remats */
3141 while(!sched_is_end(prev) && !be_is_Reload(prev) && !be_is_Spill(prev)
3142 && prev_op->is_remat && prev_op->attr.remat.pre) {
3145 prev = sched_prev(insert_pos);
3146 prev_op = get_irn_link(prev);
3149 reload = insert_reload(si, irn, insert_pos);
3151 #ifdef KEEPALIVE_RELOADS
3152 pset_insert_ptr(si->spills, reload);
3158 del_set(spill_bb->ilp);
3159 if(spill_bb->reloads) del_set(spill_bb->reloads);
3163 walker_collect_used(ir_node * irn, void * data)
3165 lc_bitset_t *used = data;
3167 lc_bitset_set(used, get_irn_idx(irn));
3170 struct kill_helper {
3176 walker_kill_unused(ir_node * bb, void * data)
3178 struct kill_helper *kh = data;
3179 const ir_node *bad = get_irg_bad(get_irn_irg(bb));
3183 for(irn=sched_first(bb); !sched_is_end(irn);) {
3184 ir_node *next = sched_next(irn);
3187 if(!lc_bitset_is_set(kh->used, get_irn_idx(irn))) {
3188 if(be_is_Spill(irn) || be_is_Reload(irn)) {
3189 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)));
3190 assert(lpp_get_sol_state(kh->si->lpp) != lpp_optimal && "optimal solution is suboptimal?");
3195 set_nodes_block(irn, bad);
3196 for (n=get_irn_arity(irn)-1; n>=0; --n) {
3197 set_irn_n(irn, n, bad);
3205 kill_all_unused_values_in_schedule(spill_ilp_t * si)
3207 struct kill_helper kh;
3209 kh.used = lc_bitset_malloc(get_irg_last_idx(si->chordal_env->irg));
3212 irg_walk_graph(si->chordal_env->irg, walker_collect_used, NULL, kh.used);
3213 irg_block_walk_graph(si->chordal_env->irg, walker_kill_unused, NULL, &kh);
3215 lc_bitset_free(kh.used);
3219 print_irn_pset(pset * p)
3223 pset_foreach(p, irn) {
3224 ir_printf("%+F\n", irn);
3229 rewire_uses(spill_ilp_t * si)
3231 dom_front_info_t *dfi = be_compute_dominance_frontiers(si->chordal_env->irg);
3233 pset *ignore = pset_new_ptr(1);
3235 pset_insert_ptr(ignore, get_irg_end(si->chordal_env->irg));
3237 /* then fix uses of spills */
3238 set_foreach(si->values, defs) {
3241 ir_node *next = defs->remats;
3244 reloads = pset_new_ptr_default();
3247 if(be_is_Reload(next)) {
3248 pset_insert_ptr(reloads, next);
3252 next = get_irn_link(next);
3255 spills = get_spills_for_value(si, defs->value);
3256 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));
3257 if(pset_count(spills) > 1) {
3258 //assert(pset_count(reloads) > 0);
3259 // print_irn_pset(spills);
3260 // print_irn_pset(reloads);
3262 be_ssa_constr_set_ignore(dfi, spills, ignore);
3269 /* first fix uses of remats and reloads */
3270 set_foreach(si->values, defs) {
3272 ir_node *next = defs->remats;
3275 nodes = pset_new_ptr_default();
3276 pset_insert_ptr(nodes, defs->value);
3279 pset_insert_ptr(nodes, next);
3280 next = get_irn_link(next);
3283 if(pset_count(nodes) > 1) {
3284 DBG((si->dbg, LEVEL_4, "\t %d new definitions for value %+F\n", pset_count(nodes)-1, defs->value));
3285 be_ssa_constr_set(dfi, nodes);
3292 // remove_unused_defs(si);
3294 be_free_dominance_frontiers(dfi);
3298 writeback_results(spill_ilp_t * si)
3300 /* walk through the graph and collect all spills, reloads and remats for a value */
3302 si->values = new_set(cmp_defs, 4096);
3304 DBG((si->dbg, LEVEL_1, "Applying results\n"));
3305 delete_unnecessary_remats(si);
3306 si->m_unknown = new_r_Unknown(si->chordal_env->irg, mode_M);
3307 irg_block_walk_graph(si->chordal_env->irg, walker_spill_placer, NULL, si);
3309 irg_block_walk_graph(si->chordal_env->irg, walker_reload_placer, NULL, si);
3311 /* clean the remat info! there are still back-edges leading there! */
3312 clean_remat_info(si);
3316 connect_all_spills_with_keep(si);
3318 del_set(si->values);
3322 get_n_regs(spill_ilp_t * si)
3324 int arch_n_regs = arch_register_class_n_regs(si->cls);
3328 for(i=0; i<arch_n_regs; i++) {
3329 if(!arch_register_type_is(&si->cls->regs[i], ignore)) {
3334 DBG((si->dbg, LEVEL_1, "\tArchitecture has %d free registers in class %s\n", free, si->cls->name));
3339 walker_reload_mover(ir_node * bb, void * data)
3341 spill_ilp_t *si = data;
3344 sched_foreach(bb, tmp) {
3345 if(be_is_Reload(tmp) && has_reg_class(si, tmp)) {
3346 ir_node *reload = tmp;
3349 /* move reload upwards */
3351 int pressure = (int)get_irn_link(reload);
3352 if(pressure < si->n_regs) {
3353 irn = sched_prev(reload);
3354 DBG((si->dbg, LEVEL_5, "regpressure before %+F: %d\n", reload, pressure));
3355 sched_remove(reload);
3356 pressure = (int)get_irn_link(irn);
3358 while(pressure < si->n_regs) {
3359 if(sched_is_end(irn) || (be_is_Reload(irn) && has_reg_class(si, irn))) break;
3361 set_irn_link(irn, INT_TO_PTR(pressure+1));
3362 DBG((si->dbg, LEVEL_5, "new regpressure before %+F: %d\n", irn, pressure+1));
3363 irn = sched_prev(irn);
3365 pressure = (int)get_irn_link(irn);
3368 DBG((si->dbg, LEVEL_3, "putting reload %+F after %+F\n", reload, irn));
3369 sched_put_after(irn, reload);
3376 move_reloads_upward(spill_ilp_t * si)
3378 irg_block_walk_graph(si->chordal_env->irg, walker_reload_mover, NULL, si);
3382 be_spill_remat(const be_chordal_env_t * chordal_env)
3384 char problem_name[256];
3385 char dump_suffix[256];
3386 char dump_suffix2[256];
3387 char dump_suffix3[256];
3388 struct obstack obst;
3391 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", chordal_env->irg, chordal_env->cls->name);
3392 ir_snprintf(dump_suffix, sizeof(dump_suffix), "-%s-remats", chordal_env->cls->name);
3393 ir_snprintf(dump_suffix2, sizeof(dump_suffix2), "-%s-pressure", chordal_env->cls->name);
3395 FIRM_DBG_REGISTER(si.dbg, "firm.be.ra.spillremat");
3396 DBG((si.dbg, LEVEL_1, "\n\n\t\t===== Processing %s =====\n\n", problem_name));
3398 obstack_init(&obst);
3399 si.chordal_env = chordal_env;
3401 si.cls = chordal_env->cls;
3402 si.lpp = new_lpp(problem_name, lpp_minimize);
3403 si.remat_info = new_set(cmp_remat_info, 4096);
3404 si.interferences = new_set(cmp_interference, 4096);
3405 si.all_possible_remats = pset_new_ptr_default();
3406 si.spills = pset_new_ptr_default();
3407 si.inverse_ops = pset_new_ptr_default();
3408 #ifndef EXECFREQ_LOOPDEPH
3409 si.execfreqs = compute_execfreq(chordal_env->irg);
3411 si.execfreqs = NULL;
3416 si.n_regs = get_n_regs(&si);
3418 set_irg_link(chordal_env->irg, &si);
3419 compute_doms(chordal_env->irg);
3421 /* compute phi classes */
3422 // phi_class_compute(chordal_env->irg);
3424 be_analyze_regpressure(chordal_env, "-pre");
3426 #ifdef COLLECT_REMATS
3427 /* collect remats */
3428 DBG((si.dbg, LEVEL_1, "Collecting remats\n"));
3429 irg_walk_graph(chordal_env->irg, walker_remat_collector, NULL, &si);
3432 /* insert possible remats */
3433 DBG((si.dbg, LEVEL_1, "Inserting possible remats\n"));
3434 irg_block_walk_graph(chordal_env->irg, walker_remat_insertor, NULL, &si);
3435 DBG((si.dbg, LEVEL_2, " -> inserted %d possible remats\n", pset_count(si.all_possible_remats)));
3438 DBG((si.dbg, LEVEL_1, "Connecting remats with keep and dumping\n"));
3439 connect_all_remats_with_keep(&si);
3440 /* dump graph with inserted remats */
3441 dump_graph_with_remats(chordal_env->irg, dump_suffix);
3445 /* recompute liveness */
3446 DBG((si.dbg, LEVEL_1, "Recomputing liveness\n"));
3447 be_liveness(chordal_env->irg);
3451 DBG((si.dbg, LEVEL_1, "\tBuilding ILP\n"));
3452 DBG((si.dbg, LEVEL_2, "\t endwalker\n"));
3453 irg_block_walk_graph(chordal_env->irg, luke_endwalker, NULL, &si);
3455 DBG((si.dbg, LEVEL_2, "\t blockwalker\n"));
3456 irg_block_walk_graph(chordal_env->irg, luke_blockwalker, NULL, &si);
3458 #ifndef NO_MEMCOPIES
3459 DBG((si.dbg, LEVEL_2, "\t memcopyhandler\n"));
3460 memcopyhandler(&si);
3468 ir_snprintf(buf, sizeof(buf), "%s-spillremat.ilp", problem_name);
3469 if ((f = fopen(buf, "wt")) != NULL) {
3470 lpp_dump_plain(si.lpp, f);
3477 DBG((si.dbg, LEVEL_1, "\tSolving %F\n", chordal_env->irg));
3479 lpp_set_time_limit(si.lpp, ILP_TIMEOUT);
3483 lpp_solve_cplex(si.lpp);
3485 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
3487 assert(lpp_is_sol_valid(si.lpp)
3488 && "solution of ILP must be valid");
3490 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));
3492 #ifdef DUMP_SOLUTION
3497 ir_snprintf(buf, sizeof(buf), "%s-spillremat.sol", problem_name);
3498 if ((f = fopen(buf, "wt")) != NULL) {
3500 for (i = 0; i < si.lpp->var_next; ++i) {
3501 lpp_name_t *name = si.lpp->vars[i];
3502 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
3509 writeback_results(&si);
3513 kill_all_unused_values_in_schedule(&si);
3515 #if defined(KEEPALIVE_SPILLS) || defined(KEEPALIVE_RELOADS)
3516 be_dump(chordal_env->irg, "-spills-placed", dump_ir_block_graph);
3519 // move reloads upwards
3520 be_liveness(chordal_env->irg);
3521 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3522 move_reloads_upward(&si);
3524 irg_block_walk_graph(chordal_env->irg, walker_pressure_annotator, NULL, &si);
3526 dump_pressure_graph(&si, dump_suffix2);
3528 // TODO fix temporarily exceeded regpressure due to remat2s
3530 // TODO insert copys to fix interferences in memory
3532 be_analyze_regpressure(chordal_env, "-post");
3534 free_dom(chordal_env->irg);
3535 del_set(si.interferences);
3536 del_pset(si.inverse_ops);
3537 del_pset(si.all_possible_remats);
3538 del_pset(si.spills);
3539 #ifndef EXECFREQ_LOOPDEPH
3540 free_execfreq(si.execfreqs);
3543 obstack_free(&obst, NULL);
3544 DBG((si.dbg, LEVEL_1, "\tdone.\n"));
3547 #else /* WITH_ILP */
3550 only_that_you_can_compile_without_WITH_ILP_defined(void)
3554 #endif /* WITH_ILP */