blob: 05078caf5fa897c681e4b1b8d45379977519b0c3 [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"
Rafael Espindolaf5a90562011-04-06 16:49:37 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/STLExtras.h"
Chris Lattnerff7fc5d2007-05-06 00:35:24 +000017#include "llvm/Constants.h"
Chris Lattner50954f52007-05-03 22:46:43 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/Module.h"
20#include "llvm/TypeSymbolTable.h"
21#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000022#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000023#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000024using namespace llvm;
25
Chris Lattner6da91d32007-05-04 05:21:47 +000026static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
Duncan Sands1df98592010-02-16 11:11:14 +000027 return V.first->getType()->isIntegerTy();
Chris Lattner6da91d32007-05-04 05:21:47 +000028}
29
Chris Lattnerfd57cec2007-04-22 06:24:45 +000030/// ValueEnumerator - Enumerate module-level information.
31ValueEnumerator::ValueEnumerator(const Module *M) {
32 // Enumerate the global variables.
33 for (Module::const_global_iterator I = M->global_begin(),
34 E = M->global_end(); I != E; ++I)
35 EnumerateValue(I);
36
37 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000038 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000039 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000040 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000041 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000042
Chris Lattner07d98b42007-04-26 02:46:40 +000043 // Enumerate the aliases.
44 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
45 I != E; ++I)
46 EnumerateValue(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +000047
Chris Lattner6da91d32007-05-04 05:21:47 +000048 // Remember what is the cutoff between globalvalue's and other constants.
49 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000050
Chris Lattnerfd57cec2007-04-22 06:24:45 +000051 // Enumerate the global variable initializers.
52 for (Module::const_global_iterator I = M->global_begin(),
53 E = M->global_end(); I != E; ++I)
54 if (I->hasInitializer())
55 EnumerateValue(I->getInitializer());
56
Chris Lattner07d98b42007-04-26 02:46:40 +000057 // Enumerate the aliasees.
58 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
59 I != E; ++I)
60 EnumerateValue(I->getAliasee());
Daniel Dunbara279bc32009-09-20 02:20:51 +000061
Chris Lattnerfd57cec2007-04-22 06:24:45 +000062 // Enumerate types used by the type symbol table.
63 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
64
Bob Wilson54eee522010-06-19 05:33:57 +000065 // Insert constants and metadata that are named at module level into the slot
Devang Patel0386f012010-01-07 19:39:36 +000066 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000067 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman17aa92c2010-07-21 23:38:33 +000068 EnumerateNamedMetadata(M);
Daniel Dunbara279bc32009-09-20 02:20:51 +000069
Chris Lattnercc7b0112009-12-31 00:51:46 +000070 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
71
Chris Lattner8d35c792007-04-26 03:50:57 +000072 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000073 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000074
Chris Lattner8d35c792007-04-26 03:50:57 +000075 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
76 I != E; ++I)
77 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000078
Chris Lattnerfd57cec2007-04-22 06:24:45 +000079 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
80 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000081 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000082 OI != E; ++OI) {
83 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000084 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000085 // These will get enumerated during function-incorporation.
86 continue;
87 EnumerateOperandType(*OI);
88 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000089 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000090 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000091 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000092 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000093 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +000094
Daniel Dunbara279bc32009-09-20 02:20:51 +000095 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +000096 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +000097 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +000098 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +000099 EnumerateMetadata(MDs[i].second);
Chris Lattnera6245242010-04-03 02:17:50 +0000100
101 if (!I->getDebugLoc().isUnknown()) {
102 MDNode *Scope, *IA;
103 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
104 if (Scope) EnumerateMetadata(Scope);
105 if (IA) EnumerateMetadata(IA);
106 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000107 }
108 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000109
Chris Lattner6da91d32007-05-04 05:21:47 +0000110 // Optimize constant ordering.
111 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000113 OptimizeTypes();
Chris Lattner12f535b2007-05-04 05:05:48 +0000114
115 // Now that we rearranged the type table, rebuild TypeMap.
116 for (unsigned i = 0, e = Types.size(); i != e; ++i)
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000117 TypeMap[Types[i]] = i+1;
118}
119
120struct TypeAndDeps {
121 const Type *Ty;
122 unsigned NumDeps;
123};
124
125static int CompareByDeps(const void *a, const void *b) {
126 const TypeAndDeps &ta = *(const TypeAndDeps*) a;
127 const TypeAndDeps &tb = *(const TypeAndDeps*) b;
128 return ta.NumDeps - tb.NumDeps;
129}
130
131static void VisitType(const Type *Ty, SmallPtrSet<const Type*, 16> &Visited,
132 std::vector<const Type*> &Out) {
133 if (Visited.count(Ty))
134 return;
135
136 Visited.insert(Ty);
137
138 for (Type::subtype_iterator I2 = Ty->subtype_begin(),
139 E2 = Ty->subtype_end(); I2 != E2; ++I2) {
140 const Type *InnerType = I2->get();
141 VisitType(InnerType, Visited, Out);
142 }
143
144 Out.push_back(Ty);
145}
146
147void ValueEnumerator::OptimizeTypes(void) {
148 // If the types form a DAG, this will compute a topological sort and
149 // no forward references will be needed when reading them in.
150 // If there are cycles, this is a simple but reasonable heuristic for
151 // the minimum feedback arc set problem.
152 const unsigned NumTypes = Types.size();
153 std::vector<TypeAndDeps> TypeDeps;
154 TypeDeps.resize(NumTypes);
155
156 for (unsigned I = 0; I < NumTypes; ++I) {
157 const Type *Ty = Types[I];
158 TypeDeps[I].Ty = Ty;
159 TypeDeps[I].NumDeps = 0;
160 }
161
162 for (unsigned I = 0; I < NumTypes; ++I) {
163 const Type *Ty = TypeDeps[I].Ty;
164 for (Type::subtype_iterator I2 = Ty->subtype_begin(),
165 E2 = Ty->subtype_end(); I2 != E2; ++I2) {
166 const Type *InnerType = I2->get();
167 unsigned InnerIndex = TypeMap.lookup(InnerType) - 1;
168 TypeDeps[InnerIndex].NumDeps++;
169 }
170 }
171 array_pod_sort(TypeDeps.begin(), TypeDeps.end(), CompareByDeps);
172
173 SmallPtrSet<const Type*, 16> Visited;
174 Types.clear();
175 Types.reserve(NumTypes);
176 for (unsigned I = 0; I < NumTypes; ++I) {
177 VisitType(TypeDeps[I].Ty, Visited, Types);
178 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000179}
180
Devang Patele8e02132009-09-18 19:26:43 +0000181unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
182 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
183 assert (I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000184 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000185}
Devang Patele8e02132009-09-18 19:26:43 +0000186
187void ValueEnumerator::setInstructionID(const Instruction *I) {
188 InstructionMap[I] = InstructionCount++;
189}
190
Devang Pateld5ac4042009-08-04 06:00:18 +0000191unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000192 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000193 ValueMapType::const_iterator I = MDValueMap.find(V);
194 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
195 return I->second-1;
196 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000197
Devang Pateld5ac4042009-08-04 06:00:18 +0000198 ValueMapType::const_iterator I = ValueMap.find(V);
199 assert(I != ValueMap.end() && "Value not in slotcalculator!");
200 return I->second-1;
201}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000202
Chris Lattner6da91d32007-05-04 05:21:47 +0000203// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000204namespace {
205 struct CstSortPredicate {
206 ValueEnumerator &VE;
207 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
208 bool operator()(const std::pair<const Value*, unsigned> &LHS,
209 const std::pair<const Value*, unsigned> &RHS) {
210 // Sort by plane.
211 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000212 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000213 VE.getTypeID(RHS.first->getType());
214 // Then by frequency.
215 return LHS.second > RHS.second;
216 }
217 };
218}
Chris Lattner6da91d32007-05-04 05:21:47 +0000219
220/// OptimizeConstants - Reorder constant pool for denser encoding.
221void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
222 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000223
Chris Lattner6da91d32007-05-04 05:21:47 +0000224 CstSortPredicate P(*this);
225 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000226
Chris Lattner6da91d32007-05-04 05:21:47 +0000227 // Ensure that integer constants are at the start of the constant pool. This
228 // is important so that GEP structure indices come before gep constant exprs.
229 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
230 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000231
Chris Lattner6da91d32007-05-04 05:21:47 +0000232 // Rebuild the modified portion of ValueMap.
233 for (; CstStart != CstEnd; ++CstStart)
234 ValueMap[Values[CstStart].first] = CstStart+1;
235}
236
237
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000238/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
239/// table.
240void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000241 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000242 TI != TE; ++TI)
243 EnumerateType(TI->second);
244}
245
246/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
247/// table into the values table.
248void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000249 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000250 VI != VE; ++VI)
251 EnumerateValue(VI->getValue());
252}
253
Dan Gohman17aa92c2010-07-21 23:38:33 +0000254/// EnumerateNamedMetadata - Insert all of the values referenced by
255/// named metadata in the specified module.
256void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
257 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
258 E = M->named_metadata_end(); I != E; ++I)
259 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000260}
261
Devang Patel8fba5782010-01-09 00:30:14 +0000262void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000263 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000264 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000265}
266
Dan Gohman309b3af2010-08-24 02:24:03 +0000267/// EnumerateMDNodeOperands - Enumerate all non-function-local values
268/// and types referenced by the given MDNode.
269void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
270 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
271 if (Value *V = N->getOperand(i)) {
272 if (isa<MDNode>(V) || isa<MDString>(V))
273 EnumerateMetadata(V);
274 else if (!isa<Instruction>(V) && !isa<Argument>(V))
275 EnumerateValue(V);
276 } else
277 EnumerateType(Type::getVoidTy(N->getContext()));
278 }
279}
280
Devang Patelbc5201f2010-01-22 22:52:10 +0000281void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000282 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000283
284 // Enumerate the type of this value.
285 EnumerateType(MD->getType());
286
287 const MDNode *N = dyn_cast<MDNode>(MD);
288
289 // In the module-level pass, skip function-local nodes themselves, but
290 // do walk their operands.
291 if (N && N->isFunctionLocal() && N->getFunction()) {
292 EnumerateMDNodeOperands(N);
293 return;
294 }
295
Devang Pateld5ac4042009-08-04 06:00:18 +0000296 // Check to see if it's already in!
297 unsigned &MDValueID = MDValueMap[MD];
298 if (MDValueID) {
299 // Increment use count.
300 MDValues[MDValueID-1].second++;
301 return;
302 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000303 MDValues.push_back(std::make_pair(MD, 1U));
304 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000305
306 // Enumerate all non-function-local operands.
307 if (N)
308 EnumerateMDNodeOperands(N);
309}
310
311/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
312/// information reachable from the given MDNode.
313void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
314 assert(N->isFunctionLocal() && N->getFunction() &&
315 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
316
317 // Enumerate the type of this value.
318 EnumerateType(N->getType());
319
320 // Check to see if it's already in!
321 unsigned &MDValueID = MDValueMap[N];
322 if (MDValueID) {
323 // Increment use count.
324 MDValues[MDValueID-1].second++;
325 return;
326 }
327 MDValues.push_back(std::make_pair(N, 1U));
328 MDValueID = MDValues.size();
329
330 // To incoroporate function-local information visit all function-local
331 // MDNodes and all function-local values they reference.
332 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
333 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000334 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000335 if (O->isFunctionLocal() && O->getFunction())
336 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000337 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000338 EnumerateValue(V);
339 }
340
341 // Also, collect all function-local MDNodes for easy access.
342 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000343}
344
Victor Hernandezd7e64572010-01-14 19:54:11 +0000345void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000346 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000347 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
348 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000349
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000350 // Check to see if it's already in!
351 unsigned &ValueID = ValueMap[V];
352 if (ValueID) {
353 // Increment use count.
354 Values[ValueID-1].second++;
355 return;
356 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000357
Chris Lattner7a303d12007-05-06 01:00:28 +0000358 // Enumerate the type of this value.
359 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000361 if (const Constant *C = dyn_cast<Constant>(V)) {
362 if (isa<GlobalValue>(C)) {
363 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000364 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
365 // Do not enumerate the initializers for an array of simple characters.
366 // The initializers just polute the value table, and we emit the strings
367 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000368 } else if (C->getNumOperands()) {
369 // If a constant has operands, enumerate them. This makes sure that if a
370 // constant has uses (for example an array of const ints), that they are
371 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000372
Chris Lattner7a303d12007-05-06 01:00:28 +0000373 // We prefer to enumerate them with values before we enumerate the user
374 // itself. This makes it more likely that we can avoid forward references
375 // in the reader. We know that there can be no cycles in the constants
376 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000377 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
378 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000379 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000380 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000381
Chris Lattner7a303d12007-05-06 01:00:28 +0000382 // Finally, add the value. Doing this could make the ValueID reference be
383 // dangling, don't reuse it.
384 Values.push_back(std::make_pair(V, 1U));
385 ValueMap[V] = Values.size();
386 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000387 }
388 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000389
Chris Lattner7a303d12007-05-06 01:00:28 +0000390 // Add the value.
391 Values.push_back(std::make_pair(V, 1U));
392 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000393}
394
395
396void ValueEnumerator::EnumerateType(const Type *Ty) {
397 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000398
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000399 // We've already seen this type.
400 if (TypeID)
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000401 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000402
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000403 // First time we saw this type, add it.
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000404 Types.push_back(Ty);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000405 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000406
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000407 // Enumerate subtypes.
408 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
409 I != E; ++I)
410 EnumerateType(*I);
411}
412
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000413// Enumerate the types for the specified value. If the value is a constant,
414// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000415void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000416 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000417
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000418 if (const Constant *C = dyn_cast<Constant>(V)) {
419 // If this constant is already enumerated, ignore it, we know its type must
420 // be enumerated.
421 if (ValueMap.count(V)) return;
422
423 // This constant may have operands, make sure to enumerate the types in
424 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000425 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
426 const User *Op = C->getOperand(i);
427
428 // Don't enumerate basic blocks here, this happens as operands to
429 // blockaddress.
430 if (isa<BasicBlock>(Op)) continue;
431
Dan Gohman879d8112010-08-25 17:09:03 +0000432 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000433 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000434
435 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000436 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
437 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000438 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000439 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000440 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000441 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000442}
443
Devang Patel05988662008-09-25 21:00:45 +0000444void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000445 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000446 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000447 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000448 if (Entry == 0) {
449 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000450 Attributes.push_back(PAL);
451 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000452 }
453}
454
455
Chris Lattner8d35c792007-04-26 03:50:57 +0000456void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000457 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000458 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000459 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000460
Chris Lattner8d35c792007-04-26 03:50:57 +0000461 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000462 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
463 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000464 EnumerateValue(I);
465
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000466 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000467
Chris Lattner8d35c792007-04-26 03:50:57 +0000468 // Add all function-level constants to the value table.
469 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
470 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000471 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000472 OI != E; ++OI) {
473 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
474 isa<InlineAsm>(*OI))
475 EnumerateValue(*OI);
476 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000477 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000478 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000479 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000480
Chris Lattner6da91d32007-05-04 05:21:47 +0000481 // Optimize the constant layout.
482 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000483
Duncan Sandsdc024672007-11-27 13:23:08 +0000484 // Add the function's parameter attributes so they are available for use in
485 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000486 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000487
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000488 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000489
Devang Patel62098692010-06-02 23:05:04 +0000490 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000491 // Add all of the instructions.
492 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000493 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000494 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
495 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000496 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000497 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000498 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000499 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000500 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000501
502 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
503 I->getAllMetadataOtherThanDebugLoc(MDs);
504 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
505 MDNode *N = MDs[i].second;
506 if (N->isFunctionLocal() && N->getFunction())
507 FnLocalMDVector.push_back(N);
508 }
509
Benjamin Kramerf0127052010-01-05 13:12:22 +0000510 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000511 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000512 }
513 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000514
515 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000516 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000517 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000518}
519
Chris Lattner8d35c792007-04-26 03:50:57 +0000520void ValueEnumerator::purgeFunction() {
521 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000522 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000523 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000524 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
525 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000526 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
527 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000528
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000529 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000530 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000531 BasicBlocks.clear();
Dan Gohman848c9ae2010-08-25 17:11:16 +0000532 FunctionLocalMDs.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000533}
Chris Lattner837e04a2009-10-28 05:24:40 +0000534
535static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
536 DenseMap<const BasicBlock*, unsigned> &IDMap) {
537 unsigned Counter = 0;
538 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
539 IDMap[BB] = ++Counter;
540}
541
542/// getGlobalBasicBlockID - This returns the function-specific ID for the
543/// specified basic block. This is relatively expensive information, so it
544/// should only be used by rare constructs such as address-of-label.
545unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
546 unsigned &Idx = GlobalBasicBlockIDs[BB];
547 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000548 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000549
550 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
551 return getGlobalBasicBlockID(BB);
552}
553