becopyilp: Use a ir_nodeset instead of a pset for all_removed.
[libfirm] / ir / be / becopyilp.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Common stuff used by all ILP formulations.
9  * @author      Daniel Grund
10  * @date        28.02.2006
11  */
12 #include "config.h"
13
14 #include <stdbool.h>
15
16 #include "be_t.h"
17 #include "beintlive_t.h"
18 #include "beirg.h"
19 #include "irtools.h"
20 #include "irprintf.h"
21
22 #include "statev_t.h"
23 #include "bemodule.h"
24 #include "error.h"
25
26 #include "lpp.h"
27
28 #include "lc_opts.h"
29 #include "lc_opts_enum.h"
30
31 #define DUMP_ILP 1
32 #define DUMP_SOL 2
33
34 static int time_limit = 60;
35 static int solve_log  = 0;
36 static unsigned dump_flags = 0;
37
38 static const lc_opt_enum_mask_items_t dump_items[] = {
39         { "ilp",   DUMP_ILP },
40         { "sol",   DUMP_SOL },
41         { NULL,    0 }
42 };
43
44 static lc_opt_enum_mask_var_t dump_var = {
45         &dump_flags, dump_items
46 };
47
48 static const lc_opt_table_entry_t options[] = {
49         LC_OPT_ENT_INT      ("limit", "time limit for solving in seconds (0 for unlimited)", &time_limit),
50         LC_OPT_ENT_BOOL     ("log",   "show ilp solving log",              &solve_log),
51         LC_OPT_ENT_ENUM_MASK("dump",  "dump flags",             &dump_var),
52         LC_OPT_LAST
53 };
54
55 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp)
56 void be_init_copyilp(void)
57 {
58         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
59         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
60         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
61         lc_opt_entry_t *co_grp = lc_opt_get_grp(chordal_grp, "co");
62         lc_opt_entry_t *ilp_grp = lc_opt_get_grp(co_grp, "ilp");
63
64         lc_opt_add_table(ilp_grp, options);
65 }
66
67 #include "becopyilp_t.h"
68 #include "beifg.h"
69
70 /******************************************************************************
71     _____ _                        _            _   _
72    / ____(_)                      | |          | | (_)
73   | (___  _ _______   _ __ ___  __| |_   _  ___| |_ _  ___  _ __
74    \___ \| |_  / _ \ | '__/ _ \/ _` | | | |/ __| __| |/ _ \| '_ \
75    ____) | |/ /  __/ | | |  __/ (_| | |_| | (__| |_| | (_) | | | |
76   |_____/|_/___\___| |_|  \___|\__,_|\__,_|\___|\__|_|\___/|_| |_|
77
78  *****************************************************************************/
79
80
81 /**
82  * Just prepare. Do nothing yet.
83  */
84 static size_red_t *new_size_red(copy_opt_t *co)
85 {
86         size_red_t *res = XMALLOC(size_red_t);
87
88         res->co = co;
89         ir_nodeset_init(&res->all_removed);
90         res->col_suff = NULL;
91         obstack_init(&res->ob);
92
93         return res;
94 }
95
96 /**
97  * Checks if a node is simplicial in the graph heeding the already removed nodes.
98  */
99 static inline bool sr_is_simplicial(size_red_t *sr, const ir_node *ifn)
100 {
101         bool              res = true;
102         ir_node         **all = NEW_ARR_F(ir_node*, 0);
103         be_lv_t    *const lv  = be_get_irg_liveness(sr->co->irg);
104         neighbours_iter_t iter;
105         be_ifg_foreach_neighbour(sr->co->cenv->ifg, &iter, ifn, curr) {
106                 /* Only consider non-removed neighbours. */
107                 if (sr_is_removed(sr, curr))
108                         continue;
109
110                 /* Check whether the current node forms a clique with all previous nodes. */
111                 for (size_t i = ARR_LEN(all); i-- != 0;) {
112                         if (!be_values_interfere(lv, curr, all[i])) {
113                                 res = false;
114                                 goto end;
115                         }
116                 }
117
118                 ARR_APP1(ir_node*, all, curr);
119         }
120
121 end:
122         DEL_ARR_F(all);
123         return res;
124 }
125
126 /**
127  * Virtually remove all nodes not related to the problem
128  * (simplicial AND not adjacent to a equal-color-edge)
129  */
130 static void sr_remove(size_red_t *const sr)
131 {
132         bool redo = true;
133         const be_ifg_t *ifg = sr->co->cenv->ifg;
134
135         while (redo) {
136                 redo = false;
137                 be_ifg_foreach_node(ifg, irn) {
138                         const arch_register_req_t *req = arch_get_irn_register_req(irn);
139                         coloring_suffix_t *cs;
140
141                         if (arch_register_req_is(req, limited) || sr_is_removed(sr, irn))
142                                 continue;
143                         if (co_gs_is_optimizable(sr->co, irn))
144                                 continue;
145                         if (!sr_is_simplicial(sr, irn))
146                                 continue;
147
148                         cs = OALLOC(&sr->ob, coloring_suffix_t);
149                         cs->irn = irn;
150                         cs->next = sr->col_suff;
151                         sr->col_suff = cs;
152
153                         ir_nodeset_insert(&sr->all_removed, irn);
154
155                         redo = true;
156                 }
157         }
158 }
159
160 /**
161  * Virtually reinsert the nodes removed before and color them
162  */
163 static void sr_reinsert(size_red_t *const sr)
164 {
165         coloring_suffix_t *cs;
166         ir_graph *irg        = sr->co->irg;
167         be_ifg_t *ifg        = sr->co->cenv->ifg;
168         unsigned  n_regs     = arch_register_class_n_regs(sr->co->cls);
169
170         unsigned *const allocatable_cols = rbitset_alloca(n_regs);
171         be_set_allocatable_regs(irg, sr->co->cls, allocatable_cols);
172
173         unsigned *const possible_cols = rbitset_alloca(n_regs);
174         neighbours_iter_t iter;
175
176         /* color the removed nodes in right order */
177         for (cs = sr->col_suff; cs; cs = cs->next) {
178                 unsigned free_col;
179                 ir_node *irn = cs->irn;
180
181                 rbitset_copy(possible_cols, allocatable_cols, n_regs);
182
183                 /* get free color by inspecting all neighbors */
184                 be_ifg_foreach_neighbour(ifg, &iter, irn, other) {
185                         const arch_register_req_t *cur_req;
186                         unsigned cur_col;
187
188                         /* only inspect nodes which are in graph right now */
189                         if (sr_is_removed(sr, other))
190                                 continue;
191
192                         cur_req = arch_get_irn_register_req(other);
193                         cur_col = get_irn_col(other);
194
195                         /* Invalidate all single size register when it is a large one */
196                         do  {
197                                 rbitset_clear(possible_cols, cur_col);
198                                 ++cur_col;
199                         } while ((cur_col % cur_req->width) != 0);
200                 }
201
202                 /* now all bits not set are possible colors */
203                 /* take one that matches the alignment constraint */
204                 free_col = 0;
205                 assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
206                 while (true) {
207                         free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
208                         if (free_col % arch_get_irn_register_req(irn)->width == 0)
209                                 break;
210                         ++free_col;
211                         assert(free_col < n_regs);
212                 }
213                 set_irn_col(sr->co->cls, irn, free_col);
214                 ir_nodeset_remove(&sr->all_removed, irn); /* irn is back in graph again */
215         }
216 }
217
218 /**
219  * Free all space.
220  */
221 static void free_size_red(size_red_t *const sr)
222 {
223         obstack_free(&sr->ob, NULL);
224         ir_nodeset_destroy(&sr->all_removed);
225         free(sr);
226 }
227
228 /******************************************************************************
229     _____                      _        _____ _      _____
230    / ____|                    (_)      |_   _| |    |  __ \
231   | |  __  ___ _ __   ___ _ __ _  ___    | | | |    | |__) |
232   | | |_ |/ _ \ '_ \ / _ \ '__| |/ __|   | | | |    |  ___/
233   | |__| |  __/ | | |  __/ |  | | (__   _| |_| |____| |
234    \_____|\___|_| |_|\___|_|  |_|\___| |_____|______|_|
235
236  *****************************************************************************/
237
238 #include <stdio.h>
239
240 ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
241 {
242         ilp_env_t *res = XMALLOC(ilp_env_t);
243
244         res->co         = co;
245         res->build      = build;
246         res->apply      = apply;
247         res->env        = env;
248         res->sr         = new_size_red(co);
249
250         return res;
251 }
252
253 lpp_sol_state_t ilp_go(ilp_env_t *ienv)
254 {
255         ir_graph *irg = ienv->co->irg;
256
257         sr_remove(ienv->sr);
258
259         ienv->build(ienv);
260
261         if (dump_flags & DUMP_ILP) {
262                 char buf[128];
263                 FILE *f;
264
265                 ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
266                             ienv->co->cenv->cls->name);
267                 f = fopen(buf, "wt");
268                 if (f == NULL) {
269                         panic("Couldn't open '%s' for writing", buf);
270                 }
271                 lpp_dump_plain(ienv->lp, f);
272                 fclose(f);
273         }
274
275         lpp_set_time_limit(ienv->lp, time_limit);
276         if (solve_log)
277                 lpp_set_log(ienv->lp, stdout);
278
279         lpp_solve(ienv->lp, be_options.ilp_server, be_options.ilp_solver);
280
281         //stat_ev_dbl("co_ilp_objval",     ienv->lp->objval);
282         //stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
283         stat_ev_int("co_ilp_iter",       lpp_get_iter_cnt(ienv->lp));
284         stat_ev_dbl("co_ilp_sol_time",   lpp_get_sol_time(ienv->lp));
285
286         ienv->apply(ienv);
287
288         sr_reinsert(ienv->sr);
289
290         return lpp_get_sol_state(ienv->lp);
291 }
292
293 void free_ilp_env(ilp_env_t *ienv)
294 {
295         free_size_red(ienv->sr);
296         lpp_free(ienv->lp);
297         free(ienv);
298 }