Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame^] | 1 | //===- llvm/ValueSymbolTable.h - Implement a Value Symtab -------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the name/Value symbol table for LLVM. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_IR_VALUESYMBOLTABLE_H |
| 15 | #define LLVM_IR_VALUESYMBOLTABLE_H |
| 16 | |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/IR/Value.h" |
| 20 | #include <cstdint> |
| 21 | |
| 22 | namespace llvm { |
| 23 | |
| 24 | class Argument; |
| 25 | class BasicBlock; |
| 26 | class Function; |
| 27 | class GlobalAlias; |
| 28 | class GlobalIFunc; |
| 29 | class GlobalVariable; |
| 30 | class Instruction; |
| 31 | template <unsigned InternalLen> class SmallString; |
| 32 | template <typename ValueSubClass> class SymbolTableListTraits; |
| 33 | |
| 34 | /// This class provides a symbol table of name/value pairs. It is essentially |
| 35 | /// a std::map<std::string,Value*> but has a controlled interface provided by |
| 36 | /// LLVM as well as ensuring uniqueness of names. |
| 37 | /// |
| 38 | class ValueSymbolTable { |
| 39 | friend class SymbolTableListTraits<Argument>; |
| 40 | friend class SymbolTableListTraits<BasicBlock>; |
| 41 | friend class SymbolTableListTraits<Function>; |
| 42 | friend class SymbolTableListTraits<GlobalAlias>; |
| 43 | friend class SymbolTableListTraits<GlobalIFunc>; |
| 44 | friend class SymbolTableListTraits<GlobalVariable>; |
| 45 | friend class SymbolTableListTraits<Instruction>; |
| 46 | friend class Value; |
| 47 | |
| 48 | /// @name Types |
| 49 | /// @{ |
| 50 | public: |
| 51 | /// @brief A mapping of names to values. |
| 52 | using ValueMap = StringMap<Value*>; |
| 53 | |
| 54 | /// @brief An iterator over a ValueMap. |
| 55 | using iterator = ValueMap::iterator; |
| 56 | |
| 57 | /// @brief A const_iterator over a ValueMap. |
| 58 | using const_iterator = ValueMap::const_iterator; |
| 59 | |
| 60 | /// @} |
| 61 | /// @name Constructors |
| 62 | /// @{ |
| 63 | |
| 64 | ValueSymbolTable() : vmap(0) {} |
| 65 | ~ValueSymbolTable(); |
| 66 | |
| 67 | /// @} |
| 68 | /// @name Accessors |
| 69 | /// @{ |
| 70 | |
| 71 | /// This method finds the value with the given \p Name in the |
| 72 | /// the symbol table. |
| 73 | /// @returns the value associated with the \p Name |
| 74 | /// @brief Lookup a named Value. |
| 75 | Value *lookup(StringRef Name) const { return vmap.lookup(Name); } |
| 76 | |
| 77 | /// @returns true iff the symbol table is empty |
| 78 | /// @brief Determine if the symbol table is empty |
| 79 | inline bool empty() const { return vmap.empty(); } |
| 80 | |
| 81 | /// @brief The number of name/type pairs is returned. |
| 82 | inline unsigned size() const { return unsigned(vmap.size()); } |
| 83 | |
| 84 | /// This function can be used from the debugger to display the |
| 85 | /// content of the symbol table while debugging. |
| 86 | /// @brief Print out symbol table on stderr |
| 87 | void dump() const; |
| 88 | |
| 89 | /// @} |
| 90 | /// @name Iteration |
| 91 | /// @{ |
| 92 | |
| 93 | /// @brief Get an iterator that from the beginning of the symbol table. |
| 94 | inline iterator begin() { return vmap.begin(); } |
| 95 | |
| 96 | /// @brief Get a const_iterator that from the beginning of the symbol table. |
| 97 | inline const_iterator begin() const { return vmap.begin(); } |
| 98 | |
| 99 | /// @brief Get an iterator to the end of the symbol table. |
| 100 | inline iterator end() { return vmap.end(); } |
| 101 | |
| 102 | /// @brief Get a const_iterator to the end of the symbol table. |
| 103 | inline const_iterator end() const { return vmap.end(); } |
| 104 | |
| 105 | /// @} |
| 106 | /// @name Mutators |
| 107 | /// @{ |
| 108 | private: |
| 109 | ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName); |
| 110 | |
| 111 | /// This method adds the provided value \p N to the symbol table. The Value |
| 112 | /// must have a name which is used to place the value in the symbol table. |
| 113 | /// If the inserted name conflicts, this renames the value. |
| 114 | /// @brief Add a named value to the symbol table |
| 115 | void reinsertValue(Value *V); |
| 116 | |
| 117 | /// createValueName - This method attempts to create a value name and insert |
| 118 | /// it into the symbol table with the specified name. If it conflicts, it |
| 119 | /// auto-renames the name and returns that instead. |
| 120 | ValueName *createValueName(StringRef Name, Value *V); |
| 121 | |
| 122 | /// This method removes a value from the symbol table. It leaves the |
| 123 | /// ValueName attached to the value, but it is no longer inserted in the |
| 124 | /// symtab. |
| 125 | void removeValueName(ValueName *V); |
| 126 | |
| 127 | /// @} |
| 128 | /// @name Internal Data |
| 129 | /// @{ |
| 130 | |
| 131 | ValueMap vmap; ///< The map that holds the symbol table. |
| 132 | mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names |
| 133 | |
| 134 | /// @} |
| 135 | }; |
| 136 | |
| 137 | } // end namespace llvm |
| 138 | |
| 139 | #endif // LLVM_IR_VALUESYMBOLTABLE_H |