blob: ca9fc776bf07fc45d47eacc3820a1e34b9c759e8 [file] [log] [blame]
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
Chris Lattnerff7fc5d2007-05-06 00:35:24 +000015#include "llvm/Constants.h"
Chris Lattner50954f52007-05-03 22:46:43 +000016#include "llvm/DerivedTypes.h"
Devang Patel0a9f7b92009-07-28 21:49:47 +000017#include "llvm/Metadata.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000018#include "llvm/Module.h"
19#include "llvm/TypeSymbolTable.h"
20#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000021#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000022#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023using namespace llvm;
24
Dan Gohmane4977cf2008-05-23 01:55:30 +000025static bool isSingleValueType(const std::pair<const llvm::Type*,
26 unsigned int> &P) {
27 return P.first->isSingleValueType();
Chris Lattner12f535b2007-05-04 05:05:48 +000028}
29
Chris Lattner6da91d32007-05-04 05:21:47 +000030static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
31 return isa<IntegerType>(V.first->getType());
32}
33
Chris Lattner12f535b2007-05-04 05:05:48 +000034static bool CompareByFrequency(const std::pair<const llvm::Type*,
35 unsigned int> &P1,
36 const std::pair<const llvm::Type*,
37 unsigned int> &P2) {
38 return P1.second > P2.second;
39}
40
Chris Lattnerfd57cec2007-04-22 06:24:45 +000041/// ValueEnumerator - Enumerate module-level information.
42ValueEnumerator::ValueEnumerator(const Module *M) {
Devang Patele8e02132009-09-18 19:26:43 +000043 InstructionCount = 0;
44
Chris Lattnerfd57cec2007-04-22 06:24:45 +000045 // Enumerate the global variables.
46 for (Module::const_global_iterator I = M->global_begin(),
47 E = M->global_end(); I != E; ++I)
48 EnumerateValue(I);
49
50 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000051 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000052 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000053 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000054 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000055
Chris Lattner07d98b42007-04-26 02:46:40 +000056 // Enumerate the aliases.
57 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
58 I != E; ++I)
59 EnumerateValue(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +000060
Chris Lattner6da91d32007-05-04 05:21:47 +000061 // Remember what is the cutoff between globalvalue's and other constants.
62 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000063
Chris Lattnerfd57cec2007-04-22 06:24:45 +000064 // Enumerate the global variable initializers.
65 for (Module::const_global_iterator I = M->global_begin(),
66 E = M->global_end(); I != E; ++I)
67 if (I->hasInitializer())
68 EnumerateValue(I->getInitializer());
69
Chris Lattner07d98b42007-04-26 02:46:40 +000070 // Enumerate the aliasees.
71 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
72 I != E; ++I)
73 EnumerateValue(I->getAliasee());
Daniel Dunbara279bc32009-09-20 02:20:51 +000074
Chris Lattnerfd57cec2007-04-22 06:24:45 +000075 // Enumerate types used by the type symbol table.
76 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
77
78 // Insert constants that are named at module level into the slot pool so that
79 // the module symbol table can refer to them...
80 EnumerateValueSymbolTable(M->getValueSymbolTable());
Daniel Dunbara279bc32009-09-20 02:20:51 +000081
Chris Lattner8d35c792007-04-26 03:50:57 +000082 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000083 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000084
Chris Lattner8d35c792007-04-26 03:50:57 +000085 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
86 I != E; ++I)
87 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000088
Devang Patele30e6782009-09-28 21:41:20 +000089 MetadataContext &TheMetadata = F->getContext().getMetadata();
Chris Lattnerfd57cec2007-04-22 06:24:45 +000090 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
91 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000092 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +000093 OI != E; ++OI)
Chris Lattner33f1d5b2007-05-06 08:35:19 +000094 EnumerateOperandType(*OI);
Chris Lattnerfd57cec2007-04-22 06:24:45 +000095 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000096 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000097 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000098 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000099 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +0000100
Daniel Dunbara279bc32009-09-20 02:20:51 +0000101 // Enumerate metadata attached with this instruction.
Devang Patele30e6782009-09-28 21:41:20 +0000102 const MetadataContext::MDMapTy *MDs = TheMetadata.getMDs(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000103 if (MDs)
Devang Patele30e6782009-09-28 21:41:20 +0000104 for (MetadataContext::MDMapTy::const_iterator MI = MDs->begin(),
Daniel Dunbara279bc32009-09-20 02:20:51 +0000105 ME = MDs->end(); MI != ME; ++MI)
106 if (MDNode *MDN = dyn_cast_or_null<MDNode>(MI->second))
107 EnumerateMetadata(MDN);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000108 }
109 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000110
Chris Lattner6da91d32007-05-04 05:21:47 +0000111 // Optimize constant ordering.
112 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000113
Chris Lattner12f535b2007-05-04 05:05:48 +0000114 // Sort the type table by frequency so that most commonly used types are early
115 // in the table (have low bit-width).
116 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000117
Dan Gohmane4977cf2008-05-23 01:55:30 +0000118 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000119 // aggregate types. This allows the aggregate types to be dropped from the
120 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000121 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000122
123 // Now that we rearranged the type table, rebuild TypeMap.
124 for (unsigned i = 0, e = Types.size(); i != e; ++i)
125 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000126}
127
Devang Patele8e02132009-09-18 19:26:43 +0000128unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
129 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
130 assert (I != InstructionMap.end() && "Instruction is not mapped!");
131 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000132}
Devang Patele8e02132009-09-18 19:26:43 +0000133
134void ValueEnumerator::setInstructionID(const Instruction *I) {
135 InstructionMap[I] = InstructionCount++;
136}
137
Devang Pateld5ac4042009-08-04 06:00:18 +0000138unsigned ValueEnumerator::getValueID(const Value *V) const {
139 if (isa<MetadataBase>(V)) {
140 ValueMapType::const_iterator I = MDValueMap.find(V);
141 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
142 return I->second-1;
143 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000144
Devang Pateld5ac4042009-08-04 06:00:18 +0000145 ValueMapType::const_iterator I = ValueMap.find(V);
146 assert(I != ValueMap.end() && "Value not in slotcalculator!");
147 return I->second-1;
148}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000149
Chris Lattner6da91d32007-05-04 05:21:47 +0000150// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000151namespace {
152 struct CstSortPredicate {
153 ValueEnumerator &VE;
154 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
155 bool operator()(const std::pair<const Value*, unsigned> &LHS,
156 const std::pair<const Value*, unsigned> &RHS) {
157 // Sort by plane.
158 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000159 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000160 VE.getTypeID(RHS.first->getType());
161 // Then by frequency.
162 return LHS.second > RHS.second;
163 }
164 };
165}
Chris Lattner6da91d32007-05-04 05:21:47 +0000166
167/// OptimizeConstants - Reorder constant pool for denser encoding.
168void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
169 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000170
Chris Lattner6da91d32007-05-04 05:21:47 +0000171 CstSortPredicate P(*this);
172 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000173
Chris Lattner6da91d32007-05-04 05:21:47 +0000174 // Ensure that integer constants are at the start of the constant pool. This
175 // is important so that GEP structure indices come before gep constant exprs.
176 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
177 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000178
Chris Lattner6da91d32007-05-04 05:21:47 +0000179 // Rebuild the modified portion of ValueMap.
180 for (; CstStart != CstEnd; ++CstStart)
181 ValueMap[Values[CstStart].first] = CstStart+1;
182}
183
184
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000185/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
186/// table.
187void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000188 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000189 TI != TE; ++TI)
190 EnumerateType(TI->second);
191}
192
193/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
194/// table into the values table.
195void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000196 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000197 VI != VE; ++VI)
198 EnumerateValue(VI->getValue());
199}
200
Devang Pateld5ac4042009-08-04 06:00:18 +0000201void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
202 // Check to see if it's already in!
203 unsigned &MDValueID = MDValueMap[MD];
204 if (MDValueID) {
205 // Increment use count.
206 MDValues[MDValueID-1].second++;
207 return;
208 }
209
210 // Enumerate the type of this value.
211 EnumerateType(MD->getType());
212
213 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
214 MDValues.push_back(std::make_pair(MD, 1U));
215 MDValueMap[MD] = MDValues.size();
216 MDValueID = MDValues.size();
Devang Patel028fa772009-10-21 21:25:09 +0000217 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
218 if (Value *V = N->getElement(i))
219 EnumerateValue(V);
Devang Pateld5ac4042009-08-04 06:00:18 +0000220 else
Owen Anderson1d0be152009-08-13 21:58:54 +0000221 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Pateld5ac4042009-08-04 06:00:18 +0000222 }
223 return;
224 } else if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
225 for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
226 E = N->elem_end(); I != E; ++I) {
227 MetadataBase *M = *I;
228 EnumerateValue(M);
229 }
230 MDValues.push_back(std::make_pair(MD, 1U));
231 MDValueMap[MD] = Values.size();
232 return;
233 }
234
235 // Add the value.
236 MDValues.push_back(std::make_pair(MD, 1U));
237 MDValueID = MDValues.size();
238}
239
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000240void ValueEnumerator::EnumerateValue(const Value *V) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000241 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
242 "Can't insert void values!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000243 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
244 return EnumerateMetadata(MB);
245
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000246 // Check to see if it's already in!
247 unsigned &ValueID = ValueMap[V];
248 if (ValueID) {
249 // Increment use count.
250 Values[ValueID-1].second++;
251 return;
252 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000253
Chris Lattner7a303d12007-05-06 01:00:28 +0000254 // Enumerate the type of this value.
255 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000256
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000257 if (const Constant *C = dyn_cast<Constant>(V)) {
258 if (isa<GlobalValue>(C)) {
259 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000260 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
261 // Do not enumerate the initializers for an array of simple characters.
262 // The initializers just polute the value table, and we emit the strings
263 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000264 } else if (C->getNumOperands()) {
265 // If a constant has operands, enumerate them. This makes sure that if a
266 // constant has uses (for example an array of const ints), that they are
267 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000268
Chris Lattner7a303d12007-05-06 01:00:28 +0000269 // We prefer to enumerate them with values before we enumerate the user
270 // itself. This makes it more likely that we can avoid forward references
271 // in the reader. We know that there can be no cycles in the constants
272 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000273 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
274 I != E; ++I)
275 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000276
Chris Lattner7a303d12007-05-06 01:00:28 +0000277 // Finally, add the value. Doing this could make the ValueID reference be
278 // dangling, don't reuse it.
279 Values.push_back(std::make_pair(V, 1U));
280 ValueMap[V] = Values.size();
281 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000282 }
283 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000284
Chris Lattner7a303d12007-05-06 01:00:28 +0000285 // Add the value.
286 Values.push_back(std::make_pair(V, 1U));
287 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000288}
289
290
291void ValueEnumerator::EnumerateType(const Type *Ty) {
292 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000293
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000294 if (TypeID) {
295 // If we've already seen this type, just increase its occurrence count.
296 Types[TypeID-1].second++;
297 return;
298 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000299
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000300 // First time we saw this type, add it.
301 Types.push_back(std::make_pair(Ty, 1U));
302 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000303
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000304 // Enumerate subtypes.
305 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
306 I != E; ++I)
307 EnumerateType(*I);
308}
309
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000310// Enumerate the types for the specified value. If the value is a constant,
311// walk through it, enumerating the types of the constant.
312void ValueEnumerator::EnumerateOperandType(const Value *V) {
313 EnumerateType(V->getType());
314 if (const Constant *C = dyn_cast<Constant>(V)) {
315 // If this constant is already enumerated, ignore it, we know its type must
316 // be enumerated.
317 if (ValueMap.count(V)) return;
318
319 // This constant may have operands, make sure to enumerate the types in
320 // them.
321 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i)
322 EnumerateOperandType(C->getOperand(i));
Nick Lewyckycb337992009-05-10 20:57:05 +0000323
324 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Devang Patele54abc92009-07-22 17:43:22 +0000325 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
326 Value *Elem = N->getElement(i);
327 if (Elem)
328 EnumerateOperandType(Elem);
329 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000330 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000331 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000332 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000333}
334
Devang Patel05988662008-09-25 21:00:45 +0000335void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000336 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000337 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000338 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000339 if (Entry == 0) {
340 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000341 Attributes.push_back(PAL);
342 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000343 }
344}
345
346
Chris Lattner8d35c792007-04-26 03:50:57 +0000347void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000348 NumModuleValues = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000349
Chris Lattner8d35c792007-04-26 03:50:57 +0000350 // Adding function arguments to the value table.
351 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000352 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000353 EnumerateValue(I);
354
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000355 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000356
Chris Lattner8d35c792007-04-26 03:50:57 +0000357 // Add all function-level constants to the value table.
358 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
359 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000361 OI != E; ++OI) {
362 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
363 isa<InlineAsm>(*OI))
364 EnumerateValue(*OI);
365 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000366 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000367 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000368 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000369
Chris Lattner6da91d32007-05-04 05:21:47 +0000370 // Optimize the constant layout.
371 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000372
Duncan Sandsdc024672007-11-27 13:23:08 +0000373 // Add the function's parameter attributes so they are available for use in
374 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000375 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000376
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000377 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000378
Chris Lattner8d35c792007-04-26 03:50:57 +0000379 // Add all of the instructions.
380 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000381 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Owen Anderson1d0be152009-08-13 21:58:54 +0000382 if (I->getType() != Type::getVoidTy(F.getContext()))
Chris Lattner8d35c792007-04-26 03:50:57 +0000383 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000384 }
385 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000386}
387
Chris Lattner8d35c792007-04-26 03:50:57 +0000388void ValueEnumerator::purgeFunction() {
389 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000390 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000391 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000392 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
393 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000394
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000395 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000396 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000397}