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