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 226*81ad6265SDimitry Andric if (llvm::is_sorted(List, llvm::less_second())) 2270b57cec5SDimitry Andric // Order is already correct. 228fe6060f1SDimitry Andric return {}; 2290b57cec5SDimitry Andric 2300b57cec5SDimitry Andric // Store the shuffle. 231fe6060f1SDimitry Andric std::vector<unsigned> Shuffle(List.size()); 2320b57cec5SDimitry Andric for (size_t I = 0, E = List.size(); I != E; ++I) 233fe6060f1SDimitry Andric Shuffle[I] = List[I].second; 234fe6060f1SDimitry Andric return Shuffle; 2350b57cec5SDimitry Andric } 2360b57cec5SDimitry Andric 237fe6060f1SDimitry Andric static UseListOrderMap predictUseListOrder(const Module *M) { 2380b57cec5SDimitry Andric OrderMap OM = orderModule(M); 239fe6060f1SDimitry Andric UseListOrderMap ULOM; 240fe6060f1SDimitry Andric for (const auto &Pair : OM) { 241fe6060f1SDimitry Andric const Value *V = Pair.first; 242fe6060f1SDimitry Andric if (V->use_empty() || std::next(V->use_begin()) == V->use_end()) 2430b57cec5SDimitry Andric continue; 2440b57cec5SDimitry Andric 245fe6060f1SDimitry Andric std::vector<unsigned> Shuffle = 246fe6060f1SDimitry Andric predictValueUseListOrder(V, Pair.second, OM); 247fe6060f1SDimitry Andric if (Shuffle.empty()) 248fe6060f1SDimitry Andric continue; 2490b57cec5SDimitry Andric 250fe6060f1SDimitry Andric const Function *F = nullptr; 251fe6060f1SDimitry Andric if (auto *I = dyn_cast<Instruction>(V)) 252fe6060f1SDimitry Andric F = I->getFunction(); 253fe6060f1SDimitry Andric if (auto *A = dyn_cast<Argument>(V)) 254fe6060f1SDimitry Andric F = A->getParent(); 255fe6060f1SDimitry Andric if (auto *BB = dyn_cast<BasicBlock>(V)) 256fe6060f1SDimitry Andric F = BB->getParent(); 257fe6060f1SDimitry Andric ULOM[F][V] = std::move(Shuffle); 258fe6060f1SDimitry Andric } 259fe6060f1SDimitry Andric return ULOM; 2600b57cec5SDimitry Andric } 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric static const Module *getModuleFromVal(const Value *V) { 2630b57cec5SDimitry Andric if (const Argument *MA = dyn_cast<Argument>(V)) 2640b57cec5SDimitry Andric return MA->getParent() ? MA->getParent()->getParent() : nullptr; 2650b57cec5SDimitry Andric 2660b57cec5SDimitry Andric if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) 2670b57cec5SDimitry Andric return BB->getParent() ? BB->getParent()->getParent() : nullptr; 2680b57cec5SDimitry Andric 2690b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) { 2700b57cec5SDimitry Andric const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr; 2710b57cec5SDimitry Andric return M ? M->getParent() : nullptr; 2720b57cec5SDimitry Andric } 2730b57cec5SDimitry Andric 2740b57cec5SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) 2750b57cec5SDimitry Andric return GV->getParent(); 2760b57cec5SDimitry Andric 2770b57cec5SDimitry Andric if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) { 2780b57cec5SDimitry Andric for (const User *U : MAV->users()) 2790b57cec5SDimitry Andric if (isa<Instruction>(U)) 2800b57cec5SDimitry Andric if (const Module *M = getModuleFromVal(U)) 2810b57cec5SDimitry Andric return M; 2820b57cec5SDimitry Andric return nullptr; 2830b57cec5SDimitry Andric } 2840b57cec5SDimitry Andric 2850b57cec5SDimitry Andric return nullptr; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric static void PrintCallingConv(unsigned cc, raw_ostream &Out) { 2890b57cec5SDimitry Andric switch (cc) { 2900b57cec5SDimitry Andric default: Out << "cc" << cc; break; 2910b57cec5SDimitry Andric case CallingConv::Fast: Out << "fastcc"; break; 2920b57cec5SDimitry Andric case CallingConv::Cold: Out << "coldcc"; break; 2930b57cec5SDimitry Andric case CallingConv::WebKit_JS: Out << "webkit_jscc"; break; 2940b57cec5SDimitry Andric case CallingConv::AnyReg: Out << "anyregcc"; break; 2950b57cec5SDimitry Andric case CallingConv::PreserveMost: Out << "preserve_mostcc"; break; 2960b57cec5SDimitry Andric case CallingConv::PreserveAll: Out << "preserve_allcc"; break; 2970b57cec5SDimitry Andric case CallingConv::CXX_FAST_TLS: Out << "cxx_fast_tlscc"; break; 2980b57cec5SDimitry Andric case CallingConv::GHC: Out << "ghccc"; break; 2998bcb0991SDimitry Andric case CallingConv::Tail: Out << "tailcc"; break; 300480093f4SDimitry Andric case CallingConv::CFGuard_Check: Out << "cfguard_checkcc"; break; 3010b57cec5SDimitry Andric case CallingConv::X86_StdCall: Out << "x86_stdcallcc"; break; 3020b57cec5SDimitry Andric case CallingConv::X86_FastCall: Out << "x86_fastcallcc"; break; 3030b57cec5SDimitry Andric case CallingConv::X86_ThisCall: Out << "x86_thiscallcc"; break; 3040b57cec5SDimitry Andric case CallingConv::X86_RegCall: Out << "x86_regcallcc"; break; 3050b57cec5SDimitry Andric case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break; 3060b57cec5SDimitry Andric case CallingConv::Intel_OCL_BI: Out << "intel_ocl_bicc"; break; 3070b57cec5SDimitry Andric case CallingConv::ARM_APCS: Out << "arm_apcscc"; break; 3080b57cec5SDimitry Andric case CallingConv::ARM_AAPCS: Out << "arm_aapcscc"; break; 3090b57cec5SDimitry Andric case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break; 3100b57cec5SDimitry Andric case CallingConv::AArch64_VectorCall: Out << "aarch64_vector_pcs"; break; 311480093f4SDimitry Andric case CallingConv::AArch64_SVE_VectorCall: 312480093f4SDimitry Andric Out << "aarch64_sve_vector_pcs"; 313480093f4SDimitry Andric break; 3140b57cec5SDimitry Andric case CallingConv::MSP430_INTR: Out << "msp430_intrcc"; break; 3150b57cec5SDimitry Andric case CallingConv::AVR_INTR: Out << "avr_intrcc "; break; 3160b57cec5SDimitry Andric case CallingConv::AVR_SIGNAL: Out << "avr_signalcc "; break; 3170b57cec5SDimitry Andric case CallingConv::PTX_Kernel: Out << "ptx_kernel"; break; 3180b57cec5SDimitry Andric case CallingConv::PTX_Device: Out << "ptx_device"; break; 3190b57cec5SDimitry Andric case CallingConv::X86_64_SysV: Out << "x86_64_sysvcc"; break; 3200b57cec5SDimitry Andric case CallingConv::Win64: Out << "win64cc"; break; 3210b57cec5SDimitry Andric case CallingConv::SPIR_FUNC: Out << "spir_func"; break; 3220b57cec5SDimitry Andric case CallingConv::SPIR_KERNEL: Out << "spir_kernel"; break; 3230b57cec5SDimitry Andric case CallingConv::Swift: Out << "swiftcc"; break; 324fe6060f1SDimitry Andric case CallingConv::SwiftTail: Out << "swifttailcc"; break; 3250b57cec5SDimitry Andric case CallingConv::X86_INTR: Out << "x86_intrcc"; break; 3260b57cec5SDimitry Andric case CallingConv::HHVM: Out << "hhvmcc"; break; 3270b57cec5SDimitry Andric case CallingConv::HHVM_C: Out << "hhvm_ccc"; break; 3280b57cec5SDimitry Andric case CallingConv::AMDGPU_VS: Out << "amdgpu_vs"; break; 3290b57cec5SDimitry Andric case CallingConv::AMDGPU_LS: Out << "amdgpu_ls"; break; 3300b57cec5SDimitry Andric case CallingConv::AMDGPU_HS: Out << "amdgpu_hs"; break; 3310b57cec5SDimitry Andric case CallingConv::AMDGPU_ES: Out << "amdgpu_es"; break; 3320b57cec5SDimitry Andric case CallingConv::AMDGPU_GS: Out << "amdgpu_gs"; break; 3330b57cec5SDimitry Andric case CallingConv::AMDGPU_PS: Out << "amdgpu_ps"; break; 3340b57cec5SDimitry Andric case CallingConv::AMDGPU_CS: Out << "amdgpu_cs"; break; 3350b57cec5SDimitry Andric case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break; 336e8d8bef9SDimitry Andric case CallingConv::AMDGPU_Gfx: Out << "amdgpu_gfx"; break; 3370b57cec5SDimitry Andric } 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric enum PrefixType { 3410b57cec5SDimitry Andric GlobalPrefix, 3420b57cec5SDimitry Andric ComdatPrefix, 3430b57cec5SDimitry Andric LabelPrefix, 3440b57cec5SDimitry Andric LocalPrefix, 3450b57cec5SDimitry Andric NoPrefix 3460b57cec5SDimitry Andric }; 3470b57cec5SDimitry Andric 3480b57cec5SDimitry Andric void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) { 3490b57cec5SDimitry Andric assert(!Name.empty() && "Cannot get empty name!"); 3500b57cec5SDimitry Andric 3510b57cec5SDimitry Andric // Scan the name to see if it needs quotes first. 3520b57cec5SDimitry Andric bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0])); 3530b57cec5SDimitry Andric if (!NeedsQuotes) { 3544824e7fdSDimitry Andric for (unsigned char C : Name) { 3550b57cec5SDimitry Andric // By making this unsigned, the value passed in to isalnum will always be 3560b57cec5SDimitry Andric // in the range 0-255. This is important when building with MSVC because 3570b57cec5SDimitry Andric // its implementation will assert. This situation can arise when dealing 3580b57cec5SDimitry Andric // with UTF-8 multibyte characters. 3590b57cec5SDimitry Andric if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' && 3600b57cec5SDimitry Andric C != '_') { 3610b57cec5SDimitry Andric NeedsQuotes = true; 3620b57cec5SDimitry Andric break; 3630b57cec5SDimitry Andric } 3640b57cec5SDimitry Andric } 3650b57cec5SDimitry Andric } 3660b57cec5SDimitry Andric 3670b57cec5SDimitry Andric // If we didn't need any quotes, just write out the name in one blast. 3680b57cec5SDimitry Andric if (!NeedsQuotes) { 3690b57cec5SDimitry Andric OS << Name; 3700b57cec5SDimitry Andric return; 3710b57cec5SDimitry Andric } 3720b57cec5SDimitry Andric 3730b57cec5SDimitry Andric // Okay, we need quotes. Output the quotes and escape any scary characters as 3740b57cec5SDimitry Andric // needed. 3750b57cec5SDimitry Andric OS << '"'; 3760b57cec5SDimitry Andric printEscapedString(Name, OS); 3770b57cec5SDimitry Andric OS << '"'; 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with % 3810b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s 3820b57cec5SDimitry Andric /// (if it has special chars in it). Print it out. 3830b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) { 3840b57cec5SDimitry Andric switch (Prefix) { 3850b57cec5SDimitry Andric case NoPrefix: 3860b57cec5SDimitry Andric break; 3870b57cec5SDimitry Andric case GlobalPrefix: 3880b57cec5SDimitry Andric OS << '@'; 3890b57cec5SDimitry Andric break; 3900b57cec5SDimitry Andric case ComdatPrefix: 3910b57cec5SDimitry Andric OS << '$'; 3920b57cec5SDimitry Andric break; 3930b57cec5SDimitry Andric case LabelPrefix: 3940b57cec5SDimitry Andric break; 3950b57cec5SDimitry Andric case LocalPrefix: 3960b57cec5SDimitry Andric OS << '%'; 3970b57cec5SDimitry Andric break; 3980b57cec5SDimitry Andric } 3990b57cec5SDimitry Andric printLLVMNameWithoutPrefix(OS, Name); 4000b57cec5SDimitry Andric } 4010b57cec5SDimitry Andric 4020b57cec5SDimitry Andric /// Turn the specified name into an 'LLVM name', which is either prefixed with % 4030b57cec5SDimitry Andric /// (if the string only contains simple characters) or is surrounded with ""'s 4040b57cec5SDimitry Andric /// (if it has special chars in it). Print it out. 4050b57cec5SDimitry Andric static void PrintLLVMName(raw_ostream &OS, const Value *V) { 4060b57cec5SDimitry Andric PrintLLVMName(OS, V->getName(), 4070b57cec5SDimitry Andric isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix); 4080b57cec5SDimitry Andric } 4090b57cec5SDimitry Andric 4105ffd83dbSDimitry Andric static void PrintShuffleMask(raw_ostream &Out, Type *Ty, ArrayRef<int> Mask) { 4115ffd83dbSDimitry Andric Out << ", <"; 4125ffd83dbSDimitry Andric if (isa<ScalableVectorType>(Ty)) 4135ffd83dbSDimitry Andric Out << "vscale x "; 4145ffd83dbSDimitry Andric Out << Mask.size() << " x i32> "; 4155ffd83dbSDimitry Andric bool FirstElt = true; 4165ffd83dbSDimitry Andric if (all_of(Mask, [](int Elt) { return Elt == 0; })) { 4175ffd83dbSDimitry Andric Out << "zeroinitializer"; 4185ffd83dbSDimitry Andric } else if (all_of(Mask, [](int Elt) { return Elt == UndefMaskElem; })) { 4195ffd83dbSDimitry Andric Out << "undef"; 4205ffd83dbSDimitry Andric } else { 4215ffd83dbSDimitry Andric Out << "<"; 4225ffd83dbSDimitry Andric for (int Elt : Mask) { 4235ffd83dbSDimitry Andric if (FirstElt) 4245ffd83dbSDimitry Andric FirstElt = false; 4255ffd83dbSDimitry Andric else 4265ffd83dbSDimitry Andric Out << ", "; 4275ffd83dbSDimitry Andric Out << "i32 "; 4285ffd83dbSDimitry Andric if (Elt == UndefMaskElem) 4295ffd83dbSDimitry Andric Out << "undef"; 4305ffd83dbSDimitry Andric else 4315ffd83dbSDimitry Andric Out << Elt; 4325ffd83dbSDimitry Andric } 4335ffd83dbSDimitry Andric Out << ">"; 4345ffd83dbSDimitry Andric } 4355ffd83dbSDimitry Andric } 4365ffd83dbSDimitry Andric 4370b57cec5SDimitry Andric namespace { 4380b57cec5SDimitry Andric 4390b57cec5SDimitry Andric class TypePrinting { 4400b57cec5SDimitry Andric public: 4410b57cec5SDimitry Andric TypePrinting(const Module *M = nullptr) : DeferredM(M) {} 4420b57cec5SDimitry Andric 4430b57cec5SDimitry Andric TypePrinting(const TypePrinting &) = delete; 4440b57cec5SDimitry Andric TypePrinting &operator=(const TypePrinting &) = delete; 4450b57cec5SDimitry Andric 4460b57cec5SDimitry Andric /// The named types that are used by the current module. 4470b57cec5SDimitry Andric TypeFinder &getNamedTypes(); 4480b57cec5SDimitry Andric 4490b57cec5SDimitry Andric /// The numbered types, number to type mapping. 4500b57cec5SDimitry Andric std::vector<StructType *> &getNumberedTypes(); 4510b57cec5SDimitry Andric 4520b57cec5SDimitry Andric bool empty(); 4530b57cec5SDimitry Andric 4540b57cec5SDimitry Andric void print(Type *Ty, raw_ostream &OS); 4550b57cec5SDimitry Andric 4560b57cec5SDimitry Andric void printStructBody(StructType *Ty, raw_ostream &OS); 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric private: 4590b57cec5SDimitry Andric void incorporateTypes(); 4600b57cec5SDimitry Andric 4610b57cec5SDimitry Andric /// A module to process lazily when needed. Set to nullptr as soon as used. 4620b57cec5SDimitry Andric const Module *DeferredM; 4630b57cec5SDimitry Andric 4640b57cec5SDimitry Andric TypeFinder NamedTypes; 4650b57cec5SDimitry Andric 4660b57cec5SDimitry Andric // The numbered types, along with their value. 4670b57cec5SDimitry Andric DenseMap<StructType *, unsigned> Type2Number; 4680b57cec5SDimitry Andric 4690b57cec5SDimitry Andric std::vector<StructType *> NumberedTypes; 4700b57cec5SDimitry Andric }; 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric } // end anonymous namespace 4730b57cec5SDimitry Andric 4740b57cec5SDimitry Andric TypeFinder &TypePrinting::getNamedTypes() { 4750b57cec5SDimitry Andric incorporateTypes(); 4760b57cec5SDimitry Andric return NamedTypes; 4770b57cec5SDimitry Andric } 4780b57cec5SDimitry Andric 4790b57cec5SDimitry Andric std::vector<StructType *> &TypePrinting::getNumberedTypes() { 4800b57cec5SDimitry Andric incorporateTypes(); 4810b57cec5SDimitry Andric 4820b57cec5SDimitry Andric // We know all the numbers that each type is used and we know that it is a 4830b57cec5SDimitry Andric // dense assignment. Convert the map to an index table, if it's not done 4840b57cec5SDimitry Andric // already (judging from the sizes): 4850b57cec5SDimitry Andric if (NumberedTypes.size() == Type2Number.size()) 4860b57cec5SDimitry Andric return NumberedTypes; 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric NumberedTypes.resize(Type2Number.size()); 4890b57cec5SDimitry Andric for (const auto &P : Type2Number) { 4900b57cec5SDimitry Andric assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?"); 4910b57cec5SDimitry Andric assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?"); 4920b57cec5SDimitry Andric NumberedTypes[P.second] = P.first; 4930b57cec5SDimitry Andric } 4940b57cec5SDimitry Andric return NumberedTypes; 4950b57cec5SDimitry Andric } 4960b57cec5SDimitry Andric 4970b57cec5SDimitry Andric bool TypePrinting::empty() { 4980b57cec5SDimitry Andric incorporateTypes(); 4990b57cec5SDimitry Andric return NamedTypes.empty() && Type2Number.empty(); 5000b57cec5SDimitry Andric } 5010b57cec5SDimitry Andric 5020b57cec5SDimitry Andric void TypePrinting::incorporateTypes() { 5030b57cec5SDimitry Andric if (!DeferredM) 5040b57cec5SDimitry Andric return; 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric NamedTypes.run(*DeferredM, false); 5070b57cec5SDimitry Andric DeferredM = nullptr; 5080b57cec5SDimitry Andric 5090b57cec5SDimitry Andric // The list of struct types we got back includes all the struct types, split 5100b57cec5SDimitry Andric // the unnamed ones out to a numbering and remove the anonymous structs. 5110b57cec5SDimitry Andric unsigned NextNumber = 0; 5120b57cec5SDimitry Andric 5130eae32dcSDimitry Andric std::vector<StructType *>::iterator NextToUse = NamedTypes.begin(); 5140eae32dcSDimitry Andric for (StructType *STy : NamedTypes) { 5150b57cec5SDimitry Andric // Ignore anonymous types. 5160b57cec5SDimitry Andric if (STy->isLiteral()) 5170b57cec5SDimitry Andric continue; 5180b57cec5SDimitry Andric 5190b57cec5SDimitry Andric if (STy->getName().empty()) 5200b57cec5SDimitry Andric Type2Number[STy] = NextNumber++; 5210b57cec5SDimitry Andric else 5220b57cec5SDimitry Andric *NextToUse++ = STy; 5230b57cec5SDimitry Andric } 5240b57cec5SDimitry Andric 5250b57cec5SDimitry Andric NamedTypes.erase(NextToUse, NamedTypes.end()); 5260b57cec5SDimitry Andric } 5270b57cec5SDimitry Andric 5280b57cec5SDimitry Andric /// Write the specified type to the specified raw_ostream, making use of type 5290b57cec5SDimitry Andric /// names or up references to shorten the type name where possible. 5300b57cec5SDimitry Andric void TypePrinting::print(Type *Ty, raw_ostream &OS) { 5310b57cec5SDimitry Andric switch (Ty->getTypeID()) { 5320b57cec5SDimitry Andric case Type::VoidTyID: OS << "void"; return; 5330b57cec5SDimitry Andric case Type::HalfTyID: OS << "half"; return; 5345ffd83dbSDimitry Andric case Type::BFloatTyID: OS << "bfloat"; return; 5350b57cec5SDimitry Andric case Type::FloatTyID: OS << "float"; return; 5360b57cec5SDimitry Andric case Type::DoubleTyID: OS << "double"; return; 5370b57cec5SDimitry Andric case Type::X86_FP80TyID: OS << "x86_fp80"; return; 5380b57cec5SDimitry Andric case Type::FP128TyID: OS << "fp128"; return; 5390b57cec5SDimitry Andric case Type::PPC_FP128TyID: OS << "ppc_fp128"; return; 5400b57cec5SDimitry Andric case Type::LabelTyID: OS << "label"; return; 5410b57cec5SDimitry Andric case Type::MetadataTyID: OS << "metadata"; return; 5420b57cec5SDimitry Andric case Type::X86_MMXTyID: OS << "x86_mmx"; return; 543e8d8bef9SDimitry Andric case Type::X86_AMXTyID: OS << "x86_amx"; return; 5440b57cec5SDimitry Andric case Type::TokenTyID: OS << "token"; return; 5450b57cec5SDimitry Andric case Type::IntegerTyID: 5460b57cec5SDimitry Andric OS << 'i' << cast<IntegerType>(Ty)->getBitWidth(); 5470b57cec5SDimitry Andric return; 5480b57cec5SDimitry Andric 5490b57cec5SDimitry Andric case Type::FunctionTyID: { 5500b57cec5SDimitry Andric FunctionType *FTy = cast<FunctionType>(Ty); 5510b57cec5SDimitry Andric print(FTy->getReturnType(), OS); 5520b57cec5SDimitry Andric OS << " ("; 553349cc55cSDimitry Andric ListSeparator LS; 554349cc55cSDimitry Andric for (Type *Ty : FTy->params()) { 555349cc55cSDimitry Andric OS << LS; 556349cc55cSDimitry Andric print(Ty, OS); 5570b57cec5SDimitry Andric } 558349cc55cSDimitry Andric if (FTy->isVarArg()) 559349cc55cSDimitry Andric OS << LS << "..."; 5600b57cec5SDimitry Andric OS << ')'; 5610b57cec5SDimitry Andric return; 5620b57cec5SDimitry Andric } 5630b57cec5SDimitry Andric case Type::StructTyID: { 5640b57cec5SDimitry Andric StructType *STy = cast<StructType>(Ty); 5650b57cec5SDimitry Andric 5660b57cec5SDimitry Andric if (STy->isLiteral()) 5670b57cec5SDimitry Andric return printStructBody(STy, OS); 5680b57cec5SDimitry Andric 5690b57cec5SDimitry Andric if (!STy->getName().empty()) 5700b57cec5SDimitry Andric return PrintLLVMName(OS, STy->getName(), LocalPrefix); 5710b57cec5SDimitry Andric 5720b57cec5SDimitry Andric incorporateTypes(); 5730b57cec5SDimitry Andric const auto I = Type2Number.find(STy); 5740b57cec5SDimitry Andric if (I != Type2Number.end()) 5750b57cec5SDimitry Andric OS << '%' << I->second; 5760b57cec5SDimitry Andric else // Not enumerated, print the hex address. 5770b57cec5SDimitry Andric OS << "%\"type " << STy << '\"'; 5780b57cec5SDimitry Andric return; 5790b57cec5SDimitry Andric } 5800b57cec5SDimitry Andric case Type::PointerTyID: { 5810b57cec5SDimitry Andric PointerType *PTy = cast<PointerType>(Ty); 582fe6060f1SDimitry Andric if (PTy->isOpaque()) { 583fe6060f1SDimitry Andric OS << "ptr"; 584fe6060f1SDimitry Andric if (unsigned AddressSpace = PTy->getAddressSpace()) 585fe6060f1SDimitry Andric OS << " addrspace(" << AddressSpace << ')'; 586fe6060f1SDimitry Andric return; 587fe6060f1SDimitry Andric } 58804eeddc0SDimitry Andric print(PTy->getNonOpaquePointerElementType(), OS); 5890b57cec5SDimitry Andric if (unsigned AddressSpace = PTy->getAddressSpace()) 5900b57cec5SDimitry Andric OS << " addrspace(" << AddressSpace << ')'; 5910b57cec5SDimitry Andric OS << '*'; 5920b57cec5SDimitry Andric return; 5930b57cec5SDimitry Andric } 5940b57cec5SDimitry Andric case Type::ArrayTyID: { 5950b57cec5SDimitry Andric ArrayType *ATy = cast<ArrayType>(Ty); 5960b57cec5SDimitry Andric OS << '[' << ATy->getNumElements() << " x "; 5970b57cec5SDimitry Andric print(ATy->getElementType(), OS); 5980b57cec5SDimitry Andric OS << ']'; 5990b57cec5SDimitry Andric return; 6000b57cec5SDimitry Andric } 6015ffd83dbSDimitry Andric case Type::FixedVectorTyID: 6025ffd83dbSDimitry Andric case Type::ScalableVectorTyID: { 6030b57cec5SDimitry Andric VectorType *PTy = cast<VectorType>(Ty); 6045ffd83dbSDimitry Andric ElementCount EC = PTy->getElementCount(); 6050b57cec5SDimitry Andric OS << "<"; 606e8d8bef9SDimitry Andric if (EC.isScalable()) 6070b57cec5SDimitry Andric OS << "vscale x "; 608e8d8bef9SDimitry Andric OS << EC.getKnownMinValue() << " x "; 6090b57cec5SDimitry Andric print(PTy->getElementType(), OS); 6100b57cec5SDimitry Andric OS << '>'; 6110b57cec5SDimitry Andric return; 6120b57cec5SDimitry Andric } 613*81ad6265SDimitry Andric case Type::DXILPointerTyID: 614*81ad6265SDimitry Andric // DXIL pointer types are only handled by the DirectX backend. To avoid 615*81ad6265SDimitry Andric // extra dependencies we just print the pointer's address here. 616*81ad6265SDimitry Andric OS << "dxil-ptr (" << Ty << ")"; 617*81ad6265SDimitry Andric return; 6180b57cec5SDimitry Andric } 6190b57cec5SDimitry Andric llvm_unreachable("Invalid TypeID"); 6200b57cec5SDimitry Andric } 6210b57cec5SDimitry Andric 6220b57cec5SDimitry Andric void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) { 6230b57cec5SDimitry Andric if (STy->isOpaque()) { 6240b57cec5SDimitry Andric OS << "opaque"; 6250b57cec5SDimitry Andric return; 6260b57cec5SDimitry Andric } 6270b57cec5SDimitry Andric 6280b57cec5SDimitry Andric if (STy->isPacked()) 6290b57cec5SDimitry Andric OS << '<'; 6300b57cec5SDimitry Andric 6310b57cec5SDimitry Andric if (STy->getNumElements() == 0) { 6320b57cec5SDimitry Andric OS << "{}"; 6330b57cec5SDimitry Andric } else { 6340b57cec5SDimitry Andric OS << "{ "; 635349cc55cSDimitry Andric ListSeparator LS; 636349cc55cSDimitry Andric for (Type *Ty : STy->elements()) { 637349cc55cSDimitry Andric OS << LS; 638349cc55cSDimitry Andric print(Ty, OS); 6390b57cec5SDimitry Andric } 6400b57cec5SDimitry Andric 6410b57cec5SDimitry Andric OS << " }"; 6420b57cec5SDimitry Andric } 6430b57cec5SDimitry Andric if (STy->isPacked()) 6440b57cec5SDimitry Andric OS << '>'; 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric 647*81ad6265SDimitry Andric AbstractSlotTrackerStorage::~AbstractSlotTrackerStorage() = default; 648fe6060f1SDimitry Andric 6490b57cec5SDimitry Andric namespace llvm { 6500b57cec5SDimitry Andric 6510b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6520b57cec5SDimitry Andric // SlotTracker Class: Enumerate slot numbers for unnamed values 6530b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 6540b57cec5SDimitry Andric /// This class provides computation of slot numbers for LLVM Assembly writing. 6550b57cec5SDimitry Andric /// 656fe6060f1SDimitry Andric class SlotTracker : public AbstractSlotTrackerStorage { 6570b57cec5SDimitry Andric public: 6580b57cec5SDimitry Andric /// ValueMap - A mapping of Values to slot numbers. 6590b57cec5SDimitry Andric using ValueMap = DenseMap<const Value *, unsigned>; 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric private: 6620b57cec5SDimitry Andric /// TheModule - The module for which we are holding slot numbers. 6630b57cec5SDimitry Andric const Module* TheModule; 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric /// TheFunction - The function for which we are holding slot numbers. 6660b57cec5SDimitry Andric const Function* TheFunction = nullptr; 6670b57cec5SDimitry Andric bool FunctionProcessed = false; 6680b57cec5SDimitry Andric bool ShouldInitializeAllMetadata; 6690b57cec5SDimitry Andric 670fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 671fe6060f1SDimitry Andric ProcessModuleHookFn; 672fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 673fe6060f1SDimitry Andric ProcessFunctionHookFn; 674fe6060f1SDimitry Andric 6750b57cec5SDimitry Andric /// The summary index for which we are holding slot numbers. 6760b57cec5SDimitry Andric const ModuleSummaryIndex *TheIndex = nullptr; 6770b57cec5SDimitry Andric 6780b57cec5SDimitry Andric /// mMap - The slot map for the module level data. 6790b57cec5SDimitry Andric ValueMap mMap; 6800b57cec5SDimitry Andric unsigned mNext = 0; 6810b57cec5SDimitry Andric 6820b57cec5SDimitry Andric /// fMap - The slot map for the function level data. 6830b57cec5SDimitry Andric ValueMap fMap; 6840b57cec5SDimitry Andric unsigned fNext = 0; 6850b57cec5SDimitry Andric 6860b57cec5SDimitry Andric /// mdnMap - Map for MDNodes. 6870b57cec5SDimitry Andric DenseMap<const MDNode*, unsigned> mdnMap; 6880b57cec5SDimitry Andric unsigned mdnNext = 0; 6890b57cec5SDimitry Andric 6900b57cec5SDimitry Andric /// asMap - The slot map for attribute sets. 6910b57cec5SDimitry Andric DenseMap<AttributeSet, unsigned> asMap; 6920b57cec5SDimitry Andric unsigned asNext = 0; 6930b57cec5SDimitry Andric 6940b57cec5SDimitry Andric /// ModulePathMap - The slot map for Module paths used in the summary index. 6950b57cec5SDimitry Andric StringMap<unsigned> ModulePathMap; 6960b57cec5SDimitry Andric unsigned ModulePathNext = 0; 6970b57cec5SDimitry Andric 6980b57cec5SDimitry Andric /// GUIDMap - The slot map for GUIDs used in the summary index. 6990b57cec5SDimitry Andric DenseMap<GlobalValue::GUID, unsigned> GUIDMap; 7000b57cec5SDimitry Andric unsigned GUIDNext = 0; 7010b57cec5SDimitry Andric 7020b57cec5SDimitry Andric /// TypeIdMap - The slot map for type ids used in the summary index. 7030b57cec5SDimitry Andric StringMap<unsigned> TypeIdMap; 7040b57cec5SDimitry Andric unsigned TypeIdNext = 0; 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric public: 7070b57cec5SDimitry Andric /// Construct from a module. 7080b57cec5SDimitry Andric /// 7090b57cec5SDimitry Andric /// If \c ShouldInitializeAllMetadata, initializes all metadata in all 7100b57cec5SDimitry Andric /// functions, giving correct numbering for metadata referenced only from 7110b57cec5SDimitry Andric /// within a function (even if no functions have been initialized). 7120b57cec5SDimitry Andric explicit SlotTracker(const Module *M, 7130b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false); 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric /// Construct from a function, starting out in incorp state. 7160b57cec5SDimitry Andric /// 7170b57cec5SDimitry Andric /// If \c ShouldInitializeAllMetadata, initializes all metadata in all 7180b57cec5SDimitry Andric /// functions, giving correct numbering for metadata referenced only from 7190b57cec5SDimitry Andric /// within a function (even if no functions have been initialized). 7200b57cec5SDimitry Andric explicit SlotTracker(const Function *F, 7210b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false); 7220b57cec5SDimitry Andric 7230b57cec5SDimitry Andric /// Construct from a module summary index. 7240b57cec5SDimitry Andric explicit SlotTracker(const ModuleSummaryIndex *Index); 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric SlotTracker(const SlotTracker &) = delete; 7270b57cec5SDimitry Andric SlotTracker &operator=(const SlotTracker &) = delete; 7280b57cec5SDimitry Andric 729fe6060f1SDimitry Andric ~SlotTracker() = default; 730fe6060f1SDimitry Andric 731fe6060f1SDimitry Andric void setProcessHook( 732fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)>); 733fe6060f1SDimitry Andric void setProcessHook(std::function<void(AbstractSlotTrackerStorage *, 734fe6060f1SDimitry Andric const Function *, bool)>); 735fe6060f1SDimitry Andric 736fe6060f1SDimitry Andric unsigned getNextMetadataSlot() override { return mdnNext; } 737fe6060f1SDimitry Andric 738fe6060f1SDimitry Andric void createMetadataSlot(const MDNode *N) override; 739fe6060f1SDimitry Andric 7400b57cec5SDimitry Andric /// Return the slot number of the specified value in it's type 7410b57cec5SDimitry Andric /// plane. If something is not in the SlotTracker, return -1. 7420b57cec5SDimitry Andric int getLocalSlot(const Value *V); 7430b57cec5SDimitry Andric int getGlobalSlot(const GlobalValue *V); 744fe6060f1SDimitry Andric int getMetadataSlot(const MDNode *N) override; 7450b57cec5SDimitry Andric int getAttributeGroupSlot(AttributeSet AS); 7460b57cec5SDimitry Andric int getModulePathSlot(StringRef Path); 7470b57cec5SDimitry Andric int getGUIDSlot(GlobalValue::GUID GUID); 7480b57cec5SDimitry Andric int getTypeIdSlot(StringRef Id); 7490b57cec5SDimitry Andric 7500b57cec5SDimitry Andric /// If you'd like to deal with a function instead of just a module, use 7510b57cec5SDimitry Andric /// this method to get its data into the SlotTracker. 7520b57cec5SDimitry Andric void incorporateFunction(const Function *F) { 7530b57cec5SDimitry Andric TheFunction = F; 7540b57cec5SDimitry Andric FunctionProcessed = false; 7550b57cec5SDimitry Andric } 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric const Function *getFunction() const { return TheFunction; } 7580b57cec5SDimitry Andric 7590b57cec5SDimitry Andric /// After calling incorporateFunction, use this method to remove the 7600b57cec5SDimitry Andric /// most recently incorporated function from the SlotTracker. This 7610b57cec5SDimitry Andric /// will reset the state of the machine back to just the module contents. 7620b57cec5SDimitry Andric void purgeFunction(); 7630b57cec5SDimitry Andric 7640b57cec5SDimitry Andric /// MDNode map iterators. 7650b57cec5SDimitry Andric using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator; 7660b57cec5SDimitry Andric 7670b57cec5SDimitry Andric mdn_iterator mdn_begin() { return mdnMap.begin(); } 7680b57cec5SDimitry Andric mdn_iterator mdn_end() { return mdnMap.end(); } 7690b57cec5SDimitry Andric unsigned mdn_size() const { return mdnMap.size(); } 7700b57cec5SDimitry Andric bool mdn_empty() const { return mdnMap.empty(); } 7710b57cec5SDimitry Andric 7720b57cec5SDimitry Andric /// AttributeSet map iterators. 7730b57cec5SDimitry Andric using as_iterator = DenseMap<AttributeSet, unsigned>::iterator; 7740b57cec5SDimitry Andric 7750b57cec5SDimitry Andric as_iterator as_begin() { return asMap.begin(); } 7760b57cec5SDimitry Andric as_iterator as_end() { return asMap.end(); } 7770b57cec5SDimitry Andric unsigned as_size() const { return asMap.size(); } 7780b57cec5SDimitry Andric bool as_empty() const { return asMap.empty(); } 7790b57cec5SDimitry Andric 7800b57cec5SDimitry Andric /// GUID map iterators. 7810b57cec5SDimitry Andric using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator; 7820b57cec5SDimitry Andric 7830b57cec5SDimitry Andric /// These functions do the actual initialization. 7840b57cec5SDimitry Andric inline void initializeIfNeeded(); 7855ffd83dbSDimitry Andric int initializeIndexIfNeeded(); 7860b57cec5SDimitry Andric 7870b57cec5SDimitry Andric // Implementation Details 7880b57cec5SDimitry Andric private: 7890b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. 7900b57cec5SDimitry Andric void CreateModuleSlot(const GlobalValue *V); 7910b57cec5SDimitry Andric 7920b57cec5SDimitry Andric /// CreateMetadataSlot - Insert the specified MDNode* into the slot table. 7930b57cec5SDimitry Andric void CreateMetadataSlot(const MDNode *N); 7940b57cec5SDimitry Andric 7950b57cec5SDimitry Andric /// CreateFunctionSlot - Insert the specified Value* into the slot table. 7960b57cec5SDimitry Andric void CreateFunctionSlot(const Value *V); 7970b57cec5SDimitry Andric 7980b57cec5SDimitry Andric /// Insert the specified AttributeSet into the slot table. 7990b57cec5SDimitry Andric void CreateAttributeSetSlot(AttributeSet AS); 8000b57cec5SDimitry Andric 8010b57cec5SDimitry Andric inline void CreateModulePathSlot(StringRef Path); 8020b57cec5SDimitry Andric void CreateGUIDSlot(GlobalValue::GUID GUID); 8030b57cec5SDimitry Andric void CreateTypeIdSlot(StringRef Id); 8040b57cec5SDimitry Andric 8050b57cec5SDimitry Andric /// Add all of the module level global variables (and their initializers) 8060b57cec5SDimitry Andric /// and function declarations, but not the contents of those functions. 8070b57cec5SDimitry Andric void processModule(); 8085ffd83dbSDimitry Andric // Returns number of allocated slots 8095ffd83dbSDimitry Andric int processIndex(); 8100b57cec5SDimitry Andric 8110b57cec5SDimitry Andric /// Add all of the functions arguments, basic blocks, and instructions. 8120b57cec5SDimitry Andric void processFunction(); 8130b57cec5SDimitry Andric 8140b57cec5SDimitry Andric /// Add the metadata directly attached to a GlobalObject. 8150b57cec5SDimitry Andric void processGlobalObjectMetadata(const GlobalObject &GO); 8160b57cec5SDimitry Andric 8170b57cec5SDimitry Andric /// Add all of the metadata from a function. 8180b57cec5SDimitry Andric void processFunctionMetadata(const Function &F); 8190b57cec5SDimitry Andric 8200b57cec5SDimitry Andric /// Add all of the metadata from an instruction. 8210b57cec5SDimitry Andric void processInstructionMetadata(const Instruction &I); 8220b57cec5SDimitry Andric }; 8230b57cec5SDimitry Andric 8240b57cec5SDimitry Andric } // end namespace llvm 8250b57cec5SDimitry Andric 8260b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M, 8270b57cec5SDimitry Andric const Function *F) 8280b57cec5SDimitry Andric : M(M), F(F), Machine(&Machine) {} 8290b57cec5SDimitry Andric 8300b57cec5SDimitry Andric ModuleSlotTracker::ModuleSlotTracker(const Module *M, 8310b57cec5SDimitry Andric bool ShouldInitializeAllMetadata) 8320b57cec5SDimitry Andric : ShouldCreateStorage(M), 8330b57cec5SDimitry Andric ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {} 8340b57cec5SDimitry Andric 8350b57cec5SDimitry Andric ModuleSlotTracker::~ModuleSlotTracker() = default; 8360b57cec5SDimitry Andric 8370b57cec5SDimitry Andric SlotTracker *ModuleSlotTracker::getMachine() { 8380b57cec5SDimitry Andric if (!ShouldCreateStorage) 8390b57cec5SDimitry Andric return Machine; 8400b57cec5SDimitry Andric 8410b57cec5SDimitry Andric ShouldCreateStorage = false; 8420b57cec5SDimitry Andric MachineStorage = 8438bcb0991SDimitry Andric std::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata); 8440b57cec5SDimitry Andric Machine = MachineStorage.get(); 845fe6060f1SDimitry Andric if (ProcessModuleHookFn) 846fe6060f1SDimitry Andric Machine->setProcessHook(ProcessModuleHookFn); 847fe6060f1SDimitry Andric if (ProcessFunctionHookFn) 848fe6060f1SDimitry Andric Machine->setProcessHook(ProcessFunctionHookFn); 8490b57cec5SDimitry Andric return Machine; 8500b57cec5SDimitry Andric } 8510b57cec5SDimitry Andric 8520b57cec5SDimitry Andric void ModuleSlotTracker::incorporateFunction(const Function &F) { 8530b57cec5SDimitry Andric // Using getMachine() may lazily create the slot tracker. 8540b57cec5SDimitry Andric if (!getMachine()) 8550b57cec5SDimitry Andric return; 8560b57cec5SDimitry Andric 8570b57cec5SDimitry Andric // Nothing to do if this is the right function already. 8580b57cec5SDimitry Andric if (this->F == &F) 8590b57cec5SDimitry Andric return; 8600b57cec5SDimitry Andric if (this->F) 8610b57cec5SDimitry Andric Machine->purgeFunction(); 8620b57cec5SDimitry Andric Machine->incorporateFunction(&F); 8630b57cec5SDimitry Andric this->F = &F; 8640b57cec5SDimitry Andric } 8650b57cec5SDimitry Andric 8660b57cec5SDimitry Andric int ModuleSlotTracker::getLocalSlot(const Value *V) { 8670b57cec5SDimitry Andric assert(F && "No function incorporated"); 8680b57cec5SDimitry Andric return Machine->getLocalSlot(V); 8690b57cec5SDimitry Andric } 8700b57cec5SDimitry Andric 871fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook( 872fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 873fe6060f1SDimitry Andric Fn) { 874fe6060f1SDimitry Andric ProcessModuleHookFn = Fn; 875fe6060f1SDimitry Andric } 876fe6060f1SDimitry Andric 877fe6060f1SDimitry Andric void ModuleSlotTracker::setProcessHook( 878fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 879fe6060f1SDimitry Andric Fn) { 880fe6060f1SDimitry Andric ProcessFunctionHookFn = Fn; 881fe6060f1SDimitry Andric } 882fe6060f1SDimitry Andric 8830b57cec5SDimitry Andric static SlotTracker *createSlotTracker(const Value *V) { 8840b57cec5SDimitry Andric if (const Argument *FA = dyn_cast<Argument>(V)) 8850b57cec5SDimitry Andric return new SlotTracker(FA->getParent()); 8860b57cec5SDimitry Andric 8870b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(V)) 8880b57cec5SDimitry Andric if (I->getParent()) 8890b57cec5SDimitry Andric return new SlotTracker(I->getParent()->getParent()); 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric if (const BasicBlock *BB = dyn_cast<BasicBlock>(V)) 8920b57cec5SDimitry Andric return new SlotTracker(BB->getParent()); 8930b57cec5SDimitry Andric 8940b57cec5SDimitry Andric if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 8950b57cec5SDimitry Andric return new SlotTracker(GV->getParent()); 8960b57cec5SDimitry Andric 8970b57cec5SDimitry Andric if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V)) 8980b57cec5SDimitry Andric return new SlotTracker(GA->getParent()); 8990b57cec5SDimitry Andric 9000b57cec5SDimitry Andric if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V)) 9010b57cec5SDimitry Andric return new SlotTracker(GIF->getParent()); 9020b57cec5SDimitry Andric 9030b57cec5SDimitry Andric if (const Function *Func = dyn_cast<Function>(V)) 9040b57cec5SDimitry Andric return new SlotTracker(Func); 9050b57cec5SDimitry Andric 9060b57cec5SDimitry Andric return nullptr; 9070b57cec5SDimitry Andric } 9080b57cec5SDimitry Andric 9090b57cec5SDimitry Andric #if 0 9100b57cec5SDimitry Andric #define ST_DEBUG(X) dbgs() << X 9110b57cec5SDimitry Andric #else 9120b57cec5SDimitry Andric #define ST_DEBUG(X) 9130b57cec5SDimitry Andric #endif 9140b57cec5SDimitry Andric 9150b57cec5SDimitry Andric // Module level constructor. Causes the contents of the Module (sans functions) 9160b57cec5SDimitry Andric // to be added to the slot table. 9170b57cec5SDimitry Andric SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata) 9180b57cec5SDimitry Andric : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {} 9190b57cec5SDimitry Andric 9200b57cec5SDimitry Andric // Function level constructor. Causes the contents of the Module and the one 9210b57cec5SDimitry Andric // function provided to be added to the slot table. 9220b57cec5SDimitry Andric SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata) 9230b57cec5SDimitry Andric : TheModule(F ? F->getParent() : nullptr), TheFunction(F), 9240b57cec5SDimitry Andric ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {} 9250b57cec5SDimitry Andric 9260b57cec5SDimitry Andric SlotTracker::SlotTracker(const ModuleSummaryIndex *Index) 9270b57cec5SDimitry Andric : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {} 9280b57cec5SDimitry Andric 9290b57cec5SDimitry Andric inline void SlotTracker::initializeIfNeeded() { 9300b57cec5SDimitry Andric if (TheModule) { 9310b57cec5SDimitry Andric processModule(); 9320b57cec5SDimitry Andric TheModule = nullptr; ///< Prevent re-processing next time we're called. 9330b57cec5SDimitry Andric } 9340b57cec5SDimitry Andric 9350b57cec5SDimitry Andric if (TheFunction && !FunctionProcessed) 9360b57cec5SDimitry Andric processFunction(); 9370b57cec5SDimitry Andric } 9380b57cec5SDimitry Andric 9395ffd83dbSDimitry Andric int SlotTracker::initializeIndexIfNeeded() { 9400b57cec5SDimitry Andric if (!TheIndex) 9415ffd83dbSDimitry Andric return 0; 9425ffd83dbSDimitry Andric int NumSlots = processIndex(); 9430b57cec5SDimitry Andric TheIndex = nullptr; ///< Prevent re-processing next time we're called. 9445ffd83dbSDimitry Andric return NumSlots; 9450b57cec5SDimitry Andric } 9460b57cec5SDimitry Andric 9470b57cec5SDimitry Andric // Iterate through all the global variables, functions, and global 9480b57cec5SDimitry Andric // variable initializers and create slots for them. 9490b57cec5SDimitry Andric void SlotTracker::processModule() { 9500b57cec5SDimitry Andric ST_DEBUG("begin processModule!\n"); 9510b57cec5SDimitry Andric 9520b57cec5SDimitry Andric // Add all of the unnamed global variables to the value table. 9530b57cec5SDimitry Andric for (const GlobalVariable &Var : TheModule->globals()) { 9540b57cec5SDimitry Andric if (!Var.hasName()) 9550b57cec5SDimitry Andric CreateModuleSlot(&Var); 9560b57cec5SDimitry Andric processGlobalObjectMetadata(Var); 9570b57cec5SDimitry Andric auto Attrs = Var.getAttributes(); 9580b57cec5SDimitry Andric if (Attrs.hasAttributes()) 9590b57cec5SDimitry Andric CreateAttributeSetSlot(Attrs); 9600b57cec5SDimitry Andric } 9610b57cec5SDimitry Andric 9620b57cec5SDimitry Andric for (const GlobalAlias &A : TheModule->aliases()) { 9630b57cec5SDimitry Andric if (!A.hasName()) 9640b57cec5SDimitry Andric CreateModuleSlot(&A); 9650b57cec5SDimitry Andric } 9660b57cec5SDimitry Andric 9670b57cec5SDimitry Andric for (const GlobalIFunc &I : TheModule->ifuncs()) { 9680b57cec5SDimitry Andric if (!I.hasName()) 9690b57cec5SDimitry Andric CreateModuleSlot(&I); 9700b57cec5SDimitry Andric } 9710b57cec5SDimitry Andric 9720b57cec5SDimitry Andric // Add metadata used by named metadata. 9730b57cec5SDimitry Andric for (const NamedMDNode &NMD : TheModule->named_metadata()) { 9740b57cec5SDimitry Andric for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i) 9750b57cec5SDimitry Andric CreateMetadataSlot(NMD.getOperand(i)); 9760b57cec5SDimitry Andric } 9770b57cec5SDimitry Andric 9780b57cec5SDimitry Andric for (const Function &F : *TheModule) { 9790b57cec5SDimitry Andric if (!F.hasName()) 9800b57cec5SDimitry Andric // Add all the unnamed functions to the table. 9810b57cec5SDimitry Andric CreateModuleSlot(&F); 9820b57cec5SDimitry Andric 9830b57cec5SDimitry Andric if (ShouldInitializeAllMetadata) 9840b57cec5SDimitry Andric processFunctionMetadata(F); 9850b57cec5SDimitry Andric 9860b57cec5SDimitry Andric // Add all the function attributes to the table. 9870b57cec5SDimitry Andric // FIXME: Add attributes of other objects? 988349cc55cSDimitry Andric AttributeSet FnAttrs = F.getAttributes().getFnAttrs(); 9890b57cec5SDimitry Andric if (FnAttrs.hasAttributes()) 9900b57cec5SDimitry Andric CreateAttributeSetSlot(FnAttrs); 9910b57cec5SDimitry Andric } 9920b57cec5SDimitry Andric 993fe6060f1SDimitry Andric if (ProcessModuleHookFn) 994fe6060f1SDimitry Andric ProcessModuleHookFn(this, TheModule, ShouldInitializeAllMetadata); 995fe6060f1SDimitry Andric 9960b57cec5SDimitry Andric ST_DEBUG("end processModule!\n"); 9970b57cec5SDimitry Andric } 9980b57cec5SDimitry Andric 9990b57cec5SDimitry Andric // Process the arguments, basic blocks, and instructions of a function. 10000b57cec5SDimitry Andric void SlotTracker::processFunction() { 10010b57cec5SDimitry Andric ST_DEBUG("begin processFunction!\n"); 10020b57cec5SDimitry Andric fNext = 0; 10030b57cec5SDimitry Andric 10040b57cec5SDimitry Andric // Process function metadata if it wasn't hit at the module-level. 10050b57cec5SDimitry Andric if (!ShouldInitializeAllMetadata) 10060b57cec5SDimitry Andric processFunctionMetadata(*TheFunction); 10070b57cec5SDimitry Andric 10080b57cec5SDimitry Andric // Add all the function arguments with no names. 10090b57cec5SDimitry Andric for(Function::const_arg_iterator AI = TheFunction->arg_begin(), 10100b57cec5SDimitry Andric AE = TheFunction->arg_end(); AI != AE; ++AI) 10110b57cec5SDimitry Andric if (!AI->hasName()) 10120b57cec5SDimitry Andric CreateFunctionSlot(&*AI); 10130b57cec5SDimitry Andric 10140b57cec5SDimitry Andric ST_DEBUG("Inserting Instructions:\n"); 10150b57cec5SDimitry Andric 10160b57cec5SDimitry Andric // Add all of the basic blocks and instructions with no names. 10170b57cec5SDimitry Andric for (auto &BB : *TheFunction) { 10180b57cec5SDimitry Andric if (!BB.hasName()) 10190b57cec5SDimitry Andric CreateFunctionSlot(&BB); 10200b57cec5SDimitry Andric 10210b57cec5SDimitry Andric for (auto &I : BB) { 10220b57cec5SDimitry Andric if (!I.getType()->isVoidTy() && !I.hasName()) 10230b57cec5SDimitry Andric CreateFunctionSlot(&I); 10240b57cec5SDimitry Andric 10250b57cec5SDimitry Andric // We allow direct calls to any llvm.foo function here, because the 10260b57cec5SDimitry Andric // target may not be linked into the optimizer. 10270b57cec5SDimitry Andric if (const auto *Call = dyn_cast<CallBase>(&I)) { 10280b57cec5SDimitry Andric // Add all the call attributes to the table. 1029349cc55cSDimitry Andric AttributeSet Attrs = Call->getAttributes().getFnAttrs(); 10300b57cec5SDimitry Andric if (Attrs.hasAttributes()) 10310b57cec5SDimitry Andric CreateAttributeSetSlot(Attrs); 10320b57cec5SDimitry Andric } 10330b57cec5SDimitry Andric } 10340b57cec5SDimitry Andric } 10350b57cec5SDimitry Andric 1036fe6060f1SDimitry Andric if (ProcessFunctionHookFn) 1037fe6060f1SDimitry Andric ProcessFunctionHookFn(this, TheFunction, ShouldInitializeAllMetadata); 1038fe6060f1SDimitry Andric 10390b57cec5SDimitry Andric FunctionProcessed = true; 10400b57cec5SDimitry Andric 10410b57cec5SDimitry Andric ST_DEBUG("end processFunction!\n"); 10420b57cec5SDimitry Andric } 10430b57cec5SDimitry Andric 10440b57cec5SDimitry Andric // Iterate through all the GUID in the index and create slots for them. 10455ffd83dbSDimitry Andric int SlotTracker::processIndex() { 10460b57cec5SDimitry Andric ST_DEBUG("begin processIndex!\n"); 10470b57cec5SDimitry Andric assert(TheIndex); 10480b57cec5SDimitry Andric 10490b57cec5SDimitry Andric // The first block of slots are just the module ids, which start at 0 and are 10500b57cec5SDimitry Andric // assigned consecutively. Since the StringMap iteration order isn't 10510b57cec5SDimitry Andric // guaranteed, use a std::map to order by module ID before assigning slots. 10520b57cec5SDimitry Andric std::map<uint64_t, StringRef> ModuleIdToPathMap; 10530b57cec5SDimitry Andric for (auto &ModPath : TheIndex->modulePaths()) 10540b57cec5SDimitry Andric ModuleIdToPathMap[ModPath.second.first] = ModPath.first(); 10550b57cec5SDimitry Andric for (auto &ModPair : ModuleIdToPathMap) 10560b57cec5SDimitry Andric CreateModulePathSlot(ModPair.second); 10570b57cec5SDimitry Andric 10580b57cec5SDimitry Andric // Start numbering the GUIDs after the module ids. 10590b57cec5SDimitry Andric GUIDNext = ModulePathNext; 10600b57cec5SDimitry Andric 10610b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) 10620b57cec5SDimitry Andric CreateGUIDSlot(GlobalList.first); 10630b57cec5SDimitry Andric 10645ffd83dbSDimitry Andric for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) 10655ffd83dbSDimitry Andric CreateGUIDSlot(GlobalValue::getGUID(TId.first)); 10665ffd83dbSDimitry Andric 10670b57cec5SDimitry Andric // Start numbering the TypeIds after the GUIDs. 10680b57cec5SDimitry Andric TypeIdNext = GUIDNext; 1069fe6060f1SDimitry Andric for (const auto &TID : TheIndex->typeIds()) 1070fe6060f1SDimitry Andric CreateTypeIdSlot(TID.second.first); 10710b57cec5SDimitry Andric 10720b57cec5SDimitry Andric ST_DEBUG("end processIndex!\n"); 10735ffd83dbSDimitry Andric return TypeIdNext; 10740b57cec5SDimitry Andric } 10750b57cec5SDimitry Andric 10760b57cec5SDimitry Andric void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) { 10770b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 10780b57cec5SDimitry Andric GO.getAllMetadata(MDs); 10790b57cec5SDimitry Andric for (auto &MD : MDs) 10800b57cec5SDimitry Andric CreateMetadataSlot(MD.second); 10810b57cec5SDimitry Andric } 10820b57cec5SDimitry Andric 10830b57cec5SDimitry Andric void SlotTracker::processFunctionMetadata(const Function &F) { 10840b57cec5SDimitry Andric processGlobalObjectMetadata(F); 10850b57cec5SDimitry Andric for (auto &BB : F) { 10860b57cec5SDimitry Andric for (auto &I : BB) 10870b57cec5SDimitry Andric processInstructionMetadata(I); 10880b57cec5SDimitry Andric } 10890b57cec5SDimitry Andric } 10900b57cec5SDimitry Andric 10910b57cec5SDimitry Andric void SlotTracker::processInstructionMetadata(const Instruction &I) { 10920b57cec5SDimitry Andric // Process metadata used directly by intrinsics. 10930b57cec5SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I)) 10940b57cec5SDimitry Andric if (Function *F = CI->getCalledFunction()) 10950b57cec5SDimitry Andric if (F->isIntrinsic()) 10960b57cec5SDimitry Andric for (auto &Op : I.operands()) 10970b57cec5SDimitry Andric if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op)) 10980b57cec5SDimitry Andric if (MDNode *N = dyn_cast<MDNode>(V->getMetadata())) 10990b57cec5SDimitry Andric CreateMetadataSlot(N); 11000b57cec5SDimitry Andric 11010b57cec5SDimitry Andric // Process metadata attached to this instruction. 11020b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 11030b57cec5SDimitry Andric I.getAllMetadata(MDs); 11040b57cec5SDimitry Andric for (auto &MD : MDs) 11050b57cec5SDimitry Andric CreateMetadataSlot(MD.second); 11060b57cec5SDimitry Andric } 11070b57cec5SDimitry Andric 11080b57cec5SDimitry Andric /// Clean up after incorporating a function. This is the only way to get out of 11090b57cec5SDimitry Andric /// the function incorporation state that affects get*Slot/Create*Slot. Function 11100b57cec5SDimitry Andric /// incorporation state is indicated by TheFunction != 0. 11110b57cec5SDimitry Andric void SlotTracker::purgeFunction() { 11120b57cec5SDimitry Andric ST_DEBUG("begin purgeFunction!\n"); 11130b57cec5SDimitry Andric fMap.clear(); // Simply discard the function level map 11140b57cec5SDimitry Andric TheFunction = nullptr; 11150b57cec5SDimitry Andric FunctionProcessed = false; 11160b57cec5SDimitry Andric ST_DEBUG("end purgeFunction!\n"); 11170b57cec5SDimitry Andric } 11180b57cec5SDimitry Andric 11190b57cec5SDimitry Andric /// getGlobalSlot - Get the slot number of a global value. 11200b57cec5SDimitry Andric int SlotTracker::getGlobalSlot(const GlobalValue *V) { 11210b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11220b57cec5SDimitry Andric initializeIfNeeded(); 11230b57cec5SDimitry Andric 11240b57cec5SDimitry Andric // Find the value in the module map 11250b57cec5SDimitry Andric ValueMap::iterator MI = mMap.find(V); 11260b57cec5SDimitry Andric return MI == mMap.end() ? -1 : (int)MI->second; 11270b57cec5SDimitry Andric } 11280b57cec5SDimitry Andric 1129fe6060f1SDimitry Andric void SlotTracker::setProcessHook( 1130fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Module *, bool)> 1131fe6060f1SDimitry Andric Fn) { 1132fe6060f1SDimitry Andric ProcessModuleHookFn = Fn; 1133fe6060f1SDimitry Andric } 1134fe6060f1SDimitry Andric 1135fe6060f1SDimitry Andric void SlotTracker::setProcessHook( 1136fe6060f1SDimitry Andric std::function<void(AbstractSlotTrackerStorage *, const Function *, bool)> 1137fe6060f1SDimitry Andric Fn) { 1138fe6060f1SDimitry Andric ProcessFunctionHookFn = Fn; 1139fe6060f1SDimitry Andric } 1140fe6060f1SDimitry Andric 1141fe6060f1SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode. 1142fe6060f1SDimitry Andric void SlotTracker::createMetadataSlot(const MDNode *N) { CreateMetadataSlot(N); } 1143fe6060f1SDimitry Andric 11440b57cec5SDimitry Andric /// getMetadataSlot - Get the slot number of a MDNode. 11450b57cec5SDimitry Andric int SlotTracker::getMetadataSlot(const MDNode *N) { 11460b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11470b57cec5SDimitry Andric initializeIfNeeded(); 11480b57cec5SDimitry Andric 11490b57cec5SDimitry Andric // Find the MDNode in the module map 11500b57cec5SDimitry Andric mdn_iterator MI = mdnMap.find(N); 11510b57cec5SDimitry Andric return MI == mdnMap.end() ? -1 : (int)MI->second; 11520b57cec5SDimitry Andric } 11530b57cec5SDimitry Andric 11540b57cec5SDimitry Andric /// getLocalSlot - Get the slot number for a value that is local to a function. 11550b57cec5SDimitry Andric int SlotTracker::getLocalSlot(const Value *V) { 11560b57cec5SDimitry Andric assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!"); 11570b57cec5SDimitry Andric 11580b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11590b57cec5SDimitry Andric initializeIfNeeded(); 11600b57cec5SDimitry Andric 11610b57cec5SDimitry Andric ValueMap::iterator FI = fMap.find(V); 11620b57cec5SDimitry Andric return FI == fMap.end() ? -1 : (int)FI->second; 11630b57cec5SDimitry Andric } 11640b57cec5SDimitry Andric 11650b57cec5SDimitry Andric int SlotTracker::getAttributeGroupSlot(AttributeSet AS) { 11660b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11670b57cec5SDimitry Andric initializeIfNeeded(); 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric // Find the AttributeSet in the module map. 11700b57cec5SDimitry Andric as_iterator AI = asMap.find(AS); 11710b57cec5SDimitry Andric return AI == asMap.end() ? -1 : (int)AI->second; 11720b57cec5SDimitry Andric } 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric int SlotTracker::getModulePathSlot(StringRef Path) { 11750b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11760b57cec5SDimitry Andric initializeIndexIfNeeded(); 11770b57cec5SDimitry Andric 11780b57cec5SDimitry Andric // Find the Module path in the map 11790b57cec5SDimitry Andric auto I = ModulePathMap.find(Path); 11800b57cec5SDimitry Andric return I == ModulePathMap.end() ? -1 : (int)I->second; 11810b57cec5SDimitry Andric } 11820b57cec5SDimitry Andric 11830b57cec5SDimitry Andric int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) { 11840b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11850b57cec5SDimitry Andric initializeIndexIfNeeded(); 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric // Find the GUID in the map 11880b57cec5SDimitry Andric guid_iterator I = GUIDMap.find(GUID); 11890b57cec5SDimitry Andric return I == GUIDMap.end() ? -1 : (int)I->second; 11900b57cec5SDimitry Andric } 11910b57cec5SDimitry Andric 11920b57cec5SDimitry Andric int SlotTracker::getTypeIdSlot(StringRef Id) { 11930b57cec5SDimitry Andric // Check for uninitialized state and do lazy initialization. 11940b57cec5SDimitry Andric initializeIndexIfNeeded(); 11950b57cec5SDimitry Andric 11960b57cec5SDimitry Andric // Find the TypeId string in the map 11970b57cec5SDimitry Andric auto I = TypeIdMap.find(Id); 11980b57cec5SDimitry Andric return I == TypeIdMap.end() ? -1 : (int)I->second; 11990b57cec5SDimitry Andric } 12000b57cec5SDimitry Andric 12010b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table. 12020b57cec5SDimitry Andric void SlotTracker::CreateModuleSlot(const GlobalValue *V) { 12030b57cec5SDimitry Andric assert(V && "Can't insert a null Value into SlotTracker!"); 12040b57cec5SDimitry Andric assert(!V->getType()->isVoidTy() && "Doesn't need a slot!"); 12050b57cec5SDimitry Andric assert(!V->hasName() && "Doesn't need a slot!"); 12060b57cec5SDimitry Andric 12070b57cec5SDimitry Andric unsigned DestSlot = mNext++; 12080b57cec5SDimitry Andric mMap[V] = DestSlot; 12090b57cec5SDimitry Andric 12100b57cec5SDimitry Andric ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << 12110b57cec5SDimitry Andric DestSlot << " ["); 12120b57cec5SDimitry Andric // G = Global, F = Function, A = Alias, I = IFunc, o = other 12130b57cec5SDimitry Andric ST_DEBUG((isa<GlobalVariable>(V) ? 'G' : 12140b57cec5SDimitry Andric (isa<Function>(V) ? 'F' : 12150b57cec5SDimitry Andric (isa<GlobalAlias>(V) ? 'A' : 12160b57cec5SDimitry Andric (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n"); 12170b57cec5SDimitry Andric } 12180b57cec5SDimitry Andric 12190b57cec5SDimitry Andric /// CreateSlot - Create a new slot for the specified value if it has no name. 12200b57cec5SDimitry Andric void SlotTracker::CreateFunctionSlot(const Value *V) { 12210b57cec5SDimitry Andric assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!"); 12220b57cec5SDimitry Andric 12230b57cec5SDimitry Andric unsigned DestSlot = fNext++; 12240b57cec5SDimitry Andric fMap[V] = DestSlot; 12250b57cec5SDimitry Andric 12260b57cec5SDimitry Andric // G = Global, F = Function, o = other 12270b57cec5SDimitry Andric ST_DEBUG(" Inserting value [" << V->getType() << "] = " << V << " slot=" << 12280b57cec5SDimitry Andric DestSlot << " [o]\n"); 12290b57cec5SDimitry Andric } 12300b57cec5SDimitry Andric 12310b57cec5SDimitry Andric /// CreateModuleSlot - Insert the specified MDNode* into the slot table. 12320b57cec5SDimitry Andric void SlotTracker::CreateMetadataSlot(const MDNode *N) { 12330b57cec5SDimitry Andric assert(N && "Can't insert a null Value into SlotTracker!"); 12340b57cec5SDimitry Andric 1235fe6060f1SDimitry Andric // Don't make slots for DIExpressions or DIArgLists. We just print them inline 1236fe6060f1SDimitry Andric // everywhere. 1237fe6060f1SDimitry Andric if (isa<DIExpression>(N) || isa<DIArgList>(N)) 12380b57cec5SDimitry Andric return; 12390b57cec5SDimitry Andric 12400b57cec5SDimitry Andric unsigned DestSlot = mdnNext; 12410b57cec5SDimitry Andric if (!mdnMap.insert(std::make_pair(N, DestSlot)).second) 12420b57cec5SDimitry Andric return; 12430b57cec5SDimitry Andric ++mdnNext; 12440b57cec5SDimitry Andric 12450b57cec5SDimitry Andric // Recursively add any MDNodes referenced by operands. 12460b57cec5SDimitry Andric for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) 12470b57cec5SDimitry Andric if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i))) 12480b57cec5SDimitry Andric CreateMetadataSlot(Op); 12490b57cec5SDimitry Andric } 12500b57cec5SDimitry Andric 12510b57cec5SDimitry Andric void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) { 12520b57cec5SDimitry Andric assert(AS.hasAttributes() && "Doesn't need a slot!"); 12530b57cec5SDimitry Andric 12540b57cec5SDimitry Andric as_iterator I = asMap.find(AS); 12550b57cec5SDimitry Andric if (I != asMap.end()) 12560b57cec5SDimitry Andric return; 12570b57cec5SDimitry Andric 12580b57cec5SDimitry Andric unsigned DestSlot = asNext++; 12590b57cec5SDimitry Andric asMap[AS] = DestSlot; 12600b57cec5SDimitry Andric } 12610b57cec5SDimitry Andric 12620b57cec5SDimitry Andric /// Create a new slot for the specified Module 12630b57cec5SDimitry Andric void SlotTracker::CreateModulePathSlot(StringRef Path) { 12640b57cec5SDimitry Andric ModulePathMap[Path] = ModulePathNext++; 12650b57cec5SDimitry Andric } 12660b57cec5SDimitry Andric 12670b57cec5SDimitry Andric /// Create a new slot for the specified GUID 12680b57cec5SDimitry Andric void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) { 12690b57cec5SDimitry Andric GUIDMap[GUID] = GUIDNext++; 12700b57cec5SDimitry Andric } 12710b57cec5SDimitry Andric 12720b57cec5SDimitry Andric /// Create a new slot for the specified Id 12730b57cec5SDimitry Andric void SlotTracker::CreateTypeIdSlot(StringRef Id) { 12740b57cec5SDimitry Andric TypeIdMap[Id] = TypeIdNext++; 12750b57cec5SDimitry Andric } 12760b57cec5SDimitry Andric 1277349cc55cSDimitry Andric namespace { 1278349cc55cSDimitry Andric /// Common instances used by most of the printer functions. 1279349cc55cSDimitry Andric struct AsmWriterContext { 1280349cc55cSDimitry Andric TypePrinting *TypePrinter = nullptr; 1281349cc55cSDimitry Andric SlotTracker *Machine = nullptr; 1282349cc55cSDimitry Andric const Module *Context = nullptr; 1283349cc55cSDimitry Andric 1284349cc55cSDimitry Andric AsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M = nullptr) 1285349cc55cSDimitry Andric : TypePrinter(TP), Machine(ST), Context(M) {} 1286349cc55cSDimitry Andric 1287349cc55cSDimitry Andric static AsmWriterContext &getEmpty() { 1288349cc55cSDimitry Andric static AsmWriterContext EmptyCtx(nullptr, nullptr); 1289349cc55cSDimitry Andric return EmptyCtx; 1290349cc55cSDimitry Andric } 1291349cc55cSDimitry Andric 1292349cc55cSDimitry Andric /// A callback that will be triggered when the underlying printer 1293349cc55cSDimitry Andric /// prints a Metadata as operand. 1294349cc55cSDimitry Andric virtual void onWriteMetadataAsOperand(const Metadata *) {} 1295349cc55cSDimitry Andric 1296*81ad6265SDimitry Andric virtual ~AsmWriterContext() = default; 1297349cc55cSDimitry Andric }; 1298349cc55cSDimitry Andric } // end anonymous namespace 1299349cc55cSDimitry Andric 13000b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13010b57cec5SDimitry Andric // AsmWriter Implementation 13020b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 13030b57cec5SDimitry Andric 13040b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, 1305349cc55cSDimitry Andric AsmWriterContext &WriterCtx); 13060b57cec5SDimitry Andric 13070b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, 1308349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 13090b57cec5SDimitry Andric bool FromValue = false); 13100b57cec5SDimitry Andric 13110b57cec5SDimitry Andric static void WriteOptimizationInfo(raw_ostream &Out, const User *U) { 13124824e7fdSDimitry Andric if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) 13134824e7fdSDimitry Andric Out << FPO->getFastMathFlags(); 13140b57cec5SDimitry Andric 13150b57cec5SDimitry Andric if (const OverflowingBinaryOperator *OBO = 13160b57cec5SDimitry Andric dyn_cast<OverflowingBinaryOperator>(U)) { 13170b57cec5SDimitry Andric if (OBO->hasNoUnsignedWrap()) 13180b57cec5SDimitry Andric Out << " nuw"; 13190b57cec5SDimitry Andric if (OBO->hasNoSignedWrap()) 13200b57cec5SDimitry Andric Out << " nsw"; 13210b57cec5SDimitry Andric } else if (const PossiblyExactOperator *Div = 13220b57cec5SDimitry Andric dyn_cast<PossiblyExactOperator>(U)) { 13230b57cec5SDimitry Andric if (Div->isExact()) 13240b57cec5SDimitry Andric Out << " exact"; 13250b57cec5SDimitry Andric } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) { 13260b57cec5SDimitry Andric if (GEP->isInBounds()) 13270b57cec5SDimitry Andric Out << " inbounds"; 13280b57cec5SDimitry Andric } 13290b57cec5SDimitry Andric } 13300b57cec5SDimitry Andric 13310b57cec5SDimitry Andric static void WriteConstantInternal(raw_ostream &Out, const Constant *CV, 1332349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 13330b57cec5SDimitry Andric if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) { 13340b57cec5SDimitry Andric if (CI->getType()->isIntegerTy(1)) { 13350b57cec5SDimitry Andric Out << (CI->getZExtValue() ? "true" : "false"); 13360b57cec5SDimitry Andric return; 13370b57cec5SDimitry Andric } 13380b57cec5SDimitry Andric Out << CI->getValue(); 13390b57cec5SDimitry Andric return; 13400b57cec5SDimitry Andric } 13410b57cec5SDimitry Andric 13420b57cec5SDimitry Andric if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) { 13430b57cec5SDimitry Andric const APFloat &APF = CFP->getValueAPF(); 13440b57cec5SDimitry Andric if (&APF.getSemantics() == &APFloat::IEEEsingle() || 13450b57cec5SDimitry Andric &APF.getSemantics() == &APFloat::IEEEdouble()) { 13460b57cec5SDimitry Andric // We would like to output the FP constant value in exponential notation, 13470b57cec5SDimitry Andric // but we cannot do this if doing so will lose precision. Check here to 13480b57cec5SDimitry Andric // make sure that we only output it in exponential format if we can parse 13490b57cec5SDimitry Andric // the value back and get the same value. 13500b57cec5SDimitry Andric // 13510b57cec5SDimitry Andric bool ignored; 13520b57cec5SDimitry Andric bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble(); 13530b57cec5SDimitry Andric bool isInf = APF.isInfinity(); 13540b57cec5SDimitry Andric bool isNaN = APF.isNaN(); 13550b57cec5SDimitry Andric if (!isInf && !isNaN) { 1356fe6060f1SDimitry Andric double Val = APF.convertToDouble(); 13570b57cec5SDimitry Andric SmallString<128> StrVal; 13580b57cec5SDimitry Andric APF.toString(StrVal, 6, 0, false); 13590b57cec5SDimitry Andric // Check to make sure that the stringized number is not some string like 13600b57cec5SDimitry Andric // "Inf" or NaN, that atof will accept, but the lexer will not. Check 13610b57cec5SDimitry Andric // that the string matches the "[-+]?[0-9]" regex. 13620b57cec5SDimitry Andric // 1363e8d8bef9SDimitry Andric assert((isDigit(StrVal[0]) || ((StrVal[0] == '-' || StrVal[0] == '+') && 1364e8d8bef9SDimitry Andric isDigit(StrVal[1]))) && 13650b57cec5SDimitry Andric "[-+]?[0-9] regex does not match!"); 13660b57cec5SDimitry Andric // Reparse stringized version! 13670b57cec5SDimitry Andric if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) { 13680b57cec5SDimitry Andric Out << StrVal; 13690b57cec5SDimitry Andric return; 13700b57cec5SDimitry Andric } 13710b57cec5SDimitry Andric } 13720b57cec5SDimitry Andric // Otherwise we could not reparse it to exactly the same value, so we must 13730b57cec5SDimitry Andric // output the string in hexadecimal format! Note that loading and storing 13740b57cec5SDimitry Andric // floating point types changes the bits of NaNs on some hosts, notably 13750b57cec5SDimitry Andric // x86, so we must not use these types. 13760b57cec5SDimitry Andric static_assert(sizeof(double) == sizeof(uint64_t), 13770b57cec5SDimitry Andric "assuming that double is 64 bits!"); 13780b57cec5SDimitry Andric APFloat apf = APF; 13790b57cec5SDimitry Andric // Floats are represented in ASCII IR as double, convert. 1380e8d8bef9SDimitry Andric // FIXME: We should allow 32-bit hex float and remove this. 1381e8d8bef9SDimitry Andric if (!isDouble) { 1382e8d8bef9SDimitry Andric // A signaling NaN is quieted on conversion, so we need to recreate the 1383e8d8bef9SDimitry Andric // expected value after convert (quiet bit of the payload is clear). 1384e8d8bef9SDimitry Andric bool IsSNAN = apf.isSignaling(); 13850b57cec5SDimitry Andric apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven, 13860b57cec5SDimitry Andric &ignored); 1387e8d8bef9SDimitry Andric if (IsSNAN) { 1388e8d8bef9SDimitry Andric APInt Payload = apf.bitcastToAPInt(); 1389e8d8bef9SDimitry Andric apf = APFloat::getSNaN(APFloat::IEEEdouble(), apf.isNegative(), 1390e8d8bef9SDimitry Andric &Payload); 1391e8d8bef9SDimitry Andric } 1392e8d8bef9SDimitry Andric } 13930b57cec5SDimitry Andric Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true); 13940b57cec5SDimitry Andric return; 13950b57cec5SDimitry Andric } 13960b57cec5SDimitry Andric 13975ffd83dbSDimitry Andric // Either half, bfloat or some form of long double. 13980b57cec5SDimitry Andric // These appear as a magic letter identifying the type, then a 13990b57cec5SDimitry Andric // fixed number of hex digits. 14000b57cec5SDimitry Andric Out << "0x"; 14010b57cec5SDimitry Andric APInt API = APF.bitcastToAPInt(); 14020b57cec5SDimitry Andric if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) { 14030b57cec5SDimitry Andric Out << 'K'; 14040b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4, 14050b57cec5SDimitry Andric /*Upper=*/true); 14060b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14070b57cec5SDimitry Andric /*Upper=*/true); 14080b57cec5SDimitry Andric return; 14090b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::IEEEquad()) { 14100b57cec5SDimitry Andric Out << 'L'; 14110b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14120b57cec5SDimitry Andric /*Upper=*/true); 14130b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16, 14140b57cec5SDimitry Andric /*Upper=*/true); 14150b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) { 14160b57cec5SDimitry Andric Out << 'M'; 14170b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16, 14180b57cec5SDimitry Andric /*Upper=*/true); 14190b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16, 14200b57cec5SDimitry Andric /*Upper=*/true); 14210b57cec5SDimitry Andric } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) { 14220b57cec5SDimitry Andric Out << 'H'; 14230b57cec5SDimitry Andric Out << format_hex_no_prefix(API.getZExtValue(), 4, 14240b57cec5SDimitry Andric /*Upper=*/true); 14255ffd83dbSDimitry Andric } else if (&APF.getSemantics() == &APFloat::BFloat()) { 14265ffd83dbSDimitry Andric Out << 'R'; 14275ffd83dbSDimitry Andric Out << format_hex_no_prefix(API.getZExtValue(), 4, 14285ffd83dbSDimitry Andric /*Upper=*/true); 14290b57cec5SDimitry Andric } else 14300b57cec5SDimitry Andric llvm_unreachable("Unsupported floating point type"); 14310b57cec5SDimitry Andric return; 14320b57cec5SDimitry Andric } 14330b57cec5SDimitry Andric 14340b57cec5SDimitry Andric if (isa<ConstantAggregateZero>(CV)) { 14350b57cec5SDimitry Andric Out << "zeroinitializer"; 14360b57cec5SDimitry Andric return; 14370b57cec5SDimitry Andric } 14380b57cec5SDimitry Andric 14390b57cec5SDimitry Andric if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) { 14400b57cec5SDimitry Andric Out << "blockaddress("; 1441349cc55cSDimitry Andric WriteAsOperandInternal(Out, BA->getFunction(), WriterCtx); 14420b57cec5SDimitry Andric Out << ", "; 1443349cc55cSDimitry Andric WriteAsOperandInternal(Out, BA->getBasicBlock(), WriterCtx); 14440b57cec5SDimitry Andric Out << ")"; 14450b57cec5SDimitry Andric return; 14460b57cec5SDimitry Andric } 14470b57cec5SDimitry Andric 1448e8d8bef9SDimitry Andric if (const auto *Equiv = dyn_cast<DSOLocalEquivalent>(CV)) { 1449e8d8bef9SDimitry Andric Out << "dso_local_equivalent "; 1450349cc55cSDimitry Andric WriteAsOperandInternal(Out, Equiv->getGlobalValue(), WriterCtx); 1451e8d8bef9SDimitry Andric return; 1452e8d8bef9SDimitry Andric } 1453e8d8bef9SDimitry Andric 14540eae32dcSDimitry Andric if (const auto *NC = dyn_cast<NoCFIValue>(CV)) { 14550eae32dcSDimitry Andric Out << "no_cfi "; 14560eae32dcSDimitry Andric WriteAsOperandInternal(Out, NC->getGlobalValue(), WriterCtx); 14570eae32dcSDimitry Andric return; 14580eae32dcSDimitry Andric } 14590eae32dcSDimitry Andric 14600b57cec5SDimitry Andric if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) { 14610b57cec5SDimitry Andric Type *ETy = CA->getType()->getElementType(); 14620b57cec5SDimitry Andric Out << '['; 1463349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14640b57cec5SDimitry Andric Out << ' '; 1465349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getOperand(0), WriterCtx); 14660b57cec5SDimitry Andric for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) { 14670b57cec5SDimitry Andric Out << ", "; 1468349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14690b57cec5SDimitry Andric Out << ' '; 1470349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getOperand(i), WriterCtx); 14710b57cec5SDimitry Andric } 14720b57cec5SDimitry Andric Out << ']'; 14730b57cec5SDimitry Andric return; 14740b57cec5SDimitry Andric } 14750b57cec5SDimitry Andric 14760b57cec5SDimitry Andric if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) { 14770b57cec5SDimitry Andric // As a special case, print the array as a string if it is an array of 14780b57cec5SDimitry Andric // i8 with ConstantInt values. 14790b57cec5SDimitry Andric if (CA->isString()) { 14800b57cec5SDimitry Andric Out << "c\""; 14810b57cec5SDimitry Andric printEscapedString(CA->getAsString(), Out); 14820b57cec5SDimitry Andric Out << '"'; 14830b57cec5SDimitry Andric return; 14840b57cec5SDimitry Andric } 14850b57cec5SDimitry Andric 14860b57cec5SDimitry Andric Type *ETy = CA->getType()->getElementType(); 14870b57cec5SDimitry Andric Out << '['; 1488349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14890b57cec5SDimitry Andric Out << ' '; 1490349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getElementAsConstant(0), WriterCtx); 14910b57cec5SDimitry Andric for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) { 14920b57cec5SDimitry Andric Out << ", "; 1493349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 14940b57cec5SDimitry Andric Out << ' '; 1495349cc55cSDimitry Andric WriteAsOperandInternal(Out, CA->getElementAsConstant(i), WriterCtx); 14960b57cec5SDimitry Andric } 14970b57cec5SDimitry Andric Out << ']'; 14980b57cec5SDimitry Andric return; 14990b57cec5SDimitry Andric } 15000b57cec5SDimitry Andric 15010b57cec5SDimitry Andric if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) { 15020b57cec5SDimitry Andric if (CS->getType()->isPacked()) 15030b57cec5SDimitry Andric Out << '<'; 15040b57cec5SDimitry Andric Out << '{'; 15050b57cec5SDimitry Andric unsigned N = CS->getNumOperands(); 15060b57cec5SDimitry Andric if (N) { 15070b57cec5SDimitry Andric Out << ' '; 1508349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CS->getOperand(0)->getType(), Out); 15090b57cec5SDimitry Andric Out << ' '; 15100b57cec5SDimitry Andric 1511349cc55cSDimitry Andric WriteAsOperandInternal(Out, CS->getOperand(0), WriterCtx); 15120b57cec5SDimitry Andric 15130b57cec5SDimitry Andric for (unsigned i = 1; i < N; i++) { 15140b57cec5SDimitry Andric Out << ", "; 1515349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CS->getOperand(i)->getType(), Out); 15160b57cec5SDimitry Andric Out << ' '; 15170b57cec5SDimitry Andric 1518349cc55cSDimitry Andric WriteAsOperandInternal(Out, CS->getOperand(i), WriterCtx); 15190b57cec5SDimitry Andric } 15200b57cec5SDimitry Andric Out << ' '; 15210b57cec5SDimitry Andric } 15220b57cec5SDimitry Andric 15230b57cec5SDimitry Andric Out << '}'; 15240b57cec5SDimitry Andric if (CS->getType()->isPacked()) 15250b57cec5SDimitry Andric Out << '>'; 15260b57cec5SDimitry Andric return; 15270b57cec5SDimitry Andric } 15280b57cec5SDimitry Andric 15290b57cec5SDimitry Andric if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) { 1530e8d8bef9SDimitry Andric auto *CVVTy = cast<FixedVectorType>(CV->getType()); 15315ffd83dbSDimitry Andric Type *ETy = CVVTy->getElementType(); 15320b57cec5SDimitry Andric Out << '<'; 1533349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 15340b57cec5SDimitry Andric Out << ' '; 1535349cc55cSDimitry Andric WriteAsOperandInternal(Out, CV->getAggregateElement(0U), WriterCtx); 15365ffd83dbSDimitry Andric for (unsigned i = 1, e = CVVTy->getNumElements(); i != e; ++i) { 15370b57cec5SDimitry Andric Out << ", "; 1538349cc55cSDimitry Andric WriterCtx.TypePrinter->print(ETy, Out); 15390b57cec5SDimitry Andric Out << ' '; 1540349cc55cSDimitry Andric WriteAsOperandInternal(Out, CV->getAggregateElement(i), WriterCtx); 15410b57cec5SDimitry Andric } 15420b57cec5SDimitry Andric Out << '>'; 15430b57cec5SDimitry Andric return; 15440b57cec5SDimitry Andric } 15450b57cec5SDimitry Andric 15460b57cec5SDimitry Andric if (isa<ConstantPointerNull>(CV)) { 15470b57cec5SDimitry Andric Out << "null"; 15480b57cec5SDimitry Andric return; 15490b57cec5SDimitry Andric } 15500b57cec5SDimitry Andric 15510b57cec5SDimitry Andric if (isa<ConstantTokenNone>(CV)) { 15520b57cec5SDimitry Andric Out << "none"; 15530b57cec5SDimitry Andric return; 15540b57cec5SDimitry Andric } 15550b57cec5SDimitry Andric 1556e8d8bef9SDimitry Andric if (isa<PoisonValue>(CV)) { 1557e8d8bef9SDimitry Andric Out << "poison"; 1558e8d8bef9SDimitry Andric return; 1559e8d8bef9SDimitry Andric } 1560e8d8bef9SDimitry Andric 15610b57cec5SDimitry Andric if (isa<UndefValue>(CV)) { 15620b57cec5SDimitry Andric Out << "undef"; 15630b57cec5SDimitry Andric return; 15640b57cec5SDimitry Andric } 15650b57cec5SDimitry Andric 15660b57cec5SDimitry Andric if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) { 15670b57cec5SDimitry Andric Out << CE->getOpcodeName(); 15680b57cec5SDimitry Andric WriteOptimizationInfo(Out, CE); 15690b57cec5SDimitry Andric if (CE->isCompare()) 15700b57cec5SDimitry Andric Out << ' ' << CmpInst::getPredicateName( 15710b57cec5SDimitry Andric static_cast<CmpInst::Predicate>(CE->getPredicate())); 15720b57cec5SDimitry Andric Out << " ("; 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric Optional<unsigned> InRangeOp; 15750b57cec5SDimitry Andric if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) { 1576349cc55cSDimitry Andric WriterCtx.TypePrinter->print(GEP->getSourceElementType(), Out); 15770b57cec5SDimitry Andric Out << ", "; 15780b57cec5SDimitry Andric InRangeOp = GEP->getInRangeIndex(); 15790b57cec5SDimitry Andric if (InRangeOp) 15800b57cec5SDimitry Andric ++*InRangeOp; 15810b57cec5SDimitry Andric } 15820b57cec5SDimitry Andric 15830b57cec5SDimitry Andric for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) { 15840b57cec5SDimitry Andric if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp) 15850b57cec5SDimitry Andric Out << "inrange "; 1586349cc55cSDimitry Andric WriterCtx.TypePrinter->print((*OI)->getType(), Out); 15870b57cec5SDimitry Andric Out << ' '; 1588349cc55cSDimitry Andric WriteAsOperandInternal(Out, *OI, WriterCtx); 15890b57cec5SDimitry Andric if (OI+1 != CE->op_end()) 15900b57cec5SDimitry Andric Out << ", "; 15910b57cec5SDimitry Andric } 15920b57cec5SDimitry Andric 15930eae32dcSDimitry Andric if (CE->hasIndices()) 15940eae32dcSDimitry Andric for (unsigned I : CE->getIndices()) 15950eae32dcSDimitry Andric Out << ", " << I; 15960b57cec5SDimitry Andric 15970b57cec5SDimitry Andric if (CE->isCast()) { 15980b57cec5SDimitry Andric Out << " to "; 1599349cc55cSDimitry Andric WriterCtx.TypePrinter->print(CE->getType(), Out); 16000b57cec5SDimitry Andric } 16010b57cec5SDimitry Andric 16025ffd83dbSDimitry Andric if (CE->getOpcode() == Instruction::ShuffleVector) 16035ffd83dbSDimitry Andric PrintShuffleMask(Out, CE->getType(), CE->getShuffleMask()); 16045ffd83dbSDimitry Andric 16050b57cec5SDimitry Andric Out << ')'; 16060b57cec5SDimitry Andric return; 16070b57cec5SDimitry Andric } 16080b57cec5SDimitry Andric 16090b57cec5SDimitry Andric Out << "<placeholder or erroneous Constant>"; 16100b57cec5SDimitry Andric } 16110b57cec5SDimitry Andric 16120b57cec5SDimitry Andric static void writeMDTuple(raw_ostream &Out, const MDTuple *Node, 1613349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 16140b57cec5SDimitry Andric Out << "!{"; 16150b57cec5SDimitry Andric for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) { 16160b57cec5SDimitry Andric const Metadata *MD = Node->getOperand(mi); 16170b57cec5SDimitry Andric if (!MD) 16180b57cec5SDimitry Andric Out << "null"; 16190b57cec5SDimitry Andric else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) { 16200b57cec5SDimitry Andric Value *V = MDV->getValue(); 1621349cc55cSDimitry Andric WriterCtx.TypePrinter->print(V->getType(), Out); 16220b57cec5SDimitry Andric Out << ' '; 1623349cc55cSDimitry Andric WriteAsOperandInternal(Out, V, WriterCtx); 16240b57cec5SDimitry Andric } else { 1625349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD, WriterCtx); 1626349cc55cSDimitry Andric WriterCtx.onWriteMetadataAsOperand(MD); 16270b57cec5SDimitry Andric } 16280b57cec5SDimitry Andric if (mi + 1 != me) 16290b57cec5SDimitry Andric Out << ", "; 16300b57cec5SDimitry Andric } 16310b57cec5SDimitry Andric 16320b57cec5SDimitry Andric Out << "}"; 16330b57cec5SDimitry Andric } 16340b57cec5SDimitry Andric 16350b57cec5SDimitry Andric namespace { 16360b57cec5SDimitry Andric 16370b57cec5SDimitry Andric struct FieldSeparator { 16380b57cec5SDimitry Andric bool Skip = true; 16390b57cec5SDimitry Andric const char *Sep; 16400b57cec5SDimitry Andric 16410b57cec5SDimitry Andric FieldSeparator(const char *Sep = ", ") : Sep(Sep) {} 16420b57cec5SDimitry Andric }; 16430b57cec5SDimitry Andric 16440b57cec5SDimitry Andric raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) { 16450b57cec5SDimitry Andric if (FS.Skip) { 16460b57cec5SDimitry Andric FS.Skip = false; 16470b57cec5SDimitry Andric return OS; 16480b57cec5SDimitry Andric } 16490b57cec5SDimitry Andric return OS << FS.Sep; 16500b57cec5SDimitry Andric } 16510b57cec5SDimitry Andric 16520b57cec5SDimitry Andric struct MDFieldPrinter { 16530b57cec5SDimitry Andric raw_ostream &Out; 16540b57cec5SDimitry Andric FieldSeparator FS; 1655349cc55cSDimitry Andric AsmWriterContext &WriterCtx; 16560b57cec5SDimitry Andric 1657349cc55cSDimitry Andric explicit MDFieldPrinter(raw_ostream &Out) 1658349cc55cSDimitry Andric : Out(Out), WriterCtx(AsmWriterContext::getEmpty()) {} 1659349cc55cSDimitry Andric MDFieldPrinter(raw_ostream &Out, AsmWriterContext &Ctx) 1660349cc55cSDimitry Andric : Out(Out), WriterCtx(Ctx) {} 16610b57cec5SDimitry Andric 16620b57cec5SDimitry Andric void printTag(const DINode *N); 16630b57cec5SDimitry Andric void printMacinfoType(const DIMacroNode *N); 16640b57cec5SDimitry Andric void printChecksum(const DIFile::ChecksumInfo<StringRef> &N); 16650b57cec5SDimitry Andric void printString(StringRef Name, StringRef Value, 16660b57cec5SDimitry Andric bool ShouldSkipEmpty = true); 16670b57cec5SDimitry Andric void printMetadata(StringRef Name, const Metadata *MD, 16680b57cec5SDimitry Andric bool ShouldSkipNull = true); 16690b57cec5SDimitry Andric template <class IntTy> 16700b57cec5SDimitry Andric void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true); 16715ffd83dbSDimitry Andric void printAPInt(StringRef Name, const APInt &Int, bool IsUnsigned, 16725ffd83dbSDimitry Andric bool ShouldSkipZero); 16730b57cec5SDimitry Andric void printBool(StringRef Name, bool Value, Optional<bool> Default = None); 16740b57cec5SDimitry Andric void printDIFlags(StringRef Name, DINode::DIFlags Flags); 16750b57cec5SDimitry Andric void printDISPFlags(StringRef Name, DISubprogram::DISPFlags Flags); 16760b57cec5SDimitry Andric template <class IntTy, class Stringifier> 16770b57cec5SDimitry Andric void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString, 16780b57cec5SDimitry Andric bool ShouldSkipZero = true); 16790b57cec5SDimitry Andric void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK); 16800b57cec5SDimitry Andric void printNameTableKind(StringRef Name, 16810b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind NTK); 16820b57cec5SDimitry Andric }; 16830b57cec5SDimitry Andric 16840b57cec5SDimitry Andric } // end anonymous namespace 16850b57cec5SDimitry Andric 16860b57cec5SDimitry Andric void MDFieldPrinter::printTag(const DINode *N) { 16870b57cec5SDimitry Andric Out << FS << "tag: "; 16880b57cec5SDimitry Andric auto Tag = dwarf::TagString(N->getTag()); 16890b57cec5SDimitry Andric if (!Tag.empty()) 16900b57cec5SDimitry Andric Out << Tag; 16910b57cec5SDimitry Andric else 16920b57cec5SDimitry Andric Out << N->getTag(); 16930b57cec5SDimitry Andric } 16940b57cec5SDimitry Andric 16950b57cec5SDimitry Andric void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) { 16960b57cec5SDimitry Andric Out << FS << "type: "; 16970b57cec5SDimitry Andric auto Type = dwarf::MacinfoString(N->getMacinfoType()); 16980b57cec5SDimitry Andric if (!Type.empty()) 16990b57cec5SDimitry Andric Out << Type; 17000b57cec5SDimitry Andric else 17010b57cec5SDimitry Andric Out << N->getMacinfoType(); 17020b57cec5SDimitry Andric } 17030b57cec5SDimitry Andric 17040b57cec5SDimitry Andric void MDFieldPrinter::printChecksum( 17050b57cec5SDimitry Andric const DIFile::ChecksumInfo<StringRef> &Checksum) { 17060b57cec5SDimitry Andric Out << FS << "checksumkind: " << Checksum.getKindAsString(); 17070b57cec5SDimitry Andric printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false); 17080b57cec5SDimitry Andric } 17090b57cec5SDimitry Andric 17100b57cec5SDimitry Andric void MDFieldPrinter::printString(StringRef Name, StringRef Value, 17110b57cec5SDimitry Andric bool ShouldSkipEmpty) { 17120b57cec5SDimitry Andric if (ShouldSkipEmpty && Value.empty()) 17130b57cec5SDimitry Andric return; 17140b57cec5SDimitry Andric 17150b57cec5SDimitry Andric Out << FS << Name << ": \""; 17160b57cec5SDimitry Andric printEscapedString(Value, Out); 17170b57cec5SDimitry Andric Out << "\""; 17180b57cec5SDimitry Andric } 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD, 1721349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 17220b57cec5SDimitry Andric if (!MD) { 17230b57cec5SDimitry Andric Out << "null"; 17240b57cec5SDimitry Andric return; 17250b57cec5SDimitry Andric } 1726349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD, WriterCtx); 1727349cc55cSDimitry Andric WriterCtx.onWriteMetadataAsOperand(MD); 17280b57cec5SDimitry Andric } 17290b57cec5SDimitry Andric 17300b57cec5SDimitry Andric void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD, 17310b57cec5SDimitry Andric bool ShouldSkipNull) { 17320b57cec5SDimitry Andric if (ShouldSkipNull && !MD) 17330b57cec5SDimitry Andric return; 17340b57cec5SDimitry Andric 17350b57cec5SDimitry Andric Out << FS << Name << ": "; 1736349cc55cSDimitry Andric writeMetadataAsOperand(Out, MD, WriterCtx); 17370b57cec5SDimitry Andric } 17380b57cec5SDimitry Andric 17390b57cec5SDimitry Andric template <class IntTy> 17400b57cec5SDimitry Andric void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) { 17410b57cec5SDimitry Andric if (ShouldSkipZero && !Int) 17420b57cec5SDimitry Andric return; 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric Out << FS << Name << ": " << Int; 17450b57cec5SDimitry Andric } 17460b57cec5SDimitry Andric 17475ffd83dbSDimitry Andric void MDFieldPrinter::printAPInt(StringRef Name, const APInt &Int, 17485ffd83dbSDimitry Andric bool IsUnsigned, bool ShouldSkipZero) { 1749349cc55cSDimitry Andric if (ShouldSkipZero && Int.isZero()) 17505ffd83dbSDimitry Andric return; 17515ffd83dbSDimitry Andric 17525ffd83dbSDimitry Andric Out << FS << Name << ": "; 17535ffd83dbSDimitry Andric Int.print(Out, !IsUnsigned); 17545ffd83dbSDimitry Andric } 17555ffd83dbSDimitry Andric 17560b57cec5SDimitry Andric void MDFieldPrinter::printBool(StringRef Name, bool Value, 17570b57cec5SDimitry Andric Optional<bool> Default) { 17580b57cec5SDimitry Andric if (Default && Value == *Default) 17590b57cec5SDimitry Andric return; 17600b57cec5SDimitry Andric Out << FS << Name << ": " << (Value ? "true" : "false"); 17610b57cec5SDimitry Andric } 17620b57cec5SDimitry Andric 17630b57cec5SDimitry Andric void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) { 17640b57cec5SDimitry Andric if (!Flags) 17650b57cec5SDimitry Andric return; 17660b57cec5SDimitry Andric 17670b57cec5SDimitry Andric Out << FS << Name << ": "; 17680b57cec5SDimitry Andric 17690b57cec5SDimitry Andric SmallVector<DINode::DIFlags, 8> SplitFlags; 17700b57cec5SDimitry Andric auto Extra = DINode::splitFlags(Flags, SplitFlags); 17710b57cec5SDimitry Andric 17720b57cec5SDimitry Andric FieldSeparator FlagsFS(" | "); 17730b57cec5SDimitry Andric for (auto F : SplitFlags) { 17740b57cec5SDimitry Andric auto StringF = DINode::getFlagString(F); 17750b57cec5SDimitry Andric assert(!StringF.empty() && "Expected valid flag"); 17760b57cec5SDimitry Andric Out << FlagsFS << StringF; 17770b57cec5SDimitry Andric } 17780b57cec5SDimitry Andric if (Extra || SplitFlags.empty()) 17790b57cec5SDimitry Andric Out << FlagsFS << Extra; 17800b57cec5SDimitry Andric } 17810b57cec5SDimitry Andric 17820b57cec5SDimitry Andric void MDFieldPrinter::printDISPFlags(StringRef Name, 17830b57cec5SDimitry Andric DISubprogram::DISPFlags Flags) { 17840b57cec5SDimitry Andric // Always print this field, because no flags in the IR at all will be 17850b57cec5SDimitry Andric // interpreted as old-style isDefinition: true. 17860b57cec5SDimitry Andric Out << FS << Name << ": "; 17870b57cec5SDimitry Andric 17880b57cec5SDimitry Andric if (!Flags) { 17890b57cec5SDimitry Andric Out << 0; 17900b57cec5SDimitry Andric return; 17910b57cec5SDimitry Andric } 17920b57cec5SDimitry Andric 17930b57cec5SDimitry Andric SmallVector<DISubprogram::DISPFlags, 8> SplitFlags; 17940b57cec5SDimitry Andric auto Extra = DISubprogram::splitFlags(Flags, SplitFlags); 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric FieldSeparator FlagsFS(" | "); 17970b57cec5SDimitry Andric for (auto F : SplitFlags) { 17980b57cec5SDimitry Andric auto StringF = DISubprogram::getFlagString(F); 17990b57cec5SDimitry Andric assert(!StringF.empty() && "Expected valid flag"); 18000b57cec5SDimitry Andric Out << FlagsFS << StringF; 18010b57cec5SDimitry Andric } 18020b57cec5SDimitry Andric if (Extra || SplitFlags.empty()) 18030b57cec5SDimitry Andric Out << FlagsFS << Extra; 18040b57cec5SDimitry Andric } 18050b57cec5SDimitry Andric 18060b57cec5SDimitry Andric void MDFieldPrinter::printEmissionKind(StringRef Name, 18070b57cec5SDimitry Andric DICompileUnit::DebugEmissionKind EK) { 18080b57cec5SDimitry Andric Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK); 18090b57cec5SDimitry Andric } 18100b57cec5SDimitry Andric 18110b57cec5SDimitry Andric void MDFieldPrinter::printNameTableKind(StringRef Name, 18120b57cec5SDimitry Andric DICompileUnit::DebugNameTableKind NTK) { 18130b57cec5SDimitry Andric if (NTK == DICompileUnit::DebugNameTableKind::Default) 18140b57cec5SDimitry Andric return; 18150b57cec5SDimitry Andric Out << FS << Name << ": " << DICompileUnit::nameTableKindString(NTK); 18160b57cec5SDimitry Andric } 18170b57cec5SDimitry Andric 18180b57cec5SDimitry Andric template <class IntTy, class Stringifier> 18190b57cec5SDimitry Andric void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value, 18200b57cec5SDimitry Andric Stringifier toString, bool ShouldSkipZero) { 18210b57cec5SDimitry Andric if (!Value) 18220b57cec5SDimitry Andric return; 18230b57cec5SDimitry Andric 18240b57cec5SDimitry Andric Out << FS << Name << ": "; 18250b57cec5SDimitry Andric auto S = toString(Value); 18260b57cec5SDimitry Andric if (!S.empty()) 18270b57cec5SDimitry Andric Out << S; 18280b57cec5SDimitry Andric else 18290b57cec5SDimitry Andric Out << Value; 18300b57cec5SDimitry Andric } 18310b57cec5SDimitry Andric 18320b57cec5SDimitry Andric static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N, 1833349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18340b57cec5SDimitry Andric Out << "!GenericDINode("; 1835349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 18360b57cec5SDimitry Andric Printer.printTag(N); 18370b57cec5SDimitry Andric Printer.printString("header", N->getHeader()); 18380b57cec5SDimitry Andric if (N->getNumDwarfOperands()) { 18390b57cec5SDimitry Andric Out << Printer.FS << "operands: {"; 18400b57cec5SDimitry Andric FieldSeparator IFS; 18410b57cec5SDimitry Andric for (auto &I : N->dwarf_operands()) { 18420b57cec5SDimitry Andric Out << IFS; 1843349cc55cSDimitry Andric writeMetadataAsOperand(Out, I, WriterCtx); 18440b57cec5SDimitry Andric } 18450b57cec5SDimitry Andric Out << "}"; 18460b57cec5SDimitry Andric } 18470b57cec5SDimitry Andric Out << ")"; 18480b57cec5SDimitry Andric } 18490b57cec5SDimitry Andric 18500b57cec5SDimitry Andric static void writeDILocation(raw_ostream &Out, const DILocation *DL, 1851349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18520b57cec5SDimitry Andric Out << "!DILocation("; 1853349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 18540b57cec5SDimitry Andric // Always output the line, since 0 is a relevant and important value for it. 18550b57cec5SDimitry Andric Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false); 18560b57cec5SDimitry Andric Printer.printInt("column", DL->getColumn()); 18570b57cec5SDimitry Andric Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false); 18580b57cec5SDimitry Andric Printer.printMetadata("inlinedAt", DL->getRawInlinedAt()); 18590b57cec5SDimitry Andric Printer.printBool("isImplicitCode", DL->isImplicitCode(), 18600b57cec5SDimitry Andric /* Default */ false); 18610b57cec5SDimitry Andric Out << ")"; 18620b57cec5SDimitry Andric } 18630b57cec5SDimitry Andric 18640b57cec5SDimitry Andric static void writeDISubrange(raw_ostream &Out, const DISubrange *N, 1865349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 18660b57cec5SDimitry Andric Out << "!DISubrange("; 1867349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1868fe6060f1SDimitry Andric 1869fe6060f1SDimitry Andric auto *Count = N->getRawCountNode(); 1870fe6060f1SDimitry Andric if (auto *CE = dyn_cast_or_null<ConstantAsMetadata>(Count)) { 1871fe6060f1SDimitry Andric auto *CV = cast<ConstantInt>(CE->getValue()); 1872fe6060f1SDimitry Andric Printer.printInt("count", CV->getSExtValue(), 1873fe6060f1SDimitry Andric /* ShouldSkipZero */ false); 1874fe6060f1SDimitry Andric } else 1875fe6060f1SDimitry Andric Printer.printMetadata("count", Count, /*ShouldSkipNull */ true); 18765ffd83dbSDimitry Andric 18775ffd83dbSDimitry Andric // A lowerBound of constant 0 should not be skipped, since it is different 18785ffd83dbSDimitry Andric // from an unspecified lower bound (= nullptr). 18795ffd83dbSDimitry Andric auto *LBound = N->getRawLowerBound(); 18805ffd83dbSDimitry Andric if (auto *LE = dyn_cast_or_null<ConstantAsMetadata>(LBound)) { 18815ffd83dbSDimitry Andric auto *LV = cast<ConstantInt>(LE->getValue()); 18825ffd83dbSDimitry Andric Printer.printInt("lowerBound", LV->getSExtValue(), 18835ffd83dbSDimitry Andric /* ShouldSkipZero */ false); 18845ffd83dbSDimitry Andric } else 18855ffd83dbSDimitry Andric Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true); 18865ffd83dbSDimitry Andric 18875ffd83dbSDimitry Andric auto *UBound = N->getRawUpperBound(); 18885ffd83dbSDimitry Andric if (auto *UE = dyn_cast_or_null<ConstantAsMetadata>(UBound)) { 18895ffd83dbSDimitry Andric auto *UV = cast<ConstantInt>(UE->getValue()); 18905ffd83dbSDimitry Andric Printer.printInt("upperBound", UV->getSExtValue(), 18915ffd83dbSDimitry Andric /* ShouldSkipZero */ false); 18925ffd83dbSDimitry Andric } else 18935ffd83dbSDimitry Andric Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true); 18945ffd83dbSDimitry Andric 18955ffd83dbSDimitry Andric auto *Stride = N->getRawStride(); 18965ffd83dbSDimitry Andric if (auto *SE = dyn_cast_or_null<ConstantAsMetadata>(Stride)) { 18975ffd83dbSDimitry Andric auto *SV = cast<ConstantInt>(SE->getValue()); 18985ffd83dbSDimitry Andric Printer.printInt("stride", SV->getSExtValue(), /* ShouldSkipZero */ false); 18995ffd83dbSDimitry Andric } else 19005ffd83dbSDimitry Andric Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true); 19015ffd83dbSDimitry Andric 19020b57cec5SDimitry Andric Out << ")"; 19030b57cec5SDimitry Andric } 19040b57cec5SDimitry Andric 1905e8d8bef9SDimitry Andric static void writeDIGenericSubrange(raw_ostream &Out, const DIGenericSubrange *N, 1906349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 1907e8d8bef9SDimitry Andric Out << "!DIGenericSubrange("; 1908349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1909e8d8bef9SDimitry Andric 1910e8d8bef9SDimitry Andric auto IsConstant = [&](Metadata *Bound) -> bool { 1911e8d8bef9SDimitry Andric if (auto *BE = dyn_cast_or_null<DIExpression>(Bound)) { 1912349cc55cSDimitry Andric return BE->isConstant() && 1913349cc55cSDimitry Andric DIExpression::SignedOrUnsignedConstant::SignedConstant == 1914349cc55cSDimitry Andric *BE->isConstant(); 1915e8d8bef9SDimitry Andric } 1916e8d8bef9SDimitry Andric return false; 1917e8d8bef9SDimitry Andric }; 1918e8d8bef9SDimitry Andric 1919e8d8bef9SDimitry Andric auto GetConstant = [&](Metadata *Bound) -> int64_t { 1920e8d8bef9SDimitry Andric assert(IsConstant(Bound) && "Expected constant"); 1921e8d8bef9SDimitry Andric auto *BE = dyn_cast_or_null<DIExpression>(Bound); 1922e8d8bef9SDimitry Andric return static_cast<int64_t>(BE->getElement(1)); 1923e8d8bef9SDimitry Andric }; 1924e8d8bef9SDimitry Andric 1925e8d8bef9SDimitry Andric auto *Count = N->getRawCountNode(); 1926e8d8bef9SDimitry Andric if (IsConstant(Count)) 1927e8d8bef9SDimitry Andric Printer.printInt("count", GetConstant(Count), 1928e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1929e8d8bef9SDimitry Andric else 1930e8d8bef9SDimitry Andric Printer.printMetadata("count", Count, /*ShouldSkipNull */ true); 1931e8d8bef9SDimitry Andric 1932e8d8bef9SDimitry Andric auto *LBound = N->getRawLowerBound(); 1933e8d8bef9SDimitry Andric if (IsConstant(LBound)) 1934e8d8bef9SDimitry Andric Printer.printInt("lowerBound", GetConstant(LBound), 1935e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1936e8d8bef9SDimitry Andric else 1937e8d8bef9SDimitry Andric Printer.printMetadata("lowerBound", LBound, /*ShouldSkipNull */ true); 1938e8d8bef9SDimitry Andric 1939e8d8bef9SDimitry Andric auto *UBound = N->getRawUpperBound(); 1940e8d8bef9SDimitry Andric if (IsConstant(UBound)) 1941e8d8bef9SDimitry Andric Printer.printInt("upperBound", GetConstant(UBound), 1942e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1943e8d8bef9SDimitry Andric else 1944e8d8bef9SDimitry Andric Printer.printMetadata("upperBound", UBound, /*ShouldSkipNull */ true); 1945e8d8bef9SDimitry Andric 1946e8d8bef9SDimitry Andric auto *Stride = N->getRawStride(); 1947e8d8bef9SDimitry Andric if (IsConstant(Stride)) 1948e8d8bef9SDimitry Andric Printer.printInt("stride", GetConstant(Stride), 1949e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 1950e8d8bef9SDimitry Andric else 1951e8d8bef9SDimitry Andric Printer.printMetadata("stride", Stride, /*ShouldSkipNull */ true); 1952e8d8bef9SDimitry Andric 1953e8d8bef9SDimitry Andric Out << ")"; 1954e8d8bef9SDimitry Andric } 1955e8d8bef9SDimitry Andric 19560b57cec5SDimitry Andric static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N, 1957349cc55cSDimitry Andric AsmWriterContext &) { 19580b57cec5SDimitry Andric Out << "!DIEnumerator("; 19590b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 19600b57cec5SDimitry Andric Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false); 19615ffd83dbSDimitry Andric Printer.printAPInt("value", N->getValue(), N->isUnsigned(), 19625ffd83dbSDimitry Andric /*ShouldSkipZero=*/false); 19635ffd83dbSDimitry Andric if (N->isUnsigned()) 19640b57cec5SDimitry Andric Printer.printBool("isUnsigned", true); 19650b57cec5SDimitry Andric Out << ")"; 19660b57cec5SDimitry Andric } 19670b57cec5SDimitry Andric 19680b57cec5SDimitry Andric static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N, 1969349cc55cSDimitry Andric AsmWriterContext &) { 19700b57cec5SDimitry Andric Out << "!DIBasicType("; 19710b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 19720b57cec5SDimitry Andric if (N->getTag() != dwarf::DW_TAG_base_type) 19730b57cec5SDimitry Andric Printer.printTag(N); 19740b57cec5SDimitry Andric Printer.printString("name", N->getName()); 19750b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 19760b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 19770b57cec5SDimitry Andric Printer.printDwarfEnum("encoding", N->getEncoding(), 19780b57cec5SDimitry Andric dwarf::AttributeEncodingString); 19790b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 19800b57cec5SDimitry Andric Out << ")"; 19810b57cec5SDimitry Andric } 19820b57cec5SDimitry Andric 1983e8d8bef9SDimitry Andric static void writeDIStringType(raw_ostream &Out, const DIStringType *N, 1984349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 1985e8d8bef9SDimitry Andric Out << "!DIStringType("; 1986349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 1987e8d8bef9SDimitry Andric if (N->getTag() != dwarf::DW_TAG_string_type) 1988e8d8bef9SDimitry Andric Printer.printTag(N); 1989e8d8bef9SDimitry Andric Printer.printString("name", N->getName()); 1990e8d8bef9SDimitry Andric Printer.printMetadata("stringLength", N->getRawStringLength()); 1991e8d8bef9SDimitry Andric Printer.printMetadata("stringLengthExpression", N->getRawStringLengthExp()); 199204eeddc0SDimitry Andric Printer.printMetadata("stringLocationExpression", 199304eeddc0SDimitry Andric N->getRawStringLocationExp()); 1994e8d8bef9SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 1995e8d8bef9SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 1996e8d8bef9SDimitry Andric Printer.printDwarfEnum("encoding", N->getEncoding(), 1997e8d8bef9SDimitry Andric dwarf::AttributeEncodingString); 1998e8d8bef9SDimitry Andric Out << ")"; 1999e8d8bef9SDimitry Andric } 2000e8d8bef9SDimitry Andric 20010b57cec5SDimitry Andric static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N, 2002349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20030b57cec5SDimitry Andric Out << "!DIDerivedType("; 2004349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20050b57cec5SDimitry Andric Printer.printTag(N); 20060b57cec5SDimitry Andric Printer.printString("name", N->getName()); 20070b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope()); 20080b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 20090b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 20100b57cec5SDimitry Andric Printer.printMetadata("baseType", N->getRawBaseType(), 20110b57cec5SDimitry Andric /* ShouldSkipNull */ false); 20120b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 20130b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 20140b57cec5SDimitry Andric Printer.printInt("offset", N->getOffsetInBits()); 20150b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20160b57cec5SDimitry Andric Printer.printMetadata("extraData", N->getRawExtraData()); 20170b57cec5SDimitry Andric if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace()) 20180b57cec5SDimitry Andric Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace, 20190b57cec5SDimitry Andric /* ShouldSkipZero */ false); 2020349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 20210b57cec5SDimitry Andric Out << ")"; 20220b57cec5SDimitry Andric } 20230b57cec5SDimitry Andric 20240b57cec5SDimitry Andric static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N, 2025349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20260b57cec5SDimitry Andric Out << "!DICompositeType("; 2027349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20280b57cec5SDimitry Andric Printer.printTag(N); 20290b57cec5SDimitry Andric Printer.printString("name", N->getName()); 20300b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope()); 20310b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 20320b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 20330b57cec5SDimitry Andric Printer.printMetadata("baseType", N->getRawBaseType()); 20340b57cec5SDimitry Andric Printer.printInt("size", N->getSizeInBits()); 20350b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 20360b57cec5SDimitry Andric Printer.printInt("offset", N->getOffsetInBits()); 20370b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20380b57cec5SDimitry Andric Printer.printMetadata("elements", N->getRawElements()); 20390b57cec5SDimitry Andric Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(), 20400b57cec5SDimitry Andric dwarf::LanguageString); 20410b57cec5SDimitry Andric Printer.printMetadata("vtableHolder", N->getRawVTableHolder()); 20420b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 20430b57cec5SDimitry Andric Printer.printString("identifier", N->getIdentifier()); 20440b57cec5SDimitry Andric Printer.printMetadata("discriminator", N->getRawDiscriminator()); 20455ffd83dbSDimitry Andric Printer.printMetadata("dataLocation", N->getRawDataLocation()); 2046e8d8bef9SDimitry Andric Printer.printMetadata("associated", N->getRawAssociated()); 2047e8d8bef9SDimitry Andric Printer.printMetadata("allocated", N->getRawAllocated()); 2048e8d8bef9SDimitry Andric if (auto *RankConst = N->getRankConst()) 2049e8d8bef9SDimitry Andric Printer.printInt("rank", RankConst->getSExtValue(), 2050e8d8bef9SDimitry Andric /* ShouldSkipZero */ false); 2051e8d8bef9SDimitry Andric else 2052e8d8bef9SDimitry Andric Printer.printMetadata("rank", N->getRawRank(), /*ShouldSkipNull */ true); 2053349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 20540b57cec5SDimitry Andric Out << ")"; 20550b57cec5SDimitry Andric } 20560b57cec5SDimitry Andric 20570b57cec5SDimitry Andric static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N, 2058349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20590b57cec5SDimitry Andric Out << "!DISubroutineType("; 2060349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20610b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 20620b57cec5SDimitry Andric Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString); 20630b57cec5SDimitry Andric Printer.printMetadata("types", N->getRawTypeArray(), 20640b57cec5SDimitry Andric /* ShouldSkipNull */ false); 20650b57cec5SDimitry Andric Out << ")"; 20660b57cec5SDimitry Andric } 20670b57cec5SDimitry Andric 2068349cc55cSDimitry Andric static void writeDIFile(raw_ostream &Out, const DIFile *N, AsmWriterContext &) { 20690b57cec5SDimitry Andric Out << "!DIFile("; 20700b57cec5SDimitry Andric MDFieldPrinter Printer(Out); 20710b57cec5SDimitry Andric Printer.printString("filename", N->getFilename(), 20720b57cec5SDimitry Andric /* ShouldSkipEmpty */ false); 20730b57cec5SDimitry Andric Printer.printString("directory", N->getDirectory(), 20740b57cec5SDimitry Andric /* ShouldSkipEmpty */ false); 20750b57cec5SDimitry Andric // Print all values for checksum together, or not at all. 20760b57cec5SDimitry Andric if (N->getChecksum()) 20770b57cec5SDimitry Andric Printer.printChecksum(*N->getChecksum()); 2078*81ad6265SDimitry Andric Printer.printString("source", N->getSource().value_or(StringRef()), 20790b57cec5SDimitry Andric /* ShouldSkipEmpty */ true); 20800b57cec5SDimitry Andric Out << ")"; 20810b57cec5SDimitry Andric } 20820b57cec5SDimitry Andric 20830b57cec5SDimitry Andric static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N, 2084349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 20850b57cec5SDimitry Andric Out << "!DICompileUnit("; 2086349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 20870b57cec5SDimitry Andric Printer.printDwarfEnum("language", N->getSourceLanguage(), 20880b57cec5SDimitry Andric dwarf::LanguageString, /* ShouldSkipZero */ false); 20890b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false); 20900b57cec5SDimitry Andric Printer.printString("producer", N->getProducer()); 20910b57cec5SDimitry Andric Printer.printBool("isOptimized", N->isOptimized()); 20920b57cec5SDimitry Andric Printer.printString("flags", N->getFlags()); 20930b57cec5SDimitry Andric Printer.printInt("runtimeVersion", N->getRuntimeVersion(), 20940b57cec5SDimitry Andric /* ShouldSkipZero */ false); 20950b57cec5SDimitry Andric Printer.printString("splitDebugFilename", N->getSplitDebugFilename()); 20960b57cec5SDimitry Andric Printer.printEmissionKind("emissionKind", N->getEmissionKind()); 20970b57cec5SDimitry Andric Printer.printMetadata("enums", N->getRawEnumTypes()); 20980b57cec5SDimitry Andric Printer.printMetadata("retainedTypes", N->getRawRetainedTypes()); 20990b57cec5SDimitry Andric Printer.printMetadata("globals", N->getRawGlobalVariables()); 21000b57cec5SDimitry Andric Printer.printMetadata("imports", N->getRawImportedEntities()); 21010b57cec5SDimitry Andric Printer.printMetadata("macros", N->getRawMacros()); 21020b57cec5SDimitry Andric Printer.printInt("dwoId", N->getDWOId()); 21030b57cec5SDimitry Andric Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true); 21040b57cec5SDimitry Andric Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(), 21050b57cec5SDimitry Andric false); 21060b57cec5SDimitry Andric Printer.printNameTableKind("nameTableKind", N->getNameTableKind()); 21070b57cec5SDimitry Andric Printer.printBool("rangesBaseAddress", N->getRangesBaseAddress(), false); 21085ffd83dbSDimitry Andric Printer.printString("sysroot", N->getSysRoot()); 21095ffd83dbSDimitry Andric Printer.printString("sdk", N->getSDK()); 21100b57cec5SDimitry Andric Out << ")"; 21110b57cec5SDimitry Andric } 21120b57cec5SDimitry Andric 21130b57cec5SDimitry Andric static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N, 2114349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21150b57cec5SDimitry Andric Out << "!DISubprogram("; 2116349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21170b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21180b57cec5SDimitry Andric Printer.printString("linkageName", N->getLinkageName()); 21190b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21200b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21210b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21220b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 21230b57cec5SDimitry Andric Printer.printInt("scopeLine", N->getScopeLine()); 21240b57cec5SDimitry Andric Printer.printMetadata("containingType", N->getRawContainingType()); 21250b57cec5SDimitry Andric if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none || 21260b57cec5SDimitry Andric N->getVirtualIndex() != 0) 21270b57cec5SDimitry Andric Printer.printInt("virtualIndex", N->getVirtualIndex(), false); 21280b57cec5SDimitry Andric Printer.printInt("thisAdjustment", N->getThisAdjustment()); 21290b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 21300b57cec5SDimitry Andric Printer.printDISPFlags("spFlags", N->getSPFlags()); 21310b57cec5SDimitry Andric Printer.printMetadata("unit", N->getRawUnit()); 21320b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 21330b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawDeclaration()); 21340b57cec5SDimitry Andric Printer.printMetadata("retainedNodes", N->getRawRetainedNodes()); 21350b57cec5SDimitry Andric Printer.printMetadata("thrownTypes", N->getRawThrownTypes()); 2136349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 2137*81ad6265SDimitry Andric Printer.printString("targetFuncName", N->getTargetFuncName()); 21380b57cec5SDimitry Andric Out << ")"; 21390b57cec5SDimitry Andric } 21400b57cec5SDimitry Andric 21410b57cec5SDimitry Andric static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N, 2142349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21430b57cec5SDimitry Andric Out << "!DILexicalBlock("; 2144349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21450b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21460b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21470b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21480b57cec5SDimitry Andric Printer.printInt("column", N->getColumn()); 21490b57cec5SDimitry Andric Out << ")"; 21500b57cec5SDimitry Andric } 21510b57cec5SDimitry Andric 21520b57cec5SDimitry Andric static void writeDILexicalBlockFile(raw_ostream &Out, 21530b57cec5SDimitry Andric const DILexicalBlockFile *N, 2154349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21550b57cec5SDimitry Andric Out << "!DILexicalBlockFile("; 2156349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21570b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21580b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21590b57cec5SDimitry Andric Printer.printInt("discriminator", N->getDiscriminator(), 21600b57cec5SDimitry Andric /* ShouldSkipZero */ false); 21610b57cec5SDimitry Andric Out << ")"; 21620b57cec5SDimitry Andric } 21630b57cec5SDimitry Andric 21640b57cec5SDimitry Andric static void writeDINamespace(raw_ostream &Out, const DINamespace *N, 2165349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21660b57cec5SDimitry Andric Out << "!DINamespace("; 2167349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21680b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21690b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 21700b57cec5SDimitry Andric Printer.printBool("exportSymbols", N->getExportSymbols(), false); 21710b57cec5SDimitry Andric Out << ")"; 21720b57cec5SDimitry Andric } 21730b57cec5SDimitry Andric 21740b57cec5SDimitry Andric static void writeDICommonBlock(raw_ostream &Out, const DICommonBlock *N, 2175349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21760b57cec5SDimitry Andric Out << "!DICommonBlock("; 2177349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21780b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), false); 21790b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawDecl(), false); 21800b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21810b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 21820b57cec5SDimitry Andric Printer.printInt("line", N->getLineNo()); 21830b57cec5SDimitry Andric Out << ")"; 21840b57cec5SDimitry Andric } 21850b57cec5SDimitry Andric 21860b57cec5SDimitry Andric static void writeDIMacro(raw_ostream &Out, const DIMacro *N, 2187349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21880b57cec5SDimitry Andric Out << "!DIMacro("; 2189349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 21900b57cec5SDimitry Andric Printer.printMacinfoType(N); 21910b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 21920b57cec5SDimitry Andric Printer.printString("name", N->getName()); 21930b57cec5SDimitry Andric Printer.printString("value", N->getValue()); 21940b57cec5SDimitry Andric Out << ")"; 21950b57cec5SDimitry Andric } 21960b57cec5SDimitry Andric 21970b57cec5SDimitry Andric static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N, 2198349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 21990b57cec5SDimitry Andric Out << "!DIMacroFile("; 2200349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22010b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22020b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false); 22030b57cec5SDimitry Andric Printer.printMetadata("nodes", N->getRawElements()); 22040b57cec5SDimitry Andric Out << ")"; 22050b57cec5SDimitry Andric } 22060b57cec5SDimitry Andric 22070b57cec5SDimitry Andric static void writeDIModule(raw_ostream &Out, const DIModule *N, 2208349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22090b57cec5SDimitry Andric Out << "!DIModule("; 2210349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22110b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22120b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22130b57cec5SDimitry Andric Printer.printString("configMacros", N->getConfigurationMacros()); 22140b57cec5SDimitry Andric Printer.printString("includePath", N->getIncludePath()); 22155ffd83dbSDimitry Andric Printer.printString("apinotes", N->getAPINotesFile()); 22165ffd83dbSDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22175ffd83dbSDimitry Andric Printer.printInt("line", N->getLineNo()); 2218e8d8bef9SDimitry Andric Printer.printBool("isDecl", N->getIsDecl(), /* Default */ false); 22190b57cec5SDimitry Andric Out << ")"; 22200b57cec5SDimitry Andric } 22210b57cec5SDimitry Andric 22220b57cec5SDimitry Andric static void writeDITemplateTypeParameter(raw_ostream &Out, 22230b57cec5SDimitry Andric const DITemplateTypeParameter *N, 2224349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22250b57cec5SDimitry Andric Out << "!DITemplateTypeParameter("; 2226349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22270b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22280b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false); 22295ffd83dbSDimitry Andric Printer.printBool("defaulted", N->isDefault(), /* Default= */ false); 22300b57cec5SDimitry Andric Out << ")"; 22310b57cec5SDimitry Andric } 22320b57cec5SDimitry Andric 22330b57cec5SDimitry Andric static void writeDITemplateValueParameter(raw_ostream &Out, 22340b57cec5SDimitry Andric const DITemplateValueParameter *N, 2235349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22360b57cec5SDimitry Andric Out << "!DITemplateValueParameter("; 2237349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22380b57cec5SDimitry Andric if (N->getTag() != dwarf::DW_TAG_template_value_parameter) 22390b57cec5SDimitry Andric Printer.printTag(N); 22400b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22410b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22425ffd83dbSDimitry Andric Printer.printBool("defaulted", N->isDefault(), /* Default= */ false); 22430b57cec5SDimitry Andric Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false); 22440b57cec5SDimitry Andric Out << ")"; 22450b57cec5SDimitry Andric } 22460b57cec5SDimitry Andric 22470b57cec5SDimitry Andric static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N, 2248349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22490b57cec5SDimitry Andric Out << "!DIGlobalVariable("; 2250349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22510b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22520b57cec5SDimitry Andric Printer.printString("linkageName", N->getLinkageName()); 22530b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22540b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22550b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22560b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22570b57cec5SDimitry Andric Printer.printBool("isLocal", N->isLocalToUnit()); 22580b57cec5SDimitry Andric Printer.printBool("isDefinition", N->isDefinition()); 22590b57cec5SDimitry Andric Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration()); 22600b57cec5SDimitry Andric Printer.printMetadata("templateParams", N->getRawTemplateParams()); 22610b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 2262349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 22630b57cec5SDimitry Andric Out << ")"; 22640b57cec5SDimitry Andric } 22650b57cec5SDimitry Andric 22660b57cec5SDimitry Andric static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N, 2267349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22680b57cec5SDimitry Andric Out << "!DILocalVariable("; 2269349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22700b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22710b57cec5SDimitry Andric Printer.printInt("arg", N->getArg()); 22720b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22730b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22740b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22750b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 22760b57cec5SDimitry Andric Printer.printDIFlags("flags", N->getFlags()); 22770b57cec5SDimitry Andric Printer.printInt("align", N->getAlignInBits()); 2278349cc55cSDimitry Andric Printer.printMetadata("annotations", N->getRawAnnotations()); 22790b57cec5SDimitry Andric Out << ")"; 22800b57cec5SDimitry Andric } 22810b57cec5SDimitry Andric 22820b57cec5SDimitry Andric static void writeDILabel(raw_ostream &Out, const DILabel *N, 2283349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22840b57cec5SDimitry Andric Out << "!DILabel("; 2285349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 22860b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 22870b57cec5SDimitry Andric Printer.printString("name", N->getName()); 22880b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 22890b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 22900b57cec5SDimitry Andric Out << ")"; 22910b57cec5SDimitry Andric } 22920b57cec5SDimitry Andric 22930b57cec5SDimitry Andric static void writeDIExpression(raw_ostream &Out, const DIExpression *N, 2294349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 22950b57cec5SDimitry Andric Out << "!DIExpression("; 22960b57cec5SDimitry Andric FieldSeparator FS; 22970b57cec5SDimitry Andric if (N->isValid()) { 2298fe6060f1SDimitry Andric for (const DIExpression::ExprOperand &Op : N->expr_ops()) { 2299fe6060f1SDimitry Andric auto OpStr = dwarf::OperationEncodingString(Op.getOp()); 23000b57cec5SDimitry Andric assert(!OpStr.empty() && "Expected valid opcode"); 23010b57cec5SDimitry Andric 23020b57cec5SDimitry Andric Out << FS << OpStr; 2303fe6060f1SDimitry Andric if (Op.getOp() == dwarf::DW_OP_LLVM_convert) { 2304fe6060f1SDimitry Andric Out << FS << Op.getArg(0); 2305fe6060f1SDimitry Andric Out << FS << dwarf::AttributeEncodingString(Op.getArg(1)); 23060b57cec5SDimitry Andric } else { 2307fe6060f1SDimitry Andric for (unsigned A = 0, AE = Op.getNumArgs(); A != AE; ++A) 2308fe6060f1SDimitry Andric Out << FS << Op.getArg(A); 23090b57cec5SDimitry Andric } 23100b57cec5SDimitry Andric } 23110b57cec5SDimitry Andric } else { 23120b57cec5SDimitry Andric for (const auto &I : N->getElements()) 23130b57cec5SDimitry Andric Out << FS << I; 23140b57cec5SDimitry Andric } 23150b57cec5SDimitry Andric Out << ")"; 23160b57cec5SDimitry Andric } 23170b57cec5SDimitry Andric 2318fe6060f1SDimitry Andric static void writeDIArgList(raw_ostream &Out, const DIArgList *N, 2319349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 2320349cc55cSDimitry Andric bool FromValue = false) { 2321fe6060f1SDimitry Andric assert(FromValue && 2322fe6060f1SDimitry Andric "Unexpected DIArgList metadata outside of value argument"); 2323fe6060f1SDimitry Andric Out << "!DIArgList("; 2324fe6060f1SDimitry Andric FieldSeparator FS; 2325349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 2326fe6060f1SDimitry Andric for (Metadata *Arg : N->getArgs()) { 2327fe6060f1SDimitry Andric Out << FS; 2328349cc55cSDimitry Andric WriteAsOperandInternal(Out, Arg, WriterCtx, true); 2329fe6060f1SDimitry Andric } 2330fe6060f1SDimitry Andric Out << ")"; 2331fe6060f1SDimitry Andric } 2332fe6060f1SDimitry Andric 23330b57cec5SDimitry Andric static void writeDIGlobalVariableExpression(raw_ostream &Out, 23340b57cec5SDimitry Andric const DIGlobalVariableExpression *N, 2335349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23360b57cec5SDimitry Andric Out << "!DIGlobalVariableExpression("; 2337349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23380b57cec5SDimitry Andric Printer.printMetadata("var", N->getVariable()); 23390b57cec5SDimitry Andric Printer.printMetadata("expr", N->getExpression()); 23400b57cec5SDimitry Andric Out << ")"; 23410b57cec5SDimitry Andric } 23420b57cec5SDimitry Andric 23430b57cec5SDimitry Andric static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N, 2344349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23450b57cec5SDimitry Andric Out << "!DIObjCProperty("; 2346349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23470b57cec5SDimitry Andric Printer.printString("name", N->getName()); 23480b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 23490b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 23500b57cec5SDimitry Andric Printer.printString("setter", N->getSetterName()); 23510b57cec5SDimitry Andric Printer.printString("getter", N->getGetterName()); 23520b57cec5SDimitry Andric Printer.printInt("attributes", N->getAttributes()); 23530b57cec5SDimitry Andric Printer.printMetadata("type", N->getRawType()); 23540b57cec5SDimitry Andric Out << ")"; 23550b57cec5SDimitry Andric } 23560b57cec5SDimitry Andric 23570b57cec5SDimitry Andric static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N, 2358349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23590b57cec5SDimitry Andric Out << "!DIImportedEntity("; 2360349cc55cSDimitry Andric MDFieldPrinter Printer(Out, WriterCtx); 23610b57cec5SDimitry Andric Printer.printTag(N); 23620b57cec5SDimitry Andric Printer.printString("name", N->getName()); 23630b57cec5SDimitry Andric Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false); 23640b57cec5SDimitry Andric Printer.printMetadata("entity", N->getRawEntity()); 23650b57cec5SDimitry Andric Printer.printMetadata("file", N->getRawFile()); 23660b57cec5SDimitry Andric Printer.printInt("line", N->getLine()); 2367349cc55cSDimitry Andric Printer.printMetadata("elements", N->getRawElements()); 23680b57cec5SDimitry Andric Out << ")"; 23690b57cec5SDimitry Andric } 23700b57cec5SDimitry Andric 23710b57cec5SDimitry Andric static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node, 2372349cc55cSDimitry Andric AsmWriterContext &Ctx) { 23730b57cec5SDimitry Andric if (Node->isDistinct()) 23740b57cec5SDimitry Andric Out << "distinct "; 23750b57cec5SDimitry Andric else if (Node->isTemporary()) 23760b57cec5SDimitry Andric Out << "<temporary!> "; // Handle broken code. 23770b57cec5SDimitry Andric 23780b57cec5SDimitry Andric switch (Node->getMetadataID()) { 23790b57cec5SDimitry Andric default: 23800b57cec5SDimitry Andric llvm_unreachable("Expected uniquable MDNode"); 23810b57cec5SDimitry Andric #define HANDLE_MDNODE_LEAF(CLASS) \ 23820b57cec5SDimitry Andric case Metadata::CLASS##Kind: \ 2383349cc55cSDimitry Andric write##CLASS(Out, cast<CLASS>(Node), Ctx); \ 23840b57cec5SDimitry Andric break; 23850b57cec5SDimitry Andric #include "llvm/IR/Metadata.def" 23860b57cec5SDimitry Andric } 23870b57cec5SDimitry Andric } 23880b57cec5SDimitry Andric 23890b57cec5SDimitry Andric // Full implementation of printing a Value as an operand with support for 23900b57cec5SDimitry Andric // TypePrinting, etc. 23910b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Value *V, 2392349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 23930b57cec5SDimitry Andric if (V->hasName()) { 23940b57cec5SDimitry Andric PrintLLVMName(Out, V); 23950b57cec5SDimitry Andric return; 23960b57cec5SDimitry Andric } 23970b57cec5SDimitry Andric 23980b57cec5SDimitry Andric const Constant *CV = dyn_cast<Constant>(V); 23990b57cec5SDimitry Andric if (CV && !isa<GlobalValue>(CV)) { 2400349cc55cSDimitry Andric assert(WriterCtx.TypePrinter && "Constants require TypePrinting!"); 2401349cc55cSDimitry Andric WriteConstantInternal(Out, CV, WriterCtx); 24020b57cec5SDimitry Andric return; 24030b57cec5SDimitry Andric } 24040b57cec5SDimitry Andric 24050b57cec5SDimitry Andric if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) { 24060b57cec5SDimitry Andric Out << "asm "; 24070b57cec5SDimitry Andric if (IA->hasSideEffects()) 24080b57cec5SDimitry Andric Out << "sideeffect "; 24090b57cec5SDimitry Andric if (IA->isAlignStack()) 24100b57cec5SDimitry Andric Out << "alignstack "; 24110b57cec5SDimitry Andric // We don't emit the AD_ATT dialect as it's the assumed default. 24120b57cec5SDimitry Andric if (IA->getDialect() == InlineAsm::AD_Intel) 24130b57cec5SDimitry Andric Out << "inteldialect "; 2414fe6060f1SDimitry Andric if (IA->canThrow()) 2415fe6060f1SDimitry Andric Out << "unwind "; 24160b57cec5SDimitry Andric Out << '"'; 24170b57cec5SDimitry Andric printEscapedString(IA->getAsmString(), Out); 24180b57cec5SDimitry Andric Out << "\", \""; 24190b57cec5SDimitry Andric printEscapedString(IA->getConstraintString(), Out); 24200b57cec5SDimitry Andric Out << '"'; 24210b57cec5SDimitry Andric return; 24220b57cec5SDimitry Andric } 24230b57cec5SDimitry Andric 24240b57cec5SDimitry Andric if (auto *MD = dyn_cast<MetadataAsValue>(V)) { 2425349cc55cSDimitry Andric WriteAsOperandInternal(Out, MD->getMetadata(), WriterCtx, 2426349cc55cSDimitry Andric /* FromValue */ true); 24270b57cec5SDimitry Andric return; 24280b57cec5SDimitry Andric } 24290b57cec5SDimitry Andric 24300b57cec5SDimitry Andric char Prefix = '%'; 24310b57cec5SDimitry Andric int Slot; 2432349cc55cSDimitry Andric auto *Machine = WriterCtx.Machine; 24330b57cec5SDimitry Andric // If we have a SlotTracker, use it. 24340b57cec5SDimitry Andric if (Machine) { 24350b57cec5SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 24360b57cec5SDimitry Andric Slot = Machine->getGlobalSlot(GV); 24370b57cec5SDimitry Andric Prefix = '@'; 24380b57cec5SDimitry Andric } else { 24390b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24400b57cec5SDimitry Andric 24410b57cec5SDimitry Andric // If the local value didn't succeed, then we may be referring to a value 24420b57cec5SDimitry Andric // from a different function. Translate it, as this can happen when using 24430b57cec5SDimitry Andric // address of blocks. 24440b57cec5SDimitry Andric if (Slot == -1) 24450b57cec5SDimitry Andric if ((Machine = createSlotTracker(V))) { 24460b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24470b57cec5SDimitry Andric delete Machine; 24480b57cec5SDimitry Andric } 24490b57cec5SDimitry Andric } 24500b57cec5SDimitry Andric } else if ((Machine = createSlotTracker(V))) { 24510b57cec5SDimitry Andric // Otherwise, create one to get the # and then destroy it. 24520b57cec5SDimitry Andric if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) { 24530b57cec5SDimitry Andric Slot = Machine->getGlobalSlot(GV); 24540b57cec5SDimitry Andric Prefix = '@'; 24550b57cec5SDimitry Andric } else { 24560b57cec5SDimitry Andric Slot = Machine->getLocalSlot(V); 24570b57cec5SDimitry Andric } 24580b57cec5SDimitry Andric delete Machine; 24590b57cec5SDimitry Andric Machine = nullptr; 24600b57cec5SDimitry Andric } else { 24610b57cec5SDimitry Andric Slot = -1; 24620b57cec5SDimitry Andric } 24630b57cec5SDimitry Andric 24640b57cec5SDimitry Andric if (Slot != -1) 24650b57cec5SDimitry Andric Out << Prefix << Slot; 24660b57cec5SDimitry Andric else 24670b57cec5SDimitry Andric Out << "<badref>"; 24680b57cec5SDimitry Andric } 24690b57cec5SDimitry Andric 24700b57cec5SDimitry Andric static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD, 2471349cc55cSDimitry Andric AsmWriterContext &WriterCtx, 24720b57cec5SDimitry Andric bool FromValue) { 2473fe6060f1SDimitry Andric // Write DIExpressions and DIArgLists inline when used as a value. Improves 2474fe6060f1SDimitry Andric // readability of debug info intrinsics. 24750b57cec5SDimitry Andric if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) { 2476349cc55cSDimitry Andric writeDIExpression(Out, Expr, WriterCtx); 24770b57cec5SDimitry Andric return; 24780b57cec5SDimitry Andric } 2479fe6060f1SDimitry Andric if (const DIArgList *ArgList = dyn_cast<DIArgList>(MD)) { 2480349cc55cSDimitry Andric writeDIArgList(Out, ArgList, WriterCtx, FromValue); 2481fe6060f1SDimitry Andric return; 2482fe6060f1SDimitry Andric } 24830b57cec5SDimitry Andric 24840b57cec5SDimitry Andric if (const MDNode *N = dyn_cast<MDNode>(MD)) { 24850b57cec5SDimitry Andric std::unique_ptr<SlotTracker> MachineStorage; 2486349cc55cSDimitry Andric SaveAndRestore<SlotTracker *> SARMachine(WriterCtx.Machine); 2487349cc55cSDimitry Andric if (!WriterCtx.Machine) { 2488349cc55cSDimitry Andric MachineStorage = std::make_unique<SlotTracker>(WriterCtx.Context); 2489349cc55cSDimitry Andric WriterCtx.Machine = MachineStorage.get(); 24900b57cec5SDimitry Andric } 2491349cc55cSDimitry Andric int Slot = WriterCtx.Machine->getMetadataSlot(N); 24920b57cec5SDimitry Andric if (Slot == -1) { 24930b57cec5SDimitry Andric if (const DILocation *Loc = dyn_cast<DILocation>(N)) { 2494349cc55cSDimitry Andric writeDILocation(Out, Loc, WriterCtx); 24950b57cec5SDimitry Andric return; 24960b57cec5SDimitry Andric } 24970b57cec5SDimitry Andric // Give the pointer value instead of "badref", since this comes up all 24980b57cec5SDimitry Andric // the time when debugging. 24990b57cec5SDimitry Andric Out << "<" << N << ">"; 25000b57cec5SDimitry Andric } else 25010b57cec5SDimitry Andric Out << '!' << Slot; 25020b57cec5SDimitry Andric return; 25030b57cec5SDimitry Andric } 25040b57cec5SDimitry Andric 25050b57cec5SDimitry Andric if (const MDString *MDS = dyn_cast<MDString>(MD)) { 25060b57cec5SDimitry Andric Out << "!\""; 25070b57cec5SDimitry Andric printEscapedString(MDS->getString(), Out); 25080b57cec5SDimitry Andric Out << '"'; 25090b57cec5SDimitry Andric return; 25100b57cec5SDimitry Andric } 25110b57cec5SDimitry Andric 25120b57cec5SDimitry Andric auto *V = cast<ValueAsMetadata>(MD); 2513349cc55cSDimitry Andric assert(WriterCtx.TypePrinter && "TypePrinter required for metadata values"); 25140b57cec5SDimitry Andric assert((FromValue || !isa<LocalAsMetadata>(V)) && 25150b57cec5SDimitry Andric "Unexpected function-local metadata outside of value argument"); 25160b57cec5SDimitry Andric 2517349cc55cSDimitry Andric WriterCtx.TypePrinter->print(V->getValue()->getType(), Out); 25180b57cec5SDimitry Andric Out << ' '; 2519349cc55cSDimitry Andric WriteAsOperandInternal(Out, V->getValue(), WriterCtx); 25200b57cec5SDimitry Andric } 25210b57cec5SDimitry Andric 25220b57cec5SDimitry Andric namespace { 25230b57cec5SDimitry Andric 25240b57cec5SDimitry Andric class AssemblyWriter { 25250b57cec5SDimitry Andric formatted_raw_ostream &Out; 25260b57cec5SDimitry Andric const Module *TheModule = nullptr; 25270b57cec5SDimitry Andric const ModuleSummaryIndex *TheIndex = nullptr; 25280b57cec5SDimitry Andric std::unique_ptr<SlotTracker> SlotTrackerStorage; 25290b57cec5SDimitry Andric SlotTracker &Machine; 25300b57cec5SDimitry Andric TypePrinting TypePrinter; 25310b57cec5SDimitry Andric AssemblyAnnotationWriter *AnnotationWriter = nullptr; 25320b57cec5SDimitry Andric SetVector<const Comdat *> Comdats; 25330b57cec5SDimitry Andric bool IsForDebug; 25340b57cec5SDimitry Andric bool ShouldPreserveUseListOrder; 2535fe6060f1SDimitry Andric UseListOrderMap UseListOrders; 25360b57cec5SDimitry Andric SmallVector<StringRef, 8> MDNames; 25370b57cec5SDimitry Andric /// Synchronization scope names registered with LLVMContext. 25380b57cec5SDimitry Andric SmallVector<StringRef, 8> SSNs; 25390b57cec5SDimitry Andric DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap; 25400b57cec5SDimitry Andric 25410b57cec5SDimitry Andric public: 25420b57cec5SDimitry Andric /// Construct an AssemblyWriter with an external SlotTracker 25430b57cec5SDimitry Andric AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M, 25440b57cec5SDimitry Andric AssemblyAnnotationWriter *AAW, bool IsForDebug, 25450b57cec5SDimitry Andric bool ShouldPreserveUseListOrder = false); 25460b57cec5SDimitry Andric 25470b57cec5SDimitry Andric AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 25480b57cec5SDimitry Andric const ModuleSummaryIndex *Index, bool IsForDebug); 25490b57cec5SDimitry Andric 2550349cc55cSDimitry Andric AsmWriterContext getContext() { 2551349cc55cSDimitry Andric return AsmWriterContext(&TypePrinter, &Machine, TheModule); 2552349cc55cSDimitry Andric } 2553349cc55cSDimitry Andric 25540b57cec5SDimitry Andric void printMDNodeBody(const MDNode *MD); 25550b57cec5SDimitry Andric void printNamedMDNode(const NamedMDNode *NMD); 25560b57cec5SDimitry Andric 25570b57cec5SDimitry Andric void printModule(const Module *M); 25580b57cec5SDimitry Andric 25590b57cec5SDimitry Andric void writeOperand(const Value *Op, bool PrintType); 25600b57cec5SDimitry Andric void writeParamOperand(const Value *Operand, AttributeSet Attrs); 25610b57cec5SDimitry Andric void writeOperandBundles(const CallBase *Call); 25620b57cec5SDimitry Andric void writeSyncScope(const LLVMContext &Context, 25630b57cec5SDimitry Andric SyncScope::ID SSID); 25640b57cec5SDimitry Andric void writeAtomic(const LLVMContext &Context, 25650b57cec5SDimitry Andric AtomicOrdering Ordering, 25660b57cec5SDimitry Andric SyncScope::ID SSID); 25670b57cec5SDimitry Andric void writeAtomicCmpXchg(const LLVMContext &Context, 25680b57cec5SDimitry Andric AtomicOrdering SuccessOrdering, 25690b57cec5SDimitry Andric AtomicOrdering FailureOrdering, 25700b57cec5SDimitry Andric SyncScope::ID SSID); 25710b57cec5SDimitry Andric 25720b57cec5SDimitry Andric void writeAllMDNodes(); 25730b57cec5SDimitry Andric void writeMDNode(unsigned Slot, const MDNode *Node); 2574480093f4SDimitry Andric void writeAttribute(const Attribute &Attr, bool InAttrGroup = false); 2575480093f4SDimitry Andric void writeAttributeSet(const AttributeSet &AttrSet, bool InAttrGroup = false); 25760b57cec5SDimitry Andric void writeAllAttributeGroups(); 25770b57cec5SDimitry Andric 25780b57cec5SDimitry Andric void printTypeIdentities(); 25790b57cec5SDimitry Andric void printGlobal(const GlobalVariable *GV); 2580349cc55cSDimitry Andric void printAlias(const GlobalAlias *GA); 2581349cc55cSDimitry Andric void printIFunc(const GlobalIFunc *GI); 25820b57cec5SDimitry Andric void printComdat(const Comdat *C); 25830b57cec5SDimitry Andric void printFunction(const Function *F); 25840b57cec5SDimitry Andric void printArgument(const Argument *FA, AttributeSet Attrs); 25850b57cec5SDimitry Andric void printBasicBlock(const BasicBlock *BB); 25860b57cec5SDimitry Andric void printInstructionLine(const Instruction &I); 25870b57cec5SDimitry Andric void printInstruction(const Instruction &I); 25880b57cec5SDimitry Andric 2589fe6060f1SDimitry Andric void printUseListOrder(const Value *V, const std::vector<unsigned> &Shuffle); 25900b57cec5SDimitry Andric void printUseLists(const Function *F); 25910b57cec5SDimitry Andric 25920b57cec5SDimitry Andric void printModuleSummaryIndex(); 25930b57cec5SDimitry Andric void printSummaryInfo(unsigned Slot, const ValueInfo &VI); 25940b57cec5SDimitry Andric void printSummary(const GlobalValueSummary &Summary); 25950b57cec5SDimitry Andric void printAliasSummary(const AliasSummary *AS); 25960b57cec5SDimitry Andric void printGlobalVarSummary(const GlobalVarSummary *GS); 25970b57cec5SDimitry Andric void printFunctionSummary(const FunctionSummary *FS); 25980b57cec5SDimitry Andric void printTypeIdSummary(const TypeIdSummary &TIS); 25990b57cec5SDimitry Andric void printTypeIdCompatibleVtableSummary(const TypeIdCompatibleVtableInfo &TI); 26000b57cec5SDimitry Andric void printTypeTestResolution(const TypeTestResolution &TTRes); 26010b57cec5SDimitry Andric void printArgs(const std::vector<uint64_t> &Args); 26020b57cec5SDimitry Andric void printWPDRes(const WholeProgramDevirtResolution &WPDRes); 26030b57cec5SDimitry Andric void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo); 26040b57cec5SDimitry Andric void printVFuncId(const FunctionSummary::VFuncId VFId); 26050b57cec5SDimitry Andric void 26065ffd83dbSDimitry Andric printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> &VCallList, 26070b57cec5SDimitry Andric const char *Tag); 26080b57cec5SDimitry Andric void 26095ffd83dbSDimitry Andric printConstVCalls(const std::vector<FunctionSummary::ConstVCall> &VCallList, 26100b57cec5SDimitry Andric const char *Tag); 26110b57cec5SDimitry Andric 26120b57cec5SDimitry Andric private: 26130b57cec5SDimitry Andric /// Print out metadata attachments. 26140b57cec5SDimitry Andric void printMetadataAttachments( 26150b57cec5SDimitry Andric const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs, 26160b57cec5SDimitry Andric StringRef Separator); 26170b57cec5SDimitry Andric 26180b57cec5SDimitry Andric // printInfoComment - Print a little comment after the instruction indicating 26190b57cec5SDimitry Andric // which slot it occupies. 26200b57cec5SDimitry Andric void printInfoComment(const Value &V); 26210b57cec5SDimitry Andric 26220b57cec5SDimitry Andric // printGCRelocateComment - print comment after call to the gc.relocate 26230b57cec5SDimitry Andric // intrinsic indicating base and derived pointer names. 26240b57cec5SDimitry Andric void printGCRelocateComment(const GCRelocateInst &Relocate); 26250b57cec5SDimitry Andric }; 26260b57cec5SDimitry Andric 26270b57cec5SDimitry Andric } // end anonymous namespace 26280b57cec5SDimitry Andric 26290b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 26300b57cec5SDimitry Andric const Module *M, AssemblyAnnotationWriter *AAW, 26310b57cec5SDimitry Andric bool IsForDebug, bool ShouldPreserveUseListOrder) 26320b57cec5SDimitry Andric : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW), 26330b57cec5SDimitry Andric IsForDebug(IsForDebug), 26340b57cec5SDimitry Andric ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) { 26350b57cec5SDimitry Andric if (!TheModule) 26360b57cec5SDimitry Andric return; 26370b57cec5SDimitry Andric for (const GlobalObject &GO : TheModule->global_objects()) 26380b57cec5SDimitry Andric if (const Comdat *C = GO.getComdat()) 26390b57cec5SDimitry Andric Comdats.insert(C); 26400b57cec5SDimitry Andric } 26410b57cec5SDimitry Andric 26420b57cec5SDimitry Andric AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, 26430b57cec5SDimitry Andric const ModuleSummaryIndex *Index, bool IsForDebug) 26440b57cec5SDimitry Andric : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr), 26450b57cec5SDimitry Andric IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {} 26460b57cec5SDimitry Andric 26470b57cec5SDimitry Andric void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) { 26480b57cec5SDimitry Andric if (!Operand) { 26490b57cec5SDimitry Andric Out << "<null operand!>"; 26500b57cec5SDimitry Andric return; 26510b57cec5SDimitry Andric } 26520b57cec5SDimitry Andric if (PrintType) { 26530b57cec5SDimitry Andric TypePrinter.print(Operand->getType(), Out); 26540b57cec5SDimitry Andric Out << ' '; 26550b57cec5SDimitry Andric } 2656349cc55cSDimitry Andric auto WriterCtx = getContext(); 2657349cc55cSDimitry Andric WriteAsOperandInternal(Out, Operand, WriterCtx); 26580b57cec5SDimitry Andric } 26590b57cec5SDimitry Andric 26600b57cec5SDimitry Andric void AssemblyWriter::writeSyncScope(const LLVMContext &Context, 26610b57cec5SDimitry Andric SyncScope::ID SSID) { 26620b57cec5SDimitry Andric switch (SSID) { 26630b57cec5SDimitry Andric case SyncScope::System: { 26640b57cec5SDimitry Andric break; 26650b57cec5SDimitry Andric } 26660b57cec5SDimitry Andric default: { 26670b57cec5SDimitry Andric if (SSNs.empty()) 26680b57cec5SDimitry Andric Context.getSyncScopeNames(SSNs); 26690b57cec5SDimitry Andric 26700b57cec5SDimitry Andric Out << " syncscope(\""; 26710b57cec5SDimitry Andric printEscapedString(SSNs[SSID], Out); 26720b57cec5SDimitry Andric Out << "\")"; 26730b57cec5SDimitry Andric break; 26740b57cec5SDimitry Andric } 26750b57cec5SDimitry Andric } 26760b57cec5SDimitry Andric } 26770b57cec5SDimitry Andric 26780b57cec5SDimitry Andric void AssemblyWriter::writeAtomic(const LLVMContext &Context, 26790b57cec5SDimitry Andric AtomicOrdering Ordering, 26800b57cec5SDimitry Andric SyncScope::ID SSID) { 26810b57cec5SDimitry Andric if (Ordering == AtomicOrdering::NotAtomic) 26820b57cec5SDimitry Andric return; 26830b57cec5SDimitry Andric 26840b57cec5SDimitry Andric writeSyncScope(Context, SSID); 26850b57cec5SDimitry Andric Out << " " << toIRString(Ordering); 26860b57cec5SDimitry Andric } 26870b57cec5SDimitry Andric 26880b57cec5SDimitry Andric void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context, 26890b57cec5SDimitry Andric AtomicOrdering SuccessOrdering, 26900b57cec5SDimitry Andric AtomicOrdering FailureOrdering, 26910b57cec5SDimitry Andric SyncScope::ID SSID) { 26920b57cec5SDimitry Andric assert(SuccessOrdering != AtomicOrdering::NotAtomic && 26930b57cec5SDimitry Andric FailureOrdering != AtomicOrdering::NotAtomic); 26940b57cec5SDimitry Andric 26950b57cec5SDimitry Andric writeSyncScope(Context, SSID); 26960b57cec5SDimitry Andric Out << " " << toIRString(SuccessOrdering); 26970b57cec5SDimitry Andric Out << " " << toIRString(FailureOrdering); 26980b57cec5SDimitry Andric } 26990b57cec5SDimitry Andric 27000b57cec5SDimitry Andric void AssemblyWriter::writeParamOperand(const Value *Operand, 27010b57cec5SDimitry Andric AttributeSet Attrs) { 27020b57cec5SDimitry Andric if (!Operand) { 27030b57cec5SDimitry Andric Out << "<null operand!>"; 27040b57cec5SDimitry Andric return; 27050b57cec5SDimitry Andric } 27060b57cec5SDimitry Andric 27070b57cec5SDimitry Andric // Print the type 27080b57cec5SDimitry Andric TypePrinter.print(Operand->getType(), Out); 27090b57cec5SDimitry Andric // Print parameter attributes list 2710480093f4SDimitry Andric if (Attrs.hasAttributes()) { 2711480093f4SDimitry Andric Out << ' '; 2712480093f4SDimitry Andric writeAttributeSet(Attrs); 2713480093f4SDimitry Andric } 27140b57cec5SDimitry Andric Out << ' '; 27150b57cec5SDimitry Andric // Print the operand 2716349cc55cSDimitry Andric auto WriterCtx = getContext(); 2717349cc55cSDimitry Andric WriteAsOperandInternal(Out, Operand, WriterCtx); 27180b57cec5SDimitry Andric } 27190b57cec5SDimitry Andric 27200b57cec5SDimitry Andric void AssemblyWriter::writeOperandBundles(const CallBase *Call) { 27210b57cec5SDimitry Andric if (!Call->hasOperandBundles()) 27220b57cec5SDimitry Andric return; 27230b57cec5SDimitry Andric 27240b57cec5SDimitry Andric Out << " [ "; 27250b57cec5SDimitry Andric 27260b57cec5SDimitry Andric bool FirstBundle = true; 27270b57cec5SDimitry Andric for (unsigned i = 0, e = Call->getNumOperandBundles(); i != e; ++i) { 27280b57cec5SDimitry Andric OperandBundleUse BU = Call->getOperandBundleAt(i); 27290b57cec5SDimitry Andric 27300b57cec5SDimitry Andric if (!FirstBundle) 27310b57cec5SDimitry Andric Out << ", "; 27320b57cec5SDimitry Andric FirstBundle = false; 27330b57cec5SDimitry Andric 27340b57cec5SDimitry Andric Out << '"'; 27350b57cec5SDimitry Andric printEscapedString(BU.getTagName(), Out); 27360b57cec5SDimitry Andric Out << '"'; 27370b57cec5SDimitry Andric 27380b57cec5SDimitry Andric Out << '('; 27390b57cec5SDimitry Andric 27400b57cec5SDimitry Andric bool FirstInput = true; 2741349cc55cSDimitry Andric auto WriterCtx = getContext(); 27420b57cec5SDimitry Andric for (const auto &Input : BU.Inputs) { 27430b57cec5SDimitry Andric if (!FirstInput) 27440b57cec5SDimitry Andric Out << ", "; 27450b57cec5SDimitry Andric FirstInput = false; 27460b57cec5SDimitry Andric 27470b57cec5SDimitry Andric TypePrinter.print(Input->getType(), Out); 27480b57cec5SDimitry Andric Out << " "; 2749349cc55cSDimitry Andric WriteAsOperandInternal(Out, Input, WriterCtx); 27500b57cec5SDimitry Andric } 27510b57cec5SDimitry Andric 27520b57cec5SDimitry Andric Out << ')'; 27530b57cec5SDimitry Andric } 27540b57cec5SDimitry Andric 27550b57cec5SDimitry Andric Out << " ]"; 27560b57cec5SDimitry Andric } 27570b57cec5SDimitry Andric 27580b57cec5SDimitry Andric void AssemblyWriter::printModule(const Module *M) { 27590b57cec5SDimitry Andric Machine.initializeIfNeeded(); 27600b57cec5SDimitry Andric 27610b57cec5SDimitry Andric if (ShouldPreserveUseListOrder) 27620b57cec5SDimitry Andric UseListOrders = predictUseListOrder(M); 27630b57cec5SDimitry Andric 27640b57cec5SDimitry Andric if (!M->getModuleIdentifier().empty() && 27650b57cec5SDimitry Andric // Don't print the ID if it will start a new line (which would 27660b57cec5SDimitry Andric // require a comment char before it). 27670b57cec5SDimitry Andric M->getModuleIdentifier().find('\n') == std::string::npos) 27680b57cec5SDimitry Andric Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; 27690b57cec5SDimitry Andric 27700b57cec5SDimitry Andric if (!M->getSourceFileName().empty()) { 27710b57cec5SDimitry Andric Out << "source_filename = \""; 27720b57cec5SDimitry Andric printEscapedString(M->getSourceFileName(), Out); 27730b57cec5SDimitry Andric Out << "\"\n"; 27740b57cec5SDimitry Andric } 27750b57cec5SDimitry Andric 27760b57cec5SDimitry Andric const std::string &DL = M->getDataLayoutStr(); 27770b57cec5SDimitry Andric if (!DL.empty()) 27780b57cec5SDimitry Andric Out << "target datalayout = \"" << DL << "\"\n"; 27790b57cec5SDimitry Andric if (!M->getTargetTriple().empty()) 27800b57cec5SDimitry Andric Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; 27810b57cec5SDimitry Andric 27820b57cec5SDimitry Andric if (!M->getModuleInlineAsm().empty()) { 27830b57cec5SDimitry Andric Out << '\n'; 27840b57cec5SDimitry Andric 27850b57cec5SDimitry Andric // Split the string into lines, to make it easier to read the .ll file. 27860b57cec5SDimitry Andric StringRef Asm = M->getModuleInlineAsm(); 27870b57cec5SDimitry Andric do { 27880b57cec5SDimitry Andric StringRef Front; 27890b57cec5SDimitry Andric std::tie(Front, Asm) = Asm.split('\n'); 27900b57cec5SDimitry Andric 27910b57cec5SDimitry Andric // We found a newline, print the portion of the asm string from the 27920b57cec5SDimitry Andric // last newline up to this newline. 27930b57cec5SDimitry Andric Out << "module asm \""; 27940b57cec5SDimitry Andric printEscapedString(Front, Out); 27950b57cec5SDimitry Andric Out << "\"\n"; 27960b57cec5SDimitry Andric } while (!Asm.empty()); 27970b57cec5SDimitry Andric } 27980b57cec5SDimitry Andric 27990b57cec5SDimitry Andric printTypeIdentities(); 28000b57cec5SDimitry Andric 28010b57cec5SDimitry Andric // Output all comdats. 28020b57cec5SDimitry Andric if (!Comdats.empty()) 28030b57cec5SDimitry Andric Out << '\n'; 28040b57cec5SDimitry Andric for (const Comdat *C : Comdats) { 28050b57cec5SDimitry Andric printComdat(C); 28060b57cec5SDimitry Andric if (C != Comdats.back()) 28070b57cec5SDimitry Andric Out << '\n'; 28080b57cec5SDimitry Andric } 28090b57cec5SDimitry Andric 28100b57cec5SDimitry Andric // Output all globals. 28110b57cec5SDimitry Andric if (!M->global_empty()) Out << '\n'; 28120b57cec5SDimitry Andric for (const GlobalVariable &GV : M->globals()) { 28130b57cec5SDimitry Andric printGlobal(&GV); Out << '\n'; 28140b57cec5SDimitry Andric } 28150b57cec5SDimitry Andric 28160b57cec5SDimitry Andric // Output all aliases. 28170b57cec5SDimitry Andric if (!M->alias_empty()) Out << "\n"; 28180b57cec5SDimitry Andric for (const GlobalAlias &GA : M->aliases()) 2819349cc55cSDimitry Andric printAlias(&GA); 28200b57cec5SDimitry Andric 28210b57cec5SDimitry Andric // Output all ifuncs. 28220b57cec5SDimitry Andric if (!M->ifunc_empty()) Out << "\n"; 28230b57cec5SDimitry Andric for (const GlobalIFunc &GI : M->ifuncs()) 2824349cc55cSDimitry Andric printIFunc(&GI); 28250b57cec5SDimitry Andric 28260b57cec5SDimitry Andric // Output all of the functions. 282713138422SDimitry Andric for (const Function &F : *M) { 282813138422SDimitry Andric Out << '\n'; 28290b57cec5SDimitry Andric printFunction(&F); 283013138422SDimitry Andric } 2831fe6060f1SDimitry Andric 2832fe6060f1SDimitry Andric // Output global use-lists. 2833fe6060f1SDimitry Andric printUseLists(nullptr); 28340b57cec5SDimitry Andric 28350b57cec5SDimitry Andric // Output all attribute groups. 28360b57cec5SDimitry Andric if (!Machine.as_empty()) { 28370b57cec5SDimitry Andric Out << '\n'; 28380b57cec5SDimitry Andric writeAllAttributeGroups(); 28390b57cec5SDimitry Andric } 28400b57cec5SDimitry Andric 28410b57cec5SDimitry Andric // Output named metadata. 28420b57cec5SDimitry Andric if (!M->named_metadata_empty()) Out << '\n'; 28430b57cec5SDimitry Andric 28440b57cec5SDimitry Andric for (const NamedMDNode &Node : M->named_metadata()) 28450b57cec5SDimitry Andric printNamedMDNode(&Node); 28460b57cec5SDimitry Andric 28470b57cec5SDimitry Andric // Output metadata. 28480b57cec5SDimitry Andric if (!Machine.mdn_empty()) { 28490b57cec5SDimitry Andric Out << '\n'; 28500b57cec5SDimitry Andric writeAllMDNodes(); 28510b57cec5SDimitry Andric } 28520b57cec5SDimitry Andric } 28530b57cec5SDimitry Andric 28540b57cec5SDimitry Andric void AssemblyWriter::printModuleSummaryIndex() { 28550b57cec5SDimitry Andric assert(TheIndex); 28565ffd83dbSDimitry Andric int NumSlots = Machine.initializeIndexIfNeeded(); 28570b57cec5SDimitry Andric 28580b57cec5SDimitry Andric Out << "\n"; 28590b57cec5SDimitry Andric 28600b57cec5SDimitry Andric // Print module path entries. To print in order, add paths to a vector 28610b57cec5SDimitry Andric // indexed by module slot. 28620b57cec5SDimitry Andric std::vector<std::pair<std::string, ModuleHash>> moduleVec; 28635ffd83dbSDimitry Andric std::string RegularLTOModuleName = 28645ffd83dbSDimitry Andric ModuleSummaryIndex::getRegularLTOModuleName(); 28650b57cec5SDimitry Andric moduleVec.resize(TheIndex->modulePaths().size()); 28660b57cec5SDimitry Andric for (auto &ModPath : TheIndex->modulePaths()) 28670b57cec5SDimitry Andric moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair( 28680b57cec5SDimitry Andric // A module id of -1 is a special entry for a regular LTO module created 28690b57cec5SDimitry Andric // during the thin link. 28700b57cec5SDimitry Andric ModPath.second.first == -1u ? RegularLTOModuleName 28715ffd83dbSDimitry Andric : (std::string)std::string(ModPath.first()), 28720b57cec5SDimitry Andric ModPath.second.second); 28730b57cec5SDimitry Andric 28740b57cec5SDimitry Andric unsigned i = 0; 28750b57cec5SDimitry Andric for (auto &ModPair : moduleVec) { 28760b57cec5SDimitry Andric Out << "^" << i++ << " = module: ("; 28770b57cec5SDimitry Andric Out << "path: \""; 28780b57cec5SDimitry Andric printEscapedString(ModPair.first, Out); 28790b57cec5SDimitry Andric Out << "\", hash: ("; 28800b57cec5SDimitry Andric FieldSeparator FS; 28810b57cec5SDimitry Andric for (auto Hash : ModPair.second) 28820b57cec5SDimitry Andric Out << FS << Hash; 28830b57cec5SDimitry Andric Out << "))\n"; 28840b57cec5SDimitry Andric } 28850b57cec5SDimitry Andric 28860b57cec5SDimitry Andric // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer 28870b57cec5SDimitry Andric // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID). 28880b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) { 28890b57cec5SDimitry Andric auto GUID = GlobalList.first; 28900b57cec5SDimitry Andric for (auto &Summary : GlobalList.second.SummaryList) 28910b57cec5SDimitry Andric SummaryToGUIDMap[Summary.get()] = GUID; 28920b57cec5SDimitry Andric } 28930b57cec5SDimitry Andric 28940b57cec5SDimitry Andric // Print the global value summary entries. 28950b57cec5SDimitry Andric for (auto &GlobalList : *TheIndex) { 28960b57cec5SDimitry Andric auto GUID = GlobalList.first; 28970b57cec5SDimitry Andric auto VI = TheIndex->getValueInfo(GlobalList); 28980b57cec5SDimitry Andric printSummaryInfo(Machine.getGUIDSlot(GUID), VI); 28990b57cec5SDimitry Andric } 29000b57cec5SDimitry Andric 29010b57cec5SDimitry Andric // Print the TypeIdMap entries. 2902fe6060f1SDimitry Andric for (const auto &TID : TheIndex->typeIds()) { 2903fe6060f1SDimitry Andric Out << "^" << Machine.getTypeIdSlot(TID.second.first) 2904fe6060f1SDimitry Andric << " = typeid: (name: \"" << TID.second.first << "\""; 2905fe6060f1SDimitry Andric printTypeIdSummary(TID.second.second); 2906fe6060f1SDimitry Andric Out << ") ; guid = " << TID.first << "\n"; 29070b57cec5SDimitry Andric } 29080b57cec5SDimitry Andric 29090b57cec5SDimitry Andric // Print the TypeIdCompatibleVtableMap entries. 29100b57cec5SDimitry Andric for (auto &TId : TheIndex->typeIdCompatibleVtableMap()) { 29110b57cec5SDimitry Andric auto GUID = GlobalValue::getGUID(TId.first); 29120b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(GUID) 29130b57cec5SDimitry Andric << " = typeidCompatibleVTable: (name: \"" << TId.first << "\""; 29140b57cec5SDimitry Andric printTypeIdCompatibleVtableSummary(TId.second); 29150b57cec5SDimitry Andric Out << ") ; guid = " << GUID << "\n"; 29160b57cec5SDimitry Andric } 29175ffd83dbSDimitry Andric 29185ffd83dbSDimitry Andric // Don't emit flags when it's not really needed (value is zero by default). 29195ffd83dbSDimitry Andric if (TheIndex->getFlags()) { 29205ffd83dbSDimitry Andric Out << "^" << NumSlots << " = flags: " << TheIndex->getFlags() << "\n"; 29215ffd83dbSDimitry Andric ++NumSlots; 29225ffd83dbSDimitry Andric } 29235ffd83dbSDimitry Andric 29245ffd83dbSDimitry Andric Out << "^" << NumSlots << " = blockcount: " << TheIndex->getBlockCount() 29255ffd83dbSDimitry Andric << "\n"; 29260b57cec5SDimitry Andric } 29270b57cec5SDimitry Andric 29280b57cec5SDimitry Andric static const char * 29290b57cec5SDimitry Andric getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) { 29300b57cec5SDimitry Andric switch (K) { 29310b57cec5SDimitry Andric case WholeProgramDevirtResolution::Indir: 29320b57cec5SDimitry Andric return "indir"; 29330b57cec5SDimitry Andric case WholeProgramDevirtResolution::SingleImpl: 29340b57cec5SDimitry Andric return "singleImpl"; 29350b57cec5SDimitry Andric case WholeProgramDevirtResolution::BranchFunnel: 29360b57cec5SDimitry Andric return "branchFunnel"; 29370b57cec5SDimitry Andric } 29380b57cec5SDimitry Andric llvm_unreachable("invalid WholeProgramDevirtResolution kind"); 29390b57cec5SDimitry Andric } 29400b57cec5SDimitry Andric 29410b57cec5SDimitry Andric static const char *getWholeProgDevirtResByArgKindName( 29420b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::Kind K) { 29430b57cec5SDimitry Andric switch (K) { 29440b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::Indir: 29450b57cec5SDimitry Andric return "indir"; 29460b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::UniformRetVal: 29470b57cec5SDimitry Andric return "uniformRetVal"; 29480b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::UniqueRetVal: 29490b57cec5SDimitry Andric return "uniqueRetVal"; 29500b57cec5SDimitry Andric case WholeProgramDevirtResolution::ByArg::VirtualConstProp: 29510b57cec5SDimitry Andric return "virtualConstProp"; 29520b57cec5SDimitry Andric } 29530b57cec5SDimitry Andric llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind"); 29540b57cec5SDimitry Andric } 29550b57cec5SDimitry Andric 29560b57cec5SDimitry Andric static const char *getTTResKindName(TypeTestResolution::Kind K) { 29570b57cec5SDimitry Andric switch (K) { 29585ffd83dbSDimitry Andric case TypeTestResolution::Unknown: 29595ffd83dbSDimitry Andric return "unknown"; 29600b57cec5SDimitry Andric case TypeTestResolution::Unsat: 29610b57cec5SDimitry Andric return "unsat"; 29620b57cec5SDimitry Andric case TypeTestResolution::ByteArray: 29630b57cec5SDimitry Andric return "byteArray"; 29640b57cec5SDimitry Andric case TypeTestResolution::Inline: 29650b57cec5SDimitry Andric return "inline"; 29660b57cec5SDimitry Andric case TypeTestResolution::Single: 29670b57cec5SDimitry Andric return "single"; 29680b57cec5SDimitry Andric case TypeTestResolution::AllOnes: 29690b57cec5SDimitry Andric return "allOnes"; 29700b57cec5SDimitry Andric } 29710b57cec5SDimitry Andric llvm_unreachable("invalid TypeTestResolution kind"); 29720b57cec5SDimitry Andric } 29730b57cec5SDimitry Andric 29740b57cec5SDimitry Andric void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) { 29750b57cec5SDimitry Andric Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind) 29760b57cec5SDimitry Andric << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth; 29770b57cec5SDimitry Andric 29780b57cec5SDimitry Andric // The following fields are only used if the target does not support the use 29790b57cec5SDimitry Andric // of absolute symbols to store constants. Print only if non-zero. 29800b57cec5SDimitry Andric if (TTRes.AlignLog2) 29810b57cec5SDimitry Andric Out << ", alignLog2: " << TTRes.AlignLog2; 29820b57cec5SDimitry Andric if (TTRes.SizeM1) 29830b57cec5SDimitry Andric Out << ", sizeM1: " << TTRes.SizeM1; 29840b57cec5SDimitry Andric if (TTRes.BitMask) 29850b57cec5SDimitry Andric // BitMask is uint8_t which causes it to print the corresponding char. 29860b57cec5SDimitry Andric Out << ", bitMask: " << (unsigned)TTRes.BitMask; 29870b57cec5SDimitry Andric if (TTRes.InlineBits) 29880b57cec5SDimitry Andric Out << ", inlineBits: " << TTRes.InlineBits; 29890b57cec5SDimitry Andric 29900b57cec5SDimitry Andric Out << ")"; 29910b57cec5SDimitry Andric } 29920b57cec5SDimitry Andric 29930b57cec5SDimitry Andric void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) { 29940b57cec5SDimitry Andric Out << ", summary: ("; 29950b57cec5SDimitry Andric printTypeTestResolution(TIS.TTRes); 29960b57cec5SDimitry Andric if (!TIS.WPDRes.empty()) { 29970b57cec5SDimitry Andric Out << ", wpdResolutions: ("; 29980b57cec5SDimitry Andric FieldSeparator FS; 29990b57cec5SDimitry Andric for (auto &WPDRes : TIS.WPDRes) { 30000b57cec5SDimitry Andric Out << FS; 30010b57cec5SDimitry Andric Out << "(offset: " << WPDRes.first << ", "; 30020b57cec5SDimitry Andric printWPDRes(WPDRes.second); 30030b57cec5SDimitry Andric Out << ")"; 30040b57cec5SDimitry Andric } 30050b57cec5SDimitry Andric Out << ")"; 30060b57cec5SDimitry Andric } 30070b57cec5SDimitry Andric Out << ")"; 30080b57cec5SDimitry Andric } 30090b57cec5SDimitry Andric 30100b57cec5SDimitry Andric void AssemblyWriter::printTypeIdCompatibleVtableSummary( 30110b57cec5SDimitry Andric const TypeIdCompatibleVtableInfo &TI) { 30120b57cec5SDimitry Andric Out << ", summary: ("; 30130b57cec5SDimitry Andric FieldSeparator FS; 30140b57cec5SDimitry Andric for (auto &P : TI) { 30150b57cec5SDimitry Andric Out << FS; 30160b57cec5SDimitry Andric Out << "(offset: " << P.AddressPointOffset << ", "; 30170b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(P.VTableVI.getGUID()); 30180b57cec5SDimitry Andric Out << ")"; 30190b57cec5SDimitry Andric } 30200b57cec5SDimitry Andric Out << ")"; 30210b57cec5SDimitry Andric } 30220b57cec5SDimitry Andric 30230b57cec5SDimitry Andric void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) { 30240b57cec5SDimitry Andric Out << "args: ("; 30250b57cec5SDimitry Andric FieldSeparator FS; 30260b57cec5SDimitry Andric for (auto arg : Args) { 30270b57cec5SDimitry Andric Out << FS; 30280b57cec5SDimitry Andric Out << arg; 30290b57cec5SDimitry Andric } 30300b57cec5SDimitry Andric Out << ")"; 30310b57cec5SDimitry Andric } 30320b57cec5SDimitry Andric 30330b57cec5SDimitry Andric void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) { 30340b57cec5SDimitry Andric Out << "wpdRes: (kind: "; 30350b57cec5SDimitry Andric Out << getWholeProgDevirtResKindName(WPDRes.TheKind); 30360b57cec5SDimitry Andric 30370b57cec5SDimitry Andric if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl) 30380b57cec5SDimitry Andric Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\""; 30390b57cec5SDimitry Andric 30400b57cec5SDimitry Andric if (!WPDRes.ResByArg.empty()) { 30410b57cec5SDimitry Andric Out << ", resByArg: ("; 30420b57cec5SDimitry Andric FieldSeparator FS; 30430b57cec5SDimitry Andric for (auto &ResByArg : WPDRes.ResByArg) { 30440b57cec5SDimitry Andric Out << FS; 30450b57cec5SDimitry Andric printArgs(ResByArg.first); 30460b57cec5SDimitry Andric Out << ", byArg: (kind: "; 30470b57cec5SDimitry Andric Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind); 30480b57cec5SDimitry Andric if (ResByArg.second.TheKind == 30490b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::UniformRetVal || 30500b57cec5SDimitry Andric ResByArg.second.TheKind == 30510b57cec5SDimitry Andric WholeProgramDevirtResolution::ByArg::UniqueRetVal) 30520b57cec5SDimitry Andric Out << ", info: " << ResByArg.second.Info; 30530b57cec5SDimitry Andric 30540b57cec5SDimitry Andric // The following fields are only used if the target does not support the 30550b57cec5SDimitry Andric // use of absolute symbols to store constants. Print only if non-zero. 30560b57cec5SDimitry Andric if (ResByArg.second.Byte || ResByArg.second.Bit) 30570b57cec5SDimitry Andric Out << ", byte: " << ResByArg.second.Byte 30580b57cec5SDimitry Andric << ", bit: " << ResByArg.second.Bit; 30590b57cec5SDimitry Andric 30600b57cec5SDimitry Andric Out << ")"; 30610b57cec5SDimitry Andric } 30620b57cec5SDimitry Andric Out << ")"; 30630b57cec5SDimitry Andric } 30640b57cec5SDimitry Andric Out << ")"; 30650b57cec5SDimitry Andric } 30660b57cec5SDimitry Andric 30670b57cec5SDimitry Andric static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) { 30680b57cec5SDimitry Andric switch (SK) { 30690b57cec5SDimitry Andric case GlobalValueSummary::AliasKind: 30700b57cec5SDimitry Andric return "alias"; 30710b57cec5SDimitry Andric case GlobalValueSummary::FunctionKind: 30720b57cec5SDimitry Andric return "function"; 30730b57cec5SDimitry Andric case GlobalValueSummary::GlobalVarKind: 30740b57cec5SDimitry Andric return "variable"; 30750b57cec5SDimitry Andric } 30760b57cec5SDimitry Andric llvm_unreachable("invalid summary kind"); 30770b57cec5SDimitry Andric } 30780b57cec5SDimitry Andric 30790b57cec5SDimitry Andric void AssemblyWriter::printAliasSummary(const AliasSummary *AS) { 30800b57cec5SDimitry Andric Out << ", aliasee: "; 30810b57cec5SDimitry Andric // The indexes emitted for distributed backends may not include the 30820b57cec5SDimitry Andric // aliasee summary (only if it is being imported directly). Handle 30830b57cec5SDimitry Andric // that case by just emitting "null" as the aliasee. 30840b57cec5SDimitry Andric if (AS->hasAliasee()) 30850b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]); 30860b57cec5SDimitry Andric else 30870b57cec5SDimitry Andric Out << "null"; 30880b57cec5SDimitry Andric } 30890b57cec5SDimitry Andric 30900b57cec5SDimitry Andric void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) { 30910b57cec5SDimitry Andric auto VTableFuncs = GS->vTableFuncs(); 30925ffd83dbSDimitry Andric Out << ", varFlags: (readonly: " << GS->VarFlags.MaybeReadOnly << ", " 30935ffd83dbSDimitry Andric << "writeonly: " << GS->VarFlags.MaybeWriteOnly << ", " 30945ffd83dbSDimitry Andric << "constant: " << GS->VarFlags.Constant; 30955ffd83dbSDimitry Andric if (!VTableFuncs.empty()) 30965ffd83dbSDimitry Andric Out << ", " 30975ffd83dbSDimitry Andric << "vcall_visibility: " << GS->VarFlags.VCallVisibility; 30985ffd83dbSDimitry Andric Out << ")"; 30995ffd83dbSDimitry Andric 31000b57cec5SDimitry Andric if (!VTableFuncs.empty()) { 31010b57cec5SDimitry Andric Out << ", vTableFuncs: ("; 31020b57cec5SDimitry Andric FieldSeparator FS; 31030b57cec5SDimitry Andric for (auto &P : VTableFuncs) { 31040b57cec5SDimitry Andric Out << FS; 31050b57cec5SDimitry Andric Out << "(virtFunc: ^" << Machine.getGUIDSlot(P.FuncVI.getGUID()) 31060b57cec5SDimitry Andric << ", offset: " << P.VTableOffset; 31070b57cec5SDimitry Andric Out << ")"; 31080b57cec5SDimitry Andric } 31090b57cec5SDimitry Andric Out << ")"; 31100b57cec5SDimitry Andric } 31110b57cec5SDimitry Andric } 31120b57cec5SDimitry Andric 31130b57cec5SDimitry Andric static std::string getLinkageName(GlobalValue::LinkageTypes LT) { 31140b57cec5SDimitry Andric switch (LT) { 31150b57cec5SDimitry Andric case GlobalValue::ExternalLinkage: 31160b57cec5SDimitry Andric return "external"; 31170b57cec5SDimitry Andric case GlobalValue::PrivateLinkage: 31180b57cec5SDimitry Andric return "private"; 31190b57cec5SDimitry Andric case GlobalValue::InternalLinkage: 31200b57cec5SDimitry Andric return "internal"; 31210b57cec5SDimitry Andric case GlobalValue::LinkOnceAnyLinkage: 31220b57cec5SDimitry Andric return "linkonce"; 31230b57cec5SDimitry Andric case GlobalValue::LinkOnceODRLinkage: 31240b57cec5SDimitry Andric return "linkonce_odr"; 31250b57cec5SDimitry Andric case GlobalValue::WeakAnyLinkage: 31260b57cec5SDimitry Andric return "weak"; 31270b57cec5SDimitry Andric case GlobalValue::WeakODRLinkage: 31280b57cec5SDimitry Andric return "weak_odr"; 31290b57cec5SDimitry Andric case GlobalValue::CommonLinkage: 31300b57cec5SDimitry Andric return "common"; 31310b57cec5SDimitry Andric case GlobalValue::AppendingLinkage: 31320b57cec5SDimitry Andric return "appending"; 31330b57cec5SDimitry Andric case GlobalValue::ExternalWeakLinkage: 31340b57cec5SDimitry Andric return "extern_weak"; 31350b57cec5SDimitry Andric case GlobalValue::AvailableExternallyLinkage: 31360b57cec5SDimitry Andric return "available_externally"; 31370b57cec5SDimitry Andric } 31380b57cec5SDimitry Andric llvm_unreachable("invalid linkage"); 31390b57cec5SDimitry Andric } 31400b57cec5SDimitry Andric 31410b57cec5SDimitry Andric // When printing the linkage types in IR where the ExternalLinkage is 31420b57cec5SDimitry Andric // not printed, and other linkage types are expected to be printed with 31430b57cec5SDimitry Andric // a space after the name. 31440b57cec5SDimitry Andric static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) { 31450b57cec5SDimitry Andric if (LT == GlobalValue::ExternalLinkage) 31460b57cec5SDimitry Andric return ""; 31470b57cec5SDimitry Andric return getLinkageName(LT) + " "; 31480b57cec5SDimitry Andric } 31490b57cec5SDimitry Andric 3150fe6060f1SDimitry Andric static const char *getVisibilityName(GlobalValue::VisibilityTypes Vis) { 3151fe6060f1SDimitry Andric switch (Vis) { 3152fe6060f1SDimitry Andric case GlobalValue::DefaultVisibility: 3153fe6060f1SDimitry Andric return "default"; 3154fe6060f1SDimitry Andric case GlobalValue::HiddenVisibility: 3155fe6060f1SDimitry Andric return "hidden"; 3156fe6060f1SDimitry Andric case GlobalValue::ProtectedVisibility: 3157fe6060f1SDimitry Andric return "protected"; 3158fe6060f1SDimitry Andric } 3159fe6060f1SDimitry Andric llvm_unreachable("invalid visibility"); 3160fe6060f1SDimitry Andric } 3161fe6060f1SDimitry Andric 31620b57cec5SDimitry Andric void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) { 31630b57cec5SDimitry Andric Out << ", insts: " << FS->instCount(); 3164349cc55cSDimitry Andric if (FS->fflags().anyFlagSet()) 3165349cc55cSDimitry Andric Out << ", " << FS->fflags(); 31660b57cec5SDimitry Andric 31670b57cec5SDimitry Andric if (!FS->calls().empty()) { 31680b57cec5SDimitry Andric Out << ", calls: ("; 31690b57cec5SDimitry Andric FieldSeparator IFS; 31700b57cec5SDimitry Andric for (auto &Call : FS->calls()) { 31710b57cec5SDimitry Andric Out << IFS; 31720b57cec5SDimitry Andric Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID()); 31730b57cec5SDimitry Andric if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown) 31740b57cec5SDimitry Andric Out << ", hotness: " << getHotnessName(Call.second.getHotness()); 31750b57cec5SDimitry Andric else if (Call.second.RelBlockFreq) 31760b57cec5SDimitry Andric Out << ", relbf: " << Call.second.RelBlockFreq; 31770b57cec5SDimitry Andric Out << ")"; 31780b57cec5SDimitry Andric } 31790b57cec5SDimitry Andric Out << ")"; 31800b57cec5SDimitry Andric } 31810b57cec5SDimitry Andric 31820b57cec5SDimitry Andric if (const auto *TIdInfo = FS->getTypeIdInfo()) 31830b57cec5SDimitry Andric printTypeIdInfo(*TIdInfo); 31845ffd83dbSDimitry Andric 31855ffd83dbSDimitry Andric auto PrintRange = [&](const ConstantRange &Range) { 3186e8d8bef9SDimitry Andric Out << "[" << Range.getSignedMin() << ", " << Range.getSignedMax() << "]"; 31875ffd83dbSDimitry Andric }; 31885ffd83dbSDimitry Andric 31895ffd83dbSDimitry Andric if (!FS->paramAccesses().empty()) { 31905ffd83dbSDimitry Andric Out << ", params: ("; 31915ffd83dbSDimitry Andric FieldSeparator IFS; 31925ffd83dbSDimitry Andric for (auto &PS : FS->paramAccesses()) { 31935ffd83dbSDimitry Andric Out << IFS; 31945ffd83dbSDimitry Andric Out << "(param: " << PS.ParamNo; 31955ffd83dbSDimitry Andric Out << ", offset: "; 31965ffd83dbSDimitry Andric PrintRange(PS.Use); 31975ffd83dbSDimitry Andric if (!PS.Calls.empty()) { 31985ffd83dbSDimitry Andric Out << ", calls: ("; 31995ffd83dbSDimitry Andric FieldSeparator IFS; 32005ffd83dbSDimitry Andric for (auto &Call : PS.Calls) { 32015ffd83dbSDimitry Andric Out << IFS; 3202e8d8bef9SDimitry Andric Out << "(callee: ^" << Machine.getGUIDSlot(Call.Callee.getGUID()); 32035ffd83dbSDimitry Andric Out << ", param: " << Call.ParamNo; 32045ffd83dbSDimitry Andric Out << ", offset: "; 32055ffd83dbSDimitry Andric PrintRange(Call.Offsets); 32065ffd83dbSDimitry Andric Out << ")"; 32075ffd83dbSDimitry Andric } 32085ffd83dbSDimitry Andric Out << ")"; 32095ffd83dbSDimitry Andric } 32105ffd83dbSDimitry Andric Out << ")"; 32115ffd83dbSDimitry Andric } 32125ffd83dbSDimitry Andric Out << ")"; 32135ffd83dbSDimitry Andric } 32140b57cec5SDimitry Andric } 32150b57cec5SDimitry Andric 32160b57cec5SDimitry Andric void AssemblyWriter::printTypeIdInfo( 32170b57cec5SDimitry Andric const FunctionSummary::TypeIdInfo &TIDInfo) { 32180b57cec5SDimitry Andric Out << ", typeIdInfo: ("; 32190b57cec5SDimitry Andric FieldSeparator TIDFS; 32200b57cec5SDimitry Andric if (!TIDInfo.TypeTests.empty()) { 32210b57cec5SDimitry Andric Out << TIDFS; 32220b57cec5SDimitry Andric Out << "typeTests: ("; 32230b57cec5SDimitry Andric FieldSeparator FS; 32240b57cec5SDimitry Andric for (auto &GUID : TIDInfo.TypeTests) { 32250b57cec5SDimitry Andric auto TidIter = TheIndex->typeIds().equal_range(GUID); 32260b57cec5SDimitry Andric if (TidIter.first == TidIter.second) { 32270b57cec5SDimitry Andric Out << FS; 32280b57cec5SDimitry Andric Out << GUID; 32290b57cec5SDimitry Andric continue; 32300b57cec5SDimitry Andric } 32310b57cec5SDimitry Andric // Print all type id that correspond to this GUID. 32320b57cec5SDimitry Andric for (auto It = TidIter.first; It != TidIter.second; ++It) { 32330b57cec5SDimitry Andric Out << FS; 32340b57cec5SDimitry Andric auto Slot = Machine.getTypeIdSlot(It->second.first); 32350b57cec5SDimitry Andric assert(Slot != -1); 32360b57cec5SDimitry Andric Out << "^" << Slot; 32370b57cec5SDimitry Andric } 32380b57cec5SDimitry Andric } 32390b57cec5SDimitry Andric Out << ")"; 32400b57cec5SDimitry Andric } 32410b57cec5SDimitry Andric if (!TIDInfo.TypeTestAssumeVCalls.empty()) { 32420b57cec5SDimitry Andric Out << TIDFS; 32430b57cec5SDimitry Andric printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls"); 32440b57cec5SDimitry Andric } 32450b57cec5SDimitry Andric if (!TIDInfo.TypeCheckedLoadVCalls.empty()) { 32460b57cec5SDimitry Andric Out << TIDFS; 32470b57cec5SDimitry Andric printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls"); 32480b57cec5SDimitry Andric } 32490b57cec5SDimitry Andric if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) { 32500b57cec5SDimitry Andric Out << TIDFS; 32510b57cec5SDimitry Andric printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls, 32520b57cec5SDimitry Andric "typeTestAssumeConstVCalls"); 32530b57cec5SDimitry Andric } 32540b57cec5SDimitry Andric if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) { 32550b57cec5SDimitry Andric Out << TIDFS; 32560b57cec5SDimitry Andric printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls, 32570b57cec5SDimitry Andric "typeCheckedLoadConstVCalls"); 32580b57cec5SDimitry Andric } 32590b57cec5SDimitry Andric Out << ")"; 32600b57cec5SDimitry Andric } 32610b57cec5SDimitry Andric 32620b57cec5SDimitry Andric void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) { 32630b57cec5SDimitry Andric auto TidIter = TheIndex->typeIds().equal_range(VFId.GUID); 32640b57cec5SDimitry Andric if (TidIter.first == TidIter.second) { 32650b57cec5SDimitry Andric Out << "vFuncId: ("; 32660b57cec5SDimitry Andric Out << "guid: " << VFId.GUID; 32670b57cec5SDimitry Andric Out << ", offset: " << VFId.Offset; 32680b57cec5SDimitry Andric Out << ")"; 32690b57cec5SDimitry Andric return; 32700b57cec5SDimitry Andric } 32710b57cec5SDimitry Andric // Print all type id that correspond to this GUID. 32720b57cec5SDimitry Andric FieldSeparator FS; 32730b57cec5SDimitry Andric for (auto It = TidIter.first; It != TidIter.second; ++It) { 32740b57cec5SDimitry Andric Out << FS; 32750b57cec5SDimitry Andric Out << "vFuncId: ("; 32760b57cec5SDimitry Andric auto Slot = Machine.getTypeIdSlot(It->second.first); 32770b57cec5SDimitry Andric assert(Slot != -1); 32780b57cec5SDimitry Andric Out << "^" << Slot; 32790b57cec5SDimitry Andric Out << ", offset: " << VFId.Offset; 32800b57cec5SDimitry Andric Out << ")"; 32810b57cec5SDimitry Andric } 32820b57cec5SDimitry Andric } 32830b57cec5SDimitry Andric 32840b57cec5SDimitry Andric void AssemblyWriter::printNonConstVCalls( 32855ffd83dbSDimitry Andric const std::vector<FunctionSummary::VFuncId> &VCallList, const char *Tag) { 32860b57cec5SDimitry Andric Out << Tag << ": ("; 32870b57cec5SDimitry Andric FieldSeparator FS; 32880b57cec5SDimitry Andric for (auto &VFuncId : VCallList) { 32890b57cec5SDimitry Andric Out << FS; 32900b57cec5SDimitry Andric printVFuncId(VFuncId); 32910b57cec5SDimitry Andric } 32920b57cec5SDimitry Andric Out << ")"; 32930b57cec5SDimitry Andric } 32940b57cec5SDimitry Andric 32950b57cec5SDimitry Andric void AssemblyWriter::printConstVCalls( 32965ffd83dbSDimitry Andric const std::vector<FunctionSummary::ConstVCall> &VCallList, 32975ffd83dbSDimitry Andric const char *Tag) { 32980b57cec5SDimitry Andric Out << Tag << ": ("; 32990b57cec5SDimitry Andric FieldSeparator FS; 33000b57cec5SDimitry Andric for (auto &ConstVCall : VCallList) { 33010b57cec5SDimitry Andric Out << FS; 33020b57cec5SDimitry Andric Out << "("; 33030b57cec5SDimitry Andric printVFuncId(ConstVCall.VFunc); 33040b57cec5SDimitry Andric if (!ConstVCall.Args.empty()) { 33050b57cec5SDimitry Andric Out << ", "; 33060b57cec5SDimitry Andric printArgs(ConstVCall.Args); 33070b57cec5SDimitry Andric } 33080b57cec5SDimitry Andric Out << ")"; 33090b57cec5SDimitry Andric } 33100b57cec5SDimitry Andric Out << ")"; 33110b57cec5SDimitry Andric } 33120b57cec5SDimitry Andric 33130b57cec5SDimitry Andric void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) { 33140b57cec5SDimitry Andric GlobalValueSummary::GVFlags GVFlags = Summary.flags(); 33150b57cec5SDimitry Andric GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage; 33160b57cec5SDimitry Andric Out << getSummaryKindName(Summary.getSummaryKind()) << ": "; 33170b57cec5SDimitry Andric Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath()) 33180b57cec5SDimitry Andric << ", flags: ("; 33190b57cec5SDimitry Andric Out << "linkage: " << getLinkageName(LT); 3320fe6060f1SDimitry Andric Out << ", visibility: " 3321fe6060f1SDimitry Andric << getVisibilityName((GlobalValue::VisibilityTypes)GVFlags.Visibility); 33220b57cec5SDimitry Andric Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport; 33230b57cec5SDimitry Andric Out << ", live: " << GVFlags.Live; 33240b57cec5SDimitry Andric Out << ", dsoLocal: " << GVFlags.DSOLocal; 33250b57cec5SDimitry Andric Out << ", canAutoHide: " << GVFlags.CanAutoHide; 33260b57cec5SDimitry Andric Out << ")"; 33270b57cec5SDimitry Andric 33280b57cec5SDimitry Andric if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind) 33290b57cec5SDimitry Andric printAliasSummary(cast<AliasSummary>(&Summary)); 33300b57cec5SDimitry Andric else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind) 33310b57cec5SDimitry Andric printFunctionSummary(cast<FunctionSummary>(&Summary)); 33320b57cec5SDimitry Andric else 33330b57cec5SDimitry Andric printGlobalVarSummary(cast<GlobalVarSummary>(&Summary)); 33340b57cec5SDimitry Andric 33350b57cec5SDimitry Andric auto RefList = Summary.refs(); 33360b57cec5SDimitry Andric if (!RefList.empty()) { 33370b57cec5SDimitry Andric Out << ", refs: ("; 33380b57cec5SDimitry Andric FieldSeparator FS; 33390b57cec5SDimitry Andric for (auto &Ref : RefList) { 33400b57cec5SDimitry Andric Out << FS; 33410b57cec5SDimitry Andric if (Ref.isReadOnly()) 33420b57cec5SDimitry Andric Out << "readonly "; 33430b57cec5SDimitry Andric else if (Ref.isWriteOnly()) 33440b57cec5SDimitry Andric Out << "writeonly "; 33450b57cec5SDimitry Andric Out << "^" << Machine.getGUIDSlot(Ref.getGUID()); 33460b57cec5SDimitry Andric } 33470b57cec5SDimitry Andric Out << ")"; 33480b57cec5SDimitry Andric } 33490b57cec5SDimitry Andric 33500b57cec5SDimitry Andric Out << ")"; 33510b57cec5SDimitry Andric } 33520b57cec5SDimitry Andric 33530b57cec5SDimitry Andric void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) { 33540b57cec5SDimitry Andric Out << "^" << Slot << " = gv: ("; 33550b57cec5SDimitry Andric if (!VI.name().empty()) 33560b57cec5SDimitry Andric Out << "name: \"" << VI.name() << "\""; 33570b57cec5SDimitry Andric else 33580b57cec5SDimitry Andric Out << "guid: " << VI.getGUID(); 33590b57cec5SDimitry Andric if (!VI.getSummaryList().empty()) { 33600b57cec5SDimitry Andric Out << ", summaries: ("; 33610b57cec5SDimitry Andric FieldSeparator FS; 33620b57cec5SDimitry Andric for (auto &Summary : VI.getSummaryList()) { 33630b57cec5SDimitry Andric Out << FS; 33640b57cec5SDimitry Andric printSummary(*Summary); 33650b57cec5SDimitry Andric } 33660b57cec5SDimitry Andric Out << ")"; 33670b57cec5SDimitry Andric } 33680b57cec5SDimitry Andric Out << ")"; 33690b57cec5SDimitry Andric if (!VI.name().empty()) 33700b57cec5SDimitry Andric Out << " ; guid = " << VI.getGUID(); 33710b57cec5SDimitry Andric Out << "\n"; 33720b57cec5SDimitry Andric } 33730b57cec5SDimitry Andric 33740b57cec5SDimitry Andric static void printMetadataIdentifier(StringRef Name, 33750b57cec5SDimitry Andric formatted_raw_ostream &Out) { 33760b57cec5SDimitry Andric if (Name.empty()) { 33770b57cec5SDimitry Andric Out << "<empty name> "; 33780b57cec5SDimitry Andric } else { 33790b57cec5SDimitry Andric if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' || 33800b57cec5SDimitry Andric Name[0] == '$' || Name[0] == '.' || Name[0] == '_') 33810b57cec5SDimitry Andric Out << Name[0]; 33820b57cec5SDimitry Andric else 33830b57cec5SDimitry Andric Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F); 33840b57cec5SDimitry Andric for (unsigned i = 1, e = Name.size(); i != e; ++i) { 33850b57cec5SDimitry Andric unsigned char C = Name[i]; 33860b57cec5SDimitry Andric if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' || 33870b57cec5SDimitry Andric C == '.' || C == '_') 33880b57cec5SDimitry Andric Out << C; 33890b57cec5SDimitry Andric else 33900b57cec5SDimitry Andric Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F); 33910b57cec5SDimitry Andric } 33920b57cec5SDimitry Andric } 33930b57cec5SDimitry Andric } 33940b57cec5SDimitry Andric 33950b57cec5SDimitry Andric void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) { 33960b57cec5SDimitry Andric Out << '!'; 33970b57cec5SDimitry Andric printMetadataIdentifier(NMD->getName(), Out); 33980b57cec5SDimitry Andric Out << " = !{"; 33990b57cec5SDimitry Andric for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) { 34000b57cec5SDimitry Andric if (i) 34010b57cec5SDimitry Andric Out << ", "; 34020b57cec5SDimitry Andric 34030b57cec5SDimitry Andric // Write DIExpressions inline. 34040b57cec5SDimitry Andric // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose. 34050b57cec5SDimitry Andric MDNode *Op = NMD->getOperand(i); 3406fe6060f1SDimitry Andric assert(!isa<DIArgList>(Op) && 3407fe6060f1SDimitry Andric "DIArgLists should not appear in NamedMDNodes"); 34080b57cec5SDimitry Andric if (auto *Expr = dyn_cast<DIExpression>(Op)) { 3409349cc55cSDimitry Andric writeDIExpression(Out, Expr, AsmWriterContext::getEmpty()); 34100b57cec5SDimitry Andric continue; 34110b57cec5SDimitry Andric } 34120b57cec5SDimitry Andric 34130b57cec5SDimitry Andric int Slot = Machine.getMetadataSlot(Op); 34140b57cec5SDimitry Andric if (Slot == -1) 34150b57cec5SDimitry Andric Out << "<badref>"; 34160b57cec5SDimitry Andric else 34170b57cec5SDimitry Andric Out << '!' << Slot; 34180b57cec5SDimitry Andric } 34190b57cec5SDimitry Andric Out << "}\n"; 34200b57cec5SDimitry Andric } 34210b57cec5SDimitry Andric 34220b57cec5SDimitry Andric static void PrintVisibility(GlobalValue::VisibilityTypes Vis, 34230b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34240b57cec5SDimitry Andric switch (Vis) { 34250b57cec5SDimitry Andric case GlobalValue::DefaultVisibility: break; 34260b57cec5SDimitry Andric case GlobalValue::HiddenVisibility: Out << "hidden "; break; 34270b57cec5SDimitry Andric case GlobalValue::ProtectedVisibility: Out << "protected "; break; 34280b57cec5SDimitry Andric } 34290b57cec5SDimitry Andric } 34300b57cec5SDimitry Andric 34310b57cec5SDimitry Andric static void PrintDSOLocation(const GlobalValue &GV, 34320b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34335ffd83dbSDimitry Andric if (GV.isDSOLocal() && !GV.isImplicitDSOLocal()) 34340b57cec5SDimitry Andric Out << "dso_local "; 34350b57cec5SDimitry Andric } 34360b57cec5SDimitry Andric 34370b57cec5SDimitry Andric static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT, 34380b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34390b57cec5SDimitry Andric switch (SCT) { 34400b57cec5SDimitry Andric case GlobalValue::DefaultStorageClass: break; 34410b57cec5SDimitry Andric case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break; 34420b57cec5SDimitry Andric case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break; 34430b57cec5SDimitry Andric } 34440b57cec5SDimitry Andric } 34450b57cec5SDimitry Andric 34460b57cec5SDimitry Andric static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM, 34470b57cec5SDimitry Andric formatted_raw_ostream &Out) { 34480b57cec5SDimitry Andric switch (TLM) { 34490b57cec5SDimitry Andric case GlobalVariable::NotThreadLocal: 34500b57cec5SDimitry Andric break; 34510b57cec5SDimitry Andric case GlobalVariable::GeneralDynamicTLSModel: 34520b57cec5SDimitry Andric Out << "thread_local "; 34530b57cec5SDimitry Andric break; 34540b57cec5SDimitry Andric case GlobalVariable::LocalDynamicTLSModel: 34550b57cec5SDimitry Andric Out << "thread_local(localdynamic) "; 34560b57cec5SDimitry Andric break; 34570b57cec5SDimitry Andric case GlobalVariable::InitialExecTLSModel: 34580b57cec5SDimitry Andric Out << "thread_local(initialexec) "; 34590b57cec5SDimitry Andric break; 34600b57cec5SDimitry Andric case GlobalVariable::LocalExecTLSModel: 34610b57cec5SDimitry Andric Out << "thread_local(localexec) "; 34620b57cec5SDimitry Andric break; 34630b57cec5SDimitry Andric } 34640b57cec5SDimitry Andric } 34650b57cec5SDimitry Andric 34660b57cec5SDimitry Andric static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) { 34670b57cec5SDimitry Andric switch (UA) { 34680b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::None: 34690b57cec5SDimitry Andric return ""; 34700b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::Local: 34710b57cec5SDimitry Andric return "local_unnamed_addr"; 34720b57cec5SDimitry Andric case GlobalVariable::UnnamedAddr::Global: 34730b57cec5SDimitry Andric return "unnamed_addr"; 34740b57cec5SDimitry Andric } 34750b57cec5SDimitry Andric llvm_unreachable("Unknown UnnamedAddr"); 34760b57cec5SDimitry Andric } 34770b57cec5SDimitry Andric 34780b57cec5SDimitry Andric static void maybePrintComdat(formatted_raw_ostream &Out, 34790b57cec5SDimitry Andric const GlobalObject &GO) { 34800b57cec5SDimitry Andric const Comdat *C = GO.getComdat(); 34810b57cec5SDimitry Andric if (!C) 34820b57cec5SDimitry Andric return; 34830b57cec5SDimitry Andric 34840b57cec5SDimitry Andric if (isa<GlobalVariable>(GO)) 34850b57cec5SDimitry Andric Out << ','; 34860b57cec5SDimitry Andric Out << " comdat"; 34870b57cec5SDimitry Andric 34880b57cec5SDimitry Andric if (GO.getName() == C->getName()) 34890b57cec5SDimitry Andric return; 34900b57cec5SDimitry Andric 34910b57cec5SDimitry Andric Out << '('; 34920b57cec5SDimitry Andric PrintLLVMName(Out, C->getName(), ComdatPrefix); 34930b57cec5SDimitry Andric Out << ')'; 34940b57cec5SDimitry Andric } 34950b57cec5SDimitry Andric 34960b57cec5SDimitry Andric void AssemblyWriter::printGlobal(const GlobalVariable *GV) { 34970b57cec5SDimitry Andric if (GV->isMaterializable()) 34980b57cec5SDimitry Andric Out << "; Materializable\n"; 34990b57cec5SDimitry Andric 3500349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GV->getParent()); 3501349cc55cSDimitry Andric WriteAsOperandInternal(Out, GV, WriterCtx); 35020b57cec5SDimitry Andric Out << " = "; 35030b57cec5SDimitry Andric 35040b57cec5SDimitry Andric if (!GV->hasInitializer() && GV->hasExternalLinkage()) 35050b57cec5SDimitry Andric Out << "external "; 35060b57cec5SDimitry Andric 35070b57cec5SDimitry Andric Out << getLinkageNameWithSpace(GV->getLinkage()); 35080b57cec5SDimitry Andric PrintDSOLocation(*GV, Out); 35090b57cec5SDimitry Andric PrintVisibility(GV->getVisibility(), Out); 35100b57cec5SDimitry Andric PrintDLLStorageClass(GV->getDLLStorageClass(), Out); 35110b57cec5SDimitry Andric PrintThreadLocalModel(GV->getThreadLocalMode(), Out); 35120b57cec5SDimitry Andric StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr()); 35130b57cec5SDimitry Andric if (!UA.empty()) 35140b57cec5SDimitry Andric Out << UA << ' '; 35150b57cec5SDimitry Andric 35160b57cec5SDimitry Andric if (unsigned AddressSpace = GV->getType()->getAddressSpace()) 35170b57cec5SDimitry Andric Out << "addrspace(" << AddressSpace << ") "; 35180b57cec5SDimitry Andric if (GV->isExternallyInitialized()) Out << "externally_initialized "; 35190b57cec5SDimitry Andric Out << (GV->isConstant() ? "constant " : "global "); 35200b57cec5SDimitry Andric TypePrinter.print(GV->getValueType(), Out); 35210b57cec5SDimitry Andric 35220b57cec5SDimitry Andric if (GV->hasInitializer()) { 35230b57cec5SDimitry Andric Out << ' '; 35240b57cec5SDimitry Andric writeOperand(GV->getInitializer(), false); 35250b57cec5SDimitry Andric } 35260b57cec5SDimitry Andric 35270b57cec5SDimitry Andric if (GV->hasSection()) { 35280b57cec5SDimitry Andric Out << ", section \""; 35290b57cec5SDimitry Andric printEscapedString(GV->getSection(), Out); 35300b57cec5SDimitry Andric Out << '"'; 35310b57cec5SDimitry Andric } 35320b57cec5SDimitry Andric if (GV->hasPartition()) { 35330b57cec5SDimitry Andric Out << ", partition \""; 35340b57cec5SDimitry Andric printEscapedString(GV->getPartition(), Out); 35350b57cec5SDimitry Andric Out << '"'; 35360b57cec5SDimitry Andric } 35370b57cec5SDimitry Andric 3538*81ad6265SDimitry Andric using SanitizerMetadata = llvm::GlobalValue::SanitizerMetadata; 3539*81ad6265SDimitry Andric if (GV->hasSanitizerMetadata()) { 3540*81ad6265SDimitry Andric SanitizerMetadata MD = GV->getSanitizerMetadata(); 3541*81ad6265SDimitry Andric if (MD.NoAddress) 3542*81ad6265SDimitry Andric Out << ", no_sanitize_address"; 3543*81ad6265SDimitry Andric if (MD.NoHWAddress) 3544*81ad6265SDimitry Andric Out << ", no_sanitize_hwaddress"; 3545*81ad6265SDimitry Andric if (MD.NoMemtag) 3546*81ad6265SDimitry Andric Out << ", no_sanitize_memtag"; 3547*81ad6265SDimitry Andric if (MD.IsDynInit) 3548*81ad6265SDimitry Andric Out << ", sanitize_address_dyninit"; 3549*81ad6265SDimitry Andric } 3550*81ad6265SDimitry Andric 35510b57cec5SDimitry Andric maybePrintComdat(Out, *GV); 35520eae32dcSDimitry Andric if (MaybeAlign A = GV->getAlign()) 35530eae32dcSDimitry Andric Out << ", align " << A->value(); 35540b57cec5SDimitry Andric 35550b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 35560b57cec5SDimitry Andric GV->getAllMetadata(MDs); 35570b57cec5SDimitry Andric printMetadataAttachments(MDs, ", "); 35580b57cec5SDimitry Andric 35590b57cec5SDimitry Andric auto Attrs = GV->getAttributes(); 35600b57cec5SDimitry Andric if (Attrs.hasAttributes()) 35610b57cec5SDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(Attrs); 35620b57cec5SDimitry Andric 35630b57cec5SDimitry Andric printInfoComment(*GV); 35640b57cec5SDimitry Andric } 35650b57cec5SDimitry Andric 3566349cc55cSDimitry Andric void AssemblyWriter::printAlias(const GlobalAlias *GA) { 3567349cc55cSDimitry Andric if (GA->isMaterializable()) 35680b57cec5SDimitry Andric Out << "; Materializable\n"; 35690b57cec5SDimitry Andric 3570349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GA->getParent()); 3571349cc55cSDimitry Andric WriteAsOperandInternal(Out, GA, WriterCtx); 35720b57cec5SDimitry Andric Out << " = "; 35730b57cec5SDimitry Andric 3574349cc55cSDimitry Andric Out << getLinkageNameWithSpace(GA->getLinkage()); 3575349cc55cSDimitry Andric PrintDSOLocation(*GA, Out); 3576349cc55cSDimitry Andric PrintVisibility(GA->getVisibility(), Out); 3577349cc55cSDimitry Andric PrintDLLStorageClass(GA->getDLLStorageClass(), Out); 3578349cc55cSDimitry Andric PrintThreadLocalModel(GA->getThreadLocalMode(), Out); 3579349cc55cSDimitry Andric StringRef UA = getUnnamedAddrEncoding(GA->getUnnamedAddr()); 35800b57cec5SDimitry Andric if (!UA.empty()) 35810b57cec5SDimitry Andric Out << UA << ' '; 35820b57cec5SDimitry Andric 35830b57cec5SDimitry Andric Out << "alias "; 35840b57cec5SDimitry Andric 3585349cc55cSDimitry Andric TypePrinter.print(GA->getValueType(), Out); 35860b57cec5SDimitry Andric Out << ", "; 35870b57cec5SDimitry Andric 3588349cc55cSDimitry Andric if (const Constant *Aliasee = GA->getAliasee()) { 3589349cc55cSDimitry Andric writeOperand(Aliasee, !isa<ConstantExpr>(Aliasee)); 35900b57cec5SDimitry Andric } else { 3591349cc55cSDimitry Andric TypePrinter.print(GA->getType(), Out); 3592349cc55cSDimitry Andric Out << " <<NULL ALIASEE>>"; 35930b57cec5SDimitry Andric } 35940b57cec5SDimitry Andric 3595349cc55cSDimitry Andric if (GA->hasPartition()) { 35960b57cec5SDimitry Andric Out << ", partition \""; 3597349cc55cSDimitry Andric printEscapedString(GA->getPartition(), Out); 35980b57cec5SDimitry Andric Out << '"'; 35990b57cec5SDimitry Andric } 36000b57cec5SDimitry Andric 3601349cc55cSDimitry Andric printInfoComment(*GA); 3602349cc55cSDimitry Andric Out << '\n'; 3603349cc55cSDimitry Andric } 3604349cc55cSDimitry Andric 3605349cc55cSDimitry Andric void AssemblyWriter::printIFunc(const GlobalIFunc *GI) { 3606349cc55cSDimitry Andric if (GI->isMaterializable()) 3607349cc55cSDimitry Andric Out << "; Materializable\n"; 3608349cc55cSDimitry Andric 3609349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, GI->getParent()); 3610349cc55cSDimitry Andric WriteAsOperandInternal(Out, GI, WriterCtx); 3611349cc55cSDimitry Andric Out << " = "; 3612349cc55cSDimitry Andric 3613349cc55cSDimitry Andric Out << getLinkageNameWithSpace(GI->getLinkage()); 3614349cc55cSDimitry Andric PrintDSOLocation(*GI, Out); 3615349cc55cSDimitry Andric PrintVisibility(GI->getVisibility(), Out); 3616349cc55cSDimitry Andric 3617349cc55cSDimitry Andric Out << "ifunc "; 3618349cc55cSDimitry Andric 3619349cc55cSDimitry Andric TypePrinter.print(GI->getValueType(), Out); 3620349cc55cSDimitry Andric Out << ", "; 3621349cc55cSDimitry Andric 3622349cc55cSDimitry Andric if (const Constant *Resolver = GI->getResolver()) { 3623349cc55cSDimitry Andric writeOperand(Resolver, !isa<ConstantExpr>(Resolver)); 3624349cc55cSDimitry Andric } else { 3625349cc55cSDimitry Andric TypePrinter.print(GI->getType(), Out); 3626349cc55cSDimitry Andric Out << " <<NULL RESOLVER>>"; 3627349cc55cSDimitry Andric } 3628349cc55cSDimitry Andric 3629349cc55cSDimitry Andric if (GI->hasPartition()) { 3630349cc55cSDimitry Andric Out << ", partition \""; 3631349cc55cSDimitry Andric printEscapedString(GI->getPartition(), Out); 3632349cc55cSDimitry Andric Out << '"'; 3633349cc55cSDimitry Andric } 3634349cc55cSDimitry Andric 3635349cc55cSDimitry Andric printInfoComment(*GI); 36360b57cec5SDimitry Andric Out << '\n'; 36370b57cec5SDimitry Andric } 36380b57cec5SDimitry Andric 36390b57cec5SDimitry Andric void AssemblyWriter::printComdat(const Comdat *C) { 36400b57cec5SDimitry Andric C->print(Out); 36410b57cec5SDimitry Andric } 36420b57cec5SDimitry Andric 36430b57cec5SDimitry Andric void AssemblyWriter::printTypeIdentities() { 36440b57cec5SDimitry Andric if (TypePrinter.empty()) 36450b57cec5SDimitry Andric return; 36460b57cec5SDimitry Andric 36470b57cec5SDimitry Andric Out << '\n'; 36480b57cec5SDimitry Andric 36490b57cec5SDimitry Andric // Emit all numbered types. 36500b57cec5SDimitry Andric auto &NumberedTypes = TypePrinter.getNumberedTypes(); 36510b57cec5SDimitry Andric for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) { 36520b57cec5SDimitry Andric Out << '%' << I << " = type "; 36530b57cec5SDimitry Andric 36540b57cec5SDimitry Andric // Make sure we print out at least one level of the type structure, so 36550b57cec5SDimitry Andric // that we do not get %2 = type %2 36560b57cec5SDimitry Andric TypePrinter.printStructBody(NumberedTypes[I], Out); 36570b57cec5SDimitry Andric Out << '\n'; 36580b57cec5SDimitry Andric } 36590b57cec5SDimitry Andric 36600b57cec5SDimitry Andric auto &NamedTypes = TypePrinter.getNamedTypes(); 36610eae32dcSDimitry Andric for (StructType *NamedType : NamedTypes) { 36620eae32dcSDimitry Andric PrintLLVMName(Out, NamedType->getName(), LocalPrefix); 36630b57cec5SDimitry Andric Out << " = type "; 36640b57cec5SDimitry Andric 36650b57cec5SDimitry Andric // Make sure we print out at least one level of the type structure, so 36660b57cec5SDimitry Andric // that we do not get %FILE = type %FILE 36670eae32dcSDimitry Andric TypePrinter.printStructBody(NamedType, Out); 36680b57cec5SDimitry Andric Out << '\n'; 36690b57cec5SDimitry Andric } 36700b57cec5SDimitry Andric } 36710b57cec5SDimitry Andric 36720b57cec5SDimitry Andric /// printFunction - Print all aspects of a function. 36730b57cec5SDimitry Andric void AssemblyWriter::printFunction(const Function *F) { 36740b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out); 36750b57cec5SDimitry Andric 36760b57cec5SDimitry Andric if (F->isMaterializable()) 36770b57cec5SDimitry Andric Out << "; Materializable\n"; 36780b57cec5SDimitry Andric 36790b57cec5SDimitry Andric const AttributeList &Attrs = F->getAttributes(); 3680349cc55cSDimitry Andric if (Attrs.hasFnAttrs()) { 3681349cc55cSDimitry Andric AttributeSet AS = Attrs.getFnAttrs(); 36820b57cec5SDimitry Andric std::string AttrStr; 36830b57cec5SDimitry Andric 36840b57cec5SDimitry Andric for (const Attribute &Attr : AS) { 36850b57cec5SDimitry Andric if (!Attr.isStringAttribute()) { 36860b57cec5SDimitry Andric if (!AttrStr.empty()) AttrStr += ' '; 36870b57cec5SDimitry Andric AttrStr += Attr.getAsString(); 36880b57cec5SDimitry Andric } 36890b57cec5SDimitry Andric } 36900b57cec5SDimitry Andric 36910b57cec5SDimitry Andric if (!AttrStr.empty()) 36920b57cec5SDimitry Andric Out << "; Function Attrs: " << AttrStr << '\n'; 36930b57cec5SDimitry Andric } 36940b57cec5SDimitry Andric 36950b57cec5SDimitry Andric Machine.incorporateFunction(F); 36960b57cec5SDimitry Andric 36970b57cec5SDimitry Andric if (F->isDeclaration()) { 36980b57cec5SDimitry Andric Out << "declare"; 36990b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 37000b57cec5SDimitry Andric F->getAllMetadata(MDs); 37010b57cec5SDimitry Andric printMetadataAttachments(MDs, " "); 37020b57cec5SDimitry Andric Out << ' '; 37030b57cec5SDimitry Andric } else 37040b57cec5SDimitry Andric Out << "define "; 37050b57cec5SDimitry Andric 37060b57cec5SDimitry Andric Out << getLinkageNameWithSpace(F->getLinkage()); 37070b57cec5SDimitry Andric PrintDSOLocation(*F, Out); 37080b57cec5SDimitry Andric PrintVisibility(F->getVisibility(), Out); 37090b57cec5SDimitry Andric PrintDLLStorageClass(F->getDLLStorageClass(), Out); 37100b57cec5SDimitry Andric 37110b57cec5SDimitry Andric // Print the calling convention. 37120b57cec5SDimitry Andric if (F->getCallingConv() != CallingConv::C) { 37130b57cec5SDimitry Andric PrintCallingConv(F->getCallingConv(), Out); 37140b57cec5SDimitry Andric Out << " "; 37150b57cec5SDimitry Andric } 37160b57cec5SDimitry Andric 37170b57cec5SDimitry Andric FunctionType *FT = F->getFunctionType(); 3718349cc55cSDimitry Andric if (Attrs.hasRetAttrs()) 37190b57cec5SDimitry Andric Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' '; 37200b57cec5SDimitry Andric TypePrinter.print(F->getReturnType(), Out); 3721349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, &Machine, F->getParent()); 37220b57cec5SDimitry Andric Out << ' '; 3723349cc55cSDimitry Andric WriteAsOperandInternal(Out, F, WriterCtx); 37240b57cec5SDimitry Andric Out << '('; 37250b57cec5SDimitry Andric 37260b57cec5SDimitry Andric // Loop over the arguments, printing them... 37270b57cec5SDimitry Andric if (F->isDeclaration() && !IsForDebug) { 37280b57cec5SDimitry Andric // We're only interested in the type here - don't print argument names. 37290b57cec5SDimitry Andric for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) { 37300b57cec5SDimitry Andric // Insert commas as we go... the first arg doesn't get a comma 37310b57cec5SDimitry Andric if (I) 37320b57cec5SDimitry Andric Out << ", "; 37330b57cec5SDimitry Andric // Output type... 37340b57cec5SDimitry Andric TypePrinter.print(FT->getParamType(I), Out); 37350b57cec5SDimitry Andric 3736349cc55cSDimitry Andric AttributeSet ArgAttrs = Attrs.getParamAttrs(I); 3737480093f4SDimitry Andric if (ArgAttrs.hasAttributes()) { 3738480093f4SDimitry Andric Out << ' '; 3739480093f4SDimitry Andric writeAttributeSet(ArgAttrs); 3740480093f4SDimitry Andric } 37410b57cec5SDimitry Andric } 37420b57cec5SDimitry Andric } else { 37430b57cec5SDimitry Andric // The arguments are meaningful here, print them in detail. 37440b57cec5SDimitry Andric for (const Argument &Arg : F->args()) { 37450b57cec5SDimitry Andric // Insert commas as we go... the first arg doesn't get a comma 37460b57cec5SDimitry Andric if (Arg.getArgNo() != 0) 37470b57cec5SDimitry Andric Out << ", "; 3748349cc55cSDimitry Andric printArgument(&Arg, Attrs.getParamAttrs(Arg.getArgNo())); 37490b57cec5SDimitry Andric } 37500b57cec5SDimitry Andric } 37510b57cec5SDimitry Andric 37520b57cec5SDimitry Andric // Finish printing arguments... 37530b57cec5SDimitry Andric if (FT->isVarArg()) { 37540b57cec5SDimitry Andric if (FT->getNumParams()) Out << ", "; 37550b57cec5SDimitry Andric Out << "..."; // Output varargs portion of signature! 37560b57cec5SDimitry Andric } 37570b57cec5SDimitry Andric Out << ')'; 37580b57cec5SDimitry Andric StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr()); 37590b57cec5SDimitry Andric if (!UA.empty()) 37600b57cec5SDimitry Andric Out << ' ' << UA; 37610b57cec5SDimitry Andric // We print the function address space if it is non-zero or if we are writing 37620b57cec5SDimitry Andric // a module with a non-zero program address space or if there is no valid 37630b57cec5SDimitry Andric // Module* so that the file can be parsed without the datalayout string. 37640b57cec5SDimitry Andric const Module *Mod = F->getParent(); 37650b57cec5SDimitry Andric if (F->getAddressSpace() != 0 || !Mod || 37660b57cec5SDimitry Andric Mod->getDataLayout().getProgramAddressSpace() != 0) 37670b57cec5SDimitry Andric Out << " addrspace(" << F->getAddressSpace() << ")"; 3768349cc55cSDimitry Andric if (Attrs.hasFnAttrs()) 3769349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttrs()); 37700b57cec5SDimitry Andric if (F->hasSection()) { 37710b57cec5SDimitry Andric Out << " section \""; 37720b57cec5SDimitry Andric printEscapedString(F->getSection(), Out); 37730b57cec5SDimitry Andric Out << '"'; 37740b57cec5SDimitry Andric } 37750b57cec5SDimitry Andric if (F->hasPartition()) { 37760b57cec5SDimitry Andric Out << " partition \""; 37770b57cec5SDimitry Andric printEscapedString(F->getPartition(), Out); 37780b57cec5SDimitry Andric Out << '"'; 37790b57cec5SDimitry Andric } 37800b57cec5SDimitry Andric maybePrintComdat(Out, *F); 37810eae32dcSDimitry Andric if (MaybeAlign A = F->getAlign()) 37820eae32dcSDimitry Andric Out << " align " << A->value(); 37830b57cec5SDimitry Andric if (F->hasGC()) 37840b57cec5SDimitry Andric Out << " gc \"" << F->getGC() << '"'; 37850b57cec5SDimitry Andric if (F->hasPrefixData()) { 37860b57cec5SDimitry Andric Out << " prefix "; 37870b57cec5SDimitry Andric writeOperand(F->getPrefixData(), true); 37880b57cec5SDimitry Andric } 37890b57cec5SDimitry Andric if (F->hasPrologueData()) { 37900b57cec5SDimitry Andric Out << " prologue "; 37910b57cec5SDimitry Andric writeOperand(F->getPrologueData(), true); 37920b57cec5SDimitry Andric } 37930b57cec5SDimitry Andric if (F->hasPersonalityFn()) { 37940b57cec5SDimitry Andric Out << " personality "; 37950b57cec5SDimitry Andric writeOperand(F->getPersonalityFn(), /*PrintType=*/true); 37960b57cec5SDimitry Andric } 37970b57cec5SDimitry Andric 37980b57cec5SDimitry Andric if (F->isDeclaration()) { 37990b57cec5SDimitry Andric Out << '\n'; 38000b57cec5SDimitry Andric } else { 38010b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> MDs; 38020b57cec5SDimitry Andric F->getAllMetadata(MDs); 38030b57cec5SDimitry Andric printMetadataAttachments(MDs, " "); 38040b57cec5SDimitry Andric 38050b57cec5SDimitry Andric Out << " {"; 38060b57cec5SDimitry Andric // Output all of the function's basic blocks. 38070b57cec5SDimitry Andric for (const BasicBlock &BB : *F) 38080b57cec5SDimitry Andric printBasicBlock(&BB); 38090b57cec5SDimitry Andric 38100b57cec5SDimitry Andric // Output the function's use-lists. 38110b57cec5SDimitry Andric printUseLists(F); 38120b57cec5SDimitry Andric 38130b57cec5SDimitry Andric Out << "}\n"; 38140b57cec5SDimitry Andric } 38150b57cec5SDimitry Andric 38160b57cec5SDimitry Andric Machine.purgeFunction(); 38170b57cec5SDimitry Andric } 38180b57cec5SDimitry Andric 38190b57cec5SDimitry Andric /// printArgument - This member is called for every argument that is passed into 38200b57cec5SDimitry Andric /// the function. Simply print it out 38210b57cec5SDimitry Andric void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) { 38220b57cec5SDimitry Andric // Output type... 38230b57cec5SDimitry Andric TypePrinter.print(Arg->getType(), Out); 38240b57cec5SDimitry Andric 38250b57cec5SDimitry Andric // Output parameter attributes list 3826480093f4SDimitry Andric if (Attrs.hasAttributes()) { 3827480093f4SDimitry Andric Out << ' '; 3828480093f4SDimitry Andric writeAttributeSet(Attrs); 3829480093f4SDimitry Andric } 38300b57cec5SDimitry Andric 38310b57cec5SDimitry Andric // Output name, if available... 38320b57cec5SDimitry Andric if (Arg->hasName()) { 38330b57cec5SDimitry Andric Out << ' '; 38340b57cec5SDimitry Andric PrintLLVMName(Out, Arg); 38358bcb0991SDimitry Andric } else { 38368bcb0991SDimitry Andric int Slot = Machine.getLocalSlot(Arg); 38378bcb0991SDimitry Andric assert(Slot != -1 && "expect argument in function here"); 38388bcb0991SDimitry Andric Out << " %" << Slot; 38390b57cec5SDimitry Andric } 38400b57cec5SDimitry Andric } 38410b57cec5SDimitry Andric 38420b57cec5SDimitry Andric /// printBasicBlock - This member is called for each basic block in a method. 38430b57cec5SDimitry Andric void AssemblyWriter::printBasicBlock(const BasicBlock *BB) { 3844fe6060f1SDimitry Andric bool IsEntryBlock = BB->getParent() && BB->isEntryBlock(); 38450b57cec5SDimitry Andric if (BB->hasName()) { // Print out the label if it exists... 38460b57cec5SDimitry Andric Out << "\n"; 38470b57cec5SDimitry Andric PrintLLVMName(Out, BB->getName(), LabelPrefix); 38480b57cec5SDimitry Andric Out << ':'; 38490b57cec5SDimitry Andric } else if (!IsEntryBlock) { 38500b57cec5SDimitry Andric Out << "\n"; 38510b57cec5SDimitry Andric int Slot = Machine.getLocalSlot(BB); 38520b57cec5SDimitry Andric if (Slot != -1) 38530b57cec5SDimitry Andric Out << Slot << ":"; 38540b57cec5SDimitry Andric else 38550b57cec5SDimitry Andric Out << "<badref>:"; 38560b57cec5SDimitry Andric } 38570b57cec5SDimitry Andric 3858480093f4SDimitry Andric if (!IsEntryBlock) { 38590b57cec5SDimitry Andric // Output predecessors for the block. 38600b57cec5SDimitry Andric Out.PadToColumn(50); 38610b57cec5SDimitry Andric Out << ";"; 38620b57cec5SDimitry Andric const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB); 38630b57cec5SDimitry Andric 38640b57cec5SDimitry Andric if (PI == PE) { 38650b57cec5SDimitry Andric Out << " No predecessors!"; 38660b57cec5SDimitry Andric } else { 38670b57cec5SDimitry Andric Out << " preds = "; 38680b57cec5SDimitry Andric writeOperand(*PI, false); 38690b57cec5SDimitry Andric for (++PI; PI != PE; ++PI) { 38700b57cec5SDimitry Andric Out << ", "; 38710b57cec5SDimitry Andric writeOperand(*PI, false); 38720b57cec5SDimitry Andric } 38730b57cec5SDimitry Andric } 38740b57cec5SDimitry Andric } 38750b57cec5SDimitry Andric 38760b57cec5SDimitry Andric Out << "\n"; 38770b57cec5SDimitry Andric 38780b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out); 38790b57cec5SDimitry Andric 38800b57cec5SDimitry Andric // Output all of the instructions in the basic block... 38810b57cec5SDimitry Andric for (const Instruction &I : *BB) { 38820b57cec5SDimitry Andric printInstructionLine(I); 38830b57cec5SDimitry Andric } 38840b57cec5SDimitry Andric 38850b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out); 38860b57cec5SDimitry Andric } 38870b57cec5SDimitry Andric 38880b57cec5SDimitry Andric /// printInstructionLine - Print an instruction and a newline character. 38890b57cec5SDimitry Andric void AssemblyWriter::printInstructionLine(const Instruction &I) { 38900b57cec5SDimitry Andric printInstruction(I); 38910b57cec5SDimitry Andric Out << '\n'; 38920b57cec5SDimitry Andric } 38930b57cec5SDimitry Andric 38940b57cec5SDimitry Andric /// printGCRelocateComment - print comment after call to the gc.relocate 38950b57cec5SDimitry Andric /// intrinsic indicating base and derived pointer names. 38960b57cec5SDimitry Andric void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) { 38970b57cec5SDimitry Andric Out << " ; ("; 38980b57cec5SDimitry Andric writeOperand(Relocate.getBasePtr(), false); 38990b57cec5SDimitry Andric Out << ", "; 39000b57cec5SDimitry Andric writeOperand(Relocate.getDerivedPtr(), false); 39010b57cec5SDimitry Andric Out << ")"; 39020b57cec5SDimitry Andric } 39030b57cec5SDimitry Andric 39040b57cec5SDimitry Andric /// printInfoComment - Print a little comment after the instruction indicating 39050b57cec5SDimitry Andric /// which slot it occupies. 39060b57cec5SDimitry Andric void AssemblyWriter::printInfoComment(const Value &V) { 39070b57cec5SDimitry Andric if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V)) 39080b57cec5SDimitry Andric printGCRelocateComment(*Relocate); 39090b57cec5SDimitry Andric 39100b57cec5SDimitry Andric if (AnnotationWriter) 39110b57cec5SDimitry Andric AnnotationWriter->printInfoComment(V, Out); 39120b57cec5SDimitry Andric } 39130b57cec5SDimitry Andric 39140b57cec5SDimitry Andric static void maybePrintCallAddrSpace(const Value *Operand, const Instruction *I, 39150b57cec5SDimitry Andric raw_ostream &Out) { 39160b57cec5SDimitry Andric // We print the address space of the call if it is non-zero. 39170b57cec5SDimitry Andric unsigned CallAddrSpace = Operand->getType()->getPointerAddressSpace(); 39180b57cec5SDimitry Andric bool PrintAddrSpace = CallAddrSpace != 0; 39190b57cec5SDimitry Andric if (!PrintAddrSpace) { 39200b57cec5SDimitry Andric const Module *Mod = getModuleFromVal(I); 39210b57cec5SDimitry Andric // We also print it if it is zero but not equal to the program address space 39220b57cec5SDimitry Andric // or if we can't find a valid Module* to make it possible to parse 39230b57cec5SDimitry Andric // the resulting file even without a datalayout string. 39240b57cec5SDimitry Andric if (!Mod || Mod->getDataLayout().getProgramAddressSpace() != 0) 39250b57cec5SDimitry Andric PrintAddrSpace = true; 39260b57cec5SDimitry Andric } 39270b57cec5SDimitry Andric if (PrintAddrSpace) 39280b57cec5SDimitry Andric Out << " addrspace(" << CallAddrSpace << ")"; 39290b57cec5SDimitry Andric } 39300b57cec5SDimitry Andric 39310b57cec5SDimitry Andric // This member is called for each Instruction in a function.. 39320b57cec5SDimitry Andric void AssemblyWriter::printInstruction(const Instruction &I) { 39330b57cec5SDimitry Andric if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out); 39340b57cec5SDimitry Andric 39350b57cec5SDimitry Andric // Print out indentation for an instruction. 39360b57cec5SDimitry Andric Out << " "; 39370b57cec5SDimitry Andric 39380b57cec5SDimitry Andric // Print out name if it exists... 39390b57cec5SDimitry Andric if (I.hasName()) { 39400b57cec5SDimitry Andric PrintLLVMName(Out, &I); 39410b57cec5SDimitry Andric Out << " = "; 39420b57cec5SDimitry Andric } else if (!I.getType()->isVoidTy()) { 39430b57cec5SDimitry Andric // Print out the def slot taken. 39440b57cec5SDimitry Andric int SlotNum = Machine.getLocalSlot(&I); 39450b57cec5SDimitry Andric if (SlotNum == -1) 39460b57cec5SDimitry Andric Out << "<badref> = "; 39470b57cec5SDimitry Andric else 39480b57cec5SDimitry Andric Out << '%' << SlotNum << " = "; 39490b57cec5SDimitry Andric } 39500b57cec5SDimitry Andric 39510b57cec5SDimitry Andric if (const CallInst *CI = dyn_cast<CallInst>(&I)) { 39520b57cec5SDimitry Andric if (CI->isMustTailCall()) 39530b57cec5SDimitry Andric Out << "musttail "; 39540b57cec5SDimitry Andric else if (CI->isTailCall()) 39550b57cec5SDimitry Andric Out << "tail "; 39560b57cec5SDimitry Andric else if (CI->isNoTailCall()) 39570b57cec5SDimitry Andric Out << "notail "; 39580b57cec5SDimitry Andric } 39590b57cec5SDimitry Andric 39600b57cec5SDimitry Andric // Print out the opcode... 39610b57cec5SDimitry Andric Out << I.getOpcodeName(); 39620b57cec5SDimitry Andric 39630b57cec5SDimitry Andric // If this is an atomic load or store, print out the atomic marker. 39640b57cec5SDimitry Andric if ((isa<LoadInst>(I) && cast<LoadInst>(I).isAtomic()) || 39650b57cec5SDimitry Andric (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic())) 39660b57cec5SDimitry Andric Out << " atomic"; 39670b57cec5SDimitry Andric 39680b57cec5SDimitry Andric if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak()) 39690b57cec5SDimitry Andric Out << " weak"; 39700b57cec5SDimitry Andric 39710b57cec5SDimitry Andric // If this is a volatile operation, print out the volatile marker. 39720b57cec5SDimitry Andric if ((isa<LoadInst>(I) && cast<LoadInst>(I).isVolatile()) || 39730b57cec5SDimitry Andric (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) || 39740b57cec5SDimitry Andric (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) || 39750b57cec5SDimitry Andric (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile())) 39760b57cec5SDimitry Andric Out << " volatile"; 39770b57cec5SDimitry Andric 39780b57cec5SDimitry Andric // Print out optimization information. 39790b57cec5SDimitry Andric WriteOptimizationInfo(Out, &I); 39800b57cec5SDimitry Andric 39810b57cec5SDimitry Andric // Print out the compare instruction predicates 39820b57cec5SDimitry Andric if (const CmpInst *CI = dyn_cast<CmpInst>(&I)) 39830b57cec5SDimitry Andric Out << ' ' << CmpInst::getPredicateName(CI->getPredicate()); 39840b57cec5SDimitry Andric 39850b57cec5SDimitry Andric // Print out the atomicrmw operation 39860b57cec5SDimitry Andric if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) 39870b57cec5SDimitry Andric Out << ' ' << AtomicRMWInst::getOperationName(RMWI->getOperation()); 39880b57cec5SDimitry Andric 39890b57cec5SDimitry Andric // Print out the type of the operands... 39900b57cec5SDimitry Andric const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr; 39910b57cec5SDimitry Andric 39920b57cec5SDimitry Andric // Special case conditional branches to swizzle the condition out to the front 39930b57cec5SDimitry Andric if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) { 39940b57cec5SDimitry Andric const BranchInst &BI(cast<BranchInst>(I)); 39950b57cec5SDimitry Andric Out << ' '; 39960b57cec5SDimitry Andric writeOperand(BI.getCondition(), true); 39970b57cec5SDimitry Andric Out << ", "; 39980b57cec5SDimitry Andric writeOperand(BI.getSuccessor(0), true); 39990b57cec5SDimitry Andric Out << ", "; 40000b57cec5SDimitry Andric writeOperand(BI.getSuccessor(1), true); 40010b57cec5SDimitry Andric 40020b57cec5SDimitry Andric } else if (isa<SwitchInst>(I)) { 40030b57cec5SDimitry Andric const SwitchInst& SI(cast<SwitchInst>(I)); 40040b57cec5SDimitry Andric // Special case switch instruction to get formatting nice and correct. 40050b57cec5SDimitry Andric Out << ' '; 40060b57cec5SDimitry Andric writeOperand(SI.getCondition(), true); 40070b57cec5SDimitry Andric Out << ", "; 40080b57cec5SDimitry Andric writeOperand(SI.getDefaultDest(), true); 40090b57cec5SDimitry Andric Out << " ["; 40100b57cec5SDimitry Andric for (auto Case : SI.cases()) { 40110b57cec5SDimitry Andric Out << "\n "; 40120b57cec5SDimitry Andric writeOperand(Case.getCaseValue(), true); 40130b57cec5SDimitry Andric Out << ", "; 40140b57cec5SDimitry Andric writeOperand(Case.getCaseSuccessor(), true); 40150b57cec5SDimitry Andric } 40160b57cec5SDimitry Andric Out << "\n ]"; 40170b57cec5SDimitry Andric } else if (isa<IndirectBrInst>(I)) { 40180b57cec5SDimitry Andric // Special case indirectbr instruction to get formatting nice and correct. 40190b57cec5SDimitry Andric Out << ' '; 40200b57cec5SDimitry Andric writeOperand(Operand, true); 40210b57cec5SDimitry Andric Out << ", ["; 40220b57cec5SDimitry Andric 40230b57cec5SDimitry Andric for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) { 40240b57cec5SDimitry Andric if (i != 1) 40250b57cec5SDimitry Andric Out << ", "; 40260b57cec5SDimitry Andric writeOperand(I.getOperand(i), true); 40270b57cec5SDimitry Andric } 40280b57cec5SDimitry Andric Out << ']'; 40290b57cec5SDimitry Andric } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) { 40300b57cec5SDimitry Andric Out << ' '; 40310b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 40320b57cec5SDimitry Andric Out << ' '; 40330b57cec5SDimitry Andric 40340b57cec5SDimitry Andric for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) { 40350b57cec5SDimitry Andric if (op) Out << ", "; 40360b57cec5SDimitry Andric Out << "[ "; 40370b57cec5SDimitry Andric writeOperand(PN->getIncomingValue(op), false); Out << ", "; 40380b57cec5SDimitry Andric writeOperand(PN->getIncomingBlock(op), false); Out << " ]"; 40390b57cec5SDimitry Andric } 40400b57cec5SDimitry Andric } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) { 40410b57cec5SDimitry Andric Out << ' '; 40420b57cec5SDimitry Andric writeOperand(I.getOperand(0), true); 4043fe6060f1SDimitry Andric for (unsigned i : EVI->indices()) 4044fe6060f1SDimitry Andric Out << ", " << i; 40450b57cec5SDimitry Andric } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) { 40460b57cec5SDimitry Andric Out << ' '; 40470b57cec5SDimitry Andric writeOperand(I.getOperand(0), true); Out << ", "; 40480b57cec5SDimitry Andric writeOperand(I.getOperand(1), true); 4049fe6060f1SDimitry Andric for (unsigned i : IVI->indices()) 4050fe6060f1SDimitry Andric Out << ", " << i; 40510b57cec5SDimitry Andric } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) { 40520b57cec5SDimitry Andric Out << ' '; 40530b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 40540b57cec5SDimitry Andric if (LPI->isCleanup() || LPI->getNumClauses() != 0) 40550b57cec5SDimitry Andric Out << '\n'; 40560b57cec5SDimitry Andric 40570b57cec5SDimitry Andric if (LPI->isCleanup()) 40580b57cec5SDimitry Andric Out << " cleanup"; 40590b57cec5SDimitry Andric 40600b57cec5SDimitry Andric for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) { 40610b57cec5SDimitry Andric if (i != 0 || LPI->isCleanup()) Out << "\n"; 40620b57cec5SDimitry Andric if (LPI->isCatch(i)) 40630b57cec5SDimitry Andric Out << " catch "; 40640b57cec5SDimitry Andric else 40650b57cec5SDimitry Andric Out << " filter "; 40660b57cec5SDimitry Andric 40670b57cec5SDimitry Andric writeOperand(LPI->getClause(i), true); 40680b57cec5SDimitry Andric } 40690b57cec5SDimitry Andric } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) { 40700b57cec5SDimitry Andric Out << " within "; 40710b57cec5SDimitry Andric writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false); 40720b57cec5SDimitry Andric Out << " ["; 40730b57cec5SDimitry Andric unsigned Op = 0; 40740b57cec5SDimitry Andric for (const BasicBlock *PadBB : CatchSwitch->handlers()) { 40750b57cec5SDimitry Andric if (Op > 0) 40760b57cec5SDimitry Andric Out << ", "; 40770b57cec5SDimitry Andric writeOperand(PadBB, /*PrintType=*/true); 40780b57cec5SDimitry Andric ++Op; 40790b57cec5SDimitry Andric } 40800b57cec5SDimitry Andric Out << "] unwind "; 40810b57cec5SDimitry Andric if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest()) 40820b57cec5SDimitry Andric writeOperand(UnwindDest, /*PrintType=*/true); 40830b57cec5SDimitry Andric else 40840b57cec5SDimitry Andric Out << "to caller"; 40850b57cec5SDimitry Andric } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) { 40860b57cec5SDimitry Andric Out << " within "; 40870b57cec5SDimitry Andric writeOperand(FPI->getParentPad(), /*PrintType=*/false); 40880b57cec5SDimitry Andric Out << " ["; 40890b57cec5SDimitry Andric for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps; 40900b57cec5SDimitry Andric ++Op) { 40910b57cec5SDimitry Andric if (Op > 0) 40920b57cec5SDimitry Andric Out << ", "; 40930b57cec5SDimitry Andric writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true); 40940b57cec5SDimitry Andric } 40950b57cec5SDimitry Andric Out << ']'; 40960b57cec5SDimitry Andric } else if (isa<ReturnInst>(I) && !Operand) { 40970b57cec5SDimitry Andric Out << " void"; 40980b57cec5SDimitry Andric } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) { 40990b57cec5SDimitry Andric Out << " from "; 41000b57cec5SDimitry Andric writeOperand(CRI->getOperand(0), /*PrintType=*/false); 41010b57cec5SDimitry Andric 41020b57cec5SDimitry Andric Out << " to "; 41030b57cec5SDimitry Andric writeOperand(CRI->getOperand(1), /*PrintType=*/true); 41040b57cec5SDimitry Andric } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) { 41050b57cec5SDimitry Andric Out << " from "; 41060b57cec5SDimitry Andric writeOperand(CRI->getOperand(0), /*PrintType=*/false); 41070b57cec5SDimitry Andric 41080b57cec5SDimitry Andric Out << " unwind "; 41090b57cec5SDimitry Andric if (CRI->hasUnwindDest()) 41100b57cec5SDimitry Andric writeOperand(CRI->getOperand(1), /*PrintType=*/true); 41110b57cec5SDimitry Andric else 41120b57cec5SDimitry Andric Out << "to caller"; 41130b57cec5SDimitry Andric } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) { 41140b57cec5SDimitry Andric // Print the calling convention being used. 41150b57cec5SDimitry Andric if (CI->getCallingConv() != CallingConv::C) { 41160b57cec5SDimitry Andric Out << " "; 41170b57cec5SDimitry Andric PrintCallingConv(CI->getCallingConv(), Out); 41180b57cec5SDimitry Andric } 41190b57cec5SDimitry Andric 41205ffd83dbSDimitry Andric Operand = CI->getCalledOperand(); 41210b57cec5SDimitry Andric FunctionType *FTy = CI->getFunctionType(); 41220b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 41230b57cec5SDimitry Andric const AttributeList &PAL = CI->getAttributes(); 41240b57cec5SDimitry Andric 4125349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 41260b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 41270b57cec5SDimitry Andric 41280b57cec5SDimitry Andric // Only print addrspace(N) if necessary: 41290b57cec5SDimitry Andric maybePrintCallAddrSpace(Operand, &I, Out); 41300b57cec5SDimitry Andric 41310b57cec5SDimitry Andric // If possible, print out the short form of the call instruction. We can 41320b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 41330b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 41340b57cec5SDimitry Andric // 41350b57cec5SDimitry Andric Out << ' '; 41360b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 41370b57cec5SDimitry Andric Out << ' '; 41380b57cec5SDimitry Andric writeOperand(Operand, false); 41390b57cec5SDimitry Andric Out << '('; 4140349cc55cSDimitry Andric for (unsigned op = 0, Eop = CI->arg_size(); op < Eop; ++op) { 41410b57cec5SDimitry Andric if (op > 0) 41420b57cec5SDimitry Andric Out << ", "; 4143349cc55cSDimitry Andric writeParamOperand(CI->getArgOperand(op), PAL.getParamAttrs(op)); 41440b57cec5SDimitry Andric } 41450b57cec5SDimitry Andric 41460b57cec5SDimitry Andric // Emit an ellipsis if this is a musttail call in a vararg function. This 41470b57cec5SDimitry Andric // is only to aid readability, musttail calls forward varargs by default. 41480b57cec5SDimitry Andric if (CI->isMustTailCall() && CI->getParent() && 41490b57cec5SDimitry Andric CI->getParent()->getParent() && 41500b57cec5SDimitry Andric CI->getParent()->getParent()->isVarArg()) 41510b57cec5SDimitry Andric Out << ", ..."; 41520b57cec5SDimitry Andric 41530b57cec5SDimitry Andric Out << ')'; 4154349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4155349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 41560b57cec5SDimitry Andric 41570b57cec5SDimitry Andric writeOperandBundles(CI); 41580b57cec5SDimitry Andric } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) { 41595ffd83dbSDimitry Andric Operand = II->getCalledOperand(); 41600b57cec5SDimitry Andric FunctionType *FTy = II->getFunctionType(); 41610b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 41620b57cec5SDimitry Andric const AttributeList &PAL = II->getAttributes(); 41630b57cec5SDimitry Andric 41640b57cec5SDimitry Andric // Print the calling convention being used. 41650b57cec5SDimitry Andric if (II->getCallingConv() != CallingConv::C) { 41660b57cec5SDimitry Andric Out << " "; 41670b57cec5SDimitry Andric PrintCallingConv(II->getCallingConv(), Out); 41680b57cec5SDimitry Andric } 41690b57cec5SDimitry Andric 4170349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 41710b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 41720b57cec5SDimitry Andric 41730b57cec5SDimitry Andric // Only print addrspace(N) if necessary: 41740b57cec5SDimitry Andric maybePrintCallAddrSpace(Operand, &I, Out); 41750b57cec5SDimitry Andric 41760b57cec5SDimitry Andric // If possible, print out the short form of the invoke instruction. We can 41770b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 41780b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 41790b57cec5SDimitry Andric // 41800b57cec5SDimitry Andric Out << ' '; 41810b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 41820b57cec5SDimitry Andric Out << ' '; 41830b57cec5SDimitry Andric writeOperand(Operand, false); 41840b57cec5SDimitry Andric Out << '('; 4185349cc55cSDimitry Andric for (unsigned op = 0, Eop = II->arg_size(); op < Eop; ++op) { 41860b57cec5SDimitry Andric if (op) 41870b57cec5SDimitry Andric Out << ", "; 4188349cc55cSDimitry Andric writeParamOperand(II->getArgOperand(op), PAL.getParamAttrs(op)); 41890b57cec5SDimitry Andric } 41900b57cec5SDimitry Andric 41910b57cec5SDimitry Andric Out << ')'; 4192349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4193349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 41940b57cec5SDimitry Andric 41950b57cec5SDimitry Andric writeOperandBundles(II); 41960b57cec5SDimitry Andric 41970b57cec5SDimitry Andric Out << "\n to "; 41980b57cec5SDimitry Andric writeOperand(II->getNormalDest(), true); 41990b57cec5SDimitry Andric Out << " unwind "; 42000b57cec5SDimitry Andric writeOperand(II->getUnwindDest(), true); 42010b57cec5SDimitry Andric } else if (const CallBrInst *CBI = dyn_cast<CallBrInst>(&I)) { 42025ffd83dbSDimitry Andric Operand = CBI->getCalledOperand(); 42030b57cec5SDimitry Andric FunctionType *FTy = CBI->getFunctionType(); 42040b57cec5SDimitry Andric Type *RetTy = FTy->getReturnType(); 42050b57cec5SDimitry Andric const AttributeList &PAL = CBI->getAttributes(); 42060b57cec5SDimitry Andric 42070b57cec5SDimitry Andric // Print the calling convention being used. 42080b57cec5SDimitry Andric if (CBI->getCallingConv() != CallingConv::C) { 42090b57cec5SDimitry Andric Out << " "; 42100b57cec5SDimitry Andric PrintCallingConv(CBI->getCallingConv(), Out); 42110b57cec5SDimitry Andric } 42120b57cec5SDimitry Andric 4213349cc55cSDimitry Andric if (PAL.hasRetAttrs()) 42140b57cec5SDimitry Andric Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex); 42150b57cec5SDimitry Andric 42160b57cec5SDimitry Andric // If possible, print out the short form of the callbr instruction. We can 42170b57cec5SDimitry Andric // only do this if the first argument is a pointer to a nonvararg function, 42180b57cec5SDimitry Andric // and if the return type is not a pointer to a function. 42190b57cec5SDimitry Andric // 42200b57cec5SDimitry Andric Out << ' '; 42210b57cec5SDimitry Andric TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out); 42220b57cec5SDimitry Andric Out << ' '; 42230b57cec5SDimitry Andric writeOperand(Operand, false); 42240b57cec5SDimitry Andric Out << '('; 4225349cc55cSDimitry Andric for (unsigned op = 0, Eop = CBI->arg_size(); op < Eop; ++op) { 42260b57cec5SDimitry Andric if (op) 42270b57cec5SDimitry Andric Out << ", "; 4228349cc55cSDimitry Andric writeParamOperand(CBI->getArgOperand(op), PAL.getParamAttrs(op)); 42290b57cec5SDimitry Andric } 42300b57cec5SDimitry Andric 42310b57cec5SDimitry Andric Out << ')'; 4232349cc55cSDimitry Andric if (PAL.hasFnAttrs()) 4233349cc55cSDimitry Andric Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttrs()); 42340b57cec5SDimitry Andric 42350b57cec5SDimitry Andric writeOperandBundles(CBI); 42360b57cec5SDimitry Andric 42370b57cec5SDimitry Andric Out << "\n to "; 42380b57cec5SDimitry Andric writeOperand(CBI->getDefaultDest(), true); 42390b57cec5SDimitry Andric Out << " ["; 42400b57cec5SDimitry Andric for (unsigned i = 0, e = CBI->getNumIndirectDests(); i != e; ++i) { 42410b57cec5SDimitry Andric if (i != 0) 42420b57cec5SDimitry Andric Out << ", "; 42430b57cec5SDimitry Andric writeOperand(CBI->getIndirectDest(i), true); 42440b57cec5SDimitry Andric } 42450b57cec5SDimitry Andric Out << ']'; 42460b57cec5SDimitry Andric } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) { 42470b57cec5SDimitry Andric Out << ' '; 42480b57cec5SDimitry Andric if (AI->isUsedWithInAlloca()) 42490b57cec5SDimitry Andric Out << "inalloca "; 42500b57cec5SDimitry Andric if (AI->isSwiftError()) 42510b57cec5SDimitry Andric Out << "swifterror "; 42520b57cec5SDimitry Andric TypePrinter.print(AI->getAllocatedType(), Out); 42530b57cec5SDimitry Andric 42540b57cec5SDimitry Andric // Explicitly write the array size if the code is broken, if it's an array 42550b57cec5SDimitry Andric // allocation, or if the type is not canonical for scalar allocations. The 42560b57cec5SDimitry Andric // latter case prevents the type from mutating when round-tripping through 42570b57cec5SDimitry Andric // assembly. 42580b57cec5SDimitry Andric if (!AI->getArraySize() || AI->isArrayAllocation() || 42590b57cec5SDimitry Andric !AI->getArraySize()->getType()->isIntegerTy(32)) { 42600b57cec5SDimitry Andric Out << ", "; 42610b57cec5SDimitry Andric writeOperand(AI->getArraySize(), true); 42620b57cec5SDimitry Andric } 42630eae32dcSDimitry Andric if (MaybeAlign A = AI->getAlign()) { 42640eae32dcSDimitry Andric Out << ", align " << A->value(); 42650b57cec5SDimitry Andric } 42660b57cec5SDimitry Andric 42670b57cec5SDimitry Andric unsigned AddrSpace = AI->getType()->getAddressSpace(); 42680b57cec5SDimitry Andric if (AddrSpace != 0) { 42690b57cec5SDimitry Andric Out << ", addrspace(" << AddrSpace << ')'; 42700b57cec5SDimitry Andric } 42710b57cec5SDimitry Andric } else if (isa<CastInst>(I)) { 42720b57cec5SDimitry Andric if (Operand) { 42730b57cec5SDimitry Andric Out << ' '; 42740b57cec5SDimitry Andric writeOperand(Operand, true); // Work with broken code 42750b57cec5SDimitry Andric } 42760b57cec5SDimitry Andric Out << " to "; 42770b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 42780b57cec5SDimitry Andric } else if (isa<VAArgInst>(I)) { 42790b57cec5SDimitry Andric if (Operand) { 42800b57cec5SDimitry Andric Out << ' '; 42810b57cec5SDimitry Andric writeOperand(Operand, true); // Work with broken code 42820b57cec5SDimitry Andric } 42830b57cec5SDimitry Andric Out << ", "; 42840b57cec5SDimitry Andric TypePrinter.print(I.getType(), Out); 42850b57cec5SDimitry Andric } else if (Operand) { // Print the normal way. 42860b57cec5SDimitry Andric if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) { 42870b57cec5SDimitry Andric Out << ' '; 42880b57cec5SDimitry Andric TypePrinter.print(GEP->getSourceElementType(), Out); 42890b57cec5SDimitry Andric Out << ','; 42900b57cec5SDimitry Andric } else if (const auto *LI = dyn_cast<LoadInst>(&I)) { 42910b57cec5SDimitry Andric Out << ' '; 42920b57cec5SDimitry Andric TypePrinter.print(LI->getType(), Out); 42930b57cec5SDimitry Andric Out << ','; 42940b57cec5SDimitry Andric } 42950b57cec5SDimitry Andric 42960b57cec5SDimitry Andric // PrintAllTypes - Instructions who have operands of all the same type 42970b57cec5SDimitry Andric // omit the type from all but the first operand. If the instruction has 42980b57cec5SDimitry Andric // different type operands (for example br), then they are all printed. 42990b57cec5SDimitry Andric bool PrintAllTypes = false; 43000b57cec5SDimitry Andric Type *TheType = Operand->getType(); 43010b57cec5SDimitry Andric 43020b57cec5SDimitry Andric // Select, Store and ShuffleVector always print all types. 43030b57cec5SDimitry Andric if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I) 43040b57cec5SDimitry Andric || isa<ReturnInst>(I)) { 43050b57cec5SDimitry Andric PrintAllTypes = true; 43060b57cec5SDimitry Andric } else { 43070b57cec5SDimitry Andric for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) { 43080b57cec5SDimitry Andric Operand = I.getOperand(i); 43090b57cec5SDimitry Andric // note that Operand shouldn't be null, but the test helps make dump() 43100b57cec5SDimitry Andric // more tolerant of malformed IR 43110b57cec5SDimitry Andric if (Operand && Operand->getType() != TheType) { 43120b57cec5SDimitry Andric PrintAllTypes = true; // We have differing types! Print them all! 43130b57cec5SDimitry Andric break; 43140b57cec5SDimitry Andric } 43150b57cec5SDimitry Andric } 43160b57cec5SDimitry Andric } 43170b57cec5SDimitry Andric 43180b57cec5SDimitry Andric if (!PrintAllTypes) { 43190b57cec5SDimitry Andric Out << ' '; 43200b57cec5SDimitry Andric TypePrinter.print(TheType, Out); 43210b57cec5SDimitry Andric } 43220b57cec5SDimitry Andric 43230b57cec5SDimitry Andric Out << ' '; 43240b57cec5SDimitry Andric for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) { 43250b57cec5SDimitry Andric if (i) Out << ", "; 43260b57cec5SDimitry Andric writeOperand(I.getOperand(i), PrintAllTypes); 43270b57cec5SDimitry Andric } 43280b57cec5SDimitry Andric } 43290b57cec5SDimitry Andric 43300b57cec5SDimitry Andric // Print atomic ordering/alignment for memory operations 43310b57cec5SDimitry Andric if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) { 43320b57cec5SDimitry Andric if (LI->isAtomic()) 43330b57cec5SDimitry Andric writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID()); 43340eae32dcSDimitry Andric if (MaybeAlign A = LI->getAlign()) 43350eae32dcSDimitry Andric Out << ", align " << A->value(); 43360b57cec5SDimitry Andric } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) { 43370b57cec5SDimitry Andric if (SI->isAtomic()) 43380b57cec5SDimitry Andric writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID()); 43390eae32dcSDimitry Andric if (MaybeAlign A = SI->getAlign()) 43400eae32dcSDimitry Andric Out << ", align " << A->value(); 43410b57cec5SDimitry Andric } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) { 43420b57cec5SDimitry Andric writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(), 43430b57cec5SDimitry Andric CXI->getFailureOrdering(), CXI->getSyncScopeID()); 4344fe6060f1SDimitry Andric Out << ", align " << CXI->getAlign().value(); 43450b57cec5SDimitry Andric } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) { 43460b57cec5SDimitry Andric writeAtomic(RMWI->getContext(), RMWI->getOrdering(), 43470b57cec5SDimitry Andric RMWI->getSyncScopeID()); 4348fe6060f1SDimitry Andric Out << ", align " << RMWI->getAlign().value(); 43490b57cec5SDimitry Andric } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) { 43500b57cec5SDimitry Andric writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID()); 43515ffd83dbSDimitry Andric } else if (const ShuffleVectorInst *SVI = dyn_cast<ShuffleVectorInst>(&I)) { 43525ffd83dbSDimitry Andric PrintShuffleMask(Out, SVI->getType(), SVI->getShuffleMask()); 43530b57cec5SDimitry Andric } 43540b57cec5SDimitry Andric 43550b57cec5SDimitry Andric // Print Metadata info. 43560b57cec5SDimitry Andric SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD; 43570b57cec5SDimitry Andric I.getAllMetadata(InstMD); 43580b57cec5SDimitry Andric printMetadataAttachments(InstMD, ", "); 43590b57cec5SDimitry Andric 43600b57cec5SDimitry Andric // Print a nice comment. 43610b57cec5SDimitry Andric printInfoComment(I); 43620b57cec5SDimitry Andric } 43630b57cec5SDimitry Andric 43640b57cec5SDimitry Andric void AssemblyWriter::printMetadataAttachments( 43650b57cec5SDimitry Andric const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs, 43660b57cec5SDimitry Andric StringRef Separator) { 43670b57cec5SDimitry Andric if (MDs.empty()) 43680b57cec5SDimitry Andric return; 43690b57cec5SDimitry Andric 43700b57cec5SDimitry Andric if (MDNames.empty()) 43710b57cec5SDimitry Andric MDs[0].second->getContext().getMDKindNames(MDNames); 43720b57cec5SDimitry Andric 4373349cc55cSDimitry Andric auto WriterCtx = getContext(); 43740b57cec5SDimitry Andric for (const auto &I : MDs) { 43750b57cec5SDimitry Andric unsigned Kind = I.first; 43760b57cec5SDimitry Andric Out << Separator; 43770b57cec5SDimitry Andric if (Kind < MDNames.size()) { 43780b57cec5SDimitry Andric Out << "!"; 43790b57cec5SDimitry Andric printMetadataIdentifier(MDNames[Kind], Out); 43800b57cec5SDimitry Andric } else 43810b57cec5SDimitry Andric Out << "!<unknown kind #" << Kind << ">"; 43820b57cec5SDimitry Andric Out << ' '; 4383349cc55cSDimitry Andric WriteAsOperandInternal(Out, I.second, WriterCtx); 43840b57cec5SDimitry Andric } 43850b57cec5SDimitry Andric } 43860b57cec5SDimitry Andric 43870b57cec5SDimitry Andric void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) { 43880b57cec5SDimitry Andric Out << '!' << Slot << " = "; 43890b57cec5SDimitry Andric printMDNodeBody(Node); 43900b57cec5SDimitry Andric Out << "\n"; 43910b57cec5SDimitry Andric } 43920b57cec5SDimitry Andric 43930b57cec5SDimitry Andric void AssemblyWriter::writeAllMDNodes() { 43940b57cec5SDimitry Andric SmallVector<const MDNode *, 16> Nodes; 43950b57cec5SDimitry Andric Nodes.resize(Machine.mdn_size()); 4396fe6060f1SDimitry Andric for (auto &I : llvm::make_range(Machine.mdn_begin(), Machine.mdn_end())) 4397fe6060f1SDimitry Andric Nodes[I.second] = cast<MDNode>(I.first); 43980b57cec5SDimitry Andric 43990b57cec5SDimitry Andric for (unsigned i = 0, e = Nodes.size(); i != e; ++i) { 44000b57cec5SDimitry Andric writeMDNode(i, Nodes[i]); 44010b57cec5SDimitry Andric } 44020b57cec5SDimitry Andric } 44030b57cec5SDimitry Andric 44040b57cec5SDimitry Andric void AssemblyWriter::printMDNodeBody(const MDNode *Node) { 4405349cc55cSDimitry Andric auto WriterCtx = getContext(); 4406349cc55cSDimitry Andric WriteMDNodeBodyInternal(Out, Node, WriterCtx); 44070b57cec5SDimitry Andric } 44080b57cec5SDimitry Andric 4409480093f4SDimitry Andric void AssemblyWriter::writeAttribute(const Attribute &Attr, bool InAttrGroup) { 4410480093f4SDimitry Andric if (!Attr.isTypeAttribute()) { 4411480093f4SDimitry Andric Out << Attr.getAsString(InAttrGroup); 4412480093f4SDimitry Andric return; 4413480093f4SDimitry Andric } 4414480093f4SDimitry Andric 4415fe6060f1SDimitry Andric Out << Attribute::getNameFromAttrKind(Attr.getKindAsEnum()); 4416480093f4SDimitry Andric if (Type *Ty = Attr.getValueAsType()) { 4417480093f4SDimitry Andric Out << '('; 4418480093f4SDimitry Andric TypePrinter.print(Ty, Out); 4419480093f4SDimitry Andric Out << ')'; 4420480093f4SDimitry Andric } 4421480093f4SDimitry Andric } 4422480093f4SDimitry Andric 4423480093f4SDimitry Andric void AssemblyWriter::writeAttributeSet(const AttributeSet &AttrSet, 4424480093f4SDimitry Andric bool InAttrGroup) { 4425480093f4SDimitry Andric bool FirstAttr = true; 4426480093f4SDimitry Andric for (const auto &Attr : AttrSet) { 4427480093f4SDimitry Andric if (!FirstAttr) 4428480093f4SDimitry Andric Out << ' '; 4429480093f4SDimitry Andric writeAttribute(Attr, InAttrGroup); 4430480093f4SDimitry Andric FirstAttr = false; 4431480093f4SDimitry Andric } 4432480093f4SDimitry Andric } 4433480093f4SDimitry Andric 44340b57cec5SDimitry Andric void AssemblyWriter::writeAllAttributeGroups() { 44350b57cec5SDimitry Andric std::vector<std::pair<AttributeSet, unsigned>> asVec; 44360b57cec5SDimitry Andric asVec.resize(Machine.as_size()); 44370b57cec5SDimitry Andric 4438fe6060f1SDimitry Andric for (auto &I : llvm::make_range(Machine.as_begin(), Machine.as_end())) 4439fe6060f1SDimitry Andric asVec[I.second] = I; 44400b57cec5SDimitry Andric 44410b57cec5SDimitry Andric for (const auto &I : asVec) 44420b57cec5SDimitry Andric Out << "attributes #" << I.second << " = { " 44430b57cec5SDimitry Andric << I.first.getAsString(true) << " }\n"; 44440b57cec5SDimitry Andric } 44450b57cec5SDimitry Andric 4446fe6060f1SDimitry Andric void AssemblyWriter::printUseListOrder(const Value *V, 4447fe6060f1SDimitry Andric const std::vector<unsigned> &Shuffle) { 44480b57cec5SDimitry Andric bool IsInFunction = Machine.getFunction(); 44490b57cec5SDimitry Andric if (IsInFunction) 44500b57cec5SDimitry Andric Out << " "; 44510b57cec5SDimitry Andric 44520b57cec5SDimitry Andric Out << "uselistorder"; 4453fe6060f1SDimitry Andric if (const BasicBlock *BB = IsInFunction ? nullptr : dyn_cast<BasicBlock>(V)) { 44540b57cec5SDimitry Andric Out << "_bb "; 44550b57cec5SDimitry Andric writeOperand(BB->getParent(), false); 44560b57cec5SDimitry Andric Out << ", "; 44570b57cec5SDimitry Andric writeOperand(BB, false); 44580b57cec5SDimitry Andric } else { 44590b57cec5SDimitry Andric Out << " "; 4460fe6060f1SDimitry Andric writeOperand(V, true); 44610b57cec5SDimitry Andric } 44620b57cec5SDimitry Andric Out << ", { "; 44630b57cec5SDimitry Andric 4464fe6060f1SDimitry Andric assert(Shuffle.size() >= 2 && "Shuffle too small"); 4465fe6060f1SDimitry Andric Out << Shuffle[0]; 4466fe6060f1SDimitry Andric for (unsigned I = 1, E = Shuffle.size(); I != E; ++I) 4467fe6060f1SDimitry Andric Out << ", " << Shuffle[I]; 44680b57cec5SDimitry Andric Out << " }\n"; 44690b57cec5SDimitry Andric } 44700b57cec5SDimitry Andric 44710b57cec5SDimitry Andric void AssemblyWriter::printUseLists(const Function *F) { 4472fe6060f1SDimitry Andric auto It = UseListOrders.find(F); 4473fe6060f1SDimitry Andric if (It == UseListOrders.end()) 44740b57cec5SDimitry Andric return; 44750b57cec5SDimitry Andric 44760b57cec5SDimitry Andric Out << "\n; uselistorder directives\n"; 4477fe6060f1SDimitry Andric for (const auto &Pair : It->second) 4478fe6060f1SDimitry Andric printUseListOrder(Pair.first, Pair.second); 44790b57cec5SDimitry Andric } 44800b57cec5SDimitry Andric 44810b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44820b57cec5SDimitry Andric // External Interface declarations 44830b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 44840b57cec5SDimitry Andric 44850b57cec5SDimitry Andric void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 44860b57cec5SDimitry Andric bool ShouldPreserveUseListOrder, 44870b57cec5SDimitry Andric bool IsForDebug) const { 44880b57cec5SDimitry Andric SlotTracker SlotTable(this->getParent()); 44890b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 44900b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this->getParent(), AAW, 44910b57cec5SDimitry Andric IsForDebug, 44920b57cec5SDimitry Andric ShouldPreserveUseListOrder); 44930b57cec5SDimitry Andric W.printFunction(this); 44940b57cec5SDimitry Andric } 44950b57cec5SDimitry Andric 44965ffd83dbSDimitry Andric void BasicBlock::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 44975ffd83dbSDimitry Andric bool ShouldPreserveUseListOrder, 44985ffd83dbSDimitry Andric bool IsForDebug) const { 4499e8d8bef9SDimitry Andric SlotTracker SlotTable(this->getParent()); 45005ffd83dbSDimitry Andric formatted_raw_ostream OS(ROS); 45015ffd83dbSDimitry Andric AssemblyWriter W(OS, SlotTable, this->getModule(), AAW, 45025ffd83dbSDimitry Andric IsForDebug, 45035ffd83dbSDimitry Andric ShouldPreserveUseListOrder); 45045ffd83dbSDimitry Andric W.printBasicBlock(this); 45055ffd83dbSDimitry Andric } 45065ffd83dbSDimitry Andric 45070b57cec5SDimitry Andric void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW, 45080b57cec5SDimitry Andric bool ShouldPreserveUseListOrder, bool IsForDebug) const { 45090b57cec5SDimitry Andric SlotTracker SlotTable(this); 45100b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 45110b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug, 45120b57cec5SDimitry Andric ShouldPreserveUseListOrder); 45130b57cec5SDimitry Andric W.printModule(this); 45140b57cec5SDimitry Andric } 45150b57cec5SDimitry Andric 45160b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const { 45170b57cec5SDimitry Andric SlotTracker SlotTable(getParent()); 45180b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 45190b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug); 45200b57cec5SDimitry Andric W.printNamedMDNode(this); 45210b57cec5SDimitry Andric } 45220b57cec5SDimitry Andric 45230b57cec5SDimitry Andric void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST, 45240b57cec5SDimitry Andric bool IsForDebug) const { 45250b57cec5SDimitry Andric Optional<SlotTracker> LocalST; 45260b57cec5SDimitry Andric SlotTracker *SlotTable; 45270b57cec5SDimitry Andric if (auto *ST = MST.getMachine()) 45280b57cec5SDimitry Andric SlotTable = ST; 45290b57cec5SDimitry Andric else { 45300b57cec5SDimitry Andric LocalST.emplace(getParent()); 45310b57cec5SDimitry Andric SlotTable = &*LocalST; 45320b57cec5SDimitry Andric } 45330b57cec5SDimitry Andric 45340b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 45350b57cec5SDimitry Andric AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug); 45360b57cec5SDimitry Andric W.printNamedMDNode(this); 45370b57cec5SDimitry Andric } 45380b57cec5SDimitry Andric 45390b57cec5SDimitry Andric void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const { 45400b57cec5SDimitry Andric PrintLLVMName(ROS, getName(), ComdatPrefix); 45410b57cec5SDimitry Andric ROS << " = comdat "; 45420b57cec5SDimitry Andric 45430b57cec5SDimitry Andric switch (getSelectionKind()) { 45440b57cec5SDimitry Andric case Comdat::Any: 45450b57cec5SDimitry Andric ROS << "any"; 45460b57cec5SDimitry Andric break; 45470b57cec5SDimitry Andric case Comdat::ExactMatch: 45480b57cec5SDimitry Andric ROS << "exactmatch"; 45490b57cec5SDimitry Andric break; 45500b57cec5SDimitry Andric case Comdat::Largest: 45510b57cec5SDimitry Andric ROS << "largest"; 45520b57cec5SDimitry Andric break; 4553fe6060f1SDimitry Andric case Comdat::NoDeduplicate: 4554fe6060f1SDimitry Andric ROS << "nodeduplicate"; 45550b57cec5SDimitry Andric break; 45560b57cec5SDimitry Andric case Comdat::SameSize: 45570b57cec5SDimitry Andric ROS << "samesize"; 45580b57cec5SDimitry Andric break; 45590b57cec5SDimitry Andric } 45600b57cec5SDimitry Andric 45610b57cec5SDimitry Andric ROS << '\n'; 45620b57cec5SDimitry Andric } 45630b57cec5SDimitry Andric 45640b57cec5SDimitry Andric void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const { 45650b57cec5SDimitry Andric TypePrinting TP; 45660b57cec5SDimitry Andric TP.print(const_cast<Type*>(this), OS); 45670b57cec5SDimitry Andric 45680b57cec5SDimitry Andric if (NoDetails) 45690b57cec5SDimitry Andric return; 45700b57cec5SDimitry Andric 45710b57cec5SDimitry Andric // If the type is a named struct type, print the body as well. 45720b57cec5SDimitry Andric if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this))) 45730b57cec5SDimitry Andric if (!STy->isLiteral()) { 45740b57cec5SDimitry Andric OS << " = type "; 45750b57cec5SDimitry Andric TP.printStructBody(STy, OS); 45760b57cec5SDimitry Andric } 45770b57cec5SDimitry Andric } 45780b57cec5SDimitry Andric 45790b57cec5SDimitry Andric static bool isReferencingMDNode(const Instruction &I) { 45800b57cec5SDimitry Andric if (const auto *CI = dyn_cast<CallInst>(&I)) 45810b57cec5SDimitry Andric if (Function *F = CI->getCalledFunction()) 45820b57cec5SDimitry Andric if (F->isIntrinsic()) 45830b57cec5SDimitry Andric for (auto &Op : I.operands()) 45840b57cec5SDimitry Andric if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op)) 45850b57cec5SDimitry Andric if (isa<MDNode>(V->getMetadata())) 45860b57cec5SDimitry Andric return true; 45870b57cec5SDimitry Andric return false; 45880b57cec5SDimitry Andric } 45890b57cec5SDimitry Andric 45900b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, bool IsForDebug) const { 45910b57cec5SDimitry Andric bool ShouldInitializeAllMetadata = false; 45920b57cec5SDimitry Andric if (auto *I = dyn_cast<Instruction>(this)) 45930b57cec5SDimitry Andric ShouldInitializeAllMetadata = isReferencingMDNode(*I); 45940b57cec5SDimitry Andric else if (isa<Function>(this) || isa<MetadataAsValue>(this)) 45950b57cec5SDimitry Andric ShouldInitializeAllMetadata = true; 45960b57cec5SDimitry Andric 45970b57cec5SDimitry Andric ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata); 45980b57cec5SDimitry Andric print(ROS, MST, IsForDebug); 45990b57cec5SDimitry Andric } 46000b57cec5SDimitry Andric 46010b57cec5SDimitry Andric void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST, 46020b57cec5SDimitry Andric bool IsForDebug) const { 46030b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 46040b57cec5SDimitry Andric SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr)); 46050b57cec5SDimitry Andric SlotTracker &SlotTable = 46060b57cec5SDimitry Andric MST.getMachine() ? *MST.getMachine() : EmptySlotTable; 46070b57cec5SDimitry Andric auto incorporateFunction = [&](const Function *F) { 46080b57cec5SDimitry Andric if (F) 46090b57cec5SDimitry Andric MST.incorporateFunction(*F); 46100b57cec5SDimitry Andric }; 46110b57cec5SDimitry Andric 46120b57cec5SDimitry Andric if (const Instruction *I = dyn_cast<Instruction>(this)) { 46130b57cec5SDimitry Andric incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr); 46140b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug); 46150b57cec5SDimitry Andric W.printInstruction(*I); 46160b57cec5SDimitry Andric } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) { 46170b57cec5SDimitry Andric incorporateFunction(BB->getParent()); 46180b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug); 46190b57cec5SDimitry Andric W.printBasicBlock(BB); 46200b57cec5SDimitry Andric } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) { 46210b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug); 46220b57cec5SDimitry Andric if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV)) 46230b57cec5SDimitry Andric W.printGlobal(V); 46240b57cec5SDimitry Andric else if (const Function *F = dyn_cast<Function>(GV)) 46250b57cec5SDimitry Andric W.printFunction(F); 4626349cc55cSDimitry Andric else if (const GlobalAlias *A = dyn_cast<GlobalAlias>(GV)) 4627349cc55cSDimitry Andric W.printAlias(A); 4628349cc55cSDimitry Andric else if (const GlobalIFunc *I = dyn_cast<GlobalIFunc>(GV)) 4629349cc55cSDimitry Andric W.printIFunc(I); 46300b57cec5SDimitry Andric else 4631349cc55cSDimitry Andric llvm_unreachable("Unknown GlobalValue to print out!"); 46320b57cec5SDimitry Andric } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) { 46330b57cec5SDimitry Andric V->getMetadata()->print(ROS, MST, getModuleFromVal(V)); 46340b57cec5SDimitry Andric } else if (const Constant *C = dyn_cast<Constant>(this)) { 46350b57cec5SDimitry Andric TypePrinting TypePrinter; 46360b57cec5SDimitry Andric TypePrinter.print(C->getType(), OS); 46370b57cec5SDimitry Andric OS << ' '; 4638349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine()); 4639349cc55cSDimitry Andric WriteConstantInternal(OS, C, WriterCtx); 46400b57cec5SDimitry Andric } else if (isa<InlineAsm>(this) || isa<Argument>(this)) { 46410b57cec5SDimitry Andric this->printAsOperand(OS, /* PrintType */ true, MST); 46420b57cec5SDimitry Andric } else { 46430b57cec5SDimitry Andric llvm_unreachable("Unknown value to print out!"); 46440b57cec5SDimitry Andric } 46450b57cec5SDimitry Andric } 46460b57cec5SDimitry Andric 46470b57cec5SDimitry Andric /// Print without a type, skipping the TypePrinting object. 46480b57cec5SDimitry Andric /// 46490b57cec5SDimitry Andric /// \return \c true iff printing was successful. 46500b57cec5SDimitry Andric static bool printWithoutType(const Value &V, raw_ostream &O, 46510b57cec5SDimitry Andric SlotTracker *Machine, const Module *M) { 46520b57cec5SDimitry Andric if (V.hasName() || isa<GlobalValue>(V) || 46530b57cec5SDimitry Andric (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) { 4654349cc55cSDimitry Andric AsmWriterContext WriterCtx(nullptr, Machine, M); 4655349cc55cSDimitry Andric WriteAsOperandInternal(O, &V, WriterCtx); 46560b57cec5SDimitry Andric return true; 46570b57cec5SDimitry Andric } 46580b57cec5SDimitry Andric return false; 46590b57cec5SDimitry Andric } 46600b57cec5SDimitry Andric 46610b57cec5SDimitry Andric static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType, 46620b57cec5SDimitry Andric ModuleSlotTracker &MST) { 46630b57cec5SDimitry Andric TypePrinting TypePrinter(MST.getModule()); 46640b57cec5SDimitry Andric if (PrintType) { 46650b57cec5SDimitry Andric TypePrinter.print(V.getType(), O); 46660b57cec5SDimitry Andric O << ' '; 46670b57cec5SDimitry Andric } 46680b57cec5SDimitry Andric 4669349cc55cSDimitry Andric AsmWriterContext WriterCtx(&TypePrinter, MST.getMachine(), MST.getModule()); 4670349cc55cSDimitry Andric WriteAsOperandInternal(O, &V, WriterCtx); 46710b57cec5SDimitry Andric } 46720b57cec5SDimitry Andric 46730b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType, 46740b57cec5SDimitry Andric const Module *M) const { 46750b57cec5SDimitry Andric if (!M) 46760b57cec5SDimitry Andric M = getModuleFromVal(this); 46770b57cec5SDimitry Andric 46780b57cec5SDimitry Andric if (!PrintType) 46790b57cec5SDimitry Andric if (printWithoutType(*this, O, nullptr, M)) 46800b57cec5SDimitry Andric return; 46810b57cec5SDimitry Andric 46820b57cec5SDimitry Andric SlotTracker Machine( 46830b57cec5SDimitry Andric M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this)); 46840b57cec5SDimitry Andric ModuleSlotTracker MST(Machine, M); 46850b57cec5SDimitry Andric printAsOperandImpl(*this, O, PrintType, MST); 46860b57cec5SDimitry Andric } 46870b57cec5SDimitry Andric 46880b57cec5SDimitry Andric void Value::printAsOperand(raw_ostream &O, bool PrintType, 46890b57cec5SDimitry Andric ModuleSlotTracker &MST) const { 46900b57cec5SDimitry Andric if (!PrintType) 46910b57cec5SDimitry Andric if (printWithoutType(*this, O, MST.getMachine(), MST.getModule())) 46920b57cec5SDimitry Andric return; 46930b57cec5SDimitry Andric 46940b57cec5SDimitry Andric printAsOperandImpl(*this, O, PrintType, MST); 46950b57cec5SDimitry Andric } 46960b57cec5SDimitry Andric 4697349cc55cSDimitry Andric /// Recursive version of printMetadataImpl. 4698349cc55cSDimitry Andric static void printMetadataImplRec(raw_ostream &ROS, const Metadata &MD, 4699349cc55cSDimitry Andric AsmWriterContext &WriterCtx) { 4700349cc55cSDimitry Andric formatted_raw_ostream OS(ROS); 4701349cc55cSDimitry Andric WriteAsOperandInternal(OS, &MD, WriterCtx, /* FromValue */ true); 4702349cc55cSDimitry Andric 4703349cc55cSDimitry Andric auto *N = dyn_cast<MDNode>(&MD); 4704349cc55cSDimitry Andric if (!N || isa<DIExpression>(MD) || isa<DIArgList>(MD)) 4705349cc55cSDimitry Andric return; 4706349cc55cSDimitry Andric 4707349cc55cSDimitry Andric OS << " = "; 4708349cc55cSDimitry Andric WriteMDNodeBodyInternal(OS, N, WriterCtx); 4709349cc55cSDimitry Andric } 4710349cc55cSDimitry Andric 4711349cc55cSDimitry Andric namespace { 4712349cc55cSDimitry Andric struct MDTreeAsmWriterContext : public AsmWriterContext { 4713349cc55cSDimitry Andric unsigned Level; 4714349cc55cSDimitry Andric // {Level, Printed string} 4715349cc55cSDimitry Andric using EntryTy = std::pair<unsigned, std::string>; 4716349cc55cSDimitry Andric SmallVector<EntryTy, 4> Buffer; 4717349cc55cSDimitry Andric 4718349cc55cSDimitry Andric // Used to break the cycle in case there is any. 4719349cc55cSDimitry Andric SmallPtrSet<const Metadata *, 4> Visited; 4720349cc55cSDimitry Andric 4721349cc55cSDimitry Andric raw_ostream &MainOS; 4722349cc55cSDimitry Andric 4723349cc55cSDimitry Andric MDTreeAsmWriterContext(TypePrinting *TP, SlotTracker *ST, const Module *M, 4724349cc55cSDimitry Andric raw_ostream &OS, const Metadata *InitMD) 4725349cc55cSDimitry Andric : AsmWriterContext(TP, ST, M), Level(0U), Visited({InitMD}), MainOS(OS) {} 4726349cc55cSDimitry Andric 4727349cc55cSDimitry Andric void onWriteMetadataAsOperand(const Metadata *MD) override { 4728*81ad6265SDimitry Andric if (!Visited.insert(MD).second) 4729349cc55cSDimitry Andric return; 4730349cc55cSDimitry Andric 4731349cc55cSDimitry Andric std::string Str; 4732349cc55cSDimitry Andric raw_string_ostream SS(Str); 4733349cc55cSDimitry Andric ++Level; 4734349cc55cSDimitry Andric // A placeholder entry to memorize the correct 4735349cc55cSDimitry Andric // position in buffer. 4736349cc55cSDimitry Andric Buffer.emplace_back(std::make_pair(Level, "")); 4737349cc55cSDimitry Andric unsigned InsertIdx = Buffer.size() - 1; 4738349cc55cSDimitry Andric 4739349cc55cSDimitry Andric printMetadataImplRec(SS, *MD, *this); 4740349cc55cSDimitry Andric Buffer[InsertIdx].second = std::move(SS.str()); 4741349cc55cSDimitry Andric --Level; 4742349cc55cSDimitry Andric } 4743349cc55cSDimitry Andric 4744349cc55cSDimitry Andric ~MDTreeAsmWriterContext() { 4745349cc55cSDimitry Andric for (const auto &Entry : Buffer) { 4746349cc55cSDimitry Andric MainOS << "\n"; 4747349cc55cSDimitry Andric unsigned NumIndent = Entry.first * 2U; 4748349cc55cSDimitry Andric MainOS.indent(NumIndent) << Entry.second; 4749349cc55cSDimitry Andric } 4750349cc55cSDimitry Andric } 4751349cc55cSDimitry Andric }; 4752349cc55cSDimitry Andric } // end anonymous namespace 4753349cc55cSDimitry Andric 47540b57cec5SDimitry Andric static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD, 47550b57cec5SDimitry Andric ModuleSlotTracker &MST, const Module *M, 4756349cc55cSDimitry Andric bool OnlyAsOperand, bool PrintAsTree = false) { 47570b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 47580b57cec5SDimitry Andric 47590b57cec5SDimitry Andric TypePrinting TypePrinter(M); 47600b57cec5SDimitry Andric 4761349cc55cSDimitry Andric std::unique_ptr<AsmWriterContext> WriterCtx; 4762349cc55cSDimitry Andric if (PrintAsTree && !OnlyAsOperand) 4763349cc55cSDimitry Andric WriterCtx = std::make_unique<MDTreeAsmWriterContext>( 4764349cc55cSDimitry Andric &TypePrinter, MST.getMachine(), M, OS, &MD); 4765349cc55cSDimitry Andric else 4766349cc55cSDimitry Andric WriterCtx = 4767349cc55cSDimitry Andric std::make_unique<AsmWriterContext>(&TypePrinter, MST.getMachine(), M); 4768349cc55cSDimitry Andric 4769349cc55cSDimitry Andric WriteAsOperandInternal(OS, &MD, *WriterCtx, /* FromValue */ true); 47700b57cec5SDimitry Andric 47710b57cec5SDimitry Andric auto *N = dyn_cast<MDNode>(&MD); 4772fe6060f1SDimitry Andric if (OnlyAsOperand || !N || isa<DIExpression>(MD) || isa<DIArgList>(MD)) 47730b57cec5SDimitry Andric return; 47740b57cec5SDimitry Andric 47750b57cec5SDimitry Andric OS << " = "; 4776349cc55cSDimitry Andric WriteMDNodeBodyInternal(OS, N, *WriterCtx); 47770b57cec5SDimitry Andric } 47780b57cec5SDimitry Andric 47790b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const { 47800b57cec5SDimitry Andric ModuleSlotTracker MST(M, isa<MDNode>(this)); 47810b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true); 47820b57cec5SDimitry Andric } 47830b57cec5SDimitry Andric 47840b57cec5SDimitry Andric void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST, 47850b57cec5SDimitry Andric const Module *M) const { 47860b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true); 47870b57cec5SDimitry Andric } 47880b57cec5SDimitry Andric 47890b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, const Module *M, 47900b57cec5SDimitry Andric bool /*IsForDebug*/) const { 47910b57cec5SDimitry Andric ModuleSlotTracker MST(M, isa<MDNode>(this)); 47920b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); 47930b57cec5SDimitry Andric } 47940b57cec5SDimitry Andric 47950b57cec5SDimitry Andric void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST, 47960b57cec5SDimitry Andric const Module *M, bool /*IsForDebug*/) const { 47970b57cec5SDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false); 47980b57cec5SDimitry Andric } 47990b57cec5SDimitry Andric 4800349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, const Module *M) const { 4801349cc55cSDimitry Andric ModuleSlotTracker MST(M, true); 4802349cc55cSDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false, 4803349cc55cSDimitry Andric /*PrintAsTree=*/true); 4804349cc55cSDimitry Andric } 4805349cc55cSDimitry Andric 4806349cc55cSDimitry Andric void MDNode::printTree(raw_ostream &OS, ModuleSlotTracker &MST, 4807349cc55cSDimitry Andric const Module *M) const { 4808349cc55cSDimitry Andric printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false, 4809349cc55cSDimitry Andric /*PrintAsTree=*/true); 4810349cc55cSDimitry Andric } 4811349cc55cSDimitry Andric 48120b57cec5SDimitry Andric void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const { 48130b57cec5SDimitry Andric SlotTracker SlotTable(this); 48140b57cec5SDimitry Andric formatted_raw_ostream OS(ROS); 48150b57cec5SDimitry Andric AssemblyWriter W(OS, SlotTable, this, IsForDebug); 48160b57cec5SDimitry Andric W.printModuleSummaryIndex(); 48170b57cec5SDimitry Andric } 48180b57cec5SDimitry Andric 4819fe6060f1SDimitry Andric void ModuleSlotTracker::collectMDNodes(MachineMDNodeListType &L, unsigned LB, 4820fe6060f1SDimitry Andric unsigned UB) const { 4821fe6060f1SDimitry Andric SlotTracker *ST = MachineStorage.get(); 4822fe6060f1SDimitry Andric if (!ST) 4823fe6060f1SDimitry Andric return; 4824fe6060f1SDimitry Andric 4825fe6060f1SDimitry Andric for (auto &I : llvm::make_range(ST->mdn_begin(), ST->mdn_end())) 4826fe6060f1SDimitry Andric if (I.second >= LB && I.second < UB) 4827fe6060f1SDimitry Andric L.push_back(std::make_pair(I.second, I.first)); 4828fe6060f1SDimitry Andric } 4829fe6060f1SDimitry Andric 48300b57cec5SDimitry Andric #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 48310b57cec5SDimitry Andric // Value::dump - allow easy printing of Values from the debugger. 48320b57cec5SDimitry Andric LLVM_DUMP_METHOD 48330b57cec5SDimitry Andric void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; } 48340b57cec5SDimitry Andric 48350b57cec5SDimitry Andric // Type::dump - allow easy printing of Types from the debugger. 48360b57cec5SDimitry Andric LLVM_DUMP_METHOD 48370b57cec5SDimitry Andric void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; } 48380b57cec5SDimitry Andric 48390b57cec5SDimitry Andric // Module::dump() - Allow printing of Modules from the debugger. 48400b57cec5SDimitry Andric LLVM_DUMP_METHOD 48410b57cec5SDimitry Andric void Module::dump() const { 48420b57cec5SDimitry Andric print(dbgs(), nullptr, 48430b57cec5SDimitry Andric /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true); 48440b57cec5SDimitry Andric } 48450b57cec5SDimitry Andric 48460b57cec5SDimitry Andric // Allow printing of Comdats from the debugger. 48470b57cec5SDimitry Andric LLVM_DUMP_METHOD 48480b57cec5SDimitry Andric void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48490b57cec5SDimitry Andric 48500b57cec5SDimitry Andric // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger. 48510b57cec5SDimitry Andric LLVM_DUMP_METHOD 48520b57cec5SDimitry Andric void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48530b57cec5SDimitry Andric 48540b57cec5SDimitry Andric LLVM_DUMP_METHOD 48550b57cec5SDimitry Andric void Metadata::dump() const { dump(nullptr); } 48560b57cec5SDimitry Andric 48570b57cec5SDimitry Andric LLVM_DUMP_METHOD 48580b57cec5SDimitry Andric void Metadata::dump(const Module *M) const { 48590b57cec5SDimitry Andric print(dbgs(), M, /*IsForDebug=*/true); 48600b57cec5SDimitry Andric dbgs() << '\n'; 48610b57cec5SDimitry Andric } 48620b57cec5SDimitry Andric 4863349cc55cSDimitry Andric LLVM_DUMP_METHOD 4864349cc55cSDimitry Andric void MDNode::dumpTree() const { dumpTree(nullptr); } 4865349cc55cSDimitry Andric 4866349cc55cSDimitry Andric LLVM_DUMP_METHOD 4867349cc55cSDimitry Andric void MDNode::dumpTree(const Module *M) const { 4868349cc55cSDimitry Andric printTree(dbgs(), M); 4869349cc55cSDimitry Andric dbgs() << '\n'; 4870349cc55cSDimitry Andric } 4871349cc55cSDimitry Andric 48720b57cec5SDimitry Andric // Allow printing of ModuleSummaryIndex from the debugger. 48730b57cec5SDimitry Andric LLVM_DUMP_METHOD 48740b57cec5SDimitry Andric void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); } 48750b57cec5SDimitry Andric #endif 4876