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