made code C89 compliant (changed unnamed union in attributes)
[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 #include "return.h"
30
31 #include "bearch.h"
32 #include "firm/bearch_firm.h"
33 #include "ia32/bearch_ia32.h"
34 #include "arm/bearch_arm.h"
35 #include "ppc32/bearch_ppc32.h"
36 #include "mips/bearch_mips.h"
37
38 #include "be_t.h"
39 #include "benumb_t.h"
40 #include "beutil.h"
41 #include "benode_t.h"
42 #include "beirgmod.h"
43 #include "besched_t.h"
44 #include "belistsched.h"
45 #include "belive_t.h"
46 #include "bespillilp.h"
47 #include "bespillbelady.h"
48 #include "bera.h"
49 #include "beraextern.h"
50 #include "bechordal_t.h"
51 #include "beifg.h"
52 #include "beifg_impl.h"
53 #include "becopyopt.h"
54 #include "becopystat.h"
55 #include "bessadestr.h"
56 #include "beabi.h"
57 #include "belower.h"
58
59 #define DUMP_INITIAL    (1 << 0)
60 #define DUMP_ABI        (1 << 1)
61 #define DUMP_SCHED      (1 << 2)
62 #define DUMP_PREPARED   (1 << 3)
63 #define DUMP_RA         (1 << 4)
64 #define DUMP_FINAL      (1 << 5)
65
66 /* options visible for anyone */
67 static be_options_t be_options = {
68         /* ilp server */
69         "i44pc52.info.uni-karlsruhe.de",
70
71         /* ilp solver */
72         "cplex"
73 };
74
75 /* dump flags */
76 static unsigned dump_flags = 2 * DUMP_FINAL - 1;
77
78 /* register allocator to use. */
79 static const be_ra_t *ra = &be_ra_chordal_allocator;
80
81 /* back end instruction set architecture to use */
82 static const arch_isa_if_t *isa_if = &ia32_isa_if;
83 #ifdef WITH_LIBCORE
84
85 static lc_opt_entry_t *be_grp_root = NULL;
86
87 /* possible dumping options */
88 static const lc_opt_enum_mask_items_t dump_items[] = {
89         { "none",       0 },
90         { "initial",    DUMP_INITIAL },
91         { "abi",        DUMP_ABI    },
92         { "sched",      DUMP_SCHED  },
93         { "prepared",   DUMP_PREPARED },
94         { "regalloc",   DUMP_RA },
95         { "final",      DUMP_FINAL },
96         { "all",        2 * DUMP_FINAL - 1 },
97         { NULL,         0 }
98 };
99
100 /* register allocators */
101 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
102         { "chordal", &be_ra_chordal_allocator },
103         { "external", &be_ra_external_allocator },
104         { NULL,      NULL }
105 };
106
107 /* instruction set architectures. */
108 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
109         { "firm",    &firm_isa },
110         { "ia32",    &ia32_isa_if },
111         { "arm",     &arm_isa_if },
112         { "ppc",     &ppc32_isa_if },
113         { "mips",    &mips_isa_if },
114         { NULL,      NULL }
115 };
116
117 static lc_opt_enum_mask_var_t dump_var = {
118         &dump_flags, dump_items
119 };
120
121 static lc_opt_enum_const_ptr_var_t ra_var = {
122         (const void **) &ra, ra_items
123 };
124
125 static lc_opt_enum_const_ptr_var_t isa_var = {
126         (const void **) &isa_if, isa_items
127 };
128
129 static const lc_opt_table_entry_t be_main_options[] = {
130         LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
131         LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
132         LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
133
134 #ifdef WITH_ILP
135         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
136         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
137 #endif /* WITH_ILP */
138         { NULL }
139 };
140
141 #endif /* WITH_LIBCORE */
142
143 void be_opt_register(void)
144 {
145 #ifdef WITH_LIBCORE
146         int i;
147         lc_opt_entry_t *be_grp_ra;
148
149         be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
150         be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
151
152         lc_opt_add_table(be_grp_root, be_main_options);
153
154         /* register allocator options */
155         for(i = 0; ra_items[i].name != NULL; ++i) {
156                 const be_ra_t *ra = ra_items[i].value;
157                 ra->register_options(be_grp_ra);
158         }
159
160         /* register isa options */
161         for(i = 0; isa_items[i].name != NULL; ++i) {
162                 const arch_isa_if_t *isa = isa_items[i].value;
163                 isa->register_options(be_grp_root);
164         }
165 #endif /* WITH_LIBCORE */
166 }
167
168
169 void be_init(void)
170 {
171         be_opt_register();
172
173         be_sched_init();
174         be_liveness_init();
175         be_numbering_init();
176         be_copy_opt_init();
177         copystat_init();
178         phi_class_init();
179 }
180
181 static be_main_env_t *be_init_env(be_main_env_t *env)
182 {
183         memset(env, 0, sizeof(*env));
184         obstack_init(&env->obst);
185         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
186         env->options  = &be_options;
187         FIRM_DBG_REGISTER(env->dbg, "be.main");
188
189         arch_env_init(env->arch_env, isa_if);
190
191         /* Register the irn handler of the architecture */
192         if (arch_isa_get_irn_handler(env->arch_env->isa))
193                 arch_env_push_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
194
195         /*
196          * Register the node handler of the back end infrastructure.
197          * This irn handler takes care of the platform independent
198          * spill, reload and perm nodes.
199          */
200         arch_env_push_irn_handler(env->arch_env, &be_node_irn_handler);
201         env->phi_handler = be_phi_handler_new(env->arch_env);
202         arch_env_push_irn_handler(env->arch_env, env->phi_handler);
203
204         return env;
205 }
206
207 static void be_done_env(be_main_env_t *env)
208 {
209         env->arch_env->isa->impl->done(env->arch_env->isa);
210         be_phi_handler_free(env->phi_handler);
211         obstack_free(&env->obst, NULL);
212 }
213
214 static void dump(int mask, ir_graph *irg, const char *suffix,
215                                  void (*dumper)(ir_graph *, const char *))
216 {
217         if(dump_flags & mask)
218                 be_dump(irg, suffix, dumper);
219 }
220
221 /**
222  * Prepare a backend graph for code generation.
223  */
224 static void prepare_graph(be_irg_t *birg)
225 {
226         ir_graph *irg = birg->irg;
227
228         /* Normalize proj nodes. */
229         normalize_proj_nodes(irg);
230
231         /* Make just one return node. */
232         normalize_one_return(irg);
233
234         /* Remove critical edges */
235         remove_critical_cf_edges(irg);
236
237         /* Compute the dominance information. */
238         free_dom(irg);
239         compute_doms(irg);
240
241         /* Ensure, that the ir_edges are computed. */
242         edges_activate(irg);
243
244         /* check, if the dominance property is fulfilled. */
245         be_check_dominance(irg);
246
247         /* reset the phi handler. */
248         be_phi_handler_reset(birg->main_env->phi_handler);
249 }
250
251 /**
252  * The Firm backend main loop.
253  * Do architecture specific lowering for all graphs
254  * and call the architecture specific code generator.
255  */
256 static void be_main_loop(FILE *file_handle)
257 {
258         int i, n;
259         arch_isa_t *isa;
260         be_main_env_t env;
261
262         be_init_env(&env);
263
264         isa = arch_env_get_isa(env.arch_env);
265
266         /* For all graphs */
267         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
268                 ir_graph *irg = get_irp_irg(i);
269                 const arch_code_generator_if_t *cg_if;
270                 be_irg_t birg;
271
272                 birg.irg      = irg;
273                 birg.main_env = &env;
274
275                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
276                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
277
278                 /* set the current graph (this is important for several firm functions) */
279                 current_ir_graph = birg.irg;
280
281                 /* Get the code generator interface. */
282                 cg_if = isa->impl->get_code_generator_if(isa);
283
284                 /* get a code generator for this graph. */
285                 birg.cg = cg_if->init(file_handle, &birg);
286
287                 /* create the code generator and generate code. */
288                 prepare_graph(&birg);
289
290                 /* some transformations need to be done before abi introduce */
291                 arch_code_generator_before_abi(birg.cg);
292
293                 /* implement the ABI conventions. */
294                 birg.abi = be_abi_introduce(&birg);
295                 dump(DUMP_ABI, irg, "-abi", dump_ir_block_graph);
296
297                 /* generate code */
298                 arch_code_generator_prepare_graph(birg.cg);
299
300                 /*
301                  * Since the code generator made a lot of new nodes and skipped
302                  * a lot of old ones, we should do dead node elimination here.
303                  * Note that this requires disabling the edges here.
304                  */
305                 edges_deactivate(irg);
306                 //dead_node_elimination(irg);
307                 edges_activate(irg);
308
309                 /* Compute loop nesting information (for weighting copies) */
310                 construct_cf_backedges(irg);
311
312                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
313
314                 /* add Keeps for should_be_different constrained nodes */
315                 // assure_constraints(&birg);
316                 dump(DUMP_PREPARED, irg, "-assured", dump_ir_block_graph);
317
318                 /* Schedule the graphs. */
319                 arch_code_generator_before_sched(birg.cg);
320                 list_sched(env.arch_env, irg);
321
322                 /* connect all stack modifying nodes together (see beabi.c) */
323                 be_abi_fix_stack_nodes(birg.abi);
324                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
325
326                 /* Verify the schedule */
327                 sched_verify_irg(irg);
328
329                 /* Do register allocation */
330                 arch_code_generator_before_ra(birg.cg);
331                 ra->allocate(&birg);
332                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
333
334                 arch_code_generator_after_ra(birg.cg);
335                 be_abi_fix_stack_bias(birg.abi);
336
337                 arch_code_generator_done(birg.cg);
338                 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
339                 be_abi_free(birg.abi);
340         }
341
342         be_done_env(&env);
343 }
344
345 /* Main interface to the frontend. */
346 void be_main(FILE *file_handle)
347 {
348         /* never build code for pseudo irgs */
349         set_visit_pseudo_irgs(0);
350
351         be_node_init();
352         be_main_loop(file_handle);
353 }
354
355 /** The debug info retriever function. */
356 static retrieve_dbg_func retrieve_dbg = NULL;
357
358 /* Sets a debug info retriever. */
359 void be_set_debug_retrieve(retrieve_dbg_func func) {
360         retrieve_dbg = func;
361 }
362
363 /* Retrieve the debug info. */
364 const char *be_retrieve_dbg_info(const dbg_info *dbg, unsigned *line) {
365         if (retrieve_dbg)
366                 return retrieve_dbg(dbg, line);
367         *line = 0;
368         return NULL;
369 }