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->Name)) {
42 //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *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.
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 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
60 if (!NewName.getValue()) {
61 // Newly inserted name. Success!
64 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
70 void ValueSymbolTable::removeValueName(ValueName *V) {
71 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
72 // Remove the value from the symbol table.
76 /// createValueName - This method attempts to create a value name and insert
77 /// it into the symbol table with the specified name. If it conflicts, it
78 /// auto-renames the name and returns that instead.
79 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
80 // In the common case, the name is not already in the symbol table.
81 ValueName &Entry = vmap.GetOrCreateValue(Name);
82 if (!Entry.getValue()) {
84 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
89 // Otherwise, there is a naming conflict. Rename this value.
90 SmallString<256> UniqueName(Name.begin(), Name.end());
93 // Trim any suffix off and append the next number.
94 UniqueName.resize(Name.size());
95 raw_svector_ostream(UniqueName) << ++LastUnique;
97 // Try insert the vmap entry with this suffix.
98 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
99 if (!NewName.getValue()) {
100 // Newly inserted name. Success!
102 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
109 // dump - print out the symbol table
111 void ValueSymbolTable::dump() const {
112 //DEBUG(dbgs() << "ValueSymbolTable:\n");
113 for (const_iterator I = begin(), E = end(); I != E; ++I) {
114 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
115 I->getValue()->dump();
116 //DEBUG(dbgs() << "\n");