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