2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Common stuff used by all ILP formulations.
23 * @author Daniel Grund
33 #include "bestatevent.h"
41 #include "lc_opts_enum.h"
46 static int time_limit = 60;
47 static int solve_log = 0;
48 static unsigned dump_flags = 0;
50 static const lc_opt_enum_mask_items_t dump_items[] = {
56 static lc_opt_enum_mask_var_t dump_var = {
57 &dump_flags, dump_items
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),
67 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp)
68 void be_init_copyilp(void)
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");
76 lc_opt_add_table(ilp_grp, options);
79 #include "becopyilp_t.h"
82 /******************************************************************************
85 | (___ _ _______ _ __ ___ __| |_ _ ___| |_ _ ___ _ __
86 \___ \| |_ / _ \ | '__/ _ \/ _` | | | |/ __| __| |/ _ \| '_ \
87 ____) | |/ / __/ | | | __/ (_| | |_| | (__| |_| | (_) | | | |
88 |_____/|_/___\___| |_| \___|\__,_|\__,_|\___|\__|_|\___/|_| |_|
90 *****************************************************************************/
93 size_red_t *new_size_red(copy_opt_t *co)
95 size_red_t *res = XMALLOC(size_red_t);
98 res->all_removed = pset_new_ptr_default();
100 obstack_init(&res->ob);
106 * Checks if a node is simplicial in the graph heeding the already removed nodes.
108 static inline bool sr_is_simplicial(size_red_t *sr, const ir_node *ifn)
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));
118 /* get all non-removed neighbors */
119 be_ifg_foreach_neighbour(ifg, &iter, ifn, curr)
120 if (!sr_is_removed(sr, curr))
123 /* check if these form a clique */
124 for (i=0; i<size; ++i)
125 for (o=i+1; o<size; ++o)
126 if (!be_ifg_connected(ifg, all[i], all[o]))
129 /* all edges exist so this is a clique */
133 void sr_remove(size_red_t *sr)
137 const be_ifg_t *ifg = sr->co->cenv->ifg;
142 be_ifg_foreach_node(ifg, &iter, irn) {
143 const arch_register_req_t *req = arch_get_irn_register_req(irn);
144 coloring_suffix_t *cs;
146 if (arch_register_req_is(req, limited) || sr_is_removed(sr, irn))
148 if (co_gs_is_optimizable(sr->co, irn))
150 if (!sr_is_simplicial(sr, irn))
153 cs = OALLOC(&sr->ob, coloring_suffix_t);
155 cs->next = sr->col_suff;
158 pset_insert_ptr(sr->all_removed, irn);
165 void sr_reinsert(size_red_t *sr)
167 coloring_suffix_t *cs;
168 ir_graph *irg = sr->co->irg;
169 be_ifg_t *ifg = sr->co->cenv->ifg;
170 unsigned n_regs = arch_register_class_n_regs(sr->co->cls);
171 unsigned *possible_cols;
172 unsigned *allocatable_cols;
174 rbitset_alloca(allocatable_cols, n_regs);
175 be_set_allocatable_regs(irg, sr->co->cls, allocatable_cols);
177 rbitset_alloca(possible_cols, n_regs);
178 neighbours_iter_t iter;
180 /* color the removed nodes in right order */
181 for (cs = sr->col_suff; cs; cs = cs->next) {
184 ir_node *irn = cs->irn;
186 rbitset_copy(possible_cols, allocatable_cols, n_regs);
188 /* get free color by inspecting all neighbors */
189 be_ifg_foreach_neighbour(ifg, &iter, irn, other) {
190 const arch_register_req_t *cur_req;
193 /* only inspect nodes which are in graph right now */
194 if (sr_is_removed(sr, other))
197 cur_req = arch_get_irn_register_req(other);
198 cur_col = get_irn_col(other);
200 /* Invalidate all single size register when it is a large one */
202 rbitset_clear(possible_cols, cur_col);
204 } while ((cur_col % cur_req->width) != 0);
207 /* now all bits not set are possible colors */
208 /* take one that matches the alignment constraint */
210 assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
212 free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
213 if (free_col % arch_get_irn_register_req(irn)->width == 0)
216 assert(free_col < n_regs);
218 set_irn_col(sr->co->cls, irn, free_col);
219 pset_remove_ptr(sr->all_removed, irn); /* irn is back in graph again */
223 void free_size_red(size_red_t *sr)
225 del_pset(sr->all_removed);
226 obstack_free(&sr->ob, NULL);
230 /******************************************************************************
231 _____ _ _____ _ _____
232 / ____| (_) |_ _| | | __ \
233 | | __ ___ _ __ ___ _ __ _ ___ | | | | | |__) |
234 | | |_ |/ _ \ '_ \ / _ \ '__| |/ __| | | | | | ___/
235 | |__| | __/ | | | __/ | | | (__ _| |_| |____| |
236 \_____|\___|_| |_|\___|_| |_|\___| |_____|______|_|
238 *****************************************************************************/
242 ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
244 ilp_env_t *res = XMALLOC(ilp_env_t);
250 res->sr = new_size_red(co);
255 lpp_sol_state_t ilp_go(ilp_env_t *ienv)
257 ir_graph *irg = ienv->co->irg;
258 be_options_t *options = be_get_irg_options(irg);
264 if (dump_flags & DUMP_ILP) {
268 ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
269 ienv->co->cenv->cls->name);
270 f = fopen(buf, "wt");
272 panic("Couldn't open '%s' for writing", buf);
274 lpp_dump_plain(ienv->lp, f);
278 lpp_set_time_limit(ienv->lp, time_limit);
280 lpp_set_log(ienv->lp, stdout);
282 lpp_solve(ienv->lp, options->ilp_server, options->ilp_solver);
284 //be_stat_ev_dbl("co_ilp_objval", ienv->lp->objval);
285 //be_stat_ev_dbl("co_ilp_best_bound", ienv->lp->best_bound);
286 be_stat_ev ("co_ilp_iter", lpp_get_iter_cnt(ienv->lp));
287 be_stat_ev_dbl("co_ilp_sol_time", lpp_get_sol_time(ienv->lp));
291 sr_reinsert(ienv->sr);
293 return lpp_get_sol_state(ienv->lp);
296 void free_ilp_env(ilp_env_t *ienv)
298 free_size_red(ienv->sr);