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 | // |
Chandler Carruth | ef860a2 | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 10 | // This file implements the ValueSymbolTable class for the IR library. |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 14 | #include "llvm/IR/ValueSymbolTable.h" |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 16 | #include "llvm/IR/GlobalValue.h" |
| 17 | #include "llvm/IR/Type.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b99eac8 | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 22 | #define DEBUG_TYPE "valuesymtab" |
| 23 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 24 | // Class destructor |
| 25 | ValueSymbolTable::~ValueSymbolTable() { |
| 26 | #ifndef NDEBUG // Only do this in -g mode... |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 27 | for (const auto &VI : vmap) |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 28 | dbgs() << "Value still in symbol table! Type = '" |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 29 | << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData() |
| 30 | << "'\n"; |
Chris Lattner | 86fc081 | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 31 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 32 | #endif |
| 33 | } |
| 34 | |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 35 | ValueName *ValueSymbolTable::makeUniqueName(Value *V, |
| 36 | SmallString<256> &UniqueName) { |
| 37 | unsigned BaseSize = UniqueName.size(); |
| 38 | while (1) { |
| 39 | // Trim any suffix off and append the next number. |
| 40 | UniqueName.resize(BaseSize); |
| 41 | raw_svector_ostream S(UniqueName); |
| 42 | if (isa<GlobalValue>(V)) |
| 43 | S << "."; |
| 44 | S << ++LastUnique; |
| 45 | |
| 46 | // Try insert the vmap entry with this suffix. |
| 47 | auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); |
| 48 | if (IterBool.second) |
| 49 | return &*IterBool.first; |
| 50 | } |
| 51 | } |
| 52 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 53 | // Insert a value into the symbol table with the specified name... |
| 54 | // |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 55 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 56 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 57 | |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 58 | // Try inserting the name, assuming it won't conflict. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 59 | if (vmap.insert(V->getValueName())) { |
| 60 | //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n"); |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Otherwise, there is a naming conflict. Rename this value. |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 65 | SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 66 | |
| 67 | // The name is too already used, just free it so we can allocate a new name. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 68 | V->getValueName()->Destroy(); |
| 69 | |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 70 | ValueName *VN = makeUniqueName(V, UniqueName); |
| 71 | V->setValueName(VN); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 72 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 74 | void ValueSymbolTable::removeValueName(ValueName *V) { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 75 | //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 76 | // Remove the value from the symbol table. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 77 | vmap.remove(V); |
| 78 | } |
| 79 | |
| 80 | /// createValueName - This method attempts to create a value name and insert |
| 81 | /// it into the symbol table with the specified name. If it conflicts, it |
| 82 | /// auto-renames the name and returns that instead. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 83 | ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 84 | // In the common case, the name is not already in the symbol table. |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 85 | auto IterBool = vmap.insert(std::make_pair(Name, V)); |
| 86 | if (IterBool.second) { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 87 | //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 88 | // << *V << "\n"); |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 89 | return &*IterBool.first; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 92 | // Otherwise, there is a naming conflict. Rename this value. |
Benjamin Kramer | feff705 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 93 | SmallString<256> UniqueName(Name.begin(), Name.end()); |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 94 | return makeUniqueName(V, UniqueName); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 97 | |
| 98 | // dump - print out the symbol table |
| 99 | // |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 100 | LLVM_DUMP_METHOD void ValueSymbolTable::dump() const { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 101 | //DEBUG(dbgs() << "ValueSymbolTable:\n"); |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 102 | for (const auto &I : *this) { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 103 | //DEBUG(dbgs() << " '" << I->getKeyData() << "' = "); |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 104 | I.getValue()->dump(); |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 105 | //DEBUG(dbgs() << "\n"); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 106 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 107 | } |