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