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 IR library. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/IR/ValueSymbolTable.h" 00015 #include "llvm/ADT/SmallString.h" 00016 #include "llvm/IR/GlobalValue.h" 00017 #include "llvm/IR/Type.h" 00018 #include "llvm/Support/Debug.h" 00019 #include "llvm/Support/raw_ostream.h" 00020 using namespace llvm; 00021 00022 #define DEBUG_TYPE "valuesymtab" 00023 00024 // Class destructor 00025 ValueSymbolTable::~ValueSymbolTable() { 00026 #ifndef NDEBUG // Only do this in -g mode... 00027 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) 00028 dbgs() << "Value still in symbol table! Type = '" 00029 << *VI->getValue()->getType() << "' Name = '" 00030 << VI->getKeyData() << "'\n"; 00031 assert(vmap.empty() && "Values remain in symbol table!"); 00032 #endif 00033 } 00034 00035 // Insert a value into the symbol table with the specified name... 00036 // 00037 void ValueSymbolTable::reinsertValue(Value* V) { 00038 assert(V->hasName() && "Can't insert nameless Value into symbol table"); 00039 00040 // Try inserting the name, assuming it won't conflict. 00041 if (vmap.insert(V->Name)) { 00042 //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n"); 00043 return; 00044 } 00045 00046 // Otherwise, there is a naming conflict. Rename this value. 00047 SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); 00048 00049 // The name is too already used, just free it so we can allocate a new name. 00050 V->Name->Destroy(); 00051 00052 unsigned BaseSize = UniqueName.size(); 00053 while (1) { 00054 // Trim any suffix off and append the next number. 00055 UniqueName.resize(BaseSize); 00056 raw_svector_ostream(UniqueName) << ++LastUnique; 00057 00058 // Try insert the vmap entry with this suffix. 00059 ValueName &NewName = vmap.GetOrCreateValue(UniqueName); 00060 if (!NewName.getValue()) { 00061 // Newly inserted name. Success! 00062 NewName.setValue(V); 00063 V->Name = &NewName; 00064 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); 00065 return; 00066 } 00067 } 00068 } 00069 00070 void ValueSymbolTable::removeValueName(ValueName *V) { 00071 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n"); 00072 // Remove the value from the symbol table. 00073 vmap.remove(V); 00074 } 00075 00076 /// createValueName - This method attempts to create a value name and insert 00077 /// it into the symbol table with the specified name. If it conflicts, it 00078 /// auto-renames the name and returns that instead. 00079 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { 00080 // In the common case, the name is not already in the symbol table. 00081 ValueName &Entry = vmap.GetOrCreateValue(Name); 00082 if (!Entry.getValue()) { 00083 Entry.setValue(V); 00084 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " 00085 // << *V << "\n"); 00086 return &Entry; 00087 } 00088 00089 // Otherwise, there is a naming conflict. Rename this value. 00090 SmallString<256> UniqueName(Name.begin(), Name.end()); 00091 00092 while (1) { 00093 // Trim any suffix off and append the next number. 00094 UniqueName.resize(Name.size()); 00095 raw_svector_ostream(UniqueName) << ++LastUnique; 00096 00097 // Try insert the vmap entry with this suffix. 00098 ValueName &NewName = vmap.GetOrCreateValue(UniqueName); 00099 if (!NewName.getValue()) { 00100 // Newly inserted name. Success! 00101 NewName.setValue(V); 00102 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); 00103 return &NewName; 00104 } 00105 } 00106 } 00107 00108 00109 // dump - print out the symbol table 00110 // 00111 void ValueSymbolTable::dump() const { 00112 //DEBUG(dbgs() << "ValueSymbolTable:\n"); 00113 for (const_iterator I = begin(), E = end(); I != E; ++I) { 00114 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = "); 00115 I->getValue()->dump(); 00116 //DEBUG(dbgs() << "\n"); 00117 } 00118 }