Home | History | Annotate | Download | only in IR
      1 //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file implements the ValueSymbolTable class for the IR library.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/IR/ValueSymbolTable.h"
     15 #include "llvm/ADT/SmallString.h"
     16 #include "llvm/IR/GlobalValue.h"
     17 #include "llvm/IR/Type.h"
     18 #include "llvm/Support/Debug.h"
     19 #include "llvm/Support/raw_ostream.h"
     20 using namespace llvm;
     21 
     22 #define DEBUG_TYPE "valuesymtab"
     23 
     24 // Class destructor
     25 ValueSymbolTable::~ValueSymbolTable() {
     26 #ifndef NDEBUG   // Only do this in -g mode...
     27   for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
     28     dbgs() << "Value still in symbol table! Type = '"
     29            << *VI->getValue()->getType() << "' Name = '"
     30            << VI->getKeyData() << "'\n";
     31   assert(vmap.empty() && "Values remain in symbol table!");
     32 #endif
     33 }
     34 
     35 // Insert a value into the symbol table with the specified name...
     36 //
     37 void ValueSymbolTable::reinsertValue(Value* V) {
     38   assert(V->hasName() && "Can't insert nameless Value into symbol table");
     39 
     40   // Try inserting the name, assuming it won't conflict.
     41   if (vmap.insert(V->Name)) {
     42     //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n");
     43     return;
     44   }
     45 
     46   // Otherwise, there is a naming conflict.  Rename this value.
     47   SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
     48 
     49   // The name is too already used, just free it so we can allocate a new name.
     50   V->Name->Destroy();
     51 
     52   unsigned BaseSize = UniqueName.size();
     53   while (1) {
     54     // Trim any suffix off and append the next number.
     55     UniqueName.resize(BaseSize);
     56     raw_svector_ostream(UniqueName) << ++LastUnique;
     57 
     58     // Try insert the vmap entry with this suffix.
     59     ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
     60     if (!NewName.getValue()) {
     61       // Newly inserted name.  Success!
     62       NewName.setValue(V);
     63       V->Name = &NewName;
     64      //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
     65       return;
     66     }
     67   }
     68 }
     69 
     70 void ValueSymbolTable::removeValueName(ValueName *V) {
     71   //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
     72   // Remove the value from the symbol table.
     73   vmap.remove(V);
     74 }
     75 
     76 /// createValueName - This method attempts to create a value name and insert
     77 /// it into the symbol table with the specified name.  If it conflicts, it
     78 /// auto-renames the name and returns that instead.
     79 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
     80   // In the common case, the name is not already in the symbol table.
     81   ValueName &Entry = vmap.GetOrCreateValue(Name);
     82   if (!Entry.getValue()) {
     83     Entry.setValue(V);
     84     //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
     85     //           << *V << "\n");
     86     return &Entry;
     87   }
     88 
     89   // Otherwise, there is a naming conflict.  Rename this value.
     90   SmallString<256> UniqueName(Name.begin(), Name.end());
     91 
     92   while (1) {
     93     // Trim any suffix off and append the next number.
     94     UniqueName.resize(Name.size());
     95     raw_svector_ostream(UniqueName) << ++LastUnique;
     96 
     97     // Try insert the vmap entry with this suffix.
     98     ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
     99     if (!NewName.getValue()) {
    100       // Newly inserted name.  Success!
    101       NewName.setValue(V);
    102      //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
    103       return &NewName;
    104     }
    105   }
    106 }
    107 
    108 
    109 // dump - print out the symbol table
    110 //
    111 void ValueSymbolTable::dump() const {
    112   //DEBUG(dbgs() << "ValueSymbolTable:\n");
    113   for (const_iterator I = begin(), E = end(); I != E; ++I) {
    114     //DEBUG(dbgs() << "  '" << I->getKeyData() << "' = ");
    115     I->getValue()->dump();
    116     //DEBUG(dbgs() << "\n");
    117   }
    118 }
    119