3 * File name: ir/ir/irphase_t.c
4 * Purpose: Phase information handling using node indexes.
5 * Author: Sebastian Hack
9 * Copyright: (c) 1998-2007 Universitaet Karlsruhe
10 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
13 #ifndef _FIRM_IR_PHASE_T_H
14 #define _FIRM_IR_PHASE_T_H
16 #include "firm_types.h"
18 #include "irgraph_t.h"
22 * For statistics: A type containing statistic data of a phase object.
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. */
32 * Collect Phase statistics.
34 * @param phase The phase.
35 * @param stat Will be filled with the statistical data.
37 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
40 * The type of a phase data init function. This callback is called to
41 * (re-) initialize the phase data for each new node.
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.
47 * @return The new (or reinitialized) phase data for this node.
49 * If newly node data is allocated, old is equal to NULL, else points to the old data.
51 typedef void *(phase_irn_data_init_t)(ir_phase *phase, ir_node *irn, void *old);
54 * The default grow factor.
55 * The node => data map does not speculatively allocate more slots.
57 #define PHASE_DEFAULT_GROWTH (256)
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. */
74 * Initialize a phase object.
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
81 * @param irn_data_init A callback that is called to initialize newly created node data.
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.
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);
89 * Free the phase and all node data associated with it.
91 * @param phase The phase.
93 void phase_free(ir_phase *phase);
96 * Re-initialize the irn data for all nodes in the node => data map using the given callback.
98 * @param phase The phase.
100 void phase_reinit_irn_data(ir_phase *phase);
103 * Re-initialize the irn data for all nodes having phase data in the given block.
105 * @param phase The phase.
106 * @param block The block.
108 * @note Beware: iterates over all nodes in the graph to find the nodes of the given block.
110 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
113 * Re-initialize the irn data for the given node.
115 * @param phase The phase.
116 * @param irn The irn.
118 #define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
121 * Returns the first node of the phase having some data assigned.
123 * @param phase The phase.
125 * @return The first irn having some data assigned, NULL otherwise
127 ir_node *phase_get_first_node(ir_phase *phase);
130 * Returns the next node after @p start having some data assigned.
132 * @param phase The phase.
133 * @param start The node to start from
135 * @return The next node after start having some data assigned, NULL otherwise
137 ir_node *phase_get_next_node(ir_phase *phase, ir_node *start);
140 * Convenience macro to iterate over all nodes of a phase
141 * having some data assigned.
143 * @param phase The phase.
144 * @param irn A local variable that will hold the current node inside the loop.
146 #define foreach_phase_irn(phase, irn) \
147 for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
150 * Get the name of the phase.
152 * @param phase The phase.
154 #define phase_get_name(phase) ((phase)->name)
157 * Get the irg the phase runs on.
159 * @param phase The phase.
161 #define phase_get_irg(phase) ((phase)->irg)
164 * Get private data pointer as passed on creating the phase.
166 * @param phase The phase.
168 #define phase_get_private(phase) ((phase)->priv)
171 * Allocate memory in the phase's memory pool.
173 * @param phase The phase.
174 * @param size Number of bytes to allocate.
176 #define phase_alloc(phase, size) obstack_alloc(phase_obst(phase), (size))
179 * Get the obstack of a phase.
181 * @param phase The phase.
183 #define phase_obst(phase) (&(phase)->obst)
186 * Get the phase node data for an irn.
188 * @param phase The phase.
189 * @param irn The irn to get data for.
191 * @return A pointer to the node data or NULL if the irn has no phase data allocated yet.
193 #define phase_get_irn_data(phase, irn) _phase_get_irn_data((phase), (irn))
196 * Get or set phase data for an irn.
198 * @param phase The phase.
199 * @param irn The irn to get (or set) node data for.
201 * @return A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
203 #define phase_get_or_set_irn_data(phase, irn) _phase_get_or_set_irn_data((phase), (irn))
206 * Set the node data for an irn.
208 * @param phase The phase.
209 * @param irn The node.
210 * @param data The node data.
212 * @return The old data or NULL if there was none.
214 #define phase_set_irn_data(phase, irn, data) _phase_set_irn_data((phase), (irn), (data))
217 * This is private and only here for performance reasons.
219 static INLINE void _phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
223 if (! phase->data_init)
226 idx = get_irn_idx(irn);
227 if (phase->data_ptr[idx])
228 phase->data_init(phase, irn, phase->data_ptr[idx]);
233 * This is private and just here for performance reasons.
235 static INLINE void _private_phase_enlarge(ir_phase *phase, unsigned max_idx)
237 unsigned last_irg_idx = get_irg_last_idx(phase->irg);
238 size_t old_cap = phase->n_data_ptr;
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);
245 phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
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;
253 * This is private and only here for performance reasons.
255 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
257 static INLINE void *_phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
259 unsigned idx = get_irn_idx(irn);
260 return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
263 static INLINE void *_phase_set_irn_data(ir_phase *ph, const ir_node *irn, void *data)
265 unsigned idx = get_irn_idx(irn);
268 /* Assure that there's a sufficient amount of slots. */
269 _private_phase_assure_capacity(ph, idx);
271 res = ph->data_ptr[idx];
272 ph->data_ptr[idx] = data;
278 static INLINE void *_phase_get_or_set_irn_data(ir_phase *ph, ir_node *irn)
280 unsigned idx = get_irn_idx(irn);
283 /* Assure that there's a sufficient amount of slots. */
284 _private_phase_assure_capacity(ph, idx);
286 res = ph->data_ptr[idx];
288 /* If there has no irn data allocated yet, do that now. */
290 phase_irn_data_init_t *data_init = ph->data_init;
292 /* call the node data structure allocator/constructor. */
293 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
299 #endif /* _FIRM_IR_PHASE_T_H */