LLVM 20.0.0git
ValueSymbolTable.cpp
Go to the documentation of this file.
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
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"
22#include "llvm/Support/Debug.h"
25#include <cassert>
26#include <utility>
27
28using namespace llvm;
29
30#define DEBUG_TYPE "valuesymtab"
31
32// Class destructor
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>(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 && Triple(M->getTargetTriple()).isNVPTX()))
56 AppenDot = true;
57 }
58
59 while (true) {
60 // Trim any suffix off and append the next number.
61 UniqueName.resize(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(std::make_pair(UniqueName.str(), V));
76 if (IterBool.second)
77 return &*IterBool.first;
78 }
79}
80
81// Insert a value into the symbol table with the specified name...
82//
83void ValueSymbolTable::reinsertValue(Value *V) {
84 assert(V->hasName() && "Can't insert nameless Value into symbol table");
85
86 // Try inserting the name, assuming it won't conflict.
87 if (vmap.insert(V->getValueName())) {
88 // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
89 // *V << "\n");
90 return;
91 }
92
93 // Otherwise, there is a naming conflict. Rename this value.
94 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
95
96 // The name is too already used, just free it so we can allocate a new name.
98 V->getValueName()->Destroy(Allocator);
99
100 ValueName *VN = makeUniqueName(V, UniqueName);
101 V->setValueName(VN);
102}
103
104void ValueSymbolTable::removeValueName(ValueName *V) {
105 // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
106 // Remove the value from the symbol table.
107 vmap.remove(V);
108}
109
110/// createValueName - This method attempts to create a value name and insert
111/// it into the symbol table with the specified name. If it conflicts, it
112/// auto-renames the name and returns that instead.
113ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
114 if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
115 Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
116
117 // In the common case, the name is not already in the symbol table.
118 auto IterBool = vmap.insert(std::make_pair(Name, V));
119 if (IterBool.second) {
120 // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
121 // << *V << "\n");
122 return &*IterBool.first;
123 }
124
125 // Otherwise, there is a naming conflict. Rename this value.
126 SmallString<256> UniqueName(Name.begin(), Name.end());
127 return makeUniqueName(V, UniqueName);
128}
129
130#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
131// dump - print out the symbol table
132//
134 // dbgs() << "ValueSymbolTable:\n";
135 for (const auto &I : *this) {
136 // dbgs() << " '" << I->getKeyData() << "' = ";
137 I.getValue()->dump();
138 // dbgs() << "\n";
139 }
140}
141#endif
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:622
std::string Name
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition: MD5.cpp:58
Basic Register Allocator
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition: SmallString.h:26
StringRef str() const
Explicit conversion to StringRef.
Definition: SmallString.h:254
size_t size() const
Definition: SmallVector.h:78
void resize(size_type N)
Definition: SmallVector.h:638
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:414
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:308
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
bool isNVPTX() const
Tests whether the target is NVPTX (32- or 64-bit).
Definition: Triple.h:855
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
LLVM Value Representation.
Definition: Value.h:74
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:691
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163