Added beirgmod and benode
[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 typedef struct _sched_info_t {
18         struct list_head list;
19         sched_timestep_t time_step;
20 } sched_info_t;
21
22 #define _sched_entry(list_head) (list_entry(list_head, sched_info_t, list))
23
24 #define get_irn_sched_info(irn) get_irn_data(irn, sched_info_t, sched_irn_data_offset)
25 #define get_sched_info_irn(sched_info) get_irn_data_base(sched_info, sched_irn_data_offset)
26
27 /**
28  * Init the scheduling stuff.
29  * To be called from the central backend initialization routine.
30  */
31 void be_sched_init(void);
32
33 /**
34  * Get the time step of an irn in a schedule.
35  * @param irn The node.
36  * @return The time step in the schedule.
37  */
38 static INLINE int _sched_get_time_step(const ir_node *irn)
39 {
40         return get_irn_sched_info(irn)->time_step;
41 }
42
43 /**
44  * Check, if an ir_node has a scheduling successor.
45  * @param irn The ir node.
46  * @return 1, if the node has a scheduling successor, 0 if not.
47  */
48 static INLINE int _sched_has_succ(const ir_node *irn)
49 {
50         const sched_info_t *info = get_irn_sched_info(irn);
51         const sched_info_t *block_info = get_irn_sched_info(get_nodes_block(irn));
52         return info->list.next != &block_info->list;
53 }
54
55 /**
56  * Check, if an ir_node has a scheduling predecessor.
57  * @param irn The ir node.
58  * @return 1, if the node has a scheduling predecessor, 0 if not.
59  */
60 static INLINE int _sched_has_prev(const ir_node *irn)
61 {
62         const sched_info_t *info = get_irn_sched_info(irn);
63         const sched_info_t *block_info = get_irn_sched_info(get_nodes_block(irn));
64         return info->list.prev != &block_info->list;
65 }
66
67 /**
68  * Get the scheduling successor of a node.
69  * @param irn The node.
70  * @return The next ir node in the schedule or NULL, if this node has no
71  * successor.
72  */
73 static INLINE ir_node *_sched_succ(const ir_node *irn)
74 {
75         const sched_info_t *info = get_irn_sched_info(irn);
76         return _sched_has_succ(irn) ? get_sched_info_irn(_sched_entry(info->list.next)) : NULL;
77 }
78
79 /**
80  * Get the scheduling predecessor of a node.
81  * @param irn The node.
82  * @return The next ir node in the schedule or NULL, if this node has no
83  * predecessor.
84  */
85 static INLINE ir_node *_sched_prev(const ir_node *irn)
86 {
87         const sched_info_t *info = get_irn_sched_info(irn);
88         return _sched_has_prev(irn) ? get_sched_info_irn(_sched_entry(info->list.prev)) : NULL;
89 }
90
91 /**
92  * Get the first node in a block schedule.
93  * @param block The block of which to get the schedule.
94  * @return The first node in the schedule or NULL if there is none.
95  */
96 static INLINE ir_node *_sched_first(const ir_node *block)
97 {
98         const sched_info_t *info = get_irn_sched_info(block);
99         assert(is_Block(block) && "Need a block here");
100         return !list_empty(&info->list) ? get_sched_info_irn(_sched_entry(info->list.next)) : NULL;
101 }
102
103 /**
104  * Get the last node in a schedule.
105  * @param block The block to get the schedule for.
106  * @return The last ir node in a schedule, or NULL if no schedule exists
107  * or it is empty.
108  */
109 static INLINE ir_node *_sched_last(const ir_node *block)
110 {
111         const sched_info_t *info = get_irn_sched_info(block);
112         assert(is_Block(block) && "Need a block here");
113         return !list_empty(&info->list) ? get_sched_info_irn(_sched_entry(info->list.prev)) : NULL;
114 }
115
116 /**
117  * Reassign the time steps in the schedule.
118  * @param block The schedule to update.
119  */
120 void sched_renumber(const ir_node *block);
121
122 static INLINE void _sched_set_time_stamp(ir_node *irn)
123 {
124   sched_info_t *inf = get_irn_sched_info(irn);
125   sched_timestep_t before_ts = _sched_entry(inf->list.prev)->time_step;
126   sched_timestep_t after_ts = _sched_entry(inf->list.next)->time_step;
127
128   /*
129    * If we are the last, we can give us a big time step,
130    * else we have to compute our time step from our
131    * neighbours.
132    */
133   if(after_ts == 0)
134     inf->time_step = before_ts + SCHED_INITIAL_GRANULARITY;
135   else {
136     sched_timestep_t ts = (before_ts + after_ts) / 2;
137
138     /*
139      * If the resolution went out, we have to renumber
140      * this block.
141      */
142     if(ts == before_ts || ts == after_ts)
143       sched_renumber(get_nodes_block(irn));
144   }
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 static INLINE ir_node *_sched_add_before(ir_node *before, ir_node *irn)
154 {
155         list_add_tail(&get_irn_sched_info(irn)->list, &get_irn_sched_info(before)->list);
156   _sched_set_time_stamp(irn);
157         return irn;
158 }
159
160 /**
161  * Add a node to a block schedule.
162  * @param block The block to whose schedule the node shall be added to.
163  * @param irn The node to add.
164  * @return The given node.
165  */
166 static INLINE ir_node *_sched_add_after(ir_node *after, ir_node *irn)
167 {
168         list_add(&get_irn_sched_info(irn)->list, &get_irn_sched_info(after)->list);
169   _sched_set_time_stamp(irn);
170         return irn;
171 }
172
173 /**
174  * Verify a schedule.
175  * @param block The block whose schedule to verify.
176  * @return      1, if the schedule is proper, 0 if not.
177  */
178 extern int sched_verify(const ir_node *block);
179
180 /**
181  * Verify the schedules in all blocks of the irg.
182  * @param irg The program graph.
183  * @return    1, if all schedules were right, 0 if not.
184  */
185 extern int sched_verify_irg(ir_graph *irg);
186
187 #define sched_get_time_step(irn)            _sched_get_time_step(irn)
188 #define sched_has_succ(irn)                               _sched_has_succ(irn)
189 #define sched_has_prev(irn)                               _sched_has_prev(irn)
190 #define sched_succ(irn)                                                   _sched_succ(irn)
191 #define sched_prev(irn)                                                   _sched_prev(irn)
192 #define sched_first(irn)                                                  _sched_first(irn)
193 #define sched_last(irn)                                                   _sched_last(irn)
194 #define sched_add_before(before, irn)   _sched_add_before(before, irn)
195 #define sched_add_after(after, irn)     _sched_add_after(after, irn)
196
197 #endif