beloopana: Remove duplicate comments.
[libfirm] / ir / ana / irtypeinfo.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief     Data structure to hold type information for nodes.
9  * @author    Goetz Lindenmaier
10  * @date      28.8.2003
11  * @brief
12  *  Data structure to hold type information for nodes.
13  *
14  *  This module defines a field "type" of type "type *" for each ir node.
15  *  It defines a flag for irgraphs to mark whether the type info of the
16  *  graph is valid.  Further it defines an auxiliary type "initial_type".
17  *
18  *  The module defines a map that contains pairs (irnode, type).  If an irnode
19  *  is not in the map it is assumed to be initialized, i.e., the initialization
20  *  requires no compute time.  As firm nodes can not be freed and reallocated
21  *  pointers for nodes are unique (until a call of dead_node_elimination).
22  */
23 #include "config.h"
24
25 #include "irtypeinfo.h"
26
27 #include <stddef.h>
28
29 #include "irgraph_t.h"
30 #include "irprog_t.h"
31 #include "irnode_t.h"
32 #include "pmap.h"
33
34 static pmap *type_node_map = NULL;
35
36
37 ir_type *initial_type = NULL;
38
39 void init_irtypeinfo(void)
40 {
41         size_t i, n;
42
43         if (initial_type == NULL)
44                 initial_type = new_type_class(new_id_from_str("initial_type"));
45
46         /* We need a new, empty map. */
47         if (type_node_map != NULL)
48                 pmap_destroy(type_node_map);
49         type_node_map = pmap_create();
50
51         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
52                 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
53 }
54
55 void free_irtypeinfo(void)
56 {
57         size_t i, n;
58
59         if (initial_type != NULL) {
60                 free_type(initial_type);
61                 initial_type = NULL;
62         }
63
64         if (type_node_map != NULL) {
65                 pmap_destroy(type_node_map);
66                 type_node_map = NULL;
67         }
68
69         for (i = 0, n = get_irp_n_irgs(); i < n; ++i)
70                 set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
71 }
72
73
74 void set_irg_typeinfo_state(ir_graph *irg, ir_typeinfo_state s)
75 {
76         assert(is_ir_graph(irg));
77         irg->typeinfo_state = s;
78         if ((irg->typeinfo_state == ir_typeinfo_consistent) &&
79             (irp->typeinfo_state == ir_typeinfo_consistent) &&
80             (s                   != ir_typeinfo_consistent)   )
81                 irp->typeinfo_state = ir_typeinfo_inconsistent;
82 }
83
84 ir_typeinfo_state get_irg_typeinfo_state(const ir_graph *irg)
85 {
86         assert(is_ir_graph(irg));
87         return irg->typeinfo_state;
88 }
89
90
91 ir_typeinfo_state get_irp_typeinfo_state(void)
92 {
93         return irp->typeinfo_state;
94 }
95 void set_irp_typeinfo_state(ir_typeinfo_state s)
96 {
97         irp->typeinfo_state = s;
98 }
99 void set_irp_typeinfo_inconsistent(void)
100 {
101         if (irp->typeinfo_state == ir_typeinfo_consistent)
102                 irp->typeinfo_state = ir_typeinfo_inconsistent;
103 }
104
105
106 ir_type *get_irn_typeinfo_type(const ir_node *n)
107 {
108         ir_type *res;
109         assert(get_irg_typeinfo_state(get_irn_irg(n)) != ir_typeinfo_none);
110
111         res = pmap_get(ir_type, type_node_map, n);
112         if (res == NULL) {
113                 res = initial_type;
114         }
115
116         return res;
117 }
118
119 void set_irn_typeinfo_type(ir_node *n, ir_type *tp)
120 {
121         assert(get_irg_typeinfo_state(get_irn_irg(n)) != ir_typeinfo_none);
122
123         pmap_insert(type_node_map, (void *)n, (void *)tp);
124 }