OSDN Git Service

0c3946c8661ebe488af64a611ee5e157ed9f93e8
[android-x86/external-llvm.git] / 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/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"
23 #include <cassert>
24 #include <utility>
25
26 using namespace llvm;
27
28 #define DEBUG_TYPE "valuesymtab"
29
30 // Class destructor
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()
36            << "'\n";
37   assert(vmap.empty() && "Values remain in symbol table!");
38 #endif
39 }
40
41 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
42                                             SmallString<256> &UniqueName) {
43   unsigned BaseSize = UniqueName.size();
44   while (true) {
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))
49       S << ".";
50     S << ++LastUnique;
51
52     // Try insert the vmap entry with this suffix.
53     auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
54     if (IterBool.second)
55       return &*IterBool.first;
56   }
57 }
58
59 // Insert a value into the symbol table with the specified name...
60 //
61 void ValueSymbolTable::reinsertValue(Value* V) {
62   assert(V->hasName() && "Can't insert nameless Value into symbol table");
63
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");
67     return;
68   }
69   
70   // Otherwise, there is a naming conflict.  Rename this value.
71   SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
72
73   // The name is too already used, just free it so we can allocate a new name.
74   V->getValueName()->Destroy();
75
76   ValueName *VN = makeUniqueName(V, UniqueName);
77   V->setValueName(VN);
78 }
79
80 void ValueSymbolTable::removeValueName(ValueName *V) {
81   //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
82   // Remove the value from the symbol table.
83   vmap.remove(V);
84 }
85
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() << ": "
94     //           << *V << "\n");
95     return &*IterBool.first;
96   }
97   
98   // Otherwise, there is a naming conflict.  Rename this value.
99   SmallString<256> UniqueName(Name.begin(), Name.end());
100   return makeUniqueName(V, UniqueName);
101 }
102
103 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
104 // dump - print out the symbol table
105 //
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();
111     //dbgs() << "\n";
112   }
113 }
114 #endif