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"
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 be_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);
119 * Adjust a and b so, that a dominates b if
120 * a dominates b or vice versa.
123 const ir_node *t = a;
129 /* If there is no dominance relation, they do not interfere. */
131 const ir_edge_t *edge;
132 ir_node *bb = get_nodes_block(b);
134 //stat_ev_dbl("beintlive_ignore", arch_irn_is(lv->birg->main_env->arch_env, a, ignore));
137 * If a is live end in b's block it is
138 * live at b's definition (a dominates b)
140 if(be_is_live_end(lv, bb, a)) {
146 * Look at all usages of a.
147 * If there's one usage of a in the block of b, then
148 * we check, if this use is dominated by b, if that's true
149 * a and b interfere. Note that b must strictly dominate the user,
150 * since if b is the last user of in the block, b and a do not
152 * Uses of a not in b's block can be disobeyed, because the
153 * check for a being live at the end of b's block is already
156 foreach_out_edge(a, edge) {
157 const ir_node *user = get_edge_src_irn(edge);
158 if(get_nodes_block(user) == bb && !is_Phi(user) && _value_strictly_dominates(b, user)) {
172 * Check if a node dominates a use.
173 * Note that the use of a phi is in its corresponding predecessor.
174 * @param irn The node.
175 * @param edge The use.
176 * @return 1, if @p irn dominates the use @p edge.
178 static inline int _dominates_use(const ir_node *irn, const ir_edge_t *edge)
180 ir_node *use = get_edge_src_irn(edge);
183 int pos = get_edge_src_pos(edge);
184 ir_node *phi_bl = get_nodes_block(use);
185 ir_node *use_bl = get_Block_cfgpred_block(phi_bl, pos);
186 ir_node *irn_bl = get_nodes_block(irn);
187 return block_dominates(irn_bl, use_bl);
190 return _value_dominates(irn, use);
194 * Check if a node strictly dominates a use.
195 * Note that the use of a phi is in its corresponding predecessor.
196 * @param irn The node.
197 * @param edge The use.
198 * @return 1, if @p irn strictly dominates the use @p edge.
200 static inline int _strictly_dominates_use(const ir_node *irn, const ir_edge_t *edge)
202 return get_edge_src_irn(edge) != irn && _dominates_use(irn, edge);
206 * Check, if a node is live in front of another.
207 * @param birg The backend irg.
208 * @param irn The node.
209 * @param where The location to check for.
210 * @return 1, if @p irn is live in front of @p where.
212 static inline int _be_lv_chk_before_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
214 const be_lv_t *lv = be_get_birg_liveness(birg);
215 const ir_edge_t *edge;
217 /* the node must strictly dominate the location, else it cannot be live there. */
218 if (!_value_dominates(irn, where) || irn == where)
222 * now that it is clear that it strictly dominates the location it is surely live
223 * if it is also live end at the block.
225 if (be_is_live_end(lv, get_nodes_block(where), irn))
229 * If the node is not live out, we have to check if there
230 * is a use which is dominated by the location.
232 foreach_out_edge (irn, edge) {
233 if (_dominates_use(where, edge))
241 * Check, if a node is live after another node.
242 * @param birg The backend irg.
243 * @param irn The node.
244 * @param where The location to check for.
245 * @return 1, if @p irn is live after @p where.
247 static inline int _be_lv_chk_after_irn(const be_irg_t *birg, const ir_node *irn, const ir_node *where)
249 const be_lv_t *lv = be_get_birg_liveness(birg);
250 const ir_edge_t *edge;
252 if (!_value_dominates(irn, where))
255 if (be_is_live_end(lv, get_nodes_block(where), irn))
258 foreach_out_edge (irn, edge) {
259 if (_strictly_dominates_use(where, edge))
266 #define value_dominates_intrablock(a, b) _value_dominates_intrablock(a, b)
267 #define value_dominates(a, b) _value_dominates(a, b)
268 #define dominates_use(a, e) _dominates_use(a, e)
269 #define strictly_dominates_use(a, e) _strictly_dominates_use(a, e)
270 #define be_lv_chk_before_irn(birg, a, b) _be_lv_chk_before_irn(birg, a, b)
271 #define be_lv_chk_after_irn(birg, a, b) _be_lv_chk_after_irn(birg, a, b)
273 #endif /* _BELIVECHK_T_H */