22073327965375fd178ff597eb657b2a315b52a4
[libfirm] / ir / be / besched.c
1 /* $Id$ */
2
3 #ifdef HAVE_CONFIG_H
4 # include "config.h"
5 #endif
6
7 #ifdef HAVE_STDLIB_H
8 # include <stdlib.h>
9 #endif
10
11 #include "impl.h"
12 #include "irprintf.h"
13 #include "irgwalk.h"
14 #include "irnode_t.h"
15 #include "irgraph_t.h"
16 #include "iredges_t.h"
17 #include "ircons.h"
18 #include "irextbb.h"
19 #include "debug.h"
20
21 #include "bearch.h"
22 #include "besched_t.h"
23 #include "beutil.h"
24 #include "belistsched.h"
25
26 FIRM_IMPL1(sched_get_time_step, int, const ir_node *)
27 FIRM_IMPL1(sched_has_next, int, const ir_node *)
28 FIRM_IMPL1(sched_has_prev, int, const ir_node *)
29 FIRM_IMPL1(sched_next, ir_node *, const ir_node *)
30 FIRM_IMPL1(sched_prev, ir_node *, const ir_node *)
31 FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
32 FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
33 FIRM_IMPL2(sched_add_after, ir_node *, ir_node *, ir_node *)
34 FIRM_IMPL2(sched_add_before, ir_node *, ir_node *, ir_node *)
35 FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
36 FIRM_IMPL1_VOID(sched_remove, ir_node *)
37
38 size_t sched_irn_data_offset = 0;
39
40 static void block_sched_dumper(ir_node *block, void *env)
41 {
42         FILE *f = env;
43         const ir_node *curr;
44
45         ir_fprintf(f, "%+F:\n", block);
46         sched_foreach(block, curr) {
47     sched_info_t *info = get_irn_sched_info(curr);
48                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
49         }
50 }
51
52 void be_sched_dump(FILE *f, ir_graph *irg)
53 {
54         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
55 }
56
57 /* Init the scheduling stuff. */
58 void be_sched_init(void)
59 {
60         sched_irn_data_offset = register_additional_node_data(sizeof(sched_info_t));
61 }
62
63 void sched_renumber(const ir_node *block)
64 {
65         ir_node *irn;
66         sched_info_t *inf;
67         sched_timestep_t step = SCHED_INITIAL_GRANULARITY;
68
69         sched_foreach(block, irn) {
70                 inf = get_irn_sched_info(irn);
71                 inf->time_step = step;
72                 step += SCHED_INITIAL_GRANULARITY;
73         }
74 }
75
76 /* Verify a schedule. */
77 int sched_verify(const ir_node *block)
78 {
79         int res = 1;
80         const ir_node *irn;
81         int i, n;
82         int *save_time_step;
83         const ir_node **save_nodes;
84         const ir_edge_t *edge;
85         pset *scheduled_nodes = pset_new_ptr_default();
86         FIRM_DBG_REGISTER(firm_dbg_module_t *dbg_sched, "firm.be.sched");
87
88         /* Count the number of nodes in the schedule. */
89         n = 0;
90         sched_foreach(block, irn)
91                 n++;
92
93         if(n <= 0)
94                 return 1;
95
96         save_time_step = xmalloc(n * sizeof(save_time_step[0]));
97         save_nodes = xmalloc(n * sizeof(save_nodes[0]));
98
99         i = 0;
100         sched_foreach(block, irn) {
101                 sched_info_t *info = get_irn_sched_info(irn);
102                 save_time_step[i] = info->time_step;
103                 save_nodes[i] = (ir_node *)irn;
104                 info->time_step = i;
105                 pset_insert_ptr(scheduled_nodes, irn);
106
107                 i += 1;
108         }
109
110         /*
111          * Check if each relevant operand of a node is scheduled before
112          * the node itself.
113          */
114         sched_foreach(block, irn) {
115                 int i, n;
116                 int step = sched_get_time_step(irn);
117
118                 for(i = 0, n = get_irn_arity(irn); i < n; i++) {
119                         ir_node *op = get_irn_n(irn, i);
120
121                         if(to_appear_in_schedule(op)
122                                 && !is_Phi(irn)
123                                 && get_nodes_block(op) == block
124                                 && sched_get_time_step(op) > step) {
125
126                                 DBG((dbg_sched, LEVEL_DEFAULT,
127                                         "%+F: %+F is operand of %+F but scheduled after\n", block, op, irn));
128                                 res = 0;
129                         }
130                 }
131         }
132
133         /* Check, if the time steps are correct */
134         for(i = 1; i < n; ++i) {
135                 if(save_time_step[i] - save_time_step[i - 1] <= 0) {
136                         DBG((dbg_sched, LEVEL_DEFAULT,
137                                 "%+F from %+F(%d) -> %+F(%d) step shrinks from %d -> %d\n",
138                                 block, save_nodes[i - 1], i - 1, save_nodes[i], i,
139                                 save_time_step[i - 1], save_time_step[i]));
140                         res = 0;
141                 }
142         }
143
144         /* Restore the old time steps */
145         i = 0;
146         sched_foreach(block, irn) {
147                 sched_info_t *info = get_irn_sched_info(irn);
148                 info->time_step = save_time_step[i++];
149         }
150
151         /* Check for all nodes in the block if they are scheduled. */
152         foreach_out_edge(block, edge) {
153                 ir_node *irn = get_edge_src_irn(edge);
154                 if(to_appear_in_schedule(irn) && !pset_find_ptr(scheduled_nodes, irn)) {
155                         DBG((dbg_sched, LEVEL_DEFAULT,
156                                 "%+F: %+F is in block but not scheduled\n", block, irn));
157                         res = 0;
158                 }
159         }
160
161         del_pset(scheduled_nodes);
162         free(save_time_step);
163         free((void *) save_nodes);
164         return res;
165 }
166
167 /**
168  * Block-Walker: verify the current block and update the status
169  */
170 static void sched_verify_walker(ir_node *block, void *data)
171 {
172         int *res = data;
173         *res &= sched_verify(block);
174 }
175
176 /* Verify the schedules in all blocks of the irg. */
177 int sched_verify_irg(ir_graph *irg)
178 {
179         int res = 1;
180         irg_block_walk_graph(irg, sched_verify_walker, NULL, &res);
181
182         return res;
183 }
184
185 int sched_skip_cf_predicator(const ir_node *irn, void *data) {
186         arch_env_t *ae = data;
187         return arch_irn_class_is(ae, irn, branch);
188 }
189
190 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
191         return is_Phi(irn);
192 }
193
194 /* Skip nodes in a schedule. */
195 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
196 {
197         const ir_node *bl = get_block(from);
198         ir_node *curr;
199
200         if (is_Block(from))
201                 from = forward ? sched_next(from) : sched_prev(from);
202
203         for(curr = from; curr != bl && predicator(curr, data); curr = forward ? sched_next(curr) : sched_prev(curr));
204
205         return curr;
206 }
207
208 /** A simple forward single linked list. */
209 typedef struct {
210         ir_node *start;   /**< start of the list */
211         ir_node *end;     /**< last block in the list */
212         unsigned n_blks;  /**< number of blocks in the list */
213 } anchor;
214
215 static void add_block(anchor *list, ir_node *block) {
216         if(list->start == NULL) {
217                 list->start = block;
218                 list->end = block;
219         } else {
220                 set_irn_link(list->end, block);
221                 list->end = block;
222         }
223
224         list->n_blks++;
225 }
226
227 static void create_block_list(ir_node *leader_block, anchor *list) {
228         int i;
229         ir_node *block = NULL;
230         const ir_edge_t *edge;
231
232         ir_extblk *extbb = get_Block_extbb(leader_block);
233         if(extbb_visited(extbb))
234                 return;
235         mark_extbb_visited(extbb);
236
237         for(i = 0; i < get_extbb_n_blocks(extbb); ++i) {
238                 block = get_extbb_block(extbb, i);
239                 add_block(list, block);
240         }
241
242         assert(block != NULL);
243
244         // pick successor extbbs
245         foreach_block_succ(block, edge) {
246                 ir_node *succ = get_edge_src_irn(edge);
247
248                 create_block_list(succ, list);
249         }
250
251         for(i = 0; i < get_extbb_n_blocks(extbb) - 1; ++i) {
252                 block = get_extbb_block(extbb, i);
253                 foreach_block_succ(block, edge) {
254                         ir_node *succ = get_edge_src_irn(edge);
255
256                         create_block_list(succ, list);
257                 }
258         }
259 }
260
261 void compute_extbb_execfreqs(ir_graph *irg, ir_exec_freq *execfreqs);
262
263 /*
264  * Calculates a block schedule. The schedule is stored as a linked
265  * list starting at the start_block of the irg.
266  */
267 ir_node **sched_create_block_schedule(ir_graph *irg, ir_exec_freq *execfreqs)
268 {
269         anchor list;
270         ir_node **blk_list, *b, *n;
271         unsigned i;
272
273         /* schedule extended basic blocks */
274         compute_extbb_execfreqs(irg, execfreqs);
275         //compute_extbb(irg);
276
277         list.start  = NULL;
278         list.end    = NULL;
279         list.n_blks = 0;
280         inc_irg_block_visited(irg);
281         create_block_list(get_irg_start_block(irg), &list);
282
283         /** create an array, so we can go forward and backward */
284         blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
285
286         for (i = 0, b = list.start; b; b = n, ++i) {
287                 n = get_irn_link(b);
288                 blk_list[i] = b;
289         }
290
291         return blk_list;
292 }
293
294 typedef struct remove_dead_nodes_env_t_ {
295         ir_graph *irg;
296         bitset_t *reachable;
297 } remove_dead_nodes_env_t;
298
299 static void mark_dead_nodes_walker(ir_node *node, void *data)
300 {
301         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
302         bitset_set(env->reachable, get_irn_idx(node));
303 }
304
305 static void remove_dead_nodes_walker(ir_node *block, void *data)
306 {
307         remove_dead_nodes_env_t *env = (remove_dead_nodes_env_t*) data;
308         ir_node *node, *next;
309
310         for(node = sched_first(block); !sched_is_end(node); node = next) {
311                 int i, arity;
312
313                 // get next node now, as after calling sched_remove it will be invalid
314                 next = sched_next(node);
315
316                 if(bitset_is_set(env->reachable, get_irn_idx(node)))
317                         continue;
318
319                 arity = get_irn_arity(node);
320                 for(i = 0; i < arity; ++i)
321                         set_irn_n(node, i, new_r_Bad(env->irg));
322
323                 sched_remove(node);
324         }
325 }
326
327 void be_remove_dead_nodes_from_schedule(ir_graph *irg)
328 {
329         remove_dead_nodes_env_t env;
330         env.irg = irg;
331         env.reachable = bitset_alloca(get_irg_last_idx(irg));
332
333         // mark all reachable nodes
334         irg_walk_graph(irg, mark_dead_nodes_walker, NULL, &env);
335
336         // walk schedule and remove non-marked nodes
337         irg_block_walk_graph(irg, remove_dead_nodes_walker, NULL, &env);
338 }