we need the fix_start_block hack
[libfirm] / ir / be / becopyheur3.c
1 /*
2  * Copyright (C) 1995-2007 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       More experiments on coalescing with Java implementation.
23  * @author      Sebastian Hack
24  * @date        25.07.2006
25  * @version     $Id$
26  */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <libcore/lc_opts.h>
32 #include <libcore/lc_opts_enum.h>
33
34 #include <stdlib.h>
35 #include <limits.h>
36
37 #include "list.h"
38 #include "pdeq.h"
39 #include "bitset.h"
40
41 #include "debug.h"
42 #include "bitfiddle.h"
43 #include "bitset.h"
44 #include "raw_bitset.h"
45
46 #include "irgraph_t.h"
47 #include "irnode_t.h"
48 #include "irprintf.h"
49 #include "irtools.h"
50
51 #include "bemodule.h"
52 #include "beabi.h"
53 #include "benode_t.h"
54 #include "becopyopt.h"
55 #include "becopyopt_t.h"
56 #include "bechordal_t.h"
57 #include "bejavacoal.h"
58
59 #include <stdlib.h>
60 #include <stdio.h>
61
62 #define DUMP_BEFORE 1
63 #define DUMP_AFTER  2
64 #define DUMP_ALL    2 * DUMP_AFTER - 1
65
66 static unsigned dump_flags = 0;
67 static int      dbg_level  = 0;
68
69 static const lc_opt_enum_mask_items_t dump_items[] = {
70         { "before",  DUMP_BEFORE },
71         { "after",   DUMP_AFTER  },
72         { "all",     DUMP_ALL    },
73         { NULL,      0 }
74 };
75
76 static lc_opt_enum_mask_var_t dump_var = {
77         &dump_flags, dump_items
78 };
79
80 static const lc_opt_table_entry_t options[] = {
81         LC_OPT_ENT_ENUM_MASK("dump", "dump ifg cloud",                              &dump_var),
82         LC_OPT_ENT_INT      ("dbg",  "debug level for the Java coalescer",          &dbg_level),
83         LC_OPT_LAST
84 };
85
86 void be_init_copyheur3(void)
87 {
88         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
89         lc_opt_entry_t *ra_grp = lc_opt_get_grp(be_grp, "ra");
90         lc_opt_entry_t *chordal_grp = lc_opt_get_grp(ra_grp, "chordal");
91         lc_opt_entry_t *co3_grp = lc_opt_get_grp(chordal_grp, "co3");
92
93         lc_opt_add_table(co3_grp, options);
94 }
95
96 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_copyheur3);
97
98 static void set_admissible_regs(be_java_coal_t *coal, copy_opt_t *co, ir_node *irn, int t_idx, int *col_map)
99 {
100         const arch_register_req_t *req;
101
102         req = arch_get_register_req(co->aenv, irn, BE_OUT_POS(0));
103
104         // ir_printf("%+F\n", irn);
105         if(arch_register_req_is(req, limited)) {
106                 unsigned i;
107                 unsigned n_regs = co->cls->n_regs;
108
109                 for(i = 0; i < n_regs; ++i) {
110                         if(!rbitset_is_set(req->limited, i) && col_map[i] >= 0) {
111                                 be_java_coal_forbid_color(coal, t_idx, col_map[i]);
112                         }
113                 }
114         }
115 }
116
117 int co_solve_heuristic_java(copy_opt_t *co)
118 {
119         be_ifg_t *ifg   = co->cenv->ifg;
120         void *nodes_it  = be_ifg_nodes_iter_alloca(ifg);
121         void *neigh_it  = be_ifg_neighbours_iter_alloca(ifg);
122         bitset_t *nodes = bitset_malloc(get_irg_last_idx(co->irg));
123         unsigned n_regs = co->cenv->cls->n_regs;
124
125         char dbg[256];
126         unsigned i, j, curr_idx;
127         int *col_map;
128         int *inv_col_map;
129
130         int *node_map;
131         int *inv_node_map;
132
133         be_java_coal_t *coal;
134         ir_node *n, *m;
135
136         col_map     = alloca(n_regs * sizeof(col_map[0]));
137         inv_col_map = alloca(n_regs * sizeof(inv_col_map[0]));
138
139         memset(inv_col_map, -1, sizeof(inv_col_map[0]) * n_regs);
140
141         for(i = 0, j = 0; i < n_regs; ++i) {
142                 const arch_register_t *reg = &co->cls->regs[i];
143                 col_map[i] = -1;
144                 if(!arch_register_type_is(reg, ignore)) {
145                         col_map[i] = j;
146                         inv_col_map[j] = i;
147                         ++j;
148                 }
149         }
150
151         node_map     = xmalloc((get_irg_last_idx(co->irg) + 1) * sizeof(node_map[0]));
152         inv_node_map = xmalloc((get_irg_last_idx(co->irg) + 1) * sizeof(inv_node_map[0]));
153
154         curr_idx = 0;
155         be_ifg_foreach_node(ifg, nodes_it, n) {
156                 if(!arch_irn_is(co->aenv, n, ignore)) {
157                         int idx = get_irn_idx(n);
158                         bitset_set(nodes, idx);
159                         node_map[idx] = curr_idx;
160                         inv_node_map[curr_idx] = idx;
161                         curr_idx++;
162                 }
163         }
164
165         if(curr_idx == 0) {
166                 free(node_map);
167                 free(inv_node_map);
168                 bitset_free(nodes);
169                 return 0;
170         }
171
172         coal = be_java_coal_init("test", curr_idx, j, dbg_level);
173
174         /* Check, if all neighbours are indeed connected to the node. */
175         be_ifg_foreach_node(ifg, nodes_it, n) {
176                 int n_idx = get_irn_idx(n);
177                 int t_idx = node_map[n_idx];
178
179                 if(bitset_is_set(nodes, n_idx)) {
180                         affinity_node_t *an = get_affinity_info(co, n);
181
182                         ir_snprintf(dbg, sizeof(dbg), "%+F", n);
183                         be_java_coal_set_debug(coal, t_idx, dbg);
184                         be_java_coal_set_color(coal, t_idx, col_map[arch_get_irn_register(co->aenv, n)->index]);
185                         set_admissible_regs(coal, co, n, t_idx, col_map);
186                         be_ifg_foreach_neighbour(ifg, neigh_it, n, m) {
187                                 int m_idx = get_irn_idx(m);
188                                 int s_idx = node_map[m_idx];
189
190                                 if(n_idx < m_idx && bitset_is_set(nodes, m_idx)) {
191                                         be_java_coal_add_int_edge(coal, s_idx, t_idx);
192                                 }
193                         }
194
195                         if(an != NULL) {
196                                 neighb_t *neigh;
197                                 co_gs_foreach_neighb(an, neigh) {
198                                         int m_idx = get_irn_idx(neigh->irn);
199                                         int s_idx = node_map[m_idx];
200
201                                         if(n_idx < m_idx && bitset_is_set(nodes, m_idx)) {
202                                                 be_java_coal_add_aff_edge(coal, s_idx, t_idx, neigh->costs);
203                                         }
204                                 }
205                         }
206                 }
207         }
208
209         if(dump_flags & DUMP_BEFORE) {
210                 char fn[512];
211                 ir_snprintf(fn, sizeof(fn), "%F-%s-before.dot", co->cenv->irg, co->cenv->cls->name);
212                 be_java_coal_dump(coal, fn);
213         }
214
215         be_java_coal_coalesce(coal);
216
217         be_ifg_foreach_node(ifg, nodes_it, n) {
218                 unsigned idx = get_irn_idx(n);
219                 if(bitset_is_set(nodes, idx)) {
220                         unsigned t_idx             = node_map[idx];
221                         unsigned col               = inv_col_map[be_java_coal_get_color(coal, t_idx)];
222                         const arch_register_t *reg;
223
224                         assert(col < n_regs);
225                         reg     = &co->cls->regs[col];
226                         arch_set_irn_register(co->aenv, n, reg);
227                 }
228         }
229
230         if(dump_flags & DUMP_AFTER) {
231                 char fn[512];
232                 ir_snprintf(fn, sizeof(fn), "%F-%s-after.dot", co->cenv->irg, co->cenv->cls->name);
233                 be_java_coal_dump(coal, fn);
234         }
235
236         be_java_coal_destroy(coal);
237         bitset_free(nodes);
238         return 0;
239 }