]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/IR/ValueSymbolTable.cpp
Update to bmake 20170413
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / IR / ValueSymbolTable.cpp
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/IR/GlobalValue.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/Support/Casting.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cassert>
23 #include <utility>
24
25 using namespace llvm;
26
27 #define DEBUG_TYPE "valuesymtab"
28
29 // Class destructor
30 ValueSymbolTable::~ValueSymbolTable() {
31 #ifndef NDEBUG   // Only do this in -g mode...
32   for (const auto &VI : vmap)
33     dbgs() << "Value still in symbol table! Type = '"
34            << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
35            << "'\n";
36   assert(vmap.empty() && "Values remain in symbol table!");
37 #endif
38 }
39
40 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
41                                             SmallString<256> &UniqueName) {
42   unsigned BaseSize = UniqueName.size();
43   while (true) {
44     // Trim any suffix off and append the next number.
45     UniqueName.resize(BaseSize);
46     raw_svector_ostream S(UniqueName);
47     if (isa<GlobalValue>(V))
48       S << ".";
49     S << ++LastUnique;
50
51     // Try insert the vmap entry with this suffix.
52     auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
53     if (IterBool.second)
54       return &*IterBool.first;
55   }
56 }
57
58 // Insert a value into the symbol table with the specified name...
59 //
60 void ValueSymbolTable::reinsertValue(Value* V) {
61   assert(V->hasName() && "Can't insert nameless Value into symbol table");
62
63   // Try inserting the name, assuming it won't conflict.
64   if (vmap.insert(V->getValueName())) {
65     //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
66     return;
67   }
68   
69   // Otherwise, there is a naming conflict.  Rename this value.
70   SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
71
72   // The name is too already used, just free it so we can allocate a new name.
73   V->getValueName()->Destroy();
74
75   ValueName *VN = makeUniqueName(V, UniqueName);
76   V->setValueName(VN);
77 }
78
79 void ValueSymbolTable::removeValueName(ValueName *V) {
80   //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
81   // Remove the value from the symbol table.
82   vmap.remove(V);
83 }
84
85 /// createValueName - This method attempts to create a value name and insert
86 /// it into the symbol table with the specified name.  If it conflicts, it
87 /// auto-renames the name and returns that instead.
88 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
89   // In the common case, the name is not already in the symbol table.
90   auto IterBool = vmap.insert(std::make_pair(Name, V));
91   if (IterBool.second) {
92     //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
93     //           << *V << "\n");
94     return &*IterBool.first;
95   }
96   
97   // Otherwise, there is a naming conflict.  Rename this value.
98   SmallString<256> UniqueName(Name.begin(), Name.end());
99   return makeUniqueName(V, UniqueName);
100 }
101
102 // dump - print out the symbol table
103 //
104 LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
105   //DEBUG(dbgs() << "ValueSymbolTable:\n");
106   for (const auto &I : *this) {
107     //DEBUG(dbgs() << "  '" << I->getKeyData() << "' = ");
108     I.getValue()->dump();
109     //DEBUG(dbgs() << "\n");
110   }
111 }