LLVM 22.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
27using namespace llvm;
28
29#define DEBUG_TYPE "valuesymtab"
30
31// Class destructor
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>(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(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(std::make_pair(UniqueName.str(), 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(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);
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(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(0, std::max(1u, (unsigned)MaxNameSize));
115
116 // In the common case, the name is not already in the symbol table.
117 auto IterBool = vmap.insert(std::make_pair(Name, 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//
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
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:638
Module.h This file contains the declarations for the Module class.
#define I(x, y, z)
Definition MD5.cpp:57
Basic Register Allocator
This file defines the SmallString class.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
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.
void resize(size_type N)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM_ABI 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:75
A raw_ostream that writes to an SmallVector or SmallString.
This is an optimization pass for GlobalISel generic memory operations.
StringMapEntry< Value * > ValueName
Definition Value.h:56
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:207