blob: 8d3514b47519133816adeb65201f2a4db88382c2 [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//
10// This file implements the ValueSymbolTable class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000014#define DEBUG_TYPE "valuesymtab"
Reid Spencer25780d52006-01-10 09:51:48 +000015#include "llvm/GlobalValue.h"
16#include "llvm/Type.h"
17#include "llvm/ValueSymbolTable.h"
Chris Lattner36b4e8f2008-06-27 21:26:26 +000018#include "llvm/ADT/SmallString.h"
Bill Wendling6a462f12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencer25780d52006-01-10 09:51:48 +000020using namespace llvm;
21
Reid Spencer25780d52006-01-10 09:51:48 +000022// Class destructor
23ValueSymbolTable::~ValueSymbolTable() {
24#ifndef NDEBUG // Only do this in -g mode...
Reid Spencer25780d52006-01-10 09:51:48 +000025 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
Chris Lattnerb377a7c2007-02-25 20:42:59 +000026 cerr << "Value still in symbol table! Type = '"
27 << VI->getValue()->getType()->getDescription() << "' Name = '"
28 << VI->getKeyData() << "'\n";
Chris Lattner86fc0812007-02-07 06:28:48 +000029 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencer25780d52006-01-10 09:51:48 +000030#endif
31}
32
Reid Spencer25780d52006-01-10 09:51:48 +000033// lookup a value - Returns null on failure...
34//
35Value *ValueSymbolTable::lookup(const std::string &Name) const {
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000036 const_iterator VI = vmap.find(Name);
Reid Spencer25780d52006-01-10 09:51:48 +000037 if (VI != vmap.end()) // We found the symbol
Chris Lattner32ab6432007-02-12 05:18:08 +000038 return VI->getValue();
Reid Spencer25780d52006-01-10 09:51:48 +000039 return 0;
40}
41
Chris Lattnere43649f2008-06-27 21:09:10 +000042Value *ValueSymbolTable::lookup(const char *NameBegin,
43 const char *NameEnd) const {
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000044 // FIXME: ValueSymbolTable should move to a StringRef based API.
45 const_iterator VI = vmap.find(StringRef(NameBegin, NameEnd - NameBegin));
Chris Lattnere43649f2008-06-27 21:09:10 +000046 if (VI != vmap.end()) // We found the symbol
47 return VI->getValue();
48 return 0;
49}
50
Reid Spencer25780d52006-01-10 09:51:48 +000051// Insert a value into the symbol table with the specified name...
52//
Chris Lattner32ab6432007-02-12 05:18:08 +000053void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000054 assert(V->hasName() && "Can't insert nameless Value into symbol table");
55
Chris Lattner770dadf2007-02-07 05:22:49 +000056 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000057 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000058 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000059 return;
60 }
61
62 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner36b4e8f2008-06-27 21:26:26 +000063 SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd());
64
65 // The name is too already used, just free it so we can allocate a new name.
Chris Lattner32ab6432007-02-12 05:18:08 +000066 V->Name->Destroy();
67
Chris Lattner71a60ba2007-02-07 05:52:51 +000068 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000069 while (1) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000070 // Trim any suffix off.
71 UniqueName.resize(BaseSize);
Chris Lattner36b4e8f2008-06-27 21:26:26 +000072 UniqueName.append_uint_32(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000073 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +000074 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000075 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
76 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +000077 if (NewName.getValue() == 0) {
78 // Newly inserted name. Success!
79 NewName.setValue(V);
80 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000081 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000082 return;
83 }
84 }
85}
Reid Spencer25780d52006-01-10 09:51:48 +000086
Chris Lattner32ab6432007-02-12 05:18:08 +000087void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000088 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000089 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000090 vmap.remove(V);
91}
92
93/// createValueName - This method attempts to create a value name and insert
94/// it into the symbol table with the specified name. If it conflicts, it
95/// auto-renames the name and returns that instead.
96ValueName *ValueSymbolTable::createValueName(const char *NameStart,
97 unsigned NameLen, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000098 // In the common case, the name is not already in the symbol table.
Daniel Dunbar5bf72e22009-07-23 18:17:34 +000099 ValueName &Entry = vmap.GetOrCreateValue(StringRef(NameStart, NameLen));
Chris Lattner32ab6432007-02-12 05:18:08 +0000100 if (Entry.getValue() == 0) {
101 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000102 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
103 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000104 return &Entry;
105 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +0000106
Chris Lattner32ab6432007-02-12 05:18:08 +0000107 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner36b4e8f2008-06-27 21:26:26 +0000108 SmallString<128> UniqueName(NameStart, NameStart+NameLen);
109
Chris Lattner32ab6432007-02-12 05:18:08 +0000110 while (1) {
111 // Trim any suffix off.
112 UniqueName.resize(NameLen);
Chris Lattner36b4e8f2008-06-27 21:26:26 +0000113 UniqueName.append_uint_32(++LastUnique);
114
Chris Lattner32ab6432007-02-12 05:18:08 +0000115 // Try insert the vmap entry with this suffix.
Jay Foad7d0479f2009-05-21 09:52:38 +0000116 ValueName &NewName =
Daniel Dunbar5bf72e22009-07-23 18:17:34 +0000117 vmap.GetOrCreateValue(StringRef(UniqueName.data(),
118 UniqueName.size()));
Chris Lattner32ab6432007-02-12 05:18:08 +0000119 if (NewName.getValue() == 0) {
120 // Newly inserted name. Success!
121 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000122 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000123 return &NewName;
124 }
125 }
Reid Spencer25780d52006-01-10 09:51:48 +0000126}
127
Reid Spencer25780d52006-01-10 09:51:48 +0000128
129// dump - print out the symbol table
130//
131void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000132 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000133 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000134 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000135 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000136 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000137 }
Reid Spencer25780d52006-01-10 09:51:48 +0000138}