ValueSymbolTable.h 4.7 KB

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