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