blob: 358b5a23d0b888c7bfba758eb7dd1c7300971f78 [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 +000020#include <algorithm>
Reid Spencer25780d52006-01-10 09:51:48 +000021using namespace llvm;
22
Reid Spencer25780d52006-01-10 09:51:48 +000023// Class destructor
24ValueSymbolTable::~ValueSymbolTable() {
25#ifndef NDEBUG // Only do this in -g mode...
26 bool LeftoverValues = true;
27 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ++VI)
28 if (!isa<Constant>(VI->second) ) {
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000029 DEBUG(DOUT << "Value still in symbol table! Type = '"
Bill Wendling6a462f12006-11-17 08:03:48 +000030 << VI->second->getType()->getDescription() << "' Name = '"
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000031 << VI->first << "'\n");
Reid Spencer25780d52006-01-10 09:51:48 +000032 LeftoverValues = false;
33 }
34 assert(LeftoverValues && "Values remain in symbol table!");
35#endif
36}
37
38// getUniqueName - Given a base name, return a string that is either equal to
39// it (or derived from it) that does not already occur in the symbol table for
40// the specified type.
41//
42std::string ValueSymbolTable::getUniqueName(const std::string &BaseName) const {
43 std::string TryName = BaseName;
44 const_iterator End = vmap.end();
45
46 // See if the name exists
47 while (vmap.find(TryName) != End) // Loop until we find a free
48 TryName = BaseName + utostr(++LastUnique); // name in the symbol table
49 return TryName;
50}
51
52
53// lookup a value - Returns null on failure...
54//
55Value *ValueSymbolTable::lookup(const std::string &Name) const {
56 const_iterator VI = vmap.find(Name);
57 if (VI != vmap.end()) // We found the symbol
58 return const_cast<Value*>(VI->second);
59 return 0;
60}
61
62// Strip the symbol table of its names.
63//
64bool ValueSymbolTable::strip() {
65 bool RemovedSymbol = false;
66 for (iterator VI = vmap.begin(), VE = vmap.end(); VI != VE; ) {
67 Value *V = VI->second;
68 ++VI;
69 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
70 // Set name to "", removing from symbol table!
71 V->setName("");
72 RemovedSymbol = true;
73 }
74 }
75 return RemovedSymbol;
76}
77
78// Insert a value into the symbol table with the specified name...
79//
80void ValueSymbolTable::insert(Value* V) {
81 assert(V && "Can't insert null Value into symbol table!");
82 assert(V->hasName() && "Can't insert nameless Value into symbol table");
83
Chris Lattner770dadf2007-02-07 05:22:49 +000084 // Try inserting the name, assuming it won't conflict.
85 if (vmap.insert(make_pair(V->Name, V)).second) {
86 DOUT << " Inserted value: " << V->Name << ": " << *V << "\n";
87 return;
88 }
89
90 // Otherwise, there is a naming conflict. Rename this value.
Chris Lattner71a60ba2007-02-07 05:52:51 +000091 std::string UniqueName = V->getName();
92 unsigned BaseSize = UniqueName.size();
93 do {
94 // Trim any suffix off.
95 UniqueName.resize(BaseSize);
96 UniqueName += utostr(++LastUnique);
97 } while (!vmap.insert(make_pair(UniqueName, V)).second);
Reid Spencer25780d52006-01-10 09:51:48 +000098
Reid Spencer3aaaa0b2007-02-05 20:47:22 +000099 DEBUG(DOUT << " Inserting value: " << UniqueName << ": " << *V << "\n");
Reid Spencer25780d52006-01-10 09:51:48 +0000100
101 // Insert the vmap entry
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000102 V->Name = UniqueName;
Reid Spencer25780d52006-01-10 09:51:48 +0000103}
104
105// Remove a value
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000106bool ValueSymbolTable::remove(Value *V) {
Reid Spencer25780d52006-01-10 09:51:48 +0000107 assert(V->hasName() && "Value doesn't have name!");
108 iterator Entry = vmap.find(V->getName());
109 if (Entry == vmap.end())
110 return false;
111
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000112 DEBUG(DOUT << " Removing Value: " << Entry->second->getName() << "\n");
Reid Spencer25780d52006-01-10 09:51:48 +0000113
114 // Remove the value from the plane...
115 vmap.erase(Entry);
116 return true;
117}
118
119
120// rename - Given a value with a non-empty name, remove its existing entry
121// from the symbol table and insert a new one for Name. This is equivalent to
122// doing "remove(V), V->Name = Name, insert(V)",
123//
124bool ValueSymbolTable::rename(Value *V, const std::string &name) {
125 assert(V && "Can't rename a null Value");
126 assert(V->hasName() && "Can't rename a nameless Value");
127 assert(!V->getName().empty() && "Can't rename an Value with null name");
128 assert(V->getName() != name && "Can't rename a Value with same name");
129 assert(!name.empty() && "Can't rename a named Value with a null name");
130
131 // Find the name
132 iterator VI = vmap.find(V->getName());
133
134 // If we didn't find it, we're done
135 if (VI == vmap.end())
136 return false;
137
138 // Remove the old entry.
139 vmap.erase(VI);
140
141 // See if we can insert the new name.
142 VI = vmap.lower_bound(name);
143
144 // Is there a naming conflict?
145 if (VI != vmap.end() && VI->first == name) {
146 V->Name = getUniqueName( name);
147 vmap.insert(make_pair(V->Name, V));
148 } else {
149 V->Name = name;
Reid Spencer3aaaa0b2007-02-05 20:47:22 +0000150 vmap.insert(VI, make_pair(V->Name, V));
Reid Spencer25780d52006-01-10 09:51:48 +0000151 }
152
153 return true;
154}
155
156// DumpVal - a std::for_each function for dumping a value
157//
158static void DumpVal(const std::pair<const std::string, Value *> &V) {
Bill Wendling6a462f12006-11-17 08:03:48 +0000159 DOUT << " '" << V.first << "' = ";
Reid Spencer25780d52006-01-10 09:51:48 +0000160 V.second->dump();
Bill Wendling6a462f12006-11-17 08:03:48 +0000161 DOUT << "\n";
Reid Spencer25780d52006-01-10 09:51:48 +0000162}
163
164// dump - print out the symbol table
165//
166void ValueSymbolTable::dump() const {
Bill Wendling6a462f12006-11-17 08:03:48 +0000167 DOUT << "ValueSymbolTable:\n";
Reid Spencer25780d52006-01-10 09:51:48 +0000168 for_each(vmap.begin(), vmap.end(), DumpVal);
169}