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/Constants.h"
16 #include "llvm/DerivedTypes.h"
17 #include "llvm/Module.h"
18 #include "llvm/TypeSymbolTable.h"
19 #include "llvm/ValueSymbolTable.h"
23 static bool isFirstClassType(const std::pair<const llvm::Type*,
25 return P.first->isFirstClassType();
28 static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
29 return isa<IntegerType>(V.first->getType());
32 static bool CompareByFrequency(const std::pair<const llvm::Type*,
34 const std::pair<const llvm::Type*,
36 return P1.second > P2.second;
39 /// ValueEnumerator - Enumerate module-level information.
40 ValueEnumerator::ValueEnumerator(const Module *M) {
41 // Enumerate the global variables.
42 for (Module::const_global_iterator I = M->global_begin(),
43 E = M->global_end(); I != E; ++I)
46 // Enumerate the functions.
47 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
50 // Enumerate the aliases.
51 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
55 // Remember what is the cutoff between globalvalue's and other constants.
56 unsigned FirstConstant = Values.size();
58 // Enumerate the global variable initializers.
59 for (Module::const_global_iterator I = M->global_begin(),
60 E = M->global_end(); I != E; ++I)
61 if (I->hasInitializer())
62 EnumerateValue(I->getInitializer());
64 // Enumerate the aliasees.
65 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
67 EnumerateValue(I->getAliasee());
69 // Enumerate types used by the type symbol table.
70 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
72 // Insert constants that are named at module level into the slot pool so that
73 // the module symbol table can refer to them...
74 EnumerateValueSymbolTable(M->getValueSymbolTable());
76 // Enumerate types used by function bodies and argument lists.
77 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
79 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
81 EnumerateType(I->getType());
83 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
84 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
85 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
87 EnumerateType((*OI)->getType());
88 EnumerateType(I->getType());
92 // Optimize constant ordering.
93 OptimizeConstants(FirstConstant, Values.size());
95 // Sort the type table by frequency so that most commonly used types are early
96 // in the table (have low bit-width).
97 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
99 // Partition the Type ID's so that the first-class types occur before the
100 // aggregate types. This allows the aggregate types to be dropped from the
101 // type table after parsing the global variable initializers.
102 std::partition(Types.begin(), Types.end(), isFirstClassType);
104 // Now that we rearranged the type table, rebuild TypeMap.
105 for (unsigned i = 0, e = Types.size(); i != e; ++i)
106 TypeMap[Types[i].first] = i+1;
109 // Optimize constant ordering.
110 struct CstSortPredicate {
112 CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
113 bool operator()(const std::pair<const Value*, unsigned> &LHS,
114 const std::pair<const Value*, unsigned> &RHS) {
116 if (LHS.first->getType() != RHS.first->getType())
117 return VE.getTypeID(LHS.first->getType()) <
118 VE.getTypeID(RHS.first->getType());
119 // Then by frequency.
120 return LHS.second > RHS.second;
124 /// OptimizeConstants - Reorder constant pool for denser encoding.
125 void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
126 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
128 CstSortPredicate P(*this);
129 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
131 // Ensure that integer constants are at the start of the constant pool. This
132 // is important so that GEP structure indices come before gep constant exprs.
133 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
136 // Rebuild the modified portion of ValueMap.
137 for (; CstStart != CstEnd; ++CstStart)
138 ValueMap[Values[CstStart].first] = CstStart+1;
142 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
144 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
145 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
147 EnumerateType(TI->second);
150 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
151 /// table into the values table.
152 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
153 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
155 EnumerateValue(VI->getValue());
158 void ValueEnumerator::EnumerateValue(const Value *V) {
159 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
161 // Check to see if it's already in!
162 unsigned &ValueID = ValueMap[V];
164 // Increment use count.
165 Values[ValueID-1].second++;
170 Values.push_back(std::make_pair(V, 1U));
171 ValueID = Values.size();
173 if (const Constant *C = dyn_cast<Constant>(V)) {
174 if (isa<GlobalValue>(C)) {
175 // Initializers for globals are handled explicitly elsewhere.
176 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
177 // Do not enumerate the initializers for an array of simple characters.
178 // The initializers just polute the value table, and we emit the strings
181 // This makes sure that if a constant has uses (for example an array of
182 // const ints), that they are inserted also.
183 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
189 EnumerateType(V->getType());
193 void ValueEnumerator::EnumerateType(const Type *Ty) {
194 unsigned &TypeID = TypeMap[Ty];
197 // If we've already seen this type, just increase its occurrence count.
198 Types[TypeID-1].second++;
202 // First time we saw this type, add it.
203 Types.push_back(std::make_pair(Ty, 1U));
204 TypeID = Types.size();
206 // Enumerate subtypes.
207 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
211 // If this is a function type, enumerate the param attrs.
212 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty))
213 EnumerateParamAttrs(FTy->getParamAttrs());
216 void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) {
217 if (PAL == 0) return; // null is always 0.
219 unsigned &Entry = ParamAttrMap[PAL];
221 // Never saw this before, add it.
222 ParamAttrs.push_back(PAL);
223 Entry = ParamAttrs.size();
228 /// PurgeAggregateValues - If there are any aggregate values at the end of the
229 /// value list, remove them and return the count of the remaining values. If
230 /// there are none, return -1.
231 int ValueEnumerator::PurgeAggregateValues() {
232 // If there are no aggregate values at the end of the list, return -1.
233 if (Values.empty() || Values.back().first->getType()->isFirstClassType())
236 // Otherwise, remove aggregate values...
237 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
240 // ... and return the new size.
241 return Values.size();
244 void ValueEnumerator::incorporateFunction(const Function &F) {
245 NumModuleValues = Values.size();
247 // Adding function arguments to the value table.
248 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
252 FirstFuncConstantID = Values.size();
254 // Add all function-level constants to the value table.
255 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
256 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
257 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
259 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
263 BasicBlocks.push_back(BB);
264 ValueMap[BB] = BasicBlocks.size();
267 // Optimize the constant layout.
268 OptimizeConstants(FirstFuncConstantID, Values.size());
270 FirstInstID = Values.size();
272 // Add all of the instructions.
273 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
274 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
275 if (I->getType() != Type::VoidTy)
281 void ValueEnumerator::purgeFunction() {
282 /// Remove purged values from the ValueMap.
283 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
284 ValueMap.erase(Values[i].first);
285 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
286 ValueMap.erase(BasicBlocks[i]);
288 Values.resize(NumModuleValues);