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