LLVM 19.0.0git
ValueSymbolTable.h
Go to the documentation of this file.
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
21namespace llvm {
22
23class Argument;
24class BasicBlock;
25class Function;
26class GlobalAlias;
27class GlobalIFunc;
28class GlobalVariable;
29class Instruction;
30template <bool ExtraIteratorBits> struct ilist_iterator_bits;
31template <unsigned InternalLen> class SmallString;
32template <typename ValueSubClass, typename ... Args> class SymbolTableListTraits;
33
34/// This class provides a symbol table of name/value pairs. It is essentially
35/// a std::map<std::string,Value*> but has a controlled interface provided by
36/// LLVM as well as ensuring uniqueness of names.
37///
39 friend class SymbolTableListTraits<Argument>;
41 friend class SymbolTableListTraits<Function>;
46 friend class Value;
47
48/// @name Types
49/// @{
50public:
51 /// A mapping of names to values.
53
54 /// An iterator over a ValueMap.
56
57 /// A const_iterator over a ValueMap.
59
60/// @}
61/// @name Constructors
62/// @{
63
64 ValueSymbolTable(int MaxNameSize = -1) : vmap(0), MaxNameSize(MaxNameSize) {}
66
67 /// @}
68 /// @name Accessors
69 /// @{
70
71 /// This method finds the value with the given \p Name in the
72 /// the symbol table.
73 /// @returns the value associated with the \p Name
74 /// Lookup a named Value.
76 if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
77 Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
78
79 return vmap.lookup(Name);
80 }
81
82 /// @returns true iff the symbol table is empty
83 /// Determine if the symbol table is empty
84 inline bool empty() const { return vmap.empty(); }
85
86 /// The number of name/type pairs is returned.
87 inline unsigned size() const { return unsigned(vmap.size()); }
88
89 /// This function can be used from the debugger to display the
90 /// content of the symbol table while debugging.
91 /// Print out symbol table on stderr
92 void dump() const;
93
94/// @}
95/// @name Iteration
96/// @{
97
98 /// Get an iterator that from the beginning of the symbol table.
99 inline iterator begin() { return vmap.begin(); }
100
101 /// Get a const_iterator that from the beginning of the symbol table.
102 inline const_iterator begin() const { return vmap.begin(); }
103
104 /// Get an iterator to the end of the symbol table.
105 inline iterator end() { return vmap.end(); }
106
107 /// Get a const_iterator to the end of the symbol table.
108 inline const_iterator end() const { return vmap.end(); }
109
110 /// @}
111 /// @name Mutators
112 /// @{
113private:
114 ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
115
116 /// This method adds the provided value \p N to the symbol table. The Value
117 /// must have a name which is used to place the value in the symbol table.
118 /// If the inserted name conflicts, this renames the value.
119 /// Add a named value to the symbol table
120 void reinsertValue(Value *V);
121
122 /// createValueName - This method attempts to create a value name and insert
123 /// it into the symbol table with the specified name. If it conflicts, it
124 /// auto-renames the name and returns that instead.
125 ValueName *createValueName(StringRef Name, Value *V);
126
127 /// This method removes a value from the symbol table. It leaves the
128 /// ValueName attached to the value, but it is no longer inserted in the
129 /// symtab.
130 void removeValueName(ValueName *V);
131
132 /// @}
133 /// @name Internal Data
134 /// @{
135
136 ValueMap vmap; ///< The map that holds the symbol table.
137 int MaxNameSize; ///< The maximum size for each name. If the limit is
138 ///< exceeded, the name is capped.
139 mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
140
141/// @}
142};
143
144} // end namespace llvm
145
146#endif // LLVM_IR_VALUESYMBOLTABLE_H
This file defines the StringMap class.
basic Basic Alias true
std::string Name
This class represents an incoming formal argument to a Function.
Definition: Argument.h:28
LLVM Basic Block Representation.
Definition: BasicBlock.h:60
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
unsigned size() const
Definition: StringMap.h:104
bool empty() const
Definition: StringMap.h:103
StringMapConstIterator< Value * > const_iterator
Definition: StringMap.h:217
iterator end()
Definition: StringMap.h:221
iterator begin()
Definition: StringMap.h:220
StringMapIterator< Value * > iterator
Definition: StringMap.h:218
ValueTy lookup(StringRef Key) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
Definition: StringMap.h:254
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
See the file comment.
Definition: ValueMap.h:84
This class provides a symbol table of name/value pairs.
ValueSymbolTable(int MaxNameSize=-1)
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
const_iterator begin() const
Get a const_iterator that from the beginning of the symbol table.
Value * lookup(StringRef Name) const
This method finds the value with the given Name in the the symbol table.
const_iterator end() const
Get a const_iterator to the end of the symbol table.
iterator end()
Get an iterator to the end of the symbol table.
unsigned size() const
The number of name/type pairs is returned.
iterator begin()
Get an iterator that from the beginning of the symbol table.
LLVM Value Representation.
Definition: Value.h:74
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Option to add extra bits to the ilist_iterator.