put timers in a global var for now, until this is solved cleaner
[libfirm] / ir / be / bera.c
1 /**
2  * Base routines for register allocation.
3  * @author Sebastian Hack
4  * @date 22.11.2004
5  */
6 #ifdef HAVE_CONFIG_H
7 #include "config.h"
8 #endif
9
10 #include <stdlib.h>
11
12 #include "pset.h"
13 #include "impl.h"
14
15 #include "irnode.h"
16 #include "irmode.h"
17 #include "irdom.h"
18
19 #include "bera.h"
20 #include "beutil.h"
21 #include "besched_t.h"
22 #include "belive_t.h"
23 #include "bemodule.h"
24
25 be_ra_timer_t *global_ra_timer = NULL;
26
27 static sched_timestep_t get_time_step(const ir_node *irn)
28 {
29         if(is_Phi(irn))
30                 return 0;
31
32         return sched_get_time_step(irn);
33 }
34
35 int value_dominates(const ir_node *a, const ir_node *b)
36 {
37         int res = 0;
38         const ir_node *ba = get_block(a);
39         const ir_node *bb = get_block(b);
40
41         /*
42          * a and b are not in the same block,
43          * so dominance is determined by the dominance of the blocks.
44          */
45         if(ba != bb) {
46                 res = block_dominates(ba, bb);
47
48         /*
49          * Dominance is determined by the time steps of the schedule.
50          */
51         } else {
52                 sched_timestep_t as = get_time_step(a);
53                 sched_timestep_t bs = get_time_step(b);
54                 res = as <= bs;
55         }
56
57         return res;
58 }
59
60 /**
61  * Check, if two values interfere.
62  * @param a The first value.
63  * @param b The second value.
64  * @return 1, if a and b interfere, 0 if not.
65  */
66 int values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
67 {
68         int a2b = value_dominates(a, b);
69         int b2a = value_dominates(b, a);
70
71         /* If there is no dominance relation, they do not interfere. */
72         if((a2b | b2a) > 0) {
73                 const ir_edge_t *edge;
74                 ir_node *bb;
75
76                 /*
77                  * Adjust a and b so, that a dominates b if
78                  * a dominates b or vice versa.
79                  */
80                 if(b2a) {
81                         const ir_node *t = a;
82                         a = b;
83                         b = t;
84                 }
85
86                 bb = get_nodes_block(b);
87
88                 /*
89                  * If a is live end in b's block it is
90                  * live at b's definition (a dominates b)
91                  */
92                 if(be_is_live_end(lv, bb, a))
93                         return 1;
94
95                 /*
96                  * Look at all usages of a.
97                  * If there's one usage of a in the block of b, then
98                  * we check, if this use is dominated by b, if that's true
99                  * a and b interfere. Note that b must strictly dominate the user,
100                  * since if b is the last user of in the block, b and a do not
101                  * interfere.
102                  * Uses of a not in b's block can be disobeyed, because the
103                  * check for a being live at the end of b's block is already
104                  * performed.
105                  */
106                 foreach_out_edge(a, edge) {
107                         const ir_node *user = get_edge_src_irn(edge);
108                         if(get_nodes_block(user) == bb && !is_Phi(user) && b != user && value_dominates(b, user))
109                                 return 1;
110                 }
111         }
112
113         return 0;
114 }
115
116 /** The list of register allocators */
117 static be_module_list_entry_t *register_allocators = NULL;
118 static be_ra_t *selected_allocator = NULL;
119
120 void be_register_allocator(const char *name, be_ra_t *allocator)
121 {
122         if(selected_allocator == NULL)
123                 selected_allocator = allocator;
124         be_add_module_to_list(&register_allocators, name, allocator);
125 }
126
127 void be_allocate_registers(be_irg_t *birg)
128 {
129         assert(selected_allocator != NULL);
130         if(selected_allocator != NULL) {
131                 selected_allocator->allocate(birg);
132         }
133 }
134
135 void be_init_ra(void)
136 {
137         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
138
139         be_add_module_list_opt(be_grp, "regalloc", "register allocator",
140                                &register_allocators, (void**) &selected_allocator);
141 }
142 BE_REGISTER_MODULE_CONSTRUCTOR(init_be_ra);