:wq
[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
53
54 #define DUMP_INITIAL            (1 << 0)
55 #define DUMP_SCHED                      (1 << 1)
56 #define DUMP_PREPARED           (1 << 2)
57 #define DUMP_RA                         (1 << 3)
58 #define DUMP_FINAL                      (1 << 4)
59
60 /* options visible for anyone */
61 be_options_t be_options = {
62         /* ilp server */
63         "i44pc52.info.uni-karlsruhe.de",
64
65         /* ilp solver */
66         "cplex"
67 };
68
69 /* dump flags */
70 static unsigned dump_flags = DUMP_INITIAL | DUMP_SCHED | DUMP_PREPARED | DUMP_RA | DUMP_FINAL;
71
72 /* register allocator to use. */
73 static const be_ra_t *ra = &be_ra_chordal_allocator;
74
75 /* back end instruction set architecture to use */
76 static const arch_isa_if_t *isa_if = &ia32_isa_if;
77
78 #ifdef WITH_LIBCORE
79
80 static lc_opt_entry_t *be_grp_root = NULL;
81
82 /* possible dumping options */
83 static const lc_opt_enum_mask_items_t dump_items[] = {
84         { "none",       0 },
85         { "initial",    DUMP_INITIAL },
86         { "sched",      DUMP_SCHED  },
87         { "prepared",   DUMP_PREPARED },
88         { "regalloc",   DUMP_RA },
89         { "final",      DUMP_FINAL },
90         { "all",        2 * DUMP_FINAL - 1 },
91         { NULL,         0 }
92 };
93
94 /* register allocators */
95 static const lc_opt_enum_const_ptr_items_t ra_items[] = {
96         { "chordal", &be_ra_chordal_allocator },
97         { "external", &be_ra_external_allocator },
98         { NULL,      NULL }
99 };
100
101 /* instruction set architectures. */
102 static const lc_opt_enum_const_ptr_items_t isa_items[] = {
103         { "firm",    &firm_isa },
104         { "ia32",    &ia32_isa_if },
105         { NULL,      NULL }
106 };
107
108 static lc_opt_enum_mask_var_t dump_var = {
109         &dump_flags, dump_items
110 };
111
112 static lc_opt_enum_const_ptr_var_t ra_var = {
113         (const void **) &ra, ra_items
114 };
115
116 static lc_opt_enum_const_ptr_var_t isa_var = {
117         (const void **) &isa_if, isa_items
118 };
119
120 static const lc_opt_table_entry_t be_main_options[] = {
121         LC_OPT_ENT_ENUM_MASK("dump", "dump irg on several occasions", &dump_var),
122         LC_OPT_ENT_ENUM_PTR("ra", "register allocator", &ra_var),
123         LC_OPT_ENT_ENUM_PTR("isa", "the instruction set architecture", &isa_var),
124
125         LC_OPT_ENT_STR ("ilp.server", "the ilp server name", be_options.ilp_server, sizeof(be_options.ilp_server)),
126         LC_OPT_ENT_STR ("ilp.solver", "the ilp solver name", be_options.ilp_solver, sizeof(be_options.ilp_solver)),
127         { NULL }
128 };
129
130 #endif /* WITH_LIBCORE */
131
132 void be_opt_register(void)
133 {
134 #ifdef WITH_LIBCORE
135         int i;
136         lc_opt_entry_t *be_grp_ra;
137
138         be_grp_root = lc_opt_get_grp(firm_opt_get_root(), "be");
139         be_grp_ra   = lc_opt_get_grp(be_grp_root, "ra");
140
141         lc_opt_add_table(be_grp_root, be_main_options);
142
143         /* register allocator options */
144         for(i = 0; ra_items[i].name != NULL; ++i) {
145                 const be_ra_t *ra = ra_items[i].value;
146                 ra->register_options(be_grp_ra);
147         }
148
149         /* register isa options */
150         for(i = 0; isa_items[i].name != NULL; ++i) {
151                 const arch_isa_if_t *isa = isa_items[i].value;
152                 isa->register_options(be_grp_root);
153         }
154 #endif /* WITH_LIBCORE */
155 }
156
157
158 void be_init(void)
159 {
160         be_opt_register();
161
162         be_sched_init();
163         be_liveness_init();
164         be_numbering_init();
165         be_copy_opt_init();
166         copystat_init();
167         phi_class_init();
168 }
169
170 static be_main_env_t *be_init_env(be_main_env_t *env)
171 {
172   obstack_init(&env->obst);
173   env->dbg = firm_dbg_register("be.main");
174
175   env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
176   arch_env_init(env->arch_env, isa_if);
177
178   /* Register the irn handler of the architecture */
179   if (arch_isa_get_irn_handler(env->arch_env->isa))
180                 arch_env_add_irn_handler(env->arch_env, arch_isa_get_irn_handler(env->arch_env->isa));
181
182   /*
183    * Register the node handler of the back end infrastructure.
184    * This irn handler takes care of the platform independent
185    * spill, reload and perm nodes.
186    */
187   arch_env_add_irn_handler(env->arch_env, &be_node_irn_handler);
188
189   return env;
190 }
191
192 static void be_done_env(be_main_env_t *env)
193 {
194         env->arch_env->isa->impl->done(env->arch_env->isa);
195         obstack_free(&env->obst, NULL);
196 }
197
198 static void dump(int mask, ir_graph *irg, const char *suffix,
199                                  void (*dumper)(ir_graph *, const char *))
200 {
201         if(dump_flags & mask)
202                 dumper(irg, suffix);
203 }
204
205 static void prepare_graph(be_main_env_t *s, ir_graph *irg)
206 {
207         /* Normalize proj nodes. */
208         normalize_proj_nodes(irg);
209
210         /* Remove critical edges */
211         remove_critical_cf_edges(irg);
212
213         /* Compute the dominance information. */
214         free_dom(irg);
215         compute_doms(irg);
216
217         /* Ensure, that the ir_edges are computed. */
218         edges_activate(irg);
219
220         /* Compute loop nesting information (for weighting copies) */
221         if (get_irg_loopinfo_state(irg) != (loopinfo_valid & loopinfo_cf_consistent))
222                 construct_cf_backedges(irg);
223
224         be_check_dominance(irg);
225 }
226
227 static void be_main_loop(FILE *file_handle)
228 {
229         int i, n;
230         arch_isa_t *isa;
231         be_main_env_t env;
232
233         be_init_env(&env);
234
235         isa = arch_env_get_isa(env.arch_env);
236
237         /* For all graphs */
238         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
239                 ir_graph *irg = get_irp_irg(i);
240
241                 arch_code_generator_t *cg;
242                 const arch_code_generator_if_t *cg_if;
243
244                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
245                 dump(DUMP_INITIAL, irg, "-begin", dump_ir_block_graph);
246
247                 /* set the current graph (this is important for several firm functions) */
248                 current_ir_graph = irg;
249
250                 /* Get the code generator interface. */
251                 cg_if = isa->impl->get_code_generator(isa);
252
253                 /* get a code generator for this graph. */
254                 cg = cg_if->init(file_handle, irg, env.arch_env);
255
256                 /* create the code generator and generate code. */
257                 prepare_graph(&env, irg);
258                 arch_code_generator_prepare_graph(cg);
259
260                 edges_deactivate(irg);
261                 dead_node_elimination(irg);
262                 edges_activate(irg);
263
264                 dump(DUMP_PREPARED, irg, "-prepared", dump_ir_block_graph);
265
266                 /* Schedule the graphs. */
267                 arch_code_generator_before_sched(cg);
268                 list_sched(isa, irg);
269
270                 dump(DUMP_SCHED, irg, "-sched", dump_ir_block_graph_sched);
271
272                 /* Verify the schedule */
273                 sched_verify_irg(irg);
274
275                 /* Do register allocation */
276                 arch_code_generator_before_ra(cg);
277                 ra->allocate(&env, irg);
278
279                 dump(DUMP_RA, irg, "-ra", dump_ir_block_graph_sched);
280
281                 arch_code_generator_done(cg);
282                 dump(DUMP_FINAL, irg, "-end", dump_ir_block_graph_sched);
283         }
284
285         be_done_env(&env);
286 }
287
288 void be_main(FILE *file_handle)
289 {
290         be_node_init();
291         be_main_loop(file_handle);
292 }