blob: f6f1dd984e9f91034ab8ec11aba56b6d2717a2a9 [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"
Bill Wendling6a462f12006-11-17 08:03:48 +000018#include "llvm/Support/Debug.h"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000019#include "llvm/Support/raw_ostream.h"
Reid Spencer25780d52006-01-10 09:51:48 +000020using namespace llvm;
21
Chandler Carruth1b9dde02014-04-22 02:02:50 +000022#define DEBUG_TYPE "valuesymtab"
23
Reid Spencer25780d52006-01-10 09:51:48 +000024// Class destructor
25ValueSymbolTable::~ValueSymbolTable() {
26#ifndef NDEBUG // Only do this in -g mode...
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000027 for (const auto &VI : vmap)
David Greene73631b92010-01-05 01:30:06 +000028 dbgs() << "Value still in symbol table! Type = '"
Benjamin Krameraf28e7d2016-06-26 14:10:56 +000029 << *VI.getValue()->getType() << "' Name = '" << VI.getKeyData()
30 << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000031 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000032#endif
33}
34
Rafael Espindolad1beb072015-11-22 00:16:24 +000035ValueName *ValueSymbolTable::makeUniqueName(Value *V,
36 SmallString<256> &UniqueName) {
37 unsigned BaseSize = UniqueName.size();
38 while (1) {
39 // Trim any suffix off and append the next number.
40 UniqueName.resize(BaseSize);
41 raw_svector_ostream S(UniqueName);
42 if (isa<GlobalValue>(V))
43 S << ".";
44 S << ++LastUnique;
45
46 // Try insert the vmap entry with this suffix.
47 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
48 if (IterBool.second)
49 return &*IterBool.first;
50 }
51}
52
Reid Spencer25780d52006-01-10 09:51:48 +000053// Insert a value into the symbol table with the specified name...
54//
Chris Lattner32ab6432007-02-12 05:18:08 +000055void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000056 assert(V->hasName() && "Can't insert nameless Value into symbol table");
57
Chris Lattner770dadf2007-02-07 05:22:49 +000058 // Try inserting the name, assuming it won't conflict.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +000059 if (vmap.insert(V->getValueName())) {
60 //DEBUG(dbgs() << " Inserted value: " << V->getValueName() << ": " << *V << "\n");
Chris Lattner770dadf2007-02-07 05:22:49 +000061 return;
62 }
63
64 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000065 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000066
67 // 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 +000068 V->getValueName()->Destroy();
69
Rafael Espindolad1beb072015-11-22 00:16:24 +000070 ValueName *VN = makeUniqueName(V, UniqueName);
71 V->setValueName(VN);
Chris Lattner32ab6432007-02-12 05:18:08 +000072}
Reid Spencer25780d52006-01-10 09:51:48 +000073
Chris Lattner32ab6432007-02-12 05:18:08 +000074void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000075 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000076 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000077 vmap.remove(V);
78}
79
80/// createValueName - This method attempts to create a value name and insert
81/// it into the symbol table with the specified name. If it conflicts, it
82/// auto-renames the name and returns that instead.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000083ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000084 // In the common case, the name is not already in the symbol table.
David Blaikie5106ce72014-11-19 05:49:42 +000085 auto IterBool = vmap.insert(std::make_pair(Name, V));
86 if (IterBool.second) {
David Greene73631b92010-01-05 01:30:06 +000087 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +000088 // << *V << "\n");
David Blaikie5106ce72014-11-19 05:49:42 +000089 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +000090 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000091
Chris Lattner32ab6432007-02-12 05:18:08 +000092 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000093 SmallString<256> UniqueName(Name.begin(), Name.end());
Rafael Espindolad1beb072015-11-22 00:16:24 +000094 return makeUniqueName(V, UniqueName);
Reid Spencer25780d52006-01-10 09:51:48 +000095}
96
Reid Spencer25780d52006-01-10 09:51:48 +000097
98// dump - print out the symbol table
99//
Yaron Kereneb2a2542016-01-29 20:50:44 +0000100LLVM_DUMP_METHOD void ValueSymbolTable::dump() const {
David Greene73631b92010-01-05 01:30:06 +0000101 //DEBUG(dbgs() << "ValueSymbolTable:\n");
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000102 for (const auto &I : *this) {
David Greene73631b92010-01-05 01:30:06 +0000103 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
Benjamin Krameraf28e7d2016-06-26 14:10:56 +0000104 I.getValue()->dump();
David Greene73631b92010-01-05 01:30:06 +0000105 //DEBUG(dbgs() << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000106 }
Reid Spencer25780d52006-01-10 09:51:48 +0000107}