2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Heuristic PBQP solver for SSA-based register allocation.
10 * @author Thomas Bersch
14 #include "adt/array.h"
19 #include "heuristical_co.h"
22 #include "html_dumper.h"
26 #include "pbqp_edge.h"
27 #include "pbqp_edge_t.h"
28 #include "pbqp_node.h"
29 #include "pbqp_node_t.h"
35 static void merge_into_RN_node(pbqp_t *pbqp, plist_t *rpeo)
37 pbqp_node_t *node = NULL;
41 /* We want to reduce the first node in reverse perfect elimination order. */
43 /* get first element from reverse perfect elimination order */
44 node = (pbqp_node_t*)plist_first(rpeo)->data;
45 /* remove element from reverse perfect elimination order */
46 plist_erase(rpeo, plist_first(rpeo));
47 /* insert node at the end of rpeo so the rpeo already exits after pbqp solving */
48 plist_insert_back(rpeo, node);
49 } while(node_is_reduced(node));
51 assert(pbqp_node_get_degree(node) > 2);
53 /* Check whether we can merge a neighbor into the current node. */
57 static void apply_RN_co(pbqp_t *pbqp)
67 if (node_is_reduced(node))
71 if (pbqp->dump_file) {
73 sprintf(txt, "RN-Reduction of Node n%d", node->index);
74 pbqp_dump_section(pbqp->dump_file, 2, txt);
75 pbqp_dump_graph(pbqp);
79 min_index = get_local_minimal_alternative(pbqp, node);
82 if (pbqp->dump_file) {
83 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
84 node->index, min_index);
89 FILE *fh = fopen("solutions.pb", "a");
90 fprintf(fh, "[%u]", min_index);
95 /* Now that we found the local minimum set all other costs to infinity. */
96 select_alternative(node, min_index);
99 static void apply_heuristic_reductions_co(pbqp_t *pbqp, plist_t *rpeo)
103 ir_timer_t *t_edge = ir_timer_new();
104 ir_timer_t *t_r1 = ir_timer_new();
105 ir_timer_t *t_r2 = ir_timer_new();
106 ir_timer_t *t_rn = ir_timer_new();
110 if (edge_bucket_get_length(edge_bucket) > 0) {
112 ir_timer_start(t_edge);
118 ir_timer_stop(t_edge);
120 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
122 ir_timer_start(t_r1);
130 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
132 ir_timer_start(t_r2);
140 } else if (merged_node != NULL) {
142 ir_timer_start(t_rn);
150 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
152 ir_timer_start(t_rn);
155 merge_into_RN_node(pbqp, rpeo);
162 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
163 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
164 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
165 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
173 void solve_pbqp_heuristical_co(pbqp_t *pbqp, plist_t *rpeo)
175 /* Reduce nodes degree ... */
176 initial_simplify_edges(pbqp);
178 /* ... and put node into bucket representing their degree. */
179 fill_node_buckets(pbqp);
182 FILE *fh = fopen("solutions.pb", "a");
183 fprintf(fh, "Solution");
187 apply_heuristic_reductions_co(pbqp, rpeo);
189 pbqp->solution = determine_solution(pbqp);
192 fh = fopen("solutions.pb", "a");
193 #if KAPS_USE_UNSIGNED
194 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
195 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
196 pbqp->num_rm, pbqp->num_rn);
198 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
199 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
204 /* Solve reduced nodes. */
205 back_propagate(pbqp);