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