irtypeinfo.c 5.17 KB
Newer Older
Matthias Braun's avatar
Matthias Braun committed
1
/*
2
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
3
 *
Matthias Braun's avatar
Matthias Braun committed
4
 * This file is part of libFirm.
5
 *
Matthias Braun's avatar
Matthias Braun committed
6
7
8
9
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
10
 *
Matthias Braun's avatar
Matthias Braun committed
11
12
13
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
14
 *
Matthias Braun's avatar
Matthias Braun committed
15
16
17
18
19
20
21
22
23
24
25
26
27
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

/**
 * @file
 * @brief     Data structure to hold type information for nodes.
 * @author    Goetz Lindenmaier
 * @date      28.8.2003
 * @version   $Id$
 * @summary
 *  Data structure to hold type information for nodes.
28
 *
Matthias Braun's avatar
Matthias Braun committed
29
30
31
 *  This module defines a field "type" of type "type *" for each ir node.
 *  It defines a flag for irgraphs to mark whether the type info of the
 *  graph is valid.  Further it defines an auxiliary type "initial_type".
32
 *
Matthias Braun's avatar
Matthias Braun committed
33
34
35
36
 *  The module defines a map that contains pairs (irnode, type).  If an irnode
 *  is not in the map it is assumed to be initialized, i.e., the initialization
 *  requires no compute time.  As firm nodes can not be freed and reallocated
 *  pointers for nodes are unique (until a call of dead_node_elimination).
37
 */
Michael Beck's avatar
Michael Beck committed
38
39
40
41
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

42
43
44
45
46
#include "irtypeinfo.h"

#include <stddef.h>

#include "irgraph_t.h"   /* for setting the state flag. */
Götz Lindenmaier's avatar
Götz Lindenmaier committed
47
#include "irprog_t.h"
48
#include "irnode_t.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "pmap.h"

/* ------------ The map. ---------------------------------------------- */


static pmap *type_node_map = NULL;


/* ------------ Auxiliary type. --------------------------------------- */

/*  This auxiliary type expresses that a field is uninitialized.  The
 *  variable is set by init_irtypeinfo.  The type is freed by
 *  free_irtypeinfo.
 */
63
ir_type *initial_type = NULL;
64
65
66
67
68
69
70
71
72

/* ------------ Initializing this module. ----------------------------- */

/*  Initializes the type information module.
 *  Generates a type "initial_type" and sets the type of all nodes to this type.
 *  Calling set/get_irn_type is invalid before calling init. Requires memory
 *  in the order of MIN(<calls to set_irn_type>, #irnodes).
 */
void init_irtypeinfo(void) {
73
  int i, n;
74
75
76
77
78
79
80
81

  if (!initial_type)
    initial_type = new_type_class(new_id_from_str("initial_type"));

  /* We need a new, empty map. */
  if (type_node_map) pmap_destroy(type_node_map);
  type_node_map = pmap_create();

82
83
  n = get_irp_n_irgs();
  for (i = 0; i < n; ++i)
Götz Lindenmaier's avatar
Götz Lindenmaier committed
84
    set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
85
86
87
}

void free_irtypeinfo(void) {
88
  int i, n;
89
90
91
92

  if (initial_type) {
    free_type(initial_type);
    initial_type = NULL;
93
94
  }
  //else assert(0 && "call init_type_info before freeing");
95
96
97
98

  if (type_node_map) {
    pmap_destroy(type_node_map);
    type_node_map = NULL;
99
100
  }
  //else assert(0 && "call init_type_info before freeing");
101

102
103
  n = get_irp_n_irgs();
  for (i = 0; i < n; ++i)
Götz Lindenmaier's avatar
Götz Lindenmaier committed
104
    set_irg_typeinfo_state(get_irp_irg(i), ir_typeinfo_none);
105
106
107
108
109
}


/* ------------ Irgraph state handling. ------------------------------- */

Götz Lindenmaier's avatar
Götz Lindenmaier committed
110
void set_irg_typeinfo_state(ir_graph *irg, ir_typeinfo_state s) {
111
112
  assert(is_ir_graph(irg));
  irg->typeinfo_state = s;
Götz Lindenmaier's avatar
Götz Lindenmaier committed
113
114
115
116
  if ((irg->typeinfo_state == ir_typeinfo_consistent) &&
      (irp->typeinfo_state == ir_typeinfo_consistent) &&
      (s                   != ir_typeinfo_consistent)   )
    irp->typeinfo_state = ir_typeinfo_inconsistent;
117
118
}

Götz Lindenmaier's avatar
Götz Lindenmaier committed
119
ir_typeinfo_state get_irg_typeinfo_state(ir_graph *irg) {
120
121
122
123
  assert(is_ir_graph(irg));
  return irg->typeinfo_state;
}

Götz Lindenmaier's avatar
Götz Lindenmaier committed
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

/* Returns accumulated type information state information.
 *
 * Returns ir_typeinfo_consistent if the type information of all irgs is
 * consistent.  Returns ir_typeinfo_inconsistent if at least one irg has inconsistent
 * or no type information.  Returns ir_typeinfo_none if no irg contains type information.
 */
ir_typeinfo_state get_irp_typeinfo_state(void) {
  return irp->typeinfo_state;
}
void set_irp_typeinfo_state(ir_typeinfo_state s) {
  irp->typeinfo_state = s;
}
/* If typeinfo is consistent, sets it to inconsistent. */
void set_irp_typeinfo_inconsistent(void) {
  if (irp->typeinfo_state == ir_typeinfo_consistent)
    irp->typeinfo_state = ir_typeinfo_inconsistent;
}


144
145
146
/* ------------ Irnode type information. ------------------------------ */

/* These routines only work properly if the ir_graph is in state
Götz Lindenmaier's avatar
Götz Lindenmaier committed
147
 * ir_typeinfo_consistent or ir_typeinfo_inconsistent.  They
148
149
 * assume current_ir_graph set properly.
 */
150
151
ir_type *get_irn_typeinfo_type(ir_node *n) {
  ir_type *res = initial_type;
152
  pmap_entry *entry;
153
154
  assert(get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_consistent  ||
	 get_irg_typeinfo_state(get_irn_irg(n)) == ir_typeinfo_inconsistent  );
155

156
157
158
  entry = pmap_find(type_node_map, n);
  if (entry)
    res = entry->value;
159
160
161
162

  return res;
}

163
void set_irn_typeinfo_type(ir_node *n, ir_type *tp) {
Götz Lindenmaier's avatar
Götz Lindenmaier committed
164
165
  assert(get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_consistent  ||
  	 get_irg_typeinfo_state(current_ir_graph) == ir_typeinfo_inconsistent  );
166
167
168

  pmap_insert(type_node_map, (void *)n, (void *)tp);
}