added new licence header
[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  * Project:     libFIRM
22  * File name:   ir/ir/irphase.c
23  * Purpose:     Phase information handling using node indexes.
24  * Author:      Sebastian Hack
25  * Modified by:
26  * Created:
27  * SVN-ID:      $Id$
28  * Copyright:   (c) 1998-2006 Universitaet Karlsruhe
29  */
30
31 #ifdef HAVE_CONFIG_H
32 #include "config.h"
33 #endif
34
35 #include "array.h"
36 #include "irnode_t.h"
37 #include "irphase_t.h"
38
39 ir_phase *phase_init(ir_phase *ph, const char *name, ir_graph *irg, unsigned growth_factor, phase_irn_data_init_t *data_init, void *priv)
40 {
41         assert(growth_factor >= 256 && "growth factor must greater or equal to 256/256");
42         assert(data_init && "You must provide a data constructor");
43
44         obstack_init(&ph->obst);
45
46         ph->name          = name;
47         ph->growth_factor = growth_factor;
48         ph->data_init     = data_init;
49         ph->irg           = irg;
50         ph->n_data_ptr    = 0;
51         ph->data_ptr      = NULL;
52         ph->priv          = priv;
53
54         return ph;
55 }
56
57 void phase_free(ir_phase *phase)
58 {
59         obstack_free(&phase->obst, NULL);
60         if(phase->data_ptr)
61                 xfree(phase->data_ptr);
62 }
63
64 phase_stat_t *phase_stat(const ir_phase *phase, phase_stat_t *stat)
65 {
66         int i, n;
67         memset(stat, 0, sizeof(stat[0]));
68
69         stat->node_map_bytes = phase->n_data_ptr * sizeof(phase->data_ptr[0]);
70         stat->node_slots     = phase->n_data_ptr;
71         for(i = 0, n = phase->n_data_ptr; i < n; ++i) {
72                 if(phase->data_ptr[i] != NULL) {
73                         stat->node_slots_used++;
74                 }
75         }
76         stat->overall_bytes = stat->node_map_bytes + obstack_memory_used(&((ir_phase *)phase)->obst);
77         return stat;
78 }
79
80 void phase_reinit_irn_data(ir_phase *phase)
81 {
82         int i, n;
83
84         if (! phase->data_init)
85                 return;
86
87         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
88                 if (phase->data_ptr[i])
89                         phase->data_init(phase, get_idx_irn(phase->irg, i), phase->data_ptr[i]);
90         }
91 }
92
93 void phase_reinit_block_irn_data(ir_phase *phase, ir_node *block)
94 {
95         int i, n;
96
97         if (! phase->data_init)
98                 return;
99
100         for (i = 0, n = phase->n_data_ptr; i < n; ++i) {
101                 if (phase->data_ptr[i]) {
102                         ir_node *irn = get_idx_irn(phase->irg, i);
103                         if (! is_Block(irn) && get_nodes_block(irn) == block)
104                                 phase->data_init(phase, irn, phase->data_ptr[i]);
105                 }
106         }
107 }
108
109 ir_node *phase_get_first_node(ir_phase *phase) {
110         unsigned i;
111
112         for (i = 0; i < phase->n_data_ptr;  ++i)
113                 if (phase->data_ptr[i])
114                         return get_idx_irn(phase->irg, i);
115
116         return NULL;
117 }
118
119 ir_node *phase_get_next_node(ir_phase *phase, ir_node *start) {
120         unsigned i;
121
122         for (i = get_irn_idx(start) + 1; i < phase->n_data_ptr; ++i)
123                 if (phase->data_ptr[i])
124                         return get_idx_irn(phase->irg, i);
125
126         return NULL;
127 }