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" |
Daniel Dunbar | b99eac8 | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
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... |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 26 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
Daniel Dunbar | b99eac8 | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 27 | errs() << "Value still in symbol table! Type = '" |
| 28 | << VI->getValue()->getType()->getDescription() << "' Name = '" |
| 29 | << VI->getKeyData() << "'\n"; |
Chris Lattner | 86fc081 | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 30 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 31 | #endif |
| 32 | } |
| 33 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 34 | // Insert a value into the symbol table with the specified name... |
| 35 | // |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 36 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 37 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 38 | |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 39 | // Try inserting the name, assuming it won't conflict. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 40 | if (vmap.insert(V->Name)) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 41 | //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n"; |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 42 | return; |
| 43 | } |
| 44 | |
| 45 | // Otherwise, there is a naming conflict. Rename this value. |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame^] | 46 | SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 47 | |
| 48 | // 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] | 49 | V->Name->Destroy(); |
| 50 | |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 51 | unsigned BaseSize = UniqueName.size(); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 52 | while (1) { |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame^] | 53 | // Trim any suffix off and append the next number. |
Chris Lattner | 71a60ba | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 54 | UniqueName.resize(BaseSize); |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame^] | 55 | raw_svector_ostream(UniqueName) << ++LastUnique; |
| 56 | |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 57 | // Try insert the vmap entry with this suffix. |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 58 | ValueName &NewName = |
Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 59 | vmap.GetOrCreateValue(StringRef(UniqueName.data(), |
| 60 | UniqueName.size())); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 61 | if (NewName.getValue() == 0) { |
| 62 | // Newly inserted name. Success! |
| 63 | NewName.setValue(V); |
| 64 | V->Name = &NewName; |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 65 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 66 | return; |
| 67 | } |
| 68 | } |
| 69 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 70 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 71 | void ValueSymbolTable::removeValueName(ValueName *V) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 72 | //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 73 | // Remove the value from the symbol table. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 74 | vmap.remove(V); |
| 75 | } |
| 76 | |
| 77 | /// createValueName - This method attempts to create a value name and insert |
| 78 | /// it into the symbol table with the specified name. If it conflicts, it |
| 79 | /// auto-renames the name and returns that instead. |
Daniel Dunbar | 84b5f6e | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 80 | ValueName *ValueSymbolTable::createValueName(const StringRef &Name, Value *V) { |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 81 | // In the common case, the name is not already in the symbol table. |
Daniel Dunbar | 84b5f6e | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 82 | ValueName &Entry = vmap.GetOrCreateValue(Name); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 83 | if (Entry.getValue() == 0) { |
| 84 | Entry.setValue(V); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 85 | //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": " |
| 86 | // << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 87 | return &Entry; |
| 88 | } |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 89 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 90 | // Otherwise, there is a naming conflict. Rename this value. |
Daniel Dunbar | 84b5f6e | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 91 | SmallString<128> UniqueName(Name.begin(), Name.end()); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 92 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 93 | while (1) { |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame^] | 94 | // Trim any suffix off and append the next number. |
Daniel Dunbar | 84b5f6e | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 95 | UniqueName.resize(Name.size()); |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame^] | 96 | raw_svector_ostream(UniqueName) << ++LastUnique; |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 97 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 98 | // Try insert the vmap entry with this suffix. |
Jay Foad | 7d0479f | 2009-05-21 09:52:38 +0000 | [diff] [blame] | 99 | ValueName &NewName = |
Daniel Dunbar | 5bf72e2 | 2009-07-23 18:17:34 +0000 | [diff] [blame] | 100 | vmap.GetOrCreateValue(StringRef(UniqueName.data(), |
| 101 | UniqueName.size())); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 102 | if (NewName.getValue() == 0) { |
| 103 | // Newly inserted name. Success! |
| 104 | NewName.setValue(V); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 105 | //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 106 | return &NewName; |
| 107 | } |
| 108 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 111 | |
| 112 | // dump - print out the symbol table |
| 113 | // |
| 114 | void ValueSymbolTable::dump() const { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 115 | //DOUT << "ValueSymbolTable:\n"; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 116 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 117 | //DOUT << " '" << I->getKeyData() << "' = "; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 118 | I->getValue()->dump(); |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 119 | //DOUT << "\n"; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 120 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 121 | } |