LLVM  7.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/ADT/Triple.h"
17 #include "llvm/IR/GlobalValue.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/IR/Type.h"
20 #include "llvm/IR/Value.h"
21 #include "llvm/Support/Casting.h"
22 #include "llvm/Support/Compiler.h"
23 #include "llvm/Support/Debug.h"
25 #include <cassert>
26 #include <utility>
27 
28 using namespace llvm;
29 
30 #define DEBUG_TYPE "valuesymtab"
31 
32 // Class destructor
34 #ifndef NDEBUG // Only do this in -g mode...
35  for (const auto &VI : vmap)
36  dbgs() << "Value still in symbol table! Type = '"
37  << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
38  << "'\n";
39  assert(vmap.empty() && "Values remain in symbol table!");
40 #endif
41 }
42 
43 ValueName *ValueSymbolTable::makeUniqueName(Value *V,
44  SmallString<256> &UniqueName) {
45  unsigned BaseSize = UniqueName.size();
46  while (true) {
47  // Trim any suffix off and append the next number.
48  UniqueName.resize(BaseSize);
49  raw_svector_ostream S(UniqueName);
50  if (auto *GV = dyn_cast<GlobalValue>(V)) {
51  // A dot is appended to mark it as clone during ABI demangling so that
52  // for example "_Z1fv" and "_Z1fv.1" both demangle to "f()", the second
53  // one being a clone.
54  // On NVPTX we cannot use a dot because PTX only allows [A-Za-z0-9_$] for
55  // identifiers. This breaks ABI demangling but at least ptxas accepts and
56  // compiles the program.
57  const Module *M = GV->getParent();
58  if (!(M && Triple(M->getTargetTriple()).isNVPTX()))
59  S << ".";
60  }
61  S << ++LastUnique;
62 
63  // Try insert the vmap entry with this suffix.
64  auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
65  if (IterBool.second)
66  return &*IterBool.first;
67  }
68 }
69 
70 // Insert a value into the symbol table with the specified name...
71 //
72 void ValueSymbolTable::reinsertValue(Value* V) {
73  assert(V->hasName() && "Can't insert nameless Value into symbol table");
74 
75  // Try inserting the name, assuming it won't conflict.
76  if (vmap.insert(V->getValueName())) {
77  //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
78  return;
79  }
80 
81  // Otherwise, there is a naming conflict. Rename this value.
82  SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
83 
84  // The name is too already used, just free it so we can allocate a new name.
85  V->getValueName()->Destroy();
86 
87  ValueName *VN = makeUniqueName(V, UniqueName);
88  V->setValueName(VN);
89 }
90 
91 void ValueSymbolTable::removeValueName(ValueName *V) {
92  //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
93  // Remove the value from the symbol table.
94  vmap.remove(V);
95 }
96 
97 /// createValueName - This method attempts to create a value name and insert
98 /// it into the symbol table with the specified name. If it conflicts, it
99 /// auto-renames the name and returns that instead.
100 ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
101  // In the common case, the name is not already in the symbol table.
102  auto IterBool = vmap.insert(std::make_pair(Name, V));
103  if (IterBool.second) {
104  //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
105  // << *V << "\n");
106  return &*IterBool.first;
107  }
108 
109  // Otherwise, there is a naming conflict. Rename this value.
110  SmallString<256> UniqueName(Name.begin(), Name.end());
111  return makeUniqueName(V, UniqueName);
112 }
113 
114 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
115 // dump - print out the symbol table
116 //
118  //dbgs() << "ValueSymbolTable:\n";
119  for (const auto &I : *this) {
120  //dbgs() << " '" << I->getKeyData() << "' = ";
121  I.getValue()->dump();
122  //dbgs() << "\n";
123  }
124 }
125 #endif
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
Compute iterated dominance frontiers using a linear time algorithm.
Definition: AllocatorList.h:24
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:434
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
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:494
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds...
Definition: Compiler.h:449
void Destroy(AllocatorTy &Allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMap.h:203
bool hasName() const
Definition: Value.h:251
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
Module.h This file contains the declarations for the Module class.
ValueName * getValueName() const
Definition: Value.cpp:196
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:368
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:224
#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:207
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:353