blob: 105ea73857afecfcc64c1a4dd53108519613c424 [file] [log] [blame]
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01001//===- llvm/ValueSymbolTable.h - Implement a Value Symtab -------*- C++ -*-===//
2//
Andrew Walbran16937d02019-10-22 13:54:20 +01003// 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
Andrew Scull5e1ddfa2018-08-14 10:06:54 +01006//
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 <unsigned InternalLen> class SmallString;
31template <typename ValueSubClass> class SymbolTableListTraits;
32
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///
37class 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
47/// @name Types
48/// @{
49public:
Andrew Scullcdfcccc2018-10-05 20:58:37 +010050 /// A mapping of names to values.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010051 using ValueMap = StringMap<Value*>;
52
Andrew Scullcdfcccc2018-10-05 20:58:37 +010053 /// An iterator over a ValueMap.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010054 using iterator = ValueMap::iterator;
55
Andrew Scullcdfcccc2018-10-05 20:58:37 +010056 /// A const_iterator over a ValueMap.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010057 using const_iterator = ValueMap::const_iterator;
58
59/// @}
60/// @name Constructors
61/// @{
62
63 ValueSymbolTable() : vmap(0) {}
64 ~ValueSymbolTable();
65
66/// @}
67/// @name Accessors
68/// @{
69
70 /// This method finds the value with the given \p Name in the
71 /// the symbol table.
72 /// @returns the value associated with the \p Name
Andrew Scullcdfcccc2018-10-05 20:58:37 +010073 /// Lookup a named Value.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010074 Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
75
76 /// @returns true iff the symbol table is empty
Andrew Scullcdfcccc2018-10-05 20:58:37 +010077 /// Determine if the symbol table is empty
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010078 inline bool empty() const { return vmap.empty(); }
79
Andrew Scullcdfcccc2018-10-05 20:58:37 +010080 /// The number of name/type pairs is returned.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010081 inline unsigned size() const { return unsigned(vmap.size()); }
82
83 /// This function can be used from the debugger to display the
84 /// content of the symbol table while debugging.
Andrew Scullcdfcccc2018-10-05 20:58:37 +010085 /// Print out symbol table on stderr
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010086 void dump() const;
87
88/// @}
89/// @name Iteration
90/// @{
91
Andrew Scullcdfcccc2018-10-05 20:58:37 +010092 /// Get an iterator that from the beginning of the symbol table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010093 inline iterator begin() { return vmap.begin(); }
94
Andrew Scullcdfcccc2018-10-05 20:58:37 +010095 /// Get a const_iterator that from the beginning of the symbol table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010096 inline const_iterator begin() const { return vmap.begin(); }
97
Andrew Scullcdfcccc2018-10-05 20:58:37 +010098 /// Get an iterator to the end of the symbol table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +010099 inline iterator end() { return vmap.end(); }
100
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100101 /// Get a const_iterator to the end of the symbol table.
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100102 inline const_iterator end() const { return vmap.end(); }
103
104 /// @}
105 /// @name Mutators
106 /// @{
107private:
108 ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
109
110 /// This method adds the provided value \p N to the symbol table. The Value
111 /// must have a name which is used to place the value in the symbol table.
112 /// If the inserted name conflicts, this renames the value.
Andrew Scullcdfcccc2018-10-05 20:58:37 +0100113 /// Add a named value to the symbol table
Andrew Scull5e1ddfa2018-08-14 10:06:54 +0100114 void reinsertValue(Value *V);
115
116 /// createValueName - This method attempts to create a value name and insert
117 /// it into the symbol table with the specified name. If it conflicts, it
118 /// auto-renames the name and returns that instead.
119 ValueName *createValueName(StringRef Name, Value *V);
120
121 /// This method removes a value from the symbol table. It leaves the
122 /// ValueName attached to the value, but it is no longer inserted in the
123 /// symtab.
124 void removeValueName(ValueName *V);
125
126 /// @}
127 /// @name Internal Data
128 /// @{
129
130 ValueMap vmap; ///< The map that holds the symbol table.
131 mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
132
133/// @}
134};
135
136} // end namespace llvm
137
138#endif // LLVM_IR_VALUESYMBOLTABLE_H