4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
18 #define PATH_CONSTRAINTS_FOR_CLASSES
19 #undef PRECOLOR_MAX_CLIQUE
21 #undef NO_NULL_COLORS_EXTRA_CSTS
22 #undef NO_NULL_COLORS_WITH_COSTS
23 #if (defined(NO_NULL_COLORS_EXTRA_CSTS) || defined(NO_NULL_COLORS_WITH_COSTS)) && !defined(NO_NULL_COLORS)
24 #error Chose your weapon!
30 #include <lpp/lpp_net.h>
31 #include <lpp/lpp_cplex.h>
32 #include <lpp/lpp_remote.h>
36 #include "iredges_t.h"
37 #include "bechordal_t.h"
38 #include "becopyopt.h"
39 #include "becopystat.h"
40 #include "besched_t.h"
43 #define LPP_HOST "i44pc52"
44 #define LPP_SOLVER "cplex"
47 static firm_dbg_module_t *dbg = NULL;
49 #define MAX(a,b) ((a<b)?(b):(a))
50 #define MIN(a,b) ((a<b)?(a):(b))
51 #define EPSILON 0.00001
52 #define SLOTS_LIVING 32
54 typedef struct _simpl_t {
55 struct list_head chain;
59 typedef struct _problem_instance_t {
60 const copy_opt_t *co; /** the copy_opt problem */
61 /* problem size reduction removing simple nodes */
62 struct list_head simplicials; /**< holds all simpl_t's in right order to color*/
63 pset *removed; /**< holds all removed simplicial irns */
65 lpp_t *curr_lp; /**< points to the problem currently used */
66 lpp_t *dilp; /**< problem formulation directly as milp */
67 #ifdef NO_NULL_COLORS_EXTRA_CSTS
68 int first_nnc_cst_idx; /**< the first index of a constraint belonging to no-null-colors stuff*/
70 int first_nnc_var_idx; /**< the first index of a constraint belonging to no-null-colors stuff*/
72 int cst_counter, first_x_var, last_x_var;
78 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
80 #define is_color_possible(irn,color) arch_reg_is_allocatable(get_arch_env(pi->co), irn, -1, arch_register_for_index(pi->co->chordal_env->cls, color))
83 * Some stuff for variable name handling.
85 #define mangle_cst(buf, prefix, nr) \
86 snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
88 #define mangle_var1(buf, prefix, color) \
89 snprintf((buf), sizeof(buf), "%c%d", (prefix), (color))
91 #define mangle_var2(buf, prefix, node_nr, color) \
92 snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
94 #define mangle_var3(buf, prefix, n1, n2, col) \
95 snprintf((buf), sizeof(buf), "%c%d_%d_%d", (prefix), (n1), (n2), (col))
97 #define mangle_var_irn(buf, prefix, irn, color) \
98 mangle_var2((buf), (prefix), get_irn_graph_nr(irn), (color))
100 #define split_var(var, nnr, col) \
101 sscanf(var, "x%d_%d", (nnr), (col))
105 * Checks if a node is simplicial in the graph
106 * heeding the already removed nodes.
108 static INLINE int pi_is_simplicial(problem_instance_t *pi, const ir_node *ifn) {
110 ir_node **all, *curr;
111 be_ifg_t *ifg = pi->co->chordal_env->ifg;
112 void *iter = be_ifg_iter_alloca(ifg);
114 all = alloca(be_ifg_degree(ifg, ifn) * sizeof(*all));
116 /* get all non-removed neighbors */
117 be_ifg_foreach_neighbour(ifg, iter, ifn, curr)
118 if (!is_removed(curr))
121 /* check if these form a clique */
122 for (i=0; i<size; ++i)
123 for (o=i+1; o<size; ++o)
124 if (!be_ifg_connected(ifg, all[i], all[o]))
127 /* all edges exist so this is a clique */
131 static int irn_cmp(const void *a, const void *b, size_t n)
137 * Iterative finds and 'removes' from the graph all nodes which are
138 * simplicial AND not member of a equal-color-wish
140 static void pi_find_simplicials(problem_instance_t *pi) {
144 const be_ifg_t *ifg = pi->co->chordal_env->ifg;
145 void *iter = be_ifg_iter_alloca(ifg);
147 DBG((dbg, LEVEL_2, "Find simlicials...\n"));
151 be_ifg_foreach_node(ifg, iter, irn) {
152 if (!is_removed(irn) && !is_optimizable(get_arch_env(pi->co), irn) && !is_optimizable_arg(pi->co, irn)) {
153 if (pi_is_simplicial(pi, irn)) {
154 simpl_t *s = xmalloc(sizeof(*s));
156 list_add(&s->chain, &pi->simplicials);
157 pset_insert_ptr(pi->removed, irn);
159 DBG((dbg, LEVEL_2, " Removed %+F\n", irn));
165 /* TODO: Count inside the last look */
166 be_ifg_foreach_node(ifg, iter, irn) {
170 if (n_nodes == pset_count(pi->removed))
171 pi->all_simplicial = 1;
174 #ifdef NO_NULL_COLORS
175 static void pi_add_constr_no_null_colors(problem_instance_t *pi) {
176 int cst_counter=0, col, var_idx, cst_idx;
177 int n_colors = pi->co->chordal_env->cls->n_regs;
180 for (col = 0; col < n_colors; ++col) {
181 mangle_var1(buf, 'u', col);
182 #ifdef NO_NULL_COLORS_WITH_COSTS
183 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (double) (1 << (col+1)) );
185 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (2.0 * n_colors) );
187 if (!pi->first_nnc_var_idx)
188 pi->first_nnc_var_idx = var_idx;
191 #ifdef NO_NULL_COLORS_EXTRA_CSTS
192 for (col = 0; col < n_colors; ++col) {
193 mangle_cst(buf, 'U', cst_counter++);
194 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
195 if (!pi->first_nnc_cst_idx)
196 pi->first_nnc_cst_idx = cst_idx;
197 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col, -1);
201 #ifndef NO_NULL_COLORS_WITH_COSTS
202 for (col = 0; col < n_colors - 1; ++col) {
203 mangle_cst(buf, 'U', cst_counter++);
204 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
205 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col , 1);
206 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col+1, -1);
214 * Add coloring-force conditions
215 * Matrix A: knapsack constraint for each node
217 static void pi_add_constr_A(problem_instance_t *pi) {
220 DBG((dbg, LEVEL_2, "Add A constraints...\n"));
221 /* iterate over all blocks */
222 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
223 struct list_head *head = pme->value;
225 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
227 list_for_each_entry_reverse(border_t, curr, head, list)
228 if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
229 int cst_idx, nnr, col;
231 nnr = get_irn_graph_nr(curr->irn);
232 mangle_cst(pi->buf, 'A', nnr);
233 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
235 /* iterate over all possible colors in order */
236 bitset_clear_all(pos_regs);
237 arch_get_allocatable_regs(get_arch_env(pi->co), curr->irn, -1, pi->co->chordal_env->cls, pos_regs);
238 bitset_foreach(pos_regs, col) {
240 mangle_var2(pi->buf, 'x', nnr, col);
241 var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
242 if (!pi->first_x_var)
243 pi->first_x_var = var_idx;
244 pi->last_x_var = var_idx;
245 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
246 #ifdef NO_NULL_COLORS_EXTRA_CSTS
247 lpp_set_factor_fast(pi->curr_lp, pi->first_nnc_cst_idx+col, var_idx, 1);
255 * Checks if all nodes in @p living are live in in block @p block.
256 * @return 1 if all are live in
259 static INLINE int all_live_in(ir_node *block, pset *living) {
261 for (n = pset_first(living); n; n = pset_next(living))
262 if (!is_live_in(block, n)) {
270 * Finds cliques in the interference graph, considering only nodes
271 * for which the color @p color is possible. Finds only 'maximal-cliques',
272 * viz cliques which are not contained in another one.
273 * Matrix B: interference constraints using cliques
275 static void pi_add_constr_B(problem_instance_t *pi, int color) {
276 enum phase_t {growing, shrinking} phase = growing;
279 pset *living = pset_new_ptr(SLOTS_LIVING);
281 DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
282 /* iterate over all blocks */
283 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
284 ir_node *block = pme->key;
285 struct list_head *head = pme->value;
287 list_for_each_entry_reverse(border_t, b, head, list) {
288 const ir_node *irn = b->irn;
289 if (is_removed(irn) || !is_color_possible(irn, color))
293 DBG((dbg, LEVEL_2, "Def %n\n", irn));
294 pset_insert_ptr(living, irn);
296 } else { /* is_use */
297 DBG((dbg, LEVEL_2, "Use %n\n", irn));
299 /* before shrinking the set, store the current 'maximum' clique;
300 * do NOT if clique is a single node
301 * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
302 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
305 mangle_cst(pi->buf, 'B', pi->cst_counter);
306 #ifdef NO_NULL_COLORS
307 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 0);
309 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
311 for (n = pset_first(living); n; n = pset_next(living)) {
313 mangle_var_irn(pi->buf, 'x', n, color);
314 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
315 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
317 #ifdef NO_NULL_COLORS
318 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+color, -1.0);
322 pset_remove_ptr(living, irn);
327 assert(0 == pset_count(living));
332 * Generates constraints which interrelate x with y variables.
333 * x1 and x2 have the different colors ==> y_12 = 1
335 static void pi_add_constr_E(problem_instance_t *pi) {
337 bitset_t *root_regs, *arg_regs, *work_regs;
339 unsigned nregs = pi->co->chordal_env->cls->n_regs;
340 root_regs = bitset_alloca(nregs);
341 arg_regs = bitset_alloca(nregs);
342 work_regs = bitset_alloca(nregs);
344 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
345 /* for all roots of optimization units */
346 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
348 int rootnr, argnr, color;
352 root = curr->nodes[0];
353 rootnr = get_irn_graph_nr(root);
354 bitset_clear_all(root_regs);
355 arch_get_allocatable_regs(get_arch_env(pi->co), root, -1, pi->co->chordal_env->cls, root_regs);
357 /* for all arguments of root */
358 for (i = 1; i < curr->node_count; ++i) {
359 arg = curr->nodes[i];
360 argnr = get_irn_graph_nr(arg);
361 bitset_clear_all(arg_regs);
362 arch_get_allocatable_regs(get_arch_env(pi->co), arg, -1, pi->co->chordal_env->cls, arg_regs);
364 /* Introduce new variable and set factor in objective function */
365 mangle_var2(buf, 'y', rootnr, argnr);
366 y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
368 /* set starting value */
369 lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
371 /* For all colors root and arg have in common, add 2 constraints to E */
372 bitset_copy(work_regs, root_regs);
373 bitset_and(work_regs, arg_regs);
374 bitset_foreach(work_regs, color) {
375 int root_idx, arg_idx, cst_idx;
376 mangle_var2(buf, 'x', rootnr, color);
377 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
378 mangle_var2(buf, 'x', argnr, color);
379 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
381 /* add root-arg-y <= 0 */
382 mangle_cst(buf, 'E', cst_counter++);
383 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
384 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
385 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
386 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
388 /* add arg-root-y <= 0 */
389 mangle_cst(buf, 'E', cst_counter++);
390 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
391 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
392 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
393 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
395 /* For all colors root and arg have "disjunct", add 1 constraints to E.
396 * If root gets a color the arg is not possible to get then they will
397 * definetly get different colors. So y has to be 1.
398 * Vice versa for arg.
400 bitset_copy(work_regs, root_regs);
401 bitset_xor(work_regs, arg_regs);
402 bitset_foreach(work_regs, color) {
403 int root_idx, arg_idx, cst_idx;
404 mangle_var2(buf, 'x', rootnr, color);
405 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
406 mangle_var2(buf, 'x', argnr, color);
407 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
409 mangle_cst(buf, 'E', cst_counter++);
410 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
411 if (bitset_is_set(root_regs, color)) {
412 /* add root-y <= 0 */
413 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
414 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
416 assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
418 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
419 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
426 static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
429 /* search optimization unit for phi */
430 list_for_each_entry(unit_t, curr, &pi->co->units, units)
431 if (curr->nodes[0] == phi) {
432 for (i=1; i<curr->node_count; ++i)
433 if (curr->nodes[i] == irn)
434 return curr->costs[i];
435 assert(0 && "irn must occur in this ou");
437 assert(0 && "phi must be found in a ou");
441 static void clique_path_walker(ir_node *block, void *env) {
442 problem_instance_t *pi = env;
443 int count, arity, row, col, other_row, *costs;
444 ir_node **phis, *phi, *irn, **phi_matrix;
446 bitset_t *candidates;
448 /* Count all phi nodes of this block */
449 for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
452 /* We at least 2 phi nodes for this class of inequalities */
456 /* Build the \Phi-Matrix */
457 arity = get_irn_arity(sched_first(block));
458 phis = alloca(count * sizeof(*phis));
459 costs = alloca(count * sizeof(costs));
460 phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
461 candidates = bitset_alloca(count);
463 phi = sched_first(block);
464 for (row=0; row<count; ++row) {
466 for (col=0; col<arity; ++col) {
467 ir_node *arg = get_irn_n(phi, col);
468 /* Sort out all arguments interfering with its phi */
469 if (nodes_interfere(pi->co->chordal_env, phi, arg)) {
470 phi_matrix[row*arity + col] = NULL;
472 phi_matrix[row*arity + col] = arg;
474 phi = sched_next(phi);
477 /* Now find the interesting patterns in the matrix:
478 * All nodes which are used at least twice in a column. */
480 for (col=0; col<arity; ++col) {
481 done = pset_new_ptr_default();
482 for (row=0; row<count; ++row) {
483 irn = phi_matrix[row*arity + col];
485 * is this an interfering arg (NULL)
486 * or has the irn already been processed in this col?
488 if (!irn || pset_find_ptr(done, irn))
491 pset_insert_ptr(done, irn);
493 /* insert irn in candidates */
494 bitset_clear_all(candidates);
495 bitset_set(candidates, row);
496 /* search the irn in the rows below */
497 for (other_row = row+1; other_row<count; ++other_row)
498 if (irn == phi_matrix[other_row*arity + col]) {
499 /* found the irn in the same col in another row */
500 bitset_set(candidates, other_row);
503 /* now we know all occurences of irn in this col */
504 if (bitset_popcnt(candidates) < 2)
507 /* generate an unequation finally.
508 * phis are indexed in the bitset,
509 * shared argument is irn
510 * rhs is phi_count - 1 */
514 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
515 int minimal_unequal_count = bitset_popcnt(candidates)-1;
517 irnnr = get_irn_graph_nr(irn);
518 mangle_cst(buf, 'M', cst_counter++);
519 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_unequal_count);
522 bitset_foreach(candidates, pos) {
524 rootnr = get_irn_graph_nr(root);
525 mangle_var2(buf, 'y', rootnr, irnnr);
526 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
527 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
531 del_pset(done); /* clear set for next row */
536 * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
537 * in the same block, iff they use the same arg at the same pos.
538 * Only one of the phis can get the arg.
540 static void pi_add_clique_path_cstr(problem_instance_t *pi) {
541 DBG((dbg, LEVEL_2, "Adding clique path constraints...\n"));
542 dom_tree_walk_irg(get_irg(pi->co), clique_path_walker, NULL, pi);
545 #ifndef PATH_CONSTRAINTS_FOR_CLASSES
547 * Matrix P: Path contraints.
548 * If 2 nodes interfere and there is a path of equal-color-edges
549 * connecting them, then at least one of those equal-color-edges
550 * will break and cause some costs.
552 static void pi_add_path_cstr(problem_instance_t *pi) {
555 DBG((dbg, LEVEL_2, "Adding path constraints...\n"));
557 /* for all optimization units (only phis) */
558 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
561 if (curr->min_nodes_costs == 0)
564 rootnr = get_irn_graph_nr(curr->nodes[0]);
565 /* check all argument pairs for interference */
566 for (i=1; i<curr->node_count; ++i) {
567 const ir_node *arg1 = curr->nodes[i];
568 int arg1nr = get_irn_graph_nr(arg1);
569 for (o=i+1; o<curr->node_count; ++o) {
570 const ir_node *arg2 = curr->nodes[o];
571 int arg2nr = get_irn_graph_nr(arg2);
572 if (nodes_interfere(pi->co->chordal_env, arg1, arg2)) {
576 mangle_cst(buf, 'P', cst_counter++);
577 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
579 mangle_var2(buf, 'y', rootnr, arg1nr);
580 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
581 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
583 mangle_var2(buf, 'y', rootnr, arg2nr);
584 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
585 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
593 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
594 static INLINE int get_y_var_idx(problem_instance_t *pi, int nnr1, int nnr2) {
598 mangle_var2(buf, 'y', nnr1, nnr2);
599 if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
602 mangle_var2(buf, 'y', nnr2, nnr1);
603 if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
606 assert(0 && "One of them must work");
610 static void check_ecc_and_add_cut(problem_instance_t *pi, ir_node **path, int length, pset *remain, ir_node *tgt) {
611 if (path[length-1] == tgt) { /* we found a path */
612 int cst_idx, var_idx, i, nnr1, nnr2;
616 mangle_cst(buf, 'Q', pi->cst_counter++);
617 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
619 /* add all vars along the path */
620 nnr2 = get_irn_graph_nr(path[0]);
621 for (i=1; i<length; ++i) {
623 nnr2 = get_irn_graph_nr(path[i]);
624 var_idx = get_y_var_idx(pi, nnr1, nnr2);
625 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
627 } else { /* try to extend the path */
628 be_chordal_env_t *cenv = pi->co->chordal_env;
629 const ir_edge_t *edge;
630 ir_node *end = path[length-1];
631 ir_node **next = alloca(pset_count(remain) * sizeof(*next));
632 int i, o, max, next_pos = 0;
633 pset *done = pset_new_ptr_default();
635 /* find all potential next nodes on path */
638 for(i=0, max=get_irn_arity(end); i<max; ++i) {
639 ir_node *arg = get_irn_n(end, i);
640 if (!pset_find_ptr(done, arg) && pset_find_ptr(remain, arg)) {
641 next[next_pos++] = arg;
642 pset_insert_ptr(done, arg);
645 /* outs of phis and other nodes */
646 foreach_out_edge(end, edge) {
647 ir_node *user = edge->src;
648 if (is_Phi(user) && !pset_find_ptr(done, user) && pset_find_ptr(remain, user)) {
649 next[next_pos++] = user;
650 pset_insert_ptr(done, user);
656 /* delete all potential nodes with interferences to other nodes in the path */
657 for (i=0; i<next_pos; ++i) {
658 ir_node *nn = next[i];
660 /* if next is the tgt, it may interfere with path[0],
661 * so skip the first check */
662 o = (nn == tgt && length > 1) ? 1 : 0;
665 if (nodes_interfere(cenv, nn, path[o])) {
670 /* now we have all possible nodes in next; impossibles are NULL */
672 /* try to finish path with all possible nodes */
673 for (i=0; i<next_pos; ++i) {
674 if (!next[i]) /* this was an impossible node */
677 path[length] = next[i];
678 pset_remove_ptr(remain, next[i]);
679 check_ecc_and_add_cut(pi, path, length+1, remain, tgt);
680 pset_insert_ptr(remain, next[i]);
685 static void path_cstr_for_classes_walker(ir_node *irn, void *env) {
686 problem_instance_t *pi = env;
687 be_chordal_env_t *cenv;
690 pset *class = get_phi_class(irn);
691 if (!class || pset_find_ptr(pi->done, class))
694 pset_insert_ptr(pi->done, class);
697 max = pset_count(class);
698 cls = alloca(max * sizeof(*cls));
699 for(i=0, m = pset_first(class); m; i++, m = pset_next(class)) {
700 DBG((dbg, LEVEL_1, " class member: %+F\n", m));
704 cenv = pi->co->chordal_env;
705 for(i=0; i<max; ++i) {
706 ir_node **path = alloca(max * sizeof(*path));
707 pset *remain = pset_new_ptr(8);
708 pset_insert_pset_ptr(remain, class);
710 /* add cls[i] to path and remove it from remainder */
712 pset_remove_ptr(remain, cls[i]);
714 for(o=i+1; o<max; ++o)
715 if (nodes_interfere(cenv, cls[i], cls[o]))
716 check_ecc_and_add_cut(pi, path, 1, remain, cls[o]);
718 /* insert back into remainder */
719 pset_insert_ptr(remain, cls[i]);
725 * Matrix P: Path contraints.
726 * If 2 nodes interfere and there is a path of equal-color-edges
727 * connecting them, then at least one of those equal-color-edges
728 * will break and cause some costs.
730 static void pi_add_path_cstr_for_classes(problem_instance_t *pi) {
731 DBG((dbg, LEVEL_2, "Adding path constraints for phi classes...\n"));
733 pi->done = pset_new_ptr_default();
734 irg_walk_graph(get_irg(pi->co), path_cstr_for_classes_walker, NULL, pi);
739 #ifdef PRECOLOR_MAX_CLIQUE
741 problem_instance_t *pi;
745 #define has_reg_class(pi,irn) \
746 (arch_get_irn_reg_class(pi->co->chordal_env->session_env->main_env->arch_env, \
747 irn, -1) == pi->co->chordal_env->cls)
749 static void preColoringWalker(ir_node *bl, void *env) {
750 struct pre_col *e = env;
751 pset **clique = e->clique;
752 pset *max_clique = clique ? *clique : NULL;
753 int max = max_clique ? pset_count(max_clique) : 0;
754 problem_instance_t *pi = e->pi;
757 pset *live = pset_new_ptr_default();
761 /* as always, bring the live end nodes to life here */
762 live_foreach(bl, li) {
763 if(live_is_end(li) && has_reg_class(pi, li->irn)) {
764 pset_insert_ptr(live, irn);
768 sched_foreach_reverse(bl, irn) {
769 int pres = pset_count(live);
774 del_pset(max_clique);
776 max_clique = pset_new_ptr_default();
777 pset_insert_pset_ptr(max_clique, live);
782 if(has_reg_class(pi, irn))
783 pset_remove_ptr(live, irn);
785 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
786 ir_node *op = get_irn_n(irn, i);
787 if(has_reg_class(pi, op) && !is_Phi(irn))
788 pset_insert_ptr(live, op);
793 *clique = max_clique;
796 static void pi_add_constr_preColoring(problem_instance_t *pi) {
798 int cst_counter, color;
799 struct pre_col pre_col;
801 pre_col.clique = NULL;
804 dom_tree_walk_irg(get_irg(pi->co), preColoringWalker, NULL, &pre_col);
807 for (irn = pset_first(*pre_col.clique); irn; irn = pset_next(*pre_col.clique)) {
808 int cst_idx, var_idx, nnr = get_irn_graph_nr(irn);
811 mangle_cst(buf, 'K', cst_counter++);
812 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_equal, 1);
814 mangle_var2(buf, 'x', nnr, color++);
815 var_idx = lpp_get_var_idx(pi->curr_lp, buf);
816 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
822 * Generate the initial problem matrices and vectors.
824 static problem_instance_t *new_pi(const copy_opt_t *co) {
825 problem_instance_t *pi;
828 DBG((dbg, LEVEL_2, "Generating new instance...\n"));
829 pi = xcalloc(1, sizeof(*pi));
831 pi->removed = pset_new_ptr_default();
832 INIT_LIST_HEAD(&pi->simplicials);
833 pi->dilp = new_lpp(co->name, lpp_minimize);
835 /* problem size reduction */
836 pi_find_simplicials(pi);
837 if (pi->all_simplicial)
840 /* built objective and constraints */
841 pi->curr_lp = pi->dilp;
842 #ifdef NO_NULL_COLORS
843 pi_add_constr_no_null_colors(pi);
846 for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
847 pi_add_constr_B(pi, col);
850 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
851 pi_add_path_cstr_for_classes(pi);
853 pi_add_path_cstr(pi);
855 pi_add_clique_path_cstr(pi);
856 #ifdef PRECOLOR_MAX_CLIQUE
857 pi_add_constr_preColoring(pi);
864 * Clean the problem instance
866 static void free_pi(problem_instance_t *pi) {
867 simpl_t *simpl, *tmp;
869 DBG((dbg, LEVEL_2, "Free instance...\n"));
871 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
873 del_pset(pi->removed);
878 * Set starting values for the mip problem according
879 * to the current coloring of the graph.
881 static void pi_set_start_sol(problem_instance_t *pi) {
884 DBG((dbg, LEVEL_2, "Set start solution...\n"));
885 for (i=pi->first_x_var; i<=pi->last_x_var; ++i) {
888 /* get variable name */
889 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
890 /* split into components */
891 if (split_var(var_name, &nnr, &col) == 2) {
892 assert(get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) != -1);
893 val = (get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) == col) ? 1 : 0;
894 lpp_set_start_value(pi->curr_lp, i, val);
896 fprintf(stderr, "Variable name is: %s\n", var_name);
897 assert(0 && "x vars always look like this 'x123_45'");
905 static void pi_solve_ilp(problem_instance_t *pi) {
908 pi_set_start_sol(pi);
909 lower_bound = co_get_lower_bound(pi->co) - co_get_inevit_copy_costs(pi->co);
910 lpp_set_bound(pi->curr_lp, lower_bound);
911 lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
912 // lpp_solve_cplex(pi->curr_lp);
913 DBG((dbg, LEVEL_1, "Solution time: %.2f\n", pi->curr_lp->sol_time));
917 * Set the color of all simplicial nodes removed form
918 * the graph before transforming it to an ilp.
920 static void pi_set_simplicials(problem_instance_t *pi) {
921 simpl_t *simpl, *tmp;
922 be_ifg_t *ifg = pi->co->chordal_env->ifg;
923 bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
924 void *iter = be_ifg_iter_alloca(ifg);
926 DBG((dbg, LEVEL_2, "Set simplicials...\n"));
927 /* color the simplicial nodes in right order */
928 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
930 ir_node *other, *irn;
932 /* get free color by inspecting all neighbors */
934 bitset_clear_all(used_cols);
936 be_ifg_foreach_neighbour(ifg, iter, irn, other) {
937 if (!is_removed(other)) /* only inspect nodes which are in graph right now */
938 bitset_set(used_cols, get_irn_col(pi->co, other));
941 /* now all bits not set are possible colors */
942 free_col = bitset_next_clear(used_cols, 0);
943 assert(free_col != -1 && "No free color found. This can not be.");
944 set_irn_col(pi->co, irn, free_col);
945 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
950 * Sets the colors of irns according to the values of variables
951 * provided by the solution of the solver.
953 static int pi_apply_solution(problem_instance_t *pi) {
956 lpp_sol_state_t state;
957 DBG((dbg, LEVEL_2, "Applying solution...\n"));
960 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //now we have ms
961 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
962 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
963 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
966 sol = xmalloc((pi->last_x_var - pi->first_x_var + 1) * sizeof(*sol));
967 state = lpp_get_solution(pi->curr_lp, sol, pi->first_x_var, pi->last_x_var);
968 if (state != lpp_optimal) {
969 printf("WARNING %s: Solution state is not 'optimal': %d\n", pi->co->name, state);
970 assert(state >= lpp_feasible && "The solution should at least be feasible!");
973 for (i=0; i<pi->last_x_var - pi->first_x_var + 1; ++i) {
977 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
978 lpp_get_var_name(pi->curr_lp, pi->first_x_var+i, var_name, sizeof(var_name));
979 if (split_var(var_name, &nnr, &col) == 2) {
980 DBG((dbg, LEVEL_2, "Irn %n Idx %d Var %s Val %f\n", get_irn_for_graph_nr(get_irg(pi->co), nnr), i, var_name, sol[i]));
981 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
982 set_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr), col);
984 assert(0 && "This should be a x-var");
990 int co_ilp_opt(copy_opt_t *co, double time_limit) {
992 problem_instance_t *pi;
994 dbg = firm_dbg_register("ir.be.copyoptilp");
995 if (!strcmp(co->name, DEBUG_IRG))
996 firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_ILP);
998 firm_dbg_set_mask(dbg, DEBUG_LVL_ILP);
1001 if (!pi->all_simplicial) {
1004 snprintf(buf, sizeof(buf), "%s.mps", co->name);
1005 lpp_dump(pi->curr_lp, buf);
1007 lpp_set_time_limit(pi->curr_lp, time_limit);
1009 res = pi_apply_solution(pi);
1010 pi_set_simplicials(pi);