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"
24 unsigned node_slots_used;
25 unsigned node_data_bytes;
26 unsigned node_map_bytes;
27 unsigned overall_bytes;
33 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
36 * The type of a phase data init function. This callback is called to
37 * (re-) initialize the phase data for each new node.
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.
43 * @return The new (or reinitialized) phase data for this node.
45 * If newly node data is allocated, old is equal to NULL, else points to the old data.
47 typedef void *(phase_irn_data_init_t)(ir_phase *phase, ir_node *irn, void *old);
50 * The default grow factor.
51 * The node => data map does not speculatively allocate more slots.
53 #define PHASE_DEFAULT_GROWTH (256)
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. */
70 * Initialize a phase object.
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
77 * @param irn_data_init A callback that is called to initialize newly created node data.
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.
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);
85 * Free the phase and all node data associated with it.
87 * @param phase The phase.
89 void phase_free(ir_phase *phase);
92 * Re-initialize the irn data for all nodes in the node => data map using the given callback.
94 * @param phase The phase.
96 void phase_reinit_irn_data(ir_phase *phase);
99 * Re-initialize the irn data for all nodes having phase data in the given block.
101 * @param phase The phase.
102 * @param block The block.
104 * @note Beware: iterates over all nodes in the graph to find the nodes of the given block.
106 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
109 * Re-initialize the irn data for the given node.
111 * @param phase The phase.
112 * @param irn The irn.
114 #define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
117 * Returns the first node of the phase having some data assigned.
119 * @param phase The phase.
121 * @return The first irn having some data assigned, NULL otherwise
123 ir_node *phase_get_first_node(ir_phase *phase);
126 * Returns the next node after @p start having some data assigned.
128 * @param phase The phase.
129 * @param start The node to start from
131 * @return The next node after start having some data assigned, NULL otherwise
133 ir_node *phase_get_next_node(ir_phase *phase, ir_node *start);
136 * Convenience macro to iterate over all nodes of a phase
137 * having some data assigned.
139 * @param phase The phase.
140 * @param irn A local variable that will hold the current node inside the loop.
142 #define foreach_phase_irn(phase, irn) \
143 for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
146 * Get the name of the phase.
148 * @param phase The phase.
150 #define phase_get_name(phase) ((phase)->name)
153 * Get the irg the phase runs on.
155 * @param phase The phase.
157 #define phase_get_irg(phase) ((phase)->irg)
160 * Get private data pointer as passed on creating the phase.
162 * @param phase The phase.
164 #define phase_get_private(phase) ((phase)->priv)
167 * Allocate memory in the phase's memory pool.
169 * @param phase The phase.
170 * @param size Number of bytes to allocate.
172 #define phase_alloc(phase, size) obstack_alloc(phase_obst(phase), (size))
175 * Get the obstack of a phase.
177 * @param phase The phase.
179 #define phase_obst(phase) (&(phase)->obst)
182 * Get the phase node data for an irn.
184 * @param phase The phase.
185 * @param irn The irn to get data for.
187 * @return A pointer to the node data or NULL if the irn has no phase data allocated yet.
189 #define phase_get_irn_data(phase, irn) _phase_get_irn_data((phase), (irn))
192 * Get or set phase data for an irn.
194 * @param phase The phase.
195 * @param irn The irn to get (or set) node data for.
197 * @return A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
199 #define phase_get_or_set_irn_data(phase, irn) _phase_get_or_set_irn_data((phase), (irn))
202 * Set the node data for an irn.
204 * @param phase The phase.
205 * @param irn The node.
206 * @param data The node data.
208 * @return The old data or NULL if there was none.
210 #define phase_set_irn_data(phase, irn, data) _phase_set_irn_data((phase), (irn), (data))
213 * This is private and only here for performance reasons.
215 static INLINE void _phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
219 if (! phase->data_init)
222 idx = get_irn_idx(irn);
223 if (phase->data_ptr[idx])
224 phase->data_init(phase, irn, phase->data_ptr[idx]);
229 * This is private and just here for performance reasons.
231 static INLINE void _private_phase_enlarge(ir_phase *phase, unsigned max_idx)
233 unsigned last_irg_idx = get_irg_last_idx(phase->irg);
234 size_t old_cap = phase->n_data_ptr;
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);
241 phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
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;
249 * This is private and only here for performance reasons.
251 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
253 static INLINE void *_phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
255 unsigned idx = get_irn_idx(irn);
256 return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
259 static INLINE void *_phase_set_irn_data(ir_phase *ph, const ir_node *irn, void *data)
261 unsigned idx = get_irn_idx(irn);
264 /* Assure that there's a sufficient amount of slots. */
265 _private_phase_assure_capacity(ph, idx);
267 res = ph->data_ptr[idx];
268 ph->data_ptr[idx] = data;
274 static INLINE void *_phase_get_or_set_irn_data(ir_phase *ph, ir_node *irn)
276 unsigned idx = get_irn_idx(irn);
279 /* Assure that there's a sufficient amount of slots. */
280 _private_phase_assure_capacity(ph, idx);
282 res = ph->data_ptr[idx];
284 /* If there has no irn data allocated yet, do that now. */
286 phase_irn_data_init_t *data_init = ph->data_init;
288 /* call the node data structure allocator/constructor. */
289 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
295 #endif /* _FIRM_IR_PHASE_T_H */