2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Data structure to hold type information for nodes.
23 * @author Goetz Lindenmaier
26 * Data structure to hold type information for nodes.
28 * This module defines a field "type" of type "type *" for each ir node.
29 * It defines a flag for irgraphs to mark whether the type info of the
30 * graph is valid. Further it defines an auxiliary type "initial_type".
32 * The module defines a map that contains pairs (irnode, type). If an irnode
33 * is not in the map it is assumed to be initialized, i.e., the initialization
34 * requires no compute time. As firm nodes can not be freed and reallocated
35 * pointers for nodes are unique (until a call of dead_node_elimination).
39 #include "irtypeinfo.h"
43 #include "irgraph_t.h"
48 /* ------------ The map. ---------------------------------------------- */
51 static pmap *type_node_map = NULL;
54 /* ------------ Auxiliary type. --------------------------------------- */
56 /* This auxiliary type expresses that a field is uninitialized. The
57 * variable is set by init_irtypeinfo. The type is freed by
60 ir_type *initial_type = NULL;
62 /* ------------ Initializing this module. ----------------------------- */
64 /* Initializes the type information module.
65 * Generates a type "initial_type" and sets the type of all nodes to this type.
66 * Calling set/get_irn_type is invalid before calling init. Requires memory
67 * in the order of MIN(<calls to set_irn_type>, #irnodes).
69 void init_irtypeinfo(void)
73 if (initial_type == NULL)
74 initial_type = new_type_class(new_id_from_str("initial_type"));
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();
81 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
82 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
85 void free_irtypeinfo(void)
89 if (initial_type != NULL) {
90 free_type(initial_type);
94 if (type_node_map != NULL) {
95 pmap_destroy(type_node_map);
99 for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
100 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
104 /* ------------ Irgraph state handling. ------------------------------- */
106 void set_irg_typeinfo_state(ir_graph *irg, ir_typeinfo_state s)
108 assert(is_ir_graph(irg));
109 irg->typeinfo_state = s;
110 if ((irg->typeinfo_state == ir_typeinfo_consistent) &&
111 (irp->typeinfo_state == ir_typeinfo_consistent) &&
112 (s != ir_typeinfo_consistent) )
113 irp->typeinfo_state = ir_typeinfo_inconsistent;
116 ir_typeinfo_state get_irg_typeinfo_state(const ir_graph *irg)
118 assert(is_ir_graph(irg));
119 return irg->typeinfo_state;
123 /* Returns accumulated type information state information.
125 * Returns ir_typeinfo_consistent if the type information of all irgs is
126 * consistent. Returns ir_typeinfo_inconsistent if at least one irg has inconsistent
127 * or no type information. Returns ir_typeinfo_none if no irg contains type information.
129 ir_typeinfo_state get_irp_typeinfo_state(void)
131 return irp->typeinfo_state;
133 void set_irp_typeinfo_state(ir_typeinfo_state s)
135 irp->typeinfo_state = s;
137 /* If typeinfo is consistent, sets it to inconsistent. */
138 void set_irp_typeinfo_inconsistent(void)
140 if (irp->typeinfo_state == ir_typeinfo_consistent)
141 irp->typeinfo_state = ir_typeinfo_inconsistent;
145 /* ------------ Irnode type information. ------------------------------ */
147 /* These routines only work properly if the ir_graph is in state
148 * ir_typeinfo_consistent or ir_typeinfo_inconsistent. They
149 * assume current_ir_graph set properly.
151 ir_type *get_irn_typeinfo_type(const ir_node *n)
153 ir_type *res = initial_type;
156 assert(get_irg_typeinfo_state(get_irn_irg(n)) != ir_typeinfo_none);
158 entry = pmap_find(type_node_map, n);
160 res = (ir_type*) entry->value;
165 void set_irn_typeinfo_type(ir_node *n, ir_type *tp)
167 assert(get_irg_typeinfo_state(current_ir_graph) != ir_typeinfo_none);
169 pmap_insert(type_node_map, (void *)n, (void *)tp);