blob: fbbe93f68771b65901b897be9739a5999ad583ba [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"
Duncan P. N. Exon Smithd9901ff2015-02-02 18:53:21 +000018#include "llvm/IR/DebugInfoMetadata.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000019#include "llvm/IR/DerivedTypes.h"
20#include "llvm/IR/Instructions.h"
21#include "llvm/IR/Module.h"
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +000022#include "llvm/IR/UseListOrder.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000023#include "llvm/IR/ValueSymbolTable.h"
Chad Rosier78037a92011-12-07 20:44:46 +000024#include "llvm/Support/Debug.h"
25#include "llvm/Support/raw_ostream.h"
Chris Lattnera8713be2007-05-04 05:05:48 +000026#include <algorithm>
Chris Lattnerc1d10d62007-04-22 06:24:45 +000027using namespace llvm;
28
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000029namespace {
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000030struct OrderMap {
31 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000032 unsigned LastGlobalConstantID;
33 unsigned LastGlobalValueID;
34
35 OrderMap() : LastGlobalConstantID(0), LastGlobalValueID(0) {}
36
37 bool isGlobalConstant(unsigned ID) const {
38 return ID <= LastGlobalConstantID;
39 }
40 bool isGlobalValue(unsigned ID) const {
41 return ID <= LastGlobalValueID && !isGlobalConstant(ID);
42 }
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000043
44 unsigned size() const { return IDs.size(); }
45 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
46 std::pair<unsigned, bool> lookup(const Value *V) const {
47 return IDs.lookup(V);
48 }
Duncan P. N. Exon Smithba4576d2014-07-30 01:20:26 +000049 void index(const Value *V) {
50 // Explicitly sequence get-size and insert-value operations to avoid UB.
51 unsigned ID = IDs.size() + 1;
52 IDs[V].first = ID;
53 }
Duncan P. N. Exon Smith2e6a87b2014-07-29 23:03:40 +000054};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000055}
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000056
57static void orderValue(const Value *V, OrderMap &OM) {
58 if (OM.lookup(V).first)
59 return;
60
61 if (const Constant *C = dyn_cast<Constant>(V))
62 if (C->getNumOperands() && !isa<GlobalValue>(C))
63 for (const Value *Op : C->operands())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000064 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000065 orderValue(Op, OM);
66
67 // Note: we cannot cache this lookup above, since inserting into the map
Duncan P. N. Exon Smithba4576d2014-07-30 01:20:26 +000068 // changes the map's size, and thus affects the other IDs.
69 OM.index(V);
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000070}
71
Rafael Espindola49e9bf82014-11-17 20:06:27 +000072static OrderMap orderModule(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000073 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
74 // and ValueEnumerator::incorporateFunction().
75 OrderMap OM;
76
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000077 // In the reader, initializers of GlobalValues are set *after* all the
78 // globals have been read. Rather than awkwardly modeling this behaviour
79 // directly in predictValueUseListOrderImpl(), just assign IDs to
80 // initializers of GlobalValues before GlobalValues themselves to model this
81 // implicitly.
Rafael Espindola49e9bf82014-11-17 20:06:27 +000082 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000083 if (G.hasInitializer())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000084 if (!isa<GlobalValue>(G.getInitializer()))
85 orderValue(G.getInitializer(), OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +000086 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000087 if (!isa<GlobalValue>(A.getAliasee()))
88 orderValue(A.getAliasee(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000089 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +000090 if (F.hasPrefixData())
Duncan P. N. Exon Smith91778672014-07-31 00:13:28 +000091 if (!isa<GlobalValue>(F.getPrefixData()))
92 orderValue(F.getPrefixData(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000093 if (F.hasPrologueData())
94 if (!isa<GlobalValue>(F.getPrologueData()))
95 orderValue(F.getPrologueData(), OM);
David Majnemer7fddecc2015-06-17 20:52:32 +000096 if (F.hasPersonalityFn())
97 if (!isa<GlobalValue>(F.getPersonalityFn()))
98 orderValue(F.getPersonalityFn(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000099 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000100 OM.LastGlobalConstantID = OM.size();
101
102 // Initializers of GlobalValues are processed in
103 // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
104 // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
105 // by giving IDs in reverse order.
106 //
107 // Since GlobalValues never reference each other directly (just through
108 // initializers), their relative IDs only matter for determining order of
109 // uses in their initializers.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000110 for (const Function &F : M)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000111 orderValue(&F, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000112 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000113 orderValue(&A, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000114 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000115 orderValue(&G, OM);
116 OM.LastGlobalValueID = OM.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000117
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000118 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000119 if (F.isDeclaration())
120 continue;
121 // Here we need to match the union of ValueEnumerator::incorporateFunction()
122 // and WriteFunction(). Basic blocks are implicitly declared before
123 // anything else (by declaring their size).
124 for (const BasicBlock &BB : F)
125 orderValue(&BB, OM);
126 for (const Argument &A : F.args())
127 orderValue(&A, OM);
128 for (const BasicBlock &BB : F)
129 for (const Instruction &I : BB)
130 for (const Value *Op : I.operands())
131 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
132 isa<InlineAsm>(*Op))
133 orderValue(Op, OM);
134 for (const BasicBlock &BB : F)
135 for (const Instruction &I : BB)
136 orderValue(&I, OM);
137 }
138 return OM;
139}
140
141static void predictValueUseListOrderImpl(const Value *V, const Function *F,
142 unsigned ID, const OrderMap &OM,
143 UseListOrderStack &Stack) {
144 // Predict use-list order for this one.
145 typedef std::pair<const Use *, unsigned> Entry;
146 SmallVector<Entry, 64> List;
147 for (const Use &U : V->uses())
148 // Check if this user will be serialized.
149 if (OM.lookup(U.getUser()).first)
150 List.push_back(std::make_pair(&U, List.size()));
151
152 if (List.size() < 2)
153 // We may have lost some users.
154 return;
155
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000156 bool IsGlobalValue = OM.isGlobalValue(ID);
157 std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000158 const Use *LU = L.first;
159 const Use *RU = R.first;
Duncan P. N. Exon Smith3f0fc7b2014-07-29 01:13:56 +0000160 if (LU == RU)
161 return false;
162
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000163 auto LID = OM.lookup(LU->getUser()).first;
164 auto RID = OM.lookup(RU->getUser()).first;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000165
166 // Global values are processed in reverse order.
167 //
168 // Moreover, initializers of GlobalValues are set *after* all the globals
169 // have been read (despite having earlier IDs). Rather than awkwardly
170 // modeling this behaviour here, orderModule() has assigned IDs to
171 // initializers of GlobalValues before GlobalValues themselves.
172 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
173 return LID < RID;
174
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000175 // If ID is 4, then expect: 7 6 5 1 2 3.
176 if (LID < RID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000177 if (RID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000178 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
179 return true;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000180 return false;
181 }
182 if (RID < LID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000183 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000184 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
185 return false;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000186 return true;
187 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000188
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000189 // LID and RID are equal, so we have different operands of the same user.
190 // Assume operands are added in order for all instructions.
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000191 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000192 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
193 return LU->getOperandNo() < RU->getOperandNo();
194 return LU->getOperandNo() > RU->getOperandNo();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000195 });
196
197 if (std::is_sorted(
198 List.begin(), List.end(),
199 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
200 // Order is already correct.
201 return;
202
203 // Store the shuffle.
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000204 Stack.emplace_back(V, F, List.size());
205 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
Duncan P. N. Exon Smithf849ace2014-07-28 22:41:50 +0000206 for (size_t I = 0, E = List.size(); I != E; ++I)
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000207 Stack.back().Shuffle[I] = List[I].second;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000208}
209
210static void predictValueUseListOrder(const Value *V, const Function *F,
211 OrderMap &OM, UseListOrderStack &Stack) {
212 auto &IDPair = OM[V];
213 assert(IDPair.first && "Unmapped value");
214 if (IDPair.second)
215 // Already predicted.
216 return;
217
218 // Do the actual prediction.
219 IDPair.second = true;
220 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
221 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
222
223 // Recursive descent into constants.
224 if (const Constant *C = dyn_cast<Constant>(V))
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000225 if (C->getNumOperands()) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000226 for (const Value *Op : C->operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000227 if (isa<Constant>(Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000228 predictValueUseListOrder(Op, F, OM, Stack);
229}
230
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000231static UseListOrderStack predictUseListOrder(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000232 OrderMap OM = orderModule(M);
233
234 // Use-list orders need to be serialized after all the users have been added
235 // to a value, or else the shuffles will be incomplete. Store them per
236 // function in a stack.
237 //
238 // Aside from function order, the order of values doesn't matter much here.
239 UseListOrderStack Stack;
240
241 // We want to visit the functions backward now so we can list function-local
242 // constants in the last Function they're used in. Module-level constants
243 // have already been visited above.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000244 for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000245 const Function &F = *I;
246 if (F.isDeclaration())
247 continue;
248 for (const BasicBlock &BB : F)
249 predictValueUseListOrder(&BB, &F, OM, Stack);
250 for (const Argument &A : F.args())
251 predictValueUseListOrder(&A, &F, OM, Stack);
252 for (const BasicBlock &BB : F)
253 for (const Instruction &I : BB)
254 for (const Value *Op : I.operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000255 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000256 predictValueUseListOrder(Op, &F, OM, Stack);
257 for (const BasicBlock &BB : F)
258 for (const Instruction &I : BB)
259 predictValueUseListOrder(&I, &F, OM, Stack);
260 }
261
262 // Visit globals last, since the module-level use-list block will be seen
263 // before the function bodies are processed.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000264 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000265 predictValueUseListOrder(&G, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000266 for (const Function &F : M)
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000267 predictValueUseListOrder(&F, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000268 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000269 predictValueUseListOrder(&A, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000270 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000271 if (G.hasInitializer())
272 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000273 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000274 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000275 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000276 if (F.hasPrefixData())
277 predictValueUseListOrder(F.getPrefixData(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000278 if (F.hasPrologueData())
279 predictValueUseListOrder(F.getPrologueData(), nullptr, OM, Stack);
David Majnemer7fddecc2015-06-17 20:52:32 +0000280 if (F.hasPersonalityFn())
281 predictValueUseListOrder(F.getPersonalityFn(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000282 }
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000283
284 return Stack;
285}
286
Duncan Sandse6beec62012-11-13 12:59:33 +0000287static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
288 return V.first->getType()->isIntOrIntVectorTy();
Chris Lattner430e80d2007-05-04 05:21:47 +0000289}
290
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000291ValueEnumerator::ValueEnumerator(const Module &M,
292 bool ShouldPreserveUseListOrder)
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000293 : HasMDString(false), HasDILocation(false), HasGenericDINode(false),
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000294 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
295 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000296 UseListOrders = predictUseListOrder(M);
297
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000298 // Enumerate the global variables.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000299 for (const GlobalVariable &GV : M.globals())
300 EnumerateValue(&GV);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000301
302 // Enumerate the functions.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000303 for (const Function & F : M) {
304 EnumerateValue(&F);
305 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000306 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000307
Chris Lattner44c17072007-04-26 02:46:40 +0000308 // Enumerate the aliases.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000309 for (const GlobalAlias &GA : M.aliases())
310 EnumerateValue(&GA);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000311
Chris Lattner430e80d2007-05-04 05:21:47 +0000312 // Remember what is the cutoff between globalvalue's and other constants.
313 unsigned FirstConstant = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000314
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000315 // Enumerate the global variable initializers.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000316 for (const GlobalVariable &GV : M.globals())
317 if (GV.hasInitializer())
318 EnumerateValue(GV.getInitializer());
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000319
Chris Lattner44c17072007-04-26 02:46:40 +0000320 // Enumerate the aliasees.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000321 for (const GlobalAlias &GA : M.aliases())
322 EnumerateValue(GA.getAliasee());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000323
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000324 // Enumerate the prefix data constants.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000325 for (const Function &F : M)
326 if (F.hasPrefixData())
327 EnumerateValue(F.getPrefixData());
Peter Collingbourne3fa50f92013-09-16 01:08:15 +0000328
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000329 // Enumerate the prologue data constants.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000330 for (const Function &F : M)
331 if (F.hasPrologueData())
332 EnumerateValue(F.getPrologueData());
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000333
David Majnemer7fddecc2015-06-17 20:52:32 +0000334 // Enumerate the personality functions.
335 for (Module::const_iterator I = M.begin(), E = M.end(); I != E; ++I)
336 if (I->hasPersonalityFn())
337 EnumerateValue(I->getPersonalityFn());
338
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000339 // Enumerate the metadata type.
340 //
341 // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
342 // only encodes the metadata type when it's used as a value.
343 EnumerateType(Type::getMetadataTy(M.getContext()));
344
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000345 // Insert constants and metadata that are named at module level into the slot
Devang Patelfcfee0f2010-01-07 19:39:36 +0000346 // pool so that the module symbol table can refer to them...
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000347 EnumerateValueSymbolTable(M.getValueSymbolTable());
Dan Gohman2637cc12010-07-21 23:38:33 +0000348 EnumerateNamedMetadata(M);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000349
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000350 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
Chris Lattner8dace892009-12-31 00:51:46 +0000351
Chris Lattner5f640b92007-04-26 03:50:57 +0000352 // Enumerate types used by function bodies and argument lists.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000353 for (const Function &F : M) {
Rafael Espindola087d6272014-06-17 03:00:40 +0000354 for (const Argument &A : F.args())
355 EnumerateType(A.getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000356
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000357 // Enumerate metadata attached to this function.
358 F.getAllMetadata(MDs);
359 for (const auto &I : MDs)
360 EnumerateMetadata(I.second);
361
Rafael Espindola087d6272014-06-17 03:00:40 +0000362 for (const BasicBlock &BB : F)
363 for (const Instruction &I : BB) {
364 for (const Use &Op : I.operands()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000365 auto *MD = dyn_cast<MetadataAsValue>(&Op);
366 if (!MD) {
367 EnumerateOperandType(Op);
368 continue;
369 }
370
371 // Local metadata is enumerated during function-incorporation.
372 if (isa<LocalAsMetadata>(MD->getMetadata()))
373 continue;
374
375 EnumerateMetadata(MD->getMetadata());
Victor Hernandez572218b2010-01-14 19:54:11 +0000376 }
Rafael Espindola087d6272014-06-17 03:00:40 +0000377 EnumerateType(I.getType());
378 if (const CallInst *CI = dyn_cast<CallInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000379 EnumerateAttributes(CI->getAttributes());
Rafael Espindola087d6272014-06-17 03:00:40 +0000380 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000381 EnumerateAttributes(II->getAttributes());
Devang Patelaf206b82009-09-18 19:26:43 +0000382
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000383 // Enumerate metadata attached with this instruction.
Devang Patel6da5dbf2009-10-22 18:55:16 +0000384 MDs.clear();
Rafael Espindola087d6272014-06-17 03:00:40 +0000385 I.getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000386 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000387 EnumerateMetadata(MDs[i].second);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000388
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000389 // Don't enumerate the location directly -- it has a special record
390 // type -- but enumerate its operands.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000391 if (DILocation *L = I.getDebugLoc())
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000392 EnumerateMDNodeOperands(L);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000393 }
394 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000395
Chris Lattner430e80d2007-05-04 05:21:47 +0000396 // Optimize constant ordering.
397 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindola337a1b22011-04-06 16:49:37 +0000398}
399
Devang Patelaf206b82009-09-18 19:26:43 +0000400unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
401 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000402 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman1f4b0282010-08-25 17:09:50 +0000403 return I->second;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404}
Devang Patelaf206b82009-09-18 19:26:43 +0000405
David Majnemerdad0a642014-06-27 18:19:56 +0000406unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
407 unsigned ComdatID = Comdats.idFor(C);
408 assert(ComdatID && "Comdat not found!");
409 return ComdatID;
410}
411
Devang Patelaf206b82009-09-18 19:26:43 +0000412void ValueEnumerator::setInstructionID(const Instruction *I) {
413 InstructionMap[I] = InstructionCount++;
414}
415
Devang Patel05eb6172009-08-04 06:00:18 +0000416unsigned ValueEnumerator::getValueID(const Value *V) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000417 if (auto *MD = dyn_cast<MetadataAsValue>(V))
418 return getMetadataID(MD->getMetadata());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000419
Devang Patel05eb6172009-08-04 06:00:18 +0000420 ValueMapType::const_iterator I = ValueMap.find(V);
421 assert(I != ValueMap.end() && "Value not in slotcalculator!");
422 return I->second-1;
423}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000424
Chad Rosier78037a92011-12-07 20:44:46 +0000425void ValueEnumerator::dump() const {
426 print(dbgs(), ValueMap, "Default");
427 dbgs() << '\n';
428 print(dbgs(), MDValueMap, "MetaData");
429 dbgs() << '\n';
430}
431
432void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
433 const char *Name) const {
434
435 OS << "Map Name: " << Name << "\n";
436 OS << "Size: " << Map.size() << "\n";
437 for (ValueMapType::const_iterator I = Map.begin(),
438 E = Map.end(); I != E; ++I) {
439
440 const Value *V = I->first;
441 if (V->hasName())
442 OS << "Value: " << V->getName();
443 else
444 OS << "Value: [null]\n";
445 V->dump();
446
447 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000448 for (const Use &U : V->uses()) {
449 if (&U != &*V->use_begin())
Chad Rosier78037a92011-12-07 20:44:46 +0000450 OS << ",";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000451 if(U->hasName())
452 OS << " " << U->getName();
Chad Rosier78037a92011-12-07 20:44:46 +0000453 else
454 OS << " [null]";
455
456 }
457 OS << "\n\n";
458 }
459}
460
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000461void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
462 const char *Name) const {
463
464 OS << "Map Name: " << Name << "\n";
465 OS << "Size: " << Map.size() << "\n";
466 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
467 const Metadata *MD = I->first;
468 OS << "Metadata: slot = " << I->second << "\n";
Nick Lewyckyee0a3a72014-12-17 01:52:08 +0000469 MD->print(OS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000470 }
471}
472
Chris Lattner430e80d2007-05-04 05:21:47 +0000473/// OptimizeConstants - Reorder constant pool for denser encoding.
474void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
475 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000476
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000477 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +0000478 // Optimizing constants makes the use-list order difficult to predict.
479 // Disable it for now when trying to preserve the order.
480 return;
481
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000482 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
483 [this](const std::pair<const Value *, unsigned> &LHS,
484 const std::pair<const Value *, unsigned> &RHS) {
485 // Sort by plane.
486 if (LHS.first->getType() != RHS.first->getType())
487 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
488 // Then by frequency.
489 return LHS.second > RHS.second;
490 });
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000491
Duncan Sandse6beec62012-11-13 12:59:33 +0000492 // Ensure that integer and vector of integer constants are at the start of the
493 // constant pool. This is important so that GEP structure indices come before
494 // gep constant exprs.
Chris Lattner430e80d2007-05-04 05:21:47 +0000495 std::partition(Values.begin()+CstStart, Values.begin()+CstEnd,
Duncan Sandse6beec62012-11-13 12:59:33 +0000496 isIntOrIntVectorValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000497
Chris Lattner430e80d2007-05-04 05:21:47 +0000498 // Rebuild the modified portion of ValueMap.
499 for (; CstStart != CstEnd; ++CstStart)
500 ValueMap[Values[CstStart].first] = CstStart+1;
501}
502
503
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000504/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
505/// table into the values table.
506void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000507 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000508 VI != VE; ++VI)
509 EnumerateValue(VI->getValue());
510}
511
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000512/// Insert all of the values referenced by named metadata in the specified
513/// module.
514void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
515 for (Module::const_named_metadata_iterator I = M.named_metadata_begin(),
516 E = M.named_metadata_end();
517 I != E; ++I)
Dan Gohman2637cc12010-07-21 23:38:33 +0000518 EnumerateNamedMDNode(I);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000519}
520
Devang Patel99ff5a82010-01-09 00:30:14 +0000521void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel99ff5a82010-01-09 00:30:14 +0000522 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohmand3d2bbe2010-08-24 02:01:24 +0000523 EnumerateMetadata(MD->getOperand(i));
Devang Patel99ff5a82010-01-09 00:30:14 +0000524}
525
Dan Gohmanc828c542010-08-24 02:24:03 +0000526/// EnumerateMDNodeOperands - Enumerate all non-function-local values
527/// and types referenced by the given MDNode.
528void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
529 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000530 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +0000531 if (!MD)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000532 continue;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000533 assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000534 EnumerateMetadata(MD);
Dan Gohmanc828c542010-08-24 02:24:03 +0000535 }
536}
537
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000538void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
539 assert(
540 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
541 "Invalid metadata kind");
Dan Gohmanc828c542010-08-24 02:24:03 +0000542
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000543 // Insert a dummy ID to block the co-recursive call to
544 // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
545 //
546 // Return early if there's already an ID.
547 if (!MDValueMap.insert(std::make_pair(MD, 0)).second)
Devang Patel05eb6172009-08-04 06:00:18 +0000548 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000549
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000550 // Visit operands first to minimize RAUW.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000551 if (auto *N = dyn_cast<MDNode>(MD))
Dan Gohmanc828c542010-08-24 02:24:03 +0000552 EnumerateMDNodeOperands(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000553 else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
554 EnumerateValue(C->getValue());
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000555
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +0000556 HasMDString |= isa<MDString>(MD);
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000557 HasDILocation |= isa<DILocation>(MD);
558 HasGenericDINode |= isa<GenericDINode>(MD);
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +0000559
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000560 // Replace the dummy ID inserted above with the correct one. MDValueMap may
561 // have changed by inserting operands, so we need a fresh lookup here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000562 MDs.push_back(MD);
563 MDValueMap[MD] = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000564}
565
566/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000567/// information reachable from the metadata.
568void ValueEnumerator::EnumerateFunctionLocalMetadata(
569 const LocalAsMetadata *Local) {
Dan Gohmanc828c542010-08-24 02:24:03 +0000570 // Check to see if it's already in!
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000571 unsigned &MDValueID = MDValueMap[Local];
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000572 if (MDValueID)
Dan Gohmanc828c542010-08-24 02:24:03 +0000573 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000574
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000575 MDs.push_back(Local);
576 MDValueID = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000577
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000578 EnumerateValue(Local->getValue());
Dan Gohmanc828c542010-08-24 02:24:03 +0000579
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000580 // Also, collect all function-local metadata for easy access.
581 FunctionLocalMDs.push_back(Local);
Devang Patel05eb6172009-08-04 06:00:18 +0000582}
583
Victor Hernandez572218b2010-01-14 19:54:11 +0000584void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattner8dace892009-12-31 00:51:46 +0000585 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000586 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
Devang Patel05eb6172009-08-04 06:00:18 +0000587
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000588 // Check to see if it's already in!
589 unsigned &ValueID = ValueMap[V];
590 if (ValueID) {
591 // Increment use count.
592 Values[ValueID-1].second++;
593 return;
594 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000595
David Majnemerdad0a642014-06-27 18:19:56 +0000596 if (auto *GO = dyn_cast<GlobalObject>(V))
597 if (const Comdat *C = GO->getComdat())
598 Comdats.insert(C);
599
Chris Lattner9ee48362007-05-06 01:00:28 +0000600 // Enumerate the type of this value.
601 EnumerateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000602
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000603 if (const Constant *C = dyn_cast<Constant>(V)) {
604 if (isa<GlobalValue>(C)) {
605 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner9ee48362007-05-06 01:00:28 +0000606 } else if (C->getNumOperands()) {
607 // If a constant has operands, enumerate them. This makes sure that if a
608 // constant has uses (for example an array of const ints), that they are
609 // inserted also.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000610
Chris Lattner9ee48362007-05-06 01:00:28 +0000611 // We prefer to enumerate them with values before we enumerate the user
612 // itself. This makes it more likely that we can avoid forward references
613 // in the reader. We know that there can be no cycles in the constants
614 // graph that don't go through a global variable.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000615 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
616 I != E; ++I)
Chris Lattneraa99c942009-11-01 01:27:45 +0000617 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandez572218b2010-01-14 19:54:11 +0000618 EnumerateValue(*I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000619
Chris Lattner9ee48362007-05-06 01:00:28 +0000620 // Finally, add the value. Doing this could make the ValueID reference be
621 // dangling, don't reuse it.
622 Values.push_back(std::make_pair(V, 1U));
623 ValueMap[V] = Values.size();
624 return;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000625 }
626 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000627
Chris Lattner9ee48362007-05-06 01:00:28 +0000628 // Add the value.
629 Values.push_back(std::make_pair(V, 1U));
630 ValueID = Values.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000631}
632
633
Chris Lattner229907c2011-07-18 04:54:35 +0000634void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000635 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000636
Rafael Espindola337a1b22011-04-06 16:49:37 +0000637 // We've already seen this type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000638 if (*TypeID)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000639 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000640
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000641 // If it is a non-anonymous struct, mark the type as being visited so that we
642 // don't recursively visit it. This is safe because we allow forward
643 // references of these in the bitcode reader.
Chris Lattner229907c2011-07-18 04:54:35 +0000644 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner335d3992011-08-12 18:06:37 +0000645 if (!STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000646 *TypeID = ~0U;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000647
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000648 // Enumerate all of the subtypes before we enumerate this type. This ensures
649 // that the type will be enumerated in an order that can be directly built.
Rafael Espindolaffbfcf22014-11-24 20:44:36 +0000650 for (Type *SubTy : Ty->subtypes())
651 EnumerateType(SubTy);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000652
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000653 // Refresh the TypeID pointer in case the table rehashed.
654 TypeID = &TypeMap[Ty];
Joe Abbey2ad8df22012-11-25 15:23:39 +0000655
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000656 // Check to see if we got the pointer another way. This can happen when
657 // enumerating recursive types that hit the base case deeper than they start.
658 //
659 // If this is actually a struct that we are treating as forward ref'able,
660 // then emit the definition now that all of its contents are available.
661 if (*TypeID && *TypeID != ~0U)
662 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000663
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000664 // Add this type now that its contents are all happily enumerated.
665 Types.push_back(Ty);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000666
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000667 *TypeID = Types.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000668}
669
Chris Lattner76fd90f2007-05-06 08:35:19 +0000670// Enumerate the types for the specified value. If the value is a constant,
671// walk through it, enumerating the types of the constant.
Victor Hernandez572218b2010-01-14 19:54:11 +0000672void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner76fd90f2007-05-06 08:35:19 +0000673 EnumerateType(V->getType());
Joe Abbey2ad8df22012-11-25 15:23:39 +0000674
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000675 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
676 assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
677 "Function-local metadata should be left for later");
Chris Lattner76fd90f2007-05-06 08:35:19 +0000678
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000679 EnumerateMetadata(MD->getMetadata());
680 return;
681 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000682
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000683 const Constant *C = dyn_cast<Constant>(V);
684 if (!C)
685 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000686
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000687 // If this constant is already enumerated, ignore it, we know its type must
688 // be enumerated.
689 if (ValueMap.count(C))
690 return;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000691
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000692 // This constant may have operands, make sure to enumerate the types in
693 // them.
694 for (unsigned i = 0, e = C->getNumOperands(); i != e; ++i) {
695 const Value *Op = C->getOperand(i);
696
697 // Don't enumerate basic blocks here, this happens as operands to
698 // blockaddress.
699 if (isa<BasicBlock>(Op))
700 continue;
701
702 EnumerateOperandType(Op);
703 }
Chris Lattner76fd90f2007-05-06 08:35:19 +0000704}
705
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000706void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000707 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000708
Chris Lattnere4bbad62007-05-03 22:46:43 +0000709 // Do a lookup.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000710 unsigned &Entry = AttributeMap[PAL];
Chris Lattnere4bbad62007-05-03 22:46:43 +0000711 if (Entry == 0) {
712 // Never saw this before, add it.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000713 Attribute.push_back(PAL);
714 Entry = Attribute.size();
Chris Lattnere4bbad62007-05-03 22:46:43 +0000715 }
Bill Wendling51f612e2013-02-10 23:06:02 +0000716
717 // Do lookups for all attribute groups.
718 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
719 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendling92ed7002013-02-11 22:33:26 +0000720 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling51f612e2013-02-10 23:06:02 +0000721 if (Entry == 0) {
Bill Wendling92ed7002013-02-11 22:33:26 +0000722 AttributeGroups.push_back(AS);
723 Entry = AttributeGroups.size();
Bill Wendling51f612e2013-02-10 23:06:02 +0000724 }
725 }
Chris Lattnere4bbad62007-05-03 22:46:43 +0000726}
727
Chad Rosier6a11b642011-06-03 17:02:19 +0000728void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewyckya72e1af2010-02-25 08:30:17 +0000729 InstructionCount = 0;
Chris Lattnere6e364c2007-04-26 05:53:54 +0000730 NumModuleValues = Values.size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000731 NumModuleMDs = MDs.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732
Chris Lattner5f640b92007-04-26 03:50:57 +0000733 // Adding function arguments to the value table.
Dan Gohman9a54c172010-07-16 22:58:39 +0000734 for (Function::const_arg_iterator I = F.arg_begin(), E = F.arg_end();
735 I != E; ++I)
Chris Lattner5f640b92007-04-26 03:50:57 +0000736 EnumerateValue(I);
737
Chris Lattnere6e364c2007-04-26 05:53:54 +0000738 FirstFuncConstantID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000739
Chris Lattner5f640b92007-04-26 03:50:57 +0000740 // Add all function-level constants to the value table.
741 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
742 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I)
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000743 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
Chris Lattner5f640b92007-04-26 03:50:57 +0000744 OI != E; ++OI) {
745 if ((isa<Constant>(*OI) && !isa<GlobalValue>(*OI)) ||
746 isa<InlineAsm>(*OI))
747 EnumerateValue(*OI);
748 }
Chris Lattner7c37b012007-04-26 04:42:16 +0000749 BasicBlocks.push_back(BB);
Chris Lattner6be58c62007-05-03 22:18:21 +0000750 ValueMap[BB] = BasicBlocks.size();
Chris Lattner5f640b92007-04-26 03:50:57 +0000751 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000752
Chris Lattner430e80d2007-05-04 05:21:47 +0000753 // Optimize the constant layout.
754 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000755
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000756 // Add the function's parameter attributes so they are available for use in
757 // the function's instruction.
Devang Patel4c758ea2008-09-25 21:00:45 +0000758 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000759
Chris Lattnere6e364c2007-04-26 05:53:54 +0000760 FirstInstID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000761
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000762 SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
Chris Lattner5f640b92007-04-26 03:50:57 +0000763 // Add all of the instructions.
764 for (Function::const_iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000765 for (BasicBlock::const_iterator I = BB->begin(), E = BB->end(); I!=E; ++I) {
Victor Hernandezcad73282010-01-13 19:36:16 +0000766 for (User::const_op_iterator OI = I->op_begin(), E = I->op_end();
767 OI != E; ++OI) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000768 if (auto *MD = dyn_cast<MetadataAsValue>(&*OI))
769 if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
Victor Hernandezd44ee352010-02-04 01:13:08 +0000770 // Enumerate metadata after the instructions they might refer to.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000771 FnLocalMDVector.push_back(Local);
Dan Gohmanc828c542010-08-24 02:24:03 +0000772 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000773
Benjamin Kramerccce8ba2010-01-05 13:12:22 +0000774 if (!I->getType()->isVoidTy())
Chris Lattner5f640b92007-04-26 03:50:57 +0000775 EnumerateValue(I);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000776 }
777 }
Victor Hernandezd44ee352010-02-04 01:13:08 +0000778
779 // Add all of the function-local metadata.
Devang Pateldf84e8b2010-06-02 23:05:04 +0000780 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohmanc828c542010-08-24 02:24:03 +0000781 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000782}
783
Chad Rosier6a11b642011-06-03 17:02:19 +0000784void ValueEnumerator::purgeFunction() {
Chris Lattner5f640b92007-04-26 03:50:57 +0000785 /// Remove purged values from the ValueMap.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000786 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner5f640b92007-04-26 03:50:57 +0000787 ValueMap.erase(Values[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000788 for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
789 MDValueMap.erase(MDs[i]);
Chris Lattner7c37b012007-04-26 04:42:16 +0000790 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
791 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000792
Chris Lattnere6e364c2007-04-26 05:53:54 +0000793 Values.resize(NumModuleValues);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000794 MDs.resize(NumModuleMDs);
Chris Lattner7c37b012007-04-26 04:42:16 +0000795 BasicBlocks.clear();
Dan Gohman22161da2010-08-25 17:11:16 +0000796 FunctionLocalMDs.clear();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000797}
Chris Lattnerf540d742009-10-28 05:24:40 +0000798
799static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
800 DenseMap<const BasicBlock*, unsigned> &IDMap) {
801 unsigned Counter = 0;
802 for (Function::const_iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
803 IDMap[BB] = ++Counter;
804}
805
806/// getGlobalBasicBlockID - This returns the function-specific ID for the
807/// specified basic block. This is relatively expensive information, so it
808/// should only be used by rare constructs such as address-of-label.
809unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
810 unsigned &Idx = GlobalBasicBlockIDs[BB];
811 if (Idx != 0)
Chris Lattneraa99c942009-11-01 01:27:45 +0000812 return Idx-1;
Chris Lattnerf540d742009-10-28 05:24:40 +0000813
814 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
815 return getGlobalBasicBlockID(BB);
816}
David Blaikie7b028102015-02-25 00:51:52 +0000817
818uint64_t ValueEnumerator::computeBitsRequiredForTypeIndicies() const {
819 return Log2_32_Ceil(getTypes().size() + 1);
820}