1 //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements the ValueSymbolTable class for the IR library.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/IR/ValueSymbolTable.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Config/llvm-config.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
30 #define DEBUG_TYPE "valuesymtab"
33 ValueSymbolTable::~ValueSymbolTable() {
34 #ifndef NDEBUG // Only do this in -g mode...
35 for (const auto &VI
: vmap
)
36 dbgs() << "Value still in symbol table! Type = '"
37 << *VI
.getValue()->getType() << "' Name = '" << VI
.getKeyData()
39 assert(vmap
.empty() && "Values remain in symbol table!");
43 ValueName
*ValueSymbolTable::makeUniqueName(Value
*V
,
44 SmallString
<256> &UniqueName
) {
45 unsigned BaseSize
= UniqueName
.size();
47 // Trim any suffix off and append the next number.
48 UniqueName
.resize(BaseSize
);
49 raw_svector_ostream
S(UniqueName
);
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
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()))
63 // Try insert the vmap entry with this suffix.
64 auto IterBool
= vmap
.insert(std::make_pair(UniqueName
, V
));
66 return &*IterBool
.first
;
70 // Insert a value into the symbol table with the specified name...
72 void ValueSymbolTable::reinsertValue(Value
* V
) {
73 assert(V
->hasName() && "Can't insert nameless Value into symbol table");
75 // Try inserting the name, assuming it won't conflict.
76 if (vmap
.insert(V
->getValueName())) {
77 // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
82 // Otherwise, there is a naming conflict. Rename this value.
83 SmallString
<256> UniqueName(V
->getName().begin(), V
->getName().end());
85 // The name is too already used, just free it so we can allocate a new name.
86 V
->getValueName()->Destroy();
88 ValueName
*VN
= makeUniqueName(V
, UniqueName
);
92 void ValueSymbolTable::removeValueName(ValueName
*V
) {
93 // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
94 // Remove the value from the symbol table.
98 /// createValueName - This method attempts to create a value name and insert
99 /// it into the symbol table with the specified name. If it conflicts, it
100 /// auto-renames the name and returns that instead.
101 ValueName
*ValueSymbolTable::createValueName(StringRef Name
, Value
*V
) {
102 // In the common case, the name is not already in the symbol table.
103 auto IterBool
= vmap
.insert(std::make_pair(Name
, V
));
104 if (IterBool
.second
) {
105 // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
107 return &*IterBool
.first
;
110 // Otherwise, there is a naming conflict. Rename this value.
111 SmallString
<256> UniqueName(Name
.begin(), Name
.end());
112 return makeUniqueName(V
, UniqueName
);
115 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
116 // dump - print out the symbol table
118 LLVM_DUMP_METHOD
void ValueSymbolTable::dump() const {
119 //dbgs() << "ValueSymbolTable:\n";
120 for (const auto &I
: *this) {
121 //dbgs() << " '" << I->getKeyData() << "' = ";
122 I
.getValue()->dump();