blob: 15f8034a36911fecd323ec2ed99010d678d5b7a5 [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.
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070076 for (const Function &F : *M) {
77 for (const Argument &A : F.args())
78 EnumerateType(A.getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +000079
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070080 for (const BasicBlock &BB : F)
81 for (const Instruction &I : BB) {
82 for (const Use &Op : I.operands()) {
83 if (MDNode *MD = dyn_cast<MDNode>(&Op))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000084 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000085 // These will get enumerated during function-incorporation.
86 continue;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070087 EnumerateOperandType(Op);
Victor Hernandezd7e64572010-01-14 19:54:11 +000088 }
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070089 EnumerateType(I.getType());
90 if (const CallInst *CI = dyn_cast<CallInst>(&I))
Devang Patel05988662008-09-25 21:00:45 +000091 EnumerateAttributes(CI->getAttributes());
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070092 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
Devang Patel05988662008-09-25 21:00:45 +000093 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +000094
Daniel Dunbara279bc32009-09-20 02:20:51 +000095 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +000096 MDs.clear();
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -070097 I.getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +000098 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +000099 EnumerateMetadata(MDs[i].second);
Joe Abbey170a15e2012-11-25 15:23:39 +0000100
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700101 if (!I.getDebugLoc().isUnknown()) {
Chris Lattnera6245242010-04-03 02:17:50 +0000102 MDNode *Scope, *IA;
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700103 I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
Chris Lattnera6245242010-04-03 02:17:50 +0000104 if (Scope) EnumerateMetadata(Scope);
105 if (IA) EnumerateMetadata(IA);
106 }
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());
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000112}
113
Devang Patele8e02132009-09-18 19:26:43 +0000114unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
115 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattner1afcace2011-07-09 17:41:24 +0000116 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000117 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000118}
Devang Patele8e02132009-09-18 19:26:43 +0000119
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700120unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
121 unsigned ComdatID = Comdats.idFor(C);
122 assert(ComdatID && "Comdat not found!");
123 return ComdatID;
124}
125
Devang Patele8e02132009-09-18 19:26:43 +0000126void ValueEnumerator::setInstructionID(const Instruction *I) {
127 InstructionMap[I] = InstructionCount++;
128}
129
Devang Pateld5ac4042009-08-04 06:00:18 +0000130unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000131 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000132 ValueMapType::const_iterator I = MDValueMap.find(V);
133 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
134 return I->second-1;
135 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000136
Devang Pateld5ac4042009-08-04 06:00:18 +0000137 ValueMapType::const_iterator I = ValueMap.find(V);
138 assert(I != ValueMap.end() && "Value not in slotcalculator!");
139 return I->second-1;
140}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000141
Chad Rosier4e6c03f2011-12-07 20:44:46 +0000142void ValueEnumerator::dump() const {
143 print(dbgs(), ValueMap, "Default");
144 dbgs() << '\n';
145 print(dbgs(), MDValueMap, "MetaData");
146 dbgs() << '\n';
147}
148
149void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
150 const char *Name) const {
151
152 OS << "Map Name: " << Name << "\n";
153 OS << "Size: " << Map.size() << "\n";
154 for (ValueMapType::const_iterator I = Map.begin(),
155 E = Map.end(); I != E; ++I) {
156
157 const Value *V = I->first;
158 if (V->hasName())
159 OS << "Value: " << V->getName();
160 else
161 OS << "Value: [null]\n";
162 V->dump();
163
164 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
Stephen Hines36b56882014-04-23 16:57:46 -0700165 for (const Use &U : V->uses()) {
166 if (&U != &*V->use_begin())
Chad Rosier4e6c03f2011-12-07 20:44:46 +0000167 OS << ",";
Stephen Hines36b56882014-04-23 16:57:46 -0700168 if(U->hasName())
169 OS << " " << U->getName();
Chad Rosier4e6c03f2011-12-07 20:44:46 +0000170 else
171 OS << " [null]";
172
173 }
174 OS << "\n\n";
175 }
176}
177
Chris Lattner6da91d32007-05-04 05:21:47 +0000178/// OptimizeConstants - Reorder constant pool for denser encoding.
179void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
180 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000181
Stephen Hines36b56882014-04-23 16:57:46 -0700182 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
183 [this](const std::pair<const Value *, unsigned> &LHS,
184 const std::pair<const Value *, unsigned> &RHS) {
185 // Sort by plane.
186 if (LHS.first->getType() != RHS.first->getType())
187 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
188 // Then by frequency.
189 return LHS.second > RHS.second;
190 });
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Duncan Sands2333e292012-11-13 12:59:33 +0000192 // Ensure that integer and vector of integer constants are at the start of the
193 // constant pool. This is important so that GEP structure indices come before
194 // gep constant exprs.
Chris Lattner6da91d32007-05-04 05:21:47 +0000195 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sands2333e292012-11-13 12:59:33 +0000196 isIntOrIntVectorValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000197
Chris Lattner6da91d32007-05-04 05:21:47 +0000198 // Rebuild the modified portion of ValueMap.
199 for (; CstStart != CstEnd; ++CstStart)
200 ValueMap[Values[CstStart].first] = CstStart+1;
201}
202
203
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000204/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
205/// table into the values table.
206void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000207 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000208 VI != VE; ++VI)
209 EnumerateValue(VI->getValue());
210}
211
Dan Gohman17aa92c2010-07-21 23:38:33 +0000212/// EnumerateNamedMetadata - Insert all of the values referenced by
213/// named metadata in the specified module.
214void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
215 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
216 E = M->named_metadata_end(); I != E; ++I)
217 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000218}
219
Devang Patel8fba5782010-01-09 00:30:14 +0000220void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000221 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000222 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000223}
224
Dan Gohman309b3af2010-08-24 02:24:03 +0000225/// EnumerateMDNodeOperands - Enumerate all non-function-local values
226/// and types referenced by the given MDNode.
227void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
228 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
229 if (Value *V = N->getOperand(i)) {
230 if (isa<MDNode>(V) || isa<MDString>(V))
231 EnumerateMetadata(V);
232 else if (!isa<Instruction>(V) && !isa<Argument>(V))
233 EnumerateValue(V);
234 } else
235 EnumerateType(Type::getVoidTy(N->getContext()));
236 }
237}
238
Devang Patelbc5201f2010-01-22 22:52:10 +0000239void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000240 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000241
242 // Enumerate the type of this value.
243 EnumerateType(MD->getType());
244
245 const MDNode *N = dyn_cast<MDNode>(MD);
246
247 // In the module-level pass, skip function-local nodes themselves, but
248 // do walk their operands.
249 if (N && N->isFunctionLocal() && N->getFunction()) {
250 EnumerateMDNodeOperands(N);
251 return;
252 }
253
Devang Pateld5ac4042009-08-04 06:00:18 +0000254 // Check to see if it's already in!
255 unsigned &MDValueID = MDValueMap[MD];
256 if (MDValueID) {
257 // Increment use count.
258 MDValues[MDValueID-1].second++;
259 return;
260 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000261 MDValues.push_back(std::make_pair(MD, 1U));
262 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000263
264 // Enumerate all non-function-local operands.
265 if (N)
266 EnumerateMDNodeOperands(N);
267}
268
269/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
270/// information reachable from the given MDNode.
271void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
272 assert(N->isFunctionLocal() && N->getFunction() &&
273 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
274
275 // Enumerate the type of this value.
276 EnumerateType(N->getType());
277
278 // Check to see if it's already in!
279 unsigned &MDValueID = MDValueMap[N];
280 if (MDValueID) {
281 // Increment use count.
282 MDValues[MDValueID-1].second++;
283 return;
284 }
285 MDValues.push_back(std::make_pair(N, 1U));
286 MDValueID = MDValues.size();
287
288 // To incoroporate function-local information visit all function-local
289 // MDNodes and all function-local values they reference.
290 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
291 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000292 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000293 if (O->isFunctionLocal() && O->getFunction())
294 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000295 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000296 EnumerateValue(V);
297 }
298
299 // Also, collect all function-local MDNodes for easy access.
300 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000301}
302
Victor Hernandezd7e64572010-01-14 19:54:11 +0000303void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000304 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000305 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
306 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000307
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000308 // Check to see if it's already in!
309 unsigned &ValueID = ValueMap[V];
310 if (ValueID) {
311 // Increment use count.
312 Values[ValueID-1].second++;
313 return;
314 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000315
Stephen Hinesc6a4f5e2014-07-21 00:45:20 -0700316 if (auto *GO = dyn_cast<GlobalObject>(V))
317 if (const Comdat *C = GO->getComdat())
318 Comdats.insert(C);
319
Chris Lattner7a303d12007-05-06 01:00:28 +0000320 // Enumerate the type of this value.
321 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000322
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000323 if (const Constant *C = dyn_cast<Constant>(V)) {
324 if (isa<GlobalValue>(C)) {
325 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner7a303d12007-05-06 01:00:28 +0000326 } else if (C->getNumOperands()) {
327 // If a constant has operands, enumerate them. This makes sure that if a
328 // constant has uses (for example an array of const ints), that they are
329 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000330
Chris Lattner7a303d12007-05-06 01:00:28 +0000331 // We prefer to enumerate them with values before we enumerate the user
332 // itself. This makes it more likely that we can avoid forward references
333 // in the reader. We know that there can be no cycles in the constants
334 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000335 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
336 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000337 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000338 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000339
Chris Lattner7a303d12007-05-06 01:00:28 +0000340 // Finally, add the value. Doing this could make the ValueID reference be
341 // dangling, don't reuse it.
342 Values.push_back(std::make_pair(V, 1U));
343 ValueMap[V] = Values.size();
344 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000345 }
346 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000347
Chris Lattner7a303d12007-05-06 01:00:28 +0000348 // Add the value.
349 Values.push_back(std::make_pair(V, 1U));
350 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000351}
352
353
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000354void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000355 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000356
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000357 // We've already seen this type.
Chris Lattner1afcace2011-07-09 17:41:24 +0000358 if (*TypeID)
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000359 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000360
Chris Lattner1afcace2011-07-09 17:41:24 +0000361 // If it is a non-anonymous struct, mark the type as being visited so that we
362 // don't recursively visit it. This is safe because we allow forward
363 // references of these in the bitcode reader.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000364 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner3ebb6492011-08-12 18:06:37 +0000365 if (!STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000366 *TypeID = ~0U;
Joe Abbey170a15e2012-11-25 15:23:39 +0000367
Chris Lattner1afcace2011-07-09 17:41:24 +0000368 // Enumerate all of the subtypes before we enumerate this type. This ensures
369 // that the type will be enumerated in an order that can be directly built.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000370 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
371 I != E; ++I)
372 EnumerateType(*I);
Joe Abbey170a15e2012-11-25 15:23:39 +0000373
Chris Lattner1afcace2011-07-09 17:41:24 +0000374 // Refresh the TypeID pointer in case the table rehashed.
375 TypeID = &TypeMap[Ty];
Joe Abbey170a15e2012-11-25 15:23:39 +0000376
Chris Lattner1afcace2011-07-09 17:41:24 +0000377 // Check to see if we got the pointer another way. This can happen when
378 // enumerating recursive types that hit the base case deeper than they start.
379 //
380 // If this is actually a struct that we are treating as forward ref'able,
381 // then emit the definition now that all of its contents are available.
382 if (*TypeID && *TypeID != ~0U)
383 return;
Joe Abbey170a15e2012-11-25 15:23:39 +0000384
Chris Lattner1afcace2011-07-09 17:41:24 +0000385 // Add this type now that its contents are all happily enumerated.
386 Types.push_back(Ty);
Joe Abbey170a15e2012-11-25 15:23:39 +0000387
Chris Lattner1afcace2011-07-09 17:41:24 +0000388 *TypeID = Types.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000389}
390
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000391// Enumerate the types for the specified value. If the value is a constant,
392// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000393void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000394 EnumerateType(V->getType());
Joe Abbey170a15e2012-11-25 15:23:39 +0000395
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000396 if (const Constant *C = dyn_cast<Constant>(V)) {
397 // If this constant is already enumerated, ignore it, we know its type must
398 // be enumerated.
399 if (ValueMap.count(V)) return;
400
401 // This constant may have operands, make sure to enumerate the types in
402 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000403 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
Jay Foad8340d0b2011-04-11 09:48:55 +0000404 const Value *Op = C->getOperand(i);
Joe Abbey170a15e2012-11-25 15:23:39 +0000405
Chris Lattner837e04a2009-10-28 05:24:40 +0000406 // Don't enumerate basic blocks here, this happens as operands to
407 // blockaddress.
408 if (isa<BasicBlock>(Op)) continue;
Joe Abbey170a15e2012-11-25 15:23:39 +0000409
Dan Gohman879d8112010-08-25 17:09:03 +0000410 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000411 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000412
413 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000414 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
415 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000416 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000417 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000418 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000419 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000420}
421
Bill Wendling105ea3d2013-02-12 08:01:22 +0000422void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000423 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling105ea3d2013-02-12 08:01:22 +0000424
Chris Lattner50954f52007-05-03 22:46:43 +0000425 // Do a lookup.
Bill Wendling105ea3d2013-02-12 08:01:22 +0000426 unsigned &Entry = AttributeMap[PAL];
Chris Lattner50954f52007-05-03 22:46:43 +0000427 if (Entry == 0) {
428 // Never saw this before, add it.
Bill Wendling034b94b2012-12-19 07:18:57 +0000429 Attribute.push_back(PAL);
430 Entry = Attribute.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000431 }
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000432
433 // Do lookups for all attribute groups.
434 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
435 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendlinge9229a62013-02-11 22:33:26 +0000436 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000437 if (Entry == 0) {
Bill Wendlinge9229a62013-02-11 22:33:26 +0000438 AttributeGroups.push_back(AS);
439 Entry = AttributeGroups.size();
Bill Wendling8c2e77f2013-02-10 23:06:02 +0000440 }
441 }
Chris Lattner50954f52007-05-03 22:46:43 +0000442}
443
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000444void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000445 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000446 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000447 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000448
Chris Lattner8d35c792007-04-26 03:50:57 +0000449 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000450 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
451 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000452 EnumerateValue(I);
453
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000454 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000455
Chris Lattner8d35c792007-04-26 03:50:57 +0000456 // Add all function-level constants to the value table.
457 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
458 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000459 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000460 OI != E; ++OI) {
461 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
462 isa<InlineAsm>(*OI))
463 EnumerateValue(*OI);
464 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000465 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000466 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000467 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000468
Chris Lattner6da91d32007-05-04 05:21:47 +0000469 // Optimize the constant layout.
470 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000471
Duncan Sandsdc024672007-11-27 13:23:08 +0000472 // Add the function's parameter attributes so they are available for use in
473 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000474 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000475
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000476 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000477
Devang Patel62098692010-06-02 23:05:04 +0000478 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000479 // Add all of the instructions.
480 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000481 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000482 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
483 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000484 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000485 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000486 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000487 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000488 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000489
490 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
491 I->getAllMetadataOtherThanDebugLoc(MDs);
492 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
493 MDNode *N = MDs[i].second;
494 if (N->isFunctionLocal() && N->getFunction())
495 FnLocalMDVector.push_back(N);
496 }
Joe Abbey170a15e2012-11-25 15:23:39 +0000497
Benjamin Kramerf0127052010-01-05 13:12:22 +0000498 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000499 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000500 }
501 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000502
503 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000504 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000505 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000506}
507
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000508void ValueEnumerator::purgeFunction() {
Chris Lattner8d35c792007-04-26 03:50:57 +0000509 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000510 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000511 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000512 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
513 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000514 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
515 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000516
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000517 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000518 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000519 BasicBlocks.clear();
Dan Gohman848c9ae2010-08-25 17:11:16 +0000520 FunctionLocalMDs.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000521}
Chris Lattner837e04a2009-10-28 05:24:40 +0000522
523static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
524 DenseMap<const BasicBlock*, unsigned> &IDMap) {
525 unsigned Counter = 0;
526 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
527 IDMap[BB] = ++Counter;
528}
529
530/// getGlobalBasicBlockID - This returns the function-specific ID for the
531/// specified basic block. This is relatively expensive information, so it
532/// should only be used by rare constructs such as address-of-label.
533unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
534 unsigned &Idx = GlobalBasicBlockIDs[BB];
535 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000536 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000537
538 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
539 return getGlobalBasicBlockID(BB);
540}
541