2 * Copyright (C) 1995-2008 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"
33 #include "irphases_t.h"
35 struct _ir_phase_info {
40 typedef struct _ir_phase_info ir_phase_info;
42 typedef void *(phase_irn_init)(ir_phase *phase, const ir_node *irn, void *old);
45 * A default node initializer.
46 * It does nothing and returns NULL.
48 extern phase_irn_init phase_irn_init_default;
54 struct obstack obst; /**< The obstack where the irn phase data will be stored on. */
55 ir_phase_id id; /**< The phase ID. */
56 const char *name; /**< The name of the phase. */
57 ir_graph *irg; /**< The irg this phase will we applied to. */
58 unsigned growth_factor; /**< The factor to leave room for additional nodes. 256 means 1.0. */
59 void *priv; /**< Some pointer private to the user of the phase. */
60 size_t n_data_ptr; /**< The length of the data_ptr array. */
61 void **data_ptr; /**< Map node indexes to irn data on the obstack. */
62 phase_irn_init *data_init; /**< A callback that is called to initialize newly created node data. */
65 #define PHASE_DEFAULT_GROWTH (256)
69 * For statistics: A type containing statistic data of a phase object.
72 unsigned node_slots; /**< The number of allocated node slots. */
73 unsigned node_slots_used; /**< The number of used node slots, i.e. nodes that have node data. */
74 unsigned node_map_bytes; /**< Number of used bytes for the node map. */
75 unsigned overall_bytes; /**< Overall number of used bytes for the phase. */
79 * Collect Phase statistics.
81 * @param phase The phase.
82 * @param stat Will be filled with the statistical data.
84 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
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_init *data_init, void *priv);
102 * Init an irg managed phase.
104 * The first sizeof(ir_phase) bytes will be considered to be a phase object;
105 * they will be properly initialized. The remaining bytes are at the user's disposal.
106 * The returned phase object will be inserted in the phase slot of the @p irg designated by the phase ID (@p id).
107 * Note that you cannot allocate phases with an ID <code>PHASE_NOT_IRG_MANAGED</code>.
109 * @param irg The irg.
110 * @param id The ID of the irg-managed phase (see irphaselist.h).
111 * @param size The size of the phase
112 * @param data_init The node data initialization function.
113 * @return The allocated phase object.
115 ir_phase *init_irg_phase(ir_graph *irg, ir_phase_id id, size_t size, phase_irn_init *data_init);
117 void free_irg_phase(ir_graph *irg, ir_phase_id id);
120 * Free the phase and all node data associated with it.
122 * @param phase The phase.
124 void phase_free(ir_phase *phase);
127 * Re-initialize the irn data for all nodes in the node => data map using the given callback.
129 * @param phase The phase.
131 void phase_reinit_irn_data(ir_phase *phase);
134 * Re-initialize the irn data for all nodes having phase data in the given block.
136 * @param phase The phase.
137 * @param block The block.
139 * @note Beware: iterates over all nodes in the graph to find the nodes of the given block.
141 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
144 * Re-initialize the irn data for the given node.
146 * @param phase The phase.
147 * @param irn The irn.
149 static inline void phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
153 if (! phase->data_init)
156 idx = get_irn_idx(irn);
157 if (phase->data_ptr[idx])
158 phase->data_init(phase, irn, phase->data_ptr[idx]);
162 * Returns the first node of the phase having some data assigned.
164 * @param phase The phase.
166 * @return The first irn having some data assigned, NULL otherwise
168 ir_node *phase_get_first_node(const ir_phase *phase);
171 * Returns the next node after @p start having some data assigned.
173 * @param phase The phase.
174 * @param start The node to start from
176 * @return The next node after start having some data assigned, NULL otherwise
178 ir_node *phase_get_next_node(const ir_phase *phase, ir_node *start);
181 * Convenience macro to iterate over all nodes of a phase
182 * having some data assigned.
184 * @param phase The phase.
185 * @param irn A local variable that will hold the current node inside the loop.
187 #define foreach_phase_irn(phase, irn) \
188 for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
191 * Get the name of the phase.
193 * @param phase The phase.
195 static inline const char *phase_get_name(const ir_phase *phase)
201 * Get the irg the phase runs on.
203 * @param phase The phase.
205 static inline ir_graph *phase_get_irg(const ir_phase *phase)
211 * Get private data pointer as passed on creating the phase.
213 * @param phase The phase.
215 static inline void *phase_get_private(const ir_phase *phase)
221 * Allocate memory in the phase's memory pool.
223 * @param phase The phase.
224 * @param size Number of bytes to allocate.
226 static inline void *phase_alloc(ir_phase *phase, size_t size)
228 return obstack_alloc(&phase->obst, size);
232 * Get the obstack of a phase.
234 * @param phase The phase.
236 static inline struct obstack *phase_obst(ir_phase *phase)
242 * Get the phase node data for an irn.
244 * @param phase The phase.
245 * @param irn The irn to get data for.
247 * @return A pointer to the node data or NULL if the irn has no phase data allocated yet.
249 static inline void *phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
251 unsigned idx = get_irn_idx(irn);
252 return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
256 * This is private and just here for performance reasons.
258 static inline void private_phase_enlarge(ir_phase *phase, unsigned max_idx)
260 unsigned last_irg_idx = get_irg_last_idx(phase->irg);
261 size_t old_cap = phase->n_data_ptr;
264 /* make the maximum index at least as big as the largest index in the graph. */
265 max_idx = MAX(max_idx, last_irg_idx);
266 new_cap = (size_t) (max_idx * phase->growth_factor / 256);
268 phase->data_ptr = XREALLOC(phase->data_ptr, void*, new_cap);
270 /* initialize the newly allocated memory. */
271 memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
272 phase->n_data_ptr = new_cap;
276 * This is private and only here for performance reasons.
278 static inline void private_phase_assure_capacity(ir_phase *ph, unsigned max_idx)
280 if (max_idx >= ph->n_data_ptr)
281 private_phase_enlarge(ph, max_idx);
286 * Get or set phase data for an irn.
288 * @param phase The phase.
289 * @param irn The irn to get (or set) node data for.
291 * @return A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
293 static inline void *phase_get_or_set_irn_data(ir_phase *ph, const ir_node *irn)
295 unsigned idx = get_irn_idx(irn);
298 /* Assure that there's a sufficient amount of slots. */
299 private_phase_assure_capacity(ph, idx + 1);
301 res = ph->data_ptr[idx];
303 /* If there has no irn data allocated yet, do that now. */
305 phase_irn_init *data_init = ph->data_init;
307 /* call the node data structure allocator/constructor. */
308 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
315 * Set the node data for an irn.
317 * @param phase The phase.
318 * @param irn The node.
319 * @param data The node data.
321 * @return The old data or NULL if there was none.
323 static inline void *phase_set_irn_data(ir_phase *ph, const ir_node *irn,
326 unsigned idx = get_irn_idx(irn);
329 /* Assure that there's a sufficient amount of slots. */
330 private_phase_assure_capacity(ph, idx + 1);
332 res = ph->data_ptr[idx];
333 ph->data_ptr[idx] = data;
339 * Get the irg-managed phase for a given phase ID.
340 * @param irg The irg.
342 * @return The corresponding phase, or NULL if there is none.
344 static inline ir_phase *get_irg_phase(const ir_graph *irg, ir_phase_id id)
346 return irg->phases[id];
349 static inline void *get_irn_phase_info(const ir_node *irn, ir_phase_id id)
351 const ir_graph *irg = get_irn_irg(irn);
352 const ir_phase *ph = get_irg_phase(irg, id);
353 assert(ph && "phase info has to be computed");
354 return phase_get_irn_data(ph, irn);
358 * Get or set information a phase holds about a node.
359 * If the given phase does not hold information of the node,
360 * the information structure will be created, initialized (see the data_init function of ir_phase), and returned.
361 * @param irn The node.
362 * @param id The ID of the phase.
364 static inline void *get_or_set_irn_phase_info(const ir_node *irn, ir_phase_id id)
366 const ir_graph *irg = get_irn_irg(irn);
367 ir_phase *ph = get_irg_phase(irg, id);
368 assert(ph && "phase info has to be computed");
369 return phase_get_or_set_irn_data(ph, irn);
372 static inline void *set_irn_phase_info(const ir_node *irn, ir_phase_id id, void *data)
374 const ir_graph *irg = get_irn_irg(irn);
375 ir_phase *ph = get_irg_phase(irg, id);
376 assert(ph && "phase info has to be computed");
377 return phase_set_irn_data(ph, irn, data);