blob: 1eb2498d8acdaa6d68afbc552a6bd0b55aee53dd [file] [log] [blame]
Reid Spencer25780d52006-01-10 09:51:48 +00001//===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2//
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 Carruth9fb823b2013-01-02 11:36:10 +000014#include "llvm/IR/ValueSymbolTable.h"
Chris Lattner36b4e8f2008-06-27 21:26:26 +000015#include "llvm/ADT/SmallString.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/GlobalValue.h"
17#include "llvm/IR/Type.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000018#include "llvm/Support/Casting.h"
19#include "llvm/Support/Compiler.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000020#include "llvm/Support/Debug.h"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000021#include "llvm/Support/raw_ostream.h"
Eugene Zelenko33d7b762016-08-23 17:14:32 +000022#include <cassert>
23#include <utility>
24
Reid Spencer25780d52006-01-10 09:51:48 +000025using namespace llvm;
26
Chandler Carruth1b9dde02014-04-22 02:02:50 +000027#define DEBUG_TYPE "valuesymtab"
28
Reid Spencer25780d52006-01-10 09:51:48 +000029// Class destructor
30ValueSymbolTable::~ValueSymbolTable() {
31#ifndef NDEBUG // Only do this in -g mode...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000032 for (const auto &VI : vmap)
David Greene73631b92010-01-05 01:30:06 +000033 dbgs() << "Value still in symbol table! Type = '"
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000034 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
35 << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000036 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000037#endif
38}
39
Rafael Espindolad1beb072015-11-22 00:16:24 +000040ValueName *ValueSymbolTable::makeUniqueName(Value *V,
41 SmallString<256> &UniqueName) {
42 unsigned BaseSize = UniqueName.size();
Eugene Zelenko33d7b762016-08-23 17:14:32 +000043 while (true) {
Rafael Espindolad1beb072015-11-22 00:16:24 +000044 // Trim any suffix off and append the next number.
45 UniqueName.resize(BaseSize);
46 raw_svector_ostream S(UniqueName);
47 if (isa<GlobalValue>(V))
48 S << ".";
49 S << ++LastUnique;
50
51 // Try insert the vmap entry with this suffix.
52 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
53 if (IterBool.second)
54 return &*IterBool.first;
55 }
56}
57
Reid Spencer25780d52006-01-10 09:51:48 +000058// Insert a value into the symbol table with the specified name...
59//
Chris Lattner32ab6432007-02-12 05:18:08 +000060void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000061 assert(V->hasName() && "Can't insert nameless Value into symbol table");
62
Chris Lattner770dadf2007-02-07 05:22:49 +000063 // Try inserting the name, assuming it won't conflict.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000064 if (vmap.insert(V->getValueName())) {
65 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
Chris Lattner770dadf2007-02-07 05:22:49 +000066 return;
67 }
68
69 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000070 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000071
72 // 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 +000073 V->getValueName()->Destroy();
74
Rafael Espindolad1beb072015-11-22 00:16:24 +000075 ValueName *VN = makeUniqueName(V, UniqueName);
76 V->setValueName(VN);
Chris Lattner32ab6432007-02-12 05:18:08 +000077}
Reid Spencer25780d52006-01-10 09:51:48 +000078
Chris Lattner32ab6432007-02-12 05:18:08 +000079void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000080 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000081 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000082 vmap.remove(V);
83}
84
85/// createValueName - This method attempts to create a value name and insert
86/// it into the symbol table with the specified name. If it conflicts, it
87/// auto-renames the name and returns that instead.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000088ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000089 // In the common case, the name is not already in the symbol table.
David Blaikie5106ce72014-11-19 05:49:42 +000090 auto IterBool = vmap.insert(std::make_pair(Name, V));
91 if (IterBool.second) {
David Greene73631b92010-01-05 01:30:06 +000092 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +000093 // << *V << "\n");
David Blaikie5106ce72014-11-19 05:49:42 +000094 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +000095 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000096
Chris Lattner32ab6432007-02-12 05:18:08 +000097 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000098 SmallString<256> UniqueName(Name.begin(), Name.end());
Rafael Espindolad1beb072015-11-22 00:16:24 +000099 return makeUniqueName(V, UniqueName);
Reid Spencer25780d52006-01-10 09:51:48 +0000100}
101
Matthias Braun8c209aa2017-01-28 02:02:38 +0000102#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
Reid Spencer25780d52006-01-10 09:51:48 +0000103// dump - print out the symbol table
104//
Yaron Kereneb2a2542016-01-29 20:50:44 +0000105LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000106 //dbgs() << "ValueSymbolTable:\n";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000107 for (const auto &I : *this) {
Matthias Braun8c209aa2017-01-28 02:02:38 +0000108 //dbgs() << " '" << I->getKeyData() << "' = ";
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000109 I.getValue()->dump();
Matthias Braun8c209aa2017-01-28 02:02:38 +0000110 //dbgs() << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000111 }
Reid Spencer25780d52006-01-10 09:51:48 +0000112}
Matthias Braun8c209aa2017-01-28 02:02:38 +0000113#endif