blob: 29c6d374da9e81730fa44fc84bf7ddf8fb06cc05 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
15#include "llvm/Constants.h"
16#include "llvm/DerivedTypes.h"
Chris Lattner85c4ec22009-10-27 17:08:31 +000017#include "llvm/LLVMContext.h"
Devang Patel7c368852009-07-28 21:49:47 +000018#include "llvm/Metadata.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000019#include "llvm/Module.h"
20#include "llvm/TypeSymbolTable.h"
21#include "llvm/ValueSymbolTable.h"
Duncan Sandsf5588dc2007-11-27 13:23:08 +000022#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000023#include <algorithm>
24using namespace llvm;
25
Dan Gohmane6b1ee62008-05-23 01:55:30 +000026static bool isSingleValueType(const std::pair<const llvm::Type*,
27 unsigned int> &P) {
28 return P.first->isSingleValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000029}
30
31static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
32 return isa<IntegerType>(V.first->getType());
33}
34
35static bool CompareByFrequency(const std::pair<const llvm::Type*,
36 unsigned int> &P1,
37 const std::pair<const llvm::Type*,
38 unsigned int> &P2) {
39 return P1.second > P2.second;
40}
41
42/// ValueEnumerator - Enumerate module-level information.
43ValueEnumerator::ValueEnumerator(const Module *M) {
Devang Pateladc37612009-09-18 19:26:43 +000044 InstructionCount = 0;
45
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046 // Enumerate the global variables.
47 for (Module::const_global_iterator I = M->global_begin(),
48 E = M->global_end(); I != E; ++I)
49 EnumerateValue(I);
50
51 // Enumerate the functions.
Duncan Sandsf5588dc2007-11-27 13:23:08 +000052 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 EnumerateValue(I);
Devang Pateld222f862008-09-25 21:00:45 +000054 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000055 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000056
57 // Enumerate the aliases.
58 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
59 I != E; ++I)
60 EnumerateValue(I);
Daniel Dunbar3be44e62009-09-20 02:20:51 +000061
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 // Remember what is the cutoff between globalvalue's and other constants.
63 unsigned FirstConstant = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +000064
Dan Gohmanf17a25c2007-07-18 16:29:46 +000065 // Enumerate the global variable initializers.
66 for (Module::const_global_iterator I = M->global_begin(),
67 E = M->global_end(); I != E; ++I)
68 if (I->hasInitializer())
69 EnumerateValue(I->getInitializer());
70
71 // Enumerate the aliasees.
72 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
73 I != E; ++I)
74 EnumerateValue(I->getAliasee());
Daniel Dunbar3be44e62009-09-20 02:20:51 +000075
Dan Gohmanf17a25c2007-07-18 16:29:46 +000076 // Enumerate types used by the type symbol table.
77 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
78
79 // Insert constants that are named at module level into the slot pool so that
80 // the module symbol table can refer to them...
81 EnumerateValueSymbolTable(M->getValueSymbolTable());
Daniel Dunbar3be44e62009-09-20 02:20:51 +000082
Dan Gohmanf17a25c2007-07-18 16:29:46 +000083 // Enumerate types used by function bodies and argument lists.
84 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +000085
Dan Gohmanf17a25c2007-07-18 16:29:46 +000086 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
87 I != E; ++I)
88 EnumerateType(I->getType());
Devang Pateladc37612009-09-18 19:26:43 +000089
Devang Patel6de78e22009-09-28 21:41:20 +000090 MetadataContext &TheMetadata = F->getContext().getMetadata();
Chris Lattner1b6d97f2009-12-28 08:14:54 +000091 typedef SmallVector<std::pair<unsigned, MDNode*>, 2> MDMapTy;
Devang Patel0f6f8942009-10-22 18:55:16 +000092 MDMapTy MDs;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
94 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbar3be44e62009-09-20 02:20:51 +000095 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 OI != E; ++OI)
97 EnumerateOperandType(*OI);
98 EnumerateType(I->getType());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000099 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +0000100 EnumerateAttributes(CI->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000101 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +0000102 EnumerateAttributes(II->getAttributes());
Devang Pateladc37612009-09-18 19:26:43 +0000103
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000104 // Enumerate metadata attached with this instruction.
Devang Patel0f6f8942009-10-22 18:55:16 +0000105 MDs.clear();
106 TheMetadata.getMDs(I, MDs);
107 for (MDMapTy::const_iterator MI = MDs.begin(), ME = MDs.end(); MI != ME;
108 ++MI)
109 EnumerateMetadata(MI->second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 }
111 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 // Optimize constant ordering.
114 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 // Sort the type table by frequency so that most commonly used types are early
117 // in the table (have low bit-width).
118 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000119
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000120 // Partition the Type ID's so that the single-value types occur before the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121 // aggregate types. This allows the aggregate types to be dropped from the
122 // type table after parsing the global variable initializers.
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000123 std::partition(Types.begin(), Types.end(), isSingleValueType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000124
125 // Now that we rearranged the type table, rebuild TypeMap.
126 for (unsigned i = 0, e = Types.size(); i != e; ++i)
127 TypeMap[Types[i].first] = i+1;
128}
129
Devang Pateladc37612009-09-18 19:26:43 +0000130unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
131 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
132 assert (I != InstructionMap.end() && "Instruction is not mapped!");
133 return I->second;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000134}
Devang Pateladc37612009-09-18 19:26:43 +0000135
136void ValueEnumerator::setInstructionID(const Instruction *I) {
137 InstructionMap[I] = InstructionCount++;
138}
139
Devang Patelea27c232009-08-04 06:00:18 +0000140unsigned ValueEnumerator::getValueID(const Value *V) const {
141 if (isa<MetadataBase>(V)) {
142 ValueMapType::const_iterator I = MDValueMap.find(V);
143 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
144 return I->second-1;
145 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000146
Devang Patelea27c232009-08-04 06:00:18 +0000147 ValueMapType::const_iterator I = ValueMap.find(V);
148 assert(I != ValueMap.end() && "Value not in slotcalculator!");
149 return I->second-1;
150}
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000151
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000152// Optimize constant ordering.
Dan Gohman089efff2008-05-13 00:00:25 +0000153namespace {
154 struct CstSortPredicate {
155 ValueEnumerator &VE;
156 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
157 bool operator()(const std::pair<const Value*, unsigned> &LHS,
158 const std::pair<const Value*, unsigned> &RHS) {
159 // Sort by plane.
160 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000161 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman089efff2008-05-13 00:00:25 +0000162 VE.getTypeID(RHS.first->getType());
163 // Then by frequency.
164 return LHS.second > RHS.second;
165 }
166 };
167}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168
169/// OptimizeConstants - Reorder constant pool for denser encoding.
170void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
171 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 CstSortPredicate P(*this);
174 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000175
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000176 // Ensure that integer constants are at the start of the constant pool. This
177 // is important so that GEP structure indices come before gep constant exprs.
178 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
179 isIntegerValue);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000180
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000181 // Rebuild the modified portion of ValueMap.
182 for (; CstStart != CstEnd; ++CstStart)
183 ValueMap[Values[CstStart].first] = CstStart+1;
184}
185
186
187/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
188/// table.
189void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000190 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000191 TI != TE; ++TI)
192 EnumerateType(TI->second);
193}
194
195/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
196/// table into the values table.
197void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000198 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000199 VI != VE; ++VI)
200 EnumerateValue(VI->getValue());
201}
202
Devang Patelea27c232009-08-04 06:00:18 +0000203void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
204 // Check to see if it's already in!
205 unsigned &MDValueID = MDValueMap[MD];
206 if (MDValueID) {
207 // Increment use count.
208 MDValues[MDValueID-1].second++;
209 return;
210 }
211
212 // Enumerate the type of this value.
213 EnumerateType(MD->getType());
214
215 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
216 MDValues.push_back(std::make_pair(MD, 1U));
217 MDValueMap[MD] = MDValues.size();
218 MDValueID = MDValues.size();
Devang Patel26c7b902009-10-21 21:25:09 +0000219 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i) {
220 if (Value *V = N->getElement(i))
221 EnumerateValue(V);
Devang Patelea27c232009-08-04 06:00:18 +0000222 else
Owen Anderson35b47072009-08-13 21:58:54 +0000223 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Patelea27c232009-08-04 06:00:18 +0000224 }
225 return;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000226 }
227
228 if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
Chris Lattner59a5a2e2009-12-28 07:57:01 +0000229 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
230 EnumerateValue(N->getElement(i));
Devang Patelea27c232009-08-04 06:00:18 +0000231 MDValues.push_back(std::make_pair(MD, 1U));
232 MDValueMap[MD] = Values.size();
233 return;
234 }
235
236 // Add the value.
237 MDValues.push_back(std::make_pair(MD, 1U));
238 MDValueID = MDValues.size();
239}
240
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000241void ValueEnumerator::EnumerateValue(const Value *V) {
Owen Anderson35b47072009-08-13 21:58:54 +0000242 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
243 "Can't insert void values!");
Devang Patelea27c232009-08-04 06:00:18 +0000244 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
245 return EnumerateMetadata(MB);
246
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000247 // Check to see if it's already in!
248 unsigned &ValueID = ValueMap[V];
249 if (ValueID) {
250 // Increment use count.
251 Values[ValueID-1].second++;
252 return;
253 }
254
255 // Enumerate the type of this value.
256 EnumerateType(V->getType());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000257
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000258 if (const Constant *C = dyn_cast<Constant>(V)) {
259 if (isa<GlobalValue>(C)) {
260 // Initializers for globals are handled explicitly elsewhere.
261 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
262 // Do not enumerate the initializers for an array of simple characters.
263 // The initializers just polute the value table, and we emit the strings
264 // specially.
265 } else if (C->getNumOperands()) {
266 // If a constant has operands, enumerate them. This makes sure that if a
267 // constant has uses (for example an array of const ints), that they are
268 // inserted also.
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000269
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000270 // We prefer to enumerate them with values before we enumerate the user
271 // itself. This makes it more likely that we can avoid forward references
272 // in the reader. We know that there can be no cycles in the constants
273 // graph that don't go through a global variable.
274 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
275 I != E; ++I)
Chris Lattner620cead2009-11-01 01:27:45 +0000276 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
277 EnumerateValue(*I);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000278
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000279 // Finally, add the value. Doing this could make the ValueID reference be
280 // dangling, don't reuse it.
281 Values.push_back(std::make_pair(V, 1U));
282 ValueMap[V] = Values.size();
283 return;
284 }
285 }
Devang Patel54199ef2009-07-23 01:07:34 +0000286
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000287 // Add the value.
288 Values.push_back(std::make_pair(V, 1U));
289 ValueID = Values.size();
290}
291
292
293void ValueEnumerator::EnumerateType(const Type *Ty) {
294 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000295
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000296 if (TypeID) {
297 // If we've already seen this type, just increase its occurrence count.
298 Types[TypeID-1].second++;
299 return;
300 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000301
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000302 // First time we saw this type, add it.
303 Types.push_back(std::make_pair(Ty, 1U));
304 TypeID = Types.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000305
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000306 // Enumerate subtypes.
307 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
308 I != E; ++I)
309 EnumerateType(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000310}
311
312// Enumerate the types for the specified value. If the value is a constant,
313// walk through it, enumerating the types of the constant.
314void ValueEnumerator::EnumerateOperandType(const Value *V) {
315 EnumerateType(V->getType());
316 if (const Constant *C = dyn_cast<Constant>(V)) {
317 // If this constant is already enumerated, ignore it, we know its type must
318 // be enumerated.
319 if (ValueMap.count(V)) return;
320
321 // This constant may have operands, make sure to enumerate the types in
322 // them.
Chris Lattnerd5693a22009-10-28 05:24:40 +0000323 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
324 const User *Op = C->getOperand(i);
325
326 // Don't enumerate basic blocks here, this happens as operands to
327 // blockaddress.
328 if (isa<BasicBlock>(Op)) continue;
329
330 EnumerateOperandType(cast<Constant>(Op));
331 }
Nick Lewycky117f4382009-05-10 20:57:05 +0000332
333 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattnerd5693a22009-10-28 05:24:40 +0000334 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
335 if (Value *Elem = N->getElement(i))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000336 EnumerateOperandType(Elem);
Nick Lewycky117f4382009-05-10 20:57:05 +0000337 }
Devang Patel54199ef2009-07-23 01:07:34 +0000338 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000339 EnumerateValue(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000340}
341
Devang Pateld222f862008-09-25 21:00:45 +0000342void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000343 if (PAL.isEmpty()) return; // null is always 0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 // Do a lookup.
Devang Pateld222f862008-09-25 21:00:45 +0000345 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000346 if (Entry == 0) {
347 // Never saw this before, add it.
Devang Pateld222f862008-09-25 21:00:45 +0000348 Attributes.push_back(PAL);
349 Entry = Attributes.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000350 }
351}
352
353
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354void ValueEnumerator::incorporateFunction(const Function &F) {
355 NumModuleValues = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000356
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000357 // Adding function arguments to the value table.
358 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
359 I != E; ++I)
360 EnumerateValue(I);
361
362 FirstFuncConstantID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000363
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000364 // Add all function-level constants to the value table.
365 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
366 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000367 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368 OI != E; ++OI) {
369 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
370 isa<InlineAsm>(*OI))
371 EnumerateValue(*OI);
372 }
373 BasicBlocks.push_back(BB);
374 ValueMap[BB] = BasicBlocks.size();
375 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000376
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000377 // Optimize the constant layout.
378 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000379
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000380 // Add the function's parameter attributes so they are available for use in
381 // the function's instruction.
Devang Pateld222f862008-09-25 21:00:45 +0000382 EnumerateAttributes(F.getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000383
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000384 FirstInstID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000385
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000386 // Add all of the instructions.
387 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
388 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Owen Anderson35b47072009-08-13 21:58:54 +0000389 if (I->getType() != Type::getVoidTy(F.getContext()))
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000390 EnumerateValue(I);
391 }
392 }
393}
394
395void ValueEnumerator::purgeFunction() {
396 /// Remove purged values from the ValueMap.
397 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
398 ValueMap.erase(Values[i].first);
399 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
400 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000401
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000402 Values.resize(NumModuleValues);
403 BasicBlocks.clear();
404}
Chris Lattnerd5693a22009-10-28 05:24:40 +0000405
406static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
407 DenseMap<const BasicBlock*, unsigned> &IDMap) {
408 unsigned Counter = 0;
409 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
410 IDMap[BB] = ++Counter;
411}
412
413/// getGlobalBasicBlockID - This returns the function-specific ID for the
414/// specified basic block. This is relatively expensive information, so it
415/// should only be used by rare constructs such as address-of-label.
416unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
417 unsigned &Idx = GlobalBasicBlockIDs[BB];
418 if (Idx != 0)
Chris Lattner620cead2009-11-01 01:27:45 +0000419 return Idx-1;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000420
421 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
422 return getGlobalBasicBlockID(BB);
423}
424