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