1//===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the ValueSymbolTable class for the IR library.
10//
11//===----------------------------------------------------------------------===//
12
13#include "llvm/IR/ValueSymbolTable.h"
14#include "llvm/ADT/SmallString.h"
15#include "llvm/Config/llvm-config.h"
16#include "llvm/IR/GlobalValue.h"
17#include "llvm/IR/Module.h"
18#include "llvm/IR/Type.h"
19#include "llvm/IR/Value.h"
20#include "llvm/Support/Casting.h"
21#include "llvm/Support/Compiler.h"
22#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
24#include "llvm/TargetParser/Triple.h"
25#include <cassert>
26
27using namespace llvm;
28
29#define DEBUG_TYPE "valuesymtab"
30
31// Class destructor
32ValueSymbolTable::~ValueSymbolTable() {
33#ifndef NDEBUG // Only do this in -g mode...
34 for (const auto &VI : vmap)
35 dbgs() << "Value still in symbol table! Type = '"
36 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
37 << "'\n";
38 assert(vmap.empty() && "Values remain in symbol table!");
39#endif
40}
41
42ValueName *ValueSymbolTable::makeUniqueName(Value *V,
43 SmallString<256> &UniqueName) {
44 unsigned BaseSize = UniqueName.size();
45 bool AppenDot = false;
46 if (auto *GV = dyn_cast<GlobalValue>(Val: V)) {
47 // A dot is appended to mark it as clone during ABI demangling so that
48 // for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
49 // one being a clone.
50 // On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
51 // identifiers. This breaks ABI demangling but at least ptxas accepts and
52 // compiles the program.
53 const Module *M = GV->getParent();
54 if (!(M && M->getTargetTriple().isNVPTX()))
55 AppenDot = true;
56 }
57
58 while (true) {
59 // Trim any suffix off and append the next number.
60 UniqueName.resize(N: BaseSize);
61 raw_svector_ostream S(UniqueName);
62 if (AppenDot)
63 S << ".";
64 S << ++LastUnique;
65
66 // Retry if MaxNameSize has been exceeded.
67 if (MaxNameSize > -1 && UniqueName.size() > (size_t)MaxNameSize) {
68 assert(BaseSize >= UniqueName.size() - (size_t)MaxNameSize &&
69 "Can't generate unique name: MaxNameSize is too small.");
70 BaseSize -= UniqueName.size() - (size_t)MaxNameSize;
71 continue;
72 }
73 // Try insert the vmap entry with this suffix.
74 auto IterBool = vmap.insert(KV: std::make_pair(x: UniqueName.str(), y&: V));
75 if (IterBool.second)
76 return &*IterBool.first;
77 }
78}
79
80// Insert a value into the symbol table with the specified name...
81//
82void ValueSymbolTable::reinsertValue(Value *V) {
83 assert(V->hasName() && "Can't insert nameless Value into symbol table");
84
85 // Try inserting the name, assuming it won't conflict.
86 if (vmap.insert(KeyValue: V->getValueName())) {
87 // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
88 // *V << "\n");
89 return;
90 }
91
92 // Otherwise, there is a naming conflict. Rename this value.
93 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
94
95 // The name is too already used, just free it so we can allocate a new name.
96 MallocAllocator Allocator;
97 V->getValueName()->Destroy(allocator&: Allocator);
98
99 ValueName *VN = makeUniqueName(V, UniqueName);
100 V->setValueName(VN);
101}
102
103void ValueSymbolTable::removeValueName(ValueName *V) {
104 // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
105 // Remove the value from the symbol table.
106 vmap.remove(KeyValue: V);
107}
108
109/// createValueName - This method attempts to create a value name and insert
110/// it into the symbol table with the specified name. If it conflicts, it
111/// auto-renames the name and returns that instead.
112ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
113 if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
114 Name = Name.substr(Start: 0, N: std::max(a: 1u, b: (unsigned)MaxNameSize));
115
116 // In the common case, the name is not already in the symbol table.
117 auto IterBool = vmap.insert(KV: std::make_pair(x&: Name, y&: V));
118 if (IterBool.second) {
119 // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
120 // << *V << "\n");
121 return &*IterBool.first;
122 }
123
124 // Otherwise, there is a naming conflict. Rename this value.
125 SmallString<256> UniqueName(Name.begin(), Name.end());
126 return makeUniqueName(V, UniqueName);
127}
128
129#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
130// dump - print out the symbol table
131//
132LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
133 // dbgs() << "ValueSymbolTable:\n";
134 for (const auto &I : *this) {
135 // dbgs() << " '" << I->getKeyData() << "' = ";
136 I.getValue()->dump();
137 // dbgs() << "\n";
138 }
139}
140#endif
141