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