2 * Scheduling algorithms.
3 * Just a simple list scheduling algorithm is here.
5 * @author Sebastian Hack
24 #include "irprintf_t.h"
27 #include "besched_t.h"
29 #include "belistsched.h"
32 * Scheduling environment for the whole graph.
34 typedef struct _sched_env_t {
35 const ir_graph *irg; /**< The graph to schedule. */
36 list_sched_selector_t *select; /**< The node selector. */
37 void *select_env; /**< A pointer to give to the selector. */
40 ir_node *trivial_selector(void *env, ir_node *block, int curr_time,
41 pset *already_scheduled, pset *ready_list)
43 ir_node *res = pset_first(ready_list);
44 pset_break(ready_list);
48 static void list_sched_block(ir_node *block, void *env_ptr);
50 void list_sched(ir_graph *irg, list_sched_selector_t *selector, void *select_env)
54 memset(&env, 0, sizeof(env));
55 env.select = selector;
56 env.select_env = select_env;
59 /* Normalize proj nodes. */
60 normalize_proj_nodes(irg);
62 /* Compute the outs */
63 if(get_irg_outs_state(irg) != outs_consistent)
67 dump_ir_block_graph(irg, "-before-sched");
69 /* Schedule each single block. */
70 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
75 * Environment for a block scheduler.
77 typedef struct _block_sched_env_t {
80 pset *already_scheduled;
82 firm_dbg_module_t *dbg;
88 * Checks, if a node is to appear in a schedule. Such nodes either
89 * consume real data (mode datab) or produce such.
90 * @param irn The node to check for.
91 * @return 1, if the node consumes/produces data, false if not.
93 static INLINE int to_appear_in_schedule(ir_node *irn)
97 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
98 ir_node *op = get_irn_n(irn, i);
99 if(mode_is_datab(get_irn_mode(op)))
103 return mode_is_datab(get_irn_mode(irn));
108 * Try to put a node in the ready set.
109 * @param env The block scheduler environment.
110 * @param irn The node to make ready.
111 * @return 1, if the node could be made ready, 0 else.
113 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
117 /* Blocks cannot be scheduled. */
122 * Check, if the given ir node is in a different block as the
123 * currently scheduled one. If that is so, don't make the node ready.
125 if(env->block != get_nodes_block(irn))
128 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
129 ir_node *op = get_irn_n(irn, i);
131 /* If the operand is local to the scheduled block and not yet
132 * scheduled, this nodes cannot be made ready, so exit. */
133 if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
137 DBG((env->dbg, LEVEL_2, "\tmaking ready: %n\n", irn));
138 pset_insert_ptr(env->ready_set, irn);
144 * Check, if a node is ready in a block schedule.
145 * @param env The block schedule environment.
146 * @param irn The node to check for.
147 * @return 1 if the node was ready, 0 if not.
149 #define is_ready(env,irn) \
150 (pset_find_ptr((env)->ready_set, irn) != NULL)
153 * Check, if a node has already been schedules.
154 * @param env The block schedule environment.
155 * @param irn The node to check for.
156 * @return 1 if the node was already scheduled, 0 if not.
158 #define is_scheduled(env,irn) \
159 (pset_find_ptr((env)->already_scheduled, irn) != NULL)
162 * Try, to make all users of a node ready.
163 * In fact, a usage node can only be made ready, if all its operands
164 * have already been scheduled yet. This is checked my make_ready().
165 * @param env The block schedule environment.
166 * @param irn The node, which usages (successors) are to be made ready.
168 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
172 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
173 ir_node *user = get_irn_out(irn, i);
175 make_ready(env, user);
180 * Compare to nodes using pointer equality.
181 * @param p1 Node one.
182 * @param p2 Node two.
183 * @return 0 if they are identical.
185 static int node_cmp_func(const void *p1, const void *p2)
191 * Append an instruction to a schedule.
192 * @param env The block scheduleing environment.
193 * @param irn The node to add to the schedule.
194 * @return The given node.
196 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
198 /* If the node consumes/produces data, it is appended to the schedule
199 * list, otherwise, it is not put into the list */
200 if(to_appear_in_schedule(irn)) {
201 sched_info_t *info = get_irn_sched_info(irn);
202 INIT_LIST_HEAD(&info->list);
203 sched_add(env->block, irn);
205 DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
208 /* Insert the node in the set of all already scheduled nodes. */
209 pset_insert_ptr(env->already_scheduled, irn);
211 /* Remove the node from the ready set */
212 if(pset_find_ptr(env->ready_set, irn))
213 pset_remove_ptr(env->ready_set, irn);
220 * Add the proj nodes of a tuple-mode irn to the schedule immediately
221 * after the tuple-moded irn. By pinning the projs after the irn, no
222 * other nodes can create a new lifetime between the tuple-moded irn and
223 * one of its projs. This should render a realistic image of a
224 * tuple-moded irn, which in fact models a node which defines multiple
227 * @param irn The tuple-moded irn.
228 * @param list The schedule list to append all the projs.
229 * @param time The time step to which the irn and all its projs are
231 * @param obst The obstack the scheduling data structures shall be
233 * @param ready_set The ready set of the list scheduler.
234 * @param already_scheduled A set containing all nodes already
237 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
240 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
242 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
243 ir_node *out = get_irn_out(irn, i);
245 assert(is_Proj(out) && "successor of a modeT node must be a proj");
247 if(get_irn_mode(out) == mode_T)
248 add_tuple_projs(env, out);
250 add_to_sched(env, out);
251 make_users_ready(env, out);
257 * Perform list scheduling on a block.
259 * Note, that the caller must compute a linked list of nodes in the block
260 * using the link field before calling this function.
262 * Also the outs must have been computed.
264 * @param block The block node.
265 * @param env Schedulting environment.
267 static void list_sched_block(ir_node *block, void *env_ptr)
269 sched_env_t *env = env_ptr;
270 block_sched_env_t be;
275 sched_info_t *info = get_irn_sched_info(block);
277 /* Initialize the block's list head that will hold the schedule. */
278 INIT_LIST_HEAD(&info->list);
280 /* Initialize the block scheduling environment */
281 be.dbg = firm_dbg_register("firm.be.sched");
284 be.ready_set = new_pset(node_cmp_func, get_irn_n_outs(block));
285 be.already_scheduled = new_pset(node_cmp_func, get_irn_n_outs(block));
287 DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
289 /* Then one can add all nodes are ready to the set. */
290 for(i = 0, n = get_irn_n_outs(block); i < n; ++i) {
291 ir_node *irn = get_irn_out(block, i);
293 /* Skip the end node because of keepalive edges. */
294 if(get_irn_opcode(irn) == iro_End)
297 /* Phi functions are scheduled immediately, since they only transfer
298 * data flow from the predecessors to this block. */
300 add_to_sched(&be, irn);
301 make_users_ready(&be, irn);
305 /* Other nodes must have all operands in other blocks to be made
310 /* Check, if the operands of a node are not local to this block */
311 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
312 ir_node *operand = get_irn_n(irn, j);
314 if(get_nodes_block(operand) == block) {
320 /* Make the node ready, if all operands live in a foreign block */
322 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
323 make_ready(&be, irn);
328 /* Increase the time, if some phi functions have been scheduled */
329 be.curr_time += phi_seen;
331 while(pset_count(be.ready_set) > 0) {
332 DBG((be.dbg, LEVEL_2, "\tready set: %*n\n", pset_iterator, be.ready_set));
333 // pset_print(stdout, be.ready_set, irn_printer);
335 /* select a node to be scheduled and check if it was ready */
336 irn = env->select(env->select_env, block, be.curr_time,
337 be.already_scheduled, be.ready_set);
339 DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
341 /* Add the node to the schedule. */
342 add_to_sched(&be, irn);
344 if(get_irn_mode(irn) == mode_T)
345 add_tuple_projs(&be, irn);
347 make_users_ready(&be, irn);
349 /* Increase the time step. */
352 /* remove the scheduled node from the ready list. */
353 if(pset_find_ptr(be.ready_set, irn))
354 pset_remove_ptr(be.ready_set, irn);
357 del_pset(be.ready_set);
358 del_pset(be.already_scheduled);