109467b48Spatrick //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This file implements the ValueSymbolTable class for the IR library.
1009467b48Spatrick //
1109467b48Spatrick //===----------------------------------------------------------------------===//
1209467b48Spatrick 
1309467b48Spatrick #include "llvm/IR/ValueSymbolTable.h"
1409467b48Spatrick #include "llvm/ADT/SmallString.h"
1509467b48Spatrick #include "llvm/ADT/Triple.h"
1609467b48Spatrick #include "llvm/Config/llvm-config.h"
1709467b48Spatrick #include "llvm/IR/GlobalValue.h"
1809467b48Spatrick #include "llvm/IR/Module.h"
1909467b48Spatrick #include "llvm/IR/Type.h"
2009467b48Spatrick #include "llvm/IR/Value.h"
2109467b48Spatrick #include "llvm/Support/Casting.h"
2209467b48Spatrick #include "llvm/Support/Compiler.h"
2309467b48Spatrick #include "llvm/Support/Debug.h"
2409467b48Spatrick #include "llvm/Support/raw_ostream.h"
2509467b48Spatrick #include <cassert>
2609467b48Spatrick #include <utility>
2709467b48Spatrick 
2809467b48Spatrick using namespace llvm;
2909467b48Spatrick 
3009467b48Spatrick #define DEBUG_TYPE "valuesymtab"
3109467b48Spatrick 
3209467b48Spatrick // Class destructor
~ValueSymbolTable()3309467b48Spatrick ValueSymbolTable::~ValueSymbolTable() {
3409467b48Spatrick #ifndef NDEBUG // Only do this in -g mode...
3509467b48Spatrick   for (const auto &VI : vmap)
3609467b48Spatrick     dbgs() << "Value still in symbol table! Type = '"
3709467b48Spatrick            << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
3809467b48Spatrick            << "'\n";
3909467b48Spatrick   assert(vmap.empty() && "Values remain in symbol table!");
4009467b48Spatrick #endif
4109467b48Spatrick }
4209467b48Spatrick 
makeUniqueName(Value * V,SmallString<256> & UniqueName)4309467b48Spatrick ValueName *ValueSymbolTable::makeUniqueName(Value *V,
4409467b48Spatrick                                             SmallString<256> &UniqueName) {
4509467b48Spatrick   unsigned BaseSize = UniqueName.size();
4609467b48Spatrick   while (true) {
4709467b48Spatrick     // Trim any suffix off and append the next number.
4809467b48Spatrick     UniqueName.resize(BaseSize);
4909467b48Spatrick     raw_svector_ostream S(UniqueName);
5009467b48Spatrick     if (auto *GV = dyn_cast<GlobalValue>(V)) {
5109467b48Spatrick       // A dot is appended to mark it as clone during ABI demangling so that
5209467b48Spatrick       // for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
5309467b48Spatrick       // one being a clone.
5409467b48Spatrick       // On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
5509467b48Spatrick       // identifiers. This breaks ABI demangling but at least ptxas accepts and
5609467b48Spatrick       // compiles the program.
5709467b48Spatrick       const Module *M = GV->getParent();
5809467b48Spatrick       if (!(M && Triple(M->getTargetTriple()).isNVPTX()))
5909467b48Spatrick         S << ".";
6009467b48Spatrick     }
6109467b48Spatrick     S << ++LastUnique;
6209467b48Spatrick 
6309467b48Spatrick     // Try insert the vmap entry with this suffix.
64*73471bf0Spatrick     auto IterBool = vmap.insert(std::make_pair(UniqueName.str(), V));
6509467b48Spatrick     if (IterBool.second)
6609467b48Spatrick       return &*IterBool.first;
6709467b48Spatrick   }
6809467b48Spatrick }
6909467b48Spatrick 
7009467b48Spatrick // Insert a value into the symbol table with the specified name...
7109467b48Spatrick //
reinsertValue(Value * V)7209467b48Spatrick void ValueSymbolTable::reinsertValue(Value *V) {
7309467b48Spatrick   assert(V->hasName() && "Can't insert nameless Value into symbol table");
7409467b48Spatrick 
7509467b48Spatrick   // Try inserting the name, assuming it won't conflict.
7609467b48Spatrick   if (vmap.insert(V->getValueName())) {
7709467b48Spatrick     // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
7809467b48Spatrick     // *V << "\n");
7909467b48Spatrick     return;
8009467b48Spatrick   }
8109467b48Spatrick 
8209467b48Spatrick   // Otherwise, there is a naming conflict.  Rename this value.
8309467b48Spatrick   SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
8409467b48Spatrick 
8509467b48Spatrick   // The name is too already used, just free it so we can allocate a new name.
86097a140dSpatrick   MallocAllocator Allocator;
87097a140dSpatrick   V->getValueName()->Destroy(Allocator);
8809467b48Spatrick 
8909467b48Spatrick   ValueName *VN = makeUniqueName(V, UniqueName);
9009467b48Spatrick   V->setValueName(VN);
9109467b48Spatrick }
9209467b48Spatrick 
removeValueName(ValueName * V)9309467b48Spatrick void ValueSymbolTable::removeValueName(ValueName *V) {
9409467b48Spatrick   // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
9509467b48Spatrick   // Remove the value from the symbol table.
9609467b48Spatrick   vmap.remove(V);
9709467b48Spatrick }
9809467b48Spatrick 
9909467b48Spatrick /// createValueName - This method attempts to create a value name and insert
10009467b48Spatrick /// it into the symbol table with the specified name.  If it conflicts, it
10109467b48Spatrick /// auto-renames the name and returns that instead.
createValueName(StringRef Name,Value * V)10209467b48Spatrick ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
103*73471bf0Spatrick   if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
104*73471bf0Spatrick     Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
105*73471bf0Spatrick 
10609467b48Spatrick   // In the common case, the name is not already in the symbol table.
10709467b48Spatrick   auto IterBool = vmap.insert(std::make_pair(Name, V));
10809467b48Spatrick   if (IterBool.second) {
10909467b48Spatrick     // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
11009467b48Spatrick     //           << *V << "\n");
11109467b48Spatrick     return &*IterBool.first;
11209467b48Spatrick   }
11309467b48Spatrick 
11409467b48Spatrick   // Otherwise, there is a naming conflict.  Rename this value.
11509467b48Spatrick   SmallString<256> UniqueName(Name.begin(), Name.end());
11609467b48Spatrick   return makeUniqueName(V, UniqueName);
11709467b48Spatrick }
11809467b48Spatrick 
11909467b48Spatrick #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
12009467b48Spatrick // dump - print out the symbol table
12109467b48Spatrick //
dump() const12209467b48Spatrick LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
12309467b48Spatrick   // dbgs() << "ValueSymbolTable:\n";
12409467b48Spatrick   for (const auto &I : *this) {
12509467b48Spatrick     // dbgs() << "  '" << I->getKeyData() << "' = ";
12609467b48Spatrick     I.getValue()->dump();
12709467b48Spatrick     // dbgs() << "\n";
12809467b48Spatrick   }
12909467b48Spatrick }
13009467b48Spatrick #endif
131