Set dump consts local to false for schedule dumping
[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 #include "bespillbelady.h"
42
43 #include "beasm_dump_globals.h"
44 #include "beasm_asm_gnu.h"
45
46 #undef DUMP_BEGIN
47 #undef DUMP_PREPARED
48 #define DUMP_SCHED
49 #define DUMP_SPILL
50 #undef DUMP_ALLOCATED
51 #undef DUMP_COPYMIN
52 #undef DUMP_SSADESTR
53 #undef DUMP_END
54
55 #define N_PHASES 256
56
57 void be_init(void)
58 {
59         be_sched_init();
60         be_liveness_init();
61         be_numbering_init();
62         be_ra_chordal_init();
63         be_copy_opt_init();
64         copystat_init();
65         phi_class_init();
66 }
67
68 static be_main_env_t *be_init_env(be_main_env_t *env)
69 {
70   const arch_isa_if_t *isa = &firm_isa;
71
72   obstack_init(&env->obst);
73   env->dbg = firm_dbg_register("be.main");
74   firm_dbg_set_mask(env->dbg, SET_LEVEL_1);
75
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();
79
80   env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
81   be_node_factory_init(env->node_factory, isa);
82
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));
86
87   return env;
88 }
89
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)
93 {
94   env->main_env = main_env;
95   env->irg = irg;
96
97   return env;
98 }
99
100 static void prepare_graph(be_main_session_env_t *s)
101 {
102         /* set the current graph (this is important for several firm functions) */
103         current_ir_graph = s->irg;
104
105         /* Let the isa prepare the graph. */
106         s->main_env->arch_env->isa->prepare_graph(s->irg);
107
108         /* Normalize proj nodes. */
109         normalize_proj_nodes(s->irg);
110
111         /* Remove critical edges */
112         remove_critical_cf_edges(s->irg);
113
114         /* Compute the dominance information. */
115         free_dom_and_peace(s->irg);
116         compute_doms(s->irg);
117
118         /* Compute the dominance frontiers */
119         s->dom_front = be_compute_dominance_frontiers(s->irg);
120
121         /* Ensure, that the ir_edges are computed. */
122         edges_activate(s->irg);
123
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);
127
128         be_check_dominance(s->irg);
129 }
130
131 static void be_main_loop(void)
132 {
133         int i, n;
134         be_main_env_t env;
135         const arch_isa_if_t *isa;
136
137         be_init_env(&env);
138         isa = arch_env_get_isa(env.arch_env);
139
140         /* For all graphs */
141         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
142                 int j, m;
143                 ir_graph *irg = get_irp_irg(i);
144                 be_main_session_env_t session;
145
146                 DBG((env.dbg, LEVEL_1, "====> IRG: %F\n", irg));
147 #ifdef DUMP_BEGIN
148                 dump_ir_block_graph(irg, "-begin");
149 #endif
150
151                 /* Init the session. */
152                 be_init_session_env(&session, &env, irg);
153
154                 /* Compute some analyses and prepare the graph for backend use. */
155                 prepare_graph(&session);
156 #ifdef DUMP_PREPARED
157                 dump_dominator_information(true);
158                 dump_ir_block_graph(irg, "-prepared");
159                 dump_dominator_information(false);
160 #endif
161
162                 /* Schedule the graphs. */
163                 list_sched(irg, trivial_selector);
164                 be_sched_imm(irg);
165
166 #ifdef DUMP_SCHED
167                 dump_ir_block_graph_sched(irg, "-sched");
168 #endif
169
170                 /* Verify the schedule */
171                 sched_verify_irg(irg);
172
173                 /* Build liveness information */
174                 be_liveness(irg);
175
176                 /* Perform the following for each register class. */
177                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
178                         be_chordal_env_t *chordal_env;
179                         const arch_register_class_t *cls = isa->get_reg_class(j);
180                         DBG((env.dbg, LEVEL_1, "----> Reg class: %s\n", cls->name));
181
182                         /* spilling */
183                         //be_spill_ilp(&session, cls);
184                         be_spill_belady(&session, cls);
185 #ifdef DUMP_SPILL
186                         dump_ir_block_graph_sched(session.irg, "-spill");
187 #endif
188                         be_liveness(irg);
189                         be_numbering(irg);
190                         be_check_pressure(&session, cls);
191
192 #if 0
193                         {
194                                 FILE *f;
195                                 char buf[128];
196                                 ir_snprintf(buf, sizeof(buf), "%F_%s-live.txt", irg, cls->name);
197                                 if((f = fopen(buf, "wt")) != NULL) {
198                                         be_liveness_dump(session.irg, f);
199                                         fclose(f);
200                                 }
201                         }
202 #endif
203
204                         /* allocation */
205                         chordal_env = be_ra_chordal(&session, cls);
206 #ifdef DUMP_ALLOCATED
207                         dump_allocated_irg(env.arch_env, irg, "");
208 #endif
209
210                         /* copy minimization */
211                         copystat_collect_cls(chordal_env);
212                         be_copy_opt(chordal_env);
213 #ifdef DUMP_COPYMIN
214                         dump_allocated_irg(env.arch_env, irg, "-copymin");
215 #endif
216
217                         /* ssa destruction */
218                         be_ssa_destruction(chordal_env);
219                         be_ssa_destruction_check(chordal_env);
220                         be_ra_chordal_check(chordal_env);
221 #ifdef DUMP_SSADESTR
222                         dump_allocated_irg(env.arch_env, irg, "-ssadestr");
223 #endif
224
225                         be_ra_chordal_done(chordal_env);
226                         be_numbering_done(irg);
227                 }
228 #ifdef DUMP_END
229                 dump_ir_block_graph_sched(session.irg, "-end");
230 #endif
231                 copystat_dump(irg);
232         }
233 }
234
235 void be_main(int argc, const char *argv[])
236 {
237         assembler_t *gnu_assembler;
238         FILE *asm_output_file;
239
240         be_main_loop();
241
242 #if 0
243         gnu_assembler = gnuasm_create_assembler();
244         asm_output_file = fopen("asm_output.asm", "w");
245
246         asm_dump_globals(gnu_assembler);
247         gnuasm_dump(gnu_assembler, asm_output_file);
248         gnuasm_delete_assembler(gnu_assembler);
249         fclose(asm_output_file);
250 #endif
251 }