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/SmallString.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 // lookup a value - Returns null on failure...
35 Value
*ValueSymbolTable::lookup(const std::string
&Name
) const {
36 const_iterator VI
= vmap
.find(Name
.data(), Name
.data() + Name
.size());
37 if (VI
!= vmap
.end()) // We found the symbol
38 return VI
->getValue();
42 Value
*ValueSymbolTable::lookup(const char *NameBegin
,
43 const char *NameEnd
) const {
44 const_iterator VI
= vmap
.find(NameBegin
, NameEnd
);
45 if (VI
!= vmap
.end()) // We found the symbol
46 return VI
->getValue();
50 // Insert a value into the symbol table with the specified name...
52 void ValueSymbolTable::reinsertValue(Value
* V
) {
53 assert(V
->hasName() && "Can't insert nameless Value into symbol table");
55 // Try inserting the name, assuming it won't conflict.
56 if (vmap
.insert(V
->Name
)) {
57 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
61 // Otherwise, there is a naming conflict. Rename this value.
62 SmallString
<128> UniqueName(V
->getNameStart(), V
->getNameEnd());
64 // The name is too already used, just free it so we can allocate a new name.
67 unsigned BaseSize
= UniqueName
.size();
69 // Trim any suffix off.
70 UniqueName
.resize(BaseSize
);
71 UniqueName
.append_uint_32(++LastUnique
);
72 // Try insert the vmap entry with this suffix.
74 vmap
.GetOrCreateValue(UniqueName
.data(),
75 UniqueName
.data() + UniqueName
.size());
76 if (NewName
.getValue() == 0) {
77 // Newly inserted name. Success!
80 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
86 void ValueSymbolTable::removeValueName(ValueName
*V
) {
87 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
88 // Remove the value from the symbol table.
92 /// createValueName - This method attempts to create a value name and insert
93 /// it into the symbol table with the specified name. If it conflicts, it
94 /// auto-renames the name and returns that instead.
95 ValueName
*ValueSymbolTable::createValueName(const char *NameStart
,
96 unsigned NameLen
, Value
*V
) {
97 // In the common case, the name is not already in the symbol table.
98 ValueName
&Entry
= vmap
.GetOrCreateValue(NameStart
, NameStart
+NameLen
);
99 if (Entry
.getValue() == 0) {
101 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
106 // Otherwise, there is a naming conflict. Rename this value.
107 SmallString
<128> UniqueName(NameStart
, NameStart
+NameLen
);
110 // Trim any suffix off.
111 UniqueName
.resize(NameLen
);
112 UniqueName
.append_uint_32(++LastUnique
);
114 // Try insert the vmap entry with this suffix.
116 vmap
.GetOrCreateValue(UniqueName
.data(),
117 UniqueName
.data() + UniqueName
.size());
118 if (NewName
.getValue() == 0) {
119 // Newly inserted name. Success!
121 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
128 // dump - print out the symbol table
130 void ValueSymbolTable::dump() const {
131 //DOUT << "ValueSymbolTable:\n";
132 for (const_iterator I
= begin(), E
= end(); I
!= E
; ++I
) {
133 //DOUT << " '" << I->getKeyData() << "' = ";
134 I
->getValue()->dump();