beifg: Factorise code to count interference components.
[libfirm] / ir / be / beintlive_t.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 Universitaet Karlsruhe
4  */
5 #ifndef _BELIVECHK_T_H
6 #define _BELIVECHK_T_H
7
8 #include "besched.h"
9 #include "belive_t.h"
10 #include "beutil.h"
11 #include "iredges_t.h"
12
13 /**
14  * Check dominance of two nodes in the same block.
15  * @param a The first node.
16  * @param b The second node.
17  * @return 1 if a comes before b in the same block or if a == b, 0 else.
18  */
19 static inline int _value_dominates_intrablock(const ir_node *a, const ir_node *b)
20 {
21         sched_timestep_t const as = sched_get_time_step(a);
22         sched_timestep_t const bs = sched_get_time_step(b);
23         return as <= bs;
24 }
25
26 /**
27  * Check strict dominance of two nodes in the same block.
28  * @param a The first node.
29  * @param b The second node.
30  * @return 1 if a comes before b in the same block, 0 else.
31  */
32 static inline int _value_strictly_dominates_intrablock(const ir_node *a, const ir_node *b)
33 {
34         sched_timestep_t const as = sched_get_time_step(a);
35         sched_timestep_t const bs = sched_get_time_step(b);
36         return as < bs;
37 }
38
39 /**
40  * Check, if one value dominates the other.
41  * The dominance is not strict here.
42  * @param a The first node.
43  * @param b The second node.
44  * @return 1 if a dominates b or if a == b, 0 else.
45  */
46 static inline int value_dominates(const ir_node *a, const ir_node *b)
47 {
48         const ir_node *block_a = get_block_const(a);
49         const ir_node *block_b = get_block_const(b);
50
51         /*
52          * a and b are not in the same block,
53          * so dominance is determined by the dominance of the blocks.
54          */
55         if(block_a != block_b) {
56                 return block_dominates(block_a, block_b);
57         }
58
59         /*
60          * Dominance is determined by the time steps of the schedule.
61          */
62         return _value_dominates_intrablock(a, b);
63 }
64
65 /**
66  * Check, if two values interfere.
67  * @param lv Liveness information
68  * @param a The first value.
69  * @param b The second value.
70  * @return true, if a and b interfere, false if not.
71  */
72 static inline bool be_values_interfere(be_lv_t const *lv, ir_node const *a, ir_node const *b)
73 {
74         if (value_dominates(b, a)) {
75                 /* Adjust a and b so, that a dominates b if
76                  * a dominates b or vice versa. */
77                 ir_node const *const t = a;
78                 a = b;
79                 b = t;
80         } else if (!value_dominates(a, b)) {
81                 /* If there is no dominance relation, they do not interfere. */
82                 return false;
83         }
84
85         ir_node *const bb = get_nodes_block(b);
86
87         /* If a is live end in b's block it is
88          * live at b's definition (a dominates b) */
89         if (be_is_live_end(lv, bb, a))
90                 return true;
91
92         /* Look at all usages of a.
93          * If there's one usage of a in the block of b, then
94          * we check, if this use is dominated by b, if that's true
95          * a and b interfere. Note that b must strictly dominate the user,
96          * since if b is the last user of in the block, b and a do not
97          * interfere.
98          * Uses of a not in b's block can be disobeyed, because the
99          * check for a being live at the end of b's block is already
100          * performed. */
101         foreach_out_edge(a, edge) {
102                 ir_node const *const user = get_edge_src_irn(edge);
103                 if (get_nodes_block(user) == bb && !is_Phi(user) && _value_strictly_dominates_intrablock(b, user))
104                         return true;
105         }
106
107         return false;
108 }
109
110 #endif