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