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