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 global variable initializers.
32 for (Module::const_global_iterator I = M->global_begin(),
33 E = M->global_end(); I != E; ++I)
34 if (I->hasInitializer())
35 EnumerateValue(I->getInitializer());
37 // FIXME: Implement the 'string constant' optimization.
39 // Enumerate types used by the type symbol table.
40 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
42 // Insert constants that are named at module level into the slot pool so that
43 // the module symbol table can refer to them...
44 EnumerateValueSymbolTable(M->getValueSymbolTable());
46 // Enumerate types used by function bodies.
47 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
48 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
49 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
50 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
52 EnumerateType((*OI)->getType());
53 EnumerateType(I->getType());
58 // FIXME: std::partition the type and value tables so that first-class types
59 // come earlier than aggregates.
61 // FIXME: Sort type/value tables by frequency.
64 /// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
66 void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
67 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
69 EnumerateType(TI->second);
72 /// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
73 /// table into the values table.
74 void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
75 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
77 EnumerateValue(VI->getValue());
80 void ValueEnumerator::EnumerateValue(const Value *V) {
81 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
83 // Check to see if it's already in!
84 unsigned &ValueID = ValueMap[V];
86 // Increment use count.
87 Values[ValueID-1].second++;
92 Values.push_back(std::make_pair(V, 1U));
93 ValueID = Values.size();
95 if (const Constant *C = dyn_cast<Constant>(V)) {
96 if (isa<GlobalValue>(C)) {
97 // Initializers for globals are handled explicitly elsewhere.
99 // This makes sure that if a constant has uses (for example an array of
100 // const ints), that they are inserted also.
101 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
107 EnumerateType(V->getType());
111 void ValueEnumerator::EnumerateType(const Type *Ty) {
112 unsigned &TypeID = TypeMap[Ty];
115 // If we've already seen this type, just increase its occurrence count.
116 Types[TypeID-1].second++;
120 // First time we saw this type, add it.
121 Types.push_back(std::make_pair(Ty, 1U));
122 TypeID = Types.size();
124 // Enumerate subtypes.
125 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
134 void SlotCalculator::incorporateFunction(const Function *F) {
135 SC_DEBUG("begin processFunction!\n");
137 // Iterate over function arguments, adding them to the value table...
138 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
140 CreateFunctionValueSlot(I);
142 SC_DEBUG("Inserting Instructions:\n");
144 // Add all of the instructions to the type planes...
145 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
146 CreateFunctionValueSlot(BB);
147 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
148 if (I->getType() != Type::VoidTy)
149 CreateFunctionValueSlot(I);
153 SC_DEBUG("end processFunction!\n");
156 void SlotCalculator::purgeFunction() {
157 SC_DEBUG("begin purgeFunction!\n");
159 // Next, remove values from existing type planes
160 for (DenseMap<unsigned,unsigned,
161 ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
162 E = ModuleLevel.end(); I != E; ++I) {
163 unsigned PlaneNo = I->first;
164 unsigned ModuleLev = I->second;
166 // Pop all function-local values in this type-plane off of Table.
167 TypePlane &Plane = getPlane(PlaneNo);
168 assert(ModuleLev < Plane.size() && "module levels higher than elements?");
169 for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
170 NodeMap.erase(Plane.back()); // Erase from nodemap
171 Plane.pop_back(); // Shrink plane
177 // Finally, remove any type planes defined by the function...
178 while (Table.size() > NumModuleTypes) {
179 TypePlane &Plane = Table.back();
180 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
181 << Plane.size() << "\n");
182 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
183 NodeMap.erase(Plane[i]); // Erase from nodemap
185 Table.pop_back(); // Nuke the plane, we don't like it.
188 SC_DEBUG("end purgeFunction!\n");
191 inline static bool hasImplicitNull(const Type* Ty) {
192 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
195 void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
196 assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
198 const Type *Ty = V->getType();
199 assert(Ty != Type::VoidTy && "Can't insert void values!");
200 assert(!isa<Constant>(V) && "Not a function-local value!");
202 unsigned TyPlane = getOrCreateTypeSlot(Ty);
203 if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
204 Table.resize(TyPlane+1, TypePlane());
206 // If this is the first value noticed of this type within this function,
207 // remember the module level for this type plane in ModuleLevel. This reminds
208 // us to remove the values in purgeFunction and tells us how many to remove.
209 if (TyPlane < NumModuleTypes)
210 ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
212 // If this is the first value to get inserted into the type plane, make sure
213 // to insert the implicit null value.
214 if (Table[TyPlane].empty()) {
215 // Label's and opaque types can't have a null value.
216 if (hasImplicitNull(Ty)) {
217 Value *ZeroInitializer = Constant::getNullValue(Ty);
219 // If we are pushing zeroinit, it will be handled below.
220 if (V != ZeroInitializer) {
221 Table[TyPlane].push_back(ZeroInitializer);
222 NodeMap[ZeroInitializer] = 0;
227 // Insert node into table and NodeMap...
228 NodeMap[V] = Table[TyPlane].size();
229 Table[TyPlane].push_back(V);
231 SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<