LLVM  6.0.0svn
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/IR/Value.h"
19 #include "llvm/Support/Casting.h"
20 #include "llvm/Support/Compiler.h"
21 #include "llvm/Support/Debug.h"
23 #include <cassert>
24 #include <utility>
25 
26 using namespace llvm;
27 
28 #define DEBUG_TYPE "valuesymtab"
29 
30 // Class destructor
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 //
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
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMap.h:126
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:436
LLVM_ATTRIBUTE_ALWAYS_INLINE size_type size() const
Definition: SmallVector.h:136
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:489
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMap.h:204
bool hasName() const
Definition: Value.h:251
ValueName * getValueName() const
Definition: Value.cpp:192
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:370
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
iterator begin() const
Definition: StringRef.h:106
StringRef getName() const
Return a constant reference to the value&#39;s name.
Definition: Value.cpp:220
#define I(x, y, z)
Definition: MD5.cpp:58
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
Definition: Value.h:73
void setValueName(ValueName *VN)
Definition: Value.cpp:203
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
iterator end() const
Definition: StringRef.h:108
void resize(size_type N)
Definition: SmallVector.h:355