1 //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ValueSymbolTable class for the IR library.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/IR/GlobalValue.h"
16 #include "llvm/IR/Type.h"
17 #include "llvm/IR/Value.h"
18 #include "llvm/IR/ValueSymbolTable.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/raw_ostream.h"
28 #define DEBUG_TYPE "valuesymtab"
31 ValueSymbolTable::~ValueSymbolTable() {
32 #ifndef NDEBUG // Only do this in -g mode...
33 for (const auto &VI : vmap)
34 dbgs() << "Value still in symbol table! Type = '"
35 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
37 assert(vmap.empty() && "Values remain in symbol table!");
41 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
42 SmallString<256> &UniqueName) {
43 unsigned BaseSize = UniqueName.size();
45 // Trim any suffix off and append the next number.
46 UniqueName.resize(BaseSize);
47 raw_svector_ostream S(UniqueName);
48 if (isa<GlobalValue>(V))
52 // Try insert the vmap entry with this suffix.
53 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
55 return &*IterBool.first;
59 // Insert a value into the symbol table with the specified name...
61 void ValueSymbolTable::reinsertValue(Value* V) {
62 assert(V->hasName() && "Can't insert nameless Value into symbol table");
64 // Try inserting the name, assuming it won't conflict.
65 if (vmap.insert(V->getValueName())) {
66 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
70 // Otherwise, there is a naming conflict. Rename this value.
71 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
73 // The name is too already used, just free it so we can allocate a new name.
74 V->getValueName()->Destroy();
76 ValueName *VN = makeUniqueName(V, UniqueName);
80 void ValueSymbolTable::removeValueName(ValueName *V) {
81 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
82 // Remove the value from the symbol table.
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 // In the common case, the name is not already in the symbol table.
91 auto IterBool = vmap.insert(std::make_pair(Name, V));
92 if (IterBool.second) {
93 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
95 return &*IterBool.first;
98 // Otherwise, there is a naming conflict. Rename this value.
99 SmallString<256> UniqueName(Name.begin(), Name.end());
100 return makeUniqueName(V, UniqueName);
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104 // dump - print out the symbol table
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();