Reid Spencer | eb7116b | 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 | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame^] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | eb7116b | 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 | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "valuesymtab" |
Reid Spencer | eb7116b | 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 | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 22 | // Class destructor |
| 23 | ValueSymbolTable::~ValueSymbolTable() { |
| 24 | #ifndef NDEBUG // Only do this in -g mode... |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 25 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 26 | cerr << "Value still in symbol table! Type = '" |
| 27 | << VI->getValue()->getType()->getDescription() << "' Name = '" |
| 28 | << VI->getKeyData() << "'\n"; |
Chris Lattner | 92f5b6a | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 29 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 30 | #endif |
| 31 | } |
| 32 | |
| 33 | // getUniqueName - Given a base name, return a string that is either equal to |
| 34 | // it (or derived from it) that does not already occur in the symbol table for |
| 35 | // the specified type. |
| 36 | // |
| 37 | std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const { |
| 38 | std::string TryName = BaseName; |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 39 | |
| 40 | // See if the name exists |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 41 | while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end()) |
| 42 | // Loop until we find a free name in the symbol table. |
| 43 | TryName = BaseName + utostr(++LastUnique); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 44 | return TryName; |
| 45 | } |
| 46 | |
| 47 | |
| 48 | // lookup a value - Returns null on failure... |
| 49 | // |
| 50 | Value *ValueSymbolTable::lookup(const std::string &Name) const { |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 51 | const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 52 | if (VI != vmap.end()) // We found the symbol |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 53 | return VI->getValue(); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 54 | return 0; |
| 55 | } |
| 56 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 57 | // Insert a value into the symbol table with the specified name... |
| 58 | // |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 59 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 60 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 61 | |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 62 | // Try inserting the name, assuming it won't conflict. |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 63 | if (vmap.insert(V->Name)) { |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 64 | //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n"; |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 65 | return; |
| 66 | } |
| 67 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 68 | // FIXME: this could be much more efficient. |
| 69 | |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 70 | // Otherwise, there is a naming conflict. Rename this value. |
Chris Lattner | 421d3da | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 71 | std::string UniqueName = V->getName(); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 72 | |
| 73 | V->Name->Destroy(); |
| 74 | |
Chris Lattner | 421d3da | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 75 | unsigned BaseSize = UniqueName.size(); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 76 | while (1) { |
Chris Lattner | 421d3da | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 77 | // Trim any suffix off. |
| 78 | UniqueName.resize(BaseSize); |
| 79 | UniqueName += utostr(++LastUnique); |
Chris Lattner | b4d8d62 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 80 | // Try insert the vmap entry with this suffix. |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 81 | ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], |
| 82 | &UniqueName[UniqueName.size()]); |
| 83 | if (NewName.getValue() == 0) { |
| 84 | // Newly inserted name. Success! |
| 85 | NewName.setValue(V); |
| 86 | V->Name = &NewName; |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 87 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 88 | return; |
| 89 | } |
| 90 | } |
| 91 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 92 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 93 | void ValueSymbolTable::removeValueName(ValueName *V) { |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 94 | //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 95 | // Remove the value from the plane. |
| 96 | vmap.remove(V); |
| 97 | } |
| 98 | |
| 99 | /// createValueName - This method attempts to create a value name and insert |
| 100 | /// it into the symbol table with the specified name. If it conflicts, it |
| 101 | /// auto-renames the name and returns that instead. |
| 102 | ValueName *ValueSymbolTable::createValueName(const char *NameStart, |
| 103 | unsigned NameLen, Value *V) { |
| 104 | ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen); |
| 105 | if (Entry.getValue() == 0) { |
| 106 | Entry.setValue(V); |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 107 | //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": " |
| 108 | // << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 109 | return &Entry; |
| 110 | } |
Chris Lattner | b4d8d62 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 111 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 112 | // FIXME: this could be much more efficient. |
| 113 | |
| 114 | // Otherwise, there is a naming conflict. Rename this value. |
| 115 | std::string UniqueName(NameStart, NameStart+NameLen); |
| 116 | while (1) { |
| 117 | // Trim any suffix off. |
| 118 | UniqueName.resize(NameLen); |
| 119 | UniqueName += utostr(++LastUnique); |
| 120 | // Try insert the vmap entry with this suffix. |
| 121 | ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], |
| 122 | &UniqueName[UniqueName.size()]); |
| 123 | if (NewName.getValue() == 0) { |
| 124 | // Newly inserted name. Success! |
| 125 | NewName.setValue(V); |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 126 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 127 | return &NewName; |
| 128 | } |
| 129 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 132 | |
| 133 | // dump - print out the symbol table |
| 134 | // |
| 135 | void ValueSymbolTable::dump() const { |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 136 | //DOUT << "ValueSymbolTable:\n"; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 137 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 138 | //DOUT << " '" << I->getKeyData() << "' = "; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 139 | I->getValue()->dump(); |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 140 | //DOUT << "\n"; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 141 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 142 | } |