a3c0e029f373a12b2c2fbd6afa6f5d6c2715b955
[libfirm] / ir / be / besched.c
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #ifdef HAVE_STDLIB_H
6 # include <stdlib.h>
7 #endif
8
9 #include "impl.h"
10 #include "irprintf.h"
11 #include "irgwalk.h"
12 #include "irnode_t.h"
13 #include "irgraph_t.h"
14 #include "iredges_t.h"
15 #include "debug.h"
16
17 #include "bearch.h"
18 #include "besched_t.h"
19 #include "beutil.h"
20 #include "belistsched.h"
21
22 FIRM_IMPL1(sched_get_time_step, int, const ir_node *)
23 FIRM_IMPL1(sched_has_next, int, const ir_node *)
24 FIRM_IMPL1(sched_has_prev, int, const ir_node *)
25 FIRM_IMPL1(sched_next, ir_node *, const ir_node *)
26 FIRM_IMPL1(sched_prev, ir_node *, const ir_node *)
27 FIRM_IMPL1(sched_first, ir_node *, const ir_node *)
28 FIRM_IMPL1(sched_last, ir_node *, const ir_node *)
29 FIRM_IMPL2(sched_add_after, ir_node *, ir_node *, ir_node *)
30 FIRM_IMPL2(sched_add_before, ir_node *, ir_node *, ir_node *)
31 FIRM_IMPL2(sched_comes_after, int, const ir_node *, const ir_node *)
32 FIRM_IMPL1_VOID(sched_remove, ir_node *)
33
34 size_t sched_irn_data_offset = 0;
35
36 static void block_sched_dumper(ir_node *block, void *env)
37 {
38         FILE *f = env;
39         const ir_node *curr;
40
41         ir_fprintf(f, "%+F:\n", block);
42         sched_foreach(block, curr) {
43     sched_info_t *info = get_irn_sched_info(curr);
44                 ir_fprintf(f, "\t%6d: %+F\n", info->time_step, curr);
45         }
46 }
47
48 void be_sched_dump(FILE *f, ir_graph *irg)
49 {
50         irg_block_walk_graph(irg, block_sched_dumper, NULL, f);
51 }
52
53 /* Init the scheduling stuff. */
54 void be_sched_init(void)
55 {
56         sched_irn_data_offset = register_additional_node_data(sizeof(sched_info_t));
57         firm_dbg_register("be.sched");
58 }
59
60 void sched_renumber(const ir_node *block)
61 {
62   ir_node *irn;
63   sched_info_t *inf;
64   sched_timestep_t step = 0;
65
66   sched_foreach(block, irn) {
67     inf = get_irn_sched_info(irn);
68     inf->time_step = step;
69     step += SCHED_INITIAL_GRANULARITY;
70   }
71 }
72
73 /* Verify a schedule. */
74 int sched_verify(const ir_node *block)
75 {
76   firm_dbg_module_t *dbg_sched = firm_dbg_register("be.sched");
77   int res = 1;
78   const ir_node *irn;
79   int i, n;
80   int *save_time_step;
81   const ir_node **save_nodes;
82   const ir_edge_t *edge;
83   pset *scheduled_nodes = pset_new_ptr_default();
84
85   firm_dbg_set_mask(dbg_sched, -1);
86
87   /* Count the number of nodes in the schedule. */
88   n = 0;
89   sched_foreach(block, irn)
90     n++;
91
92   if(n <= 0)
93     return 1;
94
95   save_time_step = xmalloc(n * sizeof(save_time_step[0]));
96   save_nodes = xmalloc(n * sizeof(save_nodes[0]));
97
98   i = 0;
99   sched_foreach(block, irn) {
100     sched_info_t *info = get_irn_sched_info(irn);
101     save_time_step[i] = info->time_step;
102     save_nodes[i] = (ir_node *)irn;
103     info->time_step = i;
104     pset_insert_ptr(scheduled_nodes, irn);
105
106     i += 1;
107   }
108
109   /*
110    * Check if each relevant operand of a node is scheduled before
111    * the node itself.
112    */
113   sched_foreach(block, irn) {
114     int i, n;
115     int step = sched_get_time_step(irn);
116
117     for(i = 0, n = get_irn_arity(irn); i < n; i++) {
118       ir_node *op = get_irn_n(irn, i);
119
120       if(to_appear_in_schedule(op)
121           && !is_Phi(irn)
122           && get_nodes_block(op) == block
123           && sched_get_time_step(op) > step) {
124
125           DBG((dbg_sched, LEVEL_DEFAULT,
126                 "%+F: %+F is operand of %+F but scheduled after\n", block, op, irn));
127           res = 0;
128       }
129     }
130   }
131
132   /* Check, if the time steps are correct */
133   for(i = 1; i < n; ++i) {
134     if(save_time_step[i] - save_time_step[i - 1] <= 0) {
135       DBG((dbg_sched, LEVEL_DEFAULT,
136             "%+F from %+F(%d) -> %+F(%d) step shrinks from %d -> %d\n",
137             block, save_nodes[i - 1], i - 1, save_nodes[i], i,
138             save_time_step[i - 1], save_time_step[i]));
139       res = 0;
140     }
141   }
142
143   /* Restore the old time steps */
144   i = 0;
145   sched_foreach(block, irn) {
146     sched_info_t *info = get_irn_sched_info(irn);
147     info->time_step = save_time_step[i++];
148   }
149
150   /* Check for all nodes in the block if they are scheduled. */
151   foreach_out_edge(block, edge) {
152     ir_node *irn = get_edge_src_irn(edge);
153     if(to_appear_in_schedule(irn) && !pset_find_ptr(scheduled_nodes, irn))
154       DBG((dbg_sched, LEVEL_DEFAULT,
155             "%+F: %+F is in block but not scheduled\n", block, irn));
156   }
157
158   del_pset(scheduled_nodes);
159   free(save_time_step);
160   free((void *) save_nodes);
161   return res;
162 }
163
164 /**
165  * Block-Walker: verify the current block and update the status
166  */
167 static void sched_verify_walker(ir_node *block, void *data)
168 {
169   int *res = data;
170   *res &= sched_verify(block);
171 }
172
173 /* Verify the schedules in all blocks of the irg. */
174 int sched_verify_irg(ir_graph *irg)
175 {
176   int res = 1;
177   irg_block_walk_graph(irg, sched_verify_walker, NULL, &res);
178
179   return res;
180 }
181
182 int sched_skip_cf_predicator(const ir_node *irn, void *data) {
183   arch_env_t *ae = data;
184   return arch_irn_classify(ae, irn) == arch_irn_class_branch;
185 }
186
187 int sched_skip_phi_predicator(const ir_node *irn, void *data) {
188         return is_Phi(irn);
189 }
190
191 /* Skip nodes in a schedule. */
192 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data)
193 {
194         const ir_node *bl = get_block(from);
195         ir_node *curr;
196
197         if (is_Block(from))
198                 from = forward ? sched_next(from) : sched_prev(from);
199
200         for(curr = from; curr != bl && predicator(curr, data); curr = forward ? sched_next(curr) : sched_prev(curr));
201
202         return curr;
203 }
204
205 /** A simple forward single linked list. */
206 typedef struct {
207         ir_node *start;   /**< start of the list */
208         ir_node *end;     /**< last block in the list */
209         unsigned n_blks;  /**< number of blocks in the list */
210 } anchor;
211
212 /**
213  * Ext-Block walker: create a block schedule
214  */
215 static void create_block_list(ir_extblk *blk, void *env) {
216         anchor *list = env;
217         int i, n;
218
219         for (i = 0, n = get_extbb_n_blocks(blk); i < n; ++i) {
220                 ir_node *block = get_extbb_block(blk, i);
221
222                 set_irn_link(block, NULL);
223                 if (list->start)
224                         set_irn_link(list->end, block);
225                 else
226                         list->start = block;
227
228                 list->end = block;
229                 ++list->n_blks;
230         }
231 }
232
233 /*
234  * Calculates a block schedule. The schedule is stored as a linked
235  * list starting at the start_block of the irg.
236  */
237 ir_node **sched_create_block_schedule(ir_graph *irg)
238 {
239         anchor list;
240         ir_node **blk_list, *b, *n;
241         unsigned i;
242
243         /* schedule extended basic blocks */
244         compute_extbb(irg);
245
246         list.start  = NULL;
247         list.end    = NULL;
248         list.n_blks = 0;
249         irg_extblock_walk_graph(irg, NULL, create_block_list, &list);
250
251         /** create an array, so we can go forward and backward */
252         blk_list = NEW_ARR_D(ir_node *, irg->obst,list.n_blks);
253
254         for (i = 0, b = list.start; b; b = n, ++i) {
255                 n = get_irn_link(b);
256                 set_irn_link(b, INT_TO_PTR(i));
257
258                 blk_list[i] = b;
259         }
260         return blk_list;
261 }