blob: fb7c0d8509b9b22f17077d6fba1a8c27f415e8ca [file] [log] [blame]
Reid Spencereb7116b2006-01-10 09:51:48 +00001//===-- ValueSymbolTable.cpp - Implement the ValueSymbolTable class -------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-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 Spencereb7116b2006-01-10 09:51:48 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueSymbolTable class for the VMCore library.
11//
12//===----------------------------------------------------------------------===//
13
Reid Spenceref9b9a72007-02-05 20:47:22 +000014#define DEBUG_TYPE "valuesymtab"
Reid Spencereb7116b2006-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 Wendling2e3def12006-11-17 08:03:48 +000019#include "llvm/Support/Debug.h"
Reid Spencereb7116b2006-01-10 09:51:48 +000020using namespace llvm;
21
Reid Spencereb7116b2006-01-10 09:51:48 +000022// Class destructor
23ValueSymbolTable::~ValueSymbolTable() {
24#ifndef NDEBUG // Only do this in -g mode...
Reid Spencereb7116b2006-01-10 09:51:48 +000025 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
Chris Lattner3f3098c2007-02-25 20:42:59 +000026 cerr << "Value still in symbol table! Type = '"
27 << VI->getValue()->getType()->getDescription() << "' Name = '"
28 << VI->getKeyData() << "'\n";
Chris Lattner92f5b6a2007-02-07 06:28:48 +000029 assert(vmap.empty() && "Values remain in symbol table!");
Reid Spencereb7116b2006-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 Spencereb7116b2006-01-10 09:51:48 +000039
40 // See if the name exists
Chris Lattnerdec628e2007-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 Spencereb7116b2006-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 Lattnerdec628e2007-02-12 05:18:08 +000051 const_iterator VI = vmap.find(&Name[0], &Name[Name.size()]);
Reid Spencereb7116b2006-01-10 09:51:48 +000052 if (VI != vmap.end()) // We found the symbol
Chris Lattnerdec628e2007-02-12 05:18:08 +000053 return VI->getValue();
Reid Spencereb7116b2006-01-10 09:51:48 +000054 return 0;
55}
56
Reid Spencereb7116b2006-01-10 09:51:48 +000057// Insert a value into the symbol table with the specified name...
58//
Chris Lattnerdec628e2007-02-12 05:18:08 +000059void ValueSymbolTable::reinsertValue(Value* V) {
Reid Spencereb7116b2006-01-10 09:51:48 +000060 assert(V->hasName() && "Can't insert nameless Value into symbol table");
61
Chris Lattnerf5111552007-02-07 05:22:49 +000062 // Try inserting the name, assuming it won't conflict.
Chris Lattnerdec628e2007-02-12 05:18:08 +000063 if (vmap.insert(V->Name)) {
Chris Lattner3f3098c2007-02-25 20:42:59 +000064 //DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
Chris Lattnerf5111552007-02-07 05:22:49 +000065 return;
66 }
67
Chris Lattnerdec628e2007-02-12 05:18:08 +000068 // FIXME: this could be much more efficient.
69
Chris Lattnerf5111552007-02-07 05:22:49 +000070 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner421d3da2007-02-07 05:52:51 +000071 std::string UniqueName = V->getName();
Chris Lattnerdec628e2007-02-12 05:18:08 +000072
73 V->Name->Destroy();
74
Chris Lattner421d3da2007-02-07 05:52:51 +000075 unsigned BaseSize = UniqueName.size();
Chris Lattnerdec628e2007-02-12 05:18:08 +000076 while (1) {
Chris Lattner421d3da2007-02-07 05:52:51 +000077 // Trim any suffix off.
78 UniqueName.resize(BaseSize);
79 UniqueName += utostr(++LastUnique);
Chris Lattnerb4d8d622007-02-07 06:25:36 +000080 // Try insert the vmap entry with this suffix.
Chris Lattnerdec628e2007-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 Lattner3f3098c2007-02-25 20:42:59 +000087 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +000088 return;
89 }
90 }
91}
Reid Spencereb7116b2006-01-10 09:51:48 +000092
Chris Lattnerdec628e2007-02-12 05:18:08 +000093void ValueSymbolTable::removeValueName(ValueName *V) {
Chris Lattner3f3098c2007-02-25 20:42:59 +000094 //DEBUG(DOUT << " Removing Value: " << V->getKeyData() << "\n");
Chris Lattnerdec628e2007-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 Lattner3f3098c2007-02-25 20:42:59 +0000107 //DEBUG(DOUT << " Inserted value: " << Entry.getKeyData() << ": "
108 // << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000109 return &Entry;
110 }
Chris Lattnerb4d8d622007-02-07 06:25:36 +0000111
Chris Lattnerdec628e2007-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 Lattner3f3098c2007-02-25 20:42:59 +0000126 //DEBUG(DOUT << " Inserted value: " << UniqueName << ": " << *V << "\n");
Chris Lattnerdec628e2007-02-12 05:18:08 +0000127 return &NewName;
128 }
129 }
Reid Spencereb7116b2006-01-10 09:51:48 +0000130}
131
Reid Spencereb7116b2006-01-10 09:51:48 +0000132
133// dump - print out the symbol table
134//
135void ValueSymbolTable::dump() const {
Chris Lattner3f3098c2007-02-25 20:42:59 +0000136 //DOUT << "ValueSymbolTable:\n";
Chris Lattnerdec628e2007-02-12 05:18:08 +0000137 for (const_iterator I = begin(), E = end(); I != E; ++I) {
Chris Lattner3f3098c2007-02-25 20:42:59 +0000138 //DOUT << " '" << I->getKeyData() << "' = ";
Chris Lattnerdec628e2007-02-12 05:18:08 +0000139 I->getValue()->dump();
Chris Lattner3f3098c2007-02-25 20:42:59 +0000140 //DOUT << "\n";
Chris Lattnerdec628e2007-02-12 05:18:08 +0000141 }
Reid Spencereb7116b2006-01-10 09:51:48 +0000142}