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 Heuristic PBQP solver for SSA-based register allocation.
24 * @author Thomas Bersch
29 #include "adt/array.h"
34 #include "heuristical_co.h"
37 #include "html_dumper.h"
41 #include "pbqp_edge.h"
42 #include "pbqp_edge_t.h"
43 #include "pbqp_node.h"
44 #include "pbqp_node_t.h"
50 static void apply_RN_co(pbqp *pbqp, plist_t *rpeo)
52 pbqp_node *node = NULL;
53 unsigned min_index = 0;
57 /* We want to reduce the first node in reverse perfect elimination order. */
59 /* get first element from reverse perfect elimination order */
60 node = plist_first(rpeo)->data;
61 /* remove element from reverse perfect elimination order */
62 plist_erase(rpeo, plist_first(rpeo));
63 /* insert node at the end of rpeo so the rpeo already exits after pbqp solving */
64 plist_insert_back(rpeo, node);
65 } while(node_is_reduced(node));
68 assert(pbqp_node_get_degree(node) > 2);
71 if (pbqp->dump_file) {
73 sprintf(txt, "RN-Reduction of Node n%d", node->index);
74 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);
90 FILE *fh = fopen("solutions.pb", "a");
91 fprintf(fh, "[%u]", min_index);
97 /* Now that we found the local minimum set all other costs to infinity. */
98 select_alternative(node, min_index);
101 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
105 ir_timer_t *t_edge = ir_timer_register("be_pbqp_edges", "pbqp reduce independent edges");
106 ir_timer_t *t_r0 = ir_timer_register("be_pbqp_r0", "pbqp R0 reductions");
107 ir_timer_t *t_r1 = ir_timer_register("be_pbqp_r1", "pbqp R1 reductions");
108 ir_timer_t *t_r2 = ir_timer_register("be_pbqp_r2", "pbqp R2 reductions");
109 ir_timer_t *t_rn = ir_timer_register("be_pbqp_rN", "pbqp RN reductions");
112 ir_timer_reset(t_edge);
113 ir_timer_reset(t_r0);
114 ir_timer_reset(t_r1);
115 ir_timer_reset(t_r2);
116 ir_timer_reset(t_rn);
120 if (edge_bucket_get_length(edge_bucket) > 0) {
122 ir_timer_start(t_r0);
130 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
132 ir_timer_start(t_r1);
140 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
142 ir_timer_start(t_r2);
150 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
152 ir_timer_start(t_rn);
155 apply_RN_co(pbqp, rpeo);
162 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_edge), (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
163 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r0), (double)ir_timer_elapsed_usec(t_r0) / 1000.0);
164 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r1), (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
165 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_r2), (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
166 printf("%-20s: %8.3lf msec\n", ir_timer_get_description(t_rn), (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
174 void solve_pbqp_heuristical_co(pbqp *pbqp, plist_t *rpeo)
176 /* Reduce nodes degree ... */
177 initial_simplify_edges(pbqp);
179 /* ... and put node into bucket representing their degree. */
180 fill_node_buckets(pbqp);
183 FILE *fh = fopen("solutions.pb", "a");
184 fprintf(fh, "Solution");
188 apply_heuristic_reductions_co(pbqp, rpeo);
190 pbqp->solution = determine_solution(pbqp);
193 fh = fopen("solutions.pb", "a");
194 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RN/BF:%u\n", pbqp->solution,
195 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
200 /* Solve reduced nodes. */
201 back_propagate(pbqp);