blob: 2b23f6dd15b6b79ddda33ed7e00315c969059bd6 [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...
Reid Spencer25780d52006-01-10 09:51:48 +000027 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
David Greene73631b92010-01-05 01:30:06 +000028 dbgs() << "Value still in symbol table! Type = '"
Chris Lattner0f214eb2011-06-18 21:18:23 +000029 << *VI->getValue()->getType() << "' Name = '"
Daniel Dunbarb99eac82009-07-24 10:05:20 +000030 << VI->getKeyData() << "'\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
Reid Spencer25780d52006-01-10 09:51:48 +000035// Insert a value into the symbol table with the specified name...
36//
Chris Lattner32ab6432007-02-12 05:18:08 +000037void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000038 assert(V->hasName() && "Can't insert nameless Value into symbol table");
39
Chris Lattner770dadf2007-02-07 05:22:49 +000040 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000041 if (vmap.insert(V->Name)) {
David Greene73631b92010-01-05 01:30:06 +000042 //DEBUG(dbgs() << " Inserted value: " << V->Name << ": " << *V << "\n");
Chris Lattner770dadf2007-02-07 05:22:49 +000043 return;
44 }
45
46 // Otherwise, there is a naming conflict. Rename this value.
Daniel Dunbar948f82b2009-08-19 19:22:52 +000047 SmallString<256> UniqueName(V->getName().begin(), V->getName().end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000048
49 // The name is too already used, just free it so we can allocate a new name.
Chris Lattner32ab6432007-02-12 05:18:08 +000050 V->Name->Destroy();
51
Chris Lattner71a60ba2007-02-07 05:52:51 +000052 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000053 while (1) {
Daniel Dunbar948f82b2009-08-19 19:22:52 +000054 // Trim any suffix off and append the next number.
Chris Lattner71a60ba2007-02-07 05:52:51 +000055 UniqueName.resize(BaseSize);
Daniel Dunbar948f82b2009-08-19 19:22:52 +000056 raw_svector_ostream(UniqueName) << ++LastUnique;
57
Chris Lattnerd17dbe92007-02-07 06:25:36 +000058 // Try insert the vmap entry with this suffix.
David Blaikie5106ce72014-11-19 05:49:42 +000059 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
60 if (IterBool.second) {
Chris Lattner32ab6432007-02-12 05:18:08 +000061 // Newly inserted name. Success!
David Blaikie5106ce72014-11-19 05:49:42 +000062 V->Name = &*IterBool.first;
David Greene73631b92010-01-05 01:30:06 +000063 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000064 return;
65 }
66 }
67}
Reid Spencer25780d52006-01-10 09:51:48 +000068
Chris Lattner32ab6432007-02-12 05:18:08 +000069void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000070 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000071 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000072 vmap.remove(V);
73}
74
75/// createValueName - This method attempts to create a value name and insert
76/// it into the symbol table with the specified name. If it conflicts, it
77/// auto-renames the name and returns that instead.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000078ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000079 // In the common case, the name is not already in the symbol table.
David Blaikie5106ce72014-11-19 05:49:42 +000080 auto IterBool = vmap.insert(std::make_pair(Name, V));
81 if (IterBool.second) {
David Greene73631b92010-01-05 01:30:06 +000082 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +000083 // << *V << "\n");
David Blaikie5106ce72014-11-19 05:49:42 +000084 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +000085 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000086
Chris Lattner32ab6432007-02-12 05:18:08 +000087 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000088 SmallString<256> UniqueName(Name.begin(), Name.end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000089
Chris Lattner32ab6432007-02-12 05:18:08 +000090 while (1) {
Daniel Dunbar948f82b2009-08-19 19:22:52 +000091 // Trim any suffix off and append the next number.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000092 UniqueName.resize(Name.size());
Daniel Dunbar948f82b2009-08-19 19:22:52 +000093 raw_svector_ostream(UniqueName) << ++LastUnique;
Chris Lattner36b4e8f2008-06-27 21:26:26 +000094
Chris Lattner32ab6432007-02-12 05:18:08 +000095 // Try insert the vmap entry with this suffix.
David Blaikie5106ce72014-11-19 05:49:42 +000096 auto IterBool = vmap.insert(std::make_pair(UniqueName, V));
97 if (IterBool.second) {
98 // DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V <<
99 // "\n");
100 return &*IterBool.first;
Chris Lattner32ab6432007-02-12 05:18:08 +0000101 }
102 }
Reid Spencer25780d52006-01-10 09:51:48 +0000103}
104
Reid Spencer25780d52006-01-10 09:51:48 +0000105
106// dump - print out the symbol table
107//
108void ValueSymbolTable::dump() const {
David Greene73631b92010-01-05 01:30:06 +0000109 //DEBUG(dbgs() << "ValueSymbolTable:\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000110 for (const_iterator I = begin(), E = end(); I != E; ++I) {
David Greene73631b92010-01-05 01:30:06 +0000111 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
Chris Lattner32ab6432007-02-12 05:18:08 +0000112 I->getValue()->dump();
David Greene73631b92010-01-05 01:30:06 +0000113 //DEBUG(dbgs() << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000114 }
Reid Spencer25780d52006-01-10 09:51:48 +0000115}