4 * Created on: Aug 28, 2009
11 #include "becopypbqp.h"
17 #include "html_dumper.h"
18 #include "heuristical_co.h"
19 #include "pbqp_node_t.h"
21 #include "becopyopt_t.h"
25 #include "irprintf_t.h"
40 static FILE *my_open(const be_chordal_env_t *env, const char *prefix, const char *suffix)
46 const char *cup_name = be_get_irg_main_env(irg)->cup_name;
49 tu_name = XMALLOCN(char, n + 1);
50 strcpy(tu_name, cup_name);
51 for (i = 0; i < n; ++i)
52 if (tu_name[i] == '.')
55 ir_snprintf(buf, sizeof(buf), "%s%s_%F_%s%s", prefix, tu_name, env->irg, env->cls->name, suffix);
57 result = fopen(buf, "wt");
59 panic("Couldn't open '%s' for writing.", buf);
66 static void insert_into_reverse_peo(ir_node *block, void *data)
68 pqueue_t *queue = new_pqueue();
69 pqueue_t *restrictedNodesQueue = new_pqueue();
70 pbqp_co_t *pbqp_co = data;
73 sched_foreach(block, irn) {
74 if (get_irn_mode(irn) == mode_T) {
75 const ir_edge_t *edge;
77 foreach_out_edge(irn, edge) {
78 ir_node *proj = get_edge_src_irn(edge);
79 if (!arch_irn_consider_in_reg_alloc(pbqp_co->cls, proj))
82 // insert proj node into priority queue (descending by their degree in ifg)
83 if (bitset_is_set(pbqp_co->restricted_nodes, get_irn_idx(proj))) {
84 pqueue_put(restrictedNodesQueue,proj, be_ifg_degree(pbqp_co->ifg,proj));
87 pqueue_put(queue,proj, be_ifg_degree(pbqp_co->ifg,proj));
91 /* first insert all restricted nodes */
92 while (!pqueue_empty(restrictedNodesQueue)) {
93 plist_insert_back(pbqp_co->rpeo, get_node(pbqp_co->pbqp, get_irn_idx(pqueue_pop_front(restrictedNodesQueue))));
96 /* insert proj nodes into reverse perfect elimination order (descending by their degree in ifg) */
97 while (!pqueue_empty(queue)) {
98 plist_insert_back(pbqp_co->rpeo, get_node(pbqp_co->pbqp, get_irn_idx(pqueue_pop_front(queue))));
102 if (!arch_irn_consider_in_reg_alloc(pbqp_co->cls, irn))
105 /* insert pbqp node into reverse peo */
106 plist_insert_back(pbqp_co->rpeo, get_node(pbqp_co->pbqp, get_irn_idx(irn)));
110 /* free priority queues */
112 del_pqueue(restrictedNodesQueue);
115 static int co_solve_heuristic_pbqp(copy_opt_t *co)
117 nodes_iter_t nodes_it;
118 neighbours_iter_t neigh_it;
119 unsigned number_registers = co->cls->n_regs;
120 unsigned number_nodes = get_irg_last_idx(co->irg);
121 ir_timer_t *t_ra_copymin_pbqp_create = ir_timer_new();
122 ir_timer_t *t_ra_copymin_pbqp_solve = ir_timer_new();
124 ir_node *if_neighb_node;
129 printf("==>> START PBQP TIMING on IRG %s (%s) <<==\n", get_entity_name(get_irg_entity(co->irg)), arch_register_class_name(co->cls));
133 ir_timer_reset_and_start(t_ra_copymin_pbqp_create);
135 /* create and initialize data structure for pbqp copy minimization optimization */
136 pbqp_co.cls = co->cls;
137 pbqp_co.rpeo = plist_new();
138 pbqp_co.pbqp = alloc_pbqp(number_nodes);
139 pbqp_co.ignore_reg = bitset_malloc(number_registers);
140 pbqp_co.restricted_nodes = bitset_malloc(number_nodes);
141 pbqp_co.ifg = co->cenv->ifg;
143 /* no node is restricted at the beginning */
144 bitset_clear_all(pbqp_co.restricted_nodes);
146 /* get ignored registers */
147 be_put_ignore_regs(co->cenv->irg, co->cls, pbqp_co.ignore_reg);
149 /* add costs vector to nodes */
150 be_ifg_foreach_node(co->cenv->ifg, &nodes_it, ifg_node) {
151 int cntFreeChoosableRegs = 0;
153 /* create costs vector */
154 struct vector *costs_vector = vector_alloc(pbqp_co.pbqp, number_registers);
158 for (cnt = 0; cnt < costs_vector->len; cnt++) {
159 if (bitset_is_set(pbqp_co.ignore_reg,cnt)) {
160 vector_set(costs_vector, cnt, INF_COSTS);
163 if (!arch_reg_out_is_allocatable(ifg_node, arch_register_for_index(co->cls, cnt)))
165 vector_set(costs_vector, cnt, INF_COSTS);
168 vector_set(costs_vector, cnt, 0);
169 cntFreeChoosableRegs++;
173 #if KAPS_ENABLE_VECTOR_NAMES
174 /* add description */
175 vector_set_description(costs_vector, cnt, arch_register_for_index(co->cls, cnt)->name);
179 /* add costs vector to node */
180 add_node_costs(pbqp_co.pbqp, get_irn_idx(ifg_node), costs_vector);
182 if (cntFreeChoosableRegs <= 4) {
183 /* node is restricted */
184 bitset_set(pbqp_co.restricted_nodes, get_irn_idx(ifg_node));
188 /* create costs matrix for interference edges */
189 struct pbqp_matrix *ife_matrix = pbqp_matrix_alloc(pbqp_co.pbqp, number_registers, number_registers);
191 for (row = 0; row < number_registers; row++) {
192 pbqp_matrix_set(ife_matrix, row, row, INF_COSTS);
195 /* create costs matrix for affinity edges */
196 struct pbqp_matrix *afe_matrix = pbqp_matrix_alloc(pbqp_co.pbqp, number_registers, number_registers);
198 for (row = 0; row < number_registers; row++) {
199 for (col = 0; col < number_registers; col++) {
201 pbqp_matrix_set(afe_matrix, row, col, 0);
204 pbqp_matrix_set(afe_matrix, row, col, 2);
209 /* add pbqp edges and cost matrix */
210 be_ifg_foreach_node(co->cenv->ifg, &nodes_it, ifg_node) {
211 /* add costs matrix between nodes (interference edge) */
212 be_ifg_foreach_neighbour(co->cenv->ifg, &neigh_it, ifg_node, if_neighb_node) {
213 if (get_edge(pbqp_co.pbqp,get_irn_idx(ifg_node), get_irn_idx(if_neighb_node)) == NULL) {
215 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_co.pbqp, ife_matrix);
217 /* add costs matrix to interference edge */
218 add_edge_costs(pbqp_co.pbqp, get_irn_idx(ifg_node), get_irn_idx(if_neighb_node) , matrix);
223 /* add costs matrix between nodes (affinity edge) */
224 affinity_node_t *aff_node = get_affinity_info(co, ifg_node);
225 neighb_t *aff_neighb_node;
226 if (aff_node != NULL) {
227 co_gs_foreach_neighb(aff_node, aff_neighb_node) {
228 /* ignore Unknowns */
229 if (get_node(pbqp_co.pbqp, get_irn_idx(aff_neighb_node->irn)) == NULL)
232 if (get_edge(pbqp_co.pbqp, get_irn_idx(aff_node->irn), get_irn_idx(aff_neighb_node->irn)) == NULL) {
234 struct pbqp_matrix *matrix = pbqp_matrix_copy(pbqp_co.pbqp, afe_matrix);
236 /* add costs matrix to affinity edge */
237 add_edge_costs(pbqp_co.pbqp, get_irn_idx(aff_node->irn), get_irn_idx(aff_neighb_node->irn) , matrix);
243 /* create reverse perfect elimination order */
244 assure_doms(co->irg);
245 dom_tree_walk_irg(co->irg, insert_into_reverse_peo, NULL, &pbqp_co);
248 ir_timer_stop(t_ra_copymin_pbqp_create);
251 // dump graph before solving pbqp
252 FILE *file = my_open(co->cenv, "", "-pbqp_copymin.html");
253 set_dumpfile(pbqp_co.pbqp, file);
257 ir_timer_reset_and_start(t_ra_copymin_pbqp_solve);
259 /* solve pbqp problem using a reverse perfect elimination order */
260 solve_pbqp_heuristical_co(pbqp_co.pbqp, pbqp_co.rpeo);
261 num solution = get_solution(pbqp_co.pbqp);
264 ir_timer_stop(t_ra_copymin_pbqp_solve);
267 printf("==>> PBQP STATISTIC on IRG %s (%s) <<==\n", get_entity_name(get_irg_entity(co->irg)), arch_register_class_name(co->cls));
268 printf("Number of Nodes: %d\n", number_nodes);
269 printf("Number of independent edges : %d\n", pbqp_co.pbqp->num_edges);
270 printf("Number of trivial solved nodes: %d\n", pbqp_co.pbqp->num_r0);
271 printf("Number of R1 reductions : %d\n", pbqp_co.pbqp->num_r1);
272 printf("Number of R2 reductions : %d\n", pbqp_co.pbqp->num_r2);
273 printf("Number of RN reductions : %d\n", pbqp_co.pbqp->num_rn);
277 printf("%-20s: %8.3lf msec\n", "copy minimization pbqp create",
278 (double)ir_timer_elapsed_usec(t_ra_copymin_pbqp_create) / 1000.0);
279 printf("%-20s: %8.3lf msec\n" , "copy minimization pbqp solve",
280 (double)ir_timer_elapsed_usec(t_ra_copymin_pbqp_solve) / 1000.0);
281 printf("==>> END PBQP TIMING on IRG %s (%s) <<==\n", get_entity_name(get_irg_entity(co->irg)), arch_register_class_name(co->cls));
284 assert(solution != INF_COSTS && "No PBQP solution found");
287 be_ifg_foreach_node(co->cenv->ifg, &nodes_it, ifg_node) {
288 num index = get_node_solution(pbqp_co.pbqp, get_irn_idx(ifg_node));
289 const arch_register_t *reg = arch_register_for_index(co->cls, index);
290 arch_set_irn_register(ifg_node, reg);
293 /* free allocated memory */
297 bitset_free(pbqp_co.ignore_reg);
298 bitset_free(pbqp_co.restricted_nodes);
299 plist_free(pbqp_co.rpeo);
300 free_pbqp(pbqp_co.pbqp);
305 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copypbqp);
306 void be_init_copypbqp(void)
308 static co_algo_info copyheur = {
309 co_solve_heuristic_pbqp, 0
312 be_register_copyopt("pbqp", ©heur);