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