config.h added
[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         sched_add(env->block, irn);
204
205         DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
206     }
207
208     /* Insert the node in the set of all already scheduled nodes. */
209     pset_insert_ptr(env->already_scheduled, irn);
210
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);
214
215     return irn;
216 }
217
218
219 /**
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
225  * values.
226  *
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
230  * related to.
231  * @param obst The obstack the scheduling data structures shall be
232  * created upon.
233  * @param ready_set The ready set of the list scheduler.
234  * @param already_scheduled A set containing all nodes already
235  * scheduled.
236  */
237 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
238 {
239     int i, n;
240     assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
241
242     for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
243         ir_node *out = get_irn_out(irn, i);
244
245         assert(is_Proj(out) && "successor of a modeT node must be a proj");
246
247         if(get_irn_mode(out) == mode_T)
248             add_tuple_projs(env, out);
249         else {
250             add_to_sched(env, out);
251             make_users_ready(env, out);
252         }
253     }
254 }
255
256 /**
257  * Perform list scheduling on a block.
258  *
259  * Note, that the caller must compute a linked list of nodes in the block
260  * using the link field before calling this function.
261  *
262  * Also the outs must have been computed.
263  *
264  * @param block The block node.
265  * @param env Schedulting environment.
266  */
267 static void list_sched_block(ir_node *block, void *env_ptr)
268 {
269     sched_env_t *env = env_ptr;
270     block_sched_env_t be;
271
272     ir_node *irn;
273     int i, n, j, m;
274     int phi_seen = 0;
275     sched_info_t *info = get_irn_sched_info(block);
276
277     /* Initialize the block's list head that will hold the schedule. */
278     INIT_LIST_HEAD(&info->list);
279
280     /* Initialize the block scheduling environment */
281                 be.dbg = firm_dbg_register("firm.be.sched");
282     be.block = block;
283     be.curr_time = 0;
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));
286
287     DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
288
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);
292
293                                 /* Skip the end node because of keepalive edges. */
294                                 if(get_irn_opcode(irn) == iro_End)
295                                         continue;
296
297         /* Phi functions are scheduled immediately, since they only transfer
298          * data flow from the predecessors to this block. */
299         if(is_Phi(irn)) {
300             add_to_sched(&be, irn);
301             make_users_ready(&be, irn);
302             phi_seen = 1;
303         }
304
305         /* Other nodes must have all operands in other blocks to be made
306          * ready */
307         else {
308             bool ready = true;
309
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);
313
314                 if(get_nodes_block(operand) == block) {
315                     ready = false;
316                     break;
317                 }
318             }
319
320             /* Make the node ready, if all operands live in a foreign block */
321             if(ready) {
322                 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
323                 make_ready(&be, irn);
324             }
325         }
326     }
327
328     /* Increase the time, if some phi functions have been scheduled */
329     be.curr_time += phi_seen;
330
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);
334
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);
338
339         DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
340
341         /* Add the node to the schedule. */
342         add_to_sched(&be, irn);
343
344         if(get_irn_mode(irn) == mode_T)
345             add_tuple_projs(&be, irn);
346         else
347             make_users_ready(&be, irn);
348
349         /* Increase the time step. */
350         be.curr_time += 1;
351
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);
355     }
356
357     del_pset(be.ready_set);
358     del_pset(be.already_scheduled);
359 }