remove $Id$, it doesn't work with git anyway
[libfirm] / ir / be / besched.h
1 /*
2  * Copyright (C) 1995-2010 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       data structures for scheduling nodes in basic blocks.
23  *              (This file does not contain the scheduling algorithms)
24  * @author      Sebastian Hack, Matthias Braun
25  */
26 #ifndef FIRM_BE_BESCHED_H
27 #define FIRM_BE_BESCHED_H
28
29 #include <stdio.h>
30 #include <stdbool.h>
31
32 #include "irgraph.h"
33 #include "irnode.h"
34 #include "beirg.h"
35 #include "beinfo.h"
36 #include "beutil.h"
37
38 static sched_info_t *get_irn_sched_info(const ir_node *node)
39 {
40         return &be_get_info(skip_Proj_const(node))->sched_info;
41 }
42
43 /**
44  * Check, if the node is scheduled.
45  * @param irn The node.
46  * @return 1, if the node is scheduled, 0 if not.
47  */
48 static inline bool sched_is_scheduled(const ir_node *irn)
49 {
50         return get_irn_sched_info(irn)->next != NULL;
51 }
52
53 /**
54  * Returns the time step of a node. Each node in a block has a timestep
55  * unique to that block. A node schedule before another node has a lower
56  * timestep than this node.
57  * @param irn The node.
58  * @return The time step in the schedule.
59  */
60 static inline sched_timestep_t sched_get_time_step(const ir_node *irn)
61 {
62         assert(sched_is_scheduled(irn));
63         return get_irn_sched_info(irn)->time_step;
64 }
65
66 static inline bool sched_is_end(const ir_node *node)
67 {
68         return is_Block(node);
69 }
70
71 static inline bool sched_is_begin(const ir_node *node)
72 {
73         return is_Block(node);
74 }
75
76 /**
77  * Check, if an ir_node has a scheduling successor.
78  * @param irn The ir node.
79  * @return 1, if the node has a scheduling successor, 0 if not.
80  */
81 static inline bool sched_has_next(const ir_node *irn)
82 {
83         const sched_info_t *info  = get_irn_sched_info(irn);
84         const ir_node      *block = is_Block(irn) ? irn : get_nodes_block(irn);
85         return info->next != block;
86 }
87
88 /**
89  * Check, if an ir_node has a scheduling predecessor.
90  * @param irn The ir node.
91  * @return 1, if the node has a scheduling predecessor, 0 if not.
92  */
93 static inline bool sched_has_prev(const ir_node *irn)
94 {
95         const sched_info_t *info  = get_irn_sched_info(irn);
96         const ir_node      *block = is_Block(irn) ? irn : get_nodes_block(irn);
97         return info->prev != block;
98 }
99
100 /**
101  * Get the scheduling successor of a node.
102  * @param irn The node.
103  * @return The next ir node in the schedule or the block, if the node has no next node.
104  */
105 static inline ir_node *sched_next(const ir_node *irn)
106 {
107         const sched_info_t *info = get_irn_sched_info(irn);
108         return info->next;
109 }
110
111 /**
112  * Get the scheduling predecessor of a node.
113  * @param irn The node.
114  * @return The next ir node in the schedule or the block, if the node has no predecessor.
115  * predecessor.
116  */
117 static inline ir_node *sched_prev(const ir_node *irn)
118 {
119         const sched_info_t *info = get_irn_sched_info(irn);
120         return info->prev;
121 }
122
123 /**
124  * Get the first node in a block schedule.
125  * @param block The block of which to get the schedule.
126  * @return The first node in the schedule or the block itself
127  *         if there is no node in the schedule.
128  */
129 static inline ir_node *sched_first(const ir_node *block)
130 {
131         assert(is_Block(block) && "Need a block here");
132         return sched_next(block);
133 }
134
135 /**
136  * Get the last node in a schedule.
137  * @param  block The block to get the schedule for.
138  * @return The last ir node in a schedule, or the block itself
139  *         if there is no node in the schedule.
140  */
141 static inline ir_node *sched_last(const ir_node *block)
142 {
143         assert(is_Block(block) && "Need a block here");
144         return sched_prev(block);
145 }
146
147 /**
148  * Add a node to a block schedule.
149  * @param block The block to whose schedule the node shall be added to.
150  * @param irn The node to add.
151  * @return The given node.
152  */
153 void sched_add_before(ir_node *before, ir_node *irn);
154
155
156 /**
157  * Add a node to a block schedule.
158  * @param block The block to whose schedule the node shall be added to.
159  * @param irn The node to add.
160  * @return The given node.
161  */
162 void sched_add_after(ir_node *after, ir_node *irn);
163
164 static inline void sched_init_block(ir_node *block)
165 {
166         sched_info_t *info = get_irn_sched_info(block);
167         assert(info->next == NULL && info->time_step == 0);
168         info->next = block;
169         info->prev = block;
170 }
171
172 static inline void sched_reset(ir_node *node)
173 {
174         sched_info_t *info = get_irn_sched_info(node);
175         info->next = NULL;
176         info->prev = NULL;
177 }
178
179 /**
180  * Remove a node from the scheduled.
181  * @param irn The node.
182  */
183 void sched_remove(ir_node *irn);
184
185 /**
186  * Checks, if one node is scheduled before another.
187  * @param n1   A node.
188  * @param n2   Another node.
189  * @return     true, if n1 is in front of n2 in the schedule, false else.
190  * @note       Both nodes must be in the same block.
191  */
192 static inline bool sched_comes_after(const ir_node *n1, const ir_node *n2)
193 {
194         assert(sched_is_scheduled(n1));
195         assert(sched_is_scheduled(n2));
196         assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
197         return sched_get_time_step(n1) < sched_get_time_step(n2);
198 }
199
200 #define sched_foreach_from(from, irn) \
201   for(irn = from; !sched_is_end(irn); irn = sched_next(irn))
202
203 #define sched_foreach_reverse_from(from, irn) \
204   for(irn = from; !sched_is_begin(irn); irn = sched_prev(irn))
205
206 /**
207  * A shorthand macro for iterating over a schedule.
208  * @param block The block.
209  * @param irn A ir node pointer used as an iterator.
210  */
211 #define sched_foreach(block,irn) \
212         sched_foreach_from(sched_first(block), irn)
213
214 /**
215  * A shorthand macro for reversely iterating over a schedule.
216  * @param block The block.
217  * @param irn A ir node pointer used as an iterator.
218  */
219 #define sched_foreach_reverse(block,irn) \
220   sched_foreach_reverse_from(sched_last(block), irn)
221
222 /**
223  * A shorthand macro for iterating over all Phi nodes of a schedule.
224  * @param block The block.
225  * @param phi A ir node pointer used as an iterator.
226  */
227 #define sched_foreach_Phi(block,phi) \
228         for (phi = sched_first(block); is_Phi(phi); phi = sched_next(phi))
229
230 /**
231  * Type for a function scheduling a graph
232  */
233 typedef void (*schedule_func) (ir_graph *irg);
234
235 /**
236  * Register new scheduling algorithm
237  */
238 void be_register_scheduler(const char *name, schedule_func func);
239
240 /**
241  * schedule a graph with the currenty selected scheduler.
242  */
243 void be_schedule_graph(ir_graph *irg);
244
245 #endif