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 strict dominance of two nodes in the same block.
38 * @param a The first node.
39 * @param b The second node.
40 * @return 1 if a comes before b in the same block, 0 else.
42 static INLINE int _value_strictly_dominates_intrablock(const ir_node *a, const ir_node *b)
44 /* TODO: ? : can be removed?! */
45 sched_timestep_t as = is_Phi(a) ? 0 : sched_get_time_step(a);
46 sched_timestep_t bs = is_Phi(b) ? 0 : sched_get_time_step(b);
51 * Check, if one value dominates the other.
52 * The dominance is not strict here.
53 * @param a The first node.
54 * @param b The second node.
55 * @return 1 if a dominates b or if a == b, 0 else.
57 static INLINE int _value_dominates(const ir_node *a, const ir_node *b)
59 const ir_node *block_a = get_block(a);
60 const ir_node *block_b = get_block(b);
63 * a and b are not in the same block,
64 * so dominance is determined by the dominance of the blocks.
66 if(block_a != block_b) {
67 return block_dominates(block_a, block_b);
71 * Dominance is determined by the time steps of the schedule.
73 return _value_dominates_intrablock(a, b);
77 * Check, if one value dominates the other.
78 * The dominance is strict here.
79 * @param a The first node.
80 * @param b The second node.
81 * @return 1 if a dominates b, 0 else.
83 static INLINE int _value_strictly_dominates(const ir_node *a, const ir_node *b)
85 const ir_node *block_a = get_block(a);
86 const ir_node *block_b = get_block(b);
89 * a and b are not in the same block,
90 * so dominance is determined by the dominance of the blocks.
92 if(block_a != block_b) {
93 return block_dominates(block_a, block_b);
97 * Dominance is determined by the time steps of the schedule.
99 return _value_strictly_dominates_intrablock(a, b);
103 * Check, if two values interfere.
104 * @param lv Liveness information (in the future we should use a be_irg_t here).
105 * @param a The first value.
106 * @param b The second value.
107 * @return 1, if a and b interfere, 0 if not.
109 static INLINE int _lv_values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
111 int a2b = _value_dominates(a, b);
112 int b2a = _value_dominates(b, a);
114 /* If there is no dominance relation, they do not interfere. */
115 if((a2b | b2a) > 0) {
116 const ir_edge_t *edge;
120 * Adjust a and b so, that a dominates b if
121 * a dominates b or vice versa.
124 const ir_node *t = a;
129 bb = get_nodes_block(b);
132 * If a is live end in b's block it is
133 * live at b's definition (a dominates b)
135 if(be_is_live_end(lv, bb, a))
139 * Look at all usages of a.
140 * If there's one usage of a in the block of b, then
141 * we check, if this use is dominated by b, if that's true
142 * a and b interfere. Note that b must strictly dominate the user,
143 * since if b is the last user of in the block, b and a do not
145 * Uses of a not in b's block can be disobeyed, because the
146 * check for a being live at the end of b's block is already
149 foreach_out_edge(a, edge) {
150 const ir_node *user = get_edge_src_irn(edge);
151 if(get_nodes_block(user) == bb && !is_Phi(user) && _value_strictly_dominates(b, user))
162 * Check if a node dominates a use.
163 * Note that the use of a phi is in its corresponding predecessor.
164 * @param irn The node.
165 * @param edge The use.
166 * @return 1, if @p irn dominates the use @p edge.
168 static INLINE int _dominates_use(const ir_node *irn, const ir_edge_t *edge)
170 ir_node *use = get_edge_src_irn(edge);
173 int pos = get_edge_src_pos(edge);
174 ir_node *phi_bl = get_nodes_block(use);
175 ir_node *use_bl = get_Block_cfgpred_block(phi_bl, pos);
176 ir_node *irn_bl = get_nodes_block(irn);
177 return block_dominates(irn_bl, use_bl);
180 return _value_dominates(irn, use);
184 * Check if a node strictly dominates a use.
185 * Note that the use of a phi is in its corresponding predecessor.
186 * @param irn The node.
187 * @param edge The use.
188 * @return 1, if @p irn strictly dominates the use @p edge.
190 static INLINE int _strictly_dominates_use(const ir_node *irn, const ir_edge_t *edge)
192 return get_edge_src_irn(edge) != irn && _dominates_use(irn, edge);
196 * Check, if a node is live in front of another.
197 * @param birg The backend irg.
198 * @param irn The node.
199 * @param where The location to check for.
200 * @return 1, if @p irn is live in front of @p where.
202 static INLINE int _be_lv_chk_before_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
204 const be_lv_t *lv = be_get_birg_liveness(birg);
205 const ir_edge_t *edge;
207 /* the node must strictly dominate the location, else it cannot be live there. */
208 if (!_value_dominates(irn, where) || irn == where)
212 * now that it is clear that it strictly dominates the location it is surely live
213 * if it is also live end at the block.
215 if (be_is_live_end(lv, get_nodes_block(where), irn))
219 * If the node is not live out, we have to check if there
220 * is a use which is dominated by the location.
222 foreach_out_edge (irn, edge) {
223 if (_dominates_use(where, edge))
231 * Check, if a node is live after another node.
232 * @param birg The backend irg.
233 * @param irn The node.
234 * @param where The location to check for.
235 * @return 1, if @p irn is live after @p where.
237 static INLINE int _be_lv_chk_after_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
239 const be_lv_t *lv = be_get_birg_liveness(birg);
240 const ir_edge_t *edge;
242 if (!_value_dominates(irn, where))
245 if (be_is_live_end(lv, get_nodes_block(where), irn))
248 foreach_out_edge (irn, edge) {
249 if (_strictly_dominates_use(where, edge))
256 #define value_dominates_intrablock(a, b) _value_dominates_intrablock(a, b)
257 #define value_dominates(a, b) _value_dominates(a, b)
258 #define values_interfere(birg, a, b) _lv_values_interfere(be_get_birg_liveness(birg), a, b)
259 #define dominates_use(a, e) _dominates_use(a, e)
260 #define strictly_dominates_use(a, e) _strictly_dominates_use(a, e)
261 #define be_lv_chk_before_irn(birg, a, b) _be_lv_chk_before_irn(birg, a, b)
262 #define be_lv_chk_after_irn(birg, a, b) _be_lv_chk_after_irn(birg, a, b)
264 #endif /* _BELIVECHK_T_H */