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