LLVM  13.0.0git
ValueSymbolTable.cpp
Go to the documentation of this file.
1 //===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the ValueSymbolTable class for the IR library.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Triple.h"
16 #include "llvm/Config/llvm-config.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  // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
78  // *V << "\n");
79  return;
80  }
81 
82  // Otherwise, there is a naming conflict. Rename this value.
83  SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
84 
85  // The name is too already used, just free it so we can allocate a new name.
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  if (MaxNameSize > -1 && Name.size() > (unsigned)MaxNameSize)
104  Name = Name.substr(0, std::max(1u, (unsigned)MaxNameSize));
105 
106  // In the common case, the name is not already in the symbol table.
107  auto IterBool = vmap.insert(std::make_pair(Name, V));
108  if (IterBool.second) {
109  // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
110  // << *V << "\n");
111  return &*IterBool.first;
112  }
113 
114  // Otherwise, there is a naming conflict. Rename this value.
115  SmallString<256> UniqueName(Name.begin(), Name.end());
116  return makeUniqueName(V, UniqueName);
117 }
118 
119 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
120 // dump - print out the symbol table
121 //
123  // dbgs() << "ValueSymbolTable:\n";
124  for (const auto &I : *this) {
125  // dbgs() << " '" << I->getKeyData() << "' = ";
126  I.getValue()->dump();
127  // dbgs() << "\n";
128  }
129 }
130 #endif
LLVM_DUMP_METHOD
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition: Compiler.h:499
llvm
Definition: AllocatorList.h:23
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::StringMapEntry
StringMapEntry - This is used to represent one value that is inserted into a StringMap.
Definition: StringMapEntry.h:98
llvm::StringMap::remove
void remove(MapEntryTy *KeyValue)
remove - Remove the specified key/value pair from the map, but do not erase it.
Definition: StringMap.h:353
llvm::Value::hasName
bool hasName() const
Definition: Value.h:262
llvm::Triple
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:45
Module.h
llvm::Triple::isNVPTX
bool isNVPTX() const
Tests whether the target is NVPTX (32- or 64-bit).
Definition: Triple.h:702
llvm::dbgs
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:132
GlobalValue.h
SmallString.h
llvm::StringMap::insert
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition: StringMap.h:276
llvm::SmallVectorImpl::resize
void resize(size_type N)
Definition: SmallVector.h:605
Type.h
llvm::SmallString< 256 >
ValueSymbolTable.h
VI
@ VI
Definition: SIInstrInfo.cpp:7494
llvm::StringRef::end
iterator end() const
Definition: StringRef.h:130
I
#define I(x, y, z)
Definition: MD5.cpp:59
llvm::Value::getValueName
ValueName * getValueName() const
Definition: Value.cpp:266
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::Value::setValueName
void setValueName(ValueName *VN)
Definition: Value.cpp:277
llvm::StringMapEntry::Destroy
void Destroy(AllocatorTy &allocator)
Destroy - Destroy this StringMapEntry, releasing memory back to the specified allocator.
Definition: StringMapEntry.h:136
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:67
Triple.h
llvm::ValueSymbolTable::~ValueSymbolTable
~ValueSymbolTable()
Definition: ValueSymbolTable.cpp:33
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
llvm::ValueSymbolTable::dump
void dump() const
This function can be used from the debugger to display the content of the symbol table while debuggin...
Definition: ValueSymbolTable.cpp:122
Compiler.h
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::Value::getName
StringRef getName() const
Return a constant reference to the value's name.
Definition: Value.cpp:294
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:52
llvm::MallocAllocator
Definition: AllocatorBase.h:80
Casting.h
Allocator
Basic Register Allocator
Definition: RegAllocBasic.cpp:146
llvm::max
Align max(MaybeAlign Lhs, Align Rhs)
Definition: Alignment.h:340
llvm::raw_svector_ostream
A raw_ostream that writes to an SmallVector or SmallString.
Definition: raw_ostream.h:647
raw_ostream.h
llvm::StringRef::begin
iterator begin() const
Definition: StringRef.h:128
Value.h
llvm::Value
LLVM Value Representation.
Definition: Value.h:75
Debug.h