2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief data structures for scheduling nodes in basic blocks.
9 * @author Sebastian Hack, Matthias Braun
11 #ifndef FIRM_BE_BESCHED_H
12 #define FIRM_BE_BESCHED_H
18 static sched_info_t *get_irn_sched_info(const ir_node *node)
20 return &be_get_info(skip_Proj_const(node))->sched_info;
24 * Check, if the node is scheduled.
25 * Block nodes are reported as scheduled as they mark the begin and end
26 * of the scheduling list.
27 * @param irn The node.
28 * @return 1, if the node is scheduled, 0 if not.
30 static inline bool sched_is_scheduled(const ir_node *irn)
32 return get_irn_sched_info(irn)->next != NULL;
36 * Returns the time step of a node. Each node in a block has a timestep
37 * unique to that block. A node schedule before another node has a lower
38 * timestep than this node.
39 * @param irn The node.
40 * @return The time step in the schedule.
42 static inline sched_timestep_t sched_get_time_step(const ir_node *irn)
44 assert(sched_is_scheduled(irn));
45 return get_irn_sched_info(irn)->time_step;
48 static inline bool sched_is_end(const ir_node *node)
50 return is_Block(node);
53 static inline bool sched_is_begin(const ir_node *node)
55 return is_Block(node);
59 * Get the scheduling successor of a node.
60 * @param irn The node.
61 * @return The next ir node in the schedule or the block, if the node has no next node.
63 static inline ir_node *sched_next(const ir_node *irn)
65 const sched_info_t *info = get_irn_sched_info(irn);
70 * Get the scheduling predecessor of a node.
71 * @param irn The node.
72 * @return The next ir node in the schedule or the block, if the node has no predecessor.
75 static inline ir_node *sched_prev(const ir_node *irn)
77 const sched_info_t *info = get_irn_sched_info(irn);
82 * Get the first node in a block schedule.
83 * @param block The block of which to get the schedule.
84 * @return The first node in the schedule or the block itself
85 * if there is no node in the schedule.
87 static inline ir_node *sched_first(const ir_node *block)
89 assert(is_Block(block) && "Need a block here");
90 return sched_next(block);
94 * Get the last node in a schedule.
95 * @param block The block to get the schedule for.
96 * @return The last ir node in a schedule, or the block itself
97 * if there is no node in the schedule.
99 static inline ir_node *sched_last(const ir_node *block)
101 assert(is_Block(block) && "Need a block here");
102 return sched_prev(block);
106 * Add a node to a block schedule.
107 * @param irn The node to add.
108 * @return The given node.
110 void sched_add_before(ir_node *before, ir_node *irn);
114 * Add a node to a block schedule.
115 * @param irn The node to add.
116 * @return The given node.
118 void sched_add_after(ir_node *after, ir_node *irn);
120 static inline void sched_init_block(ir_node *block)
122 sched_info_t *info = get_irn_sched_info(block);
123 assert(info->next == NULL && info->time_step == 0);
128 static inline void sched_reset(ir_node *node)
130 sched_info_t *info = get_irn_sched_info(node);
136 * Remove a node from the scheduled.
137 * @param irn The node.
139 void sched_remove(ir_node *irn);
142 * Remove @p old from the schedule and put @p irn in its place.
144 void sched_replace(ir_node *old, ir_node *irn);
147 * Checks, if one node is scheduled before another.
149 * @param n2 Another node.
150 * @return true, if n1 is in front of n2 in the schedule, false else.
151 * @note Both nodes must be in the same block.
153 static inline bool sched_comes_after(const ir_node *n1, const ir_node *n2)
155 assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
156 return sched_get_time_step(n1) < sched_get_time_step(n2);
159 #define sched_foreach_after(after, irn) \
160 for (ir_node *irn = (after); !sched_is_end(irn = sched_next(irn));)
162 #define sched_foreach_reverse_before(before, irn) \
163 for (ir_node *irn = (before); !sched_is_begin(irn = sched_prev(irn));)
166 * A shorthand macro for iterating over a schedule.
167 * @param block The block.
168 * @param irn A ir node pointer used as an iterator.
170 #define sched_foreach(block,irn) \
171 sched_foreach_after((assert(is_Block(block)), block), irn)
174 * A shorthand macro for reversely iterating over a schedule.
175 * @param block The block.
176 * @param irn A ir node pointer used as an iterator.
178 #define sched_foreach_reverse(block,irn) \
179 sched_foreach_reverse_before((assert(is_Block(block)), block), irn)
182 * A shorthand macro for iterating over a schedule while the current node may be
183 * removed or replaced.
185 * @param block The block.
186 * @param irn A ir node pointer used as an iterator.
188 #define sched_foreach_safe(block, irn) \
189 for (ir_node *irn, *irn##__next = sched_first(block); !sched_is_end(irn = irn##__next) ? irn##__next = sched_next(irn), 1 : 0;)
192 * A shorthand macro for reversely iterating over a schedule while the current
193 * node may be removed or replaced.
195 * @param block The block.
196 * @param irn A ir node pointer used as an iterator.
198 #define sched_foreach_reverse_safe(block, irn) \
199 for (ir_node *irn, *irn##__prev = sched_last(block); !sched_is_begin(irn = irn##__prev) ? irn##__prev = sched_prev(irn), 1 : 0;)
202 * Type for a function scheduling a graph
204 typedef void (*schedule_func) (ir_graph *irg);
207 * Register new scheduling algorithm
209 void be_register_scheduler(const char *name, schedule_func func);
212 * schedule a graph with the currenty selected scheduler.
214 void be_schedule_graph(ir_graph *irg);