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