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