Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1 | //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===// |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 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 | |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 14 | #include "llvm/IR/ValueSymbolTable.h" |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Jonas Hahnfeld | ba19c68 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
Nico Weber | 0f38c60 | 2018-04-30 14:59:11 +0000 | [diff] [blame] | 17 | #include "llvm/Config/llvm-config.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 18 | #include "llvm/IR/GlobalValue.h" |
Jonas Hahnfeld | ba19c68 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Module.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Type.h" |
Eugene Zelenko | b1df787 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 21 | #include "llvm/IR/Value.h" |
Eugene Zelenko | 9feaa97 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 22 | #include "llvm/Support/Casting.h" |
| 23 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 2e3def1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 24 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b95c2fd | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 9feaa97 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 26 | #include <cassert> |
| 27 | #include <utility> |
| 28 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
Chandler Carruth | 8677f2f | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 31 | #define DEBUG_TYPE "valuesymtab" |
| 32 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 33 | // Class destructor |
| 34 | ValueSymbolTable::~ValueSymbolTable() { |
| 35 | #ifndef NDEBUG // Only do this in -g mode... |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 36 | for (const auto &VI : vmap) |
David Greene | efc2581 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 37 | dbgs() << "Value still in symbol table! Type = '" |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 38 | << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData() |
| 39 | << "'\n"; |
Chris Lattner | 92f5b6a | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 40 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 41 | #endif |
| 42 | } |
| 43 | |
Rafael Espindola | a2197f8 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 44 | ValueName *ValueSymbolTable::makeUniqueName(Value *V, |
| 45 | SmallString<256> &UniqueName) { |
| 46 | unsigned BaseSize = UniqueName.size(); |
Eugene Zelenko | 9feaa97 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 47 | while (true) { |
Rafael Espindola | a2197f8 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 48 | // Trim any suffix off and append the next number. |
| 49 | UniqueName.resize(BaseSize); |
| 50 | raw_svector_ostream S(UniqueName); |
Jonas Hahnfeld | ba19c68 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 51 | if (auto *GV = dyn_cast<GlobalValue>(V)) { |
| 52 | // A dot is appended to mark it as clone during ABI demangling so that |
| 53 | // for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second |
| 54 | // one being a clone. |
| 55 | // On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for |
| 56 | // identifiers. This breaks ABI demangling but at least ptxas accepts and |
| 57 | // compiles the program. |
| 58 | const Module *M = GV->getParent(); |
| 59 | if (!(M && Triple(M->getTargetTriple()).isNVPTX())) |
| 60 | S << "."; |
| 61 | } |
Rafael Espindola | a2197f8 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 62 | S << ++LastUnique; |
| 63 | |
| 64 | // Try insert the vmap entry with this suffix. |
| 65 | auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); |
| 66 | if (IterBool.second) |
| 67 | return &*IterBool.first; |
| 68 | } |
| 69 | } |
| 70 | |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 71 | // Insert a value into the symbol table with the specified name... |
| 72 | // |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 73 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 74 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 75 | |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 76 | // Try inserting the name, assuming it won't conflict. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 77 | if (vmap.insert(V->getValueName())) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 78 | // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << |
| 79 | // *V << "\n"); |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 80 | return; |
| 81 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 82 | |
Chris Lattner | f511155 | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 83 | // Otherwise, there is a naming conflict. Rename this value. |
Daniel Dunbar | 29c1420 | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 84 | SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 85 | |
| 86 | // The name is too already used, just free it so we can allocate a new name. |
Duncan P. N. Exon Smith | dad20b2 | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 87 | V->getValueName()->Destroy(); |
| 88 | |
Rafael Espindola | a2197f8 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 89 | ValueName *VN = makeUniqueName(V, UniqueName); |
| 90 | V->setValueName(VN); |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 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) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 94 | // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 95 | // Remove the value from the symbol table. |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 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. |
Daniel Dunbar | 2928c83 | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 102 | ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { |
Chris Lattner | 9738846 | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 103 | // In the common case, the name is not already in the symbol table. |
David Blaikie | 1d4f28c | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 104 | auto IterBool = vmap.insert(std::make_pair(Name, V)); |
| 105 | if (IterBool.second) { |
Nicola Zaghen | 0818e78 | 2018-05-14 12:53:11 +0000 | [diff] [blame] | 106 | // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " |
Chris Lattner | 3f3098c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 107 | // << *V << "\n"); |
David Blaikie | 1d4f28c | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 108 | return &*IterBool.first; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 109 | } |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 110 | |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 111 | // Otherwise, there is a naming conflict. Rename this value. |
Benjamin Kramer | 4ef6822 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 112 | SmallString<256> UniqueName(Name.begin(), Name.end()); |
Rafael Espindola | a2197f8 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 113 | return makeUniqueName(V, UniqueName); |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 114 | } |
| 115 | |
Aaron Ballman | 1d03d38 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 116 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 117 | // dump - print out the symbol table |
| 118 | // |
Yaron Keren | 5530798 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 119 | LLVM_DUMP_METHOD void ValueSymbolTable::dump() const { |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 120 | //dbgs() << "ValueSymbolTable:\n"; |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 121 | for (const auto &I : *this) { |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 122 | //dbgs() << " '" << I->getKeyData() << "' = "; |
Benjamin Kramer | e96e21f | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 123 | I.getValue()->dump(); |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 124 | //dbgs() << "\n"; |
Chris Lattner | dec628e | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 125 | } |
Reid Spencer | eb7116b | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 126 | } |
Matthias Braun | 88d2075 | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 127 | #endif |