2 * Copyright (C) 1995-2008 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, Matthias Braun
26 #ifndef FIRM_BE_BESCHED_T_H
27 #define FIRM_BE_BESCHED_T_H
29 #define SCHED_INITIAL_GRANULARITY (1 << 14)
33 #include "irgraph_t.h"
34 #include "irphase_t.h"
35 #include "irphases_t.h"
41 #define get_irn_sched_info(irn) (&be_get_info(skip_Proj_const(irn))->sched_info)
44 * Returns non-zero if schedule information is available
46 * @param irg The graph.
48 static inline int _have_sched_info(const ir_graph *irg)
50 return be_info_initialized(irg);
54 * Check, if the node is scheduled.
55 * @param irn The node.
56 * @return 1, if the node is scheduled, 0 if not.
58 static inline int _sched_is_scheduled(const ir_node *irn)
60 return get_irn_sched_info(irn)->next != NULL;
64 * Get the time step of an irn in a schedule.
65 * @param irn The node.
66 * @return The time step in the schedule.
68 static inline int _sched_get_time_step(const ir_node *irn)
70 assert(_sched_is_scheduled(irn));
71 return get_irn_sched_info(irn)->time_step;
75 * Checks, if a node is to appear in a schedule. Such nodes either
76 * consume real data (mode datab) or produce such.
77 * @param irn The node to check for.
78 * @return 1, if the node consumes/produces data, false if not.
80 static inline int to_appear_in_schedule(const ir_node *irn)
82 switch(get_irn_opcode(irn)) {
90 return is_data_node(irn);
95 * Check, if an ir_node has a scheduling successor.
96 * @param irn The ir node.
97 * @return 1, if the node has a scheduling successor, 0 if not.
99 static inline int _sched_has_next(const ir_node *irn)
101 const sched_info_t *info = get_irn_sched_info(irn);
102 const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
103 return info->next != block;
107 * Check, if an ir_node has a scheduling predecessor.
108 * @param irn The ir node.
109 * @return 1, if the node has a scheduling predecessor, 0 if not.
111 static inline int _sched_has_prev(const ir_node *irn)
113 const sched_info_t *info = get_irn_sched_info(irn);
114 const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
115 return info->prev != block;
119 * Get the scheduling successor of a node.
120 * @param irn The node.
121 * @return The next ir node in the schedule or the block, if the node has no next node.
123 static inline ir_node *_sched_next(const ir_node *irn)
125 const sched_info_t *info = get_irn_sched_info(irn);
130 * Get the scheduling predecessor of a node.
131 * @param irn The node.
132 * @return The next ir node in the schedule or the block, if the node has no predecessor.
135 static inline ir_node *_sched_prev(const ir_node *irn)
137 const sched_info_t *info = get_irn_sched_info(irn);
142 * Get the first node in a block schedule.
143 * @param block The block of which to get the schedule.
144 * @return The first node in the schedule or the block itself
145 * if there is no node in the schedule.
147 static inline ir_node *_sched_first(const ir_node *block)
149 assert(is_Block(block) && "Need a block here");
150 return _sched_next(block);
154 * Get the last node in a schedule.
155 * @param block The block to get the schedule for.
156 * @return The last ir node in a schedule, or the block itself
157 * if there is no node in the schedule.
159 static inline ir_node *_sched_last(const ir_node *block)
161 assert(is_Block(block) && "Need a block here");
162 return _sched_prev(block);
166 * Reassign the time steps in the schedule.
167 * @param block The schedule to update.
169 void sched_renumber(const ir_node *block);
171 static inline void _sched_set_time_stamp(const ir_node *irn)
173 sched_info_t *info = get_irn_sched_info(irn);
174 const sched_info_t *prev_info = get_irn_sched_info(info->prev);
175 const sched_info_t *next_info = get_irn_sched_info(info->next);
176 sched_timestep_t before_ts = prev_info->time_step;
177 sched_timestep_t after_ts = next_info->time_step;
180 * If we are the last, we can give us a big time step,
181 * else we have to compute our time step from our
184 if(before_ts >= after_ts) {
185 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
187 if (info->time_step <= before_ts) {
188 sched_renumber(get_nodes_block(irn));
191 sched_timestep_t ts = (before_ts + after_ts) / 2;
194 * If the resolution went out, we have to renumber
197 if(ts == before_ts || ts == after_ts)
198 sched_renumber(get_nodes_block(irn));
200 info->time_step = ts;
205 * Add a node to a block schedule.
206 * @param block The block to whose schedule the node shall be added to.
207 * @param irn The node to add.
208 * @return The given node.
210 static inline void _sched_add_before(ir_node *before, ir_node *irn)
212 sched_info_t *info = get_irn_sched_info(irn);
213 ir_node *next = before;
214 sched_info_t *next_info = get_irn_sched_info(next);
215 ir_node *prev = next_info->prev;
216 sched_info_t *prev_info = get_irn_sched_info(prev);
217 assert(_sched_is_scheduled(before));
218 assert(!_sched_is_scheduled(irn));
219 assert(!is_Proj(before));
220 assert(!is_Proj(irn));
224 prev_info->next = irn;
225 next_info->prev = irn;
226 _sched_set_time_stamp(irn);
230 * Add a node to a block schedule.
231 * @param block The block to whose schedule the node shall be added to.
232 * @param irn The node to add.
233 * @return The given node.
235 static inline void _sched_add_after(ir_node *after, ir_node *irn)
237 sched_info_t *info = get_irn_sched_info(irn);
238 ir_node *prev = after;
239 sched_info_t *prev_info = get_irn_sched_info(prev);
240 ir_node *next = prev_info->next;
241 sched_info_t *next_info = get_irn_sched_info(next);
242 assert(_sched_is_scheduled(after));
243 assert(!_sched_is_scheduled(irn));
244 assert(!is_Proj(after));
245 assert(!is_Proj(irn));
249 prev_info->next = irn;
250 next_info->prev = irn;
251 _sched_set_time_stamp(irn);
254 static inline void _sched_init_block(ir_node *block)
256 sched_info_t *info = get_irn_sched_info(block);
257 assert(info->next == NULL && info->time_step == 0);
262 static inline void _sched_reset(ir_node *node)
264 sched_info_t *info = get_irn_sched_info(node);
270 * Remove a node from the scheduled.
271 * @param irn The node.
273 static inline void _sched_remove(ir_node *irn)
275 sched_info_t *info = get_irn_sched_info(irn);
276 ir_node *prev = info->prev;
277 ir_node *next = info->next;
278 sched_info_t *prev_info = get_irn_sched_info(prev);
279 sched_info_t *next_info = get_irn_sched_info(next);
280 assert(_sched_is_scheduled(irn));
282 prev_info->next = next;
283 next_info->prev = prev;
289 * Compare two nodes according to their position in the schedule.
290 * @param a The first node.
291 * @param b The second node.
292 * @return A number smaller, equals to or larger than 0, if a is
293 * before, the same, or after b in the schedule.
295 static inline int _sched_cmp(const ir_node *a, const ir_node *b)
297 assert(_sched_is_scheduled(a) && _sched_is_scheduled(b));
298 assert(get_nodes_block(a) == get_nodes_block(b));
300 return get_irn_sched_info(a)->time_step - get_irn_sched_info(b)->time_step;
304 * Checks, if one node is scheduled before another.
306 * @param n2 Another node.
307 * @return 1, if n1 is in front of n2 in the schedule, 0 else.
308 * @note Both nodes must be in the same block.
310 static inline int _sched_comes_after(const ir_node *n1, const ir_node *n2)
312 assert(_sched_is_scheduled(n1));
313 assert(_sched_is_scheduled(n2));
314 assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
315 return _sched_get_time_step(n1) < _sched_get_time_step(n2);
319 * A predicate for a node.
320 * @param irn The node.
321 * @param data The custom data.
322 * @return 1 if irn should be skipped. Else 0.
324 typedef int (sched_predicator_t)(const ir_node *irn, void *data);
327 * Predicate for sched_skip(), returns non-zero if irn is a control flow changing node.
329 * @param irn the node to evaluate
332 int sched_skip_cf_predicator(const ir_node *irn, void *data);
335 * Predicate for sched_skip(), returns non-zero if irn is a Phi node.
337 * Used with sched_skip().
339 * @param irn the node to evaluate
342 int sched_skip_phi_predicator(const ir_node *irn, void *data);
345 * Skip nodes in a schedule.
346 * @param from The node to start from.
347 * @param forward The direction (1 for forward, 0 for backward).
348 * @param predicator The predicator function which decides what is skipped.
349 * @param data context parameter for the predicator.
351 * @return The first node not rejected by the predicator or the block
352 * itself if all nodes were rejected.
354 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data);
356 #define have_sched_info(irg) _have_sched_info(irg)
357 #define sched_get_time_step(irn) _sched_get_time_step(irn)
358 #define sched_has_next(irn) _sched_has_next(irn)
359 #define sched_has_prev(irn) _sched_has_prev(irn)
360 #define sched_next(irn) _sched_next(irn)
361 #define sched_prev(irn) _sched_prev(irn)
362 #define sched_first(irn) _sched_first(irn)
363 #define sched_last(irn) _sched_last(irn)
364 #define sched_add_before(before, irn) _sched_add_before(before, irn)
365 #define sched_add_after(after, irn) _sched_add_after(after, irn)
366 #define sched_remove(irn) _sched_remove(irn)
367 #define sched_is_scheduled(irn) _sched_is_scheduled(irn)
368 #define sched_comes_after(n1, n2) _sched_comes_after(n1, n2)
369 #define sched_cmp(a, b) _sched_cmp(a, b)
371 #endif /* FIRM_BE_BESCHED_T_H */