teach reassociate to factor x+x+x -> x*3. While I'm at it,
[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 <algorithm>
21 using namespace llvm;
22
23 #define DEBUG_SYMBOL_TABLE 0
24 #define DEBUG_ABSTYPE 0
25
26 TypeSymbolTable::~TypeSymbolTable() {
27   // Drop all abstract type references in the type plane...
28   for (iterator TI = tmap.begin(), TE = tmap.end(); TI != TE; ++TI) {
29     if (TI->second->isAbstract())   // If abstract, drop the reference...
30       cast<DerivedType>(TI->second)->removeAbstractTypeUser(this);
31   }
32 }
33
34 std::string TypeSymbolTable::getUniqueName(StringRef BaseName) const {
35   std::string TryName = BaseName;
36   
37   const_iterator End = tmap.end();
38
39   // See if the name exists
40   while (tmap.find(TryName) != End)            // Loop until we find a free
41     TryName = BaseName.str() + utostr(++LastUnique); // name in the symbol table
42   return TryName;
43 }
44
45 // lookup a type by name - returns null on failure
46 Type* TypeSymbolTable::lookup(StringRef Name) const {
47   const_iterator TI = tmap.find(Name);
48   Type* result = 0;
49   if (TI != tmap.end())
50     result = const_cast<Type*>(TI->second);
51   return result;
52 }
53
54 // remove - Remove a type from the symbol table...
55 Type* TypeSymbolTable::remove(iterator Entry) {
56   assert(Entry != tmap.end() && "Invalid entry to remove!");
57   const Type* Result = Entry->second;
58
59 #if DEBUG_SYMBOL_TABLE
60   dump();
61   errs() << " Removing Value: " << Result->getDescription() << "\n";
62 #endif
63
64   tmap.erase(Entry);
65   
66   // If we are removing an abstract type, remove the symbol table from it's use
67   // list...
68   if (Result->isAbstract()) {
69 #if DEBUG_ABSTYPE
70     errs() << "Removing abstract type from symtab"
71            << Result->getDescription()
72            << "\n";
73 #endif
74     cast<DerivedType>(Result)->removeAbstractTypeUser(this);
75   }
76
77   return const_cast<Type*>(Result);
78 }
79
80
81 // insert - Insert a type into the symbol table with the specified name...
82 void TypeSymbolTable::insert(StringRef Name, const Type* T) {
83   assert(T && "Can't insert null type into symbol table!");
84
85   if (tmap.insert(std::make_pair(Name, T)).second) {
86     // Type inserted fine with no conflict.
87     
88 #if DEBUG_SYMBOL_TABLE
89     dump();
90     errs() << " Inserted type: " << Name << ": " << T->getDescription() << "\n";
91 #endif
92   } else {
93     // If there is a name conflict...
94     
95     // Check to see if there is a naming conflict.  If so, rename this type!
96     std::string UniqueName = Name;
97     if (lookup(Name))
98       UniqueName = getUniqueName(Name);
99     
100 #if DEBUG_SYMBOL_TABLE
101     dump();
102     errs() << " Inserting type: " << UniqueName << ": "
103            << T->getDescription() << "\n";
104 #endif
105
106     // Insert the tmap entry
107     tmap.insert(make_pair(UniqueName, T));
108   }
109   
110   // If we are adding an abstract type, add the symbol table to it's use list.
111   if (T->isAbstract()) {
112     cast<DerivedType>(T)->addAbstractTypeUser(this);
113 #if DEBUG_ABSTYPE
114     errs() << "Added abstract type to ST: " << T->getDescription() << "\n";
115 #endif
116   }
117 }
118
119 // This function is called when one of the types in the type plane are refined
120 void TypeSymbolTable::refineAbstractType(const DerivedType *OldType,
121                                          const Type *NewType) {
122   // Loop over all of the types in the symbol table, replacing any references
123   // to OldType with references to NewType.  Note that there may be multiple
124   // occurrences, and although we only need to remove one at a time, it's
125   // faster to remove them all in one pass.
126   //
127   for (iterator I = begin(), E = end(); I != E; ++I) {
128     if (I->second == (Type*)OldType) {  // FIXME when Types aren't const.
129 #if DEBUG_ABSTYPE
130       errs() << "Removing type " << OldType->getDescription() << "\n";
131 #endif
132       OldType->removeAbstractTypeUser(this);
133
134       I->second = (Type*)NewType;  // TODO FIXME when types aren't const
135       if (NewType->isAbstract()) {
136 #if DEBUG_ABSTYPE
137         errs() << "Added type " << NewType->getDescription() << "\n";
138 #endif
139         cast<DerivedType>(NewType)->addAbstractTypeUser(this);
140       }
141     }
142   }
143 }
144
145
146 // Handle situation where type becomes Concreate from Abstract
147 void TypeSymbolTable::typeBecameConcrete(const DerivedType *AbsTy) {
148   // Loop over all of the types in the symbol table, dropping any abstract
149   // type user entries for AbsTy which occur because there are names for the
150   // type.
151   for (iterator TI = begin(), TE = end(); TI != TE; ++TI)
152     if (TI->second == const_cast<Type*>(static_cast<const Type*>(AbsTy)))
153       AbsTy->removeAbstractTypeUser(this);
154 }
155
156 static void DumpTypes(const std::pair<const std::string, const Type*>& T ) {
157   errs() << "  '" << T.first << "' = ";
158   T.second->dump();
159   errs() << "\n";
160 }
161
162 void TypeSymbolTable::dump() const {
163   errs() << "TypeSymbolPlane: ";
164   for_each(tmap.begin(), tmap.end(), DumpTypes);
165 }
166