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"
41 #include "beasm_dump_globals.h"
42 #include "beasm_asm_gnu.h"
59 static be_main_env_t *be_init_env(be_main_env_t *env)
61 const arch_isa_if_t *isa = &firm_isa;
63 obstack_init(&env->obst);
64 env->dbg = firm_dbg_register("be.main");
66 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
67 arch_env_init(env->arch_env, isa);
68 env->arch_env->isa->init();
70 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
71 be_node_factory_init(env->node_factory, isa);
73 arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
74 arch_env_add_irn_handler(env->arch_env,
75 be_node_get_irn_handler(env->node_factory));
80 static be_main_session_env_t *
81 be_init_session_env(be_main_session_env_t *env,
82 be_main_env_t *main_env, ir_graph *irg)
84 env->main_env = main_env;
90 static void prepare_graph(be_main_session_env_t *s)
93 * Duplicate consts in each block
94 * (This is just for the firm dummy backend)
96 // localize_consts(s->irg);
98 /* Remove critical edges */
99 remove_critical_cf_edges(s->irg);
101 /* Compute the dominance information. */
102 free_dom_and_peace(s->irg);
103 compute_doms(s->irg);
105 /* Compute the dominance frontiers */
106 s->dom_front = be_compute_dominance_frontiers(s->irg);
108 /* Ensure, that the ir_edges are computed. */
109 edges_assure(s->irg);
111 /* Compute loop nesting information (for weighting copies) */
112 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
113 construct_cf_backedges(s->irg);
115 dump_dominator_information(true);
116 dump_ir_block_graph(s->irg, "-prepared");
117 dump_dominator_information(false);
120 static void be_main_loop(void)
124 const arch_isa_if_t *isa;
127 isa = arch_env_get_isa(env.arch_env);
130 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
132 ir_graph *irg = get_irp_irg(i);
133 be_main_session_env_t session;
135 DBG((env.dbg, LEVEL_1, "be irg: %F\n", irg));
137 /* Init the session. */
138 be_init_session_env(&session, &env, irg);
140 /* Compute some analyses and prepare the graph for backend use. */
141 prepare_graph(&session);
143 /* Schedule the graphs. */
144 list_sched(irg, trivial_selector);
145 dump_ir_block_graph_sched(irg, "-sched");
147 /* Verify the schedule */
148 sched_verify_irg(irg);
150 /* Build liveness information */
153 /* Liveness analysis */
157 copystat_collect_irg(irg, env.arch_env);
160 * Verifying the schedule once again cannot hurt.
162 sched_verify_irg(irg);
164 /* Perform the following for each register class. */
165 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
166 be_chordal_env_t *chordal_env;
167 const arch_register_class_t *cls = isa->get_reg_class(j);
169 DBG((env.dbg, LEVEL_1, "\treg class: %s\n", cls->name));
174 chordal_env = be_ra_chordal(irg, env.arch_env, cls);
176 #ifdef DUMP_ALLOCATED
177 dump_allocated_irg(env.arch_env, irg, "");
179 copystat_collect_cls(chordal_env);
181 be_copy_opt(chordal_env);
183 be_ssa_destruction(&session, chordal_env);
184 be_ssa_destruction_check(&session, chordal_env);
185 be_ra_chordal_check(chordal_env);
187 be_ra_chordal_done(chordal_env);
188 be_numbering_done(irg);
191 copystat_dump_pretty(irg);
195 void be_main(int argc, const char *argv[])
197 assembler_t *gnu_assembler;
198 FILE *asm_output_file;
201 gnu_assembler = gnuasm_create_assembler();
202 asm_output_file = fopen("asm_output.asm", "w");
204 asm_dump_globals(gnu_assembler);
205 gnuasm_dump(gnu_assembler, asm_output_file);
206 gnuasm_delete_assembler(gnu_assembler);
207 fclose(asm_output_file);