besched: Add and use sched_foreach_reverse_safe().
[libfirm] / ir / be / besched.h
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       data structures for scheduling nodes in basic blocks.
9  * @author      Sebastian Hack, Matthias Braun
10  */
11 #ifndef FIRM_BE_BESCHED_H
12 #define FIRM_BE_BESCHED_H
13
14 #include <stdbool.h>
15
16 #include "beinfo.h"
17
18 static sched_info_t *get_irn_sched_info(const ir_node *node)
19 {
20         return &be_get_info(skip_Proj_const(node))->sched_info;
21 }
22
23 /**
24  * Check, if the node is scheduled.
25  * Block nodes are reported as scheduled as they mark the begin and end
26  * of the scheduling list.
27  * @param irn The node.
28  * @return 1, if the node is scheduled, 0 if not.
29  */
30 static inline bool sched_is_scheduled(const ir_node *irn)
31 {
32         return get_irn_sched_info(irn)->next != NULL;
33 }
34
35 /**
36  * Returns the time step of a node. Each node in a block has a timestep
37  * unique to that block. A node schedule before another node has a lower
38  * timestep than this node.
39  * @param irn The node.
40  * @return The time step in the schedule.
41  */
42 static inline sched_timestep_t sched_get_time_step(const ir_node *irn)
43 {
44         assert(sched_is_scheduled(irn));
45         return get_irn_sched_info(irn)->time_step;
46 }
47
48 static inline bool sched_is_end(const ir_node *node)
49 {
50         return is_Block(node);
51 }
52
53 static inline bool sched_is_begin(const ir_node *node)
54 {
55         return is_Block(node);
56 }
57
58 /**
59  * Get the scheduling successor of a node.
60  * @param irn The node.
61  * @return The next ir node in the schedule or the block, if the node has no next node.
62  */
63 static inline ir_node *sched_next(const ir_node *irn)
64 {
65         const sched_info_t *info = get_irn_sched_info(irn);
66         return info->next;
67 }
68
69 /**
70  * Get the scheduling predecessor of a node.
71  * @param irn The node.
72  * @return The next ir node in the schedule or the block, if the node has no predecessor.
73  * predecessor.
74  */
75 static inline ir_node *sched_prev(const ir_node *irn)
76 {
77         const sched_info_t *info = get_irn_sched_info(irn);
78         return info->prev;
79 }
80
81 /**
82  * Get the first node in a block schedule.
83  * @param block The block of which to get the schedule.
84  * @return The first node in the schedule or the block itself
85  *         if there is no node in the schedule.
86  */
87 static inline ir_node *sched_first(const ir_node *block)
88 {
89         assert(is_Block(block) && "Need a block here");
90         return sched_next(block);
91 }
92
93 /**
94  * Get the last node in a schedule.
95  * @param  block The block to get the schedule for.
96  * @return The last ir node in a schedule, or the block itself
97  *         if there is no node in the schedule.
98  */
99 static inline ir_node *sched_last(const ir_node *block)
100 {
101         assert(is_Block(block) && "Need a block here");
102         return sched_prev(block);
103 }
104
105 /**
106  * Add a node to a block schedule.
107  * @param irn The node to add.
108  * @return The given node.
109  */
110 void sched_add_before(ir_node *before, ir_node *irn);
111
112
113 /**
114  * Add a node to a block schedule.
115  * @param irn The node to add.
116  * @return The given node.
117  */
118 void sched_add_after(ir_node *after, ir_node *irn);
119
120 static inline void sched_init_block(ir_node *block)
121 {
122         sched_info_t *info = get_irn_sched_info(block);
123         assert(info->next == NULL && info->time_step == 0);
124         info->next = block;
125         info->prev = block;
126 }
127
128 static inline void sched_reset(ir_node *node)
129 {
130         sched_info_t *info = get_irn_sched_info(node);
131         info->next = NULL;
132         info->prev = NULL;
133 }
134
135 /**
136  * Remove a node from the scheduled.
137  * @param irn The node.
138  */
139 void sched_remove(ir_node *irn);
140
141 /**
142  * Remove @p old from the schedule and put @p irn in its place.
143  */
144 void sched_replace(ir_node *old, ir_node *irn);
145
146 /**
147  * Checks, if one node is scheduled before another.
148  * @param n1   A node.
149  * @param n2   Another node.
150  * @return     true, if n1 is in front of n2 in the schedule, false else.
151  * @note       Both nodes must be in the same block.
152  */
153 static inline bool sched_comes_after(const ir_node *n1, const ir_node *n2)
154 {
155         assert((is_Block(n1) ? n1 : get_nodes_block(n1)) == (is_Block(n2) ? n2 : get_nodes_block(n2)));
156         return sched_get_time_step(n1) < sched_get_time_step(n2);
157 }
158
159 #define sched_foreach_after(after, irn) \
160         for (ir_node *irn = (after); !sched_is_end(irn = sched_next(irn));)
161
162 #define sched_foreach_reverse_before(before, irn) \
163         for (ir_node *irn = (before); !sched_is_begin(irn = sched_prev(irn));)
164
165 /**
166  * A shorthand macro for iterating over a schedule.
167  * @param block The block.
168  * @param irn A ir node pointer used as an iterator.
169  */
170 #define sched_foreach(block,irn) \
171         sched_foreach_after((assert(is_Block(block)), block), irn)
172
173 /**
174  * A shorthand macro for reversely iterating over a schedule.
175  * @param block The block.
176  * @param irn A ir node pointer used as an iterator.
177  */
178 #define sched_foreach_reverse(block,irn) \
179         sched_foreach_reverse_before((assert(is_Block(block)), block), irn)
180
181 /**
182  * A shorthand macro for iterating over a schedule while the current node may be
183  * removed or replaced.
184  *
185  * @param block  The block.
186  * @param irn    A ir node pointer used as an iterator.
187  */
188 #define sched_foreach_safe(block, irn) \
189         for (ir_node *irn, *irn##__next = sched_first(block); !sched_is_end(irn = irn##__next) ? irn##__next = sched_next(irn), 1 : 0;)
190
191 /**
192  * A shorthand macro for reversely iterating over a schedule while the current
193  * node may be removed or replaced.
194  *
195  * @param block  The block.
196  * @param irn    A ir node pointer used as an iterator.
197  */
198 #define sched_foreach_reverse_safe(block, irn) \
199         for (ir_node *irn, *irn##__prev = sched_last(block); !sched_is_begin(irn = irn##__prev) ? irn##__prev = sched_prev(irn), 1 : 0;)
200
201 /**
202  * Type for a function scheduling a graph
203  */
204 typedef void (*schedule_func) (ir_graph *irg);
205
206 /**
207  * Register new scheduling algorithm
208  */
209 void be_register_scheduler(const char *name, schedule_func func);
210
211 /**
212  * schedule a graph with the currenty selected scheduler.
213  */
214 void be_schedule_graph(ir_graph *irg);
215
216 #endif