Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / llvm / lib / IR / AsmWriter.cpp
blobc738b50a7c721ed7107b76c93e57079732760d8a
1 //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This library implements `print` family of functions in classes like
10 // Module, Function, Value, etc. In-memory representation of those classes is
11 // converted to IR strings.
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/DerivedTypes.h"
43 #include "llvm/IR/Function.h"
44 #include "llvm/IR/GlobalAlias.h"
45 #include "llvm/IR/GlobalIFunc.h"
46 #include "llvm/IR/GlobalObject.h"
47 #include "llvm/IR/GlobalValue.h"
48 #include "llvm/IR/GlobalVariable.h"
49 #include "llvm/IR/IRPrintingPasses.h"
50 #include "llvm/IR/InlineAsm.h"
51 #include "llvm/IR/InstrTypes.h"
52 #include "llvm/IR/Instruction.h"
53 #include "llvm/IR/Instructions.h"
54 #include "llvm/IR/IntrinsicInst.h"
55 #include "llvm/IR/LLVMContext.h"
56 #include "llvm/IR/Metadata.h"
57 #include "llvm/IR/Module.h"
58 #include "llvm/IR/ModuleSlotTracker.h"
59 #include "llvm/IR/ModuleSummaryIndex.h"
60 #include "llvm/IR/Operator.h"
61 #include "llvm/IR/Type.h"
62 #include "llvm/IR/TypeFinder.h"
63 #include "llvm/IR/TypedPointerType.h"
64 #include "llvm/IR/Use.h"
65 #include "llvm/IR/User.h"
66 #include "llvm/IR/Value.h"
67 #include "llvm/Support/AtomicOrdering.h"
68 #include "llvm/Support/Casting.h"
69 #include "llvm/Support/Compiler.h"
70 #include "llvm/Support/Debug.h"
71 #include "llvm/Support/ErrorHandling.h"
72 #include "llvm/Support/Format.h"
73 #include "llvm/Support/FormattedStream.h"
74 #include "llvm/Support/SaveAndRestore.h"
75 #include "llvm/Support/raw_ostream.h"
76 #include <algorithm>
77 #include <cassert>
78 #include <cctype>
79 #include <cstddef>
80 #include <cstdint>
81 #include <iterator>
82 #include <memory>
83 #include <optional>
84 #include <string>
85 #include <tuple>
86 #include <utility>
87 #include <vector>
89 using namespace llvm;
91 // Make virtual table appear in this compilation unit.
92 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
94 //===----------------------------------------------------------------------===//
95 // Helper Functions
96 //===----------------------------------------------------------------------===//
98 using OrderMap = MapVector<const Value *, unsigned>;
100 using UseListOrderMap =
101 DenseMap<const Function *, MapVector<const Value *, std::vector<unsigned>>>;
103 /// Look for a value that might be wrapped as metadata, e.g. a value in a
104 /// metadata operand. Returns the input value as-is if it is not wrapped.
105 static const Value *skipMetadataWrapper(const Value *V) {
106 if (const auto *MAV = dyn_cast<MetadataAsValue>(V))
107 if (const auto *VAM = dyn_cast<ValueAsMetadata>(MAV->getMetadata()))
108 return VAM->getValue();
109 return V;
112 static void orderValue(const Value *V, OrderMap &OM) {
113 if (OM.lookup(V))
114 return;
116 if (const Constant *C = dyn_cast<Constant>(V))
117 if (C->getNumOperands() && !isa<GlobalValue>(C))
118 for (const Value *Op : C->operands())
119 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
120 orderValue(Op, OM);
122 // Note: we cannot cache this lookup above, since inserting into the map
123 // changes the map's size, and thus affects the other IDs.
124 unsigned ID = OM.size() + 1;
125 OM[V] = ID;
128 static OrderMap orderModule(const Module *M) {
129 OrderMap OM;
131 for (const GlobalVariable &G : M->globals()) {
132 if (G.hasInitializer())
133 if (!isa<GlobalValue>(G.getInitializer()))
134 orderValue(G.getInitializer(), OM);
135 orderValue(&G, OM);
137 for (const GlobalAlias &A : M->aliases()) {
138 if (!isa<GlobalValue>(A.getAliasee()))
139 orderValue(A.getAliasee(), OM);
140 orderValue(&A, OM);
142 for (const GlobalIFunc &I : M->ifuncs()) {
143 if (!isa<GlobalValue>(I.getResolver()))
144 orderValue(I.getResolver(), OM);
145 orderValue(&I, OM);
147 for (const Function &F : *M) {
148 for (const Use &U : F.operands())
149 if (!isa<GlobalValue>(U.get()))
150 orderValue(U.get(), OM);
152 orderValue(&F, OM);
154 if (F.isDeclaration())
155 continue;
157 for (const Argument &A : F.args())
158 orderValue(&A, OM);
159 for (const BasicBlock &BB : F) {
160 orderValue(&BB, OM);
161 for (const Instruction &I : BB) {
162 for (const Value *Op : I.operands()) {
163 Op = skipMetadataWrapper(Op);
164 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
165 isa<InlineAsm>(*Op))
166 orderValue(Op, OM);
168 orderValue(&I, OM);
172 return OM;
175 static std::vector<unsigned>
176 predictValueUseListOrder(const Value *V, unsigned ID, const OrderMap &OM) {
177 // Predict use-list order for this one.
178 using Entry = std::pair<const Use *, unsigned>;
179 SmallVector<Entry, 64> List;
180 for (const Use &U : V->uses())
181 // Check if this user will be serialized.
182 if (OM.lookup(U.getUser()))
183 List.push_back(std::make_pair(&U, List.size()));
185 if (List.size() < 2)
186 // We may have lost some users.
187 return {};
189 // When referencing a value before its declaration, a temporary value is
190 // created, which will later be RAUWed with the actual value. This reverses
191 // the use list. This happens for all values apart from basic blocks.
192 bool GetsReversed = !isa<BasicBlock>(V);
193 if (auto *BA = dyn_cast<BlockAddress>(V))
194 ID = OM.lookup(BA->getBasicBlock());
195 llvm::sort(List, [&](const Entry &L, const Entry &R) {
196 const Use *LU = L.first;
197 const Use *RU = R.first;
198 if (LU == RU)
199 return false;
201 auto LID = OM.lookup(LU->getUser());
202 auto RID = OM.lookup(RU->getUser());
204 // If ID is 4, then expect: 7 6 5 1 2 3.
205 if (LID < RID) {
206 if (GetsReversed)
207 if (RID <= ID)
208 return true;
209 return false;
211 if (RID < LID) {
212 if (GetsReversed)
213 if (LID <= ID)
214 return false;
215 return true;
218 // LID and RID are equal, so we have different operands of the same user.
219 // Assume operands are added in order for all instructions.
220 if (GetsReversed)
221 if (LID <= ID)
222 return LU->getOperandNo() < RU->getOperandNo();
223 return LU->getOperandNo() > RU->getOperandNo();
226 if (llvm::is_sorted(List, llvm::less_second()))
227 // Order is already correct.
228 return {};
230 // Store the shuffle.
231 std::vector<unsigned> Shuffle(List.size());
232 for (size_t I = 0, E = List.size(); I != E; ++I)
233 Shuffle[I] = List[I].second;
234 return Shuffle;
237 static UseListOrderMap predictUseListOrder(const Module *M) {
238 OrderMap OM = orderModule(M);
239 UseListOrderMap ULOM;
240 for (const auto &Pair : OM) {
241 const Value *V = Pair.first;
242 if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
243 continue;
245 std::vector<unsigned> Shuffle =
246 predictValueUseListOrder(V, Pair.second, OM);
247 if (Shuffle.empty())
248 continue;
250 const Function *F = nullptr;
251 if (auto *I = dyn_cast<Instruction>(V))
252 F = I->getFunction();
253 if (auto *A = dyn_cast<Argument>(V))
254 F = A->getParent();
255 if (auto *BB = dyn_cast<BasicBlock>(V))
256 F = BB->getParent();
257 ULOM[F][V] = std::move(Shuffle);
259 return ULOM;
262 static const Module *getModuleFromVal(const Value *V) {
263 if (const Argument *MA = dyn_cast<Argument>(V))
264 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
266 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
267 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
269 if (const Instruction *I = dyn_cast<Instruction>(V)) {
270 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
271 return M ? M->getParent() : nullptr;
274 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
275 return GV->getParent();
277 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
278 for (const User *U : MAV->users())
279 if (isa<Instruction>(U))
280 if (const Module *M = getModuleFromVal(U))
281 return M;
282 return nullptr;
285 return nullptr;
288 static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
289 switch (cc) {
290 default: Out << "cc" << cc; break;
291 case CallingConv::Fast: Out << "fastcc"; break;
292 case CallingConv::Cold: Out << "coldcc"; break;
293 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
294 case CallingConv::AnyReg: Out << "anyregcc"; break;
295 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
296 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
297 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
298 case CallingConv::GHC: Out << "ghccc"; break;
299 case CallingConv::Tail: Out << "tailcc"; break;
300 case CallingConv::CFGuard_Check: Out << "cfguard_checkcc"; break;
301 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
302 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
303 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
304 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
305 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
306 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
307 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
308 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
309 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
310 case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
311 case CallingConv::AArch64_SVE_VectorCall:
312 Out << "aarch64_sve_vector_pcs";
313 break;
314 case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X0:
315 Out << "aarch64_sme_preservemost_from_x0";
316 break;
317 case CallingConv::AArch64_SME_ABI_Support_Routines_PreserveMost_From_X2:
318 Out << "aarch64_sme_preservemost_from_x2";
319 break;
320 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
321 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
322 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
323 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
324 case CallingConv::PTX_Device: Out << "ptx_device"; break;
325 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
326 case CallingConv::Win64: Out << "win64cc"; break;
327 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
328 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
329 case CallingConv::Swift: Out << "swiftcc"; break;
330 case CallingConv::SwiftTail: Out << "swifttailcc"; break;
331 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
332 case CallingConv::DUMMY_HHVM:
333 Out << "hhvmcc";
334 break;
335 case CallingConv::DUMMY_HHVM_C:
336 Out << "hhvm_ccc";
337 break;
338 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
339 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
340 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
341 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
342 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
343 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
344 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
345 case CallingConv::AMDGPU_CS_Chain:
346 Out << "amdgpu_cs_chain";
347 break;
348 case CallingConv::AMDGPU_CS_ChainPreserve:
349 Out << "amdgpu_cs_chain_preserve";
350 break;
351 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
352 case CallingConv::AMDGPU_Gfx: Out << "amdgpu_gfx"; break;
353 case CallingConv::M68k_RTD: Out << "m68k_rtdcc"; break;
357 enum PrefixType {
358 GlobalPrefix,
359 ComdatPrefix,
360 LabelPrefix,
361 LocalPrefix,
362 NoPrefix
365 void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
366 assert(!Name.empty() && "Cannot get empty name!");
368 // Scan the name to see if it needs quotes first.
369 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
370 if (!NeedsQuotes) {
371 for (unsigned char C : Name) {
372 // By making this unsigned, the value passed in to isalnum will always be
373 // in the range 0-255. This is important when building with MSVC because
374 // its implementation will assert. This situation can arise when dealing
375 // with UTF-8 multibyte characters.
376 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
377 C != '_') {
378 NeedsQuotes = true;
379 break;
384 // If we didn't need any quotes, just write out the name in one blast.
385 if (!NeedsQuotes) {
386 OS << Name;
387 return;
390 // Okay, we need quotes. Output the quotes and escape any scary characters as
391 // needed.
392 OS << '"';
393 printEscapedString(Name, OS);
394 OS << '"';
397 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
398 /// (if the string only contains simple characters) or is surrounded with ""'s
399 /// (if it has special chars in it). Print it out.
400 static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
401 switch (Prefix) {
402 case NoPrefix:
403 break;
404 case GlobalPrefix:
405 OS << '@';
406 break;
407 case ComdatPrefix:
408 OS << '$';
409 break;
410 case LabelPrefix:
411 break;
412 case LocalPrefix:
413 OS << '%';
414 break;
416 printLLVMNameWithoutPrefix(OS, Name);
419 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
420 /// (if the string only contains simple characters) or is surrounded with ""'s
421 /// (if it has special chars in it). Print it out.
422 static void PrintLLVMName(raw_ostream &OS, const Value *V) {
423 PrintLLVMName(OS, V->getName(),
424 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
427 static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) {
428 Out << ", <";
429 if (isa<ScalableVectorType>(Ty))
430 Out << "vscale x ";
431 Out << Mask.size() << " x i32> ";
432 bool FirstElt = true;
433 if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
434 Out << "zeroinitializer";
435 } else if (all_of(Mask, [](int Elt) { return Elt == PoisonMaskElem; })) {
436 Out << "poison";
437 } else {
438 Out << "<";
439 for (int Elt : Mask) {
440 if (FirstElt)
441 FirstElt = false;
442 else
443 Out << ", ";
444 Out << "i32 ";
445 if (Elt == PoisonMaskElem)
446 Out << "poison";
447 else
448 Out << Elt;
450 Out << ">";
454 namespace {
456 class TypePrinting {
457 public:
458 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
460 TypePrinting(const TypePrinting &) = delete;
461 TypePrinting &operator=(const TypePrinting &) = delete;
463 /// The named types that are used by the current module.
464 TypeFinder &getNamedTypes();
466 /// The numbered types, number to type mapping.
467 std::vector<StructType *> &getNumberedTypes();
469 bool empty();
471 void print(Type *Ty, raw_ostream &OS);
473 void printStructBody(StructType *Ty, raw_ostream &OS);
475 private:
476 void incorporateTypes();
478 /// A module to process lazily when needed. Set to nullptr as soon as used.
479 const Module *DeferredM;
481 TypeFinder NamedTypes;
483 // The numbered types, along with their value.
484 DenseMap<StructType *, unsigned> Type2Number;
486 std::vector<StructType *> NumberedTypes;
489 } // end anonymous namespace
491 TypeFinder &TypePrinting::getNamedTypes() {
492 incorporateTypes();
493 return NamedTypes;
496 std::vector<StructType *> &TypePrinting::getNumberedTypes() {
497 incorporateTypes();
499 // We know all the numbers that each type is used and we know that it is a
500 // dense assignment. Convert the map to an index table, if it's not done
501 // already (judging from the sizes):
502 if (NumberedTypes.size() == Type2Number.size())
503 return NumberedTypes;
505 NumberedTypes.resize(Type2Number.size());
506 for (const auto &P : Type2Number) {
507 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
508 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
509 NumberedTypes[P.second] = P.first;
511 return NumberedTypes;
514 bool TypePrinting::empty() {
515 incorporateTypes();
516 return NamedTypes.empty() && Type2Number.empty();
519 void TypePrinting::incorporateTypes() {
520 if (!DeferredM)
521 return;
523 NamedTypes.run(*DeferredM, false);
524 DeferredM = nullptr;
526 // The list of struct types we got back includes all the struct types, split
527 // the unnamed ones out to a numbering and remove the anonymous structs.
528 unsigned NextNumber = 0;
530 std::vector<StructType *>::iterator NextToUse = NamedTypes.begin();
531 for (StructType *STy : NamedTypes) {
532 // Ignore anonymous types.
533 if (STy->isLiteral())
534 continue;
536 if (STy->getName().empty())
537 Type2Number[STy] = NextNumber++;
538 else
539 *NextToUse++ = STy;
542 NamedTypes.erase(NextToUse, NamedTypes.end());
545 /// Write the specified type to the specified raw_ostream, making use of type
546 /// names or up references to shorten the type name where possible.
547 void TypePrinting::print(Type *Ty, raw_ostream &OS) {
548 switch (Ty->getTypeID()) {
549 case Type::VoidTyID: OS << "void"; return;
550 case Type::HalfTyID: OS << "half"; return;
551 case Type::BFloatTyID: OS << "bfloat"; return;
552 case Type::FloatTyID: OS << "float"; return;
553 case Type::DoubleTyID: OS << "double"; return;
554 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
555 case Type::FP128TyID: OS << "fp128"; return;
556 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
557 case Type::LabelTyID: OS << "label"; return;
558 case Type::MetadataTyID: OS << "metadata"; return;
559 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
560 case Type::X86_AMXTyID: OS << "x86_amx"; return;
561 case Type::TokenTyID: OS << "token"; return;
562 case Type::IntegerTyID:
563 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
564 return;
566 case Type::FunctionTyID: {
567 FunctionType *FTy = cast<FunctionType>(Ty);
568 print(FTy->getReturnType(), OS);
569 OS << " (";
570 ListSeparator LS;
571 for (Type *Ty : FTy->params()) {
572 OS << LS;
573 print(Ty, OS);
575 if (FTy->isVarArg())
576 OS << LS << "...";
577 OS << ')';
578 return;
580 case Type::StructTyID: {
581 StructType *STy = cast<StructType>(Ty);
583 if (STy->isLiteral())
584 return printStructBody(STy, OS);
586 if (!STy->getName().empty())
587 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
589 incorporateTypes();
590 const auto I = Type2Number.find(STy);
591 if (I != Type2Number.end())
592 OS << '%' << I->second;
593 else // Not enumerated, print the hex address.
594 OS << "%\"type " << STy << '\"';
595 return;
597 case Type::PointerTyID: {
598 PointerType *PTy = cast<PointerType>(Ty);
599 OS << "ptr";
600 if (unsigned AddressSpace = PTy->getAddressSpace())
601 OS << " addrspace(" << AddressSpace << ')';
602 return;
604 case Type::ArrayTyID: {
605 ArrayType *ATy = cast<ArrayType>(Ty);
606 OS << '[' << ATy->getNumElements() << " x ";
607 print(ATy->getElementType(), OS);
608 OS << ']';
609 return;
611 case Type::FixedVectorTyID:
612 case Type::ScalableVectorTyID: {
613 VectorType *PTy = cast<VectorType>(Ty);
614 ElementCount EC = PTy->getElementCount();
615 OS << "<";
616 if (EC.isScalable())
617 OS << "vscale x ";
618 OS << EC.getKnownMinValue() << " x ";
619 print(PTy->getElementType(), OS);
620 OS << '>';
621 return;
623 case Type::TypedPointerTyID: {
624 TypedPointerType *TPTy = cast<TypedPointerType>(Ty);
625 OS << "typedptr(" << *TPTy->getElementType() << ", "
626 << TPTy->getAddressSpace() << ")";
627 return;
629 case Type::TargetExtTyID:
630 TargetExtType *TETy = cast<TargetExtType>(Ty);
631 OS << "target(\"";
632 printEscapedString(Ty->getTargetExtName(), OS);
633 OS << "\"";
634 for (Type *Inner : TETy->type_params())
635 OS << ", " << *Inner;
636 for (unsigned IntParam : TETy->int_params())
637 OS << ", " << IntParam;
638 OS << ")";
639 return;
641 llvm_unreachable("Invalid TypeID");
644 void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
645 if (STy->isOpaque()) {
646 OS << "opaque";
647 return;
650 if (STy->isPacked())
651 OS << '<';
653 if (STy->getNumElements() == 0) {
654 OS << "{}";
655 } else {
656 OS << "{ ";
657 ListSeparator LS;
658 for (Type *Ty : STy->elements()) {
659 OS << LS;
660 print(Ty, OS);
663 OS << " }";
665 if (STy->isPacked())
666 OS << '>';
669 AbstractSlotTrackerStorage::~AbstractSlotTrackerStorage() = default;
671 namespace llvm {
673 //===----------------------------------------------------------------------===//
674 // SlotTracker Class: Enumerate slot numbers for unnamed values
675 //===----------------------------------------------------------------------===//
676 /// This class provides computation of slot numbers for LLVM Assembly writing.
678 class SlotTracker : public AbstractSlotTrackerStorage {
679 public:
680 /// ValueMap - A mapping of Values to slot numbers.
681 using ValueMap = DenseMap<const Value *, unsigned>;
683 private:
684 /// TheModule - The module for which we are holding slot numbers.
685 const Module* TheModule;
687 /// TheFunction - The function for which we are holding slot numbers.
688 const Function* TheFunction = nullptr;
689 bool FunctionProcessed = false;
690 bool ShouldInitializeAllMetadata;
692 std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
693 ProcessModuleHookFn;
694 std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
695 ProcessFunctionHookFn;
697 /// The summary index for which we are holding slot numbers.
698 const ModuleSummaryIndex *TheIndex = nullptr;
700 /// mMap - The slot map for the module level data.
701 ValueMap mMap;
702 unsigned mNext = 0;
704 /// fMap - The slot map for the function level data.
705 ValueMap fMap;
706 unsigned fNext = 0;
708 /// mdnMap - Map for MDNodes.
709 DenseMap<const MDNode*, unsigned> mdnMap;
710 unsigned mdnNext = 0;
712 /// asMap - The slot map for attribute sets.
713 DenseMap<AttributeSet, unsigned> asMap;
714 unsigned asNext = 0;
716 /// ModulePathMap - The slot map for Module paths used in the summary index.
717 StringMap<unsigned> ModulePathMap;
718 unsigned ModulePathNext = 0;
720 /// GUIDMap - The slot map for GUIDs used in the summary index.
721 DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
722 unsigned GUIDNext = 0;
724 /// TypeIdMap - The slot map for type ids used in the summary index.
725 StringMap<unsigned> TypeIdMap;
726 unsigned TypeIdNext = 0;
728 public:
729 /// Construct from a module.
731 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
732 /// functions, giving correct numbering for metadata referenced only from
733 /// within a function (even if no functions have been initialized).
734 explicit SlotTracker(const Module *M,
735 bool ShouldInitializeAllMetadata = false);
737 /// Construct from a function, starting out in incorp state.
739 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
740 /// functions, giving correct numbering for metadata referenced only from
741 /// within a function (even if no functions have been initialized).
742 explicit SlotTracker(const Function *F,
743 bool ShouldInitializeAllMetadata = false);
745 /// Construct from a module summary index.
746 explicit SlotTracker(const ModuleSummaryIndex *Index);
748 SlotTracker(const SlotTracker &) = delete;
749 SlotTracker &operator=(const SlotTracker &) = delete;
751 ~SlotTracker() = default;
753 void setProcessHook(
754 std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>);
755 void setProcessHook(std::function<void(AbstractSlotTrackerStorage *,
756 const Function *, bool)>);
758 unsigned getNextMetadataSlot() override { return mdnNext; }
760 void createMetadataSlot(const MDNode *N) override;
762 /// Return the slot number of the specified value in it's type
763 /// plane. If something is not in the SlotTracker, return -1.
764 int getLocalSlot(const Value *V);
765 int getGlobalSlot(const GlobalValue *V);
766 int getMetadataSlot(const MDNode *N) override;
767 int getAttributeGroupSlot(AttributeSet AS);
768 int getModulePathSlot(StringRef Path);
769 int getGUIDSlot(GlobalValue::GUID GUID);
770 int getTypeIdSlot(StringRef Id);
772 /// If you'd like to deal with a function instead of just a module, use
773 /// this method to get its data into the SlotTracker.
774 void incorporateFunction(const Function *F) {
775 TheFunction = F;
776 FunctionProcessed = false;
779 const Function *getFunction() const { return TheFunction; }
781 /// After calling incorporateFunction, use this method to remove the
782 /// most recently incorporated function from the SlotTracker. This
783 /// will reset the state of the machine back to just the module contents.
784 void purgeFunction();
786 /// MDNode map iterators.
787 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
789 mdn_iterator mdn_begin() { return mdnMap.begin(); }
790 mdn_iterator mdn_end() { return mdnMap.end(); }
791 unsigned mdn_size() const { return mdnMap.size(); }
792 bool mdn_empty() const { return mdnMap.empty(); }
794 /// AttributeSet map iterators.
795 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
797 as_iterator as_begin() { return asMap.begin(); }
798 as_iterator as_end() { return asMap.end(); }
799 unsigned as_size() const { return asMap.size(); }
800 bool as_empty() const { return asMap.empty(); }
802 /// GUID map iterators.
803 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
805 /// These functions do the actual initialization.
806 inline void initializeIfNeeded();
807 int initializeIndexIfNeeded();
809 // Implementation Details
810 private:
811 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
812 void CreateModuleSlot(const GlobalValue *V);
814 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
815 void CreateMetadataSlot(const MDNode *N);
817 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
818 void CreateFunctionSlot(const Value *V);
820 /// Insert the specified AttributeSet into the slot table.
821 void CreateAttributeSetSlot(AttributeSet AS);
823 inline void CreateModulePathSlot(StringRef Path);
824 void CreateGUIDSlot(GlobalValue::GUID GUID);
825 void CreateTypeIdSlot(StringRef Id);
827 /// Add all of the module level global variables (and their initializers)
828 /// and function declarations, but not the contents of those functions.
829 void processModule();
830 // Returns number of allocated slots
831 int processIndex();
833 /// Add all of the functions arguments, basic blocks, and instructions.
834 void processFunction();
836 /// Add the metadata directly attached to a GlobalObject.
837 void processGlobalObjectMetadata(const GlobalObject &GO);
839 /// Add all of the metadata from a function.
840 void processFunctionMetadata(const Function &F);
842 /// Add all of the metadata from an instruction.
843 void processInstructionMetadata(const Instruction &I);
846 } // end namespace llvm
848 ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
849 const Function *F)
850 : M(M), F(F), Machine(&Machine) {}
852 ModuleSlotTracker::ModuleSlotTracker(const Module *M,
853 bool ShouldInitializeAllMetadata)
854 : ShouldCreateStorage(M),
855 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
857 ModuleSlotTracker::~ModuleSlotTracker() = default;
859 SlotTracker *ModuleSlotTracker::getMachine() {
860 if (!ShouldCreateStorage)
861 return Machine;
863 ShouldCreateStorage = false;
864 MachineStorage =
865 std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
866 Machine = MachineStorage.get();
867 if (ProcessModuleHookFn)
868 Machine->setProcessHook(ProcessModuleHookFn);
869 if (ProcessFunctionHookFn)
870 Machine->setProcessHook(ProcessFunctionHookFn);
871 return Machine;
874 void ModuleSlotTracker::incorporateFunction(const Function &F) {
875 // Using getMachine() may lazily create the slot tracker.
876 if (!getMachine())
877 return;
879 // Nothing to do if this is the right function already.
880 if (this->F == &F)
881 return;
882 if (this->F)
883 Machine->purgeFunction();
884 Machine->incorporateFunction(&F);
885 this->F = &F;
888 int ModuleSlotTracker::getLocalSlot(const Value *V) {
889 assert(F && "No function incorporated");
890 return Machine->getLocalSlot(V);
893 void ModuleSlotTracker::setProcessHook(
894 std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
895 Fn) {
896 ProcessModuleHookFn = Fn;
899 void ModuleSlotTracker::setProcessHook(
900 std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
901 Fn) {
902 ProcessFunctionHookFn = Fn;
905 static SlotTracker *createSlotTracker(const Value *V) {
906 if (const Argument *FA = dyn_cast<Argument>(V))
907 return new SlotTracker(FA->getParent());
909 if (const Instruction *I = dyn_cast<Instruction>(V))
910 if (I->getParent())
911 return new SlotTracker(I->getParent()->getParent());
913 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
914 return new SlotTracker(BB->getParent());
916 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
917 return new SlotTracker(GV->getParent());
919 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
920 return new SlotTracker(GA->getParent());
922 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
923 return new SlotTracker(GIF->getParent());
925 if (const Function *Func = dyn_cast<Function>(V))
926 return new SlotTracker(Func);
928 return nullptr;
931 #if 0
932 #define ST_DEBUG(X) dbgs() << X
933 #else
934 #define ST_DEBUG(X)
935 #endif
937 // Module level constructor. Causes the contents of the Module (sans functions)
938 // to be added to the slot table.
939 SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
940 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
942 // Function level constructor. Causes the contents of the Module and the one
943 // function provided to be added to the slot table.
944 SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
945 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
946 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
948 SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
949 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
951 inline void SlotTracker::initializeIfNeeded() {
952 if (TheModule) {
953 processModule();
954 TheModule = nullptr; ///< Prevent re-processing next time we're called.
957 if (TheFunction && !FunctionProcessed)
958 processFunction();
961 int SlotTracker::initializeIndexIfNeeded() {
962 if (!TheIndex)
963 return 0;
964 int NumSlots = processIndex();
965 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
966 return NumSlots;
969 // Iterate through all the global variables, functions, and global
970 // variable initializers and create slots for them.
971 void SlotTracker::processModule() {
972 ST_DEBUG("begin processModule!\n");
974 // Add all of the unnamed global variables to the value table.
975 for (const GlobalVariable &Var : TheModule->globals()) {
976 if (!Var.hasName())
977 CreateModuleSlot(&Var);
978 processGlobalObjectMetadata(Var);
979 auto Attrs = Var.getAttributes();
980 if (Attrs.hasAttributes())
981 CreateAttributeSetSlot(Attrs);
984 for (const GlobalAlias &A : TheModule->aliases()) {
985 if (!A.hasName())
986 CreateModuleSlot(&A);
989 for (const GlobalIFunc &I : TheModule->ifuncs()) {
990 if (!I.hasName())
991 CreateModuleSlot(&I);
994 // Add metadata used by named metadata.
995 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
996 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
997 CreateMetadataSlot(NMD.getOperand(i));
1000 for (const Function &F : *TheModule) {
1001 if (!F.hasName())
1002 // Add all the unnamed functions to the table.
1003 CreateModuleSlot(&F);
1005 if (ShouldInitializeAllMetadata)
1006 processFunctionMetadata(F);
1008 // Add all the function attributes to the table.
1009 // FIXME: Add attributes of other objects?
1010 AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
1011 if (FnAttrs.hasAttributes())
1012 CreateAttributeSetSlot(FnAttrs);
1015 if (ProcessModuleHookFn)
1016 ProcessModuleHookFn(this, TheModule, ShouldInitializeAllMetadata);
1018 ST_DEBUG("end processModule!\n");
1021 // Process the arguments, basic blocks, and instructions of a function.
1022 void SlotTracker::processFunction() {
1023 ST_DEBUG("begin processFunction!\n");
1024 fNext = 0;
1026 // Process function metadata if it wasn't hit at the module-level.
1027 if (!ShouldInitializeAllMetadata)
1028 processFunctionMetadata(*TheFunction);
1030 // Add all the function arguments with no names.
1031 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
1032 AE = TheFunction->arg_end(); AI != AE; ++AI)
1033 if (!AI->hasName())
1034 CreateFunctionSlot(&*AI);
1036 ST_DEBUG("Inserting Instructions:\n");
1038 // Add all of the basic blocks and instructions with no names.
1039 for (auto &BB : *TheFunction) {
1040 if (!BB.hasName())
1041 CreateFunctionSlot(&BB);
1043 for (auto &I : BB) {
1044 if (!I.getType()->isVoidTy() && !I.hasName())
1045 CreateFunctionSlot(&I);
1047 // We allow direct calls to any llvm.foo function here, because the
1048 // target may not be linked into the optimizer.
1049 if (const auto *Call = dyn_cast<CallBase>(&I)) {
1050 // Add all the call attributes to the table.
1051 AttributeSet Attrs = Call->getAttributes().getFnAttrs();
1052 if (Attrs.hasAttributes())
1053 CreateAttributeSetSlot(Attrs);
1058 if (ProcessFunctionHookFn)
1059 ProcessFunctionHookFn(this, TheFunction, ShouldInitializeAllMetadata);
1061 FunctionProcessed = true;
1063 ST_DEBUG("end processFunction!\n");
1066 // Iterate through all the GUID in the index and create slots for them.
1067 int SlotTracker::processIndex() {
1068 ST_DEBUG("begin processIndex!\n");
1069 assert(TheIndex);
1071 // The first block of slots are just the module ids, which start at 0 and are
1072 // assigned consecutively. Since the StringMap iteration order isn't
1073 // guaranteed, order by path string before assigning slots.
1074 std::vector<StringRef> ModulePaths;
1075 for (auto &[ModPath, _] : TheIndex->modulePaths())
1076 ModulePaths.push_back(ModPath);
1077 llvm::sort(ModulePaths.begin(), ModulePaths.end());
1078 for (auto &ModPath : ModulePaths)
1079 CreateModulePathSlot(ModPath);
1081 // Start numbering the GUIDs after the module ids.
1082 GUIDNext = ModulePathNext;
1084 for (auto &GlobalList : *TheIndex)
1085 CreateGUIDSlot(GlobalList.first);
1087 for (auto &TId : TheIndex->typeIdCompatibleVtableMap())
1088 CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1090 // Start numbering the TypeIds after the GUIDs.
1091 TypeIdNext = GUIDNext;
1092 for (const auto &TID : TheIndex->typeIds())
1093 CreateTypeIdSlot(TID.second.first);
1095 ST_DEBUG("end processIndex!\n");
1096 return TypeIdNext;
1099 void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
1100 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1101 GO.getAllMetadata(MDs);
1102 for (auto &MD : MDs)
1103 CreateMetadataSlot(MD.second);
1106 void SlotTracker::processFunctionMetadata(const Function &F) {
1107 processGlobalObjectMetadata(F);
1108 for (auto &BB : F) {
1109 for (auto &I : BB)
1110 processInstructionMetadata(I);
1114 void SlotTracker::processInstructionMetadata(const Instruction &I) {
1115 // Process metadata used directly by intrinsics.
1116 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1117 if (Function *F = CI->getCalledFunction())
1118 if (F->isIntrinsic())
1119 for (auto &Op : I.operands())
1120 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1121 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1122 CreateMetadataSlot(N);
1124 // Process metadata attached to this instruction.
1125 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1126 I.getAllMetadata(MDs);
1127 for (auto &MD : MDs)
1128 CreateMetadataSlot(MD.second);
1131 /// Clean up after incorporating a function. This is the only way to get out of
1132 /// the function incorporation state that affects get*Slot/Create*Slot. Function
1133 /// incorporation state is indicated by TheFunction != 0.
1134 void SlotTracker::purgeFunction() {
1135 ST_DEBUG("begin purgeFunction!\n");
1136 fMap.clear(); // Simply discard the function level map
1137 TheFunction = nullptr;
1138 FunctionProcessed = false;
1139 ST_DEBUG("end purgeFunction!\n");
1142 /// getGlobalSlot - Get the slot number of a global value.
1143 int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1144 // Check for uninitialized state and do lazy initialization.
1145 initializeIfNeeded();
1147 // Find the value in the module map
1148 ValueMap::iterator MI = mMap.find(V);
1149 return MI == mMap.end() ? -1 : (int)MI->second;
1152 void SlotTracker::setProcessHook(
1153 std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
1154 Fn) {
1155 ProcessModuleHookFn = Fn;
1158 void SlotTracker::setProcessHook(
1159 std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
1160 Fn) {
1161 ProcessFunctionHookFn = Fn;
1164 /// getMetadataSlot - Get the slot number of a MDNode.
1165 void SlotTracker::createMetadataSlot(const MDNode *N) { CreateMetadataSlot(N); }
1167 /// getMetadataSlot - Get the slot number of a MDNode.
1168 int SlotTracker::getMetadataSlot(const MDNode *N) {
1169 // Check for uninitialized state and do lazy initialization.
1170 initializeIfNeeded();
1172 // Find the MDNode in the module map
1173 mdn_iterator MI = mdnMap.find(N);
1174 return MI == mdnMap.end() ? -1 : (int)MI->second;
1177 /// getLocalSlot - Get the slot number for a value that is local to a function.
1178 int SlotTracker::getLocalSlot(const Value *V) {
1179 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1181 // Check for uninitialized state and do lazy initialization.
1182 initializeIfNeeded();
1184 ValueMap::iterator FI = fMap.find(V);
1185 return FI == fMap.end() ? -1 : (int)FI->second;
1188 int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
1189 // Check for uninitialized state and do lazy initialization.
1190 initializeIfNeeded();
1192 // Find the AttributeSet in the module map.
1193 as_iterator AI = asMap.find(AS);
1194 return AI == asMap.end() ? -1 : (int)AI->second;
1197 int SlotTracker::getModulePathSlot(StringRef Path) {
1198 // Check for uninitialized state and do lazy initialization.
1199 initializeIndexIfNeeded();
1201 // Find the Module path in the map
1202 auto I = ModulePathMap.find(Path);
1203 return I == ModulePathMap.end() ? -1 : (int)I->second;
1206 int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1207 // Check for uninitialized state and do lazy initialization.
1208 initializeIndexIfNeeded();
1210 // Find the GUID in the map
1211 guid_iterator I = GUIDMap.find(GUID);
1212 return I == GUIDMap.end() ? -1 : (int)I->second;
1215 int SlotTracker::getTypeIdSlot(StringRef Id) {
1216 // Check for uninitialized state and do lazy initialization.
1217 initializeIndexIfNeeded();
1219 // Find the TypeId string in the map
1220 auto I = TypeIdMap.find(Id);
1221 return I == TypeIdMap.end() ? -1 : (int)I->second;
1224 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1225 void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1226 assert(V && "Can't insert a null Value into SlotTracker!");
1227 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
1228 assert(!V->hasName() && "Doesn't need a slot!");
1230 unsigned DestSlot = mNext++;
1231 mMap[V] = DestSlot;
1233 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1234 DestSlot << " [");
1235 // G = Global, F = Function, A = Alias, I = IFunc, o = other
1236 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1237 (isa<Function>(V) ? 'F' :
1238 (isa<GlobalAlias>(V) ? 'A' :
1239 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
1242 /// CreateSlot - Create a new slot for the specified value if it has no name.
1243 void SlotTracker::CreateFunctionSlot(const Value *V) {
1244 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
1246 unsigned DestSlot = fNext++;
1247 fMap[V] = DestSlot;
1249 // G = Global, F = Function, o = other
1250 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1251 DestSlot << " [o]\n");
1254 /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1255 void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1256 assert(N && "Can't insert a null Value into SlotTracker!");
1258 // Don't make slots for DIExpressions or DIArgLists. We just print them inline
1259 // everywhere.
1260 if (isa<DIExpression>(N) || isa<DIArgList>(N))
1261 return;
1263 unsigned DestSlot = mdnNext;
1264 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1265 return;
1266 ++mdnNext;
1268 // Recursively add any MDNodes referenced by operands.
1269 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1270 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1271 CreateMetadataSlot(Op);
1274 void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1275 assert(AS.hasAttributes() && "Doesn't need a slot!");
1277 as_iterator I = asMap.find(AS);
1278 if (I != asMap.end())
1279 return;
1281 unsigned DestSlot = asNext++;
1282 asMap[AS] = DestSlot;
1285 /// Create a new slot for the specified Module
1286 void SlotTracker::CreateModulePathSlot(StringRef Path) {
1287 ModulePathMap[Path] = ModulePathNext++;
1290 /// Create a new slot for the specified GUID
1291 void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1292 GUIDMap[GUID] = GUIDNext++;
1295 /// Create a new slot for the specified Id
1296 void SlotTracker::CreateTypeIdSlot(StringRef Id) {
1297 TypeIdMap[Id] = TypeIdNext++;
1300 namespace {
1301 /// Common instances used by most of the printer functions.
1302 struct AsmWriterContext {
1303 TypePrinting *TypePrinter = nullptr;
1304 SlotTracker *Machine = nullptr;
1305 const Module *Context = nullptr;
1307 AsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M = nullptr)
1308 : TypePrinter(TP), Machine(ST), Context(M) {}
1310 static AsmWriterContext &getEmpty() {
1311 static AsmWriterContext EmptyCtx(nullptr, nullptr);
1312 return EmptyCtx;
1315 /// A callback that will be triggered when the underlying printer
1316 /// prints a Metadata as operand.
1317 virtual void onWriteMetadataAsOperand(const Metadata *) {}
1319 virtual ~AsmWriterContext() = default;
1321 } // end anonymous namespace
1323 //===----------------------------------------------------------------------===//
1324 // AsmWriter Implementation
1325 //===----------------------------------------------------------------------===//
1327 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
1328 AsmWriterContext &WriterCtx);
1330 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1331 AsmWriterContext &WriterCtx,
1332 bool FromValue = false);
1334 static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
1335 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U))
1336 Out << FPO->getFastMathFlags();
1338 if (const OverflowingBinaryOperator *OBO =
1339 dyn_cast<OverflowingBinaryOperator>(U)) {
1340 if (OBO->hasNoUnsignedWrap())
1341 Out << " nuw";
1342 if (OBO->hasNoSignedWrap())
1343 Out << " nsw";
1344 } else if (const PossiblyExactOperator *Div =
1345 dyn_cast<PossiblyExactOperator>(U)) {
1346 if (Div->isExact())
1347 Out << " exact";
1348 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1349 if (GEP->isInBounds())
1350 Out << " inbounds";
1351 } else if (const auto *NNI = dyn_cast<PossiblyNonNegInst>(U)) {
1352 if (NNI->hasNonNeg())
1353 Out << " nneg";
1357 static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1358 AsmWriterContext &WriterCtx) {
1359 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1360 if (CI->getType()->isIntegerTy(1)) {
1361 Out << (CI->getZExtValue() ? "true" : "false");
1362 return;
1364 Out << CI->getValue();
1365 return;
1368 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
1369 const APFloat &APF = CFP->getValueAPF();
1370 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1371 &APF.getSemantics() == &APFloat::IEEEdouble()) {
1372 // We would like to output the FP constant value in exponential notation,
1373 // but we cannot do this if doing so will lose precision. Check here to
1374 // make sure that we only output it in exponential format if we can parse
1375 // the value back and get the same value.
1377 bool ignored;
1378 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1379 bool isInf = APF.isInfinity();
1380 bool isNaN = APF.isNaN();
1381 if (!isInf && !isNaN) {
1382 double Val = APF.convertToDouble();
1383 SmallString<128> StrVal;
1384 APF.toString(StrVal, 6, 0, false);
1385 // Check to make sure that the stringized number is not some string like
1386 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1387 // that the string matches the "[-+]?[0-9]" regex.
1389 assert((isDigit(StrVal[0]) || ((StrVal[0] == '-' || StrVal[0] == '+') &&
1390 isDigit(StrVal[1]))) &&
1391 "[-+]?[0-9] regex does not match!");
1392 // Reparse stringized version!
1393 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1394 Out << StrVal;
1395 return;
1398 // Otherwise we could not reparse it to exactly the same value, so we must
1399 // output the string in hexadecimal format! Note that loading and storing
1400 // floating point types changes the bits of NaNs on some hosts, notably
1401 // x86, so we must not use these types.
1402 static_assert(sizeof(double) == sizeof(uint64_t),
1403 "assuming that double is 64 bits!");
1404 APFloat apf = APF;
1405 // Floats are represented in ASCII IR as double, convert.
1406 // FIXME: We should allow 32-bit hex float and remove this.
1407 if (!isDouble) {
1408 // A signaling NaN is quieted on conversion, so we need to recreate the
1409 // expected value after convert (quiet bit of the payload is clear).
1410 bool IsSNAN = apf.isSignaling();
1411 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
1412 &ignored);
1413 if (IsSNAN) {
1414 APInt Payload = apf.bitcastToAPInt();
1415 apf = APFloat::getSNaN(APFloat::IEEEdouble(), apf.isNegative(),
1416 &Payload);
1419 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
1420 return;
1423 // Either half, bfloat or some form of long double.
1424 // These appear as a magic letter identifying the type, then a
1425 // fixed number of hex digits.
1426 Out << "0x";
1427 APInt API = APF.bitcastToAPInt();
1428 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
1429 Out << 'K';
1430 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1431 /*Upper=*/true);
1432 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1433 /*Upper=*/true);
1434 return;
1435 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
1436 Out << 'L';
1437 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1438 /*Upper=*/true);
1439 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1440 /*Upper=*/true);
1441 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
1442 Out << 'M';
1443 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1444 /*Upper=*/true);
1445 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1446 /*Upper=*/true);
1447 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
1448 Out << 'H';
1449 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1450 /*Upper=*/true);
1451 } else if (&APF.getSemantics() == &APFloat::BFloat()) {
1452 Out << 'R';
1453 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1454 /*Upper=*/true);
1455 } else
1456 llvm_unreachable("Unsupported floating point type");
1457 return;
1460 if (isa<ConstantAggregateZero>(CV) || isa<ConstantTargetNone>(CV)) {
1461 Out << "zeroinitializer";
1462 return;
1465 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1466 Out << "blockaddress(";
1467 WriteAsOperandInternal(Out, BA->getFunction(), WriterCtx);
1468 Out << ", ";
1469 WriteAsOperandInternal(Out, BA->getBasicBlock(), WriterCtx);
1470 Out << ")";
1471 return;
1474 if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) {
1475 Out << "dso_local_equivalent ";
1476 WriteAsOperandInternal(Out, Equiv->getGlobalValue(), WriterCtx);
1477 return;
1480 if (const auto *NC = dyn_cast<NoCFIValue>(CV)) {
1481 Out << "no_cfi ";
1482 WriteAsOperandInternal(Out, NC->getGlobalValue(), WriterCtx);
1483 return;
1486 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
1487 Type *ETy = CA->getType()->getElementType();
1488 Out << '[';
1489 WriterCtx.TypePrinter->print(ETy, Out);
1490 Out << ' ';
1491 WriteAsOperandInternal(Out, CA->getOperand(0), WriterCtx);
1492 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1493 Out << ", ";
1494 WriterCtx.TypePrinter->print(ETy, Out);
1495 Out << ' ';
1496 WriteAsOperandInternal(Out, CA->getOperand(i), WriterCtx);
1498 Out << ']';
1499 return;
1502 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1503 // As a special case, print the array as a string if it is an array of
1504 // i8 with ConstantInt values.
1505 if (CA->isString()) {
1506 Out << "c\"";
1507 printEscapedString(CA->getAsString(), Out);
1508 Out << '"';
1509 return;
1512 Type *ETy = CA->getType()->getElementType();
1513 Out << '[';
1514 WriterCtx.TypePrinter->print(ETy, Out);
1515 Out << ' ';
1516 WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx);
1517 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1518 Out << ", ";
1519 WriterCtx.TypePrinter->print(ETy, Out);
1520 Out << ' ';
1521 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), WriterCtx);
1523 Out << ']';
1524 return;
1527 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
1528 if (CS->getType()->isPacked())
1529 Out << '<';
1530 Out << '{';
1531 unsigned N = CS->getNumOperands();
1532 if (N) {
1533 Out << ' ';
1534 WriterCtx.TypePrinter->print(CS->getOperand(0)->getType(), Out);
1535 Out << ' ';
1537 WriteAsOperandInternal(Out, CS->getOperand(0), WriterCtx);
1539 for (unsigned i = 1; i < N; i++) {
1540 Out << ", ";
1541 WriterCtx.TypePrinter->print(CS->getOperand(i)->getType(), Out);
1542 Out << ' ';
1544 WriteAsOperandInternal(Out, CS->getOperand(i), WriterCtx);
1546 Out << ' ';
1549 Out << '}';
1550 if (CS->getType()->isPacked())
1551 Out << '>';
1552 return;
1555 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1556 auto *CVVTy = cast<FixedVectorType>(CV->getType());
1557 Type *ETy = CVVTy->getElementType();
1558 Out << '<';
1559 WriterCtx.TypePrinter->print(ETy, Out);
1560 Out << ' ';
1561 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), WriterCtx);
1562 for (unsigned i = 1, e = CVVTy->getNumElements(); i != e; ++i) {
1563 Out << ", ";
1564 WriterCtx.TypePrinter->print(ETy, Out);
1565 Out << ' ';
1566 WriteAsOperandInternal(Out, CV->getAggregateElement(i), WriterCtx);
1568 Out << '>';
1569 return;
1572 if (isa<ConstantPointerNull>(CV)) {
1573 Out << "null";
1574 return;
1577 if (isa<ConstantTokenNone>(CV)) {
1578 Out << "none";
1579 return;
1582 if (isa<PoisonValue>(CV)) {
1583 Out << "poison";
1584 return;
1587 if (isa<UndefValue>(CV)) {
1588 Out << "undef";
1589 return;
1592 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
1593 Out << CE->getOpcodeName();
1594 WriteOptimizationInfo(Out, CE);
1595 if (CE->isCompare())
1596 Out << ' ' << static_cast<CmpInst::Predicate>(CE->getPredicate());
1597 Out << " (";
1599 std::optional<unsigned> InRangeOp;
1600 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
1601 WriterCtx.TypePrinter->print(GEP->getSourceElementType(), Out);
1602 Out << ", ";
1603 InRangeOp = GEP->getInRangeIndex();
1604 if (InRangeOp)
1605 ++*InRangeOp;
1608 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
1609 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
1610 Out << "inrange ";
1611 WriterCtx.TypePrinter->print((*OI)->getType(), Out);
1612 Out << ' ';
1613 WriteAsOperandInternal(Out, *OI, WriterCtx);
1614 if (OI+1 != CE->op_end())
1615 Out << ", ";
1618 if (CE->isCast()) {
1619 Out << " to ";
1620 WriterCtx.TypePrinter->print(CE->getType(), Out);
1623 if (CE->getOpcode() == Instruction::ShuffleVector)
1624 PrintShuffleMask(Out, CE->getType(), CE->getShuffleMask());
1626 Out << ')';
1627 return;
1630 Out << "<placeholder or erroneous Constant>";
1633 static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1634 AsmWriterContext &WriterCtx) {
1635 Out << "!{";
1636 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1637 const Metadata *MD = Node->getOperand(mi);
1638 if (!MD)
1639 Out << "null";
1640 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1641 Value *V = MDV->getValue();
1642 WriterCtx.TypePrinter->print(V->getType(), Out);
1643 Out << ' ';
1644 WriteAsOperandInternal(Out, V, WriterCtx);
1645 } else {
1646 WriteAsOperandInternal(Out, MD, WriterCtx);
1647 WriterCtx.onWriteMetadataAsOperand(MD);
1649 if (mi + 1 != me)
1650 Out << ", ";
1653 Out << "}";
1656 namespace {
1658 struct FieldSeparator {
1659 bool Skip = true;
1660 const char *Sep;
1662 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
1665 raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1666 if (FS.Skip) {
1667 FS.Skip = false;
1668 return OS;
1670 return OS << FS.Sep;
1673 struct MDFieldPrinter {
1674 raw_ostream &Out;
1675 FieldSeparator FS;
1676 AsmWriterContext &WriterCtx;
1678 explicit MDFieldPrinter(raw_ostream &Out)
1679 : Out(Out), WriterCtx(AsmWriterContext::getEmpty()) {}
1680 MDFieldPrinter(raw_ostream &Out, AsmWriterContext &Ctx)
1681 : Out(Out), WriterCtx(Ctx) {}
1683 void printTag(const DINode *N);
1684 void printMacinfoType(const DIMacroNode *N);
1685 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
1686 void printString(StringRef Name, StringRef Value,
1687 bool ShouldSkipEmpty = true);
1688 void printMetadata(StringRef Name, const Metadata *MD,
1689 bool ShouldSkipNull = true);
1690 template <class IntTy>
1691 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
1692 void printAPInt(StringRef Name, const APInt &Int, bool IsUnsigned,
1693 bool ShouldSkipZero);
1694 void printBool(StringRef Name, bool Value,
1695 std::optional<bool> Default = std::nullopt);
1696 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
1697 void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
1698 template <class IntTy, class Stringifier>
1699 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1700 bool ShouldSkipZero = true);
1701 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
1702 void printNameTableKind(StringRef Name,
1703 DICompileUnit::DebugNameTableKind NTK);
1706 } // end anonymous namespace
1708 void MDFieldPrinter::printTag(const DINode *N) {
1709 Out << FS << "tag: ";
1710 auto Tag = dwarf::TagString(N->getTag());
1711 if (!Tag.empty())
1712 Out << Tag;
1713 else
1714 Out << N->getTag();
1717 void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1718 Out << FS << "type: ";
1719 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1720 if (!Type.empty())
1721 Out << Type;
1722 else
1723 Out << N->getMacinfoType();
1726 void MDFieldPrinter::printChecksum(
1727 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1728 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1729 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
1732 void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1733 bool ShouldSkipEmpty) {
1734 if (ShouldSkipEmpty && Value.empty())
1735 return;
1737 Out << FS << Name << ": \"";
1738 printEscapedString(Value, Out);
1739 Out << "\"";
1742 static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1743 AsmWriterContext &WriterCtx) {
1744 if (!MD) {
1745 Out << "null";
1746 return;
1748 WriteAsOperandInternal(Out, MD, WriterCtx);
1749 WriterCtx.onWriteMetadataAsOperand(MD);
1752 void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1753 bool ShouldSkipNull) {
1754 if (ShouldSkipNull && !MD)
1755 return;
1757 Out << FS << Name << ": ";
1758 writeMetadataAsOperand(Out, MD, WriterCtx);
1761 template <class IntTy>
1762 void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1763 if (ShouldSkipZero && !Int)
1764 return;
1766 Out << FS << Name << ": " << Int;
1769 void MDFieldPrinter::printAPInt(StringRef Name, const APInt &Int,
1770 bool IsUnsigned, bool ShouldSkipZero) {
1771 if (ShouldSkipZero && Int.isZero())
1772 return;
1774 Out << FS << Name << ": ";
1775 Int.print(Out, !IsUnsigned);
1778 void MDFieldPrinter::printBool(StringRef Name, bool Value,
1779 std::optional<bool> Default) {
1780 if (Default && Value == *Default)
1781 return;
1782 Out << FS << Name << ": " << (Value ? "true" : "false");
1785 void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
1786 if (!Flags)
1787 return;
1789 Out << FS << Name << ": ";
1791 SmallVector<DINode::DIFlags, 8> SplitFlags;
1792 auto Extra = DINode::splitFlags(Flags, SplitFlags);
1794 FieldSeparator FlagsFS(" | ");
1795 for (auto F : SplitFlags) {
1796 auto StringF = DINode::getFlagString(F);
1797 assert(!StringF.empty() && "Expected valid flag");
1798 Out << FlagsFS << StringF;
1800 if (Extra || SplitFlags.empty())
1801 Out << FlagsFS << Extra;
1804 void MDFieldPrinter::printDISPFlags(StringRef Name,
1805 DISubprogram::DISPFlags Flags) {
1806 // Always print this field, because no flags in the IR at all will be
1807 // interpreted as old-style isDefinition: true.
1808 Out << FS << Name << ": ";
1810 if (!Flags) {
1811 Out << 0;
1812 return;
1815 SmallVector<DISubprogram::DISPFlags, 8> SplitFlags;
1816 auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
1818 FieldSeparator FlagsFS(" | ");
1819 for (auto F : SplitFlags) {
1820 auto StringF = DISubprogram::getFlagString(F);
1821 assert(!StringF.empty() && "Expected valid flag");
1822 Out << FlagsFS << StringF;
1824 if (Extra || SplitFlags.empty())
1825 Out << FlagsFS << Extra;
1828 void MDFieldPrinter::printEmissionKind(StringRef Name,
1829 DICompileUnit::DebugEmissionKind EK) {
1830 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
1833 void MDFieldPrinter::printNameTableKind(StringRef Name,
1834 DICompileUnit::DebugNameTableKind NTK) {
1835 if (NTK == DICompileUnit::DebugNameTableKind::Default)
1836 return;
1837 Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
1840 template <class IntTy, class Stringifier>
1841 void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1842 Stringifier toString, bool ShouldSkipZero) {
1843 if (!Value)
1844 return;
1846 Out << FS << Name << ": ";
1847 auto S = toString(Value);
1848 if (!S.empty())
1849 Out << S;
1850 else
1851 Out << Value;
1854 static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1855 AsmWriterContext &WriterCtx) {
1856 Out << "!GenericDINode(";
1857 MDFieldPrinter Printer(Out, WriterCtx);
1858 Printer.printTag(N);
1859 Printer.printString("header", N->getHeader());
1860 if (N->getNumDwarfOperands()) {
1861 Out << Printer.FS << "operands: {";
1862 FieldSeparator IFS;
1863 for (auto &I : N->dwarf_operands()) {
1864 Out << IFS;
1865 writeMetadataAsOperand(Out, I, WriterCtx);
1867 Out << "}";
1869 Out << ")";
1872 static void writeDILocation(raw_ostream &Out, const DILocation *DL,
1873 AsmWriterContext &WriterCtx) {
1874 Out << "!DILocation(";
1875 MDFieldPrinter Printer(Out, WriterCtx);
1876 // Always output the line, since 0 is a relevant and important value for it.
1877 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1878 Printer.printInt("column", DL->getColumn());
1879 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1880 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
1881 Printer.printBool("isImplicitCode", DL->isImplicitCode(),
1882 /* Default */ false);
1883 Out << ")";
1886 static void writeDIAssignID(raw_ostream &Out, const DIAssignID *DL,
1887 AsmWriterContext &WriterCtx) {
1888 Out << "!DIAssignID()";
1889 MDFieldPrinter Printer(Out, WriterCtx);
1892 static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
1893 AsmWriterContext &WriterCtx) {
1894 Out << "!DISubrange(";
1895 MDFieldPrinter Printer(Out, WriterCtx);
1897 auto *Count = N->getRawCountNode();
1898 if (auto *CE = dyn_cast_or_null<ConstantAsMetadata>(Count)) {
1899 auto *CV = cast<ConstantInt>(CE->getValue());
1900 Printer.printInt("count", CV->getSExtValue(),
1901 /* ShouldSkipZero */ false);
1902 } else
1903 Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
1905 // A lowerBound of constant 0 should not be skipped, since it is different
1906 // from an unspecified lower bound (= nullptr).
1907 auto *LBound = N->getRawLowerBound();
1908 if (auto *LE = dyn_cast_or_null<ConstantAsMetadata>(LBound)) {
1909 auto *LV = cast<ConstantInt>(LE->getValue());
1910 Printer.printInt("lowerBound", LV->getSExtValue(),
1911 /* ShouldSkipZero */ false);
1912 } else
1913 Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
1915 auto *UBound = N->getRawUpperBound();
1916 if (auto *UE = dyn_cast_or_null<ConstantAsMetadata>(UBound)) {
1917 auto *UV = cast<ConstantInt>(UE->getValue());
1918 Printer.printInt("upperBound", UV->getSExtValue(),
1919 /* ShouldSkipZero */ false);
1920 } else
1921 Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
1923 auto *Stride = N->getRawStride();
1924 if (auto *SE = dyn_cast_or_null<ConstantAsMetadata>(Stride)) {
1925 auto *SV = cast<ConstantInt>(SE->getValue());
1926 Printer.printInt("stride", SV->getSExtValue(), /* ShouldSkipZero */ false);
1927 } else
1928 Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);
1930 Out << ")";
1933 static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N,
1934 AsmWriterContext &WriterCtx) {
1935 Out << "!DIGenericSubrange(";
1936 MDFieldPrinter Printer(Out, WriterCtx);
1938 auto IsConstant = [&](Metadata *Bound) -> bool {
1939 if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) {
1940 return BE->isConstant() &&
1941 DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1942 *BE->isConstant();
1944 return false;
1947 auto GetConstant = [&](Metadata *Bound) -> int64_t {
1948 assert(IsConstant(Bound) && "Expected constant");
1949 auto *BE = dyn_cast_or_null<DIExpression>(Bound);
1950 return static_cast<int64_t>(BE->getElement(1));
1953 auto *Count = N->getRawCountNode();
1954 if (IsConstant(Count))
1955 Printer.printInt("count", GetConstant(Count),
1956 /* ShouldSkipZero */ false);
1957 else
1958 Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
1960 auto *LBound = N->getRawLowerBound();
1961 if (IsConstant(LBound))
1962 Printer.printInt("lowerBound", GetConstant(LBound),
1963 /* ShouldSkipZero */ false);
1964 else
1965 Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
1967 auto *UBound = N->getRawUpperBound();
1968 if (IsConstant(UBound))
1969 Printer.printInt("upperBound", GetConstant(UBound),
1970 /* ShouldSkipZero */ false);
1971 else
1972 Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
1974 auto *Stride = N->getRawStride();
1975 if (IsConstant(Stride))
1976 Printer.printInt("stride", GetConstant(Stride),
1977 /* ShouldSkipZero */ false);
1978 else
1979 Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);
1981 Out << ")";
1984 static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
1985 AsmWriterContext &) {
1986 Out << "!DIEnumerator(";
1987 MDFieldPrinter Printer(Out);
1988 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
1989 Printer.printAPInt("value", N->getValue(), N->isUnsigned(),
1990 /*ShouldSkipZero=*/false);
1991 if (N->isUnsigned())
1992 Printer.printBool("isUnsigned", true);
1993 Out << ")";
1996 static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
1997 AsmWriterContext &) {
1998 Out << "!DIBasicType(";
1999 MDFieldPrinter Printer(Out);
2000 if (N->getTag() != dwarf::DW_TAG_base_type)
2001 Printer.printTag(N);
2002 Printer.printString("name", N->getName());
2003 Printer.printInt("size", N->getSizeInBits());
2004 Printer.printInt("align", N->getAlignInBits());
2005 Printer.printDwarfEnum("encoding", N->getEncoding(),
2006 dwarf::AttributeEncodingString);
2007 Printer.printDIFlags("flags", N->getFlags());
2008 Out << ")";
2011 static void writeDIStringType(raw_ostream &Out, const DIStringType *N,
2012 AsmWriterContext &WriterCtx) {
2013 Out << "!DIStringType(";
2014 MDFieldPrinter Printer(Out, WriterCtx);
2015 if (N->getTag() != dwarf::DW_TAG_string_type)
2016 Printer.printTag(N);
2017 Printer.printString("name", N->getName());
2018 Printer.printMetadata("stringLength", N->getRawStringLength());
2019 Printer.printMetadata("stringLengthExpression", N->getRawStringLengthExp());
2020 Printer.printMetadata("stringLocationExpression",
2021 N->getRawStringLocationExp());
2022 Printer.printInt("size", N->getSizeInBits());
2023 Printer.printInt("align", N->getAlignInBits());
2024 Printer.printDwarfEnum("encoding", N->getEncoding(),
2025 dwarf::AttributeEncodingString);
2026 Out << ")";
2029 static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
2030 AsmWriterContext &WriterCtx) {
2031 Out << "!DIDerivedType(";
2032 MDFieldPrinter Printer(Out, WriterCtx);
2033 Printer.printTag(N);
2034 Printer.printString("name", N->getName());
2035 Printer.printMetadata("scope", N->getRawScope());
2036 Printer.printMetadata("file", N->getRawFile());
2037 Printer.printInt("line", N->getLine());
2038 Printer.printMetadata("baseType", N->getRawBaseType(),
2039 /* ShouldSkipNull */ false);
2040 Printer.printInt("size", N->getSizeInBits());
2041 Printer.printInt("align", N->getAlignInBits());
2042 Printer.printInt("offset", N->getOffsetInBits());
2043 Printer.printDIFlags("flags", N->getFlags());
2044 Printer.printMetadata("extraData", N->getRawExtraData());
2045 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
2046 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
2047 /* ShouldSkipZero */ false);
2048 Printer.printMetadata("annotations", N->getRawAnnotations());
2049 Out << ")";
2052 static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
2053 AsmWriterContext &WriterCtx) {
2054 Out << "!DICompositeType(";
2055 MDFieldPrinter Printer(Out, WriterCtx);
2056 Printer.printTag(N);
2057 Printer.printString("name", N->getName());
2058 Printer.printMetadata("scope", N->getRawScope());
2059 Printer.printMetadata("file", N->getRawFile());
2060 Printer.printInt("line", N->getLine());
2061 Printer.printMetadata("baseType", N->getRawBaseType());
2062 Printer.printInt("size", N->getSizeInBits());
2063 Printer.printInt("align", N->getAlignInBits());
2064 Printer.printInt("offset", N->getOffsetInBits());
2065 Printer.printDIFlags("flags", N->getFlags());
2066 Printer.printMetadata("elements", N->getRawElements());
2067 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
2068 dwarf::LanguageString);
2069 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
2070 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2071 Printer.printString("identifier", N->getIdentifier());
2072 Printer.printMetadata("discriminator", N->getRawDiscriminator());
2073 Printer.printMetadata("dataLocation", N->getRawDataLocation());
2074 Printer.printMetadata("associated", N->getRawAssociated());
2075 Printer.printMetadata("allocated", N->getRawAllocated());
2076 if (auto *RankConst = N->getRankConst())
2077 Printer.printInt("rank", RankConst->getSExtValue(),
2078 /* ShouldSkipZero */ false);
2079 else
2080 Printer.printMetadata("rank", N->getRawRank(), /*ShouldSkipNull */ true);
2081 Printer.printMetadata("annotations", N->getRawAnnotations());
2082 Out << ")";
2085 static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
2086 AsmWriterContext &WriterCtx) {
2087 Out << "!DISubroutineType(";
2088 MDFieldPrinter Printer(Out, WriterCtx);
2089 Printer.printDIFlags("flags", N->getFlags());
2090 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
2091 Printer.printMetadata("types", N->getRawTypeArray(),
2092 /* ShouldSkipNull */ false);
2093 Out << ")";
2096 static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &) {
2097 Out << "!DIFile(";
2098 MDFieldPrinter Printer(Out);
2099 Printer.printString("filename", N->getFilename(),
2100 /* ShouldSkipEmpty */ false);
2101 Printer.printString("directory", N->getDirectory(),
2102 /* ShouldSkipEmpty */ false);
2103 // Print all values for checksum together, or not at all.
2104 if (N->getChecksum())
2105 Printer.printChecksum(*N->getChecksum());
2106 Printer.printString("source", N->getSource().value_or(StringRef()),
2107 /* ShouldSkipEmpty */ true);
2108 Out << ")";
2111 static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
2112 AsmWriterContext &WriterCtx) {
2113 Out << "!DICompileUnit(";
2114 MDFieldPrinter Printer(Out, WriterCtx);
2115 Printer.printDwarfEnum("language", N->getSourceLanguage(),
2116 dwarf::LanguageString, /* ShouldSkipZero */ false);
2117 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2118 Printer.printString("producer", N->getProducer());
2119 Printer.printBool("isOptimized", N->isOptimized());
2120 Printer.printString("flags", N->getFlags());
2121 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
2122 /* ShouldSkipZero */ false);
2123 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
2124 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
2125 Printer.printMetadata("enums", N->getRawEnumTypes());
2126 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
2127 Printer.printMetadata("globals", N->getRawGlobalVariables());
2128 Printer.printMetadata("imports", N->getRawImportedEntities());
2129 Printer.printMetadata("macros", N->getRawMacros());
2130 Printer.printInt("dwoId", N->getDWOId());
2131 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
2132 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
2133 false);
2134 Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
2135 Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
2136 Printer.printString("sysroot", N->getSysRoot());
2137 Printer.printString("sdk", N->getSDK());
2138 Out << ")";
2141 static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
2142 AsmWriterContext &WriterCtx) {
2143 Out << "!DISubprogram(";
2144 MDFieldPrinter Printer(Out, WriterCtx);
2145 Printer.printString("name", N->getName());
2146 Printer.printString("linkageName", N->getLinkageName());
2147 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2148 Printer.printMetadata("file", N->getRawFile());
2149 Printer.printInt("line", N->getLine());
2150 Printer.printMetadata("type", N->getRawType());
2151 Printer.printInt("scopeLine", N->getScopeLine());
2152 Printer.printMetadata("containingType", N->getRawContainingType());
2153 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
2154 N->getVirtualIndex() != 0)
2155 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
2156 Printer.printInt("thisAdjustment", N->getThisAdjustment());
2157 Printer.printDIFlags("flags", N->getFlags());
2158 Printer.printDISPFlags("spFlags", N->getSPFlags());
2159 Printer.printMetadata("unit", N->getRawUnit());
2160 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2161 Printer.printMetadata("declaration", N->getRawDeclaration());
2162 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
2163 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
2164 Printer.printMetadata("annotations", N->getRawAnnotations());
2165 Printer.printString("targetFuncName", N->getTargetFuncName());
2166 Out << ")";
2169 static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
2170 AsmWriterContext &WriterCtx) {
2171 Out << "!DILexicalBlock(";
2172 MDFieldPrinter Printer(Out, WriterCtx);
2173 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2174 Printer.printMetadata("file", N->getRawFile());
2175 Printer.printInt("line", N->getLine());
2176 Printer.printInt("column", N->getColumn());
2177 Out << ")";
2180 static void writeDILexicalBlockFile(raw_ostream &Out,
2181 const DILexicalBlockFile *N,
2182 AsmWriterContext &WriterCtx) {
2183 Out << "!DILexicalBlockFile(";
2184 MDFieldPrinter Printer(Out, WriterCtx);
2185 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2186 Printer.printMetadata("file", N->getRawFile());
2187 Printer.printInt("discriminator", N->getDiscriminator(),
2188 /* ShouldSkipZero */ false);
2189 Out << ")";
2192 static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
2193 AsmWriterContext &WriterCtx) {
2194 Out << "!DINamespace(";
2195 MDFieldPrinter Printer(Out, WriterCtx);
2196 Printer.printString("name", N->getName());
2197 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2198 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
2199 Out << ")";
2202 static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N,
2203 AsmWriterContext &WriterCtx) {
2204 Out << "!DICommonBlock(";
2205 MDFieldPrinter Printer(Out, WriterCtx);
2206 Printer.printMetadata("scope", N->getRawScope(), false);
2207 Printer.printMetadata("declaration", N->getRawDecl(), false);
2208 Printer.printString("name", N->getName());
2209 Printer.printMetadata("file", N->getRawFile());
2210 Printer.printInt("line", N->getLineNo());
2211 Out << ")";
2214 static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2215 AsmWriterContext &WriterCtx) {
2216 Out << "!DIMacro(";
2217 MDFieldPrinter Printer(Out, WriterCtx);
2218 Printer.printMacinfoType(N);
2219 Printer.printInt("line", N->getLine());
2220 Printer.printString("name", N->getName());
2221 Printer.printString("value", N->getValue());
2222 Out << ")";
2225 static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
2226 AsmWriterContext &WriterCtx) {
2227 Out << "!DIMacroFile(";
2228 MDFieldPrinter Printer(Out, WriterCtx);
2229 Printer.printInt("line", N->getLine());
2230 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2231 Printer.printMetadata("nodes", N->getRawElements());
2232 Out << ")";
2235 static void writeDIModule(raw_ostream &Out, const DIModule *N,
2236 AsmWriterContext &WriterCtx) {
2237 Out << "!DIModule(";
2238 MDFieldPrinter Printer(Out, WriterCtx);
2239 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2240 Printer.printString("name", N->getName());
2241 Printer.printString("configMacros", N->getConfigurationMacros());
2242 Printer.printString("includePath", N->getIncludePath());
2243 Printer.printString("apinotes", N->getAPINotesFile());
2244 Printer.printMetadata("file", N->getRawFile());
2245 Printer.printInt("line", N->getLineNo());
2246 Printer.printBool("isDecl", N->getIsDecl(), /* Default */ false);
2247 Out << ")";
2250 static void writeDITemplateTypeParameter(raw_ostream &Out,
2251 const DITemplateTypeParameter *N,
2252 AsmWriterContext &WriterCtx) {
2253 Out << "!DITemplateTypeParameter(";
2254 MDFieldPrinter Printer(Out, WriterCtx);
2255 Printer.printString("name", N->getName());
2256 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
2257 Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
2258 Out << ")";
2261 static void writeDITemplateValueParameter(raw_ostream &Out,
2262 const DITemplateValueParameter *N,
2263 AsmWriterContext &WriterCtx) {
2264 Out << "!DITemplateValueParameter(";
2265 MDFieldPrinter Printer(Out, WriterCtx);
2266 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
2267 Printer.printTag(N);
2268 Printer.printString("name", N->getName());
2269 Printer.printMetadata("type", N->getRawType());
2270 Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
2271 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
2272 Out << ")";
2275 static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
2276 AsmWriterContext &WriterCtx) {
2277 Out << "!DIGlobalVariable(";
2278 MDFieldPrinter Printer(Out, WriterCtx);
2279 Printer.printString("name", N->getName());
2280 Printer.printString("linkageName", N->getLinkageName());
2281 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2282 Printer.printMetadata("file", N->getRawFile());
2283 Printer.printInt("line", N->getLine());
2284 Printer.printMetadata("type", N->getRawType());
2285 Printer.printBool("isLocal", N->isLocalToUnit());
2286 Printer.printBool("isDefinition", N->isDefinition());
2287 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
2288 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2289 Printer.printInt("align", N->getAlignInBits());
2290 Printer.printMetadata("annotations", N->getRawAnnotations());
2291 Out << ")";
2294 static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
2295 AsmWriterContext &WriterCtx) {
2296 Out << "!DILocalVariable(";
2297 MDFieldPrinter Printer(Out, WriterCtx);
2298 Printer.printString("name", N->getName());
2299 Printer.printInt("arg", N->getArg());
2300 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2301 Printer.printMetadata("file", N->getRawFile());
2302 Printer.printInt("line", N->getLine());
2303 Printer.printMetadata("type", N->getRawType());
2304 Printer.printDIFlags("flags", N->getFlags());
2305 Printer.printInt("align", N->getAlignInBits());
2306 Printer.printMetadata("annotations", N->getRawAnnotations());
2307 Out << ")";
2310 static void writeDILabel(raw_ostream &Out, const DILabel *N,
2311 AsmWriterContext &WriterCtx) {
2312 Out << "!DILabel(";
2313 MDFieldPrinter Printer(Out, WriterCtx);
2314 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2315 Printer.printString("name", N->getName());
2316 Printer.printMetadata("file", N->getRawFile());
2317 Printer.printInt("line", N->getLine());
2318 Out << ")";
2321 static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
2322 AsmWriterContext &WriterCtx) {
2323 Out << "!DIExpression(";
2324 FieldSeparator FS;
2325 if (N->isValid()) {
2326 for (const DIExpression::ExprOperand &Op : N->expr_ops()) {
2327 auto OpStr = dwarf::OperationEncodingString(Op.getOp());
2328 assert(!OpStr.empty() && "Expected valid opcode");
2330 Out << FS << OpStr;
2331 if (Op.getOp() == dwarf::DW_OP_LLVM_convert) {
2332 Out << FS << Op.getArg(0);
2333 Out << FS << dwarf::AttributeEncodingString(Op.getArg(1));
2334 } else {
2335 for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A)
2336 Out << FS << Op.getArg(A);
2339 } else {
2340 for (const auto &I : N->getElements())
2341 Out << FS << I;
2343 Out << ")";
2346 static void writeDIArgList(raw_ostream &Out, const DIArgList *N,
2347 AsmWriterContext &WriterCtx,
2348 bool FromValue = false) {
2349 assert(FromValue &&
2350 "Unexpected DIArgList metadata outside of value argument");
2351 Out << "!DIArgList(";
2352 FieldSeparator FS;
2353 MDFieldPrinter Printer(Out, WriterCtx);
2354 for (Metadata *Arg : N->getArgs()) {
2355 Out << FS;
2356 WriteAsOperandInternal(Out, Arg, WriterCtx, true);
2358 Out << ")";
2361 static void writeDIGlobalVariableExpression(raw_ostream &Out,
2362 const DIGlobalVariableExpression *N,
2363 AsmWriterContext &WriterCtx) {
2364 Out << "!DIGlobalVariableExpression(";
2365 MDFieldPrinter Printer(Out, WriterCtx);
2366 Printer.printMetadata("var", N->getVariable());
2367 Printer.printMetadata("expr", N->getExpression());
2368 Out << ")";
2371 static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
2372 AsmWriterContext &WriterCtx) {
2373 Out << "!DIObjCProperty(";
2374 MDFieldPrinter Printer(Out, WriterCtx);
2375 Printer.printString("name", N->getName());
2376 Printer.printMetadata("file", N->getRawFile());
2377 Printer.printInt("line", N->getLine());
2378 Printer.printString("setter", N->getSetterName());
2379 Printer.printString("getter", N->getGetterName());
2380 Printer.printInt("attributes", N->getAttributes());
2381 Printer.printMetadata("type", N->getRawType());
2382 Out << ")";
2385 static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
2386 AsmWriterContext &WriterCtx) {
2387 Out << "!DIImportedEntity(";
2388 MDFieldPrinter Printer(Out, WriterCtx);
2389 Printer.printTag(N);
2390 Printer.printString("name", N->getName());
2391 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2392 Printer.printMetadata("entity", N->getRawEntity());
2393 Printer.printMetadata("file", N->getRawFile());
2394 Printer.printInt("line", N->getLine());
2395 Printer.printMetadata("elements", N->getRawElements());
2396 Out << ")";
2399 static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2400 AsmWriterContext &Ctx) {
2401 if (Node->isDistinct())
2402 Out << "distinct ";
2403 else if (Node->isTemporary())
2404 Out << "<temporary!> "; // Handle broken code.
2406 switch (Node->getMetadataID()) {
2407 default:
2408 llvm_unreachable("Expected uniquable MDNode");
2409 #define HANDLE_MDNODE_LEAF(CLASS) \
2410 case Metadata::CLASS##Kind: \
2411 write##CLASS(Out, cast<CLASS>(Node), Ctx); \
2412 break;
2413 #include "llvm/IR/Metadata.def"
2417 // Full implementation of printing a Value as an operand with support for
2418 // TypePrinting, etc.
2419 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
2420 AsmWriterContext &WriterCtx) {
2421 if (V->hasName()) {
2422 PrintLLVMName(Out, V);
2423 return;
2426 const Constant *CV = dyn_cast<Constant>(V);
2427 if (CV && !isa<GlobalValue>(CV)) {
2428 assert(WriterCtx.TypePrinter && "Constants require TypePrinting!");
2429 WriteConstantInternal(Out, CV, WriterCtx);
2430 return;
2433 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2434 Out << "asm ";
2435 if (IA->hasSideEffects())
2436 Out << "sideeffect ";
2437 if (IA->isAlignStack())
2438 Out << "alignstack ";
2439 // We don't emit the AD_ATT dialect as it's the assumed default.
2440 if (IA->getDialect() == InlineAsm::AD_Intel)
2441 Out << "inteldialect ";
2442 if (IA->canThrow())
2443 Out << "unwind ";
2444 Out << '"';
2445 printEscapedString(IA->getAsmString(), Out);
2446 Out << "\", \"";
2447 printEscapedString(IA->getConstraintString(), Out);
2448 Out << '"';
2449 return;
2452 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2453 WriteAsOperandInternal(Out, MD->getMetadata(), WriterCtx,
2454 /* FromValue */ true);
2455 return;
2458 char Prefix = '%';
2459 int Slot;
2460 auto *Machine = WriterCtx.Machine;
2461 // If we have a SlotTracker, use it.
2462 if (Machine) {
2463 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2464 Slot = Machine->getGlobalSlot(GV);
2465 Prefix = '@';
2466 } else {
2467 Slot = Machine->getLocalSlot(V);
2469 // If the local value didn't succeed, then we may be referring to a value
2470 // from a different function. Translate it, as this can happen when using
2471 // address of blocks.
2472 if (Slot == -1)
2473 if ((Machine = createSlotTracker(V))) {
2474 Slot = Machine->getLocalSlot(V);
2475 delete Machine;
2478 } else if ((Machine = createSlotTracker(V))) {
2479 // Otherwise, create one to get the # and then destroy it.
2480 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2481 Slot = Machine->getGlobalSlot(GV);
2482 Prefix = '@';
2483 } else {
2484 Slot = Machine->getLocalSlot(V);
2486 delete Machine;
2487 Machine = nullptr;
2488 } else {
2489 Slot = -1;
2492 if (Slot != -1)
2493 Out << Prefix << Slot;
2494 else
2495 Out << "<badref>";
2498 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2499 AsmWriterContext &WriterCtx,
2500 bool FromValue) {
2501 // Write DIExpressions and DIArgLists inline when used as a value. Improves
2502 // readability of debug info intrinsics.
2503 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2504 writeDIExpression(Out, Expr, WriterCtx);
2505 return;
2507 if (const DIArgList *ArgList = dyn_cast<DIArgList>(MD)) {
2508 writeDIArgList(Out, ArgList, WriterCtx, FromValue);
2509 return;
2512 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2513 std::unique_ptr<SlotTracker> MachineStorage;
2514 SaveAndRestore SARMachine(WriterCtx.Machine);
2515 if (!WriterCtx.Machine) {
2516 MachineStorage = std::make_unique<SlotTracker>(WriterCtx.Context);
2517 WriterCtx.Machine = MachineStorage.get();
2519 int Slot = WriterCtx.Machine->getMetadataSlot(N);
2520 if (Slot == -1) {
2521 if (const DILocation *Loc = dyn_cast<DILocation>(N)) {
2522 writeDILocation(Out, Loc, WriterCtx);
2523 return;
2525 // Give the pointer value instead of "badref", since this comes up all
2526 // the time when debugging.
2527 Out << "<" << N << ">";
2528 } else
2529 Out << '!' << Slot;
2530 return;
2533 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2534 Out << "!\"";
2535 printEscapedString(MDS->getString(), Out);
2536 Out << '"';
2537 return;
2540 auto *V = cast<ValueAsMetadata>(MD);
2541 assert(WriterCtx.TypePrinter && "TypePrinter required for metadata values");
2542 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2543 "Unexpected function-local metadata outside of value argument");
2545 WriterCtx.TypePrinter->print(V->getValue()->getType(), Out);
2546 Out << ' ';
2547 WriteAsOperandInternal(Out, V->getValue(), WriterCtx);
2550 namespace {
2552 class AssemblyWriter {
2553 formatted_raw_ostream &Out;
2554 const Module *TheModule = nullptr;
2555 const ModuleSummaryIndex *TheIndex = nullptr;
2556 std::unique_ptr<SlotTracker> SlotTrackerStorage;
2557 SlotTracker &Machine;
2558 TypePrinting TypePrinter;
2559 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
2560 SetVector<const Comdat *> Comdats;
2561 bool IsForDebug;
2562 bool ShouldPreserveUseListOrder;
2563 UseListOrderMap UseListOrders;
2564 SmallVector<StringRef, 8> MDNames;
2565 /// Synchronization scope names registered with LLVMContext.
2566 SmallVector<StringRef, 8> SSNs;
2567 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
2569 public:
2570 /// Construct an AssemblyWriter with an external SlotTracker
2571 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
2572 AssemblyAnnotationWriter *AAW, bool IsForDebug,
2573 bool ShouldPreserveUseListOrder = false);
2575 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2576 const ModuleSummaryIndex *Index, bool IsForDebug);
2578 AsmWriterContext getContext() {
2579 return AsmWriterContext(&TypePrinter, &Machine, TheModule);
2582 void printMDNodeBody(const MDNode *MD);
2583 void printNamedMDNode(const NamedMDNode *NMD);
2585 void printModule(const Module *M);
2587 void writeOperand(const Value *Op, bool PrintType);
2588 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
2589 void writeOperandBundles(const CallBase *Call);
2590 void writeSyncScope(const LLVMContext &Context,
2591 SyncScope::ID SSID);
2592 void writeAtomic(const LLVMContext &Context,
2593 AtomicOrdering Ordering,
2594 SyncScope::ID SSID);
2595 void writeAtomicCmpXchg(const LLVMContext &Context,
2596 AtomicOrdering SuccessOrdering,
2597 AtomicOrdering FailureOrdering,
2598 SyncScope::ID SSID);
2600 void writeAllMDNodes();
2601 void writeMDNode(unsigned Slot, const MDNode *Node);
2602 void writeAttribute(const Attribute &Attr, bool InAttrGroup = false);
2603 void writeAttributeSet(const AttributeSet &AttrSet, bool InAttrGroup = false);
2604 void writeAllAttributeGroups();
2606 void printTypeIdentities();
2607 void printGlobal(const GlobalVariable *GV);
2608 void printAlias(const GlobalAlias *GA);
2609 void printIFunc(const GlobalIFunc *GI);
2610 void printComdat(const Comdat *C);
2611 void printFunction(const Function *F);
2612 void printArgument(const Argument *FA, AttributeSet Attrs);
2613 void printBasicBlock(const BasicBlock *BB);
2614 void printInstructionLine(const Instruction &I);
2615 void printInstruction(const Instruction &I);
2617 void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle);
2618 void printUseLists(const Function *F);
2620 void printModuleSummaryIndex();
2621 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2622 void printSummary(const GlobalValueSummary &Summary);
2623 void printAliasSummary(const AliasSummary *AS);
2624 void printGlobalVarSummary(const GlobalVarSummary *GS);
2625 void printFunctionSummary(const FunctionSummary *FS);
2626 void printTypeIdSummary(const TypeIdSummary &TIS);
2627 void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI);
2628 void printTypeTestResolution(const TypeTestResolution &TTRes);
2629 void printArgs(const std::vector<uint64_t> &Args);
2630 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2631 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2632 void printVFuncId(const FunctionSummary::VFuncId VFId);
2633 void
2634 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> &VCallList,
2635 const char *Tag);
2636 void
2637 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> &VCallList,
2638 const char *Tag);
2640 private:
2641 /// Print out metadata attachments.
2642 void printMetadataAttachments(
2643 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2644 StringRef Separator);
2646 // printInfoComment - Print a little comment after the instruction indicating
2647 // which slot it occupies.
2648 void printInfoComment(const Value &V);
2650 // printGCRelocateComment - print comment after call to the gc.relocate
2651 // intrinsic indicating base and derived pointer names.
2652 void printGCRelocateComment(const GCRelocateInst &Relocate);
2655 } // end anonymous namespace
2657 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2658 const Module *M, AssemblyAnnotationWriter *AAW,
2659 bool IsForDebug, bool ShouldPreserveUseListOrder)
2660 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
2661 IsForDebug(IsForDebug),
2662 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
2663 if (!TheModule)
2664 return;
2665 for (const GlobalObject &GO : TheModule->global_objects())
2666 if (const Comdat *C = GO.getComdat())
2667 Comdats.insert(C);
2670 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2671 const ModuleSummaryIndex *Index, bool IsForDebug)
2672 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2673 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2675 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
2676 if (!Operand) {
2677 Out << "<null operand!>";
2678 return;
2680 if (PrintType) {
2681 TypePrinter.print(Operand->getType(), Out);
2682 Out << ' ';
2684 auto WriterCtx = getContext();
2685 WriteAsOperandInternal(Out, Operand, WriterCtx);
2688 void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2689 SyncScope::ID SSID) {
2690 switch (SSID) {
2691 case SyncScope::System: {
2692 break;
2694 default: {
2695 if (SSNs.empty())
2696 Context.getSyncScopeNames(SSNs);
2698 Out << " syncscope(\"";
2699 printEscapedString(SSNs[SSID], Out);
2700 Out << "\")";
2701 break;
2706 void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2707 AtomicOrdering Ordering,
2708 SyncScope::ID SSID) {
2709 if (Ordering == AtomicOrdering::NotAtomic)
2710 return;
2712 writeSyncScope(Context, SSID);
2713 Out << " " << toIRString(Ordering);
2716 void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2717 AtomicOrdering SuccessOrdering,
2718 AtomicOrdering FailureOrdering,
2719 SyncScope::ID SSID) {
2720 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2721 FailureOrdering != AtomicOrdering::NotAtomic);
2723 writeSyncScope(Context, SSID);
2724 Out << " " << toIRString(SuccessOrdering);
2725 Out << " " << toIRString(FailureOrdering);
2728 void AssemblyWriter::writeParamOperand(const Value *Operand,
2729 AttributeSet Attrs) {
2730 if (!Operand) {
2731 Out << "<null operand!>";
2732 return;
2735 // Print the type
2736 TypePrinter.print(Operand->getType(), Out);
2737 // Print parameter attributes list
2738 if (Attrs.hasAttributes()) {
2739 Out << ' ';
2740 writeAttributeSet(Attrs);
2742 Out << ' ';
2743 // Print the operand
2744 auto WriterCtx = getContext();
2745 WriteAsOperandInternal(Out, Operand, WriterCtx);
2748 void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
2749 if (!Call->hasOperandBundles())
2750 return;
2752 Out << " [ ";
2754 bool FirstBundle = true;
2755 for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
2756 OperandBundleUse BU = Call->getOperandBundleAt(i);
2758 if (!FirstBundle)
2759 Out << ", ";
2760 FirstBundle = false;
2762 Out << '"';
2763 printEscapedString(BU.getTagName(), Out);
2764 Out << '"';
2766 Out << '(';
2768 bool FirstInput = true;
2769 auto WriterCtx = getContext();
2770 for (const auto &Input : BU.Inputs) {
2771 if (!FirstInput)
2772 Out << ", ";
2773 FirstInput = false;
2775 if (Input == nullptr)
2776 Out << "<null operand bundle!>";
2777 else {
2778 TypePrinter.print(Input->getType(), Out);
2779 Out << " ";
2780 WriteAsOperandInternal(Out, Input, WriterCtx);
2784 Out << ')';
2787 Out << " ]";
2790 void AssemblyWriter::printModule(const Module *M) {
2791 Machine.initializeIfNeeded();
2793 if (ShouldPreserveUseListOrder)
2794 UseListOrders = predictUseListOrder(M);
2796 if (!M->getModuleIdentifier().empty() &&
2797 // Don't print the ID if it will start a new line (which would
2798 // require a comment char before it).
2799 M->getModuleIdentifier().find('\n') == std::string::npos)
2800 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2802 if (!M->getSourceFileName().empty()) {
2803 Out << "source_filename = \"";
2804 printEscapedString(M->getSourceFileName(), Out);
2805 Out << "\"\n";
2808 const std::string &DL = M->getDataLayoutStr();
2809 if (!DL.empty())
2810 Out << "target datalayout = \"" << DL << "\"\n";
2811 if (!M->getTargetTriple().empty())
2812 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
2814 if (!M->getModuleInlineAsm().empty()) {
2815 Out << '\n';
2817 // Split the string into lines, to make it easier to read the .ll file.
2818 StringRef Asm = M->getModuleInlineAsm();
2819 do {
2820 StringRef Front;
2821 std::tie(Front, Asm) = Asm.split('\n');
2823 // We found a newline, print the portion of the asm string from the
2824 // last newline up to this newline.
2825 Out << "module asm \"";
2826 printEscapedString(Front, Out);
2827 Out << "\"\n";
2828 } while (!Asm.empty());
2831 printTypeIdentities();
2833 // Output all comdats.
2834 if (!Comdats.empty())
2835 Out << '\n';
2836 for (const Comdat *C : Comdats) {
2837 printComdat(C);
2838 if (C != Comdats.back())
2839 Out << '\n';
2842 // Output all globals.
2843 if (!M->global_empty()) Out << '\n';
2844 for (const GlobalVariable &GV : M->globals()) {
2845 printGlobal(&GV); Out << '\n';
2848 // Output all aliases.
2849 if (!M->alias_empty()) Out << "\n";
2850 for (const GlobalAlias &GA : M->aliases())
2851 printAlias(&GA);
2853 // Output all ifuncs.
2854 if (!M->ifunc_empty()) Out << "\n";
2855 for (const GlobalIFunc &GI : M->ifuncs())
2856 printIFunc(&GI);
2858 // Output all of the functions.
2859 for (const Function &F : *M) {
2860 Out << '\n';
2861 printFunction(&F);
2864 // Output global use-lists.
2865 printUseLists(nullptr);
2867 // Output all attribute groups.
2868 if (!Machine.as_empty()) {
2869 Out << '\n';
2870 writeAllAttributeGroups();
2873 // Output named metadata.
2874 if (!M->named_metadata_empty()) Out << '\n';
2876 for (const NamedMDNode &Node : M->named_metadata())
2877 printNamedMDNode(&Node);
2879 // Output metadata.
2880 if (!Machine.mdn_empty()) {
2881 Out << '\n';
2882 writeAllMDNodes();
2886 void AssemblyWriter::printModuleSummaryIndex() {
2887 assert(TheIndex);
2888 int NumSlots = Machine.initializeIndexIfNeeded();
2890 Out << "\n";
2892 // Print module path entries. To print in order, add paths to a vector
2893 // indexed by module slot.
2894 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
2895 std::string RegularLTOModuleName =
2896 ModuleSummaryIndex::getRegularLTOModuleName();
2897 moduleVec.resize(TheIndex->modulePaths().size());
2898 for (auto &[ModPath, ModHash] : TheIndex->modulePaths())
2899 moduleVec[Machine.getModulePathSlot(ModPath)] = std::make_pair(
2900 // An empty module path is a special entry for a regular LTO module
2901 // created during the thin link.
2902 ModPath.empty() ? RegularLTOModuleName : std::string(ModPath), ModHash);
2904 unsigned i = 0;
2905 for (auto &ModPair : moduleVec) {
2906 Out << "^" << i++ << " = module: (";
2907 Out << "path: \"";
2908 printEscapedString(ModPair.first, Out);
2909 Out << "\", hash: (";
2910 FieldSeparator FS;
2911 for (auto Hash : ModPair.second)
2912 Out << FS << Hash;
2913 Out << "))\n";
2916 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2917 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2918 for (auto &GlobalList : *TheIndex) {
2919 auto GUID = GlobalList.first;
2920 for (auto &Summary : GlobalList.second.SummaryList)
2921 SummaryToGUIDMap[Summary.get()] = GUID;
2924 // Print the global value summary entries.
2925 for (auto &GlobalList : *TheIndex) {
2926 auto GUID = GlobalList.first;
2927 auto VI = TheIndex->getValueInfo(GlobalList);
2928 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2931 // Print the TypeIdMap entries.
2932 for (const auto &TID : TheIndex->typeIds()) {
2933 Out << "^" << Machine.getTypeIdSlot(TID.second.first)
2934 << " = typeid: (name: \"" << TID.second.first << "\"";
2935 printTypeIdSummary(TID.second.second);
2936 Out << ") ; guid = " << TID.first << "\n";
2939 // Print the TypeIdCompatibleVtableMap entries.
2940 for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
2941 auto GUID = GlobalValue::getGUID(TId.first);
2942 Out << "^" << Machine.getGUIDSlot(GUID)
2943 << " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
2944 printTypeIdCompatibleVtableSummary(TId.second);
2945 Out << ") ; guid = " << GUID << "\n";
2948 // Don't emit flags when it's not really needed (value is zero by default).
2949 if (TheIndex->getFlags()) {
2950 Out << "^" << NumSlots << " = flags: " << TheIndex->getFlags() << "\n";
2951 ++NumSlots;
2954 Out << "^" << NumSlots << " = blockcount: " << TheIndex->getBlockCount()
2955 << "\n";
2958 static const char *
2959 getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2960 switch (K) {
2961 case WholeProgramDevirtResolution::Indir:
2962 return "indir";
2963 case WholeProgramDevirtResolution::SingleImpl:
2964 return "singleImpl";
2965 case WholeProgramDevirtResolution::BranchFunnel:
2966 return "branchFunnel";
2968 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2971 static const char *getWholeProgDevirtResByArgKindName(
2972 WholeProgramDevirtResolution::ByArg::Kind K) {
2973 switch (K) {
2974 case WholeProgramDevirtResolution::ByArg::Indir:
2975 return "indir";
2976 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2977 return "uniformRetVal";
2978 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2979 return "uniqueRetVal";
2980 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2981 return "virtualConstProp";
2983 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2986 static const char *getTTResKindName(TypeTestResolution::Kind K) {
2987 switch (K) {
2988 case TypeTestResolution::Unknown:
2989 return "unknown";
2990 case TypeTestResolution::Unsat:
2991 return "unsat";
2992 case TypeTestResolution::ByteArray:
2993 return "byteArray";
2994 case TypeTestResolution::Inline:
2995 return "inline";
2996 case TypeTestResolution::Single:
2997 return "single";
2998 case TypeTestResolution::AllOnes:
2999 return "allOnes";
3001 llvm_unreachable("invalid TypeTestResolution kind");
3004 void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
3005 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
3006 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
3008 // The following fields are only used if the target does not support the use
3009 // of absolute symbols to store constants. Print only if non-zero.
3010 if (TTRes.AlignLog2)
3011 Out << ", alignLog2: " << TTRes.AlignLog2;
3012 if (TTRes.SizeM1)
3013 Out << ", sizeM1: " << TTRes.SizeM1;
3014 if (TTRes.BitMask)
3015 // BitMask is uint8_t which causes it to print the corresponding char.
3016 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
3017 if (TTRes.InlineBits)
3018 Out << ", inlineBits: " << TTRes.InlineBits;
3020 Out << ")";
3023 void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
3024 Out << ", summary: (";
3025 printTypeTestResolution(TIS.TTRes);
3026 if (!TIS.WPDRes.empty()) {
3027 Out << ", wpdResolutions: (";
3028 FieldSeparator FS;
3029 for (auto &WPDRes : TIS.WPDRes) {
3030 Out << FS;
3031 Out << "(offset: " << WPDRes.first << ", ";
3032 printWPDRes(WPDRes.second);
3033 Out << ")";
3035 Out << ")";
3037 Out << ")";
3040 void AssemblyWriter::printTypeIdCompatibleVtableSummary(
3041 const TypeIdCompatibleVtableInfo &TI) {
3042 Out << ", summary: (";
3043 FieldSeparator FS;
3044 for (auto &P : TI) {
3045 Out << FS;
3046 Out << "(offset: " << P.AddressPointOffset << ", ";
3047 Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID());
3048 Out << ")";
3050 Out << ")";
3053 void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
3054 Out << "args: (";
3055 FieldSeparator FS;
3056 for (auto arg : Args) {
3057 Out << FS;
3058 Out << arg;
3060 Out << ")";
3063 void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
3064 Out << "wpdRes: (kind: ";
3065 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
3067 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
3068 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
3070 if (!WPDRes.ResByArg.empty()) {
3071 Out << ", resByArg: (";
3072 FieldSeparator FS;
3073 for (auto &ResByArg : WPDRes.ResByArg) {
3074 Out << FS;
3075 printArgs(ResByArg.first);
3076 Out << ", byArg: (kind: ";
3077 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
3078 if (ResByArg.second.TheKind ==
3079 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
3080 ResByArg.second.TheKind ==
3081 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
3082 Out << ", info: " << ResByArg.second.Info;
3084 // The following fields are only used if the target does not support the
3085 // use of absolute symbols to store constants. Print only if non-zero.
3086 if (ResByArg.second.Byte || ResByArg.second.Bit)
3087 Out << ", byte: " << ResByArg.second.Byte
3088 << ", bit: " << ResByArg.second.Bit;
3090 Out << ")";
3092 Out << ")";
3094 Out << ")";
3097 static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
3098 switch (SK) {
3099 case GlobalValueSummary::AliasKind:
3100 return "alias";
3101 case GlobalValueSummary::FunctionKind:
3102 return "function";
3103 case GlobalValueSummary::GlobalVarKind:
3104 return "variable";
3106 llvm_unreachable("invalid summary kind");
3109 void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
3110 Out << ", aliasee: ";
3111 // The indexes emitted for distributed backends may not include the
3112 // aliasee summary (only if it is being imported directly). Handle
3113 // that case by just emitting "null" as the aliasee.
3114 if (AS->hasAliasee())
3115 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
3116 else
3117 Out << "null";
3120 void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
3121 auto VTableFuncs = GS->vTableFuncs();
3122 Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", "
3123 << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ", "
3124 << "constant: " << GS->VarFlags.Constant;
3125 if (!VTableFuncs.empty())
3126 Out << ", "
3127 << "vcall_visibility: " << GS->VarFlags.VCallVisibility;
3128 Out << ")";
3130 if (!VTableFuncs.empty()) {
3131 Out << ", vTableFuncs: (";
3132 FieldSeparator FS;
3133 for (auto &P : VTableFuncs) {
3134 Out << FS;
3135 Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID())
3136 << ", offset: " << P.VTableOffset;
3137 Out << ")";
3139 Out << ")";
3143 static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
3144 switch (LT) {
3145 case GlobalValue::ExternalLinkage:
3146 return "external";
3147 case GlobalValue::PrivateLinkage:
3148 return "private";
3149 case GlobalValue::InternalLinkage:
3150 return "internal";
3151 case GlobalValue::LinkOnceAnyLinkage:
3152 return "linkonce";
3153 case GlobalValue::LinkOnceODRLinkage:
3154 return "linkonce_odr";
3155 case GlobalValue::WeakAnyLinkage:
3156 return "weak";
3157 case GlobalValue::WeakODRLinkage:
3158 return "weak_odr";
3159 case GlobalValue::CommonLinkage:
3160 return "common";
3161 case GlobalValue::AppendingLinkage:
3162 return "appending";
3163 case GlobalValue::ExternalWeakLinkage:
3164 return "extern_weak";
3165 case GlobalValue::AvailableExternallyLinkage:
3166 return "available_externally";
3168 llvm_unreachable("invalid linkage");
3171 // When printing the linkage types in IR where the ExternalLinkage is
3172 // not printed, and other linkage types are expected to be printed with
3173 // a space after the name.
3174 static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
3175 if (LT == GlobalValue::ExternalLinkage)
3176 return "";
3177 return getLinkageName(LT) + " ";
3180 static const char *getVisibilityName(GlobalValue::VisibilityTypes Vis) {
3181 switch (Vis) {
3182 case GlobalValue::DefaultVisibility:
3183 return "default";
3184 case GlobalValue::HiddenVisibility:
3185 return "hidden";
3186 case GlobalValue::ProtectedVisibility:
3187 return "protected";
3189 llvm_unreachable("invalid visibility");
3192 void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
3193 Out << ", insts: " << FS->instCount();
3194 if (FS->fflags().anyFlagSet())
3195 Out << ", " << FS->fflags();
3197 if (!FS->calls().empty()) {
3198 Out << ", calls: (";
3199 FieldSeparator IFS;
3200 for (auto &Call : FS->calls()) {
3201 Out << IFS;
3202 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
3203 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
3204 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
3205 else if (Call.second.RelBlockFreq)
3206 Out << ", relbf: " << Call.second.RelBlockFreq;
3207 Out << ")";
3209 Out << ")";
3212 if (const auto *TIdInfo = FS->getTypeIdInfo())
3213 printTypeIdInfo(*TIdInfo);
3215 // The AllocationType identifiers capture the profiled context behavior
3216 // reaching a specific static allocation site (possibly cloned).
3217 auto AllocTypeName = [](uint8_t Type) -> const char * {
3218 switch (Type) {
3219 case (uint8_t)AllocationType::None:
3220 return "none";
3221 case (uint8_t)AllocationType::NotCold:
3222 return "notcold";
3223 case (uint8_t)AllocationType::Cold:
3224 return "cold";
3225 case (uint8_t)AllocationType::Hot:
3226 return "hot";
3228 llvm_unreachable("Unexpected alloc type");
3231 if (!FS->allocs().empty()) {
3232 Out << ", allocs: (";
3233 FieldSeparator AFS;
3234 for (auto &AI : FS->allocs()) {
3235 Out << AFS;
3236 Out << "(versions: (";
3237 FieldSeparator VFS;
3238 for (auto V : AI.Versions) {
3239 Out << VFS;
3240 Out << AllocTypeName(V);
3242 Out << "), memProf: (";
3243 FieldSeparator MIBFS;
3244 for (auto &MIB : AI.MIBs) {
3245 Out << MIBFS;
3246 Out << "(type: " << AllocTypeName((uint8_t)MIB.AllocType);
3247 Out << ", stackIds: (";
3248 FieldSeparator SIDFS;
3249 for (auto Id : MIB.StackIdIndices) {
3250 Out << SIDFS;
3251 Out << TheIndex->getStackIdAtIndex(Id);
3253 Out << "))";
3255 Out << "))";
3257 Out << ")";
3260 if (!FS->callsites().empty()) {
3261 Out << ", callsites: (";
3262 FieldSeparator SNFS;
3263 for (auto &CI : FS->callsites()) {
3264 Out << SNFS;
3265 if (CI.Callee)
3266 Out << "(callee: ^" << Machine.getGUIDSlot(CI.Callee.getGUID());
3267 else
3268 Out << "(callee: null";
3269 Out << ", clones: (";
3270 FieldSeparator VFS;
3271 for (auto V : CI.Clones) {
3272 Out << VFS;
3273 Out << V;
3275 Out << "), stackIds: (";
3276 FieldSeparator SIDFS;
3277 for (auto Id : CI.StackIdIndices) {
3278 Out << SIDFS;
3279 Out << TheIndex->getStackIdAtIndex(Id);
3281 Out << "))";
3283 Out << ")";
3286 auto PrintRange = [&](const ConstantRange &Range) {
3287 Out << "[" << Range.getSignedMin() << ", " << Range.getSignedMax() << "]";
3290 if (!FS->paramAccesses().empty()) {
3291 Out << ", params: (";
3292 FieldSeparator IFS;
3293 for (auto &PS : FS->paramAccesses()) {
3294 Out << IFS;
3295 Out << "(param: " << PS.ParamNo;
3296 Out << ", offset: ";
3297 PrintRange(PS.Use);
3298 if (!PS.Calls.empty()) {
3299 Out << ", calls: (";
3300 FieldSeparator IFS;
3301 for (auto &Call : PS.Calls) {
3302 Out << IFS;
3303 Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee.getGUID());
3304 Out << ", param: " << Call.ParamNo;
3305 Out << ", offset: ";
3306 PrintRange(Call.Offsets);
3307 Out << ")";
3309 Out << ")";
3311 Out << ")";
3313 Out << ")";
3317 void AssemblyWriter::printTypeIdInfo(
3318 const FunctionSummary::TypeIdInfo &TIDInfo) {
3319 Out << ", typeIdInfo: (";
3320 FieldSeparator TIDFS;
3321 if (!TIDInfo.TypeTests.empty()) {
3322 Out << TIDFS;
3323 Out << "typeTests: (";
3324 FieldSeparator FS;
3325 for (auto &GUID : TIDInfo.TypeTests) {
3326 auto TidIter = TheIndex->typeIds().equal_range(GUID);
3327 if (TidIter.first == TidIter.second) {
3328 Out << FS;
3329 Out << GUID;
3330 continue;
3332 // Print all type id that correspond to this GUID.
3333 for (auto It = TidIter.first; It != TidIter.second; ++It) {
3334 Out << FS;
3335 auto Slot = Machine.getTypeIdSlot(It->second.first);
3336 assert(Slot != -1);
3337 Out << "^" << Slot;
3340 Out << ")";
3342 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
3343 Out << TIDFS;
3344 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
3346 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
3347 Out << TIDFS;
3348 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
3350 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
3351 Out << TIDFS;
3352 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
3353 "typeTestAssumeConstVCalls");
3355 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
3356 Out << TIDFS;
3357 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
3358 "typeCheckedLoadConstVCalls");
3360 Out << ")";
3363 void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
3364 auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
3365 if (TidIter.first == TidIter.second) {
3366 Out << "vFuncId: (";
3367 Out << "guid: " << VFId.GUID;
3368 Out << ", offset: " << VFId.Offset;
3369 Out << ")";
3370 return;
3372 // Print all type id that correspond to this GUID.
3373 FieldSeparator FS;
3374 for (auto It = TidIter.first; It != TidIter.second; ++It) {
3375 Out << FS;
3376 Out << "vFuncId: (";
3377 auto Slot = Machine.getTypeIdSlot(It->second.first);
3378 assert(Slot != -1);
3379 Out << "^" << Slot;
3380 Out << ", offset: " << VFId.Offset;
3381 Out << ")";
3385 void AssemblyWriter::printNonConstVCalls(
3386 const std::vector<FunctionSummary::VFuncId> &VCallList, const char *Tag) {
3387 Out << Tag << ": (";
3388 FieldSeparator FS;
3389 for (auto &VFuncId : VCallList) {
3390 Out << FS;
3391 printVFuncId(VFuncId);
3393 Out << ")";
3396 void AssemblyWriter::printConstVCalls(
3397 const std::vector<FunctionSummary::ConstVCall> &VCallList,
3398 const char *Tag) {
3399 Out << Tag << ": (";
3400 FieldSeparator FS;
3401 for (auto &ConstVCall : VCallList) {
3402 Out << FS;
3403 Out << "(";
3404 printVFuncId(ConstVCall.VFunc);
3405 if (!ConstVCall.Args.empty()) {
3406 Out << ", ";
3407 printArgs(ConstVCall.Args);
3409 Out << ")";
3411 Out << ")";
3414 void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
3415 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
3416 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
3417 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
3418 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
3419 << ", flags: (";
3420 Out << "linkage: " << getLinkageName(LT);
3421 Out << ", visibility: "
3422 << getVisibilityName((GlobalValue::VisibilityTypes)GVFlags.Visibility);
3423 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
3424 Out << ", live: " << GVFlags.Live;
3425 Out << ", dsoLocal: " << GVFlags.DSOLocal;
3426 Out << ", canAutoHide: " << GVFlags.CanAutoHide;
3427 Out << ")";
3429 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
3430 printAliasSummary(cast<AliasSummary>(&Summary));
3431 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
3432 printFunctionSummary(cast<FunctionSummary>(&Summary));
3433 else
3434 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
3436 auto RefList = Summary.refs();
3437 if (!RefList.empty()) {
3438 Out << ", refs: (";
3439 FieldSeparator FS;
3440 for (auto &Ref : RefList) {
3441 Out << FS;
3442 if (Ref.isReadOnly())
3443 Out << "readonly ";
3444 else if (Ref.isWriteOnly())
3445 Out << "writeonly ";
3446 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
3448 Out << ")";
3451 Out << ")";
3454 void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3455 Out << "^" << Slot << " = gv: (";
3456 if (!VI.name().empty())
3457 Out << "name: \"" << VI.name() << "\"";
3458 else
3459 Out << "guid: " << VI.getGUID();
3460 if (!VI.getSummaryList().empty()) {
3461 Out << ", summaries: (";
3462 FieldSeparator FS;
3463 for (auto &Summary : VI.getSummaryList()) {
3464 Out << FS;
3465 printSummary(*Summary);
3467 Out << ")";
3469 Out << ")";
3470 if (!VI.name().empty())
3471 Out << " ; guid = " << VI.getGUID();
3472 Out << "\n";
3475 static void printMetadataIdentifier(StringRef Name,
3476 formatted_raw_ostream &Out) {
3477 if (Name.empty()) {
3478 Out << "<empty name> ";
3479 } else {
3480 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3481 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
3482 Out << Name[0];
3483 else
3484 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3485 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3486 unsigned char C = Name[i];
3487 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3488 C == '.' || C == '_')
3489 Out << C;
3490 else
3491 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3496 void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3497 Out << '!';
3498 printMetadataIdentifier(NMD->getName(), Out);
3499 Out << " = !{";
3500 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
3501 if (i)
3502 Out << ", ";
3504 // Write DIExpressions inline.
3505 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3506 MDNode *Op = NMD->getOperand(i);
3507 assert(!isa<DIArgList>(Op) &&
3508 "DIArgLists should not appear in NamedMDNodes");
3509 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3510 writeDIExpression(Out, Expr, AsmWriterContext::getEmpty());
3511 continue;
3514 int Slot = Machine.getMetadataSlot(Op);
3515 if (Slot == -1)
3516 Out << "<badref>";
3517 else
3518 Out << '!' << Slot;
3520 Out << "}\n";
3523 static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
3524 formatted_raw_ostream &Out) {
3525 switch (Vis) {
3526 case GlobalValue::DefaultVisibility: break;
3527 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3528 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3532 static void PrintDSOLocation(const GlobalValue &GV,
3533 formatted_raw_ostream &Out) {
3534 if (GV.isDSOLocal() && !GV.isImplicitDSOLocal())
3535 Out << "dso_local ";
3538 static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3539 formatted_raw_ostream &Out) {
3540 switch (SCT) {
3541 case GlobalValue::DefaultStorageClass: break;
3542 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3543 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3547 static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3548 formatted_raw_ostream &Out) {
3549 switch (TLM) {
3550 case GlobalVariable::NotThreadLocal:
3551 break;
3552 case GlobalVariable::GeneralDynamicTLSModel:
3553 Out << "thread_local ";
3554 break;
3555 case GlobalVariable::LocalDynamicTLSModel:
3556 Out << "thread_local(localdynamic) ";
3557 break;
3558 case GlobalVariable::InitialExecTLSModel:
3559 Out << "thread_local(initialexec) ";
3560 break;
3561 case GlobalVariable::LocalExecTLSModel:
3562 Out << "thread_local(localexec) ";
3563 break;
3567 static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3568 switch (UA) {
3569 case GlobalVariable::UnnamedAddr::None:
3570 return "";
3571 case GlobalVariable::UnnamedAddr::Local:
3572 return "local_unnamed_addr";
3573 case GlobalVariable::UnnamedAddr::Global:
3574 return "unnamed_addr";
3576 llvm_unreachable("Unknown UnnamedAddr");
3579 static void maybePrintComdat(formatted_raw_ostream &Out,
3580 const GlobalObject &GO) {
3581 const Comdat *C = GO.getComdat();
3582 if (!C)
3583 return;
3585 if (isa<GlobalVariable>(GO))
3586 Out << ',';
3587 Out << " comdat";
3589 if (GO.getName() == C->getName())
3590 return;
3592 Out << '(';
3593 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3594 Out << ')';
3597 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
3598 if (GV->isMaterializable())
3599 Out << "; Materializable\n";
3601 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GV->getParent());
3602 WriteAsOperandInternal(Out, GV, WriterCtx);
3603 Out << " = ";
3605 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3606 Out << "external ";
3608 Out << getLinkageNameWithSpace(GV->getLinkage());
3609 PrintDSOLocation(*GV, Out);
3610 PrintVisibility(GV->getVisibility(), Out);
3611 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
3612 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
3613 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3614 if (!UA.empty())
3615 Out << UA << ' ';
3617 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3618 Out << "addrspace(" << AddressSpace << ") ";
3619 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
3620 Out << (GV->isConstant() ? "constant " : "global ");
3621 TypePrinter.print(GV->getValueType(), Out);
3623 if (GV->hasInitializer()) {
3624 Out << ' ';
3625 writeOperand(GV->getInitializer(), false);
3628 if (GV->hasSection()) {
3629 Out << ", section \"";
3630 printEscapedString(GV->getSection(), Out);
3631 Out << '"';
3633 if (GV->hasPartition()) {
3634 Out << ", partition \"";
3635 printEscapedString(GV->getPartition(), Out);
3636 Out << '"';
3639 using SanitizerMetadata = llvm::GlobalValue::SanitizerMetadata;
3640 if (GV->hasSanitizerMetadata()) {
3641 SanitizerMetadata MD = GV->getSanitizerMetadata();
3642 if (MD.NoAddress)
3643 Out << ", no_sanitize_address";
3644 if (MD.NoHWAddress)
3645 Out << ", no_sanitize_hwaddress";
3646 if (MD.Memtag)
3647 Out << ", sanitize_memtag";
3648 if (MD.IsDynInit)
3649 Out << ", sanitize_address_dyninit";
3652 maybePrintComdat(Out, *GV);
3653 if (MaybeAlign A = GV->getAlign())
3654 Out << ", align " << A->value();
3656 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3657 GV->getAllMetadata(MDs);
3658 printMetadataAttachments(MDs, ", ");
3660 auto Attrs = GV->getAttributes();
3661 if (Attrs.hasAttributes())
3662 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3664 printInfoComment(*GV);
3667 void AssemblyWriter::printAlias(const GlobalAlias *GA) {
3668 if (GA->isMaterializable())
3669 Out << "; Materializable\n";
3671 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GA->getParent());
3672 WriteAsOperandInternal(Out, GA, WriterCtx);
3673 Out << " = ";
3675 Out << getLinkageNameWithSpace(GA->getLinkage());
3676 PrintDSOLocation(*GA, Out);
3677 PrintVisibility(GA->getVisibility(), Out);
3678 PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
3679 PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
3680 StringRef UA = getUnnamedAddrEncoding(GA->getUnnamedAddr());
3681 if (!UA.empty())
3682 Out << UA << ' ';
3684 Out << "alias ";
3686 TypePrinter.print(GA->getValueType(), Out);
3687 Out << ", ";
3689 if (const Constant *Aliasee = GA->getAliasee()) {
3690 writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
3691 } else {
3692 TypePrinter.print(GA->getType(), Out);
3693 Out << " <<NULL ALIASEE>>";
3696 if (GA->hasPartition()) {
3697 Out << ", partition \"";
3698 printEscapedString(GA->getPartition(), Out);
3699 Out << '"';
3702 printInfoComment(*GA);
3703 Out << '\n';
3706 void AssemblyWriter::printIFunc(const GlobalIFunc *GI) {
3707 if (GI->isMaterializable())
3708 Out << "; Materializable\n";
3710 AsmWriterContext WriterCtx(&TypePrinter, &Machine, GI->getParent());
3711 WriteAsOperandInternal(Out, GI, WriterCtx);
3712 Out << " = ";
3714 Out << getLinkageNameWithSpace(GI->getLinkage());
3715 PrintDSOLocation(*GI, Out);
3716 PrintVisibility(GI->getVisibility(), Out);
3718 Out << "ifunc ";
3720 TypePrinter.print(GI->getValueType(), Out);
3721 Out << ", ";
3723 if (const Constant *Resolver = GI->getResolver()) {
3724 writeOperand(Resolver, !isa<ConstantExpr>(Resolver));
3725 } else {
3726 TypePrinter.print(GI->getType(), Out);
3727 Out << " <<NULL RESOLVER>>";
3730 if (GI->hasPartition()) {
3731 Out << ", partition \"";
3732 printEscapedString(GI->getPartition(), Out);
3733 Out << '"';
3736 printInfoComment(*GI);
3737 Out << '\n';
3740 void AssemblyWriter::printComdat(const Comdat *C) {
3741 C->print(Out);
3744 void AssemblyWriter::printTypeIdentities() {
3745 if (TypePrinter.empty())
3746 return;
3748 Out << '\n';
3750 // Emit all numbered types.
3751 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3752 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3753 Out << '%' << I << " = type ";
3755 // Make sure we print out at least one level of the type structure, so
3756 // that we do not get %2 = type %2
3757 TypePrinter.printStructBody(NumberedTypes[I], Out);
3758 Out << '\n';
3761 auto &NamedTypes = TypePrinter.getNamedTypes();
3762 for (StructType *NamedType : NamedTypes) {
3763 PrintLLVMName(Out, NamedType->getName(), LocalPrefix);
3764 Out << " = type ";
3766 // Make sure we print out at least one level of the type structure, so
3767 // that we do not get %FILE = type %FILE
3768 TypePrinter.printStructBody(NamedType, Out);
3769 Out << '\n';
3773 /// printFunction - Print all aspects of a function.
3774 void AssemblyWriter::printFunction(const Function *F) {
3775 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
3777 if (F->isMaterializable())
3778 Out << "; Materializable\n";
3780 const AttributeList &Attrs = F->getAttributes();
3781 if (Attrs.hasFnAttrs()) {
3782 AttributeSet AS = Attrs.getFnAttrs();
3783 std::string AttrStr;
3785 for (const Attribute &Attr : AS) {
3786 if (!Attr.isStringAttribute()) {
3787 if (!AttrStr.empty()) AttrStr += ' ';
3788 AttrStr += Attr.getAsString();
3792 if (!AttrStr.empty())
3793 Out << "; Function Attrs: " << AttrStr << '\n';
3796 Machine.incorporateFunction(F);
3798 if (F->isDeclaration()) {
3799 Out << "declare";
3800 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3801 F->getAllMetadata(MDs);
3802 printMetadataAttachments(MDs, " ");
3803 Out << ' ';
3804 } else
3805 Out << "define ";
3807 Out << getLinkageNameWithSpace(F->getLinkage());
3808 PrintDSOLocation(*F, Out);
3809 PrintVisibility(F->getVisibility(), Out);
3810 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
3812 // Print the calling convention.
3813 if (F->getCallingConv() != CallingConv::C) {
3814 PrintCallingConv(F->getCallingConv(), Out);
3815 Out << " ";
3818 FunctionType *FT = F->getFunctionType();
3819 if (Attrs.hasRetAttrs())
3820 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
3821 TypePrinter.print(F->getReturnType(), Out);
3822 AsmWriterContext WriterCtx(&TypePrinter, &Machine, F->getParent());
3823 Out << ' ';
3824 WriteAsOperandInternal(Out, F, WriterCtx);
3825 Out << '(';
3827 // Loop over the arguments, printing them...
3828 if (F->isDeclaration() && !IsForDebug) {
3829 // We're only interested in the type here - don't print argument names.
3830 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
3831 // Insert commas as we go... the first arg doesn't get a comma
3832 if (I)
3833 Out << ", ";
3834 // Output type...
3835 TypePrinter.print(FT->getParamType(I), Out);
3837 AttributeSet ArgAttrs = Attrs.getParamAttrs(I);
3838 if (ArgAttrs.hasAttributes()) {
3839 Out << ' ';
3840 writeAttributeSet(ArgAttrs);
3843 } else {
3844 // The arguments are meaningful here, print them in detail.
3845 for (const Argument &Arg : F->args()) {
3846 // Insert commas as we go... the first arg doesn't get a comma
3847 if (Arg.getArgNo() != 0)
3848 Out << ", ";
3849 printArgument(&Arg, Attrs.getParamAttrs(Arg.getArgNo()));
3853 // Finish printing arguments...
3854 if (FT->isVarArg()) {
3855 if (FT->getNumParams()) Out << ", ";
3856 Out << "..."; // Output varargs portion of signature!
3858 Out << ')';
3859 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3860 if (!UA.empty())
3861 Out << ' ' << UA;
3862 // We print the function address space if it is non-zero or if we are writing
3863 // a module with a non-zero program address space or if there is no valid
3864 // Module* so that the file can be parsed without the datalayout string.
3865 const Module *Mod = F->getParent();
3866 if (F->getAddressSpace() != 0 || !Mod ||
3867 Mod->getDataLayout().getProgramAddressSpace() != 0)
3868 Out << " addrspace(" << F->getAddressSpace() << ")";
3869 if (Attrs.hasFnAttrs())
3870 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttrs());
3871 if (F->hasSection()) {
3872 Out << " section \"";
3873 printEscapedString(F->getSection(), Out);
3874 Out << '"';
3876 if (F->hasPartition()) {
3877 Out << " partition \"";
3878 printEscapedString(F->getPartition(), Out);
3879 Out << '"';
3881 maybePrintComdat(Out, *F);
3882 if (MaybeAlign A = F->getAlign())
3883 Out << " align " << A->value();
3884 if (F->hasGC())
3885 Out << " gc \"" << F->getGC() << '"';
3886 if (F->hasPrefixData()) {
3887 Out << " prefix ";
3888 writeOperand(F->getPrefixData(), true);
3890 if (F->hasPrologueData()) {
3891 Out << " prologue ";
3892 writeOperand(F->getPrologueData(), true);
3894 if (F->hasPersonalityFn()) {
3895 Out << " personality ";
3896 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3899 if (F->isDeclaration()) {
3900 Out << '\n';
3901 } else {
3902 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3903 F->getAllMetadata(MDs);
3904 printMetadataAttachments(MDs, " ");
3906 Out << " {";
3907 // Output all of the function's basic blocks.
3908 for (const BasicBlock &BB : *F)
3909 printBasicBlock(&BB);
3911 // Output the function's use-lists.
3912 printUseLists(F);
3914 Out << "}\n";
3917 Machine.purgeFunction();
3920 /// printArgument - This member is called for every argument that is passed into
3921 /// the function. Simply print it out
3922 void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
3923 // Output type...
3924 TypePrinter.print(Arg->getType(), Out);
3926 // Output parameter attributes list
3927 if (Attrs.hasAttributes()) {
3928 Out << ' ';
3929 writeAttributeSet(Attrs);
3932 // Output name, if available...
3933 if (Arg->hasName()) {
3934 Out << ' ';
3935 PrintLLVMName(Out, Arg);
3936 } else {
3937 int Slot = Machine.getLocalSlot(Arg);
3938 assert(Slot != -1 && "expect argument in function here");
3939 Out << " %" << Slot;
3943 /// printBasicBlock - This member is called for each basic block in a method.
3944 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
3945 bool IsEntryBlock = BB->getParent() && BB->isEntryBlock();
3946 if (BB->hasName()) { // Print out the label if it exists...
3947 Out << "\n";
3948 PrintLLVMName(Out, BB->getName(), LabelPrefix);
3949 Out << ':';
3950 } else if (!IsEntryBlock) {
3951 Out << "\n";
3952 int Slot = Machine.getLocalSlot(BB);
3953 if (Slot != -1)
3954 Out << Slot << ":";
3955 else
3956 Out << "<badref>:";
3959 if (!IsEntryBlock) {
3960 // Output predecessors for the block.
3961 Out.PadToColumn(50);
3962 Out << ";";
3963 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
3965 if (PI == PE) {
3966 Out << " No predecessors!";
3967 } else {
3968 Out << " preds = ";
3969 writeOperand(*PI, false);
3970 for (++PI; PI != PE; ++PI) {
3971 Out << ", ";
3972 writeOperand(*PI, false);
3977 Out << "\n";
3979 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
3981 // Output all of the instructions in the basic block...
3982 for (const Instruction &I : *BB) {
3983 printInstructionLine(I);
3986 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
3989 /// printInstructionLine - Print an instruction and a newline character.
3990 void AssemblyWriter::printInstructionLine(const Instruction &I) {
3991 printInstruction(I);
3992 Out << '\n';
3995 /// printGCRelocateComment - print comment after call to the gc.relocate
3996 /// intrinsic indicating base and derived pointer names.
3997 void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
3998 Out << " ; (";
3999 writeOperand(Relocate.getBasePtr(), false);
4000 Out << ", ";
4001 writeOperand(Relocate.getDerivedPtr(), false);
4002 Out << ")";
4005 /// printInfoComment - Print a little comment after the instruction indicating
4006 /// which slot it occupies.
4007 void AssemblyWriter::printInfoComment(const Value &V) {
4008 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
4009 printGCRelocateComment(*Relocate);
4011 if (AnnotationWriter)
4012 AnnotationWriter->printInfoComment(V, Out);
4015 static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
4016 raw_ostream &Out) {
4017 // We print the address space of the call if it is non-zero.
4018 if (Operand == nullptr) {
4019 Out << " <cannot get addrspace!>";
4020 return;
4022 unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
4023 bool PrintAddrSpace = CallAddrSpace != 0;
4024 if (!PrintAddrSpace) {
4025 const Module *Mod = getModuleFromVal(I);
4026 // We also print it if it is zero but not equal to the program address space
4027 // or if we can't find a valid Module* to make it possible to parse
4028 // the resulting file even without a datalayout string.
4029 if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
4030 PrintAddrSpace = true;
4032 if (PrintAddrSpace)
4033 Out << " addrspace(" << CallAddrSpace << ")";
4036 // This member is called for each Instruction in a function..
4037 void AssemblyWriter::printInstruction(const Instruction &I) {
4038 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
4040 // Print out indentation for an instruction.
4041 Out << " ";
4043 // Print out name if it exists...
4044 if (I.hasName()) {
4045 PrintLLVMName(Out, &I);
4046 Out << " = ";
4047 } else if (!I.getType()->isVoidTy()) {
4048 // Print out the def slot taken.
4049 int SlotNum = Machine.getLocalSlot(&I);
4050 if (SlotNum == -1)
4051 Out << "<badref> = ";
4052 else
4053 Out << '%' << SlotNum << " = ";
4056 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
4057 if (CI->isMustTailCall())
4058 Out << "musttail ";
4059 else if (CI->isTailCall())
4060 Out << "tail ";
4061 else if (CI->isNoTailCall())
4062 Out << "notail ";
4065 // Print out the opcode...
4066 Out << I.getOpcodeName();
4068 // If this is an atomic load or store, print out the atomic marker.
4069 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
4070 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
4071 Out << " atomic";
4073 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
4074 Out << " weak";
4076 // If this is a volatile operation, print out the volatile marker.
4077 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
4078 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
4079 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
4080 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
4081 Out << " volatile";
4083 // Print out optimization information.
4084 WriteOptimizationInfo(Out, &I);
4086 // Print out the compare instruction predicates
4087 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
4088 Out << ' ' << CI->getPredicate();
4090 // Print out the atomicrmw operation
4091 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
4092 Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
4094 // Print out the type of the operands...
4095 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
4097 // Special case conditional branches to swizzle the condition out to the front
4098 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
4099 const BranchInst &BI(cast<BranchInst>(I));
4100 Out << ' ';
4101 writeOperand(BI.getCondition(), true);
4102 Out << ", ";
4103 writeOperand(BI.getSuccessor(0), true);
4104 Out << ", ";
4105 writeOperand(BI.getSuccessor(1), true);
4107 } else if (isa<SwitchInst>(I)) {
4108 const SwitchInst& SI(cast<SwitchInst>(I));
4109 // Special case switch instruction to get formatting nice and correct.
4110 Out << ' ';
4111 writeOperand(SI.getCondition(), true);
4112 Out << ", ";
4113 writeOperand(SI.getDefaultDest(), true);
4114 Out << " [";
4115 for (auto Case : SI.cases()) {
4116 Out << "\n ";
4117 writeOperand(Case.getCaseValue(), true);
4118 Out << ", ";
4119 writeOperand(Case.getCaseSuccessor(), true);
4121 Out << "\n ]";
4122 } else if (isa<IndirectBrInst>(I)) {
4123 // Special case indirectbr instruction to get formatting nice and correct.
4124 Out << ' ';
4125 writeOperand(Operand, true);
4126 Out << ", [";
4128 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
4129 if (i != 1)
4130 Out << ", ";
4131 writeOperand(I.getOperand(i), true);
4133 Out << ']';
4134 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
4135 Out << ' ';
4136 TypePrinter.print(I.getType(), Out);
4137 Out << ' ';
4139 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
4140 if (op) Out << ", ";
4141 Out << "[ ";
4142 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
4143 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
4145 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
4146 Out << ' ';
4147 writeOperand(I.getOperand(0), true);
4148 for (unsigned i : EVI->indices())
4149 Out << ", " << i;
4150 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
4151 Out << ' ';
4152 writeOperand(I.getOperand(0), true); Out << ", ";
4153 writeOperand(I.getOperand(1), true);
4154 for (unsigned i : IVI->indices())
4155 Out << ", " << i;
4156 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
4157 Out << ' ';
4158 TypePrinter.print(I.getType(), Out);
4159 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
4160 Out << '\n';
4162 if (LPI->isCleanup())
4163 Out << " cleanup";
4165 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
4166 if (i != 0 || LPI->isCleanup()) Out << "\n";
4167 if (LPI->isCatch(i))
4168 Out << " catch ";
4169 else
4170 Out << " filter ";
4172 writeOperand(LPI->getClause(i), true);
4174 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
4175 Out << " within ";
4176 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
4177 Out << " [";
4178 unsigned Op = 0;
4179 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
4180 if (Op > 0)
4181 Out << ", ";
4182 writeOperand(PadBB, /*PrintType=*/true);
4183 ++Op;
4185 Out << "] unwind ";
4186 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
4187 writeOperand(UnwindDest, /*PrintType=*/true);
4188 else
4189 Out << "to caller";
4190 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
4191 Out << " within ";
4192 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
4193 Out << " [";
4194 for (unsigned Op = 0, NumOps = FPI->arg_size(); Op < NumOps; ++Op) {
4195 if (Op > 0)
4196 Out << ", ";
4197 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
4199 Out << ']';
4200 } else if (isa<ReturnInst>(I) && !Operand) {
4201 Out << " void";
4202 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
4203 Out << " from ";
4204 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
4206 Out << " to ";
4207 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
4208 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
4209 Out << " from ";
4210 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
4212 Out << " unwind ";
4213 if (CRI->hasUnwindDest())
4214 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
4215 else
4216 Out << "to caller";
4217 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
4218 // Print the calling convention being used.
4219 if (CI->getCallingConv() != CallingConv::C) {
4220 Out << " ";
4221 PrintCallingConv(CI->getCallingConv(), Out);
4224 Operand = CI->getCalledOperand();
4225 FunctionType *FTy = CI->getFunctionType();
4226 Type *RetTy = FTy->getReturnType();
4227 const AttributeList &PAL = CI->getAttributes();
4229 if (PAL.hasRetAttrs())
4230 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4232 // Only print addrspace(N) if necessary:
4233 maybePrintCallAddrSpace(Operand, &I, Out);
4235 // If possible, print out the short form of the call instruction. We can
4236 // only do this if the first argument is a pointer to a nonvararg function,
4237 // and if the return type is not a pointer to a function.
4238 Out << ' ';
4239 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4240 Out << ' ';
4241 writeOperand(Operand, false);
4242 Out << '(';
4243 for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) {
4244 if (op > 0)
4245 Out << ", ";
4246 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op));
4249 // Emit an ellipsis if this is a musttail call in a vararg function. This
4250 // is only to aid readability, musttail calls forward varargs by default.
4251 if (CI->isMustTailCall() && CI->getParent() &&
4252 CI->getParent()->getParent() &&
4253 CI->getParent()->getParent()->isVarArg()) {
4254 if (CI->arg_size() > 0)
4255 Out << ", ";
4256 Out << "...";
4259 Out << ')';
4260 if (PAL.hasFnAttrs())
4261 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4263 writeOperandBundles(CI);
4264 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
4265 Operand = II->getCalledOperand();
4266 FunctionType *FTy = II->getFunctionType();
4267 Type *RetTy = FTy->getReturnType();
4268 const AttributeList &PAL = II->getAttributes();
4270 // Print the calling convention being used.
4271 if (II->getCallingConv() != CallingConv::C) {
4272 Out << " ";
4273 PrintCallingConv(II->getCallingConv(), Out);
4276 if (PAL.hasRetAttrs())
4277 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4279 // Only print addrspace(N) if necessary:
4280 maybePrintCallAddrSpace(Operand, &I, Out);
4282 // If possible, print out the short form of the invoke instruction. We can
4283 // only do this if the first argument is a pointer to a nonvararg function,
4284 // and if the return type is not a pointer to a function.
4286 Out << ' ';
4287 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4288 Out << ' ';
4289 writeOperand(Operand, false);
4290 Out << '(';
4291 for (unsigned op = 0, Eop = II->arg_size(); op < Eop; ++op) {
4292 if (op)
4293 Out << ", ";
4294 writeParamOperand(II->getArgOperand(op), PAL.getParamAttrs(op));
4297 Out << ')';
4298 if (PAL.hasFnAttrs())
4299 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4301 writeOperandBundles(II);
4303 Out << "\n to ";
4304 writeOperand(II->getNormalDest(), true);
4305 Out << " unwind ";
4306 writeOperand(II->getUnwindDest(), true);
4307 } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
4308 Operand = CBI->getCalledOperand();
4309 FunctionType *FTy = CBI->getFunctionType();
4310 Type *RetTy = FTy->getReturnType();
4311 const AttributeList &PAL = CBI->getAttributes();
4313 // Print the calling convention being used.
4314 if (CBI->getCallingConv() != CallingConv::C) {
4315 Out << " ";
4316 PrintCallingConv(CBI->getCallingConv(), Out);
4319 if (PAL.hasRetAttrs())
4320 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
4322 // If possible, print out the short form of the callbr instruction. We can
4323 // only do this if the first argument is a pointer to a nonvararg function,
4324 // and if the return type is not a pointer to a function.
4326 Out << ' ';
4327 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
4328 Out << ' ';
4329 writeOperand(Operand, false);
4330 Out << '(';
4331 for (unsigned op = 0, Eop = CBI->arg_size(); op < Eop; ++op) {
4332 if (op)
4333 Out << ", ";
4334 writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttrs(op));
4337 Out << ')';
4338 if (PAL.hasFnAttrs())
4339 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
4341 writeOperandBundles(CBI);
4343 Out << "\n to ";
4344 writeOperand(CBI->getDefaultDest(), true);
4345 Out << " [";
4346 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) {
4347 if (i != 0)
4348 Out << ", ";
4349 writeOperand(CBI->getIndirectDest(i), true);
4351 Out << ']';
4352 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
4353 Out << ' ';
4354 if (AI->isUsedWithInAlloca())
4355 Out << "inalloca ";
4356 if (AI->isSwiftError())
4357 Out << "swifterror ";
4358 TypePrinter.print(AI->getAllocatedType(), Out);
4360 // Explicitly write the array size if the code is broken, if it's an array
4361 // allocation, or if the type is not canonical for scalar allocations. The
4362 // latter case prevents the type from mutating when round-tripping through
4363 // assembly.
4364 if (!AI->getArraySize() || AI->isArrayAllocation() ||
4365 !AI->getArraySize()->getType()->isIntegerTy(32)) {
4366 Out << ", ";
4367 writeOperand(AI->getArraySize(), true);
4369 if (MaybeAlign A = AI->getAlign()) {
4370 Out << ", align " << A->value();
4373 unsigned AddrSpace = AI->getAddressSpace();
4374 if (AddrSpace != 0) {
4375 Out << ", addrspace(" << AddrSpace << ')';
4377 } else if (isa<CastInst>(I)) {
4378 if (Operand) {
4379 Out << ' ';
4380 writeOperand(Operand, true); // Work with broken code
4382 Out << " to ";
4383 TypePrinter.print(I.getType(), Out);
4384 } else if (isa<VAArgInst>(I)) {
4385 if (Operand) {
4386 Out << ' ';
4387 writeOperand(Operand, true); // Work with broken code
4389 Out << ", ";
4390 TypePrinter.print(I.getType(), Out);
4391 } else if (Operand) { // Print the normal way.
4392 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
4393 Out << ' ';
4394 TypePrinter.print(GEP->getSourceElementType(), Out);
4395 Out << ',';
4396 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
4397 Out << ' ';
4398 TypePrinter.print(LI->getType(), Out);
4399 Out << ',';
4402 // PrintAllTypes - Instructions who have operands of all the same type
4403 // omit the type from all but the first operand. If the instruction has
4404 // different type operands (for example br), then they are all printed.
4405 bool PrintAllTypes = false;
4406 Type *TheType = Operand->getType();
4408 // Select, Store, ShuffleVector, CmpXchg and AtomicRMW always print all
4409 // types.
4410 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) ||
4411 isa<ReturnInst>(I) || isa<AtomicCmpXchgInst>(I) ||
4412 isa<AtomicRMWInst>(I)) {
4413 PrintAllTypes = true;
4414 } else {
4415 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
4416 Operand = I.getOperand(i);
4417 // note that Operand shouldn't be null, but the test helps make dump()
4418 // more tolerant of malformed IR
4419 if (Operand && Operand->getType() != TheType) {
4420 PrintAllTypes = true; // We have differing types! Print them all!
4421 break;
4426 if (!PrintAllTypes) {
4427 Out << ' ';
4428 TypePrinter.print(TheType, Out);
4431 Out << ' ';
4432 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
4433 if (i) Out << ", ";
4434 writeOperand(I.getOperand(i), PrintAllTypes);
4438 // Print atomic ordering/alignment for memory operations
4439 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
4440 if (LI->isAtomic())
4441 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
4442 if (MaybeAlign A = LI->getAlign())
4443 Out << ", align " << A->value();
4444 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
4445 if (SI->isAtomic())
4446 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
4447 if (MaybeAlign A = SI->getAlign())
4448 Out << ", align " << A->value();
4449 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
4450 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
4451 CXI->getFailureOrdering(), CXI->getSyncScopeID());
4452 Out << ", align " << CXI->getAlign().value();
4453 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
4454 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
4455 RMWI->getSyncScopeID());
4456 Out << ", align " << RMWI->getAlign().value();
4457 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
4458 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
4459 } else if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(&I)) {
4460 PrintShuffleMask(Out, SVI->getType(), SVI->getShuffleMask());
4463 // Print Metadata info.
4464 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
4465 I.getAllMetadata(InstMD);
4466 printMetadataAttachments(InstMD, ", ");
4468 // Print a nice comment.
4469 printInfoComment(I);
4472 void AssemblyWriter::printMetadataAttachments(
4473 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
4474 StringRef Separator) {
4475 if (MDs.empty())
4476 return;
4478 if (MDNames.empty())
4479 MDs[0].second->getContext().getMDKindNames(MDNames);
4481 auto WriterCtx = getContext();
4482 for (const auto &I : MDs) {
4483 unsigned Kind = I.first;
4484 Out << Separator;
4485 if (Kind < MDNames.size()) {
4486 Out << "!";
4487 printMetadataIdentifier(MDNames[Kind], Out);
4488 } else
4489 Out << "!<unknown kind #" << Kind << ">";
4490 Out << ' ';
4491 WriteAsOperandInternal(Out, I.second, WriterCtx);
4495 void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
4496 Out << '!' << Slot << " = ";
4497 printMDNodeBody(Node);
4498 Out << "\n";
4501 void AssemblyWriter::writeAllMDNodes() {
4502 SmallVector<const MDNode *, 16> Nodes;
4503 Nodes.resize(Machine.mdn_size());
4504 for (auto &I : llvm::make_range(Machine.mdn_begin(), Machine.mdn_end()))
4505 Nodes[I.second] = cast<MDNode>(I.first);
4507 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4508 writeMDNode(i, Nodes[i]);
4512 void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
4513 auto WriterCtx = getContext();
4514 WriteMDNodeBodyInternal(Out, Node, WriterCtx);
4517 void AssemblyWriter::writeAttribute(const Attribute &Attr, bool InAttrGroup) {
4518 if (!Attr.isTypeAttribute()) {
4519 Out << Attr.getAsString(InAttrGroup);
4520 return;
4523 Out << Attribute::getNameFromAttrKind(Attr.getKindAsEnum());
4524 if (Type *Ty = Attr.getValueAsType()) {
4525 Out << '(';
4526 TypePrinter.print(Ty, Out);
4527 Out << ')';
4531 void AssemblyWriter::writeAttributeSet(const AttributeSet &AttrSet,
4532 bool InAttrGroup) {
4533 bool FirstAttr = true;
4534 for (const auto &Attr : AttrSet) {
4535 if (!FirstAttr)
4536 Out << ' ';
4537 writeAttribute(Attr, InAttrGroup);
4538 FirstAttr = false;
4542 void AssemblyWriter::writeAllAttributeGroups() {
4543 std::vector<std::pair<AttributeSet, unsigned>> asVec;
4544 asVec.resize(Machine.as_size());
4546 for (auto &I : llvm::make_range(Machine.as_begin(), Machine.as_end()))
4547 asVec[I.second] = I;
4549 for (const auto &I : asVec)
4550 Out << "attributes #" << I.second << " = { "
4551 << I.first.getAsString(true) << " }\n";
4554 void AssemblyWriter::printUseListOrder(const Value *V,
4555 const std::vector<unsigned> &Shuffle) {
4556 bool IsInFunction = Machine.getFunction();
4557 if (IsInFunction)
4558 Out << " ";
4560 Out << "uselistorder";
4561 if (const BasicBlock *BB = IsInFunction ? nullptr : dyn_cast<BasicBlock>(V)) {
4562 Out << "_bb ";
4563 writeOperand(BB->getParent(), false);
4564 Out << ", ";
4565 writeOperand(BB, false);
4566 } else {
4567 Out << " ";
4568 writeOperand(V, true);
4570 Out << ", { ";
4572 assert(Shuffle.size() >= 2 && "Shuffle too small");
4573 Out << Shuffle[0];
4574 for (unsigned I = 1, E = Shuffle.size(); I != E; ++I)
4575 Out << ", " << Shuffle[I];
4576 Out << " }\n";
4579 void AssemblyWriter::printUseLists(const Function *F) {
4580 auto It = UseListOrders.find(F);
4581 if (It == UseListOrders.end())
4582 return;
4584 Out << "\n; uselistorder directives\n";
4585 for (const auto &Pair : It->second)
4586 printUseListOrder(Pair.first, Pair.second);
4589 //===----------------------------------------------------------------------===//
4590 // External Interface declarations
4591 //===----------------------------------------------------------------------===//
4593 void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4594 bool ShouldPreserveUseListOrder,
4595 bool IsForDebug) const {
4596 SlotTracker SlotTable(this->getParent());
4597 formatted_raw_ostream OS(ROS);
4598 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
4599 IsForDebug,
4600 ShouldPreserveUseListOrder);
4601 W.printFunction(this);
4604 void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4605 bool ShouldPreserveUseListOrder,
4606 bool IsForDebug) const {
4607 SlotTracker SlotTable(this->getParent());
4608 formatted_raw_ostream OS(ROS);
4609 AssemblyWriter W(OS, SlotTable, this->getModule(), AAW,
4610 IsForDebug,
4611 ShouldPreserveUseListOrder);
4612 W.printBasicBlock(this);
4615 void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4616 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
4617 SlotTracker SlotTable(this);
4618 formatted_raw_ostream OS(ROS);
4619 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
4620 ShouldPreserveUseListOrder);
4621 W.printModule(this);
4624 void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
4625 SlotTracker SlotTable(getParent());
4626 formatted_raw_ostream OS(ROS);
4627 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
4628 W.printNamedMDNode(this);
4631 void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4632 bool IsForDebug) const {
4633 std::optional<SlotTracker> LocalST;
4634 SlotTracker *SlotTable;
4635 if (auto *ST = MST.getMachine())
4636 SlotTable = ST;
4637 else {
4638 LocalST.emplace(getParent());
4639 SlotTable = &*LocalST;
4642 formatted_raw_ostream OS(ROS);
4643 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4644 W.printNamedMDNode(this);
4647 void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
4648 PrintLLVMName(ROS, getName(), ComdatPrefix);
4649 ROS << " = comdat ";
4651 switch (getSelectionKind()) {
4652 case Comdat::Any:
4653 ROS << "any";
4654 break;
4655 case Comdat::ExactMatch:
4656 ROS << "exactmatch";
4657 break;
4658 case Comdat::Largest:
4659 ROS << "largest";
4660 break;
4661 case Comdat::NoDeduplicate:
4662 ROS << "nodeduplicate";
4663 break;
4664 case Comdat::SameSize:
4665 ROS << "samesize";
4666 break;
4669 ROS << '\n';
4672 void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
4673 TypePrinting TP;
4674 TP.print(const_cast<Type*>(this), OS);
4676 if (NoDetails)
4677 return;
4679 // If the type is a named struct type, print the body as well.
4680 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
4681 if (!STy->isLiteral()) {
4682 OS << " = type ";
4683 TP.printStructBody(STy, OS);
4687 static bool isReferencingMDNode(const Instruction &I) {
4688 if (const auto *CI = dyn_cast<CallInst>(&I))
4689 if (Function *F = CI->getCalledFunction())
4690 if (F->isIntrinsic())
4691 for (auto &Op : I.operands())
4692 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4693 if (isa<MDNode>(V->getMetadata()))
4694 return true;
4695 return false;
4698 void Value::print(raw_ostream &ROS, bool IsForDebug) const {
4699 bool ShouldInitializeAllMetadata = false;
4700 if (auto *I = dyn_cast<Instruction>(this))
4701 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4702 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4703 ShouldInitializeAllMetadata = true;
4705 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
4706 print(ROS, MST, IsForDebug);
4709 void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4710 bool IsForDebug) const {
4711 formatted_raw_ostream OS(ROS);
4712 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4713 SlotTracker &SlotTable =
4714 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4715 auto incorporateFunction = [&](const Function *F) {
4716 if (F)
4717 MST.incorporateFunction(*F);
4720 if (const Instruction *I = dyn_cast<Instruction>(this)) {
4721 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
4722 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
4723 W.printInstruction(*I);
4724 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
4725 incorporateFunction(BB->getParent());
4726 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
4727 W.printBasicBlock(BB);
4728 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
4729 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
4730 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4731 W.printGlobal(V);
4732 else if (const Function *F = dyn_cast<Function>(GV))
4733 W.printFunction(F);
4734 else if (const GlobalAlias *A = dyn_cast<GlobalAlias>(GV))
4735 W.printAlias(A);
4736 else if (const GlobalIFunc *I = dyn_cast<GlobalIFunc>(GV))
4737 W.printIFunc(I);
4738 else
4739 llvm_unreachable("Unknown GlobalValue to print out!");
4740 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
4741 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
4742 } else if (const Constant *C = dyn_cast<Constant>(this)) {
4743 TypePrinting TypePrinter;
4744 TypePrinter.print(C->getType(), OS);
4745 OS << ' ';
4746 AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine());
4747 WriteConstantInternal(OS, C, WriterCtx);
4748 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
4749 this->printAsOperand(OS, /* PrintType */ true, MST);
4750 } else {
4751 llvm_unreachable("Unknown value to print out!");
4755 /// Print without a type, skipping the TypePrinting object.
4757 /// \return \c true iff printing was successful.
4758 static bool printWithoutType(const Value &V, raw_ostream &O,
4759 SlotTracker *Machine, const Module *M) {
4760 if (V.hasName() || isa<GlobalValue>(V) ||
4761 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4762 AsmWriterContext WriterCtx(nullptr, Machine, M);
4763 WriteAsOperandInternal(O, &V, WriterCtx);
4764 return true;
4766 return false;
4769 static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4770 ModuleSlotTracker &MST) {
4771 TypePrinting TypePrinter(MST.getModule());
4772 if (PrintType) {
4773 TypePrinter.print(V.getType(), O);
4774 O << ' ';
4777 AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine(), MST.getModule());
4778 WriteAsOperandInternal(O, &V, WriterCtx);
4781 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4782 const Module *M) const {
4783 if (!M)
4784 M = getModuleFromVal(this);
4786 if (!PrintType)
4787 if (printWithoutType(*this, O, nullptr, M))
4788 return;
4790 SlotTracker Machine(
4791 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4792 ModuleSlotTracker MST(Machine, M);
4793 printAsOperandImpl(*this, O, PrintType, MST);
4796 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4797 ModuleSlotTracker &MST) const {
4798 if (!PrintType)
4799 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4800 return;
4802 printAsOperandImpl(*this, O, PrintType, MST);
4805 /// Recursive version of printMetadataImpl.
4806 static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD,
4807 AsmWriterContext &WriterCtx) {
4808 formatted_raw_ostream OS(ROS);
4809 WriteAsOperandInternal(OS, &MD, WriterCtx, /* FromValue */ true);
4811 auto *N = dyn_cast<MDNode>(&MD);
4812 if (!N || isa<DIExpression>(MD) || isa<DIArgList>(MD))
4813 return;
4815 OS << " = ";
4816 WriteMDNodeBodyInternal(OS, N, WriterCtx);
4819 namespace {
4820 struct MDTreeAsmWriterContext : public AsmWriterContext {
4821 unsigned Level;
4822 // {Level, Printed string}
4823 using EntryTy = std::pair<unsigned, std::string>;
4824 SmallVector<EntryTy, 4> Buffer;
4826 // Used to break the cycle in case there is any.
4827 SmallPtrSet<const Metadata *, 4> Visited;
4829 raw_ostream &MainOS;
4831 MDTreeAsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M,
4832 raw_ostream &OS, const Metadata *InitMD)
4833 : AsmWriterContext(TP, ST, M), Level(0U), Visited({InitMD}), MainOS(OS) {}
4835 void onWriteMetadataAsOperand(const Metadata *MD) override {
4836 if (!Visited.insert(MD).second)
4837 return;
4839 std::string Str;
4840 raw_string_ostream SS(Str);
4841 ++Level;
4842 // A placeholder entry to memorize the correct
4843 // position in buffer.
4844 Buffer.emplace_back(std::make_pair(Level, ""));
4845 unsigned InsertIdx = Buffer.size() - 1;
4847 printMetadataImplRec(SS, *MD, *this);
4848 Buffer[InsertIdx].second = std::move(SS.str());
4849 --Level;
4852 ~MDTreeAsmWriterContext() {
4853 for (const auto &Entry : Buffer) {
4854 MainOS << "\n";
4855 unsigned NumIndent = Entry.first * 2U;
4856 MainOS.indent(NumIndent) << Entry.second;
4860 } // end anonymous namespace
4862 static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
4863 ModuleSlotTracker &MST, const Module *M,
4864 bool OnlyAsOperand, bool PrintAsTree = false) {
4865 formatted_raw_ostream OS(ROS);
4867 TypePrinting TypePrinter(M);
4869 std::unique_ptr<AsmWriterContext> WriterCtx;
4870 if (PrintAsTree && !OnlyAsOperand)
4871 WriterCtx = std::make_unique<MDTreeAsmWriterContext>(
4872 &TypePrinter, MST.getMachine(), M, OS, &MD);
4873 else
4874 WriterCtx =
4875 std::make_unique<AsmWriterContext>(&TypePrinter, MST.getMachine(), M);
4877 WriteAsOperandInternal(OS, &MD, *WriterCtx, /* FromValue */ true);
4879 auto *N = dyn_cast<MDNode>(&MD);
4880 if (OnlyAsOperand || !N || isa<DIExpression>(MD) || isa<DIArgList>(MD))
4881 return;
4883 OS << " = ";
4884 WriteMDNodeBodyInternal(OS, N, *WriterCtx);
4887 void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
4888 ModuleSlotTracker MST(M, isa<MDNode>(this));
4889 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4892 void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4893 const Module *M) const {
4894 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4897 void Metadata::print(raw_ostream &OS, const Module *M,
4898 bool /*IsForDebug*/) const {
4899 ModuleSlotTracker MST(M, isa<MDNode>(this));
4900 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4903 void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
4904 const Module *M, bool /*IsForDebug*/) const {
4905 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4908 void MDNode::printTree(raw_ostream &OS, const Module *M) const {
4909 ModuleSlotTracker MST(M, true);
4910 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
4911 /*PrintAsTree=*/true);
4914 void MDNode::printTree(raw_ostream &OS, ModuleSlotTracker &MST,
4915 const Module *M) const {
4916 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
4917 /*PrintAsTree=*/true);
4920 void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4921 SlotTracker SlotTable(this);
4922 formatted_raw_ostream OS(ROS);
4923 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4924 W.printModuleSummaryIndex();
4927 void ModuleSlotTracker::collectMDNodes(MachineMDNodeListType &L, unsigned LB,
4928 unsigned UB) const {
4929 SlotTracker *ST = MachineStorage.get();
4930 if (!ST)
4931 return;
4933 for (auto &I : llvm::make_range(ST->mdn_begin(), ST->mdn_end()))
4934 if (I.second >= LB && I.second < UB)
4935 L.push_back(std::make_pair(I.second, I.first));
4938 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4939 // Value::dump - allow easy printing of Values from the debugger.
4940 LLVM_DUMP_METHOD
4941 void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4943 // Type::dump - allow easy printing of Types from the debugger.
4944 LLVM_DUMP_METHOD
4945 void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4947 // Module::dump() - Allow printing of Modules from the debugger.
4948 LLVM_DUMP_METHOD
4949 void Module::dump() const {
4950 print(dbgs(), nullptr,
4951 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4954 // Allow printing of Comdats from the debugger.
4955 LLVM_DUMP_METHOD
4956 void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4958 // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
4959 LLVM_DUMP_METHOD
4960 void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4962 LLVM_DUMP_METHOD
4963 void Metadata::dump() const { dump(nullptr); }
4965 LLVM_DUMP_METHOD
4966 void Metadata::dump(const Module *M) const {
4967 print(dbgs(), M, /*IsForDebug=*/true);
4968 dbgs() << '\n';
4971 LLVM_DUMP_METHOD
4972 void MDNode::dumpTree() const { dumpTree(nullptr); }
4974 LLVM_DUMP_METHOD
4975 void MDNode::dumpTree(const Module *M) const {
4976 printTree(dbgs(), M);
4977 dbgs() << '\n';
4980 // Allow printing of ModuleSummaryIndex from the debugger.
4981 LLVM_DUMP_METHOD
4982 void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4983 #endif