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