blob: c05d6ae0d2171936ad78f6b0773c8b64617ce430 [file] [log] [blame]
Chris Lattnerc1d10d62007-04-22 06:24:45 +00001//===-- ValueEnumerator.cpp - Number values and types for bitcode writer --===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-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 Lattnerc1d10d62007-04-22 06:24:45 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the ValueEnumerator class.
11//
12//===----------------------------------------------------------------------===//
13
14#include "ValueEnumerator.h"
Rafael Espindola337a1b22011-04-06 16:49:37 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/ADT/SmallPtrSet.h"
Chandler Carruth9fb823b2013-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"
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +000021#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000022#include "llvm/IR/ValueSymbolTable.h"
Chad Rosier78037a92011-12-07 20:44:46 +000023#include "llvm/Support/Debug.h"
24#include "llvm/Support/raw_ostream.h"
Chris Lattnera8713be2007-05-04 05:05:48 +000025#include <algorithm>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000026using namespace llvm;
27
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000028namespace {
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000029struct OrderMap {
30 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000031 unsigned LastGlobalConstantID;
32 unsigned LastGlobalValueID;
33
34 OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
35
36 bool isGlobalConstant(unsigned ID) const {
37 return ID <= LastGlobalConstantID;
38 }
39 bool isGlobalValue(unsigned ID) const {
40 return ID <= LastGlobalValueID && !isGlobalConstant(ID);
41 }
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000042
43 unsigned size() const { return IDs.size(); }
44 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
45 std::pair<unsigned, bool> lookup(const Value *V) const {
46 return IDs.lookup(V);
47 }
Duncan P. N. Exon Smithba4576d2014-07-30 01:20:26 +000048 void index(const Value *V) {
49 // Explicitly sequence get-size and insert-value operations to avoid UB.
50 unsigned ID = IDs.size() + 1;
51 IDs[V].first = ID;
52 }
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000053};
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000054}
55
56static void orderValue(const Value *V, OrderMap &OM) {
57 if (OM.lookup(V).first)
58 return;
59
60 if (const Constant *C = dyn_cast<Constant>(V))
61 if (C->getNumOperands() && !isa<GlobalValue>(C))
62 for (const Value *Op : C->operands())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000063 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000064 orderValue(Op, OM);
65
66 // Note: we cannot cache this lookup above, since inserting into the map
Duncan P. N. Exon Smithba4576d2014-07-30 01:20:26 +000067 // changes the map's size, and thus affects the other IDs.
68 OM.index(V);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000069}
70
71static OrderMap orderModule(const Module *M) {
72 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
73 // and ValueEnumerator::incorporateFunction().
74 OrderMap OM;
75
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000076 // In the reader, initializers of GlobalValues are set *after* all the
77 // globals have been read. Rather than awkwardly modeling this behaviour
78 // directly in predictValueUseListOrderImpl(), just assign IDs to
79 // initializers of GlobalValues before GlobalValues themselves to model this
80 // implicitly.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000081 for (const GlobalVariable &G : M->globals())
82 if (G.hasInitializer())
83 orderValue(G.getInitializer(), OM);
84 for (const GlobalAlias &A : M->aliases())
85 orderValue(A.getAliasee(), OM);
86 for (const Function &F : *M)
87 if (F.hasPrefixData())
88 orderValue(F.getPrefixData(), OM);
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000089 OM.LastGlobalConstantID = OM.size();
90
91 // Initializers of GlobalValues are processed in
92 // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
93 // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
94 // by giving IDs in reverse order.
95 //
96 // Since GlobalValues never reference each other directly (just through
97 // initializers), their relative IDs only matter for determining order of
98 // uses in their initializers.
99 for (const Function &F : *M)
100 orderValue(&F, OM);
101 for (const GlobalAlias &A : M->aliases())
102 orderValue(&A, OM);
103 for (const GlobalVariable &G : M->globals())
104 orderValue(&G, OM);
105 OM.LastGlobalValueID = OM.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000106
107 for (const Function &F : *M) {
108 if (F.isDeclaration())
109 continue;
110 // Here we need to match the union of ValueEnumerator::incorporateFunction()
111 // and WriteFunction(). Basic blocks are implicitly declared before
112 // anything else (by declaring their size).
113 for (const BasicBlock &BB : F)
114 orderValue(&BB, OM);
115 for (const Argument &A : F.args())
116 orderValue(&A, OM);
117 for (const BasicBlock &BB : F)
118 for (const Instruction &I : BB)
119 for (const Value *Op : I.operands())
120 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
121 isa<InlineAsm>(*Op))
122 orderValue(Op, OM);
123 for (const BasicBlock &BB : F)
124 for (const Instruction &I : BB)
125 orderValue(&I, OM);
126 }
127 return OM;
128}
129
130static void predictValueUseListOrderImpl(const Value *V, const Function *F,
131 unsigned ID, const OrderMap &OM,
132 UseListOrderStack &Stack) {
133 // Predict use-list order for this one.
134 typedef std::pair<const Use *, unsigned> Entry;
135 SmallVector<Entry, 64> List;
136 for (const Use &U : V->uses())
137 // Check if this user will be serialized.
138 if (OM.lookup(U.getUser()).first)
139 List.push_back(std::make_pair(&U, List.size()));
140
141 if (List.size() < 2)
142 // We may have lost some users.
143 return;
144
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000145 bool IsGlobalValue = OM.isGlobalValue(ID);
146 std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000147 const Use *LU = L.first;
148 const Use *RU = R.first;
Duncan P. N. Exon Smith3f0fc7b2014-07-29 01:13:56 +0000149 if (LU == RU)
150 return false;
151
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000152 auto LID = OM.lookup(LU->getUser()).first;
153 auto RID = OM.lookup(RU->getUser()).first;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000154
155 // Global values are processed in reverse order.
156 //
157 // Moreover, initializers of GlobalValues are set *after* all the globals
158 // have been read (despite having earlier IDs). Rather than awkwardly
159 // modeling this behaviour here, orderModule() has assigned IDs to
160 // initializers of GlobalValues before GlobalValues themselves.
161 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
162 return LID < RID;
163
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000164 // If ID is 4, then expect: 7 6 5 1 2 3.
165 if (LID < RID) {
166 if (RID < ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000167 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
168 return true;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000169 return false;
170 }
171 if (RID < LID) {
172 if (LID < ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000173 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
174 return false;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000175 return true;
176 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000177
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000178 // LID and RID are equal, so we have different operands of the same user.
179 // Assume operands are added in order for all instructions.
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000180 if (LID < ID)
181 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
182 return LU->getOperandNo() < RU->getOperandNo();
183 return LU->getOperandNo() > RU->getOperandNo();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000184 });
185
186 if (std::is_sorted(
187 List.begin(), List.end(),
188 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
189 // Order is already correct.
190 return;
191
192 // Store the shuffle.
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000193 Stack.emplace_back(V, F, List.size());
194 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
Duncan P. N. Exon Smithf849ace2014-07-28 22:41:50 +0000195 for (size_t I = 0, E = List.size(); I != E; ++I)
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000196 Stack.back().Shuffle[I] = List[I].second;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000197}
198
199static void predictValueUseListOrder(const Value *V, const Function *F,
200 OrderMap &OM, UseListOrderStack &Stack) {
201 auto &IDPair = OM[V];
202 assert(IDPair.first && "Unmapped value");
203 if (IDPair.second)
204 // Already predicted.
205 return;
206
207 // Do the actual prediction.
208 IDPair.second = true;
209 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
210 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
211
212 // Recursive descent into constants.
213 if (const Constant *C = dyn_cast<Constant>(V))
214 if (C->getNumOperands() && !isa<GlobalValue>(C))
215 for (const Value *Op : C->operands())
216 if (isa<Constant>(Op) && !isa<GlobalValue>(Op))
217 predictValueUseListOrder(Op, F, OM, Stack);
218}
219
220static UseListOrderStack predictUseListOrder(const Module *M) {
221 OrderMap OM = orderModule(M);
222
223 // Use-list orders need to be serialized after all the users have been added
224 // to a value, or else the shuffles will be incomplete. Store them per
225 // function in a stack.
226 //
227 // Aside from function order, the order of values doesn't matter much here.
228 UseListOrderStack Stack;
229
230 // We want to visit the functions backward now so we can list function-local
231 // constants in the last Function they're used in. Module-level constants
232 // have already been visited above.
233 for (auto I = M->rbegin(), E = M->rend(); I != E; ++I) {
234 const Function &F = *I;
235 if (F.isDeclaration())
236 continue;
237 for (const BasicBlock &BB : F)
238 predictValueUseListOrder(&BB, &F, OM, Stack);
239 for (const Argument &A : F.args())
240 predictValueUseListOrder(&A, &F, OM, Stack);
241 for (const BasicBlock &BB : F)
242 for (const Instruction &I : BB)
243 for (const Value *Op : I.operands())
244 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
245 isa<InlineAsm>(*Op))
246 predictValueUseListOrder(Op, &F, OM, Stack);
247 for (const BasicBlock &BB : F)
248 for (const Instruction &I : BB)
249 predictValueUseListOrder(&I, &F, OM, Stack);
250 }
251
252 // Visit globals last, since the module-level use-list block will be seen
253 // before the function bodies are processed.
254 for (const GlobalVariable &G : M->globals())
255 predictValueUseListOrder(&G, nullptr, OM, Stack);
256 for (const Function &F : *M)
257 predictValueUseListOrder(&F, nullptr, OM, Stack);
258 for (const GlobalAlias &A : M->aliases())
259 predictValueUseListOrder(&A, nullptr, OM, Stack);
260 for (const GlobalVariable &G : M->globals())
261 if (G.hasInitializer())
262 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
263 for (const GlobalAlias &A : M->aliases())
264 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
265 for (const Function &F : *M)
266 if (F.hasPrefixData())
267 predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
268
269 return Stack;
270}
271
Duncan Sandse6beec62012-11-13 12:59:33 +0000272static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
273 return V.first->getType()->isIntOrIntVectorTy();
Chris Lattner430e80d2007-05-04 05:21:47 +0000274}
275
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000276/// ValueEnumerator - Enumerate module-level information.
277ValueEnumerator::ValueEnumerator(const Module *M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000278 if (shouldPreserveBitcodeUseListOrder())
279 UseListOrders = predictUseListOrder(M);
280
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000281 // Enumerate the global variables.
282 for (Module::const_global_iterator I = M->global_begin(),
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000283
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000284 E = M->global_end(); I != E; ++I)
285 EnumerateValue(I);
286
287 // Enumerate the functions.
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000288 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000289 EnumerateValue(I);
Devang Patel4c758ea2008-09-25 21:00:45 +0000290 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000291 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000292
Chris Lattner44c17072007-04-26 02:46:40 +0000293 // Enumerate the aliases.
294 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
295 I != E; ++I)
296 EnumerateValue(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000297
Chris Lattner430e80d2007-05-04 05:21:47 +0000298 // Remember what is the cutoff between globalvalue's and other constants.
299 unsigned FirstConstant = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000300
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000301 // Enumerate the global variable initializers.
302 for (Module::const_global_iterator I = M->global_begin(),
303 E = M->global_end(); I != E; ++I)
304 if (I->hasInitializer())
305 EnumerateValue(I->getInitializer());
306
Chris Lattner44c17072007-04-26 02:46:40 +0000307 // Enumerate the aliasees.
308 for (Module::const_alias_iterator I = M->alias_begin(), E = M->alias_end();
309 I != E; ++I)
310 EnumerateValue(I->getAliasee());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000311
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000312 // Enumerate the prefix data constants.
313 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
314 if (I->hasPrefixData())
315 EnumerateValue(I->getPrefixData());
316
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000317 // Insert constants and metadata that are named at module level into the slot
Devang Patelfcfee0f2010-01-07 19:39:36 +0000318 // pool so that the module symbol table can refer to them...
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000319 EnumerateValueSymbolTable(M->getValueSymbolTable());
Dan Gohman2637cc12010-07-21 23:38:33 +0000320 EnumerateNamedMetadata(M);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000321
Chris Lattner8dace892009-12-31 00:51:46 +0000322 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
323
Chris Lattner5f640b92007-04-26 03:50:57 +0000324 // Enumerate types used by function bodies and argument lists.
Rafael Espindola087d6272014-06-17 03:00:40 +0000325 for (const Function &F : *M) {
326 for (const Argument &A : F.args())
327 EnumerateType(A.getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000328
Rafael Espindola087d6272014-06-17 03:00:40 +0000329 for (const BasicBlock &BB : F)
330 for (const Instruction &I : BB) {
331 for (const Use &Op : I.operands()) {
332 if (MDNode *MD = dyn_cast<MDNode>(&Op))
Victor Hernandez1b081382010-02-06 01:21:09 +0000333 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandez572218b2010-01-14 19:54:11 +0000334 // These will get enumerated during function-incorporation.
335 continue;
Rafael Espindola087d6272014-06-17 03:00:40 +0000336 EnumerateOperandType(Op);
Victor Hernandez572218b2010-01-14 19:54:11 +0000337 }
Rafael Espindola087d6272014-06-17 03:00:40 +0000338 EnumerateType(I.getType());
339 if (const CallInst *CI = dyn_cast<CallInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000340 EnumerateAttributes(CI->getAttributes());
Rafael Espindola087d6272014-06-17 03:00:40 +0000341 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000342 EnumerateAttributes(II->getAttributes());
Devang Patelaf206b82009-09-18 19:26:43 +0000343
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000344 // Enumerate metadata attached with this instruction.
Devang Patel6da5dbf2009-10-22 18:55:16 +0000345 MDs.clear();
Rafael Espindola087d6272014-06-17 03:00:40 +0000346 I.getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000347 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Victor Hernandez572218b2010-01-14 19:54:11 +0000348 EnumerateMetadata(MDs[i].second);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000349
Rafael Espindola087d6272014-06-17 03:00:40 +0000350 if (!I.getDebugLoc().isUnknown()) {
Chris Lattner07d09ed2010-04-03 02:17:50 +0000351 MDNode *Scope, *IA;
Rafael Espindola087d6272014-06-17 03:00:40 +0000352 I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
Chris Lattner07d09ed2010-04-03 02:17:50 +0000353 if (Scope) EnumerateMetadata(Scope);
354 if (IA) EnumerateMetadata(IA);
355 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000356 }
357 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000358
Chris Lattner430e80d2007-05-04 05:21:47 +0000359 // Optimize constant ordering.
360 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindola337a1b22011-04-06 16:49:37 +0000361}
362
Devang Patelaf206b82009-09-18 19:26:43 +0000363unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
364 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000365 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman1f4b0282010-08-25 17:09:50 +0000366 return I->second;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000367}
Devang Patelaf206b82009-09-18 19:26:43 +0000368
David Majnemerdad0a642014-06-27 18:19:56 +0000369unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
370 unsigned ComdatID = Comdats.idFor(C);
371 assert(ComdatID && "Comdat not found!");
372 return ComdatID;
373}
374
Devang Patelaf206b82009-09-18 19:26:43 +0000375void ValueEnumerator::setInstructionID(const Instruction *I) {
376 InstructionMap[I] = InstructionCount++;
377}
378
Devang Patel05eb6172009-08-04 06:00:18 +0000379unsigned ValueEnumerator::getValueID(const Value *V) const {
Devang Patelac277eb2010-01-22 22:52:10 +0000380 if (isa<MDNode>(V) || isa<MDString>(V)) {
Devang Patel05eb6172009-08-04 06:00:18 +0000381 ValueMapType::const_iterator I = MDValueMap.find(V);
382 assert(I != MDValueMap.end() && "Value not in slotcalculator!");
383 return I->second-1;
384 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000385
Devang Patel05eb6172009-08-04 06:00:18 +0000386 ValueMapType::const_iterator I = ValueMap.find(V);
387 assert(I != ValueMap.end() && "Value not in slotcalculator!");
388 return I->second-1;
389}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000390
Chad Rosier78037a92011-12-07 20:44:46 +0000391void ValueEnumerator::dump() const {
392 print(dbgs(), ValueMap, "Default");
393 dbgs() << '\n';
394 print(dbgs(), MDValueMap, "MetaData");
395 dbgs() << '\n';
396}
397
398void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
399 const char *Name) const {
400
401 OS << "Map Name: " << Name << "\n";
402 OS << "Size: " << Map.size() << "\n";
403 for (ValueMapType::const_iterator I = Map.begin(),
404 E = Map.end(); I != E; ++I) {
405
406 const Value *V = I->first;
407 if (V->hasName())
408 OS << "Value: " << V->getName();
409 else
410 OS << "Value: [null]\n";
411 V->dump();
412
413 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000414 for (const Use &U : V->uses()) {
415 if (&U != &*V->use_begin())
Chad Rosier78037a92011-12-07 20:44:46 +0000416 OS << ",";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000417 if(U->hasName())
418 OS << " " << U->getName();
Chad Rosier78037a92011-12-07 20:44:46 +0000419 else
420 OS << " [null]";
421
422 }
423 OS << "\n\n";
424 }
425}
426
Chris Lattner430e80d2007-05-04 05:21:47 +0000427/// OptimizeConstants - Reorder constant pool for denser encoding.
428void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
429 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000430
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +0000431 if (shouldPreserveBitcodeUseListOrder())
432 // Optimizing constants makes the use-list order difficult to predict.
433 // Disable it for now when trying to preserve the order.
434 return;
435
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000436 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
437 [this](const std::pair<const Value *, unsigned> &LHS,
438 const std::pair<const Value *, unsigned> &RHS) {
439 // Sort by plane.
440 if (LHS.first->getType() != RHS.first->getType())
441 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
442 // Then by frequency.
443 return LHS.second > RHS.second;
444 });
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000445
Duncan Sandse6beec62012-11-13 12:59:33 +0000446 // Ensure that integer and vector of integer constants are at the start of the
447 // constant pool. This is important so that GEP structure indices come before
448 // gep constant exprs.
Chris Lattner430e80d2007-05-04 05:21:47 +0000449 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sandse6beec62012-11-13 12:59:33 +0000450 isIntOrIntVectorValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000451
Chris Lattner430e80d2007-05-04 05:21:47 +0000452 // Rebuild the modified portion of ValueMap.
453 for (; CstStart != CstEnd; ++CstStart)
454 ValueMap[Values[CstStart].first] = CstStart+1;
455}
456
457
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000458/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
459/// table into the values table.
460void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000461 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000462 VI != VE; ++VI)
463 EnumerateValue(VI->getValue());
464}
465
Dan Gohman2637cc12010-07-21 23:38:33 +0000466/// EnumerateNamedMetadata - Insert all of the values referenced by
467/// named metadata in the specified module.
468void ValueEnumerator::EnumerateNamedMetadata(const Module *M) {
469 for (Module::const_named_metadata_iterator I = M->named_metadata_begin(),
470 E = M->named_metadata_end(); I != E; ++I)
471 EnumerateNamedMDNode(I);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000472}
473
Devang Patel99ff5a82010-01-09 00:30:14 +0000474void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel99ff5a82010-01-09 00:30:14 +0000475 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohmand3d2bbe2010-08-24 02:01:24 +0000476 EnumerateMetadata(MD->getOperand(i));
Devang Patel99ff5a82010-01-09 00:30:14 +0000477}
478
Dan Gohmanc828c542010-08-24 02:24:03 +0000479/// EnumerateMDNodeOperands - Enumerate all non-function-local values
480/// and types referenced by the given MDNode.
481void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
482 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
483 if (Value *V = N->getOperand(i)) {
484 if (isa<MDNode>(V) || isa<MDString>(V))
485 EnumerateMetadata(V);
486 else if (!isa<Instruction>(V) && !isa<Argument>(V))
487 EnumerateValue(V);
488 } else
489 EnumerateType(Type::getVoidTy(N->getContext()));
490 }
491}
492
Devang Patelac277eb2010-01-22 22:52:10 +0000493void ValueEnumerator::EnumerateMetadata(const Value *MD) {
Benjamin Kramer14bb1142010-01-23 09:54:23 +0000494 assert((isa<MDNode>(MD) || isa<MDString>(MD)) && "Invalid metadata kind");
Dan Gohmanc828c542010-08-24 02:24:03 +0000495
496 // Enumerate the type of this value.
497 EnumerateType(MD->getType());
498
499 const MDNode *N = dyn_cast<MDNode>(MD);
500
501 // In the module-level pass, skip function-local nodes themselves, but
502 // do walk their operands.
503 if (N && N->isFunctionLocal() && N->getFunction()) {
504 EnumerateMDNodeOperands(N);
505 return;
506 }
507
Devang Patel05eb6172009-08-04 06:00:18 +0000508 // Check to see if it's already in!
509 unsigned &MDValueID = MDValueMap[MD];
510 if (MDValueID) {
511 // Increment use count.
512 MDValues[MDValueID-1].second++;
513 return;
514 }
Devang Patel05eb6172009-08-04 06:00:18 +0000515 MDValues.push_back(std::make_pair(MD, 1U));
516 MDValueID = MDValues.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000517
518 // Enumerate all non-function-local operands.
519 if (N)
520 EnumerateMDNodeOperands(N);
521}
522
523/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
524/// information reachable from the given MDNode.
525void ValueEnumerator::EnumerateFunctionLocalMetadata(const MDNode *N) {
526 assert(N->isFunctionLocal() && N->getFunction() &&
527 "EnumerateFunctionLocalMetadata called on non-function-local mdnode!");
528
529 // Enumerate the type of this value.
530 EnumerateType(N->getType());
531
532 // Check to see if it's already in!
533 unsigned &MDValueID = MDValueMap[N];
534 if (MDValueID) {
535 // Increment use count.
536 MDValues[MDValueID-1].second++;
537 return;
538 }
539 MDValues.push_back(std::make_pair(N, 1U));
540 MDValueID = MDValues.size();
541
542 // To incoroporate function-local information visit all function-local
543 // MDNodes and all function-local values they reference.
544 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
545 if (Value *V = N->getOperand(i)) {
Dan Gohman10215a12010-08-24 02:40:27 +0000546 if (MDNode *O = dyn_cast<MDNode>(V)) {
Dan Gohmanc828c542010-08-24 02:24:03 +0000547 if (O->isFunctionLocal() && O->getFunction())
548 EnumerateFunctionLocalMetadata(O);
Dan Gohman10215a12010-08-24 02:40:27 +0000549 } else if (isa<Instruction>(V) || isa<Argument>(V))
Dan Gohmanc828c542010-08-24 02:24:03 +0000550 EnumerateValue(V);
551 }
552
553 // Also, collect all function-local MDNodes for easy access.
554 FunctionLocalMDs.push_back(N);
Devang Patel05eb6172009-08-04 06:00:18 +0000555}
556
Victor Hernandez572218b2010-01-14 19:54:11 +0000557void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattner8dace892009-12-31 00:51:46 +0000558 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Dan Gohmanc828c542010-08-24 02:24:03 +0000559 assert(!isa<MDNode>(V) && !isa<MDString>(V) &&
560 "EnumerateValue doesn't handle Metadata!");
Devang Patel05eb6172009-08-04 06:00:18 +0000561
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000562 // Check to see if it's already in!
563 unsigned &ValueID = ValueMap[V];
564 if (ValueID) {
565 // Increment use count.
566 Values[ValueID-1].second++;
567 return;
568 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000569
David Majnemerdad0a642014-06-27 18:19:56 +0000570 if (auto *GO = dyn_cast<GlobalObject>(V))
571 if (const Comdat *C = GO->getComdat())
572 Comdats.insert(C);
573
Chris Lattner9ee48362007-05-06 01:00:28 +0000574 // Enumerate the type of this value.
575 EnumerateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000576
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000577 if (const Constant *C = dyn_cast<Constant>(V)) {
578 if (isa<GlobalValue>(C)) {
579 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner9ee48362007-05-06 01:00:28 +0000580 } else if (C->getNumOperands()) {
581 // If a constant has operands, enumerate them. This makes sure that if a
582 // constant has uses (for example an array of const ints), that they are
583 // inserted also.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000584
Chris Lattner9ee48362007-05-06 01:00:28 +0000585 // We prefer to enumerate them with values before we enumerate the user
586 // itself. This makes it more likely that we can avoid forward references
587 // in the reader. We know that there can be no cycles in the constants
588 // graph that don't go through a global variable.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000589 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
590 I != E; ++I)
Chris Lattneraa99c942009-11-01 01:27:45 +0000591 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandez572218b2010-01-14 19:54:11 +0000592 EnumerateValue(*I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000593
Chris Lattner9ee48362007-05-06 01:00:28 +0000594 // Finally, add the value. Doing this could make the ValueID reference be
595 // dangling, don't reuse it.
596 Values.push_back(std::make_pair(V, 1U));
597 ValueMap[V] = Values.size();
598 return;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000599 }
600 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000601
Chris Lattner9ee48362007-05-06 01:00:28 +0000602 // Add the value.
603 Values.push_back(std::make_pair(V, 1U));
604 ValueID = Values.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000605}
606
607
Chris Lattner229907c2011-07-18 04:54:35 +0000608void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000609 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000610
Rafael Espindola337a1b22011-04-06 16:49:37 +0000611 // We've already seen this type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000612 if (*TypeID)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000613 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000614
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000615 // If it is a non-anonymous struct, mark the type as being visited so that we
616 // don't recursively visit it. This is safe because we allow forward
617 // references of these in the bitcode reader.
Chris Lattner229907c2011-07-18 04:54:35 +0000618 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner335d3992011-08-12 18:06:37 +0000619 if (!STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000620 *TypeID = ~0U;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000621
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000622 // Enumerate all of the subtypes before we enumerate this type. This ensures
623 // that the type will be enumerated in an order that can be directly built.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000624 for (Type::subtype_iterator I = Ty->subtype_begin(), E = Ty->subtype_end();
625 I != E; ++I)
626 EnumerateType(*I);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000627
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000628 // Refresh the TypeID pointer in case the table rehashed.
629 TypeID = &TypeMap[Ty];
Joe Abbey2ad8df22012-11-25 15:23:39 +0000630
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000631 // Check to see if we got the pointer another way. This can happen when
632 // enumerating recursive types that hit the base case deeper than they start.
633 //
634 // If this is actually a struct that we are treating as forward ref'able,
635 // then emit the definition now that all of its contents are available.
636 if (*TypeID && *TypeID != ~0U)
637 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000638
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000639 // Add this type now that its contents are all happily enumerated.
640 Types.push_back(Ty);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000642 *TypeID = Types.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000643}
644
Chris Lattner76fd90f2007-05-06 08:35:19 +0000645// Enumerate the types for the specified value. If the value is a constant,
646// walk through it, enumerating the types of the constant.
Victor Hernandez572218b2010-01-14 19:54:11 +0000647void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner76fd90f2007-05-06 08:35:19 +0000648 EnumerateType(V->getType());
Joe Abbey2ad8df22012-11-25 15:23:39 +0000649
Chris Lattner76fd90f2007-05-06 08:35:19 +0000650 if (const Constant *C = dyn_cast<Constant>(V)) {
651 // If this constant is already enumerated, ignore it, we know its type must
652 // be enumerated.
653 if (ValueMap.count(V)) return;
654
655 // This constant may have operands, make sure to enumerate the types in
656 // them.
Chris Lattnerf540d742009-10-28 05:24:40 +0000657 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
Jay Foad0159a1e2011-04-11 09:48:55 +0000658 const Value *Op = C->getOperand(i);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000659
Chris Lattnerf540d742009-10-28 05:24:40 +0000660 // Don't enumerate basic blocks here, this happens as operands to
661 // blockaddress.
662 if (isa<BasicBlock>(Op)) continue;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000663
Dan Gohman9cfe5322010-08-25 17:09:03 +0000664 EnumerateOperandType(Op);
Chris Lattnerf540d742009-10-28 05:24:40 +0000665 }
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000666
667 if (const MDNode *N = dyn_cast<MDNode>(V)) {
Chris Lattner9b493022009-12-31 01:22:29 +0000668 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
669 if (Value *Elem = N->getOperand(i))
Victor Hernandez572218b2010-01-14 19:54:11 +0000670 EnumerateOperandType(Elem);
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000671 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000672 } else if (isa<MDString>(V) || isa<MDNode>(V))
Dan Gohmanab09a122010-08-24 02:10:52 +0000673 EnumerateMetadata(V);
Chris Lattner76fd90f2007-05-06 08:35:19 +0000674}
675
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000676void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000677 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000678
Chris Lattnere4bbad62007-05-03 22:46:43 +0000679 // Do a lookup.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000680 unsigned &Entry = AttributeMap[PAL];
Chris Lattnere4bbad62007-05-03 22:46:43 +0000681 if (Entry == 0) {
682 // Never saw this before, add it.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000683 Attribute.push_back(PAL);
684 Entry = Attribute.size();
Chris Lattnere4bbad62007-05-03 22:46:43 +0000685 }
Bill Wendling51f612e2013-02-10 23:06:02 +0000686
687 // Do lookups for all attribute groups.
688 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
689 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendling92ed7002013-02-11 22:33:26 +0000690 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling51f612e2013-02-10 23:06:02 +0000691 if (Entry == 0) {
Bill Wendling92ed7002013-02-11 22:33:26 +0000692 AttributeGroups.push_back(AS);
693 Entry = AttributeGroups.size();
Bill Wendling51f612e2013-02-10 23:06:02 +0000694 }
695 }
Chris Lattnere4bbad62007-05-03 22:46:43 +0000696}
697
Chad Rosier6a11b642011-06-03 17:02:19 +0000698void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewyckya72e1af2010-02-25 08:30:17 +0000699 InstructionCount = 0;
Chris Lattnere6e364c2007-04-26 05:53:54 +0000700 NumModuleValues = Values.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000701 NumModuleMDValues = MDValues.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000702
Chris Lattner5f640b92007-04-26 03:50:57 +0000703 // Adding function arguments to the value table.
Dan Gohman9a54c172010-07-16 22:58:39 +0000704 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
705 I != E; ++I)
Chris Lattner5f640b92007-04-26 03:50:57 +0000706 EnumerateValue(I);
707
Chris Lattnere6e364c2007-04-26 05:53:54 +0000708 FirstFuncConstantID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000709
Chris Lattner5f640b92007-04-26 03:50:57 +0000710 // Add all function-level constants to the value table.
711 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
712 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000713 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner5f640b92007-04-26 03:50:57 +0000714 OI != E; ++OI) {
715 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
716 isa<InlineAsm>(*OI))
717 EnumerateValue(*OI);
718 }
Chris Lattner7c37b012007-04-26 04:42:16 +0000719 BasicBlocks.push_back(BB);
Chris Lattner6be58c62007-05-03 22:18:21 +0000720 ValueMap[BB] = BasicBlocks.size();
Chris Lattner5f640b92007-04-26 03:50:57 +0000721 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000722
Chris Lattner430e80d2007-05-04 05:21:47 +0000723 // Optimize the constant layout.
724 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000725
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000726 // Add the function's parameter attributes so they are available for use in
727 // the function's instruction.
Devang Patel4c758ea2008-09-25 21:00:45 +0000728 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000729
Chris Lattnere6e364c2007-04-26 05:53:54 +0000730 FirstInstID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000731
Devang Pateldf84e8b2010-06-02 23:05:04 +0000732 SmallVector<MDNode *, 8> FnLocalMDVector;
Chris Lattner5f640b92007-04-26 03:50:57 +0000733 // Add all of the instructions.
734 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000735 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezcad73282010-01-13 19:36:16 +0000736 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
737 OI != E; ++OI) {
Victor Hernandez572218b2010-01-14 19:54:11 +0000738 if (MDNode *MD = dyn_cast<MDNode>(*OI))
Victor Hernandez1b081382010-02-06 01:21:09 +0000739 if (MD->isFunctionLocal() && MD->getFunction())
Victor Hernandezd44ee352010-02-04 01:13:08 +0000740 // Enumerate metadata after the instructions they might refer to.
Devang Pateldf84e8b2010-06-02 23:05:04 +0000741 FnLocalMDVector.push_back(MD);
Victor Hernandezcad73282010-01-13 19:36:16 +0000742 }
Dan Gohmanc828c542010-08-24 02:24:03 +0000743
744 SmallVector<std::pair<unsigned, MDNode*>, 8> MDs;
745 I->getAllMetadataOtherThanDebugLoc(MDs);
746 for (unsigned i = 0, e = MDs.size(); i != e; ++i) {
747 MDNode *N = MDs[i].second;
748 if (N->isFunctionLocal() && N->getFunction())
749 FnLocalMDVector.push_back(N);
750 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000751
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000752 if (!I->getType()->isVoidTy())
Chris Lattner5f640b92007-04-26 03:50:57 +0000753 EnumerateValue(I);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000754 }
755 }
Victor Hernandezd44ee352010-02-04 01:13:08 +0000756
757 // Add all of the function-local metadata.
Devang Pateldf84e8b2010-06-02 23:05:04 +0000758 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohmanc828c542010-08-24 02:24:03 +0000759 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000760}
761
Chad Rosier6a11b642011-06-03 17:02:19 +0000762void ValueEnumerator::purgeFunction() {
Chris Lattner5f640b92007-04-26 03:50:57 +0000763 /// Remove purged values from the ValueMap.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000764 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner5f640b92007-04-26 03:50:57 +0000765 ValueMap.erase(Values[i].first);
Dan Gohmanc828c542010-08-24 02:24:03 +0000766 for (unsigned i = NumModuleMDValues, e = MDValues.size(); i != e; ++i)
767 MDValueMap.erase(MDValues[i].first);
Chris Lattner7c37b012007-04-26 04:42:16 +0000768 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
769 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000770
Chris Lattnere6e364c2007-04-26 05:53:54 +0000771 Values.resize(NumModuleValues);
Dan Gohmanc828c542010-08-24 02:24:03 +0000772 MDValues.resize(NumModuleMDValues);
Chris Lattner7c37b012007-04-26 04:42:16 +0000773 BasicBlocks.clear();
Dan Gohman22161da2010-08-25 17:11:16 +0000774 FunctionLocalMDs.clear();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000775}
Chris Lattnerf540d742009-10-28 05:24:40 +0000776
777static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
778 DenseMap<const BasicBlock*, unsigned> &IDMap) {
779 unsigned Counter = 0;
780 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
781 IDMap[BB] = ++Counter;
782}
783
784/// getGlobalBasicBlockID - This returns the function-specific ID for the
785/// specified basic block. This is relatively expensive information, so it
786/// should only be used by rare constructs such as address-of-label.
787unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
788 unsigned &Idx = GlobalBasicBlockIDs[BB];
789 if (Idx != 0)
Chris Lattneraa99c942009-11-01 01:27:45 +0000790 return Idx-1;
Chris Lattnerf540d742009-10-28 05:24:40 +0000791
792 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
793 return getGlobalBasicBlockID(BB);
794}
795