blob: 3a0c54ed723f630357d115c8ffcca1153433a92b [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 {
Chris Lattner32ab6432007-02-12 05:18:08 +000036 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
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 {
44 const_iterator VI = vmap.find(NameBegin, NameEnd);
45 if (VI != vmap.end()) // We found the symbol
46 return VI->getValue();
47 return 0;
48}
49
Reid Spencer25780d52006-01-10 09:51:48 +000050// Insert a value into the symbol table with the specified name...
51//
Chris Lattner32ab6432007-02-12 05:18:08 +000052void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000053 assert(V->hasName() && "Can't insert nameless Value into symbol table");
54
Chris Lattner770dadf2007-02-07 05:22:49 +000055 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000056 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000057 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000058 return;
59 }
60
61 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner36b4e8f2008-06-27 21:26:26 +000062 SmallString<128> UniqueName(V->getNameStart(), V->getNameEnd());
63
64 // The name is too already used, just free it so we can allocate a new name.
Chris Lattner32ab6432007-02-12 05:18:08 +000065 V->Name->Destroy();
66
Chris Lattner71a60ba2007-02-07 05:52:51 +000067 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000068 while (1) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000069 // Trim any suffix off.
70 UniqueName.resize(BaseSize);
Chris Lattner36b4e8f2008-06-27 21:26:26 +000071 UniqueName.append_uint_32(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000072 // Try insert the vmap entry with this suffix.
Chris Lattner32ab6432007-02-12 05:18:08 +000073 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
74 &UniqueName[UniqueName.size()]);
75 if (NewName.getValue() == 0) {
76 // Newly inserted name. Success!
77 NewName.setValue(V);
78 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000079 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000080 return;
81 }
82 }
83}
Reid Spencer25780d52006-01-10 09:51:48 +000084
Chris Lattner32ab6432007-02-12 05:18:08 +000085void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000086 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner36b4e8f2008-06-27 21:26:26 +000087 // Remove the value from the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000088 vmap.remove(V);
89}
90
91/// createValueName - This method attempts to create a value name and insert
92/// it into the symbol table with the specified name. If it conflicts, it
93/// auto-renames the name and returns that instead.
94ValueName *ValueSymbolTable::createValueName(const char *NameStart,
95 unsigned NameLen, Value *V) {
Chris Lattner36b4e8f2008-06-27 21:26:26 +000096 // In the common case, the name is not already in the symbol table.
Chris Lattner32ab6432007-02-12 05:18:08 +000097 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
98 if (Entry.getValue() == 0) {
99 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000100 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
101 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000102 return &Entry;
103 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +0000104
Chris Lattner32ab6432007-02-12 05:18:08 +0000105 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner36b4e8f2008-06-27 21:26:26 +0000106 SmallString<128> UniqueName(NameStart, NameStart+NameLen);
107
Chris Lattner32ab6432007-02-12 05:18:08 +0000108 while (1) {
109 // Trim any suffix off.
110 UniqueName.resize(NameLen);
Chris Lattner36b4e8f2008-06-27 21:26:26 +0000111 UniqueName.append_uint_32(++LastUnique);
112
Chris Lattner32ab6432007-02-12 05:18:08 +0000113 // Try insert the vmap entry with this suffix.
114 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
115 &UniqueName[UniqueName.size()]);
116 if (NewName.getValue() == 0) {
117 // Newly inserted name. Success!
118 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000119 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000120 return &NewName;
121 }
122 }
Reid Spencer25780d52006-01-10 09:51:48 +0000123}
124
Reid Spencer25780d52006-01-10 09:51:48 +0000125
126// dump - print out the symbol table
127//
128void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000129 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000130 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000131 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000132 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000133 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000134 }
Reid Spencer25780d52006-01-10 09:51:48 +0000135}