Added phases using node indexes
[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 "irphase.h"
19
20 typedef struct {
21         unsigned node_slots;
22         unsigned node_slots_used;
23         unsigned node_data_bytes;
24         unsigned node_map_bytes;
25         unsigned overall_bytes;
26 } phase_stat_t;
27
28 /**
29  * Phase statistics.
30  */
31 phase_stat_t *phase_stat(const phase_t *phase, phase_stat_t *stat);
32
33 typedef void (phase_irn_data_init_t)(const phase_t *phase, const ir_node *irn, void *data);
34
35 /**
36  * The default grow factor.
37  * The node => data map does not speculatively allocate more slots.
38  */
39 #define PHASE_DEFAULT_GROWTH (256)
40
41 /**
42  * A phase object.
43  */
44 struct _phase_t {
45         struct obstack obst;        /**< The obstack where the irn phase data will be stored on. */
46         const char *name;           /**< The name of the phase. */
47         ir_graph   *irg;            /**< The irg this phase will we applied to. */
48         unsigned    growth_factor;  /**< factor to leave room for add. nodes. 256 means 1.0. */
49         void       *priv;           /**< Some pointer private to the user of the phase. */
50         size_t      data_size;      /**< The amount of bytes which shall be allocated for each irn. */
51         size_t      n_data_ptr;     /**< The length of the data_ptr array. */
52         void      **data_ptr;       /**< Map node indexes to irn data on the obstack. */
53         phase_irn_data_init_t *data_init; /**< A callback that is called to initialize newly created node data. */
54 };
55
56 /**
57  * Make a new phase.
58  * @param name          The name of the phase.
59  * @param irg           The graph the phase will run on.
60  * @param data_size     The amount of extra storage in bytes that should be allocated for each node.
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_new(const char *name, ir_graph *irg, size_t data_size, unsigned growth_factor, phase_irn_data_init_t *irn_data_init, void *priv);
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  * Get the name of the phase.
77  */
78 #define phase_get_name(ph)                 ((ph)->name)
79
80 /**
81  * Get the irg the phase runs on.
82  */
83 #define phase_get_irg(ph)                  ((ph)->irg)
84
85 /**
86  * Get private data pointer as passed on creating the phase.
87  */
88 #define phase_get_private(ph)              ((ph)->priv)
89
90 /**
91  * Allocate memory in the phase's memory pool.
92  */
93 #define phase_alloc(ph, size)              obstack_alloc(&(ph)->obst, (size))
94
95 /**
96  * Get the phase data for an irn.
97  * @param ph   The phase.
98  * @param irn  The irn to get data for.
99  * @return     A pointer to the data or NULL if the irn has no phase data.
100  */
101 #define phase_get_irn_data(ph, irn)        _phase_get_irn_data((ph), (irn))
102
103 /**
104  * Get or set phase data for an irn.
105  * @param ph   The phase.
106  * @param irn  The irn to get (or set) node data for.
107  * @return     A (non-NULL) pointer to phase data for the irn. Either existent one or newly allocated one.
108  */
109 #define phase_get_or_set_irn_data(ph, irn) _phase_get_or_set_irn_data((ph), (irn))
110
111 /**
112  * This is private and just here for performance reasons.
113  */
114 static INLINE void _private_phase_enlarge(phase_t *phase, unsigned max_idx)
115 {
116         unsigned last_irg_idx = get_irg_last_idx(phase->irg);
117         size_t old_cap        = phase->n_data_ptr;
118         size_t new_cap;
119
120         /* make the maximum index at least as big as the largest index in the graph. */
121         max_idx = MAX(max_idx, last_irg_idx);
122         new_cap = (size_t) (max_idx * phase->growth_factor / 256);
123
124         phase->data_ptr = (void **) realloc(phase->data_ptr, new_cap * sizeof(phase->data_ptr[0]));
125
126         /* initialize the newly allocated memory. */
127         memset(phase->data_ptr + old_cap, 0, (new_cap - old_cap) * sizeof(phase->data_ptr[0]));
128         phase->n_data_ptr = new_cap;
129 }
130
131 /**
132  * This is private and only here for performance reasons.
133  */
134 #define _private_phase_assure_capacity(ph, max_idx) ((max_idx) >= (ph)->n_data_ptr ? (_private_phase_enlarge((ph), (max_idx)), 1) : 1)
135
136 static INLINE void *_phase_get_irn_data(phase_t *ph, const ir_node *irn)
137 {
138         unsigned idx = get_irn_idx(irn);
139         return idx < ph->n_data_ptr ? ph->data_ptr[idx] : NULL;
140 }
141
142 static INLINE void *_phase_get_or_set_irn_data(phase_t *ph, const ir_node *irn)
143 {
144         unsigned idx = get_irn_idx(irn);
145         void *res;
146
147         /* Assure that there's a sufficient amount of slots. */
148         _private_phase_assure_capacity(ph, idx);
149
150         res = ph->data_ptr[idx];
151
152         /* If there has no irn data allocated yet, do that now. */
153         if(!res) {
154                 phase_irn_data_init_t *data_init = ph->data_init;
155                 res = ph->data_ptr[idx] = phase_alloc(ph, ph->data_size);
156
157                 /* Call the irn data callback, if there is one. */
158                 if(data_init)
159                         data_init(ph, irn, res);
160         }
161         return res;
162 }
163
164
165 #endif /* _FIRM_IR_PHASE_T_H */