Next step of refactoring
[libfirm] / ir / be / becopyilp1.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  * CVS-ID:      $Id$
7  *
8  * ILP formalization using:
9  *  - 2 classes of vars: Nodes- and optimality variables.
10  *  - Clique constraints
11  *  - Path constraints
12  *  - Clique path constraints
13  */
14
15 #include "becopyilp_t.h"
16 #include "benumb_t.h"
17 #include "belive_t.h"
18 #include "irdom_t.h"
19 #include "irgwalk.h"
20 #include "xmalloc.h"
21 #include "pset.h"
22 #include "irprog.h"
23 #include "irdom_t.h"
24 #include "iredges_t.h"
25
26 #include "becopystat.h"
27 #include "besched_t.h"
28 #include "phiclass.h"
29
30 #if 0 //temporary
31
32 #define PATH_CONSTRAINTS_FOR_CLASSES
33 #undef DUMP_MPS
34
35 static firm_dbg_module_t *dbg = NULL;
36 #define SLOTS_LIVING 32
37
38 typedef struct _problem_instance_t {
39         const copy_opt_t *co;                   /**< the copy opt problem */
40         size_red_t *sr;                                 /**< problem size reduction. removes simple nodes */
41         lpp_t *lp;                                              /**< the linear programming problem */
42
43         /* Helpers for maintaining indices and finding variables */
44         int first_nnc_var_idx;                  /**< the first index of a constraint belonging to no-null-colors stuff*/
45         int cst_counter, first_x_var, last_x_var;
46         char buf[32];
47         pset *done;
48 } problem_instance_t;
49
50 #define is_color_possible(irn,color) arch_reg_is_allocatable(pi->co->aenv, irn, -1, arch_register_for_index(pi->co->cls, color))
51
52 /*
53  * Some stuff for variable name handling.
54  */
55 #define mangle_cst(buf, prefix, nr) \
56                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
57
58 #define mangle_var1(buf, prefix, color) \
59                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (color))
60
61 #define mangle_var2(buf, prefix, node_nr, color) \
62                         snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
63
64 #define mangle_var3(buf, prefix, n1, n2, col) \
65                         snprintf((buf), sizeof(buf), "%c%d_%d_%d", (prefix), (n1), (n2), (col))
66
67 #define mangle_var_irn(buf, prefix, irn, color) \
68                         mangle_var2((buf), (prefix), get_irn_graph_nr(irn), (color))
69
70 #define split_var(var, nnr, col) \
71                         sscanf(var, "x%d_%d", (nnr), (col))
72
73
74 /**
75  * Add coloring-force conditions
76  * Matrix A: knapsack constraint for each node
77  */
78 static void pi_add_constr_A(problem_instance_t *pi) {
79         pmap_entry *pme;
80
81         DBG((dbg, LEVEL_2, "Add A constraints...\n"));
82         /* iterate over all blocks */
83         pmap_foreach(pi->co->cenv->border_heads, pme) {
84                 struct list_head *head = pme->value;
85                 border_t *curr;
86                 bitset_t *pos_regs = bitset_alloca(pi->co->cenv->cls->n_regs);
87
88                 list_for_each_entry_reverse(border_t, curr, head, list)
89                         if (curr->is_def && curr->is_real && !sr_is_removed(pi->sr->curr->irn)) {
90                                 int cst_idx, nnr, col;
91
92                                 nnr = get_irn_graph_nr(curr->irn);
93                                 mangle_cst(pi->buf, 'A', nnr);
94                                 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
95
96                                 /* iterate over all possible colors in order */
97                                 bitset_clear_all(pos_regs);
98                                 arch_get_allocatable_regs(pi->co->aenv, curr->irn, -1, pos_regs);
99                                 bitset_foreach(pos_regs, col) {
100                                         int var_idx;
101                                         mangle_var2(pi->buf, 'x', nnr, col);
102                                         var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
103                                         if (!pi->first_x_var)
104                                                 pi->first_x_var = var_idx;
105                                         pi->last_x_var = var_idx;
106                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
107                                 }
108                         }
109         }
110 }
111
112 /**
113  * Checks if all nodes in @p living are live in in block @p block.
114  * @return 1 if all are live in
115  *         0 else
116  */
117 static INLINE int all_live_in(ir_node *block, pset *living) {
118         ir_node *n;
119         for (n = pset_first(living); n; n = pset_next(living))
120                 if (!is_live_in(block, n)) {
121                         pset_break(living);
122                         return 0;
123                 }
124         return 1;
125 }
126
127 /**
128  * Finds cliques in the interference graph, considering only nodes
129  * for which the color @p color is possible. Finds only 'maximal-cliques',
130  * viz cliques which are not contained in another one.
131  * Matrix B: interference constraints using cliques
132  */
133 static void pi_add_constr_B(problem_instance_t *pi, int color) {
134         enum phase_t {growing, shrinking} phase = growing;
135         border_t *b;
136         pmap_entry *pme;
137         pset *living = pset_new_ptr(SLOTS_LIVING);
138
139         DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
140         /* iterate over all blocks */
141         pmap_foreach(pi->co->cenv->border_heads, pme) {
142                 ir_node *block = pme->key;
143                 struct list_head *head = pme->value;
144
145                 list_for_each_entry_reverse(border_t, b, head, list) {
146                         const ir_node *irn = b->irn;
147                         if (is_removed(irn) || !is_color_possible(irn, color))
148                                 continue;
149
150                         if (b->is_def) {
151                                 DBG((dbg, LEVEL_2, "Def %n\n", irn));
152                                 pset_insert_ptr(living, irn);
153                                 phase = growing;
154                         } else { /* is_use */
155                                 DBG((dbg, LEVEL_2, "Use %n\n", irn));
156
157                                 /* before shrinking the set, store the current 'maximum' clique;
158                                  * do NOT if clique is a single node
159                                  * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
160                                 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
161                                         int cst_idx;
162                                         ir_node *n;
163                                         mangle_cst(pi->buf, 'B', pi->cst_counter);
164                                         cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
165                                         for (n = pset_first(living); n; n = pset_next(living)) {
166                                                 int var_idx;
167                                                 mangle_var_irn(pi->buf, 'x', n, color);
168                                                 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
169                                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
170                                         }
171                                         pi->cst_counter++;
172                                 }
173                                 pset_remove_ptr(living, irn);
174                                 phase = shrinking;
175                         }
176                 }
177         }
178         assert(0 == pset_count(living));
179         del_pset(living);
180 }
181
182 /**
183  * Generates constraints which interrelate x with y variables.
184  * x1 and x2 have the different colors ==> y_12 = 1
185  */
186 static void pi_add_constr_E(problem_instance_t *pi) {
187         unit_t *curr;
188         bitset_t *root_regs, *arg_regs, *work_regs;
189         int cst_counter = 0;
190         unsigned nregs = pi->co->cenv->cls->n_regs;
191         root_regs = bitset_alloca(nregs);
192         arg_regs = bitset_alloca(nregs);
193         work_regs = bitset_alloca(nregs);
194
195         DBG((dbg, LEVEL_2, "Add E constraints...\n"));
196         /* for all roots of optimization units */
197         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
198                 ir_node *root, *arg;
199                 int rootnr, argnr, color;
200                 int y_idx, i;
201                 char buf[32];
202
203                 root = curr->nodes[0];
204                 rootnr = get_irn_graph_nr(root);
205                 bitset_clear_all(root_regs);
206                 arch_get_allocatable_regs(pi->co->aenv, root, -1, root_regs);
207
208                 /* for all arguments of root */
209                 for (i = 1; i < curr->node_count; ++i) {
210                         arg = curr->nodes[i];
211                         argnr = get_irn_graph_nr(arg);
212                         bitset_clear_all(arg_regs);
213                         arch_get_allocatable_regs(pi->co->aenv, arg, -1, arg_regs);
214
215                         /* Introduce new variable and set factor in objective function */
216                         mangle_var2(buf, 'y', rootnr, argnr);
217                         y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
218
219                         /* set starting value */
220                         lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
221
222                         /* For all colors root and arg have in common, add 2 constraints to E */
223                         bitset_copy(work_regs, root_regs);
224                         bitset_and(work_regs, arg_regs);
225                         bitset_foreach(work_regs, color) {
226                                 int root_idx, arg_idx, cst_idx;
227                                 mangle_var2(buf, 'x', rootnr, color);
228                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
229                                 mangle_var2(buf, 'x', argnr, color);
230                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
231
232                                 /* add root-arg-y <= 0 */
233                                 mangle_cst(buf, 'E', cst_counter++);
234                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
235                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
236                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
237                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
238
239                                 /* add arg-root-y <= 0 */
240                                 mangle_cst(buf, 'E', cst_counter++);
241                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
242                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
243                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
244                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
245                         }
246                         /* For all colors root and arg have "disjunct", add 1 constraints to E.
247                          * If root gets a color the arg is not possible to get then they will
248                          * definetly get different colors. So y has to be 1.
249                          * Vice versa for arg.
250                          */
251                         bitset_copy(work_regs, root_regs);
252                         bitset_xor(work_regs, arg_regs);
253                         bitset_foreach(work_regs, color) {
254                                 int root_idx, arg_idx, cst_idx;
255                                 mangle_var2(buf, 'x', rootnr, color);
256                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
257                                 mangle_var2(buf, 'x', argnr, color);
258                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
259
260                                 mangle_cst(buf, 'E', cst_counter++);
261                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
262                                 if (bitset_is_set(root_regs, color)) {
263                                         /* add root-y <= 0 */
264                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
265                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
266                                 } else {
267                                         assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
268                                         /* add arg-y <= 0 */
269                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
270                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
271                                 }
272                         }
273                 }
274         }
275 }
276
277 static void clique_path_walker(ir_node *block, void *env) {
278         problem_instance_t *pi = env;
279         int count, arity, row, col, other_row, *costs;
280         ir_node **phis, *phi, *irn, **phi_matrix;
281         pset *done;
282         bitset_t *candidates;
283
284         /* Count all phi nodes of this block */
285         for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
286                 count++;
287
288         /* We at least 2 phi nodes for this class of inequalities */
289         if (count < 2)
290                 return;
291
292         /* Build the \Phi-Matrix */
293         arity = get_irn_arity(sched_first(block));
294         phis = alloca(count * sizeof(*phis));
295         costs = alloca(count * sizeof(costs));
296         phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
297         candidates = bitset_alloca(count);
298
299         phi = sched_first(block);
300         for (row=0; row<count; ++row) {
301                 phis[row] = phi;
302                 for (col=0; col<arity; ++col) {
303                         ir_node *arg = get_irn_n(phi, col);
304                         /* Sort out all arguments interfering with its phi */
305                         if (nodes_interfere(pi->co->cenv, phi, arg)) {
306                                 phi_matrix[row*arity + col] =  NULL;
307                         } else
308                                 phi_matrix[row*arity + col] =  arg;
309                 }
310                 phi = sched_next(phi);
311         }
312
313         /* Now find the interesting patterns in the matrix:
314          * All nodes which are used at least twice in a column. */
315         /* columnwise ... */
316         for (col=0; col<arity; ++col) {
317                 done = pset_new_ptr_default();
318                 for (row=0; row<count; ++row) {
319                         irn = phi_matrix[row*arity + col];
320                         /*
321                          * is this an interfering arg (NULL)
322                          * or has the irn already been processed in this col?
323                          */
324                         if (!irn || pset_find_ptr(done, irn))
325                                 continue;
326                         else
327                                 pset_insert_ptr(done, irn);
328
329                         /* insert irn in candidates */
330                         bitset_clear_all(candidates);
331                         bitset_set(candidates, row);
332                         /* search the irn in the rows below */
333                         for (other_row = row+1; other_row<count; ++other_row)
334                                 if (irn == phi_matrix[other_row*arity + col]) {
335                                         /* found the irn in the same col in another row */
336                                         bitset_set(candidates, other_row);
337                                 }
338
339                         /* now we know all occurences of irn in this col */
340                         if (bitset_popcnt(candidates) < 2)
341                                 continue;
342
343                         /* generate an unequation finally.
344                          * phis are indexed in the bitset,
345                          * shared argument is irn
346                          * rhs is phi_count - 1 */
347                         {
348                                 char buf[32];
349                                 ir_node *root;
350                                 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
351                                 int minimal_unequal_count = bitset_popcnt(candidates)-1;
352
353                                 irnnr = get_irn_graph_nr(irn);
354                                 mangle_cst(buf, 'M', cst_counter++);
355                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_unequal_count);
356
357                                 /* for all phis */
358                                 bitset_foreach(candidates, pos) {
359                                         root = phis[pos];
360                                         rootnr = get_irn_graph_nr(root);
361                                         mangle_var2(buf, 'y', rootnr, irnnr);
362                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
363                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
364                                 }
365                         }
366                 }
367                 del_pset(done); /* clear set for next row */
368         } /*next col*/
369 }
370
371 /**
372  * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
373  * in the same block, iff they use the same arg at the same pos.
374  * Only one of the phis can get the arg.
375  */
376 static void pi_add_clique_path_cstr(problem_instance_t *pi) {
377         DBG((dbg, LEVEL_2, "Adding clique path constraints...\n"));
378         dom_tree_walk_irg(pi->co->irg, clique_path_walker, NULL, pi);
379 }
380
381 #ifndef PATH_CONSTRAINTS_FOR_CLASSES
382 /**
383  * Matrix P: Path contraints.
384  * If 2 nodes interfere and there is a path of equal-color-edges
385  * connecting them, then at least one of those equal-color-edges
386  * will break and cause some costs.
387  */
388 static void pi_add_path_cstr(problem_instance_t *pi) {
389         unit_t *curr;
390         int cst_counter = 0;
391         DBG((dbg, LEVEL_2, "Adding path constraints...\n"));
392
393         /* for all optimization units (only phis) */
394         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
395                 int i, o, rootnr;
396
397                 if (curr->min_nodes_costs == 0)
398                         continue;
399
400                 rootnr = get_irn_graph_nr(curr->nodes[0]);
401                 /* check all argument pairs for interference */
402                 for (i=1; i<curr->node_count; ++i) {
403                         const ir_node *arg1 = curr->nodes[i];
404                         int arg1nr = get_irn_graph_nr(arg1);
405                         for (o=i+1; o<curr->node_count; ++o) {
406                                 const ir_node *arg2 = curr->nodes[o];
407                                 int arg2nr = get_irn_graph_nr(arg2);
408                                 if (nodes_interfere(pi->co->cenv, arg1, arg2)) {
409                                         int cst_idx, y_idx;
410                                         char buf[32];
411
412                                         mangle_cst(buf, 'P', cst_counter++);
413                                         cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
414
415                                         mangle_var2(buf, 'y', rootnr, arg1nr);
416                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
417                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
418
419                                         mangle_var2(buf, 'y', rootnr, arg2nr);
420                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
421                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
422                                 }
423                         }
424                 }
425         }
426 }
427 #endif
428
429 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
430 static INLINE int get_y_var_idx(problem_instance_t *pi, int nnr1, int nnr2) {
431         int res;
432         char buf[30];
433
434         mangle_var2(buf, 'y', nnr1, nnr2);
435         if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
436                 return res;
437
438         mangle_var2(buf, 'y', nnr2, nnr1);
439         if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
440                 return res;
441
442         assert(0 && "One of them must work");
443   return -1;
444 }
445
446 static void check_ecc_and_add_cut(problem_instance_t *pi, ir_node **path, int length, pset *remain, ir_node *tgt) {
447         if (path[length-1] == tgt) { /* we found a path */
448                 int cst_idx, var_idx, i, nnr1, nnr2;
449                 char buf[30];
450
451                 /* add cut to ilp */
452                 mangle_cst(buf, 'Q', pi->cst_counter++);
453                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
454
455                 /* add all vars along the path */
456                 nnr2 = get_irn_graph_nr(path[0]);
457                 for (i=1; i<length; ++i) {
458                         nnr1 = nnr2;
459                         nnr2 = get_irn_graph_nr(path[i]);
460                         var_idx = get_y_var_idx(pi, nnr1, nnr2);
461                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
462                 }
463         } else { /* try to extend the path */
464                 be_chordal_env_t *cenv = pi->co->cenv;
465                 const ir_edge_t *edge;
466                 ir_node *end = path[length-1];
467                 ir_node **next = alloca(pset_count(remain) * sizeof(*next));
468                 int i, o, max, next_pos = 0;
469                 pset *done = pset_new_ptr_default();
470
471                 /* find all potential next nodes on path */
472                 /*  args of phis */
473                 if (is_Phi(end))
474                         for(i=0, max=get_irn_arity(end); i<max; ++i) {
475                                 ir_node *arg = get_irn_n(end, i);
476                                 if (!pset_find_ptr(done, arg) && pset_find_ptr(remain, arg)) {
477                                         next[next_pos++] = arg;
478                                         pset_insert_ptr(done, arg);
479                                 }
480                         }
481                 /*  outs of phis and other nodes */
482                 foreach_out_edge(end, edge) {
483                         ir_node *user = edge->src;
484                         if (is_Phi(user) && !pset_find_ptr(done, user) && pset_find_ptr(remain, user)) {
485                                 next[next_pos++] = user;
486                                 pset_insert_ptr(done, user);
487                         }
488                 }
489                 del_pset(done);
490
491
492                 /* delete all potential nodes with interferences to other nodes in the path */
493                 for (i=0; i<next_pos; ++i) {
494                         ir_node *nn = next[i];
495
496                         /* if next is the tgt, it may interfere with path[0],
497                          * so skip the first check */
498                         o = (nn == tgt && length > 1) ? 1 : 0;
499
500                         for(; o<length; ++o)
501                                 if (nodes_interfere(cenv, nn, path[o])) {
502                                         next[i] = NULL;
503                                         break;
504                                 }
505                 }
506                 /* now we have all possible nodes in next; impossibles are NULL */
507
508                 /* try to finish path with all possible nodes */
509                 for (i=0; i<next_pos; ++i) {
510                         if (!next[i]) /* this was an impossible node */
511                                 continue;
512
513                         path[length] = next[i];
514                         pset_remove_ptr(remain, next[i]);
515                         check_ecc_and_add_cut(pi, path, length+1, remain, tgt);
516                         pset_insert_ptr(remain, next[i]);
517                 }
518         }
519 }
520
521 static void path_cstr_for_classes_walker(ir_node *irn, void *env) {
522         problem_instance_t *pi = env;
523         be_chordal_env_t *cenv;
524         int i, o, max;
525         ir_node *m, **cls;
526         pset *class = get_phi_class(irn);
527         if (!class || pset_find_ptr(pi->done, class))
528                 return;
529
530         pset_insert_ptr(pi->done, class);
531
532         /* pset to array */
533         max = pset_count(class);
534         cls = alloca(max * sizeof(*cls));
535         for(i=0, m = pset_first(class); m; i++, m = pset_next(class)) {
536                 DBG((dbg, LEVEL_1, " class member: %+F\n", m));
537                 cls[i] = m;
538         }
539
540         cenv = pi->co->cenv;
541         for(i=0; i<max; ++i) {
542                 ir_node **path = alloca(max * sizeof(*path));
543                 pset *remain = pset_new_ptr(8);
544                 pset_insert_pset_ptr(remain, class);
545
546                 /* add cls[i] to path and remove it from remainder */
547                 path[0] = cls[i];
548                 pset_remove_ptr(remain, cls[i]);
549
550                 for(o=i+1; o<max; ++o)
551                         if (nodes_interfere(cenv, cls[i], cls[o]))
552                                 check_ecc_and_add_cut(pi, path, 1, remain, cls[o]);
553
554                 /* insert back into remainder */
555                 pset_insert_ptr(remain, cls[i]);
556         }
557 }
558
559
560 /**
561  * Matrix P: Path contraints.
562  * If 2 nodes interfere and there is a path of equal-color-edges
563  * connecting them, then at least one of those equal-color-edges
564  * will break and cause some costs.
565  */
566 static void pi_add_path_cstr_for_classes(problem_instance_t *pi) {
567         DBG((dbg, LEVEL_2, "Adding path constraints for phi classes...\n"));
568         pi->cst_counter = 0;
569         pi->done = pset_new_ptr_default();
570         irg_walk_graph(pi->co->irg, path_cstr_for_classes_walker, NULL, pi);
571         del_pset(pi->done);
572 }
573 #endif
574
575
576
577 /**
578  * Generate the initial problem matrices and vectors.
579  */
580 static problem_instance_t *new_pi(const copy_opt_t *co) {
581         problem_instance_t *pi;
582         int col;
583
584         DBG((dbg, LEVEL_2, "Generating new instance...\n"));
585         pi = xcalloc(1, sizeof(*pi));
586         pi->co  = co;
587         pi->sr  = new_size_red(co);
588         pi->lp  = new_lpp(co->name, lpp_minimize);
589
590         return pi;
591 }
592
593 static void pi_construct(problem_instance_t *pi) {
594         pi_add_constr_A(pi);
595         for (col = 0; col < pi->co->cls->n_regs; ++col)
596                 pi_add_constr_B(pi, col);
597         pi_add_constr_E(pi);
598
599 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
600         pi_add_path_cstr_for_classes(pi);
601 #else
602         pi_add_path_cstr(pi);
603 #endif
604         pi_add_clique_path_cstr(pi);
605 }
606
607 /**
608  * Clean the problem instance
609  */
610 static void free_pi(problem_instance_t *pi) {
611         simpl_t *simpl, *tmp;
612
613         DBG((dbg, LEVEL_2, "Free instance...\n"));
614         free_lpp(pi->lp);
615         free_size_red(pi->sr);
616         free(pi);
617 }
618
619
620 /**
621  * Set starting values for the mip problem according
622  * to the current coloring of the graph.
623  */
624 static void pi_set_start_sol(problem_instance_t *pi) {
625         int i;
626         char var_name[64];
627         DBG((dbg, LEVEL_2, "Set start solution...\n"));
628         for (i=pi->first_x_var; i<=pi->last_x_var; ++i) {
629                 int nnr, col;
630                 double val;
631                 /* get variable name */
632                 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
633                 /* split into components */
634                 if (split_var(var_name, &nnr, &col) == 2) {
635                         assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) != -1);
636                         val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) == col) ? 1 : 0;
637                         lpp_set_start_value(pi->curr_lp, i, val);
638                 } else {
639                         fprintf(stderr, "Variable name is: %s\n", var_name);
640                         assert(0 && "x vars always look like this 'x123_45'");
641                 }
642         }
643 }
644
645
646 /**
647  * Invoke a solver
648  */
649 static void pi_solve_ilp(problem_instance_t *pi, double time_limit) {
650         double lower_bound;
651 #ifdef DUMP_MPS
652         char buf[512];
653         snprintf(buf, sizeof(buf), "%s.ilp1", co->name);
654         lpp_dump(pi->lp, buf);
655 #endif
656
657         pi_set_start_sol(pi);
658
659         lower_bound = co_get_lower_bound(pi->co) - co_get_inevit_copy_costs(pi->co);
660         lpp_set_bound(pi->curr_lp, lower_bound);
661
662         lpp_set_time_limit(pi->curr_lp, time_limit);
663
664 #ifdef LPP_SOLVE_REMOTE
665         lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
666 #else
667         lpp_solve_cplex(pi->curr_lp);
668 #endif
669         DBG((dbg, LEVEL_1, "Solution time: %.2f\n", pi->curr_lp->sol_time));
670 }
671
672
673 /**
674  * Sets the colors of irns according to the values of variables
675  * provided by the solution of the solver.
676  */
677 static int pi_apply_solution(problem_instance_t *pi) {
678         int res = 1, i;
679         double *sol;
680         lpp_sol_state_t state;
681         DBG((dbg, LEVEL_2, "Applying solution...\n"));
682
683 #ifdef DO_STAT
684         copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp)));  //now we have ms
685         copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
686         copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
687         copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
688 #endif
689
690         sol = xmalloc((pi->last_x_var - pi->first_x_var + 1) * sizeof(*sol));
691         state = lpp_get_solution(pi->curr_lp, sol, pi->first_x_var, pi->last_x_var);
692         if (state != lpp_optimal) {
693                 printf("WARNING %s: Solution state is not 'optimal': %d\n", pi->co->name, state);
694                 assert(state >= lpp_feasible && "The solution should at least be feasible!");
695                 res = 0;
696         }
697         for (i=0; i<pi->last_x_var - pi->first_x_var + 1; ++i) {
698                 int nnr, col;
699                 char var_name[64];
700
701                 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
702                         lpp_get_var_name(pi->curr_lp, pi->first_x_var+i, var_name, sizeof(var_name));
703                         if (split_var(var_name, &nnr, &col) == 2) {
704                                 DBG((dbg, LEVEL_2, "Irn %n  Idx %d  Var %s  Val %f\n", get_irn_for_graph_nr(pi->co->irg, nnr), i, var_name, sol[i]));
705                                 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
706                                 set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr), col);
707                         } else
708                                 assert(0 && "This should be a x-var");
709                 }
710         }
711         return res;
712 }
713
714
715 int co_solve_ilp11(copy_opt_t *co, double time_limit) {
716         int res = 1;
717         problem_instance_t *pi;
718
719         dbg = firm_dbg_register("ir.be.copyoptilp1");
720
721         pi = new_pi(co);
722
723         sr_remove(pi->sr);                              /* problem size reduction */
724         pi_construct(pi);                               /* set up the problem */
725         pi_solve_ilp(pi, time_limit);   /* solve it */
726         res = pi_apply_solution(pi);    /* apply solution */
727         sr_reinsert(pi->sr);                    /* complete the partial solution */
728
729         free_pi(pi);
730         return res;
731 }
732 #endif
733
734 #include "becopyilp_t.h"
735
736 #define DEBUG_LVL 1
737
738 typedef struct _my_env_t {
739         int foo;
740 } my_env_t;
741
742
743 static void ilp2_build(ilp_env_t *ienv) {
744         ienv->lp = new_lpp(ienv->co->name, lpp_minimize);
745
746 }
747
748 static void ilp2_apply(ilp_env_t *ienv) {
749
750 }
751
752 int co_solve_ilp1(copy_opt_t *co, double time_limit) {
753         lpp_sol_state_t sol_state;
754         ilp_env_t *ienv;
755         my_env_t my;
756         firm_dbg_module_t *dbg = firm_dbg_register("ir.be.coilp2");
757
758         firm_dbg_set_mask(dbg, DEBUG_LVL);
759
760         // my.bla = TODO
761
762         ienv = new_ilp_env(co, dbg, ilp2_build, ilp2_apply, &my);
763
764         sol_state = ilp_go(ienv, time_limit);
765
766         free_ilp_env(ienv);
767
768         return sol_state == lpp_optimal;
769 }