blob: 6c43f433b887629bc1d94782ade2c43d6892dd41 [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"
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
Bob Wilson54eee522010-06-19 05:33:57 +000063 // Insert constants and metadata that are named at module level into the slot
Devang Patel0386f012010-01-07 19:39:36 +000064 // pool so that the module symbol table can refer to them...
Chris Lattnerfd57cec2007-04-22 06:24:45 +000065 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman17aa92c2010-07-21 23:38:33 +000066 EnumerateNamedMetadata(M);
Daniel Dunbara279bc32009-09-20 02:20:51 +000067
Chris Lattnercc7b0112009-12-31 00:51:46 +000068 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
69
Chris Lattner8d35c792007-04-26 03:50:57 +000070 // Enumerate types used by function bodies and argument lists.
Chris Lattnerfd57cec2007-04-22 06:24:45 +000071 for (Module::const_iterator F = M->begin(), E = M->end(); F != E; ++F) {
Daniel Dunbara279bc32009-09-20 02:20:51 +000072
Chris Lattner8d35c792007-04-26 03:50:57 +000073 for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
74 I != E; ++I)
75 EnumerateType(I->getType());
Devang Patele8e02132009-09-18 19:26:43 +000076
Chris Lattnerfd57cec2007-04-22 06:24:45 +000077 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
78 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E;++I){
Daniel Dunbara279bc32009-09-20 02:20:51 +000079 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Victor Hernandezd7e64572010-01-14 19:54:11 +000080 OI != E; ++OI) {
81 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +000082 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd7e64572010-01-14 19:54:11 +000083 // These will get enumerated during function-incorporation.
84 continue;
85 EnumerateOperandType(*OI);
86 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +000087 EnumerateType(I->getType());
Duncan Sandsdc024672007-11-27 13:23:08 +000088 if (const CallInst *CI = dyn_cast<CallInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000089 EnumerateAttributes(CI->getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +000090 else if (const InvokeInst *II = dyn_cast<InvokeInst>(I))
Devang Patel05988662008-09-25 21:00:45 +000091 EnumerateAttributes(II->getAttributes());
Devang Patele8e02132009-09-18 19:26:43 +000092
Daniel Dunbara279bc32009-09-20 02:20:51 +000093 // Enumerate metadata attached with this instruction.
Devang Patelf61b2372009-10-22 18:55:16 +000094 MDs.clear();
Chris Lattnera6245242010-04-03 02:17:50 +000095 I->getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner3990b122009-12-28 23:41:32 +000096 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandezd7e64572010-01-14 19:54:11 +000097 EnumerateMetadata(MDs[i].second);
Joe Abbey170a15e2012-11-25 15:23:39 +000098
Chris Lattnera6245242010-04-03 02:17:50 +000099 if (!I->getDebugLoc().isUnknown()) {
100 MDNode *Scope, *IA;
101 I->getDebugLoc().getScopeAndInlinedAt(Scope, IA, I->getContext());
102 if (Scope) EnumerateMetadata(Scope);
103 if (IA) EnumerateMetadata(IA);
104 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000105 }
106 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000107
Chris Lattner6da91d32007-05-04 05:21:47 +0000108 // Optimize constant ordering.
109 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000110}
111
Devang Patele8e02132009-09-18 19:26:43 +0000112unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
113 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattner1afcace2011-07-09 17:41:24 +0000114 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman5c18fa22010-08-25 17:09:50 +0000115 return I->second;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000116}
Devang Patele8e02132009-09-18 19:26:43 +0000117
118void ValueEnumerator::setInstructionID(const Instruction *I) {
119 InstructionMap[I] = InstructionCount++;
120}
121
Devang Pateld5ac4042009-08-04 06:00:18 +0000122unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelbc5201f2010-01-22 22:52:10 +0000123 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Pateld5ac4042009-08-04 06:00:18 +0000124 ValueMapType::const_iterator I = MDValueMap.find(V);
125 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
126 return I->second-1;
127 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000128
Devang Pateld5ac4042009-08-04 06:00:18 +0000129 ValueMapType::const_iterator I = ValueMap.find(V);
130 assert(I != ValueMap.end() && "Value not in slotcalculator!");
131 return I->second-1;
132}
Daniel Dunbara279bc32009-09-20 02:20:51 +0000133
Chad Rosier4e6c03f2011-12-07 20:44:46 +0000134void ValueEnumerator::dump() const {
135 print(dbgs(), ValueMap, "Default");
136 dbgs() << '\n';
137 print(dbgs(), MDValueMap, "MetaData");
138 dbgs() << '\n';
139}
140
141void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
142 const char *Name) const {
143
144 OS << "Map Name: " << Name << "\n";
145 OS << "Size: " << Map.size() << "\n";
146 for (ValueMapType::const_iterator I = Map.begin(),
147 E = Map.end(); I != E; ++I) {
148
149 const Value *V = I->first;
150 if (V->hasName())
151 OS << "Value: " << V->getName();
152 else
153 OS << "Value: [null]\n";
154 V->dump();
155
156 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
157 for (Value::const_use_iterator UI = V->use_begin(), UE = V->use_end();
158 UI != UE; ++UI) {
159 if (UI != V->use_begin())
160 OS << ",";
161 if((*UI)->hasName())
162 OS << " " << (*UI)->getName();
163 else
164 OS << " [null]";
165
166 }
167 OS << "\n\n";
168 }
169}
170
Chris Lattner6da91d32007-05-04 05:21:47 +0000171// Optimize constant ordering.
Dan Gohman844731a2008-05-13 00:00:25 +0000172namespace {
173 struct CstSortPredicate {
174 ValueEnumerator &VE;
175 explicit CstSortPredicate(ValueEnumerator &ve) : VE(ve) {}
176 bool operator()(const std::pair<const Value*, unsigned> &LHS,
177 const std::pair<const Value*, unsigned> &RHS) {
178 // Sort by plane.
179 if (LHS.first->getType() != RHS.first->getType())
Daniel Dunbara279bc32009-09-20 02:20:51 +0000180 return VE.getTypeID(LHS.first->getType()) <
Dan Gohman844731a2008-05-13 00:00:25 +0000181 VE.getTypeID(RHS.first->getType());
182 // Then by frequency.
183 return LHS.second > RHS.second;
184 }
185 };
186}
Chris Lattner6da91d32007-05-04 05:21:47 +0000187
188/// OptimizeConstants - Reorder constant pool for denser encoding.
189void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
190 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000191
Chris Lattner6da91d32007-05-04 05:21:47 +0000192 CstSortPredicate P(*this);
193 std::stable_sort(Values.begin()+CstStart, Values.begin()+CstEnd, P);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000194
Duncan Sands2333e292012-11-13 12:59:33 +0000195 // Ensure that integer and vector of integer constants are at the start of the
196 // constant pool. This is important so that GEP structure indices come before
197 // gep constant exprs.
Chris Lattner6da91d32007-05-04 05:21:47 +0000198 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sands2333e292012-11-13 12:59:33 +0000199 isIntOrIntVectorValue);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000200
Chris Lattner6da91d32007-05-04 05:21:47 +0000201 // Rebuild the modified portion of ValueMap.
202 for (; CstStart != CstEnd; ++CstStart)
203 ValueMap[Values[CstStart].first] = CstStart+1;
204}
205
206
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000207/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
208/// table into the values table.
209void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbara279bc32009-09-20 02:20:51 +0000210 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000211 VI != VE; ++VI)
212 EnumerateValue(VI->getValue());
213}
214
Dan Gohman17aa92c2010-07-21 23:38:33 +0000215/// EnumerateNamedMetadata - Insert all of the values referenced by
216/// named metadata in the specified module.
217void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
218 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
219 E = M->named_metadata_end(); I != E; ++I)
220 EnumerateNamedMDNode(I);
Devang Patel0386f012010-01-07 19:39:36 +0000221}
222
Devang Patel8fba5782010-01-09 00:30:14 +0000223void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel8fba5782010-01-09 00:30:14 +0000224 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohman078b0532010-08-24 02:01:24 +0000225 EnumerateMetadata(MD->getOperand(i));
Devang Patel8fba5782010-01-09 00:30:14 +0000226}
227
Dan Gohman309b3af2010-08-24 02:24:03 +0000228/// EnumerateMDNodeOperands - Enumerate all non-function-local values
229/// and types referenced by the given MDNode.
230void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
231 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
232 if (Value *V = N->getOperand(i)) {
233 if (isa<MDNode>(V) || isa<MDString>(V))
234 EnumerateMetadata(V);
235 else if (!isa<Instruction>(V) && !isa<Argument>(V))
236 EnumerateValue(V);
237 } else
238 EnumerateType(Type::getVoidTy(N->getContext()));
239 }
240}
241
Devang Patelbc5201f2010-01-22 22:52:10 +0000242void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramere88a8e62010-01-23 09:54:23 +0000243 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohman309b3af2010-08-24 02:24:03 +0000244
245 // Enumerate the type of this value.
246 EnumerateType(MD->getType());
247
248 const MDNode *N = dyn_cast<MDNode>(MD);
249
250 // In the module-level pass, skip function-local nodes themselves, but
251 // do walk their operands.
252 if (N && N->isFunctionLocal() && N->getFunction()) {
253 EnumerateMDNodeOperands(N);
254 return;
255 }
256
Devang Pateld5ac4042009-08-04 06:00:18 +0000257 // Check to see if it's already in!
258 unsigned &MDValueID = MDValueMap[MD];
259 if (MDValueID) {
260 // Increment use count.
261 MDValues[MDValueID-1].second++;
262 return;
263 }
Devang Pateld5ac4042009-08-04 06:00:18 +0000264 MDValues.push_back(std::make_pair(MD, 1U));
265 MDValueID = MDValues.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000266
267 // Enumerate all non-function-local operands.
268 if (N)
269 EnumerateMDNodeOperands(N);
270}
271
272/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
273/// information reachable from the given MDNode.
274void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
275 assert(N->isFunctionLocal() && N->getFunction() &&
276 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
277
278 // Enumerate the type of this value.
279 EnumerateType(N->getType());
280
281 // Check to see if it's already in!
282 unsigned &MDValueID = MDValueMap[N];
283 if (MDValueID) {
284 // Increment use count.
285 MDValues[MDValueID-1].second++;
286 return;
287 }
288 MDValues.push_back(std::make_pair(N, 1U));
289 MDValueID = MDValues.size();
290
291 // To incoroporate function-local information visit all function-local
292 // MDNodes and all function-local values they reference.
293 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
294 if (Value *V = N->getOperand(i)) {
Dan Gohmand01347e2010-08-24 02:40:27 +0000295 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohman309b3af2010-08-24 02:24:03 +0000296 if (O->isFunctionLocal() && O->getFunction())
297 EnumerateFunctionLocalMetadata(O);
Dan Gohmand01347e2010-08-24 02:40:27 +0000298 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohman309b3af2010-08-24 02:24:03 +0000299 EnumerateValue(V);
300 }
301
302 // Also, collect all function-local MDNodes for easy access.
303 FunctionLocalMDs.push_back(N);
Devang Pateld5ac4042009-08-04 06:00:18 +0000304}
305
Victor Hernandezd7e64572010-01-14 19:54:11 +0000306void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattnercc7b0112009-12-31 00:51:46 +0000307 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohman309b3af2010-08-24 02:24:03 +0000308 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
309 "EnumerateValue doesn't handle Metadata!");
Devang Pateld5ac4042009-08-04 06:00:18 +0000310
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000311 // Check to see if it's already in!
312 unsigned &ValueID = ValueMap[V];
313 if (ValueID) {
314 // Increment use count.
315 Values[ValueID-1].second++;
316 return;
317 }
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000318
Chris Lattner7a303d12007-05-06 01:00:28 +0000319 // Enumerate the type of this value.
320 EnumerateType(V->getType());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000321
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000322 if (const Constant *C = dyn_cast<Constant>(V)) {
323 if (isa<GlobalValue>(C)) {
324 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner7a303d12007-05-06 01:00:28 +0000325 } else if (C->getNumOperands()) {
326 // If a constant has operands, enumerate them. This makes sure that if a
327 // constant has uses (for example an array of const ints), that they are
328 // inserted also.
Daniel Dunbara279bc32009-09-20 02:20:51 +0000329
Chris Lattner7a303d12007-05-06 01:00:28 +0000330 // We prefer to enumerate them with values before we enumerate the user
331 // itself. This makes it more likely that we can avoid forward references
332 // in the reader. We know that there can be no cycles in the constants
333 // graph that don't go through a global variable.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000334 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
335 I != E; ++I)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000336 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000337 EnumerateValue(*I);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000338
Chris Lattner7a303d12007-05-06 01:00:28 +0000339 // Finally, add the value. Doing this could make the ValueID reference be
340 // dangling, don't reuse it.
341 Values.push_back(std::make_pair(V, 1U));
342 ValueMap[V] = Values.size();
343 return;
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000344 }
345 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000346
Chris Lattner7a303d12007-05-06 01:00:28 +0000347 // Add the value.
348 Values.push_back(std::make_pair(V, 1U));
349 ValueID = Values.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000350}
351
352
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000353void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattner1afcace2011-07-09 17:41:24 +0000354 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbara279bc32009-09-20 02:20:51 +0000355
Rafael Espindolaf5a90562011-04-06 16:49:37 +0000356 // We've already seen this type.
Chris Lattner1afcace2011-07-09 17:41:24 +0000357 if (*TypeID)
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000358 return;
Daniel Dunbara279bc32009-09-20 02:20:51 +0000359
Chris Lattner1afcace2011-07-09 17:41:24 +0000360 // If it is a non-anonymous struct, mark the type as being visited so that we
361 // don't recursively visit it. This is safe because we allow forward
362 // references of these in the bitcode reader.
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000363 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner3ebb6492011-08-12 18:06:37 +0000364 if (!STy->isLiteral())
Chris Lattner1afcace2011-07-09 17:41:24 +0000365 *TypeID = ~0U;
Joe Abbey170a15e2012-11-25 15:23:39 +0000366
Chris Lattner1afcace2011-07-09 17:41:24 +0000367 // Enumerate all of the subtypes before we enumerate this type. This ensures
368 // that the type will be enumerated in an order that can be directly built.
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000369 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
370 I != E; ++I)
371 EnumerateType(*I);
Joe Abbey170a15e2012-11-25 15:23:39 +0000372
Chris Lattner1afcace2011-07-09 17:41:24 +0000373 // Refresh the TypeID pointer in case the table rehashed.
374 TypeID = &TypeMap[Ty];
Joe Abbey170a15e2012-11-25 15:23:39 +0000375
Chris Lattner1afcace2011-07-09 17:41:24 +0000376 // Check to see if we got the pointer another way. This can happen when
377 // enumerating recursive types that hit the base case deeper than they start.
378 //
379 // If this is actually a struct that we are treating as forward ref'able,
380 // then emit the definition now that all of its contents are available.
381 if (*TypeID && *TypeID != ~0U)
382 return;
Joe Abbey170a15e2012-11-25 15:23:39 +0000383
Chris Lattner1afcace2011-07-09 17:41:24 +0000384 // Add this type now that its contents are all happily enumerated.
385 Types.push_back(Ty);
Joe Abbey170a15e2012-11-25 15:23:39 +0000386
Chris Lattner1afcace2011-07-09 17:41:24 +0000387 *TypeID = Types.size();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000388}
389
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000390// Enumerate the types for the specified value. If the value is a constant,
391// walk through it, enumerating the types of the constant.
Victor Hernandezd7e64572010-01-14 19:54:11 +0000392void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000393 EnumerateType(V->getType());
Joe Abbey170a15e2012-11-25 15:23:39 +0000394
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000395 if (const Constant *C = dyn_cast<Constant>(V)) {
396 // If this constant is already enumerated, ignore it, we know its type must
397 // be enumerated.
398 if (ValueMap.count(V)) return;
399
400 // This constant may have operands, make sure to enumerate the types in
401 // them.
Chris Lattner837e04a2009-10-28 05:24:40 +0000402 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
Jay Foad8340d0b2011-04-11 09:48:55 +0000403 const Value *Op = C->getOperand(i);
Joe Abbey170a15e2012-11-25 15:23:39 +0000404
Chris Lattner837e04a2009-10-28 05:24:40 +0000405 // Don't enumerate basic blocks here, this happens as operands to
406 // blockaddress.
407 if (isa<BasicBlock>(Op)) continue;
Joe Abbey170a15e2012-11-25 15:23:39 +0000408
Dan Gohman879d8112010-08-25 17:09:03 +0000409 EnumerateOperandType(Op);
Chris Lattner837e04a2009-10-28 05:24:40 +0000410 }
Nick Lewyckycb337992009-05-10 20:57:05 +0000411
412 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner5d0cacd2009-12-31 01:22:29 +0000413 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
414 if (Value *Elem = N->getOperand(i))
Victor Hernandezd7e64572010-01-14 19:54:11 +0000415 EnumerateOperandType(Elem);
Nick Lewyckycb337992009-05-10 20:57:05 +0000416 }
Devang Patel104cf9e2009-07-23 01:07:34 +0000417 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohman78aeae22010-08-24 02:10:52 +0000418 EnumerateMetadata(V);
Chris Lattner33f1d5b2007-05-06 08:35:19 +0000419}
420
Devang Patel05988662008-09-25 21:00:45 +0000421void ValueEnumerator::EnumerateAttributes(const AttrListPtr &PAL) {
Chris Lattner58d74912008-03-12 17:45:29 +0000422 if (PAL.isEmpty()) return; // null is always 0.
Chris Lattner50954f52007-05-03 22:46:43 +0000423 // Do a lookup.
Devang Patel05988662008-09-25 21:00:45 +0000424 unsigned &Entry = AttributeMap[PAL.getRawPointer()];
Chris Lattner50954f52007-05-03 22:46:43 +0000425 if (Entry == 0) {
426 // Never saw this before, add it.
Devang Patel05988662008-09-25 21:00:45 +0000427 Attributes.push_back(PAL);
428 Entry = Attributes.size();
Chris Lattner50954f52007-05-03 22:46:43 +0000429 }
430}
431
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000432void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewycky9a49f152010-02-25 08:30:17 +0000433 InstructionCount = 0;
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000434 NumModuleValues = Values.size();
Dan Gohman309b3af2010-08-24 02:24:03 +0000435 NumModuleMDValues = MDValues.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000436
Chris Lattner8d35c792007-04-26 03:50:57 +0000437 // Adding function arguments to the value table.
Dan Gohman6dd26ba2010-07-16 22:58:39 +0000438 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
439 I != E; ++I)
Chris Lattner8d35c792007-04-26 03:50:57 +0000440 EnumerateValue(I);
441
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000442 FirstFuncConstantID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000443
Chris Lattner8d35c792007-04-26 03:50:57 +0000444 // Add all function-level constants to the value table.
445 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
446 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbara279bc32009-09-20 02:20:51 +0000447 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner8d35c792007-04-26 03:50:57 +0000448 OI != E; ++OI) {
449 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
450 isa<InlineAsm>(*OI))
451 EnumerateValue(*OI);
452 }
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000453 BasicBlocks.push_back(BB);
Chris Lattnere825ed52007-05-03 22:18:21 +0000454 ValueMap[BB] = BasicBlocks.size();
Chris Lattner8d35c792007-04-26 03:50:57 +0000455 }
Daniel Dunbara279bc32009-09-20 02:20:51 +0000456
Chris Lattner6da91d32007-05-04 05:21:47 +0000457 // Optimize the constant layout.
458 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbara279bc32009-09-20 02:20:51 +0000459
Duncan Sandsdc024672007-11-27 13:23:08 +0000460 // Add the function's parameter attributes so they are available for use in
461 // the function's instruction.
Devang Patel05988662008-09-25 21:00:45 +0000462 EnumerateAttributes(F.getAttributes());
Duncan Sandsdc024672007-11-27 13:23:08 +0000463
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000464 FirstInstID = Values.size();
Daniel Dunbara279bc32009-09-20 02:20:51 +0000465
Devang Patel62098692010-06-02 23:05:04 +0000466 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner8d35c792007-04-26 03:50:57 +0000467 // Add all of the instructions.
468 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000469 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezab9cd102010-01-13 19:36:16 +0000470 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
471 OI != E; ++OI) {
Victor Hernandezd7e64572010-01-14 19:54:11 +0000472 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez2b3365c2010-02-06 01:21:09 +0000473 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000474 // Enumerate metadata after the instructions they might refer to.
Devang Patel62098692010-06-02 23:05:04 +0000475 FnLocalMDVector.push_back(MD);
Victor Hernandezab9cd102010-01-13 19:36:16 +0000476 }
Dan Gohman309b3af2010-08-24 02:24:03 +0000477
478 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
479 I->getAllMetadataOtherThanDebugLoc(MDs);
480 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
481 MDNode *N = MDs[i].second;
482 if (N->isFunctionLocal() && N->getFunction())
483 FnLocalMDVector.push_back(N);
484 }
Joe Abbey170a15e2012-11-25 15:23:39 +0000485
Benjamin Kramerf0127052010-01-05 13:12:22 +0000486 if (!I->getType()->isVoidTy())
Chris Lattner8d35c792007-04-26 03:50:57 +0000487 EnumerateValue(I);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000488 }
489 }
Victor Hernandezaf6ce142010-02-04 01:13:08 +0000490
491 // Add all of the function-local metadata.
Devang Patel62098692010-06-02 23:05:04 +0000492 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohman309b3af2010-08-24 02:24:03 +0000493 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000494}
495
Chad Rosier6a0c04d2011-06-03 17:02:19 +0000496void ValueEnumerator::purgeFunction() {
Chris Lattner8d35c792007-04-26 03:50:57 +0000497 /// Remove purged values from the ValueMap.
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000498 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner8d35c792007-04-26 03:50:57 +0000499 ValueMap.erase(Values[i].first);
Dan Gohman309b3af2010-08-24 02:24:03 +0000500 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
501 MDValueMap.erase(MDValues[i].first);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000502 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
503 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbara279bc32009-09-20 02:20:51 +0000504
Chris Lattnerb9d0c2a2007-04-26 05:53:54 +0000505 Values.resize(NumModuleValues);
Dan Gohman309b3af2010-08-24 02:24:03 +0000506 MDValues.resize(NumModuleMDValues);
Chris Lattnerc59c0af2007-04-26 04:42:16 +0000507 BasicBlocks.clear();
Dan Gohman848c9ae2010-08-25 17:11:16 +0000508 FunctionLocalMDs.clear();
Chris Lattnerfd57cec2007-04-22 06:24:45 +0000509}
Chris Lattner837e04a2009-10-28 05:24:40 +0000510
511static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
512 DenseMap<const BasicBlock*, unsigned> &IDMap) {
513 unsigned Counter = 0;
514 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
515 IDMap[BB] = ++Counter;
516}
517
518/// getGlobalBasicBlockID - This returns the function-specific ID for the
519/// specified basic block. This is relatively expensive information, so it
520/// should only be used by rare constructs such as address-of-label.
521unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
522 unsigned &Idx = GlobalBasicBlockIDs[BB];
523 if (Idx != 0)
Chris Lattnercdfc9402009-11-01 01:27:45 +0000524 return Idx-1;
Chris Lattner837e04a2009-10-28 05:24:40 +0000525
526 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
527 return getGlobalBasicBlockID(BB);
528}
529