LLVM  3.7.0
ValueSymbolTable.cpp
Go to the documentation of this file.
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 
15 #include "llvm/ADT/SmallString.h"
16 #include "llvm/IR/GlobalValue.h"
17 #include "llvm/IR/Type.h"
18 #include "llvm/Support/Debug.h"
20 using namespace llvm;
21 
22 #define DEBUG_TYPE "valuesymtab"
23 
24 // Class destructor
26 #ifndef NDEBUG // Only do this in -g mode...
27  for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
28  dbgs() << "Value still in symbol table! Type = '"
29  << *VI->getValue()->getType() << "' Name = '"
30  << VI->getKeyData() << "'\n";
31  assert(vmap.empty() && "Values remain in symbol table!");
32 #endif
33 }
34 
35 // Insert a value into the symbol table with the specified name...
36 //
37 void ValueSymbolTable::reinsertValue(Value* V) {
38  assert(V->hasName() && "Can't insert nameless Value into symbol table");
39 
40  // Try inserting the name, assuming it won't conflict.
41  if (vmap.insert(V->getValueName())) {
42  //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
43  return;
44  }
45 
46  // Otherwise, there is a naming conflict. Rename this value.
47  SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
48 
49  // The name is too already used, just free it so we can allocate a new name.
50  V->getValueName()->Destroy();
51 
52  unsigned BaseSize = UniqueName.size();
53  while (1) {
54  // Trim any suffix off and append the next number.
55  UniqueName.resize(BaseSize);
56  raw_svector_ostream(UniqueName) << "." << ++LastUnique;
57 
58  // Try insert the vmap entry with this suffix.
59  auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
60  if (IterBool.second) {
61  // Newly inserted name. Success!
62  V->setValueName(&*IterBool.first);
63  //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
64  return;
65  }
66  }
67 }
68 
69 void ValueSymbolTable::removeValueName(ValueName *V) {
70  //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
71  // Remove the value from the symbol table.
72  vmap.remove(V);
73 }
74 
75 /// createValueName - This method attempts to create a value name and insert
76 /// it into the symbol table with the specified name. If it conflicts, it
77 /// auto-renames the name and returns that instead.
78 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
79  // In the common case, the name is not already in the symbol table.
80  auto IterBool = vmap.insert(std::make_pair(Name, V));
81  if (IterBool.second) {
82  //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
83  // << *V << "\n");
84  return &*IterBool.first;
85  }
86 
87  // Otherwise, there is a naming conflict. Rename this value.
88  SmallString<256> UniqueName(Name.begin(), Name.end());
89 
90  while (1) {
91  // Trim any suffix off and append the next number.
92  UniqueName.resize(Name.size());
93  raw_svector_ostream(UniqueName) << ++LastUnique;
94 
95  // Try insert the vmap entry with this suffix.
96  auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
97  if (IterBool.second) {
98  // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V <<
99  // "\n");
100  return &*IterBool.first;
101  }
102  }
103 }
104 
105 
106 // dump - print out the symbol table
107 //
109  //DEBUG(dbgs() << "ValueSymbolTable:\n");
110  for (const_iterator I = begin(), E = end(); I != E; ++I) {
111  //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
112  I->getValue()->dump();
113  //DEBUG(dbgs() << "\n");
114  }
115 }
iterator begin()
Get an iterator that from the beginning of the symbol table.
bool hasName() const
Definition: Value.h:228
size_t size() const
size - Get the string size.
Definition: StringRef.h:113
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:28
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:356
bool empty() const
Definition: StringMap.h:98
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:488
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:188
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMap.h:193
iterator end()
Get an iterator to the end of the symbol table.
ValueName * getValueName() const
Definition: Value.cpp:160
iterator begin() const
Definition: StringRef.h:90
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:298
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:123
iterator begin()
Definition: StringMap.h:252
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM Value Representation.
Definition: Value.h:69
iterator end() const
Definition: StringRef.h:92
void setValueName(ValueName *VN)
Definition: Value.cpp:171
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
iterator end()
Definition: StringMap.h:255
void resize(size_type N)
Definition: SmallVector.h:376