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