2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Phase information handling using node indexes.
23 * @author Sebastian Hack
26 #ifndef FIRM_IR_PHASE_T_H
27 #define FIRM_IR_PHASE_T_H
29 #include "firm_types.h"
31 #include "irgraph_t.h"
35 * For statistics: A type containing statistic data of a phase object.
38 unsigned node_slots; /**< The number of allocated node slots. */
39 unsigned node_slots_used; /**< The number of used node slots, ie. nodes that have node data. */
40 unsigned node_map_bytes; /**< Number of used bytes for the node map. */
41 unsigned overall_bytes; /**< Overall number of used bytes for the phase. */
45 * Collect Phase statistics.
47 * @param phase The phase.
48 * @param stat Will be filled with the statistical data.
50 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
53 * The type of a phase data init function. This callback is called to
54 * (re-) initialize the phase data for each new node.
56 * @param phase The phase.
57 * @param irn The node for which the phase data is (re-) initialized
58 * @param old The old phase data for this node.
60 * @return The new (or reinitialized) phase data for this node.
62 * If newly node data is allocated, old is equal to NULL, else points to the old data.
64 typedef void *(phase_irn_data_init_t)(ir_phase *phase, ir_node *irn, void *old);
67 * The default grow factor.
68 * The node => data map does not speculatively allocate more slots.
70 #define PHASE_DEFAULT_GROWTH (256)
76 struct obstack obst; /**< The obstack where the irn phase data will be stored on. */
77 const char *name; /**< The name of the phase. */
78 ir_graph *irg; /**< The irg this phase will we applied to. */
79 unsigned growth_factor; /**< The factor to leave room for additional nodes. 256 means 1.0. */
80 void *priv; /**< Some pointer private to the user of the phase. */
81 size_t n_data_ptr; /**< The length of the data_ptr array. */
82 void **data_ptr; /**< Map node indexes to irn data on the obstack. */
83 phase_irn_data_init_t *data_init; /**< A callback that is called to initialize newly created node data. */
87 * Initialize a phase object.
89 * @param name The name of the phase. Just for debugging.
90 * @param irg The graph the phase will run on.
91 * @param growth_factor A factor denoting how many node slots will be additionally allocated,
92 * if the node => data is full. The factor is given in units of 1/256, so
94 * @param irn_data_init A callback that is called to initialize newly created node data.
96 * @param priv Some private pointer which is kept in the phase and can be retrieved with phase_get_private().
97 * @return A new phase object.
99 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);
102 * Free the phase and all node data associated with it.
104 * @param phase The phase.
106 void phase_free(ir_phase *phase);
109 * Re-initialize the irn data for all nodes in the node => data map using the given callback.
111 * @param phase The phase.
113 void phase_reinit_irn_data(ir_phase *phase);
116 * Re-initialize the irn data for all nodes having phase data in the given block.
118 * @param phase The phase.
119 * @param block The block.
121 * @note Beware: iterates over all nodes in the graph to find the nodes of the given block.
123 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
126 * Re-initialize the irn data for the given node.
128 * @param phase The phase.
129 * @param irn The irn.
131 #define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
134 * Returns the first node of the phase having some data assigned.
136 * @param phase The phase.
138 * @return The first irn having some data assigned, NULL otherwise
140 ir_node *phase_get_first_node(const ir_phase *phase);
143 * Returns the next node after @p start having some data assigned.
145 * @param phase The phase.
146 * @param start The node to start from
148 * @return The next node after start having some data assigned, NULL otherwise
150 ir_node *phase_get_next_node(const ir_phase *phase, ir_node *start);
153 * Convenience macro to iterate over all nodes of a phase
154 * having some data assigned.
156 * @param phase The phase.
157 * @param irn A local variable that will hold the current node inside the loop.
159 #define foreach_phase_irn(phase, irn) \
160 for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
163 * Get the name of the phase.
165 * @param phase The phase.
167 #define phase_get_name(phase) ((phase)->name)
170 * Get the irg the phase runs on.
172 * @param phase The phase.
174 #define phase_get_irg(phase) ((phase)->irg)
177 * Get private data pointer as passed on creating the phase.
179 * @param phase The phase.
181 #define phase_get_private(phase) ((phase)->priv)
184 * Allocate memory in the phase's memory pool.
186 * @param phase The phase.
187 * @param size Number of bytes to allocate.
189 #define phase_alloc(phase, size) obstack_alloc(phase_obst(phase), (size))
192 * Get the obstack of a phase.
194 * @param phase The phase.
196 #define phase_obst(phase) (&(phase)->obst)
199 * Get the phase node data for an irn.
201 * @param phase The phase.
202 * @param irn The irn to get data for.
204 * @return A pointer to the node data or NULL if the irn has no phase data allocated yet.
206 #define phase_get_irn_data(phase, irn) _phase_get_irn_data((phase), (irn))
209 * Get or set phase data for an irn.
211 * @param phase The phase.
212 * @param irn The irn to get (or set) node data for.
214 * @return A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
216 #define phase_get_or_set_irn_data(phase, irn) _phase_get_or_set_irn_data((phase), (irn))
219 * Set the node data for an irn.
221 * @param phase The phase.
222 * @param irn The node.
223 * @param data The node data.
225 * @return The old data or NULL if there was none.
227 #define phase_set_irn_data(phase, irn, data) _phase_set_irn_data((phase), (irn), (data))
230 * This is private and only here for performance reasons.
232 static INLINE void _phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
236 if (! phase->data_init)
239 idx = get_irn_idx(irn);
240 if (phase->data_ptr[idx])
241 phase->data_init(phase, irn, phase->data_ptr[idx]);
246 * This is private and just here for performance reasons.
248 static INLINE void _private_phase_enlarge(ir_phase *phase, unsigned max_idx)
250 unsigned last_irg_idx = get_irg_last_idx(phase->irg);
251 size_t old_cap = phase->n_data_ptr;
254 /* make the maximum index at least as big as the largest index in the graph. */
255 max_idx = MAX(max_idx, last_irg_idx);
256 new_cap = (size_t) (max_idx * phase->growth_factor / 256);
258 phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
260 /* initialize the newly allocated memory. */
261 memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
262 phase->n_data_ptr = new_cap;
266 * This is private and only here for performance reasons.
268 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
270 static INLINE void *_phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
272 unsigned idx = get_irn_idx(irn);
273 return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
276 static INLINE void *_phase_set_irn_data(ir_phase *ph, const ir_node *irn, void *data)
278 unsigned idx = get_irn_idx(irn);
281 /* Assure that there's a sufficient amount of slots. */
282 _private_phase_assure_capacity(ph, idx);
284 res = ph->data_ptr[idx];
285 ph->data_ptr[idx] = data;
291 static INLINE void *_phase_get_or_set_irn_data(ir_phase *ph, ir_node *irn)
293 unsigned idx = get_irn_idx(irn);
296 /* Assure that there's a sufficient amount of slots. */
297 _private_phase_assure_capacity(ph, idx);
299 res = ph->data_ptr[idx];
301 /* If there has no irn data allocated yet, do that now. */
303 phase_irn_data_init_t *data_init = ph->data_init;
305 /* call the node data structure allocator/constructor. */
306 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);