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"
41 #include "bespillbelady.h"
43 #include "beasm_dump_globals.h"
44 #include "beasm_asm_gnu.h"
68 static be_main_env_t *be_init_env(be_main_env_t *env)
70 const arch_isa_if_t *isa = &firm_isa;
72 obstack_init(&env->obst);
73 env->dbg = firm_dbg_register("be.main");
74 firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
76 env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
77 arch_env_init(env->arch_env, isa);
78 env->arch_env->isa->init();
80 env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
81 be_node_factory_init(env->node_factory, isa);
83 arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
84 arch_env_add_irn_handler(env->arch_env,
85 be_node_get_irn_handler(env->node_factory));
90 static be_main_session_env_t *
91 be_init_session_env(be_main_session_env_t *env,
92 be_main_env_t *main_env, ir_graph *irg)
94 env->main_env = main_env;
100 static void prepare_graph(be_main_session_env_t *s)
102 /* set the current graph (this is important for several firm functions) */
103 current_ir_graph = s->irg;
105 /* Let the isa prepare the graph. */
106 s->main_env->arch_env->isa->prepare_graph(s->irg);
108 /* Normalize proj nodes. */
109 normalize_proj_nodes(s->irg);
111 /* Remove critical edges */
112 remove_critical_cf_edges(s->irg);
114 /* Compute the dominance information. */
115 free_dom_and_peace(s->irg);
116 compute_doms(s->irg);
118 /* Compute the dominance frontiers */
119 s->dom_front = be_compute_dominance_frontiers(s->irg);
121 /* Ensure, that the ir_edges are computed. */
122 edges_activate(s->irg);
124 /* Compute loop nesting information (for weighting copies) */
125 if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
126 construct_cf_backedges(s->irg);
128 be_check_dominance(s->irg);
131 static void be_main_loop(void)
135 const arch_isa_if_t *isa;
138 isa = arch_env_get_isa(env.arch_env);
141 for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
143 ir_graph *irg = get_irp_irg(i);
144 be_main_session_env_t session;
146 DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
148 dump_ir_block_graph(irg, "-begin");
151 /* Init the session. */
152 be_init_session_env(&session, &env, irg);
154 /* Compute some analyses and prepare the graph for backend use. */
155 prepare_graph(&session);
157 dump_dominator_information(true);
158 dump_ir_block_graph(irg, "-prepared");
159 dump_dominator_information(false);
162 /* Schedule the graphs. */
163 list_sched(irg, trivial_selector);
165 dump_ir_block_graph_sched(irg, "-sched");
168 /* Verify the schedule */
169 sched_verify_irg(irg);
171 /* Build liveness information */
174 /* Perform the following for each register class. */
175 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
176 be_chordal_env_t *chordal_env;
177 const arch_register_class_t *cls = isa->get_reg_class(j);
178 DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
181 be_spill_ilp(&session, cls);
182 //be_spill_belady(&session, cls);
184 dump_ir_block_graph_sched(session.irg, "-spill");
188 be_check_pressure(&session, cls);
194 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
195 if((f = fopen(buf, "wt")) != NULL) {
196 be_liveness_dump(session.irg, f);
203 chordal_env = be_ra_chordal(&session, cls);
204 #ifdef DUMP_ALLOCATED
205 dump_allocated_irg(env.arch_env, irg, "");
208 /* copy minimization */
209 copystat_collect_cls(chordal_env);
210 be_copy_opt(chordal_env);
212 dump_allocated_irg(env.arch_env, irg, "-copymin");
215 /* ssa destruction */
216 be_ssa_destruction(chordal_env);
217 be_ssa_destruction_check(chordal_env);
218 be_ra_chordal_check(chordal_env);
220 dump_allocated_irg(env.arch_env, irg, "-ssadestr");
223 be_ra_chordal_done(chordal_env);
224 be_numbering_done(irg);
227 dump_ir_block_graph_sched(session.irg, "-end");
233 void be_main(int argc, const char *argv[])
235 assembler_t *gnu_assembler;
236 FILE *asm_output_file;
241 gnu_assembler = gnuasm_create_assembler();
242 asm_output_file = fopen("asm_output.asm", "w");
244 asm_dump_globals(gnu_assembler);
245 gnuasm_dump(gnu_assembler, asm_output_file);
246 gnuasm_delete_assembler(gnu_assembler);
247 fclose(asm_output_file);