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"
57 extern const arch_isa_if_t firm_isa;
58 extern const arch_isa_if_t ia32_isa;
62 const arch_isa_if_t *isa;
63 } isa_name_arch_map[] = {
64 { "firm", &firm_isa },
68 #define N_ISAS (sizeof(isa_name_arch_map) / sizeof(isa_name_arch_map[0]))
70 const arch_isa_if_t *get_backend_isa_if(const char *name) {
73 for (i = 0; i < N_ISAS; i++) {
74 if (!strcmp(isa_name_arch_map[i].name, name))
75 return isa_name_arch_map[i].isa;
78 fprintf(stderr, "Unsupported ISA '%s'\n", name);
94 static be_main_env_t *be_init_env(be_main_env_t *env, const arch_isa_if_t *isa)
96 obstack_init(&env->obst);
97 env->dbg = firm_dbg_register("be.main");
98 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
100 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
101 arch_env_init(env->arch_env, isa);
102 env->arch_env->isa->init();
104 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
105 be_node_factory_init(env->node_factory, isa);
107 /* Register the irn handler of the architecture */
108 if (isa->irn_handler)
109 arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
112 * Register the node handler of the back end infrastructure.
113 * This irn handler takes care of the platform independent
114 * spill, reload and perm nodes.
116 arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
121 static be_main_session_env_t *
122 be_init_session_env(be_main_session_env_t *env,
123 be_main_env_t *main_env, ir_graph *irg)
125 env->main_env = main_env;
131 static void prepare_graph(be_main_session_env_t *s)
133 /* set the current graph (this is important for several firm functions) */
134 current_ir_graph = s->irg;
136 /* Let the isa prepare the graph. */
137 s->main_env->arch_env->isa->prepare_graph(s->irg);
139 /* Normalize proj nodes. */
140 normalize_proj_nodes(s->irg);
142 /* Remove critical edges */
143 remove_critical_cf_edges(s->irg);
145 /* Compute the dominance information. */
146 free_dom_and_peace(s->irg);
147 compute_doms(s->irg);
149 /* Compute the dominance frontiers */
150 s->dom_front = be_compute_dominance_frontiers(s->irg);
152 /* Ensure, that the ir_edges are computed. */
153 edges_activate(s->irg);
155 /* Compute loop nesting information (for weighting copies) */
156 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
157 construct_cf_backedges(s->irg);
159 be_check_dominance(s->irg);
162 static void be_main_loop(const arch_isa_if_t *isa)
167 be_init_env(&env, isa);
170 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
171 ir_graph *irg = get_irp_irg(i);
172 be_main_session_env_t session;
174 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
176 dump_ir_block_graph(irg, "-begin");
179 /* Init the session. */
180 be_init_session_env(&session, &env, irg);
182 /* Compute some analyses and prepare the graph for backend use. */
183 prepare_graph(&session);
186 dump_dominator_information(1);
187 dump_ir_block_graph(irg, "-prepared");
188 dump_dominator_information(0);
191 /* Schedule the graphs. */
192 list_sched(irg, trivial_selector);
194 // TODO: add architecture dependent schedule magic
195 // be_sched_imm(irg);
198 dump_ir_block_graph_sched(irg, "-sched");
201 /* Verify the schedule */
202 sched_verify_irg(irg);
204 /* Build liveness information */
207 /* Perform the following for each register class. */
208 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
209 be_chordal_env_t *chordal_env;
210 const arch_register_class_t *cls = isa->get_reg_class(j);
211 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
214 //be_spill_ilp(&session, cls);
215 be_spill_belady(&session, cls);
217 dump_ir_block_graph_sched(session.irg, "-spill");
221 be_check_pressure(&session, cls);
227 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
228 if((f = fopen(buf, "wt")) != NULL) {
229 be_liveness_dump(session.irg, f);
236 chordal_env = be_ra_chordal(&session, cls);
237 #ifdef DUMP_ALLOCATED
238 dump_allocated_irg(env.arch_env, irg, "");
241 /* copy minimization */
242 copystat_collect_cls(chordal_env);
243 be_copy_opt(chordal_env);
245 dump_allocated_irg(env.arch_env, irg, "-copymin");
248 /* ssa destruction */
249 be_ssa_destruction(chordal_env);
250 be_ssa_destruction_check(chordal_env);
251 be_ra_chordal_check(chordal_env);
253 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
256 be_ra_chordal_done(chordal_env);
257 be_numbering_done(irg);
260 dump_ir_block_graph_sched(session.irg, "-end");
267 * Backend main driver. Expects the following arguments:
269 * argv[0] = backend isa name
270 * argv[1] = output file name
272 void be_main(int argc, const char *argv[])
274 FILE *asm_output_file;
275 const arch_isa_if_t *isa;
277 /* get requested isa */
278 isa = get_backend_isa_if(argv[0]);
282 /* generate code if supported by isa */
284 asm_output_file = fopen(argv[1], "w");
285 isa->codegen(asm_output_file);
286 fclose(asm_output_file);