blob: 1172c4fa219b167d49a353c663aa6b0152792887 [file] [log] [blame]
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
15#include "llvm/Module.h"
16#include "llvm/TypeSymbolTable.h"
17#include "llvm/ValueSymbolTable.h"
18using namespace llvm;
19
20/// ValueEnumerator - Enumerate module-level information.
21ValueEnumerator::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)
25 EnumerateValue(I);
26
27 // Enumerate the functions.
28 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
29 EnumerateValue(I);
30
Chris Lattner44c17072007-04-26 02:46:40 +000031 // Enumerate the aliases.
32 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
33 I != E; ++I)
34 EnumerateValue(I);
35
Chris Lattnerc1d10d62007-04-22 06:24:45 +000036 // 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());
41
Chris Lattner44c17072007-04-26 02:46:40 +000042 // Enumerate the aliasees.
43 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
44 I != E; ++I)
45 EnumerateValue(I->getAliasee());
46
Chris Lattnerc1d10d62007-04-22 06:24:45 +000047 // FIXME: Implement the 'string constant' optimization.
48
49 // Enumerate types used by the type symbol table.
50 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
51
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());
55
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();
61 OI != E; ++OI)
62 EnumerateType((*OI)->getType());
63 EnumerateType(I->getType());
64 }
65 }
66
67
68 // FIXME: std::partition the type and value tables so that first-class types
Chris Lattner52523562007-04-24 00:16:04 +000069 // come earlier than aggregates. FIXME: Emit a marker into the module
70 // indicating which aggregates types AND values can be dropped form the table.
Chris Lattnerc1d10d62007-04-22 06:24:45 +000071
72 // FIXME: Sort type/value tables by frequency.
Chris Lattner52523562007-04-24 00:16:04 +000073
74 // FIXME: Sort constants by type to reduce size.
Chris Lattnerc1d10d62007-04-22 06:24:45 +000075}
76
77/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
78/// table.
79void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
80 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
81 TI != TE; ++TI)
82 EnumerateType(TI->second);
83}
84
85/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
86/// table into the values table.
87void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
88 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
89 VI != VE; ++VI)
90 EnumerateValue(VI->getValue());
91}
92
93void ValueEnumerator::EnumerateValue(const Value *V) {
94 assert(V->getType() != Type::VoidTy && "Can't insert void values!");
95
96 // Check to see if it's already in!
97 unsigned &ValueID = ValueMap[V];
98 if (ValueID) {
99 // Increment use count.
100 Values[ValueID-1].second++;
101 return;
102 }
103
104 // Add the value.
105 Values.push_back(std::make_pair(V, 1U));
106 ValueID = Values.size();
107
108 if (const Constant *C = dyn_cast<Constant>(V)) {
109 if (isa<GlobalValue>(C)) {
110 // Initializers for globals are handled explicitly elsewhere.
111 } else {
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();
115 I != E; ++I)
116 EnumerateValue(*I);
117 }
118 }
119
120 EnumerateType(V->getType());
121}
122
123
124void ValueEnumerator::EnumerateType(const Type *Ty) {
125 unsigned &TypeID = TypeMap[Ty];
126
127 if (TypeID) {
128 // If we've already seen this type, just increase its occurrence count.
129 Types[TypeID-1].second++;
130 return;
131 }
132
133 // First time we saw this type, add it.
134 Types.push_back(std::make_pair(Ty, 1U));
135 TypeID = Types.size();
136
137 // Enumerate subtypes.
138 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
139 I != E; ++I)
140 EnumerateType(*I);
141}
142
143
144
145#if 0
146
147void SlotCalculator::incorporateFunction(const Function *F) {
148 SC_DEBUG("begin processFunction!\n");
149
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();
152 I != E; ++I)
153 CreateFunctionValueSlot(I);
154
155 SC_DEBUG("Inserting Instructions:\n");
156
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);
163 }
164 }
165
166 SC_DEBUG("end processFunction!\n");
167}
168
169void SlotCalculator::purgeFunction() {
170 SC_DEBUG("begin purgeFunction!\n");
171
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;
178
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
185 }
186 }
187
188 ModuleLevel.clear();
189
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
197
198 Table.pop_back(); // Nuke the plane, we don't like it.
199 }
200
201 SC_DEBUG("end purgeFunction!\n");
202}
203
204inline static bool hasImplicitNull(const Type* Ty) {
205 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
206}
207
208void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
209 assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
210
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!");
214
215 unsigned TyPlane = getOrCreateTypeSlot(Ty);
216 if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
217 Table.resize(TyPlane+1, TypePlane());
218
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()));
224
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);
231
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;
236 }
237 }
238 }
239
240 // Insert node into table and NodeMap...
241 NodeMap[V] = Table[TyPlane].size();
242 Table[TyPlane].push_back(V);
243
244 SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
245 NodeMap[V] << "\n");
246}
247
248#endif