6 * File name: ir/ana/irtypeinfo.c
7 * Purpose: Data structure to hold type information for nodes.
8 * Author: Goetz Lindenmaier
12 * Copyright: (c) 2003 Universität Karlsruhe
13 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
15 * Data structure to hold type information for nodes.
17 * This module defines a field "type" of type "type *" for each ir node.
18 * It defines a flag for irgraphs to mark whether the type info of the
19 * graph is valid. Further it defines an auxiliary type "initial_type".
21 * The module defines a map that contains pairs (irnode, type). If an irnode
22 * is not in the map it is assumed to be initialized, i.e., the initialization
23 * requires no compute time. As firm nodes can not be freed and reallocated
24 * pointers for nodes are unique (until a call of dead_node_elimination).
32 #include "irtypeinfo.h"
36 #include "irgraph_t.h" /* for setting the state flag. */
41 /* ------------ The map. ---------------------------------------------- */
44 static pmap *type_node_map = NULL;
47 /* ------------ Auxiliary type. --------------------------------------- */
49 /* This auxiliary type expresses that a field is uninitialized. The
50 * variable is set by init_irtypeinfo. The type is freed by
53 ir_type *initial_type = NULL;
55 /* ------------ Initializing this module. ----------------------------- */
57 /* Initializes the type information module.
58 * Generates a type "initial_type" and sets the type of all nodes to this type.
59 * Calling set/get_irn_type is invalid before calling init. Requires memory
60 * in the order of MIN(<calls to set_irn_type>, #irnodes).
62 void init_irtypeinfo(void) {
66 initial_type = new_type_class(new_id_from_str("initial_type"));
68 /* We need a new, empty map. */
69 if (type_node_map) pmap_destroy(type_node_map);
70 type_node_map = pmap_create();
72 for (i = 0; i < get_irp_n_irgs(); ++i)
73 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
76 void free_irtypeinfo(void) {
80 free_type(initial_type);
83 //else assert(0 && "call init_type_info before freeing");
86 pmap_destroy(type_node_map);
89 //else assert(0 && "call init_type_info before freeing");
91 for (i = 0; i < get_irp_n_irgs(); ++i)
92 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
96 /* ------------ Irgraph state handling. ------------------------------- */
98 void set_irg_typeinfo_state(ir_graph *irg, ir_typeinfo_state s) {
99 assert(is_ir_graph(irg));
100 irg->typeinfo_state = s;
101 if ((irg->typeinfo_state == ir_typeinfo_consistent) &&
102 (irp->typeinfo_state == ir_typeinfo_consistent) &&
103 (s != ir_typeinfo_consistent) )
104 irp->typeinfo_state = ir_typeinfo_inconsistent;
107 ir_typeinfo_state get_irg_typeinfo_state(ir_graph *irg) {
108 assert(is_ir_graph(irg));
109 return irg->typeinfo_state;
113 /* Returns accumulated type information state information.
115 * Returns ir_typeinfo_consistent if the type information of all irgs is
116 * consistent. Returns ir_typeinfo_inconsistent if at least one irg has inconsistent
117 * or no type information. Returns ir_typeinfo_none if no irg contains type information.
119 ir_typeinfo_state get_irp_typeinfo_state(void) {
120 return irp->typeinfo_state;
122 void set_irp_typeinfo_state(ir_typeinfo_state s) {
123 irp->typeinfo_state = s;
125 /* If typeinfo is consistent, sets it to inconsistent. */
126 void set_irp_typeinfo_inconsistent(void) {
127 if (irp->typeinfo_state == ir_typeinfo_consistent)
128 irp->typeinfo_state = ir_typeinfo_inconsistent;
132 /* ------------ Irnode type information. ------------------------------ */
134 /* These routines only work properly if the ir_graph is in state
135 * ir_typeinfo_consistent or ir_typeinfo_inconsistent. They
136 * assume current_ir_graph set properly.
138 ir_type *get_irn_typeinfo_type(ir_node *n) {
139 ir_type *res = initial_type;
140 assert(get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_consistent ||
141 get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_inconsistent );
143 if (pmap_contains(type_node_map, (void *)n))
144 res = (ir_type *) pmap_get(type_node_map, (void *)n);
149 void set_irn_typeinfo_type(ir_node *n, ir_type *tp) {
150 assert(get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_consistent ||
151 get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_inconsistent );
153 pmap_insert(type_node_map, (void *)n, (void *)tp);