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 const list_sched_selector_t *selector; /**< The node selector. */
37 void *selector_env; /**< A pointer to give to the selector. */
40 static ir_node *trivial_select(void *env, void *block_env, const struct list_head *sched_head,
41 int curr_time, pset *ready_set)
43 ir_node *res = pset_first(ready_set);
44 pset_break(ready_set);
48 static const list_sched_selector_t trivial_selector_struct = {
56 const list_sched_selector_t *trivial_selector = &trivial_selector_struct;
58 static void list_sched_block(ir_node *block, void *env_ptr);
60 void list_sched(ir_graph *irg, const list_sched_selector_t *selector)
64 memset(&env, 0, sizeof(env));
65 env.selector = selector;
66 env.selector_env = selector->init_graph ? selector->init_graph(irg) : NULL;
69 /* Normalize proj nodes. */
70 normalize_proj_nodes(irg);
72 /* Compute the outs */
73 if(get_irg_outs_state(irg) != outs_consistent)
77 //dump_ir_block_graph(irg, "-before-sched");
79 /* Schedule each single block. */
80 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
82 if(selector->finish_graph)
83 selector->finish_graph(env.selector_env, irg);
88 * Environment for a block scheduler.
90 typedef struct _block_sched_env_t {
93 pset *already_scheduled;
95 firm_dbg_module_t *dbg;
102 * Try to put a node in the ready set.
103 * @param env The block scheduler environment.
104 * @param irn The node to make ready.
105 * @return 1, if the node could be made ready, 0 else.
107 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
111 /* Blocks cannot be scheduled. */
116 * Check, if the given ir node is in a different block as the
117 * currently scheduled one. If that is so, don't make the node ready.
119 if(env->block != get_nodes_block(irn))
122 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
123 ir_node *op = get_irn_n(irn, i);
125 /* If the operand is local to the scheduled block and not yet
126 * scheduled, this nodes cannot be made ready, so exit. */
127 if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
131 DBG((env->dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
132 pset_insert_ptr(env->ready_set, irn);
138 * Check, if a node is ready in a block schedule.
139 * @param env The block schedule environment.
140 * @param irn The node to check for.
141 * @return 1 if the node was ready, 0 if not.
143 #define is_ready(env,irn) \
144 (pset_find_ptr((env)->ready_set, irn) != NULL)
147 * Check, if a node has already been schedules.
148 * @param env The block schedule environment.
149 * @param irn The node to check for.
150 * @return 1 if the node was already scheduled, 0 if not.
152 #define is_scheduled(env,irn) \
153 (pset_find_ptr((env)->already_scheduled, irn) != NULL)
156 * Try, to make all users of a node ready.
157 * In fact, a usage node can only be made ready, if all its operands
158 * have already been scheduled yet. This is checked my make_ready().
159 * @param env The block schedule environment.
160 * @param irn The node, which usages (successors) are to be made ready.
162 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
166 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
167 ir_node *user = get_irn_out(irn, i);
169 make_ready(env, user);
174 * Compare to nodes using pointer equality.
175 * @param p1 Node one.
176 * @param p2 Node two.
177 * @return 0 if they are identical.
179 static int node_cmp_func(const void *p1, const void *p2)
185 * Append an instruction to a schedule.
186 * @param env The block scheduleing environment.
187 * @param irn The node to add to the schedule.
188 * @return The given node.
190 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
192 /* If the node consumes/produces data, it is appended to the schedule
193 * list, otherwise, it is not put into the list */
194 if(to_appear_in_schedule(irn)) {
195 sched_info_t *info = get_irn_sched_info(irn);
196 INIT_LIST_HEAD(&info->list);
198 sched_add_before(env->block, irn);
200 DBG((env->dbg, LEVEL_2, "\tadding %+F\n", irn));
203 /* Insert the node in the set of all already scheduled nodes. */
204 pset_insert_ptr(env->already_scheduled, irn);
206 /* Remove the node from the ready set */
207 if(pset_find_ptr(env->ready_set, irn))
208 pset_remove_ptr(env->ready_set, irn);
215 * Add the proj nodes of a tuple-mode irn to the schedule immediately
216 * after the tuple-moded irn. By pinning the projs after the irn, no
217 * other nodes can create a new lifetime between the tuple-moded irn and
218 * one of its projs. This should render a realistic image of a
219 * tuple-moded irn, which in fact models a node which defines multiple
222 * @param irn The tuple-moded irn.
223 * @param list The schedule list to append all the projs.
224 * @param time The time step to which the irn and all its projs are
226 * @param obst The obstack the scheduling data structures shall be
228 * @param ready_set The ready set of the list scheduler.
229 * @param already_scheduled A set containing all nodes already
232 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
235 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
237 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
238 ir_node *out = get_irn_out(irn, i);
240 assert(is_Proj(out) && "successor of a modeT node must be a proj");
242 if(get_irn_mode(out) == mode_T)
243 add_tuple_projs(env, out);
245 add_to_sched(env, out);
246 make_users_ready(env, out);
252 * Perform list scheduling on a block.
254 * Note, that the caller must compute a linked list of nodes in the block
255 * using the link field before calling this function.
257 * Also the outs must have been computed.
259 * @param block The block node.
260 * @param env Schedulting environment.
262 static void list_sched_block(ir_node *block, void *env_ptr)
264 void *block_env = NULL;
265 sched_env_t *env = env_ptr;
266 block_sched_env_t be;
267 const list_sched_selector_t *selector = env->selector;
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 firm_dbg_set_mask(be.dbg, 0);
286 if(selector->init_block)
287 block_env = selector->init_block(env->selector_env, block);
289 DBG((be.dbg, LEVEL_1, "scheduling %+F\n", block));
291 /* Then one can add all nodes are ready to the set. */
292 for(i = 0, n = get_irn_n_outs(block); i < n; ++i) {
293 ir_node *irn = get_irn_out(block, i);
295 /* Skip the end node because of keepalive edges. */
296 if(get_irn_opcode(irn) == iro_End)
299 /* Phi functions are scheduled immediately, since they only transfer
300 * data flow from the predecessors to this block. */
302 add_to_sched(&be, irn);
303 make_users_ready(&be, irn);
307 /* Other nodes must have all operands in other blocks to be made
312 /* Check, if the operands of a node are not local to this block */
313 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
314 ir_node *operand = get_irn_n(irn, j);
316 if(get_nodes_block(operand) == block) {
322 /* Make the node ready, if all operands live in a foreign block */
324 DBG((be.dbg, LEVEL_2, "\timmediately ready: %+F\n", irn));
325 make_ready(&be, irn);
330 /* Increase the time, if some phi functions have been scheduled */
331 be.curr_time += phi_seen;
333 while(pset_count(be.ready_set) > 0) {
334 // DBG((be.dbg, LEVEL_2, "\tready set: %*n\n", pset_iterator, be.ready_set));
336 /* select a node to be scheduled and check if it was ready */
337 irn = selector->select(env->selector_env, block_env, &info->list, be.curr_time, be.ready_set);
339 DBG((be.dbg, LEVEL_3, "\tpicked node %+F\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 if(selector->finish_block)
358 selector->finish_block(env->selector_env, block_env, block);
360 del_pset(be.ready_set);
361 del_pset(be.already_scheduled);