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