4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include <lpp/lpp_net.h>
22 //#include <lpp/lpp_cplex.h>
24 #include "becopyopt.h"
25 #include "becopystat.h"
26 #include "besched_t.h"
28 #define LPP_HOST "i44pc52"
29 #define LPP_SOLVER "cplex"
32 static firm_dbg_module_t *dbg = NULL;
34 #define MAX(a,b) ((a<b)?(b):(a))
35 #define MIN(a,b) ((a<b)?(a):(b))
36 #define EPSILON 0.00001
37 #define SLOTS_LIVING 32
39 typedef struct _simpl_t {
40 struct list_head chain;
44 typedef struct _problem_instance_t {
45 const copy_opt_t *co; /** the copy_opt problem */
46 /* problem size reduction removing simple nodes */
47 struct list_head simplicials; /**< holds all simpl_t's in right order to color*/
48 pset *removed; /**< holds all removed simplicial irns */
50 lpp_t *dilp, *ilp_new, *ilp_newr; /**< problem formulation directly as milp */
52 lpp_t *curr_lp; /**< points to the problem currently used */
53 int cst_counter, last_x_var;
58 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
60 #define is_color_possible(irn,color) arch_reg_is_allocatable(get_arch_env(pi->co), irn, arch_pos_make_out(0), arch_register_for_index(pi->co->chordal_env->cls, color))
63 * Some stuff for variable name handling.
65 #define mangle_cst(buf, prefix, nr) \
66 snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
68 #define mangle_var(buf, prefix, node_nr, color) \
69 snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
71 #define mangle_var3(buf, prefix, n1, n2, col) \
72 snprintf((buf), sizeof(buf), "%c%d_%d_%d", (prefix), (n1), (n2), (col))
74 #define mangle_var_irn(buf, prefix, irn, color) \
75 mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
77 #define split_var(var, nnr, col) \
78 sscanf(var, "x%d_%d", (nnr), (col))
82 * Checks if a node is simplicial in the graph
83 * heeding the already removed nodes.
85 static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
87 if_node_t **all, *curr;
88 all = alloca(ifn_get_degree(ifn) * sizeof(*all));
90 /* get all non-removed neighbors */
91 foreach_neighb(ifn, curr)
92 if (!is_removed(curr))
95 /* check if these form a clique */
96 for (i=0; i<size; ++i)
97 for (o=i+1; o<size; ++o)
98 if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
101 /* all edges exist so this is a clique */
106 * Iterative finds and 'removes' from the graph all nodes which are
107 * simplicial AND not member of a equal-color-wish
109 static void pi_find_simplicials(problem_instance_t *pi) {
114 DBG((dbg, LEVEL_2, "Find simlicials...\n"));
116 if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
119 for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
120 ir_node *irn = get_irn_for_graph_nr(get_irg(pi->co), ifn->nnr);
121 if (!is_removed(irn) && !is_optimizable(get_arch_env(pi->co), irn) && !is_optimizable_arg(pi->co, irn)) {
122 if (pi_is_simplicial(pi, ifn)) {
123 simpl_t *s = xmalloc(sizeof(*s));
125 list_add(&s->chain, &pi->simplicials);
126 pset_insert_ptr(pi->removed, irn);
128 DBG((dbg, LEVEL_2, " Removed %n %d\n", irn, get_irn_graph_nr(irn)));
133 if (set_count(be_ra_get_ifg_nodes(pi->co->chordal_env)) == pset_count(pi->removed))
134 pi->all_simplicial = 1;
138 * Add coloring-force conditions
139 * Matrix A: knapsack constraint for each node
141 static void pi_add_constr_A(problem_instance_t *pi) {
144 DBG((dbg, LEVEL_2, "Add A constraints...\n"));
145 /* iterate over all blocks */
146 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
147 struct list_head *head = pme->value;
149 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
151 list_for_each_entry_reverse(border_t, curr, head, list)
152 if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
153 int cst_idx, nnr, col;
155 nnr = get_irn_graph_nr(curr->irn);
156 mangle_cst(pi->buf, 'A', nnr);
157 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
159 /* iterate over all possible colors in order */
160 bitset_clear_all(pos_regs);
161 arch_get_allocatable_regs(get_arch_env(pi->co), curr->irn, arch_pos_make_out(0), pi->co->chordal_env->cls, pos_regs);
162 bitset_foreach(pos_regs, col) {
164 mangle_var(pi->buf, 'x', nnr, col);
165 var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
166 pi->last_x_var = var_idx;
167 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
174 * Checks if all nodes in @p living are live in in block @p block.
175 * @return 1 if all are live in
178 static INLINE int all_live_in(ir_node *block, pset *living) {
180 for (n = pset_first(living); n; n = pset_next(living))
181 if (!is_live_in(block, n)) {
189 * Finds cliques in the interference graph, considering only nodes
190 * for which the color @p color is possible. Finds only 'maximal-cliques',
191 * viz cliques which are not contained in another one.
192 * Matrix B: interference constraints using cliques
194 static void pi_add_constr_B(problem_instance_t *pi, int color) {
195 enum phase_t {growing, shrinking} phase = growing;
198 pset *living = pset_new_ptr(SLOTS_LIVING);
200 DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
201 /* iterate over all blocks */
202 pmap_foreach(pi->co->chordal_env->border_heads, pme) {
203 ir_node *block = pme->key;
204 struct list_head *head = pme->value;
206 list_for_each_entry_reverse(border_t, b, head, list) {
207 const ir_node *irn = b->irn;
208 if (is_removed(irn) || !is_color_possible(irn, color))
212 DBG((dbg, LEVEL_2, "Def %n\n", irn));
213 pset_insert_ptr(living, irn);
215 } else { /* is_use */
216 DBG((dbg, LEVEL_2, "Use %n\n", irn));
218 /* before shrinking the set, store the current 'maximum' clique;
219 * do NOT if clique is a single node
220 * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
221 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
224 mangle_cst(pi->buf, 'B', pi->cst_counter);
225 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
226 for (n = pset_first(living); n; n = pset_next(living)) {
228 mangle_var_irn(pi->buf, 'x', n, color);
229 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
230 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
234 pset_remove_ptr(living, irn);
239 assert(0 == pset_count(living));
243 * Generates constraints which interrelate x with y variables.
244 * x1 and x2 have the different colors ==> y_12 = 1
246 static void pi_add_constr_E_new_reverse(problem_instance_t *pi) {
248 bitset_t *root_regs, *arg_regs, *work_regs;
250 unsigned nregs = pi->co->chordal_env->cls->n_regs;
251 root_regs = bitset_alloca(nregs);
252 arg_regs = bitset_alloca(nregs);
253 work_regs = bitset_alloca(nregs);
255 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
256 /* for all roots of optimization units */
257 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
259 int rootnr, argnr, color;
263 root = curr->nodes[0];
264 rootnr = get_irn_graph_nr(root);
265 bitset_clear_all(root_regs);
266 arch_get_allocatable_regs(get_arch_env(pi->co), root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
268 /* for all arguments of root */
269 for (i = 1; i < curr->node_count; ++i) {
272 arg = curr->nodes[i];
273 argnr = get_irn_graph_nr(arg);
274 bitset_clear_all(arg_regs);
275 arch_get_allocatable_regs(get_arch_env(pi->co), arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
277 /* For all colors root and arg have in common, add 2 constraints to E */
278 bitset_copy(work_regs, root_regs);
279 bitset_and(work_regs, arg_regs);
281 /* y_ij1 + y_ij2 + .... + y_ijk <= 1 */
282 mangle_cst(buf, 'Z', cst_counter++);
283 extra_cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, bitset_popcnt(work_regs)-1);
285 bitset_foreach(work_regs, color) {
286 int root_idx, arg_idx, cst_idx;
288 /* Introduce new variable and set factor in objective function */
289 mangle_var3(buf, 'y', rootnr, argnr, color);
290 y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
292 /* add new y var to the extra Z constraint */
293 lpp_set_factor_fast(pi->curr_lp, extra_cst_idx, y_idx, 1);
295 /* set starting value */
296 lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root)==color && get_irn_col(pi->co, arg)==color) );
298 mangle_var(buf, 'x', rootnr, color);
299 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
300 mangle_var(buf, 'x', argnr, color);
301 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
303 /* add root+y >= 1 */
304 mangle_cst(buf, 'E', cst_counter++);
305 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
306 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
307 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
310 mangle_cst(buf, 'E', cst_counter++);
311 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
312 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
313 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
320 * Generates constraints which interrelate x with y variables.
321 * x1 and x2 have the different colors ==> y_12 = 1
323 static void pi_add_constr_E_new(problem_instance_t *pi) {
325 bitset_t *root_regs, *arg_regs, *work_regs;
327 unsigned nregs = pi->co->chordal_env->cls->n_regs;
328 root_regs = bitset_alloca(nregs);
329 arg_regs = bitset_alloca(nregs);
330 work_regs = bitset_alloca(nregs);
332 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
333 /* for all roots of optimization units */
334 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
336 int rootnr, argnr, color;
340 root = curr->nodes[0];
341 rootnr = get_irn_graph_nr(root);
342 bitset_clear_all(root_regs);
343 arch_get_allocatable_regs(get_arch_env(pi->co), root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
345 /* for all arguments of root */
346 for (i = 1; i < curr->node_count; ++i) {
349 arg = curr->nodes[i];
350 argnr = get_irn_graph_nr(arg);
351 bitset_clear_all(arg_regs);
352 arch_get_allocatable_regs(get_arch_env(pi->co), arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
354 /* y_ij1 + y_ij2 + .... + y_ijk <= 1 */
355 mangle_cst(buf, 'Z', cst_counter++);
356 extra_cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 1);
358 /* For all colors root and arg have in common, add 2 constraints to E */
359 bitset_copy(work_regs, root_regs);
360 bitset_and(work_regs, arg_regs);
361 bitset_foreach(work_regs, color) {
362 int root_idx, arg_idx, cst_idx;
364 /* Introduce new variable and set factor in objective function */
365 mangle_var3(buf, 'y', rootnr, argnr, color);
366 y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, -curr->costs[i]);
368 /* add new y var to the extra Z constraint */
369 lpp_set_factor_fast(pi->curr_lp, extra_cst_idx, y_idx, 1);
371 /* set starting value */
372 lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root)==color && get_irn_col(pi->co, arg)==color) );
374 mangle_var(buf, 'x', rootnr, color);
375 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
376 mangle_var(buf, 'x', argnr, color);
377 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
379 /* add root+arg-2y >= 0 */
380 mangle_cst(buf, 'E', cst_counter++);
381 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
382 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
383 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
384 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -2);
391 * Generates constraints which interrelate x with y variables.
392 * x1 and x2 have the different colors ==> y_12 = 1
394 static void pi_add_constr_E_old(problem_instance_t *pi) {
396 bitset_t *root_regs, *arg_regs, *work_regs;
398 unsigned nregs = pi->co->chordal_env->cls->n_regs;
399 root_regs = bitset_alloca(nregs);
400 arg_regs = bitset_alloca(nregs);
401 work_regs = bitset_alloca(nregs);
403 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
404 /* for all roots of optimization units */
405 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
407 int rootnr, argnr, color;
411 root = curr->nodes[0];
412 rootnr = get_irn_graph_nr(root);
413 bitset_clear_all(root_regs);
414 arch_get_allocatable_regs(get_arch_env(pi->co), root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
416 /* for all arguments of root */
417 for (i = 1; i < curr->node_count; ++i) {
418 arg = curr->nodes[i];
419 argnr = get_irn_graph_nr(arg);
420 bitset_clear_all(arg_regs);
421 arch_get_allocatable_regs(get_arch_env(pi->co), arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
423 /* Introduce new variable and set factor in objective function */
424 mangle_var(buf, 'y', rootnr, argnr);
425 y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
427 /* set starting value */
428 lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
430 /* For all colors root and arg have in common, add 2 constraints to E */
431 bitset_copy(work_regs, root_regs);
432 bitset_and(work_regs, arg_regs);
433 bitset_foreach(work_regs, color) {
434 int root_idx, arg_idx, cst_idx;
435 mangle_var(buf, 'x', rootnr, color);
436 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
437 mangle_var(buf, 'x', argnr, color);
438 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
440 /* add root-arg-y <= 0 */
441 mangle_cst(buf, 'E', cst_counter++);
442 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
443 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
444 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
445 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
447 /* add arg-root-y <= 0 */
448 mangle_cst(buf, 'E', cst_counter++);
449 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
450 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
451 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
452 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
454 /* For all colors root and arg have "disjunct", add 1 constraints to E.
455 * If root gets a color the arg is not possible to get then they will
456 * definetly get different colors. So y has to be 1.
457 * Vice versa for arg.
459 bitset_copy(work_regs, root_regs);
460 bitset_xor(work_regs, arg_regs);
461 bitset_foreach(work_regs, color) {
462 int root_idx, arg_idx, cst_idx;
463 mangle_var(buf, 'x', rootnr, color);
464 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
465 mangle_var(buf, 'x', argnr, color);
466 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
468 mangle_cst(buf, 'E', cst_counter++);
469 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
470 if (bitset_is_set(root_regs, color)) {
471 /* add root-y <= 0 */
472 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
473 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
475 assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
477 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
478 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
485 static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
488 /* search optimization unit for phi */
489 list_for_each_entry(unit_t, curr, &pi->co->units, units)
490 if (curr->nodes[0] == phi) {
491 for (i=1; i<curr->node_count; ++i)
492 if (curr->nodes[i] == irn)
493 return curr->costs[i];
494 assert(0 && "irn must occur in this ou");
496 assert(0 && "phi must be found in a ou");
500 static void M_constr_walker(ir_node *block, void *env) {
501 problem_instance_t *pi = env;
502 int count, arity, row, col, other_row, *costs;
503 ir_node **phis, *phi, *irn, **phi_matrix;
505 bitset_t *candidates;
507 /* Count all phi nodes of this block */
508 for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
511 /* We at least 2 phi nodes for this class of inequalities */
515 /* Build the \Phi-Matrix */
516 arity = get_irn_arity(sched_first(block));
517 phis = alloca(count * sizeof(*phis));
518 costs = alloca(count * sizeof(costs));
519 phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
520 candidates = bitset_alloca(count);
522 phi = sched_first(block);
523 for (row=0; row<count; ++row) {
525 for (col=0; col<arity; ++col) {
526 ir_node *arg = get_irn_n(phi, col);
527 /* Sort out all arguments interfering with its phi */
528 if (nodes_interfere(pi->co->chordal_env, phi, arg)) {
529 phi_matrix[row*arity + col] = NULL;
531 phi_matrix[row*arity + col] = arg;
533 phi = sched_next(phi);
536 /* Now find the interesting patterns in the matrix:
537 * All nodes which are used at least twice in a column. */
539 for (col=0; col<arity; ++col) {
540 done = pset_new_ptr_default();
541 for (row=0; row<count; ++row) {
542 irn = phi_matrix[row*arity + col];
544 * is this an interfering arg (NULL)
545 * or has the irn already been processed in this col?
547 if (!irn || pset_find_ptr(done, irn))
550 pset_insert_ptr(done, irn);
552 /* insert irn in candidates */
553 bitset_clear_all(candidates);
554 bitset_set(candidates, row);
555 /* search the irn in the rows below */
556 for (other_row = row+1; other_row<count; ++other_row)
557 if (irn == phi_matrix[other_row*arity + col]) {
558 /* found the irn in the same col in another row */
559 bitset_set(candidates, other_row);
562 /* now we know all occurences of irn in this col */
563 if (bitset_popcnt(candidates) < 2)
566 /* compute the minimal costs (rhs) */
567 int phi_nr, sum=0, max=-1, minimal_costs;
569 bitset_foreach(candidates, phi_nr) {
570 costs[phi_nr] = get_costs(pi, phis[phi_nr], irn);
571 assert_costs = costs[phi_nr];
572 sum += costs[phi_nr];
573 max = MAX(max, costs[phi_nr]);
575 minimal_costs = sum - max;
578 if (assert_costs != -1) {
579 bitset_foreach(candidates, phi_nr) {
580 /* possible, if argument of a OU are joined */
581 if (costs[phi_nr] != assert_costs)
586 /* generate an unequation finally.
587 * phis are indexed in the bitset,
588 * shared argument is irn
589 * rhs is minimal_costs */
593 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
595 irnnr = get_irn_graph_nr(irn);
596 mangle_cst(buf, 'M', cst_counter++);
597 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_costs);
600 bitset_foreach(candidates, pos) {
602 rootnr = get_irn_graph_nr(root);
603 mangle_var(buf, 'y', rootnr, irnnr);
604 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
605 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, costs[pos]);
609 del_pset(done); /* clear set for next row */
614 * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
615 * in the same block, iff they use the same arg at the same pos.
616 * Only one of the phis can get the arg.
618 static void pi_add_constr_M(problem_instance_t *pi) {
619 dom_tree_walk_irg(get_irg(pi->co), M_constr_walker, NULL, pi);
623 * Matrix P: Path contraints.
624 * If 2 nodes interfere and are there is a path of equal-color-edges
625 * connecting them, then at least one of those equal-color-edges
626 * will break and cause some costs.
628 static void pi_add_constr_P(problem_instance_t *pi) {
631 DBG((dbg, LEVEL_2, "Add P constraints...\n"));
633 /* for all optimization units (only phis) */
634 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
637 if (curr->min_nodes_costs == 0)
640 rootnr = get_irn_graph_nr(curr->nodes[0]);
641 /* check all argument pairs for interference */
642 for (i=1; i<curr->node_count; ++i) {
643 const ir_node *arg1 = curr->nodes[i];
644 int arg1nr = get_irn_graph_nr(arg1);
645 for (o=i+1; o<curr->node_count; ++o) {
646 const ir_node *arg2 = curr->nodes[o];
647 int arg2nr = get_irn_graph_nr(arg2);
648 if (nodes_interfere(pi->co->chordal_env, arg1, arg2)) {
649 int cst_idx, y_idx, min_costs;
652 mangle_cst(buf, 'P', cst_counter++);
653 min_costs = MIN(curr->costs[i], curr->costs[o]);
654 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, min_costs);
656 mangle_var(buf, 'y', rootnr, arg1nr);
657 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
658 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, curr->costs[i]);
660 mangle_var(buf, 'y', rootnr, arg2nr);
661 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
662 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, curr->costs[o]);
669 static void pi_solve_ilp(problem_instance_t *pi);
672 * Generate the initial problem matrices and vectors.
674 static problem_instance_t *new_pi(const copy_opt_t *co) {
675 problem_instance_t *pi;
678 DBG((dbg, LEVEL_2, "Generating new instance...\n"));
679 pi = xcalloc(1, sizeof(*pi));
681 pi->removed = pset_new_ptr_default();
682 INIT_LIST_HEAD(&pi->simplicials);
683 pi->dilp = new_lpp(co->name, lpp_minimize);
686 /* problem size reduction */
687 pi_find_simplicials(pi);
688 if (pi->all_simplicial)
691 /* built objective and constraints */
692 pi->curr_lp = pi->dilp;
694 for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
695 pi_add_constr_B(pi, col);
696 pi_add_constr_E_old(pi);
703 pi->ilp_new = new_lpp(co->name, lpp_minimize);
704 pi->ilp_newr = new_lpp(co->name, lpp_minimize);
706 pi->curr_lp = pi->ilp_new;
708 for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
709 pi_add_constr_B(pi, col);
710 pi_add_constr_E_new(pi);
713 pi->curr_lp = pi->ilp_newr;
715 for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
716 pi_add_constr_B(pi, col);
717 pi_add_constr_E_new_reverse(pi);
725 * Clean the problem instance
727 static void free_pi(problem_instance_t *pi) {
728 simpl_t *simpl, *tmp;
730 DBG((dbg, LEVEL_2, "Free instance...\n"));
732 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
734 del_pset(pi->removed);
739 * Set starting values for the mip problem according
740 * to the current coloring of the graph.
742 static void pi_set_start_sol(problem_instance_t *pi) {
745 DBG((dbg, LEVEL_2, "Set start solution...\n"));
746 for (i=1; i<=pi->last_x_var; ++i) {
749 /* get variable name */
750 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
751 /* split into components */
752 if (split_var(var_name, &nnr, &col) == 2) {
753 assert(get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) != -1);
754 val = (get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) == col) ? 1 : 0;
755 lpp_set_start_value(pi->curr_lp, i, val);
757 fprintf(stderr, "Variable name is: %s\n", var_name);
758 assert(0 && "x vars always look like this 'x123_45'");
766 static void pi_solve_ilp(problem_instance_t *pi) {
767 pi_set_start_sol(pi);
768 lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
769 printf(" SOLUTION TIME: %.2f\n", pi->curr_lp->sol_time);
773 * Set the color of all simplicial nodes removed form
774 * the graph before transforming it to an ilp.
776 static void pi_set_simplicials(problem_instance_t *pi) {
777 simpl_t *simpl, *tmp;
778 bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
780 DBG((dbg, LEVEL_2, "Set simplicials...\n"));
781 /* color the simplicial nodes in right order */
782 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
784 ir_node *other_irn, *irn;
785 if_node_t *other, *ifn;
787 /* get free color by inspecting all neighbors */
789 irn = get_irn_for_graph_nr(get_irg(pi->co), ifn->nnr);
790 bitset_clear_all(used_cols);
791 foreach_neighb(ifn, other) {
792 other_irn = get_irn_for_graph_nr(get_irg(pi->co), other->nnr);
793 if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
794 bitset_set(used_cols, get_irn_col(pi->co, other_irn));
797 /* now all bits not set are possible colors */
798 free_col = bitset_next_clear(used_cols, 0);
799 assert(free_col != -1 && "No free color found. This can not be.");
800 set_irn_col(pi->co, irn, free_col);
801 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
806 * Sets the colors of irns according to the values of variables
807 * provided by the solution of the solver.
809 static void pi_apply_solution(problem_instance_t *pi) {
812 lpp_sol_state_t state;
813 DBG((dbg, LEVEL_2, "Applying solution...\n"));
816 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //new we have ms
817 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
818 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
819 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
822 sol = xmalloc((pi->last_x_var+1) * sizeof(*sol));
823 state = lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
824 if (state != lpp_optimal) {
825 printf("Solution state is not 'optimal': %d\n", state);
826 assert(state >= lpp_feasible && "The solution should at least be feasible!");
828 for (i=0; i<pi->last_x_var; ++i) {
832 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
833 lpp_get_var_name(pi->curr_lp, 1+i, var_name, sizeof(var_name));
834 if (split_var(var_name, &nnr, &col) == 2) {
835 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]));
836 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
837 set_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr), col);
839 assert(0 && "This should be a x-var");
844 void co_ilp_opt(copy_opt_t *co) {
845 problem_instance_t *pi;
847 dbg = firm_dbg_register("ir.be.copyoptilp");
848 if (!strcmp(co->name, DEBUG_IRG))
849 firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_ILP);
851 firm_dbg_set_mask(dbg, DEBUG_LVL_ILP);
854 if (!pi->all_simplicial) {
857 snprintf(buf, sizeof(buf), "%s.mps", co->name);
858 lpp_dump(pi->curr_lp, buf);
861 pi_apply_solution(pi);
862 pi_set_simplicials(pi);