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