1321369Sdim//===- llvm/ValueSymbolTable.h - Implement a Value Symtab -------*- C++ -*-===// 2249259Sdim// 3353358Sdim// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4353358Sdim// See https://llvm.org/LICENSE.txt for license information. 5353358Sdim// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6249259Sdim// 7249259Sdim//===----------------------------------------------------------------------===// 8249259Sdim// 9249259Sdim// This file implements the name/Value symbol table for LLVM. 10249259Sdim// 11249259Sdim//===----------------------------------------------------------------------===// 12249259Sdim 13249259Sdim#ifndef LLVM_IR_VALUESYMBOLTABLE_H 14249259Sdim#define LLVM_IR_VALUESYMBOLTABLE_H 15249259Sdim 16249259Sdim#include "llvm/ADT/StringMap.h" 17321369Sdim#include "llvm/ADT/StringRef.h" 18249259Sdim#include "llvm/IR/Value.h" 19321369Sdim#include <cstdint> 20249259Sdim 21249259Sdimnamespace llvm { 22249259Sdim 23321369Sdimclass Argument; 24321369Sdimclass BasicBlock; 25321369Sdimclass Function; 26321369Sdimclass GlobalAlias; 27321369Sdimclass GlobalIFunc; 28321369Sdimclass GlobalVariable; 29321369Sdimclass Instruction; 30321369Sdimtemplate <unsigned InternalLen> class SmallString; 31321369Sdimtemplate <typename ValueSubClass> class SymbolTableListTraits; 32321369Sdim 33249259Sdim/// This class provides a symbol table of name/value pairs. It is essentially 34249259Sdim/// a std::map<std::string,Value*> but has a controlled interface provided by 35249259Sdim/// LLVM as well as ensuring uniqueness of names. 36249259Sdim/// 37249259Sdimclass ValueSymbolTable { 38296417Sdim friend class SymbolTableListTraits<Argument>; 39296417Sdim friend class SymbolTableListTraits<BasicBlock>; 40296417Sdim friend class SymbolTableListTraits<Function>; 41296417Sdim friend class SymbolTableListTraits<GlobalAlias>; 42309124Sdim friend class SymbolTableListTraits<GlobalIFunc>; 43321369Sdim friend class SymbolTableListTraits<GlobalVariable>; 44321369Sdim friend class SymbolTableListTraits<Instruction>; 45321369Sdim friend class Value; 46321369Sdim 47249259Sdim/// @name Types 48249259Sdim/// @{ 49249259Sdimpublic: 50341825Sdim /// A mapping of names to values. 51321369Sdim using ValueMap = StringMap<Value*>; 52249259Sdim 53341825Sdim /// An iterator over a ValueMap. 54321369Sdim using iterator = ValueMap::iterator; 55249259Sdim 56341825Sdim /// A const_iterator over a ValueMap. 57321369Sdim using const_iterator = ValueMap::const_iterator; 58249259Sdim 59249259Sdim/// @} 60249259Sdim/// @name Constructors 61249259Sdim/// @{ 62321369Sdim 63321369Sdim ValueSymbolTable() : vmap(0) {} 64249259Sdim ~ValueSymbolTable(); 65249259Sdim 66249259Sdim/// @} 67249259Sdim/// @name Accessors 68249259Sdim/// @{ 69321369Sdim 70249259Sdim /// This method finds the value with the given \p Name in the 71296417Sdim /// the symbol table. 72249259Sdim /// @returns the value associated with the \p Name 73341825Sdim /// Lookup a named Value. 74249259Sdim Value *lookup(StringRef Name) const { return vmap.lookup(Name); } 75249259Sdim 76249259Sdim /// @returns true iff the symbol table is empty 77341825Sdim /// Determine if the symbol table is empty 78249259Sdim inline bool empty() const { return vmap.empty(); } 79249259Sdim 80341825Sdim /// The number of name/type pairs is returned. 81249259Sdim inline unsigned size() const { return unsigned(vmap.size()); } 82249259Sdim 83249259Sdim /// This function can be used from the debugger to display the 84249259Sdim /// content of the symbol table while debugging. 85341825Sdim /// Print out symbol table on stderr 86249259Sdim void dump() const; 87249259Sdim 88249259Sdim/// @} 89249259Sdim/// @name Iteration 90249259Sdim/// @{ 91321369Sdim 92341825Sdim /// Get an iterator that from the beginning of the symbol table. 93249259Sdim inline iterator begin() { return vmap.begin(); } 94249259Sdim 95341825Sdim /// Get a const_iterator that from the beginning of the symbol table. 96249259Sdim inline const_iterator begin() const { return vmap.begin(); } 97249259Sdim 98341825Sdim /// Get an iterator to the end of the symbol table. 99249259Sdim inline iterator end() { return vmap.end(); } 100249259Sdim 101341825Sdim /// Get a const_iterator to the end of the symbol table. 102249259Sdim inline const_iterator end() const { return vmap.end(); } 103296417Sdim 104296417Sdim /// @} 105296417Sdim /// @name Mutators 106296417Sdim /// @{ 107249259Sdimprivate: 108296417Sdim ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName); 109296417Sdim 110249259Sdim /// This method adds the provided value \p N to the symbol table. The Value 111296417Sdim /// must have a name which is used to place the value in the symbol table. 112249259Sdim /// If the inserted name conflicts, this renames the value. 113341825Sdim /// Add a named value to the symbol table 114249259Sdim void reinsertValue(Value *V); 115296417Sdim 116249259Sdim /// createValueName - This method attempts to create a value name and insert 117249259Sdim /// it into the symbol table with the specified name. If it conflicts, it 118249259Sdim /// auto-renames the name and returns that instead. 119249259Sdim ValueName *createValueName(StringRef Name, Value *V); 120296417Sdim 121249259Sdim /// This method removes a value from the symbol table. It leaves the 122249259Sdim /// ValueName attached to the value, but it is no longer inserted in the 123249259Sdim /// symtab. 124249259Sdim void removeValueName(ValueName *V); 125296417Sdim 126296417Sdim /// @} 127296417Sdim /// @name Internal Data 128296417Sdim /// @{ 129321369Sdim 130249259Sdim ValueMap vmap; ///< The map that holds the symbol table. 131321369Sdim mutable uint32_t LastUnique = 0; ///< Counter for tracking unique names 132249259Sdim 133249259Sdim/// @} 134249259Sdim}; 135249259Sdim 136321369Sdim} // end namespace llvm 137249259Sdim 138321369Sdim#endif // LLVM_IR_VALUESYMBOLTABLE_H 139