867e43be2233e73fc38f498bdeb711778825fe1c
[libfirm] / ir / ana / irtypeinfo.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     Data structure to hold type information for nodes.
23  * @author    Goetz Lindenmaier
24  * @date      28.8.2003
25  * @version   $Id$
26  * @brief
27  *  Data structure to hold type information for nodes.
28  *
29  *  This module defines a field "type" of type "type *" for each ir node.
30  *  It defines a flag for irgraphs to mark whether the type info of the
31  *  graph is valid.  Further it defines an auxiliary type "initial_type".
32  *
33  *  The module defines a map that contains pairs (irnode, type).  If an irnode
34  *  is not in the map it is assumed to be initialized, i.e., the initialization
35  *  requires no compute time.  As firm nodes can not be freed and reallocated
36  *  pointers for nodes are unique (until a call of dead_node_elimination).
37  */
38 #include "config.h"
39
40 #include "irtypeinfo.h"
41
42 #include <stddef.h>
43
44 #include "irgraph_t.h"   /* for setting the state flag. */
45 #include "irprog_t.h"
46 #include "irnode_t.h"
47 #include "pmap.h"
48
49 /* ------------ The map. ---------------------------------------------- */
50
51
52 static pmap *type_node_map = NULL;
53
54
55 /* ------------ Auxiliary type. --------------------------------------- */
56
57 /*  This auxiliary type expresses that a field is uninitialized.  The
58  *  variable is set by init_irtypeinfo.  The type is freed by
59  *  free_irtypeinfo.
60  */
61 ir_type *initial_type = NULL;
62
63 /* ------------ Initializing this module. ----------------------------- */
64
65 /*  Initializes the type information module.
66  *  Generates a type "initial_type" and sets the type of all nodes to this type.
67  *  Calling set/get_irn_type is invalid before calling init. Requires memory
68  *  in the order of MIN(<calls to set_irn_type>, #irnodes).
69  */
70 void init_irtypeinfo(void) {
71         int i;
72
73         if (initial_type == NULL)
74                 initial_type = new_type_class(new_id_from_str("initial_type"));
75
76         /* We need a new, empty map. */
77         if (type_node_map != NULL)
78                 pmap_destroy(type_node_map);
79         type_node_map = pmap_create();
80
81         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
82                 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
83 }
84
85 void free_irtypeinfo(void) {
86         int i;
87
88         if (initial_type != NULL) {
89                 free_type(initial_type);
90                 initial_type = NULL;
91         }
92
93         if (type_node_map != NULL) {
94                 pmap_destroy(type_node_map);
95                 type_node_map = NULL;
96         }
97
98         for (i = get_irp_n_irgs() - 1; i >= 0; --i)
99                 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
100 }
101
102
103 /* ------------ Irgraph state handling. ------------------------------- */
104
105 void set_irg_typeinfo_state(ir_graph *irg, ir_typeinfo_state s) {
106         assert(is_ir_graph(irg));
107         irg->typeinfo_state = s;
108         if ((irg->typeinfo_state == ir_typeinfo_consistent) &&
109             (irp->typeinfo_state == ir_typeinfo_consistent) &&
110             (s                   != ir_typeinfo_consistent)   )
111                 irp->typeinfo_state = ir_typeinfo_inconsistent;
112 }
113
114 ir_typeinfo_state get_irg_typeinfo_state(const ir_graph *irg) {
115         assert(is_ir_graph(irg));
116         return irg->typeinfo_state;
117 }
118
119
120 /* Returns accumulated type information state information.
121  *
122  * Returns ir_typeinfo_consistent if the type information of all irgs is
123  * consistent.  Returns ir_typeinfo_inconsistent if at least one irg has inconsistent
124  * or no type information.  Returns ir_typeinfo_none if no irg contains type information.
125  */
126 ir_typeinfo_state get_irp_typeinfo_state(void) {
127         return irp->typeinfo_state;
128 }
129 void set_irp_typeinfo_state(ir_typeinfo_state s) {
130         irp->typeinfo_state = s;
131 }
132 /* If typeinfo is consistent, sets it to inconsistent. */
133 void set_irp_typeinfo_inconsistent(void) {
134         if (irp->typeinfo_state == ir_typeinfo_consistent)
135                 irp->typeinfo_state = ir_typeinfo_inconsistent;
136 }
137
138
139 /* ------------ Irnode type information. ------------------------------ */
140
141 /* These routines only work properly if the ir_graph is in state
142  * ir_typeinfo_consistent or ir_typeinfo_inconsistent.  They
143  * assume current_ir_graph set properly.
144  */
145 ir_type *get_irn_typeinfo_type(const ir_node *n) {
146         ir_type *res = initial_type;
147         pmap_entry *entry;
148
149         assert(get_irg_typeinfo_state(get_irn_irg(n)) != ir_typeinfo_none);
150
151         entry = pmap_find(type_node_map, n);
152         if (entry != NULL)
153                 res = entry->value;
154
155         return res;
156 }
157
158 void set_irn_typeinfo_type(ir_node *n, ir_type *tp) {
159         assert(get_irg_typeinfo_state(current_ir_graph) != ir_typeinfo_none);
160
161         pmap_insert(type_node_map, (void *)n, (void *)tp);
162 }