Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:11

0001 //===- llvm/ValueSymbolTable.h - Implement a Value Symtab -------*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements the name/Value symbol table for LLVM.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_IR_VALUESYMBOLTABLE_H
0014 #define LLVM_IR_VALUESYMBOLTABLE_H
0015 
0016 #include "llvm/ADT/StringMap.h"
0017 #include "llvm/ADT/StringRef.h"
0018 #include "llvm/IR/Value.h"
0019 #include <cstdint>
0020 
0021 namespace llvm {
0022 
0023 class Argument;
0024 class BasicBlock;
0025 class Function;
0026 class GlobalAlias;
0027 class GlobalIFunc;
0028 class GlobalVariable;
0029 class Instruction;
0030 template <bool ExtraIteratorBits> struct ilist_iterator_bits;
0031 template <class ParentTy> struct ilist_parent;
0032 template <unsigned InternalLen> class SmallString;
0033 template <typename ValueSubClass, typename ... Args> class SymbolTableListTraits;
0034 
0035 /// This class provides a symbol table of name/value pairs. It is essentially
0036 /// a std::map<std::string,Value*> but has a controlled interface provided by
0037 /// LLVM as well as ensuring uniqueness of names.
0038 ///
0039 class ValueSymbolTable {
0040   friend class SymbolTableListTraits<Argument>;
0041   friend class SymbolTableListTraits<BasicBlock>;
0042   friend class SymbolTableListTraits<Function>;
0043   friend class SymbolTableListTraits<GlobalAlias>;
0044   friend class SymbolTableListTraits<GlobalIFunc>;
0045   friend class SymbolTableListTraits<GlobalVariable>;
0046   friend class SymbolTableListTraits<Instruction, ilist_iterator_bits<true>,
0047                                      ilist_parent<BasicBlock>>;
0048   friend class Value;
0049 
0050 /// @name Types
0051 /// @{
0052 public:
0053   /// A mapping of names to values.
0054   using ValueMap = StringMap<Value*>;
0055 
0056   /// An iterator over a ValueMap.
0057   using iterator = ValueMap::iterator;
0058 
0059   /// A const_iterator over a ValueMap.
0060   using const_iterator = ValueMap::const_iterator;
0061 
0062 /// @}
0063 /// @name Constructors
0064 /// @{
0065 
0066   ValueSymbolTable(int MaxNameSize = -1) : vmap(0), MaxNameSize(MaxNameSize) {}
0067   ~ValueSymbolTable();
0068 
0069   /// @}
0070   /// @name Accessors
0071   /// @{
0072 
0073   /// This method finds the value with the given \p Name in the
0074   /// the symbol table.
0075   /// @returns the value associated with the \p Name
0076   /// Lookup a named Value.
0077   Value *lookup(StringRef Name) const {
0078     if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
0079       Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
0080 
0081     return vmap.lookup(Name);
0082   }
0083 
0084   /// @returns true iff the symbol table is empty
0085   /// Determine if the symbol table is empty
0086   inline bool empty() const { return vmap.empty(); }
0087 
0088   /// The number of name/type pairs is returned.
0089   inline unsigned size() const { return unsigned(vmap.size()); }
0090 
0091   /// This function can be used from the debugger to display the
0092   /// content of the symbol table while debugging.
0093   /// Print out symbol table on stderr
0094   void dump() const;
0095 
0096 /// @}
0097 /// @name Iteration
0098 /// @{
0099 
0100   /// Get an iterator that from the beginning of the symbol table.
0101   inline iterator begin() { return vmap.begin(); }
0102 
0103   /// Get a const_iterator that from the beginning of the symbol table.
0104   inline const_iterator begin() const { return vmap.begin(); }
0105 
0106   /// Get an iterator to the end of the symbol table.
0107   inline iterator end() { return vmap.end(); }
0108 
0109   /// Get a const_iterator to the end of the symbol table.
0110   inline const_iterator end() const { return vmap.end(); }
0111 
0112   /// @}
0113   /// @name Mutators
0114   /// @{
0115 private:
0116   ValueName *makeUniqueName(Value *V, SmallString<256> &UniqueName);
0117 
0118   /// This method adds the provided value \p N to the symbol table.  The Value
0119   /// must have a name which is used to place the value in the symbol table.
0120   /// If the inserted name conflicts, this renames the value.
0121   /// Add a named value to the symbol table
0122   void reinsertValue(Value *V);
0123 
0124   /// createValueName - This method attempts to create a value name and insert
0125   /// it into the symbol table with the specified name.  If it conflicts, it
0126   /// auto-renames the name and returns that instead.
0127   ValueName *createValueName(StringRef Name, Value *V);
0128 
0129   /// This method removes a value from the symbol table.  It leaves the
0130   /// ValueName attached to the value, but it is no longer inserted in the
0131   /// symtab.
0132   void removeValueName(ValueName *V);
0133 
0134   /// @}
0135   /// @name Internal Data
0136   /// @{
0137 
0138   ValueMap vmap;                    ///< The map that holds the symbol table.
0139   int MaxNameSize; ///< The maximum size for each name. If the limit is
0140                    ///< exceeded, the name is capped.
0141   mutable uint32_t LastUnique = 0;  ///< Counter for tracking unique names
0142 
0143 /// @}
0144 };
0145 
0146 } // end namespace llvm
0147 
0148 #endif // LLVM_IR_VALUESYMBOLTABLE_H