d25617fb16cdb74cb33c986f3f20492b83e1e16f
[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
18 #include "be_t.h"
19 #include "bera_t.h"
20 #include "benumb_t.h"
21 #include "besched_t.h"
22 #include "belistsched.h"
23 #include "belive_t.h"
24 #include "beutil.h"
25 #include "bephicongr_t.h"
26 #include "bechordal.h"
27 #include "bechordal.h"
28 #include "bephiopt.h"
29 #include "phistat.h"
30
31 #define N_PHASES 256
32
33 typedef struct _be_graph_info_t {
34         bitset_t *applied_phases;
35 } be_graph_info_t;
36
37 static size_t be_info_offset = 0;
38
39 #define get_irg_be_info(irg) get_irg_data(irg, be_graph_info_t, be_info_offset)
40
41 static int phase_ids = 1;
42 static struct obstack obst;
43
44 int phase_register(phase_t *phase)
45 {
46         phase->id = phase_ids;
47         return phase_ids++;
48 }
49
50 void phase_applied(const ir_graph *irg, const phase_t *phase)
51 {
52         be_graph_info_t *info = get_irg_be_info(irg);
53
54         if(!info->applied_phases)
55                 info->applied_phases = bitset_obstack_alloc(&obst, N_PHASES);
56
57         bitset_set(info->applied_phases, phase->id);
58 }
59
60 int phase_depends_on(const ir_graph *irg, const phase_t *phase, int n, ...)
61 {
62         int errors = 0;
63         int i;
64         va_list args;
65
66         if(n > 0) {
67                 const be_graph_info_t *info = get_irg_be_info(irg);
68                 const bitset_t *applied_phases = info->applied_phases;
69
70                 va_start(args, n);
71
72                 for(i = 0; i < n; ++i) {
73                         const phase_t *dep_phase = va_arg(args, const phase_t *);
74
75                         if(!applied_phases || !bitset_is_set(applied_phases, dep_phase->id)) {
76                                 errors++;
77                                 fprintf(stderr, "phase dependency unfulfilled: \"%s\" depends on \"%s\"\n",
78                                                 phase->name, dep_phase->name);
79                         }
80                 }
81
82                 va_end(args);
83
84                 assert(errors > 0 && "There were phase dependency errors");
85         }
86
87         return errors;
88 }
89
90 void be_init(void)
91 {
92         obstack_init(&obst);
93         be_info_offset = register_additional_graph_data(sizeof(be_graph_info_t));
94
95         be_sched_init();
96         be_liveness_init();
97         be_numbering_init();
98         be_ra_init();
99         be_ra_chordal_init();
100         be_phi_congr_class_init();
101 }
102
103 extern void be_ra_chordal(ir_graph *irg);
104
105 static void be_main_loop(void)
106 {
107         int i, n;
108
109         for(i = 0, n = get_irp_n_irgs(); i < n; ++i) {
110                 ir_graph *irg = get_irp_irg(i);
111
112                 localize_consts(irg);
113                 be_numbering(irg);
114                 list_sched(irg, trivial_selector, NULL);
115                 be_liveness(irg);
116                 be_ra_chordal(irg);
117                 //be_phi_opt(irg);
118
119                 //dump_allocated_irg(irg);
120
121                 be_ra_chordal_done(irg);
122                 be_numbering_done(irg);
123         }
124 }
125
126 void be_main(int argc, const char *argv[])
127 {
128         be_main_loop();
129 }