- All backend modules use module constructors for registering their options now
[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 static sched_timestep_t get_time_step(const ir_node *irn)
26 {
27         if(is_Phi(irn))
28                 return 0;
29
30         return sched_get_time_step(irn);
31 }
32
33 int value_dominates(const ir_node *a, const ir_node *b)
34 {
35         int res = 0;
36         const ir_node *ba = get_block(a);
37         const ir_node *bb = get_block(b);
38
39         /*
40          * a and b are not in the same block,
41          * so dominance is determined by the dominance of the blocks.
42          */
43         if(ba != bb) {
44                 res = block_dominates(ba, bb);
45
46         /*
47          * Dominance is determined by the time steps of the schedule.
48          */
49         } else {
50                 sched_timestep_t as = get_time_step(a);
51                 sched_timestep_t bs = get_time_step(b);
52                 res = as <= bs;
53         }
54
55         return res;
56 }
57
58 /**
59  * Check, if two values interfere.
60  * @param a The first value.
61  * @param b The second value.
62  * @return 1, if a and b interfere, 0 if not.
63  */
64 int values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
65 {
66         int a2b = value_dominates(a, b);
67         int b2a = value_dominates(b, a);
68
69         /* If there is no dominance relation, they do not interfere. */
70         if((a2b | b2a) > 0) {
71                 const ir_edge_t *edge;
72                 ir_node *bb;
73
74                 /*
75                  * Adjust a and b so, that a dominates b if
76                  * a dominates b or vice versa.
77                  */
78                 if(b2a) {
79                         const ir_node *t = a;
80                         a = b;
81                         b = t;
82                 }
83
84                 bb = get_nodes_block(b);
85
86                 /*
87                  * If a is live end in b's block it is
88                  * live at b's definition (a dominates b)
89                  */
90                 if(be_is_live_end(lv, bb, a))
91                         return 1;
92
93                 /*
94                  * Look at all usages of a.
95                  * If there's one usage of a in the block of b, then
96                  * we check, if this use is dominated by b, if that's true
97                  * a and b interfere. Note that b must strictly dominate the user,
98                  * since if b is the last user of in the block, b and a do not
99                  * interfere.
100                  * Uses of a not in b's block can be disobeyed, because the
101                  * check for a being live at the end of b's block is already
102                  * performed.
103                  */
104                 foreach_out_edge(a, edge) {
105                         const ir_node *user = get_edge_src_irn(edge);
106                         if(get_nodes_block(user) == bb && !is_Phi(user) && b != user && value_dominates(b, user))
107                                 return 1;
108                 }
109         }
110
111         return 0;
112 }
113
114 /** The list of register allocators */
115 static be_module_list_entry_t *register_allocators = NULL;
116 static be_ra_t *selected_allocator = NULL;
117
118 void be_register_allocator(const char *name, be_ra_t *allocator)
119 {
120         if(selected_allocator == NULL)
121                 selected_allocator = allocator;
122         be_add_module_to_list(&register_allocators, name, allocator);
123 }
124
125 void be_init_ra(void)
126 {
127         lc_opt_entry_t *be_grp = lc_opt_get_grp(firm_opt_get_root(), "be");
128
129         be_add_module_list_opt(be_grp, "regalloc", "register allocator",
130                                &register_allocators, (void**) &selected_allocator);
131 }
132 BE_REGISTER_MODULE_CONSTRUCTOR(init_be_ra);