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