3 * @author Sebastian Hack
21 #include "iredges_t.h"
26 #include "bechordal_t.h"
28 #include "besched_t.h"
29 #include "belistsched.h"
32 #include "bechordal.h"
34 #include "becopyoptmain.h"
35 #include "becopystat.h"
36 #include "bessadestr.h"
37 #include "bearch_firm.h"
40 #include "bespillilp.h"
41 #include "bespillbelady.h"
43 #include "beasm_dump_globals.h"
44 #include "beasm_asm_gnu.h"
46 #include "firm2arch.h"
50 #define DUMP_TRANSFORM
73 static be_main_env_t *be_init_env(be_main_env_t *env)
75 const arch_isa_if_t *isa = &firm_isa;
77 obstack_init(&env->obst);
78 env->dbg = firm_dbg_register("be.main");
79 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
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();
85 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
86 be_node_factory_init(env->node_factory, isa);
88 /* Register the irn handler of the architecture */
90 arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
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.
97 arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
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)
106 env->main_env = main_env;
112 static void prepare_graph(be_main_session_env_t *s)
114 /* set the current graph (this is important for several firm functions) */
115 current_ir_graph = s->irg;
117 /* Let the isa prepare the graph. */
118 s->main_env->arch_env->isa->prepare_graph(s->irg);
120 /* Normalize proj nodes. */
121 normalize_proj_nodes(s->irg);
123 /* Remove critical edges */
124 remove_critical_cf_edges(s->irg);
126 /* Compute the dominance information. */
127 free_dom_and_peace(s->irg);
128 compute_doms(s->irg);
130 /* Compute the dominance frontiers */
131 s->dom_front = be_compute_dominance_frontiers(s->irg);
133 /* Ensure, that the ir_edges are computed. */
134 edges_activate(s->irg);
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);
140 be_check_dominance(s->irg);
143 static void be_main_loop(void)
147 const arch_isa_if_t *isa;
150 isa = arch_env_get_isa(env.arch_env);
152 /* create required irop's */
153 create_bearch_asm_opcodes();
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;
160 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
162 dump_ir_block_graph(irg, "-begin");
165 /* Init the session. */
166 be_init_session_env(&session, &env, irg);
168 /* Compute some analyses and prepare the graph for backend use. */
169 prepare_graph(&session);
173 #ifdef DUMP_TRANSFORM
174 dump_ir_block_graph(irg, "-transformed");
179 dump_dominator_information(true);
180 dump_ir_block_graph(irg, "-prepared");
181 dump_dominator_information(false);
184 /* Schedule the graphs. */
185 list_sched(irg, trivial_selector);
189 dump_ir_block_graph_sched(irg, "-sched");
192 /* Verify the schedule */
193 sched_verify_irg(irg);
195 /* Build liveness information */
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;
203 const arch_register_class_t *cls = isa->get_reg_class(j);
204 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
207 //be_spill_ilp(&session, cls);
208 be_spill_belady(&session, cls);
210 dump_ir_block_graph_sched(session.irg, "-spill");
214 be_check_pressure(&session, cls);
217 chordal_env = be_ra_chordal(&session, cls);
218 #ifdef DUMP_ALLOCATED
219 dump_allocated_irg(env.arch_env, irg, "");
222 /* Build the interference graph. */
223 ifg = be_ifg_std_new(chordal_env);
226 /* copy minimization */
227 copystat_collect_cls(chordal_env);
228 be_copy_opt(chordal_env);
230 dump_allocated_irg(env.arch_env, irg, "-copymin");
233 /* ssa destruction */
234 be_ssa_destruction(chordal_env);
235 be_ssa_destruction_check(chordal_env);
236 be_ra_chordal_check(chordal_env);
238 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
243 be_ra_chordal_done(chordal_env);
244 be_numbering_done(irg);
247 dump_ir_block_graph_sched(session.irg, "-end");
253 void be_main(int argc, const char *argv[])
255 FILE *asm_output_file;
259 asm_output_file = fopen(argv[0], "w");
260 firmbe_gen_code(asm_output_file);
261 fclose(asm_output_file);