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