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