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 ILP based copy minimization.
23 * @author Daniel Grund
27 * ILP formalization using G=(V, E, Q):
28 * - 2 class of variables: coloring vars x_ic and equal color vars y_ij
30 * - Clique-star constraints
33 * \min \sum_{ (i,j) \in Q } w_ij y_ij
35 * \sum_c x_nc = 1 n \in N, c \in C
37 * x_nc = 0 n \in N, c \not\in C(n)
39 * \sum x_nc <= 1 x_nc \in Clique \in AllCliques, c \in C
41 * \sum_{e \in p} y_e >= 1 p \in P path constraints
43 * \sum_{e \in cs} y_e >= |cs| - 1 cs \in CP clique-star constraints
45 * x_nc, y_ij \in N, w_ij \in R^+
52 #include "raw_bitset.h"
57 #include "becopyilp_t.h"
59 #include "besched_t.h"
63 typedef struct _local_env_t {
65 int first_x_var, last_x_var;
67 bitset_t *normal_colors;
69 DEBUG_ONLY(firm_dbg_module_t *dbg;)
72 static void build_coloring_cstr(ilp_env_t *ienv) {
73 be_ifg_t *ifg = ienv->co->cenv->ifg;
74 void *iter = be_ifg_nodes_iter_alloca(ifg);
79 colors = bitset_alloca(arch_register_class_n_regs(ienv->co->cls));
81 be_ifg_foreach_node(ifg, iter, irn)
82 if (!sr_is_removed(ienv->sr, irn)) {
85 const arch_register_req_t *req;
86 int curr_node_color = get_irn_col(irn);
87 int node_nr = (int)get_irn_idx(irn);
88 local_env_t *lenv = ienv->env;
90 pmap_insert(lenv->nr_2_irn, INT_TO_PTR(node_nr), irn);
92 req = arch_get_register_req(irn, -1);
94 bitset_clear_all(colors);
96 /* get assignable colors */
97 if (arch_register_req_is(req, limited)) {
98 rbitset_copy_to_bitset(req->limited, colors);
100 bitset_copy(colors, lenv->normal_colors);
103 /* add the coloring constraint */
104 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 1.0);
106 bitset_foreach(colors, col) {
107 int var_idx = lpp_add_var(ienv->lp, name_cdd(buf, 'x', node_nr, col), lpp_binary, 0.0);
108 lpp_set_start_value(ienv->lp, var_idx, (col == (unsigned) curr_node_color) ? 1.0 : 0.0);
109 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
111 lenv->last_x_var = var_idx;
112 if (lenv->first_x_var == -1)
113 lenv->first_x_var = var_idx;
116 /* add register constraint constraints */
117 bitset_foreach_clear(colors, col) {
118 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 0.0);
119 int var_idx = lpp_add_var(ienv->lp, name_cdd(buf, 'x', node_nr, col), lpp_binary, 0.0);
120 lpp_set_start_value(ienv->lp, var_idx, 0.0);
121 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
123 lenv->last_x_var = var_idx;
128 static void build_interference_cstr(ilp_env_t *ienv) {
129 lpp_t *lpp = ienv->lp;
130 local_env_t *lenv = ienv->env;
131 be_ifg_t *ifg = ienv->co->cenv->ifg;
132 int n_colors = lenv->n_colors;
135 void *iter = be_ifg_cliques_iter_alloca(ifg);
136 ir_node **clique = alloca(sizeof(*clique) * n_colors);
141 /* for each maximal clique */
142 be_ifg_foreach_clique(ifg, iter, clique, &size) {
145 for (i=0; i<size; ++i)
146 if (!sr_is_removed(ienv->sr, clique[i]))
153 for (col=0; col<n_colors; ++col) {
154 int cst_idx = lpp_add_cst(lpp, NULL, lpp_less, 1.0);
156 /* for each member of this clique */
157 for (i=0; i<size; ++i) {
158 ir_node *irn = clique[i];
160 if (!sr_is_removed(ienv->sr, irn)) {
161 int var_idx = lpp_get_var_idx(lpp, name_cdd(buf, 'x', (int)get_irn_idx(irn), col));
162 lpp_set_factor_fast(lpp, cst_idx, var_idx, 1);
171 * TODO: Remove the dependency of the opt-units data structure
172 * by walking over all affinity edges. Graph structure
173 * does not provide this walker, yet.
175 static void build_affinity_cstr(ilp_env_t *ienv) {
176 local_env_t *lenv = ienv->env;
177 int n_colors = lenv->n_colors;
180 /* for all optimization units */
181 list_for_each_entry(unit_t, curr, &ienv->co->units, units) {
183 int root_nr, arg_nr, i, col, y_idx, root_idx, arg_idx;
185 int root_col, arg_col;
187 root = curr->nodes[0];
188 root_nr = (int) get_irn_idx(root);
189 root_col = get_irn_col(root);
191 for (i = 1; i < curr->node_count; ++i) {
192 arg = curr->nodes[i];
193 arg_nr = (int) get_irn_idx(arg);
194 arg_col = get_irn_col(arg);
196 /* add a new affinity variable */
197 y_idx = lpp_add_var(ienv->lp, name_cdd_sorted(buf, 'y', root_nr, arg_nr), lpp_binary, curr->costs[i]);
198 lpp_set_start_value(ienv->lp, y_idx, (root_col==arg_col) ? 0.0 : 1.0);
200 /* add constraints relating the affinity var to the color vars */
201 for (col=0; col<n_colors; ++col) {
202 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_less, 0.0);
203 root_idx = lpp_get_var_idx(ienv->lp, name_cdd(buf, 'x', root_nr, col));
204 arg_idx = lpp_get_var_idx(ienv->lp, name_cdd(buf, 'x', arg_nr, col));
206 lpp_set_factor_fast(ienv->lp, cst_idx, root_idx, 1.0);
207 lpp_set_factor_fast(ienv->lp, cst_idx, arg_idx, -1.0);
208 lpp_set_factor_fast(ienv->lp, cst_idx, y_idx, -1.0);
215 * Helping stuff for build_clique_star_cstr
217 typedef struct _edge_t {
221 static int compare_edge_t(const void *k1, const void *k2, size_t size) {
222 const edge_t *e1 = k1;
223 const edge_t *e2 = k2;
226 return ! (e1->n1 == e2->n1 && e1->n2 == e2->n2);
229 #define HASH_EDGE(e) (hash_irn((e)->n1) ^ hash_irn((e)->n2))
231 static inline edge_t *add_edge(set *edges, ir_node *n1, ir_node *n2, int *counter) {
234 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
242 return set_insert(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
245 static inline edge_t *find_edge(set *edges, ir_node *n1, ir_node *n2) {
248 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
255 return set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
258 static inline void remove_edge(set *edges, ir_node *n1, ir_node *n2, int *counter) {
261 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
268 e = set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
276 #define pset_foreach(pset, irn) for(irn=pset_first(pset); irn; irn=pset_next(pset))
279 * Search for an interference clique and an external node
280 * with affinity edges to all nodes of the clique.
281 * At most 1 node of the clique can be colored equally with the external node.
283 static void build_clique_star_cstr(ilp_env_t *ienv) {
284 affinity_node_t *aff;
286 /* for each node with affinity edges */
287 co_gs_foreach_aff_node(ienv->co, aff) {
290 const ir_node *center = aff->irn;
293 int i, o, n_nodes, n_edges;
295 if (arch_irn_is(aff->irn, ignore))
299 edges = new_set(compare_edge_t, 8);
301 /* get all affinity neighbours */
303 co_gs_foreach_neighb(aff, nbr) {
304 if (!arch_irn_is(nbr->irn, ignore)) {
305 obstack_ptr_grow(&ob, nbr->irn);
309 nodes = obstack_finish(&ob);
311 /* get all interference edges between these */
313 for (i=0; i<n_nodes; ++i)
315 if (be_ifg_connected(ienv->co->cenv->ifg, nodes[i], nodes[o]))
316 add_edge(edges, nodes[i], nodes[o], &n_edges);
318 /* cover all these interference edges with maximal cliques */
321 pset *clique = pset_new_ptr(8);
324 /* get 2 starting nodes to form a clique */
325 for (e=set_first(edges); !e->n1; e=set_next(edges))
328 /* we could be stepped out of the loop before the set iterated to the end */
331 pset_insert_ptr(clique, e->n1);
332 pset_insert_ptr(clique, e->n2);
333 remove_edge(edges, e->n1, e->n2, &n_edges);
335 /* while the clique is growing */
339 /* search for a candidate to extend the clique */
340 for (i=0; i<n_nodes; ++i) {
341 ir_node *member, *cand = nodes[i];
344 /* if its already in the clique try the next */
345 if (pset_find_ptr(clique, cand))
348 /* are there all necessary interferences? */
350 pset_foreach(clique, member) {
351 if (!find_edge(edges, cand, member)) {
358 /* now we know if we have a clique extender */
360 /* first remove all covered edges */
361 pset_foreach(clique, member)
362 remove_edge(edges, cand, member, &n_edges);
364 /* insert into clique */
365 pset_insert_ptr(clique, cand);
372 /* now the clique is maximal. Finally add the constraint */
375 int var_idx, cst_idx, center_nr, member_nr;
378 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater, pset_count(clique)-1);
379 center_nr = get_irn_idx(center);
381 pset_foreach(clique, member) {
382 member_nr = get_irn_idx(member);
383 var_idx = lpp_get_var_idx(ienv->lp, name_cdd_sorted(buf, 'y', center_nr, member_nr));
384 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
392 obstack_free(&ob, NULL);
397 static void extend_path(ilp_env_t *ienv, pdeq *path, const ir_node *irn) {
398 be_ifg_t *ifg = ienv->co->cenv->ifg;
401 affinity_node_t *aff;
404 /* do not walk backwards or in circles */
405 if (pdeq_contains(path, irn))
408 if (arch_irn_is(irn, ignore))
411 /* insert the new irn */
412 pdeq_putr(path, irn);
416 /* check for forbidden interferences */
417 len = pdeq_len(path);
418 curr_path = alloca(len * sizeof(*curr_path));
419 pdeq_copyl(path, (const void **)curr_path);
421 for (i=1; i<len; ++i)
422 if (be_ifg_connected(ifg, irn, curr_path[i]))
427 /* check for terminating interference */
428 if (be_ifg_connected(ifg, irn, curr_path[0])) {
430 /* One node is not a path. */
431 /* And a path of length 2 is covered by a clique star constraint. */
433 /* finally build the constraint */
434 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater, 1.0);
435 for (i=1; i<len; ++i) {
437 int nr_1 = get_irn_idx(curr_path[i-1]);
438 int nr_2 = get_irn_idx(curr_path[i]);
439 int var_idx = lpp_get_var_idx(ienv->lp, name_cdd_sorted(buf, 'y', nr_1, nr_2));
440 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
444 /* this path cannot be extended anymore */
450 /* recursively extend the path */
451 aff = get_affinity_info(ienv->co, irn);
452 co_gs_foreach_neighb(aff, nbr)
453 extend_path(ienv, path, nbr->irn);
463 * Search a path of affinity edges, whose ends are connected
464 * by an interference edge and there are no other interference
466 * Then at least one of these affinity edges must break.
468 static void build_path_cstr(ilp_env_t *ienv) {
469 affinity_node_t *aff_info;
471 /* for each node with affinity edges */
472 co_gs_foreach_aff_node(ienv->co, aff_info) {
473 pdeq *path = new_pdeq();
475 extend_path(ienv, path, aff_info->irn);
481 static void ilp2_build(ilp_env_t *ienv) {
482 local_env_t *lenv = ienv->env;
485 ienv->lp = new_lpp(ienv->co->name, lpp_minimize);
486 build_coloring_cstr(ienv);
487 build_interference_cstr(ienv);
488 build_affinity_cstr(ienv);
489 build_clique_star_cstr(ienv);
490 build_path_cstr(ienv);
492 lower_bound = co_get_lower_bound(ienv->co) - co_get_inevit_copy_costs(ienv->co);
493 lpp_set_bound(ienv->lp, lower_bound);
494 lpp_set_time_limit(ienv->lp, lenv->time_limit);
497 static void ilp2_apply(ilp_env_t *ienv) {
498 local_env_t *lenv = ienv->env;
501 /* first check if there was sth. to optimize */
502 if (lenv->first_x_var >= 0) {
503 int count = lenv->last_x_var - lenv->first_x_var + 1;
504 double *sol = XMALLOCN(double, count);
505 lpp_sol_state_t state = lpp_get_solution(ienv->lp, sol, lenv->first_x_var, lenv->last_x_var);
507 if (state != lpp_optimal) {
508 printf("WARNING %s: Solution state is not 'optimal': %d\n", ienv->co->name, state);
509 assert(state >= lpp_feasible && "The solution should at least be feasible!");
512 for (i=0; i<count; ++i) {
516 if (sol[i] > 1-EPSILON) { /* split variable name into components */
517 lpp_get_var_name(ienv->lp, lenv->first_x_var+i, var_name, sizeof(var_name));
519 if (sscanf(var_name, "x_%d_%d", &nodenr, &color) == 2) {
520 ir_node *irn = pmap_get(lenv->nr_2_irn, INT_TO_PTR(nodenr));
521 assert(irn && "This node number must be present in the map");
523 set_irn_col(ienv->co, irn, color);
525 assert(0 && "This should be a x-var");
533 /* TODO adapt to multiple possible ILPs */
534 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //now we have ms
535 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
536 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
537 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
541 int co_solve_ilp2(copy_opt_t *co) {
542 lpp_sol_state_t sol_state;
546 ASSERT_OU_AVAIL(co); //See build_clique_st
552 my.nr_2_irn = pmap_create();
553 FIRM_DBG_REGISTER(my.dbg, "firm.be.coilp2");
555 my.normal_colors = bitset_alloca(arch_register_class_n_regs(co->cls));
556 bitset_clear_all(my.normal_colors);
557 arch_put_non_ignore_regs(co->cls, my.normal_colors);
558 my.n_colors = bitset_popcnt(my.normal_colors);
560 ienv = new_ilp_env(co, ilp2_build, ilp2_apply, &my);
562 sol_state = ilp_go(ienv);
564 pmap_destroy(my.nr_2_irn);
567 return sol_state == lpp_optimal;
572 static inline void only_that_you_can_compile_without_WITH_ILP_defined(void) {
575 #endif /* WITH_ILP */