1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
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 ValueSymbolTable class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/ValueSymbolTable.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
27 #define DEBUG_TYPE "valuesymtab"
30 ValueSymbolTable::~ValueSymbolTable() {
31 #ifndef NDEBUG // Only do this in -g mode...
32 for (const auto &VI : vmap)
33 dbgs() << "Value still in symbol table! Type = '"
34 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
36 assert(vmap.empty() && "Values remain in symbol table!");
40 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
41 SmallString<256> &UniqueName) {
42 unsigned BaseSize = UniqueName.size();
44 // Trim any suffix off and append the next number.
45 UniqueName.resize(BaseSize);
46 raw_svector_ostream S(UniqueName);
47 if (isa<GlobalValue>(V))
51 // Try insert the vmap entry with this suffix.
52 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
54 return &*IterBool.first;
58 // Insert a value into the symbol table with the specified name...
60 void ValueSymbolTable::reinsertValue(Value* V) {
61 assert(V->hasName() && "Can't insert nameless Value into symbol table");
63 // Try inserting the name, assuming it won't conflict.
64 if (vmap.insert(V->getValueName())) {
65 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
69 // Otherwise, there is a naming conflict. Rename this value.
70 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
72 // The name is too already used, just free it so we can allocate a new name.
73 V->getValueName()->Destroy();
75 ValueName *VN = makeUniqueName(V, UniqueName);
79 void ValueSymbolTable::removeValueName(ValueName *V) {
80 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
81 // Remove the value from the symbol table.
85 /// createValueName - This method attempts to create a value name and insert
86 /// it into the symbol table with the specified name. If it conflicts, it
87 /// auto-renames the name and returns that instead.
88 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
89 // In the common case, the name is not already in the symbol table.
90 auto IterBool = vmap.insert(std::make_pair(Name, V));
91 if (IterBool.second) {
92 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
94 return &*IterBool.first;
97 // Otherwise, there is a naming conflict. Rename this value.
98 SmallString<256> UniqueName(Name.begin(), Name.end());
99 return makeUniqueName(V, UniqueName);
102 // dump - print out the symbol table
104 LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
105 //DEBUG(dbgs() << "ValueSymbolTable:\n");
106 for (const auto &I : *this) {
107 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
108 I.getValue()->dump();
109 //DEBUG(dbgs() << "\n");