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 // Insert a value into the symbol table with the specified name...
37 void ValueSymbolTable::reinsertValue(Value* V) {
38 assert(V->hasName() && "Can't insert nameless Value into symbol table");
40 // Try inserting the name, assuming it won't conflict.
41 if (vmap.insert(V->getValueName())) {
42 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
46 // Otherwise, there is a naming conflict. Rename this value.
47 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
49 // The name is too already used, just free it so we can allocate a new name.
50 V->getValueName()->Destroy();
52 unsigned BaseSize = UniqueName.size();
54 // Trim any suffix off and append the next number.
55 UniqueName.resize(BaseSize);
56 raw_svector_ostream(UniqueName) << "." << ++LastUnique;
58 // Try insert the vmap entry with this suffix.
59 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
60 if (IterBool.second) {
61 // Newly inserted name. Success!
62 V->setValueName(&*IterBool.first);
63 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
69 void ValueSymbolTable::removeValueName(ValueName *V) {
70 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
71 // Remove the value from the symbol table.
75 /// createValueName - This method attempts to create a value name and insert
76 /// it into the symbol table with the specified name. If it conflicts, it
77 /// auto-renames the name and returns that instead.
78 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
79 // In the common case, the name is not already in the symbol table.
80 auto IterBool = vmap.insert(std::make_pair(Name, V));
81 if (IterBool.second) {
82 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
84 return &*IterBool.first;
87 // Otherwise, there is a naming conflict. Rename this value.
88 SmallString<256> UniqueName(Name.begin(), Name.end());
91 // Trim any suffix off and append the next number.
92 UniqueName.resize(Name.size());
93 raw_svector_ostream(UniqueName) << ++LastUnique;
95 // Try insert the vmap entry with this suffix.
96 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
97 if (IterBool.second) {
98 // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V <<
100 return &*IterBool.first;
106 // dump - print out the symbol table
108 void ValueSymbolTable::dump() const {
109 //DEBUG(dbgs() << "ValueSymbolTable:\n");
110 for (const_iterator I = begin(), E = end(); I != E; ++I) {
111 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
112 I->getValue()->dump();
113 //DEBUG(dbgs() << "\n");