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