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