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