Added workaround for end block keepalive edges.
[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
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <string.h>
11
12 #include "fourcc.h"
13 #include "obst.h"
14 #include "irouts.h"
15 #include "irgwalk.h"
16 #include "irnode_t.h"
17 #include "irmode_t.h"
18 #include "list.h"
19 #include "iterator.h"
20 #include "irdump.h"
21 #include "irprintf_t.h"
22 #include "debug.h"
23
24 #include "besched_t.h"
25 #include "beutil.h"
26 #include "belistsched.h"
27
28 /**
29  * Scheduling environment for the whole graph.
30  */
31 typedef struct _sched_env_t {
32     const ir_graph *irg;                        /**< The graph to schedule. */
33     list_sched_selector_t *select;  /**< The node selector. */
34     void *select_env;                               /**< A pointer to give to the selector. */
35 } sched_env_t;
36
37 ir_node *trivial_selector(void *env, ir_node *block, int curr_time,
38         pset *already_scheduled, pset *ready_list)
39 {
40     ir_node *res = pset_first(ready_list);
41     pset_break(ready_list);
42     return res;
43 }
44
45 static void list_sched_block(ir_node *block, void *env_ptr);
46
47 void list_sched(ir_graph *irg, list_sched_selector_t *selector, void *select_env)
48 {
49     sched_env_t env;
50
51     memset(&env, 0, sizeof(env));
52     env.select = selector;
53     env.select_env = select_env;
54     env.irg = irg;
55
56     /* Normalize proj nodes. */
57     normalize_proj_nodes(irg);
58
59     /* Compute the outs */
60     if(get_irg_outs_state(irg) != outs_consistent)
61         compute_outs(irg);
62
63     /* Dump the graph. */
64     dump_ir_block_graph(irg, "-before-sched");
65
66     /* Schedule each single block. */
67     irg_block_walk_graph(irg, list_sched_block, NULL, &env);
68 }
69
70
71 /**
72  * Environment for a block scheduler.
73  */
74 typedef struct _block_sched_env_t {
75     int curr_time;
76     pset *ready_set;
77     pset *already_scheduled;
78     ir_node *block;
79                 firm_dbg_module_t *dbg;
80 } block_sched_env_t;
81
82
83
84 /**
85  * Checks, if a node is to appear in a schedule. Such nodes either
86  * consume real data (mode datab) or produce such.
87  * @param irn The node to check for.
88  * @return 1, if the node consumes/produces data, false if not.
89  */
90 static INLINE int to_appear_in_schedule(ir_node *irn)
91 {
92     int i, n;
93
94     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
95         ir_node *op = get_irn_n(irn, i);
96         if(mode_is_datab(get_irn_mode(op)))
97             return 1;
98     }
99
100     return mode_is_datab(get_irn_mode(irn));
101 }
102
103
104 /**
105  * Try to put a node in the ready set.
106  * @param env The block scheduler environment.
107  * @param irn The node to make ready.
108  * @return 1, if the node could be made ready, 0 else.
109  */
110 static INLINE int make_ready(block_sched_env_t *env, ir_node *irn)
111 {
112     int i, n;
113
114     /* Blocks cannot be scheduled. */
115     if(is_Block(irn))
116         return 0;
117
118     /*
119      * Check, if the given ir node is in a different block as the
120      * currently scheduled one. If that is so, don't make the node ready.
121      */
122     if(env->block != get_nodes_block(irn))
123         return 0;
124
125     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
126         ir_node *op = get_irn_n(irn, i);
127
128         /* If the operand is local to the scheduled block and not yet
129          * scheduled, this nodes cannot be made ready, so exit. */
130         if(!pset_find_ptr(env->already_scheduled, op) && get_nodes_block(op) == env->block)
131             return 0;
132     }
133
134     DBG((env->dbg, LEVEL_2, "\tmaking ready: %n\n", irn));
135     pset_insert_ptr(env->ready_set, irn);
136
137     return 1;
138 }
139
140 /**
141  * Check, if a node is ready in a block schedule.
142  * @param env The block schedule environment.
143  * @param irn The node to check for.
144  * @return 1 if the node was ready, 0 if not.
145  */
146 #define is_ready(env,irn) \
147   (pset_find_ptr((env)->ready_set, irn) != NULL)
148
149 /**
150  * Check, if a node has already been schedules.
151  * @param env The block schedule environment.
152  * @param irn The node to check for.
153  * @return 1 if the node was already scheduled, 0 if not.
154  */
155 #define is_scheduled(env,irn) \
156   (pset_find_ptr((env)->already_scheduled, irn) != NULL)
157
158 /**
159  * Try, to make all users of a node ready.
160  * In fact, a usage node can only be made ready, if all its operands
161  * have already been scheduled yet. This is checked my make_ready().
162  * @param env The block schedule environment.
163  * @param irn The node, which usages (successors) are to be made ready.
164  */
165 static INLINE void make_users_ready(block_sched_env_t *env, ir_node *irn)
166 {
167     int i, n;
168
169     for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
170         ir_node *user = get_irn_out(irn, i);
171                                 if(!is_Phi(user))
172                                         make_ready(env, user);
173     }
174 }
175
176 /**
177  * Compare to nodes using pointer equality.
178  * @param p1 Node one.
179  * @param p2 Node two.
180  * @return 0 if they are identical.
181  */
182 static int node_cmp_func(const void *p1, const void *p2)
183 {
184     return p1 != p2;
185 }
186
187 /**
188  * Append an instruction to a schedule.
189  * @param env The block scheduleing environment.
190  * @param irn The node to add to the schedule.
191  * @return The given node.
192  */
193 static ir_node *add_to_sched(block_sched_env_t *env, ir_node *irn)
194 {
195     /* If the node consumes/produces data, it is appended to the schedule
196      * list, otherwise, it is not put into the list */
197     if(to_appear_in_schedule(irn)) {
198         sched_info_t *info = get_irn_sched_info(irn);
199         INIT_LIST_HEAD(&info->list);
200         sched_add(env->block, irn);
201
202         DBG((env->dbg, LEVEL_2, "\tadding %n\n", irn));
203     }
204
205     /* Insert the node in the set of all already scheduled nodes. */
206     pset_insert_ptr(env->already_scheduled, irn);
207
208     /* Remove the node from the ready set */
209     if(pset_find_ptr(env->ready_set, irn))
210         pset_remove_ptr(env->ready_set, irn);
211
212     return irn;
213 }
214
215
216 /**
217  * Add the proj nodes of a tuple-mode irn to the schedule immediately
218  * after the tuple-moded irn. By pinning the projs after the irn, no
219  * other nodes can create a new lifetime between the tuple-moded irn and
220  * one of its projs. This should render a realistic image of a
221  * tuple-moded irn, which in fact models a node which defines multiple
222  * values.
223  *
224  * @param irn The tuple-moded irn.
225  * @param list The schedule list to append all the projs.
226  * @param time The time step to which the irn and all its projs are
227  * related to.
228  * @param obst The obstack the scheduling data structures shall be
229  * created upon.
230  * @param ready_set The ready set of the list scheduler.
231  * @param already_scheduled A set containing all nodes already
232  * scheduled.
233  */
234 static void add_tuple_projs(block_sched_env_t *env, ir_node *irn)
235 {
236     int i, n;
237     assert(get_irn_mode(irn) == mode_T && "Mode of node must be tuple");
238
239     for(i = 0, n = get_irn_n_outs(irn); i < n; ++i) {
240         ir_node *out = get_irn_out(irn, i);
241
242         assert(is_Proj(out) && "successor of a modeT node must be a proj");
243
244         if(get_irn_mode(out) == mode_T)
245             add_tuple_projs(env, out);
246         else {
247             add_to_sched(env, out);
248             make_users_ready(env, out);
249         }
250     }
251 }
252
253 /**
254  * Perform list scheduling on a block.
255  *
256  * Note, that the caller must compute a linked list of nodes in the block
257  * using the link field before calling this function.
258  *
259  * Also the outs must have been computed.
260  *
261  * @param block The block node.
262  * @param env Schedulting environment.
263  */
264 static void list_sched_block(ir_node *block, void *env_ptr)
265 {
266     sched_env_t *env = env_ptr;
267     block_sched_env_t be;
268
269     ir_node *irn;
270     int j, m;
271     int phi_seen = 0;
272     sched_info_t *info = get_irn_sched_info(block);
273
274     /* Initialize the block's list head that will hold the schedule. */
275     INIT_LIST_HEAD(&info->list);
276
277     /* Initialize the block scheduling environment */
278                 be.dbg = firm_dbg_register("firm.be.sched");
279     be.block = block;
280     be.curr_time = 0;
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));
283
284     DBG((be.dbg, LEVEL_1, "scheduling %n\n", block));
285
286     /* Then one can add all nodes are ready to the set. */
287     int i;
288     int n;
289     for(i = 0, n = get_irn_n_outs(block); i < n; ++i) {
290         ir_node *irn = get_irn_out(block, i);
291
292                                 /* Skip the end node because of keepalive edges. */
293                                 if(get_irn_opcode(irn) == iro_End)
294                                         continue;
295
296         /* Phi functions are scheduled immediately, since they only transfer
297          * data flow from the predecessors to this block. */
298         if(is_Phi(irn)) {
299             add_to_sched(&be, irn);
300             make_users_ready(&be, irn);
301             phi_seen = 1;
302         }
303
304         /* Other nodes must have all operands in other blocks to be made
305          * ready */
306         else {
307             bool ready = true;
308
309             /* Check, if the operands of a node are not local to this block */
310             for(j = 0, m = get_irn_arity(irn); j < m; ++j) {
311                 ir_node *operand = get_irn_n(irn, j);
312
313                 if(get_nodes_block(operand) == block) {
314                     ready = false;
315                     break;
316                 }
317             }
318
319             /* Make the node ready, if all operands live in a foreign block */
320             if(ready) {
321                 DBG((be.dbg, LEVEL_2, "\timmediately ready: %n\n", irn));
322                 make_ready(&be, irn);
323             }
324         }
325     }
326
327     /* Increase the time, if some phi functions have been scheduled */
328     be.curr_time += phi_seen;
329
330     while(pset_count(be.ready_set) > 0) {
331         DBG((be.dbg, LEVEL_2, "\tready set: %*n\n", pset_iterator, be.ready_set));
332         // pset_print(stdout, be.ready_set, irn_printer);
333
334         /* select a node to be scheduled and check if it was ready */
335         irn = env->select(env->select_env, block, be.curr_time,
336                 be.already_scheduled, be.ready_set);
337
338         DBG((be.dbg, LEVEL_3, "\tpicked node %n\n", irn));
339
340         /* Add the node to the schedule. */
341         add_to_sched(&be, irn);
342
343         if(get_irn_mode(irn) == mode_T)
344             add_tuple_projs(&be, irn);
345         else
346             make_users_ready(&be, irn);
347
348         /* Increase the time step. */
349         be.curr_time += 1;
350
351         /* remove the scheduled node from the ready list. */
352         if(pset_find_ptr(be.ready_set, irn))
353             pset_remove_ptr(be.ready_set, irn);
354     }
355
356     del_pset(be.ready_set);
357     del_pset(be.already_scheduled);
358 }