blob: f0d68238229247c2df4031e5ac0a308532c4b833 [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 Lattner21f0c312006-01-11 05:39:45 +00005// This file was developed by the LLVM research group. It is distributed under
6// the University of Illinois Open Source 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
Reid Spencer25780d52006-01-10 09:51:48 +000057// Insert a value into the symbol table with the specified name...
58//
Chris Lattner32ab6432007-02-12 05:18:08 +000059void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencer25780d52006-01-10 09:51:48 +000060 assert(V->hasName() && "Can't insert nameless Value into symbol table");
61
Chris Lattner770dadf2007-02-07 05:22:49 +000062 // Try inserting the name, assuming it won't conflict.
Chris Lattner32ab6432007-02-12 05:18:08 +000063 if (vmap.insert(V->Name)) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000064 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattner770dadf2007-02-07 05:22:49 +000065 return;
66 }
67
Chris Lattner32ab6432007-02-12 05:18:08 +000068 // FIXME: this could be much more efficient.
69
Chris Lattner770dadf2007-02-07 05:22:49 +000070 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner71a60ba2007-02-07 05:52:51 +000071 std::string UniqueName = V->getName();
Chris Lattner32ab6432007-02-12 05:18:08 +000072
73 V->Name->Destroy();
74
Chris Lattner71a60ba2007-02-07 05:52:51 +000075 unsigned BaseSize = UniqueName.size();
Chris Lattner32ab6432007-02-12 05:18:08 +000076 while (1) {
Chris Lattner71a60ba2007-02-07 05:52:51 +000077 // Trim any suffix off.
78 UniqueName.resize(BaseSize);
79 UniqueName += utostr(++LastUnique);
Chris Lattnerd17dbe92007-02-07 06:25:36 +000080 // Try insert the vmap entry with this suffix.
Chris Lattner32ab6432007-02-12 05:18:08 +000081 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
82 &UniqueName[UniqueName.size()]);
83 if (NewName.getValue() == 0) {
84 // Newly inserted name. Success!
85 NewName.setValue(V);
86 V->Name = &NewName;
Chris Lattnerb377a7c2007-02-25 20:42:59 +000087 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000088 return;
89 }
90 }
91}
Reid Spencer25780d52006-01-10 09:51:48 +000092
Chris Lattner32ab6432007-02-12 05:18:08 +000093void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +000094 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +000095 // Remove the value from the plane.
96 vmap.remove(V);
97}
98
99/// createValueName - This method attempts to create a value name and insert
100/// it into the symbol table with the specified name. If it conflicts, it
101/// auto-renames the name and returns that instead.
102ValueName *ValueSymbolTable::createValueName(const char *NameStart,
103 unsigned NameLen, Value *V) {
104 ValueName &Entry = vmap.GetOrCreateValue(NameStart, NameStart+NameLen);
105 if (Entry.getValue() == 0) {
106 Entry.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000107 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
108 // << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000109 return &Entry;
110 }
Chris Lattnerd17dbe92007-02-07 06:25:36 +0000111
Chris Lattner32ab6432007-02-12 05:18:08 +0000112 // FIXME: this could be much more efficient.
113
114 // Otherwise, there is a naming conflict. Rename this value.
115 std::string UniqueName(NameStart, NameStart+NameLen);
116 while (1) {
117 // Trim any suffix off.
118 UniqueName.resize(NameLen);
119 UniqueName += utostr(++LastUnique);
120 // Try insert the vmap entry with this suffix.
121 ValueName &NewName = vmap.GetOrCreateValue(&UniqueName[0],
122 &UniqueName[UniqueName.size()]);
123 if (NewName.getValue() == 0) {
124 // Newly inserted name. Success!
125 NewName.setValue(V);
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000126 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattner32ab6432007-02-12 05:18:08 +0000127 return &NewName;
128 }
129 }
Reid Spencer25780d52006-01-10 09:51:48 +0000130}
131
Reid Spencer25780d52006-01-10 09:51:48 +0000132
133// dump - print out the symbol table
134//
135void ValueSymbolTable::dump() const {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000136 //DOUT << "ValueSymbolTable:\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000137 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000138 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattner32ab6432007-02-12 05:18:08 +0000139 I->getValue()->dump();
Chris Lattnerb377a7c2007-02-25 20:42:59 +0000140 //DOUT << "\n";
Chris Lattner32ab6432007-02-12 05:18:08 +0000141 }
Reid Spencer25780d52006-01-10 09:51:48 +0000142}