Argh, we need to propagate if at least one infinity is added.
[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 apply_RN_co(pbqp *pbqp, plist_t *rpeo)
51 {
52         pbqp_node   *node         = NULL;
53         unsigned     min_index    = 0;
54
55         assert(pbqp);
56
57         /* We want to reduce the first node in reverse perfect elimination order. */
58         do {
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));
66
67         assert(node);
68         assert(pbqp_node_get_degree(node) > 2);
69
70 #if     KAPS_DUMP
71         if (pbqp->dump_file) {
72                 char     txt[100];
73                 sprintf(txt, "RN-Reduction of Node n%d", node->index);
74                 dump_section(pbqp->dump_file, 2, txt);
75                 pbqp_dump_graph(pbqp);
76         }
77 #endif
78         /* Check whether we can merge a neighbor into the current node. */
79         apply_RM(pbqp, node);
80
81         /* Apply optimal solution for the given node, if possible. */
82         if (pbqp_node_get_degree(node) < 3)
83                 return;
84
85         min_index = get_local_minimal_alternative(pbqp, node);
86
87 #if     KAPS_DUMP
88         if (pbqp->dump_file) {
89                 fprintf(pbqp->dump_file, "node n%d is set to %d<br><br>\n",
90                                         node->index, min_index);
91         }
92 #endif
93
94 #if KAPS_STATISTIC
95                 FILE *fh = fopen("solutions.pb", "a");
96                 fprintf(fh, "[%u]", min_index);
97                 fclose(fh);
98                 pbqp->num_rn++;
99 #endif
100
101         /* Now that we found the local minimum set all other costs to infinity. */
102         select_alternative(node, min_index);
103 }
104
105 static void apply_heuristic_reductions_co(pbqp *pbqp, plist_t *rpeo)
106 {
107         #if KAPS_TIMING
108                 /* create timers */
109                 ir_timer_t *t_edge = ir_timer_new();
110                 ir_timer_t *t_r1   = ir_timer_new();
111                 ir_timer_t *t_r2   = ir_timer_new();
112                 ir_timer_t *t_rn   = ir_timer_new();
113         #endif
114
115         for (;;) {
116                 if (edge_bucket_get_length(edge_bucket) > 0) {
117                         #if KAPS_TIMING
118                                 ir_timer_start(t_edge);
119                         #endif
120
121                         apply_edge(pbqp);
122
123                         #if KAPS_TIMING
124                                 ir_timer_stop(t_edge);
125                         #endif
126                 } else if (node_bucket_get_length(node_buckets[1]) > 0) {
127                         #if KAPS_TIMING
128                                 ir_timer_start(t_r1);
129                         #endif
130
131                         apply_RI(pbqp);
132
133                         #if KAPS_TIMING
134                                 ir_timer_stop(t_r1);
135                         #endif
136                 } else if (node_bucket_get_length(node_buckets[2]) > 0) {
137                         #if KAPS_TIMING
138                                 ir_timer_start(t_r2);
139                         #endif
140
141                         apply_RII(pbqp);
142
143                         #if KAPS_TIMING
144                                 ir_timer_stop(t_r2);
145                         #endif
146                 } else if (node_bucket_get_length(node_buckets[3]) > 0) {
147                         #if KAPS_TIMING
148                                 ir_timer_start(t_rn);
149                         #endif
150
151                         apply_RN_co(pbqp, rpeo);
152
153                         #if KAPS_TIMING
154                                 ir_timer_stop(t_rn);
155                         #endif
156                 } else {
157                         #if KAPS_TIMING
158                                 printf("pbqp RE reductions: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_edge) / 1000.0);
159                                 printf("pbqp R1 reductions: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_r1) / 1000.0);
160                                 printf("pbqp R2 reductions: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_r2) / 1000.0);
161                                 printf("pbqp RN reductions: %8.3lf msec\n", (double)ir_timer_elapsed_usec(t_rn) / 1000.0);
162                         #endif
163
164                         return;
165                 }
166         }
167 }
168
169 void solve_pbqp_heuristical_co(pbqp *pbqp, plist_t *rpeo)
170 {
171         /* Reduce nodes degree ... */
172         initial_simplify_edges(pbqp);
173
174         /* ... and put node into bucket representing their degree. */
175         fill_node_buckets(pbqp);
176
177         #if KAPS_STATISTIC
178                 FILE *fh = fopen("solutions.pb", "a");
179                 fprintf(fh, "Solution");
180                 fclose(fh);
181         #endif
182
183         apply_heuristic_reductions_co(pbqp, rpeo);
184
185         pbqp->solution = determine_solution(pbqp);
186
187         #if KAPS_STATISTIC
188                 fh = fopen("solutions.pb", "a");
189                 #if KAPS_USE_UNSIGNED
190                         fprintf(fh, ": %u RE:%u R0:%u R1:%u R2:%u RM:%u RN/BF:%u\n", pbqp->solution,
191                                         pbqp->num_edges, pbqp->num_r0, pbqp->num_r1, pbqp->num_r2,
192                                         pbqp->num_rm, pbqp->num_rn);
193                 #else
194                         fprintf(fh, ": %lld 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                 #endif
197                 fclose(fh);
198         #endif
199
200         /* Solve reduced nodes. */
201         back_propagate(pbqp);
202
203         free_buckets();
204 }