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 | // |
Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the ValueSymbolTable class for the IR library. |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Reid Spencer | ef9b9a7 | 2007-02-05 20:47:22 +0000 | [diff] [blame] | 14 | #define DEBUG_TYPE "valuesymtab" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/ValueSymbolTable.h" |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/GlobalValue.h" |
| 18 | #include "llvm/IR/Type.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b95c2fd | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Reid Spencer | eb7116b | 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 | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 26 | for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI) |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 27 | dbgs() << "Value still in symbol table! Type = '" |
Chris Lattner | 0cd0d88 | 2011-06-18 21:18:23 +0000 | [diff] [blame] | 28 | << *VI->getValue()->getType() << "' Name = '" |
Daniel Dunbar | b95c2fd | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 29 | << VI->getKeyData() << "'\n"; |
Chris Lattner | 92f5b6a | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 30 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 31 | #endif |
| 32 | } |
| 33 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 34 | // Insert a value into the symbol table with the specified name... |
| 35 | // |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 36 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 37 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 38 | |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 39 | // Try inserting the name, assuming it won't conflict. |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 40 | if (vmap.insert(V->Name)) { |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 41 | //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n"); |
Chris Lattner | f511155 | 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 | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 46 | SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); |
Chris Lattner | 9738846 | 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 | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 49 | V->Name->Destroy(); |
| 50 | |
Chris Lattner | 421d3da | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 51 | unsigned BaseSize = UniqueName.size(); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 52 | while (1) { |
Daniel Dunbar | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 53 | // Trim any suffix off and append the next number. |
Chris Lattner | 421d3da | 2007-02-07 05:52:51 +0000 | [diff] [blame] | 54 | UniqueName.resize(BaseSize); |
Daniel Dunbar | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 55 | raw_svector_ostream(UniqueName) << ++LastUnique; |
| 56 | |
Chris Lattner | b4d8d62 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 57 | // Try insert the vmap entry with this suffix. |
Benjamin Kramer | 4ef6822 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 58 | ValueName &NewName = vmap.GetOrCreateValue(UniqueName); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 59 | if (NewName.getValue() == 0) { |
| 60 | // Newly inserted name. Success! |
| 61 | NewName.setValue(V); |
| 62 | V->Name = &NewName; |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 63 | //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 64 | return; |
| 65 | } |
| 66 | } |
| 67 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 68 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 69 | void ValueSymbolTable::removeValueName(ValueName *V) { |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 70 | //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 71 | // Remove the value from the symbol table. |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 78 | ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 79 | // In the common case, the name is not already in the symbol table. |
Daniel Dunbar | bc8d813 | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 80 | ValueName &Entry = vmap.GetOrCreateValue(Name); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 81 | if (Entry.getValue() == 0) { |
| 82 | Entry.setValue(V); |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 83 | //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 84 | // << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 85 | return &Entry; |
| 86 | } |
Chris Lattner | b4d8d62 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 87 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 88 | // Otherwise, there is a naming conflict. Rename this value. |
Benjamin Kramer | 4ef6822 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 89 | SmallString<256> UniqueName(Name.begin(), Name.end()); |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 90 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 91 | while (1) { |
Daniel Dunbar | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 92 | // Trim any suffix off and append the next number. |
Daniel Dunbar | bc8d813 | 2009-07-23 18:52:12 +0000 | [diff] [blame] | 93 | UniqueName.resize(Name.size()); |
Daniel Dunbar | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 94 | raw_svector_ostream(UniqueName) << ++LastUnique; |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 95 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 96 | // Try insert the vmap entry with this suffix. |
Benjamin Kramer | 4ef6822 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 97 | ValueName &NewName = vmap.GetOrCreateValue(UniqueName); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 98 | if (NewName.getValue() == 0) { |
| 99 | // Newly inserted name. Success! |
| 100 | NewName.setValue(V); |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 101 | //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 102 | return &NewName; |
| 103 | } |
| 104 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 107 | |
| 108 | // dump - print out the symbol table |
| 109 | // |
| 110 | void ValueSymbolTable::dump() const { |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 111 | //DEBUG(dbgs() << "ValueSymbolTable:\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 112 | for (const_iterator I = begin(), E = end(); I != E; ++I) { |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 113 | //DEBUG(dbgs() << " '" << I->getKeyData() << "' = "); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 114 | I->getValue()->dump(); |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 115 | //DEBUG(dbgs() << "\n"); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 116 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 117 | } |