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