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