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
28 #include "adt/array.h"
33 #include "heuristical_co.h"
36 #include "html_dumper.h"
40 #include "pbqp_edge.h"
41 #include "pbqp_edge_t.h"
42 #include "pbqp_node.h"
43 #include "pbqp_node_t.h"
49 static void merge_into_RN_node(pbqp_t *pbqp, plist_t *rpeo)
51 pbqp_node_t *node = NULL;
55 /* We want to reduce the first node in reverse perfect elimination order. */
57 /* get first element from reverse perfect elimination order */
58 node = (pbqp_node_t*)plist_first(rpeo)->data;
59 /* remove element from reverse perfect elimination order */
60 plist_erase(rpeo, plist_first(rpeo));
61 /* insert node at the end of rpeo so the rpeo already exits after pbqp solving */
62 plist_insert_back(rpeo, node);
63 } while(node_is_reduced(node));
65 assert(pbqp_node_get_degree(node) > 2);
67 /* Check whether we can merge a neighbor into the current node. */
71 static void apply_RN_co(pbqp_t *pbqp)
81 if (node_is_reduced(node))
85 if (pbqp->dump_file) {
87 sprintf(txt, "RN-Reduction of Node n%d", node->index);
88 pbqp_dump_section(pbqp->dump_file, 2, txt);
89 pbqp_dump_graph(pbqp);
93 min_index = get_local_minimal_alternative(pbqp, node);
96 if (pbqp->dump_file) {
97 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
98 node->index, min_index);
103 FILE *fh = fopen("solutions.pb", "a");
104 fprintf(fh, "[%u]", min_index);
109 /* Now that we found the local minimum set all other costs to infinity. */
110 select_alternative(node, min_index);
113 static void apply_heuristic_reductions_co(pbqp_t *pbqp, plist_t *rpeo)
117 ir_timer_t *t_edge = ir_timer_new();
118 ir_timer_t *t_r1 = ir_timer_new();
119 ir_timer_t *t_r2 = ir_timer_new();
120 ir_timer_t *t_rn = ir_timer_new();
124 if (edge_bucket_get_length(edge_bucket) > 0) {
126 ir_timer_start(t_edge);
132 ir_timer_stop(t_edge);
134 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
136 ir_timer_start(t_r1);
144 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
146 ir_timer_start(t_r2);
154 } else if (merged_node != NULL) {
156 ir_timer_start(t_rn);
164 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
166 ir_timer_start(t_rn);
169 merge_into_RN_node(pbqp, rpeo);
176 printf("PBQP RE reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
177 printf("PBQP R1 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
178 printf("PBQP R2 reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
179 printf("PBQP RN reductions: %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
187 void solve_pbqp_heuristical_co(pbqp_t *pbqp, plist_t *rpeo)
189 /* Reduce nodes degree ... */
190 initial_simplify_edges(pbqp);
192 /* ... and put node into bucket representing their degree. */
193 fill_node_buckets(pbqp);
196 FILE *fh = fopen("solutions.pb", "a");
197 fprintf(fh, "Solution");
201 apply_heuristic_reductions_co(pbqp, rpeo);
203 pbqp->solution = determine_solution(pbqp);
206 fh = fopen("solutions.pb", "a");
207 #if KAPS_USE_UNSIGNED
208 fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
209 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
210 pbqp->num_rm, pbqp->num_rn);
212 fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
213 pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
218 /* Solve reduced nodes. */
219 back_propagate(pbqp);