Added support for memory phis in liveness
[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 "pset.h"
11 #include "impl.h"
12
13 #include "irnode.h"
14 #include "irmode.h"
15 #include "irdom.h"
16
17 #include "beutil.h"
18 #include "besched_t.h"
19 #include "belive_t.h"
20
21 static sched_timestep_t get_time_step(const ir_node *irn)
22 {
23         return is_Phi(irn) ? 0 : sched_get_time_step(irn);
24 }
25
26 int value_dominates(const ir_node *a, const ir_node *b)
27 {
28         int res = 0;
29         const ir_node *ba = get_block(a);
30         const ir_node *bb = get_block(b);
31
32         /*
33          * a and b are not in the same block,
34          * so dominance is determined by the dominance of the blocks.
35          */
36         if(ba != bb) {
37                 res = block_dominates(ba, bb);
38
39         /*
40          * Dominance is determined by the time steps of the schedule.
41          */
42         } else {
43                 sched_timestep_t as = get_time_step(a);
44                 sched_timestep_t bs = get_time_step(b);
45                 res = as <= bs;
46         }
47
48         return res;
49 }
50
51 /**
52  * Check, if two values interfere.
53  * @param a The first value.
54  * @param b The second value.
55  * @return 1, if a and b interfere, 0 if not.
56  */
57 int values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
58 {
59         int a2b = value_dominates(a, b);
60         int b2a = value_dominates(b, a);
61
62         /* If there is no dominance relation, they do not interfere. */
63         if((a2b | b2a) > 0) {
64                 const ir_edge_t *edge;
65                 ir_node *bb;
66
67                 /*
68                  * Adjust a and b so, that a dominates b if
69                  * a dominates b or vice versa.
70                  */
71                 if(b2a) {
72                         const ir_node *t = a;
73                         a = b;
74                         b = t;
75                 }
76
77                 bb = get_nodes_block(b);
78
79                 /*
80                  * If a is live end in b's block it is
81                  * live at b's definition (a dominates b)
82                  */
83                 if(be_is_live_end(lv, bb, a))
84                         return 1;
85
86                 /*
87                  * Look at all usages of a.
88                  * If there's one usage of a in the block of b, then
89                  * we check, if this use is dominated by b, if that's true
90                  * a and b interfere. Note that b must strictly dominate the user,
91                  * since if b is the last user of in the block, b and a do not
92                  * interfere.
93                  * Uses of a not in b's block can be disobeyed, because the
94                  * check for a being live at the end of b's block is already
95                  * performed.
96                  */
97                 foreach_out_edge(a, edge) {
98                         const ir_node *user = edge->src;
99                         if(get_nodes_block(user) == bb && !is_Phi(user) && b != user && value_dominates(b, user))
100                                 return 1;
101                 }
102   }
103   return 0;
104 }