1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueSymbolTable class for the IR library.
11 //
12 //===----------------------------------------------------------------------===//
13
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/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 #define DEBUG_TYPE "valuesymtab"
23
24 // Class destructor
~ValueSymbolTable()25 ValueSymbolTable::~ValueSymbolTable() {
26 #ifndef NDEBUG // Only do this in -g mode...
27 for (const auto &VI : vmap)
28 dbgs() << "Value still in symbol table! Type = '"
29 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
30 << "'\n";
31 assert(vmap.empty() && "Values remain in symbol table!");
32 #endif
33 }
34
makeUniqueName(Value * V,SmallString<256> & UniqueName)35 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
36 SmallString<256> &UniqueName) {
37 unsigned BaseSize = UniqueName.size();
38 while (1) {
39 // Trim any suffix off and append the next number.
40 UniqueName.resize(BaseSize);
41 raw_svector_ostream S(UniqueName);
42 if (isa<GlobalValue>(V))
43 S << ".";
44 S << ++LastUnique;
45
46 // Try insert the vmap entry with this suffix.
47 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
48 if (IterBool.second)
49 return &*IterBool.first;
50 }
51 }
52
53 // Insert a value into the symbol table with the specified name...
54 //
reinsertValue(Value * V)55 void ValueSymbolTable::reinsertValue(Value* V) {
56 assert(V->hasName() && "Can't insert nameless Value into symbol table");
57
58 // Try inserting the name, assuming it won't conflict.
59 if (vmap.insert(V->getValueName())) {
60 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
61 return;
62 }
63
64 // Otherwise, there is a naming conflict. Rename this value.
65 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
66
67 // The name is too already used, just free it so we can allocate a new name.
68 V->getValueName()->Destroy();
69
70 ValueName *VN = makeUniqueName(V, UniqueName);
71 V->setValueName(VN);
72 }
73
removeValueName(ValueName * V)74 void ValueSymbolTable::removeValueName(ValueName *V) {
75 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
76 // Remove the value from the symbol table.
77 vmap.remove(V);
78 }
79
80 /// createValueName - This method attempts to create a value name and insert
81 /// it into the symbol table with the specified name. If it conflicts, it
82 /// auto-renames the name and returns that instead.
createValueName(StringRef Name,Value * V)83 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
84 // In the common case, the name is not already in the symbol table.
85 auto IterBool = vmap.insert(std::make_pair(Name, V));
86 if (IterBool.second) {
87 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
88 // << *V << "\n");
89 return &*IterBool.first;
90 }
91
92 // Otherwise, there is a naming conflict. Rename this value.
93 SmallString<256> UniqueName(Name.begin(), Name.end());
94 return makeUniqueName(V, UniqueName);
95 }
96
97
98 // dump - print out the symbol table
99 //
dump() const100 LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
101 //DEBUG(dbgs() << "ValueSymbolTable:\n");
102 for (const auto &I : *this) {
103 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
104 I.getValue()->dump();
105 //DEBUG(dbgs() << "\n");
106 }
107 }
108