- get_Block_cfgpred_arr() IS supported, but should not be in the official
[libfirm] / ir / ir / irphase.c
1 /*
2  * Copyright (C) 1995-2008 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 #include "config.h"
30
31 #include "array.h"
32 #include "util.h"
33 #include "irnode_t.h"
34 #include "irgraph_t.h"
35 #include "irphase_t.h"
36
37 void *phase_irn_init_default(ir_phase *ph, const ir_node *irn, void *old)
38 {
39         (void) ph;
40         (void) irn;
41         (void) old;
42         return NULL;
43 }
44
45 ir_phase *init_irg_phase(ir_graph *irg, ir_phase_id id, size_t size, phase_irn_init *data_init)
46 {
47         ir_phase *ph;
48
49         size = MAX(sizeof(*ph), size);
50         assert(id != PHASE_NOT_IRG_MANAGED && id < PHASE_LAST);
51         assert(irg->phases[id] == NULL && "you cannot overwrite another irg managed phase");
52
53         ph = xmalloc(size);
54         memset(ph, 0, size);
55         obstack_init(&ph->obst);
56         ph->id            = id;
57         ph->growth_factor = PHASE_DEFAULT_GROWTH;
58         ph->data_init     = data_init;
59         ph->irg           = irg;
60         ph->n_data_ptr    = 0;
61         ph->data_ptr      = NULL;
62
63         irg->phases[id] = ph;
64
65         return ph;
66 }
67
68 void free_irg_phase(ir_graph *irg, ir_phase_id id)
69 {
70         ir_phase *ph = get_irg_phase(irg, id);
71         phase_free(ph);
72         xfree(ph);
73         irg->phases[id] = NULL;
74 }
75
76 ir_phase *phase_init(ir_phase *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_init *data_init, void *priv)
77 {
78         obstack_init(&ph->obst);
79
80         (void) name;
81         ph->id            = PHASE_NOT_IRG_MANAGED;
82         ph->growth_factor = growth_factor;
83         ph->data_init     = data_init;
84         ph->irg           = irg;
85         ph->n_data_ptr    = 0;
86         ph->data_ptr      = NULL;
87         ph->priv          = priv;
88         return ph;
89 }
90
91 void phase_free(ir_phase *phase)
92 {
93         obstack_free(&phase->obst, NULL);
94         if(phase->data_ptr)
95                 xfree(phase->data_ptr);
96 }
97
98 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat)
99 {
100         int i, n;
101         memset(stat, 0, sizeof(stat[0]));
102
103         stat->node_map_bytes = phase->n_data_ptr * sizeof(phase->data_ptr[0]);
104         stat->node_slots     = phase->n_data_ptr;
105         for(i = 0, n = phase->n_data_ptr; i < n; ++i) {
106                 if(phase->data_ptr[i] != NULL) {
107                         stat->node_slots_used++;
108                 }
109         }
110         stat->overall_bytes = stat->node_map_bytes + obstack_memory_used(&((ir_phase *)phase)->obst);
111         return stat;
112 }
113
114 void phase_reinit_irn_data(ir_phase *phase)
115 {
116         int i, n;
117
118         if (! phase->data_init)
119                 return;
120
121         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
122                 if (phase->data_ptr[i])
123                         phase->data_init(phase, get_idx_irn(phase->irg, i), phase->data_ptr[i]);
124         }
125 }
126
127 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block)
128 {
129         int i, n;
130
131         if (! phase->data_init)
132                 return;
133
134         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
135                 if (phase->data_ptr[i]) {
136                         ir_node *irn = get_idx_irn(phase->irg, i);
137                         if (! is_Block(irn) && get_nodes_block(irn) == block)
138                                 phase->data_init(phase, irn, phase->data_ptr[i]);
139                 }
140         }
141 }
142
143 ir_node *phase_get_first_node(const ir_phase *phase) {
144         unsigned i;
145
146         for (i = 0; i < phase->n_data_ptr;  ++i)
147                 if (phase->data_ptr[i])
148                         return get_idx_irn(phase->irg, i);
149
150         return NULL;
151 }
152
153 ir_node *phase_get_next_node(const ir_phase *phase, ir_node *start) {
154         unsigned i;
155
156         for (i = get_irn_idx(start) + 1; i < phase->n_data_ptr; ++i)
157                 if (phase->data_ptr[i])
158                         return get_idx_irn(phase->irg, i);
159
160         return NULL;
161 }