blob: 9ae9905b9f1db8d99b5c1d0e37893e4e9c7919ac [file] [log] [blame]
Chris Lattnerfd57cec2007-04-22 06:24:45 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerfd57cec2007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
Rafael Espindolaf5a90562011-04-06 16:49:37 +000015#include "llvm/ADT/SmallPtrSet.h"
16#include "llvm/ADT/STLExtras.h"
Chris Lattnerff7fc5d2007-05-06 00:35:24 +000017#include "llvm/Constants.h"
Chris Lattner50954f52007-05-03 22:46:43 +000018#include "llvm/DerivedTypes.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000019#include "llvm/Module.h"
Chris Lattnerfd57cec2007-04-22 06:24:45 +000020#include "llvm/ValueSymbolTable.h"
Duncan Sandsdc024672007-11-27 13:23:08 +000021#include "llvm/Instructions.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000022#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000023using namespace llvm;
24
Chris Lattner6da91d32007-05-04 05:21:47 +000025static bool isIntegerValue(const std::pair<const Value*, unsigned> &V) {
Duncan Sands1df98592010-02-16 11:11:14 +000026 return V.first->getType()->isIntegerTy();
Chris Lattner6da91d32007-05-04 05:21:47 +000027}
28
Chris Lattnerfd57cec2007-04-22 06:24:45 +000029/// ValueEnumerator - Enumerate module-level information.
30ValueEnumerator::ValueEnumerator(const Module *M) {
31 // Enumerate the global variables.
32 for (Module::const_global_iterator I = M->global_begin(),
33 E = M->global_end(); I != E; ++I)
34 EnumerateValue(I);
35
36 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000037 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000038 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000039 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000040 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000041
Chris Lattner07d98b42007-04-26 02:46:40 +000042 // Enumerate the aliases.
43 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
44 I != E; ++I)
45 EnumerateValue(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +000046
Chris Lattner6da91d32007-05-04 05:21:47 +000047 // Remember what is the cutoff between globalvalue's and other constants.
48 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000049
Chris Lattnerfd57cec2007-04-22 06:24:45 +000050 // Enumerate the global variable initializers.
51 for (Module::const_global_iterator I = M->global_begin(),
52 E = M->global_end(); I != E; ++I)
53 if (I->hasInitializer())
54 EnumerateValue(I->getInitializer());
55
Chris Lattner07d98b42007-04-26 02:46:40 +000056 // Enumerate the aliasees.
57 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
58 I != E; ++I)
59 EnumerateValue(I->getAliasee());
Daniel Dunbara279bc32009-09-20 02:20:51 +000060
Bob Wilson54eee522010-06-19 05:33:57 +000061 // Insert constants and metadata that are named at module level into the slot
Devang Patel0386f012010-01-07 19:39:36 +000062 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000063 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman17aa92c2010-07-21 23:38:33 +000064 EnumerateNamedMetadata(M);
Daniel Dunbara279bc32009-09-20 02:20:51 +000065
Chris Lattnercc7b0112009-12-31 00:51:46 +000066 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
67
Chris Lattner8d35c792007-04-26 03:50:57 +000068 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000069 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000070
Chris Lattner8d35c792007-04-26 03:50:57 +000071 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
72 I != E; ++I)
73 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000074
Chris Lattnerfd57cec2007-04-22 06:24:45 +000075 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
76 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000077 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000078 OI != E; ++OI) {
79 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000080 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000081 // These will get enumerated during function-incorporation.
82 continue;
83 EnumerateOperandType(*OI);
84 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000085 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000086 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000087 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000088 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000089 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +000090
Daniel Dunbara279bc32009-09-20 02:20:51 +000091 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +000092 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +000093 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +000094 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +000095 EnumerateMetadata(MDs[i].second);
Chris Lattnera6245242010-04-03 02:17:50 +000096
97 if (!I->getDebugLoc().isUnknown()) {
98 MDNode *Scope, *IA;
99 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
100 if (Scope) EnumerateMetadata(Scope);
101 if (IA) EnumerateMetadata(IA);
102 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000103 }
104 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000105
Chris Lattner6da91d32007-05-04 05:21:47 +0000106 // Optimize constant ordering.
107 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000108}
109
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000110
Devang Patele8e02132009-09-18 19:26:43 +0000111unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
112 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattner1afcace2011-07-09 17:41:24 +0000113 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000114 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000115}
Devang Patele8e02132009-09-18 19:26:43 +0000116
117void ValueEnumerator::setInstructionID(const Instruction *I) {
118 InstructionMap[I] = InstructionCount++;
119}
120
Devang Pateld5ac4042009-08-04 06:00:18 +0000121unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000122 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000123 ValueMapType::const_iterator I = MDValueMap.find(V);
124 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
125 return I->second-1;
126 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000127
Devang Pateld5ac4042009-08-04 06:00:18 +0000128 ValueMapType::const_iterator I = ValueMap.find(V);
129 assert(I != ValueMap.end() && "Value not in slotcalculator!");
130 return I->second-1;
131}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000132
Chris Lattner6da91d32007-05-04 05:21:47 +0000133// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000134namespace {
135 struct CstSortPredicate {
136 ValueEnumerator &VE;
137 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
138 bool operator()(const std::pair<const Value*, unsigned> &LHS,
139 const std::pair<const Value*, unsigned> &RHS) {
140 // Sort by plane.
141 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000142 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000143 VE.getTypeID(RHS.first->getType());
144 // Then by frequency.
145 return LHS.second > RHS.second;
146 }
147 };
148}
Chris Lattner6da91d32007-05-04 05:21:47 +0000149
150/// OptimizeConstants - Reorder constant pool for denser encoding.
151void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
152 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000153
Chris Lattner6da91d32007-05-04 05:21:47 +0000154 CstSortPredicate P(*this);
155 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000156
Chris Lattner6da91d32007-05-04 05:21:47 +0000157 // Ensure that integer constants are at the start of the constant pool. This
158 // is important so that GEP structure indices come before gep constant exprs.
159 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
160 isIntegerValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000161
Chris Lattner6da91d32007-05-04 05:21:47 +0000162 // Rebuild the modified portion of ValueMap.
163 for (; CstStart != CstEnd; ++CstStart)
164 ValueMap[Values[CstStart].first] = CstStart+1;
165}
166
167
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000168/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
169/// table into the values table.
170void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000171 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000172 VI != VE; ++VI)
173 EnumerateValue(VI->getValue());
174}
175
Dan Gohman17aa92c2010-07-21 23:38:33 +0000176/// EnumerateNamedMetadata - Insert all of the values referenced by
177/// named metadata in the specified module.
178void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
179 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
180 E = M->named_metadata_end(); I != E; ++I)
181 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000182}
183
Devang Patel8fba5782010-01-09 00:30:14 +0000184void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000185 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000186 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000187}
188
Dan Gohman309b3af2010-08-24 02:24:03 +0000189/// EnumerateMDNodeOperands - Enumerate all non-function-local values
190/// and types referenced by the given MDNode.
191void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
192 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
193 if (Value *V = N->getOperand(i)) {
194 if (isa<MDNode>(V) || isa<MDString>(V))
195 EnumerateMetadata(V);
196 else if (!isa<Instruction>(V) && !isa<Argument>(V))
197 EnumerateValue(V);
198 } else
199 EnumerateType(Type::getVoidTy(N->getContext()));
200 }
201}
202
Devang Patelbc5201f2010-01-22 22:52:10 +0000203void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000204 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000205
206 // Enumerate the type of this value.
207 EnumerateType(MD->getType());
208
209 const MDNode *N = dyn_cast<MDNode>(MD);
210
211 // In the module-level pass, skip function-local nodes themselves, but
212 // do walk their operands.
213 if (N && N->isFunctionLocal() && N->getFunction()) {
214 EnumerateMDNodeOperands(N);
215 return;
216 }
217
Devang Pateld5ac4042009-08-04 06:00:18 +0000218 // Check to see if it's already in!
219 unsigned &MDValueID = MDValueMap[MD];
220 if (MDValueID) {
221 // Increment use count.
222 MDValues[MDValueID-1].second++;
223 return;
224 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000225 MDValues.push_back(std::make_pair(MD, 1U));
226 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000227
228 // Enumerate all non-function-local operands.
229 if (N)
230 EnumerateMDNodeOperands(N);
231}
232
233/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
234/// information reachable from the given MDNode.
235void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
236 assert(N->isFunctionLocal() && N->getFunction() &&
237 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
238
239 // Enumerate the type of this value.
240 EnumerateType(N->getType());
241
242 // Check to see if it's already in!
243 unsigned &MDValueID = MDValueMap[N];
244 if (MDValueID) {
245 // Increment use count.
246 MDValues[MDValueID-1].second++;
247 return;
248 }
249 MDValues.push_back(std::make_pair(N, 1U));
250 MDValueID = MDValues.size();
251
252 // To incoroporate function-local information visit all function-local
253 // MDNodes and all function-local values they reference.
254 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
255 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000256 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000257 if (O->isFunctionLocal() && O->getFunction())
258 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000259 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000260 EnumerateValue(V);
261 }
262
263 // Also, collect all function-local MDNodes for easy access.
264 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000265}
266
Victor Hernandezd7e64572010-01-14 19:54:11 +0000267void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000268 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000269 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
270 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000271
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000272 // Check to see if it's already in!
273 unsigned &ValueID = ValueMap[V];
274 if (ValueID) {
275 // Increment use count.
276 Values[ValueID-1].second++;
277 return;
278 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000279
Chris Lattner7a303d12007-05-06 01:00:28 +0000280 // Enumerate the type of this value.
281 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000282
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000283 if (const Constant *C = dyn_cast<Constant>(V)) {
284 if (isa<GlobalValue>(C)) {
285 // Initializers for globals are handled explicitly elsewhere.
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000286 } else if (isa<ConstantArray>(C) && cast<ConstantArray>(C)->isString()) {
287 // Do not enumerate the initializers for an array of simple characters.
Chris Lattner7a2bdde2011-04-15 05:18:47 +0000288 // The initializers just pollute the value table, and we emit the strings
Chris Lattnerff7fc5d2007-05-06 00:35:24 +0000289 // specially.
Chris Lattner7a303d12007-05-06 01:00:28 +0000290 } else if (C->getNumOperands()) {
291 // If a constant has operands, enumerate them. This makes sure that if a
292 // constant has uses (for example an array of const ints), that they are
293 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000294
Chris Lattner7a303d12007-05-06 01:00:28 +0000295 // We prefer to enumerate them with values before we enumerate the user
296 // itself. This makes it more likely that we can avoid forward references
297 // in the reader. We know that there can be no cycles in the constants
298 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000299 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
300 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000301 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000302 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000303
Chris Lattner7a303d12007-05-06 01:00:28 +0000304 // Finally, add the value. Doing this could make the ValueID reference be
305 // dangling, don't reuse it.
306 Values.push_back(std::make_pair(V, 1U));
307 ValueMap[V] = Values.size();
308 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000309 }
310 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000311
Chris Lattner7a303d12007-05-06 01:00:28 +0000312 // Add the value.
313 Values.push_back(std::make_pair(V, 1U));
314 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000315}
316
317
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000318void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000319 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000320
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000321 // We've already seen this type.
Chris Lattner1afcace2011-07-09 17:41:24 +0000322 if (*TypeID)
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000323 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000324
Chris Lattner1afcace2011-07-09 17:41:24 +0000325 // If it is a non-anonymous struct, mark the type as being visited so that we
326 // don't recursively visit it. This is safe because we allow forward
327 // references of these in the bitcode reader.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000328 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner3ebb6492011-08-12 18:06:37 +0000329 if (!STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000330 *TypeID = ~0U;
331
332 // Enumerate all of the subtypes before we enumerate this type. This ensures
333 // that the type will be enumerated in an order that can be directly built.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000334 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
335 I != E; ++I)
336 EnumerateType(*I);
Chris Lattner1afcace2011-07-09 17:41:24 +0000337
338 // Refresh the TypeID pointer in case the table rehashed.
339 TypeID = &TypeMap[Ty];
340
341 // Check to see if we got the pointer another way. This can happen when
342 // enumerating recursive types that hit the base case deeper than they start.
343 //
344 // If this is actually a struct that we are treating as forward ref'able,
345 // then emit the definition now that all of its contents are available.
346 if (*TypeID && *TypeID != ~0U)
347 return;
348
349 // Add this type now that its contents are all happily enumerated.
350 Types.push_back(Ty);
351
352 *TypeID = Types.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000353}
354
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000355// Enumerate the types for the specified value. If the value is a constant,
356// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000357void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000358 EnumerateType(V->getType());
Victor Hernandezab9cd102010-01-13 19:36:16 +0000359
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000360 if (const Constant *C = dyn_cast<Constant>(V)) {
361 // If this constant is already enumerated, ignore it, we know its type must
362 // be enumerated.
363 if (ValueMap.count(V)) return;
364
365 // This constant may have operands, make sure to enumerate the types in
366 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000367 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
Jay Foad8340d0b2011-04-11 09:48:55 +0000368 const Value *Op = C->getOperand(i);
Chris Lattner837e04a2009-10-28 05:24:40 +0000369
370 // Don't enumerate basic blocks here, this happens as operands to
371 // blockaddress.
372 if (isa<BasicBlock>(Op)) continue;
373
Dan Gohman879d8112010-08-25 17:09:03 +0000374 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000375 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000376
377 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000378 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
379 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000380 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000381 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000382 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000383 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000384}
385
Devang Patel05988662008-09-25 21:00:45 +0000386void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000387 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000388 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000389 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000390 if (Entry == 0) {
391 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000392 Attributes.push_back(PAL);
393 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000394 }
395}
396
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000397void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000398 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000399 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000400 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000401
Chris Lattner8d35c792007-04-26 03:50:57 +0000402 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000403 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
404 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000405 EnumerateValue(I);
406
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000407 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000408
Chris Lattner8d35c792007-04-26 03:50:57 +0000409 // Add all function-level constants to the value table.
410 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
411 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000412 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000413 OI != E; ++OI) {
414 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
415 isa<InlineAsm>(*OI))
416 EnumerateValue(*OI);
417 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000418 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000419 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000420 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000421
Chris Lattner6da91d32007-05-04 05:21:47 +0000422 // Optimize the constant layout.
423 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000424
Duncan Sandsdc024672007-11-27 13:23:08 +0000425 // Add the function's parameter attributes so they are available for use in
426 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000427 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000428
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000429 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000430
Devang Patel62098692010-06-02 23:05:04 +0000431 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000432 // Add all of the instructions.
433 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000434 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000435 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
436 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000437 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000438 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000439 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000440 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000441 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000442
443 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
444 I->getAllMetadataOtherThanDebugLoc(MDs);
445 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
446 MDNode *N = MDs[i].second;
447 if (N->isFunctionLocal() && N->getFunction())
448 FnLocalMDVector.push_back(N);
449 }
450
Benjamin Kramerf0127052010-01-05 13:12:22 +0000451 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000452 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000453 }
454 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000455
456 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000457 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000458 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000459}
460
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000461void ValueEnumerator::purgeFunction() {
Chris Lattner8d35c792007-04-26 03:50:57 +0000462 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000463 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000464 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000465 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
466 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000467 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
468 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000469
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000470 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000471 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000472 BasicBlocks.clear();
Dan Gohman848c9ae2010-08-25 17:11:16 +0000473 FunctionLocalMDs.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000474}
Chris Lattner837e04a2009-10-28 05:24:40 +0000475
476static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
477 DenseMap<const BasicBlock*, unsigned> &IDMap) {
478 unsigned Counter = 0;
479 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
480 IDMap[BB] = ++Counter;
481}
482
483/// getGlobalBasicBlockID - This returns the function-specific ID for the
484/// specified basic block. This is relatively expensive information, so it
485/// should only be used by rare constructs such as address-of-label.
486unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
487 unsigned &Idx = GlobalBasicBlockIDs[BB];
488 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000489 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000490
491 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
492 return getGlobalBasicBlockID(BB);
493}
494