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