Use covariant return types for Instruction::clone, and eliminate
[oota-llvm.git] / lib / VMCore / TypeSymbolTable.cpp
1 //===-- TypeSymbolTable.cpp - Implement the TypeSymbolTable class ---------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the TypeSymbolTable class for the VMCore library.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/TypeSymbolTable.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/ADT/StringExtras.h"
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/ManagedStatic.h"
19 #include "llvm/Support/raw_ostream.h"
20 #include "llvm/System/RWMutex.h"
21 #include "llvm/System/Threading.h"
22 #include <algorithm>
23 using namespace llvm;
24
25 #define DEBUG_SYMBOL_TABLE 0
26 #define DEBUG_ABSTYPE 0
27
28 static ManagedStatic<sys::SmartRWMutex<true> > TypeSymbolTableLock;
29
30 TypeSymbolTable::~TypeSymbolTable() {
31   // Drop all abstract type references in the type plane...
32   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
33     if (TI->second->isAbstract())   // If abstract, drop the reference...
34       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
35   }
36 }
37
38 std::string TypeSymbolTable::getUniqueName(const StringRef &BaseName) const {
39   std::string TryName = BaseName;
40   
41   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
42   
43   const_iterator End = tmap.end();
44
45   // See if the name exists
46   while (tmap.find(TryName) != End)            // Loop until we find a free
47     TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table
48   return TryName;
49 }
50
51 // lookup a type by name - returns null on failure
52 Type* TypeSymbolTable::lookup(const StringRef &Name) const {
53   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
54   
55   const_iterator TI = tmap.find(Name);
56   Type* result = 0;
57   if (TI != tmap.end())
58     result = const_cast<Type*>(TI->second);
59   return result;
60 }
61
62 TypeSymbolTable::iterator TypeSymbolTable::find(const StringRef &Name) {
63   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);  
64   return tmap.find(Name);
65 }
66
67 TypeSymbolTable::const_iterator
68 TypeSymbolTable::find(const StringRef &Name) const {
69   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);  
70   return tmap.find(Name);
71 }
72
73 // remove - Remove a type from the symbol table...
74 Type* TypeSymbolTable::remove(iterator Entry) {
75   TypeSymbolTableLock->writer_acquire();
76   
77   assert(Entry != tmap.end() && "Invalid entry to remove!");
78   const Type* Result = Entry->second;
79
80 #if DEBUG_SYMBOL_TABLE
81   dump();
82   errs() << " Removing Value: " << Result->getName() << "\n";
83 #endif
84
85   tmap.erase(Entry);
86   
87   TypeSymbolTableLock->writer_release();
88
89   // If we are removing an abstract type, remove the symbol table from it's use
90   // list...
91   if (Result->isAbstract()) {
92 #if DEBUG_ABSTYPE
93     errs() << "Removing abstract type from symtab"
94            << Result->getDescription()
95            << "\n";
96 #endif
97     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
98   }
99
100   return const_cast<Type*>(Result);
101 }
102
103
104 // insert - Insert a type into the symbol table with the specified name...
105 void TypeSymbolTable::insert(const StringRef &Name, const Type* T) {
106   assert(T && "Can't insert null type into symbol table!");
107
108   TypeSymbolTableLock->writer_acquire();
109
110   if (tmap.insert(std::make_pair(Name, T)).second) {
111     // Type inserted fine with no conflict.
112     
113 #if DEBUG_SYMBOL_TABLE
114     dump();
115     errs() << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
116 #endif
117   } else {
118     // If there is a name conflict...
119     
120     // Check to see if there is a naming conflict.  If so, rename this type!
121     std::string UniqueName = Name;
122     if (lookup(Name))
123       UniqueName = getUniqueName(Name);
124     
125 #if DEBUG_SYMBOL_TABLE
126     dump();
127     errs() << " Inserting type: " << UniqueName << ": "
128            << T->getDescription() << "\n";
129 #endif
130
131     // Insert the tmap entry
132     tmap.insert(make_pair(UniqueName, T));
133   }
134   
135   TypeSymbolTableLock->writer_release();
136
137   // If we are adding an abstract type, add the symbol table to it's use list.
138   if (T->isAbstract()) {
139     cast<DerivedType>(T)->addAbstractTypeUser(this);
140 #if DEBUG_ABSTYPE
141     errs() << "Added abstract type to ST: " << T->getDescription() << "\n";
142 #endif
143   }
144 }
145
146 // This function is called when one of the types in the type plane are refined
147 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
148                                          const Type *NewType) {
149   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
150   
151   // Loop over all of the types in the symbol table, replacing any references
152   // to OldType with references to NewType.  Note that there may be multiple
153   // occurrences, and although we only need to remove one at a time, it's
154   // faster to remove them all in one pass.
155   //
156   for (iterator I = begin(), E = end(); I != E; ++I) {
157     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
158 #if DEBUG_ABSTYPE
159       errs() << "Removing type " << OldType->getDescription() << "\n";
160 #endif
161       OldType->removeAbstractTypeUser(this);
162
163       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
164       if (NewType->isAbstract()) {
165 #if DEBUG_ABSTYPE
166         errs() << "Added type " << NewType->getDescription() << "\n";
167 #endif
168         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
169       }
170     }
171   }
172 }
173
174
175 // Handle situation where type becomes Concreate from Abstract
176 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
177   // Loop over all of the types in the symbol table, dropping any abstract
178   // type user entries for AbsTy which occur because there are names for the
179   // type.
180   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
181   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
182     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
183       AbsTy->removeAbstractTypeUser(this);
184 }
185
186 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
187   errs() << "  '" << T.first << "' = ";
188   T.second->dump();
189   errs() << "\n";
190 }
191
192 void TypeSymbolTable::dump() const {
193   errs() << "TypeSymbolPlane: ";
194   sys::SmartScopedReader<true> Reader(*TypeSymbolTableLock);
195   for_each(tmap.begin(), tmap.end(), DumpTypes);
196 }
197
198 // vim: sw=2 ai