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"
21 /// ValueEnumerator - Enumerate module-level information.
22 ValueEnumerator::ValueEnumerator(const Module *M) {
23 // Enumerate the global variables.
24 for (Module::const_global_iterator I = M->global_begin(),
25 E = M->global_end(); I != E; ++I)
28 // Enumerate the functions.
29 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
32 // Enumerate the aliases.
33 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
37 // Enumerate the global variable initializers.
38 for (Module::const_global_iterator I = M->global_begin(),
39 E = M->global_end(); I != E; ++I)
40 if (I->hasInitializer())
41 EnumerateValue(I->getInitializer());
43 // Enumerate the aliasees.
44 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
46 EnumerateValue(I->getAliasee());
48 // FIXME: Implement the 'string constant' optimization.
50 // Enumerate types used by the type symbol table.
51 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
53 // Insert constants that are named at module level into the slot pool so that
54 // the module symbol table can refer to them...
55 EnumerateValueSymbolTable(M->getValueSymbolTable());
57 // Enumerate types used by function bodies and argument lists.
58 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
60 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
62 EnumerateType(I->getType());
64 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
65 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
66 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
68 EnumerateType((*OI)->getType());
69 EnumerateType(I->getType());
74 // FIXME: std::partition the type and value tables so that first-class types
75 // come earlier than aggregates. FIXME: Emit a marker into the module
76 // indicating which aggregates types AND values can be dropped form the table.
78 // FIXME: Sort type/value tables by frequency.
80 // FIXME: Sort constants by type to reduce size.
83 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
85 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
86 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
88 EnumerateType(TI->second);
91 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
92 /// table into the values table.
93 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
94 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
96 EnumerateValue(VI->getValue());
99 void ValueEnumerator::EnumerateValue(const Value *V) {
100 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
102 // Check to see if it's already in!
103 unsigned &ValueID = ValueMap[V];
105 // Increment use count.
106 Values[ValueID-1].second++;
111 Values.push_back(std::make_pair(V, 1U));
112 ValueID = Values.size();
114 if (const Constant *C = dyn_cast<Constant>(V)) {
115 if (isa<GlobalValue>(C)) {
116 // Initializers for globals are handled explicitly elsewhere.
118 // This makes sure that if a constant has uses (for example an array of
119 // const ints), that they are inserted also.
120 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
126 EnumerateType(V->getType());
130 void ValueEnumerator::EnumerateType(const Type *Ty) {
131 unsigned &TypeID = TypeMap[Ty];
134 // If we've already seen this type, just increase its occurrence count.
135 Types[TypeID-1].second++;
139 // First time we saw this type, add it.
140 Types.push_back(std::make_pair(Ty, 1U));
141 TypeID = Types.size();
143 // Enumerate subtypes.
144 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
148 // If this is a function type, enumerate the param attrs.
149 if (const FunctionType *FTy = dyn_cast<FunctionType>(Ty))
150 EnumerateParamAttrs(FTy->getParamAttrs());
153 void ValueEnumerator::EnumerateParamAttrs(const ParamAttrsList *PAL) {
154 if (PAL == 0) return; // null is always 0.
156 unsigned &Entry = ParamAttrMap[PAL];
158 // Never saw this before, add it.
159 ParamAttrs.push_back(PAL);
160 Entry = ParamAttrs.size();
165 /// PurgeAggregateValues - If there are any aggregate values at the end of the
166 /// value list, remove them and return the count of the remaining values. If
167 /// there are none, return -1.
168 int ValueEnumerator::PurgeAggregateValues() {
169 // If there are no aggregate values at the end of the list, return -1.
170 if (Values.empty() || Values.back().first->getType()->isFirstClassType())
173 // Otherwise, remove aggregate values...
174 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
177 // ... and return the new size.
178 return Values.size();
181 void ValueEnumerator::incorporateFunction(const Function &F) {
182 NumModuleValues = Values.size();
184 // Adding function arguments to the value table.
185 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
189 FirstFuncConstantID = Values.size();
191 // Add all function-level constants to the value table.
192 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
193 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
194 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
196 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
200 BasicBlocks.push_back(BB);
201 ValueMap[BB] = BasicBlocks.size();
204 FirstInstID = Values.size();
206 // Add all of the instructions.
207 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
208 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
209 if (I->getType() != Type::VoidTy)
215 void ValueEnumerator::purgeFunction() {
216 /// Remove purged values from the ValueMap.
217 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
218 ValueMap.erase(Values[i].first);
219 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
220 ValueMap.erase(BasicBlocks[i]);
222 Values.resize(NumModuleValues);