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