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