ValueSymbolTable.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. #ifndef LLVM_IR_VALUESYMBOLTABLE_H
  13. #define LLVM_IR_VALUESYMBOLTABLE_H
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/IR/Value.h"
  17. #include <cstdint>
  18. namespace llvm {
  19. class Argument;
  20. class BasicBlock;
  21. class Function;
  22. class GlobalAlias;
  23. class GlobalIFunc;
  24. class GlobalVariable;
  25. class Instruction;
  26. template <unsigned InternalLen> class SmallString;
  27. template <typename ValueSubClass> class SymbolTableListTraits;
  28. /// This class provides a symbol table of name/value pairs. It is essentially
  29. /// a std::map<std::string,Value*> but has a controlled interface provided by
  30. /// LLVM as well as ensuring uniqueness of names.
  31. ///
  32. class ValueSymbolTable {
  33. friend class SymbolTableListTraits<Argument>;
  34. friend class SymbolTableListTraits<BasicBlock>;
  35. friend class SymbolTableListTraits<Function>;
  36. friend class SymbolTableListTraits<GlobalAlias>;
  37. friend class SymbolTableListTraits<GlobalIFunc>;
  38. friend class SymbolTableListTraits<GlobalVariable>;
  39. friend class SymbolTableListTraits<Instruction>;
  40. friend class Value;
  41. /// @name Types
  42. /// @{
  43. public:
  44. /// A mapping of names to values.
  45. using ValueMap = StringMap<Value*>;
  46. /// An iterator over a ValueMap.
  47. using iterator = ValueMap::iterator;
  48. /// A const_iterator over a ValueMap.
  49. using const_iterator = ValueMap::const_iterator;
  50. /// @}
  51. /// @name Constructors
  52. /// @{
  53. ValueSymbolTable(int MaxNameSize = -1) : vmap(0), MaxNameSize(MaxNameSize) {}
  54. ~ValueSymbolTable();
  55. /// @}
  56. /// @name Accessors
  57. /// @{
  58. /// This method finds the value with the given \p Name in the
  59. /// the symbol table.
  60. /// @returns the value associated with the \p Name
  61. /// Lookup a named Value.
  62. Value *lookup(StringRef Name) const {
  63. if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
  64. Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
  65. return vmap.lookup(Name);
  66. }
  67. /// @returns true iff the symbol table is empty
  68. /// Determine if the symbol table is empty
  69. inline bool empty() const { return vmap.empty(); }
  70. /// The number of name/type pairs is returned.
  71. inline unsigned size() const { return unsigned(vmap.size()); }
  72. /// This function can be used from the debugger to display the
  73. /// content of the symbol table while debugging.
  74. /// Print out symbol table on stderr
  75. void dump() const;
  76. /// @}
  77. /// @name Iteration
  78. /// @{
  79. /// Get an iterator that from the beginning of the symbol table.
  80. inline iterator begin() { return vmap.begin(); }
  81. /// Get a const_iterator that from the beginning of the symbol table.
  82. inline const_iterator begin() const { return vmap.begin(); }
  83. /// Get an iterator to the end of the symbol table.
  84. inline iterator end() { return vmap.end(); }
  85. /// Get a const_iterator to the end of the symbol table.
  86. inline const_iterator end() const { return vmap.end(); }
  87. /// @}
  88. /// @name Mutators
  89. /// @{
  90. private:
  91. ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
  92. /// This method adds the provided value \p N to the symbol table. The Value
  93. /// must have a name which is used to place the value in the symbol table.
  94. /// If the inserted name conflicts, this renames the value.
  95. /// Add a named value to the symbol table
  96. void reinsertValue(Value *V);
  97. /// createValueName - This method attempts to create a value name and insert
  98. /// it into the symbol table with the specified name. If it conflicts, it
  99. /// auto-renames the name and returns that instead.
  100. ValueName *createValueName(StringRef Name, Value *V);
  101. /// This method removes a value from the symbol table. It leaves the
  102. /// ValueName attached to the value, but it is no longer inserted in the
  103. /// symtab.
  104. void removeValueName(ValueName *V);
  105. /// @}
  106. /// @name Internal Data
  107. /// @{
  108. ValueMap vmap; ///< The map that holds the symbol table.
  109. int MaxNameSize; ///< The maximum size for each name. If the limit is
  110. ///< exceeded, the name is capped.
  111. mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
  112. /// @}
  113. };
  114. } // end namespace llvm
  115. #endif // LLVM_IR_VALUESYMBOLTABLE_H