added missing function
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date 25.11.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdarg.h>
11
12 #ifdef WITH_LIBCORE
13 #include <libcore/lc_opts.h>
14 #include <libcore/lc_opts_enum.h>
15 #endif /* WITH_LIBCORE */
16
17 #include "obst.h"
18 #include "bitset.h"
19
20 #include "irprog.h"
21 #include "irgopt.h"
22 #include "irgraph.h"
23 #include "irdump.h"
24 #include "phiclass.h"
25 #include "irdom_t.h"
26 #include "iredges_t.h"
27 #include "irloop_t.h"
28 #include "irtools.h"
29
30 #include "bearch.h"
31 #include "firm/bearch_firm.h"
32 #include "ia32/bearch_ia32.h"
33
34 #include "be_t.h"
35 #include "benumb_t.h"
36 #include "beutil.h"
37 #include "benode_t.h"
38 #include "beirgmod.h"
39 #include "besched_t.h"
40 #include "belistsched.h"
41 #include "belive_t.h"
42 #include "bespillilp.h"
43 #include "bespillbelady.h"
44 #include "bera.h"
45 #include "beraextern.h"
46 #include "bechordal_t.h"
47 #include "beifg.h"
48 #include "beifg_impl.h"
49 #include "becopyopt.h"
50 #include "becopystat.h"
51 #include "bessadestr.h"
52 #include "beabi.h"
53
54
55 #define DUMP_INITIAL            (1 << 0)
56 #define DUMP_ABI                (1 << 1)
57 #define DUMP_SCHED                      (1 << 2)
58 #define DUMP_PREPARED           (1 << 3)
59 #define DUMP_RA                         (1 << 4)
60 #define DUMP_FINAL                      (1 << 5)
61
62 /* options visible for anyone */
63 be_options_t be_options = {
64         /* ilp server */
65         "i44pc52.info.uni-karlsruhe.de",
66
67         /* ilp solver */
68         "cplex"
69 };
70
71 /* dump flags */
72 static unsigned dump_flags = 2 * DUMP_FINAL - 1;
73
74 /* register allocator to use. */
75 static const be_ra_t *ra = &be_ra_chordal_allocator;
76
77 /* back end instruction set architecture to use */
78 static const arch_isa_if_t *isa_if = &ia32_isa_if;
79 #ifdef WITH_LIBCORE
80
81 static lc_opt_entry_t *be_grp_root = NULL;
82
83 /* possible dumping options */
84 static const lc_opt_enum_mask_items_t dump_items[] = {
85         { "none",       0 },
86         { "initial",    DUMP_INITIAL },
87         { "abi",        DUMP_ABI    },
88         { "sched",      DUMP_SCHED  },
89         { "prepared",   DUMP_PREPARED },
90         { "regalloc",   DUMP_RA },
91         { "final",      DUMP_FINAL },
92         { "all",        2 * DUMP_FINAL - 1 },
93         { NULL,         0 }
94 };
95
96 /* register allocators */
97 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
98         { "chordal", &be_ra_chordal_allocator },
99         { "external", &be_ra_external_allocator },
100         { NULL,      NULL }
101 };
102
103 /* instruction set architectures. */
104 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
105         { "firm",    &firm_isa },
106         { "ia32",    &ia32_isa_if },
107         { NULL,      NULL }
108 };
109
110 static lc_opt_enum_mask_var_t dump_var = {
111         &dump_flags, dump_items
112 };
113
114 static lc_opt_enum_const_ptr_var_t ra_var = {
115         (const void **) &ra, ra_items
116 };
117
118 static lc_opt_enum_const_ptr_var_t isa_var = {
119         (const void **) &isa_if, isa_items
120 };
121
122 static const lc_opt_table_entry_t be_main_options[] = {
123         LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
124         LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
125         LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
126
127         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
128         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
129         { NULL }
130 };
131
132 #endif /* WITH_LIBCORE */
133
134 void be_opt_register(void)
135 {
136 #ifdef WITH_LIBCORE
137         int i;
138         lc_opt_entry_t *be_grp_ra;
139
140         be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
141         be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
142
143         lc_opt_add_table(be_grp_root, be_main_options);
144
145         /* register allocator options */
146         for(i = 0; ra_items[i].name != NULL; ++i) {
147                 const be_ra_t *ra = ra_items[i].value;
148                 ra->register_options(be_grp_ra);
149         }
150
151         /* register isa options */
152         for(i = 0; isa_items[i].name != NULL; ++i) {
153                 const arch_isa_if_t *isa = isa_items[i].value;
154                 isa->register_options(be_grp_root);
155         }
156 #endif /* WITH_LIBCORE */
157 }
158
159
160 void be_init(void)
161 {
162         be_opt_register();
163
164         be_sched_init();
165         be_liveness_init();
166         be_numbering_init();
167         be_copy_opt_init();
168         copystat_init();
169         phi_class_init();
170 }
171
172 static be_main_env_t *be_init_env(be_main_env_t *env)
173 {
174         memset(env, 0, sizeof(*env));
175         obstack_init(&env->obst);
176         env->dbg = firm_dbg_register("be.main");
177
178         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
179         arch_env_init(env->arch_env, isa_if);
180
181         /* Register the irn handler of the architecture */
182         if (arch_isa_get_irn_handler(env->arch_env->isa))
183                 arch_env_add_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
184
185                 /*
186                 * Register the node handler of the back end infrastructure.
187                 * This irn handler takes care of the platform independent
188                 * spill, reload and perm nodes.
189         */
190         arch_env_add_irn_handler(env->arch_env, &be_node_irn_handler);
191
192         return env;
193 }
194
195 static void be_done_env(be_main_env_t *env)
196 {
197         env->arch_env->isa->impl->done(env->arch_env->isa);
198         obstack_free(&env->obst, NULL);
199 }
200
201 static void dump(int mask, ir_graph *irg, const char *suffix,
202                                  void (*dumper)(ir_graph *, const char *))
203 {
204         if(dump_flags & mask)
205                 dumper(irg, suffix);
206 }
207
208 static void prepare_graph(be_irg_t *birg)
209 {
210         ir_graph *irg = birg->irg;
211
212         /* Normalize proj nodes. */
213         normalize_proj_nodes(irg);
214
215         /* Make just one return node. */
216         // normalize_one_return(irg);
217
218         /* Remove critical edges */
219         remove_critical_cf_edges(irg);
220
221         /* Compute the dominance information. */
222         free_dom(irg);
223         compute_doms(irg);
224
225         /* Ensure, that the ir_edges are computed. */
226         edges_activate(irg);
227
228         /* check, if the dominance property is fulfilled. */
229         be_check_dominance(irg);
230
231 }
232
233 static void be_main_loop(FILE *file_handle)
234 {
235         int i, n;
236         arch_isa_t *isa;
237         be_main_env_t env;
238
239         be_init_env(&env);
240
241         isa = arch_env_get_isa(env.arch_env);
242
243         /* For all graphs */
244         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
245                 ir_graph *irg = get_irp_irg(i);
246                 const arch_code_generator_if_t *cg_if;
247                 be_irg_t birg;
248
249                 birg.irg      = irg;
250                 birg.main_env = &env;
251
252                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
253                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
254
255                 /* set the current graph (this is important for several firm functions) */
256                 current_ir_graph = birg.irg;
257
258                 /* Get the code generator interface. */
259                 cg_if = isa->impl->get_code_generator_if(isa);
260
261                 /* get a code generator for this graph. */
262                 birg.cg = cg_if->init(file_handle, &birg);
263
264                 /* create the code generator and generate code. */
265                 prepare_graph(&birg);
266
267                 /* implement the ABI conventions. */
268                 birg.abi = be_abi_introduce(&birg);
269                 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
270
271                 /* generate code */
272                 arch_code_generator_prepare_graph(birg.cg);
273
274                 /*
275                  * Since the code generator made a lot of new nodes and skipped
276                  * a lot of old ones, we should do dead node elim here.
277                  * Note that this requires disabling the edges here.
278                  */
279                 edges_deactivate(irg);
280                 dead_node_elimination(irg);
281                 edges_activate(irg);
282
283                 /* Compute loop nesting information (for weighting copies) */
284                 construct_cf_backedges(irg);
285
286                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
287
288                 /* Schedule the graphs. */
289                 arch_code_generator_before_sched(birg.cg);
290                 list_sched(isa, irg);
291
292                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
293
294                 /* connect all stack modifying nodes together (see beabi.c) */
295                 be_abi_fix_stack_nodes(birg.abi);
296
297                 /* Verify the schedule */
298                 sched_verify_irg(irg);
299
300                 /* Do register allocation */
301                 arch_code_generator_before_ra(birg.cg);
302                 ra->allocate(&birg);
303                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
304
305                 be_abi_fix_stack_bias(birg.abi);
306
307                 arch_code_generator_done(birg.cg);
308                 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
309                 be_abi_free(birg.abi);
310         }
311
312         be_done_env(&env);
313 }
314
315 void be_main(FILE *file_handle)
316 {
317         /* never build code for pseudo irgs */
318         set_visit_pseudo_irgs(0);
319
320         be_node_init();
321         be_main_loop(file_handle);
322 }