LLVM API Documentation

ValueSymbolTable.cpp

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