1 //===- llvm/ValueSymbolTable.h - Implement a Value Symtab -------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the name/Value symbol table for LLVM.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IR_VALUESYMBOLTABLE_H
15 #define LLVM_IR_VALUESYMBOLTABLE_H
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/Value.h"
31 template <unsigned InternalLen> class SmallString;
32 template <typename ValueSubClass> class SymbolTableListTraits;
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.
38 class ValueSymbolTable {
39 friend class SymbolTableListTraits<Argument>;
40 friend class SymbolTableListTraits<BasicBlock>;
41 friend class SymbolTableListTraits<Function>;
42 friend class SymbolTableListTraits<GlobalAlias>;
43 friend class SymbolTableListTraits<GlobalIFunc>;
44 friend class SymbolTableListTraits<GlobalVariable>;
45 friend class SymbolTableListTraits<Instruction>;
51 /// A mapping of names to values.
52 using ValueMap = StringMap<Value*>;
54 /// An iterator over a ValueMap.
55 using iterator = ValueMap::iterator;
57 /// A const_iterator over a ValueMap.
58 using const_iterator = ValueMap::const_iterator;
61 /// @name Constructors
64 ValueSymbolTable() : vmap(0) {}
71 /// This method finds the value with the given \p Name in the
73 /// @returns the value associated with the \p Name
74 /// Lookup a named Value.
75 Value *lookup(StringRef Name) const { return vmap.lookup(Name); }
77 /// @returns true iff the symbol table is empty
78 /// Determine if the symbol table is empty
79 inline bool empty() const { return vmap.empty(); }
81 /// The number of name/type pairs is returned.
82 inline unsigned size() const { return unsigned(vmap.size()); }
84 /// This function can be used from the debugger to display the
85 /// content of the symbol table while debugging.
86 /// Print out symbol table on stderr
93 /// Get an iterator that from the beginning of the symbol table.
94 inline iterator begin() { return vmap.begin(); }
96 /// Get a const_iterator that from the beginning of the symbol table.
97 inline const_iterator begin() const { return vmap.begin(); }
99 /// Get an iterator to the end of the symbol table.
100 inline iterator end() { return vmap.end(); }
102 /// Get a const_iterator to the end of the symbol table.
103 inline const_iterator end() const { return vmap.end(); }
109 ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
111 /// This method adds the provided value \p N to the symbol table. The Value
112 /// must have a name which is used to place the value in the symbol table.
113 /// If the inserted name conflicts, this renames the value.
114 /// Add a named value to the symbol table
115 void reinsertValue(Value *V);
117 /// createValueName - This method attempts to create a value name and insert
118 /// it into the symbol table with the specified name. If it conflicts, it
119 /// auto-renames the name and returns that instead.
120 ValueName *createValueName(StringRef Name, Value *V);
122 /// This method removes a value from the symbol table. It leaves the
123 /// ValueName attached to the value, but it is no longer inserted in the
125 void removeValueName(ValueName *V);
128 /// @name Internal Data
131 ValueMap vmap; ///< The map that holds the symbol table.
132 mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names
137 } // end namespace llvm
139 #endif // LLVM_IR_VALUESYMBOLTABLE_H