simple optimization for the type table
[oota-llvm.git] / lib / Bitcode / Writer / ValueEnumerator.cpp
1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements the ValueEnumerator class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ValueEnumerator.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/TypeSymbolTable.h"
18 #include "llvm/ValueSymbolTable.h"
19 #include <algorithm>
20 using namespace llvm;
21
22 static bool isFirstClassType(const std::pair<const llvm::Type*,
23                              unsigned int> &P) {
24   return P.first->isFirstClassType();
25 }
26
27 static bool CompareByFrequency(const std::pair<const llvm::Type*,
28                                unsigned int> &P1,
29                                const std::pair<const llvm::Type*,
30                                unsigned int> &P2) {
31   return P1.second > P2.second;
32 }
33
34 /// ValueEnumerator - Enumerate module-level information.
35 ValueEnumerator::ValueEnumerator(const Module *M) {
36   // Enumerate the global variables.
37   for (Module::const_global_iterator I = M->global_begin(),
38          E = M->global_end(); I != E; ++I)
39     EnumerateValue(I);
40
41   // Enumerate the functions.
42   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
43     EnumerateValue(I);
44
45   // Enumerate the aliases.
46   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
47        I != E; ++I)
48     EnumerateValue(I);
49   
50   // Enumerate the global variable initializers.
51   for (Module::const_global_iterator I = M->global_begin(),
52          E = M->global_end(); I != E; ++I)
53     if (I->hasInitializer())
54       EnumerateValue(I->getInitializer());
55
56   // Enumerate the aliasees.
57   for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
58        I != E; ++I)
59     EnumerateValue(I->getAliasee());
60   
61   // FIXME: Implement the 'string constant' optimization.
62
63   // Enumerate types used by the type symbol table.
64   EnumerateTypeSymbolTable(M->getTypeSymbolTable());
65
66   // Insert constants that are named at module level into the slot pool so that
67   // the module symbol table can refer to them...
68   EnumerateValueSymbolTable(M->getValueSymbolTable());
69   
70   // Enumerate types used by function bodies and argument lists.
71   for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
72     
73     for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
74          I != E; ++I)
75       EnumerateType(I->getType());
76     
77     for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
78       for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
79         for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
80              OI != E; ++OI)
81           EnumerateType((*OI)->getType());
82         EnumerateType(I->getType());
83       }
84   }
85   
86   // Sort the type table by frequency so that most commonly used types are early
87   // in the table (have low bit-width).
88   std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
89     
90   // Partition the Type ID's so that the first-class types occur before the
91   // aggregate types.  This allows the aggregate types to be dropped from the
92   // type table after parsing the global variable initializers.
93   std::partition(Types.begin(), Types.end(), isFirstClassType);
94
95   // Now that we rearranged the type table, rebuild TypeMap.
96   for (unsigned i = 0, e = Types.size(); i != e; ++i)
97     TypeMap[Types[i].first] = i+1;
98
99   // FIXME: Emit a marker into the module indicating which aggregates types can
100   // be dropped form the table.
101   
102   // FIXME: Sort value tables by frequency.
103     
104   // FIXME: Sort constants by type to reduce size.
105 }
106
107 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
108 /// table.
109 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
110   for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); 
111        TI != TE; ++TI)
112     EnumerateType(TI->second);
113 }
114
115 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
116 /// table into the values table.
117 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
118   for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end(); 
119        VI != VE; ++VI)
120     EnumerateValue(VI->getValue());
121 }
122
123 void ValueEnumerator::EnumerateValue(const Value *V) {
124   assert(V->getType() != Type::VoidTy && "Can't insert void values!");
125   
126   // Check to see if it's already in!
127   unsigned &ValueID = ValueMap[V];
128   if (ValueID) {
129     // Increment use count.
130     Values[ValueID-1].second++;
131     return;
132   }
133   
134   // Add the value.
135   Values.push_back(std::make_pair(V, 1U));
136   ValueID = Values.size();
137
138   if (const Constant *C = dyn_cast<Constant>(V)) {
139     if (isa<GlobalValue>(C)) {
140       // Initializers for globals are handled explicitly elsewhere.
141     } else {
142       // This makes sure that if a constant has uses (for example an array of
143       // const ints), that they are inserted also.
144       for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
145            I != E; ++I)
146         EnumerateValue(*I);
147     }
148   }
149
150   EnumerateType(V->getType());
151 }
152
153
154 void ValueEnumerator::EnumerateType(const Type *Ty) {
155   unsigned &TypeID = TypeMap[Ty];
156   
157   if (TypeID) {
158     // If we've already seen this type, just increase its occurrence count.
159     Types[TypeID-1].second++;
160     return;
161   }
162   
163   // First time we saw this type, add it.
164   Types.push_back(std::make_pair(Ty, 1U));
165   TypeID = Types.size();
166   
167   // Enumerate subtypes.
168   for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
169        I != E; ++I)
170     EnumerateType(*I);
171   
172   // If this is a function type, enumerate the param attrs.
173   if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty))
174     EnumerateParamAttrs(FTy->getParamAttrs());
175 }
176
177 void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) {
178   if (PAL == 0) return;  // null is always 0.
179   // Do a lookup.
180   unsigned &Entry = ParamAttrMap[PAL];
181   if (Entry == 0) {
182     // Never saw this before, add it.
183     ParamAttrs.push_back(PAL);
184     Entry = ParamAttrs.size();
185   }
186 }
187
188
189 /// PurgeAggregateValues - If there are any aggregate values at the end of the
190 /// value list, remove them and return the count of the remaining values.  If
191 /// there are none, return -1.
192 int ValueEnumerator::PurgeAggregateValues() {
193   // If there are no aggregate values at the end of the list, return -1.
194   if (Values.empty() || Values.back().first->getType()->isFirstClassType())
195     return -1;
196   
197   // Otherwise, remove aggregate values...
198   while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
199     Values.pop_back();
200   
201   // ... and return the new size.
202   return Values.size();
203 }
204
205 void ValueEnumerator::incorporateFunction(const Function &F) {
206   NumModuleValues = Values.size();
207   
208   // Adding function arguments to the value table.
209   for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
210       I != E; ++I)
211     EnumerateValue(I);
212
213   FirstFuncConstantID = Values.size();
214   
215   // Add all function-level constants to the value table.
216   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
217     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
218       for (User::const_op_iterator OI = I->op_begin(), E = I->op_end(); 
219            OI != E; ++OI) {
220         if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
221             isa<InlineAsm>(*OI))
222           EnumerateValue(*OI);
223       }
224     BasicBlocks.push_back(BB);
225     ValueMap[BB] = BasicBlocks.size();
226   }
227   
228   FirstInstID = Values.size();
229   
230   // Add all of the instructions.
231   for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
232     for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
233       if (I->getType() != Type::VoidTy)
234         EnumerateValue(I);
235     }
236   }
237 }
238
239 void ValueEnumerator::purgeFunction() {
240   /// Remove purged values from the ValueMap.
241   for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
242     ValueMap.erase(Values[i].first);
243   for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
244     ValueMap.erase(BasicBlocks[i]);
245     
246   Values.resize(NumModuleValues);
247   BasicBlocks.clear();
248 }
249