LLVM 24.0.0git
AsmWriter.cpp
Go to the documentation of this file.
1//===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
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 library implements `print` family of functions in classes like
10// Module, Function, Value, etc. In-memory representation of those classes is
11// converted to IR strings.
12//
13// Note that these routines must be extremely tolerant of various errors in the
14// LLVM code, because it can be used for debugging transformations.
15//
16//===----------------------------------------------------------------------===//
17
18#include "IROptions.h"
19#include "LLVMContextImpl.h"
20#include "llvm/ADT/APFloat.h"
21#include "llvm/ADT/APInt.h"
22#include "llvm/ADT/ArrayRef.h"
23#include "llvm/ADT/DenseMap.h"
24#include "llvm/ADT/STLExtras.h"
30#include "llvm/ADT/StringRef.h"
33#include "llvm/Config/llvm-config.h"
34#include "llvm/IR/Argument.h"
36#include "llvm/IR/Attributes.h"
37#include "llvm/IR/BasicBlock.h"
38#include "llvm/IR/CFG.h"
39#include "llvm/IR/CallingConv.h"
40#include "llvm/IR/Comdat.h"
41#include "llvm/IR/Constant.h"
42#include "llvm/IR/Constants.h"
46#include "llvm/IR/Function.h"
47#include "llvm/IR/GlobalAlias.h"
48#include "llvm/IR/GlobalIFunc.h"
50#include "llvm/IR/GlobalValue.h"
53#include "llvm/IR/InlineAsm.h"
54#include "llvm/IR/InstrTypes.h"
55#include "llvm/IR/Instruction.h"
58#include "llvm/IR/Intrinsics.h"
59#include "llvm/IR/LLVMContext.h"
60#include "llvm/IR/Metadata.h"
61#include "llvm/IR/Module.h"
64#include "llvm/IR/Operator.h"
65#include "llvm/IR/Type.h"
66#include "llvm/IR/TypeFinder.h"
68#include "llvm/IR/Use.h"
69#include "llvm/IR/User.h"
70#include "llvm/IR/Value.h"
74#include "llvm/Support/Debug.h"
79#include <cassert>
80#include <cctype>
81#include <cstddef>
82#include <cstdint>
83#include <iterator>
84#include <memory>
85#include <optional>
86#include <string>
87#include <tuple>
88#include <utility>
89#include <vector>
90
91using namespace llvm;
92
93// Make virtual table appear in this compilation unit.
95
96//===----------------------------------------------------------------------===//
97// Helper Functions
98//===----------------------------------------------------------------------===//
99
101
104
105/// Look for a value that might be wrapped as metadata, e.g. a value in a
106/// metadata operand. Returns the input value as-is if it is not wrapped.
107static const Value *skipMetadataWrapper(const Value *V) {
108 if (const auto *MAV = dyn_cast<MetadataAsValue>(V))
109 if (const auto *VAM = dyn_cast<ValueAsMetadata>(MAV->getMetadata()))
110 return VAM->getValue();
111 return V;
112}
113
114static void orderValue(const Value *V, OrderMap &OM) {
115 if (OM.lookup(V))
116 return;
117
118 if (const auto *C = dyn_cast<Constant>(V)) {
119 if (isa<ConstantData>(C))
120 return;
121
122 if (C->getNumOperands() && !isa<GlobalValue>(C))
123 for (const Value *Op : C->operands())
125 orderValue(Op, OM);
126 }
127
128 // Note: we cannot cache this lookup above, since inserting into the map
129 // changes the map's size, and thus affects the other IDs.
130 unsigned ID = OM.size() + 1;
131 OM[V] = ID;
132}
133
134static OrderMap orderModule(const Module *M) {
135 OrderMap OM;
136
137 auto OrderConstantValue = [&OM](const Value *V) {
139 orderValue(V, OM);
140 };
141
142 auto OrderConstantFromMetadata = [&](Metadata *MD) {
143 if (const auto *VAM = dyn_cast<ValueAsMetadata>(MD)) {
144 OrderConstantValue(VAM->getValue());
145 } else if (const auto *AL = dyn_cast<DIArgList>(MD)) {
146 for (const auto *VAM : AL->getArgs())
147 OrderConstantValue(VAM->getValue());
148 }
149 };
150
151 for (const GlobalVariable &G : M->globals()) {
152 if (G.hasInitializer())
153 if (!isa<GlobalValue>(G.getInitializer()))
154 orderValue(G.getInitializer(), OM);
155 orderValue(&G, OM);
156 }
157 for (const GlobalAlias &A : M->aliases()) {
158 if (!isa<GlobalValue>(A.getAliasee()))
159 orderValue(A.getAliasee(), OM);
160 orderValue(&A, OM);
161 }
162 for (const GlobalIFunc &I : M->ifuncs()) {
163 if (!isa<GlobalValue>(I.getResolver()))
164 orderValue(I.getResolver(), OM);
165 orderValue(&I, OM);
166 }
167 for (const Function &F : *M) {
168 for (const Use &U : F.operands())
169 if (!isa<GlobalValue>(U.get()))
170 orderValue(U.get(), OM);
171
172 orderValue(&F, OM);
173
174 if (F.isDeclaration())
175 continue;
176
177 for (const Argument &A : F.args())
178 orderValue(&A, OM);
179 for (const BasicBlock &BB : F) {
180 orderValue(&BB, OM);
181 for (const Instruction &I : BB) {
182 // Debug records can contain Value references, that can then contain
183 // Values disconnected from the rest of the Value hierachy, if wrapped
184 // in some kind of constant-expression. Find and order any Values that
185 // are wrapped in debug-info.
186 for (DbgVariableRecord &DVR : filterDbgVars(I.getDbgRecordRange())) {
187 OrderConstantFromMetadata(DVR.getRawLocation());
188 if (DVR.isDbgAssign())
189 OrderConstantFromMetadata(DVR.getRawAddress());
190 }
191
192 for (const Value *Op : I.operands()) {
194 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
196 orderValue(Op, OM);
197 }
198 orderValue(&I, OM);
199 }
200 }
201 }
202 return OM;
203}
204
205static std::vector<unsigned>
206predictValueUseListOrder(const Value *V, unsigned ID, const OrderMap &OM) {
207 // Predict use-list order for this one.
208 using Entry = std::pair<const Use *, unsigned>;
210 for (const Use &U : V->uses())
211 // Check if this user will be serialized.
212 if (OM.lookup(U.getUser()))
213 List.push_back(std::make_pair(&U, List.size()));
214
215 if (List.size() < 2)
216 // We may have lost some users.
217 return {};
218
219 // When referencing a value before its declaration, a temporary value is
220 // created, which will later be RAUWed with the actual value. This reverses
221 // the use list. This happens for all values apart from basic blocks.
222 bool GetsReversed = !isa<BasicBlock>(V);
223 if (auto *BA = dyn_cast<BlockAddress>(V))
224 ID = OM.lookup(BA->getBasicBlock());
225 llvm::sort(List, [&](const Entry &L, const Entry &R) {
226 const Use *LU = L.first;
227 const Use *RU = R.first;
228 if (LU == RU)
229 return false;
230
231 auto LID = OM.lookup(LU->getUser());
232 auto RID = OM.lookup(RU->getUser());
233
234 // If ID is 4, then expect: 7 6 5 1 2 3.
235 if (LID < RID) {
236 if (GetsReversed)
237 if (RID <= ID)
238 return true;
239 return false;
240 }
241 if (RID < LID) {
242 if (GetsReversed)
243 if (LID <= ID)
244 return false;
245 return true;
246 }
247
248 // LID and RID are equal, so we have different operands of the same user.
249 // Assume operands are added in order for all instructions.
250 if (GetsReversed)
251 if (LID <= ID)
252 return LU->getOperandNo() < RU->getOperandNo();
253 return LU->getOperandNo() > RU->getOperandNo();
254 });
255
257 // Order is already correct.
258 return {};
259
260 // Store the shuffle.
261 std::vector<unsigned> Shuffle(List.size());
262 for (size_t I = 0, E = List.size(); I != E; ++I)
263 Shuffle[I] = List[I].second;
264 return Shuffle;
265}
266
268 OrderMap OM = orderModule(M);
269 UseListOrderMap ULOM;
270 for (const auto &Pair : OM) {
271 const Value *V = Pair.first;
272 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
273 continue;
274
275 std::vector<unsigned> Shuffle =
276 predictValueUseListOrder(V, Pair.second, OM);
277 if (Shuffle.empty())
278 continue;
279
280 const Function *F = nullptr;
281 if (auto *I = dyn_cast<Instruction>(V))
282 F = I->getFunction();
283 if (auto *A = dyn_cast<Argument>(V))
284 F = A->getParent();
285 if (auto *BB = dyn_cast<BasicBlock>(V))
286 F = BB->getParent();
287 ULOM[F][V] = std::move(Shuffle);
288 }
289 return ULOM;
290}
291
292static const Module *getModuleFromVal(const Value *V) {
293 if (const auto *MA = dyn_cast<Argument>(V))
294 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
295
296 if (const auto *BB = dyn_cast<BasicBlock>(V))
297 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
298
299 if (const auto *I = dyn_cast<Instruction>(V)) {
300 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
301 return M ? M->getParent() : nullptr;
302 }
303
304 if (const auto *GV = dyn_cast<GlobalValue>(V))
305 return GV->getParent();
306
307 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
308 for (const User *U : MAV->users())
309 if (isa<Instruction>(U))
310 if (const Module *M = getModuleFromVal(U))
311 return M;
312 return nullptr;
313 }
314
315 return nullptr;
316}
317
318static const Module *getModuleFromDPI(const DbgMarker *Marker) {
319 const Function *M =
320 Marker->getParent() ? Marker->getParent()->getParent() : nullptr;
321 return M ? M->getParent() : nullptr;
322}
323
324static const Module *getModuleFromDPI(const DbgRecord *DR) {
325 return DR->getMarker() ? getModuleFromDPI(DR->getMarker()) : nullptr;
326}
327
328static void printCallingConv(unsigned cc, raw_ostream &Out) {
329 switch (cc) {
330 default: Out << "cc" << cc; break;
331 case CallingConv::Fast: Out << "fastcc"; break;
332 case CallingConv::Cold: Out << "coldcc"; break;
333 case CallingConv::AnyReg: Out << "anyregcc"; break;
334 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
335 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
336 case CallingConv::PreserveNone: Out << "preserve_nonecc"; break;
337 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
338 case CallingConv::GHC: Out << "ghccc"; break;
339 case CallingConv::Tail: Out << "tailcc"; break;
340 case CallingConv::GRAAL: Out << "graalcc"; break;
341 case CallingConv::CFGuard_Check: Out << "cfguard_checkcc"; break;
342 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
343 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
344 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
345 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
346 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
347 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
348 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
349 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
350 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
351 case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
353 Out << "aarch64_sve_vector_pcs";
354 break;
356 Out << "aarch64_sme_preservemost_from_x0";
357 break;
359 Out << "aarch64_sme_preservemost_from_x1";
360 break;
362 Out << "aarch64_sme_preservemost_from_x2";
363 break;
364 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
365 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
366 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
367 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
368 case CallingConv::PTX_Device: Out << "ptx_device"; break;
369 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
370 case CallingConv::Win64: Out << "win64cc"; break;
371 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
372 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
373 case CallingConv::Swift: Out << "swiftcc"; break;
374 case CallingConv::SwiftTail: Out << "swifttailcc"; break;
375 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
377 Out << "hhvmcc";
378 break;
380 Out << "hhvm_ccc";
381 break;
382 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
383 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
384 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
385 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
386 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
387 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
388 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
390 Out << "amdgpu_cs_chain";
391 break;
393 Out << "amdgpu_cs_chain_preserve";
394 break;
395 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
396 case CallingConv::AMDGPU_Gfx: Out << "amdgpu_gfx"; break;
398 Out << "amdgpu_gfx_whole_wave";
399 break;
400 case CallingConv::M68k_RTD: Out << "m68k_rtdcc"; break;
402 Out << "wasm_emscripten_invokecc";
403 break;
405 Out << "riscv_vector_cc";
406 break;
407#define CC_VLS_CASE(ABI_VLEN) \
408 case CallingConv::RISCV_VLSCall_##ABI_VLEN: \
409 Out << "riscv_vls_cc(" #ABI_VLEN ")"; \
410 break;
411 CC_VLS_CASE(32)
412 CC_VLS_CASE(64)
413 CC_VLS_CASE(128)
414 CC_VLS_CASE(256)
415 CC_VLS_CASE(512)
416 CC_VLS_CASE(1024)
417 CC_VLS_CASE(2048)
418 CC_VLS_CASE(4096)
419 CC_VLS_CASE(8192)
420 CC_VLS_CASE(16384)
421 CC_VLS_CASE(32768)
422 CC_VLS_CASE(65536)
423#undef CC_VLS_CASE
425 Out << "cheriot_compartmentcallcc";
426 break;
428 Out << "cheriot_compartmentcalleecc";
429 break;
431 Out << "cheriot_librarycallcc";
432 break;
433 }
434}
435
443
445 assert(!Name.empty() && "Cannot get empty name!");
446
447 // Scan the name to see if it needs quotes first.
448 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
449 if (!NeedsQuotes) {
450 for (unsigned char C : Name) {
451 // By making this unsigned, the value passed in to isalnum will always be
452 // in the range 0-255. This is important when building with MSVC because
453 // its implementation will assert. This situation can arise when dealing
454 // with UTF-8 multibyte characters.
455 if (!isalnum(C) && C != '-' && C != '.' && C != '_') {
456 NeedsQuotes = true;
457 break;
458 }
459 }
460 }
461
462 // If we didn't need any quotes, just write out the name in one blast.
463 if (!NeedsQuotes) {
464 OS << Name;
465 return;
466 }
467
468 // Okay, we need quotes. Output the quotes and escape any scary characters as
469 // needed.
470 OS << '"';
471 printEscapedString(Name, OS);
472 OS << '"';
473}
474
475/// Turn the specified name into an 'LLVM name', which is either prefixed with %
476/// (if the string only contains simple characters) or is surrounded with ""'s
477/// (if it has special chars in it). Print it out.
478static void printLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
479 switch (Prefix) {
480 case NoPrefix:
481 break;
482 case GlobalPrefix:
483 OS << '@';
484 break;
485 case ComdatPrefix:
486 OS << '$';
487 break;
488 case LabelPrefix:
489 break;
490 case LocalPrefix:
491 OS << '%';
492 break;
493 }
495}
496
497/// Turn the specified name into an 'LLVM name', which is either prefixed with %
498/// (if the string only contains simple characters) or is surrounded with ""'s
499/// (if it has special chars in it). Print it out.
500static void printLLVMName(raw_ostream &OS, const Value *V) {
501 printLLVMName(OS, V->getName(),
503}
504
505static void printShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) {
506 Out << ", <";
508 Out << "vscale x ";
509 Out << Mask.size() << " x i32> ";
510 if (all_of(Mask, equal_to(0))) {
511 Out << "zeroinitializer";
512 } else if (all_of(Mask, equal_to(PoisonMaskElem))) {
513 Out << "poison";
514 } else {
515 Out << "<";
516 ListSeparator LS;
517 for (int Elt : Mask) {
518 Out << LS << "i32 ";
519 if (Elt == PoisonMaskElem)
520 Out << "poison";
521 else
522 Out << Elt;
523 }
524 Out << ">";
525 }
526}
527
528namespace {
529
530class TypePrinting {
531public:
532 TypePrinting(const Module *M = nullptr)
533 : M(M), TypesIncorporated(M == nullptr) {}
534
535 TypePrinting(const TypePrinting &) = delete;
536 TypePrinting &operator=(const TypePrinting &) = delete;
537
538 /// The named types that are used by the current module.
539 TypeFinder &getNamedTypes();
540
541 /// The numbered types, number to type mapping.
542 std::vector<StructType *> &getNumberedTypes();
543
544 bool empty();
545
546 void print(Type *Ty, raw_ostream &OS);
547
548 void printStructBody(StructType *Ty, raw_ostream &OS);
549
550private:
551 void incorporateTypes();
552
553 /// A module to process lazily.
554 const Module *M;
555 bool TypesIncorporated;
556
557 TypeFinder NamedTypes;
558
559 // The numbered types, along with their value.
560 DenseMap<StructType *, unsigned> Type2Number;
561
562 std::vector<StructType *> NumberedTypes;
563};
564
565} // end anonymous namespace
566
567TypeFinder &TypePrinting::getNamedTypes() {
568 incorporateTypes();
569 return NamedTypes;
570}
571
572std::vector<StructType *> &TypePrinting::getNumberedTypes() {
573 incorporateTypes();
574
575 // We know all the numbers that each type is used and we know that it is a
576 // dense assignment. Convert the map to an index table, if it's not done
577 // already (judging from the sizes):
578 if (NumberedTypes.size() == Type2Number.size())
579 return NumberedTypes;
580
581 NumberedTypes.resize(Type2Number.size());
582 for (const auto &P : Type2Number) {
583 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
584 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
585 NumberedTypes[P.second] = P.first;
586 }
587 return NumberedTypes;
588}
589
590bool TypePrinting::empty() {
591 incorporateTypes();
592 return NamedTypes.empty() && Type2Number.empty();
593}
594
595void TypePrinting::incorporateTypes() {
596 if (TypesIncorporated)
597 return;
598
599 NamedTypes.run(*M, false);
600 TypesIncorporated = true;
601
602 // The list of struct types we got back includes all the struct types, split
603 // the unnamed ones out to a numbering and remove the anonymous structs.
604 unsigned NextNumber = 0;
605
606 std::vector<StructType *>::iterator NextToUse = NamedTypes.begin();
607 for (StructType *STy : NamedTypes) {
608 // Ignore anonymous types.
609 if (STy->isLiteral())
610 continue;
611
612 if (STy->getName().empty())
613 Type2Number[STy] = NextNumber++;
614 else
615 *NextToUse++ = STy;
616 }
617
618 NamedTypes.erase(NextToUse, NamedTypes.end());
619}
620
621static void printAddressSpace(const Module *M, unsigned AS, raw_ostream &OS,
622 StringRef Prefix = " ", StringRef Suffix = "",
623 bool ForcePrint = false) {
624 if (AS == 0 && !ForcePrint)
625 return;
626 OS << Prefix << "addrspace(";
627 StringRef ASName = M && getIROptions(M->getContext()).print_addrspace_name
628 ? M->getDataLayout().getAddressSpaceName(AS)
629 : "";
630 if (!ASName.empty())
631 OS << "\"" << ASName << "\"";
632 else
633 OS << AS;
634 OS << ")" << Suffix;
635}
636
637/// Write the specified type to the specified raw_ostream, making use of type
638/// names or up references to shorten the type name where possible.
639void TypePrinting::print(Type *Ty, raw_ostream &OS) {
640 switch (Ty->getTypeID()) {
641 case Type::VoidTyID: OS << "void"; return;
642 case Type::HalfTyID: OS << "half"; return;
643 case Type::BFloatTyID: OS << "bfloat"; return;
644 case Type::FloatTyID: OS << "float"; return;
645 case Type::DoubleTyID: OS << "double"; return;
646 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
647 case Type::FP128TyID: OS << "fp128"; return;
648 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
649 case Type::LabelTyID: OS << "label"; return;
650 case Type::MetadataTyID:
651 OS << "metadata";
652 return;
653 case Type::X86_AMXTyID: OS << "x86_amx"; return;
654 case Type::TokenTyID: OS << "token"; return;
655 case Type::ByteTyID:
656 OS << 'b' << Ty->getByteBitWidth();
657 return;
658 case Type::IntegerTyID:
659 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
660 return;
661
662 case Type::FunctionTyID: {
663 FunctionType *FTy = cast<FunctionType>(Ty);
664 print(FTy->getReturnType(), OS);
665 OS << " (";
666 ListSeparator LS;
667 for (Type *Ty : FTy->params()) {
668 OS << LS;
669 print(Ty, OS);
670 }
671 if (FTy->isVarArg())
672 OS << LS << "...";
673 OS << ')';
674 return;
675 }
676 case Type::StructTyID: {
677 StructType *STy = cast<StructType>(Ty);
678
679 if (STy->isLiteral())
680 return printStructBody(STy, OS);
681
682 if (!STy->getName().empty())
683 return printLLVMName(OS, STy->getName(), LocalPrefix);
684
685 incorporateTypes();
686 const auto I = Type2Number.find(STy);
687 if (I != Type2Number.end())
688 OS << '%' << I->second;
689 else // Not enumerated, print the hex address.
690 OS << "%\"type " << STy << '\"';
691 return;
692 }
693 case Type::PointerTyID: {
695 OS << "ptr";
696 printAddressSpace(M, PTy->getAddressSpace(), OS);
697 return;
698 }
699 case Type::ArrayTyID: {
700 ArrayType *ATy = cast<ArrayType>(Ty);
701 OS << '[' << ATy->getNumElements() << " x ";
702 print(ATy->getElementType(), OS);
703 OS << ']';
704 return;
705 }
706 case Type::FixedVectorTyID:
707 case Type::ScalableVectorTyID: {
708 VectorType *PTy = cast<VectorType>(Ty);
709 ElementCount EC = PTy->getElementCount();
710 OS << "<";
711 if (EC.isScalable())
712 OS << "vscale x ";
713 OS << EC.getKnownMinValue() << " x ";
714 print(PTy->getElementType(), OS);
715 OS << '>';
716 return;
717 }
718 case Type::TypedPointerTyID: {
719 TypedPointerType *TPTy = cast<TypedPointerType>(Ty);
720 OS << "typedptr(" << *TPTy->getElementType() << ", "
721 << TPTy->getAddressSpace() << ")";
722 return;
723 }
724 case Type::TargetExtTyID:
725 TargetExtType *TETy = cast<TargetExtType>(Ty);
726 OS << "target(\"";
728 OS << "\"";
729 for (Type *Inner : TETy->type_params()) {
730 OS << ", ";
731 Inner->print(OS, /*IsForDebug=*/false, /*NoDetails=*/true);
732 }
733 for (unsigned IntParam : TETy->int_params())
734 OS << ", " << IntParam;
735 OS << ")";
736 return;
737 }
738 llvm_unreachable("Invalid TypeID");
739}
740
741void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
742 if (STy->isOpaque()) {
743 OS << "opaque";
744 return;
745 }
746
747 if (STy->isPacked())
748 OS << '<';
749
750 if (STy->getNumElements() == 0) {
751 OS << "{}";
752 } else {
753 OS << "{ ";
754 ListSeparator LS;
755 for (Type *Ty : STy->elements()) {
756 OS << LS;
757 print(Ty, OS);
758 }
759
760 OS << " }";
761 }
762 if (STy->isPacked())
763 OS << '>';
764}
765
767
768//===----------------------------------------------------------------------===//
769// SlotTracker Class: Enumerate slot numbers for unnamed values
770//===----------------------------------------------------------------------===//
771/// This class provides computation of slot numbers for LLVM Assembly writing.
772///
774public:
775 /// ValueMap - A mapping of Values to slot numbers.
777
778private:
779 /// TheModule - The module for which we are holding slot numbers.
780 const Module* TheModule;
781
782 /// TheFunction - The function for which we are holding slot numbers.
783 const Function* TheFunction = nullptr;
784 bool FunctionProcessed = false;
785 bool ShouldTrackMetadataDefinitions;
786
787 std::function<void(AbstractSlotTrackerStorage *, const Module *)>
788 ProcessModuleHookFn;
789 std::function<void(AbstractSlotTrackerStorage *, const Function *)>
790 ProcessFunctionHookFn;
791
792 /// The summary index for which we are holding slot numbers.
793 const ModuleSummaryIndex *TheIndex = nullptr;
794
795 /// mMap - The slot map for the module level data.
796 ValueMap mMap;
797 unsigned mNext = 0;
798
799 /// fMap - The slot map for the function level data.
800 ValueMap fMap;
801 unsigned fNext = 0;
802
803 /// mdnMap - Map for MDNodes.
805 /// asMap - The slot map for attribute sets.
807 unsigned asNext = 0;
808
809 /// ModulePathMap - The slot map for Module paths used in the summary index.
810 StringMap<unsigned> ModulePathMap;
811 unsigned ModulePathNext = 0;
812
813 /// GUIDMap - The slot map for GUIDs used in the summary index.
815 unsigned GUIDNext = 0;
816
817 /// TypeIdMap - The slot map for type ids used in the summary index.
818 StringMap<unsigned> TypeIdMap;
819 unsigned TypeIdNext = 0;
820
821 /// TypeIdCompatibleVtableMap - The slot map for type compatible vtable ids
822 /// used in the summary index.
823 StringMap<unsigned> TypeIdCompatibleVtableMap;
824 unsigned TypeIdCompatibleVtableNext = 0;
825
826public:
827 /// Construct from a module.
828 ///
829 explicit SlotTracker(const Module *M,
830 bool ShouldTrackMetadataDefinitions = false);
831
832 /// Construct from a function, starting out in incorp state.
833 ///
834 explicit SlotTracker(const Function *F);
835
836 /// Construct from a module summary index.
837 explicit SlotTracker(const ModuleSummaryIndex *Index);
838
839 SlotTracker(const SlotTracker &) = delete;
841
842 ~SlotTracker() override = default;
843
844 void setProcessHook(
845 std::function<void(AbstractSlotTrackerStorage *, const Module *)>);
846 void setProcessHook(
847 std::function<void(AbstractSlotTrackerStorage *, const Function *)>);
848
849 void createMetadataSlot(const MDNode *N) override;
850
851 /// Return the slot number of the specified value in it's type
852 /// plane. If something is not in the SlotTracker, return -1.
853 int getLocalSlot(const Value *V);
854 int getGlobalSlot(const GlobalValue *V);
855 int getMetadataSlot(const MDNode *N) override;
859 int getTypeIdSlot(StringRef Id);
861
862 /// If you'd like to deal with a function instead of just a module, use
863 /// this method to get its data into the SlotTracker.
865 TheFunction = F;
866 FunctionProcessed = false;
867 }
868
869 const Function *getFunction() const { return TheFunction; }
870
871 /// After calling incorporateFunction, use this method to remove the
872 /// most recently incorporated function from the SlotTracker. This
873 /// will reset the state of the machine back to just the module contents.
874 void purgeFunction();
875
876 /// MDNode map iterators.
878
879 mdn_iterator mdn_begin() { return mdnMap.begin(); }
880 mdn_iterator mdn_end() { return mdnMap.end(); }
881 unsigned mdn_size() const { return mdnMap.size(); }
882 bool mdn_empty() const { return mdnMap.empty(); }
883
884 /// AttributeSet map iterators.
886
887 as_iterator as_begin() { return asMap.begin(); }
888 as_iterator as_end() { return asMap.end(); }
889 unsigned as_size() const { return asMap.size(); }
890 bool as_empty() const { return asMap.empty(); }
891
892 /// GUID map iterators.
894
895 /// These functions do the actual initialization.
896 inline void initializeIfNeeded();
898
899 // Implementation Details
900private:
901 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
902 void CreateModuleSlot(const GlobalValue *V);
903
904 /// Record a metadata definition and the metadata nodes referenced by it.
905 void CreateMetadataSlot(const MDNode *N);
906
907 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
908 void CreateFunctionSlot(const Value *V);
909
910 /// Insert the specified AttributeSet into the slot table.
911 void CreateAttributeSetSlot(AttributeSet AS);
912
913 inline void CreateModulePathSlot(StringRef Path);
914 void CreateGUIDSlot(GlobalValue::GUID GUID);
915 void CreateTypeIdSlot(StringRef Id);
916 void CreateTypeIdCompatibleVtableSlot(StringRef Id);
917
918 /// Add all of the module level global variables (and their initializers)
919 /// and function declarations, but not the contents of those functions.
920 void processModule();
921 // Returns number of allocated slots
922 int processIndex();
923
924 /// Add all of the functions arguments, basic blocks, and instructions.
925 void processFunction();
926};
927
929 const Function *F)
930 : M(M), F(F), Machine(&Machine) {}
931
933 : ShouldCreateStorage(M), M(M) {}
934
936
938 if (!ShouldCreateStorage)
939 return Machine;
940
941 ShouldCreateStorage = false;
942 MachineStorage = std::make_unique<SlotTracker>(M);
943 Machine = MachineStorage.get();
944 if (ProcessModuleHookFn)
945 Machine->setProcessHook(ProcessModuleHookFn);
946 if (ProcessFunctionHookFn)
947 Machine->setProcessHook(ProcessFunctionHookFn);
948 return Machine;
949}
950
952 // Using getMachine() may lazily create the slot tracker.
953 if (!getMachine())
954 return;
955
956 // Nothing to do if this is the right function already.
957 if (this->F == &F)
958 return;
959 if (this->F)
960 Machine->purgeFunction();
961 Machine->incorporateFunction(&F);
962 this->F = &F;
963}
964
966 assert(F && "No function incorporated");
967 return Machine->getLocalSlot(V);
968}
969
971 std::function<void(AbstractSlotTrackerStorage *, const Module *)> Fn) {
972 ProcessModuleHookFn = std::move(Fn);
973}
974
976 std::function<void(AbstractSlotTrackerStorage *, const Function *)> Fn) {
977 ProcessFunctionHookFn = std::move(Fn);
978}
979
981 if (const auto *FA = dyn_cast<Argument>(V))
982 return new SlotTracker(FA->getParent());
983
984 if (const auto *I = dyn_cast<Instruction>(V))
985 if (I->getParent())
986 return new SlotTracker(I->getParent()->getParent());
987
988 if (const auto *BB = dyn_cast<BasicBlock>(V))
989 return new SlotTracker(BB->getParent());
990
991 if (const auto *GV = dyn_cast<GlobalVariable>(V))
992 return new SlotTracker(GV->getParent());
993
994 if (const auto *GA = dyn_cast<GlobalAlias>(V))
995 return new SlotTracker(GA->getParent());
996
997 if (const auto *GIF = dyn_cast<GlobalIFunc>(V))
998 return new SlotTracker(GIF->getParent());
999
1000 if (const auto *Func = dyn_cast<Function>(V))
1001 return new SlotTracker(Func);
1002
1003 return nullptr;
1004}
1005
1006#if 0
1007#define ST_DEBUG(X) dbgs() << X
1008#else
1009#define ST_DEBUG(X)
1010#endif
1011
1012// Module level constructor. Causes the contents of the Module (sans functions)
1013// to be added to the slot table.
1014SlotTracker::SlotTracker(const Module *M, bool ShouldTrackMetadataDefinitions)
1015 : TheModule(M),
1016 ShouldTrackMetadataDefinitions(ShouldTrackMetadataDefinitions) {}
1017
1018// Function level constructor. Causes the contents of the Module and the one
1019// function provided to be added to the slot table.
1021 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
1022 ShouldTrackMetadataDefinitions(false) {}
1023
1025 : TheModule(nullptr), ShouldTrackMetadataDefinitions(false),
1026 TheIndex(Index) {}
1027
1029 if (TheModule) {
1030 processModule();
1031 TheModule = nullptr; ///< Prevent re-processing next time we're called.
1032 }
1033
1034 if (TheFunction && !FunctionProcessed)
1035 processFunction();
1036}
1037
1039 if (!TheIndex)
1040 return 0;
1041 int NumSlots = processIndex();
1042 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
1043 return NumSlots;
1044}
1045
1046// Iterate through all the global variables, functions, and global
1047// variable initializers and create slots for them.
1048void SlotTracker::processModule() {
1049 ST_DEBUG("begin processModule!\n");
1050
1051 // Add all of the unnamed global variables to the value table.
1052 for (const GlobalVariable &Var : TheModule->globals()) {
1053 if (!Var.hasName())
1054 CreateModuleSlot(&Var);
1055 auto Attrs = Var.getAttributes();
1056 if (Attrs.hasAttributes())
1057 CreateAttributeSetSlot(Attrs);
1058 }
1059
1060 for (const GlobalAlias &A : TheModule->aliases()) {
1061 if (!A.hasName())
1062 CreateModuleSlot(&A);
1063 }
1064
1065 for (const GlobalIFunc &I : TheModule->ifuncs()) {
1066 if (!I.hasName())
1067 CreateModuleSlot(&I);
1068 }
1069
1070 for (const Function &F : *TheModule) {
1071 if (!F.hasName())
1072 // Add all the unnamed functions to the table.
1073 CreateModuleSlot(&F);
1074
1075 // Add all the function attributes to the table.
1076 // FIXME: Add attributes of other objects?
1077 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
1078 if (FnAttrs.hasAttributes())
1079 CreateAttributeSetSlot(FnAttrs);
1080 }
1081
1082 if (ProcessModuleHookFn)
1083 ProcessModuleHookFn(this, TheModule);
1084
1085 ST_DEBUG("end processModule!\n");
1086}
1087
1088// Process the arguments, basic blocks, and instructions of a function.
1089void SlotTracker::processFunction() {
1090 ST_DEBUG("begin processFunction!\n");
1091 fNext = 0;
1092
1093 // Add all the function arguments with no names.
1094 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
1095 AE = TheFunction->arg_end(); AI != AE; ++AI)
1096 if (!AI->hasName())
1097 CreateFunctionSlot(&*AI);
1098
1099 ST_DEBUG("Inserting Instructions:\n");
1100
1101 // Add all of the basic blocks and instructions with no names.
1102 for (auto &BB : *TheFunction) {
1103 if (!BB.hasName())
1104 CreateFunctionSlot(&BB);
1105
1106 for (auto &I : BB) {
1107 if (!I.getType()->isVoidTy() && !I.hasName())
1108 CreateFunctionSlot(&I);
1109
1110 // We allow direct calls to any llvm.foo function here, because the
1111 // target may not be linked into the optimizer.
1112 if (const auto *Call = dyn_cast<CallBase>(&I)) {
1113 // Add all the call attributes to the table.
1114 AttributeSet Attrs = Call->getAttributes().getFnAttrs();
1115 if (Attrs.hasAttributes())
1116 CreateAttributeSetSlot(Attrs);
1117 }
1118 }
1119 }
1120
1121 if (ProcessFunctionHookFn)
1122 ProcessFunctionHookFn(this, TheFunction);
1123
1124 FunctionProcessed = true;
1125
1126 ST_DEBUG("end processFunction!\n");
1127}
1128
1129// Iterate through all the GUID in the index and create slots for them.
1130int SlotTracker::processIndex() {
1131 ST_DEBUG("begin processIndex!\n");
1132 assert(TheIndex);
1133
1134 // The first block of slots are just the module ids, which start at 0 and are
1135 // assigned consecutively. Since the StringMap iteration order isn't
1136 // guaranteed, order by path string before assigning slots.
1137 std::vector<StringRef> ModulePaths;
1138 for (auto &[ModPath, _] : TheIndex->modulePaths())
1139 ModulePaths.push_back(ModPath);
1140 llvm::sort(ModulePaths);
1141 for (auto &ModPath : ModulePaths)
1142 CreateModulePathSlot(ModPath);
1143
1144 // Start numbering the GUIDs after the module ids.
1145 GUIDNext = ModulePathNext;
1146
1147 // Sort by GUID for deterministic slot assignment.
1148 for (const auto &GlobalList : TheIndex->sortedGlobalValueSummariesRange())
1149 CreateGUIDSlot(GlobalList.first);
1150
1151 // Start numbering the TypeIdCompatibleVtables after the GUIDs.
1152 TypeIdCompatibleVtableNext = GUIDNext;
1153 for (auto &TId : TheIndex->typeIdCompatibleVtableMap())
1154 CreateTypeIdCompatibleVtableSlot(TId.first);
1155
1156 // Start numbering the TypeIds after the TypeIdCompatibleVtables.
1157 TypeIdNext = TypeIdCompatibleVtableNext;
1158 for (const auto &TID : TheIndex->typeIds())
1159 CreateTypeIdSlot(TID.second.first);
1160
1161 ST_DEBUG("end processIndex!\n");
1162 return TypeIdNext;
1163}
1164
1165namespace {
1166class MetadataNodeVisitor {
1167 /// Visited MDNodes.
1168 SmallPtrSet<const MDNode *, 32> VisitedMDNodes;
1169 function_ref<void(const MDNode *)> Visit;
1170
1171 void visit(const MDNode *N) {
1172 if (isa<DIExpression>(N) || !VisitedMDNodes.insert(N).second)
1173 return;
1174
1175 Visit(N);
1176 for (const MDOperand &Op : N->operands())
1177 if (const auto *OpNode = dyn_cast_or_null<MDNode>(Op.get()))
1178 visit(OpNode);
1179 }
1180
1181 void visitGlobalObjectMetadata(const GlobalObject &GO) {
1183 GO.getAllMetadata(MDs);
1184 for (auto &MD : MDs)
1185 visit(MD.second);
1186 }
1187
1188 void visitDbgRecordMetadata(const DbgRecord &DR) {
1189 if (const auto *DVR = dyn_cast<const DbgVariableRecord>(&DR)) {
1190 if (auto *Empty = dyn_cast_if_present<MDNode>(DVR->getRawLocation()))
1191 visit(Empty);
1192 if (DVR->getRawVariable())
1193 visit(DVR->getRawVariable());
1194 if (DVR->isDbgAssign()) {
1195 if (auto *AssignID = DVR->getRawAssignID())
1196 visit(cast<MDNode>(AssignID));
1197 if (auto *Empty = dyn_cast_if_present<MDNode>(DVR->getRawAddress()))
1198 visit(Empty);
1199 }
1200 } else if (const auto *DLR = dyn_cast<const DbgLabelRecord>(&DR)) {
1201 visit(DLR->getRawLabel());
1202 } else {
1203 llvm_unreachable("unsupported DbgRecord kind");
1204 }
1205 if (DR.getDebugLoc())
1207 }
1208
1209 void visitInstructionMetadata(const Instruction &I) {
1210 if (const auto *CI = dyn_cast<CallInst>(&I))
1211 if (Function *F = CI->getCalledFunction())
1212 if (F->isIntrinsic())
1213 for (auto &Op : I.operands())
1215 if (auto *N = dyn_cast<MDNode>(V->getMetadata()))
1216 visit(N);
1217
1219 I.getAllMetadata(MDs);
1220 for (auto &MD : MDs)
1221 visit(MD.second);
1222 }
1223
1224 void visitFunctionMetadata(const Function &F) {
1225 visitGlobalObjectMetadata(F);
1226 for (const BasicBlock &BB : F)
1227 for (const Instruction &I : BB) {
1228 for (const DbgRecord &DR : I.getDbgRecordRange())
1229 visitDbgRecordMetadata(DR);
1230 visitInstructionMetadata(I);
1231 }
1232 }
1233
1234public:
1235 MetadataNodeVisitor(function_ref<void(const MDNode *)> Visit)
1236 : Visit(Visit) {}
1237
1238 void visitModuleMetadata(const Module &M) {
1239 for (const GlobalVariable &Var : M.globals())
1240 visitGlobalObjectMetadata(Var);
1241 for (const GlobalIFunc &I : M.ifuncs())
1242 visitGlobalObjectMetadata(I);
1243 for (const NamedMDNode &NMD : M.named_metadata())
1244 for (const MDNode *N : NMD.operands())
1245 visit(N);
1246 for (const Function &F : M)
1247 visitFunctionMetadata(F);
1248 }
1249
1250 void visitMetadata(ArrayRef<const MDNode *> Metadata) {
1251 for (const MDNode *N : Metadata)
1252 visit(N);
1253 }
1254
1255 bool contains(const MDNode *N) const { return VisitedMDNodes.contains(N); }
1256};
1257
1258class MetadataIDRenumberer {
1259 uint32_t NextID = 0;
1260
1261public:
1262 void run(const Module &M, ArrayRef<const MDNode *> AdditionalMetadata,
1263 ModuleSlotTracker::MachineMDNodeListType *AdditionalMetadataNodes =
1264 nullptr) {
1265 bool IsAdditionalMetadata = false;
1266 auto Renumber = [&](const MDNode *N) {
1267 N->getContext().pImpl->setMetadataPrintID(const_cast<MDNode *>(N),
1268 NextID++);
1269 if (IsAdditionalMetadata && AdditionalMetadataNodes)
1270 AdditionalMetadataNodes->emplace_back(
1271 N->getContext().pImpl->getMetadataPrintID(N), N);
1272 };
1273 MetadataNodeVisitor Visitor(Renumber);
1274
1275 Visitor.visitModuleMetadata(M);
1276
1277 IsAdditionalMetadata = true;
1278 Visitor.visitMetadata(AdditionalMetadata);
1279
1280 // Keep IDs unique for nodes outside the canonical output.
1281 SmallVector<MDNode *, 32> RemainingNodes;
1282 M.getContext().pImpl->getAllMetadataNodes(RemainingNodes);
1283 llvm::erase_if(RemainingNodes, [&](const MDNode *N) {
1284 return Visitor.contains(N) ||
1285 M.getContext().pImpl->getMetadataPrintID(N) >= NextID;
1286 });
1287 llvm::sort(RemainingNodes, [&](const MDNode *LHS, const MDNode *RHS) {
1288 return M.getContext().pImpl->getMetadataPrintID(LHS) <
1289 M.getContext().pImpl->getMetadataPrintID(RHS);
1290 });
1291 for (MDNode *N : RemainingNodes)
1292 M.getContext().pImpl->setMetadataPrintID(
1293 N, M.getContext().pImpl->allocateMetadataPrintID());
1294
1295 if (AdditionalMetadataNodes)
1296 llvm::sort(*AdditionalMetadataNodes);
1297 }
1298};
1299} // namespace
1300
1302 MetadataIDRenumberer().run(*this, {});
1303}
1304
1306 ArrayRef<const MDNode *> AdditionalMetadata,
1307 MachineMDNodeListType *AdditionalMetadataNodes) const {
1308 assert(M && "metadata renumbering requires a module");
1309 MetadataIDRenumberer().run(*M, AdditionalMetadata, AdditionalMetadataNodes);
1310}
1311
1313 ArrayRef<const MDNode *> AdditionalMetadata,
1314 MachineMDNodeListType &AdditionalMetadataNodes) const {
1315 assert(M && "metadata collection requires a module");
1316 bool IsAdditionalMetadata = false;
1317 auto Collect = [&](const MDNode *N) {
1318 if (IsAdditionalMetadata)
1319 AdditionalMetadataNodes.emplace_back(
1320 N->getContext().pImpl->getMetadataPrintID(N), N);
1321 };
1322 MetadataNodeVisitor Visitor(Collect);
1323 Visitor.visitModuleMetadata(*M);
1324 IsAdditionalMetadata = true;
1325 Visitor.visitMetadata(AdditionalMetadata);
1326 llvm::sort(AdditionalMetadataNodes);
1327}
1328
1329/// Clean up after incorporating a function. This is the only way to get out of
1330/// the function incorporation state that affects get*Slot/Create*Slot. Function
1331/// incorporation state is indicated by TheFunction != 0.
1333 ST_DEBUG("begin purgeFunction!\n");
1334 fMap.clear(); // Simply discard the function level map
1335 TheFunction = nullptr;
1336 FunctionProcessed = false;
1337 ST_DEBUG("end purgeFunction!\n");
1338}
1339
1340/// getGlobalSlot - Get the slot number of a global value.
1342 // Check for uninitialized state and do lazy initialization.
1344
1345 // Find the value in the module map
1346 ValueMap::iterator MI = mMap.find(V);
1347 return MI == mMap.end() ? -1 : (int)MI->second;
1348}
1349
1351 std::function<void(AbstractSlotTrackerStorage *, const Module *)> Fn) {
1352 ProcessModuleHookFn = std::move(Fn);
1353}
1354
1356 std::function<void(AbstractSlotTrackerStorage *, const Function *)> Fn) {
1357 ProcessFunctionHookFn = std::move(Fn);
1358}
1359
1360/// getMetadataSlot - Get the slot number of a MDNode.
1361void SlotTracker::createMetadataSlot(const MDNode *N) { CreateMetadataSlot(N); }
1362
1363/// getMetadataSlot - Get the slot number of a MDNode.
1365 // Check for uninitialized state and do lazy initialization.
1367
1368 if (isa<DIExpression>(N))
1369 return -1;
1370 if (ShouldTrackMetadataDefinitions)
1371 CreateMetadataSlot(N);
1372 return N->getContext().pImpl->getMetadataPrintID(N);
1373}
1374
1375/// getLocalSlot - Get the slot number for a value that is local to a function.
1377 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1378
1379 // Check for uninitialized state and do lazy initialization.
1381
1382 ValueMap::iterator FI = fMap.find(V);
1383 return FI == fMap.end() ? -1 : (int)FI->second;
1384}
1385
1387 // Check for uninitialized state and do lazy initialization.
1389
1390 // Find the AttributeSet in the module map.
1391 as_iterator AI = asMap.find(AS);
1392 return AI == asMap.end() ? -1 : (int)AI->second;
1393}
1394
1396 // Check for uninitialized state and do lazy initialization.
1398
1399 // Find the Module path in the map
1400 auto I = ModulePathMap.find(Path);
1401 return I == ModulePathMap.end() ? -1 : (int)I->second;
1402}
1403
1405 // Check for uninitialized state and do lazy initialization.
1407
1408 // Find the GUID in the map
1409 guid_iterator I = GUIDMap.find(GUID);
1410 return I == GUIDMap.end() ? -1 : (int)I->second;
1411}
1412
1414 // Check for uninitialized state and do lazy initialization.
1416
1417 // Find the TypeId string in the map
1418 auto I = TypeIdMap.find(Id);
1419 return I == TypeIdMap.end() ? -1 : (int)I->second;
1420}
1421
1423 // Check for uninitialized state and do lazy initialization.
1425
1426 // Find the TypeIdCompatibleVtable string in the map
1427 auto I = TypeIdCompatibleVtableMap.find(Id);
1428 return I == TypeIdCompatibleVtableMap.end() ? -1 : (int)I->second;
1429}
1430
1431/// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1432void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1433 assert(V && "Can't insert a null Value into SlotTracker!");
1434 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
1435 assert(!V->hasName() && "Doesn't need a slot!");
1436
1437 unsigned DestSlot = mNext++;
1438 mMap[V] = DestSlot;
1439
1440 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1441 DestSlot << " [");
1442 // G = Global, F = Function, A = Alias, I = IFunc, o = other
1443 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1444 (isa<Function>(V) ? 'F' :
1445 (isa<GlobalAlias>(V) ? 'A' :
1446 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
1447}
1448
1449/// CreateSlot - Create a new slot for the specified value if it has no name.
1450void SlotTracker::CreateFunctionSlot(const Value *V) {
1451 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
1452
1453 unsigned DestSlot = fNext++;
1454 fMap[V] = DestSlot;
1455
1456 // G = Global, F = Function, o = other
1457 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1458 DestSlot << " [o]\n");
1459}
1460
1461/// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1462void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1463 assert(N && "Can't insert a null Value into SlotTracker!");
1464
1465 if (isa<DIExpression>(N))
1466 return;
1467
1468 unsigned ID = N->getContext().pImpl->getMetadataPrintID(N);
1469 if (!mdnMap.try_emplace(N, ID).second)
1470 return;
1471
1472 for (const MDOperand &Op : N->operands())
1473 if (const auto *OpNode = dyn_cast_or_null<MDNode>(Op.get()))
1474 CreateMetadataSlot(OpNode);
1475}
1476
1477void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1478 assert(AS.hasAttributes() && "Doesn't need a slot!");
1479
1480 if (asMap.try_emplace(AS, asNext).second)
1481 ++asNext;
1482}
1483
1484/// Create a new slot for the specified Module
1485void SlotTracker::CreateModulePathSlot(StringRef Path) {
1486 ModulePathMap[Path] = ModulePathNext++;
1487}
1488
1489/// Create a new slot for the specified GUID
1490void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1491 GUIDMap[GUID] = GUIDNext++;
1492}
1493
1494/// Create a new slot for the specified Id
1495void SlotTracker::CreateTypeIdSlot(StringRef Id) {
1496 TypeIdMap[Id] = TypeIdNext++;
1497}
1498
1499/// Create a new slot for the specified Id
1500void SlotTracker::CreateTypeIdCompatibleVtableSlot(StringRef Id) {
1501 TypeIdCompatibleVtableMap[Id] = TypeIdCompatibleVtableNext++;
1502}
1503
1504namespace {
1505/// Common instances used by most of the printer functions.
1506struct AsmWriterContext {
1507 TypePrinting *TypePrinter = nullptr;
1508 SlotTracker *Machine = nullptr;
1509 const Module *Context = nullptr;
1510 const ModuleSlotTracker *MST = nullptr;
1511
1512 AsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M = nullptr,
1513 const ModuleSlotTracker *MST = nullptr)
1514 : TypePrinter(TP), Machine(ST), Context(M), MST(MST) {}
1515
1516 static AsmWriterContext &getEmpty() {
1517 static AsmWriterContext EmptyCtx(nullptr, nullptr);
1518 return EmptyCtx;
1519 }
1520
1521 /// A callback that will be triggered when the underlying printer
1522 /// prints a Metadata as operand.
1523 virtual void onWriteMetadataAsOperand(const Metadata *) {}
1524
1525 virtual ~AsmWriterContext() = default;
1526};
1527} // end anonymous namespace
1528
1529//===----------------------------------------------------------------------===//
1530// AsmWriter Implementation
1531//===----------------------------------------------------------------------===//
1532
1533static void writeAsOperandInternal(raw_ostream &Out, const Value *V,
1534 AsmWriterContext &WriterCtx,
1535 bool PrintType = false);
1536
1537static void writeAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1538 AsmWriterContext &WriterCtx,
1539 bool FromValue = false);
1540
1541static void writeOptimizationInfo(raw_ostream &Out, const User *U) {
1542 if (const auto *FPO = dyn_cast<const FPMathOperator>(U))
1543 Out << FPO->getFastMathFlags();
1544
1545 if (const auto *OBO = dyn_cast<OverflowingBinaryOperator>(U)) {
1546 if (OBO->hasNoUnsignedWrap())
1547 Out << " nuw";
1548 if (OBO->hasNoSignedWrap())
1549 Out << " nsw";
1550 } else if (const auto *Div = dyn_cast<PossiblyExactOperator>(U)) {
1551 if (Div->isExact())
1552 Out << " exact";
1553 } else if (const auto *PDI = dyn_cast<PossiblyDisjointInst>(U)) {
1554 if (PDI->isDisjoint())
1555 Out << " disjoint";
1556 } else if (const auto *GEP = dyn_cast<GEPOperator>(U)) {
1557 if (GEP->isInBounds())
1558 Out << " inbounds";
1559 else if (GEP->hasNoUnsignedSignedWrap())
1560 Out << " nusw";
1561 if (GEP->hasNoUnsignedWrap())
1562 Out << " nuw";
1563 if (auto InRange = GEP->getInRange()) {
1564 Out << " inrange(" << InRange->getLower() << ", " << InRange->getUpper()
1565 << ")";
1566 }
1567 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(U)) {
1568 if (NNI->hasNonNeg())
1569 Out << " nneg";
1570 } else if (const auto *TI = dyn_cast<TruncInst>(U)) {
1571 if (TI->hasNoUnsignedWrap())
1572 Out << " nuw";
1573 if (TI->hasNoSignedWrap())
1574 Out << " nsw";
1575 } else if (const auto *ICmp = dyn_cast<ICmpInst>(U)) {
1576 if (ICmp->hasSameSign())
1577 Out << " samesign";
1578 } else if (const auto *ASC = dyn_cast<AddrSpaceCastInst>(U)) {
1579 if (ASC->hasNonNull())
1580 Out << " nonnull";
1581 }
1582}
1583
1584static void WriteFullHexAPInt(raw_ostream &Out, const APInt &Val) {
1586 Val.toStringUnsigned(Bits, 16);
1587 unsigned NumDigits = std::max((Val.getBitWidth() + 3) / 4, 1U);
1588 Out << "0x";
1589 for (unsigned i = 0; i < NumDigits - Bits.size(); i++)
1590 Out << '0';
1591 Out << Bits;
1592}
1593
1594static void writeAPFloatInternal(raw_ostream &Out, const APFloat &APF) {
1595 bool ForceBitwiseOutput = false;
1596 if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
1597 // ppc_fp128 types are double-double. The special cases set the second
1598 // (high) double to +0.0, so if the high word is nonzero, force the use of
1599 // bitwise output.
1600 APInt HiWord = APF.bitcastToAPInt().lshr(64);
1601 ForceBitwiseOutput = !HiWord.isZero();
1602 }
1603
1604 if (!ForceBitwiseOutput) {
1605 // Check for special values in APFloat.
1606 if (APF.isInfinity()) {
1607 Out << (APF.isNegative() ? '-' : '+') << "inf";
1608 return;
1609 }
1610
1611 if (APF.isNaN()) {
1612 Out << (APF.isNegative() ? '-' : '+');
1613 APInt Payload = APF.getNaNPayload();
1614 // The quiet bit of a NaN is the highest bit of the payload, so the
1615 // preferred QNaN value happens to be the sign mask value.
1616 if (Payload.isSignMask()) {
1617 Out << "qnan";
1618 } else {
1619 if (APF.isSignaling())
1620 Out << 's';
1621 Out << "nan(";
1622 // Clear out the signaling/quiet bit of the payload for output.
1623 Payload.clearBit(Payload.getBitWidth() - 1);
1624 // Trim the string to exclude leading 0's.
1625 WriteFullHexAPInt(Out, Payload.trunc(Payload.getActiveBits()));
1626 Out << ')';
1627 }
1628 return;
1629 }
1630 }
1631
1632 // Try for a decimal string output. If the value is convertible back to the
1633 // same APFloat value, then we know that it is safe to use it. Otherwise, fall
1634 // back onto the hexadecimal format.
1635 SmallString<128> StrVal;
1636 APF.toString(StrVal, 6, 0, false);
1637 if (APFloat(APF.getSemantics(), StrVal) == APF) {
1638 Out << StrVal;
1639 return;
1640 }
1641
1642 // Fallback to the hexadecimal format representing the bit string exactly.
1643 Out << 'f';
1644 APInt API = APF.bitcastToAPInt();
1645 WriteFullHexAPInt(Out, API);
1646}
1647
1648static void writeConstantInternal(raw_ostream &Out, const Constant *CV,
1649 AsmWriterContext &WriterCtx) {
1650 if (const auto *CI = dyn_cast<ConstantInt>(CV)) {
1651 Type *Ty = CI->getType();
1652
1653 if (Ty->isVectorTy()) {
1654 Out << "splat (";
1655 WriterCtx.TypePrinter->print(Ty->getScalarType(), Out);
1656 Out << " ";
1657 }
1658
1659 if (Ty->getScalarType()->isIntegerTy(1))
1660 Out << (CI->getZExtValue() ? "true" : "false");
1661 else
1662 Out << CI->getValue();
1663
1664 if (Ty->isVectorTy())
1665 Out << ")";
1666
1667 return;
1668 }
1669
1670 if (const auto *CB = dyn_cast<ConstantByte>(CV)) {
1671 Type *Ty = CB->getType();
1672
1673 if (Ty->isVectorTy()) {
1674 Out << "splat (";
1675 WriterCtx.TypePrinter->print(Ty->getScalarType(), Out);
1676 Out << " ";
1677 }
1678
1679 Out << CB->getValue();
1680
1681 if (Ty->isVectorTy())
1682 Out << ")";
1683
1684 return;
1685 }
1686
1687 if (const auto *CFP = dyn_cast<ConstantFP>(CV)) {
1688 Type *Ty = CFP->getType();
1689
1690 if (Ty->isVectorTy()) {
1691 if (CFP->getValue().bitcastToAPInt().isZero()) {
1692 Out << "zeroinitializer";
1693 return;
1694 }
1695
1696 Out << "splat (";
1697 WriterCtx.TypePrinter->print(Ty->getScalarType(), Out);
1698 Out << " ";
1699 }
1700
1701 writeAPFloatInternal(Out, CFP->getValueAPF());
1702
1703 if (Ty->isVectorTy())
1704 Out << ")";
1705
1706 return;
1707 }
1708
1710 Out << "zeroinitializer";
1711 return;
1712 }
1713
1714 if (const auto *BA = dyn_cast<BlockAddress>(CV)) {
1715 Out << "blockaddress(";
1716 writeAsOperandInternal(Out, BA->getFunction(), WriterCtx);
1717 Out << ", ";
1718 writeAsOperandInternal(Out, BA->getBasicBlock(), WriterCtx);
1719 Out << ")";
1720 return;
1721 }
1722
1723 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) {
1724 Out << "dso_local_equivalent ";
1725 writeAsOperandInternal(Out, Equiv->getGlobalValue(), WriterCtx);
1726 return;
1727 }
1728
1729 if (const auto *NC = dyn_cast<NoCFIValue>(CV)) {
1730 Out << "no_cfi ";
1731 writeAsOperandInternal(Out, NC->getGlobalValue(), WriterCtx);
1732 return;
1733 }
1734
1735 if (const auto *CPA = dyn_cast<ConstantPtrAuth>(CV)) {
1736 Out << "ptrauth (";
1737
1738 // ptrauth (ptr CST, i32 KEY[, i64 DISC[, ptr ADDRDISC[, ptr DS]?]?]?)
1739 unsigned NumOpsToWrite = 2;
1740 if (!CPA->getOperand(2)->isNullValue())
1741 NumOpsToWrite = 3;
1742 if (!isa<ConstantPointerNull>(CPA->getOperand(3)))
1743 NumOpsToWrite = 4;
1744 if (!isa<ConstantPointerNull>(CPA->getOperand(4)))
1745 NumOpsToWrite = 5;
1746
1747 ListSeparator LS;
1748 for (unsigned i = 0, e = NumOpsToWrite; i != e; ++i) {
1749 Out << LS;
1750 writeAsOperandInternal(Out, CPA->getOperand(i), WriterCtx,
1751 /*PrintType=*/true);
1752 }
1753 Out << ')';
1754 return;
1755 }
1756
1757 if (const auto *CA = dyn_cast<ConstantArray>(CV)) {
1758 Out << '[';
1759 ListSeparator LS;
1760 for (const Value *Op : CA->operands()) {
1761 Out << LS;
1762 writeAsOperandInternal(Out, Op, WriterCtx, /*PrintType=*/true);
1763 }
1764 Out << ']';
1765 return;
1766 }
1767
1768 if (const auto *CA = dyn_cast<ConstantDataArray>(CV)) {
1769 // As a special case, print the array as a string if it is an array of
1770 // i8 with ConstantInt values.
1771 if (CA->isString()) {
1772 Out << "c\"";
1773 printEscapedString(CA->getAsString(), Out);
1774 Out << '"';
1775 return;
1776 }
1777
1778 Out << '[';
1779 ListSeparator LS;
1780 for (uint64_t i = 0, e = CA->getNumElements(); i != e; ++i) {
1781 Out << LS;
1782 writeAsOperandInternal(Out, CA->getElementAsConstant(i), WriterCtx,
1783 /*PrintType=*/true);
1784 }
1785 Out << ']';
1786 return;
1787 }
1788
1789 if (const auto *CS = dyn_cast<ConstantStruct>(CV)) {
1790 if (CS->getType()->isPacked())
1791 Out << '<';
1792 Out << '{';
1793 if (CS->getNumOperands() != 0) {
1794 Out << ' ';
1795 ListSeparator LS;
1796 for (const Value *Op : CS->operands()) {
1797 Out << LS;
1798 writeAsOperandInternal(Out, Op, WriterCtx, /*PrintType=*/true);
1799 }
1800 Out << ' ';
1801 }
1802 Out << '}';
1803 if (CS->getType()->isPacked())
1804 Out << '>';
1805 return;
1806 }
1807
1809 auto *CVVTy = cast<FixedVectorType>(CV->getType());
1810
1811 // Use the same shorthand for splat vector (i.e. "splat(Ty val)") as is
1812 // permitted on IR input to reduce the output changes when enabling
1813 // UseConstant{Int,FP}ForFixedLengthSplat.
1814 // TODO: Remove this block when the UseConstant{Int,FP}ForFixedLengthSplat
1815 // options are removed.
1816 if (auto *SplatVal = CV->getSplatValue()) {
1817 if (isa<ConstantInt>(SplatVal) || isa<ConstantFP>(SplatVal) ||
1818 isa<ConstantByte>(SplatVal)) {
1819 Out << "splat (";
1820 writeAsOperandInternal(Out, SplatVal, WriterCtx, /*PrintType=*/true);
1821 Out << ')';
1822 return;
1823 }
1824 }
1825
1826 Out << '<';
1827 ListSeparator LS;
1828 for (unsigned i = 0, e = CVVTy->getNumElements(); i != e; ++i) {
1829 Out << LS;
1830 writeAsOperandInternal(Out, CV->getAggregateElement(i), WriterCtx,
1831 /*PrintType=*/true);
1832 }
1833 Out << '>';
1834 return;
1835 }
1836
1837 if (const auto *CPN = dyn_cast<ConstantPointerNull>(CV)) {
1838 if (auto *VT = dyn_cast<VectorType>(CPN->getType())) {
1839 Out << "splat (";
1841 ConstantPointerNull::get(VT->getElementType()),
1842 WriterCtx, /*PrintType=*/true);
1843 Out << ')';
1844 return;
1845 }
1846
1847 Out << "null";
1848 return;
1849 }
1850
1851 if (isa<ConstantTokenNone>(CV)) {
1852 Out << "none";
1853 return;
1854 }
1855
1856 if (isa<PoisonValue>(CV)) {
1857 Out << "poison";
1858 return;
1859 }
1860
1861 if (isa<UndefValue>(CV)) {
1862 Out << "undef";
1863 return;
1864 }
1865
1866 if (const auto *CE = dyn_cast<ConstantExpr>(CV)) {
1867 // Use the same shorthand for splat vector (i.e. "splat(Ty val)") as is
1868 // permitted on IR input to reduce the output changes when enabling
1869 // UseConstant{Int,FP}ForScalableSplat.
1870 // TODO: Remove this block when the UseConstant{Int,FP}ForScalableSplat
1871 // options are removed.
1872 if (CE->getOpcode() == Instruction::ShuffleVector) {
1873 if (auto *SplatVal = CE->getSplatValue()) {
1874 if (isa<ConstantInt>(SplatVal) || isa<ConstantFP>(SplatVal) ||
1875 isa<ConstantByte>(SplatVal)) {
1876 Out << "splat (";
1877 writeAsOperandInternal(Out, SplatVal, WriterCtx, /*PrintType=*/true);
1878 Out << ')';
1879 return;
1880 }
1881 }
1882 }
1883
1884 Out << CE->getOpcodeName();
1885 writeOptimizationInfo(Out, CE);
1886 Out << " (";
1887
1888 if (const auto *GEP = dyn_cast<GEPOperator>(CE)) {
1889 WriterCtx.TypePrinter->print(GEP->getSourceElementType(), Out);
1890 Out << ", ";
1891 }
1892
1893 ListSeparator LS;
1894 for (const Value *Op : CE->operands()) {
1895 Out << LS;
1896 writeAsOperandInternal(Out, Op, WriterCtx, /*PrintType=*/true);
1897 }
1898
1899 if (CE->isCast()) {
1900 Out << " to ";
1901 WriterCtx.TypePrinter->print(CE->getType(), Out);
1902 }
1903
1904 if (CE->getOpcode() == Instruction::ShuffleVector)
1905 printShuffleMask(Out, CE->getType(), CE->getShuffleMask());
1906
1907 Out << ')';
1908 return;
1909 }
1910
1911 Out << "<placeholder or erroneous Constant>";
1912}
1913
1914static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1915 AsmWriterContext &WriterCtx) {
1916 Out << "!{";
1917 ListSeparator LS;
1918 for (const Metadata *MD : Node->operands()) {
1919 Out << LS;
1920 if (!MD) {
1921 Out << "null";
1922 } else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1923 Value *V = MDV->getValue();
1924 writeAsOperandInternal(Out, V, WriterCtx, /*PrintType=*/true);
1925 } else {
1926 writeAsOperandInternal(Out, MD, WriterCtx);
1927 WriterCtx.onWriteMetadataAsOperand(MD);
1928 }
1929 }
1930
1931 Out << "}";
1932}
1933
1934namespace {
1935
1936struct MDFieldPrinter {
1937 raw_ostream &Out;
1938 ListSeparator FS;
1939 AsmWriterContext &WriterCtx;
1940
1941 explicit MDFieldPrinter(raw_ostream &Out)
1942 : Out(Out), WriterCtx(AsmWriterContext::getEmpty()) {}
1943 MDFieldPrinter(raw_ostream &Out, AsmWriterContext &Ctx)
1944 : Out(Out), WriterCtx(Ctx) {}
1945
1946 void printTag(const DINode *N);
1947 void printMacinfoType(const DIMacroNode *N);
1948 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
1949 void printString(StringRef Name, StringRef Value,
1950 bool ShouldSkipEmpty = true);
1951 void printMetadata(StringRef Name, const Metadata *MD,
1952 bool ShouldSkipNull = true);
1953 void printMetadataOrInt(StringRef Name, const Metadata *MD, bool IsUnsigned,
1954 bool ShouldSkipZero = true);
1955 template <class IntTy>
1956 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
1957 void printAPInt(StringRef Name, const APInt &Int, bool IsUnsigned,
1958 bool ShouldSkipZero);
1959 void printBool(StringRef Name, bool Value,
1960 std::optional<bool> Default = std::nullopt);
1961 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
1962 void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
1963 template <class IntTy, class Stringifier>
1964 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1965 bool ShouldSkipZero = true);
1966 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
1967 void printNameTableKind(StringRef Name,
1969 void printFixedPointKind(StringRef Name, DIFixedPointType::FixedPointKind V);
1970};
1971
1972} // end anonymous namespace
1973
1974void MDFieldPrinter::printTag(const DINode *N) {
1975 Out << FS << "tag: ";
1976 auto Tag = dwarf::TagString(N->getTag());
1977 if (!Tag.empty())
1978 Out << Tag;
1979 else
1980 Out << N->getTag();
1981}
1982
1983void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1984 Out << FS << "type: ";
1985 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1986 if (!Type.empty())
1987 Out << Type;
1988 else
1989 Out << N->getMacinfoType();
1990}
1991
1992void MDFieldPrinter::printChecksum(
1993 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1994 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1995 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
1996}
1997
1998void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1999 bool ShouldSkipEmpty) {
2000 if (ShouldSkipEmpty && Value.empty())
2001 return;
2002
2003 Out << FS << Name << ": \"";
2005 Out << "\"";
2006}
2007
2008static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
2009 AsmWriterContext &WriterCtx) {
2010 if (!MD) {
2011 Out << "null";
2012 return;
2013 }
2014 writeAsOperandInternal(Out, MD, WriterCtx);
2015 WriterCtx.onWriteMetadataAsOperand(MD);
2016}
2017
2018void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
2019 bool ShouldSkipNull) {
2020 if (ShouldSkipNull && !MD)
2021 return;
2022
2023 Out << FS << Name << ": ";
2024 writeMetadataAsOperand(Out, MD, WriterCtx);
2025}
2026
2027void MDFieldPrinter::printMetadataOrInt(StringRef Name, const Metadata *MD,
2028 bool IsUnsigned, bool ShouldSkipZero) {
2029 if (!MD)
2030 return;
2031
2032 if (auto *CI = dyn_cast<ConstantAsMetadata>(MD)) {
2033 auto *CV = cast<ConstantInt>(CI->getValue());
2034 if (IsUnsigned)
2035 printInt(Name, CV->getZExtValue(), ShouldSkipZero);
2036 else
2037 printInt(Name, CV->getSExtValue(), ShouldSkipZero);
2038 } else
2039 printMetadata(Name, MD);
2040}
2041
2042template <class IntTy>
2043void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
2044 if (ShouldSkipZero && !Int)
2045 return;
2046
2047 Out << FS << Name << ": " << Int;
2048}
2049
2050void MDFieldPrinter::printAPInt(StringRef Name, const APInt &Int,
2051 bool IsUnsigned, bool ShouldSkipZero) {
2052 if (ShouldSkipZero && Int.isZero())
2053 return;
2054
2055 Out << FS << Name << ": ";
2056 Int.print(Out, !IsUnsigned);
2057}
2058
2059void MDFieldPrinter::printBool(StringRef Name, bool Value,
2060 std::optional<bool> Default) {
2061 if (Default && Value == *Default)
2062 return;
2063 Out << FS << Name << ": " << (Value ? "true" : "false");
2064}
2065
2066void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
2067 if (!Flags)
2068 return;
2069
2070 Out << FS << Name << ": ";
2071
2073 auto Extra = DINode::splitFlags(Flags, SplitFlags);
2074
2075 ListSeparator FlagsFS(" | ");
2076 for (auto F : SplitFlags) {
2077 auto StringF = DINode::getFlagString(F);
2078 assert(!StringF.empty() && "Expected valid flag");
2079 Out << FlagsFS << StringF;
2080 }
2081 if (Extra || SplitFlags.empty())
2082 Out << FlagsFS << Extra;
2083}
2084
2085void MDFieldPrinter::printDISPFlags(StringRef Name,
2087 // Always print this field, because no flags in the IR at all will be
2088 // interpreted as old-style isDefinition: true.
2089 Out << FS << Name << ": ";
2090
2091 if (!Flags) {
2092 Out << 0;
2093 return;
2094 }
2095
2097 auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
2098
2099 ListSeparator FlagsFS(" | ");
2100 for (auto F : SplitFlags) {
2101 auto StringF = DISubprogram::getFlagString(F);
2102 assert(!StringF.empty() && "Expected valid flag");
2103 Out << FlagsFS << StringF;
2104 }
2105 if (Extra || SplitFlags.empty())
2106 Out << FlagsFS << Extra;
2107}
2108
2109void MDFieldPrinter::printEmissionKind(StringRef Name,
2111 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
2112}
2113
2114void MDFieldPrinter::printNameTableKind(StringRef Name,
2117 return;
2118 Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
2119}
2120
2121void MDFieldPrinter::printFixedPointKind(StringRef Name,
2124}
2125
2126template <class IntTy, class Stringifier>
2127void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
2128 Stringifier toString, bool ShouldSkipZero) {
2129 if (ShouldSkipZero && !Value)
2130 return;
2131
2132 Out << FS << Name << ": ";
2133 auto S = toString(Value);
2134 if (!S.empty())
2135 Out << S;
2136 else
2137 Out << Value;
2138}
2139
2141 AsmWriterContext &WriterCtx) {
2142 Out << "!GenericDINode(";
2143 MDFieldPrinter Printer(Out, WriterCtx);
2144 Printer.printTag(N);
2145 Printer.printString("header", N->getHeader());
2146 if (N->getNumDwarfOperands()) {
2147 Out << Printer.FS << "operands: {";
2148 ListSeparator IFS;
2149 for (auto &I : N->dwarf_operands()) {
2150 Out << IFS;
2151 writeMetadataAsOperand(Out, I, WriterCtx);
2152 }
2153 Out << "}";
2154 }
2155 Out << ")";
2156}
2157
2158static void writeDILocation(raw_ostream &Out, const DILocation *DL,
2159 AsmWriterContext &WriterCtx) {
2160 Out << "!DILocation(";
2161 MDFieldPrinter Printer(Out, WriterCtx);
2162 // Always output the line, since 0 is a relevant and important value for it.
2163 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
2164 Printer.printInt("column", DL->getColumn());
2165 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
2166 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
2167 Printer.printBool("isImplicitCode", DL->isImplicitCode(),
2168 /* Default */ false);
2169 Printer.printInt("atomGroup", DL->getAtomGroup());
2170 Printer.printInt<unsigned>("atomRank", DL->getAtomRank());
2171 Printer.printMetadata("irlayers", DL->getRawIRLayers());
2172 Out << ")";
2173}
2174
2175static void writeDILayerLoc(raw_ostream &Out, const DILayerLoc *N,
2176 AsmWriterContext &WriterCtx) {
2177 Out << "!DILayerLoc(";
2178 MDFieldPrinter Printer(Out, WriterCtx);
2179 Printer.printInt("line", N->getLine(), /* ShouldSkipZero */ false);
2180 Printer.printInt("column", N->getColumn());
2181 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2182 Printer.printString("kind", N->getKind(), /* ShouldSkipEmpty */ false);
2183 Out << ")";
2184}
2185
2187 AsmWriterContext &WriterCtx) {
2188 Out << "!DILayerLocList(";
2189 ListSeparator FS;
2190 for (const MDOperand &Op : N->operands()) {
2191 Out << FS;
2192 writeMetadataAsOperand(Out, Op, WriterCtx);
2193 }
2194 Out << ")";
2195}
2196
2197static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL,
2198 AsmWriterContext &WriterCtx) {
2199 Out << "!DIAssignID()";
2200 MDFieldPrinter Printer(Out, WriterCtx);
2201}
2202
2203static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
2204 AsmWriterContext &WriterCtx) {
2205 Out << "!DISubrange(";
2206 MDFieldPrinter Printer(Out, WriterCtx);
2207
2208 Printer.printMetadataOrInt("count", N->getRawCountNode(),
2209 /* IsUnsigned */ false,
2210 /* ShouldSkipZero */ false);
2211
2212 // A lowerBound of constant 0 should not be skipped, since it is different
2213 // from an unspecified lower bound (= nullptr).
2214 Printer.printMetadataOrInt("lowerBound", N->getRawLowerBound(),
2215 /* IsUnsigned */ false,
2216 /* ShouldSkipZero */ false);
2217 Printer.printMetadataOrInt("upperBound", N->getRawUpperBound(),
2218 /* IsUnsigned */ false,
2219 /* ShouldSkipZero */ false);
2220 Printer.printMetadataOrInt("stride", N->getRawStride(),
2221 /* IsUnsigned */ false,
2222 /* ShouldSkipZero */ false);
2223
2224 Out << ")";
2225}
2226
2228 AsmWriterContext &WriterCtx) {
2229 Out << "!DIGenericSubrange(";
2230 MDFieldPrinter Printer(Out, WriterCtx);
2231
2232 auto GetConstant = [&](Metadata *Bound) -> std::optional<int64_t> {
2233 auto *BE = dyn_cast_or_null<DIExpression>(Bound);
2234 if (!BE)
2235 return std::nullopt;
2236 if (BE->isConstant() &&
2238 *BE->isConstant()) {
2239 return static_cast<int64_t>(BE->getElement(1));
2240 }
2241 return std::nullopt;
2242 };
2243
2244 auto *Count = N->getRawCountNode();
2245 if (auto ConstantCount = GetConstant(Count))
2246 Printer.printInt("count", *ConstantCount,
2247 /* ShouldSkipZero */ false);
2248 else
2249 Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
2250
2251 auto *LBound = N->getRawLowerBound();
2252 if (auto ConstantLBound = GetConstant(LBound))
2253 Printer.printInt("lowerBound", *ConstantLBound,
2254 /* ShouldSkipZero */ false);
2255 else
2256 Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
2257
2258 auto *UBound = N->getRawUpperBound();
2259 if (auto ConstantUBound = GetConstant(UBound))
2260 Printer.printInt("upperBound", *ConstantUBound,
2261 /* ShouldSkipZero */ false);
2262 else
2263 Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
2264
2265 auto *Stride = N->getRawStride();
2266 if (auto ConstantStride = GetConstant(Stride))
2267 Printer.printInt("stride", *ConstantStride,
2268 /* ShouldSkipZero */ false);
2269 else
2270 Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);
2271
2272 Out << ")";
2273}
2274
2276 AsmWriterContext &) {
2277 Out << "!DIEnumerator(";
2278 MDFieldPrinter Printer(Out);
2279 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
2280 Printer.printAPInt("value", N->getValue(), N->isUnsigned(),
2281 /*ShouldSkipZero=*/false);
2282 if (N->isUnsigned())
2283 Printer.printBool("isUnsigned", true);
2284 Out << ")";
2285}
2286
2288 AsmWriterContext &WriterCtx) {
2289 Out << "!DIBasicType(";
2290 MDFieldPrinter Printer(Out, WriterCtx);
2291 if (N->getTag() != dwarf::DW_TAG_base_type)
2292 Printer.printTag(N);
2293 Printer.printString("name", N->getName());
2294 Printer.printMetadata("scope", N->getRawScope());
2295 Printer.printMetadata("file", N->getRawFile());
2296 Printer.printInt("line", N->getLine());
2297 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2298 Printer.printInt("align", N->getAlignInBits());
2299 Printer.printInt("dataSize", N->getDataSizeInBits());
2300 Printer.printDwarfEnum("encoding", N->getEncoding(),
2302 Printer.printInt("num_extra_inhabitants", N->getNumExtraInhabitants());
2303 Printer.printDIFlags("flags", N->getFlags());
2304 Out << ")";
2305}
2306
2308 AsmWriterContext &WriterCtx) {
2309 Out << "!DIFixedPointType(";
2310 MDFieldPrinter Printer(Out, WriterCtx);
2311 if (N->getTag() != dwarf::DW_TAG_base_type)
2312 Printer.printTag(N);
2313 Printer.printString("name", N->getName());
2314 Printer.printMetadata("scope", N->getRawScope());
2315 Printer.printMetadata("file", N->getRawFile());
2316 Printer.printInt("line", N->getLine());
2317 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2318 Printer.printInt("align", N->getAlignInBits());
2319 Printer.printDwarfEnum("encoding", N->getEncoding(),
2321 Printer.printDIFlags("flags", N->getFlags());
2322 Printer.printFixedPointKind("kind", N->getKind());
2323 if (N->isRational()) {
2324 bool IsUnsigned = !N->isSigned();
2325 Printer.printAPInt("numerator", N->getNumerator(), IsUnsigned, false);
2326 Printer.printAPInt("denominator", N->getDenominator(), IsUnsigned, false);
2327 } else {
2328 Printer.printInt("factor", N->getFactor());
2329 }
2330 Out << ")";
2331}
2332
2334 AsmWriterContext &WriterCtx) {
2335 Out << "!DIStringType(";
2336 MDFieldPrinter Printer(Out, WriterCtx);
2337 if (N->getTag() != dwarf::DW_TAG_string_type)
2338 Printer.printTag(N);
2339 Printer.printString("name", N->getName());
2340 Printer.printMetadata("stringLength", N->getRawStringLength());
2341 Printer.printMetadata("stringLengthExpression", N->getRawStringLengthExp());
2342 Printer.printMetadata("stringLocationExpression",
2343 N->getRawStringLocationExp());
2344 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2345 Printer.printInt("align", N->getAlignInBits());
2346 Printer.printDwarfEnum("encoding", N->getEncoding(),
2348 Printer.printMetadata("charType", N->getRawCharType());
2349 Out << ")";
2350}
2351
2353 AsmWriterContext &WriterCtx) {
2354 Out << "!DIDerivedType(";
2355 MDFieldPrinter Printer(Out, WriterCtx);
2356 Printer.printTag(N);
2357 Printer.printString("name", N->getName());
2358 Printer.printMetadata("scope", N->getRawScope());
2359 Printer.printMetadata("file", N->getRawFile());
2360 Printer.printInt("line", N->getLine());
2361 Printer.printMetadata("baseType", N->getRawBaseType(),
2362 /* ShouldSkipNull */ false);
2363 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2364 Printer.printInt("align", N->getAlignInBits());
2365 Printer.printMetadataOrInt("offset", N->getRawOffsetInBits(), true);
2366 Printer.printDIFlags("flags", N->getFlags());
2367 Printer.printMetadata("extraData", N->getRawExtraData());
2368 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
2369 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
2370 /* ShouldSkipZero */ false);
2371 Printer.printMetadata("annotations", N->getRawAnnotations());
2372 if (auto PtrAuthData = N->getPtrAuthData()) {
2373 Printer.printInt("ptrAuthKey", PtrAuthData->key());
2374 Printer.printBool("ptrAuthIsAddressDiscriminated",
2375 PtrAuthData->isAddressDiscriminated());
2376 Printer.printInt("ptrAuthExtraDiscriminator",
2377 PtrAuthData->extraDiscriminator());
2378 Printer.printBool("ptrAuthIsaPointer", PtrAuthData->isaPointer());
2379 Printer.printBool("ptrAuthAuthenticatesNullValues",
2380 PtrAuthData->authenticatesNullValues());
2381 }
2382 Out << ")";
2383}
2384
2386 AsmWriterContext &WriterCtx) {
2387 Out << "!DISubrangeType(";
2388 MDFieldPrinter Printer(Out, WriterCtx);
2389 Printer.printString("name", N->getName());
2390 Printer.printMetadata("scope", N->getRawScope());
2391 Printer.printMetadata("file", N->getRawFile());
2392 Printer.printInt("line", N->getLine());
2393 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2394 Printer.printInt("align", N->getAlignInBits());
2395 Printer.printDIFlags("flags", N->getFlags());
2396 Printer.printMetadata("baseType", N->getRawBaseType(),
2397 /* ShouldSkipNull */ false);
2398 Printer.printMetadata("lowerBound", N->getRawLowerBound());
2399 Printer.printMetadata("upperBound", N->getRawUpperBound());
2400 Printer.printMetadata("stride", N->getRawStride());
2401 Printer.printMetadata("bias", N->getRawBias());
2402 Out << ")";
2403}
2404
2406 AsmWriterContext &WriterCtx) {
2407 Out << "!DICompositeType(";
2408 MDFieldPrinter Printer(Out, WriterCtx);
2409 Printer.printTag(N);
2410 Printer.printString("name", N->getName());
2411 Printer.printMetadata("scope", N->getRawScope());
2412 Printer.printMetadata("file", N->getRawFile());
2413 Printer.printInt("line", N->getLine());
2414 Printer.printMetadata("baseType", N->getRawBaseType());
2415 Printer.printMetadataOrInt("size", N->getRawSizeInBits(), true);
2416 Printer.printInt("align", N->getAlignInBits());
2417 Printer.printMetadataOrInt("offset", N->getRawOffsetInBits(), true);
2418 Printer.printInt("num_extra_inhabitants", N->getNumExtraInhabitants());
2419 Printer.printDIFlags("flags", N->getFlags());
2420 Printer.printMetadata("elements", N->getRawElements());
2421 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
2423 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
2424 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2425 Printer.printString("identifier", N->getIdentifier());
2426 Printer.printMetadata("discriminator", N->getRawDiscriminator());
2427 Printer.printMetadata("dataLocation", N->getRawDataLocation());
2428 Printer.printMetadata("associated", N->getRawAssociated());
2429 Printer.printMetadata("allocated", N->getRawAllocated());
2430 if (auto *RankConst = N->getRankConst())
2431 Printer.printInt("rank", RankConst->getSExtValue(),
2432 /* ShouldSkipZero */ false);
2433 else
2434 Printer.printMetadata("rank", N->getRawRank(), /*ShouldSkipNull */ true);
2435 Printer.printMetadata("annotations", N->getRawAnnotations());
2436 if (auto *Specification = N->getRawSpecification())
2437 Printer.printMetadata("specification", Specification);
2438
2439 if (auto EnumKind = N->getEnumKind())
2440 Printer.printDwarfEnum("enumKind", *EnumKind, dwarf::EnumKindString,
2441 /*ShouldSkipZero=*/false);
2442
2443 Printer.printMetadata("bitStride", N->getRawBitStride());
2444 Out << ")";
2445}
2446
2448 AsmWriterContext &WriterCtx) {
2449 Out << "!DISubroutineType(";
2450 MDFieldPrinter Printer(Out, WriterCtx);
2451 Printer.printDIFlags("flags", N->getFlags());
2452 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
2453 Printer.printMetadata("types", N->getRawTypeArray(),
2454 /* ShouldSkipNull */ false);
2455 Out << ")";
2456}
2457
2458static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &) {
2459 Out << "!DIFile(";
2460 MDFieldPrinter Printer(Out);
2461 Printer.printString("filename", N->getFilename(),
2462 /* ShouldSkipEmpty */ false);
2463 Printer.printString("directory", N->getDirectory(),
2464 /* ShouldSkipEmpty */ false);
2465 // Print all values for checksum together, or not at all.
2466 if (N->getChecksum())
2467 Printer.printChecksum(*N->getChecksum());
2468 if (N->getSource())
2469 Printer.printString("source", *N->getSource(),
2470 /* ShouldSkipEmpty */ false);
2471 Out << ")";
2472}
2473
2475 AsmWriterContext &WriterCtx) {
2476 Out << "!DICompileUnit(";
2477 MDFieldPrinter Printer(Out, WriterCtx);
2478
2479 DISourceLanguageName Lang = N->getSourceLanguage();
2480
2481 if (Lang.hasVersionedName()) {
2482 Printer.printDwarfEnum(
2483 "sourceLanguageName",
2484 static_cast<llvm::dwarf::SourceLanguageName>(Lang.getName()),
2486 /* ShouldSkipZero */ false);
2487
2488 Printer.printInt("sourceLanguageVersion", Lang.getVersion(),
2489 /*ShouldSkipZero=*/true);
2490 } else {
2491 Printer.printDwarfEnum("language", Lang.getName(), dwarf::LanguageString,
2492 /* ShouldSkipZero */ false);
2493 }
2494
2495 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2496 Printer.printString("producer", N->getProducer());
2497 Printer.printBool("isOptimized", N->isOptimized());
2498 Printer.printString("flags", N->getFlags());
2499 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
2500 /* ShouldSkipZero */ false);
2501 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
2502 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
2503 Printer.printMetadata("enums", N->getRawEnumTypes());
2504 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
2505 Printer.printMetadata("globals", N->getRawGlobalVariables());
2506 Printer.printMetadata("imports", N->getRawImportedEntities());
2507 Printer.printMetadata("macros", N->getRawMacros());
2508 Printer.printInt("dwoId", N->getDWOId());
2509 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
2510 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
2511 false);
2512 Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
2513 Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
2514 Printer.printString("sysroot", N->getSysRoot());
2515 Printer.printString("sdk", N->getSDK());
2516 Printer.printDwarfEnum("dialect", Lang.getDialect(),
2518 Out << ")";
2519}
2520
2522 AsmWriterContext &WriterCtx) {
2523 Out << "!DISubprogram(";
2524 MDFieldPrinter Printer(Out, WriterCtx);
2525 Printer.printString("name", N->getName());
2526 Printer.printString("linkageName", N->getLinkageName());
2527 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2528 Printer.printMetadata("file", N->getRawFile());
2529 Printer.printInt("line", N->getLine());
2530 Printer.printMetadata("type", N->getRawType());
2531 Printer.printInt("scopeLine", N->getScopeLine());
2532 Printer.printMetadata("containingType", N->getRawContainingType());
2533 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
2534 N->getVirtualIndex() != 0)
2535 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
2536 Printer.printInt("thisAdjustment", N->getThisAdjustment());
2537 Printer.printDIFlags("flags", N->getFlags());
2538 Printer.printDISPFlags("spFlags", N->getSPFlags());
2539 Printer.printMetadata("unit", N->getRawUnit());
2540 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2541 Printer.printMetadata("declaration", N->getRawDeclaration());
2542 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
2543 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
2544 Printer.printMetadata("annotations", N->getRawAnnotations());
2545 Printer.printString("targetFuncName", N->getTargetFuncName());
2546 Printer.printBool("keyInstructions", N->getKeyInstructionsEnabled(), false);
2547 Out << ")";
2548}
2549
2551 AsmWriterContext &WriterCtx) {
2552 Out << "!DILexicalBlock(";
2553 MDFieldPrinter Printer(Out, WriterCtx);
2554 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2555 Printer.printMetadata("file", N->getRawFile());
2556 Printer.printInt("line", N->getLine());
2557 Printer.printInt("column", N->getColumn());
2558 Out << ")";
2559}
2560
2562 const DILexicalBlockFile *N,
2563 AsmWriterContext &WriterCtx) {
2564 Out << "!DILexicalBlockFile(";
2565 MDFieldPrinter Printer(Out, WriterCtx);
2566 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2567 Printer.printMetadata("file", N->getRawFile());
2568 Printer.printInt("discriminator", N->getDiscriminator(),
2569 /* ShouldSkipZero */ false);
2570 Out << ")";
2571}
2572
2574 AsmWriterContext &WriterCtx) {
2575 Out << "!DINamespace(";
2576 MDFieldPrinter Printer(Out, WriterCtx);
2577 Printer.printString("name", N->getName());
2578 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2579 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
2580 Out << ")";
2581}
2582
2584 AsmWriterContext &WriterCtx) {
2585 Out << "!DICommonBlock(";
2586 MDFieldPrinter Printer(Out, WriterCtx);
2587 Printer.printMetadata("scope", N->getRawScope(), false);
2588 Printer.printMetadata("declaration", N->getRawDecl(), false);
2589 Printer.printString("name", N->getName());
2590 Printer.printMetadata("file", N->getRawFile());
2591 Printer.printInt("line", N->getLineNo());
2592 Out << ")";
2593}
2594
2595static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2596 AsmWriterContext &WriterCtx) {
2597 Out << "!DIMacro(";
2598 MDFieldPrinter Printer(Out, WriterCtx);
2599 Printer.printMacinfoType(N);
2600 Printer.printInt("line", N->getLine());
2601 Printer.printString("name", N->getName());
2602 Printer.printString("value", N->getValue());
2603 Out << ")";
2604}
2605
2607 AsmWriterContext &WriterCtx) {
2608 Out << "!DIMacroFile(";
2609 MDFieldPrinter Printer(Out, WriterCtx);
2610 Printer.printInt("line", N->getLine());
2611 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2612 Printer.printMetadata("nodes", N->getRawElements());
2613 Out << ")";
2614}
2615
2616static void writeDIModule(raw_ostream &Out, const DIModule *N,
2617 AsmWriterContext &WriterCtx) {
2618 Out << "!DIModule(";
2619 MDFieldPrinter Printer(Out, WriterCtx);
2620 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2621 Printer.printString("name", N->getName());
2622 Printer.printString("configMacros", N->getConfigurationMacros());
2623 Printer.printString("includePath", N->getIncludePath());
2624 Printer.printString("apinotes", N->getAPINotesFile());
2625 Printer.printMetadata("file", N->getRawFile());
2626 Printer.printInt("line", N->getLineNo());
2627 Printer.printBool("isDecl", N->getIsDecl(), /* Default */ false);
2628 Out << ")";
2629}
2630
2633 AsmWriterContext &WriterCtx) {
2634 Out << "!DITemplateTypeParameter(";
2635 MDFieldPrinter Printer(Out, WriterCtx);
2636 Printer.printString("name", N->getName());
2637 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
2638 Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
2639 Out << ")";
2640}
2641
2644 AsmWriterContext &WriterCtx) {
2645 Out << "!DITemplateValueParameter(";
2646 MDFieldPrinter Printer(Out, WriterCtx);
2647 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
2648 Printer.printTag(N);
2649 Printer.printString("name", N->getName());
2650 Printer.printMetadata("type", N->getRawType());
2651 Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
2652 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
2653 Out << ")";
2654}
2655
2657 AsmWriterContext &WriterCtx) {
2658 Out << "!DIGlobalVariable(";
2659 MDFieldPrinter Printer(Out, WriterCtx);
2660 Printer.printString("name", N->getName());
2661 Printer.printString("linkageName", N->getLinkageName());
2662 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2663 Printer.printMetadata("file", N->getRawFile());
2664 Printer.printInt("line", N->getLine());
2665 Printer.printMetadata("type", N->getRawType());
2666 Printer.printBool("isLocal", N->isLocalToUnit());
2667 Printer.printBool("isDefinition", N->isDefinition());
2668 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
2669 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2670 Printer.printInt("align", N->getAlignInBits());
2671 Printer.printMetadata("annotations", N->getRawAnnotations());
2672 Out << ")";
2673}
2674
2676 AsmWriterContext &WriterCtx) {
2677 Out << "!DILocalVariable(";
2678 MDFieldPrinter Printer(Out, WriterCtx);
2679 Printer.printString("name", N->getName());
2680 Printer.printInt("arg", N->getArg());
2681 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2682 Printer.printMetadata("file", N->getRawFile());
2683 Printer.printInt("line", N->getLine());
2684 Printer.printMetadata("type", N->getRawType());
2685 Printer.printDIFlags("flags", N->getFlags());
2686 Printer.printInt("align", N->getAlignInBits());
2687 Printer.printMetadata("annotations", N->getRawAnnotations());
2688 Out << ")";
2689}
2690
2691static void writeDILabel(raw_ostream &Out, const DILabel *N,
2692 AsmWriterContext &WriterCtx) {
2693 Out << "!DILabel(";
2694 MDFieldPrinter Printer(Out, WriterCtx);
2695 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2696 Printer.printString("name", N->getName());
2697 Printer.printMetadata("file", N->getRawFile());
2698 Printer.printInt("line", N->getLine(), /* ShouldSkipZero */ false);
2699 Printer.printInt("column", N->getColumn());
2700 Printer.printBool("isArtificial", N->isArtificial(), false);
2701 if (N->getCoroSuspendIdx())
2702 Printer.printInt("coroSuspendIdx", *N->getCoroSuspendIdx(),
2703 /* ShouldSkipZero */ false);
2704 Out << ")";
2705}
2706
2708 AsmWriterContext &WriterCtx) {
2709 Out << "!DIExpression(";
2710 ListSeparator FS;
2711 if (N->isValid()) {
2712 for (const DIExpression::ExprOperand &Op : N->expr_ops()) {
2713 auto OpStr = dwarf::OperationEncodingString(Op.getOp());
2714 assert(!OpStr.empty() && "Expected valid opcode");
2715
2716 Out << FS << OpStr;
2717 if (auto Convert = dyn_cast<DIExpression::ConvertOp>(Op)) {
2718 Out << FS << Convert.getBitSize();
2719 Out << FS << dwarf::AttributeEncodingString(Convert.getEncoding());
2720 } else {
2721 for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A)
2722 Out << FS << Op.getArg(A);
2723 }
2724 }
2725 } else {
2726 for (const auto &I : N->getElements())
2727 Out << FS << I;
2728 }
2729 Out << ")";
2730}
2731
2732static void writeDIArgList(raw_ostream &Out, const DIArgList *N,
2733 AsmWriterContext &WriterCtx,
2734 bool FromValue = false) {
2735 assert(FromValue &&
2736 "Unexpected DIArgList metadata outside of value argument");
2737 Out << "!DIArgList(";
2738 ListSeparator FS;
2739 MDFieldPrinter Printer(Out, WriterCtx);
2740 for (const Metadata *Arg : N->getArgs()) {
2741 Out << FS;
2742 writeAsOperandInternal(Out, Arg, WriterCtx, true);
2743 }
2744 Out << ")";
2745}
2746
2749 AsmWriterContext &WriterCtx) {
2750 Out << "!DIGlobalVariableExpression(";
2751 MDFieldPrinter Printer(Out, WriterCtx);
2752 Printer.printMetadata("var", N->getVariable());
2753 Printer.printMetadata("expr", N->getExpression());
2754 Out << ")";
2755}
2756
2758 AsmWriterContext &WriterCtx) {
2759 Out << "!DIObjCProperty(";
2760 MDFieldPrinter Printer(Out, WriterCtx);
2761 Printer.printString("name", N->getName());
2762 Printer.printMetadata("file", N->getRawFile());
2763 Printer.printInt("line", N->getLine());
2764 Printer.printString("setter", N->getSetterName());
2765 Printer.printString("getter", N->getGetterName());
2766 Printer.printInt("attributes", N->getAttributes());
2767 Printer.printMetadata("type", N->getRawType());
2768 Out << ")";
2769}
2770
2771static void writeDIProperty(raw_ostream &Out, const DIProperty *N,
2772 AsmWriterContext &WriterCtx) {
2773 Out << "!DIProperty(";
2774 MDFieldPrinter Printer(Out, WriterCtx);
2775 Printer.printString("name", N->getName());
2776 Printer.printMetadata("file", N->getRawFile());
2777 Printer.printInt("line", N->getLine());
2778 Printer.printMetadata("type", N->getRawType());
2779 Printer.printMetadata("backing_storage", N->getRawBackingStorage());
2780 Out << ")";
2781}
2782
2784 AsmWriterContext &WriterCtx) {
2785 Out << "!DIImportedEntity(";
2786 MDFieldPrinter Printer(Out, WriterCtx);
2787 Printer.printTag(N);
2788 Printer.printString("name", N->getName());
2789 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2790 Printer.printMetadata("entity", N->getRawEntity());
2791 Printer.printMetadata("file", N->getRawFile());
2792 Printer.printInt("line", N->getLine());
2793 Printer.printMetadata("elements", N->getRawElements());
2794 Out << ")";
2795}
2796
2798 AsmWriterContext &Ctx) {
2799 if (Node->isDistinct())
2800 Out << "distinct ";
2801 else if (Node->isTemporary())
2802 Out << "<temporary!> "; // Handle broken code.
2803
2804 switch (Node->getMetadataID()) {
2805 default:
2806 llvm_unreachable("Expected uniquable MDNode");
2807#define HANDLE_MDNODE_LEAF(CLASS) \
2808 case Metadata::CLASS##Kind: \
2809 write##CLASS(Out, cast<CLASS>(Node), Ctx); \
2810 break;
2811#include "llvm/IR/Metadata.def"
2812 }
2813}
2814
2815// Full implementation of printing a Value as an operand with support for
2816// TypePrinting, etc.
2818 AsmWriterContext &WriterCtx,
2819 bool PrintType) {
2820 if (PrintType) {
2821 WriterCtx.TypePrinter->print(V->getType(), Out);
2822 Out << ' ';
2823 }
2824
2825 if (V->hasName()) {
2826 printLLVMName(Out, V);
2827 return;
2828 }
2829
2830 const auto *CV = dyn_cast<Constant>(V);
2831 if (CV && !isa<GlobalValue>(CV)) {
2832 assert(WriterCtx.TypePrinter && "Constants require TypePrinting!");
2833 writeConstantInternal(Out, CV, WriterCtx);
2834 return;
2835 }
2836
2837 if (const auto *IA = dyn_cast<InlineAsm>(V)) {
2838 Out << "asm ";
2839 if (IA->hasSideEffects())
2840 Out << "sideeffect ";
2841 if (IA->isAlignStack())
2842 Out << "alignstack ";
2843 // We don't emit the AD_ATT dialect as it's the assumed default.
2844 if (IA->getDialect() == InlineAsm::AD_Intel)
2845 Out << "inteldialect ";
2846 if (IA->canThrow())
2847 Out << "unwind ";
2848 Out << '"';
2849 printEscapedString(IA->getAsmString(), Out);
2850 Out << "\", \"";
2851 printEscapedString(IA->getConstraintString(), Out);
2852 Out << '"';
2853 return;
2854 }
2855
2856 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2857 writeAsOperandInternal(Out, MD->getMetadata(), WriterCtx,
2858 /* FromValue */ true);
2859 return;
2860 }
2861
2862 char Prefix = '%';
2863 int Slot;
2864 auto *Machine = WriterCtx.Machine;
2865 // If we have a SlotTracker, use it.
2866 if (Machine) {
2867 if (const auto *GV = dyn_cast<GlobalValue>(V)) {
2868 Slot = Machine->getGlobalSlot(GV);
2869 Prefix = '@';
2870 } else {
2871 Slot = Machine->getLocalSlot(V);
2872
2873 // If the local value didn't succeed, then we may be referring to a value
2874 // from a different function. Translate it, as this can happen when using
2875 // address of blocks.
2876 if (Slot == -1)
2877 if ((Machine = createSlotTracker(V))) {
2878 Slot = Machine->getLocalSlot(V);
2879 delete Machine;
2880 }
2881 }
2882 } else if ((Machine = createSlotTracker(V))) {
2883 // Otherwise, create one to get the # and then destroy it.
2884 if (const auto *GV = dyn_cast<GlobalValue>(V)) {
2885 Slot = Machine->getGlobalSlot(GV);
2886 Prefix = '@';
2887 } else {
2888 Slot = Machine->getLocalSlot(V);
2889 }
2890 delete Machine;
2891 Machine = nullptr;
2892 } else {
2893 Slot = -1;
2894 }
2895
2896 if (Slot != -1)
2897 Out << Prefix << Slot;
2898 else
2899 Out << "<badref>";
2900}
2901
2902static void writeAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2903 AsmWriterContext &WriterCtx,
2904 bool FromValue) {
2905 // Write DIExpressions and DIArgLists inline when used as a value. Improves
2906 // readability of debug info intrinsics.
2907 if (const auto *Expr = dyn_cast<DIExpression>(MD)) {
2908 writeDIExpression(Out, Expr, WriterCtx);
2909 return;
2910 }
2911 if (const auto *ArgList = dyn_cast<DIArgList>(MD)) {
2912 writeDIArgList(Out, ArgList, WriterCtx, FromValue);
2913 return;
2914 }
2915
2916 if (const auto *N = dyn_cast<MDNode>(MD)) {
2917 if (const auto *Loc = dyn_cast<DILocation>(N);
2918 Loc && WriterCtx.MST &&
2919 WriterCtx.MST->shouldPrintDebugLocationInline(Loc)) {
2920 writeDILocation(Out, Loc, WriterCtx);
2921 return;
2922 }
2923
2924 std::unique_ptr<SlotTracker> MachineStorage;
2925 SaveAndRestore SARMachine(WriterCtx.Machine);
2926 if (!WriterCtx.Machine) {
2927 MachineStorage = std::make_unique<SlotTracker>(WriterCtx.Context);
2928 WriterCtx.Machine = MachineStorage.get();
2929 }
2930 int Slot = WriterCtx.Machine->getMetadataSlot(N);
2931 if (Slot == -1) {
2932 if (const auto *Loc = dyn_cast<DILocation>(N)) {
2933 writeDILocation(Out, Loc, WriterCtx);
2934 return;
2935 }
2936 // Give the pointer value instead of "badref", since this comes up all
2937 // the time when debugging.
2938 Out << "<" << N << ">";
2939 } else
2940 Out << '!' << Slot;
2941 return;
2942 }
2943
2944 if (const auto *MDS = dyn_cast<MDString>(MD)) {
2945 Out << "!\"";
2946 printEscapedString(MDS->getString(), Out);
2947 Out << '"';
2948 return;
2949 }
2950
2951 auto *V = cast<ValueAsMetadata>(MD);
2952 assert(WriterCtx.TypePrinter && "TypePrinter required for metadata values");
2953 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2954 "Unexpected function-local metadata outside of value argument");
2955
2956 writeAsOperandInternal(Out, V->getValue(), WriterCtx, /*PrintType=*/true);
2957}
2958
2959namespace {
2960
2961class AssemblyWriter {
2962 const IROptions &Opts;
2963 formatted_raw_ostream &Out;
2964 const Module *TheModule = nullptr;
2965 const ModuleSummaryIndex *TheIndex = nullptr;
2966 std::unique_ptr<SlotTracker> SlotTrackerStorage;
2967 SlotTracker &Machine;
2968 TypePrinting TypePrinter;
2969 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
2970 bool IsForDebug;
2971 bool ShouldPreserveUseListOrder;
2972 UseListOrderMap UseListOrders;
2974 /// Synchronization scope names registered with LLVMContext.
2976 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
2977
2978public:
2979 /// Construct an AssemblyWriter with an external SlotTracker
2980 AssemblyWriter(const IROptions &Opts, formatted_raw_ostream &o,
2981 SlotTracker &Mac, const Module *M,
2982 AssemblyAnnotationWriter *AAW, bool IsForDebug,
2983 bool ShouldPreserveUseListOrder = false);
2984
2985 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2986 const ModuleSummaryIndex *Index, bool IsForDebug);
2987
2988 AsmWriterContext getContext() {
2989 return AsmWriterContext(&TypePrinter, &Machine, TheModule);
2990 }
2991
2992 void printMDNodeBody(const MDNode *MD);
2993 void printNamedMDNode(const NamedMDNode *NMD);
2994
2995 void printModule(const Module *M);
2996
2997 void writeOperand(const Value *Op, bool PrintType);
2998 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
2999 void writeOperandBundles(const CallBase *Call);
3000 void writeSyncScope(const LLVMContext &Context,
3001 SyncScope::ID SSID);
3002 void writeAtomic(const LLVMContext &Context,
3003 AtomicOrdering Ordering,
3004 SyncScope::ID SSID);
3005 void writeAtomicCmpXchg(const LLVMContext &Context,
3006 AtomicOrdering SuccessOrdering,
3007 AtomicOrdering FailureOrdering,
3008 SyncScope::ID SSID);
3009
3010 void writeAllMDNodes();
3011 void writeMDNode(unsigned Slot, const MDNode *Node);
3012 void writeAttribute(const Attribute &Attr, bool InAttrGroup = false);
3013 void writeAttributeSet(const AttributeSet &AttrSet, bool InAttrGroup = false);
3014 void writeAllAttributeGroups();
3015
3016 void printTypeIdentities();
3017 void printGlobal(const GlobalVariable *GV);
3018 void printAlias(const GlobalAlias *GA);
3019 void printIFunc(const GlobalIFunc *GI);
3020 void printComdat(const Comdat *C);
3021 void printFunction(const Function *F);
3022 void printArgument(const Argument *FA, AttributeSet Attrs);
3023 void printBasicBlock(const BasicBlock *BB);
3024 void printInstructionLine(const Instruction &I);
3025 void printInstruction(const Instruction &I);
3026 void printDbgMarker(const DbgMarker &DPI);
3027 void printDbgVariableRecord(const DbgVariableRecord &DVR);
3028 void printDbgLabelRecord(const DbgLabelRecord &DLR);
3029 void printDbgRecord(const DbgRecord &DR);
3030 void printDbgRecordLine(const DbgRecord &DR);
3031
3032 void printUseListOrder(const Value *V, ArrayRef<unsigned> Shuffle);
3033 void printUseLists(const Function *F);
3034
3035 void printModuleSummaryIndex();
3036 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
3037 void printSummary(const GlobalValueSummary &Summary);
3038 void printAliasSummary(const AliasSummary *AS);
3039 void printGlobalVarSummary(const GlobalVarSummary *GS);
3040 void printFunctionSummary(const FunctionSummary *FS);
3041 void printTypeIdSummary(const TypeIdSummary &TIS);
3042 void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI);
3043 void printTypeTestResolution(const TypeTestResolution &TTRes);
3044 void printArgs(ArrayRef<uint64_t> Args);
3045 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
3046 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
3047 void printVFuncId(const FunctionSummary::VFuncId VFId);
3048 void printNonConstVCalls(ArrayRef<FunctionSummary::VFuncId> VCallList,
3049 const char *Tag);
3050 void printConstVCalls(ArrayRef<FunctionSummary::ConstVCall> VCallList,
3051 const char *Tag);
3052
3053private:
3054 /// Print out metadata attachments.
3055 void printMetadataAttachments(
3056 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
3057 StringRef Separator);
3058
3059 // printInfoComment - Print a little comment after the instruction indicating
3060 // which slot it occupies.
3061 void printInfoComment(const Value &V, bool isMaterializable = false);
3062
3063 // printGCRelocateComment - print comment after call to the gc.relocate
3064 // intrinsic indicating base and derived pointer names.
3065 void printGCRelocateComment(const GCRelocateInst &Relocate);
3066};
3067
3068} // end anonymous namespace
3069
3070// The options of what has no LLVMContext: a summary index or a detached debug
3071// record.
3072constexpr IROptions DefaultOptions;
3073
3074static const IROptions &getOptions(const Module *M) {
3075 return M ? getIROptions(M->getContext()) : DefaultOptions;
3076}
3077
3078AssemblyWriter::AssemblyWriter(const IROptions &Opts, formatted_raw_ostream &o,
3079 SlotTracker &Mac, const Module *M,
3080 AssemblyAnnotationWriter *AAW, bool IsForDebug,
3081 bool ShouldPreserveUseListOrder)
3082 : Opts(Opts), Out(o), TheModule(M), Machine(Mac), TypePrinter(M),
3083 AnnotationWriter(AAW), IsForDebug(IsForDebug),
3084 ShouldPreserveUseListOrder(
3085 valueOr(Opts.preserve_ll_uselistorder, ShouldPreserveUseListOrder)) {}
3086
3087AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
3088 const ModuleSummaryIndex *Index, bool IsForDebug)
3089 : Opts(DefaultOptions), Out(o), TheIndex(Index), Machine(Mac),
3090 TypePrinter(/*Module=*/nullptr), IsForDebug(IsForDebug),
3091 ShouldPreserveUseListOrder(false) {}
3092
3093void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
3094 if (!Operand) {
3095 Out << "<null operand!>";
3096 return;
3097 }
3098 auto WriteCtx = getContext();
3099 writeAsOperandInternal(Out, Operand, WriteCtx, PrintType);
3100}
3101
3102void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
3103 SyncScope::ID SSID) {
3104 switch (SSID) {
3105 case SyncScope::System: {
3106 break;
3107 }
3108 default: {
3109 if (SSNs.empty())
3110 Context.getSyncScopeNames(SSNs);
3111
3112 Out << " syncscope(\"";
3113 printEscapedString(SSNs[SSID], Out);
3114 Out << "\")";
3115 break;
3116 }
3117 }
3118}
3119
3120void AssemblyWriter::writeAtomic(const LLVMContext &Context,
3121 AtomicOrdering Ordering,
3122 SyncScope::ID SSID) {
3123 if (Ordering == AtomicOrdering::NotAtomic)
3124 return;
3125
3126 writeSyncScope(Context, SSID);
3127 Out << " " << toIRString(Ordering);
3128}
3129
3130void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
3131 AtomicOrdering SuccessOrdering,
3132 AtomicOrdering FailureOrdering,
3133 SyncScope::ID SSID) {
3134 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
3135 FailureOrdering != AtomicOrdering::NotAtomic);
3136
3137 writeSyncScope(Context, SSID);
3138 Out << " " << toIRString(SuccessOrdering);
3139 Out << " " << toIRString(FailureOrdering);
3140}
3141
3142void AssemblyWriter::writeParamOperand(const Value *Operand,
3143 AttributeSet Attrs) {
3144 if (!Operand) {
3145 Out << "<null operand!>";
3146 return;
3147 }
3148
3149 // Print the type
3150 TypePrinter.print(Operand->getType(), Out);
3151 // Print parameter attributes list
3152 if (Attrs.hasAttributes()) {
3153 Out << ' ';
3154 writeAttributeSet(Attrs);
3155 }
3156 Out << ' ';
3157 // Print the operand
3158 auto WriterCtx = getContext();
3159 writeAsOperandInternal(Out, Operand, WriterCtx);
3160}
3161
3162void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
3163 if (!Call->hasOperandBundles())
3164 return;
3165
3166 Out << " [ ";
3167
3168 ListSeparator LS;
3169 for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
3170 OperandBundleUse BU = Call->getOperandBundleAt(i);
3171
3172 Out << LS << '"';
3173 printEscapedString(BU.getTagName(), Out);
3174 Out << '"';
3175
3176 Out << '(';
3177
3178 ListSeparator InnerLS;
3179 auto WriterCtx = getContext();
3180 for (const auto &Input : BU.Inputs) {
3181 Out << InnerLS;
3182 if (Input == nullptr)
3183 Out << "<null operand bundle!>";
3184 else
3185 writeAsOperandInternal(Out, Input, WriterCtx, /*PrintType=*/true);
3186 }
3187
3188 Out << ')';
3189 }
3190
3191 Out << " ]";
3192}
3193
3194void AssemblyWriter::printModule(const Module *M) {
3195 Machine.initializeIfNeeded();
3196
3197 if (ShouldPreserveUseListOrder)
3198 UseListOrders = predictUseListOrder(M);
3199
3200 if (!M->getModuleIdentifier().empty() &&
3201 // Don't print the ID if it will start a new line (which would
3202 // require a comment char before it).
3203 M->getModuleIdentifier().find('\n') == std::string::npos)
3204 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
3205
3206 if (!M->getSourceFileName().empty()) {
3207 Out << "source_filename = \"";
3208 printEscapedString(M->getSourceFileName(), Out);
3209 Out << "\"\n";
3210 }
3211
3212 const std::string &DL = M->getDataLayoutStr();
3213 if (!DL.empty())
3214 Out << "target datalayout = \"" << DL << "\"\n";
3215 if (!M->getTargetTriple().empty())
3216 Out << "target triple = \"" << M->getTargetTriple().str() << "\"\n";
3217
3218 if (M->hasModuleInlineAsm()) {
3219 Out << '\n';
3220
3221 for (const Module::GlobalAsmFragment &Frag : M->getModuleInlineAsm()) {
3222 Out << "module asm";
3224 Frag.Props.getAsStrings();
3225 if (!Props.empty()) {
3226 ListSeparator LS;
3227 Out << "(";
3228 for (auto [Key, Value] : Props) {
3229 Out << LS;
3230 Out << Key << ": \"";
3232 Out << "\"";
3233 }
3234 Out << ")";
3235 }
3236 Out << "\n";
3237 // Split the string into lines, to make it easier to read the .ll file.
3238 StringRef Asm = Frag.Asm;
3239 do {
3240 StringRef Front;
3241 std::tie(Front, Asm) = Asm.split('\n');
3242
3243 // We found a newline, print the portion of the asm string from the
3244 // last newline up to this newline.
3245 Out << " \"";
3246 printEscapedString(Front, Out);
3247 Out << "\"\n";
3248 } while (!Asm.empty());
3249 }
3250 }
3251
3252 printTypeIdentities();
3253
3254 {
3255 // Output each used comdat at its first global object.
3256 SmallPtrSet<const Comdat *, 8> PrintedComdats;
3257 for (const GlobalObject &GO : M->global_objects()) {
3258 const Comdat *C = GO.getComdat();
3259 if (!C || !PrintedComdats.insert(C).second)
3260 continue;
3261 Out << '\n';
3262 printComdat(C);
3263 }
3264 }
3265
3266 // Output all globals.
3267 if (!M->global_empty()) Out << '\n';
3268 for (const GlobalVariable &GV : M->globals()) {
3269 printGlobal(&GV); Out << '\n';
3270 }
3271
3272 // Output all aliases.
3273 if (!M->alias_empty()) Out << "\n";
3274 for (const GlobalAlias &GA : M->aliases())
3275 printAlias(&GA);
3276
3277 // Output all ifuncs.
3278 if (!M->ifunc_empty()) Out << "\n";
3279 for (const GlobalIFunc &GI : M->ifuncs())
3280 printIFunc(&GI);
3281
3282 // Output all of the functions.
3283 for (const Function &F : *M) {
3284 Out << '\n';
3285 printFunction(&F);
3286 }
3287
3288 // Output global use-lists.
3289 printUseLists(nullptr);
3290
3291 // Output all attribute groups.
3292 if (!Machine.as_empty()) {
3293 Out << '\n';
3294 writeAllAttributeGroups();
3295 }
3296
3297 // Output named metadata.
3298 if (!M->named_metadata_empty()) Out << '\n';
3299
3300 for (const NamedMDNode &Node : M->named_metadata())
3301 printNamedMDNode(&Node);
3302
3303 // Output metadata.
3304 if (!Machine.mdn_empty()) {
3305 Out << '\n';
3306 writeAllMDNodes();
3307 }
3308}
3309
3310void AssemblyWriter::printModuleSummaryIndex() {
3311 assert(TheIndex);
3312 int NumSlots = Machine.initializeIndexIfNeeded();
3313
3314 Out << "\n";
3315
3316 // Print module path entries. To print in order, add paths to a vector
3317 // indexed by module slot.
3318 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
3319 std::string RegularLTOModuleName =
3321 moduleVec.resize(TheIndex->modulePaths().size());
3322 for (auto &[ModPath, ModHash] : TheIndex->modulePaths())
3323 moduleVec[Machine.getModulePathSlot(ModPath)] = std::make_pair(
3324 // An empty module path is a special entry for a regular LTO module
3325 // created during the thin link.
3326 ModPath.empty() ? RegularLTOModuleName : std::string(ModPath), ModHash);
3327
3328 unsigned i = 0;
3329 for (auto &ModPair : moduleVec) {
3330 Out << "^" << i++ << " = module: (";
3331 Out << "path: \"";
3332 printEscapedString(ModPair.first, Out);
3333 Out << "\", hash: (";
3334 ListSeparator FS;
3335 for (auto Hash : ModPair.second)
3336 Out << FS << Hash;
3337 Out << "))\n";
3338 }
3339
3340 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
3341 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
3342 // Sort by GUID for deterministic output matching slot assignment order.
3343 auto SortedGVS = TheIndex->sortedGlobalValueSummariesRange();
3344
3345 for (const auto &GlobalList : SortedGVS) {
3346 auto GUID = GlobalList.first;
3347 for (auto &Summary : GlobalList.second.getSummaryList())
3348 SummaryToGUIDMap[Summary.get()] = GUID;
3349 }
3350
3351 // Print the global value summary entries.
3352 for (const auto &GlobalList : SortedGVS) {
3353 auto GUID = GlobalList.first;
3354 auto VI = TheIndex->getValueInfo(GlobalList);
3355 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
3356 }
3357
3358 // Print the TypeIdMap entries.
3359 for (const auto &TID : TheIndex->typeIds()) {
3360 Out << "^" << Machine.getTypeIdSlot(TID.second.first)
3361 << " = typeid: (name: \"" << TID.second.first << "\"";
3362 printTypeIdSummary(TID.second.second);
3363 Out << ") ; guid = " << TID.first << "\n";
3364 }
3365
3366 // Print the TypeIdCompatibleVtableMap entries.
3367 for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
3369 Out << "^" << Machine.getTypeIdCompatibleVtableSlot(TId.first)
3370 << " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
3371 printTypeIdCompatibleVtableSummary(TId.second);
3372 Out << ") ; guid = " << GUID << "\n";
3373 }
3374
3375 // Don't emit flags when it's not really needed (value is zero by default).
3376 if (TheIndex->getFlags()) {
3377 Out << "^" << NumSlots << " = flags: " << TheIndex->getFlags() << "\n";
3378 ++NumSlots;
3379 }
3380
3381 Out << "^" << NumSlots << " = blockcount: " << TheIndex->getBlockCount()
3382 << "\n";
3383}
3384
3385static const char *
3387 switch (K) {
3389 return "indir";
3391 return "singleImpl";
3393 return "branchFunnel";
3394 }
3395 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
3396}
3397
3400 switch (K) {
3402 return "indir";
3404 return "uniformRetVal";
3406 return "uniqueRetVal";
3408 return "virtualConstProp";
3409 }
3410 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
3411}
3412
3414 switch (K) {
3416 return "unknown";
3418 return "unsat";
3420 return "byteArray";
3422 return "inline";
3424 return "single";
3426 return "allOnes";
3427 }
3428 llvm_unreachable("invalid TypeTestResolution kind");
3429}
3430
3431void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
3432 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
3433 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
3434
3435 // The following fields are only used if the target does not support the use
3436 // of absolute symbols to store constants. Print only if non-zero.
3437 if (TTRes.AlignLog2)
3438 Out << ", alignLog2: " << TTRes.AlignLog2;
3439 if (TTRes.SizeM1)
3440 Out << ", sizeM1: " << TTRes.SizeM1;
3441 if (TTRes.BitMask)
3442 // BitMask is uint8_t which causes it to print the corresponding char.
3443 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
3444 if (TTRes.InlineBits)
3445 Out << ", inlineBits: " << TTRes.InlineBits;
3446
3447 Out << ")";
3448}
3449
3450void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
3451 Out << ", summary: (";
3452 printTypeTestResolution(TIS.TTRes);
3453 if (!TIS.WPDRes.empty()) {
3454 Out << ", wpdResolutions: (";
3455 ListSeparator FS;
3456 for (auto &WPDRes : TIS.WPDRes) {
3457 Out << FS;
3458 Out << "(offset: " << WPDRes.first << ", ";
3459 printWPDRes(WPDRes.second);
3460 Out << ")";
3461 }
3462 Out << ")";
3463 }
3464 Out << ")";
3465}
3466
3467void AssemblyWriter::printTypeIdCompatibleVtableSummary(
3468 const TypeIdCompatibleVtableInfo &TI) {
3469 Out << ", summary: (";
3470 ListSeparator FS;
3471 for (auto &P : TI) {
3472 Out << FS;
3473 Out << "(offset: " << P.AddressPointOffset << ", ";
3474 Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID());
3475 Out << ")";
3476 }
3477 Out << ")";
3478}
3479
3480void AssemblyWriter::printArgs(ArrayRef<uint64_t> Args) {
3481 Out << "args: (" << llvm::interleaved(Args) << ')';
3482}
3483
3484void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
3485 Out << "wpdRes: (kind: ";
3487
3489 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
3490
3491 if (!WPDRes.ResByArg.empty()) {
3492 Out << ", resByArg: (";
3493 ListSeparator FS;
3494 for (auto &ResByArg : WPDRes.ResByArg) {
3495 Out << FS;
3496 printArgs(ResByArg.first);
3497 Out << ", byArg: (kind: ";
3498 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
3499 if (ResByArg.second.TheKind ==
3501 ResByArg.second.TheKind ==
3503 Out << ", info: " << ResByArg.second.Info;
3504
3505 // The following fields are only used if the target does not support the
3506 // use of absolute symbols to store constants. Print only if non-zero.
3507 if (ResByArg.second.Byte || ResByArg.second.Bit)
3508 Out << ", byte: " << ResByArg.second.Byte
3509 << ", bit: " << ResByArg.second.Bit;
3510
3511 Out << ")";
3512 }
3513 Out << ")";
3514 }
3515 Out << ")";
3516}
3517
3519 switch (SK) {
3521 return "alias";
3523 return "function";
3525 return "variable";
3526 }
3527 llvm_unreachable("invalid summary kind");
3528}
3529
3530void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
3531 Out << ", aliasee: ";
3532 // The indexes emitted for distributed backends may not include the
3533 // aliasee summary (only if it is being imported directly). Handle
3534 // that case by just emitting "null" as the aliasee.
3535 if (AS->hasAliasee())
3536 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
3537 else
3538 Out << "null";
3539}
3540
3541void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
3542 auto VTableFuncs = GS->vTableFuncs();
3543 Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", "
3544 << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ", "
3545 << "constant: " << GS->VarFlags.Constant;
3546 if (!VTableFuncs.empty())
3547 Out << ", "
3548 << "vcall_visibility: " << GS->VarFlags.VCallVisibility;
3549 Out << ")";
3550
3551 if (!VTableFuncs.empty()) {
3552 Out << ", vTableFuncs: (";
3553 ListSeparator FS;
3554 for (auto &P : VTableFuncs) {
3555 Out << FS;
3556 Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID())
3557 << ", offset: " << P.VTableOffset;
3558 Out << ")";
3559 }
3560 Out << ")";
3561 }
3562}
3563
3565 switch (LT) {
3567 return "external";
3569 return "private";
3571 return "internal";
3573 return "linkonce";
3575 return "linkonce_odr";
3577 return "weak";
3579 return "weak_odr";
3581 return "common";
3583 return "appending";
3585 return "extern_weak";
3587 return "available_externally";
3588 }
3589 llvm_unreachable("invalid linkage");
3590}
3591
3592// When printing the linkage types in IR where the ExternalLinkage is
3593// not printed, and other linkage types are expected to be printed with
3594// a space after the name.
3597 return "";
3598 return getLinkageName(LT) + " ";
3599}
3600
3602 switch (Vis) {
3604 return "default";
3606 return "hidden";
3608 return "protected";
3609 }
3610 llvm_unreachable("invalid visibility");
3611}
3612
3614 switch (IK) {
3616 return "definition";
3618 return "declaration";
3619 }
3620 llvm_unreachable("invalid import kind");
3621}
3622
3623void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
3624 Out << ", insts: " << FS->instCount();
3625 if (FS->fflags().anyFlagSet())
3626 Out << ", " << FS->fflags();
3627
3628 if (!FS->calls().empty()) {
3629 Out << ", calls: (";
3630 ListSeparator IFS;
3631 for (auto &Call : FS->calls()) {
3632 Out << IFS;
3633 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
3634 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
3635 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
3636 // Follow the convention of emitting flags as a boolean value, but only
3637 // emit if true to avoid unnecessary verbosity and test churn.
3638 if (Call.second.HasTailCall)
3639 Out << ", tail: 1";
3640 if (Call.second.HasIndirectCall)
3641 Out << ", indirect: 1";
3642 Out << ")";
3643 }
3644 Out << ")";
3645 }
3646
3647 if (const auto *TIdInfo = FS->getTypeIdInfo())
3648 printTypeIdInfo(*TIdInfo);
3649
3650 // The AllocationType identifiers capture the profiled context behavior
3651 // reaching a specific static allocation site (possibly cloned).
3652 auto AllocTypeName = [](uint8_t Type) -> const char * {
3653 switch (Type) {
3654 case (uint8_t)AllocationType::None:
3655 return "none";
3656 case (uint8_t)AllocationType::NotCold:
3657 return "notcold";
3658 case (uint8_t)AllocationType::Cold:
3659 return "cold";
3660 case (uint8_t)AllocationType::Hot:
3661 return "hot";
3662 case (uint8_t)AllocationType::NotCold | (uint8_t)AllocationType::Cold:
3663 // The contexts reaching the allocation could not be disambiguated.
3664 return "ambiguous";
3665 }
3666 llvm_unreachable("Unexpected alloc type");
3667 };
3668
3669 if (!FS->allocs().empty()) {
3670 Out << ", allocs: (";
3671 ListSeparator AFS;
3672 for (auto &AI : FS->allocs()) {
3673 Out << AFS;
3674 Out << "(versions: (";
3675 ListSeparator VFS;
3676 for (auto V : AI.Versions) {
3677 Out << VFS;
3678 Out << AllocTypeName(V);
3679 }
3680 Out << "), memProf: (";
3681 ListSeparator MIBFS;
3682 for (auto &MIB : AI.MIBs) {
3683 Out << MIBFS;
3684 Out << "(type: " << AllocTypeName((uint8_t)MIB.AllocType);
3685 Out << ", stackIds: (";
3686 ListSeparator SIDFS;
3687 for (auto Id : MIB.StackIdIndices) {
3688 Out << SIDFS;
3689 Out << TheIndex->getStackIdAtIndex(Id);
3690 }
3691 Out << "))";
3692 }
3693 Out << "))";
3694 }
3695 Out << ")";
3696 }
3697
3698 if (!FS->callsites().empty()) {
3699 Out << ", callsites: (";
3700 ListSeparator SNFS;
3701 for (auto &CI : FS->callsites()) {
3702 Out << SNFS;
3703 if (CI.Callee)
3704 Out << "(callee: ^" << Machine.getGUIDSlot(CI.Callee.getGUID());
3705 else
3706 Out << "(callee: null";
3707 Out << ", clones: (";
3708 ListSeparator VFS;
3709 for (auto V : CI.Clones) {
3710 Out << VFS;
3711 Out << V;
3712 }
3713 Out << "), stackIds: (";
3714 ListSeparator SIDFS;
3715 for (auto Id : CI.StackIdIndices) {
3716 Out << SIDFS;
3717 Out << TheIndex->getStackIdAtIndex(Id);
3718 }
3719 Out << "))";
3720 }
3721 Out << ")";
3722 }
3723
3724 auto PrintRange = [&](const ConstantRange &Range) {
3725 Out << "[" << Range.getSignedMin() << ", " << Range.getSignedMax() << "]";
3726 };
3727
3728 if (!FS->paramAccesses().empty()) {
3729 Out << ", params: (";
3730 ListSeparator IFS;
3731 for (auto &PS : FS->paramAccesses()) {
3732 Out << IFS;
3733 Out << "(param: " << PS.ParamNo;
3734 Out << ", offset: ";
3735 PrintRange(PS.Use);
3736 if (!PS.Calls.empty()) {
3737 Out << ", calls: (";
3738 ListSeparator IFS;
3739 for (auto &Call : PS.Calls) {
3740 Out << IFS;
3741 Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee.getGUID());
3742 Out << ", param: " << Call.ParamNo;
3743 Out << ", offset: ";
3744 PrintRange(Call.Offsets);
3745 Out << ")";
3746 }
3747 Out << ")";
3748 }
3749 Out << ")";
3750 }
3751 Out << ")";
3752 }
3753}
3754
3755void AssemblyWriter::printTypeIdInfo(
3756 const FunctionSummary::TypeIdInfo &TIDInfo) {
3757 Out << ", typeIdInfo: (";
3758 ListSeparator TIDFS;
3759 if (!TIDInfo.TypeTests.empty()) {
3760 Out << TIDFS;
3761 Out << "typeTests: (";
3762 ListSeparator FS;
3763 for (auto &GUID : TIDInfo.TypeTests) {
3764 auto TidIter = TheIndex->typeIds().equal_range(GUID);
3765 if (TidIter.first == TidIter.second) {
3766 Out << FS;
3767 Out << GUID;
3768 continue;
3769 }
3770 // Print all type id that correspond to this GUID.
3771 for (const auto &[GUID, TypeIdPair] : make_range(TidIter)) {
3772 Out << FS;
3773 auto Slot = Machine.getTypeIdSlot(TypeIdPair.first);
3774 assert(Slot != -1);
3775 Out << "^" << Slot;
3776 }
3777 }
3778 Out << ")";
3779 }
3780 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
3781 Out << TIDFS;
3782 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
3783 }
3784 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
3785 Out << TIDFS;
3786 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
3787 }
3788 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
3789 Out << TIDFS;
3790 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
3791 "typeTestAssumeConstVCalls");
3792 }
3793 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
3794 Out << TIDFS;
3795 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
3796 "typeCheckedLoadConstVCalls");
3797 }
3798 Out << ")";
3799}
3800
3801void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
3802 auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
3803 if (TidIter.first == TidIter.second) {
3804 Out << "vFuncId: (";
3805 Out << "guid: " << VFId.GUID;
3806 Out << ", offset: " << VFId.Offset;
3807 Out << ")";
3808 return;
3809 }
3810 // Print all type id that correspond to this GUID.
3811 ListSeparator FS;
3812 for (const auto &[GUID, TypeIdPair] : make_range(TidIter)) {
3813 Out << FS;
3814 Out << "vFuncId: (";
3815 auto Slot = Machine.getTypeIdSlot(TypeIdPair.first);
3816 assert(Slot != -1);
3817 Out << "^" << Slot;
3818 Out << ", offset: " << VFId.Offset;
3819 Out << ")";
3820 }
3821}
3822
3823void AssemblyWriter::printNonConstVCalls(
3824 ArrayRef<FunctionSummary::VFuncId> VCallList, const char *Tag) {
3825 Out << Tag << ": (";
3826 ListSeparator FS;
3827 for (auto &VFuncId : VCallList) {
3828 Out << FS;
3829 printVFuncId(VFuncId);
3830 }
3831 Out << ")";
3832}
3833
3834void AssemblyWriter::printConstVCalls(
3835 ArrayRef<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
3836 Out << Tag << ": (";
3837 ListSeparator FS;
3838 for (auto &ConstVCall : VCallList) {
3839 Out << FS;
3840 Out << "(";
3841 printVFuncId(ConstVCall.VFunc);
3842 if (!ConstVCall.Args.empty()) {
3843 Out << ", ";
3844 printArgs(ConstVCall.Args);
3845 }
3846 Out << ")";
3847 }
3848 Out << ")";
3849}
3850
3851void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
3852 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
3854 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
3855 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
3856 << ", flags: (";
3857 Out << "linkage: " << getLinkageName(LT);
3858 Out << ", visibility: "
3860 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
3861 Out << ", live: " << GVFlags.Live;
3862 Out << ", dsoLocal: " << GVFlags.DSOLocal;
3863 Out << ", canAutoHide: " << GVFlags.CanAutoHide;
3864 Out << ", importType: "
3866 Out << ", noRenameOnPromotion: " << GVFlags.NoRenameOnPromotion;
3867 Out << ")";
3868
3869 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
3870 printAliasSummary(cast<AliasSummary>(&Summary));
3871 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
3872 printFunctionSummary(cast<FunctionSummary>(&Summary));
3873 else
3874 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
3875
3876 auto RefList = Summary.refs();
3877 if (!RefList.empty()) {
3878 Out << ", refs: (";
3879 ListSeparator FS;
3880 for (auto &Ref : RefList) {
3881 Out << FS;
3882 if (Ref.isReadOnly())
3883 Out << "readonly ";
3884 else if (Ref.isWriteOnly())
3885 Out << "writeonly ";
3886 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
3887 }
3888 Out << ")";
3889 }
3890
3891 Out << ")";
3892}
3893
3894void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3895 Out << "^" << Slot << " = gv: (";
3896 if (VI.hasName() && !VI.name().empty())
3897 Out << "name: \"" << VI.name() << "\"";
3898 else
3899 Out << "guid: " << VI.getGUID();
3900 if (!VI.getSummaryList().empty()) {
3901 Out << ", summaries: (";
3902 ListSeparator FS;
3903 for (auto &Summary : VI.getSummaryList()) {
3904 Out << FS;
3905 printSummary(*Summary);
3906 }
3907 Out << ")";
3908 }
3909 Out << ")";
3910 if (VI.hasName() && !VI.name().empty())
3911 Out << " ; guid = " << VI.getGUID();
3912 Out << "\n";
3913}
3914
3916 formatted_raw_ostream &Out) {
3917 if (Name.empty()) {
3918 Out << "<empty name> ";
3919 } else {
3920 unsigned char FirstC = static_cast<unsigned char>(Name[0]);
3921 if (isalpha(FirstC) || FirstC == '-' || FirstC == '$' || FirstC == '.' ||
3922 FirstC == '_')
3923 Out << FirstC;
3924 else
3925 Out << '\\' << hexdigit(FirstC >> 4) << hexdigit(FirstC & 0x0F);
3926 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3927 unsigned char C = Name[i];
3928 if (isalnum(C) || C == '-' || C == '$' || C == '.' || C == '_')
3929 Out << C;
3930 else
3931 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3932 }
3933 }
3934}
3935
3936void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3937 Out << '!';
3938 printMetadataIdentifier(NMD->getName(), Out);
3939 Out << " = !{";
3940 ListSeparator LS;
3941 for (const MDNode *Op : NMD->operands()) {
3942 Out << LS;
3943 // Write DIExpressions inline.
3944 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3945 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3946 writeDIExpression(Out, Expr, AsmWriterContext::getEmpty());
3947 continue;
3948 }
3949
3950 int Slot = Machine.getMetadataSlot(Op);
3951 if (Slot == -1)
3952 Out << "<badref>";
3953 else
3954 Out << '!' << Slot;
3955 }
3956 Out << "}\n";
3957}
3958
3960 formatted_raw_ostream &Out) {
3961 switch (Vis) {
3963 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3964 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3965 }
3966}
3967
3968static void printDSOLocation(const GlobalValue &GV,
3969 formatted_raw_ostream &Out) {
3970 if (GV.isDSOLocal() && !GV.isImplicitDSOLocal())
3971 Out << "dso_local ";
3972}
3973
3975 formatted_raw_ostream &Out) {
3976 switch (SCT) {
3978 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3979 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3980 }
3981}
3982
3984 formatted_raw_ostream &Out) {
3985 switch (TLM) {
3987 break;
3989 Out << "thread_local ";
3990 break;
3992 Out << "thread_local(localdynamic) ";
3993 break;
3995 Out << "thread_local(initialexec) ";
3996 break;
3998 Out << "thread_local(localexec) ";
3999 break;
4000 }
4001}
4002
4004 switch (UA) {
4006 return "";
4008 return "local_unnamed_addr";
4010 return "unnamed_addr";
4011 }
4012 llvm_unreachable("Unknown UnnamedAddr");
4013}
4014
4016 const GlobalObject &GO) {
4017 const Comdat *C = GO.getComdat();
4018 if (!C)
4019 return;
4020
4021 if (isa<GlobalVariable>(GO))
4022 Out << ',';
4023 Out << " comdat";
4024
4025 if (GO.getName() == C->getName())
4026 return;
4027
4028 Out << '(';
4029 printLLVMName(Out, C->getName(), ComdatPrefix);
4030 Out << ')';
4031}
4032
4033void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
4034 if (GV->isMaterializable())
4035 Out << "; Materializable\n";
4036
4037 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GV->getParent());
4038 writeAsOperandInternal(Out, GV, WriterCtx);
4039 Out << " = ";
4040
4041 if (!GV->hasInitializer() && GV->hasExternalLinkage())
4042 Out << "external ";
4043
4044 Out << getLinkageNameWithSpace(GV->getLinkage());
4045 printDSOLocation(*GV, Out);
4046 printVisibility(GV->getVisibility(), Out);
4049 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
4050 if (!UA.empty())
4051 Out << UA << ' ';
4052
4054 /*Prefix=*/"", /*Suffix=*/" ");
4055 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
4056 Out << (GV->isConstant() ? "constant " : "global ");
4057 TypePrinter.print(GV->getValueType(), Out);
4058
4059 if (GV->hasInitializer()) {
4060 Out << ' ';
4061 writeOperand(GV->getInitializer(), false);
4062 }
4063
4064 if (GV->hasSection()) {
4065 Out << ", section \"";
4066 printEscapedString(GV->getSection(), Out);
4067 Out << '"';
4068 }
4069 if (GV->hasPartition()) {
4070 Out << ", partition \"";
4071 printEscapedString(GV->getPartition(), Out);
4072 Out << '"';
4073 }
4074 if (auto CM = GV->getCodeModel()) {
4075 Out << ", code_model \"";
4076 switch (*CM) {
4077 case CodeModel::Tiny:
4078 Out << "tiny";
4079 break;
4080 case CodeModel::Small:
4081 Out << "small";
4082 break;
4083 case CodeModel::Kernel:
4084 Out << "kernel";
4085 break;
4086 case CodeModel::Medium:
4087 Out << "medium";
4088 break;
4089 case CodeModel::Large:
4090 Out << "large";
4091 break;
4092 }
4093 Out << '"';
4094 }
4095
4096 using SanitizerMetadata = llvm::GlobalValue::SanitizerMetadata;
4097 if (GV->hasSanitizerMetadata()) {
4099 if (MD.NoAddress)
4100 Out << ", no_sanitize_address";
4101 if (MD.NoHWAddress)
4102 Out << ", no_sanitize_hwaddress";
4103 if (MD.Memtag)
4104 Out << ", sanitize_memtag";
4105 if (MD.IsDynInit)
4106 Out << ", sanitize_address_dyninit";
4107 }
4108
4109 maybePrintComdat(Out, *GV);
4110 if (MaybeAlign A = GV->getAlign())
4111 Out << ", align " << A->value();
4112
4114 GV->getAllMetadata(MDs);
4115 printMetadataAttachments(MDs, ", ");
4116
4117 auto Attrs = GV->getAttributes();
4118 if (Attrs.hasAttributes())
4119 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
4120
4121 printInfoComment(*GV, GV->isMaterializable());
4122}
4123
4124void AssemblyWriter::printAlias(const GlobalAlias *GA) {
4125 if (GA->isMaterializable())
4126 Out << "; Materializable\n";
4127
4128 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GA->getParent());
4129 writeAsOperandInternal(Out, GA, WriterCtx);
4130 Out << " = ";
4131
4132 Out << getLinkageNameWithSpace(GA->getLinkage());
4133 printDSOLocation(*GA, Out);
4134 printVisibility(GA->getVisibility(), Out);
4137 StringRef UA = getUnnamedAddrEncoding(GA->getUnnamedAddr());
4138 if (!UA.empty())
4139 Out << UA << ' ';
4140
4141 Out << "alias ";
4142
4143 TypePrinter.print(GA->getValueType(), Out);
4144 Out << ", ";
4145
4146 if (const Constant *Aliasee = GA->getAliasee()) {
4147 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
4148 } else {
4149 TypePrinter.print(GA->getType(), Out);
4150 Out << " <<NULL ALIASEE>>";
4151 }
4152
4153 if (GA->hasPartition()) {
4154 Out << ", partition \"";
4155 printEscapedString(GA->getPartition(), Out);
4156 Out << '"';
4157 }
4158
4159 printInfoComment(*GA, GA->isMaterializable());
4160 Out << '\n';
4161}
4162
4163void AssemblyWriter::printIFunc(const GlobalIFunc *GI) {
4164 if (GI->isMaterializable())
4165 Out << "; Materializable\n";
4166
4167 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GI->getParent());
4168 writeAsOperandInternal(Out, GI, WriterCtx);
4169 Out << " = ";
4170
4171 Out << getLinkageNameWithSpace(GI->getLinkage());
4172 printDSOLocation(*GI, Out);
4173 printVisibility(GI->getVisibility(), Out);
4174
4175 Out << "ifunc ";
4176
4177 TypePrinter.print(GI->getValueType(), Out);
4178 Out << ", ";
4179
4180 if (const Constant *Resolver = GI->getResolver()) {
4181 writeOperand(Resolver, !isa<ConstantExpr>(Resolver));
4182 } else {
4183 TypePrinter.print(GI->getType(), Out);
4184 Out << " <<NULL RESOLVER>>";
4185 }
4186
4187 if (GI->hasPartition()) {
4188 Out << ", partition \"";
4189 printEscapedString(GI->getPartition(), Out);
4190 Out << '"';
4191 }
4193 GI->getAllMetadata(MDs);
4194 if (!MDs.empty()) {
4195 printMetadataAttachments(MDs, ", ");
4196 }
4197
4198 printInfoComment(*GI, GI->isMaterializable());
4199 Out << '\n';
4200}
4201
4202void AssemblyWriter::printComdat(const Comdat *C) {
4203 C->print(Out);
4204}
4205
4206void AssemblyWriter::printTypeIdentities() {
4207 if (TypePrinter.empty())
4208 return;
4209
4210 Out << '\n';
4211
4212 // Emit all numbered types.
4213 auto &NumberedTypes = TypePrinter.getNumberedTypes();
4214 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
4215 Out << '%' << I << " = type ";
4216
4217 // Make sure we print out at least one level of the type structure, so
4218 // that we do not get %2 = type %2
4219 TypePrinter.printStructBody(NumberedTypes[I], Out);
4220 Out << '\n';
4221 }
4222
4223 auto &NamedTypes = TypePrinter.getNamedTypes();
4224 for (StructType *NamedType : NamedTypes) {
4225 printLLVMName(Out, NamedType->getName(), LocalPrefix);
4226 Out << " = type ";
4227
4228 // Make sure we print out at least one level of the type structure, so
4229 // that we do not get %FILE = type %FILE
4230 TypePrinter.printStructBody(NamedType, Out);
4231 Out << '\n';
4232 }
4233}
4234
4235/// printFunction - Print all aspects of a function.
4236void AssemblyWriter::printFunction(const Function *F) {
4237 if (F->isMaterializable())
4238 Out << "; Materializable\n";
4239 else if (AnnotationWriter)
4240 AnnotationWriter->emitFunctionAnnot(F, Out);
4241
4242 const AttributeList &Attrs = F->getAttributes();
4243 if (Attrs.hasFnAttrs()) {
4244 AttributeSet AS = Attrs.getFnAttrs();
4245 std::string AttrStr;
4246
4247 for (const Attribute &Attr : AS) {
4248 if (!Attr.isStringAttribute()) {
4249 if (!AttrStr.empty()) AttrStr += ' ';
4250 AttrStr += Attr.getAsString();
4251 }
4252 }
4253
4254 if (!AttrStr.empty())
4255 Out << "; Function Attrs: " << AttrStr << '\n';
4256 }
4257
4258 if (F->isIntrinsic() && F->getIntrinsicID() == Intrinsic::not_intrinsic)
4259 Out << "; Unknown intrinsic\n";
4260
4261 Machine.incorporateFunction(F);
4262
4263 if (F->isDeclaration()) {
4264 Out << "declare";
4266 F->getAllMetadata(MDs);
4267 printMetadataAttachments(MDs, " ");
4268 Out << ' ';
4269 } else
4270 Out << "define ";
4271
4272 Out << getLinkageNameWithSpace(F->getLinkage());
4273 printDSOLocation(*F, Out);
4274 printVisibility(F->getVisibility(), Out);
4275 printDLLStorageClass(F->getDLLStorageClass(), Out);
4276
4277 // Print the calling convention.
4278 if (F->getCallingConv() != CallingConv::C) {
4279 printCallingConv(F->getCallingConv(), Out);
4280 Out << " ";
4281 }
4282
4283 FunctionType *FT = F->getFunctionType();
4284 if (Attrs.hasRetAttrs())
4285 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
4286 TypePrinter.print(F->getReturnType(), Out);
4287 AsmWriterContext WriterCtx(&TypePrinter, &Machine, F->getParent());
4288 Out << ' ';
4289 writeAsOperandInternal(Out, F, WriterCtx);
4290 Out << '(';
4291
4292 // Loop over the arguments, printing them...
4293 if (F->isDeclaration() && !IsForDebug) {
4294 // We're only interested in the type here - don't print argument names.
4295 ListSeparator LS;
4296 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
4297 Out << LS;
4298 // Output type.
4299 TypePrinter.print(FT->getParamType(I), Out);
4300
4301 AttributeSet ArgAttrs = Attrs.getParamAttrs(I);
4302 if (ArgAttrs.hasAttributes()) {
4303 Out << ' ';
4304 writeAttributeSet(ArgAttrs);
4305 }
4306 }
4307 } else {
4308 // The arguments are meaningful here, print them in detail.
4309 ListSeparator LS;
4310 for (const Argument &Arg : F->args()) {
4311 Out << LS;
4312 printArgument(&Arg, Attrs.getParamAttrs(Arg.getArgNo()));
4313 }
4314 }
4315
4316 // Finish printing arguments...
4317 if (FT->isVarArg()) {
4318 if (FT->getNumParams()) Out << ", ";
4319 Out << "..."; // Output varargs portion of signature!
4320 }
4321 Out << ')';
4322 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
4323 if (!UA.empty())
4324 Out << ' ' << UA;
4325 // We print the function address space if it is non-zero or if we are writing
4326 // a module with a non-zero program address space or if there is no valid
4327 // Module* so that the file can be parsed without the datalayout string.
4328 const Module *Mod = F->getParent();
4329 bool ForcePrintAddressSpace =
4330 !Mod || Mod->getDataLayout().getProgramAddressSpace() != 0;
4331 printAddressSpace(Mod, F->getAddressSpace(), Out, /*Prefix=*/" ",
4332 /*Suffix=*/"", ForcePrintAddressSpace);
4333 if (Attrs.hasFnAttrs())
4334 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttrs());
4335 if (F->hasSection()) {
4336 Out << " section \"";
4337 printEscapedString(F->getSection(), Out);
4338 Out << '"';
4339 }
4340 if (F->hasPartition()) {
4341 Out << " partition \"";
4342 printEscapedString(F->getPartition(), Out);
4343 Out << '"';
4344 }
4345 maybePrintComdat(Out, *F);
4346 if (MaybeAlign A = F->getAlign())
4347 Out << " align " << A->value();
4348 if (MaybeAlign A = F->getPreferredAlignment())
4349 Out << " prefalign(" << A->value() << ')';
4350 if (F->hasGC())
4351 Out << " gc \"" << F->getGC() << '"';
4352 if (F->hasPrefixData()) {
4353 Out << " prefix ";
4354 writeOperand(F->getPrefixData(), true);
4355 }
4356 if (F->hasPrologueData()) {
4357 Out << " prologue ";
4358 writeOperand(F->getPrologueData(), true);
4359 }
4360 if (F->hasPersonalityFn()) {
4361 Out << " personality ";
4362 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
4363 }
4364
4365 if (Opts.print_prof_data) {
4366 if (auto *MDProf = F->getMetadata(LLVMContext::MD_prof)) {
4367 Out << " ";
4368 MDProf->print(Out, TheModule, /*IsForDebug=*/true);
4369 }
4370 }
4371
4372 if (F->isDeclaration()) {
4373 Out << '\n';
4374 } else {
4376 F->getAllMetadata(MDs);
4377 printMetadataAttachments(MDs, " ");
4378
4379 Out << " {";
4380 // Output all of the function's basic blocks.
4381 for (const BasicBlock &BB : *F)
4382 printBasicBlock(&BB);
4383
4384 // Output the function's use-lists.
4385 printUseLists(F);
4386
4387 Out << "}\n";
4388 }
4389
4390 Machine.purgeFunction();
4391}
4392
4393/// printArgument - This member is called for every argument that is passed into
4394/// the function. Simply print it out
4395void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
4396 // Output type...
4397 TypePrinter.print(Arg->getType(), Out);
4398
4399 // Output parameter attributes list
4400 if (Attrs.hasAttributes()) {
4401 Out << ' ';
4402 writeAttributeSet(Attrs);
4403 }
4404
4405 // Output name, if available...
4406 if (Arg->hasName()) {
4407 Out << ' ';
4408 printLLVMName(Out, Arg);
4409 } else {
4410 int Slot = Machine.getLocalSlot(Arg);
4411 assert(Slot != -1 && "expect argument in function here");
4412 Out << " %" << Slot;
4413 }
4414}
4415
4416/// printBasicBlock - This member is called for each basic block in a method.
4417void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
4418 bool IsEntryBlock = BB->getParent() && BB->isEntryBlock();
4419 if (BB->hasName()) { // Print out the label if it exists...
4420 Out << "\n";
4421 printLLVMName(Out, BB->getName(), LabelPrefix);
4422 Out << ':';
4423 } else if (!IsEntryBlock) {
4424 Out << "\n";
4425 int Slot = Machine.getLocalSlot(BB);
4426 if (Slot != -1)
4427 Out << Slot << ":";
4428 else
4429 Out << "<badref>:";
4430 }
4431
4432 if (!IsEntryBlock) {
4433 // Output predecessors for the block.
4434 Out.PadToColumn(50);
4435 Out << ";";
4436 if (pred_empty(BB)) {
4437 Out << " No predecessors!";
4438 } else {
4439 Out << " preds = ";
4440 ListSeparator LS;
4441 for (const BasicBlock *Pred : predecessors(BB)) {
4442 Out << LS;
4443 writeOperand(Pred, false);
4444 }
4445 }
4446 }
4447
4448 Out << "\n";
4449
4450 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
4451
4452 // Output all of the instructions in the basic block...
4453 for (const Instruction &I : *BB) {
4454 for (const DbgRecord &DR : I.getDbgRecordRange())
4455 printDbgRecordLine(DR);
4456 printInstructionLine(I);
4457 }
4458
4459 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
4460}
4461
4462/// printInstructionLine - Print an instruction and a newline character.
4463void AssemblyWriter::printInstructionLine(const Instruction &I) {
4464 printInstruction(I);
4465 Out << '\n';
4466}
4467
4468/// printGCRelocateComment - print comment after call to the gc.relocate
4469/// intrinsic indicating base and derived pointer names.
4470void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
4471 Out << " ; (";
4472 if (Value *BasePtr = Relocate.getBasePtr())
4473 writeOperand(BasePtr, false);
4474 else
4475 Out << "invalid";
4476 Out << ", ";
4477 if (Value *DerivedPtr = Relocate.getDerivedPtr())
4478 writeOperand(DerivedPtr, false);
4479 else
4480 Out << "invalid";
4481 Out << ")";
4482}
4483
4484/// printInfoComment - Print a little comment after the instruction indicating
4485/// which slot it occupies.
4486void AssemblyWriter::printInfoComment(const Value &V, bool isMaterializable) {
4487 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
4488 printGCRelocateComment(*Relocate);
4489
4490 if (AnnotationWriter && !isMaterializable)
4491 AnnotationWriter->printInfoComment(V, Out);
4492
4493 if (Opts.print_inst_debug_locs) {
4494 if (auto *I = dyn_cast<Instruction>(&V)) {
4495 if (I->getDebugLoc()) {
4496 Out << " ; ";
4497 I->getDebugLoc().print(Out);
4498 }
4499 }
4500 }
4501 if (Opts.print_prof_data) {
4502 if (auto *I = dyn_cast<Instruction>(&V)) {
4503 if (auto *MD = I->getMetadata(LLVMContext::MD_prof)) {
4504 Out << " ; ";
4505 MD->print(Out, TheModule, /*IsForDebug=*/true);
4506 }
4507 }
4508 }
4509
4510 if (Opts.print_inst_addrs)
4511 Out << " ; " << &V;
4512}
4513
4514static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
4515 raw_ostream &Out) {
4516 if (Operand == nullptr) {
4517 Out << " <cannot get addrspace!>";
4518 return;
4519 }
4520
4521 // We print the address space of the call if it is non-zero.
4522 // We also print it if it is zero but not equal to the program address space
4523 // or if we can't find a valid Module* to make it possible to parse
4524 // the resulting file even without a datalayout string.
4525 unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
4526 const Module *Mod = getModuleFromVal(I);
4527 bool ForcePrintAddrSpace =
4528 !Mod || Mod->getDataLayout().getProgramAddressSpace() != 0;
4529 printAddressSpace(Mod, CallAddrSpace, Out, /*Prefix=*/" ", /*Suffix=*/"",
4530 ForcePrintAddrSpace);
4531}
4532
4533// This member is called for each Instruction in a function..
4534void AssemblyWriter::printInstruction(const Instruction &I) {
4535 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
4536
4537 // Print out indentation for an instruction.
4538 Out << " ";
4539
4540 // Print out name if it exists...
4541 if (I.hasName()) {
4542 printLLVMName(Out, &I);
4543 Out << " = ";
4544 } else if (!I.getType()->isVoidTy()) {
4545 // Print out the def slot taken.
4546 int SlotNum = Machine.getLocalSlot(&I);
4547 if (SlotNum == -1)
4548 Out << "<badref> = ";
4549 else
4550 Out << '%' << SlotNum << " = ";
4551 }
4552
4553 if (const auto *CI = dyn_cast<CallInst>(&I)) {
4554 if (CI->isMustTailCall())
4555 Out << "musttail ";
4556 else if (CI->isTailCall())
4557 Out << "tail ";
4558 else if (CI->isNoTailCall())
4559 Out << "notail ";
4560 }
4561
4562 // Print out the opcode...
4563 Out << I.getOpcodeName();
4564
4565 // If this is an atomic load or store, print out the atomic marker.
4566 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
4568 Out << " atomic";
4569
4571 Out << " weak";
4572
4573 // If this is a volatile operation, print out the volatile marker.
4574 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
4575 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
4576 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
4577 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
4578 Out << " volatile";
4579
4580 // Print the elementwise marker for atomic loads and stores.
4583 Out << " elementwise";
4584
4585 // Print out optimization information.
4586 writeOptimizationInfo(Out, &I);
4587
4588 // Print out the compare instruction predicates
4589 if (const auto *CI = dyn_cast<CmpInst>(&I))
4590 Out << ' ' << CI->getPredicate();
4591
4592 // Print out the atomicrmw operation
4593 if (const auto *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
4594 if (RMWI->isElementwise())
4595 Out << " elementwise";
4596 Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
4597 }
4598
4599 // Print out the type of the operands...
4600 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
4601
4602 // Special case conditional branches to swizzle the condition out to the front
4603 if (const auto *BI = dyn_cast<CondBrInst>(&I)) {
4604 Out << ' ';
4605 writeOperand(BI->getCondition(), true);
4606 Out << ", ";
4607 writeOperand(BI->getSuccessor(0), true);
4608 Out << ", ";
4609 writeOperand(BI->getSuccessor(1), true);
4610 } else if (isa<SwitchInst>(I)) {
4611 const SwitchInst& SI(cast<SwitchInst>(I));
4612 // Special case switch instruction to get formatting nice and correct.
4613 Out << ' ';
4614 writeOperand(SI.getCondition(), true);
4615 Out << ", ";
4616 writeOperand(SI.getDefaultDest(), true);
4617 Out << " [";
4618 for (auto Case : SI.cases()) {
4619 Out << "\n ";
4620 writeOperand(Case.getCaseValue(), true);
4621 Out << ", ";
4622 writeOperand(Case.getCaseSuccessor(), true);
4623 }
4624 Out << "\n ]";
4625 } else if (isa<IndirectBrInst>(I)) {
4626 // Special case indirectbr instruction to get formatting nice and correct.
4627 Out << ' ';
4628 writeOperand(Operand, true);
4629 Out << ", [";
4630
4631 ListSeparator LS;
4632 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
4633 Out << LS;
4634 writeOperand(I.getOperand(i), true);
4635 }
4636 Out << ']';
4637 } else if (const auto *PN = dyn_cast<PHINode>(&I)) {
4638 Out << ' ';
4639 TypePrinter.print(I.getType(), Out);
4640 Out << ' ';
4641
4642 ListSeparator LS;
4643 for (const auto &[V, Block] :
4644 zip_equal(PN->incoming_values(), PN->blocks())) {
4645 Out << LS << "[ ";
4646 writeOperand(V, false);
4647 Out << ", ";
4648 writeOperand(Block, false);
4649 Out << " ]";
4650 }
4651 } else if (const auto *EVI = dyn_cast<ExtractValueInst>(&I)) {
4652 Out << ' ';
4653 writeOperand(I.getOperand(0), true);
4654 Out << ", ";
4655 Out << llvm::interleaved(EVI->indices());
4656 } else if (const auto *IVI = dyn_cast<InsertValueInst>(&I)) {
4657 Out << ' ';
4658 writeOperand(I.getOperand(0), true); Out << ", ";
4659 writeOperand(I.getOperand(1), true);
4660 Out << ", ";
4661 Out << llvm::interleaved(IVI->indices());
4662 } else if (const auto *LPI = dyn_cast<LandingPadInst>(&I)) {
4663 Out << ' ';
4664 TypePrinter.print(I.getType(), Out);
4665 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
4666 Out << '\n';
4667
4668 if (LPI->isCleanup())
4669 Out << " cleanup";
4670
4671 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
4672 if (i != 0 || LPI->isCleanup()) Out << "\n";
4673 if (LPI->isCatch(i))
4674 Out << " catch ";
4675 else
4676 Out << " filter ";
4677
4678 writeOperand(LPI->getClause(i), true);
4679 }
4680 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
4681 Out << " within ";
4682 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
4683 Out << " [";
4684 ListSeparator LS;
4685 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
4686 Out << LS;
4687 writeOperand(PadBB, /*PrintType=*/true);
4688 }
4689 Out << "] unwind ";
4690 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
4691 writeOperand(UnwindDest, /*PrintType=*/true);
4692 else
4693 Out << "to caller";
4694 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
4695 Out << " within ";
4696 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
4697 Out << " [";
4698 ListSeparator LS;
4699 for (const Value *Op : FPI->arg_operands()) {
4700 Out << LS;
4701 writeOperand(Op, /*PrintType=*/true);
4702 }
4703 Out << ']';
4704 } else if (isa<ReturnInst>(I) && !Operand) {
4705 Out << " void";
4706 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
4707 Out << " from ";
4708 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
4709
4710 Out << " to ";
4711 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
4712 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
4713 Out << " from ";
4714 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
4715
4716 Out << " unwind ";
4717 if (CRI->hasUnwindDest())
4718 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
4719 else
4720 Out << "to caller";
4721 } else if (const auto *CI = dyn_cast<CallInst>(&I)) {
4722 // Print the calling convention being used.
4723 if (CI->getCallingConv() != CallingConv::C) {
4724 Out << " ";
4725 printCallingConv(CI->getCallingConv(), Out);
4726 }
4727
4728 Operand = CI->getCalledOperand();
4729 FunctionType *FTy = CI->getFunctionType();
4730 Type *RetTy = FTy->getReturnType();
4731 const AttributeList &PAL = CI->getAttributes();
4732
4733 if (PAL.hasRetAttrs())
4734 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4735
4736 // Only print addrspace(N) if necessary:
4737 maybePrintCallAddrSpace(Operand, &I, Out);
4738
4739 // If possible, print out the short form of the call instruction. We can
4740 // only do this if the first argument is a pointer to a nonvararg function,
4741 // and if the return type is not a pointer to a function.
4742 Out << ' ';
4743 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4744 Out << ' ';
4745 writeOperand(Operand, false);
4746 Out << '(';
4747 bool HasPrettyPrintedArgs =
4748 isa<IntrinsicInst>(CI) &&
4749 Intrinsic::hasPrettyPrintedArgs(CI->getIntrinsicID());
4750
4751 ListSeparator LS;
4752 Function *CalledFunc = CI->getCalledFunction();
4753 auto PrintArgComment = [&](unsigned ArgNo) {
4754 const auto *ConstArg = dyn_cast<Constant>(CI->getArgOperand(ArgNo));
4755 if (!ConstArg || !CalledFunc)
4756 return;
4757 std::string ArgComment;
4758 raw_string_ostream ArgCommentStream(ArgComment);
4759 Intrinsic::ID IID = CalledFunc->getIntrinsicID();
4760 Intrinsic::printImmArg(IID, ArgNo, ArgCommentStream, ConstArg);
4761 if (ArgComment.empty())
4762 return;
4763 Out << "/* " << ArgComment << " */ ";
4764 };
4765 if (HasPrettyPrintedArgs) {
4766 for (unsigned ArgNo = 0, NumArgs = CI->arg_size(); ArgNo < NumArgs;
4767 ++ArgNo) {
4768 Out << LS;
4769 PrintArgComment(ArgNo);
4770 writeParamOperand(CI->getArgOperand(ArgNo), PAL.getParamAttrs(ArgNo));
4771 }
4772 } else {
4773 for (unsigned ArgNo = 0, NumArgs = CI->arg_size(); ArgNo < NumArgs;
4774 ++ArgNo) {
4775 Out << LS;
4776 writeParamOperand(CI->getArgOperand(ArgNo), PAL.getParamAttrs(ArgNo));
4777 }
4778 }
4779 // Emit an ellipsis if this is a musttail call in a vararg function. This
4780 // is only to aid readability, musttail calls forward varargs by default.
4781 if (CI->isMustTailCall() && CI->getParent() &&
4782 CI->getParent()->getParent() &&
4783 CI->getParent()->getParent()->isVarArg()) {
4784 if (CI->arg_size() > 0)
4785 Out << ", ";
4786 Out << "...";
4787 }
4788
4789 Out << ')';
4790 if (PAL.hasFnAttrs())
4791 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4792
4793 writeOperandBundles(CI);
4794 } else if (const auto *II = dyn_cast<InvokeInst>(&I)) {
4795 Operand = II->getCalledOperand();
4796 FunctionType *FTy = II->getFunctionType();
4797 Type *RetTy = FTy->getReturnType();
4798 const AttributeList &PAL = II->getAttributes();
4799
4800 // Print the calling convention being used.
4801 if (II->getCallingConv() != CallingConv::C) {
4802 Out << " ";
4803 printCallingConv(II->getCallingConv(), Out);
4804 }
4805
4806 if (PAL.hasRetAttrs())
4807 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4808
4809 // Only print addrspace(N) if necessary:
4810 maybePrintCallAddrSpace(Operand, &I, Out);
4811
4812 // If possible, print out the short form of the invoke instruction. We can
4813 // only do this if the first argument is a pointer to a nonvararg function,
4814 // and if the return type is not a pointer to a function.
4815 //
4816 Out << ' ';
4817 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4818 Out << ' ';
4819 writeOperand(Operand, false);
4820 Out << '(';
4821 ListSeparator LS;
4822 for (unsigned op = 0, Eop = II->arg_size(); op < Eop; ++op) {
4823 Out << LS;
4824 writeParamOperand(II->getArgOperand(op), PAL.getParamAttrs(op));
4825 }
4826
4827 Out << ')';
4828 if (PAL.hasFnAttrs())
4829 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4830
4831 writeOperandBundles(II);
4832
4833 Out << "\n to ";
4834 writeOperand(II->getNormalDest(), true);
4835 Out << " unwind ";
4836 writeOperand(II->getUnwindDest(), true);
4837 } else if (const auto *CBI = dyn_cast<CallBrInst>(&I)) {
4838 Operand = CBI->getCalledOperand();
4839 FunctionType *FTy = CBI->getFunctionType();
4840 Type *RetTy = FTy->getReturnType();
4841 const AttributeList &PAL = CBI->getAttributes();
4842
4843 // Print the calling convention being used.
4844 if (CBI->getCallingConv() != CallingConv::C) {
4845 Out << " ";
4846 printCallingConv(CBI->getCallingConv(), Out);
4847 }
4848
4849 if (PAL.hasRetAttrs())
4850 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4851
4852 // If possible, print out the short form of the callbr instruction. We can
4853 // only do this if the first argument is a pointer to a nonvararg function,
4854 // and if the return type is not a pointer to a function.
4855 //
4856 Out << ' ';
4857 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4858 Out << ' ';
4859 writeOperand(Operand, false);
4860 Out << '(';
4861 ListSeparator ArgLS;
4862 for (unsigned op = 0, Eop = CBI->arg_size(); op < Eop; ++op) {
4863 Out << ArgLS;
4864 writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttrs(op));
4865 }
4866
4867 Out << ')';
4868 if (PAL.hasFnAttrs())
4869 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4870
4871 writeOperandBundles(CBI);
4872
4873 Out << "\n to ";
4874 writeOperand(CBI->getDefaultDest(), true);
4875 Out << " [";
4876 ListSeparator DestLS;
4877 for (const BasicBlock *Dest : CBI->getIndirectDests()) {
4878 Out << DestLS;
4879 writeOperand(Dest, true);
4880 }
4881 Out << ']';
4882 } else if (const auto *AI = dyn_cast<AllocaInst>(&I)) {
4883 Out << ' ';
4884 if (AI->isUsedWithInAlloca())
4885 Out << "inalloca ";
4886 if (AI->isSwiftError())
4887 Out << "swifterror ";
4888 TypePrinter.print(AI->getAllocatedType(), Out);
4889
4890 // Explicitly write the array size if the code is broken, if it's an array
4891 // allocation, or if the type is not canonical for scalar allocations. The
4892 // latter case prevents the type from mutating when round-tripping through
4893 // assembly.
4894 if (!AI->getArraySize() || AI->isArrayAllocation() ||
4895 !AI->getArraySize()->getType()->isIntegerTy(32)) {
4896 Out << ", ";
4897 writeOperand(AI->getArraySize(), true);
4898 }
4899 if (MaybeAlign A = AI->getAlign()) {
4900 Out << ", align " << A->value();
4901 }
4902
4903 printAddressSpace(AI->getModule(), AI->getAddressSpace(), Out,
4904 /*Prefix=*/", ");
4905 } else if (isa<CastInst>(I)) {
4906 if (Operand) {
4907 Out << ' ';
4908 writeOperand(Operand, true); // Work with broken code
4909 }
4910 Out << " to ";
4911 TypePrinter.print(I.getType(), Out);
4912 } else if (isa<VAArgInst>(I)) {
4913 if (Operand) {
4914 Out << ' ';
4915 writeOperand(Operand, true); // Work with broken code
4916 }
4917 Out << ", ";
4918 TypePrinter.print(I.getType(), Out);
4919 } else if (Operand) { // Print the normal way.
4920 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
4921 Out << ' ';
4922 TypePrinter.print(GEP->getSourceElementType(), Out);
4923 Out << ',';
4924 } else if (isa<BitExtractInst>(I) || isa<LoadInst>(I)) {
4925 Out << ' ';
4926 TypePrinter.print(I.getType(), Out);
4927 Out << ',';
4928 }
4929
4930 // PrintAllTypes - Instructions who have operands of all the same type
4931 // omit the type from all but the first operand. If the instruction has
4932 // different type operands (for example br), then they are all printed.
4933 bool PrintAllTypes = false;
4934 Type *TheType = Operand->getType();
4935
4936 // Select, Store, ShuffleVector, CmpXchg and AtomicRMW always print all
4937 // types.
4941 PrintAllTypes = true;
4942 } else {
4943 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
4944 Operand = I.getOperand(i);
4945 // note that Operand shouldn't be null, but the test helps make dump()
4946 // more tolerant of malformed IR
4947 if (Operand && Operand->getType() != TheType) {
4948 PrintAllTypes = true; // We have differing types! Print them all!
4949 break;
4950 }
4951 }
4952 }
4953
4954 if (!PrintAllTypes) {
4955 Out << ' ';
4956 TypePrinter.print(TheType, Out);
4957 }
4958
4959 Out << ' ';
4960 ListSeparator LS;
4961 for (const Value *Op : I.operands()) {
4962 Out << LS;
4963 writeOperand(Op, PrintAllTypes);
4964 }
4965 }
4966
4967 // Print atomic ordering/alignment for memory operations
4968 if (const auto *LI = dyn_cast<LoadInst>(&I)) {
4969 if (LI->isAtomic())
4970 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
4971 if (MaybeAlign A = LI->getAlign())
4972 Out << ", align " << A->value();
4973 } else if (const auto *SI = dyn_cast<StoreInst>(&I)) {
4974 if (SI->isAtomic())
4975 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
4976 if (MaybeAlign A = SI->getAlign())
4977 Out << ", align " << A->value();
4978 } else if (const auto *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
4979 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
4980 CXI->getFailureOrdering(), CXI->getSyncScopeID());
4981 Out << ", align " << CXI->getAlign().value();
4982 } else if (const auto *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
4983 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
4984 RMWI->getSyncScopeID());
4985 Out << ", align " << RMWI->getAlign().value();
4986 } else if (const auto *FI = dyn_cast<FenceInst>(&I)) {
4987 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
4988 } else if (const auto *SVI = dyn_cast<ShuffleVectorInst>(&I)) {
4989 printShuffleMask(Out, SVI->getType(), SVI->getShuffleMask());
4990 }
4991
4992 // Print Metadata info.
4994 I.getAllMetadata(InstMD);
4995 printMetadataAttachments(InstMD, ", ");
4996
4997 // Print a nice comment.
4998 printInfoComment(I);
4999}
5000
5001void AssemblyWriter::printDbgMarker(const DbgMarker &Marker) {
5002 // There's no formal representation of a DbgMarker -- print purely as a
5003 // debugging aid.
5004 for (const DbgRecord &DPR : Marker.StoredDbgRecords) {
5005 printDbgRecord(DPR);
5006 Out << "\n";
5007 }
5008
5009 Out << " DbgMarker -> { ";
5010 printInstruction(*Marker.MarkedInstr);
5011 Out << " }";
5012}
5013
5014void AssemblyWriter::printDbgRecord(const DbgRecord &DR) {
5015 if (auto *DVR = dyn_cast<DbgVariableRecord>(&DR))
5016 printDbgVariableRecord(*DVR);
5017 else if (auto *DLR = dyn_cast<DbgLabelRecord>(&DR))
5018 printDbgLabelRecord(*DLR);
5019 else
5020 llvm_unreachable("Unexpected DbgRecord kind");
5021}
5022
5023void AssemblyWriter::printDbgVariableRecord(const DbgVariableRecord &DVR) {
5024 auto WriterCtx = getContext();
5025 Out << "#dbg_";
5026 switch (DVR.getType()) {
5027 case DbgVariableRecord::LocationType::Value:
5028 Out << "value";
5029 break;
5030 case DbgVariableRecord::LocationType::Declare:
5031 Out << "declare";
5032 break;
5033 case DbgVariableRecord::LocationType::DeclareValue:
5034 Out << "declare_value";
5035 break;
5036 case DbgVariableRecord::LocationType::Assign:
5037 Out << "assign";
5038 break;
5039 default:
5041 "Tried to print a DbgVariableRecord with an invalid LocationType!");
5042 }
5043
5044 auto PrintOrNull = [&](Metadata *M) {
5045 if (!M)
5046 Out << "(null)";
5047 else
5048 writeAsOperandInternal(Out, M, WriterCtx, true);
5049 };
5050
5051 Out << "(";
5052 PrintOrNull(DVR.getRawLocation());
5053 Out << ", ";
5054 PrintOrNull(DVR.getRawVariable());
5055 Out << ", ";
5056 PrintOrNull(DVR.getRawExpression());
5057 Out << ", ";
5058 if (DVR.isDbgAssign()) {
5059 PrintOrNull(DVR.getRawAssignID());
5060 Out << ", ";
5061 PrintOrNull(DVR.getRawAddress());
5062 Out << ", ";
5063 PrintOrNull(DVR.getRawAddressExpression());
5064 Out << ", ";
5065 }
5066 PrintOrNull(DVR.getDebugLoc().getAsMDNode());
5067 Out << ")";
5068}
5069
5070/// printDbgRecordLine - Print a DbgRecord with indentation and a newline
5071/// character.
5072void AssemblyWriter::printDbgRecordLine(const DbgRecord &DR) {
5073 // Print lengthier indentation to bring out-of-line with instructions.
5074 Out << " ";
5075 printDbgRecord(DR);
5076 Out << '\n';
5077}
5078
5079void AssemblyWriter::printDbgLabelRecord(const DbgLabelRecord &Label) {
5080 auto WriterCtx = getContext();
5081 Out << "#dbg_label(";
5082 writeAsOperandInternal(Out, Label.getRawLabel(), WriterCtx, true);
5083 Out << ", ";
5084 writeAsOperandInternal(Out, Label.getDebugLoc(), WriterCtx, true);
5085 Out << ")";
5086}
5087
5088void AssemblyWriter::printMetadataAttachments(
5089 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
5090 StringRef Separator) {
5091 if (MDs.empty())
5092 return;
5093
5094 if (MDNames.empty())
5095 MDs[0].second->getContext().getMDKindNames(MDNames);
5096
5097 auto WriterCtx = getContext();
5098 for (const auto &I : MDs) {
5099 unsigned Kind = I.first;
5100 Out << Separator;
5101 if (Kind < MDNames.size()) {
5102 Out << "!";
5103 printMetadataIdentifier(MDNames[Kind], Out);
5104 } else
5105 Out << "!<unknown kind #" << Kind << ">";
5106 Out << ' ';
5107 writeAsOperandInternal(Out, I.second, WriterCtx);
5108 }
5109}
5110
5111void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
5112 if (AnnotationWriter)
5113 AnnotationWriter->emitMDNodeAnnot(Node, Out);
5114
5115 Out << '!' << Slot << " = ";
5116 printMDNodeBody(Node);
5117 Out << "\n";
5118}
5119
5120void AssemblyWriter::writeAllMDNodes() {
5122 Nodes.reserve(Machine.mdn_size());
5123 for (auto &I : llvm::make_range(Machine.mdn_begin(), Machine.mdn_end()))
5124 Nodes.emplace_back(I.second, cast<MDNode>(I.first));
5125 llvm::sort(Nodes);
5126
5127 for (auto [Slot, Node] : Nodes)
5128 writeMDNode(Slot, Node);
5129}
5130
5131void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
5132 auto WriterCtx = getContext();
5133 writeMDNodeBodyInternal(Out, Node, WriterCtx);
5134}
5135
5136void AssemblyWriter::writeAttribute(const Attribute &Attr, bool InAttrGroup) {
5137 if (!Attr.isTypeAttribute()) {
5138 Out << Attr.getAsString(InAttrGroup);
5139 return;
5140 }
5141
5142 Out << Attribute::getNameFromAttrKind(Attr.getKindAsEnum());
5143 if (Type *Ty = Attr.getValueAsType()) {
5144 Out << '(';
5145 TypePrinter.print(Ty, Out);
5146 Out << ')';
5147 }
5148}
5149
5150void AssemblyWriter::writeAttributeSet(const AttributeSet &AttrSet,
5151 bool InAttrGroup) {
5152 ListSeparator LS(" ");
5153 for (const auto &Attr : AttrSet) {
5154 Out << LS;
5155 writeAttribute(Attr, InAttrGroup);
5156 }
5157}
5158
5159void AssemblyWriter::writeAllAttributeGroups() {
5160 std::vector<std::pair<AttributeSet, unsigned>> asVec;
5161 asVec.resize(Machine.as_size());
5162
5163 for (auto &I : llvm::make_range(Machine.as_begin(), Machine.as_end()))
5164 asVec[I.second] = I;
5165
5166 for (const auto &I : asVec)
5167 Out << "attributes #" << I.second << " = { "
5168 << I.first.getAsString(true) << " }\n";
5169}
5170
5171void AssemblyWriter::printUseListOrder(const Value *V,
5172 ArrayRef<unsigned> Shuffle) {
5173 if (Machine.getFunction())
5174 Out << " ";
5175
5176 Out << "uselistorder ";
5177 writeOperand(V, true);
5178
5179 assert(Shuffle.size() >= 2 && "Shuffle too small");
5180 Out << ", { " << llvm::interleaved(Shuffle) << " }\n";
5181}
5182
5183void AssemblyWriter::printUseLists(const Function *F) {
5184 auto It = UseListOrders.find(F);
5185 if (It == UseListOrders.end())
5186 return;
5187
5188 Out << "\n; uselistorder directives\n";
5189 for (const auto &Pair : It->second)
5190 printUseListOrder(Pair.first, Pair.second);
5191}
5192
5193//===----------------------------------------------------------------------===//
5194// External Interface declarations
5195//===----------------------------------------------------------------------===//
5196
5198 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
5199 SlotTracker SlotTable(this);
5200 formatted_raw_ostream OS(ROS);
5201 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable, this->getParent(),
5202 AAW, IsForDebug, ShouldPreserveUseListOrder);
5203 W.printFunction(this);
5204}
5205
5207 bool ShouldPreserveUseListOrder,
5208 bool IsForDebug) const {
5209 SlotTracker SlotTable(this->getParent());
5210 formatted_raw_ostream OS(ROS);
5211 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable, this->getModule(),
5212 AAW, IsForDebug, ShouldPreserveUseListOrder);
5213 W.printBasicBlock(this);
5214}
5215
5217 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
5218 SlotTracker SlotTable(this, /*ShouldTrackMetadataDefinitions=*/true);
5219 formatted_raw_ostream OS(ROS);
5220 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable, this, AAW,
5221 IsForDebug, ShouldPreserveUseListOrder);
5222 W.printModule(this);
5223}
5224
5225void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
5226 SlotTracker SlotTable(getParent());
5227 formatted_raw_ostream OS(ROS);
5228 AssemblyWriter W(getOptions(getParent()), OS, SlotTable, getParent(), nullptr,
5229 IsForDebug);
5230 W.printNamedMDNode(this);
5231}
5232
5234 bool IsForDebug) const {
5235 std::optional<SlotTracker> LocalST;
5236 SlotTracker *SlotTable;
5237 if (auto *ST = MST.getMachine())
5238 SlotTable = ST;
5239 else {
5240 LocalST.emplace(getParent());
5241 SlotTable = &*LocalST;
5242 }
5243
5244 formatted_raw_ostream OS(ROS);
5245 AssemblyWriter W(getOptions(getParent()), OS, *SlotTable, getParent(),
5246 nullptr, IsForDebug);
5247 W.printNamedMDNode(this);
5248}
5249
5250void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
5252 ROS << " = comdat ";
5253
5254 switch (getSelectionKind()) {
5255 case Comdat::Any:
5256 ROS << "any";
5257 break;
5258 case Comdat::ExactMatch:
5259 ROS << "exactmatch";
5260 break;
5261 case Comdat::Largest:
5262 ROS << "largest";
5263 break;
5265 ROS << "nodeduplicate";
5266 break;
5267 case Comdat::SameSize:
5268 ROS << "samesize";
5269 break;
5270 }
5271
5272 ROS << '\n';
5273}
5274
5275void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
5276 TypePrinting TP;
5277 TP.print(const_cast<Type*>(this), OS);
5278
5279 if (NoDetails)
5280 return;
5281
5282 // If the type is a named struct type, print the body as well.
5283 if (auto *STy = dyn_cast<StructType>(const_cast<Type *>(this)))
5284 if (!STy->isLiteral()) {
5285 OS << " = type ";
5286 TP.printStructBody(STy, OS);
5287 }
5288}
5289
5290void DbgMarker::print(raw_ostream &ROS, bool IsForDebug) const {
5291
5293 print(ROS, MST, IsForDebug);
5294}
5295
5296void DbgVariableRecord::print(raw_ostream &ROS, bool IsForDebug) const {
5297
5299 print(ROS, MST, IsForDebug);
5300}
5301
5303 bool IsForDebug) const {
5304 formatted_raw_ostream OS(ROS);
5305 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
5306 SlotTracker &SlotTable =
5307 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
5308 const Function *F = getParent() ? getParent()->getParent() : nullptr;
5309 if (F)
5310 MST.incorporateFunction(*F);
5311 const Module *M = getModuleFromDPI(this);
5312 AssemblyWriter W(getOptions(M), OS, SlotTable, M, nullptr, IsForDebug);
5313 W.printDbgMarker(*this);
5314}
5315
5316void DbgLabelRecord::print(raw_ostream &ROS, bool IsForDebug) const {
5317
5319 print(ROS, MST, IsForDebug);
5320}
5321
5323 bool IsForDebug) const {
5324 formatted_raw_ostream OS(ROS);
5325 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
5326 SlotTracker &SlotTable =
5327 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
5328 const Function *F = Marker && Marker->getParent()
5329 ? Marker->getParent()->getParent()
5330 : nullptr;
5331 if (F)
5332 MST.incorporateFunction(*F);
5333 const Module *M = getModuleFromDPI(this);
5334 AssemblyWriter W(getOptions(M), OS, SlotTable, M, nullptr, IsForDebug);
5335 W.printDbgVariableRecord(*this);
5336}
5337
5339 bool IsForDebug) const {
5340 formatted_raw_ostream OS(ROS);
5341 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
5342 SlotTracker &SlotTable =
5343 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
5344 const Function *F =
5345 Marker->getParent() ? Marker->getParent()->getParent() : nullptr;
5346 if (F)
5347 MST.incorporateFunction(*F);
5348
5349 const Module *M = getModuleFromDPI(this);
5350 AssemblyWriter W(getOptions(M), OS, SlotTable, M, nullptr, IsForDebug);
5351 W.printDbgLabelRecord(*this);
5352}
5353
5354void Value::print(raw_ostream &ROS, bool IsForDebug) const {
5355 if (const auto *F = dyn_cast<Function>(this)) {
5356 F->print(ROS, nullptr, /*ShouldPreserveUseListOrder=*/false, IsForDebug);
5357 return;
5358 }
5359 if (const auto *BB = dyn_cast<BasicBlock>(this)) {
5360 BB->print(ROS, nullptr, /*ShouldPreserveUseListOrder=*/false, IsForDebug);
5361 return;
5362 }
5363
5365 print(ROS, MST, IsForDebug);
5366}
5367
5369 bool IsForDebug) const {
5370 formatted_raw_ostream OS(ROS);
5371 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
5372 SlotTracker &SlotTable =
5373 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
5374 auto IncorporateFunction = [&](const Function *F) {
5375 if (F)
5376 MST.incorporateFunction(*F);
5377 };
5378
5379 if (const auto *I = dyn_cast<Instruction>(this)) {
5380 IncorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
5381 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable,
5382 getModuleFromVal(I), nullptr, IsForDebug);
5383 W.printInstruction(*I);
5384 } else if (const auto *BB = dyn_cast<BasicBlock>(this)) {
5385 IncorporateFunction(BB->getParent());
5386 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable,
5387 getModuleFromVal(BB), nullptr, IsForDebug);
5388 W.printBasicBlock(BB);
5389 } else if (const auto *GV = dyn_cast<GlobalValue>(this)) {
5390 AssemblyWriter W(getIROptions(getContext()), OS, SlotTable, GV->getParent(),
5391 nullptr, IsForDebug);
5392 if (const auto *V = dyn_cast<GlobalVariable>(GV))
5393 W.printGlobal(V);
5394 else if (const auto *F = dyn_cast<Function>(GV))
5395 W.printFunction(F);
5396 else if (const auto *A = dyn_cast<GlobalAlias>(GV))
5397 W.printAlias(A);
5398 else if (const auto *I = dyn_cast<GlobalIFunc>(GV))
5399 W.printIFunc(I);
5400 else
5401 llvm_unreachable("Unknown GlobalValue to print out!");
5402 } else if (const auto *V = dyn_cast<MetadataAsValue>(this)) {
5403 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
5404 } else if (const auto *C = dyn_cast<Constant>(this)) {
5405 TypePrinting TypePrinter;
5406 TypePrinter.print(C->getType(), OS);
5407 OS << ' ';
5408 AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine());
5409 writeConstantInternal(OS, C, WriterCtx);
5410 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
5411 this->printAsOperand(OS, /* PrintType */ true, MST);
5412 } else {
5413 llvm_unreachable("Unknown value to print out!");
5414 }
5415}
5416
5417/// Print without a type, skipping the TypePrinting object.
5418///
5419/// \return \c true iff printing was successful.
5420static bool printWithoutType(const Value &V, raw_ostream &O,
5421 SlotTracker *Machine, const Module *M) {
5422 if (V.hasName() || isa<GlobalValue>(V) ||
5424 AsmWriterContext WriterCtx(nullptr, Machine, M);
5425 writeAsOperandInternal(O, &V, WriterCtx);
5426 return true;
5427 }
5428 return false;
5429}
5430
5431static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
5432 ModuleSlotTracker &MST) {
5433 TypePrinting TypePrinter(MST.getModule());
5434 AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine(), MST.getModule());
5435 writeAsOperandInternal(O, &V, WriterCtx, PrintType);
5436}
5437
5438void Value::printAsOperand(raw_ostream &O, bool PrintType,
5439 const Module *M) const {
5440 if (!M)
5441 M = getModuleFromVal(this);
5442
5443 if (!PrintType)
5444 if (printWithoutType(*this, O, nullptr, M))
5445 return;
5446
5448 ModuleSlotTracker MST(Machine, M);
5449 printAsOperandImpl(*this, O, PrintType, MST);
5450}
5451
5452void Value::printAsOperand(raw_ostream &O, bool PrintType,
5453 ModuleSlotTracker &MST) const {
5454 if (!PrintType)
5455 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
5456 return;
5457
5458 printAsOperandImpl(*this, O, PrintType, MST);
5459}
5460
5461/// Recursive version of printMetadataImpl.
5462static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD,
5463 AsmWriterContext &WriterCtx) {
5464 formatted_raw_ostream OS(ROS);
5465 writeAsOperandInternal(OS, &MD, WriterCtx, /* FromValue */ true);
5466
5467 auto *N = dyn_cast<MDNode>(&MD);
5468 if (!N || isa<DIExpression>(MD))
5469 return;
5470
5471 OS << " = ";
5472 writeMDNodeBodyInternal(OS, N, WriterCtx);
5473}
5474
5475namespace {
5476struct MDTreeAsmWriterContext : public AsmWriterContext {
5477 unsigned Level;
5478 // {Level, Printed string}
5479 using EntryTy = std::pair<unsigned, std::string>;
5481
5482 // Used to break the cycle in case there is any.
5483 SmallPtrSet<const Metadata *, 4> Visited;
5484
5485 raw_ostream &MainOS;
5486
5487 MDTreeAsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M,
5488 const ModuleSlotTracker *MST, raw_ostream &OS,
5489 const Metadata *InitMD)
5490 : AsmWriterContext(TP, ST, M, MST), Level(0U), Visited({InitMD}),
5491 MainOS(OS) {}
5492
5493 void onWriteMetadataAsOperand(const Metadata *MD) override {
5494 if (!Visited.insert(MD).second)
5495 return;
5496
5497 std::string Str;
5498 raw_string_ostream SS(Str);
5499 ++Level;
5500 // A placeholder entry to memorize the correct
5501 // position in buffer.
5502 Buffer.emplace_back(std::make_pair(Level, ""));
5503 unsigned InsertIdx = Buffer.size() - 1;
5504
5505 printMetadataImplRec(SS, *MD, *this);
5506 Buffer[InsertIdx].second = std::move(SS.str());
5507 --Level;
5508 }
5509
5510 ~MDTreeAsmWriterContext() override {
5511 for (const auto &Entry : Buffer) {
5512 MainOS << "\n";
5513 unsigned NumIndent = Entry.first * 2U;
5514 MainOS.indent(NumIndent) << Entry.second;
5515 }
5516 }
5517};
5518} // end anonymous namespace
5519
5520static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
5521 ModuleSlotTracker &MST, const Module *M,
5522 bool OnlyAsOperand, bool PrintAsTree = false) {
5523 formatted_raw_ostream OS(ROS);
5524
5525 TypePrinting TypePrinter(M);
5526
5527 std::unique_ptr<AsmWriterContext> WriterCtx;
5528 if (PrintAsTree && !OnlyAsOperand)
5529 WriterCtx = std::make_unique<MDTreeAsmWriterContext>(
5530 &TypePrinter, MST.getMachine(), M, &MST, OS, &MD);
5531 else
5532 WriterCtx = std::make_unique<AsmWriterContext>(&TypePrinter,
5533 MST.getMachine(), M, &MST);
5534
5535 writeAsOperandInternal(OS, &MD, *WriterCtx, /* FromValue */ true);
5536
5537 auto *N = dyn_cast<MDNode>(&MD);
5538 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
5539 return;
5540
5541 OS << " = ";
5542 writeMDNodeBodyInternal(OS, N, *WriterCtx);
5543}
5544
5546 ModuleSlotTracker MST(M);
5547 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
5548}
5549
5551 const Module *M) const {
5552 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
5553}
5554
5556 bool /*IsForDebug*/) const {
5557 ModuleSlotTracker MST(M);
5558 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
5559}
5560
5562 const Module *M, bool /*IsForDebug*/) const {
5563 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
5564}
5565
5566void MDNode::printTree(raw_ostream &OS, const Module *M) const {
5567 ModuleSlotTracker MST(M);
5568 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
5569 /*PrintAsTree=*/true);
5570}
5571
5573 const Module *M) const {
5574 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
5575 /*PrintAsTree=*/true);
5576}
5577
5578void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
5579 SlotTracker SlotTable(this);
5580 formatted_raw_ostream OS(ROS);
5581 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
5582 W.printModuleSummaryIndex();
5583}
5584
5586 SlotTracker *ST = MachineStorage.get();
5587 if (!ST)
5588 return;
5589
5590 for (auto &I : llvm::make_range(ST->mdn_begin(), ST->mdn_end()))
5591 L.push_back(std::make_pair(I.second, I.first));
5592}
5593
5594#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
5595// Value::dump - allow easy printing of Values from the debugger.
5597void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5598
5599// Value::dump - allow easy printing of Values from the debugger.
5601void DbgMarker::dump() const {
5602 print(dbgs(), /*IsForDebug=*/true);
5603 dbgs() << '\n';
5604}
5605
5606// Value::dump - allow easy printing of Values from the debugger.
5608void DbgRecord::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5609
5610// Type::dump - allow easy printing of Types from the debugger.
5612void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5613
5614// Module::dump() - Allow printing of Modules from the debugger.
5616void Module::dump() const {
5617 print(dbgs(), nullptr,
5618 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
5619}
5620
5621// Allow printing of Comdats from the debugger.
5623void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
5624
5625// NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
5627void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
5628
5630void Metadata::dump() const { dump(nullptr); }
5631
5633void Metadata::dump(const Module *M) const {
5634 print(dbgs(), M, /*IsForDebug=*/true);
5635 dbgs() << '\n';
5636}
5637
5639void MDNode::dumpTree() const { dumpTree(nullptr); }
5640
5642void MDNode::dumpTree(const Module *M) const {
5643 printTree(dbgs(), M);
5644 dbgs() << '\n';
5645}
5646
5647// Allow printing of ModuleSummaryIndex from the debugger.
5649void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
5650#endif
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
amdgpu next use AMDGPU Next Use Analysis Printer
This file declares a class to represent arbitrary precision floating point values and provide a varie...
This file implements a class to represent arbitrary precision integral constant values and operations...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
static void writeDIMacro(raw_ostream &Out, const DIMacro *N, AsmWriterContext &WriterCtx)
static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD, AsmWriterContext &WriterCtx)
static void writeDIGlobalVariableExpression(raw_ostream &Out, const DIGlobalVariableExpression *N, AsmWriterContext &WriterCtx)
static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, AsmWriterContext &WriterCtx)
static void writeDIFixedPointType(raw_ostream &Out, const DIFixedPointType *N, AsmWriterContext &WriterCtx)
static void printDSOLocation(const GlobalValue &GV, formatted_raw_ostream &Out)
static const char * getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K)
static void writeDISubrangeType(raw_ostream &Out, const DISubrangeType *N, AsmWriterContext &WriterCtx)
static void WriteFullHexAPInt(raw_ostream &Out, const APInt &Val)
static void writeAPFloatInternal(raw_ostream &Out, const APFloat &APF)
static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD, ModuleSlotTracker &MST, const Module *M, bool OnlyAsOperand, bool PrintAsTree=false)
static void writeDIStringType(raw_ostream &Out, const DIStringType *N, AsmWriterContext &WriterCtx)
static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT)
static std::vector< unsigned > predictValueUseListOrder(const Value *V, unsigned ID, const OrderMap &OM)
static void writeDILayerLocList(raw_ostream &Out, const DILayerLocList *N, AsmWriterContext &WriterCtx)
static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, AsmWriterContext &WriterCtx)
static void orderValue(const Value *V, OrderMap &OM)
static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, AsmWriterContext &WriterCtx)
static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA)
static const char * getWholeProgDevirtResByArgKindName(WholeProgramDevirtResolution::ByArg::Kind K)
static void writeDILayerLoc(raw_ostream &Out, const DILayerLoc *N, AsmWriterContext &WriterCtx)
static void writeMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, AsmWriterContext &Ctx)
static void writeDIModule(raw_ostream &Out, const DIModule *N, AsmWriterContext &WriterCtx)
static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &)
static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, AsmWriterContext &WriterCtx)
static void writeOptimizationInfo(raw_ostream &Out, const User *U)
#define CC_VLS_CASE(ABI_VLEN)
static void writeDILabel(raw_ostream &Out, const DILabel *N, AsmWriterContext &WriterCtx)
static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, AsmWriterContext &WriterCtx)
static void printMetadataIdentifier(StringRef Name, formatted_raw_ostream &Out)
static void printShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef< int > Mask)
static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, AsmWriterContext &WriterCtx)
static const Module * getModuleFromDPI(const DbgMarker *Marker)
static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType, ModuleSlotTracker &MST)
static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, AsmWriterContext &WriterCtx)
static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, AsmWriterContext &WriterCtx)
static const char * getSummaryKindName(GlobalValueSummary::SummaryKind SK)
static OrderMap orderModule(const Module *M)
static const char * getVisibilityName(GlobalValue::VisibilityTypes Vis)
static void printCallingConv(unsigned cc, raw_ostream &Out)
static void printAddressSpace(const Module *M, unsigned AS, raw_ostream &OS, StringRef Prefix=" ", StringRef Suffix="", bool ForcePrint=false)
static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD, AsmWriterContext &WriterCtx)
Recursive version of printMetadataImpl.
static SlotTracker * createSlotTracker(const Value *V)
static void writeDILocation(raw_ostream &Out, const DILocation *DL, AsmWriterContext &WriterCtx)
static void writeDINamespace(raw_ostream &Out, const DINamespace *N, AsmWriterContext &WriterCtx)
DenseMap< const Function *, MapVector< const Value *, std::vector< unsigned > > > UseListOrderMap
static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, AsmWriterContext &WriterCtx)
static UseListOrderMap predictUseListOrder(const Module *M)
static void printThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, formatted_raw_ostream &Out)
static std::string getLinkageName(GlobalValue::LinkageTypes LT)
static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, AsmWriterContext &WriterCtx)
static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, AsmWriterContext &WriterCtx)
static const char * getTTResKindName(TypeTestResolution::Kind K)
static void writeDITemplateTypeParameter(raw_ostream &Out, const DITemplateTypeParameter *N, AsmWriterContext &WriterCtx)
static const char * getImportTypeName(GlobalValueSummary::ImportKind IK)
static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, AsmWriterContext &WriterCtx)
static const IROptions & getOptions(const Module *M)
static const Module * getModuleFromVal(const Value *V)
static void printLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix)
Turn the specified name into an 'LLVM name', which is either prefixed with % (if the string only cont...
static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I, raw_ostream &Out)
static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, AsmWriterContext &WriterCtx)
static void writeDISubrange(raw_ostream &Out, const DISubrange *N, AsmWriterContext &WriterCtx)
static void writeDIProperty(raw_ostream &Out, const DIProperty *N, AsmWriterContext &WriterCtx)
static void writeDILexicalBlockFile(raw_ostream &Out, const DILexicalBlockFile *N, AsmWriterContext &WriterCtx)
static void writeConstantInternal(raw_ostream &Out, const Constant *CV, AsmWriterContext &WriterCtx)
static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, AsmWriterContext &)
static void writeAsOperandInternal(raw_ostream &Out, const Value *V, AsmWriterContext &WriterCtx, bool PrintType=false)
static void printVisibility(GlobalValue::VisibilityTypes Vis, formatted_raw_ostream &Out)
constexpr IROptions DefaultOptions
static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, AsmWriterContext &WriterCtx)
static void writeDIExpression(raw_ostream &Out, const DIExpression *N, AsmWriterContext &WriterCtx)
static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL, AsmWriterContext &WriterCtx)
static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, AsmWriterContext &WriterCtx)
PrefixType
@ GlobalPrefix
@ LabelPrefix
@ LocalPrefix
@ NoPrefix
@ ComdatPrefix
static void maybePrintComdat(formatted_raw_ostream &Out, const GlobalObject &GO)
static void printDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT, formatted_raw_ostream &Out)
static bool printWithoutType(const Value &V, raw_ostream &O, SlotTracker *Machine, const Module *M)
Print without a type, skipping the TypePrinting object.
#define ST_DEBUG(X)
static void writeDIArgList(raw_ostream &Out, const DIArgList *N, AsmWriterContext &WriterCtx, bool FromValue=false)
static void writeDITemplateValueParameter(raw_ostream &Out, const DITemplateValueParameter *N, AsmWriterContext &WriterCtx)
static const Value * skipMetadataWrapper(const Value *V)
Look for a value that might be wrapped as metadata, e.g.
static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, AsmWriterContext &WriterCtx)
Atomic ordering constants.
This file contains the simple types necessary to represent the attributes associated with functions a...
static const Function * getParent(const Value *V)
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:690
This file contains the declarations for the subclasses of Constant, which represent the different fla...
dxil translate DXIL Translate Metadata
This file defines the DenseMap class.
@ Default
This file contains constants used for implementing Dwarf debug support.
This file contains the declaration of the GlobalIFunc class, which represents a single indirect funct...
GlobalValue::SanitizerMetadata SanitizerMetadata
Definition Globals.cpp:317
#define op(i)
Hexagon Common GEP
#define _
IRTranslator LLVM IR MI
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This file contains an interface for creating legacy passes to print out IR in various granularities.
Module.h This file contains the declarations for the Module class.
This defines the Use class.
static bool isAtomic(MachineBasicBlock::iterator I)
#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
Machine Check Debug Module
This file contains the declarations for metadata subclasses.
static bool InRange(int64_t Value, unsigned short Shift, int LBound, int HBound)
ModuleSummaryIndex.h This file contains the declarations the classes that hold the module index and s...
static bool processModule(Module &M, NVPTXTargetMachine &TM)
static bool processFunction(Function &F, NVPTXTargetMachine &TM)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
Function const char TargetMachine * Machine
if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod
static StringRef getName(Value *V)
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
This file contains some templates that are useful if you are working with the STL at all.
static bool contains(SmallPtrSetImpl< ConstantExpr * > &Cache, ConstantExpr *Expr, Constant *C)
Definition Value.cpp:479
This file provides utility classes that use RAII to save and restore values.
This file defines the SmallPtrSet class.
This file defines the SmallString class.
This file defines the SmallVector class.
This file contains some functions that are useful when dealing with strings.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
static UseListOrderStack predictUseListOrder(const Module &M)
Value * RHS
Value * LHS
static const fltSemantics & PPCDoubleDouble()
Definition APFloat.h:310
bool isNegative() const
Definition APFloat.h:1583
void toString(SmallVectorImpl< char > &Str, unsigned FormatPrecision=0, unsigned FormatMaxPadding=3, bool TruncateZero=true) const
Definition APFloat.h:1620
const fltSemantics & getSemantics() const
Definition APFloat.h:1591
bool isNaN() const
Definition APFloat.h:1581
bool isSignaling() const
Definition APFloat.h:1585
APInt bitcastToAPInt() const
Definition APFloat.h:1475
APInt getNaNPayload() const
If the value is a NaN value, return an integer containing the payload of this value.
Definition APFloat.h:1609
bool isInfinity() const
Definition APFloat.h:1580
Class for arbitrary precision integers.
Definition APInt.h:78
void clearBit(unsigned BitPosition)
Set a given bit to 0.
Definition APInt.h:1426
unsigned getActiveBits() const
Compute the number of active bits in the value.
Definition APInt.h:1532
LLVM_ABI APInt trunc(unsigned width) const
Truncate to new width.
Definition APInt.cpp:970
void toStringUnsigned(SmallVectorImpl< char > &Str, unsigned Radix=10) const
Considers the APInt to be unsigned and converts it into a string in the radix given.
Definition APInt.h:1711
bool isZero() const
Determine if this value is zero, i.e. all bits are clear.
Definition APInt.h:376
bool isSignMask() const
Check if the APInt's value is returned by getSignMask.
Definition APInt.h:462
unsigned getBitWidth() const
Return the number of bits in the APInt.
Definition APInt.h:1508
APInt lshr(unsigned shiftAmt) const
Logical right-shift function.
Definition APInt.h:853
Abstract interface of slot tracker storage.
const GlobalValueSummary & getAliasee() const
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
size_t size() const
Get the array size.
Definition ArrayRef.h:141
virtual void emitMDNodeAnnot(const MDNode *, formatted_raw_ostream &)
emitMDNodeAnnot - This may be implemented to emit a string right before a metadata node is emitted.
virtual void emitBasicBlockStartAnnot(const BasicBlock *, formatted_raw_ostream &)
emitBasicBlockStartAnnot - This may be implemented to emit a string right after the basic block label...
virtual void emitBasicBlockEndAnnot(const BasicBlock *, formatted_raw_ostream &)
emitBasicBlockEndAnnot - This may be implemented to emit a string right after the basic block.
virtual void emitFunctionAnnot(const Function *, formatted_raw_ostream &)
emitFunctionAnnot - This may be implemented to emit a string right before the start of a function.
virtual void emitInstructionAnnot(const Instruction *, formatted_raw_ostream &)
emitInstructionAnnot - This may be implemented to emit a string right before an instruction is emitte...
virtual void printInfoComment(const Value &, formatted_raw_ostream &)
printInfoComment - This may be implemented to emit a comment to the right of an instruction or global...
static LLVM_ABI StringRef getOperationName(BinOp Op)
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:406
bool hasAttributes() const
Return true if attributes exists in this set.
Definition Attributes.h:484
LLVM_ABI std::string getAsString(bool InAttrGrp=false) const
The Attribute is converted to a string of equivalent mnemonic.
LLVM_ABI Attribute::AttrKind getKindAsEnum() const
Return the attribute's kind as an enum (Attribute::AttrKind).
LLVM_ABI bool isTypeAttribute() const
Return true if the attribute is a type attribute.
LLVM_ABI Type * getValueAsType() const
Return the attribute's value as a Type.
LLVM Basic Block Representation.
Definition BasicBlock.h:62
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
LLVM_ABI void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the basic block to an output stream with an optional AssemblyAnnotationWriter.
LLVM_ABI bool isEntryBlock() const
Return true if this is the entry block of the containing function.
LLVM_ABI const Module * getModule() const
Return the module owning the function this basic block belongs to, or nullptr if the function does no...
OperandBundleUse getOperandBundleAt(unsigned Index) const
Return the operand bundle at a specific index.
unsigned getNumOperandBundles() const
Return the number of operand bundles associated with this User.
AttributeList getAttributes() const
Return the attributes for this call.
bool hasOperandBundles() const
Return true if this User has any operand bundles.
LLVM_ABI void print(raw_ostream &OS, bool IsForDebug=false) const
LLVM_ABI void dump() const
@ Largest
The linker will choose the largest COMDAT.
Definition Comdat.h:39
@ SameSize
The data referenced by the COMDAT must be the same size.
Definition Comdat.h:41
@ Any
The linker may choose any COMDAT.
Definition Comdat.h:37
@ NoDeduplicate
No deduplication is performed.
Definition Comdat.h:40
@ ExactMatch
The data referenced by the COMDAT must be the same.
Definition Comdat.h:38
SelectionKind getSelectionKind() const
Definition Comdat.h:47
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
LLVM_ABI APInt getSignedMin() const
Return the smallest signed value contained in the ConstantRange.
LLVM_ABI APInt getSignedMax() const
Return the largest signed value contained in the ConstantRange.
This is an important base class in LLVM.
Definition Constant.h:44
LLVM_ABI Constant * getSplatValue(bool AllowPoison=false) const
If all elements of the vector constant have the same value, return that value.
LLVM_ABI Constant * getAggregateElement(unsigned Elt) const
For aggregates (struct/array/vector) return the constant that corresponds to the specified element if...
List of ValueAsMetadata, to be used as an argument to a dbg.value intrinsic.
Basic type, like 'int' or 'float'.
Debug common block.
static LLVM_ABI const char * nameTableKindString(DebugNameTableKind PK)
static LLVM_ABI const char * emissionKindString(DebugEmissionKind EK)
Enumeration value.
A lightweight wrapper around an expression operand.
DWARF expression.
static LLVM_ABI const char * fixedPointKindString(FixedPointKind)
A pair of DIGlobalVariable and DIExpression.
An imported module (C++ using directive or similar).
A sequence of DILayerLoc entries.
A single intermediate-IR layer location.
Debug lexical block.
Macro Info DWARF-like metadata node.
Represents a module in the programming language, for example, a Clang module, or a Fortran module.
Debug lexical block.
Tagged DWARF-like metadata node.
static LLVM_ABI DIFlags splitFlags(DIFlags Flags, SmallVectorImpl< DIFlags > &SplitFlags)
Split up a flags bitfield.
static LLVM_ABI StringRef getFlagString(DIFlags Flag)
DIFlags
Debug info flags.
A property of a class or structure.
Wrapper structure that holds source language identity metadata that includes language name,...
uint32_t getVersion() const
Returns language version. Only valid for versioned language names.
uint16_t getName() const
Returns a versioned or unversioned language name.
String type, Fortran CHARACTER(n)
Subprogram description. Uses SubclassData1.
static LLVM_ABI DISPFlags splitFlags(DISPFlags Flags, SmallVectorImpl< DISPFlags > &SplitFlags)
Split up a flags bitfield for easier printing.
static LLVM_ABI StringRef getFlagString(DISPFlags Flag)
DISPFlags
Debug info subprogram flags.
Array subrange.
Type array for a subprogram.
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Per-instruction record of debug-info.
LLVM_ABI void dump() const
Instruction * MarkedInstr
Link back to the Instruction that owns this marker.
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on DbgMarker.
LLVM_ABI const BasicBlock * getParent() const
simple_ilist< DbgRecord > StoredDbgRecords
List of DbgRecords, the non-instruction equivalent of llvm.dbg.
Base class for non-instruction debug metadata records that have positions within IR.
DebugLoc getDebugLoc() const
LLVM_ABI void dump() const
DbgMarker * Marker
Marker that this DbgRecord is linked into.
Record of a variable value-assignment, aka a non instruction representation of the dbg....
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Metadata * getRawLocation() const
Returns the metadata operand for the first location description.
LLVM_ABI MDNode * getAsMDNode() const
Return this as a bar MDNode.
Definition DebugLoc.cpp:81
DenseMapIterator< KeyT, ValueT, KeyInfoT, BucketT > iterator
Definition DenseMap.h:678
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:247
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW=nullptr, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the function to an output stream with an optional AssemblyAnnotationWriter.
const Function & getFunction() const
Definition Function.h:167
const Argument * const_arg_iterator
Definition Function.h:74
LLVM_ABI Value * getBasePtr() const
LLVM_ABI Value * getDerivedPtr() const
Generic tagged DWARF-like metadata node.
const Constant * getAliasee() const
Definition GlobalAlias.h:87
const Constant * getResolver() const
Definition GlobalIFunc.h:73
StringRef getSection() const
Get the custom section of this global if it has one.
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
const Comdat * getComdat() const
bool hasSection() const
Check if this global has a custom object file section.
SummaryKind
Sububclass discriminator (for dyn_cast<> et al.)
bool hasPartition() const
static LLVM_ABI GUID getGUIDAssumingExternalLinkage(StringRef GlobalName)
Return a 64-bit global unique ID constructed from the name of a global symbol.
Definition Globals.cpp:80
LLVM_ABI const SanitizerMetadata & getSanitizerMetadata() const
Definition Globals.cpp:318
bool hasExternalLinkage() const
bool isDSOLocal() const
VisibilityTypes getVisibility() const
bool isImplicitDSOLocal() const
LinkageTypes getLinkage() const
uint64_t GUID
Declare a type to represent a global unique identifier for a global value.
ThreadLocalMode getThreadLocalMode() const
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition GlobalValue.h:74
@ DLLExportStorageClass
Function to be accessible from DLL.
Definition GlobalValue.h:77
@ DLLImportStorageClass
Function to be imported from DLL.
Definition GlobalValue.h:76
bool hasSanitizerMetadata() const
LLVM_ABI StringRef getPartition() const
Definition Globals.cpp:295
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition GlobalValue.h:67
@ DefaultVisibility
The GV is visible.
Definition GlobalValue.h:68
@ HiddenVisibility
The GV is hidden.
Definition GlobalValue.h:69
@ ProtectedVisibility
The GV is protected.
Definition GlobalValue.h:70
LLVM_ABI bool isMaterializable() const
If this function's Module is being lazily streamed in functions from disk or some other source,...
Definition Globals.cpp:47
UnnamedAddr getUnnamedAddr() const
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition GlobalValue.h:52
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
@ CommonLinkage
Tentative definitions.
Definition GlobalValue.h:63
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
@ LinkOnceAnyLinkage
Keep one copy of function when linking (inline)
Definition GlobalValue.h:55
@ WeakODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:58
@ ExternalLinkage
Externally visible function.
Definition GlobalValue.h:53
@ WeakAnyLinkage
Keep one copy of named function when linking (weak)
Definition GlobalValue.h:57
@ AppendingLinkage
Special purpose, only applies to global arrays.
Definition GlobalValue.h:59
@ AvailableExternallyLinkage
Available for inspection, not emission.
Definition GlobalValue.h:54
@ ExternalWeakLinkage
ExternalWeak linkage description.
Definition GlobalValue.h:62
@ LinkOnceODRLinkage
Same, but only replaced by something equivalent.
Definition GlobalValue.h:56
DLLStorageClassTypes getDLLStorageClass() const
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool isExternallyInitialized() const
bool hasInitializer() const
Definitions have initializers, declarations don't.
AttributeSet getAttributes() const
Return the attribute set for this global.
std::optional< CodeModel::Model > getCodeModel() const
Get the custom code model of this global if it has one.
MaybeAlign getAlign() const
Returns the alignment of the given variable.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
A helper class to return the specified delimiter string after the first invocation of operator String...
Metadata node.
Definition Metadata.h:1081
LLVM_ABI void printTree(raw_ostream &OS, const Module *M=nullptr) const
Print in tree shape.
LLVM_ABI void dumpTree() const
User-friendly dump in tree shape.
Tracking metadata reference owned by Metadata.
Definition Metadata.h:902
Tuple of metadata.
Definition Metadata.h:1496
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
Root of the metadata hierarchy.
Definition Metadata.h:64
LLVM_ABI void print(raw_ostream &OS, const Module *M=nullptr, bool IsForDebug=false) const
Print.
LLVM_ABI void printAsOperand(raw_ostream &OS, const Module *M=nullptr) const
Print as operand.
LLVM_ABI void dump() const
User-friendly dump.
Manage lifetime of a slot tracker for printing IR.
const Module * getModule() const
ModuleSlotTracker(SlotTracker &Machine, const Module *M, const Function *F=nullptr)
Wrap a preinitialized SlotTracker.
void renumberMetadataForAssembly(ArrayRef< const MDNode * > AdditionalMetadata, MachineMDNodeListType *AdditionalMetadataNodes=nullptr) const
Renumber module metadata and then additional metadata for canonical assembly output.
void collectMDNodes(MachineMDNodeListType &L) const
void setProcessHook(std::function< void(AbstractSlotTrackerStorage *, const Module *)>)
virtual ~ModuleSlotTracker()
Destructor to clean up storage.
void collectAdditionalMetadata(ArrayRef< const MDNode * > AdditionalMetadata, MachineMDNodeListType &AdditionalMetadataNodes) const
Collect metadata reachable from AdditionalMetadata but not from the module.
int getLocalSlot(const Value *V)
Return the slot number of the specified local value.
virtual bool shouldPrintDebugLocationInline(const DILocation *) const
Return whether a debug location should be printed inline instead of by ID.
SlotTracker * getMachine()
Lazily creates a slot tracker.
SmallVector< std::pair< unsigned, const MDNode * >, 0 > MachineMDNodeListType
void incorporateFunction(const Function &F)
Incorporate the given function.
Class to hold module path string table and global value map, and encapsulate methods for operating on...
const TypeIdSummaryMapTy & typeIds() const
ValueInfo getValueInfo(const GlobalValueSummaryMapTy::value_type &R) const
Return a ValueInfo for the index value_type (convenient when iterating index).
static constexpr const char * getRegularLTOModuleName()
const auto & typeIdCompatibleVtableMap() const
const StringMap< ModuleHash > & modulePaths() const
Table of modules, containing module hash and id.
LLVM_ABI void dump() const
Dump to stderr (for debugging).
GlobalValueSummaryMapTy::SortedEntriesRange sortedGlobalValueSummariesRange() const
uint64_t getStackIdAtIndex(unsigned Index) const
LLVM_ABI void print(raw_ostream &OS, bool IsForDebug=false) const
Print to an output stream.
LLVM_ABI uint64_t getFlags() const
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
void renumberMetadataForAssembly()
Renumber the IDs stored in metadata nodes into canonical assembly order.
iterator_range< alias_iterator > aliases()
Definition Module.h:853
iterator_range< global_iterator > globals()
Definition Module.h:802
void print(raw_ostream &OS, AssemblyAnnotationWriter *AAW, bool ShouldPreserveUseListOrder=false, bool IsForDebug=false) const
Print the module to an output stream with an optional AssemblyAnnotationWriter.
void dump() const
Dump the module to stderr (for debugging).
LLVM_ABI void dump() const
LLVM_ABI StringRef getName() const
LLVM_ABI void print(raw_ostream &ROS, bool IsForDebug=false) const
iterator_range< op_iterator > operands()
Definition Metadata.h:1893
unsigned getAddressSpace() const
Return the address space of the Pointer type.
This class provides computation of slot numbers for LLVM Assembly writing.
DenseMap< const Value *, unsigned > ValueMap
ValueMap - A mapping of Values to slot numbers.
bool mdn_empty() const
int getMetadataSlot(const MDNode *N) override
getMetadataSlot - Get the slot number of a MDNode.
~SlotTracker() override=default
int getTypeIdCompatibleVtableSlot(StringRef Id)
int getModulePathSlot(StringRef Path)
bool as_empty() const
unsigned mdn_size() const
SlotTracker(const SlotTracker &)=delete
void purgeFunction()
After calling incorporateFunction, use this method to remove the most recently incorporated function ...
mdn_iterator mdn_end()
int getTypeIdSlot(StringRef Id)
void initializeIfNeeded()
These functions do the actual initialization.
int getGlobalSlot(const GlobalValue *V)
getGlobalSlot - Get the slot number of a global value.
as_iterator as_begin()
SlotTracker(const Module *M, bool ShouldTrackMetadataDefinitions=false)
Construct from a module.
const Function * getFunction() const
DenseMap< GlobalValue::GUID, unsigned >::iterator guid_iterator
GUID map iterators.
void incorporateFunction(const Function *F)
If you'd like to deal with a function instead of just a module, use this method to get its data into ...
int getLocalSlot(const Value *V)
Return the slot number of the specified value in it's type plane.
int getAttributeGroupSlot(AttributeSet AS)
void setProcessHook(std::function< void(AbstractSlotTrackerStorage *, const Module *)>)
void createMetadataSlot(const MDNode *N) override
getMetadataSlot - Get the slot number of a MDNode.
DenseMap< const MDNode *, unsigned >::iterator mdn_iterator
MDNode map iterators.
as_iterator as_end()
unsigned as_size() const
SlotTracker & operator=(const SlotTracker &)=delete
int getGUIDSlot(GlobalValue::GUID GUID)
mdn_iterator mdn_begin()
int initializeIndexIfNeeded()
DenseMap< AttributeSet, unsigned >::iterator as_iterator
AttributeSet map iterators.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
Definition SmallString.h:26
reference emplace_back(ArgTypes &&... Args)
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.
unsigned size() const
Definition StringMap.h:104
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
ArrayRef< Type * > elements() const
bool isPacked() const
unsigned getNumElements() const
Random access to the elements.
bool isLiteral() const
Return true if this type is uniqued by structural equivalence, false if it is a struct definition.
bool isOpaque() const
Return true if this is a type with an identity that has no body specified yet.
LLVM_ABI StringRef getName() const
Return the name for this struct type if it has an identity.
Definition Type.cpp:736
ArrayRef< Type * > type_params() const
Return the type parameters for this particular target extension type.
ArrayRef< unsigned > int_params() const
Return the integer parameters for this particular target extension type.
TypeFinder - Walk over a module, identifying all of the types that are used by the module.
Definition TypeFinder.h:31
LLVM_ABI void run(const Module &M, bool onlyNamed)
iterator begin()
Definition TypeFinder.h:51
bool empty() const
Definition TypeFinder.h:57
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
LLVM_ABI StringRef getTargetExtName() const
Type(LLVMContext &C, TypeID tid)
Definition Type.h:95
LLVM_ABI void dump() const
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false, bool NoDetails=false) const
Print the current type.
LLVM_ABI unsigned getByteBitWidth() const
TypeID getTypeID() const
Return the type id for the type.
Definition Type.h:138
Type * getElementType() const
unsigned getAddressSpace() const
Return the address space of the Pointer type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVM_ABI void print(raw_ostream &O, bool IsForDebug=false) const
Implement operator<< on Value.
LLVM_ABI void getAllMetadata(SmallVectorImpl< std::pair< unsigned, MDNode * > > &MDs) const
Appends all metadata attached to this value to MDs, sorting by KindID.
iterator_range< user_iterator > users()
Definition Value.h:428
LLVM_ABI void printAsOperand(raw_ostream &O, bool PrintType=true, const Module *M=nullptr) const
Print the name of this Value out to the specified raw_ostream.
bool hasName() const
Definition Value.h:263
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:314
LLVM_ABI void dump() const
Support for debugging, callable in GDB: V->dump()
formatted_raw_ostream - A raw_ostream that wraps another one and keeps track of line and column posit...
formatted_raw_ostream & PadToColumn(unsigned NewCol)
PadToColumn - Align the output to some column number.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
raw_ostream & indent(unsigned NumSpaces)
indent - Insert 'NumSpaces' spaces.
CallInst * Call
LLVM_ABI StringRef LanguageDialectString(unsigned LanguageDialect)
Definition Dwarf.cpp:623
LLVM_ABI StringRef SourceLanguageNameString(SourceLanguageName Lang)
Definition Dwarf.cpp:603
LLVM_ABI StringRef EnumKindString(unsigned EnumKind)
Definition Dwarf.cpp:394
LLVM_ABI StringRef LanguageString(unsigned Language)
Definition Dwarf.cpp:413
LLVM_ABI StringRef AttributeEncodingString(unsigned Encoding)
Definition Dwarf.cpp:264
LLVM_ABI StringRef ConventionString(unsigned Convention)
Definition Dwarf.cpp:659
LLVM_ABI StringRef MacinfoString(unsigned Encoding)
Definition Dwarf.cpp:723
LLVM_ABI StringRef OperationEncodingString(unsigned Encoding)
Definition Dwarf.cpp:138
LLVM_ABI StringRef TagString(unsigned Tag)
Definition Dwarf.cpp:21
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
This file contains the declaration of the Comdat class, which represents a single COMDAT in LLVM.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Attrs[]
Key for Kernel::Metadata::mAttrs.
@ Entry
Definition COFF.h:862
@ AArch64_VectorCall
Used between AArch64 Advanced SIMD functions.
@ X86_64_SysV
The C convention as specified in the x86-64 supplement to the System V ABI, used on most non-Windows ...
@ RISCV_VectorCall
Calling convention used for RISC-V V-extension.
@ AMDGPU_CS
Used for Mesa/AMDPAL compute shaders.
@ AMDGPU_VS
Used for Mesa vertex shaders, or AMDPAL last shader stage before rasterization (vertex shader if tess...
@ AVR_SIGNAL
Used for AVR signal routines.
@ Swift
Calling convention for Swift.
Definition CallingConv.h:69
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
@ AArch64_SVE_VectorCall
Used between AArch64 SVE functions.
@ ARM_APCS
ARM Procedure Calling Standard (obsolete, but still used on some targets).
@ CHERIoT_CompartmentCall
Calling convention used for CHERIoT when crossing a protection boundary.
@ CFGuard_Check
Special calling convention on Windows for calling the Control Guard Check ICall funtion.
Definition CallingConv.h:82
@ AVR_INTR
Used for AVR interrupt routines.
@ PreserveMost
Used for runtime calls that preserves most registers.
Definition CallingConv.h:63
@ AnyReg
OBSOLETED - Used for stack based JavaScript calls.
Definition CallingConv.h:60
@ AMDGPU_Gfx
Used for AMD graphics targets.
@ DUMMY_HHVM
Placeholders for HHVM calling conventions (deprecated, removed).
@ AMDGPU_CS_ChainPreserve
Used on AMDGPUs to give the middle-end more control over argument placement.
@ AMDGPU_HS
Used for Mesa/AMDPAL hull shaders (= tessellation control shaders).
@ ARM_AAPCS
ARM Architecture Procedure Calling Standard calling convention (aka EABI).
@ CHERIoT_CompartmentCallee
Calling convention used for the callee of CHERIoT_CompartmentCall.
@ AMDGPU_GS
Used for Mesa/AMDPAL geometry shaders.
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2
Preserve X2-X15, X19-X29, SP, Z0-Z31, P0-P15.
@ CHERIoT_LibraryCall
Calling convention used for CHERIoT for cross-library calls to a stateless compartment.
@ CXX_FAST_TLS
Used for access functions.
Definition CallingConv.h:72
@ X86_INTR
x86 hardware interrupt context.
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
Preserve X0-X13, X19-X29, SP, Z0-Z31, P0-P15.
@ WASM_EmscriptenInvoke
For emscripten __invoke_* functions.
@ AMDGPU_CS_Chain
Used on AMDGPUs to give the middle-end more control over argument placement.
@ GHC
Used by the Glasgow Haskell Compiler (GHC).
Definition CallingConv.h:50
@ AMDGPU_PS
Used for Mesa/AMDPAL pixel shaders.
@ Cold
Attempts to make code in the caller as efficient as possible under the assumption that the call is no...
Definition CallingConv.h:47
@ AArch64_SME_ABI_Support_Routines_PreserveMost_From_X1
Preserve X1-X15, X19-X29, SP, Z0-Z31, P0-P15.
@ X86_ThisCall
Similar to X86_StdCall.
@ PTX_Device
Call to a PTX device function.
@ SPIR_KERNEL
Used for SPIR kernel functions.
@ PreserveAll
Used for runtime calls that preserves (almost) all registers.
Definition CallingConv.h:66
@ X86_StdCall
stdcall is mostly used by the Win32 API.
Definition CallingConv.h:99
@ SPIR_FUNC
Used for SPIR non-kernel device functions.
@ Fast
Attempts to make calls as fast as possible (e.g.
Definition CallingConv.h:41
@ MSP430_INTR
Used for MSP430 interrupt routines.
@ X86_VectorCall
MSVC calling convention that passes vectors and vector aggregates in SSE registers.
@ Intel_OCL_BI
Used for Intel OpenCL built-ins.
@ PreserveNone
Used for runtime calls that preserves none general registers.
Definition CallingConv.h:90
@ AMDGPU_ES
Used for AMDPAL shader stage before geometry shader if geometry is in use.
@ Tail
Attemps to make calls as fast as possible while guaranteeing that tail call optimization can always b...
Definition CallingConv.h:76
@ Win64
The C convention as implemented on Windows/x86-64 and AArch64.
@ PTX_Kernel
Call to a PTX kernel. Passes all arguments in parameter space.
@ SwiftTail
This follows the Swift calling convention in how arguments are passed but guarantees tail calls will ...
Definition CallingConv.h:87
@ GRAAL
Used by GraalVM. Two additional registers are reserved.
@ AMDGPU_LS
Used for AMDPAL vertex shader if tessellation is in use.
@ ARM_AAPCS_VFP
Same as ARM_AAPCS, but uses hard floating point ABI.
@ X86_RegCall
Register calling convention used for parameters transfer optimization.
@ M68k_RTD
Used for M68k rtd-based CC (similar to X86's stdcall).
@ X86_FastCall
'fast' analog of X86_StdCall.
LLVM_ABI void printImmArg(ID IID, unsigned ArgIdx, raw_ostream &OS, const Constant *ImmArgVal)
Print the argument info for the arguments with ArgInfo.
LLVM_ABI bool hasPrettyPrintedArgs(ID id)
Returns true if the intrinsic has pretty printed immediate arguments.
@ System
Synchronized with respect to all concurrently executing threads.
Definition LLVMContext.h:66
SourceLanguageName
Definition Dwarf.h:229
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
bool empty() const
Definition BasicBlock.h:101
bool isElementwise(const VPValue *V)
Return true if V is elementwise, i.e. none of the lanes are permuted.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)
zip iterator that assumes that all iteratees have the same length.
Definition STLExtras.h:856
InterleavedRange< Range > interleaved(const Range &R, StringRef Separator=", ", StringRef Prefix="", StringRef Suffix="")
Output range R as a sequence of interleaved elements.
const char * getHotnessName(CalleeInfo::HotnessType HT)
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
LLVM_ABI void printEscapedString(StringRef Name, raw_ostream &Out)
Print each character of the specified string, escaping it if it is not printable or if it is an escap...
constexpr auto equal_to(T &&Arg)
Functor variant of std::equal_to that can be used as a UnaryPredicate in functional algorithms like a...
Definition STLExtras.h:2189
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
const char * toIRString(AtomicOrdering ao)
String used by LLVM IR to represent atomic ordering.
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
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
char hexdigit(unsigned X, bool LowerCase=false)
hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).
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
LLVM_ATTRIBUTE_VISIBILITY_DEFAULT AnalysisKey InnerAnalysisManagerProxy< AnalysisManagerT, IRUnitT, ExtraArgTs... >::Key
constexpr int PoisonMaskElem
AtomicOrdering
Atomic ordering for LLVM's memory model.
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
Definition InstrProf.h:145
DWARFExpression::Operation Op
ArrayRef(const T &OneElt) -> ArrayRef< T >
std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)
const IROptions & getIROptions(const LLVMContext &C)
Definition IROptions.h:27
LLVM_ABI Printable printBasicBlock(const BasicBlock *BB)
Print BasicBlock BB as an operand or print "<nullptr>" if BB is a nullptr.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void erase_if(Container &C, UnaryPredicate P)
Provide a container algorithm similar to C++ Library Fundamentals v2's erase_if which is equivalent t...
Definition STLExtras.h:2208
auto predecessors(const MachineBasicBlock *BB)
constexpr bool valueOr(BoolOrDefault X, bool Default)
bool pred_empty(const BasicBlock *BB)
Definition CFG.h:107
std::vector< TypeIdOffsetVtableInfo > TypeIdCompatibleVtableInfo
List of vtable definitions decorated by a particular type identifier, and their corresponding offsets...
static auto filterDbgVars(iterator_range< simple_ilist< DbgRecord >::iterator > R)
Filter the DbgRecord range to DbgVariableRecord types only and downcast.
LLVM_ABI void printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name)
Print out a name of an LLVM value without any prefixes.
#define N
#define NC
Definition regutils.h:42
A single checksum, represented by a Kind and a Value (a string).
T Value
The string value of the checksum.
StringRef getKindAsString() const
std::vector< ConstVCall > TypeCheckedLoadConstVCalls
std::vector< VFuncId > TypeCheckedLoadVCalls
std::vector< ConstVCall > TypeTestAssumeConstVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
std::vector< GlobalValue::GUID > TypeTests
List of type identifiers used by this function in llvm.type.test intrinsics referenced by something o...
std::vector< VFuncId > TypeTestAssumeVCalls
List of virtual calls made by this function using (respectively) llvm.assume(llvm....
unsigned NoRenameOnPromotion
This field is written by the ThinLTO prelink stage to decide whether a particular static global value...
unsigned DSOLocal
Indicates that the linker resolved the symbol to a definition from within the same linkage unit.
unsigned CanAutoHide
In the per-module summary, indicates that the global value is linkonce_odr and global unnamed addr (s...
unsigned ImportType
This field is written by the ThinLTO indexing step to postlink combined summary.
unsigned NotEligibleToImport
Indicate if the global value cannot be imported (e.g.
unsigned Linkage
The linkage type of the associated global value.
unsigned Visibility
Indicates the visibility.
unsigned Live
In per-module summary, indicate that the global value must be considered a live root for index-based ...
StringRef getTagName() const
Return the tag of this operand bundle as a string.
ArrayRef< Use > Inputs
A utility class that uses RAII to save and restore the value of a variable.
std::map< uint64_t, WholeProgramDevirtResolution > WPDRes
Mapping from byte offset to whole-program devirt resolution for that (typeid, byte offset) pair.
TypeTestResolution TTRes
Kind
Specifies which kind of type check we should emit for this byte array.
@ Unknown
Unknown (analysis not performed, don't lower)
@ Single
Single element (last example in "Short Inline Bit Vectors")
@ Inline
Inlined bit vector ("Short Inline Bit Vectors")
@ Unsat
Unsatisfiable type (i.e. no global has this type metadata)
@ AllOnes
All-ones bit vector ("Eliminating Bit Vector Checks for All-Ones Bit Vectors")
@ ByteArray
Test a byte array (first example)
unsigned SizeM1BitWidth
Range of size-1 expressed as a bit width.
enum llvm::TypeTestResolution::Kind TheKind
@ UniformRetVal
Uniform return value optimization.
@ VirtualConstProp
Virtual constant propagation.
@ UniqueRetVal
Unique return value optimization.
@ Indir
Just do a regular virtual call.
enum llvm::WholeProgramDevirtResolution::Kind TheKind
std::map< std::vector< uint64_t >, ByArg > ResByArg
Resolutions for calls with all constant integer arguments (excluding the first argument,...
@ SingleImpl
Single implementation devirtualization.
@ Indir
Just do a regular virtual call.
@ BranchFunnel
When retpoline mitigation is enabled, use a branch funnel that is defined in the merged module.
Function object to check whether the second component of a container supported by std::get (like std:...
Definition STLExtras.h:1464