becopyilp: Turn the colouring suffix linked list into an array.
[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         res->col_suff = NEW_ARR_F(ir_node*, 0);
90         ir_nodeset_init(&res->all_removed);
91
92         return res;
93 }
94
95 /**
96  * Checks if a node is simplicial in the graph heeding the already removed nodes.
97  */
98 static inline bool sr_is_simplicial(size_red_t *sr, const ir_node *ifn)
99 {
100         bool              res = true;
101         ir_node         **all = NEW_ARR_F(ir_node*, 0);
102         be_lv_t    *const lv  = be_get_irg_liveness(sr->co->irg);
103         neighbours_iter_t iter;
104         be_ifg_foreach_neighbour(sr->co->cenv->ifg, &iter, ifn, curr) {
105                 /* Only consider non-removed neighbours. */
106                 if (sr_is_removed(sr, curr))
107                         continue;
108
109                 /* Check whether the current node forms a clique with all previous nodes. */
110                 for (size_t i = ARR_LEN(all); i-- != 0;) {
111                         if (!be_values_interfere(lv, curr, all[i])) {
112                                 res = false;
113                                 goto end;
114                         }
115                 }
116
117                 ARR_APP1(ir_node*, all, curr);
118         }
119
120 end:
121         DEL_ARR_F(all);
122         return res;
123 }
124
125 /**
126  * Virtually remove all nodes not related to the problem
127  * (simplicial AND not adjacent to a equal-color-edge)
128  */
129 static void sr_remove(size_red_t *const sr)
130 {
131         bool redo = true;
132         const be_ifg_t *ifg = sr->co->cenv->ifg;
133
134         while (redo) {
135                 redo = false;
136                 be_ifg_foreach_node(ifg, irn) {
137                         const arch_register_req_t *req = arch_get_irn_register_req(irn);
138                         if (arch_register_req_is(req, limited) || sr_is_removed(sr, irn))
139                                 continue;
140                         if (co_gs_is_optimizable(sr->co, irn))
141                                 continue;
142                         if (!sr_is_simplicial(sr, irn))
143                                 continue;
144
145                         ARR_APP1(ir_node*, sr->col_suff, irn);
146                         ir_nodeset_insert(&sr->all_removed, irn);
147
148                         redo = true;
149                 }
150         }
151 }
152
153 /**
154  * Virtually reinsert the nodes removed before and color them
155  */
156 static void sr_reinsert(size_red_t *const sr)
157 {
158         ir_graph *irg        = sr->co->irg;
159         be_ifg_t *ifg        = sr->co->cenv->ifg;
160         unsigned  n_regs     = arch_register_class_n_regs(sr->co->cls);
161
162         unsigned *const allocatable_cols = rbitset_alloca(n_regs);
163         be_set_allocatable_regs(irg, sr->co->cls, allocatable_cols);
164
165         unsigned *const possible_cols = rbitset_alloca(n_regs);
166         neighbours_iter_t iter;
167
168         /* color the removed nodes in right order */
169         for (size_t i = ARR_LEN(sr->col_suff); i-- != 0;) {
170                 ir_node *const irn = sr->col_suff[i];
171
172                 rbitset_copy(possible_cols, allocatable_cols, n_regs);
173
174                 /* get free color by inspecting all neighbors */
175                 be_ifg_foreach_neighbour(ifg, &iter, irn, other) {
176                         const arch_register_req_t *cur_req;
177                         unsigned cur_col;
178
179                         /* only inspect nodes which are in graph right now */
180                         if (sr_is_removed(sr, other))
181                                 continue;
182
183                         cur_req = arch_get_irn_register_req(other);
184                         cur_col = get_irn_col(other);
185
186                         /* Invalidate all single size register when it is a large one */
187                         do  {
188                                 rbitset_clear(possible_cols, cur_col);
189                                 ++cur_col;
190                         } while ((cur_col % cur_req->width) != 0);
191                 }
192
193                 /* now all bits not set are possible colors */
194                 /* take one that matches the alignment constraint */
195                 unsigned free_col;
196                 assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
197                 for (;;) {
198                         free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
199                         if (free_col % arch_get_irn_register_req(irn)->width == 0)
200                                 break;
201                         ++free_col;
202                         assert(free_col < n_regs);
203                 }
204                 set_irn_col(sr->co->cls, irn, free_col);
205                 ir_nodeset_remove(&sr->all_removed, irn); /* irn is back in graph again */
206         }
207 }
208
209 /**
210  * Free all space.
211  */
212 static void free_size_red(size_red_t *const sr)
213 {
214         ir_nodeset_destroy(&sr->all_removed);
215         DEL_ARR_F(sr->col_suff);
216         free(sr);
217 }
218
219 /******************************************************************************
220     _____                      _        _____ _      _____
221    / ____|                    (_)      |_   _| |    |  __ \
222   | |  __  ___ _ __   ___ _ __ _  ___    | | | |    | |__) |
223   | | |_ |/ _ \ '_ \ / _ \ '__| |/ __|   | | | |    |  ___/
224   | |__| |  __/ | | |  __/ |  | | (__   _| |_| |____| |
225    \_____|\___|_| |_|\___|_|  |_|\___| |_____|______|_|
226
227  *****************************************************************************/
228
229 #include <stdio.h>
230
231 ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
232 {
233         ilp_env_t *res = XMALLOC(ilp_env_t);
234
235         res->co         = co;
236         res->build      = build;
237         res->apply      = apply;
238         res->env        = env;
239         res->sr         = new_size_red(co);
240
241         return res;
242 }
243
244 lpp_sol_state_t ilp_go(ilp_env_t *ienv)
245 {
246         ir_graph *irg = ienv->co->irg;
247
248         sr_remove(ienv->sr);
249
250         ienv->build(ienv);
251
252         if (dump_flags & DUMP_ILP) {
253                 char buf[128];
254                 FILE *f;
255
256                 ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
257                             ienv->co->cenv->cls->name);
258                 f = fopen(buf, "wt");
259                 if (f == NULL) {
260                         panic("Couldn't open '%s' for writing", buf);
261                 }
262                 lpp_dump_plain(ienv->lp, f);
263                 fclose(f);
264         }
265
266         lpp_set_time_limit(ienv->lp, time_limit);
267         if (solve_log)
268                 lpp_set_log(ienv->lp, stdout);
269
270         lpp_solve(ienv->lp, be_options.ilp_server, be_options.ilp_solver);
271
272         //stat_ev_dbl("co_ilp_objval",     ienv->lp->objval);
273         //stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
274         stat_ev_int("co_ilp_iter",       lpp_get_iter_cnt(ienv->lp));
275         stat_ev_dbl("co_ilp_sol_time",   lpp_get_sol_time(ienv->lp));
276
277         ienv->apply(ienv);
278
279         sr_reinsert(ienv->sr);
280
281         return lpp_get_sol_state(ienv->lp);
282 }
283
284 void free_ilp_env(ilp_env_t *ienv)
285 {
286         free_size_red(ienv->sr);
287         lpp_free(ienv->lp);
288         free(ienv);
289 }