a2d849982e8c440c806d03514b21c9edb2481d97
[libfirm] / ir / ir / irphase_t.h
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/irphase_t.c
4  * Purpose:     Phase information handling using node indexes.
5  * Author:      Sebastian Hack
6  * Modified by:
7  * Created:
8  * CVS-ID:      $Id$
9  * Copyright:   (c) 1998-2007 Universitaet Karlsruhe
10  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
11  */
12
13 #ifndef _FIRM_IR_PHASE_T_H
14 #define _FIRM_IR_PHASE_T_H
15
16 #include "firm_types.h"
17 #include "obstack.h"
18 #include "irgraph_t.h"
19 #include "irtools.h"
20 #include "irtools.h"
21
22 typedef struct {
23         unsigned node_slots;
24         unsigned node_slots_used;
25         unsigned node_data_bytes;
26         unsigned node_map_bytes;
27         unsigned overall_bytes;
28 } phase_stat_t;
29
30 /**
31  * Phase statistics.
32  */
33 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat);
34
35 typedef void *(phase_irn_data_init_t)(ir_phase *phase, ir_node *irn, void *old);
36
37 /**
38  * The default grow factor.
39  * The node => data map does not speculatively allocate more slots.
40  */
41 #define PHASE_DEFAULT_GROWTH (256)
42
43 /**
44  * A phase object.
45  */
46 struct _ir_phase {
47         struct obstack         obst;           /**< The obstack where the irn phase data will be stored on. */
48         const char            *name;           /**< The name of the phase. */
49         ir_graph              *irg;            /**< The irg this phase will we applied to. */
50         unsigned               growth_factor;  /**< factor to leave room for add. nodes. 256 means 1.0. */
51         void                  *priv;           /**< Some pointer private to the user of the phase. */
52         size_t                 n_data_ptr;     /**< The length of the data_ptr array. */
53         void                 **data_ptr;       /**< Map node indexes to irn data on the obstack. */
54         phase_irn_data_init_t *data_init;      /**< A callback that is called to initialize newly created node data. */
55 };
56
57 /**
58  * Initialize a phase object.
59  * @param name          The name of the phase.
60  * @param irg           The graph the phase will run on.
61  * @param growth_factor A factor denoting how many node slots will be additionally allocated,
62  *                      if the node => data is full. The factor is given in units of 1/256, so
63  *                      256 means 1.0.
64  * @param irn_data_init A callback that is called to initialize newly created node data.
65  *                      Must be non-null.
66  * @param priv          Some private pointer which is kept in the phase and can be retrieved with phase_get_private().
67  * @return              A new phase object.
68  */
69 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);
70
71 /**
72  * Free the phase and all node data associated with it.
73  * @param phase The phase.
74  */
75 void phase_free(ir_phase *phase);
76
77 /**
78  * Re-initialize the irn data for all nodes in the node => data map using the given callback.
79  * @param phase The phase.
80  */
81 void phase_reinit_irn_data(ir_phase *phase);
82
83 /**
84  * Re-initialize the irn data for all nodes in the given block.
85  * @param phase The phase.
86  * @param block The block.
87  */
88 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block);
89
90 /**
91  * Re-initialize the irn data for the given node.
92  * @param phase The phase.
93  * @param irn   The irn.
94  */
95 #define phase_reinit_single_irn_data(phase, irn) _phase_reinit_single_irn_data((phase), (irn))
96
97 /**
98  * Returns the first node of the phase having some data assigned.
99  * @param phase The phase.
100  * @return The first irn having some data assigned, NULL otherwise
101  */
102 ir_node *phase_get_first_node(ir_phase *phase);
103
104 /**
105  * Returns the next node after @p start having some data assigned.
106  * @param phase The phase.
107  * @param start The node to start from
108  * @return The next node after start having some data assigned, NULL otherwise
109  */
110 ir_node *phase_get_next_node(ir_phase *phase, ir_node *start);
111
112 /**
113  * Convenience macro to iterate over all nodes of a phase
114  * having some data assigned.
115  */
116 #define foreach_phase_irn(phase, irn) \
117         for (irn = phase_get_first_node(phase); irn; irn = phase_get_next_node(phase, irn))
118
119 /**
120  * Get the name of the phase.
121  */
122 #define phase_get_name(ph)                 ((ph)->name)
123
124 /**
125  * Get the irg the phase runs on.
126  */
127 #define phase_get_irg(ph)                  ((ph)->irg)
128
129 /**
130  * Get private data pointer as passed on creating the phase.
131  */
132 #define phase_get_private(ph)              ((ph)->priv)
133
134 /**
135  * Allocate memory in the phase's memory pool.
136  */
137 #define phase_alloc(ph, size)              obstack_alloc(phase_obst(ph), (size))
138
139 /**
140  * Get the obstack of the phase.
141  */
142 #define phase_obst(ph)                     (&(ph)->obst)
143
144 /**
145  * Get the phase data for an irn.
146  * @param ph   The phase.
147  * @param irn  The irn to get data for.
148  * @return     A pointer to the data or NULL if the irn has no phase data.
149  */
150 #define phase_get_irn_data(ph, irn)        _phase_get_irn_data((ph), (irn))
151
152 /**
153  * Get or set phase data for an irn.
154  * @param ph   The phase.
155  * @param irn  The irn to get (or set) node data for.
156  * @return     A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
157  */
158 #define phase_get_or_set_irn_data(ph, irn) _phase_get_or_set_irn_data((ph), (irn))
159
160 /**
161  * Set the data for an irn.
162  * @param ph The phase.
163  * @param irn The node.
164  * @param data The data.
165  * @return The old data or NULL if there was none.
166  */
167 #define phase_set_irn_data(ph, irn, data)  _phase_set_irn_data((ph), (irn), (data))
168
169 /**
170  * This is private and only here for performance reasons.
171  */
172 static INLINE void _phase_reinit_single_irn_data(ir_phase *phase, ir_node *irn)
173 {
174         int idx;
175
176         if (! phase->data_init)
177                 return;
178
179         idx = get_irn_idx(irn);
180         if (phase->data_ptr[idx])
181                 phase->data_init(phase, irn, phase->data_ptr[idx]);
182 }
183
184
185 /**
186  * This is private and just here for performance reasons.
187  */
188 static INLINE void _private_phase_enlarge(ir_phase *phase, unsigned max_idx)
189 {
190         unsigned last_irg_idx = get_irg_last_idx(phase->irg);
191         size_t old_cap        = phase->n_data_ptr;
192         size_t new_cap;
193
194         /* make the maximum index at least as big as the largest index in the graph. */
195         max_idx = MAX(max_idx, last_irg_idx);
196         new_cap = (size_t) (max_idx * phase->growth_factor / 256);
197
198         phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
199
200         /* initialize the newly allocated memory. */
201         memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
202         phase->n_data_ptr = new_cap;
203 }
204
205 /**
206  * This is private and only here for performance reasons.
207  */
208 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
209
210 static INLINE void *_phase_get_irn_data(const ir_phase *ph, const ir_node *irn)
211 {
212         unsigned idx = get_irn_idx(irn);
213         return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
214 }
215
216 static INLINE void *_phase_set_irn_data(ir_phase *ph, const ir_node *irn, void *data)
217 {
218         unsigned idx = get_irn_idx(irn);
219         void *res;
220
221         /* Assure that there's a sufficient amount of slots. */
222         _private_phase_assure_capacity(ph, idx);
223
224         res = ph->data_ptr[idx];
225         ph->data_ptr[idx] = data;
226
227         return res;
228 }
229
230
231 static INLINE void *_phase_get_or_set_irn_data(ir_phase *ph, ir_node *irn)
232 {
233         unsigned idx = get_irn_idx(irn);
234         void *res;
235
236         /* Assure that there's a sufficient amount of slots. */
237         _private_phase_assure_capacity(ph, idx);
238
239         res = ph->data_ptr[idx];
240
241         /* If there has no irn data allocated yet, do that now. */
242         if(!res) {
243                 phase_irn_data_init_t *data_init = ph->data_init;
244
245                 /* call the node data structure allocator/constructor. */
246                 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
247
248         }
249         return res;
250 }
251
252 #endif /* _FIRM_IR_PHASE_T_H */