added additional cut inequalities for multi-col-usage.
[libfirm] / ir / be / becopyilp.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                17.05.2005
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #ifdef HAVE_ALLOCA_H
12 #include <alloca.h>
13 #endif
14 #ifdef HAVE_MALLOC_H
15 #include <malloc.h>
16 #endif
17
18 #include "irprog.h"
19
20 #include <lpp/lpp.h>
21 #include <lpp/lpp_net.h>
22 #include "xmalloc.h"
23 #include "becopyopt.h"
24 #include "becopystat.h"
25 #include "besched_t.h"
26
27 #define LPP_HOST "i44pc52"
28 #define LPP_SOLVER "cplex"
29
30 #undef DUMP_MPS
31 #define DEBUG_LVL SET_LEVEL_1
32 static firm_dbg_module_t *dbg = NULL;
33
34 #define MAX(a,b) ((a<b)?(b):(a))
35 #define EPSILON 0.00001
36 #define SLOTS_LIVING 32
37
38 typedef struct _simpl_t {
39         struct list_head chain;
40         if_node_t *ifn;
41 } simpl_t;
42
43 typedef struct _problem_instance_t {
44         const copy_opt_t *co;                   /** the copy_opt problem */
45         /* problem size reduction removing simple nodes */
46         struct list_head simplicials;   /**< holds all simpl_t's in right order to color*/
47         pset *removed;                                  /**< holds all removed simplicial irns */
48         /* lp problem */
49         lpp_t *dilp;                                    /**< problem formulation directly as milp */
50         /* overhead stuff */
51         lpp_t *curr_lp;                                 /**< points to the problem currently used */
52         int cst_counter, last_x_var;
53         char buf[32];
54         int all_simplicial;
55 } problem_instance_t;
56
57 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
58
59 #define is_color_possible(irn,color) arch_reg_is_allocatable(pi->co->chordal_env->arch_env, irn, arch_pos_make_out(0), arch_register_for_index(pi->co->chordal_env->cls, color))
60
61 /*
62  * Some stuff for variable name handling.
63  */
64 #define mangle_cst(buf, prefix, nr) \
65                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
66
67 #define mangle_var(buf, prefix, node_nr, color) \
68                         snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
69
70 #define mangle_var_irn(buf, prefix, irn, color) \
71                         mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
72
73 #define split_var(var, nnr, col) \
74                         sscanf(var, "x%d_%d", (nnr), (col))
75
76
77 /**
78  * Checks if a node is simplicial in the graph
79  * heeding the already removed nodes.
80  */
81 static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
82         int i, o, size = 0;
83         if_node_t **all, *curr;
84         all = alloca(ifn_get_degree(ifn) * sizeof(*all));
85
86         /* get all non-removed neighbors */
87         foreach_neighb(ifn, curr)
88                 if (!is_removed(curr))
89                         all[size++] = curr;
90
91         /* check if these form a clique */
92         for (i=0; i<size; ++i)
93                 for (o=i+1; o<size; ++o)
94                         if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
95                                 return 0;
96
97         /* all edges exist so this is a clique */
98         return 1;
99 }
100
101 /**
102  * Iterative finds and 'removes' from the graph all nodes which are
103  * simplicial AND not member of a equal-color-wish
104  */
105 static void pi_find_simplicials(problem_instance_t *pi) {
106         set *if_nodes;
107         if_node_t *ifn;
108         int redo = 1;
109
110         DBG((dbg, LEVEL_2, "Find simlicials...\n"));
111
112         if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
113         while (redo) {
114                 redo = 0;
115                 for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
116                         ir_node *irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
117                         if (!is_removed(irn) && !is_optimizable(pi->co->chordal_env->arch_env, irn) &&
118           !is_optimizable_arg(pi->co, irn) && pi_is_simplicial(pi, ifn)) {
119                                 simpl_t *s = xmalloc(sizeof(*s));
120                                 s->ifn = ifn;
121                                 list_add(&s->chain, &pi->simplicials);
122                                 pset_insert_ptr(pi->removed, irn);
123                                 redo = 1;
124                                 DBG((dbg, LEVEL_2, " Removed %n %d\n", irn, get_irn_graph_nr(irn)));
125                         }
126                 }
127         }
128         if (set_count(be_ra_get_ifg_nodes(pi->co->chordal_env)) == pset_count(pi->removed))
129                 pi->all_simplicial = 1;
130 }
131
132 /**
133  * Add coloring-force conditions
134  * Matrix A: knapsack constraint for each node
135  */
136 static void pi_add_constr_A(problem_instance_t *pi) {
137         pmap_entry *pme;
138
139         DBG((dbg, LEVEL_2, "Add A constraints...\n"));
140         /* iterate over all blocks */
141         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
142                 struct list_head *head = pme->value;
143                 border_t *curr;
144                 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
145
146                 list_for_each_entry_reverse(border_t, curr, head, list)
147                         if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
148                                 int cst_idx, nnr, col;
149
150                                 nnr = get_irn_graph_nr(curr->irn);
151                                 mangle_cst(pi->buf, 'A', nnr);
152                                 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
153
154                                 // iterate over all possible colors in order
155                                 bitset_clear_all(pos_regs);
156                                 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, curr->irn, arch_pos_make_out(0), pi->co->chordal_env->cls, pos_regs);
157                                 bitset_foreach(pos_regs, col) {
158                                         int var_idx;
159                                         mangle_var(pi->buf, 'x', nnr, col);
160                                         var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
161                                         pi->last_x_var = var_idx;
162                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
163                                 }
164                         }
165         }
166 }
167
168 /**
169  * Checks if all nodes in @p living are live in in block @p block.
170  * @return 1 if all are live in
171  *         0 else
172  */
173 static INLINE int all_live_in(ir_node *block, pset *living) {
174         ir_node *n;
175         for (n = pset_first(living); n; n = pset_next(living))
176                 if (!is_live_in(block, n)) {
177                         pset_break(living);
178                         return 0;
179                 }
180         return 1;
181 }
182
183 /**
184  * Finds cliques in the interference graph, considering only nodes
185  * for which the color @p color is possible. Finds only 'maximal-cliques',
186  * viz cliques which are not contained in another one.
187  * Matrix B: interference constraints using cliques
188  */
189 static void pi_add_constr_B(problem_instance_t *pi, int color) {
190         enum phase_t {growing, shrinking} phase = growing;
191         border_t *b;
192         pmap_entry *pme;
193         pset *living = pset_new_ptr(SLOTS_LIVING);
194
195         DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
196         /* iterate over all blocks */
197         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
198                 ir_node *block = pme->key;
199                 struct list_head *head = pme->value;
200
201                 list_for_each_entry_reverse(border_t, b, head, list) {
202                         const ir_node *irn = b->irn;
203                         if (is_removed(irn) || !is_color_possible(irn, color))
204                                 continue;
205
206                         if (b->is_def) {
207                                 DBG((dbg, LEVEL_2, "Def %n\n", irn));
208                                 pset_insert_ptr(living, irn);
209                                 phase = growing;
210                         } else { /* is_use */
211                                 DBG((dbg, LEVEL_2, "Use %n\n", irn));
212
213                                 /* before shrinking the set, store the current 'maximum' clique;
214                                  * do NOT if clique is a single node
215                                  * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
216                                 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
217                                         int cst_idx;
218                                         ir_node *n;
219                                         mangle_cst(pi->buf, 'B', pi->cst_counter);
220                                         cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
221                                         for (n = pset_first(living); n; n = pset_next(living)) {
222                                                 int var_idx;
223                                                 mangle_var_irn(pi->buf, 'x', n, color);
224                                                 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
225                                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
226                                         }
227                                         pi->cst_counter++;
228                                 }
229                                 pset_remove_ptr(living, irn);
230                                 phase = shrinking;
231                         }
232                 }
233         }
234         assert(0 == pset_count(living));
235         del_pset(living);
236 }
237
238 /**
239  * Generates constraints which interrelate x with y variables.
240  * x1 and x2 have the different colors ==> y_12 = 1
241  */
242 static void pi_add_constr_E(problem_instance_t *pi) {
243         unit_t *curr;
244         bitset_t *root_regs, *arg_regs, *work_regs;
245         int cst_counter = 0;
246         unsigned nregs = pi->co->chordal_env->cls->n_regs;
247         root_regs = bitset_alloca(nregs);
248         arg_regs = bitset_alloca(nregs);
249         work_regs = bitset_alloca(nregs);
250
251         DBG((dbg, LEVEL_2, "Add E constraints...\n"));
252         /* for all roots of optimization units */
253         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
254                 ir_node *root, *arg;
255                 int rootnr, argnr, color;
256                 int y_idx, i;
257                 char buf[32];
258
259                 root = curr->nodes[0];
260                 rootnr = get_irn_graph_nr(root);
261                 bitset_clear_all(root_regs);
262                 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
263
264                 /* for all arguments of root */
265                 for (i = 1; i < curr->node_count; ++i) {
266                         arg = curr->nodes[i];
267                         argnr = get_irn_graph_nr(arg);
268                         bitset_clear_all(arg_regs);
269                         arch_get_allocatable_regs(pi->co->chordal_env->arch_env, arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
270
271                         /* Introduce new variable and set factor in objective function */
272                         mangle_var(buf, 'y', rootnr, argnr);
273                         y_idx = lpp_add_var(pi->curr_lp, buf, lpp_continous, curr->costs[i]);
274
275                         //BETTER: y vars as binary or continous vars ??
276                         /* set starting value */
277                         //lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
278
279                         /* For all colors root and arg have in common, add 2 constraints to E */
280                         bitset_copy(work_regs, root_regs);
281                         bitset_and(work_regs, arg_regs);
282                         bitset_foreach(work_regs, color) {
283                                 int root_idx, arg_idx, cst_idx;
284                                 mangle_var(buf, 'x', rootnr, color);
285                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
286                                 mangle_var(buf, 'x', argnr, color);
287                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
288
289                                 /* add root-arg-y <= 0 */
290                                 mangle_cst(buf, 'E', cst_counter++);
291                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
292                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
293                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
294                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
295
296                                 /* add arg-root-y <= 0 */
297                                 mangle_cst(buf, 'E', cst_counter++);
298                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
299                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
300                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
301                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
302                         }
303                         /* For all colors root and arg have "disjunct", add 1 constraints to E.
304                          * If root gets a color the arg is not possible to get then they will
305                          * definetly get different colors. So y has to be 1.
306                          * Vice versa for arg.
307                          */
308                         bitset_copy(work_regs, root_regs);
309                         bitset_xor(work_regs, arg_regs);
310                         bitset_foreach(work_regs, color) {
311                                 int root_idx, arg_idx, cst_idx;
312                                 mangle_var(buf, 'x', rootnr, color);
313                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
314                                 mangle_var(buf, 'x', argnr, color);
315                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
316
317                                 mangle_cst(buf, 'E', cst_counter++);
318                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
319                                 if (bitset_is_set(root_regs, color)) {
320                                         /* add root-y <= 0 */
321                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
322                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
323                                 } else {
324                                         assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
325                                         /* add arg-y <= 0 */
326                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
327                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
328                                 }
329                         }
330                 }
331         }
332 }
333
334 /**
335  * Matrix S: maximum independent set constraints
336  * Generates lower bound-cuts for optimization units with inner interferences.
337  * Sum(y_{root, arg}, arg \in Args) <= max_indep_set_size - 1
338  */
339 static void pi_add_constr_S(problem_instance_t *pi) {
340         unit_t *curr;
341         int cst_counter = 0;
342         DBG((dbg, LEVEL_2, "Add S constraints...\n"));
343
344         /* for all optimization units */
345         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
346                 const ir_node *root, *arg;
347                 int rootnr, argnr;
348                 int cst_idx, y_idx, i;
349                 char buf[32];
350
351                 if (curr->minimal_costs == 0)
352                         continue;
353
354                 root = curr->nodes[0];
355                 rootnr = get_irn_graph_nr(root);
356                 mangle_cst(buf, 'S', cst_counter++);
357                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, curr->minimal_costs);
358
359                 /* for all arguments */
360                 for (i = 1; i < curr->node_count; ++i) {
361                         arg = curr->nodes[i];
362                         argnr = get_irn_graph_nr(arg);
363                         mangle_var(buf, 'y', rootnr, argnr);
364                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
365                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, curr->costs[i]);
366                 }
367         }
368 }
369
370 static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
371         int i;
372         unit_t *curr;
373         /* search optimization unit for phi */
374         list_for_each_entry(unit_t, curr, &pi->co->units, units)
375                 if (curr->nodes[0] == phi) {
376                         for (i=1; i<curr->node_count; ++i)
377                                 if (curr->nodes[i] == irn)
378                                         return curr->costs[i];
379                         assert(0 && "irn must occur in this ou");
380                 }
381         assert(0 && "phi must be found in a ou");
382         return 0;
383 }
384
385 static void M_constr_walker(ir_node *block, void *env) {
386         problem_instance_t *pi = env;
387         int count, arity, row, col, other_row, *costs;
388         ir_node **phis, *phi, *irn, **phi_matrix;
389         pset *done;
390         bitset_t *candidates;
391
392         /* Count all phi nodes of this block */
393         for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
394                 count++;
395
396         /* We at least 2 phi nodes for this class of inequalities */
397         if (count < 2)
398                 return;
399
400         /* Build the \Phi-Matrix */
401         arity = get_irn_arity(sched_first(block));
402         phis = alloca(count * sizeof(*phis));
403         costs = alloca(count * sizeof(costs));
404         phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
405         candidates = bitset_alloca(count);
406
407         phi = sched_first(block);
408         for (row=0; row<count; ++row) {
409                 phis[row] = phi;
410                 for (col=0; col<arity; ++col)
411                         phi_matrix[row*arity + col] = get_irn_n(phi, col);
412                 phi = sched_next(phi);
413         }
414
415         /* Now find the interesting patterns in the matrix:
416          * All nodes which are used at least twice in a column. */
417         /* columnwise ... */
418         for (col=0; col<arity; ++col) {
419                 done = pset_new_ptr_default();
420                 for (row=0; row<count; ++row) {
421                         irn = phi_matrix[row*arity + col];
422                         /* has the irn already been processed in this col? */
423                         if (pset_find_ptr(done, irn))
424                                 continue;
425                         else
426                                 pset_insert_ptr(done, irn);
427
428                         /* insert irn in candidates */
429                         bitset_clear_all(candidates);
430                         bitset_set(candidates, row);
431                         /* search the irn in the rows below */
432                         for (other_row = row+1; other_row<count; ++other_row)
433                                 if (irn == phi_matrix[other_row*arity + col]) {
434                                         /* found the irn in the same col in another row */
435                                         bitset_set(candidates, other_row);
436                                 }
437
438                         /* now we know all occurences of irn in this col */
439                         if (bitset_popcnt(candidates) < 2)
440                                 continue;
441
442                         /* compute the minimal costs (rhs) */
443                         int phi_nr, sum=0, max=-1, minimal_costs;
444                         bitset_foreach(candidates, phi_nr) {
445                                 costs[phi_nr] = get_costs(pi, phis[phi_nr], irn);
446                                 sum += costs[phi_nr];
447                                 max = MAX(max, costs[phi_nr]);
448                         }
449                         minimal_costs = sum - max;
450
451                         /* generate an unequation finally.
452                          * phis are indexed in the bitset,
453                          * shared argument is irn
454                          * rhs is minimal_costs */
455                         {
456                                 char buf[32];
457                                 ir_node *root;
458                                 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
459
460                                 irnnr = get_irn_graph_nr(irn);
461                                 mangle_cst(buf, 'M', cst_counter++);
462                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_costs);
463
464                                 /* for all phis */
465                                 bitset_foreach(candidates, pos) {
466                                         root = phis[pos];
467                                         rootnr = get_irn_graph_nr(root);
468                                         mangle_var(buf, 'y', rootnr, irnnr);
469                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
470                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, costs[pos]);
471                                 }
472                         }
473                 }
474                 del_pset(done); /* clear set for next row */
475         } /*next col*/
476 }
477
478 /**
479  * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
480  * in the same block, iff they use the same arg at the same pos.
481  * Only one of the phis can get the arg.
482  */
483 static void pi_add_constr_M(problem_instance_t *pi) {
484         dom_tree_walk_irg(pi->co->chordal_env->irg, M_constr_walker, NULL, pi);
485 }
486
487 /**
488  * Generate the initial problem matrices and vectors.
489  */
490 static problem_instance_t *new_pi(const copy_opt_t *co) {
491         problem_instance_t *pi;
492         int col;
493
494         DBG((dbg, LEVEL_2, "Generating new instance...\n"));
495         pi = xcalloc(1, sizeof(*pi));
496         pi->co = co;
497         pi->removed = pset_new_ptr_default();
498         INIT_LIST_HEAD(&pi->simplicials);
499         pi->dilp = new_lpp(co->name, lpp_minimize);
500         pi->last_x_var = -1;
501
502         /* problem size reduction */
503         pi_find_simplicials(pi);
504         //BETTER If you wish to see it: dump_ifg_w/o_removed
505         if (pi->all_simplicial)
506                 return pi;
507
508         /* built objective abd constraints */
509         pi->curr_lp = pi->dilp;
510         pi_add_constr_A(pi);
511         for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
512                 pi_add_constr_B(pi, col);
513         pi_add_constr_E(pi);
514         pi_add_constr_S(pi);
515         pi_add_constr_M(pi);
516
517         return pi;
518 }
519
520 /**
521  * Clean the problem instance
522  */
523 static void free_pi(problem_instance_t *pi) {
524         simpl_t *simpl, *tmp;
525
526         DBG((dbg, LEVEL_2, "Free instance...\n"));
527         free_lpp(pi->dilp);
528         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
529                 free(simpl);
530         del_pset(pi->removed);
531         free(pi);
532 }
533
534 /**
535  * Set starting values for the mip problem according
536  * to the current coloring of the graph.
537  */
538 static void pi_set_start_sol(problem_instance_t *pi) {
539         int i;
540         char var_name[64];
541         DBG((dbg, LEVEL_2, "Set start solution...\n"));
542         for (i=1; i<=pi->last_x_var; ++i) {
543                 int nnr, col;
544                 double val;
545                 /* get variable name */
546                 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
547                 /* split into components */
548                 if (split_var(var_name, &nnr, &col) == 2) {
549                         assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) != -1);
550                         val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) == col) ? 1 : 0;
551                         lpp_set_start_value(pi->curr_lp, i, val);
552                 } else {
553                         fprintf(stderr, "Variable name is: %s\n", var_name);
554                         assert(0 && "x vars always look like this 'x123_45'");
555                 }
556         }
557 }
558
559 /**
560  * Invoke a solver
561  */
562 static void pi_solve_ilp(problem_instance_t *pi) {
563         pi_set_start_sol(pi);
564         lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
565 }
566
567 /**
568  * Set the color of all simplicial nodes removed form
569  * the graph before transforming it to an ilp.
570  */
571 static void pi_set_simplicials(problem_instance_t *pi) {
572         simpl_t *simpl, *tmp;
573         bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
574
575         DBG((dbg, LEVEL_2, "Set simplicials...\n"));
576         /* color the simplicial nodes in right order */
577         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
578                 int free_col;
579                 ir_node *other_irn, *irn;
580                 if_node_t *other, *ifn;
581
582                 /* get free color by inspecting all neighbors */
583                 ifn = simpl->ifn;
584                 irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
585                 bitset_clear_all(used_cols);
586                 foreach_neighb(ifn, other) {
587                         other_irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, other->nnr);
588                         if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
589                                 bitset_set(used_cols, get_irn_col(pi->co, other_irn));
590                 }
591
592                 /* now all bits not set are possible colors */
593                 free_col = bitset_next_clear(used_cols, 0);
594                 assert(free_col != -1 && "No free color found. This can not be.");
595                 set_irn_col(pi->co, irn, free_col);
596                 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
597         }
598 }
599
600 /**
601  * Sets the colors of irns according to the values of variables
602  * provided by the solution of the solver.
603  */
604 static void pi_apply_solution(problem_instance_t *pi) {
605         int i;
606         double *sol;
607         lpp_sol_state_t state;
608         DBG((dbg, LEVEL_2, "Applying solution...\n"));
609
610 #ifdef DO_STAT
611         curr_vals[I_ILP_ITER] += lpp_get_iter_cnt(pi->curr_lp);
612         curr_vals[I_ILP_TIME] += lpp_get_sol_time(pi->curr_lp);
613 #endif
614
615         sol = xmalloc((pi->last_x_var+1) * sizeof(*sol));
616         state = lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
617         if (state != lpp_optimal) {
618                 printf("Solution state is not 'optimal': %d\n", state);
619                 assert(state >= lpp_feasible && "The solution should at least be feasible!");
620         }
621         for (i=0; i<pi->last_x_var; ++i) {
622                 int nnr, col;
623                 char var_name[64];
624
625                 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
626                         lpp_get_var_name(pi->curr_lp, 1+i, var_name, sizeof(var_name));
627                         if (split_var(var_name, &nnr, &col) == 2) {
628                                 DBG((dbg, LEVEL_2, "Irn %n  Idx %d  Var %s  Val %f\n", get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr), i, var_name, sol[i]));
629                                 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
630                                 set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr), col);
631                         } else
632                                 assert(0 && "This should be a x-var");
633                 }
634         }
635 }
636
637 void co_ilp_opt(copy_opt_t *co) {
638         problem_instance_t *pi;
639
640         dbg = firm_dbg_register("ir.be.copyoptilp");
641         if (!strcmp(co->name, DEBUG_IRG))
642                 firm_dbg_set_mask(dbg, DEBUG_LVL_ILP);
643         else
644                 firm_dbg_set_mask(dbg, DEBUG_LVL);
645
646         pi = new_pi(co);
647         if (!pi->all_simplicial) {
648 #ifdef DUMP_MPS
649                 char buf[512];
650                 snprintf(buf, sizeof(buf), "%s.mps", co->name);
651                 lpp_dump(pi->curr_lp, buf);
652 #endif
653                 pi_solve_ilp(pi);
654                 pi_apply_solution(pi);
655                 pi_set_simplicials(pi);
656         }
657         free_pi(pi);
658 }