blob: 0c3946c8661ebe488af64a611ee5e157ed9f93e8 [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
Chris Lattner36b4e8f2008-06-27 21:26:26 +000014#include "llvm/ADT/SmallString.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000015#include "llvm/IR/GlobalValue.h"
16#include "llvm/IR/Type.h"
Eugene Zelenkodeaf6952017-02-17 00:00:09 +000017#include "llvm/IR/Value.h"
18#include "llvm/IR/ValueSymbolTable.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000019#include "llvm/Support/Casting.h"
20#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000021#include "llvm/Support/Debug.h"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000022#include "llvm/Support/raw_ostream.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000023#include <cassert>
24#include <utility>
25
Reid Spencer25780d52006-01-10 09:51:48 +000026using namespace llvm;
27
Chandler Carruth1b9dde02014-04-22 02:02:50 +000028#define DEBUG_TYPE "valuesymtab"
29
Reid Spencer25780d52006-01-10 09:51:48 +000030// Class destructor
31ValueSymbolTable::~ValueSymbolTable() {
32#ifndef NDEBUG // Only do this in -g mode...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000033 for (const auto &VI : vmap)
David Greene73631b92010-01-05 01:30:06 +000034 dbgs() << "Value still in symbol table! Type = '"
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000035 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
36 << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000037 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000038#endif
39}
40
Rafael Espindolad1beb072015-11-22 00:16:24 +000041ValueName *ValueSymbolTable::makeUniqueName(Value *V,
42 SmallString<256> &UniqueName) {
43 unsigned BaseSize = UniqueName.size();
Eugene Zelenko33d7b762016-08-23 17:14:32 +000044 while (true) {
Rafael Espindolad1beb072015-11-22 00:16:24 +000045 // Trim any suffix off and append the next number.
46 UniqueName.resize(BaseSize);
47 raw_svector_ostream S(UniqueName);
48 if (isa<GlobalValue>(V))
49 S << ".";
50 S << ++LastUnique;
51
52 // Try insert the vmap entry with this suffix.
53 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
54 if (IterBool.second)
55 return &*IterBool.first;
56 }
57}
58
Reid Spencer25780d52006-01-10 09:51:48 +000059// Insert a value into the symbol table with the specified name...
60//
Chris Lattner32ab6432007-02-12 05:18:08 +000061void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000062 assert(V->hasName() && "Can't insert nameless Value into symbol table");
63
Chris Lattner770dadf2007-02-07 05:22:49 +000064 // Try inserting the name, assuming it won't conflict.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000065 if (vmap.insert(V->getValueName())) {
66 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
Chris Lattner770dadf2007-02-07 05:22:49 +000067 return;
68 }
69
70 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000071 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000072
73 // 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 +000074 V->getValueName()->Destroy();
75
Rafael Espindolad1beb072015-11-22 00:16:24 +000076 ValueName *VN = makeUniqueName(V, UniqueName);
77 V->setValueName(VN);
Chris Lattner32ab6432007-02-12 05:18:08 +000078}
Reid Spencer25780d52006-01-10 09:51:48 +000079
Chris Lattner32ab6432007-02-12 05:18:08 +000080void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000081 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000082 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000083 vmap.remove(V);
84}
85
86/// createValueName - This method attempts to create a value name and insert
87/// it into the symbol table with the specified name. If it conflicts, it
88/// auto-renames the name and returns that instead.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000089ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000090 // In the common case, the name is not already in the symbol table.
David Blaikie5106ce72014-11-19 05:49:42 +000091 auto IterBool = vmap.insert(std::make_pair(Name, V));
92 if (IterBool.second) {
David Greene73631b92010-01-05 01:30:06 +000093 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +000094 // << *V << "\n");
David Blaikie5106ce72014-11-19 05:49:42 +000095 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +000096 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000097
Chris Lattner32ab6432007-02-12 05:18:08 +000098 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000099 SmallString<256> UniqueName(Name.begin(), Name.end());
Rafael Espindolad1beb072015-11-22 00:16:24 +0000100 return makeUniqueName(V, UniqueName);
Reid Spencer25780d52006-01-10 09:51:48 +0000101}
102
Matthias Braun8c209aa2017-01-28 02:02:38 +0000103#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Spencer25780d52006-01-10 09:51:48 +0000104// dump - print out the symbol table
105//
Yaron Kereneb2a2542016-01-29 20:50:44 +0000106LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000107 //dbgs() << "ValueSymbolTable:\n";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000108 for (const auto &I : *this) {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000109 //dbgs() << " '" << I->getKeyData() << "' = ";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000110 I.getValue()->dump();
Matthias Braun8c209aa2017-01-28 02:02:38 +0000111 //dbgs() << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000112 }
Reid Spencer25780d52006-01-10 09:51:48 +0000113}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000114#endif