blob: 08b5e45703aba5f8a9f62e92637d4cd8a423bec5 [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) {
Vedant Kumar3a63fb32015-12-19 08:52:49 +000090 for (const Use &U : F.operands())
91 if (!isa<GlobalValue>(U.get()))
92 orderValue(U.get(), OM);
Peter Collingbourne51d2de72014-12-03 02:08:38 +000093 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +000094 OM.LastGlobalConstantID = OM.size();
95
96 // Initializers of GlobalValues are processed in
97 // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
98 // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
99 // by giving IDs in reverse order.
100 //
101 // Since GlobalValues never reference each other directly (just through
102 // initializers), their relative IDs only matter for determining order of
103 // uses in their initializers.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000104 for (const Function &F : M)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000105 orderValue(&F, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000106 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000107 orderValue(&A, OM);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000108 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000109 orderValue(&G, OM);
110 OM.LastGlobalValueID = OM.size();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000111
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000112 for (const Function &F : M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000113 if (F.isDeclaration())
114 continue;
115 // Here we need to match the union of ValueEnumerator::incorporateFunction()
116 // and WriteFunction(). Basic blocks are implicitly declared before
117 // anything else (by declaring their size).
118 for (const BasicBlock &BB : F)
119 orderValue(&BB, OM);
120 for (const Argument &A : F.args())
121 orderValue(&A, OM);
122 for (const BasicBlock &BB : F)
123 for (const Instruction &I : BB)
124 for (const Value *Op : I.operands())
125 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
126 isa<InlineAsm>(*Op))
127 orderValue(Op, OM);
128 for (const BasicBlock &BB : F)
129 for (const Instruction &I : BB)
130 orderValue(&I, OM);
131 }
132 return OM;
133}
134
135static void predictValueUseListOrderImpl(const Value *V, const Function *F,
136 unsigned ID, const OrderMap &OM,
137 UseListOrderStack &Stack) {
138 // Predict use-list order for this one.
139 typedef std::pair<const Use *, unsigned> Entry;
140 SmallVector<Entry, 64> List;
141 for (const Use &U : V->uses())
142 // Check if this user will be serialized.
143 if (OM.lookup(U.getUser()).first)
144 List.push_back(std::make_pair(&U, List.size()));
145
146 if (List.size() < 2)
147 // We may have lost some users.
148 return;
149
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000150 bool IsGlobalValue = OM.isGlobalValue(ID);
151 std::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000152 const Use *LU = L.first;
153 const Use *RU = R.first;
Duncan P. N. Exon Smith3f0fc7b2014-07-29 01:13:56 +0000154 if (LU == RU)
155 return false;
156
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000157 auto LID = OM.lookup(LU->getUser()).first;
158 auto RID = OM.lookup(RU->getUser()).first;
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000159
160 // Global values are processed in reverse order.
161 //
162 // Moreover, initializers of GlobalValues are set *after* all the globals
163 // have been read (despite having earlier IDs). Rather than awkwardly
164 // modeling this behaviour here, orderModule() has assigned IDs to
165 // initializers of GlobalValues before GlobalValues themselves.
166 if (OM.isGlobalValue(LID) && OM.isGlobalValue(RID))
167 return LID < RID;
168
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000169 // If ID is 4, then expect: 7 6 5 1 2 3.
170 if (LID < RID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000171 if (RID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000172 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
173 return true;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000174 return false;
175 }
176 if (RID < LID) {
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000177 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000178 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
179 return false;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000180 return true;
181 }
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000182
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000183 // LID and RID are equal, so we have different operands of the same user.
184 // Assume operands are added in order for all instructions.
Duncan P. N. Exon Smithab6adeb2014-07-31 18:33:12 +0000185 if (LID <= ID)
Duncan P. N. Exon Smith3cbca202014-07-30 01:22:16 +0000186 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
187 return LU->getOperandNo() < RU->getOperandNo();
188 return LU->getOperandNo() > RU->getOperandNo();
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000189 });
190
191 if (std::is_sorted(
192 List.begin(), List.end(),
193 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
194 // Order is already correct.
195 return;
196
197 // Store the shuffle.
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000198 Stack.emplace_back(V, F, List.size());
199 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
Duncan P. N. Exon Smithf849ace2014-07-28 22:41:50 +0000200 for (size_t I = 0, E = List.size(); I != E; ++I)
Duncan P. N. Exon Smithd7a281a2014-07-29 16:58:18 +0000201 Stack.back().Shuffle[I] = List[I].second;
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000202}
203
204static void predictValueUseListOrder(const Value *V, const Function *F,
205 OrderMap &OM, UseListOrderStack &Stack) {
206 auto &IDPair = OM[V];
207 assert(IDPair.first && "Unmapped value");
208 if (IDPair.second)
209 // Already predicted.
210 return;
211
212 // Do the actual prediction.
213 IDPair.second = true;
214 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
215 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
216
217 // Recursive descent into constants.
218 if (const Constant *C = dyn_cast<Constant>(V))
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000219 if (C->getNumOperands()) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000220 for (const Value *Op : C->operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000221 if (isa<Constant>(Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000222 predictValueUseListOrder(Op, F, OM, Stack);
223}
224
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000225static UseListOrderStack predictUseListOrder(const Module &M) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000226 OrderMap OM = orderModule(M);
227
228 // Use-list orders need to be serialized after all the users have been added
229 // to a value, or else the shuffles will be incomplete. Store them per
230 // function in a stack.
231 //
232 // Aside from function order, the order of values doesn't matter much here.
233 UseListOrderStack Stack;
234
235 // We want to visit the functions backward now so we can list function-local
236 // constants in the last Function they're used in. Module-level constants
237 // have already been visited above.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000238 for (auto I = M.rbegin(), E = M.rend(); I != E; ++I) {
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000239 const Function &F = *I;
240 if (F.isDeclaration())
241 continue;
242 for (const BasicBlock &BB : F)
243 predictValueUseListOrder(&BB, &F, OM, Stack);
244 for (const Argument &A : F.args())
245 predictValueUseListOrder(&A, &F, OM, Stack);
246 for (const BasicBlock &BB : F)
247 for (const Instruction &I : BB)
248 for (const Value *Op : I.operands())
Duncan P. N. Exon Smithc69b5162014-07-30 17:51:09 +0000249 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000250 predictValueUseListOrder(Op, &F, OM, Stack);
251 for (const BasicBlock &BB : F)
252 for (const Instruction &I : BB)
253 predictValueUseListOrder(&I, &F, OM, Stack);
254 }
255
256 // Visit globals last, since the module-level use-list block will be seen
257 // before the function bodies are processed.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000258 for (const GlobalVariable &G : M.globals())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000259 predictValueUseListOrder(&G, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000260 for (const Function &F : M)
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000261 predictValueUseListOrder(&F, nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000262 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000263 predictValueUseListOrder(&A, nullptr, OM, Stack);
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 if (G.hasInitializer())
266 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000267 for (const GlobalAlias &A : M.aliases())
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000268 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000269 for (const Function &F : M) {
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000270 for (const Use &U : F.operands())
271 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
Peter Collingbourne51d2de72014-12-03 02:08:38 +0000272 }
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000273
274 return Stack;
275}
276
Duncan Sandse6beec62012-11-13 12:59:33 +0000277static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
278 return V.first->getType()->isIntOrIntVectorTy();
Chris Lattner430e80d2007-05-04 05:21:47 +0000279}
280
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000281ValueEnumerator::ValueEnumerator(const Module &M,
282 bool ShouldPreserveUseListOrder)
Duncan P. N. Exon Smithfc811002016-03-25 15:22:27 +0000283 : HasMDString(false),
284 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000285 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith1f66c852014-07-28 21:19:41 +0000286 UseListOrders = predictUseListOrder(M);
287
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000288 // Enumerate the global variables.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000289 for (const GlobalVariable &GV : M.globals())
290 EnumerateValue(&GV);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000291
292 // Enumerate the functions.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000293 for (const Function & F : M) {
294 EnumerateValue(&F);
295 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000296 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000297
Chris Lattner44c17072007-04-26 02:46:40 +0000298 // Enumerate the aliases.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000299 for (const GlobalAlias &GA : M.aliases())
300 EnumerateValue(&GA);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000301
Chris Lattner430e80d2007-05-04 05:21:47 +0000302 // Remember what is the cutoff between globalvalue's and other constants.
303 unsigned FirstConstant = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000304
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000305 // Enumerate the global variable initializers.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000306 for (const GlobalVariable &GV : M.globals())
307 if (GV.hasInitializer())
308 EnumerateValue(GV.getInitializer());
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000309
Chris Lattner44c17072007-04-26 02:46:40 +0000310 // Enumerate the aliasees.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000311 for (const GlobalAlias &GA : M.aliases())
312 EnumerateValue(GA.getAliasee());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000313
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000314 // Enumerate any optional Function data.
Yaron Keren4c20deb2015-06-12 20:18:20 +0000315 for (const Function &F : M)
Vedant Kumar3a63fb32015-12-19 08:52:49 +0000316 for (const Use &U : F.operands())
317 EnumerateValue(U.get());
David Majnemer7fddecc2015-06-17 20:52:32 +0000318
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000319 // Enumerate the metadata type.
320 //
321 // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
322 // only encodes the metadata type when it's used as a value.
323 EnumerateType(Type::getMetadataTy(M.getContext()));
324
Joe Abbeybc6f4ba2013-04-01 02:28:07 +0000325 // Insert constants and metadata that are named at module level into the slot
Devang Patelfcfee0f2010-01-07 19:39:36 +0000326 // pool so that the module symbol table can refer to them...
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000327 EnumerateValueSymbolTable(M.getValueSymbolTable());
Dan Gohman2637cc12010-07-21 23:38:33 +0000328 EnumerateNamedMetadata(M);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000329
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000330 SmallVector<std::pair<unsigned, MDNode *>, 8> MDs;
Chris Lattner8dace892009-12-31 00:51:46 +0000331
Chris Lattner5f640b92007-04-26 03:50:57 +0000332 // Enumerate types used by function bodies and argument lists.
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000333 for (const Function &F : M) {
Rafael Espindola087d6272014-06-17 03:00:40 +0000334 for (const Argument &A : F.args())
335 EnumerateType(A.getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000336
Duncan P. N. Exon Smith3d4cd752015-04-24 22:04:41 +0000337 // Enumerate metadata attached to this function.
338 F.getAllMetadata(MDs);
339 for (const auto &I : MDs)
340 EnumerateMetadata(I.second);
341
Rafael Espindola087d6272014-06-17 03:00:40 +0000342 for (const BasicBlock &BB : F)
343 for (const Instruction &I : BB) {
344 for (const Use &Op : I.operands()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000345 auto *MD = dyn_cast<MetadataAsValue>(&Op);
346 if (!MD) {
347 EnumerateOperandType(Op);
348 continue;
349 }
350
351 // Local metadata is enumerated during function-incorporation.
352 if (isa<LocalAsMetadata>(MD->getMetadata()))
353 continue;
354
355 EnumerateMetadata(MD->getMetadata());
Victor Hernandez572218b2010-01-14 19:54:11 +0000356 }
Rafael Espindola087d6272014-06-17 03:00:40 +0000357 EnumerateType(I.getType());
358 if (const CallInst *CI = dyn_cast<CallInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000359 EnumerateAttributes(CI->getAttributes());
Rafael Espindola087d6272014-06-17 03:00:40 +0000360 else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I))
Devang Patel4c758ea2008-09-25 21:00:45 +0000361 EnumerateAttributes(II->getAttributes());
Devang Patelaf206b82009-09-18 19:26:43 +0000362
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000363 // Enumerate metadata attached with this instruction.
Devang Patel6da5dbf2009-10-22 18:55:16 +0000364 MDs.clear();
Rafael Espindola087d6272014-06-17 03:00:40 +0000365 I.getAllMetadataOtherThanDebugLoc(MDs);
Chris Lattner2f2aa2b2009-12-28 23:41:32 +0000366 for (unsigned i = 0, e = MDs.size(); i != e; ++i)
Duncan P. N. Exon Smithde36e802014-11-11 21:30:22 +0000367 EnumerateMetadata(MDs[i].second);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000368
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000369 // Don't enumerate the location directly -- it has a special record
370 // type -- but enumerate its operands.
Duncan P. N. Exon Smitha9308c42015-04-29 16:38:44 +0000371 if (DILocation *L = I.getDebugLoc())
Duncan P. N. Exon Smithab659fb32015-03-30 19:40:05 +0000372 EnumerateMDNodeOperands(L);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000373 }
374 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000375
Chris Lattner430e80d2007-05-04 05:21:47 +0000376 // Optimize constant ordering.
377 OptimizeConstants(FirstConstant, Values.size());
Rafael Espindola337a1b22011-04-06 16:49:37 +0000378}
379
Devang Patelaf206b82009-09-18 19:26:43 +0000380unsigned ValueEnumerator::getInstructionID(const Instruction *Inst) const {
381 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000382 assert(I != InstructionMap.end() && "Instruction is not mapped!");
Dan Gohman1f4b0282010-08-25 17:09:50 +0000383 return I->second;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000384}
Devang Patelaf206b82009-09-18 19:26:43 +0000385
David Majnemerdad0a642014-06-27 18:19:56 +0000386unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
387 unsigned ComdatID = Comdats.idFor(C);
388 assert(ComdatID && "Comdat not found!");
389 return ComdatID;
390}
391
Devang Patelaf206b82009-09-18 19:26:43 +0000392void ValueEnumerator::setInstructionID(const Instruction *I) {
393 InstructionMap[I] = InstructionCount++;
394}
395
Devang Patel05eb6172009-08-04 06:00:18 +0000396unsigned ValueEnumerator::getValueID(const Value *V) const {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000397 if (auto *MD = dyn_cast<MetadataAsValue>(V))
398 return getMetadataID(MD->getMetadata());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000399
Devang Patel05eb6172009-08-04 06:00:18 +0000400 ValueMapType::const_iterator I = ValueMap.find(V);
401 assert(I != ValueMap.end() && "Value not in slotcalculator!");
402 return I->second-1;
403}
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000404
Yaron Kereneb2a2542016-01-29 20:50:44 +0000405LLVM_DUMP_METHOD void ValueEnumerator::dump() const {
Chad Rosier78037a92011-12-07 20:44:46 +0000406 print(dbgs(), ValueMap, "Default");
407 dbgs() << '\n';
Teresa Johnson61b406e2015-12-29 23:00:22 +0000408 print(dbgs(), MetadataMap, "MetaData");
Chad Rosier78037a92011-12-07 20:44:46 +0000409 dbgs() << '\n';
410}
411
412void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
413 const char *Name) const {
414
415 OS << "Map Name: " << Name << "\n";
416 OS << "Size: " << Map.size() << "\n";
417 for (ValueMapType::const_iterator I = Map.begin(),
418 E = Map.end(); I != E; ++I) {
419
420 const Value *V = I->first;
421 if (V->hasName())
422 OS << "Value: " << V->getName();
423 else
424 OS << "Value: [null]\n";
425 V->dump();
426
427 OS << " Uses(" << std::distance(V->use_begin(),V->use_end()) << "):";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000428 for (const Use &U : V->uses()) {
429 if (&U != &*V->use_begin())
Chad Rosier78037a92011-12-07 20:44:46 +0000430 OS << ",";
Chandler Carruthcdf47882014-03-09 03:16:01 +0000431 if(U->hasName())
432 OS << " " << U->getName();
Chad Rosier78037a92011-12-07 20:44:46 +0000433 else
434 OS << " [null]";
435
436 }
437 OS << "\n\n";
438 }
439}
440
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000441void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
442 const char *Name) const {
443
444 OS << "Map Name: " << Name << "\n";
445 OS << "Size: " << Map.size() << "\n";
446 for (auto I = Map.begin(), E = Map.end(); I != E; ++I) {
447 const Metadata *MD = I->first;
448 OS << "Metadata: slot = " << I->second << "\n";
Nick Lewyckyee0a3a72014-12-17 01:52:08 +0000449 MD->print(OS);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000450 }
451}
452
Chris Lattner430e80d2007-05-04 05:21:47 +0000453/// OptimizeConstants - Reorder constant pool for denser encoding.
454void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
455 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000456
Duncan P. N. Exon Smith458593a2015-04-14 23:45:11 +0000457 if (ShouldPreserveUseListOrder)
Duncan P. N. Exon Smith15eb0ab2014-07-25 16:13:16 +0000458 // Optimizing constants makes the use-list order difficult to predict.
459 // Disable it for now when trying to preserve the order.
460 return;
461
Benjamin Kramer3a377bc2014-03-01 11:47:00 +0000462 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
463 [this](const std::pair<const Value *, unsigned> &LHS,
464 const std::pair<const Value *, unsigned> &RHS) {
465 // Sort by plane.
466 if (LHS.first->getType() != RHS.first->getType())
467 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
468 // Then by frequency.
469 return LHS.second > RHS.second;
470 });
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000471
Duncan Sandse6beec62012-11-13 12:59:33 +0000472 // Ensure that integer and vector of integer constants are at the start of the
473 // constant pool. This is important so that GEP structure indices come before
474 // gep constant exprs.
Duncan P. N. Exon Smithbdde9e12016-03-25 02:20:28 +0000475 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
476 isIntOrIntVectorValue);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000477
Chris Lattner430e80d2007-05-04 05:21:47 +0000478 // Rebuild the modified portion of ValueMap.
479 for (; CstStart != CstEnd; ++CstStart)
480 ValueMap[Values[CstStart].first] = CstStart+1;
481}
482
483
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000484/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
485/// table into the values table.
486void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000487 for (ValueSymbolTable::const_iterator VI = VST.begin(), VE = VST.end();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000488 VI != VE; ++VI)
489 EnumerateValue(VI->getValue());
490}
491
Rafael Espindola49e9bf82014-11-17 20:06:27 +0000492/// Insert all of the values referenced by named metadata in the specified
493/// module.
494void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000495 for (const auto &I : M.named_metadata())
496 EnumerateNamedMDNode(&I);
Devang Patelfcfee0f2010-01-07 19:39:36 +0000497}
498
Devang Patel99ff5a82010-01-09 00:30:14 +0000499void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
Devang Patel99ff5a82010-01-09 00:30:14 +0000500 for (unsigned i = 0, e = MD->getNumOperands(); i != e; ++i)
Dan Gohmand3d2bbe2010-08-24 02:01:24 +0000501 EnumerateMetadata(MD->getOperand(i));
Devang Patel99ff5a82010-01-09 00:30:14 +0000502}
503
Dan Gohmanc828c542010-08-24 02:24:03 +0000504/// EnumerateMDNodeOperands - Enumerate all non-function-local values
505/// and types referenced by the given MDNode.
506void ValueEnumerator::EnumerateMDNodeOperands(const MDNode *N) {
507 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000508 Metadata *MD = N->getOperand(i);
Duncan P. N. Exon Smith5c7006e2014-12-11 23:02:24 +0000509 if (!MD)
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000510 continue;
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000511 assert(!isa<LocalAsMetadata>(MD) && "MDNodes cannot be function-local");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000512 EnumerateMetadata(MD);
Dan Gohmanc828c542010-08-24 02:24:03 +0000513 }
514}
515
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000516void ValueEnumerator::EnumerateMetadata(const Metadata *MD) {
517 assert(
518 (isa<MDNode>(MD) || isa<MDString>(MD) || isa<ConstantAsMetadata>(MD)) &&
519 "Invalid metadata kind");
Dan Gohmanc828c542010-08-24 02:24:03 +0000520
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000521 // Insert a dummy ID to block the co-recursive call to
522 // EnumerateMDNodeOperands() from re-visiting MD in a cyclic graph.
523 //
524 // Return early if there's already an ID.
Teresa Johnson61b406e2015-12-29 23:00:22 +0000525 if (!MetadataMap.insert(std::make_pair(MD, 0)).second)
Devang Patel05eb6172009-08-04 06:00:18 +0000526 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000527
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000528 // Visit operands first to minimize RAUW.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000529 if (auto *N = dyn_cast<MDNode>(MD))
Dan Gohmanc828c542010-08-24 02:24:03 +0000530 EnumerateMDNodeOperands(N);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000531 else if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
532 EnumerateValue(C->getValue());
Duncan P. N. Exon Smithfc811002016-03-25 15:22:27 +0000533
534 HasMDString |= isa<MDString>(MD);
Duncan P. N. Exon Smith2fcf60e2015-01-12 22:30:34 +0000535
Teresa Johnson61b406e2015-12-29 23:00:22 +0000536 // Replace the dummy ID inserted above with the correct one. MetadataMap may
Duncan P. N. Exon Smith44e5b4e532014-10-21 22:27:47 +0000537 // have changed by inserting operands, so we need a fresh lookup here.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000538 MDs.push_back(MD);
Teresa Johnson61b406e2015-12-29 23:00:22 +0000539 MetadataMap[MD] = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000540}
541
542/// EnumerateFunctionLocalMetadataa - Incorporate function-local metadata
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000543/// information reachable from the metadata.
544void ValueEnumerator::EnumerateFunctionLocalMetadata(
545 const LocalAsMetadata *Local) {
Dan Gohmanc828c542010-08-24 02:24:03 +0000546 // Check to see if it's already in!
Teresa Johnson61b406e2015-12-29 23:00:22 +0000547 unsigned &MetadataID = MetadataMap[Local];
548 if (MetadataID)
Dan Gohmanc828c542010-08-24 02:24:03 +0000549 return;
Duncan P. N. Exon Smith60d87e72014-10-21 22:13:34 +0000550
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000551 MDs.push_back(Local);
Teresa Johnson61b406e2015-12-29 23:00:22 +0000552 MetadataID = MDs.size();
Dan Gohmanc828c542010-08-24 02:24:03 +0000553
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000554 EnumerateValue(Local->getValue());
Dan Gohmanc828c542010-08-24 02:24:03 +0000555
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000556 // Also, collect all function-local metadata for easy access.
557 FunctionLocalMDs.push_back(Local);
Devang Patel05eb6172009-08-04 06:00:18 +0000558}
559
Victor Hernandez572218b2010-01-14 19:54:11 +0000560void ValueEnumerator::EnumerateValue(const Value *V) {
Chris Lattner8dace892009-12-31 00:51:46 +0000561 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000562 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
Devang Patel05eb6172009-08-04 06:00:18 +0000563
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000564 // Check to see if it's already in!
565 unsigned &ValueID = ValueMap[V];
566 if (ValueID) {
567 // Increment use count.
568 Values[ValueID-1].second++;
569 return;
570 }
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000571
David Majnemerdad0a642014-06-27 18:19:56 +0000572 if (auto *GO = dyn_cast<GlobalObject>(V))
573 if (const Comdat *C = GO->getComdat())
574 Comdats.insert(C);
575
Chris Lattner9ee48362007-05-06 01:00:28 +0000576 // Enumerate the type of this value.
577 EnumerateType(V->getType());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000578
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000579 if (const Constant *C = dyn_cast<Constant>(V)) {
580 if (isa<GlobalValue>(C)) {
581 // Initializers for globals are handled explicitly elsewhere.
Chris Lattner9ee48362007-05-06 01:00:28 +0000582 } else if (C->getNumOperands()) {
583 // If a constant has operands, enumerate them. This makes sure that if a
584 // constant has uses (for example an array of const ints), that they are
585 // inserted also.
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000586
Chris Lattner9ee48362007-05-06 01:00:28 +0000587 // We prefer to enumerate them with values before we enumerate the user
588 // itself. This makes it more likely that we can avoid forward references
589 // in the reader. We know that there can be no cycles in the constants
590 // graph that don't go through a global variable.
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000591 for (User::const_op_iterator I = C->op_begin(), E = C->op_end();
592 I != E; ++I)
Chris Lattneraa99c942009-11-01 01:27:45 +0000593 if (!isa<BasicBlock>(*I)) // Don't enumerate BB operand to BlockAddress.
Victor Hernandez572218b2010-01-14 19:54:11 +0000594 EnumerateValue(*I);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000595
Chris Lattner9ee48362007-05-06 01:00:28 +0000596 // Finally, add the value. Doing this could make the ValueID reference be
597 // dangling, don't reuse it.
598 Values.push_back(std::make_pair(V, 1U));
599 ValueMap[V] = Values.size();
600 return;
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000601 }
602 }
Devang Patele059ba6e2009-07-23 01:07:34 +0000603
Chris Lattner9ee48362007-05-06 01:00:28 +0000604 // Add the value.
605 Values.push_back(std::make_pair(V, 1U));
606 ValueID = Values.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000607}
608
609
Chris Lattner229907c2011-07-18 04:54:35 +0000610void ValueEnumerator::EnumerateType(Type *Ty) {
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000611 unsigned *TypeID = &TypeMap[Ty];
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000612
Rafael Espindola337a1b22011-04-06 16:49:37 +0000613 // We've already seen this type.
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000614 if (*TypeID)
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000615 return;
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000616
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000617 // If it is a non-anonymous struct, mark the type as being visited so that we
618 // don't recursively visit it. This is safe because we allow forward
619 // references of these in the bitcode reader.
Chris Lattner229907c2011-07-18 04:54:35 +0000620 if (StructType *STy = dyn_cast<StructType>(Ty))
Chris Lattner335d3992011-08-12 18:06:37 +0000621 if (!STy->isLiteral())
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000622 *TypeID = ~0U;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000623
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000624 // Enumerate all of the subtypes before we enumerate this type. This ensures
625 // that the type will be enumerated in an order that can be directly built.
Rafael Espindolaffbfcf22014-11-24 20:44:36 +0000626 for (Type *SubTy : Ty->subtypes())
627 EnumerateType(SubTy);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000628
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000629 // Refresh the TypeID pointer in case the table rehashed.
630 TypeID = &TypeMap[Ty];
Joe Abbey2ad8df22012-11-25 15:23:39 +0000631
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000632 // Check to see if we got the pointer another way. This can happen when
633 // enumerating recursive types that hit the base case deeper than they start.
634 //
635 // If this is actually a struct that we are treating as forward ref'able,
636 // then emit the definition now that all of its contents are available.
637 if (*TypeID && *TypeID != ~0U)
638 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000639
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000640 // Add this type now that its contents are all happily enumerated.
641 Types.push_back(Ty);
Joe Abbey2ad8df22012-11-25 15:23:39 +0000642
Chris Lattnerb1ed91f2011-07-09 17:41:24 +0000643 *TypeID = Types.size();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000644}
645
Chris Lattner76fd90f2007-05-06 08:35:19 +0000646// Enumerate the types for the specified value. If the value is a constant,
647// walk through it, enumerating the types of the constant.
Victor Hernandez572218b2010-01-14 19:54:11 +0000648void ValueEnumerator::EnumerateOperandType(const Value *V) {
Chris Lattner76fd90f2007-05-06 08:35:19 +0000649 EnumerateType(V->getType());
Joe Abbey2ad8df22012-11-25 15:23:39 +0000650
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000651 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
652 assert(!isa<LocalAsMetadata>(MD->getMetadata()) &&
653 "Function-local metadata should be left for later");
Chris Lattner76fd90f2007-05-06 08:35:19 +0000654
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000655 EnumerateMetadata(MD->getMetadata());
656 return;
657 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000658
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000659 const Constant *C = dyn_cast<Constant>(V);
660 if (!C)
661 return;
Joe Abbey2ad8df22012-11-25 15:23:39 +0000662
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000663 // If this constant is already enumerated, ignore it, we know its type must
664 // be enumerated.
665 if (ValueMap.count(C))
666 return;
Nick Lewyckyb8f9b7a2009-05-10 20:57:05 +0000667
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000668 // This constant may have operands, make sure to enumerate the types in
669 // them.
Pete Cooper125ad172015-06-25 20:51:38 +0000670 for (const Value *Op : C->operands()) {
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000671 // Don't enumerate basic blocks here, this happens as operands to
672 // blockaddress.
673 if (isa<BasicBlock>(Op))
674 continue;
675
676 EnumerateOperandType(Op);
677 }
Chris Lattner76fd90f2007-05-06 08:35:19 +0000678}
679
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000680void ValueEnumerator::EnumerateAttributes(AttributeSet PAL) {
Chris Lattner8a923e72008-03-12 17:45:29 +0000681 if (PAL.isEmpty()) return; // null is always 0.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000682
Chris Lattnere4bbad62007-05-03 22:46:43 +0000683 // Do a lookup.
Bill Wendling7b5f4f32013-02-12 08:01:22 +0000684 unsigned &Entry = AttributeMap[PAL];
Chris Lattnere4bbad62007-05-03 22:46:43 +0000685 if (Entry == 0) {
686 // Never saw this before, add it.
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000687 Attribute.push_back(PAL);
688 Entry = Attribute.size();
Chris Lattnere4bbad62007-05-03 22:46:43 +0000689 }
Bill Wendling51f612e2013-02-10 23:06:02 +0000690
691 // Do lookups for all attribute groups.
692 for (unsigned i = 0, e = PAL.getNumSlots(); i != e; ++i) {
693 AttributeSet AS = PAL.getSlotAttributes(i);
Bill Wendling92ed7002013-02-11 22:33:26 +0000694 unsigned &Entry = AttributeGroupMap[AS];
Bill Wendling51f612e2013-02-10 23:06:02 +0000695 if (Entry == 0) {
Bill Wendling92ed7002013-02-11 22:33:26 +0000696 AttributeGroups.push_back(AS);
697 Entry = AttributeGroups.size();
Bill Wendling51f612e2013-02-10 23:06:02 +0000698 }
699 }
Chris Lattnere4bbad62007-05-03 22:46:43 +0000700}
701
Chad Rosier6a11b642011-06-03 17:02:19 +0000702void ValueEnumerator::incorporateFunction(const Function &F) {
Nick Lewyckya72e1af2010-02-25 08:30:17 +0000703 InstructionCount = 0;
Chris Lattnere6e364c2007-04-26 05:53:54 +0000704 NumModuleValues = Values.size();
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000705 NumModuleMDs = MDs.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000706
Chris Lattner5f640b92007-04-26 03:50:57 +0000707 // Adding function arguments to the value table.
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000708 for (const auto &I : F.args())
709 EnumerateValue(&I);
Chris Lattner5f640b92007-04-26 03:50:57 +0000710
Chris Lattnere6e364c2007-04-26 05:53:54 +0000711 FirstFuncConstantID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000712
Chris Lattner5f640b92007-04-26 03:50:57 +0000713 // Add all function-level constants to the value table.
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000714 for (const BasicBlock &BB : F) {
715 for (const Instruction &I : BB)
716 for (const Use &OI : I.operands()) {
717 if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))
718 EnumerateValue(OI);
Chris Lattner5f640b92007-04-26 03:50:57 +0000719 }
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000720 BasicBlocks.push_back(&BB);
721 ValueMap[&BB] = BasicBlocks.size();
Chris Lattner5f640b92007-04-26 03:50:57 +0000722 }
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000723
Chris Lattner430e80d2007-05-04 05:21:47 +0000724 // Optimize the constant layout.
725 OptimizeConstants(FirstFuncConstantID, Values.size());
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000726
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000727 // Add the function's parameter attributes so they are available for use in
728 // the function's instruction.
Devang Patel4c758ea2008-09-25 21:00:45 +0000729 EnumerateAttributes(F.getAttributes());
Duncan Sandsad0ea2d2007-11-27 13:23:08 +0000730
Chris Lattnere6e364c2007-04-26 05:53:54 +0000731 FirstInstID = Values.size();
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000732
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000733 SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
Chris Lattner5f640b92007-04-26 03:50:57 +0000734 // Add all of the instructions.
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000735 for (const BasicBlock &BB : F) {
736 for (const Instruction &I : BB) {
737 for (const Use &OI : I.operands()) {
738 if (auto *MD = dyn_cast<MetadataAsValue>(&OI))
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000739 if (auto *Local = dyn_cast<LocalAsMetadata>(MD->getMetadata()))
Victor Hernandezd44ee352010-02-04 01:13:08 +0000740 // Enumerate metadata after the instructions they might refer to.
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000741 FnLocalMDVector.push_back(Local);
Dan Gohmanc828c542010-08-24 02:24:03 +0000742 }
Joe Abbey2ad8df22012-11-25 15:23:39 +0000743
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000744 if (!I.getType()->isVoidTy())
745 EnumerateValue(&I);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000746 }
747 }
Victor Hernandezd44ee352010-02-04 01:13:08 +0000748
749 // Add all of the function-local metadata.
Devang Pateldf84e8b2010-06-02 23:05:04 +0000750 for (unsigned i = 0, e = FnLocalMDVector.size(); i != e; ++i)
Dan Gohmanc828c542010-08-24 02:24:03 +0000751 EnumerateFunctionLocalMetadata(FnLocalMDVector[i]);
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000752}
753
Chad Rosier6a11b642011-06-03 17:02:19 +0000754void ValueEnumerator::purgeFunction() {
Chris Lattner5f640b92007-04-26 03:50:57 +0000755 /// Remove purged values from the ValueMap.
Chris Lattnere6e364c2007-04-26 05:53:54 +0000756 for (unsigned i = NumModuleValues, e = Values.size(); i != e; ++i)
Chris Lattner5f640b92007-04-26 03:50:57 +0000757 ValueMap.erase(Values[i].first);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000758 for (unsigned i = NumModuleMDs, e = MDs.size(); i != e; ++i)
Teresa Johnson61b406e2015-12-29 23:00:22 +0000759 MetadataMap.erase(MDs[i]);
Chris Lattner7c37b012007-04-26 04:42:16 +0000760 for (unsigned i = 0, e = BasicBlocks.size(); i != e; ++i)
761 ValueMap.erase(BasicBlocks[i]);
Daniel Dunbar7d6781b2009-09-20 02:20:51 +0000762
Chris Lattnere6e364c2007-04-26 05:53:54 +0000763 Values.resize(NumModuleValues);
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000764 MDs.resize(NumModuleMDs);
Chris Lattner7c37b012007-04-26 04:42:16 +0000765 BasicBlocks.clear();
Dan Gohman22161da2010-08-25 17:11:16 +0000766 FunctionLocalMDs.clear();
Chris Lattnerc1d10d62007-04-22 06:24:45 +0000767}
Chris Lattnerf540d742009-10-28 05:24:40 +0000768
769static void IncorporateFunctionInfoGlobalBBIDs(const Function *F,
770 DenseMap<const BasicBlock*, unsigned> &IDMap) {
771 unsigned Counter = 0;
Duncan P. N. Exon Smith584af872015-10-13 03:26:19 +0000772 for (const BasicBlock &BB : *F)
773 IDMap[&BB] = ++Counter;
Chris Lattnerf540d742009-10-28 05:24:40 +0000774}
775
776/// getGlobalBasicBlockID - This returns the function-specific ID for the
777/// specified basic block. This is relatively expensive information, so it
778/// should only be used by rare constructs such as address-of-label.
779unsigned ValueEnumerator::getGlobalBasicBlockID(const BasicBlock *BB) const {
780 unsigned &Idx = GlobalBasicBlockIDs[BB];
781 if (Idx != 0)
Chris Lattneraa99c942009-11-01 01:27:45 +0000782 return Idx-1;
Chris Lattnerf540d742009-10-28 05:24:40 +0000783
784 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
785 return getGlobalBasicBlockID(BB);
786}
David Blaikie7b028102015-02-25 00:51:52 +0000787
788uint64_t ValueEnumerator::computeBitsRequiredForTypeIndicies() const {
789 return Log2_32_Ceil(getTypes().size() + 1);
790}