1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group. It is distributed under
6 // the University of Illinois Open Source 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 // Insert a value into the symbol table with the specified name...
59 void ValueSymbolTable::reinsertValue(Value* V) {
60 assert(V->hasName() && "Can't insert nameless Value into symbol table");
62 // Try inserting the name, assuming it won't conflict.
63 if (vmap.insert(V->Name)) {
64 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
68 // FIXME: this could be much more efficient.
70 // Otherwise, there is a naming conflict. Rename this value.
71 std::string UniqueName = V->getName();
75 unsigned BaseSize = UniqueName.size();
77 // Trim any suffix off.
78 UniqueName.resize(BaseSize);
79 UniqueName += utostr(++LastUnique);
80 // Try insert the vmap entry with this suffix.
81 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
82 &UniqueName[UniqueName.size()]);
83 if (NewName.getValue() == 0) {
84 // Newly inserted name. Success!
87 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
93 void ValueSymbolTable::removeValueName(ValueName *V) {
94 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
95 // Remove the value from the plane.
99 /// createValueName - This method attempts to create a value name and insert
100 /// it into the symbol table with the specified name. If it conflicts, it
101 /// auto-renames the name and returns that instead.
102 ValueName *ValueSymbolTable::createValueName(const char *NameStart,
103 unsigned NameLen, Value *V) {
104 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
105 if (Entry.getValue() == 0) {
107 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
112 // FIXME: this could be much more efficient.
114 // Otherwise, there is a naming conflict. Rename this value.
115 std::string UniqueName(NameStart, NameStart+NameLen);
117 // Trim any suffix off.
118 UniqueName.resize(NameLen);
119 UniqueName += utostr(++LastUnique);
120 // Try insert the vmap entry with this suffix.
121 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
122 &UniqueName[UniqueName.size()]);
123 if (NewName.getValue() == 0) {
124 // Newly inserted name. Success!
126 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
133 // dump - print out the symbol table
135 void ValueSymbolTable::dump() const {
136 //DOUT << "ValueSymbolTable:\n";
137 for (const_iterator I = begin(), E = end(); I != E; ++I) {
138 //DOUT << " '" << I->getKeyData() << "' = ";
139 I->getValue()->dump();