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