handle Block_entity like other node attributes
[libfirm] / ir / be / belistsched.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
6  * This file may be distributed and/or modified under the terms of the
7  * GNU General Public License version 2 as published by the Free Software
8  * Foundation and appearing in the file LICENSE.GPL included in the
9  * packaging of this file.
10  *
11  * Licensees holding valid libFirm Professional Edition licenses may use
12  * this file in accordance with the libFirm Commercial License.
13  * Agreement provided with the Software.
14  *
15  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Primitive list scheduling with different node selectors.
23  * @author      Sebastian Hack
24  * @date        20.10.2004
25  */
26 #include "config.h"
27
28 #include <stdio.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <stdbool.h>
33
34 #include "benode.h"
35 #include "be_t.h"
36
37 #include "obst.h"
38 #include "list.h"
39 #include "iterator.h"
40
41 #include "iredges_t.h"
42 #include "irgwalk.h"
43 #include "irnode_t.h"
44 #include "irmode_t.h"
45 #include "irdump.h"
46 #include "irprintf_t.h"
47 #include "array.h"
48 #include "debug.h"
49 #include "irtools.h"
50
51 #include "bemodule.h"
52 #include "besched.h"
53 #include "beutil.h"
54 #include "belive_t.h"
55 #include "belistsched.h"
56 #include "bearch.h"
57 #include "bestat.h"
58 #include "beirg.h"
59
60 #include "lc_opts.h"
61 #include "lc_opts_enum.h"
62
63 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
64
65 /**
66  * Scheduling environment for the whole graph.
67  */
68 typedef struct sched_env_t {
69         unsigned *scheduled;                   /**< bitset of already scheduled nodes */
70         const list_sched_selector_t *selector; /**< The node selector. */
71         void *selector_env;                    /**< A pointer to give to the selector. */
72 } sched_env_t;
73
74 /**
75  * Environment for a block scheduler.
76  */
77 typedef struct block_sched_env_t {
78         /** scheduling info per node, copied from the global scheduler object */
79         unsigned                    *scheduled;
80         /** the set of candidates */
81         ir_nodeset_t                 cands;
82         ir_node                     *block;     /**< the current block */
83         sched_env_t                 *sched_env; /**< the scheduler environment */
84         const list_sched_selector_t *selector;
85         void                        *selector_block_env;
86 } block_sched_env_t;
87
88 /**
89  * Returns non-zero if the node is already scheduled
90  */
91 static bool is_already_scheduled(const sched_env_t *env, ir_node *n)
92 {
93         unsigned idx = get_irn_idx(n);
94         return rbitset_is_set(env->scheduled, idx);
95 }
96
97 /**
98  * Mark a node as already scheduled
99  */
100 static void set_already_scheduled(sched_env_t *env, ir_node *n)
101 {
102         unsigned idx = get_irn_idx(n);
103         rbitset_set(env->scheduled, idx);
104 }
105
106 static void selected(block_sched_env_t *env, ir_node *irn);
107 static void add_to_sched(block_sched_env_t *env, ir_node *irn);
108
109 /**
110  * Put a node in the ready set, or make it available immediately if it doesn't
111  * need to be scheduled
112  */
113 static void node_ready(block_sched_env_t *env, ir_node *pred, ir_node *irn)
114 {
115         if (is_Proj(irn)
116             || (arch_get_irn_flags(irn) & arch_irn_flags_not_scheduled)) {
117                 selected(env, irn);
118                 DB((dbg, LEVEL_3, "\tmaking immediately available: %+F\n", irn));
119         } else if (be_is_Keep(irn) || be_is_CopyKeep(irn)) {
120                 /* Keeps must be scheduled immediately */
121                 add_to_sched(env, irn);
122         } else {
123                 ir_nodeset_insert(&env->cands, irn);
124
125                 /* Notify selector about the ready node. */
126                 if (env->selector->node_ready)
127                         env->selector->node_ready(env->selector_block_env, irn, pred);
128
129                 DB((dbg, LEVEL_2, "\tmaking ready: %+F\n", irn));
130         }
131 }
132
133 /**
134  * Try to put a node in the ready set.
135  * @param env   The block scheduler environment.
136  * @param pred  The previous scheduled node.
137  * @param irn   The node to make ready.
138  * @return 1, if the node could be made ready, 0 else.
139  */
140 static void try_make_ready(block_sched_env_t *env, ir_node *pred, ir_node *irn)
141 {
142         int i, n;
143
144         /* we schedule one block at a time, so no need to consider users in other
145          * blocks */
146         if (is_Block(irn) || get_nodes_block(irn) != env->block)
147                 return;
148         if (is_Phi(irn) || is_End(irn))
149                 return;
150         /* check if all operands are already available */
151         n = get_irn_ins_or_deps(irn);
152         for (i = 0; i < n; ++i) {
153                 ir_node *op = get_irn_in_or_dep(irn, i);
154
155                 /* If the operand is local to the scheduled block and not yet
156                  * scheduled, this nodes cannot be made ready, so exit. */
157                 if (get_nodes_block(op) == env->block
158                                 && !is_already_scheduled(env->sched_env, op))
159                         return;
160         }
161
162         node_ready(env, pred, irn);
163 }
164
165 static void selected(block_sched_env_t *env, ir_node *node)
166 {
167         const ir_edge_t *edge;
168
169         /* notify the selector about the finally selected node. */
170         if (env->selector->node_selected)
171                 env->selector->node_selected(env->selector_block_env, node);
172
173     /* Insert the node in the set of all available scheduled nodes. */
174     set_already_scheduled(env->sched_env, node);
175
176     /* check users, they might be ready now */
177         foreach_out_edge(node, edge) {
178                 ir_node *user = get_edge_src_irn(edge);
179                 try_make_ready(env, node, user);
180         }
181         foreach_out_edge_kind(node, edge, EDGE_KIND_DEP) {
182                 ir_node *user = get_edge_src_irn(edge);
183                 try_make_ready(env, node, user);
184         }
185 }
186
187 /**
188  * Append an instruction to a schedule.
189  * @param env The block scheduling environment.
190  * @param irn The node to add to the schedule.
191  * @return    The given node.
192  */
193 static void add_to_sched(block_sched_env_t *env, ir_node *irn)
194 {
195         assert(! (arch_get_irn_flags(irn) & arch_irn_flags_not_scheduled));
196
197         sched_add_before(env->block, irn);
198
199         DB((dbg, LEVEL_2, "\tschedule %+F\n", irn));
200
201         /* Remove the node from the ready set */
202         ir_nodeset_remove(&env->cands, irn);
203
204         selected(env, irn);
205 }
206
207 /**
208  * Perform list scheduling on a block.
209  *
210  * Note, that the caller must compute a linked list of nodes in the block
211  * using the link field before calling this function.
212  *
213  * Also the outs must have been computed.
214  *
215  * @param block The block node.
216  * @param env Scheduling environment.
217  */
218 static void list_sched_block(ir_node *block, void *env_ptr)
219 {
220         sched_env_t *env                      = (sched_env_t*)env_ptr;
221         const list_sched_selector_t *selector = env->selector;
222
223         block_sched_env_t be;
224         const ir_edge_t *edge;
225         ir_nodeset_t *cands = &be.cands;
226
227         /* Initialize the block's list head that will hold the schedule. */
228         sched_init_block(block);
229
230         /* Initialize the block scheduling environment */
231         be.block     = block;
232         be.selector  = selector;
233         be.sched_env = env;
234         ir_nodeset_init_size(cands, get_irn_n_edges(block));
235
236         DB((dbg, LEVEL_1, "scheduling %+F\n", block));
237
238         if (selector->init_block)
239                 be.selector_block_env = selector->init_block(env->selector_env, block);
240
241         /* Then one can add all nodes are ready to the set. */
242         foreach_out_edge(block, edge) {
243                 ir_node *irn = get_edge_src_irn(edge);
244
245                 if (is_Phi(irn)) {
246                         /* Phi functions are scheduled immediately, since they only
247                          * transfer data flow from the predecessors to this block. */
248                         add_to_sched(&be, irn);
249                 } else if (be_is_Start(irn)) {
250                         /* The start block will be scheduled as the first node */
251                         add_to_sched(&be, irn);
252                 } else {
253                         try_make_ready(&be, NULL, irn);
254                 }
255         }
256
257         /* Iterate over all remaining nodes */
258         while (ir_nodeset_size(cands) > 0) {
259                 ir_node *irn = be.selector->select(be.selector_block_env, cands);
260                 DB((dbg, LEVEL_2, "\tpicked node %+F\n", irn));
261
262                 /* remove the scheduled node from the ready list. */
263                 ir_nodeset_remove(cands, irn);
264                 /* Add the node to the schedule. */
265                 add_to_sched(&be, irn);
266         }
267
268         if (selector->finish_block)
269                 selector->finish_block(be.selector_block_env);
270 }
271
272 /* List schedule a graph. */
273 void be_list_sched_graph(ir_graph *irg, const list_sched_selector_t *selector)
274 {
275         int num_nodes;
276         sched_env_t env;
277
278         /* Matze: This is very slow, we should avoid it to improve backend speed,
279          * we just have to make sure that we have no dangling out-edges at this
280          * point...
281          */
282         edges_deactivate(irg);
283         edges_activate(irg);
284
285         num_nodes = get_irg_last_idx(irg);
286
287         /* initialize environment for list scheduler */
288         memset(&env, 0, sizeof(env));
289         env.selector  = selector;
290         env.scheduled = rbitset_malloc(num_nodes);
291
292         if (selector->init_graph != NULL)
293                 env.selector_env = selector->init_graph(irg);
294
295         /* Schedule each single block. */
296         irg_block_walk_graph(irg, list_sched_block, NULL, &env);
297
298         if (selector->finish_graph != NULL)
299                 selector->finish_graph(env.selector_env);
300
301         free(env.scheduled);
302 }
303
304 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_listsched)
305 void be_init_listsched(void)
306 {
307         FIRM_DBG_REGISTER(dbg, "firm.be.sched");
308 }