Lots of changes: Removed all file for phi-opt. Generic approach is now
[libfirm] / ir / be / becopyilp.c
1 /**
2  * Minimizing copies with an exact algorithm using mixed integer programming (MIP).
3  * Problem statement as a 'quadratic 0-1 program with linear constraints' with
4  * n binary variables. Constraints are knapsack (enforce color for each node) and
5  * cliques of ifg (interference constraints).
6  * Transformation into a 'mixed integer program' with n binary variables and
7  * additional 2n real variables. Constraints are the above the transformed
8  * objective function and 'complementary conditions' for two var classes.
9  * @author Daniel Grund
10  *
11  * NOTE: Unfortunately no good solver is available locally (or even for linking)
12  *       We use CPLEX 9.0 which runs on a machine residing at the Rechenzentrum.
13  * @date 12.04.2005
14  */
15
16 #include "becopyopt.h"
17
18 #define DUMP_MPS                        /**< dumps the problem in "CPLEX"-MPS format. NOT fixed-column-MPS. */
19 #define USE_SOS                         /**< uses Special Ordered Sets when using MPS */
20 #define DO_SOLVE                        /**< solve the MPS output with CPLEX */
21 #define DUMP_MATRICES           /**< dumps all matrices completely. only recommended for small problems */
22 #define DUMP_LP                         /**< dumps the problem in LP format. 'human-readable' equations etc... */
23 #define DELETE_FILES            /**< deletes all dumped files after use */
24 #define EXPECT_FILENAME "runme" /** name of the expect-script */
25
26 /* CPLEX-account related stuff */
27 #define SSH_USER_HOST_PATH "kb61@sp-smp.rz.uni-karlsruhe.de"
28 #define SSH_PASSWD "!cplex90"
29
30 #define DEBUG_LVL SET_LEVEL_1
31 static firm_dbg_module_t *dbg = NULL;
32
33 #define SLOTS_NUM2POS 256
34 #define SLOTS_LIVING 32
35
36 /**
37  * A type storing names of the x variables in the form x[NUMBER]_[COLOR]
38  */
39 typedef struct _x_name_t {
40         int n, c;
41 } x_name_t;
42
43 /**
44  * For each node taking part in the opt-problem its position in the
45  * x-variable-vector is stored in a set. This set maps the node-nr (given by
46  * benumb) to the position in the vector.
47  */
48 typedef struct _num2pos_t {
49         int num, pos;
50 } num2pos_t;
51
52 /**
53  * A type storing the unmodified '0-1 quadratic program' of the form
54  * min f = xQx
55  * udN:  Ax  = e
56  *       Bx <= e
57  *        x \in {0, 1}
58  *
59  * This problem is called the original problem
60  */
61 typedef struct _problem_instance_t {
62         ir_graph* irg;
63         const char *name;
64         int x_dim, A_dim, B_dim;        /**< number of: x variables, rows in A, rows in B */
65         x_name_t *x;                            /**< stores the names of the x variables. all possible colors for a node are ordered and occupy consecutive entries. lives in obstack ob. */
66         set *num2pos;                           /**< maps node numbers to positions in x. */
67         sp_matrix_t *Q, *A, *B;         /**< the (sparse) matrices of this problem */
68
69         /* needed only for linearizations */
70         int bigM, maxQij, minQij;
71
72         /* overhead needed to build this */
73         struct obstack ob;
74         int curr_color;
75         int curr_row;
76 } problem_instance_t;
77
78 /* Nodes have consecutive numbers so this hash shoud be fine */
79 #define HASH_NUM(num) num
80
81 static int set_cmp_num2pos(const void *x, const void *y, size_t size) {
82         return ((num2pos_t *)x)->num != ((num2pos_t *)y)->num;
83 }
84
85 /**
86  * Sets the first position of node with number num to pos.
87  * See x_name_t *x in _problem_instance_t.
88  */
89 static INLINE void pi_set_first_pos(problem_instance_t *pi, int num, int pos) {
90         num2pos_t find;
91         find.num = num;
92         find.pos = pos;
93         set_insert(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
94 }
95
96 /**
97  * Get position by number. (First possible color)
98  * returns -1 if not found.
99  */
100 static INLINE int pi_get_first_pos(problem_instance_t *pi, int num) {
101         num2pos_t find, *found;
102         find.num = num;
103         found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
104         if (found) {
105                 assert(pi->x[found->pos].n == num && (found->pos == 0 || pi->x[found->pos-1].n != num) && "pi->num2pos is broken!");
106                 return found->pos;
107         } else
108                 return -1;
109 }
110
111 /**
112  * Get position by number and color.
113  * returns -1 if not found.
114  */
115 static INLINE int pi_get_pos(problem_instance_t *pi, int num, int col) {
116         num2pos_t find, *found;
117         find.num = num;
118         int pos;
119         found = set_find(pi->num2pos, &find, sizeof(find), HASH_NUM(num));
120         if (!found)
121                 return -1;
122         pos = found->pos;
123         while (pos < pi->x_dim && pi->x[pos].n == num && pi->x[pos].c < col)
124                 pos++;
125
126         if (pi->x[pos].n == num && pi->x[pos].c == col)
127                 return pos;
128         else
129                 return -1;
130 }
131
132 static INLINE FILE *ffopen(const char *base, const char *ext, const char *mode) {
133         FILE *out;
134         char buf[1024];
135
136         snprintf(buf, sizeof(buf), "%s.%s", base, ext);
137         if (! (out = fopen(buf, mode))) {
138                 fprintf(stderr, "Cannot open file %s in mode %s\n", buf, mode);
139                 assert(0);
140         }
141         return out;
142 }
143
144 #ifdef DUMP_MATRICES
145 /**
146  * Dump the raw matrices of the problem to a file for debugging.
147  */
148 static void pi_dump_matrices(problem_instance_t *pi) {
149         int i;
150         FILE *out = ffopen(pi->name, "matrix", "wt");
151
152         DBG((dbg, LEVEL_1, "Dumping raw...\n"));
153         fprintf(out, "\n\nx-names =\n");
154         for (i=0; i<pi->x_dim; ++i)
155                 fprintf(out, "%5d %2d\n", pi->x[i].n, pi->x[i].c);
156
157         fprintf(out, "\n\n-Q =\n");
158         matrix_dump(pi->Q, out, -1);
159
160         fprintf(out, "\n\nA =\n");
161         matrix_dump(pi->A, out, 1);
162
163         fprintf(out, "\n\nB =\n");
164         matrix_dump(pi->B, out, 1);
165
166         fclose(out);
167 }
168 #endif
169
170 #ifdef DUMP_LP
171 /**
172  * Dumps the problem instance as a MILP. The original problem is transformed into:
173  * min f = es - Mex
174  * udN:  Qx -y -s +Me = 0
175  *       Ax  = e
176  *       Bx <= e
177  *        y <= 2M(e-x)
178  *        x \in N   y, s >= 0
179  *
180  * with M >= max sum Q'ij * x_j
181  *            i   j
182  */
183 static void pi_dump_lp(problem_instance_t *pi) {
184         int i, max_abs_Qij;
185         matrix_elem_t *e;
186         FILE *out = ffopen(pi->name, "lpo", "wt");
187
188         DBG((dbg, LEVEL_1, "Dumping lp_org...\n"));
189         /* calc the big M for Q */
190         max_abs_Qij = pi->maxQij;
191         if (-pi->minQij > max_abs_Qij)
192                 max_abs_Qij = -pi->minQij;
193         pi->bigM = pi->A_dim * max_abs_Qij;
194         DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
195
196         /* generate objective function */
197         fprintf(out, "min: ");
198         for (i=0; i<pi->x_dim; ++i)
199                 fprintf(out, "+s%d_%d -%dx%d_%d ", pi->x[i].n, pi->x[i].c, pi->bigM, pi->x[i].n, pi->x[i].c);
200         fprintf(out, ";\n\n");
201
202         /* constraints for former objective function */
203         for (i=0; i<pi->x_dim; ++i)     {
204                 matrix_foreach_in_row(pi->Q, i, e) {
205                         int Qio = e->val;
206                                 if (Qio == 1)
207                                         fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
208                                 else if(Qio == -1)
209                                         fprintf(out, "-x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
210                                 else
211                                         fprintf(out, "%+dx%d_%d ", Qio, pi->x[e->col].n, pi->x[e->col].c);
212                 }
213                 fprintf(out, "-y%d_%d -s%d_%d +%d= 0;\n", pi->x[i].n, pi->x[i].c, pi->x[i].n, pi->x[i].c, pi->bigM);
214         }
215         fprintf(out, "\n\n");
216
217         /* constraints for (special) complementary condition */
218         for (i=0; i<pi->x_dim; ++i)
219                 fprintf(out, "y%d_%d <= %d - %dx%d_%d;\n", pi->x[i].n, pi->x[i].c, 2*pi->bigM, 2*pi->bigM, pi->x[i].n, pi->x[i].c);
220         fprintf(out, "\n\n");
221
222         /* knapsack constraints */
223         for (i=0; i<pi->A_dim; ++i)     {
224                 matrix_foreach_in_row(pi->Q, i, e)
225                         fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
226                 fprintf(out, " = 1;\n");
227         }
228         fprintf(out, "\n\n");
229
230         /* interference graph constraints */
231         for (i=0; i<pi->B_dim; ++i)     {
232                 matrix_foreach_in_row(pi->Q, i, e)
233                         fprintf(out, "+x%d_%d ", pi->x[e->col].n, pi->x[e->col].c);
234                 fprintf(out, " <= 1;\n");
235         }
236         fprintf(out, "\n\n");
237
238         /* integer constraints */
239         fprintf(out, "int x%d_%d", pi->x[0].n, pi->x[0].c);
240         for (i=1; i<pi->x_dim; ++i)
241                 fprintf(out, ", x%d_%d", pi->x[i].n, pi->x[i].c);
242         fprintf(out, ";\n");
243
244         fclose(out);
245 }
246 #endif
247
248 #ifdef DUMP_MPS
249 /**
250  * Dumps an mps file representing the problem. This is NOT the old-style,
251  * fixed-column format. Some white spaces are important, in general spaces
252  * are separators, MARKER-lines are used in COLUMN section to define binaries.
253  */
254 //BETTER use last 2 fields in COLUMNS section
255 static void pi_dump_mps(problem_instance_t *pi) {
256         int i, max_abs_Qij;
257         matrix_elem_t *e;
258         FILE *out = ffopen(pi->name, "mps", "wt");
259
260         DBG((dbg, LEVEL_1, "Dumping mps...\n"));
261         max_abs_Qij = pi->maxQij;
262         if (-pi->minQij > max_abs_Qij)
263                 max_abs_Qij = -pi->minQij;
264         pi->bigM = pi->A_dim * max_abs_Qij;
265         DBG((dbg, LEVEL_2, "BigM = %d\n", pi->bigM));
266
267         fprintf(out, "NAME %s\n", pi->name);
268
269         fprintf(out, "ROWS\n");
270         fprintf(out, " N obj\n");
271         for (i=0; i<pi->x_dim; ++i)
272                 fprintf(out, " E cQ%d\n", i);
273         for (i=0; i<pi->A_dim; ++i)
274                 fprintf(out, " E cA%d\n", i);
275         for (i=0; i<pi->B_dim; ++i)
276                 fprintf(out, " L cB%d\n", i);
277         for (i=0; i<pi->x_dim; ++i)
278                 fprintf(out, " L cy%d\n", i);
279
280         fprintf(out, "COLUMNS\n");
281         /* the x vars come first */
282         /* mark them as binaries */
283         fprintf(out, "    MARKI0\t'MARKER'\t'INTORG'\n");
284 #ifdef USE_SOS
285         int sos_cnt = 0;
286         fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
287 #endif
288         for (i=0; i<pi->x_dim; ++i) {
289 #ifdef USE_SOS
290                 if (i>0 && pi->x[i].n != pi->x[i-1].n) {
291                         fprintf(out, "    SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
292                         fprintf(out, " S1 SOS_%d\t'MARKER'\t'SOSORG'\n", sos_cnt++);
293                 }
294 #endif
295                 /* participation in objective */
296                 fprintf(out, "    x%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, -pi->bigM);
297                 /* in Q */
298                 matrix_foreach_in_col(pi->Q, i, e)
299                         fprintf(out, "    x%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
300                 /* in A */
301                 matrix_foreach_in_col(pi->A, i, e)
302                         fprintf(out, "    x%d_%d\tcA%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
303                 /* in B */
304                 matrix_foreach_in_col(pi->B, i, e)
305                         fprintf(out, "    x%d_%d\tcB%d\t%d\n", pi->x[i].n, pi->x[i].c, e->row, e->val);
306                 /* in y */
307                 fprintf(out, "    x%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 2*pi->bigM);
308         }
309
310 #ifdef USE_SOS
311         fprintf(out, "    SOS_%d\t'MARKER'\t'SOSEND'\n", sos_cnt++);
312 #endif
313         fprintf(out, "    MARKI1\t'MARKER'\t'INTEND'\n"); /* end of marking */
314
315         /* next the s vars */
316         for (i=0; i<pi->x_dim; ++i) {
317                 /* participation in objective */
318                 fprintf(out, "    s%d_%d\tobj\t%d\n", pi->x[i].n, pi->x[i].c, 1);
319                 /* in Q */
320                 fprintf(out, "    s%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
321         }
322
323         /* next the y vars */
324         for (i=0; i<pi->x_dim; ++i) {
325                 /* in Q */
326                 fprintf(out, "    y%d_%d\tcQ%d\t%d\n", pi->x[i].n, pi->x[i].c, i, -1);
327                 /* in y */
328                 fprintf(out, "    y%d_%d\tcy%d\t%d\n", pi->x[i].n, pi->x[i].c, i, 1);
329         }
330
331         fprintf(out, "RHS\n");
332         for (i=0; i<pi->x_dim; ++i)
333                 fprintf(out, "    rhs\tcQ%d\t%d\n", i, -pi->bigM);
334         for (i=0; i<pi->A_dim; ++i)
335                 fprintf(out, "    rhs\tcA%d\t%d\n", i, 1);
336         for (i=0; i<pi->B_dim; ++i)
337                 fprintf(out, "    rhs\tcB%d\t%d\n", i, 1);
338         for (i=0; i<pi->x_dim; ++i)
339                 fprintf(out, "    rhs\tcy%d\t%d\n", i, 2*pi->bigM);
340
341         fprintf(out, "ENDATA\n");
342         fclose(out);
343
344         out = ffopen(pi->name, "mst", "wt");
345         fprintf(out, "NAME\n");
346         for (i=0; i<pi->x_dim; ++i) {
347                 int val, n, c;
348                 n = pi->x[i].n;
349                 c = pi->x[i].c;
350                 if (get_irn_color(get_irn_for_graph_nr(pi->irg, n)) == c)
351                         val = 1;
352                 else
353                         val = 0;
354                 fprintf(out, "    x%d_%d\t%d\n", n, c, val);
355         }
356         fprintf(out, "ENDATA\n");
357         fclose(out);
358 }
359 #endif
360
361 #ifdef DO_SOLVE
362 /**
363  * Invoke an external solver
364  */
365 static void pi_solve_ilp(problem_instance_t *pi) {
366         FILE *out;
367
368         /* write command file for CPLEX */
369         out = ffopen(pi->name, "cmd", "wt");
370         fprintf(out, "read %s.mps\n", pi->name);
371         fprintf(out, "read %s.mst\n", pi->name);
372         fprintf(out, "set mip strategy mipstart 1\n");
373         fprintf(out, "optimize\n");
374         fprintf(out, "set logfile %s.sol\n", pi->name);
375         fprintf(out, "display solution variables 1-%d\n", pi->x_dim);
376         fprintf(out, "set logfile cplex.log\n");
377         fprintf(out, "quit\n");
378         fclose(out);
379
380         /* write expect-file for copying problem to RZ */
381         out = ffopen(EXPECT_FILENAME, "exp", "wt");
382         fprintf(out, "#! /usr/bin/expect\n");
383         fprintf(out, "spawn scp %s.mps %s.mst %s.cmd %s:\n", pi->name, pi->name, pi->name, SSH_USER_HOST_PATH);
384         fprintf(out, "expect \":\"\n");
385         fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
386         fprintf(out, "interact\n");
387
388         fprintf(out, "spawn ssh %s \"./cplex90 < %s.cmd\"\n", SSH_USER_HOST_PATH, pi->name);
389         fprintf(out, "expect \":\"\n");
390         fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
391         fprintf(out, "interact\n");
392
393         fprintf(out, "spawn scp %s:%s.sol .\n", SSH_USER_HOST_PATH, pi->name);
394         fprintf(out, "expect \":\"\n");
395         fprintf(out, "send \"%s\\n\"\n", SSH_PASSWD);
396         fprintf(out, "interact\n");
397         fclose(out);
398
399         /* call the expect script */
400         chmod(EXPECT_FILENAME ".exp", 0700);
401         system(EXPECT_FILENAME ".exp");
402 }
403
404 /**
405  * Sets the colors of irns according to the values of variables found in the
406  * output file of the solver.
407  */
408 static void pi_apply_solution(problem_instance_t *pi) {
409         FILE *in = ffopen(pi->name, "sol", "rt");
410
411         if (!in)
412                 return;
413         DBG((dbg, LEVEL_1, "Applying solution...\n"));
414         while (!feof(in)) {
415                 char buf[1024];
416                 int num = -1, col = -1, val = -1;
417                 if (fscanf(in, "x%d_%d %d.%s\n", &num, &col, &val, buf) != 3) {
418                         while(fscanf(in, "%1020s\n", buf) != 1);
419                         continue;
420                 }
421                 if (val == 1) {
422                         DBG((dbg, LEVEL_1, "x%d_%d = %d\n", num, col, val));
423                         set_irn_color(get_irn_for_graph_nr(pi->irg, num), col);
424                 }
425         }
426         fclose(in);
427 }
428 #endif /* DO_SOLVE */
429
430 #ifdef DELETE_FILES
431 static void pi_delete_files(problem_instance_t *pi) {
432         char buf[1024];
433         int end = snprintf(buf, sizeof(buf), "%s", pi->name);
434 #ifdef DUMP_MATRICES
435         snprintf(buf+end, sizeof(buf)-end, ".matrix");
436         remove(buf);
437 #endif
438 #ifdef DUMP_MPS
439         snprintf(buf+end, sizeof(buf)-end, ".mps");
440         remove(buf);
441         snprintf(buf+end, sizeof(buf)-end, ".mst");
442         remove(buf);
443         snprintf(buf+end, sizeof(buf)-end, ".cmd");
444         remove(buf);
445         remove(EXPECT_FILENAME ".exp");
446 #endif
447 #ifdef DUMP_LP
448         snprintf(buf+end, sizeof(buf)-end, ".lp");
449         remove(buf);
450 #endif
451 }
452 #endif
453
454 /**
455  * Collects all irns in currently processed register class
456  */
457 static void pi_collect_x_names(ir_node *block, void *env) {
458         problem_instance_t *pi = env;
459         struct list_head *head = &get_ra_block_info(block)->border_head;
460         border_t *curr;
461
462         list_for_each_entry_reverse(border_t, curr, head, list)
463                 if (curr->is_def && curr->is_real) {
464                         x_name_t xx;
465                         pi->A_dim++;                    /* one knapsack constraint for each node */
466
467                         xx.n = get_irn_graph_nr(curr->irn);
468                         pi_set_first_pos(pi, xx.n, pi->x_dim);
469                         //TODO iterate over all possible colors !!MUST BE IN ORDER!!
470                         for (xx.c=0; xx.c<MAX_COLORS; ++xx.c) {
471                                 if (!is_possible_color(irn, xx.c))
472                                         continue;
473                                 DBG((dbg, LEVEL_2, "Adding %n %d\n", curr->irn, xx.c));
474                                 obstack_grow(&pi->ob, &xx, sizeof(xx));
475                                 pi->x_dim++;            /* one x variable for each node and color */
476                         }
477                 }
478 }
479
480 /**
481  * Checks if all nodes in living are live_out in block block.
482  */
483 static INLINE int all_live_out(ir_node *block, pset *living) {
484         ir_node *n;
485         for (n = pset_first(living); n; n = pset_next(living))
486                 if (!is_live_out(block, n)) {
487                         pset_break(living);
488                         return 0;
489                 }
490         return 1;
491 }
492
493 /**
494  * Finds cliques in the interference graph, considering only nodes
495  * for which the color pi->curr_color is possible. Finds only 'maximal-cliques',
496  * viz cliques which are not conatained in another one.
497  * This is used for the matrix B.
498  */
499 static void pi_clique_finder(ir_node *block, void *env) {
500         problem_instance_t *pi = env;
501         enum phase_t {growing, shrinking} phase = growing;
502         struct list_head *head = &get_ra_block_info(block)->border_head;
503         border_t *b;
504         pset *living = pset_new_ptr(SLOTS_LIVING);
505
506         list_for_each_entry_reverse(border_t, b, head, list) {
507                 const ir_node *irn = b->irn;
508                 if (!is_possible_color(n, pi->curr_col))
509                         continue;
510
511                 if (b->is_def) {
512                         DBG((dbg, LEVEL_2, "Def %n\n", irn));
513                         pset_insert_ptr(living, irn);
514                         phase = growing;
515                 } else { /* is_use */
516                         DBG((dbg, LEVEL_2, "Use %n\n", irn));
517
518                         /* before shrinking the set, store the current 'maximum' clique;
519                          * do NOT if clique is a single node
520                          * do NOT if all values are live_out */
521                         if (phase == growing && pset_count(living) >= 2 && !all_live_out(block, living)) {
522                                 ir_node *n;
523                                 for (n = pset_first(living); n; n = pset_next(living)) {
524                                         int pos = pi_get_pos(pi, get_irn_graph_nr(n), pi->curr_color);
525                                         matrix_set(pi->B, pi->curr_row, pos, 1);
526                                         DBG((dbg, LEVEL_2, "B[%d, %d] := %d\n", pi->curr_row, pos, 1));
527                                 }
528                                 pi->curr_row++;
529                         }
530                         pset_remove_ptr(living, irn);
531                         phase = shrinking;
532                 }
533         }
534
535         del_pset(living);
536 }
537
538 /**
539  * Generate the initial problem matrices and vectors.
540  */
541 static problem_instance_t *new_pi(const copy_opt_t *co) {
542         DBG((dbg, LEVEL_1, "Generating new instance...\n"));
543         problem_instance_t *pi = calloc(1, sizeof(*pi));
544         pi->irg = co->irg;
545         pi->name =      get_entity_name(get_irg_entity(co->irg));
546         pi->num2pos = new_set(set_cmp_num2pos, SLOTS_NUM2POS);
547         pi->bigM = 1;
548
549         /* Vector x
550          * one entry per node and possible color */
551         obstack_init(&pi->ob);
552         dom_tree_walk_irg(co->irg, pi_collect_x_names, NULL, &pi->ob);
553         pi->x = obstack_finish(&pi->ob);
554
555         /* Matrix Q
556          * weights for the 'same-color-optimization' target */
557         {
558                 unit_t *curr;
559                 pi->Q = new_matrix(pi->x_dim, pi->x_dim);
560
561                 list_for_each_entry(unit_t, curr, &co->units, units) {
562                         const ir_node *root, *arg;
563                         int rootnr, argnr;
564                         unsigned rootpos, argpos;
565                         int i;
566
567                         root = curr->nodes[0];
568                         rootnr = get_irn_graph_nr(root);
569                         rootpos = pi_get_first_pos(pi, rootnr);
570                         for (i = 1; i < curr->node_count; ++i) {
571                                 int weight = -get_weight(root, arg);
572                                 arg = curr->nodes[i];
573                                 argnr = get_irn_graph_nr(arg);
574                                 argpos = pi_get_first_pos(pi, argnr);
575
576                                 DBG((dbg, LEVEL_2, "Q[%n, %n] := %d\n", root, arg, weight));
577                                 /* for all colors root and arg have in common, set the weight for
578                                  * this pair in the objective function matrix Q */
579                                 while (rootpos < pi->x_dim && argpos < pi->x_dim &&
580                                                 pi->x[rootpos].n == rootnr && pi->x[argpos].n == argnr) {
581                                         if (pi->x[rootpos].c < pi->x[argpos].c)
582                                                 ++rootpos;
583                                         else if (pi->x[rootpos].c > pi->x[argpos].c)
584                                                 ++argpos;
585                                         else {
586                                                 matrix_set(pi->Q, rootpos++, argpos++, weight);
587
588                                                 if (weight < pi->minQij) {
589                                                         DBG((dbg, LEVEL_2, "minQij = %d\n", weight));
590                                                         pi->minQij = weight;
591                                                 }
592                                                 if (weight > pi->maxQij) {
593                                                         DBG((dbg, LEVEL_2, "maxQij = %d\n", weight));
594                                                         pi->maxQij = weight;
595                                                 }
596                                         }
597                                 }
598                         }
599                 }
600         }
601
602         /* Matrix A
603          * knapsack constraint for each node */
604         {
605                 int row = 0, col = 0;
606                 pi->A = new_matrix(pi->A_dim, pi->x_dim);
607                 while (col < pi->x_dim) {
608                         int curr_n = pi->x[col].n;
609                         while (col < pi->x_dim && pi->x[col].n == curr_n) {
610                                 DBG((dbg, LEVEL_2, "A[%d, %d] := %d\n", row, col, 1));
611                                 matrix_set(pi->A, row, col++, 1);
612                         }
613                         ++row;
614                 }
615                 assert(row == pi->A_dim);
616         }
617
618         /* Matrix B
619          * interference constraints using exactly those cliques not contained in others. */
620         {
621                 int color, expected_clipques = pi->A_dim/3 * MAX_COLORS;
622                 pi->B = new_matrix(expected_clipques, pi->x_dim);
623                 for (color = 0; color < MAX_COLORS; ++color) {
624                         pi->curr_color = color;
625                         dom_tree_walk_irg(pi->irg, pi_clique_finder, NULL, pi);
626                 }
627                 pi->B_dim = matrix_get_rowcount(pi->B);
628         }
629
630         return pi;
631 }
632
633 /**
634  * clean the problem instance
635  */
636 static void free_pi(problem_instance_t *pi) {
637         del_matrix(pi->Q);
638         del_matrix(pi->A);
639         del_matrix(pi->B);
640         del_set(pi->num2pos);
641         obstack_free(&pi->ob, NULL);
642         free(pi);
643 }
644
645 void co_ilp_opt(copy_opt_t *co) {
646         dbg = firm_dbg_register("ir.be.copyoptilp");
647         firm_dbg_set_mask(dbg, DEBUG_LVL);
648
649         problem_instance_t *pi = new_pi(co);
650
651 #ifdef DUMP_MATRICES
652         pi_dump_matrices(pi);
653 #endif
654
655 #ifdef DUMP_LP
656         pi_dump_lp(pi);
657 #endif
658
659 #ifdef DUMP_MPS
660         pi_dump_mps(pi);
661 #endif
662
663 #ifdef DO_SOLVE
664         pi_solve_ilp(pi);
665         pi_apply_solution(pi);
666 #endif
667
668 #ifdef DELETE_FILES
669         pi_delete_files(pi);
670 #endif
671
672         free_pi(pi);
673 }