Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 1 | //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 21f0c31 | 2006-01-11 05:39:45 +0000 | [diff] [blame] | 5 | // This file was developed by the LLVM research group. It is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ValueSymbolTable class for the VMCore library. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 14 | #define DEBUG_TYPE "valuesymtab" |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 15 | #include "llvm/GlobalValue.h" |
| 16 | #include "llvm/Type.h" |
| 17 | #include "llvm/ValueSymbolTable.h" |
| 18 | #include "llvm/ADT/StringExtras.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 23 | // Class destructor |
| 24 | ValueSymbolTable::~ValueSymbolTable() { |
| 25 | #ifndef NDEBUG // Only do this in -g mode... |
| 26 | bool LeftoverValues = true; |
| 27 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
| 28 | if (!isa<Constant>(VI->second) ) { |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 29 | DEBUG(DOUT << "Value still in symbol table! Type = '" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 30 | << VI->second->getType()->getDescription() << "' Name = '" |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 31 | << VI->first << "'\n"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 32 | LeftoverValues = false; |
| 33 | } |
| 34 | assert(LeftoverValues && "Values remain in symbol table!"); |
| 35 | #endif |
| 36 | } |
| 37 | |
| 38 | // getUniqueName - Given a base name, return a string that is either equal to |
| 39 | // it (or derived from it) that does not already occur in the symbol table for |
| 40 | // the specified type. |
| 41 | // |
| 42 | std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const { |
| 43 | std::string TryName = BaseName; |
| 44 | const_iterator End = vmap.end(); |
| 45 | |
| 46 | // See if the name exists |
| 47 | while (vmap.find(TryName) != End) // Loop until we find a free |
| 48 | TryName = BaseName + utostr(++LastUnique); // name in the symbol table |
| 49 | return TryName; |
| 50 | } |
| 51 | |
| 52 | |
| 53 | // lookup a value - Returns null on failure... |
| 54 | // |
| 55 | Value *ValueSymbolTable::lookup(const std::string &Name) const { |
| 56 | const_iterator VI = vmap.find(Name); |
| 57 | if (VI != vmap.end()) // We found the symbol |
| 58 | return const_cast<Value*>(VI->second); |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | // Strip the symbol table of its names. |
| 63 | // |
| 64 | bool ValueSymbolTable::strip() { |
| 65 | bool RemovedSymbol = false; |
| 66 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) { |
| 67 | Value *V = VI->second; |
| 68 | ++VI; |
| 69 | if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) { |
| 70 | // Set name to "", removing from symbol table! |
| 71 | V->setName(""); |
| 72 | RemovedSymbol = true; |
| 73 | } |
| 74 | } |
| 75 | return RemovedSymbol; |
| 76 | } |
| 77 | |
| 78 | // Insert a value into the symbol table with the specified name... |
| 79 | // |
| 80 | void ValueSymbolTable::insert(Value* V) { |
| 81 | assert(V && "Can't insert null Value into symbol table!"); |
| 82 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 83 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 84 | // Check to see if there is a naming conflict. If so, rename this value |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 85 | std::string UniqueName = getUniqueName(V->getName()); |
| 86 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 87 | DEBUG(DOUT << " Inserting value: " << UniqueName << ": " << *V << "\n"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 88 | |
| 89 | // Insert the vmap entry |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 90 | V->Name = UniqueName; |
| 91 | vmap.insert(make_pair(V->Name, V)); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | // Remove a value |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 95 | bool ValueSymbolTable::remove(Value *V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 96 | assert(V->hasName() && "Value doesn't have name!"); |
| 97 | iterator Entry = vmap.find(V->getName()); |
| 98 | if (Entry == vmap.end()) |
| 99 | return false; |
| 100 | |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 101 | DEBUG(DOUT << " Removing Value: " << Entry->second->getName() << "\n"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 102 | |
| 103 | // Remove the value from the plane... |
| 104 | vmap.erase(Entry); |
| 105 | return true; |
| 106 | } |
| 107 | |
| 108 | |
| 109 | // rename - Given a value with a non-empty name, remove its existing entry |
| 110 | // from the symbol table and insert a new one for Name. This is equivalent to |
| 111 | // doing "remove(V), V->Name = Name, insert(V)", |
| 112 | // |
| 113 | bool ValueSymbolTable::rename(Value *V, const std::string &name) { |
| 114 | assert(V && "Can't rename a null Value"); |
| 115 | assert(V->hasName() && "Can't rename a nameless Value"); |
| 116 | assert(!V->getName().empty() && "Can't rename an Value with null name"); |
| 117 | assert(V->getName() != name && "Can't rename a Value with same name"); |
| 118 | assert(!name.empty() && "Can't rename a named Value with a null name"); |
| 119 | |
| 120 | // Find the name |
| 121 | iterator VI = vmap.find(V->getName()); |
| 122 | |
| 123 | // If we didn't find it, we're done |
| 124 | if (VI == vmap.end()) |
| 125 | return false; |
| 126 | |
| 127 | // Remove the old entry. |
| 128 | vmap.erase(VI); |
| 129 | |
| 130 | // See if we can insert the new name. |
| 131 | VI = vmap.lower_bound(name); |
| 132 | |
| 133 | // Is there a naming conflict? |
| 134 | if (VI != vmap.end() && VI->first == name) { |
| 135 | V->Name = getUniqueName( name); |
| 136 | vmap.insert(make_pair(V->Name, V)); |
| 137 | } else { |
| 138 | V->Name = name; |
Reid Spencer | 3aaaa0b | 2007-02-05 20:47:22 +0000 | [diff] [blame^] | 139 | vmap.insert(VI, make_pair(V->Name, V)); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | return true; |
| 143 | } |
| 144 | |
| 145 | // DumpVal - a std::for_each function for dumping a value |
| 146 | // |
| 147 | static void DumpVal(const std::pair<const std::string, Value *> &V) { |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 148 | DOUT << " '" << V.first << "' = "; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 149 | V.second->dump(); |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 150 | DOUT << "\n"; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | // dump - print out the symbol table |
| 154 | // |
| 155 | void ValueSymbolTable::dump() const { |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 156 | DOUT << "ValueSymbolTable:\n"; |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 157 | for_each(vmap.begin(), vmap.end(), DumpVal); |
| 158 | } |