blob: 37b44b1eecdfa94b36df541cb9d3b3904066949e [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
Devang Patel833d4422010-01-07 19:39:36 +000077 // Insert constants and metadata that are named at module level into the slot
78 // pool so that the module symbol table can refer to them...
Dan Gohmanf17a25c2007-07-18 16:29:46 +000079 EnumerateValueSymbolTable(M->getValueSymbolTable());
Devang Patel833d4422010-01-07 19:39:36 +000080 EnumerateMDSymbolTable(M->getMDSymbolTable());
Daniel Dunbar3be44e62009-09-20 02:20:51 +000081
Chris Lattnerd5d03a32009-12-31 00:51:46 +000082 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
83
Dan Gohmanf17a25c2007-07-18 16:29:46 +000084 // Enumerate types used by function bodies and argument lists.
85 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +000086
Dan Gohmanf17a25c2007-07-18 16:29:46 +000087 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
88 I != E; ++I)
89 EnumerateType(I->getType());
Devang Pateladc37612009-09-18 19:26:43 +000090
Dan Gohmanf17a25c2007-07-18 16:29:46 +000091 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
92 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbar3be44e62009-09-20 02:20:51 +000093 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 OI != E; ++OI)
Victor Hernandez1dda6842010-01-13 19:36:16 +000095 EnumerateOperandType(*OI, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +000096 EnumerateType(I->getType());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000097 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +000098 EnumerateAttributes(CI->getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +000099 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Pateld222f862008-09-25 21:00:45 +0000100 EnumerateAttributes(II->getAttributes());
Devang Pateladc37612009-09-18 19:26:43 +0000101
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000102 // Enumerate metadata attached with this instruction.
Devang Patel0f6f8942009-10-22 18:55:16 +0000103 MDs.clear();
Chris Lattnerdcf06572009-12-28 23:41:32 +0000104 I->getAllMetadata(MDs);
105 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandez1dda6842010-01-13 19:36:16 +0000106 EnumerateMetadata(MDs[i].second, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000107 }
108 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000109
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 // Optimize constant ordering.
111 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000112
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000113 // Sort the type table by frequency so that most commonly used types are early
114 // in the table (have low bit-width).
115 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000116
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000117 // Partition the Type ID's so that the single-value types occur before the
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 // aggregate types. This allows the aggregate types to be dropped from the
119 // type table after parsing the global variable initializers.
Dan Gohmane6b1ee62008-05-23 01:55:30 +0000120 std::partition(Types.begin(), Types.end(), isSingleValueType);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000121
122 // Now that we rearranged the type table, rebuild TypeMap.
123 for (unsigned i = 0, e = Types.size(); i != e; ++i)
124 TypeMap[Types[i].first] = i+1;
125}
126
Devang Pateladc37612009-09-18 19:26:43 +0000127unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
128 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
129 assert (I != InstructionMap.end() && "Instruction is not mapped!");
130 return I->second;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000131}
Devang Pateladc37612009-09-18 19:26:43 +0000132
133void ValueEnumerator::setInstructionID(const Instruction *I) {
134 InstructionMap[I] = InstructionCount++;
135}
136
Devang Patelea27c232009-08-04 06:00:18 +0000137unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patel89fe1312010-01-09 01:24:03 +0000138 if (isa<MetadataBase>(V)) {
Devang Patelea27c232009-08-04 06:00:18 +0000139 ValueMapType::const_iterator I = MDValueMap.find(V);
140 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
141 return I->second-1;
142 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000143
Devang Patelea27c232009-08-04 06:00:18 +0000144 ValueMapType::const_iterator I = ValueMap.find(V);
145 assert(I != ValueMap.end() && "Value not in slotcalculator!");
146 return I->second-1;
147}
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000148
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000149// Optimize constant ordering.
Dan Gohman089efff2008-05-13 00:00:25 +0000150namespace {
151 struct CstSortPredicate {
152 ValueEnumerator &VE;
153 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
154 bool operator()(const std::pair<const Value*, unsigned> &LHS,
155 const std::pair<const Value*, unsigned> &RHS) {
156 // Sort by plane.
157 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000158 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman089efff2008-05-13 00:00:25 +0000159 VE.getTypeID(RHS.first->getType());
160 // Then by frequency.
161 return LHS.second > RHS.second;
162 }
163 };
164}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165
166/// OptimizeConstants - Reorder constant pool for denser encoding.
167void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
168 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000169
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 CstSortPredicate P(*this);
171 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000172
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000173 // Ensure that integer constants are at the start of the constant pool. This
174 // is important so that GEP structure indices come before gep constant exprs.
175 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
176 isIntegerValue);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000177
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 // Rebuild the modified portion of ValueMap.
179 for (; CstStart != CstEnd; ++CstStart)
180 ValueMap[Values[CstStart].first] = CstStart+1;
181}
182
183
184/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
185/// table.
186void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000187 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188 TI != TE; ++TI)
189 EnumerateType(TI->second);
190}
191
192/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
193/// table into the values table.
194void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000195 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000196 VI != VE; ++VI)
197 EnumerateValue(VI->getValue());
198}
199
Devang Patel833d4422010-01-07 19:39:36 +0000200/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata
201/// table.
202void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) {
203 for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end();
204 MI != ME; ++MI)
205 EnumerateValue(MI->getValue());
206}
207
Devang Patel3c5c9ee2010-01-09 00:30:14 +0000208void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
209 // Check to see if it's already in!
210 unsigned &MDValueID = MDValueMap[MD];
211 if (MDValueID) {
212 // Increment use count.
213 MDValues[MDValueID-1].second++;
214 return;
215 }
216
217 // Enumerate the type of this value.
218 EnumerateType(MD->getType());
219
220 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
221 if (MDNode *E = MD->getOperand(i))
222 EnumerateValue(E);
223 MDValues.push_back(std::make_pair(MD, 1U));
224 MDValueMap[MD] = Values.size();
225}
226
Victor Hernandez1dda6842010-01-13 19:36:16 +0000227void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD, bool isGlobal) {
Devang Patelea27c232009-08-04 06:00:18 +0000228 // Check to see if it's already in!
229 unsigned &MDValueID = MDValueMap[MD];
230 if (MDValueID) {
231 // Increment use count.
232 MDValues[MDValueID-1].second++;
233 return;
234 }
235
236 // Enumerate the type of this value.
237 EnumerateType(MD->getType());
238
239 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Victor Hernandez1dda6842010-01-13 19:36:16 +0000240 if ((isGlobal && !N->isFunctionLocal()) ||
241 (!isGlobal && N->isFunctionLocal())) {
242 MDValues.push_back(std::make_pair(MD, 1U));
243 MDValueMap[MD] = MDValues.size();
244 MDValueID = MDValues.size();
245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
246 if (Value *V = N->getOperand(i))
247 EnumerateValue(V);
248 else
249 EnumerateType(Type::getVoidTy(MD->getContext()));
250 }
251 return;
Devang Patelea27c232009-08-04 06:00:18 +0000252 }
253 return;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000254 }
255
Devang Patelea27c232009-08-04 06:00:18 +0000256 // Add the value.
Chris Lattnerd5d03a32009-12-31 00:51:46 +0000257 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Patelea27c232009-08-04 06:00:18 +0000258 MDValues.push_back(std::make_pair(MD, 1U));
259 MDValueID = MDValues.size();
260}
261
Victor Hernandez1dda6842010-01-13 19:36:16 +0000262void ValueEnumerator::EnumerateValue(const Value *V, bool isGlobal) {
Chris Lattnerd5d03a32009-12-31 00:51:46 +0000263 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Patelea27c232009-08-04 06:00:18 +0000264 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
Victor Hernandez1dda6842010-01-13 19:36:16 +0000265 return EnumerateMetadata(MB, isGlobal);
Devang Patel3c5c9ee2010-01-09 00:30:14 +0000266 else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
267 return EnumerateNamedMDNode(NMD);
Devang Patelea27c232009-08-04 06:00:18 +0000268
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000269 // Check to see if it's already in!
270 unsigned &ValueID = ValueMap[V];
271 if (ValueID) {
272 // Increment use count.
273 Values[ValueID-1].second++;
274 return;
275 }
276
277 // Enumerate the type of this value.
278 EnumerateType(V->getType());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000279
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000280 if (const Constant *C = dyn_cast<Constant>(V)) {
281 if (isa<GlobalValue>(C)) {
282 // Initializers for globals are handled explicitly elsewhere.
283 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
284 // Do not enumerate the initializers for an array of simple characters.
285 // The initializers just polute the value table, and we emit the strings
286 // specially.
287 } else if (C->getNumOperands()) {
288 // If a constant has operands, enumerate them. This makes sure that if a
289 // constant has uses (for example an array of const ints), that they are
290 // inserted also.
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000291
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000292 // We prefer to enumerate them with values before we enumerate the user
293 // itself. This makes it more likely that we can avoid forward references
294 // in the reader. We know that there can be no cycles in the constants
295 // graph that don't go through a global variable.
296 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
297 I != E; ++I)
Chris Lattner620cead2009-11-01 01:27:45 +0000298 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandez1dda6842010-01-13 19:36:16 +0000299 EnumerateValue(*I, isGlobal);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000300
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000301 // Finally, add the value. Doing this could make the ValueID reference be
302 // dangling, don't reuse it.
303 Values.push_back(std::make_pair(V, 1U));
304 ValueMap[V] = Values.size();
305 return;
306 }
307 }
Devang Patel54199ef2009-07-23 01:07:34 +0000308
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000309 // Add the value.
310 Values.push_back(std::make_pair(V, 1U));
311 ValueID = Values.size();
312}
313
314
315void ValueEnumerator::EnumerateType(const Type *Ty) {
316 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000317
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000318 if (TypeID) {
319 // If we've already seen this type, just increase its occurrence count.
320 Types[TypeID-1].second++;
321 return;
322 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000323
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000324 // First time we saw this type, add it.
325 Types.push_back(std::make_pair(Ty, 1U));
326 TypeID = Types.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000327
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000328 // Enumerate subtypes.
329 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
330 I != E; ++I)
331 EnumerateType(*I);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000332}
333
334// Enumerate the types for the specified value. If the value is a constant,
335// walk through it, enumerating the types of the constant.
Victor Hernandez1dda6842010-01-13 19:36:16 +0000336void ValueEnumerator::EnumerateOperandType(const Value *V, bool isGlobal) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000337 EnumerateType(V->getType());
Victor Hernandez1dda6842010-01-13 19:36:16 +0000338
339 // During function-incorporation, only enumerate metadata operands.
340 if (!isGlobal)
341 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
342 return EnumerateMetadata(MB, isGlobal);
343
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000344 if (const Constant *C = dyn_cast<Constant>(V)) {
345 // If this constant is already enumerated, ignore it, we know its type must
346 // be enumerated.
347 if (ValueMap.count(V)) return;
348
349 // This constant may have operands, make sure to enumerate the types in
350 // them.
Chris Lattnerd5693a22009-10-28 05:24:40 +0000351 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
352 const User *Op = C->getOperand(i);
353
354 // Don't enumerate basic blocks here, this happens as operands to
355 // blockaddress.
356 if (isa<BasicBlock>(Op)) continue;
357
Victor Hernandez1dda6842010-01-13 19:36:16 +0000358 EnumerateOperandType(cast<Constant>(Op), isGlobal);
Chris Lattnerd5693a22009-10-28 05:24:40 +0000359 }
Nick Lewycky117f4382009-05-10 20:57:05 +0000360
361 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattnerece76b02009-12-31 01:22:29 +0000362 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
363 if (Value *Elem = N->getOperand(i))
Victor Hernandez1dda6842010-01-13 19:36:16 +0000364 EnumerateOperandType(Elem, isGlobal);
Nick Lewycky117f4382009-05-10 20:57:05 +0000365 }
Devang Patel54199ef2009-07-23 01:07:34 +0000366 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patel0c0a6ca2009-07-22 17:43:22 +0000367 EnumerateValue(V);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000368}
369
Devang Pateld222f862008-09-25 21:00:45 +0000370void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner1c8733e2008-03-12 17:45:29 +0000371 if (PAL.isEmpty()) return; // null is always 0.
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000372 // Do a lookup.
Devang Pateld222f862008-09-25 21:00:45 +0000373 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000374 if (Entry == 0) {
375 // Never saw this before, add it.
Devang Pateld222f862008-09-25 21:00:45 +0000376 Attributes.push_back(PAL);
377 Entry = Attributes.size();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000378 }
379}
380
381
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000382void ValueEnumerator::incorporateFunction(const Function &F) {
383 NumModuleValues = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000384
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000385 // Adding function arguments to the value table.
386 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
387 I != E; ++I)
388 EnumerateValue(I);
389
390 FirstFuncConstantID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000391
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000392 // Add all function-level constants to the value table.
393 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
394 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000395 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000396 OI != E; ++OI) {
397 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
398 isa<InlineAsm>(*OI))
399 EnumerateValue(*OI);
400 }
401 BasicBlocks.push_back(BB);
402 ValueMap[BB] = BasicBlocks.size();
403 }
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000404
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000405 // Optimize the constant layout.
406 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000407
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000408 // Add the function's parameter attributes so they are available for use in
409 // the function's instruction.
Devang Pateld222f862008-09-25 21:00:45 +0000410 EnumerateAttributes(F.getAttributes());
Duncan Sandsf5588dc2007-11-27 13:23:08 +0000411
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000412 FirstInstID = Values.size();
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000413
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000414 // Add all of the instructions.
415 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
416 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandez1dda6842010-01-13 19:36:16 +0000417 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
418 OI != E; ++OI) {
419 EnumerateOperandType(*OI, false);
420 }
Benjamin Kramerf2052d52010-01-05 13:12:22 +0000421 if (!I->getType()->isVoidTy())
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000422 EnumerateValue(I);
423 }
424 }
425}
426
427void ValueEnumerator::purgeFunction() {
428 /// Remove purged values from the ValueMap.
429 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
430 ValueMap.erase(Values[i].first);
431 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
432 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar3be44e62009-09-20 02:20:51 +0000433
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000434 Values.resize(NumModuleValues);
435 BasicBlocks.clear();
436}
Chris Lattnerd5693a22009-10-28 05:24:40 +0000437
438static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
439 DenseMap<const BasicBlock*, unsigned> &IDMap) {
440 unsigned Counter = 0;
441 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
442 IDMap[BB] = ++Counter;
443}
444
445/// getGlobalBasicBlockID - This returns the function-specific ID for the
446/// specified basic block. This is relatively expensive information, so it
447/// should only be used by rare constructs such as address-of-label.
448unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
449 unsigned &Idx = GlobalBasicBlockIDs[BB];
450 if (Idx != 0)
Chris Lattner620cead2009-11-01 01:27:45 +0000451 return Idx-1;
Chris Lattnerd5693a22009-10-28 05:24:40 +0000452
453 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
454 return getGlobalBasicBlockID(BB);
455}
456