2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Scheduling utilities for nodes in Blocks and Blocks.
23 * @author Sebastian Hack
26 #ifndef FIRM_BE_BESCHED_H
27 #define FIRM_BE_BESCHED_H
38 void be_sched_dump(FILE *f, ir_graph *irg);
41 * returns the time step of a node. Each node in a block has a timestep
42 * unique to that block. a node schedule before another node has a lower
43 * timestep than this node.
45 int sched_get_time_step(const ir_node *irn);
46 int sched_has_next(const ir_node *irn);
47 int sched_has_prev(const ir_node *irn);
48 int sched_is_scheduled(const ir_node *irn);
49 ir_node *sched_next(const ir_node *irn);
50 ir_node *sched_prev(const ir_node *irn);
51 ir_node *sched_first(const ir_node *block);
52 ir_node *sched_last(const ir_node *block);
53 void sched_add_before(ir_node *before, ir_node *irn);
54 void sched_add_after(ir_node *after, ir_node *irn);
55 void sched_init_block(ir_node *block);
56 void sched_reset(ir_node *node);
57 int sched_comes_after(const ir_node *n1, const ir_node *n2);
58 void sched_remove(ir_node *irn);
60 #define sched_is_end(irn) is_Block(irn)
61 #define sched_is_begin(irn) is_Block(irn)
63 #define sched_foreach_from(from, irn) \
64 for(irn = from; !sched_is_end(irn); irn = sched_next(irn))
66 #define sched_foreach_reverse_from(from, irn) \
67 for(irn = from; !sched_is_begin(irn); irn = sched_prev(irn))
70 * A shorthand macro for iterating over a schedule.
71 * @param block The block.
72 * @param irn A ir node pointer used as an iterator.
74 #define sched_foreach(block,irn) \
75 sched_foreach_from(sched_first(block), irn)
78 * A shorthand macro for reversely iterating over a schedule.
79 * @param block The block.
80 * @param irn A ir node pointer used as an iterator.
82 #define sched_foreach_reverse(block,irn) \
83 sched_foreach_reverse_from(sched_last(block), irn)
86 * A shorthand macro for iterating over all Phi nodes of a schedule.
87 * @param block The block.
88 * @param phi A ir node pointer used as an iterator.
90 #define sched_foreach_Phi(block,phi) \
91 for (phi = sched_first(block); is_Phi(phi); phi = sched_next(phi))
94 * Removes dead nodes from schedule
95 * @param irg the graph
97 void be_remove_dead_nodes_from_schedule(ir_graph *irg);
99 #define SCHED_INITIAL_GRANULARITY (1 << 14)
100 #define get_irn_sched_info(irn) (&be_get_info(skip_Proj_const(irn))->sched_info)
103 * Check, if the node is scheduled.
104 * @param irn The node.
105 * @return 1, if the node is scheduled, 0 if not.
107 static inline int _sched_is_scheduled(const ir_node *irn)
109 return get_irn_sched_info(irn)->next != NULL;
113 * Get the time step of an irn in a schedule.
114 * @param irn The node.
115 * @return The time step in the schedule.
117 static inline int _sched_get_time_step(const ir_node *irn)
119 assert(_sched_is_scheduled(irn));
120 return get_irn_sched_info(irn)->time_step;
124 * Checks, if a node is to appear in a schedule. Such nodes either
125 * consume real data (mode datab) or produce such.
126 * @param irn The node to check for.
127 * @return 1, if the node consumes/produces data, false if not.
129 static inline bool to_appear_in_schedule(const ir_node *irn)
131 switch(get_irn_opcode(irn)) {
145 return mode_is_data(get_irn_mode(irn));
147 return ! (arch_irn_get_flags(irn) & arch_irn_flags_not_scheduled);
152 * Check, if an ir_node has a scheduling successor.
153 * @param irn The ir node.
154 * @return 1, if the node has a scheduling successor, 0 if not.
156 static inline int _sched_has_next(const ir_node *irn)
158 const sched_info_t *info = get_irn_sched_info(irn);
159 const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
160 return info->next != block;
164 * Check, if an ir_node has a scheduling predecessor.
165 * @param irn The ir node.
166 * @return 1, if the node has a scheduling predecessor, 0 if not.
168 static inline int _sched_has_prev(const ir_node *irn)
170 const sched_info_t *info = get_irn_sched_info(irn);
171 const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
172 return info->prev != block;
176 * Get the scheduling successor of a node.
177 * @param irn The node.
178 * @return The next ir node in the schedule or the block, if the node has no next node.
180 static inline ir_node *_sched_next(const ir_node *irn)
182 const sched_info_t *info = get_irn_sched_info(irn);
187 * Get the scheduling predecessor of a node.
188 * @param irn The node.
189 * @return The next ir node in the schedule or the block, if the node has no predecessor.
192 static inline ir_node *_sched_prev(const ir_node *irn)
194 const sched_info_t *info = get_irn_sched_info(irn);
199 * Get the first node in a block schedule.
200 * @param block The block of which to get the schedule.
201 * @return The first node in the schedule or the block itself
202 * if there is no node in the schedule.
204 static inline ir_node *_sched_first(const ir_node *block)
206 assert(is_Block(block) && "Need a block here");
207 return _sched_next(block);
211 * Get the last node in a schedule.
212 * @param block The block to get the schedule for.
213 * @return The last ir node in a schedule, or the block itself
214 * if there is no node in the schedule.
216 static inline ir_node *_sched_last(const ir_node *block)
218 assert(is_Block(block) && "Need a block here");
219 return _sched_prev(block);
223 * Reassign the time steps in the schedule.
224 * @param block The schedule to update.
226 void sched_renumber(const ir_node *block);
228 static inline void _sched_set_time_stamp(const ir_node *irn)
230 sched_info_t *info = get_irn_sched_info(irn);
231 const sched_info_t *prev_info = get_irn_sched_info(info->prev);
232 const sched_info_t *next_info = get_irn_sched_info(info->next);
233 sched_timestep_t before_ts = prev_info->time_step;
234 sched_timestep_t after_ts = next_info->time_step;
237 * If we are the last, we can give us a big time step,
238 * else we have to compute our time step from our
241 if(before_ts >= after_ts) {
242 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
244 if (info->time_step <= before_ts) {
245 sched_renumber(get_nodes_block(irn));
248 sched_timestep_t ts = (before_ts + after_ts) / 2;
251 * If the resolution went out, we have to renumber
254 if(ts == before_ts || ts == after_ts)
255 sched_renumber(get_nodes_block(irn));
257 info->time_step = ts;
262 * Add a node to a block schedule.
263 * @param block The block to whose schedule the node shall be added to.
264 * @param irn The node to add.
265 * @return The given node.
267 static inline void _sched_add_before(ir_node *before, ir_node *irn)
269 sched_info_t *info = get_irn_sched_info(irn);
270 ir_node *next = before;
271 sched_info_t *next_info = get_irn_sched_info(next);
272 ir_node *prev = next_info->prev;
273 sched_info_t *prev_info = get_irn_sched_info(prev);
274 assert(_sched_is_scheduled(before));
275 assert(!_sched_is_scheduled(irn));
276 assert(!is_Proj(before));
277 assert(!is_Proj(irn));
281 prev_info->next = irn;
282 next_info->prev = irn;
283 _sched_set_time_stamp(irn);
287 * Add a node to a block schedule.
288 * @param block The block to whose schedule the node shall be added to.
289 * @param irn The node to add.
290 * @return The given node.
292 static inline void _sched_add_after(ir_node *after, ir_node *irn)
294 sched_info_t *info = get_irn_sched_info(irn);
295 ir_node *prev = after;
296 sched_info_t *prev_info = get_irn_sched_info(prev);
297 ir_node *next = prev_info->next;
298 sched_info_t *next_info = get_irn_sched_info(next);
299 assert(_sched_is_scheduled(after));
300 assert(!_sched_is_scheduled(irn));
301 assert(!is_Proj(after));
302 assert(!is_Proj(irn));
306 prev_info->next = irn;
307 next_info->prev = irn;
308 _sched_set_time_stamp(irn);
311 static inline void _sched_init_block(ir_node *block)
313 sched_info_t *info = get_irn_sched_info(block);
314 assert(info->next == NULL && info->time_step == 0);
319 static inline void _sched_reset(ir_node *node)
321 sched_info_t *info = get_irn_sched_info(node);
327 * Remove a node from the scheduled.
328 * @param irn The node.
330 static inline void _sched_remove(ir_node *irn)
332 sched_info_t *info = get_irn_sched_info(irn);
333 ir_node *prev = info->prev;
334 ir_node *next = info->next;
335 sched_info_t *prev_info = get_irn_sched_info(prev);
336 sched_info_t *next_info = get_irn_sched_info(next);
337 assert(_sched_is_scheduled(irn));
339 prev_info->next = next;
340 next_info->prev = prev;
346 * Compare two nodes according to their position in the schedule.
347 * @param a The first node.
348 * @param b The second node.
349 * @return A number smaller, equals to or larger than 0, if a is
350 * before, the same, or after b in the schedule.
352 static inline int _sched_cmp(const ir_node *a, const ir_node *b)
354 assert(_sched_is_scheduled(a) && _sched_is_scheduled(b));
355 assert(get_nodes_block(a) == get_nodes_block(b));
357 return get_irn_sched_info(a)->time_step - get_irn_sched_info(b)->time_step;
361 * Checks, if one node is scheduled before another.
363 * @param n2 Another node.
364 * @return 1, if n1 is in front of n2 in the schedule, 0 else.
365 * @note Both nodes must be in the same block.
367 static inline int _sched_comes_after(const ir_node *n1, const ir_node *n2)
369 assert(_sched_is_scheduled(n1));
370 assert(_sched_is_scheduled(n2));
371 assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
372 return _sched_get_time_step(n1) < _sched_get_time_step(n2);
376 * A predicate for a node.
377 * @param irn The node.
378 * @param data The custom data.
379 * @return 1 if irn should be skipped. Else 0.
381 typedef int (sched_predicator_t)(const ir_node *irn, void *data);
384 * Predicate for sched_skip(), returns non-zero if irn is a control flow changing node.
386 * @param irn the node to evaluate
389 int sched_skip_cf_predicator(const ir_node *irn, void *data);
392 * Predicate for sched_skip(), returns non-zero if irn is a Phi node.
394 * Used with sched_skip().
396 * @param irn the node to evaluate
399 int sched_skip_phi_predicator(const ir_node *irn, void *data);
402 * Skip nodes in a schedule.
403 * @param from The node to start from.
404 * @param forward The direction (1 for forward, 0 for backward).
405 * @param predicator The predicator function which decides what is skipped.
406 * @param data context parameter for the predicator.
408 * @return The first node not rejected by the predicator or the block
409 * itself if all nodes were rejected.
411 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data);
413 #define sched_get_time_step(irn) _sched_get_time_step(irn)
414 #define sched_has_next(irn) _sched_has_next(irn)
415 #define sched_has_prev(irn) _sched_has_prev(irn)
416 #define sched_next(irn) _sched_next(irn)
417 #define sched_prev(irn) _sched_prev(irn)
418 #define sched_first(irn) _sched_first(irn)
419 #define sched_last(irn) _sched_last(irn)
420 #define sched_add_before(before, irn) _sched_add_before(before, irn)
421 #define sched_add_after(after, irn) _sched_add_after(after, irn)
422 #define sched_remove(irn) _sched_remove(irn)
423 #define sched_is_scheduled(irn) _sched_is_scheduled(irn)
424 #define sched_comes_after(n1, n2) _sched_comes_after(n1, n2)
425 #define sched_cmp(a, b) _sched_cmp(a, b)