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