fixed wrong register request index
[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 #include "obst.h"
13 #include "bitset.h"
14
15 #include "irprog.h"
16 #include "irgopt.h"
17 #include "irgraph.h"
18 #include "irdump.h"
19 #include "phiclass.h"
20 #include "irdom_t.h"
21 #include "iredges_t.h"
22 #include "irloop_t.h"
23
24 #include "be_t.h"
25 #include "bechordal_t.h"
26 #include "benumb_t.h"
27 #include "besched_t.h"
28 #include "belistsched.h"
29 #include "belive_t.h"
30 #include "beutil.h"
31 #include "bechordal.h"
32 #include "bearch.h"
33 #include "becopyoptmain.h"
34 #include "becopystat.h"
35 #include "bessadestr.h"
36 #include "benode_t.h"
37 #include "beirgmod.h"
38 #include "bespillilp.h"
39 #include "bespillbelady.h"
40
41 // #include "beasm_dump_globals.h"
42 // #include "beasm_asm_gnu.h"
43
44 // #include "bearch_firm.h"
45
46 #include "firm2arch.h"
47
48 #undef DUMP_BEGIN
49 #define DUMP_PREPARED
50 #define DUMP_SCHED
51 #define DUMP_SPILL
52 #undef DUMP_ALLOCATED
53 #undef DUMP_COPYMIN
54 #undef DUMP_SSADESTR
55 #undef DUMP_END
56
57 #define N_PHASES 256
58
59 extern const arch_isa_if_t firm_isa;
60 extern const arch_isa_if_t ia32_isa;
61
62 const struct {
63   const char *name;
64   const arch_isa_if_t *isa;
65 } isa_name_arch_map[] = {
66   { "firm", &firm_isa },
67   { "ia32", &ia32_isa }
68 };
69
70 #define N_ISAS (sizeof(isa_name_arch_map) / sizeof(isa_name_arch_map[0]))
71
72 const arch_isa_if_t *get_backend_isa_if(const char *name) {
73   int i;
74
75   for (i = 0; i < N_ISAS; i++) {
76     if (!strcmp(isa_name_arch_map[i].name, name))
77       return isa_name_arch_map[i].isa;
78   }
79
80   fprintf(stderr, "Unsupported ISA '%s'\n", name);
81   assert(0);
82   return NULL;
83 }
84
85 void be_init(void)
86 {
87   be_sched_init();
88   be_liveness_init();
89   be_numbering_init();
90   be_ra_chordal_init();
91   be_copy_opt_init();
92   copystat_init();
93   phi_class_init();
94 }
95
96 static be_main_env_t *be_init_env(be_main_env_t *env, const arch_isa_if_t *isa)
97 {
98   obstack_init(&env->obst);
99   env->dbg = firm_dbg_register("be.main");
100   firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
101
102   env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
103   arch_env_init(env->arch_env, isa);
104   env->arch_env->isa->init();
105
106   env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
107   be_node_factory_init(env->node_factory, isa);
108
109   /* Register the irn handler of the architecture */
110   if (isa->irn_handler)
111     arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
112
113   /*
114    * Register the node handler of the back end infrastructure.
115    * This irn handler takes care of the platform independent
116    * spill, reload and perm nodes.
117    */
118   arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
119
120   return env;
121 }
122
123 static be_main_session_env_t *
124 be_init_session_env(be_main_session_env_t *env,
125     be_main_env_t *main_env, ir_graph *irg)
126 {
127   env->main_env = main_env;
128   env->irg = irg;
129
130   return env;
131 }
132
133 static void prepare_graph(be_main_session_env_t *s)
134 {
135   /* set the current graph (this is important for several firm functions) */
136   current_ir_graph = s->irg;
137
138   /* Let the isa prepare the graph. */
139   s->main_env->arch_env->isa->prepare_graph(s->irg);
140
141   /* Normalize proj nodes. */
142   normalize_proj_nodes(s->irg);
143
144   /* Remove critical edges */
145   remove_critical_cf_edges(s->irg);
146
147   /* Compute the dominance information. */
148   free_dom_and_peace(s->irg);
149   compute_doms(s->irg);
150
151   /* Compute the dominance frontiers */
152   s->dom_front = be_compute_dominance_frontiers(s->irg);
153
154   /* Ensure, that the ir_edges are computed. */
155   edges_activate(s->irg);
156
157   /* Compute loop nesting information (for weighting copies) */
158   if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
159     construct_cf_backedges(s->irg);
160
161   be_check_dominance(s->irg);
162 }
163
164 static void be_main_loop(const arch_isa_if_t *isa)
165 {
166         int i, j, n, m;
167         be_main_env_t env;
168
169         be_init_env(&env, isa);
170
171         /* For all graphs */
172         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
173                 ir_graph *irg = get_irp_irg(i);
174                 be_main_session_env_t session;
175
176                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
177 #ifdef DUMP_BEGIN
178                 dump_ir_block_graph(irg, "-begin");
179 #endif
180
181                 /* Init the session. */
182                 be_init_session_env(&session, &env, irg);
183
184                 /* Compute some analyses and prepare the graph for backend use. */
185                 prepare_graph(&session);
186
187 #ifdef DUMP_PREPARED
188                 dump_dominator_information(1);
189                 dump_ir_block_graph(irg, "-prepared");
190                 dump_dominator_information(0);
191 #endif
192
193                 /* Schedule the graphs. */
194                 list_sched(irg, trivial_selector);
195
196 // TODO: add architecture dependent schedule magic
197 //              be_sched_imm(irg);
198
199 #ifdef DUMP_SCHED
200                 dump_ir_block_graph_sched(irg, "-sched");
201 #endif
202
203                 /* Verify the schedule */
204                 sched_verify_irg(irg);
205
206                 /* Build liveness information */
207                 be_liveness(irg);
208
209                 /* Perform the following for each register class. */
210                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
211                         be_chordal_env_t *chordal_env;
212                         const arch_register_class_t *cls = isa->get_reg_class(j);
213                         DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
214
215                         /* spilling */
216                         //be_spill_ilp(&session, cls);
217                         be_spill_belady(&session, cls);
218 #ifdef DUMP_SPILL
219                         dump_ir_block_graph_sched(session.irg, "-spill");
220 #endif
221                         be_liveness(irg);
222                         be_numbering(irg);
223                         be_check_pressure(&session, cls);
224
225 #if 0
226                         {
227                                 FILE *f;
228                                 char buf[128];
229                                 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
230                                 if((f = fopen(buf, "wt")) != NULL) {
231                                         be_liveness_dump(session.irg, f);
232                                         fclose(f);
233                                 }
234                         }
235 #endif
236
237                         /* allocation */
238                         chordal_env = be_ra_chordal(&session, cls);
239 #ifdef DUMP_ALLOCATED
240                         dump_allocated_irg(env.arch_env, irg, "");
241 #endif
242
243                         /* copy minimization */
244                         copystat_collect_cls(chordal_env);
245                         be_copy_opt(chordal_env);
246 #ifdef DUMP_COPYMIN
247                         dump_allocated_irg(env.arch_env, irg, "-copymin");
248 #endif
249
250                         /* ssa destruction */
251                         be_ssa_destruction(chordal_env);
252                         be_ssa_destruction_check(chordal_env);
253                         be_ra_chordal_check(chordal_env);
254 #ifdef DUMP_SSADESTR
255                         dump_allocated_irg(env.arch_env, irg, "-ssadestr");
256 #endif
257
258                         be_ra_chordal_done(chordal_env);
259                         be_numbering_done(irg);
260                 }
261 #ifdef DUMP_END
262                 dump_ir_block_graph_sched(session.irg, "-end");
263 #endif
264                 copystat_dump(irg);
265         }
266 }
267
268 /**
269  * Backend main driver. Expects the following arguments:
270  * argc = 2
271  * argv[0] = backend isa name
272  * argv[1] = output file name
273  */
274 void be_main(int argc, const char *argv[])
275 {
276   FILE *asm_output_file;
277   const arch_isa_if_t *isa;
278
279   /* get requested isa */
280   isa = get_backend_isa_if(argv[0]);
281
282   be_main_loop(isa);
283
284   /* generate code if supported by isa */
285   if (isa->codegen) {
286     asm_output_file = fopen(argv[1], "w");
287     isa->codegen(asm_output_file);
288     fclose(asm_output_file);
289   }
290 }