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 info->time_step = env->curr_time;
204 sched_add(env->block, irn);
206 DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
209 /* Insert the node in the set of all already scheduled nodes. */
210 pset_insert_ptr(env->already_scheduled, irn);
212 /* Remove the node from the ready set */
213 if(pset_find_ptr(env->ready_set, irn))
214 pset_remove_ptr(env->ready_set, irn);
221 * Add the proj nodes of a tuple-mode irn to the schedule immediately
222 * after the tuple-moded irn. By pinning the projs after the irn, no
223 * other nodes can create a new lifetime between the tuple-moded irn and
224 * one of its projs. This should render a realistic image of a
225 * tuple-moded irn, which in fact models a node which defines multiple
228 * @param irn The tuple-moded irn.
229 * @param list The schedule list to append all the projs.
230 * @param time The time step to which the irn and all its projs are
232 * @param obst The obstack the scheduling data structures shall be
234 * @param ready_set The ready set of the list scheduler.
235 * @param already_scheduled A set containing all nodes already
238 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
241 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
243 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
244 ir_node *out = get_irn_out(irn, i);
246 assert(is_Proj(out) && "successor of a modeT node must be a proj");
248 if(get_irn_mode(out) == mode_T)
249 add_tuple_projs(env, out);
251 add_to_sched(env, out);
252 make_users_ready(env, out);
258 * Perform list scheduling on a block.
260 * Note, that the caller must compute a linked list of nodes in the block
261 * using the link field before calling this function.
263 * Also the outs must have been computed.
265 * @param block The block node.
266 * @param env Schedulting environment.
268 static void list_sched_block(ir_node *block, void *env_ptr)
270 sched_env_t *env = env_ptr;
271 block_sched_env_t be;
276 sched_info_t *info = get_irn_sched_info(block);
278 /* Initialize the block's list head that will hold the schedule. */
279 INIT_LIST_HEAD(&info->list);
281 /* Initialize the block scheduling environment */
282 be.dbg = firm_dbg_register("firm.be.sched");
285 be.ready_set = new_pset(node_cmp_func, get_irn_n_outs(block));
286 be.already_scheduled = new_pset(node_cmp_func, get_irn_n_outs(block));
288 DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
290 /* Then one can add all nodes are ready to the set. */
291 for(i = 0, n = get_irn_n_outs(block); i < n; ++i) {
292 ir_node *irn = get_irn_out(block, i);
294 /* Skip the end node because of keepalive edges. */
295 if(get_irn_opcode(irn) == iro_End)
298 /* Phi functions are scheduled immediately, since they only transfer
299 * data flow from the predecessors to this block. */
301 add_to_sched(&be, irn);
302 make_users_ready(&be, irn);
306 /* Other nodes must have all operands in other blocks to be made
311 /* Check, if the operands of a node are not local to this block */
312 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
313 ir_node *operand = get_irn_n(irn, j);
315 if(get_nodes_block(operand) == block) {
321 /* Make the node ready, if all operands live in a foreign block */
323 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
324 make_ready(&be, irn);
329 /* Increase the time, if some phi functions have been scheduled */
330 be.curr_time += phi_seen;
332 while(pset_count(be.ready_set) > 0) {
333 DBG((be.dbg, LEVEL_2, "\tready set: %*n\n", pset_iterator, be.ready_set));
334 // pset_print(stdout, be.ready_set, irn_printer);
336 /* select a node to be scheduled and check if it was ready */
337 irn = env->select(env->select_env, block, be.curr_time,
338 be.already_scheduled, be.ready_set);
340 DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
342 /* Add the node to the schedule. */
343 add_to_sched(&be, irn);
345 if(get_irn_mode(irn) == mode_T)
346 add_tuple_projs(&be, irn);
348 make_users_ready(&be, irn);
350 /* Increase the time step. */
353 /* remove the scheduled node from the ready list. */
354 if(pset_find_ptr(be.ready_set, irn))
355 pset_remove_ptr(be.ready_set, irn);
358 del_pset(be.ready_set);
359 del_pset(be.already_scheduled);