Fixed a bug
[libfirm] / ir / be / besched_t.h
1
2 #ifndef _BESCHED_T_H
3 #define _BESCHED_T_H
4
5 #define SCHED_INITIAL_GRANULARITY (1 << 14)
6
7 #include "list.h"
8 #include "irnode_t.h"
9 #include "irgraph_t.h"
10
11 #include "besched.h"
12
13 typedef unsigned int sched_timestep_t;
14
15 extern size_t sched_irn_data_offset;
16
17 /**
18  * The schedule structure which is present at each ir node.
19  */
20 typedef struct _sched_info_t {
21         struct list_head list;         /**< The list head to list the nodes in a schedule. */
22         sched_timestep_t time_step;    /**< If a is after b in a schedule, its time step is
23                                         larger than b's. */
24
25     int scheduled : 1;             /**< 1, if the node is in the schedule of the block, 0 else. */
26 } sched_info_t;
27
28 #define _sched_entry(list_head) (list_entry(list_head, sched_info_t, list))
29
30 #define get_irn_sched_info(irn) get_irn_data(irn, sched_info_t, sched_irn_data_offset)
31 #define get_sched_info_irn(sched_info) get_irn_data_base(sched_info, sched_irn_data_offset)
32
33 /**
34  * Init the scheduling stuff.
35  * To be called from the central backend initialization routine.
36  */
37 void be_sched_init(void);
38
39 /**
40  * Get the time step of an irn in a schedule.
41  * @param irn The node.
42  * @return The time step in the schedule.
43  */
44 static INLINE int _sched_get_time_step(const ir_node *irn)
45 {
46         return get_irn_sched_info(irn)->time_step;
47 }
48
49 /**
50  * Checks, if a node is to appear in a schedule. Such nodes either
51  * consume real data (mode datab) or produce such.
52  * @param irn The node to check for.
53  * @return 1, if the node consumes/produces data, false if not.
54  */
55 static INLINE int to_appear_in_schedule(ir_node *irn)
56 {
57   int i, n;
58
59   for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
60     ir_node *op = get_irn_n(irn, i);
61     if(mode_is_datab(get_irn_mode(op)))
62       return 1;
63   }
64
65   return mode_is_datab(get_irn_mode(irn));
66 }
67
68 /**
69  * Check, if an ir_node has a scheduling successor.
70  * @param irn The ir node.
71  * @return 1, if the node has a scheduling successor, 0 if not.
72  */
73 static INLINE int _sched_has_next(const ir_node *irn)
74 {
75   const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
76         const sched_info_t *info = get_irn_sched_info(irn);
77         const sched_info_t *block_info = get_irn_sched_info(block);
78         return info->list.next != &block_info->list;
79 }
80
81 /**
82  * Check, if an ir_node has a scheduling predecessor.
83  * @param irn The ir node.
84  * @return 1, if the node has a scheduling predecessor, 0 if not.
85  */
86 static INLINE int _sched_has_prev(const ir_node *irn)
87 {
88   const ir_node *block = is_Block(irn) ? irn : get_nodes_block(irn);
89         const sched_info_t *info = get_irn_sched_info(irn);
90         const sched_info_t *block_info = get_irn_sched_info(block);
91         return info->list.prev != &block_info->list;
92 }
93
94 /**
95  * Get the scheduling successor of a node.
96  * @param irn The node.
97  * @return The next ir node in the schedule or the block, if the node has no next node.
98  */
99 static INLINE ir_node *_sched_next(const ir_node *irn)
100 {
101         const sched_info_t *info = get_irn_sched_info(irn);
102         return get_sched_info_irn(_sched_entry(info->list.next));
103 }
104
105 /**
106  * Get the scheduling predecessor of a node.
107  * @param irn The node.
108  * @return The next ir node in the schedule or the block, if the node has no predecessor.
109  * predecessor.
110  */
111 static INLINE ir_node *_sched_prev(const ir_node *irn)
112 {
113         const sched_info_t *info = get_irn_sched_info(irn);
114         return get_sched_info_irn(_sched_entry(info->list.prev));
115 }
116
117 /**
118  * Get the first node in a block schedule.
119  * @param block The block of which to get the schedule.
120  * @return The first node in the schedule or the block itself if there is node in the schedule.
121  */
122 static INLINE ir_node *_sched_first(const ir_node *block)
123 {
124         assert(is_Block(block) && "Need a block here");
125         return _sched_next(block);
126 }
127
128 /**
129  * Get the last node in a schedule.
130  * @param block The block to get the schedule for.
131  * @return The last ir node in a schedule, or the block itself, if there is node in the schedule.
132  * or it is empty.
133  */
134 static INLINE ir_node *_sched_last(const ir_node *block)
135 {
136         assert(is_Block(block) && "Need a block here");
137         return _sched_prev(block);
138 }
139
140 /**
141  * Reassign the time steps in the schedule.
142  * @param block The schedule to update.
143  */
144 void sched_renumber(const ir_node *block);
145
146 static INLINE void _sched_set_time_stamp(ir_node *irn)
147 {
148   sched_info_t *inf = get_irn_sched_info(irn);
149   sched_timestep_t before_ts = _sched_entry(inf->list.prev)->time_step;
150   sched_timestep_t after_ts = _sched_entry(inf->list.next)->time_step;
151
152   /*
153    * If we are the last, we can give us a big time step,
154    * else we have to compute our time step from our
155    * neighbours.
156    */
157   if(after_ts == 0)
158     inf->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
159   else {
160     sched_timestep_t ts = (before_ts + after_ts) / 2;
161
162     /*
163      * If the resolution went out, we have to renumber
164      * this block.
165      */
166     if(ts == before_ts || ts == after_ts)
167       sched_renumber(get_nodes_block(irn));
168   }
169 }
170
171 /**
172  * Add a node to a block schedule.
173  * @param block The block to whose schedule the node shall be added to.
174  * @param irn The node to add.
175  * @return The given node.
176  */
177 static INLINE ir_node *_sched_add_before(ir_node *before, ir_node *irn)
178 {
179   sched_info_t *info = get_irn_sched_info(irn);
180         list_add_tail(&info->list, &get_irn_sched_info(before)->list);
181   _sched_set_time_stamp(irn);
182   info->scheduled = 1;
183         return irn;
184 }
185
186 /**
187  * Add a node to a block schedule.
188  * @param block The block to whose schedule the node shall be added to.
189  * @param irn The node to add.
190  * @return The given node.
191  */
192 static INLINE ir_node *_sched_add_after(ir_node *after, ir_node *irn)
193 {
194   sched_info_t *info = get_irn_sched_info(irn);
195         list_add(&info->list, &get_irn_sched_info(after)->list);
196   _sched_set_time_stamp(irn);
197   info->scheduled = 1;
198         return irn;
199 }
200
201 /**
202  * Check, if thenode is scheduled.
203  * @param irn The node.
204  * @return 1, if the node is scheduled, 0 if not.
205  */
206 static INLINE int _sched_is_scheduled(ir_node *irn)
207 {
208   return get_irn_sched_info(irn)->scheduled;
209 }
210
211 /**
212  * Verify a schedule.
213  * @param block The block whose schedule to verify.
214  * @return      1, if the schedule is proper, 0 if not.
215  */
216 extern int sched_verify(const ir_node *block);
217
218 /**
219  * Verify the schedules in all blocks of the irg.
220  * @param irg The program graph.
221  * @return    1, if all schedules were right, 0 if not.
222  */
223 extern int sched_verify_irg(ir_graph *irg);
224
225
226 #define sched_get_time_step(irn)            _sched_get_time_step(irn)
227 #define sched_has_succ(irn)                               _sched_has_succ(irn)
228 #define sched_has_prev(irn)                               _sched_has_prev(irn)
229 #define sched_succ(irn)                                                   _sched_succ(irn)
230 #define sched_prev(irn)                                                   _sched_prev(irn)
231 #define sched_first(irn)                                                  _sched_first(irn)
232 #define sched_last(irn)                                                   _sched_last(irn)
233 #define sched_add_before(before, irn)   _sched_add_before(before, irn)
234 #define sched_add_after(after, irn)     _sched_add_after(after, irn)
235 #define sched_is_scheduled(irn)         _sched_is_scheduled(irn)
236
237 #endif