blob: aa4c3afab40ec748f5f6e9b43c8a6aa7a6bd55ca [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
Devang Patel0386f012010-01-07 19:39:36 +000075 // Insert constants and metadata that are named at module level into the slot
76 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000077 EnumerateValueSymbolTable(M->getValueSymbolTable());
Devang Patel0386f012010-01-07 19:39:36 +000078 EnumerateMDSymbolTable(M->getMDSymbolTable());
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 Lattner3990b122009-12-28 23:41:32 +0000107 I->getAllMetadata(MDs);
108 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +0000109 EnumerateMetadata(MDs[i].second);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000110 }
111 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000112
Chris Lattner6da91d32007-05-04 05:21:47 +0000113 // Optimize constant ordering.
114 OptimizeConstants(FirstConstant, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000115
Chris Lattner12f535b2007-05-04 05:05:48 +0000116 // Sort the type table by frequency so that most commonly used types are early
117 // in the table (have low bit-width).
118 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000119
Dan Gohmane4977cf2008-05-23 01:55:30 +0000120 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000121 // aggregate types. This allows the aggregate types to be dropped from the
122 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000123 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000124
125 // Now that we rearranged the type table, rebuild TypeMap.
126 for (unsigned i = 0, e = Types.size(); i != e; ++i)
127 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000128}
129
Devang Patele8e02132009-09-18 19:26:43 +0000130unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
131 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
132 assert (I != InstructionMap.end() && "Instruction is not mapped!");
133 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000134}
Devang Patele8e02132009-09-18 19:26:43 +0000135
136void ValueEnumerator::setInstructionID(const Instruction *I) {
137 InstructionMap[I] = InstructionCount++;
138}
139
Devang Pateld5ac4042009-08-04 06:00:18 +0000140unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000141 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000142 ValueMapType::const_iterator I = MDValueMap.find(V);
143 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
144 return I->second-1;
145 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000146
Devang Pateld5ac4042009-08-04 06:00:18 +0000147 ValueMapType::const_iterator I = ValueMap.find(V);
148 assert(I != ValueMap.end() && "Value not in slotcalculator!");
149 return I->second-1;
150}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000151
Chris Lattner6da91d32007-05-04 05:21:47 +0000152// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000153namespace {
154 struct CstSortPredicate {
155 ValueEnumerator &VE;
156 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
157 bool operator()(const std::pair<const Value*, unsigned> &LHS,
158 const std::pair<const Value*, unsigned> &RHS) {
159 // Sort by plane.
160 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000161 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000162 VE.getTypeID(RHS.first->getType());
163 // Then by frequency.
164 return LHS.second > RHS.second;
165 }
166 };
167}
Chris Lattner6da91d32007-05-04 05:21:47 +0000168
169/// OptimizeConstants - Reorder constant pool for denser encoding.
170void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
171 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000172
Chris Lattner6da91d32007-05-04 05:21:47 +0000173 CstSortPredicate P(*this);
174 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000175
Chris Lattner6da91d32007-05-04 05:21:47 +0000176 // Ensure that integer constants are at the start of the constant pool. This
177 // is important so that GEP structure indices come before gep constant exprs.
178 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
179 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000180
Chris Lattner6da91d32007-05-04 05:21:47 +0000181 // Rebuild the modified portion of ValueMap.
182 for (; CstStart != CstEnd; ++CstStart)
183 ValueMap[Values[CstStart].first] = CstStart+1;
184}
185
186
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000187/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
188/// table.
189void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000190 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000191 TI != TE; ++TI)
192 EnumerateType(TI->second);
193}
194
195/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
196/// table into the values table.
197void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000198 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000199 VI != VE; ++VI)
200 EnumerateValue(VI->getValue());
201}
202
Devang Patel0386f012010-01-07 19:39:36 +0000203/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata
204/// table.
205void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) {
206 for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end();
207 MI != ME; ++MI)
208 EnumerateValue(MI->getValue());
209}
210
Devang Patel8fba5782010-01-09 00:30:14 +0000211void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
212 // Check to see if it's already in!
213 unsigned &MDValueID = MDValueMap[MD];
214 if (MDValueID) {
215 // Increment use count.
216 MDValues[MDValueID-1].second++;
217 return;
218 }
219
220 // Enumerate the type of this value.
221 EnumerateType(MD->getType());
222
223 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
224 if (MDNode *E = MD->getOperand(i))
225 EnumerateValue(E);
226 MDValues.push_back(std::make_pair(MD, 1U));
227 MDValueMap[MD] = Values.size();
228}
229
Devang Patelbc5201f2010-01-22 22:52:10 +0000230void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000231 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000232 // Check to see if it's already in!
233 unsigned &MDValueID = MDValueMap[MD];
234 if (MDValueID) {
235 // Increment use count.
236 MDValues[MDValueID-1].second++;
237 return;
238 }
239
240 // Enumerate the type of this value.
241 EnumerateType(MD->getType());
242
243 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000244 MDValues.push_back(std::make_pair(MD, 1U));
245 MDValueMap[MD] = MDValues.size();
246 MDValueID = MDValues.size();
247 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
248 if (Value *V = N->getOperand(i))
249 EnumerateValue(V);
250 else
251 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Pateld5ac4042009-08-04 06:00:18 +0000252 }
253 return;
Chris Lattner837e04a2009-10-28 05:24:40 +0000254 }
255
Devang Pateld5ac4042009-08-04 06:00:18 +0000256 // Add the value.
Chris Lattnercc7b0112009-12-31 00:51:46 +0000257 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000258 MDValues.push_back(std::make_pair(MD, 1U));
259 MDValueID = MDValues.size();
260}
261
Victor Hernandezd7e64572010-01-14 19:54:11 +0000262void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000263 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Patelbc5201f2010-01-22 22:52:10 +0000264 if (isa<MDNode>(V) || isa<MDString>(V))
265 return EnumerateMetadata(V);
Devang Patel8fba5782010-01-09 00:30:14 +0000266 else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
267 return EnumerateNamedMDNode(NMD);
Devang Pateld5ac4042009-08-04 06:00:18 +0000268
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000269 // Check to see if it's already in!
270 unsigned &ValueID = ValueMap[V];
271 if (ValueID) {
272 // Increment use count.
273 Values[ValueID-1].second++;
274 return;
275 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000276
Chris Lattner7a303d12007-05-06 01:00:28 +0000277 // Enumerate the type of this value.
278 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000279
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000280 if (const Constant *C = dyn_cast<Constant>(V)) {
281 if (isa<GlobalValue>(C)) {
282 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000283 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
284 // Do not enumerate the initializers for an array of simple characters.
285 // The initializers just polute the value table, and we emit the strings
286 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000287 } else if (C->getNumOperands()) {
288 // If a constant has operands, enumerate them. This makes sure that if a
289 // constant has uses (for example an array of const ints), that they are
290 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000291
Chris Lattner7a303d12007-05-06 01:00:28 +0000292 // We prefer to enumerate them with values before we enumerate the user
293 // itself. This makes it more likely that we can avoid forward references
294 // in the reader. We know that there can be no cycles in the constants
295 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000296 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
297 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000298 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000299 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000300
Chris Lattner7a303d12007-05-06 01:00:28 +0000301 // Finally, add the value. Doing this could make the ValueID reference be
302 // dangling, don't reuse it.
303 Values.push_back(std::make_pair(V, 1U));
304 ValueMap[V] = Values.size();
305 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000306 }
307 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000308
Chris Lattner7a303d12007-05-06 01:00:28 +0000309 // Add the value.
310 Values.push_back(std::make_pair(V, 1U));
311 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000312}
313
314
315void ValueEnumerator::EnumerateType(const Type *Ty) {
316 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000317
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000318 if (TypeID) {
319 // If we've already seen this type, just increase its occurrence count.
320 Types[TypeID-1].second++;
321 return;
322 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000323
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000324 // First time we saw this type, add it.
325 Types.push_back(std::make_pair(Ty, 1U));
326 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000327
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000328 // Enumerate subtypes.
329 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
330 I != E; ++I)
331 EnumerateType(*I);
332}
333
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000334// Enumerate the types for the specified value. If the value is a constant,
335// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000336void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000337 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000338
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000339 if (const Constant *C = dyn_cast<Constant>(V)) {
340 // If this constant is already enumerated, ignore it, we know its type must
341 // be enumerated.
342 if (ValueMap.count(V)) return;
343
344 // This constant may have operands, make sure to enumerate the types in
345 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000346 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
347 const User *Op = C->getOperand(i);
348
349 // Don't enumerate basic blocks here, this happens as operands to
350 // blockaddress.
351 if (isa<BasicBlock>(Op)) continue;
352
Victor Hernandezd7e64572010-01-14 19:54:11 +0000353 EnumerateOperandType(cast<Constant>(Op));
Chris Lattner837e04a2009-10-28 05:24:40 +0000354 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000355
356 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000357 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
358 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000359 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000360 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000361 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000362 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000363}
364
Devang Patel05988662008-09-25 21:00:45 +0000365void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000366 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000367 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000368 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000369 if (Entry == 0) {
370 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000371 Attributes.push_back(PAL);
372 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000373 }
374}
375
376
Chris Lattner8d35c792007-04-26 03:50:57 +0000377void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000378 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000379 NumModuleValues = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000380
Chris Lattner8d35c792007-04-26 03:50:57 +0000381 // Adding function arguments to the value table.
382 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000383 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000384 EnumerateValue(I);
385
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000386 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000387
Chris Lattner8d35c792007-04-26 03:50:57 +0000388 // Add all function-level constants to the value table.
389 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
390 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000391 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000392 OI != E; ++OI) {
393 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
394 isa<InlineAsm>(*OI))
395 EnumerateValue(*OI);
396 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000397 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000398 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000399 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000400
Chris Lattner6da91d32007-05-04 05:21:47 +0000401 // Optimize the constant layout.
402 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000403
Duncan Sandsdc024672007-11-27 13:23:08 +0000404 // Add the function's parameter attributes so they are available for use in
405 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000406 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000407
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000408 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000409
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000410 SmallVector<MDNode *, 8> FunctionLocalMDs;
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))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000417 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000418 // Enumerate metadata after the instructions they might refer to.
419 FunctionLocalMDs.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000420 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000421 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000422 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000423 }
424 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000425
426 // Add all of the function-local metadata.
427 for (unsigned i = 0, e = FunctionLocalMDs.size(); i != e; ++i)
428 EnumerateOperandType(FunctionLocalMDs[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000429}
430
Chris Lattner8d35c792007-04-26 03:50:57 +0000431void ValueEnumerator::purgeFunction() {
432 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000433 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000434 ValueMap.erase(Values[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000435 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
436 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000437
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000438 Values.resize(NumModuleValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000439 BasicBlocks.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000440}
Chris Lattner837e04a2009-10-28 05:24:40 +0000441
442static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
443 DenseMap<const BasicBlock*, unsigned> &IDMap) {
444 unsigned Counter = 0;
445 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
446 IDMap[BB] = ++Counter;
447}
448
449/// getGlobalBasicBlockID - This returns the function-specific ID for the
450/// specified basic block. This is relatively expensive information, so it
451/// should only be used by rare constructs such as address-of-label.
452unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
453 unsigned &Idx = GlobalBasicBlockIDs[BB];
454 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000455 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000456
457 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
458 return getGlobalBasicBlockID(BB);
459}
460