blob: 0da1990c3a3f8e4b9c2eef1c9ada42fa37b3f215 [file] [log] [blame]
Eugene Zelenkodeaf6952017-02-17 00:00:09 +00001//===- ValueSymbolTable.cpp - Implement the ValueSymbolTable class --------===//
Reid Spencer25780d52006-01-10 09:51:48 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Spencer25780d52006-01-10 09:51:48 +00007//
8//===----------------------------------------------------------------------===//
9//
Chandler Carruthef860a22013-01-02 09:10:48 +000010// This file implements the ValueSymbolTable class for the IR library.
Reid Spencer25780d52006-01-10 09:51:48 +000011//
12//===----------------------------------------------------------------------===//
13
Chandler Carruth6bda14b2017-06-06 11:49:48 +000014#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner36b4e8f2008-06-27 21:26:26 +000015#include "llvm/ADT/SmallString.h"
Jonas Hahnfeld5db24d72017-12-04 14:19:33 +000016#include "llvm/ADT/Triple.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000017#include "llvm/IR/GlobalValue.h"
Jonas Hahnfeld5db24d72017-12-04 14:19:33 +000018#include "llvm/IR/Module.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000020#include "llvm/IR/Value.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000021#include "llvm/Support/Casting.h"
22#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000023#include "llvm/Support/Debug.h"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000024#include "llvm/Support/raw_ostream.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000025#include <cassert>
26#include <utility>
27
Reid Spencer25780d52006-01-10 09:51:48 +000028using namespace llvm;
29
Chandler Carruth1b9dde02014-04-22 02:02:50 +000030#define DEBUG_TYPE "valuesymtab"
31
Reid Spencer25780d52006-01-10 09:51:48 +000032// Class destructor
33ValueSymbolTable::~ValueSymbolTable() {
34#ifndef NDEBUG // Only do this in -g mode...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000035 for (const auto &VI : vmap)
David Greene73631b92010-01-05 01:30:06 +000036 dbgs() << "Value still in symbol table! Type = '"
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000037 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
38 << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000039 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000040#endif
41}
42
Rafael Espindolad1beb072015-11-22 00:16:24 +000043ValueName *ValueSymbolTable::makeUniqueName(Value *V,
44 SmallString<256> &UniqueName) {
45 unsigned BaseSize = UniqueName.size();
Eugene Zelenko33d7b762016-08-23 17:14:32 +000046 while (true) {
Rafael Espindolad1beb072015-11-22 00:16:24 +000047 // Trim any suffix off and append the next number.
48 UniqueName.resize(BaseSize);
49 raw_svector_ostream S(UniqueName);
Jonas Hahnfeld5db24d72017-12-04 14:19:33 +000050 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 }
Rafael Espindolad1beb072015-11-22 00:16:24 +000061 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
Reid Spencer25780d52006-01-10 09:51:48 +000070// Insert a value into the symbol table with the specified name...
71//
Chris Lattner32ab6432007-02-12 05:18:08 +000072void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000073 assert(V->hasName() && "Can't insert nameless Value into symbol table");
74
Chris Lattner770dadf2007-02-07 05:22:49 +000075 // Try inserting the name, assuming it won't conflict.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000076 if (vmap.insert(V->getValueName())) {
77 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
Chris Lattner770dadf2007-02-07 05:22:49 +000078 return;
79 }
80
81 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000082 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000083
84 // The name is too already used, just free it so we can allocate a new name.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000085 V->getValueName()->Destroy();
86
Rafael Espindolad1beb072015-11-22 00:16:24 +000087 ValueName *VN = makeUniqueName(V, UniqueName);
88 V->setValueName(VN);
Chris Lattner32ab6432007-02-12 05:18:08 +000089}
Reid Spencer25780d52006-01-10 09:51:48 +000090
Chris Lattner32ab6432007-02-12 05:18:08 +000091void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000092 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000093 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000094 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.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +0000100ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +0000101 // In the common case, the name is not already in the symbol table.
David Blaikie5106ce72014-11-19 05:49:42 +0000102 auto IterBool = vmap.insert(std::make_pair(Name, V));
103 if (IterBool.second) {
David Greene73631b92010-01-05 01:30:06 +0000104 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000105 // << *V << "\n");
David Blaikie5106ce72014-11-19 05:49:42 +0000106 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +0000107 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +0000108
Chris Lattner32ab6432007-02-12 05:18:08 +0000109 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +0000110 SmallString<256> UniqueName(Name.begin(), Name.end());
Rafael Espindolad1beb072015-11-22 00:16:24 +0000111 return makeUniqueName(V, UniqueName);
Reid Spencer25780d52006-01-10 09:51:48 +0000112}
113
Aaron Ballman615eb472017-10-15 14:32:27 +0000114#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Spencer25780d52006-01-10 09:51:48 +0000115// dump - print out the symbol table
116//
Yaron Kereneb2a2542016-01-29 20:50:44 +0000117LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000118 //dbgs() << "ValueSymbolTable:\n";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000119 for (const auto &I : *this) {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000120 //dbgs() << " '" << I->getKeyData() << "' = ";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000121 I.getValue()->dump();
Matthias Braun8c209aa2017-01-28 02:02:38 +0000122 //dbgs() << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000123 }
Reid Spencer25780d52006-01-10 09:51:48 +0000124}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000125#endif