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