- Added a few asserts to the besched API, so you can't schedule a node
[libfirm] / ir / be / besched_t.h
1
2 #ifndef _BESCHED_T_H
3 #define _BESCHED_T_H
4
5 #define SCHED_INITIAL_GRANULARITY (1 << 14)
6
7 #include "list.h"
8 #include "irnode_t.h"
9 #include "irgraph_t.h"
10
11 #include "beutil.h"
12 #include "besched.h"
13
14 typedef unsigned int sched_timestep_t;
15
16 extern size_t sched_irn_data_offset;
17
18 /**
19  * The schedule structure which is present at each ir node.
20  *
21  * Currently, only basic blocks are scheduled. The list head of
22  * every block schedule list is the Block list.
23  */
24 typedef struct _sched_info_t {
25         struct list_head list;         /**< The list head to list the nodes in a schedule. */
26         sched_timestep_t time_step;    /**< If a is after b in a schedule, its time step is
27                                         larger than b's. */
28         unsigned scheduled : 1;        /**< 1, if the node is in the schedule of the block, 0 else. */
29 } sched_info_t;
30
31 #define _sched_entry(list_head) (list_entry(list_head, sched_info_t, list))
32
33 #define get_irn_sched_info(irn) get_irn_data(irn, sched_info_t, sched_irn_data_offset)
34 #define get_sched_info_irn(sched_info) get_irn_data_base(sched_info, sched_irn_data_offset)
35
36 /**
37  * Init the scheduling stuff.
38  * To be called from the central backend initialization routine.
39  */
40 void be_sched_init(void);
41
42 /**
43  * Check, if the node is scheduled.
44  * @param irn The node.
45  * @return 1, if the node is scheduled, 0 if not.
46  */
47 static INLINE int _sched_is_scheduled(const ir_node *irn)
48 {
49   return get_irn_sched_info(irn)->scheduled;
50 }
51
52 /**
53  * Get the time step of an irn in a schedule.
54  * @param irn The node.
55  * @return The time step in the schedule.
56  */
57 static INLINE int _sched_get_time_step(const ir_node *irn)
58 {
59         assert(_sched_is_scheduled(irn));
60         return get_irn_sched_info(irn)->time_step;
61 }
62
63 /**
64  * Checks, if a node is to appear in a schedule. Such nodes either
65  * consume real data (mode datab) or produce such.
66  * @param irn The node to check for.
67  * @return 1, if the node consumes/produces data, false if not.
68  */
69 static INLINE int to_appear_in_schedule(const ir_node *irn)
70 {
71         switch(get_irn_opcode(irn)) {
72                 case iro_Start:
73                 case iro_Jmp:
74                 case iro_Break:
75                         return 1;
76                 default:
77                         return is_data_node(irn);
78         }
79 }
80
81 /**
82  * Check, if an ir_node has a scheduling successor.
83  * @param irn The ir node.
84  * @return 1, if the node has a scheduling successor, 0 if not.
85  */
86 static INLINE int _sched_has_next(const ir_node *irn)
87 {
88         const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
89         const sched_info_t *info = get_irn_sched_info(irn);
90         const sched_info_t *block_info = get_irn_sched_info(block);
91         return info->list.next != &block_info->list;
92 }
93
94 /**
95  * Check, if an ir_node has a scheduling predecessor.
96  * @param irn The ir node.
97  * @return 1, if the node has a scheduling predecessor, 0 if not.
98  */
99 static INLINE int _sched_has_prev(const ir_node *irn)
100 {
101         const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
102         const sched_info_t *info = get_irn_sched_info(irn);
103         const sched_info_t *block_info = get_irn_sched_info(block);
104         return info->list.prev != &block_info->list;
105 }
106
107 /**
108  * Get the scheduling successor of a node.
109  * @param irn The node.
110  * @return The next ir node in the schedule or the block, if the node has no next node.
111  */
112 static INLINE ir_node *_sched_next(const ir_node *irn)
113 {
114         const sched_info_t *info = get_irn_sched_info(irn);
115         return get_sched_info_irn(_sched_entry(info->list.next));
116 }
117
118 /**
119  * Get the scheduling predecessor of a node.
120  * @param irn The node.
121  * @return The next ir node in the schedule or the block, if the node has no predecessor.
122  * predecessor.
123  */
124 static INLINE ir_node *_sched_prev(const ir_node *irn)
125 {
126         const sched_info_t *info = get_irn_sched_info(irn);
127         return get_sched_info_irn(_sched_entry(info->list.prev));
128 }
129
130 /**
131  * Get the first node in a block schedule.
132  * @param block The block of which to get the schedule.
133  * @return The first node in the schedule or the block itself
134  *         if there is no node in the schedule.
135  */
136 static INLINE ir_node *_sched_first(const ir_node *block)
137 {
138         assert(is_Block(block) && "Need a block here");
139         return _sched_next(block);
140 }
141
142 /**
143  * Get the last node in a schedule.
144  * @param  block The block to get the schedule for.
145  * @return The last ir node in a schedule, or the block itself
146  *         if there is no node in the schedule.
147  */
148 static INLINE ir_node *_sched_last(const ir_node *block)
149 {
150         assert(is_Block(block) && "Need a block here");
151         return _sched_prev(block);
152 }
153
154 /**
155  * Reassign the time steps in the schedule.
156  * @param block The schedule to update.
157  */
158 void sched_renumber(const ir_node *block);
159
160 static INLINE void _sched_set_time_stamp(ir_node *irn)
161 {
162         sched_info_t *inf = get_irn_sched_info(irn);
163         sched_timestep_t before_ts = _sched_entry(inf->list.prev)->time_step;
164         sched_timestep_t after_ts = _sched_entry(inf->list.next)->time_step;
165
166         /*
167          * If we are the last, we can give us a big time step,
168          * else we have to compute our time step from our
169          * neighbours.
170          */
171         if(before_ts >= after_ts)
172                 inf->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
173         else {
174                 sched_timestep_t ts = (before_ts + after_ts) / 2;
175
176                 /*
177                  * If the resolution went out, we have to renumber
178                  * this block.
179                  */
180                 if(ts == before_ts || ts == after_ts)
181                         sched_renumber(get_nodes_block(irn));
182                 else
183                         inf->time_step = ts;
184         }
185 }
186
187 /**
188  * Add a node to a block schedule.
189  * @param block The block to whose schedule the node shall be added to.
190  * @param irn The node to add.
191  * @return The given node.
192  */
193 static INLINE ir_node *_sched_add_before(ir_node *before, ir_node *irn)
194 {
195         assert(_sched_is_scheduled(before) && !_sched_is_scheduled(irn));
196         sched_info_t *info = get_irn_sched_info(irn);
197         list_add_tail(&info->list, &get_irn_sched_info(before)->list);
198         _sched_set_time_stamp(irn);
199         info->scheduled = 1;
200         return irn;
201 }
202
203 /**
204  * Add a node to a block schedule.
205  * @param block The block to whose schedule the node shall be added to.
206  * @param irn The node to add.
207  * @return The given node.
208  */
209 static INLINE ir_node *_sched_add_after(ir_node *after, ir_node *irn)
210 {
211         assert(_sched_is_scheduled(after) && !_sched_is_scheduled(irn));
212         sched_info_t *info = get_irn_sched_info(irn);
213         list_add(&info->list, &get_irn_sched_info(after)->list);
214         _sched_set_time_stamp(irn);
215         info->scheduled = 1;
216         return irn;
217 }
218
219 static INLINE void _sched_init_block(ir_node *block)
220 {
221         sched_info_t *info = get_irn_sched_info(block);
222         assert(info->scheduled == 0 && info->time_step == 0);
223         INIT_LIST_HEAD(&info->list);
224         info->scheduled = 1;
225 }
226
227 static INLINE void _sched_reset(ir_node *node)
228 {
229         sched_info_t *info = get_irn_sched_info(node);
230         info->scheduled = 0;
231 }
232
233 /**
234  * Remove a node from the scheduled.
235  * @param irn The node.
236  */
237 static INLINE void _sched_remove(ir_node *irn)
238 {
239   sched_info_t *info = get_irn_sched_info(irn);
240   list_del(&info->list);
241         INIT_LIST_HEAD(&info->list);
242   info->scheduled = 0;
243 }
244
245 /**
246  * Compare two nodes according to their position in the schedule.
247  * @param a The first node.
248  * @param b The second node.
249  * @return A number smaller, equals to or larger than 0, if a is
250  *         before, the same, or after b in the schedule.
251  */
252 static INLINE int _sched_cmp(const ir_node *a, const ir_node *b)
253 {
254   assert(_sched_is_scheduled(a) && _sched_is_scheduled(b));
255   assert(get_nodes_block(a) == get_nodes_block(b));
256
257   return get_irn_sched_info(a)->time_step - get_irn_sched_info(b)->time_step;
258 }
259
260 /**
261  * Verify a schedule.
262  * @param block The block whose schedule to verify.
263  * @return      1, if the schedule is proper, 0 if not.
264  */
265 extern int sched_verify(const ir_node *block);
266
267 /**
268  * Verify the schedules in all blocks of the irg.
269  * @param irg The program graph.
270  * @return    1, if all schedules were right, 0 if not.
271  */
272 extern int sched_verify_irg(ir_graph *irg);
273
274 /**
275  * Checks, if one node is scheduled before another.
276  * @param n1   A node.
277  * @param n2   Another node.
278  * @return     1, if n1 is in front of n2 in the schedule, 0 else.
279  * @note       Both nodes must be in the same block.
280  */
281 static INLINE int _sched_comes_after(const ir_node *n1, const ir_node *n2)
282 {
283         assert(_sched_is_scheduled(n1));
284         assert(_sched_is_scheduled(n2));
285         assert(get_nodes_block(n1) == get_nodes_block(n2));
286         return _sched_get_time_step(n1) < _sched_get_time_step(n2);
287 }
288
289 /**
290  * A predicate for a node.
291  * @param irn The node.
292  * @param data The custom data.
293  * @return 1 if irn should be skipped. Else 0.
294  */
295 typedef int (sched_predicator_t)(const ir_node *irn, void *data);
296
297
298 int sched_skip_cf_predicator(const ir_node *irn, void *data);
299 int sched_skip_phi_predicator(const ir_node *irn, void *data);
300
301 /**
302  * Skip nodes in a schedule.
303  * @param from The node to start from.
304  * @param forward The direction (1 for forward, 0 for backward).
305  * @param predicator The one who decides what is skipped.
306  * @param data Food for the predicator.
307  * @return The first node rejected by the predicator or the block
308  * itself if none was rejected.
309  */
310 extern ir_node *sched_skip(ir_node *from, int forward,
311     sched_predicator_t *predicator, void *data);
312
313 #define sched_get_time_step(irn)        _sched_get_time_step(irn)
314 #define sched_has_next(irn)             _sched_has_next(irn)
315 #define sched_has_prev(irn)             _sched_has_prev(irn)
316 #define sched_next(irn)                 _sched_next(irn)
317 #define sched_prev(irn)                 _sched_prev(irn)
318 #define sched_first(irn)                _sched_first(irn)
319 #define sched_last(irn)                 _sched_last(irn)
320 #define sched_add_before(before, irn)   _sched_add_before(before, irn)
321 #define sched_add_after(after, irn)     _sched_add_after(after, irn)
322 #define sched_remove(irn)               _sched_remove(irn)
323 #define sched_is_scheduled(irn)         _sched_is_scheduled(irn)
324 #define sched_comes_after(n1, n2)       _sched_comes_after(n1, n2)
325 #define sched_cmp(a, b)                 _sched_cmp(a, b)
326
327 #endif