[InstCombine] Signed saturation patterns
[llvm-core.git] / lib / IR / AsmWriter.cpp
blobb0c26e0ecaf5f52e8bbafcacabd06558b2a8e2e6
1 //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This library implements `print` family of functions in classes like
10 // Module, Function, Value, etc. In-memory representation of those classes is
11 // converted to IR strings.
13 // Note that these routines must be extremely tolerant of various errors in the
14 // LLVM code, because it can be used for debugging transformations.
16 //===----------------------------------------------------------------------===//
18 #include "llvm/ADT/APFloat.h"
19 #include "llvm/ADT/APInt.h"
20 #include "llvm/ADT/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/None.h"
23 #include "llvm/ADT/Optional.h"
24 #include "llvm/ADT/STLExtras.h"
25 #include "llvm/ADT/SetVector.h"
26 #include "llvm/ADT/SmallString.h"
27 #include "llvm/ADT/SmallVector.h"
28 #include "llvm/ADT/StringExtras.h"
29 #include "llvm/ADT/StringRef.h"
30 #include "llvm/ADT/iterator_range.h"
31 #include "llvm/BinaryFormat/Dwarf.h"
32 #include "llvm/Config/llvm-config.h"
33 #include "llvm/IR/Argument.h"
34 #include "llvm/IR/AssemblyAnnotationWriter.h"
35 #include "llvm/IR/Attributes.h"
36 #include "llvm/IR/BasicBlock.h"
37 #include "llvm/IR/CFG.h"
38 #include "llvm/IR/CallingConv.h"
39 #include "llvm/IR/Comdat.h"
40 #include "llvm/IR/Constant.h"
41 #include "llvm/IR/Constants.h"
42 #include "llvm/IR/DebugInfoMetadata.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/GlobalIndirectSymbol.h"
48 #include "llvm/IR/GlobalObject.h"
49 #include "llvm/IR/GlobalValue.h"
50 #include "llvm/IR/GlobalVariable.h"
51 #include "llvm/IR/IRPrintingPasses.h"
52 #include "llvm/IR/InlineAsm.h"
53 #include "llvm/IR/InstrTypes.h"
54 #include "llvm/IR/Instruction.h"
55 #include "llvm/IR/Instructions.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/Statepoint.h"
63 #include "llvm/IR/Type.h"
64 #include "llvm/IR/TypeFinder.h"
65 #include "llvm/IR/Use.h"
66 #include "llvm/IR/UseListOrder.h"
67 #include "llvm/IR/User.h"
68 #include "llvm/IR/Value.h"
69 #include "llvm/Support/AtomicOrdering.h"
70 #include "llvm/Support/Casting.h"
71 #include "llvm/Support/Compiler.h"
72 #include "llvm/Support/Debug.h"
73 #include "llvm/Support/ErrorHandling.h"
74 #include "llvm/Support/Format.h"
75 #include "llvm/Support/FormattedStream.h"
76 #include "llvm/Support/raw_ostream.h"
77 #include <algorithm>
78 #include <cassert>
79 #include <cctype>
80 #include <cstddef>
81 #include <cstdint>
82 #include <iterator>
83 #include <memory>
84 #include <string>
85 #include <tuple>
86 #include <utility>
87 #include <vector>
89 using namespace llvm;
91 // Make virtual table appear in this compilation unit.
92 AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
94 //===----------------------------------------------------------------------===//
95 // Helper Functions
96 //===----------------------------------------------------------------------===//
98 namespace {
100 struct OrderMap {
101 DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
103 unsigned size() const { return IDs.size(); }
104 std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
106 std::pair<unsigned, bool> lookup(const Value *V) const {
107 return IDs.lookup(V);
110 void index(const Value *V) {
111 // Explicitly sequence get-size and insert-value operations to avoid UB.
112 unsigned ID = IDs.size() + 1;
113 IDs[V].first = ID;
117 } // end anonymous namespace
119 static void orderValue(const Value *V, OrderMap &OM) {
120 if (OM.lookup(V).first)
121 return;
123 if (const Constant *C = dyn_cast<Constant>(V))
124 if (C->getNumOperands() && !isa<GlobalValue>(C))
125 for (const Value *Op : C->operands())
126 if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
127 orderValue(Op, OM);
129 // Note: we cannot cache this lookup above, since inserting into the map
130 // changes the map's size, and thus affects the other IDs.
131 OM.index(V);
134 static OrderMap orderModule(const Module *M) {
135 // This needs to match the order used by ValueEnumerator::ValueEnumerator()
136 // and ValueEnumerator::incorporateFunction().
137 OrderMap OM;
139 for (const GlobalVariable &G : M->globals()) {
140 if (G.hasInitializer())
141 if (!isa<GlobalValue>(G.getInitializer()))
142 orderValue(G.getInitializer(), OM);
143 orderValue(&G, OM);
145 for (const GlobalAlias &A : M->aliases()) {
146 if (!isa<GlobalValue>(A.getAliasee()))
147 orderValue(A.getAliasee(), OM);
148 orderValue(&A, OM);
150 for (const GlobalIFunc &I : M->ifuncs()) {
151 if (!isa<GlobalValue>(I.getResolver()))
152 orderValue(I.getResolver(), OM);
153 orderValue(&I, OM);
155 for (const Function &F : *M) {
156 for (const Use &U : F.operands())
157 if (!isa<GlobalValue>(U.get()))
158 orderValue(U.get(), OM);
160 orderValue(&F, OM);
162 if (F.isDeclaration())
163 continue;
165 for (const Argument &A : F.args())
166 orderValue(&A, OM);
167 for (const BasicBlock &BB : F) {
168 orderValue(&BB, OM);
169 for (const Instruction &I : BB) {
170 for (const Value *Op : I.operands())
171 if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
172 isa<InlineAsm>(*Op))
173 orderValue(Op, OM);
174 orderValue(&I, OM);
178 return OM;
181 static void predictValueUseListOrderImpl(const Value *V, const Function *F,
182 unsigned ID, const OrderMap &OM,
183 UseListOrderStack &Stack) {
184 // Predict use-list order for this one.
185 using Entry = std::pair<const Use *, unsigned>;
186 SmallVector<Entry, 64> List;
187 for (const Use &U : V->uses())
188 // Check if this user will be serialized.
189 if (OM.lookup(U.getUser()).first)
190 List.push_back(std::make_pair(&U, List.size()));
192 if (List.size() < 2)
193 // We may have lost some users.
194 return;
196 bool GetsReversed =
197 !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
198 if (auto *BA = dyn_cast<BlockAddress>(V))
199 ID = OM.lookup(BA->getBasicBlock()).first;
200 llvm::sort(List, [&](const Entry &L, const Entry &R) {
201 const Use *LU = L.first;
202 const Use *RU = R.first;
203 if (LU == RU)
204 return false;
206 auto LID = OM.lookup(LU->getUser()).first;
207 auto RID = OM.lookup(RU->getUser()).first;
209 // If ID is 4, then expect: 7 6 5 1 2 3.
210 if (LID < RID) {
211 if (GetsReversed)
212 if (RID <= ID)
213 return true;
214 return false;
216 if (RID < LID) {
217 if (GetsReversed)
218 if (LID <= ID)
219 return false;
220 return true;
223 // LID and RID are equal, so we have different operands of the same user.
224 // Assume operands are added in order for all instructions.
225 if (GetsReversed)
226 if (LID <= ID)
227 return LU->getOperandNo() < RU->getOperandNo();
228 return LU->getOperandNo() > RU->getOperandNo();
231 if (std::is_sorted(
232 List.begin(), List.end(),
233 [](const Entry &L, const Entry &R) { return L.second < R.second; }))
234 // Order is already correct.
235 return;
237 // Store the shuffle.
238 Stack.emplace_back(V, F, List.size());
239 assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
240 for (size_t I = 0, E = List.size(); I != E; ++I)
241 Stack.back().Shuffle[I] = List[I].second;
244 static void predictValueUseListOrder(const Value *V, const Function *F,
245 OrderMap &OM, UseListOrderStack &Stack) {
246 auto &IDPair = OM[V];
247 assert(IDPair.first && "Unmapped value");
248 if (IDPair.second)
249 // Already predicted.
250 return;
252 // Do the actual prediction.
253 IDPair.second = true;
254 if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
255 predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
257 // Recursive descent into constants.
258 if (const Constant *C = dyn_cast<Constant>(V))
259 if (C->getNumOperands()) // Visit GlobalValues.
260 for (const Value *Op : C->operands())
261 if (isa<Constant>(Op)) // Visit GlobalValues.
262 predictValueUseListOrder(Op, F, OM, Stack);
265 static UseListOrderStack predictUseListOrder(const Module *M) {
266 OrderMap OM = orderModule(M);
268 // Use-list orders need to be serialized after all the users have been added
269 // to a value, or else the shuffles will be incomplete. Store them per
270 // function in a stack.
272 // Aside from function order, the order of values doesn't matter much here.
273 UseListOrderStack Stack;
275 // We want to visit the functions backward now so we can list function-local
276 // constants in the last Function they're used in. Module-level constants
277 // have already been visited above.
278 for (const Function &F : make_range(M->rbegin(), M->rend())) {
279 if (F.isDeclaration())
280 continue;
281 for (const BasicBlock &BB : F)
282 predictValueUseListOrder(&BB, &F, OM, Stack);
283 for (const Argument &A : F.args())
284 predictValueUseListOrder(&A, &F, OM, Stack);
285 for (const BasicBlock &BB : F)
286 for (const Instruction &I : BB)
287 for (const Value *Op : I.operands())
288 if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
289 predictValueUseListOrder(Op, &F, OM, Stack);
290 for (const BasicBlock &BB : F)
291 for (const Instruction &I : BB)
292 predictValueUseListOrder(&I, &F, OM, Stack);
295 // Visit globals last.
296 for (const GlobalVariable &G : M->globals())
297 predictValueUseListOrder(&G, nullptr, OM, Stack);
298 for (const Function &F : *M)
299 predictValueUseListOrder(&F, nullptr, OM, Stack);
300 for (const GlobalAlias &A : M->aliases())
301 predictValueUseListOrder(&A, nullptr, OM, Stack);
302 for (const GlobalIFunc &I : M->ifuncs())
303 predictValueUseListOrder(&I, nullptr, OM, Stack);
304 for (const GlobalVariable &G : M->globals())
305 if (G.hasInitializer())
306 predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
307 for (const GlobalAlias &A : M->aliases())
308 predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
309 for (const GlobalIFunc &I : M->ifuncs())
310 predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
311 for (const Function &F : *M)
312 for (const Use &U : F.operands())
313 predictValueUseListOrder(U.get(), nullptr, OM, Stack);
315 return Stack;
318 static const Module *getModuleFromVal(const Value *V) {
319 if (const Argument *MA = dyn_cast<Argument>(V))
320 return MA->getParent() ? MA->getParent()->getParent() : nullptr;
322 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
323 return BB->getParent() ? BB->getParent()->getParent() : nullptr;
325 if (const Instruction *I = dyn_cast<Instruction>(V)) {
326 const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
327 return M ? M->getParent() : nullptr;
330 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
331 return GV->getParent();
333 if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
334 for (const User *U : MAV->users())
335 if (isa<Instruction>(U))
336 if (const Module *M = getModuleFromVal(U))
337 return M;
338 return nullptr;
341 return nullptr;
344 static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
345 switch (cc) {
346 default: Out << "cc" << cc; break;
347 case CallingConv::Fast: Out << "fastcc"; break;
348 case CallingConv::Cold: Out << "coldcc"; break;
349 case CallingConv::WebKit_JS: Out << "webkit_jscc"; break;
350 case CallingConv::AnyReg: Out << "anyregcc"; break;
351 case CallingConv::PreserveMost: Out << "preserve_mostcc"; break;
352 case CallingConv::PreserveAll: Out << "preserve_allcc"; break;
353 case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break;
354 case CallingConv::GHC: Out << "ghccc"; break;
355 case CallingConv::Tail: Out << "tailcc"; break;
356 case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break;
357 case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break;
358 case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break;
359 case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break;
360 case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
361 case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break;
362 case CallingConv::ARM_APCS: Out << "arm_apcscc"; break;
363 case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break;
364 case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
365 case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
366 case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break;
367 case CallingConv::AVR_INTR: Out << "avr_intrcc "; break;
368 case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break;
369 case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break;
370 case CallingConv::PTX_Device: Out << "ptx_device"; break;
371 case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break;
372 case CallingConv::Win64: Out << "win64cc"; break;
373 case CallingConv::SPIR_FUNC: Out << "spir_func"; break;
374 case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break;
375 case CallingConv::Swift: Out << "swiftcc"; break;
376 case CallingConv::X86_INTR: Out << "x86_intrcc"; break;
377 case CallingConv::HHVM: Out << "hhvmcc"; break;
378 case CallingConv::HHVM_C: Out << "hhvm_ccc"; break;
379 case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break;
380 case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break;
381 case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break;
382 case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break;
383 case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break;
384 case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break;
385 case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break;
386 case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
390 enum PrefixType {
391 GlobalPrefix,
392 ComdatPrefix,
393 LabelPrefix,
394 LocalPrefix,
395 NoPrefix
398 void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
399 assert(!Name.empty() && "Cannot get empty name!");
401 // Scan the name to see if it needs quotes first.
402 bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
403 if (!NeedsQuotes) {
404 for (unsigned i = 0, e = Name.size(); i != e; ++i) {
405 // By making this unsigned, the value passed in to isalnum will always be
406 // in the range 0-255. This is important when building with MSVC because
407 // its implementation will assert. This situation can arise when dealing
408 // with UTF-8 multibyte characters.
409 unsigned char C = Name[i];
410 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
411 C != '_') {
412 NeedsQuotes = true;
413 break;
418 // If we didn't need any quotes, just write out the name in one blast.
419 if (!NeedsQuotes) {
420 OS << Name;
421 return;
424 // Okay, we need quotes. Output the quotes and escape any scary characters as
425 // needed.
426 OS << '"';
427 printEscapedString(Name, OS);
428 OS << '"';
431 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
432 /// (if the string only contains simple characters) or is surrounded with ""'s
433 /// (if it has special chars in it). Print it out.
434 static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
435 switch (Prefix) {
436 case NoPrefix:
437 break;
438 case GlobalPrefix:
439 OS << '@';
440 break;
441 case ComdatPrefix:
442 OS << '$';
443 break;
444 case LabelPrefix:
445 break;
446 case LocalPrefix:
447 OS << '%';
448 break;
450 printLLVMNameWithoutPrefix(OS, Name);
453 /// Turn the specified name into an 'LLVM name', which is either prefixed with %
454 /// (if the string only contains simple characters) or is surrounded with ""'s
455 /// (if it has special chars in it). Print it out.
456 static void PrintLLVMName(raw_ostream &OS, const Value *V) {
457 PrintLLVMName(OS, V->getName(),
458 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
461 namespace {
463 class TypePrinting {
464 public:
465 TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
467 TypePrinting(const TypePrinting &) = delete;
468 TypePrinting &operator=(const TypePrinting &) = delete;
470 /// The named types that are used by the current module.
471 TypeFinder &getNamedTypes();
473 /// The numbered types, number to type mapping.
474 std::vector<StructType *> &getNumberedTypes();
476 bool empty();
478 void print(Type *Ty, raw_ostream &OS);
480 void printStructBody(StructType *Ty, raw_ostream &OS);
482 private:
483 void incorporateTypes();
485 /// A module to process lazily when needed. Set to nullptr as soon as used.
486 const Module *DeferredM;
488 TypeFinder NamedTypes;
490 // The numbered types, along with their value.
491 DenseMap<StructType *, unsigned> Type2Number;
493 std::vector<StructType *> NumberedTypes;
496 } // end anonymous namespace
498 TypeFinder &TypePrinting::getNamedTypes() {
499 incorporateTypes();
500 return NamedTypes;
503 std::vector<StructType *> &TypePrinting::getNumberedTypes() {
504 incorporateTypes();
506 // We know all the numbers that each type is used and we know that it is a
507 // dense assignment. Convert the map to an index table, if it's not done
508 // already (judging from the sizes):
509 if (NumberedTypes.size() == Type2Number.size())
510 return NumberedTypes;
512 NumberedTypes.resize(Type2Number.size());
513 for (const auto &P : Type2Number) {
514 assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
515 assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
516 NumberedTypes[P.second] = P.first;
518 return NumberedTypes;
521 bool TypePrinting::empty() {
522 incorporateTypes();
523 return NamedTypes.empty() && Type2Number.empty();
526 void TypePrinting::incorporateTypes() {
527 if (!DeferredM)
528 return;
530 NamedTypes.run(*DeferredM, false);
531 DeferredM = nullptr;
533 // The list of struct types we got back includes all the struct types, split
534 // the unnamed ones out to a numbering and remove the anonymous structs.
535 unsigned NextNumber = 0;
537 std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
538 for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
539 StructType *STy = *I;
541 // Ignore anonymous types.
542 if (STy->isLiteral())
543 continue;
545 if (STy->getName().empty())
546 Type2Number[STy] = NextNumber++;
547 else
548 *NextToUse++ = STy;
551 NamedTypes.erase(NextToUse, NamedTypes.end());
554 /// Write the specified type to the specified raw_ostream, making use of type
555 /// names or up references to shorten the type name where possible.
556 void TypePrinting::print(Type *Ty, raw_ostream &OS) {
557 switch (Ty->getTypeID()) {
558 case Type::VoidTyID: OS << "void"; return;
559 case Type::HalfTyID: OS << "half"; return;
560 case Type::FloatTyID: OS << "float"; return;
561 case Type::DoubleTyID: OS << "double"; return;
562 case Type::X86_FP80TyID: OS << "x86_fp80"; return;
563 case Type::FP128TyID: OS << "fp128"; return;
564 case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
565 case Type::LabelTyID: OS << "label"; return;
566 case Type::MetadataTyID: OS << "metadata"; return;
567 case Type::X86_MMXTyID: OS << "x86_mmx"; return;
568 case Type::TokenTyID: OS << "token"; return;
569 case Type::IntegerTyID:
570 OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
571 return;
573 case Type::FunctionTyID: {
574 FunctionType *FTy = cast<FunctionType>(Ty);
575 print(FTy->getReturnType(), OS);
576 OS << " (";
577 for (FunctionType::param_iterator I = FTy->param_begin(),
578 E = FTy->param_end(); I != E; ++I) {
579 if (I != FTy->param_begin())
580 OS << ", ";
581 print(*I, OS);
583 if (FTy->isVarArg()) {
584 if (FTy->getNumParams()) OS << ", ";
585 OS << "...";
587 OS << ')';
588 return;
590 case Type::StructTyID: {
591 StructType *STy = cast<StructType>(Ty);
593 if (STy->isLiteral())
594 return printStructBody(STy, OS);
596 if (!STy->getName().empty())
597 return PrintLLVMName(OS, STy->getName(), LocalPrefix);
599 incorporateTypes();
600 const auto I = Type2Number.find(STy);
601 if (I != Type2Number.end())
602 OS << '%' << I->second;
603 else // Not enumerated, print the hex address.
604 OS << "%\"type " << STy << '\"';
605 return;
607 case Type::PointerTyID: {
608 PointerType *PTy = cast<PointerType>(Ty);
609 print(PTy->getElementType(), OS);
610 if (unsigned AddressSpace = PTy->getAddressSpace())
611 OS << " addrspace(" << AddressSpace << ')';
612 OS << '*';
613 return;
615 case Type::ArrayTyID: {
616 ArrayType *ATy = cast<ArrayType>(Ty);
617 OS << '[' << ATy->getNumElements() << " x ";
618 print(ATy->getElementType(), OS);
619 OS << ']';
620 return;
622 case Type::VectorTyID: {
623 VectorType *PTy = cast<VectorType>(Ty);
624 OS << "<";
625 if (PTy->isScalable())
626 OS << "vscale x ";
627 OS << PTy->getNumElements() << " x ";
628 print(PTy->getElementType(), OS);
629 OS << '>';
630 return;
633 llvm_unreachable("Invalid TypeID");
636 void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
637 if (STy->isOpaque()) {
638 OS << "opaque";
639 return;
642 if (STy->isPacked())
643 OS << '<';
645 if (STy->getNumElements() == 0) {
646 OS << "{}";
647 } else {
648 StructType::element_iterator I = STy->element_begin();
649 OS << "{ ";
650 print(*I++, OS);
651 for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
652 OS << ", ";
653 print(*I, OS);
656 OS << " }";
658 if (STy->isPacked())
659 OS << '>';
662 namespace llvm {
664 //===----------------------------------------------------------------------===//
665 // SlotTracker Class: Enumerate slot numbers for unnamed values
666 //===----------------------------------------------------------------------===//
667 /// This class provides computation of slot numbers for LLVM Assembly writing.
669 class SlotTracker {
670 public:
671 /// ValueMap - A mapping of Values to slot numbers.
672 using ValueMap = DenseMap<const Value *, unsigned>;
674 private:
675 /// TheModule - The module for which we are holding slot numbers.
676 const Module* TheModule;
678 /// TheFunction - The function for which we are holding slot numbers.
679 const Function* TheFunction = nullptr;
680 bool FunctionProcessed = false;
681 bool ShouldInitializeAllMetadata;
683 /// The summary index for which we are holding slot numbers.
684 const ModuleSummaryIndex *TheIndex = nullptr;
686 /// mMap - The slot map for the module level data.
687 ValueMap mMap;
688 unsigned mNext = 0;
690 /// fMap - The slot map for the function level data.
691 ValueMap fMap;
692 unsigned fNext = 0;
694 /// mdnMap - Map for MDNodes.
695 DenseMap<const MDNode*, unsigned> mdnMap;
696 unsigned mdnNext = 0;
698 /// asMap - The slot map for attribute sets.
699 DenseMap<AttributeSet, unsigned> asMap;
700 unsigned asNext = 0;
702 /// ModulePathMap - The slot map for Module paths used in the summary index.
703 StringMap<unsigned> ModulePathMap;
704 unsigned ModulePathNext = 0;
706 /// GUIDMap - The slot map for GUIDs used in the summary index.
707 DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
708 unsigned GUIDNext = 0;
710 /// TypeIdMap - The slot map for type ids used in the summary index.
711 StringMap<unsigned> TypeIdMap;
712 unsigned TypeIdNext = 0;
714 public:
715 /// Construct from a module.
717 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
718 /// functions, giving correct numbering for metadata referenced only from
719 /// within a function (even if no functions have been initialized).
720 explicit SlotTracker(const Module *M,
721 bool ShouldInitializeAllMetadata = false);
723 /// Construct from a function, starting out in incorp state.
725 /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
726 /// functions, giving correct numbering for metadata referenced only from
727 /// within a function (even if no functions have been initialized).
728 explicit SlotTracker(const Function *F,
729 bool ShouldInitializeAllMetadata = false);
731 /// Construct from a module summary index.
732 explicit SlotTracker(const ModuleSummaryIndex *Index);
734 SlotTracker(const SlotTracker &) = delete;
735 SlotTracker &operator=(const SlotTracker &) = delete;
737 /// Return the slot number of the specified value in it's type
738 /// plane. If something is not in the SlotTracker, return -1.
739 int getLocalSlot(const Value *V);
740 int getGlobalSlot(const GlobalValue *V);
741 int getMetadataSlot(const MDNode *N);
742 int getAttributeGroupSlot(AttributeSet AS);
743 int getModulePathSlot(StringRef Path);
744 int getGUIDSlot(GlobalValue::GUID GUID);
745 int getTypeIdSlot(StringRef Id);
747 /// If you'd like to deal with a function instead of just a module, use
748 /// this method to get its data into the SlotTracker.
749 void incorporateFunction(const Function *F) {
750 TheFunction = F;
751 FunctionProcessed = false;
754 const Function *getFunction() const { return TheFunction; }
756 /// After calling incorporateFunction, use this method to remove the
757 /// most recently incorporated function from the SlotTracker. This
758 /// will reset the state of the machine back to just the module contents.
759 void purgeFunction();
761 /// MDNode map iterators.
762 using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
764 mdn_iterator mdn_begin() { return mdnMap.begin(); }
765 mdn_iterator mdn_end() { return mdnMap.end(); }
766 unsigned mdn_size() const { return mdnMap.size(); }
767 bool mdn_empty() const { return mdnMap.empty(); }
769 /// AttributeSet map iterators.
770 using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
772 as_iterator as_begin() { return asMap.begin(); }
773 as_iterator as_end() { return asMap.end(); }
774 unsigned as_size() const { return asMap.size(); }
775 bool as_empty() const { return asMap.empty(); }
777 /// GUID map iterators.
778 using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
780 /// These functions do the actual initialization.
781 inline void initializeIfNeeded();
782 void initializeIndexIfNeeded();
784 // Implementation Details
785 private:
786 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
787 void CreateModuleSlot(const GlobalValue *V);
789 /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
790 void CreateMetadataSlot(const MDNode *N);
792 /// CreateFunctionSlot - Insert the specified Value* into the slot table.
793 void CreateFunctionSlot(const Value *V);
795 /// Insert the specified AttributeSet into the slot table.
796 void CreateAttributeSetSlot(AttributeSet AS);
798 inline void CreateModulePathSlot(StringRef Path);
799 void CreateGUIDSlot(GlobalValue::GUID GUID);
800 void CreateTypeIdSlot(StringRef Id);
802 /// Add all of the module level global variables (and their initializers)
803 /// and function declarations, but not the contents of those functions.
804 void processModule();
805 void processIndex();
807 /// Add all of the functions arguments, basic blocks, and instructions.
808 void processFunction();
810 /// Add the metadata directly attached to a GlobalObject.
811 void processGlobalObjectMetadata(const GlobalObject &GO);
813 /// Add all of the metadata from a function.
814 void processFunctionMetadata(const Function &F);
816 /// Add all of the metadata from an instruction.
817 void processInstructionMetadata(const Instruction &I);
820 } // end namespace llvm
822 ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
823 const Function *F)
824 : M(M), F(F), Machine(&Machine) {}
826 ModuleSlotTracker::ModuleSlotTracker(const Module *M,
827 bool ShouldInitializeAllMetadata)
828 : ShouldCreateStorage(M),
829 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
831 ModuleSlotTracker::~ModuleSlotTracker() = default;
833 SlotTracker *ModuleSlotTracker::getMachine() {
834 if (!ShouldCreateStorage)
835 return Machine;
837 ShouldCreateStorage = false;
838 MachineStorage =
839 std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
840 Machine = MachineStorage.get();
841 return Machine;
844 void ModuleSlotTracker::incorporateFunction(const Function &F) {
845 // Using getMachine() may lazily create the slot tracker.
846 if (!getMachine())
847 return;
849 // Nothing to do if this is the right function already.
850 if (this->F == &F)
851 return;
852 if (this->F)
853 Machine->purgeFunction();
854 Machine->incorporateFunction(&F);
855 this->F = &F;
858 int ModuleSlotTracker::getLocalSlot(const Value *V) {
859 assert(F && "No function incorporated");
860 return Machine->getLocalSlot(V);
863 static SlotTracker *createSlotTracker(const Value *V) {
864 if (const Argument *FA = dyn_cast<Argument>(V))
865 return new SlotTracker(FA->getParent());
867 if (const Instruction *I = dyn_cast<Instruction>(V))
868 if (I->getParent())
869 return new SlotTracker(I->getParent()->getParent());
871 if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
872 return new SlotTracker(BB->getParent());
874 if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
875 return new SlotTracker(GV->getParent());
877 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
878 return new SlotTracker(GA->getParent());
880 if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
881 return new SlotTracker(GIF->getParent());
883 if (const Function *Func = dyn_cast<Function>(V))
884 return new SlotTracker(Func);
886 return nullptr;
889 #if 0
890 #define ST_DEBUG(X) dbgs() << X
891 #else
892 #define ST_DEBUG(X)
893 #endif
895 // Module level constructor. Causes the contents of the Module (sans functions)
896 // to be added to the slot table.
897 SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
898 : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
900 // Function level constructor. Causes the contents of the Module and the one
901 // function provided to be added to the slot table.
902 SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
903 : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
904 ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
906 SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
907 : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
909 inline void SlotTracker::initializeIfNeeded() {
910 if (TheModule) {
911 processModule();
912 TheModule = nullptr; ///< Prevent re-processing next time we're called.
915 if (TheFunction && !FunctionProcessed)
916 processFunction();
919 void SlotTracker::initializeIndexIfNeeded() {
920 if (!TheIndex)
921 return;
922 processIndex();
923 TheIndex = nullptr; ///< Prevent re-processing next time we're called.
926 // Iterate through all the global variables, functions, and global
927 // variable initializers and create slots for them.
928 void SlotTracker::processModule() {
929 ST_DEBUG("begin processModule!\n");
931 // Add all of the unnamed global variables to the value table.
932 for (const GlobalVariable &Var : TheModule->globals()) {
933 if (!Var.hasName())
934 CreateModuleSlot(&Var);
935 processGlobalObjectMetadata(Var);
936 auto Attrs = Var.getAttributes();
937 if (Attrs.hasAttributes())
938 CreateAttributeSetSlot(Attrs);
941 for (const GlobalAlias &A : TheModule->aliases()) {
942 if (!A.hasName())
943 CreateModuleSlot(&A);
946 for (const GlobalIFunc &I : TheModule->ifuncs()) {
947 if (!I.hasName())
948 CreateModuleSlot(&I);
951 // Add metadata used by named metadata.
952 for (const NamedMDNode &NMD : TheModule->named_metadata()) {
953 for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
954 CreateMetadataSlot(NMD.getOperand(i));
957 for (const Function &F : *TheModule) {
958 if (!F.hasName())
959 // Add all the unnamed functions to the table.
960 CreateModuleSlot(&F);
962 if (ShouldInitializeAllMetadata)
963 processFunctionMetadata(F);
965 // Add all the function attributes to the table.
966 // FIXME: Add attributes of other objects?
967 AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
968 if (FnAttrs.hasAttributes())
969 CreateAttributeSetSlot(FnAttrs);
972 ST_DEBUG("end processModule!\n");
975 // Process the arguments, basic blocks, and instructions of a function.
976 void SlotTracker::processFunction() {
977 ST_DEBUG("begin processFunction!\n");
978 fNext = 0;
980 // Process function metadata if it wasn't hit at the module-level.
981 if (!ShouldInitializeAllMetadata)
982 processFunctionMetadata(*TheFunction);
984 // Add all the function arguments with no names.
985 for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
986 AE = TheFunction->arg_end(); AI != AE; ++AI)
987 if (!AI->hasName())
988 CreateFunctionSlot(&*AI);
990 ST_DEBUG("Inserting Instructions:\n");
992 // Add all of the basic blocks and instructions with no names.
993 for (auto &BB : *TheFunction) {
994 if (!BB.hasName())
995 CreateFunctionSlot(&BB);
997 for (auto &I : BB) {
998 if (!I.getType()->isVoidTy() && !I.hasName())
999 CreateFunctionSlot(&I);
1001 // We allow direct calls to any llvm.foo function here, because the
1002 // target may not be linked into the optimizer.
1003 if (const auto *Call = dyn_cast<CallBase>(&I)) {
1004 // Add all the call attributes to the table.
1005 AttributeSet Attrs = Call->getAttributes().getFnAttributes();
1006 if (Attrs.hasAttributes())
1007 CreateAttributeSetSlot(Attrs);
1012 FunctionProcessed = true;
1014 ST_DEBUG("end processFunction!\n");
1017 // Iterate through all the GUID in the index and create slots for them.
1018 void SlotTracker::processIndex() {
1019 ST_DEBUG("begin processIndex!\n");
1020 assert(TheIndex);
1022 // The first block of slots are just the module ids, which start at 0 and are
1023 // assigned consecutively. Since the StringMap iteration order isn't
1024 // guaranteed, use a std::map to order by module ID before assigning slots.
1025 std::map<uint64_t, StringRef> ModuleIdToPathMap;
1026 for (auto &ModPath : TheIndex->modulePaths())
1027 ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
1028 for (auto &ModPair : ModuleIdToPathMap)
1029 CreateModulePathSlot(ModPair.second);
1031 // Start numbering the GUIDs after the module ids.
1032 GUIDNext = ModulePathNext;
1034 for (auto &GlobalList : *TheIndex)
1035 CreateGUIDSlot(GlobalList.first);
1037 // Start numbering the TypeIds after the GUIDs.
1038 TypeIdNext = GUIDNext;
1040 for (auto TidIter = TheIndex->typeIds().begin();
1041 TidIter != TheIndex->typeIds().end(); TidIter++)
1042 CreateTypeIdSlot(TidIter->second.first);
1044 for (auto &TId : TheIndex->typeIdCompatibleVtableMap())
1045 CreateGUIDSlot(GlobalValue::getGUID(TId.first));
1047 ST_DEBUG("end processIndex!\n");
1050 void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
1051 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1052 GO.getAllMetadata(MDs);
1053 for (auto &MD : MDs)
1054 CreateMetadataSlot(MD.second);
1057 void SlotTracker::processFunctionMetadata(const Function &F) {
1058 processGlobalObjectMetadata(F);
1059 for (auto &BB : F) {
1060 for (auto &I : BB)
1061 processInstructionMetadata(I);
1065 void SlotTracker::processInstructionMetadata(const Instruction &I) {
1066 // Process metadata used directly by intrinsics.
1067 if (const CallInst *CI = dyn_cast<CallInst>(&I))
1068 if (Function *F = CI->getCalledFunction())
1069 if (F->isIntrinsic())
1070 for (auto &Op : I.operands())
1071 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
1072 if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
1073 CreateMetadataSlot(N);
1075 // Process metadata attached to this instruction.
1076 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
1077 I.getAllMetadata(MDs);
1078 for (auto &MD : MDs)
1079 CreateMetadataSlot(MD.second);
1082 /// Clean up after incorporating a function. This is the only way to get out of
1083 /// the function incorporation state that affects get*Slot/Create*Slot. Function
1084 /// incorporation state is indicated by TheFunction != 0.
1085 void SlotTracker::purgeFunction() {
1086 ST_DEBUG("begin purgeFunction!\n");
1087 fMap.clear(); // Simply discard the function level map
1088 TheFunction = nullptr;
1089 FunctionProcessed = false;
1090 ST_DEBUG("end purgeFunction!\n");
1093 /// getGlobalSlot - Get the slot number of a global value.
1094 int SlotTracker::getGlobalSlot(const GlobalValue *V) {
1095 // Check for uninitialized state and do lazy initialization.
1096 initializeIfNeeded();
1098 // Find the value in the module map
1099 ValueMap::iterator MI = mMap.find(V);
1100 return MI == mMap.end() ? -1 : (int)MI->second;
1103 /// getMetadataSlot - Get the slot number of a MDNode.
1104 int SlotTracker::getMetadataSlot(const MDNode *N) {
1105 // Check for uninitialized state and do lazy initialization.
1106 initializeIfNeeded();
1108 // Find the MDNode in the module map
1109 mdn_iterator MI = mdnMap.find(N);
1110 return MI == mdnMap.end() ? -1 : (int)MI->second;
1113 /// getLocalSlot - Get the slot number for a value that is local to a function.
1114 int SlotTracker::getLocalSlot(const Value *V) {
1115 assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
1117 // Check for uninitialized state and do lazy initialization.
1118 initializeIfNeeded();
1120 ValueMap::iterator FI = fMap.find(V);
1121 return FI == fMap.end() ? -1 : (int)FI->second;
1124 int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
1125 // Check for uninitialized state and do lazy initialization.
1126 initializeIfNeeded();
1128 // Find the AttributeSet in the module map.
1129 as_iterator AI = asMap.find(AS);
1130 return AI == asMap.end() ? -1 : (int)AI->second;
1133 int SlotTracker::getModulePathSlot(StringRef Path) {
1134 // Check for uninitialized state and do lazy initialization.
1135 initializeIndexIfNeeded();
1137 // Find the Module path in the map
1138 auto I = ModulePathMap.find(Path);
1139 return I == ModulePathMap.end() ? -1 : (int)I->second;
1142 int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
1143 // Check for uninitialized state and do lazy initialization.
1144 initializeIndexIfNeeded();
1146 // Find the GUID in the map
1147 guid_iterator I = GUIDMap.find(GUID);
1148 return I == GUIDMap.end() ? -1 : (int)I->second;
1151 int SlotTracker::getTypeIdSlot(StringRef Id) {
1152 // Check for uninitialized state and do lazy initialization.
1153 initializeIndexIfNeeded();
1155 // Find the TypeId string in the map
1156 auto I = TypeIdMap.find(Id);
1157 return I == TypeIdMap.end() ? -1 : (int)I->second;
1160 /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
1161 void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
1162 assert(V && "Can't insert a null Value into SlotTracker!");
1163 assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
1164 assert(!V->hasName() && "Doesn't need a slot!");
1166 unsigned DestSlot = mNext++;
1167 mMap[V] = DestSlot;
1169 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1170 DestSlot << " [");
1171 // G = Global, F = Function, A = Alias, I = IFunc, o = other
1172 ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
1173 (isa<Function>(V) ? 'F' :
1174 (isa<GlobalAlias>(V) ? 'A' :
1175 (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
1178 /// CreateSlot - Create a new slot for the specified value if it has no name.
1179 void SlotTracker::CreateFunctionSlot(const Value *V) {
1180 assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
1182 unsigned DestSlot = fNext++;
1183 fMap[V] = DestSlot;
1185 // G = Global, F = Function, o = other
1186 ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" <<
1187 DestSlot << " [o]\n");
1190 /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
1191 void SlotTracker::CreateMetadataSlot(const MDNode *N) {
1192 assert(N && "Can't insert a null Value into SlotTracker!");
1194 // Don't make slots for DIExpressions. We just print them inline everywhere.
1195 if (isa<DIExpression>(N))
1196 return;
1198 unsigned DestSlot = mdnNext;
1199 if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
1200 return;
1201 ++mdnNext;
1203 // Recursively add any MDNodes referenced by operands.
1204 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1205 if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
1206 CreateMetadataSlot(Op);
1209 void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
1210 assert(AS.hasAttributes() && "Doesn't need a slot!");
1212 as_iterator I = asMap.find(AS);
1213 if (I != asMap.end())
1214 return;
1216 unsigned DestSlot = asNext++;
1217 asMap[AS] = DestSlot;
1220 /// Create a new slot for the specified Module
1221 void SlotTracker::CreateModulePathSlot(StringRef Path) {
1222 ModulePathMap[Path] = ModulePathNext++;
1225 /// Create a new slot for the specified GUID
1226 void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
1227 GUIDMap[GUID] = GUIDNext++;
1230 /// Create a new slot for the specified Id
1231 void SlotTracker::CreateTypeIdSlot(StringRef Id) {
1232 TypeIdMap[Id] = TypeIdNext++;
1235 //===----------------------------------------------------------------------===//
1236 // AsmWriter Implementation
1237 //===----------------------------------------------------------------------===//
1239 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
1240 TypePrinting *TypePrinter,
1241 SlotTracker *Machine,
1242 const Module *Context);
1244 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1245 TypePrinting *TypePrinter,
1246 SlotTracker *Machine, const Module *Context,
1247 bool FromValue = false);
1249 static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
1250 if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
1251 // 'Fast' is an abbreviation for all fast-math-flags.
1252 if (FPO->isFast())
1253 Out << " fast";
1254 else {
1255 if (FPO->hasAllowReassoc())
1256 Out << " reassoc";
1257 if (FPO->hasNoNaNs())
1258 Out << " nnan";
1259 if (FPO->hasNoInfs())
1260 Out << " ninf";
1261 if (FPO->hasNoSignedZeros())
1262 Out << " nsz";
1263 if (FPO->hasAllowReciprocal())
1264 Out << " arcp";
1265 if (FPO->hasAllowContract())
1266 Out << " contract";
1267 if (FPO->hasApproxFunc())
1268 Out << " afn";
1272 if (const OverflowingBinaryOperator *OBO =
1273 dyn_cast<OverflowingBinaryOperator>(U)) {
1274 if (OBO->hasNoUnsignedWrap())
1275 Out << " nuw";
1276 if (OBO->hasNoSignedWrap())
1277 Out << " nsw";
1278 } else if (const PossiblyExactOperator *Div =
1279 dyn_cast<PossiblyExactOperator>(U)) {
1280 if (Div->isExact())
1281 Out << " exact";
1282 } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
1283 if (GEP->isInBounds())
1284 Out << " inbounds";
1288 static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1289 TypePrinting &TypePrinter,
1290 SlotTracker *Machine,
1291 const Module *Context) {
1292 if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
1293 if (CI->getType()->isIntegerTy(1)) {
1294 Out << (CI->getZExtValue() ? "true" : "false");
1295 return;
1297 Out << CI->getValue();
1298 return;
1301 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
1302 const APFloat &APF = CFP->getValueAPF();
1303 if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
1304 &APF.getSemantics() == &APFloat::IEEEdouble()) {
1305 // We would like to output the FP constant value in exponential notation,
1306 // but we cannot do this if doing so will lose precision. Check here to
1307 // make sure that we only output it in exponential format if we can parse
1308 // the value back and get the same value.
1310 bool ignored;
1311 bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
1312 bool isInf = APF.isInfinity();
1313 bool isNaN = APF.isNaN();
1314 if (!isInf && !isNaN) {
1315 double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
1316 SmallString<128> StrVal;
1317 APF.toString(StrVal, 6, 0, false);
1318 // Check to make sure that the stringized number is not some string like
1319 // "Inf" or NaN, that atof will accept, but the lexer will not. Check
1320 // that the string matches the "[-+]?[0-9]" regex.
1322 assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
1323 ((StrVal[0] == '-' || StrVal[0] == '+') &&
1324 (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
1325 "[-+]?[0-9] regex does not match!");
1326 // Reparse stringized version!
1327 if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
1328 Out << StrVal;
1329 return;
1332 // Otherwise we could not reparse it to exactly the same value, so we must
1333 // output the string in hexadecimal format! Note that loading and storing
1334 // floating point types changes the bits of NaNs on some hosts, notably
1335 // x86, so we must not use these types.
1336 static_assert(sizeof(double) == sizeof(uint64_t),
1337 "assuming that double is 64 bits!");
1338 APFloat apf = APF;
1339 // Floats are represented in ASCII IR as double, convert.
1340 if (!isDouble)
1341 apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
1342 &ignored);
1343 Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
1344 return;
1347 // Either half, or some form of long double.
1348 // These appear as a magic letter identifying the type, then a
1349 // fixed number of hex digits.
1350 Out << "0x";
1351 APInt API = APF.bitcastToAPInt();
1352 if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
1353 Out << 'K';
1354 Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
1355 /*Upper=*/true);
1356 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1357 /*Upper=*/true);
1358 return;
1359 } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
1360 Out << 'L';
1361 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1362 /*Upper=*/true);
1363 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1364 /*Upper=*/true);
1365 } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
1366 Out << 'M';
1367 Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
1368 /*Upper=*/true);
1369 Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
1370 /*Upper=*/true);
1371 } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
1372 Out << 'H';
1373 Out << format_hex_no_prefix(API.getZExtValue(), 4,
1374 /*Upper=*/true);
1375 } else
1376 llvm_unreachable("Unsupported floating point type");
1377 return;
1380 if (isa<ConstantAggregateZero>(CV)) {
1381 Out << "zeroinitializer";
1382 return;
1385 if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
1386 Out << "blockaddress(";
1387 WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
1388 Context);
1389 Out << ", ";
1390 WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
1391 Context);
1392 Out << ")";
1393 return;
1396 if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
1397 Type *ETy = CA->getType()->getElementType();
1398 Out << '[';
1399 TypePrinter.print(ETy, Out);
1400 Out << ' ';
1401 WriteAsOperandInternal(Out, CA->getOperand(0),
1402 &TypePrinter, Machine,
1403 Context);
1404 for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
1405 Out << ", ";
1406 TypePrinter.print(ETy, Out);
1407 Out << ' ';
1408 WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
1409 Context);
1411 Out << ']';
1412 return;
1415 if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
1416 // As a special case, print the array as a string if it is an array of
1417 // i8 with ConstantInt values.
1418 if (CA->isString()) {
1419 Out << "c\"";
1420 printEscapedString(CA->getAsString(), Out);
1421 Out << '"';
1422 return;
1425 Type *ETy = CA->getType()->getElementType();
1426 Out << '[';
1427 TypePrinter.print(ETy, Out);
1428 Out << ' ';
1429 WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
1430 &TypePrinter, Machine,
1431 Context);
1432 for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
1433 Out << ", ";
1434 TypePrinter.print(ETy, Out);
1435 Out << ' ';
1436 WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
1437 Machine, Context);
1439 Out << ']';
1440 return;
1443 if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
1444 if (CS->getType()->isPacked())
1445 Out << '<';
1446 Out << '{';
1447 unsigned N = CS->getNumOperands();
1448 if (N) {
1449 Out << ' ';
1450 TypePrinter.print(CS->getOperand(0)->getType(), Out);
1451 Out << ' ';
1453 WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
1454 Context);
1456 for (unsigned i = 1; i < N; i++) {
1457 Out << ", ";
1458 TypePrinter.print(CS->getOperand(i)->getType(), Out);
1459 Out << ' ';
1461 WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
1462 Context);
1464 Out << ' ';
1467 Out << '}';
1468 if (CS->getType()->isPacked())
1469 Out << '>';
1470 return;
1473 if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1474 Type *ETy = CV->getType()->getVectorElementType();
1475 Out << '<';
1476 TypePrinter.print(ETy, Out);
1477 Out << ' ';
1478 WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
1479 Machine, Context);
1480 for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
1481 Out << ", ";
1482 TypePrinter.print(ETy, Out);
1483 Out << ' ';
1484 WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
1485 Machine, Context);
1487 Out << '>';
1488 return;
1491 if (isa<ConstantPointerNull>(CV)) {
1492 Out << "null";
1493 return;
1496 if (isa<ConstantTokenNone>(CV)) {
1497 Out << "none";
1498 return;
1501 if (isa<UndefValue>(CV)) {
1502 Out << "undef";
1503 return;
1506 if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
1507 Out << CE->getOpcodeName();
1508 WriteOptimizationInfo(Out, CE);
1509 if (CE->isCompare())
1510 Out << ' ' << CmpInst::getPredicateName(
1511 static_cast<CmpInst::Predicate>(CE->getPredicate()));
1512 Out << " (";
1514 Optional<unsigned> InRangeOp;
1515 if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
1516 TypePrinter.print(GEP->getSourceElementType(), Out);
1517 Out << ", ";
1518 InRangeOp = GEP->getInRangeIndex();
1519 if (InRangeOp)
1520 ++*InRangeOp;
1523 for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
1524 if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
1525 Out << "inrange ";
1526 TypePrinter.print((*OI)->getType(), Out);
1527 Out << ' ';
1528 WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
1529 if (OI+1 != CE->op_end())
1530 Out << ", ";
1533 if (CE->hasIndices()) {
1534 ArrayRef<unsigned> Indices = CE->getIndices();
1535 for (unsigned i = 0, e = Indices.size(); i != e; ++i)
1536 Out << ", " << Indices[i];
1539 if (CE->isCast()) {
1540 Out << " to ";
1541 TypePrinter.print(CE->getType(), Out);
1544 Out << ')';
1545 return;
1548 Out << "<placeholder or erroneous Constant>";
1551 static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1552 TypePrinting *TypePrinter, SlotTracker *Machine,
1553 const Module *Context) {
1554 Out << "!{";
1555 for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
1556 const Metadata *MD = Node->getOperand(mi);
1557 if (!MD)
1558 Out << "null";
1559 else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
1560 Value *V = MDV->getValue();
1561 TypePrinter->print(V->getType(), Out);
1562 Out << ' ';
1563 WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
1564 } else {
1565 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1567 if (mi + 1 != me)
1568 Out << ", ";
1571 Out << "}";
1574 namespace {
1576 struct FieldSeparator {
1577 bool Skip = true;
1578 const char *Sep;
1580 FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
1583 raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
1584 if (FS.Skip) {
1585 FS.Skip = false;
1586 return OS;
1588 return OS << FS.Sep;
1591 struct MDFieldPrinter {
1592 raw_ostream &Out;
1593 FieldSeparator FS;
1594 TypePrinting *TypePrinter = nullptr;
1595 SlotTracker *Machine = nullptr;
1596 const Module *Context = nullptr;
1598 explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
1599 MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
1600 SlotTracker *Machine, const Module *Context)
1601 : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
1604 void printTag(const DINode *N);
1605 void printMacinfoType(const DIMacroNode *N);
1606 void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
1607 void printString(StringRef Name, StringRef Value,
1608 bool ShouldSkipEmpty = true);
1609 void printMetadata(StringRef Name, const Metadata *MD,
1610 bool ShouldSkipNull = true);
1611 template <class IntTy>
1612 void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
1613 void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
1614 void printDIFlags(StringRef Name, DINode::DIFlags Flags);
1615 void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
1616 template <class IntTy, class Stringifier>
1617 void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
1618 bool ShouldSkipZero = true);
1619 void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
1620 void printNameTableKind(StringRef Name,
1621 DICompileUnit::DebugNameTableKind NTK);
1624 } // end anonymous namespace
1626 void MDFieldPrinter::printTag(const DINode *N) {
1627 Out << FS << "tag: ";
1628 auto Tag = dwarf::TagString(N->getTag());
1629 if (!Tag.empty())
1630 Out << Tag;
1631 else
1632 Out << N->getTag();
1635 void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
1636 Out << FS << "type: ";
1637 auto Type = dwarf::MacinfoString(N->getMacinfoType());
1638 if (!Type.empty())
1639 Out << Type;
1640 else
1641 Out << N->getMacinfoType();
1644 void MDFieldPrinter::printChecksum(
1645 const DIFile::ChecksumInfo<StringRef> &Checksum) {
1646 Out << FS << "checksumkind: " << Checksum.getKindAsString();
1647 printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
1650 void MDFieldPrinter::printString(StringRef Name, StringRef Value,
1651 bool ShouldSkipEmpty) {
1652 if (ShouldSkipEmpty && Value.empty())
1653 return;
1655 Out << FS << Name << ": \"";
1656 printEscapedString(Value, Out);
1657 Out << "\"";
1660 static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1661 TypePrinting *TypePrinter,
1662 SlotTracker *Machine,
1663 const Module *Context) {
1664 if (!MD) {
1665 Out << "null";
1666 return;
1668 WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
1671 void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
1672 bool ShouldSkipNull) {
1673 if (ShouldSkipNull && !MD)
1674 return;
1676 Out << FS << Name << ": ";
1677 writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
1680 template <class IntTy>
1681 void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
1682 if (ShouldSkipZero && !Int)
1683 return;
1685 Out << FS << Name << ": " << Int;
1688 void MDFieldPrinter::printBool(StringRef Name, bool Value,
1689 Optional<bool> Default) {
1690 if (Default && Value == *Default)
1691 return;
1692 Out << FS << Name << ": " << (Value ? "true" : "false");
1695 void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
1696 if (!Flags)
1697 return;
1699 Out << FS << Name << ": ";
1701 SmallVector<DINode::DIFlags, 8> SplitFlags;
1702 auto Extra = DINode::splitFlags(Flags, SplitFlags);
1704 FieldSeparator FlagsFS(" | ");
1705 for (auto F : SplitFlags) {
1706 auto StringF = DINode::getFlagString(F);
1707 assert(!StringF.empty() && "Expected valid flag");
1708 Out << FlagsFS << StringF;
1710 if (Extra || SplitFlags.empty())
1711 Out << FlagsFS << Extra;
1714 void MDFieldPrinter::printDISPFlags(StringRef Name,
1715 DISubprogram::DISPFlags Flags) {
1716 // Always print this field, because no flags in the IR at all will be
1717 // interpreted as old-style isDefinition: true.
1718 Out << FS << Name << ": ";
1720 if (!Flags) {
1721 Out << 0;
1722 return;
1725 SmallVector<DISubprogram::DISPFlags, 8> SplitFlags;
1726 auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
1728 FieldSeparator FlagsFS(" | ");
1729 for (auto F : SplitFlags) {
1730 auto StringF = DISubprogram::getFlagString(F);
1731 assert(!StringF.empty() && "Expected valid flag");
1732 Out << FlagsFS << StringF;
1734 if (Extra || SplitFlags.empty())
1735 Out << FlagsFS << Extra;
1738 void MDFieldPrinter::printEmissionKind(StringRef Name,
1739 DICompileUnit::DebugEmissionKind EK) {
1740 Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
1743 void MDFieldPrinter::printNameTableKind(StringRef Name,
1744 DICompileUnit::DebugNameTableKind NTK) {
1745 if (NTK == DICompileUnit::DebugNameTableKind::Default)
1746 return;
1747 Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
1750 template <class IntTy, class Stringifier>
1751 void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
1752 Stringifier toString, bool ShouldSkipZero) {
1753 if (!Value)
1754 return;
1756 Out << FS << Name << ": ";
1757 auto S = toString(Value);
1758 if (!S.empty())
1759 Out << S;
1760 else
1761 Out << Value;
1764 static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1765 TypePrinting *TypePrinter, SlotTracker *Machine,
1766 const Module *Context) {
1767 Out << "!GenericDINode(";
1768 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1769 Printer.printTag(N);
1770 Printer.printString("header", N->getHeader());
1771 if (N->getNumDwarfOperands()) {
1772 Out << Printer.FS << "operands: {";
1773 FieldSeparator IFS;
1774 for (auto &I : N->dwarf_operands()) {
1775 Out << IFS;
1776 writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
1778 Out << "}";
1780 Out << ")";
1783 static void writeDILocation(raw_ostream &Out, const DILocation *DL,
1784 TypePrinting *TypePrinter, SlotTracker *Machine,
1785 const Module *Context) {
1786 Out << "!DILocation(";
1787 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1788 // Always output the line, since 0 is a relevant and important value for it.
1789 Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
1790 Printer.printInt("column", DL->getColumn());
1791 Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
1792 Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
1793 Printer.printBool("isImplicitCode", DL->isImplicitCode(),
1794 /* Default */ false);
1795 Out << ")";
1798 static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
1799 TypePrinting *TypePrinter, SlotTracker *Machine,
1800 const Module *Context) {
1801 Out << "!DISubrange(";
1802 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1803 if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
1804 Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
1805 else
1806 Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
1807 /*ShouldSkipNull */ false);
1808 Printer.printInt("lowerBound", N->getLowerBound());
1809 Out << ")";
1812 static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
1813 TypePrinting *, SlotTracker *, const Module *) {
1814 Out << "!DIEnumerator(";
1815 MDFieldPrinter Printer(Out);
1816 Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
1817 if (N->isUnsigned()) {
1818 auto Value = static_cast<uint64_t>(N->getValue());
1819 Printer.printInt("value", Value, /* ShouldSkipZero */ false);
1820 Printer.printBool("isUnsigned", true);
1821 } else {
1822 Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
1824 Out << ")";
1827 static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
1828 TypePrinting *, SlotTracker *, const Module *) {
1829 Out << "!DIBasicType(";
1830 MDFieldPrinter Printer(Out);
1831 if (N->getTag() != dwarf::DW_TAG_base_type)
1832 Printer.printTag(N);
1833 Printer.printString("name", N->getName());
1834 Printer.printInt("size", N->getSizeInBits());
1835 Printer.printInt("align", N->getAlignInBits());
1836 Printer.printDwarfEnum("encoding", N->getEncoding(),
1837 dwarf::AttributeEncodingString);
1838 Printer.printDIFlags("flags", N->getFlags());
1839 Out << ")";
1842 static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
1843 TypePrinting *TypePrinter, SlotTracker *Machine,
1844 const Module *Context) {
1845 Out << "!DIDerivedType(";
1846 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1847 Printer.printTag(N);
1848 Printer.printString("name", N->getName());
1849 Printer.printMetadata("scope", N->getRawScope());
1850 Printer.printMetadata("file", N->getRawFile());
1851 Printer.printInt("line", N->getLine());
1852 Printer.printMetadata("baseType", N->getRawBaseType(),
1853 /* ShouldSkipNull */ false);
1854 Printer.printInt("size", N->getSizeInBits());
1855 Printer.printInt("align", N->getAlignInBits());
1856 Printer.printInt("offset", N->getOffsetInBits());
1857 Printer.printDIFlags("flags", N->getFlags());
1858 Printer.printMetadata("extraData", N->getRawExtraData());
1859 if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
1860 Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
1861 /* ShouldSkipZero */ false);
1862 Out << ")";
1865 static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
1866 TypePrinting *TypePrinter,
1867 SlotTracker *Machine, const Module *Context) {
1868 Out << "!DICompositeType(";
1869 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1870 Printer.printTag(N);
1871 Printer.printString("name", N->getName());
1872 Printer.printMetadata("scope", N->getRawScope());
1873 Printer.printMetadata("file", N->getRawFile());
1874 Printer.printInt("line", N->getLine());
1875 Printer.printMetadata("baseType", N->getRawBaseType());
1876 Printer.printInt("size", N->getSizeInBits());
1877 Printer.printInt("align", N->getAlignInBits());
1878 Printer.printInt("offset", N->getOffsetInBits());
1879 Printer.printDIFlags("flags", N->getFlags());
1880 Printer.printMetadata("elements", N->getRawElements());
1881 Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
1882 dwarf::LanguageString);
1883 Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
1884 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1885 Printer.printString("identifier", N->getIdentifier());
1886 Printer.printMetadata("discriminator", N->getRawDiscriminator());
1887 Out << ")";
1890 static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
1891 TypePrinting *TypePrinter,
1892 SlotTracker *Machine, const Module *Context) {
1893 Out << "!DISubroutineType(";
1894 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1895 Printer.printDIFlags("flags", N->getFlags());
1896 Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
1897 Printer.printMetadata("types", N->getRawTypeArray(),
1898 /* ShouldSkipNull */ false);
1899 Out << ")";
1902 static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
1903 SlotTracker *, const Module *) {
1904 Out << "!DIFile(";
1905 MDFieldPrinter Printer(Out);
1906 Printer.printString("filename", N->getFilename(),
1907 /* ShouldSkipEmpty */ false);
1908 Printer.printString("directory", N->getDirectory(),
1909 /* ShouldSkipEmpty */ false);
1910 // Print all values for checksum together, or not at all.
1911 if (N->getChecksum())
1912 Printer.printChecksum(*N->getChecksum());
1913 Printer.printString("source", N->getSource().getValueOr(StringRef()),
1914 /* ShouldSkipEmpty */ true);
1915 Out << ")";
1918 static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
1919 TypePrinting *TypePrinter, SlotTracker *Machine,
1920 const Module *Context) {
1921 Out << "!DICompileUnit(";
1922 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1923 Printer.printDwarfEnum("language", N->getSourceLanguage(),
1924 dwarf::LanguageString, /* ShouldSkipZero */ false);
1925 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
1926 Printer.printString("producer", N->getProducer());
1927 Printer.printBool("isOptimized", N->isOptimized());
1928 Printer.printString("flags", N->getFlags());
1929 Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
1930 /* ShouldSkipZero */ false);
1931 Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
1932 Printer.printEmissionKind("emissionKind", N->getEmissionKind());
1933 Printer.printMetadata("enums", N->getRawEnumTypes());
1934 Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
1935 Printer.printMetadata("globals", N->getRawGlobalVariables());
1936 Printer.printMetadata("imports", N->getRawImportedEntities());
1937 Printer.printMetadata("macros", N->getRawMacros());
1938 Printer.printInt("dwoId", N->getDWOId());
1939 Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
1940 Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
1941 false);
1942 Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
1943 Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
1944 Out << ")";
1947 static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
1948 TypePrinting *TypePrinter, SlotTracker *Machine,
1949 const Module *Context) {
1950 Out << "!DISubprogram(";
1951 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1952 Printer.printString("name", N->getName());
1953 Printer.printString("linkageName", N->getLinkageName());
1954 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1955 Printer.printMetadata("file", N->getRawFile());
1956 Printer.printInt("line", N->getLine());
1957 Printer.printMetadata("type", N->getRawType());
1958 Printer.printInt("scopeLine", N->getScopeLine());
1959 Printer.printMetadata("containingType", N->getRawContainingType());
1960 if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
1961 N->getVirtualIndex() != 0)
1962 Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
1963 Printer.printInt("thisAdjustment", N->getThisAdjustment());
1964 Printer.printDIFlags("flags", N->getFlags());
1965 Printer.printDISPFlags("spFlags", N->getSPFlags());
1966 Printer.printMetadata("unit", N->getRawUnit());
1967 Printer.printMetadata("templateParams", N->getRawTemplateParams());
1968 Printer.printMetadata("declaration", N->getRawDeclaration());
1969 Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
1970 Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
1971 Out << ")";
1974 static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
1975 TypePrinting *TypePrinter, SlotTracker *Machine,
1976 const Module *Context) {
1977 Out << "!DILexicalBlock(";
1978 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1979 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1980 Printer.printMetadata("file", N->getRawFile());
1981 Printer.printInt("line", N->getLine());
1982 Printer.printInt("column", N->getColumn());
1983 Out << ")";
1986 static void writeDILexicalBlockFile(raw_ostream &Out,
1987 const DILexicalBlockFile *N,
1988 TypePrinting *TypePrinter,
1989 SlotTracker *Machine,
1990 const Module *Context) {
1991 Out << "!DILexicalBlockFile(";
1992 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
1993 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
1994 Printer.printMetadata("file", N->getRawFile());
1995 Printer.printInt("discriminator", N->getDiscriminator(),
1996 /* ShouldSkipZero */ false);
1997 Out << ")";
2000 static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
2001 TypePrinting *TypePrinter, SlotTracker *Machine,
2002 const Module *Context) {
2003 Out << "!DINamespace(";
2004 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2005 Printer.printString("name", N->getName());
2006 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2007 Printer.printBool("exportSymbols", N->getExportSymbols(), false);
2008 Out << ")";
2011 static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N,
2012 TypePrinting *TypePrinter, SlotTracker *Machine,
2013 const Module *Context) {
2014 Out << "!DICommonBlock(";
2015 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2016 Printer.printMetadata("scope", N->getRawScope(), false);
2017 Printer.printMetadata("declaration", N->getRawDecl(), false);
2018 Printer.printString("name", N->getName());
2019 Printer.printMetadata("file", N->getRawFile());
2020 Printer.printInt("line", N->getLineNo());
2021 Out << ")";
2024 static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2025 TypePrinting *TypePrinter, SlotTracker *Machine,
2026 const Module *Context) {
2027 Out << "!DIMacro(";
2028 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2029 Printer.printMacinfoType(N);
2030 Printer.printInt("line", N->getLine());
2031 Printer.printString("name", N->getName());
2032 Printer.printString("value", N->getValue());
2033 Out << ")";
2036 static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
2037 TypePrinting *TypePrinter, SlotTracker *Machine,
2038 const Module *Context) {
2039 Out << "!DIMacroFile(";
2040 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2041 Printer.printInt("line", N->getLine());
2042 Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
2043 Printer.printMetadata("nodes", N->getRawElements());
2044 Out << ")";
2047 static void writeDIModule(raw_ostream &Out, const DIModule *N,
2048 TypePrinting *TypePrinter, SlotTracker *Machine,
2049 const Module *Context) {
2050 Out << "!DIModule(";
2051 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2052 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2053 Printer.printString("name", N->getName());
2054 Printer.printString("configMacros", N->getConfigurationMacros());
2055 Printer.printString("includePath", N->getIncludePath());
2056 Printer.printString("isysroot", N->getISysRoot());
2057 Out << ")";
2061 static void writeDITemplateTypeParameter(raw_ostream &Out,
2062 const DITemplateTypeParameter *N,
2063 TypePrinting *TypePrinter,
2064 SlotTracker *Machine,
2065 const Module *Context) {
2066 Out << "!DITemplateTypeParameter(";
2067 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2068 Printer.printString("name", N->getName());
2069 Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
2070 Out << ")";
2073 static void writeDITemplateValueParameter(raw_ostream &Out,
2074 const DITemplateValueParameter *N,
2075 TypePrinting *TypePrinter,
2076 SlotTracker *Machine,
2077 const Module *Context) {
2078 Out << "!DITemplateValueParameter(";
2079 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2080 if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
2081 Printer.printTag(N);
2082 Printer.printString("name", N->getName());
2083 Printer.printMetadata("type", N->getRawType());
2084 Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
2085 Out << ")";
2088 static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
2089 TypePrinting *TypePrinter,
2090 SlotTracker *Machine, const Module *Context) {
2091 Out << "!DIGlobalVariable(";
2092 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2093 Printer.printString("name", N->getName());
2094 Printer.printString("linkageName", N->getLinkageName());
2095 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2096 Printer.printMetadata("file", N->getRawFile());
2097 Printer.printInt("line", N->getLine());
2098 Printer.printMetadata("type", N->getRawType());
2099 Printer.printBool("isLocal", N->isLocalToUnit());
2100 Printer.printBool("isDefinition", N->isDefinition());
2101 Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
2102 Printer.printMetadata("templateParams", N->getRawTemplateParams());
2103 Printer.printInt("align", N->getAlignInBits());
2104 Out << ")";
2107 static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
2108 TypePrinting *TypePrinter,
2109 SlotTracker *Machine, const Module *Context) {
2110 Out << "!DILocalVariable(";
2111 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2112 Printer.printString("name", N->getName());
2113 Printer.printInt("arg", N->getArg());
2114 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2115 Printer.printMetadata("file", N->getRawFile());
2116 Printer.printInt("line", N->getLine());
2117 Printer.printMetadata("type", N->getRawType());
2118 Printer.printDIFlags("flags", N->getFlags());
2119 Printer.printInt("align", N->getAlignInBits());
2120 Out << ")";
2123 static void writeDILabel(raw_ostream &Out, const DILabel *N,
2124 TypePrinting *TypePrinter,
2125 SlotTracker *Machine, const Module *Context) {
2126 Out << "!DILabel(";
2127 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2128 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2129 Printer.printString("name", N->getName());
2130 Printer.printMetadata("file", N->getRawFile());
2131 Printer.printInt("line", N->getLine());
2132 Out << ")";
2135 static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
2136 TypePrinting *TypePrinter, SlotTracker *Machine,
2137 const Module *Context) {
2138 Out << "!DIExpression(";
2139 FieldSeparator FS;
2140 if (N->isValid()) {
2141 for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
2142 auto OpStr = dwarf::OperationEncodingString(I->getOp());
2143 assert(!OpStr.empty() && "Expected valid opcode");
2145 Out << FS << OpStr;
2146 if (I->getOp() == dwarf::DW_OP_LLVM_convert) {
2147 Out << FS << I->getArg(0);
2148 Out << FS << dwarf::AttributeEncodingString(I->getArg(1));
2149 } else {
2150 for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
2151 Out << FS << I->getArg(A);
2154 } else {
2155 for (const auto &I : N->getElements())
2156 Out << FS << I;
2158 Out << ")";
2161 static void writeDIGlobalVariableExpression(raw_ostream &Out,
2162 const DIGlobalVariableExpression *N,
2163 TypePrinting *TypePrinter,
2164 SlotTracker *Machine,
2165 const Module *Context) {
2166 Out << "!DIGlobalVariableExpression(";
2167 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2168 Printer.printMetadata("var", N->getVariable());
2169 Printer.printMetadata("expr", N->getExpression());
2170 Out << ")";
2173 static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
2174 TypePrinting *TypePrinter, SlotTracker *Machine,
2175 const Module *Context) {
2176 Out << "!DIObjCProperty(";
2177 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2178 Printer.printString("name", N->getName());
2179 Printer.printMetadata("file", N->getRawFile());
2180 Printer.printInt("line", N->getLine());
2181 Printer.printString("setter", N->getSetterName());
2182 Printer.printString("getter", N->getGetterName());
2183 Printer.printInt("attributes", N->getAttributes());
2184 Printer.printMetadata("type", N->getRawType());
2185 Out << ")";
2188 static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
2189 TypePrinting *TypePrinter,
2190 SlotTracker *Machine, const Module *Context) {
2191 Out << "!DIImportedEntity(";
2192 MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
2193 Printer.printTag(N);
2194 Printer.printString("name", N->getName());
2195 Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
2196 Printer.printMetadata("entity", N->getRawEntity());
2197 Printer.printMetadata("file", N->getRawFile());
2198 Printer.printInt("line", N->getLine());
2199 Out << ")";
2202 static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2203 TypePrinting *TypePrinter,
2204 SlotTracker *Machine,
2205 const Module *Context) {
2206 if (Node->isDistinct())
2207 Out << "distinct ";
2208 else if (Node->isTemporary())
2209 Out << "<temporary!> "; // Handle broken code.
2211 switch (Node->getMetadataID()) {
2212 default:
2213 llvm_unreachable("Expected uniquable MDNode");
2214 #define HANDLE_MDNODE_LEAF(CLASS) \
2215 case Metadata::CLASS##Kind: \
2216 write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context); \
2217 break;
2218 #include "llvm/IR/Metadata.def"
2222 // Full implementation of printing a Value as an operand with support for
2223 // TypePrinting, etc.
2224 static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
2225 TypePrinting *TypePrinter,
2226 SlotTracker *Machine,
2227 const Module *Context) {
2228 if (V->hasName()) {
2229 PrintLLVMName(Out, V);
2230 return;
2233 const Constant *CV = dyn_cast<Constant>(V);
2234 if (CV && !isa<GlobalValue>(CV)) {
2235 assert(TypePrinter && "Constants require TypePrinting!");
2236 WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
2237 return;
2240 if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
2241 Out << "asm ";
2242 if (IA->hasSideEffects())
2243 Out << "sideeffect ";
2244 if (IA->isAlignStack())
2245 Out << "alignstack ";
2246 // We don't emit the AD_ATT dialect as it's the assumed default.
2247 if (IA->getDialect() == InlineAsm::AD_Intel)
2248 Out << "inteldialect ";
2249 Out << '"';
2250 printEscapedString(IA->getAsmString(), Out);
2251 Out << "\", \"";
2252 printEscapedString(IA->getConstraintString(), Out);
2253 Out << '"';
2254 return;
2257 if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2258 WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
2259 Context, /* FromValue */ true);
2260 return;
2263 char Prefix = '%';
2264 int Slot;
2265 // If we have a SlotTracker, use it.
2266 if (Machine) {
2267 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2268 Slot = Machine->getGlobalSlot(GV);
2269 Prefix = '@';
2270 } else {
2271 Slot = Machine->getLocalSlot(V);
2273 // If the local value didn't succeed, then we may be referring to a value
2274 // from a different function. Translate it, as this can happen when using
2275 // address of blocks.
2276 if (Slot == -1)
2277 if ((Machine = createSlotTracker(V))) {
2278 Slot = Machine->getLocalSlot(V);
2279 delete Machine;
2282 } else if ((Machine = createSlotTracker(V))) {
2283 // Otherwise, create one to get the # and then destroy it.
2284 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
2285 Slot = Machine->getGlobalSlot(GV);
2286 Prefix = '@';
2287 } else {
2288 Slot = Machine->getLocalSlot(V);
2290 delete Machine;
2291 Machine = nullptr;
2292 } else {
2293 Slot = -1;
2296 if (Slot != -1)
2297 Out << Prefix << Slot;
2298 else
2299 Out << "<badref>";
2302 static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2303 TypePrinting *TypePrinter,
2304 SlotTracker *Machine, const Module *Context,
2305 bool FromValue) {
2306 // Write DIExpressions inline when used as a value. Improves readability of
2307 // debug info intrinsics.
2308 if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2309 writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
2310 return;
2313 if (const MDNode *N = dyn_cast<MDNode>(MD)) {
2314 std::unique_ptr<SlotTracker> MachineStorage;
2315 if (!Machine) {
2316 MachineStorage = std::make_unique<SlotTracker>(Context);
2317 Machine = MachineStorage.get();
2319 int Slot = Machine->getMetadataSlot(N);
2320 if (Slot == -1) {
2321 if (const DILocation *Loc = dyn_cast<DILocation>(N)) {
2322 writeDILocation(Out, Loc, TypePrinter, Machine, Context);
2323 return;
2325 // Give the pointer value instead of "badref", since this comes up all
2326 // the time when debugging.
2327 Out << "<" << N << ">";
2328 } else
2329 Out << '!' << Slot;
2330 return;
2333 if (const MDString *MDS = dyn_cast<MDString>(MD)) {
2334 Out << "!\"";
2335 printEscapedString(MDS->getString(), Out);
2336 Out << '"';
2337 return;
2340 auto *V = cast<ValueAsMetadata>(MD);
2341 assert(TypePrinter && "TypePrinter required for metadata values");
2342 assert((FromValue || !isa<LocalAsMetadata>(V)) &&
2343 "Unexpected function-local metadata outside of value argument");
2345 TypePrinter->print(V->getValue()->getType(), Out);
2346 Out << ' ';
2347 WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
2350 namespace {
2352 class AssemblyWriter {
2353 formatted_raw_ostream &Out;
2354 const Module *TheModule = nullptr;
2355 const ModuleSummaryIndex *TheIndex = nullptr;
2356 std::unique_ptr<SlotTracker> SlotTrackerStorage;
2357 SlotTracker &Machine;
2358 TypePrinting TypePrinter;
2359 AssemblyAnnotationWriter *AnnotationWriter = nullptr;
2360 SetVector<const Comdat *> Comdats;
2361 bool IsForDebug;
2362 bool ShouldPreserveUseListOrder;
2363 UseListOrderStack UseListOrders;
2364 SmallVector<StringRef, 8> MDNames;
2365 /// Synchronization scope names registered with LLVMContext.
2366 SmallVector<StringRef, 8> SSNs;
2367 DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
2369 public:
2370 /// Construct an AssemblyWriter with an external SlotTracker
2371 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
2372 AssemblyAnnotationWriter *AAW, bool IsForDebug,
2373 bool ShouldPreserveUseListOrder = false);
2375 AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2376 const ModuleSummaryIndex *Index, bool IsForDebug);
2378 void printMDNodeBody(const MDNode *MD);
2379 void printNamedMDNode(const NamedMDNode *NMD);
2381 void printModule(const Module *M);
2383 void writeOperand(const Value *Op, bool PrintType);
2384 void writeParamOperand(const Value *Operand, AttributeSet Attrs);
2385 void writeOperandBundles(const CallBase *Call);
2386 void writeSyncScope(const LLVMContext &Context,
2387 SyncScope::ID SSID);
2388 void writeAtomic(const LLVMContext &Context,
2389 AtomicOrdering Ordering,
2390 SyncScope::ID SSID);
2391 void writeAtomicCmpXchg(const LLVMContext &Context,
2392 AtomicOrdering SuccessOrdering,
2393 AtomicOrdering FailureOrdering,
2394 SyncScope::ID SSID);
2396 void writeAllMDNodes();
2397 void writeMDNode(unsigned Slot, const MDNode *Node);
2398 void writeAllAttributeGroups();
2400 void printTypeIdentities();
2401 void printGlobal(const GlobalVariable *GV);
2402 void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
2403 void printComdat(const Comdat *C);
2404 void printFunction(const Function *F);
2405 void printArgument(const Argument *FA, AttributeSet Attrs);
2406 void printBasicBlock(const BasicBlock *BB);
2407 void printInstructionLine(const Instruction &I);
2408 void printInstruction(const Instruction &I);
2410 void printUseListOrder(const UseListOrder &Order);
2411 void printUseLists(const Function *F);
2413 void printModuleSummaryIndex();
2414 void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
2415 void printSummary(const GlobalValueSummary &Summary);
2416 void printAliasSummary(const AliasSummary *AS);
2417 void printGlobalVarSummary(const GlobalVarSummary *GS);
2418 void printFunctionSummary(const FunctionSummary *FS);
2419 void printTypeIdSummary(const TypeIdSummary &TIS);
2420 void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI);
2421 void printTypeTestResolution(const TypeTestResolution &TTRes);
2422 void printArgs(const std::vector<uint64_t> &Args);
2423 void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
2424 void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
2425 void printVFuncId(const FunctionSummary::VFuncId VFId);
2426 void
2427 printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
2428 const char *Tag);
2429 void
2430 printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
2431 const char *Tag);
2433 private:
2434 /// Print out metadata attachments.
2435 void printMetadataAttachments(
2436 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
2437 StringRef Separator);
2439 // printInfoComment - Print a little comment after the instruction indicating
2440 // which slot it occupies.
2441 void printInfoComment(const Value &V);
2443 // printGCRelocateComment - print comment after call to the gc.relocate
2444 // intrinsic indicating base and derived pointer names.
2445 void printGCRelocateComment(const GCRelocateInst &Relocate);
2448 } // end anonymous namespace
2450 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2451 const Module *M, AssemblyAnnotationWriter *AAW,
2452 bool IsForDebug, bool ShouldPreserveUseListOrder)
2453 : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
2454 IsForDebug(IsForDebug),
2455 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
2456 if (!TheModule)
2457 return;
2458 for (const GlobalObject &GO : TheModule->global_objects())
2459 if (const Comdat *C = GO.getComdat())
2460 Comdats.insert(C);
2463 AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
2464 const ModuleSummaryIndex *Index, bool IsForDebug)
2465 : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
2466 IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
2468 void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
2469 if (!Operand) {
2470 Out << "<null operand!>";
2471 return;
2473 if (PrintType) {
2474 TypePrinter.print(Operand->getType(), Out);
2475 Out << ' ';
2477 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
2480 void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
2481 SyncScope::ID SSID) {
2482 switch (SSID) {
2483 case SyncScope::System: {
2484 break;
2486 default: {
2487 if (SSNs.empty())
2488 Context.getSyncScopeNames(SSNs);
2490 Out << " syncscope(\"";
2491 printEscapedString(SSNs[SSID], Out);
2492 Out << "\")";
2493 break;
2498 void AssemblyWriter::writeAtomic(const LLVMContext &Context,
2499 AtomicOrdering Ordering,
2500 SyncScope::ID SSID) {
2501 if (Ordering == AtomicOrdering::NotAtomic)
2502 return;
2504 writeSyncScope(Context, SSID);
2505 Out << " " << toIRString(Ordering);
2508 void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
2509 AtomicOrdering SuccessOrdering,
2510 AtomicOrdering FailureOrdering,
2511 SyncScope::ID SSID) {
2512 assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
2513 FailureOrdering != AtomicOrdering::NotAtomic);
2515 writeSyncScope(Context, SSID);
2516 Out << " " << toIRString(SuccessOrdering);
2517 Out << " " << toIRString(FailureOrdering);
2520 void AssemblyWriter::writeParamOperand(const Value *Operand,
2521 AttributeSet Attrs) {
2522 if (!Operand) {
2523 Out << "<null operand!>";
2524 return;
2527 // Print the type
2528 TypePrinter.print(Operand->getType(), Out);
2529 // Print parameter attributes list
2530 if (Attrs.hasAttributes())
2531 Out << ' ' << Attrs.getAsString();
2532 Out << ' ';
2533 // Print the operand
2534 WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
2537 void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
2538 if (!Call->hasOperandBundles())
2539 return;
2541 Out << " [ ";
2543 bool FirstBundle = true;
2544 for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
2545 OperandBundleUse BU = Call->getOperandBundleAt(i);
2547 if (!FirstBundle)
2548 Out << ", ";
2549 FirstBundle = false;
2551 Out << '"';
2552 printEscapedString(BU.getTagName(), Out);
2553 Out << '"';
2555 Out << '(';
2557 bool FirstInput = true;
2558 for (const auto &Input : BU.Inputs) {
2559 if (!FirstInput)
2560 Out << ", ";
2561 FirstInput = false;
2563 TypePrinter.print(Input->getType(), Out);
2564 Out << " ";
2565 WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
2568 Out << ')';
2571 Out << " ]";
2574 void AssemblyWriter::printModule(const Module *M) {
2575 Machine.initializeIfNeeded();
2577 if (ShouldPreserveUseListOrder)
2578 UseListOrders = predictUseListOrder(M);
2580 if (!M->getModuleIdentifier().empty() &&
2581 // Don't print the ID if it will start a new line (which would
2582 // require a comment char before it).
2583 M->getModuleIdentifier().find('\n') == std::string::npos)
2584 Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
2586 if (!M->getSourceFileName().empty()) {
2587 Out << "source_filename = \"";
2588 printEscapedString(M->getSourceFileName(), Out);
2589 Out << "\"\n";
2592 const std::string &DL = M->getDataLayoutStr();
2593 if (!DL.empty())
2594 Out << "target datalayout = \"" << DL << "\"\n";
2595 if (!M->getTargetTriple().empty())
2596 Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
2598 if (!M->getModuleInlineAsm().empty()) {
2599 Out << '\n';
2601 // Split the string into lines, to make it easier to read the .ll file.
2602 StringRef Asm = M->getModuleInlineAsm();
2603 do {
2604 StringRef Front;
2605 std::tie(Front, Asm) = Asm.split('\n');
2607 // We found a newline, print the portion of the asm string from the
2608 // last newline up to this newline.
2609 Out << "module asm \"";
2610 printEscapedString(Front, Out);
2611 Out << "\"\n";
2612 } while (!Asm.empty());
2615 printTypeIdentities();
2617 // Output all comdats.
2618 if (!Comdats.empty())
2619 Out << '\n';
2620 for (const Comdat *C : Comdats) {
2621 printComdat(C);
2622 if (C != Comdats.back())
2623 Out << '\n';
2626 // Output all globals.
2627 if (!M->global_empty()) Out << '\n';
2628 for (const GlobalVariable &GV : M->globals()) {
2629 printGlobal(&GV); Out << '\n';
2632 // Output all aliases.
2633 if (!M->alias_empty()) Out << "\n";
2634 for (const GlobalAlias &GA : M->aliases())
2635 printIndirectSymbol(&GA);
2637 // Output all ifuncs.
2638 if (!M->ifunc_empty()) Out << "\n";
2639 for (const GlobalIFunc &GI : M->ifuncs())
2640 printIndirectSymbol(&GI);
2642 // Output global use-lists.
2643 printUseLists(nullptr);
2645 // Output all of the functions.
2646 for (const Function &F : *M)
2647 printFunction(&F);
2648 assert(UseListOrders.empty() && "All use-lists should have been consumed");
2650 // Output all attribute groups.
2651 if (!Machine.as_empty()) {
2652 Out << '\n';
2653 writeAllAttributeGroups();
2656 // Output named metadata.
2657 if (!M->named_metadata_empty()) Out << '\n';
2659 for (const NamedMDNode &Node : M->named_metadata())
2660 printNamedMDNode(&Node);
2662 // Output metadata.
2663 if (!Machine.mdn_empty()) {
2664 Out << '\n';
2665 writeAllMDNodes();
2669 void AssemblyWriter::printModuleSummaryIndex() {
2670 assert(TheIndex);
2671 Machine.initializeIndexIfNeeded();
2673 Out << "\n";
2675 // Print module path entries. To print in order, add paths to a vector
2676 // indexed by module slot.
2677 std::vector<std::pair<std::string, ModuleHash>> moduleVec;
2678 std::string RegularLTOModuleName = "[Regular LTO]";
2679 moduleVec.resize(TheIndex->modulePaths().size());
2680 for (auto &ModPath : TheIndex->modulePaths())
2681 moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
2682 // A module id of -1 is a special entry for a regular LTO module created
2683 // during the thin link.
2684 ModPath.second.first == -1u ? RegularLTOModuleName
2685 : (std::string)ModPath.first(),
2686 ModPath.second.second);
2688 unsigned i = 0;
2689 for (auto &ModPair : moduleVec) {
2690 Out << "^" << i++ << " = module: (";
2691 Out << "path: \"";
2692 printEscapedString(ModPair.first, Out);
2693 Out << "\", hash: (";
2694 FieldSeparator FS;
2695 for (auto Hash : ModPair.second)
2696 Out << FS << Hash;
2697 Out << "))\n";
2700 // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
2701 // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
2702 for (auto &GlobalList : *TheIndex) {
2703 auto GUID = GlobalList.first;
2704 for (auto &Summary : GlobalList.second.SummaryList)
2705 SummaryToGUIDMap[Summary.get()] = GUID;
2708 // Print the global value summary entries.
2709 for (auto &GlobalList : *TheIndex) {
2710 auto GUID = GlobalList.first;
2711 auto VI = TheIndex->getValueInfo(GlobalList);
2712 printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
2715 // Print the TypeIdMap entries.
2716 for (auto TidIter = TheIndex->typeIds().begin();
2717 TidIter != TheIndex->typeIds().end(); TidIter++) {
2718 Out << "^" << Machine.getTypeIdSlot(TidIter->second.first)
2719 << " = typeid: (name: \"" << TidIter->second.first << "\"";
2720 printTypeIdSummary(TidIter->second.second);
2721 Out << ") ; guid = " << TidIter->first << "\n";
2724 // Print the TypeIdCompatibleVtableMap entries.
2725 for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
2726 auto GUID = GlobalValue::getGUID(TId.first);
2727 Out << "^" << Machine.getGUIDSlot(GUID)
2728 << " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
2729 printTypeIdCompatibleVtableSummary(TId.second);
2730 Out << ") ; guid = " << GUID << "\n";
2734 static const char *
2735 getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
2736 switch (K) {
2737 case WholeProgramDevirtResolution::Indir:
2738 return "indir";
2739 case WholeProgramDevirtResolution::SingleImpl:
2740 return "singleImpl";
2741 case WholeProgramDevirtResolution::BranchFunnel:
2742 return "branchFunnel";
2744 llvm_unreachable("invalid WholeProgramDevirtResolution kind");
2747 static const char *getWholeProgDevirtResByArgKindName(
2748 WholeProgramDevirtResolution::ByArg::Kind K) {
2749 switch (K) {
2750 case WholeProgramDevirtResolution::ByArg::Indir:
2751 return "indir";
2752 case WholeProgramDevirtResolution::ByArg::UniformRetVal:
2753 return "uniformRetVal";
2754 case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
2755 return "uniqueRetVal";
2756 case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
2757 return "virtualConstProp";
2759 llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
2762 static const char *getTTResKindName(TypeTestResolution::Kind K) {
2763 switch (K) {
2764 case TypeTestResolution::Unsat:
2765 return "unsat";
2766 case TypeTestResolution::ByteArray:
2767 return "byteArray";
2768 case TypeTestResolution::Inline:
2769 return "inline";
2770 case TypeTestResolution::Single:
2771 return "single";
2772 case TypeTestResolution::AllOnes:
2773 return "allOnes";
2775 llvm_unreachable("invalid TypeTestResolution kind");
2778 void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
2779 Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
2780 << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
2782 // The following fields are only used if the target does not support the use
2783 // of absolute symbols to store constants. Print only if non-zero.
2784 if (TTRes.AlignLog2)
2785 Out << ", alignLog2: " << TTRes.AlignLog2;
2786 if (TTRes.SizeM1)
2787 Out << ", sizeM1: " << TTRes.SizeM1;
2788 if (TTRes.BitMask)
2789 // BitMask is uint8_t which causes it to print the corresponding char.
2790 Out << ", bitMask: " << (unsigned)TTRes.BitMask;
2791 if (TTRes.InlineBits)
2792 Out << ", inlineBits: " << TTRes.InlineBits;
2794 Out << ")";
2797 void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
2798 Out << ", summary: (";
2799 printTypeTestResolution(TIS.TTRes);
2800 if (!TIS.WPDRes.empty()) {
2801 Out << ", wpdResolutions: (";
2802 FieldSeparator FS;
2803 for (auto &WPDRes : TIS.WPDRes) {
2804 Out << FS;
2805 Out << "(offset: " << WPDRes.first << ", ";
2806 printWPDRes(WPDRes.second);
2807 Out << ")";
2809 Out << ")";
2811 Out << ")";
2814 void AssemblyWriter::printTypeIdCompatibleVtableSummary(
2815 const TypeIdCompatibleVtableInfo &TI) {
2816 Out << ", summary: (";
2817 FieldSeparator FS;
2818 for (auto &P : TI) {
2819 Out << FS;
2820 Out << "(offset: " << P.AddressPointOffset << ", ";
2821 Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID());
2822 Out << ")";
2824 Out << ")";
2827 void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
2828 Out << "args: (";
2829 FieldSeparator FS;
2830 for (auto arg : Args) {
2831 Out << FS;
2832 Out << arg;
2834 Out << ")";
2837 void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
2838 Out << "wpdRes: (kind: ";
2839 Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
2841 if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
2842 Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
2844 if (!WPDRes.ResByArg.empty()) {
2845 Out << ", resByArg: (";
2846 FieldSeparator FS;
2847 for (auto &ResByArg : WPDRes.ResByArg) {
2848 Out << FS;
2849 printArgs(ResByArg.first);
2850 Out << ", byArg: (kind: ";
2851 Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
2852 if (ResByArg.second.TheKind ==
2853 WholeProgramDevirtResolution::ByArg::UniformRetVal ||
2854 ResByArg.second.TheKind ==
2855 WholeProgramDevirtResolution::ByArg::UniqueRetVal)
2856 Out << ", info: " << ResByArg.second.Info;
2858 // The following fields are only used if the target does not support the
2859 // use of absolute symbols to store constants. Print only if non-zero.
2860 if (ResByArg.second.Byte || ResByArg.second.Bit)
2861 Out << ", byte: " << ResByArg.second.Byte
2862 << ", bit: " << ResByArg.second.Bit;
2864 Out << ")";
2866 Out << ")";
2868 Out << ")";
2871 static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
2872 switch (SK) {
2873 case GlobalValueSummary::AliasKind:
2874 return "alias";
2875 case GlobalValueSummary::FunctionKind:
2876 return "function";
2877 case GlobalValueSummary::GlobalVarKind:
2878 return "variable";
2880 llvm_unreachable("invalid summary kind");
2883 void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
2884 Out << ", aliasee: ";
2885 // The indexes emitted for distributed backends may not include the
2886 // aliasee summary (only if it is being imported directly). Handle
2887 // that case by just emitting "null" as the aliasee.
2888 if (AS->hasAliasee())
2889 Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
2890 else
2891 Out << "null";
2894 void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
2895 Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", "
2896 << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ")";
2898 auto VTableFuncs = GS->vTableFuncs();
2899 if (!VTableFuncs.empty()) {
2900 Out << ", vTableFuncs: (";
2901 FieldSeparator FS;
2902 for (auto &P : VTableFuncs) {
2903 Out << FS;
2904 Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID())
2905 << ", offset: " << P.VTableOffset;
2906 Out << ")";
2908 Out << ")";
2912 static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
2913 switch (LT) {
2914 case GlobalValue::ExternalLinkage:
2915 return "external";
2916 case GlobalValue::PrivateLinkage:
2917 return "private";
2918 case GlobalValue::InternalLinkage:
2919 return "internal";
2920 case GlobalValue::LinkOnceAnyLinkage:
2921 return "linkonce";
2922 case GlobalValue::LinkOnceODRLinkage:
2923 return "linkonce_odr";
2924 case GlobalValue::WeakAnyLinkage:
2925 return "weak";
2926 case GlobalValue::WeakODRLinkage:
2927 return "weak_odr";
2928 case GlobalValue::CommonLinkage:
2929 return "common";
2930 case GlobalValue::AppendingLinkage:
2931 return "appending";
2932 case GlobalValue::ExternalWeakLinkage:
2933 return "extern_weak";
2934 case GlobalValue::AvailableExternallyLinkage:
2935 return "available_externally";
2937 llvm_unreachable("invalid linkage");
2940 // When printing the linkage types in IR where the ExternalLinkage is
2941 // not printed, and other linkage types are expected to be printed with
2942 // a space after the name.
2943 static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
2944 if (LT == GlobalValue::ExternalLinkage)
2945 return "";
2946 return getLinkageName(LT) + " ";
2949 void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
2950 Out << ", insts: " << FS->instCount();
2952 FunctionSummary::FFlags FFlags = FS->fflags();
2953 if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
2954 FFlags.ReturnDoesNotAlias | FFlags.NoInline) {
2955 Out << ", funcFlags: (";
2956 Out << "readNone: " << FFlags.ReadNone;
2957 Out << ", readOnly: " << FFlags.ReadOnly;
2958 Out << ", noRecurse: " << FFlags.NoRecurse;
2959 Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
2960 Out << ", noInline: " << FFlags.NoInline;
2961 Out << ")";
2963 if (!FS->calls().empty()) {
2964 Out << ", calls: (";
2965 FieldSeparator IFS;
2966 for (auto &Call : FS->calls()) {
2967 Out << IFS;
2968 Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
2969 if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
2970 Out << ", hotness: " << getHotnessName(Call.second.getHotness());
2971 else if (Call.second.RelBlockFreq)
2972 Out << ", relbf: " << Call.second.RelBlockFreq;
2973 Out << ")";
2975 Out << ")";
2978 if (const auto *TIdInfo = FS->getTypeIdInfo())
2979 printTypeIdInfo(*TIdInfo);
2982 void AssemblyWriter::printTypeIdInfo(
2983 const FunctionSummary::TypeIdInfo &TIDInfo) {
2984 Out << ", typeIdInfo: (";
2985 FieldSeparator TIDFS;
2986 if (!TIDInfo.TypeTests.empty()) {
2987 Out << TIDFS;
2988 Out << "typeTests: (";
2989 FieldSeparator FS;
2990 for (auto &GUID : TIDInfo.TypeTests) {
2991 auto TidIter = TheIndex->typeIds().equal_range(GUID);
2992 if (TidIter.first == TidIter.second) {
2993 Out << FS;
2994 Out << GUID;
2995 continue;
2997 // Print all type id that correspond to this GUID.
2998 for (auto It = TidIter.first; It != TidIter.second; ++It) {
2999 Out << FS;
3000 auto Slot = Machine.getTypeIdSlot(It->second.first);
3001 assert(Slot != -1);
3002 Out << "^" << Slot;
3005 Out << ")";
3007 if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
3008 Out << TIDFS;
3009 printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
3011 if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
3012 Out << TIDFS;
3013 printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
3015 if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
3016 Out << TIDFS;
3017 printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
3018 "typeTestAssumeConstVCalls");
3020 if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
3021 Out << TIDFS;
3022 printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
3023 "typeCheckedLoadConstVCalls");
3025 Out << ")";
3028 void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
3029 auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
3030 if (TidIter.first == TidIter.second) {
3031 Out << "vFuncId: (";
3032 Out << "guid: " << VFId.GUID;
3033 Out << ", offset: " << VFId.Offset;
3034 Out << ")";
3035 return;
3037 // Print all type id that correspond to this GUID.
3038 FieldSeparator FS;
3039 for (auto It = TidIter.first; It != TidIter.second; ++It) {
3040 Out << FS;
3041 Out << "vFuncId: (";
3042 auto Slot = Machine.getTypeIdSlot(It->second.first);
3043 assert(Slot != -1);
3044 Out << "^" << Slot;
3045 Out << ", offset: " << VFId.Offset;
3046 Out << ")";
3050 void AssemblyWriter::printNonConstVCalls(
3051 const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
3052 Out << Tag << ": (";
3053 FieldSeparator FS;
3054 for (auto &VFuncId : VCallList) {
3055 Out << FS;
3056 printVFuncId(VFuncId);
3058 Out << ")";
3061 void AssemblyWriter::printConstVCalls(
3062 const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
3063 Out << Tag << ": (";
3064 FieldSeparator FS;
3065 for (auto &ConstVCall : VCallList) {
3066 Out << FS;
3067 Out << "(";
3068 printVFuncId(ConstVCall.VFunc);
3069 if (!ConstVCall.Args.empty()) {
3070 Out << ", ";
3071 printArgs(ConstVCall.Args);
3073 Out << ")";
3075 Out << ")";
3078 void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
3079 GlobalValueSummary::GVFlags GVFlags = Summary.flags();
3080 GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
3081 Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
3082 Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
3083 << ", flags: (";
3084 Out << "linkage: " << getLinkageName(LT);
3085 Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
3086 Out << ", live: " << GVFlags.Live;
3087 Out << ", dsoLocal: " << GVFlags.DSOLocal;
3088 Out << ", canAutoHide: " << GVFlags.CanAutoHide;
3089 Out << ")";
3091 if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
3092 printAliasSummary(cast<AliasSummary>(&Summary));
3093 else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
3094 printFunctionSummary(cast<FunctionSummary>(&Summary));
3095 else
3096 printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
3098 auto RefList = Summary.refs();
3099 if (!RefList.empty()) {
3100 Out << ", refs: (";
3101 FieldSeparator FS;
3102 for (auto &Ref : RefList) {
3103 Out << FS;
3104 if (Ref.isReadOnly())
3105 Out << "readonly ";
3106 else if (Ref.isWriteOnly())
3107 Out << "writeonly ";
3108 Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
3110 Out << ")";
3113 Out << ")";
3116 void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
3117 Out << "^" << Slot << " = gv: (";
3118 if (!VI.name().empty())
3119 Out << "name: \"" << VI.name() << "\"";
3120 else
3121 Out << "guid: " << VI.getGUID();
3122 if (!VI.getSummaryList().empty()) {
3123 Out << ", summaries: (";
3124 FieldSeparator FS;
3125 for (auto &Summary : VI.getSummaryList()) {
3126 Out << FS;
3127 printSummary(*Summary);
3129 Out << ")";
3131 Out << ")";
3132 if (!VI.name().empty())
3133 Out << " ; guid = " << VI.getGUID();
3134 Out << "\n";
3137 static void printMetadataIdentifier(StringRef Name,
3138 formatted_raw_ostream &Out) {
3139 if (Name.empty()) {
3140 Out << "<empty name> ";
3141 } else {
3142 if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
3143 Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
3144 Out << Name[0];
3145 else
3146 Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
3147 for (unsigned i = 1, e = Name.size(); i != e; ++i) {
3148 unsigned char C = Name[i];
3149 if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
3150 C == '.' || C == '_')
3151 Out << C;
3152 else
3153 Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
3158 void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
3159 Out << '!';
3160 printMetadataIdentifier(NMD->getName(), Out);
3161 Out << " = !{";
3162 for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
3163 if (i)
3164 Out << ", ";
3166 // Write DIExpressions inline.
3167 // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
3168 MDNode *Op = NMD->getOperand(i);
3169 if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3170 writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
3171 continue;
3174 int Slot = Machine.getMetadataSlot(Op);
3175 if (Slot == -1)
3176 Out << "<badref>";
3177 else
3178 Out << '!' << Slot;
3180 Out << "}\n";
3183 static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
3184 formatted_raw_ostream &Out) {
3185 switch (Vis) {
3186 case GlobalValue::DefaultVisibility: break;
3187 case GlobalValue::HiddenVisibility: Out << "hidden "; break;
3188 case GlobalValue::ProtectedVisibility: Out << "protected "; break;
3192 static void PrintDSOLocation(const GlobalValue &GV,
3193 formatted_raw_ostream &Out) {
3194 // GVs with local linkage or non default visibility are implicitly dso_local,
3195 // so we don't print it.
3196 bool Implicit = GV.hasLocalLinkage() ||
3197 (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
3198 if (GV.isDSOLocal() && !Implicit)
3199 Out << "dso_local ";
3202 static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
3203 formatted_raw_ostream &Out) {
3204 switch (SCT) {
3205 case GlobalValue::DefaultStorageClass: break;
3206 case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
3207 case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
3211 static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
3212 formatted_raw_ostream &Out) {
3213 switch (TLM) {
3214 case GlobalVariable::NotThreadLocal:
3215 break;
3216 case GlobalVariable::GeneralDynamicTLSModel:
3217 Out << "thread_local ";
3218 break;
3219 case GlobalVariable::LocalDynamicTLSModel:
3220 Out << "thread_local(localdynamic) ";
3221 break;
3222 case GlobalVariable::InitialExecTLSModel:
3223 Out << "thread_local(initialexec) ";
3224 break;
3225 case GlobalVariable::LocalExecTLSModel:
3226 Out << "thread_local(localexec) ";
3227 break;
3231 static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
3232 switch (UA) {
3233 case GlobalVariable::UnnamedAddr::None:
3234 return "";
3235 case GlobalVariable::UnnamedAddr::Local:
3236 return "local_unnamed_addr";
3237 case GlobalVariable::UnnamedAddr::Global:
3238 return "unnamed_addr";
3240 llvm_unreachable("Unknown UnnamedAddr");
3243 static void maybePrintComdat(formatted_raw_ostream &Out,
3244 const GlobalObject &GO) {
3245 const Comdat *C = GO.getComdat();
3246 if (!C)
3247 return;
3249 if (isa<GlobalVariable>(GO))
3250 Out << ',';
3251 Out << " comdat";
3253 if (GO.getName() == C->getName())
3254 return;
3256 Out << '(';
3257 PrintLLVMName(Out, C->getName(), ComdatPrefix);
3258 Out << ')';
3261 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
3262 if (GV->isMaterializable())
3263 Out << "; Materializable\n";
3265 WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
3266 Out << " = ";
3268 if (!GV->hasInitializer() && GV->hasExternalLinkage())
3269 Out << "external ";
3271 Out << getLinkageNameWithSpace(GV->getLinkage());
3272 PrintDSOLocation(*GV, Out);
3273 PrintVisibility(GV->getVisibility(), Out);
3274 PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
3275 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
3276 StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
3277 if (!UA.empty())
3278 Out << UA << ' ';
3280 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
3281 Out << "addrspace(" << AddressSpace << ") ";
3282 if (GV->isExternallyInitialized()) Out << "externally_initialized ";
3283 Out << (GV->isConstant() ? "constant " : "global ");
3284 TypePrinter.print(GV->getValueType(), Out);
3286 if (GV->hasInitializer()) {
3287 Out << ' ';
3288 writeOperand(GV->getInitializer(), false);
3291 if (GV->hasSection()) {
3292 Out << ", section \"";
3293 printEscapedString(GV->getSection(), Out);
3294 Out << '"';
3296 if (GV->hasPartition()) {
3297 Out << ", partition \"";
3298 printEscapedString(GV->getPartition(), Out);
3299 Out << '"';
3302 maybePrintComdat(Out, *GV);
3303 if (GV->getAlignment())
3304 Out << ", align " << GV->getAlignment();
3306 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3307 GV->getAllMetadata(MDs);
3308 printMetadataAttachments(MDs, ", ");
3310 auto Attrs = GV->getAttributes();
3311 if (Attrs.hasAttributes())
3312 Out << " #" << Machine.getAttributeGroupSlot(Attrs);
3314 printInfoComment(*GV);
3317 void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
3318 if (GIS->isMaterializable())
3319 Out << "; Materializable\n";
3321 WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
3322 Out << " = ";
3324 Out << getLinkageNameWithSpace(GIS->getLinkage());
3325 PrintDSOLocation(*GIS, Out);
3326 PrintVisibility(GIS->getVisibility(), Out);
3327 PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
3328 PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
3329 StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
3330 if (!UA.empty())
3331 Out << UA << ' ';
3333 if (isa<GlobalAlias>(GIS))
3334 Out << "alias ";
3335 else if (isa<GlobalIFunc>(GIS))
3336 Out << "ifunc ";
3337 else
3338 llvm_unreachable("Not an alias or ifunc!");
3340 TypePrinter.print(GIS->getValueType(), Out);
3342 Out << ", ";
3344 const Constant *IS = GIS->getIndirectSymbol();
3346 if (!IS) {
3347 TypePrinter.print(GIS->getType(), Out);
3348 Out << " <<NULL ALIASEE>>";
3349 } else {
3350 writeOperand(IS, !isa<ConstantExpr>(IS));
3353 if (GIS->hasPartition()) {
3354 Out << ", partition \"";
3355 printEscapedString(GIS->getPartition(), Out);
3356 Out << '"';
3359 printInfoComment(*GIS);
3360 Out << '\n';
3363 void AssemblyWriter::printComdat(const Comdat *C) {
3364 C->print(Out);
3367 void AssemblyWriter::printTypeIdentities() {
3368 if (TypePrinter.empty())
3369 return;
3371 Out << '\n';
3373 // Emit all numbered types.
3374 auto &NumberedTypes = TypePrinter.getNumberedTypes();
3375 for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
3376 Out << '%' << I << " = type ";
3378 // Make sure we print out at least one level of the type structure, so
3379 // that we do not get %2 = type %2
3380 TypePrinter.printStructBody(NumberedTypes[I], Out);
3381 Out << '\n';
3384 auto &NamedTypes = TypePrinter.getNamedTypes();
3385 for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
3386 PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
3387 Out << " = type ";
3389 // Make sure we print out at least one level of the type structure, so
3390 // that we do not get %FILE = type %FILE
3391 TypePrinter.printStructBody(NamedTypes[I], Out);
3392 Out << '\n';
3396 /// printFunction - Print all aspects of a function.
3397 void AssemblyWriter::printFunction(const Function *F) {
3398 // Print out the return type and name.
3399 Out << '\n';
3401 if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
3403 if (F->isMaterializable())
3404 Out << "; Materializable\n";
3406 const AttributeList &Attrs = F->getAttributes();
3407 if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
3408 AttributeSet AS = Attrs.getFnAttributes();
3409 std::string AttrStr;
3411 for (const Attribute &Attr : AS) {
3412 if (!Attr.isStringAttribute()) {
3413 if (!AttrStr.empty()) AttrStr += ' ';
3414 AttrStr += Attr.getAsString();
3418 if (!AttrStr.empty())
3419 Out << "; Function Attrs: " << AttrStr << '\n';
3422 Machine.incorporateFunction(F);
3424 if (F->isDeclaration()) {
3425 Out << "declare";
3426 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3427 F->getAllMetadata(MDs);
3428 printMetadataAttachments(MDs, " ");
3429 Out << ' ';
3430 } else
3431 Out << "define ";
3433 Out << getLinkageNameWithSpace(F->getLinkage());
3434 PrintDSOLocation(*F, Out);
3435 PrintVisibility(F->getVisibility(), Out);
3436 PrintDLLStorageClass(F->getDLLStorageClass(), Out);
3438 // Print the calling convention.
3439 if (F->getCallingConv() != CallingConv::C) {
3440 PrintCallingConv(F->getCallingConv(), Out);
3441 Out << " ";
3444 FunctionType *FT = F->getFunctionType();
3445 if (Attrs.hasAttributes(AttributeList::ReturnIndex))
3446 Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
3447 TypePrinter.print(F->getReturnType(), Out);
3448 Out << ' ';
3449 WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
3450 Out << '(';
3452 // Loop over the arguments, printing them...
3453 if (F->isDeclaration() && !IsForDebug) {
3454 // We're only interested in the type here - don't print argument names.
3455 for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
3456 // Insert commas as we go... the first arg doesn't get a comma
3457 if (I)
3458 Out << ", ";
3459 // Output type...
3460 TypePrinter.print(FT->getParamType(I), Out);
3462 AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
3463 if (ArgAttrs.hasAttributes())
3464 Out << ' ' << ArgAttrs.getAsString();
3466 } else {
3467 // The arguments are meaningful here, print them in detail.
3468 for (const Argument &Arg : F->args()) {
3469 // Insert commas as we go... the first arg doesn't get a comma
3470 if (Arg.getArgNo() != 0)
3471 Out << ", ";
3472 printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
3476 // Finish printing arguments...
3477 if (FT->isVarArg()) {
3478 if (FT->getNumParams()) Out << ", ";
3479 Out << "..."; // Output varargs portion of signature!
3481 Out << ')';
3482 StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
3483 if (!UA.empty())
3484 Out << ' ' << UA;
3485 // We print the function address space if it is non-zero or if we are writing
3486 // a module with a non-zero program address space or if there is no valid
3487 // Module* so that the file can be parsed without the datalayout string.
3488 const Module *Mod = F->getParent();
3489 if (F->getAddressSpace() != 0 || !Mod ||
3490 Mod->getDataLayout().getProgramAddressSpace() != 0)
3491 Out << " addrspace(" << F->getAddressSpace() << ")";
3492 if (Attrs.hasAttributes(AttributeList::FunctionIndex))
3493 Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
3494 if (F->hasSection()) {
3495 Out << " section \"";
3496 printEscapedString(F->getSection(), Out);
3497 Out << '"';
3499 if (F->hasPartition()) {
3500 Out << " partition \"";
3501 printEscapedString(F->getPartition(), Out);
3502 Out << '"';
3504 maybePrintComdat(Out, *F);
3505 if (F->getAlignment())
3506 Out << " align " << F->getAlignment();
3507 if (F->hasGC())
3508 Out << " gc \"" << F->getGC() << '"';
3509 if (F->hasPrefixData()) {
3510 Out << " prefix ";
3511 writeOperand(F->getPrefixData(), true);
3513 if (F->hasPrologueData()) {
3514 Out << " prologue ";
3515 writeOperand(F->getPrologueData(), true);
3517 if (F->hasPersonalityFn()) {
3518 Out << " personality ";
3519 writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
3522 if (F->isDeclaration()) {
3523 Out << '\n';
3524 } else {
3525 SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
3526 F->getAllMetadata(MDs);
3527 printMetadataAttachments(MDs, " ");
3529 Out << " {";
3530 // Output all of the function's basic blocks.
3531 for (const BasicBlock &BB : *F)
3532 printBasicBlock(&BB);
3534 // Output the function's use-lists.
3535 printUseLists(F);
3537 Out << "}\n";
3540 Machine.purgeFunction();
3543 /// printArgument - This member is called for every argument that is passed into
3544 /// the function. Simply print it out
3545 void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
3546 // Output type...
3547 TypePrinter.print(Arg->getType(), Out);
3549 // Output parameter attributes list
3550 if (Attrs.hasAttributes())
3551 Out << ' ' << Attrs.getAsString();
3553 // Output name, if available...
3554 if (Arg->hasName()) {
3555 Out << ' ';
3556 PrintLLVMName(Out, Arg);
3557 } else {
3558 int Slot = Machine.getLocalSlot(Arg);
3559 assert(Slot != -1 && "expect argument in function here");
3560 Out << " %" << Slot;
3564 /// printBasicBlock - This member is called for each basic block in a method.
3565 void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
3566 bool IsEntryBlock = BB == &BB->getParent()->getEntryBlock();
3567 if (BB->hasName()) { // Print out the label if it exists...
3568 Out << "\n";
3569 PrintLLVMName(Out, BB->getName(), LabelPrefix);
3570 Out << ':';
3571 } else if (!IsEntryBlock) {
3572 Out << "\n";
3573 int Slot = Machine.getLocalSlot(BB);
3574 if (Slot != -1)
3575 Out << Slot << ":";
3576 else
3577 Out << "<badref>:";
3580 if (!BB->getParent()) {
3581 Out.PadToColumn(50);
3582 Out << "; Error: Block without parent!";
3583 } else if (!IsEntryBlock) {
3584 // Output predecessors for the block.
3585 Out.PadToColumn(50);
3586 Out << ";";
3587 const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
3589 if (PI == PE) {
3590 Out << " No predecessors!";
3591 } else {
3592 Out << " preds = ";
3593 writeOperand(*PI, false);
3594 for (++PI; PI != PE; ++PI) {
3595 Out << ", ";
3596 writeOperand(*PI, false);
3601 Out << "\n";
3603 if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
3605 // Output all of the instructions in the basic block...
3606 for (const Instruction &I : *BB) {
3607 printInstructionLine(I);
3610 if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
3613 /// printInstructionLine - Print an instruction and a newline character.
3614 void AssemblyWriter::printInstructionLine(const Instruction &I) {
3615 printInstruction(I);
3616 Out << '\n';
3619 /// printGCRelocateComment - print comment after call to the gc.relocate
3620 /// intrinsic indicating base and derived pointer names.
3621 void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
3622 Out << " ; (";
3623 writeOperand(Relocate.getBasePtr(), false);
3624 Out << ", ";
3625 writeOperand(Relocate.getDerivedPtr(), false);
3626 Out << ")";
3629 /// printInfoComment - Print a little comment after the instruction indicating
3630 /// which slot it occupies.
3631 void AssemblyWriter::printInfoComment(const Value &V) {
3632 if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
3633 printGCRelocateComment(*Relocate);
3635 if (AnnotationWriter)
3636 AnnotationWriter->printInfoComment(V, Out);
3639 static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
3640 raw_ostream &Out) {
3641 // We print the address space of the call if it is non-zero.
3642 unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
3643 bool PrintAddrSpace = CallAddrSpace != 0;
3644 if (!PrintAddrSpace) {
3645 const Module *Mod = getModuleFromVal(I);
3646 // We also print it if it is zero but not equal to the program address space
3647 // or if we can't find a valid Module* to make it possible to parse
3648 // the resulting file even without a datalayout string.
3649 if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
3650 PrintAddrSpace = true;
3652 if (PrintAddrSpace)
3653 Out << " addrspace(" << CallAddrSpace << ")";
3656 // This member is called for each Instruction in a function..
3657 void AssemblyWriter::printInstruction(const Instruction &I) {
3658 if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
3660 // Print out indentation for an instruction.
3661 Out << " ";
3663 // Print out name if it exists...
3664 if (I.hasName()) {
3665 PrintLLVMName(Out, &I);
3666 Out << " = ";
3667 } else if (!I.getType()->isVoidTy()) {
3668 // Print out the def slot taken.
3669 int SlotNum = Machine.getLocalSlot(&I);
3670 if (SlotNum == -1)
3671 Out << "<badref> = ";
3672 else
3673 Out << '%' << SlotNum << " = ";
3676 if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3677 if (CI->isMustTailCall())
3678 Out << "musttail ";
3679 else if (CI->isTailCall())
3680 Out << "tail ";
3681 else if (CI->isNoTailCall())
3682 Out << "notail ";
3685 // Print out the opcode...
3686 Out << I.getOpcodeName();
3688 // If this is an atomic load or store, print out the atomic marker.
3689 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) ||
3690 (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
3691 Out << " atomic";
3693 if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
3694 Out << " weak";
3696 // If this is a volatile operation, print out the volatile marker.
3697 if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) ||
3698 (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
3699 (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
3700 (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
3701 Out << " volatile";
3703 // Print out optimization information.
3704 WriteOptimizationInfo(Out, &I);
3706 // Print out the compare instruction predicates
3707 if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
3708 Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
3710 // Print out the atomicrmw operation
3711 if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
3712 Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
3714 // Print out the type of the operands...
3715 const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
3717 // Special case conditional branches to swizzle the condition out to the front
3718 if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
3719 const BranchInst &BI(cast<BranchInst>(I));
3720 Out << ' ';
3721 writeOperand(BI.getCondition(), true);
3722 Out << ", ";
3723 writeOperand(BI.getSuccessor(0), true);
3724 Out << ", ";
3725 writeOperand(BI.getSuccessor(1), true);
3727 } else if (isa<SwitchInst>(I)) {
3728 const SwitchInst& SI(cast<SwitchInst>(I));
3729 // Special case switch instruction to get formatting nice and correct.
3730 Out << ' ';
3731 writeOperand(SI.getCondition(), true);
3732 Out << ", ";
3733 writeOperand(SI.getDefaultDest(), true);
3734 Out << " [";
3735 for (auto Case : SI.cases()) {
3736 Out << "\n ";
3737 writeOperand(Case.getCaseValue(), true);
3738 Out << ", ";
3739 writeOperand(Case.getCaseSuccessor(), true);
3741 Out << "\n ]";
3742 } else if (isa<IndirectBrInst>(I)) {
3743 // Special case indirectbr instruction to get formatting nice and correct.
3744 Out << ' ';
3745 writeOperand(Operand, true);
3746 Out << ", [";
3748 for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
3749 if (i != 1)
3750 Out << ", ";
3751 writeOperand(I.getOperand(i), true);
3753 Out << ']';
3754 } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
3755 Out << ' ';
3756 TypePrinter.print(I.getType(), Out);
3757 Out << ' ';
3759 for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
3760 if (op) Out << ", ";
3761 Out << "[ ";
3762 writeOperand(PN->getIncomingValue(op), false); Out << ", ";
3763 writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
3765 } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
3766 Out << ' ';
3767 writeOperand(I.getOperand(0), true);
3768 for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
3769 Out << ", " << *i;
3770 } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
3771 Out << ' ';
3772 writeOperand(I.getOperand(0), true); Out << ", ";
3773 writeOperand(I.getOperand(1), true);
3774 for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
3775 Out << ", " << *i;
3776 } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
3777 Out << ' ';
3778 TypePrinter.print(I.getType(), Out);
3779 if (LPI->isCleanup() || LPI->getNumClauses() != 0)
3780 Out << '\n';
3782 if (LPI->isCleanup())
3783 Out << " cleanup";
3785 for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
3786 if (i != 0 || LPI->isCleanup()) Out << "\n";
3787 if (LPI->isCatch(i))
3788 Out << " catch ";
3789 else
3790 Out << " filter ";
3792 writeOperand(LPI->getClause(i), true);
3794 } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
3795 Out << " within ";
3796 writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
3797 Out << " [";
3798 unsigned Op = 0;
3799 for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
3800 if (Op > 0)
3801 Out << ", ";
3802 writeOperand(PadBB, /*PrintType=*/true);
3803 ++Op;
3805 Out << "] unwind ";
3806 if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
3807 writeOperand(UnwindDest, /*PrintType=*/true);
3808 else
3809 Out << "to caller";
3810 } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
3811 Out << " within ";
3812 writeOperand(FPI->getParentPad(), /*PrintType=*/false);
3813 Out << " [";
3814 for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
3815 ++Op) {
3816 if (Op > 0)
3817 Out << ", ";
3818 writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
3820 Out << ']';
3821 } else if (isa<ReturnInst>(I) && !Operand) {
3822 Out << " void";
3823 } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
3824 Out << " from ";
3825 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
3827 Out << " to ";
3828 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
3829 } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
3830 Out << " from ";
3831 writeOperand(CRI->getOperand(0), /*PrintType=*/false);
3833 Out << " unwind ";
3834 if (CRI->hasUnwindDest())
3835 writeOperand(CRI->getOperand(1), /*PrintType=*/true);
3836 else
3837 Out << "to caller";
3838 } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
3839 // Print the calling convention being used.
3840 if (CI->getCallingConv() != CallingConv::C) {
3841 Out << " ";
3842 PrintCallingConv(CI->getCallingConv(), Out);
3845 Operand = CI->getCalledValue();
3846 FunctionType *FTy = CI->getFunctionType();
3847 Type *RetTy = FTy->getReturnType();
3848 const AttributeList &PAL = CI->getAttributes();
3850 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3851 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3853 // Only print addrspace(N) if necessary:
3854 maybePrintCallAddrSpace(Operand, &I, Out);
3856 // If possible, print out the short form of the call instruction. We can
3857 // only do this if the first argument is a pointer to a nonvararg function,
3858 // and if the return type is not a pointer to a function.
3860 Out << ' ';
3861 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3862 Out << ' ';
3863 writeOperand(Operand, false);
3864 Out << '(';
3865 for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
3866 if (op > 0)
3867 Out << ", ";
3868 writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
3871 // Emit an ellipsis if this is a musttail call in a vararg function. This
3872 // is only to aid readability, musttail calls forward varargs by default.
3873 if (CI->isMustTailCall() && CI->getParent() &&
3874 CI->getParent()->getParent() &&
3875 CI->getParent()->getParent()->isVarArg())
3876 Out << ", ...";
3878 Out << ')';
3879 if (PAL.hasAttributes(AttributeList::FunctionIndex))
3880 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3882 writeOperandBundles(CI);
3883 } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
3884 Operand = II->getCalledValue();
3885 FunctionType *FTy = II->getFunctionType();
3886 Type *RetTy = FTy->getReturnType();
3887 const AttributeList &PAL = II->getAttributes();
3889 // Print the calling convention being used.
3890 if (II->getCallingConv() != CallingConv::C) {
3891 Out << " ";
3892 PrintCallingConv(II->getCallingConv(), Out);
3895 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3896 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3898 // Only print addrspace(N) if necessary:
3899 maybePrintCallAddrSpace(Operand, &I, Out);
3901 // If possible, print out the short form of the invoke instruction. We can
3902 // only do this if the first argument is a pointer to a nonvararg function,
3903 // and if the return type is not a pointer to a function.
3905 Out << ' ';
3906 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3907 Out << ' ';
3908 writeOperand(Operand, false);
3909 Out << '(';
3910 for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
3911 if (op)
3912 Out << ", ";
3913 writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
3916 Out << ')';
3917 if (PAL.hasAttributes(AttributeList::FunctionIndex))
3918 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3920 writeOperandBundles(II);
3922 Out << "\n to ";
3923 writeOperand(II->getNormalDest(), true);
3924 Out << " unwind ";
3925 writeOperand(II->getUnwindDest(), true);
3926 } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
3927 Operand = CBI->getCalledValue();
3928 FunctionType *FTy = CBI->getFunctionType();
3929 Type *RetTy = FTy->getReturnType();
3930 const AttributeList &PAL = CBI->getAttributes();
3932 // Print the calling convention being used.
3933 if (CBI->getCallingConv() != CallingConv::C) {
3934 Out << " ";
3935 PrintCallingConv(CBI->getCallingConv(), Out);
3938 if (PAL.hasAttributes(AttributeList::ReturnIndex))
3939 Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
3941 // If possible, print out the short form of the callbr instruction. We can
3942 // only do this if the first argument is a pointer to a nonvararg function,
3943 // and if the return type is not a pointer to a function.
3945 Out << ' ';
3946 TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
3947 Out << ' ';
3948 writeOperand(Operand, false);
3949 Out << '(';
3950 for (unsigned op = 0, Eop = CBI->getNumArgOperands(); op < Eop; ++op) {
3951 if (op)
3952 Out << ", ";
3953 writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttributes(op));
3956 Out << ')';
3957 if (PAL.hasAttributes(AttributeList::FunctionIndex))
3958 Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
3960 writeOperandBundles(CBI);
3962 Out << "\n to ";
3963 writeOperand(CBI->getDefaultDest(), true);
3964 Out << " [";
3965 for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) {
3966 if (i != 0)
3967 Out << ", ";
3968 writeOperand(CBI->getIndirectDest(i), true);
3970 Out << ']';
3971 } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
3972 Out << ' ';
3973 if (AI->isUsedWithInAlloca())
3974 Out << "inalloca ";
3975 if (AI->isSwiftError())
3976 Out << "swifterror ";
3977 TypePrinter.print(AI->getAllocatedType(), Out);
3979 // Explicitly write the array size if the code is broken, if it's an array
3980 // allocation, or if the type is not canonical for scalar allocations. The
3981 // latter case prevents the type from mutating when round-tripping through
3982 // assembly.
3983 if (!AI->getArraySize() || AI->isArrayAllocation() ||
3984 !AI->getArraySize()->getType()->isIntegerTy(32)) {
3985 Out << ", ";
3986 writeOperand(AI->getArraySize(), true);
3988 if (AI->getAlignment()) {
3989 Out << ", align " << AI->getAlignment();
3992 unsigned AddrSpace = AI->getType()->getAddressSpace();
3993 if (AddrSpace != 0) {
3994 Out << ", addrspace(" << AddrSpace << ')';
3996 } else if (isa<CastInst>(I)) {
3997 if (Operand) {
3998 Out << ' ';
3999 writeOperand(Operand, true); // Work with broken code
4001 Out << " to ";
4002 TypePrinter.print(I.getType(), Out);
4003 } else if (isa<VAArgInst>(I)) {
4004 if (Operand) {
4005 Out << ' ';
4006 writeOperand(Operand, true); // Work with broken code
4008 Out << ", ";
4009 TypePrinter.print(I.getType(), Out);
4010 } else if (Operand) { // Print the normal way.
4011 if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
4012 Out << ' ';
4013 TypePrinter.print(GEP->getSourceElementType(), Out);
4014 Out << ',';
4015 } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
4016 Out << ' ';
4017 TypePrinter.print(LI->getType(), Out);
4018 Out << ',';
4021 // PrintAllTypes - Instructions who have operands of all the same type
4022 // omit the type from all but the first operand. If the instruction has
4023 // different type operands (for example br), then they are all printed.
4024 bool PrintAllTypes = false;
4025 Type *TheType = Operand->getType();
4027 // Select, Store and ShuffleVector always print all types.
4028 if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
4029 || isa<ReturnInst>(I)) {
4030 PrintAllTypes = true;
4031 } else {
4032 for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
4033 Operand = I.getOperand(i);
4034 // note that Operand shouldn't be null, but the test helps make dump()
4035 // more tolerant of malformed IR
4036 if (Operand && Operand->getType() != TheType) {
4037 PrintAllTypes = true; // We have differing types! Print them all!
4038 break;
4043 if (!PrintAllTypes) {
4044 Out << ' ';
4045 TypePrinter.print(TheType, Out);
4048 Out << ' ';
4049 for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
4050 if (i) Out << ", ";
4051 writeOperand(I.getOperand(i), PrintAllTypes);
4055 // Print atomic ordering/alignment for memory operations
4056 if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
4057 if (LI->isAtomic())
4058 writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
4059 if (LI->getAlignment())
4060 Out << ", align " << LI->getAlignment();
4061 } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
4062 if (SI->isAtomic())
4063 writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
4064 if (SI->getAlignment())
4065 Out << ", align " << SI->getAlignment();
4066 } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
4067 writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
4068 CXI->getFailureOrdering(), CXI->getSyncScopeID());
4069 } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
4070 writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
4071 RMWI->getSyncScopeID());
4072 } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
4073 writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
4076 // Print Metadata info.
4077 SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
4078 I.getAllMetadata(InstMD);
4079 printMetadataAttachments(InstMD, ", ");
4081 // Print a nice comment.
4082 printInfoComment(I);
4085 void AssemblyWriter::printMetadataAttachments(
4086 const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
4087 StringRef Separator) {
4088 if (MDs.empty())
4089 return;
4091 if (MDNames.empty())
4092 MDs[0].second->getContext().getMDKindNames(MDNames);
4094 for (const auto &I : MDs) {
4095 unsigned Kind = I.first;
4096 Out << Separator;
4097 if (Kind < MDNames.size()) {
4098 Out << "!";
4099 printMetadataIdentifier(MDNames[Kind], Out);
4100 } else
4101 Out << "!<unknown kind #" << Kind << ">";
4102 Out << ' ';
4103 WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
4107 void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
4108 Out << '!' << Slot << " = ";
4109 printMDNodeBody(Node);
4110 Out << "\n";
4113 void AssemblyWriter::writeAllMDNodes() {
4114 SmallVector<const MDNode *, 16> Nodes;
4115 Nodes.resize(Machine.mdn_size());
4116 for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
4117 I != E; ++I)
4118 Nodes[I->second] = cast<MDNode>(I->first);
4120 for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
4121 writeMDNode(i, Nodes[i]);
4125 void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
4126 WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
4129 void AssemblyWriter::writeAllAttributeGroups() {
4130 std::vector<std::pair<AttributeSet, unsigned>> asVec;
4131 asVec.resize(Machine.as_size());
4133 for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
4134 I != E; ++I)
4135 asVec[I->second] = *I;
4137 for (const auto &I : asVec)
4138 Out << "attributes #" << I.second << " = { "
4139 << I.first.getAsString(true) << " }\n";
4142 void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
4143 bool IsInFunction = Machine.getFunction();
4144 if (IsInFunction)
4145 Out << " ";
4147 Out << "uselistorder";
4148 if (const BasicBlock *BB =
4149 IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
4150 Out << "_bb ";
4151 writeOperand(BB->getParent(), false);
4152 Out << ", ";
4153 writeOperand(BB, false);
4154 } else {
4155 Out << " ";
4156 writeOperand(Order.V, true);
4158 Out << ", { ";
4160 assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
4161 Out << Order.Shuffle[0];
4162 for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
4163 Out << ", " << Order.Shuffle[I];
4164 Out << " }\n";
4167 void AssemblyWriter::printUseLists(const Function *F) {
4168 auto hasMore =
4169 [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
4170 if (!hasMore())
4171 // Nothing to do.
4172 return;
4174 Out << "\n; uselistorder directives\n";
4175 while (hasMore()) {
4176 printUseListOrder(UseListOrders.back());
4177 UseListOrders.pop_back();
4181 //===----------------------------------------------------------------------===//
4182 // External Interface declarations
4183 //===----------------------------------------------------------------------===//
4185 void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4186 bool ShouldPreserveUseListOrder,
4187 bool IsForDebug) const {
4188 SlotTracker SlotTable(this->getParent());
4189 formatted_raw_ostream OS(ROS);
4190 AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
4191 IsForDebug,
4192 ShouldPreserveUseListOrder);
4193 W.printFunction(this);
4196 void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
4197 bool ShouldPreserveUseListOrder, bool IsForDebug) const {
4198 SlotTracker SlotTable(this);
4199 formatted_raw_ostream OS(ROS);
4200 AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
4201 ShouldPreserveUseListOrder);
4202 W.printModule(this);
4205 void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
4206 SlotTracker SlotTable(getParent());
4207 formatted_raw_ostream OS(ROS);
4208 AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
4209 W.printNamedMDNode(this);
4212 void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4213 bool IsForDebug) const {
4214 Optional<SlotTracker> LocalST;
4215 SlotTracker *SlotTable;
4216 if (auto *ST = MST.getMachine())
4217 SlotTable = ST;
4218 else {
4219 LocalST.emplace(getParent());
4220 SlotTable = &*LocalST;
4223 formatted_raw_ostream OS(ROS);
4224 AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
4225 W.printNamedMDNode(this);
4228 void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
4229 PrintLLVMName(ROS, getName(), ComdatPrefix);
4230 ROS << " = comdat ";
4232 switch (getSelectionKind()) {
4233 case Comdat::Any:
4234 ROS << "any";
4235 break;
4236 case Comdat::ExactMatch:
4237 ROS << "exactmatch";
4238 break;
4239 case Comdat::Largest:
4240 ROS << "largest";
4241 break;
4242 case Comdat::NoDuplicates:
4243 ROS << "noduplicates";
4244 break;
4245 case Comdat::SameSize:
4246 ROS << "samesize";
4247 break;
4250 ROS << '\n';
4253 void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
4254 TypePrinting TP;
4255 TP.print(const_cast<Type*>(this), OS);
4257 if (NoDetails)
4258 return;
4260 // If the type is a named struct type, print the body as well.
4261 if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
4262 if (!STy->isLiteral()) {
4263 OS << " = type ";
4264 TP.printStructBody(STy, OS);
4268 static bool isReferencingMDNode(const Instruction &I) {
4269 if (const auto *CI = dyn_cast<CallInst>(&I))
4270 if (Function *F = CI->getCalledFunction())
4271 if (F->isIntrinsic())
4272 for (auto &Op : I.operands())
4273 if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
4274 if (isa<MDNode>(V->getMetadata()))
4275 return true;
4276 return false;
4279 void Value::print(raw_ostream &ROS, bool IsForDebug) const {
4280 bool ShouldInitializeAllMetadata = false;
4281 if (auto *I = dyn_cast<Instruction>(this))
4282 ShouldInitializeAllMetadata = isReferencingMDNode(*I);
4283 else if (isa<Function>(this) || isa<MetadataAsValue>(this))
4284 ShouldInitializeAllMetadata = true;
4286 ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
4287 print(ROS, MST, IsForDebug);
4290 void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
4291 bool IsForDebug) const {
4292 formatted_raw_ostream OS(ROS);
4293 SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
4294 SlotTracker &SlotTable =
4295 MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
4296 auto incorporateFunction = [&](const Function *F) {
4297 if (F)
4298 MST.incorporateFunction(*F);
4301 if (const Instruction *I = dyn_cast<Instruction>(this)) {
4302 incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
4303 AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
4304 W.printInstruction(*I);
4305 } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
4306 incorporateFunction(BB->getParent());
4307 AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
4308 W.printBasicBlock(BB);
4309 } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
4310 AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
4311 if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
4312 W.printGlobal(V);
4313 else if (const Function *F = dyn_cast<Function>(GV))
4314 W.printFunction(F);
4315 else
4316 W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
4317 } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
4318 V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
4319 } else if (const Constant *C = dyn_cast<Constant>(this)) {
4320 TypePrinting TypePrinter;
4321 TypePrinter.print(C->getType(), OS);
4322 OS << ' ';
4323 WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
4324 } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
4325 this->printAsOperand(OS, /* PrintType */ true, MST);
4326 } else {
4327 llvm_unreachable("Unknown value to print out!");
4331 /// Print without a type, skipping the TypePrinting object.
4333 /// \return \c true iff printing was successful.
4334 static bool printWithoutType(const Value &V, raw_ostream &O,
4335 SlotTracker *Machine, const Module *M) {
4336 if (V.hasName() || isa<GlobalValue>(V) ||
4337 (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4338 WriteAsOperandInternal(O, &V, nullptr, Machine, M);
4339 return true;
4341 return false;
4344 static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
4345 ModuleSlotTracker &MST) {
4346 TypePrinting TypePrinter(MST.getModule());
4347 if (PrintType) {
4348 TypePrinter.print(V.getType(), O);
4349 O << ' ';
4352 WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
4353 MST.getModule());
4356 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4357 const Module *M) const {
4358 if (!M)
4359 M = getModuleFromVal(this);
4361 if (!PrintType)
4362 if (printWithoutType(*this, O, nullptr, M))
4363 return;
4365 SlotTracker Machine(
4366 M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
4367 ModuleSlotTracker MST(Machine, M);
4368 printAsOperandImpl(*this, O, PrintType, MST);
4371 void Value::printAsOperand(raw_ostream &O, bool PrintType,
4372 ModuleSlotTracker &MST) const {
4373 if (!PrintType)
4374 if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
4375 return;
4377 printAsOperandImpl(*this, O, PrintType, MST);
4380 static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
4381 ModuleSlotTracker &MST, const Module *M,
4382 bool OnlyAsOperand) {
4383 formatted_raw_ostream OS(ROS);
4385 TypePrinting TypePrinter(M);
4387 WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
4388 /* FromValue */ true);
4390 auto *N = dyn_cast<MDNode>(&MD);
4391 if (OnlyAsOperand || !N || isa<DIExpression>(MD))
4392 return;
4394 OS << " = ";
4395 WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
4398 void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
4399 ModuleSlotTracker MST(M, isa<MDNode>(this));
4400 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4403 void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
4404 const Module *M) const {
4405 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
4408 void Metadata::print(raw_ostream &OS, const Module *M,
4409 bool /*IsForDebug*/) const {
4410 ModuleSlotTracker MST(M, isa<MDNode>(this));
4411 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4414 void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
4415 const Module *M, bool /*IsForDebug*/) const {
4416 printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
4419 void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
4420 SlotTracker SlotTable(this);
4421 formatted_raw_ostream OS(ROS);
4422 AssemblyWriter W(OS, SlotTable, this, IsForDebug);
4423 W.printModuleSummaryIndex();
4426 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4427 // Value::dump - allow easy printing of Values from the debugger.
4428 LLVM_DUMP_METHOD
4429 void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4431 // Type::dump - allow easy printing of Types from the debugger.
4432 LLVM_DUMP_METHOD
4433 void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
4435 // Module::dump() - Allow printing of Modules from the debugger.
4436 LLVM_DUMP_METHOD
4437 void Module::dump() const {
4438 print(dbgs(), nullptr,
4439 /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
4442 // Allow printing of Comdats from the debugger.
4443 LLVM_DUMP_METHOD
4444 void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4446 // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
4447 LLVM_DUMP_METHOD
4448 void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4450 LLVM_DUMP_METHOD
4451 void Metadata::dump() const { dump(nullptr); }
4453 LLVM_DUMP_METHOD
4454 void Metadata::dump(const Module *M) const {
4455 print(dbgs(), M, /*IsForDebug=*/true);
4456 dbgs() << '\n';
4459 // Allow printing of ModuleSummaryIndex from the debugger.
4460 LLVM_DUMP_METHOD
4461 void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
4462 #endif