blob: 29a779d810b5511c53a77542e63f93b87d04d11f [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) {
30 return isa<IntegerType>(V.first->getType());
31}
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) {
Devang Patele8e02132009-09-18 19:26:43 +000042 InstructionCount = 0;
43
Chris Lattnerfd57cec2007-04-22 06:24:45 +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 Sandsdc024672007-11-27 13:23:08 +000050 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000051 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000052 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000053 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000054
Chris Lattner07d98b42007-04-26 02:46:40 +000055 // 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 Dunbara279bc32009-09-20 02:20:51 +000059
Chris Lattner6da91d32007-05-04 05:21:47 +000060 // Remember what is the cutoff between globalvalue's and other constants.
61 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000062
Chris Lattnerfd57cec2007-04-22 06:24:45 +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
Chris Lattner07d98b42007-04-26 02:46:40 +000069 // 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 Dunbara279bc32009-09-20 02:20:51 +000073
Chris Lattnerfd57cec2007-04-22 06:24:45 +000074 // Enumerate types used by the type symbol table.
75 EnumerateTypeSymbolTable(M->getTypeSymbolTable());
76
Devang Patel0386f012010-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...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000079 EnumerateValueSymbolTable(M->getValueSymbolTable());
Devang Patel0386f012010-01-07 19:39:36 +000080 EnumerateMDSymbolTable(M->getMDSymbolTable());
Daniel Dunbara279bc32009-09-20 02:20:51 +000081
Chris Lattnercc7b0112009-12-31 00:51:46 +000082 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
83
Chris Lattner8d35c792007-04-26 03:50:57 +000084 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000085 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000086
Chris Lattner8d35c792007-04-26 03:50:57 +000087 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
88 I != E; ++I)
89 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000090
Chris Lattnerfd57cec2007-04-22 06:24:45 +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 Dunbara279bc32009-09-20 02:20:51 +000093 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000094 OI != E; ++OI) {
95 if (MDNode *MD = dyn_cast<MDNode>(*OI))
96 if (MD->isFunctionLocal())
97 // These will get enumerated during function-incorporation.
98 continue;
99 EnumerateOperandType(*OI);
100 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000101 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +0000102 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +0000103 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000104 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +0000105 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +0000106
Daniel Dunbara279bc32009-09-20 02:20:51 +0000107 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +0000108 MDs.clear();
Chris Lattner3990b122009-12-28 23:41:32 +0000109 I->getAllMetadata(MDs);
110 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +0000111 EnumerateMetadata(MDs[i].second);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000112 }
113 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000114
Chris Lattner6da91d32007-05-04 05:21:47 +0000115 // Optimize constant ordering.
116 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000117
Chris Lattner12f535b2007-05-04 05:05:48 +0000118 // Sort the type table by frequency so that most commonly used types are early
119 // in the table (have low bit-width).
120 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000121
Dan Gohmane4977cf2008-05-23 01:55:30 +0000122 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000123 // aggregate types. This allows the aggregate types to be dropped from the
124 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000125 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000126
127 // Now that we rearranged the type table, rebuild TypeMap.
128 for (unsigned i = 0, e = Types.size(); i != e; ++i)
129 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000130}
131
Devang Patele8e02132009-09-18 19:26:43 +0000132unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
133 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
134 assert (I != InstructionMap.end() && "Instruction is not mapped!");
135 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000136}
Devang Patele8e02132009-09-18 19:26:43 +0000137
138void ValueEnumerator::setInstructionID(const Instruction *I) {
139 InstructionMap[I] = InstructionCount++;
140}
141
Devang Pateld5ac4042009-08-04 06:00:18 +0000142unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000143 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000144 ValueMapType::const_iterator I = MDValueMap.find(V);
145 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
146 return I->second-1;
147 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000148
Devang Pateld5ac4042009-08-04 06:00:18 +0000149 ValueMapType::const_iterator I = ValueMap.find(V);
150 assert(I != ValueMap.end() && "Value not in slotcalculator!");
151 return I->second-1;
152}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000153
Chris Lattner6da91d32007-05-04 05:21:47 +0000154// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000155namespace {
156 struct CstSortPredicate {
157 ValueEnumerator &VE;
158 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
159 bool operator()(const std::pair<const Value*, unsigned> &LHS,
160 const std::pair<const Value*, unsigned> &RHS) {
161 // Sort by plane.
162 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000163 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000164 VE.getTypeID(RHS.first->getType());
165 // Then by frequency.
166 return LHS.second > RHS.second;
167 }
168 };
169}
Chris Lattner6da91d32007-05-04 05:21:47 +0000170
171/// OptimizeConstants - Reorder constant pool for denser encoding.
172void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
173 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000174
Chris Lattner6da91d32007-05-04 05:21:47 +0000175 CstSortPredicate P(*this);
176 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000177
Chris Lattner6da91d32007-05-04 05:21:47 +0000178 // Ensure that integer constants are at the start of the constant pool. This
179 // is important so that GEP structure indices come before gep constant exprs.
180 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
181 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000182
Chris Lattner6da91d32007-05-04 05:21:47 +0000183 // Rebuild the modified portion of ValueMap.
184 for (; CstStart != CstEnd; ++CstStart)
185 ValueMap[Values[CstStart].first] = CstStart+1;
186}
187
188
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000189/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
190/// table.
191void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000192 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000193 TI != TE; ++TI)
194 EnumerateType(TI->second);
195}
196
197/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
198/// table into the values table.
199void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000200 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000201 VI != VE; ++VI)
202 EnumerateValue(VI->getValue());
203}
204
Devang Patel0386f012010-01-07 19:39:36 +0000205/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata
206/// table.
207void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) {
208 for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end();
209 MI != ME; ++MI)
210 EnumerateValue(MI->getValue());
211}
212
Devang Patel8fba5782010-01-09 00:30:14 +0000213void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
214 // Check to see if it's already in!
215 unsigned &MDValueID = MDValueMap[MD];
216 if (MDValueID) {
217 // Increment use count.
218 MDValues[MDValueID-1].second++;
219 return;
220 }
221
222 // Enumerate the type of this value.
223 EnumerateType(MD->getType());
224
225 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
226 if (MDNode *E = MD->getOperand(i))
227 EnumerateValue(E);
228 MDValues.push_back(std::make_pair(MD, 1U));
229 MDValueMap[MD] = Values.size();
230}
231
Devang Patelbc5201f2010-01-22 22:52:10 +0000232void ValueEnumerator::EnumerateMetadata(const Value *MD) {
233 assert(isa<MDNode>(MD) || isa<MDString>(MD) && "Invalid metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000234 // Check to see if it's already in!
235 unsigned &MDValueID = MDValueMap[MD];
236 if (MDValueID) {
237 // Increment use count.
238 MDValues[MDValueID-1].second++;
239 return;
240 }
241
242 // Enumerate the type of this value.
243 EnumerateType(MD->getType());
244
245 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000246 MDValues.push_back(std::make_pair(MD, 1U));
247 MDValueMap[MD] = MDValues.size();
248 MDValueID = MDValues.size();
249 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
250 if (Value *V = N->getOperand(i))
251 EnumerateValue(V);
252 else
253 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Pateld5ac4042009-08-04 06:00:18 +0000254 }
255 return;
Chris Lattner837e04a2009-10-28 05:24:40 +0000256 }
257
Devang Pateld5ac4042009-08-04 06:00:18 +0000258 // Add the value.
Chris Lattnercc7b0112009-12-31 00:51:46 +0000259 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000260 MDValues.push_back(std::make_pair(MD, 1U));
261 MDValueID = MDValues.size();
262}
263
Victor Hernandezd7e64572010-01-14 19:54:11 +0000264void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000265 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Patelbc5201f2010-01-22 22:52:10 +0000266 if (isa<MDNode>(V) || isa<MDString>(V))
267 return EnumerateMetadata(V);
Devang Patel8fba5782010-01-09 00:30:14 +0000268 else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
269 return EnumerateNamedMDNode(NMD);
Devang Pateld5ac4042009-08-04 06:00:18 +0000270
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000271 // Check to see if it's already in!
272 unsigned &ValueID = ValueMap[V];
273 if (ValueID) {
274 // Increment use count.
275 Values[ValueID-1].second++;
276 return;
277 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000278
Chris Lattner7a303d12007-05-06 01:00:28 +0000279 // Enumerate the type of this value.
280 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000281
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000282 if (const Constant *C = dyn_cast<Constant>(V)) {
283 if (isa<GlobalValue>(C)) {
284 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000285 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
286 // Do not enumerate the initializers for an array of simple characters.
287 // The initializers just polute the value table, and we emit the strings
288 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000289 } else if (C->getNumOperands()) {
290 // If a constant has operands, enumerate them. This makes sure that if a
291 // constant has uses (for example an array of const ints), that they are
292 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000293
Chris Lattner7a303d12007-05-06 01:00:28 +0000294 // We prefer to enumerate them with values before we enumerate the user
295 // itself. This makes it more likely that we can avoid forward references
296 // in the reader. We know that there can be no cycles in the constants
297 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000298 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
299 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000300 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000301 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000302
Chris Lattner7a303d12007-05-06 01:00:28 +0000303 // Finally, add the value. Doing this could make the ValueID reference be
304 // dangling, don't reuse it.
305 Values.push_back(std::make_pair(V, 1U));
306 ValueMap[V] = Values.size();
307 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000308 }
309 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000310
Chris Lattner7a303d12007-05-06 01:00:28 +0000311 // Add the value.
312 Values.push_back(std::make_pair(V, 1U));
313 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000314}
315
316
317void ValueEnumerator::EnumerateType(const Type *Ty) {
318 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000319
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000320 if (TypeID) {
321 // If we've already seen this type, just increase its occurrence count.
322 Types[TypeID-1].second++;
323 return;
324 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000325
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000326 // First time we saw this type, add it.
327 Types.push_back(std::make_pair(Ty, 1U));
328 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000329
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000330 // Enumerate subtypes.
331 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
332 I != E; ++I)
333 EnumerateType(*I);
334}
335
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000336// Enumerate the types for the specified value. If the value is a constant,
337// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000338void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000339 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000340
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000341 if (const Constant *C = dyn_cast<Constant>(V)) {
342 // If this constant is already enumerated, ignore it, we know its type must
343 // be enumerated.
344 if (ValueMap.count(V)) return;
345
346 // This constant may have operands, make sure to enumerate the types in
347 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000348 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
349 const User *Op = C->getOperand(i);
350
351 // Don't enumerate basic blocks here, this happens as operands to
352 // blockaddress.
353 if (isa<BasicBlock>(Op)) continue;
354
Victor Hernandezd7e64572010-01-14 19:54:11 +0000355 EnumerateOperandType(cast<Constant>(Op));
Chris Lattner837e04a2009-10-28 05:24:40 +0000356 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000357
358 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000359 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
360 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000361 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000362 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000363 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000364 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000365}
366
Devang Patel05988662008-09-25 21:00:45 +0000367void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000368 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000369 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000370 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000371 if (Entry == 0) {
372 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000373 Attributes.push_back(PAL);
374 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000375 }
376}
377
378
Chris Lattner8d35c792007-04-26 03:50:57 +0000379void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000380 NumModuleValues = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000381
Chris Lattner8d35c792007-04-26 03:50:57 +0000382 // Adding function arguments to the value table.
383 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000384 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000385 EnumerateValue(I);
386
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000387 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000388
Chris Lattner8d35c792007-04-26 03:50:57 +0000389 // Add all function-level constants to the value table.
390 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
391 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000392 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000393 OI != E; ++OI) {
394 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
395 isa<InlineAsm>(*OI))
396 EnumerateValue(*OI);
397 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000398 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000399 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000400 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattner6da91d32007-05-04 05:21:47 +0000402 // Optimize the constant layout.
403 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000404
Duncan Sandsdc024672007-11-27 13:23:08 +0000405 // Add the function's parameter attributes so they are available for use in
406 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000407 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000408
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000409 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000410
Chris Lattner8d35c792007-04-26 03:50:57 +0000411 // Add all of the instructions.
412 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000413 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000414 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
415 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000416 if (MDNode *MD = dyn_cast<MDNode>(*OI))
417 if (!MD->isFunctionLocal())
418 // These were already enumerated during ValueEnumerator creation.
419 continue;
420 EnumerateOperandType(*OI);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000421 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000422 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000423 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000424 }
425 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000426}
427
Chris Lattner8d35c792007-04-26 03:50:57 +0000428void ValueEnumerator::purgeFunction() {
429 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000430 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000431 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000432 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
433 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000434
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000435 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000436 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000437}
Chris Lattner837e04a2009-10-28 05:24:40 +0000438
439static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
440 DenseMap<const BasicBlock*, unsigned> &IDMap) {
441 unsigned Counter = 0;
442 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
443 IDMap[BB] = ++Counter;
444}
445
446/// getGlobalBasicBlockID - This returns the function-specific ID for the
447/// specified basic block. This is relatively expensive information, so it
448/// should only be used by rare constructs such as address-of-label.
449unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
450 unsigned &Idx = GlobalBasicBlockIDs[BB];
451 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000452 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000453
454 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
455 return getGlobalBasicBlockID(BB);
456}
457