fa665d2c0ae6d4054523dadf21c873d7d991c5c9
[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 "becopyoptmain.h"
50 #include "becopystat.h"
51 #include "bessadestr.h"
52 #include "beabi.h"
53
54
55 #define DUMP_INITIAL            (1 << 0)
56 #define DUMP_SCHED                      (1 << 1)
57 #define DUMP_PREPARED           (1 << 2)
58 #define DUMP_RA                         (1 << 3)
59 #define DUMP_FINAL                      (1 << 4)
60
61 /* options visible for anyone */
62 be_options_t be_options = {
63         /* ilp server */
64         "i44pc52.info.uni-karlsruhe.de",
65
66         /* ilp solver */
67         "cplex"
68 };
69
70 /* dump flags */
71 static unsigned dump_flags = DUMP_INITIAL | DUMP_SCHED | DUMP_PREPARED | DUMP_RA | DUMP_FINAL;
72
73 /* register allocator to use. */
74 static const be_ra_t *ra = &be_ra_chordal_allocator;
75
76 /* back end instruction set architecture to use */
77 static const arch_isa_if_t *isa_if = &ia32_isa_if;
78
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         { "sched",      DUMP_SCHED  },
88         { "prepared",   DUMP_PREPARED },
89         { "regalloc",   DUMP_RA },
90         { "final",      DUMP_FINAL },
91         { "all",        2 * DUMP_FINAL - 1 },
92         { NULL,         0 }
93 };
94
95 /* register allocators */
96 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
97         { "chordal", &be_ra_chordal_allocator },
98         { "external", &be_ra_external_allocator },
99         { NULL,      NULL }
100 };
101
102 /* instruction set architectures. */
103 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
104         { "firm",    &firm_isa },
105         { "ia32",    &ia32_isa_if },
106         { NULL,      NULL }
107 };
108
109 static lc_opt_enum_mask_var_t dump_var = {
110         &dump_flags, dump_items
111 };
112
113 static lc_opt_enum_const_ptr_var_t ra_var = {
114         (const void **) &ra, ra_items
115 };
116
117 static lc_opt_enum_const_ptr_var_t isa_var = {
118         (const void **) &isa_if, isa_items
119 };
120
121 static const lc_opt_table_entry_t be_main_options[] = {
122         LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
123         LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
124         LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
125
126         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
127         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
128         { NULL }
129 };
130
131 #endif /* WITH_LIBCORE */
132
133 void be_opt_register(void)
134 {
135 #ifdef WITH_LIBCORE
136         int i;
137         lc_opt_entry_t *be_grp_ra;
138
139         be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
140         be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
141
142         lc_opt_add_table(be_grp_root, be_main_options);
143
144         /* register allocator options */
145         for(i = 0; ra_items[i].name != NULL; ++i) {
146                 const be_ra_t *ra = ra_items[i].value;
147                 ra->register_options(be_grp_ra);
148         }
149
150         /* register isa options */
151         for(i = 0; isa_items[i].name != NULL; ++i) {
152                 const arch_isa_if_t *isa = isa_items[i].value;
153                 isa->register_options(be_grp_root);
154         }
155 #endif /* WITH_LIBCORE */
156 }
157
158
159 void be_init(void)
160 {
161         be_opt_register();
162
163         be_sched_init();
164         be_liveness_init();
165         be_numbering_init();
166         be_copy_opt_init();
167         copystat_init();
168         phi_class_init();
169 }
170
171 static be_main_env_t *be_init_env(be_main_env_t *env)
172 {
173         int i, j, n;
174
175   memset(env, 0, sizeof(*env));
176         obstack_init(&env->obst);
177         env->dbg = firm_dbg_register("be.main");
178
179         env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
180         arch_env_init(env->arch_env, isa_if);
181
182         /* Register the irn handler of the architecture */
183         if (arch_isa_get_irn_handler(env->arch_env->isa))
184                 arch_env_add_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
185
186                 /*
187                 * Register the node handler of the back end infrastructure.
188                 * This irn handler takes care of the platform independent
189                 * spill, reload and perm nodes.
190         */
191         arch_env_add_irn_handler(env->arch_env, &be_node_irn_handler);
192
193         /*
194         * Create the list of caller save registers.
195         */
196         for(i = 0, n = arch_isa_get_n_reg_class(env->arch_env->isa); i < n; ++i) {
197                 const arch_register_class_t *cls = arch_isa_get_reg_class(env->arch_env->isa, i);
198                 for(j = 0; j < cls->n_regs; ++j) {
199                         const arch_register_t *reg = arch_register_for_index(cls, j);
200                         if(arch_register_type_is(reg, caller_save))
201                                 obstack_ptr_grow(&env->obst, reg);
202                 }
203         }
204         obstack_ptr_grow(&env->obst, NULL);
205         env->caller_save = obstack_finish(&env->obst);
206
207         /*
208         * Create the list of callee save registers.
209         */
210         for(i = 0, n = arch_isa_get_n_reg_class(env->arch_env->isa); i < n; ++i) {
211                 const arch_register_class_t *cls = arch_isa_get_reg_class(env->arch_env->isa, i);
212                 for(j = 0; j < cls->n_regs; ++j) {
213                         const arch_register_t *reg = arch_register_for_index(cls, j);
214                         if(arch_register_type_is(reg, callee_save))
215                                 obstack_ptr_grow(&env->obst, reg);
216                 }
217         }
218         obstack_ptr_grow(&env->obst, NULL);
219         env->callee_save = obstack_finish(&env->obst);
220
221         return env;
222 }
223
224 static void be_done_env(be_main_env_t *env)
225 {
226         env->arch_env->isa->impl->done(env->arch_env->isa);
227         obstack_free(&env->obst, NULL);
228 }
229
230 static void dump(int mask, ir_graph *irg, const char *suffix,
231                                  void (*dumper)(ir_graph *, const char *))
232 {
233         if(dump_flags & mask)
234                 dumper(irg, suffix);
235 }
236
237 static void prepare_graph(be_irg_t *birg)
238 {
239         ir_graph *irg = birg->irg;
240
241         /* Normalize proj nodes. */
242         normalize_proj_nodes(irg);
243
244         /* Remove critical edges */
245         remove_critical_cf_edges(irg);
246
247         /* Compute the dominance information. */
248         free_dom(irg);
249         compute_doms(irg);
250
251         /* Ensure, that the ir_edges are computed. */
252         edges_activate(irg);
253
254         /* Compute loop nesting information (for weighting copies) */
255         if (get_irg_loopinfo_state(irg) != (loopinfo_valid & loopinfo_cf_consistent))
256                 construct_cf_backedges(irg);
257
258         /* check, if the dominance property is fulfilled. */
259         be_check_dominance(irg);
260
261         /* compute the dominance frontiers. */
262         birg->dom_front = be_compute_dominance_frontiers(irg);
263 }
264
265 static void be_main_loop(FILE *file_handle)
266 {
267         int i, n;
268         arch_isa_t *isa;
269         be_main_env_t env;
270
271         be_init_env(&env);
272
273         isa = arch_env_get_isa(env.arch_env);
274
275         /* For all graphs */
276         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
277                 ir_graph *irg = get_irp_irg(i);
278                 const arch_code_generator_if_t *cg_if;
279                 be_irg_t birg;
280
281                 birg.irg      = irg;
282                 birg.main_env = &env;
283
284                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
285                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
286
287                 /* set the current graph (this is important for several firm functions) */
288                 current_ir_graph = birg.irg;
289
290                 /* Get the code generator interface. */
291                 cg_if = isa->impl->get_code_generator_if(isa);
292
293                 /* get a code generator for this graph. */
294                 birg.cg = cg_if->init(file_handle, birg.irg, env.arch_env);
295
296                 /* create the code generator and generate code. */
297                 prepare_graph(&birg);
298
299                 /* implement the ABI conventions. */
300                 // birg.abi = be_abi_introduce(&birg);
301
302                 arch_code_generator_prepare_graph(birg.cg);
303
304                 /*
305                  * Since the code generator made a lot of new nodes and skipped
306                  * a lot of old ones, we should do dead node elim here.
307                  * Note that this requires disabling the edges here.
308                  */
309                 edges_deactivate(irg);
310                 dead_node_elimination(irg);
311                 edges_activate(irg);
312
313                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
314
315                 /* Schedule the graphs. */
316                 arch_code_generator_before_sched(birg.cg);
317                 list_sched(isa, irg);
318
319                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
320
321                 /* connect all stack modifying nodes together (see beabi.c) */
322                 // be_abi_fix_stack(birg.abi);
323
324                 /* Verify the schedule */
325                 sched_verify_irg(irg);
326
327                 /* Do register allocation */
328                 arch_code_generator_before_ra(birg.cg);
329                 ra->allocate(&birg);
330
331                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
332
333                 arch_code_generator_done(birg.cg);
334                 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
335
336                 be_free_dominance_frontiers(birg.dom_front);
337         }
338
339         be_done_env(&env);
340 }
341
342 void be_main(FILE *file_handle)
343 {
344         /* never build code for pseudo irgs */
345         set_visit_pseudo_irgs(0);
346
347         be_node_init();
348         be_main_loop(file_handle);
349 }