ValueSymbolTable.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the ValueSymbolTable class for the IR library.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/IR/ValueSymbolTable.h"
  14. #include "llvm/ADT/SmallString.h"
  15. #include "llvm/IR/GlobalValue.h"
  16. #include "llvm/IR/Type.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/raw_ostream.h"
  19. using namespace llvm;
  20. #define DEBUG_TYPE "valuesymtab"
  21. // Class destructor
  22. ValueSymbolTable::~ValueSymbolTable() {
  23. #ifndef NDEBUG // Only do this in -g mode...
  24. for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
  25. dbgs() << "Value still in symbol table! Type = '"
  26. << *VI->getValue()->getType() << "' Name = '"
  27. << VI->getKeyData() << "'\n";
  28. assert(vmap.empty() && "Values remain in symbol table!");
  29. #endif
  30. }
  31. // Insert a value into the symbol table with the specified name...
  32. //
  33. void ValueSymbolTable::reinsertValue(Value* V) {
  34. assert(V->hasName() && "Can't insert nameless Value into symbol table");
  35. // Try inserting the name, assuming it won't conflict.
  36. if (vmap.insert(V->getValueName())) {
  37. //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
  38. return;
  39. }
  40. // Otherwise, there is a naming conflict. Rename this value.
  41. SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
  42. // The name is too already used, just free it so we can allocate a new name.
  43. V->getValueName()->Destroy();
  44. unsigned BaseSize = UniqueName.size();
  45. while (1) {
  46. // Trim any suffix off and append the next number.
  47. UniqueName.resize(BaseSize);
  48. raw_svector_ostream(UniqueName) << "." << ++LastUnique;
  49. // Try insert the vmap entry with this suffix.
  50. auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
  51. if (IterBool.second) {
  52. // Newly inserted name. Success!
  53. V->setValueName(&*IterBool.first);
  54. //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
  55. return;
  56. }
  57. }
  58. }
  59. void ValueSymbolTable::removeValueName(ValueName *V) {
  60. //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
  61. // Remove the value from the symbol table.
  62. vmap.remove(V);
  63. }
  64. /// createValueName - This method attempts to create a value name and insert
  65. /// it into the symbol table with the specified name. If it conflicts, it
  66. /// auto-renames the name and returns that instead.
  67. ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
  68. // In the common case, the name is not already in the symbol table.
  69. auto IterBool = vmap.insert(std::make_pair(Name, V));
  70. if (IterBool.second) {
  71. //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
  72. // << *V << "\n");
  73. return &*IterBool.first;
  74. }
  75. // Otherwise, there is a naming conflict. Rename this value.
  76. SmallString<256> UniqueName(Name.begin(), Name.end());
  77. while (1) {
  78. // Trim any suffix off and append the next number.
  79. UniqueName.resize(Name.size());
  80. raw_svector_ostream(UniqueName) << ++LastUnique;
  81. // Try insert the vmap entry with this suffix.
  82. auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
  83. if (IterBool.second) {
  84. // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V <<
  85. // "\n");
  86. return &*IterBool.first;
  87. }
  88. }
  89. }
  90. // dump - print out the symbol table
  91. //
  92. void ValueSymbolTable::dump() const {
  93. //DEBUG(dbgs() << "ValueSymbolTable:\n");
  94. for (const_iterator I = begin(), E = end(); I != E; ++I) {
  95. //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
  96. I->getValue()->dump();
  97. //DEBUG(dbgs() << "\n");
  98. }
  99. }