ValueSymbolTable.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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() : vmap(0) {}
  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 { return vmap.lookup(Name); }
  68. /// @returns true iff the symbol table is empty
  69. /// Determine if the symbol table is empty
  70. inline bool empty() const { return vmap.empty(); }
  71. /// The number of name/type pairs is returned.
  72. inline unsigned size() const { return unsigned(vmap.size()); }
  73. /// This function can be used from the debugger to display the
  74. /// content of the symbol table while debugging.
  75. /// Print out symbol table on stderr
  76. void dump() const;
  77. /// @}
  78. /// @name Iteration
  79. /// @{
  80. /// Get an iterator that from the beginning of the symbol table.
  81. inline iterator begin() { return vmap.begin(); }
  82. /// Get a const_iterator that from the beginning of the symbol table.
  83. inline const_iterator begin() const { return vmap.begin(); }
  84. /// Get an iterator to the end of the symbol table.
  85. inline iterator end() { return vmap.end(); }
  86. /// Get a const_iterator to the end of the symbol table.
  87. inline const_iterator end() const { return vmap.end(); }
  88. /// @}
  89. /// @name Mutators
  90. /// @{
  91. private:
  92. ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
  93. /// This method adds the provided value \p N to the symbol table. The Value
  94. /// must have a name which is used to place the value in the symbol table.
  95. /// If the inserted name conflicts, this renames the value.
  96. /// Add a named value to the symbol table
  97. void reinsertValue(Value *V);
  98. /// createValueName - This method attempts to create a value name and insert
  99. /// it into the symbol table with the specified name. If it conflicts, it
  100. /// auto-renames the name and returns that instead.
  101. ValueName *createValueName(StringRef Name, Value *V);
  102. /// This method removes a value from the symbol table. It leaves the
  103. /// ValueName attached to the value, but it is no longer inserted in the
  104. /// symtab.
  105. void removeValueName(ValueName *V);
  106. /// @}
  107. /// @name Internal Data
  108. /// @{
  109. ValueMap vmap; ///< The map that holds the symbol table.
  110. mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
  111. /// @}
  112. };
  113. } // end namespace llvm
  114. #endif // LLVM_IR_VALUESYMBOLTABLE_H
  115. #ifdef __GNUC__
  116. #pragma GCC diagnostic pop
  117. #endif