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