blob: a1641043b22aaa715def57238ff80c6726d58885 [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/STLExtras.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000017#include "llvm/IR/Constants.h"
18#include "llvm/IR/DerivedTypes.h"
19#include "llvm/IR/Instructions.h"
20#include "llvm/IR/Module.h"
21#include "llvm/IR/ValueSymbolTable.h"
Chad Rosier4e6c03f2011-12-07 20:44:46 +000022#include "llvm/Support/Debug.h"
23#include "llvm/Support/raw_ostream.h"
Chris Lattner12f535b2007-05-04 05:05:48 +000024#include <algorithm>
Chris Lattnerfd57cec2007-04-22 06:24:45 +000025using namespace llvm;
26
Duncan Sands2333e292012-11-13 12:59:33 +000027static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
28 return V.first->getType()->isIntOrIntVectorTy();
Chris Lattner6da91d32007-05-04 05:21:47 +000029}
30
Chris Lattnerfd57cec2007-04-22 06:24:45 +000031/// ValueEnumerator - Enumerate module-level information.
32ValueEnumerator::ValueEnumerator(const Module *M) {
33 // Enumerate the global variables.
34 for (Module::const_global_iterator I = M->global_begin(),
35 E = M->global_end(); I != E; ++I)
36 EnumerateValue(I);
37
38 // Enumerate the functions.
Duncan Sandsdc024672007-11-27 13:23:08 +000039 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +000040 EnumerateValue(I);
Devang Patel05988662008-09-25 21:00:45 +000041 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000042 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000043
Chris Lattner07d98b42007-04-26 02:46:40 +000044 // Enumerate the aliases.
45 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
46 I != E; ++I)
47 EnumerateValue(I);
Daniel Dunbara279bc32009-09-20 02:20:51 +000048
Chris Lattner6da91d32007-05-04 05:21:47 +000049 // Remember what is the cutoff between globalvalue's and other constants.
50 unsigned FirstConstant = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +000051
Chris Lattnerfd57cec2007-04-22 06:24:45 +000052 // Enumerate the global variable initializers.
53 for (Module::const_global_iterator I = M->global_begin(),
54 E = M->global_end(); I != E; ++I)
55 if (I->hasInitializer())
56 EnumerateValue(I->getInitializer());
57
Chris Lattner07d98b42007-04-26 02:46:40 +000058 // Enumerate the aliasees.
59 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
60 I != E; ++I)
61 EnumerateValue(I->getAliasee());
Daniel Dunbara279bc32009-09-20 02:20:51 +000062
Peter Collingbourne1e3037f2013-09-16 01:08:15 +000063 // Enumerate the prefix data constants.
64 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
65 if (I->hasPrefixData())
66 EnumerateValue(I->getPrefixData());
67
Joe Abbeyef7964c2013-04-01 02:28:07 +000068 // Insert constants and metadata that are named at module level into the slot
Devang Patel0386f012010-01-07 19:39:36 +000069 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000070 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman17aa92c2010-07-21 23:38:33 +000071 EnumerateNamedMetadata(M);
Daniel Dunbara279bc32009-09-20 02:20:51 +000072
Chris Lattnercc7b0112009-12-31 00:51:46 +000073 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
74
Chris Lattner8d35c792007-04-26 03:50:57 +000075 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000076 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000077
Chris Lattner8d35c792007-04-26 03:50:57 +000078 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
79 I != E; ++I)
80 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000081
Chris Lattnerfd57cec2007-04-22 06:24:45 +000082 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
83 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000084 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000085 OI != E; ++OI) {
86 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000087 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000088 // These will get enumerated during function-incorporation.
89 continue;
90 EnumerateOperandType(*OI);
91 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000092 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000093 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000094 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000095 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000096 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +000097
Daniel Dunbara279bc32009-09-20 02:20:51 +000098 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +000099 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +0000100 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +0000101 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +0000102 EnumerateMetadata(MDs[i].second);
Joe Abbey170a15e2012-11-25 15:23:39 +0000103
Chris Lattnera6245242010-04-03 02:17:50 +0000104 if (!I->getDebugLoc().isUnknown()) {
105 MDNode *Scope, *IA;
106 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
107 if (Scope) EnumerateMetadata(Scope);
108 if (IA) EnumerateMetadata(IA);
109 }
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());
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000115}
116
Devang Patele8e02132009-09-18 19:26:43 +0000117unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
118 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattner1afcace2011-07-09 17:41:24 +0000119 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000120 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000121}
Devang Patele8e02132009-09-18 19:26:43 +0000122
123void ValueEnumerator::setInstructionID(const Instruction *I) {
124 InstructionMap[I] = InstructionCount++;
125}
126
Devang Pateld5ac4042009-08-04 06:00:18 +0000127unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000128 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000129 ValueMapType::const_iterator I = MDValueMap.find(V);
130 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
131 return I->second-1;
132 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000133
Devang Pateld5ac4042009-08-04 06:00:18 +0000134 ValueMapType::const_iterator I = ValueMap.find(V);
135 assert(I != ValueMap.end() && "Value not in slotcalculator!");
136 return I->second-1;
137}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000138
Chad Rosier4e6c03f2011-12-07 20:44:46 +0000139void ValueEnumerator::dump() const {
140 print(dbgs(), ValueMap, "Default");
141 dbgs() << '\n';
142 print(dbgs(), MDValueMap, "MetaData");
143 dbgs() << '\n';
144}
145
146void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
147 const char *Name) const {
148
149 OS << "Map Name: " << Name << "\n";
150 OS << "Size: " << Map.size() << "\n";
151 for (ValueMapType::const_iterator I = Map.begin(),
152 E = Map.end(); I != E; ++I) {
153
154 const Value *V = I->first;
155 if (V->hasName())
156 OS << "Value: " << V->getName();
157 else
158 OS << "Value: [null]\n";
159 V->dump();
160
161 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
162 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
163 UI != UE; ++UI) {
164 if (UI != V->use_begin())
165 OS << ",";
166 if((*UI)->hasName())
167 OS << " " << (*UI)->getName();
168 else
169 OS << " [null]";
170
171 }
172 OS << "\n\n";
173 }
174}
175
Chris Lattner6da91d32007-05-04 05:21:47 +0000176// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000177namespace {
178 struct CstSortPredicate {
179 ValueEnumerator &VE;
180 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
181 bool operator()(const std::pair<const Value*, unsigned> &LHS,
182 const std::pair<const Value*, unsigned> &RHS) {
183 // Sort by plane.
184 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000185 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000186 VE.getTypeID(RHS.first->getType());
187 // Then by frequency.
188 return LHS.second > RHS.second;
189 }
190 };
191}
Chris Lattner6da91d32007-05-04 05:21:47 +0000192
193/// OptimizeConstants - Reorder constant pool for denser encoding.
194void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
195 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000196
Chris Lattner6da91d32007-05-04 05:21:47 +0000197 CstSortPredicate P(*this);
198 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000199
Duncan Sands2333e292012-11-13 12:59:33 +0000200 // Ensure that integer and vector of integer constants are at the start of the
201 // constant pool. This is important so that GEP structure indices come before
202 // gep constant exprs.
Chris Lattner6da91d32007-05-04 05:21:47 +0000203 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sands2333e292012-11-13 12:59:33 +0000204 isIntOrIntVectorValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000205
Chris Lattner6da91d32007-05-04 05:21:47 +0000206 // Rebuild the modified portion of ValueMap.
207 for (; CstStart != CstEnd; ++CstStart)
208 ValueMap[Values[CstStart].first] = CstStart+1;
209}
210
211
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000212/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
213/// table into the values table.
214void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000215 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000216 VI != VE; ++VI)
217 EnumerateValue(VI->getValue());
218}
219
Dan Gohman17aa92c2010-07-21 23:38:33 +0000220/// EnumerateNamedMetadata - Insert all of the values referenced by
221/// named metadata in the specified module.
222void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
223 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
224 E = M->named_metadata_end(); I != E; ++I)
225 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000226}
227
Devang Patel8fba5782010-01-09 00:30:14 +0000228void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000229 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000230 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000231}
232
Dan Gohman309b3af2010-08-24 02:24:03 +0000233/// EnumerateMDNodeOperands - Enumerate all non-function-local values
234/// and types referenced by the given MDNode.
235void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
236 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
237 if (Value *V = N->getOperand(i)) {
238 if (isa<MDNode>(V) || isa<MDString>(V))
239 EnumerateMetadata(V);
240 else if (!isa<Instruction>(V) && !isa<Argument>(V))
241 EnumerateValue(V);
242 } else
243 EnumerateType(Type::getVoidTy(N->getContext()));
244 }
245}
246
Devang Patelbc5201f2010-01-22 22:52:10 +0000247void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000248 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000249
250 // Enumerate the type of this value.
251 EnumerateType(MD->getType());
252
253 const MDNode *N = dyn_cast<MDNode>(MD);
254
255 // In the module-level pass, skip function-local nodes themselves, but
256 // do walk their operands.
257 if (N && N->isFunctionLocal() && N->getFunction()) {
258 EnumerateMDNodeOperands(N);
259 return;
260 }
261
Devang Pateld5ac4042009-08-04 06:00:18 +0000262 // Check to see if it's already in!
263 unsigned &MDValueID = MDValueMap[MD];
264 if (MDValueID) {
265 // Increment use count.
266 MDValues[MDValueID-1].second++;
267 return;
268 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000269 MDValues.push_back(std::make_pair(MD, 1U));
270 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000271
272 // Enumerate all non-function-local operands.
273 if (N)
274 EnumerateMDNodeOperands(N);
275}
276
277/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
278/// information reachable from the given MDNode.
279void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
280 assert(N->isFunctionLocal() && N->getFunction() &&
281 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
282
283 // Enumerate the type of this value.
284 EnumerateType(N->getType());
285
286 // Check to see if it's already in!
287 unsigned &MDValueID = MDValueMap[N];
288 if (MDValueID) {
289 // Increment use count.
290 MDValues[MDValueID-1].second++;
291 return;
292 }
293 MDValues.push_back(std::make_pair(N, 1U));
294 MDValueID = MDValues.size();
295
296 // To incoroporate function-local information visit all function-local
297 // MDNodes and all function-local values they reference.
298 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
299 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000300 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000301 if (O->isFunctionLocal() && O->getFunction())
302 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000303 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000304 EnumerateValue(V);
305 }
306
307 // Also, collect all function-local MDNodes for easy access.
308 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000309}
310
Victor Hernandezd7e64572010-01-14 19:54:11 +0000311void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000312 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000313 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
314 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000315
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000316 // Check to see if it's already in!
317 unsigned &ValueID = ValueMap[V];
318 if (ValueID) {
319 // Increment use count.
320 Values[ValueID-1].second++;
321 return;
322 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000323
Chris Lattner7a303d12007-05-06 01:00:28 +0000324 // Enumerate the type of this value.
325 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000326
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000327 if (const Constant *C = dyn_cast<Constant>(V)) {
328 if (isa<GlobalValue>(C)) {
329 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner7a303d12007-05-06 01:00:28 +0000330 } else if (C->getNumOperands()) {
331 // If a constant has operands, enumerate them. This makes sure that if a
332 // constant has uses (for example an array of const ints), that they are
333 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000334
Chris Lattner7a303d12007-05-06 01:00:28 +0000335 // We prefer to enumerate them with values before we enumerate the user
336 // itself. This makes it more likely that we can avoid forward references
337 // in the reader. We know that there can be no cycles in the constants
338 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000339 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
340 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000341 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000342 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000343
Chris Lattner7a303d12007-05-06 01:00:28 +0000344 // Finally, add the value. Doing this could make the ValueID reference be
345 // dangling, don't reuse it.
346 Values.push_back(std::make_pair(V, 1U));
347 ValueMap[V] = Values.size();
348 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000349 }
350 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000351
Chris Lattner7a303d12007-05-06 01:00:28 +0000352 // Add the value.
353 Values.push_back(std::make_pair(V, 1U));
354 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000355}
356
357
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000358void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000359 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000361 // We've already seen this type.
Chris Lattner1afcace2011-07-09 17:41:24 +0000362 if (*TypeID)
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000363 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000364
Chris Lattner1afcace2011-07-09 17:41:24 +0000365 // If it is a non-anonymous struct, mark the type as being visited so that we
366 // don't recursively visit it. This is safe because we allow forward
367 // references of these in the bitcode reader.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000368 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner3ebb6492011-08-12 18:06:37 +0000369 if (!STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000370 *TypeID = ~0U;
Joe Abbey170a15e2012-11-25 15:23:39 +0000371
Chris Lattner1afcace2011-07-09 17:41:24 +0000372 // Enumerate all of the subtypes before we enumerate this type. This ensures
373 // that the type will be enumerated in an order that can be directly built.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000374 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
375 I != E; ++I)
376 EnumerateType(*I);
Joe Abbey170a15e2012-11-25 15:23:39 +0000377
Chris Lattner1afcace2011-07-09 17:41:24 +0000378 // Refresh the TypeID pointer in case the table rehashed.
379 TypeID = &TypeMap[Ty];
Joe Abbey170a15e2012-11-25 15:23:39 +0000380
Chris Lattner1afcace2011-07-09 17:41:24 +0000381 // Check to see if we got the pointer another way. This can happen when
382 // enumerating recursive types that hit the base case deeper than they start.
383 //
384 // If this is actually a struct that we are treating as forward ref'able,
385 // then emit the definition now that all of its contents are available.
386 if (*TypeID && *TypeID != ~0U)
387 return;
Joe Abbey170a15e2012-11-25 15:23:39 +0000388
Chris Lattner1afcace2011-07-09 17:41:24 +0000389 // Add this type now that its contents are all happily enumerated.
390 Types.push_back(Ty);
Joe Abbey170a15e2012-11-25 15:23:39 +0000391
Chris Lattner1afcace2011-07-09 17:41:24 +0000392 *TypeID = Types.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000393}
394
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000395// Enumerate the types for the specified value. If the value is a constant,
396// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000397void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000398 EnumerateType(V->getType());
Joe Abbey170a15e2012-11-25 15:23:39 +0000399
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000400 if (const Constant *C = dyn_cast<Constant>(V)) {
401 // If this constant is already enumerated, ignore it, we know its type must
402 // be enumerated.
403 if (ValueMap.count(V)) return;
404
405 // This constant may have operands, make sure to enumerate the types in
406 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000407 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
Jay Foad8340d0b2011-04-11 09:48:55 +0000408 const Value *Op = C->getOperand(i);
Joe Abbey170a15e2012-11-25 15:23:39 +0000409
Chris Lattner837e04a2009-10-28 05:24:40 +0000410 // Don't enumerate basic blocks here, this happens as operands to
411 // blockaddress.
412 if (isa<BasicBlock>(Op)) continue;
Joe Abbey170a15e2012-11-25 15:23:39 +0000413
Dan Gohman879d8112010-08-25 17:09:03 +0000414 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000415 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000416
417 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000418 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
419 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000420 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000421 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000422 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000423 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000424}
425
Bill Wendling105ea3d2013-02-12 08:01:22 +0000426void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000427 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling105ea3d2013-02-12 08:01:22 +0000428
Chris Lattner50954f52007-05-03 22:46:43 +0000429 // Do a lookup.
Bill Wendling105ea3d2013-02-12 08:01:22 +0000430 unsigned &Entry = AttributeMap[PAL];
Chris Lattner50954f52007-05-03 22:46:43 +0000431 if (Entry == 0) {
432 // Never saw this before, add it.
Bill Wendling034b94b2012-12-19 07:18:57 +0000433 Attribute.push_back(PAL);
434 Entry = Attribute.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000435 }
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000436
437 // Do lookups for all attribute groups.
438 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
439 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendlinge9229a62013-02-11 22:33:26 +0000440 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000441 if (Entry == 0) {
Bill Wendlinge9229a62013-02-11 22:33:26 +0000442 AttributeGroups.push_back(AS);
443 Entry = AttributeGroups.size();
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000444 }
445 }
Chris Lattner50954f52007-05-03 22:46:43 +0000446}
447
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000448void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000449 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000450 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000451 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000452
Chris Lattner8d35c792007-04-26 03:50:57 +0000453 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000454 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
455 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000456 EnumerateValue(I);
457
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000458 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000459
Chris Lattner8d35c792007-04-26 03:50:57 +0000460 // Add all function-level constants to the value table.
461 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
462 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000463 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000464 OI != E; ++OI) {
465 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
466 isa<InlineAsm>(*OI))
467 EnumerateValue(*OI);
468 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000469 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000470 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000471 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000472
Chris Lattner6da91d32007-05-04 05:21:47 +0000473 // Optimize the constant layout.
474 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000475
Duncan Sandsdc024672007-11-27 13:23:08 +0000476 // Add the function's parameter attributes so they are available for use in
477 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000478 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000479
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000480 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000481
Devang Patel62098692010-06-02 23:05:04 +0000482 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000483 // Add all of the instructions.
484 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000485 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000486 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
487 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000488 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000489 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000490 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000491 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000492 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000493
494 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
495 I->getAllMetadataOtherThanDebugLoc(MDs);
496 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
497 MDNode *N = MDs[i].second;
498 if (N->isFunctionLocal() && N->getFunction())
499 FnLocalMDVector.push_back(N);
500 }
Joe Abbey170a15e2012-11-25 15:23:39 +0000501
Benjamin Kramerf0127052010-01-05 13:12:22 +0000502 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000503 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000504 }
505 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000506
507 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000508 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000509 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000510}
511
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000512void ValueEnumerator::purgeFunction() {
Chris Lattner8d35c792007-04-26 03:50:57 +0000513 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000514 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000515 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000516 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
517 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000518 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
519 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000520
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000521 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000522 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000523 BasicBlocks.clear();
Dan Gohman848c9ae2010-08-25 17:11:16 +0000524 FunctionLocalMDs.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000525}
Chris Lattner837e04a2009-10-28 05:24:40 +0000526
527static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
528 DenseMap<const BasicBlock*, unsigned> &IDMap) {
529 unsigned Counter = 0;
530 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
531 IDMap[BB] = ++Counter;
532}
533
534/// getGlobalBasicBlockID - This returns the function-specific ID for the
535/// specified basic block. This is relatively expensive information, so it
536/// should only be used by rare constructs such as address-of-label.
537unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
538 unsigned &Idx = GlobalBasicBlockIDs[BB];
539 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000540 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000541
542 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
543 return getGlobalBasicBlockID(BB);
544}
545