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

source code of llvm/lib/IR/AsmWriter.cpp