blob: 15f8f6cdb0d8ae603c6b792e43afcc3992cc2914 [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"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000017#include "llvm/Module.h"
18#include "llvm/TypeSymbolTable.h"
19#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000020#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000021#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000022using namespace llvm;
23
Dan Gohmane4977cf2008-05-23 01:55:30 +000024static bool isSingleValueType(const std::pair<const llvm::Type*,
25 unsigned int> &P) {
26 return P.first->isSingleValueType();
Chris Lattner12f535b2007-05-04 05:05:48 +000027}
28
Chris Lattner6da91d32007-05-04 05:21:47 +000029static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
Duncan Sands1df98592010-02-16 11:11:14 +000030 return V.first->getType()->isIntegerTy();
Chris Lattner6da91d32007-05-04 05:21:47 +000031}
32
Chris Lattner12f535b2007-05-04 05:05:48 +000033static 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
Chris Lattnerfd57cec2007-04-22 06:24:45 +000040/// ValueEnumerator - Enumerate module-level information.
41ValueEnumerator::ValueEnumerator(const Module *M) {
42 // Enumerate the global variables.
43 for (Module::const_global_iterator I = M->global_begin(),
44 E = M->global_end(); I != E; ++I)
45 EnumerateValue(I);
46
47 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000048 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000049 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000050 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000051 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000052
Chris Lattner07d98b42007-04-26 02:46:40 +000053 // Enumerate the aliases.
54 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
55 I != E; ++I)
56 EnumerateValue(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +000057
Chris Lattner6da91d32007-05-04 05:21:47 +000058 // Remember what is the cutoff between globalvalue's and other constants.
59 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000060
Chris Lattnerfd57cec2007-04-22 06:24:45 +000061 // Enumerate the global variable initializers.
62 for (Module::const_global_iterator I = M->global_begin(),
63 E = M->global_end(); I != E; ++I)
64 if (I->hasInitializer())
65 EnumerateValue(I->getInitializer());
66
Chris Lattner07d98b42007-04-26 02:46:40 +000067 // Enumerate the aliasees.
68 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
69 I != E; ++I)
70 EnumerateValue(I->getAliasee());
Daniel Dunbara279bc32009-09-20 02:20:51 +000071
Chris Lattnerfd57cec2007-04-22 06:24:45 +000072 // Enumerate types used by the type symbol table.
73 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
74
Bob Wilson54eee522010-06-19 05:33:57 +000075 // Insert constants and metadata that are named at module level into the slot
Devang Patel0386f012010-01-07 19:39:36 +000076 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000077 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman17aa92c2010-07-21 23:38:33 +000078 EnumerateNamedMetadata(M);
Daniel Dunbara279bc32009-09-20 02:20:51 +000079
Chris Lattnercc7b0112009-12-31 00:51:46 +000080 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
81
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
Chris Lattnerfd57cec2007-04-22 06:24:45 +000089 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
90 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000091 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000092 OI != E; ++OI) {
93 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000094 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000095 // These will get enumerated during function-incorporation.
96 continue;
97 EnumerateOperandType(*OI);
98 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000099 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +0000100 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +0000101 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000102 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +0000103 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +0000104
Daniel Dunbara279bc32009-09-20 02:20:51 +0000105 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +0000106 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +0000107 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +0000108 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +0000109 EnumerateMetadata(MDs[i].second);
Chris Lattnera6245242010-04-03 02:17:50 +0000110
111 if (!I->getDebugLoc().isUnknown()) {
112 MDNode *Scope, *IA;
113 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
114 if (Scope) EnumerateMetadata(Scope);
115 if (IA) EnumerateMetadata(IA);
116 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000117 }
118 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000119
Chris Lattner6da91d32007-05-04 05:21:47 +0000120 // Optimize constant ordering.
121 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000122
Chris Lattner12f535b2007-05-04 05:05:48 +0000123 // Sort the type table by frequency so that most commonly used types are early
124 // in the table (have low bit-width).
125 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000126
Dan Gohmane4977cf2008-05-23 01:55:30 +0000127 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000128 // aggregate types. This allows the aggregate types to be dropped from the
129 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000130 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000131
132 // Now that we rearranged the type table, rebuild TypeMap.
133 for (unsigned i = 0, e = Types.size(); i != e; ++i)
134 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000135}
136
Devang Patele8e02132009-09-18 19:26:43 +0000137unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
138 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
139 assert (I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000140 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000141}
Devang Patele8e02132009-09-18 19:26:43 +0000142
143void ValueEnumerator::setInstructionID(const Instruction *I) {
144 InstructionMap[I] = InstructionCount++;
145}
146
Devang Pateld5ac4042009-08-04 06:00:18 +0000147unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000148 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000149 ValueMapType::const_iterator I = MDValueMap.find(V);
150 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
151 return I->second-1;
152 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000153
Devang Pateld5ac4042009-08-04 06:00:18 +0000154 ValueMapType::const_iterator I = ValueMap.find(V);
155 assert(I != ValueMap.end() && "Value not in slotcalculator!");
156 return I->second-1;
157}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000158
Chris Lattner6da91d32007-05-04 05:21:47 +0000159// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000160namespace {
161 struct CstSortPredicate {
162 ValueEnumerator &VE;
163 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
164 bool operator()(const std::pair<const Value*, unsigned> &LHS,
165 const std::pair<const Value*, unsigned> &RHS) {
166 // Sort by plane.
167 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000168 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000169 VE.getTypeID(RHS.first->getType());
170 // Then by frequency.
171 return LHS.second > RHS.second;
172 }
173 };
174}
Chris Lattner6da91d32007-05-04 05:21:47 +0000175
176/// OptimizeConstants - Reorder constant pool for denser encoding.
177void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
178 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000179
Chris Lattner6da91d32007-05-04 05:21:47 +0000180 CstSortPredicate P(*this);
181 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000182
Chris Lattner6da91d32007-05-04 05:21:47 +0000183 // Ensure that integer constants are at the start of the constant pool. This
184 // is important so that GEP structure indices come before gep constant exprs.
185 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
186 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000187
Chris Lattner6da91d32007-05-04 05:21:47 +0000188 // Rebuild the modified portion of ValueMap.
189 for (; CstStart != CstEnd; ++CstStart)
190 ValueMap[Values[CstStart].first] = CstStart+1;
191}
192
193
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000194/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
195/// table.
196void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000197 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000198 TI != TE; ++TI)
199 EnumerateType(TI->second);
200}
201
202/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
203/// table into the values table.
204void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000205 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000206 VI != VE; ++VI)
207 EnumerateValue(VI->getValue());
208}
209
Dan Gohman17aa92c2010-07-21 23:38:33 +0000210/// EnumerateNamedMetadata - Insert all of the values referenced by
211/// named metadata in the specified module.
212void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
213 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
214 E = M->named_metadata_end(); I != E; ++I)
215 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000216}
217
Devang Patel8fba5782010-01-09 00:30:14 +0000218void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000219 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000220 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000221}
222
Dan Gohman309b3af2010-08-24 02:24:03 +0000223/// EnumerateMDNodeOperands - Enumerate all non-function-local values
224/// and types referenced by the given MDNode.
225void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
226 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
227 if (Value *V = N->getOperand(i)) {
228 if (isa<MDNode>(V) || isa<MDString>(V))
229 EnumerateMetadata(V);
230 else if (!isa<Instruction>(V) && !isa<Argument>(V))
231 EnumerateValue(V);
232 } else
233 EnumerateType(Type::getVoidTy(N->getContext()));
234 }
235}
236
Devang Patelbc5201f2010-01-22 22:52:10 +0000237void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000238 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000239
240 // Enumerate the type of this value.
241 EnumerateType(MD->getType());
242
243 const MDNode *N = dyn_cast<MDNode>(MD);
244
245 // In the module-level pass, skip function-local nodes themselves, but
246 // do walk their operands.
247 if (N && N->isFunctionLocal() && N->getFunction()) {
248 EnumerateMDNodeOperands(N);
249 return;
250 }
251
Devang Pateld5ac4042009-08-04 06:00:18 +0000252 // Check to see if it's already in!
253 unsigned &MDValueID = MDValueMap[MD];
254 if (MDValueID) {
255 // Increment use count.
256 MDValues[MDValueID-1].second++;
257 return;
258 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000259 MDValues.push_back(std::make_pair(MD, 1U));
260 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000261
262 // Enumerate all non-function-local operands.
263 if (N)
264 EnumerateMDNodeOperands(N);
265}
266
267/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
268/// information reachable from the given MDNode.
269void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
270 assert(N->isFunctionLocal() && N->getFunction() &&
271 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
272
273 // Enumerate the type of this value.
274 EnumerateType(N->getType());
275
276 // Check to see if it's already in!
277 unsigned &MDValueID = MDValueMap[N];
278 if (MDValueID) {
279 // Increment use count.
280 MDValues[MDValueID-1].second++;
281 return;
282 }
283 MDValues.push_back(std::make_pair(N, 1U));
284 MDValueID = MDValues.size();
285
286 // To incoroporate function-local information visit all function-local
287 // MDNodes and all function-local values they reference.
288 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
289 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000290 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000291 if (O->isFunctionLocal() && O->getFunction())
292 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000293 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000294 EnumerateValue(V);
295 }
296
297 // Also, collect all function-local MDNodes for easy access.
298 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000299}
300
Victor Hernandezd7e64572010-01-14 19:54:11 +0000301void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000302 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000303 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
304 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000305
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000306 // Check to see if it's already in!
307 unsigned &ValueID = ValueMap[V];
308 if (ValueID) {
309 // Increment use count.
310 Values[ValueID-1].second++;
311 return;
312 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000313
Chris Lattner7a303d12007-05-06 01:00:28 +0000314 // Enumerate the type of this value.
315 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000316
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000317 if (const Constant *C = dyn_cast<Constant>(V)) {
318 if (isa<GlobalValue>(C)) {
319 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000320 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
321 // Do not enumerate the initializers for an array of simple characters.
322 // The initializers just polute the value table, and we emit the strings
323 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000324 } else if (C->getNumOperands()) {
325 // If a constant has operands, enumerate them. This makes sure that if a
326 // constant has uses (for example an array of const ints), that they are
327 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000328
Chris Lattner7a303d12007-05-06 01:00:28 +0000329 // We prefer to enumerate them with values before we enumerate the user
330 // itself. This makes it more likely that we can avoid forward references
331 // in the reader. We know that there can be no cycles in the constants
332 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000333 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
334 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000335 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000336 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000337
Chris Lattner7a303d12007-05-06 01:00:28 +0000338 // Finally, add the value. Doing this could make the ValueID reference be
339 // dangling, don't reuse it.
340 Values.push_back(std::make_pair(V, 1U));
341 ValueMap[V] = Values.size();
342 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000343 }
344 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000345
Chris Lattner7a303d12007-05-06 01:00:28 +0000346 // Add the value.
347 Values.push_back(std::make_pair(V, 1U));
348 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000349}
350
351
352void ValueEnumerator::EnumerateType(const Type *Ty) {
353 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000354
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000355 if (TypeID) {
356 // If we've already seen this type, just increase its occurrence count.
357 Types[TypeID-1].second++;
358 return;
359 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000361 // First time we saw this type, add it.
362 Types.push_back(std::make_pair(Ty, 1U));
363 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000364
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000365 // Enumerate subtypes.
366 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
367 I != E; ++I)
368 EnumerateType(*I);
369}
370
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000371// Enumerate the types for the specified value. If the value is a constant,
372// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000373void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000374 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000375
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000376 if (const Constant *C = dyn_cast<Constant>(V)) {
377 // If this constant is already enumerated, ignore it, we know its type must
378 // be enumerated.
379 if (ValueMap.count(V)) return;
380
381 // This constant may have operands, make sure to enumerate the types in
382 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000383 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
384 const User *Op = C->getOperand(i);
385
386 // Don't enumerate basic blocks here, this happens as operands to
387 // blockaddress.
388 if (isa<BasicBlock>(Op)) continue;
389
Dan Gohman879d8112010-08-25 17:09:03 +0000390 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000391 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000392
393 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000394 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
395 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000396 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000397 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000398 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000399 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000400}
401
Devang Patel05988662008-09-25 21:00:45 +0000402void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000403 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000404 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000405 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000406 if (Entry == 0) {
407 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000408 Attributes.push_back(PAL);
409 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000410 }
411}
412
413
Chris Lattner8d35c792007-04-26 03:50:57 +0000414void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000415 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000416 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000417 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000418
Chris Lattner8d35c792007-04-26 03:50:57 +0000419 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000420 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
421 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000422 EnumerateValue(I);
423
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000424 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000425
Chris Lattner8d35c792007-04-26 03:50:57 +0000426 // Add all function-level constants to the value table.
427 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
428 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000429 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000430 OI != E; ++OI) {
431 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
432 isa<InlineAsm>(*OI))
433 EnumerateValue(*OI);
434 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000435 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000436 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000437 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000438
Chris Lattner6da91d32007-05-04 05:21:47 +0000439 // Optimize the constant layout.
440 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000441
Duncan Sandsdc024672007-11-27 13:23:08 +0000442 // Add the function's parameter attributes so they are available for use in
443 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000444 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000445
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000446 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000447
Devang Patel62098692010-06-02 23:05:04 +0000448 FunctionLocalMDs.clear();
449 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000450 // Add all of the instructions.
451 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000452 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000453 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
454 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000455 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000456 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000457 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000458 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000459 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000460
461 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
462 I->getAllMetadataOtherThanDebugLoc(MDs);
463 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
464 MDNode *N = MDs[i].second;
465 if (N->isFunctionLocal() && N->getFunction())
466 FnLocalMDVector.push_back(N);
467 }
468
Benjamin Kramerf0127052010-01-05 13:12:22 +0000469 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000470 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000471 }
472 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000473
474 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000475 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000476 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000477}
478
Chris Lattner8d35c792007-04-26 03:50:57 +0000479void ValueEnumerator::purgeFunction() {
480 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000481 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000482 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000483 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
484 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000485 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
486 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000487
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000488 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000489 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000490 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000491}
Chris Lattner837e04a2009-10-28 05:24:40 +0000492
493static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
494 DenseMap<const BasicBlock*, unsigned> &IDMap) {
495 unsigned Counter = 0;
496 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
497 IDMap[BB] = ++Counter;
498}
499
500/// getGlobalBasicBlockID - This returns the function-specific ID for the
501/// specified basic block. This is relatively expensive information, so it
502/// should only be used by rare constructs such as address-of-label.
503unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
504 unsigned &Idx = GlobalBasicBlockIDs[BB];
505 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000506 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000507
508 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
509 return getGlobalBasicBlockID(BB);
510}
511