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"
22 #include "besched_t.h"
26 * Check dominance of two nodes in the same block.
27 * @param a The first node.
28 * @param b The second node.
29 * @return 1 if a comes before b in the same block or if a == b, 0 else.
31 static INLINE int _value_dominates_intrablock(const ir_node *a, const ir_node *b)
33 /* TODO: ? : can be removed?! */
34 sched_timestep_t as = sched_is_scheduled(a) ? sched_get_time_step(a) : 0;
35 sched_timestep_t bs = sched_is_scheduled(b) ? sched_get_time_step(b) : 0;
40 * Check strict dominance of two nodes in the same block.
41 * @param a The first node.
42 * @param b The second node.
43 * @return 1 if a comes before b in the same block, 0 else.
45 static INLINE int _value_strictly_dominates_intrablock(const ir_node *a, const ir_node *b)
47 /* TODO: ? : can be removed?! */
48 sched_timestep_t as = sched_is_scheduled(a) ? sched_get_time_step(a) : 0;
49 sched_timestep_t bs = sched_is_scheduled(b) ? sched_get_time_step(b) : 0;
54 * Check, if one value dominates the other.
55 * The dominance is not strict here.
56 * @param a The first node.
57 * @param b The second node.
58 * @return 1 if a dominates b or if a == b, 0 else.
60 static INLINE int _value_dominates(const ir_node *a, const ir_node *b)
62 const ir_node *block_a = get_block_const(a);
63 const ir_node *block_b = get_block_const(b);
66 * a and b are not in the same block,
67 * so dominance is determined by the dominance of the blocks.
69 if(block_a != block_b) {
70 return block_dominates(block_a, block_b);
74 * Dominance is determined by the time steps of the schedule.
76 return _value_dominates_intrablock(a, b);
80 * Check, if one value dominates the other.
81 * The dominance is strict here.
82 * @param a The first node.
83 * @param b The second node.
84 * @return 1 if a dominates b, 0 else.
86 static INLINE int _value_strictly_dominates(const ir_node *a, const ir_node *b)
88 const ir_node *block_a = get_block_const(a);
89 const ir_node *block_b = get_block_const(b);
92 * a and b are not in the same block,
93 * so dominance is determined by the dominance of the blocks.
95 if(block_a != block_b) {
96 return block_dominates(block_a, block_b);
100 * Dominance is determined by the time steps of the schedule.
102 return _value_strictly_dominates_intrablock(a, b);
106 * Check, if two values interfere.
107 * @param lv Liveness information (in the future we should use a be_irg_t here).
108 * @param a The first value.
109 * @param b The second value.
110 * @return 1, if a and b interfere, 0 if not.
112 static INLINE int _lv_values_interfere(const be_lv_t *lv, const ir_node *a, const ir_node *b)
114 int a2b = _value_dominates(a, b);
115 int b2a = _value_dominates(b, a);
118 stat_ev_ctx_push("beintlive");
121 * Adjust a and b so, that a dominates b if
122 * a dominates b or vice versa.
125 const ir_node *t = a;
131 /* If there is no dominance relation, they do not interfere. */
133 const ir_edge_t *edge;
134 ir_node *bb = get_nodes_block(b);
136 stat_ev_dbl("beintlive_ignore", arch_irn_is(lv->birg->main_env->arch_env, a, ignore));
139 * If a is live end in b's block it is
140 * live at b's definition (a dominates b)
142 if(be_is_live_end(lv, bb, a)) {
148 * Look at all usages of a.
149 * If there's one usage of a in the block of b, then
150 * we check, if this use is dominated by b, if that's true
151 * a and b interfere. Note that b must strictly dominate the user,
152 * since if b is the last user of in the block, b and a do not
154 * Uses of a not in b's block can be disobeyed, because the
155 * check for a being live at the end of b's block is already
158 foreach_out_edge(a, edge) {
159 const ir_node *user = get_edge_src_irn(edge);
160 if(get_nodes_block(user) == bb && !is_Phi(user) && _value_strictly_dominates(b, user)) {
168 stat_ev_ctx_pop("beintlive");
175 * Check if a node dominates a use.
176 * Note that the use of a phi is in its corresponding predecessor.
177 * @param irn The node.
178 * @param edge The use.
179 * @return 1, if @p irn dominates the use @p edge.
181 static INLINE int _dominates_use(const ir_node *irn, const ir_edge_t *edge)
183 ir_node *use = get_edge_src_irn(edge);
186 int pos = get_edge_src_pos(edge);
187 ir_node *phi_bl = get_nodes_block(use);
188 ir_node *use_bl = get_Block_cfgpred_block(phi_bl, pos);
189 ir_node *irn_bl = get_nodes_block(irn);
190 return block_dominates(irn_bl, use_bl);
193 return _value_dominates(irn, use);
197 * Check if a node strictly dominates a use.
198 * Note that the use of a phi is in its corresponding predecessor.
199 * @param irn The node.
200 * @param edge The use.
201 * @return 1, if @p irn strictly dominates the use @p edge.
203 static INLINE int _strictly_dominates_use(const ir_node *irn, const ir_edge_t *edge)
205 return get_edge_src_irn(edge) != irn && _dominates_use(irn, edge);
209 * Check, if a node is live in front of another.
210 * @param birg The backend irg.
211 * @param irn The node.
212 * @param where The location to check for.
213 * @return 1, if @p irn is live in front of @p where.
215 static INLINE int _be_lv_chk_before_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
217 const be_lv_t *lv = be_get_birg_liveness(birg);
218 const ir_edge_t *edge;
220 /* the node must strictly dominate the location, else it cannot be live there. */
221 if (!_value_dominates(irn, where) || irn == where)
225 * now that it is clear that it strictly dominates the location it is surely live
226 * if it is also live end at the block.
228 if (be_is_live_end(lv, get_nodes_block(where), irn))
232 * If the node is not live out, we have to check if there
233 * is a use which is dominated by the location.
235 foreach_out_edge (irn, edge) {
236 if (_dominates_use(where, edge))
244 * Check, if a node is live after another node.
245 * @param birg The backend irg.
246 * @param irn The node.
247 * @param where The location to check for.
248 * @return 1, if @p irn is live after @p where.
250 static INLINE int _be_lv_chk_after_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
252 const be_lv_t *lv = be_get_birg_liveness(birg);
253 const ir_edge_t *edge;
255 if (!_value_dominates(irn, where))
258 if (be_is_live_end(lv, get_nodes_block(where), irn))
261 foreach_out_edge (irn, edge) {
262 if (_strictly_dominates_use(where, edge))
269 #define value_dominates_intrablock(a, b) _value_dominates_intrablock(a, b)
270 #define value_dominates(a, b) _value_dominates(a, b)
271 #define values_interfere(birg, a, b) _lv_values_interfere(be_get_birg_liveness(birg), a, b)
272 #define dominates_use(a, e) _dominates_use(a, e)
273 #define strictly_dominates_use(a, e) _strictly_dominates_use(a, e)
274 #define be_lv_chk_before_irn(birg, a, b) _be_lv_chk_before_irn(birg, a, b)
275 #define be_lv_chk_after_irn(birg, a, b) _be_lv_chk_after_irn(birg, a, b)
277 #endif /* _BELIVECHK_T_H */