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->getValueName())) {
     42     //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *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->getValueName()->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     auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
     60     if (IterBool.second) {
     61       // Newly inserted name.  Success!
     62       V->setValueName(&*IterBool.first);
     63      //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
     64       return;
     65     }
     66   }
     67 }
     68 
     69 void ValueSymbolTable::removeValueName(ValueName *V) {
     70   //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
     71   // Remove the value from the symbol table.
     72   vmap.remove(V);
     73 }
     74 
     75 /// createValueName - This method attempts to create a value name and insert
     76 /// it into the symbol table with the specified name.  If it conflicts, it
     77 /// auto-renames the name and returns that instead.
     78 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
     79   // In the common case, the name is not already in the symbol table.
     80   auto IterBool = vmap.insert(std::make_pair(Name, V));
     81   if (IterBool.second) {
     82     //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
     83     //           << *V << "\n");
     84     return &*IterBool.first;
     85   }
     86 
     87   // Otherwise, there is a naming conflict.  Rename this value.
     88   SmallString<256> UniqueName(Name.begin(), Name.end());
     89 
     90   while (1) {
     91     // Trim any suffix off and append the next number.
     92     UniqueName.resize(Name.size());
     93     raw_svector_ostream(UniqueName) << ++LastUnique;
     94 
     95     // Try insert the vmap entry with this suffix.
     96     auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
     97     if (IterBool.second) {
     98       // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V <<
     99       //       "\n");
    100       return &*IterBool.first;
    101     }
    102   }
    103 }
    104 
    105 
    106 // dump - print out the symbol table
    107 //
    108 void ValueSymbolTable::dump() const {
    109   //DEBUG(dbgs() << "ValueSymbolTable:\n");
    110   for (const_iterator I = begin(), E = end(); I != E; ++I) {
    111     //DEBUG(dbgs() << "  '" << I->getKeyData() << "' = ");
    112     I->getValue()->dump();
    113     //DEBUG(dbgs() << "\n");
    114   }
    115 }
    116