blob: e9e979a9a72bb300c637e84e5be58babf30b1325 [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.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000059 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
Craig Topperc6207612014-04-09 06:08:46 +000060 if (!NewName.getValue()) {
Chris Lattner32ab6432007-02-12 05:18:08 +000061 // Newly inserted name. Success!
62 NewName.setValue(V);
63 V->Name = &NewName;
David Greene73631b92010-01-05 01:30:06 +000064 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000065 return;
66 }
67 }
68}
Reid Spencer25780d52006-01-10 09:51:48 +000069
Chris Lattner32ab6432007-02-12 05:18:08 +000070void ValueSymbolTable::removeValueName(ValueName *V) {
David Greene73631b92010-01-05 01:30:06 +000071 //DEBUG(dbgs() << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000072 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000073 vmap.remove(V);
74}
75
76/// createValueName - This method attempts to create a value name and insert
77/// it into the symbol table with the specified name. If it conflicts, it
78/// auto-renames the name and returns that instead.
Daniel Dunbarad36e8a2009-11-06 10:58:06 +000079ValueName *ValueSymbolTable::createValueName(StringRef Name, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000080 // In the common case, the name is not already in the symbol table.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000081 ValueName &Entry = vmap.GetOrCreateValue(Name);
Craig Topperc6207612014-04-09 06:08:46 +000082 if (!Entry.getValue()) {
Chris Lattner32ab6432007-02-12 05:18:08 +000083 Entry.setValue(V);
David Greene73631b92010-01-05 01:30:06 +000084 //DEBUG(dbgs() << " Inserted value: " << Entry.getKeyData() << ": "
Chris Lattnerb377a7c2007-02-25 20:42:59 +000085 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000086 return &Entry;
87 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +000088
Chris Lattner32ab6432007-02-12 05:18:08 +000089 // Otherwise, there is a naming conflict. Rename this value.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000090 SmallString<256> UniqueName(Name.begin(), Name.end());
Chris Lattner36b4e8f2008-06-27 21:26:26 +000091
Chris Lattner32ab6432007-02-12 05:18:08 +000092 while (1) {
Daniel Dunbar948f82b2009-08-19 19:22:52 +000093 // Trim any suffix off and append the next number.
Daniel Dunbar84b5f6e2009-07-23 18:52:12 +000094 UniqueName.resize(Name.size());
Daniel Dunbar948f82b2009-08-19 19:22:52 +000095 raw_svector_ostream(UniqueName) << ++LastUnique;
Chris Lattner36b4e8f2008-06-27 21:26:26 +000096
Chris Lattner32ab6432007-02-12 05:18:08 +000097 // Try insert the vmap entry with this suffix.
Benjamin Kramerfeff7052010-03-31 16:04:26 +000098 ValueName &NewName = vmap.GetOrCreateValue(UniqueName);
Craig Topperc6207612014-04-09 06:08:46 +000099 if (!NewName.getValue()) {
Chris Lattner32ab6432007-02-12 05:18:08 +0000100 // Newly inserted name. Success!
101 NewName.setValue(V);
David Greene73631b92010-01-05 01:30:06 +0000102 //DEBUG(dbgs() << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000103 return &NewName;
104 }
105 }
Reid Spencer25780d52006-01-10 09:51:48 +0000106}
107
Reid Spencer25780d52006-01-10 09:51:48 +0000108
109// dump - print out the symbol table
110//
111void ValueSymbolTable::dump() const {
David Greene73631b92010-01-05 01:30:06 +0000112 //DEBUG(dbgs() << "ValueSymbolTable:\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000113 for (const_iterator I = begin(), E = end(); I != E; ++I) {
David Greene73631b92010-01-05 01:30:06 +0000114 //DEBUG(dbgs() << " '" << I->getKeyData() << "' = ");
Chris Lattner32ab6432007-02-12 05:18:08 +0000115 I->getValue()->dump();
David Greene73631b92010-01-05 01:30:06 +0000116 //DEBUG(dbgs() << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000117 }
Reid Spencer25780d52006-01-10 09:51:48 +0000118}