LLVM API Documentation
00001 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements the ValueSymbolTable class for the VMCore library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "valuesymtab" 00015 #include "llvm/GlobalValue.h" 00016 #include "llvm/Type.h" 00017 #include "llvm/ValueSymbolTable.h" 00018 #include "llvm/ADT/SmallString.h" 00019 #include "llvm/Support/Debug.h" 00020 using namespace llvm; 00021 00022 // Class destructor 00023 ValueSymbolTable::~ValueSymbolTable() { 00024 #ifndef NDEBUG // Only do this in -g mode... 00025 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) 00026 cerr << "Value still in symbol table! Type = '" 00027 << VI->getValue()->getType()->getDescription() << "' Name = '" 00028 << VI->getKeyData() << "'\n"; 00029 assert(vmap.empty() && "Values remain in symbol table!"); 00030 #endif 00031 } 00032 00033 // lookup a value - Returns null on failure... 00034 // 00035 Value *ValueSymbolTable::lookup(const std::string &Name) const { 00036 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]); 00037 if (VI != vmap.end()) // We found the symbol 00038 return VI->getValue(); 00039 return 0; 00040 } 00041 00042 Value *ValueSymbolTable::lookup(const char *NameBegin, 00043 const char *NameEnd) const { 00044 const_iterator VI = vmap.find(NameBegin, NameEnd); 00045 if (VI != vmap.end()) // We found the symbol 00046 return VI->getValue(); 00047 return 0; 00048 } 00049 00050 // Insert a value into the symbol table with the specified name... 00051 // 00052 void ValueSymbolTable::reinsertValue(Value* V) { 00053 assert(V->hasName() && "Can't insert nameless Value into symbol table"); 00054 00055 // Try inserting the name, assuming it won't conflict. 00056 if (vmap.insert(V->Name)) { 00057 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n"; 00058 return; 00059 } 00060 00061 // Otherwise, there is a naming conflict. Rename this value. 00062 SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd()); 00063 00064 // The name is too already used, just free it so we can allocate a new name. 00065 V->Name->Destroy(); 00066 00067 unsigned BaseSize = UniqueName.size(); 00068 while (1) { 00069 // Trim any suffix off. 00070 UniqueName.resize(BaseSize); 00071 UniqueName.append_uint_32(++LastUnique); 00072 // Try insert the vmap entry with this suffix. 00073 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], 00074 &UniqueName[UniqueName.size()]); 00075 if (NewName.getValue() == 0) { 00076 // Newly inserted name. Success! 00077 NewName.setValue(V); 00078 V->Name = &NewName; 00079 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); 00080 return; 00081 } 00082 } 00083 } 00084 00085 void ValueSymbolTable::removeValueName(ValueName *V) { 00086 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n"); 00087 // Remove the value from the symbol table. 00088 vmap.remove(V); 00089 } 00090 00091 /// createValueName - This method attempts to create a value name and insert 00092 /// it into the symbol table with the specified name. If it conflicts, it 00093 /// auto-renames the name and returns that instead. 00094 ValueName *ValueSymbolTable::createValueName(const char *NameStart, 00095 unsigned NameLen, Value *V) { 00096 // In the common case, the name is not already in the symbol table. 00097 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen); 00098 if (Entry.getValue() == 0) { 00099 Entry.setValue(V); 00100 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": " 00101 // << *V << "\n"); 00102 return &Entry; 00103 } 00104 00105 // Otherwise, there is a naming conflict. Rename this value. 00106 SmallString<128> UniqueName(NameStart, NameStart+NameLen); 00107 00108 while (1) { 00109 // Trim any suffix off. 00110 UniqueName.resize(NameLen); 00111 UniqueName.append_uint_32(++LastUnique); 00112 00113 // Try insert the vmap entry with this suffix. 00114 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], 00115 &UniqueName[UniqueName.size()]); 00116 if (NewName.getValue() == 0) { 00117 // Newly inserted name. Success! 00118 NewName.setValue(V); 00119 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); 00120 return &NewName; 00121 } 00122 } 00123 } 00124 00125 00126 // dump - print out the symbol table 00127 // 00128 void ValueSymbolTable::dump() const { 00129 //DOUT << "ValueSymbolTable:\n"; 00130 for (const_iterator I = begin(), E = end(); I != E; ++I) { 00131 //DOUT << " '" << I->getKeyData() << "' = "; 00132 I->getValue()->dump(); 00133 //DOUT << "\n"; 00134 } 00135 }