The big committ:
[libfirm] / ir / be / becopyilp2.c
1 /**
2  * Author:      Daniel Grund
3  * Date:                28.02.2006
4  * Copyright:   (c) Universitaet Karlsruhe
5  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
6  *
7  *
8  * ILP formalization using G=(V, E, Q):
9  *  - 2 class of variables: coloring vars x_ic   and   equal color vars y_ij
10  *  - Path constraints
11  *  - Clique-star constraints
12  *
13  *
14  *      \min \sum_{ (i,j) \in Q }  w_ij y_ij
15  *
16  *              \sum_c x_nc                     =  1                    n \in N, c \in C
17  *
18  *              x_nc                            =  0                    n \in N, c \not\in C(n)
19  *
20  *              \sum x_nc                       <= 1                    x_nc \in Clique \in AllCliques,  c \in C
21  *
22  *              \sum_{e \in p} y_e      >= 1                    p \in P         path constraints
23  *
24  *              \sum_{e \in cs} y_e     >= |cs| - 1             cs \in CP       clique-star constraints
25  *
26  *              x_nc, y_ij \in N,   w_ij \in R^+
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif /* HAVE_CONFIG_H */
32
33 #ifdef WITH_ILP
34
35 #include "bitset.h"
36 #include "raw_bitset.h"
37 #include "pdeq.h"
38
39 #include "irtools.h"
40 #include "irgwalk.h"
41 #include "becopyilp_t.h"
42 #include "beifg_t.h"
43 #include "besched_t.h"
44 #include "benodesets.h"
45
46 #define DEBUG_LVL 1
47
48 typedef struct _local_env_t {
49         double time_limit;
50         int first_x_var, last_x_var;
51         pmap *nr_2_irn;
52         DEBUG_ONLY(firm_dbg_module_t *dbg;)
53 } local_env_t;
54
55 static void build_coloring_cstr(ilp_env_t *ienv) {
56         be_ifg_t *ifg = ienv->co->cenv->ifg;
57         void *iter = be_ifg_nodes_iter_alloca(ifg);
58         bitset_t *colors;
59         ir_node *irn;
60         char buf[16];
61
62         colors = bitset_alloca(arch_register_class_n_regs(ienv->co->cls));
63
64         be_ifg_foreach_node(ifg, iter, irn)
65                 if (!sr_is_removed(ienv->sr, irn)) {
66                         int col, cst_idx;
67                         const arch_register_req_t *req;
68                         int curr_node_color = get_irn_col(ienv->co, irn);
69                         int node_nr = (int)get_irn_node_nr(irn);
70                         local_env_t *lenv = ienv->env;
71
72                         pmap_insert(lenv->nr_2_irn, INT_TO_PTR(node_nr), irn);
73
74                         req = arch_get_register_req(ienv->co->aenv, irn, -1);
75
76                         /* get assignable colors */
77                         if (arch_register_req_is(req, limited)) {
78                                 rbitset_copy_to_bitset(req->limited, colors);
79                         } else {
80                                 arch_register_class_put(req->cls, colors);
81                                 // bitset_andnot(colors, ienv->co->cenv->ignore_colors);
82                         }
83
84                         /* add the coloring constraint */
85                         cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 1.0);
86
87                         bitset_foreach(colors, col) {
88                                 int var_idx = lpp_add_var(ienv->lp, name_cdd(buf, 'x', node_nr, col), lpp_binary, 0.0);
89                                 lpp_set_start_value(ienv->lp, var_idx, (col == curr_node_color) ? 1.0 : 0.0);
90                                 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
91
92                                 lenv->last_x_var = var_idx;
93                                 if (lenv->first_x_var == -1)
94                                         lenv->first_x_var = var_idx;
95                         }
96
97                         /* add register constraint constraints */
98                         bitset_foreach_clear(colors, col) {
99                                 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 0.0);
100                                 int var_idx = lpp_add_var(ienv->lp, name_cdd(buf, 'x', node_nr, col), lpp_binary, 0.0);
101                                 lpp_set_start_value(ienv->lp, var_idx, 0.0);
102                                 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
103
104                                 lenv->last_x_var = var_idx;
105                         }
106                 }
107 }
108
109 static void build_interference_cstr(ilp_env_t *ienv) {
110         lpp_t *lpp = ienv->lp;
111         be_ifg_t *ifg = ienv->co->cenv->ifg;
112         int n_colors = arch_register_class_n_regs(ienv->co->cls);
113         int i, col;
114
115         void *iter = be_ifg_cliques_iter_alloca(ifg);
116         ir_node **clique = alloca(sizeof(*clique) * n_colors);
117         int size;
118
119         char buf[16];
120
121         /* for each maximal clique */
122         be_ifg_foreach_clique(ifg, iter, clique, &size) {
123                 int realsize = 0;
124
125                 for (i=0; i<size; ++i)
126                         if (!sr_is_removed(ienv->sr, clique[i]))
127                                 ++realsize;
128
129                 if (realsize < 2)
130                         continue;
131
132                 /* for all colors */
133                 for (col=0; col<n_colors; ++col) {
134                         int cst_idx = lpp_add_cst(lpp, NULL, lpp_less, 1.0);
135
136                         /* for each member of this clique */
137                         for (i=0; i<size; ++i) {
138                                 ir_node *irn = clique[i];
139
140                                 if (!sr_is_removed(ienv->sr, irn)) {
141                                         int var_idx = lpp_get_var_idx(lpp, name_cdd(buf, 'x', (int)get_irn_node_nr(irn), col));
142                                         lpp_set_factor_fast(lpp, cst_idx, var_idx, 1);
143                                 }
144                         }
145                 }
146         }
147 }
148
149
150 /**
151  * TODO: Remove the dependency of the opt-units data structure
152  *       by walking over all affinity edges. Graph structure
153  *       does not provide this walker, yet.
154  */
155 static void build_affinity_cstr(ilp_env_t *ienv) {
156         unit_t *curr;
157         int n_colors = arch_register_class_n_regs(ienv->co->cls);
158
159         /* for all optimization units */
160         list_for_each_entry(unit_t, curr, &ienv->co->units, units) {
161                 ir_node *root, *arg;
162                 int root_nr, arg_nr, i, col, y_idx, root_idx, arg_idx;
163                 char buf[16];
164                 int root_col, arg_col;
165
166                 root = curr->nodes[0];
167                 root_nr = (int) get_irn_node_nr(root);
168                 root_col = get_irn_col(ienv->co, root);
169
170                 for (i = 1; i < curr->node_count; ++i) {
171                         arg = curr->nodes[i];
172                         arg_nr = (int) get_irn_node_nr(arg);
173                         arg_col = get_irn_col(ienv->co, arg);
174
175                         /* add a new affinity variable */
176                         y_idx = lpp_add_var(ienv->lp, name_cdd_sorted(buf, 'y', root_nr, arg_nr), lpp_binary, curr->costs[i]);
177                         lpp_set_start_value(ienv->lp, y_idx, (root_col==arg_col) ? 0.0 : 1.0);
178
179                         /* add constraints relating the affinity var to the color vars */
180                         for (col=0; col<n_colors; ++col) {
181                                 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_less, 0.0);
182                                 root_idx = lpp_get_var_idx(ienv->lp, name_cdd(buf, 'x', root_nr, col));
183                                 arg_idx  = lpp_get_var_idx(ienv->lp, name_cdd(buf, 'x', arg_nr,  col));
184
185                                 lpp_set_factor_fast(ienv->lp, cst_idx, root_idx,  1.0);
186                                 lpp_set_factor_fast(ienv->lp, cst_idx, arg_idx,  -1.0);
187                                 lpp_set_factor_fast(ienv->lp, cst_idx, y_idx, -1.0);
188                         }
189                 }
190         }
191 }
192
193 /**
194  * Helping stuff for build_clique_star_cstr
195  */
196 typedef struct _edge_t {
197         ir_node *n1, *n2;
198 } edge_t;
199
200 static int compare_edge_t(const void *k1, const void *k2, size_t size) {
201         const edge_t *e1 = k1;
202         const edge_t *e2 = k2;
203
204         return ! (e1->n1 == e2->n1   &&   e1->n2 == e2->n2);
205 }
206
207 #define HASH_EDGE(e) (nodeset_hash((e)->n1) ^ nodeset_hash((e)->n2))
208
209 static INLINE edge_t *add_edge(set *edges, ir_node *n1, ir_node *n2, int *counter) {
210         edge_t new_edge;
211
212         if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
213                 new_edge.n1 = n1;
214                 new_edge.n2 = n2;
215         } else {
216                 new_edge.n1 = n2;
217                 new_edge.n2 = n1;
218         }
219         (*counter)++;
220         return set_insert(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
221 }
222
223 static INLINE edge_t *find_edge(set *edges, ir_node *n1, ir_node *n2) {
224         edge_t new_edge;
225
226         if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
227                 new_edge.n1 = n1;
228                 new_edge.n2 = n2;
229         } else {
230                 new_edge.n1 = n2;
231                 new_edge.n2 = n1;
232         }
233         return set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
234 }
235
236 static INLINE void remove_edge(set *edges, ir_node *n1, ir_node *n2, int *counter) {
237         edge_t new_edge, *e;
238
239         if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
240                 new_edge.n1 = n1;
241                 new_edge.n2 = n2;
242         } else {
243                 new_edge.n1 = n2;
244                 new_edge.n2 = n1;
245         }
246         e = set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
247         if (e) {
248                 e->n1 = NULL;
249                 e->n2 = NULL;
250                 (*counter)--;
251         }
252 }
253
254 #define pset_foreach(pset, irn)  for(irn=pset_first(pset); irn; irn=pset_next(pset))
255
256 /**
257  * Search for an interference clique and an external node
258  * with affinity edges to all nodes of the clique.
259  * At most 1 node of the clique can be colored equally with the external node.
260  */
261 static void build_clique_star_cstr(ilp_env_t *ienv) {
262         affinity_node_t *aff;
263
264         /* for each node with affinity edges */
265         co_gs_foreach_aff_node(ienv->co, aff) {
266                 struct obstack ob;
267                 neighb_t *nbr;
268                 ir_node *center = aff->irn;
269                 ir_node **nodes;
270                 set *edges;
271                 int i, o, n_nodes, n_edges;
272
273                 obstack_init(&ob);
274                 edges = new_set(compare_edge_t, 8);
275
276                 /* get all affinity neighbours */
277                 n_nodes = 0;
278                 co_gs_foreach_neighb(aff, nbr) {
279                         obstack_ptr_grow(&ob, nbr->irn);
280                         ++n_nodes;
281                 }
282                 nodes = obstack_finish(&ob);
283
284                 /* get all interference edges between these */
285                 n_edges = 0;
286                 for (i=0; i<n_nodes; ++i)
287                         for (o=0; o<i; ++o)
288                                 if (be_ifg_connected(ienv->co->cenv->ifg, nodes[i], nodes[o]))
289                                         add_edge(edges, nodes[i], nodes[o], &n_edges);
290
291                 /* cover all these interference edges with maximal cliques */
292                 while (n_edges) {
293                         edge_t *e;
294                         pset *clique = pset_new_ptr(8);
295                         int growed;
296
297                         /* get 2 starting nodes to form a clique */
298                         for (e=set_first(edges); !e->n1; e=set_next(edges))
299                                 /*nothing*/ ;
300
301                         /* we could be stepped out of the loop before the set iterated to the end */
302                         set_break(edges);
303
304                         pset_insert_ptr(clique, e->n1);
305                         pset_insert_ptr(clique, e->n2);
306                         remove_edge(edges, e->n1, e->n2, &n_edges);
307
308                         /* while the clique is growing */
309                         do {
310                                 growed = 0;
311
312                                 /* search for a candidate to extend the clique */
313                                 for (i=0; i<n_nodes; ++i) {
314                                         ir_node *member, *cand = nodes[i];
315                                         int is_cand;
316
317                                         /* if its already in the clique try the next */
318                                         if (pset_find_ptr(clique, cand))
319                                                 continue;
320
321                                         /* are there all necessary interferences? */
322                                         is_cand = 1;
323                                         pset_foreach(clique, member) {
324                                                 if (!find_edge(edges, cand, member)) {
325                                                         is_cand = 0;
326                                                         pset_break(clique);
327                                                         break;
328                                                 }
329                                         }
330
331                                         /* now we know if we have a clique extender */
332                                         if (is_cand) {
333                                                 /* first remove all covered edges */
334                                                 pset_foreach(clique, member)
335                                                         remove_edge(edges, cand, member, &n_edges);
336
337                                                 /* insert into clique */
338                                                 pset_insert_ptr(clique, cand);
339                                                 growed = 1;
340                                                 break;
341                                         }
342                                 }
343                         } while (growed);
344
345                         /* now the clique is maximal. Finally add the constraint */
346                         {
347                                 ir_node *member;
348                                 int var_idx, cst_idx, center_nr, member_nr;
349                                 char buf[16];
350
351                                 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater, pset_count(clique)-1);
352                                 center_nr = get_irn_node_nr(center);
353
354                                 pset_foreach(clique, member) {
355                                         member_nr = get_irn_node_nr(member);
356                                         var_idx = lpp_get_var_idx(ienv->lp, name_cdd_sorted(buf, 'y', center_nr, member_nr));
357                                         lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
358                                 }
359                         }
360
361                         del_pset(clique);
362                 }
363
364                 del_set(edges);
365                 obstack_free(&ob, NULL);
366         }
367 }
368
369
370 static void extend_path(ilp_env_t *ienv, pdeq *path, ir_node *irn) {
371         be_ifg_t *ifg = ienv->co->cenv->ifg;
372         int i, len;
373         ir_node **curr_path;
374         affinity_node_t *aff;
375         neighb_t *nbr;
376
377         /* do not walk backwards or in circles */
378         if (pdeq_contains(path, irn))
379                 return;
380
381         /* insert the new irn */
382         pdeq_putr(path, irn);
383
384
385
386         /* check for forbidden interferences */
387         len = pdeq_len(path);
388         curr_path = alloca(len * sizeof(*curr_path));
389         pdeq_copyl(path, (const void **)curr_path);
390
391         for (i=1; i<len; ++i)
392                 if (be_ifg_connected(ifg, irn, curr_path[i]))
393                         goto end;
394
395
396
397         /* check for terminating interference */
398         if (be_ifg_connected(ifg, irn, curr_path[0])) {
399
400                 /* One node is not a path. */
401                 /* And a path of length 2 is covered by a clique star constraint. */
402                 if (len > 2) {
403                         /* finally build the constraint */
404                         int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater, 1.0);
405                         for (i=1; i<len; ++i) {
406                                 char buf[16];
407                                 int nr_1    = get_irn_node_nr(curr_path[i-1]);
408                                 int nr_2    = get_irn_node_nr(curr_path[i]);
409                                 int var_idx = lpp_get_var_idx(ienv->lp, name_cdd_sorted(buf, 'y', nr_1, nr_2));
410                                 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
411                         }
412                 }
413
414                 /* this path cannot be extended anymore */
415                 goto end;
416         }
417
418
419
420         /* recursively extend the path */
421         aff = get_affinity_info(ienv->co, irn);
422         co_gs_foreach_neighb(aff, nbr)
423                 extend_path(ienv, path, nbr->irn);
424
425
426 end:
427         /* remove the irn */
428         pdeq_getr(path);
429
430 }
431
432 /**
433  *  Search a path of affinity edges, whose ends are connected
434  *  by an interference edge and there are no other interference
435  *  edges in between.
436  *  Then at least one of these affinity edges must break.
437  */
438 static void build_path_cstr(ilp_env_t *ienv) {
439         affinity_node_t *aff_info;
440
441         /* for each node with affinity edges */
442         co_gs_foreach_aff_node(ienv->co, aff_info) {
443                 pdeq *path = new_pdeq();
444
445                 extend_path(ienv, path, aff_info->irn);
446
447                 del_pdeq(path);
448         }
449 }
450
451 static void ilp2_build(ilp_env_t *ienv) {
452         local_env_t *lenv = ienv->env;
453         int lower_bound;
454
455         ienv->lp = new_lpp(ienv->co->name, lpp_minimize);
456         build_coloring_cstr(ienv);
457         build_interference_cstr(ienv);
458         build_affinity_cstr(ienv);
459         build_clique_star_cstr(ienv);
460         build_path_cstr(ienv);
461
462         lower_bound = co_get_lower_bound(ienv->co) - co_get_inevit_copy_costs(ienv->co);
463         lpp_set_bound(ienv->lp, lower_bound);
464         lpp_set_time_limit(ienv->lp, lenv->time_limit);
465 }
466
467 static void ilp2_apply(ilp_env_t *ienv) {
468         local_env_t *lenv = ienv->env;
469         double *sol;
470         lpp_sol_state_t state;
471         int i, count;
472
473         /* first check if there was sth. to optimize */
474         if (lenv->first_x_var >= 0) {
475
476                 count = lenv->last_x_var - lenv->first_x_var + 1;
477                 sol = xmalloc(count * sizeof(sol[0]));
478                 state = lpp_get_solution(ienv->lp, sol, lenv->first_x_var, lenv->last_x_var);
479                 if (state != lpp_optimal) {
480                         printf("WARNING %s: Solution state is not 'optimal': %d\n", ienv->co->name, state);
481                         assert(state >= lpp_feasible && "The solution should at least be feasible!");
482                 }
483
484                 for (i=0; i<count; ++i) {
485                         int nodenr, color;
486                         char var_name[16];
487
488                         if (sol[i] > 1-EPSILON) { /* split variable name into components */
489                                 lpp_get_var_name(ienv->lp, lenv->first_x_var+i, var_name, sizeof(var_name));
490
491                                 if (sscanf(var_name, "x_%d_%d", &nodenr, &color) == 2) {
492                                         ir_node *irn = pmap_get(lenv->nr_2_irn, INT_TO_PTR(nodenr));
493                                         assert(irn && "This node number must be present in the map");
494
495                                         set_irn_col(ienv->co, irn, color);
496                                 } else
497                                         assert(0 && "This should be a x-var");
498                         }
499                 }
500         }
501
502 #ifdef COPYOPT_STAT
503         /* TODO adapt to multiple possible ILPs */
504         copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp)));  //now we have ms
505         copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
506         copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
507         copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
508 #endif
509 }
510
511 int co_solve_ilp2(copy_opt_t *co) {
512         lpp_sol_state_t sol_state;
513         ilp_env_t *ienv;
514         local_env_t my;
515
516         ASSERT_OU_AVAIL(co); //See build_clique_st
517         ASSERT_GS_AVAIL(co);
518
519         my.time_limit  = 0;
520         my.first_x_var = -1;
521         my.last_x_var  = -1;
522         my.nr_2_irn    = pmap_create();
523         FIRM_DBG_REGISTER(my.dbg, "firm.be.coilp2");
524
525         ienv = new_ilp_env(co, ilp2_build, ilp2_apply, &my);
526
527         sol_state = ilp_go(ienv);
528
529         pmap_destroy(my.nr_2_irn);
530         free_ilp_env(ienv);
531
532         return sol_state == lpp_optimal;
533 }
534
535 #else /* WITH_ILP */
536
537 static INLINE void only_that_you_can_compile_without_WITH_ILP_defined(void) {
538 }
539
540 #endif /* WITH_ILP */