4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
20 #include "becopyopt.h"
21 #include "becopystat.h"
23 #define DEBUG_LVL SET_LEVEL_1
24 static firm_dbg_module_t *dbg = NULL;
26 #define SLOTS_LIVING 32
29 * Represents the _costs_ if node n and m have different colors.
32 #define get_weight(n,m) 1
34 typedef struct _simpl_t {
35 struct list_head chain;
39 typedef struct _problem_instance_t {
40 const copy_opt_t *co; /** the original copy_opt problem */
41 /* problem size reduction removing simple nodes */
42 struct list_head simplicials; /**< holds all simpl_t's in right order to color*/
43 pset *removed; /**< holds all removed simplicial irns */
45 lpp_t *dilp; /**< problem formulation directly as milp */
47 lpp_t *curr_lp; /**< points to the problem currently used */
48 int curr_color, cst_counter, last_x_var;
52 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
54 * Some stuff for variable name handling.
56 #define mangle_cst(buf, prefix, nr) \
57 snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
59 #define mangle_var(buf, prefix, node_nr, color) \
60 snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
62 #define mangle_var_irn(buf, prefix, irn, color) \
63 mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
65 #define split_var(var, nnr, col) \
66 sscanf(var, "x%d_%d", (nnr), (col))
70 * Checks if a node is simplicial in the graph
71 * heeding the already removed nodes.
73 static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
75 if_node_t **all, *curr;
76 all = alloca(ifn_get_degree(ifn) * sizeof(*all));
78 /* get all non-removed neighbors */
79 foreach_neighb(ifn, curr)
80 if (!is_removed(curr))
83 /* check if these form a clique */
84 for (i=0; i<size; ++i)
85 for (o=i+1; o<size; ++o)
86 if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
89 /* all edges exist so this is a clique */
94 * Iterative finds and 'removes' from the graph all nodes which are
95 * simplicial AND not member of a equal-color-wish
97 static void pi_find_simplicials(problem_instance_t *pi) {
102 if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
105 for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
106 ir_node *irn = get_irn_for_graph_nr(pi->co->irg, ifn->nnr);
107 if (!is_removed(irn) && !is_optimizable(irn) &&
108 !is_optimizable_arg(pi->co, irn) && pi_is_simplicial(pi, ifn)) {
109 simpl_t *s = xmalloc(sizeof(*s));
111 list_add(&s->chain, &pi->simplicials);
112 pset_insert_ptr(pi->removed, irn);
114 DBG((dbg, LEVEL_2, " Removed %n\n", irn));
121 * Add coloring-force conditions
123 static void pi_add_constr_A(ir_node *block, void *env) {
124 problem_instance_t *pi = env;
125 struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
127 bitset_t *pos_regs = bitset_alloca(pi->co->cls->n_regs);
129 list_for_each_entry_reverse(border_t, curr, head, list)
130 if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
131 int cst_idx, nnr, col;
133 nnr = get_irn_graph_nr(curr->irn);
134 mangle_cst(pi->buf, 'A', nnr);
135 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, equal, 1);
137 // iterate over all possible colors in order
138 bitset_clear_all(pos_regs);
139 arch_get_allocatable_regs(pi->co->env, curr->irn, arch_pos_make_out(0), pi->co->cls, pos_regs);
140 bitset_foreach(pos_regs, col) {
142 mangle_var(pi->buf, 'x', nnr, col);
143 var_idx = lpp_add_var(pi->curr_lp, pi->buf, binary, 0);
144 pi->last_x_var = var_idx;
145 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
151 * Checks if all nodes in @p living are live in in block @p block.
152 * @return 1 if all are live in
155 static INLINE int all_live_in(ir_node *block, pset *living) {
157 for (n = pset_first(living); n; n = pset_next(living))
158 if (!is_live_in(block, n)) {
166 * Finds cliques in the interference graph, considering only nodes
167 * for which the color pi->curr_color is possible. Finds only 'maximal-cliques',
168 * viz cliques which are not contained in another one.
169 * This is used for the matrix B.
172 static void pi_add_constr_B(ir_node *block, void *env) {
173 problem_instance_t *pi = env;
174 enum phase_t {growing, shrinking} phase = growing;
175 struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
177 pset *living = pset_new_ptr(SLOTS_LIVING);
179 list_for_each_entry_reverse(border_t, b, head, list) {
180 const ir_node *irn = b->irn;
185 DBG((dbg, LEVEL_2, "Def %n\n", irn));
186 pset_insert_ptr(living, irn);
188 } else { /* is_use */
189 DBG((dbg, LEVEL_2, "Use %n\n", irn));
191 /* before shrinking the set, store the current 'maximum' clique;
192 * do NOT if clique is a single node
193 * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
194 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
197 mangle_cst(pi->buf, 'B', pi->cst_counter);
198 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, less, 1);
199 for (n = pset_first(living); n; n = pset_next(living)) {
201 mangle_var_irn(pi->buf, 'x', n, pi->curr_color);
202 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
204 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
208 pset_remove_ptr(living, irn);
216 static void pi_add_constr_E(problem_instance_t *pi) {
218 bitset_t *root_regs, *arg_regs;
219 root_regs = bitset_alloca(pi->co->cls->n_regs);
220 arg_regs = bitset_alloca(pi->co->cls->n_regs);
222 /* for all roots of optimization units */
223 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
224 const ir_node *root, *arg;
225 int rootnr, argnr, color;
226 int y_idx, i, cst_counter = 0;
229 root = curr->nodes[0];
230 rootnr = get_irn_graph_nr(root);
231 bitset_clear_all(root_regs);
232 arch_get_allocatable_regs(pi->co->env, root, arch_pos_make_out(0), pi->co->cls, root_regs);
234 /* for all arguments of root */
235 for (i = 1; i < curr->node_count; ++i) {
236 arg = curr->nodes[i];
237 argnr = get_irn_graph_nr(arg);
238 bitset_clear_all(arg_regs);
239 arch_get_allocatable_regs(pi->co->env, arg, arch_pos_make_out(0), pi->co->cls, arg_regs);
241 /* Introduce new variable and set factor in objective function */
242 y_idx = lpp_add_var(pi->curr_lp, NULL, real, get_weight(root, arg));
244 /* For all colors root and arg have in common, add 2 constraints to E */
245 bitset_and(arg_regs, root_regs);
246 bitset_foreach(arg_regs, color) {
247 int root_idx, arg_idx, cst_idx;
248 mangle_var(buf, 'x', rootnr, color);
249 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
250 mangle_var(buf, 'x', argnr, color);
251 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
253 /* add root-arg+y <= 1 */
254 mangle_cst(buf, 'E', cst_counter++);
255 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
256 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
257 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
258 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
260 /* add arg-root+y <= 1 */
261 mangle_cst(buf, 'E', cst_counter++);
262 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
263 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
264 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
265 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
272 * Generate the initial problem matrices and vectors.
274 static problem_instance_t *new_pi(const copy_opt_t *co) {
275 problem_instance_t *pi;
277 DBG((dbg, LEVEL_1, "Generating new instance...\n"));
278 pi = xcalloc(1, sizeof(*pi));
280 pi->removed = pset_new_ptr_default();
281 INIT_LIST_HEAD(&pi->simplicials);
282 pi->dilp = new_lpp(co->name, minimize);
284 /* problem size reduction */
285 pi_find_simplicials(pi);
286 //TODO dump_ifg_w/o_removed
288 pi->curr_lp = pi->dilp;
290 /* Matrix A: knapsack constraint for each node */
291 dom_tree_walk_irg(co->irg, pi_add_constr_A, NULL, pi);
292 /* Matrix B: interference constraints using cliques */
293 for (pi->curr_color = 0; pi->curr_color < pi->co->cls->n_regs; ++pi->curr_color)
294 dom_tree_walk_irg(co->irg, pi_add_constr_B, NULL, pi);
295 /* Matrix E weights for the 'same-color-optimization' target */
301 * Clean the problem instance
303 static void free_pi(problem_instance_t *pi) {
304 DBG((dbg, LEVEL_1, "Free instance...\n"));
305 /* pi->simplicials get freed during apply_solution */
307 del_pset(pi->removed);
312 * Set starting values for the mip problem according
313 * to the current coloring of the graph.
315 static void pi_set_start_sol(problem_instance_t *pi) {
317 for (i=1; i<=pi->last_x_var; ++i) {
320 /* get variable name */
321 const char *var_name = lpp_get_var_name(pi->curr_lp, i);
322 /* split into components */
323 if (split_var(var_name, &nnr, &col) == 2) {
324 assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) != -1);
325 val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr)) == col) ? 1 : 0;
326 lpp_set_start_value(pi->curr_lp, i, val);
328 assert(0 && "x vars always look like this 'x123_45'");
335 static void pi_solve_ilp(problem_instance_t *pi) {
336 pi_set_start_sol(pi);
337 lpp_solve(pi->curr_lp, 1);
341 * Set the color of all simplicial nodes removed form
342 * the graph before transforming it to an ilp.
344 static void pi_set_simplicials(problem_instance_t *pi) {
345 simpl_t *simpl, *tmp;
346 bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->cls));
348 /* color the simplicial nodes in right order */
349 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
351 ir_node *other_irn, *irn;
352 if_node_t *other, *ifn;
354 /* get free color by inspecting all neighbors */
356 irn = get_irn_for_graph_nr(pi->co->irg, ifn->nnr);
357 bitset_clear_all(used_cols);
358 foreach_neighb(ifn, other) {
359 other_irn = get_irn_for_graph_nr(pi->co->irg, other->nnr);
360 if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
361 bitset_set(used_cols, get_irn_col(pi->co, other_irn));
364 /* now all bits not set are possible colors */
365 free_col = bitset_next_clear(used_cols, 0);
366 assert(free_col != -1 && "No free color found. This can not be.");
367 set_irn_col(pi->co, irn, free_col);
368 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
374 * Sets the colors of irns according to the values of variables
375 * provided by the solution of the solver.
377 static void pi_apply_solution(problem_instance_t *pi) {
378 // else if (vars_section && sscanf(buf, "x%d_%d %d", &num, &col, &val) == 3 && val == 1) {
379 // set_irn_col(lpp, get_irn_for_graph_nr(lpp->irg, num), col);
382 DBG((dbg, LEVEL_1, "Applying solution...\n"));
388 sol = xmalloc(pi->last_x_var * sizeof(*sol));
389 lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
390 for (i=0; i<pi->last_x_var; ++i)
391 if (sol[i] == 1) { /* split varibale name into components */
393 const char *var_name = lpp_get_var_name(pi->curr_lp, 1+i);
394 if (split_var(var_name, &nnr, &col) == 2) {
395 DBG((dbg, LEVEL_2, " x%d = %d\n", nnr, col));
396 set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->irg, nnr), col);
398 assert(0 && "this should be a x-var");
400 pi_set_simplicials(pi);
403 void co_ilp_opt(copy_opt_t *co) {
404 problem_instance_t *pi;
405 dbg = firm_dbg_register("ir.be.copyoptilp");
406 if (!strcmp(co->name, DEBUG_IRG))
407 firm_dbg_set_mask(dbg, -1);
409 firm_dbg_set_mask(dbg, DEBUG_LVL);
413 pi_apply_solution(pi);