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"
36 #include "bearch_firm.h"
39 #include "bespillilp.h"
40 #include "bespillbelady.h"
42 #include "beasm_dump_globals.h"
43 #include "beasm_asm_gnu.h"
45 #include "firm2arch.h"
49 #define DUMP_TRANSFORM
70 static be_main_env_t *be_init_env(be_main_env_t *env)
72 const arch_isa_if_t *isa = &firm_isa;
74 obstack_init(&env->obst);
75 env->dbg = firm_dbg_register("be.main");
76 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
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();
82 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
83 be_node_factory_init(env->node_factory, isa);
85 /* Register the irn handler of the architecture */
87 arch_env_add_irn_handler(env->arch_env, isa->irn_handler);
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.
94 arch_env_add_irn_handler(env->arch_env, be_node_get_irn_handler(env->node_factory));
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)
103 env->main_env = main_env;
109 static void prepare_graph(be_main_session_env_t *s)
111 /* set the current graph (this is important for several firm functions) */
112 current_ir_graph = s->irg;
114 /* Let the isa prepare the graph. */
115 s->main_env->arch_env->isa->prepare_graph(s->irg);
117 /* Normalize proj nodes. */
118 normalize_proj_nodes(s->irg);
120 /* Remove critical edges */
121 remove_critical_cf_edges(s->irg);
123 /* Compute the dominance information. */
124 free_dom_and_peace(s->irg);
125 compute_doms(s->irg);
127 /* Compute the dominance frontiers */
128 s->dom_front = be_compute_dominance_frontiers(s->irg);
130 /* Ensure, that the ir_edges are computed. */
131 edges_activate(s->irg);
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);
137 be_check_dominance(s->irg);
140 static void be_main_loop(void)
144 const arch_isa_if_t *isa;
147 isa = arch_env_get_isa(env.arch_env);
149 /* create required irop's */
150 create_bearch_asm_opcodes();
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;
157 DBG((env.dbg, LEVEL_2, "====> IRG: %F\n", irg));
159 dump_ir_block_graph(irg, "-begin");
162 /* Init the session. */
163 be_init_session_env(&session, &env, irg);
165 /* Compute some analyses and prepare the graph for backend use. */
166 prepare_graph(&session);
170 #ifdef DUMP_TRANSFORM
171 dump_ir_block_graph(irg, "-transformed");
177 dump_dominator_information(true);
178 dump_ir_block_graph(irg, "-prepared");
179 dump_dominator_information(false);
182 /* Schedule the graphs. */
183 list_sched(irg, trivial_selector);
187 dump_ir_block_graph_sched(irg, "-sched");
190 /* Verify the schedule */
191 sched_verify_irg(irg);
193 /* Build liveness information */
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));
203 //be_spill_ilp(&session, cls);
204 be_spill_belady(&session, cls);
206 dump_ir_block_graph_sched(session.irg, "-spill");
210 be_check_pressure(&session, cls);
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);
225 chordal_env = be_ra_chordal(&session, cls);
226 #ifdef DUMP_ALLOCATED
227 dump_allocated_irg(env.arch_env, irg, "");
230 /* copy minimization */
231 copystat_collect_cls(chordal_env);
232 be_copy_opt(chordal_env);
234 dump_allocated_irg(env.arch_env, irg, "-copymin");
237 /* ssa destruction */
238 be_ssa_destruction(chordal_env);
239 be_ssa_destruction_check(chordal_env);
240 be_ra_chordal_check(chordal_env);
242 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
245 be_ra_chordal_done(chordal_env);
246 be_numbering_done(irg);
249 dump_ir_block_graph_sched(session.irg, "-end");
256 void be_main(int argc, const char *argv[])
258 FILE *asm_output_file;
262 asm_output_file = fopen(argv[0], "w");
263 firmbe_gen_code(asm_output_file);
264 fclose(asm_output_file);