1 //===-- SymbolTable.cpp - Implement the SymbolTable class -----------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and revised by Reid
6 // Spencer. It is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 // This file implements the SymbolTable class for the VMCore library.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/SymbolTable.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/ADT/StringExtras.h"
24 #define DEBUG_SYMBOL_TABLE 0
25 #define DEBUG_ABSTYPE 0
27 SymbolTable::~SymbolTable() {
28 // Drop all abstract type references in the type plane...
29 for (type_iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
30 if (TI->second->isAbstract()) // If abstract, drop the reference...
31 cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
34 // TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the
35 // planes that could still have entries!
37 #ifndef NDEBUG // Only do this in -g mode...
38 bool LeftoverValues = true;
39 for (plane_iterator PI = pmap.begin(); PI != pmap.end(); ++PI) {
40 for (value_iterator VI = PI->second.begin(); VI != PI->second.end(); ++VI)
41 if (!isa<Constant>(VI->second) ) {
42 std::cerr << "Value still in symbol table! Type = '"
43 << PI->first->getDescription() << "' Name = '"
44 << VI->first << "'\n";
45 LeftoverValues = false;
49 assert(LeftoverValues && "Values remain in symbol table!");
53 // getUniqueName - Given a base name, return a string that is either equal to
54 // it (or derived from it) that does not already occur in the symbol table for
55 // the specified type.
57 std::string SymbolTable::getUniqueName(const Type *Ty,
58 const std::string &BaseName) const {
60 plane_const_iterator PI = pmap.find(Ty);
61 if (PI == pmap.end()) return BaseName;
63 std::string TryName = BaseName;
64 const ValueMap& vmap = PI->second;
65 value_const_iterator End = vmap.end();
67 // See if the name exists
68 while (vmap.find(TryName) != End) // Loop until we find a free
69 TryName = BaseName + utostr(++LastUnique); // name in the symbol table
74 // lookup a value - Returns null on failure...
75 Value *SymbolTable::lookup(const Type *Ty, const std::string &Name) const {
76 plane_const_iterator PI = pmap.find(Ty);
77 if (PI != pmap.end()) { // We have symbols in that plane.
78 value_const_iterator VI = PI->second.find(Name);
79 if (VI != PI->second.end()) // and the name is in our hash table.
86 // lookup a type by name - returns null on failure
87 Type* SymbolTable::lookupType( const std::string& Name ) const {
88 type_const_iterator TI = tmap.find( Name );
89 if ( TI != tmap.end() )
90 return const_cast<Type*>(TI->second);
95 void SymbolTable::remove(Value *N) {
96 assert(N->hasName() && "Value doesn't have name!");
97 if (InternallyInconsistent) return;
99 plane_iterator PI = pmap.find(N->getType());
100 assert(PI != pmap.end() &&
101 "Trying to remove a value that doesn't have a type plane yet!");
102 removeEntry(PI, PI->second.find(N->getName()));
105 /// changeName - Given a value with a non-empty name, remove its existing entry
106 /// from the symbol table and insert a new one for Name. This is equivalent to
107 /// doing "remove(V), V->Name = Name, insert(V)", but is faster, and will not
108 /// temporarily remove the symbol table plane if V is the last value in the
109 /// symtab with that name (which could invalidate iterators to that plane).
110 void SymbolTable::changeName(Value *V, const std::string &name) {
111 assert(!V->getName().empty() && !name.empty() && V->getName() != name &&
112 "Illegal use of this method!");
114 plane_iterator PI = pmap.find(V->getType());
115 assert(PI != pmap.end() && "Value doesn't have an entry in this table?");
116 ValueMap &VM = PI->second;
120 if (!InternallyInconsistent) {
121 VI = VM.find(V->getName());
122 assert(VI != VM.end() && "Value does have an entry in this table?");
124 // Remove the old entry.
128 // See if we can insert the new name.
129 VI = VM.lower_bound(name);
131 // Is there a naming conflict?
132 if (VI != VM.end() && VI->first == name) {
133 V->Name = getUniqueName(V->getType(), name);
134 VM.insert(make_pair(V->Name, V));
137 VM.insert(VI, make_pair(name, V));
142 // removeEntry - Remove a value from the symbol table...
143 Value *SymbolTable::removeEntry(plane_iterator Plane, value_iterator Entry) {
144 if (InternallyInconsistent) return 0;
145 assert(Plane != pmap.end() &&
146 Entry != Plane->second.end() && "Invalid entry to remove!");
148 Value *Result = Entry->second;
149 #if DEBUG_SYMBOL_TABLE
151 std::cerr << " Removing Value: " << Result->getName() << "\n";
154 // Remove the value from the plane...
155 Plane->second.erase(Entry);
157 // If the plane is empty, remove it now!
158 if (Plane->second.empty()) {
159 // If the plane represented an abstract type that we were interested in,
160 // unlink ourselves from this plane.
162 if (Plane->first->isAbstract()) {
164 std::cerr << "Plane Empty: Removing type: "
165 << Plane->first->getDescription() << "\n";
167 cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
176 // remove - Remove a type
177 void SymbolTable::remove(const Type* Ty ) {
178 type_iterator TI = this->type_begin();
179 type_iterator TE = this->type_end();
181 // Search for the entry
182 while ( TI != TE && TI->second != Ty )
186 this->removeEntry( TI );
190 // removeEntry - Remove a type from the symbol table...
191 Type* SymbolTable::removeEntry(type_iterator Entry) {
192 if (InternallyInconsistent) return 0;
193 assert( Entry != tmap.end() && "Invalid entry to remove!");
195 const Type* Result = Entry->second;
197 #if DEBUG_SYMBOL_TABLE
199 std::cerr << " Removing Value: " << Result->getName() << "\n";
204 // If we are removing an abstract type, remove the symbol table from it's use
206 if (Result->isAbstract()) {
208 std::cerr << "Removing abstract type from symtab" << Result->getDescription()<<"\n";
210 cast<DerivedType>(Result)->removeAbstractTypeUser(this);
213 return const_cast<Type*>(Result);
217 // insertEntry - Insert a value into the symbol table with the specified name.
218 void SymbolTable::insertEntry(const std::string &Name, const Type *VTy,
220 plane_iterator PI = pmap.find(VTy); // Plane iterator
221 value_iterator VI; // Actual value iterator
222 ValueMap *VM; // The plane we care about.
224 #if DEBUG_SYMBOL_TABLE
226 std::cerr << " Inserting definition: " << Name << ": "
227 << VTy->getDescription() << "\n";
230 if (PI == pmap.end()) { // Not in collection yet... insert dummy entry
231 // Insert a new empty element. I points to the new elements.
232 VM = &pmap.insert(make_pair(VTy, ValueMap())).first->second;
235 // Check to see if the type is abstract. If so, it might be refined in the
236 // future, which would cause the plane of the old type to get merged into
239 if (VTy->isAbstract()) {
240 cast<DerivedType>(VTy)->addAbstractTypeUser(this);
242 std::cerr << "Added abstract type value: " << VTy->getDescription()
248 // Check to see if there is a naming conflict. If so, rename this value!
250 VI = VM->lower_bound(Name);
251 if (VI != VM->end() && VI->first == Name) {
252 std::string UniqueName = getUniqueName(VTy, Name);
253 assert(InternallyInconsistent == false &&
254 "Infinite loop inserting value!");
255 InternallyInconsistent = true;
256 V->setName(UniqueName);
257 InternallyInconsistent = false;
262 VM->insert(VI, make_pair(Name, V));
266 // insertEntry - Insert a value into the symbol table with the specified
269 void SymbolTable::insertEntry(const std::string& Name, const Type* T) {
271 // Check to see if there is a naming conflict. If so, rename this type!
272 std::string UniqueName = Name;
273 if (lookupType(Name))
274 UniqueName = getUniqueName(T, Name);
276 #if DEBUG_SYMBOL_TABLE
278 std::cerr << " Inserting type: " << UniqueName << ": "
279 << T->getDescription() << "\n";
282 // Insert the tmap entry
283 tmap.insert(make_pair(UniqueName, T));
285 // If we are adding an abstract type, add the symbol table to it's use list.
286 if (T->isAbstract()) {
287 cast<DerivedType>(T)->addAbstractTypeUser(this);
289 std::cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
294 // Strip the symbol table of its names.
295 bool SymbolTable::strip() {
296 bool RemovedSymbol = false;
297 for (plane_iterator I = pmap.begin(); I != pmap.end();) {
298 // Removing items from the plane can cause the plane itself to get deleted.
299 // If this happens, make sure we incremented our plane iterator already!
300 ValueMap &Plane = (I++)->second;
301 value_iterator B = Plane.begin(), Bend = Plane.end();
302 while (B != Bend) { // Found nonempty type plane!
303 Value *V = B->second;
304 if (!isa<GlobalValue>(V) || cast<GlobalValue>(V)->hasInternalLinkage()) {
305 // Set name to "", removing from symbol table!
307 RemovedSymbol = true;
313 for (type_iterator TI = tmap.begin(); TI != tmap.end(); ) {
314 const Type* T = (TI++)->second;
316 RemovedSymbol = true;
319 return RemovedSymbol;
323 // This function is called when one of the types in the type plane are refined
324 void SymbolTable::refineAbstractType(const DerivedType *OldType,
325 const Type *NewType) {
327 // Search to see if we have any values of the type Oldtype. If so, we need to
328 // move them into the newtype plane...
329 plane_iterator PI = pmap.find(OldType);
330 if (PI != pmap.end()) {
331 // Get a handle to the new type plane...
332 plane_iterator NewTypeIt = pmap.find(NewType);
333 if (NewTypeIt == pmap.end()) { // If no plane exists, add one
334 NewTypeIt = pmap.insert(make_pair(NewType, ValueMap())).first;
336 if (NewType->isAbstract()) {
337 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
339 std::cerr << "[Added] refined to abstype: " << NewType->getDescription()
345 ValueMap &NewPlane = NewTypeIt->second;
346 ValueMap &OldPlane = PI->second;
347 while (!OldPlane.empty()) {
348 std::pair<const std::string, Value*> V = *OldPlane.begin();
350 // Check to see if there is already a value in the symbol table that this
351 // would collide with.
352 value_iterator VI = NewPlane.find(V.first);
353 if (VI != NewPlane.end() && VI->second == V.second) {
356 } else if (VI != NewPlane.end()) {
357 // The only thing we are allowing for now is two external global values
360 GlobalValue *ExistGV = dyn_cast<GlobalValue>(VI->second);
361 GlobalValue *NewGV = dyn_cast<GlobalValue>(V.second);
363 if (ExistGV && NewGV) {
364 assert((ExistGV->isExternal() || NewGV->isExternal()) &&
365 "Two planes folded together with overlapping value names!");
367 // Make sure that ExistGV is the one we want to keep!
368 if (!NewGV->isExternal())
369 std::swap(NewGV, ExistGV);
371 // Ok we have two external global values. Make all uses of the new
372 // one use the old one...
373 NewGV->uncheckedReplaceAllUsesWith(ExistGV);
375 // Now we just convert it to an unnamed method... which won't get
376 // added to our symbol table. The problem is that if we call
377 // setName on the method that it will try to remove itself from
378 // the symbol table and die... because it's not in the symtab
379 // right now. To fix this, we have an internally consistent flag
380 // that turns remove into a noop. Thus the name will get null'd
381 // out, but the symbol table won't get upset.
383 assert(InternallyInconsistent == false &&
384 "Symbol table already inconsistent!");
385 InternallyInconsistent = true;
387 // Remove newM from the symtab
389 InternallyInconsistent = false;
391 // Now we can remove this global from the module entirely...
392 Module *M = NewGV->getParent();
393 if (Function *F = dyn_cast<Function>(NewGV))
394 M->getFunctionList().remove(F);
396 M->getGlobalList().remove(cast<GlobalVariable>(NewGV));
399 // If they are not global values, they must be just random values who
400 // happen to conflict now that types have been resolved. If this is
401 // the case, reinsert the value into the new plane, allowing it to get
403 assert(V.second->getType() == NewType &&"Type resolution is broken!");
407 insertEntry(V.first, NewType, V.second);
409 // Remove the item from the old type plane
410 OldPlane.erase(OldPlane.begin());
413 // Ok, now we are not referencing the type anymore... take me off your user
416 std::cerr << "Removing type " << OldType->getDescription() << "\n";
418 OldType->removeAbstractTypeUser(this);
420 // Remove the plane that is no longer used
424 // Loop over all of the types in the symbol table, replacing any references
425 // to OldType with references to NewType. Note that there may be multiple
426 // occurrences, and although we only need to remove one at a time, it's
427 // faster to remove them all in one pass.
429 for (type_iterator I = type_begin(), E = type_end(); I != E; ++I) {
430 if (I->second == (Type*)OldType) { // FIXME when Types aren't const.
432 std::cerr << "Removing type " << OldType->getDescription() << "\n";
434 OldType->removeAbstractTypeUser(this);
436 I->second = (Type*)NewType; // TODO FIXME when types aren't const
437 if (NewType->isAbstract()) {
439 std::cerr << "Added type " << NewType->getDescription() << "\n";
441 cast<DerivedType>(NewType)->addAbstractTypeUser(this);
448 // Handle situation where type becomes Concreate from Abstract
449 void SymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
450 plane_iterator PI = pmap.find(AbsTy);
452 // If there are any values in the symbol table of this type, then the type
453 // plane is a use of the abstract type which must be dropped.
454 if (PI != pmap.end())
455 AbsTy->removeAbstractTypeUser(this);
457 // Loop over all of the types in the symbol table, dropping any abstract
458 // type user entries for AbsTy which occur because there are names for the
460 for (type_iterator TI = type_begin(), TE = type_end(); TI != TE; ++TI)
461 if (TI->second == (Type*)AbsTy) // FIXME when Types aren't const.
462 AbsTy->removeAbstractTypeUser(this);
465 static void DumpVal(const std::pair<const std::string, Value *> &V) {
466 std::cerr << " '" << V.first << "' = ";
471 static void DumpPlane(const std::pair<const Type *,
472 std::map<const std::string, Value *> >&P){
475 for_each(P.second.begin(), P.second.end(), DumpVal);
478 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
479 std::cerr << " '" << T.first << "' = ";
484 void SymbolTable::dump() const {
485 std::cerr << "Symbol table dump:\n Plane:";
486 for_each(pmap.begin(), pmap.end(), DumpPlane);
487 std::cerr << " Types: ";
488 for_each(tmap.begin(), tmap.end(), DumpTypes);