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