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_4
48 #define LPP_SERVER "i44pc52"
49 #define LPP_SOLVER "cplex"
53 #define COST_REMAT (-9)
55 #define is_end_of_block_use(lr) (is_Block((lr)->user))
57 typedef struct _spill_stat_t {
63 typedef struct _spill_ilp_t {
64 const arch_register_class_t *cls;
65 firm_dbg_module_t *dbg;
75 typedef struct _live_range_t live_range_t;
77 typedef struct _irn_use_head_t {
78 struct list_head head;
82 live_range_t *closest_use;
85 struct _live_range_t {
86 struct list_head list;
87 irn_use_head_t *use_head;
95 static int has_reg_class(const spill_ilp_t *si, const ir_node *irn)
97 return arch_irn_has_reg_class(si->senv.session->main_env->arch_env,
98 irn, arch_pos_make_out(0), si->cls);
101 static int cmp_live_range(const void *a, const void *b, size_t n)
103 const live_range_t *p = a;
104 const live_range_t *q = b;
106 return !(p->user == q->user && p->irn == q->irn && p->pos == q->pos);
109 static int cmp_irn_use_head(const void *a, const void *b, size_t n)
111 const irn_use_head_t *p = a;
112 const irn_use_head_t *q = b;
114 return !(p->irn == q->irn);
118 * Checks, if a vertain node can be recomputed at a certain position.
119 * @param si The spill ILP environment.
120 * @param irn The node to recompute.
121 * @param live The nodes live at the place where @p irn shall be
123 * @return 1, if irn can be recomputed, 0 if not.
125 static INLINE int can_remat(const spill_ilp_t *si, const ir_node *irn, pset *live)
128 const arch_env_t *arch_env = si->senv.session->main_env->arch_env;
129 int remat = (arch_irn_get_flags(arch_env, irn) & arch_irn_flags_rematerializable) != 0;
131 for(i = 0, n = get_irn_arity(irn); i < n && remat; ++i) {
132 ir_node *op = get_irn_n(irn, i);
133 remat &= !has_reg_class(si, op) || pset_find_ptr(live, op);
139 static live_range_t *get_live_range(spill_ilp_t *si, ir_node *irn, ir_node *user, int pos)
141 live_range_t lr, *res;
142 irn_use_head_t iuh, *head;
144 unsigned hash = HASH_COMBINE(HASH_PTR(irn), HASH_PTR(user));
150 lr.is_remat_var = -1;
152 res = set_insert(si->live_ranges, &lr, sizeof(lr), hash);
153 is_new = res->in_mem_var == -1;
157 ir_snprintf(buf, sizeof(buf), "m_%s%N_%N_%d",
158 is_Phi(irn) ? "phi_" : "", irn, user, MAX(pos, 0));
159 res->in_mem_var = lpp_add_var(si->lpp, buf, lpp_binary, pos >= 0 ? COST_LOAD : 0.0);
162 memset(&iuh, 0, sizeof(iuh));
165 head = set_insert(si->irn_use_heads, &iuh, sizeof(iuh), HASH_PTR(irn));
166 if(head->n_uses == -1) {
168 INIT_LIST_HEAD(&head->head);
172 list_add_tail(&res->list, &head->head);
176 res->use_head = head;
181 static ir_node *process_irn(spill_ilp_t *si, pset *live, ir_node *irn, int *demand)
184 int relevant_args = 0, results = 0;
186 DBG((si->dbg, LEVEL_1, "at %+F\n", irn));
188 while(is_Proj(irn)) {
189 if(has_reg_class(si, irn)) {
190 assert(pset_find_ptr(live, irn) && "node must be live");
191 pset_remove_ptr(live, irn);
195 DBG((si->dbg, LEVEL_1, "skipped proj %+F\n", irn));
196 irn = sched_prev(irn);
199 DBG((si->dbg, LEVEL_1, "\tlanded at irn %+F\n", irn));
202 assert(get_irn_mode(irn) == mode_T && "node before projs must be tuple");
204 if(has_reg_class(si, irn)) {
205 assert( pset_find_ptr(live, irn) && "node must be live");
206 pset_remove_ptr(live, irn);
210 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
211 ir_node *op = get_irn_n(irn, i);
212 if(has_reg_class(si, op) && !pset_find_ptr(live, op)) {
214 DBG((si->dbg, LEVEL_1, "\trelevant arg %+F\n", op));
218 *demand = MAX(results, relevant_args);
219 DBG((si->dbg, LEVEL_1, "\tdemand: %d\n", *demand));
223 static void process_block(ir_node *bl, void *data)
227 spill_ilp_t *si = data;
229 int n_regs = arch_register_class_n_regs(si->cls);
230 int n_preds = get_irn_arity(bl);
231 pset *live = pset_new_ptr_default();
233 ir_node *irn, *next_irn;
235 /* as always, bring the live end nodes to life here */
236 live_foreach(bl, li) {
237 if(live_is_end(li) && has_reg_class(si, li->irn)) {
238 ir_node *irn = (ir_node *) li->irn;
239 pset_insert_ptr(live, irn);
242 * The "user" of the live range to the end of a block
243 * is the block itself. This is quite arbitrary.
245 set_irn_link(irn, get_live_range(si, irn, bl, -1));
249 for(irn = sched_last(bl); !sched_is_begin(irn) && !is_Phi(irn); irn = sched_prev(irn)) {
256 /* We handle phi togther with live ins after this loop (see below). */
261 if(has_reg_class(si, irn))
262 pset_remove_ptr(live, irn);
264 demand = register_demand(si, live, irn);
265 n_live = pset_count(live);
268 irn = process_irn(si, live, irn, &demand);
269 n_live = pset_count(live);
272 * Determine, how many values (which are not used at the label)
274 * demand means the number of registers, the operation will consume.
275 * So there are n_regs - demand registers available to store values
276 * which are not used at this label. The rest must reside in memory.
278 must_be_in_mem = MAX(n_live + demand - n_regs, 0);
280 if(must_be_in_mem > 0) {
283 * The constraint limiting the pressure at this label to
284 * the number of free registers.
286 ir_snprintf(buf, sizeof(buf), "cp_%N_%d", bl, step);
287 cst = lpp_add_cst(si->lpp, buf, lpp_greater, must_be_in_mem);
289 for(l = pset_first(live); l; l = pset_next(live)) {
290 live_range_t *lr = get_irn_link(l);
291 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
295 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
296 ir_node *op = get_irn_n(irn, i);
298 if(has_reg_class(si, op)) {
299 live_range_t *op_lr = get_live_range(si, op, irn, i);
301 set_irn_link(op, op_lr);
304 * The operand is reloaded at its usage, so it must not occur
305 * in the constraint which determines which values live at the
306 * instruction must reside in memory.
308 if(must_be_in_mem > 0) {
309 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, 0.0);
313 * Check, if the node is a rematerializable node and
314 * if its operands are live here.
316 if(si->enable_remat && can_remat(si, op, live)) {
321 for(j = 0, n = get_irn_arity(op); j < n; ++j)
322 n_operands += has_reg_class(si, get_irn_n(op, j));
324 /* Make the remat constraint for this operand */
325 ir_snprintf(buf, sizeof(buf), "ce1_%N_%N_%d", op, irn, i);
326 cst = lpp_add_cst(si->lpp, buf, lpp_less, n_operands);
328 /* Make the rematerialize variable for the operand */
329 ir_snprintf(buf, sizeof(buf), "e_%N_%N_%d", op, irn, i);
330 op_lr->is_remat_var = lpp_add_var(si->lpp, buf, lpp_binary, COST_REMAT);
331 lpp_set_factor_fast(si->lpp, cst, op_lr->is_remat_var, n_operands);
333 for(j = 0, n = get_irn_arity(op); j < n; ++j) {
334 ir_node *oop = get_irn_n(op, j);
335 if(has_reg_class(si, oop)) {
336 live_range_t *lr = get_irn_link(oop);
337 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
341 ir_snprintf(buf, sizeof(buf), "ce2_%N_%N_%d", op, irn, i);
342 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0.0);
343 lpp_set_factor_fast(si->lpp, cst, op_lr->is_remat_var, 1.0);
344 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, -1.0);
349 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
350 ir_node *op = get_irn_n(irn, i);
351 if(has_reg_class(si, op) && !is_Phi(irn))
352 pset_insert_ptr(live, op);
358 if(bl == get_irg_start_block(get_irn_irg(bl)))
362 * Here, only the phis in the block and the values live in are in the
365 * If a value is live in, it must be in a register in all predecessor
366 * blocks or in memory at the end of all predecessor blocks. Also, the
367 * closest use in the current block must then be from register or
368 * memory, respectively.
370 for(irn = pset_first(live); irn; irn = pset_next(live)) {
371 live_range_t *lr = get_irn_link(irn);
372 int is_phi = is_Phi(irn) && get_nodes_block(irn) == bl;
376 lr->use_head->closest_use = lr;
378 assert(has_reg_class(si, irn));
379 assert(is_Phi(irn) || is_live_in(bl, irn));
382 ir_snprintf(buf, sizeof(buf), "c%s_%N_%N", (is_phi ? "phi" : "li"), irn, bl);
383 cst = lpp_add_cst(si->lpp, buf, lpp_equal, 0.0);
384 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, -n_preds);
386 for(i = 0; i < n_preds; ++i) {
387 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
388 ir_node *end_node = is_phi ? get_irn_n(irn, i) : irn;
389 live_range_t *op_lr = get_live_range(si, end_node, pred_bl, -1);
391 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, 1.0);
395 for(i = 0; i < n_preds; ++i) {
396 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
397 ir_node *end_node = is_phi ? get_irn_n(irn, i) : irn;
398 live_range_t *op_lr = get_live_range(si, end_node, pred_bl, -1);
400 ir_snprintf(buf, sizeof(buf), "cpred_%N_%N_%d", lr->irn, bl, i);
401 cst = lpp_add_cst(si->lpp, buf, lpp_equal, 0.0);
402 lpp_set_factor_fast(si->lpp, cst, op_lr->in_mem_var, 1.0);
403 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, -1.0);
413 * Add the costs for a store.
415 * If one of the uses is from memory, add additional costs for the
418 * m_1 + ... + m_n - M * s <= 0
420 * @param si The ILP spilling environment.
422 static void add_store_costs(spill_ilp_t *si)
426 double costs = si->enable_store ? COST_STORE : 0.0;
428 for(uh = set_first(si->irn_use_heads); uh; uh = set_next(si->irn_use_heads)) {
432 ir_snprintf(buf, sizeof(buf), "cs_%N", uh->irn);
433 cst = lpp_add_cst(si->lpp, buf, lpp_less, 0);
435 ir_snprintf(buf, sizeof(buf), "s_%N", uh->irn);
436 uh->spill_var = lpp_add_var(si->lpp, buf, lpp_binary, costs);
437 lpp_set_factor_fast(si->lpp, cst, uh->spill_var, -BIGM);
439 list_for_each_entry(live_range_t, lr, &uh->head, list)
440 lpp_set_factor_fast(si->lpp, cst, lr->in_mem_var, 1.0);
444 static INLINE int is_zero(double x)
446 return fabs(x) < 0.00001;
449 static int is_spilled(const spill_ilp_t *si, const live_range_t *lr)
451 return !is_zero(lpp_get_var_sol(si->lpp, lr->in_mem_var));
454 static void writeback_results(spill_ilp_t *si)
456 const be_node_factory_t *fact = si->senv.session->main_env->node_factory;
458 si->senv.mem_phis = pset_new_ptr_default();
460 for(uh = set_first(si->irn_use_heads); uh; uh = set_next(si->irn_use_heads)) {
461 if(is_Phi(uh->irn) && is_spilled(si, uh->closest_use))
462 pset_insert_ptr(si->senv.mem_phis, uh->irn);
465 /* Look at each node and examine the usages. */
466 for(uh = set_first(si->irn_use_heads); uh; uh = set_next(si->irn_use_heads)) {
471 ir_node *irn = uh->irn;
472 ir_mode *mode = get_irn_mode(irn);
474 /* Go through all live ranges of the node. */
475 list_for_each_entry(live_range_t, lr, &uh->head, list) {
476 int spilled = is_spilled(si, lr);
477 // int rematd = !is_zero(lpp_get_var_sol(si->lpp, lr->is_remat_var));
479 if(spilled && !is_end_of_block_use(lr)) {
480 ir_node *bl = get_nodes_block(lr->user);
483 ir_node *spill = be_spill_node(&si->senv, lr->irn);
484 ir_node *reload = new_Reload(fact, si->cls, si->senv.session->irg, bl, mode, spill);
486 /* inc_stats_reload(si); */
487 obstack_ptr_grow(si->obst, reload);
490 sched_add_before(lr->user, reload);
495 reloads = obstack_finish(si->obst);
496 be_introduce_copies_ignore(si->senv.session->dom_front, irn, n_reloads, reloads, si->senv.mem_phis);
497 obstack_free(si->obst, reloads);
500 be_remove_spilled_phis(&si->senv);
503 void be_spill_ilp(const be_main_session_env_t *session_env,
504 const arch_register_class_t *cls)
506 char problem_name[256];
510 ir_snprintf(problem_name, sizeof(problem_name), "%F_%s", session_env->irg, cls->name);
514 si.dbg = firm_dbg_register("be.ra.spillilp");
515 si.senv.session = session_env;
517 si.lpp = new_lpp(problem_name, lpp_minimize);
518 si.irn_use_heads = new_set(cmp_irn_use_head, 4096);
519 si.live_ranges = new_set(cmp_live_range, 16384);
520 si.senv.spill_ctxs = new_set(be_set_cmp_spillctx, 4096);
524 firm_dbg_set_mask(si.dbg, DBG_LEVEL);
525 irg_block_walk_graph(session_env->irg, process_block, NULL, &si);
527 add_store_costs(&si);
534 ir_snprintf(buf, sizeof(buf), "spill-%s.ilp", problem_name);
535 if((f = fopen(buf, "wt")) != NULL) {
536 lpp_dump_plain(si.lpp, f);
542 DBG((si.dbg, LEVEL_1, "%F\n", session_env->irg));
543 // lpp_solve_net(si.lpp, LPP_SERVER, LPP_SOLVER);
544 lpp_solve_cplex(si.lpp);
545 assert(lpp_is_sol_valid(si.lpp) && "solution of ILP must be valid");
547 DBG((si.dbg, LEVEL_1, "\tnodes: %d, vars: %d, csts: %d\n",
548 set_count(si.irn_use_heads), si.lpp->var_next, si.lpp->cst_next));
549 DBG((si.dbg, LEVEL_1, "\titerations: %d, solution time: %g\n",
550 si.lpp->iterations, si.lpp->sol_time));
557 ir_snprintf(buf, sizeof(buf), "spill-%s.sol", problem_name);
558 if((f = fopen(buf, "wt")) != NULL) {
560 for(i = 0; i < si.lpp->var_next; ++i) {
561 lpp_name_t *name = si.lpp->vars[i];
562 fprintf(f, "%10s %4d %10f\n", name->name, name->nr, name->value);
569 writeback_results(&si);
575 ir_snprintf(buf, sizeof(buf), "%s-spill.stat", problem_name);
576 if((f = fopen(buf, "wt")) != NULL) {
577 fprintf(f, "%20s: %d\n", "nodes", set_count(si.irn_use_heads));
578 fprintf(f, "%20s: %d\n", "vars", si.lpp->var_next);
579 fprintf(f, "%20s: %d\n", "csts", si.lpp->cst_next);
580 fprintf(f, "%20s: %f\n", "sol time", si.lpp->sol_time);
581 fprintf(f, "%20s: %d\n", "spills", si->stats.n_spills);
582 fprintf(f, "%20s: %d\n", "reloads", si->stats.n_reloads);
583 fprintf(f, "%20s: %d\n", "remats", si->stats.n_remat);
589 del_set(si.irn_use_heads);
590 del_set(si.live_ranges);
592 obstack_free(&obst, NULL);