4 * Copyright: (c) Universitaet Karlsruhe
5 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
21 #include "lpp_local.h"
22 #include "lpp_remote.h"
24 #include "becopyopt.h"
25 #include "becopystat.h"
28 #define DEBUG_LVL SET_LEVEL_1
29 static firm_dbg_module_t *dbg = NULL;
31 #define SLOTS_LIVING 32
34 * Represents the _costs_ if node n and m have different colors.
37 #define get_weight(n,m) 1
39 typedef struct _simpl_t {
40 struct list_head chain;
44 typedef struct _problem_instance_t {
45 const copy_opt_t *co; /** the copy_opt problem */
46 /* problem size reduction removing simple nodes */
47 struct list_head simplicials; /**< holds all simpl_t's in right order to color*/
48 pset *removed; /**< holds all removed simplicial irns */
50 lpp_t *dilp; /**< problem formulation directly as milp */
52 lpp_t *curr_lp; /**< points to the problem currently used */
53 int curr_color, cst_counter, last_x_var;
58 #define is_removed(irn) pset_find_ptr(pi->removed, irn)
60 #define is_color_possible(irn,color) arch_reg_is_allocatable(pi->co->chordal_env->arch_env, irn, arch_pos_make_out(0), arch_register_for_index(pi->co->chordal_env->cls, color))
63 * Some stuff for variable name handling.
65 #define mangle_cst(buf, prefix, nr) \
66 snprintf((buf), sizeof(buf), "%c%d", (prefix), (nr))
68 #define mangle_var(buf, prefix, node_nr, color) \
69 snprintf((buf), sizeof(buf), "%c%d_%d", (prefix), (node_nr), (color))
71 #define mangle_var_irn(buf, prefix, irn, color) \
72 mangle_var((buf), (prefix), get_irn_graph_nr(irn), (color))
74 #define split_var(var, nnr, col) \
75 sscanf(var, "x%d_%d", (nnr), (col))
79 * Checks if a node is simplicial in the graph
80 * heeding the already removed nodes.
82 static INLINE int pi_is_simplicial(problem_instance_t *pi, const if_node_t *ifn) {
84 if_node_t **all, *curr;
85 all = alloca(ifn_get_degree(ifn) * sizeof(*all));
87 /* get all non-removed neighbors */
88 foreach_neighb(ifn, curr)
89 if (!is_removed(curr))
92 /* check if these form a clique */
93 for (i=0; i<size; ++i)
94 for (o=i+1; o<size; ++o)
95 if (!ifg_has_edge(pi->co->chordal_env, all[i], all[o]))
98 /* all edges exist so this is a clique */
103 * Iterative finds and 'removes' from the graph all nodes which are
104 * simplicial AND not member of a equal-color-wish
106 static void pi_find_simplicials(problem_instance_t *pi) {
111 DBG((dbg, LEVEL_2, "Find simlicials...\n"));
113 if_nodes = be_ra_get_ifg_nodes(pi->co->chordal_env);
116 for (ifn = set_first(if_nodes); ifn; ifn = set_next(if_nodes)) {
117 ir_node *irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
118 if (!is_removed(irn) && !is_optimizable(irn) &&
119 !is_optimizable_arg(pi->co, irn) && pi_is_simplicial(pi, ifn)) {
120 simpl_t *s = xmalloc(sizeof(*s));
122 list_add(&s->chain, &pi->simplicials);
123 pset_insert_ptr(pi->removed, irn);
125 DBG((dbg, LEVEL_2, " Removed %n\n", irn));
132 * Add coloring-force conditions
134 static void pi_add_constr_A(ir_node *block, void *env) {
135 problem_instance_t *pi = env;
136 struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
138 bitset_t *pos_regs = bitset_alloca(pi->co->chordal_env->cls->n_regs);
140 list_for_each_entry_reverse(border_t, curr, head, list)
141 if (curr->is_def && curr->is_real && !is_removed(curr->irn)) {
142 int cst_idx, nnr, col;
144 nnr = get_irn_graph_nr(curr->irn);
145 mangle_cst(pi->buf, 'A', nnr);
146 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, equal, 1);
148 // iterate over all possible colors in order
149 bitset_clear_all(pos_regs);
150 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, curr->irn, arch_pos_make_out(0), pi->co->chordal_env->cls, pos_regs);
151 bitset_foreach(pos_regs, col) {
153 mangle_var(pi->buf, 'x', nnr, col);
154 var_idx = lpp_add_var(pi->curr_lp, pi->buf, binary, 0);
155 pi->last_x_var = var_idx;
156 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
162 * Checks if all nodes in @p living are live in in block @p block.
163 * @return 1 if all are live in
166 static INLINE int all_live_in(ir_node *block, pset *living) {
168 for (n = pset_first(living); n; n = pset_next(living))
169 if (!is_live_in(block, n)) {
177 * Finds cliques in the interference graph, considering only nodes
178 * for which the color pi->curr_color is possible. Finds only 'maximal-cliques',
179 * viz cliques which are not contained in another one.
180 * This is used for the matrix B.
182 static void pi_add_constr_B(ir_node *block, void *env) {
183 problem_instance_t *pi = env;
184 enum phase_t {growing, shrinking} phase = growing;
185 struct list_head *head = get_block_border_head(pi->co->chordal_env, block);
187 pset *living = pset_new_ptr(SLOTS_LIVING);
189 list_for_each_entry_reverse(border_t, b, head, list) {
190 const ir_node *irn = b->irn;
191 if (is_removed(irn) || !is_color_possible(irn, pi->curr_color))
195 DBG((dbg, LEVEL_2, "Def %n\n", irn));
196 pset_insert_ptr(living, irn);
198 } else { /* is_use */
199 DBG((dbg, LEVEL_2, "Use %n\n", irn));
201 /* before shrinking the set, store the current 'maximum' clique;
202 * do NOT if clique is a single node
203 * do NOT if all values are live_in (in this case they were contained in a live-out clique elsewhere) */
204 if (phase == growing && pset_count(living) >= 2 && !all_live_in(block, living)) {
207 mangle_cst(pi->buf, 'B', pi->cst_counter);
208 cst_idx = lpp_add_cst(pi->curr_lp, pi->buf, less, 1);
209 for (n = pset_first(living); n; n = pset_next(living)) {
211 mangle_var_irn(pi->buf, 'x', n, pi->curr_color);
212 var_idx = lpp_get_var_idx(pi->curr_lp, pi->buf);
213 lpp_set_factor_fast(pi->curr_lp, cst_idx, var_idx, 1);
217 pset_remove_ptr(living, irn);
225 static void pi_add_constr_E(problem_instance_t *pi) {
227 bitset_t *root_regs, *arg_regs;
229 unsigned nregs = pi->co->chordal_env->cls->n_regs;
230 root_regs = bitset_alloca(nregs);
231 arg_regs = bitset_alloca(nregs);
233 /* for all roots of optimization units */
234 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
235 const ir_node *root, *arg;
236 int rootnr, argnr, color;
240 root = curr->nodes[0];
241 rootnr = get_irn_graph_nr(root);
242 bitset_clear_all(root_regs);
243 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, root, arch_pos_make_out(0), pi->co->chordal_env->cls, root_regs);
245 /* for all arguments of root */
246 for (i = 1; i < curr->node_count; ++i) {
247 arg = curr->nodes[i];
248 argnr = get_irn_graph_nr(arg);
249 bitset_clear_all(arg_regs);
250 arch_get_allocatable_regs(pi->co->chordal_env->arch_env, arg, arch_pos_make_out(0), pi->co->chordal_env->cls, arg_regs);
252 /* Introduce new variable and set factor in objective function */
253 mangle_var(buf, 'y', rootnr, argnr);
254 y_idx = lpp_add_var(pi->curr_lp, buf, continous, get_weight(root, arg));
255 /* set starting value */
256 //lpp_set_start_value(pi->curr_lp, y_idx, (get_irn_col(pi->co, root) != get_irn_col(pi->co, arg)));
258 /* For all colors root and arg have in common, add 2 constraints to E */
259 bitset_and(arg_regs, root_regs);
260 bitset_foreach(arg_regs, color) {
261 int root_idx, arg_idx, cst_idx;
262 mangle_var(buf, 'x', rootnr, color);
263 root_idx = lpp_get_var_idx(pi->curr_lp, buf);
264 mangle_var(buf, 'x', argnr, color);
265 arg_idx = lpp_get_var_idx(pi->curr_lp, buf);
267 /* add root-arg-y <= 0 */
268 mangle_cst(buf, 'E', cst_counter++);
269 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
270 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, 1);
271 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, -1);
272 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
274 /* add arg-root-y <= 0 */
275 mangle_cst(buf, 'E', cst_counter++);
276 cst_idx = lpp_add_cst(pi->curr_lp, buf, less, 0);
277 lpp_set_factor_fast(pi->curr_lp, cst_idx, root_idx, -1);
278 lpp_set_factor_fast(pi->curr_lp, cst_idx, arg_idx, 1);
279 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, -1);
286 * Sum(y_root_arg, arg \in Args) <= max_indep_set_size - 1
288 static void pi_add_constr_M(problem_instance_t *pi) {
292 /* for all optimization units */
293 list_for_each_entry(unit_t, curr, &pi->co->units, units) {
294 const ir_node *root, *arg;
296 int cst_idx, y_idx, i;
299 if (curr->ifg_mis_size == curr->node_count)
302 root = curr->nodes[0];
303 rootnr = get_irn_graph_nr(root);
304 mangle_cst(buf, 'M', cst_counter++);
305 cst_idx = lpp_add_cst(pi->curr_lp, buf, greater, curr->node_count - curr->ifg_mis_size);
307 /* for all arguments */
308 for (i = 1; i < curr->node_count; ++i) {
309 arg = curr->nodes[i];
310 argnr = get_irn_graph_nr(arg);
311 mangle_var(buf, 'y', rootnr, argnr);
312 y_idx = lpp_get_var_idx(pi->curr_lp, buf);
313 lpp_set_factor_fast(pi->curr_lp, cst_idx, y_idx, 1);
319 * Generate the initial problem matrices and vectors.
321 static problem_instance_t *new_pi(const copy_opt_t *co) {
322 problem_instance_t *pi;
324 DBG((dbg, LEVEL_2, "Generating new instance...\n"));
325 pi = xcalloc(1, sizeof(*pi));
327 pi->removed = pset_new_ptr_default();
328 INIT_LIST_HEAD(&pi->simplicials);
329 pi->dilp = new_lpp(co->name, minimize);
332 /* problem size reduction */
333 pi_find_simplicials(pi);
334 //TODO dump_ifg_w/o_removed
335 if (set_count(be_ra_get_ifg_nodes(pi->co->chordal_env)) == pset_count(pi->removed))
336 pi->all_simplicial = 1;
338 pi->curr_lp = pi->dilp;
340 /* Matrix A: knapsack constraint for each node */
341 DBG((dbg, LEVEL_2, "Add A constraints...\n"));
342 dom_tree_walk_irg(co->chordal_env->irg, pi_add_constr_A, NULL, pi);
343 /* Matrix B: interference constraints using cliques */
344 DBG((dbg, LEVEL_2, "Add B constraints...\n"));
345 for (pi->curr_color = 0; pi->curr_color < pi->co->chordal_env->cls->n_regs; ++pi->curr_color)
346 dom_tree_walk_irg(co->chordal_env->irg, pi_add_constr_B, NULL, pi);
347 /* Matrix E: interrelate x with y variables */
348 DBG((dbg, LEVEL_2, "Add E constraints...\n"));
350 /* Matrix M: maximum independent set constraints */
351 DBG((dbg, LEVEL_2, "Add M constraints...\n"));
352 //pi_add_constr_M(pi);
358 * Clean the problem instance
360 static void free_pi(problem_instance_t *pi) {
361 DBG((dbg, LEVEL_2, "Free instance...\n"));
362 /* pi->simplicials get freed during apply_solution */
364 del_pset(pi->removed);
369 * Set starting values for the mip problem according
370 * to the current coloring of the graph.
372 static void pi_set_start_sol(problem_instance_t *pi) {
375 DBG((dbg, LEVEL_2, "Set start solution...\n"));
376 for (i=1; i<=pi->last_x_var; ++i) {
379 /* get variable name */
380 lpp_get_var_name(pi->curr_lp, i, var_name, sizeof(var_name));
381 /* split into components */
382 if (split_var(var_name, &nnr, &col) == 2) {
383 assert(get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) != -1);
384 val = (get_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr)) == col) ? 1 : 0;
385 lpp_set_start_value(pi->curr_lp, i, val);
387 fprintf(stderr, "Variable name is: %s\n", var_name);
388 assert(0 && "x vars always look like this 'x123_45'");
396 static void pi_solve_ilp(problem_instance_t *pi, void (*lpp_solve)(lpp_t *)) {
397 pi_set_start_sol(pi);
398 lpp_solve(pi->curr_lp);
399 DBG((dbg, LEVEL_1, "Solution time: %f\n", lpp_get_sol_time(pi->curr_lp)));
403 * Set the color of all simplicial nodes removed form
404 * the graph before transforming it to an ilp.
406 static void pi_set_simplicials(problem_instance_t *pi) {
407 simpl_t *simpl, *tmp;
408 bitset_t *used_cols = bitset_alloca(arch_register_class_n_regs(pi->co->chordal_env->cls));
410 DBG((dbg, LEVEL_2, "Set simplicials...\n"));
411 /* color the simplicial nodes in right order */
412 list_for_each_entry_safe(simpl_t, simpl, tmp, &pi->simplicials, chain) {
414 ir_node *other_irn, *irn;
415 if_node_t *other, *ifn;
417 /* get free color by inspecting all neighbors */
419 irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, ifn->nnr);
420 bitset_clear_all(used_cols);
421 foreach_neighb(ifn, other) {
422 other_irn = get_irn_for_graph_nr(pi->co->chordal_env->irg, other->nnr);
423 if (!is_removed(other_irn)) /* only inspect nodes which are in graph right now */
424 bitset_set(used_cols, get_irn_col(pi->co, other_irn));
427 /* now all bits not set are possible colors */
428 free_col = bitset_next_clear(used_cols, 0);
429 assert(free_col != -1 && "No free color found. This can not be.");
430 set_irn_col(pi->co, irn, free_col);
431 pset_remove_ptr(pi->removed, irn); /* irn is back in graph again */
437 * Sets the colors of irns according to the values of variables
438 * provided by the solution of the solver.
440 static void pi_apply_solution(problem_instance_t *pi) {
444 DBG((dbg, LEVEL_2, "Applying solution...\n"));
450 sol = xmalloc((pi->last_x_var+1) * sizeof(*sol));
451 state = lpp_get_solution(pi->curr_lp, sol, 1, pi->last_x_var);
452 if (state != optimal) {
453 printf("Solution state is not 'optimal': %d\n", state);
454 if (state < feasible)
457 for (i=0; i<pi->last_x_var; ++i)
458 if (sol[i] == 1.0) { /* split varibale name into components */
461 lpp_get_var_name(pi->curr_lp, 1+i, var_name, sizeof(var_name));
462 if (split_var(var_name, &nnr, &col) == 2) {
463 DBG((dbg, LEVEL_2, " x%d = %d\n", nnr, col));
464 set_irn_col(pi->co, get_irn_for_graph_nr(pi->co->chordal_env->irg, nnr), col);
466 assert(0 && "This should be a x-var");
470 void co_ilp_opt(copy_opt_t *co) {
471 problem_instance_t *pi;
473 dbg = firm_dbg_register("ir.be.copyoptilp");
474 if (!strcmp(co->name, DEBUG_IRG))
475 firm_dbg_set_mask(dbg, -1);
477 firm_dbg_set_mask(dbg, DEBUG_LVL);
480 if (!pi->all_simplicial) {
483 snprintf(buf, sizeof(buf), "%s.mps", co->name);
484 lpp_dump(pi->curr_lp, buf);
486 pi_solve_ilp(pi, lpp_solve_local);
487 pi_apply_solution(pi);
489 pi_set_simplicials(pi);