blob: edd41ed21d3b67698198f99ad056921cd923492e [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!");
140 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
Devang Patelbc5201f2010-01-22 22:52:10 +0000223void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000224 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000225 // Check to see if it's already in!
226 unsigned &MDValueID = MDValueMap[MD];
227 if (MDValueID) {
228 // Increment use count.
229 MDValues[MDValueID-1].second++;
230 return;
231 }
232
233 // Enumerate the type of this value.
234 EnumerateType(MD->getType());
235
236 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000237 MDValues.push_back(std::make_pair(MD, 1U));
238 MDValueMap[MD] = MDValues.size();
239 MDValueID = MDValues.size();
240 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
241 if (Value *V = N->getOperand(i))
242 EnumerateValue(V);
243 else
244 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Pateld5ac4042009-08-04 06:00:18 +0000245 }
Devang Patel62098692010-06-02 23:05:04 +0000246 if (N->isFunctionLocal() && N->getFunction())
247 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000248 return;
Chris Lattner837e04a2009-10-28 05:24:40 +0000249 }
250
Devang Pateld5ac4042009-08-04 06:00:18 +0000251 // Add the value.
Chris Lattnercc7b0112009-12-31 00:51:46 +0000252 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000253 MDValues.push_back(std::make_pair(MD, 1U));
254 MDValueID = MDValues.size();
255}
256
Victor Hernandezd7e64572010-01-14 19:54:11 +0000257void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000258 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Patelbc5201f2010-01-22 22:52:10 +0000259 if (isa<MDNode>(V) || isa<MDString>(V))
260 return EnumerateMetadata(V);
Devang Pateld5ac4042009-08-04 06:00:18 +0000261
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000262 // Check to see if it's already in!
263 unsigned &ValueID = ValueMap[V];
264 if (ValueID) {
265 // Increment use count.
266 Values[ValueID-1].second++;
267 return;
268 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000269
Chris Lattner7a303d12007-05-06 01:00:28 +0000270 // Enumerate the type of this value.
271 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000272
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000273 if (const Constant *C = dyn_cast<Constant>(V)) {
274 if (isa<GlobalValue>(C)) {
275 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000276 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
277 // Do not enumerate the initializers for an array of simple characters.
278 // The initializers just polute the value table, and we emit the strings
279 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000280 } else if (C->getNumOperands()) {
281 // If a constant has operands, enumerate them. This makes sure that if a
282 // constant has uses (for example an array of const ints), that they are
283 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000284
Chris Lattner7a303d12007-05-06 01:00:28 +0000285 // We prefer to enumerate them with values before we enumerate the user
286 // itself. This makes it more likely that we can avoid forward references
287 // in the reader. We know that there can be no cycles in the constants
288 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000289 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
290 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000291 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000292 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000293
Chris Lattner7a303d12007-05-06 01:00:28 +0000294 // Finally, add the value. Doing this could make the ValueID reference be
295 // dangling, don't reuse it.
296 Values.push_back(std::make_pair(V, 1U));
297 ValueMap[V] = Values.size();
298 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000299 }
300 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000301
Chris Lattner7a303d12007-05-06 01:00:28 +0000302 // Add the value.
303 Values.push_back(std::make_pair(V, 1U));
304 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000305}
306
307
308void ValueEnumerator::EnumerateType(const Type *Ty) {
309 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000310
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000311 if (TypeID) {
312 // If we've already seen this type, just increase its occurrence count.
313 Types[TypeID-1].second++;
314 return;
315 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000316
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000317 // First time we saw this type, add it.
318 Types.push_back(std::make_pair(Ty, 1U));
319 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000320
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000321 // Enumerate subtypes.
322 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
323 I != E; ++I)
324 EnumerateType(*I);
325}
326
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000327// Enumerate the types for the specified value. If the value is a constant,
328// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000329void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000330 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000331
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000332 if (const Constant *C = dyn_cast<Constant>(V)) {
333 // If this constant is already enumerated, ignore it, we know its type must
334 // be enumerated.
335 if (ValueMap.count(V)) return;
336
337 // This constant may have operands, make sure to enumerate the types in
338 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000339 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
340 const User *Op = C->getOperand(i);
341
342 // Don't enumerate basic blocks here, this happens as operands to
343 // blockaddress.
344 if (isa<BasicBlock>(Op)) continue;
345
Victor Hernandezd7e64572010-01-14 19:54:11 +0000346 EnumerateOperandType(cast<Constant>(Op));
Chris Lattner837e04a2009-10-28 05:24:40 +0000347 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000348
349 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000350 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
351 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000352 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000353 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000354 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000355 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000356}
357
Devang Patel05988662008-09-25 21:00:45 +0000358void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000359 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000360 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000361 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000362 if (Entry == 0) {
363 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000364 Attributes.push_back(PAL);
365 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000366 }
367}
368
369
Chris Lattner8d35c792007-04-26 03:50:57 +0000370void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000371 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000372 NumModuleValues = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000373
Chris Lattner8d35c792007-04-26 03:50:57 +0000374 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000375 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
376 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000377 EnumerateValue(I);
378
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000379 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Chris Lattner8d35c792007-04-26 03:50:57 +0000381 // Add all function-level constants to the value table.
382 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
383 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000384 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000385 OI != E; ++OI) {
386 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
387 isa<InlineAsm>(*OI))
388 EnumerateValue(*OI);
389 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000390 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000391 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000392 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000393
Chris Lattner6da91d32007-05-04 05:21:47 +0000394 // Optimize the constant layout.
395 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000396
Duncan Sandsdc024672007-11-27 13:23:08 +0000397 // Add the function's parameter attributes so they are available for use in
398 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000399 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000400
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000401 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000402
Devang Patel62098692010-06-02 23:05:04 +0000403 FunctionLocalMDs.clear();
404 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000405 // Add all of the instructions.
406 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000407 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000408 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
409 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000410 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000411 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000412 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000413 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000414 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000415 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000416 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000417 }
418 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000419
420 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000421 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
422 EnumerateOperandType(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000423}
424
Chris Lattner8d35c792007-04-26 03:50:57 +0000425void ValueEnumerator::purgeFunction() {
426 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000427 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000428 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000429 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
430 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000431
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000432 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000433 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000434}
Chris Lattner837e04a2009-10-28 05:24:40 +0000435
436static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
437 DenseMap<const BasicBlock*, unsigned> &IDMap) {
438 unsigned Counter = 0;
439 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
440 IDMap[BB] = ++Counter;
441}
442
443/// getGlobalBasicBlockID - This returns the function-specific ID for the
444/// specified basic block. This is relatively expensive information, so it
445/// should only be used by rare constructs such as address-of-label.
446unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
447 unsigned &Idx = GlobalBasicBlockIDs[BB];
448 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000449 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000450
451 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
452 return getGlobalBasicBlockID(BB);
453}
454