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