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