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