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/Module.h"
16 #include "llvm/TypeSymbolTable.h"
17 #include "llvm/ValueSymbolTable.h"
20 /// ValueEnumerator - Enumerate module-level information.
21 ValueEnumerator::ValueEnumerator(const Module *M) {
22 // Enumerate the global variables.
23 for (Module::const_global_iterator I = M->global_begin(),
24 E = M->global_end(); I != E; ++I)
27 // Enumerate the functions.
28 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
31 // Enumerate the aliases.
32 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
36 // Enumerate the global variable initializers.
37 for (Module::const_global_iterator I = M->global_begin(),
38 E = M->global_end(); I != E; ++I)
39 if (I->hasInitializer())
40 EnumerateValue(I->getInitializer());
42 // Enumerate the aliasees.
43 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
45 EnumerateValue(I->getAliasee());
47 // FIXME: Implement the 'string constant' optimization.
49 // Enumerate types used by the type symbol table.
50 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
52 // Insert constants that are named at module level into the slot pool so that
53 // the module symbol table can refer to them...
54 EnumerateValueSymbolTable(M->getValueSymbolTable());
56 // Enumerate types used by function bodies.
57 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
58 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
59 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
60 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
62 EnumerateType((*OI)->getType());
63 EnumerateType(I->getType());
68 // FIXME: std::partition the type and value tables so that first-class types
69 // come earlier than aggregates. FIXME: Emit a marker into the module
70 // indicating which aggregates types AND values can be dropped form the table.
72 // FIXME: Sort type/value tables by frequency.
74 // FIXME: Sort constants by type to reduce size.
77 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
79 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
80 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
82 EnumerateType(TI->second);
85 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
86 /// table into the values table.
87 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
88 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
90 EnumerateValue(VI->getValue());
93 void ValueEnumerator::EnumerateValue(const Value *V) {
94 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
96 // Check to see if it's already in!
97 unsigned &ValueID = ValueMap[V];
99 // Increment use count.
100 Values[ValueID-1].second++;
105 Values.push_back(std::make_pair(V, 1U));
106 ValueID = Values.size();
108 if (const Constant *C = dyn_cast<Constant>(V)) {
109 if (isa<GlobalValue>(C)) {
110 // Initializers for globals are handled explicitly elsewhere.
112 // This makes sure that if a constant has uses (for example an array of
113 // const ints), that they are inserted also.
114 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
120 EnumerateType(V->getType());
124 void ValueEnumerator::EnumerateType(const Type *Ty) {
125 unsigned &TypeID = TypeMap[Ty];
128 // If we've already seen this type, just increase its occurrence count.
129 Types[TypeID-1].second++;
133 // First time we saw this type, add it.
134 Types.push_back(std::make_pair(Ty, 1U));
135 TypeID = Types.size();
137 // Enumerate subtypes.
138 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
147 void SlotCalculator::incorporateFunction(const Function *F) {
148 SC_DEBUG("begin processFunction!\n");
150 // Iterate over function arguments, adding them to the value table...
151 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
153 CreateFunctionValueSlot(I);
155 SC_DEBUG("Inserting Instructions:\n");
157 // Add all of the instructions to the type planes...
158 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
159 CreateFunctionValueSlot(BB);
160 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
161 if (I->getType() != Type::VoidTy)
162 CreateFunctionValueSlot(I);
166 SC_DEBUG("end processFunction!\n");
169 void SlotCalculator::purgeFunction() {
170 SC_DEBUG("begin purgeFunction!\n");
172 // Next, remove values from existing type planes
173 for (DenseMap<unsigned,unsigned,
174 ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
175 E = ModuleLevel.end(); I != E; ++I) {
176 unsigned PlaneNo = I->first;
177 unsigned ModuleLev = I->second;
179 // Pop all function-local values in this type-plane off of Table.
180 TypePlane &Plane = getPlane(PlaneNo);
181 assert(ModuleLev < Plane.size() && "module levels higher than elements?");
182 for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
183 NodeMap.erase(Plane.back()); // Erase from nodemap
184 Plane.pop_back(); // Shrink plane
190 // Finally, remove any type planes defined by the function...
191 while (Table.size() > NumModuleTypes) {
192 TypePlane &Plane = Table.back();
193 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
194 << Plane.size() << "\n");
195 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
196 NodeMap.erase(Plane[i]); // Erase from nodemap
198 Table.pop_back(); // Nuke the plane, we don't like it.
201 SC_DEBUG("end purgeFunction!\n");
204 inline static bool hasImplicitNull(const Type* Ty) {
205 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
208 void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
209 assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
211 const Type *Ty = V->getType();
212 assert(Ty != Type::VoidTy && "Can't insert void values!");
213 assert(!isa<Constant>(V) && "Not a function-local value!");
215 unsigned TyPlane = getOrCreateTypeSlot(Ty);
216 if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
217 Table.resize(TyPlane+1, TypePlane());
219 // If this is the first value noticed of this type within this function,
220 // remember the module level for this type plane in ModuleLevel. This reminds
221 // us to remove the values in purgeFunction and tells us how many to remove.
222 if (TyPlane < NumModuleTypes)
223 ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
225 // If this is the first value to get inserted into the type plane, make sure
226 // to insert the implicit null value.
227 if (Table[TyPlane].empty()) {
228 // Label's and opaque types can't have a null value.
229 if (hasImplicitNull(Ty)) {
230 Value *ZeroInitializer = Constant::getNullValue(Ty);
232 // If we are pushing zeroinit, it will be handled below.
233 if (V != ZeroInitializer) {
234 Table[TyPlane].push_back(ZeroInitializer);
235 NodeMap[ZeroInitializer] = 0;
240 // Insert node into table and NodeMap...
241 NodeMap[V] = Table[TyPlane].size();
242 Table[TyPlane].push_back(V);
244 SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<