- Use Pass::AnalysisImpls instead of findAnalysisGroupMemeber
[oota-llvm.git] / lib / VMCore / SymbolTable.cpp
1 //===-- SymbolTable.cpp - Implement the SymbolTable class -------------------=//
2 //
3 // This file implements the SymbolTable class for the VMCore library.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/SymbolTable.h"
8 #include "llvm/DerivedTypes.h"
9 #include "llvm/Module.h"
10 #include "llvm/InstrTypes.h"
11 #include "Support/StringExtras.h"
12 #include <iostream>
13 #include <algorithm>
14
15 using std::string;
16 using std::pair;
17 using std::make_pair;
18 using std::map;
19 using std::cerr;
20
21 #define DEBUG_SYMBOL_TABLE 0
22 #define DEBUG_ABSTYPE 0
23
24 SymbolTable::~SymbolTable() {
25   // Drop all abstract type references in the type plane...
26   iterator TyPlane = find(Type::TypeTy);
27   if (TyPlane != end()) {
28     VarMap &TyP = TyPlane->second;
29     for (VarMap::iterator I = TyP.begin(), E = TyP.end(); I != E; ++I) {
30       const Type *Ty = cast<const Type>(I->second);
31       if (Ty->isAbstract())   // If abstract, drop the reference...
32         cast<DerivedType>(Ty)->removeAbstractTypeUser(this);
33     }
34   }
35
36  // TODO: FIXME: BIG ONE: This doesn't unreference abstract types for the planes
37  // that could still have entries!
38
39 #ifndef NDEBUG   // Only do this in -g mode...
40   bool LeftoverValues = true;
41   for (iterator i = begin(); i != end(); ++i) {
42     for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
43       if (!isa<Constant>(I->second) && !isa<Type>(I->second)) {
44         cerr << "Value still in symbol table! Type = '"
45              << i->first->getDescription() << "' Name = '"
46              << I->first << "'\n";
47         LeftoverValues = false;
48       }
49   }
50   
51   assert(LeftoverValues && "Values remain in symbol table!");
52 #endif
53 }
54
55 // getUniqueName - Given a base name, return a string that is either equal to
56 // it (or derived from it) that does not already occur in the symbol table for
57 // the specified type.
58 //
59 string SymbolTable::getUniqueName(const Type *Ty, const string &BaseName) {
60   iterator I = find(Ty);
61   if (I == end()) return BaseName;
62
63   string TryName = BaseName;
64   unsigned Counter = 0;
65   type_iterator End = I->second.end();
66
67   while (I->second.find(TryName) != End)     // Loop until we find unoccupied
68     TryName = BaseName + utostr(++Counter);  // Name in the symbol table
69   return TryName;
70 }
71
72
73
74 // lookup - Returns null on failure...
75 Value *SymbolTable::localLookup(const Type *Ty, const string &Name) {
76   iterator I = find(Ty);
77   if (I != end()) {                      // We have symbols in that plane...
78     type_iterator J = I->second.find(Name);
79     if (J != I->second.end())            // and the name is in our hash table...
80       return J->second;
81   }
82
83   return 0;
84 }
85
86 // lookup - Returns null on failure...
87 Value *SymbolTable::lookup(const Type *Ty, const string &Name) {
88   Value *LV = localLookup(Ty, Name);
89   if (LV) return LV;
90   return ParentSymTab ? ParentSymTab->lookup(Ty, Name) : 0;
91 }
92
93 void SymbolTable::remove(Value *N) {
94   assert(N->hasName() && "Value doesn't have name!");
95   if (InternallyInconsistent) return;
96
97   iterator I = find(N->getType());
98   assert(I != end() &&
99          "Trying to remove a type that doesn't have a plane yet!");
100   removeEntry(I, I->second.find(N->getName()));
101 }
102
103 // removeEntry - Remove a value from the symbol table...
104 //
105 Value *SymbolTable::removeEntry(iterator Plane, type_iterator Entry) {
106   if (InternallyInconsistent) return 0;
107   assert(Plane != super::end() &&
108          Entry != Plane->second.end() && "Invalid entry to remove!");
109
110   Value *Result = Entry->second;
111   const Type *Ty = Result->getType();
112 #if DEBUG_SYMBOL_TABLE
113   dump();
114   std::cerr << " Removing Value: " << Result->getName() << "\n";
115 #endif
116
117   // Remove the value from the plane...
118   Plane->second.erase(Entry);
119
120   // If the plane is empty, remove it now!
121   if (Plane->second.empty()) {
122     // If the plane represented an abstract type that we were interested in,
123     // unlink ourselves from this plane.
124     //
125     if (Plane->first->isAbstract()) {
126 #if DEBUG_ABSTYPE
127       cerr << "Plane Empty: Removing type: " << Plane->first->getDescription()
128            << "\n";
129 #endif
130       cast<DerivedType>(Plane->first)->removeAbstractTypeUser(this);
131     }
132
133     erase(Plane);
134   }
135
136   // If we are removing an abstract type, remove the symbol table from it's use
137   // list...
138   if (Ty == Type::TypeTy) {
139     const Type *T = cast<const Type>(Result);
140     if (T->isAbstract()) {
141 #if DEBUG_ABSTYPE
142       cerr << "Removing abs type from symtab" << T->getDescription() << "\n";
143 #endif
144       cast<DerivedType>(T)->removeAbstractTypeUser(this);
145     }
146   }
147
148   return Result;
149 }
150
151 // insertEntry - Insert a value into the symbol table with the specified
152 // name...
153 //
154 void SymbolTable::insertEntry(const string &Name, const Type *VTy, Value *V) {
155
156   // Check to see if there is a naming conflict.  If so, rename this value!
157   if (localLookup(VTy, Name)) {
158     string UniqueName = getUniqueName(VTy, Name);
159     assert(InternallyInconsistent == false && "Infinite loop inserting entry!");
160     InternallyInconsistent = true;
161     V->setName(UniqueName, this);
162     InternallyInconsistent = false;
163     return;
164   }
165
166 #if DEBUG_SYMBOL_TABLE
167   dump();
168   cerr << " Inserting definition: " << Name << ": " 
169        << VTy->getDescription() << "\n";
170 #endif
171
172   iterator I = find(VTy);
173   if (I == end()) {      // Not in collection yet... insert dummy entry
174     // Insert a new empty element.  I points to the new elements.
175     I = super::insert(make_pair(VTy, VarMap())).first;
176     assert(I != end() && "How did insert fail?");
177
178     // Check to see if the type is abstract.  If so, it might be refined in the
179     // future, which would cause the plane of the old type to get merged into
180     // a new type plane.
181     //
182     if (VTy->isAbstract()) {
183       cast<DerivedType>(VTy)->addAbstractTypeUser(this);
184 #if DEBUG_ABSTYPE
185       cerr << "Added abstract type value: " << VTy->getDescription() << "\n";
186 #endif
187     }
188   }
189
190   I->second.insert(make_pair(Name, V));
191
192   // If we are adding an abstract type, add the symbol table to it's use list.
193   if (VTy == Type::TypeTy) {
194     const Type *T = cast<const Type>(V);
195     if (T->isAbstract()) {
196       cast<DerivedType>(T)->addAbstractTypeUser(this);
197 #if DEBUG_ABSTYPE
198       cerr << "Added abstract type to ST: " << T->getDescription() << "\n";
199 #endif
200     }
201   }
202 }
203
204 // This function is called when one of the types in the type plane are refined
205 void SymbolTable::refineAbstractType(const DerivedType *OldType,
206                                      const Type *NewType) {
207   if (OldType == NewType && OldType->isAbstract())
208     return;  // Noop, don't waste time dinking around
209
210   // Search to see if we have any values of the type oldtype.  If so, we need to
211   // move them into the newtype plane...
212   iterator TPI = find(OldType);
213   if (OldType != NewType && TPI != end()) {
214     // Get a handle to the new type plane...
215     iterator NewTypeIt = find(NewType);
216     if (NewTypeIt == super::end()) {      // If no plane exists, add one
217       NewTypeIt = super::insert(make_pair(NewType, VarMap())).first;
218       
219       if (NewType->isAbstract()) {
220         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
221 #if DEBUG_ABSTYPE
222         cerr << "[Added] refined to abstype: "<<NewType->getDescription()<<"\n";
223 #endif
224       }
225     }
226
227     VarMap &NewPlane = NewTypeIt->second;
228     VarMap &OldPlane = TPI->second;
229     while (!OldPlane.empty()) {
230       pair<const string, Value*> V = *OldPlane.begin();
231
232       // Check to see if there is already a value in the symbol table that this
233       // would collide with.
234       type_iterator TI = NewPlane.find(V.first);
235       if (TI != NewPlane.end() && TI->second == V.second) {
236         // No action
237
238       } else if (TI != NewPlane.end()) {
239         // The only thing we are allowing for now is two method prototypes being
240         // folded into one.
241         //
242         Function *ExistM = dyn_cast<Function>(TI->second);
243         Function *NewM = dyn_cast<Function>(V.second);
244
245         if (ExistM && NewM && ExistM->isExternal() && NewM->isExternal()) {
246           // Ok we have two external methods.  Make all uses of the new one
247           // use the old one...
248           //
249           NewM->replaceAllUsesWith(ExistM);
250           
251           // Now we just convert it to an unnamed method... which won't get
252           // added to our symbol table.  The problem is that if we call
253           // setName on the method that it will try to remove itself from
254           // the symbol table and die... because it's not in the symtab
255           // right now.  To fix this, we have an internally consistent flag
256           // that turns remove into a noop.  Thus the name will get null'd
257           // out, but the symbol table won't get upset.
258           //
259           assert(InternallyInconsistent == false &&
260                  "Symbol table already inconsistent!");
261           InternallyInconsistent = true;
262
263           // Remove newM from the symtab
264           NewM->setName("");
265           InternallyInconsistent = false;
266
267           // Now we can remove this method from the module entirely...
268           NewM->getParent()->getFunctionList().remove(NewM);
269           delete NewM;
270
271         } else {
272           assert(0 && "Two ploanes folded together with overlapping "
273                  "value names!");
274         }
275       } else {
276         insertEntry(V.first, NewType, V.second);
277
278       }
279       // Remove the item from the old type plane
280       OldPlane.erase(OldPlane.begin());
281     }
282
283     // Ok, now we are not referencing the type anymore... take me off your user
284     // list please!
285 #if DEBUG_ABSTYPE
286     cerr << "Removing type " << OldType->getDescription() << "\n";
287 #endif
288     OldType->removeAbstractTypeUser(this);
289
290     // Remove the plane that is no longer used
291     erase(TPI);
292   } else if (TPI != end()) {
293     assert(OldType == NewType);
294 #if DEBUG_ABSTYPE
295     cerr << "Removing SELF type " << OldType->getDescription() << "\n";
296 #endif
297     OldType->removeAbstractTypeUser(this);
298   }
299
300   TPI = find(Type::TypeTy);
301   if (TPI != end()) {  
302     // Loop over all of the types in the symbol table, replacing any references
303     // to OldType with references to NewType.  Note that there may be multiple
304     // occurances, and although we only need to remove one at a time, it's
305     // faster to remove them all in one pass.
306     //
307     VarMap &TyPlane = TPI->second;
308     for (VarMap::iterator I = TyPlane.begin(), E = TyPlane.end(); I != E; ++I)
309       if (I->second == (Value*)OldType) {  // FIXME when Types aren't const.
310 #if DEBUG_ABSTYPE
311         cerr << "Removing type " << OldType->getDescription() << "\n";
312 #endif
313         OldType->removeAbstractTypeUser(this);
314         
315         I->second = (Value*)NewType;  // TODO FIXME when types aren't const
316         if (NewType->isAbstract()) {
317 #if DEBUG_ABSTYPE
318           cerr << "Added type " << NewType->getDescription() << "\n";
319 #endif
320           cast<const DerivedType>(NewType)->addAbstractTypeUser(this);
321         }
322       }
323   }
324 }
325
326 static void DumpVal(const pair<const string, Value *> &V) {
327   std::cout << "  '" << V.first << "' = ";
328   V.second->dump();
329   std::cout << "\n";
330 }
331
332 static void DumpPlane(const pair<const Type *, map<const string, Value *> >&P) {
333   std::cout << "  Plane: ";
334   P.first->dump();
335   std::cout << "\n";
336   for_each(P.second.begin(), P.second.end(), DumpVal);
337 }
338
339 void SymbolTable::dump() const {
340   std::cout << "Symbol table dump:\n";
341   for_each(begin(), end(), DumpPlane);
342
343   if (ParentSymTab) {
344     std::cout << "Parent ";
345     ParentSymTab->dump();
346   }
347 }