LLVM  4.0.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/Casting.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
22 #include <cassert>
23 #include <utility>
24 
25 using namespace llvm;
26 
27 #define DEBUG_TYPE "valuesymtab"
28 
29 // Class destructor
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 //
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 }
bool hasName() const
Definition: Value.h:236
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:450
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:36
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:414
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:490
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:191
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMap.h:203
ValueName * getValueName() const
Definition: Value.cpp:163
iterator begin() const
Definition: StringRef.h:103
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:348
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
#define I(x, y, z)
Definition: MD5.cpp:54
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:135
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:71
iterator end() const
Definition: StringRef.h:105
void setValueName(ValueName *VN)
Definition: Value.cpp:174
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:47
void resize(size_type N)
Definition: SmallVector.h:352