blob: f527863e1b5d8fd563c9ad34ed857cb303109f59 [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"
18#include "llvm/ADT/StringExtras.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
33// getUniqueName - Given a base name, return a string that is either equal to
34// it (or derived from it) that does not already occur in the symbol table for
35// the specified type.
36//
37std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
38 std::string TryName = BaseName;
Reid Spencer25780d52006-01-10 09:51:48 +000039
40 // See if the name exists
Chris Lattner32ab6432007-02-12 05:18:08 +000041 while (vmap.find(&TryName[0], &TryName[TryName.size()]) != vmap.end())
42 // Loop until we find a free name in the symbol table.
43 TryName = BaseName + utostr(++LastUnique);
Reid Spencer25780d52006-01-10 09:51:48 +000044 return TryName;
45}
46
47
48// lookup a value - Returns null on failure...
49//
50Value *ValueSymbolTable::lookup(const std::string &Name) const {
Chris Lattner32ab6432007-02-12 05:18:08 +000051 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
Reid Spencer25780d52006-01-10 09:51:48 +000052 if (VI != vmap.end()) // We found the symbol
Chris Lattner32ab6432007-02-12 05:18:08 +000053 return VI->getValue();
Reid Spencer25780d52006-01-10 09:51:48 +000054 return 0;
55}
56
Chris Lattnere43649f2008-06-27 21:09:10 +000057Value *ValueSymbolTable::lookup(const char *NameBegin,
58 const char *NameEnd) const {
59 const_iterator VI = vmap.find(NameBegin, NameEnd);
60 if (VI != vmap.end()) // We found the symbol
61 return VI->getValue();
62 return 0;
63}
64
Reid Spencer25780d52006-01-10 09:51:48 +000065// Insert a value into the symbol table with the specified name...
66//
Chris Lattner32ab6432007-02-12 05:18:08 +000067void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000068 assert(V->hasName() && "Can't insert nameless Value into symbol table");
69
Chris Lattner770dadf2007-02-07 05:22:49 +000070 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000071 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000072 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000073 return;
74 }
75
Chris Lattner32ab6432007-02-12 05:18:08 +000076 // FIXME: this could be much more efficient.
77
Chris Lattner770dadf2007-02-07 05:22:49 +000078 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner71a60ba2007-02-07 05:52:51 +000079 std::string UniqueName = V->getName();
Chris Lattner32ab6432007-02-12 05:18:08 +000080
81 V->Name->Destroy();
82
Chris Lattner71a60ba2007-02-07 05:52:51 +000083 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000084 while (1) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000085 // Trim any suffix off.
86 UniqueName.resize(BaseSize);
87 UniqueName += utostr(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000088 // Try insert the vmap entry with this suffix.
Chris Lattner32ab6432007-02-12 05:18:08 +000089 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
90 &UniqueName[UniqueName.size()]);
91 if (NewName.getValue() == 0) {
92 // Newly inserted name. Success!
93 NewName.setValue(V);
94 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000095 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000096 return;
97 }
98 }
99}
Reid Spencer25780d52006-01-10 09:51:48 +0000100
Chris Lattner32ab6432007-02-12 05:18:08 +0000101void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000102 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000103 // Remove the value from the plane.
104 vmap.remove(V);
105}
106
107/// createValueName - This method attempts to create a value name and insert
108/// it into the symbol table with the specified name. If it conflicts, it
109/// auto-renames the name and returns that instead.
110ValueName *ValueSymbolTable::createValueName(const char *NameStart,
111 unsigned NameLen, Value *V) {
112 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
113 if (Entry.getValue() == 0) {
114 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000115 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
116 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000117 return &Entry;
118 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +0000119
Chris Lattner32ab6432007-02-12 05:18:08 +0000120 // FIXME: this could be much more efficient.
121
122 // Otherwise, there is a naming conflict. Rename this value.
123 std::string UniqueName(NameStart, NameStart+NameLen);
124 while (1) {
125 // Trim any suffix off.
126 UniqueName.resize(NameLen);
127 UniqueName += utostr(++LastUnique);
128 // Try insert the vmap entry with this suffix.
129 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
130 &UniqueName[UniqueName.size()]);
131 if (NewName.getValue() == 0) {
132 // Newly inserted name. Success!
133 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000134 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000135 return &NewName;
136 }
137 }
Reid Spencer25780d52006-01-10 09:51:48 +0000138}
139
Reid Spencer25780d52006-01-10 09:51:48 +0000140
141// dump - print out the symbol table
142//
143void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000144 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000145 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000146 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000147 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000148 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000149 }
Reid Spencer25780d52006-01-10 09:51:48 +0000150}