• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
~ValueSymbolTable()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 
makeUniqueName(Value * V,SmallString<256> & UniqueName)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 //
reinsertValue(Value * V)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     // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
79     // *V << "\n");
80     return;
81   }
82 
83   // Otherwise, there is a naming conflict.  Rename this value.
84   SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
85 
86   // The name is too already used, just free it so we can allocate a new name.
87   V->getValueName()->Destroy();
88 
89   ValueName *VN = makeUniqueName(V, UniqueName);
90   V->setValueName(VN);
91 }
92 
removeValueName(ValueName * V)93 void ValueSymbolTable::removeValueName(ValueName *V) {
94   // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
95   // Remove the value from the symbol table.
96   vmap.remove(V);
97 }
98 
99 /// createValueName - This method attempts to create a value name and insert
100 /// it into the symbol table with the specified name.  If it conflicts, it
101 /// auto-renames the name and returns that instead.
createValueName(StringRef Name,Value * V)102 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
103   // In the common case, the name is not already in the symbol table.
104   auto IterBool = vmap.insert(std::make_pair(Name, V));
105   if (IterBool.second) {
106     // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
107     //           << *V << "\n");
108     return &*IterBool.first;
109   }
110 
111   // Otherwise, there is a naming conflict.  Rename this value.
112   SmallString<256> UniqueName(Name.begin(), Name.end());
113   return makeUniqueName(V, UniqueName);
114 }
115 
116 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
117 // dump - print out the symbol table
118 //
dump() const119 LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
120   //dbgs() << "ValueSymbolTable:\n";
121   for (const auto &I : *this) {
122     //dbgs() << "  '" << I->getKeyData() << "' = ";
123     I.getValue()->dump();
124     //dbgs() << "\n";
125   }
126 }
127 #endif
128