1 //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ValueSymbolTable class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/ValueSymbolTable.h"
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/ADT/Triple.h"
17 #include "llvm/Config/llvm-config.h"
18 #include "llvm/IR/GlobalValue.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/IR/Type.h"
21 #include "llvm/IR/Value.h"
22 #include "llvm/Support/Casting.h"
23 #include "llvm/Support/Compiler.h"
24 #include "llvm/Support/Debug.h"
25 #include "llvm/Support/raw_ostream.h"
31 #define DEBUG_TYPE "valuesymtab"
34 ValueSymbolTable::~ValueSymbolTable() {
35 #ifndef NDEBUG // Only do this in -g mode...
36 for (const auto &VI
: vmap
)
37 dbgs() << "Value still in symbol table! Type = '"
38 << *VI
.getValue()->getType() << "' Name = '" << VI
.getKeyData()
40 assert(vmap
.empty() && "Values remain in symbol table!");
44 ValueName
*ValueSymbolTable::makeUniqueName(Value
*V
,
45 SmallString
<256> &UniqueName
) {
46 unsigned BaseSize
= UniqueName
.size();
48 // Trim any suffix off and append the next number.
49 UniqueName
.resize(BaseSize
);
50 raw_svector_ostream
S(UniqueName
);
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
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()))
64 // Try insert the vmap entry with this suffix.
65 auto IterBool
= vmap
.insert(std::make_pair(UniqueName
, V
));
67 return &*IterBool
.first
;
71 // Insert a value into the symbol table with the specified name...
73 void ValueSymbolTable::reinsertValue(Value
* V
) {
74 assert(V
->hasName() && "Can't insert nameless Value into symbol table");
76 // Try inserting the name, assuming it won't conflict.
77 if (vmap
.insert(V
->getValueName())) {
78 // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
83 // Otherwise, there is a naming conflict. Rename this value.
84 SmallString
<256> UniqueName(V
->getName().begin(), V
->getName().end());
86 // The name is too already used, just free it so we can allocate a new name.
87 V
->getValueName()->Destroy();
89 ValueName
*VN
= makeUniqueName(V
, UniqueName
);
93 void ValueSymbolTable::removeValueName(ValueName
*V
) {
94 // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
95 // Remove the value from the symbol table.
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.
102 ValueName
*ValueSymbolTable::createValueName(StringRef Name
, Value
*V
) {
103 // In the common case, the name is not already in the symbol table.
104 auto IterBool
= vmap
.insert(std::make_pair(Name
, V
));
105 if (IterBool
.second
) {
106 // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
108 return &*IterBool
.first
;
111 // Otherwise, there is a naming conflict. Rename this value.
112 SmallString
<256> UniqueName(Name
.begin(), Name
.end());
113 return makeUniqueName(V
, UniqueName
);
116 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
117 // dump - print out the symbol table
119 LLVM_DUMP_METHOD
void ValueSymbolTable::dump() const {
120 //dbgs() << "ValueSymbolTable:\n";
121 for (const auto &I
: *this) {
122 //dbgs() << " '" << I->getKeyData() << "' = ";
123 I
.getValue()->dump();