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