blob: ef0a61ee2a1a740686f5074b1b6a56c7cec0d0ad [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();
Chris Lattnerfd57cec2007-04-22 06:24:45 +000094 OI != E; ++OI)
Victor Hernandezab9cd102010-01-13 19:36:16 +000095 EnumerateOperandType(*OI, true);
Chris Lattnerfd57cec2007-04-22 06:24:45 +000096 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000097 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000098 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000099 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +0000100 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +0000101
Daniel Dunbara279bc32009-09-20 02:20:51 +0000102 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +0000103 MDs.clear();
Chris Lattner3990b122009-12-28 23:41:32 +0000104 I->getAllMetadata(MDs);
105 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezab9cd102010-01-13 19:36:16 +0000106 EnumerateMetadata(MDs[i].second, true);
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
Chris Lattner12f535b2007-05-04 05:05:48 +0000113 // Sort the type table by frequency so that most commonly used types are early
114 // in the table (have low bit-width).
115 std::stable_sort(Types.begin(), Types.end(), CompareByFrequency);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000116
Dan Gohmane4977cf2008-05-23 01:55:30 +0000117 // Partition the Type ID's so that the single-value types occur before the
Chris Lattner12f535b2007-05-04 05:05:48 +0000118 // aggregate types. This allows the aggregate types to be dropped from the
119 // type table after parsing the global variable initializers.
Dan Gohmane4977cf2008-05-23 01:55:30 +0000120 std::partition(Types.begin(), Types.end(), isSingleValueType);
Chris Lattner12f535b2007-05-04 05:05:48 +0000121
122 // Now that we rearranged the type table, rebuild TypeMap.
123 for (unsigned i = 0, e = Types.size(); i != e; ++i)
124 TypeMap[Types[i].first] = i+1;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000125}
126
Devang Patele8e02132009-09-18 19:26:43 +0000127unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
128 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
129 assert (I != InstructionMap.end() && "Instruction is not mapped!");
130 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000131}
Devang Patele8e02132009-09-18 19:26:43 +0000132
133void ValueEnumerator::setInstructionID(const Instruction *I) {
134 InstructionMap[I] = InstructionCount++;
135}
136
Devang Pateld5ac4042009-08-04 06:00:18 +0000137unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patel380e80f2010-01-09 01:24:03 +0000138 if (isa<MetadataBase>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000139 ValueMapType::const_iterator I = MDValueMap.find(V);
140 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
141 return I->second-1;
142 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000143
Devang Pateld5ac4042009-08-04 06:00:18 +0000144 ValueMapType::const_iterator I = ValueMap.find(V);
145 assert(I != ValueMap.end() && "Value not in slotcalculator!");
146 return I->second-1;
147}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000148
Chris Lattner6da91d32007-05-04 05:21:47 +0000149// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000150namespace {
151 struct CstSortPredicate {
152 ValueEnumerator &VE;
153 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
154 bool operator()(const std::pair<const Value*, unsigned> &LHS,
155 const std::pair<const Value*, unsigned> &RHS) {
156 // Sort by plane.
157 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000158 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000159 VE.getTypeID(RHS.first->getType());
160 // Then by frequency.
161 return LHS.second > RHS.second;
162 }
163 };
164}
Chris Lattner6da91d32007-05-04 05:21:47 +0000165
166/// OptimizeConstants - Reorder constant pool for denser encoding.
167void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
168 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000169
Chris Lattner6da91d32007-05-04 05:21:47 +0000170 CstSortPredicate P(*this);
171 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000172
Chris Lattner6da91d32007-05-04 05:21:47 +0000173 // Ensure that integer constants are at the start of the constant pool. This
174 // is important so that GEP structure indices come before gep constant exprs.
175 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
176 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000177
Chris Lattner6da91d32007-05-04 05:21:47 +0000178 // Rebuild the modified portion of ValueMap.
179 for (; CstStart != CstEnd; ++CstStart)
180 ValueMap[Values[CstStart].first] = CstStart+1;
181}
182
183
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000184/// EnumerateTypeSymbolTable - Insert all of the types in the specified symbol
185/// table.
186void ValueEnumerator::EnumerateTypeSymbolTable(const TypeSymbolTable &TST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000187 for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000188 TI != TE; ++TI)
189 EnumerateType(TI->second);
190}
191
192/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
193/// table into the values table.
194void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000195 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000196 VI != VE; ++VI)
197 EnumerateValue(VI->getValue());
198}
199
Devang Patel0386f012010-01-07 19:39:36 +0000200/// EnumerateMDSymbolTable - Insert all of the values in the specified metadata
201/// table.
202void ValueEnumerator::EnumerateMDSymbolTable(const MDSymbolTable &MST) {
203 for (MDSymbolTable::const_iterator MI = MST.begin(), ME = MST.end();
204 MI != ME; ++MI)
205 EnumerateValue(MI->getValue());
206}
207
Devang Patel8fba5782010-01-09 00:30:14 +0000208void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
209 // Check to see if it's already in!
210 unsigned &MDValueID = MDValueMap[MD];
211 if (MDValueID) {
212 // Increment use count.
213 MDValues[MDValueID-1].second++;
214 return;
215 }
216
217 // Enumerate the type of this value.
218 EnumerateType(MD->getType());
219
220 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
221 if (MDNode *E = MD->getOperand(i))
222 EnumerateValue(E);
223 MDValues.push_back(std::make_pair(MD, 1U));
224 MDValueMap[MD] = Values.size();
225}
226
Victor Hernandezab9cd102010-01-13 19:36:16 +0000227void ValueEnumerator::EnumerateMetadata(const MetadataBase *MD, bool isGlobal) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000228 // Check to see if it's already in!
229 unsigned &MDValueID = MDValueMap[MD];
230 if (MDValueID) {
231 // Increment use count.
232 MDValues[MDValueID-1].second++;
233 return;
234 }
235
236 // Enumerate the type of this value.
237 EnumerateType(MD->getType());
238
239 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000240 if ((isGlobal && !N->isFunctionLocal()) ||
241 (!isGlobal && N->isFunctionLocal())) {
242 MDValues.push_back(std::make_pair(MD, 1U));
243 MDValueMap[MD] = MDValues.size();
244 MDValueID = MDValues.size();
Victor Hernandezd0f20ac2010-01-14 01:46:02 +0000245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
Victor Hernandezab9cd102010-01-13 19:36:16 +0000246 if (Value *V = N->getOperand(i))
247 EnumerateValue(V);
248 else
249 EnumerateType(Type::getVoidTy(MD->getContext()));
Devang Pateld5ac4042009-08-04 06:00:18 +0000250 }
251 return;
Chris Lattner837e04a2009-10-28 05:24:40 +0000252 }
253
Devang Pateld5ac4042009-08-04 06:00:18 +0000254 // Add the value.
Chris Lattnercc7b0112009-12-31 00:51:46 +0000255 assert(isa<MDString>(MD) && "Unknown metadata kind");
Devang Pateld5ac4042009-08-04 06:00:18 +0000256 MDValues.push_back(std::make_pair(MD, 1U));
257 MDValueID = MDValues.size();
258}
259
Victor Hernandezab9cd102010-01-13 19:36:16 +0000260void ValueEnumerator::EnumerateValue(const Value *V, bool isGlobal) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000261 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000262 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
Victor Hernandezab9cd102010-01-13 19:36:16 +0000263 return EnumerateMetadata(MB, isGlobal);
Devang Patel8fba5782010-01-09 00:30:14 +0000264 else if (const NamedMDNode *NMD = dyn_cast<NamedMDNode>(V))
265 return EnumerateNamedMDNode(NMD);
Devang Pateld5ac4042009-08-04 06:00:18 +0000266
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000267 // Check to see if it's already in!
268 unsigned &ValueID = ValueMap[V];
269 if (ValueID) {
270 // Increment use count.
271 Values[ValueID-1].second++;
272 return;
273 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000274
Chris Lattner7a303d12007-05-06 01:00:28 +0000275 // Enumerate the type of this value.
276 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000277
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000278 if (const Constant *C = dyn_cast<Constant>(V)) {
279 if (isa<GlobalValue>(C)) {
280 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000281 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
282 // Do not enumerate the initializers for an array of simple characters.
283 // The initializers just polute the value table, and we emit the strings
284 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000285 } else if (C->getNumOperands()) {
286 // If a constant has operands, enumerate them. This makes sure that if a
287 // constant has uses (for example an array of const ints), that they are
288 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000289
Chris Lattner7a303d12007-05-06 01:00:28 +0000290 // We prefer to enumerate them with values before we enumerate the user
291 // itself. This makes it more likely that we can avoid forward references
292 // in the reader. We know that there can be no cycles in the constants
293 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000294 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
295 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000296 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezab9cd102010-01-13 19:36:16 +0000297 EnumerateValue(*I, isGlobal);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000298
Chris Lattner7a303d12007-05-06 01:00:28 +0000299 // Finally, add the value. Doing this could make the ValueID reference be
300 // dangling, don't reuse it.
301 Values.push_back(std::make_pair(V, 1U));
302 ValueMap[V] = Values.size();
303 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000304 }
305 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000306
Chris Lattner7a303d12007-05-06 01:00:28 +0000307 // Add the value.
308 Values.push_back(std::make_pair(V, 1U));
309 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000310}
311
312
313void ValueEnumerator::EnumerateType(const Type *Ty) {
314 unsigned &TypeID = TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000315
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000316 if (TypeID) {
317 // If we've already seen this type, just increase its occurrence count.
318 Types[TypeID-1].second++;
319 return;
320 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000321
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000322 // First time we saw this type, add it.
323 Types.push_back(std::make_pair(Ty, 1U));
324 TypeID = Types.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000325
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000326 // Enumerate subtypes.
327 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
328 I != E; ++I)
329 EnumerateType(*I);
330}
331
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000332// Enumerate the types for the specified value. If the value is a constant,
333// walk through it, enumerating the types of the constant.
Victor Hernandezab9cd102010-01-13 19:36:16 +0000334void ValueEnumerator::EnumerateOperandType(const Value *V, bool isGlobal) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000335 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000336
337 // During function-incorporation, only enumerate metadata operands.
338 if (!isGlobal)
339 if (const MetadataBase *MB = dyn_cast<MetadataBase>(V))
340 return EnumerateMetadata(MB, isGlobal);
341
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000342 if (const Constant *C = dyn_cast<Constant>(V)) {
343 // If this constant is already enumerated, ignore it, we know its type must
344 // be enumerated.
345 if (ValueMap.count(V)) return;
346
347 // This constant may have operands, make sure to enumerate the types in
348 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000349 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
350 const User *Op = C->getOperand(i);
351
352 // Don't enumerate basic blocks here, this happens as operands to
353 // blockaddress.
354 if (isa<BasicBlock>(Op)) continue;
355
Victor Hernandezab9cd102010-01-13 19:36:16 +0000356 EnumerateOperandType(cast<Constant>(Op), isGlobal);
Chris Lattner837e04a2009-10-28 05:24:40 +0000357 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000358
359 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000360 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
361 if (Value *Elem = N->getOperand(i))
Victor Hernandezab9cd102010-01-13 19:36:16 +0000362 EnumerateOperandType(Elem, isGlobal);
Nick Lewyckycb337992009-05-10 20:57:05 +0000363 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000364 } else if (isa<MDString>(V) || isa<MDNode>(V))
Devang Patele54abc92009-07-22 17:43:22 +0000365 EnumerateValue(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000366}
367
Devang Patel05988662008-09-25 21:00:45 +0000368void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000369 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000370 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000371 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000372 if (Entry == 0) {
373 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000374 Attributes.push_back(PAL);
375 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000376 }
377}
378
379
Chris Lattner8d35c792007-04-26 03:50:57 +0000380void ValueEnumerator::incorporateFunction(const Function &F) {
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000381 NumModuleValues = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000382
Chris Lattner8d35c792007-04-26 03:50:57 +0000383 // Adding function arguments to the value table.
384 for(Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000385 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000386 EnumerateValue(I);
387
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000388 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000389
Chris Lattner8d35c792007-04-26 03:50:57 +0000390 // Add all function-level constants to the value table.
391 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
392 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000393 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000394 OI != E; ++OI) {
395 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
396 isa<InlineAsm>(*OI))
397 EnumerateValue(*OI);
398 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000399 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000400 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000401 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000402
Chris Lattner6da91d32007-05-04 05:21:47 +0000403 // Optimize the constant layout.
404 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000405
Duncan Sandsdc024672007-11-27 13:23:08 +0000406 // Add the function's parameter attributes so they are available for use in
407 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000408 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000409
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000410 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000411
Chris Lattner8d35c792007-04-26 03:50:57 +0000412 // Add all of the instructions.
413 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000414 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000415 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
416 OI != E; ++OI) {
417 EnumerateOperandType(*OI, false);
418 }
Benjamin Kramerf0127052010-01-05 13:12:22 +0000419 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000420 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000421 }
422 }
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