Recent version :-)
[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
23 #include "be_t.h"
24 #include "bechordal_t.h"
25 #include "benumb_t.h"
26 #include "besched_t.h"
27 #include "belistsched.h"
28 #include "belive_t.h"
29 #include "beutil.h"
30 #include "bechordal.h"
31 #include "bearch.h"
32 #include "becopyoptmain.h"
33 #include "becopystat.h"
34 #include "bessadestr.h"
35 #include "bearch_firm.h"
36 #include "benode_t.h"
37 #include "beirgmod.h"
38
39 #include "beasm_dump_globals.h"
40 #include "beasm_asm_gnu.h"
41
42 #undef DUMP_ALLOCATED
43 #undef DUMP_LOCALIZED
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
64   env->arch_env = obstack_alloc(&env->obst, sizeof(env->arch_env[0]));
65   arch_env_init(env->arch_env, isa);
66   env->arch_env->isa->init();
67
68   env->node_factory = obstack_alloc(&env->obst, sizeof(*env->node_factory));
69   be_node_factory_init(env->node_factory, isa);
70
71   arch_env_add_irn_handler(env->arch_env, &firm_irn_handler);
72   arch_env_add_irn_handler(env->arch_env,
73       be_node_get_irn_handler(env->node_factory));
74
75   return env;
76 }
77
78 static be_main_session_env_t *
79 be_init_session_env(be_main_session_env_t *env,
80     be_main_env_t *main_env, ir_graph *irg)
81 {
82   env->main_env = main_env;
83   env->irg = irg;
84
85   return env;
86 }
87
88 static void prepare_graph(be_main_session_env_t *s)
89 {
90         /*
91          * Duplicate consts in each block
92          * (This is just for the firm dummy backend)
93          */
94         // localize_consts(s->irg);
95
96         /* Remove critical edges */
97         remove_critical_cf_edges(s->irg);
98
99         /* Compute the dominance information. */
100         free_dom_and_peace(s->irg);
101         compute_doms(s->irg);
102
103         /* Compute the dominance frontiers */
104         s->dom_front = be_compute_dominance_frontiers(s->irg);
105
106         /* Ensure, that the ir_edges are computed. */
107         edges_assure(s->irg);
108
109         dump_dominator_information(true);
110         dump_ir_block_graph(s->irg, "-prepared");
111         dump_dominator_information(false);
112 }
113
114 static void be_main_loop(void)
115 {
116         int i, n;
117         be_main_env_t env;
118         const arch_isa_if_t *isa;
119
120         be_init_env(&env);
121         isa = arch_env_get_isa(env.arch_env);
122
123         /* For all graphs */
124         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
125                 int j, m;
126                 ir_graph *irg = get_irp_irg(i);
127                 be_main_session_env_t session;
128
129                 /* Init the session. */
130                 be_init_session_env(&session, &env, irg);
131
132                 /* Compute some analyses and prepare the graph for backend use. */
133                 prepare_graph(&session);
134
135 #ifdef DUMP_LOCALIZED
136                 dump_consts_local(0);
137                 dump_ir_block_graph(irg, "-local-const");
138 #endif
139                 be_numbering(irg);
140
141                 /* Schedule the graphs. */
142                 list_sched(irg, trivial_selector);
143
144     /* Verify the schedule */
145     sched_verify_irg(irg);
146
147                 /* Liveness analysis */
148                 be_liveness(irg);
149
150                 dump_ir_block_graph_sched(irg, "-sched");
151                 copystat_reset();
152                 copystat_collect_irg(irg, env.arch_env);
153
154                 /* Perform the following for each register class. */
155                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
156                         be_chordal_env_t *chordal_env;
157                         const arch_register_class_t *cls = isa->get_reg_class(j);
158
159       chordal_env = be_ra_chordal(irg, env.arch_env, cls);
160
161 #ifdef DUMP_ALLOCATED
162                         dump_allocated_irg(env.arch_env, irg, "");
163 #endif
164                         copystat_collect_cls(chordal_env);
165
166                         be_copy_opt(chordal_env);
167                         be_ssa_destruction(&session, chordal_env);
168                         be_ra_chordal_done(chordal_env);
169                 }
170                 copystat_dump_pretty(irg);
171             be_numbering_done(irg);
172         }
173 }
174
175 void be_main(int argc, const char *argv[])
176 {
177         assembler_t *gnu_assembler;
178         FILE *asm_output_file;
179
180         be_main_loop();
181         gnu_assembler = gnuasm_create_assembler();
182         asm_output_file = fopen("asm_output.asm", "w");
183
184         asm_dump_globals(gnu_assembler);
185         gnuasm_dump(gnu_assembler, asm_output_file);
186         gnuasm_delete_assembler(gnu_assembler);
187         fclose(asm_output_file);
188 }