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/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
22 #define DEBUG_TYPE "valuesymtab"
25 ValueSymbolTable::~ValueSymbolTable() {
26 #ifndef NDEBUG // Only do this in -g mode...
27 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
28 dbgs() << "Value still in symbol table! Type = '"
29 << *VI->getValue()->getType() << "' Name = '"
30 << VI->getKeyData() << "'\n";
31 assert(vmap.empty() && "Values remain in symbol table!");
35 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
36 SmallString<256> &UniqueName) {
37 unsigned BaseSize = UniqueName.size();
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))
46 // Try insert the vmap entry with this suffix.
47 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
49 return &*IterBool.first;
53 // Insert a value into the symbol table with the specified name...
55 void ValueSymbolTable::reinsertValue(Value* V) {
56 assert(V->hasName() && "Can't insert nameless Value into symbol table");
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");
64 // Otherwise, there is a naming conflict. Rename this value.
65 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
67 // The name is too already used, just free it so we can allocate a new name.
68 V->getValueName()->Destroy();
70 ValueName *VN = makeUniqueName(V, UniqueName);
74 void ValueSymbolTable::removeValueName(ValueName *V) {
75 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
76 // Remove the value from the symbol table.
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.
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() << ": "
89 return &*IterBool.first;
92 // Otherwise, there is a naming conflict. Rename this value.
93 SmallString<256> UniqueName(Name.begin(), Name.end());
94 return makeUniqueName(V, UniqueName);
98 // dump - print out the symbol table
100 void ValueSymbolTable::dump() const {
101 //DEBUG(dbgs() << "ValueSymbolTable:\n");
102 for (const_iterator I = begin(), E = end(); I != E; ++I) {
103 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
104 I->getValue()->dump();
105 //DEBUG(dbgs() << "\n");