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