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
26 * ILP formalization using G=(V, E, Q):
27 * - 2 class of variables: coloring vars x_ic and equal color vars y_ij
29 * - Clique-star constraints
32 * \min \sum_{ (i,j) \in Q } w_ij y_ij
34 * \sum_c x_nc = 1 n \in N, c \in C
36 * x_nc = 0 n \in N, c \not\in C(n)
38 * \sum x_nc <= 1 x_nc \in Clique \in AllCliques, c \in C
40 * \sum_{e \in p} y_e >= 1 p \in P path constraints
42 * \sum_{e \in cs} y_e >= |cs| - 1 cs \in CP clique-star constraints
44 * x_nc, y_ij \in N, w_ij \in R^+
51 #include "raw_bitset.h"
56 #include "becopyilp_t.h"
61 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
63 typedef struct local_env_t {
66 const unsigned *allocatable_colors;
69 static unsigned check_alignment_constraints(ir_node *node)
71 const arch_register_req_t *req = arch_get_irn_register_req(node);
72 // For larger than 1 variables, support only aligned constraints
73 assert((arch_register_req_is(req, aligned) || req->width == 1) && "Unaligned large (width > 1) variables not supported");
74 return arch_register_req_is(req, aligned) && req->width > 1;
77 static void make_color_var_name(char *buf, size_t buf_size,
78 const ir_node *node, unsigned color)
80 unsigned node_idx = get_irn_idx(node);
81 snprintf(buf, buf_size, "x_%u_%u", node_idx, color);
84 static void build_coloring_cstr(ilp_env_t *ienv)
86 local_env_t *lenv = (local_env_t*)ienv->env;
87 be_ifg_t *ifg = ienv->co->cenv->ifg;
88 unsigned n_regs = arch_register_class_n_regs(ienv->co->cls);
89 const unsigned *allocatable_colors = lenv->allocatable_colors;
94 unsigned *const colors = rbitset_alloca(n_regs);
95 be_ifg_foreach_node(ifg, &iter, irn) {
96 const arch_register_req_t *req;
99 unsigned curr_node_color;
100 unsigned has_alignment_cstr;
102 if (sr_is_removed(ienv->sr, irn))
105 has_alignment_cstr = check_alignment_constraints(irn);
107 req = arch_get_irn_register_req(irn);
109 /* get assignable colors */
110 if (arch_register_req_is(req, limited)) {
111 rbitset_copy(colors, req->limited, n_regs);
113 rbitset_copy(colors, allocatable_colors, n_regs);
116 /* add the coloring constraint */
117 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 1.0);
119 curr_node_color = get_irn_col(irn);
120 for (col = 0; col < n_regs; ++col) {
123 if (!rbitset_is_set(colors, col)
124 || (has_alignment_cstr && ((col % req->width) != 0)))
127 make_color_var_name(buf, sizeof(buf), irn, col);
128 var_idx = lpp_add_var(ienv->lp, buf, lpp_binary, 0.0);
129 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
131 val = (col == curr_node_color) ? 1.0 : 0.0;
132 lpp_set_start_value(ienv->lp, var_idx, val);
134 lenv->last_x_var = var_idx;
135 if (lenv->first_x_var == -1)
136 lenv->first_x_var = var_idx;
139 /* add register constraint constraints */
140 for (col = 0; col < n_regs; ++col) {
143 if (rbitset_is_set(colors, col)
144 // for aligned variable, we set the unaligned part to 0
145 && (!has_alignment_cstr || ((col % req->width) == 0)))
148 make_color_var_name(buf, sizeof(buf), irn, col);
149 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 0.0);
150 var_idx = lpp_add_var(ienv->lp, buf, lpp_binary, 0.0);
151 lpp_set_start_value(ienv->lp, var_idx, 0.0);
152 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
154 lenv->last_x_var = var_idx;
159 static void build_interference_cstr(ilp_env_t *ienv)
161 lpp_t *lpp = ienv->lp;
162 local_env_t *lenv = (local_env_t*)ienv->env;
163 be_ifg_t *ifg = ienv->co->cenv->ifg;
164 unsigned n_colors = arch_register_class_n_regs(ienv->co->cls);
165 ir_node **clique = ALLOCAN(ir_node*, n_colors);
166 const unsigned *allocatable_colors = lenv->allocatable_colors;
172 /* for each maximal clique */
173 be_ifg_foreach_clique(ifg, &iter, clique, &size) {
174 unsigned realsize = 0;
176 for (i=0; i<size; ++i) {
177 if (!sr_is_removed(ienv->sr, clique[i]))
185 for (col=0; col<n_colors; ++col) {
187 if (!rbitset_is_set(allocatable_colors, col))
190 cst_idx = lpp_add_cst(lpp, NULL, lpp_less_equal, 1.0);
192 /* for each member of this clique */
193 for (i=0; i<size; ++i) {
194 ir_node *irn = clique[i];
197 unsigned aligment_offset = 0;
199 if (sr_is_removed(ienv->sr, irn))
202 // Use the first part of the large registers for all
203 // interference, since it is the only colorable one.
204 if (check_alignment_constraints(irn)) {
205 const arch_register_req_t *req
206 = arch_get_irn_register_req(irn);
207 aligment_offset = col % req->width;
210 make_color_var_name(buf, sizeof(buf), irn,
211 col - aligment_offset);
212 var_idx = lpp_get_var_idx(lpp, buf);
213 lpp_set_factor_fast(lpp, cst_idx, var_idx, 1);
219 static void make_affinity_var_name(char *buf, size_t buf_size,
220 const ir_node *node1, const ir_node *node2)
222 unsigned n1 = get_irn_idx(node1);
223 unsigned n2 = get_irn_idx(node2);
225 snprintf(buf, buf_size, "y_%u_%u", n1, n2);
227 snprintf(buf, buf_size, "y_%u_%u", n2, n1);
233 * TODO: Remove the dependency of the opt-units data structure
234 * by walking over all affinity edges. Graph structure
235 * does not provide this walker, yet.
237 static void build_affinity_cstr(ilp_env_t *ienv)
239 unsigned n_colors = arch_register_class_n_regs(ienv->co->cls);
241 /* for all optimization units */
242 list_for_each_entry(unit_t, curr, &ienv->co->units, units) {
243 ir_node *root = curr->nodes[0];
244 unsigned root_col = get_irn_col(root);
247 for (i = 1; i < curr->node_count; ++i) {
248 ir_node *arg = curr->nodes[i];
249 unsigned arg_col = get_irn_col(arg);
255 /* add a new affinity variable */
256 make_affinity_var_name(buf, sizeof(buf), arg, root);
257 y_idx = lpp_add_var(ienv->lp, buf, lpp_binary, curr->costs[i]);
258 val = (root_col == arg_col) ? 0.0 : 1.0;
259 lpp_set_start_value(ienv->lp, y_idx, val);
261 /* add constraints relating the affinity var to the color vars */
262 for (col=0; col<n_colors; ++col) {
263 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_less_equal, 0.0);
267 make_color_var_name(buf, sizeof(buf), root, col);
268 root_idx = lpp_get_var_idx(ienv->lp, buf);
269 make_color_var_name(buf, sizeof(buf), arg, col);
270 arg_idx = lpp_get_var_idx(ienv->lp, buf);
272 lpp_set_factor_fast(ienv->lp, cst_idx, root_idx, 1.0);
273 lpp_set_factor_fast(ienv->lp, cst_idx, arg_idx, -1.0);
274 lpp_set_factor_fast(ienv->lp, cst_idx, y_idx, -1.0);
281 * Helping stuff for build_clique_star_cstr
283 typedef struct edge_t {
288 static int compare_edge_t(const void *k1, const void *k2, size_t size)
290 const edge_t *e1 = (const edge_t*)k1;
291 const edge_t *e2 = (const edge_t*)k2;
294 return ! (e1->n1 == e2->n1 && e1->n2 == e2->n2);
297 #define HASH_EDGE(e) (hash_irn((e)->n1) ^ hash_irn((e)->n2))
299 static inline edge_t *add_edge(set *edges, ir_node *n1, ir_node *n2, size_t *counter)
303 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
311 return set_insert(edge_t, edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
314 static inline edge_t *find_edge(set *edges, ir_node *n1, ir_node *n2)
318 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
325 return set_find(edge_t, edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
328 static inline void remove_edge(set *edges, ir_node *n1, ir_node *n2, size_t *counter)
332 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
339 e = set_find(edge_t, edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
347 #define pset_foreach(pset, irn) foreach_pset((pset), ir_node, (irn))
350 * Search for an interference clique and an external node
351 * with affinity edges to all nodes of the clique.
352 * At most 1 node of the clique can be colored equally with the external node.
354 static void build_clique_star_cstr(ilp_env_t *ienv)
356 /* for each node with affinity edges */
357 co_gs_foreach_aff_node(ienv->co, aff) {
359 const ir_node *center = aff->irn;
365 if (arch_irn_is_ignore(aff->irn))
369 edges = new_set(compare_edge_t, 8);
371 /* get all affinity neighbours */
373 co_gs_foreach_neighb(aff, nbr) {
374 if (!arch_irn_is_ignore(nbr->irn)) {
375 obstack_ptr_grow(&ob, nbr->irn);
379 nodes = (ir_node**)obstack_finish(&ob);
381 /* get all interference edges between these */
383 for (i=0; i<n_nodes; ++i) {
384 for (o=0; o<i; ++o) {
385 if (be_ifg_connected(ienv->co->cenv->ifg, nodes[i], nodes[o]))
386 add_edge(edges, nodes[i], nodes[o], &n_edges);
390 /* cover all these interference edges with maximal cliques */
393 pset *clique = pset_new_ptr(8);
396 /* get 2 starting nodes to form a clique */
397 for (e = set_first(edge_t, edges); !e->n1; e = set_next(edge_t, edges)) {}
399 /* we could be stepped out of the loop before the set iterated to the end */
402 pset_insert_ptr(clique, e->n1);
403 pset_insert_ptr(clique, e->n2);
404 remove_edge(edges, e->n1, e->n2, &n_edges);
406 /* while the clique is growing */
410 /* search for a candidate to extend the clique */
411 for (i=0; i<n_nodes; ++i) {
412 ir_node *cand = nodes[i];
415 /* if its already in the clique try the next */
416 if (pset_find_ptr(clique, cand))
419 /* are there all necessary interferences? */
421 pset_foreach(clique, member) {
422 if (!find_edge(edges, cand, member)) {
429 /* now we know if we have a clique extender */
431 /* first remove all covered edges */
432 pset_foreach(clique, member)
433 remove_edge(edges, cand, member, &n_edges);
435 /* insert into clique */
436 pset_insert_ptr(clique, cand);
443 /* now the clique is maximal. Finally add the constraint */
449 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater_equal, pset_count(clique)-1);
451 pset_foreach(clique, member) {
452 make_affinity_var_name(buf, sizeof(buf), center, member);
453 var_idx = lpp_get_var_idx(ienv->lp, buf);
454 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
462 obstack_free(&ob, NULL);
466 static void extend_path(ilp_env_t *ienv, pdeq *path, const ir_node *irn)
468 be_ifg_t *ifg = ienv->co->cenv->ifg;
471 affinity_node_t *aff;
473 /* do not walk backwards or in circles */
474 if (pdeq_contains(path, irn))
477 if (arch_irn_is_ignore(irn))
480 /* insert the new irn */
481 pdeq_putr(path, irn);
483 /* check for forbidden interferences */
484 len = pdeq_len(path);
485 curr_path = ALLOCAN(ir_node*, len);
486 pdeq_copyl(path, (const void **)curr_path);
488 for (i=1; i<len; ++i) {
489 if (be_ifg_connected(ifg, irn, curr_path[i]))
493 /* check for terminating interference */
494 if (be_ifg_connected(ifg, irn, curr_path[0])) {
495 /* One node is not a path. */
496 /* And a path of length 2 is covered by a clique star constraint. */
498 /* finally build the constraint */
499 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater_equal, 1.0);
500 for (i=1; i<len; ++i) {
504 make_affinity_var_name(buf, sizeof(buf), curr_path[i-1], curr_path[i]);
505 var_idx = lpp_get_var_idx(ienv->lp, buf);
506 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
510 /* this path cannot be extended anymore */
514 /* recursively extend the path */
515 aff = get_affinity_info(ienv->co, irn);
516 co_gs_foreach_neighb(aff, nbr) {
517 extend_path(ienv, path, nbr->irn);
526 * Search a path of affinity edges, whose ends are connected
527 * by an interference edge and there are no other interference
529 * Then at least one of these affinity edges must break.
531 static void build_path_cstr(ilp_env_t *ienv)
533 /* for each node with affinity edges */
534 co_gs_foreach_aff_node(ienv->co, aff_info) {
535 pdeq *path = new_pdeq();
537 extend_path(ienv, path, aff_info->irn);
543 static void ilp2_build(ilp_env_t *ienv)
547 ienv->lp = lpp_new(ienv->co->name, lpp_minimize);
548 build_coloring_cstr(ienv);
549 build_interference_cstr(ienv);
550 build_affinity_cstr(ienv);
551 build_clique_star_cstr(ienv);
552 build_path_cstr(ienv);
554 lower_bound = co_get_lower_bound(ienv->co) - co_get_inevit_copy_costs(ienv->co);
555 lpp_set_bound(ienv->lp, lower_bound);
558 static void ilp2_apply(ilp_env_t *ienv)
560 local_env_t *lenv = (local_env_t*)ienv->env;
561 ir_graph *irg = ienv->co->irg;
563 /* first check if there was sth. to optimize */
564 if (lenv->first_x_var >= 0) {
565 int count = lenv->last_x_var - lenv->first_x_var + 1;
566 double *sol = XMALLOCN(double, count);
567 lpp_sol_state_t state = lpp_get_solution(ienv->lp, sol, lenv->first_x_var, lenv->last_x_var);
570 if (state != lpp_optimal) {
571 printf("WARNING %s: Solution state is not 'optimal': %d\n",
572 ienv->co->name, (int)state);
573 if (state < lpp_feasible) {
574 panic("Copy coalescing solution not feasible!");
578 for (i=0; i<count; ++i) {
582 if (sol[i] <= 1-EPSILON)
584 /* split variable name into components */
585 lpp_get_var_name(ienv->lp, lenv->first_x_var+i, var_name, sizeof(var_name));
587 if (sscanf(var_name, "x_%u_%u", &nodenr, &color) == 2) {
588 ir_node *irn = get_idx_irn(irg, nodenr);
589 set_irn_col(ienv->co->cls, irn, color);
591 panic("This should be a x-var");
599 /* TODO adapt to multiple possible ILPs */
600 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //now we have ms
601 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
602 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
603 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
608 * Solves the problem using mixed integer programming
609 * @returns 1 iff solution state was optimal
610 * Uses the OU and the GRAPH data structure
611 * Dependency of the OU structure can be removed
613 static int co_solve_ilp2(copy_opt_t *co)
615 unsigned n_regs = arch_register_class_n_regs(co->cls);
616 lpp_sol_state_t sol_state;
620 ASSERT_OU_AVAIL(co); //See build_clique_st
625 FIRM_DBG_REGISTER(dbg, "firm.be.coilp2");
627 unsigned *const allocatable_colors = rbitset_alloca(n_regs);
628 be_set_allocatable_regs(co->irg, co->cls, allocatable_colors);
629 my.allocatable_colors = allocatable_colors;
631 ienv = new_ilp_env(co, ilp2_build, ilp2_apply, &my);
633 sol_state = ilp_go(ienv);
637 return sol_state == lpp_optimal;
640 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp2)
641 void be_init_copyilp2(void)
643 static co_algo_info copyheur = {
647 be_register_copyopt("ilp", ©heur);