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