a9ccd6af5825a934e9280df5b42ce3ffed5c42a0
[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-2006 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 "obstack.h"
17 #include "irgraph_t.h"
18 #include "irtools.h"
19 #include "irphase.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 phase_t *phase, phase_stat_t *stat);
34
35 typedef void *(phase_irn_data_init_t)(phase_t *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 _phase_t {
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. 256 means 1.0.
63  * @param irn_data_init A callback that is called to initialize newly created node data.
64  * @param priv          Some private pointer which is kept in the phase and can be retrieved with phase_get_private().
65  * @return              A new phase object.
66  */
67 phase_t *phase_init(phase_t *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_data_init_t *data_init);
68
69 /**
70  * Free the phase and all node data associated with it.
71  * @param phase The phase.
72  */
73 void phase_free(phase_t *phase);
74
75 /**
76  * Re-initialize the irn data for all nodes in the node => data map using the given callback.
77  * @param phase The phase.
78  * @note This function will pass NULL to the init function passed to phase_new().
79  */
80 void phase_reinit_irn_data(phase_t *phase);
81
82 /**
83  * Get the name of the phase.
84  */
85 #define phase_get_name(ph)                 ((ph)->name)
86
87 /**
88  * Get the irg the phase runs on.
89  */
90 #define phase_get_irg(ph)                  ((ph)->irg)
91
92 /**
93  * Get private data pointer as passed on creating the phase.
94  */
95 #define phase_get_private(ph)              ((ph)->priv)
96
97 /**
98  * Allocate memory in the phase's memory pool.
99  */
100 #define phase_alloc(ph, size)              obstack_alloc(phase_obst(ph), (size))
101
102 /**
103  * Get the obstack of the phase.
104  */
105 #define phase_obst(ph)                     (&(ph)->obst)
106
107 /**
108  * Get the phase data for an irn.
109  * @param ph   The phase.
110  * @param irn  The irn to get data for.
111  * @return     A pointer to the data or NULL if the irn has no phase data.
112  */
113 #define phase_get_irn_data(ph, irn)        _phase_get_irn_data((ph), (irn))
114
115 /**
116  * Get or set phase data for an irn.
117  * @param ph   The phase.
118  * @param irn  The irn to get (or set) node data for.
119  * @return     A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
120  */
121 #define phase_get_or_set_irn_data(ph, irn) _phase_get_or_set_irn_data((ph), (irn))
122
123 /**
124  * Set the data for an irn.
125  * @param ph The phase.
126  * @param irn The node.
127  * @param data The data.
128  * @return The old data or NULL if there was none.
129  */
130 #define phase_set_irn_data(ph, irn, data)  _phase_set_irn_data((ph), (irn), (data))
131
132 /**
133  * This is private and just here for performance reasons.
134  */
135 static INLINE void _private_phase_enlarge(phase_t *phase, unsigned max_idx)
136 {
137         unsigned last_irg_idx = get_irg_last_idx(phase->irg);
138         size_t old_cap        = phase->n_data_ptr;
139         size_t new_cap;
140
141         /* make the maximum index at least as big as the largest index in the graph. */
142         max_idx = MAX(max_idx, last_irg_idx);
143         new_cap = (size_t) (max_idx * phase->growth_factor / 256);
144
145         phase->data_ptr = (void **)xrealloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
146
147         /* initialize the newly allocated memory. */
148         memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
149         phase->n_data_ptr = new_cap;
150 }
151
152 /**
153  * This is private and only here for performance reasons.
154  */
155 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
156
157 static INLINE void *_phase_get_irn_data(const phase_t *ph, const ir_node *irn)
158 {
159         unsigned idx = get_irn_idx(irn);
160         return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
161 }
162
163 static INLINE void *_phase_set_irn_data(phase_t *ph, const ir_node *irn, void *data)
164 {
165         unsigned idx = get_irn_idx(irn);
166         void *res;
167
168         /* Assure that there's a sufficient amount of slots. */
169         _private_phase_assure_capacity(ph, idx);
170
171         res = ph->data_ptr[idx];
172         ph->data_ptr[idx] = data;
173
174         return res;
175 }
176
177
178 static INLINE void *_phase_get_or_set_irn_data(phase_t *ph, ir_node *irn)
179 {
180         unsigned idx = get_irn_idx(irn);
181         void *res;
182
183         /* Assure that there's a sufficient amount of slots. */
184         _private_phase_assure_capacity(ph, idx);
185
186         res = ph->data_ptr[idx];
187
188         /* If there has no irn data allocated yet, do that now. */
189         if(!res) {
190                 phase_irn_data_init_t *data_init = ph->data_init;
191
192                 /* call the node data structure allocator/constructor. */
193                 res = ph->data_ptr[idx] = data_init(ph, irn, NULL);
194
195         }
196         return res;
197 }
198
199
200 #endif /* _FIRM_IR_PHASE_T_H */