1 //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
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
7 //===----------------------------------------------------------------------===//
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.
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.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
23 #include "llvm/ADT/SetVector.h"
24 #include "llvm/ADT/SmallPtrSet.h"
25 #include "llvm/ADT/SmallString.h"
26 #include "llvm/ADT/SmallVector.h"
27 #include "llvm/ADT/StringExtras.h"
28 #include "llvm/ADT/StringRef.h"
29 #include "llvm/ADT/iterator_range.h"
30 #include "llvm/BinaryFormat/Dwarf.h"
31 #include "llvm/Config/llvm-config.h"
32 #include "llvm/IR/Argument.h"
33 #include "llvm/IR/AssemblyAnnotationWriter.h"
34 #include "llvm/IR/Attributes.h"
35 #include "llvm/IR/BasicBlock.h"
36 #include "llvm/IR/CFG.h"
37 #include "llvm/IR/CallingConv.h"
38 #include "llvm/IR/Comdat.h"
39 #include "llvm/IR/Constant.h"
40 #include "llvm/IR/Constants.h"
41 #include "llvm/IR/DebugInfoMetadata.h"
42 #include "llvm/IR/DebugProgramInstruction.h"
43 #include "llvm/IR/DerivedTypes.h"
44 #include "llvm/IR/Function.h"
45 #include "llvm/IR/GlobalAlias.h"
46 #include "llvm/IR/GlobalIFunc.h"
47 #include "llvm/IR/GlobalObject.h"
48 #include "llvm/IR/GlobalValue.h"
49 #include "llvm/IR/GlobalVariable.h"
50 #include "llvm/IR/IRPrintingPasses.h"
51 #include "llvm/IR/InlineAsm.h"
52 #include "llvm/IR/InstrTypes.h"
53 #include "llvm/IR/Instruction.h"
54 #include "llvm/IR/Instructions.h"
55 #include "llvm/IR/IntrinsicInst.h"
56 #include "llvm/IR/LLVMContext.h"
57 #include "llvm/IR/Metadata.h"
58 #include "llvm/IR/Module.h"
59 #include "llvm/IR/ModuleSlotTracker.h"
60 #include "llvm/IR/ModuleSummaryIndex.h"
61 #include "llvm/IR/Operator.h"
62 #include "llvm/IR/Type.h"
63 #include "llvm/IR/TypeFinder.h"
64 #include "llvm/IR/TypedPointerType.h"
65 #include "llvm/IR/Use.h"
66 #include "llvm/IR/User.h"
67 #include "llvm/IR/Value.h"
68 #include "llvm/Support/AtomicOrdering.h"
69 #include "llvm/Support/Casting.h"
70 #include "llvm/Support/Compiler.h"
71 #include "llvm/Support/Debug.h"
72 #include "llvm/Support/ErrorHandling.h"
73 #include "llvm/Support/Format.h"
74 #include "llvm/Support/FormattedStream.h"
75 #include "llvm/Support/SaveAndRestore.h"
76 #include "llvm/Support/raw_ostream.h"
92 // Make virtual table appear in this compilation unit.
93 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 using OrderMap
= MapVector
<const Value
*, unsigned>;
101 using UseListOrderMap
=
102 DenseMap
<const Function
*, MapVector
<const Value
*, std::vector
<unsigned>>>;
104 /// Look for a value that might be wrapped as metadata, e.g. a value in a
105 /// metadata operand. Returns the input value as-is if it is not wrapped.
106 static const Value
*skipMetadataWrapper(const Value
*V
) {
107 if (const auto *MAV
= dyn_cast
<MetadataAsValue
>(V
))
108 if (const auto *VAM
= dyn_cast
<ValueAsMetadata
>(MAV
->getMetadata()))
109 return VAM
->getValue();
113 static void orderValue(const Value
*V
, OrderMap
&OM
) {
117 if (const Constant
*C
= dyn_cast
<Constant
>(V
))
118 if (C
->getNumOperands() && !isa
<GlobalValue
>(C
))
119 for (const Value
*Op
: C
->operands())
120 if (!isa
<BasicBlock
>(Op
) && !isa
<GlobalValue
>(Op
))
123 // Note: we cannot cache this lookup above, since inserting into the map
124 // changes the map's size, and thus affects the other IDs.
125 unsigned ID
= OM
.size() + 1;
129 static OrderMap
orderModule(const Module
*M
) {
132 for (const GlobalVariable
&G
: M
->globals()) {
133 if (G
.hasInitializer())
134 if (!isa
<GlobalValue
>(G
.getInitializer()))
135 orderValue(G
.getInitializer(), OM
);
138 for (const GlobalAlias
&A
: M
->aliases()) {
139 if (!isa
<GlobalValue
>(A
.getAliasee()))
140 orderValue(A
.getAliasee(), OM
);
143 for (const GlobalIFunc
&I
: M
->ifuncs()) {
144 if (!isa
<GlobalValue
>(I
.getResolver()))
145 orderValue(I
.getResolver(), OM
);
148 for (const Function
&F
: *M
) {
149 for (const Use
&U
: F
.operands())
150 if (!isa
<GlobalValue
>(U
.get()))
151 orderValue(U
.get(), OM
);
155 if (F
.isDeclaration())
158 for (const Argument
&A
: F
.args())
160 for (const BasicBlock
&BB
: F
) {
162 for (const Instruction
&I
: BB
) {
163 for (const Value
*Op
: I
.operands()) {
164 Op
= skipMetadataWrapper(Op
);
165 if ((isa
<Constant
>(*Op
) && !isa
<GlobalValue
>(*Op
)) ||
176 static std::vector
<unsigned>
177 predictValueUseListOrder(const Value
*V
, unsigned ID
, const OrderMap
&OM
) {
178 // Predict use-list order for this one.
179 using Entry
= std::pair
<const Use
*, unsigned>;
180 SmallVector
<Entry
, 64> List
;
181 for (const Use
&U
: V
->uses())
182 // Check if this user will be serialized.
183 if (OM
.lookup(U
.getUser()))
184 List
.push_back(std::make_pair(&U
, List
.size()));
187 // We may have lost some users.
190 // When referencing a value before its declaration, a temporary value is
191 // created, which will later be RAUWed with the actual value. This reverses
192 // the use list. This happens for all values apart from basic blocks.
193 bool GetsReversed
= !isa
<BasicBlock
>(V
);
194 if (auto *BA
= dyn_cast
<BlockAddress
>(V
))
195 ID
= OM
.lookup(BA
->getBasicBlock());
196 llvm::sort(List
, [&](const Entry
&L
, const Entry
&R
) {
197 const Use
*LU
= L
.first
;
198 const Use
*RU
= R
.first
;
202 auto LID
= OM
.lookup(LU
->getUser());
203 auto RID
= OM
.lookup(RU
->getUser());
205 // If ID is 4, then expect: 7 6 5 1 2 3.
219 // LID and RID are equal, so we have different operands of the same user.
220 // Assume operands are added in order for all instructions.
223 return LU
->getOperandNo() < RU
->getOperandNo();
224 return LU
->getOperandNo() > RU
->getOperandNo();
227 if (llvm::is_sorted(List
, llvm::less_second()))
228 // Order is already correct.
231 // Store the shuffle.
232 std::vector
<unsigned> Shuffle(List
.size());
233 for (size_t I
= 0, E
= List
.size(); I
!= E
; ++I
)
234 Shuffle
[I
] = List
[I
].second
;
238 static UseListOrderMap
predictUseListOrder(const Module
*M
) {
239 OrderMap OM
= orderModule(M
);
240 UseListOrderMap ULOM
;
241 for (const auto &Pair
: OM
) {
242 const Value
*V
= Pair
.first
;
243 if (V
->use_empty() || std::next(V
->use_begin()) == V
->use_end())
246 std::vector
<unsigned> Shuffle
=
247 predictValueUseListOrder(V
, Pair
.second
, OM
);
251 const Function
*F
= nullptr;
252 if (auto *I
= dyn_cast
<Instruction
>(V
))
253 F
= I
->getFunction();
254 if (auto *A
= dyn_cast
<Argument
>(V
))
256 if (auto *BB
= dyn_cast
<BasicBlock
>(V
))
258 ULOM
[F
][V
] = std::move(Shuffle
);
263 static const Module
*getModuleFromVal(const Value
*V
) {
264 if (const Argument
*MA
= dyn_cast
<Argument
>(V
))
265 return MA
->getParent() ? MA
->getParent()->getParent() : nullptr;
267 if (const BasicBlock
*BB
= dyn_cast
<BasicBlock
>(V
))
268 return BB
->getParent() ? BB
->getParent()->getParent() : nullptr;
270 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
)) {
271 const Function
*M
= I
->getParent() ? I
->getParent()->getParent() : nullptr;
272 return M
? M
->getParent() : nullptr;
275 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
))
276 return GV
->getParent();
278 if (const auto *MAV
= dyn_cast
<MetadataAsValue
>(V
)) {
279 for (const User
*U
: MAV
->users())
280 if (isa
<Instruction
>(U
))
281 if (const Module
*M
= getModuleFromVal(U
))
289 static const Module
*getModuleFromDPI(const DPMarker
*Marker
) {
291 Marker
->getParent() ? Marker
->getParent()->getParent() : nullptr;
292 return M
? M
->getParent() : nullptr;
295 static const Module
*getModuleFromDPI(const DPValue
*DPV
) {
296 return getModuleFromDPI(DPV
->getMarker());
299 static void PrintCallingConv(unsigned cc
, raw_ostream
&Out
) {
301 default: Out
<< "cc" << cc
; break;
302 case CallingConv::Fast
: Out
<< "fastcc"; break;
303 case CallingConv::Cold
: Out
<< "coldcc"; break;
304 case CallingConv::AnyReg
: Out
<< "anyregcc"; break;
305 case CallingConv::PreserveMost
: Out
<< "preserve_mostcc"; break;
306 case CallingConv::PreserveAll
: Out
<< "preserve_allcc"; break;
307 case CallingConv::CXX_FAST_TLS
: Out
<< "cxx_fast_tlscc"; break;
308 case CallingConv::GHC
: Out
<< "ghccc"; break;
309 case CallingConv::Tail
: Out
<< "tailcc"; break;
310 case CallingConv::GRAAL
: Out
<< "graalcc"; break;
311 case CallingConv::CFGuard_Check
: Out
<< "cfguard_checkcc"; break;
312 case CallingConv::X86_StdCall
: Out
<< "x86_stdcallcc"; break;
313 case CallingConv::X86_FastCall
: Out
<< "x86_fastcallcc"; break;
314 case CallingConv::X86_ThisCall
: Out
<< "x86_thiscallcc"; break;
315 case CallingConv::X86_RegCall
: Out
<< "x86_regcallcc"; break;
316 case CallingConv::X86_VectorCall
:Out
<< "x86_vectorcallcc"; break;
317 case CallingConv::Intel_OCL_BI
: Out
<< "intel_ocl_bicc"; break;
318 case CallingConv::ARM_APCS
: Out
<< "arm_apcscc"; break;
319 case CallingConv::ARM_AAPCS
: Out
<< "arm_aapcscc"; break;
320 case CallingConv::ARM_AAPCS_VFP
: Out
<< "arm_aapcs_vfpcc"; break;
321 case CallingConv::AArch64_VectorCall
: Out
<< "aarch64_vector_pcs"; break;
322 case CallingConv::AArch64_SVE_VectorCall
:
323 Out
<< "aarch64_sve_vector_pcs";
325 case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0
:
326 Out
<< "aarch64_sme_preservemost_from_x0";
328 case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2
:
329 Out
<< "aarch64_sme_preservemost_from_x2";
331 case CallingConv::MSP430_INTR
: Out
<< "msp430_intrcc"; break;
332 case CallingConv::AVR_INTR
: Out
<< "avr_intrcc "; break;
333 case CallingConv::AVR_SIGNAL
: Out
<< "avr_signalcc "; break;
334 case CallingConv::PTX_Kernel
: Out
<< "ptx_kernel"; break;
335 case CallingConv::PTX_Device
: Out
<< "ptx_device"; break;
336 case CallingConv::X86_64_SysV
: Out
<< "x86_64_sysvcc"; break;
337 case CallingConv::Win64
: Out
<< "win64cc"; break;
338 case CallingConv::SPIR_FUNC
: Out
<< "spir_func"; break;
339 case CallingConv::SPIR_KERNEL
: Out
<< "spir_kernel"; break;
340 case CallingConv::Swift
: Out
<< "swiftcc"; break;
341 case CallingConv::SwiftTail
: Out
<< "swifttailcc"; break;
342 case CallingConv::X86_INTR
: Out
<< "x86_intrcc"; break;
343 case CallingConv::DUMMY_HHVM
:
346 case CallingConv::DUMMY_HHVM_C
:
349 case CallingConv::AMDGPU_VS
: Out
<< "amdgpu_vs"; break;
350 case CallingConv::AMDGPU_LS
: Out
<< "amdgpu_ls"; break;
351 case CallingConv::AMDGPU_HS
: Out
<< "amdgpu_hs"; break;
352 case CallingConv::AMDGPU_ES
: Out
<< "amdgpu_es"; break;
353 case CallingConv::AMDGPU_GS
: Out
<< "amdgpu_gs"; break;
354 case CallingConv::AMDGPU_PS
: Out
<< "amdgpu_ps"; break;
355 case CallingConv::AMDGPU_CS
: Out
<< "amdgpu_cs"; break;
356 case CallingConv::AMDGPU_CS_Chain
:
357 Out
<< "amdgpu_cs_chain";
359 case CallingConv::AMDGPU_CS_ChainPreserve
:
360 Out
<< "amdgpu_cs_chain_preserve";
362 case CallingConv::AMDGPU_KERNEL
: Out
<< "amdgpu_kernel"; break;
363 case CallingConv::AMDGPU_Gfx
: Out
<< "amdgpu_gfx"; break;
364 case CallingConv::M68k_RTD
: Out
<< "m68k_rtdcc"; break;
376 void llvm::printLLVMNameWithoutPrefix(raw_ostream
&OS
, StringRef Name
) {
377 assert(!Name
.empty() && "Cannot get empty name!");
379 // Scan the name to see if it needs quotes first.
380 bool NeedsQuotes
= isdigit(static_cast<unsigned char>(Name
[0]));
382 for (unsigned char C
: Name
) {
383 // By making this unsigned, the value passed in to isalnum will always be
384 // in the range 0-255. This is important when building with MSVC because
385 // its implementation will assert. This situation can arise when dealing
386 // with UTF-8 multibyte characters.
387 if (!isalnum(static_cast<unsigned char>(C
)) && C
!= '-' && C
!= '.' &&
395 // If we didn't need any quotes, just write out the name in one blast.
401 // Okay, we need quotes. Output the quotes and escape any scary characters as
404 printEscapedString(Name
, OS
);
408 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
409 /// (if the string only contains simple characters) or is surrounded with ""'s
410 /// (if it has special chars in it). Print it out.
411 static void PrintLLVMName(raw_ostream
&OS
, StringRef Name
, PrefixType Prefix
) {
427 printLLVMNameWithoutPrefix(OS
, Name
);
430 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
431 /// (if the string only contains simple characters) or is surrounded with ""'s
432 /// (if it has special chars in it). Print it out.
433 static void PrintLLVMName(raw_ostream
&OS
, const Value
*V
) {
434 PrintLLVMName(OS
, V
->getName(),
435 isa
<GlobalValue
>(V
) ? GlobalPrefix
: LocalPrefix
);
438 static void PrintShuffleMask(raw_ostream
&Out
, Type
*Ty
, ArrayRef
<int> Mask
) {
440 if (isa
<ScalableVectorType
>(Ty
))
442 Out
<< Mask
.size() << " x i32> ";
443 bool FirstElt
= true;
444 if (all_of(Mask
, [](int Elt
) { return Elt
== 0; })) {
445 Out
<< "zeroinitializer";
446 } else if (all_of(Mask
, [](int Elt
) { return Elt
== PoisonMaskElem
; })) {
450 for (int Elt
: Mask
) {
456 if (Elt
== PoisonMaskElem
)
469 TypePrinting(const Module
*M
= nullptr) : DeferredM(M
) {}
471 TypePrinting(const TypePrinting
&) = delete;
472 TypePrinting
&operator=(const TypePrinting
&) = delete;
474 /// The named types that are used by the current module.
475 TypeFinder
&getNamedTypes();
477 /// The numbered types, number to type mapping.
478 std::vector
<StructType
*> &getNumberedTypes();
482 void print(Type
*Ty
, raw_ostream
&OS
);
484 void printStructBody(StructType
*Ty
, raw_ostream
&OS
);
487 void incorporateTypes();
489 /// A module to process lazily when needed. Set to nullptr as soon as used.
490 const Module
*DeferredM
;
492 TypeFinder NamedTypes
;
494 // The numbered types, along with their value.
495 DenseMap
<StructType
*, unsigned> Type2Number
;
497 std::vector
<StructType
*> NumberedTypes
;
500 } // end anonymous namespace
502 TypeFinder
&TypePrinting::getNamedTypes() {
507 std::vector
<StructType
*> &TypePrinting::getNumberedTypes() {
510 // We know all the numbers that each type is used and we know that it is a
511 // dense assignment. Convert the map to an index table, if it's not done
512 // already (judging from the sizes):
513 if (NumberedTypes
.size() == Type2Number
.size())
514 return NumberedTypes
;
516 NumberedTypes
.resize(Type2Number
.size());
517 for (const auto &P
: Type2Number
) {
518 assert(P
.second
< NumberedTypes
.size() && "Didn't get a dense numbering?");
519 assert(!NumberedTypes
[P
.second
] && "Didn't get a unique numbering?");
520 NumberedTypes
[P
.second
] = P
.first
;
522 return NumberedTypes
;
525 bool TypePrinting::empty() {
527 return NamedTypes
.empty() && Type2Number
.empty();
530 void TypePrinting::incorporateTypes() {
534 NamedTypes
.run(*DeferredM
, false);
537 // The list of struct types we got back includes all the struct types, split
538 // the unnamed ones out to a numbering and remove the anonymous structs.
539 unsigned NextNumber
= 0;
541 std::vector
<StructType
*>::iterator NextToUse
= NamedTypes
.begin();
542 for (StructType
*STy
: NamedTypes
) {
543 // Ignore anonymous types.
544 if (STy
->isLiteral())
547 if (STy
->getName().empty())
548 Type2Number
[STy
] = NextNumber
++;
553 NamedTypes
.erase(NextToUse
, NamedTypes
.end());
556 /// Write the specified type to the specified raw_ostream, making use of type
557 /// names or up references to shorten the type name where possible.
558 void TypePrinting::print(Type
*Ty
, raw_ostream
&OS
) {
559 switch (Ty
->getTypeID()) {
560 case Type::VoidTyID
: OS
<< "void"; return;
561 case Type::HalfTyID
: OS
<< "half"; return;
562 case Type::BFloatTyID
: OS
<< "bfloat"; return;
563 case Type::FloatTyID
: OS
<< "float"; return;
564 case Type::DoubleTyID
: OS
<< "double"; return;
565 case Type::X86_FP80TyID
: OS
<< "x86_fp80"; return;
566 case Type::FP128TyID
: OS
<< "fp128"; return;
567 case Type::PPC_FP128TyID
: OS
<< "ppc_fp128"; return;
568 case Type::LabelTyID
: OS
<< "label"; return;
569 case Type::MetadataTyID
: OS
<< "metadata"; return;
570 case Type::X86_MMXTyID
: OS
<< "x86_mmx"; return;
571 case Type::X86_AMXTyID
: OS
<< "x86_amx"; return;
572 case Type::TokenTyID
: OS
<< "token"; return;
573 case Type::IntegerTyID
:
574 OS
<< 'i' << cast
<IntegerType
>(Ty
)->getBitWidth();
577 case Type::FunctionTyID
: {
578 FunctionType
*FTy
= cast
<FunctionType
>(Ty
);
579 print(FTy
->getReturnType(), OS
);
582 for (Type
*Ty
: FTy
->params()) {
591 case Type::StructTyID
: {
592 StructType
*STy
= cast
<StructType
>(Ty
);
594 if (STy
->isLiteral())
595 return printStructBody(STy
, OS
);
597 if (!STy
->getName().empty())
598 return PrintLLVMName(OS
, STy
->getName(), LocalPrefix
);
601 const auto I
= Type2Number
.find(STy
);
602 if (I
!= Type2Number
.end())
603 OS
<< '%' << I
->second
;
604 else // Not enumerated, print the hex address.
605 OS
<< "%\"type " << STy
<< '\"';
608 case Type::PointerTyID
: {
609 PointerType
*PTy
= cast
<PointerType
>(Ty
);
611 if (unsigned AddressSpace
= PTy
->getAddressSpace())
612 OS
<< " addrspace(" << AddressSpace
<< ')';
615 case Type::ArrayTyID
: {
616 ArrayType
*ATy
= cast
<ArrayType
>(Ty
);
617 OS
<< '[' << ATy
->getNumElements() << " x ";
618 print(ATy
->getElementType(), OS
);
622 case Type::FixedVectorTyID
:
623 case Type::ScalableVectorTyID
: {
624 VectorType
*PTy
= cast
<VectorType
>(Ty
);
625 ElementCount EC
= PTy
->getElementCount();
629 OS
<< EC
.getKnownMinValue() << " x ";
630 print(PTy
->getElementType(), OS
);
634 case Type::TypedPointerTyID
: {
635 TypedPointerType
*TPTy
= cast
<TypedPointerType
>(Ty
);
636 OS
<< "typedptr(" << *TPTy
->getElementType() << ", "
637 << TPTy
->getAddressSpace() << ")";
640 case Type::TargetExtTyID
:
641 TargetExtType
*TETy
= cast
<TargetExtType
>(Ty
);
643 printEscapedString(Ty
->getTargetExtName(), OS
);
645 for (Type
*Inner
: TETy
->type_params())
646 OS
<< ", " << *Inner
;
647 for (unsigned IntParam
: TETy
->int_params())
648 OS
<< ", " << IntParam
;
652 llvm_unreachable("Invalid TypeID");
655 void TypePrinting::printStructBody(StructType
*STy
, raw_ostream
&OS
) {
656 if (STy
->isOpaque()) {
664 if (STy
->getNumElements() == 0) {
669 for (Type
*Ty
: STy
->elements()) {
680 AbstractSlotTrackerStorage::~AbstractSlotTrackerStorage() = default;
684 //===----------------------------------------------------------------------===//
685 // SlotTracker Class: Enumerate slot numbers for unnamed values
686 //===----------------------------------------------------------------------===//
687 /// This class provides computation of slot numbers for LLVM Assembly writing.
689 class SlotTracker
: public AbstractSlotTrackerStorage
{
691 /// ValueMap - A mapping of Values to slot numbers.
692 using ValueMap
= DenseMap
<const Value
*, unsigned>;
695 /// TheModule - The module for which we are holding slot numbers.
696 const Module
* TheModule
;
698 /// TheFunction - The function for which we are holding slot numbers.
699 const Function
* TheFunction
= nullptr;
700 bool FunctionProcessed
= false;
701 bool ShouldInitializeAllMetadata
;
703 std::function
<void(AbstractSlotTrackerStorage
*, const Module
*, bool)>
705 std::function
<void(AbstractSlotTrackerStorage
*, const Function
*, bool)>
706 ProcessFunctionHookFn
;
708 /// The summary index for which we are holding slot numbers.
709 const ModuleSummaryIndex
*TheIndex
= nullptr;
711 /// mMap - The slot map for the module level data.
715 /// fMap - The slot map for the function level data.
719 /// mdnMap - Map for MDNodes.
720 DenseMap
<const MDNode
*, unsigned> mdnMap
;
721 unsigned mdnNext
= 0;
723 /// asMap - The slot map for attribute sets.
724 DenseMap
<AttributeSet
, unsigned> asMap
;
727 /// ModulePathMap - The slot map for Module paths used in the summary index.
728 StringMap
<unsigned> ModulePathMap
;
729 unsigned ModulePathNext
= 0;
731 /// GUIDMap - The slot map for GUIDs used in the summary index.
732 DenseMap
<GlobalValue::GUID
, unsigned> GUIDMap
;
733 unsigned GUIDNext
= 0;
735 /// TypeIdMap - The slot map for type ids used in the summary index.
736 StringMap
<unsigned> TypeIdMap
;
737 unsigned TypeIdNext
= 0;
739 /// TypeIdCompatibleVtableMap - The slot map for type compatible vtable ids
740 /// used in the summary index.
741 StringMap
<unsigned> TypeIdCompatibleVtableMap
;
742 unsigned TypeIdCompatibleVtableNext
= 0;
745 /// Construct from a module.
747 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
748 /// functions, giving correct numbering for metadata referenced only from
749 /// within a function (even if no functions have been initialized).
750 explicit SlotTracker(const Module
*M
,
751 bool ShouldInitializeAllMetadata
= false);
753 /// Construct from a function, starting out in incorp state.
755 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
756 /// functions, giving correct numbering for metadata referenced only from
757 /// within a function (even if no functions have been initialized).
758 explicit SlotTracker(const Function
*F
,
759 bool ShouldInitializeAllMetadata
= false);
761 /// Construct from a module summary index.
762 explicit SlotTracker(const ModuleSummaryIndex
*Index
);
764 SlotTracker(const SlotTracker
&) = delete;
765 SlotTracker
&operator=(const SlotTracker
&) = delete;
767 ~SlotTracker() = default;
770 std::function
<void(AbstractSlotTrackerStorage
*, const Module
*, bool)>);
771 void setProcessHook(std::function
<void(AbstractSlotTrackerStorage
*,
772 const Function
*, bool)>);
774 unsigned getNextMetadataSlot() override
{ return mdnNext
; }
776 void createMetadataSlot(const MDNode
*N
) override
;
778 /// Return the slot number of the specified value in it's type
779 /// plane. If something is not in the SlotTracker, return -1.
780 int getLocalSlot(const Value
*V
);
781 int getGlobalSlot(const GlobalValue
*V
);
782 int getMetadataSlot(const MDNode
*N
) override
;
783 int getAttributeGroupSlot(AttributeSet AS
);
784 int getModulePathSlot(StringRef Path
);
785 int getGUIDSlot(GlobalValue::GUID GUID
);
786 int getTypeIdSlot(StringRef Id
);
787 int getTypeIdCompatibleVtableSlot(StringRef Id
);
789 /// If you'd like to deal with a function instead of just a module, use
790 /// this method to get its data into the SlotTracker.
791 void incorporateFunction(const Function
*F
) {
793 FunctionProcessed
= false;
796 const Function
*getFunction() const { return TheFunction
; }
798 /// After calling incorporateFunction, use this method to remove the
799 /// most recently incorporated function from the SlotTracker. This
800 /// will reset the state of the machine back to just the module contents.
801 void purgeFunction();
803 /// MDNode map iterators.
804 using mdn_iterator
= DenseMap
<const MDNode
*, unsigned>::iterator
;
806 mdn_iterator
mdn_begin() { return mdnMap
.begin(); }
807 mdn_iterator
mdn_end() { return mdnMap
.end(); }
808 unsigned mdn_size() const { return mdnMap
.size(); }
809 bool mdn_empty() const { return mdnMap
.empty(); }
811 /// AttributeSet map iterators.
812 using as_iterator
= DenseMap
<AttributeSet
, unsigned>::iterator
;
814 as_iterator
as_begin() { return asMap
.begin(); }
815 as_iterator
as_end() { return asMap
.end(); }
816 unsigned as_size() const { return asMap
.size(); }
817 bool as_empty() const { return asMap
.empty(); }
819 /// GUID map iterators.
820 using guid_iterator
= DenseMap
<GlobalValue::GUID
, unsigned>::iterator
;
822 /// These functions do the actual initialization.
823 inline void initializeIfNeeded();
824 int initializeIndexIfNeeded();
826 // Implementation Details
828 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
829 void CreateModuleSlot(const GlobalValue
*V
);
831 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
832 void CreateMetadataSlot(const MDNode
*N
);
834 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
835 void CreateFunctionSlot(const Value
*V
);
837 /// Insert the specified AttributeSet into the slot table.
838 void CreateAttributeSetSlot(AttributeSet AS
);
840 inline void CreateModulePathSlot(StringRef Path
);
841 void CreateGUIDSlot(GlobalValue::GUID GUID
);
842 void CreateTypeIdSlot(StringRef Id
);
843 void CreateTypeIdCompatibleVtableSlot(StringRef Id
);
845 /// Add all of the module level global variables (and their initializers)
846 /// and function declarations, but not the contents of those functions.
847 void processModule();
848 // Returns number of allocated slots
851 /// Add all of the functions arguments, basic blocks, and instructions.
852 void processFunction();
854 /// Add the metadata directly attached to a GlobalObject.
855 void processGlobalObjectMetadata(const GlobalObject
&GO
);
857 /// Add all of the metadata from a function.
858 void processFunctionMetadata(const Function
&F
);
860 /// Add all of the metadata from an instruction.
861 void processInstructionMetadata(const Instruction
&I
);
863 /// Add all of the metadata from an instruction.
864 void processDPValueMetadata(const DPValue
&DPV
);
867 } // end namespace llvm
869 ModuleSlotTracker::ModuleSlotTracker(SlotTracker
&Machine
, const Module
*M
,
871 : M(M
), F(F
), Machine(&Machine
) {}
873 ModuleSlotTracker::ModuleSlotTracker(const Module
*M
,
874 bool ShouldInitializeAllMetadata
)
875 : ShouldCreateStorage(M
),
876 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata
), M(M
) {}
878 ModuleSlotTracker::~ModuleSlotTracker() = default;
880 SlotTracker
*ModuleSlotTracker::getMachine() {
881 if (!ShouldCreateStorage
)
884 ShouldCreateStorage
= false;
886 std::make_unique
<SlotTracker
>(M
, ShouldInitializeAllMetadata
);
887 Machine
= MachineStorage
.get();
888 if (ProcessModuleHookFn
)
889 Machine
->setProcessHook(ProcessModuleHookFn
);
890 if (ProcessFunctionHookFn
)
891 Machine
->setProcessHook(ProcessFunctionHookFn
);
895 void ModuleSlotTracker::incorporateFunction(const Function
&F
) {
896 // Using getMachine() may lazily create the slot tracker.
900 // Nothing to do if this is the right function already.
904 Machine
->purgeFunction();
905 Machine
->incorporateFunction(&F
);
909 int ModuleSlotTracker::getLocalSlot(const Value
*V
) {
910 assert(F
&& "No function incorporated");
911 return Machine
->getLocalSlot(V
);
914 void ModuleSlotTracker::setProcessHook(
915 std::function
<void(AbstractSlotTrackerStorage
*, const Module
*, bool)>
917 ProcessModuleHookFn
= Fn
;
920 void ModuleSlotTracker::setProcessHook(
921 std::function
<void(AbstractSlotTrackerStorage
*, const Function
*, bool)>
923 ProcessFunctionHookFn
= Fn
;
926 static SlotTracker
*createSlotTracker(const Value
*V
) {
927 if (const Argument
*FA
= dyn_cast
<Argument
>(V
))
928 return new SlotTracker(FA
->getParent());
930 if (const Instruction
*I
= dyn_cast
<Instruction
>(V
))
932 return new SlotTracker(I
->getParent()->getParent());
934 if (const BasicBlock
*BB
= dyn_cast
<BasicBlock
>(V
))
935 return new SlotTracker(BB
->getParent());
937 if (const GlobalVariable
*GV
= dyn_cast
<GlobalVariable
>(V
))
938 return new SlotTracker(GV
->getParent());
940 if (const GlobalAlias
*GA
= dyn_cast
<GlobalAlias
>(V
))
941 return new SlotTracker(GA
->getParent());
943 if (const GlobalIFunc
*GIF
= dyn_cast
<GlobalIFunc
>(V
))
944 return new SlotTracker(GIF
->getParent());
946 if (const Function
*Func
= dyn_cast
<Function
>(V
))
947 return new SlotTracker(Func
);
953 #define ST_DEBUG(X) dbgs() << X
958 // Module level constructor. Causes the contents of the Module (sans functions)
959 // to be added to the slot table.
960 SlotTracker::SlotTracker(const Module
*M
, bool ShouldInitializeAllMetadata
)
961 : TheModule(M
), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata
) {}
963 // Function level constructor. Causes the contents of the Module and the one
964 // function provided to be added to the slot table.
965 SlotTracker::SlotTracker(const Function
*F
, bool ShouldInitializeAllMetadata
)
966 : TheModule(F
? F
->getParent() : nullptr), TheFunction(F
),
967 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata
) {}
969 SlotTracker::SlotTracker(const ModuleSummaryIndex
*Index
)
970 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index
) {}
972 inline void SlotTracker::initializeIfNeeded() {
975 TheModule
= nullptr; ///< Prevent re-processing next time we're called.
978 if (TheFunction
&& !FunctionProcessed
)
982 int SlotTracker::initializeIndexIfNeeded() {
985 int NumSlots
= processIndex();
986 TheIndex
= nullptr; ///< Prevent re-processing next time we're called.
990 // Iterate through all the global variables, functions, and global
991 // variable initializers and create slots for them.
992 void SlotTracker::processModule() {
993 ST_DEBUG("begin processModule!\n");
995 // Add all of the unnamed global variables to the value table.
996 for (const GlobalVariable
&Var
: TheModule
->globals()) {
998 CreateModuleSlot(&Var
);
999 processGlobalObjectMetadata(Var
);
1000 auto Attrs
= Var
.getAttributes();
1001 if (Attrs
.hasAttributes())
1002 CreateAttributeSetSlot(Attrs
);
1005 for (const GlobalAlias
&A
: TheModule
->aliases()) {
1007 CreateModuleSlot(&A
);
1010 for (const GlobalIFunc
&I
: TheModule
->ifuncs()) {
1012 CreateModuleSlot(&I
);
1015 // Add metadata used by named metadata.
1016 for (const NamedMDNode
&NMD
: TheModule
->named_metadata()) {
1017 for (unsigned i
= 0, e
= NMD
.getNumOperands(); i
!= e
; ++i
)
1018 CreateMetadataSlot(NMD
.getOperand(i
));
1021 for (const Function
&F
: *TheModule
) {
1023 // Add all the unnamed functions to the table.
1024 CreateModuleSlot(&F
);
1026 if (ShouldInitializeAllMetadata
)
1027 processFunctionMetadata(F
);
1029 // Add all the function attributes to the table.
1030 // FIXME: Add attributes of other objects?
1031 AttributeSet FnAttrs
= F
.getAttributes().getFnAttrs();
1032 if (FnAttrs
.hasAttributes())
1033 CreateAttributeSetSlot(FnAttrs
);
1036 if (ProcessModuleHookFn
)
1037 ProcessModuleHookFn(this, TheModule
, ShouldInitializeAllMetadata
);
1039 ST_DEBUG("end processModule!\n");
1042 // Process the arguments, basic blocks, and instructions of a function.
1043 void SlotTracker::processFunction() {
1044 ST_DEBUG("begin processFunction!\n");
1047 // Process function metadata if it wasn't hit at the module-level.
1048 if (!ShouldInitializeAllMetadata
)
1049 processFunctionMetadata(*TheFunction
);
1051 // Add all the function arguments with no names.
1052 for(Function::const_arg_iterator AI
= TheFunction
->arg_begin(),
1053 AE
= TheFunction
->arg_end(); AI
!= AE
; ++AI
)
1055 CreateFunctionSlot(&*AI
);
1057 ST_DEBUG("Inserting Instructions:\n");
1059 // Add all of the basic blocks and instructions with no names.
1060 for (auto &BB
: *TheFunction
) {
1062 CreateFunctionSlot(&BB
);
1064 for (auto &I
: BB
) {
1065 if (!I
.getType()->isVoidTy() && !I
.hasName())
1066 CreateFunctionSlot(&I
);
1068 // We allow direct calls to any llvm.foo function here, because the
1069 // target may not be linked into the optimizer.
1070 if (const auto *Call
= dyn_cast
<CallBase
>(&I
)) {
1071 // Add all the call attributes to the table.
1072 AttributeSet Attrs
= Call
->getAttributes().getFnAttrs();
1073 if (Attrs
.hasAttributes())
1074 CreateAttributeSetSlot(Attrs
);
1079 if (ProcessFunctionHookFn
)
1080 ProcessFunctionHookFn(this, TheFunction
, ShouldInitializeAllMetadata
);
1082 FunctionProcessed
= true;
1084 ST_DEBUG("end processFunction!\n");
1087 // Iterate through all the GUID in the index and create slots for them.
1088 int SlotTracker::processIndex() {
1089 ST_DEBUG("begin processIndex!\n");
1092 // The first block of slots are just the module ids, which start at 0 and are
1093 // assigned consecutively. Since the StringMap iteration order isn't
1094 // guaranteed, order by path string before assigning slots.
1095 std::vector
<StringRef
> ModulePaths
;
1096 for (auto &[ModPath
, _
] : TheIndex
->modulePaths())
1097 ModulePaths
.push_back(ModPath
);
1098 llvm::sort(ModulePaths
.begin(), ModulePaths
.end());
1099 for (auto &ModPath
: ModulePaths
)
1100 CreateModulePathSlot(ModPath
);
1102 // Start numbering the GUIDs after the module ids.
1103 GUIDNext
= ModulePathNext
;
1105 for (auto &GlobalList
: *TheIndex
)
1106 CreateGUIDSlot(GlobalList
.first
);
1108 // Start numbering the TypeIdCompatibleVtables after the GUIDs.
1109 TypeIdCompatibleVtableNext
= GUIDNext
;
1110 for (auto &TId
: TheIndex
->typeIdCompatibleVtableMap())
1111 CreateTypeIdCompatibleVtableSlot(TId
.first
);
1113 // Start numbering the TypeIds after the TypeIdCompatibleVtables.
1114 TypeIdNext
= TypeIdCompatibleVtableNext
;
1115 for (const auto &TID
: TheIndex
->typeIds())
1116 CreateTypeIdSlot(TID
.second
.first
);
1118 ST_DEBUG("end processIndex!\n");
1122 void SlotTracker::processGlobalObjectMetadata(const GlobalObject
&GO
) {
1123 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
1124 GO
.getAllMetadata(MDs
);
1125 for (auto &MD
: MDs
)
1126 CreateMetadataSlot(MD
.second
);
1129 void SlotTracker::processFunctionMetadata(const Function
&F
) {
1130 processGlobalObjectMetadata(F
);
1131 for (auto &BB
: F
) {
1132 for (auto &I
: BB
) {
1133 for (const DPValue
&DPV
: I
.getDbgValueRange())
1134 processDPValueMetadata(DPV
);
1135 processInstructionMetadata(I
);
1140 void SlotTracker::processDPValueMetadata(const DPValue
&DPV
) {
1141 CreateMetadataSlot(DPV
.getVariable());
1142 CreateMetadataSlot(DPV
.getDebugLoc());
1143 if (DPV
.isDbgAssign()) {
1144 CreateMetadataSlot(DPV
.getAssignID());
1148 void SlotTracker::processInstructionMetadata(const Instruction
&I
) {
1149 // Process metadata used directly by intrinsics.
1150 if (const CallInst
*CI
= dyn_cast
<CallInst
>(&I
))
1151 if (Function
*F
= CI
->getCalledFunction())
1152 if (F
->isIntrinsic())
1153 for (auto &Op
: I
.operands())
1154 if (auto *V
= dyn_cast_or_null
<MetadataAsValue
>(Op
))
1155 if (MDNode
*N
= dyn_cast
<MDNode
>(V
->getMetadata()))
1156 CreateMetadataSlot(N
);
1158 // Process metadata attached to this instruction.
1159 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
1160 I
.getAllMetadata(MDs
);
1161 for (auto &MD
: MDs
)
1162 CreateMetadataSlot(MD
.second
);
1165 /// Clean up after incorporating a function. This is the only way to get out of
1166 /// the function incorporation state that affects get*Slot/Create*Slot. Function
1167 /// incorporation state is indicated by TheFunction != 0.
1168 void SlotTracker::purgeFunction() {
1169 ST_DEBUG("begin purgeFunction!\n");
1170 fMap
.clear(); // Simply discard the function level map
1171 TheFunction
= nullptr;
1172 FunctionProcessed
= false;
1173 ST_DEBUG("end purgeFunction!\n");
1176 /// getGlobalSlot - Get the slot number of a global value.
1177 int SlotTracker::getGlobalSlot(const GlobalValue
*V
) {
1178 // Check for uninitialized state and do lazy initialization.
1179 initializeIfNeeded();
1181 // Find the value in the module map
1182 ValueMap::iterator MI
= mMap
.find(V
);
1183 return MI
== mMap
.end() ? -1 : (int)MI
->second
;
1186 void SlotTracker::setProcessHook(
1187 std::function
<void(AbstractSlotTrackerStorage
*, const Module
*, bool)>
1189 ProcessModuleHookFn
= Fn
;
1192 void SlotTracker::setProcessHook(
1193 std::function
<void(AbstractSlotTrackerStorage
*, const Function
*, bool)>
1195 ProcessFunctionHookFn
= Fn
;
1198 /// getMetadataSlot - Get the slot number of a MDNode.
1199 void SlotTracker::createMetadataSlot(const MDNode
*N
) { CreateMetadataSlot(N
); }
1201 /// getMetadataSlot - Get the slot number of a MDNode.
1202 int SlotTracker::getMetadataSlot(const MDNode
*N
) {
1203 // Check for uninitialized state and do lazy initialization.
1204 initializeIfNeeded();
1206 // Find the MDNode in the module map
1207 mdn_iterator MI
= mdnMap
.find(N
);
1208 return MI
== mdnMap
.end() ? -1 : (int)MI
->second
;
1211 /// getLocalSlot - Get the slot number for a value that is local to a function.
1212 int SlotTracker::getLocalSlot(const Value
*V
) {
1213 assert(!isa
<Constant
>(V
) && "Can't get a constant or global slot with this!");
1215 // Check for uninitialized state and do lazy initialization.
1216 initializeIfNeeded();
1218 ValueMap::iterator FI
= fMap
.find(V
);
1219 return FI
== fMap
.end() ? -1 : (int)FI
->second
;
1222 int SlotTracker::getAttributeGroupSlot(AttributeSet AS
) {
1223 // Check for uninitialized state and do lazy initialization.
1224 initializeIfNeeded();
1226 // Find the AttributeSet in the module map.
1227 as_iterator AI
= asMap
.find(AS
);
1228 return AI
== asMap
.end() ? -1 : (int)AI
->second
;
1231 int SlotTracker::getModulePathSlot(StringRef Path
) {
1232 // Check for uninitialized state and do lazy initialization.
1233 initializeIndexIfNeeded();
1235 // Find the Module path in the map
1236 auto I
= ModulePathMap
.find(Path
);
1237 return I
== ModulePathMap
.end() ? -1 : (int)I
->second
;
1240 int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID
) {
1241 // Check for uninitialized state and do lazy initialization.
1242 initializeIndexIfNeeded();
1244 // Find the GUID in the map
1245 guid_iterator I
= GUIDMap
.find(GUID
);
1246 return I
== GUIDMap
.end() ? -1 : (int)I
->second
;
1249 int SlotTracker::getTypeIdSlot(StringRef Id
) {
1250 // Check for uninitialized state and do lazy initialization.
1251 initializeIndexIfNeeded();
1253 // Find the TypeId string in the map
1254 auto I
= TypeIdMap
.find(Id
);
1255 return I
== TypeIdMap
.end() ? -1 : (int)I
->second
;
1258 int SlotTracker::getTypeIdCompatibleVtableSlot(StringRef Id
) {
1259 // Check for uninitialized state and do lazy initialization.
1260 initializeIndexIfNeeded();
1262 // Find the TypeIdCompatibleVtable string in the map
1263 auto I
= TypeIdCompatibleVtableMap
.find(Id
);
1264 return I
== TypeIdCompatibleVtableMap
.end() ? -1 : (int)I
->second
;
1267 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1268 void SlotTracker::CreateModuleSlot(const GlobalValue
*V
) {
1269 assert(V
&& "Can't insert a null Value into SlotTracker!");
1270 assert(!V
->getType()->isVoidTy() && "Doesn't need a slot!");
1271 assert(!V
->hasName() && "Doesn't need a slot!");
1273 unsigned DestSlot
= mNext
++;
1276 ST_DEBUG(" Inserting value [" << V
->getType() << "] = " << V
<< " slot=" <<
1278 // G = Global, F = Function, A = Alias, I = IFunc, o = other
1279 ST_DEBUG((isa
<GlobalVariable
>(V
) ? 'G' :
1280 (isa
<Function
>(V
) ? 'F' :
1281 (isa
<GlobalAlias
>(V
) ? 'A' :
1282 (isa
<GlobalIFunc
>(V
) ? 'I' : 'o')))) << "]\n");
1285 /// CreateSlot - Create a new slot for the specified value if it has no name.
1286 void SlotTracker::CreateFunctionSlot(const Value
*V
) {
1287 assert(!V
->getType()->isVoidTy() && !V
->hasName() && "Doesn't need a slot!");
1289 unsigned DestSlot
= fNext
++;
1292 // G = Global, F = Function, o = other
1293 ST_DEBUG(" Inserting value [" << V
->getType() << "] = " << V
<< " slot=" <<
1294 DestSlot
<< " [o]\n");
1297 /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1298 void SlotTracker::CreateMetadataSlot(const MDNode
*N
) {
1299 assert(N
&& "Can't insert a null Value into SlotTracker!");
1301 // Don't make slots for DIExpressions. We just print them inline everywhere.
1302 if (isa
<DIExpression
>(N
))
1305 unsigned DestSlot
= mdnNext
;
1306 if (!mdnMap
.insert(std::make_pair(N
, DestSlot
)).second
)
1310 // Recursively add any MDNodes referenced by operands.
1311 for (unsigned i
= 0, e
= N
->getNumOperands(); i
!= e
; ++i
)
1312 if (const MDNode
*Op
= dyn_cast_or_null
<MDNode
>(N
->getOperand(i
)))
1313 CreateMetadataSlot(Op
);
1316 void SlotTracker::CreateAttributeSetSlot(AttributeSet AS
) {
1317 assert(AS
.hasAttributes() && "Doesn't need a slot!");
1319 as_iterator I
= asMap
.find(AS
);
1320 if (I
!= asMap
.end())
1323 unsigned DestSlot
= asNext
++;
1324 asMap
[AS
] = DestSlot
;
1327 /// Create a new slot for the specified Module
1328 void SlotTracker::CreateModulePathSlot(StringRef Path
) {
1329 ModulePathMap
[Path
] = ModulePathNext
++;
1332 /// Create a new slot for the specified GUID
1333 void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID
) {
1334 GUIDMap
[GUID
] = GUIDNext
++;
1337 /// Create a new slot for the specified Id
1338 void SlotTracker::CreateTypeIdSlot(StringRef Id
) {
1339 TypeIdMap
[Id
] = TypeIdNext
++;
1342 /// Create a new slot for the specified Id
1343 void SlotTracker::CreateTypeIdCompatibleVtableSlot(StringRef Id
) {
1344 TypeIdCompatibleVtableMap
[Id
] = TypeIdCompatibleVtableNext
++;
1348 /// Common instances used by most of the printer functions.
1349 struct AsmWriterContext
{
1350 TypePrinting
*TypePrinter
= nullptr;
1351 SlotTracker
*Machine
= nullptr;
1352 const Module
*Context
= nullptr;
1354 AsmWriterContext(TypePrinting
*TP
, SlotTracker
*ST
, const Module
*M
= nullptr)
1355 : TypePrinter(TP
), Machine(ST
), Context(M
) {}
1357 static AsmWriterContext
&getEmpty() {
1358 static AsmWriterContext
EmptyCtx(nullptr, nullptr);
1362 /// A callback that will be triggered when the underlying printer
1363 /// prints a Metadata as operand.
1364 virtual void onWriteMetadataAsOperand(const Metadata
*) {}
1366 virtual ~AsmWriterContext() = default;
1368 } // end anonymous namespace
1370 //===----------------------------------------------------------------------===//
1371 // AsmWriter Implementation
1372 //===----------------------------------------------------------------------===//
1374 static void WriteAsOperandInternal(raw_ostream
&Out
, const Value
*V
,
1375 AsmWriterContext
&WriterCtx
);
1377 static void WriteAsOperandInternal(raw_ostream
&Out
, const Metadata
*MD
,
1378 AsmWriterContext
&WriterCtx
,
1379 bool FromValue
= false);
1381 static void WriteOptimizationInfo(raw_ostream
&Out
, const User
*U
) {
1382 if (const FPMathOperator
*FPO
= dyn_cast
<const FPMathOperator
>(U
))
1383 Out
<< FPO
->getFastMathFlags();
1385 if (const OverflowingBinaryOperator
*OBO
=
1386 dyn_cast
<OverflowingBinaryOperator
>(U
)) {
1387 if (OBO
->hasNoUnsignedWrap())
1389 if (OBO
->hasNoSignedWrap())
1391 } else if (const PossiblyExactOperator
*Div
=
1392 dyn_cast
<PossiblyExactOperator
>(U
)) {
1395 } else if (const PossiblyDisjointInst
*PDI
=
1396 dyn_cast
<PossiblyDisjointInst
>(U
)) {
1397 if (PDI
->isDisjoint())
1399 } else if (const GEPOperator
*GEP
= dyn_cast
<GEPOperator
>(U
)) {
1400 if (GEP
->isInBounds())
1402 } else if (const auto *NNI
= dyn_cast
<PossiblyNonNegInst
>(U
)) {
1403 if (NNI
->hasNonNeg())
1408 static void WriteConstantInternal(raw_ostream
&Out
, const Constant
*CV
,
1409 AsmWriterContext
&WriterCtx
) {
1410 if (const ConstantInt
*CI
= dyn_cast
<ConstantInt
>(CV
)) {
1411 if (CI
->getType()->isIntegerTy(1)) {
1412 Out
<< (CI
->getZExtValue() ? "true" : "false");
1415 Out
<< CI
->getValue();
1419 if (const ConstantFP
*CFP
= dyn_cast
<ConstantFP
>(CV
)) {
1420 const APFloat
&APF
= CFP
->getValueAPF();
1421 if (&APF
.getSemantics() == &APFloat::IEEEsingle() ||
1422 &APF
.getSemantics() == &APFloat::IEEEdouble()) {
1423 // We would like to output the FP constant value in exponential notation,
1424 // but we cannot do this if doing so will lose precision. Check here to
1425 // make sure that we only output it in exponential format if we can parse
1426 // the value back and get the same value.
1429 bool isDouble
= &APF
.getSemantics() == &APFloat::IEEEdouble();
1430 bool isInf
= APF
.isInfinity();
1431 bool isNaN
= APF
.isNaN();
1432 if (!isInf
&& !isNaN
) {
1433 double Val
= APF
.convertToDouble();
1434 SmallString
<128> StrVal
;
1435 APF
.toString(StrVal
, 6, 0, false);
1436 // Check to make sure that the stringized number is not some string like
1437 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1438 // that the string matches the "[-+]?[0-9]" regex.
1440 assert((isDigit(StrVal
[0]) || ((StrVal
[0] == '-' || StrVal
[0] == '+') &&
1441 isDigit(StrVal
[1]))) &&
1442 "[-+]?[0-9] regex does not match!");
1443 // Reparse stringized version!
1444 if (APFloat(APFloat::IEEEdouble(), StrVal
).convertToDouble() == Val
) {
1449 // Otherwise we could not reparse it to exactly the same value, so we must
1450 // output the string in hexadecimal format! Note that loading and storing
1451 // floating point types changes the bits of NaNs on some hosts, notably
1452 // x86, so we must not use these types.
1453 static_assert(sizeof(double) == sizeof(uint64_t),
1454 "assuming that double is 64 bits!");
1456 // Floats are represented in ASCII IR as double, convert.
1457 // FIXME: We should allow 32-bit hex float and remove this.
1459 // A signaling NaN is quieted on conversion, so we need to recreate the
1460 // expected value after convert (quiet bit of the payload is clear).
1461 bool IsSNAN
= apf
.isSignaling();
1462 apf
.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven
,
1465 APInt Payload
= apf
.bitcastToAPInt();
1466 apf
= APFloat::getSNaN(APFloat::IEEEdouble(), apf
.isNegative(),
1470 Out
<< format_hex(apf
.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
1474 // Either half, bfloat or some form of long double.
1475 // These appear as a magic letter identifying the type, then a
1476 // fixed number of hex digits.
1478 APInt API
= APF
.bitcastToAPInt();
1479 if (&APF
.getSemantics() == &APFloat::x87DoubleExtended()) {
1481 Out
<< format_hex_no_prefix(API
.getHiBits(16).getZExtValue(), 4,
1483 Out
<< format_hex_no_prefix(API
.getLoBits(64).getZExtValue(), 16,
1486 } else if (&APF
.getSemantics() == &APFloat::IEEEquad()) {
1488 Out
<< format_hex_no_prefix(API
.getLoBits(64).getZExtValue(), 16,
1490 Out
<< format_hex_no_prefix(API
.getHiBits(64).getZExtValue(), 16,
1492 } else if (&APF
.getSemantics() == &APFloat::PPCDoubleDouble()) {
1494 Out
<< format_hex_no_prefix(API
.getLoBits(64).getZExtValue(), 16,
1496 Out
<< format_hex_no_prefix(API
.getHiBits(64).getZExtValue(), 16,
1498 } else if (&APF
.getSemantics() == &APFloat::IEEEhalf()) {
1500 Out
<< format_hex_no_prefix(API
.getZExtValue(), 4,
1502 } else if (&APF
.getSemantics() == &APFloat::BFloat()) {
1504 Out
<< format_hex_no_prefix(API
.getZExtValue(), 4,
1507 llvm_unreachable("Unsupported floating point type");
1511 if (isa
<ConstantAggregateZero
>(CV
) || isa
<ConstantTargetNone
>(CV
)) {
1512 Out
<< "zeroinitializer";
1516 if (const BlockAddress
*BA
= dyn_cast
<BlockAddress
>(CV
)) {
1517 Out
<< "blockaddress(";
1518 WriteAsOperandInternal(Out
, BA
->getFunction(), WriterCtx
);
1520 WriteAsOperandInternal(Out
, BA
->getBasicBlock(), WriterCtx
);
1525 if (const auto *Equiv
= dyn_cast
<DSOLocalEquivalent
>(CV
)) {
1526 Out
<< "dso_local_equivalent ";
1527 WriteAsOperandInternal(Out
, Equiv
->getGlobalValue(), WriterCtx
);
1531 if (const auto *NC
= dyn_cast
<NoCFIValue
>(CV
)) {
1533 WriteAsOperandInternal(Out
, NC
->getGlobalValue(), WriterCtx
);
1537 if (const ConstantArray
*CA
= dyn_cast
<ConstantArray
>(CV
)) {
1538 Type
*ETy
= CA
->getType()->getElementType();
1540 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1542 WriteAsOperandInternal(Out
, CA
->getOperand(0), WriterCtx
);
1543 for (unsigned i
= 1, e
= CA
->getNumOperands(); i
!= e
; ++i
) {
1545 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1547 WriteAsOperandInternal(Out
, CA
->getOperand(i
), WriterCtx
);
1553 if (const ConstantDataArray
*CA
= dyn_cast
<ConstantDataArray
>(CV
)) {
1554 // As a special case, print the array as a string if it is an array of
1555 // i8 with ConstantInt values.
1556 if (CA
->isString()) {
1558 printEscapedString(CA
->getAsString(), Out
);
1563 Type
*ETy
= CA
->getType()->getElementType();
1565 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1567 WriteAsOperandInternal(Out
, CA
->getElementAsConstant(0), WriterCtx
);
1568 for (unsigned i
= 1, e
= CA
->getNumElements(); i
!= e
; ++i
) {
1570 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1572 WriteAsOperandInternal(Out
, CA
->getElementAsConstant(i
), WriterCtx
);
1578 if (const ConstantStruct
*CS
= dyn_cast
<ConstantStruct
>(CV
)) {
1579 if (CS
->getType()->isPacked())
1582 unsigned N
= CS
->getNumOperands();
1585 WriterCtx
.TypePrinter
->print(CS
->getOperand(0)->getType(), Out
);
1588 WriteAsOperandInternal(Out
, CS
->getOperand(0), WriterCtx
);
1590 for (unsigned i
= 1; i
< N
; i
++) {
1592 WriterCtx
.TypePrinter
->print(CS
->getOperand(i
)->getType(), Out
);
1595 WriteAsOperandInternal(Out
, CS
->getOperand(i
), WriterCtx
);
1601 if (CS
->getType()->isPacked())
1606 if (isa
<ConstantVector
>(CV
) || isa
<ConstantDataVector
>(CV
)) {
1607 auto *CVVTy
= cast
<FixedVectorType
>(CV
->getType());
1608 Type
*ETy
= CVVTy
->getElementType();
1610 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1612 WriteAsOperandInternal(Out
, CV
->getAggregateElement(0U), WriterCtx
);
1613 for (unsigned i
= 1, e
= CVVTy
->getNumElements(); i
!= e
; ++i
) {
1615 WriterCtx
.TypePrinter
->print(ETy
, Out
);
1617 WriteAsOperandInternal(Out
, CV
->getAggregateElement(i
), WriterCtx
);
1623 if (isa
<ConstantPointerNull
>(CV
)) {
1628 if (isa
<ConstantTokenNone
>(CV
)) {
1633 if (isa
<PoisonValue
>(CV
)) {
1638 if (isa
<UndefValue
>(CV
)) {
1643 if (const ConstantExpr
*CE
= dyn_cast
<ConstantExpr
>(CV
)) {
1644 Out
<< CE
->getOpcodeName();
1645 WriteOptimizationInfo(Out
, CE
);
1646 if (CE
->isCompare())
1647 Out
<< ' ' << static_cast<CmpInst::Predicate
>(CE
->getPredicate());
1650 std::optional
<unsigned> InRangeOp
;
1651 if (const GEPOperator
*GEP
= dyn_cast
<GEPOperator
>(CE
)) {
1652 WriterCtx
.TypePrinter
->print(GEP
->getSourceElementType(), Out
);
1654 InRangeOp
= GEP
->getInRangeIndex();
1659 for (User::const_op_iterator OI
=CE
->op_begin(); OI
!= CE
->op_end(); ++OI
) {
1660 if (InRangeOp
&& unsigned(OI
- CE
->op_begin()) == *InRangeOp
)
1662 WriterCtx
.TypePrinter
->print((*OI
)->getType(), Out
);
1664 WriteAsOperandInternal(Out
, *OI
, WriterCtx
);
1665 if (OI
+1 != CE
->op_end())
1671 WriterCtx
.TypePrinter
->print(CE
->getType(), Out
);
1674 if (CE
->getOpcode() == Instruction::ShuffleVector
)
1675 PrintShuffleMask(Out
, CE
->getType(), CE
->getShuffleMask());
1681 Out
<< "<placeholder or erroneous Constant>";
1684 static void writeMDTuple(raw_ostream
&Out
, const MDTuple
*Node
,
1685 AsmWriterContext
&WriterCtx
) {
1687 for (unsigned mi
= 0, me
= Node
->getNumOperands(); mi
!= me
; ++mi
) {
1688 const Metadata
*MD
= Node
->getOperand(mi
);
1691 else if (auto *MDV
= dyn_cast
<ValueAsMetadata
>(MD
)) {
1692 Value
*V
= MDV
->getValue();
1693 WriterCtx
.TypePrinter
->print(V
->getType(), Out
);
1695 WriteAsOperandInternal(Out
, V
, WriterCtx
);
1697 WriteAsOperandInternal(Out
, MD
, WriterCtx
);
1698 WriterCtx
.onWriteMetadataAsOperand(MD
);
1709 struct FieldSeparator
{
1713 FieldSeparator(const char *Sep
= ", ") : Sep(Sep
) {}
1716 raw_ostream
&operator<<(raw_ostream
&OS
, FieldSeparator
&FS
) {
1721 return OS
<< FS
.Sep
;
1724 struct MDFieldPrinter
{
1727 AsmWriterContext
&WriterCtx
;
1729 explicit MDFieldPrinter(raw_ostream
&Out
)
1730 : Out(Out
), WriterCtx(AsmWriterContext::getEmpty()) {}
1731 MDFieldPrinter(raw_ostream
&Out
, AsmWriterContext
&Ctx
)
1732 : Out(Out
), WriterCtx(Ctx
) {}
1734 void printTag(const DINode
*N
);
1735 void printMacinfoType(const DIMacroNode
*N
);
1736 void printChecksum(const DIFile::ChecksumInfo
<StringRef
> &N
);
1737 void printString(StringRef Name
, StringRef Value
,
1738 bool ShouldSkipEmpty
= true);
1739 void printMetadata(StringRef Name
, const Metadata
*MD
,
1740 bool ShouldSkipNull
= true);
1741 template <class IntTy
>
1742 void printInt(StringRef Name
, IntTy Int
, bool ShouldSkipZero
= true);
1743 void printAPInt(StringRef Name
, const APInt
&Int
, bool IsUnsigned
,
1744 bool ShouldSkipZero
);
1745 void printBool(StringRef Name
, bool Value
,
1746 std::optional
<bool> Default
= std::nullopt
);
1747 void printDIFlags(StringRef Name
, DINode::DIFlags Flags
);
1748 void printDISPFlags(StringRef Name
, DISubprogram::DISPFlags Flags
);
1749 template <class IntTy
, class Stringifier
>
1750 void printDwarfEnum(StringRef Name
, IntTy Value
, Stringifier toString
,
1751 bool ShouldSkipZero
= true);
1752 void printEmissionKind(StringRef Name
, DICompileUnit::DebugEmissionKind EK
);
1753 void printNameTableKind(StringRef Name
,
1754 DICompileUnit::DebugNameTableKind NTK
);
1757 } // end anonymous namespace
1759 void MDFieldPrinter::printTag(const DINode
*N
) {
1760 Out
<< FS
<< "tag: ";
1761 auto Tag
= dwarf::TagString(N
->getTag());
1768 void MDFieldPrinter::printMacinfoType(const DIMacroNode
*N
) {
1769 Out
<< FS
<< "type: ";
1770 auto Type
= dwarf::MacinfoString(N
->getMacinfoType());
1774 Out
<< N
->getMacinfoType();
1777 void MDFieldPrinter::printChecksum(
1778 const DIFile::ChecksumInfo
<StringRef
> &Checksum
) {
1779 Out
<< FS
<< "checksumkind: " << Checksum
.getKindAsString();
1780 printString("checksum", Checksum
.Value
, /* ShouldSkipEmpty */ false);
1783 void MDFieldPrinter::printString(StringRef Name
, StringRef Value
,
1784 bool ShouldSkipEmpty
) {
1785 if (ShouldSkipEmpty
&& Value
.empty())
1788 Out
<< FS
<< Name
<< ": \"";
1789 printEscapedString(Value
, Out
);
1793 static void writeMetadataAsOperand(raw_ostream
&Out
, const Metadata
*MD
,
1794 AsmWriterContext
&WriterCtx
) {
1799 WriteAsOperandInternal(Out
, MD
, WriterCtx
);
1800 WriterCtx
.onWriteMetadataAsOperand(MD
);
1803 void MDFieldPrinter::printMetadata(StringRef Name
, const Metadata
*MD
,
1804 bool ShouldSkipNull
) {
1805 if (ShouldSkipNull
&& !MD
)
1808 Out
<< FS
<< Name
<< ": ";
1809 writeMetadataAsOperand(Out
, MD
, WriterCtx
);
1812 template <class IntTy
>
1813 void MDFieldPrinter::printInt(StringRef Name
, IntTy Int
, bool ShouldSkipZero
) {
1814 if (ShouldSkipZero
&& !Int
)
1817 Out
<< FS
<< Name
<< ": " << Int
;
1820 void MDFieldPrinter::printAPInt(StringRef Name
, const APInt
&Int
,
1821 bool IsUnsigned
, bool ShouldSkipZero
) {
1822 if (ShouldSkipZero
&& Int
.isZero())
1825 Out
<< FS
<< Name
<< ": ";
1826 Int
.print(Out
, !IsUnsigned
);
1829 void MDFieldPrinter::printBool(StringRef Name
, bool Value
,
1830 std::optional
<bool> Default
) {
1831 if (Default
&& Value
== *Default
)
1833 Out
<< FS
<< Name
<< ": " << (Value
? "true" : "false");
1836 void MDFieldPrinter::printDIFlags(StringRef Name
, DINode::DIFlags Flags
) {
1840 Out
<< FS
<< Name
<< ": ";
1842 SmallVector
<DINode::DIFlags
, 8> SplitFlags
;
1843 auto Extra
= DINode::splitFlags(Flags
, SplitFlags
);
1845 FieldSeparator
FlagsFS(" | ");
1846 for (auto F
: SplitFlags
) {
1847 auto StringF
= DINode::getFlagString(F
);
1848 assert(!StringF
.empty() && "Expected valid flag");
1849 Out
<< FlagsFS
<< StringF
;
1851 if (Extra
|| SplitFlags
.empty())
1852 Out
<< FlagsFS
<< Extra
;
1855 void MDFieldPrinter::printDISPFlags(StringRef Name
,
1856 DISubprogram::DISPFlags Flags
) {
1857 // Always print this field, because no flags in the IR at all will be
1858 // interpreted as old-style isDefinition: true.
1859 Out
<< FS
<< Name
<< ": ";
1866 SmallVector
<DISubprogram::DISPFlags
, 8> SplitFlags
;
1867 auto Extra
= DISubprogram::splitFlags(Flags
, SplitFlags
);
1869 FieldSeparator
FlagsFS(" | ");
1870 for (auto F
: SplitFlags
) {
1871 auto StringF
= DISubprogram::getFlagString(F
);
1872 assert(!StringF
.empty() && "Expected valid flag");
1873 Out
<< FlagsFS
<< StringF
;
1875 if (Extra
|| SplitFlags
.empty())
1876 Out
<< FlagsFS
<< Extra
;
1879 void MDFieldPrinter::printEmissionKind(StringRef Name
,
1880 DICompileUnit::DebugEmissionKind EK
) {
1881 Out
<< FS
<< Name
<< ": " << DICompileUnit::emissionKindString(EK
);
1884 void MDFieldPrinter::printNameTableKind(StringRef Name
,
1885 DICompileUnit::DebugNameTableKind NTK
) {
1886 if (NTK
== DICompileUnit::DebugNameTableKind::Default
)
1888 Out
<< FS
<< Name
<< ": " << DICompileUnit::nameTableKindString(NTK
);
1891 template <class IntTy
, class Stringifier
>
1892 void MDFieldPrinter::printDwarfEnum(StringRef Name
, IntTy Value
,
1893 Stringifier toString
, bool ShouldSkipZero
) {
1897 Out
<< FS
<< Name
<< ": ";
1898 auto S
= toString(Value
);
1905 static void writeGenericDINode(raw_ostream
&Out
, const GenericDINode
*N
,
1906 AsmWriterContext
&WriterCtx
) {
1907 Out
<< "!GenericDINode(";
1908 MDFieldPrinter
Printer(Out
, WriterCtx
);
1909 Printer
.printTag(N
);
1910 Printer
.printString("header", N
->getHeader());
1911 if (N
->getNumDwarfOperands()) {
1912 Out
<< Printer
.FS
<< "operands: {";
1914 for (auto &I
: N
->dwarf_operands()) {
1916 writeMetadataAsOperand(Out
, I
, WriterCtx
);
1923 static void writeDILocation(raw_ostream
&Out
, const DILocation
*DL
,
1924 AsmWriterContext
&WriterCtx
) {
1925 Out
<< "!DILocation(";
1926 MDFieldPrinter
Printer(Out
, WriterCtx
);
1927 // Always output the line, since 0 is a relevant and important value for it.
1928 Printer
.printInt("line", DL
->getLine(), /* ShouldSkipZero */ false);
1929 Printer
.printInt("column", DL
->getColumn());
1930 Printer
.printMetadata("scope", DL
->getRawScope(), /* ShouldSkipNull */ false);
1931 Printer
.printMetadata("inlinedAt", DL
->getRawInlinedAt());
1932 Printer
.printBool("isImplicitCode", DL
->isImplicitCode(),
1933 /* Default */ false);
1937 static void writeDIAssignID(raw_ostream
&Out
, const DIAssignID
*DL
,
1938 AsmWriterContext
&WriterCtx
) {
1939 Out
<< "!DIAssignID()";
1940 MDFieldPrinter
Printer(Out
, WriterCtx
);
1943 static void writeDISubrange(raw_ostream
&Out
, const DISubrange
*N
,
1944 AsmWriterContext
&WriterCtx
) {
1945 Out
<< "!DISubrange(";
1946 MDFieldPrinter
Printer(Out
, WriterCtx
);
1948 auto *Count
= N
->getRawCountNode();
1949 if (auto *CE
= dyn_cast_or_null
<ConstantAsMetadata
>(Count
)) {
1950 auto *CV
= cast
<ConstantInt
>(CE
->getValue());
1951 Printer
.printInt("count", CV
->getSExtValue(),
1952 /* ShouldSkipZero */ false);
1954 Printer
.printMetadata("count", Count
, /*ShouldSkipNull */ true);
1956 // A lowerBound of constant 0 should not be skipped, since it is different
1957 // from an unspecified lower bound (= nullptr).
1958 auto *LBound
= N
->getRawLowerBound();
1959 if (auto *LE
= dyn_cast_or_null
<ConstantAsMetadata
>(LBound
)) {
1960 auto *LV
= cast
<ConstantInt
>(LE
->getValue());
1961 Printer
.printInt("lowerBound", LV
->getSExtValue(),
1962 /* ShouldSkipZero */ false);
1964 Printer
.printMetadata("lowerBound", LBound
, /*ShouldSkipNull */ true);
1966 auto *UBound
= N
->getRawUpperBound();
1967 if (auto *UE
= dyn_cast_or_null
<ConstantAsMetadata
>(UBound
)) {
1968 auto *UV
= cast
<ConstantInt
>(UE
->getValue());
1969 Printer
.printInt("upperBound", UV
->getSExtValue(),
1970 /* ShouldSkipZero */ false);
1972 Printer
.printMetadata("upperBound", UBound
, /*ShouldSkipNull */ true);
1974 auto *Stride
= N
->getRawStride();
1975 if (auto *SE
= dyn_cast_or_null
<ConstantAsMetadata
>(Stride
)) {
1976 auto *SV
= cast
<ConstantInt
>(SE
->getValue());
1977 Printer
.printInt("stride", SV
->getSExtValue(), /* ShouldSkipZero */ false);
1979 Printer
.printMetadata("stride", Stride
, /*ShouldSkipNull */ true);
1984 static void writeDIGenericSubrange(raw_ostream
&Out
, const DIGenericSubrange
*N
,
1985 AsmWriterContext
&WriterCtx
) {
1986 Out
<< "!DIGenericSubrange(";
1987 MDFieldPrinter
Printer(Out
, WriterCtx
);
1989 auto IsConstant
= [&](Metadata
*Bound
) -> bool {
1990 if (auto *BE
= dyn_cast_or_null
<DIExpression
>(Bound
)) {
1991 return BE
->isConstant() &&
1992 DIExpression::SignedOrUnsignedConstant::SignedConstant
==
1998 auto GetConstant
= [&](Metadata
*Bound
) -> int64_t {
1999 assert(IsConstant(Bound
) && "Expected constant");
2000 auto *BE
= dyn_cast_or_null
<DIExpression
>(Bound
);
2001 return static_cast<int64_t>(BE
->getElement(1));
2004 auto *Count
= N
->getRawCountNode();
2005 if (IsConstant(Count
))
2006 Printer
.printInt("count", GetConstant(Count
),
2007 /* ShouldSkipZero */ false);
2009 Printer
.printMetadata("count", Count
, /*ShouldSkipNull */ true);
2011 auto *LBound
= N
->getRawLowerBound();
2012 if (IsConstant(LBound
))
2013 Printer
.printInt("lowerBound", GetConstant(LBound
),
2014 /* ShouldSkipZero */ false);
2016 Printer
.printMetadata("lowerBound", LBound
, /*ShouldSkipNull */ true);
2018 auto *UBound
= N
->getRawUpperBound();
2019 if (IsConstant(UBound
))
2020 Printer
.printInt("upperBound", GetConstant(UBound
),
2021 /* ShouldSkipZero */ false);
2023 Printer
.printMetadata("upperBound", UBound
, /*ShouldSkipNull */ true);
2025 auto *Stride
= N
->getRawStride();
2026 if (IsConstant(Stride
))
2027 Printer
.printInt("stride", GetConstant(Stride
),
2028 /* ShouldSkipZero */ false);
2030 Printer
.printMetadata("stride", Stride
, /*ShouldSkipNull */ true);
2035 static void writeDIEnumerator(raw_ostream
&Out
, const DIEnumerator
*N
,
2036 AsmWriterContext
&) {
2037 Out
<< "!DIEnumerator(";
2038 MDFieldPrinter
Printer(Out
);
2039 Printer
.printString("name", N
->getName(), /* ShouldSkipEmpty */ false);
2040 Printer
.printAPInt("value", N
->getValue(), N
->isUnsigned(),
2041 /*ShouldSkipZero=*/false);
2042 if (N
->isUnsigned())
2043 Printer
.printBool("isUnsigned", true);
2047 static void writeDIBasicType(raw_ostream
&Out
, const DIBasicType
*N
,
2048 AsmWriterContext
&) {
2049 Out
<< "!DIBasicType(";
2050 MDFieldPrinter
Printer(Out
);
2051 if (N
->getTag() != dwarf::DW_TAG_base_type
)
2052 Printer
.printTag(N
);
2053 Printer
.printString("name", N
->getName());
2054 Printer
.printInt("size", N
->getSizeInBits());
2055 Printer
.printInt("align", N
->getAlignInBits());
2056 Printer
.printDwarfEnum("encoding", N
->getEncoding(),
2057 dwarf::AttributeEncodingString
);
2058 Printer
.printDIFlags("flags", N
->getFlags());
2062 static void writeDIStringType(raw_ostream
&Out
, const DIStringType
*N
,
2063 AsmWriterContext
&WriterCtx
) {
2064 Out
<< "!DIStringType(";
2065 MDFieldPrinter
Printer(Out
, WriterCtx
);
2066 if (N
->getTag() != dwarf::DW_TAG_string_type
)
2067 Printer
.printTag(N
);
2068 Printer
.printString("name", N
->getName());
2069 Printer
.printMetadata("stringLength", N
->getRawStringLength());
2070 Printer
.printMetadata("stringLengthExpression", N
->getRawStringLengthExp());
2071 Printer
.printMetadata("stringLocationExpression",
2072 N
->getRawStringLocationExp());
2073 Printer
.printInt("size", N
->getSizeInBits());
2074 Printer
.printInt("align", N
->getAlignInBits());
2075 Printer
.printDwarfEnum("encoding", N
->getEncoding(),
2076 dwarf::AttributeEncodingString
);
2080 static void writeDIDerivedType(raw_ostream
&Out
, const DIDerivedType
*N
,
2081 AsmWriterContext
&WriterCtx
) {
2082 Out
<< "!DIDerivedType(";
2083 MDFieldPrinter
Printer(Out
, WriterCtx
);
2084 Printer
.printTag(N
);
2085 Printer
.printString("name", N
->getName());
2086 Printer
.printMetadata("scope", N
->getRawScope());
2087 Printer
.printMetadata("file", N
->getRawFile());
2088 Printer
.printInt("line", N
->getLine());
2089 Printer
.printMetadata("baseType", N
->getRawBaseType(),
2090 /* ShouldSkipNull */ false);
2091 Printer
.printInt("size", N
->getSizeInBits());
2092 Printer
.printInt("align", N
->getAlignInBits());
2093 Printer
.printInt("offset", N
->getOffsetInBits());
2094 Printer
.printDIFlags("flags", N
->getFlags());
2095 Printer
.printMetadata("extraData", N
->getRawExtraData());
2096 if (const auto &DWARFAddressSpace
= N
->getDWARFAddressSpace())
2097 Printer
.printInt("dwarfAddressSpace", *DWARFAddressSpace
,
2098 /* ShouldSkipZero */ false);
2099 Printer
.printMetadata("annotations", N
->getRawAnnotations());
2103 static void writeDICompositeType(raw_ostream
&Out
, const DICompositeType
*N
,
2104 AsmWriterContext
&WriterCtx
) {
2105 Out
<< "!DICompositeType(";
2106 MDFieldPrinter
Printer(Out
, WriterCtx
);
2107 Printer
.printTag(N
);
2108 Printer
.printString("name", N
->getName());
2109 Printer
.printMetadata("scope", N
->getRawScope());
2110 Printer
.printMetadata("file", N
->getRawFile());
2111 Printer
.printInt("line", N
->getLine());
2112 Printer
.printMetadata("baseType", N
->getRawBaseType());
2113 Printer
.printInt("size", N
->getSizeInBits());
2114 Printer
.printInt("align", N
->getAlignInBits());
2115 Printer
.printInt("offset", N
->getOffsetInBits());
2116 Printer
.printDIFlags("flags", N
->getFlags());
2117 Printer
.printMetadata("elements", N
->getRawElements());
2118 Printer
.printDwarfEnum("runtimeLang", N
->getRuntimeLang(),
2119 dwarf::LanguageString
);
2120 Printer
.printMetadata("vtableHolder", N
->getRawVTableHolder());
2121 Printer
.printMetadata("templateParams", N
->getRawTemplateParams());
2122 Printer
.printString("identifier", N
->getIdentifier());
2123 Printer
.printMetadata("discriminator", N
->getRawDiscriminator());
2124 Printer
.printMetadata("dataLocation", N
->getRawDataLocation());
2125 Printer
.printMetadata("associated", N
->getRawAssociated());
2126 Printer
.printMetadata("allocated", N
->getRawAllocated());
2127 if (auto *RankConst
= N
->getRankConst())
2128 Printer
.printInt("rank", RankConst
->getSExtValue(),
2129 /* ShouldSkipZero */ false);
2131 Printer
.printMetadata("rank", N
->getRawRank(), /*ShouldSkipNull */ true);
2132 Printer
.printMetadata("annotations", N
->getRawAnnotations());
2136 static void writeDISubroutineType(raw_ostream
&Out
, const DISubroutineType
*N
,
2137 AsmWriterContext
&WriterCtx
) {
2138 Out
<< "!DISubroutineType(";
2139 MDFieldPrinter
Printer(Out
, WriterCtx
);
2140 Printer
.printDIFlags("flags", N
->getFlags());
2141 Printer
.printDwarfEnum("cc", N
->getCC(), dwarf::ConventionString
);
2142 Printer
.printMetadata("types", N
->getRawTypeArray(),
2143 /* ShouldSkipNull */ false);
2147 static void writeDIFile(raw_ostream
&Out
, const DIFile
*N
, AsmWriterContext
&) {
2149 MDFieldPrinter
Printer(Out
);
2150 Printer
.printString("filename", N
->getFilename(),
2151 /* ShouldSkipEmpty */ false);
2152 Printer
.printString("directory", N
->getDirectory(),
2153 /* ShouldSkipEmpty */ false);
2154 // Print all values for checksum together, or not at all.
2155 if (N
->getChecksum())
2156 Printer
.printChecksum(*N
->getChecksum());
2157 Printer
.printString("source", N
->getSource().value_or(StringRef()),
2158 /* ShouldSkipEmpty */ true);
2162 static void writeDICompileUnit(raw_ostream
&Out
, const DICompileUnit
*N
,
2163 AsmWriterContext
&WriterCtx
) {
2164 Out
<< "!DICompileUnit(";
2165 MDFieldPrinter
Printer(Out
, WriterCtx
);
2166 Printer
.printDwarfEnum("language", N
->getSourceLanguage(),
2167 dwarf::LanguageString
, /* ShouldSkipZero */ false);
2168 Printer
.printMetadata("file", N
->getRawFile(), /* ShouldSkipNull */ false);
2169 Printer
.printString("producer", N
->getProducer());
2170 Printer
.printBool("isOptimized", N
->isOptimized());
2171 Printer
.printString("flags", N
->getFlags());
2172 Printer
.printInt("runtimeVersion", N
->getRuntimeVersion(),
2173 /* ShouldSkipZero */ false);
2174 Printer
.printString("splitDebugFilename", N
->getSplitDebugFilename());
2175 Printer
.printEmissionKind("emissionKind", N
->getEmissionKind());
2176 Printer
.printMetadata("enums", N
->getRawEnumTypes());
2177 Printer
.printMetadata("retainedTypes", N
->getRawRetainedTypes());
2178 Printer
.printMetadata("globals", N
->getRawGlobalVariables());
2179 Printer
.printMetadata("imports", N
->getRawImportedEntities());
2180 Printer
.printMetadata("macros", N
->getRawMacros());
2181 Printer
.printInt("dwoId", N
->getDWOId());
2182 Printer
.printBool("splitDebugInlining", N
->getSplitDebugInlining(), true);
2183 Printer
.printBool("debugInfoForProfiling", N
->getDebugInfoForProfiling(),
2185 Printer
.printNameTableKind("nameTableKind", N
->getNameTableKind());
2186 Printer
.printBool("rangesBaseAddress", N
->getRangesBaseAddress(), false);
2187 Printer
.printString("sysroot", N
->getSysRoot());
2188 Printer
.printString("sdk", N
->getSDK());
2192 static void writeDISubprogram(raw_ostream
&Out
, const DISubprogram
*N
,
2193 AsmWriterContext
&WriterCtx
) {
2194 Out
<< "!DISubprogram(";
2195 MDFieldPrinter
Printer(Out
, WriterCtx
);
2196 Printer
.printString("name", N
->getName());
2197 Printer
.printString("linkageName", N
->getLinkageName());
2198 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2199 Printer
.printMetadata("file", N
->getRawFile());
2200 Printer
.printInt("line", N
->getLine());
2201 Printer
.printMetadata("type", N
->getRawType());
2202 Printer
.printInt("scopeLine", N
->getScopeLine());
2203 Printer
.printMetadata("containingType", N
->getRawContainingType());
2204 if (N
->getVirtuality() != dwarf::DW_VIRTUALITY_none
||
2205 N
->getVirtualIndex() != 0)
2206 Printer
.printInt("virtualIndex", N
->getVirtualIndex(), false);
2207 Printer
.printInt("thisAdjustment", N
->getThisAdjustment());
2208 Printer
.printDIFlags("flags", N
->getFlags());
2209 Printer
.printDISPFlags("spFlags", N
->getSPFlags());
2210 Printer
.printMetadata("unit", N
->getRawUnit());
2211 Printer
.printMetadata("templateParams", N
->getRawTemplateParams());
2212 Printer
.printMetadata("declaration", N
->getRawDeclaration());
2213 Printer
.printMetadata("retainedNodes", N
->getRawRetainedNodes());
2214 Printer
.printMetadata("thrownTypes", N
->getRawThrownTypes());
2215 Printer
.printMetadata("annotations", N
->getRawAnnotations());
2216 Printer
.printString("targetFuncName", N
->getTargetFuncName());
2220 static void writeDILexicalBlock(raw_ostream
&Out
, const DILexicalBlock
*N
,
2221 AsmWriterContext
&WriterCtx
) {
2222 Out
<< "!DILexicalBlock(";
2223 MDFieldPrinter
Printer(Out
, WriterCtx
);
2224 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2225 Printer
.printMetadata("file", N
->getRawFile());
2226 Printer
.printInt("line", N
->getLine());
2227 Printer
.printInt("column", N
->getColumn());
2231 static void writeDILexicalBlockFile(raw_ostream
&Out
,
2232 const DILexicalBlockFile
*N
,
2233 AsmWriterContext
&WriterCtx
) {
2234 Out
<< "!DILexicalBlockFile(";
2235 MDFieldPrinter
Printer(Out
, WriterCtx
);
2236 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2237 Printer
.printMetadata("file", N
->getRawFile());
2238 Printer
.printInt("discriminator", N
->getDiscriminator(),
2239 /* ShouldSkipZero */ false);
2243 static void writeDINamespace(raw_ostream
&Out
, const DINamespace
*N
,
2244 AsmWriterContext
&WriterCtx
) {
2245 Out
<< "!DINamespace(";
2246 MDFieldPrinter
Printer(Out
, WriterCtx
);
2247 Printer
.printString("name", N
->getName());
2248 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2249 Printer
.printBool("exportSymbols", N
->getExportSymbols(), false);
2253 static void writeDICommonBlock(raw_ostream
&Out
, const DICommonBlock
*N
,
2254 AsmWriterContext
&WriterCtx
) {
2255 Out
<< "!DICommonBlock(";
2256 MDFieldPrinter
Printer(Out
, WriterCtx
);
2257 Printer
.printMetadata("scope", N
->getRawScope(), false);
2258 Printer
.printMetadata("declaration", N
->getRawDecl(), false);
2259 Printer
.printString("name", N
->getName());
2260 Printer
.printMetadata("file", N
->getRawFile());
2261 Printer
.printInt("line", N
->getLineNo());
2265 static void writeDIMacro(raw_ostream
&Out
, const DIMacro
*N
,
2266 AsmWriterContext
&WriterCtx
) {
2268 MDFieldPrinter
Printer(Out
, WriterCtx
);
2269 Printer
.printMacinfoType(N
);
2270 Printer
.printInt("line", N
->getLine());
2271 Printer
.printString("name", N
->getName());
2272 Printer
.printString("value", N
->getValue());
2276 static void writeDIMacroFile(raw_ostream
&Out
, const DIMacroFile
*N
,
2277 AsmWriterContext
&WriterCtx
) {
2278 Out
<< "!DIMacroFile(";
2279 MDFieldPrinter
Printer(Out
, WriterCtx
);
2280 Printer
.printInt("line", N
->getLine());
2281 Printer
.printMetadata("file", N
->getRawFile(), /* ShouldSkipNull */ false);
2282 Printer
.printMetadata("nodes", N
->getRawElements());
2286 static void writeDIModule(raw_ostream
&Out
, const DIModule
*N
,
2287 AsmWriterContext
&WriterCtx
) {
2288 Out
<< "!DIModule(";
2289 MDFieldPrinter
Printer(Out
, WriterCtx
);
2290 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2291 Printer
.printString("name", N
->getName());
2292 Printer
.printString("configMacros", N
->getConfigurationMacros());
2293 Printer
.printString("includePath", N
->getIncludePath());
2294 Printer
.printString("apinotes", N
->getAPINotesFile());
2295 Printer
.printMetadata("file", N
->getRawFile());
2296 Printer
.printInt("line", N
->getLineNo());
2297 Printer
.printBool("isDecl", N
->getIsDecl(), /* Default */ false);
2301 static void writeDITemplateTypeParameter(raw_ostream
&Out
,
2302 const DITemplateTypeParameter
*N
,
2303 AsmWriterContext
&WriterCtx
) {
2304 Out
<< "!DITemplateTypeParameter(";
2305 MDFieldPrinter
Printer(Out
, WriterCtx
);
2306 Printer
.printString("name", N
->getName());
2307 Printer
.printMetadata("type", N
->getRawType(), /* ShouldSkipNull */ false);
2308 Printer
.printBool("defaulted", N
->isDefault(), /* Default= */ false);
2312 static void writeDITemplateValueParameter(raw_ostream
&Out
,
2313 const DITemplateValueParameter
*N
,
2314 AsmWriterContext
&WriterCtx
) {
2315 Out
<< "!DITemplateValueParameter(";
2316 MDFieldPrinter
Printer(Out
, WriterCtx
);
2317 if (N
->getTag() != dwarf::DW_TAG_template_value_parameter
)
2318 Printer
.printTag(N
);
2319 Printer
.printString("name", N
->getName());
2320 Printer
.printMetadata("type", N
->getRawType());
2321 Printer
.printBool("defaulted", N
->isDefault(), /* Default= */ false);
2322 Printer
.printMetadata("value", N
->getValue(), /* ShouldSkipNull */ false);
2326 static void writeDIGlobalVariable(raw_ostream
&Out
, const DIGlobalVariable
*N
,
2327 AsmWriterContext
&WriterCtx
) {
2328 Out
<< "!DIGlobalVariable(";
2329 MDFieldPrinter
Printer(Out
, WriterCtx
);
2330 Printer
.printString("name", N
->getName());
2331 Printer
.printString("linkageName", N
->getLinkageName());
2332 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2333 Printer
.printMetadata("file", N
->getRawFile());
2334 Printer
.printInt("line", N
->getLine());
2335 Printer
.printMetadata("type", N
->getRawType());
2336 Printer
.printBool("isLocal", N
->isLocalToUnit());
2337 Printer
.printBool("isDefinition", N
->isDefinition());
2338 Printer
.printMetadata("declaration", N
->getRawStaticDataMemberDeclaration());
2339 Printer
.printMetadata("templateParams", N
->getRawTemplateParams());
2340 Printer
.printInt("align", N
->getAlignInBits());
2341 Printer
.printMetadata("annotations", N
->getRawAnnotations());
2345 static void writeDILocalVariable(raw_ostream
&Out
, const DILocalVariable
*N
,
2346 AsmWriterContext
&WriterCtx
) {
2347 Out
<< "!DILocalVariable(";
2348 MDFieldPrinter
Printer(Out
, WriterCtx
);
2349 Printer
.printString("name", N
->getName());
2350 Printer
.printInt("arg", N
->getArg());
2351 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2352 Printer
.printMetadata("file", N
->getRawFile());
2353 Printer
.printInt("line", N
->getLine());
2354 Printer
.printMetadata("type", N
->getRawType());
2355 Printer
.printDIFlags("flags", N
->getFlags());
2356 Printer
.printInt("align", N
->getAlignInBits());
2357 Printer
.printMetadata("annotations", N
->getRawAnnotations());
2361 static void writeDILabel(raw_ostream
&Out
, const DILabel
*N
,
2362 AsmWriterContext
&WriterCtx
) {
2364 MDFieldPrinter
Printer(Out
, WriterCtx
);
2365 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2366 Printer
.printString("name", N
->getName());
2367 Printer
.printMetadata("file", N
->getRawFile());
2368 Printer
.printInt("line", N
->getLine());
2372 static void writeDIExpression(raw_ostream
&Out
, const DIExpression
*N
,
2373 AsmWriterContext
&WriterCtx
) {
2374 Out
<< "!DIExpression(";
2377 for (const DIExpression::ExprOperand
&Op
: N
->expr_ops()) {
2378 auto OpStr
= dwarf::OperationEncodingString(Op
.getOp());
2379 assert(!OpStr
.empty() && "Expected valid opcode");
2382 if (Op
.getOp() == dwarf::DW_OP_LLVM_convert
) {
2383 Out
<< FS
<< Op
.getArg(0);
2384 Out
<< FS
<< dwarf::AttributeEncodingString(Op
.getArg(1));
2386 for (unsigned A
= 0, AE
= Op
.getNumArgs(); A
!= AE
; ++A
)
2387 Out
<< FS
<< Op
.getArg(A
);
2391 for (const auto &I
: N
->getElements())
2397 static void writeDIArgList(raw_ostream
&Out
, const DIArgList
*N
,
2398 AsmWriterContext
&WriterCtx
,
2399 bool FromValue
= false) {
2401 "Unexpected DIArgList metadata outside of value argument");
2402 Out
<< "!DIArgList(";
2404 MDFieldPrinter
Printer(Out
, WriterCtx
);
2405 for (Metadata
*Arg
: N
->getArgs()) {
2407 WriteAsOperandInternal(Out
, Arg
, WriterCtx
, true);
2412 static void writeDIGlobalVariableExpression(raw_ostream
&Out
,
2413 const DIGlobalVariableExpression
*N
,
2414 AsmWriterContext
&WriterCtx
) {
2415 Out
<< "!DIGlobalVariableExpression(";
2416 MDFieldPrinter
Printer(Out
, WriterCtx
);
2417 Printer
.printMetadata("var", N
->getVariable());
2418 Printer
.printMetadata("expr", N
->getExpression());
2422 static void writeDIObjCProperty(raw_ostream
&Out
, const DIObjCProperty
*N
,
2423 AsmWriterContext
&WriterCtx
) {
2424 Out
<< "!DIObjCProperty(";
2425 MDFieldPrinter
Printer(Out
, WriterCtx
);
2426 Printer
.printString("name", N
->getName());
2427 Printer
.printMetadata("file", N
->getRawFile());
2428 Printer
.printInt("line", N
->getLine());
2429 Printer
.printString("setter", N
->getSetterName());
2430 Printer
.printString("getter", N
->getGetterName());
2431 Printer
.printInt("attributes", N
->getAttributes());
2432 Printer
.printMetadata("type", N
->getRawType());
2436 static void writeDIImportedEntity(raw_ostream
&Out
, const DIImportedEntity
*N
,
2437 AsmWriterContext
&WriterCtx
) {
2438 Out
<< "!DIImportedEntity(";
2439 MDFieldPrinter
Printer(Out
, WriterCtx
);
2440 Printer
.printTag(N
);
2441 Printer
.printString("name", N
->getName());
2442 Printer
.printMetadata("scope", N
->getRawScope(), /* ShouldSkipNull */ false);
2443 Printer
.printMetadata("entity", N
->getRawEntity());
2444 Printer
.printMetadata("file", N
->getRawFile());
2445 Printer
.printInt("line", N
->getLine());
2446 Printer
.printMetadata("elements", N
->getRawElements());
2450 static void WriteMDNodeBodyInternal(raw_ostream
&Out
, const MDNode
*Node
,
2451 AsmWriterContext
&Ctx
) {
2452 if (Node
->isDistinct())
2454 else if (Node
->isTemporary())
2455 Out
<< "<temporary!> "; // Handle broken code.
2457 switch (Node
->getMetadataID()) {
2459 llvm_unreachable("Expected uniquable MDNode");
2460 #define HANDLE_MDNODE_LEAF(CLASS) \
2461 case Metadata::CLASS##Kind: \
2462 write##CLASS(Out, cast<CLASS>(Node), Ctx); \
2464 #include "llvm/IR/Metadata.def"
2468 // Full implementation of printing a Value as an operand with support for
2469 // TypePrinting, etc.
2470 static void WriteAsOperandInternal(raw_ostream
&Out
, const Value
*V
,
2471 AsmWriterContext
&WriterCtx
) {
2473 PrintLLVMName(Out
, V
);
2477 const Constant
*CV
= dyn_cast
<Constant
>(V
);
2478 if (CV
&& !isa
<GlobalValue
>(CV
)) {
2479 assert(WriterCtx
.TypePrinter
&& "Constants require TypePrinting!");
2480 WriteConstantInternal(Out
, CV
, WriterCtx
);
2484 if (const InlineAsm
*IA
= dyn_cast
<InlineAsm
>(V
)) {
2486 if (IA
->hasSideEffects())
2487 Out
<< "sideeffect ";
2488 if (IA
->isAlignStack())
2489 Out
<< "alignstack ";
2490 // We don't emit the AD_ATT dialect as it's the assumed default.
2491 if (IA
->getDialect() == InlineAsm::AD_Intel
)
2492 Out
<< "inteldialect ";
2496 printEscapedString(IA
->getAsmString(), Out
);
2498 printEscapedString(IA
->getConstraintString(), Out
);
2503 if (auto *MD
= dyn_cast
<MetadataAsValue
>(V
)) {
2504 WriteAsOperandInternal(Out
, MD
->getMetadata(), WriterCtx
,
2505 /* FromValue */ true);
2511 auto *Machine
= WriterCtx
.Machine
;
2512 // If we have a SlotTracker, use it.
2514 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
2515 Slot
= Machine
->getGlobalSlot(GV
);
2518 Slot
= Machine
->getLocalSlot(V
);
2520 // If the local value didn't succeed, then we may be referring to a value
2521 // from a different function. Translate it, as this can happen when using
2522 // address of blocks.
2524 if ((Machine
= createSlotTracker(V
))) {
2525 Slot
= Machine
->getLocalSlot(V
);
2529 } else if ((Machine
= createSlotTracker(V
))) {
2530 // Otherwise, create one to get the # and then destroy it.
2531 if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(V
)) {
2532 Slot
= Machine
->getGlobalSlot(GV
);
2535 Slot
= Machine
->getLocalSlot(V
);
2544 Out
<< Prefix
<< Slot
;
2549 static void WriteAsOperandInternal(raw_ostream
&Out
, const Metadata
*MD
,
2550 AsmWriterContext
&WriterCtx
,
2552 // Write DIExpressions and DIArgLists inline when used as a value. Improves
2553 // readability of debug info intrinsics.
2554 if (const DIExpression
*Expr
= dyn_cast
<DIExpression
>(MD
)) {
2555 writeDIExpression(Out
, Expr
, WriterCtx
);
2558 if (const DIArgList
*ArgList
= dyn_cast
<DIArgList
>(MD
)) {
2559 writeDIArgList(Out
, ArgList
, WriterCtx
, FromValue
);
2563 if (const MDNode
*N
= dyn_cast
<MDNode
>(MD
)) {
2564 std::unique_ptr
<SlotTracker
> MachineStorage
;
2565 SaveAndRestore
SARMachine(WriterCtx
.Machine
);
2566 if (!WriterCtx
.Machine
) {
2567 MachineStorage
= std::make_unique
<SlotTracker
>(WriterCtx
.Context
);
2568 WriterCtx
.Machine
= MachineStorage
.get();
2570 int Slot
= WriterCtx
.Machine
->getMetadataSlot(N
);
2572 if (const DILocation
*Loc
= dyn_cast
<DILocation
>(N
)) {
2573 writeDILocation(Out
, Loc
, WriterCtx
);
2576 // Give the pointer value instead of "badref", since this comes up all
2577 // the time when debugging.
2578 Out
<< "<" << N
<< ">";
2584 if (const MDString
*MDS
= dyn_cast
<MDString
>(MD
)) {
2586 printEscapedString(MDS
->getString(), Out
);
2591 auto *V
= cast
<ValueAsMetadata
>(MD
);
2592 assert(WriterCtx
.TypePrinter
&& "TypePrinter required for metadata values");
2593 assert((FromValue
|| !isa
<LocalAsMetadata
>(V
)) &&
2594 "Unexpected function-local metadata outside of value argument");
2596 WriterCtx
.TypePrinter
->print(V
->getValue()->getType(), Out
);
2598 WriteAsOperandInternal(Out
, V
->getValue(), WriterCtx
);
2603 class AssemblyWriter
{
2604 formatted_raw_ostream
&Out
;
2605 const Module
*TheModule
= nullptr;
2606 const ModuleSummaryIndex
*TheIndex
= nullptr;
2607 std::unique_ptr
<SlotTracker
> SlotTrackerStorage
;
2608 SlotTracker
&Machine
;
2609 TypePrinting TypePrinter
;
2610 AssemblyAnnotationWriter
*AnnotationWriter
= nullptr;
2611 SetVector
<const Comdat
*> Comdats
;
2613 bool ShouldPreserveUseListOrder
;
2614 UseListOrderMap UseListOrders
;
2615 SmallVector
<StringRef
, 8> MDNames
;
2616 /// Synchronization scope names registered with LLVMContext.
2617 SmallVector
<StringRef
, 8> SSNs
;
2618 DenseMap
<const GlobalValueSummary
*, GlobalValue::GUID
> SummaryToGUIDMap
;
2621 /// Construct an AssemblyWriter with an external SlotTracker
2622 AssemblyWriter(formatted_raw_ostream
&o
, SlotTracker
&Mac
, const Module
*M
,
2623 AssemblyAnnotationWriter
*AAW
, bool IsForDebug
,
2624 bool ShouldPreserveUseListOrder
= false);
2626 AssemblyWriter(formatted_raw_ostream
&o
, SlotTracker
&Mac
,
2627 const ModuleSummaryIndex
*Index
, bool IsForDebug
);
2629 AsmWriterContext
getContext() {
2630 return AsmWriterContext(&TypePrinter
, &Machine
, TheModule
);
2633 void printMDNodeBody(const MDNode
*MD
);
2634 void printNamedMDNode(const NamedMDNode
*NMD
);
2636 void printModule(const Module
*M
);
2638 void writeOperand(const Value
*Op
, bool PrintType
);
2639 void writeParamOperand(const Value
*Operand
, AttributeSet Attrs
);
2640 void writeOperandBundles(const CallBase
*Call
);
2641 void writeSyncScope(const LLVMContext
&Context
,
2642 SyncScope::ID SSID
);
2643 void writeAtomic(const LLVMContext
&Context
,
2644 AtomicOrdering Ordering
,
2645 SyncScope::ID SSID
);
2646 void writeAtomicCmpXchg(const LLVMContext
&Context
,
2647 AtomicOrdering SuccessOrdering
,
2648 AtomicOrdering FailureOrdering
,
2649 SyncScope::ID SSID
);
2651 void writeAllMDNodes();
2652 void writeMDNode(unsigned Slot
, const MDNode
*Node
);
2653 void writeAttribute(const Attribute
&Attr
, bool InAttrGroup
= false);
2654 void writeAttributeSet(const AttributeSet
&AttrSet
, bool InAttrGroup
= false);
2655 void writeAllAttributeGroups();
2657 void printTypeIdentities();
2658 void printGlobal(const GlobalVariable
*GV
);
2659 void printAlias(const GlobalAlias
*GA
);
2660 void printIFunc(const GlobalIFunc
*GI
);
2661 void printComdat(const Comdat
*C
);
2662 void printFunction(const Function
*F
);
2663 void printArgument(const Argument
*FA
, AttributeSet Attrs
);
2664 void printBasicBlock(const BasicBlock
*BB
);
2665 void printInstructionLine(const Instruction
&I
);
2666 void printInstruction(const Instruction
&I
);
2667 void printDPMarker(const DPMarker
&DPI
);
2668 void printDPValue(const DPValue
&DPI
);
2670 void printUseListOrder(const Value
*V
, const std::vector
<unsigned> &Shuffle
);
2671 void printUseLists(const Function
*F
);
2673 void printModuleSummaryIndex();
2674 void printSummaryInfo(unsigned Slot
, const ValueInfo
&VI
);
2675 void printSummary(const GlobalValueSummary
&Summary
);
2676 void printAliasSummary(const AliasSummary
*AS
);
2677 void printGlobalVarSummary(const GlobalVarSummary
*GS
);
2678 void printFunctionSummary(const FunctionSummary
*FS
);
2679 void printTypeIdSummary(const TypeIdSummary
&TIS
);
2680 void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo
&TI
);
2681 void printTypeTestResolution(const TypeTestResolution
&TTRes
);
2682 void printArgs(const std::vector
<uint64_t> &Args
);
2683 void printWPDRes(const WholeProgramDevirtResolution
&WPDRes
);
2684 void printTypeIdInfo(const FunctionSummary::TypeIdInfo
&TIDInfo
);
2685 void printVFuncId(const FunctionSummary::VFuncId VFId
);
2687 printNonConstVCalls(const std::vector
<FunctionSummary::VFuncId
> &VCallList
,
2690 printConstVCalls(const std::vector
<FunctionSummary::ConstVCall
> &VCallList
,
2694 /// Print out metadata attachments.
2695 void printMetadataAttachments(
2696 const SmallVectorImpl
<std::pair
<unsigned, MDNode
*>> &MDs
,
2697 StringRef Separator
);
2699 // printInfoComment - Print a little comment after the instruction indicating
2700 // which slot it occupies.
2701 void printInfoComment(const Value
&V
);
2703 // printGCRelocateComment - print comment after call to the gc.relocate
2704 // intrinsic indicating base and derived pointer names.
2705 void printGCRelocateComment(const GCRelocateInst
&Relocate
);
2708 } // end anonymous namespace
2710 AssemblyWriter::AssemblyWriter(formatted_raw_ostream
&o
, SlotTracker
&Mac
,
2711 const Module
*M
, AssemblyAnnotationWriter
*AAW
,
2712 bool IsForDebug
, bool ShouldPreserveUseListOrder
)
2713 : Out(o
), TheModule(M
), Machine(Mac
), TypePrinter(M
), AnnotationWriter(AAW
),
2714 IsForDebug(IsForDebug
),
2715 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
) {
2718 for (const GlobalObject
&GO
: TheModule
->global_objects())
2719 if (const Comdat
*C
= GO
.getComdat())
2723 AssemblyWriter::AssemblyWriter(formatted_raw_ostream
&o
, SlotTracker
&Mac
,
2724 const ModuleSummaryIndex
*Index
, bool IsForDebug
)
2725 : Out(o
), TheIndex(Index
), Machine(Mac
), TypePrinter(/*Module=*/nullptr),
2726 IsForDebug(IsForDebug
), ShouldPreserveUseListOrder(false) {}
2728 void AssemblyWriter::writeOperand(const Value
*Operand
, bool PrintType
) {
2730 Out
<< "<null operand!>";
2734 TypePrinter
.print(Operand
->getType(), Out
);
2737 auto WriterCtx
= getContext();
2738 WriteAsOperandInternal(Out
, Operand
, WriterCtx
);
2741 void AssemblyWriter::writeSyncScope(const LLVMContext
&Context
,
2742 SyncScope::ID SSID
) {
2744 case SyncScope::System
: {
2749 Context
.getSyncScopeNames(SSNs
);
2751 Out
<< " syncscope(\"";
2752 printEscapedString(SSNs
[SSID
], Out
);
2759 void AssemblyWriter::writeAtomic(const LLVMContext
&Context
,
2760 AtomicOrdering Ordering
,
2761 SyncScope::ID SSID
) {
2762 if (Ordering
== AtomicOrdering::NotAtomic
)
2765 writeSyncScope(Context
, SSID
);
2766 Out
<< " " << toIRString(Ordering
);
2769 void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext
&Context
,
2770 AtomicOrdering SuccessOrdering
,
2771 AtomicOrdering FailureOrdering
,
2772 SyncScope::ID SSID
) {
2773 assert(SuccessOrdering
!= AtomicOrdering::NotAtomic
&&
2774 FailureOrdering
!= AtomicOrdering::NotAtomic
);
2776 writeSyncScope(Context
, SSID
);
2777 Out
<< " " << toIRString(SuccessOrdering
);
2778 Out
<< " " << toIRString(FailureOrdering
);
2781 void AssemblyWriter::writeParamOperand(const Value
*Operand
,
2782 AttributeSet Attrs
) {
2784 Out
<< "<null operand!>";
2789 TypePrinter
.print(Operand
->getType(), Out
);
2790 // Print parameter attributes list
2791 if (Attrs
.hasAttributes()) {
2793 writeAttributeSet(Attrs
);
2796 // Print the operand
2797 auto WriterCtx
= getContext();
2798 WriteAsOperandInternal(Out
, Operand
, WriterCtx
);
2801 void AssemblyWriter::writeOperandBundles(const CallBase
*Call
) {
2802 if (!Call
->hasOperandBundles())
2807 bool FirstBundle
= true;
2808 for (unsigned i
= 0, e
= Call
->getNumOperandBundles(); i
!= e
; ++i
) {
2809 OperandBundleUse BU
= Call
->getOperandBundleAt(i
);
2813 FirstBundle
= false;
2816 printEscapedString(BU
.getTagName(), Out
);
2821 bool FirstInput
= true;
2822 auto WriterCtx
= getContext();
2823 for (const auto &Input
: BU
.Inputs
) {
2828 if (Input
== nullptr)
2829 Out
<< "<null operand bundle!>";
2831 TypePrinter
.print(Input
->getType(), Out
);
2833 WriteAsOperandInternal(Out
, Input
, WriterCtx
);
2843 void AssemblyWriter::printModule(const Module
*M
) {
2844 Machine
.initializeIfNeeded();
2846 if (ShouldPreserveUseListOrder
)
2847 UseListOrders
= predictUseListOrder(M
);
2849 if (!M
->getModuleIdentifier().empty() &&
2850 // Don't print the ID if it will start a new line (which would
2851 // require a comment char before it).
2852 M
->getModuleIdentifier().find('\n') == std::string::npos
)
2853 Out
<< "; ModuleID = '" << M
->getModuleIdentifier() << "'\n";
2855 if (!M
->getSourceFileName().empty()) {
2856 Out
<< "source_filename = \"";
2857 printEscapedString(M
->getSourceFileName(), Out
);
2861 const std::string
&DL
= M
->getDataLayoutStr();
2863 Out
<< "target datalayout = \"" << DL
<< "\"\n";
2864 if (!M
->getTargetTriple().empty())
2865 Out
<< "target triple = \"" << M
->getTargetTriple() << "\"\n";
2867 if (!M
->getModuleInlineAsm().empty()) {
2870 // Split the string into lines, to make it easier to read the .ll file.
2871 StringRef Asm
= M
->getModuleInlineAsm();
2874 std::tie(Front
, Asm
) = Asm
.split('\n');
2876 // We found a newline, print the portion of the asm string from the
2877 // last newline up to this newline.
2878 Out
<< "module asm \"";
2879 printEscapedString(Front
, Out
);
2881 } while (!Asm
.empty());
2884 printTypeIdentities();
2886 // Output all comdats.
2887 if (!Comdats
.empty())
2889 for (const Comdat
*C
: Comdats
) {
2891 if (C
!= Comdats
.back())
2895 // Output all globals.
2896 if (!M
->global_empty()) Out
<< '\n';
2897 for (const GlobalVariable
&GV
: M
->globals()) {
2898 printGlobal(&GV
); Out
<< '\n';
2901 // Output all aliases.
2902 if (!M
->alias_empty()) Out
<< "\n";
2903 for (const GlobalAlias
&GA
: M
->aliases())
2906 // Output all ifuncs.
2907 if (!M
->ifunc_empty()) Out
<< "\n";
2908 for (const GlobalIFunc
&GI
: M
->ifuncs())
2911 // Output all of the functions.
2912 for (const Function
&F
: *M
) {
2917 // Output global use-lists.
2918 printUseLists(nullptr);
2920 // Output all attribute groups.
2921 if (!Machine
.as_empty()) {
2923 writeAllAttributeGroups();
2926 // Output named metadata.
2927 if (!M
->named_metadata_empty()) Out
<< '\n';
2929 for (const NamedMDNode
&Node
: M
->named_metadata())
2930 printNamedMDNode(&Node
);
2933 if (!Machine
.mdn_empty()) {
2939 void AssemblyWriter::printModuleSummaryIndex() {
2941 int NumSlots
= Machine
.initializeIndexIfNeeded();
2945 // Print module path entries. To print in order, add paths to a vector
2946 // indexed by module slot.
2947 std::vector
<std::pair
<std::string
, ModuleHash
>> moduleVec
;
2948 std::string RegularLTOModuleName
=
2949 ModuleSummaryIndex::getRegularLTOModuleName();
2950 moduleVec
.resize(TheIndex
->modulePaths().size());
2951 for (auto &[ModPath
, ModHash
] : TheIndex
->modulePaths())
2952 moduleVec
[Machine
.getModulePathSlot(ModPath
)] = std::make_pair(
2953 // An empty module path is a special entry for a regular LTO module
2954 // created during the thin link.
2955 ModPath
.empty() ? RegularLTOModuleName
: std::string(ModPath
), ModHash
);
2958 for (auto &ModPair
: moduleVec
) {
2959 Out
<< "^" << i
++ << " = module: (";
2961 printEscapedString(ModPair
.first
, Out
);
2962 Out
<< "\", hash: (";
2964 for (auto Hash
: ModPair
.second
)
2969 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2970 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2971 for (auto &GlobalList
: *TheIndex
) {
2972 auto GUID
= GlobalList
.first
;
2973 for (auto &Summary
: GlobalList
.second
.SummaryList
)
2974 SummaryToGUIDMap
[Summary
.get()] = GUID
;
2977 // Print the global value summary entries.
2978 for (auto &GlobalList
: *TheIndex
) {
2979 auto GUID
= GlobalList
.first
;
2980 auto VI
= TheIndex
->getValueInfo(GlobalList
);
2981 printSummaryInfo(Machine
.getGUIDSlot(GUID
), VI
);
2984 // Print the TypeIdMap entries.
2985 for (const auto &TID
: TheIndex
->typeIds()) {
2986 Out
<< "^" << Machine
.getTypeIdSlot(TID
.second
.first
)
2987 << " = typeid: (name: \"" << TID
.second
.first
<< "\"";
2988 printTypeIdSummary(TID
.second
.second
);
2989 Out
<< ") ; guid = " << TID
.first
<< "\n";
2992 // Print the TypeIdCompatibleVtableMap entries.
2993 for (auto &TId
: TheIndex
->typeIdCompatibleVtableMap()) {
2994 auto GUID
= GlobalValue::getGUID(TId
.first
);
2995 Out
<< "^" << Machine
.getTypeIdCompatibleVtableSlot(TId
.first
)
2996 << " = typeidCompatibleVTable: (name: \"" << TId
.first
<< "\"";
2997 printTypeIdCompatibleVtableSummary(TId
.second
);
2998 Out
<< ") ; guid = " << GUID
<< "\n";
3001 // Don't emit flags when it's not really needed (value is zero by default).
3002 if (TheIndex
->getFlags()) {
3003 Out
<< "^" << NumSlots
<< " = flags: " << TheIndex
->getFlags() << "\n";
3007 Out
<< "^" << NumSlots
<< " = blockcount: " << TheIndex
->getBlockCount()
3012 getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K
) {
3014 case WholeProgramDevirtResolution::Indir
:
3016 case WholeProgramDevirtResolution::SingleImpl
:
3017 return "singleImpl";
3018 case WholeProgramDevirtResolution::BranchFunnel
:
3019 return "branchFunnel";
3021 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
3024 static const char *getWholeProgDevirtResByArgKindName(
3025 WholeProgramDevirtResolution::ByArg::Kind K
) {
3027 case WholeProgramDevirtResolution::ByArg::Indir
:
3029 case WholeProgramDevirtResolution::ByArg::UniformRetVal
:
3030 return "uniformRetVal";
3031 case WholeProgramDevirtResolution::ByArg::UniqueRetVal
:
3032 return "uniqueRetVal";
3033 case WholeProgramDevirtResolution::ByArg::VirtualConstProp
:
3034 return "virtualConstProp";
3036 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
3039 static const char *getTTResKindName(TypeTestResolution::Kind K
) {
3041 case TypeTestResolution::Unknown
:
3043 case TypeTestResolution::Unsat
:
3045 case TypeTestResolution::ByteArray
:
3047 case TypeTestResolution::Inline
:
3049 case TypeTestResolution::Single
:
3051 case TypeTestResolution::AllOnes
:
3054 llvm_unreachable("invalid TypeTestResolution kind");
3057 void AssemblyWriter::printTypeTestResolution(const TypeTestResolution
&TTRes
) {
3058 Out
<< "typeTestRes: (kind: " << getTTResKindName(TTRes
.TheKind
)
3059 << ", sizeM1BitWidth: " << TTRes
.SizeM1BitWidth
;
3061 // The following fields are only used if the target does not support the use
3062 // of absolute symbols to store constants. Print only if non-zero.
3063 if (TTRes
.AlignLog2
)
3064 Out
<< ", alignLog2: " << TTRes
.AlignLog2
;
3066 Out
<< ", sizeM1: " << TTRes
.SizeM1
;
3068 // BitMask is uint8_t which causes it to print the corresponding char.
3069 Out
<< ", bitMask: " << (unsigned)TTRes
.BitMask
;
3070 if (TTRes
.InlineBits
)
3071 Out
<< ", inlineBits: " << TTRes
.InlineBits
;
3076 void AssemblyWriter::printTypeIdSummary(const TypeIdSummary
&TIS
) {
3077 Out
<< ", summary: (";
3078 printTypeTestResolution(TIS
.TTRes
);
3079 if (!TIS
.WPDRes
.empty()) {
3080 Out
<< ", wpdResolutions: (";
3082 for (auto &WPDRes
: TIS
.WPDRes
) {
3084 Out
<< "(offset: " << WPDRes
.first
<< ", ";
3085 printWPDRes(WPDRes
.second
);
3093 void AssemblyWriter::printTypeIdCompatibleVtableSummary(
3094 const TypeIdCompatibleVtableInfo
&TI
) {
3095 Out
<< ", summary: (";
3097 for (auto &P
: TI
) {
3099 Out
<< "(offset: " << P
.AddressPointOffset
<< ", ";
3100 Out
<< "^" << Machine
.getGUIDSlot(P
.VTableVI
.getGUID());
3106 void AssemblyWriter::printArgs(const std::vector
<uint64_t> &Args
) {
3109 for (auto arg
: Args
) {
3116 void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution
&WPDRes
) {
3117 Out
<< "wpdRes: (kind: ";
3118 Out
<< getWholeProgDevirtResKindName(WPDRes
.TheKind
);
3120 if (WPDRes
.TheKind
== WholeProgramDevirtResolution::SingleImpl
)
3121 Out
<< ", singleImplName: \"" << WPDRes
.SingleImplName
<< "\"";
3123 if (!WPDRes
.ResByArg
.empty()) {
3124 Out
<< ", resByArg: (";
3126 for (auto &ResByArg
: WPDRes
.ResByArg
) {
3128 printArgs(ResByArg
.first
);
3129 Out
<< ", byArg: (kind: ";
3130 Out
<< getWholeProgDevirtResByArgKindName(ResByArg
.second
.TheKind
);
3131 if (ResByArg
.second
.TheKind
==
3132 WholeProgramDevirtResolution::ByArg::UniformRetVal
||
3133 ResByArg
.second
.TheKind
==
3134 WholeProgramDevirtResolution::ByArg::UniqueRetVal
)
3135 Out
<< ", info: " << ResByArg
.second
.Info
;
3137 // The following fields are only used if the target does not support the
3138 // use of absolute symbols to store constants. Print only if non-zero.
3139 if (ResByArg
.second
.Byte
|| ResByArg
.second
.Bit
)
3140 Out
<< ", byte: " << ResByArg
.second
.Byte
3141 << ", bit: " << ResByArg
.second
.Bit
;
3150 static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK
) {
3152 case GlobalValueSummary::AliasKind
:
3154 case GlobalValueSummary::FunctionKind
:
3156 case GlobalValueSummary::GlobalVarKind
:
3159 llvm_unreachable("invalid summary kind");
3162 void AssemblyWriter::printAliasSummary(const AliasSummary
*AS
) {
3163 Out
<< ", aliasee: ";
3164 // The indexes emitted for distributed backends may not include the
3165 // aliasee summary (only if it is being imported directly). Handle
3166 // that case by just emitting "null" as the aliasee.
3167 if (AS
->hasAliasee())
3168 Out
<< "^" << Machine
.getGUIDSlot(SummaryToGUIDMap
[&AS
->getAliasee()]);
3173 void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary
*GS
) {
3174 auto VTableFuncs
= GS
->vTableFuncs();
3175 Out
<< ", varFlags: (readonly: " << GS
->VarFlags
.MaybeReadOnly
<< ", "
3176 << "writeonly: " << GS
->VarFlags
.MaybeWriteOnly
<< ", "
3177 << "constant: " << GS
->VarFlags
.Constant
;
3178 if (!VTableFuncs
.empty())
3180 << "vcall_visibility: " << GS
->VarFlags
.VCallVisibility
;
3183 if (!VTableFuncs
.empty()) {
3184 Out
<< ", vTableFuncs: (";
3186 for (auto &P
: VTableFuncs
) {
3188 Out
<< "(virtFunc: ^" << Machine
.getGUIDSlot(P
.FuncVI
.getGUID())
3189 << ", offset: " << P
.VTableOffset
;
3196 static std::string
getLinkageName(GlobalValue::LinkageTypes LT
) {
3198 case GlobalValue::ExternalLinkage
:
3200 case GlobalValue::PrivateLinkage
:
3202 case GlobalValue::InternalLinkage
:
3204 case GlobalValue::LinkOnceAnyLinkage
:
3206 case GlobalValue::LinkOnceODRLinkage
:
3207 return "linkonce_odr";
3208 case GlobalValue::WeakAnyLinkage
:
3210 case GlobalValue::WeakODRLinkage
:
3212 case GlobalValue::CommonLinkage
:
3214 case GlobalValue::AppendingLinkage
:
3216 case GlobalValue::ExternalWeakLinkage
:
3217 return "extern_weak";
3218 case GlobalValue::AvailableExternallyLinkage
:
3219 return "available_externally";
3221 llvm_unreachable("invalid linkage");
3224 // When printing the linkage types in IR where the ExternalLinkage is
3225 // not printed, and other linkage types are expected to be printed with
3226 // a space after the name.
3227 static std::string
getLinkageNameWithSpace(GlobalValue::LinkageTypes LT
) {
3228 if (LT
== GlobalValue::ExternalLinkage
)
3230 return getLinkageName(LT
) + " ";
3233 static const char *getVisibilityName(GlobalValue::VisibilityTypes Vis
) {
3235 case GlobalValue::DefaultVisibility
:
3237 case GlobalValue::HiddenVisibility
:
3239 case GlobalValue::ProtectedVisibility
:
3242 llvm_unreachable("invalid visibility");
3245 void AssemblyWriter::printFunctionSummary(const FunctionSummary
*FS
) {
3246 Out
<< ", insts: " << FS
->instCount();
3247 if (FS
->fflags().anyFlagSet())
3248 Out
<< ", " << FS
->fflags();
3250 if (!FS
->calls().empty()) {
3251 Out
<< ", calls: (";
3253 for (auto &Call
: FS
->calls()) {
3255 Out
<< "(callee: ^" << Machine
.getGUIDSlot(Call
.first
.getGUID());
3256 if (Call
.second
.getHotness() != CalleeInfo::HotnessType::Unknown
)
3257 Out
<< ", hotness: " << getHotnessName(Call
.second
.getHotness());
3258 else if (Call
.second
.RelBlockFreq
)
3259 Out
<< ", relbf: " << Call
.second
.RelBlockFreq
;
3260 // Follow the convention of emitting flags as a boolean value, but only
3261 // emit if true to avoid unnecessary verbosity and test churn.
3262 if (Call
.second
.HasTailCall
)
3269 if (const auto *TIdInfo
= FS
->getTypeIdInfo())
3270 printTypeIdInfo(*TIdInfo
);
3272 // The AllocationType identifiers capture the profiled context behavior
3273 // reaching a specific static allocation site (possibly cloned).
3274 auto AllocTypeName
= [](uint8_t Type
) -> const char * {
3276 case (uint8_t)AllocationType::None
:
3278 case (uint8_t)AllocationType::NotCold
:
3280 case (uint8_t)AllocationType::Cold
:
3282 case (uint8_t)AllocationType::Hot
:
3285 llvm_unreachable("Unexpected alloc type");
3288 if (!FS
->allocs().empty()) {
3289 Out
<< ", allocs: (";
3291 for (auto &AI
: FS
->allocs()) {
3293 Out
<< "(versions: (";
3295 for (auto V
: AI
.Versions
) {
3297 Out
<< AllocTypeName(V
);
3299 Out
<< "), memProf: (";
3300 FieldSeparator MIBFS
;
3301 for (auto &MIB
: AI
.MIBs
) {
3303 Out
<< "(type: " << AllocTypeName((uint8_t)MIB
.AllocType
);
3304 Out
<< ", stackIds: (";
3305 FieldSeparator SIDFS
;
3306 for (auto Id
: MIB
.StackIdIndices
) {
3308 Out
<< TheIndex
->getStackIdAtIndex(Id
);
3317 if (!FS
->callsites().empty()) {
3318 Out
<< ", callsites: (";
3319 FieldSeparator SNFS
;
3320 for (auto &CI
: FS
->callsites()) {
3323 Out
<< "(callee: ^" << Machine
.getGUIDSlot(CI
.Callee
.getGUID());
3325 Out
<< "(callee: null";
3326 Out
<< ", clones: (";
3328 for (auto V
: CI
.Clones
) {
3332 Out
<< "), stackIds: (";
3333 FieldSeparator SIDFS
;
3334 for (auto Id
: CI
.StackIdIndices
) {
3336 Out
<< TheIndex
->getStackIdAtIndex(Id
);
3343 auto PrintRange
= [&](const ConstantRange
&Range
) {
3344 Out
<< "[" << Range
.getSignedMin() << ", " << Range
.getSignedMax() << "]";
3347 if (!FS
->paramAccesses().empty()) {
3348 Out
<< ", params: (";
3350 for (auto &PS
: FS
->paramAccesses()) {
3352 Out
<< "(param: " << PS
.ParamNo
;
3353 Out
<< ", offset: ";
3355 if (!PS
.Calls
.empty()) {
3356 Out
<< ", calls: (";
3358 for (auto &Call
: PS
.Calls
) {
3360 Out
<< "(callee: ^" << Machine
.getGUIDSlot(Call
.Callee
.getGUID());
3361 Out
<< ", param: " << Call
.ParamNo
;
3362 Out
<< ", offset: ";
3363 PrintRange(Call
.Offsets
);
3374 void AssemblyWriter::printTypeIdInfo(
3375 const FunctionSummary::TypeIdInfo
&TIDInfo
) {
3376 Out
<< ", typeIdInfo: (";
3377 FieldSeparator TIDFS
;
3378 if (!TIDInfo
.TypeTests
.empty()) {
3380 Out
<< "typeTests: (";
3382 for (auto &GUID
: TIDInfo
.TypeTests
) {
3383 auto TidIter
= TheIndex
->typeIds().equal_range(GUID
);
3384 if (TidIter
.first
== TidIter
.second
) {
3389 // Print all type id that correspond to this GUID.
3390 for (auto It
= TidIter
.first
; It
!= TidIter
.second
; ++It
) {
3392 auto Slot
= Machine
.getTypeIdSlot(It
->second
.first
);
3399 if (!TIDInfo
.TypeTestAssumeVCalls
.empty()) {
3401 printNonConstVCalls(TIDInfo
.TypeTestAssumeVCalls
, "typeTestAssumeVCalls");
3403 if (!TIDInfo
.TypeCheckedLoadVCalls
.empty()) {
3405 printNonConstVCalls(TIDInfo
.TypeCheckedLoadVCalls
, "typeCheckedLoadVCalls");
3407 if (!TIDInfo
.TypeTestAssumeConstVCalls
.empty()) {
3409 printConstVCalls(TIDInfo
.TypeTestAssumeConstVCalls
,
3410 "typeTestAssumeConstVCalls");
3412 if (!TIDInfo
.TypeCheckedLoadConstVCalls
.empty()) {
3414 printConstVCalls(TIDInfo
.TypeCheckedLoadConstVCalls
,
3415 "typeCheckedLoadConstVCalls");
3420 void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId
) {
3421 auto TidIter
= TheIndex
->typeIds().equal_range(VFId
.GUID
);
3422 if (TidIter
.first
== TidIter
.second
) {
3423 Out
<< "vFuncId: (";
3424 Out
<< "guid: " << VFId
.GUID
;
3425 Out
<< ", offset: " << VFId
.Offset
;
3429 // Print all type id that correspond to this GUID.
3431 for (auto It
= TidIter
.first
; It
!= TidIter
.second
; ++It
) {
3433 Out
<< "vFuncId: (";
3434 auto Slot
= Machine
.getTypeIdSlot(It
->second
.first
);
3437 Out
<< ", offset: " << VFId
.Offset
;
3442 void AssemblyWriter::printNonConstVCalls(
3443 const std::vector
<FunctionSummary::VFuncId
> &VCallList
, const char *Tag
) {
3444 Out
<< Tag
<< ": (";
3446 for (auto &VFuncId
: VCallList
) {
3448 printVFuncId(VFuncId
);
3453 void AssemblyWriter::printConstVCalls(
3454 const std::vector
<FunctionSummary::ConstVCall
> &VCallList
,
3456 Out
<< Tag
<< ": (";
3458 for (auto &ConstVCall
: VCallList
) {
3461 printVFuncId(ConstVCall
.VFunc
);
3462 if (!ConstVCall
.Args
.empty()) {
3464 printArgs(ConstVCall
.Args
);
3471 void AssemblyWriter::printSummary(const GlobalValueSummary
&Summary
) {
3472 GlobalValueSummary::GVFlags GVFlags
= Summary
.flags();
3473 GlobalValue::LinkageTypes LT
= (GlobalValue::LinkageTypes
)GVFlags
.Linkage
;
3474 Out
<< getSummaryKindName(Summary
.getSummaryKind()) << ": ";
3475 Out
<< "(module: ^" << Machine
.getModulePathSlot(Summary
.modulePath())
3477 Out
<< "linkage: " << getLinkageName(LT
);
3478 Out
<< ", visibility: "
3479 << getVisibilityName((GlobalValue::VisibilityTypes
)GVFlags
.Visibility
);
3480 Out
<< ", notEligibleToImport: " << GVFlags
.NotEligibleToImport
;
3481 Out
<< ", live: " << GVFlags
.Live
;
3482 Out
<< ", dsoLocal: " << GVFlags
.DSOLocal
;
3483 Out
<< ", canAutoHide: " << GVFlags
.CanAutoHide
;
3486 if (Summary
.getSummaryKind() == GlobalValueSummary::AliasKind
)
3487 printAliasSummary(cast
<AliasSummary
>(&Summary
));
3488 else if (Summary
.getSummaryKind() == GlobalValueSummary::FunctionKind
)
3489 printFunctionSummary(cast
<FunctionSummary
>(&Summary
));
3491 printGlobalVarSummary(cast
<GlobalVarSummary
>(&Summary
));
3493 auto RefList
= Summary
.refs();
3494 if (!RefList
.empty()) {
3497 for (auto &Ref
: RefList
) {
3499 if (Ref
.isReadOnly())
3501 else if (Ref
.isWriteOnly())
3502 Out
<< "writeonly ";
3503 Out
<< "^" << Machine
.getGUIDSlot(Ref
.getGUID());
3511 void AssemblyWriter::printSummaryInfo(unsigned Slot
, const ValueInfo
&VI
) {
3512 Out
<< "^" << Slot
<< " = gv: (";
3513 if (!VI
.name().empty())
3514 Out
<< "name: \"" << VI
.name() << "\"";
3516 Out
<< "guid: " << VI
.getGUID();
3517 if (!VI
.getSummaryList().empty()) {
3518 Out
<< ", summaries: (";
3520 for (auto &Summary
: VI
.getSummaryList()) {
3522 printSummary(*Summary
);
3527 if (!VI
.name().empty())
3528 Out
<< " ; guid = " << VI
.getGUID();
3532 static void printMetadataIdentifier(StringRef Name
,
3533 formatted_raw_ostream
&Out
) {
3535 Out
<< "<empty name> ";
3537 unsigned char FirstC
= static_cast<unsigned char>(Name
[0]);
3538 if (isalpha(FirstC
) || FirstC
== '-' || FirstC
== '$' || FirstC
== '.' ||
3542 Out
<< '\\' << hexdigit(FirstC
>> 4) << hexdigit(FirstC
& 0x0F);
3543 for (unsigned i
= 1, e
= Name
.size(); i
!= e
; ++i
) {
3544 unsigned char C
= Name
[i
];
3545 if (isalnum(C
) || C
== '-' || C
== '$' || C
== '.' || C
== '_')
3548 Out
<< '\\' << hexdigit(C
>> 4) << hexdigit(C
& 0x0F);
3553 void AssemblyWriter::printNamedMDNode(const NamedMDNode
*NMD
) {
3555 printMetadataIdentifier(NMD
->getName(), Out
);
3557 for (unsigned i
= 0, e
= NMD
->getNumOperands(); i
!= e
; ++i
) {
3561 // Write DIExpressions inline.
3562 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3563 MDNode
*Op
= NMD
->getOperand(i
);
3564 if (auto *Expr
= dyn_cast
<DIExpression
>(Op
)) {
3565 writeDIExpression(Out
, Expr
, AsmWriterContext::getEmpty());
3569 int Slot
= Machine
.getMetadataSlot(Op
);
3578 static void PrintVisibility(GlobalValue::VisibilityTypes Vis
,
3579 formatted_raw_ostream
&Out
) {
3581 case GlobalValue::DefaultVisibility
: break;
3582 case GlobalValue::HiddenVisibility
: Out
<< "hidden "; break;
3583 case GlobalValue::ProtectedVisibility
: Out
<< "protected "; break;
3587 static void PrintDSOLocation(const GlobalValue
&GV
,
3588 formatted_raw_ostream
&Out
) {
3589 if (GV
.isDSOLocal() && !GV
.isImplicitDSOLocal())
3590 Out
<< "dso_local ";
3593 static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT
,
3594 formatted_raw_ostream
&Out
) {
3596 case GlobalValue::DefaultStorageClass
: break;
3597 case GlobalValue::DLLImportStorageClass
: Out
<< "dllimport "; break;
3598 case GlobalValue::DLLExportStorageClass
: Out
<< "dllexport "; break;
3602 static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM
,
3603 formatted_raw_ostream
&Out
) {
3605 case GlobalVariable::NotThreadLocal
:
3607 case GlobalVariable::GeneralDynamicTLSModel
:
3608 Out
<< "thread_local ";
3610 case GlobalVariable::LocalDynamicTLSModel
:
3611 Out
<< "thread_local(localdynamic) ";
3613 case GlobalVariable::InitialExecTLSModel
:
3614 Out
<< "thread_local(initialexec) ";
3616 case GlobalVariable::LocalExecTLSModel
:
3617 Out
<< "thread_local(localexec) ";
3622 static StringRef
getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA
) {
3624 case GlobalVariable::UnnamedAddr::None
:
3626 case GlobalVariable::UnnamedAddr::Local
:
3627 return "local_unnamed_addr";
3628 case GlobalVariable::UnnamedAddr::Global
:
3629 return "unnamed_addr";
3631 llvm_unreachable("Unknown UnnamedAddr");
3634 static void maybePrintComdat(formatted_raw_ostream
&Out
,
3635 const GlobalObject
&GO
) {
3636 const Comdat
*C
= GO
.getComdat();
3640 if (isa
<GlobalVariable
>(GO
))
3644 if (GO
.getName() == C
->getName())
3648 PrintLLVMName(Out
, C
->getName(), ComdatPrefix
);
3652 void AssemblyWriter::printGlobal(const GlobalVariable
*GV
) {
3653 if (GV
->isMaterializable())
3654 Out
<< "; Materializable\n";
3656 AsmWriterContext
WriterCtx(&TypePrinter
, &Machine
, GV
->getParent());
3657 WriteAsOperandInternal(Out
, GV
, WriterCtx
);
3660 if (!GV
->hasInitializer() && GV
->hasExternalLinkage())
3663 Out
<< getLinkageNameWithSpace(GV
->getLinkage());
3664 PrintDSOLocation(*GV
, Out
);
3665 PrintVisibility(GV
->getVisibility(), Out
);
3666 PrintDLLStorageClass(GV
->getDLLStorageClass(), Out
);
3667 PrintThreadLocalModel(GV
->getThreadLocalMode(), Out
);
3668 StringRef UA
= getUnnamedAddrEncoding(GV
->getUnnamedAddr());
3672 if (unsigned AddressSpace
= GV
->getType()->getAddressSpace())
3673 Out
<< "addrspace(" << AddressSpace
<< ") ";
3674 if (GV
->isExternallyInitialized()) Out
<< "externally_initialized ";
3675 Out
<< (GV
->isConstant() ? "constant " : "global ");
3676 TypePrinter
.print(GV
->getValueType(), Out
);
3678 if (GV
->hasInitializer()) {
3680 writeOperand(GV
->getInitializer(), false);
3683 if (GV
->hasSection()) {
3684 Out
<< ", section \"";
3685 printEscapedString(GV
->getSection(), Out
);
3688 if (GV
->hasPartition()) {
3689 Out
<< ", partition \"";
3690 printEscapedString(GV
->getPartition(), Out
);
3693 if (auto CM
= GV
->getCodeModel()) {
3694 Out
<< ", code_model \"";
3696 case CodeModel::Tiny
:
3699 case CodeModel::Small
:
3702 case CodeModel::Kernel
:
3705 case CodeModel::Medium
:
3708 case CodeModel::Large
:
3715 using SanitizerMetadata
= llvm::GlobalValue::SanitizerMetadata
;
3716 if (GV
->hasSanitizerMetadata()) {
3717 SanitizerMetadata MD
= GV
->getSanitizerMetadata();
3719 Out
<< ", no_sanitize_address";
3721 Out
<< ", no_sanitize_hwaddress";
3723 Out
<< ", sanitize_memtag";
3725 Out
<< ", sanitize_address_dyninit";
3728 maybePrintComdat(Out
, *GV
);
3729 if (MaybeAlign A
= GV
->getAlign())
3730 Out
<< ", align " << A
->value();
3732 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
3733 GV
->getAllMetadata(MDs
);
3734 printMetadataAttachments(MDs
, ", ");
3736 auto Attrs
= GV
->getAttributes();
3737 if (Attrs
.hasAttributes())
3738 Out
<< " #" << Machine
.getAttributeGroupSlot(Attrs
);
3740 printInfoComment(*GV
);
3743 void AssemblyWriter::printAlias(const GlobalAlias
*GA
) {
3744 if (GA
->isMaterializable())
3745 Out
<< "; Materializable\n";
3747 AsmWriterContext
WriterCtx(&TypePrinter
, &Machine
, GA
->getParent());
3748 WriteAsOperandInternal(Out
, GA
, WriterCtx
);
3751 Out
<< getLinkageNameWithSpace(GA
->getLinkage());
3752 PrintDSOLocation(*GA
, Out
);
3753 PrintVisibility(GA
->getVisibility(), Out
);
3754 PrintDLLStorageClass(GA
->getDLLStorageClass(), Out
);
3755 PrintThreadLocalModel(GA
->getThreadLocalMode(), Out
);
3756 StringRef UA
= getUnnamedAddrEncoding(GA
->getUnnamedAddr());
3762 TypePrinter
.print(GA
->getValueType(), Out
);
3765 if (const Constant
*Aliasee
= GA
->getAliasee()) {
3766 writeOperand(Aliasee
, !isa
<ConstantExpr
>(Aliasee
));
3768 TypePrinter
.print(GA
->getType(), Out
);
3769 Out
<< " <<NULL ALIASEE>>";
3772 if (GA
->hasPartition()) {
3773 Out
<< ", partition \"";
3774 printEscapedString(GA
->getPartition(), Out
);
3778 printInfoComment(*GA
);
3782 void AssemblyWriter::printIFunc(const GlobalIFunc
*GI
) {
3783 if (GI
->isMaterializable())
3784 Out
<< "; Materializable\n";
3786 AsmWriterContext
WriterCtx(&TypePrinter
, &Machine
, GI
->getParent());
3787 WriteAsOperandInternal(Out
, GI
, WriterCtx
);
3790 Out
<< getLinkageNameWithSpace(GI
->getLinkage());
3791 PrintDSOLocation(*GI
, Out
);
3792 PrintVisibility(GI
->getVisibility(), Out
);
3796 TypePrinter
.print(GI
->getValueType(), Out
);
3799 if (const Constant
*Resolver
= GI
->getResolver()) {
3800 writeOperand(Resolver
, !isa
<ConstantExpr
>(Resolver
));
3802 TypePrinter
.print(GI
->getType(), Out
);
3803 Out
<< " <<NULL RESOLVER>>";
3806 if (GI
->hasPartition()) {
3807 Out
<< ", partition \"";
3808 printEscapedString(GI
->getPartition(), Out
);
3812 printInfoComment(*GI
);
3816 void AssemblyWriter::printComdat(const Comdat
*C
) {
3820 void AssemblyWriter::printTypeIdentities() {
3821 if (TypePrinter
.empty())
3826 // Emit all numbered types.
3827 auto &NumberedTypes
= TypePrinter
.getNumberedTypes();
3828 for (unsigned I
= 0, E
= NumberedTypes
.size(); I
!= E
; ++I
) {
3829 Out
<< '%' << I
<< " = type ";
3831 // Make sure we print out at least one level of the type structure, so
3832 // that we do not get %2 = type %2
3833 TypePrinter
.printStructBody(NumberedTypes
[I
], Out
);
3837 auto &NamedTypes
= TypePrinter
.getNamedTypes();
3838 for (StructType
*NamedType
: NamedTypes
) {
3839 PrintLLVMName(Out
, NamedType
->getName(), LocalPrefix
);
3842 // Make sure we print out at least one level of the type structure, so
3843 // that we do not get %FILE = type %FILE
3844 TypePrinter
.printStructBody(NamedType
, Out
);
3849 /// printFunction - Print all aspects of a function.
3850 void AssemblyWriter::printFunction(const Function
*F
) {
3851 bool ConvertBack
= F
->IsNewDbgInfoFormat
;
3853 const_cast<Function
*>(F
)->convertFromNewDbgValues();
3854 if (AnnotationWriter
) AnnotationWriter
->emitFunctionAnnot(F
, Out
);
3856 if (F
->isMaterializable())
3857 Out
<< "; Materializable\n";
3859 const AttributeList
&Attrs
= F
->getAttributes();
3860 if (Attrs
.hasFnAttrs()) {
3861 AttributeSet AS
= Attrs
.getFnAttrs();
3862 std::string AttrStr
;
3864 for (const Attribute
&Attr
: AS
) {
3865 if (!Attr
.isStringAttribute()) {
3866 if (!AttrStr
.empty()) AttrStr
+= ' ';
3867 AttrStr
+= Attr
.getAsString();
3871 if (!AttrStr
.empty())
3872 Out
<< "; Function Attrs: " << AttrStr
<< '\n';
3875 Machine
.incorporateFunction(F
);
3877 if (F
->isDeclaration()) {
3879 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
3880 F
->getAllMetadata(MDs
);
3881 printMetadataAttachments(MDs
, " ");
3886 Out
<< getLinkageNameWithSpace(F
->getLinkage());
3887 PrintDSOLocation(*F
, Out
);
3888 PrintVisibility(F
->getVisibility(), Out
);
3889 PrintDLLStorageClass(F
->getDLLStorageClass(), Out
);
3891 // Print the calling convention.
3892 if (F
->getCallingConv() != CallingConv::C
) {
3893 PrintCallingConv(F
->getCallingConv(), Out
);
3897 FunctionType
*FT
= F
->getFunctionType();
3898 if (Attrs
.hasRetAttrs())
3899 Out
<< Attrs
.getAsString(AttributeList::ReturnIndex
) << ' ';
3900 TypePrinter
.print(F
->getReturnType(), Out
);
3901 AsmWriterContext
WriterCtx(&TypePrinter
, &Machine
, F
->getParent());
3903 WriteAsOperandInternal(Out
, F
, WriterCtx
);
3906 // Loop over the arguments, printing them...
3907 if (F
->isDeclaration() && !IsForDebug
) {
3908 // We're only interested in the type here - don't print argument names.
3909 for (unsigned I
= 0, E
= FT
->getNumParams(); I
!= E
; ++I
) {
3910 // Insert commas as we go... the first arg doesn't get a comma
3914 TypePrinter
.print(FT
->getParamType(I
), Out
);
3916 AttributeSet ArgAttrs
= Attrs
.getParamAttrs(I
);
3917 if (ArgAttrs
.hasAttributes()) {
3919 writeAttributeSet(ArgAttrs
);
3923 // The arguments are meaningful here, print them in detail.
3924 for (const Argument
&Arg
: F
->args()) {
3925 // Insert commas as we go... the first arg doesn't get a comma
3926 if (Arg
.getArgNo() != 0)
3928 printArgument(&Arg
, Attrs
.getParamAttrs(Arg
.getArgNo()));
3932 // Finish printing arguments...
3933 if (FT
->isVarArg()) {
3934 if (FT
->getNumParams()) Out
<< ", ";
3935 Out
<< "..."; // Output varargs portion of signature!
3938 StringRef UA
= getUnnamedAddrEncoding(F
->getUnnamedAddr());
3941 // We print the function address space if it is non-zero or if we are writing
3942 // a module with a non-zero program address space or if there is no valid
3943 // Module* so that the file can be parsed without the datalayout string.
3944 const Module
*Mod
= F
->getParent();
3945 if (F
->getAddressSpace() != 0 || !Mod
||
3946 Mod
->getDataLayout().getProgramAddressSpace() != 0)
3947 Out
<< " addrspace(" << F
->getAddressSpace() << ")";
3948 if (Attrs
.hasFnAttrs())
3949 Out
<< " #" << Machine
.getAttributeGroupSlot(Attrs
.getFnAttrs());
3950 if (F
->hasSection()) {
3951 Out
<< " section \"";
3952 printEscapedString(F
->getSection(), Out
);
3955 if (F
->hasPartition()) {
3956 Out
<< " partition \"";
3957 printEscapedString(F
->getPartition(), Out
);
3960 maybePrintComdat(Out
, *F
);
3961 if (MaybeAlign A
= F
->getAlign())
3962 Out
<< " align " << A
->value();
3964 Out
<< " gc \"" << F
->getGC() << '"';
3965 if (F
->hasPrefixData()) {
3967 writeOperand(F
->getPrefixData(), true);
3969 if (F
->hasPrologueData()) {
3970 Out
<< " prologue ";
3971 writeOperand(F
->getPrologueData(), true);
3973 if (F
->hasPersonalityFn()) {
3974 Out
<< " personality ";
3975 writeOperand(F
->getPersonalityFn(), /*PrintType=*/true);
3978 if (F
->isDeclaration()) {
3981 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> MDs
;
3982 F
->getAllMetadata(MDs
);
3983 printMetadataAttachments(MDs
, " ");
3986 // Output all of the function's basic blocks.
3987 for (const BasicBlock
&BB
: *F
)
3988 printBasicBlock(&BB
);
3990 // Output the function's use-lists.
3997 const_cast<Function
*>(F
)->convertToNewDbgValues();
3998 Machine
.purgeFunction();
4001 /// printArgument - This member is called for every argument that is passed into
4002 /// the function. Simply print it out
4003 void AssemblyWriter::printArgument(const Argument
*Arg
, AttributeSet Attrs
) {
4005 TypePrinter
.print(Arg
->getType(), Out
);
4007 // Output parameter attributes list
4008 if (Attrs
.hasAttributes()) {
4010 writeAttributeSet(Attrs
);
4013 // Output name, if available...
4014 if (Arg
->hasName()) {
4016 PrintLLVMName(Out
, Arg
);
4018 int Slot
= Machine
.getLocalSlot(Arg
);
4019 assert(Slot
!= -1 && "expect argument in function here");
4020 Out
<< " %" << Slot
;
4024 /// printBasicBlock - This member is called for each basic block in a method.
4025 void AssemblyWriter::printBasicBlock(const BasicBlock
*BB
) {
4026 bool IsEntryBlock
= BB
->getParent() && BB
->isEntryBlock();
4027 if (BB
->hasName()) { // Print out the label if it exists...
4029 PrintLLVMName(Out
, BB
->getName(), LabelPrefix
);
4031 } else if (!IsEntryBlock
) {
4033 int Slot
= Machine
.getLocalSlot(BB
);
4040 if (!IsEntryBlock
) {
4041 // Output predecessors for the block.
4042 Out
.PadToColumn(50);
4044 const_pred_iterator PI
= pred_begin(BB
), PE
= pred_end(BB
);
4047 Out
<< " No predecessors!";
4050 writeOperand(*PI
, false);
4051 for (++PI
; PI
!= PE
; ++PI
) {
4053 writeOperand(*PI
, false);
4060 if (AnnotationWriter
) AnnotationWriter
->emitBasicBlockStartAnnot(BB
, Out
);
4062 // Output all of the instructions in the basic block...
4063 for (const Instruction
&I
: *BB
) {
4064 printInstructionLine(I
);
4067 if (AnnotationWriter
) AnnotationWriter
->emitBasicBlockEndAnnot(BB
, Out
);
4070 /// printInstructionLine - Print an instruction and a newline character.
4071 void AssemblyWriter::printInstructionLine(const Instruction
&I
) {
4072 printInstruction(I
);
4076 /// printGCRelocateComment - print comment after call to the gc.relocate
4077 /// intrinsic indicating base and derived pointer names.
4078 void AssemblyWriter::printGCRelocateComment(const GCRelocateInst
&Relocate
) {
4080 writeOperand(Relocate
.getBasePtr(), false);
4082 writeOperand(Relocate
.getDerivedPtr(), false);
4086 /// printInfoComment - Print a little comment after the instruction indicating
4087 /// which slot it occupies.
4088 void AssemblyWriter::printInfoComment(const Value
&V
) {
4089 if (const auto *Relocate
= dyn_cast
<GCRelocateInst
>(&V
))
4090 printGCRelocateComment(*Relocate
);
4092 if (AnnotationWriter
) {
4093 AnnotationWriter
->printInfoComment(V
, Out
);
4094 } else if (const Instruction
*I
= dyn_cast
<Instruction
>(&V
)) {
4096 // In the new, experimental DPValue representation of debug-info, print
4097 // out which instructions have DPMarkers and where they are.
4098 Out
<< "; dbgmarker @ " << I
->DbgMarker
;
4103 static void maybePrintCallAddrSpace(const Value
*Operand
, const Instruction
*I
,
4105 // We print the address space of the call if it is non-zero.
4106 if (Operand
== nullptr) {
4107 Out
<< " <cannot get addrspace!>";
4110 unsigned CallAddrSpace
= Operand
->getType()->getPointerAddressSpace();
4111 bool PrintAddrSpace
= CallAddrSpace
!= 0;
4112 if (!PrintAddrSpace
) {
4113 const Module
*Mod
= getModuleFromVal(I
);
4114 // We also print it if it is zero but not equal to the program address space
4115 // or if we can't find a valid Module* to make it possible to parse
4116 // the resulting file even without a datalayout string.
4117 if (!Mod
|| Mod
->getDataLayout().getProgramAddressSpace() != 0)
4118 PrintAddrSpace
= true;
4121 Out
<< " addrspace(" << CallAddrSpace
<< ")";
4124 // This member is called for each Instruction in a function..
4125 void AssemblyWriter::printInstruction(const Instruction
&I
) {
4126 if (AnnotationWriter
) AnnotationWriter
->emitInstructionAnnot(&I
, Out
);
4128 // Print out indentation for an instruction.
4131 // Print out name if it exists...
4133 PrintLLVMName(Out
, &I
);
4135 } else if (!I
.getType()->isVoidTy()) {
4136 // Print out the def slot taken.
4137 int SlotNum
= Machine
.getLocalSlot(&I
);
4139 Out
<< "<badref> = ";
4141 Out
<< '%' << SlotNum
<< " = ";
4144 if (const CallInst
*CI
= dyn_cast
<CallInst
>(&I
)) {
4145 if (CI
->isMustTailCall())
4147 else if (CI
->isTailCall())
4149 else if (CI
->isNoTailCall())
4153 // Print out the opcode...
4154 Out
<< I
.getOpcodeName();
4156 // If this is an atomic load or store, print out the atomic marker.
4157 if ((isa
<LoadInst
>(I
) && cast
<LoadInst
>(I
).isAtomic()) ||
4158 (isa
<StoreInst
>(I
) && cast
<StoreInst
>(I
).isAtomic()))
4161 if (isa
<AtomicCmpXchgInst
>(I
) && cast
<AtomicCmpXchgInst
>(I
).isWeak())
4164 // If this is a volatile operation, print out the volatile marker.
4165 if ((isa
<LoadInst
>(I
) && cast
<LoadInst
>(I
).isVolatile()) ||
4166 (isa
<StoreInst
>(I
) && cast
<StoreInst
>(I
).isVolatile()) ||
4167 (isa
<AtomicCmpXchgInst
>(I
) && cast
<AtomicCmpXchgInst
>(I
).isVolatile()) ||
4168 (isa
<AtomicRMWInst
>(I
) && cast
<AtomicRMWInst
>(I
).isVolatile()))
4171 // Print out optimization information.
4172 WriteOptimizationInfo(Out
, &I
);
4174 // Print out the compare instruction predicates
4175 if (const CmpInst
*CI
= dyn_cast
<CmpInst
>(&I
))
4176 Out
<< ' ' << CI
->getPredicate();
4178 // Print out the atomicrmw operation
4179 if (const AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(&I
))
4180 Out
<< ' ' << AtomicRMWInst::getOperationName(RMWI
->getOperation());
4182 // Print out the type of the operands...
4183 const Value
*Operand
= I
.getNumOperands() ? I
.getOperand(0) : nullptr;
4185 // Special case conditional branches to swizzle the condition out to the front
4186 if (isa
<BranchInst
>(I
) && cast
<BranchInst
>(I
).isConditional()) {
4187 const BranchInst
&BI(cast
<BranchInst
>(I
));
4189 writeOperand(BI
.getCondition(), true);
4191 writeOperand(BI
.getSuccessor(0), true);
4193 writeOperand(BI
.getSuccessor(1), true);
4195 } else if (isa
<SwitchInst
>(I
)) {
4196 const SwitchInst
& SI(cast
<SwitchInst
>(I
));
4197 // Special case switch instruction to get formatting nice and correct.
4199 writeOperand(SI
.getCondition(), true);
4201 writeOperand(SI
.getDefaultDest(), true);
4203 for (auto Case
: SI
.cases()) {
4205 writeOperand(Case
.getCaseValue(), true);
4207 writeOperand(Case
.getCaseSuccessor(), true);
4210 } else if (isa
<IndirectBrInst
>(I
)) {
4211 // Special case indirectbr instruction to get formatting nice and correct.
4213 writeOperand(Operand
, true);
4216 for (unsigned i
= 1, e
= I
.getNumOperands(); i
!= e
; ++i
) {
4219 writeOperand(I
.getOperand(i
), true);
4222 } else if (const PHINode
*PN
= dyn_cast
<PHINode
>(&I
)) {
4224 TypePrinter
.print(I
.getType(), Out
);
4227 for (unsigned op
= 0, Eop
= PN
->getNumIncomingValues(); op
< Eop
; ++op
) {
4228 if (op
) Out
<< ", ";
4230 writeOperand(PN
->getIncomingValue(op
), false); Out
<< ", ";
4231 writeOperand(PN
->getIncomingBlock(op
), false); Out
<< " ]";
4233 } else if (const ExtractValueInst
*EVI
= dyn_cast
<ExtractValueInst
>(&I
)) {
4235 writeOperand(I
.getOperand(0), true);
4236 for (unsigned i
: EVI
->indices())
4238 } else if (const InsertValueInst
*IVI
= dyn_cast
<InsertValueInst
>(&I
)) {
4240 writeOperand(I
.getOperand(0), true); Out
<< ", ";
4241 writeOperand(I
.getOperand(1), true);
4242 for (unsigned i
: IVI
->indices())
4244 } else if (const LandingPadInst
*LPI
= dyn_cast
<LandingPadInst
>(&I
)) {
4246 TypePrinter
.print(I
.getType(), Out
);
4247 if (LPI
->isCleanup() || LPI
->getNumClauses() != 0)
4250 if (LPI
->isCleanup())
4253 for (unsigned i
= 0, e
= LPI
->getNumClauses(); i
!= e
; ++i
) {
4254 if (i
!= 0 || LPI
->isCleanup()) Out
<< "\n";
4255 if (LPI
->isCatch(i
))
4260 writeOperand(LPI
->getClause(i
), true);
4262 } else if (const auto *CatchSwitch
= dyn_cast
<CatchSwitchInst
>(&I
)) {
4264 writeOperand(CatchSwitch
->getParentPad(), /*PrintType=*/false);
4267 for (const BasicBlock
*PadBB
: CatchSwitch
->handlers()) {
4270 writeOperand(PadBB
, /*PrintType=*/true);
4274 if (const BasicBlock
*UnwindDest
= CatchSwitch
->getUnwindDest())
4275 writeOperand(UnwindDest
, /*PrintType=*/true);
4278 } else if (const auto *FPI
= dyn_cast
<FuncletPadInst
>(&I
)) {
4280 writeOperand(FPI
->getParentPad(), /*PrintType=*/false);
4282 for (unsigned Op
= 0, NumOps
= FPI
->arg_size(); Op
< NumOps
; ++Op
) {
4285 writeOperand(FPI
->getArgOperand(Op
), /*PrintType=*/true);
4288 } else if (isa
<ReturnInst
>(I
) && !Operand
) {
4290 } else if (const auto *CRI
= dyn_cast
<CatchReturnInst
>(&I
)) {
4292 writeOperand(CRI
->getOperand(0), /*PrintType=*/false);
4295 writeOperand(CRI
->getOperand(1), /*PrintType=*/true);
4296 } else if (const auto *CRI
= dyn_cast
<CleanupReturnInst
>(&I
)) {
4298 writeOperand(CRI
->getOperand(0), /*PrintType=*/false);
4301 if (CRI
->hasUnwindDest())
4302 writeOperand(CRI
->getOperand(1), /*PrintType=*/true);
4305 } else if (const CallInst
*CI
= dyn_cast
<CallInst
>(&I
)) {
4306 // Print the calling convention being used.
4307 if (CI
->getCallingConv() != CallingConv::C
) {
4309 PrintCallingConv(CI
->getCallingConv(), Out
);
4312 Operand
= CI
->getCalledOperand();
4313 FunctionType
*FTy
= CI
->getFunctionType();
4314 Type
*RetTy
= FTy
->getReturnType();
4315 const AttributeList
&PAL
= CI
->getAttributes();
4317 if (PAL
.hasRetAttrs())
4318 Out
<< ' ' << PAL
.getAsString(AttributeList::ReturnIndex
);
4320 // Only print addrspace(N) if necessary:
4321 maybePrintCallAddrSpace(Operand
, &I
, Out
);
4323 // If possible, print out the short form of the call instruction. We can
4324 // only do this if the first argument is a pointer to a nonvararg function,
4325 // and if the return type is not a pointer to a function.
4327 TypePrinter
.print(FTy
->isVarArg() ? FTy
: RetTy
, Out
);
4329 writeOperand(Operand
, false);
4331 for (unsigned op
= 0, Eop
= CI
->arg_size(); op
< Eop
; ++op
) {
4334 writeParamOperand(CI
->getArgOperand(op
), PAL
.getParamAttrs(op
));
4337 // Emit an ellipsis if this is a musttail call in a vararg function. This
4338 // is only to aid readability, musttail calls forward varargs by default.
4339 if (CI
->isMustTailCall() && CI
->getParent() &&
4340 CI
->getParent()->getParent() &&
4341 CI
->getParent()->getParent()->isVarArg()) {
4342 if (CI
->arg_size() > 0)
4348 if (PAL
.hasFnAttrs())
4349 Out
<< " #" << Machine
.getAttributeGroupSlot(PAL
.getFnAttrs());
4351 writeOperandBundles(CI
);
4352 } else if (const InvokeInst
*II
= dyn_cast
<InvokeInst
>(&I
)) {
4353 Operand
= II
->getCalledOperand();
4354 FunctionType
*FTy
= II
->getFunctionType();
4355 Type
*RetTy
= FTy
->getReturnType();
4356 const AttributeList
&PAL
= II
->getAttributes();
4358 // Print the calling convention being used.
4359 if (II
->getCallingConv() != CallingConv::C
) {
4361 PrintCallingConv(II
->getCallingConv(), Out
);
4364 if (PAL
.hasRetAttrs())
4365 Out
<< ' ' << PAL
.getAsString(AttributeList::ReturnIndex
);
4367 // Only print addrspace(N) if necessary:
4368 maybePrintCallAddrSpace(Operand
, &I
, Out
);
4370 // If possible, print out the short form of the invoke instruction. We can
4371 // only do this if the first argument is a pointer to a nonvararg function,
4372 // and if the return type is not a pointer to a function.
4375 TypePrinter
.print(FTy
->isVarArg() ? FTy
: RetTy
, Out
);
4377 writeOperand(Operand
, false);
4379 for (unsigned op
= 0, Eop
= II
->arg_size(); op
< Eop
; ++op
) {
4382 writeParamOperand(II
->getArgOperand(op
), PAL
.getParamAttrs(op
));
4386 if (PAL
.hasFnAttrs())
4387 Out
<< " #" << Machine
.getAttributeGroupSlot(PAL
.getFnAttrs());
4389 writeOperandBundles(II
);
4392 writeOperand(II
->getNormalDest(), true);
4394 writeOperand(II
->getUnwindDest(), true);
4395 } else if (const CallBrInst
*CBI
= dyn_cast
<CallBrInst
>(&I
)) {
4396 Operand
= CBI
->getCalledOperand();
4397 FunctionType
*FTy
= CBI
->getFunctionType();
4398 Type
*RetTy
= FTy
->getReturnType();
4399 const AttributeList
&PAL
= CBI
->getAttributes();
4401 // Print the calling convention being used.
4402 if (CBI
->getCallingConv() != CallingConv::C
) {
4404 PrintCallingConv(CBI
->getCallingConv(), Out
);
4407 if (PAL
.hasRetAttrs())
4408 Out
<< ' ' << PAL
.getAsString(AttributeList::ReturnIndex
);
4410 // If possible, print out the short form of the callbr instruction. We can
4411 // only do this if the first argument is a pointer to a nonvararg function,
4412 // and if the return type is not a pointer to a function.
4415 TypePrinter
.print(FTy
->isVarArg() ? FTy
: RetTy
, Out
);
4417 writeOperand(Operand
, false);
4419 for (unsigned op
= 0, Eop
= CBI
->arg_size(); op
< Eop
; ++op
) {
4422 writeParamOperand(CBI
->getArgOperand(op
), PAL
.getParamAttrs(op
));
4426 if (PAL
.hasFnAttrs())
4427 Out
<< " #" << Machine
.getAttributeGroupSlot(PAL
.getFnAttrs());
4429 writeOperandBundles(CBI
);
4432 writeOperand(CBI
->getDefaultDest(), true);
4434 for (unsigned i
= 0, e
= CBI
->getNumIndirectDests(); i
!= e
; ++i
) {
4437 writeOperand(CBI
->getIndirectDest(i
), true);
4440 } else if (const AllocaInst
*AI
= dyn_cast
<AllocaInst
>(&I
)) {
4442 if (AI
->isUsedWithInAlloca())
4444 if (AI
->isSwiftError())
4445 Out
<< "swifterror ";
4446 TypePrinter
.print(AI
->getAllocatedType(), Out
);
4448 // Explicitly write the array size if the code is broken, if it's an array
4449 // allocation, or if the type is not canonical for scalar allocations. The
4450 // latter case prevents the type from mutating when round-tripping through
4452 if (!AI
->getArraySize() || AI
->isArrayAllocation() ||
4453 !AI
->getArraySize()->getType()->isIntegerTy(32)) {
4455 writeOperand(AI
->getArraySize(), true);
4457 if (MaybeAlign A
= AI
->getAlign()) {
4458 Out
<< ", align " << A
->value();
4461 unsigned AddrSpace
= AI
->getAddressSpace();
4462 if (AddrSpace
!= 0) {
4463 Out
<< ", addrspace(" << AddrSpace
<< ')';
4465 } else if (isa
<CastInst
>(I
)) {
4468 writeOperand(Operand
, true); // Work with broken code
4471 TypePrinter
.print(I
.getType(), Out
);
4472 } else if (isa
<VAArgInst
>(I
)) {
4475 writeOperand(Operand
, true); // Work with broken code
4478 TypePrinter
.print(I
.getType(), Out
);
4479 } else if (Operand
) { // Print the normal way.
4480 if (const auto *GEP
= dyn_cast
<GetElementPtrInst
>(&I
)) {
4482 TypePrinter
.print(GEP
->getSourceElementType(), Out
);
4484 } else if (const auto *LI
= dyn_cast
<LoadInst
>(&I
)) {
4486 TypePrinter
.print(LI
->getType(), Out
);
4490 // PrintAllTypes - Instructions who have operands of all the same type
4491 // omit the type from all but the first operand. If the instruction has
4492 // different type operands (for example br), then they are all printed.
4493 bool PrintAllTypes
= false;
4494 Type
*TheType
= Operand
->getType();
4496 // Select, Store, ShuffleVector, CmpXchg and AtomicRMW always print all
4498 if (isa
<SelectInst
>(I
) || isa
<StoreInst
>(I
) || isa
<ShuffleVectorInst
>(I
) ||
4499 isa
<ReturnInst
>(I
) || isa
<AtomicCmpXchgInst
>(I
) ||
4500 isa
<AtomicRMWInst
>(I
)) {
4501 PrintAllTypes
= true;
4503 for (unsigned i
= 1, E
= I
.getNumOperands(); i
!= E
; ++i
) {
4504 Operand
= I
.getOperand(i
);
4505 // note that Operand shouldn't be null, but the test helps make dump()
4506 // more tolerant of malformed IR
4507 if (Operand
&& Operand
->getType() != TheType
) {
4508 PrintAllTypes
= true; // We have differing types! Print them all!
4514 if (!PrintAllTypes
) {
4516 TypePrinter
.print(TheType
, Out
);
4520 for (unsigned i
= 0, E
= I
.getNumOperands(); i
!= E
; ++i
) {
4522 writeOperand(I
.getOperand(i
), PrintAllTypes
);
4526 // Print atomic ordering/alignment for memory operations
4527 if (const LoadInst
*LI
= dyn_cast
<LoadInst
>(&I
)) {
4529 writeAtomic(LI
->getContext(), LI
->getOrdering(), LI
->getSyncScopeID());
4530 if (MaybeAlign A
= LI
->getAlign())
4531 Out
<< ", align " << A
->value();
4532 } else if (const StoreInst
*SI
= dyn_cast
<StoreInst
>(&I
)) {
4534 writeAtomic(SI
->getContext(), SI
->getOrdering(), SI
->getSyncScopeID());
4535 if (MaybeAlign A
= SI
->getAlign())
4536 Out
<< ", align " << A
->value();
4537 } else if (const AtomicCmpXchgInst
*CXI
= dyn_cast
<AtomicCmpXchgInst
>(&I
)) {
4538 writeAtomicCmpXchg(CXI
->getContext(), CXI
->getSuccessOrdering(),
4539 CXI
->getFailureOrdering(), CXI
->getSyncScopeID());
4540 Out
<< ", align " << CXI
->getAlign().value();
4541 } else if (const AtomicRMWInst
*RMWI
= dyn_cast
<AtomicRMWInst
>(&I
)) {
4542 writeAtomic(RMWI
->getContext(), RMWI
->getOrdering(),
4543 RMWI
->getSyncScopeID());
4544 Out
<< ", align " << RMWI
->getAlign().value();
4545 } else if (const FenceInst
*FI
= dyn_cast
<FenceInst
>(&I
)) {
4546 writeAtomic(FI
->getContext(), FI
->getOrdering(), FI
->getSyncScopeID());
4547 } else if (const ShuffleVectorInst
*SVI
= dyn_cast
<ShuffleVectorInst
>(&I
)) {
4548 PrintShuffleMask(Out
, SVI
->getType(), SVI
->getShuffleMask());
4551 // Print Metadata info.
4552 SmallVector
<std::pair
<unsigned, MDNode
*>, 4> InstMD
;
4553 I
.getAllMetadata(InstMD
);
4554 printMetadataAttachments(InstMD
, ", ");
4556 // Print a nice comment.
4557 printInfoComment(I
);
4560 void AssemblyWriter::printDPMarker(const DPMarker
&Marker
) {
4561 // There's no formal representation of a DPMarker -- print purely as a
4563 for (const DPValue
&DPI2
: Marker
.StoredDPValues
) {
4568 Out
<< " DPMarker -> { ";
4569 printInstruction(*Marker
.MarkedInstr
);
4574 void AssemblyWriter::printDPValue(const DPValue
&Value
) {
4575 // There's no formal representation of a DPValue -- print purely as a
4579 switch (Value
.getType()) {
4580 case DPValue::LocationType::Value
:
4583 case DPValue::LocationType::Declare
:
4586 case DPValue::LocationType::Assign
:
4590 llvm_unreachable("Tried to print a DPValue with an invalid LocationType!");
4593 auto WriterCtx
= getContext();
4594 WriteAsOperandInternal(Out
, Value
.getRawLocation(), WriterCtx
, true);
4596 WriteAsOperandInternal(Out
, Value
.getVariable(), WriterCtx
, true);
4598 WriteAsOperandInternal(Out
, Value
.getExpression(), WriterCtx
, true);
4600 if (Value
.isDbgAssign()) {
4601 WriteAsOperandInternal(Out
, Value
.getAssignID(), WriterCtx
, true);
4603 WriteAsOperandInternal(Out
, Value
.getRawAddress(), WriterCtx
, true);
4605 WriteAsOperandInternal(Out
, Value
.getAddressExpression(), WriterCtx
, true);
4608 WriteAsOperandInternal(Out
, Value
.getDebugLoc().get(), WriterCtx
, true);
4609 Out
<< " marker @" << Value
.getMarker();
4613 void AssemblyWriter::printMetadataAttachments(
4614 const SmallVectorImpl
<std::pair
<unsigned, MDNode
*>> &MDs
,
4615 StringRef Separator
) {
4619 if (MDNames
.empty())
4620 MDs
[0].second
->getContext().getMDKindNames(MDNames
);
4622 auto WriterCtx
= getContext();
4623 for (const auto &I
: MDs
) {
4624 unsigned Kind
= I
.first
;
4626 if (Kind
< MDNames
.size()) {
4628 printMetadataIdentifier(MDNames
[Kind
], Out
);
4630 Out
<< "!<unknown kind #" << Kind
<< ">";
4632 WriteAsOperandInternal(Out
, I
.second
, WriterCtx
);
4636 void AssemblyWriter::writeMDNode(unsigned Slot
, const MDNode
*Node
) {
4637 Out
<< '!' << Slot
<< " = ";
4638 printMDNodeBody(Node
);
4642 void AssemblyWriter::writeAllMDNodes() {
4643 SmallVector
<const MDNode
*, 16> Nodes
;
4644 Nodes
.resize(Machine
.mdn_size());
4645 for (auto &I
: llvm::make_range(Machine
.mdn_begin(), Machine
.mdn_end()))
4646 Nodes
[I
.second
] = cast
<MDNode
>(I
.first
);
4648 for (unsigned i
= 0, e
= Nodes
.size(); i
!= e
; ++i
) {
4649 writeMDNode(i
, Nodes
[i
]);
4653 void AssemblyWriter::printMDNodeBody(const MDNode
*Node
) {
4654 auto WriterCtx
= getContext();
4655 WriteMDNodeBodyInternal(Out
, Node
, WriterCtx
);
4658 void AssemblyWriter::writeAttribute(const Attribute
&Attr
, bool InAttrGroup
) {
4659 if (!Attr
.isTypeAttribute()) {
4660 Out
<< Attr
.getAsString(InAttrGroup
);
4664 Out
<< Attribute::getNameFromAttrKind(Attr
.getKindAsEnum());
4665 if (Type
*Ty
= Attr
.getValueAsType()) {
4667 TypePrinter
.print(Ty
, Out
);
4672 void AssemblyWriter::writeAttributeSet(const AttributeSet
&AttrSet
,
4674 bool FirstAttr
= true;
4675 for (const auto &Attr
: AttrSet
) {
4678 writeAttribute(Attr
, InAttrGroup
);
4683 void AssemblyWriter::writeAllAttributeGroups() {
4684 std::vector
<std::pair
<AttributeSet
, unsigned>> asVec
;
4685 asVec
.resize(Machine
.as_size());
4687 for (auto &I
: llvm::make_range(Machine
.as_begin(), Machine
.as_end()))
4688 asVec
[I
.second
] = I
;
4690 for (const auto &I
: asVec
)
4691 Out
<< "attributes #" << I
.second
<< " = { "
4692 << I
.first
.getAsString(true) << " }\n";
4695 void AssemblyWriter::printUseListOrder(const Value
*V
,
4696 const std::vector
<unsigned> &Shuffle
) {
4697 bool IsInFunction
= Machine
.getFunction();
4701 Out
<< "uselistorder";
4702 if (const BasicBlock
*BB
= IsInFunction
? nullptr : dyn_cast
<BasicBlock
>(V
)) {
4704 writeOperand(BB
->getParent(), false);
4706 writeOperand(BB
, false);
4709 writeOperand(V
, true);
4713 assert(Shuffle
.size() >= 2 && "Shuffle too small");
4715 for (unsigned I
= 1, E
= Shuffle
.size(); I
!= E
; ++I
)
4716 Out
<< ", " << Shuffle
[I
];
4720 void AssemblyWriter::printUseLists(const Function
*F
) {
4721 auto It
= UseListOrders
.find(F
);
4722 if (It
== UseListOrders
.end())
4725 Out
<< "\n; uselistorder directives\n";
4726 for (const auto &Pair
: It
->second
)
4727 printUseListOrder(Pair
.first
, Pair
.second
);
4730 //===----------------------------------------------------------------------===//
4731 // External Interface declarations
4732 //===----------------------------------------------------------------------===//
4734 void Function::print(raw_ostream
&ROS
, AssemblyAnnotationWriter
*AAW
,
4735 bool ShouldPreserveUseListOrder
,
4736 bool IsForDebug
) const {
4737 SlotTracker
SlotTable(this->getParent());
4738 formatted_raw_ostream
OS(ROS
);
4739 AssemblyWriter
W(OS
, SlotTable
, this->getParent(), AAW
,
4741 ShouldPreserveUseListOrder
);
4742 W
.printFunction(this);
4745 void BasicBlock::print(raw_ostream
&ROS
, AssemblyAnnotationWriter
*AAW
,
4746 bool ShouldPreserveUseListOrder
,
4747 bool IsForDebug
) const {
4748 SlotTracker
SlotTable(this->getParent());
4749 formatted_raw_ostream
OS(ROS
);
4750 AssemblyWriter
W(OS
, SlotTable
, this->getModule(), AAW
,
4752 ShouldPreserveUseListOrder
);
4753 W
.printBasicBlock(this);
4756 void Module::print(raw_ostream
&ROS
, AssemblyAnnotationWriter
*AAW
,
4757 bool ShouldPreserveUseListOrder
, bool IsForDebug
) const {
4758 // RemoveDIs: always print with debug-info in intrinsic format.
4759 bool ConvertAfter
= IsNewDbgInfoFormat
;
4760 if (IsNewDbgInfoFormat
)
4761 const_cast<Module
*>(this)->convertFromNewDbgValues();
4763 SlotTracker
SlotTable(this);
4764 formatted_raw_ostream
OS(ROS
);
4765 AssemblyWriter
W(OS
, SlotTable
, this, AAW
, IsForDebug
,
4766 ShouldPreserveUseListOrder
);
4767 W
.printModule(this);
4770 const_cast<Module
*>(this)->convertToNewDbgValues();
4773 void NamedMDNode::print(raw_ostream
&ROS
, bool IsForDebug
) const {
4774 SlotTracker
SlotTable(getParent());
4775 formatted_raw_ostream
OS(ROS
);
4776 AssemblyWriter
W(OS
, SlotTable
, getParent(), nullptr, IsForDebug
);
4777 W
.printNamedMDNode(this);
4780 void NamedMDNode::print(raw_ostream
&ROS
, ModuleSlotTracker
&MST
,
4781 bool IsForDebug
) const {
4782 std::optional
<SlotTracker
> LocalST
;
4783 SlotTracker
*SlotTable
;
4784 if (auto *ST
= MST
.getMachine())
4787 LocalST
.emplace(getParent());
4788 SlotTable
= &*LocalST
;
4791 formatted_raw_ostream
OS(ROS
);
4792 AssemblyWriter
W(OS
, *SlotTable
, getParent(), nullptr, IsForDebug
);
4793 W
.printNamedMDNode(this);
4796 void Comdat::print(raw_ostream
&ROS
, bool /*IsForDebug*/) const {
4797 PrintLLVMName(ROS
, getName(), ComdatPrefix
);
4798 ROS
<< " = comdat ";
4800 switch (getSelectionKind()) {
4804 case Comdat::ExactMatch
:
4805 ROS
<< "exactmatch";
4807 case Comdat::Largest
:
4810 case Comdat::NoDeduplicate
:
4811 ROS
<< "nodeduplicate";
4813 case Comdat::SameSize
:
4821 void Type::print(raw_ostream
&OS
, bool /*IsForDebug*/, bool NoDetails
) const {
4823 TP
.print(const_cast<Type
*>(this), OS
);
4828 // If the type is a named struct type, print the body as well.
4829 if (StructType
*STy
= dyn_cast
<StructType
>(const_cast<Type
*>(this)))
4830 if (!STy
->isLiteral()) {
4832 TP
.printStructBody(STy
, OS
);
4836 static bool isReferencingMDNode(const Instruction
&I
) {
4837 if (const auto *CI
= dyn_cast
<CallInst
>(&I
))
4838 if (Function
*F
= CI
->getCalledFunction())
4839 if (F
->isIntrinsic())
4840 for (auto &Op
: I
.operands())
4841 if (auto *V
= dyn_cast_or_null
<MetadataAsValue
>(Op
))
4842 if (isa
<MDNode
>(V
->getMetadata()))
4847 void DPMarker::print(raw_ostream
&ROS
, bool IsForDebug
) const {
4849 ModuleSlotTracker
MST(getModuleFromDPI(this), true);
4850 print(ROS
, MST
, IsForDebug
);
4853 void DPValue::print(raw_ostream
&ROS
, bool IsForDebug
) const {
4855 ModuleSlotTracker
MST(getModuleFromDPI(this), true);
4856 print(ROS
, MST
, IsForDebug
);
4859 void DPMarker::print(raw_ostream
&ROS
, ModuleSlotTracker
&MST
,
4860 bool IsForDebug
) const {
4861 // There's no formal representation of a DPMarker -- print purely as a
4863 formatted_raw_ostream
OS(ROS
);
4864 SlotTracker
EmptySlotTable(static_cast<const Module
*>(nullptr));
4865 SlotTracker
&SlotTable
=
4866 MST
.getMachine() ? *MST
.getMachine() : EmptySlotTable
;
4867 auto incorporateFunction
= [&](const Function
*F
) {
4869 MST
.incorporateFunction(*F
);
4871 incorporateFunction(getParent() ? getParent()->getParent() : nullptr);
4872 AssemblyWriter
W(OS
, SlotTable
, getModuleFromDPI(this), nullptr, IsForDebug
);
4873 W
.printDPMarker(*this);
4876 void DPValue::print(raw_ostream
&ROS
, ModuleSlotTracker
&MST
,
4877 bool IsForDebug
) const {
4878 // There's no formal representation of a DPValue -- print purely as a
4880 formatted_raw_ostream
OS(ROS
);
4881 SlotTracker
EmptySlotTable(static_cast<const Module
*>(nullptr));
4882 SlotTracker
&SlotTable
=
4883 MST
.getMachine() ? *MST
.getMachine() : EmptySlotTable
;
4884 auto incorporateFunction
= [&](const Function
*F
) {
4886 MST
.incorporateFunction(*F
);
4888 incorporateFunction(Marker
->getParent() ? Marker
->getParent()->getParent()
4890 AssemblyWriter
W(OS
, SlotTable
, getModuleFromDPI(this), nullptr, IsForDebug
);
4891 W
.printDPValue(*this);
4894 void Value::print(raw_ostream
&ROS
, bool IsForDebug
) const {
4895 bool ShouldInitializeAllMetadata
= false;
4896 if (auto *I
= dyn_cast
<Instruction
>(this))
4897 ShouldInitializeAllMetadata
= isReferencingMDNode(*I
);
4898 else if (isa
<Function
>(this) || isa
<MetadataAsValue
>(this))
4899 ShouldInitializeAllMetadata
= true;
4901 ModuleSlotTracker
MST(getModuleFromVal(this), ShouldInitializeAllMetadata
);
4902 print(ROS
, MST
, IsForDebug
);
4905 void Value::print(raw_ostream
&ROS
, ModuleSlotTracker
&MST
,
4906 bool IsForDebug
) const {
4907 formatted_raw_ostream
OS(ROS
);
4908 SlotTracker
EmptySlotTable(static_cast<const Module
*>(nullptr));
4909 SlotTracker
&SlotTable
=
4910 MST
.getMachine() ? *MST
.getMachine() : EmptySlotTable
;
4911 auto incorporateFunction
= [&](const Function
*F
) {
4913 MST
.incorporateFunction(*F
);
4916 if (const Instruction
*I
= dyn_cast
<Instruction
>(this)) {
4917 incorporateFunction(I
->getParent() ? I
->getParent()->getParent() : nullptr);
4918 AssemblyWriter
W(OS
, SlotTable
, getModuleFromVal(I
), nullptr, IsForDebug
);
4919 W
.printInstruction(*I
);
4920 } else if (const BasicBlock
*BB
= dyn_cast
<BasicBlock
>(this)) {
4921 incorporateFunction(BB
->getParent());
4922 AssemblyWriter
W(OS
, SlotTable
, getModuleFromVal(BB
), nullptr, IsForDebug
);
4923 W
.printBasicBlock(BB
);
4924 } else if (const GlobalValue
*GV
= dyn_cast
<GlobalValue
>(this)) {
4925 AssemblyWriter
W(OS
, SlotTable
, GV
->getParent(), nullptr, IsForDebug
);
4926 if (const GlobalVariable
*V
= dyn_cast
<GlobalVariable
>(GV
))
4928 else if (const Function
*F
= dyn_cast
<Function
>(GV
))
4930 else if (const GlobalAlias
*A
= dyn_cast
<GlobalAlias
>(GV
))
4932 else if (const GlobalIFunc
*I
= dyn_cast
<GlobalIFunc
>(GV
))
4935 llvm_unreachable("Unknown GlobalValue to print out!");
4936 } else if (const MetadataAsValue
*V
= dyn_cast
<MetadataAsValue
>(this)) {
4937 V
->getMetadata()->print(ROS
, MST
, getModuleFromVal(V
));
4938 } else if (const Constant
*C
= dyn_cast
<Constant
>(this)) {
4939 TypePrinting TypePrinter
;
4940 TypePrinter
.print(C
->getType(), OS
);
4942 AsmWriterContext
WriterCtx(&TypePrinter
, MST
.getMachine());
4943 WriteConstantInternal(OS
, C
, WriterCtx
);
4944 } else if (isa
<InlineAsm
>(this) || isa
<Argument
>(this)) {
4945 this->printAsOperand(OS
, /* PrintType */ true, MST
);
4947 llvm_unreachable("Unknown value to print out!");
4951 /// Print without a type, skipping the TypePrinting object.
4953 /// \return \c true iff printing was successful.
4954 static bool printWithoutType(const Value
&V
, raw_ostream
&O
,
4955 SlotTracker
*Machine
, const Module
*M
) {
4956 if (V
.hasName() || isa
<GlobalValue
>(V
) ||
4957 (!isa
<Constant
>(V
) && !isa
<MetadataAsValue
>(V
))) {
4958 AsmWriterContext
WriterCtx(nullptr, Machine
, M
);
4959 WriteAsOperandInternal(O
, &V
, WriterCtx
);
4965 static void printAsOperandImpl(const Value
&V
, raw_ostream
&O
, bool PrintType
,
4966 ModuleSlotTracker
&MST
) {
4967 TypePrinting
TypePrinter(MST
.getModule());
4969 TypePrinter
.print(V
.getType(), O
);
4973 AsmWriterContext
WriterCtx(&TypePrinter
, MST
.getMachine(), MST
.getModule());
4974 WriteAsOperandInternal(O
, &V
, WriterCtx
);
4977 void Value::printAsOperand(raw_ostream
&O
, bool PrintType
,
4978 const Module
*M
) const {
4980 M
= getModuleFromVal(this);
4983 if (printWithoutType(*this, O
, nullptr, M
))
4986 SlotTracker
Machine(
4987 M
, /* ShouldInitializeAllMetadata */ isa
<MetadataAsValue
>(this));
4988 ModuleSlotTracker
MST(Machine
, M
);
4989 printAsOperandImpl(*this, O
, PrintType
, MST
);
4992 void Value::printAsOperand(raw_ostream
&O
, bool PrintType
,
4993 ModuleSlotTracker
&MST
) const {
4995 if (printWithoutType(*this, O
, MST
.getMachine(), MST
.getModule()))
4998 printAsOperandImpl(*this, O
, PrintType
, MST
);
5001 /// Recursive version of printMetadataImpl.
5002 static void printMetadataImplRec(raw_ostream
&ROS
, const Metadata
&MD
,
5003 AsmWriterContext
&WriterCtx
) {
5004 formatted_raw_ostream
OS(ROS
);
5005 WriteAsOperandInternal(OS
, &MD
, WriterCtx
, /* FromValue */ true);
5007 auto *N
= dyn_cast
<MDNode
>(&MD
);
5008 if (!N
|| isa
<DIExpression
>(MD
))
5012 WriteMDNodeBodyInternal(OS
, N
, WriterCtx
);
5016 struct MDTreeAsmWriterContext
: public AsmWriterContext
{
5018 // {Level, Printed string}
5019 using EntryTy
= std::pair
<unsigned, std::string
>;
5020 SmallVector
<EntryTy
, 4> Buffer
;
5022 // Used to break the cycle in case there is any.
5023 SmallPtrSet
<const Metadata
*, 4> Visited
;
5025 raw_ostream
&MainOS
;
5027 MDTreeAsmWriterContext(TypePrinting
*TP
, SlotTracker
*ST
, const Module
*M
,
5028 raw_ostream
&OS
, const Metadata
*InitMD
)
5029 : AsmWriterContext(TP
, ST
, M
), Level(0U), Visited({InitMD
}), MainOS(OS
) {}
5031 void onWriteMetadataAsOperand(const Metadata
*MD
) override
{
5032 if (!Visited
.insert(MD
).second
)
5036 raw_string_ostream
SS(Str
);
5038 // A placeholder entry to memorize the correct
5039 // position in buffer.
5040 Buffer
.emplace_back(std::make_pair(Level
, ""));
5041 unsigned InsertIdx
= Buffer
.size() - 1;
5043 printMetadataImplRec(SS
, *MD
, *this);
5044 Buffer
[InsertIdx
].second
= std::move(SS
.str());
5048 ~MDTreeAsmWriterContext() {
5049 for (const auto &Entry
: Buffer
) {
5051 unsigned NumIndent
= Entry
.first
* 2U;
5052 MainOS
.indent(NumIndent
) << Entry
.second
;
5056 } // end anonymous namespace
5058 static void printMetadataImpl(raw_ostream
&ROS
, const Metadata
&MD
,
5059 ModuleSlotTracker
&MST
, const Module
*M
,
5060 bool OnlyAsOperand
, bool PrintAsTree
= false) {
5061 formatted_raw_ostream
OS(ROS
);
5063 TypePrinting
TypePrinter(M
);
5065 std::unique_ptr
<AsmWriterContext
> WriterCtx
;
5066 if (PrintAsTree
&& !OnlyAsOperand
)
5067 WriterCtx
= std::make_unique
<MDTreeAsmWriterContext
>(
5068 &TypePrinter
, MST
.getMachine(), M
, OS
, &MD
);
5071 std::make_unique
<AsmWriterContext
>(&TypePrinter
, MST
.getMachine(), M
);
5073 WriteAsOperandInternal(OS
, &MD
, *WriterCtx
, /* FromValue */ true);
5075 auto *N
= dyn_cast
<MDNode
>(&MD
);
5076 if (OnlyAsOperand
|| !N
|| isa
<DIExpression
>(MD
))
5080 WriteMDNodeBodyInternal(OS
, N
, *WriterCtx
);
5083 void Metadata::printAsOperand(raw_ostream
&OS
, const Module
*M
) const {
5084 ModuleSlotTracker
MST(M
, isa
<MDNode
>(this));
5085 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ true);
5088 void Metadata::printAsOperand(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
5089 const Module
*M
) const {
5090 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ true);
5093 void Metadata::print(raw_ostream
&OS
, const Module
*M
,
5094 bool /*IsForDebug*/) const {
5095 ModuleSlotTracker
MST(M
, isa
<MDNode
>(this));
5096 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ false);
5099 void Metadata::print(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
5100 const Module
*M
, bool /*IsForDebug*/) const {
5101 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ false);
5104 void MDNode::printTree(raw_ostream
&OS
, const Module
*M
) const {
5105 ModuleSlotTracker
MST(M
, true);
5106 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ false,
5107 /*PrintAsTree=*/true);
5110 void MDNode::printTree(raw_ostream
&OS
, ModuleSlotTracker
&MST
,
5111 const Module
*M
) const {
5112 printMetadataImpl(OS
, *this, MST
, M
, /* OnlyAsOperand */ false,
5113 /*PrintAsTree=*/true);
5116 void ModuleSummaryIndex::print(raw_ostream
&ROS
, bool IsForDebug
) const {
5117 SlotTracker
SlotTable(this);
5118 formatted_raw_ostream
OS(ROS
);
5119 AssemblyWriter
W(OS
, SlotTable
, this, IsForDebug
);
5120 W
.printModuleSummaryIndex();
5123 void ModuleSlotTracker::collectMDNodes(MachineMDNodeListType
&L
, unsigned LB
,
5124 unsigned UB
) const {
5125 SlotTracker
*ST
= MachineStorage
.get();
5129 for (auto &I
: llvm::make_range(ST
->mdn_begin(), ST
->mdn_end()))
5130 if (I
.second
>= LB
&& I
.second
< UB
)
5131 L
.push_back(std::make_pair(I
.second
, I
.first
));
5134 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
5135 // Value::dump - allow easy printing of Values from the debugger.
5137 void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5139 // Value::dump - allow easy printing of Values from the debugger.
5141 void DPMarker::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5143 // Value::dump - allow easy printing of Values from the debugger.
5145 void DPValue::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5147 // Type::dump - allow easy printing of Types from the debugger.
5149 void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
5151 // Module::dump() - Allow printing of Modules from the debugger.
5153 void Module::dump() const {
5154 print(dbgs(), nullptr,
5155 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
5158 // Allow printing of Comdats from the debugger.
5160 void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
5162 // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
5164 void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
5167 void Metadata::dump() const { dump(nullptr); }
5170 void Metadata::dump(const Module
*M
) const {
5171 print(dbgs(), M
, /*IsForDebug=*/true);
5176 void MDNode::dumpTree() const { dumpTree(nullptr); }
5179 void MDNode::dumpTree(const Module
*M
) const {
5180 printTree(dbgs(), M
);
5184 // Allow printing of ModuleSummaryIndex from the debugger.
5186 void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }