Made everything really kaputt
[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  * CVS-ID:      $Id$
7  */
8 #ifdef HAVE_CONFIG_H
9 #include "config.h"
10 #endif
11 #ifdef HAVE_ALLOCA_H
12 #include <alloca.h>
13 #endif
14 #ifdef HAVE_MALLOC_H
15 #include <malloc.h>
16 #endif
17
18 #define PATH_CONSTRAINTS_FOR_CLASSES
19 #undef PRECOLOR_MAX_CLIQUE
20 #undef NO_NULL_COLORS
21 #undef NO_NULL_COLORS_EXTRA_CSTS
22 #undef NO_NULL_COLORS_WITH_COSTS
23 #if (defined(NO_NULL_COLORS_EXTRA_CSTS) || defined(NO_NULL_COLORS_WITH_COSTS)) && !defined(NO_NULL_COLORS)
24 #error Chose your weapon!
25 #endif
26
27 #include "irprog.h"
28
29 #include <lpp/lpp.h>
30 #include <lpp/lpp_net.h>
31 #include <lpp/lpp_cplex.h>
32 #include <lpp/lpp_remote.h>
33 #include "xmalloc.h"
34 #include "pset.h"
35 #include "irdom_t.h"
36 #include "iredges_t.h"
37 #include "bechordal_t.h"
38 #include "becopyopt.h"
39 #include "becopystat.h"
40 #include "besched_t.h"
41 #include "phiclass.h"
42
43 #define LPP_HOST "i44pc52"
44 #define LPP_SOLVER "cplex"
45
46 #undef DUMP_MPS
47 static firm_dbg_module_t *dbg = NULL;
48
49 #define MAX(a,b) ((a<b)?(b):(a))
50 #define MIN(a,b) ((a<b)?(a):(b))
51 #define EPSILON 0.00001
52 #define SLOTS_LIVING 32
53
54 typedef struct _simpl_t {
55         struct list_head chain;
56         ir_node *irn;
57 } simpl_t;
58
59 typedef struct _problem_instance_t {
60         const copy_opt_t *co;                   /** the copy_opt problem */
61         /* problem size reduction removing simple nodes */
62         struct list_head simplicials;   /**< holds all simpl_t's in right order to color*/
63         pset *removed;                                  /**< holds all removed simplicial irns */
64         /* lp problem */
65         lpp_t *curr_lp;                                 /**< points to the problem currently used */
66         lpp_t *dilp;                                    /**< problem formulation directly as milp */
67 #ifdef NO_NULL_COLORS_EXTRA_CSTS
68         int first_nnc_cst_idx;                  /**< the first index of a constraint belonging to no-null-colors stuff*/
69 #endif
70         int first_nnc_var_idx;                  /**< the first index of a constraint belonging to no-null-colors stuff*/
71
72         int cst_counter, first_x_var, last_x_var;
73         char buf[32];
74         int all_simplicial;
75         pset *done;
76 } problem_instance_t;
77
78 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
79
80 #define is_color_possible(irn,color) arch_reg_is_allocatable(get_arch_env(pi->co), irn, -1, arch_register_for_index(pi->co->chordal_env->cls, color))
81
82 /*
83  * Some stuff for variable name handling.
84  */
85 #define mangle_cst(buf, prefix, nr) \
86                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
87
88 #define mangle_var1(buf, prefix, color) \
89                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (color))
90
91 #define mangle_var2(buf, prefix, node_nr, color) \
92                         snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
93
94 #define mangle_var3(buf, prefix, n1, n2, col) \
95                         snprintf((buf), sizeof(buf), "%c%d_%d_%d", (prefix), (n1), (n2), (col))
96
97 #define mangle_var_irn(buf, prefix, irn, color) \
98                         mangle_var2((buf), (prefix), get_irn_graph_nr(irn), (color))
99
100 #define split_var(var, nnr, col) \
101                         sscanf(var, "x%d_%d", (nnr), (col))
102
103
104 /**
105  * Checks if a node is simplicial in the graph
106  * heeding the already removed nodes.
107  */
108 static INLINE int pi_is_simplicial(problem_instance_t *pi, const ir_node *ifn) {
109         int i, o, size = 0;
110         ir_node **all, *curr;
111         be_ifg_t *ifg = pi->co->chordal_env->ifg;
112         void *iter = be_ifg_iter_alloca(ifg);
113
114         all = alloca(be_ifg_degree(ifg, ifn) * sizeof(*all));
115
116         /* get all non-removed neighbors */
117         be_ifg_foreach_neighbour(ifg, iter, ifn, curr)
118                 if (!is_removed(curr))
119                         all[size++] = curr;
120
121         /* check if these form a clique */
122         for (i=0; i<size; ++i)
123                 for (o=i+1; o<size; ++o)
124                         if (!be_ifg_connected(ifg, all[i], all[o]))
125                                 return 0;
126
127         /* all edges exist so this is a clique */
128         return 1;
129 }
130
131 static int irn_cmp(const void *a, const void *b, size_t n)
132 {
133         return a != b;
134 }
135
136 /**
137  * Iterative finds and 'removes' from the graph all nodes which are
138  * simplicial AND not member of a equal-color-wish
139  */
140 static void pi_find_simplicials(problem_instance_t *pi) {
141         ir_node *irn;
142         int redo = 1;
143         int n_nodes = 0;
144         const be_ifg_t *ifg = pi->co->chordal_env->ifg;
145         void *iter = be_ifg_iter_alloca(ifg);
146
147         DBG((dbg, LEVEL_2, "Find simlicials...\n"));
148
149         while (redo) {
150                 redo = 0;
151                 be_ifg_foreach_node(ifg, iter, irn) {
152                         if (!is_removed(irn) && !is_optimizable(get_arch_env(pi->co), irn) && !is_optimizable_arg(pi->co, irn)) {
153                         if (pi_is_simplicial(pi, irn)) {
154                                         simpl_t *s = xmalloc(sizeof(*s));
155                                         s->irn = irn;
156                                         list_add(&s->chain, &pi->simplicials);
157                                         pset_insert_ptr(pi->removed, irn);
158                                         redo = 1;
159                                         DBG((dbg, LEVEL_2, " Removed %+F\n", irn));
160                         }
161                         }
162                 }
163         }
164
165         /* TODO: Count inside the last look */
166         be_ifg_foreach_node(ifg, iter, irn) {
167                 n_nodes++;
168         }
169
170         if (n_nodes == pset_count(pi->removed))
171                 pi->all_simplicial = 1;
172 }
173
174 #ifdef NO_NULL_COLORS
175 static void pi_add_constr_no_null_colors(problem_instance_t *pi) {
176         int cst_counter=0, col, var_idx, cst_idx;
177         int n_colors = pi->co->chordal_env->cls->n_regs;
178         char buf[40];
179
180         for (col = 0; col < n_colors; ++col) {
181                 mangle_var1(buf, 'u', col);
182 #ifdef NO_NULL_COLORS_WITH_COSTS
183                 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (double) (1 << (col+1)) );
184 #else
185                 var_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, 1.0 / (2.0 * n_colors) );
186 #endif
187                 if (!pi->first_nnc_var_idx)
188                         pi->first_nnc_var_idx = var_idx;
189         }
190
191 #ifdef NO_NULL_COLORS_EXTRA_CSTS
192         for (col = 0; col < n_colors; ++col) {
193                 mangle_cst(buf, 'U', cst_counter++);
194                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
195                 if (!pi->first_nnc_cst_idx)
196                         pi->first_nnc_cst_idx = cst_idx;
197                 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col, -1);
198         }
199 #endif
200
201 #ifndef NO_NULL_COLORS_WITH_COSTS
202         for (col = 0; col < n_colors - 1; ++col) {
203                 mangle_cst(buf, 'U', cst_counter++);
204                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 0);
205                 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col  ,  1);
206                 lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+col+1, -1);
207         }
208 #endif
209
210 }
211 #endif
212
213 /**
214  * Add coloring-force conditions
215  * Matrix A: knapsack constraint for each node
216  */
217 static void pi_add_constr_A(problem_instance_t *pi) {
218         pmap_entry *pme;
219
220         DBG((dbg, LEVEL_2, "Add A constraints...\n"));
221         /* iterate over all blocks */
222         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
223                 struct list_head *head = pme->value;
224                 border_t *curr;
225                 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
226
227                 list_for_each_entry_reverse(border_t, curr, head, list)
228                         if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
229                                 int cst_idx, nnr, col;
230
231                                 nnr = get_irn_graph_nr(curr->irn);
232                                 mangle_cst(pi->buf, 'A', nnr);
233                                 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_equal, 1);
234
235                                 /* iterate over all possible colors in order */
236                                 bitset_clear_all(pos_regs);
237                                 arch_get_allocatable_regs(get_arch_env(pi->co), curr->irn, -1, pi->co->chordal_env->cls, pos_regs);
238                                 bitset_foreach(pos_regs, col) {
239                                         int var_idx;
240                                         mangle_var2(pi->buf, 'x', nnr, col);
241                                         var_idx = lpp_add_var(pi->curr_lp, pi->buf, lpp_binary, 0);
242                                         if (!pi->first_x_var)
243                                                 pi->first_x_var = var_idx;
244                                         pi->last_x_var = var_idx;
245                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
246 #ifdef NO_NULL_COLORS_EXTRA_CSTS
247                                         lpp_set_factor_fast(pi->curr_lp, pi->first_nnc_cst_idx+col, var_idx, 1);
248 #endif
249                                 }
250                         }
251         }
252 }
253
254 /**
255  * Checks if all nodes in @p living are live in in block @p block.
256  * @return 1 if all are live in
257  *         0 else
258  */
259 static INLINE int all_live_in(ir_node *block, pset *living) {
260         ir_node *n;
261         for (n = pset_first(living); n; n = pset_next(living))
262                 if (!is_live_in(block, n)) {
263                         pset_break(living);
264                         return 0;
265                 }
266         return 1;
267 }
268
269 /**
270  * Finds cliques in the interference graph, considering only nodes
271  * for which the color @p color is possible. Finds only 'maximal-cliques',
272  * viz cliques which are not contained in another one.
273  * Matrix B: interference constraints using cliques
274  */
275 static void pi_add_constr_B(problem_instance_t *pi, int color) {
276         enum phase_t {growing, shrinking} phase = growing;
277         border_t *b;
278         pmap_entry *pme;
279         pset *living = pset_new_ptr(SLOTS_LIVING);
280
281         DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
282         /* iterate over all blocks */
283         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
284                 ir_node *block = pme->key;
285                 struct list_head *head = pme->value;
286
287                 list_for_each_entry_reverse(border_t, b, head, list) {
288                         const ir_node *irn = b->irn;
289                         if (is_removed(irn) || !is_color_possible(irn, color))
290                                 continue;
291
292                         if (b->is_def) {
293                                 DBG((dbg, LEVEL_2, "Def %n\n", irn));
294                                 pset_insert_ptr(living, irn);
295                                 phase = growing;
296                         } else { /* is_use */
297                                 DBG((dbg, LEVEL_2, "Use %n\n", irn));
298
299                                 /* before shrinking the set, store the current 'maximum' clique;
300                                  * do NOT if clique is a single node
301                                  * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
302                                 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
303                                         int cst_idx;
304                                         ir_node *n;
305                                         mangle_cst(pi->buf, 'B', pi->cst_counter);
306 #ifdef NO_NULL_COLORS
307                                         cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 0);
308 #else
309                                         cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, lpp_less, 1);
310 #endif
311                                         for (n = pset_first(living); n; n = pset_next(living)) {
312                                                 int var_idx;
313                                                 mangle_var_irn(pi->buf, 'x', n, color);
314                                                 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
315                                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
316                                         }
317 #ifdef NO_NULL_COLORS
318                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, pi->first_nnc_var_idx+color, -1.0);
319 #endif
320                                         pi->cst_counter++;
321                                 }
322                                 pset_remove_ptr(living, irn);
323                                 phase = shrinking;
324                         }
325                 }
326         }
327         assert(0 == pset_count(living));
328         del_pset(living);
329 }
330
331 /**
332  * Generates constraints which interrelate x with y variables.
333  * x1 and x2 have the different colors ==> y_12 = 1
334  */
335 static void pi_add_constr_E(problem_instance_t *pi) {
336         unit_t *curr;
337         bitset_t *root_regs, *arg_regs, *work_regs;
338         int cst_counter = 0;
339         unsigned nregs = pi->co->chordal_env->cls->n_regs;
340         root_regs = bitset_alloca(nregs);
341         arg_regs = bitset_alloca(nregs);
342         work_regs = bitset_alloca(nregs);
343
344         DBG((dbg, LEVEL_2, "Add E constraints...\n"));
345         /* for all roots of optimization units */
346         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
347                 ir_node *root, *arg;
348                 int rootnr, argnr, color;
349                 int y_idx, i;
350                 char buf[32];
351
352                 root = curr->nodes[0];
353                 rootnr = get_irn_graph_nr(root);
354                 bitset_clear_all(root_regs);
355                 arch_get_allocatable_regs(get_arch_env(pi->co), root, -1, pi->co->chordal_env->cls, root_regs);
356
357                 /* for all arguments of root */
358                 for (i = 1; i < curr->node_count; ++i) {
359                         arg = curr->nodes[i];
360                         argnr = get_irn_graph_nr(arg);
361                         bitset_clear_all(arg_regs);
362                         arch_get_allocatable_regs(get_arch_env(pi->co), arg, -1, pi->co->chordal_env->cls, arg_regs);
363
364                         /* Introduce new variable and set factor in objective function */
365                         mangle_var2(buf, 'y', rootnr, argnr);
366                         y_idx = lpp_add_var(pi->curr_lp, buf, lpp_binary, curr->costs[i]);
367
368                         /* set starting value */
369                         lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
370
371                         /* For all colors root and arg have in common, add 2 constraints to E */
372                         bitset_copy(work_regs, root_regs);
373                         bitset_and(work_regs, arg_regs);
374                         bitset_foreach(work_regs, color) {
375                                 int root_idx, arg_idx, cst_idx;
376                                 mangle_var2(buf, 'x', rootnr, color);
377                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
378                                 mangle_var2(buf, 'x', argnr, color);
379                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
380
381                                 /* add root-arg-y <= 0 */
382                                 mangle_cst(buf, 'E', cst_counter++);
383                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
384                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
385                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
386                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
387
388                                 /* add arg-root-y <= 0 */
389                                 mangle_cst(buf, 'E', cst_counter++);
390                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
391                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
392                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
393                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
394                         }
395                         /* For all colors root and arg have "disjunct", add 1 constraints to E.
396                          * If root gets a color the arg is not possible to get then they will
397                          * definetly get different colors. So y has to be 1.
398                          * Vice versa for arg.
399                          */
400                         bitset_copy(work_regs, root_regs);
401                         bitset_xor(work_regs, arg_regs);
402                         bitset_foreach(work_regs, color) {
403                                 int root_idx, arg_idx, cst_idx;
404                                 mangle_var2(buf, 'x', rootnr, color);
405                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
406                                 mangle_var2(buf, 'x', argnr, color);
407                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
408
409                                 mangle_cst(buf, 'E', cst_counter++);
410                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_less, 0);
411                                 if (bitset_is_set(root_regs, color)) {
412                                         /* add root-y <= 0 */
413                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
414                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
415                                 } else {
416                                         assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
417                                         /* add arg-y <= 0 */
418                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
419                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
420                                 }
421                         }
422                 }
423         }
424 }
425
426 static INLINE int get_costs(problem_instance_t *pi, ir_node *phi, ir_node *irn) {
427         int i;
428         unit_t *curr;
429         /* search optimization unit for phi */
430         list_for_each_entry(unit_t, curr, &pi->co->units, units)
431                 if (curr->nodes[0] == phi) {
432                         for (i=1; i<curr->node_count; ++i)
433                                 if (curr->nodes[i] == irn)
434                                         return curr->costs[i];
435                         assert(0 && "irn must occur in this ou");
436                 }
437         assert(0 && "phi must be found in a ou");
438         return 0;
439 }
440
441 static void clique_path_walker(ir_node *block, void *env) {
442         problem_instance_t *pi = env;
443         int count, arity, row, col, other_row, *costs;
444         ir_node **phis, *phi, *irn, **phi_matrix;
445         pset *done;
446         bitset_t *candidates;
447
448         /* Count all phi nodes of this block */
449         for (count=0, irn = sched_first(block); is_Phi(irn); irn = sched_next(irn))
450                 count++;
451
452         /* We at least 2 phi nodes for this class of inequalities */
453         if (count < 2)
454                 return;
455
456         /* Build the \Phi-Matrix */
457         arity = get_irn_arity(sched_first(block));
458         phis = alloca(count * sizeof(*phis));
459         costs = alloca(count * sizeof(costs));
460         phi_matrix = alloca(count*arity * sizeof(*phi_matrix));
461         candidates = bitset_alloca(count);
462
463         phi = sched_first(block);
464         for (row=0; row<count; ++row) {
465                 phis[row] = phi;
466                 for (col=0; col<arity; ++col) {
467                         ir_node *arg = get_irn_n(phi, col);
468                         /* Sort out all arguments interfering with its phi */
469                         if (nodes_interfere(pi->co->chordal_env, phi, arg)) {
470                                 phi_matrix[row*arity + col] =  NULL;
471                         } else
472                                 phi_matrix[row*arity + col] =  arg;
473                 }
474                 phi = sched_next(phi);
475         }
476
477         /* Now find the interesting patterns in the matrix:
478          * All nodes which are used at least twice in a column. */
479         /* columnwise ... */
480         for (col=0; col<arity; ++col) {
481                 done = pset_new_ptr_default();
482                 for (row=0; row<count; ++row) {
483                         irn = phi_matrix[row*arity + col];
484                         /*
485                          * is this an interfering arg (NULL)
486                          * or has the irn already been processed in this col?
487                          */
488                         if (!irn || pset_find_ptr(done, irn))
489                                 continue;
490                         else
491                                 pset_insert_ptr(done, irn);
492
493                         /* insert irn in candidates */
494                         bitset_clear_all(candidates);
495                         bitset_set(candidates, row);
496                         /* search the irn in the rows below */
497                         for (other_row = row+1; other_row<count; ++other_row)
498                                 if (irn == phi_matrix[other_row*arity + col]) {
499                                         /* found the irn in the same col in another row */
500                                         bitset_set(candidates, other_row);
501                                 }
502
503                         /* now we know all occurences of irn in this col */
504                         if (bitset_popcnt(candidates) < 2)
505                                 continue;
506
507                         /* generate an unequation finally.
508                          * phis are indexed in the bitset,
509                          * shared argument is irn
510                          * rhs is phi_count - 1 */
511                         {
512                                 char buf[32];
513                                 ir_node *root;
514                                 int pos, irnnr, rootnr, cst_idx, y_idx, cst_counter = 0;
515                                 int minimal_unequal_count = bitset_popcnt(candidates)-1;
516
517                                 irnnr = get_irn_graph_nr(irn);
518                                 mangle_cst(buf, 'M', cst_counter++);
519                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, minimal_unequal_count);
520
521                                 /* for all phis */
522                                 bitset_foreach(candidates, pos) {
523                                         root = phis[pos];
524                                         rootnr = get_irn_graph_nr(root);
525                                         mangle_var2(buf, 'y', rootnr, irnnr);
526                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
527                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
528                                 }
529                         }
530                 }
531                 del_pset(done); /* clear set for next row */
532         } /*next col*/
533 }
534
535 /**
536  * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
537  * in the same block, iff they use the same arg at the same pos.
538  * Only one of the phis can get the arg.
539  */
540 static void pi_add_clique_path_cstr(problem_instance_t *pi) {
541         DBG((dbg, LEVEL_2, "Adding clique path constraints...\n"));
542         dom_tree_walk_irg(get_irg(pi->co), clique_path_walker, NULL, pi);
543 }
544
545 #ifndef PATH_CONSTRAINTS_FOR_CLASSES
546 /**
547  * Matrix P: Path contraints.
548  * If 2 nodes interfere and there is a path of equal-color-edges
549  * connecting them, then at least one of those equal-color-edges
550  * will break and cause some costs.
551  */
552 static void pi_add_path_cstr(problem_instance_t *pi) {
553         unit_t *curr;
554         int cst_counter = 0;
555         DBG((dbg, LEVEL_2, "Adding path constraints...\n"));
556
557         /* for all optimization units (only phis) */
558         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
559                 int i, o, rootnr;
560
561                 if (curr->min_nodes_costs == 0)
562                         continue;
563
564                 rootnr = get_irn_graph_nr(curr->nodes[0]);
565                 /* check all argument pairs for interference */
566                 for (i=1; i<curr->node_count; ++i) {
567                         const ir_node *arg1 = curr->nodes[i];
568                         int arg1nr = get_irn_graph_nr(arg1);
569                         for (o=i+1; o<curr->node_count; ++o) {
570                                 const ir_node *arg2 = curr->nodes[o];
571                                 int arg2nr = get_irn_graph_nr(arg2);
572                                 if (nodes_interfere(pi->co->chordal_env, arg1, arg2)) {
573                                         int cst_idx, y_idx;
574                                         char buf[32];
575
576                                         mangle_cst(buf, 'P', cst_counter++);
577                                         cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
578
579                                         mangle_var2(buf, 'y', rootnr, arg1nr);
580                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
581                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
582
583                                         mangle_var2(buf, 'y', rootnr, arg2nr);
584                                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
585                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
586                                 }
587                         }
588                 }
589         }
590 }
591 #endif
592
593 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
594 static INLINE int get_y_var_idx(problem_instance_t *pi, int nnr1, int nnr2) {
595         int res;
596         char buf[30];
597
598         mangle_var2(buf, 'y', nnr1, nnr2);
599         if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
600                 return res;
601
602         mangle_var2(buf, 'y', nnr2, nnr1);
603         if ((res = lpp_get_var_idx(pi->curr_lp, buf)) != -1)
604                 return res;
605
606         assert(0 && "One of them must work");
607   return -1;
608 }
609
610 static void check_ecc_and_add_cut(problem_instance_t *pi, ir_node **path, int length, pset *remain, ir_node *tgt) {
611         if (path[length-1] == tgt) { /* we found a path */
612                 int cst_idx, var_idx, i, nnr1, nnr2;
613                 char buf[30];
614
615                 /* add cut to ilp */
616                 mangle_cst(buf, 'Q', pi->cst_counter++);
617                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_greater, 1);
618
619                 /* add all vars along the path */
620                 nnr2 = get_irn_graph_nr(path[0]);
621                 for (i=1; i<length; ++i) {
622                         nnr1 = nnr2;
623                         nnr2 = get_irn_graph_nr(path[i]);
624                         var_idx = get_y_var_idx(pi, nnr1, nnr2);
625                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
626                 }
627         } else { /* try to extend the path */
628                 be_chordal_env_t *cenv = pi->co->chordal_env;
629                 const ir_edge_t *edge;
630                 ir_node *end = path[length-1];
631                 ir_node **next = alloca(pset_count(remain) * sizeof(*next));
632                 int i, o, max, next_pos = 0;
633                 pset *done = pset_new_ptr_default();
634
635                 /* find all potential next nodes on path */
636                 /*  args of phis */
637                 if (is_Phi(end))
638                         for(i=0, max=get_irn_arity(end); i<max; ++i) {
639                                 ir_node *arg = get_irn_n(end, i);
640                                 if (!pset_find_ptr(done, arg) && pset_find_ptr(remain, arg)) {
641                                         next[next_pos++] = arg;
642                                         pset_insert_ptr(done, arg);
643                                 }
644                         }
645                 /*  outs of phis and other nodes */
646                 foreach_out_edge(end, edge) {
647                         ir_node *user = edge->src;
648                         if (is_Phi(user) && !pset_find_ptr(done, user) && pset_find_ptr(remain, user)) {
649                                 next[next_pos++] = user;
650                                 pset_insert_ptr(done, user);
651                         }
652                 }
653                 del_pset(done);
654
655
656                 /* delete all potential nodes with interferences to other nodes in the path */
657                 for (i=0; i<next_pos; ++i) {
658                         ir_node *nn = next[i];
659
660                         /* if next is the tgt, it may interfere with path[0],
661                          * so skip the first check */
662                         o = (nn == tgt && length > 1) ? 1 : 0;
663
664                         for(; o<length; ++o)
665                                 if (nodes_interfere(cenv, nn, path[o])) {
666                                         next[i] = NULL;
667                                         break;
668                                 }
669                 }
670                 /* now we have all possible nodes in next; impossibles are NULL */
671
672                 /* try to finish path with all possible nodes */
673                 for (i=0; i<next_pos; ++i) {
674                         if (!next[i]) /* this was an impossible node */
675                                 continue;
676
677                         path[length] = next[i];
678                         pset_remove_ptr(remain, next[i]);
679                         check_ecc_and_add_cut(pi, path, length+1, remain, tgt);
680                         pset_insert_ptr(remain, next[i]);
681                 }
682         }
683 }
684
685 static void path_cstr_for_classes_walker(ir_node *irn, void *env) {
686         problem_instance_t *pi = env;
687         be_chordal_env_t *cenv;
688         int i, o, max;
689         ir_node *m, **cls;
690         pset *class = get_phi_class(irn);
691         if (!class || pset_find_ptr(pi->done, class))
692                 return;
693
694         pset_insert_ptr(pi->done, class);
695
696         /* pset to array */
697         max = pset_count(class);
698         cls = alloca(max * sizeof(*cls));
699         for(i=0, m = pset_first(class); m; i++, m = pset_next(class)) {
700                 DBG((dbg, LEVEL_1, " class member: %+F\n", m));
701                 cls[i] = m;
702         }
703
704         cenv = pi->co->chordal_env;
705         for(i=0; i<max; ++i) {
706                 ir_node **path = alloca(max * sizeof(*path));
707                 pset *remain = pset_new_ptr(8);
708                 pset_insert_pset_ptr(remain, class);
709
710                 /* add cls[i] to path and remove it from remainder */
711                 path[0] = cls[i];
712                 pset_remove_ptr(remain, cls[i]);
713
714                 for(o=i+1; o<max; ++o)
715                         if (nodes_interfere(cenv, cls[i], cls[o]))
716                                 check_ecc_and_add_cut(pi, path, 1, remain, cls[o]);
717
718                 /* insert back into remainder */
719                 pset_insert_ptr(remain, cls[i]);
720         }
721 }
722
723
724 /**
725  * Matrix P: Path contraints.
726  * If 2 nodes interfere and there is a path of equal-color-edges
727  * connecting them, then at least one of those equal-color-edges
728  * will break and cause some costs.
729  */
730 static void pi_add_path_cstr_for_classes(problem_instance_t *pi) {
731         DBG((dbg, LEVEL_2, "Adding path constraints for phi classes...\n"));
732         pi->cst_counter = 0;
733         pi->done = pset_new_ptr_default();
734         irg_walk_graph(get_irg(pi->co), path_cstr_for_classes_walker, NULL, pi);
735         del_pset(pi->done);
736 }
737 #endif
738
739 #ifdef PRECOLOR_MAX_CLIQUE
740 struct pre_col {
741         problem_instance_t *pi;
742         pset **clique;
743 };
744
745 #define has_reg_class(pi,irn) \
746   (arch_get_irn_reg_class(pi->co->chordal_env->session_env->main_env->arch_env, \
747                           irn, -1) == pi->co->chordal_env->cls)
748
749 static void preColoringWalker(ir_node *bl, void *env) {
750         struct pre_col *e = env;
751         pset **clique = e->clique;
752         pset *max_clique = clique ? *clique : NULL;
753         int max = max_clique ? pset_count(max_clique) : 0;
754         problem_instance_t *pi = e->pi;
755
756         int i, n;
757         pset *live       = pset_new_ptr_default();
758         ir_node *irn;
759         irn_live_t *li;
760
761         /* as always, bring the live end nodes to life here */
762         live_foreach(bl, li) {
763           if(live_is_end(li) && has_reg_class(pi, li->irn)) {
764             pset_insert_ptr(live, irn);
765           }
766         }
767
768         sched_foreach_reverse(bl, irn) {
769                 int pres = pset_count(live);
770
771                 if(pres > max) {
772                         max = pres;
773                         if(max_clique)
774                                 del_pset(max_clique);
775
776                         max_clique = pset_new_ptr_default();
777                         pset_insert_pset_ptr(max_clique, live);
778                 }
779
780
781
782                 if(has_reg_class(pi, irn))
783                         pset_remove_ptr(live, irn);
784
785                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
786                         ir_node *op = get_irn_n(irn, i);
787                         if(has_reg_class(pi, op) && !is_Phi(irn))
788                                 pset_insert_ptr(live, op);
789                 }
790         }
791
792   del_pset(live);
793   *clique = max_clique;
794 }
795
796 static void pi_add_constr_preColoring(problem_instance_t *pi) {
797         ir_node *irn;
798         int cst_counter, color;
799         struct pre_col pre_col;
800
801         pre_col.clique = NULL;
802         pre_col.pi = pi;
803
804         dom_tree_walk_irg(get_irg(pi->co), preColoringWalker, NULL, &pre_col);
805
806         color = 0;
807         for (irn = pset_first(*pre_col.clique); irn; irn = pset_next(*pre_col.clique)) {
808                 int cst_idx, var_idx, nnr = get_irn_graph_nr(irn);
809                 char buf[100];
810
811                 mangle_cst(buf, 'K', cst_counter++);
812                 cst_idx = lpp_add_cst(pi->curr_lp, buf, lpp_equal, 1);
813
814                 mangle_var2(buf, 'x', nnr, color++);
815                 var_idx = lpp_get_var_idx(pi->curr_lp, buf);
816                 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
817         }
818 }
819 #endif
820
821 /**
822  * Generate the initial problem matrices and vectors.
823  */
824 static problem_instance_t *new_pi(const copy_opt_t *co) {
825         problem_instance_t *pi;
826         int col;
827
828         DBG((dbg, LEVEL_2, "Generating new instance...\n"));
829         pi = xcalloc(1, sizeof(*pi));
830         pi->co = co;
831         pi->removed = pset_new_ptr_default();
832         INIT_LIST_HEAD(&pi->simplicials);
833         pi->dilp     = new_lpp(co->name, lpp_minimize);
834
835         /* problem size reduction */
836         pi_find_simplicials(pi);
837         if (pi->all_simplicial)
838                 return pi;
839
840         /* built objective and constraints */
841         pi->curr_lp = pi->dilp;
842 #ifdef NO_NULL_COLORS
843         pi_add_constr_no_null_colors(pi);
844 #endif
845         pi_add_constr_A(pi);
846         for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
847                 pi_add_constr_B(pi, col);
848         pi_add_constr_E(pi);
849
850 #ifdef PATH_CONSTRAINTS_FOR_CLASSES
851         pi_add_path_cstr_for_classes(pi);
852 #else
853         pi_add_path_cstr(pi);
854 #endif
855         pi_add_clique_path_cstr(pi);
856 #ifdef PRECOLOR_MAX_CLIQUE
857         pi_add_constr_preColoring(pi);
858 #endif
859
860         return pi;
861 }
862
863 /**
864  * Clean the problem instance
865  */
866 static void free_pi(problem_instance_t *pi) {
867         simpl_t *simpl, *tmp;
868
869         DBG((dbg, LEVEL_2, "Free instance...\n"));
870         free_lpp(pi->dilp);
871         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
872                 free(simpl);
873         del_pset(pi->removed);
874         free(pi);
875 }
876
877 /**
878  * Set starting values for the mip problem according
879  * to the current coloring of the graph.
880  */
881 static void pi_set_start_sol(problem_instance_t *pi) {
882         int i;
883         char var_name[64];
884         DBG((dbg, LEVEL_2, "Set start solution...\n"));
885         for (i=pi->first_x_var; i<=pi->last_x_var; ++i) {
886                 int nnr, col;
887                 double val;
888                 /* get variable name */
889                 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
890                 /* split into components */
891                 if (split_var(var_name, &nnr, &col) == 2) {
892                         assert(get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) != -1);
893                         val = (get_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr)) == col) ? 1 : 0;
894                         lpp_set_start_value(pi->curr_lp, i, val);
895                 } else {
896                         fprintf(stderr, "Variable name is: %s\n", var_name);
897                         assert(0 && "x vars always look like this 'x123_45'");
898                 }
899         }
900 }
901
902 /**
903  * Invoke a solver
904  */
905 static void pi_solve_ilp(problem_instance_t *pi) {
906   double lower_bound;
907
908         pi_set_start_sol(pi);
909         lower_bound = co_get_lower_bound(pi->co) - co_get_inevit_copy_costs(pi->co);
910         lpp_set_bound(pi->curr_lp, lower_bound);
911         lpp_solve_net(pi->curr_lp, LPP_HOST, LPP_SOLVER);
912 //      lpp_solve_cplex(pi->curr_lp);
913         DBG((dbg, LEVEL_1, "Solution time: %.2f\n", pi->curr_lp->sol_time));
914 }
915
916 /**
917  * Set the color of all simplicial nodes removed form
918  * the graph before transforming it to an ilp.
919  */
920 static void pi_set_simplicials(problem_instance_t *pi) {
921         simpl_t *simpl, *tmp;
922         be_ifg_t *ifg        = pi->co->chordal_env->ifg;
923         bitset_t *used_cols  = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
924         void *iter           = be_ifg_iter_alloca(ifg);
925
926         DBG((dbg, LEVEL_2, "Set simplicials...\n"));
927         /* color the simplicial nodes in right order */
928         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
929                 int free_col;
930                 ir_node *other, *irn;
931
932                 /* get free color by inspecting all neighbors */
933                 irn = simpl->irn;
934                 bitset_clear_all(used_cols);
935
936                 be_ifg_foreach_neighbour(ifg, iter, irn, other) {
937                         if (!is_removed(other)) /* only inspect nodes which are in graph right now */
938                                 bitset_set(used_cols, get_irn_col(pi->co, other));
939                 }
940
941                 /* now all bits not set are possible colors */
942                 free_col = bitset_next_clear(used_cols, 0);
943                 assert(free_col != -1 && "No free color found. This can not be.");
944                 set_irn_col(pi->co, irn, free_col);
945                 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
946         }
947 }
948
949 /**
950  * Sets the colors of irns according to the values of variables
951  * provided by the solution of the solver.
952  */
953 static int pi_apply_solution(problem_instance_t *pi) {
954         int res = 1, i;
955         double *sol;
956         lpp_sol_state_t state;
957         DBG((dbg, LEVEL_2, "Applying solution...\n"));
958
959 #ifdef DO_STAT
960         copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp)));  //now we have ms
961         copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
962         copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
963         copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
964 #endif
965
966         sol = xmalloc((pi->last_x_var - pi->first_x_var + 1) * sizeof(*sol));
967         state = lpp_get_solution(pi->curr_lp, sol, pi->first_x_var, pi->last_x_var);
968         if (state != lpp_optimal) {
969                 printf("WARNING %s: Solution state is not 'optimal': %d\n", pi->co->name, state);
970                 assert(state >= lpp_feasible && "The solution should at least be feasible!");
971                 res = 0;
972         }
973         for (i=0; i<pi->last_x_var - pi->first_x_var + 1; ++i) {
974                 int nnr, col;
975                 char var_name[64];
976
977                 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
978                         lpp_get_var_name(pi->curr_lp, pi->first_x_var+i, var_name, sizeof(var_name));
979                         if (split_var(var_name, &nnr, &col) == 2) {
980                                 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]));
981                                 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
982                                 set_irn_col(pi->co, get_irn_for_graph_nr(get_irg(pi->co), nnr), col);
983                         } else
984                                 assert(0 && "This should be a x-var");
985                 }
986         }
987         return res;
988 }
989
990 int co_ilp_opt(copy_opt_t *co, double time_limit) {
991         int res = 1;
992         problem_instance_t *pi;
993
994         dbg = firm_dbg_register("ir.be.copyoptilp");
995         if (!strcmp(co->name, DEBUG_IRG))
996                 firm_dbg_set_mask(dbg, DEBUG_IRG_LVL_ILP);
997         else
998                 firm_dbg_set_mask(dbg, DEBUG_LVL_ILP);
999
1000         pi = new_pi(co);
1001         if (!pi->all_simplicial) {
1002 #ifdef DUMP_MPS
1003                 char buf[512];
1004                 snprintf(buf, sizeof(buf), "%s.mps", co->name);
1005                 lpp_dump(pi->curr_lp, buf);
1006 #endif
1007                 lpp_set_time_limit(pi->curr_lp, time_limit);
1008                 pi_solve_ilp(pi);
1009                 res = pi_apply_solution(pi);
1010                 pi_set_simplicials(pi);
1011         }
1012         free_pi(pi);
1013         return res;
1014 }