xref: /freebsd/contrib/llvm-project/llvm/lib/IR/AsmWriter.cpp (revision 349cc55c9796c4596a5b9904cd3281af295f878f)
10b57cec5SDimitry Andric //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This library implements `print` family of functions in classes like
100b57cec5SDimitry Andric // Module, Function, Value, etc. In-memory representation of those classes is
110b57cec5SDimitry Andric // converted to IR strings.
120b57cec5SDimitry Andric //
130b57cec5SDimitry Andric // Note that these routines must be extremely tolerant of various errors in the
140b57cec5SDimitry Andric // LLVM code, because it can be used for debugging transformations.
150b57cec5SDimitry Andric //
160b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
170b57cec5SDimitry Andric 
180b57cec5SDimitry Andric #include "llvm/ADT/APFloat.h"
190b57cec5SDimitry Andric #include "llvm/ADT/APInt.h"
200b57cec5SDimitry Andric #include "llvm/ADT/ArrayRef.h"
210b57cec5SDimitry Andric #include "llvm/ADT/DenseMap.h"
220b57cec5SDimitry Andric #include "llvm/ADT/None.h"
230b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
240b57cec5SDimitry Andric #include "llvm/ADT/STLExtras.h"
250b57cec5SDimitry Andric #include "llvm/ADT/SetVector.h"
26*349cc55cSDimitry Andric #include "llvm/ADT/SmallPtrSet.h"
270b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h"
290b57cec5SDimitry Andric #include "llvm/ADT/StringExtras.h"
300b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h"
310b57cec5SDimitry Andric #include "llvm/ADT/iterator_range.h"
320b57cec5SDimitry Andric #include "llvm/BinaryFormat/Dwarf.h"
330b57cec5SDimitry Andric #include "llvm/Config/llvm-config.h"
340b57cec5SDimitry Andric #include "llvm/IR/Argument.h"
350b57cec5SDimitry Andric #include "llvm/IR/AssemblyAnnotationWriter.h"
360b57cec5SDimitry Andric #include "llvm/IR/Attributes.h"
370b57cec5SDimitry Andric #include "llvm/IR/BasicBlock.h"
380b57cec5SDimitry Andric #include "llvm/IR/CFG.h"
390b57cec5SDimitry Andric #include "llvm/IR/CallingConv.h"
400b57cec5SDimitry Andric #include "llvm/IR/Comdat.h"
410b57cec5SDimitry Andric #include "llvm/IR/Constant.h"
420b57cec5SDimitry Andric #include "llvm/IR/Constants.h"
430b57cec5SDimitry Andric #include "llvm/IR/DebugInfoMetadata.h"
440b57cec5SDimitry Andric #include "llvm/IR/DerivedTypes.h"
450b57cec5SDimitry Andric #include "llvm/IR/Function.h"
460b57cec5SDimitry Andric #include "llvm/IR/GlobalAlias.h"
470b57cec5SDimitry Andric #include "llvm/IR/GlobalIFunc.h"
480b57cec5SDimitry Andric #include "llvm/IR/GlobalObject.h"
490b57cec5SDimitry Andric #include "llvm/IR/GlobalValue.h"
500b57cec5SDimitry Andric #include "llvm/IR/GlobalVariable.h"
510b57cec5SDimitry Andric #include "llvm/IR/IRPrintingPasses.h"
520b57cec5SDimitry Andric #include "llvm/IR/InlineAsm.h"
530b57cec5SDimitry Andric #include "llvm/IR/InstrTypes.h"
540b57cec5SDimitry Andric #include "llvm/IR/Instruction.h"
550b57cec5SDimitry Andric #include "llvm/IR/Instructions.h"
56fe6060f1SDimitry Andric #include "llvm/IR/IntrinsicInst.h"
570b57cec5SDimitry Andric #include "llvm/IR/LLVMContext.h"
580b57cec5SDimitry Andric #include "llvm/IR/Metadata.h"
590b57cec5SDimitry Andric #include "llvm/IR/Module.h"
600b57cec5SDimitry Andric #include "llvm/IR/ModuleSlotTracker.h"
610b57cec5SDimitry Andric #include "llvm/IR/ModuleSummaryIndex.h"
620b57cec5SDimitry Andric #include "llvm/IR/Operator.h"
630b57cec5SDimitry Andric #include "llvm/IR/Type.h"
640b57cec5SDimitry Andric #include "llvm/IR/TypeFinder.h"
650b57cec5SDimitry Andric #include "llvm/IR/Use.h"
660b57cec5SDimitry Andric #include "llvm/IR/User.h"
670b57cec5SDimitry Andric #include "llvm/IR/Value.h"
680b57cec5SDimitry Andric #include "llvm/Support/AtomicOrdering.h"
690b57cec5SDimitry Andric #include "llvm/Support/Casting.h"
700b57cec5SDimitry Andric #include "llvm/Support/Compiler.h"
710b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
720b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
730b57cec5SDimitry Andric #include "llvm/Support/Format.h"
740b57cec5SDimitry Andric #include "llvm/Support/FormattedStream.h"
75*349cc55cSDimitry Andric #include "llvm/Support/SaveAndRestore.h"
760b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
770b57cec5SDimitry Andric #include <algorithm>
780b57cec5SDimitry Andric #include <cassert>
790b57cec5SDimitry Andric #include <cctype>
800b57cec5SDimitry Andric #include <cstddef>
810b57cec5SDimitry Andric #include <cstdint>
820b57cec5SDimitry Andric #include <iterator>
830b57cec5SDimitry Andric #include <memory>
840b57cec5SDimitry Andric #include <string>
850b57cec5SDimitry Andric #include <tuple>
860b57cec5SDimitry Andric #include <utility>
870b57cec5SDimitry Andric #include <vector>
880b57cec5SDimitry Andric 
890b57cec5SDimitry Andric using namespace llvm;
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric // Make virtual table appear in this compilation unit.
920b57cec5SDimitry Andric AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
950b57cec5SDimitry Andric // Helper Functions
960b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
970b57cec5SDimitry Andric 
98fe6060f1SDimitry Andric using OrderMap = MapVector<const Value *, unsigned>;
990b57cec5SDimitry Andric 
100fe6060f1SDimitry Andric using UseListOrderMap =
101fe6060f1SDimitry Andric     DenseMap<const Function *, MapVector<const Value *, std::vector<unsigned>>>;
1020b57cec5SDimitry Andric 
103e8d8bef9SDimitry Andric /// Look for a value that might be wrapped as metadata, e.g. a value in a
104e8d8bef9SDimitry Andric /// metadata operand. Returns the input value as-is if it is not wrapped.
105e8d8bef9SDimitry Andric static const Value *skipMetadataWrapper(const Value *V) {
106e8d8bef9SDimitry Andric   if (const auto *MAV = dyn_cast<MetadataAsValue>(V))
107e8d8bef9SDimitry Andric     if (const auto *VAM = dyn_cast<ValueAsMetadata>(MAV->getMetadata()))
108e8d8bef9SDimitry Andric       return VAM->getValue();
109e8d8bef9SDimitry Andric   return V;
110e8d8bef9SDimitry Andric }
111e8d8bef9SDimitry Andric 
1120b57cec5SDimitry Andric static void orderValue(const Value *V, OrderMap &OM) {
113fe6060f1SDimitry Andric   if (OM.lookup(V))
1140b57cec5SDimitry Andric     return;
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric   if (const Constant *C = dyn_cast<Constant>(V))
1170b57cec5SDimitry Andric     if (C->getNumOperands() && !isa<GlobalValue>(C))
1180b57cec5SDimitry Andric       for (const Value *Op : C->operands())
1190b57cec5SDimitry Andric         if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
1200b57cec5SDimitry Andric           orderValue(Op, OM);
1210b57cec5SDimitry Andric 
1220b57cec5SDimitry Andric   // Note: we cannot cache this lookup above, since inserting into the map
1230b57cec5SDimitry Andric   // changes the map's size, and thus affects the other IDs.
124fe6060f1SDimitry Andric   unsigned ID = OM.size() + 1;
125fe6060f1SDimitry Andric   OM[V] = ID;
1260b57cec5SDimitry Andric }
1270b57cec5SDimitry Andric 
1280b57cec5SDimitry Andric static OrderMap orderModule(const Module *M) {
1290b57cec5SDimitry Andric   OrderMap OM;
1300b57cec5SDimitry Andric 
1310b57cec5SDimitry Andric   for (const GlobalVariable &G : M->globals()) {
1320b57cec5SDimitry Andric     if (G.hasInitializer())
1330b57cec5SDimitry Andric       if (!isa<GlobalValue>(G.getInitializer()))
1340b57cec5SDimitry Andric         orderValue(G.getInitializer(), OM);
1350b57cec5SDimitry Andric     orderValue(&G, OM);
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric   for (const GlobalAlias &A : M->aliases()) {
1380b57cec5SDimitry Andric     if (!isa<GlobalValue>(A.getAliasee()))
1390b57cec5SDimitry Andric       orderValue(A.getAliasee(), OM);
1400b57cec5SDimitry Andric     orderValue(&A, OM);
1410b57cec5SDimitry Andric   }
1420b57cec5SDimitry Andric   for (const GlobalIFunc &I : M->ifuncs()) {
1430b57cec5SDimitry Andric     if (!isa<GlobalValue>(I.getResolver()))
1440b57cec5SDimitry Andric       orderValue(I.getResolver(), OM);
1450b57cec5SDimitry Andric     orderValue(&I, OM);
1460b57cec5SDimitry Andric   }
1470b57cec5SDimitry Andric   for (const Function &F : *M) {
1480b57cec5SDimitry Andric     for (const Use &U : F.operands())
1490b57cec5SDimitry Andric       if (!isa<GlobalValue>(U.get()))
1500b57cec5SDimitry Andric         orderValue(U.get(), OM);
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric     orderValue(&F, OM);
1530b57cec5SDimitry Andric 
1540b57cec5SDimitry Andric     if (F.isDeclaration())
1550b57cec5SDimitry Andric       continue;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     for (const Argument &A : F.args())
1580b57cec5SDimitry Andric       orderValue(&A, OM);
1590b57cec5SDimitry Andric     for (const BasicBlock &BB : F) {
1600b57cec5SDimitry Andric       orderValue(&BB, OM);
1610b57cec5SDimitry Andric       for (const Instruction &I : BB) {
162e8d8bef9SDimitry Andric         for (const Value *Op : I.operands()) {
163e8d8bef9SDimitry Andric           Op = skipMetadataWrapper(Op);
1640b57cec5SDimitry Andric           if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
1650b57cec5SDimitry Andric               isa<InlineAsm>(*Op))
1660b57cec5SDimitry Andric             orderValue(Op, OM);
167e8d8bef9SDimitry Andric         }
1680b57cec5SDimitry Andric         orderValue(&I, OM);
1690b57cec5SDimitry Andric       }
1700b57cec5SDimitry Andric     }
1710b57cec5SDimitry Andric   }
1720b57cec5SDimitry Andric   return OM;
1730b57cec5SDimitry Andric }
1740b57cec5SDimitry Andric 
175fe6060f1SDimitry Andric static std::vector<unsigned>
176fe6060f1SDimitry Andric predictValueUseListOrder(const Value *V, unsigned ID, const OrderMap &OM) {
1770b57cec5SDimitry Andric   // Predict use-list order for this one.
1780b57cec5SDimitry Andric   using Entry = std::pair<const Use *, unsigned>;
1790b57cec5SDimitry Andric   SmallVector<Entry, 64> List;
1800b57cec5SDimitry Andric   for (const Use &U : V->uses())
1810b57cec5SDimitry Andric     // Check if this user will be serialized.
182fe6060f1SDimitry Andric     if (OM.lookup(U.getUser()))
1830b57cec5SDimitry Andric       List.push_back(std::make_pair(&U, List.size()));
1840b57cec5SDimitry Andric 
1850b57cec5SDimitry Andric   if (List.size() < 2)
1860b57cec5SDimitry Andric     // We may have lost some users.
187fe6060f1SDimitry Andric     return {};
1880b57cec5SDimitry Andric 
189fe6060f1SDimitry Andric   // When referencing a value before its declaration, a temporary value is
190fe6060f1SDimitry Andric   // created, which will later be RAUWed with the actual value. This reverses
191fe6060f1SDimitry Andric   // the use list. This happens for all values apart from basic blocks.
192fe6060f1SDimitry Andric   bool GetsReversed = !isa<BasicBlock>(V);
1930b57cec5SDimitry Andric   if (auto *BA = dyn_cast<BlockAddress>(V))
194fe6060f1SDimitry Andric     ID = OM.lookup(BA->getBasicBlock());
1950b57cec5SDimitry Andric   llvm::sort(List, [&](const Entry &L, const Entry &R) {
1960b57cec5SDimitry Andric     const Use *LU = L.first;
1970b57cec5SDimitry Andric     const Use *RU = R.first;
1980b57cec5SDimitry Andric     if (LU == RU)
1990b57cec5SDimitry Andric       return false;
2000b57cec5SDimitry Andric 
201fe6060f1SDimitry Andric     auto LID = OM.lookup(LU->getUser());
202fe6060f1SDimitry Andric     auto RID = OM.lookup(RU->getUser());
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric     // If ID is 4, then expect: 7 6 5 1 2 3.
2050b57cec5SDimitry Andric     if (LID < RID) {
2060b57cec5SDimitry Andric       if (GetsReversed)
2070b57cec5SDimitry Andric         if (RID <= ID)
2080b57cec5SDimitry Andric           return true;
2090b57cec5SDimitry Andric       return false;
2100b57cec5SDimitry Andric     }
2110b57cec5SDimitry Andric     if (RID < LID) {
2120b57cec5SDimitry Andric       if (GetsReversed)
2130b57cec5SDimitry Andric         if (LID <= ID)
2140b57cec5SDimitry Andric           return false;
2150b57cec5SDimitry Andric       return true;
2160b57cec5SDimitry Andric     }
2170b57cec5SDimitry Andric 
2180b57cec5SDimitry Andric     // LID and RID are equal, so we have different operands of the same user.
2190b57cec5SDimitry Andric     // Assume operands are added in order for all instructions.
2200b57cec5SDimitry Andric     if (GetsReversed)
2210b57cec5SDimitry Andric       if (LID <= ID)
2220b57cec5SDimitry Andric         return LU->getOperandNo() < RU->getOperandNo();
2230b57cec5SDimitry Andric     return LU->getOperandNo() > RU->getOperandNo();
2240b57cec5SDimitry Andric   });
2250b57cec5SDimitry Andric 
2265ffd83dbSDimitry Andric   if (llvm::is_sorted(List, [](const Entry &L, const Entry &R) {
2275ffd83dbSDimitry Andric         return L.second < R.second;
2285ffd83dbSDimitry Andric       }))
2290b57cec5SDimitry Andric     // Order is already correct.
230fe6060f1SDimitry Andric     return {};
2310b57cec5SDimitry Andric 
2320b57cec5SDimitry Andric   // Store the shuffle.
233fe6060f1SDimitry Andric   std::vector<unsigned> Shuffle(List.size());
2340b57cec5SDimitry Andric   for (size_t I = 0, E = List.size(); I != E; ++I)
235fe6060f1SDimitry Andric     Shuffle[I] = List[I].second;
236fe6060f1SDimitry Andric   return Shuffle;
2370b57cec5SDimitry Andric }
2380b57cec5SDimitry Andric 
239fe6060f1SDimitry Andric static UseListOrderMap predictUseListOrder(const Module *M) {
2400b57cec5SDimitry Andric   OrderMap OM = orderModule(M);
241fe6060f1SDimitry Andric   UseListOrderMap ULOM;
242fe6060f1SDimitry Andric   for (const auto &Pair : OM) {
243fe6060f1SDimitry Andric     const Value *V = Pair.first;
244fe6060f1SDimitry Andric     if (V->use_empty() || std::next(V->use_begin()) == V->use_end())
2450b57cec5SDimitry Andric       continue;
2460b57cec5SDimitry Andric 
247fe6060f1SDimitry Andric     std::vector<unsigned> Shuffle =
248fe6060f1SDimitry Andric         predictValueUseListOrder(V, Pair.second, OM);
249fe6060f1SDimitry Andric     if (Shuffle.empty())
250fe6060f1SDimitry Andric       continue;
2510b57cec5SDimitry Andric 
252fe6060f1SDimitry Andric     const Function *F = nullptr;
253fe6060f1SDimitry Andric     if (auto *I = dyn_cast<Instruction>(V))
254fe6060f1SDimitry Andric       F = I->getFunction();
255fe6060f1SDimitry Andric     if (auto *A = dyn_cast<Argument>(V))
256fe6060f1SDimitry Andric       F = A->getParent();
257fe6060f1SDimitry Andric     if (auto *BB = dyn_cast<BasicBlock>(V))
258fe6060f1SDimitry Andric       F = BB->getParent();
259fe6060f1SDimitry Andric     ULOM[F][V] = std::move(Shuffle);
260fe6060f1SDimitry Andric   }
261fe6060f1SDimitry Andric   return ULOM;
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric static const Module *getModuleFromVal(const Value *V) {
2650b57cec5SDimitry Andric   if (const Argument *MA = dyn_cast<Argument>(V))
2660b57cec5SDimitry Andric     return MA->getParent() ? MA->getParent()->getParent() : nullptr;
2670b57cec5SDimitry Andric 
2680b57cec5SDimitry Andric   if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
2690b57cec5SDimitry Andric     return BB->getParent() ? BB->getParent()->getParent() : nullptr;
2700b57cec5SDimitry Andric 
2710b57cec5SDimitry Andric   if (const Instruction *I = dyn_cast<Instruction>(V)) {
2720b57cec5SDimitry Andric     const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
2730b57cec5SDimitry Andric     return M ? M->getParent() : nullptr;
2740b57cec5SDimitry Andric   }
2750b57cec5SDimitry Andric 
2760b57cec5SDimitry Andric   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
2770b57cec5SDimitry Andric     return GV->getParent();
2780b57cec5SDimitry Andric 
2790b57cec5SDimitry Andric   if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
2800b57cec5SDimitry Andric     for (const User *U : MAV->users())
2810b57cec5SDimitry Andric       if (isa<Instruction>(U))
2820b57cec5SDimitry Andric         if (const Module *M = getModuleFromVal(U))
2830b57cec5SDimitry Andric           return M;
2840b57cec5SDimitry Andric     return nullptr;
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   return nullptr;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
2900b57cec5SDimitry Andric static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
2910b57cec5SDimitry Andric   switch (cc) {
2920b57cec5SDimitry Andric   default:                         Out << "cc" << cc; break;
2930b57cec5SDimitry Andric   case CallingConv::Fast:          Out << "fastcc"; break;
2940b57cec5SDimitry Andric   case CallingConv::Cold:          Out << "coldcc"; break;
2950b57cec5SDimitry Andric   case CallingConv::WebKit_JS:     Out << "webkit_jscc"; break;
2960b57cec5SDimitry Andric   case CallingConv::AnyReg:        Out << "anyregcc"; break;
2970b57cec5SDimitry Andric   case CallingConv::PreserveMost:  Out << "preserve_mostcc"; break;
2980b57cec5SDimitry Andric   case CallingConv::PreserveAll:   Out << "preserve_allcc"; break;
2990b57cec5SDimitry Andric   case CallingConv::CXX_FAST_TLS:  Out << "cxx_fast_tlscc"; break;
3000b57cec5SDimitry Andric   case CallingConv::GHC:           Out << "ghccc"; break;
3018bcb0991SDimitry Andric   case CallingConv::Tail:          Out << "tailcc"; break;
302480093f4SDimitry Andric   case CallingConv::CFGuard_Check: Out << "cfguard_checkcc"; break;
3030b57cec5SDimitry Andric   case CallingConv::X86_StdCall:   Out << "x86_stdcallcc"; break;
3040b57cec5SDimitry Andric   case CallingConv::X86_FastCall:  Out << "x86_fastcallcc"; break;
3050b57cec5SDimitry Andric   case CallingConv::X86_ThisCall:  Out << "x86_thiscallcc"; break;
3060b57cec5SDimitry Andric   case CallingConv::X86_RegCall:   Out << "x86_regcallcc"; break;
3070b57cec5SDimitry Andric   case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
3080b57cec5SDimitry Andric   case CallingConv::Intel_OCL_BI:  Out << "intel_ocl_bicc"; break;
3090b57cec5SDimitry Andric   case CallingConv::ARM_APCS:      Out << "arm_apcscc"; break;
3100b57cec5SDimitry Andric   case CallingConv::ARM_AAPCS:     Out << "arm_aapcscc"; break;
3110b57cec5SDimitry Andric   case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
3120b57cec5SDimitry Andric   case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break;
313480093f4SDimitry Andric   case CallingConv::AArch64_SVE_VectorCall:
314480093f4SDimitry Andric     Out << "aarch64_sve_vector_pcs";
315480093f4SDimitry Andric     break;
3160b57cec5SDimitry Andric   case CallingConv::MSP430_INTR:   Out << "msp430_intrcc"; break;
3170b57cec5SDimitry Andric   case CallingConv::AVR_INTR:      Out << "avr_intrcc "; break;
3180b57cec5SDimitry Andric   case CallingConv::AVR_SIGNAL:    Out << "avr_signalcc "; break;
3190b57cec5SDimitry Andric   case CallingConv::PTX_Kernel:    Out << "ptx_kernel"; break;
3200b57cec5SDimitry Andric   case CallingConv::PTX_Device:    Out << "ptx_device"; break;
3210b57cec5SDimitry Andric   case CallingConv::X86_64_SysV:   Out << "x86_64_sysvcc"; break;
3220b57cec5SDimitry Andric   case CallingConv::Win64:         Out << "win64cc"; break;
3230b57cec5SDimitry Andric   case CallingConv::SPIR_FUNC:     Out << "spir_func"; break;
3240b57cec5SDimitry Andric   case CallingConv::SPIR_KERNEL:   Out << "spir_kernel"; break;
3250b57cec5SDimitry Andric   case CallingConv::Swift:         Out << "swiftcc"; break;
326fe6060f1SDimitry Andric   case CallingConv::SwiftTail:     Out << "swifttailcc"; break;
3270b57cec5SDimitry Andric   case CallingConv::X86_INTR:      Out << "x86_intrcc"; break;
3280b57cec5SDimitry Andric   case CallingConv::HHVM:          Out << "hhvmcc"; break;
3290b57cec5SDimitry Andric   case CallingConv::HHVM_C:        Out << "hhvm_ccc"; break;
3300b57cec5SDimitry Andric   case CallingConv::AMDGPU_VS:     Out << "amdgpu_vs"; break;
3310b57cec5SDimitry Andric   case CallingConv::AMDGPU_LS:     Out << "amdgpu_ls"; break;
3320b57cec5SDimitry Andric   case CallingConv::AMDGPU_HS:     Out << "amdgpu_hs"; break;
3330b57cec5SDimitry Andric   case CallingConv::AMDGPU_ES:     Out << "amdgpu_es"; break;
3340b57cec5SDimitry Andric   case CallingConv::AMDGPU_GS:     Out << "amdgpu_gs"; break;
3350b57cec5SDimitry Andric   case CallingConv::AMDGPU_PS:     Out << "amdgpu_ps"; break;
3360b57cec5SDimitry Andric   case CallingConv::AMDGPU_CS:     Out << "amdgpu_cs"; break;
3370b57cec5SDimitry Andric   case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
338e8d8bef9SDimitry Andric   case CallingConv::AMDGPU_Gfx:    Out << "amdgpu_gfx"; break;
3390b57cec5SDimitry Andric   }
3400b57cec5SDimitry Andric }
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric enum PrefixType {
3430b57cec5SDimitry Andric   GlobalPrefix,
3440b57cec5SDimitry Andric   ComdatPrefix,
3450b57cec5SDimitry Andric   LabelPrefix,
3460b57cec5SDimitry Andric   LocalPrefix,
3470b57cec5SDimitry Andric   NoPrefix
3480b57cec5SDimitry Andric };
3490b57cec5SDimitry Andric 
3500b57cec5SDimitry Andric void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
3510b57cec5SDimitry Andric   assert(!Name.empty() && "Cannot get empty name!");
3520b57cec5SDimitry Andric 
3530b57cec5SDimitry Andric   // Scan the name to see if it needs quotes first.
3540b57cec5SDimitry Andric   bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
3550b57cec5SDimitry Andric   if (!NeedsQuotes) {
3560b57cec5SDimitry Andric     for (unsigned i = 0, e = Name.size(); i != e; ++i) {
3570b57cec5SDimitry Andric       // By making this unsigned, the value passed in to isalnum will always be
3580b57cec5SDimitry Andric       // in the range 0-255.  This is important when building with MSVC because
3590b57cec5SDimitry Andric       // its implementation will assert.  This situation can arise when dealing
3600b57cec5SDimitry Andric       // with UTF-8 multibyte characters.
3610b57cec5SDimitry Andric       unsigned char C = Name[i];
3620b57cec5SDimitry Andric       if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
3630b57cec5SDimitry Andric           C != '_') {
3640b57cec5SDimitry Andric         NeedsQuotes = true;
3650b57cec5SDimitry Andric         break;
3660b57cec5SDimitry Andric       }
3670b57cec5SDimitry Andric     }
3680b57cec5SDimitry Andric   }
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   // If we didn't need any quotes, just write out the name in one blast.
3710b57cec5SDimitry Andric   if (!NeedsQuotes) {
3720b57cec5SDimitry Andric     OS << Name;
3730b57cec5SDimitry Andric     return;
3740b57cec5SDimitry Andric   }
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   // Okay, we need quotes.  Output the quotes and escape any scary characters as
3770b57cec5SDimitry Andric   // needed.
3780b57cec5SDimitry Andric   OS << '"';
3790b57cec5SDimitry Andric   printEscapedString(Name, OS);
3800b57cec5SDimitry Andric   OS << '"';
3810b57cec5SDimitry Andric }
3820b57cec5SDimitry Andric 
3830b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with %
3840b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s
3850b57cec5SDimitry Andric /// (if it has special chars in it). Print it out.
3860b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
3870b57cec5SDimitry Andric   switch (Prefix) {
3880b57cec5SDimitry Andric   case NoPrefix:
3890b57cec5SDimitry Andric     break;
3900b57cec5SDimitry Andric   case GlobalPrefix:
3910b57cec5SDimitry Andric     OS << '@';
3920b57cec5SDimitry Andric     break;
3930b57cec5SDimitry Andric   case ComdatPrefix:
3940b57cec5SDimitry Andric     OS << '$';
3950b57cec5SDimitry Andric     break;
3960b57cec5SDimitry Andric   case LabelPrefix:
3970b57cec5SDimitry Andric     break;
3980b57cec5SDimitry Andric   case LocalPrefix:
3990b57cec5SDimitry Andric     OS << '%';
4000b57cec5SDimitry Andric     break;
4010b57cec5SDimitry Andric   }
4020b57cec5SDimitry Andric   printLLVMNameWithoutPrefix(OS, Name);
4030b57cec5SDimitry Andric }
4040b57cec5SDimitry Andric 
4050b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with %
4060b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s
4070b57cec5SDimitry Andric /// (if it has special chars in it). Print it out.
4080b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, const Value *V) {
4090b57cec5SDimitry Andric   PrintLLVMName(OS, V->getName(),
4100b57cec5SDimitry Andric                 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
4110b57cec5SDimitry Andric }
4120b57cec5SDimitry Andric 
4135ffd83dbSDimitry Andric static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) {
4145ffd83dbSDimitry Andric   Out << ", <";
4155ffd83dbSDimitry Andric   if (isa<ScalableVectorType>(Ty))
4165ffd83dbSDimitry Andric     Out << "vscale x ";
4175ffd83dbSDimitry Andric   Out << Mask.size() << " x i32> ";
4185ffd83dbSDimitry Andric   bool FirstElt = true;
4195ffd83dbSDimitry Andric   if (all_of(Mask, [](int Elt) { return Elt == 0; })) {
4205ffd83dbSDimitry Andric     Out << "zeroinitializer";
4215ffd83dbSDimitry Andric   } else if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) {
4225ffd83dbSDimitry Andric     Out << "undef";
4235ffd83dbSDimitry Andric   } else {
4245ffd83dbSDimitry Andric     Out << "<";
4255ffd83dbSDimitry Andric     for (int Elt : Mask) {
4265ffd83dbSDimitry Andric       if (FirstElt)
4275ffd83dbSDimitry Andric         FirstElt = false;
4285ffd83dbSDimitry Andric       else
4295ffd83dbSDimitry Andric         Out << ", ";
4305ffd83dbSDimitry Andric       Out << "i32 ";
4315ffd83dbSDimitry Andric       if (Elt == UndefMaskElem)
4325ffd83dbSDimitry Andric         Out << "undef";
4335ffd83dbSDimitry Andric       else
4345ffd83dbSDimitry Andric         Out << Elt;
4355ffd83dbSDimitry Andric     }
4365ffd83dbSDimitry Andric     Out << ">";
4375ffd83dbSDimitry Andric   }
4385ffd83dbSDimitry Andric }
4395ffd83dbSDimitry Andric 
4400b57cec5SDimitry Andric namespace {
4410b57cec5SDimitry Andric 
4420b57cec5SDimitry Andric class TypePrinting {
4430b57cec5SDimitry Andric public:
4440b57cec5SDimitry Andric   TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric   TypePrinting(const TypePrinting &) = delete;
4470b57cec5SDimitry Andric   TypePrinting &operator=(const TypePrinting &) = delete;
4480b57cec5SDimitry Andric 
4490b57cec5SDimitry Andric   /// The named types that are used by the current module.
4500b57cec5SDimitry Andric   TypeFinder &getNamedTypes();
4510b57cec5SDimitry Andric 
4520b57cec5SDimitry Andric   /// The numbered types, number to type mapping.
4530b57cec5SDimitry Andric   std::vector<StructType *> &getNumberedTypes();
4540b57cec5SDimitry Andric 
4550b57cec5SDimitry Andric   bool empty();
4560b57cec5SDimitry Andric 
4570b57cec5SDimitry Andric   void print(Type *Ty, raw_ostream &OS);
4580b57cec5SDimitry Andric 
4590b57cec5SDimitry Andric   void printStructBody(StructType *Ty, raw_ostream &OS);
4600b57cec5SDimitry Andric 
4610b57cec5SDimitry Andric private:
4620b57cec5SDimitry Andric   void incorporateTypes();
4630b57cec5SDimitry Andric 
4640b57cec5SDimitry Andric   /// A module to process lazily when needed. Set to nullptr as soon as used.
4650b57cec5SDimitry Andric   const Module *DeferredM;
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   TypeFinder NamedTypes;
4680b57cec5SDimitry Andric 
4690b57cec5SDimitry Andric   // The numbered types, along with their value.
4700b57cec5SDimitry Andric   DenseMap<StructType *, unsigned> Type2Number;
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   std::vector<StructType *> NumberedTypes;
4730b57cec5SDimitry Andric };
4740b57cec5SDimitry Andric 
4750b57cec5SDimitry Andric } // end anonymous namespace
4760b57cec5SDimitry Andric 
4770b57cec5SDimitry Andric TypeFinder &TypePrinting::getNamedTypes() {
4780b57cec5SDimitry Andric   incorporateTypes();
4790b57cec5SDimitry Andric   return NamedTypes;
4800b57cec5SDimitry Andric }
4810b57cec5SDimitry Andric 
4820b57cec5SDimitry Andric std::vector<StructType *> &TypePrinting::getNumberedTypes() {
4830b57cec5SDimitry Andric   incorporateTypes();
4840b57cec5SDimitry Andric 
4850b57cec5SDimitry Andric   // We know all the numbers that each type is used and we know that it is a
4860b57cec5SDimitry Andric   // dense assignment. Convert the map to an index table, if it's not done
4870b57cec5SDimitry Andric   // already (judging from the sizes):
4880b57cec5SDimitry Andric   if (NumberedTypes.size() == Type2Number.size())
4890b57cec5SDimitry Andric     return NumberedTypes;
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   NumberedTypes.resize(Type2Number.size());
4920b57cec5SDimitry Andric   for (const auto &P : Type2Number) {
4930b57cec5SDimitry Andric     assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
4940b57cec5SDimitry Andric     assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
4950b57cec5SDimitry Andric     NumberedTypes[P.second] = P.first;
4960b57cec5SDimitry Andric   }
4970b57cec5SDimitry Andric   return NumberedTypes;
4980b57cec5SDimitry Andric }
4990b57cec5SDimitry Andric 
5000b57cec5SDimitry Andric bool TypePrinting::empty() {
5010b57cec5SDimitry Andric   incorporateTypes();
5020b57cec5SDimitry Andric   return NamedTypes.empty() && Type2Number.empty();
5030b57cec5SDimitry Andric }
5040b57cec5SDimitry Andric 
5050b57cec5SDimitry Andric void TypePrinting::incorporateTypes() {
5060b57cec5SDimitry Andric   if (!DeferredM)
5070b57cec5SDimitry Andric     return;
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   NamedTypes.run(*DeferredM, false);
5100b57cec5SDimitry Andric   DeferredM = nullptr;
5110b57cec5SDimitry Andric 
5120b57cec5SDimitry Andric   // The list of struct types we got back includes all the struct types, split
5130b57cec5SDimitry Andric   // the unnamed ones out to a numbering and remove the anonymous structs.
5140b57cec5SDimitry Andric   unsigned NextNumber = 0;
5150b57cec5SDimitry Andric 
5160b57cec5SDimitry Andric   std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
5170b57cec5SDimitry Andric   for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
5180b57cec5SDimitry Andric     StructType *STy = *I;
5190b57cec5SDimitry Andric 
5200b57cec5SDimitry Andric     // Ignore anonymous types.
5210b57cec5SDimitry Andric     if (STy->isLiteral())
5220b57cec5SDimitry Andric       continue;
5230b57cec5SDimitry Andric 
5240b57cec5SDimitry Andric     if (STy->getName().empty())
5250b57cec5SDimitry Andric       Type2Number[STy] = NextNumber++;
5260b57cec5SDimitry Andric     else
5270b57cec5SDimitry Andric       *NextToUse++ = STy;
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   NamedTypes.erase(NextToUse, NamedTypes.end());
5310b57cec5SDimitry Andric }
5320b57cec5SDimitry Andric 
5330b57cec5SDimitry Andric /// Write the specified type to the specified raw_ostream, making use of type
5340b57cec5SDimitry Andric /// names or up references to shorten the type name where possible.
5350b57cec5SDimitry Andric void TypePrinting::print(Type *Ty, raw_ostream &OS) {
5360b57cec5SDimitry Andric   switch (Ty->getTypeID()) {
5370b57cec5SDimitry Andric   case Type::VoidTyID:      OS << "void"; return;
5380b57cec5SDimitry Andric   case Type::HalfTyID:      OS << "half"; return;
5395ffd83dbSDimitry Andric   case Type::BFloatTyID:    OS << "bfloat"; return;
5400b57cec5SDimitry Andric   case Type::FloatTyID:     OS << "float"; return;
5410b57cec5SDimitry Andric   case Type::DoubleTyID:    OS << "double"; return;
5420b57cec5SDimitry Andric   case Type::X86_FP80TyID:  OS << "x86_fp80"; return;
5430b57cec5SDimitry Andric   case Type::FP128TyID:     OS << "fp128"; return;
5440b57cec5SDimitry Andric   case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
5450b57cec5SDimitry Andric   case Type::LabelTyID:     OS << "label"; return;
5460b57cec5SDimitry Andric   case Type::MetadataTyID:  OS << "metadata"; return;
5470b57cec5SDimitry Andric   case Type::X86_MMXTyID:   OS << "x86_mmx"; return;
548e8d8bef9SDimitry Andric   case Type::X86_AMXTyID:   OS << "x86_amx"; return;
5490b57cec5SDimitry Andric   case Type::TokenTyID:     OS << "token"; return;
5500b57cec5SDimitry Andric   case Type::IntegerTyID:
5510b57cec5SDimitry Andric     OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
5520b57cec5SDimitry Andric     return;
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric   case Type::FunctionTyID: {
5550b57cec5SDimitry Andric     FunctionType *FTy = cast<FunctionType>(Ty);
5560b57cec5SDimitry Andric     print(FTy->getReturnType(), OS);
5570b57cec5SDimitry Andric     OS << " (";
558*349cc55cSDimitry Andric     ListSeparator LS;
559*349cc55cSDimitry Andric     for (Type *Ty : FTy->params()) {
560*349cc55cSDimitry Andric       OS << LS;
561*349cc55cSDimitry Andric       print(Ty, OS);
5620b57cec5SDimitry Andric     }
563*349cc55cSDimitry Andric     if (FTy->isVarArg())
564*349cc55cSDimitry Andric       OS << LS << "...";
5650b57cec5SDimitry Andric     OS << ')';
5660b57cec5SDimitry Andric     return;
5670b57cec5SDimitry Andric   }
5680b57cec5SDimitry Andric   case Type::StructTyID: {
5690b57cec5SDimitry Andric     StructType *STy = cast<StructType>(Ty);
5700b57cec5SDimitry Andric 
5710b57cec5SDimitry Andric     if (STy->isLiteral())
5720b57cec5SDimitry Andric       return printStructBody(STy, OS);
5730b57cec5SDimitry Andric 
5740b57cec5SDimitry Andric     if (!STy->getName().empty())
5750b57cec5SDimitry Andric       return PrintLLVMName(OS, STy->getName(), LocalPrefix);
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric     incorporateTypes();
5780b57cec5SDimitry Andric     const auto I = Type2Number.find(STy);
5790b57cec5SDimitry Andric     if (I != Type2Number.end())
5800b57cec5SDimitry Andric       OS << '%' << I->second;
5810b57cec5SDimitry Andric     else  // Not enumerated, print the hex address.
5820b57cec5SDimitry Andric       OS << "%\"type " << STy << '\"';
5830b57cec5SDimitry Andric     return;
5840b57cec5SDimitry Andric   }
5850b57cec5SDimitry Andric   case Type::PointerTyID: {
5860b57cec5SDimitry Andric     PointerType *PTy = cast<PointerType>(Ty);
587fe6060f1SDimitry Andric     if (PTy->isOpaque()) {
588fe6060f1SDimitry Andric       OS << "ptr";
589fe6060f1SDimitry Andric       if (unsigned AddressSpace = PTy->getAddressSpace())
590fe6060f1SDimitry Andric         OS << " addrspace(" << AddressSpace << ')';
591fe6060f1SDimitry Andric       return;
592fe6060f1SDimitry Andric     }
5930b57cec5SDimitry Andric     print(PTy->getElementType(), OS);
5940b57cec5SDimitry Andric     if (unsigned AddressSpace = PTy->getAddressSpace())
5950b57cec5SDimitry Andric       OS << " addrspace(" << AddressSpace << ')';
5960b57cec5SDimitry Andric     OS << '*';
5970b57cec5SDimitry Andric     return;
5980b57cec5SDimitry Andric   }
5990b57cec5SDimitry Andric   case Type::ArrayTyID: {
6000b57cec5SDimitry Andric     ArrayType *ATy = cast<ArrayType>(Ty);
6010b57cec5SDimitry Andric     OS << '[' << ATy->getNumElements() << " x ";
6020b57cec5SDimitry Andric     print(ATy->getElementType(), OS);
6030b57cec5SDimitry Andric     OS << ']';
6040b57cec5SDimitry Andric     return;
6050b57cec5SDimitry Andric   }
6065ffd83dbSDimitry Andric   case Type::FixedVectorTyID:
6075ffd83dbSDimitry Andric   case Type::ScalableVectorTyID: {
6080b57cec5SDimitry Andric     VectorType *PTy = cast<VectorType>(Ty);
6095ffd83dbSDimitry Andric     ElementCount EC = PTy->getElementCount();
6100b57cec5SDimitry Andric     OS << "<";
611e8d8bef9SDimitry Andric     if (EC.isScalable())
6120b57cec5SDimitry Andric       OS << "vscale x ";
613e8d8bef9SDimitry Andric     OS << EC.getKnownMinValue() << " x ";
6140b57cec5SDimitry Andric     print(PTy->getElementType(), OS);
6150b57cec5SDimitry Andric     OS << '>';
6160b57cec5SDimitry Andric     return;
6170b57cec5SDimitry Andric   }
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric   llvm_unreachable("Invalid TypeID");
6200b57cec5SDimitry Andric }
6210b57cec5SDimitry Andric 
6220b57cec5SDimitry Andric void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
6230b57cec5SDimitry Andric   if (STy->isOpaque()) {
6240b57cec5SDimitry Andric     OS << "opaque";
6250b57cec5SDimitry Andric     return;
6260b57cec5SDimitry Andric   }
6270b57cec5SDimitry Andric 
6280b57cec5SDimitry Andric   if (STy->isPacked())
6290b57cec5SDimitry Andric     OS << '<';
6300b57cec5SDimitry Andric 
6310b57cec5SDimitry Andric   if (STy->getNumElements() == 0) {
6320b57cec5SDimitry Andric     OS << "{}";
6330b57cec5SDimitry Andric   } else {
6340b57cec5SDimitry Andric     OS << "{ ";
635*349cc55cSDimitry Andric     ListSeparator LS;
636*349cc55cSDimitry Andric     for (Type *Ty : STy->elements()) {
637*349cc55cSDimitry Andric       OS << LS;
638*349cc55cSDimitry Andric       print(Ty, OS);
6390b57cec5SDimitry Andric     }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric     OS << " }";
6420b57cec5SDimitry Andric   }
6430b57cec5SDimitry Andric   if (STy->isPacked())
6440b57cec5SDimitry Andric     OS << '>';
6450b57cec5SDimitry Andric }
6460b57cec5SDimitry Andric 
647fe6060f1SDimitry Andric AbstractSlotTrackerStorage::~AbstractSlotTrackerStorage() {}
648fe6060f1SDimitry Andric 
6490b57cec5SDimitry Andric namespace llvm {
6500b57cec5SDimitry Andric 
6510b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6520b57cec5SDimitry Andric // SlotTracker Class: Enumerate slot numbers for unnamed values
6530b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
6540b57cec5SDimitry Andric /// This class provides computation of slot numbers for LLVM Assembly writing.
6550b57cec5SDimitry Andric ///
656fe6060f1SDimitry Andric class SlotTracker : public AbstractSlotTrackerStorage {
6570b57cec5SDimitry Andric public:
6580b57cec5SDimitry Andric   /// ValueMap - A mapping of Values to slot numbers.
6590b57cec5SDimitry Andric   using ValueMap = DenseMap<const Value *, unsigned>;
6600b57cec5SDimitry Andric 
6610b57cec5SDimitry Andric private:
6620b57cec5SDimitry Andric   /// TheModule - The module for which we are holding slot numbers.
6630b57cec5SDimitry Andric   const Module* TheModule;
6640b57cec5SDimitry Andric 
6650b57cec5SDimitry Andric   /// TheFunction - The function for which we are holding slot numbers.
6660b57cec5SDimitry Andric   const Function* TheFunction = nullptr;
6670b57cec5SDimitry Andric   bool FunctionProcessed = false;
6680b57cec5SDimitry Andric   bool ShouldInitializeAllMetadata;
6690b57cec5SDimitry Andric 
670fe6060f1SDimitry Andric   std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
671fe6060f1SDimitry Andric       ProcessModuleHookFn;
672fe6060f1SDimitry Andric   std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
673fe6060f1SDimitry Andric       ProcessFunctionHookFn;
674fe6060f1SDimitry Andric 
6750b57cec5SDimitry Andric   /// The summary index for which we are holding slot numbers.
6760b57cec5SDimitry Andric   const ModuleSummaryIndex *TheIndex = nullptr;
6770b57cec5SDimitry Andric 
6780b57cec5SDimitry Andric   /// mMap - The slot map for the module level data.
6790b57cec5SDimitry Andric   ValueMap mMap;
6800b57cec5SDimitry Andric   unsigned mNext = 0;
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric   /// fMap - The slot map for the function level data.
6830b57cec5SDimitry Andric   ValueMap fMap;
6840b57cec5SDimitry Andric   unsigned fNext = 0;
6850b57cec5SDimitry Andric 
6860b57cec5SDimitry Andric   /// mdnMap - Map for MDNodes.
6870b57cec5SDimitry Andric   DenseMap<const MDNode*, unsigned> mdnMap;
6880b57cec5SDimitry Andric   unsigned mdnNext = 0;
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric   /// asMap - The slot map for attribute sets.
6910b57cec5SDimitry Andric   DenseMap<AttributeSet, unsigned> asMap;
6920b57cec5SDimitry Andric   unsigned asNext = 0;
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric   /// ModulePathMap - The slot map for Module paths used in the summary index.
6950b57cec5SDimitry Andric   StringMap<unsigned> ModulePathMap;
6960b57cec5SDimitry Andric   unsigned ModulePathNext = 0;
6970b57cec5SDimitry Andric 
6980b57cec5SDimitry Andric   /// GUIDMap - The slot map for GUIDs used in the summary index.
6990b57cec5SDimitry Andric   DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
7000b57cec5SDimitry Andric   unsigned GUIDNext = 0;
7010b57cec5SDimitry Andric 
7020b57cec5SDimitry Andric   /// TypeIdMap - The slot map for type ids used in the summary index.
7030b57cec5SDimitry Andric   StringMap<unsigned> TypeIdMap;
7040b57cec5SDimitry Andric   unsigned TypeIdNext = 0;
7050b57cec5SDimitry Andric 
7060b57cec5SDimitry Andric public:
7070b57cec5SDimitry Andric   /// Construct from a module.
7080b57cec5SDimitry Andric   ///
7090b57cec5SDimitry Andric   /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
7100b57cec5SDimitry Andric   /// functions, giving correct numbering for metadata referenced only from
7110b57cec5SDimitry Andric   /// within a function (even if no functions have been initialized).
7120b57cec5SDimitry Andric   explicit SlotTracker(const Module *M,
7130b57cec5SDimitry Andric                        bool ShouldInitializeAllMetadata = false);
7140b57cec5SDimitry Andric 
7150b57cec5SDimitry Andric   /// Construct from a function, starting out in incorp state.
7160b57cec5SDimitry Andric   ///
7170b57cec5SDimitry Andric   /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
7180b57cec5SDimitry Andric   /// functions, giving correct numbering for metadata referenced only from
7190b57cec5SDimitry Andric   /// within a function (even if no functions have been initialized).
7200b57cec5SDimitry Andric   explicit SlotTracker(const Function *F,
7210b57cec5SDimitry Andric                        bool ShouldInitializeAllMetadata = false);
7220b57cec5SDimitry Andric 
7230b57cec5SDimitry Andric   /// Construct from a module summary index.
7240b57cec5SDimitry Andric   explicit SlotTracker(const ModuleSummaryIndex *Index);
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   SlotTracker(const SlotTracker &) = delete;
7270b57cec5SDimitry Andric   SlotTracker &operator=(const SlotTracker &) = delete;
7280b57cec5SDimitry Andric 
729fe6060f1SDimitry Andric   ~SlotTracker() = default;
730fe6060f1SDimitry Andric 
731fe6060f1SDimitry Andric   void setProcessHook(
732fe6060f1SDimitry Andric       std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>);
733fe6060f1SDimitry Andric   void setProcessHook(std::function<void(AbstractSlotTrackerStorage *,
734fe6060f1SDimitry Andric                                          const Function *, bool)>);
735fe6060f1SDimitry Andric 
736fe6060f1SDimitry Andric   unsigned getNextMetadataSlot() override { return mdnNext; }
737fe6060f1SDimitry Andric 
738fe6060f1SDimitry Andric   void createMetadataSlot(const MDNode *N) override;
739fe6060f1SDimitry Andric 
7400b57cec5SDimitry Andric   /// Return the slot number of the specified value in it's type
7410b57cec5SDimitry Andric   /// plane.  If something is not in the SlotTracker, return -1.
7420b57cec5SDimitry Andric   int getLocalSlot(const Value *V);
7430b57cec5SDimitry Andric   int getGlobalSlot(const GlobalValue *V);
744fe6060f1SDimitry Andric   int getMetadataSlot(const MDNode *N) override;
7450b57cec5SDimitry Andric   int getAttributeGroupSlot(AttributeSet AS);
7460b57cec5SDimitry Andric   int getModulePathSlot(StringRef Path);
7470b57cec5SDimitry Andric   int getGUIDSlot(GlobalValue::GUID GUID);
7480b57cec5SDimitry Andric   int getTypeIdSlot(StringRef Id);
7490b57cec5SDimitry Andric 
7500b57cec5SDimitry Andric   /// If you'd like to deal with a function instead of just a module, use
7510b57cec5SDimitry Andric   /// this method to get its data into the SlotTracker.
7520b57cec5SDimitry Andric   void incorporateFunction(const Function *F) {
7530b57cec5SDimitry Andric     TheFunction = F;
7540b57cec5SDimitry Andric     FunctionProcessed = false;
7550b57cec5SDimitry Andric   }
7560b57cec5SDimitry Andric 
7570b57cec5SDimitry Andric   const Function *getFunction() const { return TheFunction; }
7580b57cec5SDimitry Andric 
7590b57cec5SDimitry Andric   /// After calling incorporateFunction, use this method to remove the
7600b57cec5SDimitry Andric   /// most recently incorporated function from the SlotTracker. This
7610b57cec5SDimitry Andric   /// will reset the state of the machine back to just the module contents.
7620b57cec5SDimitry Andric   void purgeFunction();
7630b57cec5SDimitry Andric 
7640b57cec5SDimitry Andric   /// MDNode map iterators.
7650b57cec5SDimitry Andric   using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
7660b57cec5SDimitry Andric 
7670b57cec5SDimitry Andric   mdn_iterator mdn_begin() { return mdnMap.begin(); }
7680b57cec5SDimitry Andric   mdn_iterator mdn_end() { return mdnMap.end(); }
7690b57cec5SDimitry Andric   unsigned mdn_size() const { return mdnMap.size(); }
7700b57cec5SDimitry Andric   bool mdn_empty() const { return mdnMap.empty(); }
7710b57cec5SDimitry Andric 
7720b57cec5SDimitry Andric   /// AttributeSet map iterators.
7730b57cec5SDimitry Andric   using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
7740b57cec5SDimitry Andric 
7750b57cec5SDimitry Andric   as_iterator as_begin()   { return asMap.begin(); }
7760b57cec5SDimitry Andric   as_iterator as_end()     { return asMap.end(); }
7770b57cec5SDimitry Andric   unsigned as_size() const { return asMap.size(); }
7780b57cec5SDimitry Andric   bool as_empty() const    { return asMap.empty(); }
7790b57cec5SDimitry Andric 
7800b57cec5SDimitry Andric   /// GUID map iterators.
7810b57cec5SDimitry Andric   using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric   /// These functions do the actual initialization.
7840b57cec5SDimitry Andric   inline void initializeIfNeeded();
7855ffd83dbSDimitry Andric   int initializeIndexIfNeeded();
7860b57cec5SDimitry Andric 
7870b57cec5SDimitry Andric   // Implementation Details
7880b57cec5SDimitry Andric private:
7890b57cec5SDimitry Andric   /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
7900b57cec5SDimitry Andric   void CreateModuleSlot(const GlobalValue *V);
7910b57cec5SDimitry Andric 
7920b57cec5SDimitry Andric   /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
7930b57cec5SDimitry Andric   void CreateMetadataSlot(const MDNode *N);
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric   /// CreateFunctionSlot - Insert the specified Value* into the slot table.
7960b57cec5SDimitry Andric   void CreateFunctionSlot(const Value *V);
7970b57cec5SDimitry Andric 
7980b57cec5SDimitry Andric   /// Insert the specified AttributeSet into the slot table.
7990b57cec5SDimitry Andric   void CreateAttributeSetSlot(AttributeSet AS);
8000b57cec5SDimitry Andric 
8010b57cec5SDimitry Andric   inline void CreateModulePathSlot(StringRef Path);
8020b57cec5SDimitry Andric   void CreateGUIDSlot(GlobalValue::GUID GUID);
8030b57cec5SDimitry Andric   void CreateTypeIdSlot(StringRef Id);
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric   /// Add all of the module level global variables (and their initializers)
8060b57cec5SDimitry Andric   /// and function declarations, but not the contents of those functions.
8070b57cec5SDimitry Andric   void processModule();
8085ffd83dbSDimitry Andric   // Returns number of allocated slots
8095ffd83dbSDimitry Andric   int processIndex();
8100b57cec5SDimitry Andric 
8110b57cec5SDimitry Andric   /// Add all of the functions arguments, basic blocks, and instructions.
8120b57cec5SDimitry Andric   void processFunction();
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric   /// Add the metadata directly attached to a GlobalObject.
8150b57cec5SDimitry Andric   void processGlobalObjectMetadata(const GlobalObject &GO);
8160b57cec5SDimitry Andric 
8170b57cec5SDimitry Andric   /// Add all of the metadata from a function.
8180b57cec5SDimitry Andric   void processFunctionMetadata(const Function &F);
8190b57cec5SDimitry Andric 
8200b57cec5SDimitry Andric   /// Add all of the metadata from an instruction.
8210b57cec5SDimitry Andric   void processInstructionMetadata(const Instruction &I);
8220b57cec5SDimitry Andric };
8230b57cec5SDimitry Andric 
8240b57cec5SDimitry Andric } // end namespace llvm
8250b57cec5SDimitry Andric 
8260b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
8270b57cec5SDimitry Andric                                      const Function *F)
8280b57cec5SDimitry Andric     : M(M), F(F), Machine(&Machine) {}
8290b57cec5SDimitry Andric 
8300b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(const Module *M,
8310b57cec5SDimitry Andric                                      bool ShouldInitializeAllMetadata)
8320b57cec5SDimitry Andric     : ShouldCreateStorage(M),
8330b57cec5SDimitry Andric       ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
8340b57cec5SDimitry Andric 
8350b57cec5SDimitry Andric ModuleSlotTracker::~ModuleSlotTracker() = default;
8360b57cec5SDimitry Andric 
8370b57cec5SDimitry Andric SlotTracker *ModuleSlotTracker::getMachine() {
8380b57cec5SDimitry Andric   if (!ShouldCreateStorage)
8390b57cec5SDimitry Andric     return Machine;
8400b57cec5SDimitry Andric 
8410b57cec5SDimitry Andric   ShouldCreateStorage = false;
8420b57cec5SDimitry Andric   MachineStorage =
8438bcb0991SDimitry Andric       std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
8440b57cec5SDimitry Andric   Machine = MachineStorage.get();
845fe6060f1SDimitry Andric   if (ProcessModuleHookFn)
846fe6060f1SDimitry Andric     Machine->setProcessHook(ProcessModuleHookFn);
847fe6060f1SDimitry Andric   if (ProcessFunctionHookFn)
848fe6060f1SDimitry Andric     Machine->setProcessHook(ProcessFunctionHookFn);
8490b57cec5SDimitry Andric   return Machine;
8500b57cec5SDimitry Andric }
8510b57cec5SDimitry Andric 
8520b57cec5SDimitry Andric void ModuleSlotTracker::incorporateFunction(const Function &F) {
8530b57cec5SDimitry Andric   // Using getMachine() may lazily create the slot tracker.
8540b57cec5SDimitry Andric   if (!getMachine())
8550b57cec5SDimitry Andric     return;
8560b57cec5SDimitry Andric 
8570b57cec5SDimitry Andric   // Nothing to do if this is the right function already.
8580b57cec5SDimitry Andric   if (this->F == &F)
8590b57cec5SDimitry Andric     return;
8600b57cec5SDimitry Andric   if (this->F)
8610b57cec5SDimitry Andric     Machine->purgeFunction();
8620b57cec5SDimitry Andric   Machine->incorporateFunction(&F);
8630b57cec5SDimitry Andric   this->F = &F;
8640b57cec5SDimitry Andric }
8650b57cec5SDimitry Andric 
8660b57cec5SDimitry Andric int ModuleSlotTracker::getLocalSlot(const Value *V) {
8670b57cec5SDimitry Andric   assert(F && "No function incorporated");
8680b57cec5SDimitry Andric   return Machine->getLocalSlot(V);
8690b57cec5SDimitry Andric }
8700b57cec5SDimitry Andric 
871fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook(
872fe6060f1SDimitry Andric     std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
873fe6060f1SDimitry Andric         Fn) {
874fe6060f1SDimitry Andric   ProcessModuleHookFn = Fn;
875fe6060f1SDimitry Andric }
876fe6060f1SDimitry Andric 
877fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook(
878fe6060f1SDimitry Andric     std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
879fe6060f1SDimitry Andric         Fn) {
880fe6060f1SDimitry Andric   ProcessFunctionHookFn = Fn;
881fe6060f1SDimitry Andric }
882fe6060f1SDimitry Andric 
8830b57cec5SDimitry Andric static SlotTracker *createSlotTracker(const Value *V) {
8840b57cec5SDimitry Andric   if (const Argument *FA = dyn_cast<Argument>(V))
8850b57cec5SDimitry Andric     return new SlotTracker(FA->getParent());
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   if (const Instruction *I = dyn_cast<Instruction>(V))
8880b57cec5SDimitry Andric     if (I->getParent())
8890b57cec5SDimitry Andric       return new SlotTracker(I->getParent()->getParent());
8900b57cec5SDimitry Andric 
8910b57cec5SDimitry Andric   if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
8920b57cec5SDimitry Andric     return new SlotTracker(BB->getParent());
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
8950b57cec5SDimitry Andric     return new SlotTracker(GV->getParent());
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
8980b57cec5SDimitry Andric     return new SlotTracker(GA->getParent());
8990b57cec5SDimitry Andric 
9000b57cec5SDimitry Andric   if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
9010b57cec5SDimitry Andric     return new SlotTracker(GIF->getParent());
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric   if (const Function *Func = dyn_cast<Function>(V))
9040b57cec5SDimitry Andric     return new SlotTracker(Func);
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   return nullptr;
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric #if 0
9100b57cec5SDimitry Andric #define ST_DEBUG(X) dbgs() << X
9110b57cec5SDimitry Andric #else
9120b57cec5SDimitry Andric #define ST_DEBUG(X)
9130b57cec5SDimitry Andric #endif
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric // Module level constructor. Causes the contents of the Module (sans functions)
9160b57cec5SDimitry Andric // to be added to the slot table.
9170b57cec5SDimitry Andric SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
9180b57cec5SDimitry Andric     : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
9190b57cec5SDimitry Andric 
9200b57cec5SDimitry Andric // Function level constructor. Causes the contents of the Module and the one
9210b57cec5SDimitry Andric // function provided to be added to the slot table.
9220b57cec5SDimitry Andric SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
9230b57cec5SDimitry Andric     : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
9240b57cec5SDimitry Andric       ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
9270b57cec5SDimitry Andric     : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
9280b57cec5SDimitry Andric 
9290b57cec5SDimitry Andric inline void SlotTracker::initializeIfNeeded() {
9300b57cec5SDimitry Andric   if (TheModule) {
9310b57cec5SDimitry Andric     processModule();
9320b57cec5SDimitry Andric     TheModule = nullptr; ///< Prevent re-processing next time we're called.
9330b57cec5SDimitry Andric   }
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   if (TheFunction && !FunctionProcessed)
9360b57cec5SDimitry Andric     processFunction();
9370b57cec5SDimitry Andric }
9380b57cec5SDimitry Andric 
9395ffd83dbSDimitry Andric int SlotTracker::initializeIndexIfNeeded() {
9400b57cec5SDimitry Andric   if (!TheIndex)
9415ffd83dbSDimitry Andric     return 0;
9425ffd83dbSDimitry Andric   int NumSlots = processIndex();
9430b57cec5SDimitry Andric   TheIndex = nullptr; ///< Prevent re-processing next time we're called.
9445ffd83dbSDimitry Andric   return NumSlots;
9450b57cec5SDimitry Andric }
9460b57cec5SDimitry Andric 
9470b57cec5SDimitry Andric // Iterate through all the global variables, functions, and global
9480b57cec5SDimitry Andric // variable initializers and create slots for them.
9490b57cec5SDimitry Andric void SlotTracker::processModule() {
9500b57cec5SDimitry Andric   ST_DEBUG("begin processModule!\n");
9510b57cec5SDimitry Andric 
9520b57cec5SDimitry Andric   // Add all of the unnamed global variables to the value table.
9530b57cec5SDimitry Andric   for (const GlobalVariable &Var : TheModule->globals()) {
9540b57cec5SDimitry Andric     if (!Var.hasName())
9550b57cec5SDimitry Andric       CreateModuleSlot(&Var);
9560b57cec5SDimitry Andric     processGlobalObjectMetadata(Var);
9570b57cec5SDimitry Andric     auto Attrs = Var.getAttributes();
9580b57cec5SDimitry Andric     if (Attrs.hasAttributes())
9590b57cec5SDimitry Andric       CreateAttributeSetSlot(Attrs);
9600b57cec5SDimitry Andric   }
9610b57cec5SDimitry Andric 
9620b57cec5SDimitry Andric   for (const GlobalAlias &A : TheModule->aliases()) {
9630b57cec5SDimitry Andric     if (!A.hasName())
9640b57cec5SDimitry Andric       CreateModuleSlot(&A);
9650b57cec5SDimitry Andric   }
9660b57cec5SDimitry Andric 
9670b57cec5SDimitry Andric   for (const GlobalIFunc &I : TheModule->ifuncs()) {
9680b57cec5SDimitry Andric     if (!I.hasName())
9690b57cec5SDimitry Andric       CreateModuleSlot(&I);
9700b57cec5SDimitry Andric   }
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric   // Add metadata used by named metadata.
9730b57cec5SDimitry Andric   for (const NamedMDNode &NMD : TheModule->named_metadata()) {
9740b57cec5SDimitry Andric     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
9750b57cec5SDimitry Andric       CreateMetadataSlot(NMD.getOperand(i));
9760b57cec5SDimitry Andric   }
9770b57cec5SDimitry Andric 
9780b57cec5SDimitry Andric   for (const Function &F : *TheModule) {
9790b57cec5SDimitry Andric     if (!F.hasName())
9800b57cec5SDimitry Andric       // Add all the unnamed functions to the table.
9810b57cec5SDimitry Andric       CreateModuleSlot(&F);
9820b57cec5SDimitry Andric 
9830b57cec5SDimitry Andric     if (ShouldInitializeAllMetadata)
9840b57cec5SDimitry Andric       processFunctionMetadata(F);
9850b57cec5SDimitry Andric 
9860b57cec5SDimitry Andric     // Add all the function attributes to the table.
9870b57cec5SDimitry Andric     // FIXME: Add attributes of other objects?
988*349cc55cSDimitry Andric     AttributeSet FnAttrs = F.getAttributes().getFnAttrs();
9890b57cec5SDimitry Andric     if (FnAttrs.hasAttributes())
9900b57cec5SDimitry Andric       CreateAttributeSetSlot(FnAttrs);
9910b57cec5SDimitry Andric   }
9920b57cec5SDimitry Andric 
993fe6060f1SDimitry Andric   if (ProcessModuleHookFn)
994fe6060f1SDimitry Andric     ProcessModuleHookFn(this, TheModule, ShouldInitializeAllMetadata);
995fe6060f1SDimitry Andric 
9960b57cec5SDimitry Andric   ST_DEBUG("end processModule!\n");
9970b57cec5SDimitry Andric }
9980b57cec5SDimitry Andric 
9990b57cec5SDimitry Andric // Process the arguments, basic blocks, and instructions  of a function.
10000b57cec5SDimitry Andric void SlotTracker::processFunction() {
10010b57cec5SDimitry Andric   ST_DEBUG("begin processFunction!\n");
10020b57cec5SDimitry Andric   fNext = 0;
10030b57cec5SDimitry Andric 
10040b57cec5SDimitry Andric   // Process function metadata if it wasn't hit at the module-level.
10050b57cec5SDimitry Andric   if (!ShouldInitializeAllMetadata)
10060b57cec5SDimitry Andric     processFunctionMetadata(*TheFunction);
10070b57cec5SDimitry Andric 
10080b57cec5SDimitry Andric   // Add all the function arguments with no names.
10090b57cec5SDimitry Andric   for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
10100b57cec5SDimitry Andric       AE = TheFunction->arg_end(); AI != AE; ++AI)
10110b57cec5SDimitry Andric     if (!AI->hasName())
10120b57cec5SDimitry Andric       CreateFunctionSlot(&*AI);
10130b57cec5SDimitry Andric 
10140b57cec5SDimitry Andric   ST_DEBUG("Inserting Instructions:\n");
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   // Add all of the basic blocks and instructions with no names.
10170b57cec5SDimitry Andric   for (auto &BB : *TheFunction) {
10180b57cec5SDimitry Andric     if (!BB.hasName())
10190b57cec5SDimitry Andric       CreateFunctionSlot(&BB);
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric     for (auto &I : BB) {
10220b57cec5SDimitry Andric       if (!I.getType()->isVoidTy() && !I.hasName())
10230b57cec5SDimitry Andric         CreateFunctionSlot(&I);
10240b57cec5SDimitry Andric 
10250b57cec5SDimitry Andric       // We allow direct calls to any llvm.foo function here, because the
10260b57cec5SDimitry Andric       // target may not be linked into the optimizer.
10270b57cec5SDimitry Andric       if (const auto *Call = dyn_cast<CallBase>(&I)) {
10280b57cec5SDimitry Andric         // Add all the call attributes to the table.
1029*349cc55cSDimitry Andric         AttributeSet Attrs = Call->getAttributes().getFnAttrs();
10300b57cec5SDimitry Andric         if (Attrs.hasAttributes())
10310b57cec5SDimitry Andric           CreateAttributeSetSlot(Attrs);
10320b57cec5SDimitry Andric       }
10330b57cec5SDimitry Andric     }
10340b57cec5SDimitry Andric   }
10350b57cec5SDimitry Andric 
1036fe6060f1SDimitry Andric   if (ProcessFunctionHookFn)
1037fe6060f1SDimitry Andric     ProcessFunctionHookFn(this, TheFunction, ShouldInitializeAllMetadata);
1038fe6060f1SDimitry Andric 
10390b57cec5SDimitry Andric   FunctionProcessed = true;
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric   ST_DEBUG("end processFunction!\n");
10420b57cec5SDimitry Andric }
10430b57cec5SDimitry Andric 
10440b57cec5SDimitry Andric // Iterate through all the GUID in the index and create slots for them.
10455ffd83dbSDimitry Andric int SlotTracker::processIndex() {
10460b57cec5SDimitry Andric   ST_DEBUG("begin processIndex!\n");
10470b57cec5SDimitry Andric   assert(TheIndex);
10480b57cec5SDimitry Andric 
10490b57cec5SDimitry Andric   // The first block of slots are just the module ids, which start at 0 and are
10500b57cec5SDimitry Andric   // assigned consecutively. Since the StringMap iteration order isn't
10510b57cec5SDimitry Andric   // guaranteed, use a std::map to order by module ID before assigning slots.
10520b57cec5SDimitry Andric   std::map<uint64_t, StringRef> ModuleIdToPathMap;
10530b57cec5SDimitry Andric   for (auto &ModPath : TheIndex->modulePaths())
10540b57cec5SDimitry Andric     ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
10550b57cec5SDimitry Andric   for (auto &ModPair : ModuleIdToPathMap)
10560b57cec5SDimitry Andric     CreateModulePathSlot(ModPair.second);
10570b57cec5SDimitry Andric 
10580b57cec5SDimitry Andric   // Start numbering the GUIDs after the module ids.
10590b57cec5SDimitry Andric   GUIDNext = ModulePathNext;
10600b57cec5SDimitry Andric 
10610b57cec5SDimitry Andric   for (auto &GlobalList : *TheIndex)
10620b57cec5SDimitry Andric     CreateGUIDSlot(GlobalList.first);
10630b57cec5SDimitry Andric 
10645ffd83dbSDimitry Andric   for (auto &TId : TheIndex->typeIdCompatibleVtableMap())
10655ffd83dbSDimitry Andric     CreateGUIDSlot(GlobalValue::getGUID(TId.first));
10665ffd83dbSDimitry Andric 
10670b57cec5SDimitry Andric   // Start numbering the TypeIds after the GUIDs.
10680b57cec5SDimitry Andric   TypeIdNext = GUIDNext;
1069fe6060f1SDimitry Andric   for (const auto &TID : TheIndex->typeIds())
1070fe6060f1SDimitry Andric     CreateTypeIdSlot(TID.second.first);
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   ST_DEBUG("end processIndex!\n");
10735ffd83dbSDimitry Andric   return TypeIdNext;
10740b57cec5SDimitry Andric }
10750b57cec5SDimitry Andric 
10760b57cec5SDimitry Andric void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
10770b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
10780b57cec5SDimitry Andric   GO.getAllMetadata(MDs);
10790b57cec5SDimitry Andric   for (auto &MD : MDs)
10800b57cec5SDimitry Andric     CreateMetadataSlot(MD.second);
10810b57cec5SDimitry Andric }
10820b57cec5SDimitry Andric 
10830b57cec5SDimitry Andric void SlotTracker::processFunctionMetadata(const Function &F) {
10840b57cec5SDimitry Andric   processGlobalObjectMetadata(F);
10850b57cec5SDimitry Andric   for (auto &BB : F) {
10860b57cec5SDimitry Andric     for (auto &I : BB)
10870b57cec5SDimitry Andric       processInstructionMetadata(I);
10880b57cec5SDimitry Andric   }
10890b57cec5SDimitry Andric }
10900b57cec5SDimitry Andric 
10910b57cec5SDimitry Andric void SlotTracker::processInstructionMetadata(const Instruction &I) {
10920b57cec5SDimitry Andric   // Process metadata used directly by intrinsics.
10930b57cec5SDimitry Andric   if (const CallInst *CI = dyn_cast<CallInst>(&I))
10940b57cec5SDimitry Andric     if (Function *F = CI->getCalledFunction())
10950b57cec5SDimitry Andric       if (F->isIntrinsic())
10960b57cec5SDimitry Andric         for (auto &Op : I.operands())
10970b57cec5SDimitry Andric           if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
10980b57cec5SDimitry Andric             if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
10990b57cec5SDimitry Andric               CreateMetadataSlot(N);
11000b57cec5SDimitry Andric 
11010b57cec5SDimitry Andric   // Process metadata attached to this instruction.
11020b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
11030b57cec5SDimitry Andric   I.getAllMetadata(MDs);
11040b57cec5SDimitry Andric   for (auto &MD : MDs)
11050b57cec5SDimitry Andric     CreateMetadataSlot(MD.second);
11060b57cec5SDimitry Andric }
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric /// Clean up after incorporating a function. This is the only way to get out of
11090b57cec5SDimitry Andric /// the function incorporation state that affects get*Slot/Create*Slot. Function
11100b57cec5SDimitry Andric /// incorporation state is indicated by TheFunction != 0.
11110b57cec5SDimitry Andric void SlotTracker::purgeFunction() {
11120b57cec5SDimitry Andric   ST_DEBUG("begin purgeFunction!\n");
11130b57cec5SDimitry Andric   fMap.clear(); // Simply discard the function level map
11140b57cec5SDimitry Andric   TheFunction = nullptr;
11150b57cec5SDimitry Andric   FunctionProcessed = false;
11160b57cec5SDimitry Andric   ST_DEBUG("end purgeFunction!\n");
11170b57cec5SDimitry Andric }
11180b57cec5SDimitry Andric 
11190b57cec5SDimitry Andric /// getGlobalSlot - Get the slot number of a global value.
11200b57cec5SDimitry Andric int SlotTracker::getGlobalSlot(const GlobalValue *V) {
11210b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11220b57cec5SDimitry Andric   initializeIfNeeded();
11230b57cec5SDimitry Andric 
11240b57cec5SDimitry Andric   // Find the value in the module map
11250b57cec5SDimitry Andric   ValueMap::iterator MI = mMap.find(V);
11260b57cec5SDimitry Andric   return MI == mMap.end() ? -1 : (int)MI->second;
11270b57cec5SDimitry Andric }
11280b57cec5SDimitry Andric 
1129fe6060f1SDimitry Andric void SlotTracker::setProcessHook(
1130fe6060f1SDimitry Andric     std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>
1131fe6060f1SDimitry Andric         Fn) {
1132fe6060f1SDimitry Andric   ProcessModuleHookFn = Fn;
1133fe6060f1SDimitry Andric }
1134fe6060f1SDimitry Andric 
1135fe6060f1SDimitry Andric void SlotTracker::setProcessHook(
1136fe6060f1SDimitry Andric     std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)>
1137fe6060f1SDimitry Andric         Fn) {
1138fe6060f1SDimitry Andric   ProcessFunctionHookFn = Fn;
1139fe6060f1SDimitry Andric }
1140fe6060f1SDimitry Andric 
1141fe6060f1SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode.
1142fe6060f1SDimitry Andric void SlotTracker::createMetadataSlot(const MDNode *N) { CreateMetadataSlot(N); }
1143fe6060f1SDimitry Andric 
11440b57cec5SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode.
11450b57cec5SDimitry Andric int SlotTracker::getMetadataSlot(const MDNode *N) {
11460b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11470b57cec5SDimitry Andric   initializeIfNeeded();
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric   // Find the MDNode in the module map
11500b57cec5SDimitry Andric   mdn_iterator MI = mdnMap.find(N);
11510b57cec5SDimitry Andric   return MI == mdnMap.end() ? -1 : (int)MI->second;
11520b57cec5SDimitry Andric }
11530b57cec5SDimitry Andric 
11540b57cec5SDimitry Andric /// getLocalSlot - Get the slot number for a value that is local to a function.
11550b57cec5SDimitry Andric int SlotTracker::getLocalSlot(const Value *V) {
11560b57cec5SDimitry Andric   assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
11570b57cec5SDimitry Andric 
11580b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11590b57cec5SDimitry Andric   initializeIfNeeded();
11600b57cec5SDimitry Andric 
11610b57cec5SDimitry Andric   ValueMap::iterator FI = fMap.find(V);
11620b57cec5SDimitry Andric   return FI == fMap.end() ? -1 : (int)FI->second;
11630b57cec5SDimitry Andric }
11640b57cec5SDimitry Andric 
11650b57cec5SDimitry Andric int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
11660b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11670b57cec5SDimitry Andric   initializeIfNeeded();
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric   // Find the AttributeSet in the module map.
11700b57cec5SDimitry Andric   as_iterator AI = asMap.find(AS);
11710b57cec5SDimitry Andric   return AI == asMap.end() ? -1 : (int)AI->second;
11720b57cec5SDimitry Andric }
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric int SlotTracker::getModulePathSlot(StringRef Path) {
11750b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11760b57cec5SDimitry Andric   initializeIndexIfNeeded();
11770b57cec5SDimitry Andric 
11780b57cec5SDimitry Andric   // Find the Module path in the map
11790b57cec5SDimitry Andric   auto I = ModulePathMap.find(Path);
11800b57cec5SDimitry Andric   return I == ModulePathMap.end() ? -1 : (int)I->second;
11810b57cec5SDimitry Andric }
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
11840b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11850b57cec5SDimitry Andric   initializeIndexIfNeeded();
11860b57cec5SDimitry Andric 
11870b57cec5SDimitry Andric   // Find the GUID in the map
11880b57cec5SDimitry Andric   guid_iterator I = GUIDMap.find(GUID);
11890b57cec5SDimitry Andric   return I == GUIDMap.end() ? -1 : (int)I->second;
11900b57cec5SDimitry Andric }
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric int SlotTracker::getTypeIdSlot(StringRef Id) {
11930b57cec5SDimitry Andric   // Check for uninitialized state and do lazy initialization.
11940b57cec5SDimitry Andric   initializeIndexIfNeeded();
11950b57cec5SDimitry Andric 
11960b57cec5SDimitry Andric   // Find the TypeId string in the map
11970b57cec5SDimitry Andric   auto I = TypeIdMap.find(Id);
11980b57cec5SDimitry Andric   return I == TypeIdMap.end() ? -1 : (int)I->second;
11990b57cec5SDimitry Andric }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
12020b57cec5SDimitry Andric void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
12030b57cec5SDimitry Andric   assert(V && "Can't insert a null Value into SlotTracker!");
12040b57cec5SDimitry Andric   assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
12050b57cec5SDimitry Andric   assert(!V->hasName() && "Doesn't need a slot!");
12060b57cec5SDimitry Andric 
12070b57cec5SDimitry Andric   unsigned DestSlot = mNext++;
12080b57cec5SDimitry Andric   mMap[V] = DestSlot;
12090b57cec5SDimitry Andric 
12100b57cec5SDimitry Andric   ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
12110b57cec5SDimitry Andric            DestSlot << " [");
12120b57cec5SDimitry Andric   // G = Global, F = Function, A = Alias, I = IFunc, o = other
12130b57cec5SDimitry Andric   ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
12140b57cec5SDimitry Andric             (isa<Function>(V) ? 'F' :
12150b57cec5SDimitry Andric              (isa<GlobalAlias>(V) ? 'A' :
12160b57cec5SDimitry Andric               (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
12170b57cec5SDimitry Andric }
12180b57cec5SDimitry Andric 
12190b57cec5SDimitry Andric /// CreateSlot - Create a new slot for the specified value if it has no name.
12200b57cec5SDimitry Andric void SlotTracker::CreateFunctionSlot(const Value *V) {
12210b57cec5SDimitry Andric   assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
12220b57cec5SDimitry Andric 
12230b57cec5SDimitry Andric   unsigned DestSlot = fNext++;
12240b57cec5SDimitry Andric   fMap[V] = DestSlot;
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric   // G = Global, F = Function, o = other
12270b57cec5SDimitry Andric   ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
12280b57cec5SDimitry Andric            DestSlot << " [o]\n");
12290b57cec5SDimitry Andric }
12300b57cec5SDimitry Andric 
12310b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
12320b57cec5SDimitry Andric void SlotTracker::CreateMetadataSlot(const MDNode *N) {
12330b57cec5SDimitry Andric   assert(N && "Can't insert a null Value into SlotTracker!");
12340b57cec5SDimitry Andric 
1235fe6060f1SDimitry Andric   // Don't make slots for DIExpressions or DIArgLists. We just print them inline
1236fe6060f1SDimitry Andric   // everywhere.
1237fe6060f1SDimitry Andric   if (isa<DIExpression>(N) || isa<DIArgList>(N))
12380b57cec5SDimitry Andric     return;
12390b57cec5SDimitry Andric 
12400b57cec5SDimitry Andric   unsigned DestSlot = mdnNext;
12410b57cec5SDimitry Andric   if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
12420b57cec5SDimitry Andric     return;
12430b57cec5SDimitry Andric   ++mdnNext;
12440b57cec5SDimitry Andric 
12450b57cec5SDimitry Andric   // Recursively add any MDNodes referenced by operands.
12460b57cec5SDimitry Andric   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
12470b57cec5SDimitry Andric     if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
12480b57cec5SDimitry Andric       CreateMetadataSlot(Op);
12490b57cec5SDimitry Andric }
12500b57cec5SDimitry Andric 
12510b57cec5SDimitry Andric void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
12520b57cec5SDimitry Andric   assert(AS.hasAttributes() && "Doesn't need a slot!");
12530b57cec5SDimitry Andric 
12540b57cec5SDimitry Andric   as_iterator I = asMap.find(AS);
12550b57cec5SDimitry Andric   if (I != asMap.end())
12560b57cec5SDimitry Andric     return;
12570b57cec5SDimitry Andric 
12580b57cec5SDimitry Andric   unsigned DestSlot = asNext++;
12590b57cec5SDimitry Andric   asMap[AS] = DestSlot;
12600b57cec5SDimitry Andric }
12610b57cec5SDimitry Andric 
12620b57cec5SDimitry Andric /// Create a new slot for the specified Module
12630b57cec5SDimitry Andric void SlotTracker::CreateModulePathSlot(StringRef Path) {
12640b57cec5SDimitry Andric   ModulePathMap[Path] = ModulePathNext++;
12650b57cec5SDimitry Andric }
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric /// Create a new slot for the specified GUID
12680b57cec5SDimitry Andric void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
12690b57cec5SDimitry Andric   GUIDMap[GUID] = GUIDNext++;
12700b57cec5SDimitry Andric }
12710b57cec5SDimitry Andric 
12720b57cec5SDimitry Andric /// Create a new slot for the specified Id
12730b57cec5SDimitry Andric void SlotTracker::CreateTypeIdSlot(StringRef Id) {
12740b57cec5SDimitry Andric   TypeIdMap[Id] = TypeIdNext++;
12750b57cec5SDimitry Andric }
12760b57cec5SDimitry Andric 
1277*349cc55cSDimitry Andric namespace {
1278*349cc55cSDimitry Andric /// Common instances used by most of the printer functions.
1279*349cc55cSDimitry Andric struct AsmWriterContext {
1280*349cc55cSDimitry Andric   TypePrinting *TypePrinter = nullptr;
1281*349cc55cSDimitry Andric   SlotTracker *Machine = nullptr;
1282*349cc55cSDimitry Andric   const Module *Context = nullptr;
1283*349cc55cSDimitry Andric 
1284*349cc55cSDimitry Andric   AsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M = nullptr)
1285*349cc55cSDimitry Andric       : TypePrinter(TP), Machine(ST), Context(M) {}
1286*349cc55cSDimitry Andric 
1287*349cc55cSDimitry Andric   static AsmWriterContext &getEmpty() {
1288*349cc55cSDimitry Andric     static AsmWriterContext EmptyCtx(nullptr, nullptr);
1289*349cc55cSDimitry Andric     return EmptyCtx;
1290*349cc55cSDimitry Andric   }
1291*349cc55cSDimitry Andric 
1292*349cc55cSDimitry Andric   /// A callback that will be triggered when the underlying printer
1293*349cc55cSDimitry Andric   /// prints a Metadata as operand.
1294*349cc55cSDimitry Andric   virtual void onWriteMetadataAsOperand(const Metadata *) {}
1295*349cc55cSDimitry Andric 
1296*349cc55cSDimitry Andric   virtual ~AsmWriterContext() {}
1297*349cc55cSDimitry Andric };
1298*349cc55cSDimitry Andric } // end anonymous namespace
1299*349cc55cSDimitry Andric 
13000b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13010b57cec5SDimitry Andric // AsmWriter Implementation
13020b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
13030b57cec5SDimitry Andric 
13040b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
1305*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx);
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
1308*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx,
13090b57cec5SDimitry Andric                                    bool FromValue = false);
13100b57cec5SDimitry Andric 
13110b57cec5SDimitry Andric static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
13120b57cec5SDimitry Andric   if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
13130b57cec5SDimitry Andric     // 'Fast' is an abbreviation for all fast-math-flags.
13140b57cec5SDimitry Andric     if (FPO->isFast())
13150b57cec5SDimitry Andric       Out << " fast";
13160b57cec5SDimitry Andric     else {
13170b57cec5SDimitry Andric       if (FPO->hasAllowReassoc())
13180b57cec5SDimitry Andric         Out << " reassoc";
13190b57cec5SDimitry Andric       if (FPO->hasNoNaNs())
13200b57cec5SDimitry Andric         Out << " nnan";
13210b57cec5SDimitry Andric       if (FPO->hasNoInfs())
13220b57cec5SDimitry Andric         Out << " ninf";
13230b57cec5SDimitry Andric       if (FPO->hasNoSignedZeros())
13240b57cec5SDimitry Andric         Out << " nsz";
13250b57cec5SDimitry Andric       if (FPO->hasAllowReciprocal())
13260b57cec5SDimitry Andric         Out << " arcp";
13270b57cec5SDimitry Andric       if (FPO->hasAllowContract())
13280b57cec5SDimitry Andric         Out << " contract";
13290b57cec5SDimitry Andric       if (FPO->hasApproxFunc())
13300b57cec5SDimitry Andric         Out << " afn";
13310b57cec5SDimitry Andric     }
13320b57cec5SDimitry Andric   }
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric   if (const OverflowingBinaryOperator *OBO =
13350b57cec5SDimitry Andric         dyn_cast<OverflowingBinaryOperator>(U)) {
13360b57cec5SDimitry Andric     if (OBO->hasNoUnsignedWrap())
13370b57cec5SDimitry Andric       Out << " nuw";
13380b57cec5SDimitry Andric     if (OBO->hasNoSignedWrap())
13390b57cec5SDimitry Andric       Out << " nsw";
13400b57cec5SDimitry Andric   } else if (const PossiblyExactOperator *Div =
13410b57cec5SDimitry Andric                dyn_cast<PossiblyExactOperator>(U)) {
13420b57cec5SDimitry Andric     if (Div->isExact())
13430b57cec5SDimitry Andric       Out << " exact";
13440b57cec5SDimitry Andric   } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
13450b57cec5SDimitry Andric     if (GEP->isInBounds())
13460b57cec5SDimitry Andric       Out << " inbounds";
13470b57cec5SDimitry Andric   }
13480b57cec5SDimitry Andric }
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
1351*349cc55cSDimitry Andric                                   AsmWriterContext &WriterCtx) {
13520b57cec5SDimitry Andric   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
13530b57cec5SDimitry Andric     if (CI->getType()->isIntegerTy(1)) {
13540b57cec5SDimitry Andric       Out << (CI->getZExtValue() ? "true" : "false");
13550b57cec5SDimitry Andric       return;
13560b57cec5SDimitry Andric     }
13570b57cec5SDimitry Andric     Out << CI->getValue();
13580b57cec5SDimitry Andric     return;
13590b57cec5SDimitry Andric   }
13600b57cec5SDimitry Andric 
13610b57cec5SDimitry Andric   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
13620b57cec5SDimitry Andric     const APFloat &APF = CFP->getValueAPF();
13630b57cec5SDimitry Andric     if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
13640b57cec5SDimitry Andric         &APF.getSemantics() == &APFloat::IEEEdouble()) {
13650b57cec5SDimitry Andric       // We would like to output the FP constant value in exponential notation,
13660b57cec5SDimitry Andric       // but we cannot do this if doing so will lose precision.  Check here to
13670b57cec5SDimitry Andric       // make sure that we only output it in exponential format if we can parse
13680b57cec5SDimitry Andric       // the value back and get the same value.
13690b57cec5SDimitry Andric       //
13700b57cec5SDimitry Andric       bool ignored;
13710b57cec5SDimitry Andric       bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
13720b57cec5SDimitry Andric       bool isInf = APF.isInfinity();
13730b57cec5SDimitry Andric       bool isNaN = APF.isNaN();
13740b57cec5SDimitry Andric       if (!isInf && !isNaN) {
1375fe6060f1SDimitry Andric         double Val = APF.convertToDouble();
13760b57cec5SDimitry Andric         SmallString<128> StrVal;
13770b57cec5SDimitry Andric         APF.toString(StrVal, 6, 0, false);
13780b57cec5SDimitry Andric         // Check to make sure that the stringized number is not some string like
13790b57cec5SDimitry Andric         // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
13800b57cec5SDimitry Andric         // that the string matches the "[-+]?[0-9]" regex.
13810b57cec5SDimitry Andric         //
1382e8d8bef9SDimitry Andric         assert((isDigit(StrVal[0]) || ((StrVal[0] == '-' || StrVal[0] == '+') &&
1383e8d8bef9SDimitry Andric                                        isDigit(StrVal[1]))) &&
13840b57cec5SDimitry Andric                "[-+]?[0-9] regex does not match!");
13850b57cec5SDimitry Andric         // Reparse stringized version!
13860b57cec5SDimitry Andric         if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
13870b57cec5SDimitry Andric           Out << StrVal;
13880b57cec5SDimitry Andric           return;
13890b57cec5SDimitry Andric         }
13900b57cec5SDimitry Andric       }
13910b57cec5SDimitry Andric       // Otherwise we could not reparse it to exactly the same value, so we must
13920b57cec5SDimitry Andric       // output the string in hexadecimal format!  Note that loading and storing
13930b57cec5SDimitry Andric       // floating point types changes the bits of NaNs on some hosts, notably
13940b57cec5SDimitry Andric       // x86, so we must not use these types.
13950b57cec5SDimitry Andric       static_assert(sizeof(double) == sizeof(uint64_t),
13960b57cec5SDimitry Andric                     "assuming that double is 64 bits!");
13970b57cec5SDimitry Andric       APFloat apf = APF;
13980b57cec5SDimitry Andric       // Floats are represented in ASCII IR as double, convert.
1399e8d8bef9SDimitry Andric       // FIXME: We should allow 32-bit hex float and remove this.
1400e8d8bef9SDimitry Andric       if (!isDouble) {
1401e8d8bef9SDimitry Andric         // A signaling NaN is quieted on conversion, so we need to recreate the
1402e8d8bef9SDimitry Andric         // expected value after convert (quiet bit of the payload is clear).
1403e8d8bef9SDimitry Andric         bool IsSNAN = apf.isSignaling();
14040b57cec5SDimitry Andric         apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
14050b57cec5SDimitry Andric                     &ignored);
1406e8d8bef9SDimitry Andric         if (IsSNAN) {
1407e8d8bef9SDimitry Andric           APInt Payload = apf.bitcastToAPInt();
1408e8d8bef9SDimitry Andric           apf = APFloat::getSNaN(APFloat::IEEEdouble(), apf.isNegative(),
1409e8d8bef9SDimitry Andric                                  &Payload);
1410e8d8bef9SDimitry Andric         }
1411e8d8bef9SDimitry Andric       }
14120b57cec5SDimitry Andric       Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
14130b57cec5SDimitry Andric       return;
14140b57cec5SDimitry Andric     }
14150b57cec5SDimitry Andric 
14165ffd83dbSDimitry Andric     // Either half, bfloat or some form of long double.
14170b57cec5SDimitry Andric     // These appear as a magic letter identifying the type, then a
14180b57cec5SDimitry Andric     // fixed number of hex digits.
14190b57cec5SDimitry Andric     Out << "0x";
14200b57cec5SDimitry Andric     APInt API = APF.bitcastToAPInt();
14210b57cec5SDimitry Andric     if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
14220b57cec5SDimitry Andric       Out << 'K';
14230b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
14240b57cec5SDimitry Andric                                   /*Upper=*/true);
14250b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
14260b57cec5SDimitry Andric                                   /*Upper=*/true);
14270b57cec5SDimitry Andric       return;
14280b57cec5SDimitry Andric     } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
14290b57cec5SDimitry Andric       Out << 'L';
14300b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
14310b57cec5SDimitry Andric                                   /*Upper=*/true);
14320b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
14330b57cec5SDimitry Andric                                   /*Upper=*/true);
14340b57cec5SDimitry Andric     } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
14350b57cec5SDimitry Andric       Out << 'M';
14360b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
14370b57cec5SDimitry Andric                                   /*Upper=*/true);
14380b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
14390b57cec5SDimitry Andric                                   /*Upper=*/true);
14400b57cec5SDimitry Andric     } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
14410b57cec5SDimitry Andric       Out << 'H';
14420b57cec5SDimitry Andric       Out << format_hex_no_prefix(API.getZExtValue(), 4,
14430b57cec5SDimitry Andric                                   /*Upper=*/true);
14445ffd83dbSDimitry Andric     } else if (&APF.getSemantics() == &APFloat::BFloat()) {
14455ffd83dbSDimitry Andric       Out << 'R';
14465ffd83dbSDimitry Andric       Out << format_hex_no_prefix(API.getZExtValue(), 4,
14475ffd83dbSDimitry Andric                                   /*Upper=*/true);
14480b57cec5SDimitry Andric     } else
14490b57cec5SDimitry Andric       llvm_unreachable("Unsupported floating point type");
14500b57cec5SDimitry Andric     return;
14510b57cec5SDimitry Andric   }
14520b57cec5SDimitry Andric 
14530b57cec5SDimitry Andric   if (isa<ConstantAggregateZero>(CV)) {
14540b57cec5SDimitry Andric     Out << "zeroinitializer";
14550b57cec5SDimitry Andric     return;
14560b57cec5SDimitry Andric   }
14570b57cec5SDimitry Andric 
14580b57cec5SDimitry Andric   if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
14590b57cec5SDimitry Andric     Out << "blockaddress(";
1460*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, BA->getFunction(), WriterCtx);
14610b57cec5SDimitry Andric     Out << ", ";
1462*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, BA->getBasicBlock(), WriterCtx);
14630b57cec5SDimitry Andric     Out << ")";
14640b57cec5SDimitry Andric     return;
14650b57cec5SDimitry Andric   }
14660b57cec5SDimitry Andric 
1467e8d8bef9SDimitry Andric   if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) {
1468e8d8bef9SDimitry Andric     Out << "dso_local_equivalent ";
1469*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, Equiv->getGlobalValue(), WriterCtx);
1470e8d8bef9SDimitry Andric     return;
1471e8d8bef9SDimitry Andric   }
1472e8d8bef9SDimitry Andric 
14730b57cec5SDimitry Andric   if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
14740b57cec5SDimitry Andric     Type *ETy = CA->getType()->getElementType();
14750b57cec5SDimitry Andric     Out << '[';
1476*349cc55cSDimitry Andric     WriterCtx.TypePrinter->print(ETy, Out);
14770b57cec5SDimitry Andric     Out << ' ';
1478*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, CA->getOperand(0), WriterCtx);
14790b57cec5SDimitry Andric     for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
14800b57cec5SDimitry Andric       Out << ", ";
1481*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(ETy, Out);
14820b57cec5SDimitry Andric       Out << ' ';
1483*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, CA->getOperand(i), WriterCtx);
14840b57cec5SDimitry Andric     }
14850b57cec5SDimitry Andric     Out << ']';
14860b57cec5SDimitry Andric     return;
14870b57cec5SDimitry Andric   }
14880b57cec5SDimitry Andric 
14890b57cec5SDimitry Andric   if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
14900b57cec5SDimitry Andric     // As a special case, print the array as a string if it is an array of
14910b57cec5SDimitry Andric     // i8 with ConstantInt values.
14920b57cec5SDimitry Andric     if (CA->isString()) {
14930b57cec5SDimitry Andric       Out << "c\"";
14940b57cec5SDimitry Andric       printEscapedString(CA->getAsString(), Out);
14950b57cec5SDimitry Andric       Out << '"';
14960b57cec5SDimitry Andric       return;
14970b57cec5SDimitry Andric     }
14980b57cec5SDimitry Andric 
14990b57cec5SDimitry Andric     Type *ETy = CA->getType()->getElementType();
15000b57cec5SDimitry Andric     Out << '[';
1501*349cc55cSDimitry Andric     WriterCtx.TypePrinter->print(ETy, Out);
15020b57cec5SDimitry Andric     Out << ' ';
1503*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx);
15040b57cec5SDimitry Andric     for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
15050b57cec5SDimitry Andric       Out << ", ";
1506*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(ETy, Out);
15070b57cec5SDimitry Andric       Out << ' ';
1508*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, CA->getElementAsConstant(i), WriterCtx);
15090b57cec5SDimitry Andric     }
15100b57cec5SDimitry Andric     Out << ']';
15110b57cec5SDimitry Andric     return;
15120b57cec5SDimitry Andric   }
15130b57cec5SDimitry Andric 
15140b57cec5SDimitry Andric   if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
15150b57cec5SDimitry Andric     if (CS->getType()->isPacked())
15160b57cec5SDimitry Andric       Out << '<';
15170b57cec5SDimitry Andric     Out << '{';
15180b57cec5SDimitry Andric     unsigned N = CS->getNumOperands();
15190b57cec5SDimitry Andric     if (N) {
15200b57cec5SDimitry Andric       Out << ' ';
1521*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(CS->getOperand(0)->getType(), Out);
15220b57cec5SDimitry Andric       Out << ' ';
15230b57cec5SDimitry Andric 
1524*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, CS->getOperand(0), WriterCtx);
15250b57cec5SDimitry Andric 
15260b57cec5SDimitry Andric       for (unsigned i = 1; i < N; i++) {
15270b57cec5SDimitry Andric         Out << ", ";
1528*349cc55cSDimitry Andric         WriterCtx.TypePrinter->print(CS->getOperand(i)->getType(), Out);
15290b57cec5SDimitry Andric         Out << ' ';
15300b57cec5SDimitry Andric 
1531*349cc55cSDimitry Andric         WriteAsOperandInternal(Out, CS->getOperand(i), WriterCtx);
15320b57cec5SDimitry Andric       }
15330b57cec5SDimitry Andric       Out << ' ';
15340b57cec5SDimitry Andric     }
15350b57cec5SDimitry Andric 
15360b57cec5SDimitry Andric     Out << '}';
15370b57cec5SDimitry Andric     if (CS->getType()->isPacked())
15380b57cec5SDimitry Andric       Out << '>';
15390b57cec5SDimitry Andric     return;
15400b57cec5SDimitry Andric   }
15410b57cec5SDimitry Andric 
15420b57cec5SDimitry Andric   if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
1543e8d8bef9SDimitry Andric     auto *CVVTy = cast<FixedVectorType>(CV->getType());
15445ffd83dbSDimitry Andric     Type *ETy = CVVTy->getElementType();
15450b57cec5SDimitry Andric     Out << '<';
1546*349cc55cSDimitry Andric     WriterCtx.TypePrinter->print(ETy, Out);
15470b57cec5SDimitry Andric     Out << ' ';
1548*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, CV->getAggregateElement(0U), WriterCtx);
15495ffd83dbSDimitry Andric     for (unsigned i = 1, e = CVVTy->getNumElements(); i != e; ++i) {
15500b57cec5SDimitry Andric       Out << ", ";
1551*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(ETy, Out);
15520b57cec5SDimitry Andric       Out << ' ';
1553*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, CV->getAggregateElement(i), WriterCtx);
15540b57cec5SDimitry Andric     }
15550b57cec5SDimitry Andric     Out << '>';
15560b57cec5SDimitry Andric     return;
15570b57cec5SDimitry Andric   }
15580b57cec5SDimitry Andric 
15590b57cec5SDimitry Andric   if (isa<ConstantPointerNull>(CV)) {
15600b57cec5SDimitry Andric     Out << "null";
15610b57cec5SDimitry Andric     return;
15620b57cec5SDimitry Andric   }
15630b57cec5SDimitry Andric 
15640b57cec5SDimitry Andric   if (isa<ConstantTokenNone>(CV)) {
15650b57cec5SDimitry Andric     Out << "none";
15660b57cec5SDimitry Andric     return;
15670b57cec5SDimitry Andric   }
15680b57cec5SDimitry Andric 
1569e8d8bef9SDimitry Andric   if (isa<PoisonValue>(CV)) {
1570e8d8bef9SDimitry Andric     Out << "poison";
1571e8d8bef9SDimitry Andric     return;
1572e8d8bef9SDimitry Andric   }
1573e8d8bef9SDimitry Andric 
15740b57cec5SDimitry Andric   if (isa<UndefValue>(CV)) {
15750b57cec5SDimitry Andric     Out << "undef";
15760b57cec5SDimitry Andric     return;
15770b57cec5SDimitry Andric   }
15780b57cec5SDimitry Andric 
15790b57cec5SDimitry Andric   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
15800b57cec5SDimitry Andric     Out << CE->getOpcodeName();
15810b57cec5SDimitry Andric     WriteOptimizationInfo(Out, CE);
15820b57cec5SDimitry Andric     if (CE->isCompare())
15830b57cec5SDimitry Andric       Out << ' ' << CmpInst::getPredicateName(
15840b57cec5SDimitry Andric                         static_cast<CmpInst::Predicate>(CE->getPredicate()));
15850b57cec5SDimitry Andric     Out << " (";
15860b57cec5SDimitry Andric 
15870b57cec5SDimitry Andric     Optional<unsigned> InRangeOp;
15880b57cec5SDimitry Andric     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
1589*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(GEP->getSourceElementType(), Out);
15900b57cec5SDimitry Andric       Out << ", ";
15910b57cec5SDimitry Andric       InRangeOp = GEP->getInRangeIndex();
15920b57cec5SDimitry Andric       if (InRangeOp)
15930b57cec5SDimitry Andric         ++*InRangeOp;
15940b57cec5SDimitry Andric     }
15950b57cec5SDimitry Andric 
15960b57cec5SDimitry Andric     for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
15970b57cec5SDimitry Andric       if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
15980b57cec5SDimitry Andric         Out << "inrange ";
1599*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print((*OI)->getType(), Out);
16000b57cec5SDimitry Andric       Out << ' ';
1601*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, *OI, WriterCtx);
16020b57cec5SDimitry Andric       if (OI+1 != CE->op_end())
16030b57cec5SDimitry Andric         Out << ", ";
16040b57cec5SDimitry Andric     }
16050b57cec5SDimitry Andric 
16060b57cec5SDimitry Andric     if (CE->hasIndices()) {
16070b57cec5SDimitry Andric       ArrayRef<unsigned> Indices = CE->getIndices();
16080b57cec5SDimitry Andric       for (unsigned i = 0, e = Indices.size(); i != e; ++i)
16090b57cec5SDimitry Andric         Out << ", " << Indices[i];
16100b57cec5SDimitry Andric     }
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric     if (CE->isCast()) {
16130b57cec5SDimitry Andric       Out << " to ";
1614*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(CE->getType(), Out);
16150b57cec5SDimitry Andric     }
16160b57cec5SDimitry Andric 
16175ffd83dbSDimitry Andric     if (CE->getOpcode() == Instruction::ShuffleVector)
16185ffd83dbSDimitry Andric       PrintShuffleMask(Out, CE->getType(), CE->getShuffleMask());
16195ffd83dbSDimitry Andric 
16200b57cec5SDimitry Andric     Out << ')';
16210b57cec5SDimitry Andric     return;
16220b57cec5SDimitry Andric   }
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   Out << "<placeholder or erroneous Constant>";
16250b57cec5SDimitry Andric }
16260b57cec5SDimitry Andric 
16270b57cec5SDimitry Andric static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
1628*349cc55cSDimitry Andric                          AsmWriterContext &WriterCtx) {
16290b57cec5SDimitry Andric   Out << "!{";
16300b57cec5SDimitry Andric   for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
16310b57cec5SDimitry Andric     const Metadata *MD = Node->getOperand(mi);
16320b57cec5SDimitry Andric     if (!MD)
16330b57cec5SDimitry Andric       Out << "null";
16340b57cec5SDimitry Andric     else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
16350b57cec5SDimitry Andric       Value *V = MDV->getValue();
1636*349cc55cSDimitry Andric       WriterCtx.TypePrinter->print(V->getType(), Out);
16370b57cec5SDimitry Andric       Out << ' ';
1638*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, V, WriterCtx);
16390b57cec5SDimitry Andric     } else {
1640*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, MD, WriterCtx);
1641*349cc55cSDimitry Andric       WriterCtx.onWriteMetadataAsOperand(MD);
16420b57cec5SDimitry Andric     }
16430b57cec5SDimitry Andric     if (mi + 1 != me)
16440b57cec5SDimitry Andric       Out << ", ";
16450b57cec5SDimitry Andric   }
16460b57cec5SDimitry Andric 
16470b57cec5SDimitry Andric   Out << "}";
16480b57cec5SDimitry Andric }
16490b57cec5SDimitry Andric 
16500b57cec5SDimitry Andric namespace {
16510b57cec5SDimitry Andric 
16520b57cec5SDimitry Andric struct FieldSeparator {
16530b57cec5SDimitry Andric   bool Skip = true;
16540b57cec5SDimitry Andric   const char *Sep;
16550b57cec5SDimitry Andric 
16560b57cec5SDimitry Andric   FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
16570b57cec5SDimitry Andric };
16580b57cec5SDimitry Andric 
16590b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
16600b57cec5SDimitry Andric   if (FS.Skip) {
16610b57cec5SDimitry Andric     FS.Skip = false;
16620b57cec5SDimitry Andric     return OS;
16630b57cec5SDimitry Andric   }
16640b57cec5SDimitry Andric   return OS << FS.Sep;
16650b57cec5SDimitry Andric }
16660b57cec5SDimitry Andric 
16670b57cec5SDimitry Andric struct MDFieldPrinter {
16680b57cec5SDimitry Andric   raw_ostream &Out;
16690b57cec5SDimitry Andric   FieldSeparator FS;
1670*349cc55cSDimitry Andric   AsmWriterContext &WriterCtx;
16710b57cec5SDimitry Andric 
1672*349cc55cSDimitry Andric   explicit MDFieldPrinter(raw_ostream &Out)
1673*349cc55cSDimitry Andric       : Out(Out), WriterCtx(AsmWriterContext::getEmpty()) {}
1674*349cc55cSDimitry Andric   MDFieldPrinter(raw_ostream &Out, AsmWriterContext &Ctx)
1675*349cc55cSDimitry Andric       : Out(Out), WriterCtx(Ctx) {}
16760b57cec5SDimitry Andric 
16770b57cec5SDimitry Andric   void printTag(const DINode *N);
16780b57cec5SDimitry Andric   void printMacinfoType(const DIMacroNode *N);
16790b57cec5SDimitry Andric   void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
16800b57cec5SDimitry Andric   void printString(StringRef Name, StringRef Value,
16810b57cec5SDimitry Andric                    bool ShouldSkipEmpty = true);
16820b57cec5SDimitry Andric   void printMetadata(StringRef Name, const Metadata *MD,
16830b57cec5SDimitry Andric                      bool ShouldSkipNull = true);
16840b57cec5SDimitry Andric   template <class IntTy>
16850b57cec5SDimitry Andric   void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
16865ffd83dbSDimitry Andric   void printAPInt(StringRef Name, const APInt &Int, bool IsUnsigned,
16875ffd83dbSDimitry Andric                   bool ShouldSkipZero);
16880b57cec5SDimitry Andric   void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
16890b57cec5SDimitry Andric   void printDIFlags(StringRef Name, DINode::DIFlags Flags);
16900b57cec5SDimitry Andric   void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags);
16910b57cec5SDimitry Andric   template <class IntTy, class Stringifier>
16920b57cec5SDimitry Andric   void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
16930b57cec5SDimitry Andric                       bool ShouldSkipZero = true);
16940b57cec5SDimitry Andric   void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
16950b57cec5SDimitry Andric   void printNameTableKind(StringRef Name,
16960b57cec5SDimitry Andric                           DICompileUnit::DebugNameTableKind NTK);
16970b57cec5SDimitry Andric };
16980b57cec5SDimitry Andric 
16990b57cec5SDimitry Andric } // end anonymous namespace
17000b57cec5SDimitry Andric 
17010b57cec5SDimitry Andric void MDFieldPrinter::printTag(const DINode *N) {
17020b57cec5SDimitry Andric   Out << FS << "tag: ";
17030b57cec5SDimitry Andric   auto Tag = dwarf::TagString(N->getTag());
17040b57cec5SDimitry Andric   if (!Tag.empty())
17050b57cec5SDimitry Andric     Out << Tag;
17060b57cec5SDimitry Andric   else
17070b57cec5SDimitry Andric     Out << N->getTag();
17080b57cec5SDimitry Andric }
17090b57cec5SDimitry Andric 
17100b57cec5SDimitry Andric void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
17110b57cec5SDimitry Andric   Out << FS << "type: ";
17120b57cec5SDimitry Andric   auto Type = dwarf::MacinfoString(N->getMacinfoType());
17130b57cec5SDimitry Andric   if (!Type.empty())
17140b57cec5SDimitry Andric     Out << Type;
17150b57cec5SDimitry Andric   else
17160b57cec5SDimitry Andric     Out << N->getMacinfoType();
17170b57cec5SDimitry Andric }
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric void MDFieldPrinter::printChecksum(
17200b57cec5SDimitry Andric     const DIFile::ChecksumInfo<StringRef> &Checksum) {
17210b57cec5SDimitry Andric   Out << FS << "checksumkind: " << Checksum.getKindAsString();
17220b57cec5SDimitry Andric   printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
17230b57cec5SDimitry Andric }
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric void MDFieldPrinter::printString(StringRef Name, StringRef Value,
17260b57cec5SDimitry Andric                                  bool ShouldSkipEmpty) {
17270b57cec5SDimitry Andric   if (ShouldSkipEmpty && Value.empty())
17280b57cec5SDimitry Andric     return;
17290b57cec5SDimitry Andric 
17300b57cec5SDimitry Andric   Out << FS << Name << ": \"";
17310b57cec5SDimitry Andric   printEscapedString(Value, Out);
17320b57cec5SDimitry Andric   Out << "\"";
17330b57cec5SDimitry Andric }
17340b57cec5SDimitry Andric 
17350b57cec5SDimitry Andric static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
1736*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx) {
17370b57cec5SDimitry Andric   if (!MD) {
17380b57cec5SDimitry Andric     Out << "null";
17390b57cec5SDimitry Andric     return;
17400b57cec5SDimitry Andric   }
1741*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, MD, WriterCtx);
1742*349cc55cSDimitry Andric   WriterCtx.onWriteMetadataAsOperand(MD);
17430b57cec5SDimitry Andric }
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
17460b57cec5SDimitry Andric                                    bool ShouldSkipNull) {
17470b57cec5SDimitry Andric   if (ShouldSkipNull && !MD)
17480b57cec5SDimitry Andric     return;
17490b57cec5SDimitry Andric 
17500b57cec5SDimitry Andric   Out << FS << Name << ": ";
1751*349cc55cSDimitry Andric   writeMetadataAsOperand(Out, MD, WriterCtx);
17520b57cec5SDimitry Andric }
17530b57cec5SDimitry Andric 
17540b57cec5SDimitry Andric template <class IntTy>
17550b57cec5SDimitry Andric void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
17560b57cec5SDimitry Andric   if (ShouldSkipZero && !Int)
17570b57cec5SDimitry Andric     return;
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric   Out << FS << Name << ": " << Int;
17600b57cec5SDimitry Andric }
17610b57cec5SDimitry Andric 
17625ffd83dbSDimitry Andric void MDFieldPrinter::printAPInt(StringRef Name, const APInt &Int,
17635ffd83dbSDimitry Andric                                 bool IsUnsigned, bool ShouldSkipZero) {
1764*349cc55cSDimitry Andric   if (ShouldSkipZero && Int.isZero())
17655ffd83dbSDimitry Andric     return;
17665ffd83dbSDimitry Andric 
17675ffd83dbSDimitry Andric   Out << FS << Name << ": ";
17685ffd83dbSDimitry Andric   Int.print(Out, !IsUnsigned);
17695ffd83dbSDimitry Andric }
17705ffd83dbSDimitry Andric 
17710b57cec5SDimitry Andric void MDFieldPrinter::printBool(StringRef Name, bool Value,
17720b57cec5SDimitry Andric                                Optional<bool> Default) {
17730b57cec5SDimitry Andric   if (Default && Value == *Default)
17740b57cec5SDimitry Andric     return;
17750b57cec5SDimitry Andric   Out << FS << Name << ": " << (Value ? "true" : "false");
17760b57cec5SDimitry Andric }
17770b57cec5SDimitry Andric 
17780b57cec5SDimitry Andric void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
17790b57cec5SDimitry Andric   if (!Flags)
17800b57cec5SDimitry Andric     return;
17810b57cec5SDimitry Andric 
17820b57cec5SDimitry Andric   Out << FS << Name << ": ";
17830b57cec5SDimitry Andric 
17840b57cec5SDimitry Andric   SmallVector<DINode::DIFlags, 8> SplitFlags;
17850b57cec5SDimitry Andric   auto Extra = DINode::splitFlags(Flags, SplitFlags);
17860b57cec5SDimitry Andric 
17870b57cec5SDimitry Andric   FieldSeparator FlagsFS(" | ");
17880b57cec5SDimitry Andric   for (auto F : SplitFlags) {
17890b57cec5SDimitry Andric     auto StringF = DINode::getFlagString(F);
17900b57cec5SDimitry Andric     assert(!StringF.empty() && "Expected valid flag");
17910b57cec5SDimitry Andric     Out << FlagsFS << StringF;
17920b57cec5SDimitry Andric   }
17930b57cec5SDimitry Andric   if (Extra || SplitFlags.empty())
17940b57cec5SDimitry Andric     Out << FlagsFS << Extra;
17950b57cec5SDimitry Andric }
17960b57cec5SDimitry Andric 
17970b57cec5SDimitry Andric void MDFieldPrinter::printDISPFlags(StringRef Name,
17980b57cec5SDimitry Andric                                     DISubprogram::DISPFlags Flags) {
17990b57cec5SDimitry Andric   // Always print this field, because no flags in the IR at all will be
18000b57cec5SDimitry Andric   // interpreted as old-style isDefinition: true.
18010b57cec5SDimitry Andric   Out << FS << Name << ": ";
18020b57cec5SDimitry Andric 
18030b57cec5SDimitry Andric   if (!Flags) {
18040b57cec5SDimitry Andric     Out << 0;
18050b57cec5SDimitry Andric     return;
18060b57cec5SDimitry Andric   }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric   SmallVector<DISubprogram::DISPFlags, 8> SplitFlags;
18090b57cec5SDimitry Andric   auto Extra = DISubprogram::splitFlags(Flags, SplitFlags);
18100b57cec5SDimitry Andric 
18110b57cec5SDimitry Andric   FieldSeparator FlagsFS(" | ");
18120b57cec5SDimitry Andric   for (auto F : SplitFlags) {
18130b57cec5SDimitry Andric     auto StringF = DISubprogram::getFlagString(F);
18140b57cec5SDimitry Andric     assert(!StringF.empty() && "Expected valid flag");
18150b57cec5SDimitry Andric     Out << FlagsFS << StringF;
18160b57cec5SDimitry Andric   }
18170b57cec5SDimitry Andric   if (Extra || SplitFlags.empty())
18180b57cec5SDimitry Andric     Out << FlagsFS << Extra;
18190b57cec5SDimitry Andric }
18200b57cec5SDimitry Andric 
18210b57cec5SDimitry Andric void MDFieldPrinter::printEmissionKind(StringRef Name,
18220b57cec5SDimitry Andric                                        DICompileUnit::DebugEmissionKind EK) {
18230b57cec5SDimitry Andric   Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
18240b57cec5SDimitry Andric }
18250b57cec5SDimitry Andric 
18260b57cec5SDimitry Andric void MDFieldPrinter::printNameTableKind(StringRef Name,
18270b57cec5SDimitry Andric                                         DICompileUnit::DebugNameTableKind NTK) {
18280b57cec5SDimitry Andric   if (NTK == DICompileUnit::DebugNameTableKind::Default)
18290b57cec5SDimitry Andric     return;
18300b57cec5SDimitry Andric   Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK);
18310b57cec5SDimitry Andric }
18320b57cec5SDimitry Andric 
18330b57cec5SDimitry Andric template <class IntTy, class Stringifier>
18340b57cec5SDimitry Andric void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
18350b57cec5SDimitry Andric                                     Stringifier toString, bool ShouldSkipZero) {
18360b57cec5SDimitry Andric   if (!Value)
18370b57cec5SDimitry Andric     return;
18380b57cec5SDimitry Andric 
18390b57cec5SDimitry Andric   Out << FS << Name << ": ";
18400b57cec5SDimitry Andric   auto S = toString(Value);
18410b57cec5SDimitry Andric   if (!S.empty())
18420b57cec5SDimitry Andric     Out << S;
18430b57cec5SDimitry Andric   else
18440b57cec5SDimitry Andric     Out << Value;
18450b57cec5SDimitry Andric }
18460b57cec5SDimitry Andric 
18470b57cec5SDimitry Andric static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
1848*349cc55cSDimitry Andric                                AsmWriterContext &WriterCtx) {
18490b57cec5SDimitry Andric   Out << "!GenericDINode(";
1850*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
18510b57cec5SDimitry Andric   Printer.printTag(N);
18520b57cec5SDimitry Andric   Printer.printString("header", N->getHeader());
18530b57cec5SDimitry Andric   if (N->getNumDwarfOperands()) {
18540b57cec5SDimitry Andric     Out << Printer.FS << "operands: {";
18550b57cec5SDimitry Andric     FieldSeparator IFS;
18560b57cec5SDimitry Andric     for (auto &I : N->dwarf_operands()) {
18570b57cec5SDimitry Andric       Out << IFS;
1858*349cc55cSDimitry Andric       writeMetadataAsOperand(Out, I, WriterCtx);
18590b57cec5SDimitry Andric     }
18600b57cec5SDimitry Andric     Out << "}";
18610b57cec5SDimitry Andric   }
18620b57cec5SDimitry Andric   Out << ")";
18630b57cec5SDimitry Andric }
18640b57cec5SDimitry Andric 
18650b57cec5SDimitry Andric static void writeDILocation(raw_ostream &Out, const DILocation *DL,
1866*349cc55cSDimitry Andric                             AsmWriterContext &WriterCtx) {
18670b57cec5SDimitry Andric   Out << "!DILocation(";
1868*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
18690b57cec5SDimitry Andric   // Always output the line, since 0 is a relevant and important value for it.
18700b57cec5SDimitry Andric   Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
18710b57cec5SDimitry Andric   Printer.printInt("column", DL->getColumn());
18720b57cec5SDimitry Andric   Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
18730b57cec5SDimitry Andric   Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
18740b57cec5SDimitry Andric   Printer.printBool("isImplicitCode", DL->isImplicitCode(),
18750b57cec5SDimitry Andric                     /* Default */ false);
18760b57cec5SDimitry Andric   Out << ")";
18770b57cec5SDimitry Andric }
18780b57cec5SDimitry Andric 
18790b57cec5SDimitry Andric static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
1880*349cc55cSDimitry Andric                             AsmWriterContext &WriterCtx) {
18810b57cec5SDimitry Andric   Out << "!DISubrange(";
1882*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
1883fe6060f1SDimitry Andric 
1884fe6060f1SDimitry Andric   auto *Count = N->getRawCountNode();
1885fe6060f1SDimitry Andric   if (auto *CE = dyn_cast_or_null<ConstantAsMetadata>(Count)) {
1886fe6060f1SDimitry Andric     auto *CV = cast<ConstantInt>(CE->getValue());
1887fe6060f1SDimitry Andric     Printer.printInt("count", CV->getSExtValue(),
1888fe6060f1SDimitry Andric                      /* ShouldSkipZero */ false);
1889fe6060f1SDimitry Andric   } else
1890fe6060f1SDimitry Andric     Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
18915ffd83dbSDimitry Andric 
18925ffd83dbSDimitry Andric   // A lowerBound of constant 0 should not be skipped, since it is different
18935ffd83dbSDimitry Andric   // from an unspecified lower bound (= nullptr).
18945ffd83dbSDimitry Andric   auto *LBound = N->getRawLowerBound();
18955ffd83dbSDimitry Andric   if (auto *LE = dyn_cast_or_null<ConstantAsMetadata>(LBound)) {
18965ffd83dbSDimitry Andric     auto *LV = cast<ConstantInt>(LE->getValue());
18975ffd83dbSDimitry Andric     Printer.printInt("lowerBound", LV->getSExtValue(),
18985ffd83dbSDimitry Andric                      /* ShouldSkipZero */ false);
18995ffd83dbSDimitry Andric   } else
19005ffd83dbSDimitry Andric     Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
19015ffd83dbSDimitry Andric 
19025ffd83dbSDimitry Andric   auto *UBound = N->getRawUpperBound();
19035ffd83dbSDimitry Andric   if (auto *UE = dyn_cast_or_null<ConstantAsMetadata>(UBound)) {
19045ffd83dbSDimitry Andric     auto *UV = cast<ConstantInt>(UE->getValue());
19055ffd83dbSDimitry Andric     Printer.printInt("upperBound", UV->getSExtValue(),
19065ffd83dbSDimitry Andric                      /* ShouldSkipZero */ false);
19075ffd83dbSDimitry Andric   } else
19085ffd83dbSDimitry Andric     Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
19095ffd83dbSDimitry Andric 
19105ffd83dbSDimitry Andric   auto *Stride = N->getRawStride();
19115ffd83dbSDimitry Andric   if (auto *SE = dyn_cast_or_null<ConstantAsMetadata>(Stride)) {
19125ffd83dbSDimitry Andric     auto *SV = cast<ConstantInt>(SE->getValue());
19135ffd83dbSDimitry Andric     Printer.printInt("stride", SV->getSExtValue(), /* ShouldSkipZero */ false);
19145ffd83dbSDimitry Andric   } else
19155ffd83dbSDimitry Andric     Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);
19165ffd83dbSDimitry Andric 
19170b57cec5SDimitry Andric   Out << ")";
19180b57cec5SDimitry Andric }
19190b57cec5SDimitry Andric 
1920e8d8bef9SDimitry Andric static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N,
1921*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx) {
1922e8d8bef9SDimitry Andric   Out << "!DIGenericSubrange(";
1923*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
1924e8d8bef9SDimitry Andric 
1925e8d8bef9SDimitry Andric   auto IsConstant = [&](Metadata *Bound) -> bool {
1926e8d8bef9SDimitry Andric     if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) {
1927*349cc55cSDimitry Andric       return BE->isConstant() &&
1928*349cc55cSDimitry Andric              DIExpression::SignedOrUnsignedConstant::SignedConstant ==
1929*349cc55cSDimitry Andric                  *BE->isConstant();
1930e8d8bef9SDimitry Andric     }
1931e8d8bef9SDimitry Andric     return false;
1932e8d8bef9SDimitry Andric   };
1933e8d8bef9SDimitry Andric 
1934e8d8bef9SDimitry Andric   auto GetConstant = [&](Metadata *Bound) -> int64_t {
1935e8d8bef9SDimitry Andric     assert(IsConstant(Bound) && "Expected constant");
1936e8d8bef9SDimitry Andric     auto *BE = dyn_cast_or_null<DIExpression>(Bound);
1937e8d8bef9SDimitry Andric     return static_cast<int64_t>(BE->getElement(1));
1938e8d8bef9SDimitry Andric   };
1939e8d8bef9SDimitry Andric 
1940e8d8bef9SDimitry Andric   auto *Count = N->getRawCountNode();
1941e8d8bef9SDimitry Andric   if (IsConstant(Count))
1942e8d8bef9SDimitry Andric     Printer.printInt("count", GetConstant(Count),
1943e8d8bef9SDimitry Andric                      /* ShouldSkipZero */ false);
1944e8d8bef9SDimitry Andric   else
1945e8d8bef9SDimitry Andric     Printer.printMetadata("count", Count, /*ShouldSkipNull */ true);
1946e8d8bef9SDimitry Andric 
1947e8d8bef9SDimitry Andric   auto *LBound = N->getRawLowerBound();
1948e8d8bef9SDimitry Andric   if (IsConstant(LBound))
1949e8d8bef9SDimitry Andric     Printer.printInt("lowerBound", GetConstant(LBound),
1950e8d8bef9SDimitry Andric                      /* ShouldSkipZero */ false);
1951e8d8bef9SDimitry Andric   else
1952e8d8bef9SDimitry Andric     Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true);
1953e8d8bef9SDimitry Andric 
1954e8d8bef9SDimitry Andric   auto *UBound = N->getRawUpperBound();
1955e8d8bef9SDimitry Andric   if (IsConstant(UBound))
1956e8d8bef9SDimitry Andric     Printer.printInt("upperBound", GetConstant(UBound),
1957e8d8bef9SDimitry Andric                      /* ShouldSkipZero */ false);
1958e8d8bef9SDimitry Andric   else
1959e8d8bef9SDimitry Andric     Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true);
1960e8d8bef9SDimitry Andric 
1961e8d8bef9SDimitry Andric   auto *Stride = N->getRawStride();
1962e8d8bef9SDimitry Andric   if (IsConstant(Stride))
1963e8d8bef9SDimitry Andric     Printer.printInt("stride", GetConstant(Stride),
1964e8d8bef9SDimitry Andric                      /* ShouldSkipZero */ false);
1965e8d8bef9SDimitry Andric   else
1966e8d8bef9SDimitry Andric     Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true);
1967e8d8bef9SDimitry Andric 
1968e8d8bef9SDimitry Andric   Out << ")";
1969e8d8bef9SDimitry Andric }
1970e8d8bef9SDimitry Andric 
19710b57cec5SDimitry Andric static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
1972*349cc55cSDimitry Andric                               AsmWriterContext &) {
19730b57cec5SDimitry Andric   Out << "!DIEnumerator(";
19740b57cec5SDimitry Andric   MDFieldPrinter Printer(Out);
19750b57cec5SDimitry Andric   Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
19765ffd83dbSDimitry Andric   Printer.printAPInt("value", N->getValue(), N->isUnsigned(),
19775ffd83dbSDimitry Andric                      /*ShouldSkipZero=*/false);
19785ffd83dbSDimitry Andric   if (N->isUnsigned())
19790b57cec5SDimitry Andric     Printer.printBool("isUnsigned", true);
19800b57cec5SDimitry Andric   Out << ")";
19810b57cec5SDimitry Andric }
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
1984*349cc55cSDimitry Andric                              AsmWriterContext &) {
19850b57cec5SDimitry Andric   Out << "!DIBasicType(";
19860b57cec5SDimitry Andric   MDFieldPrinter Printer(Out);
19870b57cec5SDimitry Andric   if (N->getTag() != dwarf::DW_TAG_base_type)
19880b57cec5SDimitry Andric     Printer.printTag(N);
19890b57cec5SDimitry Andric   Printer.printString("name", N->getName());
19900b57cec5SDimitry Andric   Printer.printInt("size", N->getSizeInBits());
19910b57cec5SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
19920b57cec5SDimitry Andric   Printer.printDwarfEnum("encoding", N->getEncoding(),
19930b57cec5SDimitry Andric                          dwarf::AttributeEncodingString);
19940b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
19950b57cec5SDimitry Andric   Out << ")";
19960b57cec5SDimitry Andric }
19970b57cec5SDimitry Andric 
1998e8d8bef9SDimitry Andric static void writeDIStringType(raw_ostream &Out, const DIStringType *N,
1999*349cc55cSDimitry Andric                               AsmWriterContext &WriterCtx) {
2000e8d8bef9SDimitry Andric   Out << "!DIStringType(";
2001*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
2002e8d8bef9SDimitry Andric   if (N->getTag() != dwarf::DW_TAG_string_type)
2003e8d8bef9SDimitry Andric     Printer.printTag(N);
2004e8d8bef9SDimitry Andric   Printer.printString("name", N->getName());
2005e8d8bef9SDimitry Andric   Printer.printMetadata("stringLength", N->getRawStringLength());
2006e8d8bef9SDimitry Andric   Printer.printMetadata("stringLengthExpression", N->getRawStringLengthExp());
2007e8d8bef9SDimitry Andric   Printer.printInt("size", N->getSizeInBits());
2008e8d8bef9SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
2009e8d8bef9SDimitry Andric   Printer.printDwarfEnum("encoding", N->getEncoding(),
2010e8d8bef9SDimitry Andric                          dwarf::AttributeEncodingString);
2011e8d8bef9SDimitry Andric   Out << ")";
2012e8d8bef9SDimitry Andric }
2013e8d8bef9SDimitry Andric 
20140b57cec5SDimitry Andric static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
2015*349cc55cSDimitry Andric                                AsmWriterContext &WriterCtx) {
20160b57cec5SDimitry Andric   Out << "!DIDerivedType(";
2017*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
20180b57cec5SDimitry Andric   Printer.printTag(N);
20190b57cec5SDimitry Andric   Printer.printString("name", N->getName());
20200b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope());
20210b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
20220b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
20230b57cec5SDimitry Andric   Printer.printMetadata("baseType", N->getRawBaseType(),
20240b57cec5SDimitry Andric                         /* ShouldSkipNull */ false);
20250b57cec5SDimitry Andric   Printer.printInt("size", N->getSizeInBits());
20260b57cec5SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
20270b57cec5SDimitry Andric   Printer.printInt("offset", N->getOffsetInBits());
20280b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
20290b57cec5SDimitry Andric   Printer.printMetadata("extraData", N->getRawExtraData());
20300b57cec5SDimitry Andric   if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
20310b57cec5SDimitry Andric     Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
20320b57cec5SDimitry Andric                      /* ShouldSkipZero */ false);
2033*349cc55cSDimitry Andric   Printer.printMetadata("annotations", N->getRawAnnotations());
20340b57cec5SDimitry Andric   Out << ")";
20350b57cec5SDimitry Andric }
20360b57cec5SDimitry Andric 
20370b57cec5SDimitry Andric static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
2038*349cc55cSDimitry Andric                                  AsmWriterContext &WriterCtx) {
20390b57cec5SDimitry Andric   Out << "!DICompositeType(";
2040*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
20410b57cec5SDimitry Andric   Printer.printTag(N);
20420b57cec5SDimitry Andric   Printer.printString("name", N->getName());
20430b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope());
20440b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
20450b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
20460b57cec5SDimitry Andric   Printer.printMetadata("baseType", N->getRawBaseType());
20470b57cec5SDimitry Andric   Printer.printInt("size", N->getSizeInBits());
20480b57cec5SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
20490b57cec5SDimitry Andric   Printer.printInt("offset", N->getOffsetInBits());
20500b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
20510b57cec5SDimitry Andric   Printer.printMetadata("elements", N->getRawElements());
20520b57cec5SDimitry Andric   Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
20530b57cec5SDimitry Andric                          dwarf::LanguageString);
20540b57cec5SDimitry Andric   Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
20550b57cec5SDimitry Andric   Printer.printMetadata("templateParams", N->getRawTemplateParams());
20560b57cec5SDimitry Andric   Printer.printString("identifier", N->getIdentifier());
20570b57cec5SDimitry Andric   Printer.printMetadata("discriminator", N->getRawDiscriminator());
20585ffd83dbSDimitry Andric   Printer.printMetadata("dataLocation", N->getRawDataLocation());
2059e8d8bef9SDimitry Andric   Printer.printMetadata("associated", N->getRawAssociated());
2060e8d8bef9SDimitry Andric   Printer.printMetadata("allocated", N->getRawAllocated());
2061e8d8bef9SDimitry Andric   if (auto *RankConst = N->getRankConst())
2062e8d8bef9SDimitry Andric     Printer.printInt("rank", RankConst->getSExtValue(),
2063e8d8bef9SDimitry Andric                      /* ShouldSkipZero */ false);
2064e8d8bef9SDimitry Andric   else
2065e8d8bef9SDimitry Andric     Printer.printMetadata("rank", N->getRawRank(), /*ShouldSkipNull */ true);
2066*349cc55cSDimitry Andric   Printer.printMetadata("annotations", N->getRawAnnotations());
20670b57cec5SDimitry Andric   Out << ")";
20680b57cec5SDimitry Andric }
20690b57cec5SDimitry Andric 
20700b57cec5SDimitry Andric static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
2071*349cc55cSDimitry Andric                                   AsmWriterContext &WriterCtx) {
20720b57cec5SDimitry Andric   Out << "!DISubroutineType(";
2073*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
20740b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
20750b57cec5SDimitry Andric   Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
20760b57cec5SDimitry Andric   Printer.printMetadata("types", N->getRawTypeArray(),
20770b57cec5SDimitry Andric                         /* ShouldSkipNull */ false);
20780b57cec5SDimitry Andric   Out << ")";
20790b57cec5SDimitry Andric }
20800b57cec5SDimitry Andric 
2081*349cc55cSDimitry Andric static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &) {
20820b57cec5SDimitry Andric   Out << "!DIFile(";
20830b57cec5SDimitry Andric   MDFieldPrinter Printer(Out);
20840b57cec5SDimitry Andric   Printer.printString("filename", N->getFilename(),
20850b57cec5SDimitry Andric                       /* ShouldSkipEmpty */ false);
20860b57cec5SDimitry Andric   Printer.printString("directory", N->getDirectory(),
20870b57cec5SDimitry Andric                       /* ShouldSkipEmpty */ false);
20880b57cec5SDimitry Andric   // Print all values for checksum together, or not at all.
20890b57cec5SDimitry Andric   if (N->getChecksum())
20900b57cec5SDimitry Andric     Printer.printChecksum(*N->getChecksum());
20910b57cec5SDimitry Andric   Printer.printString("source", N->getSource().getValueOr(StringRef()),
20920b57cec5SDimitry Andric                       /* ShouldSkipEmpty */ true);
20930b57cec5SDimitry Andric   Out << ")";
20940b57cec5SDimitry Andric }
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
2097*349cc55cSDimitry Andric                                AsmWriterContext &WriterCtx) {
20980b57cec5SDimitry Andric   Out << "!DICompileUnit(";
2099*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21000b57cec5SDimitry Andric   Printer.printDwarfEnum("language", N->getSourceLanguage(),
21010b57cec5SDimitry Andric                          dwarf::LanguageString, /* ShouldSkipZero */ false);
21020b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
21030b57cec5SDimitry Andric   Printer.printString("producer", N->getProducer());
21040b57cec5SDimitry Andric   Printer.printBool("isOptimized", N->isOptimized());
21050b57cec5SDimitry Andric   Printer.printString("flags", N->getFlags());
21060b57cec5SDimitry Andric   Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
21070b57cec5SDimitry Andric                    /* ShouldSkipZero */ false);
21080b57cec5SDimitry Andric   Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
21090b57cec5SDimitry Andric   Printer.printEmissionKind("emissionKind", N->getEmissionKind());
21100b57cec5SDimitry Andric   Printer.printMetadata("enums", N->getRawEnumTypes());
21110b57cec5SDimitry Andric   Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
21120b57cec5SDimitry Andric   Printer.printMetadata("globals", N->getRawGlobalVariables());
21130b57cec5SDimitry Andric   Printer.printMetadata("imports", N->getRawImportedEntities());
21140b57cec5SDimitry Andric   Printer.printMetadata("macros", N->getRawMacros());
21150b57cec5SDimitry Andric   Printer.printInt("dwoId", N->getDWOId());
21160b57cec5SDimitry Andric   Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
21170b57cec5SDimitry Andric   Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
21180b57cec5SDimitry Andric                     false);
21190b57cec5SDimitry Andric   Printer.printNameTableKind("nameTableKind", N->getNameTableKind());
21200b57cec5SDimitry Andric   Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false);
21215ffd83dbSDimitry Andric   Printer.printString("sysroot", N->getSysRoot());
21225ffd83dbSDimitry Andric   Printer.printString("sdk", N->getSDK());
21230b57cec5SDimitry Andric   Out << ")";
21240b57cec5SDimitry Andric }
21250b57cec5SDimitry Andric 
21260b57cec5SDimitry Andric static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
2127*349cc55cSDimitry Andric                               AsmWriterContext &WriterCtx) {
21280b57cec5SDimitry Andric   Out << "!DISubprogram(";
2129*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21300b57cec5SDimitry Andric   Printer.printString("name", N->getName());
21310b57cec5SDimitry Andric   Printer.printString("linkageName", N->getLinkageName());
21320b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
21330b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
21340b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
21350b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType());
21360b57cec5SDimitry Andric   Printer.printInt("scopeLine", N->getScopeLine());
21370b57cec5SDimitry Andric   Printer.printMetadata("containingType", N->getRawContainingType());
21380b57cec5SDimitry Andric   if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
21390b57cec5SDimitry Andric       N->getVirtualIndex() != 0)
21400b57cec5SDimitry Andric     Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
21410b57cec5SDimitry Andric   Printer.printInt("thisAdjustment", N->getThisAdjustment());
21420b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
21430b57cec5SDimitry Andric   Printer.printDISPFlags("spFlags", N->getSPFlags());
21440b57cec5SDimitry Andric   Printer.printMetadata("unit", N->getRawUnit());
21450b57cec5SDimitry Andric   Printer.printMetadata("templateParams", N->getRawTemplateParams());
21460b57cec5SDimitry Andric   Printer.printMetadata("declaration", N->getRawDeclaration());
21470b57cec5SDimitry Andric   Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
21480b57cec5SDimitry Andric   Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
2149*349cc55cSDimitry Andric   Printer.printMetadata("annotations", N->getRawAnnotations());
21500b57cec5SDimitry Andric   Out << ")";
21510b57cec5SDimitry Andric }
21520b57cec5SDimitry Andric 
21530b57cec5SDimitry Andric static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
2154*349cc55cSDimitry Andric                                 AsmWriterContext &WriterCtx) {
21550b57cec5SDimitry Andric   Out << "!DILexicalBlock(";
2156*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21570b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
21580b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
21590b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
21600b57cec5SDimitry Andric   Printer.printInt("column", N->getColumn());
21610b57cec5SDimitry Andric   Out << ")";
21620b57cec5SDimitry Andric }
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric static void writeDILexicalBlockFile(raw_ostream &Out,
21650b57cec5SDimitry Andric                                     const DILexicalBlockFile *N,
2166*349cc55cSDimitry Andric                                     AsmWriterContext &WriterCtx) {
21670b57cec5SDimitry Andric   Out << "!DILexicalBlockFile(";
2168*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21690b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
21700b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
21710b57cec5SDimitry Andric   Printer.printInt("discriminator", N->getDiscriminator(),
21720b57cec5SDimitry Andric                    /* ShouldSkipZero */ false);
21730b57cec5SDimitry Andric   Out << ")";
21740b57cec5SDimitry Andric }
21750b57cec5SDimitry Andric 
21760b57cec5SDimitry Andric static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
2177*349cc55cSDimitry Andric                              AsmWriterContext &WriterCtx) {
21780b57cec5SDimitry Andric   Out << "!DINamespace(";
2179*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21800b57cec5SDimitry Andric   Printer.printString("name", N->getName());
21810b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
21820b57cec5SDimitry Andric   Printer.printBool("exportSymbols", N->getExportSymbols(), false);
21830b57cec5SDimitry Andric   Out << ")";
21840b57cec5SDimitry Andric }
21850b57cec5SDimitry Andric 
21860b57cec5SDimitry Andric static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N,
2187*349cc55cSDimitry Andric                                AsmWriterContext &WriterCtx) {
21880b57cec5SDimitry Andric   Out << "!DICommonBlock(";
2189*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
21900b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), false);
21910b57cec5SDimitry Andric   Printer.printMetadata("declaration", N->getRawDecl(), false);
21920b57cec5SDimitry Andric   Printer.printString("name", N->getName());
21930b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
21940b57cec5SDimitry Andric   Printer.printInt("line", N->getLineNo());
21950b57cec5SDimitry Andric   Out << ")";
21960b57cec5SDimitry Andric }
21970b57cec5SDimitry Andric 
21980b57cec5SDimitry Andric static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
2199*349cc55cSDimitry Andric                          AsmWriterContext &WriterCtx) {
22000b57cec5SDimitry Andric   Out << "!DIMacro(";
2201*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22020b57cec5SDimitry Andric   Printer.printMacinfoType(N);
22030b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
22040b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22050b57cec5SDimitry Andric   Printer.printString("value", N->getValue());
22060b57cec5SDimitry Andric   Out << ")";
22070b57cec5SDimitry Andric }
22080b57cec5SDimitry Andric 
22090b57cec5SDimitry Andric static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
2210*349cc55cSDimitry Andric                              AsmWriterContext &WriterCtx) {
22110b57cec5SDimitry Andric   Out << "!DIMacroFile(";
2212*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22130b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
22140b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
22150b57cec5SDimitry Andric   Printer.printMetadata("nodes", N->getRawElements());
22160b57cec5SDimitry Andric   Out << ")";
22170b57cec5SDimitry Andric }
22180b57cec5SDimitry Andric 
22190b57cec5SDimitry Andric static void writeDIModule(raw_ostream &Out, const DIModule *N,
2220*349cc55cSDimitry Andric                           AsmWriterContext &WriterCtx) {
22210b57cec5SDimitry Andric   Out << "!DIModule(";
2222*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22230b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
22240b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22250b57cec5SDimitry Andric   Printer.printString("configMacros", N->getConfigurationMacros());
22260b57cec5SDimitry Andric   Printer.printString("includePath", N->getIncludePath());
22275ffd83dbSDimitry Andric   Printer.printString("apinotes", N->getAPINotesFile());
22285ffd83dbSDimitry Andric   Printer.printMetadata("file", N->getRawFile());
22295ffd83dbSDimitry Andric   Printer.printInt("line", N->getLineNo());
2230e8d8bef9SDimitry Andric   Printer.printBool("isDecl", N->getIsDecl(), /* Default */ false);
22310b57cec5SDimitry Andric   Out << ")";
22320b57cec5SDimitry Andric }
22330b57cec5SDimitry Andric 
22340b57cec5SDimitry Andric static void writeDITemplateTypeParameter(raw_ostream &Out,
22350b57cec5SDimitry Andric                                          const DITemplateTypeParameter *N,
2236*349cc55cSDimitry Andric                                          AsmWriterContext &WriterCtx) {
22370b57cec5SDimitry Andric   Out << "!DITemplateTypeParameter(";
2238*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22390b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22400b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
22415ffd83dbSDimitry Andric   Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
22420b57cec5SDimitry Andric   Out << ")";
22430b57cec5SDimitry Andric }
22440b57cec5SDimitry Andric 
22450b57cec5SDimitry Andric static void writeDITemplateValueParameter(raw_ostream &Out,
22460b57cec5SDimitry Andric                                           const DITemplateValueParameter *N,
2247*349cc55cSDimitry Andric                                           AsmWriterContext &WriterCtx) {
22480b57cec5SDimitry Andric   Out << "!DITemplateValueParameter(";
2249*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22500b57cec5SDimitry Andric   if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
22510b57cec5SDimitry Andric     Printer.printTag(N);
22520b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22530b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType());
22545ffd83dbSDimitry Andric   Printer.printBool("defaulted", N->isDefault(), /* Default= */ false);
22550b57cec5SDimitry Andric   Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
22560b57cec5SDimitry Andric   Out << ")";
22570b57cec5SDimitry Andric }
22580b57cec5SDimitry Andric 
22590b57cec5SDimitry Andric static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
2260*349cc55cSDimitry Andric                                   AsmWriterContext &WriterCtx) {
22610b57cec5SDimitry Andric   Out << "!DIGlobalVariable(";
2262*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22630b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22640b57cec5SDimitry Andric   Printer.printString("linkageName", N->getLinkageName());
22650b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
22660b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
22670b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
22680b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType());
22690b57cec5SDimitry Andric   Printer.printBool("isLocal", N->isLocalToUnit());
22700b57cec5SDimitry Andric   Printer.printBool("isDefinition", N->isDefinition());
22710b57cec5SDimitry Andric   Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
22720b57cec5SDimitry Andric   Printer.printMetadata("templateParams", N->getRawTemplateParams());
22730b57cec5SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
2274*349cc55cSDimitry Andric   Printer.printMetadata("annotations", N->getRawAnnotations());
22750b57cec5SDimitry Andric   Out << ")";
22760b57cec5SDimitry Andric }
22770b57cec5SDimitry Andric 
22780b57cec5SDimitry Andric static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
2279*349cc55cSDimitry Andric                                  AsmWriterContext &WriterCtx) {
22800b57cec5SDimitry Andric   Out << "!DILocalVariable(";
2281*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22820b57cec5SDimitry Andric   Printer.printString("name", N->getName());
22830b57cec5SDimitry Andric   Printer.printInt("arg", N->getArg());
22840b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
22850b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
22860b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
22870b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType());
22880b57cec5SDimitry Andric   Printer.printDIFlags("flags", N->getFlags());
22890b57cec5SDimitry Andric   Printer.printInt("align", N->getAlignInBits());
2290*349cc55cSDimitry Andric   Printer.printMetadata("annotations", N->getRawAnnotations());
22910b57cec5SDimitry Andric   Out << ")";
22920b57cec5SDimitry Andric }
22930b57cec5SDimitry Andric 
22940b57cec5SDimitry Andric static void writeDILabel(raw_ostream &Out, const DILabel *N,
2295*349cc55cSDimitry Andric                          AsmWriterContext &WriterCtx) {
22960b57cec5SDimitry Andric   Out << "!DILabel(";
2297*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
22980b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
22990b57cec5SDimitry Andric   Printer.printString("name", N->getName());
23000b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
23010b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
23020b57cec5SDimitry Andric   Out << ")";
23030b57cec5SDimitry Andric }
23040b57cec5SDimitry Andric 
23050b57cec5SDimitry Andric static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
2306*349cc55cSDimitry Andric                               AsmWriterContext &WriterCtx) {
23070b57cec5SDimitry Andric   Out << "!DIExpression(";
23080b57cec5SDimitry Andric   FieldSeparator FS;
23090b57cec5SDimitry Andric   if (N->isValid()) {
2310fe6060f1SDimitry Andric     for (const DIExpression::ExprOperand &Op : N->expr_ops()) {
2311fe6060f1SDimitry Andric       auto OpStr = dwarf::OperationEncodingString(Op.getOp());
23120b57cec5SDimitry Andric       assert(!OpStr.empty() && "Expected valid opcode");
23130b57cec5SDimitry Andric 
23140b57cec5SDimitry Andric       Out << FS << OpStr;
2315fe6060f1SDimitry Andric       if (Op.getOp() == dwarf::DW_OP_LLVM_convert) {
2316fe6060f1SDimitry Andric         Out << FS << Op.getArg(0);
2317fe6060f1SDimitry Andric         Out << FS << dwarf::AttributeEncodingString(Op.getArg(1));
23180b57cec5SDimitry Andric       } else {
2319fe6060f1SDimitry Andric         for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A)
2320fe6060f1SDimitry Andric           Out << FS << Op.getArg(A);
23210b57cec5SDimitry Andric       }
23220b57cec5SDimitry Andric     }
23230b57cec5SDimitry Andric   } else {
23240b57cec5SDimitry Andric     for (const auto &I : N->getElements())
23250b57cec5SDimitry Andric       Out << FS << I;
23260b57cec5SDimitry Andric   }
23270b57cec5SDimitry Andric   Out << ")";
23280b57cec5SDimitry Andric }
23290b57cec5SDimitry Andric 
2330fe6060f1SDimitry Andric static void writeDIArgList(raw_ostream &Out, const DIArgList *N,
2331*349cc55cSDimitry Andric                            AsmWriterContext &WriterCtx,
2332*349cc55cSDimitry Andric                            bool FromValue = false) {
2333fe6060f1SDimitry Andric   assert(FromValue &&
2334fe6060f1SDimitry Andric          "Unexpected DIArgList metadata outside of value argument");
2335fe6060f1SDimitry Andric   Out << "!DIArgList(";
2336fe6060f1SDimitry Andric   FieldSeparator FS;
2337*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
2338fe6060f1SDimitry Andric   for (Metadata *Arg : N->getArgs()) {
2339fe6060f1SDimitry Andric     Out << FS;
2340*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, Arg, WriterCtx, true);
2341fe6060f1SDimitry Andric   }
2342fe6060f1SDimitry Andric   Out << ")";
2343fe6060f1SDimitry Andric }
2344fe6060f1SDimitry Andric 
23450b57cec5SDimitry Andric static void writeDIGlobalVariableExpression(raw_ostream &Out,
23460b57cec5SDimitry Andric                                             const DIGlobalVariableExpression *N,
2347*349cc55cSDimitry Andric                                             AsmWriterContext &WriterCtx) {
23480b57cec5SDimitry Andric   Out << "!DIGlobalVariableExpression(";
2349*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
23500b57cec5SDimitry Andric   Printer.printMetadata("var", N->getVariable());
23510b57cec5SDimitry Andric   Printer.printMetadata("expr", N->getExpression());
23520b57cec5SDimitry Andric   Out << ")";
23530b57cec5SDimitry Andric }
23540b57cec5SDimitry Andric 
23550b57cec5SDimitry Andric static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
2356*349cc55cSDimitry Andric                                 AsmWriterContext &WriterCtx) {
23570b57cec5SDimitry Andric   Out << "!DIObjCProperty(";
2358*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
23590b57cec5SDimitry Andric   Printer.printString("name", N->getName());
23600b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
23610b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
23620b57cec5SDimitry Andric   Printer.printString("setter", N->getSetterName());
23630b57cec5SDimitry Andric   Printer.printString("getter", N->getGetterName());
23640b57cec5SDimitry Andric   Printer.printInt("attributes", N->getAttributes());
23650b57cec5SDimitry Andric   Printer.printMetadata("type", N->getRawType());
23660b57cec5SDimitry Andric   Out << ")";
23670b57cec5SDimitry Andric }
23680b57cec5SDimitry Andric 
23690b57cec5SDimitry Andric static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
2370*349cc55cSDimitry Andric                                   AsmWriterContext &WriterCtx) {
23710b57cec5SDimitry Andric   Out << "!DIImportedEntity(";
2372*349cc55cSDimitry Andric   MDFieldPrinter Printer(Out, WriterCtx);
23730b57cec5SDimitry Andric   Printer.printTag(N);
23740b57cec5SDimitry Andric   Printer.printString("name", N->getName());
23750b57cec5SDimitry Andric   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
23760b57cec5SDimitry Andric   Printer.printMetadata("entity", N->getRawEntity());
23770b57cec5SDimitry Andric   Printer.printMetadata("file", N->getRawFile());
23780b57cec5SDimitry Andric   Printer.printInt("line", N->getLine());
2379*349cc55cSDimitry Andric   Printer.printMetadata("elements", N->getRawElements());
23800b57cec5SDimitry Andric   Out << ")";
23810b57cec5SDimitry Andric }
23820b57cec5SDimitry Andric 
23830b57cec5SDimitry Andric static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
2384*349cc55cSDimitry Andric                                     AsmWriterContext &Ctx) {
23850b57cec5SDimitry Andric   if (Node->isDistinct())
23860b57cec5SDimitry Andric     Out << "distinct ";
23870b57cec5SDimitry Andric   else if (Node->isTemporary())
23880b57cec5SDimitry Andric     Out << "<temporary!> "; // Handle broken code.
23890b57cec5SDimitry Andric 
23900b57cec5SDimitry Andric   switch (Node->getMetadataID()) {
23910b57cec5SDimitry Andric   default:
23920b57cec5SDimitry Andric     llvm_unreachable("Expected uniquable MDNode");
23930b57cec5SDimitry Andric #define HANDLE_MDNODE_LEAF(CLASS)                                              \
23940b57cec5SDimitry Andric   case Metadata::CLASS##Kind:                                                  \
2395*349cc55cSDimitry Andric     write##CLASS(Out, cast<CLASS>(Node), Ctx);                                 \
23960b57cec5SDimitry Andric     break;
23970b57cec5SDimitry Andric #include "llvm/IR/Metadata.def"
23980b57cec5SDimitry Andric   }
23990b57cec5SDimitry Andric }
24000b57cec5SDimitry Andric 
24010b57cec5SDimitry Andric // Full implementation of printing a Value as an operand with support for
24020b57cec5SDimitry Andric // TypePrinting, etc.
24030b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
2404*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx) {
24050b57cec5SDimitry Andric   if (V->hasName()) {
24060b57cec5SDimitry Andric     PrintLLVMName(Out, V);
24070b57cec5SDimitry Andric     return;
24080b57cec5SDimitry Andric   }
24090b57cec5SDimitry Andric 
24100b57cec5SDimitry Andric   const Constant *CV = dyn_cast<Constant>(V);
24110b57cec5SDimitry Andric   if (CV && !isa<GlobalValue>(CV)) {
2412*349cc55cSDimitry Andric     assert(WriterCtx.TypePrinter && "Constants require TypePrinting!");
2413*349cc55cSDimitry Andric     WriteConstantInternal(Out, CV, WriterCtx);
24140b57cec5SDimitry Andric     return;
24150b57cec5SDimitry Andric   }
24160b57cec5SDimitry Andric 
24170b57cec5SDimitry Andric   if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
24180b57cec5SDimitry Andric     Out << "asm ";
24190b57cec5SDimitry Andric     if (IA->hasSideEffects())
24200b57cec5SDimitry Andric       Out << "sideeffect ";
24210b57cec5SDimitry Andric     if (IA->isAlignStack())
24220b57cec5SDimitry Andric       Out << "alignstack ";
24230b57cec5SDimitry Andric     // We don't emit the AD_ATT dialect as it's the assumed default.
24240b57cec5SDimitry Andric     if (IA->getDialect() == InlineAsm::AD_Intel)
24250b57cec5SDimitry Andric       Out << "inteldialect ";
2426fe6060f1SDimitry Andric     if (IA->canThrow())
2427fe6060f1SDimitry Andric       Out << "unwind ";
24280b57cec5SDimitry Andric     Out << '"';
24290b57cec5SDimitry Andric     printEscapedString(IA->getAsmString(), Out);
24300b57cec5SDimitry Andric     Out << "\", \"";
24310b57cec5SDimitry Andric     printEscapedString(IA->getConstraintString(), Out);
24320b57cec5SDimitry Andric     Out << '"';
24330b57cec5SDimitry Andric     return;
24340b57cec5SDimitry Andric   }
24350b57cec5SDimitry Andric 
24360b57cec5SDimitry Andric   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
2437*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, MD->getMetadata(), WriterCtx,
2438*349cc55cSDimitry Andric                            /* FromValue */ true);
24390b57cec5SDimitry Andric     return;
24400b57cec5SDimitry Andric   }
24410b57cec5SDimitry Andric 
24420b57cec5SDimitry Andric   char Prefix = '%';
24430b57cec5SDimitry Andric   int Slot;
2444*349cc55cSDimitry Andric   auto *Machine = WriterCtx.Machine;
24450b57cec5SDimitry Andric   // If we have a SlotTracker, use it.
24460b57cec5SDimitry Andric   if (Machine) {
24470b57cec5SDimitry Andric     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
24480b57cec5SDimitry Andric       Slot = Machine->getGlobalSlot(GV);
24490b57cec5SDimitry Andric       Prefix = '@';
24500b57cec5SDimitry Andric     } else {
24510b57cec5SDimitry Andric       Slot = Machine->getLocalSlot(V);
24520b57cec5SDimitry Andric 
24530b57cec5SDimitry Andric       // If the local value didn't succeed, then we may be referring to a value
24540b57cec5SDimitry Andric       // from a different function.  Translate it, as this can happen when using
24550b57cec5SDimitry Andric       // address of blocks.
24560b57cec5SDimitry Andric       if (Slot == -1)
24570b57cec5SDimitry Andric         if ((Machine = createSlotTracker(V))) {
24580b57cec5SDimitry Andric           Slot = Machine->getLocalSlot(V);
24590b57cec5SDimitry Andric           delete Machine;
24600b57cec5SDimitry Andric         }
24610b57cec5SDimitry Andric     }
24620b57cec5SDimitry Andric   } else if ((Machine = createSlotTracker(V))) {
24630b57cec5SDimitry Andric     // Otherwise, create one to get the # and then destroy it.
24640b57cec5SDimitry Andric     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
24650b57cec5SDimitry Andric       Slot = Machine->getGlobalSlot(GV);
24660b57cec5SDimitry Andric       Prefix = '@';
24670b57cec5SDimitry Andric     } else {
24680b57cec5SDimitry Andric       Slot = Machine->getLocalSlot(V);
24690b57cec5SDimitry Andric     }
24700b57cec5SDimitry Andric     delete Machine;
24710b57cec5SDimitry Andric     Machine = nullptr;
24720b57cec5SDimitry Andric   } else {
24730b57cec5SDimitry Andric     Slot = -1;
24740b57cec5SDimitry Andric   }
24750b57cec5SDimitry Andric 
24760b57cec5SDimitry Andric   if (Slot != -1)
24770b57cec5SDimitry Andric     Out << Prefix << Slot;
24780b57cec5SDimitry Andric   else
24790b57cec5SDimitry Andric     Out << "<badref>";
24800b57cec5SDimitry Andric }
24810b57cec5SDimitry Andric 
24820b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
2483*349cc55cSDimitry Andric                                    AsmWriterContext &WriterCtx,
24840b57cec5SDimitry Andric                                    bool FromValue) {
2485fe6060f1SDimitry Andric   // Write DIExpressions and DIArgLists inline when used as a value. Improves
2486fe6060f1SDimitry Andric   // readability of debug info intrinsics.
24870b57cec5SDimitry Andric   if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
2488*349cc55cSDimitry Andric     writeDIExpression(Out, Expr, WriterCtx);
24890b57cec5SDimitry Andric     return;
24900b57cec5SDimitry Andric   }
2491fe6060f1SDimitry Andric   if (const DIArgList *ArgList = dyn_cast<DIArgList>(MD)) {
2492*349cc55cSDimitry Andric     writeDIArgList(Out, ArgList, WriterCtx, FromValue);
2493fe6060f1SDimitry Andric     return;
2494fe6060f1SDimitry Andric   }
24950b57cec5SDimitry Andric 
24960b57cec5SDimitry Andric   if (const MDNode *N = dyn_cast<MDNode>(MD)) {
24970b57cec5SDimitry Andric     std::unique_ptr<SlotTracker> MachineStorage;
2498*349cc55cSDimitry Andric     SaveAndRestore<SlotTracker *> SARMachine(WriterCtx.Machine);
2499*349cc55cSDimitry Andric     if (!WriterCtx.Machine) {
2500*349cc55cSDimitry Andric       MachineStorage = std::make_unique<SlotTracker>(WriterCtx.Context);
2501*349cc55cSDimitry Andric       WriterCtx.Machine = MachineStorage.get();
25020b57cec5SDimitry Andric     }
2503*349cc55cSDimitry Andric     int Slot = WriterCtx.Machine->getMetadataSlot(N);
25040b57cec5SDimitry Andric     if (Slot == -1) {
25050b57cec5SDimitry Andric       if (const DILocation *Loc = dyn_cast<DILocation>(N)) {
2506*349cc55cSDimitry Andric         writeDILocation(Out, Loc, WriterCtx);
25070b57cec5SDimitry Andric         return;
25080b57cec5SDimitry Andric       }
25090b57cec5SDimitry Andric       // Give the pointer value instead of "badref", since this comes up all
25100b57cec5SDimitry Andric       // the time when debugging.
25110b57cec5SDimitry Andric       Out << "<" << N << ">";
25120b57cec5SDimitry Andric     } else
25130b57cec5SDimitry Andric       Out << '!' << Slot;
25140b57cec5SDimitry Andric     return;
25150b57cec5SDimitry Andric   }
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric   if (const MDString *MDS = dyn_cast<MDString>(MD)) {
25180b57cec5SDimitry Andric     Out << "!\"";
25190b57cec5SDimitry Andric     printEscapedString(MDS->getString(), Out);
25200b57cec5SDimitry Andric     Out << '"';
25210b57cec5SDimitry Andric     return;
25220b57cec5SDimitry Andric   }
25230b57cec5SDimitry Andric 
25240b57cec5SDimitry Andric   auto *V = cast<ValueAsMetadata>(MD);
2525*349cc55cSDimitry Andric   assert(WriterCtx.TypePrinter && "TypePrinter required for metadata values");
25260b57cec5SDimitry Andric   assert((FromValue || !isa<LocalAsMetadata>(V)) &&
25270b57cec5SDimitry Andric          "Unexpected function-local metadata outside of value argument");
25280b57cec5SDimitry Andric 
2529*349cc55cSDimitry Andric   WriterCtx.TypePrinter->print(V->getValue()->getType(), Out);
25300b57cec5SDimitry Andric   Out << ' ';
2531*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, V->getValue(), WriterCtx);
25320b57cec5SDimitry Andric }
25330b57cec5SDimitry Andric 
25340b57cec5SDimitry Andric namespace {
25350b57cec5SDimitry Andric 
25360b57cec5SDimitry Andric class AssemblyWriter {
25370b57cec5SDimitry Andric   formatted_raw_ostream &Out;
25380b57cec5SDimitry Andric   const Module *TheModule = nullptr;
25390b57cec5SDimitry Andric   const ModuleSummaryIndex *TheIndex = nullptr;
25400b57cec5SDimitry Andric   std::unique_ptr<SlotTracker> SlotTrackerStorage;
25410b57cec5SDimitry Andric   SlotTracker &Machine;
25420b57cec5SDimitry Andric   TypePrinting TypePrinter;
25430b57cec5SDimitry Andric   AssemblyAnnotationWriter *AnnotationWriter = nullptr;
25440b57cec5SDimitry Andric   SetVector<const Comdat *> Comdats;
25450b57cec5SDimitry Andric   bool IsForDebug;
25460b57cec5SDimitry Andric   bool ShouldPreserveUseListOrder;
2547fe6060f1SDimitry Andric   UseListOrderMap UseListOrders;
25480b57cec5SDimitry Andric   SmallVector<StringRef, 8> MDNames;
25490b57cec5SDimitry Andric   /// Synchronization scope names registered with LLVMContext.
25500b57cec5SDimitry Andric   SmallVector<StringRef, 8> SSNs;
25510b57cec5SDimitry Andric   DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
25520b57cec5SDimitry Andric 
25530b57cec5SDimitry Andric public:
25540b57cec5SDimitry Andric   /// Construct an AssemblyWriter with an external SlotTracker
25550b57cec5SDimitry Andric   AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
25560b57cec5SDimitry Andric                  AssemblyAnnotationWriter *AAW, bool IsForDebug,
25570b57cec5SDimitry Andric                  bool ShouldPreserveUseListOrder = false);
25580b57cec5SDimitry Andric 
25590b57cec5SDimitry Andric   AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
25600b57cec5SDimitry Andric                  const ModuleSummaryIndex *Index, bool IsForDebug);
25610b57cec5SDimitry Andric 
2562*349cc55cSDimitry Andric   AsmWriterContext getContext() {
2563*349cc55cSDimitry Andric     return AsmWriterContext(&TypePrinter, &Machine, TheModule);
2564*349cc55cSDimitry Andric   }
2565*349cc55cSDimitry Andric 
25660b57cec5SDimitry Andric   void printMDNodeBody(const MDNode *MD);
25670b57cec5SDimitry Andric   void printNamedMDNode(const NamedMDNode *NMD);
25680b57cec5SDimitry Andric 
25690b57cec5SDimitry Andric   void printModule(const Module *M);
25700b57cec5SDimitry Andric 
25710b57cec5SDimitry Andric   void writeOperand(const Value *Op, bool PrintType);
25720b57cec5SDimitry Andric   void writeParamOperand(const Value *Operand, AttributeSet Attrs);
25730b57cec5SDimitry Andric   void writeOperandBundles(const CallBase *Call);
25740b57cec5SDimitry Andric   void writeSyncScope(const LLVMContext &Context,
25750b57cec5SDimitry Andric                       SyncScope::ID SSID);
25760b57cec5SDimitry Andric   void writeAtomic(const LLVMContext &Context,
25770b57cec5SDimitry Andric                    AtomicOrdering Ordering,
25780b57cec5SDimitry Andric                    SyncScope::ID SSID);
25790b57cec5SDimitry Andric   void writeAtomicCmpXchg(const LLVMContext &Context,
25800b57cec5SDimitry Andric                           AtomicOrdering SuccessOrdering,
25810b57cec5SDimitry Andric                           AtomicOrdering FailureOrdering,
25820b57cec5SDimitry Andric                           SyncScope::ID SSID);
25830b57cec5SDimitry Andric 
25840b57cec5SDimitry Andric   void writeAllMDNodes();
25850b57cec5SDimitry Andric   void writeMDNode(unsigned Slot, const MDNode *Node);
2586480093f4SDimitry Andric   void writeAttribute(const Attribute &Attr, bool InAttrGroup = false);
2587480093f4SDimitry Andric   void writeAttributeSet(const AttributeSet &AttrSet, bool InAttrGroup = false);
25880b57cec5SDimitry Andric   void writeAllAttributeGroups();
25890b57cec5SDimitry Andric 
25900b57cec5SDimitry Andric   void printTypeIdentities();
25910b57cec5SDimitry Andric   void printGlobal(const GlobalVariable *GV);
2592*349cc55cSDimitry Andric   void printAlias(const GlobalAlias *GA);
2593*349cc55cSDimitry Andric   void printIFunc(const GlobalIFunc *GI);
25940b57cec5SDimitry Andric   void printComdat(const Comdat *C);
25950b57cec5SDimitry Andric   void printFunction(const Function *F);
25960b57cec5SDimitry Andric   void printArgument(const Argument *FA, AttributeSet Attrs);
25970b57cec5SDimitry Andric   void printBasicBlock(const BasicBlock *BB);
25980b57cec5SDimitry Andric   void printInstructionLine(const Instruction &I);
25990b57cec5SDimitry Andric   void printInstruction(const Instruction &I);
26000b57cec5SDimitry Andric 
2601fe6060f1SDimitry Andric   void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle);
26020b57cec5SDimitry Andric   void printUseLists(const Function *F);
26030b57cec5SDimitry Andric 
26040b57cec5SDimitry Andric   void printModuleSummaryIndex();
26050b57cec5SDimitry Andric   void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
26060b57cec5SDimitry Andric   void printSummary(const GlobalValueSummary &Summary);
26070b57cec5SDimitry Andric   void printAliasSummary(const AliasSummary *AS);
26080b57cec5SDimitry Andric   void printGlobalVarSummary(const GlobalVarSummary *GS);
26090b57cec5SDimitry Andric   void printFunctionSummary(const FunctionSummary *FS);
26100b57cec5SDimitry Andric   void printTypeIdSummary(const TypeIdSummary &TIS);
26110b57cec5SDimitry Andric   void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI);
26120b57cec5SDimitry Andric   void printTypeTestResolution(const TypeTestResolution &TTRes);
26130b57cec5SDimitry Andric   void printArgs(const std::vector<uint64_t> &Args);
26140b57cec5SDimitry Andric   void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
26150b57cec5SDimitry Andric   void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
26160b57cec5SDimitry Andric   void printVFuncId(const FunctionSummary::VFuncId VFId);
26170b57cec5SDimitry Andric   void
26185ffd83dbSDimitry Andric   printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> &VCallList,
26190b57cec5SDimitry Andric                       const char *Tag);
26200b57cec5SDimitry Andric   void
26215ffd83dbSDimitry Andric   printConstVCalls(const std::vector<FunctionSummary::ConstVCall> &VCallList,
26220b57cec5SDimitry Andric                    const char *Tag);
26230b57cec5SDimitry Andric 
26240b57cec5SDimitry Andric private:
26250b57cec5SDimitry Andric   /// Print out metadata attachments.
26260b57cec5SDimitry Andric   void printMetadataAttachments(
26270b57cec5SDimitry Andric       const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
26280b57cec5SDimitry Andric       StringRef Separator);
26290b57cec5SDimitry Andric 
26300b57cec5SDimitry Andric   // printInfoComment - Print a little comment after the instruction indicating
26310b57cec5SDimitry Andric   // which slot it occupies.
26320b57cec5SDimitry Andric   void printInfoComment(const Value &V);
26330b57cec5SDimitry Andric 
26340b57cec5SDimitry Andric   // printGCRelocateComment - print comment after call to the gc.relocate
26350b57cec5SDimitry Andric   // intrinsic indicating base and derived pointer names.
26360b57cec5SDimitry Andric   void printGCRelocateComment(const GCRelocateInst &Relocate);
26370b57cec5SDimitry Andric };
26380b57cec5SDimitry Andric 
26390b57cec5SDimitry Andric } // end anonymous namespace
26400b57cec5SDimitry Andric 
26410b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
26420b57cec5SDimitry Andric                                const Module *M, AssemblyAnnotationWriter *AAW,
26430b57cec5SDimitry Andric                                bool IsForDebug, bool ShouldPreserveUseListOrder)
26440b57cec5SDimitry Andric     : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
26450b57cec5SDimitry Andric       IsForDebug(IsForDebug),
26460b57cec5SDimitry Andric       ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
26470b57cec5SDimitry Andric   if (!TheModule)
26480b57cec5SDimitry Andric     return;
26490b57cec5SDimitry Andric   for (const GlobalObject &GO : TheModule->global_objects())
26500b57cec5SDimitry Andric     if (const Comdat *C = GO.getComdat())
26510b57cec5SDimitry Andric       Comdats.insert(C);
26520b57cec5SDimitry Andric }
26530b57cec5SDimitry Andric 
26540b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
26550b57cec5SDimitry Andric                                const ModuleSummaryIndex *Index, bool IsForDebug)
26560b57cec5SDimitry Andric     : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
26570b57cec5SDimitry Andric       IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
26580b57cec5SDimitry Andric 
26590b57cec5SDimitry Andric void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
26600b57cec5SDimitry Andric   if (!Operand) {
26610b57cec5SDimitry Andric     Out << "<null operand!>";
26620b57cec5SDimitry Andric     return;
26630b57cec5SDimitry Andric   }
26640b57cec5SDimitry Andric   if (PrintType) {
26650b57cec5SDimitry Andric     TypePrinter.print(Operand->getType(), Out);
26660b57cec5SDimitry Andric     Out << ' ';
26670b57cec5SDimitry Andric   }
2668*349cc55cSDimitry Andric   auto WriterCtx = getContext();
2669*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, Operand, WriterCtx);
26700b57cec5SDimitry Andric }
26710b57cec5SDimitry Andric 
26720b57cec5SDimitry Andric void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
26730b57cec5SDimitry Andric                                     SyncScope::ID SSID) {
26740b57cec5SDimitry Andric   switch (SSID) {
26750b57cec5SDimitry Andric   case SyncScope::System: {
26760b57cec5SDimitry Andric     break;
26770b57cec5SDimitry Andric   }
26780b57cec5SDimitry Andric   default: {
26790b57cec5SDimitry Andric     if (SSNs.empty())
26800b57cec5SDimitry Andric       Context.getSyncScopeNames(SSNs);
26810b57cec5SDimitry Andric 
26820b57cec5SDimitry Andric     Out << " syncscope(\"";
26830b57cec5SDimitry Andric     printEscapedString(SSNs[SSID], Out);
26840b57cec5SDimitry Andric     Out << "\")";
26850b57cec5SDimitry Andric     break;
26860b57cec5SDimitry Andric   }
26870b57cec5SDimitry Andric   }
26880b57cec5SDimitry Andric }
26890b57cec5SDimitry Andric 
26900b57cec5SDimitry Andric void AssemblyWriter::writeAtomic(const LLVMContext &Context,
26910b57cec5SDimitry Andric                                  AtomicOrdering Ordering,
26920b57cec5SDimitry Andric                                  SyncScope::ID SSID) {
26930b57cec5SDimitry Andric   if (Ordering == AtomicOrdering::NotAtomic)
26940b57cec5SDimitry Andric     return;
26950b57cec5SDimitry Andric 
26960b57cec5SDimitry Andric   writeSyncScope(Context, SSID);
26970b57cec5SDimitry Andric   Out << " " << toIRString(Ordering);
26980b57cec5SDimitry Andric }
26990b57cec5SDimitry Andric 
27000b57cec5SDimitry Andric void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
27010b57cec5SDimitry Andric                                         AtomicOrdering SuccessOrdering,
27020b57cec5SDimitry Andric                                         AtomicOrdering FailureOrdering,
27030b57cec5SDimitry Andric                                         SyncScope::ID SSID) {
27040b57cec5SDimitry Andric   assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
27050b57cec5SDimitry Andric          FailureOrdering != AtomicOrdering::NotAtomic);
27060b57cec5SDimitry Andric 
27070b57cec5SDimitry Andric   writeSyncScope(Context, SSID);
27080b57cec5SDimitry Andric   Out << " " << toIRString(SuccessOrdering);
27090b57cec5SDimitry Andric   Out << " " << toIRString(FailureOrdering);
27100b57cec5SDimitry Andric }
27110b57cec5SDimitry Andric 
27120b57cec5SDimitry Andric void AssemblyWriter::writeParamOperand(const Value *Operand,
27130b57cec5SDimitry Andric                                        AttributeSet Attrs) {
27140b57cec5SDimitry Andric   if (!Operand) {
27150b57cec5SDimitry Andric     Out << "<null operand!>";
27160b57cec5SDimitry Andric     return;
27170b57cec5SDimitry Andric   }
27180b57cec5SDimitry Andric 
27190b57cec5SDimitry Andric   // Print the type
27200b57cec5SDimitry Andric   TypePrinter.print(Operand->getType(), Out);
27210b57cec5SDimitry Andric   // Print parameter attributes list
2722480093f4SDimitry Andric   if (Attrs.hasAttributes()) {
2723480093f4SDimitry Andric     Out << ' ';
2724480093f4SDimitry Andric     writeAttributeSet(Attrs);
2725480093f4SDimitry Andric   }
27260b57cec5SDimitry Andric   Out << ' ';
27270b57cec5SDimitry Andric   // Print the operand
2728*349cc55cSDimitry Andric   auto WriterCtx = getContext();
2729*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, Operand, WriterCtx);
27300b57cec5SDimitry Andric }
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric void AssemblyWriter::writeOperandBundles(const CallBase *Call) {
27330b57cec5SDimitry Andric   if (!Call->hasOperandBundles())
27340b57cec5SDimitry Andric     return;
27350b57cec5SDimitry Andric 
27360b57cec5SDimitry Andric   Out << " [ ";
27370b57cec5SDimitry Andric 
27380b57cec5SDimitry Andric   bool FirstBundle = true;
27390b57cec5SDimitry Andric   for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) {
27400b57cec5SDimitry Andric     OperandBundleUse BU = Call->getOperandBundleAt(i);
27410b57cec5SDimitry Andric 
27420b57cec5SDimitry Andric     if (!FirstBundle)
27430b57cec5SDimitry Andric       Out << ", ";
27440b57cec5SDimitry Andric     FirstBundle = false;
27450b57cec5SDimitry Andric 
27460b57cec5SDimitry Andric     Out << '"';
27470b57cec5SDimitry Andric     printEscapedString(BU.getTagName(), Out);
27480b57cec5SDimitry Andric     Out << '"';
27490b57cec5SDimitry Andric 
27500b57cec5SDimitry Andric     Out << '(';
27510b57cec5SDimitry Andric 
27520b57cec5SDimitry Andric     bool FirstInput = true;
2753*349cc55cSDimitry Andric     auto WriterCtx = getContext();
27540b57cec5SDimitry Andric     for (const auto &Input : BU.Inputs) {
27550b57cec5SDimitry Andric       if (!FirstInput)
27560b57cec5SDimitry Andric         Out << ", ";
27570b57cec5SDimitry Andric       FirstInput = false;
27580b57cec5SDimitry Andric 
27590b57cec5SDimitry Andric       TypePrinter.print(Input->getType(), Out);
27600b57cec5SDimitry Andric       Out << " ";
2761*349cc55cSDimitry Andric       WriteAsOperandInternal(Out, Input, WriterCtx);
27620b57cec5SDimitry Andric     }
27630b57cec5SDimitry Andric 
27640b57cec5SDimitry Andric     Out << ')';
27650b57cec5SDimitry Andric   }
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric   Out << " ]";
27680b57cec5SDimitry Andric }
27690b57cec5SDimitry Andric 
27700b57cec5SDimitry Andric void AssemblyWriter::printModule(const Module *M) {
27710b57cec5SDimitry Andric   Machine.initializeIfNeeded();
27720b57cec5SDimitry Andric 
27730b57cec5SDimitry Andric   if (ShouldPreserveUseListOrder)
27740b57cec5SDimitry Andric     UseListOrders = predictUseListOrder(M);
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric   if (!M->getModuleIdentifier().empty() &&
27770b57cec5SDimitry Andric       // Don't print the ID if it will start a new line (which would
27780b57cec5SDimitry Andric       // require a comment char before it).
27790b57cec5SDimitry Andric       M->getModuleIdentifier().find('\n') == std::string::npos)
27800b57cec5SDimitry Andric     Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
27810b57cec5SDimitry Andric 
27820b57cec5SDimitry Andric   if (!M->getSourceFileName().empty()) {
27830b57cec5SDimitry Andric     Out << "source_filename = \"";
27840b57cec5SDimitry Andric     printEscapedString(M->getSourceFileName(), Out);
27850b57cec5SDimitry Andric     Out << "\"\n";
27860b57cec5SDimitry Andric   }
27870b57cec5SDimitry Andric 
27880b57cec5SDimitry Andric   const std::string &DL = M->getDataLayoutStr();
27890b57cec5SDimitry Andric   if (!DL.empty())
27900b57cec5SDimitry Andric     Out << "target datalayout = \"" << DL << "\"\n";
27910b57cec5SDimitry Andric   if (!M->getTargetTriple().empty())
27920b57cec5SDimitry Andric     Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
27930b57cec5SDimitry Andric 
27940b57cec5SDimitry Andric   if (!M->getModuleInlineAsm().empty()) {
27950b57cec5SDimitry Andric     Out << '\n';
27960b57cec5SDimitry Andric 
27970b57cec5SDimitry Andric     // Split the string into lines, to make it easier to read the .ll file.
27980b57cec5SDimitry Andric     StringRef Asm = M->getModuleInlineAsm();
27990b57cec5SDimitry Andric     do {
28000b57cec5SDimitry Andric       StringRef Front;
28010b57cec5SDimitry Andric       std::tie(Front, Asm) = Asm.split('\n');
28020b57cec5SDimitry Andric 
28030b57cec5SDimitry Andric       // We found a newline, print the portion of the asm string from the
28040b57cec5SDimitry Andric       // last newline up to this newline.
28050b57cec5SDimitry Andric       Out << "module asm \"";
28060b57cec5SDimitry Andric       printEscapedString(Front, Out);
28070b57cec5SDimitry Andric       Out << "\"\n";
28080b57cec5SDimitry Andric     } while (!Asm.empty());
28090b57cec5SDimitry Andric   }
28100b57cec5SDimitry Andric 
28110b57cec5SDimitry Andric   printTypeIdentities();
28120b57cec5SDimitry Andric 
28130b57cec5SDimitry Andric   // Output all comdats.
28140b57cec5SDimitry Andric   if (!Comdats.empty())
28150b57cec5SDimitry Andric     Out << '\n';
28160b57cec5SDimitry Andric   for (const Comdat *C : Comdats) {
28170b57cec5SDimitry Andric     printComdat(C);
28180b57cec5SDimitry Andric     if (C != Comdats.back())
28190b57cec5SDimitry Andric       Out << '\n';
28200b57cec5SDimitry Andric   }
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric   // Output all globals.
28230b57cec5SDimitry Andric   if (!M->global_empty()) Out << '\n';
28240b57cec5SDimitry Andric   for (const GlobalVariable &GV : M->globals()) {
28250b57cec5SDimitry Andric     printGlobal(&GV); Out << '\n';
28260b57cec5SDimitry Andric   }
28270b57cec5SDimitry Andric 
28280b57cec5SDimitry Andric   // Output all aliases.
28290b57cec5SDimitry Andric   if (!M->alias_empty()) Out << "\n";
28300b57cec5SDimitry Andric   for (const GlobalAlias &GA : M->aliases())
2831*349cc55cSDimitry Andric     printAlias(&GA);
28320b57cec5SDimitry Andric 
28330b57cec5SDimitry Andric   // Output all ifuncs.
28340b57cec5SDimitry Andric   if (!M->ifunc_empty()) Out << "\n";
28350b57cec5SDimitry Andric   for (const GlobalIFunc &GI : M->ifuncs())
2836*349cc55cSDimitry Andric     printIFunc(&GI);
28370b57cec5SDimitry Andric 
28380b57cec5SDimitry Andric   // Output all of the functions.
283913138422SDimitry Andric   for (const Function &F : *M) {
284013138422SDimitry Andric     Out << '\n';
28410b57cec5SDimitry Andric     printFunction(&F);
284213138422SDimitry Andric   }
2843fe6060f1SDimitry Andric 
2844fe6060f1SDimitry Andric   // Output global use-lists.
2845fe6060f1SDimitry Andric   printUseLists(nullptr);
28460b57cec5SDimitry Andric 
28470b57cec5SDimitry Andric   // Output all attribute groups.
28480b57cec5SDimitry Andric   if (!Machine.as_empty()) {
28490b57cec5SDimitry Andric     Out << '\n';
28500b57cec5SDimitry Andric     writeAllAttributeGroups();
28510b57cec5SDimitry Andric   }
28520b57cec5SDimitry Andric 
28530b57cec5SDimitry Andric   // Output named metadata.
28540b57cec5SDimitry Andric   if (!M->named_metadata_empty()) Out << '\n';
28550b57cec5SDimitry Andric 
28560b57cec5SDimitry Andric   for (const NamedMDNode &Node : M->named_metadata())
28570b57cec5SDimitry Andric     printNamedMDNode(&Node);
28580b57cec5SDimitry Andric 
28590b57cec5SDimitry Andric   // Output metadata.
28600b57cec5SDimitry Andric   if (!Machine.mdn_empty()) {
28610b57cec5SDimitry Andric     Out << '\n';
28620b57cec5SDimitry Andric     writeAllMDNodes();
28630b57cec5SDimitry Andric   }
28640b57cec5SDimitry Andric }
28650b57cec5SDimitry Andric 
28660b57cec5SDimitry Andric void AssemblyWriter::printModuleSummaryIndex() {
28670b57cec5SDimitry Andric   assert(TheIndex);
28685ffd83dbSDimitry Andric   int NumSlots = Machine.initializeIndexIfNeeded();
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric   Out << "\n";
28710b57cec5SDimitry Andric 
28720b57cec5SDimitry Andric   // Print module path entries. To print in order, add paths to a vector
28730b57cec5SDimitry Andric   // indexed by module slot.
28740b57cec5SDimitry Andric   std::vector<std::pair<std::string, ModuleHash>> moduleVec;
28755ffd83dbSDimitry Andric   std::string RegularLTOModuleName =
28765ffd83dbSDimitry Andric       ModuleSummaryIndex::getRegularLTOModuleName();
28770b57cec5SDimitry Andric   moduleVec.resize(TheIndex->modulePaths().size());
28780b57cec5SDimitry Andric   for (auto &ModPath : TheIndex->modulePaths())
28790b57cec5SDimitry Andric     moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
28800b57cec5SDimitry Andric         // A module id of -1 is a special entry for a regular LTO module created
28810b57cec5SDimitry Andric         // during the thin link.
28820b57cec5SDimitry Andric         ModPath.second.first == -1u ? RegularLTOModuleName
28835ffd83dbSDimitry Andric                                     : (std::string)std::string(ModPath.first()),
28840b57cec5SDimitry Andric         ModPath.second.second);
28850b57cec5SDimitry Andric 
28860b57cec5SDimitry Andric   unsigned i = 0;
28870b57cec5SDimitry Andric   for (auto &ModPair : moduleVec) {
28880b57cec5SDimitry Andric     Out << "^" << i++ << " = module: (";
28890b57cec5SDimitry Andric     Out << "path: \"";
28900b57cec5SDimitry Andric     printEscapedString(ModPair.first, Out);
28910b57cec5SDimitry Andric     Out << "\", hash: (";
28920b57cec5SDimitry Andric     FieldSeparator FS;
28930b57cec5SDimitry Andric     for (auto Hash : ModPair.second)
28940b57cec5SDimitry Andric       Out << FS << Hash;
28950b57cec5SDimitry Andric     Out << "))\n";
28960b57cec5SDimitry Andric   }
28970b57cec5SDimitry Andric 
28980b57cec5SDimitry Andric   // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
28990b57cec5SDimitry Andric   // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
29000b57cec5SDimitry Andric   for (auto &GlobalList : *TheIndex) {
29010b57cec5SDimitry Andric     auto GUID = GlobalList.first;
29020b57cec5SDimitry Andric     for (auto &Summary : GlobalList.second.SummaryList)
29030b57cec5SDimitry Andric       SummaryToGUIDMap[Summary.get()] = GUID;
29040b57cec5SDimitry Andric   }
29050b57cec5SDimitry Andric 
29060b57cec5SDimitry Andric   // Print the global value summary entries.
29070b57cec5SDimitry Andric   for (auto &GlobalList : *TheIndex) {
29080b57cec5SDimitry Andric     auto GUID = GlobalList.first;
29090b57cec5SDimitry Andric     auto VI = TheIndex->getValueInfo(GlobalList);
29100b57cec5SDimitry Andric     printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
29110b57cec5SDimitry Andric   }
29120b57cec5SDimitry Andric 
29130b57cec5SDimitry Andric   // Print the TypeIdMap entries.
2914fe6060f1SDimitry Andric   for (const auto &TID : TheIndex->typeIds()) {
2915fe6060f1SDimitry Andric     Out << "^" << Machine.getTypeIdSlot(TID.second.first)
2916fe6060f1SDimitry Andric         << " = typeid: (name: \"" << TID.second.first << "\"";
2917fe6060f1SDimitry Andric     printTypeIdSummary(TID.second.second);
2918fe6060f1SDimitry Andric     Out << ") ; guid = " << TID.first << "\n";
29190b57cec5SDimitry Andric   }
29200b57cec5SDimitry Andric 
29210b57cec5SDimitry Andric   // Print the TypeIdCompatibleVtableMap entries.
29220b57cec5SDimitry Andric   for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) {
29230b57cec5SDimitry Andric     auto GUID = GlobalValue::getGUID(TId.first);
29240b57cec5SDimitry Andric     Out << "^" << Machine.getGUIDSlot(GUID)
29250b57cec5SDimitry Andric         << " = typeidCompatibleVTable: (name: \"" << TId.first << "\"";
29260b57cec5SDimitry Andric     printTypeIdCompatibleVtableSummary(TId.second);
29270b57cec5SDimitry Andric     Out << ") ; guid = " << GUID << "\n";
29280b57cec5SDimitry Andric   }
29295ffd83dbSDimitry Andric 
29305ffd83dbSDimitry Andric   // Don't emit flags when it's not really needed (value is zero by default).
29315ffd83dbSDimitry Andric   if (TheIndex->getFlags()) {
29325ffd83dbSDimitry Andric     Out << "^" << NumSlots << " = flags: " << TheIndex->getFlags() << "\n";
29335ffd83dbSDimitry Andric     ++NumSlots;
29345ffd83dbSDimitry Andric   }
29355ffd83dbSDimitry Andric 
29365ffd83dbSDimitry Andric   Out << "^" << NumSlots << " = blockcount: " << TheIndex->getBlockCount()
29375ffd83dbSDimitry Andric       << "\n";
29380b57cec5SDimitry Andric }
29390b57cec5SDimitry Andric 
29400b57cec5SDimitry Andric static const char *
29410b57cec5SDimitry Andric getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
29420b57cec5SDimitry Andric   switch (K) {
29430b57cec5SDimitry Andric   case WholeProgramDevirtResolution::Indir:
29440b57cec5SDimitry Andric     return "indir";
29450b57cec5SDimitry Andric   case WholeProgramDevirtResolution::SingleImpl:
29460b57cec5SDimitry Andric     return "singleImpl";
29470b57cec5SDimitry Andric   case WholeProgramDevirtResolution::BranchFunnel:
29480b57cec5SDimitry Andric     return "branchFunnel";
29490b57cec5SDimitry Andric   }
29500b57cec5SDimitry Andric   llvm_unreachable("invalid WholeProgramDevirtResolution kind");
29510b57cec5SDimitry Andric }
29520b57cec5SDimitry Andric 
29530b57cec5SDimitry Andric static const char *getWholeProgDevirtResByArgKindName(
29540b57cec5SDimitry Andric     WholeProgramDevirtResolution::ByArg::Kind K) {
29550b57cec5SDimitry Andric   switch (K) {
29560b57cec5SDimitry Andric   case WholeProgramDevirtResolution::ByArg::Indir:
29570b57cec5SDimitry Andric     return "indir";
29580b57cec5SDimitry Andric   case WholeProgramDevirtResolution::ByArg::UniformRetVal:
29590b57cec5SDimitry Andric     return "uniformRetVal";
29600b57cec5SDimitry Andric   case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
29610b57cec5SDimitry Andric     return "uniqueRetVal";
29620b57cec5SDimitry Andric   case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
29630b57cec5SDimitry Andric     return "virtualConstProp";
29640b57cec5SDimitry Andric   }
29650b57cec5SDimitry Andric   llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
29660b57cec5SDimitry Andric }
29670b57cec5SDimitry Andric 
29680b57cec5SDimitry Andric static const char *getTTResKindName(TypeTestResolution::Kind K) {
29690b57cec5SDimitry Andric   switch (K) {
29705ffd83dbSDimitry Andric   case TypeTestResolution::Unknown:
29715ffd83dbSDimitry Andric     return "unknown";
29720b57cec5SDimitry Andric   case TypeTestResolution::Unsat:
29730b57cec5SDimitry Andric     return "unsat";
29740b57cec5SDimitry Andric   case TypeTestResolution::ByteArray:
29750b57cec5SDimitry Andric     return "byteArray";
29760b57cec5SDimitry Andric   case TypeTestResolution::Inline:
29770b57cec5SDimitry Andric     return "inline";
29780b57cec5SDimitry Andric   case TypeTestResolution::Single:
29790b57cec5SDimitry Andric     return "single";
29800b57cec5SDimitry Andric   case TypeTestResolution::AllOnes:
29810b57cec5SDimitry Andric     return "allOnes";
29820b57cec5SDimitry Andric   }
29830b57cec5SDimitry Andric   llvm_unreachable("invalid TypeTestResolution kind");
29840b57cec5SDimitry Andric }
29850b57cec5SDimitry Andric 
29860b57cec5SDimitry Andric void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
29870b57cec5SDimitry Andric   Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
29880b57cec5SDimitry Andric       << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
29890b57cec5SDimitry Andric 
29900b57cec5SDimitry Andric   // The following fields are only used if the target does not support the use
29910b57cec5SDimitry Andric   // of absolute symbols to store constants. Print only if non-zero.
29920b57cec5SDimitry Andric   if (TTRes.AlignLog2)
29930b57cec5SDimitry Andric     Out << ", alignLog2: " << TTRes.AlignLog2;
29940b57cec5SDimitry Andric   if (TTRes.SizeM1)
29950b57cec5SDimitry Andric     Out << ", sizeM1: " << TTRes.SizeM1;
29960b57cec5SDimitry Andric   if (TTRes.BitMask)
29970b57cec5SDimitry Andric     // BitMask is uint8_t which causes it to print the corresponding char.
29980b57cec5SDimitry Andric     Out << ", bitMask: " << (unsigned)TTRes.BitMask;
29990b57cec5SDimitry Andric   if (TTRes.InlineBits)
30000b57cec5SDimitry Andric     Out << ", inlineBits: " << TTRes.InlineBits;
30010b57cec5SDimitry Andric 
30020b57cec5SDimitry Andric   Out << ")";
30030b57cec5SDimitry Andric }
30040b57cec5SDimitry Andric 
30050b57cec5SDimitry Andric void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
30060b57cec5SDimitry Andric   Out << ", summary: (";
30070b57cec5SDimitry Andric   printTypeTestResolution(TIS.TTRes);
30080b57cec5SDimitry Andric   if (!TIS.WPDRes.empty()) {
30090b57cec5SDimitry Andric     Out << ", wpdResolutions: (";
30100b57cec5SDimitry Andric     FieldSeparator FS;
30110b57cec5SDimitry Andric     for (auto &WPDRes : TIS.WPDRes) {
30120b57cec5SDimitry Andric       Out << FS;
30130b57cec5SDimitry Andric       Out << "(offset: " << WPDRes.first << ", ";
30140b57cec5SDimitry Andric       printWPDRes(WPDRes.second);
30150b57cec5SDimitry Andric       Out << ")";
30160b57cec5SDimitry Andric     }
30170b57cec5SDimitry Andric     Out << ")";
30180b57cec5SDimitry Andric   }
30190b57cec5SDimitry Andric   Out << ")";
30200b57cec5SDimitry Andric }
30210b57cec5SDimitry Andric 
30220b57cec5SDimitry Andric void AssemblyWriter::printTypeIdCompatibleVtableSummary(
30230b57cec5SDimitry Andric     const TypeIdCompatibleVtableInfo &TI) {
30240b57cec5SDimitry Andric   Out << ", summary: (";
30250b57cec5SDimitry Andric   FieldSeparator FS;
30260b57cec5SDimitry Andric   for (auto &P : TI) {
30270b57cec5SDimitry Andric     Out << FS;
30280b57cec5SDimitry Andric     Out << "(offset: " << P.AddressPointOffset << ", ";
30290b57cec5SDimitry Andric     Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID());
30300b57cec5SDimitry Andric     Out << ")";
30310b57cec5SDimitry Andric   }
30320b57cec5SDimitry Andric   Out << ")";
30330b57cec5SDimitry Andric }
30340b57cec5SDimitry Andric 
30350b57cec5SDimitry Andric void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
30360b57cec5SDimitry Andric   Out << "args: (";
30370b57cec5SDimitry Andric   FieldSeparator FS;
30380b57cec5SDimitry Andric   for (auto arg : Args) {
30390b57cec5SDimitry Andric     Out << FS;
30400b57cec5SDimitry Andric     Out << arg;
30410b57cec5SDimitry Andric   }
30420b57cec5SDimitry Andric   Out << ")";
30430b57cec5SDimitry Andric }
30440b57cec5SDimitry Andric 
30450b57cec5SDimitry Andric void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
30460b57cec5SDimitry Andric   Out << "wpdRes: (kind: ";
30470b57cec5SDimitry Andric   Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
30480b57cec5SDimitry Andric 
30490b57cec5SDimitry Andric   if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
30500b57cec5SDimitry Andric     Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
30510b57cec5SDimitry Andric 
30520b57cec5SDimitry Andric   if (!WPDRes.ResByArg.empty()) {
30530b57cec5SDimitry Andric     Out << ", resByArg: (";
30540b57cec5SDimitry Andric     FieldSeparator FS;
30550b57cec5SDimitry Andric     for (auto &ResByArg : WPDRes.ResByArg) {
30560b57cec5SDimitry Andric       Out << FS;
30570b57cec5SDimitry Andric       printArgs(ResByArg.first);
30580b57cec5SDimitry Andric       Out << ", byArg: (kind: ";
30590b57cec5SDimitry Andric       Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
30600b57cec5SDimitry Andric       if (ResByArg.second.TheKind ==
30610b57cec5SDimitry Andric               WholeProgramDevirtResolution::ByArg::UniformRetVal ||
30620b57cec5SDimitry Andric           ResByArg.second.TheKind ==
30630b57cec5SDimitry Andric               WholeProgramDevirtResolution::ByArg::UniqueRetVal)
30640b57cec5SDimitry Andric         Out << ", info: " << ResByArg.second.Info;
30650b57cec5SDimitry Andric 
30660b57cec5SDimitry Andric       // The following fields are only used if the target does not support the
30670b57cec5SDimitry Andric       // use of absolute symbols to store constants. Print only if non-zero.
30680b57cec5SDimitry Andric       if (ResByArg.second.Byte || ResByArg.second.Bit)
30690b57cec5SDimitry Andric         Out << ", byte: " << ResByArg.second.Byte
30700b57cec5SDimitry Andric             << ", bit: " << ResByArg.second.Bit;
30710b57cec5SDimitry Andric 
30720b57cec5SDimitry Andric       Out << ")";
30730b57cec5SDimitry Andric     }
30740b57cec5SDimitry Andric     Out << ")";
30750b57cec5SDimitry Andric   }
30760b57cec5SDimitry Andric   Out << ")";
30770b57cec5SDimitry Andric }
30780b57cec5SDimitry Andric 
30790b57cec5SDimitry Andric static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
30800b57cec5SDimitry Andric   switch (SK) {
30810b57cec5SDimitry Andric   case GlobalValueSummary::AliasKind:
30820b57cec5SDimitry Andric     return "alias";
30830b57cec5SDimitry Andric   case GlobalValueSummary::FunctionKind:
30840b57cec5SDimitry Andric     return "function";
30850b57cec5SDimitry Andric   case GlobalValueSummary::GlobalVarKind:
30860b57cec5SDimitry Andric     return "variable";
30870b57cec5SDimitry Andric   }
30880b57cec5SDimitry Andric   llvm_unreachable("invalid summary kind");
30890b57cec5SDimitry Andric }
30900b57cec5SDimitry Andric 
30910b57cec5SDimitry Andric void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
30920b57cec5SDimitry Andric   Out << ", aliasee: ";
30930b57cec5SDimitry Andric   // The indexes emitted for distributed backends may not include the
30940b57cec5SDimitry Andric   // aliasee summary (only if it is being imported directly). Handle
30950b57cec5SDimitry Andric   // that case by just emitting "null" as the aliasee.
30960b57cec5SDimitry Andric   if (AS->hasAliasee())
30970b57cec5SDimitry Andric     Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
30980b57cec5SDimitry Andric   else
30990b57cec5SDimitry Andric     Out << "null";
31000b57cec5SDimitry Andric }
31010b57cec5SDimitry Andric 
31020b57cec5SDimitry Andric void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
31030b57cec5SDimitry Andric   auto VTableFuncs = GS->vTableFuncs();
31045ffd83dbSDimitry Andric   Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", "
31055ffd83dbSDimitry Andric       << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ", "
31065ffd83dbSDimitry Andric       << "constant: " << GS->VarFlags.Constant;
31075ffd83dbSDimitry Andric   if (!VTableFuncs.empty())
31085ffd83dbSDimitry Andric     Out << ", "
31095ffd83dbSDimitry Andric         << "vcall_visibility: " << GS->VarFlags.VCallVisibility;
31105ffd83dbSDimitry Andric   Out << ")";
31115ffd83dbSDimitry Andric 
31120b57cec5SDimitry Andric   if (!VTableFuncs.empty()) {
31130b57cec5SDimitry Andric     Out << ", vTableFuncs: (";
31140b57cec5SDimitry Andric     FieldSeparator FS;
31150b57cec5SDimitry Andric     for (auto &P : VTableFuncs) {
31160b57cec5SDimitry Andric       Out << FS;
31170b57cec5SDimitry Andric       Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID())
31180b57cec5SDimitry Andric           << ", offset: " << P.VTableOffset;
31190b57cec5SDimitry Andric       Out << ")";
31200b57cec5SDimitry Andric     }
31210b57cec5SDimitry Andric     Out << ")";
31220b57cec5SDimitry Andric   }
31230b57cec5SDimitry Andric }
31240b57cec5SDimitry Andric 
31250b57cec5SDimitry Andric static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
31260b57cec5SDimitry Andric   switch (LT) {
31270b57cec5SDimitry Andric   case GlobalValue::ExternalLinkage:
31280b57cec5SDimitry Andric     return "external";
31290b57cec5SDimitry Andric   case GlobalValue::PrivateLinkage:
31300b57cec5SDimitry Andric     return "private";
31310b57cec5SDimitry Andric   case GlobalValue::InternalLinkage:
31320b57cec5SDimitry Andric     return "internal";
31330b57cec5SDimitry Andric   case GlobalValue::LinkOnceAnyLinkage:
31340b57cec5SDimitry Andric     return "linkonce";
31350b57cec5SDimitry Andric   case GlobalValue::LinkOnceODRLinkage:
31360b57cec5SDimitry Andric     return "linkonce_odr";
31370b57cec5SDimitry Andric   case GlobalValue::WeakAnyLinkage:
31380b57cec5SDimitry Andric     return "weak";
31390b57cec5SDimitry Andric   case GlobalValue::WeakODRLinkage:
31400b57cec5SDimitry Andric     return "weak_odr";
31410b57cec5SDimitry Andric   case GlobalValue::CommonLinkage:
31420b57cec5SDimitry Andric     return "common";
31430b57cec5SDimitry Andric   case GlobalValue::AppendingLinkage:
31440b57cec5SDimitry Andric     return "appending";
31450b57cec5SDimitry Andric   case GlobalValue::ExternalWeakLinkage:
31460b57cec5SDimitry Andric     return "extern_weak";
31470b57cec5SDimitry Andric   case GlobalValue::AvailableExternallyLinkage:
31480b57cec5SDimitry Andric     return "available_externally";
31490b57cec5SDimitry Andric   }
31500b57cec5SDimitry Andric   llvm_unreachable("invalid linkage");
31510b57cec5SDimitry Andric }
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric // When printing the linkage types in IR where the ExternalLinkage is
31540b57cec5SDimitry Andric // not printed, and other linkage types are expected to be printed with
31550b57cec5SDimitry Andric // a space after the name.
31560b57cec5SDimitry Andric static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
31570b57cec5SDimitry Andric   if (LT == GlobalValue::ExternalLinkage)
31580b57cec5SDimitry Andric     return "";
31590b57cec5SDimitry Andric   return getLinkageName(LT) + " ";
31600b57cec5SDimitry Andric }
31610b57cec5SDimitry Andric 
3162fe6060f1SDimitry Andric static const char *getVisibilityName(GlobalValue::VisibilityTypes Vis) {
3163fe6060f1SDimitry Andric   switch (Vis) {
3164fe6060f1SDimitry Andric   case GlobalValue::DefaultVisibility:
3165fe6060f1SDimitry Andric     return "default";
3166fe6060f1SDimitry Andric   case GlobalValue::HiddenVisibility:
3167fe6060f1SDimitry Andric     return "hidden";
3168fe6060f1SDimitry Andric   case GlobalValue::ProtectedVisibility:
3169fe6060f1SDimitry Andric     return "protected";
3170fe6060f1SDimitry Andric   }
3171fe6060f1SDimitry Andric   llvm_unreachable("invalid visibility");
3172fe6060f1SDimitry Andric }
3173fe6060f1SDimitry Andric 
31740b57cec5SDimitry Andric void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
31750b57cec5SDimitry Andric   Out << ", insts: " << FS->instCount();
3176*349cc55cSDimitry Andric   if (FS->fflags().anyFlagSet())
3177*349cc55cSDimitry Andric     Out << ", " << FS->fflags();
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric   if (!FS->calls().empty()) {
31800b57cec5SDimitry Andric     Out << ", calls: (";
31810b57cec5SDimitry Andric     FieldSeparator IFS;
31820b57cec5SDimitry Andric     for (auto &Call : FS->calls()) {
31830b57cec5SDimitry Andric       Out << IFS;
31840b57cec5SDimitry Andric       Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
31850b57cec5SDimitry Andric       if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
31860b57cec5SDimitry Andric         Out << ", hotness: " << getHotnessName(Call.second.getHotness());
31870b57cec5SDimitry Andric       else if (Call.second.RelBlockFreq)
31880b57cec5SDimitry Andric         Out << ", relbf: " << Call.second.RelBlockFreq;
31890b57cec5SDimitry Andric       Out << ")";
31900b57cec5SDimitry Andric     }
31910b57cec5SDimitry Andric     Out << ")";
31920b57cec5SDimitry Andric   }
31930b57cec5SDimitry Andric 
31940b57cec5SDimitry Andric   if (const auto *TIdInfo = FS->getTypeIdInfo())
31950b57cec5SDimitry Andric     printTypeIdInfo(*TIdInfo);
31965ffd83dbSDimitry Andric 
31975ffd83dbSDimitry Andric   auto PrintRange = [&](const ConstantRange &Range) {
3198e8d8bef9SDimitry Andric     Out << "[" << Range.getSignedMin() << ", " << Range.getSignedMax() << "]";
31995ffd83dbSDimitry Andric   };
32005ffd83dbSDimitry Andric 
32015ffd83dbSDimitry Andric   if (!FS->paramAccesses().empty()) {
32025ffd83dbSDimitry Andric     Out << ", params: (";
32035ffd83dbSDimitry Andric     FieldSeparator IFS;
32045ffd83dbSDimitry Andric     for (auto &PS : FS->paramAccesses()) {
32055ffd83dbSDimitry Andric       Out << IFS;
32065ffd83dbSDimitry Andric       Out << "(param: " << PS.ParamNo;
32075ffd83dbSDimitry Andric       Out << ", offset: ";
32085ffd83dbSDimitry Andric       PrintRange(PS.Use);
32095ffd83dbSDimitry Andric       if (!PS.Calls.empty()) {
32105ffd83dbSDimitry Andric         Out << ", calls: (";
32115ffd83dbSDimitry Andric         FieldSeparator IFS;
32125ffd83dbSDimitry Andric         for (auto &Call : PS.Calls) {
32135ffd83dbSDimitry Andric           Out << IFS;
3214e8d8bef9SDimitry Andric           Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee.getGUID());
32155ffd83dbSDimitry Andric           Out << ", param: " << Call.ParamNo;
32165ffd83dbSDimitry Andric           Out << ", offset: ";
32175ffd83dbSDimitry Andric           PrintRange(Call.Offsets);
32185ffd83dbSDimitry Andric           Out << ")";
32195ffd83dbSDimitry Andric         }
32205ffd83dbSDimitry Andric         Out << ")";
32215ffd83dbSDimitry Andric       }
32225ffd83dbSDimitry Andric       Out << ")";
32235ffd83dbSDimitry Andric     }
32245ffd83dbSDimitry Andric     Out << ")";
32255ffd83dbSDimitry Andric   }
32260b57cec5SDimitry Andric }
32270b57cec5SDimitry Andric 
32280b57cec5SDimitry Andric void AssemblyWriter::printTypeIdInfo(
32290b57cec5SDimitry Andric     const FunctionSummary::TypeIdInfo &TIDInfo) {
32300b57cec5SDimitry Andric   Out << ", typeIdInfo: (";
32310b57cec5SDimitry Andric   FieldSeparator TIDFS;
32320b57cec5SDimitry Andric   if (!TIDInfo.TypeTests.empty()) {
32330b57cec5SDimitry Andric     Out << TIDFS;
32340b57cec5SDimitry Andric     Out << "typeTests: (";
32350b57cec5SDimitry Andric     FieldSeparator FS;
32360b57cec5SDimitry Andric     for (auto &GUID : TIDInfo.TypeTests) {
32370b57cec5SDimitry Andric       auto TidIter = TheIndex->typeIds().equal_range(GUID);
32380b57cec5SDimitry Andric       if (TidIter.first == TidIter.second) {
32390b57cec5SDimitry Andric         Out << FS;
32400b57cec5SDimitry Andric         Out << GUID;
32410b57cec5SDimitry Andric         continue;
32420b57cec5SDimitry Andric       }
32430b57cec5SDimitry Andric       // Print all type id that correspond to this GUID.
32440b57cec5SDimitry Andric       for (auto It = TidIter.first; It != TidIter.second; ++It) {
32450b57cec5SDimitry Andric         Out << FS;
32460b57cec5SDimitry Andric         auto Slot = Machine.getTypeIdSlot(It->second.first);
32470b57cec5SDimitry Andric         assert(Slot != -1);
32480b57cec5SDimitry Andric         Out << "^" << Slot;
32490b57cec5SDimitry Andric       }
32500b57cec5SDimitry Andric     }
32510b57cec5SDimitry Andric     Out << ")";
32520b57cec5SDimitry Andric   }
32530b57cec5SDimitry Andric   if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
32540b57cec5SDimitry Andric     Out << TIDFS;
32550b57cec5SDimitry Andric     printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
32560b57cec5SDimitry Andric   }
32570b57cec5SDimitry Andric   if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
32580b57cec5SDimitry Andric     Out << TIDFS;
32590b57cec5SDimitry Andric     printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
32600b57cec5SDimitry Andric   }
32610b57cec5SDimitry Andric   if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
32620b57cec5SDimitry Andric     Out << TIDFS;
32630b57cec5SDimitry Andric     printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
32640b57cec5SDimitry Andric                      "typeTestAssumeConstVCalls");
32650b57cec5SDimitry Andric   }
32660b57cec5SDimitry Andric   if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
32670b57cec5SDimitry Andric     Out << TIDFS;
32680b57cec5SDimitry Andric     printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
32690b57cec5SDimitry Andric                      "typeCheckedLoadConstVCalls");
32700b57cec5SDimitry Andric   }
32710b57cec5SDimitry Andric   Out << ")";
32720b57cec5SDimitry Andric }
32730b57cec5SDimitry Andric 
32740b57cec5SDimitry Andric void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
32750b57cec5SDimitry Andric   auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID);
32760b57cec5SDimitry Andric   if (TidIter.first == TidIter.second) {
32770b57cec5SDimitry Andric     Out << "vFuncId: (";
32780b57cec5SDimitry Andric     Out << "guid: " << VFId.GUID;
32790b57cec5SDimitry Andric     Out << ", offset: " << VFId.Offset;
32800b57cec5SDimitry Andric     Out << ")";
32810b57cec5SDimitry Andric     return;
32820b57cec5SDimitry Andric   }
32830b57cec5SDimitry Andric   // Print all type id that correspond to this GUID.
32840b57cec5SDimitry Andric   FieldSeparator FS;
32850b57cec5SDimitry Andric   for (auto It = TidIter.first; It != TidIter.second; ++It) {
32860b57cec5SDimitry Andric     Out << FS;
32870b57cec5SDimitry Andric     Out << "vFuncId: (";
32880b57cec5SDimitry Andric     auto Slot = Machine.getTypeIdSlot(It->second.first);
32890b57cec5SDimitry Andric     assert(Slot != -1);
32900b57cec5SDimitry Andric     Out << "^" << Slot;
32910b57cec5SDimitry Andric     Out << ", offset: " << VFId.Offset;
32920b57cec5SDimitry Andric     Out << ")";
32930b57cec5SDimitry Andric   }
32940b57cec5SDimitry Andric }
32950b57cec5SDimitry Andric 
32960b57cec5SDimitry Andric void AssemblyWriter::printNonConstVCalls(
32975ffd83dbSDimitry Andric     const std::vector<FunctionSummary::VFuncId> &VCallList, const char *Tag) {
32980b57cec5SDimitry Andric   Out << Tag << ": (";
32990b57cec5SDimitry Andric   FieldSeparator FS;
33000b57cec5SDimitry Andric   for (auto &VFuncId : VCallList) {
33010b57cec5SDimitry Andric     Out << FS;
33020b57cec5SDimitry Andric     printVFuncId(VFuncId);
33030b57cec5SDimitry Andric   }
33040b57cec5SDimitry Andric   Out << ")";
33050b57cec5SDimitry Andric }
33060b57cec5SDimitry Andric 
33070b57cec5SDimitry Andric void AssemblyWriter::printConstVCalls(
33085ffd83dbSDimitry Andric     const std::vector<FunctionSummary::ConstVCall> &VCallList,
33095ffd83dbSDimitry Andric     const char *Tag) {
33100b57cec5SDimitry Andric   Out << Tag << ": (";
33110b57cec5SDimitry Andric   FieldSeparator FS;
33120b57cec5SDimitry Andric   for (auto &ConstVCall : VCallList) {
33130b57cec5SDimitry Andric     Out << FS;
33140b57cec5SDimitry Andric     Out << "(";
33150b57cec5SDimitry Andric     printVFuncId(ConstVCall.VFunc);
33160b57cec5SDimitry Andric     if (!ConstVCall.Args.empty()) {
33170b57cec5SDimitry Andric       Out << ", ";
33180b57cec5SDimitry Andric       printArgs(ConstVCall.Args);
33190b57cec5SDimitry Andric     }
33200b57cec5SDimitry Andric     Out << ")";
33210b57cec5SDimitry Andric   }
33220b57cec5SDimitry Andric   Out << ")";
33230b57cec5SDimitry Andric }
33240b57cec5SDimitry Andric 
33250b57cec5SDimitry Andric void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
33260b57cec5SDimitry Andric   GlobalValueSummary::GVFlags GVFlags = Summary.flags();
33270b57cec5SDimitry Andric   GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
33280b57cec5SDimitry Andric   Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
33290b57cec5SDimitry Andric   Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
33300b57cec5SDimitry Andric       << ", flags: (";
33310b57cec5SDimitry Andric   Out << "linkage: " << getLinkageName(LT);
3332fe6060f1SDimitry Andric   Out << ", visibility: "
3333fe6060f1SDimitry Andric       << getVisibilityName((GlobalValue::VisibilityTypes)GVFlags.Visibility);
33340b57cec5SDimitry Andric   Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
33350b57cec5SDimitry Andric   Out << ", live: " << GVFlags.Live;
33360b57cec5SDimitry Andric   Out << ", dsoLocal: " << GVFlags.DSOLocal;
33370b57cec5SDimitry Andric   Out << ", canAutoHide: " << GVFlags.CanAutoHide;
33380b57cec5SDimitry Andric   Out << ")";
33390b57cec5SDimitry Andric 
33400b57cec5SDimitry Andric   if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
33410b57cec5SDimitry Andric     printAliasSummary(cast<AliasSummary>(&Summary));
33420b57cec5SDimitry Andric   else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
33430b57cec5SDimitry Andric     printFunctionSummary(cast<FunctionSummary>(&Summary));
33440b57cec5SDimitry Andric   else
33450b57cec5SDimitry Andric     printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
33460b57cec5SDimitry Andric 
33470b57cec5SDimitry Andric   auto RefList = Summary.refs();
33480b57cec5SDimitry Andric   if (!RefList.empty()) {
33490b57cec5SDimitry Andric     Out << ", refs: (";
33500b57cec5SDimitry Andric     FieldSeparator FS;
33510b57cec5SDimitry Andric     for (auto &Ref : RefList) {
33520b57cec5SDimitry Andric       Out << FS;
33530b57cec5SDimitry Andric       if (Ref.isReadOnly())
33540b57cec5SDimitry Andric         Out << "readonly ";
33550b57cec5SDimitry Andric       else if (Ref.isWriteOnly())
33560b57cec5SDimitry Andric         Out << "writeonly ";
33570b57cec5SDimitry Andric       Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
33580b57cec5SDimitry Andric     }
33590b57cec5SDimitry Andric     Out << ")";
33600b57cec5SDimitry Andric   }
33610b57cec5SDimitry Andric 
33620b57cec5SDimitry Andric   Out << ")";
33630b57cec5SDimitry Andric }
33640b57cec5SDimitry Andric 
33650b57cec5SDimitry Andric void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
33660b57cec5SDimitry Andric   Out << "^" << Slot << " = gv: (";
33670b57cec5SDimitry Andric   if (!VI.name().empty())
33680b57cec5SDimitry Andric     Out << "name: \"" << VI.name() << "\"";
33690b57cec5SDimitry Andric   else
33700b57cec5SDimitry Andric     Out << "guid: " << VI.getGUID();
33710b57cec5SDimitry Andric   if (!VI.getSummaryList().empty()) {
33720b57cec5SDimitry Andric     Out << ", summaries: (";
33730b57cec5SDimitry Andric     FieldSeparator FS;
33740b57cec5SDimitry Andric     for (auto &Summary : VI.getSummaryList()) {
33750b57cec5SDimitry Andric       Out << FS;
33760b57cec5SDimitry Andric       printSummary(*Summary);
33770b57cec5SDimitry Andric     }
33780b57cec5SDimitry Andric     Out << ")";
33790b57cec5SDimitry Andric   }
33800b57cec5SDimitry Andric   Out << ")";
33810b57cec5SDimitry Andric   if (!VI.name().empty())
33820b57cec5SDimitry Andric     Out << " ; guid = " << VI.getGUID();
33830b57cec5SDimitry Andric   Out << "\n";
33840b57cec5SDimitry Andric }
33850b57cec5SDimitry Andric 
33860b57cec5SDimitry Andric static void printMetadataIdentifier(StringRef Name,
33870b57cec5SDimitry Andric                                     formatted_raw_ostream &Out) {
33880b57cec5SDimitry Andric   if (Name.empty()) {
33890b57cec5SDimitry Andric     Out << "<empty name> ";
33900b57cec5SDimitry Andric   } else {
33910b57cec5SDimitry Andric     if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
33920b57cec5SDimitry Andric         Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
33930b57cec5SDimitry Andric       Out << Name[0];
33940b57cec5SDimitry Andric     else
33950b57cec5SDimitry Andric       Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
33960b57cec5SDimitry Andric     for (unsigned i = 1, e = Name.size(); i != e; ++i) {
33970b57cec5SDimitry Andric       unsigned char C = Name[i];
33980b57cec5SDimitry Andric       if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
33990b57cec5SDimitry Andric           C == '.' || C == '_')
34000b57cec5SDimitry Andric         Out << C;
34010b57cec5SDimitry Andric       else
34020b57cec5SDimitry Andric         Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
34030b57cec5SDimitry Andric     }
34040b57cec5SDimitry Andric   }
34050b57cec5SDimitry Andric }
34060b57cec5SDimitry Andric 
34070b57cec5SDimitry Andric void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
34080b57cec5SDimitry Andric   Out << '!';
34090b57cec5SDimitry Andric   printMetadataIdentifier(NMD->getName(), Out);
34100b57cec5SDimitry Andric   Out << " = !{";
34110b57cec5SDimitry Andric   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
34120b57cec5SDimitry Andric     if (i)
34130b57cec5SDimitry Andric       Out << ", ";
34140b57cec5SDimitry Andric 
34150b57cec5SDimitry Andric     // Write DIExpressions inline.
34160b57cec5SDimitry Andric     // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
34170b57cec5SDimitry Andric     MDNode *Op = NMD->getOperand(i);
3418fe6060f1SDimitry Andric     assert(!isa<DIArgList>(Op) &&
3419fe6060f1SDimitry Andric            "DIArgLists should not appear in NamedMDNodes");
34200b57cec5SDimitry Andric     if (auto *Expr = dyn_cast<DIExpression>(Op)) {
3421*349cc55cSDimitry Andric       writeDIExpression(Out, Expr, AsmWriterContext::getEmpty());
34220b57cec5SDimitry Andric       continue;
34230b57cec5SDimitry Andric     }
34240b57cec5SDimitry Andric 
34250b57cec5SDimitry Andric     int Slot = Machine.getMetadataSlot(Op);
34260b57cec5SDimitry Andric     if (Slot == -1)
34270b57cec5SDimitry Andric       Out << "<badref>";
34280b57cec5SDimitry Andric     else
34290b57cec5SDimitry Andric       Out << '!' << Slot;
34300b57cec5SDimitry Andric   }
34310b57cec5SDimitry Andric   Out << "}\n";
34320b57cec5SDimitry Andric }
34330b57cec5SDimitry Andric 
34340b57cec5SDimitry Andric static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
34350b57cec5SDimitry Andric                             formatted_raw_ostream &Out) {
34360b57cec5SDimitry Andric   switch (Vis) {
34370b57cec5SDimitry Andric   case GlobalValue::DefaultVisibility: break;
34380b57cec5SDimitry Andric   case GlobalValue::HiddenVisibility:    Out << "hidden "; break;
34390b57cec5SDimitry Andric   case GlobalValue::ProtectedVisibility: Out << "protected "; break;
34400b57cec5SDimitry Andric   }
34410b57cec5SDimitry Andric }
34420b57cec5SDimitry Andric 
34430b57cec5SDimitry Andric static void PrintDSOLocation(const GlobalValue &GV,
34440b57cec5SDimitry Andric                              formatted_raw_ostream &Out) {
34455ffd83dbSDimitry Andric   if (GV.isDSOLocal() && !GV.isImplicitDSOLocal())
34460b57cec5SDimitry Andric     Out << "dso_local ";
34470b57cec5SDimitry Andric }
34480b57cec5SDimitry Andric 
34490b57cec5SDimitry Andric static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
34500b57cec5SDimitry Andric                                  formatted_raw_ostream &Out) {
34510b57cec5SDimitry Andric   switch (SCT) {
34520b57cec5SDimitry Andric   case GlobalValue::DefaultStorageClass: break;
34530b57cec5SDimitry Andric   case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
34540b57cec5SDimitry Andric   case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
34550b57cec5SDimitry Andric   }
34560b57cec5SDimitry Andric }
34570b57cec5SDimitry Andric 
34580b57cec5SDimitry Andric static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
34590b57cec5SDimitry Andric                                   formatted_raw_ostream &Out) {
34600b57cec5SDimitry Andric   switch (TLM) {
34610b57cec5SDimitry Andric     case GlobalVariable::NotThreadLocal:
34620b57cec5SDimitry Andric       break;
34630b57cec5SDimitry Andric     case GlobalVariable::GeneralDynamicTLSModel:
34640b57cec5SDimitry Andric       Out << "thread_local ";
34650b57cec5SDimitry Andric       break;
34660b57cec5SDimitry Andric     case GlobalVariable::LocalDynamicTLSModel:
34670b57cec5SDimitry Andric       Out << "thread_local(localdynamic) ";
34680b57cec5SDimitry Andric       break;
34690b57cec5SDimitry Andric     case GlobalVariable::InitialExecTLSModel:
34700b57cec5SDimitry Andric       Out << "thread_local(initialexec) ";
34710b57cec5SDimitry Andric       break;
34720b57cec5SDimitry Andric     case GlobalVariable::LocalExecTLSModel:
34730b57cec5SDimitry Andric       Out << "thread_local(localexec) ";
34740b57cec5SDimitry Andric       break;
34750b57cec5SDimitry Andric   }
34760b57cec5SDimitry Andric }
34770b57cec5SDimitry Andric 
34780b57cec5SDimitry Andric static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
34790b57cec5SDimitry Andric   switch (UA) {
34800b57cec5SDimitry Andric   case GlobalVariable::UnnamedAddr::None:
34810b57cec5SDimitry Andric     return "";
34820b57cec5SDimitry Andric   case GlobalVariable::UnnamedAddr::Local:
34830b57cec5SDimitry Andric     return "local_unnamed_addr";
34840b57cec5SDimitry Andric   case GlobalVariable::UnnamedAddr::Global:
34850b57cec5SDimitry Andric     return "unnamed_addr";
34860b57cec5SDimitry Andric   }
34870b57cec5SDimitry Andric   llvm_unreachable("Unknown UnnamedAddr");
34880b57cec5SDimitry Andric }
34890b57cec5SDimitry Andric 
34900b57cec5SDimitry Andric static void maybePrintComdat(formatted_raw_ostream &Out,
34910b57cec5SDimitry Andric                              const GlobalObject &GO) {
34920b57cec5SDimitry Andric   const Comdat *C = GO.getComdat();
34930b57cec5SDimitry Andric   if (!C)
34940b57cec5SDimitry Andric     return;
34950b57cec5SDimitry Andric 
34960b57cec5SDimitry Andric   if (isa<GlobalVariable>(GO))
34970b57cec5SDimitry Andric     Out << ',';
34980b57cec5SDimitry Andric   Out << " comdat";
34990b57cec5SDimitry Andric 
35000b57cec5SDimitry Andric   if (GO.getName() == C->getName())
35010b57cec5SDimitry Andric     return;
35020b57cec5SDimitry Andric 
35030b57cec5SDimitry Andric   Out << '(';
35040b57cec5SDimitry Andric   PrintLLVMName(Out, C->getName(), ComdatPrefix);
35050b57cec5SDimitry Andric   Out << ')';
35060b57cec5SDimitry Andric }
35070b57cec5SDimitry Andric 
35080b57cec5SDimitry Andric void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
35090b57cec5SDimitry Andric   if (GV->isMaterializable())
35100b57cec5SDimitry Andric     Out << "; Materializable\n";
35110b57cec5SDimitry Andric 
3512*349cc55cSDimitry Andric   AsmWriterContext WriterCtx(&TypePrinter, &Machine, GV->getParent());
3513*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, GV, WriterCtx);
35140b57cec5SDimitry Andric   Out << " = ";
35150b57cec5SDimitry Andric 
35160b57cec5SDimitry Andric   if (!GV->hasInitializer() && GV->hasExternalLinkage())
35170b57cec5SDimitry Andric     Out << "external ";
35180b57cec5SDimitry Andric 
35190b57cec5SDimitry Andric   Out << getLinkageNameWithSpace(GV->getLinkage());
35200b57cec5SDimitry Andric   PrintDSOLocation(*GV, Out);
35210b57cec5SDimitry Andric   PrintVisibility(GV->getVisibility(), Out);
35220b57cec5SDimitry Andric   PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
35230b57cec5SDimitry Andric   PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
35240b57cec5SDimitry Andric   StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
35250b57cec5SDimitry Andric   if (!UA.empty())
35260b57cec5SDimitry Andric       Out << UA << ' ';
35270b57cec5SDimitry Andric 
35280b57cec5SDimitry Andric   if (unsigned AddressSpace = GV->getType()->getAddressSpace())
35290b57cec5SDimitry Andric     Out << "addrspace(" << AddressSpace << ") ";
35300b57cec5SDimitry Andric   if (GV->isExternallyInitialized()) Out << "externally_initialized ";
35310b57cec5SDimitry Andric   Out << (GV->isConstant() ? "constant " : "global ");
35320b57cec5SDimitry Andric   TypePrinter.print(GV->getValueType(), Out);
35330b57cec5SDimitry Andric 
35340b57cec5SDimitry Andric   if (GV->hasInitializer()) {
35350b57cec5SDimitry Andric     Out << ' ';
35360b57cec5SDimitry Andric     writeOperand(GV->getInitializer(), false);
35370b57cec5SDimitry Andric   }
35380b57cec5SDimitry Andric 
35390b57cec5SDimitry Andric   if (GV->hasSection()) {
35400b57cec5SDimitry Andric     Out << ", section \"";
35410b57cec5SDimitry Andric     printEscapedString(GV->getSection(), Out);
35420b57cec5SDimitry Andric     Out << '"';
35430b57cec5SDimitry Andric   }
35440b57cec5SDimitry Andric   if (GV->hasPartition()) {
35450b57cec5SDimitry Andric     Out << ", partition \"";
35460b57cec5SDimitry Andric     printEscapedString(GV->getPartition(), Out);
35470b57cec5SDimitry Andric     Out << '"';
35480b57cec5SDimitry Andric   }
35490b57cec5SDimitry Andric 
35500b57cec5SDimitry Andric   maybePrintComdat(Out, *GV);
35510b57cec5SDimitry Andric   if (GV->getAlignment())
35520b57cec5SDimitry Andric     Out << ", align " << GV->getAlignment();
35530b57cec5SDimitry Andric 
35540b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
35550b57cec5SDimitry Andric   GV->getAllMetadata(MDs);
35560b57cec5SDimitry Andric   printMetadataAttachments(MDs, ", ");
35570b57cec5SDimitry Andric 
35580b57cec5SDimitry Andric   auto Attrs = GV->getAttributes();
35590b57cec5SDimitry Andric   if (Attrs.hasAttributes())
35600b57cec5SDimitry Andric     Out << " #" << Machine.getAttributeGroupSlot(Attrs);
35610b57cec5SDimitry Andric 
35620b57cec5SDimitry Andric   printInfoComment(*GV);
35630b57cec5SDimitry Andric }
35640b57cec5SDimitry Andric 
3565*349cc55cSDimitry Andric void AssemblyWriter::printAlias(const GlobalAlias *GA) {
3566*349cc55cSDimitry Andric   if (GA->isMaterializable())
35670b57cec5SDimitry Andric     Out << "; Materializable\n";
35680b57cec5SDimitry Andric 
3569*349cc55cSDimitry Andric   AsmWriterContext WriterCtx(&TypePrinter, &Machine, GA->getParent());
3570*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, GA, WriterCtx);
35710b57cec5SDimitry Andric   Out << " = ";
35720b57cec5SDimitry Andric 
3573*349cc55cSDimitry Andric   Out << getLinkageNameWithSpace(GA->getLinkage());
3574*349cc55cSDimitry Andric   PrintDSOLocation(*GA, Out);
3575*349cc55cSDimitry Andric   PrintVisibility(GA->getVisibility(), Out);
3576*349cc55cSDimitry Andric   PrintDLLStorageClass(GA->getDLLStorageClass(), Out);
3577*349cc55cSDimitry Andric   PrintThreadLocalModel(GA->getThreadLocalMode(), Out);
3578*349cc55cSDimitry Andric   StringRef UA = getUnnamedAddrEncoding(GA->getUnnamedAddr());
35790b57cec5SDimitry Andric   if (!UA.empty())
35800b57cec5SDimitry Andric       Out << UA << ' ';
35810b57cec5SDimitry Andric 
35820b57cec5SDimitry Andric   Out << "alias ";
35830b57cec5SDimitry Andric 
3584*349cc55cSDimitry Andric   TypePrinter.print(GA->getValueType(), Out);
35850b57cec5SDimitry Andric   Out << ", ";
35860b57cec5SDimitry Andric 
3587*349cc55cSDimitry Andric   if (const Constant *Aliasee = GA->getAliasee()) {
3588*349cc55cSDimitry Andric     writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee));
35890b57cec5SDimitry Andric   } else {
3590*349cc55cSDimitry Andric     TypePrinter.print(GA->getType(), Out);
3591*349cc55cSDimitry Andric     Out << " <<NULL ALIASEE>>";
35920b57cec5SDimitry Andric   }
35930b57cec5SDimitry Andric 
3594*349cc55cSDimitry Andric   if (GA->hasPartition()) {
35950b57cec5SDimitry Andric     Out << ", partition \"";
3596*349cc55cSDimitry Andric     printEscapedString(GA->getPartition(), Out);
35970b57cec5SDimitry Andric     Out << '"';
35980b57cec5SDimitry Andric   }
35990b57cec5SDimitry Andric 
3600*349cc55cSDimitry Andric   printInfoComment(*GA);
3601*349cc55cSDimitry Andric   Out << '\n';
3602*349cc55cSDimitry Andric }
3603*349cc55cSDimitry Andric 
3604*349cc55cSDimitry Andric void AssemblyWriter::printIFunc(const GlobalIFunc *GI) {
3605*349cc55cSDimitry Andric   if (GI->isMaterializable())
3606*349cc55cSDimitry Andric     Out << "; Materializable\n";
3607*349cc55cSDimitry Andric 
3608*349cc55cSDimitry Andric   AsmWriterContext WriterCtx(&TypePrinter, &Machine, GI->getParent());
3609*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, GI, WriterCtx);
3610*349cc55cSDimitry Andric   Out << " = ";
3611*349cc55cSDimitry Andric 
3612*349cc55cSDimitry Andric   Out << getLinkageNameWithSpace(GI->getLinkage());
3613*349cc55cSDimitry Andric   PrintDSOLocation(*GI, Out);
3614*349cc55cSDimitry Andric   PrintVisibility(GI->getVisibility(), Out);
3615*349cc55cSDimitry Andric 
3616*349cc55cSDimitry Andric   Out << "ifunc ";
3617*349cc55cSDimitry Andric 
3618*349cc55cSDimitry Andric   TypePrinter.print(GI->getValueType(), Out);
3619*349cc55cSDimitry Andric   Out << ", ";
3620*349cc55cSDimitry Andric 
3621*349cc55cSDimitry Andric   if (const Constant *Resolver = GI->getResolver()) {
3622*349cc55cSDimitry Andric     writeOperand(Resolver, !isa<ConstantExpr>(Resolver));
3623*349cc55cSDimitry Andric   } else {
3624*349cc55cSDimitry Andric     TypePrinter.print(GI->getType(), Out);
3625*349cc55cSDimitry Andric     Out << " <<NULL RESOLVER>>";
3626*349cc55cSDimitry Andric   }
3627*349cc55cSDimitry Andric 
3628*349cc55cSDimitry Andric   if (GI->hasPartition()) {
3629*349cc55cSDimitry Andric     Out << ", partition \"";
3630*349cc55cSDimitry Andric     printEscapedString(GI->getPartition(), Out);
3631*349cc55cSDimitry Andric     Out << '"';
3632*349cc55cSDimitry Andric   }
3633*349cc55cSDimitry Andric 
3634*349cc55cSDimitry Andric   printInfoComment(*GI);
36350b57cec5SDimitry Andric   Out << '\n';
36360b57cec5SDimitry Andric }
36370b57cec5SDimitry Andric 
36380b57cec5SDimitry Andric void AssemblyWriter::printComdat(const Comdat *C) {
36390b57cec5SDimitry Andric   C->print(Out);
36400b57cec5SDimitry Andric }
36410b57cec5SDimitry Andric 
36420b57cec5SDimitry Andric void AssemblyWriter::printTypeIdentities() {
36430b57cec5SDimitry Andric   if (TypePrinter.empty())
36440b57cec5SDimitry Andric     return;
36450b57cec5SDimitry Andric 
36460b57cec5SDimitry Andric   Out << '\n';
36470b57cec5SDimitry Andric 
36480b57cec5SDimitry Andric   // Emit all numbered types.
36490b57cec5SDimitry Andric   auto &NumberedTypes = TypePrinter.getNumberedTypes();
36500b57cec5SDimitry Andric   for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
36510b57cec5SDimitry Andric     Out << '%' << I << " = type ";
36520b57cec5SDimitry Andric 
36530b57cec5SDimitry Andric     // Make sure we print out at least one level of the type structure, so
36540b57cec5SDimitry Andric     // that we do not get %2 = type %2
36550b57cec5SDimitry Andric     TypePrinter.printStructBody(NumberedTypes[I], Out);
36560b57cec5SDimitry Andric     Out << '\n';
36570b57cec5SDimitry Andric   }
36580b57cec5SDimitry Andric 
36590b57cec5SDimitry Andric   auto &NamedTypes = TypePrinter.getNamedTypes();
36600b57cec5SDimitry Andric   for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
36610b57cec5SDimitry Andric     PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
36620b57cec5SDimitry Andric     Out << " = type ";
36630b57cec5SDimitry Andric 
36640b57cec5SDimitry Andric     // Make sure we print out at least one level of the type structure, so
36650b57cec5SDimitry Andric     // that we do not get %FILE = type %FILE
36660b57cec5SDimitry Andric     TypePrinter.printStructBody(NamedTypes[I], Out);
36670b57cec5SDimitry Andric     Out << '\n';
36680b57cec5SDimitry Andric   }
36690b57cec5SDimitry Andric }
36700b57cec5SDimitry Andric 
36710b57cec5SDimitry Andric /// printFunction - Print all aspects of a function.
36720b57cec5SDimitry Andric void AssemblyWriter::printFunction(const Function *F) {
36730b57cec5SDimitry Andric   if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
36740b57cec5SDimitry Andric 
36750b57cec5SDimitry Andric   if (F->isMaterializable())
36760b57cec5SDimitry Andric     Out << "; Materializable\n";
36770b57cec5SDimitry Andric 
36780b57cec5SDimitry Andric   const AttributeList &Attrs = F->getAttributes();
3679*349cc55cSDimitry Andric   if (Attrs.hasFnAttrs()) {
3680*349cc55cSDimitry Andric     AttributeSet AS = Attrs.getFnAttrs();
36810b57cec5SDimitry Andric     std::string AttrStr;
36820b57cec5SDimitry Andric 
36830b57cec5SDimitry Andric     for (const Attribute &Attr : AS) {
36840b57cec5SDimitry Andric       if (!Attr.isStringAttribute()) {
36850b57cec5SDimitry Andric         if (!AttrStr.empty()) AttrStr += ' ';
36860b57cec5SDimitry Andric         AttrStr += Attr.getAsString();
36870b57cec5SDimitry Andric       }
36880b57cec5SDimitry Andric     }
36890b57cec5SDimitry Andric 
36900b57cec5SDimitry Andric     if (!AttrStr.empty())
36910b57cec5SDimitry Andric       Out << "; Function Attrs: " << AttrStr << '\n';
36920b57cec5SDimitry Andric   }
36930b57cec5SDimitry Andric 
36940b57cec5SDimitry Andric   Machine.incorporateFunction(F);
36950b57cec5SDimitry Andric 
36960b57cec5SDimitry Andric   if (F->isDeclaration()) {
36970b57cec5SDimitry Andric     Out << "declare";
36980b57cec5SDimitry Andric     SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
36990b57cec5SDimitry Andric     F->getAllMetadata(MDs);
37000b57cec5SDimitry Andric     printMetadataAttachments(MDs, " ");
37010b57cec5SDimitry Andric     Out << ' ';
37020b57cec5SDimitry Andric   } else
37030b57cec5SDimitry Andric     Out << "define ";
37040b57cec5SDimitry Andric 
37050b57cec5SDimitry Andric   Out << getLinkageNameWithSpace(F->getLinkage());
37060b57cec5SDimitry Andric   PrintDSOLocation(*F, Out);
37070b57cec5SDimitry Andric   PrintVisibility(F->getVisibility(), Out);
37080b57cec5SDimitry Andric   PrintDLLStorageClass(F->getDLLStorageClass(), Out);
37090b57cec5SDimitry Andric 
37100b57cec5SDimitry Andric   // Print the calling convention.
37110b57cec5SDimitry Andric   if (F->getCallingConv() != CallingConv::C) {
37120b57cec5SDimitry Andric     PrintCallingConv(F->getCallingConv(), Out);
37130b57cec5SDimitry Andric     Out << " ";
37140b57cec5SDimitry Andric   }
37150b57cec5SDimitry Andric 
37160b57cec5SDimitry Andric   FunctionType *FT = F->getFunctionType();
3717*349cc55cSDimitry Andric   if (Attrs.hasRetAttrs())
37180b57cec5SDimitry Andric     Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
37190b57cec5SDimitry Andric   TypePrinter.print(F->getReturnType(), Out);
3720*349cc55cSDimitry Andric   AsmWriterContext WriterCtx(&TypePrinter, &Machine, F->getParent());
37210b57cec5SDimitry Andric   Out << ' ';
3722*349cc55cSDimitry Andric   WriteAsOperandInternal(Out, F, WriterCtx);
37230b57cec5SDimitry Andric   Out << '(';
37240b57cec5SDimitry Andric 
37250b57cec5SDimitry Andric   // Loop over the arguments, printing them...
37260b57cec5SDimitry Andric   if (F->isDeclaration() && !IsForDebug) {
37270b57cec5SDimitry Andric     // We're only interested in the type here - don't print argument names.
37280b57cec5SDimitry Andric     for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
37290b57cec5SDimitry Andric       // Insert commas as we go... the first arg doesn't get a comma
37300b57cec5SDimitry Andric       if (I)
37310b57cec5SDimitry Andric         Out << ", ";
37320b57cec5SDimitry Andric       // Output type...
37330b57cec5SDimitry Andric       TypePrinter.print(FT->getParamType(I), Out);
37340b57cec5SDimitry Andric 
3735*349cc55cSDimitry Andric       AttributeSet ArgAttrs = Attrs.getParamAttrs(I);
3736480093f4SDimitry Andric       if (ArgAttrs.hasAttributes()) {
3737480093f4SDimitry Andric         Out << ' ';
3738480093f4SDimitry Andric         writeAttributeSet(ArgAttrs);
3739480093f4SDimitry Andric       }
37400b57cec5SDimitry Andric     }
37410b57cec5SDimitry Andric   } else {
37420b57cec5SDimitry Andric     // The arguments are meaningful here, print them in detail.
37430b57cec5SDimitry Andric     for (const Argument &Arg : F->args()) {
37440b57cec5SDimitry Andric       // Insert commas as we go... the first arg doesn't get a comma
37450b57cec5SDimitry Andric       if (Arg.getArgNo() != 0)
37460b57cec5SDimitry Andric         Out << ", ";
3747*349cc55cSDimitry Andric       printArgument(&Arg, Attrs.getParamAttrs(Arg.getArgNo()));
37480b57cec5SDimitry Andric     }
37490b57cec5SDimitry Andric   }
37500b57cec5SDimitry Andric 
37510b57cec5SDimitry Andric   // Finish printing arguments...
37520b57cec5SDimitry Andric   if (FT->isVarArg()) {
37530b57cec5SDimitry Andric     if (FT->getNumParams()) Out << ", ";
37540b57cec5SDimitry Andric     Out << "...";  // Output varargs portion of signature!
37550b57cec5SDimitry Andric   }
37560b57cec5SDimitry Andric   Out << ')';
37570b57cec5SDimitry Andric   StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
37580b57cec5SDimitry Andric   if (!UA.empty())
37590b57cec5SDimitry Andric     Out << ' ' << UA;
37600b57cec5SDimitry Andric   // We print the function address space if it is non-zero or if we are writing
37610b57cec5SDimitry Andric   // a module with a non-zero program address space or if there is no valid
37620b57cec5SDimitry Andric   // Module* so that the file can be parsed without the datalayout string.
37630b57cec5SDimitry Andric   const Module *Mod = F->getParent();
37640b57cec5SDimitry Andric   if (F->getAddressSpace() != 0 || !Mod ||
37650b57cec5SDimitry Andric       Mod->getDataLayout().getProgramAddressSpace() != 0)
37660b57cec5SDimitry Andric     Out << " addrspace(" << F->getAddressSpace() << ")";
3767*349cc55cSDimitry Andric   if (Attrs.hasFnAttrs())
3768*349cc55cSDimitry Andric     Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttrs());
37690b57cec5SDimitry Andric   if (F->hasSection()) {
37700b57cec5SDimitry Andric     Out << " section \"";
37710b57cec5SDimitry Andric     printEscapedString(F->getSection(), Out);
37720b57cec5SDimitry Andric     Out << '"';
37730b57cec5SDimitry Andric   }
37740b57cec5SDimitry Andric   if (F->hasPartition()) {
37750b57cec5SDimitry Andric     Out << " partition \"";
37760b57cec5SDimitry Andric     printEscapedString(F->getPartition(), Out);
37770b57cec5SDimitry Andric     Out << '"';
37780b57cec5SDimitry Andric   }
37790b57cec5SDimitry Andric   maybePrintComdat(Out, *F);
37800b57cec5SDimitry Andric   if (F->getAlignment())
37810b57cec5SDimitry Andric     Out << " align " << F->getAlignment();
37820b57cec5SDimitry Andric   if (F->hasGC())
37830b57cec5SDimitry Andric     Out << " gc \"" << F->getGC() << '"';
37840b57cec5SDimitry Andric   if (F->hasPrefixData()) {
37850b57cec5SDimitry Andric     Out << " prefix ";
37860b57cec5SDimitry Andric     writeOperand(F->getPrefixData(), true);
37870b57cec5SDimitry Andric   }
37880b57cec5SDimitry Andric   if (F->hasPrologueData()) {
37890b57cec5SDimitry Andric     Out << " prologue ";
37900b57cec5SDimitry Andric     writeOperand(F->getPrologueData(), true);
37910b57cec5SDimitry Andric   }
37920b57cec5SDimitry Andric   if (F->hasPersonalityFn()) {
37930b57cec5SDimitry Andric     Out << " personality ";
37940b57cec5SDimitry Andric     writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
37950b57cec5SDimitry Andric   }
37960b57cec5SDimitry Andric 
37970b57cec5SDimitry Andric   if (F->isDeclaration()) {
37980b57cec5SDimitry Andric     Out << '\n';
37990b57cec5SDimitry Andric   } else {
38000b57cec5SDimitry Andric     SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
38010b57cec5SDimitry Andric     F->getAllMetadata(MDs);
38020b57cec5SDimitry Andric     printMetadataAttachments(MDs, " ");
38030b57cec5SDimitry Andric 
38040b57cec5SDimitry Andric     Out << " {";
38050b57cec5SDimitry Andric     // Output all of the function's basic blocks.
38060b57cec5SDimitry Andric     for (const BasicBlock &BB : *F)
38070b57cec5SDimitry Andric       printBasicBlock(&BB);
38080b57cec5SDimitry Andric 
38090b57cec5SDimitry Andric     // Output the function's use-lists.
38100b57cec5SDimitry Andric     printUseLists(F);
38110b57cec5SDimitry Andric 
38120b57cec5SDimitry Andric     Out << "}\n";
38130b57cec5SDimitry Andric   }
38140b57cec5SDimitry Andric 
38150b57cec5SDimitry Andric   Machine.purgeFunction();
38160b57cec5SDimitry Andric }
38170b57cec5SDimitry Andric 
38180b57cec5SDimitry Andric /// printArgument - This member is called for every argument that is passed into
38190b57cec5SDimitry Andric /// the function.  Simply print it out
38200b57cec5SDimitry Andric void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
38210b57cec5SDimitry Andric   // Output type...
38220b57cec5SDimitry Andric   TypePrinter.print(Arg->getType(), Out);
38230b57cec5SDimitry Andric 
38240b57cec5SDimitry Andric   // Output parameter attributes list
3825480093f4SDimitry Andric   if (Attrs.hasAttributes()) {
3826480093f4SDimitry Andric     Out << ' ';
3827480093f4SDimitry Andric     writeAttributeSet(Attrs);
3828480093f4SDimitry Andric   }
38290b57cec5SDimitry Andric 
38300b57cec5SDimitry Andric   // Output name, if available...
38310b57cec5SDimitry Andric   if (Arg->hasName()) {
38320b57cec5SDimitry Andric     Out << ' ';
38330b57cec5SDimitry Andric     PrintLLVMName(Out, Arg);
38348bcb0991SDimitry Andric   } else {
38358bcb0991SDimitry Andric     int Slot = Machine.getLocalSlot(Arg);
38368bcb0991SDimitry Andric     assert(Slot != -1 && "expect argument in function here");
38378bcb0991SDimitry Andric     Out << " %" << Slot;
38380b57cec5SDimitry Andric   }
38390b57cec5SDimitry Andric }
38400b57cec5SDimitry Andric 
38410b57cec5SDimitry Andric /// printBasicBlock - This member is called for each basic block in a method.
38420b57cec5SDimitry Andric void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
3843fe6060f1SDimitry Andric   bool IsEntryBlock = BB->getParent() && BB->isEntryBlock();
38440b57cec5SDimitry Andric   if (BB->hasName()) {              // Print out the label if it exists...
38450b57cec5SDimitry Andric     Out << "\n";
38460b57cec5SDimitry Andric     PrintLLVMName(Out, BB->getName(), LabelPrefix);
38470b57cec5SDimitry Andric     Out << ':';
38480b57cec5SDimitry Andric   } else if (!IsEntryBlock) {
38490b57cec5SDimitry Andric     Out << "\n";
38500b57cec5SDimitry Andric     int Slot = Machine.getLocalSlot(BB);
38510b57cec5SDimitry Andric     if (Slot != -1)
38520b57cec5SDimitry Andric       Out << Slot << ":";
38530b57cec5SDimitry Andric     else
38540b57cec5SDimitry Andric       Out << "<badref>:";
38550b57cec5SDimitry Andric   }
38560b57cec5SDimitry Andric 
3857480093f4SDimitry Andric   if (!IsEntryBlock) {
38580b57cec5SDimitry Andric     // Output predecessors for the block.
38590b57cec5SDimitry Andric     Out.PadToColumn(50);
38600b57cec5SDimitry Andric     Out << ";";
38610b57cec5SDimitry Andric     const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
38620b57cec5SDimitry Andric 
38630b57cec5SDimitry Andric     if (PI == PE) {
38640b57cec5SDimitry Andric       Out << " No predecessors!";
38650b57cec5SDimitry Andric     } else {
38660b57cec5SDimitry Andric       Out << " preds = ";
38670b57cec5SDimitry Andric       writeOperand(*PI, false);
38680b57cec5SDimitry Andric       for (++PI; PI != PE; ++PI) {
38690b57cec5SDimitry Andric         Out << ", ";
38700b57cec5SDimitry Andric         writeOperand(*PI, false);
38710b57cec5SDimitry Andric       }
38720b57cec5SDimitry Andric     }
38730b57cec5SDimitry Andric   }
38740b57cec5SDimitry Andric 
38750b57cec5SDimitry Andric   Out << "\n";
38760b57cec5SDimitry Andric 
38770b57cec5SDimitry Andric   if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
38780b57cec5SDimitry Andric 
38790b57cec5SDimitry Andric   // Output all of the instructions in the basic block...
38800b57cec5SDimitry Andric   for (const Instruction &I : *BB) {
38810b57cec5SDimitry Andric     printInstructionLine(I);
38820b57cec5SDimitry Andric   }
38830b57cec5SDimitry Andric 
38840b57cec5SDimitry Andric   if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
38850b57cec5SDimitry Andric }
38860b57cec5SDimitry Andric 
38870b57cec5SDimitry Andric /// printInstructionLine - Print an instruction and a newline character.
38880b57cec5SDimitry Andric void AssemblyWriter::printInstructionLine(const Instruction &I) {
38890b57cec5SDimitry Andric   printInstruction(I);
38900b57cec5SDimitry Andric   Out << '\n';
38910b57cec5SDimitry Andric }
38920b57cec5SDimitry Andric 
38930b57cec5SDimitry Andric /// printGCRelocateComment - print comment after call to the gc.relocate
38940b57cec5SDimitry Andric /// intrinsic indicating base and derived pointer names.
38950b57cec5SDimitry Andric void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
38960b57cec5SDimitry Andric   Out << " ; (";
38970b57cec5SDimitry Andric   writeOperand(Relocate.getBasePtr(), false);
38980b57cec5SDimitry Andric   Out << ", ";
38990b57cec5SDimitry Andric   writeOperand(Relocate.getDerivedPtr(), false);
39000b57cec5SDimitry Andric   Out << ")";
39010b57cec5SDimitry Andric }
39020b57cec5SDimitry Andric 
39030b57cec5SDimitry Andric /// printInfoComment - Print a little comment after the instruction indicating
39040b57cec5SDimitry Andric /// which slot it occupies.
39050b57cec5SDimitry Andric void AssemblyWriter::printInfoComment(const Value &V) {
39060b57cec5SDimitry Andric   if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
39070b57cec5SDimitry Andric     printGCRelocateComment(*Relocate);
39080b57cec5SDimitry Andric 
39090b57cec5SDimitry Andric   if (AnnotationWriter)
39100b57cec5SDimitry Andric     AnnotationWriter->printInfoComment(V, Out);
39110b57cec5SDimitry Andric }
39120b57cec5SDimitry Andric 
39130b57cec5SDimitry Andric static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I,
39140b57cec5SDimitry Andric                                     raw_ostream &Out) {
39150b57cec5SDimitry Andric   // We print the address space of the call if it is non-zero.
39160b57cec5SDimitry Andric   unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace();
39170b57cec5SDimitry Andric   bool PrintAddrSpace = CallAddrSpace != 0;
39180b57cec5SDimitry Andric   if (!PrintAddrSpace) {
39190b57cec5SDimitry Andric     const Module *Mod = getModuleFromVal(I);
39200b57cec5SDimitry Andric     // We also print it if it is zero but not equal to the program address space
39210b57cec5SDimitry Andric     // or if we can't find a valid Module* to make it possible to parse
39220b57cec5SDimitry Andric     // the resulting file even without a datalayout string.
39230b57cec5SDimitry Andric     if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0)
39240b57cec5SDimitry Andric       PrintAddrSpace = true;
39250b57cec5SDimitry Andric   }
39260b57cec5SDimitry Andric   if (PrintAddrSpace)
39270b57cec5SDimitry Andric     Out << " addrspace(" << CallAddrSpace << ")";
39280b57cec5SDimitry Andric }
39290b57cec5SDimitry Andric 
39300b57cec5SDimitry Andric // This member is called for each Instruction in a function..
39310b57cec5SDimitry Andric void AssemblyWriter::printInstruction(const Instruction &I) {
39320b57cec5SDimitry Andric   if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
39330b57cec5SDimitry Andric 
39340b57cec5SDimitry Andric   // Print out indentation for an instruction.
39350b57cec5SDimitry Andric   Out << "  ";
39360b57cec5SDimitry Andric 
39370b57cec5SDimitry Andric   // Print out name if it exists...
39380b57cec5SDimitry Andric   if (I.hasName()) {
39390b57cec5SDimitry Andric     PrintLLVMName(Out, &I);
39400b57cec5SDimitry Andric     Out << " = ";
39410b57cec5SDimitry Andric   } else if (!I.getType()->isVoidTy()) {
39420b57cec5SDimitry Andric     // Print out the def slot taken.
39430b57cec5SDimitry Andric     int SlotNum = Machine.getLocalSlot(&I);
39440b57cec5SDimitry Andric     if (SlotNum == -1)
39450b57cec5SDimitry Andric       Out << "<badref> = ";
39460b57cec5SDimitry Andric     else
39470b57cec5SDimitry Andric       Out << '%' << SlotNum << " = ";
39480b57cec5SDimitry Andric   }
39490b57cec5SDimitry Andric 
39500b57cec5SDimitry Andric   if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
39510b57cec5SDimitry Andric     if (CI->isMustTailCall())
39520b57cec5SDimitry Andric       Out << "musttail ";
39530b57cec5SDimitry Andric     else if (CI->isTailCall())
39540b57cec5SDimitry Andric       Out << "tail ";
39550b57cec5SDimitry Andric     else if (CI->isNoTailCall())
39560b57cec5SDimitry Andric       Out << "notail ";
39570b57cec5SDimitry Andric   }
39580b57cec5SDimitry Andric 
39590b57cec5SDimitry Andric   // Print out the opcode...
39600b57cec5SDimitry Andric   Out << I.getOpcodeName();
39610b57cec5SDimitry Andric 
39620b57cec5SDimitry Andric   // If this is an atomic load or store, print out the atomic marker.
39630b57cec5SDimitry Andric   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isAtomic()) ||
39640b57cec5SDimitry Andric       (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
39650b57cec5SDimitry Andric     Out << " atomic";
39660b57cec5SDimitry Andric 
39670b57cec5SDimitry Andric   if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
39680b57cec5SDimitry Andric     Out << " weak";
39690b57cec5SDimitry Andric 
39700b57cec5SDimitry Andric   // If this is a volatile operation, print out the volatile marker.
39710b57cec5SDimitry Andric   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isVolatile()) ||
39720b57cec5SDimitry Andric       (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
39730b57cec5SDimitry Andric       (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
39740b57cec5SDimitry Andric       (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
39750b57cec5SDimitry Andric     Out << " volatile";
39760b57cec5SDimitry Andric 
39770b57cec5SDimitry Andric   // Print out optimization information.
39780b57cec5SDimitry Andric   WriteOptimizationInfo(Out, &I);
39790b57cec5SDimitry Andric 
39800b57cec5SDimitry Andric   // Print out the compare instruction predicates
39810b57cec5SDimitry Andric   if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
39820b57cec5SDimitry Andric     Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
39830b57cec5SDimitry Andric 
39840b57cec5SDimitry Andric   // Print out the atomicrmw operation
39850b57cec5SDimitry Andric   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
39860b57cec5SDimitry Andric     Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation());
39870b57cec5SDimitry Andric 
39880b57cec5SDimitry Andric   // Print out the type of the operands...
39890b57cec5SDimitry Andric   const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
39900b57cec5SDimitry Andric 
39910b57cec5SDimitry Andric   // Special case conditional branches to swizzle the condition out to the front
39920b57cec5SDimitry Andric   if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
39930b57cec5SDimitry Andric     const BranchInst &BI(cast<BranchInst>(I));
39940b57cec5SDimitry Andric     Out << ' ';
39950b57cec5SDimitry Andric     writeOperand(BI.getCondition(), true);
39960b57cec5SDimitry Andric     Out << ", ";
39970b57cec5SDimitry Andric     writeOperand(BI.getSuccessor(0), true);
39980b57cec5SDimitry Andric     Out << ", ";
39990b57cec5SDimitry Andric     writeOperand(BI.getSuccessor(1), true);
40000b57cec5SDimitry Andric 
40010b57cec5SDimitry Andric   } else if (isa<SwitchInst>(I)) {
40020b57cec5SDimitry Andric     const SwitchInst& SI(cast<SwitchInst>(I));
40030b57cec5SDimitry Andric     // Special case switch instruction to get formatting nice and correct.
40040b57cec5SDimitry Andric     Out << ' ';
40050b57cec5SDimitry Andric     writeOperand(SI.getCondition(), true);
40060b57cec5SDimitry Andric     Out << ", ";
40070b57cec5SDimitry Andric     writeOperand(SI.getDefaultDest(), true);
40080b57cec5SDimitry Andric     Out << " [";
40090b57cec5SDimitry Andric     for (auto Case : SI.cases()) {
40100b57cec5SDimitry Andric       Out << "\n    ";
40110b57cec5SDimitry Andric       writeOperand(Case.getCaseValue(), true);
40120b57cec5SDimitry Andric       Out << ", ";
40130b57cec5SDimitry Andric       writeOperand(Case.getCaseSuccessor(), true);
40140b57cec5SDimitry Andric     }
40150b57cec5SDimitry Andric     Out << "\n  ]";
40160b57cec5SDimitry Andric   } else if (isa<IndirectBrInst>(I)) {
40170b57cec5SDimitry Andric     // Special case indirectbr instruction to get formatting nice and correct.
40180b57cec5SDimitry Andric     Out << ' ';
40190b57cec5SDimitry Andric     writeOperand(Operand, true);
40200b57cec5SDimitry Andric     Out << ", [";
40210b57cec5SDimitry Andric 
40220b57cec5SDimitry Andric     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
40230b57cec5SDimitry Andric       if (i != 1)
40240b57cec5SDimitry Andric         Out << ", ";
40250b57cec5SDimitry Andric       writeOperand(I.getOperand(i), true);
40260b57cec5SDimitry Andric     }
40270b57cec5SDimitry Andric     Out << ']';
40280b57cec5SDimitry Andric   } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
40290b57cec5SDimitry Andric     Out << ' ';
40300b57cec5SDimitry Andric     TypePrinter.print(I.getType(), Out);
40310b57cec5SDimitry Andric     Out << ' ';
40320b57cec5SDimitry Andric 
40330b57cec5SDimitry Andric     for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
40340b57cec5SDimitry Andric       if (op) Out << ", ";
40350b57cec5SDimitry Andric       Out << "[ ";
40360b57cec5SDimitry Andric       writeOperand(PN->getIncomingValue(op), false); Out << ", ";
40370b57cec5SDimitry Andric       writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
40380b57cec5SDimitry Andric     }
40390b57cec5SDimitry Andric   } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
40400b57cec5SDimitry Andric     Out << ' ';
40410b57cec5SDimitry Andric     writeOperand(I.getOperand(0), true);
4042fe6060f1SDimitry Andric     for (unsigned i : EVI->indices())
4043fe6060f1SDimitry Andric       Out << ", " << i;
40440b57cec5SDimitry Andric   } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
40450b57cec5SDimitry Andric     Out << ' ';
40460b57cec5SDimitry Andric     writeOperand(I.getOperand(0), true); Out << ", ";
40470b57cec5SDimitry Andric     writeOperand(I.getOperand(1), true);
4048fe6060f1SDimitry Andric     for (unsigned i : IVI->indices())
4049fe6060f1SDimitry Andric       Out << ", " << i;
40500b57cec5SDimitry Andric   } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
40510b57cec5SDimitry Andric     Out << ' ';
40520b57cec5SDimitry Andric     TypePrinter.print(I.getType(), Out);
40530b57cec5SDimitry Andric     if (LPI->isCleanup() || LPI->getNumClauses() != 0)
40540b57cec5SDimitry Andric       Out << '\n';
40550b57cec5SDimitry Andric 
40560b57cec5SDimitry Andric     if (LPI->isCleanup())
40570b57cec5SDimitry Andric       Out << "          cleanup";
40580b57cec5SDimitry Andric 
40590b57cec5SDimitry Andric     for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
40600b57cec5SDimitry Andric       if (i != 0 || LPI->isCleanup()) Out << "\n";
40610b57cec5SDimitry Andric       if (LPI->isCatch(i))
40620b57cec5SDimitry Andric         Out << "          catch ";
40630b57cec5SDimitry Andric       else
40640b57cec5SDimitry Andric         Out << "          filter ";
40650b57cec5SDimitry Andric 
40660b57cec5SDimitry Andric       writeOperand(LPI->getClause(i), true);
40670b57cec5SDimitry Andric     }
40680b57cec5SDimitry Andric   } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
40690b57cec5SDimitry Andric     Out << " within ";
40700b57cec5SDimitry Andric     writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
40710b57cec5SDimitry Andric     Out << " [";
40720b57cec5SDimitry Andric     unsigned Op = 0;
40730b57cec5SDimitry Andric     for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
40740b57cec5SDimitry Andric       if (Op > 0)
40750b57cec5SDimitry Andric         Out << ", ";
40760b57cec5SDimitry Andric       writeOperand(PadBB, /*PrintType=*/true);
40770b57cec5SDimitry Andric       ++Op;
40780b57cec5SDimitry Andric     }
40790b57cec5SDimitry Andric     Out << "] unwind ";
40800b57cec5SDimitry Andric     if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
40810b57cec5SDimitry Andric       writeOperand(UnwindDest, /*PrintType=*/true);
40820b57cec5SDimitry Andric     else
40830b57cec5SDimitry Andric       Out << "to caller";
40840b57cec5SDimitry Andric   } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
40850b57cec5SDimitry Andric     Out << " within ";
40860b57cec5SDimitry Andric     writeOperand(FPI->getParentPad(), /*PrintType=*/false);
40870b57cec5SDimitry Andric     Out << " [";
40880b57cec5SDimitry Andric     for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
40890b57cec5SDimitry Andric          ++Op) {
40900b57cec5SDimitry Andric       if (Op > 0)
40910b57cec5SDimitry Andric         Out << ", ";
40920b57cec5SDimitry Andric       writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
40930b57cec5SDimitry Andric     }
40940b57cec5SDimitry Andric     Out << ']';
40950b57cec5SDimitry Andric   } else if (isa<ReturnInst>(I) && !Operand) {
40960b57cec5SDimitry Andric     Out << " void";
40970b57cec5SDimitry Andric   } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
40980b57cec5SDimitry Andric     Out << " from ";
40990b57cec5SDimitry Andric     writeOperand(CRI->getOperand(0), /*PrintType=*/false);
41000b57cec5SDimitry Andric 
41010b57cec5SDimitry Andric     Out << " to ";
41020b57cec5SDimitry Andric     writeOperand(CRI->getOperand(1), /*PrintType=*/true);
41030b57cec5SDimitry Andric   } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
41040b57cec5SDimitry Andric     Out << " from ";
41050b57cec5SDimitry Andric     writeOperand(CRI->getOperand(0), /*PrintType=*/false);
41060b57cec5SDimitry Andric 
41070b57cec5SDimitry Andric     Out << " unwind ";
41080b57cec5SDimitry Andric     if (CRI->hasUnwindDest())
41090b57cec5SDimitry Andric       writeOperand(CRI->getOperand(1), /*PrintType=*/true);
41100b57cec5SDimitry Andric     else
41110b57cec5SDimitry Andric       Out << "to caller";
41120b57cec5SDimitry Andric   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
41130b57cec5SDimitry Andric     // Print the calling convention being used.
41140b57cec5SDimitry Andric     if (CI->getCallingConv() != CallingConv::C) {
41150b57cec5SDimitry Andric       Out << " ";
41160b57cec5SDimitry Andric       PrintCallingConv(CI->getCallingConv(), Out);
41170b57cec5SDimitry Andric     }
41180b57cec5SDimitry Andric 
41195ffd83dbSDimitry Andric     Operand = CI->getCalledOperand();
41200b57cec5SDimitry Andric     FunctionType *FTy = CI->getFunctionType();
41210b57cec5SDimitry Andric     Type *RetTy = FTy->getReturnType();
41220b57cec5SDimitry Andric     const AttributeList &PAL = CI->getAttributes();
41230b57cec5SDimitry Andric 
4124*349cc55cSDimitry Andric     if (PAL.hasRetAttrs())
41250b57cec5SDimitry Andric       Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
41260b57cec5SDimitry Andric 
41270b57cec5SDimitry Andric     // Only print addrspace(N) if necessary:
41280b57cec5SDimitry Andric     maybePrintCallAddrSpace(Operand, &I, Out);
41290b57cec5SDimitry Andric 
41300b57cec5SDimitry Andric     // If possible, print out the short form of the call instruction.  We can
41310b57cec5SDimitry Andric     // only do this if the first argument is a pointer to a nonvararg function,
41320b57cec5SDimitry Andric     // and if the return type is not a pointer to a function.
41330b57cec5SDimitry Andric     //
41340b57cec5SDimitry Andric     Out << ' ';
41350b57cec5SDimitry Andric     TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
41360b57cec5SDimitry Andric     Out << ' ';
41370b57cec5SDimitry Andric     writeOperand(Operand, false);
41380b57cec5SDimitry Andric     Out << '(';
4139*349cc55cSDimitry Andric     for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) {
41400b57cec5SDimitry Andric       if (op > 0)
41410b57cec5SDimitry Andric         Out << ", ";
4142*349cc55cSDimitry Andric       writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op));
41430b57cec5SDimitry Andric     }
41440b57cec5SDimitry Andric 
41450b57cec5SDimitry Andric     // Emit an ellipsis if this is a musttail call in a vararg function.  This
41460b57cec5SDimitry Andric     // is only to aid readability, musttail calls forward varargs by default.
41470b57cec5SDimitry Andric     if (CI->isMustTailCall() && CI->getParent() &&
41480b57cec5SDimitry Andric         CI->getParent()->getParent() &&
41490b57cec5SDimitry Andric         CI->getParent()->getParent()->isVarArg())
41500b57cec5SDimitry Andric       Out << ", ...";
41510b57cec5SDimitry Andric 
41520b57cec5SDimitry Andric     Out << ')';
4153*349cc55cSDimitry Andric     if (PAL.hasFnAttrs())
4154*349cc55cSDimitry Andric       Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
41550b57cec5SDimitry Andric 
41560b57cec5SDimitry Andric     writeOperandBundles(CI);
41570b57cec5SDimitry Andric   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
41585ffd83dbSDimitry Andric     Operand = II->getCalledOperand();
41590b57cec5SDimitry Andric     FunctionType *FTy = II->getFunctionType();
41600b57cec5SDimitry Andric     Type *RetTy = FTy->getReturnType();
41610b57cec5SDimitry Andric     const AttributeList &PAL = II->getAttributes();
41620b57cec5SDimitry Andric 
41630b57cec5SDimitry Andric     // Print the calling convention being used.
41640b57cec5SDimitry Andric     if (II->getCallingConv() != CallingConv::C) {
41650b57cec5SDimitry Andric       Out << " ";
41660b57cec5SDimitry Andric       PrintCallingConv(II->getCallingConv(), Out);
41670b57cec5SDimitry Andric     }
41680b57cec5SDimitry Andric 
4169*349cc55cSDimitry Andric     if (PAL.hasRetAttrs())
41700b57cec5SDimitry Andric       Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
41710b57cec5SDimitry Andric 
41720b57cec5SDimitry Andric     // Only print addrspace(N) if necessary:
41730b57cec5SDimitry Andric     maybePrintCallAddrSpace(Operand, &I, Out);
41740b57cec5SDimitry Andric 
41750b57cec5SDimitry Andric     // If possible, print out the short form of the invoke instruction. We can
41760b57cec5SDimitry Andric     // only do this if the first argument is a pointer to a nonvararg function,
41770b57cec5SDimitry Andric     // and if the return type is not a pointer to a function.
41780b57cec5SDimitry Andric     //
41790b57cec5SDimitry Andric     Out << ' ';
41800b57cec5SDimitry Andric     TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
41810b57cec5SDimitry Andric     Out << ' ';
41820b57cec5SDimitry Andric     writeOperand(Operand, false);
41830b57cec5SDimitry Andric     Out << '(';
4184*349cc55cSDimitry Andric     for (unsigned op = 0, Eop = II->arg_size(); op < Eop; ++op) {
41850b57cec5SDimitry Andric       if (op)
41860b57cec5SDimitry Andric         Out << ", ";
4187*349cc55cSDimitry Andric       writeParamOperand(II->getArgOperand(op), PAL.getParamAttrs(op));
41880b57cec5SDimitry Andric     }
41890b57cec5SDimitry Andric 
41900b57cec5SDimitry Andric     Out << ')';
4191*349cc55cSDimitry Andric     if (PAL.hasFnAttrs())
4192*349cc55cSDimitry Andric       Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
41930b57cec5SDimitry Andric 
41940b57cec5SDimitry Andric     writeOperandBundles(II);
41950b57cec5SDimitry Andric 
41960b57cec5SDimitry Andric     Out << "\n          to ";
41970b57cec5SDimitry Andric     writeOperand(II->getNormalDest(), true);
41980b57cec5SDimitry Andric     Out << " unwind ";
41990b57cec5SDimitry Andric     writeOperand(II->getUnwindDest(), true);
42000b57cec5SDimitry Andric   } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) {
42015ffd83dbSDimitry Andric     Operand = CBI->getCalledOperand();
42020b57cec5SDimitry Andric     FunctionType *FTy = CBI->getFunctionType();
42030b57cec5SDimitry Andric     Type *RetTy = FTy->getReturnType();
42040b57cec5SDimitry Andric     const AttributeList &PAL = CBI->getAttributes();
42050b57cec5SDimitry Andric 
42060b57cec5SDimitry Andric     // Print the calling convention being used.
42070b57cec5SDimitry Andric     if (CBI->getCallingConv() != CallingConv::C) {
42080b57cec5SDimitry Andric       Out << " ";
42090b57cec5SDimitry Andric       PrintCallingConv(CBI->getCallingConv(), Out);
42100b57cec5SDimitry Andric     }
42110b57cec5SDimitry Andric 
4212*349cc55cSDimitry Andric     if (PAL.hasRetAttrs())
42130b57cec5SDimitry Andric       Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
42140b57cec5SDimitry Andric 
42150b57cec5SDimitry Andric     // If possible, print out the short form of the callbr instruction. We can
42160b57cec5SDimitry Andric     // only do this if the first argument is a pointer to a nonvararg function,
42170b57cec5SDimitry Andric     // and if the return type is not a pointer to a function.
42180b57cec5SDimitry Andric     //
42190b57cec5SDimitry Andric     Out << ' ';
42200b57cec5SDimitry Andric     TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
42210b57cec5SDimitry Andric     Out << ' ';
42220b57cec5SDimitry Andric     writeOperand(Operand, false);
42230b57cec5SDimitry Andric     Out << '(';
4224*349cc55cSDimitry Andric     for (unsigned op = 0, Eop = CBI->arg_size(); op < Eop; ++op) {
42250b57cec5SDimitry Andric       if (op)
42260b57cec5SDimitry Andric         Out << ", ";
4227*349cc55cSDimitry Andric       writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttrs(op));
42280b57cec5SDimitry Andric     }
42290b57cec5SDimitry Andric 
42300b57cec5SDimitry Andric     Out << ')';
4231*349cc55cSDimitry Andric     if (PAL.hasFnAttrs())
4232*349cc55cSDimitry Andric       Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs());
42330b57cec5SDimitry Andric 
42340b57cec5SDimitry Andric     writeOperandBundles(CBI);
42350b57cec5SDimitry Andric 
42360b57cec5SDimitry Andric     Out << "\n          to ";
42370b57cec5SDimitry Andric     writeOperand(CBI->getDefaultDest(), true);
42380b57cec5SDimitry Andric     Out << " [";
42390b57cec5SDimitry Andric     for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) {
42400b57cec5SDimitry Andric       if (i != 0)
42410b57cec5SDimitry Andric         Out << ", ";
42420b57cec5SDimitry Andric       writeOperand(CBI->getIndirectDest(i), true);
42430b57cec5SDimitry Andric     }
42440b57cec5SDimitry Andric     Out << ']';
42450b57cec5SDimitry Andric   } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
42460b57cec5SDimitry Andric     Out << ' ';
42470b57cec5SDimitry Andric     if (AI->isUsedWithInAlloca())
42480b57cec5SDimitry Andric       Out << "inalloca ";
42490b57cec5SDimitry Andric     if (AI->isSwiftError())
42500b57cec5SDimitry Andric       Out << "swifterror ";
42510b57cec5SDimitry Andric     TypePrinter.print(AI->getAllocatedType(), Out);
42520b57cec5SDimitry Andric 
42530b57cec5SDimitry Andric     // Explicitly write the array size if the code is broken, if it's an array
42540b57cec5SDimitry Andric     // allocation, or if the type is not canonical for scalar allocations.  The
42550b57cec5SDimitry Andric     // latter case prevents the type from mutating when round-tripping through
42560b57cec5SDimitry Andric     // assembly.
42570b57cec5SDimitry Andric     if (!AI->getArraySize() || AI->isArrayAllocation() ||
42580b57cec5SDimitry Andric         !AI->getArraySize()->getType()->isIntegerTy(32)) {
42590b57cec5SDimitry Andric       Out << ", ";
42600b57cec5SDimitry Andric       writeOperand(AI->getArraySize(), true);
42610b57cec5SDimitry Andric     }
42620b57cec5SDimitry Andric     if (AI->getAlignment()) {
42630b57cec5SDimitry Andric       Out << ", align " << AI->getAlignment();
42640b57cec5SDimitry Andric     }
42650b57cec5SDimitry Andric 
42660b57cec5SDimitry Andric     unsigned AddrSpace = AI->getType()->getAddressSpace();
42670b57cec5SDimitry Andric     if (AddrSpace != 0) {
42680b57cec5SDimitry Andric       Out << ", addrspace(" << AddrSpace << ')';
42690b57cec5SDimitry Andric     }
42700b57cec5SDimitry Andric   } else if (isa<CastInst>(I)) {
42710b57cec5SDimitry Andric     if (Operand) {
42720b57cec5SDimitry Andric       Out << ' ';
42730b57cec5SDimitry Andric       writeOperand(Operand, true);   // Work with broken code
42740b57cec5SDimitry Andric     }
42750b57cec5SDimitry Andric     Out << " to ";
42760b57cec5SDimitry Andric     TypePrinter.print(I.getType(), Out);
42770b57cec5SDimitry Andric   } else if (isa<VAArgInst>(I)) {
42780b57cec5SDimitry Andric     if (Operand) {
42790b57cec5SDimitry Andric       Out << ' ';
42800b57cec5SDimitry Andric       writeOperand(Operand, true);   // Work with broken code
42810b57cec5SDimitry Andric     }
42820b57cec5SDimitry Andric     Out << ", ";
42830b57cec5SDimitry Andric     TypePrinter.print(I.getType(), Out);
42840b57cec5SDimitry Andric   } else if (Operand) {   // Print the normal way.
42850b57cec5SDimitry Andric     if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
42860b57cec5SDimitry Andric       Out << ' ';
42870b57cec5SDimitry Andric       TypePrinter.print(GEP->getSourceElementType(), Out);
42880b57cec5SDimitry Andric       Out << ',';
42890b57cec5SDimitry Andric     } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
42900b57cec5SDimitry Andric       Out << ' ';
42910b57cec5SDimitry Andric       TypePrinter.print(LI->getType(), Out);
42920b57cec5SDimitry Andric       Out << ',';
42930b57cec5SDimitry Andric     }
42940b57cec5SDimitry Andric 
42950b57cec5SDimitry Andric     // PrintAllTypes - Instructions who have operands of all the same type
42960b57cec5SDimitry Andric     // omit the type from all but the first operand.  If the instruction has
42970b57cec5SDimitry Andric     // different type operands (for example br), then they are all printed.
42980b57cec5SDimitry Andric     bool PrintAllTypes = false;
42990b57cec5SDimitry Andric     Type *TheType = Operand->getType();
43000b57cec5SDimitry Andric 
43010b57cec5SDimitry Andric     // Select, Store and ShuffleVector always print all types.
43020b57cec5SDimitry Andric     if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
43030b57cec5SDimitry Andric         || isa<ReturnInst>(I)) {
43040b57cec5SDimitry Andric       PrintAllTypes = true;
43050b57cec5SDimitry Andric     } else {
43060b57cec5SDimitry Andric       for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
43070b57cec5SDimitry Andric         Operand = I.getOperand(i);
43080b57cec5SDimitry Andric         // note that Operand shouldn't be null, but the test helps make dump()
43090b57cec5SDimitry Andric         // more tolerant of malformed IR
43100b57cec5SDimitry Andric         if (Operand && Operand->getType() != TheType) {
43110b57cec5SDimitry Andric           PrintAllTypes = true;    // We have differing types!  Print them all!
43120b57cec5SDimitry Andric           break;
43130b57cec5SDimitry Andric         }
43140b57cec5SDimitry Andric       }
43150b57cec5SDimitry Andric     }
43160b57cec5SDimitry Andric 
43170b57cec5SDimitry Andric     if (!PrintAllTypes) {
43180b57cec5SDimitry Andric       Out << ' ';
43190b57cec5SDimitry Andric       TypePrinter.print(TheType, Out);
43200b57cec5SDimitry Andric     }
43210b57cec5SDimitry Andric 
43220b57cec5SDimitry Andric     Out << ' ';
43230b57cec5SDimitry Andric     for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
43240b57cec5SDimitry Andric       if (i) Out << ", ";
43250b57cec5SDimitry Andric       writeOperand(I.getOperand(i), PrintAllTypes);
43260b57cec5SDimitry Andric     }
43270b57cec5SDimitry Andric   }
43280b57cec5SDimitry Andric 
43290b57cec5SDimitry Andric   // Print atomic ordering/alignment for memory operations
43300b57cec5SDimitry Andric   if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
43310b57cec5SDimitry Andric     if (LI->isAtomic())
43320b57cec5SDimitry Andric       writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
43330b57cec5SDimitry Andric     if (LI->getAlignment())
43340b57cec5SDimitry Andric       Out << ", align " << LI->getAlignment();
43350b57cec5SDimitry Andric   } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
43360b57cec5SDimitry Andric     if (SI->isAtomic())
43370b57cec5SDimitry Andric       writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
43380b57cec5SDimitry Andric     if (SI->getAlignment())
43390b57cec5SDimitry Andric       Out << ", align " << SI->getAlignment();
43400b57cec5SDimitry Andric   } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
43410b57cec5SDimitry Andric     writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
43420b57cec5SDimitry Andric                        CXI->getFailureOrdering(), CXI->getSyncScopeID());
4343fe6060f1SDimitry Andric     Out << ", align " << CXI->getAlign().value();
43440b57cec5SDimitry Andric   } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
43450b57cec5SDimitry Andric     writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
43460b57cec5SDimitry Andric                 RMWI->getSyncScopeID());
4347fe6060f1SDimitry Andric     Out << ", align " << RMWI->getAlign().value();
43480b57cec5SDimitry Andric   } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
43490b57cec5SDimitry Andric     writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
43505ffd83dbSDimitry Andric   } else if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(&I)) {
43515ffd83dbSDimitry Andric     PrintShuffleMask(Out, SVI->getType(), SVI->getShuffleMask());
43520b57cec5SDimitry Andric   }
43530b57cec5SDimitry Andric 
43540b57cec5SDimitry Andric   // Print Metadata info.
43550b57cec5SDimitry Andric   SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
43560b57cec5SDimitry Andric   I.getAllMetadata(InstMD);
43570b57cec5SDimitry Andric   printMetadataAttachments(InstMD, ", ");
43580b57cec5SDimitry Andric 
43590b57cec5SDimitry Andric   // Print a nice comment.
43600b57cec5SDimitry Andric   printInfoComment(I);
43610b57cec5SDimitry Andric }
43620b57cec5SDimitry Andric 
43630b57cec5SDimitry Andric void AssemblyWriter::printMetadataAttachments(
43640b57cec5SDimitry Andric     const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
43650b57cec5SDimitry Andric     StringRef Separator) {
43660b57cec5SDimitry Andric   if (MDs.empty())
43670b57cec5SDimitry Andric     return;
43680b57cec5SDimitry Andric 
43690b57cec5SDimitry Andric   if (MDNames.empty())
43700b57cec5SDimitry Andric     MDs[0].second->getContext().getMDKindNames(MDNames);
43710b57cec5SDimitry Andric 
4372*349cc55cSDimitry Andric   auto WriterCtx = getContext();
43730b57cec5SDimitry Andric   for (const auto &I : MDs) {
43740b57cec5SDimitry Andric     unsigned Kind = I.first;
43750b57cec5SDimitry Andric     Out << Separator;
43760b57cec5SDimitry Andric     if (Kind < MDNames.size()) {
43770b57cec5SDimitry Andric       Out << "!";
43780b57cec5SDimitry Andric       printMetadataIdentifier(MDNames[Kind], Out);
43790b57cec5SDimitry Andric     } else
43800b57cec5SDimitry Andric       Out << "!<unknown kind #" << Kind << ">";
43810b57cec5SDimitry Andric     Out << ' ';
4382*349cc55cSDimitry Andric     WriteAsOperandInternal(Out, I.second, WriterCtx);
43830b57cec5SDimitry Andric   }
43840b57cec5SDimitry Andric }
43850b57cec5SDimitry Andric 
43860b57cec5SDimitry Andric void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
43870b57cec5SDimitry Andric   Out << '!' << Slot << " = ";
43880b57cec5SDimitry Andric   printMDNodeBody(Node);
43890b57cec5SDimitry Andric   Out << "\n";
43900b57cec5SDimitry Andric }
43910b57cec5SDimitry Andric 
43920b57cec5SDimitry Andric void AssemblyWriter::writeAllMDNodes() {
43930b57cec5SDimitry Andric   SmallVector<const MDNode *, 16> Nodes;
43940b57cec5SDimitry Andric   Nodes.resize(Machine.mdn_size());
4395fe6060f1SDimitry Andric   for (auto &I : llvm::make_range(Machine.mdn_begin(), Machine.mdn_end()))
4396fe6060f1SDimitry Andric     Nodes[I.second] = cast<MDNode>(I.first);
43970b57cec5SDimitry Andric 
43980b57cec5SDimitry Andric   for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
43990b57cec5SDimitry Andric     writeMDNode(i, Nodes[i]);
44000b57cec5SDimitry Andric   }
44010b57cec5SDimitry Andric }
44020b57cec5SDimitry Andric 
44030b57cec5SDimitry Andric void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
4404*349cc55cSDimitry Andric   auto WriterCtx = getContext();
4405*349cc55cSDimitry Andric   WriteMDNodeBodyInternal(Out, Node, WriterCtx);
44060b57cec5SDimitry Andric }
44070b57cec5SDimitry Andric 
4408480093f4SDimitry Andric void AssemblyWriter::writeAttribute(const Attribute &Attr, bool InAttrGroup) {
4409480093f4SDimitry Andric   if (!Attr.isTypeAttribute()) {
4410480093f4SDimitry Andric     Out << Attr.getAsString(InAttrGroup);
4411480093f4SDimitry Andric     return;
4412480093f4SDimitry Andric   }
4413480093f4SDimitry Andric 
4414fe6060f1SDimitry Andric   Out << Attribute::getNameFromAttrKind(Attr.getKindAsEnum());
4415480093f4SDimitry Andric   if (Type *Ty = Attr.getValueAsType()) {
4416480093f4SDimitry Andric     Out << '(';
4417480093f4SDimitry Andric     TypePrinter.print(Ty, Out);
4418480093f4SDimitry Andric     Out << ')';
4419480093f4SDimitry Andric   }
4420480093f4SDimitry Andric }
4421480093f4SDimitry Andric 
4422480093f4SDimitry Andric void AssemblyWriter::writeAttributeSet(const AttributeSet &AttrSet,
4423480093f4SDimitry Andric                                        bool InAttrGroup) {
4424480093f4SDimitry Andric   bool FirstAttr = true;
4425480093f4SDimitry Andric   for (const auto &Attr : AttrSet) {
4426480093f4SDimitry Andric     if (!FirstAttr)
4427480093f4SDimitry Andric       Out << ' ';
4428480093f4SDimitry Andric     writeAttribute(Attr, InAttrGroup);
4429480093f4SDimitry Andric     FirstAttr = false;
4430480093f4SDimitry Andric   }
4431480093f4SDimitry Andric }
4432480093f4SDimitry Andric 
44330b57cec5SDimitry Andric void AssemblyWriter::writeAllAttributeGroups() {
44340b57cec5SDimitry Andric   std::vector<std::pair<AttributeSet, unsigned>> asVec;
44350b57cec5SDimitry Andric   asVec.resize(Machine.as_size());
44360b57cec5SDimitry Andric 
4437fe6060f1SDimitry Andric   for (auto &I : llvm::make_range(Machine.as_begin(), Machine.as_end()))
4438fe6060f1SDimitry Andric     asVec[I.second] = I;
44390b57cec5SDimitry Andric 
44400b57cec5SDimitry Andric   for (const auto &I : asVec)
44410b57cec5SDimitry Andric     Out << "attributes #" << I.second << " = { "
44420b57cec5SDimitry Andric         << I.first.getAsString(true) << " }\n";
44430b57cec5SDimitry Andric }
44440b57cec5SDimitry Andric 
4445fe6060f1SDimitry Andric void AssemblyWriter::printUseListOrder(const Value *V,
4446fe6060f1SDimitry Andric                                        const std::vector<unsigned> &Shuffle) {
44470b57cec5SDimitry Andric   bool IsInFunction = Machine.getFunction();
44480b57cec5SDimitry Andric   if (IsInFunction)
44490b57cec5SDimitry Andric     Out << "  ";
44500b57cec5SDimitry Andric 
44510b57cec5SDimitry Andric   Out << "uselistorder";
4452fe6060f1SDimitry Andric   if (const BasicBlock *BB = IsInFunction ? nullptr : dyn_cast<BasicBlock>(V)) {
44530b57cec5SDimitry Andric     Out << "_bb ";
44540b57cec5SDimitry Andric     writeOperand(BB->getParent(), false);
44550b57cec5SDimitry Andric     Out << ", ";
44560b57cec5SDimitry Andric     writeOperand(BB, false);
44570b57cec5SDimitry Andric   } else {
44580b57cec5SDimitry Andric     Out << " ";
4459fe6060f1SDimitry Andric     writeOperand(V, true);
44600b57cec5SDimitry Andric   }
44610b57cec5SDimitry Andric   Out << ", { ";
44620b57cec5SDimitry Andric 
4463fe6060f1SDimitry Andric   assert(Shuffle.size() >= 2 && "Shuffle too small");
4464fe6060f1SDimitry Andric   Out << Shuffle[0];
4465fe6060f1SDimitry Andric   for (unsigned I = 1, E = Shuffle.size(); I != E; ++I)
4466fe6060f1SDimitry Andric     Out << ", " << Shuffle[I];
44670b57cec5SDimitry Andric   Out << " }\n";
44680b57cec5SDimitry Andric }
44690b57cec5SDimitry Andric 
44700b57cec5SDimitry Andric void AssemblyWriter::printUseLists(const Function *F) {
4471fe6060f1SDimitry Andric   auto It = UseListOrders.find(F);
4472fe6060f1SDimitry Andric   if (It == UseListOrders.end())
44730b57cec5SDimitry Andric     return;
44740b57cec5SDimitry Andric 
44750b57cec5SDimitry Andric   Out << "\n; uselistorder directives\n";
4476fe6060f1SDimitry Andric   for (const auto &Pair : It->second)
4477fe6060f1SDimitry Andric     printUseListOrder(Pair.first, Pair.second);
44780b57cec5SDimitry Andric }
44790b57cec5SDimitry Andric 
44800b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
44810b57cec5SDimitry Andric //                       External Interface declarations
44820b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
44830b57cec5SDimitry Andric 
44840b57cec5SDimitry Andric void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
44850b57cec5SDimitry Andric                      bool ShouldPreserveUseListOrder,
44860b57cec5SDimitry Andric                      bool IsForDebug) const {
44870b57cec5SDimitry Andric   SlotTracker SlotTable(this->getParent());
44880b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
44890b57cec5SDimitry Andric   AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
44900b57cec5SDimitry Andric                    IsForDebug,
44910b57cec5SDimitry Andric                    ShouldPreserveUseListOrder);
44920b57cec5SDimitry Andric   W.printFunction(this);
44930b57cec5SDimitry Andric }
44940b57cec5SDimitry Andric 
44955ffd83dbSDimitry Andric void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
44965ffd83dbSDimitry Andric                      bool ShouldPreserveUseListOrder,
44975ffd83dbSDimitry Andric                      bool IsForDebug) const {
4498e8d8bef9SDimitry Andric   SlotTracker SlotTable(this->getParent());
44995ffd83dbSDimitry Andric   formatted_raw_ostream OS(ROS);
45005ffd83dbSDimitry Andric   AssemblyWriter W(OS, SlotTable, this->getModule(), AAW,
45015ffd83dbSDimitry Andric                    IsForDebug,
45025ffd83dbSDimitry Andric                    ShouldPreserveUseListOrder);
45035ffd83dbSDimitry Andric   W.printBasicBlock(this);
45045ffd83dbSDimitry Andric }
45055ffd83dbSDimitry Andric 
45060b57cec5SDimitry Andric void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
45070b57cec5SDimitry Andric                    bool ShouldPreserveUseListOrder, bool IsForDebug) const {
45080b57cec5SDimitry Andric   SlotTracker SlotTable(this);
45090b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
45100b57cec5SDimitry Andric   AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
45110b57cec5SDimitry Andric                    ShouldPreserveUseListOrder);
45120b57cec5SDimitry Andric   W.printModule(this);
45130b57cec5SDimitry Andric }
45140b57cec5SDimitry Andric 
45150b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
45160b57cec5SDimitry Andric   SlotTracker SlotTable(getParent());
45170b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
45180b57cec5SDimitry Andric   AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
45190b57cec5SDimitry Andric   W.printNamedMDNode(this);
45200b57cec5SDimitry Andric }
45210b57cec5SDimitry Andric 
45220b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
45230b57cec5SDimitry Andric                         bool IsForDebug) const {
45240b57cec5SDimitry Andric   Optional<SlotTracker> LocalST;
45250b57cec5SDimitry Andric   SlotTracker *SlotTable;
45260b57cec5SDimitry Andric   if (auto *ST = MST.getMachine())
45270b57cec5SDimitry Andric     SlotTable = ST;
45280b57cec5SDimitry Andric   else {
45290b57cec5SDimitry Andric     LocalST.emplace(getParent());
45300b57cec5SDimitry Andric     SlotTable = &*LocalST;
45310b57cec5SDimitry Andric   }
45320b57cec5SDimitry Andric 
45330b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
45340b57cec5SDimitry Andric   AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
45350b57cec5SDimitry Andric   W.printNamedMDNode(this);
45360b57cec5SDimitry Andric }
45370b57cec5SDimitry Andric 
45380b57cec5SDimitry Andric void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
45390b57cec5SDimitry Andric   PrintLLVMName(ROS, getName(), ComdatPrefix);
45400b57cec5SDimitry Andric   ROS << " = comdat ";
45410b57cec5SDimitry Andric 
45420b57cec5SDimitry Andric   switch (getSelectionKind()) {
45430b57cec5SDimitry Andric   case Comdat::Any:
45440b57cec5SDimitry Andric     ROS << "any";
45450b57cec5SDimitry Andric     break;
45460b57cec5SDimitry Andric   case Comdat::ExactMatch:
45470b57cec5SDimitry Andric     ROS << "exactmatch";
45480b57cec5SDimitry Andric     break;
45490b57cec5SDimitry Andric   case Comdat::Largest:
45500b57cec5SDimitry Andric     ROS << "largest";
45510b57cec5SDimitry Andric     break;
4552fe6060f1SDimitry Andric   case Comdat::NoDeduplicate:
4553fe6060f1SDimitry Andric     ROS << "nodeduplicate";
45540b57cec5SDimitry Andric     break;
45550b57cec5SDimitry Andric   case Comdat::SameSize:
45560b57cec5SDimitry Andric     ROS << "samesize";
45570b57cec5SDimitry Andric     break;
45580b57cec5SDimitry Andric   }
45590b57cec5SDimitry Andric 
45600b57cec5SDimitry Andric   ROS << '\n';
45610b57cec5SDimitry Andric }
45620b57cec5SDimitry Andric 
45630b57cec5SDimitry Andric void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
45640b57cec5SDimitry Andric   TypePrinting TP;
45650b57cec5SDimitry Andric   TP.print(const_cast<Type*>(this), OS);
45660b57cec5SDimitry Andric 
45670b57cec5SDimitry Andric   if (NoDetails)
45680b57cec5SDimitry Andric     return;
45690b57cec5SDimitry Andric 
45700b57cec5SDimitry Andric   // If the type is a named struct type, print the body as well.
45710b57cec5SDimitry Andric   if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
45720b57cec5SDimitry Andric     if (!STy->isLiteral()) {
45730b57cec5SDimitry Andric       OS << " = type ";
45740b57cec5SDimitry Andric       TP.printStructBody(STy, OS);
45750b57cec5SDimitry Andric     }
45760b57cec5SDimitry Andric }
45770b57cec5SDimitry Andric 
45780b57cec5SDimitry Andric static bool isReferencingMDNode(const Instruction &I) {
45790b57cec5SDimitry Andric   if (const auto *CI = dyn_cast<CallInst>(&I))
45800b57cec5SDimitry Andric     if (Function *F = CI->getCalledFunction())
45810b57cec5SDimitry Andric       if (F->isIntrinsic())
45820b57cec5SDimitry Andric         for (auto &Op : I.operands())
45830b57cec5SDimitry Andric           if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
45840b57cec5SDimitry Andric             if (isa<MDNode>(V->getMetadata()))
45850b57cec5SDimitry Andric               return true;
45860b57cec5SDimitry Andric   return false;
45870b57cec5SDimitry Andric }
45880b57cec5SDimitry Andric 
45890b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, bool IsForDebug) const {
45900b57cec5SDimitry Andric   bool ShouldInitializeAllMetadata = false;
45910b57cec5SDimitry Andric   if (auto *I = dyn_cast<Instruction>(this))
45920b57cec5SDimitry Andric     ShouldInitializeAllMetadata = isReferencingMDNode(*I);
45930b57cec5SDimitry Andric   else if (isa<Function>(this) || isa<MetadataAsValue>(this))
45940b57cec5SDimitry Andric     ShouldInitializeAllMetadata = true;
45950b57cec5SDimitry Andric 
45960b57cec5SDimitry Andric   ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
45970b57cec5SDimitry Andric   print(ROS, MST, IsForDebug);
45980b57cec5SDimitry Andric }
45990b57cec5SDimitry Andric 
46000b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
46010b57cec5SDimitry Andric                   bool IsForDebug) const {
46020b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
46030b57cec5SDimitry Andric   SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
46040b57cec5SDimitry Andric   SlotTracker &SlotTable =
46050b57cec5SDimitry Andric       MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
46060b57cec5SDimitry Andric   auto incorporateFunction = [&](const Function *F) {
46070b57cec5SDimitry Andric     if (F)
46080b57cec5SDimitry Andric       MST.incorporateFunction(*F);
46090b57cec5SDimitry Andric   };
46100b57cec5SDimitry Andric 
46110b57cec5SDimitry Andric   if (const Instruction *I = dyn_cast<Instruction>(this)) {
46120b57cec5SDimitry Andric     incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
46130b57cec5SDimitry Andric     AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
46140b57cec5SDimitry Andric     W.printInstruction(*I);
46150b57cec5SDimitry Andric   } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
46160b57cec5SDimitry Andric     incorporateFunction(BB->getParent());
46170b57cec5SDimitry Andric     AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
46180b57cec5SDimitry Andric     W.printBasicBlock(BB);
46190b57cec5SDimitry Andric   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
46200b57cec5SDimitry Andric     AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
46210b57cec5SDimitry Andric     if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
46220b57cec5SDimitry Andric       W.printGlobal(V);
46230b57cec5SDimitry Andric     else if (const Function *F = dyn_cast<Function>(GV))
46240b57cec5SDimitry Andric       W.printFunction(F);
4625*349cc55cSDimitry Andric     else if (const GlobalAlias *A = dyn_cast<GlobalAlias>(GV))
4626*349cc55cSDimitry Andric       W.printAlias(A);
4627*349cc55cSDimitry Andric     else if (const GlobalIFunc *I = dyn_cast<GlobalIFunc>(GV))
4628*349cc55cSDimitry Andric       W.printIFunc(I);
46290b57cec5SDimitry Andric     else
4630*349cc55cSDimitry Andric       llvm_unreachable("Unknown GlobalValue to print out!");
46310b57cec5SDimitry Andric   } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
46320b57cec5SDimitry Andric     V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
46330b57cec5SDimitry Andric   } else if (const Constant *C = dyn_cast<Constant>(this)) {
46340b57cec5SDimitry Andric     TypePrinting TypePrinter;
46350b57cec5SDimitry Andric     TypePrinter.print(C->getType(), OS);
46360b57cec5SDimitry Andric     OS << ' ';
4637*349cc55cSDimitry Andric     AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine());
4638*349cc55cSDimitry Andric     WriteConstantInternal(OS, C, WriterCtx);
46390b57cec5SDimitry Andric   } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
46400b57cec5SDimitry Andric     this->printAsOperand(OS, /* PrintType */ true, MST);
46410b57cec5SDimitry Andric   } else {
46420b57cec5SDimitry Andric     llvm_unreachable("Unknown value to print out!");
46430b57cec5SDimitry Andric   }
46440b57cec5SDimitry Andric }
46450b57cec5SDimitry Andric 
46460b57cec5SDimitry Andric /// Print without a type, skipping the TypePrinting object.
46470b57cec5SDimitry Andric ///
46480b57cec5SDimitry Andric /// \return \c true iff printing was successful.
46490b57cec5SDimitry Andric static bool printWithoutType(const Value &V, raw_ostream &O,
46500b57cec5SDimitry Andric                              SlotTracker *Machine, const Module *M) {
46510b57cec5SDimitry Andric   if (V.hasName() || isa<GlobalValue>(V) ||
46520b57cec5SDimitry Andric       (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
4653*349cc55cSDimitry Andric     AsmWriterContext WriterCtx(nullptr, Machine, M);
4654*349cc55cSDimitry Andric     WriteAsOperandInternal(O, &V, WriterCtx);
46550b57cec5SDimitry Andric     return true;
46560b57cec5SDimitry Andric   }
46570b57cec5SDimitry Andric   return false;
46580b57cec5SDimitry Andric }
46590b57cec5SDimitry Andric 
46600b57cec5SDimitry Andric static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
46610b57cec5SDimitry Andric                                ModuleSlotTracker &MST) {
46620b57cec5SDimitry Andric   TypePrinting TypePrinter(MST.getModule());
46630b57cec5SDimitry Andric   if (PrintType) {
46640b57cec5SDimitry Andric     TypePrinter.print(V.getType(), O);
46650b57cec5SDimitry Andric     O << ' ';
46660b57cec5SDimitry Andric   }
46670b57cec5SDimitry Andric 
4668*349cc55cSDimitry Andric   AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine(), MST.getModule());
4669*349cc55cSDimitry Andric   WriteAsOperandInternal(O, &V, WriterCtx);
46700b57cec5SDimitry Andric }
46710b57cec5SDimitry Andric 
46720b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType,
46730b57cec5SDimitry Andric                            const Module *M) const {
46740b57cec5SDimitry Andric   if (!M)
46750b57cec5SDimitry Andric     M = getModuleFromVal(this);
46760b57cec5SDimitry Andric 
46770b57cec5SDimitry Andric   if (!PrintType)
46780b57cec5SDimitry Andric     if (printWithoutType(*this, O, nullptr, M))
46790b57cec5SDimitry Andric       return;
46800b57cec5SDimitry Andric 
46810b57cec5SDimitry Andric   SlotTracker Machine(
46820b57cec5SDimitry Andric       M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
46830b57cec5SDimitry Andric   ModuleSlotTracker MST(Machine, M);
46840b57cec5SDimitry Andric   printAsOperandImpl(*this, O, PrintType, MST);
46850b57cec5SDimitry Andric }
46860b57cec5SDimitry Andric 
46870b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType,
46880b57cec5SDimitry Andric                            ModuleSlotTracker &MST) const {
46890b57cec5SDimitry Andric   if (!PrintType)
46900b57cec5SDimitry Andric     if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
46910b57cec5SDimitry Andric       return;
46920b57cec5SDimitry Andric 
46930b57cec5SDimitry Andric   printAsOperandImpl(*this, O, PrintType, MST);
46940b57cec5SDimitry Andric }
46950b57cec5SDimitry Andric 
4696*349cc55cSDimitry Andric /// Recursive version of printMetadataImpl.
4697*349cc55cSDimitry Andric static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD,
4698*349cc55cSDimitry Andric                                  AsmWriterContext &WriterCtx) {
4699*349cc55cSDimitry Andric   formatted_raw_ostream OS(ROS);
4700*349cc55cSDimitry Andric   WriteAsOperandInternal(OS, &MD, WriterCtx, /* FromValue */ true);
4701*349cc55cSDimitry Andric 
4702*349cc55cSDimitry Andric   auto *N = dyn_cast<MDNode>(&MD);
4703*349cc55cSDimitry Andric   if (!N || isa<DIExpression>(MD) || isa<DIArgList>(MD))
4704*349cc55cSDimitry Andric     return;
4705*349cc55cSDimitry Andric 
4706*349cc55cSDimitry Andric   OS << " = ";
4707*349cc55cSDimitry Andric   WriteMDNodeBodyInternal(OS, N, WriterCtx);
4708*349cc55cSDimitry Andric }
4709*349cc55cSDimitry Andric 
4710*349cc55cSDimitry Andric namespace {
4711*349cc55cSDimitry Andric struct MDTreeAsmWriterContext : public AsmWriterContext {
4712*349cc55cSDimitry Andric   unsigned Level;
4713*349cc55cSDimitry Andric   // {Level, Printed string}
4714*349cc55cSDimitry Andric   using EntryTy = std::pair<unsigned, std::string>;
4715*349cc55cSDimitry Andric   SmallVector<EntryTy, 4> Buffer;
4716*349cc55cSDimitry Andric 
4717*349cc55cSDimitry Andric   // Used to break the cycle in case there is any.
4718*349cc55cSDimitry Andric   SmallPtrSet<const Metadata *, 4> Visited;
4719*349cc55cSDimitry Andric 
4720*349cc55cSDimitry Andric   raw_ostream &MainOS;
4721*349cc55cSDimitry Andric 
4722*349cc55cSDimitry Andric   MDTreeAsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M,
4723*349cc55cSDimitry Andric                          raw_ostream &OS, const Metadata *InitMD)
4724*349cc55cSDimitry Andric       : AsmWriterContext(TP, ST, M), Level(0U), Visited({InitMD}), MainOS(OS) {}
4725*349cc55cSDimitry Andric 
4726*349cc55cSDimitry Andric   void onWriteMetadataAsOperand(const Metadata *MD) override {
4727*349cc55cSDimitry Andric     if (Visited.count(MD))
4728*349cc55cSDimitry Andric       return;
4729*349cc55cSDimitry Andric     Visited.insert(MD);
4730*349cc55cSDimitry Andric 
4731*349cc55cSDimitry Andric     std::string Str;
4732*349cc55cSDimitry Andric     raw_string_ostream SS(Str);
4733*349cc55cSDimitry Andric     ++Level;
4734*349cc55cSDimitry Andric     // A placeholder entry to memorize the correct
4735*349cc55cSDimitry Andric     // position in buffer.
4736*349cc55cSDimitry Andric     Buffer.emplace_back(std::make_pair(Level, ""));
4737*349cc55cSDimitry Andric     unsigned InsertIdx = Buffer.size() - 1;
4738*349cc55cSDimitry Andric 
4739*349cc55cSDimitry Andric     printMetadataImplRec(SS, *MD, *this);
4740*349cc55cSDimitry Andric     Buffer[InsertIdx].second = std::move(SS.str());
4741*349cc55cSDimitry Andric     --Level;
4742*349cc55cSDimitry Andric   }
4743*349cc55cSDimitry Andric 
4744*349cc55cSDimitry Andric   ~MDTreeAsmWriterContext() {
4745*349cc55cSDimitry Andric     for (const auto &Entry : Buffer) {
4746*349cc55cSDimitry Andric       MainOS << "\n";
4747*349cc55cSDimitry Andric       unsigned NumIndent = Entry.first * 2U;
4748*349cc55cSDimitry Andric       MainOS.indent(NumIndent) << Entry.second;
4749*349cc55cSDimitry Andric     }
4750*349cc55cSDimitry Andric   }
4751*349cc55cSDimitry Andric };
4752*349cc55cSDimitry Andric } // end anonymous namespace
4753*349cc55cSDimitry Andric 
47540b57cec5SDimitry Andric static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
47550b57cec5SDimitry Andric                               ModuleSlotTracker &MST, const Module *M,
4756*349cc55cSDimitry Andric                               bool OnlyAsOperand, bool PrintAsTree = false) {
47570b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
47580b57cec5SDimitry Andric 
47590b57cec5SDimitry Andric   TypePrinting TypePrinter(M);
47600b57cec5SDimitry Andric 
4761*349cc55cSDimitry Andric   std::unique_ptr<AsmWriterContext> WriterCtx;
4762*349cc55cSDimitry Andric   if (PrintAsTree && !OnlyAsOperand)
4763*349cc55cSDimitry Andric     WriterCtx = std::make_unique<MDTreeAsmWriterContext>(
4764*349cc55cSDimitry Andric         &TypePrinter, MST.getMachine(), M, OS, &MD);
4765*349cc55cSDimitry Andric   else
4766*349cc55cSDimitry Andric     WriterCtx =
4767*349cc55cSDimitry Andric         std::make_unique<AsmWriterContext>(&TypePrinter, MST.getMachine(), M);
4768*349cc55cSDimitry Andric 
4769*349cc55cSDimitry Andric   WriteAsOperandInternal(OS, &MD, *WriterCtx, /* FromValue */ true);
47700b57cec5SDimitry Andric 
47710b57cec5SDimitry Andric   auto *N = dyn_cast<MDNode>(&MD);
4772fe6060f1SDimitry Andric   if (OnlyAsOperand || !N || isa<DIExpression>(MD) || isa<DIArgList>(MD))
47730b57cec5SDimitry Andric     return;
47740b57cec5SDimitry Andric 
47750b57cec5SDimitry Andric   OS << " = ";
4776*349cc55cSDimitry Andric   WriteMDNodeBodyInternal(OS, N, *WriterCtx);
47770b57cec5SDimitry Andric }
47780b57cec5SDimitry Andric 
47790b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
47800b57cec5SDimitry Andric   ModuleSlotTracker MST(M, isa<MDNode>(this));
47810b57cec5SDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
47820b57cec5SDimitry Andric }
47830b57cec5SDimitry Andric 
47840b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
47850b57cec5SDimitry Andric                               const Module *M) const {
47860b57cec5SDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
47870b57cec5SDimitry Andric }
47880b57cec5SDimitry Andric 
47890b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, const Module *M,
47900b57cec5SDimitry Andric                      bool /*IsForDebug*/) const {
47910b57cec5SDimitry Andric   ModuleSlotTracker MST(M, isa<MDNode>(this));
47920b57cec5SDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
47930b57cec5SDimitry Andric }
47940b57cec5SDimitry Andric 
47950b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
47960b57cec5SDimitry Andric                      const Module *M, bool /*IsForDebug*/) const {
47970b57cec5SDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
47980b57cec5SDimitry Andric }
47990b57cec5SDimitry Andric 
4800*349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, const Module *M) const {
4801*349cc55cSDimitry Andric   ModuleSlotTracker MST(M, true);
4802*349cc55cSDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
4803*349cc55cSDimitry Andric                     /*PrintAsTree=*/true);
4804*349cc55cSDimitry Andric }
4805*349cc55cSDimitry Andric 
4806*349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, ModuleSlotTracker &MST,
4807*349cc55cSDimitry Andric                        const Module *M) const {
4808*349cc55cSDimitry Andric   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false,
4809*349cc55cSDimitry Andric                     /*PrintAsTree=*/true);
4810*349cc55cSDimitry Andric }
4811*349cc55cSDimitry Andric 
48120b57cec5SDimitry Andric void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
48130b57cec5SDimitry Andric   SlotTracker SlotTable(this);
48140b57cec5SDimitry Andric   formatted_raw_ostream OS(ROS);
48150b57cec5SDimitry Andric   AssemblyWriter W(OS, SlotTable, this, IsForDebug);
48160b57cec5SDimitry Andric   W.printModuleSummaryIndex();
48170b57cec5SDimitry Andric }
48180b57cec5SDimitry Andric 
4819fe6060f1SDimitry Andric void ModuleSlotTracker::collectMDNodes(MachineMDNodeListType &L, unsigned LB,
4820fe6060f1SDimitry Andric                                        unsigned UB) const {
4821fe6060f1SDimitry Andric   SlotTracker *ST = MachineStorage.get();
4822fe6060f1SDimitry Andric   if (!ST)
4823fe6060f1SDimitry Andric     return;
4824fe6060f1SDimitry Andric 
4825fe6060f1SDimitry Andric   for (auto &I : llvm::make_range(ST->mdn_begin(), ST->mdn_end()))
4826fe6060f1SDimitry Andric     if (I.second >= LB && I.second < UB)
4827fe6060f1SDimitry Andric       L.push_back(std::make_pair(I.second, I.first));
4828fe6060f1SDimitry Andric }
4829fe6060f1SDimitry Andric 
48300b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
48310b57cec5SDimitry Andric // Value::dump - allow easy printing of Values from the debugger.
48320b57cec5SDimitry Andric LLVM_DUMP_METHOD
48330b57cec5SDimitry Andric void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
48340b57cec5SDimitry Andric 
48350b57cec5SDimitry Andric // Type::dump - allow easy printing of Types from the debugger.
48360b57cec5SDimitry Andric LLVM_DUMP_METHOD
48370b57cec5SDimitry Andric void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
48380b57cec5SDimitry Andric 
48390b57cec5SDimitry Andric // Module::dump() - Allow printing of Modules from the debugger.
48400b57cec5SDimitry Andric LLVM_DUMP_METHOD
48410b57cec5SDimitry Andric void Module::dump() const {
48420b57cec5SDimitry Andric   print(dbgs(), nullptr,
48430b57cec5SDimitry Andric         /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
48440b57cec5SDimitry Andric }
48450b57cec5SDimitry Andric 
48460b57cec5SDimitry Andric // Allow printing of Comdats from the debugger.
48470b57cec5SDimitry Andric LLVM_DUMP_METHOD
48480b57cec5SDimitry Andric void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
48490b57cec5SDimitry Andric 
48500b57cec5SDimitry Andric // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
48510b57cec5SDimitry Andric LLVM_DUMP_METHOD
48520b57cec5SDimitry Andric void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
48530b57cec5SDimitry Andric 
48540b57cec5SDimitry Andric LLVM_DUMP_METHOD
48550b57cec5SDimitry Andric void Metadata::dump() const { dump(nullptr); }
48560b57cec5SDimitry Andric 
48570b57cec5SDimitry Andric LLVM_DUMP_METHOD
48580b57cec5SDimitry Andric void Metadata::dump(const Module *M) const {
48590b57cec5SDimitry Andric   print(dbgs(), M, /*IsForDebug=*/true);
48600b57cec5SDimitry Andric   dbgs() << '\n';
48610b57cec5SDimitry Andric }
48620b57cec5SDimitry Andric 
4863*349cc55cSDimitry Andric LLVM_DUMP_METHOD
4864*349cc55cSDimitry Andric void MDNode::dumpTree() const { dumpTree(nullptr); }
4865*349cc55cSDimitry Andric 
4866*349cc55cSDimitry Andric LLVM_DUMP_METHOD
4867*349cc55cSDimitry Andric void MDNode::dumpTree(const Module *M) const {
4868*349cc55cSDimitry Andric   printTree(dbgs(), M);
4869*349cc55cSDimitry Andric   dbgs() << '\n';
4870*349cc55cSDimitry Andric }
4871*349cc55cSDimitry Andric 
48720b57cec5SDimitry Andric // Allow printing of ModuleSummaryIndex from the debugger.
48730b57cec5SDimitry Andric LLVM_DUMP_METHOD
48740b57cec5SDimitry Andric void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
48750b57cec5SDimitry Andric #endif
4876