Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 1 | //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===// |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 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 | 6bda14b | 2017-06-06 11:49:48 +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" |
Jonas Hahnfeld | 5db24d7 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Triple.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 17 | #include "llvm/IR/GlobalValue.h" |
Jonas Hahnfeld | 5db24d7 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 18 | #include "llvm/IR/Module.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 19 | #include "llvm/IR/Type.h" |
Eugene Zelenko | deaf695 | 2017-02-17 00:00:09 +0000 | [diff] [blame] | 20 | #include "llvm/IR/Value.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Casting.h" |
| 22 | #include "llvm/Support/Compiler.h" |
Bill Wendling | 6a462f1 | 2006-11-17 08:03:48 +0000 | [diff] [blame] | 23 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | b99eac8 | 2009-07-24 10:05:20 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 25 | #include <cassert> |
| 26 | #include <utility> |
| 27 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Chandler Carruth | 1b9dde0 | 2014-04-22 02:02:50 +0000 | [diff] [blame] | 30 | #define DEBUG_TYPE "valuesymtab" |
| 31 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 32 | // Class destructor |
| 33 | ValueSymbolTable::~ValueSymbolTable() { |
| 34 | #ifndef NDEBUG // Only do this in -g mode... |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 35 | for (const auto &VI : vmap) |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 36 | dbgs() << "Value still in symbol table! Type = '" |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 37 | << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData() |
| 38 | << "'\n"; |
Chris Lattner | 86fc081 | 2007-02-07 06:28:48 +0000 | [diff] [blame] | 39 | assert(vmap.empty() && "Values remain in symbol table!"); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 40 | #endif |
| 41 | } |
| 42 | |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 43 | ValueName *ValueSymbolTable::makeUniqueName(Value *V, |
| 44 | SmallString<256> &UniqueName) { |
| 45 | unsigned BaseSize = UniqueName.size(); |
Eugene Zelenko | 33d7b76 | 2016-08-23 17:14:32 +0000 | [diff] [blame] | 46 | while (true) { |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 47 | // Trim any suffix off and append the next number. |
| 48 | UniqueName.resize(BaseSize); |
| 49 | raw_svector_ostream S(UniqueName); |
Jonas Hahnfeld | 5db24d7 | 2017-12-04 14:19:33 +0000 | [diff] [blame] | 50 | if (auto *GV = dyn_cast<GlobalValue>(V)) { |
| 51 | // A dot is appended to mark it as clone during ABI demangling so that |
| 52 | // for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second |
| 53 | // one being a clone. |
| 54 | // On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for |
| 55 | // identifiers. This breaks ABI demangling but at least ptxas accepts and |
| 56 | // compiles the program. |
| 57 | const Module *M = GV->getParent(); |
| 58 | if (!(M && Triple(M->getTargetTriple()).isNVPTX())) |
| 59 | S << "."; |
| 60 | } |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 61 | S << ++LastUnique; |
| 62 | |
| 63 | // Try insert the vmap entry with this suffix. |
| 64 | auto IterBool = vmap.insert(std::make_pair(UniqueName, V)); |
| 65 | if (IterBool.second) |
| 66 | return &*IterBool.first; |
| 67 | } |
| 68 | } |
| 69 | |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 70 | // Insert a value into the symbol table with the specified name... |
| 71 | // |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 72 | void ValueSymbolTable::reinsertValue(Value* V) { |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 73 | assert(V->hasName() && "Can't insert nameless Value into symbol table"); |
| 74 | |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 75 | // Try inserting the name, assuming it won't conflict. |
Duncan P. N. Exon Smith | 5bf8fef | 2014-12-09 18:38:53 +0000 | [diff] [blame] | 76 | if (vmap.insert(V->getValueName())) { |
| 77 | //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n"); |
Chris Lattner | 770dadf | 2007-02-07 05:22:49 +0000 | [diff] [blame] | 78 | return; |
| 79 | } |
| 80 | |
| 81 | // Otherwise, there is a naming conflict. Rename this value. |
Daniel Dunbar | 948f82b | 2009-08-19 19:22:52 +0000 | [diff] [blame] | 82 | SmallString<256> UniqueName(V->getName().begin(), V->getName().end()); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 83 | |
| 84 | // 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] | 85 | V->getValueName()->Destroy(); |
| 86 | |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 87 | ValueName *VN = makeUniqueName(V, UniqueName); |
| 88 | V->setValueName(VN); |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 89 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 90 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 91 | void ValueSymbolTable::removeValueName(ValueName *V) { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 92 | //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n"); |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 93 | // Remove the value from the symbol table. |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 94 | vmap.remove(V); |
| 95 | } |
| 96 | |
| 97 | /// createValueName - This method attempts to create a value name and insert |
| 98 | /// it into the symbol table with the specified name. If it conflicts, it |
| 99 | /// auto-renames the name and returns that instead. |
Daniel Dunbar | ad36e8a | 2009-11-06 10:58:06 +0000 | [diff] [blame] | 100 | ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) { |
Chris Lattner | 36b4e8f | 2008-06-27 21:26:26 +0000 | [diff] [blame] | 101 | // 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] | 102 | auto IterBool = vmap.insert(std::make_pair(Name, V)); |
| 103 | if (IterBool.second) { |
David Greene | 73631b9 | 2010-01-05 01:30:06 +0000 | [diff] [blame] | 104 | //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": " |
Chris Lattner | b377a7c | 2007-02-25 20:42:59 +0000 | [diff] [blame] | 105 | // << *V << "\n"); |
David Blaikie | 5106ce7 | 2014-11-19 05:49:42 +0000 | [diff] [blame] | 106 | return &*IterBool.first; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 107 | } |
Chris Lattner | d17dbe9 | 2007-02-07 06:25:36 +0000 | [diff] [blame] | 108 | |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 109 | // Otherwise, there is a naming conflict. Rename this value. |
Benjamin Kramer | feff705 | 2010-03-31 16:04:26 +0000 | [diff] [blame] | 110 | SmallString<256> UniqueName(Name.begin(), Name.end()); |
Rafael Espindola | d1beb07 | 2015-11-22 00:16:24 +0000 | [diff] [blame] | 111 | return makeUniqueName(V, UniqueName); |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Aaron Ballman | 615eb47 | 2017-10-15 14:32:27 +0000 | [diff] [blame] | 114 | #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 115 | // dump - print out the symbol table |
| 116 | // |
Yaron Keren | eb2a254 | 2016-01-29 20:50:44 +0000 | [diff] [blame] | 117 | LLVM_DUMP_METHOD void ValueSymbolTable::dump() const { |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 118 | //dbgs() << "ValueSymbolTable:\n"; |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 119 | for (const auto &I : *this) { |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 120 | //dbgs() << " '" << I->getKeyData() << "' = "; |
Benjamin Kramer | af28e7d | 2016-06-26 14:10:56 +0000 | [diff] [blame] | 121 | I.getValue()->dump(); |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 122 | //dbgs() << "\n"; |
Chris Lattner | 32ab643 | 2007-02-12 05:18:08 +0000 | [diff] [blame] | 123 | } |
Reid Spencer | 25780d5 | 2006-01-10 09:51:48 +0000 | [diff] [blame] | 124 | } |
Matthias Braun | 8c209aa | 2017-01-28 02:02:38 +0000 | [diff] [blame] | 125 | #endif |