Changed phase node initializer to take const ir_node
[libfirm] / ir / ir / irphase.c
1 /*
2  * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief    Phase information handling using node indexes.
23  * @author   Sebastian Hack
24  * @version  $Id$
25  * @summary
26  *  A phase contains a link to private data for each node in an ir graph.
27  *  A phase is independent from the globally visible link field of ir nodes.
28  */
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "array.h"
34 #include "util.h"
35 #include "irnode_t.h"
36 #include "irgraph_t.h"
37 #include "irphase_t.h"
38
39 void *phase_irn_init_default(ir_phase *ph, const ir_node *irn, void *old)
40 {
41         (void) ph;
42         (void) irn;
43         (void) old;
44         return NULL;
45 }
46
47 ir_phase *init_irg_phase(ir_graph *irg, ir_phase_id id, size_t size, phase_irn_init *data_init)
48 {
49         ir_phase *ph;
50
51         size = MAX(sizeof(*ph), size);
52         assert(id != PHASE_NOT_IRG_MANAGED && id < PHASE_LAST);
53         assert(irg->phases[id] == NULL && "you cannot overwrite another irg managed phase");
54
55         ph = xmalloc(size);
56         memset(ph, 0, size);
57         obstack_init(&ph->obst);
58         ph->id            = id;
59         ph->growth_factor = PHASE_DEFAULT_GROWTH;
60         ph->data_init     = data_init;
61         ph->irg           = irg;
62         ph->n_data_ptr    = 0;
63         ph->data_ptr      = NULL;
64
65         irg->phases[id] = ph;
66
67         return ph;
68 }
69
70 void free_irg_phase(ir_graph *irg, ir_phase_id id)
71 {
72         ir_phase *ph = get_irg_phase(irg, id);
73         phase_free(ph);
74         xfree(ph);
75         irg->phases[id] = NULL;
76 }
77
78 ir_phase *phase_init(ir_phase *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_init *data_init, void *priv)
79 {
80         obstack_init(&ph->obst);
81
82         (void) name;
83         ph->id            = PHASE_NOT_IRG_MANAGED;
84         ph->growth_factor = growth_factor;
85         ph->data_init     = data_init;
86         ph->irg           = irg;
87         ph->n_data_ptr    = 0;
88         ph->data_ptr      = NULL;
89         ph->priv          = priv;
90         return ph;
91 }
92
93 void phase_free(ir_phase *phase)
94 {
95         obstack_free(&phase->obst, NULL);
96         if(phase->data_ptr)
97                 xfree(phase->data_ptr);
98 }
99
100 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat)
101 {
102         int i, n;
103         memset(stat, 0, sizeof(stat[0]));
104
105         stat->node_map_bytes = phase->n_data_ptr * sizeof(phase->data_ptr[0]);
106         stat->node_slots     = phase->n_data_ptr;
107         for(i = 0, n = phase->n_data_ptr; i < n; ++i) {
108                 if(phase->data_ptr[i] != NULL) {
109                         stat->node_slots_used++;
110                 }
111         }
112         stat->overall_bytes = stat->node_map_bytes + obstack_memory_used(&((ir_phase *)phase)->obst);
113         return stat;
114 }
115
116 void phase_reinit_irn_data(ir_phase *phase)
117 {
118         int i, n;
119
120         if (! phase->data_init)
121                 return;
122
123         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
124                 if (phase->data_ptr[i])
125                         phase->data_init(phase, get_idx_irn(phase->irg, i), phase->data_ptr[i]);
126         }
127 }
128
129 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block)
130 {
131         int i, n;
132
133         if (! phase->data_init)
134                 return;
135
136         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
137                 if (phase->data_ptr[i]) {
138                         ir_node *irn = get_idx_irn(phase->irg, i);
139                         if (! is_Block(irn) && get_nodes_block(irn) == block)
140                                 phase->data_init(phase, irn, phase->data_ptr[i]);
141                 }
142         }
143 }
144
145 ir_node *phase_get_first_node(const ir_phase *phase) {
146         unsigned i;
147
148         for (i = 0; i < phase->n_data_ptr;  ++i)
149                 if (phase->data_ptr[i])
150                         return get_idx_irn(phase->irg, i);
151
152         return NULL;
153 }
154
155 ir_node *phase_get_next_node(const ir_phase *phase, ir_node *start) {
156         unsigned i;
157
158         for (i = get_irn_idx(start) + 1; i < phase->n_data_ptr; ++i)
159                 if (phase->data_ptr[i])
160                         return get_idx_irn(phase->irg, i);
161
162         return NULL;
163 }