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" 26349cc55cSDimitry 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" 75349cc55cSDimitry 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) { 356*4824e7fdSDimitry Andric for (unsigned char C : Name) { 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 if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' && 3620b57cec5SDimitry Andric C != '_') { 3630b57cec5SDimitry Andric NeedsQuotes = true; 3640b57cec5SDimitry Andric break; 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric } 3670b57cec5SDimitry Andric } 3680b57cec5SDimitry Andric 3690b57cec5SDimitry Andric // If we didn't need any quotes, just write out the name in one blast. 3700b57cec5SDimitry Andric if (!NeedsQuotes) { 3710b57cec5SDimitry Andric OS << Name; 3720b57cec5SDimitry Andric return; 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric 3750b57cec5SDimitry Andric // Okay, we need quotes. Output the quotes and escape any scary characters as 3760b57cec5SDimitry Andric // needed. 3770b57cec5SDimitry Andric OS << '"'; 3780b57cec5SDimitry Andric printEscapedString(Name, OS); 3790b57cec5SDimitry Andric OS << '"'; 3800b57cec5SDimitry Andric } 3810b57cec5SDimitry Andric 3820b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with % 3830b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s 3840b57cec5SDimitry Andric /// (if it has special chars in it). Print it out. 3850b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) { 3860b57cec5SDimitry Andric switch (Prefix) { 3870b57cec5SDimitry Andric case NoPrefix: 3880b57cec5SDimitry Andric break; 3890b57cec5SDimitry Andric case GlobalPrefix: 3900b57cec5SDimitry Andric OS << '@'; 3910b57cec5SDimitry Andric break; 3920b57cec5SDimitry Andric case ComdatPrefix: 3930b57cec5SDimitry Andric OS << '$'; 3940b57cec5SDimitry Andric break; 3950b57cec5SDimitry Andric case LabelPrefix: 3960b57cec5SDimitry Andric break; 3970b57cec5SDimitry Andric case LocalPrefix: 3980b57cec5SDimitry Andric OS << '%'; 3990b57cec5SDimitry Andric break; 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric printLLVMNameWithoutPrefix(OS, Name); 4020b57cec5SDimitry Andric } 4030b57cec5SDimitry Andric 4040b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with % 4050b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s 4060b57cec5SDimitry Andric /// (if it has special chars in it). Print it out. 4070b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, const Value *V) { 4080b57cec5SDimitry Andric PrintLLVMName(OS, V->getName(), 4090b57cec5SDimitry Andric isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric 4125ffd83dbSDimitry Andric static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) { 4135ffd83dbSDimitry Andric Out << ", <"; 4145ffd83dbSDimitry Andric if (isa<ScalableVectorType>(Ty)) 4155ffd83dbSDimitry Andric Out << "vscale x "; 4165ffd83dbSDimitry Andric Out << Mask.size() << " x i32> "; 4175ffd83dbSDimitry Andric bool FirstElt = true; 4185ffd83dbSDimitry Andric if (all_of(Mask, [](int Elt) { return Elt == 0; })) { 4195ffd83dbSDimitry Andric Out << "zeroinitializer"; 4205ffd83dbSDimitry Andric } else if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) { 4215ffd83dbSDimitry Andric Out << "undef"; 4225ffd83dbSDimitry Andric } else { 4235ffd83dbSDimitry Andric Out << "<"; 4245ffd83dbSDimitry Andric for (int Elt : Mask) { 4255ffd83dbSDimitry Andric if (FirstElt) 4265ffd83dbSDimitry Andric FirstElt = false; 4275ffd83dbSDimitry Andric else 4285ffd83dbSDimitry Andric Out << ", "; 4295ffd83dbSDimitry Andric Out << "i32 "; 4305ffd83dbSDimitry Andric if (Elt == UndefMaskElem) 4315ffd83dbSDimitry Andric Out << "undef"; 4325ffd83dbSDimitry Andric else 4335ffd83dbSDimitry Andric Out << Elt; 4345ffd83dbSDimitry Andric } 4355ffd83dbSDimitry Andric Out << ">"; 4365ffd83dbSDimitry Andric } 4375ffd83dbSDimitry Andric } 4385ffd83dbSDimitry Andric 4390b57cec5SDimitry Andric namespace { 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric class TypePrinting { 4420b57cec5SDimitry Andric public: 4430b57cec5SDimitry Andric TypePrinting(const Module *M = nullptr) : DeferredM(M) {} 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric TypePrinting(const TypePrinting &) = delete; 4460b57cec5SDimitry Andric TypePrinting &operator=(const TypePrinting &) = delete; 4470b57cec5SDimitry Andric 4480b57cec5SDimitry Andric /// The named types that are used by the current module. 4490b57cec5SDimitry Andric TypeFinder &getNamedTypes(); 4500b57cec5SDimitry Andric 4510b57cec5SDimitry Andric /// The numbered types, number to type mapping. 4520b57cec5SDimitry Andric std::vector<StructType *> &getNumberedTypes(); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric bool empty(); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric void print(Type *Ty, raw_ostream &OS); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric void printStructBody(StructType *Ty, raw_ostream &OS); 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric private: 4610b57cec5SDimitry Andric void incorporateTypes(); 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric /// A module to process lazily when needed. Set to nullptr as soon as used. 4640b57cec5SDimitry Andric const Module *DeferredM; 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric TypeFinder NamedTypes; 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric // The numbered types, along with their value. 4690b57cec5SDimitry Andric DenseMap<StructType *, unsigned> Type2Number; 4700b57cec5SDimitry Andric 4710b57cec5SDimitry Andric std::vector<StructType *> NumberedTypes; 4720b57cec5SDimitry Andric }; 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric } // end anonymous namespace 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric TypeFinder &TypePrinting::getNamedTypes() { 4770b57cec5SDimitry Andric incorporateTypes(); 4780b57cec5SDimitry Andric return NamedTypes; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric std::vector<StructType *> &TypePrinting::getNumberedTypes() { 4820b57cec5SDimitry Andric incorporateTypes(); 4830b57cec5SDimitry Andric 4840b57cec5SDimitry Andric // We know all the numbers that each type is used and we know that it is a 4850b57cec5SDimitry Andric // dense assignment. Convert the map to an index table, if it's not done 4860b57cec5SDimitry Andric // already (judging from the sizes): 4870b57cec5SDimitry Andric if (NumberedTypes.size() == Type2Number.size()) 4880b57cec5SDimitry Andric return NumberedTypes; 4890b57cec5SDimitry Andric 4900b57cec5SDimitry Andric NumberedTypes.resize(Type2Number.size()); 4910b57cec5SDimitry Andric for (const auto &P : Type2Number) { 4920b57cec5SDimitry Andric assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?"); 4930b57cec5SDimitry Andric assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?"); 4940b57cec5SDimitry Andric NumberedTypes[P.second] = P.first; 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric return NumberedTypes; 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric bool TypePrinting::empty() { 5000b57cec5SDimitry Andric incorporateTypes(); 5010b57cec5SDimitry Andric return NamedTypes.empty() && Type2Number.empty(); 5020b57cec5SDimitry Andric } 5030b57cec5SDimitry Andric 5040b57cec5SDimitry Andric void TypePrinting::incorporateTypes() { 5050b57cec5SDimitry Andric if (!DeferredM) 5060b57cec5SDimitry Andric return; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric NamedTypes.run(*DeferredM, false); 5090b57cec5SDimitry Andric DeferredM = nullptr; 5100b57cec5SDimitry Andric 5110b57cec5SDimitry Andric // The list of struct types we got back includes all the struct types, split 5120b57cec5SDimitry Andric // the unnamed ones out to a numbering and remove the anonymous structs. 5130b57cec5SDimitry Andric unsigned NextNumber = 0; 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E; 5160b57cec5SDimitry Andric for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) { 5170b57cec5SDimitry Andric StructType *STy = *I; 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric // Ignore anonymous types. 5200b57cec5SDimitry Andric if (STy->isLiteral()) 5210b57cec5SDimitry Andric continue; 5220b57cec5SDimitry Andric 5230b57cec5SDimitry Andric if (STy->getName().empty()) 5240b57cec5SDimitry Andric Type2Number[STy] = NextNumber++; 5250b57cec5SDimitry Andric else 5260b57cec5SDimitry Andric *NextToUse++ = STy; 5270b57cec5SDimitry Andric } 5280b57cec5SDimitry Andric 5290b57cec5SDimitry Andric NamedTypes.erase(NextToUse, NamedTypes.end()); 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric 5320b57cec5SDimitry Andric /// Write the specified type to the specified raw_ostream, making use of type 5330b57cec5SDimitry Andric /// names or up references to shorten the type name where possible. 5340b57cec5SDimitry Andric void TypePrinting::print(Type *Ty, raw_ostream &OS) { 5350b57cec5SDimitry Andric switch (Ty->getTypeID()) { 5360b57cec5SDimitry Andric case Type::VoidTyID: OS << "void"; return; 5370b57cec5SDimitry Andric case Type::HalfTyID: OS << "half"; return; 5385ffd83dbSDimitry Andric case Type::BFloatTyID: OS << "bfloat"; return; 5390b57cec5SDimitry Andric case Type::FloatTyID: OS << "float"; return; 5400b57cec5SDimitry Andric case Type::DoubleTyID: OS << "double"; return; 5410b57cec5SDimitry Andric case Type::X86_FP80TyID: OS << "x86_fp80"; return; 5420b57cec5SDimitry Andric case Type::FP128TyID: OS << "fp128"; return; 5430b57cec5SDimitry Andric case Type::PPC_FP128TyID: OS << "ppc_fp128"; return; 5440b57cec5SDimitry Andric case Type::LabelTyID: OS << "label"; return; 5450b57cec5SDimitry Andric case Type::MetadataTyID: OS << "metadata"; return; 5460b57cec5SDimitry Andric case Type::X86_MMXTyID: OS << "x86_mmx"; return; 547e8d8bef9SDimitry Andric case Type::X86_AMXTyID: OS << "x86_amx"; return; 5480b57cec5SDimitry Andric case Type::TokenTyID: OS << "token"; return; 5490b57cec5SDimitry Andric case Type::IntegerTyID: 5500b57cec5SDimitry Andric OS << 'i' << cast<IntegerType>(Ty)->getBitWidth(); 5510b57cec5SDimitry Andric return; 5520b57cec5SDimitry Andric 5530b57cec5SDimitry Andric case Type::FunctionTyID: { 5540b57cec5SDimitry Andric FunctionType *FTy = cast<FunctionType>(Ty); 5550b57cec5SDimitry Andric print(FTy->getReturnType(), OS); 5560b57cec5SDimitry Andric OS << " ("; 557349cc55cSDimitry Andric ListSeparator LS; 558349cc55cSDimitry Andric for (Type *Ty : FTy->params()) { 559349cc55cSDimitry Andric OS << LS; 560349cc55cSDimitry Andric print(Ty, OS); 5610b57cec5SDimitry Andric } 562349cc55cSDimitry Andric if (FTy->isVarArg()) 563349cc55cSDimitry Andric OS << LS << "..."; 5640b57cec5SDimitry Andric OS << ')'; 5650b57cec5SDimitry Andric return; 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric case Type::StructTyID: { 5680b57cec5SDimitry Andric StructType *STy = cast<StructType>(Ty); 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric if (STy->isLiteral()) 5710b57cec5SDimitry Andric return printStructBody(STy, OS); 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric if (!STy->getName().empty()) 5740b57cec5SDimitry Andric return PrintLLVMName(OS, STy->getName(), LocalPrefix); 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric incorporateTypes(); 5770b57cec5SDimitry Andric const auto I = Type2Number.find(STy); 5780b57cec5SDimitry Andric if (I != Type2Number.end()) 5790b57cec5SDimitry Andric OS << '%' << I->second; 5800b57cec5SDimitry Andric else // Not enumerated, print the hex address. 5810b57cec5SDimitry Andric OS << "%\"type " << STy << '\"'; 5820b57cec5SDimitry Andric return; 5830b57cec5SDimitry Andric } 5840b57cec5SDimitry Andric case Type::PointerTyID: { 5850b57cec5SDimitry Andric PointerType *PTy = cast<PointerType>(Ty); 586fe6060f1SDimitry Andric if (PTy->isOpaque()) { 587fe6060f1SDimitry Andric OS << "ptr"; 588fe6060f1SDimitry Andric if (unsigned AddressSpace = PTy->getAddressSpace()) 589fe6060f1SDimitry Andric OS << " addrspace(" << AddressSpace << ')'; 590fe6060f1SDimitry Andric return; 591fe6060f1SDimitry Andric } 5920b57cec5SDimitry Andric print(PTy->getElementType(), OS); 5930b57cec5SDimitry Andric if (unsigned AddressSpace = PTy->getAddressSpace()) 5940b57cec5SDimitry Andric OS << " addrspace(" << AddressSpace << ')'; 5950b57cec5SDimitry Andric OS << '*'; 5960b57cec5SDimitry Andric return; 5970b57cec5SDimitry Andric } 5980b57cec5SDimitry Andric case Type::ArrayTyID: { 5990b57cec5SDimitry Andric ArrayType *ATy = cast<ArrayType>(Ty); 6000b57cec5SDimitry Andric OS << '[' << ATy->getNumElements() << " x "; 6010b57cec5SDimitry Andric print(ATy->getElementType(), OS); 6020b57cec5SDimitry Andric OS << ']'; 6030b57cec5SDimitry Andric return; 6040b57cec5SDimitry Andric } 6055ffd83dbSDimitry Andric case Type::FixedVectorTyID: 6065ffd83dbSDimitry Andric case Type::ScalableVectorTyID: { 6070b57cec5SDimitry Andric VectorType *PTy = cast<VectorType>(Ty); 6085ffd83dbSDimitry Andric ElementCount EC = PTy->getElementCount(); 6090b57cec5SDimitry Andric OS << "<"; 610e8d8bef9SDimitry Andric if (EC.isScalable()) 6110b57cec5SDimitry Andric OS << "vscale x "; 612e8d8bef9SDimitry Andric OS << EC.getKnownMinValue() << " x "; 6130b57cec5SDimitry Andric print(PTy->getElementType(), OS); 6140b57cec5SDimitry Andric OS << '>'; 6150b57cec5SDimitry Andric return; 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric } 6180b57cec5SDimitry Andric llvm_unreachable("Invalid TypeID"); 6190b57cec5SDimitry Andric } 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) { 6220b57cec5SDimitry Andric if (STy->isOpaque()) { 6230b57cec5SDimitry Andric OS << "opaque"; 6240b57cec5SDimitry Andric return; 6250b57cec5SDimitry Andric } 6260b57cec5SDimitry Andric 6270b57cec5SDimitry Andric if (STy->isPacked()) 6280b57cec5SDimitry Andric OS << '<'; 6290b57cec5SDimitry Andric 6300b57cec5SDimitry Andric if (STy->getNumElements() == 0) { 6310b57cec5SDimitry Andric OS << "{}"; 6320b57cec5SDimitry Andric } else { 6330b57cec5SDimitry Andric OS << "{ "; 634349cc55cSDimitry Andric ListSeparator LS; 635349cc55cSDimitry Andric for (Type *Ty : STy->elements()) { 636349cc55cSDimitry Andric OS << LS; 637349cc55cSDimitry Andric print(Ty, OS); 6380b57cec5SDimitry Andric } 6390b57cec5SDimitry Andric 6400b57cec5SDimitry Andric OS << " }"; 6410b57cec5SDimitry Andric } 6420b57cec5SDimitry Andric if (STy->isPacked()) 6430b57cec5SDimitry Andric OS << '>'; 6440b57cec5SDimitry Andric } 6450b57cec5SDimitry Andric 646fe6060f1SDimitry Andric AbstractSlotTrackerStorage::~AbstractSlotTrackerStorage() {} 647fe6060f1SDimitry Andric 6480b57cec5SDimitry Andric namespace llvm { 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6510b57cec5SDimitry Andric // SlotTracker Class: Enumerate slot numbers for unnamed values 6520b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6530b57cec5SDimitry Andric /// This class provides computation of slot numbers for LLVM Assembly writing. 6540b57cec5SDimitry Andric /// 655fe6060f1SDimitry Andric class SlotTracker : public AbstractSlotTrackerStorage { 6560b57cec5SDimitry Andric public: 6570b57cec5SDimitry Andric /// ValueMap - A mapping of Values to slot numbers. 6580b57cec5SDimitry Andric using ValueMap = DenseMap<const Value *, unsigned>; 6590b57cec5SDimitry Andric 6600b57cec5SDimitry Andric private: 6610b57cec5SDimitry Andric /// TheModule - The module for which we are holding slot numbers. 6620b57cec5SDimitry Andric const Module* TheModule; 6630b57cec5SDimitry Andric 6640b57cec5SDimitry Andric /// TheFunction - The function for which we are holding slot numbers. 6650b57cec5SDimitry Andric const Function* TheFunction = nullptr; 6660b57cec5SDimitry Andric bool FunctionProcessed = false; 6670b57cec5SDimitry Andric bool ShouldInitializeAllMetadata; 6680b57cec5SDimitry Andric 669fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 670fe6060f1SDimitry Andric ProcessModuleHookFn; 671fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 672fe6060f1SDimitry Andric ProcessFunctionHookFn; 673fe6060f1SDimitry Andric 6740b57cec5SDimitry Andric /// The summary index for which we are holding slot numbers. 6750b57cec5SDimitry Andric const ModuleSummaryIndex *TheIndex = nullptr; 6760b57cec5SDimitry Andric 6770b57cec5SDimitry Andric /// mMap - The slot map for the module level data. 6780b57cec5SDimitry Andric ValueMap mMap; 6790b57cec5SDimitry Andric unsigned mNext = 0; 6800b57cec5SDimitry Andric 6810b57cec5SDimitry Andric /// fMap - The slot map for the function level data. 6820b57cec5SDimitry Andric ValueMap fMap; 6830b57cec5SDimitry Andric unsigned fNext = 0; 6840b57cec5SDimitry Andric 6850b57cec5SDimitry Andric /// mdnMap - Map for MDNodes. 6860b57cec5SDimitry Andric DenseMap<const MDNode*, unsigned> mdnMap; 6870b57cec5SDimitry Andric unsigned mdnNext = 0; 6880b57cec5SDimitry Andric 6890b57cec5SDimitry Andric /// asMap - The slot map for attribute sets. 6900b57cec5SDimitry Andric DenseMap<AttributeSet, unsigned> asMap; 6910b57cec5SDimitry Andric unsigned asNext = 0; 6920b57cec5SDimitry Andric 6930b57cec5SDimitry Andric /// ModulePathMap - The slot map for Module paths used in the summary index. 6940b57cec5SDimitry Andric StringMap<unsigned> ModulePathMap; 6950b57cec5SDimitry Andric unsigned ModulePathNext = 0; 6960b57cec5SDimitry Andric 6970b57cec5SDimitry Andric /// GUIDMap - The slot map for GUIDs used in the summary index. 6980b57cec5SDimitry Andric DenseMap<GlobalValue::GUID, unsigned> GUIDMap; 6990b57cec5SDimitry Andric unsigned GUIDNext = 0; 7000b57cec5SDimitry Andric 7010b57cec5SDimitry Andric /// TypeIdMap - The slot map for type ids used in the summary index. 7020b57cec5SDimitry Andric StringMap<unsigned> TypeIdMap; 7030b57cec5SDimitry Andric unsigned TypeIdNext = 0; 7040b57cec5SDimitry Andric 7050b57cec5SDimitry Andric public: 7060b57cec5SDimitry Andric /// Construct from a module. 7070b57cec5SDimitry Andric /// 7080b57cec5SDimitry Andric /// If \c ShouldInitializeAllMetadata, initializes all metadata in all 7090b57cec5SDimitry Andric /// functions, giving correct numbering for metadata referenced only from 7100b57cec5SDimitry Andric /// within a function (even if no functions have been initialized). 7110b57cec5SDimitry Andric explicit SlotTracker(const Module *M, 7120b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false); 7130b57cec5SDimitry Andric 7140b57cec5SDimitry Andric /// Construct from a function, starting out in incorp state. 7150b57cec5SDimitry Andric /// 7160b57cec5SDimitry Andric /// If \c ShouldInitializeAllMetadata, initializes all metadata in all 7170b57cec5SDimitry Andric /// functions, giving correct numbering for metadata referenced only from 7180b57cec5SDimitry Andric /// within a function (even if no functions have been initialized). 7190b57cec5SDimitry Andric explicit SlotTracker(const Function *F, 7200b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false); 7210b57cec5SDimitry Andric 7220b57cec5SDimitry Andric /// Construct from a module summary index. 7230b57cec5SDimitry Andric explicit SlotTracker(const ModuleSummaryIndex *Index); 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric SlotTracker(const SlotTracker &) = delete; 7260b57cec5SDimitry Andric SlotTracker &operator=(const SlotTracker &) = delete; 7270b57cec5SDimitry Andric 728fe6060f1SDimitry Andric ~SlotTracker() = default; 729fe6060f1SDimitry Andric 730fe6060f1SDimitry Andric void setProcessHook( 731fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>); 732fe6060f1SDimitry Andric void setProcessHook(std::function<void(AbstractSlotTrackerStorage *, 733fe6060f1SDimitry Andric const Function *, bool)>); 734fe6060f1SDimitry Andric 735fe6060f1SDimitry Andric unsigned getNextMetadataSlot() override { return mdnNext; } 736fe6060f1SDimitry Andric 737fe6060f1SDimitry Andric void createMetadataSlot(const MDNode *N) override; 738fe6060f1SDimitry Andric 7390b57cec5SDimitry Andric /// Return the slot number of the specified value in it's type 7400b57cec5SDimitry Andric /// plane. If something is not in the SlotTracker, return -1. 7410b57cec5SDimitry Andric int getLocalSlot(const Value *V); 7420b57cec5SDimitry Andric int getGlobalSlot(const GlobalValue *V); 743fe6060f1SDimitry Andric int getMetadataSlot(const MDNode *N) override; 7440b57cec5SDimitry Andric int getAttributeGroupSlot(AttributeSet AS); 7450b57cec5SDimitry Andric int getModulePathSlot(StringRef Path); 7460b57cec5SDimitry Andric int getGUIDSlot(GlobalValue::GUID GUID); 7470b57cec5SDimitry Andric int getTypeIdSlot(StringRef Id); 7480b57cec5SDimitry Andric 7490b57cec5SDimitry Andric /// If you'd like to deal with a function instead of just a module, use 7500b57cec5SDimitry Andric /// this method to get its data into the SlotTracker. 7510b57cec5SDimitry Andric void incorporateFunction(const Function *F) { 7520b57cec5SDimitry Andric TheFunction = F; 7530b57cec5SDimitry Andric FunctionProcessed = false; 7540b57cec5SDimitry Andric } 7550b57cec5SDimitry Andric 7560b57cec5SDimitry Andric const Function *getFunction() const { return TheFunction; } 7570b57cec5SDimitry Andric 7580b57cec5SDimitry Andric /// After calling incorporateFunction, use this method to remove the 7590b57cec5SDimitry Andric /// most recently incorporated function from the SlotTracker. This 7600b57cec5SDimitry Andric /// will reset the state of the machine back to just the module contents. 7610b57cec5SDimitry Andric void purgeFunction(); 7620b57cec5SDimitry Andric 7630b57cec5SDimitry Andric /// MDNode map iterators. 7640b57cec5SDimitry Andric using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator; 7650b57cec5SDimitry Andric 7660b57cec5SDimitry Andric mdn_iterator mdn_begin() { return mdnMap.begin(); } 7670b57cec5SDimitry Andric mdn_iterator mdn_end() { return mdnMap.end(); } 7680b57cec5SDimitry Andric unsigned mdn_size() const { return mdnMap.size(); } 7690b57cec5SDimitry Andric bool mdn_empty() const { return mdnMap.empty(); } 7700b57cec5SDimitry Andric 7710b57cec5SDimitry Andric /// AttributeSet map iterators. 7720b57cec5SDimitry Andric using as_iterator = DenseMap<AttributeSet, unsigned>::iterator; 7730b57cec5SDimitry Andric 7740b57cec5SDimitry Andric as_iterator as_begin() { return asMap.begin(); } 7750b57cec5SDimitry Andric as_iterator as_end() { return asMap.end(); } 7760b57cec5SDimitry Andric unsigned as_size() const { return asMap.size(); } 7770b57cec5SDimitry Andric bool as_empty() const { return asMap.empty(); } 7780b57cec5SDimitry Andric 7790b57cec5SDimitry Andric /// GUID map iterators. 7800b57cec5SDimitry Andric using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator; 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric /// These functions do the actual initialization. 7830b57cec5SDimitry Andric inline void initializeIfNeeded(); 7845ffd83dbSDimitry Andric int initializeIndexIfNeeded(); 7850b57cec5SDimitry Andric 7860b57cec5SDimitry Andric // Implementation Details 7870b57cec5SDimitry Andric private: 7880b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. 7890b57cec5SDimitry Andric void CreateModuleSlot(const GlobalValue *V); 7900b57cec5SDimitry Andric 7910b57cec5SDimitry Andric /// CreateMetadataSlot - Insert the specified MDNode* into the slot table. 7920b57cec5SDimitry Andric void CreateMetadataSlot(const MDNode *N); 7930b57cec5SDimitry Andric 7940b57cec5SDimitry Andric /// CreateFunctionSlot - Insert the specified Value* into the slot table. 7950b57cec5SDimitry Andric void CreateFunctionSlot(const Value *V); 7960b57cec5SDimitry Andric 7970b57cec5SDimitry Andric /// Insert the specified AttributeSet into the slot table. 7980b57cec5SDimitry Andric void CreateAttributeSetSlot(AttributeSet AS); 7990b57cec5SDimitry Andric 8000b57cec5SDimitry Andric inline void CreateModulePathSlot(StringRef Path); 8010b57cec5SDimitry Andric void CreateGUIDSlot(GlobalValue::GUID GUID); 8020b57cec5SDimitry Andric void CreateTypeIdSlot(StringRef Id); 8030b57cec5SDimitry Andric 8040b57cec5SDimitry Andric /// Add all of the module level global variables (and their initializers) 8050b57cec5SDimitry Andric /// and function declarations, but not the contents of those functions. 8060b57cec5SDimitry Andric void processModule(); 8075ffd83dbSDimitry Andric // Returns number of allocated slots 8085ffd83dbSDimitry Andric int processIndex(); 8090b57cec5SDimitry Andric 8100b57cec5SDimitry Andric /// Add all of the functions arguments, basic blocks, and instructions. 8110b57cec5SDimitry Andric void processFunction(); 8120b57cec5SDimitry Andric 8130b57cec5SDimitry Andric /// Add the metadata directly attached to a GlobalObject. 8140b57cec5SDimitry Andric void processGlobalObjectMetadata(const GlobalObject &GO); 8150b57cec5SDimitry Andric 8160b57cec5SDimitry Andric /// Add all of the metadata from a function. 8170b57cec5SDimitry Andric void processFunctionMetadata(const Function &F); 8180b57cec5SDimitry Andric 8190b57cec5SDimitry Andric /// Add all of the metadata from an instruction. 8200b57cec5SDimitry Andric void processInstructionMetadata(const Instruction &I); 8210b57cec5SDimitry Andric }; 8220b57cec5SDimitry Andric 8230b57cec5SDimitry Andric } // end namespace llvm 8240b57cec5SDimitry Andric 8250b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M, 8260b57cec5SDimitry Andric const Function *F) 8270b57cec5SDimitry Andric : M(M), F(F), Machine(&Machine) {} 8280b57cec5SDimitry Andric 8290b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(const Module *M, 8300b57cec5SDimitry Andric bool ShouldInitializeAllMetadata) 8310b57cec5SDimitry Andric : ShouldCreateStorage(M), 8320b57cec5SDimitry Andric ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {} 8330b57cec5SDimitry Andric 8340b57cec5SDimitry Andric ModuleSlotTracker::~ModuleSlotTracker() = default; 8350b57cec5SDimitry Andric 8360b57cec5SDimitry Andric SlotTracker *ModuleSlotTracker::getMachine() { 8370b57cec5SDimitry Andric if (!ShouldCreateStorage) 8380b57cec5SDimitry Andric return Machine; 8390b57cec5SDimitry Andric 8400b57cec5SDimitry Andric ShouldCreateStorage = false; 8410b57cec5SDimitry Andric MachineStorage = 8428bcb0991SDimitry Andric std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata); 8430b57cec5SDimitry Andric Machine = MachineStorage.get(); 844fe6060f1SDimitry Andric if (ProcessModuleHookFn) 845fe6060f1SDimitry Andric Machine->setProcessHook(ProcessModuleHookFn); 846fe6060f1SDimitry Andric if (ProcessFunctionHookFn) 847fe6060f1SDimitry Andric Machine->setProcessHook(ProcessFunctionHookFn); 8480b57cec5SDimitry Andric return Machine; 8490b57cec5SDimitry Andric } 8500b57cec5SDimitry Andric 8510b57cec5SDimitry Andric void ModuleSlotTracker::incorporateFunction(const Function &F) { 8520b57cec5SDimitry Andric // Using getMachine() may lazily create the slot tracker. 8530b57cec5SDimitry Andric if (!getMachine()) 8540b57cec5SDimitry Andric return; 8550b57cec5SDimitry Andric 8560b57cec5SDimitry Andric // Nothing to do if this is the right function already. 8570b57cec5SDimitry Andric if (this->F == &F) 8580b57cec5SDimitry Andric return; 8590b57cec5SDimitry Andric if (this->F) 8600b57cec5SDimitry Andric Machine->purgeFunction(); 8610b57cec5SDimitry Andric Machine->incorporateFunction(&F); 8620b57cec5SDimitry Andric this->F = &F; 8630b57cec5SDimitry Andric } 8640b57cec5SDimitry Andric 8650b57cec5SDimitry Andric int ModuleSlotTracker::getLocalSlot(const Value *V) { 8660b57cec5SDimitry Andric assert(F && "No function incorporated"); 8670b57cec5SDimitry Andric return Machine->getLocalSlot(V); 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric 870fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook( 871fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 872fe6060f1SDimitry Andric Fn) { 873fe6060f1SDimitry Andric ProcessModuleHookFn = Fn; 874fe6060f1SDimitry Andric } 875fe6060f1SDimitry Andric 876fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook( 877fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 878fe6060f1SDimitry Andric Fn) { 879fe6060f1SDimitry Andric ProcessFunctionHookFn = Fn; 880fe6060f1SDimitry Andric } 881fe6060f1SDimitry Andric 8820b57cec5SDimitry Andric static SlotTracker *createSlotTracker(const Value *V) { 8830b57cec5SDimitry Andric if (const Argument *FA = dyn_cast<Argument>(V)) 8840b57cec5SDimitry Andric return new SlotTracker(FA->getParent()); 8850b57cec5SDimitry Andric 8860b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) 8870b57cec5SDimitry Andric if (I->getParent()) 8880b57cec5SDimitry Andric return new SlotTracker(I->getParent()->getParent()); 8890b57cec5SDimitry Andric 8900b57cec5SDimitry Andric if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) 8910b57cec5SDimitry Andric return new SlotTracker(BB->getParent()); 8920b57cec5SDimitry Andric 8930b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 8940b57cec5SDimitry Andric return new SlotTracker(GV->getParent()); 8950b57cec5SDimitry Andric 8960b57cec5SDimitry Andric if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) 8970b57cec5SDimitry Andric return new SlotTracker(GA->getParent()); 8980b57cec5SDimitry Andric 8990b57cec5SDimitry Andric if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V)) 9000b57cec5SDimitry Andric return new SlotTracker(GIF->getParent()); 9010b57cec5SDimitry Andric 9020b57cec5SDimitry Andric if (const Function *Func = dyn_cast<Function>(V)) 9030b57cec5SDimitry Andric return new SlotTracker(Func); 9040b57cec5SDimitry Andric 9050b57cec5SDimitry Andric return nullptr; 9060b57cec5SDimitry Andric } 9070b57cec5SDimitry Andric 9080b57cec5SDimitry Andric #if 0 9090b57cec5SDimitry Andric #define ST_DEBUG(X) dbgs() << X 9100b57cec5SDimitry Andric #else 9110b57cec5SDimitry Andric #define ST_DEBUG(X) 9120b57cec5SDimitry Andric #endif 9130b57cec5SDimitry Andric 9140b57cec5SDimitry Andric // Module level constructor. Causes the contents of the Module (sans functions) 9150b57cec5SDimitry Andric // to be added to the slot table. 9160b57cec5SDimitry Andric SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata) 9170b57cec5SDimitry Andric : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {} 9180b57cec5SDimitry Andric 9190b57cec5SDimitry Andric // Function level constructor. Causes the contents of the Module and the one 9200b57cec5SDimitry Andric // function provided to be added to the slot table. 9210b57cec5SDimitry Andric SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata) 9220b57cec5SDimitry Andric : TheModule(F ? F->getParent() : nullptr), TheFunction(F), 9230b57cec5SDimitry Andric ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {} 9240b57cec5SDimitry Andric 9250b57cec5SDimitry Andric SlotTracker::SlotTracker(const ModuleSummaryIndex *Index) 9260b57cec5SDimitry Andric : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {} 9270b57cec5SDimitry Andric 9280b57cec5SDimitry Andric inline void SlotTracker::initializeIfNeeded() { 9290b57cec5SDimitry Andric if (TheModule) { 9300b57cec5SDimitry Andric processModule(); 9310b57cec5SDimitry Andric TheModule = nullptr; ///< Prevent re-processing next time we're called. 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric 9340b57cec5SDimitry Andric if (TheFunction && !FunctionProcessed) 9350b57cec5SDimitry Andric processFunction(); 9360b57cec5SDimitry Andric } 9370b57cec5SDimitry Andric 9385ffd83dbSDimitry Andric int SlotTracker::initializeIndexIfNeeded() { 9390b57cec5SDimitry Andric if (!TheIndex) 9405ffd83dbSDimitry Andric return 0; 9415ffd83dbSDimitry Andric int NumSlots = processIndex(); 9420b57cec5SDimitry Andric TheIndex = nullptr; ///< Prevent re-processing next time we're called. 9435ffd83dbSDimitry Andric return NumSlots; 9440b57cec5SDimitry Andric } 9450b57cec5SDimitry Andric 9460b57cec5SDimitry Andric // Iterate through all the global variables, functions, and global 9470b57cec5SDimitry Andric // variable initializers and create slots for them. 9480b57cec5SDimitry Andric void SlotTracker::processModule() { 9490b57cec5SDimitry Andric ST_DEBUG("begin processModule!\n"); 9500b57cec5SDimitry Andric 9510b57cec5SDimitry Andric // Add all of the unnamed global variables to the value table. 9520b57cec5SDimitry Andric for (const GlobalVariable &Var : TheModule->globals()) { 9530b57cec5SDimitry Andric if (!Var.hasName()) 9540b57cec5SDimitry Andric CreateModuleSlot(&Var); 9550b57cec5SDimitry Andric processGlobalObjectMetadata(Var); 9560b57cec5SDimitry Andric auto Attrs = Var.getAttributes(); 9570b57cec5SDimitry Andric if (Attrs.hasAttributes()) 9580b57cec5SDimitry Andric CreateAttributeSetSlot(Attrs); 9590b57cec5SDimitry Andric } 9600b57cec5SDimitry Andric 9610b57cec5SDimitry Andric for (const GlobalAlias &A : TheModule->aliases()) { 9620b57cec5SDimitry Andric if (!A.hasName()) 9630b57cec5SDimitry Andric CreateModuleSlot(&A); 9640b57cec5SDimitry Andric } 9650b57cec5SDimitry Andric 9660b57cec5SDimitry Andric for (const GlobalIFunc &I : TheModule->ifuncs()) { 9670b57cec5SDimitry Andric if (!I.hasName()) 9680b57cec5SDimitry Andric CreateModuleSlot(&I); 9690b57cec5SDimitry Andric } 9700b57cec5SDimitry Andric 9710b57cec5SDimitry Andric // Add metadata used by named metadata. 9720b57cec5SDimitry Andric for (const NamedMDNode &NMD : TheModule->named_metadata()) { 9730b57cec5SDimitry Andric for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) 9740b57cec5SDimitry Andric CreateMetadataSlot(NMD.getOperand(i)); 9750b57cec5SDimitry Andric } 9760b57cec5SDimitry Andric 9770b57cec5SDimitry Andric for (const Function &F : *TheModule) { 9780b57cec5SDimitry Andric if (!F.hasName()) 9790b57cec5SDimitry Andric // Add all the unnamed functions to the table. 9800b57cec5SDimitry Andric CreateModuleSlot(&F); 9810b57cec5SDimitry Andric 9820b57cec5SDimitry Andric if (ShouldInitializeAllMetadata) 9830b57cec5SDimitry Andric processFunctionMetadata(F); 9840b57cec5SDimitry Andric 9850b57cec5SDimitry Andric // Add all the function attributes to the table. 9860b57cec5SDimitry Andric // FIXME: Add attributes of other objects? 987349cc55cSDimitry Andric AttributeSet FnAttrs = F.getAttributes().getFnAttrs(); 9880b57cec5SDimitry Andric if (FnAttrs.hasAttributes()) 9890b57cec5SDimitry Andric CreateAttributeSetSlot(FnAttrs); 9900b57cec5SDimitry Andric } 9910b57cec5SDimitry Andric 992fe6060f1SDimitry Andric if (ProcessModuleHookFn) 993fe6060f1SDimitry Andric ProcessModuleHookFn(this, TheModule, ShouldInitializeAllMetadata); 994fe6060f1SDimitry Andric 9950b57cec5SDimitry Andric ST_DEBUG("end processModule!\n"); 9960b57cec5SDimitry Andric } 9970b57cec5SDimitry Andric 9980b57cec5SDimitry Andric // Process the arguments, basic blocks, and instructions of a function. 9990b57cec5SDimitry Andric void SlotTracker::processFunction() { 10000b57cec5SDimitry Andric ST_DEBUG("begin processFunction!\n"); 10010b57cec5SDimitry Andric fNext = 0; 10020b57cec5SDimitry Andric 10030b57cec5SDimitry Andric // Process function metadata if it wasn't hit at the module-level. 10040b57cec5SDimitry Andric if (!ShouldInitializeAllMetadata) 10050b57cec5SDimitry Andric processFunctionMetadata(*TheFunction); 10060b57cec5SDimitry Andric 10070b57cec5SDimitry Andric // Add all the function arguments with no names. 10080b57cec5SDimitry Andric for(Function::const_arg_iterator AI = TheFunction->arg_begin(), 10090b57cec5SDimitry Andric AE = TheFunction->arg_end(); AI != AE; ++AI) 10100b57cec5SDimitry Andric if (!AI->hasName()) 10110b57cec5SDimitry Andric CreateFunctionSlot(&*AI); 10120b57cec5SDimitry Andric 10130b57cec5SDimitry Andric ST_DEBUG("Inserting Instructions:\n"); 10140b57cec5SDimitry Andric 10150b57cec5SDimitry Andric // Add all of the basic blocks and instructions with no names. 10160b57cec5SDimitry Andric for (auto &BB : *TheFunction) { 10170b57cec5SDimitry Andric if (!BB.hasName()) 10180b57cec5SDimitry Andric CreateFunctionSlot(&BB); 10190b57cec5SDimitry Andric 10200b57cec5SDimitry Andric for (auto &I : BB) { 10210b57cec5SDimitry Andric if (!I.getType()->isVoidTy() && !I.hasName()) 10220b57cec5SDimitry Andric CreateFunctionSlot(&I); 10230b57cec5SDimitry Andric 10240b57cec5SDimitry Andric // We allow direct calls to any llvm.foo function here, because the 10250b57cec5SDimitry Andric // target may not be linked into the optimizer. 10260b57cec5SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(&I)) { 10270b57cec5SDimitry Andric // Add all the call attributes to the table. 1028349cc55cSDimitry Andric AttributeSet Attrs = Call->getAttributes().getFnAttrs(); 10290b57cec5SDimitry Andric if (Attrs.hasAttributes()) 10300b57cec5SDimitry Andric CreateAttributeSetSlot(Attrs); 10310b57cec5SDimitry Andric } 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric } 10340b57cec5SDimitry Andric 1035fe6060f1SDimitry Andric if (ProcessFunctionHookFn) 1036fe6060f1SDimitry Andric ProcessFunctionHookFn(this, TheFunction, ShouldInitializeAllMetadata); 1037fe6060f1SDimitry Andric 10380b57cec5SDimitry Andric FunctionProcessed = true; 10390b57cec5SDimitry Andric 10400b57cec5SDimitry Andric ST_DEBUG("end processFunction!\n"); 10410b57cec5SDimitry Andric } 10420b57cec5SDimitry Andric 10430b57cec5SDimitry Andric // Iterate through all the GUID in the index and create slots for them. 10445ffd83dbSDimitry Andric int SlotTracker::processIndex() { 10450b57cec5SDimitry Andric ST_DEBUG("begin processIndex!\n"); 10460b57cec5SDimitry Andric assert(TheIndex); 10470b57cec5SDimitry Andric 10480b57cec5SDimitry Andric // The first block of slots are just the module ids, which start at 0 and are 10490b57cec5SDimitry Andric // assigned consecutively. Since the StringMap iteration order isn't 10500b57cec5SDimitry Andric // guaranteed, use a std::map to order by module ID before assigning slots. 10510b57cec5SDimitry Andric std::map<uint64_t, StringRef> ModuleIdToPathMap; 10520b57cec5SDimitry Andric for (auto &ModPath : TheIndex->modulePaths()) 10530b57cec5SDimitry Andric ModuleIdToPathMap[ModPath.second.first] = ModPath.first(); 10540b57cec5SDimitry Andric for (auto &ModPair : ModuleIdToPathMap) 10550b57cec5SDimitry Andric CreateModulePathSlot(ModPair.second); 10560b57cec5SDimitry Andric 10570b57cec5SDimitry Andric // Start numbering the GUIDs after the module ids. 10580b57cec5SDimitry Andric GUIDNext = ModulePathNext; 10590b57cec5SDimitry Andric 10600b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) 10610b57cec5SDimitry Andric CreateGUIDSlot(GlobalList.first); 10620b57cec5SDimitry Andric 10635ffd83dbSDimitry Andric for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) 10645ffd83dbSDimitry Andric CreateGUIDSlot(GlobalValue::getGUID(TId.first)); 10655ffd83dbSDimitry Andric 10660b57cec5SDimitry Andric // Start numbering the TypeIds after the GUIDs. 10670b57cec5SDimitry Andric TypeIdNext = GUIDNext; 1068fe6060f1SDimitry Andric for (const auto &TID : TheIndex->typeIds()) 1069fe6060f1SDimitry Andric CreateTypeIdSlot(TID.second.first); 10700b57cec5SDimitry Andric 10710b57cec5SDimitry Andric ST_DEBUG("end processIndex!\n"); 10725ffd83dbSDimitry Andric return TypeIdNext; 10730b57cec5SDimitry Andric } 10740b57cec5SDimitry Andric 10750b57cec5SDimitry Andric void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) { 10760b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 10770b57cec5SDimitry Andric GO.getAllMetadata(MDs); 10780b57cec5SDimitry Andric for (auto &MD : MDs) 10790b57cec5SDimitry Andric CreateMetadataSlot(MD.second); 10800b57cec5SDimitry Andric } 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric void SlotTracker::processFunctionMetadata(const Function &F) { 10830b57cec5SDimitry Andric processGlobalObjectMetadata(F); 10840b57cec5SDimitry Andric for (auto &BB : F) { 10850b57cec5SDimitry Andric for (auto &I : BB) 10860b57cec5SDimitry Andric processInstructionMetadata(I); 10870b57cec5SDimitry Andric } 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric 10900b57cec5SDimitry Andric void SlotTracker::processInstructionMetadata(const Instruction &I) { 10910b57cec5SDimitry Andric // Process metadata used directly by intrinsics. 10920b57cec5SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I)) 10930b57cec5SDimitry Andric if (Function *F = CI->getCalledFunction()) 10940b57cec5SDimitry Andric if (F->isIntrinsic()) 10950b57cec5SDimitry Andric for (auto &Op : I.operands()) 10960b57cec5SDimitry Andric if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op)) 10970b57cec5SDimitry Andric if (MDNode *N = dyn_cast<MDNode>(V->getMetadata())) 10980b57cec5SDimitry Andric CreateMetadataSlot(N); 10990b57cec5SDimitry Andric 11000b57cec5SDimitry Andric // Process metadata attached to this instruction. 11010b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 11020b57cec5SDimitry Andric I.getAllMetadata(MDs); 11030b57cec5SDimitry Andric for (auto &MD : MDs) 11040b57cec5SDimitry Andric CreateMetadataSlot(MD.second); 11050b57cec5SDimitry Andric } 11060b57cec5SDimitry Andric 11070b57cec5SDimitry Andric /// Clean up after incorporating a function. This is the only way to get out of 11080b57cec5SDimitry Andric /// the function incorporation state that affects get*Slot/Create*Slot. Function 11090b57cec5SDimitry Andric /// incorporation state is indicated by TheFunction != 0. 11100b57cec5SDimitry Andric void SlotTracker::purgeFunction() { 11110b57cec5SDimitry Andric ST_DEBUG("begin purgeFunction!\n"); 11120b57cec5SDimitry Andric fMap.clear(); // Simply discard the function level map 11130b57cec5SDimitry Andric TheFunction = nullptr; 11140b57cec5SDimitry Andric FunctionProcessed = false; 11150b57cec5SDimitry Andric ST_DEBUG("end purgeFunction!\n"); 11160b57cec5SDimitry Andric } 11170b57cec5SDimitry Andric 11180b57cec5SDimitry Andric /// getGlobalSlot - Get the slot number of a global value. 11190b57cec5SDimitry Andric int SlotTracker::getGlobalSlot(const GlobalValue *V) { 11200b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11210b57cec5SDimitry Andric initializeIfNeeded(); 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric // Find the value in the module map 11240b57cec5SDimitry Andric ValueMap::iterator MI = mMap.find(V); 11250b57cec5SDimitry Andric return MI == mMap.end() ? -1 : (int)MI->second; 11260b57cec5SDimitry Andric } 11270b57cec5SDimitry Andric 1128fe6060f1SDimitry Andric void SlotTracker::setProcessHook( 1129fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 1130fe6060f1SDimitry Andric Fn) { 1131fe6060f1SDimitry Andric ProcessModuleHookFn = Fn; 1132fe6060f1SDimitry Andric } 1133fe6060f1SDimitry Andric 1134fe6060f1SDimitry Andric void SlotTracker::setProcessHook( 1135fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 1136fe6060f1SDimitry Andric Fn) { 1137fe6060f1SDimitry Andric ProcessFunctionHookFn = Fn; 1138fe6060f1SDimitry Andric } 1139fe6060f1SDimitry Andric 1140fe6060f1SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode. 1141fe6060f1SDimitry Andric void SlotTracker::createMetadataSlot(const MDNode *N) { CreateMetadataSlot(N); } 1142fe6060f1SDimitry Andric 11430b57cec5SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode. 11440b57cec5SDimitry Andric int SlotTracker::getMetadataSlot(const MDNode *N) { 11450b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11460b57cec5SDimitry Andric initializeIfNeeded(); 11470b57cec5SDimitry Andric 11480b57cec5SDimitry Andric // Find the MDNode in the module map 11490b57cec5SDimitry Andric mdn_iterator MI = mdnMap.find(N); 11500b57cec5SDimitry Andric return MI == mdnMap.end() ? -1 : (int)MI->second; 11510b57cec5SDimitry Andric } 11520b57cec5SDimitry Andric 11530b57cec5SDimitry Andric /// getLocalSlot - Get the slot number for a value that is local to a function. 11540b57cec5SDimitry Andric int SlotTracker::getLocalSlot(const Value *V) { 11550b57cec5SDimitry Andric assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!"); 11560b57cec5SDimitry Andric 11570b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11580b57cec5SDimitry Andric initializeIfNeeded(); 11590b57cec5SDimitry Andric 11600b57cec5SDimitry Andric ValueMap::iterator FI = fMap.find(V); 11610b57cec5SDimitry Andric return FI == fMap.end() ? -1 : (int)FI->second; 11620b57cec5SDimitry Andric } 11630b57cec5SDimitry Andric 11640b57cec5SDimitry Andric int SlotTracker::getAttributeGroupSlot(AttributeSet AS) { 11650b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11660b57cec5SDimitry Andric initializeIfNeeded(); 11670b57cec5SDimitry Andric 11680b57cec5SDimitry Andric // Find the AttributeSet in the module map. 11690b57cec5SDimitry Andric as_iterator AI = asMap.find(AS); 11700b57cec5SDimitry Andric return AI == asMap.end() ? -1 : (int)AI->second; 11710b57cec5SDimitry Andric } 11720b57cec5SDimitry Andric 11730b57cec5SDimitry Andric int SlotTracker::getModulePathSlot(StringRef Path) { 11740b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11750b57cec5SDimitry Andric initializeIndexIfNeeded(); 11760b57cec5SDimitry Andric 11770b57cec5SDimitry Andric // Find the Module path in the map 11780b57cec5SDimitry Andric auto I = ModulePathMap.find(Path); 11790b57cec5SDimitry Andric return I == ModulePathMap.end() ? -1 : (int)I->second; 11800b57cec5SDimitry Andric } 11810b57cec5SDimitry Andric 11820b57cec5SDimitry Andric int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) { 11830b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11840b57cec5SDimitry Andric initializeIndexIfNeeded(); 11850b57cec5SDimitry Andric 11860b57cec5SDimitry Andric // Find the GUID in the map 11870b57cec5SDimitry Andric guid_iterator I = GUIDMap.find(GUID); 11880b57cec5SDimitry Andric return I == GUIDMap.end() ? -1 : (int)I->second; 11890b57cec5SDimitry Andric } 11900b57cec5SDimitry Andric 11910b57cec5SDimitry Andric int SlotTracker::getTypeIdSlot(StringRef Id) { 11920b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11930b57cec5SDimitry Andric initializeIndexIfNeeded(); 11940b57cec5SDimitry Andric 11950b57cec5SDimitry Andric // Find the TypeId string in the map 11960b57cec5SDimitry Andric auto I = TypeIdMap.find(Id); 11970b57cec5SDimitry Andric return I == TypeIdMap.end() ? -1 : (int)I->second; 11980b57cec5SDimitry Andric } 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. 12010b57cec5SDimitry Andric void SlotTracker::CreateModuleSlot(const GlobalValue *V) { 12020b57cec5SDimitry Andric assert(V && "Can't insert a null Value into SlotTracker!"); 12030b57cec5SDimitry Andric assert(!V->getType()->isVoidTy() && "Doesn't need a slot!"); 12040b57cec5SDimitry Andric assert(!V->hasName() && "Doesn't need a slot!"); 12050b57cec5SDimitry Andric 12060b57cec5SDimitry Andric unsigned DestSlot = mNext++; 12070b57cec5SDimitry Andric mMap[V] = DestSlot; 12080b57cec5SDimitry Andric 12090b57cec5SDimitry Andric ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << 12100b57cec5SDimitry Andric DestSlot << " ["); 12110b57cec5SDimitry Andric // G = Global, F = Function, A = Alias, I = IFunc, o = other 12120b57cec5SDimitry Andric ST_DEBUG((isa<GlobalVariable>(V) ? 'G' : 12130b57cec5SDimitry Andric (isa<Function>(V) ? 'F' : 12140b57cec5SDimitry Andric (isa<GlobalAlias>(V) ? 'A' : 12150b57cec5SDimitry Andric (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n"); 12160b57cec5SDimitry Andric } 12170b57cec5SDimitry Andric 12180b57cec5SDimitry Andric /// CreateSlot - Create a new slot for the specified value if it has no name. 12190b57cec5SDimitry Andric void SlotTracker::CreateFunctionSlot(const Value *V) { 12200b57cec5SDimitry Andric assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!"); 12210b57cec5SDimitry Andric 12220b57cec5SDimitry Andric unsigned DestSlot = fNext++; 12230b57cec5SDimitry Andric fMap[V] = DestSlot; 12240b57cec5SDimitry Andric 12250b57cec5SDimitry Andric // G = Global, F = Function, o = other 12260b57cec5SDimitry Andric ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << 12270b57cec5SDimitry Andric DestSlot << " [o]\n"); 12280b57cec5SDimitry Andric } 12290b57cec5SDimitry Andric 12300b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified MDNode* into the slot table. 12310b57cec5SDimitry Andric void SlotTracker::CreateMetadataSlot(const MDNode *N) { 12320b57cec5SDimitry Andric assert(N && "Can't insert a null Value into SlotTracker!"); 12330b57cec5SDimitry Andric 1234fe6060f1SDimitry Andric // Don't make slots for DIExpressions or DIArgLists. We just print them inline 1235fe6060f1SDimitry Andric // everywhere. 1236fe6060f1SDimitry Andric if (isa<DIExpression>(N) || isa<DIArgList>(N)) 12370b57cec5SDimitry Andric return; 12380b57cec5SDimitry Andric 12390b57cec5SDimitry Andric unsigned DestSlot = mdnNext; 12400b57cec5SDimitry Andric if (!mdnMap.insert(std::make_pair(N, DestSlot)).second) 12410b57cec5SDimitry Andric return; 12420b57cec5SDimitry Andric ++mdnNext; 12430b57cec5SDimitry Andric 12440b57cec5SDimitry Andric // Recursively add any MDNodes referenced by operands. 12450b57cec5SDimitry Andric for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 12460b57cec5SDimitry Andric if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i))) 12470b57cec5SDimitry Andric CreateMetadataSlot(Op); 12480b57cec5SDimitry Andric } 12490b57cec5SDimitry Andric 12500b57cec5SDimitry Andric void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) { 12510b57cec5SDimitry Andric assert(AS.hasAttributes() && "Doesn't need a slot!"); 12520b57cec5SDimitry Andric 12530b57cec5SDimitry Andric as_iterator I = asMap.find(AS); 12540b57cec5SDimitry Andric if (I != asMap.end()) 12550b57cec5SDimitry Andric return; 12560b57cec5SDimitry Andric 12570b57cec5SDimitry Andric unsigned DestSlot = asNext++; 12580b57cec5SDimitry Andric asMap[AS] = DestSlot; 12590b57cec5SDimitry Andric } 12600b57cec5SDimitry Andric 12610b57cec5SDimitry Andric /// Create a new slot for the specified Module 12620b57cec5SDimitry Andric void SlotTracker::CreateModulePathSlot(StringRef Path) { 12630b57cec5SDimitry Andric ModulePathMap[Path] = ModulePathNext++; 12640b57cec5SDimitry Andric } 12650b57cec5SDimitry Andric 12660b57cec5SDimitry Andric /// Create a new slot for the specified GUID 12670b57cec5SDimitry Andric void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) { 12680b57cec5SDimitry Andric GUIDMap[GUID] = GUIDNext++; 12690b57cec5SDimitry Andric } 12700b57cec5SDimitry Andric 12710b57cec5SDimitry Andric /// Create a new slot for the specified Id 12720b57cec5SDimitry Andric void SlotTracker::CreateTypeIdSlot(StringRef Id) { 12730b57cec5SDimitry Andric TypeIdMap[Id] = TypeIdNext++; 12740b57cec5SDimitry Andric } 12750b57cec5SDimitry Andric 1276349cc55cSDimitry Andric namespace { 1277349cc55cSDimitry Andric /// Common instances used by most of the printer functions. 1278349cc55cSDimitry Andric struct AsmWriterContext { 1279349cc55cSDimitry Andric TypePrinting *TypePrinter = nullptr; 1280349cc55cSDimitry Andric SlotTracker *Machine = nullptr; 1281349cc55cSDimitry Andric const Module *Context = nullptr; 1282349cc55cSDimitry Andric 1283349cc55cSDimitry Andric AsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M = nullptr) 1284349cc55cSDimitry Andric : TypePrinter(TP), Machine(ST), Context(M) {} 1285349cc55cSDimitry Andric 1286349cc55cSDimitry Andric static AsmWriterContext &getEmpty() { 1287349cc55cSDimitry Andric static AsmWriterContext EmptyCtx(nullptr, nullptr); 1288349cc55cSDimitry Andric return EmptyCtx; 1289349cc55cSDimitry Andric } 1290349cc55cSDimitry Andric 1291349cc55cSDimitry Andric /// A callback that will be triggered when the underlying printer 1292349cc55cSDimitry Andric /// prints a Metadata as operand. 1293349cc55cSDimitry Andric virtual void onWriteMetadataAsOperand(const Metadata *) {} 1294349cc55cSDimitry Andric 1295349cc55cSDimitry Andric virtual ~AsmWriterContext() {} 1296349cc55cSDimitry Andric }; 1297349cc55cSDimitry Andric } // end anonymous namespace 1298349cc55cSDimitry Andric 12990b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13000b57cec5SDimitry Andric // AsmWriter Implementation 13010b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13020b57cec5SDimitry Andric 13030b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, 1304349cc55cSDimitry Andric AsmWriterContext &WriterCtx); 13050b57cec5SDimitry Andric 13060b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, 1307349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 13080b57cec5SDimitry Andric bool FromValue = false); 13090b57cec5SDimitry Andric 13100b57cec5SDimitry Andric static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { 1311*4824e7fdSDimitry Andric if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) 1312*4824e7fdSDimitry Andric Out << FPO->getFastMathFlags(); 13130b57cec5SDimitry Andric 13140b57cec5SDimitry Andric if (const OverflowingBinaryOperator *OBO = 13150b57cec5SDimitry Andric dyn_cast<OverflowingBinaryOperator>(U)) { 13160b57cec5SDimitry Andric if (OBO->hasNoUnsignedWrap()) 13170b57cec5SDimitry Andric Out << " nuw"; 13180b57cec5SDimitry Andric if (OBO->hasNoSignedWrap()) 13190b57cec5SDimitry Andric Out << " nsw"; 13200b57cec5SDimitry Andric } else if (const PossiblyExactOperator *Div = 13210b57cec5SDimitry Andric dyn_cast<PossiblyExactOperator>(U)) { 13220b57cec5SDimitry Andric if (Div->isExact()) 13230b57cec5SDimitry Andric Out << " exact"; 13240b57cec5SDimitry Andric } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) { 13250b57cec5SDimitry Andric if (GEP->isInBounds()) 13260b57cec5SDimitry Andric Out << " inbounds"; 13270b57cec5SDimitry Andric } 13280b57cec5SDimitry Andric } 13290b57cec5SDimitry Andric 13300b57cec5SDimitry Andric static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, 1331349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 13320b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 13330b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(1)) { 13340b57cec5SDimitry Andric Out << (CI->getZExtValue() ? "true" : "false"); 13350b57cec5SDimitry Andric return; 13360b57cec5SDimitry Andric } 13370b57cec5SDimitry Andric Out << CI->getValue(); 13380b57cec5SDimitry Andric return; 13390b57cec5SDimitry Andric } 13400b57cec5SDimitry Andric 13410b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 13420b57cec5SDimitry Andric const APFloat &APF = CFP->getValueAPF(); 13430b57cec5SDimitry Andric if (&APF.getSemantics() == &APFloat::IEEEsingle() || 13440b57cec5SDimitry Andric &APF.getSemantics() == &APFloat::IEEEdouble()) { 13450b57cec5SDimitry Andric // We would like to output the FP constant value in exponential notation, 13460b57cec5SDimitry Andric // but we cannot do this if doing so will lose precision. Check here to 13470b57cec5SDimitry Andric // make sure that we only output it in exponential format if we can parse 13480b57cec5SDimitry Andric // the value back and get the same value. 13490b57cec5SDimitry Andric // 13500b57cec5SDimitry Andric bool ignored; 13510b57cec5SDimitry Andric bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble(); 13520b57cec5SDimitry Andric bool isInf = APF.isInfinity(); 13530b57cec5SDimitry Andric bool isNaN = APF.isNaN(); 13540b57cec5SDimitry Andric if (!isInf && !isNaN) { 1355fe6060f1SDimitry Andric double Val = APF.convertToDouble(); 13560b57cec5SDimitry Andric SmallString<128> StrVal; 13570b57cec5SDimitry Andric APF.toString(StrVal, 6, 0, false); 13580b57cec5SDimitry Andric // Check to make sure that the stringized number is not some string like 13590b57cec5SDimitry Andric // "Inf" or NaN, that atof will accept, but the lexer will not. Check 13600b57cec5SDimitry Andric // that the string matches the "[-+]?[0-9]" regex. 13610b57cec5SDimitry Andric // 1362e8d8bef9SDimitry Andric assert((isDigit(StrVal[0]) || ((StrVal[0] == '-' || StrVal[0] == '+') && 1363e8d8bef9SDimitry Andric isDigit(StrVal[1]))) && 13640b57cec5SDimitry Andric "[-+]?[0-9] regex does not match!"); 13650b57cec5SDimitry Andric // Reparse stringized version! 13660b57cec5SDimitry Andric if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) { 13670b57cec5SDimitry Andric Out << StrVal; 13680b57cec5SDimitry Andric return; 13690b57cec5SDimitry Andric } 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric // Otherwise we could not reparse it to exactly the same value, so we must 13720b57cec5SDimitry Andric // output the string in hexadecimal format! Note that loading and storing 13730b57cec5SDimitry Andric // floating point types changes the bits of NaNs on some hosts, notably 13740b57cec5SDimitry Andric // x86, so we must not use these types. 13750b57cec5SDimitry Andric static_assert(sizeof(double) == sizeof(uint64_t), 13760b57cec5SDimitry Andric "assuming that double is 64 bits!"); 13770b57cec5SDimitry Andric APFloat apf = APF; 13780b57cec5SDimitry Andric // Floats are represented in ASCII IR as double, convert. 1379e8d8bef9SDimitry Andric // FIXME: We should allow 32-bit hex float and remove this. 1380e8d8bef9SDimitry Andric if (!isDouble) { 1381e8d8bef9SDimitry Andric // A signaling NaN is quieted on conversion, so we need to recreate the 1382e8d8bef9SDimitry Andric // expected value after convert (quiet bit of the payload is clear). 1383e8d8bef9SDimitry Andric bool IsSNAN = apf.isSignaling(); 13840b57cec5SDimitry Andric apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, 13850b57cec5SDimitry Andric &ignored); 1386e8d8bef9SDimitry Andric if (IsSNAN) { 1387e8d8bef9SDimitry Andric APInt Payload = apf.bitcastToAPInt(); 1388e8d8bef9SDimitry Andric apf = APFloat::getSNaN(APFloat::IEEEdouble(), apf.isNegative(), 1389e8d8bef9SDimitry Andric &Payload); 1390e8d8bef9SDimitry Andric } 1391e8d8bef9SDimitry Andric } 13920b57cec5SDimitry Andric Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true); 13930b57cec5SDimitry Andric return; 13940b57cec5SDimitry Andric } 13950b57cec5SDimitry Andric 13965ffd83dbSDimitry Andric // Either half, bfloat or some form of long double. 13970b57cec5SDimitry Andric // These appear as a magic letter identifying the type, then a 13980b57cec5SDimitry Andric // fixed number of hex digits. 13990b57cec5SDimitry Andric Out << "0x"; 14000b57cec5SDimitry Andric APInt API = APF.bitcastToAPInt(); 14010b57cec5SDimitry Andric if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) { 14020b57cec5SDimitry Andric Out << 'K'; 14030b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4, 14040b57cec5SDimitry Andric /*Upper=*/true); 14050b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14060b57cec5SDimitry Andric /*Upper=*/true); 14070b57cec5SDimitry Andric return; 14080b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::IEEEquad()) { 14090b57cec5SDimitry Andric Out << 'L'; 14100b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14110b57cec5SDimitry Andric /*Upper=*/true); 14120b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16, 14130b57cec5SDimitry Andric /*Upper=*/true); 14140b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) { 14150b57cec5SDimitry Andric Out << 'M'; 14160b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14170b57cec5SDimitry Andric /*Upper=*/true); 14180b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16, 14190b57cec5SDimitry Andric /*Upper=*/true); 14200b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) { 14210b57cec5SDimitry Andric Out << 'H'; 14220b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getZExtValue(), 4, 14230b57cec5SDimitry Andric /*Upper=*/true); 14245ffd83dbSDimitry Andric } else if (&APF.getSemantics() == &APFloat::BFloat()) { 14255ffd83dbSDimitry Andric Out << 'R'; 14265ffd83dbSDimitry Andric Out << format_hex_no_prefix(API.getZExtValue(), 4, 14275ffd83dbSDimitry Andric /*Upper=*/true); 14280b57cec5SDimitry Andric } else 14290b57cec5SDimitry Andric llvm_unreachable("Unsupported floating point type"); 14300b57cec5SDimitry Andric return; 14310b57cec5SDimitry Andric } 14320b57cec5SDimitry Andric 14330b57cec5SDimitry Andric if (isa<ConstantAggregateZero>(CV)) { 14340b57cec5SDimitry Andric Out << "zeroinitializer"; 14350b57cec5SDimitry Andric return; 14360b57cec5SDimitry Andric } 14370b57cec5SDimitry Andric 14380b57cec5SDimitry Andric if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { 14390b57cec5SDimitry Andric Out << "blockaddress("; 1440349cc55cSDimitry Andric WriteAsOperandInternal(Out, BA->getFunction(), WriterCtx); 14410b57cec5SDimitry Andric Out << ", "; 1442349cc55cSDimitry Andric WriteAsOperandInternal(Out, BA->getBasicBlock(), WriterCtx); 14430b57cec5SDimitry Andric Out << ")"; 14440b57cec5SDimitry Andric return; 14450b57cec5SDimitry Andric } 14460b57cec5SDimitry Andric 1447e8d8bef9SDimitry Andric if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) { 1448e8d8bef9SDimitry Andric Out << "dso_local_equivalent "; 1449349cc55cSDimitry Andric WriteAsOperandInternal(Out, Equiv->getGlobalValue(), WriterCtx); 1450e8d8bef9SDimitry Andric return; 1451e8d8bef9SDimitry Andric } 1452e8d8bef9SDimitry Andric 14530b57cec5SDimitry Andric if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { 14540b57cec5SDimitry Andric Type *ETy = CA->getType()->getElementType(); 14550b57cec5SDimitry Andric Out << '['; 1456349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14570b57cec5SDimitry Andric Out << ' '; 1458349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getOperand(0), WriterCtx); 14590b57cec5SDimitry Andric for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { 14600b57cec5SDimitry Andric Out << ", "; 1461349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14620b57cec5SDimitry Andric Out << ' '; 1463349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getOperand(i), WriterCtx); 14640b57cec5SDimitry Andric } 14650b57cec5SDimitry Andric Out << ']'; 14660b57cec5SDimitry Andric return; 14670b57cec5SDimitry Andric } 14680b57cec5SDimitry Andric 14690b57cec5SDimitry Andric if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) { 14700b57cec5SDimitry Andric // As a special case, print the array as a string if it is an array of 14710b57cec5SDimitry Andric // i8 with ConstantInt values. 14720b57cec5SDimitry Andric if (CA->isString()) { 14730b57cec5SDimitry Andric Out << "c\""; 14740b57cec5SDimitry Andric printEscapedString(CA->getAsString(), Out); 14750b57cec5SDimitry Andric Out << '"'; 14760b57cec5SDimitry Andric return; 14770b57cec5SDimitry Andric } 14780b57cec5SDimitry Andric 14790b57cec5SDimitry Andric Type *ETy = CA->getType()->getElementType(); 14800b57cec5SDimitry Andric Out << '['; 1481349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14820b57cec5SDimitry Andric Out << ' '; 1483349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx); 14840b57cec5SDimitry Andric for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) { 14850b57cec5SDimitry Andric Out << ", "; 1486349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14870b57cec5SDimitry Andric Out << ' '; 1488349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getElementAsConstant(i), WriterCtx); 14890b57cec5SDimitry Andric } 14900b57cec5SDimitry Andric Out << ']'; 14910b57cec5SDimitry Andric return; 14920b57cec5SDimitry Andric } 14930b57cec5SDimitry Andric 14940b57cec5SDimitry Andric if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { 14950b57cec5SDimitry Andric if (CS->getType()->isPacked()) 14960b57cec5SDimitry Andric Out << '<'; 14970b57cec5SDimitry Andric Out << '{'; 14980b57cec5SDimitry Andric unsigned N = CS->getNumOperands(); 14990b57cec5SDimitry Andric if (N) { 15000b57cec5SDimitry Andric Out << ' '; 1501349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CS->getOperand(0)->getType(), Out); 15020b57cec5SDimitry Andric Out << ' '; 15030b57cec5SDimitry Andric 1504349cc55cSDimitry Andric WriteAsOperandInternal(Out, CS->getOperand(0), WriterCtx); 15050b57cec5SDimitry Andric 15060b57cec5SDimitry Andric for (unsigned i = 1; i < N; i++) { 15070b57cec5SDimitry Andric Out << ", "; 1508349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CS->getOperand(i)->getType(), Out); 15090b57cec5SDimitry Andric Out << ' '; 15100b57cec5SDimitry Andric 1511349cc55cSDimitry Andric WriteAsOperandInternal(Out, CS->getOperand(i), WriterCtx); 15120b57cec5SDimitry Andric } 15130b57cec5SDimitry Andric Out << ' '; 15140b57cec5SDimitry Andric } 15150b57cec5SDimitry Andric 15160b57cec5SDimitry Andric Out << '}'; 15170b57cec5SDimitry Andric if (CS->getType()->isPacked()) 15180b57cec5SDimitry Andric Out << '>'; 15190b57cec5SDimitry Andric return; 15200b57cec5SDimitry Andric } 15210b57cec5SDimitry Andric 15220b57cec5SDimitry Andric if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) { 1523e8d8bef9SDimitry Andric auto *CVVTy = cast<FixedVectorType>(CV->getType()); 15245ffd83dbSDimitry Andric Type *ETy = CVVTy->getElementType(); 15250b57cec5SDimitry Andric Out << '<'; 1526349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 15270b57cec5SDimitry Andric Out << ' '; 1528349cc55cSDimitry Andric WriteAsOperandInternal(Out, CV->getAggregateElement(0U), WriterCtx); 15295ffd83dbSDimitry Andric for (unsigned i = 1, e = CVVTy->getNumElements(); i != e; ++i) { 15300b57cec5SDimitry Andric Out << ", "; 1531349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 15320b57cec5SDimitry Andric Out << ' '; 1533349cc55cSDimitry Andric WriteAsOperandInternal(Out, CV->getAggregateElement(i), WriterCtx); 15340b57cec5SDimitry Andric } 15350b57cec5SDimitry Andric Out << '>'; 15360b57cec5SDimitry Andric return; 15370b57cec5SDimitry Andric } 15380b57cec5SDimitry Andric 15390b57cec5SDimitry Andric if (isa<ConstantPointerNull>(CV)) { 15400b57cec5SDimitry Andric Out << "null"; 15410b57cec5SDimitry Andric return; 15420b57cec5SDimitry Andric } 15430b57cec5SDimitry Andric 15440b57cec5SDimitry Andric if (isa<ConstantTokenNone>(CV)) { 15450b57cec5SDimitry Andric Out << "none"; 15460b57cec5SDimitry Andric return; 15470b57cec5SDimitry Andric } 15480b57cec5SDimitry Andric 1549e8d8bef9SDimitry Andric if (isa<PoisonValue>(CV)) { 1550e8d8bef9SDimitry Andric Out << "poison"; 1551e8d8bef9SDimitry Andric return; 1552e8d8bef9SDimitry Andric } 1553e8d8bef9SDimitry Andric 15540b57cec5SDimitry Andric if (isa<UndefValue>(CV)) { 15550b57cec5SDimitry Andric Out << "undef"; 15560b57cec5SDimitry Andric return; 15570b57cec5SDimitry Andric } 15580b57cec5SDimitry Andric 15590b57cec5SDimitry Andric if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 15600b57cec5SDimitry Andric Out << CE->getOpcodeName(); 15610b57cec5SDimitry Andric WriteOptimizationInfo(Out, CE); 15620b57cec5SDimitry Andric if (CE->isCompare()) 15630b57cec5SDimitry Andric Out << ' ' << CmpInst::getPredicateName( 15640b57cec5SDimitry Andric static_cast<CmpInst::Predicate>(CE->getPredicate())); 15650b57cec5SDimitry Andric Out << " ("; 15660b57cec5SDimitry Andric 15670b57cec5SDimitry Andric Optional<unsigned> InRangeOp; 15680b57cec5SDimitry Andric if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) { 1569349cc55cSDimitry Andric WriterCtx.TypePrinter->print(GEP->getSourceElementType(), Out); 15700b57cec5SDimitry Andric Out << ", "; 15710b57cec5SDimitry Andric InRangeOp = GEP->getInRangeIndex(); 15720b57cec5SDimitry Andric if (InRangeOp) 15730b57cec5SDimitry Andric ++*InRangeOp; 15740b57cec5SDimitry Andric } 15750b57cec5SDimitry Andric 15760b57cec5SDimitry Andric for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { 15770b57cec5SDimitry Andric if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp) 15780b57cec5SDimitry Andric Out << "inrange "; 1579349cc55cSDimitry Andric WriterCtx.TypePrinter->print((*OI)->getType(), Out); 15800b57cec5SDimitry Andric Out << ' '; 1581349cc55cSDimitry Andric WriteAsOperandInternal(Out, *OI, WriterCtx); 15820b57cec5SDimitry Andric if (OI+1 != CE->op_end()) 15830b57cec5SDimitry Andric Out << ", "; 15840b57cec5SDimitry Andric } 15850b57cec5SDimitry Andric 15860b57cec5SDimitry Andric if (CE->hasIndices()) { 15870b57cec5SDimitry Andric ArrayRef<unsigned> Indices = CE->getIndices(); 15880b57cec5SDimitry Andric for (unsigned i = 0, e = Indices.size(); i != e; ++i) 15890b57cec5SDimitry Andric Out << ", " << Indices[i]; 15900b57cec5SDimitry Andric } 15910b57cec5SDimitry Andric 15920b57cec5SDimitry Andric if (CE->isCast()) { 15930b57cec5SDimitry Andric Out << " to "; 1594349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CE->getType(), Out); 15950b57cec5SDimitry Andric } 15960b57cec5SDimitry Andric 15975ffd83dbSDimitry Andric if (CE->getOpcode() == Instruction::ShuffleVector) 15985ffd83dbSDimitry Andric PrintShuffleMask(Out, CE->getType(), CE->getShuffleMask()); 15995ffd83dbSDimitry Andric 16000b57cec5SDimitry Andric Out << ')'; 16010b57cec5SDimitry Andric return; 16020b57cec5SDimitry Andric } 16030b57cec5SDimitry Andric 16040b57cec5SDimitry Andric Out << "<placeholder or erroneous Constant>"; 16050b57cec5SDimitry Andric } 16060b57cec5SDimitry Andric 16070b57cec5SDimitry Andric static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, 1608349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 16090b57cec5SDimitry Andric Out << "!{"; 16100b57cec5SDimitry Andric for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { 16110b57cec5SDimitry Andric const Metadata *MD = Node->getOperand(mi); 16120b57cec5SDimitry Andric if (!MD) 16130b57cec5SDimitry Andric Out << "null"; 16140b57cec5SDimitry Andric else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) { 16150b57cec5SDimitry Andric Value *V = MDV->getValue(); 1616349cc55cSDimitry Andric WriterCtx.TypePrinter->print(V->getType(), Out); 16170b57cec5SDimitry Andric Out << ' '; 1618349cc55cSDimitry Andric WriteAsOperandInternal(Out, V, WriterCtx); 16190b57cec5SDimitry Andric } else { 1620349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD, WriterCtx); 1621349cc55cSDimitry Andric WriterCtx.onWriteMetadataAsOperand(MD); 16220b57cec5SDimitry Andric } 16230b57cec5SDimitry Andric if (mi + 1 != me) 16240b57cec5SDimitry Andric Out << ", "; 16250b57cec5SDimitry Andric } 16260b57cec5SDimitry Andric 16270b57cec5SDimitry Andric Out << "}"; 16280b57cec5SDimitry Andric } 16290b57cec5SDimitry Andric 16300b57cec5SDimitry Andric namespace { 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric struct FieldSeparator { 16330b57cec5SDimitry Andric bool Skip = true; 16340b57cec5SDimitry Andric const char *Sep; 16350b57cec5SDimitry Andric 16360b57cec5SDimitry Andric FieldSeparator(const char *Sep = ", ") : Sep(Sep) {} 16370b57cec5SDimitry Andric }; 16380b57cec5SDimitry Andric 16390b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) { 16400b57cec5SDimitry Andric if (FS.Skip) { 16410b57cec5SDimitry Andric FS.Skip = false; 16420b57cec5SDimitry Andric return OS; 16430b57cec5SDimitry Andric } 16440b57cec5SDimitry Andric return OS << FS.Sep; 16450b57cec5SDimitry Andric } 16460b57cec5SDimitry Andric 16470b57cec5SDimitry Andric struct MDFieldPrinter { 16480b57cec5SDimitry Andric raw_ostream &Out; 16490b57cec5SDimitry Andric FieldSeparator FS; 1650349cc55cSDimitry Andric AsmWriterContext &WriterCtx; 16510b57cec5SDimitry Andric 1652349cc55cSDimitry Andric explicit MDFieldPrinter(raw_ostream &Out) 1653349cc55cSDimitry Andric : Out(Out), WriterCtx(AsmWriterContext::getEmpty()) {} 1654349cc55cSDimitry Andric MDFieldPrinter(raw_ostream &Out, AsmWriterContext &Ctx) 1655349cc55cSDimitry Andric : Out(Out), WriterCtx(Ctx) {} 16560b57cec5SDimitry Andric 16570b57cec5SDimitry Andric void printTag(const DINode *N); 16580b57cec5SDimitry Andric void printMacinfoType(const DIMacroNode *N); 16590b57cec5SDimitry Andric void printChecksum(const DIFile::ChecksumInfo<StringRef> &N); 16600b57cec5SDimitry Andric void printString(StringRef Name, StringRef Value, 16610b57cec5SDimitry Andric bool ShouldSkipEmpty = true); 16620b57cec5SDimitry Andric void printMetadata(StringRef Name, const Metadata *MD, 16630b57cec5SDimitry Andric bool ShouldSkipNull = true); 16640b57cec5SDimitry Andric template <class IntTy> 16650b57cec5SDimitry Andric void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true); 16665ffd83dbSDimitry Andric void printAPInt(StringRef Name, const APInt &Int, bool IsUnsigned, 16675ffd83dbSDimitry Andric bool ShouldSkipZero); 16680b57cec5SDimitry Andric void printBool(StringRef Name, bool Value, Optional<bool> Default = None); 16690b57cec5SDimitry Andric void printDIFlags(StringRef Name, DINode::DIFlags Flags); 16700b57cec5SDimitry Andric void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags); 16710b57cec5SDimitry Andric template <class IntTy, class Stringifier> 16720b57cec5SDimitry Andric void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString, 16730b57cec5SDimitry Andric bool ShouldSkipZero = true); 16740b57cec5SDimitry Andric void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK); 16750b57cec5SDimitry Andric void printNameTableKind(StringRef Name, 16760b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind NTK); 16770b57cec5SDimitry Andric }; 16780b57cec5SDimitry Andric 16790b57cec5SDimitry Andric } // end anonymous namespace 16800b57cec5SDimitry Andric 16810b57cec5SDimitry Andric void MDFieldPrinter::printTag(const DINode *N) { 16820b57cec5SDimitry Andric Out << FS << "tag: "; 16830b57cec5SDimitry Andric auto Tag = dwarf::TagString(N->getTag()); 16840b57cec5SDimitry Andric if (!Tag.empty()) 16850b57cec5SDimitry Andric Out << Tag; 16860b57cec5SDimitry Andric else 16870b57cec5SDimitry Andric Out << N->getTag(); 16880b57cec5SDimitry Andric } 16890b57cec5SDimitry Andric 16900b57cec5SDimitry Andric void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) { 16910b57cec5SDimitry Andric Out << FS << "type: "; 16920b57cec5SDimitry Andric auto Type = dwarf::MacinfoString(N->getMacinfoType()); 16930b57cec5SDimitry Andric if (!Type.empty()) 16940b57cec5SDimitry Andric Out << Type; 16950b57cec5SDimitry Andric else 16960b57cec5SDimitry Andric Out << N->getMacinfoType(); 16970b57cec5SDimitry Andric } 16980b57cec5SDimitry Andric 16990b57cec5SDimitry Andric void MDFieldPrinter::printChecksum( 17000b57cec5SDimitry Andric const DIFile::ChecksumInfo<StringRef> &Checksum) { 17010b57cec5SDimitry Andric Out << FS << "checksumkind: " << Checksum.getKindAsString(); 17020b57cec5SDimitry Andric printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false); 17030b57cec5SDimitry Andric } 17040b57cec5SDimitry Andric 17050b57cec5SDimitry Andric void MDFieldPrinter::printString(StringRef Name, StringRef Value, 17060b57cec5SDimitry Andric bool ShouldSkipEmpty) { 17070b57cec5SDimitry Andric if (ShouldSkipEmpty && Value.empty()) 17080b57cec5SDimitry Andric return; 17090b57cec5SDimitry Andric 17100b57cec5SDimitry Andric Out << FS << Name << ": \""; 17110b57cec5SDimitry Andric printEscapedString(Value, Out); 17120b57cec5SDimitry Andric Out << "\""; 17130b57cec5SDimitry Andric } 17140b57cec5SDimitry Andric 17150b57cec5SDimitry Andric static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD, 1716349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 17170b57cec5SDimitry Andric if (!MD) { 17180b57cec5SDimitry Andric Out << "null"; 17190b57cec5SDimitry Andric return; 17200b57cec5SDimitry Andric } 1721349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD, WriterCtx); 1722349cc55cSDimitry Andric WriterCtx.onWriteMetadataAsOperand(MD); 17230b57cec5SDimitry Andric } 17240b57cec5SDimitry Andric 17250b57cec5SDimitry Andric void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD, 17260b57cec5SDimitry Andric bool ShouldSkipNull) { 17270b57cec5SDimitry Andric if (ShouldSkipNull && !MD) 17280b57cec5SDimitry Andric return; 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric Out << FS << Name << ": "; 1731349cc55cSDimitry Andric writeMetadataAsOperand(Out, MD, WriterCtx); 17320b57cec5SDimitry Andric } 17330b57cec5SDimitry Andric 17340b57cec5SDimitry Andric template <class IntTy> 17350b57cec5SDimitry Andric void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) { 17360b57cec5SDimitry Andric if (ShouldSkipZero && !Int) 17370b57cec5SDimitry Andric return; 17380b57cec5SDimitry Andric 17390b57cec5SDimitry Andric Out << FS << Name << ": " << Int; 17400b57cec5SDimitry Andric } 17410b57cec5SDimitry Andric 17425ffd83dbSDimitry Andric void MDFieldPrinter::printAPInt(StringRef Name, const APInt &Int, 17435ffd83dbSDimitry Andric bool IsUnsigned, bool ShouldSkipZero) { 1744349cc55cSDimitry Andric if (ShouldSkipZero && Int.isZero()) 17455ffd83dbSDimitry Andric return; 17465ffd83dbSDimitry Andric 17475ffd83dbSDimitry Andric Out << FS << Name << ": "; 17485ffd83dbSDimitry Andric Int.print(Out, !IsUnsigned); 17495ffd83dbSDimitry Andric } 17505ffd83dbSDimitry Andric 17510b57cec5SDimitry Andric void MDFieldPrinter::printBool(StringRef Name, bool Value, 17520b57cec5SDimitry Andric Optional<bool> Default) { 17530b57cec5SDimitry Andric if (Default && Value == *Default) 17540b57cec5SDimitry Andric return; 17550b57cec5SDimitry Andric Out << FS << Name << ": " << (Value ? "true" : "false"); 17560b57cec5SDimitry Andric } 17570b57cec5SDimitry Andric 17580b57cec5SDimitry Andric void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) { 17590b57cec5SDimitry Andric if (!Flags) 17600b57cec5SDimitry Andric return; 17610b57cec5SDimitry Andric 17620b57cec5SDimitry Andric Out << FS << Name << ": "; 17630b57cec5SDimitry Andric 17640b57cec5SDimitry Andric SmallVector<DINode::DIFlags, 8> SplitFlags; 17650b57cec5SDimitry Andric auto Extra = DINode::splitFlags(Flags, SplitFlags); 17660b57cec5SDimitry Andric 17670b57cec5SDimitry Andric FieldSeparator FlagsFS(" | "); 17680b57cec5SDimitry Andric for (auto F : SplitFlags) { 17690b57cec5SDimitry Andric auto StringF = DINode::getFlagString(F); 17700b57cec5SDimitry Andric assert(!StringF.empty() && "Expected valid flag"); 17710b57cec5SDimitry Andric Out << FlagsFS << StringF; 17720b57cec5SDimitry Andric } 17730b57cec5SDimitry Andric if (Extra || SplitFlags.empty()) 17740b57cec5SDimitry Andric Out << FlagsFS << Extra; 17750b57cec5SDimitry Andric } 17760b57cec5SDimitry Andric 17770b57cec5SDimitry Andric void MDFieldPrinter::printDISPFlags(StringRef Name, 17780b57cec5SDimitry Andric DISubprogram::DISPFlags Flags) { 17790b57cec5SDimitry Andric // Always print this field, because no flags in the IR at all will be 17800b57cec5SDimitry Andric // interpreted as old-style isDefinition: true. 17810b57cec5SDimitry Andric Out << FS << Name << ": "; 17820b57cec5SDimitry Andric 17830b57cec5SDimitry Andric if (!Flags) { 17840b57cec5SDimitry Andric Out << 0; 17850b57cec5SDimitry Andric return; 17860b57cec5SDimitry Andric } 17870b57cec5SDimitry Andric 17880b57cec5SDimitry Andric SmallVector<DISubprogram::DISPFlags, 8> SplitFlags; 17890b57cec5SDimitry Andric auto Extra = DISubprogram::splitFlags(Flags, SplitFlags); 17900b57cec5SDimitry Andric 17910b57cec5SDimitry Andric FieldSeparator FlagsFS(" | "); 17920b57cec5SDimitry Andric for (auto F : SplitFlags) { 17930b57cec5SDimitry Andric auto StringF = DISubprogram::getFlagString(F); 17940b57cec5SDimitry Andric assert(!StringF.empty() && "Expected valid flag"); 17950b57cec5SDimitry Andric Out << FlagsFS << StringF; 17960b57cec5SDimitry Andric } 17970b57cec5SDimitry Andric if (Extra || SplitFlags.empty()) 17980b57cec5SDimitry Andric Out << FlagsFS << Extra; 17990b57cec5SDimitry Andric } 18000b57cec5SDimitry Andric 18010b57cec5SDimitry Andric void MDFieldPrinter::printEmissionKind(StringRef Name, 18020b57cec5SDimitry Andric DICompileUnit::DebugEmissionKind EK) { 18030b57cec5SDimitry Andric Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK); 18040b57cec5SDimitry Andric } 18050b57cec5SDimitry Andric 18060b57cec5SDimitry Andric void MDFieldPrinter::printNameTableKind(StringRef Name, 18070b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind NTK) { 18080b57cec5SDimitry Andric if (NTK == DICompileUnit::DebugNameTableKind::Default) 18090b57cec5SDimitry Andric return; 18100b57cec5SDimitry Andric Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK); 18110b57cec5SDimitry Andric } 18120b57cec5SDimitry Andric 18130b57cec5SDimitry Andric template <class IntTy, class Stringifier> 18140b57cec5SDimitry Andric void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value, 18150b57cec5SDimitry Andric Stringifier toString, bool ShouldSkipZero) { 18160b57cec5SDimitry Andric if (!Value) 18170b57cec5SDimitry Andric return; 18180b57cec5SDimitry Andric 18190b57cec5SDimitry Andric Out << FS << Name << ": "; 18200b57cec5SDimitry Andric auto S = toString(Value); 18210b57cec5SDimitry Andric if (!S.empty()) 18220b57cec5SDimitry Andric Out << S; 18230b57cec5SDimitry Andric else 18240b57cec5SDimitry Andric Out << Value; 18250b57cec5SDimitry Andric } 18260b57cec5SDimitry Andric 18270b57cec5SDimitry Andric static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, 1828349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18290b57cec5SDimitry Andric Out << "!GenericDINode("; 1830349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 18310b57cec5SDimitry Andric Printer.printTag(N); 18320b57cec5SDimitry Andric Printer.printString("header", N->getHeader()); 18330b57cec5SDimitry Andric if (N->getNumDwarfOperands()) { 18340b57cec5SDimitry Andric Out << Printer.FS << "operands: {"; 18350b57cec5SDimitry Andric FieldSeparator IFS; 18360b57cec5SDimitry Andric for (auto &I : N->dwarf_operands()) { 18370b57cec5SDimitry Andric Out << IFS; 1838349cc55cSDimitry Andric writeMetadataAsOperand(Out, I, WriterCtx); 18390b57cec5SDimitry Andric } 18400b57cec5SDimitry Andric Out << "}"; 18410b57cec5SDimitry Andric } 18420b57cec5SDimitry Andric Out << ")"; 18430b57cec5SDimitry Andric } 18440b57cec5SDimitry Andric 18450b57cec5SDimitry Andric static void writeDILocation(raw_ostream &Out, const DILocation *DL, 1846349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18470b57cec5SDimitry Andric Out << "!DILocation("; 1848349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 18490b57cec5SDimitry Andric // Always output the line, since 0 is a relevant and important value for it. 18500b57cec5SDimitry Andric Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false); 18510b57cec5SDimitry Andric Printer.printInt("column", DL->getColumn()); 18520b57cec5SDimitry Andric Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false); 18530b57cec5SDimitry Andric Printer.printMetadata("inlinedAt", DL->getRawInlinedAt()); 18540b57cec5SDimitry Andric Printer.printBool("isImplicitCode", DL->isImplicitCode(), 18550b57cec5SDimitry Andric /* Default */ false); 18560b57cec5SDimitry Andric Out << ")"; 18570b57cec5SDimitry Andric } 18580b57cec5SDimitry Andric 18590b57cec5SDimitry Andric static void writeDISubrange(raw_ostream &Out, const DISubrange *N, 1860349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18610b57cec5SDimitry Andric Out << "!DISubrange("; 1862349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1863fe6060f1SDimitry Andric 1864fe6060f1SDimitry Andric auto *Count = N->getRawCountNode(); 1865fe6060f1SDimitry Andric if (auto *CE = dyn_cast_or_null<ConstantAsMetadata>(Count)) { 1866fe6060f1SDimitry Andric auto *CV = cast<ConstantInt>(CE->getValue()); 1867fe6060f1SDimitry Andric Printer.printInt("count", CV->getSExtValue(), 1868fe6060f1SDimitry Andric /* ShouldSkipZero */ false); 1869fe6060f1SDimitry Andric } else 1870fe6060f1SDimitry Andric Printer.printMetadata("count", Count, /*ShouldSkipNull */ true); 18715ffd83dbSDimitry Andric 18725ffd83dbSDimitry Andric // A lowerBound of constant 0 should not be skipped, since it is different 18735ffd83dbSDimitry Andric // from an unspecified lower bound (= nullptr). 18745ffd83dbSDimitry Andric auto *LBound = N->getRawLowerBound(); 18755ffd83dbSDimitry Andric if (auto *LE = dyn_cast_or_null<ConstantAsMetadata>(LBound)) { 18765ffd83dbSDimitry Andric auto *LV = cast<ConstantInt>(LE->getValue()); 18775ffd83dbSDimitry Andric Printer.printInt("lowerBound", LV->getSExtValue(), 18785ffd83dbSDimitry Andric /* ShouldSkipZero */ false); 18795ffd83dbSDimitry Andric } else 18805ffd83dbSDimitry Andric Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true); 18815ffd83dbSDimitry Andric 18825ffd83dbSDimitry Andric auto *UBound = N->getRawUpperBound(); 18835ffd83dbSDimitry Andric if (auto *UE = dyn_cast_or_null<ConstantAsMetadata>(UBound)) { 18845ffd83dbSDimitry Andric auto *UV = cast<ConstantInt>(UE->getValue()); 18855ffd83dbSDimitry Andric Printer.printInt("upperBound", UV->getSExtValue(), 18865ffd83dbSDimitry Andric /* ShouldSkipZero */ false); 18875ffd83dbSDimitry Andric } else 18885ffd83dbSDimitry Andric Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true); 18895ffd83dbSDimitry Andric 18905ffd83dbSDimitry Andric auto *Stride = N->getRawStride(); 18915ffd83dbSDimitry Andric if (auto *SE = dyn_cast_or_null<ConstantAsMetadata>(Stride)) { 18925ffd83dbSDimitry Andric auto *SV = cast<ConstantInt>(SE->getValue()); 18935ffd83dbSDimitry Andric Printer.printInt("stride", SV->getSExtValue(), /* ShouldSkipZero */ false); 18945ffd83dbSDimitry Andric } else 18955ffd83dbSDimitry Andric Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true); 18965ffd83dbSDimitry Andric 18970b57cec5SDimitry Andric Out << ")"; 18980b57cec5SDimitry Andric } 18990b57cec5SDimitry Andric 1900e8d8bef9SDimitry Andric static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, 1901349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 1902e8d8bef9SDimitry Andric Out << "!DIGenericSubrange("; 1903349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1904e8d8bef9SDimitry Andric 1905e8d8bef9SDimitry Andric auto IsConstant = [&](Metadata *Bound) -> bool { 1906e8d8bef9SDimitry Andric if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) { 1907349cc55cSDimitry Andric return BE->isConstant() && 1908349cc55cSDimitry Andric DIExpression::SignedOrUnsignedConstant::SignedConstant == 1909349cc55cSDimitry Andric *BE->isConstant(); 1910e8d8bef9SDimitry Andric } 1911e8d8bef9SDimitry Andric return false; 1912e8d8bef9SDimitry Andric }; 1913e8d8bef9SDimitry Andric 1914e8d8bef9SDimitry Andric auto GetConstant = [&](Metadata *Bound) -> int64_t { 1915e8d8bef9SDimitry Andric assert(IsConstant(Bound) && "Expected constant"); 1916e8d8bef9SDimitry Andric auto *BE = dyn_cast_or_null<DIExpression>(Bound); 1917e8d8bef9SDimitry Andric return static_cast<int64_t>(BE->getElement(1)); 1918e8d8bef9SDimitry Andric }; 1919e8d8bef9SDimitry Andric 1920e8d8bef9SDimitry Andric auto *Count = N->getRawCountNode(); 1921e8d8bef9SDimitry Andric if (IsConstant(Count)) 1922e8d8bef9SDimitry Andric Printer.printInt("count", GetConstant(Count), 1923e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1924e8d8bef9SDimitry Andric else 1925e8d8bef9SDimitry Andric Printer.printMetadata("count", Count, /*ShouldSkipNull */ true); 1926e8d8bef9SDimitry Andric 1927e8d8bef9SDimitry Andric auto *LBound = N->getRawLowerBound(); 1928e8d8bef9SDimitry Andric if (IsConstant(LBound)) 1929e8d8bef9SDimitry Andric Printer.printInt("lowerBound", GetConstant(LBound), 1930e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1931e8d8bef9SDimitry Andric else 1932e8d8bef9SDimitry Andric Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true); 1933e8d8bef9SDimitry Andric 1934e8d8bef9SDimitry Andric auto *UBound = N->getRawUpperBound(); 1935e8d8bef9SDimitry Andric if (IsConstant(UBound)) 1936e8d8bef9SDimitry Andric Printer.printInt("upperBound", GetConstant(UBound), 1937e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1938e8d8bef9SDimitry Andric else 1939e8d8bef9SDimitry Andric Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true); 1940e8d8bef9SDimitry Andric 1941e8d8bef9SDimitry Andric auto *Stride = N->getRawStride(); 1942e8d8bef9SDimitry Andric if (IsConstant(Stride)) 1943e8d8bef9SDimitry Andric Printer.printInt("stride", GetConstant(Stride), 1944e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1945e8d8bef9SDimitry Andric else 1946e8d8bef9SDimitry Andric Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true); 1947e8d8bef9SDimitry Andric 1948e8d8bef9SDimitry Andric Out << ")"; 1949e8d8bef9SDimitry Andric } 1950e8d8bef9SDimitry Andric 19510b57cec5SDimitry Andric static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, 1952349cc55cSDimitry Andric AsmWriterContext &) { 19530b57cec5SDimitry Andric Out << "!DIEnumerator("; 19540b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 19550b57cec5SDimitry Andric Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false); 19565ffd83dbSDimitry Andric Printer.printAPInt("value", N->getValue(), N->isUnsigned(), 19575ffd83dbSDimitry Andric /*ShouldSkipZero=*/false); 19585ffd83dbSDimitry Andric if (N->isUnsigned()) 19590b57cec5SDimitry Andric Printer.printBool("isUnsigned", true); 19600b57cec5SDimitry Andric Out << ")"; 19610b57cec5SDimitry Andric } 19620b57cec5SDimitry Andric 19630b57cec5SDimitry Andric static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, 1964349cc55cSDimitry Andric AsmWriterContext &) { 19650b57cec5SDimitry Andric Out << "!DIBasicType("; 19660b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 19670b57cec5SDimitry Andric if (N->getTag() != dwarf::DW_TAG_base_type) 19680b57cec5SDimitry Andric Printer.printTag(N); 19690b57cec5SDimitry Andric Printer.printString("name", N->getName()); 19700b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 19710b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 19720b57cec5SDimitry Andric Printer.printDwarfEnum("encoding", N->getEncoding(), 19730b57cec5SDimitry Andric dwarf::AttributeEncodingString); 19740b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 19750b57cec5SDimitry Andric Out << ")"; 19760b57cec5SDimitry Andric } 19770b57cec5SDimitry Andric 1978e8d8bef9SDimitry Andric static void writeDIStringType(raw_ostream &Out, const DIStringType *N, 1979349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 1980e8d8bef9SDimitry Andric Out << "!DIStringType("; 1981349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1982e8d8bef9SDimitry Andric if (N->getTag() != dwarf::DW_TAG_string_type) 1983e8d8bef9SDimitry Andric Printer.printTag(N); 1984e8d8bef9SDimitry Andric Printer.printString("name", N->getName()); 1985e8d8bef9SDimitry Andric Printer.printMetadata("stringLength", N->getRawStringLength()); 1986e8d8bef9SDimitry Andric Printer.printMetadata("stringLengthExpression", N->getRawStringLengthExp()); 1987e8d8bef9SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 1988e8d8bef9SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 1989e8d8bef9SDimitry Andric Printer.printDwarfEnum("encoding", N->getEncoding(), 1990e8d8bef9SDimitry Andric dwarf::AttributeEncodingString); 1991e8d8bef9SDimitry Andric Out << ")"; 1992e8d8bef9SDimitry Andric } 1993e8d8bef9SDimitry Andric 19940b57cec5SDimitry Andric static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, 1995349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 19960b57cec5SDimitry Andric Out << "!DIDerivedType("; 1997349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 19980b57cec5SDimitry Andric Printer.printTag(N); 19990b57cec5SDimitry Andric Printer.printString("name", N->getName()); 20000b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope()); 20010b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 20020b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 20030b57cec5SDimitry Andric Printer.printMetadata("baseType", N->getRawBaseType(), 20040b57cec5SDimitry Andric /* ShouldSkipNull */ false); 20050b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 20060b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 20070b57cec5SDimitry Andric Printer.printInt("offset", N->getOffsetInBits()); 20080b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20090b57cec5SDimitry Andric Printer.printMetadata("extraData", N->getRawExtraData()); 20100b57cec5SDimitry Andric if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace()) 20110b57cec5SDimitry Andric Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace, 20120b57cec5SDimitry Andric /* ShouldSkipZero */ false); 2013349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 20140b57cec5SDimitry Andric Out << ")"; 20150b57cec5SDimitry Andric } 20160b57cec5SDimitry Andric 20170b57cec5SDimitry Andric static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, 2018349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20190b57cec5SDimitry Andric Out << "!DICompositeType("; 2020349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20210b57cec5SDimitry Andric Printer.printTag(N); 20220b57cec5SDimitry Andric Printer.printString("name", N->getName()); 20230b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope()); 20240b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 20250b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 20260b57cec5SDimitry Andric Printer.printMetadata("baseType", N->getRawBaseType()); 20270b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 20280b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 20290b57cec5SDimitry Andric Printer.printInt("offset", N->getOffsetInBits()); 20300b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20310b57cec5SDimitry Andric Printer.printMetadata("elements", N->getRawElements()); 20320b57cec5SDimitry Andric Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(), 20330b57cec5SDimitry Andric dwarf::LanguageString); 20340b57cec5SDimitry Andric Printer.printMetadata("vtableHolder", N->getRawVTableHolder()); 20350b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 20360b57cec5SDimitry Andric Printer.printString("identifier", N->getIdentifier()); 20370b57cec5SDimitry Andric Printer.printMetadata("discriminator", N->getRawDiscriminator()); 20385ffd83dbSDimitry Andric Printer.printMetadata("dataLocation", N->getRawDataLocation()); 2039e8d8bef9SDimitry Andric Printer.printMetadata("associated", N->getRawAssociated()); 2040e8d8bef9SDimitry Andric Printer.printMetadata("allocated", N->getRawAllocated()); 2041e8d8bef9SDimitry Andric if (auto *RankConst = N->getRankConst()) 2042e8d8bef9SDimitry Andric Printer.printInt("rank", RankConst->getSExtValue(), 2043e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 2044e8d8bef9SDimitry Andric else 2045e8d8bef9SDimitry Andric Printer.printMetadata("rank", N->getRawRank(), /*ShouldSkipNull */ true); 2046349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 20470b57cec5SDimitry Andric Out << ")"; 20480b57cec5SDimitry Andric } 20490b57cec5SDimitry Andric 20500b57cec5SDimitry Andric static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, 2051349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20520b57cec5SDimitry Andric Out << "!DISubroutineType("; 2053349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20540b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20550b57cec5SDimitry Andric Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString); 20560b57cec5SDimitry Andric Printer.printMetadata("types", N->getRawTypeArray(), 20570b57cec5SDimitry Andric /* ShouldSkipNull */ false); 20580b57cec5SDimitry Andric Out << ")"; 20590b57cec5SDimitry Andric } 20600b57cec5SDimitry Andric 2061349cc55cSDimitry Andric static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &) { 20620b57cec5SDimitry Andric Out << "!DIFile("; 20630b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 20640b57cec5SDimitry Andric Printer.printString("filename", N->getFilename(), 20650b57cec5SDimitry Andric /* ShouldSkipEmpty */ false); 20660b57cec5SDimitry Andric Printer.printString("directory", N->getDirectory(), 20670b57cec5SDimitry Andric /* ShouldSkipEmpty */ false); 20680b57cec5SDimitry Andric // Print all values for checksum together, or not at all. 20690b57cec5SDimitry Andric if (N->getChecksum()) 20700b57cec5SDimitry Andric Printer.printChecksum(*N->getChecksum()); 20710b57cec5SDimitry Andric Printer.printString("source", N->getSource().getValueOr(StringRef()), 20720b57cec5SDimitry Andric /* ShouldSkipEmpty */ true); 20730b57cec5SDimitry Andric Out << ")"; 20740b57cec5SDimitry Andric } 20750b57cec5SDimitry Andric 20760b57cec5SDimitry Andric static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, 2077349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20780b57cec5SDimitry Andric Out << "!DICompileUnit("; 2079349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20800b57cec5SDimitry Andric Printer.printDwarfEnum("language", N->getSourceLanguage(), 20810b57cec5SDimitry Andric dwarf::LanguageString, /* ShouldSkipZero */ false); 20820b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false); 20830b57cec5SDimitry Andric Printer.printString("producer", N->getProducer()); 20840b57cec5SDimitry Andric Printer.printBool("isOptimized", N->isOptimized()); 20850b57cec5SDimitry Andric Printer.printString("flags", N->getFlags()); 20860b57cec5SDimitry Andric Printer.printInt("runtimeVersion", N->getRuntimeVersion(), 20870b57cec5SDimitry Andric /* ShouldSkipZero */ false); 20880b57cec5SDimitry Andric Printer.printString("splitDebugFilename", N->getSplitDebugFilename()); 20890b57cec5SDimitry Andric Printer.printEmissionKind("emissionKind", N->getEmissionKind()); 20900b57cec5SDimitry Andric Printer.printMetadata("enums", N->getRawEnumTypes()); 20910b57cec5SDimitry Andric Printer.printMetadata("retainedTypes", N->getRawRetainedTypes()); 20920b57cec5SDimitry Andric Printer.printMetadata("globals", N->getRawGlobalVariables()); 20930b57cec5SDimitry Andric Printer.printMetadata("imports", N->getRawImportedEntities()); 20940b57cec5SDimitry Andric Printer.printMetadata("macros", N->getRawMacros()); 20950b57cec5SDimitry Andric Printer.printInt("dwoId", N->getDWOId()); 20960b57cec5SDimitry Andric Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true); 20970b57cec5SDimitry Andric Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(), 20980b57cec5SDimitry Andric false); 20990b57cec5SDimitry Andric Printer.printNameTableKind("nameTableKind", N->getNameTableKind()); 21000b57cec5SDimitry Andric Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false); 21015ffd83dbSDimitry Andric Printer.printString("sysroot", N->getSysRoot()); 21025ffd83dbSDimitry Andric Printer.printString("sdk", N->getSDK()); 21030b57cec5SDimitry Andric Out << ")"; 21040b57cec5SDimitry Andric } 21050b57cec5SDimitry Andric 21060b57cec5SDimitry Andric static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, 2107349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21080b57cec5SDimitry Andric Out << "!DISubprogram("; 2109349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21100b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21110b57cec5SDimitry Andric Printer.printString("linkageName", N->getLinkageName()); 21120b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21130b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21140b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21150b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 21160b57cec5SDimitry Andric Printer.printInt("scopeLine", N->getScopeLine()); 21170b57cec5SDimitry Andric Printer.printMetadata("containingType", N->getRawContainingType()); 21180b57cec5SDimitry Andric if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none || 21190b57cec5SDimitry Andric N->getVirtualIndex() != 0) 21200b57cec5SDimitry Andric Printer.printInt("virtualIndex", N->getVirtualIndex(), false); 21210b57cec5SDimitry Andric Printer.printInt("thisAdjustment", N->getThisAdjustment()); 21220b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 21230b57cec5SDimitry Andric Printer.printDISPFlags("spFlags", N->getSPFlags()); 21240b57cec5SDimitry Andric Printer.printMetadata("unit", N->getRawUnit()); 21250b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 21260b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawDeclaration()); 21270b57cec5SDimitry Andric Printer.printMetadata("retainedNodes", N->getRawRetainedNodes()); 21280b57cec5SDimitry Andric Printer.printMetadata("thrownTypes", N->getRawThrownTypes()); 2129349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 21300b57cec5SDimitry Andric Out << ")"; 21310b57cec5SDimitry Andric } 21320b57cec5SDimitry Andric 21330b57cec5SDimitry Andric static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, 2134349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21350b57cec5SDimitry Andric Out << "!DILexicalBlock("; 2136349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21370b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21380b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21390b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21400b57cec5SDimitry Andric Printer.printInt("column", N->getColumn()); 21410b57cec5SDimitry Andric Out << ")"; 21420b57cec5SDimitry Andric } 21430b57cec5SDimitry Andric 21440b57cec5SDimitry Andric static void writeDILexicalBlockFile(raw_ostream &Out, 21450b57cec5SDimitry Andric const DILexicalBlockFile *N, 2146349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21470b57cec5SDimitry Andric Out << "!DILexicalBlockFile("; 2148349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21490b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21500b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21510b57cec5SDimitry Andric Printer.printInt("discriminator", N->getDiscriminator(), 21520b57cec5SDimitry Andric /* ShouldSkipZero */ false); 21530b57cec5SDimitry Andric Out << ")"; 21540b57cec5SDimitry Andric } 21550b57cec5SDimitry Andric 21560b57cec5SDimitry Andric static void writeDINamespace(raw_ostream &Out, const DINamespace *N, 2157349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21580b57cec5SDimitry Andric Out << "!DINamespace("; 2159349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21600b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21610b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21620b57cec5SDimitry Andric Printer.printBool("exportSymbols", N->getExportSymbols(), false); 21630b57cec5SDimitry Andric Out << ")"; 21640b57cec5SDimitry Andric } 21650b57cec5SDimitry Andric 21660b57cec5SDimitry Andric static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, 2167349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21680b57cec5SDimitry Andric Out << "!DICommonBlock("; 2169349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21700b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), false); 21710b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawDecl(), false); 21720b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21730b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21740b57cec5SDimitry Andric Printer.printInt("line", N->getLineNo()); 21750b57cec5SDimitry Andric Out << ")"; 21760b57cec5SDimitry Andric } 21770b57cec5SDimitry Andric 21780b57cec5SDimitry Andric static void writeDIMacro(raw_ostream &Out, const DIMacro *N, 2179349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21800b57cec5SDimitry Andric Out << "!DIMacro("; 2181349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21820b57cec5SDimitry Andric Printer.printMacinfoType(N); 21830b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21840b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21850b57cec5SDimitry Andric Printer.printString("value", N->getValue()); 21860b57cec5SDimitry Andric Out << ")"; 21870b57cec5SDimitry Andric } 21880b57cec5SDimitry Andric 21890b57cec5SDimitry Andric static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, 2190349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21910b57cec5SDimitry Andric Out << "!DIMacroFile("; 2192349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21930b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21940b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false); 21950b57cec5SDimitry Andric Printer.printMetadata("nodes", N->getRawElements()); 21960b57cec5SDimitry Andric Out << ")"; 21970b57cec5SDimitry Andric } 21980b57cec5SDimitry Andric 21990b57cec5SDimitry Andric static void writeDIModule(raw_ostream &Out, const DIModule *N, 2200349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22010b57cec5SDimitry Andric Out << "!DIModule("; 2202349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22030b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22040b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22050b57cec5SDimitry Andric Printer.printString("configMacros", N->getConfigurationMacros()); 22060b57cec5SDimitry Andric Printer.printString("includePath", N->getIncludePath()); 22075ffd83dbSDimitry Andric Printer.printString("apinotes", N->getAPINotesFile()); 22085ffd83dbSDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22095ffd83dbSDimitry Andric Printer.printInt("line", N->getLineNo()); 2210e8d8bef9SDimitry Andric Printer.printBool("isDecl", N->getIsDecl(), /* Default */ false); 22110b57cec5SDimitry Andric Out << ")"; 22120b57cec5SDimitry Andric } 22130b57cec5SDimitry Andric 22140b57cec5SDimitry Andric static void writeDITemplateTypeParameter(raw_ostream &Out, 22150b57cec5SDimitry Andric const DITemplateTypeParameter *N, 2216349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22170b57cec5SDimitry Andric Out << "!DITemplateTypeParameter("; 2218349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22190b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22200b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false); 22215ffd83dbSDimitry Andric Printer.printBool("defaulted", N->isDefault(), /* Default= */ false); 22220b57cec5SDimitry Andric Out << ")"; 22230b57cec5SDimitry Andric } 22240b57cec5SDimitry Andric 22250b57cec5SDimitry Andric static void writeDITemplateValueParameter(raw_ostream &Out, 22260b57cec5SDimitry Andric const DITemplateValueParameter *N, 2227349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22280b57cec5SDimitry Andric Out << "!DITemplateValueParameter("; 2229349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22300b57cec5SDimitry Andric if (N->getTag() != dwarf::DW_TAG_template_value_parameter) 22310b57cec5SDimitry Andric Printer.printTag(N); 22320b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22330b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22345ffd83dbSDimitry Andric Printer.printBool("defaulted", N->isDefault(), /* Default= */ false); 22350b57cec5SDimitry Andric Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false); 22360b57cec5SDimitry Andric Out << ")"; 22370b57cec5SDimitry Andric } 22380b57cec5SDimitry Andric 22390b57cec5SDimitry Andric static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, 2240349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22410b57cec5SDimitry Andric Out << "!DIGlobalVariable("; 2242349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22430b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22440b57cec5SDimitry Andric Printer.printString("linkageName", N->getLinkageName()); 22450b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22460b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22470b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22480b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22490b57cec5SDimitry Andric Printer.printBool("isLocal", N->isLocalToUnit()); 22500b57cec5SDimitry Andric Printer.printBool("isDefinition", N->isDefinition()); 22510b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration()); 22520b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 22530b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 2254349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 22550b57cec5SDimitry Andric Out << ")"; 22560b57cec5SDimitry Andric } 22570b57cec5SDimitry Andric 22580b57cec5SDimitry Andric static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, 2259349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22600b57cec5SDimitry Andric Out << "!DILocalVariable("; 2261349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22620b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22630b57cec5SDimitry Andric Printer.printInt("arg", N->getArg()); 22640b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22650b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22660b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22670b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22680b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 22690b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 2270349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 22710b57cec5SDimitry Andric Out << ")"; 22720b57cec5SDimitry Andric } 22730b57cec5SDimitry Andric 22740b57cec5SDimitry Andric static void writeDILabel(raw_ostream &Out, const DILabel *N, 2275349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22760b57cec5SDimitry Andric Out << "!DILabel("; 2277349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22780b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22790b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22800b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22810b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22820b57cec5SDimitry Andric Out << ")"; 22830b57cec5SDimitry Andric } 22840b57cec5SDimitry Andric 22850b57cec5SDimitry Andric static void writeDIExpression(raw_ostream &Out, const DIExpression *N, 2286349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22870b57cec5SDimitry Andric Out << "!DIExpression("; 22880b57cec5SDimitry Andric FieldSeparator FS; 22890b57cec5SDimitry Andric if (N->isValid()) { 2290fe6060f1SDimitry Andric for (const DIExpression::ExprOperand &Op : N->expr_ops()) { 2291fe6060f1SDimitry Andric auto OpStr = dwarf::OperationEncodingString(Op.getOp()); 22920b57cec5SDimitry Andric assert(!OpStr.empty() && "Expected valid opcode"); 22930b57cec5SDimitry Andric 22940b57cec5SDimitry Andric Out << FS << OpStr; 2295fe6060f1SDimitry Andric if (Op.getOp() == dwarf::DW_OP_LLVM_convert) { 2296fe6060f1SDimitry Andric Out << FS << Op.getArg(0); 2297fe6060f1SDimitry Andric Out << FS << dwarf::AttributeEncodingString(Op.getArg(1)); 22980b57cec5SDimitry Andric } else { 2299fe6060f1SDimitry Andric for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A) 2300fe6060f1SDimitry Andric Out << FS << Op.getArg(A); 23010b57cec5SDimitry Andric } 23020b57cec5SDimitry Andric } 23030b57cec5SDimitry Andric } else { 23040b57cec5SDimitry Andric for (const auto &I : N->getElements()) 23050b57cec5SDimitry Andric Out << FS << I; 23060b57cec5SDimitry Andric } 23070b57cec5SDimitry Andric Out << ")"; 23080b57cec5SDimitry Andric } 23090b57cec5SDimitry Andric 2310fe6060f1SDimitry Andric static void writeDIArgList(raw_ostream &Out, const DIArgList *N, 2311349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 2312349cc55cSDimitry Andric bool FromValue = false) { 2313fe6060f1SDimitry Andric assert(FromValue && 2314fe6060f1SDimitry Andric "Unexpected DIArgList metadata outside of value argument"); 2315fe6060f1SDimitry Andric Out << "!DIArgList("; 2316fe6060f1SDimitry Andric FieldSeparator FS; 2317349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 2318fe6060f1SDimitry Andric for (Metadata *Arg : N->getArgs()) { 2319fe6060f1SDimitry Andric Out << FS; 2320349cc55cSDimitry Andric WriteAsOperandInternal(Out, Arg, WriterCtx, true); 2321fe6060f1SDimitry Andric } 2322fe6060f1SDimitry Andric Out << ")"; 2323fe6060f1SDimitry Andric } 2324fe6060f1SDimitry Andric 23250b57cec5SDimitry Andric static void writeDIGlobalVariableExpression(raw_ostream &Out, 23260b57cec5SDimitry Andric const DIGlobalVariableExpression *N, 2327349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23280b57cec5SDimitry Andric Out << "!DIGlobalVariableExpression("; 2329349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23300b57cec5SDimitry Andric Printer.printMetadata("var", N->getVariable()); 23310b57cec5SDimitry Andric Printer.printMetadata("expr", N->getExpression()); 23320b57cec5SDimitry Andric Out << ")"; 23330b57cec5SDimitry Andric } 23340b57cec5SDimitry Andric 23350b57cec5SDimitry Andric static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, 2336349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23370b57cec5SDimitry Andric Out << "!DIObjCProperty("; 2338349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23390b57cec5SDimitry Andric Printer.printString("name", N->getName()); 23400b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 23410b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 23420b57cec5SDimitry Andric Printer.printString("setter", N->getSetterName()); 23430b57cec5SDimitry Andric Printer.printString("getter", N->getGetterName()); 23440b57cec5SDimitry Andric Printer.printInt("attributes", N->getAttributes()); 23450b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 23460b57cec5SDimitry Andric Out << ")"; 23470b57cec5SDimitry Andric } 23480b57cec5SDimitry Andric 23490b57cec5SDimitry Andric static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, 2350349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23510b57cec5SDimitry Andric Out << "!DIImportedEntity("; 2352349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23530b57cec5SDimitry Andric Printer.printTag(N); 23540b57cec5SDimitry Andric Printer.printString("name", N->getName()); 23550b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 23560b57cec5SDimitry Andric Printer.printMetadata("entity", N->getRawEntity()); 23570b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 23580b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 2359349cc55cSDimitry Andric Printer.printMetadata("elements", N->getRawElements()); 23600b57cec5SDimitry Andric Out << ")"; 23610b57cec5SDimitry Andric } 23620b57cec5SDimitry Andric 23630b57cec5SDimitry Andric static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, 2364349cc55cSDimitry Andric AsmWriterContext &Ctx) { 23650b57cec5SDimitry Andric if (Node->isDistinct()) 23660b57cec5SDimitry Andric Out << "distinct "; 23670b57cec5SDimitry Andric else if (Node->isTemporary()) 23680b57cec5SDimitry Andric Out << "<temporary!> "; // Handle broken code. 23690b57cec5SDimitry Andric 23700b57cec5SDimitry Andric switch (Node->getMetadataID()) { 23710b57cec5SDimitry Andric default: 23720b57cec5SDimitry Andric llvm_unreachable("Expected uniquable MDNode"); 23730b57cec5SDimitry Andric #define HANDLE_MDNODE_LEAF(CLASS) \ 23740b57cec5SDimitry Andric case Metadata::CLASS##Kind: \ 2375349cc55cSDimitry Andric write##CLASS(Out, cast<CLASS>(Node), Ctx); \ 23760b57cec5SDimitry Andric break; 23770b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 23780b57cec5SDimitry Andric } 23790b57cec5SDimitry Andric } 23800b57cec5SDimitry Andric 23810b57cec5SDimitry Andric // Full implementation of printing a Value as an operand with support for 23820b57cec5SDimitry Andric // TypePrinting, etc. 23830b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, 2384349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23850b57cec5SDimitry Andric if (V->hasName()) { 23860b57cec5SDimitry Andric PrintLLVMName(Out, V); 23870b57cec5SDimitry Andric return; 23880b57cec5SDimitry Andric } 23890b57cec5SDimitry Andric 23900b57cec5SDimitry Andric const Constant *CV = dyn_cast<Constant>(V); 23910b57cec5SDimitry Andric if (CV && !isa<GlobalValue>(CV)) { 2392349cc55cSDimitry Andric assert(WriterCtx.TypePrinter && "Constants require TypePrinting!"); 2393349cc55cSDimitry Andric WriteConstantInternal(Out, CV, WriterCtx); 23940b57cec5SDimitry Andric return; 23950b57cec5SDimitry Andric } 23960b57cec5SDimitry Andric 23970b57cec5SDimitry Andric if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 23980b57cec5SDimitry Andric Out << "asm "; 23990b57cec5SDimitry Andric if (IA->hasSideEffects()) 24000b57cec5SDimitry Andric Out << "sideeffect "; 24010b57cec5SDimitry Andric if (IA->isAlignStack()) 24020b57cec5SDimitry Andric Out << "alignstack "; 24030b57cec5SDimitry Andric // We don't emit the AD_ATT dialect as it's the assumed default. 24040b57cec5SDimitry Andric if (IA->getDialect() == InlineAsm::AD_Intel) 24050b57cec5SDimitry Andric Out << "inteldialect "; 2406fe6060f1SDimitry Andric if (IA->canThrow()) 2407fe6060f1SDimitry Andric Out << "unwind "; 24080b57cec5SDimitry Andric Out << '"'; 24090b57cec5SDimitry Andric printEscapedString(IA->getAsmString(), Out); 24100b57cec5SDimitry Andric Out << "\", \""; 24110b57cec5SDimitry Andric printEscapedString(IA->getConstraintString(), Out); 24120b57cec5SDimitry Andric Out << '"'; 24130b57cec5SDimitry Andric return; 24140b57cec5SDimitry Andric } 24150b57cec5SDimitry Andric 24160b57cec5SDimitry Andric if (auto *MD = dyn_cast<MetadataAsValue>(V)) { 2417349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD->getMetadata(), WriterCtx, 2418349cc55cSDimitry Andric /* FromValue */ true); 24190b57cec5SDimitry Andric return; 24200b57cec5SDimitry Andric } 24210b57cec5SDimitry Andric 24220b57cec5SDimitry Andric char Prefix = '%'; 24230b57cec5SDimitry Andric int Slot; 2424349cc55cSDimitry Andric auto *Machine = WriterCtx.Machine; 24250b57cec5SDimitry Andric // If we have a SlotTracker, use it. 24260b57cec5SDimitry Andric if (Machine) { 24270b57cec5SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 24280b57cec5SDimitry Andric Slot = Machine->getGlobalSlot(GV); 24290b57cec5SDimitry Andric Prefix = '@'; 24300b57cec5SDimitry Andric } else { 24310b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24320b57cec5SDimitry Andric 24330b57cec5SDimitry Andric // If the local value didn't succeed, then we may be referring to a value 24340b57cec5SDimitry Andric // from a different function. Translate it, as this can happen when using 24350b57cec5SDimitry Andric // address of blocks. 24360b57cec5SDimitry Andric if (Slot == -1) 24370b57cec5SDimitry Andric if ((Machine = createSlotTracker(V))) { 24380b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24390b57cec5SDimitry Andric delete Machine; 24400b57cec5SDimitry Andric } 24410b57cec5SDimitry Andric } 24420b57cec5SDimitry Andric } else if ((Machine = createSlotTracker(V))) { 24430b57cec5SDimitry Andric // Otherwise, create one to get the # and then destroy it. 24440b57cec5SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 24450b57cec5SDimitry Andric Slot = Machine->getGlobalSlot(GV); 24460b57cec5SDimitry Andric Prefix = '@'; 24470b57cec5SDimitry Andric } else { 24480b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24490b57cec5SDimitry Andric } 24500b57cec5SDimitry Andric delete Machine; 24510b57cec5SDimitry Andric Machine = nullptr; 24520b57cec5SDimitry Andric } else { 24530b57cec5SDimitry Andric Slot = -1; 24540b57cec5SDimitry Andric } 24550b57cec5SDimitry Andric 24560b57cec5SDimitry Andric if (Slot != -1) 24570b57cec5SDimitry Andric Out << Prefix << Slot; 24580b57cec5SDimitry Andric else 24590b57cec5SDimitry Andric Out << "<badref>"; 24600b57cec5SDimitry Andric } 24610b57cec5SDimitry Andric 24620b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, 2463349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 24640b57cec5SDimitry Andric bool FromValue) { 2465fe6060f1SDimitry Andric // Write DIExpressions and DIArgLists inline when used as a value. Improves 2466fe6060f1SDimitry Andric // readability of debug info intrinsics. 24670b57cec5SDimitry Andric if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) { 2468349cc55cSDimitry Andric writeDIExpression(Out, Expr, WriterCtx); 24690b57cec5SDimitry Andric return; 24700b57cec5SDimitry Andric } 2471fe6060f1SDimitry Andric if (const DIArgList *ArgList = dyn_cast<DIArgList>(MD)) { 2472349cc55cSDimitry Andric writeDIArgList(Out, ArgList, WriterCtx, FromValue); 2473fe6060f1SDimitry Andric return; 2474fe6060f1SDimitry Andric } 24750b57cec5SDimitry Andric 24760b57cec5SDimitry Andric if (const MDNode *N = dyn_cast<MDNode>(MD)) { 24770b57cec5SDimitry Andric std::unique_ptr<SlotTracker> MachineStorage; 2478349cc55cSDimitry Andric SaveAndRestore<SlotTracker *> SARMachine(WriterCtx.Machine); 2479349cc55cSDimitry Andric if (!WriterCtx.Machine) { 2480349cc55cSDimitry Andric MachineStorage = std::make_unique<SlotTracker>(WriterCtx.Context); 2481349cc55cSDimitry Andric WriterCtx.Machine = MachineStorage.get(); 24820b57cec5SDimitry Andric } 2483349cc55cSDimitry Andric int Slot = WriterCtx.Machine->getMetadataSlot(N); 24840b57cec5SDimitry Andric if (Slot == -1) { 24850b57cec5SDimitry Andric if (const DILocation *Loc = dyn_cast<DILocation>(N)) { 2486349cc55cSDimitry Andric writeDILocation(Out, Loc, WriterCtx); 24870b57cec5SDimitry Andric return; 24880b57cec5SDimitry Andric } 24890b57cec5SDimitry Andric // Give the pointer value instead of "badref", since this comes up all 24900b57cec5SDimitry Andric // the time when debugging. 24910b57cec5SDimitry Andric Out << "<" << N << ">"; 24920b57cec5SDimitry Andric } else 24930b57cec5SDimitry Andric Out << '!' << Slot; 24940b57cec5SDimitry Andric return; 24950b57cec5SDimitry Andric } 24960b57cec5SDimitry Andric 24970b57cec5SDimitry Andric if (const MDString *MDS = dyn_cast<MDString>(MD)) { 24980b57cec5SDimitry Andric Out << "!\""; 24990b57cec5SDimitry Andric printEscapedString(MDS->getString(), Out); 25000b57cec5SDimitry Andric Out << '"'; 25010b57cec5SDimitry Andric return; 25020b57cec5SDimitry Andric } 25030b57cec5SDimitry Andric 25040b57cec5SDimitry Andric auto *V = cast<ValueAsMetadata>(MD); 2505349cc55cSDimitry Andric assert(WriterCtx.TypePrinter && "TypePrinter required for metadata values"); 25060b57cec5SDimitry Andric assert((FromValue || !isa<LocalAsMetadata>(V)) && 25070b57cec5SDimitry Andric "Unexpected function-local metadata outside of value argument"); 25080b57cec5SDimitry Andric 2509349cc55cSDimitry Andric WriterCtx.TypePrinter->print(V->getValue()->getType(), Out); 25100b57cec5SDimitry Andric Out << ' '; 2511349cc55cSDimitry Andric WriteAsOperandInternal(Out, V->getValue(), WriterCtx); 25120b57cec5SDimitry Andric } 25130b57cec5SDimitry Andric 25140b57cec5SDimitry Andric namespace { 25150b57cec5SDimitry Andric 25160b57cec5SDimitry Andric class AssemblyWriter { 25170b57cec5SDimitry Andric formatted_raw_ostream &Out; 25180b57cec5SDimitry Andric const Module *TheModule = nullptr; 25190b57cec5SDimitry Andric const ModuleSummaryIndex *TheIndex = nullptr; 25200b57cec5SDimitry Andric std::unique_ptr<SlotTracker> SlotTrackerStorage; 25210b57cec5SDimitry Andric SlotTracker &Machine; 25220b57cec5SDimitry Andric TypePrinting TypePrinter; 25230b57cec5SDimitry Andric AssemblyAnnotationWriter *AnnotationWriter = nullptr; 25240b57cec5SDimitry Andric SetVector<const Comdat *> Comdats; 25250b57cec5SDimitry Andric bool IsForDebug; 25260b57cec5SDimitry Andric bool ShouldPreserveUseListOrder; 2527fe6060f1SDimitry Andric UseListOrderMap UseListOrders; 25280b57cec5SDimitry Andric SmallVector<StringRef, 8> MDNames; 25290b57cec5SDimitry Andric /// Synchronization scope names registered with LLVMContext. 25300b57cec5SDimitry Andric SmallVector<StringRef, 8> SSNs; 25310b57cec5SDimitry Andric DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap; 25320b57cec5SDimitry Andric 25330b57cec5SDimitry Andric public: 25340b57cec5SDimitry Andric /// Construct an AssemblyWriter with an external SlotTracker 25350b57cec5SDimitry Andric AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M, 25360b57cec5SDimitry Andric AssemblyAnnotationWriter *AAW, bool IsForDebug, 25370b57cec5SDimitry Andric bool ShouldPreserveUseListOrder = false); 25380b57cec5SDimitry Andric 25390b57cec5SDimitry Andric AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 25400b57cec5SDimitry Andric const ModuleSummaryIndex *Index, bool IsForDebug); 25410b57cec5SDimitry Andric 2542349cc55cSDimitry Andric AsmWriterContext getContext() { 2543349cc55cSDimitry Andric return AsmWriterContext(&TypePrinter, &Machine, TheModule); 2544349cc55cSDimitry Andric } 2545349cc55cSDimitry Andric 25460b57cec5SDimitry Andric void printMDNodeBody(const MDNode *MD); 25470b57cec5SDimitry Andric void printNamedMDNode(const NamedMDNode *NMD); 25480b57cec5SDimitry Andric 25490b57cec5SDimitry Andric void printModule(const Module *M); 25500b57cec5SDimitry Andric 25510b57cec5SDimitry Andric void writeOperand(const Value *Op, bool PrintType); 25520b57cec5SDimitry Andric void writeParamOperand(const Value *Operand, AttributeSet Attrs); 25530b57cec5SDimitry Andric void writeOperandBundles(const CallBase *Call); 25540b57cec5SDimitry Andric void writeSyncScope(const LLVMContext &Context, 25550b57cec5SDimitry Andric SyncScope::ID SSID); 25560b57cec5SDimitry Andric void writeAtomic(const LLVMContext &Context, 25570b57cec5SDimitry Andric AtomicOrdering Ordering, 25580b57cec5SDimitry Andric SyncScope::ID SSID); 25590b57cec5SDimitry Andric void writeAtomicCmpXchg(const LLVMContext &Context, 25600b57cec5SDimitry Andric AtomicOrdering SuccessOrdering, 25610b57cec5SDimitry Andric AtomicOrdering FailureOrdering, 25620b57cec5SDimitry Andric SyncScope::ID SSID); 25630b57cec5SDimitry Andric 25640b57cec5SDimitry Andric void writeAllMDNodes(); 25650b57cec5SDimitry Andric void writeMDNode(unsigned Slot, const MDNode *Node); 2566480093f4SDimitry Andric void writeAttribute(const Attribute &Attr, bool InAttrGroup = false); 2567480093f4SDimitry Andric void writeAttributeSet(const AttributeSet &AttrSet, bool InAttrGroup = false); 25680b57cec5SDimitry Andric void writeAllAttributeGroups(); 25690b57cec5SDimitry Andric 25700b57cec5SDimitry Andric void printTypeIdentities(); 25710b57cec5SDimitry Andric void printGlobal(const GlobalVariable *GV); 2572349cc55cSDimitry Andric void printAlias(const GlobalAlias *GA); 2573349cc55cSDimitry Andric void printIFunc(const GlobalIFunc *GI); 25740b57cec5SDimitry Andric void printComdat(const Comdat *C); 25750b57cec5SDimitry Andric void printFunction(const Function *F); 25760b57cec5SDimitry Andric void printArgument(const Argument *FA, AttributeSet Attrs); 25770b57cec5SDimitry Andric void printBasicBlock(const BasicBlock *BB); 25780b57cec5SDimitry Andric void printInstructionLine(const Instruction &I); 25790b57cec5SDimitry Andric void printInstruction(const Instruction &I); 25800b57cec5SDimitry Andric 2581fe6060f1SDimitry Andric void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle); 25820b57cec5SDimitry Andric void printUseLists(const Function *F); 25830b57cec5SDimitry Andric 25840b57cec5SDimitry Andric void printModuleSummaryIndex(); 25850b57cec5SDimitry Andric void printSummaryInfo(unsigned Slot, const ValueInfo &VI); 25860b57cec5SDimitry Andric void printSummary(const GlobalValueSummary &Summary); 25870b57cec5SDimitry Andric void printAliasSummary(const AliasSummary *AS); 25880b57cec5SDimitry Andric void printGlobalVarSummary(const GlobalVarSummary *GS); 25890b57cec5SDimitry Andric void printFunctionSummary(const FunctionSummary *FS); 25900b57cec5SDimitry Andric void printTypeIdSummary(const TypeIdSummary &TIS); 25910b57cec5SDimitry Andric void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI); 25920b57cec5SDimitry Andric void printTypeTestResolution(const TypeTestResolution &TTRes); 25930b57cec5SDimitry Andric void printArgs(const std::vector<uint64_t> &Args); 25940b57cec5SDimitry Andric void printWPDRes(const WholeProgramDevirtResolution &WPDRes); 25950b57cec5SDimitry Andric void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo); 25960b57cec5SDimitry Andric void printVFuncId(const FunctionSummary::VFuncId VFId); 25970b57cec5SDimitry Andric void 25985ffd83dbSDimitry Andric printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> &VCallList, 25990b57cec5SDimitry Andric const char *Tag); 26000b57cec5SDimitry Andric void 26015ffd83dbSDimitry Andric printConstVCalls(const std::vector<FunctionSummary::ConstVCall> &VCallList, 26020b57cec5SDimitry Andric const char *Tag); 26030b57cec5SDimitry Andric 26040b57cec5SDimitry Andric private: 26050b57cec5SDimitry Andric /// Print out metadata attachments. 26060b57cec5SDimitry Andric void printMetadataAttachments( 26070b57cec5SDimitry Andric const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs, 26080b57cec5SDimitry Andric StringRef Separator); 26090b57cec5SDimitry Andric 26100b57cec5SDimitry Andric // printInfoComment - Print a little comment after the instruction indicating 26110b57cec5SDimitry Andric // which slot it occupies. 26120b57cec5SDimitry Andric void printInfoComment(const Value &V); 26130b57cec5SDimitry Andric 26140b57cec5SDimitry Andric // printGCRelocateComment - print comment after call to the gc.relocate 26150b57cec5SDimitry Andric // intrinsic indicating base and derived pointer names. 26160b57cec5SDimitry Andric void printGCRelocateComment(const GCRelocateInst &Relocate); 26170b57cec5SDimitry Andric }; 26180b57cec5SDimitry Andric 26190b57cec5SDimitry Andric } // end anonymous namespace 26200b57cec5SDimitry Andric 26210b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 26220b57cec5SDimitry Andric const Module *M, AssemblyAnnotationWriter *AAW, 26230b57cec5SDimitry Andric bool IsForDebug, bool ShouldPreserveUseListOrder) 26240b57cec5SDimitry Andric : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW), 26250b57cec5SDimitry Andric IsForDebug(IsForDebug), 26260b57cec5SDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { 26270b57cec5SDimitry Andric if (!TheModule) 26280b57cec5SDimitry Andric return; 26290b57cec5SDimitry Andric for (const GlobalObject &GO : TheModule->global_objects()) 26300b57cec5SDimitry Andric if (const Comdat *C = GO.getComdat()) 26310b57cec5SDimitry Andric Comdats.insert(C); 26320b57cec5SDimitry Andric } 26330b57cec5SDimitry Andric 26340b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 26350b57cec5SDimitry Andric const ModuleSummaryIndex *Index, bool IsForDebug) 26360b57cec5SDimitry Andric : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr), 26370b57cec5SDimitry Andric IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {} 26380b57cec5SDimitry Andric 26390b57cec5SDimitry Andric void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) { 26400b57cec5SDimitry Andric if (!Operand) { 26410b57cec5SDimitry Andric Out << "<null operand!>"; 26420b57cec5SDimitry Andric return; 26430b57cec5SDimitry Andric } 26440b57cec5SDimitry Andric if (PrintType) { 26450b57cec5SDimitry Andric TypePrinter.print(Operand->getType(), Out); 26460b57cec5SDimitry Andric Out << ' '; 26470b57cec5SDimitry Andric } 2648349cc55cSDimitry Andric auto WriterCtx = getContext(); 2649349cc55cSDimitry Andric WriteAsOperandInternal(Out, Operand, WriterCtx); 26500b57cec5SDimitry Andric } 26510b57cec5SDimitry Andric 26520b57cec5SDimitry Andric void AssemblyWriter::writeSyncScope(const LLVMContext &Context, 26530b57cec5SDimitry Andric SyncScope::ID SSID) { 26540b57cec5SDimitry Andric switch (SSID) { 26550b57cec5SDimitry Andric case SyncScope::System: { 26560b57cec5SDimitry Andric break; 26570b57cec5SDimitry Andric } 26580b57cec5SDimitry Andric default: { 26590b57cec5SDimitry Andric if (SSNs.empty()) 26600b57cec5SDimitry Andric Context.getSyncScopeNames(SSNs); 26610b57cec5SDimitry Andric 26620b57cec5SDimitry Andric Out << " syncscope(\""; 26630b57cec5SDimitry Andric printEscapedString(SSNs[SSID], Out); 26640b57cec5SDimitry Andric Out << "\")"; 26650b57cec5SDimitry Andric break; 26660b57cec5SDimitry Andric } 26670b57cec5SDimitry Andric } 26680b57cec5SDimitry Andric } 26690b57cec5SDimitry Andric 26700b57cec5SDimitry Andric void AssemblyWriter::writeAtomic(const LLVMContext &Context, 26710b57cec5SDimitry Andric AtomicOrdering Ordering, 26720b57cec5SDimitry Andric SyncScope::ID SSID) { 26730b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic) 26740b57cec5SDimitry Andric return; 26750b57cec5SDimitry Andric 26760b57cec5SDimitry Andric writeSyncScope(Context, SSID); 26770b57cec5SDimitry Andric Out << " " << toIRString(Ordering); 26780b57cec5SDimitry Andric } 26790b57cec5SDimitry Andric 26800b57cec5SDimitry Andric void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context, 26810b57cec5SDimitry Andric AtomicOrdering SuccessOrdering, 26820b57cec5SDimitry Andric AtomicOrdering FailureOrdering, 26830b57cec5SDimitry Andric SyncScope::ID SSID) { 26840b57cec5SDimitry Andric assert(SuccessOrdering != AtomicOrdering::NotAtomic && 26850b57cec5SDimitry Andric FailureOrdering != AtomicOrdering::NotAtomic); 26860b57cec5SDimitry Andric 26870b57cec5SDimitry Andric writeSyncScope(Context, SSID); 26880b57cec5SDimitry Andric Out << " " << toIRString(SuccessOrdering); 26890b57cec5SDimitry Andric Out << " " << toIRString(FailureOrdering); 26900b57cec5SDimitry Andric } 26910b57cec5SDimitry Andric 26920b57cec5SDimitry Andric void AssemblyWriter::writeParamOperand(const Value *Operand, 26930b57cec5SDimitry Andric AttributeSet Attrs) { 26940b57cec5SDimitry Andric if (!Operand) { 26950b57cec5SDimitry Andric Out << "<null operand!>"; 26960b57cec5SDimitry Andric return; 26970b57cec5SDimitry Andric } 26980b57cec5SDimitry Andric 26990b57cec5SDimitry Andric // Print the type 27000b57cec5SDimitry Andric TypePrinter.print(Operand->getType(), Out); 27010b57cec5SDimitry Andric // Print parameter attributes list 2702480093f4SDimitry Andric if (Attrs.hasAttributes()) { 2703480093f4SDimitry Andric Out << ' '; 2704480093f4SDimitry Andric writeAttributeSet(Attrs); 2705480093f4SDimitry Andric } 27060b57cec5SDimitry Andric Out << ' '; 27070b57cec5SDimitry Andric // Print the operand 2708349cc55cSDimitry Andric auto WriterCtx = getContext(); 2709349cc55cSDimitry Andric WriteAsOperandInternal(Out, Operand, WriterCtx); 27100b57cec5SDimitry Andric } 27110b57cec5SDimitry Andric 27120b57cec5SDimitry Andric void AssemblyWriter::writeOperandBundles(const CallBase *Call) { 27130b57cec5SDimitry Andric if (!Call->hasOperandBundles()) 27140b57cec5SDimitry Andric return; 27150b57cec5SDimitry Andric 27160b57cec5SDimitry Andric Out << " [ "; 27170b57cec5SDimitry Andric 27180b57cec5SDimitry Andric bool FirstBundle = true; 27190b57cec5SDimitry Andric for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) { 27200b57cec5SDimitry Andric OperandBundleUse BU = Call->getOperandBundleAt(i); 27210b57cec5SDimitry Andric 27220b57cec5SDimitry Andric if (!FirstBundle) 27230b57cec5SDimitry Andric Out << ", "; 27240b57cec5SDimitry Andric FirstBundle = false; 27250b57cec5SDimitry Andric 27260b57cec5SDimitry Andric Out << '"'; 27270b57cec5SDimitry Andric printEscapedString(BU.getTagName(), Out); 27280b57cec5SDimitry Andric Out << '"'; 27290b57cec5SDimitry Andric 27300b57cec5SDimitry Andric Out << '('; 27310b57cec5SDimitry Andric 27320b57cec5SDimitry Andric bool FirstInput = true; 2733349cc55cSDimitry Andric auto WriterCtx = getContext(); 27340b57cec5SDimitry Andric for (const auto &Input : BU.Inputs) { 27350b57cec5SDimitry Andric if (!FirstInput) 27360b57cec5SDimitry Andric Out << ", "; 27370b57cec5SDimitry Andric FirstInput = false; 27380b57cec5SDimitry Andric 27390b57cec5SDimitry Andric TypePrinter.print(Input->getType(), Out); 27400b57cec5SDimitry Andric Out << " "; 2741349cc55cSDimitry Andric WriteAsOperandInternal(Out, Input, WriterCtx); 27420b57cec5SDimitry Andric } 27430b57cec5SDimitry Andric 27440b57cec5SDimitry Andric Out << ')'; 27450b57cec5SDimitry Andric } 27460b57cec5SDimitry Andric 27470b57cec5SDimitry Andric Out << " ]"; 27480b57cec5SDimitry Andric } 27490b57cec5SDimitry Andric 27500b57cec5SDimitry Andric void AssemblyWriter::printModule(const Module *M) { 27510b57cec5SDimitry Andric Machine.initializeIfNeeded(); 27520b57cec5SDimitry Andric 27530b57cec5SDimitry Andric if (ShouldPreserveUseListOrder) 27540b57cec5SDimitry Andric UseListOrders = predictUseListOrder(M); 27550b57cec5SDimitry Andric 27560b57cec5SDimitry Andric if (!M->getModuleIdentifier().empty() && 27570b57cec5SDimitry Andric // Don't print the ID if it will start a new line (which would 27580b57cec5SDimitry Andric // require a comment char before it). 27590b57cec5SDimitry Andric M->getModuleIdentifier().find('\n') == std::string::npos) 27600b57cec5SDimitry Andric Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; 27610b57cec5SDimitry Andric 27620b57cec5SDimitry Andric if (!M->getSourceFileName().empty()) { 27630b57cec5SDimitry Andric Out << "source_filename = \""; 27640b57cec5SDimitry Andric printEscapedString(M->getSourceFileName(), Out); 27650b57cec5SDimitry Andric Out << "\"\n"; 27660b57cec5SDimitry Andric } 27670b57cec5SDimitry Andric 27680b57cec5SDimitry Andric const std::string &DL = M->getDataLayoutStr(); 27690b57cec5SDimitry Andric if (!DL.empty()) 27700b57cec5SDimitry Andric Out << "target datalayout = \"" << DL << "\"\n"; 27710b57cec5SDimitry Andric if (!M->getTargetTriple().empty()) 27720b57cec5SDimitry Andric Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; 27730b57cec5SDimitry Andric 27740b57cec5SDimitry Andric if (!M->getModuleInlineAsm().empty()) { 27750b57cec5SDimitry Andric Out << '\n'; 27760b57cec5SDimitry Andric 27770b57cec5SDimitry Andric // Split the string into lines, to make it easier to read the .ll file. 27780b57cec5SDimitry Andric StringRef Asm = M->getModuleInlineAsm(); 27790b57cec5SDimitry Andric do { 27800b57cec5SDimitry Andric StringRef Front; 27810b57cec5SDimitry Andric std::tie(Front, Asm) = Asm.split('\n'); 27820b57cec5SDimitry Andric 27830b57cec5SDimitry Andric // We found a newline, print the portion of the asm string from the 27840b57cec5SDimitry Andric // last newline up to this newline. 27850b57cec5SDimitry Andric Out << "module asm \""; 27860b57cec5SDimitry Andric printEscapedString(Front, Out); 27870b57cec5SDimitry Andric Out << "\"\n"; 27880b57cec5SDimitry Andric } while (!Asm.empty()); 27890b57cec5SDimitry Andric } 27900b57cec5SDimitry Andric 27910b57cec5SDimitry Andric printTypeIdentities(); 27920b57cec5SDimitry Andric 27930b57cec5SDimitry Andric // Output all comdats. 27940b57cec5SDimitry Andric if (!Comdats.empty()) 27950b57cec5SDimitry Andric Out << '\n'; 27960b57cec5SDimitry Andric for (const Comdat *C : Comdats) { 27970b57cec5SDimitry Andric printComdat(C); 27980b57cec5SDimitry Andric if (C != Comdats.back()) 27990b57cec5SDimitry Andric Out << '\n'; 28000b57cec5SDimitry Andric } 28010b57cec5SDimitry Andric 28020b57cec5SDimitry Andric // Output all globals. 28030b57cec5SDimitry Andric if (!M->global_empty()) Out << '\n'; 28040b57cec5SDimitry Andric for (const GlobalVariable &GV : M->globals()) { 28050b57cec5SDimitry Andric printGlobal(&GV); Out << '\n'; 28060b57cec5SDimitry Andric } 28070b57cec5SDimitry Andric 28080b57cec5SDimitry Andric // Output all aliases. 28090b57cec5SDimitry Andric if (!M->alias_empty()) Out << "\n"; 28100b57cec5SDimitry Andric for (const GlobalAlias &GA : M->aliases()) 2811349cc55cSDimitry Andric printAlias(&GA); 28120b57cec5SDimitry Andric 28130b57cec5SDimitry Andric // Output all ifuncs. 28140b57cec5SDimitry Andric if (!M->ifunc_empty()) Out << "\n"; 28150b57cec5SDimitry Andric for (const GlobalIFunc &GI : M->ifuncs()) 2816349cc55cSDimitry Andric printIFunc(&GI); 28170b57cec5SDimitry Andric 28180b57cec5SDimitry Andric // Output all of the functions. 281913138422SDimitry Andric for (const Function &F : *M) { 282013138422SDimitry Andric Out << '\n'; 28210b57cec5SDimitry Andric printFunction(&F); 282213138422SDimitry Andric } 2823fe6060f1SDimitry Andric 2824fe6060f1SDimitry Andric // Output global use-lists. 2825fe6060f1SDimitry Andric printUseLists(nullptr); 28260b57cec5SDimitry Andric 28270b57cec5SDimitry Andric // Output all attribute groups. 28280b57cec5SDimitry Andric if (!Machine.as_empty()) { 28290b57cec5SDimitry Andric Out << '\n'; 28300b57cec5SDimitry Andric writeAllAttributeGroups(); 28310b57cec5SDimitry Andric } 28320b57cec5SDimitry Andric 28330b57cec5SDimitry Andric // Output named metadata. 28340b57cec5SDimitry Andric if (!M->named_metadata_empty()) Out << '\n'; 28350b57cec5SDimitry Andric 28360b57cec5SDimitry Andric for (const NamedMDNode &Node : M->named_metadata()) 28370b57cec5SDimitry Andric printNamedMDNode(&Node); 28380b57cec5SDimitry Andric 28390b57cec5SDimitry Andric // Output metadata. 28400b57cec5SDimitry Andric if (!Machine.mdn_empty()) { 28410b57cec5SDimitry Andric Out << '\n'; 28420b57cec5SDimitry Andric writeAllMDNodes(); 28430b57cec5SDimitry Andric } 28440b57cec5SDimitry Andric } 28450b57cec5SDimitry Andric 28460b57cec5SDimitry Andric void AssemblyWriter::printModuleSummaryIndex() { 28470b57cec5SDimitry Andric assert(TheIndex); 28485ffd83dbSDimitry Andric int NumSlots = Machine.initializeIndexIfNeeded(); 28490b57cec5SDimitry Andric 28500b57cec5SDimitry Andric Out << "\n"; 28510b57cec5SDimitry Andric 28520b57cec5SDimitry Andric // Print module path entries. To print in order, add paths to a vector 28530b57cec5SDimitry Andric // indexed by module slot. 28540b57cec5SDimitry Andric std::vector<std::pair<std::string, ModuleHash>> moduleVec; 28555ffd83dbSDimitry Andric std::string RegularLTOModuleName = 28565ffd83dbSDimitry Andric ModuleSummaryIndex::getRegularLTOModuleName(); 28570b57cec5SDimitry Andric moduleVec.resize(TheIndex->modulePaths().size()); 28580b57cec5SDimitry Andric for (auto &ModPath : TheIndex->modulePaths()) 28590b57cec5SDimitry Andric moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair( 28600b57cec5SDimitry Andric // A module id of -1 is a special entry for a regular LTO module created 28610b57cec5SDimitry Andric // during the thin link. 28620b57cec5SDimitry Andric ModPath.second.first == -1u ? RegularLTOModuleName 28635ffd83dbSDimitry Andric : (std::string)std::string(ModPath.first()), 28640b57cec5SDimitry Andric ModPath.second.second); 28650b57cec5SDimitry Andric 28660b57cec5SDimitry Andric unsigned i = 0; 28670b57cec5SDimitry Andric for (auto &ModPair : moduleVec) { 28680b57cec5SDimitry Andric Out << "^" << i++ << " = module: ("; 28690b57cec5SDimitry Andric Out << "path: \""; 28700b57cec5SDimitry Andric printEscapedString(ModPair.first, Out); 28710b57cec5SDimitry Andric Out << "\", hash: ("; 28720b57cec5SDimitry Andric FieldSeparator FS; 28730b57cec5SDimitry Andric for (auto Hash : ModPair.second) 28740b57cec5SDimitry Andric Out << FS << Hash; 28750b57cec5SDimitry Andric Out << "))\n"; 28760b57cec5SDimitry Andric } 28770b57cec5SDimitry Andric 28780b57cec5SDimitry Andric // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer 28790b57cec5SDimitry Andric // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID). 28800b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) { 28810b57cec5SDimitry Andric auto GUID = GlobalList.first; 28820b57cec5SDimitry Andric for (auto &Summary : GlobalList.second.SummaryList) 28830b57cec5SDimitry Andric SummaryToGUIDMap[Summary.get()] = GUID; 28840b57cec5SDimitry Andric } 28850b57cec5SDimitry Andric 28860b57cec5SDimitry Andric // Print the global value summary entries. 28870b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) { 28880b57cec5SDimitry Andric auto GUID = GlobalList.first; 28890b57cec5SDimitry Andric auto VI = TheIndex->getValueInfo(GlobalList); 28900b57cec5SDimitry Andric printSummaryInfo(Machine.getGUIDSlot(GUID), VI); 28910b57cec5SDimitry Andric } 28920b57cec5SDimitry Andric 28930b57cec5SDimitry Andric // Print the TypeIdMap entries. 2894fe6060f1SDimitry Andric for (const auto &TID : TheIndex->typeIds()) { 2895fe6060f1SDimitry Andric Out << "^" << Machine.getTypeIdSlot(TID.second.first) 2896fe6060f1SDimitry Andric << " = typeid: (name: \"" << TID.second.first << "\""; 2897fe6060f1SDimitry Andric printTypeIdSummary(TID.second.second); 2898fe6060f1SDimitry Andric Out << ") ; guid = " << TID.first << "\n"; 28990b57cec5SDimitry Andric } 29000b57cec5SDimitry Andric 29010b57cec5SDimitry Andric // Print the TypeIdCompatibleVtableMap entries. 29020b57cec5SDimitry Andric for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) { 29030b57cec5SDimitry Andric auto GUID = GlobalValue::getGUID(TId.first); 29040b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(GUID) 29050b57cec5SDimitry Andric << " = typeidCompatibleVTable: (name: \"" << TId.first << "\""; 29060b57cec5SDimitry Andric printTypeIdCompatibleVtableSummary(TId.second); 29070b57cec5SDimitry Andric Out << ") ; guid = " << GUID << "\n"; 29080b57cec5SDimitry Andric } 29095ffd83dbSDimitry Andric 29105ffd83dbSDimitry Andric // Don't emit flags when it's not really needed (value is zero by default). 29115ffd83dbSDimitry Andric if (TheIndex->getFlags()) { 29125ffd83dbSDimitry Andric Out << "^" << NumSlots << " = flags: " << TheIndex->getFlags() << "\n"; 29135ffd83dbSDimitry Andric ++NumSlots; 29145ffd83dbSDimitry Andric } 29155ffd83dbSDimitry Andric 29165ffd83dbSDimitry Andric Out << "^" << NumSlots << " = blockcount: " << TheIndex->getBlockCount() 29175ffd83dbSDimitry Andric << "\n"; 29180b57cec5SDimitry Andric } 29190b57cec5SDimitry Andric 29200b57cec5SDimitry Andric static const char * 29210b57cec5SDimitry Andric getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) { 29220b57cec5SDimitry Andric switch (K) { 29230b57cec5SDimitry Andric case WholeProgramDevirtResolution::Indir: 29240b57cec5SDimitry Andric return "indir"; 29250b57cec5SDimitry Andric case WholeProgramDevirtResolution::SingleImpl: 29260b57cec5SDimitry Andric return "singleImpl"; 29270b57cec5SDimitry Andric case WholeProgramDevirtResolution::BranchFunnel: 29280b57cec5SDimitry Andric return "branchFunnel"; 29290b57cec5SDimitry Andric } 29300b57cec5SDimitry Andric llvm_unreachable("invalid WholeProgramDevirtResolution kind"); 29310b57cec5SDimitry Andric } 29320b57cec5SDimitry Andric 29330b57cec5SDimitry Andric static const char *getWholeProgDevirtResByArgKindName( 29340b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::Kind K) { 29350b57cec5SDimitry Andric switch (K) { 29360b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::Indir: 29370b57cec5SDimitry Andric return "indir"; 29380b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::UniformRetVal: 29390b57cec5SDimitry Andric return "uniformRetVal"; 29400b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::UniqueRetVal: 29410b57cec5SDimitry Andric return "uniqueRetVal"; 29420b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::VirtualConstProp: 29430b57cec5SDimitry Andric return "virtualConstProp"; 29440b57cec5SDimitry Andric } 29450b57cec5SDimitry Andric llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind"); 29460b57cec5SDimitry Andric } 29470b57cec5SDimitry Andric 29480b57cec5SDimitry Andric static const char *getTTResKindName(TypeTestResolution::Kind K) { 29490b57cec5SDimitry Andric switch (K) { 29505ffd83dbSDimitry Andric case TypeTestResolution::Unknown: 29515ffd83dbSDimitry Andric return "unknown"; 29520b57cec5SDimitry Andric case TypeTestResolution::Unsat: 29530b57cec5SDimitry Andric return "unsat"; 29540b57cec5SDimitry Andric case TypeTestResolution::ByteArray: 29550b57cec5SDimitry Andric return "byteArray"; 29560b57cec5SDimitry Andric case TypeTestResolution::Inline: 29570b57cec5SDimitry Andric return "inline"; 29580b57cec5SDimitry Andric case TypeTestResolution::Single: 29590b57cec5SDimitry Andric return "single"; 29600b57cec5SDimitry Andric case TypeTestResolution::AllOnes: 29610b57cec5SDimitry Andric return "allOnes"; 29620b57cec5SDimitry Andric } 29630b57cec5SDimitry Andric llvm_unreachable("invalid TypeTestResolution kind"); 29640b57cec5SDimitry Andric } 29650b57cec5SDimitry Andric 29660b57cec5SDimitry Andric void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) { 29670b57cec5SDimitry Andric Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind) 29680b57cec5SDimitry Andric << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth; 29690b57cec5SDimitry Andric 29700b57cec5SDimitry Andric // The following fields are only used if the target does not support the use 29710b57cec5SDimitry Andric // of absolute symbols to store constants. Print only if non-zero. 29720b57cec5SDimitry Andric if (TTRes.AlignLog2) 29730b57cec5SDimitry Andric Out << ", alignLog2: " << TTRes.AlignLog2; 29740b57cec5SDimitry Andric if (TTRes.SizeM1) 29750b57cec5SDimitry Andric Out << ", sizeM1: " << TTRes.SizeM1; 29760b57cec5SDimitry Andric if (TTRes.BitMask) 29770b57cec5SDimitry Andric // BitMask is uint8_t which causes it to print the corresponding char. 29780b57cec5SDimitry Andric Out << ", bitMask: " << (unsigned)TTRes.BitMask; 29790b57cec5SDimitry Andric if (TTRes.InlineBits) 29800b57cec5SDimitry Andric Out << ", inlineBits: " << TTRes.InlineBits; 29810b57cec5SDimitry Andric 29820b57cec5SDimitry Andric Out << ")"; 29830b57cec5SDimitry Andric } 29840b57cec5SDimitry Andric 29850b57cec5SDimitry Andric void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) { 29860b57cec5SDimitry Andric Out << ", summary: ("; 29870b57cec5SDimitry Andric printTypeTestResolution(TIS.TTRes); 29880b57cec5SDimitry Andric if (!TIS.WPDRes.empty()) { 29890b57cec5SDimitry Andric Out << ", wpdResolutions: ("; 29900b57cec5SDimitry Andric FieldSeparator FS; 29910b57cec5SDimitry Andric for (auto &WPDRes : TIS.WPDRes) { 29920b57cec5SDimitry Andric Out << FS; 29930b57cec5SDimitry Andric Out << "(offset: " << WPDRes.first << ", "; 29940b57cec5SDimitry Andric printWPDRes(WPDRes.second); 29950b57cec5SDimitry Andric Out << ")"; 29960b57cec5SDimitry Andric } 29970b57cec5SDimitry Andric Out << ")"; 29980b57cec5SDimitry Andric } 29990b57cec5SDimitry Andric Out << ")"; 30000b57cec5SDimitry Andric } 30010b57cec5SDimitry Andric 30020b57cec5SDimitry Andric void AssemblyWriter::printTypeIdCompatibleVtableSummary( 30030b57cec5SDimitry Andric const TypeIdCompatibleVtableInfo &TI) { 30040b57cec5SDimitry Andric Out << ", summary: ("; 30050b57cec5SDimitry Andric FieldSeparator FS; 30060b57cec5SDimitry Andric for (auto &P : TI) { 30070b57cec5SDimitry Andric Out << FS; 30080b57cec5SDimitry Andric Out << "(offset: " << P.AddressPointOffset << ", "; 30090b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID()); 30100b57cec5SDimitry Andric Out << ")"; 30110b57cec5SDimitry Andric } 30120b57cec5SDimitry Andric Out << ")"; 30130b57cec5SDimitry Andric } 30140b57cec5SDimitry Andric 30150b57cec5SDimitry Andric void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) { 30160b57cec5SDimitry Andric Out << "args: ("; 30170b57cec5SDimitry Andric FieldSeparator FS; 30180b57cec5SDimitry Andric for (auto arg : Args) { 30190b57cec5SDimitry Andric Out << FS; 30200b57cec5SDimitry Andric Out << arg; 30210b57cec5SDimitry Andric } 30220b57cec5SDimitry Andric Out << ")"; 30230b57cec5SDimitry Andric } 30240b57cec5SDimitry Andric 30250b57cec5SDimitry Andric void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) { 30260b57cec5SDimitry Andric Out << "wpdRes: (kind: "; 30270b57cec5SDimitry Andric Out << getWholeProgDevirtResKindName(WPDRes.TheKind); 30280b57cec5SDimitry Andric 30290b57cec5SDimitry Andric if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl) 30300b57cec5SDimitry Andric Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\""; 30310b57cec5SDimitry Andric 30320b57cec5SDimitry Andric if (!WPDRes.ResByArg.empty()) { 30330b57cec5SDimitry Andric Out << ", resByArg: ("; 30340b57cec5SDimitry Andric FieldSeparator FS; 30350b57cec5SDimitry Andric for (auto &ResByArg : WPDRes.ResByArg) { 30360b57cec5SDimitry Andric Out << FS; 30370b57cec5SDimitry Andric printArgs(ResByArg.first); 30380b57cec5SDimitry Andric Out << ", byArg: (kind: "; 30390b57cec5SDimitry Andric Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind); 30400b57cec5SDimitry Andric if (ResByArg.second.TheKind == 30410b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::UniformRetVal || 30420b57cec5SDimitry Andric ResByArg.second.TheKind == 30430b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::UniqueRetVal) 30440b57cec5SDimitry Andric Out << ", info: " << ResByArg.second.Info; 30450b57cec5SDimitry Andric 30460b57cec5SDimitry Andric // The following fields are only used if the target does not support the 30470b57cec5SDimitry Andric // use of absolute symbols to store constants. Print only if non-zero. 30480b57cec5SDimitry Andric if (ResByArg.second.Byte || ResByArg.second.Bit) 30490b57cec5SDimitry Andric Out << ", byte: " << ResByArg.second.Byte 30500b57cec5SDimitry Andric << ", bit: " << ResByArg.second.Bit; 30510b57cec5SDimitry Andric 30520b57cec5SDimitry Andric Out << ")"; 30530b57cec5SDimitry Andric } 30540b57cec5SDimitry Andric Out << ")"; 30550b57cec5SDimitry Andric } 30560b57cec5SDimitry Andric Out << ")"; 30570b57cec5SDimitry Andric } 30580b57cec5SDimitry Andric 30590b57cec5SDimitry Andric static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) { 30600b57cec5SDimitry Andric switch (SK) { 30610b57cec5SDimitry Andric case GlobalValueSummary::AliasKind: 30620b57cec5SDimitry Andric return "alias"; 30630b57cec5SDimitry Andric case GlobalValueSummary::FunctionKind: 30640b57cec5SDimitry Andric return "function"; 30650b57cec5SDimitry Andric case GlobalValueSummary::GlobalVarKind: 30660b57cec5SDimitry Andric return "variable"; 30670b57cec5SDimitry Andric } 30680b57cec5SDimitry Andric llvm_unreachable("invalid summary kind"); 30690b57cec5SDimitry Andric } 30700b57cec5SDimitry Andric 30710b57cec5SDimitry Andric void AssemblyWriter::printAliasSummary(const AliasSummary *AS) { 30720b57cec5SDimitry Andric Out << ", aliasee: "; 30730b57cec5SDimitry Andric // The indexes emitted for distributed backends may not include the 30740b57cec5SDimitry Andric // aliasee summary (only if it is being imported directly). Handle 30750b57cec5SDimitry Andric // that case by just emitting "null" as the aliasee. 30760b57cec5SDimitry Andric if (AS->hasAliasee()) 30770b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]); 30780b57cec5SDimitry Andric else 30790b57cec5SDimitry Andric Out << "null"; 30800b57cec5SDimitry Andric } 30810b57cec5SDimitry Andric 30820b57cec5SDimitry Andric void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) { 30830b57cec5SDimitry Andric auto VTableFuncs = GS->vTableFuncs(); 30845ffd83dbSDimitry Andric Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", " 30855ffd83dbSDimitry Andric << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ", " 30865ffd83dbSDimitry Andric << "constant: " << GS->VarFlags.Constant; 30875ffd83dbSDimitry Andric if (!VTableFuncs.empty()) 30885ffd83dbSDimitry Andric Out << ", " 30895ffd83dbSDimitry Andric << "vcall_visibility: " << GS->VarFlags.VCallVisibility; 30905ffd83dbSDimitry Andric Out << ")"; 30915ffd83dbSDimitry Andric 30920b57cec5SDimitry Andric if (!VTableFuncs.empty()) { 30930b57cec5SDimitry Andric Out << ", vTableFuncs: ("; 30940b57cec5SDimitry Andric FieldSeparator FS; 30950b57cec5SDimitry Andric for (auto &P : VTableFuncs) { 30960b57cec5SDimitry Andric Out << FS; 30970b57cec5SDimitry Andric Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID()) 30980b57cec5SDimitry Andric << ", offset: " << P.VTableOffset; 30990b57cec5SDimitry Andric Out << ")"; 31000b57cec5SDimitry Andric } 31010b57cec5SDimitry Andric Out << ")"; 31020b57cec5SDimitry Andric } 31030b57cec5SDimitry Andric } 31040b57cec5SDimitry Andric 31050b57cec5SDimitry Andric static std::string getLinkageName(GlobalValue::LinkageTypes LT) { 31060b57cec5SDimitry Andric switch (LT) { 31070b57cec5SDimitry Andric case GlobalValue::ExternalLinkage: 31080b57cec5SDimitry Andric return "external"; 31090b57cec5SDimitry Andric case GlobalValue::PrivateLinkage: 31100b57cec5SDimitry Andric return "private"; 31110b57cec5SDimitry Andric case GlobalValue::InternalLinkage: 31120b57cec5SDimitry Andric return "internal"; 31130b57cec5SDimitry Andric case GlobalValue::LinkOnceAnyLinkage: 31140b57cec5SDimitry Andric return "linkonce"; 31150b57cec5SDimitry Andric case GlobalValue::LinkOnceODRLinkage: 31160b57cec5SDimitry Andric return "linkonce_odr"; 31170b57cec5SDimitry Andric case GlobalValue::WeakAnyLinkage: 31180b57cec5SDimitry Andric return "weak"; 31190b57cec5SDimitry Andric case GlobalValue::WeakODRLinkage: 31200b57cec5SDimitry Andric return "weak_odr"; 31210b57cec5SDimitry Andric case GlobalValue::CommonLinkage: 31220b57cec5SDimitry Andric return "common"; 31230b57cec5SDimitry Andric case GlobalValue::AppendingLinkage: 31240b57cec5SDimitry Andric return "appending"; 31250b57cec5SDimitry Andric case GlobalValue::ExternalWeakLinkage: 31260b57cec5SDimitry Andric return "extern_weak"; 31270b57cec5SDimitry Andric case GlobalValue::AvailableExternallyLinkage: 31280b57cec5SDimitry Andric return "available_externally"; 31290b57cec5SDimitry Andric } 31300b57cec5SDimitry Andric llvm_unreachable("invalid linkage"); 31310b57cec5SDimitry Andric } 31320b57cec5SDimitry Andric 31330b57cec5SDimitry Andric // When printing the linkage types in IR where the ExternalLinkage is 31340b57cec5SDimitry Andric // not printed, and other linkage types are expected to be printed with 31350b57cec5SDimitry Andric // a space after the name. 31360b57cec5SDimitry Andric static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) { 31370b57cec5SDimitry Andric if (LT == GlobalValue::ExternalLinkage) 31380b57cec5SDimitry Andric return ""; 31390b57cec5SDimitry Andric return getLinkageName(LT) + " "; 31400b57cec5SDimitry Andric } 31410b57cec5SDimitry Andric 3142fe6060f1SDimitry Andric static const char *getVisibilityName(GlobalValue::VisibilityTypes Vis) { 3143fe6060f1SDimitry Andric switch (Vis) { 3144fe6060f1SDimitry Andric case GlobalValue::DefaultVisibility: 3145fe6060f1SDimitry Andric return "default"; 3146fe6060f1SDimitry Andric case GlobalValue::HiddenVisibility: 3147fe6060f1SDimitry Andric return "hidden"; 3148fe6060f1SDimitry Andric case GlobalValue::ProtectedVisibility: 3149fe6060f1SDimitry Andric return "protected"; 3150fe6060f1SDimitry Andric } 3151fe6060f1SDimitry Andric llvm_unreachable("invalid visibility"); 3152fe6060f1SDimitry Andric } 3153fe6060f1SDimitry Andric 31540b57cec5SDimitry Andric void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) { 31550b57cec5SDimitry Andric Out << ", insts: " << FS->instCount(); 3156349cc55cSDimitry Andric if (FS->fflags().anyFlagSet()) 3157349cc55cSDimitry Andric Out << ", " << FS->fflags(); 31580b57cec5SDimitry Andric 31590b57cec5SDimitry Andric if (!FS->calls().empty()) { 31600b57cec5SDimitry Andric Out << ", calls: ("; 31610b57cec5SDimitry Andric FieldSeparator IFS; 31620b57cec5SDimitry Andric for (auto &Call : FS->calls()) { 31630b57cec5SDimitry Andric Out << IFS; 31640b57cec5SDimitry Andric Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID()); 31650b57cec5SDimitry Andric if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown) 31660b57cec5SDimitry Andric Out << ", hotness: " << getHotnessName(Call.second.getHotness()); 31670b57cec5SDimitry Andric else if (Call.second.RelBlockFreq) 31680b57cec5SDimitry Andric Out << ", relbf: " << Call.second.RelBlockFreq; 31690b57cec5SDimitry Andric Out << ")"; 31700b57cec5SDimitry Andric } 31710b57cec5SDimitry Andric Out << ")"; 31720b57cec5SDimitry Andric } 31730b57cec5SDimitry Andric 31740b57cec5SDimitry Andric if (const auto *TIdInfo = FS->getTypeIdInfo()) 31750b57cec5SDimitry Andric printTypeIdInfo(*TIdInfo); 31765ffd83dbSDimitry Andric 31775ffd83dbSDimitry Andric auto PrintRange = [&](const ConstantRange &Range) { 3178e8d8bef9SDimitry Andric Out << "[" << Range.getSignedMin() << ", " << Range.getSignedMax() << "]"; 31795ffd83dbSDimitry Andric }; 31805ffd83dbSDimitry Andric 31815ffd83dbSDimitry Andric if (!FS->paramAccesses().empty()) { 31825ffd83dbSDimitry Andric Out << ", params: ("; 31835ffd83dbSDimitry Andric FieldSeparator IFS; 31845ffd83dbSDimitry Andric for (auto &PS : FS->paramAccesses()) { 31855ffd83dbSDimitry Andric Out << IFS; 31865ffd83dbSDimitry Andric Out << "(param: " << PS.ParamNo; 31875ffd83dbSDimitry Andric Out << ", offset: "; 31885ffd83dbSDimitry Andric PrintRange(PS.Use); 31895ffd83dbSDimitry Andric if (!PS.Calls.empty()) { 31905ffd83dbSDimitry Andric Out << ", calls: ("; 31915ffd83dbSDimitry Andric FieldSeparator IFS; 31925ffd83dbSDimitry Andric for (auto &Call : PS.Calls) { 31935ffd83dbSDimitry Andric Out << IFS; 3194e8d8bef9SDimitry Andric Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee.getGUID()); 31955ffd83dbSDimitry Andric Out << ", param: " << Call.ParamNo; 31965ffd83dbSDimitry Andric Out << ", offset: "; 31975ffd83dbSDimitry Andric PrintRange(Call.Offsets); 31985ffd83dbSDimitry Andric Out << ")"; 31995ffd83dbSDimitry Andric } 32005ffd83dbSDimitry Andric Out << ")"; 32015ffd83dbSDimitry Andric } 32025ffd83dbSDimitry Andric Out << ")"; 32035ffd83dbSDimitry Andric } 32045ffd83dbSDimitry Andric Out << ")"; 32055ffd83dbSDimitry Andric } 32060b57cec5SDimitry Andric } 32070b57cec5SDimitry Andric 32080b57cec5SDimitry Andric void AssemblyWriter::printTypeIdInfo( 32090b57cec5SDimitry Andric const FunctionSummary::TypeIdInfo &TIDInfo) { 32100b57cec5SDimitry Andric Out << ", typeIdInfo: ("; 32110b57cec5SDimitry Andric FieldSeparator TIDFS; 32120b57cec5SDimitry Andric if (!TIDInfo.TypeTests.empty()) { 32130b57cec5SDimitry Andric Out << TIDFS; 32140b57cec5SDimitry Andric Out << "typeTests: ("; 32150b57cec5SDimitry Andric FieldSeparator FS; 32160b57cec5SDimitry Andric for (auto &GUID : TIDInfo.TypeTests) { 32170b57cec5SDimitry Andric auto TidIter = TheIndex->typeIds().equal_range(GUID); 32180b57cec5SDimitry Andric if (TidIter.first == TidIter.second) { 32190b57cec5SDimitry Andric Out << FS; 32200b57cec5SDimitry Andric Out << GUID; 32210b57cec5SDimitry Andric continue; 32220b57cec5SDimitry Andric } 32230b57cec5SDimitry Andric // Print all type id that correspond to this GUID. 32240b57cec5SDimitry Andric for (auto It = TidIter.first; It != TidIter.second; ++It) { 32250b57cec5SDimitry Andric Out << FS; 32260b57cec5SDimitry Andric auto Slot = Machine.getTypeIdSlot(It->second.first); 32270b57cec5SDimitry Andric assert(Slot != -1); 32280b57cec5SDimitry Andric Out << "^" << Slot; 32290b57cec5SDimitry Andric } 32300b57cec5SDimitry Andric } 32310b57cec5SDimitry Andric Out << ")"; 32320b57cec5SDimitry Andric } 32330b57cec5SDimitry Andric if (!TIDInfo.TypeTestAssumeVCalls.empty()) { 32340b57cec5SDimitry Andric Out << TIDFS; 32350b57cec5SDimitry Andric printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls"); 32360b57cec5SDimitry Andric } 32370b57cec5SDimitry Andric if (!TIDInfo.TypeCheckedLoadVCalls.empty()) { 32380b57cec5SDimitry Andric Out << TIDFS; 32390b57cec5SDimitry Andric printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls"); 32400b57cec5SDimitry Andric } 32410b57cec5SDimitry Andric if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) { 32420b57cec5SDimitry Andric Out << TIDFS; 32430b57cec5SDimitry Andric printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls, 32440b57cec5SDimitry Andric "typeTestAssumeConstVCalls"); 32450b57cec5SDimitry Andric } 32460b57cec5SDimitry Andric if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) { 32470b57cec5SDimitry Andric Out << TIDFS; 32480b57cec5SDimitry Andric printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls, 32490b57cec5SDimitry Andric "typeCheckedLoadConstVCalls"); 32500b57cec5SDimitry Andric } 32510b57cec5SDimitry Andric Out << ")"; 32520b57cec5SDimitry Andric } 32530b57cec5SDimitry Andric 32540b57cec5SDimitry Andric void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) { 32550b57cec5SDimitry Andric auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID); 32560b57cec5SDimitry Andric if (TidIter.first == TidIter.second) { 32570b57cec5SDimitry Andric Out << "vFuncId: ("; 32580b57cec5SDimitry Andric Out << "guid: " << VFId.GUID; 32590b57cec5SDimitry Andric Out << ", offset: " << VFId.Offset; 32600b57cec5SDimitry Andric Out << ")"; 32610b57cec5SDimitry Andric return; 32620b57cec5SDimitry Andric } 32630b57cec5SDimitry Andric // Print all type id that correspond to this GUID. 32640b57cec5SDimitry Andric FieldSeparator FS; 32650b57cec5SDimitry Andric for (auto It = TidIter.first; It != TidIter.second; ++It) { 32660b57cec5SDimitry Andric Out << FS; 32670b57cec5SDimitry Andric Out << "vFuncId: ("; 32680b57cec5SDimitry Andric auto Slot = Machine.getTypeIdSlot(It->second.first); 32690b57cec5SDimitry Andric assert(Slot != -1); 32700b57cec5SDimitry Andric Out << "^" << Slot; 32710b57cec5SDimitry Andric Out << ", offset: " << VFId.Offset; 32720b57cec5SDimitry Andric Out << ")"; 32730b57cec5SDimitry Andric } 32740b57cec5SDimitry Andric } 32750b57cec5SDimitry Andric 32760b57cec5SDimitry Andric void AssemblyWriter::printNonConstVCalls( 32775ffd83dbSDimitry Andric const std::vector<FunctionSummary::VFuncId> &VCallList, const char *Tag) { 32780b57cec5SDimitry Andric Out << Tag << ": ("; 32790b57cec5SDimitry Andric FieldSeparator FS; 32800b57cec5SDimitry Andric for (auto &VFuncId : VCallList) { 32810b57cec5SDimitry Andric Out << FS; 32820b57cec5SDimitry Andric printVFuncId(VFuncId); 32830b57cec5SDimitry Andric } 32840b57cec5SDimitry Andric Out << ")"; 32850b57cec5SDimitry Andric } 32860b57cec5SDimitry Andric 32870b57cec5SDimitry Andric void AssemblyWriter::printConstVCalls( 32885ffd83dbSDimitry Andric const std::vector<FunctionSummary::ConstVCall> &VCallList, 32895ffd83dbSDimitry Andric const char *Tag) { 32900b57cec5SDimitry Andric Out << Tag << ": ("; 32910b57cec5SDimitry Andric FieldSeparator FS; 32920b57cec5SDimitry Andric for (auto &ConstVCall : VCallList) { 32930b57cec5SDimitry Andric Out << FS; 32940b57cec5SDimitry Andric Out << "("; 32950b57cec5SDimitry Andric printVFuncId(ConstVCall.VFunc); 32960b57cec5SDimitry Andric if (!ConstVCall.Args.empty()) { 32970b57cec5SDimitry Andric Out << ", "; 32980b57cec5SDimitry Andric printArgs(ConstVCall.Args); 32990b57cec5SDimitry Andric } 33000b57cec5SDimitry Andric Out << ")"; 33010b57cec5SDimitry Andric } 33020b57cec5SDimitry Andric Out << ")"; 33030b57cec5SDimitry Andric } 33040b57cec5SDimitry Andric 33050b57cec5SDimitry Andric void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) { 33060b57cec5SDimitry Andric GlobalValueSummary::GVFlags GVFlags = Summary.flags(); 33070b57cec5SDimitry Andric GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage; 33080b57cec5SDimitry Andric Out << getSummaryKindName(Summary.getSummaryKind()) << ": "; 33090b57cec5SDimitry Andric Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath()) 33100b57cec5SDimitry Andric << ", flags: ("; 33110b57cec5SDimitry Andric Out << "linkage: " << getLinkageName(LT); 3312fe6060f1SDimitry Andric Out << ", visibility: " 3313fe6060f1SDimitry Andric << getVisibilityName((GlobalValue::VisibilityTypes)GVFlags.Visibility); 33140b57cec5SDimitry Andric Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport; 33150b57cec5SDimitry Andric Out << ", live: " << GVFlags.Live; 33160b57cec5SDimitry Andric Out << ", dsoLocal: " << GVFlags.DSOLocal; 33170b57cec5SDimitry Andric Out << ", canAutoHide: " << GVFlags.CanAutoHide; 33180b57cec5SDimitry Andric Out << ")"; 33190b57cec5SDimitry Andric 33200b57cec5SDimitry Andric if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind) 33210b57cec5SDimitry Andric printAliasSummary(cast<AliasSummary>(&Summary)); 33220b57cec5SDimitry Andric else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind) 33230b57cec5SDimitry Andric printFunctionSummary(cast<FunctionSummary>(&Summary)); 33240b57cec5SDimitry Andric else 33250b57cec5SDimitry Andric printGlobalVarSummary(cast<GlobalVarSummary>(&Summary)); 33260b57cec5SDimitry Andric 33270b57cec5SDimitry Andric auto RefList = Summary.refs(); 33280b57cec5SDimitry Andric if (!RefList.empty()) { 33290b57cec5SDimitry Andric Out << ", refs: ("; 33300b57cec5SDimitry Andric FieldSeparator FS; 33310b57cec5SDimitry Andric for (auto &Ref : RefList) { 33320b57cec5SDimitry Andric Out << FS; 33330b57cec5SDimitry Andric if (Ref.isReadOnly()) 33340b57cec5SDimitry Andric Out << "readonly "; 33350b57cec5SDimitry Andric else if (Ref.isWriteOnly()) 33360b57cec5SDimitry Andric Out << "writeonly "; 33370b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(Ref.getGUID()); 33380b57cec5SDimitry Andric } 33390b57cec5SDimitry Andric Out << ")"; 33400b57cec5SDimitry Andric } 33410b57cec5SDimitry Andric 33420b57cec5SDimitry Andric Out << ")"; 33430b57cec5SDimitry Andric } 33440b57cec5SDimitry Andric 33450b57cec5SDimitry Andric void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) { 33460b57cec5SDimitry Andric Out << "^" << Slot << " = gv: ("; 33470b57cec5SDimitry Andric if (!VI.name().empty()) 33480b57cec5SDimitry Andric Out << "name: \"" << VI.name() << "\""; 33490b57cec5SDimitry Andric else 33500b57cec5SDimitry Andric Out << "guid: " << VI.getGUID(); 33510b57cec5SDimitry Andric if (!VI.getSummaryList().empty()) { 33520b57cec5SDimitry Andric Out << ", summaries: ("; 33530b57cec5SDimitry Andric FieldSeparator FS; 33540b57cec5SDimitry Andric for (auto &Summary : VI.getSummaryList()) { 33550b57cec5SDimitry Andric Out << FS; 33560b57cec5SDimitry Andric printSummary(*Summary); 33570b57cec5SDimitry Andric } 33580b57cec5SDimitry Andric Out << ")"; 33590b57cec5SDimitry Andric } 33600b57cec5SDimitry Andric Out << ")"; 33610b57cec5SDimitry Andric if (!VI.name().empty()) 33620b57cec5SDimitry Andric Out << " ; guid = " << VI.getGUID(); 33630b57cec5SDimitry Andric Out << "\n"; 33640b57cec5SDimitry Andric } 33650b57cec5SDimitry Andric 33660b57cec5SDimitry Andric static void printMetadataIdentifier(StringRef Name, 33670b57cec5SDimitry Andric formatted_raw_ostream &Out) { 33680b57cec5SDimitry Andric if (Name.empty()) { 33690b57cec5SDimitry Andric Out << "<empty name> "; 33700b57cec5SDimitry Andric } else { 33710b57cec5SDimitry Andric if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' || 33720b57cec5SDimitry Andric Name[0] == '$' || Name[0] == '.' || Name[0] == '_') 33730b57cec5SDimitry Andric Out << Name[0]; 33740b57cec5SDimitry Andric else 33750b57cec5SDimitry Andric Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F); 33760b57cec5SDimitry Andric for (unsigned i = 1, e = Name.size(); i != e; ++i) { 33770b57cec5SDimitry Andric unsigned char C = Name[i]; 33780b57cec5SDimitry Andric if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' || 33790b57cec5SDimitry Andric C == '.' || C == '_') 33800b57cec5SDimitry Andric Out << C; 33810b57cec5SDimitry Andric else 33820b57cec5SDimitry Andric Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); 33830b57cec5SDimitry Andric } 33840b57cec5SDimitry Andric } 33850b57cec5SDimitry Andric } 33860b57cec5SDimitry Andric 33870b57cec5SDimitry Andric void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) { 33880b57cec5SDimitry Andric Out << '!'; 33890b57cec5SDimitry Andric printMetadataIdentifier(NMD->getName(), Out); 33900b57cec5SDimitry Andric Out << " = !{"; 33910b57cec5SDimitry Andric for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { 33920b57cec5SDimitry Andric if (i) 33930b57cec5SDimitry Andric Out << ", "; 33940b57cec5SDimitry Andric 33950b57cec5SDimitry Andric // Write DIExpressions inline. 33960b57cec5SDimitry Andric // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose. 33970b57cec5SDimitry Andric MDNode *Op = NMD->getOperand(i); 3398fe6060f1SDimitry Andric assert(!isa<DIArgList>(Op) && 3399fe6060f1SDimitry Andric "DIArgLists should not appear in NamedMDNodes"); 34000b57cec5SDimitry Andric if (auto *Expr = dyn_cast<DIExpression>(Op)) { 3401349cc55cSDimitry Andric writeDIExpression(Out, Expr, AsmWriterContext::getEmpty()); 34020b57cec5SDimitry Andric continue; 34030b57cec5SDimitry Andric } 34040b57cec5SDimitry Andric 34050b57cec5SDimitry Andric int Slot = Machine.getMetadataSlot(Op); 34060b57cec5SDimitry Andric if (Slot == -1) 34070b57cec5SDimitry Andric Out << "<badref>"; 34080b57cec5SDimitry Andric else 34090b57cec5SDimitry Andric Out << '!' << Slot; 34100b57cec5SDimitry Andric } 34110b57cec5SDimitry Andric Out << "}\n"; 34120b57cec5SDimitry Andric } 34130b57cec5SDimitry Andric 34140b57cec5SDimitry Andric static void PrintVisibility(GlobalValue::VisibilityTypes Vis, 34150b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34160b57cec5SDimitry Andric switch (Vis) { 34170b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: break; 34180b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: Out << "hidden "; break; 34190b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: Out << "protected "; break; 34200b57cec5SDimitry Andric } 34210b57cec5SDimitry Andric } 34220b57cec5SDimitry Andric 34230b57cec5SDimitry Andric static void PrintDSOLocation(const GlobalValue &GV, 34240b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34255ffd83dbSDimitry Andric if (GV.isDSOLocal() && !GV.isImplicitDSOLocal()) 34260b57cec5SDimitry Andric Out << "dso_local "; 34270b57cec5SDimitry Andric } 34280b57cec5SDimitry Andric 34290b57cec5SDimitry Andric static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT, 34300b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34310b57cec5SDimitry Andric switch (SCT) { 34320b57cec5SDimitry Andric case GlobalValue::DefaultStorageClass: break; 34330b57cec5SDimitry Andric case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break; 34340b57cec5SDimitry Andric case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break; 34350b57cec5SDimitry Andric } 34360b57cec5SDimitry Andric } 34370b57cec5SDimitry Andric 34380b57cec5SDimitry Andric static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, 34390b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34400b57cec5SDimitry Andric switch (TLM) { 34410b57cec5SDimitry Andric case GlobalVariable::NotThreadLocal: 34420b57cec5SDimitry Andric break; 34430b57cec5SDimitry Andric case GlobalVariable::GeneralDynamicTLSModel: 34440b57cec5SDimitry Andric Out << "thread_local "; 34450b57cec5SDimitry Andric break; 34460b57cec5SDimitry Andric case GlobalVariable::LocalDynamicTLSModel: 34470b57cec5SDimitry Andric Out << "thread_local(localdynamic) "; 34480b57cec5SDimitry Andric break; 34490b57cec5SDimitry Andric case GlobalVariable::InitialExecTLSModel: 34500b57cec5SDimitry Andric Out << "thread_local(initialexec) "; 34510b57cec5SDimitry Andric break; 34520b57cec5SDimitry Andric case GlobalVariable::LocalExecTLSModel: 34530b57cec5SDimitry Andric Out << "thread_local(localexec) "; 34540b57cec5SDimitry Andric break; 34550b57cec5SDimitry Andric } 34560b57cec5SDimitry Andric } 34570b57cec5SDimitry Andric 34580b57cec5SDimitry Andric static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) { 34590b57cec5SDimitry Andric switch (UA) { 34600b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::None: 34610b57cec5SDimitry Andric return ""; 34620b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::Local: 34630b57cec5SDimitry Andric return "local_unnamed_addr"; 34640b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::Global: 34650b57cec5SDimitry Andric return "unnamed_addr"; 34660b57cec5SDimitry Andric } 34670b57cec5SDimitry Andric llvm_unreachable("Unknown UnnamedAddr"); 34680b57cec5SDimitry Andric } 34690b57cec5SDimitry Andric 34700b57cec5SDimitry Andric static void maybePrintComdat(formatted_raw_ostream &Out, 34710b57cec5SDimitry Andric const GlobalObject &GO) { 34720b57cec5SDimitry Andric const Comdat *C = GO.getComdat(); 34730b57cec5SDimitry Andric if (!C) 34740b57cec5SDimitry Andric return; 34750b57cec5SDimitry Andric 34760b57cec5SDimitry Andric if (isa<GlobalVariable>(GO)) 34770b57cec5SDimitry Andric Out << ','; 34780b57cec5SDimitry Andric Out << " comdat"; 34790b57cec5SDimitry Andric 34800b57cec5SDimitry Andric if (GO.getName() == C->getName()) 34810b57cec5SDimitry Andric return; 34820b57cec5SDimitry Andric 34830b57cec5SDimitry Andric Out << '('; 34840b57cec5SDimitry Andric PrintLLVMName(Out, C->getName(), ComdatPrefix); 34850b57cec5SDimitry Andric Out << ')'; 34860b57cec5SDimitry Andric } 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric void AssemblyWriter::printGlobal(const GlobalVariable *GV) { 34890b57cec5SDimitry Andric if (GV->isMaterializable()) 34900b57cec5SDimitry Andric Out << "; Materializable\n"; 34910b57cec5SDimitry Andric 3492349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GV->getParent()); 3493349cc55cSDimitry Andric WriteAsOperandInternal(Out, GV, WriterCtx); 34940b57cec5SDimitry Andric Out << " = "; 34950b57cec5SDimitry Andric 34960b57cec5SDimitry Andric if (!GV->hasInitializer() && GV->hasExternalLinkage()) 34970b57cec5SDimitry Andric Out << "external "; 34980b57cec5SDimitry Andric 34990b57cec5SDimitry Andric Out << getLinkageNameWithSpace(GV->getLinkage()); 35000b57cec5SDimitry Andric PrintDSOLocation(*GV, Out); 35010b57cec5SDimitry Andric PrintVisibility(GV->getVisibility(), Out); 35020b57cec5SDimitry Andric PrintDLLStorageClass(GV->getDLLStorageClass(), Out); 35030b57cec5SDimitry Andric PrintThreadLocalModel(GV->getThreadLocalMode(), Out); 35040b57cec5SDimitry Andric StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr()); 35050b57cec5SDimitry Andric if (!UA.empty()) 35060b57cec5SDimitry Andric Out << UA << ' '; 35070b57cec5SDimitry Andric 35080b57cec5SDimitry Andric if (unsigned AddressSpace = GV->getType()->getAddressSpace()) 35090b57cec5SDimitry Andric Out << "addrspace(" << AddressSpace << ") "; 35100b57cec5SDimitry Andric if (GV->isExternallyInitialized()) Out << "externally_initialized "; 35110b57cec5SDimitry Andric Out << (GV->isConstant() ? "constant " : "global "); 35120b57cec5SDimitry Andric TypePrinter.print(GV->getValueType(), Out); 35130b57cec5SDimitry Andric 35140b57cec5SDimitry Andric if (GV->hasInitializer()) { 35150b57cec5SDimitry Andric Out << ' '; 35160b57cec5SDimitry Andric writeOperand(GV->getInitializer(), false); 35170b57cec5SDimitry Andric } 35180b57cec5SDimitry Andric 35190b57cec5SDimitry Andric if (GV->hasSection()) { 35200b57cec5SDimitry Andric Out << ", section \""; 35210b57cec5SDimitry Andric printEscapedString(GV->getSection(), Out); 35220b57cec5SDimitry Andric Out << '"'; 35230b57cec5SDimitry Andric } 35240b57cec5SDimitry Andric if (GV->hasPartition()) { 35250b57cec5SDimitry Andric Out << ", partition \""; 35260b57cec5SDimitry Andric printEscapedString(GV->getPartition(), Out); 35270b57cec5SDimitry Andric Out << '"'; 35280b57cec5SDimitry Andric } 35290b57cec5SDimitry Andric 35300b57cec5SDimitry Andric maybePrintComdat(Out, *GV); 35310b57cec5SDimitry Andric if (GV->getAlignment()) 35320b57cec5SDimitry Andric Out << ", align " << GV->getAlignment(); 35330b57cec5SDimitry Andric 35340b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 35350b57cec5SDimitry Andric GV->getAllMetadata(MDs); 35360b57cec5SDimitry Andric printMetadataAttachments(MDs, ", "); 35370b57cec5SDimitry Andric 35380b57cec5SDimitry Andric auto Attrs = GV->getAttributes(); 35390b57cec5SDimitry Andric if (Attrs.hasAttributes()) 35400b57cec5SDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(Attrs); 35410b57cec5SDimitry Andric 35420b57cec5SDimitry Andric printInfoComment(*GV); 35430b57cec5SDimitry Andric } 35440b57cec5SDimitry Andric 3545349cc55cSDimitry Andric void AssemblyWriter::printAlias(const GlobalAlias *GA) { 3546349cc55cSDimitry Andric if (GA->isMaterializable()) 35470b57cec5SDimitry Andric Out << "; Materializable\n"; 35480b57cec5SDimitry Andric 3549349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GA->getParent()); 3550349cc55cSDimitry Andric WriteAsOperandInternal(Out, GA, WriterCtx); 35510b57cec5SDimitry Andric Out << " = "; 35520b57cec5SDimitry Andric 3553349cc55cSDimitry Andric Out << getLinkageNameWithSpace(GA->getLinkage()); 3554349cc55cSDimitry Andric PrintDSOLocation(*GA, Out); 3555349cc55cSDimitry Andric PrintVisibility(GA->getVisibility(), Out); 3556349cc55cSDimitry Andric PrintDLLStorageClass(GA->getDLLStorageClass(), Out); 3557349cc55cSDimitry Andric PrintThreadLocalModel(GA->getThreadLocalMode(), Out); 3558349cc55cSDimitry Andric StringRef UA = getUnnamedAddrEncoding(GA->getUnnamedAddr()); 35590b57cec5SDimitry Andric if (!UA.empty()) 35600b57cec5SDimitry Andric Out << UA << ' '; 35610b57cec5SDimitry Andric 35620b57cec5SDimitry Andric Out << "alias "; 35630b57cec5SDimitry Andric 3564349cc55cSDimitry Andric TypePrinter.print(GA->getValueType(), Out); 35650b57cec5SDimitry Andric Out << ", "; 35660b57cec5SDimitry Andric 3567349cc55cSDimitry Andric if (const Constant *Aliasee = GA->getAliasee()) { 3568349cc55cSDimitry Andric writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee)); 35690b57cec5SDimitry Andric } else { 3570349cc55cSDimitry Andric TypePrinter.print(GA->getType(), Out); 3571349cc55cSDimitry Andric Out << " <<NULL ALIASEE>>"; 35720b57cec5SDimitry Andric } 35730b57cec5SDimitry Andric 3574349cc55cSDimitry Andric if (GA->hasPartition()) { 35750b57cec5SDimitry Andric Out << ", partition \""; 3576349cc55cSDimitry Andric printEscapedString(GA->getPartition(), Out); 35770b57cec5SDimitry Andric Out << '"'; 35780b57cec5SDimitry Andric } 35790b57cec5SDimitry Andric 3580349cc55cSDimitry Andric printInfoComment(*GA); 3581349cc55cSDimitry Andric Out << '\n'; 3582349cc55cSDimitry Andric } 3583349cc55cSDimitry Andric 3584349cc55cSDimitry Andric void AssemblyWriter::printIFunc(const GlobalIFunc *GI) { 3585349cc55cSDimitry Andric if (GI->isMaterializable()) 3586349cc55cSDimitry Andric Out << "; Materializable\n"; 3587349cc55cSDimitry Andric 3588349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GI->getParent()); 3589349cc55cSDimitry Andric WriteAsOperandInternal(Out, GI, WriterCtx); 3590349cc55cSDimitry Andric Out << " = "; 3591349cc55cSDimitry Andric 3592349cc55cSDimitry Andric Out << getLinkageNameWithSpace(GI->getLinkage()); 3593349cc55cSDimitry Andric PrintDSOLocation(*GI, Out); 3594349cc55cSDimitry Andric PrintVisibility(GI->getVisibility(), Out); 3595349cc55cSDimitry Andric 3596349cc55cSDimitry Andric Out << "ifunc "; 3597349cc55cSDimitry Andric 3598349cc55cSDimitry Andric TypePrinter.print(GI->getValueType(), Out); 3599349cc55cSDimitry Andric Out << ", "; 3600349cc55cSDimitry Andric 3601349cc55cSDimitry Andric if (const Constant *Resolver = GI->getResolver()) { 3602349cc55cSDimitry Andric writeOperand(Resolver, !isa<ConstantExpr>(Resolver)); 3603349cc55cSDimitry Andric } else { 3604349cc55cSDimitry Andric TypePrinter.print(GI->getType(), Out); 3605349cc55cSDimitry Andric Out << " <<NULL RESOLVER>>"; 3606349cc55cSDimitry Andric } 3607349cc55cSDimitry Andric 3608349cc55cSDimitry Andric if (GI->hasPartition()) { 3609349cc55cSDimitry Andric Out << ", partition \""; 3610349cc55cSDimitry Andric printEscapedString(GI->getPartition(), Out); 3611349cc55cSDimitry Andric Out << '"'; 3612349cc55cSDimitry Andric } 3613349cc55cSDimitry Andric 3614349cc55cSDimitry Andric printInfoComment(*GI); 36150b57cec5SDimitry Andric Out << '\n'; 36160b57cec5SDimitry Andric } 36170b57cec5SDimitry Andric 36180b57cec5SDimitry Andric void AssemblyWriter::printComdat(const Comdat *C) { 36190b57cec5SDimitry Andric C->print(Out); 36200b57cec5SDimitry Andric } 36210b57cec5SDimitry Andric 36220b57cec5SDimitry Andric void AssemblyWriter::printTypeIdentities() { 36230b57cec5SDimitry Andric if (TypePrinter.empty()) 36240b57cec5SDimitry Andric return; 36250b57cec5SDimitry Andric 36260b57cec5SDimitry Andric Out << '\n'; 36270b57cec5SDimitry Andric 36280b57cec5SDimitry Andric // Emit all numbered types. 36290b57cec5SDimitry Andric auto &NumberedTypes = TypePrinter.getNumberedTypes(); 36300b57cec5SDimitry Andric for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) { 36310b57cec5SDimitry Andric Out << '%' << I << " = type "; 36320b57cec5SDimitry Andric 36330b57cec5SDimitry Andric // Make sure we print out at least one level of the type structure, so 36340b57cec5SDimitry Andric // that we do not get %2 = type %2 36350b57cec5SDimitry Andric TypePrinter.printStructBody(NumberedTypes[I], Out); 36360b57cec5SDimitry Andric Out << '\n'; 36370b57cec5SDimitry Andric } 36380b57cec5SDimitry Andric 36390b57cec5SDimitry Andric auto &NamedTypes = TypePrinter.getNamedTypes(); 36400b57cec5SDimitry Andric for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) { 36410b57cec5SDimitry Andric PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix); 36420b57cec5SDimitry Andric Out << " = type "; 36430b57cec5SDimitry Andric 36440b57cec5SDimitry Andric // Make sure we print out at least one level of the type structure, so 36450b57cec5SDimitry Andric // that we do not get %FILE = type %FILE 36460b57cec5SDimitry Andric TypePrinter.printStructBody(NamedTypes[I], Out); 36470b57cec5SDimitry Andric Out << '\n'; 36480b57cec5SDimitry Andric } 36490b57cec5SDimitry Andric } 36500b57cec5SDimitry Andric 36510b57cec5SDimitry Andric /// printFunction - Print all aspects of a function. 36520b57cec5SDimitry Andric void AssemblyWriter::printFunction(const Function *F) { 36530b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); 36540b57cec5SDimitry Andric 36550b57cec5SDimitry Andric if (F->isMaterializable()) 36560b57cec5SDimitry Andric Out << "; Materializable\n"; 36570b57cec5SDimitry Andric 36580b57cec5SDimitry Andric const AttributeList &Attrs = F->getAttributes(); 3659349cc55cSDimitry Andric if (Attrs.hasFnAttrs()) { 3660349cc55cSDimitry Andric AttributeSet AS = Attrs.getFnAttrs(); 36610b57cec5SDimitry Andric std::string AttrStr; 36620b57cec5SDimitry Andric 36630b57cec5SDimitry Andric for (const Attribute &Attr : AS) { 36640b57cec5SDimitry Andric if (!Attr.isStringAttribute()) { 36650b57cec5SDimitry Andric if (!AttrStr.empty()) AttrStr += ' '; 36660b57cec5SDimitry Andric AttrStr += Attr.getAsString(); 36670b57cec5SDimitry Andric } 36680b57cec5SDimitry Andric } 36690b57cec5SDimitry Andric 36700b57cec5SDimitry Andric if (!AttrStr.empty()) 36710b57cec5SDimitry Andric Out << "; Function Attrs: " << AttrStr << '\n'; 36720b57cec5SDimitry Andric } 36730b57cec5SDimitry Andric 36740b57cec5SDimitry Andric Machine.incorporateFunction(F); 36750b57cec5SDimitry Andric 36760b57cec5SDimitry Andric if (F->isDeclaration()) { 36770b57cec5SDimitry Andric Out << "declare"; 36780b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 36790b57cec5SDimitry Andric F->getAllMetadata(MDs); 36800b57cec5SDimitry Andric printMetadataAttachments(MDs, " "); 36810b57cec5SDimitry Andric Out << ' '; 36820b57cec5SDimitry Andric } else 36830b57cec5SDimitry Andric Out << "define "; 36840b57cec5SDimitry Andric 36850b57cec5SDimitry Andric Out << getLinkageNameWithSpace(F->getLinkage()); 36860b57cec5SDimitry Andric PrintDSOLocation(*F, Out); 36870b57cec5SDimitry Andric PrintVisibility(F->getVisibility(), Out); 36880b57cec5SDimitry Andric PrintDLLStorageClass(F->getDLLStorageClass(), Out); 36890b57cec5SDimitry Andric 36900b57cec5SDimitry Andric // Print the calling convention. 36910b57cec5SDimitry Andric if (F->getCallingConv() != CallingConv::C) { 36920b57cec5SDimitry Andric PrintCallingConv(F->getCallingConv(), Out); 36930b57cec5SDimitry Andric Out << " "; 36940b57cec5SDimitry Andric } 36950b57cec5SDimitry Andric 36960b57cec5SDimitry Andric FunctionType *FT = F->getFunctionType(); 3697349cc55cSDimitry Andric if (Attrs.hasRetAttrs()) 36980b57cec5SDimitry Andric Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' '; 36990b57cec5SDimitry Andric TypePrinter.print(F->getReturnType(), Out); 3700349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, F->getParent()); 37010b57cec5SDimitry Andric Out << ' '; 3702349cc55cSDimitry Andric WriteAsOperandInternal(Out, F, WriterCtx); 37030b57cec5SDimitry Andric Out << '('; 37040b57cec5SDimitry Andric 37050b57cec5SDimitry Andric // Loop over the arguments, printing them... 37060b57cec5SDimitry Andric if (F->isDeclaration() && !IsForDebug) { 37070b57cec5SDimitry Andric // We're only interested in the type here - don't print argument names. 37080b57cec5SDimitry Andric for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) { 37090b57cec5SDimitry Andric // Insert commas as we go... the first arg doesn't get a comma 37100b57cec5SDimitry Andric if (I) 37110b57cec5SDimitry Andric Out << ", "; 37120b57cec5SDimitry Andric // Output type... 37130b57cec5SDimitry Andric TypePrinter.print(FT->getParamType(I), Out); 37140b57cec5SDimitry Andric 3715349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(I); 3716480093f4SDimitry Andric if (ArgAttrs.hasAttributes()) { 3717480093f4SDimitry Andric Out << ' '; 3718480093f4SDimitry Andric writeAttributeSet(ArgAttrs); 3719480093f4SDimitry Andric } 37200b57cec5SDimitry Andric } 37210b57cec5SDimitry Andric } else { 37220b57cec5SDimitry Andric // The arguments are meaningful here, print them in detail. 37230b57cec5SDimitry Andric for (const Argument &Arg : F->args()) { 37240b57cec5SDimitry Andric // Insert commas as we go... the first arg doesn't get a comma 37250b57cec5SDimitry Andric if (Arg.getArgNo() != 0) 37260b57cec5SDimitry Andric Out << ", "; 3727349cc55cSDimitry Andric printArgument(&Arg, Attrs.getParamAttrs(Arg.getArgNo())); 37280b57cec5SDimitry Andric } 37290b57cec5SDimitry Andric } 37300b57cec5SDimitry Andric 37310b57cec5SDimitry Andric // Finish printing arguments... 37320b57cec5SDimitry Andric if (FT->isVarArg()) { 37330b57cec5SDimitry Andric if (FT->getNumParams()) Out << ", "; 37340b57cec5SDimitry Andric Out << "..."; // Output varargs portion of signature! 37350b57cec5SDimitry Andric } 37360b57cec5SDimitry Andric Out << ')'; 37370b57cec5SDimitry Andric StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr()); 37380b57cec5SDimitry Andric if (!UA.empty()) 37390b57cec5SDimitry Andric Out << ' ' << UA; 37400b57cec5SDimitry Andric // We print the function address space if it is non-zero or if we are writing 37410b57cec5SDimitry Andric // a module with a non-zero program address space or if there is no valid 37420b57cec5SDimitry Andric // Module* so that the file can be parsed without the datalayout string. 37430b57cec5SDimitry Andric const Module *Mod = F->getParent(); 37440b57cec5SDimitry Andric if (F->getAddressSpace() != 0 || !Mod || 37450b57cec5SDimitry Andric Mod->getDataLayout().getProgramAddressSpace() != 0) 37460b57cec5SDimitry Andric Out << " addrspace(" << F->getAddressSpace() << ")"; 3747349cc55cSDimitry Andric if (Attrs.hasFnAttrs()) 3748349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttrs()); 37490b57cec5SDimitry Andric if (F->hasSection()) { 37500b57cec5SDimitry Andric Out << " section \""; 37510b57cec5SDimitry Andric printEscapedString(F->getSection(), Out); 37520b57cec5SDimitry Andric Out << '"'; 37530b57cec5SDimitry Andric } 37540b57cec5SDimitry Andric if (F->hasPartition()) { 37550b57cec5SDimitry Andric Out << " partition \""; 37560b57cec5SDimitry Andric printEscapedString(F->getPartition(), Out); 37570b57cec5SDimitry Andric Out << '"'; 37580b57cec5SDimitry Andric } 37590b57cec5SDimitry Andric maybePrintComdat(Out, *F); 37600b57cec5SDimitry Andric if (F->getAlignment()) 37610b57cec5SDimitry Andric Out << " align " << F->getAlignment(); 37620b57cec5SDimitry Andric if (F->hasGC()) 37630b57cec5SDimitry Andric Out << " gc \"" << F->getGC() << '"'; 37640b57cec5SDimitry Andric if (F->hasPrefixData()) { 37650b57cec5SDimitry Andric Out << " prefix "; 37660b57cec5SDimitry Andric writeOperand(F->getPrefixData(), true); 37670b57cec5SDimitry Andric } 37680b57cec5SDimitry Andric if (F->hasPrologueData()) { 37690b57cec5SDimitry Andric Out << " prologue "; 37700b57cec5SDimitry Andric writeOperand(F->getPrologueData(), true); 37710b57cec5SDimitry Andric } 37720b57cec5SDimitry Andric if (F->hasPersonalityFn()) { 37730b57cec5SDimitry Andric Out << " personality "; 37740b57cec5SDimitry Andric writeOperand(F->getPersonalityFn(), /*PrintType=*/true); 37750b57cec5SDimitry Andric } 37760b57cec5SDimitry Andric 37770b57cec5SDimitry Andric if (F->isDeclaration()) { 37780b57cec5SDimitry Andric Out << '\n'; 37790b57cec5SDimitry Andric } else { 37800b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 37810b57cec5SDimitry Andric F->getAllMetadata(MDs); 37820b57cec5SDimitry Andric printMetadataAttachments(MDs, " "); 37830b57cec5SDimitry Andric 37840b57cec5SDimitry Andric Out << " {"; 37850b57cec5SDimitry Andric // Output all of the function's basic blocks. 37860b57cec5SDimitry Andric for (const BasicBlock &BB : *F) 37870b57cec5SDimitry Andric printBasicBlock(&BB); 37880b57cec5SDimitry Andric 37890b57cec5SDimitry Andric // Output the function's use-lists. 37900b57cec5SDimitry Andric printUseLists(F); 37910b57cec5SDimitry Andric 37920b57cec5SDimitry Andric Out << "}\n"; 37930b57cec5SDimitry Andric } 37940b57cec5SDimitry Andric 37950b57cec5SDimitry Andric Machine.purgeFunction(); 37960b57cec5SDimitry Andric } 37970b57cec5SDimitry Andric 37980b57cec5SDimitry Andric /// printArgument - This member is called for every argument that is passed into 37990b57cec5SDimitry Andric /// the function. Simply print it out 38000b57cec5SDimitry Andric void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) { 38010b57cec5SDimitry Andric // Output type... 38020b57cec5SDimitry Andric TypePrinter.print(Arg->getType(), Out); 38030b57cec5SDimitry Andric 38040b57cec5SDimitry Andric // Output parameter attributes list 3805480093f4SDimitry Andric if (Attrs.hasAttributes()) { 3806480093f4SDimitry Andric Out << ' '; 3807480093f4SDimitry Andric writeAttributeSet(Attrs); 3808480093f4SDimitry Andric } 38090b57cec5SDimitry Andric 38100b57cec5SDimitry Andric // Output name, if available... 38110b57cec5SDimitry Andric if (Arg->hasName()) { 38120b57cec5SDimitry Andric Out << ' '; 38130b57cec5SDimitry Andric PrintLLVMName(Out, Arg); 38148bcb0991SDimitry Andric } else { 38158bcb0991SDimitry Andric int Slot = Machine.getLocalSlot(Arg); 38168bcb0991SDimitry Andric assert(Slot != -1 && "expect argument in function here"); 38178bcb0991SDimitry Andric Out << " %" << Slot; 38180b57cec5SDimitry Andric } 38190b57cec5SDimitry Andric } 38200b57cec5SDimitry Andric 38210b57cec5SDimitry Andric /// printBasicBlock - This member is called for each basic block in a method. 38220b57cec5SDimitry Andric void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { 3823fe6060f1SDimitry Andric bool IsEntryBlock = BB->getParent() && BB->isEntryBlock(); 38240b57cec5SDimitry Andric if (BB->hasName()) { // Print out the label if it exists... 38250b57cec5SDimitry Andric Out << "\n"; 38260b57cec5SDimitry Andric PrintLLVMName(Out, BB->getName(), LabelPrefix); 38270b57cec5SDimitry Andric Out << ':'; 38280b57cec5SDimitry Andric } else if (!IsEntryBlock) { 38290b57cec5SDimitry Andric Out << "\n"; 38300b57cec5SDimitry Andric int Slot = Machine.getLocalSlot(BB); 38310b57cec5SDimitry Andric if (Slot != -1) 38320b57cec5SDimitry Andric Out << Slot << ":"; 38330b57cec5SDimitry Andric else 38340b57cec5SDimitry Andric Out << "<badref>:"; 38350b57cec5SDimitry Andric } 38360b57cec5SDimitry Andric 3837480093f4SDimitry Andric if (!IsEntryBlock) { 38380b57cec5SDimitry Andric // Output predecessors for the block. 38390b57cec5SDimitry Andric Out.PadToColumn(50); 38400b57cec5SDimitry Andric Out << ";"; 38410b57cec5SDimitry Andric const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 38420b57cec5SDimitry Andric 38430b57cec5SDimitry Andric if (PI == PE) { 38440b57cec5SDimitry Andric Out << " No predecessors!"; 38450b57cec5SDimitry Andric } else { 38460b57cec5SDimitry Andric Out << " preds = "; 38470b57cec5SDimitry Andric writeOperand(*PI, false); 38480b57cec5SDimitry Andric for (++PI; PI != PE; ++PI) { 38490b57cec5SDimitry Andric Out << ", "; 38500b57cec5SDimitry Andric writeOperand(*PI, false); 38510b57cec5SDimitry Andric } 38520b57cec5SDimitry Andric } 38530b57cec5SDimitry Andric } 38540b57cec5SDimitry Andric 38550b57cec5SDimitry Andric Out << "\n"; 38560b57cec5SDimitry Andric 38570b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); 38580b57cec5SDimitry Andric 38590b57cec5SDimitry Andric // Output all of the instructions in the basic block... 38600b57cec5SDimitry Andric for (const Instruction &I : *BB) { 38610b57cec5SDimitry Andric printInstructionLine(I); 38620b57cec5SDimitry Andric } 38630b57cec5SDimitry Andric 38640b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); 38650b57cec5SDimitry Andric } 38660b57cec5SDimitry Andric 38670b57cec5SDimitry Andric /// printInstructionLine - Print an instruction and a newline character. 38680b57cec5SDimitry Andric void AssemblyWriter::printInstructionLine(const Instruction &I) { 38690b57cec5SDimitry Andric printInstruction(I); 38700b57cec5SDimitry Andric Out << '\n'; 38710b57cec5SDimitry Andric } 38720b57cec5SDimitry Andric 38730b57cec5SDimitry Andric /// printGCRelocateComment - print comment after call to the gc.relocate 38740b57cec5SDimitry Andric /// intrinsic indicating base and derived pointer names. 38750b57cec5SDimitry Andric void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) { 38760b57cec5SDimitry Andric Out << " ; ("; 38770b57cec5SDimitry Andric writeOperand(Relocate.getBasePtr(), false); 38780b57cec5SDimitry Andric Out << ", "; 38790b57cec5SDimitry Andric writeOperand(Relocate.getDerivedPtr(), false); 38800b57cec5SDimitry Andric Out << ")"; 38810b57cec5SDimitry Andric } 38820b57cec5SDimitry Andric 38830b57cec5SDimitry Andric /// printInfoComment - Print a little comment after the instruction indicating 38840b57cec5SDimitry Andric /// which slot it occupies. 38850b57cec5SDimitry Andric void AssemblyWriter::printInfoComment(const Value &V) { 38860b57cec5SDimitry Andric if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V)) 38870b57cec5SDimitry Andric printGCRelocateComment(*Relocate); 38880b57cec5SDimitry Andric 38890b57cec5SDimitry Andric if (AnnotationWriter) 38900b57cec5SDimitry Andric AnnotationWriter->printInfoComment(V, Out); 38910b57cec5SDimitry Andric } 38920b57cec5SDimitry Andric 38930b57cec5SDimitry Andric static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I, 38940b57cec5SDimitry Andric raw_ostream &Out) { 38950b57cec5SDimitry Andric // We print the address space of the call if it is non-zero. 38960b57cec5SDimitry Andric unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace(); 38970b57cec5SDimitry Andric bool PrintAddrSpace = CallAddrSpace != 0; 38980b57cec5SDimitry Andric if (!PrintAddrSpace) { 38990b57cec5SDimitry Andric const Module *Mod = getModuleFromVal(I); 39000b57cec5SDimitry Andric // We also print it if it is zero but not equal to the program address space 39010b57cec5SDimitry Andric // or if we can't find a valid Module* to make it possible to parse 39020b57cec5SDimitry Andric // the resulting file even without a datalayout string. 39030b57cec5SDimitry Andric if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0) 39040b57cec5SDimitry Andric PrintAddrSpace = true; 39050b57cec5SDimitry Andric } 39060b57cec5SDimitry Andric if (PrintAddrSpace) 39070b57cec5SDimitry Andric Out << " addrspace(" << CallAddrSpace << ")"; 39080b57cec5SDimitry Andric } 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric // This member is called for each Instruction in a function.. 39110b57cec5SDimitry Andric void AssemblyWriter::printInstruction(const Instruction &I) { 39120b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); 39130b57cec5SDimitry Andric 39140b57cec5SDimitry Andric // Print out indentation for an instruction. 39150b57cec5SDimitry Andric Out << " "; 39160b57cec5SDimitry Andric 39170b57cec5SDimitry Andric // Print out name if it exists... 39180b57cec5SDimitry Andric if (I.hasName()) { 39190b57cec5SDimitry Andric PrintLLVMName(Out, &I); 39200b57cec5SDimitry Andric Out << " = "; 39210b57cec5SDimitry Andric } else if (!I.getType()->isVoidTy()) { 39220b57cec5SDimitry Andric // Print out the def slot taken. 39230b57cec5SDimitry Andric int SlotNum = Machine.getLocalSlot(&I); 39240b57cec5SDimitry Andric if (SlotNum == -1) 39250b57cec5SDimitry Andric Out << "<badref> = "; 39260b57cec5SDimitry Andric else 39270b57cec5SDimitry Andric Out << '%' << SlotNum << " = "; 39280b57cec5SDimitry Andric } 39290b57cec5SDimitry Andric 39300b57cec5SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I)) { 39310b57cec5SDimitry Andric if (CI->isMustTailCall()) 39320b57cec5SDimitry Andric Out << "musttail "; 39330b57cec5SDimitry Andric else if (CI->isTailCall()) 39340b57cec5SDimitry Andric Out << "tail "; 39350b57cec5SDimitry Andric else if (CI->isNoTailCall()) 39360b57cec5SDimitry Andric Out << "notail "; 39370b57cec5SDimitry Andric } 39380b57cec5SDimitry Andric 39390b57cec5SDimitry Andric // Print out the opcode... 39400b57cec5SDimitry Andric Out << I.getOpcodeName(); 39410b57cec5SDimitry Andric 39420b57cec5SDimitry Andric // If this is an atomic load or store, print out the atomic marker. 39430b57cec5SDimitry Andric if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) || 39440b57cec5SDimitry Andric (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic())) 39450b57cec5SDimitry Andric Out << " atomic"; 39460b57cec5SDimitry Andric 39470b57cec5SDimitry Andric if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak()) 39480b57cec5SDimitry Andric Out << " weak"; 39490b57cec5SDimitry Andric 39500b57cec5SDimitry Andric // If this is a volatile operation, print out the volatile marker. 39510b57cec5SDimitry Andric if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || 39520b57cec5SDimitry Andric (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) || 39530b57cec5SDimitry Andric (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) || 39540b57cec5SDimitry Andric (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile())) 39550b57cec5SDimitry Andric Out << " volatile"; 39560b57cec5SDimitry Andric 39570b57cec5SDimitry Andric // Print out optimization information. 39580b57cec5SDimitry Andric WriteOptimizationInfo(Out, &I); 39590b57cec5SDimitry Andric 39600b57cec5SDimitry Andric // Print out the compare instruction predicates 39610b57cec5SDimitry Andric if (const CmpInst *CI = dyn_cast<CmpInst>(&I)) 39620b57cec5SDimitry Andric Out << ' ' << CmpInst::getPredicateName(CI->getPredicate()); 39630b57cec5SDimitry Andric 39640b57cec5SDimitry Andric // Print out the atomicrmw operation 39650b57cec5SDimitry Andric if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) 39660b57cec5SDimitry Andric Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation()); 39670b57cec5SDimitry Andric 39680b57cec5SDimitry Andric // Print out the type of the operands... 39690b57cec5SDimitry Andric const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr; 39700b57cec5SDimitry Andric 39710b57cec5SDimitry Andric // Special case conditional branches to swizzle the condition out to the front 39720b57cec5SDimitry Andric if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) { 39730b57cec5SDimitry Andric const BranchInst &BI(cast<BranchInst>(I)); 39740b57cec5SDimitry Andric Out << ' '; 39750b57cec5SDimitry Andric writeOperand(BI.getCondition(), true); 39760b57cec5SDimitry Andric Out << ", "; 39770b57cec5SDimitry Andric writeOperand(BI.getSuccessor(0), true); 39780b57cec5SDimitry Andric Out << ", "; 39790b57cec5SDimitry Andric writeOperand(BI.getSuccessor(1), true); 39800b57cec5SDimitry Andric 39810b57cec5SDimitry Andric } else if (isa<SwitchInst>(I)) { 39820b57cec5SDimitry Andric const SwitchInst& SI(cast<SwitchInst>(I)); 39830b57cec5SDimitry Andric // Special case switch instruction to get formatting nice and correct. 39840b57cec5SDimitry Andric Out << ' '; 39850b57cec5SDimitry Andric writeOperand(SI.getCondition(), true); 39860b57cec5SDimitry Andric Out << ", "; 39870b57cec5SDimitry Andric writeOperand(SI.getDefaultDest(), true); 39880b57cec5SDimitry Andric Out << " ["; 39890b57cec5SDimitry Andric for (auto Case : SI.cases()) { 39900b57cec5SDimitry Andric Out << "\n "; 39910b57cec5SDimitry Andric writeOperand(Case.getCaseValue(), true); 39920b57cec5SDimitry Andric Out << ", "; 39930b57cec5SDimitry Andric writeOperand(Case.getCaseSuccessor(), true); 39940b57cec5SDimitry Andric } 39950b57cec5SDimitry Andric Out << "\n ]"; 39960b57cec5SDimitry Andric } else if (isa<IndirectBrInst>(I)) { 39970b57cec5SDimitry Andric // Special case indirectbr instruction to get formatting nice and correct. 39980b57cec5SDimitry Andric Out << ' '; 39990b57cec5SDimitry Andric writeOperand(Operand, true); 40000b57cec5SDimitry Andric Out << ", ["; 40010b57cec5SDimitry Andric 40020b57cec5SDimitry Andric for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { 40030b57cec5SDimitry Andric if (i != 1) 40040b57cec5SDimitry Andric Out << ", "; 40050b57cec5SDimitry Andric writeOperand(I.getOperand(i), true); 40060b57cec5SDimitry Andric } 40070b57cec5SDimitry Andric Out << ']'; 40080b57cec5SDimitry Andric } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) { 40090b57cec5SDimitry Andric Out << ' '; 40100b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 40110b57cec5SDimitry Andric Out << ' '; 40120b57cec5SDimitry Andric 40130b57cec5SDimitry Andric for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) { 40140b57cec5SDimitry Andric if (op) Out << ", "; 40150b57cec5SDimitry Andric Out << "[ "; 40160b57cec5SDimitry Andric writeOperand(PN->getIncomingValue(op), false); Out << ", "; 40170b57cec5SDimitry Andric writeOperand(PN->getIncomingBlock(op), false); Out << " ]"; 40180b57cec5SDimitry Andric } 40190b57cec5SDimitry Andric } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) { 40200b57cec5SDimitry Andric Out << ' '; 40210b57cec5SDimitry Andric writeOperand(I.getOperand(0), true); 4022fe6060f1SDimitry Andric for (unsigned i : EVI->indices()) 4023fe6060f1SDimitry Andric Out << ", " << i; 40240b57cec5SDimitry Andric } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) { 40250b57cec5SDimitry Andric Out << ' '; 40260b57cec5SDimitry Andric writeOperand(I.getOperand(0), true); Out << ", "; 40270b57cec5SDimitry Andric writeOperand(I.getOperand(1), true); 4028fe6060f1SDimitry Andric for (unsigned i : IVI->indices()) 4029fe6060f1SDimitry Andric Out << ", " << i; 40300b57cec5SDimitry Andric } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) { 40310b57cec5SDimitry Andric Out << ' '; 40320b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 40330b57cec5SDimitry Andric if (LPI->isCleanup() || LPI->getNumClauses() != 0) 40340b57cec5SDimitry Andric Out << '\n'; 40350b57cec5SDimitry Andric 40360b57cec5SDimitry Andric if (LPI->isCleanup()) 40370b57cec5SDimitry Andric Out << " cleanup"; 40380b57cec5SDimitry Andric 40390b57cec5SDimitry Andric for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) { 40400b57cec5SDimitry Andric if (i != 0 || LPI->isCleanup()) Out << "\n"; 40410b57cec5SDimitry Andric if (LPI->isCatch(i)) 40420b57cec5SDimitry Andric Out << " catch "; 40430b57cec5SDimitry Andric else 40440b57cec5SDimitry Andric Out << " filter "; 40450b57cec5SDimitry Andric 40460b57cec5SDimitry Andric writeOperand(LPI->getClause(i), true); 40470b57cec5SDimitry Andric } 40480b57cec5SDimitry Andric } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) { 40490b57cec5SDimitry Andric Out << " within "; 40500b57cec5SDimitry Andric writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false); 40510b57cec5SDimitry Andric Out << " ["; 40520b57cec5SDimitry Andric unsigned Op = 0; 40530b57cec5SDimitry Andric for (const BasicBlock *PadBB : CatchSwitch->handlers()) { 40540b57cec5SDimitry Andric if (Op > 0) 40550b57cec5SDimitry Andric Out << ", "; 40560b57cec5SDimitry Andric writeOperand(PadBB, /*PrintType=*/true); 40570b57cec5SDimitry Andric ++Op; 40580b57cec5SDimitry Andric } 40590b57cec5SDimitry Andric Out << "] unwind "; 40600b57cec5SDimitry Andric if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest()) 40610b57cec5SDimitry Andric writeOperand(UnwindDest, /*PrintType=*/true); 40620b57cec5SDimitry Andric else 40630b57cec5SDimitry Andric Out << "to caller"; 40640b57cec5SDimitry Andric } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) { 40650b57cec5SDimitry Andric Out << " within "; 40660b57cec5SDimitry Andric writeOperand(FPI->getParentPad(), /*PrintType=*/false); 40670b57cec5SDimitry Andric Out << " ["; 40680b57cec5SDimitry Andric for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps; 40690b57cec5SDimitry Andric ++Op) { 40700b57cec5SDimitry Andric if (Op > 0) 40710b57cec5SDimitry Andric Out << ", "; 40720b57cec5SDimitry Andric writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true); 40730b57cec5SDimitry Andric } 40740b57cec5SDimitry Andric Out << ']'; 40750b57cec5SDimitry Andric } else if (isa<ReturnInst>(I) && !Operand) { 40760b57cec5SDimitry Andric Out << " void"; 40770b57cec5SDimitry Andric } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) { 40780b57cec5SDimitry Andric Out << " from "; 40790b57cec5SDimitry Andric writeOperand(CRI->getOperand(0), /*PrintType=*/false); 40800b57cec5SDimitry Andric 40810b57cec5SDimitry Andric Out << " to "; 40820b57cec5SDimitry Andric writeOperand(CRI->getOperand(1), /*PrintType=*/true); 40830b57cec5SDimitry Andric } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) { 40840b57cec5SDimitry Andric Out << " from "; 40850b57cec5SDimitry Andric writeOperand(CRI->getOperand(0), /*PrintType=*/false); 40860b57cec5SDimitry Andric 40870b57cec5SDimitry Andric Out << " unwind "; 40880b57cec5SDimitry Andric if (CRI->hasUnwindDest()) 40890b57cec5SDimitry Andric writeOperand(CRI->getOperand(1), /*PrintType=*/true); 40900b57cec5SDimitry Andric else 40910b57cec5SDimitry Andric Out << "to caller"; 40920b57cec5SDimitry Andric } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { 40930b57cec5SDimitry Andric // Print the calling convention being used. 40940b57cec5SDimitry Andric if (CI->getCallingConv() != CallingConv::C) { 40950b57cec5SDimitry Andric Out << " "; 40960b57cec5SDimitry Andric PrintCallingConv(CI->getCallingConv(), Out); 40970b57cec5SDimitry Andric } 40980b57cec5SDimitry Andric 40995ffd83dbSDimitry Andric Operand = CI->getCalledOperand(); 41000b57cec5SDimitry Andric FunctionType *FTy = CI->getFunctionType(); 41010b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 41020b57cec5SDimitry Andric const AttributeList &PAL = CI->getAttributes(); 41030b57cec5SDimitry Andric 4104349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 41050b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 41060b57cec5SDimitry Andric 41070b57cec5SDimitry Andric // Only print addrspace(N) if necessary: 41080b57cec5SDimitry Andric maybePrintCallAddrSpace(Operand, &I, Out); 41090b57cec5SDimitry Andric 41100b57cec5SDimitry Andric // If possible, print out the short form of the call instruction. We can 41110b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 41120b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 41130b57cec5SDimitry Andric // 41140b57cec5SDimitry Andric Out << ' '; 41150b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 41160b57cec5SDimitry Andric Out << ' '; 41170b57cec5SDimitry Andric writeOperand(Operand, false); 41180b57cec5SDimitry Andric Out << '('; 4119349cc55cSDimitry Andric for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) { 41200b57cec5SDimitry Andric if (op > 0) 41210b57cec5SDimitry Andric Out << ", "; 4122349cc55cSDimitry Andric writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op)); 41230b57cec5SDimitry Andric } 41240b57cec5SDimitry Andric 41250b57cec5SDimitry Andric // Emit an ellipsis if this is a musttail call in a vararg function. This 41260b57cec5SDimitry Andric // is only to aid readability, musttail calls forward varargs by default. 41270b57cec5SDimitry Andric if (CI->isMustTailCall() && CI->getParent() && 41280b57cec5SDimitry Andric CI->getParent()->getParent() && 41290b57cec5SDimitry Andric CI->getParent()->getParent()->isVarArg()) 41300b57cec5SDimitry Andric Out << ", ..."; 41310b57cec5SDimitry Andric 41320b57cec5SDimitry Andric Out << ')'; 4133349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4134349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 41350b57cec5SDimitry Andric 41360b57cec5SDimitry Andric writeOperandBundles(CI); 41370b57cec5SDimitry Andric } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { 41385ffd83dbSDimitry Andric Operand = II->getCalledOperand(); 41390b57cec5SDimitry Andric FunctionType *FTy = II->getFunctionType(); 41400b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 41410b57cec5SDimitry Andric const AttributeList &PAL = II->getAttributes(); 41420b57cec5SDimitry Andric 41430b57cec5SDimitry Andric // Print the calling convention being used. 41440b57cec5SDimitry Andric if (II->getCallingConv() != CallingConv::C) { 41450b57cec5SDimitry Andric Out << " "; 41460b57cec5SDimitry Andric PrintCallingConv(II->getCallingConv(), Out); 41470b57cec5SDimitry Andric } 41480b57cec5SDimitry Andric 4149349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 41500b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 41510b57cec5SDimitry Andric 41520b57cec5SDimitry Andric // Only print addrspace(N) if necessary: 41530b57cec5SDimitry Andric maybePrintCallAddrSpace(Operand, &I, Out); 41540b57cec5SDimitry Andric 41550b57cec5SDimitry Andric // If possible, print out the short form of the invoke instruction. We can 41560b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 41570b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 41580b57cec5SDimitry Andric // 41590b57cec5SDimitry Andric Out << ' '; 41600b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 41610b57cec5SDimitry Andric Out << ' '; 41620b57cec5SDimitry Andric writeOperand(Operand, false); 41630b57cec5SDimitry Andric Out << '('; 4164349cc55cSDimitry Andric for (unsigned op = 0, Eop = II->arg_size(); op < Eop; ++op) { 41650b57cec5SDimitry Andric if (op) 41660b57cec5SDimitry Andric Out << ", "; 4167349cc55cSDimitry Andric writeParamOperand(II->getArgOperand(op), PAL.getParamAttrs(op)); 41680b57cec5SDimitry Andric } 41690b57cec5SDimitry Andric 41700b57cec5SDimitry Andric Out << ')'; 4171349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4172349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 41730b57cec5SDimitry Andric 41740b57cec5SDimitry Andric writeOperandBundles(II); 41750b57cec5SDimitry Andric 41760b57cec5SDimitry Andric Out << "\n to "; 41770b57cec5SDimitry Andric writeOperand(II->getNormalDest(), true); 41780b57cec5SDimitry Andric Out << " unwind "; 41790b57cec5SDimitry Andric writeOperand(II->getUnwindDest(), true); 41800b57cec5SDimitry Andric } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) { 41815ffd83dbSDimitry Andric Operand = CBI->getCalledOperand(); 41820b57cec5SDimitry Andric FunctionType *FTy = CBI->getFunctionType(); 41830b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 41840b57cec5SDimitry Andric const AttributeList &PAL = CBI->getAttributes(); 41850b57cec5SDimitry Andric 41860b57cec5SDimitry Andric // Print the calling convention being used. 41870b57cec5SDimitry Andric if (CBI->getCallingConv() != CallingConv::C) { 41880b57cec5SDimitry Andric Out << " "; 41890b57cec5SDimitry Andric PrintCallingConv(CBI->getCallingConv(), Out); 41900b57cec5SDimitry Andric } 41910b57cec5SDimitry Andric 4192349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 41930b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 41940b57cec5SDimitry Andric 41950b57cec5SDimitry Andric // If possible, print out the short form of the callbr instruction. We can 41960b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 41970b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 41980b57cec5SDimitry Andric // 41990b57cec5SDimitry Andric Out << ' '; 42000b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 42010b57cec5SDimitry Andric Out << ' '; 42020b57cec5SDimitry Andric writeOperand(Operand, false); 42030b57cec5SDimitry Andric Out << '('; 4204349cc55cSDimitry Andric for (unsigned op = 0, Eop = CBI->arg_size(); op < Eop; ++op) { 42050b57cec5SDimitry Andric if (op) 42060b57cec5SDimitry Andric Out << ", "; 4207349cc55cSDimitry Andric writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttrs(op)); 42080b57cec5SDimitry Andric } 42090b57cec5SDimitry Andric 42100b57cec5SDimitry Andric Out << ')'; 4211349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4212349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 42130b57cec5SDimitry Andric 42140b57cec5SDimitry Andric writeOperandBundles(CBI); 42150b57cec5SDimitry Andric 42160b57cec5SDimitry Andric Out << "\n to "; 42170b57cec5SDimitry Andric writeOperand(CBI->getDefaultDest(), true); 42180b57cec5SDimitry Andric Out << " ["; 42190b57cec5SDimitry Andric for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) { 42200b57cec5SDimitry Andric if (i != 0) 42210b57cec5SDimitry Andric Out << ", "; 42220b57cec5SDimitry Andric writeOperand(CBI->getIndirectDest(i), true); 42230b57cec5SDimitry Andric } 42240b57cec5SDimitry Andric Out << ']'; 42250b57cec5SDimitry Andric } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 42260b57cec5SDimitry Andric Out << ' '; 42270b57cec5SDimitry Andric if (AI->isUsedWithInAlloca()) 42280b57cec5SDimitry Andric Out << "inalloca "; 42290b57cec5SDimitry Andric if (AI->isSwiftError()) 42300b57cec5SDimitry Andric Out << "swifterror "; 42310b57cec5SDimitry Andric TypePrinter.print(AI->getAllocatedType(), Out); 42320b57cec5SDimitry Andric 42330b57cec5SDimitry Andric // Explicitly write the array size if the code is broken, if it's an array 42340b57cec5SDimitry Andric // allocation, or if the type is not canonical for scalar allocations. The 42350b57cec5SDimitry Andric // latter case prevents the type from mutating when round-tripping through 42360b57cec5SDimitry Andric // assembly. 42370b57cec5SDimitry Andric if (!AI->getArraySize() || AI->isArrayAllocation() || 42380b57cec5SDimitry Andric !AI->getArraySize()->getType()->isIntegerTy(32)) { 42390b57cec5SDimitry Andric Out << ", "; 42400b57cec5SDimitry Andric writeOperand(AI->getArraySize(), true); 42410b57cec5SDimitry Andric } 42420b57cec5SDimitry Andric if (AI->getAlignment()) { 42430b57cec5SDimitry Andric Out << ", align " << AI->getAlignment(); 42440b57cec5SDimitry Andric } 42450b57cec5SDimitry Andric 42460b57cec5SDimitry Andric unsigned AddrSpace = AI->getType()->getAddressSpace(); 42470b57cec5SDimitry Andric if (AddrSpace != 0) { 42480b57cec5SDimitry Andric Out << ", addrspace(" << AddrSpace << ')'; 42490b57cec5SDimitry Andric } 42500b57cec5SDimitry Andric } else if (isa<CastInst>(I)) { 42510b57cec5SDimitry Andric if (Operand) { 42520b57cec5SDimitry Andric Out << ' '; 42530b57cec5SDimitry Andric writeOperand(Operand, true); // Work with broken code 42540b57cec5SDimitry Andric } 42550b57cec5SDimitry Andric Out << " to "; 42560b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 42570b57cec5SDimitry Andric } else if (isa<VAArgInst>(I)) { 42580b57cec5SDimitry Andric if (Operand) { 42590b57cec5SDimitry Andric Out << ' '; 42600b57cec5SDimitry Andric writeOperand(Operand, true); // Work with broken code 42610b57cec5SDimitry Andric } 42620b57cec5SDimitry Andric Out << ", "; 42630b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 42640b57cec5SDimitry Andric } else if (Operand) { // Print the normal way. 42650b57cec5SDimitry Andric if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) { 42660b57cec5SDimitry Andric Out << ' '; 42670b57cec5SDimitry Andric TypePrinter.print(GEP->getSourceElementType(), Out); 42680b57cec5SDimitry Andric Out << ','; 42690b57cec5SDimitry Andric } else if (const auto *LI = dyn_cast<LoadInst>(&I)) { 42700b57cec5SDimitry Andric Out << ' '; 42710b57cec5SDimitry Andric TypePrinter.print(LI->getType(), Out); 42720b57cec5SDimitry Andric Out << ','; 42730b57cec5SDimitry Andric } 42740b57cec5SDimitry Andric 42750b57cec5SDimitry Andric // PrintAllTypes - Instructions who have operands of all the same type 42760b57cec5SDimitry Andric // omit the type from all but the first operand. If the instruction has 42770b57cec5SDimitry Andric // different type operands (for example br), then they are all printed. 42780b57cec5SDimitry Andric bool PrintAllTypes = false; 42790b57cec5SDimitry Andric Type *TheType = Operand->getType(); 42800b57cec5SDimitry Andric 42810b57cec5SDimitry Andric // Select, Store and ShuffleVector always print all types. 42820b57cec5SDimitry Andric if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) 42830b57cec5SDimitry Andric || isa<ReturnInst>(I)) { 42840b57cec5SDimitry Andric PrintAllTypes = true; 42850b57cec5SDimitry Andric } else { 42860b57cec5SDimitry Andric for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { 42870b57cec5SDimitry Andric Operand = I.getOperand(i); 42880b57cec5SDimitry Andric // note that Operand shouldn't be null, but the test helps make dump() 42890b57cec5SDimitry Andric // more tolerant of malformed IR 42900b57cec5SDimitry Andric if (Operand && Operand->getType() != TheType) { 42910b57cec5SDimitry Andric PrintAllTypes = true; // We have differing types! Print them all! 42920b57cec5SDimitry Andric break; 42930b57cec5SDimitry Andric } 42940b57cec5SDimitry Andric } 42950b57cec5SDimitry Andric } 42960b57cec5SDimitry Andric 42970b57cec5SDimitry Andric if (!PrintAllTypes) { 42980b57cec5SDimitry Andric Out << ' '; 42990b57cec5SDimitry Andric TypePrinter.print(TheType, Out); 43000b57cec5SDimitry Andric } 43010b57cec5SDimitry Andric 43020b57cec5SDimitry Andric Out << ' '; 43030b57cec5SDimitry Andric for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { 43040b57cec5SDimitry Andric if (i) Out << ", "; 43050b57cec5SDimitry Andric writeOperand(I.getOperand(i), PrintAllTypes); 43060b57cec5SDimitry Andric } 43070b57cec5SDimitry Andric } 43080b57cec5SDimitry Andric 43090b57cec5SDimitry Andric // Print atomic ordering/alignment for memory operations 43100b57cec5SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) { 43110b57cec5SDimitry Andric if (LI->isAtomic()) 43120b57cec5SDimitry Andric writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID()); 43130b57cec5SDimitry Andric if (LI->getAlignment()) 43140b57cec5SDimitry Andric Out << ", align " << LI->getAlignment(); 43150b57cec5SDimitry Andric } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) { 43160b57cec5SDimitry Andric if (SI->isAtomic()) 43170b57cec5SDimitry Andric writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID()); 43180b57cec5SDimitry Andric if (SI->getAlignment()) 43190b57cec5SDimitry Andric Out << ", align " << SI->getAlignment(); 43200b57cec5SDimitry Andric } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) { 43210b57cec5SDimitry Andric writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(), 43220b57cec5SDimitry Andric CXI->getFailureOrdering(), CXI->getSyncScopeID()); 4323fe6060f1SDimitry Andric Out << ", align " << CXI->getAlign().value(); 43240b57cec5SDimitry Andric } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) { 43250b57cec5SDimitry Andric writeAtomic(RMWI->getContext(), RMWI->getOrdering(), 43260b57cec5SDimitry Andric RMWI->getSyncScopeID()); 4327fe6060f1SDimitry Andric Out << ", align " << RMWI->getAlign().value(); 43280b57cec5SDimitry Andric } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) { 43290b57cec5SDimitry Andric writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID()); 43305ffd83dbSDimitry Andric } else if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(&I)) { 43315ffd83dbSDimitry Andric PrintShuffleMask(Out, SVI->getType(), SVI->getShuffleMask()); 43320b57cec5SDimitry Andric } 43330b57cec5SDimitry Andric 43340b57cec5SDimitry Andric // Print Metadata info. 43350b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD; 43360b57cec5SDimitry Andric I.getAllMetadata(InstMD); 43370b57cec5SDimitry Andric printMetadataAttachments(InstMD, ", "); 43380b57cec5SDimitry Andric 43390b57cec5SDimitry Andric // Print a nice comment. 43400b57cec5SDimitry Andric printInfoComment(I); 43410b57cec5SDimitry Andric } 43420b57cec5SDimitry Andric 43430b57cec5SDimitry Andric void AssemblyWriter::printMetadataAttachments( 43440b57cec5SDimitry Andric const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs, 43450b57cec5SDimitry Andric StringRef Separator) { 43460b57cec5SDimitry Andric if (MDs.empty()) 43470b57cec5SDimitry Andric return; 43480b57cec5SDimitry Andric 43490b57cec5SDimitry Andric if (MDNames.empty()) 43500b57cec5SDimitry Andric MDs[0].second->getContext().getMDKindNames(MDNames); 43510b57cec5SDimitry Andric 4352349cc55cSDimitry Andric auto WriterCtx = getContext(); 43530b57cec5SDimitry Andric for (const auto &I : MDs) { 43540b57cec5SDimitry Andric unsigned Kind = I.first; 43550b57cec5SDimitry Andric Out << Separator; 43560b57cec5SDimitry Andric if (Kind < MDNames.size()) { 43570b57cec5SDimitry Andric Out << "!"; 43580b57cec5SDimitry Andric printMetadataIdentifier(MDNames[Kind], Out); 43590b57cec5SDimitry Andric } else 43600b57cec5SDimitry Andric Out << "!<unknown kind #" << Kind << ">"; 43610b57cec5SDimitry Andric Out << ' '; 4362349cc55cSDimitry Andric WriteAsOperandInternal(Out, I.second, WriterCtx); 43630b57cec5SDimitry Andric } 43640b57cec5SDimitry Andric } 43650b57cec5SDimitry Andric 43660b57cec5SDimitry Andric void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) { 43670b57cec5SDimitry Andric Out << '!' << Slot << " = "; 43680b57cec5SDimitry Andric printMDNodeBody(Node); 43690b57cec5SDimitry Andric Out << "\n"; 43700b57cec5SDimitry Andric } 43710b57cec5SDimitry Andric 43720b57cec5SDimitry Andric void AssemblyWriter::writeAllMDNodes() { 43730b57cec5SDimitry Andric SmallVector<const MDNode *, 16> Nodes; 43740b57cec5SDimitry Andric Nodes.resize(Machine.mdn_size()); 4375fe6060f1SDimitry Andric for (auto &I : llvm::make_range(Machine.mdn_begin(), Machine.mdn_end())) 4376fe6060f1SDimitry Andric Nodes[I.second] = cast<MDNode>(I.first); 43770b57cec5SDimitry Andric 43780b57cec5SDimitry Andric for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 43790b57cec5SDimitry Andric writeMDNode(i, Nodes[i]); 43800b57cec5SDimitry Andric } 43810b57cec5SDimitry Andric } 43820b57cec5SDimitry Andric 43830b57cec5SDimitry Andric void AssemblyWriter::printMDNodeBody(const MDNode *Node) { 4384349cc55cSDimitry Andric auto WriterCtx = getContext(); 4385349cc55cSDimitry Andric WriteMDNodeBodyInternal(Out, Node, WriterCtx); 43860b57cec5SDimitry Andric } 43870b57cec5SDimitry Andric 4388480093f4SDimitry Andric void AssemblyWriter::writeAttribute(const Attribute &Attr, bool InAttrGroup) { 4389480093f4SDimitry Andric if (!Attr.isTypeAttribute()) { 4390480093f4SDimitry Andric Out << Attr.getAsString(InAttrGroup); 4391480093f4SDimitry Andric return; 4392480093f4SDimitry Andric } 4393480093f4SDimitry Andric 4394fe6060f1SDimitry Andric Out << Attribute::getNameFromAttrKind(Attr.getKindAsEnum()); 4395480093f4SDimitry Andric if (Type *Ty = Attr.getValueAsType()) { 4396480093f4SDimitry Andric Out << '('; 4397480093f4SDimitry Andric TypePrinter.print(Ty, Out); 4398480093f4SDimitry Andric Out << ')'; 4399480093f4SDimitry Andric } 4400480093f4SDimitry Andric } 4401480093f4SDimitry Andric 4402480093f4SDimitry Andric void AssemblyWriter::writeAttributeSet(const AttributeSet &AttrSet, 4403480093f4SDimitry Andric bool InAttrGroup) { 4404480093f4SDimitry Andric bool FirstAttr = true; 4405480093f4SDimitry Andric for (const auto &Attr : AttrSet) { 4406480093f4SDimitry Andric if (!FirstAttr) 4407480093f4SDimitry Andric Out << ' '; 4408480093f4SDimitry Andric writeAttribute(Attr, InAttrGroup); 4409480093f4SDimitry Andric FirstAttr = false; 4410480093f4SDimitry Andric } 4411480093f4SDimitry Andric } 4412480093f4SDimitry Andric 44130b57cec5SDimitry Andric void AssemblyWriter::writeAllAttributeGroups() { 44140b57cec5SDimitry Andric std::vector<std::pair<AttributeSet, unsigned>> asVec; 44150b57cec5SDimitry Andric asVec.resize(Machine.as_size()); 44160b57cec5SDimitry Andric 4417fe6060f1SDimitry Andric for (auto &I : llvm::make_range(Machine.as_begin(), Machine.as_end())) 4418fe6060f1SDimitry Andric asVec[I.second] = I; 44190b57cec5SDimitry Andric 44200b57cec5SDimitry Andric for (const auto &I : asVec) 44210b57cec5SDimitry Andric Out << "attributes #" << I.second << " = { " 44220b57cec5SDimitry Andric << I.first.getAsString(true) << " }\n"; 44230b57cec5SDimitry Andric } 44240b57cec5SDimitry Andric 4425fe6060f1SDimitry Andric void AssemblyWriter::printUseListOrder(const Value *V, 4426fe6060f1SDimitry Andric const std::vector<unsigned> &Shuffle) { 44270b57cec5SDimitry Andric bool IsInFunction = Machine.getFunction(); 44280b57cec5SDimitry Andric if (IsInFunction) 44290b57cec5SDimitry Andric Out << " "; 44300b57cec5SDimitry Andric 44310b57cec5SDimitry Andric Out << "uselistorder"; 4432fe6060f1SDimitry Andric if (const BasicBlock *BB = IsInFunction ? nullptr : dyn_cast<BasicBlock>(V)) { 44330b57cec5SDimitry Andric Out << "_bb "; 44340b57cec5SDimitry Andric writeOperand(BB->getParent(), false); 44350b57cec5SDimitry Andric Out << ", "; 44360b57cec5SDimitry Andric writeOperand(BB, false); 44370b57cec5SDimitry Andric } else { 44380b57cec5SDimitry Andric Out << " "; 4439fe6060f1SDimitry Andric writeOperand(V, true); 44400b57cec5SDimitry Andric } 44410b57cec5SDimitry Andric Out << ", { "; 44420b57cec5SDimitry Andric 4443fe6060f1SDimitry Andric assert(Shuffle.size() >= 2 && "Shuffle too small"); 4444fe6060f1SDimitry Andric Out << Shuffle[0]; 4445fe6060f1SDimitry Andric for (unsigned I = 1, E = Shuffle.size(); I != E; ++I) 4446fe6060f1SDimitry Andric Out << ", " << Shuffle[I]; 44470b57cec5SDimitry Andric Out << " }\n"; 44480b57cec5SDimitry Andric } 44490b57cec5SDimitry Andric 44500b57cec5SDimitry Andric void AssemblyWriter::printUseLists(const Function *F) { 4451fe6060f1SDimitry Andric auto It = UseListOrders.find(F); 4452fe6060f1SDimitry Andric if (It == UseListOrders.end()) 44530b57cec5SDimitry Andric return; 44540b57cec5SDimitry Andric 44550b57cec5SDimitry Andric Out << "\n; uselistorder directives\n"; 4456fe6060f1SDimitry Andric for (const auto &Pair : It->second) 4457fe6060f1SDimitry Andric printUseListOrder(Pair.first, Pair.second); 44580b57cec5SDimitry Andric } 44590b57cec5SDimitry Andric 44600b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44610b57cec5SDimitry Andric // External Interface declarations 44620b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44630b57cec5SDimitry Andric 44640b57cec5SDimitry Andric void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 44650b57cec5SDimitry Andric bool ShouldPreserveUseListOrder, 44660b57cec5SDimitry Andric bool IsForDebug) const { 44670b57cec5SDimitry Andric SlotTracker SlotTable(this->getParent()); 44680b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 44690b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this->getParent(), AAW, 44700b57cec5SDimitry Andric IsForDebug, 44710b57cec5SDimitry Andric ShouldPreserveUseListOrder); 44720b57cec5SDimitry Andric W.printFunction(this); 44730b57cec5SDimitry Andric } 44740b57cec5SDimitry Andric 44755ffd83dbSDimitry Andric void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 44765ffd83dbSDimitry Andric bool ShouldPreserveUseListOrder, 44775ffd83dbSDimitry Andric bool IsForDebug) const { 4478e8d8bef9SDimitry Andric SlotTracker SlotTable(this->getParent()); 44795ffd83dbSDimitry Andric formatted_raw_ostream OS(ROS); 44805ffd83dbSDimitry Andric AssemblyWriter W(OS, SlotTable, this->getModule(), AAW, 44815ffd83dbSDimitry Andric IsForDebug, 44825ffd83dbSDimitry Andric ShouldPreserveUseListOrder); 44835ffd83dbSDimitry Andric W.printBasicBlock(this); 44845ffd83dbSDimitry Andric } 44855ffd83dbSDimitry Andric 44860b57cec5SDimitry Andric void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 44870b57cec5SDimitry Andric bool ShouldPreserveUseListOrder, bool IsForDebug) const { 44880b57cec5SDimitry Andric SlotTracker SlotTable(this); 44890b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 44900b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug, 44910b57cec5SDimitry Andric ShouldPreserveUseListOrder); 44920b57cec5SDimitry Andric W.printModule(this); 44930b57cec5SDimitry Andric } 44940b57cec5SDimitry Andric 44950b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const { 44960b57cec5SDimitry Andric SlotTracker SlotTable(getParent()); 44970b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 44980b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug); 44990b57cec5SDimitry Andric W.printNamedMDNode(this); 45000b57cec5SDimitry Andric } 45010b57cec5SDimitry Andric 45020b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST, 45030b57cec5SDimitry Andric bool IsForDebug) const { 45040b57cec5SDimitry Andric Optional<SlotTracker> LocalST; 45050b57cec5SDimitry Andric SlotTracker *SlotTable; 45060b57cec5SDimitry Andric if (auto *ST = MST.getMachine()) 45070b57cec5SDimitry Andric SlotTable = ST; 45080b57cec5SDimitry Andric else { 45090b57cec5SDimitry Andric LocalST.emplace(getParent()); 45100b57cec5SDimitry Andric SlotTable = &*LocalST; 45110b57cec5SDimitry Andric } 45120b57cec5SDimitry Andric 45130b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 45140b57cec5SDimitry Andric AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug); 45150b57cec5SDimitry Andric W.printNamedMDNode(this); 45160b57cec5SDimitry Andric } 45170b57cec5SDimitry Andric 45180b57cec5SDimitry Andric void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const { 45190b57cec5SDimitry Andric PrintLLVMName(ROS, getName(), ComdatPrefix); 45200b57cec5SDimitry Andric ROS << " = comdat "; 45210b57cec5SDimitry Andric 45220b57cec5SDimitry Andric switch (getSelectionKind()) { 45230b57cec5SDimitry Andric case Comdat::Any: 45240b57cec5SDimitry Andric ROS << "any"; 45250b57cec5SDimitry Andric break; 45260b57cec5SDimitry Andric case Comdat::ExactMatch: 45270b57cec5SDimitry Andric ROS << "exactmatch"; 45280b57cec5SDimitry Andric break; 45290b57cec5SDimitry Andric case Comdat::Largest: 45300b57cec5SDimitry Andric ROS << "largest"; 45310b57cec5SDimitry Andric break; 4532fe6060f1SDimitry Andric case Comdat::NoDeduplicate: 4533fe6060f1SDimitry Andric ROS << "nodeduplicate"; 45340b57cec5SDimitry Andric break; 45350b57cec5SDimitry Andric case Comdat::SameSize: 45360b57cec5SDimitry Andric ROS << "samesize"; 45370b57cec5SDimitry Andric break; 45380b57cec5SDimitry Andric } 45390b57cec5SDimitry Andric 45400b57cec5SDimitry Andric ROS << '\n'; 45410b57cec5SDimitry Andric } 45420b57cec5SDimitry Andric 45430b57cec5SDimitry Andric void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const { 45440b57cec5SDimitry Andric TypePrinting TP; 45450b57cec5SDimitry Andric TP.print(const_cast<Type*>(this), OS); 45460b57cec5SDimitry Andric 45470b57cec5SDimitry Andric if (NoDetails) 45480b57cec5SDimitry Andric return; 45490b57cec5SDimitry Andric 45500b57cec5SDimitry Andric // If the type is a named struct type, print the body as well. 45510b57cec5SDimitry Andric if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this))) 45520b57cec5SDimitry Andric if (!STy->isLiteral()) { 45530b57cec5SDimitry Andric OS << " = type "; 45540b57cec5SDimitry Andric TP.printStructBody(STy, OS); 45550b57cec5SDimitry Andric } 45560b57cec5SDimitry Andric } 45570b57cec5SDimitry Andric 45580b57cec5SDimitry Andric static bool isReferencingMDNode(const Instruction &I) { 45590b57cec5SDimitry Andric if (const auto *CI = dyn_cast<CallInst>(&I)) 45600b57cec5SDimitry Andric if (Function *F = CI->getCalledFunction()) 45610b57cec5SDimitry Andric if (F->isIntrinsic()) 45620b57cec5SDimitry Andric for (auto &Op : I.operands()) 45630b57cec5SDimitry Andric if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op)) 45640b57cec5SDimitry Andric if (isa<MDNode>(V->getMetadata())) 45650b57cec5SDimitry Andric return true; 45660b57cec5SDimitry Andric return false; 45670b57cec5SDimitry Andric } 45680b57cec5SDimitry Andric 45690b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, bool IsForDebug) const { 45700b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false; 45710b57cec5SDimitry Andric if (auto *I = dyn_cast<Instruction>(this)) 45720b57cec5SDimitry Andric ShouldInitializeAllMetadata = isReferencingMDNode(*I); 45730b57cec5SDimitry Andric else if (isa<Function>(this) || isa<MetadataAsValue>(this)) 45740b57cec5SDimitry Andric ShouldInitializeAllMetadata = true; 45750b57cec5SDimitry Andric 45760b57cec5SDimitry Andric ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata); 45770b57cec5SDimitry Andric print(ROS, MST, IsForDebug); 45780b57cec5SDimitry Andric } 45790b57cec5SDimitry Andric 45800b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST, 45810b57cec5SDimitry Andric bool IsForDebug) const { 45820b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 45830b57cec5SDimitry Andric SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr)); 45840b57cec5SDimitry Andric SlotTracker &SlotTable = 45850b57cec5SDimitry Andric MST.getMachine() ? *MST.getMachine() : EmptySlotTable; 45860b57cec5SDimitry Andric auto incorporateFunction = [&](const Function *F) { 45870b57cec5SDimitry Andric if (F) 45880b57cec5SDimitry Andric MST.incorporateFunction(*F); 45890b57cec5SDimitry Andric }; 45900b57cec5SDimitry Andric 45910b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(this)) { 45920b57cec5SDimitry Andric incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr); 45930b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug); 45940b57cec5SDimitry Andric W.printInstruction(*I); 45950b57cec5SDimitry Andric } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) { 45960b57cec5SDimitry Andric incorporateFunction(BB->getParent()); 45970b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug); 45980b57cec5SDimitry Andric W.printBasicBlock(BB); 45990b57cec5SDimitry Andric } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { 46000b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug); 46010b57cec5SDimitry Andric if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV)) 46020b57cec5SDimitry Andric W.printGlobal(V); 46030b57cec5SDimitry Andric else if (const Function *F = dyn_cast<Function>(GV)) 46040b57cec5SDimitry Andric W.printFunction(F); 4605349cc55cSDimitry Andric else if (const GlobalAlias *A = dyn_cast<GlobalAlias>(GV)) 4606349cc55cSDimitry Andric W.printAlias(A); 4607349cc55cSDimitry Andric else if (const GlobalIFunc *I = dyn_cast<GlobalIFunc>(GV)) 4608349cc55cSDimitry Andric W.printIFunc(I); 46090b57cec5SDimitry Andric else 4610349cc55cSDimitry Andric llvm_unreachable("Unknown GlobalValue to print out!"); 46110b57cec5SDimitry Andric } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) { 46120b57cec5SDimitry Andric V->getMetadata()->print(ROS, MST, getModuleFromVal(V)); 46130b57cec5SDimitry Andric } else if (const Constant *C = dyn_cast<Constant>(this)) { 46140b57cec5SDimitry Andric TypePrinting TypePrinter; 46150b57cec5SDimitry Andric TypePrinter.print(C->getType(), OS); 46160b57cec5SDimitry Andric OS << ' '; 4617349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine()); 4618349cc55cSDimitry Andric WriteConstantInternal(OS, C, WriterCtx); 46190b57cec5SDimitry Andric } else if (isa<InlineAsm>(this) || isa<Argument>(this)) { 46200b57cec5SDimitry Andric this->printAsOperand(OS, /* PrintType */ true, MST); 46210b57cec5SDimitry Andric } else { 46220b57cec5SDimitry Andric llvm_unreachable("Unknown value to print out!"); 46230b57cec5SDimitry Andric } 46240b57cec5SDimitry Andric } 46250b57cec5SDimitry Andric 46260b57cec5SDimitry Andric /// Print without a type, skipping the TypePrinting object. 46270b57cec5SDimitry Andric /// 46280b57cec5SDimitry Andric /// \return \c true iff printing was successful. 46290b57cec5SDimitry Andric static bool printWithoutType(const Value &V, raw_ostream &O, 46300b57cec5SDimitry Andric SlotTracker *Machine, const Module *M) { 46310b57cec5SDimitry Andric if (V.hasName() || isa<GlobalValue>(V) || 46320b57cec5SDimitry Andric (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) { 4633349cc55cSDimitry Andric AsmWriterContext WriterCtx(nullptr, Machine, M); 4634349cc55cSDimitry Andric WriteAsOperandInternal(O, &V, WriterCtx); 46350b57cec5SDimitry Andric return true; 46360b57cec5SDimitry Andric } 46370b57cec5SDimitry Andric return false; 46380b57cec5SDimitry Andric } 46390b57cec5SDimitry Andric 46400b57cec5SDimitry Andric static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType, 46410b57cec5SDimitry Andric ModuleSlotTracker &MST) { 46420b57cec5SDimitry Andric TypePrinting TypePrinter(MST.getModule()); 46430b57cec5SDimitry Andric if (PrintType) { 46440b57cec5SDimitry Andric TypePrinter.print(V.getType(), O); 46450b57cec5SDimitry Andric O << ' '; 46460b57cec5SDimitry Andric } 46470b57cec5SDimitry Andric 4648349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine(), MST.getModule()); 4649349cc55cSDimitry Andric WriteAsOperandInternal(O, &V, WriterCtx); 46500b57cec5SDimitry Andric } 46510b57cec5SDimitry Andric 46520b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType, 46530b57cec5SDimitry Andric const Module *M) const { 46540b57cec5SDimitry Andric if (!M) 46550b57cec5SDimitry Andric M = getModuleFromVal(this); 46560b57cec5SDimitry Andric 46570b57cec5SDimitry Andric if (!PrintType) 46580b57cec5SDimitry Andric if (printWithoutType(*this, O, nullptr, M)) 46590b57cec5SDimitry Andric return; 46600b57cec5SDimitry Andric 46610b57cec5SDimitry Andric SlotTracker Machine( 46620b57cec5SDimitry Andric M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this)); 46630b57cec5SDimitry Andric ModuleSlotTracker MST(Machine, M); 46640b57cec5SDimitry Andric printAsOperandImpl(*this, O, PrintType, MST); 46650b57cec5SDimitry Andric } 46660b57cec5SDimitry Andric 46670b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType, 46680b57cec5SDimitry Andric ModuleSlotTracker &MST) const { 46690b57cec5SDimitry Andric if (!PrintType) 46700b57cec5SDimitry Andric if (printWithoutType(*this, O, MST.getMachine(), MST.getModule())) 46710b57cec5SDimitry Andric return; 46720b57cec5SDimitry Andric 46730b57cec5SDimitry Andric printAsOperandImpl(*this, O, PrintType, MST); 46740b57cec5SDimitry Andric } 46750b57cec5SDimitry Andric 4676349cc55cSDimitry Andric /// Recursive version of printMetadataImpl. 4677349cc55cSDimitry Andric static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD, 4678349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 4679349cc55cSDimitry Andric formatted_raw_ostream OS(ROS); 4680349cc55cSDimitry Andric WriteAsOperandInternal(OS, &MD, WriterCtx, /* FromValue */ true); 4681349cc55cSDimitry Andric 4682349cc55cSDimitry Andric auto *N = dyn_cast<MDNode>(&MD); 4683349cc55cSDimitry Andric if (!N || isa<DIExpression>(MD) || isa<DIArgList>(MD)) 4684349cc55cSDimitry Andric return; 4685349cc55cSDimitry Andric 4686349cc55cSDimitry Andric OS << " = "; 4687349cc55cSDimitry Andric WriteMDNodeBodyInternal(OS, N, WriterCtx); 4688349cc55cSDimitry Andric } 4689349cc55cSDimitry Andric 4690349cc55cSDimitry Andric namespace { 4691349cc55cSDimitry Andric struct MDTreeAsmWriterContext : public AsmWriterContext { 4692349cc55cSDimitry Andric unsigned Level; 4693349cc55cSDimitry Andric // {Level, Printed string} 4694349cc55cSDimitry Andric using EntryTy = std::pair<unsigned, std::string>; 4695349cc55cSDimitry Andric SmallVector<EntryTy, 4> Buffer; 4696349cc55cSDimitry Andric 4697349cc55cSDimitry Andric // Used to break the cycle in case there is any. 4698349cc55cSDimitry Andric SmallPtrSet<const Metadata *, 4> Visited; 4699349cc55cSDimitry Andric 4700349cc55cSDimitry Andric raw_ostream &MainOS; 4701349cc55cSDimitry Andric 4702349cc55cSDimitry Andric MDTreeAsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M, 4703349cc55cSDimitry Andric raw_ostream &OS, const Metadata *InitMD) 4704349cc55cSDimitry Andric : AsmWriterContext(TP, ST, M), Level(0U), Visited({InitMD}), MainOS(OS) {} 4705349cc55cSDimitry Andric 4706349cc55cSDimitry Andric void onWriteMetadataAsOperand(const Metadata *MD) override { 4707349cc55cSDimitry Andric if (Visited.count(MD)) 4708349cc55cSDimitry Andric return; 4709349cc55cSDimitry Andric Visited.insert(MD); 4710349cc55cSDimitry Andric 4711349cc55cSDimitry Andric std::string Str; 4712349cc55cSDimitry Andric raw_string_ostream SS(Str); 4713349cc55cSDimitry Andric ++Level; 4714349cc55cSDimitry Andric // A placeholder entry to memorize the correct 4715349cc55cSDimitry Andric // position in buffer. 4716349cc55cSDimitry Andric Buffer.emplace_back(std::make_pair(Level, "")); 4717349cc55cSDimitry Andric unsigned InsertIdx = Buffer.size() - 1; 4718349cc55cSDimitry Andric 4719349cc55cSDimitry Andric printMetadataImplRec(SS, *MD, *this); 4720349cc55cSDimitry Andric Buffer[InsertIdx].second = std::move(SS.str()); 4721349cc55cSDimitry Andric --Level; 4722349cc55cSDimitry Andric } 4723349cc55cSDimitry Andric 4724349cc55cSDimitry Andric ~MDTreeAsmWriterContext() { 4725349cc55cSDimitry Andric for (const auto &Entry : Buffer) { 4726349cc55cSDimitry Andric MainOS << "\n"; 4727349cc55cSDimitry Andric unsigned NumIndent = Entry.first * 2U; 4728349cc55cSDimitry Andric MainOS.indent(NumIndent) << Entry.second; 4729349cc55cSDimitry Andric } 4730349cc55cSDimitry Andric } 4731349cc55cSDimitry Andric }; 4732349cc55cSDimitry Andric } // end anonymous namespace 4733349cc55cSDimitry Andric 47340b57cec5SDimitry Andric static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD, 47350b57cec5SDimitry Andric ModuleSlotTracker &MST, const Module *M, 4736349cc55cSDimitry Andric bool OnlyAsOperand, bool PrintAsTree = false) { 47370b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 47380b57cec5SDimitry Andric 47390b57cec5SDimitry Andric TypePrinting TypePrinter(M); 47400b57cec5SDimitry Andric 4741349cc55cSDimitry Andric std::unique_ptr<AsmWriterContext> WriterCtx; 4742349cc55cSDimitry Andric if (PrintAsTree && !OnlyAsOperand) 4743349cc55cSDimitry Andric WriterCtx = std::make_unique<MDTreeAsmWriterContext>( 4744349cc55cSDimitry Andric &TypePrinter, MST.getMachine(), M, OS, &MD); 4745349cc55cSDimitry Andric else 4746349cc55cSDimitry Andric WriterCtx = 4747349cc55cSDimitry Andric std::make_unique<AsmWriterContext>(&TypePrinter, MST.getMachine(), M); 4748349cc55cSDimitry Andric 4749349cc55cSDimitry Andric WriteAsOperandInternal(OS, &MD, *WriterCtx, /* FromValue */ true); 47500b57cec5SDimitry Andric 47510b57cec5SDimitry Andric auto *N = dyn_cast<MDNode>(&MD); 4752fe6060f1SDimitry Andric if (OnlyAsOperand || !N || isa<DIExpression>(MD) || isa<DIArgList>(MD)) 47530b57cec5SDimitry Andric return; 47540b57cec5SDimitry Andric 47550b57cec5SDimitry Andric OS << " = "; 4756349cc55cSDimitry Andric WriteMDNodeBodyInternal(OS, N, *WriterCtx); 47570b57cec5SDimitry Andric } 47580b57cec5SDimitry Andric 47590b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const { 47600b57cec5SDimitry Andric ModuleSlotTracker MST(M, isa<MDNode>(this)); 47610b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true); 47620b57cec5SDimitry Andric } 47630b57cec5SDimitry Andric 47640b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST, 47650b57cec5SDimitry Andric const Module *M) const { 47660b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true); 47670b57cec5SDimitry Andric } 47680b57cec5SDimitry Andric 47690b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, const Module *M, 47700b57cec5SDimitry Andric bool /*IsForDebug*/) const { 47710b57cec5SDimitry Andric ModuleSlotTracker MST(M, isa<MDNode>(this)); 47720b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); 47730b57cec5SDimitry Andric } 47740b57cec5SDimitry Andric 47750b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST, 47760b57cec5SDimitry Andric const Module *M, bool /*IsForDebug*/) const { 47770b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); 47780b57cec5SDimitry Andric } 47790b57cec5SDimitry Andric 4780349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, const Module *M) const { 4781349cc55cSDimitry Andric ModuleSlotTracker MST(M, true); 4782349cc55cSDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false, 4783349cc55cSDimitry Andric /*PrintAsTree=*/true); 4784349cc55cSDimitry Andric } 4785349cc55cSDimitry Andric 4786349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, ModuleSlotTracker &MST, 4787349cc55cSDimitry Andric const Module *M) const { 4788349cc55cSDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false, 4789349cc55cSDimitry Andric /*PrintAsTree=*/true); 4790349cc55cSDimitry Andric } 4791349cc55cSDimitry Andric 47920b57cec5SDimitry Andric void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const { 47930b57cec5SDimitry Andric SlotTracker SlotTable(this); 47940b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 47950b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this, IsForDebug); 47960b57cec5SDimitry Andric W.printModuleSummaryIndex(); 47970b57cec5SDimitry Andric } 47980b57cec5SDimitry Andric 4799fe6060f1SDimitry Andric void ModuleSlotTracker::collectMDNodes(MachineMDNodeListType &L, unsigned LB, 4800fe6060f1SDimitry Andric unsigned UB) const { 4801fe6060f1SDimitry Andric SlotTracker *ST = MachineStorage.get(); 4802fe6060f1SDimitry Andric if (!ST) 4803fe6060f1SDimitry Andric return; 4804fe6060f1SDimitry Andric 4805fe6060f1SDimitry Andric for (auto &I : llvm::make_range(ST->mdn_begin(), ST->mdn_end())) 4806fe6060f1SDimitry Andric if (I.second >= LB && I.second < UB) 4807fe6060f1SDimitry Andric L.push_back(std::make_pair(I.second, I.first)); 4808fe6060f1SDimitry Andric } 4809fe6060f1SDimitry Andric 48100b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 48110b57cec5SDimitry Andric // Value::dump - allow easy printing of Values from the debugger. 48120b57cec5SDimitry Andric LLVM_DUMP_METHOD 48130b57cec5SDimitry Andric void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; } 48140b57cec5SDimitry Andric 48150b57cec5SDimitry Andric // Type::dump - allow easy printing of Types from the debugger. 48160b57cec5SDimitry Andric LLVM_DUMP_METHOD 48170b57cec5SDimitry Andric void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; } 48180b57cec5SDimitry Andric 48190b57cec5SDimitry Andric // Module::dump() - Allow printing of Modules from the debugger. 48200b57cec5SDimitry Andric LLVM_DUMP_METHOD 48210b57cec5SDimitry Andric void Module::dump() const { 48220b57cec5SDimitry Andric print(dbgs(), nullptr, 48230b57cec5SDimitry Andric /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true); 48240b57cec5SDimitry Andric } 48250b57cec5SDimitry Andric 48260b57cec5SDimitry Andric // Allow printing of Comdats from the debugger. 48270b57cec5SDimitry Andric LLVM_DUMP_METHOD 48280b57cec5SDimitry Andric void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48290b57cec5SDimitry Andric 48300b57cec5SDimitry Andric // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. 48310b57cec5SDimitry Andric LLVM_DUMP_METHOD 48320b57cec5SDimitry Andric void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48330b57cec5SDimitry Andric 48340b57cec5SDimitry Andric LLVM_DUMP_METHOD 48350b57cec5SDimitry Andric void Metadata::dump() const { dump(nullptr); } 48360b57cec5SDimitry Andric 48370b57cec5SDimitry Andric LLVM_DUMP_METHOD 48380b57cec5SDimitry Andric void Metadata::dump(const Module *M) const { 48390b57cec5SDimitry Andric print(dbgs(), M, /*IsForDebug=*/true); 48400b57cec5SDimitry Andric dbgs() << '\n'; 48410b57cec5SDimitry Andric } 48420b57cec5SDimitry Andric 4843349cc55cSDimitry Andric LLVM_DUMP_METHOD 4844349cc55cSDimitry Andric void MDNode::dumpTree() const { dumpTree(nullptr); } 4845349cc55cSDimitry Andric 4846349cc55cSDimitry Andric LLVM_DUMP_METHOD 4847349cc55cSDimitry Andric void MDNode::dumpTree(const Module *M) const { 4848349cc55cSDimitry Andric printTree(dbgs(), M); 4849349cc55cSDimitry Andric dbgs() << '\n'; 4850349cc55cSDimitry Andric } 4851349cc55cSDimitry Andric 48520b57cec5SDimitry Andric // Allow printing of ModuleSummaryIndex from the debugger. 48530b57cec5SDimitry Andric LLVM_DUMP_METHOD 48540b57cec5SDimitry Andric void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48550b57cec5SDimitry Andric #endif 4856