4 * @author Sebastian Hack
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
26 #include <lpp/lpp_net.h>
27 #include <lpp/lpp_cplex.h>
31 #include "besched_t.h"
36 #include "bespillilp.h"
40 #define MAX(a,b) ((a) > (b) ? (a) : (b))
42 #define DBG_LEVEL SET_LEVEL_3
49 #define LPP_SERVER "i44pc52"
50 #define LPP_SOLVER "cplex"
54 #define COST_REMAT (-9)
56 #define is_end_of_block_use(lr) (is_Block((lr)->user))
61 typedef struct _edge_reload_t {
66 struct _edge_reload_t *next;
69 typedef struct _spill_stat_t {
75 typedef struct _spill_ilp_t {
76 const arch_register_class_t *cls;
77 const be_main_session_env_t *session;
78 firm_dbg_module_t *dbg;
90 typedef struct _live_range_t live_range_t;
92 typedef struct _irn_use_head_t {
93 struct list_head head;
97 live_range_t *closest_use;
100 struct _live_range_t {
101 struct list_head list;
102 irn_use_head_t *use_head;
111 * Associates the first use of a live-in in a block
112 * with its live range.
114 typedef struct _first_use_t {
116 ir_node *irn; /**< A value live in at bl. */
117 live_range_t *lr; /**< The live range for the first use of irn in bl. */
122 static INLINE int has_reg_class(const spill_ilp_t *si, const ir_node *irn)
124 return arch_irn_has_reg_class(si->session->main_env->arch_env,
125 irn, arch_pos_make_out(0), si->cls);
128 static int cmp_live_range(const void *a, const void *b, size_t n)
130 const live_range_t *p = a;
131 const live_range_t *q = b;
133 return !(p->user == q->user && p->irn == q->irn && p->pos == q->pos);
136 static int cmp_irn_use_head(const void *a, const void *b, size_t n)
138 const irn_use_head_t *p = a;
139 const irn_use_head_t *q = b;
141 return !(p->irn == q->irn);
144 static irn_use_head_t *get_use_head(spill_ilp_t *si, const ir_node *irn)
146 irn_use_head_t templ;
147 templ.irn = (ir_node *) irn;
148 return set_find(si->irn_use_heads, &templ, sizeof(templ), HASH_PTR(irn));
151 static int cmp_first_use(const void *a, const void *b, size_t n)
153 const first_use_t *p = a;
154 const first_use_t *q = b;
156 return !(p->irn == q->irn && p->bl == q->bl);
159 static void add_first_use(spill_ilp_t *si, ir_node *bl, ir_node *irn, live_range_t *lr)
166 set_insert(si->first_uses, &templ, sizeof(templ),
167 HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn)));
170 static live_range_t *get_first_use_lr(spill_ilp_t *si, ir_node *bl, ir_node *irn)
177 res = set_find(si->first_uses, &templ, sizeof(templ),
178 HASH_COMBINE(HASH_PTR(bl), HASH_PTR(irn)));
180 return res ? res->lr : NULL;
184 * Checks, if a vertain node can be recomputed at a certain position.
185 * @param si The spill ILP environment.
186 * @param irn The node to recompute.
187 * @param live The nodes live at the place where @p irn shall be
189 * @return 1, if irn can be recomputed, 0 if not.
191 static INLINE int can_remat(const spill_ilp_t *si, const ir_node *irn, pset *live)
194 const arch_env_t *arch_env = si->session->main_env->arch_env;
195 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
197 for(i = 0, n = get_irn_arity(irn); i < n && remat; ++i) {
198 ir_node *op = get_irn_n(irn, i);
199 remat &= !has_reg_class(si, op) || pset_find_ptr(live, op);
205 static live_range_t *get_live_range(spill_ilp_t *si, ir_node *irn, ir_node *user, int pos)
207 live_range_t lr, *res;
208 irn_use_head_t iuh, *head;
210 unsigned hash = HASH_COMBINE(HASH_PTR(irn), HASH_PTR(user));
216 lr.is_remat_var = -1;
218 res = set_insert(si->live_ranges, &lr, sizeof(lr), hash);
219 is_new = res->in_mem_var == -1;
223 ir_snprintf(buf, sizeof(buf), "m_%s%N_%N_%d",
224 is_Phi(irn) ? "phi_" : "", irn, user, MAX(pos, 0));
225 res->in_mem_var = lpp_add_var(si->lpp, buf, lpp_binary, pos >= 0 ? COST_LOAD : 0.0);
228 memset(&iuh, 0, sizeof(iuh));
231 head = set_insert(si->irn_use_heads, &iuh, sizeof(iuh), HASH_PTR(irn));
232 if(head->n_uses == -1) {
234 INIT_LIST_HEAD(&head->head);
238 list_add_tail(&res->list, &head->head);
242 res->use_head = head;
247 static void print_live_set(spill_ilp_t *si, pset *s) {
249 for(n=pset_first(s); n; n=pset_next(s))
250 DBG((si->dbg, LEVEL_3, " %+F\n", n));
253 static void process_block(ir_node *bl, void *data)
257 spill_ilp_t *si = data;
259 int n_regs = arch_register_class_n_regs(si->cls);
260 int n_preds = get_irn_arity(bl);
261 pset *live = pset_new_ptr_default();
265 DBG((si->dbg, LEVEL_3, "\n"));
266 DBG((si->dbg, LEVEL_3, "Processing %+F\n", bl));
269 * Get all live-end values of this block
271 live_foreach(bl, li) {
272 if(live_is_end(li) && has_reg_class(si, li->irn)) {
273 ir_node *irn = (ir_node *) li->irn;
274 pset_insert_ptr(live, irn);
276 /*The "user" of the live range to the end of a block
277 * is the block itself. This is quite arbitrary. */
278 set_irn_link(irn, get_live_range(si, irn, bl, -1));
281 DBG((si->dbg, LEVEL_3, "Live-End:\n"));
282 print_live_set(si, live);
285 * Walk through the schedule of this block from end to begin.
286 * Phis are handled togther with live ins after this loop.
288 for(irn = sched_last(bl); !sched_is_begin(irn) && !is_Phi(irn); irn = sched_prev(irn)) {
291 int relevant_args, results;
298 * Determine the number of results
300 /* Special handling of Projs */
302 if(has_reg_class(si, irn)) {
303 assert(pset_find_ptr(live, irn) && "node must be live");
304 pset_remove_ptr(live, irn);
308 DBG((si->dbg, LEVEL_2, "Skipped %+F\n", irn));
312 DBG((si->dbg, LEVEL_1, "Irn %+F\n", irn));
315 assert(get_irn_mode(irn) == mode_T && "node before projs must be tuple");
320 if(has_reg_class(si, irn)) {
321 assert(get_irn_mode(irn) != mode_T && "node must not be a tuple");
322 assert(pset_find_ptr(live, irn) && "node must be live");
323 pset_remove_ptr(live, irn);
330 /* cand holds the irns which may be spilled */
331 cand = pset_new_ptr(8);
332 for(l=pset_first(live); l; l=pset_next(live))
333 pset_insert_ptr(cand, l);
336 * Determine number of arguments
339 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
340 ir_node *op = get_irn_n(irn, i);
341 if(has_reg_class(si, op)) {
342 DBG((si->dbg, LEVEL_2, " arg %+F\n", op));
344 /* arguments must not be spilled */
345 if(pset_find_ptr(cand, op))
346 pset_remove_ptr(cand, op);
351 * Determine, how many values must be in memory.
352 * We have 'n_regs' registers.
353 * The instr. needs 'demand'.
354 * So (R:= n_regs - demand) registers can be used for candidates 'cand'.
355 * The rest (M:= n_cand - R) must reside in memory.
357 demand = MAX(results, relevant_args);
358 n_cand = pset_count(cand);
359 must_be_in_mem = n_cand - (n_regs - demand);
361 DBG((si->dbg, LEVEL_1, " Demand: %d, Cands: %d, InMem: %d\n", demand, n_cand, must_be_in_mem));
362 DBG((si->dbg, LEVEL_3, " Cand-Set:\n"));
363 print_live_set(si, cand);
366 * Generate the corresponding constraint spilling
367 * enough candidates at this label.
369 if(must_be_in_mem > 0) {
370 ir_snprintf(buf, sizeof(buf), "cp_%N_%N_%d", bl, irn, step);
371 cst = lpp_add_cst(si->lpp, buf, lpp_greater, must_be_in_mem);
373 for(l = pset_first(cand); l; l = pset_next(cand)) {
374 live_range_t *lr = get_irn_link(l);
375 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
379 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
380 ir_node *op = get_irn_n(irn, i);
382 if(has_reg_class(si, op)) {
383 live_range_t *op_lr = get_live_range(si, op, irn, i);
384 set_irn_link(op, op_lr);
387 // * The operand is reloaded at its usage, so it must not occur
388 // * in the constraint which determines which values live at the
389 // * instruction must reside in memory.
391 // if(must_be_in_mem > 0) {
392 // DBG((si->dbg, LEVEL_3, " Resetting %+F to 0:\n", op));
393 // lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, 0.0);
397 * Check, if the node is a rematerializable node and
398 * if its operands are live here.
400 if(si->enable_remat && can_remat(si, op, live)) {
405 for(j = 0, n = get_irn_arity(op); j < n; ++j)
406 n_operands += has_reg_class(si, get_irn_n(op, j));
408 /* Make the remat constraint for this operand */
409 ir_snprintf(buf, sizeof(buf), "ce1_%N_%N_%d", op, irn, i);
410 cst = lpp_add_cst(si->lpp, buf, lpp_less, n_operands);
412 /* Make the rematerialize variable for the operand */
413 ir_snprintf(buf, sizeof(buf), "e_%N_%N_%d", op, irn, i);
414 op_lr->is_remat_var = lpp_add_var(si->lpp, buf, lpp_binary, COST_REMAT);
415 lpp_set_factor_fast(si->lpp, cst, op_lr->is_remat_var, n_operands);
417 for(j = 0, n = get_irn_arity(op); j < n; ++j) {
418 ir_node *oop = get_irn_n(op, j);
419 if(has_reg_class(si, oop)) {
420 live_range_t *lr = get_irn_link(oop);
421 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
425 ir_snprintf(buf, sizeof(buf), "ce2_%N_%N_%d", op, irn, i);
426 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
427 lpp_set_factor_fast(si->lpp, cst, op_lr->is_remat_var, 1.0);
428 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, -1.0);
434 * Insert arguments of current instr into the live set
436 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
437 ir_node *op = get_irn_n(irn, i);
438 if(has_reg_class(si, op))
439 pset_insert_ptr(live, op);
446 if(bl == get_irg_start_block(get_irn_irg(bl)))
450 * Here, the live set contains
451 * - phis of the block
452 * - live-in values of the block
454 * TODO: comment is wrong
455 * If a value is live in, it must be in a register in all predecessor
456 * blocks or in memory at the end of all predecessor blocks. Also, the
457 * closest use in the current block must then be from register or
458 * memory, respectively.
460 for(irn = pset_first(live); irn; irn = pset_next(live)) {
461 live_range_t *lr = get_irn_link(irn);
462 int is_phi = is_Phi(irn) && get_nodes_block(irn) == bl;
465 assert(has_reg_class(si, irn));
466 assert(is_Phi(irn) || is_live_in(bl, irn));
468 /* Deprecated: Can be done with the first uses map */
470 lr->use_head->closest_use = lr;
473 * Remind the liverange of the first use of a live (or phi) in the
476 add_first_use(si, bl, irn, lr);
478 for(i = 0; i < n_preds; ++i) {
479 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
480 ir_node *end_node = is_phi ? get_irn_n(irn, i) : irn;
481 live_range_t *op_lr = get_live_range(si, end_node, pred_bl, -1);
482 edge_reload_t *edge = obstack_alloc(si->obst, sizeof(edge[0]));
484 ir_snprintf(buf, sizeof(buf), "edge_%N_%N_%N_%N", bl, pred_bl, end_node, op_lr->irn);
485 edge->in_mem_var = lpp_add_var(si->lpp, buf, lpp_binary, COST_LOAD);
487 edge->irn = end_node;
490 ir_snprintf(buf, sizeof(buf), "cedge_%N_%N_%N_%N", bl, pred_bl, end_node, op_lr->irn);
491 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
492 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, 1.0);
493 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, -1.0);
494 lpp_set_factor_fast(si->lpp, cst, edge->in_mem_var, -1.0);
503 * Add the costs for a store.
505 * If one of the uses is from memory, add additional costs for the
508 * m_1 + ... + m_n - M * s <= 0
510 * @param si The ILP spilling environment.
512 static void add_store_costs(spill_ilp_t *si)
516 double costs = si->enable_store ? COST_STORE : 0.0;
518 for(uh = set_first(si->irn_use_heads); uh; uh = set_next(si->irn_use_heads)) {
522 ir_snprintf(buf, sizeof(buf), "cs_%N", uh->irn);
523 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
525 ir_snprintf(buf, sizeof(buf), "s_%N", uh->irn);
526 uh->spill_var = lpp_add_var(si->lpp, buf, lpp_binary, costs);
527 lpp_set_factor_fast(si->lpp, cst, uh->spill_var, -BIGM);
529 list_for_each_entry(live_range_t, lr, &uh->head, list)
530 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
534 static INLINE int is_zero(double x)
536 return fabs(x) < 0.00001;
539 static int is_spilled(const spill_ilp_t *si, const live_range_t *lr)
541 return !is_zero(lpp_get_var_sol(si->lpp, lr->in_mem_var));
544 static int is_mem_phi(const ir_node *phi, void *data)
546 spill_ilp_t *si = data;
547 return is_spilled(si, get_use_head(si, phi)->closest_use);
550 static void writeback_results(spill_ilp_t *si)
555 /* Look at each node and examine the usages. */
556 for(uh = set_first(si->irn_use_heads); uh; uh = set_next(si->irn_use_heads)) {
559 /* Go through all live ranges of the node. */
560 list_for_each_entry(live_range_t, lr, &uh->head, list) {
561 if(is_spilled(si, lr) && !is_end_of_block_use(lr))
562 be_add_reload(si->senv, lr->irn, lr->user);
566 for(edge = si->edges; edge; edge = edge->next) {
567 if(!is_zero(edge->in_mem_var))
568 be_add_reload_on_edge(si->senv, edge->irn, edge->bl, edge->pos);
571 be_insert_spills_reloads(si->senv, NULL);
574 void be_spill_ilp(const be_main_session_env_t *session_env,
575 const arch_register_class_t *cls)
577 char problem_name[256];
581 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", session_env->irg, cls->name);
584 si.session = session_env;
586 si.dbg = firm_dbg_register("be.ra.spillilp");
587 si.senv = be_new_spill_env(si.dbg, session_env, cls, is_mem_phi, &si);
589 si.lpp = new_lpp(problem_name, lpp_minimize);
590 si.irn_use_heads = new_set(cmp_irn_use_head, 4096);
591 si.live_ranges = new_set(cmp_live_range, 16384);
592 si.first_uses = new_set(cmp_first_use, 4096);
597 firm_dbg_set_mask(si.dbg, DBG_LEVEL);
598 irg_block_walk_graph(session_env->irg, process_block, NULL, &si);
600 add_store_costs(&si);
607 ir_snprintf(buf, sizeof(buf), "spill-%s.ilp", problem_name);
608 if((f = fopen(buf, "wt")) != NULL) {
609 lpp_dump_plain(si.lpp, f);
615 DBG((si.dbg, LEVEL_1, "%F\n", session_env->irg));
617 lpp_solve_cplex(si.lpp);
619 lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
621 assert(lpp_is_sol_valid(si.lpp) && "solution of ILP must be valid");
623 DBG((si.dbg, LEVEL_1, "\tnodes: %d, vars: %d, csts: %d\n",
624 set_count(si.irn_use_heads), si.lpp->var_next, si.lpp->cst_next));
625 DBG((si.dbg, LEVEL_1, "\titerations: %d, solution time: %g\n",
626 si.lpp->iterations, si.lpp->sol_time));
633 ir_snprintf(buf, sizeof(buf), "spill-%s.sol", problem_name);
634 if((f = fopen(buf, "wt")) != NULL) {
636 for(i = 0; i < si.lpp->var_next; ++i) {
637 lpp_name_t *name = si.lpp->vars[i];
638 fprintf(f, "%20s %4d %10f\n", name->name, name->nr, name->value);
645 writeback_results(&si);
651 ir_snprintf(buf, sizeof(buf), "%s-spill.stat", problem_name);
652 if((f = fopen(buf, "wt")) != NULL) {
653 fprintf(f, "%20s: %d\n", "nodes", set_count(si.irn_use_heads));
654 fprintf(f, "%20s: %d\n", "vars", si.lpp->var_next);
655 fprintf(f, "%20s: %d\n", "csts", si.lpp->cst_next);
656 fprintf(f, "%20s: %f\n", "sol time", si.lpp->sol_time);
657 fprintf(f, "%20s: %d\n", "spills", si->stats.n_spills);
658 fprintf(f, "%20s: %d\n", "reloads", si->stats.n_reloads);
659 fprintf(f, "%20s: %d\n", "remats", si->stats.n_remat);
665 del_set(si.irn_use_heads);
666 del_set(si.live_ranges);
668 obstack_free(&obst, NULL);