fix a bunch of warnings reported by cparser
[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  * @brief
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 void phase_init(ir_phase *phase, ir_graph *irg, phase_irn_init *data_init)
46 {
47         memset(phase, 0, sizeof(*phase));
48
49         obstack_init(&phase->obst);
50         phase->data_init  = data_init;
51         phase->irg        = irg;
52         phase->n_data_ptr = 0;
53         phase->data_ptr   = NULL;
54 }
55
56 ir_phase *new_phase(ir_graph *irg, phase_irn_init *data_init)
57 {
58         ir_phase *phase = xmalloc(sizeof(*phase));
59         phase_init(phase, irg, data_init);
60         return phase;
61 }
62
63 void phase_deinit(ir_phase *phase)
64 {
65         obstack_free(&phase->obst, NULL);
66         if (phase->data_ptr)
67                 xfree(phase->data_ptr);
68 }
69
70 void phase_free(ir_phase *phase)
71 {
72         phase_deinit(phase);
73         xfree(phase);
74 }
75
76 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat)
77 {
78         int i, n;
79         memset(stat, 0, sizeof(stat[0]));
80
81         stat->node_map_bytes = phase->n_data_ptr * sizeof(phase->data_ptr[0]);
82         stat->node_slots     = phase->n_data_ptr;
83         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
84                 if (phase->data_ptr[i] != NULL) {
85                         stat->node_slots_used++;
86                 }
87         }
88         stat->overall_bytes = stat->node_map_bytes + obstack_memory_used(&((ir_phase *)phase)->obst);
89         return stat;
90 }
91
92 void phase_reinit_irn_data(ir_phase *phase)
93 {
94         int i, n;
95
96         if (! phase->data_init)
97                 return;
98
99         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
100                 if (phase->data_ptr[i])
101                         phase->data_init(phase, get_idx_irn(phase->irg, i), phase->data_ptr[i]);
102         }
103 }
104
105 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block)
106 {
107         int i, n;
108
109         if (! phase->data_init)
110                 return;
111
112         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
113                 if (phase->data_ptr[i]) {
114                         ir_node *irn = get_idx_irn(phase->irg, i);
115                         if (! is_Block(irn) && get_nodes_block(irn) == block)
116                                 phase->data_init(phase, irn, phase->data_ptr[i]);
117                 }
118         }
119 }
120
121 ir_node *phase_get_first_node(const ir_phase *phase)
122 {
123         unsigned i;
124
125         for (i = 0; i < phase->n_data_ptr;  ++i)
126                 if (phase->data_ptr[i])
127                         return get_idx_irn(phase->irg, i);
128
129         return NULL;
130 }
131
132 ir_node *phase_get_next_node(const ir_phase *phase, ir_node *start)
133 {
134         unsigned i;
135
136         for (i = get_irn_idx(start) + 1; i < phase->n_data_ptr; ++i)
137                 if (phase->data_ptr[i])
138                         return get_idx_irn(phase->irg, i);
139
140         return NULL;
141 }