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