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