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