Added irn_handler field to arch_isa_t
[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 "bechordal_t.h"
26 #include "benumb_t.h"
27 #include "besched_t.h"
28 #include "belistsched.h"
29 #include "belive_t.h"
30 #include "beutil.h"
31 #include "bechordal.h"
32 #include "bearch.h"
33 #include "becopyoptmain.h"
34 #include "becopystat.h"
35 #include "bessadestr.h"
36 #include "bearch_firm.h"
37 #include "benode_t.h"
38 #include "beirgmod.h"
39 #include "bespillilp.h"
40 #include "bespillbelady.h"
41
42 #include "beasm_dump_globals.h"
43 #include "beasm_asm_gnu.h"
44
45 #include "firm2arch.h"
46
47 #undef DUMP_BEGIN
48 #undef DUMP_PREPARED
49 #define DUMP_TRANSFORM
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   /* Register the irn handler of the architecture */
86   if (isa->irn_handler)
87                 arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
88
89   /*
90    * Register the node handler of the back end infrastructure.
91    * This irn handler takes care of the platform independent
92    * spill, reload and perm nodes.
93    */
94   arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
95
96   return env;
97 }
98
99 static be_main_session_env_t *
100 be_init_session_env(be_main_session_env_t *env,
101     be_main_env_t *main_env, ir_graph *irg)
102 {
103   env->main_env = main_env;
104   env->irg = irg;
105
106   return env;
107 }
108
109 static void prepare_graph(be_main_session_env_t *s)
110 {
111         /* set the current graph (this is important for several firm functions) */
112         current_ir_graph = s->irg;
113
114         /* Let the isa prepare the graph. */
115         s->main_env->arch_env->isa->prepare_graph(s->irg);
116
117         /* Normalize proj nodes. */
118         normalize_proj_nodes(s->irg);
119
120         /* Remove critical edges */
121         remove_critical_cf_edges(s->irg);
122
123         /* Compute the dominance information. */
124         free_dom_and_peace(s->irg);
125         compute_doms(s->irg);
126
127         /* Compute the dominance frontiers */
128         s->dom_front = be_compute_dominance_frontiers(s->irg);
129
130         /* Ensure, that the ir_edges are computed. */
131         edges_activate(s->irg);
132
133         /* Compute loop nesting information (for weighting copies) */
134         if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
135                 construct_cf_backedges(s->irg);
136
137         be_check_dominance(s->irg);
138 }
139
140 static void be_main_loop(void)
141 {
142         int i, n;
143         be_main_env_t env;
144         const arch_isa_if_t *isa;
145
146         be_init_env(&env);
147         isa = arch_env_get_isa(env.arch_env);
148
149         /* create required irop's */
150         create_bearch_asm_opcodes();
151
152         /* For all graphs */
153         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
154                 ir_graph *irg = get_irp_irg(i);
155                 be_main_session_env_t session;
156
157                 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
158 #ifdef DUMP_BEGIN
159                 dump_ir_block_graph(irg, "-begin");
160 #endif
161
162                 /* Init the session. */
163                 be_init_session_env(&session, &env, irg);
164
165                 /* Compute some analyses and prepare the graph for backend use. */
166                 prepare_graph(&session);
167
168                 transform_firm(irg);
169
170 #ifdef DUMP_TRANSFORM
171                 dump_ir_block_graph(irg, "-transformed");
172 #endif
173
174
175 #if 0
176 #ifdef DUMP_PREPARED
177                 dump_dominator_information(true);
178                 dump_ir_block_graph(irg, "-prepared");
179                 dump_dominator_information(false);
180 #endif
181
182                 /* Schedule the graphs. */
183                 list_sched(irg, trivial_selector);
184                 be_sched_imm(irg);
185
186 #ifdef DUMP_SCHED
187                 dump_ir_block_graph_sched(irg, "-sched");
188 #endif
189
190                 /* Verify the schedule */
191                 sched_verify_irg(irg);
192
193                 /* Build liveness information */
194                 be_liveness(irg);
195
196                 /* Perform the following for each register class. */
197                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
198                         be_chordal_env_t *chordal_env;
199                         const arch_register_class_t *cls = isa->get_reg_class(j);
200                         DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
201
202                         /* spilling */
203                         //be_spill_ilp(&session, cls);
204                         be_spill_belady(&session, cls);
205 #ifdef DUMP_SPILL
206                         dump_ir_block_graph_sched(session.irg, "-spill");
207 #endif
208                         be_liveness(irg);
209                         be_numbering(irg);
210                         be_check_pressure(&session, cls);
211
212 #if 0
213                         {
214                                 FILE *f;
215                                 char buf[128];
216                                 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
217                                 if((f = fopen(buf, "wt")) != NULL) {
218                                         be_liveness_dump(session.irg, f);
219                                         fclose(f);
220                                 }
221                         }
222 #endif
223
224                         /* allocation */
225                         chordal_env = be_ra_chordal(&session, cls);
226 #ifdef DUMP_ALLOCATED
227                         dump_allocated_irg(env.arch_env, irg, "");
228 #endif
229
230                         /* copy minimization */
231                         copystat_collect_cls(chordal_env);
232                         be_copy_opt(chordal_env);
233 #ifdef DUMP_COPYMIN
234                         dump_allocated_irg(env.arch_env, irg, "-copymin");
235 #endif
236
237                         /* ssa destruction */
238                         be_ssa_destruction(chordal_env);
239                         be_ssa_destruction_check(chordal_env);
240                         be_ra_chordal_check(chordal_env);
241 #ifdef DUMP_SSADESTR
242                         dump_allocated_irg(env.arch_env, irg, "-ssadestr");
243 #endif
244
245                         be_ra_chordal_done(chordal_env);
246                         be_numbering_done(irg);
247                 }
248 #ifdef DUMP_END
249                 dump_ir_block_graph_sched(session.irg, "-end");
250 #endif
251                 copystat_dump(irg);
252 #endif /* if 0 */
253         }
254 }
255
256 void be_main(int argc, const char *argv[])
257 {
258   FILE *asm_output_file;
259
260   be_main_loop();
261
262   asm_output_file = fopen(argv[0], "w");
263   firmbe_gen_code(asm_output_file);
264   fclose(asm_output_file);
265 }