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 | f3ebc3f | 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 | 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" |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 18 | #include "llvm/ADT/SmallString.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 | using namespace llvm; |
| 21 | |
Reid Spencer | 25780d5 | 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 | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 25 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
Chris Lattner | b377a7c | 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 | 86fc081 | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 29 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 30 | #endif |
| 31 | } |
| 32 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 33 | // lookup a value - Returns null on failure... |
| 34 | // |
| 35 | Value *ValueSymbolTable::lookup(const std::string &Name) const { |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 36 | const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 37 | if (VI != vmap.end()) // We found the symbol |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 38 | return VI->getValue(); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Chris Lattner | e43649f | 2008-06-27 21:09:10 +0000 | [diff] [blame] | 42 | Value *ValueSymbolTable::lookup(const char *NameBegin, |
| 43 | const char *NameEnd) const { |
| 44 | const_iterator VI = vmap.find(NameBegin, NameEnd); |
| 45 | if (VI != vmap.end()) // We found the symbol |
| 46 | return VI->getValue(); |
| 47 | return 0; |
| 48 | } |
| 49 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 50 | // Insert a value into the symbol table with the specified name... |
| 51 | // |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 52 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 53 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 54 | |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 55 | // Try inserting the name, assuming it won't conflict. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 56 | if (vmap.insert(V->Name)) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 57 | //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n"; |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 58 | return; |
| 59 | } |
| 60 | |
| 61 | // Otherwise, there is a naming conflict. Rename this value. |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 62 | SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd()); |
| 63 | |
| 64 | // The name is too already used, just free it so we can allocate a new name. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 65 | V->Name->Destroy(); |
| 66 | |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 67 | unsigned BaseSize = UniqueName.size(); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 68 | while (1) { |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 69 | // Trim any suffix off. |
| 70 | UniqueName.resize(BaseSize); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 71 | UniqueName.append_uint_32(++LastUnique); |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 72 | // Try insert the vmap entry with this suffix. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 73 | ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], |
| 74 | &UniqueName[UniqueName.size()]); |
| 75 | if (NewName.getValue() == 0) { |
| 76 | // Newly inserted name. Success! |
| 77 | NewName.setValue(V); |
| 78 | V->Name = &NewName; |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 79 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | } |
| 83 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 84 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 85 | void ValueSymbolTable::removeValueName(ValueName *V) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 86 | //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 87 | // Remove the value from the symbol table. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 88 | vmap.remove(V); |
| 89 | } |
| 90 | |
| 91 | /// createValueName - This method attempts to create a value name and insert |
| 92 | /// it into the symbol table with the specified name. If it conflicts, it |
| 93 | /// auto-renames the name and returns that instead. |
| 94 | ValueName *ValueSymbolTable::createValueName(const char *NameStart, |
| 95 | unsigned NameLen, Value *V) { |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 96 | // In the common case, the name is not already in the symbol table. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 97 | ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen); |
| 98 | if (Entry.getValue() == 0) { |
| 99 | Entry.setValue(V); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 100 | //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": " |
| 101 | // << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 102 | return &Entry; |
| 103 | } |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 104 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 105 | // Otherwise, there is a naming conflict. Rename this value. |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 106 | SmallString<128> UniqueName(NameStart, NameStart+NameLen); |
| 107 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 108 | while (1) { |
| 109 | // Trim any suffix off. |
| 110 | UniqueName.resize(NameLen); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame^] | 111 | UniqueName.append_uint_32(++LastUnique); |
| 112 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 113 | // Try insert the vmap entry with this suffix. |
| 114 | ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0], |
| 115 | &UniqueName[UniqueName.size()]); |
| 116 | if (NewName.getValue() == 0) { |
| 117 | // Newly inserted name. Success! |
| 118 | NewName.setValue(V); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 119 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 120 | return &NewName; |
| 121 | } |
| 122 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 125 | |
| 126 | // dump - print out the symbol table |
| 127 | // |
| 128 | void ValueSymbolTable::dump() const { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 129 | //DOUT << "ValueSymbolTable:\n"; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 130 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 131 | //DOUT << " '" << I->getKeyData() << "' = "; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 132 | I->getValue()->dump(); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 133 | //DOUT << "\n"; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 134 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 135 | } |