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
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);
135 * If a is live end in b's block it is
136 * live at b's definition (a dominates b)
138 if(be_is_live_end(lv, bb, a)) {
144 * Look at all usages of a.
145 * If there's one usage of a in the block of b, then
146 * we check, if this use is dominated by b, if that's true
147 * a and b interfere. Note that b must strictly dominate the user,
148 * since if b is the last user of in the block, b and a do not
150 * Uses of a not in b's block can be disobeyed, because the
151 * check for a being live at the end of b's block is already
154 foreach_out_edge(a, edge) {
155 const ir_node *user = get_edge_src_irn(edge);
156 if(get_nodes_block(user) == bb && !is_Phi(user) && _value_strictly_dominates(b, user)) {
170 * Check if a node dominates a use.
171 * Note that the use of a phi is in its corresponding predecessor.
172 * @param irn The node.
173 * @param edge The use.
174 * @return 1, if @p irn dominates the use @p edge.
176 static inline int _dominates_use(const ir_node *irn, const ir_edge_t *edge)
178 ir_node *use = get_edge_src_irn(edge);
181 int pos = get_edge_src_pos(edge);
182 ir_node *phi_bl = get_nodes_block(use);
183 ir_node *use_bl = get_Block_cfgpred_block(phi_bl, pos);
184 ir_node *irn_bl = get_nodes_block(irn);
185 return block_dominates(irn_bl, use_bl);
188 return _value_dominates(irn, use);
192 * Check if a node strictly dominates a use.
193 * Note that the use of a phi is in its corresponding predecessor.
194 * @param irn The node.
195 * @param edge The use.
196 * @return 1, if @p irn strictly dominates the use @p edge.
198 static inline int _strictly_dominates_use(const ir_node *irn, const ir_edge_t *edge)
200 return get_edge_src_irn(edge) != irn && _dominates_use(irn, edge);
204 * Check, if a node is live in front of another.
205 * @param irg The backend irg.
206 * @param irn The node.
207 * @param where The location to check for.
208 * @return 1, if @p irn is live in front of @p where.
210 static inline int _be_lv_chk_before_irn(ir_graph *irg, const ir_node *irn,
211 const ir_node *where)
213 const be_lv_t *lv = be_get_irg_liveness(irg);
214 const ir_edge_t *edge;
216 /* the node must strictly dominate the location, else it cannot be live there. */
217 if (!_value_dominates(irn, where) || irn == where)
221 * now that it is clear that it strictly dominates the location it is surely live
222 * if it is also live end at the block.
224 if (be_is_live_end(lv, get_nodes_block(where), irn))
228 * If the node is not live out, we have to check if there
229 * is a use which is dominated by the location.
231 foreach_out_edge (irn, edge) {
232 if (_dominates_use(where, edge))
240 * Check, if a node is live after another node.
241 * @param irg The backend irg.
242 * @param irn The node.
243 * @param where The location to check for.
244 * @return 1, if @p irn is live after @p where.
246 static inline int _be_lv_chk_after_irn(ir_graph *irg, const ir_node *irn,
247 const ir_node *where)
249 const be_lv_t *lv = be_get_irg_liveness(irg);
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(irg, a, b) _be_lv_chk_before_irn(irg, a, b)
271 #define be_lv_chk_after_irn(irg, a, b) _be_lv_chk_after_irn(irg, a, b)