2 * Scheduling algorithms.
3 * Just a simple list scheduling algorithm is here.
5 * @author Sebastian Hack
21 #include "irprintf_t.h"
24 #include "besched_t.h"
26 #include "belistsched.h"
29 * Scheduling environment for the whole graph.
31 typedef struct _sched_env_t {
32 const ir_graph *irg; /**< The graph to schedule. */
33 list_sched_selector_t *select; /**< The node selector. */
34 void *select_env; /**< A pointer to give to the selector. */
37 ir_node *trivial_selector(void *env, ir_node *block, int curr_time,
38 pset *already_scheduled, pset *ready_list)
40 ir_node *res = pset_first(ready_list);
41 pset_break(ready_list);
45 static void list_sched_block(ir_node *block, void *env_ptr);
47 void list_sched(ir_graph *irg, list_sched_selector_t *selector, void *select_env)
51 memset(&env, 0, sizeof(env));
52 env.select = selector;
53 env.select_env = select_env;
56 /* Normalize proj nodes. */
57 normalize_proj_nodes(irg);
59 /* Compute the outs */
60 if(get_irg_outs_state(irg) != outs_consistent)
64 dump_ir_block_graph(irg, "-before-sched");
66 /* Schedule each single block. */
67 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
72 * Environment for a block scheduler.
74 typedef struct _block_sched_env_t {
77 pset *already_scheduled;
79 firm_dbg_module_t *dbg;
85 * Checks, if a node is to appear in a schedule. Such nodes either
86 * consume real data (mode datab) or produce such.
87 * @param irn The node to check for.
88 * @return 1, if the node consumes/produces data, false if not.
90 static INLINE int to_appear_in_schedule(ir_node *irn)
94 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
95 ir_node *op = get_irn_n(irn, i);
96 if(mode_is_datab(get_irn_mode(op)))
100 return mode_is_datab(get_irn_mode(irn));
105 * Try to put a node in the ready set.
106 * @param env The block scheduler environment.
107 * @param irn The node to make ready.
108 * @return 1, if the node could be made ready, 0 else.
110 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
114 /* Blocks cannot be scheduled. */
119 * Check, if the given ir node is in a different block as the
120 * currently scheduled one. If that is so, don't make the node ready.
122 if(env->block != get_nodes_block(irn))
125 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
126 ir_node *op = get_irn_n(irn, i);
128 /* If the operand is local to the scheduled block and not yet
129 * scheduled, this nodes cannot be made ready, so exit. */
130 if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
134 DBG((env->dbg, LEVEL_2, "\tmaking ready: %n\n", irn));
135 pset_insert_ptr(env->ready_set, irn);
141 * Check, if a node is ready in a block schedule.
142 * @param env The block schedule environment.
143 * @param irn The node to check for.
144 * @return 1 if the node was ready, 0 if not.
146 #define is_ready(env,irn) \
147 (pset_find_ptr((env)->ready_set, irn) != NULL)
150 * Check, if a node has already been schedules.
151 * @param env The block schedule environment.
152 * @param irn The node to check for.
153 * @return 1 if the node was already scheduled, 0 if not.
155 #define is_scheduled(env,irn) \
156 (pset_find_ptr((env)->already_scheduled, irn) != NULL)
159 * Try, to make all users of a node ready.
160 * In fact, a usage node can only be made ready, if all its operands
161 * have already been scheduled yet. This is checked my make_ready().
162 * @param env The block schedule environment.
163 * @param irn The node, which usages (successors) are to be made ready.
165 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
169 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
170 ir_node *user = get_irn_out(irn, i);
172 make_ready(env, user);
177 * Compare to nodes using pointer equality.
178 * @param p1 Node one.
179 * @param p2 Node two.
180 * @return 0 if they are identical.
182 static int node_cmp_func(const void *p1, const void *p2)
188 * Append an instruction to a schedule.
189 * @param env The block scheduleing environment.
190 * @param irn The node to add to the schedule.
191 * @return The given node.
193 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
195 /* If the node consumes/produces data, it is appended to the schedule
196 * list, otherwise, it is not put into the list */
197 if(to_appear_in_schedule(irn)) {
198 sched_info_t *info = get_irn_sched_info(irn);
199 INIT_LIST_HEAD(&info->list);
200 sched_add(env->block, irn);
202 DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
205 /* Insert the node in the set of all already scheduled nodes. */
206 pset_insert_ptr(env->already_scheduled, irn);
208 /* Remove the node from the ready set */
209 if(pset_find_ptr(env->ready_set, irn))
210 pset_remove_ptr(env->ready_set, irn);
217 * Add the proj nodes of a tuple-mode irn to the schedule immediately
218 * after the tuple-moded irn. By pinning the projs after the irn, no
219 * other nodes can create a new lifetime between the tuple-moded irn and
220 * one of its projs. This should render a realistic image of a
221 * tuple-moded irn, which in fact models a node which defines multiple
224 * @param irn The tuple-moded irn.
225 * @param list The schedule list to append all the projs.
226 * @param time The time step to which the irn and all its projs are
228 * @param obst The obstack the scheduling data structures shall be
230 * @param ready_set The ready set of the list scheduler.
231 * @param already_scheduled A set containing all nodes already
234 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
237 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
239 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
240 ir_node *out = get_irn_out(irn, i);
242 assert(is_Proj(out) && "successor of a modeT node must be a proj");
244 if(get_irn_mode(out) == mode_T)
245 add_tuple_projs(env, out);
247 add_to_sched(env, out);
248 make_users_ready(env, out);
254 * Perform list scheduling on a block.
256 * Note, that the caller must compute a linked list of nodes in the block
257 * using the link field before calling this function.
259 * Also the outs must have been computed.
261 * @param block The block node.
262 * @param env Schedulting environment.
264 static void list_sched_block(ir_node *block, void *env_ptr)
266 sched_env_t *env = env_ptr;
267 block_sched_env_t be;
272 sched_info_t *info = get_irn_sched_info(block);
274 /* Initialize the block's list head that will hold the schedule. */
275 INIT_LIST_HEAD(&info->list);
277 /* Initialize the block scheduling environment */
278 be.dbg = firm_dbg_register("firm.be.sched");
281 be.ready_set = new_pset(node_cmp_func, get_irn_n_outs(block));
282 be.already_scheduled = new_pset(node_cmp_func, get_irn_n_outs(block));
284 DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
286 /* Then one can add all nodes are ready to the set. */
289 for(i = 0, n = get_irn_n_outs(block); i < n; ++i) {
290 ir_node *irn = get_irn_out(block, i);
292 /* Skip the end node because of keepalive edges. */
293 if(get_irn_opcode(irn) == iro_End)
296 /* Phi functions are scheduled immediately, since they only transfer
297 * data flow from the predecessors to this block. */
299 add_to_sched(&be, irn);
300 make_users_ready(&be, irn);
304 /* Other nodes must have all operands in other blocks to be made
309 /* Check, if the operands of a node are not local to this block */
310 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
311 ir_node *operand = get_irn_n(irn, j);
313 if(get_nodes_block(operand) == block) {
319 /* Make the node ready, if all operands live in a foreign block */
321 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
322 make_ready(&be, irn);
327 /* Increase the time, if some phi functions have been scheduled */
328 be.curr_time += phi_seen;
330 while(pset_count(be.ready_set) > 0) {
331 DBG((be.dbg, LEVEL_2, "\tready set: %*n\n", pset_iterator, be.ready_set));
332 // pset_print(stdout, be.ready_set, irn_printer);
334 /* select a node to be scheduled and check if it was ready */
335 irn = env->select(env->select_env, block, be.curr_time,
336 be.already_scheduled, be.ready_set);
338 DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
340 /* Add the node to the schedule. */
341 add_to_sched(&be, irn);
343 if(get_irn_mode(irn) == mode_T)
344 add_tuple_projs(&be, irn);
346 make_users_ready(&be, irn);
348 /* Increase the time step. */
351 /* remove the scheduled node from the ready list. */
352 if(pset_find_ptr(be.ready_set, irn))
353 pset_remove_ptr(be.ready_set, irn);
356 del_pset(be.ready_set);
357 del_pset(be.already_scheduled);