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 VMCore library.
12 //===----------------------------------------------------------------------===//
14 #define DEBUG_TYPE "valuesymtab"
15 #include "llvm/GlobalValue.h"
16 #include "llvm/Type.h"
17 #include "llvm/ValueSymbolTable.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/Debug.h"
23 ValueSymbolTable::~ValueSymbolTable() {
24 #ifndef NDEBUG // Only do this in -g mode...
25 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
26 cerr << "Value still in symbol table! Type = '"
27 << VI->getValue()->getType()->getDescription() << "' Name = '"
28 << VI->getKeyData() << "'\n";
29 assert(vmap.empty() && "Values remain in symbol table!");
33 // getUniqueName - Given a base name, return a string that is either equal to
34 // it (or derived from it) that does not already occur in the symbol table for
35 // the specified type.
37 std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
38 std::string TryName = BaseName;
40 // See if the name exists
41 while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end())
42 // Loop until we find a free name in the symbol table.
43 TryName = BaseName + utostr(++LastUnique);
48 // lookup a value - Returns null on failure...
50 Value *ValueSymbolTable::lookup(const std::string &Name) const {
51 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
52 if (VI != vmap.end()) // We found the symbol
53 return VI->getValue();
57 Value *ValueSymbolTable::lookup(const char *NameBegin,
58 const char *NameEnd) const {
59 const_iterator VI = vmap.find(NameBegin, NameEnd);
60 if (VI != vmap.end()) // We found the symbol
61 return VI->getValue();
65 // Insert a value into the symbol table with the specified name...
67 void ValueSymbolTable::reinsertValue(Value* V) {
68 assert(V->hasName() && "Can't insert nameless Value into symbol table");
70 // Try inserting the name, assuming it won't conflict.
71 if (vmap.insert(V->Name)) {
72 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
76 // FIXME: this could be much more efficient.
78 // Otherwise, there is a naming conflict. Rename this value.
79 std::string UniqueName = V->getName();
83 unsigned BaseSize = UniqueName.size();
85 // Trim any suffix off.
86 UniqueName.resize(BaseSize);
87 UniqueName += utostr(++LastUnique);
88 // Try insert the vmap entry with this suffix.
89 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
90 &UniqueName[UniqueName.size()]);
91 if (NewName.getValue() == 0) {
92 // Newly inserted name. Success!
95 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
101 void ValueSymbolTable::removeValueName(ValueName *V) {
102 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
103 // Remove the value from the plane.
107 /// createValueName - This method attempts to create a value name and insert
108 /// it into the symbol table with the specified name. If it conflicts, it
109 /// auto-renames the name and returns that instead.
110 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
111 unsigned NameLen, Value *V) {
112 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
113 if (Entry.getValue() == 0) {
115 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
120 // FIXME: this could be much more efficient.
122 // Otherwise, there is a naming conflict. Rename this value.
123 std::string UniqueName(NameStart, NameStart+NameLen);
125 // Trim any suffix off.
126 UniqueName.resize(NameLen);
127 UniqueName += utostr(++LastUnique);
128 // Try insert the vmap entry with this suffix.
129 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
130 &UniqueName[UniqueName.size()]);
131 if (NewName.getValue() == 0) {
132 // Newly inserted name. Success!
134 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
141 // dump - print out the symbol table
143 void ValueSymbolTable::dump() const {
144 //DOUT << "ValueSymbolTable:\n";
145 for (const_iterator I = begin(), E = end(); I != E; ++I) {
146 //DOUT << " '" << I->getKeyData() << "' = ";
147 I->getValue()->dump();