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