Bugfixes
[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.h"
21 #include "lpp_local.h"
22 #include "lpp_remote.h"
23 #include "xmalloc.h"
24 #include "becopyopt.h"
25 #include "becopystat.h"
26
27 #undef DUMP_MPS
28 #define DEBUG_LVL SET_LEVEL_1
29 static firm_dbg_module_t *dbg = NULL;
30
31 #define EPSILON 0.00001
32 #define SLOTS_LIVING 32
33
34 typedef struct _simpl_t {
35         struct list_head chain;
36         if_node_t *ifn;
37 } simpl_t;
38
39 typedef struct _problem_instance_t {
40         const copy_opt_t *co;                   /** the copy_opt problem */
41         /* problem size reduction removing simple nodes */
42         struct list_head simplicials;   /**< holds all simpl_t's in right order to color*/
43         pset *removed;                                  /**< holds all removed simplicial irns */
44         /* lp problem */
45         lpp_t *dilp;                                    /**< problem formulation directly as milp */
46         /* overhead stuff */
47         lpp_t *curr_lp;                                 /**< points to the problem currently used */
48         int cst_counter, last_x_var;
49         char buf[32];
50         int all_simplicial;
51 } problem_instance_t;
52
53 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
54
55 #define is_color_possible(irn,color) arch_reg_is_allocatable(pi->co->chordal_env->arch_env, irn, arch_pos_make_out(0), arch_register_for_index(pi->co->chordal_env->cls, color))
56
57 /*
58  * Some stuff for variable name handling.
59  */
60 #define mangle_cst(buf, prefix, nr) \
61                         snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
62
63 #define mangle_var(buf, prefix, node_nr, color) \
64                         snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
65
66 #define mangle_var_irn(buf, prefix, irn, color) \
67                         mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
68
69 #define split_var(var, nnr, col) \
70                         sscanf(var, "x%d_%d", (nnr), (col))
71
72
73 /**
74  * Checks if a node is simplicial in the graph
75  * heeding the already removed nodes.
76  */
77 static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
78         int i, o, size = 0;
79         if_node_t **all, *curr;
80         all = alloca(ifn_get_degree(ifn) * sizeof(*all));
81
82         /* get all non-removed neighbors */
83         foreach_neighb(ifn, curr)
84                 if (!is_removed(curr))
85                         all[size++] = curr;
86
87         /* check if these form a clique */
88         for (i=0; i<size; ++i)
89                 for (o=i+1; o<size; ++o)
90                         if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
91                                 return 0;
92
93         /* all edges exist so this is a clique */
94         return 1;
95 }
96
97 /**
98  * Iterative finds and 'removes' from the graph all nodes which are
99  * simplicial AND not member of a equal-color-wish
100  */
101 static void pi_find_simplicials(problem_instance_t *pi) {
102         set *if_nodes;
103         if_node_t *ifn;
104         int redo = 1;
105
106         DBG((dbg, LEVEL_2, "Find simlicials...\n"));
107
108         if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
109         while (redo) {
110                 redo = 0;
111                 for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
112                         ir_node *irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
113                         if (!is_removed(irn) && !is_optimizable(pi->co->chordal_env->arch_env, irn) &&
114           !is_optimizable_arg(pi->co, irn) && pi_is_simplicial(pi, ifn)) {
115                                 simpl_t *s = xmalloc(sizeof(*s));
116                                 s->ifn = ifn;
117                                 list_add(&s->chain, &pi->simplicials);
118                                 pset_insert_ptr(pi->removed, irn);
119                                 redo = 1;
120                                 DBG((dbg, LEVEL_2, " Removed %n %d\n", irn, get_irn_graph_nr(irn)));
121                         }
122                 }
123         }
124         if (set_count(be_ra_get_ifg_nodes(pi->co->chordal_env)) == pset_count(pi->removed))
125                 pi->all_simplicial = 1;
126 }
127
128 /**
129  * Add coloring-force conditions
130  * Matrix A: knapsack constraint for each node
131  */
132 static void pi_add_constr_A(problem_instance_t *pi) {
133         pmap_entry *pme;
134
135         DBG((dbg, LEVEL_2, "Add A constraints...\n"));
136         /* iterate over all blocks */
137         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
138                 struct list_head *head = pme->value;
139                 border_t *curr;
140                 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
141
142                 list_for_each_entry_reverse(border_t, curr, head, list)
143                         if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
144                                 int cst_idx, nnr, col;
145
146                                 nnr = get_irn_graph_nr(curr->irn);
147                                 mangle_cst(pi->buf, 'A', nnr);
148                                 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, equal, 1);
149
150                                 // iterate over all possible colors in order
151                                 bitset_clear_all(pos_regs);
152                                 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, curr->irn, arch_pos_make_out(0), pi->co->chordal_env->cls, pos_regs);
153                                 bitset_foreach(pos_regs, col) {
154                                         int var_idx;
155                                         mangle_var(pi->buf, 'x', nnr, col);
156                                         var_idx = lpp_add_var(pi->curr_lp, pi->buf, binary, 0);
157                                         pi->last_x_var = var_idx;
158                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
159                                 }
160                         }
161         }
162 }
163
164 /**
165  * Checks if all nodes in @p living are live in in block @p block.
166  * @return 1 if all are live in
167  *         0 else
168  */
169 static INLINE int all_live_in(ir_node *block, pset *living) {
170         ir_node *n;
171         for (n = pset_first(living); n; n = pset_next(living))
172                 if (!is_live_in(block, n)) {
173                         pset_break(living);
174                         return 0;
175                 }
176         return 1;
177 }
178
179 /**
180  * Finds cliques in the interference graph, considering only nodes
181  * for which the color @p color is possible. Finds only 'maximal-cliques',
182  * viz cliques which are not contained in another one.
183  * Matrix B: interference constraints using cliques
184  */
185 static void pi_add_constr_B(problem_instance_t *pi, int color) {
186         enum phase_t {growing, shrinking} phase = growing;
187         border_t *b;
188         pmap_entry *pme;
189         pset *living = pset_new_ptr(SLOTS_LIVING);
190
191         DBG((dbg, LEVEL_2, "Add B constraints (col = %d)...\n", color));
192         /* iterate over all blocks */
193         pmap_foreach(pi->co->chordal_env->border_heads, pme) {
194                 ir_node *block = pme->key;
195                 struct list_head *head = pme->value;
196
197                 list_for_each_entry_reverse(border_t, b, head, list) {
198                         const ir_node *irn = b->irn;
199                         if (is_removed(irn) || !is_color_possible(irn, color))
200                                 continue;
201
202                         if (b->is_def) {
203                                 DBG((dbg, LEVEL_2, "Def %n\n", irn));
204                                 pset_insert_ptr(living, irn);
205                                 phase = growing;
206                         } else { /* is_use */
207                                 DBG((dbg, LEVEL_2, "Use %n\n", irn));
208
209                                 /* before shrinking the set, store the current 'maximum' clique;
210                                  * do NOT if clique is a single node
211                                  * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
212                                 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
213                                         int cst_idx;
214                                         ir_node *n;
215                                         mangle_cst(pi->buf, 'B', pi->cst_counter);
216                                         cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, less, 1);
217                                         for (n = pset_first(living); n; n = pset_next(living)) {
218                                                 int var_idx;
219                                                 mangle_var_irn(pi->buf, 'x', n, color);
220                                                 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
221                                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
222                                         }
223                                         pi->cst_counter++;
224                                 }
225                                 pset_remove_ptr(living, irn);
226                                 phase = shrinking;
227                         }
228                 }
229         }
230         assert(0 == pset_count(living));
231         del_pset(living);
232 }
233
234 /**
235  * Generates constraints which interrelate x with y variables.
236  * x1 and x2 have the different colors ==> y_12 = 1
237  */
238 static void pi_add_constr_E(problem_instance_t *pi) {
239         unit_t *curr;
240         bitset_t *root_regs, *arg_regs, *work_regs;
241         int cst_counter = 0;
242         unsigned nregs = pi->co->chordal_env->cls->n_regs;
243         root_regs = bitset_alloca(nregs);
244         arg_regs = bitset_alloca(nregs);
245         work_regs = bitset_alloca(nregs);
246
247         DBG((dbg, LEVEL_2, "Add E constraints...\n"));
248         /* for all roots of optimization units */
249         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
250                 ir_node *root, *arg;
251                 int rootnr, argnr, color;
252                 int y_idx, i;
253                 char buf[32];
254
255                 root = curr->nodes[0];
256                 rootnr = get_irn_graph_nr(root);
257                 bitset_clear_all(root_regs);
258                 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
259
260                 /* for all arguments of root */
261                 for (i = 1; i < curr->node_count; ++i) {
262                         arg = curr->nodes[i];
263                         argnr = get_irn_graph_nr(arg);
264                         bitset_clear_all(arg_regs);
265                         arch_get_allocatable_regs(pi->co->chordal_env->arch_env, arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
266
267                         /* Introduce new variable and set factor in objective function */
268                         mangle_var(buf, 'y', rootnr, argnr);
269                         y_idx = lpp_add_var(pi->curr_lp, buf, continous, curr->costs[i]);
270
271                         //BETTER: y vars as binary or continous vars ??
272                         /* set starting value */
273                         //lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
274
275                         /* For all colors root and arg have in common, add 2 constraints to E */
276                         bitset_copy(work_regs, root_regs);
277                         bitset_and(work_regs, arg_regs);
278                         bitset_foreach(work_regs, color) {
279                                 int root_idx, arg_idx, cst_idx;
280                                 mangle_var(buf, 'x', rootnr, color);
281                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
282                                 mangle_var(buf, 'x', argnr, color);
283                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
284
285                                 /* add root-arg-y <= 0 */
286                                 mangle_cst(buf, 'E', cst_counter++);
287                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
288                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
289                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
290                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
291
292                                 /* add arg-root-y <= 0 */
293                                 mangle_cst(buf, 'E', cst_counter++);
294                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
295                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
296                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
297                                 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
298                         }
299                         /* For all colors root and arg have "disjunct", add 1 constraints to E.
300                          * If root gets a color the arg is not possible to get then they will
301                          * definetly get different colors. So y has to be 1.
302                          * Vice versa for arg.
303                          */
304                         bitset_copy(work_regs, root_regs);
305                         bitset_xor(work_regs, arg_regs);
306                         bitset_foreach(work_regs, color) {
307                                 int root_idx, arg_idx, cst_idx;
308                                 mangle_var(buf, 'x', rootnr, color);
309                                 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
310                                 mangle_var(buf, 'x', argnr, color);
311                                 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
312
313                                 mangle_cst(buf, 'E', cst_counter++);
314                                 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
315                                 if (bitset_is_set(root_regs, color)) {
316                                         /* add root-y <= 0 */
317                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
318                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
319                                 } else {
320                                         assert(bitset_is_set(arg_regs, color) && "bitset_xor is buggy");
321                                         /* add arg-y <= 0 */
322                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
323                                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
324                                 }
325                         }
326                 }
327         }
328 }
329
330 /**
331  * Matrix S: maximum independent set constraints
332  * Generates lower bound-cuts for optimization units with inner interferences.
333  * Sum(y_{root, arg}, arg \in Args) <= max_indep_set_size - 1
334  */
335 static void pi_add_constr_S(problem_instance_t *pi) {
336         unit_t *curr;
337         int cst_counter = 0;
338         DBG((dbg, LEVEL_2, "Add M constraints...\n"));
339
340         /* for all optimization units */
341         list_for_each_entry(unit_t, curr, &pi->co->units, units) {
342                 const ir_node *root, *arg;
343                 int rootnr, argnr;
344                 int cst_idx, y_idx, i;
345                 char buf[32];
346
347                 if (curr->minimal_costs == 0)
348                         continue;
349
350                 root = curr->nodes[0];
351                 rootnr = get_irn_graph_nr(root);
352                 mangle_cst(buf, 'M', cst_counter++);
353                 cst_idx = lpp_add_cst(pi->curr_lp, buf, greater, curr->minimal_costs);
354
355                 /* for all arguments */
356                 for (i = 1; i < curr->node_count; ++i) {
357                         arg = curr->nodes[i];
358                         argnr = get_irn_graph_nr(arg);
359                         mangle_var(buf, 'y', rootnr, argnr);
360                         y_idx = lpp_get_var_idx(pi->curr_lp, buf);
361                         lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, curr->costs[i]);
362                 }
363         }
364 }
365
366 /**
367  * Matrix M: Multi-Arg-Use. Interrelates different \phi-functions
368  * in the same block, iff they use the same arg at the same pos.
369  * Only one of the phis can get the arg.
370  */
371 static void pi_add_constr_M(problem_instance_t *pi) {
372         //TODO pi_add_constr_M
373 }
374
375 /**
376  * Generate the initial problem matrices and vectors.
377  */
378 static problem_instance_t *new_pi(const copy_opt_t *co) {
379         problem_instance_t *pi;
380         int col;
381
382         DBG((dbg, LEVEL_2, "Generating new instance...\n"));
383         pi = xcalloc(1, sizeof(*pi));
384         pi->co = co;
385         pi->removed = pset_new_ptr_default();
386         INIT_LIST_HEAD(&pi->simplicials);
387         pi->dilp = new_lpp(co->name, minimize);
388         pi->last_x_var = -1;
389
390         /* problem size reduction */
391         pi_find_simplicials(pi);
392         //BETTER If you wish to see it: dump_ifg_w/o_removed
393         if (pi->all_simplicial)
394                 return pi;
395
396         /* built objective abd constraints */
397         pi->curr_lp = pi->dilp;
398         pi_add_constr_A(pi);
399         for (col = 0; col < pi->co->chordal_env->cls->n_regs; ++col)
400                 pi_add_constr_B(pi, col);
401         pi_add_constr_E(pi);
402         pi_add_constr_S(pi);
403         pi_add_constr_M(pi);
404
405         return pi;
406 }
407
408 /**
409  * Clean the problem instance
410  */
411 static void free_pi(problem_instance_t *pi) {
412         simpl_t *simpl, *tmp;
413
414         DBG((dbg, LEVEL_2, "Free instance...\n"));
415         free_lpp(pi->dilp);
416         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain)
417                 free(simpl);
418         del_pset(pi->removed);
419         free(pi);
420 }
421
422 /**
423  * Set starting values for the mip problem according
424  * to the current coloring of the graph.
425  */
426 static void pi_set_start_sol(problem_instance_t *pi) {
427         int i;
428         char var_name[64];
429         DBG((dbg, LEVEL_2, "Set start solution...\n"));
430         for (i=1; i<=pi->last_x_var; ++i) {
431                 int nnr, col;
432                 double val;
433                 /* get variable name */
434                 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
435                 /* split into components */
436                 if (split_var(var_name, &nnr, &col) == 2) {
437                         assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) != -1);
438                         val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) == col) ? 1 : 0;
439                         lpp_set_start_value(pi->curr_lp, i, val);
440                 } else {
441                         fprintf(stderr, "Variable name is: %s\n", var_name);
442                         assert(0 && "x vars always look like this 'x123_45'");
443                 }
444         }
445 }
446
447 /**
448  * Invoke a solver
449  */
450 static void pi_solve_ilp(problem_instance_t *pi, void (*lpp_solve)(lpp_t *)) {
451         pi_set_start_sol(pi);
452         lpp_solve(pi->curr_lp);
453 }
454
455 /**
456  * Set the color of all simplicial nodes removed form
457  * the graph before transforming it to an ilp.
458  */
459 static void pi_set_simplicials(problem_instance_t *pi) {
460         simpl_t *simpl, *tmp;
461         bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
462
463         DBG((dbg, LEVEL_2, "Set simplicials...\n"));
464         /* color the simplicial nodes in right order */
465         list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
466                 int free_col;
467                 ir_node *other_irn, *irn;
468                 if_node_t *other, *ifn;
469
470                 /* get free color by inspecting all neighbors */
471                 ifn = simpl->ifn;
472                 irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
473                 bitset_clear_all(used_cols);
474                 foreach_neighb(ifn, other) {
475                         other_irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, other->nnr);
476                         if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
477                                 bitset_set(used_cols, get_irn_col(pi->co, other_irn));
478                 }
479
480                 /* now all bits not set are possible colors */
481                 free_col = bitset_next_clear(used_cols, 0);
482                 assert(free_col != -1 && "No free color found. This can not be.");
483                 set_irn_col(pi->co, irn, free_col);
484                 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
485         }
486 }
487
488 /**
489  * Sets the colors of irns according to the values of variables
490  * provided by the solution of the solver.
491  */
492 static void pi_apply_solution(problem_instance_t *pi) {
493         int i;
494         double *sol;
495         sol_state_t state;
496         DBG((dbg, LEVEL_2, "Applying solution...\n"));
497
498 #ifdef DO_STAT
499         curr_vals[I_ILP_ITER] += lpp_get_iter_cnt(pi->curr_lp);
500         curr_vals[I_ILP_TIME] += lpp_get_sol_time(pi->curr_lp);
501 #endif
502
503         sol = xmalloc((pi->last_x_var+1) * sizeof(*sol));
504         state = lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
505         if (state != optimal) {
506                 printf("Solution state is not 'optimal': %d\n", state);
507                 assert(state >= feasible && "The solution should at least be feasible!");
508         }
509         for (i=0; i<pi->last_x_var; ++i) {
510                 int nnr, col;
511                 char var_name[64];
512
513                 if (sol[i] > 1-EPSILON) { /* split varibale name into components */
514                         lpp_get_var_name(pi->curr_lp, 1+i, var_name, sizeof(var_name));
515                         if (split_var(var_name, &nnr, &col) == 2) {
516                                 DBG((dbg, LEVEL_2, "Irn %n  Idx %d  Var %s  Val %f\n", get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr), i, var_name, sol[i]));
517                                 DBG((dbg, LEVEL_2, "x%d = %d\n", nnr, col));
518                                 set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr), col);
519                         } else
520                                 assert(0 && "This should be a x-var");
521                 }
522         }
523 }
524
525 void co_ilp_opt(copy_opt_t *co) {
526         problem_instance_t *pi;
527
528         dbg = firm_dbg_register("ir.be.copyoptilp");
529         if (!strcmp(co->name, DEBUG_IRG))
530                 firm_dbg_set_mask(dbg, DEBUG_LVL_ILP);
531         else
532                 firm_dbg_set_mask(dbg, DEBUG_LVL);
533
534         pi = new_pi(co);
535         if (!pi->all_simplicial) {
536 #ifdef DUMP_MPS
537                 char buf[512];
538                 snprintf(buf, sizeof(buf), "%s.mps", co->name);
539                 lpp_dump(pi->curr_lp, buf);
540 #endif
541                 pi_solve_ilp(pi, lpp_solve_local);
542                 pi_apply_solution(pi);
543                 pi_set_simplicials(pi);
544         }
545         free_pi(pi);
546 }