blob: d840d4ae9fe9f869d7e3c55d9bd5d06452d43ae9 [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//
Chris Lattnerf3ebc3f2007-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 Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
Chris Lattner036d1bd2007-05-06 00:35:24 +000015#include "llvm/Constants.h"
Chris Lattnere4bbad62007-05-03 22:46:43 +000016#include "llvm/DerivedTypes.h"
Chris Lattner26a7ae42009-10-27 17:08:31 +000017#include "llvm/LLVMContext.h"
Devang Patela4f43fb2009-07-28 21:49:47 +000018#include "llvm/Metadata.h"
Chris Lattnerc1d10d62007-04-22 06:24:45 +000019#include "llvm/Module.h"
20#include "llvm/TypeSymbolTable.h"
21#include "llvm/ValueSymbolTable.h"
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000022#include "llvm/Instructions.h"
Chris Lattnera8713be2007-05-04 05:05:48 +000023#include <algorithm>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000024using namespace llvm;
25
Dan Gohman30499842008-05-23 01:55:30 +000026static bool isSingleValueType(const std::pair<const llvm::Type*,
27 unsigned int> &P) {
28 return P.first->isSingleValueType();
Chris Lattnera8713be2007-05-04 05:05:48 +000029}
30
Chris Lattner430e80d2007-05-04 05:21:47 +000031static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
32 return isa<IntegerType>(V.first->getType());
33}
34
Chris Lattnera8713be2007-05-04 05:05:48 +000035static 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
Chris Lattnerc1d10d62007-04-22 06:24:45 +000042/// ValueEnumerator - Enumerate module-level information.
43ValueEnumerator::ValueEnumerator(const Module *M) {
Devang Patelaf206b82009-09-18 19:26:43 +000044 InstructionCount = 0;
45
Chris Lattnerc1d10d62007-04-22 06:24:45 +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 Sandsad0ea2d2007-11-27 13:23:08 +000052 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +000053 EnumerateValue(I);
Devang Patel4c758ea2008-09-25 21:00:45 +000054 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000055 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +000056
Chris Lattner44c17072007-04-26 02:46:40 +000057 // 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 Dunbar7d6781b2009-09-20 02:20:51 +000061
Chris Lattner430e80d2007-05-04 05:21:47 +000062 // Remember what is the cutoff between globalvalue's and other constants.
63 unsigned FirstConstant = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000064
Chris Lattnerc1d10d62007-04-22 06:24:45 +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
Chris Lattner44c17072007-04-26 02:46:40 +000071 // 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 Dunbar7d6781b2009-09-20 02:20:51 +000075
Chris Lattnerc1d10d62007-04-22 06:24:45 +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 Dunbar7d6781b2009-09-20 02:20:51 +000082
Chris Lattner5f640b92007-04-26 03:50:57 +000083 // Enumerate types used by function bodies and argument lists.
Chris Lattnerc1d10d62007-04-22 06:24:45 +000084 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +000085
Chris Lattner5f640b92007-04-26 03:50:57 +000086 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
87 I != E; ++I)
88 EnumerateType(I->getType());
Devang Patelaf206b82009-09-18 19:26:43 +000089
Devang Patel2d85eef2009-09-28 21:41:20 +000090 MetadataContext &TheMetadata = F->getContext().getMetadata();
Devang Patel6da5dbf2009-10-22 18:55:16 +000091 typedef SmallVector<std::pair<unsigned, TrackingVH<MDNode> >, 2> MDMapTy;
92 MDMapTy MDs;
Chris Lattnerc1d10d62007-04-22 06:24:45 +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 Dunbar7d6781b2009-09-20 02:20:51 +000095 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +000096 OI != E; ++OI)
Chris Lattner76fd90f2007-05-06 08:35:19 +000097 EnumerateOperandType(*OI);
Chris Lattnerc1d10d62007-04-22 06:24:45 +000098 EnumerateType(I->getType());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +000099 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000100 EnumerateAttributes(CI->getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000101 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000102 EnumerateAttributes(II->getAttributes());
Devang Patelaf206b82009-09-18 19:26:43 +0000103
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000104 // Enumerate metadata attached with this instruction.
Devang Patel6da5dbf2009-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);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000110 }
111 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000112
Chris Lattner430e80d2007-05-04 05:21:47 +0000113 // Optimize constant ordering.
114 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000115
Chris Lattnera8713be2007-05-04 05:05:48 +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 Dunbar7d6781b2009-09-20 02:20:51 +0000119
Dan Gohman30499842008-05-23 01:55:30 +0000120 // Partition the Type ID's so that the single-value types occur before the
Chris Lattnera8713be2007-05-04 05:05:48 +0000121 // aggregate types. This allows the aggregate types to be dropped from the
122 // type table after parsing the global variable initializers.
Dan Gohman30499842008-05-23 01:55:30 +0000123 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattnera8713be2007-05-04 05:05:48 +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;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000128}
129
Devang Patelaf206b82009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000134}
Devang Patelaf206b82009-09-18 19:26:43 +0000135
136void ValueEnumerator::setInstructionID(const Instruction *I) {
137 InstructionMap[I] = InstructionCount++;
138}
139
Devang Patel05eb6172009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000146
Devang Patel05eb6172009-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 Dunbar7d6781b2009-09-20 02:20:51 +0000151
Chris Lattner430e80d2007-05-04 05:21:47 +0000152// Optimize constant ordering.
Dan Gohmand78c4002008-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 Dunbar7d6781b2009-09-20 02:20:51 +0000161 return VE.getTypeID(LHS.first->getType()) <
Dan Gohmand78c4002008-05-13 00:00:25 +0000162 VE.getTypeID(RHS.first->getType());
163 // Then by frequency.
164 return LHS.second > RHS.second;
165 }
166 };
167}
Chris Lattner430e80d2007-05-04 05:21:47 +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 Dunbar7d6781b2009-09-20 02:20:51 +0000172
Chris Lattner430e80d2007-05-04 05:21:47 +0000173 CstSortPredicate P(*this);
174 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000175
Chris Lattner430e80d2007-05-04 05:21:47 +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 Dunbar7d6781b2009-09-20 02:20:51 +0000180
Chris Lattner430e80d2007-05-04 05:21:47 +0000181 // Rebuild the modified portion of ValueMap.
182 for (; CstStart != CstEnd; ++CstStart)
183 ValueMap[Values[CstStart].first] = CstStart+1;
184}
185
186
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000187/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
188/// table.
189void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000190 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +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 Dunbar7d6781b2009-09-20 02:20:51 +0000198 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000199 VI != VE; ++VI)
200 EnumerateValue(VI->getValue());
201}
202
Devang Patel05eb6172009-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 Patel49914e6e2009-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 Patel05eb6172009-08-04 06:00:18 +0000222 else
Owen Anderson55f1c092009-08-13 21:58:54 +0000223 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Patel05eb6172009-08-04 06:00:18 +0000224 }
225 return;
Chris Lattnerf540d742009-10-28 05:24:40 +0000226 }
227
228 if (const NamedMDNode *N = dyn_cast<NamedMDNode>(MD)) {
Devang Patel05eb6172009-08-04 06:00:18 +0000229 for(NamedMDNode::const_elem_iterator I = N->elem_begin(),
230 E = N->elem_end(); I != E; ++I) {
231 MetadataBase *M = *I;
232 EnumerateValue(M);
233 }
234 MDValues.push_back(std::make_pair(MD, 1U));
235 MDValueMap[MD] = Values.size();
236 return;
237 }
238
239 // Add the value.
240 MDValues.push_back(std::make_pair(MD, 1U));
241 MDValueID = MDValues.size();
242}
243
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000244void ValueEnumerator::EnumerateValue(const Value *V) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000245 assert(V->getType() != Type::getVoidTy(V->getContext()) &&
246 "Can't insert void values!");
Devang Patel05eb6172009-08-04 06:00:18 +0000247 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
248 return EnumerateMetadata(MB);
249
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000250 // Check to see if it's already in!
251 unsigned &ValueID = ValueMap[V];
252 if (ValueID) {
253 // Increment use count.
254 Values[ValueID-1].second++;
255 return;
256 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000257
Chris Lattner9ee48362007-05-06 01:00:28 +0000258 // Enumerate the type of this value.
259 EnumerateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000260
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000261 if (const Constant *C = dyn_cast<Constant>(V)) {
262 if (isa<GlobalValue>(C)) {
263 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner036d1bd2007-05-06 00:35:24 +0000264 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
265 // Do not enumerate the initializers for an array of simple characters.
266 // The initializers just polute the value table, and we emit the strings
267 // specially.
Chris Lattner9ee48362007-05-06 01:00:28 +0000268 } else if (C->getNumOperands()) {
269 // If a constant has operands, enumerate them. This makes sure that if a
270 // constant has uses (for example an array of const ints), that they are
271 // inserted also.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000272
Chris Lattner9ee48362007-05-06 01:00:28 +0000273 // We prefer to enumerate them with values before we enumerate the user
274 // itself. This makes it more likely that we can avoid forward references
275 // in the reader. We know that there can be no cycles in the constants
276 // graph that don't go through a global variable.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000277 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
278 I != E; ++I)
Chris Lattnerf540d742009-10-28 05:24:40 +0000279 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
280 EnumerateValue(*I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000281
Chris Lattner9ee48362007-05-06 01:00:28 +0000282 // Finally, add the value. Doing this could make the ValueID reference be
283 // dangling, don't reuse it.
284 Values.push_back(std::make_pair(V, 1U));
285 ValueMap[V] = Values.size();
286 return;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000287 }
288 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000289
Chris Lattner9ee48362007-05-06 01:00:28 +0000290 // Add the value.
291 Values.push_back(std::make_pair(V, 1U));
292 ValueID = Values.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000293}
294
295
296void ValueEnumerator::EnumerateType(const Type *Ty) {
297 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000298
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000299 if (TypeID) {
300 // If we've already seen this type, just increase its occurrence count.
301 Types[TypeID-1].second++;
302 return;
303 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000304
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000305 // First time we saw this type, add it.
306 Types.push_back(std::make_pair(Ty, 1U));
307 TypeID = Types.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000308
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000309 // Enumerate subtypes.
310 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
311 I != E; ++I)
312 EnumerateType(*I);
313}
314
Chris Lattner76fd90f2007-05-06 08:35:19 +0000315// Enumerate the types for the specified value. If the value is a constant,
316// walk through it, enumerating the types of the constant.
317void ValueEnumerator::EnumerateOperandType(const Value *V) {
318 EnumerateType(V->getType());
319 if (const Constant *C = dyn_cast<Constant>(V)) {
320 // If this constant is already enumerated, ignore it, we know its type must
321 // be enumerated.
322 if (ValueMap.count(V)) return;
323
324 // This constant may have operands, make sure to enumerate the types in
325 // them.
Chris Lattnerf540d742009-10-28 05:24:40 +0000326 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
327 const User *Op = C->getOperand(i);
328
329 // Don't enumerate basic blocks here, this happens as operands to
330 // blockaddress.
331 if (isa<BasicBlock>(Op)) continue;
332
333 EnumerateOperandType(cast<Constant>(Op));
334 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000335
336 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattnerf540d742009-10-28 05:24:40 +0000337 for (unsigned i = 0, e = N->getNumElements(); i != e; ++i)
338 if (Value *Elem = N->getElement(i))
Devang Patel7428d8a2009-07-22 17:43:22 +0000339 EnumerateOperandType(Elem);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000340 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000341 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patel7428d8a2009-07-22 17:43:22 +0000342 EnumerateValue(V);
Chris Lattner76fd90f2007-05-06 08:35:19 +0000343}
344
Devang Patel4c758ea2008-09-25 21:00:45 +0000345void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000346 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattnere4bbad62007-05-03 22:46:43 +0000347 // Do a lookup.
Devang Patel4c758ea2008-09-25 21:00:45 +0000348 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattnere4bbad62007-05-03 22:46:43 +0000349 if (Entry == 0) {
350 // Never saw this before, add it.
Devang Patel4c758ea2008-09-25 21:00:45 +0000351 Attributes.push_back(PAL);
352 Entry = Attributes.size();
Chris Lattnere4bbad62007-05-03 22:46:43 +0000353 }
354}
355
356
Chris Lattner5f640b92007-04-26 03:50:57 +0000357void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnere6e364c2007-04-26 05:53:54 +0000358 NumModuleValues = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000359
Chris Lattner5f640b92007-04-26 03:50:57 +0000360 // Adding function arguments to the value table.
361 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000362 I != E; ++I)
Chris Lattner5f640b92007-04-26 03:50:57 +0000363 EnumerateValue(I);
364
Chris Lattnere6e364c2007-04-26 05:53:54 +0000365 FirstFuncConstantID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000366
Chris Lattner5f640b92007-04-26 03:50:57 +0000367 // Add all function-level constants to the value table.
368 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
369 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000370 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner5f640b92007-04-26 03:50:57 +0000371 OI != E; ++OI) {
372 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
373 isa<InlineAsm>(*OI))
374 EnumerateValue(*OI);
375 }
Chris Lattner7c37b012007-04-26 04:42:16 +0000376 BasicBlocks.push_back(BB);
Chris Lattner6be58c62007-05-03 22:18:21 +0000377 ValueMap[BB] = BasicBlocks.size();
Chris Lattner5f640b92007-04-26 03:50:57 +0000378 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000379
Chris Lattner430e80d2007-05-04 05:21:47 +0000380 // Optimize the constant layout.
381 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000382
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000383 // Add the function's parameter attributes so they are available for use in
384 // the function's instruction.
Devang Patel4c758ea2008-09-25 21:00:45 +0000385 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000386
Chris Lattnere6e364c2007-04-26 05:53:54 +0000387 FirstInstID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000388
Chris Lattner5f640b92007-04-26 03:50:57 +0000389 // Add all of the instructions.
390 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000391 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Owen Anderson55f1c092009-08-13 21:58:54 +0000392 if (I->getType() != Type::getVoidTy(F.getContext()))
Chris Lattner5f640b92007-04-26 03:50:57 +0000393 EnumerateValue(I);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000394 }
395 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000396}
397
Chris Lattner5f640b92007-04-26 03:50:57 +0000398void ValueEnumerator::purgeFunction() {
399 /// Remove purged values from the ValueMap.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000400 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner5f640b92007-04-26 03:50:57 +0000401 ValueMap.erase(Values[i].first);
Chris Lattner7c37b012007-04-26 04:42:16 +0000402 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
403 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404
Chris Lattnere6e364c2007-04-26 05:53:54 +0000405 Values.resize(NumModuleValues);
Chris Lattner7c37b012007-04-26 04:42:16 +0000406 BasicBlocks.clear();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000407}
Chris Lattnerf540d742009-10-28 05:24:40 +0000408
409static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
410 DenseMap<const BasicBlock*, unsigned> &IDMap) {
411 unsigned Counter = 0;
412 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
413 IDMap[BB] = ++Counter;
414}
415
416/// getGlobalBasicBlockID - This returns the function-specific ID for the
417/// specified basic block. This is relatively expensive information, so it
418/// should only be used by rare constructs such as address-of-label.
419unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
420 unsigned &Idx = GlobalBasicBlockIDs[BB];
421 if (Idx != 0)
422 return Idx-1;
423
424 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
425 return getGlobalBasicBlockID(BB);
426}
427