LLVM API Documentation

Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

SlotTable.h

Go to the documentation of this file.
00001 //===-- Internal/SlotTable.h - Type/Value Slot Holder -----------*- C++ -*-===//
00002 // 
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by Reid Spencer and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 // 
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file declares the SlotTable class for type plane numbering.
00011 // 
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_INTERNAL_SLOTTABLE_H
00015 #define LLVM_INTERNAL_SLOTTABLE_H
00016 
00017 #include <vector>
00018 #include <map>
00019 
00020 namespace llvm {
00021 
00022 // Forward declarations
00023 class Value;
00024 class Type;
00025 class Module;
00026 class Function;
00027 class SymbolTable;
00028 class ConstantArray;
00029 
00030 /// This class is the common abstract data type for both the SlotMachine and
00031 /// the SlotCalculator. It provides the two-way mapping between Values and 
00032 /// Slots as well as the two-way mapping between Types and Slots. For Values,
00033 /// the slot number can be extracted by simply using the getSlot()
00034 /// method and passing in the Value. For Types, it is the same. 
00035 /// @brief Abstract data type for slot numbers.
00036 class SlotTable
00037 {
00038 /// @name Types
00039 /// @{
00040 public:
00041 
00042   /// This type is used throughout the code to make it clear that 
00043   /// an unsigned value refers to a Slot number and not something else.
00044   /// @brief Type slot number identification type.
00045   typedef unsigned SlotNum;
00046 
00047   /// This type is used throughout the code to make it clear that an
00048   /// unsigned value refers to a type plane number and not something else.
00049   /// @brief The type of a plane number (corresponds to Type::TypeID).
00050   typedef unsigned PlaneNum;
00051 
00052   /// @brief Some constants used as flags instead of actual slot numbers
00053   enum Constants {
00054       MAX_SLOT = 4294967294U,
00055       BAD_SLOT = 4294967295U
00056   };
00057 
00058   /// @brief A single plane of Values. Intended index is slot number.
00059   typedef std::vector<const Value*> ValuePlane; 
00060 
00061   /// @brief A table of Values. Intended index is Type::TypeID.
00062   typedef std::vector<ValuePlane> ValueTable; 
00063 
00064   /// @brief A map of values to slot numbers.
00065   typedef std::map<const Value*,SlotNum> ValueMap; 
00066 
00067   /// @brief A single plane of Types. Intended index is slot number.
00068   typedef std::vector<const Type*>  TypePlane;
00069 
00070   /// @brief A map of types to slot numbers.
00071   typedef std::map<const Type*,SlotNum> TypeMap;
00072 
00073 /// @}
00074 /// @name Constructors
00075 /// @{
00076 public:
00077   /// This constructor initializes all the containers in the SlotTable
00078   /// to empty and then inserts all the primitive types into the type plane
00079   /// by default. This is done as a convenience since most uses of the
00080   /// SlotTable will need the primitive types. If you don't need them, pass
00081   /// in true.
00082   /// @brief Default Constructor
00083   explicit SlotTable( 
00084       bool dont_insert_primitives = false ///< Control insertion of primitives.
00085   );
00086 
00087 /// @}
00088 /// @name Accessors
00089 /// @{
00090 public:
00091   /// @brief Get the number of planes of values.
00092   size_t value_size() const { return vTable.size(); }
00093 
00094   /// @brief Get the number of types.
00095   size_t type_size() const { return tPlane.size(); }
00096 
00097   /// @brief Determine if a specific type plane in the value table exists
00098   bool plane_exists(PlaneNum plane) const {
00099     return vTable.size() > plane;
00100   }
00101 
00102   /// @brief Determine if a specific type plane in the value table is empty
00103   bool plane_empty(PlaneNum plane) const {
00104     return (plane_exists(plane) ? vTable[plane].empty() : true);
00105   }
00106 
00107   /// @brief Get the number of entries in a specific plane of the value table
00108   size_t plane_size(PlaneNum plane) const {
00109     return (plane_exists(plane) ? vTable[plane].size() : 0 );
00110   }
00111 
00112   /// @returns true if the slot table is completely empty.
00113   /// @brief Determine if the SlotTable is empty.
00114   bool empty() const;
00115 
00116   /// @returns the slot number or BAD_SLOT if Val is not in table.
00117   /// @brief Get a slot number for a Value.
00118   SlotNum getSlot(const Value* Val) const;
00119 
00120   /// @returns the slot number or BAD_SLOT if Type is not in the table.
00121   /// @brief Get a slot number for a Type.
00122   SlotNum getSlot(const Type* Typ) const;
00123 
00124   /// @returns true iff the Value is in the table.
00125   /// @brief Determine if a Value has a slot number.
00126   bool hasSlot(const Value* Val) { return getSlot(Val) != BAD_SLOT; }
00127 
00128   /// @returns true iff the Type is in the table.
00129   /// @brief Determine if a Type has a slot number.
00130   bool hasSlot(const Type* Typ) { return getSlot(Typ) != BAD_SLOT; }
00131 
00132 /// @}
00133 /// @name Mutators
00134 /// @{
00135 public:
00136   /// @brief Completely clear the SlotTable;
00137   void clear();
00138 
00139   /// @brief Resize the table to incorporate at least \p new_size planes
00140   void resize( size_t new_size );
00141 
00142   /// @returns the slot number of the newly inserted value in its plane
00143   /// @brief Add a Value to the SlotTable
00144   SlotNum insert(const Value* Val, PlaneNum plane );
00145 
00146   /// @returns the slot number of the newly inserted type
00147   /// @brief Add a Type to the SlotTable
00148   SlotNum insert( const Type* Typ );
00149 
00150   /// @returns the slot number that \p Val had when it was in the table
00151   /// @brief Remove a Value from the SlotTable
00152   SlotNum remove( const Value* Val, PlaneNum plane );
00153 
00154   /// @returns the slot number that \p Typ had when it was in the table
00155   /// @brief Remove a Type from the SlotTable
00156   SlotNum remove( const Type* Typ );
00157 
00158 /// @}
00159 /// @name Implementation Details
00160 /// @{
00161 private:
00162   /// Insert the primitive types into the type plane. This is called
00163   /// by the constructor to initialize the type plane.
00164   void insertPrimitives();
00165 
00166 /// @}
00167 /// @name Data
00168 /// @{
00169 private:
00170   /// A two dimensional table of Values indexed by type and slot number. This
00171   /// allows for efficient lookup of a Value by its type and slot number.
00172   ValueTable vTable;
00173 
00174   /// A map of Values to unsigned integer. This allows for efficient lookup of
00175   /// A Value's slot number in its type plane. 
00176   ValueMap   vMap;
00177 
00178   /// A one dimensional vector of Types indexed by slot number. Types are
00179   /// handled separately because they are not Values. 
00180   TypePlane  tPlane;
00181 
00182   /// A map of Types to unsigned integer. This allows for efficient lookup of
00183   /// a Type's slot number in the type plane.
00184   TypeMap    tMap;
00185 
00186 /// @}
00187 
00188 };
00189 
00190 } // End llvm namespace
00191 
00192 // vim: sw=2 
00193 
00194 #endif