3 * @author Sebastian Hack
22 #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"
42 #include "beasm_dump_globals.h"
43 #include "beasm_asm_gnu.h"
67 static be_main_env_t *be_init_env(be_main_env_t *env)
69 const arch_isa_if_t *isa = &firm_isa;
71 obstack_init(&env->obst);
72 env->dbg = firm_dbg_register("be.main");
73 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
75 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
76 arch_env_init(env->arch_env, isa);
77 env->arch_env->isa->init();
79 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
80 be_node_factory_init(env->node_factory, isa);
82 arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
83 arch_env_add_irn_handler(env->arch_env,
84 be_node_get_irn_handler(env->node_factory));
89 static be_main_session_env_t *
90 be_init_session_env(be_main_session_env_t *env,
91 be_main_env_t *main_env, ir_graph *irg)
93 env->main_env = main_env;
99 static void prepare_graph(be_main_session_env_t *s)
101 /* set the current graph (this is important for several firm functions) */
102 current_ir_graph = s->irg;
104 /* Let the isa prepare the graph. */
105 s->main_env->arch_env->isa->prepare_graph(s->irg);
107 /* Normalize proj nodes. */
108 normalize_proj_nodes(s->irg);
110 /* Remove critical edges */
111 remove_critical_cf_edges(s->irg);
113 /* Compute the dominance information. */
114 free_dom_and_peace(s->irg);
115 compute_doms(s->irg);
117 /* Compute the dominance frontiers */
118 s->dom_front = be_compute_dominance_frontiers(s->irg);
120 /* Ensure, that the ir_edges are computed. */
121 edges_activate(s->irg);
123 /* Compute loop nesting information (for weighting copies) */
124 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
125 construct_cf_backedges(s->irg);
127 be_check_dominance(s->irg);
130 static void be_main_loop(void)
134 const arch_isa_if_t *isa;
137 isa = arch_env_get_isa(env.arch_env);
140 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
142 ir_graph *irg = get_irp_irg(i);
143 be_main_session_env_t session;
145 DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
147 dump_ir_block_graph(irg, "-begin");
150 /* Init the session. */
151 be_init_session_env(&session, &env, irg);
153 /* Compute some analyses and prepare the graph for backend use. */
154 prepare_graph(&session);
156 dump_dominator_information(true);
157 dump_ir_block_graph(irg, "-prepared");
158 dump_dominator_information(false);
161 /* Schedule the graphs. */
162 list_sched(irg, trivial_selector);
164 dump_ir_block_graph_sched(irg, "-sched");
167 /* Verify the schedule */
168 sched_verify_irg(irg);
170 /* Build liveness information */
173 /* Perform the following for each register class. */
174 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
175 be_chordal_env_t *chordal_env;
176 const arch_register_class_t *cls = isa->get_reg_class(j);
177 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
180 be_spill_ilp(&session, cls);
182 dump_ir_block_graph_sched(session.irg, "-spill");
186 be_check_pressure(&session, cls);
192 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
193 if((f = fopen(buf, "wt")) != NULL) {
194 be_liveness_dump(session.irg, f);
201 chordal_env = be_ra_chordal(&session, cls);
202 #ifdef DUMP_ALLOCATED
203 dump_allocated_irg(env.arch_env, irg, "");
206 /* copy minimization */
207 copystat_collect_cls(chordal_env);
208 be_copy_opt(chordal_env);
210 dump_allocated_irg(env.arch_env, irg, "-copymin");
213 /* ssa destruction */
214 be_ssa_destruction(chordal_env);
215 be_ssa_destruction_check(chordal_env);
216 be_ra_chordal_check(chordal_env);
218 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
221 be_ra_chordal_done(chordal_env);
222 be_numbering_done(irg);
225 dump_ir_block_graph_sched(session.irg, "-end");
231 void be_main(int argc, const char *argv[])
233 assembler_t *gnu_assembler;
234 FILE *asm_output_file;
239 gnu_assembler = gnuasm_create_assembler();
240 asm_output_file = fopen("asm_output.asm", "w");
242 asm_dump_globals(gnu_assembler);
243 gnuasm_dump(gnu_assembler, asm_output_file);
244 gnuasm_delete_assembler(gnu_assembler);
245 fclose(asm_output_file);