2 * Scheduling algorithms.
3 * Just a simple list scheduling algorithm is here.
5 * @author Sebastian Hack
21 #include "irprintf_t.h"
23 #include "besched_t.h"
25 #include "belistsched.h"
28 * Scheduling environment for the whole graph.
30 typedef struct _sched_env_t {
31 const ir_graph *irg; /**< The graph to schedule. */
32 list_sched_selector_t *select; /**< The node selector. */
33 void *select_env; /**< A pointer to give to the selector. */
36 ir_node *trivial_selector(void *env, ir_node *block, int curr_time,
37 pset *already_scheduled, pset *ready_list)
39 ir_node *res = pset_first(ready_list);
40 pset_break(ready_list);
44 static void list_sched_block(ir_node *block, void *env_ptr);
46 void list_sched(ir_graph *irg, list_sched_selector_t *selector, void *select_env)
50 memset(&env, 0, sizeof(env));
51 env.select = selector;
52 env.select_env = select_env;
55 /* Normalize proj nodes. */
56 normalize_proj_nodes(irg);
58 /* Compute the outs */
59 if(get_irg_outs_state(irg) != outs_consistent)
63 dump_ir_block_graph(irg, "-before-sched");
65 /* Schedule each single block. */
66 irg_block_walk_graph(irg, list_sched_block, NULL, &env);
71 * Environment for a block scheduler.
73 typedef struct _block_sched_env_t {
76 pset *already_scheduled;
83 * Checks, if a node is to appear in a schedule. Such nodes either
84 * consume real data (mode datab) or produce such.
85 * @param irn The node to check for.
86 * @return 1, if the node consumes/produces data, false if not.
88 static INLINE int to_appear_in_schedule(ir_node *irn)
92 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
93 ir_node *op = get_irn_n(irn, i);
94 if(mode_is_datab(get_irn_mode(op)))
98 return mode_is_datab(get_irn_mode(irn));
103 * Try to put a node in the ready set.
104 * @param env The block scheduler environment.
105 * @param irn The node to make ready.
106 * @return 1, if the node could be made ready, 0 else.
108 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
112 /* Blocks cannot be scheduled. */
117 * Check, if the given ir node is in a different block as the
118 * currently scheduled one. If that is so, don't make the node ready.
120 if(env->block != get_nodes_block(irn))
123 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
124 ir_node *op = get_irn_n(irn, i);
126 /* If the operand is local to the scheduled block and not yet
127 * scheduled, this nodes cannot be made ready, so exit. */
128 if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
132 ir_debugf("\tmaking ready: %n\n", irn);
133 pset_insert_ptr(env->ready_set, irn);
139 * Check, if a node is ready in a block schedule.
140 * @param env The block schedule environment.
141 * @param irn The node to check for.
142 * @return 1 if the node was ready, 0 if not.
144 #define is_ready(env,irn) \
145 (pset_find_ptr((env)->ready_set, irn) != NULL)
148 * Check, if a node has already been schedules.
149 * @param env The block schedule environment.
150 * @param irn The node to check for.
151 * @return 1 if the node was already scheduled, 0 if not.
153 #define is_scheduled(env,irn) \
154 (pset_find_ptr((env)->already_scheduled, irn) != NULL)
157 * Try, to make all users of a node ready.
158 * In fact, a usage node can only be made ready, if all its operands
159 * have already been scheduled yet. This is checked my make_ready().
160 * @param env The block schedule environment.
161 * @param irn The node, which usages (successors) are to be made ready.
163 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
167 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
168 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);
197 sched_add(env->block, irn);
199 ir_debugf("\tadding %n\n", irn);
202 /* Insert the node in the set of all already scheduled nodes. */
203 pset_insert_ptr(env->already_scheduled, irn);
205 /* Remove the node from the ready set */
206 if(pset_find_ptr(env->ready_set, irn))
207 pset_remove_ptr(env->ready_set, irn);
214 * Add the proj nodes of a tuple-mode irn to the schedule immediately
215 * after the tuple-moded irn. By pinning the projs after the irn, no
216 * other nodes can create a new lifetime between the tuple-moded irn and
217 * one of its projs. This should render a realistic image of a
218 * tuple-moded irn, which in fact models a node which defines multiple
221 * @param irn The tuple-moded irn.
222 * @param list The schedule list to append all the projs.
223 * @param time The time step to which the irn and all its projs are
225 * @param obst The obstack the scheduling data structures shall be
227 * @param ready_set The ready set of the list scheduler.
228 * @param already_scheduled A set containing all nodes already
231 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
234 assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
236 for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
237 ir_node *out = get_irn_out(irn, i);
239 assert(is_Proj(out) && "successor of a modeT node must be a proj");
241 if(get_irn_mode(out) == mode_T)
242 add_tuple_projs(env, out);
244 add_to_sched(env, out);
245 make_users_ready(env, out);
251 * Perform list scheduling on a block.
253 * Note, that the caller must compute a linked list of nodes in the block
254 * using the link field before calling this function.
256 * Also the outs must have been computed.
258 * @param block The block node.
259 * @param env Schedulting environment.
261 static void list_sched_block(ir_node *block, void *env_ptr)
263 sched_env_t *env = env_ptr;
264 block_sched_env_t be;
269 sched_info_t *info = get_irn_sched_info(block);
271 /* Initialize the block's list head that will hold the schedule. */
272 INIT_LIST_HEAD(&info->list);
274 /* Initialize the block scheduling environment */
277 be.ready_set = new_pset(node_cmp_func, get_irn_n_outs(block));
278 be.already_scheduled = new_pset(node_cmp_func, get_irn_n_outs(block));
280 ir_debugf("scheduling %n\n", block);
282 /* Then one can add all nodes are ready to the set. */
283 for(int i = 0, n = get_irn_n_outs(block); i < n; ++i) {
284 ir_node *irn = get_irn_out(block, i);
286 /* Phi functions are scheduled immediately, since they only transfer
287 * data flow from the predecessors to this block. */
289 add_to_sched(&be, irn);
290 make_users_ready(&be, irn);
294 /* Other nodes must have all operands in other blocks to be made
299 /* Check, if the operands of a node are not local to this block */
300 for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
301 ir_node *operand = get_irn_n(irn, j);
303 if(get_nodes_block(operand) == block) {
309 /* Make the node ready, if all operands live in a foreign block */
311 ir_debugf("\timmediately ready: %n\n", irn);
312 make_ready(&be, irn);
317 /* Increase the time, if some phi functions have been scheduled */
318 be.curr_time += phi_seen;
320 while(pset_count(be.ready_set) > 0) {
321 ir_debugf("\tready set: %*n\n", pset_iterator, be.ready_set);
322 // pset_print(stdout, be.ready_set, irn_printer);
324 /* select a node to be scheduled and check if it was ready */
325 irn = env->select(env->select_env, block, be.curr_time,
326 be.already_scheduled, be.ready_set);
328 ir_debugf("\tpicked node %n\n", irn);
330 /* Add the node to the schedule. */
331 add_to_sched(&be, irn);
333 if(get_irn_mode(irn) == mode_T)
334 add_tuple_projs(&be, irn);
336 make_users_ready(&be, irn);
338 /* Increase the time step. */
341 /* remove the scheduled node from the ready list. */
342 if(pset_find_ptr(be.ready_set, irn))
343 pset_remove_ptr(be.ready_set, irn);
346 del_pset(be.ready_set);
347 del_pset(be.already_scheduled);
351 list_for_each_entry(sched_info_t, inf, &info->list, list) {
352 ir_node *irn = get_sched_info_irn(inf);
353 ir_debugf("node: %n, pos: %d\n", irn, inf->time_step);