1 //===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements the ValueEnumerator class.
12 //===----------------------------------------------------------------------===//
14 #include "ValueEnumerator.h"
15 #include "llvm/DerivedTypes.h"
16 #include "llvm/Module.h"
17 #include "llvm/TypeSymbolTable.h"
18 #include "llvm/ValueSymbolTable.h"
22 static bool isFirstClassType(const std::pair<const llvm::Type*,
24 return P.first->isFirstClassType();
27 static bool CompareByFrequency(const std::pair<const llvm::Type*,
29 const std::pair<const llvm::Type*,
31 return P1.second > P2.second;
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)
41 // Enumerate the functions.
42 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
45 // Enumerate the aliases.
46 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
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());
56 // Enumerate the aliasees.
57 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
59 EnumerateValue(I->getAliasee());
61 // FIXME: Implement the 'string constant' optimization.
63 // Enumerate types used by the type symbol table.
64 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
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());
70 // Enumerate types used by function bodies and argument lists.
71 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
73 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
75 EnumerateType(I->getType());
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();
81 EnumerateType((*OI)->getType());
82 EnumerateType(I->getType());
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);
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);
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;
99 // FIXME: Emit a marker into the module indicating which aggregates types can
100 // be dropped form the table.
102 // FIXME: Sort value tables by frequency.
104 // FIXME: Sort constants by type to reduce size.
107 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
109 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
110 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
112 EnumerateType(TI->second);
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();
120 EnumerateValue(VI->getValue());
123 void ValueEnumerator::EnumerateValue(const Value *V) {
124 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
126 // Check to see if it's already in!
127 unsigned &ValueID = ValueMap[V];
129 // Increment use count.
130 Values[ValueID-1].second++;
135 Values.push_back(std::make_pair(V, 1U));
136 ValueID = Values.size();
138 if (const Constant *C = dyn_cast<Constant>(V)) {
139 if (isa<GlobalValue>(C)) {
140 // Initializers for globals are handled explicitly elsewhere.
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();
150 EnumerateType(V->getType());
154 void ValueEnumerator::EnumerateType(const Type *Ty) {
155 unsigned &TypeID = TypeMap[Ty];
158 // If we've already seen this type, just increase its occurrence count.
159 Types[TypeID-1].second++;
163 // First time we saw this type, add it.
164 Types.push_back(std::make_pair(Ty, 1U));
165 TypeID = Types.size();
167 // Enumerate subtypes.
168 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
172 // If this is a function type, enumerate the param attrs.
173 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty))
174 EnumerateParamAttrs(FTy->getParamAttrs());
177 void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) {
178 if (PAL == 0) return; // null is always 0.
180 unsigned &Entry = ParamAttrMap[PAL];
182 // Never saw this before, add it.
183 ParamAttrs.push_back(PAL);
184 Entry = ParamAttrs.size();
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())
197 // Otherwise, remove aggregate values...
198 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
201 // ... and return the new size.
202 return Values.size();
205 void ValueEnumerator::incorporateFunction(const Function &F) {
206 NumModuleValues = Values.size();
208 // Adding function arguments to the value table.
209 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
213 FirstFuncConstantID = Values.size();
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();
220 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
224 BasicBlocks.push_back(BB);
225 ValueMap[BB] = BasicBlocks.size();
228 FirstInstID = Values.size();
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)
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]);
246 Values.resize(NumModuleValues);