LLVM 24.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"
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///
41 friend class SymbolTableListTraits<Argument>;
43 friend class SymbolTableListTraits<Function>;
49 friend class Value;
50
51/// @name Types
52/// @{
53public:
54 /// A mapping of names to values.
56
57 /// An iterator over a ValueMap.
59
60 /// A const_iterator over a ValueMap.
62
63/// @}
64/// @name Constructors
65/// @{
66
67 ValueSymbolTable(int MaxNameSize = -1) : vmap(0), MaxNameSize(MaxNameSize) {}
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(0, std::max(1u, (unsigned)MaxNameSize));
81
82 return vmap.lookup(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 /// Return the value named \p Name, or give the name to the result of
93 /// \p CreateUnnamed, which must return an unnamed value.
94 template <typename CreateFn>
95 Value *lookupOrInsert(StringRef Name, CreateFn CreateUnnamed) {
96 assert(!Name.empty() && "Can't insert nameless Value into symbol table");
97 assert(MaxNameSize < 0 && "truncated names are not supported");
98
99 ValueName *Entry = &*vmap.try_emplace(Name, nullptr).first;
100 if (Value *V = Entry->getValue())
101 return V;
102
103 Value *V = CreateUnnamed();
104 insertUnnamed(Entry, V);
105 return V;
106 }
107
108 /// This function can be used from the debugger to display the
109 /// content of the symbol table while debugging.
110 /// Print out symbol table on stderr
111 LLVM_ABI void dump() const;
112
113 /// @}
114 /// @name Iteration
115 /// @{
116
117 /// Get an iterator that from the beginning of the symbol table.
118 inline iterator begin() { return vmap.begin(); }
119
120 /// Get a const_iterator that from the beginning of the symbol table.
121 inline const_iterator begin() const { return vmap.begin(); }
122
123 /// Get an iterator to the end of the symbol table.
124 inline iterator end() { return vmap.end(); }
125
126 /// Get a const_iterator to the end of the symbol table.
127 inline const_iterator end() const { return vmap.end(); }
128
129 /// @}
130 /// @name Mutators
131 /// @{
132private:
133 ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
134
135 LLVM_ABI void insertUnnamed(ValueName *Entry, Value *V);
136
137 /// This method adds the provided value \p N to the symbol table. The Value
138 /// must have a name which is used to place the value in the symbol table.
139 /// If the inserted name conflicts, this renames the value.
140 /// Add a named value to the symbol table
141 void reinsertValue(Value *V);
142
143 /// createValueName - This method attempts to create a value name and insert
144 /// it into the symbol table with the specified name. If it conflicts, it
145 /// auto-renames the name and returns that instead.
146 ValueName *createValueName(StringRef Name, Value *V);
147
148 /// This method removes a value from the symbol table. It leaves the
149 /// ValueName attached to the value, but it is no longer inserted in the
150 /// symtab.
151 void removeValueName(ValueName *V);
152
153 /// @}
154 /// @name Internal Data
155 /// @{
156
157 ValueMap vmap; ///< The map that holds the symbol table.
158 int MaxNameSize; ///< The maximum size for each name. If the limit is
159 ///< exceeded, the name is capped.
160 mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
161
162/// @}
163};
164
165} // end namespace llvm
166
167#endif // LLVM_IR_VALUESYMBOLTABLE_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
#define LLVM_ABI
Definition Compiler.h:211
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
LLVM Basic Block Representation.
Definition BasicBlock.h:62
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:28
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
StringMapIterBase< Value *, false > iterator
Definition StringMap.h:209
StringMapIterBase< Value *, true > const_iterator
Definition StringMap.h:208
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
See the file comment.
Definition ValueMap.h:84
ValueSymbolTable(int MaxNameSize=-1)
LLVM_ABI 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.
Value * lookupOrInsert(StringRef Name, CreateFn CreateUnnamed)
Return the value named Name, or give the name to the result of CreateUnnamed, which must return an un...
ValueMap::const_iterator const_iterator
A const_iterator over a ValueMap.
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.
StringMap< Value * > ValueMap
A mapping of names to values.
unsigned size() const
The number of name/type pairs is returned.
iterator begin()
Get an iterator that from the beginning of the symbol table.
ValueMap::iterator iterator
An iterator over a ValueMap.
LLVM Value Representation.
Definition Value.h:75
This is an optimization pass for GlobalISel generic memory operations.
StringMapEntry< Value * > ValueName
Definition Value.h:56
Option to add extra bits to the ilist_iterator.
Option to add a pointer to this list's owner in every node.