ValueSymbolTable.cpp 4.5 KB

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