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