*** empty log message ***
[libfirm] / ir / be / belistsched.c
1 /**
2  * Scheduling algorithms.
3  * Just a simple list scheduling algorithm is here.
4  * @date 20.10.2004
5  * @author Sebastian Hack
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <stdio.h>
12 #include <stdarg.h>
13 #include <string.h>
14
15 #include "fourcc.h"
16 #include "obst.h"
17 #include "irouts.h"
18 #include "irgwalk.h"
19 #include "irnode_t.h"
20 #include "irmode_t.h"
21 #include "list.h"
22 #include "iterator.h"
23 #include "irdump.h"
24 #include "irprintf_t.h"
25 #include "debug.h"
26
27 #include "besched_t.h"
28 #include "beutil.h"
29 #include "belistsched.h"
30
31 /**
32  * Scheduling environment for the whole graph.
33  */
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. */
38 } sched_env_t;
39
40 ir_node *trivial_selector(void *env, ir_node *block, int curr_time,
41         pset *already_scheduled, pset *ready_list)
42 {
43     ir_node *res = pset_first(ready_list);
44     pset_break(ready_list);
45     return res;
46 }
47
48 static void list_sched_block(ir_node *block, void *env_ptr);
49
50 void list_sched(ir_graph *irg, list_sched_selector_t *selector, void *select_env)
51 {
52     sched_env_t env;
53
54     memset(&env, 0, sizeof(env));
55     env.select = selector;
56     env.select_env = select_env;
57     env.irg = irg;
58
59     /* Normalize proj nodes. */
60     normalize_proj_nodes(irg);
61
62     /* Compute the outs */
63     if(get_irg_outs_state(irg) != outs_consistent)
64         compute_outs(irg);
65
66     /* Dump the graph. */
67     //dump_ir_block_graph(irg, "-before-sched");
68
69     /* Schedule each single block. */
70     irg_block_walk_graph(irg, list_sched_block, NULL, &env);
71 }
72
73
74 /**
75  * Environment for a block scheduler.
76  */
77 typedef struct _block_sched_env_t {
78     int curr_time;
79     pset *ready_set;
80     pset *already_scheduled;
81     ir_node *block;
82                 firm_dbg_module_t *dbg;
83 } block_sched_env_t;
84
85
86
87 /**
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.
92  */
93 static INLINE int to_appear_in_schedule(ir_node *irn)
94 {
95     int i, n;
96
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)))
100             return 1;
101     }
102
103     return mode_is_datab(get_irn_mode(irn));
104 }
105
106
107 /**
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.
112  */
113 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
114 {
115     int i, n;
116
117     /* Blocks cannot be scheduled. */
118     if(is_Block(irn))
119         return 0;
120
121     /*
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.
124      */
125     if(env->block != get_nodes_block(irn))
126         return 0;
127
128     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
129         ir_node *op = get_irn_n(irn, i);
130
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)
134             return 0;
135     }
136
137     DBG((env->dbg, LEVEL_2, "\tmaking ready: %n\n", irn));
138     pset_insert_ptr(env->ready_set, irn);
139
140     return 1;
141 }
142
143 /**
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.
148  */
149 #define is_ready(env,irn) \
150   (pset_find_ptr((env)->ready_set, irn) != NULL)
151
152 /**
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.
157  */
158 #define is_scheduled(env,irn) \
159   (pset_find_ptr((env)->already_scheduled, irn) != NULL)
160
161 /**
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.
167  */
168 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
169 {
170     int i, n;
171
172     for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
173         ir_node *user = get_irn_out(irn, i);
174                                 if(!is_Phi(user))
175                                         make_ready(env, user);
176     }
177 }
178
179 /**
180  * Compare to nodes using pointer equality.
181  * @param p1 Node one.
182  * @param p2 Node two.
183  * @return 0 if they are identical.
184  */
185 static int node_cmp_func(const void *p1, const void *p2)
186 {
187     return p1 != p2;
188 }
189
190 /**
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.
195  */
196 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
197 {
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);
205
206         DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
207     }
208
209     /* Insert the node in the set of all already scheduled nodes. */
210     pset_insert_ptr(env->already_scheduled, irn);
211
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);
215
216     return irn;
217 }
218
219
220 /**
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
226  * values.
227  *
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
231  * related to.
232  * @param obst The obstack the scheduling data structures shall be
233  * created upon.
234  * @param ready_set The ready set of the list scheduler.
235  * @param already_scheduled A set containing all nodes already
236  * scheduled.
237  */
238 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
239 {
240     int i, n;
241     assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
242
243     for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
244         ir_node *out = get_irn_out(irn, i);
245
246         assert(is_Proj(out) && "successor of a modeT node must be a proj");
247
248         if(get_irn_mode(out) == mode_T)
249             add_tuple_projs(env, out);
250         else {
251             add_to_sched(env, out);
252             make_users_ready(env, out);
253         }
254     }
255 }
256
257 /**
258  * Perform list scheduling on a block.
259  *
260  * Note, that the caller must compute a linked list of nodes in the block
261  * using the link field before calling this function.
262  *
263  * Also the outs must have been computed.
264  *
265  * @param block The block node.
266  * @param env Schedulting environment.
267  */
268 static void list_sched_block(ir_node *block, void *env_ptr)
269 {
270     sched_env_t *env = env_ptr;
271     block_sched_env_t be;
272
273     ir_node *irn;
274     int i, n, j, m;
275     int phi_seen = 0;
276     sched_info_t *info = get_irn_sched_info(block);
277
278     /* Initialize the block's list head that will hold the schedule. */
279     INIT_LIST_HEAD(&info->list);
280
281     /* Initialize the block scheduling environment */
282                 be.dbg = firm_dbg_register("firm.be.sched");
283     be.block = block;
284     be.curr_time = 0;
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));
287
288     DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
289
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);
293
294                                 /* Skip the end node because of keepalive edges. */
295                                 if(get_irn_opcode(irn) == iro_End)
296                                         continue;
297
298         /* Phi functions are scheduled immediately, since they only transfer
299          * data flow from the predecessors to this block. */
300         if(is_Phi(irn)) {
301             add_to_sched(&be, irn);
302             make_users_ready(&be, irn);
303             phi_seen = 1;
304         }
305
306         /* Other nodes must have all operands in other blocks to be made
307          * ready */
308         else {
309             bool ready = true;
310
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);
314
315                 if(get_nodes_block(operand) == block) {
316                     ready = false;
317                     break;
318                 }
319             }
320
321             /* Make the node ready, if all operands live in a foreign block */
322             if(ready) {
323                 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
324                 make_ready(&be, irn);
325             }
326         }
327     }
328
329     /* Increase the time, if some phi functions have been scheduled */
330     be.curr_time += phi_seen;
331
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);
335
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);
339
340         DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
341
342         /* Add the node to the schedule. */
343         add_to_sched(&be, irn);
344
345         if(get_irn_mode(irn) == mode_T)
346             add_tuple_projs(&be, irn);
347         else
348             make_users_ready(&be, irn);
349
350         /* Increase the time step. */
351         be.curr_time += 1;
352
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);
356     }
357
358     del_pset(be.ready_set);
359     del_pset(be.already_scheduled);
360 }