blob: f4bea560404383be2b7f232e9c48e27ced15a4aa [file] [log] [blame]
Eugene Zelenkob1df7872017-02-17 00:00:09 +00001//===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
Reid Spencereb7116b2006-01-10 09:51:48 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencereb7116b2006-01-10 09:51:48 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruthc2c50cd2013-01-02 09:10:48 +000010// This file implements the ValueSymbolTable class for the IR library.
Reid Spencereb7116b2006-01-10 09:51:48 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruthe3e43d92017-06-06 11:49:48 +000014#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner97388462008-06-27 21:26:26 +000015#include "llvm/ADT/SmallString.h"
Jonas Hahnfeldba19c682017-12-04 14:19:33 +000016#include "llvm/ADT/Triple.h"
Nico Weber0f38c602018-04-30 14:59:11 +000017#include "llvm/Config/llvm-config.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/GlobalValue.h"
Jonas Hahnfeldba19c682017-12-04 14:19:33 +000019#include "llvm/IR/Module.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000020#include "llvm/IR/Type.h"
Eugene Zelenkob1df7872017-02-17 00:00:09 +000021#include "llvm/IR/Value.h"
Eugene Zelenko9feaa972016-08-23 17:14:32 +000022#include "llvm/Support/Casting.h"
23#include "llvm/Support/Compiler.h"
Bill Wendling2e3def12006-11-17 08:03:48 +000024#include "llvm/Support/Debug.h"
Daniel Dunbarb95c2fd2009-07-24 10:05:20 +000025#include "llvm/Support/raw_ostream.h"
Eugene Zelenko9feaa972016-08-23 17:14:32 +000026#include <cassert>
27#include <utility>
28
Reid Spencereb7116b2006-01-10 09:51:48 +000029using namespace llvm;
30
Chandler Carruth8677f2f2014-04-22 02:02:50 +000031#define DEBUG_TYPE "valuesymtab"
32
Reid Spencereb7116b2006-01-10 09:51:48 +000033// Class destructor
34ValueSymbolTable::~ValueSymbolTable() {
35#ifndef NDEBUG // Only do this in -g mode...
Benjamin Kramere96e21f2016-06-26 14:10:56 +000036 for (const auto &VI : vmap)
David Greeneefc25812010-01-05 01:30:06 +000037 dbgs() << "Value still in symbol table! Type = '"
Benjamin Kramere96e21f2016-06-26 14:10:56 +000038 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
39 << "'\n";
Chris Lattner92f5b6a2007-02-07 06:28:48 +000040 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencereb7116b2006-01-10 09:51:48 +000041#endif
42}
43
Rafael Espindolaa2197f82015-11-22 00:16:24 +000044ValueName *ValueSymbolTable::makeUniqueName(Value *V,
45 SmallString<256> &UniqueName) {
46 unsigned BaseSize = UniqueName.size();
Eugene Zelenko9feaa972016-08-23 17:14:32 +000047 while (true) {
Rafael Espindolaa2197f82015-11-22 00:16:24 +000048 // Trim any suffix off and append the next number.
49 UniqueName.resize(BaseSize);
50 raw_svector_ostream S(UniqueName);
Jonas Hahnfeldba19c682017-12-04 14:19:33 +000051 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 }
Rafael Espindolaa2197f82015-11-22 00:16:24 +000062 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
Reid Spencereb7116b2006-01-10 09:51:48 +000071// Insert a value into the symbol table with the specified name...
72//
Chris Lattnerdec628e2007-02-12 05:18:08 +000073void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencereb7116b2006-01-10 09:51:48 +000074 assert(V->hasName() && "Can't insert nameless Value into symbol table");
75
Chris Lattnerf5111552007-02-07 05:22:49 +000076 // Try inserting the name, assuming it won't conflict.
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +000077 if (vmap.insert(V->getValueName())) {
Nicola Zaghen0818e782018-05-14 12:53:11 +000078 // LLVM_DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " <<
79 // *V << "\n");
Chris Lattnerf5111552007-02-07 05:22:49 +000080 return;
81 }
Fangrui Songaf7b1832018-07-30 19:41:25 +000082
Chris Lattnerf5111552007-02-07 05:22:49 +000083 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar29c14202009-08-19 19:22:52 +000084 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner97388462008-06-27 21:26:26 +000085
86 // The name is too already used, just free it so we can allocate a new name.
Duncan P. N. Exon Smithdad20b22014-12-09 18:38:53 +000087 V->getValueName()->Destroy();
88
Rafael Espindolaa2197f82015-11-22 00:16:24 +000089 ValueName *VN = makeUniqueName(V, UniqueName);
90 V->setValueName(VN);
Chris Lattnerdec628e2007-02-12 05:18:08 +000091}
Reid Spencereb7116b2006-01-10 09:51:48 +000092
Chris Lattnerdec628e2007-02-12 05:18:08 +000093void ValueSymbolTable::removeValueName(ValueName *V) {
Nicola Zaghen0818e782018-05-14 12:53:11 +000094 // LLVM_DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner97388462008-06-27 21:26:26 +000095 // Remove the value from the symbol table.
Chris Lattnerdec628e2007-02-12 05:18:08 +000096 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.
Daniel Dunbar2928c832009-11-06 10:58:06 +0000102ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner97388462008-06-27 21:26:26 +0000103 // In the common case, the name is not already in the symbol table.
David Blaikie1d4f28c2014-11-19 05:49:42 +0000104 auto IterBool = vmap.insert(std::make_pair(Name, V));
105 if (IterBool.second) {
Nicola Zaghen0818e782018-05-14 12:53:11 +0000106 // LLVM_DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattner3f3098c2007-02-25 20:42:59 +0000107 // << *V << "\n");
David Blaikie1d4f28c2014-11-19 05:49:42 +0000108 return &*IterBool.first;
Chris Lattnerdec628e2007-02-12 05:18:08 +0000109 }
Fangrui Songaf7b1832018-07-30 19:41:25 +0000110
Chris Lattnerdec628e2007-02-12 05:18:08 +0000111 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramer4ef68222010-03-31 16:04:26 +0000112 SmallString<256> UniqueName(Name.begin(), Name.end());
Rafael Espindolaa2197f82015-11-22 00:16:24 +0000113 return makeUniqueName(V, UniqueName);
Reid Spencereb7116b2006-01-10 09:51:48 +0000114}
115
Aaron Ballman1d03d382017-10-15 14:32:27 +0000116#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Spencereb7116b2006-01-10 09:51:48 +0000117// dump - print out the symbol table
118//
Yaron Keren55307982016-01-29 20:50:44 +0000119LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
Matthias Braun88d20752017-01-28 02:02:38 +0000120 //dbgs() << "ValueSymbolTable:\n";
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000121 for (const auto &I : *this) {
Matthias Braun88d20752017-01-28 02:02:38 +0000122 //dbgs() << " '" << I->getKeyData() << "' = ";
Benjamin Kramere96e21f2016-06-26 14:10:56 +0000123 I.getValue()->dump();
Matthias Braun88d20752017-01-28 02:02:38 +0000124 //dbgs() << "\n";
Chris Lattnerdec628e2007-02-12 05:18:08 +0000125 }
Reid Spencereb7116b2006-01-10 09:51:48 +0000126}
Matthias Braun88d20752017-01-28 02:02:38 +0000127#endif