b33e9d74647907ae22e0a5c5617f2f7cceb7a2c3
[libfirm] / heuristical_co.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief   Heuristic PBQP solver for SSA-based register allocation.
23  * @date    18.09.2009
24  * @author  Thomas Bersch
25  * @version $Id$
26  */
27 #include "config.h"
28
29 #include "adt/array.h"
30 #include "assert.h"
31 #include "error.h"
32
33 #include "bucket.h"
34 #include "heuristical_co.h"
35 #include "optimal.h"
36 #if     KAPS_DUMP
37 #include "html_dumper.h"
38 #endif
39 #include "kaps.h"
40 #include "matrix.h"
41 #include "pbqp_edge.h"
42 #include "pbqp_edge_t.h"
43 #include "pbqp_node.h"
44 #include "pbqp_node_t.h"
45 #include "vector.h"
46
47 #include "plist.h"
48 #include "timing.h"
49
50 static void merge_into_RN_node(pbqp *pbqp, plist_t *rpeo)
51 {
52         pbqp_node   *node         = NULL;
53
54         assert(pbqp);
55
56         /* We want to reduce the first node in reverse perfect elimination order. */
57         do {
58                 /* get first element from reverse perfect elimination order */
59                 node = plist_first(rpeo)->data;
60                 /* remove element from reverse perfect elimination order */
61                 plist_erase(rpeo, plist_first(rpeo));
62                 /* insert node at the end of rpeo so the rpeo already exits after pbqp solving */
63                 plist_insert_back(rpeo, node);
64         } while(node_is_reduced(node));
65
66         assert(node);
67         assert(pbqp_node_get_degree(node) > 2);
68
69         /* Check whether we can merge a neighbor into the current node. */
70         apply_RM(pbqp, node);
71 }
72
73 static void apply_RN_co(pbqp *pbqp)
74 {
75         pbqp_node   *node;
76         unsigned     min_index;
77
78         assert(pbqp);
79
80         node        = merged_node;
81         merged_node = NULL;
82         assert(node);
83
84         if (node_is_reduced(node))
85                 return;
86
87 #if     KAPS_DUMP
88         if (pbqp->dump_file) {
89                 char     txt[100];
90                 sprintf(txt, "RN-Reduction of Node n%d", node->index);
91                 dump_section(pbqp->dump_file, 2, txt);
92                 pbqp_dump_graph(pbqp);
93         }
94 #endif
95
96         min_index = get_local_minimal_alternative(pbqp, node);
97
98 #if     KAPS_DUMP
99         if (pbqp->dump_file) {
100                 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
101                                         node->index, min_index);
102         }
103 #endif
104
105 #if KAPS_STATISTIC
106                 FILE *fh = fopen("solutions.pb", "a");
107                 fprintf(fh, "[%u]", min_index);
108                 fclose(fh);
109                 pbqp->num_rn++;
110 #endif
111
112         /* Now that we found the local minimum set all other costs to infinity. */
113         select_alternative(node, min_index);
114 }
115
116 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
117 {
118         #if KAPS_TIMING
119                 /* create timers */
120                 ir_timer_t *t_edge = ir_timer_new();
121                 ir_timer_t *t_r1   = ir_timer_new();
122                 ir_timer_t *t_r2   = ir_timer_new();
123                 ir_timer_t *t_rn   = ir_timer_new();
124         #endif
125
126         for (;;) {
127                 if (edge_bucket_get_length(edge_bucket) > 0) {
128                         #if KAPS_TIMING
129                                 ir_timer_start(t_edge);
130                         #endif
131
132                         apply_edge(pbqp);
133
134                         #if KAPS_TIMING
135                                 ir_timer_stop(t_edge);
136                         #endif
137                 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
138                         #if KAPS_TIMING
139                                 ir_timer_start(t_r1);
140                         #endif
141
142                         apply_RI(pbqp);
143
144                         #if KAPS_TIMING
145                                 ir_timer_stop(t_r1);
146                         #endif
147                 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
148                         #if KAPS_TIMING
149                                 ir_timer_start(t_r2);
150                         #endif
151
152                         apply_RII(pbqp);
153
154                         #if KAPS_TIMING
155                                 ir_timer_stop(t_r2);
156                         #endif
157                 } else if (merged_node != NULL) {
158                         #if KAPS_TIMING
159                                 ir_timer_start(t_rn);
160                         #endif
161
162                         apply_RN_co(pbqp);
163
164                         #if KAPS_TIMING
165                                 ir_timer_stop(t_rn);
166                         #endif
167                 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
168                         #if KAPS_TIMING
169                                 ir_timer_start(t_rn);
170                         #endif
171
172                         merge_into_RN_node(pbqp, rpeo);
173
174                         #if KAPS_TIMING
175                                 ir_timer_stop(t_rn);
176                         #endif
177                 } else {
178                         #if KAPS_TIMING
179                                 printf("PBQP RE reductions:           %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
180                                 printf("PBQP R1 reductions:           %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
181                                 printf("PBQP R2 reductions:           %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
182                                 printf("PBQP RN reductions:           %10.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
183                         #endif
184
185                         return;
186                 }
187         }
188 }
189
190 void solve_pbqp_heuristical_co(pbqp *pbqp, plist_t *rpeo)
191 {
192         /* Reduce nodes degree ... */
193         initial_simplify_edges(pbqp);
194
195         /* ... and put node into bucket representing their degree. */
196         fill_node_buckets(pbqp);
197
198         #if KAPS_STATISTIC
199                 FILE *fh = fopen("solutions.pb", "a");
200                 fprintf(fh, "Solution");
201                 fclose(fh);
202         #endif
203
204         apply_heuristic_reductions_co(pbqp, rpeo);
205
206         pbqp->solution = determine_solution(pbqp);
207
208         #if KAPS_STATISTIC
209                 fh = fopen("solutions.pb", "a");
210                 #if KAPS_USE_UNSIGNED
211                         fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
212                                         pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
213                                         pbqp->num_rm, pbqp->num_rn);
214                 #else
215                         fprintf(fh, ": %lld RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
216                                         pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
217                 #endif
218                 fclose(fh);
219         #endif
220
221         /* Solve reduced nodes. */
222         back_propagate(pbqp);
223
224         free_buckets();
225 }