amd64: small changes w.r.t. stack alignment.
[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     sched_get_time_step(const ir_node *irn);
45 int     sched_has_next(const ir_node *irn);
46 int     sched_has_prev(const ir_node *irn);
47 int     sched_is_scheduled(const ir_node *irn);
48 ir_node *sched_next(const ir_node *irn);
49 ir_node *sched_prev(const ir_node *irn);
50 ir_node *sched_first(const ir_node *block);
51 ir_node *sched_last(const ir_node *block);
52 void    sched_add_before(ir_node *before, ir_node *irn);
53 void    sched_add_after(ir_node *after, ir_node *irn);
54 void    sched_init_block(ir_node *block);
55 void    sched_reset(ir_node *node);
56 int     sched_comes_after(const ir_node *n1, const ir_node *n2);
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  * Check, if the node is scheduled.
103  * @param irn The node.
104  * @return 1, if the node is scheduled, 0 if not.
105  */
106 static inline int _sched_is_scheduled(const ir_node *irn)
107 {
108         return get_irn_sched_info(irn)->next != NULL;
109 }
110
111 /**
112  * Get the time step of an irn in a schedule.
113  * @param irn The node.
114  * @return The time step in the schedule.
115  */
116 static inline int _sched_get_time_step(const ir_node *irn)
117 {
118         assert(_sched_is_scheduled(irn));
119         return get_irn_sched_info(irn)->time_step;
120 }
121
122 /**
123  * Checks, if a node is to appear in a schedule. Such nodes either
124  * consume real data (mode datab) or produce such.
125  * @param irn The node to check for.
126  * @return 1, if the node consumes/produces data, false if not.
127  */
128 static inline int to_appear_in_schedule(const ir_node *irn)
129 {
130         switch(get_irn_opcode(irn)) {
131                 case iro_Jmp:
132                 case iro_Break:
133                         return 1;
134                 case iro_Proj:
135                         return 0;
136                 default:
137                         return is_data_node(irn);
138         }
139 }
140
141 /**
142  * Check, if an ir_node has a scheduling successor.
143  * @param irn The ir node.
144  * @return 1, if the node has a scheduling successor, 0 if not.
145  */
146 static inline int _sched_has_next(const ir_node *irn)
147 {
148         const sched_info_t *info  = get_irn_sched_info(irn);
149         const ir_node      *block = is_Block(irn) ? irn : get_nodes_block(irn);
150         return info->next != block;
151 }
152
153 /**
154  * Check, if an ir_node has a scheduling predecessor.
155  * @param irn The ir node.
156  * @return 1, if the node has a scheduling predecessor, 0 if not.
157  */
158 static inline int _sched_has_prev(const ir_node *irn)
159 {
160         const sched_info_t *info  = get_irn_sched_info(irn);
161         const ir_node      *block = is_Block(irn) ? irn : get_nodes_block(irn);
162         return info->prev != block;
163 }
164
165 /**
166  * Get the scheduling successor of a node.
167  * @param irn The node.
168  * @return The next ir node in the schedule or the block, if the node has no next node.
169  */
170 static inline ir_node *_sched_next(const ir_node *irn)
171 {
172         const sched_info_t *info = get_irn_sched_info(irn);
173         return info->next;
174 }
175
176 /**
177  * Get the scheduling predecessor 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 predecessor.
180  * predecessor.
181  */
182 static inline ir_node *_sched_prev(const ir_node *irn)
183 {
184         const sched_info_t *info = get_irn_sched_info(irn);
185         return info->prev;
186 }
187
188 /**
189  * Get the first node in a block schedule.
190  * @param block The block of which to get the schedule.
191  * @return The first node in the schedule or the block itself
192  *         if there is no node in the schedule.
193  */
194 static inline ir_node *_sched_first(const ir_node *block)
195 {
196         assert(is_Block(block) && "Need a block here");
197         return _sched_next(block);
198 }
199
200 /**
201  * Get the last node in a schedule.
202  * @param  block The block to get the schedule for.
203  * @return The last ir node in a schedule, or the block itself
204  *         if there is no node in the schedule.
205  */
206 static inline ir_node *_sched_last(const ir_node *block)
207 {
208         assert(is_Block(block) && "Need a block here");
209         return _sched_prev(block);
210 }
211
212 /**
213  * Reassign the time steps in the schedule.
214  * @param block The schedule to update.
215  */
216 void sched_renumber(const ir_node *block);
217
218 static inline void _sched_set_time_stamp(const ir_node *irn)
219 {
220         sched_info_t       *info      = get_irn_sched_info(irn);
221         const sched_info_t *prev_info = get_irn_sched_info(info->prev);
222         const sched_info_t *next_info = get_irn_sched_info(info->next);
223         sched_timestep_t    before_ts = prev_info->time_step;
224         sched_timestep_t    after_ts  = next_info->time_step;
225
226         /*
227          * If we are the last, we can give us a big time step,
228          * else we have to compute our time step from our
229          * neighbours.
230          */
231         if(before_ts >= after_ts) {
232                 info->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
233                 /* overflow? */
234                 if (info->time_step <= before_ts) {
235                         sched_renumber(get_nodes_block(irn));
236                 }
237         } else {
238                 sched_timestep_t ts = (before_ts + after_ts) / 2;
239
240                 /*
241                  * If the resolution went out, we have to renumber
242                  * this block.
243                  */
244                 if(ts == before_ts || ts == after_ts)
245                         sched_renumber(get_nodes_block(irn));
246                 else
247                         info->time_step = ts;
248         }
249 }
250
251 /**
252  * Add a node to a block schedule.
253  * @param block The block to whose schedule the node shall be added to.
254  * @param irn The node to add.
255  * @return The given node.
256  */
257 static inline void _sched_add_before(ir_node *before, ir_node *irn)
258 {
259         sched_info_t *info      = get_irn_sched_info(irn);
260         ir_node      *next      = before;
261         sched_info_t *next_info = get_irn_sched_info(next);
262         ir_node      *prev      = next_info->prev;
263         sched_info_t *prev_info = get_irn_sched_info(prev);
264         assert(_sched_is_scheduled(before));
265         assert(!_sched_is_scheduled(irn));
266         assert(!is_Proj(before));
267         assert(!is_Proj(irn));
268
269         info->prev = prev;
270         info->next = next;
271         prev_info->next = irn;
272         next_info->prev = irn;
273         _sched_set_time_stamp(irn);
274 }
275
276 /**
277  * Add a node to a block schedule.
278  * @param block The block to whose schedule the node shall be added to.
279  * @param irn The node to add.
280  * @return The given node.
281  */
282 static inline void _sched_add_after(ir_node *after, ir_node *irn)
283 {
284         sched_info_t *info      = get_irn_sched_info(irn);
285         ir_node      *prev      = after;
286         sched_info_t *prev_info = get_irn_sched_info(prev);
287         ir_node      *next      = prev_info->next;
288         sched_info_t *next_info = get_irn_sched_info(next);
289         assert(_sched_is_scheduled(after));
290         assert(!_sched_is_scheduled(irn));
291         assert(!is_Proj(after));
292         assert(!is_Proj(irn));
293
294         info->prev = prev;
295         info->next = next;
296         prev_info->next = irn;
297         next_info->prev = irn;
298         _sched_set_time_stamp(irn);
299 }
300
301 static inline void _sched_init_block(ir_node *block)
302 {
303         sched_info_t *info = get_irn_sched_info(block);
304         assert(info->next == NULL && info->time_step == 0);
305         info->next = block;
306         info->prev = block;
307 }
308
309 static inline void _sched_reset(ir_node *node)
310 {
311         sched_info_t *info = get_irn_sched_info(node);
312         info->next = NULL;
313         info->prev = NULL;
314 }
315
316 /**
317  * Remove a node from the scheduled.
318  * @param irn The node.
319  */
320 static inline void _sched_remove(ir_node *irn)
321 {
322         sched_info_t *info      = get_irn_sched_info(irn);
323         ir_node      *prev      = info->prev;
324         ir_node      *next      = info->next;
325         sched_info_t *prev_info = get_irn_sched_info(prev);
326         sched_info_t *next_info = get_irn_sched_info(next);
327         assert(_sched_is_scheduled(irn));
328
329         prev_info->next = next;
330         next_info->prev = prev;
331         info->next      = NULL;
332         info->prev      = NULL;
333 }
334
335 /**
336  * Compare two nodes according to their position in the schedule.
337  * @param a The first node.
338  * @param b The second node.
339  * @return A number smaller, equals to or larger than 0, if a is
340  *         before, the same, or after b in the schedule.
341  */
342 static inline int _sched_cmp(const ir_node *a, const ir_node *b)
343 {
344         assert(_sched_is_scheduled(a) && _sched_is_scheduled(b));
345         assert(get_nodes_block(a) == get_nodes_block(b));
346
347         return get_irn_sched_info(a)->time_step - get_irn_sched_info(b)->time_step;
348 }
349
350 /**
351  * Checks, if one node is scheduled before another.
352  * @param n1   A node.
353  * @param n2   Another node.
354  * @return     1, if n1 is in front of n2 in the schedule, 0 else.
355  * @note       Both nodes must be in the same block.
356  */
357 static inline int _sched_comes_after(const ir_node *n1, const ir_node *n2)
358 {
359         assert(_sched_is_scheduled(n1));
360         assert(_sched_is_scheduled(n2));
361         assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
362         return _sched_get_time_step(n1) < _sched_get_time_step(n2);
363 }
364
365 /**
366  * A predicate for a node.
367  * @param irn The node.
368  * @param data The custom data.
369  * @return 1 if irn should be skipped. Else 0.
370  */
371 typedef int (sched_predicator_t)(const ir_node *irn, void *data);
372
373 /**
374  * Predicate for sched_skip(), returns non-zero if irn is a control flow changing node.
375  *
376  * @param irn   the node to evaluate
377  * @param data  unused
378  */
379 int sched_skip_cf_predicator(const ir_node *irn, void *data);
380
381 /**
382  * Predicate for sched_skip(), returns non-zero if irn is a Phi node.
383  *
384  * Used with sched_skip().
385  *
386  * @param irn  the node to evaluate
387  * @param data unused
388  */
389 int sched_skip_phi_predicator(const ir_node *irn, void *data);
390
391 /**
392  * Skip nodes in a schedule.
393  * @param from        The node to start from.
394  * @param forward     The direction (1 for forward, 0 for backward).
395  * @param predicator  The predicator function which decides what is skipped.
396  * @param data        context parameter for the predicator.
397  *
398  * @return The first node not rejected by the predicator or the block
399  *         itself if all nodes were rejected.
400  */
401 ir_node *sched_skip(ir_node *from, int forward, sched_predicator_t *predicator, void *data);
402
403 #define sched_get_time_step(irn)        _sched_get_time_step(irn)
404 #define sched_has_next(irn)             _sched_has_next(irn)
405 #define sched_has_prev(irn)             _sched_has_prev(irn)
406 #define sched_next(irn)                 _sched_next(irn)
407 #define sched_prev(irn)                 _sched_prev(irn)
408 #define sched_first(irn)                _sched_first(irn)
409 #define sched_last(irn)                 _sched_last(irn)
410 #define sched_add_before(before, irn)   _sched_add_before(before, irn)
411 #define sched_add_after(after, irn)     _sched_add_after(after, irn)
412 #define sched_remove(irn)               _sched_remove(irn)
413 #define sched_is_scheduled(irn)         _sched_is_scheduled(irn)
414 #define sched_comes_after(n1, n2)       _sched_comes_after(n1, n2)
415 #define sched_cmp(a, b)                 _sched_cmp(a, b)
416
417
418
419 #endif