3 * @author Sebastian Hack
21 #include "iredges_t.h"
25 #include "bechordal_t.h"
27 #include "besched_t.h"
28 #include "belistsched.h"
31 #include "bechordal.h"
33 #include "becopyoptmain.h"
34 #include "becopystat.h"
35 #include "bessadestr.h"
38 #include "bespillilp.h"
39 #include "bespillbelady.h"
41 // #include "beasm_dump_globals.h"
42 // #include "beasm_asm_gnu.h"
44 // #include "bearch_firm.h"
46 #include "firm2arch.h"
59 extern const arch_isa_if_t firm_isa;
60 extern const arch_isa_if_t ia32_isa;
64 const arch_isa_if_t *isa;
65 } isa_name_arch_map[] = {
66 { "firm", &firm_isa },
70 #define N_ISAS (sizeof(isa_name_arch_map) / sizeof(isa_name_arch_map[0]))
72 const arch_isa_if_t *get_backend_isa_if(const char *name) {
75 for (i = 0; i < N_ISAS; i++) {
76 if (!strcmp(isa_name_arch_map[i].name, name))
77 return isa_name_arch_map[i].isa;
80 fprintf(stderr, "Unsupported ISA '%s'\n", name);
96 static be_main_env_t *be_init_env(be_main_env_t *env, const arch_isa_if_t *isa)
98 obstack_init(&env->obst);
99 env->dbg = firm_dbg_register("be.main");
100 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
102 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
103 arch_env_init(env->arch_env, isa);
104 env->arch_env->isa->init();
106 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
107 be_node_factory_init(env->node_factory, isa);
109 /* Register the irn handler of the architecture */
110 if (isa->irn_handler)
111 arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
114 * Register the node handler of the back end infrastructure.
115 * This irn handler takes care of the platform independent
116 * spill, reload and perm nodes.
118 arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
123 static be_main_session_env_t *
124 be_init_session_env(be_main_session_env_t *env,
125 be_main_env_t *main_env, ir_graph *irg)
127 env->main_env = main_env;
133 static void prepare_graph(be_main_session_env_t *s)
135 /* set the current graph (this is important for several firm functions) */
136 current_ir_graph = s->irg;
138 /* Let the isa prepare the graph. */
139 s->main_env->arch_env->isa->prepare_graph(s->irg);
141 /* Normalize proj nodes. */
142 normalize_proj_nodes(s->irg);
144 /* Remove critical edges */
145 remove_critical_cf_edges(s->irg);
147 /* Compute the dominance information. */
148 free_dom_and_peace(s->irg);
149 compute_doms(s->irg);
151 /* Compute the dominance frontiers */
152 s->dom_front = be_compute_dominance_frontiers(s->irg);
154 /* Ensure, that the ir_edges are computed. */
155 edges_activate(s->irg);
157 /* Compute loop nesting information (for weighting copies) */
158 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
159 construct_cf_backedges(s->irg);
161 be_check_dominance(s->irg);
164 static void be_main_loop(const arch_isa_if_t *isa)
169 be_init_env(&env, isa);
172 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
173 ir_graph *irg = get_irp_irg(i);
174 be_main_session_env_t session;
176 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
178 dump_ir_block_graph(irg, "-begin");
181 /* Init the session. */
182 be_init_session_env(&session, &env, irg);
184 /* Compute some analyses and prepare the graph for backend use. */
185 prepare_graph(&session);
188 dump_dominator_information(1);
189 dump_ir_block_graph(irg, "-prepared");
190 dump_dominator_information(0);
193 /* Schedule the graphs. */
194 list_sched(irg, trivial_selector);
196 // TODO: add architecture dependent schedule magic
197 // be_sched_imm(irg);
200 dump_ir_block_graph_sched(irg, "-sched");
203 /* Verify the schedule */
204 sched_verify_irg(irg);
206 /* Build liveness information */
209 /* Perform the following for each register class. */
210 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
211 be_chordal_env_t *chordal_env;
212 const arch_register_class_t *cls = isa->get_reg_class(j);
213 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
216 //be_spill_ilp(&session, cls);
217 be_spill_belady(&session, cls);
219 dump_ir_block_graph_sched(session.irg, "-spill");
223 be_check_pressure(&session, cls);
229 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
230 if((f = fopen(buf, "wt")) != NULL) {
231 be_liveness_dump(session.irg, f);
238 chordal_env = be_ra_chordal(&session, cls);
239 #ifdef DUMP_ALLOCATED
240 dump_allocated_irg(env.arch_env, irg, "");
243 /* copy minimization */
244 copystat_collect_cls(chordal_env);
245 be_copy_opt(chordal_env);
247 dump_allocated_irg(env.arch_env, irg, "-copymin");
250 /* ssa destruction */
251 be_ssa_destruction(chordal_env);
252 be_ssa_destruction_check(chordal_env);
253 be_ra_chordal_check(chordal_env);
255 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
258 be_ra_chordal_done(chordal_env);
259 be_numbering_done(irg);
262 dump_ir_block_graph_sched(session.irg, "-end");
269 * Backend main driver. Expects the following arguments:
271 * argv[0] = backend isa name
272 * argv[1] = output file name
274 void be_main(int argc, const char *argv[])
276 FILE *asm_output_file;
277 const arch_isa_if_t *isa;
279 /* get requested isa */
280 isa = get_backend_isa_if(argv[0]);
284 /* generate code if supported by isa */
286 asm_output_file = fopen(argv[1], "w");
287 isa->codegen(asm_output_file);
288 fclose(asm_output_file);