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_neighbours_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_neighbours_iter_alloca(ifg);
147 DBG((dbg, LEVEL_2, "Find simlicials...\n"));
150 arch_register_req_t req;
152 be_ifg_foreach_node(ifg, iter, irn) {
153 if (!is_removed(irn) && !is_optimizable(get_arch_env(pi->co), irn, &req) && !is_optimizable_arg(pi->co, irn)) {
154 if (pi_is_simplicial(pi, irn)) {
155 simpl_t *s = xmalloc(sizeof(*s));
157 list_add(&s->chain, &pi->simplicials);
158 pset_insert_ptr(pi->removed, irn);
160 DBG((dbg, LEVEL_2, " Removed %+F\n", irn));
166 /* TODO: Count inside the last look */
167 be_ifg_foreach_node(ifg, iter, irn) {
171 if (n_nodes == pset_count(pi->removed))
172 pi->all_simplicial = 1;
175 #ifdef NO_NULL_COLORS
176 static void pi_add_constr_no_null_colors(problem_instance_t *pi) {
177 int cst_counter=0, col, var_idx, cst_idx;
178 int n_colors = pi->co->chordal_env->cls->n_regs;
181 for (col = 0; col < n_colors; ++col) {
182 mangle_var1(buf, 'u', col);
183 #ifdef NO_NULL_COLORS_WITH_COSTS
184 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (double) (1 << (col+1)) );
186 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (2.0 * n_colors) );
188 if (!pi->first_nnc_var_idx)
189 pi->first_nnc_var_idx = var_idx;
192 #ifdef NO_NULL_COLORS_EXTRA_CSTS
193 for (col = 0; col < n_colors; ++col) {
194 mangle_cst(buf, 'U', cst_counter++);
195 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
196 if (!pi->first_nnc_cst_idx)
197 pi->first_nnc_cst_idx = cst_idx;
198 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col, -1);
202 #ifndef NO_NULL_COLORS_WITH_COSTS
203 for (col = 0; col < n_colors - 1; ++col) {
204 mangle_cst(buf, 'U', cst_counter++);
205 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
206 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col , 1);
207 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col+1, -1);
215 * Add coloring-force conditions
216 * Matrix A: knapsack constraint for each node
218 static void pi_add_constr_A(problem_instance_t *pi) {
221 DBG((dbg, LEVEL_2, "Add A constraints...\n"));
222 /* iterate over all blocks */
223 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
224 struct list_head *head = pme->value;
226 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
228 list_for_each_entry_reverse(border_t, curr, head, list)
229 if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
230 int cst_idx, nnr, col;
232 nnr = get_irn_graph_nr(curr->irn);
233 mangle_cst(pi->buf, 'A', nnr);
234 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
236 /* iterate over all possible colors in order */
237 bitset_clear_all(pos_regs);
238 arch_get_allocatable_regs(get_arch_env(pi->co), curr->irn, -1, pi->co->chordal_env->cls, pos_regs);
239 bitset_foreach(pos_regs, col) {
241 mangle_var2(pi->buf, 'x', nnr, col);
242 var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
243 if (!pi->first_x_var)
244 pi->first_x_var = var_idx;
245 pi->last_x_var = var_idx;
246 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
247 #ifdef NO_NULL_COLORS_EXTRA_CSTS
248 lpp_set_factor_fast(pi->curr_lp, pi->first_nnc_cst_idx+col, var_idx, 1);
256 * Checks if all nodes in @p living are live in in block @p block.
257 * @return 1 if all are live in
260 static INLINE int all_live_in(ir_node *block, pset *living) {
262 for (n = pset_first(living); n; n = pset_next(living))
263 if (!is_live_in(block, n)) {
271 * Finds cliques in the interference graph, considering only nodes
272 * for which the color @p color is possible. Finds only 'maximal-cliques',
273 * viz cliques which are not contained in another one.
274 * Matrix B: interference constraints using cliques
276 static void pi_add_constr_B(problem_instance_t *pi, int color) {
277 enum phase_t {growing, shrinking} phase = growing;
280 pset *living = pset_new_ptr(SLOTS_LIVING);
282 DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
283 /* iterate over all blocks */
284 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
285 ir_node *block = pme->key;
286 struct list_head *head = pme->value;
288 list_for_each_entry_reverse(border_t, b, head, list) {
289 const ir_node *irn = b->irn;
290 if (is_removed(irn) || !is_color_possible(irn, color))
294 DBG((dbg, LEVEL_2, "Def %n\n", irn));
295 pset_insert_ptr(living, irn);
297 } else { /* is_use */
298 DBG((dbg, LEVEL_2, "Use %n\n", irn));
300 /* before shrinking the set, store the current 'maximum' clique;
301 * do NOT if clique is a single node
302 * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
303 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
306 mangle_cst(pi->buf, 'B', pi->cst_counter);
307 #ifdef NO_NULL_COLORS
308 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 0);
310 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
312 for (n = pset_first(living); n; n = pset_next(living)) {
314 mangle_var_irn(pi->buf, 'x', n, color);
315 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
316 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
318 #ifdef NO_NULL_COLORS
319 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+color, -1.0);
323 pset_remove_ptr(living, irn);
328 assert(0 == pset_count(living));
333 * Generates constraints which interrelate x with y variables.
334 * x1 and x2 have the different colors ==> y_12 = 1
336 static void pi_add_constr_E(problem_instance_t *pi) {
338 bitset_t *root_regs, *arg_regs, *work_regs;
340 unsigned nregs = pi->co->chordal_env->cls->n_regs;
341 root_regs = bitset_alloca(nregs);
342 arg_regs = bitset_alloca(nregs);
343 work_regs = bitset_alloca(nregs);
345 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
346 /* for all roots of optimization units */
347 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
349 int rootnr, argnr, color;
353 root = curr->nodes[0];
354 rootnr = get_irn_graph_nr(root);
355 bitset_clear_all(root_regs);
356 arch_get_allocatable_regs(get_arch_env(pi->co), root, -1, pi->co->chordal_env->cls, root_regs);
358 /* for all arguments of root */
359 for (i = 1; i < curr->node_count; ++i) {
360 arg = curr->nodes[i];
361 argnr = get_irn_graph_nr(arg);
362 bitset_clear_all(arg_regs);
363 arch_get_allocatable_regs(get_arch_env(pi->co), arg, -1, pi->co->chordal_env->cls, arg_regs);
365 /* Introduce new variable and set factor in objective function */
366 mangle_var2(buf, 'y', rootnr, argnr);
367 y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
369 /* set starting value */
370 lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
372 /* For all colors root and arg have in common, add 2 constraints to E */
373 bitset_copy(work_regs, root_regs);
374 bitset_and(work_regs, arg_regs);
375 bitset_foreach(work_regs, color) {
376 int root_idx, arg_idx, cst_idx;
377 mangle_var2(buf, 'x', rootnr, color);
378 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
379 mangle_var2(buf, 'x', argnr, color);
380 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
382 /* add root-arg-y <= 0 */
383 mangle_cst(buf, 'E', cst_counter++);
384 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
385 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
386 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
387 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
389 /* add arg-root-y <= 0 */
390 mangle_cst(buf, 'E', cst_counter++);
391 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
392 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
393 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
394 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
396 /* For all colors root and arg have "disjunct", add 1 constraints to E.
397 * If root gets a color the arg is not possible to get then they will
398 * definetly get different colors. So y has to be 1.
399 * Vice versa for arg.
401 bitset_copy(work_regs, root_regs);
402 bitset_xor(work_regs, arg_regs);
403 bitset_foreach(work_regs, color) {
404 int root_idx, arg_idx, cst_idx;
405 mangle_var2(buf, 'x', rootnr, color);
406 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
407 mangle_var2(buf, 'x', argnr, color);
408 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
410 mangle_cst(buf, 'E', cst_counter++);
411 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
412 if (bitset_is_set(root_regs, color)) {
413 /* add root-y <= 0 */
414 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
415 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
417 assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
419 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
420 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
427 static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
430 /* search optimization unit for phi */
431 list_for_each_entry(unit_t, curr, &pi->co->units, units)
432 if (curr->nodes[0] == phi) {
433 for (i=1; i<curr->node_count; ++i)
434 if (curr->nodes[i] == irn)
435 return curr->costs[i];
436 assert(0 && "irn must occur in this ou");
438 assert(0 && "phi must be found in a ou");
442 static void clique_path_walker(ir_node *block, void *env) {
443 problem_instance_t *pi = env;
444 int count, arity, row, col, other_row, *costs;
445 ir_node **phis, *phi, *irn, **phi_matrix;
447 bitset_t *candidates;
449 /* Count all phi nodes of this block */
450 for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
453 /* We at least 2 phi nodes for this class of inequalities */
457 /* Build the \Phi-Matrix */
458 arity = get_irn_arity(sched_first(block));
459 phis = alloca(count * sizeof(*phis));
460 costs = alloca(count * sizeof(costs));
461 phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
462 candidates = bitset_alloca(count);
464 phi = sched_first(block);
465 for (row=0; row<count; ++row) {
467 for (col=0; col<arity; ++col) {
468 ir_node *arg = get_irn_n(phi, col);
469 /* Sort out all arguments interfering with its phi */
470 if (nodes_interfere(pi->co->chordal_env, phi, arg)) {
471 phi_matrix[row*arity + col] = NULL;
473 phi_matrix[row*arity + col] = arg;
475 phi = sched_next(phi);
478 /* Now find the interesting patterns in the matrix:
479 * All nodes which are used at least twice in a column. */
481 for (col=0; col<arity; ++col) {
482 done = pset_new_ptr_default();
483 for (row=0; row<count; ++row) {
484 irn = phi_matrix[row*arity + col];
486 * is this an interfering arg (NULL)
487 * or has the irn already been processed in this col?
489 if (!irn || pset_find_ptr(done, irn))
492 pset_insert_ptr(done, irn);
494 /* insert irn in candidates */
495 bitset_clear_all(candidates);
496 bitset_set(candidates, row);
497 /* search the irn in the rows below */
498 for (other_row = row+1; other_row<count; ++other_row)
499 if (irn == phi_matrix[other_row*arity + col]) {
500 /* found the irn in the same col in another row */
501 bitset_set(candidates, other_row);
504 /* now we know all occurences of irn in this col */
505 if (bitset_popcnt(candidates) < 2)
508 /* generate an unequation finally.
509 * phis are indexed in the bitset,
510 * shared argument is irn
511 * rhs is phi_count - 1 */
515 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
516 int minimal_unequal_count = bitset_popcnt(candidates)-1;
518 irnnr = get_irn_graph_nr(irn);
519 mangle_cst(buf, 'M', cst_counter++);
520 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_unequal_count);
523 bitset_foreach(candidates, pos) {
525 rootnr = get_irn_graph_nr(root);
526 mangle_var2(buf, 'y', rootnr, irnnr);
527 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
528 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
532 del_pset(done); /* clear set for next row */
537 * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
538 * in the same block, iff they use the same arg at the same pos.
539 * Only one of the phis can get the arg.
541 static void pi_add_clique_path_cstr(problem_instance_t *pi) {
542 DBG((dbg, LEVEL_2, "Adding clique path constraints...\n"));
543 dom_tree_walk_irg(get_irg(pi->co), clique_path_walker, NULL, pi);
546 #ifndef PATH_CONSTRAINTS_FOR_CLASSES
548 * Matrix P: Path contraints.
549 * If 2 nodes interfere and there is a path of equal-color-edges
550 * connecting them, then at least one of those equal-color-edges
551 * will break and cause some costs.
553 static void pi_add_path_cstr(problem_instance_t *pi) {
556 DBG((dbg, LEVEL_2, "Adding path constraints...\n"));
558 /* for all optimization units (only phis) */
559 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
562 if (curr->min_nodes_costs == 0)
565 rootnr = get_irn_graph_nr(curr->nodes[0]);
566 /* check all argument pairs for interference */
567 for (i=1; i<curr->node_count; ++i) {
568 const ir_node *arg1 = curr->nodes[i];
569 int arg1nr = get_irn_graph_nr(arg1);
570 for (o=i+1; o<curr->node_count; ++o) {
571 const ir_node *arg2 = curr->nodes[o];
572 int arg2nr = get_irn_graph_nr(arg2);
573 if (nodes_interfere(pi->co->chordal_env, arg1, arg2)) {
577 mangle_cst(buf, 'P', cst_counter++);
578 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
580 mangle_var2(buf, 'y', rootnr, arg1nr);
581 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
582 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
584 mangle_var2(buf, 'y', rootnr, arg2nr);
585 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
586 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
594 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
595 static INLINE int get_y_var_idx(problem_instance_t *pi, int nnr1, int nnr2) {
599 mangle_var2(buf, 'y', nnr1, nnr2);
600 if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
603 mangle_var2(buf, 'y', nnr2, nnr1);
604 if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
607 assert(0 && "One of them must work");
611 static void check_ecc_and_add_cut(problem_instance_t *pi, ir_node **path, int length, pset *remain, ir_node *tgt) {
612 if (path[length-1] == tgt) { /* we found a path */
613 int cst_idx, var_idx, i, nnr1, nnr2;
617 mangle_cst(buf, 'Q', pi->cst_counter++);
618 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
620 /* add all vars along the path */
621 nnr2 = get_irn_graph_nr(path[0]);
622 for (i=1; i<length; ++i) {
624 nnr2 = get_irn_graph_nr(path[i]);
625 var_idx = get_y_var_idx(pi, nnr1, nnr2);
626 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
628 } else { /* try to extend the path */
629 be_chordal_env_t *cenv = pi->co->chordal_env;
630 const ir_edge_t *edge;
631 ir_node *end = path[length-1];
632 ir_node **next = alloca(pset_count(remain) * sizeof(*next));
633 int i, o, max, next_pos = 0;
634 pset *done = pset_new_ptr_default();
636 /* find all potential next nodes on path */
639 for(i=0, max=get_irn_arity(end); i<max; ++i) {
640 ir_node *arg = get_irn_n(end, i);
641 if (!pset_find_ptr(done, arg) && pset_find_ptr(remain, arg)) {
642 next[next_pos++] = arg;
643 pset_insert_ptr(done, arg);
646 /* outs of phis and other nodes */
647 foreach_out_edge(end, edge) {
648 ir_node *user = edge->src;
649 if (is_Phi(user) && !pset_find_ptr(done, user) && pset_find_ptr(remain, user)) {
650 next[next_pos++] = user;
651 pset_insert_ptr(done, user);
657 /* delete all potential nodes with interferences to other nodes in the path */
658 for (i=0; i<next_pos; ++i) {
659 ir_node *nn = next[i];
661 /* if next is the tgt, it may interfere with path[0],
662 * so skip the first check */
663 o = (nn == tgt && length > 1) ? 1 : 0;
666 if (nodes_interfere(cenv, nn, path[o])) {
671 /* now we have all possible nodes in next; impossibles are NULL */
673 /* try to finish path with all possible nodes */
674 for (i=0; i<next_pos; ++i) {
675 if (!next[i]) /* this was an impossible node */
678 path[length] = next[i];
679 pset_remove_ptr(remain, next[i]);
680 check_ecc_and_add_cut(pi, path, length+1, remain, tgt);
681 pset_insert_ptr(remain, next[i]);
686 static void path_cstr_for_classes_walker(ir_node *irn, void *env) {
687 problem_instance_t *pi = env;
688 be_chordal_env_t *cenv;
691 pset *class = get_phi_class(irn);
692 if (!class || pset_find_ptr(pi->done, class))
695 pset_insert_ptr(pi->done, class);
698 max = pset_count(class);
699 cls = alloca(max * sizeof(*cls));
700 for(i=0, m = pset_first(class); m; i++, m = pset_next(class)) {
701 DBG((dbg, LEVEL_1, " class member: %+F\n", m));
705 cenv = pi->co->chordal_env;
706 for(i=0; i<max; ++i) {
707 ir_node **path = alloca(max * sizeof(*path));
708 pset *remain = pset_new_ptr(8);
709 pset_insert_pset_ptr(remain, class);
711 /* add cls[i] to path and remove it from remainder */
713 pset_remove_ptr(remain, cls[i]);
715 for(o=i+1; o<max; ++o)
716 if (nodes_interfere(cenv, cls[i], cls[o]))
717 check_ecc_and_add_cut(pi, path, 1, remain, cls[o]);
719 /* insert back into remainder */
720 pset_insert_ptr(remain, cls[i]);
726 * Matrix P: Path contraints.
727 * If 2 nodes interfere and there is a path of equal-color-edges
728 * connecting them, then at least one of those equal-color-edges
729 * will break and cause some costs.
731 static void pi_add_path_cstr_for_classes(problem_instance_t *pi) {
732 DBG((dbg, LEVEL_2, "Adding path constraints for phi classes...\n"));
734 pi->done = pset_new_ptr_default();
735 irg_walk_graph(get_irg(pi->co), path_cstr_for_classes_walker, NULL, pi);
740 #ifdef PRECOLOR_MAX_CLIQUE
742 problem_instance_t *pi;
746 #define has_reg_class(pi,irn) \
747 (arch_get_irn_reg_class(pi->co->chordal_env->session_env->main_env->arch_env, \
748 irn, -1) == pi->co->chordal_env->cls)
750 static void preColoringWalker(ir_node *bl, void *env) {
751 struct pre_col *e = env;
752 pset **clique = e->clique;
753 pset *max_clique = clique ? *clique : NULL;
754 int max = max_clique ? pset_count(max_clique) : 0;
755 problem_instance_t *pi = e->pi;
758 pset *live = pset_new_ptr_default();
762 /* as always, bring the live end nodes to life here */
763 live_foreach(bl, li) {
764 if(live_is_end(li) && has_reg_class(pi, li->irn)) {
765 pset_insert_ptr(live, irn);
769 sched_foreach_reverse(bl, irn) {
770 int pres = pset_count(live);
775 del_pset(max_clique);
777 max_clique = pset_new_ptr_default();
778 pset_insert_pset_ptr(max_clique, live);
783 if(has_reg_class(pi, irn))
784 pset_remove_ptr(live, irn);
786 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
787 ir_node *op = get_irn_n(irn, i);
788 if(has_reg_class(pi, op) && !is_Phi(irn))
789 pset_insert_ptr(live, op);
794 *clique = max_clique;
797 static void pi_add_constr_preColoring(problem_instance_t *pi) {
799 int cst_counter, color;
800 struct pre_col pre_col;
802 pre_col.clique = NULL;
805 dom_tree_walk_irg(get_irg(pi->co), preColoringWalker, NULL, &pre_col);
808 for (irn = pset_first(*pre_col.clique); irn; irn = pset_next(*pre_col.clique)) {
809 int cst_idx, var_idx, nnr = get_irn_graph_nr(irn);
812 mangle_cst(buf, 'K', cst_counter++);
813 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_equal, 1);
815 mangle_var2(buf, 'x', nnr, color++);
816 var_idx = lpp_get_var_idx(pi->curr_lp, buf);
817 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
823 * Generate the initial problem matrices and vectors.
825 static problem_instance_t *new_pi(const copy_opt_t *co) {
826 problem_instance_t *pi;
829 DBG((dbg, LEVEL_2, "Generating new instance...\n"));
830 pi = xcalloc(1, sizeof(*pi));
832 pi->removed = pset_new_ptr_default();
833 INIT_LIST_HEAD(&pi->simplicials);
834 pi->dilp = new_lpp(co->name, lpp_minimize);
836 /* problem size reduction */
837 pi_find_simplicials(pi);
838 if (pi->all_simplicial)
841 /* built objective and constraints */
842 pi->curr_lp = pi->dilp;
843 #ifdef NO_NULL_COLORS
844 pi_add_constr_no_null_colors(pi);
847 for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
848 pi_add_constr_B(pi, col);
851 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
852 pi_add_path_cstr_for_classes(pi);
854 pi_add_path_cstr(pi);
856 pi_add_clique_path_cstr(pi);
857 #ifdef PRECOLOR_MAX_CLIQUE
858 pi_add_constr_preColoring(pi);
865 * Clean the problem instance
867 static void free_pi(problem_instance_t *pi) {
868 simpl_t *simpl, *tmp;
870 DBG((dbg, LEVEL_2, "Free instance...\n"));
872 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
874 del_pset(pi->removed);
879 * Set starting values for the mip problem according
880 * to the current coloring of the graph.
882 static void pi_set_start_sol(problem_instance_t *pi) {
885 DBG((dbg, LEVEL_2, "Set start solution...\n"));
886 for (i=pi->first_x_var; i<=pi->last_x_var; ++i) {
889 /* get variable name */
890 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
891 /* split into components */
892 if (split_var(var_name, &nnr, &col) == 2) {
893 assert(get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) != -1);
894 val = (get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) == col) ? 1 : 0;
895 lpp_set_start_value(pi->curr_lp, i, val);
897 fprintf(stderr, "Variable name is: %s\n", var_name);
898 assert(0 && "x vars always look like this 'x123_45'");
906 static void pi_solve_ilp(problem_instance_t *pi) {
909 pi_set_start_sol(pi);
910 lower_bound = co_get_lower_bound(pi->co) - co_get_inevit_copy_costs(pi->co);
911 lpp_set_bound(pi->curr_lp, lower_bound);
912 lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
913 // lpp_solve_cplex(pi->curr_lp);
914 DBG((dbg, LEVEL_1, "Solution time: %.2f\n", pi->curr_lp->sol_time));
918 * Set the color of all simplicial nodes removed form
919 * the graph before transforming it to an ilp.
921 static void pi_set_simplicials(problem_instance_t *pi) {
922 simpl_t *simpl, *tmp;
923 be_ifg_t *ifg = pi->co->chordal_env->ifg;
924 bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
925 void *iter = be_ifg_neighbours_iter_alloca(ifg);
927 DBG((dbg, LEVEL_2, "Set simplicials...\n"));
928 /* color the simplicial nodes in right order */
929 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
931 ir_node *other, *irn;
933 /* get free color by inspecting all neighbors */
935 bitset_clear_all(used_cols);
937 be_ifg_foreach_neighbour(ifg, iter, irn, other) {
938 if (!is_removed(other)) /* only inspect nodes which are in graph right now */
939 bitset_set(used_cols, get_irn_col(pi->co, other));
942 /* now all bits not set are possible colors */
943 free_col = bitset_next_clear(used_cols, 0);
944 assert(free_col != -1 && "No free color found. This can not be.");
945 set_irn_col(pi->co, irn, free_col);
946 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
951 * Sets the colors of irns according to the values of variables
952 * provided by the solution of the solver.
954 static int pi_apply_solution(problem_instance_t *pi) {
957 lpp_sol_state_t state;
958 DBG((dbg, LEVEL_2, "Applying solution...\n"));
961 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //now we have ms
962 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
963 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
964 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
967 sol = xmalloc((pi->last_x_var - pi->first_x_var + 1) * sizeof(*sol));
968 state = lpp_get_solution(pi->curr_lp, sol, pi->first_x_var, pi->last_x_var);
969 if (state != lpp_optimal) {
970 printf("WARNING %s: Solution state is not 'optimal': %d\n", pi->co->name, state);
971 assert(state >= lpp_feasible && "The solution should at least be feasible!");
974 for (i=0; i<pi->last_x_var - pi->first_x_var + 1; ++i) {
978 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
979 lpp_get_var_name(pi->curr_lp, pi->first_x_var+i, var_name, sizeof(var_name));
980 if (split_var(var_name, &nnr, &col) == 2) {
981 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]));
982 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
983 set_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr), col);
985 assert(0 && "This should be a x-var");
991 int co_ilp_opt(copy_opt_t *co, double time_limit) {
993 problem_instance_t *pi;
995 dbg = firm_dbg_register("ir.be.copyoptilp");
998 if (!pi->all_simplicial) {
1001 snprintf(buf, sizeof(buf), "%s.mps", co->name);
1002 lpp_dump(pi->curr_lp, buf);
1004 lpp_set_time_limit(pi->curr_lp, time_limit);
1006 res = pi_apply_solution(pi);
1007 pi_set_simplicials(pi);