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