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