Fixed includes.
[libfirm] / ir / ir / irphase_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irphase_t.c
4  * Purpose:     Phase information handling using node indexes.
5  * Author:      Sebastian Hack
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2007 Universitaet Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifndef _FIRM_IR_PHASE_T_H
14 #define _FIRM_IR_PHASE_T_H
15
16 #include "firm_types.h"
17 #include "obst.h"
18 #include "irgraph_t.h"
19 #include "irtools.h"
20
21 /**
22  * For statistics: A type containing statistic data of a phase object.
23  */
24 typedef struct {
25         unsigned node_slots;       /**< The number of allocated node slots. */
26         unsigned node_slots_used;  /**< The number of used node slots, ie. nodes that have node data. */
27         unsigned node_map_bytes;   /**< Number of used bytes for the node map. */
28         unsigned overall_bytes;    /**< Overall number of used bytes for the phase. */
29 } phase_stat_t;
30
31 /**
32  * Collect Phase statistics.
33  *
34  * @param phase  The phase.
35  * @param stat   Will be filled with the statistical data.
36  */
37 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
38
39 /**
40  * The type of a phase data init function. This callback is called to
41  * (re-) initialize the phase data for each new node.
42  *
43  * @param phase  The phase.
44  * @param irn    The node for which the phase data is (re-) initialized
45  * @param old    The old phase data for this node.
46  *
47  * @return The new (or reinitialized) phase data for this node.
48  *
49  * If newly node data is allocated, old is equal to NULL, else points to the old data.
50  */
51 typedef void *(phase_irn_data_init_t)(ir_phase *phase, ir_node *irn, void *old);
52
53 /**
54  * The default grow factor.
55  * The node => data map does not speculatively allocate more slots.
56  */
57 #define PHASE_DEFAULT_GROWTH (256)
58
59 /**
60  * A phase object.
61  */
62 struct _ir_phase {
63         struct obstack         obst;           /**< The obstack where the irn phase data will be stored on. */
64         const char            *name;           /**< The name of the phase. */
65         ir_graph              *irg;            /**< The irg this phase will we applied to. */
66         unsigned               growth_factor;  /**< The factor to leave room for additional nodes. 256 means 1.0. */
67         void                  *priv;           /**< Some pointer private to the user of the phase. */
68         size_t                 n_data_ptr;     /**< The length of the data_ptr array. */
69         void                 **data_ptr;       /**< Map node indexes to irn data on the obstack. */
70         phase_irn_data_init_t *data_init;      /**< A callback that is called to initialize newly created node data. */
71 };
72
73 /**
74  * Initialize a phase object.
75  *
76  * @param name          The name of the phase. Just for debugging.
77  * @param irg           The graph the phase will run on.
78  * @param growth_factor A factor denoting how many node slots will be additionally allocated,
79  *                      if the node => data is full. The factor is given in units of 1/256, so
80  *                      256 means 1.0.
81  * @param irn_data_init A callback that is called to initialize newly created node data.
82  *                      Must be non-null.
83  * @param priv          Some private pointer which is kept in the phase and can be retrieved with phase_get_private().
84  * @return              A new phase object.
85  */
86 ir_phase *phase_init(ir_phase *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_data_init_t *data_init, void *priv);
87
88 /**
89  * Free the phase and all node data associated with it.
90  *
91  * @param phase  The phase.
92  */
93 void phase_free(ir_phase *phase);
94
95 /**
96  * Re-initialize the irn data for all nodes in the node => data map using the given callback.
97  *
98  * @param phase  The phase.
99  */
100 void phase_reinit_irn_data(ir_phase *phase);
101
102 /**
103  * Re-initialize the irn data for all nodes having phase data in the given block.
104  *
105  * @param phase  The phase.
106  * @param block  The block.
107  *
108  * @note Beware: iterates over all nodes in the graph to find the nodes of the given block.
109  */
110 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
111
112 /**
113  * Re-initialize the irn data for the given node.
114  *
115  * @param phase  The phase.
116  * @param irn    The irn.
117  */
118 #define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
119
120 /**
121  * Returns the first node of the phase having some data assigned.
122  *
123  * @param phase  The phase.
124  *
125  * @return The first irn having some data assigned, NULL otherwise
126  */
127 ir_node *phase_get_first_node(ir_phase *phase);
128
129 /**
130  * Returns the next node after @p start having some data assigned.
131  *
132  * @param phase  The phase.
133  * @param start  The node to start from
134  *
135  * @return The next node after start having some data assigned, NULL otherwise
136  */
137 ir_node *phase_get_next_node(ir_phase *phase, ir_node *start);
138
139 /**
140  * Convenience macro to iterate over all nodes of a phase
141  * having some data assigned.
142  *
143  * @param phase  The phase.
144  * @param irn    A local variable that will hold the current node inside the loop.
145  */
146 #define foreach_phase_irn(phase, irn) \
147         for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
148
149 /**
150  * Get the name of the phase.
151  *
152  * @param phase  The phase.
153  */
154 #define phase_get_name(phase)                 ((phase)->name)
155
156 /**
157  * Get the irg the phase runs on.
158  *
159  * @param phase  The phase.
160  */
161 #define phase_get_irg(phase)                  ((phase)->irg)
162
163 /**
164  * Get private data pointer as passed on creating the phase.
165  *
166  * @param phase  The phase.
167  */
168 #define phase_get_private(phase)              ((phase)->priv)
169
170 /**
171  * Allocate memory in the phase's memory pool.
172  *
173  * @param phase  The phase.
174  * @param size   Number of bytes to allocate.
175  */
176 #define phase_alloc(phase, size)              obstack_alloc(phase_obst(phase), (size))
177
178 /**
179  * Get the obstack of a phase.
180  *
181  * @param phase  The phase.
182  */
183 #define phase_obst(phase)                     (&(phase)->obst)
184
185 /**
186  * Get the phase node data for an irn.
187  *
188  * @param phase   The phase.
189  * @param irn     The irn to get data for.
190  *
191  * @return A pointer to the node data or NULL if the irn has no phase data allocated yet.
192  */
193 #define phase_get_irn_data(phase, irn)        _phase_get_irn_data((phase), (irn))
194
195 /**
196  * Get or set phase data for an irn.
197  *
198  * @param phase  The phase.
199  * @param irn    The irn to get (or set) node data for.
200  *
201  * @return A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
202  */
203 #define phase_get_or_set_irn_data(phase, irn) _phase_get_or_set_irn_data((phase), (irn))
204
205 /**
206  * Set the node data for an irn.
207  *
208  * @param phase  The phase.
209  * @param irn    The node.
210  * @param data   The node data.
211  *
212  * @return The old data or NULL if there was none.
213  */
214 #define phase_set_irn_data(phase, irn, data)  _phase_set_irn_data((phase), (irn), (data))
215
216 /**
217  * This is private and only here for performance reasons.
218  */
219 static INLINE void _phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
220 {
221         int idx;
222
223         if (! phase->data_init)
224                 return;
225
226         idx = get_irn_idx(irn);
227         if (phase->data_ptr[idx])
228                 phase->data_init(phase, irn, phase->data_ptr[idx]);
229 }
230
231
232 /**
233  * This is private and just here for performance reasons.
234  */
235 static INLINE void _private_phase_enlarge(ir_phase *phase, unsigned max_idx)
236 {
237         unsigned last_irg_idx = get_irg_last_idx(phase->irg);
238         size_t old_cap        = phase->n_data_ptr;
239         size_t new_cap;
240
241         /* make the maximum index at least as big as the largest index in the graph. */
242         max_idx = MAX(max_idx, last_irg_idx);
243         new_cap = (size_t) (max_idx * phase->growth_factor / 256);
244
245         phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
246
247         /* initialize the newly allocated memory. */
248         memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
249         phase->n_data_ptr = new_cap;
250 }
251
252 /**
253  * This is private and only here for performance reasons.
254  */
255 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
256
257 static INLINE void *_phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
258 {
259         unsigned idx = get_irn_idx(irn);
260         return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
261 }
262
263 static INLINE void *_phase_set_irn_data(ir_phase *ph, const ir_node *irn, void *data)
264 {
265         unsigned idx = get_irn_idx(irn);
266         void *res;
267
268         /* Assure that there's a sufficient amount of slots. */
269         _private_phase_assure_capacity(ph, idx);
270
271         res = ph->data_ptr[idx];
272         ph->data_ptr[idx] = data;
273
274         return res;
275 }
276
277
278 static INLINE void *_phase_get_or_set_irn_data(ir_phase *ph, ir_node *irn)
279 {
280         unsigned idx = get_irn_idx(irn);
281         void *res;
282
283         /* Assure that there's a sufficient amount of slots. */
284         _private_phase_assure_capacity(ph, idx);
285
286         res = ph->data_ptr[idx];
287
288         /* If there has no irn data allocated yet, do that now. */
289         if(!res) {
290                 phase_irn_data_init_t *data_init = ph->data_init;
291
292                 /* call the node data structure allocator/constructor. */
293                 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
294
295         }
296         return res;
297 }
298
299 #endif /* _FIRM_IR_PHASE_T_H */