blob: c22dbf7d54379f0c2a7d3fa47183a7f9767038a2 [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
Chris Lattner831d4202007-04-26 03:27:58 +0000143/// PurgeAggregateValues - If there are any aggregate values at the end of the
144/// value list, remove them and return the count of the remaining values. If
145/// there are none, return -1.
146int ValueEnumerator::PurgeAggregateValues() {
147 // If there are no aggregate values at the end of the list, return -1.
148 if (Values.empty() || Values.back().first->getType()->isFirstClassType())
149 return -1;
150
151 // Otherwise, remove aggregate values...
152 while (!Values.empty() && !Values.back().first->getType()->isFirstClassType())
153 Values.pop_back();
154
155 // ... and return the new size.
156 return Values.size();
157}
158
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000159
160
161#if 0
162
163void SlotCalculator::incorporateFunction(const Function *F) {
164 SC_DEBUG("begin processFunction!\n");
165
166 // Iterate over function arguments, adding them to the value table...
167 for(Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
168 I != E; ++I)
169 CreateFunctionValueSlot(I);
170
171 SC_DEBUG("Inserting Instructions:\n");
172
173 // Add all of the instructions to the type planes...
174 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB) {
175 CreateFunctionValueSlot(BB);
176 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
177 if (I->getType() != Type::VoidTy)
178 CreateFunctionValueSlot(I);
179 }
180 }
181
182 SC_DEBUG("end processFunction!\n");
183}
184
185void SlotCalculator::purgeFunction() {
186 SC_DEBUG("begin purgeFunction!\n");
187
188 // Next, remove values from existing type planes
189 for (DenseMap<unsigned,unsigned,
190 ModuleLevelDenseMapKeyInfo>::iterator I = ModuleLevel.begin(),
191 E = ModuleLevel.end(); I != E; ++I) {
192 unsigned PlaneNo = I->first;
193 unsigned ModuleLev = I->second;
194
195 // Pop all function-local values in this type-plane off of Table.
196 TypePlane &Plane = getPlane(PlaneNo);
197 assert(ModuleLev < Plane.size() && "module levels higher than elements?");
198 for (unsigned i = ModuleLev, e = Plane.size(); i != e; ++i) {
199 NodeMap.erase(Plane.back()); // Erase from nodemap
200 Plane.pop_back(); // Shrink plane
201 }
202 }
203
204 ModuleLevel.clear();
205
206 // Finally, remove any type planes defined by the function...
207 while (Table.size() > NumModuleTypes) {
208 TypePlane &Plane = Table.back();
209 SC_DEBUG("Removing Plane " << (Table.size()-1) << " of size "
210 << Plane.size() << "\n");
211 for (unsigned i = 0, e = Plane.size(); i != e; ++i)
212 NodeMap.erase(Plane[i]); // Erase from nodemap
213
214 Table.pop_back(); // Nuke the plane, we don't like it.
215 }
216
217 SC_DEBUG("end purgeFunction!\n");
218}
219
220inline static bool hasImplicitNull(const Type* Ty) {
221 return Ty != Type::LabelTy && Ty != Type::VoidTy && !isa<OpaqueType>(Ty);
222}
223
224void SlotCalculator::CreateFunctionValueSlot(const Value *V) {
225 assert(!NodeMap.count(V) && "Function-local value can't be inserted!");
226
227 const Type *Ty = V->getType();
228 assert(Ty != Type::VoidTy && "Can't insert void values!");
229 assert(!isa<Constant>(V) && "Not a function-local value!");
230
231 unsigned TyPlane = getOrCreateTypeSlot(Ty);
232 if (Table.size() <= TyPlane) // Make sure we have the type plane allocated.
233 Table.resize(TyPlane+1, TypePlane());
234
235 // If this is the first value noticed of this type within this function,
236 // remember the module level for this type plane in ModuleLevel. This reminds
237 // us to remove the values in purgeFunction and tells us how many to remove.
238 if (TyPlane < NumModuleTypes)
239 ModuleLevel.insert(std::make_pair(TyPlane, Table[TyPlane].size()));
240
241 // If this is the first value to get inserted into the type plane, make sure
242 // to insert the implicit null value.
243 if (Table[TyPlane].empty()) {
244 // Label's and opaque types can't have a null value.
245 if (hasImplicitNull(Ty)) {
246 Value *ZeroInitializer = Constant::getNullValue(Ty);
247
248 // If we are pushing zeroinit, it will be handled below.
249 if (V != ZeroInitializer) {
250 Table[TyPlane].push_back(ZeroInitializer);
251 NodeMap[ZeroInitializer] = 0;
252 }
253 }
254 }
255
256 // Insert node into table and NodeMap...
257 NodeMap[V] = Table[TyPlane].size();
258 Table[TyPlane].push_back(V);
259
260 SC_DEBUG(" Inserting value [" << TyPlane << "] = " << *V << " slot=" <<
261 NodeMap[V] << "\n");
262}
263
264#endif