blob: c90dfbd776abf4597d857ef1e246380fa9770dfc [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
Rafael Espindola49e9bf82014-11-17 20:06:27 +000071static OrderMap orderModule(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000072 // 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.
Rafael Espindola49e9bf82014-11-17 20:06:27 +000081 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000082 if (G.hasInitializer())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000083 if (!isa<GlobalValue>(G.getInitializer()))
84 orderValue(G.getInitializer(), OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +000085 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000086 if (!isa<GlobalValue>(A.getAliasee()))
87 orderValue(A.getAliasee(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000088 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000089 if (F.hasPrefixData())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000090 if (!isa<GlobalValue>(F.getPrefixData()))
91 orderValue(F.getPrefixData(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000092 if (F.hasPrologueData())
93 if (!isa<GlobalValue>(F.getPrologueData()))
94 orderValue(F.getPrologueData(), OM);
95 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000096 OM.LastGlobalConstantID = OM.size();
97
98 // Initializers of GlobalValues are processed in
99 // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
100 // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
101 // by giving IDs in reverse order.
102 //
103 // Since GlobalValues never reference each other directly (just through
104 // initializers), their relative IDs only matter for determining order of
105 // uses in their initializers.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000106 for (const Function &F : M)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000107 orderValue(&F, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000108 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000109 orderValue(&A, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000110 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000111 orderValue(&G, OM);
112 OM.LastGlobalValueID = OM.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000113
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000114 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000115 if (F.isDeclaration())
116 continue;
117 // Here we need to match the union of ValueEnumerator::incorporateFunction()
118 // and WriteFunction(). Basic blocks are implicitly declared before
119 // anything else (by declaring their size).
120 for (const BasicBlock &BB : F)
121 orderValue(&BB, OM);
122 for (const Argument &A : F.args())
123 orderValue(&A, OM);
124 for (const BasicBlock &BB : F)
125 for (const Instruction &I : BB)
126 for (const Value *Op : I.operands())
127 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
128 isa<InlineAsm>(*Op))
129 orderValue(Op, OM);
130 for (const BasicBlock &BB : F)
131 for (const Instruction &I : BB)
132 orderValue(&I, OM);
133 }
134 return OM;
135}
136
137static void predictValueUseListOrderImpl(const Value *V, const Function *F,
138 unsigned ID, const OrderMap &OM,
139 UseListOrderStack &Stack) {
140 // Predict use-list order for this one.
141 typedef std::pair<const Use *, unsigned> Entry;
142 SmallVector<Entry, 64> List;
143 for (const Use &U : V->uses())
144 // Check if this user will be serialized.
145 if (OM.lookup(U.getUser()).first)
146 List.push_back(std::make_pair(&U, List.size()));
147
148 if (List.size() < 2)
149 // We may have lost some users.
150 return;
151
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000152 bool IsGlobalValue = OM.isGlobalValue(ID);
153 std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000154 const Use *LU = L.first;
155 const Use *RU = R.first;
Duncan P. N. Exon Smith3f0fc7b2014-07-29 01:13:56 +0000156 if (LU == RU)
157 return false;
158
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000159 auto LID = OM.lookup(LU->getUser()).first;
160 auto RID = OM.lookup(RU->getUser()).first;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000161
162 // Global values are processed in reverse order.
163 //
164 // Moreover, initializers of GlobalValues are set *after* all the globals
165 // have been read (despite having earlier IDs). Rather than awkwardly
166 // modeling this behaviour here, orderModule() has assigned IDs to
167 // initializers of GlobalValues before GlobalValues themselves.
168 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
169 return LID < RID;
170
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000171 // If ID is 4, then expect: 7 6 5 1 2 3.
172 if (LID < RID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000173 if (RID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000174 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
175 return true;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000176 return false;
177 }
178 if (RID < LID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000179 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000180 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
181 return false;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000182 return true;
183 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000184
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000185 // LID and RID are equal, so we have different operands of the same user.
186 // Assume operands are added in order for all instructions.
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000187 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000188 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
189 return LU->getOperandNo() < RU->getOperandNo();
190 return LU->getOperandNo() > RU->getOperandNo();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000191 });
192
193 if (std::is_sorted(
194 List.begin(), List.end(),
195 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
196 // Order is already correct.
197 return;
198
199 // Store the shuffle.
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000200 Stack.emplace_back(V, F, List.size());
201 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
Duncan P. N. Exon Smithf849ace2014-07-28 22:41:50 +0000202 for (size_t I = 0, E = List.size(); I != E; ++I)
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000203 Stack.back().Shuffle[I] = List[I].second;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000204}
205
206static void predictValueUseListOrder(const Value *V, const Function *F,
207 OrderMap &OM, UseListOrderStack &Stack) {
208 auto &IDPair = OM[V];
209 assert(IDPair.first && "Unmapped value");
210 if (IDPair.second)
211 // Already predicted.
212 return;
213
214 // Do the actual prediction.
215 IDPair.second = true;
216 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
217 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
218
219 // Recursive descent into constants.
220 if (const Constant *C = dyn_cast<Constant>(V))
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000221 if (C->getNumOperands()) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000222 for (const Value *Op : C->operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000223 if (isa<Constant>(Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000224 predictValueUseListOrder(Op, F, OM, Stack);
225}
226
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000227static UseListOrderStack predictUseListOrder(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000228 OrderMap OM = orderModule(M);
229
230 // Use-list orders need to be serialized after all the users have been added
231 // to a value, or else the shuffles will be incomplete. Store them per
232 // function in a stack.
233 //
234 // Aside from function order, the order of values doesn't matter much here.
235 UseListOrderStack Stack;
236
237 // We want to visit the functions backward now so we can list function-local
238 // constants in the last Function they're used in. Module-level constants
239 // have already been visited above.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000240 for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000241 const Function &F = *I;
242 if (F.isDeclaration())
243 continue;
244 for (const BasicBlock &BB : F)
245 predictValueUseListOrder(&BB, &F, OM, Stack);
246 for (const Argument &A : F.args())
247 predictValueUseListOrder(&A, &F, OM, Stack);
248 for (const BasicBlock &BB : F)
249 for (const Instruction &I : BB)
250 for (const Value *Op : I.operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000251 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000252 predictValueUseListOrder(Op, &F, OM, Stack);
253 for (const BasicBlock &BB : F)
254 for (const Instruction &I : BB)
255 predictValueUseListOrder(&I, &F, OM, Stack);
256 }
257
258 // Visit globals last, since the module-level use-list block will be seen
259 // before the function bodies are processed.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000260 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000261 predictValueUseListOrder(&G, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000262 for (const Function &F : M)
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000263 predictValueUseListOrder(&F, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000264 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000265 predictValueUseListOrder(&A, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000266 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000267 if (G.hasInitializer())
268 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000269 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000270 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000271 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000272 if (F.hasPrefixData())
273 predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000274 if (F.hasPrologueData())
275 predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
276 }
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000277
278 return Stack;
279}
280
Duncan Sandse6beec62012-11-13 12:59:33 +0000281static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
282 return V.first->getType()->isIntOrIntVectorTy();
Chris Lattner430e80d2007-05-04 05:21:47 +0000283}
284
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000285ValueEnumerator::ValueEnumerator(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000286 if (shouldPreserveBitcodeUseListOrder())
287 UseListOrders = predictUseListOrder(M);
288
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000289 // Enumerate the global variables.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000290 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
291 I != E; ++I)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000292 EnumerateValue(I);
293
294 // Enumerate the functions.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000295 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000296 EnumerateValue(I);
Devang Patel4c758ea2008-09-25 21:00:45 +0000297 EnumerateAttributes(cast<Function>(I)->getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000298 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000299
Chris Lattner44c17072007-04-26 02:46:40 +0000300 // Enumerate the aliases.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000301 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner44c17072007-04-26 02:46:40 +0000302 I != E; ++I)
303 EnumerateValue(I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000304
Chris Lattner430e80d2007-05-04 05:21:47 +0000305 // Remember what is the cutoff between globalvalue's and other constants.
306 unsigned FirstConstant = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000307
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000308 // Enumerate the global variable initializers.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000309 for (Module::const_global_iterator I = M.global_begin(), E = M.global_end();
310 I != E; ++I)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000311 if (I->hasInitializer())
312 EnumerateValue(I->getInitializer());
313
Chris Lattner44c17072007-04-26 02:46:40 +0000314 // Enumerate the aliasees.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000315 for (Module::const_alias_iterator I = M.alias_begin(), E = M.alias_end();
Chris Lattner44c17072007-04-26 02:46:40 +0000316 I != E; ++I)
317 EnumerateValue(I->getAliasee());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000318
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000319 // Enumerate the prefix data constants.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000320 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000321 if (I->hasPrefixData())
322 EnumerateValue(I->getPrefixData());
323
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000324 // Enumerate the prologue data constants.
325 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
326 if (I->hasPrologueData())
327 EnumerateValue(I->getPrologueData());
328
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000329 // Enumerate the metadata type.
330 //
331 // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
332 // only encodes the metadata type when it's used as a value.
333 EnumerateType(Type::getMetadataTy(M.getContext()));
334
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000335 // Insert constants and metadata that are named at module level into the slot
Devang Patelfcfee0f2010-01-07 19:39:36 +0000336 // pool so that the module symbol table can refer to them...
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000337 EnumerateValueSymbolTable(M.getValueSymbolTable());
Dan Gohman2637cc12010-07-21 23:38:33 +0000338 EnumerateNamedMetadata(M);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000339
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000340 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
Chris Lattner8dace892009-12-31 00:51:46 +0000341
Chris Lattner5f640b92007-04-26 03:50:57 +0000342 // Enumerate types used by function bodies and argument lists.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000343 for (const Function &F : M) {
Rafael Espindola087d6272014-06-17 03:00:40 +0000344 for (const Argument &A : F.args())
345 EnumerateType(A.getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000346
Rafael Espindola087d6272014-06-17 03:00:40 +0000347 for (const BasicBlock &BB : F)
348 for (const Instruction &I : BB) {
349 for (const Use &Op : I.operands()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000350 auto *MD = dyn_cast<MetadataAsValue>(&Op);
351 if (!MD) {
352 EnumerateOperandType(Op);
353 continue;
354 }
355
356 // Local metadata is enumerated during function-incorporation.
357 if (isa<LocalAsMetadata>(MD->getMetadata()))
358 continue;
359
360 EnumerateMetadata(MD->getMetadata());
Victor Hernandez572218b2010-01-14 19:54:11 +0000361 }
Rafael Espindola087d6272014-06-17 03:00:40 +0000362 EnumerateType(I.getType());
363 if (const CallInst *CI = dyn_cast<CallInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000364 EnumerateAttributes(CI->getAttributes());
Rafael Espindola087d6272014-06-17 03:00:40 +0000365 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000366 EnumerateAttributes(II->getAttributes());
Devang Patelaf206b82009-09-18 19:26:43 +0000367
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000368 // Enumerate metadata attached with this instruction.
Devang Patel6da5dbf2009-10-22 18:55:16 +0000369 MDs.clear();
Rafael Espindola087d6272014-06-17 03:00:40 +0000370 I.getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000371 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000372 EnumerateMetadata(MDs[i].second);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000373
Rafael Espindola087d6272014-06-17 03:00:40 +0000374 if (!I.getDebugLoc().isUnknown()) {
Chris Lattner07d09ed2010-04-03 02:17:50 +0000375 MDNode *Scope, *IA;
Rafael Espindola087d6272014-06-17 03:00:40 +0000376 I.getDebugLoc().getScopeAndInlinedAt(Scope, IA, I.getContext());
Chris Lattner07d09ed2010-04-03 02:17:50 +0000377 if (Scope) EnumerateMetadata(Scope);
378 if (IA) EnumerateMetadata(IA);
379 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000380 }
381 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000382
Chris Lattner430e80d2007-05-04 05:21:47 +0000383 // Optimize constant ordering.
384 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindola337a1b22011-04-06 16:49:37 +0000385}
386
Devang Patelaf206b82009-09-18 19:26:43 +0000387unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
388 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000389 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman1f4b0282010-08-25 17:09:50 +0000390 return I->second;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000391}
Devang Patelaf206b82009-09-18 19:26:43 +0000392
David Majnemerdad0a642014-06-27 18:19:56 +0000393unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
394 unsigned ComdatID = Comdats.idFor(C);
395 assert(ComdatID && "Comdat not found!");
396 return ComdatID;
397}
398
Devang Patelaf206b82009-09-18 19:26:43 +0000399void ValueEnumerator::setInstructionID(const Instruction *I) {
400 InstructionMap[I] = InstructionCount++;
401}
402
Devang Patel05eb6172009-08-04 06:00:18 +0000403unsigned ValueEnumerator::getValueID(const Value *V) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000404 if (auto *MD = dyn_cast<MetadataAsValue>(V))
405 return getMetadataID(MD->getMetadata());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000406
Devang Patel05eb6172009-08-04 06:00:18 +0000407 ValueMapType::const_iterator I = ValueMap.find(V);
408 assert(I != ValueMap.end() && "Value not in slotcalculator!");
409 return I->second-1;
410}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000411
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000412unsigned ValueEnumerator::getMetadataID(const Metadata *MD) const {
413 auto I = MDValueMap.find(MD);
414 assert(I != MDValueMap.end() && "Metadata not in slotcalculator!");
415 return I->second - 1;
416}
417
Chad Rosier78037a92011-12-07 20:44:46 +0000418void ValueEnumerator::dump() const {
419 print(dbgs(), ValueMap, "Default");
420 dbgs() << '\n';
421 print(dbgs(), MDValueMap, "MetaData");
422 dbgs() << '\n';
423}
424
425void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
426 const char *Name) const {
427
428 OS << "Map Name: " << Name << "\n";
429 OS << "Size: " << Map.size() << "\n";
430 for (ValueMapType::const_iterator I = Map.begin(),
431 E = Map.end(); I != E; ++I) {
432
433 const Value *V = I->first;
434 if (V->hasName())
435 OS << "Value: " << V->getName();
436 else
437 OS << "Value: [null]\n";
438 V->dump();
439
440 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000441 for (const Use &U : V->uses()) {
442 if (&U != &*V->use_begin())
Chad Rosier78037a92011-12-07 20:44:46 +0000443 OS << ",";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000444 if(U->hasName())
445 OS << " " << U->getName();
Chad Rosier78037a92011-12-07 20:44:46 +0000446 else
447 OS << " [null]";
448
449 }
450 OS << "\n\n";
451 }
452}
453
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000454void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
455 const char *Name) const {
456
457 OS << "Map Name: " << Name << "\n";
458 OS << "Size: " << Map.size() << "\n";
459 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
460 const Metadata *MD = I->first;
461 OS << "Metadata: slot = " << I->second << "\n";
462 MD->dump();
463 }
464}
465
Chris Lattner430e80d2007-05-04 05:21:47 +0000466/// OptimizeConstants - Reorder constant pool for denser encoding.
467void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
468 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000469
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +0000470 if (shouldPreserveBitcodeUseListOrder())
471 // Optimizing constants makes the use-list order difficult to predict.
472 // Disable it for now when trying to preserve the order.
473 return;
474
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000475 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
476 [this](const std::pair<const Value *, unsigned> &LHS,
477 const std::pair<const Value *, unsigned> &RHS) {
478 // Sort by plane.
479 if (LHS.first->getType() != RHS.first->getType())
480 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
481 // Then by frequency.
482 return LHS.second > RHS.second;
483 });
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000484
Duncan Sandse6beec62012-11-13 12:59:33 +0000485 // Ensure that integer and vector of integer constants are at the start of the
486 // constant pool. This is important so that GEP structure indices come before
487 // gep constant exprs.
Chris Lattner430e80d2007-05-04 05:21:47 +0000488 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sandse6beec62012-11-13 12:59:33 +0000489 isIntOrIntVectorValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000490
Chris Lattner430e80d2007-05-04 05:21:47 +0000491 // Rebuild the modified portion of ValueMap.
492 for (; CstStart != CstEnd; ++CstStart)
493 ValueMap[Values[CstStart].first] = CstStart+1;
494}
495
496
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000497/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
498/// table into the values table.
499void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000500 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000501 VI != VE; ++VI)
502 EnumerateValue(VI->getValue());
503}
504
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000505/// Insert all of the values referenced by named metadata in the specified
506/// module.
507void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
508 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
509 E = M.named_metadata_end();
510 I != E; ++I)
Dan Gohman2637cc12010-07-21 23:38:33 +0000511 EnumerateNamedMDNode(I);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000512}
513
Devang Patel99ff5a82010-01-09 00:30:14 +0000514void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel99ff5a82010-01-09 00:30:14 +0000515 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohmand3d2bbe2010-08-24 02:01:24 +0000516 EnumerateMetadata(MD->getOperand(i));
Devang Patel99ff5a82010-01-09 00:30:14 +0000517}
518
Dan Gohmanc828c542010-08-24 02:24:03 +0000519/// EnumerateMDNodeOperands - Enumerate all non-function-local values
520/// and types referenced by the given MDNode.
521void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
522 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000523 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +0000524 if (!MD)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000525 continue;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000526 assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000527 EnumerateMetadata(MD);
Dan Gohmanc828c542010-08-24 02:24:03 +0000528 }
529}
530
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000531void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
532 assert(
533 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
534 "Invalid metadata kind");
Dan Gohmanc828c542010-08-24 02:24:03 +0000535
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000536 // Insert a dummy ID to block the co-recursive call to
537 // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
538 //
539 // Return early if there's already an ID.
540 if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
Devang Patel05eb6172009-08-04 06:00:18 +0000541 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000542
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000543 // Visit operands first to minimize RAUW.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000544 if (auto *N = dyn_cast<MDNode>(MD))
Dan Gohmanc828c542010-08-24 02:24:03 +0000545 EnumerateMDNodeOperands(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000546 else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
547 EnumerateValue(C->getValue());
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000548
549 // Replace the dummy ID inserted above with the correct one. MDValueMap may
550 // have changed by inserting operands, so we need a fresh lookup here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000551 MDs.push_back(MD);
552 MDValueMap[MD] = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000553}
554
555/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000556/// information reachable from the metadata.
557void ValueEnumerator::EnumerateFunctionLocalMetadata(
558 const LocalAsMetadata *Local) {
Dan Gohmanc828c542010-08-24 02:24:03 +0000559 // Check to see if it's already in!
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000560 unsigned &MDValueID = MDValueMap[Local];
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000561 if (MDValueID)
Dan Gohmanc828c542010-08-24 02:24:03 +0000562 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000563
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000564 MDs.push_back(Local);
565 MDValueID = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000566
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000567 EnumerateValue(Local->getValue());
Dan Gohmanc828c542010-08-24 02:24:03 +0000568
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000569 // Also, collect all function-local metadata for easy access.
570 FunctionLocalMDs.push_back(Local);
Devang Patel05eb6172009-08-04 06:00:18 +0000571}
572
Victor Hernandez572218b2010-01-14 19:54:11 +0000573void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattner8dace892009-12-31 00:51:46 +0000574 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000575 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
Devang Patel05eb6172009-08-04 06:00:18 +0000576
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000577 // Check to see if it's already in!
578 unsigned &ValueID = ValueMap[V];
579 if (ValueID) {
580 // Increment use count.
581 Values[ValueID-1].second++;
582 return;
583 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000584
David Majnemerdad0a642014-06-27 18:19:56 +0000585 if (auto *GO = dyn_cast<GlobalObject>(V))
586 if (const Comdat *C = GO->getComdat())
587 Comdats.insert(C);
588
Chris Lattner9ee48362007-05-06 01:00:28 +0000589 // Enumerate the type of this value.
590 EnumerateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000591
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000592 if (const Constant *C = dyn_cast<Constant>(V)) {
593 if (isa<GlobalValue>(C)) {
594 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner9ee48362007-05-06 01:00:28 +0000595 } else if (C->getNumOperands()) {
596 // If a constant has operands, enumerate them. This makes sure that if a
597 // constant has uses (for example an array of const ints), that they are
598 // inserted also.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000599
Chris Lattner9ee48362007-05-06 01:00:28 +0000600 // We prefer to enumerate them with values before we enumerate the user
601 // itself. This makes it more likely that we can avoid forward references
602 // in the reader. We know that there can be no cycles in the constants
603 // graph that don't go through a global variable.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000604 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
605 I != E; ++I)
Chris Lattneraa99c942009-11-01 01:27:45 +0000606 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandez572218b2010-01-14 19:54:11 +0000607 EnumerateValue(*I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000608
Chris Lattner9ee48362007-05-06 01:00:28 +0000609 // Finally, add the value. Doing this could make the ValueID reference be
610 // dangling, don't reuse it.
611 Values.push_back(std::make_pair(V, 1U));
612 ValueMap[V] = Values.size();
613 return;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000614 }
615 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000616
Chris Lattner9ee48362007-05-06 01:00:28 +0000617 // Add the value.
618 Values.push_back(std::make_pair(V, 1U));
619 ValueID = Values.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000620}
621
622
Chris Lattner229907c2011-07-18 04:54:35 +0000623void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000624 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000625
Rafael Espindola337a1b22011-04-06 16:49:37 +0000626 // We've already seen this type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000627 if (*TypeID)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000628 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000629
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000630 // If it is a non-anonymous struct, mark the type as being visited so that we
631 // don't recursively visit it. This is safe because we allow forward
632 // references of these in the bitcode reader.
Chris Lattner229907c2011-07-18 04:54:35 +0000633 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner335d3992011-08-12 18:06:37 +0000634 if (!STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000635 *TypeID = ~0U;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000636
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000637 // Enumerate all of the subtypes before we enumerate this type. This ensures
638 // that the type will be enumerated in an order that can be directly built.
Rafael Espindolaffbfcf22014-11-24 20:44:36 +0000639 for (Type *SubTy : Ty->subtypes())
640 EnumerateType(SubTy);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000641
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000642 // Refresh the TypeID pointer in case the table rehashed.
643 TypeID = &TypeMap[Ty];
Joe Abbey2ad8df22012-11-25 15:23:39 +0000644
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000645 // Check to see if we got the pointer another way. This can happen when
646 // enumerating recursive types that hit the base case deeper than they start.
647 //
648 // If this is actually a struct that we are treating as forward ref'able,
649 // then emit the definition now that all of its contents are available.
650 if (*TypeID && *TypeID != ~0U)
651 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000652
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000653 // Add this type now that its contents are all happily enumerated.
654 Types.push_back(Ty);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000655
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000656 *TypeID = Types.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000657}
658
Chris Lattner76fd90f2007-05-06 08:35:19 +0000659// Enumerate the types for the specified value. If the value is a constant,
660// walk through it, enumerating the types of the constant.
Victor Hernandez572218b2010-01-14 19:54:11 +0000661void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner76fd90f2007-05-06 08:35:19 +0000662 EnumerateType(V->getType());
Joe Abbey2ad8df22012-11-25 15:23:39 +0000663
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000664 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
665 assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
666 "Function-local metadata should be left for later");
Chris Lattner76fd90f2007-05-06 08:35:19 +0000667
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000668 EnumerateMetadata(MD->getMetadata());
669 return;
670 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000671
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000672 const Constant *C = dyn_cast<Constant>(V);
673 if (!C)
674 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000675
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000676 // If this constant is already enumerated, ignore it, we know its type must
677 // be enumerated.
678 if (ValueMap.count(C))
679 return;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000680
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000681 // This constant may have operands, make sure to enumerate the types in
682 // them.
683 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
684 const Value *Op = C->getOperand(i);
685
686 // Don't enumerate basic blocks here, this happens as operands to
687 // blockaddress.
688 if (isa<BasicBlock>(Op))
689 continue;
690
691 EnumerateOperandType(Op);
692 }
Chris Lattner76fd90f2007-05-06 08:35:19 +0000693}
694
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000695void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000696 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000697
Chris Lattnere4bbad62007-05-03 22:46:43 +0000698 // Do a lookup.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000699 unsigned &Entry = AttributeMap[PAL];
Chris Lattnere4bbad62007-05-03 22:46:43 +0000700 if (Entry == 0) {
701 // Never saw this before, add it.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000702 Attribute.push_back(PAL);
703 Entry = Attribute.size();
Chris Lattnere4bbad62007-05-03 22:46:43 +0000704 }
Bill Wendling51f612e2013-02-10 23:06:02 +0000705
706 // Do lookups for all attribute groups.
707 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
708 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendling92ed7002013-02-11 22:33:26 +0000709 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling51f612e2013-02-10 23:06:02 +0000710 if (Entry == 0) {
Bill Wendling92ed7002013-02-11 22:33:26 +0000711 AttributeGroups.push_back(AS);
712 Entry = AttributeGroups.size();
Bill Wendling51f612e2013-02-10 23:06:02 +0000713 }
714 }
Chris Lattnere4bbad62007-05-03 22:46:43 +0000715}
716
Chad Rosier6a11b642011-06-03 17:02:19 +0000717void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewyckya72e1af2010-02-25 08:30:17 +0000718 InstructionCount = 0;
Chris Lattnere6e364c2007-04-26 05:53:54 +0000719 NumModuleValues = Values.size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000720 NumModuleMDs = MDs.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000721
Chris Lattner5f640b92007-04-26 03:50:57 +0000722 // Adding function arguments to the value table.
Dan Gohman9a54c172010-07-16 22:58:39 +0000723 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
724 I != E; ++I)
Chris Lattner5f640b92007-04-26 03:50:57 +0000725 EnumerateValue(I);
726
Chris Lattnere6e364c2007-04-26 05:53:54 +0000727 FirstFuncConstantID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000728
Chris Lattner5f640b92007-04-26 03:50:57 +0000729 // Add all function-level constants to the value table.
730 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
731 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner5f640b92007-04-26 03:50:57 +0000733 OI != E; ++OI) {
734 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
735 isa<InlineAsm>(*OI))
736 EnumerateValue(*OI);
737 }
Chris Lattner7c37b012007-04-26 04:42:16 +0000738 BasicBlocks.push_back(BB);
Chris Lattner6be58c62007-05-03 22:18:21 +0000739 ValueMap[BB] = BasicBlocks.size();
Chris Lattner5f640b92007-04-26 03:50:57 +0000740 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000741
Chris Lattner430e80d2007-05-04 05:21:47 +0000742 // Optimize the constant layout.
743 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000744
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000745 // Add the function's parameter attributes so they are available for use in
746 // the function's instruction.
Devang Patel4c758ea2008-09-25 21:00:45 +0000747 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000748
Chris Lattnere6e364c2007-04-26 05:53:54 +0000749 FirstInstID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000750
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000751 SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
Chris Lattner5f640b92007-04-26 03:50:57 +0000752 // Add all of the instructions.
753 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000754 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezcad73282010-01-13 19:36:16 +0000755 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
756 OI != E; ++OI) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000757 if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
758 if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
Victor Hernandezd44ee352010-02-04 01:13:08 +0000759 // Enumerate metadata after the instructions they might refer to.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000760 FnLocalMDVector.push_back(Local);
Dan Gohmanc828c542010-08-24 02:24:03 +0000761 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000762
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000763 if (!I->getType()->isVoidTy())
Chris Lattner5f640b92007-04-26 03:50:57 +0000764 EnumerateValue(I);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000765 }
766 }
Victor Hernandezd44ee352010-02-04 01:13:08 +0000767
768 // Add all of the function-local metadata.
Devang Pateldf84e8b2010-06-02 23:05:04 +0000769 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohmanc828c542010-08-24 02:24:03 +0000770 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000771}
772
Chad Rosier6a11b642011-06-03 17:02:19 +0000773void ValueEnumerator::purgeFunction() {
Chris Lattner5f640b92007-04-26 03:50:57 +0000774 /// Remove purged values from the ValueMap.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000775 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner5f640b92007-04-26 03:50:57 +0000776 ValueMap.erase(Values[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000777 for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
778 MDValueMap.erase(MDs[i]);
Chris Lattner7c37b012007-04-26 04:42:16 +0000779 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
780 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000781
Chris Lattnere6e364c2007-04-26 05:53:54 +0000782 Values.resize(NumModuleValues);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000783 MDs.resize(NumModuleMDs);
Chris Lattner7c37b012007-04-26 04:42:16 +0000784 BasicBlocks.clear();
Dan Gohman22161da2010-08-25 17:11:16 +0000785 FunctionLocalMDs.clear();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000786}
Chris Lattnerf540d742009-10-28 05:24:40 +0000787
788static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
789 DenseMap<const BasicBlock*, unsigned> &IDMap) {
790 unsigned Counter = 0;
791 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
792 IDMap[BB] = ++Counter;
793}
794
795/// getGlobalBasicBlockID - This returns the function-specific ID for the
796/// specified basic block. This is relatively expensive information, so it
797/// should only be used by rare constructs such as address-of-label.
798unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
799 unsigned &Idx = GlobalBasicBlockIDs[BB];
800 if (Idx != 0)
Chris Lattneraa99c942009-11-01 01:27:45 +0000801 return Idx-1;
Chris Lattnerf540d742009-10-28 05:24:40 +0000802
803 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
804 return getGlobalBasicBlockID(BB);
805}
806