blob: c409c2066d13266d1124dd3f151d8c6246e42012 [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"
17#include "llvm/Module.h"
18#include "llvm/TypeSymbolTable.h"
19#include "llvm/ValueSymbolTable.h"
Duncan Sandsf5588dc2007-11-27 13:23:08 +000020#include "llvm/Instructions.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000021#include <algorithm>
22using namespace llvm;
23
Dan Gohmane6b1ee62008-05-23 01:55:30 +000024static bool isSingleValueType(const std::pair<const llvm::Type*,
25 unsigned int> &P) {
26 return P.first->isSingleValueType();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000027}
28
29static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
30 return isa<IntegerType>(V.first->getType());
31}
32
33static bool CompareByFrequency(const std::pair<const llvm::Type*,
34 unsigned int> &P1,
35 const std::pair<const llvm::Type*,
36 unsigned int> &P2) {
37 return P1.second > P2.second;
38}
39
40/// ValueEnumerator - Enumerate module-level information.
41ValueEnumerator::ValueEnumerator(const Module *M) {
Devang Pateladc37612009-09-18 19:26:43 +000042 InstructionCount = 0;
43
Dan Gohmanf17a25c2007-07-18 16:29:46 +000044 // Enumerate the global variables.
45 for (Module::const_global_iterator I = M->global_begin(),
46 E = M->global_end(); I != E; ++I)
47 EnumerateValue(I);
48
49 // Enumerate the functions.
Duncan Sandsf5588dc2007-11-27 13:23:08 +000050 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000051 EnumerateValue(I);
Devang Pateld222f862008-09-25 21:00:45 +000052 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000053 }
Dan Gohmanf17a25c2007-07-18 16:29:46 +000054
55 // Enumerate the aliases.
56 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
57 I != E; ++I)
58 EnumerateValue(I);
Daniel Dunbar3be44e62009-09-20 02:20:51 +000059
Dan Gohmanf17a25c2007-07-18 16:29:46 +000060 // Remember what is the cutoff between globalvalue's and other constants.
61 unsigned FirstConstant = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +000062
Dan Gohmanf17a25c2007-07-18 16:29:46 +000063 // Enumerate the global variable initializers.
64 for (Module::const_global_iterator I = M->global_begin(),
65 E = M->global_end(); I != E; ++I)
66 if (I->hasInitializer())
67 EnumerateValue(I->getInitializer());
68
69 // Enumerate the aliasees.
70 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
71 I != E; ++I)
72 EnumerateValue(I->getAliasee());
Daniel Dunbar3be44e62009-09-20 02:20:51 +000073
Dan Gohmanf17a25c2007-07-18 16:29:46 +000074 // Enumerate types used by the type symbol table.
75 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
76
77 // Insert constants that are named at module level into the slot pool so that
78 // the module symbol table can refer to them...
79 EnumerateValueSymbolTable(M->getValueSymbolTable());
Daniel Dunbar3be44e62009-09-20 02:20:51 +000080
Chris Lattnerd5d03a32009-12-31 00:51:46 +000081 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
82
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
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Dunbar3be44e62009-09-20 02:20:51 +000092 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000093 OI != E; ++OI)
94 EnumerateOperandType(*OI);
95 EnumerateType(I->getType());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000096 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +000097 EnumerateAttributes(CI->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000098 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +000099 EnumerateAttributes(II->getAttributes());
Devang Pateladc37612009-09-18 19:26:43 +0000100
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000101 // Enumerate metadata attached with this instruction.
Devang Patel0f6f8942009-10-22 18:55:16 +0000102 MDs.clear();
Chris Lattnerdcf06572009-12-28 23:41:32 +0000103 I->getAllMetadata(MDs);
104 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
105 EnumerateMetadata(MDs[i].second);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106 }
107 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000108
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000109 // Optimize constant ordering.
110 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000111
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 // Sort the type table by frequency so that most commonly used types are early
113 // in the table (have low bit-width).
114 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000115
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000116 // Partition the Type ID's so that the single-value types occur before the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 // aggregate types. This allows the aggregate types to be dropped from the
118 // type table after parsing the global variable initializers.
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000119 std::partition(Types.begin(), Types.end(), isSingleValueType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000120
121 // Now that we rearranged the type table, rebuild TypeMap.
122 for (unsigned i = 0, e = Types.size(); i != e; ++i)
123 TypeMap[Types[i].first] = i+1;
124}
125
Devang Pateladc37612009-09-18 19:26:43 +0000126unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
127 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
128 assert (I != InstructionMap.end() && "Instruction is not mapped!");
129 return I->second;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000130}
Devang Pateladc37612009-09-18 19:26:43 +0000131
132void ValueEnumerator::setInstructionID(const Instruction *I) {
133 InstructionMap[I] = InstructionCount++;
134}
135
Devang Patelea27c232009-08-04 06:00:18 +0000136unsigned ValueEnumerator::getValueID(const Value *V) const {
137 if (isa<MetadataBase>(V)) {
138 ValueMapType::const_iterator I = MDValueMap.find(V);
139 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
140 return I->second-1;
141 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000142
Devang Patelea27c232009-08-04 06:00:18 +0000143 ValueMapType::const_iterator I = ValueMap.find(V);
144 assert(I != ValueMap.end() && "Value not in slotcalculator!");
145 return I->second-1;
146}
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000147
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000148// Optimize constant ordering.
Dan Gohman089efff2008-05-13 00:00:25 +0000149namespace {
150 struct CstSortPredicate {
151 ValueEnumerator &VE;
152 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
153 bool operator()(const std::pair<const Value*, unsigned> &LHS,
154 const std::pair<const Value*, unsigned> &RHS) {
155 // Sort by plane.
156 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000157 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman089efff2008-05-13 00:00:25 +0000158 VE.getTypeID(RHS.first->getType());
159 // Then by frequency.
160 return LHS.second > RHS.second;
161 }
162 };
163}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164
165/// OptimizeConstants - Reorder constant pool for denser encoding.
166void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
167 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000168
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000169 CstSortPredicate P(*this);
170 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000171
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000172 // Ensure that integer constants are at the start of the constant pool. This
173 // is important so that GEP structure indices come before gep constant exprs.
174 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
175 isIntegerValue);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000176
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000177 // Rebuild the modified portion of ValueMap.
178 for (; CstStart != CstEnd; ++CstStart)
179 ValueMap[Values[CstStart].first] = CstStart+1;
180}
181
182
183/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
184/// table.
185void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000186 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000187 TI != TE; ++TI)
188 EnumerateType(TI->second);
189}
190
191/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
192/// table into the values table.
193void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000194 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000195 VI != VE; ++VI)
196 EnumerateValue(VI->getValue());
197}
198
Devang Patelea27c232009-08-04 06:00:18 +0000199void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD) {
200 // Check to see if it's already in!
201 unsigned &MDValueID = MDValueMap[MD];
202 if (MDValueID) {
203 // Increment use count.
204 MDValues[MDValueID-1].second++;
205 return;
206 }
207
208 // Enumerate the type of this value.
209 EnumerateType(MD->getType());
210
211 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
212 MDValues.push_back(std::make_pair(MD, 1U));
213 MDValueMap[MD] = MDValues.size();
214 MDValueID = MDValues.size();
Chris Lattnerece76b02009-12-31 01:22:29 +0000215 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
216 if (Value *V = N->getOperand(i))
Devang Patel26c7b902009-10-21 21:25:09 +0000217 EnumerateValue(V);
Devang Patelea27c232009-08-04 06:00:18 +0000218 else
Owen Anderson35b47072009-08-13 21:58:54 +0000219 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Patelea27c232009-08-04 06:00:18 +0000220 }
221 return;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000222 }
223
224 if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
Chris Lattnerece76b02009-12-31 01:22:29 +0000225 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Devang Patel148981c2010-01-05 21:47:32 +0000226 if (MDNode *E = N->getOperand(i))
227 EnumerateValue(E);
Devang Patelea27c232009-08-04 06:00:18 +0000228 MDValues.push_back(std::make_pair(MD, 1U));
229 MDValueMap[MD] = Values.size();
230 return;
231 }
232
233 // Add the value.
Chris Lattnerd5d03a32009-12-31 00:51:46 +0000234 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Patelea27c232009-08-04 06:00:18 +0000235 MDValues.push_back(std::make_pair(MD, 1U));
236 MDValueID = MDValues.size();
237}
238
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000239void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnerd5d03a32009-12-31 00:51:46 +0000240 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Patelea27c232009-08-04 06:00:18 +0000241 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
242 return EnumerateMetadata(MB);
243
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000244 // Check to see if it's already in!
245 unsigned &ValueID = ValueMap[V];
246 if (ValueID) {
247 // Increment use count.
248 Values[ValueID-1].second++;
249 return;
250 }
251
252 // Enumerate the type of this value.
253 EnumerateType(V->getType());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000254
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000255 if (const Constant *C = dyn_cast<Constant>(V)) {
256 if (isa<GlobalValue>(C)) {
257 // Initializers for globals are handled explicitly elsewhere.
258 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
259 // Do not enumerate the initializers for an array of simple characters.
260 // The initializers just polute the value table, and we emit the strings
261 // specially.
262 } else if (C->getNumOperands()) {
263 // If a constant has operands, enumerate them. This makes sure that if a
264 // constant has uses (for example an array of const ints), that they are
265 // inserted also.
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000266
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000267 // We prefer to enumerate them with values before we enumerate the user
268 // itself. This makes it more likely that we can avoid forward references
269 // in the reader. We know that there can be no cycles in the constants
270 // graph that don't go through a global variable.
271 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
272 I != E; ++I)
Chris Lattner620cead2009-11-01 01:27:45 +0000273 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
274 EnumerateValue(*I);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000275
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000276 // Finally, add the value. Doing this could make the ValueID reference be
277 // dangling, don't reuse it.
278 Values.push_back(std::make_pair(V, 1U));
279 ValueMap[V] = Values.size();
280 return;
281 }
282 }
Devang Patel54199ef2009-07-23 01:07:34 +0000283
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000284 // Add the value.
285 Values.push_back(std::make_pair(V, 1U));
286 ValueID = Values.size();
287}
288
289
290void ValueEnumerator::EnumerateType(const Type *Ty) {
291 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000292
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000293 if (TypeID) {
294 // If we've already seen this type, just increase its occurrence count.
295 Types[TypeID-1].second++;
296 return;
297 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000298
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000299 // First time we saw this type, add it.
300 Types.push_back(std::make_pair(Ty, 1U));
301 TypeID = Types.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000302
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000303 // Enumerate subtypes.
304 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
305 I != E; ++I)
306 EnumerateType(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000307}
308
309// Enumerate the types for the specified value. If the value is a constant,
310// walk through it, enumerating the types of the constant.
311void ValueEnumerator::EnumerateOperandType(const Value *V) {
312 EnumerateType(V->getType());
313 if (const Constant *C = dyn_cast<Constant>(V)) {
314 // If this constant is already enumerated, ignore it, we know its type must
315 // be enumerated.
316 if (ValueMap.count(V)) return;
317
318 // This constant may have operands, make sure to enumerate the types in
319 // them.
Chris Lattnerd5693a22009-10-28 05:24:40 +0000320 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
321 const User *Op = C->getOperand(i);
322
323 // Don't enumerate basic blocks here, this happens as operands to
324 // blockaddress.
325 if (isa<BasicBlock>(Op)) continue;
326
327 EnumerateOperandType(cast<Constant>(Op));
328 }
Nick Lewycky117f4382009-05-10 20:57:05 +0000329
330 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattnerece76b02009-12-31 01:22:29 +0000331 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
332 if (Value *Elem = N->getOperand(i))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000333 EnumerateOperandType(Elem);
Nick Lewycky117f4382009-05-10 20:57:05 +0000334 }
Devang Patel54199ef2009-07-23 01:07:34 +0000335 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000336 EnumerateValue(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337}
338
Devang Pateld222f862008-09-25 21:00:45 +0000339void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000340 if (PAL.isEmpty()) return; // null is always 0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000341 // Do a lookup.
Devang Pateld222f862008-09-25 21:00:45 +0000342 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000343 if (Entry == 0) {
344 // Never saw this before, add it.
Devang Pateld222f862008-09-25 21:00:45 +0000345 Attributes.push_back(PAL);
346 Entry = Attributes.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000347 }
348}
349
350
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000351void ValueEnumerator::incorporateFunction(const Function &F) {
352 NumModuleValues = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000353
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000354 // Adding function arguments to the value table.
355 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
356 I != E; ++I)
357 EnumerateValue(I);
358
359 FirstFuncConstantID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000360
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000361 // Add all function-level constants to the value table.
362 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
363 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000364 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000365 OI != E; ++OI) {
366 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
367 isa<InlineAsm>(*OI))
368 EnumerateValue(*OI);
369 }
370 BasicBlocks.push_back(BB);
371 ValueMap[BB] = BasicBlocks.size();
372 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000373
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 // Optimize the constant layout.
375 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000376
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000377 // Add the function's parameter attributes so they are available for use in
378 // the function's instruction.
Devang Pateld222f862008-09-25 21:00:45 +0000379 EnumerateAttributes(F.getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000380
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000381 FirstInstID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000382
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000383 // Add all of the instructions.
384 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
385 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Benjamin Kramerf2052d52010-01-05 13:12:22 +0000386 if (!I->getType()->isVoidTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000387 EnumerateValue(I);
388 }
389 }
390}
391
392void ValueEnumerator::purgeFunction() {
393 /// Remove purged values from the ValueMap.
394 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
395 ValueMap.erase(Values[i].first);
396 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
397 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000398
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000399 Values.resize(NumModuleValues);
400 BasicBlocks.clear();
401}
Chris Lattnerd5693a22009-10-28 05:24:40 +0000402
403static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
404 DenseMap<const BasicBlock*, unsigned> &IDMap) {
405 unsigned Counter = 0;
406 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
407 IDMap[BB] = ++Counter;
408}
409
410/// getGlobalBasicBlockID - This returns the function-specific ID for the
411/// specified basic block. This is relatively expensive information, so it
412/// should only be used by rare constructs such as address-of-label.
413unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
414 unsigned &Idx = GlobalBasicBlockIDs[BB];
415 if (Idx != 0)
Chris Lattner620cead2009-11-01 01:27:45 +0000416 return Idx-1;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000417
418 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
419 return getGlobalBasicBlockID(BB);
420}
421