bugfix
[libfirm] / ir / be / bemain.c
1 /**
2  * Backend driver.
3  * @author Sebastian Hack
4  * @date 25.11.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdarg.h>
11 #include <mcheck.h>
12
13 #include "obst.h"
14 #include "bitset.h"
15
16 #include "irprog.h"
17 #include "irgopt.h"
18 #include "irgraph.h"
19 #include "irdump.h"
20 #include "phiclass.h"
21 #include "irdom_t.h"
22 #include "iredges_t.h"
23 #include "irloop_t.h"
24
25 #include "be_t.h"
26 #include "bechordal_t.h"
27 #include "benumb_t.h"
28 #include "besched_t.h"
29 #include "belistsched.h"
30 #include "belive_t.h"
31 #include "beutil.h"
32 #include "bechordal.h"
33 #include "bearch.h"
34 #include "becopyoptmain.h"
35 #include "becopystat.h"
36 #include "bessadestr.h"
37 #include "bearch_firm.h"
38 #include "benode_t.h"
39 #include "beirgmod.h"
40 #include "bespillilp.h"
41
42 #include "beasm_dump_globals.h"
43 #include "beasm_asm_gnu.h"
44
45 #undef DUMP_ALLOCATED
46
47 #define N_PHASES 256
48
49
50 void be_init(void)
51 {
52         be_sched_init();
53         be_liveness_init();
54         be_numbering_init();
55         be_ra_chordal_init();
56         be_copy_opt_init();
57         copystat_init();
58         phi_class_init();
59 }
60
61 static be_main_env_t *be_init_env(be_main_env_t *env)
62 {
63   const arch_isa_if_t *isa = &firm_isa;
64
65   obstack_init(&env->obst);
66   env->dbg = firm_dbg_register("be.main");
67   firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
68
69   env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
70   arch_env_init(env->arch_env, isa);
71   env->arch_env->isa->init();
72
73   env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
74   be_node_factory_init(env->node_factory, isa);
75
76   arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
77   arch_env_add_irn_handler(env->arch_env,
78       be_node_get_irn_handler(env->node_factory));
79
80   return env;
81 }
82
83 static be_main_session_env_t *
84 be_init_session_env(be_main_session_env_t *env,
85     be_main_env_t *main_env, ir_graph *irg)
86 {
87   env->main_env = main_env;
88   env->irg = irg;
89
90   return env;
91 }
92
93 static void prepare_graph(be_main_session_env_t *s)
94 {
95         /* set the current graph (this is important for several firm functions) */
96         current_ir_graph = s->irg;
97
98         /* Let the isa prepare the graph. */
99         s->main_env->arch_env->isa->prepare_graph(s->irg);
100
101         /* Remove critical edges */
102         remove_critical_cf_edges(s->irg);
103
104         /* Compute the dominance information. */
105         free_dom_and_peace(s->irg);
106         compute_doms(s->irg);
107
108         /* Compute the dominance frontiers */
109         s->dom_front = be_compute_dominance_frontiers(s->irg);
110
111         /* Ensure, that the ir_edges are computed. */
112         edges_activate(s->irg);
113
114         /* Compute loop nesting information (for weighting copies) */
115         if (get_irg_loopinfo_state(s->irg) != (loopinfo_valid & loopinfo_cf_consistent))
116                 construct_cf_backedges(s->irg);
117
118         dump_dominator_information(true);
119         dump_ir_block_graph(s->irg, "-prepared");
120         dump_dominator_information(false);
121 }
122
123 static void be_main_loop(void)
124 {
125         int i, n;
126         be_main_env_t env;
127         const arch_isa_if_t *isa;
128
129         be_init_env(&env);
130         isa = arch_env_get_isa(env.arch_env);
131
132         /* For all graphs */
133         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
134                 int j, m;
135                 ir_graph *irg = get_irp_irg(i);
136                 be_main_session_env_t session;
137
138                 DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
139
140                 /* Init the session. */
141                 be_init_session_env(&session, &env, irg);
142
143                 /* Compute some analyses and prepare the graph for backend use. */
144                 prepare_graph(&session);
145
146                 /* Schedule the graphs. */
147                 list_sched(irg, trivial_selector);
148                 dump_ir_block_graph_sched(irg, "-sched");
149
150                 /* Verify the schedule */
151                 sched_verify_irg(irg);
152
153                 /* Build liveness information */
154                 be_liveness(irg);
155
156                 /* Perform the following for each register class. */
157                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
158                         be_chordal_env_t *chordal_env;
159                         const arch_register_class_t *cls = isa->get_reg_class(j);
160
161                         DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
162
163                         be_spill_ilp(&session, cls);
164                         dump_ir_block_graph_sched(session.irg, "-spill");
165
166                         be_liveness(irg);
167                         be_numbering(irg);
168                         be_check_pressure(&session, cls);
169
170 #if 0
171                         {
172                                 FILE *f;
173                                 char buf[128];
174                                 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
175                                 if((f = fopen(buf, "wt")) != NULL) {
176                                         be_liveness_dump(session.irg, f);
177                                         fclose(f);
178                                 }
179                         }
180 #endif
181
182                         chordal_env = be_ra_chordal(&session, cls);
183
184 #ifdef DUMP_ALLOCATED
185                         dump_allocated_irg(env.arch_env, irg, "");
186 #endif
187                         copystat_collect_cls(chordal_env);
188
189 //                      dump_allocated_irg(env.arch_env, irg, "pre");
190                         be_copy_opt(chordal_env);
191 //                      dump_allocated_irg(env.arch_env, irg, "post");
192
193                         be_ssa_destruction(chordal_env);
194                         be_ssa_destruction_check(chordal_env);
195                         be_ra_chordal_check(chordal_env);
196
197                         be_ra_chordal_done(chordal_env);
198                         be_numbering_done(irg);
199                 }
200                 dump_ir_block_graph_sched(session.irg, "-post");
201
202                 copystat_dump(irg);
203         }
204 }
205
206 void be_main(int argc, const char *argv[])
207 {
208         assembler_t *gnu_assembler;
209         FILE *asm_output_file;
210
211         mtrace();
212         be_main_loop();
213         muntrace();
214
215         gnu_assembler = gnuasm_create_assembler();
216         asm_output_file = fopen("asm_output.asm", "w");
217
218         asm_dump_globals(gnu_assembler);
219         gnuasm_dump(gnu_assembler, asm_output_file);
220         gnuasm_delete_assembler(gnu_assembler);
221         fclose(asm_output_file);
222
223 }