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"
66 static be_main_env_t *be_init_env(be_main_env_t *env)
68 const arch_isa_if_t *isa = &firm_isa;
70 obstack_init(&env->obst);
71 env->dbg = firm_dbg_register("be.main");
72 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
74 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
75 arch_env_init(env->arch_env, isa);
76 env->arch_env->isa->init();
78 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
79 be_node_factory_init(env->node_factory, isa);
81 arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
82 arch_env_add_irn_handler(env->arch_env,
83 be_node_get_irn_handler(env->node_factory));
88 static be_main_session_env_t *
89 be_init_session_env(be_main_session_env_t *env,
90 be_main_env_t *main_env, ir_graph *irg)
92 env->main_env = main_env;
98 static void prepare_graph(be_main_session_env_t *s)
100 /* set the current graph (this is important for several firm functions) */
101 current_ir_graph = s->irg;
103 /* Let the isa prepare the graph. */
104 s->main_env->arch_env->isa->prepare_graph(s->irg);
106 /* Remove critical edges */
107 remove_critical_cf_edges(s->irg);
109 /* Compute the dominance information. */
110 free_dom_and_peace(s->irg);
111 compute_doms(s->irg);
113 /* Compute the dominance frontiers */
114 s->dom_front = be_compute_dominance_frontiers(s->irg);
116 /* Ensure, that the ir_edges are computed. */
117 edges_activate(s->irg);
119 /* Compute loop nesting information (for weighting copies) */
120 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
121 construct_cf_backedges(s->irg);
124 dump_dominator_information(true);
125 dump_ir_block_graph(s->irg, "-prepared");
126 dump_dominator_information(false);
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 dump_ir_block_graph(irg, "-be-begin");
147 DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
149 /* Init the session. */
150 be_init_session_env(&session, &env, irg);
152 /* Compute some analyses and prepare the graph for backend use. */
153 prepare_graph(&session);
155 /* Schedule the graphs. */
156 list_sched(irg, trivial_selector);
158 dump_ir_block_graph_sched(irg, "-sched");
161 /* Verify the schedule */
162 sched_verify_irg(irg);
164 /* Build liveness information */
167 /* Perform the following for each register class. */
168 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
169 be_chordal_env_t *chordal_env;
170 const arch_register_class_t *cls = isa->get_reg_class(j);
172 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
174 be_spill_ilp(&session, cls);
176 dump_ir_block_graph_sched(session.irg, "-spill");
180 be_check_pressure(&session, cls);
186 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
187 if((f = fopen(buf, "wt")) != NULL) {
188 be_liveness_dump(session.irg, f);
194 chordal_env = be_ra_chordal(&session, cls);
196 #ifdef DUMP_ALLOCATED
197 dump_allocated_irg(env.arch_env, irg, "");
199 copystat_collect_cls(chordal_env);
201 dump_allocated_irg(env.arch_env, irg, "pre");
202 be_copy_opt(chordal_env);
203 dump_allocated_irg(env.arch_env, irg, "post");
205 be_ssa_destruction(chordal_env);
206 be_ssa_destruction_check(chordal_env);
207 be_ra_chordal_check(chordal_env);
209 be_ra_chordal_done(chordal_env);
210 be_numbering_done(irg);
213 dump_ir_block_graph_sched(session.irg, "-post");
219 void be_main(int argc, const char *argv[])
221 assembler_t *gnu_assembler;
222 FILE *asm_output_file;
228 gnu_assembler = gnuasm_create_assembler();
229 asm_output_file = fopen("asm_output.asm", "w");
231 asm_dump_globals(gnu_assembler);
232 gnuasm_dump(gnu_assembler, asm_output_file);
233 gnuasm_delete_assembler(gnu_assembler);
234 fclose(asm_output_file);