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