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