LLVM 24.0.0git
ValueEnumerator.cpp
Go to the documentation of this file.
1//===- ValueEnumerator.cpp - Number values and types for bitcode writer ---===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the ValueEnumerator class.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ValueEnumerator.h"
15#include "llvm/Config/llvm-config.h"
16#include "llvm/IR/Argument.h"
17#include "llvm/IR/BasicBlock.h"
18#include "llvm/IR/Constant.h"
21#include "llvm/IR/Function.h"
23#include "llvm/IR/GlobalAlias.h"
24#include "llvm/IR/GlobalIFunc.h"
26#include "llvm/IR/GlobalValue.h"
28#include "llvm/IR/Instruction.h"
30#include "llvm/IR/Metadata.h"
31#include "llvm/IR/Module.h"
32#include "llvm/IR/Operator.h"
33#include "llvm/IR/Type.h"
34#include "llvm/IR/Use.h"
35#include "llvm/IR/User.h"
36#include "llvm/IR/Value.h"
40#include "llvm/Support/Debug.h"
43#include <algorithm>
44#include <cstddef>
45#include <iterator>
46#include <tuple>
47
48using namespace llvm;
49
50namespace {
51
52struct OrderMap {
53 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
54 unsigned LastGlobalValueID = 0;
55
56 OrderMap() = default;
57
58 bool isGlobalValue(unsigned ID) const {
59 return ID <= LastGlobalValueID;
60 }
61
62 unsigned size() const { return IDs.size(); }
63 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
64
65 std::pair<unsigned, bool> lookup(const Value *V) const {
66 return IDs.lookup(V);
67 }
68
69 void index(const Value *V) {
70 // Explicitly sequence get-size and insert-value operations to avoid UB.
71 unsigned ID = IDs.size() + 1;
72 IDs[V].first = ID;
73 }
74};
75
76} // end anonymous namespace
77
78static void orderValue(const Value *V, OrderMap &OM) {
79 if (OM.lookup(V).first)
80 return;
81
82 if (const Constant *C = dyn_cast<Constant>(V)) {
83 if (C->getNumOperands()) {
84 for (const Value *Op : C->operands())
86 orderValue(Op, OM);
87 if (auto *CE = dyn_cast<ConstantExpr>(C))
88 if (CE->getOpcode() == Instruction::ShuffleVector)
89 orderValue(CE->getShuffleMaskForBitcode(), OM);
90 }
91 }
92
93 // Note: we cannot cache this lookup above, since inserting into the map
94 // changes the map's size, and thus affects the other IDs.
95 OM.index(V);
96}
97
98static OrderMap orderModule(const Module &M) {
99 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
100 // and ValueEnumerator::incorporateFunction().
101 OrderMap OM;
102
103 // Initializers of GlobalValues are processed in
104 // BitcodeReader::ResolveGlobalAndAliasInits(). Match the order there rather
105 // than ValueEnumerator, and match the code in predictValueUseListOrderImpl()
106 // by giving IDs in reverse order.
107 //
108 // Since GlobalValues never reference each other directly (just through
109 // initializers), their relative IDs only matter for determining order of
110 // uses in their initializers.
111 for (const GlobalVariable &G : reverse(M.globals()))
112 orderValue(&G, OM);
113 for (const GlobalAlias &A : reverse(M.aliases()))
114 orderValue(&A, OM);
115 for (const GlobalIFunc &I : reverse(M.ifuncs()))
116 orderValue(&I, OM);
117 for (const Function &F : reverse(M))
118 orderValue(&F, OM);
119 OM.LastGlobalValueID = OM.size();
120
121 auto orderConstantValue = [&OM](const Value *V) {
123 orderValue(V, OM);
124 };
125
126 for (const Function &F : M) {
127 if (F.isDeclaration())
128 continue;
129 // Here we need to match the union of ValueEnumerator::incorporateFunction()
130 // and WriteFunction(). Basic blocks are implicitly declared before
131 // anything else (by declaring their size).
132 for (const BasicBlock &BB : F)
133 orderValue(&BB, OM);
134
135 // Metadata used by instructions is decoded before the actual instructions,
136 // so visit any constants used by it beforehand.
137 for (const BasicBlock &BB : F)
138 for (const Instruction &I : BB) {
139 auto OrderConstantFromMetadata = [&](Metadata *MD) {
140 if (const auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
141 orderConstantValue(VAM->getValue());
142 } else if (const auto *AL = dyn_cast<DIArgList>(MD)) {
143 for (const auto *VAM : AL->getArgs())
144 orderConstantValue(VAM->getValue());
145 }
146 };
147
148 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
149 OrderConstantFromMetadata(DVR.getRawLocation());
150 if (DVR.isDbgAssign())
151 OrderConstantFromMetadata(DVR.getRawAddress());
152 }
153
154 for (const Value *V : I.operands()) {
155 if (const auto *MAV = dyn_cast<MetadataAsValue>(V))
156 OrderConstantFromMetadata(MAV->getMetadata());
157 }
158 }
159
160 for (const Argument &A : F.args())
161 orderValue(&A, OM);
162 for (const BasicBlock &BB : F)
163 for (const Instruction &I : BB) {
164 for (const Value *Op : I.operands())
165 orderConstantValue(Op);
166 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
167 orderValue(SVI->getShuffleMaskForBitcode(), OM);
168 if (auto *SI = dyn_cast<SwitchInst>(&I)) {
169 for (const auto &Case : SI->cases())
170 orderValue(Case.getCaseValue(), OM);
171 }
172 orderValue(&I, OM);
173 }
174 }
175 return OM;
176}
177
178static void predictValueUseListOrderImpl(const Value *V, const Function *F,
179 unsigned ID, const OrderMap &OM,
180 UseListOrderStack &Stack) {
181 // Predict use-list order for this one.
182 using Entry = std::pair<const Use *, unsigned>;
184 for (const Use &U : V->uses())
185 // Check if this user will be serialized.
186 if (OM.lookup(U.getUser()).first)
187 List.push_back(std::make_pair(&U, List.size()));
188
189 if (List.size() < 2)
190 // We may have lost some users.
191 return;
192
193 bool IsGlobalValue = OM.isGlobalValue(ID);
194 llvm::sort(List, [&](const Entry &L, const Entry &R) {
195 const Use *LU = L.first;
196 const Use *RU = R.first;
197 if (LU == RU)
198 return false;
199
200 auto LID = OM.lookup(LU->getUser()).first;
201 auto RID = OM.lookup(RU->getUser()).first;
202
203 // If ID is 4, then expect: 7 6 5 1 2 3.
204 if (LID < RID) {
205 if (RID <= ID)
206 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
207 return true;
208 return false;
209 }
210 if (RID < LID) {
211 if (LID <= ID)
212 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
213 return false;
214 return true;
215 }
216
217 // LID and RID are equal, so we have different operands of the same user.
218 // Assume operands are added in order for all instructions.
219 if (LID <= ID)
220 if (!IsGlobalValue) // GlobalValue uses don't get reversed.
221 return LU->getOperandNo() < RU->getOperandNo();
222 return LU->getOperandNo() > RU->getOperandNo();
223 });
224
226 // Order is already correct.
227 return;
228
229 // Store the shuffle.
230 Stack.emplace_back(V, F, List.size());
231 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
232 for (size_t I = 0, E = List.size(); I != E; ++I)
233 Stack.back().Shuffle[I] = List[I].second;
234}
235
236static void predictValueUseListOrder(const Value *V, const Function *F,
237 OrderMap &OM, UseListOrderStack &Stack) {
238 if (!V->hasUseList())
239 return;
240
241 auto &IDPair = OM[V];
242 assert(IDPair.first && "Unmapped value");
243 if (IDPair.second)
244 // Already predicted.
245 return;
246
247 // Do the actual prediction.
248 IDPair.second = true;
249 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
250 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
251
252 // Recursive descent into constants.
253 if (const Constant *C = dyn_cast<Constant>(V)) {
254 if (C->getNumOperands()) { // Visit GlobalValues.
255 for (const Value *Op : C->operands())
256 if (isa<Constant>(Op)) // Visit GlobalValues.
257 predictValueUseListOrder(Op, F, OM, Stack);
258 if (auto *CE = dyn_cast<ConstantExpr>(C))
259 if (CE->getOpcode() == Instruction::ShuffleVector)
260 predictValueUseListOrder(CE->getShuffleMaskForBitcode(), F, OM,
261 Stack);
262 }
263 }
264}
265
267 OrderMap OM = orderModule(M);
268
269 // Use-list orders need to be serialized after all the users have been added
270 // to a value, or else the shuffles will be incomplete. Store them per
271 // function in a stack.
272 //
273 // Aside from function order, the order of values doesn't matter much here.
274 UseListOrderStack Stack;
275
276 // We want to visit the functions backward now so we can list function-local
277 // constants in the last Function they're used in. Module-level constants
278 // have already been visited above.
279 for (const Function &F : llvm::reverse(M)) {
280 auto PredictValueOrderFromMetadata = [&](Metadata *MD) {
281 if (const auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
282 predictValueUseListOrder(VAM->getValue(), &F, OM, Stack);
283 } else if (const auto *AL = dyn_cast<DIArgList>(MD)) {
284 for (const auto *VAM : AL->getArgs())
285 predictValueUseListOrder(VAM->getValue(), &F, OM, Stack);
286 }
287 };
288 if (F.isDeclaration())
289 continue;
290 for (const BasicBlock &BB : F)
291 predictValueUseListOrder(&BB, &F, OM, Stack);
292 for (const Argument &A : F.args())
293 predictValueUseListOrder(&A, &F, OM, Stack);
294 for (const BasicBlock &BB : F) {
295 for (const Instruction &I : BB) {
296 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
297 PredictValueOrderFromMetadata(DVR.getRawLocation());
298 if (DVR.isDbgAssign())
299 PredictValueOrderFromMetadata(DVR.getRawAddress());
300 }
301 for (const Value *Op : I.operands()) {
302 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
303 predictValueUseListOrder(Op, &F, OM, Stack);
304 if (const auto *MAV = dyn_cast<MetadataAsValue>(Op))
305 PredictValueOrderFromMetadata(MAV->getMetadata());
306 }
307 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
308 predictValueUseListOrder(SVI->getShuffleMaskForBitcode(), &F, OM,
309 Stack);
310 predictValueUseListOrder(&I, &F, OM, Stack);
311 }
312 }
313 }
314
315 // Visit globals last, since the module-level use-list block will be seen
316 // before the function bodies are processed.
317 for (const GlobalVariable &G : M.globals())
318 predictValueUseListOrder(&G, nullptr, OM, Stack);
319 for (const Function &F : M)
320 predictValueUseListOrder(&F, nullptr, OM, Stack);
321 for (const GlobalAlias &A : M.aliases())
322 predictValueUseListOrder(&A, nullptr, OM, Stack);
323 for (const GlobalIFunc &I : M.ifuncs())
324 predictValueUseListOrder(&I, nullptr, OM, Stack);
325 for (const GlobalVariable &G : M.globals())
326 if (G.hasInitializer())
327 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
328 for (const GlobalAlias &A : M.aliases())
329 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
330 for (const GlobalIFunc &I : M.ifuncs())
331 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
332 for (const Function &F : M) {
333 for (const Use &U : F.operands())
334 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
335 }
336
337 return Stack;
338}
339
340static bool isIntOrIntVectorValue(const std::pair<const Value*, unsigned> &V) {
341 return V.first->getType()->isIntOrIntVectorTy();
342}
343
345 bool ShouldPreserveUseListOrder)
346 : ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
347 if (ShouldPreserveUseListOrder)
349
350 // Enumerate the global variables.
351 for (const GlobalVariable &GV : M.globals()) {
352 EnumerateValue(&GV);
353 EnumerateType(GV.getValueType());
354 }
355
356 // Enumerate the functions.
357 for (const Function & F : M) {
358 EnumerateValue(&F);
359 EnumerateType(F.getFunctionType());
360 EnumerateAttributes(F.getAttributes());
361 }
362
363 // Enumerate the aliases.
364 for (const GlobalAlias &GA : M.aliases()) {
365 EnumerateValue(&GA);
366 EnumerateType(GA.getValueType());
367 }
368
369 // Enumerate the ifuncs.
370 for (const GlobalIFunc &GIF : M.ifuncs()) {
371 EnumerateValue(&GIF);
372 EnumerateType(GIF.getValueType());
373 }
374
375 // Remember what is the cutoff between globalvalue's and other constants.
376 unsigned FirstConstant = Values.size();
377
378 // Enumerate the global variable initializers and attributes.
379 for (const GlobalVariable &GV : M.globals()) {
380 if (GV.hasInitializer())
381 EnumerateValue(GV.getInitializer());
382 if (GV.hasAttributes())
383 EnumerateAttributes(GV.getAttributesAsList(AttributeList::FunctionIndex));
384 }
385
386 // Enumerate the aliasees.
387 for (const GlobalAlias &GA : M.aliases())
388 EnumerateValue(GA.getAliasee());
389
390 // Enumerate the ifunc resolvers.
391 for (const GlobalIFunc &GIF : M.ifuncs())
392 EnumerateValue(GIF.getResolver());
393
394 // Enumerate any optional Function data.
395 for (const Function &F : M)
396 for (const Use &U : F.operands())
397 EnumerateValue(U.get());
398
399 // Enumerate the metadata type.
400 //
401 // TODO: Move this to ValueEnumerator::EnumerateOperandType() once bitcode
402 // only encodes the metadata type when it's used as a value.
403 EnumerateType(Type::getMetadataTy(M.getContext()));
404
405 // Insert constants and metadata that are named at module level into the slot
406 // pool so that the module symbol table can refer to them...
407 EnumerateValueSymbolTable(M.getValueSymbolTable());
408 EnumerateNamedMetadata(M);
409
411 for (const GlobalVariable &GV : M.globals()) {
412 MDs.clear();
413 GV.getAllMetadata(MDs);
414 for (const auto &I : MDs)
415 // FIXME: Pass GV to EnumerateMetadata and arrange for the bitcode writer
416 // to write metadata to the global variable's own metadata block
417 // (PR28134).
418 EnumerateMetadata(nullptr, I.second);
419 }
420
421 // Enumerate types used by function bodies and argument lists.
422 for (const Function &F : M) {
423 for (const Argument &A : F.args())
424 EnumerateType(A.getType());
425
426 // Enumerate metadata attached to this function.
427 MDs.clear();
428 F.getAllMetadata(MDs);
429 for (const auto &I : MDs)
430 EnumerateMetadata(F.isDeclaration() ? nullptr : &F, I.second);
431
432 for (const BasicBlock &BB : F)
433 for (const Instruction &I : BB) {
434 // Local metadata is enumerated during function-incorporation, but
435 // any ConstantAsMetadata arguments in a DIArgList should be examined
436 // now.
437 auto EnumerateNonLocalValuesFromMetadata = [&](Metadata *MD) {
438 assert(MD && "Metadata unexpectedly null");
439 if (const auto *AL = dyn_cast<DIArgList>(MD)) {
440 for (const auto *VAM : AL->getArgs()) {
442 EnumerateMetadata(&F, VAM);
443 }
444 return;
445 }
446
447 if (!isa<LocalAsMetadata>(MD))
448 EnumerateMetadata(&F, MD);
449 };
450
451 for (DbgRecord &DR : I.getDbgRecordRange()) {
453 EnumerateMetadata(&F, DLR->getLabel());
454 EnumerateMetadata(&F, &*DLR->getDebugLoc());
455 continue;
456 }
457 // Enumerate non-local location metadata.
459 EnumerateNonLocalValuesFromMetadata(DVR.getRawLocation());
460 EnumerateMetadata(&F, DVR.getExpression());
461 EnumerateMetadata(&F, DVR.getVariable());
462 EnumerateMetadata(&F, &*DVR.getDebugLoc());
463 if (DVR.isDbgAssign()) {
464 EnumerateNonLocalValuesFromMetadata(DVR.getRawAddress());
465 EnumerateMetadata(&F, DVR.getAssignID());
466 EnumerateMetadata(&F, DVR.getAddressExpression());
467 }
468 }
469 for (const Use &Op : I.operands()) {
470 auto *MD = dyn_cast<MetadataAsValue>(&Op);
471 if (!MD) {
472 EnumerateOperandType(Op);
473 continue;
474 }
475
476 EnumerateNonLocalValuesFromMetadata(MD->getMetadata());
477 }
478 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
479 EnumerateType(SVI->getShuffleMaskForBitcode()->getType());
480 if (auto *GEP = dyn_cast<GetElementPtrInst>(&I))
481 EnumerateType(GEP->getSourceElementType());
482 if (auto *AI = dyn_cast<AllocaInst>(&I))
483 EnumerateType(AI->getAllocatedType());
484 EnumerateType(I.getType());
485 if (const auto *Call = dyn_cast<CallBase>(&I)) {
486 EnumerateAttributes(Call->getAttributes());
487 EnumerateType(Call->getFunctionType());
488 }
489
490 // Enumerate metadata attached with this instruction.
491 MDs.clear();
492 I.getAllMetadataOtherThanDebugLoc(MDs);
493 for (const auto &MD : MDs)
494 EnumerateMetadata(&F, MD.second);
495
496 // Don't enumerate the location directly -- it has a special record
497 // type -- but enumerate its operands.
498 if (DILocation *L = I.getDebugLoc())
499 for (const Metadata *Op : L->operands())
500 EnumerateMetadata(&F, Op);
501 }
502 }
503 for (const GlobalIFunc &GIF : M.ifuncs()) {
504 MDs.clear();
505 GIF.getAllMetadata(MDs);
506 for (const auto &I : MDs)
507 EnumerateMetadata(nullptr, I.second);
508 }
509
510 // Optimize constant ordering.
511 OptimizeConstants(FirstConstant, Values.size());
512
513 // Organize metadata ordering.
514 organizeMetadata();
515}
516
518 InstructionMapType::const_iterator I = InstructionMap.find(Inst);
519 assert(I != InstructionMap.end() && "Instruction is not mapped!");
520 return I->second;
521}
522
523unsigned ValueEnumerator::getComdatID(const Comdat *C) const {
524 unsigned ComdatID = Comdats.idFor(C);
525 assert(ComdatID && "Comdat not found!");
526 return ComdatID;
527}
528
530 InstructionMap[I] = InstructionCount++;
531}
532
533unsigned ValueEnumerator::getValueID(const Value *V) const {
534 if (auto *MD = dyn_cast<MetadataAsValue>(V))
535 return getMetadataID(MD->getMetadata());
536
537 ValueMapType::const_iterator I = ValueMap.find(V);
538 assert(I != ValueMap.end() && "Value not in slotcalculator!");
539 return I->second-1;
540}
541
542#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
544 print(dbgs(), ValueMap, "Default");
545 dbgs() << '\n';
546 print(dbgs(), MetadataMap, "MetaData");
547 dbgs() << '\n';
548}
549#endif
550
551void ValueEnumerator::print(raw_ostream &OS, const ValueMapType &Map,
552 const char *Name) const {
553 OS << "Map Name: " << Name << "\n";
554 OS << "Size: " << Map.size() << "\n";
555 for (const auto &I : Map) {
556 const Value *V = I.first;
557 if (V->hasName())
558 OS << "Value: " << V->getName();
559 else
560 OS << "Value: [null]\n";
561 V->print(errs());
562 errs() << '\n';
563
564 OS << " Uses(" << V->getNumUses() << "):";
565 for (const Use &U : V->uses()) {
566 if (&U != &*V->use_begin())
567 OS << ",";
568 if(U->hasName())
569 OS << " " << U->getName();
570 else
571 OS << " [null]";
572
573 }
574 OS << "\n\n";
575 }
576}
577
578void ValueEnumerator::print(raw_ostream &OS, const MetadataMapType &Map,
579 const char *Name) const {
580 OS << "Map Name: " << Name << "\n";
581 OS << "Size: " << Map.size() << "\n";
582 for (const auto &I : Map) {
583 const Metadata *MD = I.first;
584 OS << "Metadata: slot = " << I.second.ID << "\n";
585 OS << "Metadata: function = " << I.second.F << "\n";
586 MD->print(OS);
587 OS << "\n";
588 }
589}
590
591/// OptimizeConstants - Reorder constant pool for denser encoding.
592void ValueEnumerator::OptimizeConstants(unsigned CstStart, unsigned CstEnd) {
593 if (CstStart == CstEnd || CstStart+1 == CstEnd) return;
594
595 if (ShouldPreserveUseListOrder)
596 // Optimizing constants makes the use-list order difficult to predict.
597 // Disable it for now when trying to preserve the order.
598 return;
599
600 std::stable_sort(Values.begin() + CstStart, Values.begin() + CstEnd,
601 [this](const std::pair<const Value *, unsigned> &LHS,
602 const std::pair<const Value *, unsigned> &RHS) {
603 // Sort by plane.
604 if (LHS.first->getType() != RHS.first->getType())
605 return getTypeID(LHS.first->getType()) < getTypeID(RHS.first->getType());
606 // Then by frequency.
607 return LHS.second > RHS.second;
608 });
609
610 // Ensure that integer and vector of integer constants are at the start of the
611 // constant pool. This is important so that GEP structure indices come before
612 // gep constant exprs.
613 std::stable_partition(Values.begin() + CstStart, Values.begin() + CstEnd,
615
616 // Rebuild the modified portion of ValueMap.
617 for (; CstStart != CstEnd; ++CstStart)
618 ValueMap[Values[CstStart].first] = CstStart+1;
619}
620
621/// EnumerateValueSymbolTable - Insert all of the values in the specified symbol
622/// table into the values table.
623void ValueEnumerator::EnumerateValueSymbolTable(const ValueSymbolTable &VST) {
624 for (const auto &VI : VST)
625 EnumerateValue(VI.getValue());
626}
627
628/// Insert all of the values referenced by named metadata in the specified
629/// module.
630void ValueEnumerator::EnumerateNamedMetadata(const Module &M) {
631 for (const auto &I : M.named_metadata())
632 EnumerateNamedMDNode(&I);
633}
634
635void ValueEnumerator::EnumerateNamedMDNode(const NamedMDNode *MD) {
636 for (const MDNode *N : MD->operands())
637 EnumerateMetadata(nullptr, N);
638}
639
640unsigned ValueEnumerator::getMetadataFunctionID(const Function *F) const {
641 return F ? getValueID(F) + 1 : 0;
642}
643
644void ValueEnumerator::EnumerateMetadata(const Function *F, const Metadata *MD) {
645 EnumerateMetadata(getMetadataFunctionID(F), MD);
646}
647
648void ValueEnumerator::EnumerateFunctionLocalMetadata(
649 const Function &F, const LocalAsMetadata *Local) {
650 EnumerateFunctionLocalMetadata(getMetadataFunctionID(&F), Local);
651}
652
653void ValueEnumerator::EnumerateFunctionLocalListMetadata(
654 const Function &F, const DIArgList *ArgList) {
655 EnumerateFunctionLocalListMetadata(getMetadataFunctionID(&F), ArgList);
656}
657
658void ValueEnumerator::dropFunctionFromMetadata(
659 MetadataMapType::value_type &FirstMD) {
661 auto push = [&Worklist](MetadataMapType::value_type &MD) {
662 auto &Entry = MD.second;
663
664 // Nothing to do if this metadata isn't tagged.
665 if (!Entry.F)
666 return;
667
668 // Drop the function tag.
669 Entry.F = 0;
670
671 // If this is has an ID and is an MDNode, then its operands have entries as
672 // well. We need to drop the function from them too.
673 if (Entry.ID)
674 if (auto *N = dyn_cast<MDNode>(MD.first))
675 Worklist.push_back(N);
676 };
677 push(FirstMD);
678 while (!Worklist.empty()) {
679 const MDNode *Next = Worklist.pop_back_val();
680 for (const Metadata *Op : Next->operands()) {
681 if (!Op)
682 continue;
683 auto MD = MetadataMap.find(Op);
684 if (MD != MetadataMap.end())
685 push(*MD);
686 }
687 if (auto *FLContext = dyn_cast<MDFunctionLocalContext>(Next)) {
689 FLContext->getUsedMDNodes(UsedNodes);
690 for (const Metadata *Op : UsedNodes)
691 if (auto MD = MetadataMap.find(Op); MD != MetadataMap.end())
692 push(*MD);
693 }
694 }
695}
696
697void ValueEnumerator::EnumerateMetadata(unsigned F, const Metadata *MD) {
698 // It's vital for reader efficiency that uniqued subgraphs are done in
699 // post-order; it's expensive when their operands have forward references.
700 // If a distinct node is referenced from a uniqued node, it'll be delayed
701 // until the uniqued subgraph has been completely traversed.
702 SmallVector<const MDNode *, 32> DelayedDistinctNodes;
703
704 // Start by enumerating MD, and then work through its transitive operands in
705 // post-order. This requires a depth-first search.
707
708 if (const MDNode *N = enumerateMetadataImpl(F, MD))
709 Worklist.push_back(std::make_pair(N, 0));
710
711 // Fetching the MDNodes used by an MDFunctionLocalContext is expensive; if we
712 // need to get them at all, do so once in this map and reuse as needed.
713 DenseMap<const MDFunctionLocalContext *, SmallVector<const MDNode *, 0>>
714 FLContextUsedNodes;
715 while (!Worklist.empty()) {
716 const MDNode *N = Worklist.back().first;
717 size_t &OpNum = Worklist.back().second;
718
719 // Enumerate operands until we hit a new node. We need to traverse these
720 // nodes' operands before visiting the rest of N's operands.
721 if (OpNum < N->getNumOperands()) {
722 MDNode::op_iterator I = std::find_if(
723 N->op_begin() + OpNum, N->op_end(),
724 [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
725 if (I != N->op_end()) {
726 auto *Op = cast<MDNode>(*I);
727 OpNum = I - N->op_begin() + 1;
728
729 // Delay traversing Op if it's a distinct node and N is uniqued.
730 if (Op->isDistinct() && !N->isDistinct())
731 DelayedDistinctNodes.push_back(Op);
732 else
733 Worklist.push_back(std::make_pair(Op, 0));
734 continue;
735 }
736 }
737 // MDFunctionLocalContext contains MDNode references outside of its
738 // operands, which can be fetched via `getUsedMDNodes()`; OpNum indexes into
739 // this vector if N is an MDFunctionLocalContext.
740 if (auto *FLContext = dyn_cast<MDFunctionLocalContext>(N)) {
741 // Currently, MDFunctionLocalContext has no operands; if it ever does in
742 // future, then we need to account for them by offsetting OpNum by the
743 // operand count. For now, we have an assert to prevent a silent fail from
744 // being introduced.
745 assert(N->getNumOperands() == 0 &&
746 "MDFunctionLocalContext expected to have no operands");
747 auto Result = FLContextUsedNodes.try_emplace(FLContext);
748 auto &UsedMDNodes = Result.first->second;
749 if (Result.second)
750 FLContext->getUsedMDNodes(UsedMDNodes);
751 auto *NextUsed = std::find_if(
752 UsedMDNodes.begin() + OpNum, UsedMDNodes.end(),
753 [&](const Metadata *MD) { return enumerateMetadataImpl(F, MD); });
754 if (NextUsed != UsedMDNodes.end()) {
755 auto *Op = *NextUsed;
756 OpNum = NextUsed - UsedMDNodes.begin() + 1;
757
758 // Delay traversing Op if it's a distinct node and N is uniqued.
759 if (Op->isDistinct() && !N->isDistinct())
760 DelayedDistinctNodes.push_back(Op);
761 else
762 Worklist.push_back(std::make_pair(Op, 0));
763 continue;
764 }
765 } else {
766 assert(OpNum <= N->getNumOperands() &&
767 "Visited past the end of N's operands");
768 }
769
770 // All the operands have been visited. Now assign an ID.
771 Worklist.pop_back();
772 MDs.push_back(N);
773 MetadataMap[N].ID = MDs.size();
774
775 // Flush out any delayed distinct nodes; these are all the distinct nodes
776 // that are leaves in last uniqued subgraph.
777 if (Worklist.empty() || Worklist.back().first->isDistinct()) {
778 for (const MDNode *N : DelayedDistinctNodes)
779 Worklist.push_back(std::make_pair(N, 0));
780 DelayedDistinctNodes.clear();
781 }
782 }
783}
784
785const MDNode *ValueEnumerator::enumerateMetadataImpl(unsigned F, const Metadata *MD) {
786 if (!MD)
787 return nullptr;
788
789 assert(
791 "Invalid metadata kind");
792
793 auto Insertion = MetadataMap.insert(std::make_pair(MD, MDIndex(F)));
794 MDIndex &Entry = Insertion.first->second;
795 if (!Insertion.second) {
796 // Already mapped. If F doesn't match the function tag, drop it.
797 if (Entry.hasDifferentFunction(F))
798 dropFunctionFromMetadata(*Insertion.first);
799 return nullptr;
800 }
801
802 // Don't assign IDs to metadata nodes.
803 if (auto *N = dyn_cast<MDNode>(MD))
804 return N;
805
806 // Save the metadata.
807 MDs.push_back(MD);
808 Entry.ID = MDs.size();
809
810 // Enumerate the constant, if any.
811 if (auto *C = dyn_cast<ConstantAsMetadata>(MD))
812 EnumerateValue(C->getValue());
813
814 return nullptr;
815}
816
817/// EnumerateFunctionLocalMetadata - Incorporate function-local metadata
818/// information reachable from the metadata.
819void ValueEnumerator::EnumerateFunctionLocalMetadata(
820 unsigned F, const LocalAsMetadata *Local) {
821 assert(F && "Expected a function");
822
823 // Check to see if it's already in!
824 MDIndex &Index = MetadataMap[Local];
825 if (Index.ID) {
826 assert(Index.F == F && "Expected the same function");
827 return;
828 }
829
830 MDs.push_back(Local);
831 Index.F = F;
832 Index.ID = MDs.size();
833
834 EnumerateValue(Local->getValue());
835}
836
837/// EnumerateFunctionLocalListMetadata - Incorporate function-local metadata
838/// information reachable from the metadata.
839void ValueEnumerator::EnumerateFunctionLocalListMetadata(
840 unsigned F, const DIArgList *ArgList) {
841 assert(F && "Expected a function");
842
843 // Check to see if it's already in!
844 MDIndex &Index = MetadataMap[ArgList];
845 if (Index.ID) {
846 assert(Index.F == F && "Expected the same function");
847 return;
848 }
849
850 for (ValueAsMetadata *VAM : ArgList->getArgs()) {
851 if (isa<LocalAsMetadata>(VAM)) {
852 assert(MetadataMap.count(VAM) &&
853 "LocalAsMetadata should be enumerated before DIArgList");
854 assert(MetadataMap[VAM].F == F &&
855 "Expected LocalAsMetadata in the same function");
856 } else {
858 "Expected LocalAsMetadata or ConstantAsMetadata");
859 assert(ValueMap.count(VAM->getValue()) &&
860 "Constant should be enumerated beforeDIArgList");
861 EnumerateMetadata(F, VAM);
862 }
863 }
864
865 MDs.push_back(ArgList);
866 Index.F = F;
867 Index.ID = MDs.size();
868}
869
870static unsigned getMetadataTypeOrder(const Metadata *MD) {
871 // Strings are emitted in bulk and must come first.
872 if (isa<MDString>(MD))
873 return 0;
874
875 // ConstantAsMetadata doesn't reference anything. We may as well shuffle it
876 // to the front since we can detect it.
877 auto *N = dyn_cast<MDNode>(MD);
878 if (!N)
879 return 1;
880
881 // The reader is fast forward references for distinct node operands, but slow
882 // when uniqued operands are unresolved.
883 return N->isDistinct() ? 2 : 3;
884}
885
886void ValueEnumerator::organizeMetadata() {
887 assert(MetadataMap.size() == MDs.size() &&
888 "Metadata map and vector out of sync");
889
890 if (MDs.empty())
891 return;
892
893 // Copy out the index information from MetadataMap in order to choose a new
894 // order.
896 Order.reserve(MetadataMap.size());
897 for (const Metadata *MD : MDs)
898 Order.push_back(MetadataMap.lookup(MD));
899
900 // Partition:
901 // - by function, then
902 // - by isa<MDString>
903 // and then sort by the original/current ID. Since the IDs are guaranteed to
904 // be unique, the result of llvm::sort will be deterministic. There's no need
905 // for std::stable_sort.
906 llvm::sort(Order, [this](MDIndex LHS, MDIndex RHS) {
907 return std::make_tuple(LHS.F, getMetadataTypeOrder(LHS.get(MDs)), LHS.ID) <
908 std::make_tuple(RHS.F, getMetadataTypeOrder(RHS.get(MDs)), RHS.ID);
909 });
910
911 // Rebuild MDs, index the metadata ranges for each function in FunctionMDs,
912 // and fix up MetadataMap.
913 std::vector<const Metadata *> OldMDs;
914 MDs.swap(OldMDs);
915 MDs.reserve(OldMDs.size());
916 for (unsigned I = 0, E = Order.size(); I != E && !Order[I].F; ++I) {
917 auto *MD = Order[I].get(OldMDs);
918 MDs.push_back(MD);
919 MetadataMap[MD].ID = I + 1;
920 if (isa<MDString>(MD))
921 ++NumMDStrings;
922 }
923
924 // Return early if there's nothing for the functions.
925 if (MDs.size() == Order.size())
926 return;
927
928 // Build the function metadata ranges.
929 MDRange R;
930 FunctionMDs.reserve(OldMDs.size());
931 unsigned PrevF = 0;
932 for (unsigned I = MDs.size(), E = Order.size(), ID = MDs.size(); I != E;
933 ++I) {
934 unsigned F = Order[I].F;
935 if (!PrevF) {
936 PrevF = F;
937 } else if (PrevF != F) {
938 R.Last = FunctionMDs.size();
939 std::swap(R, FunctionMDInfo[PrevF]);
940 R.First = FunctionMDs.size();
941
942 ID = MDs.size();
943 PrevF = F;
944 }
945
946 auto *MD = Order[I].get(OldMDs);
947 FunctionMDs.push_back(MD);
948 MetadataMap[MD].ID = ++ID;
949 if (isa<MDString>(MD))
950 ++R.NumStrings;
951 }
952 R.Last = FunctionMDs.size();
953 FunctionMDInfo[PrevF] = R;
954}
955
956void ValueEnumerator::incorporateFunctionMetadata(const Function &F) {
957 NumModuleMDs = MDs.size();
958
959 auto R = FunctionMDInfo.lookup(getValueID(&F) + 1);
960 NumMDStrings = R.NumStrings;
961 MDs.insert(MDs.end(), FunctionMDs.begin() + R.First,
962 FunctionMDs.begin() + R.Last);
963}
964
965void ValueEnumerator::EnumerateValue(const Value *V) {
966 assert(!V->getType()->isVoidTy() && "Can't insert void values!");
967 assert(!isa<MetadataAsValue>(V) && "EnumerateValue doesn't handle Metadata!");
968
969 // Check to see if it's already in!
970 unsigned &ValueID = ValueMap[V];
971 if (ValueID) {
972 // Increment use count.
973 Values[ValueID-1].second++;
974 return;
975 }
976
977 if (auto *GO = dyn_cast<GlobalObject>(V))
978 if (const Comdat *C = GO->getComdat())
979 Comdats.insert(C);
980
981 // Enumerate the type of this value.
982 EnumerateType(V->getType());
983
984 if (const Constant *C = dyn_cast<Constant>(V)) {
985 if (isa<GlobalValue>(C)) {
986 // Initializers for globals are handled explicitly elsewhere.
987 } else if (C->getNumOperands()) {
988 // If a constant has operands, enumerate them. This makes sure that if a
989 // constant has uses (for example an array of const ints), that they are
990 // inserted also.
991
992 // We prefer to enumerate them with values before we enumerate the user
993 // itself. This makes it more likely that we can avoid forward references
994 // in the reader. We know that there can be no cycles in the constants
995 // graph that don't go through a global variable.
996 for (const Use &U : C->operands())
997 if (!isa<BasicBlock>(U)) // Don't enumerate BB operand to BlockAddress.
998 EnumerateValue(U);
999 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1000 if (CE->getOpcode() == Instruction::ShuffleVector)
1001 EnumerateValue(CE->getShuffleMaskForBitcode());
1002 if (auto *GEP = dyn_cast<GEPOperator>(CE))
1003 EnumerateType(GEP->getSourceElementType());
1004 }
1005
1006 // Finally, add the value. Doing this could make the ValueID reference be
1007 // dangling, don't reuse it.
1008 Values.push_back(std::make_pair(V, 1U));
1009 ValueMap[V] = Values.size();
1010 return;
1011 }
1012 }
1013
1014 // Add the value.
1015 Values.push_back(std::make_pair(V, 1U));
1016 ValueID = Values.size();
1017}
1018
1019
1020void ValueEnumerator::EnumerateType(Type *Ty) {
1021 unsigned *TypeID = &TypeMap[Ty];
1022
1023 // We've already seen this type.
1024 if (*TypeID)
1025 return;
1026
1027 // If it is a non-anonymous struct, mark the type as being visited so that we
1028 // don't recursively visit it. This is safe because we allow forward
1029 // references of these in the bitcode reader.
1030 if (StructType *STy = dyn_cast<StructType>(Ty))
1031 if (!STy->isLiteral())
1032 *TypeID = ~0U;
1033
1034 // Enumerate all of the subtypes before we enumerate this type. This ensures
1035 // that the type will be enumerated in an order that can be directly built.
1036 for (Type *SubTy : Ty->subtypes())
1037 EnumerateType(SubTy);
1038
1039 // Refresh the TypeID pointer in case the table rehashed.
1040 TypeID = &TypeMap[Ty];
1041
1042 // Check to see if we got the pointer another way. This can happen when
1043 // enumerating recursive types that hit the base case deeper than they start.
1044 //
1045 // If this is actually a struct that we are treating as forward ref'able,
1046 // then emit the definition now that all of its contents are available.
1047 if (*TypeID && *TypeID != ~0U)
1048 return;
1049
1050 // Add this type now that its contents are all happily enumerated.
1051 Types.push_back(Ty);
1052
1053 *TypeID = Types.size();
1054}
1055
1056// Enumerate the types for the specified value. If the value is a constant,
1057// walk through it, enumerating the types of the constant.
1058void ValueEnumerator::EnumerateOperandType(const Value *V) {
1059 EnumerateType(V->getType());
1060
1061 assert(!isa<MetadataAsValue>(V) && "Unexpected metadata operand");
1062
1063 const Constant *C = dyn_cast<Constant>(V);
1064 if (!C)
1065 return;
1066
1067 // If this constant is already enumerated, ignore it, we know its type must
1068 // be enumerated.
1069 if (ValueMap.count(C))
1070 return;
1071
1072 // This constant may have operands, make sure to enumerate the types in
1073 // them.
1074 for (const Value *Op : C->operands()) {
1075 // Don't enumerate basic blocks here, this happens as operands to
1076 // blockaddress.
1077 if (isa<BasicBlock>(Op))
1078 continue;
1079
1080 EnumerateOperandType(Op);
1081 }
1082 if (auto *CE = dyn_cast<ConstantExpr>(C)) {
1083 if (CE->getOpcode() == Instruction::ShuffleVector)
1084 EnumerateOperandType(CE->getShuffleMaskForBitcode());
1085 if (CE->getOpcode() == Instruction::GetElementPtr)
1086 EnumerateType(cast<GEPOperator>(CE)->getSourceElementType());
1087 }
1088}
1089
1090void ValueEnumerator::EnumerateAttributes(AttributeList PAL) {
1091 if (PAL.isEmpty()) return; // null is always 0.
1092
1093 // Do a lookup.
1094 unsigned &Entry = AttributeListMap[PAL];
1095 if (Entry == 0) {
1096 // Never saw this before, add it.
1097 AttributeLists.push_back(PAL);
1098 Entry = AttributeLists.size();
1099 }
1100
1101 // Do lookups for all attribute groups.
1102 for (unsigned i : PAL.indexes()) {
1103 AttributeSet AS = PAL.getAttributes(i);
1104 if (!AS.hasAttributes())
1105 continue;
1106 IndexAndAttrSet Pair = {i, AS};
1107 unsigned &Entry = AttributeGroupMap[Pair];
1108 if (Entry == 0) {
1109 AttributeGroups.push_back(Pair);
1110 Entry = AttributeGroups.size();
1111
1112 for (Attribute Attr : AS) {
1113 if (Attr.isTypeAttribute())
1114 EnumerateType(Attr.getValueAsType());
1115 }
1116 }
1117 }
1118}
1119
1121 InstructionCount = 0;
1122 NumModuleValues = Values.size();
1123
1124 // Add global metadata to the function block. This doesn't include
1125 // LocalAsMetadata.
1126 incorporateFunctionMetadata(F);
1127
1128 // Adding function arguments to the value table.
1129 for (const auto &I : F.args()) {
1130 EnumerateValue(&I);
1131 if (I.hasAttribute(Attribute::ByVal))
1132 EnumerateType(I.getParamByValType());
1133 else if (I.hasAttribute(Attribute::StructRet))
1134 EnumerateType(I.getParamStructRetType());
1135 else if (I.hasAttribute(Attribute::ByRef))
1136 EnumerateType(I.getParamByRefType());
1137 }
1138 FirstFuncConstantID = Values.size();
1139
1140 // Add all function-level constants to the value table.
1141 for (const BasicBlock &BB : F) {
1142 for (const Instruction &I : BB) {
1143 for (const Use &OI : I.operands()) {
1144 if ((isa<Constant>(OI) && !isa<GlobalValue>(OI)) || isa<InlineAsm>(OI))
1145 EnumerateValue(OI);
1146 }
1147 if (auto *SVI = dyn_cast<ShuffleVectorInst>(&I))
1148 EnumerateValue(SVI->getShuffleMaskForBitcode());
1149 if (auto *SI = dyn_cast<SwitchInst>(&I)) {
1150 for (const auto &Case : SI->cases())
1151 EnumerateValue(Case.getCaseValue());
1152 }
1153 }
1154 BasicBlocks.push_back(&BB);
1155 ValueMap[&BB] = BasicBlocks.size();
1156 }
1157
1158 // Optimize the constant layout.
1159 OptimizeConstants(FirstFuncConstantID, Values.size());
1160
1161 // Add the function's parameter attributes so they are available for use in
1162 // the function's instruction.
1163 EnumerateAttributes(F.getAttributes());
1164
1165 FirstInstID = Values.size();
1166
1167 SmallVector<LocalAsMetadata *, 8> FnLocalMDVector;
1168 SmallVector<DIArgList *, 8> ArgListMDVector;
1169
1170 auto AddFnLocalMetadata = [&](Metadata *MD) {
1171 if (!MD)
1172 return;
1173 if (auto *Local = dyn_cast<LocalAsMetadata>(MD)) {
1174 // Enumerate metadata after the instructions they might refer to.
1175 FnLocalMDVector.push_back(Local);
1176 } else if (auto *ArgList = dyn_cast<DIArgList>(MD)) {
1177 ArgListMDVector.push_back(ArgList);
1178 for (ValueAsMetadata *VMD : ArgList->getArgs()) {
1179 if (auto *Local = dyn_cast<LocalAsMetadata>(VMD)) {
1180 // Enumerate metadata after the instructions they might refer
1181 // to.
1182 FnLocalMDVector.push_back(Local);
1183 }
1184 }
1185 }
1186 };
1187
1188 // Add all of the instructions.
1189 for (const BasicBlock &BB : F) {
1190 for (const Instruction &I : BB) {
1191 for (const Use &OI : I.operands()) {
1192 if (auto *MD = dyn_cast<MetadataAsValue>(&OI))
1193 AddFnLocalMetadata(MD->getMetadata());
1194 }
1195 /// RemoveDIs: Add non-instruction function-local metadata uses.
1196 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
1197 assert(DVR.getRawLocation() &&
1198 "DbgVariableRecord location unexpectedly null");
1199 AddFnLocalMetadata(DVR.getRawLocation());
1200 if (DVR.isDbgAssign()) {
1201 assert(DVR.getRawAddress() &&
1202 "DbgVariableRecord location unexpectedly null");
1203 AddFnLocalMetadata(DVR.getRawAddress());
1204 }
1205 }
1206 if (!I.getType()->isVoidTy())
1207 EnumerateValue(&I);
1208 }
1209 }
1210
1211 // Add all of the function-local metadata.
1212 for (const LocalAsMetadata *Local : FnLocalMDVector) {
1213 // At this point, every local values have been incorporated, we shouldn't
1214 // have a metadata operand that references a value that hasn't been seen.
1215 assert(ValueMap.count(Local->getValue()) &&
1216 "Missing value for metadata operand");
1217 EnumerateFunctionLocalMetadata(F, Local);
1218 }
1219 // DIArgList entries must come after function-local metadata, as it is not
1220 // possible to forward-reference them.
1221 for (const DIArgList *ArgList : ArgListMDVector)
1222 EnumerateFunctionLocalListMetadata(F, ArgList);
1223}
1224
1226 /// Remove purged values from the ValueMap.
1227 for (const auto &V : llvm::drop_begin(Values, NumModuleValues))
1228 ValueMap.erase(V.first);
1229 for (const Metadata *MD : llvm::drop_begin(MDs, NumModuleMDs))
1230 MetadataMap.erase(MD);
1231 for (const BasicBlock *BB : BasicBlocks)
1232 ValueMap.erase(BB);
1233
1234 Values.resize(NumModuleValues);
1235 MDs.resize(NumModuleMDs);
1236 BasicBlocks.clear();
1237 NumMDStrings = 0;
1238}
1239
1242 unsigned Counter = 0;
1243 for (const BasicBlock &BB : *F)
1244 IDMap[&BB] = ++Counter;
1245}
1246
1247/// getGlobalBasicBlockID - This returns the function-specific ID for the
1248/// specified basic block. This is relatively expensive information, so it
1249/// should only be used by rare constructs such as address-of-label.
1251 unsigned &Idx = GlobalBasicBlockIDs[BB];
1252 if (Idx != 0)
1253 return Idx-1;
1254
1255 IncorporateFunctionInfoGlobalBBIDs(BB->getParent(), GlobalBasicBlockIDs);
1256 return getGlobalBasicBlockID(BB);
1257}
1258
for(const MachineOperand &MO :llvm::drop_begin(OldMI.operands(), Desc.getNumOperands()))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MapVector< const Value *, unsigned > OrderMap
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define LLVM_DUMP_METHOD
Mark debug helper function definitions like dump() that should not be stripped from debug builds.
Definition Compiler.h:702
dxil translate DXIL Translate Metadata
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
Hexagon Common GEP
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool lookup(const GsymReader &GR, GsymDataExtractor &Data, uint64_t &Offset, uint64_t BaseAddr, uint64_t Addr, SourceLocations &SrcLocs, llvm::Error &Err)
A Lookup helper functions.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
This file contains the declarations for metadata subclasses.
Type::TypeID TypeID
This file defines the SmallVector class.
static void predictValueUseListOrderImpl(const Value *V, const Function *F, unsigned ID, const OrderMap &OM, UseListOrderStack &Stack)
static unsigned getMetadataTypeOrder(const Metadata *MD)
static void orderValue(const Value *V, OrderMap &OM)
static void predictValueUseListOrder(const Value *V, const Function *F, OrderMap &OM, UseListOrderStack &Stack)
static UseListOrderStack predictUseListOrder(const Module &M)
static void IncorporateFunctionInfoGlobalBBIDs(const Function *F, DenseMap< const BasicBlock *, unsigned > &IDMap)
static bool isIntOrIntVectorValue(const std::pair< const Value *, unsigned > &V)
static OrderMap orderModule(const Module &M)
Value * RHS
Value * LHS
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
bool hasAttributes() const
Return true if attributes exists in this set.
Definition Attributes.h:526
LLVM Basic Block Representation.
Definition BasicBlock.h:61
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:212
This is an important base class in LLVM.
Definition Constant.h:44
List of ValueAsMetadata, to be used as an argument to a dbg.value intrinsic.
ArrayRef< ValueAsMetadata * > getArgs() const
Records a position in IR for a source label (DILabel).
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI DIAssignID * getAssignID() const
DIExpression * getExpression() const
DILocalVariable * getVariable() const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
DIExpression * getAddressExpression() const
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT, true > const_iterator
Definition DenseMap.h:679
unsigned size() const
Definition DenseMap.h:717
ValueT lookup(const_arg_type_t< KeyT > Val) const
Return the entry for the specified key, or a default constructed value if no such entry exists.
Definition DenseMap.h:793
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:856
Metadata node.
Definition Metadata.h:1087
const MDOperand * op_iterator
Definition Metadata.h:1430
unsigned & operator[](const const Value *&Key)
Definition MapVector.h:101
Root of the metadata hierarchy.
Definition Metadata.h:65
LLVM_ABI void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A tuple of MDNodes.
Definition Metadata.h:1806
iterator_range< op_iterator > operands()
Definition Metadata.h:1902
void reserve(size_type N)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
static LLVM_ABI Type * getMetadataTy(LLVMContext &C)
Definition Type.cpp:273
ArrayRef< Type * > subtypes() const
Definition Type.h:383
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
Value wrapper in the Metadata hierarchy.
Definition Metadata.h:472
unsigned getMetadataID(const Metadata *MD) const
UseListOrderStack UseListOrders
void print(raw_ostream &OS, const ValueMapType &Map, const char *Name) const
unsigned getInstructionID(const Instruction *I) const
void incorporateFunction(const Function &F)
incorporateFunction/purgeFunction - If you'd like to deal with a function, use these two methods to g...
ValueEnumerator(const Module &M, bool ShouldPreserveUseListOrder)
unsigned getComdatID(const Comdat *C) const
uint64_t computeBitsRequiredForTypeIndices() const
unsigned getValueID(const Value *V) const
unsigned getGlobalBasicBlockID(const BasicBlock *BB) const
getGlobalBasicBlockID - This returns the function-specific ID for the specified basic block.
void setInstructionID(const Instruction *I)
std::pair< unsigned, AttributeSet > IndexAndAttrSet
Attribute groups as encoded in bitcode are almost AttributeSets, but they include the AttributeList i...
const TypeList & getTypes() const
See the file comment.
Definition ValueMap.h:84
This class provides a symbol table of name/value pairs.
LLVM Value Representation.
Definition Value.h:75
iterator_range< use_iterator > uses()
Definition Value.h:382
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
CallInst * Call
@ Entry
Definition COFF.h:862
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:51
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
unsigned Log2_32_Ceil(uint32_t Value)
Return the ceil log base 2 of the specified value, 32 if the value is zero.
Definition MathExtras.h:339
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
void sort(IteratorTy Start, IteratorTy End)
Definition STLExtras.h:1652
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool is_sorted(R &&Range, Compare C)
Wrapper function around std::is_sorted to check if elements in a range R are sorted with respect to a...
Definition STLExtras.h:1986
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
std::vector< UseListOrder > UseListOrderStack
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
DWARFExpression::Operation Op
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
Function object to check whether the second component of a container supported by std::get (like std:...
Definition STLExtras.h:1464