Bugfix concerning lower bound compution. Debug-Level for specific IRG
[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 "irgraph.h"
17 #include "irdump.h"
18 #include "phiclass.h"
19
20 #include "be_t.h"
21 #include "bera_t.h"
22 #include "benumb_t.h"
23 #include "besched_t.h"
24 #include "belistsched.h"
25 #include "belive_t.h"
26 #include "beutil.h"
27 #include "bechordal.h"
28 #include "bearch.h"
29 #include "becopyoptmain.h"
30 #include "becopystat.h"
31
32 #include "beasm_dump_globals.h"
33 #include "beasm_asm_gnu.h"
34
35 #undef DUMP_ALLOCATED
36 #define DUMP_LOCALIZED
37
38 #define N_PHASES 256
39
40 typedef struct _be_graph_info_t {
41         bitset_t *applied_phases;
42 } be_graph_info_t;
43
44 static size_t be_info_offset = 0;
45
46 #define get_irg_be_info(irg) get_irg_data(irg, be_graph_info_t, be_info_offset)
47
48 static int phase_ids = 1;
49 static struct obstack obst;
50
51 int phase_register(phase_t *phase)
52 {
53         phase->id = phase_ids;
54         return phase_ids++;
55 }
56
57 void phase_applied(const ir_graph *irg, const phase_t *phase)
58 {
59         be_graph_info_t *info = get_irg_be_info(irg);
60
61         if(!info->applied_phases)
62                 info->applied_phases = bitset_obstack_alloc(&obst, N_PHASES);
63
64         bitset_set(info->applied_phases, phase->id);
65 }
66
67 int phase_depends_on(const ir_graph *irg, const phase_t *phase, int n, ...)
68 {
69         int errors = 0;
70         int i;
71         va_list args;
72
73         if(n > 0) {
74                 const be_graph_info_t *info = get_irg_be_info(irg);
75                 const bitset_t *applied_phases = info->applied_phases;
76
77                 va_start(args, n);
78
79                 for(i = 0; i < n; ++i) {
80                         const phase_t *dep_phase = va_arg(args, const phase_t *);
81
82                         if(!applied_phases || !bitset_is_set(applied_phases, dep_phase->id)) {
83                                 errors++;
84                                 fprintf(stderr, "phase dependency unfulfilled: \"%s\" depends on \"%s\"\n",
85                                                 phase->name, dep_phase->name);
86                         }
87                 }
88
89                 va_end(args);
90
91                 assert(errors > 0 && "There were phase dependency errors");
92         }
93
94         return errors;
95 }
96
97 void be_init(void)
98 {
99         obstack_init(&obst);
100         be_info_offset = register_additional_graph_data(sizeof(be_graph_info_t));
101
102         be_sched_init();
103         be_liveness_init();
104         be_numbering_init();
105         be_ra_init();
106         be_ra_chordal_init();
107 #ifdef DO_STAT
108         stat_init();
109 #endif
110         be_copy_opt_init();
111 }
112
113 /* The preliminary Firm backend isa. */
114 extern arch_isa_if_t arch_isa_if_firm;
115
116 static void be_main_loop(void)
117 {
118         int i, n;
119         const arch_isa_if_t *isa = &arch_isa_if_firm;
120
121         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
122                 int j, m;
123                 ir_graph *irg = get_irp_irg(i);
124
125                 localize_consts(irg);
126 #ifdef DUMP_LOCALIZED
127                 dump_consts_local(0);
128                 dump_ir_block_graph(irg, "-local-const");
129 #endif
130                 be_numbering(irg);
131
132                 /* Schedule the graphs. */
133                 list_sched(irg, trivial_selector);
134
135                 /* Liveness analysis */
136                 be_liveness(irg);
137
138 #ifdef DO_STAT
139                 stat_reset();
140                 stat_collect_irg(irg);
141 #endif
142                 /* Perform the following for each register class. */
143                 for(j = 0, m = isa->get_n_reg_class(); j < m; ++j) {
144                         const arch_register_class_t *cls = isa->get_reg_class(j);
145
146                         be_ra_chordal(irg, isa, cls);
147
148 #ifdef DUMP_ALLOCATED
149                         dump_allocated_irg(irg, "");
150 #endif
151                         be_copy_opt(irg, isa, cls);
152                         be_ra_chordal_done(irg);
153                 }
154 #ifdef DO_STAT
155                 stat_dump(irg);
156 #endif
157             be_numbering_done(irg);
158         }
159 }
160
161 void be_main(int argc, const char *argv[])
162 {
163         assembler_t *gnu_assembler;
164         FILE *asm_output_file;
165
166         be_main_loop();
167         gnu_assembler = gnuasm_create_assembler();
168         asm_output_file = fopen("asm_output.asm", "w");
169
170         asm_dump_globals(gnu_assembler);
171         gnuasm_dump(gnu_assembler, asm_output_file);
172         gnuasm_delete_assembler(gnu_assembler);
173         fclose(asm_output_file);
174 }