3a34fba33cf4b1b173beb14b443fa69659818d79
[libfirm] / ir / ana / irtypeinfo.c
1 /**
2  *
3  * @file irtypeinfo.c
4  *
5  * Project:     libFIRM
6  * File name:   ir/ana/irtypeinfo.c
7  * Purpose:     Data structure to hold type information for nodes.
8  * Author:      Goetz Lindenmaier
9  * Modified by:
10  * Created:     28.8.2003
11  * CVS-ID:      $Id$
12  * Copyright:   (c) 2003 Universität Karlsruhe
13  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
14  *
15  * Data structure to hold type information for nodes.
16  *
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".
20  *
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).
25  *
26  */
27
28 #ifdef HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31
32 #include "irtypeinfo.h"
33
34 #include <stddef.h>
35
36 #include "irgraph_t.h"   /* for setting the state flag. */
37 #include "irprog.h"
38 #include "pmap.h"
39
40 /* ------------ The map. ---------------------------------------------- */
41
42
43 static pmap *type_node_map = NULL;
44
45
46 /* ------------ Auxiliary type. --------------------------------------- */
47
48 /*  This auxiliary type expresses that a field is uninitialized.  The
49  *  variable is set by init_irtypeinfo.  The type is freed by
50  *  free_irtypeinfo.
51  */
52 type *initial_type = NULL;
53
54
55 /* ------------ Initializing this module. ----------------------------- */
56
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).
61  */
62 void init_irtypeinfo(void) {
63   int i;
64
65   if (!initial_type)
66     initial_type = new_type_class(new_id_from_str("initial_type"));
67
68   /* We need a new, empty map. */
69   if (type_node_map) pmap_destroy(type_node_map);
70   type_node_map = pmap_create();
71
72   for (i = 0; i < get_irp_n_irgs(); ++i)
73     set_irg_typeinfo_state(get_irp_irg(i), irg_typeinfo_none);
74 }
75
76 void free_irtypeinfo(void) {
77   int i;
78
79   if (initial_type) {
80     free_type(initial_type);
81     initial_type = NULL;
82   } else
83     assert(0 && "call init_type_info before freeing");
84
85   if (type_node_map) {
86     pmap_destroy(type_node_map);
87     type_node_map = NULL;
88   } else
89     assert(0 && "call init_type_info before freeing");
90
91   for (i = 0; i < get_irp_n_irgs(); ++i)
92     set_irg_typeinfo_state(get_irp_irg(i), irg_typeinfo_none);
93 }
94
95
96 /* ------------ Irgraph state handling. ------------------------------- */
97
98 void set_irg_typeinfo_state(ir_graph *irg, irg_typeinfo_state s) {
99   assert(is_ir_graph(irg));
100   irg->typeinfo_state = s;
101 }
102
103 irg_typeinfo_state get_irg_typeinfo_state(ir_graph *irg) {
104   assert(is_ir_graph(irg));
105   return irg->typeinfo_state;
106 }
107
108 /* ------------ Irnode type information. ------------------------------ */
109
110 /* These routines only work properly if the ir_graph is in state
111  * irg_typeinfo_consistent or irg_typeinfo_inconsistent.  They
112  * assume current_ir_graph set properly.
113  */
114 type *get_irn_type(ir_node *n) {
115   type *res = initial_type;
116   assert(get_irg_typeinfo_state(current_ir_graph) == irg_typeinfo_consistent  ||
117          get_irg_typeinfo_state(current_ir_graph) == irg_typeinfo_inconsistent  );
118
119   if (pmap_contains(type_node_map, (void *)n))
120     res = (type *) pmap_get(type_node_map, (void *)n);
121
122   return res;
123 }
124
125 void set_irn_type(ir_node *n, type *tp) {
126   assert(get_irg_typeinfo_state(current_ir_graph) == irg_typeinfo_consistent  ||
127          get_irg_typeinfo_state(current_ir_graph) == irg_typeinfo_inconsistent  );
128
129   pmap_insert(type_node_map, (void *)n, (void *)tp);
130 }