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