2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Common stuff used by all ILP formulations.
17 #include "beintlive_t.h"
29 #include "lc_opts_enum.h"
34 static int time_limit = 60;
35 static int solve_log = 0;
36 static unsigned dump_flags = 0;
38 static const lc_opt_enum_mask_items_t dump_items[] = {
44 static lc_opt_enum_mask_var_t dump_var = {
45 &dump_flags, dump_items
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),
55 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp)
56 void be_init_copyilp(void)
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");
64 lc_opt_add_table(ilp_grp, options);
67 #include "becopyilp_t.h"
70 /******************************************************************************
73 | (___ _ _______ _ __ ___ __| |_ _ ___| |_ _ ___ _ __
74 \___ \| |_ / _ \ | '__/ _ \/ _` | | | |/ __| __| |/ _ \| '_ \
75 ____) | |/ / __/ | | | __/ (_| | |_| | (__| |_| | (_) | | | |
76 |_____/|_/___\___| |_| \___|\__,_|\__,_|\___|\__|_|\___/|_| |_|
78 *****************************************************************************/
82 * Just prepare. Do nothing yet.
84 static size_red_t *new_size_red(copy_opt_t *co)
86 size_red_t *res = XMALLOC(size_red_t);
89 res->col_suff = NEW_ARR_F(ir_node*, 0);
90 ir_nodeset_init(&res->all_removed);
96 * Checks if a node is simplicial in the graph heeding the already removed nodes.
98 static inline bool sr_is_simplicial(size_red_t *sr, const ir_node *ifn)
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))
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])) {
117 ARR_APP1(ir_node*, all, curr);
126 * Virtually remove all nodes not related to the problem
127 * (simplicial AND not adjacent to a equal-color-edge)
129 static void sr_remove(size_red_t *const sr)
132 const be_ifg_t *ifg = sr->co->cenv->ifg;
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))
140 if (co_gs_is_optimizable(sr->co, irn))
142 if (!sr_is_simplicial(sr, irn))
145 ARR_APP1(ir_node*, sr->col_suff, irn);
146 ir_nodeset_insert(&sr->all_removed, irn);
154 * Virtually reinsert the nodes removed before and color them
156 static void sr_reinsert(size_red_t *const sr)
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);
162 unsigned *const allocatable_cols = rbitset_alloca(n_regs);
163 be_set_allocatable_regs(irg, sr->co->cls, allocatable_cols);
165 unsigned *const possible_cols = rbitset_alloca(n_regs);
166 neighbours_iter_t iter;
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];
172 rbitset_copy(possible_cols, allocatable_cols, n_regs);
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;
179 /* only inspect nodes which are in graph right now */
180 if (sr_is_removed(sr, other))
183 cur_req = arch_get_irn_register_req(other);
184 cur_col = get_irn_col(other);
186 /* Invalidate all single size register when it is a large one */
188 rbitset_clear(possible_cols, cur_col);
190 } while ((cur_col % cur_req->width) != 0);
193 /* now all bits not set are possible colors */
194 /* take one that matches the alignment constraint */
196 assert(!rbitset_is_empty(possible_cols, n_regs) && "No free color found. This can not be.");
198 free_col = (unsigned)rbitset_next(possible_cols, free_col, true);
199 if (free_col % arch_get_irn_register_req(irn)->width == 0)
202 assert(free_col < n_regs);
204 set_irn_col(sr->co->cls, irn, free_col);
205 ir_nodeset_remove(&sr->all_removed, irn); /* irn is back in graph again */
212 static void free_size_red(size_red_t *const sr)
214 ir_nodeset_destroy(&sr->all_removed);
215 DEL_ARR_F(sr->col_suff);
219 /******************************************************************************
220 _____ _ _____ _ _____
221 / ____| (_) |_ _| | | __ \
222 | | __ ___ _ __ ___ _ __ _ ___ | | | | | |__) |
223 | | |_ |/ _ \ '_ \ / _ \ '__| |/ __| | | | | | ___/
224 | |__| | __/ | | | __/ | | | (__ _| |_| |____| |
225 \_____|\___|_| |_|\___|_| |_|\___| |_____|______|_|
227 *****************************************************************************/
231 ilp_env_t *new_ilp_env(copy_opt_t *co, ilp_callback build, ilp_callback apply, void *env)
233 ilp_env_t *res = XMALLOC(ilp_env_t);
239 res->sr = new_size_red(co);
244 lpp_sol_state_t ilp_go(ilp_env_t *ienv)
246 ir_graph *irg = ienv->co->irg;
252 if (dump_flags & DUMP_ILP) {
256 ir_snprintf(buf, sizeof(buf), "%F_%s-co.ilp", irg,
257 ienv->co->cenv->cls->name);
258 f = fopen(buf, "wt");
260 panic("Couldn't open '%s' for writing", buf);
262 lpp_dump_plain(ienv->lp, f);
266 lpp_set_time_limit(ienv->lp, time_limit);
268 lpp_set_log(ienv->lp, stdout);
270 lpp_solve(ienv->lp, be_options.ilp_server, be_options.ilp_solver);
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));
279 sr_reinsert(ienv->sr);
281 return lpp_get_sol_state(ienv->lp);
284 void free_ilp_env(ilp_env_t *ienv)
286 free_size_red(ienv->sr);