4 * @author Sebastian Hack
6 * Principal routines for liveness and interference checks.
8 * Copyright (C) 2007 Universitaet Karlsruhe
9 * Released under the GPL
12 #ifndef _BELIVECHK_T_H
13 #define _BELIVECHK_T_H
15 #include "irgraph_t.h"
16 #include "irphase_t.h"
17 #include "iredges_t.h"
20 #include "besched_t.h"
23 * Check dominance of two nodes in the same block.
24 * @param a The first node.
25 * @param b The second node.
26 * @return 1 if a comes before b in the same block or if a == b, 0 else.
28 static INLINE int _value_dominates_intrablock(const ir_node *a, const ir_node *b)
30 /* TODO: ? : can be removed?! */
31 sched_timestep_t as = is_Phi(a) ? 0 : sched_get_time_step(a);
32 sched_timestep_t bs = is_Phi(b) ? 0 : sched_get_time_step(b);
37 * Check, if one value dominates the other.
38 * The dominance is not strict here.
39 * @param a The first node.
40 * @param b The second node.
41 * @return 1 if a dominates b or if a == b, 0 else.
43 static INLINE int _value_dominates(const ir_node *a, const ir_node *b)
45 const ir_node *block_a = get_block(a);
46 const ir_node *block_b = get_block(b);
49 * a and b are not in the same block,
50 * so dominance is determined by the dominance of the blocks.
52 if(block_a != block_b) {
53 return block_dominates(block_a, block_b);
57 * Dominance is determined by the time steps of the schedule.
59 return _value_dominates_intrablock(a, b);
63 * Check, if two values interfere.
64 * @param lv Liveness information (in the future we should use a be_irg_t here).
65 * @param a The first value.
66 * @param b The second value.
67 * @return 1, if a and b interfere, 0 if not.
69 static INLINE int _lv_values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
71 int a2b = _value_dominates(a, b);
72 int b2a = _value_dominates(b, a);
74 /* If there is no dominance relation, they do not interfere. */
76 const ir_edge_t *edge;
80 * Adjust a and b so, that a dominates b if
81 * a dominates b or vice versa.
89 bb = get_nodes_block(b);
92 * If a is live end in b's block it is
93 * live at b's definition (a dominates b)
95 if(be_is_live_end(lv, bb, a))
99 * Look at all usages of a.
100 * If there's one usage of a in the block of b, then
101 * we check, if this use is dominated by b, if that's true
102 * a and b interfere. Note that b must strictly dominate the user,
103 * since if b is the last user of in the block, b and a do not
105 * Uses of a not in b's block can be disobeyed, because the
106 * check for a being live at the end of b's block is already
109 foreach_out_edge(a, edge) {
110 const ir_node *user = get_edge_src_irn(edge);
111 if(get_nodes_block(user) == bb && !is_Phi(user) && b != user && _value_dominates(b, user))
122 * Check if a node dominates a use.
123 * Note that the use of a phi is in its corresponding predecessor.
124 * @param irn The node.
125 * @param edge The use.
126 * @return 1, if @p irn dominates the use @p edge.
128 static INLINE int _dominates_use(const ir_node *irn, const ir_edge_t *edge)
130 ir_node *use = get_edge_src_irn(edge);
133 int pos = get_edge_src_pos(edge);
134 ir_node *phi_bl = get_nodes_block(use);
135 ir_node *use_bl = get_Block_cfgpred_block(phi_bl, pos);
136 ir_node *irn_bl = get_nodes_block(irn);
137 return block_dominates(irn_bl, use_bl);
140 return _value_dominates(irn, use);
144 * Check if a node strictly dominates a use.
145 * Note that the use of a phi is in its corresponding predecessor.
146 * @param irn The node.
147 * @param edge The use.
148 * @return 1, if @p irn strictly dominates the use @p edge.
150 static INLINE int _strictly_dominates_use(const ir_node *irn, const ir_edge_t *edge)
152 return get_edge_src_irn(edge) != irn && _dominates_use(irn, edge);
156 * Check, if a node is live in front of another.
157 * @param birg The backend irg.
158 * @param irn The node.
159 * @param where The location to check for.
160 * @return 1, if @p irn is live in front of @p where.
162 static INLINE int _be_lv_chk_before_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
164 const be_lv_t *lv = be_get_birg_liveness(birg);
165 const ir_edge_t *edge;
167 /* the node must strictly dominate the location, else it cannot be live there. */
168 if (!_value_dominates(irn, where) || irn == where)
172 * now that it is clear that it strictly dominates the location it is surely live
173 * if it is also live end at the block.
175 if (be_is_live_end(lv, get_nodes_block(where), irn))
179 * If the node is not live out, we have to check if there
180 * is a use which is dominated by the location.
182 foreach_out_edge (irn, edge) {
183 if (_dominates_use(where, edge))
191 * Check, if a node is live after another node.
192 * @param birg The backend irg.
193 * @param irn The node.
194 * @param where The location to check for.
195 * @return 1, if @p irn is live after @p where.
197 static INLINE int _be_lv_chk_after_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
199 const be_lv_t *lv = be_get_birg_liveness(birg);
200 const ir_edge_t *edge;
202 if (!_value_dominates(irn, where))
205 if (be_is_live_end(lv, get_nodes_block(where), irn))
208 foreach_out_edge (irn, edge) {
209 if (_strictly_dominates_use(where, edge))
216 #define value_dominates_intrablock(a, b) _value_dominates_intrablock(a, b)
217 #define value_dominates(a, b) _value_dominates(a, b)
218 #define values_interfere(birg, a, b) _lv_values_interfere(be_get_birg_liveness(birg), a, b)
219 #define dominates_use(a, e) _dominates_use(a, e)
220 #define strictly_dominates_use(a, e) _strictly_dominates_use(a, e)
221 #define be_lv_chk_before_irn(birg, a, b) _be_lv_chk_before_irn(birg, a, b)
222 #define be_lv_chk_after_irn(birg, a, b) _be_lv_chk_after_irn(birg, a, b)
224 #endif /* _BELIVECHK_T_H */