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^+
49 #include "raw_bitset.h"
54 #include "becopyilp_t.h"
59 DEBUG_ONLY(static firm_dbg_module_t *dbg;)
61 typedef struct local_env_t {
64 const unsigned *allocatable_colors;
67 static unsigned check_alignment_constraints(ir_node *node)
69 const arch_register_req_t *req = arch_get_irn_register_req(node);
70 // For larger than 1 variables, support only aligned constraints
71 assert(((!(req->type & arch_register_req_type_aligned)
73 || (req->type & arch_register_req_type_aligned))
74 && "Unaligned large (width > 1) variables not supported");
75 return (req->type & arch_register_req_type_aligned) && req->width > 1;
78 static void make_color_var_name(char *buf, size_t buf_size,
79 const ir_node *node, unsigned color)
81 unsigned node_idx = get_irn_idx(node);
82 snprintf(buf, buf_size, "x_%u_%u", node_idx, color);
85 static void build_coloring_cstr(ilp_env_t *ienv)
87 local_env_t *lenv = (local_env_t*)ienv->env;
88 be_ifg_t *ifg = ienv->co->cenv->ifg;
89 unsigned n_regs = arch_register_class_n_regs(ienv->co->cls);
90 const unsigned *allocatable_colors = lenv->allocatable_colors;
96 rbitset_alloca(colors, n_regs);
98 be_ifg_foreach_node(ifg, &iter, irn) {
99 const arch_register_req_t *req;
102 unsigned curr_node_color;
103 unsigned has_alignment_cstr;
105 if (sr_is_removed(ienv->sr, irn))
108 has_alignment_cstr = check_alignment_constraints(irn);
110 req = arch_get_irn_register_req(irn);
112 /* get assignable colors */
113 if (arch_register_req_is(req, limited)) {
114 rbitset_copy(colors, req->limited, n_regs);
116 rbitset_copy(colors, allocatable_colors, n_regs);
119 /* add the coloring constraint */
120 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 1.0);
122 curr_node_color = get_irn_col(irn);
123 for (col = 0; col < n_regs; ++col) {
126 if (!rbitset_is_set(colors, col)
127 || (has_alignment_cstr && ((col % req->width) != 0)))
130 make_color_var_name(buf, sizeof(buf), irn, col);
131 var_idx = lpp_add_var(ienv->lp, buf, lpp_binary, 0.0);
132 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
134 val = (col == curr_node_color) ? 1.0 : 0.0;
135 lpp_set_start_value(ienv->lp, var_idx, val);
137 lenv->last_x_var = var_idx;
138 if (lenv->first_x_var == -1)
139 lenv->first_x_var = var_idx;
142 /* add register constraint constraints */
143 for (col = 0; col < n_regs; ++col) {
146 if (rbitset_is_set(colors, col)
147 // for aligned variable, we set the unaligned part to 0
148 && (!has_alignment_cstr || ((col % req->width) == 0)))
151 make_color_var_name(buf, sizeof(buf), irn, col);
152 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_equal, 0.0);
153 var_idx = lpp_add_var(ienv->lp, buf, lpp_binary, 0.0);
154 lpp_set_start_value(ienv->lp, var_idx, 0.0);
155 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1);
157 lenv->last_x_var = var_idx;
162 static void build_interference_cstr(ilp_env_t *ienv)
164 lpp_t *lpp = ienv->lp;
165 local_env_t *lenv = (local_env_t*)ienv->env;
166 be_ifg_t *ifg = ienv->co->cenv->ifg;
167 unsigned n_colors = arch_register_class_n_regs(ienv->co->cls);
168 ir_node **clique = ALLOCAN(ir_node*, n_colors);
169 const unsigned *allocatable_colors = lenv->allocatable_colors;
175 /* for each maximal clique */
176 be_ifg_foreach_clique(ifg, &iter, clique, &size) {
177 unsigned realsize = 0;
179 for (i=0; i<size; ++i) {
180 if (!sr_is_removed(ienv->sr, clique[i]))
188 for (col=0; col<n_colors; ++col) {
190 if (!rbitset_is_set(allocatable_colors, col))
193 cst_idx = lpp_add_cst(lpp, NULL, lpp_less_equal, 1.0);
195 /* for each member of this clique */
196 for (i=0; i<size; ++i) {
197 ir_node *irn = clique[i];
200 unsigned aligment_offset = 0;
202 if (sr_is_removed(ienv->sr, irn))
205 // Use the first part of the large registers for all
206 // interference, since it is the only colorable one.
207 if (check_alignment_constraints(irn)) {
208 const arch_register_req_t *req
209 = arch_get_irn_register_req(irn);
210 aligment_offset = col % req->width;
213 make_color_var_name(buf, sizeof(buf), irn,
214 col - aligment_offset);
215 var_idx = lpp_get_var_idx(lpp, buf);
216 lpp_set_factor_fast(lpp, cst_idx, var_idx, 1);
222 static void make_affinity_var_name(char *buf, size_t buf_size,
223 const ir_node *node1, const ir_node *node2)
225 unsigned n1 = get_irn_idx(node1);
226 unsigned n2 = get_irn_idx(node2);
228 snprintf(buf, buf_size, "y_%u_%u", n1, n2);
230 snprintf(buf, buf_size, "y_%u_%u", n2, n1);
236 * TODO: Remove the dependency of the opt-units data structure
237 * by walking over all affinity edges. Graph structure
238 * does not provide this walker, yet.
240 static void build_affinity_cstr(ilp_env_t *ienv)
242 unsigned n_colors = arch_register_class_n_regs(ienv->co->cls);
245 /* for all optimization units */
246 list_for_each_entry(unit_t, curr, &ienv->co->units, units) {
247 ir_node *root = curr->nodes[0];
248 unsigned root_col = get_irn_col(root);
251 for (i = 1; i < curr->node_count; ++i) {
252 ir_node *arg = curr->nodes[i];
253 unsigned arg_col = get_irn_col(arg);
259 /* add a new affinity variable */
260 make_affinity_var_name(buf, sizeof(buf), arg, root);
261 y_idx = lpp_add_var(ienv->lp, buf, lpp_binary, curr->costs[i]);
262 val = (root_col == arg_col) ? 0.0 : 1.0;
263 lpp_set_start_value(ienv->lp, y_idx, val);
265 /* add constraints relating the affinity var to the color vars */
266 for (col=0; col<n_colors; ++col) {
267 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_less_equal, 0.0);
271 make_color_var_name(buf, sizeof(buf), root, col);
272 root_idx = lpp_get_var_idx(ienv->lp, buf);
273 make_color_var_name(buf, sizeof(buf), arg, col);
274 arg_idx = lpp_get_var_idx(ienv->lp, buf);
276 lpp_set_factor_fast(ienv->lp, cst_idx, root_idx, 1.0);
277 lpp_set_factor_fast(ienv->lp, cst_idx, arg_idx, -1.0);
278 lpp_set_factor_fast(ienv->lp, cst_idx, y_idx, -1.0);
285 * Helping stuff for build_clique_star_cstr
287 typedef struct edge_t {
292 static int compare_edge_t(const void *k1, const void *k2, size_t size)
294 const edge_t *e1 = (const edge_t*)k1;
295 const edge_t *e2 = (const edge_t*)k2;
298 return ! (e1->n1 == e2->n1 && e1->n2 == e2->n2);
301 #define HASH_EDGE(e) (hash_irn((e)->n1) ^ hash_irn((e)->n2))
303 static inline edge_t *add_edge(set *edges, ir_node *n1, ir_node *n2, size_t *counter)
307 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
315 return (edge_t*)set_insert(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
318 static inline edge_t *find_edge(set *edges, ir_node *n1, ir_node *n2)
322 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
329 return (edge_t*)set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
332 static inline void remove_edge(set *edges, ir_node *n1, ir_node *n2, size_t *counter)
336 if (PTR_TO_INT(n1) < PTR_TO_INT(n2)) {
343 e = (edge_t*)set_find(edges, &new_edge, sizeof(new_edge), HASH_EDGE(&new_edge));
351 #define pset_foreach(pset, irn) for (irn=(ir_node*)pset_first(pset); irn; irn=(ir_node*)pset_next(pset))
354 * Search for an interference clique and an external node
355 * with affinity edges to all nodes of the clique.
356 * At most 1 node of the clique can be colored equally with the external node.
358 static void build_clique_star_cstr(ilp_env_t *ienv)
360 affinity_node_t *aff;
362 /* for each node with affinity edges */
363 co_gs_foreach_aff_node(ienv->co, aff) {
366 const ir_node *center = aff->irn;
372 if (arch_irn_is_ignore(aff->irn))
376 edges = new_set(compare_edge_t, 8);
378 /* get all affinity neighbours */
380 co_gs_foreach_neighb(aff, nbr) {
381 if (!arch_irn_is_ignore(nbr->irn)) {
382 obstack_ptr_grow(&ob, nbr->irn);
386 nodes = (ir_node**)obstack_finish(&ob);
388 /* get all interference edges between these */
390 for (i=0; i<n_nodes; ++i) {
391 for (o=0; o<i; ++o) {
392 if (be_ifg_connected(ienv->co->cenv->ifg, nodes[i], nodes[o]))
393 add_edge(edges, nodes[i], nodes[o], &n_edges);
397 /* cover all these interference edges with maximal cliques */
400 pset *clique = pset_new_ptr(8);
403 /* get 2 starting nodes to form a clique */
404 for (e=(edge_t*)set_first(edges); !e->n1; e=(edge_t*)set_next(edges)) {
407 /* we could be stepped out of the loop before the set iterated to the end */
410 pset_insert_ptr(clique, e->n1);
411 pset_insert_ptr(clique, e->n2);
412 remove_edge(edges, e->n1, e->n2, &n_edges);
414 /* while the clique is growing */
418 /* search for a candidate to extend the clique */
419 for (i=0; i<n_nodes; ++i) {
420 ir_node *cand = nodes[i];
424 /* if its already in the clique try the next */
425 if (pset_find_ptr(clique, cand))
428 /* are there all necessary interferences? */
430 pset_foreach(clique, member) {
431 if (!find_edge(edges, cand, member)) {
438 /* now we know if we have a clique extender */
440 /* first remove all covered edges */
441 pset_foreach(clique, member)
442 remove_edge(edges, cand, member, &n_edges);
444 /* insert into clique */
445 pset_insert_ptr(clique, cand);
452 /* now the clique is maximal. Finally add the constraint */
459 cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater_equal, pset_count(clique)-1);
461 pset_foreach(clique, member) {
462 make_affinity_var_name(buf, sizeof(buf), center, member);
463 var_idx = lpp_get_var_idx(ienv->lp, buf);
464 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
472 obstack_free(&ob, NULL);
476 static void extend_path(ilp_env_t *ienv, pdeq *path, const ir_node *irn)
478 be_ifg_t *ifg = ienv->co->cenv->ifg;
481 affinity_node_t *aff;
484 /* do not walk backwards or in circles */
485 if (pdeq_contains(path, irn))
488 if (arch_irn_is_ignore(irn))
491 /* insert the new irn */
492 pdeq_putr(path, irn);
494 /* check for forbidden interferences */
495 len = pdeq_len(path);
496 curr_path = ALLOCAN(ir_node*, len);
497 pdeq_copyl(path, (const void **)curr_path);
499 for (i=1; i<len; ++i) {
500 if (be_ifg_connected(ifg, irn, curr_path[i]))
504 /* check for terminating interference */
505 if (be_ifg_connected(ifg, irn, curr_path[0])) {
506 /* One node is not a path. */
507 /* And a path of length 2 is covered by a clique star constraint. */
509 /* finally build the constraint */
510 int cst_idx = lpp_add_cst(ienv->lp, NULL, lpp_greater_equal, 1.0);
511 for (i=1; i<len; ++i) {
515 make_affinity_var_name(buf, sizeof(buf), curr_path[i-1], curr_path[i]);
516 var_idx = lpp_get_var_idx(ienv->lp, buf);
517 lpp_set_factor_fast(ienv->lp, cst_idx, var_idx, 1.0);
521 /* this path cannot be extended anymore */
525 /* recursively extend the path */
526 aff = get_affinity_info(ienv->co, irn);
527 co_gs_foreach_neighb(aff, nbr) {
528 extend_path(ienv, path, nbr->irn);
537 * Search a path of affinity edges, whose ends are connected
538 * by an interference edge and there are no other interference
540 * Then at least one of these affinity edges must break.
542 static void build_path_cstr(ilp_env_t *ienv)
544 affinity_node_t *aff_info;
546 /* for each node with affinity edges */
547 co_gs_foreach_aff_node(ienv->co, aff_info) {
548 pdeq *path = new_pdeq();
550 extend_path(ienv, path, aff_info->irn);
556 static void ilp2_build(ilp_env_t *ienv)
560 ienv->lp = lpp_new(ienv->co->name, lpp_minimize);
561 build_coloring_cstr(ienv);
562 build_interference_cstr(ienv);
563 build_affinity_cstr(ienv);
564 build_clique_star_cstr(ienv);
565 build_path_cstr(ienv);
567 lower_bound = co_get_lower_bound(ienv->co) - co_get_inevit_copy_costs(ienv->co);
568 lpp_set_bound(ienv->lp, lower_bound);
571 static void ilp2_apply(ilp_env_t *ienv)
573 local_env_t *lenv = (local_env_t*)ienv->env;
574 ir_graph *irg = ienv->co->irg;
576 /* first check if there was sth. to optimize */
577 if (lenv->first_x_var >= 0) {
578 int count = lenv->last_x_var - lenv->first_x_var + 1;
579 double *sol = XMALLOCN(double, count);
580 lpp_sol_state_t state = lpp_get_solution(ienv->lp, sol, lenv->first_x_var, lenv->last_x_var);
583 if (state != lpp_optimal) {
584 printf("WARNING %s: Solution state is not 'optimal': %d\n",
585 ienv->co->name, (int)state);
586 if (state < lpp_feasible) {
587 panic("Copy coalescing solution not feasible!");
591 for (i=0; i<count; ++i) {
595 if (sol[i] <= 1-EPSILON)
597 /* split variable name into components */
598 lpp_get_var_name(ienv->lp, lenv->first_x_var+i, var_name, sizeof(var_name));
600 if (sscanf(var_name, "x_%u_%u", &nodenr, &color) == 2) {
601 ir_node *irn = get_idx_irn(irg, nodenr);
602 set_irn_col(ienv->co->cls, irn, color);
604 panic("This should be a x-var");
612 /* TODO adapt to multiple possible ILPs */
613 copystat_add_ilp_time((int)(1000.0*lpp_get_sol_time(pi->curr_lp))); //now we have ms
614 copystat_add_ilp_vars(lpp_get_var_count(pi->curr_lp));
615 copystat_add_ilp_csts(lpp_get_cst_count(pi->curr_lp));
616 copystat_add_ilp_iter(lpp_get_iter_cnt(pi->curr_lp));
621 * Solves the problem using mixed integer programming
622 * @returns 1 iff solution state was optimal
623 * Uses the OU and the GRAPH data structure
624 * Dependency of the OU structure can be removed
626 static int co_solve_ilp2(copy_opt_t *co)
628 unsigned *allocatable_colors;
629 unsigned n_regs = arch_register_class_n_regs(co->cls);
630 lpp_sol_state_t sol_state;
634 ASSERT_OU_AVAIL(co); //See build_clique_st
639 FIRM_DBG_REGISTER(dbg, "firm.be.coilp2");
641 rbitset_alloca(allocatable_colors, n_regs);
642 be_set_allocatable_regs(co->irg, co->cls, allocatable_colors);
643 my.allocatable_colors = allocatable_colors;
645 ienv = new_ilp_env(co, ilp2_build, ilp2_apply, &my);
647 sol_state = ilp_go(ienv);
651 return sol_state == lpp_optimal;
654 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyilp2)
655 void be_init_copyilp2(void)
657 static co_algo_info copyheur = {
661 be_register_copyopt("ilp", ©heur);