blob: 78d9ea2710b60535f85989efd6eaf2873c044f7f [file] [log] [blame]
Reid Spencer293a55f2004-05-25 18:44:51 +00001//===-- Internal/SlotTable.h - Type/Value Slot Holder -----------*- C++ -*-===//
Misha Brukman23c6d2c2005-04-21 21:48:46 +00002//
Reid Spencer293a55f2004-05-25 18:44:51 +00003// The LLVM Compiler Infrastructure
4//
5// This file was developed by Reid Spencer and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
Misha Brukman23c6d2c2005-04-21 21:48:46 +00007//
Reid Spencer293a55f2004-05-25 18:44:51 +00008//===----------------------------------------------------------------------===//
9//
10// This file declares the SlotTable class for type plane numbering.
Misha Brukman23c6d2c2005-04-21 21:48:46 +000011//
Reid Spencer293a55f2004-05-25 18:44:51 +000012//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_INTERNAL_SLOTTABLE_H
15#define LLVM_INTERNAL_SLOTTABLE_H
16
17#include <vector>
18#include <map>
19
20namespace llvm {
21
22// Forward declarations
23class Value;
24class Type;
25class Module;
26class Function;
27class SymbolTable;
28class ConstantArray;
29
30/// This class is the common abstract data type for both the SlotMachine and
Misha Brukman23c6d2c2005-04-21 21:48:46 +000031/// the SlotCalculator. It provides the two-way mapping between Values and
Reid Spencer293a55f2004-05-25 18:44:51 +000032/// Slots as well as the two-way mapping between Types and Slots. For Values,
33/// the slot number can be extracted by simply using the getSlot()
Misha Brukman23c6d2c2005-04-21 21:48:46 +000034/// method and passing in the Value. For Types, it is the same.
Reid Spencer293a55f2004-05-25 18:44:51 +000035/// @brief Abstract data type for slot numbers.
36class SlotTable
37{
38/// @name Types
39/// @{
40public:
41
Misha Brukman23c6d2c2005-04-21 21:48:46 +000042 /// This type is used throughout the code to make it clear that
Reid Spencer293a55f2004-05-25 18:44:51 +000043 /// an unsigned value refers to a Slot number and not something else.
44 /// @brief Type slot number identification type.
45 typedef unsigned SlotNum;
46
47 /// This type is used throughout the code to make it clear that an
48 /// unsigned value refers to a type plane number and not something else.
Chris Lattnerf70c22b2004-06-17 18:19:28 +000049 /// @brief The type of a plane number (corresponds to Type::TypeID).
Reid Spencer293a55f2004-05-25 18:44:51 +000050 typedef unsigned PlaneNum;
51
52 /// @brief Some constants used as flags instead of actual slot numbers
53 enum Constants {
54 MAX_SLOT = 4294967294U,
55 BAD_SLOT = 4294967295U
56 };
57
58 /// @brief A single plane of Values. Intended index is slot number.
Misha Brukman23c6d2c2005-04-21 21:48:46 +000059 typedef std::vector<const Value*> ValuePlane;
Reid Spencer293a55f2004-05-25 18:44:51 +000060
Chris Lattnerf70c22b2004-06-17 18:19:28 +000061 /// @brief A table of Values. Intended index is Type::TypeID.
Misha Brukman23c6d2c2005-04-21 21:48:46 +000062 typedef std::vector<ValuePlane> ValueTable;
Reid Spencer293a55f2004-05-25 18:44:51 +000063
64 /// @brief A map of values to slot numbers.
Misha Brukman23c6d2c2005-04-21 21:48:46 +000065 typedef std::map<const Value*,SlotNum> ValueMap;
Reid Spencer293a55f2004-05-25 18:44:51 +000066
67 /// @brief A single plane of Types. Intended index is slot number.
68 typedef std::vector<const Type*> TypePlane;
69
70 /// @brief A map of types to slot numbers.
71 typedef std::map<const Type*,SlotNum> TypeMap;
72
73/// @}
74/// @name Constructors
75/// @{
76public:
77 /// This constructor initializes all the containers in the SlotTable
78 /// to empty and then inserts all the primitive types into the type plane
79 /// by default. This is done as a convenience since most uses of the
80 /// SlotTable will need the primitive types. If you don't need them, pass
81 /// in true.
82 /// @brief Default Constructor
Misha Brukman23c6d2c2005-04-21 21:48:46 +000083 explicit SlotTable(
Reid Spencer293a55f2004-05-25 18:44:51 +000084 bool dont_insert_primitives = false ///< Control insertion of primitives.
85 );
86
87/// @}
88/// @name Accessors
89/// @{
90public:
91 /// @brief Get the number of planes of values.
92 size_t value_size() const { return vTable.size(); }
93
Reid Spencer293a55f2004-05-25 18:44:51 +000094 /// @brief Determine if a specific type plane in the value table exists
95 bool plane_exists(PlaneNum plane) const {
96 return vTable.size() > plane;
97 }
98
99 /// @brief Determine if a specific type plane in the value table is empty
100 bool plane_empty(PlaneNum plane) const {
101 return (plane_exists(plane) ? vTable[plane].empty() : true);
102 }
103
104 /// @brief Get the number of entries in a specific plane of the value table
105 size_t plane_size(PlaneNum plane) const {
106 return (plane_exists(plane) ? vTable[plane].size() : 0 );
107 }
108
109 /// @returns true if the slot table is completely empty.
110 /// @brief Determine if the SlotTable is empty.
111 bool empty() const;
112
113 /// @returns the slot number or BAD_SLOT if Val is not in table.
114 /// @brief Get a slot number for a Value.
115 SlotNum getSlot(const Value* Val) const;
116
117 /// @returns the slot number or BAD_SLOT if Type is not in the table.
118 /// @brief Get a slot number for a Type.
119 SlotNum getSlot(const Type* Typ) const;
120
121 /// @returns true iff the Value is in the table.
122 /// @brief Determine if a Value has a slot number.
123 bool hasSlot(const Value* Val) { return getSlot(Val) != BAD_SLOT; }
124
125 /// @returns true iff the Type is in the table.
126 /// @brief Determine if a Type has a slot number.
127 bool hasSlot(const Type* Typ) { return getSlot(Typ) != BAD_SLOT; }
128
129/// @}
130/// @name Mutators
131/// @{
132public:
133 /// @brief Completely clear the SlotTable;
134 void clear();
135
136 /// @brief Resize the table to incorporate at least \p new_size planes
137 void resize( size_t new_size );
138
139 /// @returns the slot number of the newly inserted value in its plane
140 /// @brief Add a Value to the SlotTable
141 SlotNum insert(const Value* Val, PlaneNum plane );
142
143 /// @returns the slot number of the newly inserted type
144 /// @brief Add a Type to the SlotTable
145 SlotNum insert( const Type* Typ );
146
147 /// @returns the slot number that \p Val had when it was in the table
148 /// @brief Remove a Value from the SlotTable
149 SlotNum remove( const Value* Val, PlaneNum plane );
150
151 /// @returns the slot number that \p Typ had when it was in the table
152 /// @brief Remove a Type from the SlotTable
153 SlotNum remove( const Type* Typ );
154
155/// @}
156/// @name Implementation Details
157/// @{
158private:
159 /// Insert the primitive types into the type plane. This is called
160 /// by the constructor to initialize the type plane.
161 void insertPrimitives();
162
163/// @}
164/// @name Data
165/// @{
166private:
167 /// A two dimensional table of Values indexed by type and slot number. This
168 /// allows for efficient lookup of a Value by its type and slot number.
169 ValueTable vTable;
170
171 /// A map of Values to unsigned integer. This allows for efficient lookup of
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000172 /// A Value's slot number in its type plane.
Reid Spencer293a55f2004-05-25 18:44:51 +0000173 ValueMap vMap;
174
175 /// A one dimensional vector of Types indexed by slot number. Types are
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000176 /// handled separately because they are not Values.
Reid Spencer293a55f2004-05-25 18:44:51 +0000177 TypePlane tPlane;
178
179 /// A map of Types to unsigned integer. This allows for efficient lookup of
180 /// a Type's slot number in the type plane.
181 TypeMap tMap;
182
183/// @}
184
185};
186
187} // End llvm namespace
188
Misha Brukman23c6d2c2005-04-21 21:48:46 +0000189// vim: sw=2
Reid Spencer293a55f2004-05-25 18:44:51 +0000190
191#endif