LCOV - code coverage report
Current view: top level - lib/IR - AsmWriter.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 2038 2080 98.0 %
Date: 2018-07-13 00:08:38 Functions: 162 162 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- AsmWriter.cpp - Printing LLVM as an assembly file ------------------===//
       2             : //
       3             : //                     The LLVM Compiler Infrastructure
       4             : //
       5             : // This file is distributed under the University of Illinois Open Source
       6             : // License. See LICENSE.TXT for details.
       7             : //
       8             : //===----------------------------------------------------------------------===//
       9             : //
      10             : // This library implements `print` family of functions in classes like
      11             : // Module, Function, Value, etc. In-memory representation of those classes is
      12             : // converted to IR strings.
      13             : //
      14             : // Note that these routines must be extremely tolerant of various errors in the
      15             : // LLVM code, because it can be used for debugging transformations.
      16             : //
      17             : //===----------------------------------------------------------------------===//
      18             : 
      19             : #include "llvm/ADT/APFloat.h"
      20             : #include "llvm/ADT/APInt.h"
      21             : #include "llvm/ADT/ArrayRef.h"
      22             : #include "llvm/ADT/DenseMap.h"
      23             : #include "llvm/ADT/None.h"
      24             : #include "llvm/ADT/Optional.h"
      25             : #include "llvm/ADT/STLExtras.h"
      26             : #include "llvm/ADT/SetVector.h"
      27             : #include "llvm/ADT/SmallString.h"
      28             : #include "llvm/ADT/SmallVector.h"
      29             : #include "llvm/ADT/StringExtras.h"
      30             : #include "llvm/ADT/StringRef.h"
      31             : #include "llvm/ADT/iterator_range.h"
      32             : #include "llvm/BinaryFormat/Dwarf.h"
      33             : #include "llvm/Config/llvm-config.h"
      34             : #include "llvm/IR/Argument.h"
      35             : #include "llvm/IR/AssemblyAnnotationWriter.h"
      36             : #include "llvm/IR/Attributes.h"
      37             : #include "llvm/IR/BasicBlock.h"
      38             : #include "llvm/IR/CFG.h"
      39             : #include "llvm/IR/CallSite.h"
      40             : #include "llvm/IR/CallingConv.h"
      41             : #include "llvm/IR/Comdat.h"
      42             : #include "llvm/IR/Constant.h"
      43             : #include "llvm/IR/Constants.h"
      44             : #include "llvm/IR/DebugInfoMetadata.h"
      45             : #include "llvm/IR/DerivedTypes.h"
      46             : #include "llvm/IR/Function.h"
      47             : #include "llvm/IR/GlobalAlias.h"
      48             : #include "llvm/IR/GlobalIFunc.h"
      49             : #include "llvm/IR/GlobalIndirectSymbol.h"
      50             : #include "llvm/IR/GlobalObject.h"
      51             : #include "llvm/IR/GlobalValue.h"
      52             : #include "llvm/IR/GlobalVariable.h"
      53             : #include "llvm/IR/IRPrintingPasses.h"
      54             : #include "llvm/IR/InlineAsm.h"
      55             : #include "llvm/IR/InstrTypes.h"
      56             : #include "llvm/IR/Instruction.h"
      57             : #include "llvm/IR/Instructions.h"
      58             : #include "llvm/IR/LLVMContext.h"
      59             : #include "llvm/IR/Metadata.h"
      60             : #include "llvm/IR/Module.h"
      61             : #include "llvm/IR/ModuleSlotTracker.h"
      62             : #include "llvm/IR/ModuleSummaryIndex.h"
      63             : #include "llvm/IR/Operator.h"
      64             : #include "llvm/IR/Statepoint.h"
      65             : #include "llvm/IR/Type.h"
      66             : #include "llvm/IR/TypeFinder.h"
      67             : #include "llvm/IR/Use.h"
      68             : #include "llvm/IR/UseListOrder.h"
      69             : #include "llvm/IR/User.h"
      70             : #include "llvm/IR/Value.h"
      71             : #include "llvm/Support/AtomicOrdering.h"
      72             : #include "llvm/Support/Casting.h"
      73             : #include "llvm/Support/Compiler.h"
      74             : #include "llvm/Support/Debug.h"
      75             : #include "llvm/Support/ErrorHandling.h"
      76             : #include "llvm/Support/Format.h"
      77             : #include "llvm/Support/FormattedStream.h"
      78             : #include "llvm/Support/raw_ostream.h"
      79             : #include <algorithm>
      80             : #include <cassert>
      81             : #include <cctype>
      82             : #include <cstddef>
      83             : #include <cstdint>
      84             : #include <iterator>
      85             : #include <memory>
      86             : #include <string>
      87             : #include <tuple>
      88             : #include <utility>
      89             : #include <vector>
      90             : 
      91             : using namespace llvm;
      92             : 
      93             : // Make virtual table appear in this compilation unit.
      94             : AssemblyAnnotationWriter::~AssemblyAnnotationWriter() = default;
      95             : 
      96             : //===----------------------------------------------------------------------===//
      97             : // Helper Functions
      98             : //===----------------------------------------------------------------------===//
      99             : 
     100             : namespace {
     101             : 
     102         754 : struct OrderMap {
     103             :   DenseMap<const Value *, std::pair<unsigned, bool>> IDs;
     104             : 
     105             :   unsigned size() const { return IDs.size(); }
     106       16810 :   std::pair<unsigned, bool> &operator[](const Value *V) { return IDs[V]; }
     107             : 
     108             :   std::pair<unsigned, bool> lookup(const Value *V) const {
     109       35832 :     return IDs.lookup(V);
     110             :   }
     111             : 
     112             :   void index(const Value *V) {
     113             :     // Explicitly sequence get-size and insert-value operations to avoid UB.
     114       13482 :     unsigned ID = IDs.size() + 1;
     115       13482 :     IDs[V].first = ID;
     116             :   }
     117             : };
     118             : 
     119             : } // end anonymous namespace
     120             : 
     121       14880 : static void orderValue(const Value *V, OrderMap &OM) {
     122       14880 :   if (OM.lookup(V).first)
     123             :     return;
     124             : 
     125             :   if (const Constant *C = dyn_cast<Constant>(V))
     126        5176 :     if (C->getNumOperands() && !isa<GlobalValue>(C))
     127        3680 :       for (const Value *Op : C->operands())
     128        1264 :         if (!isa<BasicBlock>(Op) && !isa<GlobalValue>(Op))
     129         936 :           orderValue(Op, OM);
     130             : 
     131             :   // Note: we cannot cache this lookup above, since inserting into the map
     132             :   // changes the map's size, and thus affects the other IDs.
     133             :   OM.index(V);
     134             : }
     135             : 
     136         754 : static OrderMap orderModule(const Module *M) {
     137             :   // This needs to match the order used by ValueEnumerator::ValueEnumerator()
     138             :   // and ValueEnumerator::incorporateFunction().
     139             :   OrderMap OM;
     140             : 
     141        1668 :   for (const GlobalVariable &G : M->globals()) {
     142         914 :     if (G.hasInitializer())
     143             :       if (!isa<GlobalValue>(G.getInitializer()))
     144         690 :         orderValue(G.getInitializer(), OM);
     145         914 :     orderValue(&G, OM);
     146             :   }
     147         898 :   for (const GlobalAlias &A : M->aliases()) {
     148             :     if (!isa<GlobalValue>(A.getAliasee()))
     149          60 :       orderValue(A.getAliasee(), OM);
     150         144 :     orderValue(&A, OM);
     151             :   }
     152         766 :   for (const GlobalIFunc &I : M->ifuncs()) {
     153             :     if (!isa<GlobalValue>(I.getResolver()))
     154           0 :       orderValue(I.getResolver(), OM);
     155          12 :     orderValue(&I, OM);
     156             :   }
     157        2828 :   for (const Function &F : *M) {
     158        4220 :     for (const Use &U : F.operands())
     159          36 :       if (!isa<GlobalValue>(U.get()))
     160          36 :         orderValue(U.get(), OM);
     161             : 
     162        2074 :     orderValue(&F, OM);
     163             : 
     164        2074 :     if (F.isDeclaration())
     165         342 :       continue;
     166             : 
     167        3054 :     for (const Argument &A : F.args())
     168        1322 :       orderValue(&A, OM);
     169        3720 :     for (const BasicBlock &BB : F) {
     170        1988 :       orderValue(&BB, OM);
     171        6968 :       for (const Instruction &I : BB) {
     172       23312 :         for (const Value *Op : I.operands())
     173       11644 :           if ((isa<Constant>(*Op) && !isa<GlobalValue>(*Op)) ||
     174             :               isa<InlineAsm>(*Op))
     175        1724 :             orderValue(Op, OM);
     176        4980 :         orderValue(&I, OM);
     177             :       }
     178             :     }
     179             :   }
     180         754 :   return OM;
     181             : }
     182             : 
     183        1528 : static void predictValueUseListOrderImpl(const Value *V, const Function *F,
     184             :                                          unsigned ID, const OrderMap &OM,
     185             :                                          UseListOrderStack &Stack) {
     186             :   // Predict use-list order for this one.
     187             :   using Entry = std::pair<const Use *, unsigned>;
     188             :   SmallVector<Entry, 64> List;
     189        8130 :   for (const Use &U : V->uses())
     190             :     // Check if this user will be serialized.
     191       10148 :     if (OM.lookup(U.getUser()).first)
     192        4950 :       List.push_back(std::make_pair(&U, List.size()));
     193             : 
     194        1528 :   if (List.size() < 2)
     195             :     // We may have lost some users.
     196             :     return;
     197             : 
     198             :   bool GetsReversed =
     199        2920 :       !isa<GlobalVariable>(V) && !isa<Function>(V) && !isa<BasicBlock>(V);
     200             :   if (auto *BA = dyn_cast<BlockAddress>(V))
     201          32 :     ID = OM.lookup(BA->getBasicBlock()).first;
     202        7923 :   llvm::sort(List.begin(), List.end(), [&](const Entry &L, const Entry &R) {
     203        7923 :     const Use *LU = L.first;
     204        7923 :     const Use *RU = R.first;
     205        7923 :     if (LU == RU)
     206             :       return false;
     207             : 
     208       15846 :     auto LID = OM.lookup(LU->getUser()).first;
     209        7923 :     auto RID = OM.lookup(RU->getUser()).first;
     210             : 
     211             :     // If ID is 4, then expect: 7 6 5 1 2 3.
     212        7923 :     if (LID < RID) {
     213        7923 :       if (GetsReversed)
     214        7347 :         if (RID <= ID)
     215             :           return true;
     216             :       return false;
     217             :     }
     218        3976 :     if (RID < LID) {
     219        3134 :       if (GetsReversed)
     220        2941 :         if (LID <= ID)
     221             :           return false;
     222             :       return true;
     223             :     }
     224             : 
     225             :     // LID and RID are equal, so we have different operands of the same user.
     226             :     // Assume operands are added in order for all instructions.
     227         842 :     if (GetsReversed)
     228         803 :       if (LID <= ID)
     229          12 :         return LU->getOperandNo() < RU->getOperandNo();
     230         830 :     return LU->getOperandNo() > RU->getOperandNo();
     231             :   });
     232             : 
     233        1460 :   if (std::is_sorted(
     234             :           List.begin(), List.end(),
     235             :           [](const Entry &L, const Entry &R) { return L.second < R.second; }))
     236             :     // Order is already correct.
     237             :     return;
     238             : 
     239             :   // Store the shuffle.
     240         855 :   Stack.emplace_back(V, F, List.size());
     241             :   assert(List.size() == Stack.back().Shuffle.size() && "Wrong size");
     242        7213 :   for (size_t I = 0, E = List.size(); I != E; ++I)
     243        6358 :     Stack.back().Shuffle[I] = List[I].second;
     244             : }
     245             : 
     246       16810 : static void predictValueUseListOrder(const Value *V, const Function *F,
     247             :                                      OrderMap &OM, UseListOrderStack &Stack) {
     248             :   auto &IDPair = OM[V];
     249             :   assert(IDPair.first && "Unmapped value");
     250       16810 :   if (IDPair.second)
     251             :     // Already predicted.
     252             :     return;
     253             : 
     254             :   // Do the actual prediction.
     255       13482 :   IDPair.second = true;
     256       18922 :   if (!V->use_empty() && std::next(V->use_begin()) != V->use_end())
     257        1528 :     predictValueUseListOrderImpl(V, F, IDPair.first, OM, Stack);
     258             : 
     259             :   // Recursive descent into constants.
     260             :   if (const Constant *C = dyn_cast<Constant>(V))
     261        5176 :     if (C->getNumOperands()) // Visit GlobalValues.
     262        7464 :       for (const Value *Op : C->operands())
     263        2222 :         if (isa<Constant>(Op)) // Visit GlobalValues.
     264        2190 :           predictValueUseListOrder(Op, F, OM, Stack);
     265             : }
     266             : 
     267         754 : static UseListOrderStack predictUseListOrder(const Module *M) {
     268         754 :   OrderMap OM = orderModule(M);
     269             : 
     270             :   // Use-list orders need to be serialized after all the users have been added
     271             :   // to a value, or else the shuffles will be incomplete.  Store them per
     272             :   // function in a stack.
     273             :   //
     274             :   // Aside from function order, the order of values doesn't matter much here.
     275             :   UseListOrderStack Stack;
     276             : 
     277             :   // We want to visit the functions backward now so we can list function-local
     278             :   // constants in the last Function they're used in.  Module-level constants
     279             :   // have already been visited above.
     280        2828 :   for (const Function &F : make_range(M->rbegin(), M->rend())) {
     281        2074 :     if (F.isDeclaration())
     282         342 :       continue;
     283        3720 :     for (const BasicBlock &BB : F)
     284        1988 :       predictValueUseListOrder(&BB, &F, OM, Stack);
     285        3054 :     for (const Argument &A : F.args())
     286        1322 :       predictValueUseListOrder(&A, &F, OM, Stack);
     287        3720 :     for (const BasicBlock &BB : F)
     288        6968 :       for (const Instruction &I : BB)
     289       23312 :         for (const Value *Op : I.operands())
     290        6676 :           if (isa<Constant>(*Op) || isa<InlineAsm>(*Op)) // Visit GlobalValues.
     291        2228 :             predictValueUseListOrder(Op, &F, OM, Stack);
     292        3720 :     for (const BasicBlock &BB : F)
     293        6968 :       for (const Instruction &I : BB)
     294        4980 :         predictValueUseListOrder(&I, &F, OM, Stack);
     295             :   }
     296             : 
     297             :   // Visit globals last.
     298        1668 :   for (const GlobalVariable &G : M->globals())
     299         914 :     predictValueUseListOrder(&G, nullptr, OM, Stack);
     300        2828 :   for (const Function &F : *M)
     301        2074 :     predictValueUseListOrder(&F, nullptr, OM, Stack);
     302         898 :   for (const GlobalAlias &A : M->aliases())
     303         144 :     predictValueUseListOrder(&A, nullptr, OM, Stack);
     304         766 :   for (const GlobalIFunc &I : M->ifuncs())
     305          12 :     predictValueUseListOrder(&I, nullptr, OM, Stack);
     306        1668 :   for (const GlobalVariable &G : M->globals())
     307         914 :     if (G.hasInitializer())
     308         766 :       predictValueUseListOrder(G.getInitializer(), nullptr, OM, Stack);
     309         898 :   for (const GlobalAlias &A : M->aliases())
     310         144 :     predictValueUseListOrder(A.getAliasee(), nullptr, OM, Stack);
     311         766 :   for (const GlobalIFunc &I : M->ifuncs())
     312          12 :     predictValueUseListOrder(I.getResolver(), nullptr, OM, Stack);
     313        2828 :   for (const Function &F : *M)
     314        4220 :     for (const Use &U : F.operands())
     315          36 :       predictValueUseListOrder(U.get(), nullptr, OM, Stack);
     316             : 
     317         754 :   return Stack;
     318             : }
     319             : 
     320      110419 : static const Module *getModuleFromVal(const Value *V) {
     321             :   if (const Argument *MA = dyn_cast<Argument>(V))
     322        4670 :     return MA->getParent() ? MA->getParent()->getParent() : nullptr;
     323             : 
     324             :   if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
     325       12291 :     return BB->getParent() ? BB->getParent()->getParent() : nullptr;
     326             : 
     327             :   if (const Instruction *I = dyn_cast<Instruction>(V)) {
     328       73969 :     const Function *M = I->getParent() ? I->getParent()->getParent() : nullptr;
     329       73893 :     return M ? M->getParent() : nullptr;
     330             :   }
     331             : 
     332             :   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V))
     333        1165 :     return GV->getParent();
     334             : 
     335             :   if (const auto *MAV = dyn_cast<MetadataAsValue>(V)) {
     336          10 :     for (const User *U : MAV->users())
     337          10 :       if (isa<Instruction>(U))
     338          10 :         if (const Module *M = getModuleFromVal(U))
     339             :           return M;
     340             :     return nullptr;
     341             :   }
     342             : 
     343             :   return nullptr;
     344             : }
     345             : 
     346       13696 : static void PrintCallingConv(unsigned cc, raw_ostream &Out) {
     347       13696 :   switch (cc) {
     348          65 :   default:                         Out << "cc" << cc; break;
     349         407 :   case CallingConv::Fast:          Out << "fastcc"; break;
     350         502 :   case CallingConv::Cold:          Out << "coldcc"; break;
     351           9 :   case CallingConv::WebKit_JS:     Out << "webkit_jscc"; break;
     352           9 :   case CallingConv::AnyReg:        Out << "anyregcc"; break;
     353          23 :   case CallingConv::PreserveMost:  Out << "preserve_mostcc"; break;
     354          15 :   case CallingConv::PreserveAll:   Out << "preserve_allcc"; break;
     355          54 :   case CallingConv::CXX_FAST_TLS:  Out << "cxx_fast_tlscc"; break;
     356          46 :   case CallingConv::GHC:           Out << "ghccc"; break;
     357         143 :   case CallingConv::X86_StdCall:   Out << "x86_stdcallcc"; break;
     358         128 :   case CallingConv::X86_FastCall:  Out << "x86_fastcallcc"; break;
     359        5730 :   case CallingConv::X86_ThisCall:  Out << "x86_thiscallcc"; break;
     360         157 :   case CallingConv::X86_RegCall:   Out << "x86_regcallcc"; break;
     361         123 :   case CallingConv::X86_VectorCall:Out << "x86_vectorcallcc"; break;
     362          33 :   case CallingConv::Intel_OCL_BI:  Out << "intel_ocl_bicc"; break;
     363          34 :   case CallingConv::ARM_APCS:      Out << "arm_apcscc"; break;
     364         603 :   case CallingConv::ARM_AAPCS:     Out << "arm_aapcscc"; break;
     365         710 :   case CallingConv::ARM_AAPCS_VFP: Out << "arm_aapcs_vfpcc"; break;
     366          28 :   case CallingConv::MSP430_INTR:   Out << "msp430_intrcc"; break;
     367          13 :   case CallingConv::AVR_INTR:      Out << "avr_intrcc "; break;
     368          13 :   case CallingConv::AVR_SIGNAL:    Out << "avr_signalcc "; break;
     369          28 :   case CallingConv::PTX_Kernel:    Out << "ptx_kernel"; break;
     370         100 :   case CallingConv::PTX_Device:    Out << "ptx_device"; break;
     371          22 :   case CallingConv::X86_64_SysV:   Out << "x86_64_sysvcc"; break;
     372          35 :   case CallingConv::Win64:         Out << "win64cc"; break;
     373         287 :   case CallingConv::SPIR_FUNC:     Out << "spir_func"; break;
     374         207 :   case CallingConv::SPIR_KERNEL:   Out << "spir_kernel"; break;
     375        2204 :   case CallingConv::Swift:         Out << "swiftcc"; break;
     376          37 :   case CallingConv::X86_INTR:      Out << "x86_intrcc"; break;
     377          14 :   case CallingConv::HHVM:          Out << "hhvmcc"; break;
     378          16 :   case CallingConv::HHVM_C:        Out << "hhvm_ccc"; break;
     379          25 :   case CallingConv::AMDGPU_VS:     Out << "amdgpu_vs"; break;
     380           6 :   case CallingConv::AMDGPU_LS:     Out << "amdgpu_ls"; break;
     381           8 :   case CallingConv::AMDGPU_HS:     Out << "amdgpu_hs"; break;
     382           6 :   case CallingConv::AMDGPU_ES:     Out << "amdgpu_es"; break;
     383          12 :   case CallingConv::AMDGPU_GS:     Out << "amdgpu_gs"; break;
     384         130 :   case CallingConv::AMDGPU_PS:     Out << "amdgpu_ps"; break;
     385          16 :   case CallingConv::AMDGPU_CS:     Out << "amdgpu_cs"; break;
     386        1698 :   case CallingConv::AMDGPU_KERNEL: Out << "amdgpu_kernel"; break;
     387             :   }
     388       13696 : }
     389             : 
     390             : enum PrefixType {
     391             :   GlobalPrefix,
     392             :   ComdatPrefix,
     393             :   LabelPrefix,
     394             :   LocalPrefix,
     395             :   NoPrefix
     396             : };
     397             : 
     398     4689631 : void llvm::printLLVMNameWithoutPrefix(raw_ostream &OS, StringRef Name) {
     399             :   assert(!Name.empty() && "Cannot get empty name!");
     400             : 
     401             :   // Scan the name to see if it needs quotes first.
     402     4689631 :   bool NeedsQuotes = isdigit(static_cast<unsigned char>(Name[0]));
     403             :   if (!NeedsQuotes) {
     404    53945912 :     for (unsigned i = 0, e = Name.size(); i != e; ++i) {
     405             :       // By making this unsigned, the value passed in to isalnum will always be
     406             :       // in the range 0-255.  This is important when building with MSVC because
     407             :       // its implementation will assert.  This situation can arise when dealing
     408             :       // with UTF-8 multibyte characters.
     409    98734412 :       unsigned char C = Name[i];
     410    49367206 :       if (!isalnum(static_cast<unsigned char>(C)) && C != '-' && C != '.' &&
     411             :           C != '_') {
     412             :         NeedsQuotes = true;
     413             :         break;
     414             :       }
     415             :     }
     416             :   }
     417             : 
     418             :   // If we didn't need any quotes, just write out the name in one blast.
     419     4689631 :   if (!NeedsQuotes) {
     420     4578706 :     OS << Name;
     421     4578706 :     return;
     422             :   }
     423             : 
     424             :   // Okay, we need quotes.  Output the quotes and escape any scary characters as
     425             :   // needed.
     426             :   OS << '"';
     427      110925 :   printEscapedString(Name, OS);
     428             :   OS << '"';
     429             : }
     430             : 
     431             : /// Turn the specified name into an 'LLVM name', which is either prefixed with %
     432             : /// (if the string only contains simple characters) or is surrounded with ""'s
     433             : /// (if it has special chars in it). Print it out.
     434     4421379 : static void PrintLLVMName(raw_ostream &OS, StringRef Name, PrefixType Prefix) {
     435     4421379 :   switch (Prefix) {
     436             :   case NoPrefix:
     437             :     break;
     438             :   case GlobalPrefix:
     439             :     OS << '@';
     440             :     break;
     441             :   case ComdatPrefix:
     442             :     OS << '$';
     443             :     break;
     444             :   case LabelPrefix:
     445             :     break;
     446             :   case LocalPrefix:
     447             :     OS << '%';
     448             :     break;
     449             :   }
     450     4688252 :   printLLVMNameWithoutPrefix(OS, Name);
     451     4421379 : }
     452             : 
     453             : /// Turn the specified name into an 'LLVM name', which is either prefixed with %
     454             : /// (if the string only contains simple characters) or is surrounded with ""'s
     455             : /// (if it has special chars in it). Print it out.
     456     3767859 : static void PrintLLVMName(raw_ostream &OS, const Value *V) {
     457     3767859 :   PrintLLVMName(OS, V->getName(),
     458             :                 isa<GlobalValue>(V) ? GlobalPrefix : LocalPrefix);
     459     3767859 : }
     460             : 
     461             : namespace {
     462             : 
     463      237808 : class TypePrinting {
     464             : public:
     465      118904 :   TypePrinting(const Module *M = nullptr) : DeferredM(M) {}
     466             : 
     467             :   TypePrinting(const TypePrinting &) = delete;
     468             :   TypePrinting &operator=(const TypePrinting &) = delete;
     469             : 
     470             :   /// The named types that are used by the current module.
     471             :   TypeFinder &getNamedTypes();
     472             : 
     473             :   /// The numbered types, number to type mapping.
     474             :   std::vector<StructType *> &getNumberedTypes();
     475             : 
     476             :   bool empty();
     477             : 
     478             :   void print(Type *Ty, raw_ostream &OS);
     479             : 
     480             :   void printStructBody(StructType *Ty, raw_ostream &OS);
     481             : 
     482             : private:
     483             :   void incorporateTypes();
     484             : 
     485             :   /// A module to process lazily when needed. Set to nullptr as soon as used.
     486             :   const Module *DeferredM;
     487             : 
     488             :   TypeFinder NamedTypes;
     489             : 
     490             :   // The numbered types, along with their value.
     491             :   DenseMap<StructType *, unsigned> Type2Number;
     492             : 
     493             :   std::vector<StructType *> NumberedTypes;
     494             : };
     495             : 
     496             : } // end anonymous namespace
     497             : 
     498             : TypeFinder &TypePrinting::getNamedTypes() {
     499        6023 :   incorporateTypes();
     500             :   return NamedTypes;
     501             : }
     502             : 
     503        6023 : std::vector<StructType *> &TypePrinting::getNumberedTypes() {
     504        6023 :   incorporateTypes();
     505             : 
     506             :   // We know all the numbers that each type is used and we know that it is a
     507             :   // dense assignment. Convert the map to an index table, if it's not done
     508             :   // already (judging from the sizes):
     509       12046 :   if (NumberedTypes.size() == Type2Number.size())
     510        5567 :     return NumberedTypes;
     511             : 
     512         456 :   NumberedTypes.resize(Type2Number.size());
     513        1869 :   for (const auto &P : Type2Number) {
     514             :     assert(P.second < NumberedTypes.size() && "Didn't get a dense numbering?");
     515             :     assert(!NumberedTypes[P.second] && "Didn't get a unique numbering?");
     516        1914 :     NumberedTypes[P.second] = P.first;
     517             :   }
     518         456 :   return NumberedTypes;
     519             : }
     520             : 
     521             : bool TypePrinting::empty() {
     522       16686 :   incorporateTypes();
     523       27445 :   return NamedTypes.empty() && Type2Number.empty();
     524             : }
     525             : 
     526       48944 : void TypePrinting::incorporateTypes() {
     527       48944 :   if (!DeferredM)
     528             :     return;
     529             : 
     530       16691 :   NamedTypes.run(*DeferredM, false);
     531       16691 :   DeferredM = nullptr;
     532             : 
     533             :   // The list of struct types we got back includes all the struct types, split
     534             :   // the unnamed ones out to a numbering and remove the anonymous structs.
     535             :   unsigned NextNumber = 0;
     536             : 
     537             :   std::vector<StructType*>::iterator NextToUse = NamedTypes.begin(), I, E;
     538       57646 :   for (I = NamedTypes.begin(), E = NamedTypes.end(); I != E; ++I) {
     539       40955 :     StructType *STy = *I;
     540             : 
     541             :     // Ignore anonymous types.
     542       40955 :     if (STy->isLiteral())
     543        9318 :       continue;
     544             : 
     545       63274 :     if (STy->getName().empty())
     546        1932 :       Type2Number[STy] = NextNumber++;
     547             :     else
     548       30671 :       *NextToUse++ = STy;
     549             :   }
     550             : 
     551             :   NamedTypes.erase(NextToUse, NamedTypes.end());
     552             : }
     553             : 
     554             : /// Write the specified type to the specified raw_ostream, making use of type
     555             : /// names or up references to shorten the type name where possible.
     556    10187011 : void TypePrinting::print(Type *Ty, raw_ostream &OS) {
     557    10187011 :   switch (Ty->getTypeID()) {
     558      277243 :   case Type::VoidTyID:      OS << "void"; return;
     559       40271 :   case Type::HalfTyID:      OS << "half"; return;
     560      267709 :   case Type::FloatTyID:     OS << "float"; return;
     561      203096 :   case Type::DoubleTyID:    OS << "double"; return;
     562       12078 :   case Type::X86_FP80TyID:  OS << "x86_fp80"; return;
     563        3705 :   case Type::FP128TyID:     OS << "fp128"; return;
     564        1021 :   case Type::PPC_FP128TyID: OS << "ppc_fp128"; return;
     565      220791 :   case Type::LabelTyID:     OS << "label"; return;
     566       18471 :   case Type::MetadataTyID:  OS << "metadata"; return;
     567        2864 :   case Type::X86_MMXTyID:   OS << "x86_mmx"; return;
     568        2575 :   case Type::TokenTyID:     OS << "token"; return;
     569             :   case Type::IntegerTyID:
     570             :     OS << 'i' << cast<IntegerType>(Ty)->getBitWidth();
     571             :     return;
     572             : 
     573             :   case Type::FunctionTyID: {
     574             :     FunctionType *FTy = cast<FunctionType>(Ty);
     575      200632 :     print(FTy->getReturnType(), OS);
     576      100316 :     OS << " (";
     577      244867 :     for (FunctionType::param_iterator I = FTy->param_begin(),
     578      345183 :          E = FTy->param_end(); I != E; ++I) {
     579      289102 :       if (I != FTy->param_begin())
     580       73133 :         OS << ", ";
     581      144551 :       print(*I, OS);
     582             :     }
     583      100316 :     if (FTy->isVarArg()) {
     584       41726 :       if (FTy->getNumParams()) OS << ", ";
     585       41726 :       OS << "...";
     586             :     }
     587             :     OS << ')';
     588             :     return;
     589             :   }
     590             :   case Type::StructTyID: {
     591             :     StructType *STy = cast<StructType>(Ty);
     592             : 
     593      713766 :     if (STy->isLiteral())
     594       99172 :       return printStructBody(STy, OS);
     595             : 
     596     1229188 :     if (!STy->getName().empty())
     597      594382 :       return PrintLLVMName(OS, STy->getName(), LocalPrefix);
     598             : 
     599       20212 :     incorporateTypes();
     600       20212 :     const auto I = Type2Number.find(STy);
     601       20212 :     if (I != Type2Number.end())
     602       20202 :       OS << '%' << I->second;
     603             :     else  // Not enumerated, print the hex address.
     604          10 :       OS << "%\"type " << STy << '\"';
     605             :     return;
     606             :   }
     607             :   case Type::PointerTyID: {
     608             :     PointerType *PTy = cast<PointerType>(Ty);
     609     2721803 :     print(PTy->getElementType(), OS);
     610     2721803 :     if (unsigned AddressSpace = PTy->getAddressSpace())
     611       28480 :       OS << " addrspace(" << AddressSpace << ')';
     612             :     OS << '*';
     613             :     return;
     614             :   }
     615             :   case Type::ArrayTyID: {
     616             :     ArrayType *ATy = cast<ArrayType>(Ty);
     617      312499 :     OS << '[' << ATy->getNumElements() << " x ";
     618      312499 :     print(ATy->getElementType(), OS);
     619             :     OS << ']';
     620             :     return;
     621             :   }
     622             :   case Type::VectorTyID: {
     623             :     VectorType *PTy = cast<VectorType>(Ty);
     624      936642 :     OS << "<" << PTy->getNumElements() << " x ";
     625      936642 :     print(PTy->getElementType(), OS);
     626             :     OS << '>';
     627             :     return;
     628             :   }
     629             :   }
     630           0 :   llvm_unreachable("Invalid TypeID");
     631             : }
     632             : 
     633      130883 : void TypePrinting::printStructBody(StructType *STy, raw_ostream &OS) {
     634      130883 :   if (STy->isOpaque()) {
     635        1734 :     OS << "opaque";
     636        1734 :     return;
     637             :   }
     638             : 
     639      129149 :   if (STy->isPacked())
     640             :     OS << '<';
     641             : 
     642      129149 :   if (STy->getNumElements() == 0) {
     643         514 :     OS << "{}";
     644             :   } else {
     645      128635 :     StructType::element_iterator I = STy->element_begin();
     646      128635 :     OS << "{ ";
     647      128635 :     print(*I++, OS);
     648      909592 :     for (StructType::element_iterator E = STy->element_end(); I != E; ++I) {
     649      326161 :       OS << ", ";
     650      326161 :       print(*I, OS);
     651             :     }
     652             : 
     653      128635 :     OS << " }";
     654             :   }
     655      129149 :   if (STy->isPacked())
     656             :     OS << '>';
     657             : }
     658             : 
     659             : namespace llvm {
     660             : 
     661             : //===----------------------------------------------------------------------===//
     662             : // SlotTracker Class: Enumerate slot numbers for unnamed values
     663             : //===----------------------------------------------------------------------===//
     664             : /// This class provides computation of slot numbers for LLVM Assembly writing.
     665             : ///
     666      369672 : class SlotTracker {
     667             : public:
     668             :   /// ValueMap - A mapping of Values to slot numbers.
     669             :   using ValueMap = DenseMap<const Value *, unsigned>;
     670             : 
     671             : private:
     672             :   /// TheModule - The module for which we are holding slot numbers.
     673             :   const Module* TheModule;
     674             : 
     675             :   /// TheFunction - The function for which we are holding slot numbers.
     676             :   const Function* TheFunction = nullptr;
     677             :   bool FunctionProcessed = false;
     678             :   bool ShouldInitializeAllMetadata;
     679             : 
     680             :   /// The summary index for which we are holding slot numbers.
     681             :   const ModuleSummaryIndex *TheIndex = nullptr;
     682             : 
     683             :   /// mMap - The slot map for the module level data.
     684             :   ValueMap mMap;
     685             :   unsigned mNext = 0;
     686             : 
     687             :   /// fMap - The slot map for the function level data.
     688             :   ValueMap fMap;
     689             :   unsigned fNext = 0;
     690             : 
     691             :   /// mdnMap - Map for MDNodes.
     692             :   DenseMap<const MDNode*, unsigned> mdnMap;
     693             :   unsigned mdnNext = 0;
     694             : 
     695             :   /// asMap - The slot map for attribute sets.
     696             :   DenseMap<AttributeSet, unsigned> asMap;
     697             :   unsigned asNext = 0;
     698             : 
     699             :   /// ModulePathMap - The slot map for Module paths used in the summary index.
     700             :   StringMap<unsigned> ModulePathMap;
     701             :   unsigned ModulePathNext = 0;
     702             : 
     703             :   /// GUIDMap - The slot map for GUIDs used in the summary index.
     704             :   DenseMap<GlobalValue::GUID, unsigned> GUIDMap;
     705             :   unsigned GUIDNext = 0;
     706             : 
     707             : public:
     708             :   /// Construct from a module.
     709             :   ///
     710             :   /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
     711             :   /// functions, giving correct numbering for metadata referenced only from
     712             :   /// within a function (even if no functions have been initialized).
     713             :   explicit SlotTracker(const Module *M,
     714             :                        bool ShouldInitializeAllMetadata = false);
     715             : 
     716             :   /// Construct from a function, starting out in incorp state.
     717             :   ///
     718             :   /// If \c ShouldInitializeAllMetadata, initializes all metadata in all
     719             :   /// functions, giving correct numbering for metadata referenced only from
     720             :   /// within a function (even if no functions have been initialized).
     721             :   explicit SlotTracker(const Function *F,
     722             :                        bool ShouldInitializeAllMetadata = false);
     723             : 
     724             :   /// Construct from a module summary index.
     725             :   explicit SlotTracker(const ModuleSummaryIndex *Index);
     726             : 
     727             :   SlotTracker(const SlotTracker &) = delete;
     728             :   SlotTracker &operator=(const SlotTracker &) = delete;
     729             : 
     730             :   /// Return the slot number of the specified value in it's type
     731             :   /// plane.  If something is not in the SlotTracker, return -1.
     732             :   int getLocalSlot(const Value *V);
     733             :   int getGlobalSlot(const GlobalValue *V);
     734             :   int getMetadataSlot(const MDNode *N);
     735             :   int getAttributeGroupSlot(AttributeSet AS);
     736             :   int getModulePathSlot(StringRef Path);
     737             :   int getGUIDSlot(GlobalValue::GUID GUID);
     738             : 
     739             :   /// If you'd like to deal with a function instead of just a module, use
     740             :   /// this method to get its data into the SlotTracker.
     741             :   void incorporateFunction(const Function *F) {
     742      242162 :     TheFunction = F;
     743      242162 :     FunctionProcessed = false;
     744             :   }
     745             : 
     746             :   const Function *getFunction() const { return TheFunction; }
     747             : 
     748             :   /// After calling incorporateFunction, use this method to remove the
     749             :   /// most recently incorporated function from the SlotTracker. This
     750             :   /// will reset the state of the machine back to just the module contents.
     751             :   void purgeFunction();
     752             : 
     753             :   /// MDNode map iterators.
     754             :   using mdn_iterator = DenseMap<const MDNode*, unsigned>::iterator;
     755             : 
     756       10222 :   mdn_iterator mdn_begin() { return mdnMap.begin(); }
     757             :   mdn_iterator mdn_end() { return mdnMap.end(); }
     758             :   unsigned mdn_size() const { return mdnMap.size(); }
     759             :   bool mdn_empty() const { return mdnMap.empty(); }
     760             : 
     761             :   /// AttributeSet map iterators.
     762             :   using as_iterator = DenseMap<AttributeSet, unsigned>::iterator;
     763             : 
     764       11807 :   as_iterator as_begin()   { return asMap.begin(); }
     765             :   as_iterator as_end()     { return asMap.end(); }
     766             :   unsigned as_size() const { return asMap.size(); }
     767             :   bool as_empty() const    { return asMap.empty(); }
     768             : 
     769             :   /// GUID map iterators.
     770             :   using guid_iterator = DenseMap<GlobalValue::GUID, unsigned>::iterator;
     771             : 
     772             :   /// These functions do the actual initialization.
     773             :   inline void initializeIfNeeded();
     774             :   void initializeIndexIfNeeded();
     775             : 
     776             :   // Implementation Details
     777             : private:
     778             :   /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
     779             :   void CreateModuleSlot(const GlobalValue *V);
     780             : 
     781             :   /// CreateMetadataSlot - Insert the specified MDNode* into the slot table.
     782             :   void CreateMetadataSlot(const MDNode *N);
     783             : 
     784             :   /// CreateFunctionSlot - Insert the specified Value* into the slot table.
     785             :   void CreateFunctionSlot(const Value *V);
     786             : 
     787             :   /// Insert the specified AttributeSet into the slot table.
     788             :   void CreateAttributeSetSlot(AttributeSet AS);
     789             : 
     790             :   inline void CreateModulePathSlot(StringRef Path);
     791             :   void CreateGUIDSlot(GlobalValue::GUID GUID);
     792             : 
     793             :   /// Add all of the module level global variables (and their initializers)
     794             :   /// and function declarations, but not the contents of those functions.
     795             :   void processModule();
     796             :   void processIndex();
     797             : 
     798             :   /// Add all of the functions arguments, basic blocks, and instructions.
     799             :   void processFunction();
     800             : 
     801             :   /// Add the metadata directly attached to a GlobalObject.
     802             :   void processGlobalObjectMetadata(const GlobalObject &GO);
     803             : 
     804             :   /// Add all of the metadata from a function.
     805             :   void processFunctionMetadata(const Function &F);
     806             : 
     807             :   /// Add all of the metadata from an instruction.
     808             :   void processInstructionMetadata(const Instruction &I);
     809             : };
     810             : 
     811             : } // end namespace llvm
     812             : 
     813       29020 : ModuleSlotTracker::ModuleSlotTracker(SlotTracker &Machine, const Module *M,
     814       29020 :                                      const Function *F)
     815       29020 :     : M(M), F(F), Machine(&Machine) {}
     816             : 
     817      137694 : ModuleSlotTracker::ModuleSlotTracker(const Module *M,
     818      137694 :                                      bool ShouldInitializeAllMetadata)
     819             :     : ShouldCreateStorage(M),
     820      137694 :       ShouldInitializeAllMetadata(ShouldInitializeAllMetadata), M(M) {}
     821             : 
     822             : ModuleSlotTracker::~ModuleSlotTracker() = default;
     823             : 
     824      151601 : SlotTracker *ModuleSlotTracker::getMachine() {
     825      151601 :   if (!ShouldCreateStorage)
     826      110269 :     return Machine;
     827             : 
     828       41332 :   ShouldCreateStorage = false;
     829       41332 :   MachineStorage =
     830      123996 :       llvm::make_unique<SlotTracker>(M, ShouldInitializeAllMetadata);
     831       41332 :   Machine = MachineStorage.get();
     832       41332 :   return Machine;
     833             : }
     834             : 
     835       40998 : void ModuleSlotTracker::incorporateFunction(const Function &F) {
     836             :   // Using getMachine() may lazily create the slot tracker.
     837       40998 :   if (!getMachine())
     838             :     return;
     839             : 
     840             :   // Nothing to do if this is the right function already.
     841       40998 :   if (this->F == &F)
     842             :     return;
     843       40886 :   if (this->F)
     844          54 :     Machine->purgeFunction();
     845       40886 :   Machine->incorporateFunction(&F);
     846       40886 :   this->F = &F;
     847             : }
     848             : 
     849        3397 : int ModuleSlotTracker::getLocalSlot(const Value *V) {
     850             :   assert(F && "No function incorporated");
     851        3397 :   return Machine->getLocalSlot(V);
     852             : }
     853             : 
     854        1390 : static SlotTracker *createSlotTracker(const Value *V) {
     855             :   if (const Argument *FA = dyn_cast<Argument>(V))
     856          16 :     return new SlotTracker(FA->getParent());
     857             : 
     858             :   if (const Instruction *I = dyn_cast<Instruction>(V))
     859        1166 :     if (I->getParent())
     860        1166 :       return new SlotTracker(I->getParent()->getParent());
     861             : 
     862             :   if (const BasicBlock *BB = dyn_cast<BasicBlock>(V))
     863         179 :     return new SlotTracker(BB->getParent());
     864             : 
     865             :   if (const GlobalVariable *GV = dyn_cast<GlobalVariable>(V))
     866          20 :     return new SlotTracker(GV->getParent());
     867             : 
     868             :   if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(V))
     869           0 :     return new SlotTracker(GA->getParent());
     870             : 
     871             :   if (const GlobalIFunc *GIF = dyn_cast<GlobalIFunc>(V))
     872           0 :     return new SlotTracker(GIF->getParent());
     873             : 
     874             :   if (const Function *Func = dyn_cast<Function>(V))
     875           9 :     return new SlotTracker(Func);
     876             : 
     877             :   return nullptr;
     878             : }
     879             : 
     880             : #if 0
     881             : #define ST_DEBUG(X) dbgs() << X
     882             : #else
     883             : #define ST_DEBUG(X)
     884             : #endif
     885             : 
     886             : // Module level constructor. Causes the contents of the Module (sans functions)
     887             : // to be added to the slot table.
     888      121788 : SlotTracker::SlotTracker(const Module *M, bool ShouldInitializeAllMetadata)
     889      852516 :     : TheModule(M), ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
     890             : 
     891             : // Function level constructor. Causes the contents of the Module and the one
     892             : // function provided to be added to the slot table.
     893        1370 : SlotTracker::SlotTracker(const Function *F, bool ShouldInitializeAllMetadata)
     894        1370 :     : TheModule(F ? F->getParent() : nullptr), TheFunction(F),
     895        9590 :       ShouldInitializeAllMetadata(ShouldInitializeAllMetadata) {}
     896             : 
     897          66 : SlotTracker::SlotTracker(const ModuleSummaryIndex *Index)
     898         462 :     : TheModule(nullptr), ShouldInitializeAllMetadata(false), TheIndex(Index) {}
     899             : 
     900     1947505 : inline void SlotTracker::initializeIfNeeded() {
     901     1947505 :   if (TheModule) {
     902       25881 :     processModule();
     903       25881 :     TheModule = nullptr; ///< Prevent re-processing next time we're called.
     904             :   }
     905             : 
     906     1947505 :   if (TheFunction && !FunctionProcessed)
     907      159250 :     processFunction();
     908     1947505 : }
     909             : 
     910         912 : void SlotTracker::initializeIndexIfNeeded() {
     911         912 :   if (!TheIndex)
     912             :     return;
     913          66 :   processIndex();
     914          66 :   TheIndex = nullptr; ///< Prevent re-processing next time we're called.
     915             : }
     916             : 
     917             : // Iterate through all the global variables, functions, and global
     918             : // variable initializers and create slots for them.
     919       25881 : void SlotTracker::processModule() {
     920             :   ST_DEBUG("begin processModule!\n");
     921             : 
     922             :   // Add all of the unnamed global variables to the value table.
     923      137661 :   for (const GlobalVariable &Var : TheModule->globals()) {
     924       85899 :     if (!Var.hasName())
     925        5820 :       CreateModuleSlot(&Var);
     926       85899 :     processGlobalObjectMetadata(Var);
     927             :     auto Attrs = Var.getAttributes();
     928       85899 :     if (Attrs.hasAttributes())
     929          33 :       CreateAttributeSetSlot(Attrs);
     930             :   }
     931             : 
     932       52952 :   for (const GlobalAlias &A : TheModule->aliases()) {
     933        1190 :     if (!A.hasName())
     934           4 :       CreateModuleSlot(&A);
     935             :   }
     936             : 
     937       51839 :   for (const GlobalIFunc &I : TheModule->ifuncs()) {
     938          77 :     if (!I.hasName())
     939           0 :       CreateModuleSlot(&I);
     940             :   }
     941             : 
     942             :   // Add metadata used by named metadata.
     943       72523 :   for (const NamedMDNode &NMD : TheModule->named_metadata()) {
     944       53960 :     for (unsigned i = 0, e = NMD.getNumOperands(); i != e; ++i)
     945       33199 :       CreateMetadataSlot(NMD.getOperand(i));
     946             :   }
     947             : 
     948      390442 :   for (const Function &F : *TheModule) {
     949      338680 :     if (!F.hasName())
     950             :       // Add all the unnamed functions to the table.
     951          38 :       CreateModuleSlot(&F);
     952             : 
     953      338680 :     if (ShouldInitializeAllMetadata)
     954       40987 :       processFunctionMetadata(F);
     955             : 
     956             :     // Add all the function attributes to the table.
     957             :     // FIXME: Add attributes of other objects?
     958      338680 :     AttributeSet FnAttrs = F.getAttributes().getFnAttributes();
     959      338680 :     if (FnAttrs.hasAttributes())
     960      237704 :       CreateAttributeSetSlot(FnAttrs);
     961             :   }
     962             : 
     963             :   ST_DEBUG("end processModule!\n");
     964       25881 : }
     965             : 
     966             : // Process the arguments, basic blocks, and instructions  of a function.
     967      159250 : void SlotTracker::processFunction() {
     968             :   ST_DEBUG("begin processFunction!\n");
     969      159250 :   fNext = 0;
     970             : 
     971             :   // Process function metadata if it wasn't hit at the module-level.
     972      159250 :   if (!ShouldInitializeAllMetadata)
     973      157187 :     processFunctionMetadata(*TheFunction);
     974             : 
     975             :   // Add all the function arguments with no names.
     976      407601 :   for(Function::const_arg_iterator AI = TheFunction->arg_begin(),
     977      566851 :       AE = TheFunction->arg_end(); AI != AE; ++AI)
     978      248351 :     if (!AI->hasName())
     979       82550 :       CreateFunctionSlot(&*AI);
     980             : 
     981             :   ST_DEBUG("Inserting Instructions:\n");
     982             : 
     983             :   // Add all of the basic blocks and instructions with no names.
     984      614483 :   for (auto &BB : *TheFunction) {
     985      295983 :     if (!BB.hasName())
     986       21435 :       CreateFunctionSlot(&BB);
     987             : 
     988     2554321 :     for (auto &I : BB) {
     989     5973550 :       if (!I.getType()->isVoidTy() && !I.hasName())
     990      727359 :         CreateFunctionSlot(&I);
     991             : 
     992             :       // We allow direct calls to any llvm.foo function here, because the
     993             :       // target may not be linked into the optimizer.
     994     2258338 :       if (auto CS = ImmutableCallSite(&I)) {
     995             :         // Add all the call attributes to the table.
     996      168678 :         AttributeSet Attrs = CS.getAttributes().getFnAttributes();
     997      168678 :         if (Attrs.hasAttributes())
     998       54637 :           CreateAttributeSetSlot(Attrs);
     999             :       }
    1000             :     }
    1001             :   }
    1002             : 
    1003      159250 :   FunctionProcessed = true;
    1004             : 
    1005             :   ST_DEBUG("end processFunction!\n");
    1006      159250 : }
    1007             : 
    1008             : // Iterate through all the GUID in the index and create slots for them.
    1009          66 : void SlotTracker::processIndex() {
    1010             :   ST_DEBUG("begin processIndex!\n");
    1011             :   assert(TheIndex);
    1012             : 
    1013             :   // The first block of slots are just the module ids, which start at 0 and are
    1014             :   // assigned consecutively. Since the StringMap iteration order isn't
    1015             :   // guaranteed, use a std::map to order by module ID before assigning slots.
    1016             :   std::map<uint64_t, StringRef> ModuleIdToPathMap;
    1017         208 :   for (auto &ModPath : TheIndex->modulePaths())
    1018         152 :     ModuleIdToPathMap[ModPath.second.first] = ModPath.first();
    1019         142 :   for (auto &ModPair : ModuleIdToPathMap)
    1020          76 :     CreateModulePathSlot(ModPair.second);
    1021             : 
    1022             :   // Start numbering the GUIDs after the module ids.
    1023          66 :   GUIDNext = ModulePathNext;
    1024             : 
    1025         442 :   for (auto &GlobalList : *TheIndex)
    1026         310 :     CreateGUIDSlot(GlobalList.first);
    1027             : 
    1028         143 :   for (auto &TId : TheIndex->typeIds())
    1029          11 :     CreateGUIDSlot(GlobalValue::getGUID(TId.first));
    1030             : 
    1031             :   ST_DEBUG("end processIndex!\n");
    1032          66 : }
    1033             : 
    1034      284073 : void SlotTracker::processGlobalObjectMetadata(const GlobalObject &GO) {
    1035             :   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    1036      284073 :   GO.getAllMetadata(MDs);
    1037      297925 :   for (auto &MD : MDs)
    1038        6926 :     CreateMetadataSlot(MD.second);
    1039      284073 : }
    1040             : 
    1041      198174 : void SlotTracker::processFunctionMetadata(const Function &F) {
    1042      198174 :   processGlobalObjectMetadata(F);
    1043      536228 :   for (auto &BB : F) {
    1044     2698475 :     for (auto &I : BB)
    1045     2360421 :       processInstructionMetadata(I);
    1046             :   }
    1047      198174 : }
    1048             : 
    1049     2360421 : void SlotTracker::processInstructionMetadata(const Instruction &I) {
    1050             :   // Process metadata used directly by intrinsics.
    1051             :   if (const CallInst *CI = dyn_cast<CallInst>(&I))
    1052             :     if (Function *F = CI->getCalledFunction())
    1053      164654 :       if (F->isIntrinsic())
    1054      546140 :         for (auto &Op : I.operands())
    1055             :           if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
    1056       17412 :             if (MDNode *N = dyn_cast<MDNode>(V->getMetadata()))
    1057       11564 :               CreateMetadataSlot(N);
    1058             : 
    1059             :   // Process metadata attached to this instruction.
    1060             :   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    1061             :   I.getAllMetadata(MDs);
    1062     2671861 :   for (auto &MD : MDs)
    1063      155720 :     CreateMetadataSlot(MD.second);
    1064     2360421 : }
    1065             : 
    1066             : /// Clean up after incorporating a function. This is the only way to get out of
    1067             : /// the function incorporation state that affects get*Slot/Create*Slot. Function
    1068             : /// incorporation state is indicated by TheFunction != 0.
    1069      201330 : void SlotTracker::purgeFunction() {
    1070             :   ST_DEBUG("begin purgeFunction!\n");
    1071      201330 :   fMap.clear(); // Simply discard the function level map
    1072      201330 :   TheFunction = nullptr;
    1073      201330 :   FunctionProcessed = false;
    1074             :   ST_DEBUG("end purgeFunction!\n");
    1075      201330 : }
    1076             : 
    1077             : /// getGlobalSlot - Get the slot number of a global value.
    1078       28051 : int SlotTracker::getGlobalSlot(const GlobalValue *V) {
    1079             :   // Check for uninitialized state and do lazy initialization.
    1080       28051 :   initializeIfNeeded();
    1081             : 
    1082             :   // Find the value in the module map
    1083       28051 :   ValueMap::iterator MI = mMap.find(V);
    1084       28051 :   return MI == mMap.end() ? -1 : (int)MI->second;
    1085             : }
    1086             : 
    1087             : /// getMetadataSlot - Get the slot number of a MDNode.
    1088      276801 : int SlotTracker::getMetadataSlot(const MDNode *N) {
    1089             :   // Check for uninitialized state and do lazy initialization.
    1090      276801 :   initializeIfNeeded();
    1091             : 
    1092             :   // Find the MDNode in the module map
    1093      276801 :   mdn_iterator MI = mdnMap.find(N);
    1094      276801 :   return MI == mdnMap.end() ? -1 : (int)MI->second;
    1095             : }
    1096             : 
    1097             : /// getLocalSlot - Get the slot number for a value that is local to a function.
    1098     1429170 : int SlotTracker::getLocalSlot(const Value *V) {
    1099             :   assert(!isa<Constant>(V) && "Can't get a constant or global slot with this!");
    1100             : 
    1101             :   // Check for uninitialized state and do lazy initialization.
    1102     1429170 :   initializeIfNeeded();
    1103             : 
    1104     1429170 :   ValueMap::iterator FI = fMap.find(V);
    1105     1429170 :   return FI == fMap.end() ? -1 : (int)FI->second;
    1106             : }
    1107             : 
    1108      196797 : int SlotTracker::getAttributeGroupSlot(AttributeSet AS) {
    1109             :   // Check for uninitialized state and do lazy initialization.
    1110      196797 :   initializeIfNeeded();
    1111             : 
    1112             :   // Find the AttributeSet in the module map.
    1113      196797 :   as_iterator AI = asMap.find(AS);
    1114      196797 :   return AI == asMap.end() ? -1 : (int)AI->second;
    1115             : }
    1116             : 
    1117         293 : int SlotTracker::getModulePathSlot(StringRef Path) {
    1118             :   // Check for uninitialized state and do lazy initialization.
    1119         293 :   initializeIndexIfNeeded();
    1120             : 
    1121             :   // Find the Module path in the map
    1122         293 :   auto I = ModulePathMap.find(Path);
    1123         879 :   return I == ModulePathMap.end() ? -1 : (int)I->second;
    1124             : }
    1125             : 
    1126         553 : int SlotTracker::getGUIDSlot(GlobalValue::GUID GUID) {
    1127             :   // Check for uninitialized state and do lazy initialization.
    1128         553 :   initializeIndexIfNeeded();
    1129             : 
    1130             :   // Find the GUID in the map
    1131         553 :   guid_iterator I = GUIDMap.find(GUID);
    1132         553 :   return I == GUIDMap.end() ? -1 : (int)I->second;
    1133             : }
    1134             : 
    1135             : /// CreateModuleSlot - Insert the specified GlobalValue* into the slot table.
    1136        5862 : void SlotTracker::CreateModuleSlot(const GlobalValue *V) {
    1137             :   assert(V && "Can't insert a null Value into SlotTracker!");
    1138             :   assert(!V->getType()->isVoidTy() && "Doesn't need a slot!");
    1139             :   assert(!V->hasName() && "Doesn't need a slot!");
    1140             : 
    1141        5862 :   unsigned DestSlot = mNext++;
    1142       11724 :   mMap[V] = DestSlot;
    1143             : 
    1144             :   ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
    1145             :            DestSlot << " [");
    1146             :   // G = Global, F = Function, A = Alias, I = IFunc, o = other
    1147             :   ST_DEBUG((isa<GlobalVariable>(V) ? 'G' :
    1148             :             (isa<Function>(V) ? 'F' :
    1149             :              (isa<GlobalAlias>(V) ? 'A' :
    1150             :               (isa<GlobalIFunc>(V) ? 'I' : 'o')))) << "]\n");
    1151        5862 : }
    1152             : 
    1153             : /// CreateSlot - Create a new slot for the specified value if it has no name.
    1154      831344 : void SlotTracker::CreateFunctionSlot(const Value *V) {
    1155             :   assert(!V->getType()->isVoidTy() && !V->hasName() && "Doesn't need a slot!");
    1156             : 
    1157      831344 :   unsigned DestSlot = fNext++;
    1158     1662688 :   fMap[V] = DestSlot;
    1159             : 
    1160             :   // G = Global, F = Function, o = other
    1161             :   ST_DEBUG("  Inserting value [" << V->getType() << "] = " << V << " slot=" <<
    1162             :            DestSlot << " [o]\n");
    1163      831344 : }
    1164             : 
    1165             : /// CreateModuleSlot - Insert the specified MDNode* into the slot table.
    1166      357519 : void SlotTracker::CreateMetadataSlot(const MDNode *N) {
    1167             :   assert(N && "Can't insert a null Value into SlotTracker!");
    1168             : 
    1169             :   // Don't make slots for DIExpressions. We just print them inline everywhere.
    1170      357519 :   if (isa<DIExpression>(N))
    1171             :     return;
    1172             : 
    1173      351126 :   unsigned DestSlot = mdnNext;
    1174      702252 :   if (!mdnMap.insert(std::make_pair(N, DestSlot)).second)
    1175             :     return;
    1176      126116 :   ++mdnNext;
    1177             : 
    1178             :   // Recursively add any MDNodes referenced by operands.
    1179      512326 :   for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
    1180             :     if (const MDNode *Op = dyn_cast_or_null<MDNode>(N->getOperand(i)))
    1181      150110 :       CreateMetadataSlot(Op);
    1182             : }
    1183             : 
    1184      292374 : void SlotTracker::CreateAttributeSetSlot(AttributeSet AS) {
    1185             :   assert(AS.hasAttributes() && "Doesn't need a slot!");
    1186             : 
    1187      292374 :   as_iterator I = asMap.find(AS);
    1188      292374 :   if (I != asMap.end())
    1189      252527 :     return;
    1190             : 
    1191       39847 :   unsigned DestSlot = asNext++;
    1192       39847 :   asMap[AS] = DestSlot;
    1193             : }
    1194             : 
    1195             : /// Create a new slot for the specified Module
    1196          76 : void SlotTracker::CreateModulePathSlot(StringRef Path) {
    1197         152 :   ModulePathMap[Path] = ModulePathNext++;
    1198          76 : }
    1199             : 
    1200             : /// Create a new slot for the specified GUID
    1201         321 : void SlotTracker::CreateGUIDSlot(GlobalValue::GUID GUID) {
    1202         642 :   GUIDMap[GUID] = GUIDNext++;
    1203         321 : }
    1204             : 
    1205             : //===----------------------------------------------------------------------===//
    1206             : // AsmWriter Implementation
    1207             : //===----------------------------------------------------------------------===//
    1208             : 
    1209             : static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
    1210             :                                    TypePrinting *TypePrinter,
    1211             :                                    SlotTracker *Machine,
    1212             :                                    const Module *Context);
    1213             : 
    1214             : static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
    1215             :                                    TypePrinting *TypePrinter,
    1216             :                                    SlotTracker *Machine, const Module *Context,
    1217             :                                    bool FromValue = false);
    1218             : 
    1219         947 : static void writeAtomicRMWOperation(raw_ostream &Out,
    1220             :                                     AtomicRMWInst::BinOp Op) {
    1221         947 :   switch (Op) {
    1222           0 :   default: Out << " <unknown operation " << Op << ">"; break;
    1223         102 :   case AtomicRMWInst::Xchg: Out << " xchg"; break;
    1224         335 :   case AtomicRMWInst::Add:  Out << " add"; break;
    1225         103 :   case AtomicRMWInst::Sub:  Out << " sub"; break;
    1226          95 :   case AtomicRMWInst::And:  Out << " and"; break;
    1227          44 :   case AtomicRMWInst::Nand: Out << " nand"; break;
    1228          88 :   case AtomicRMWInst::Or:   Out << " or"; break;
    1229          70 :   case AtomicRMWInst::Xor:  Out << " xor"; break;
    1230          29 :   case AtomicRMWInst::Max:  Out << " max"; break;
    1231          31 :   case AtomicRMWInst::Min:  Out << " min"; break;
    1232          25 :   case AtomicRMWInst::UMax: Out << " umax"; break;
    1233          25 :   case AtomicRMWInst::UMin: Out << " umin"; break;
    1234             :   }
    1235         947 : }
    1236             : 
    1237     2327523 : static void WriteOptimizationInfo(raw_ostream &Out, const User *U) {
    1238       94887 :   if (const FPMathOperator *FPO = dyn_cast<const FPMathOperator>(U)) {
    1239             :     // 'Fast' is an abbreviation for all fast-math-flags.
    1240             :     if (FPO->isFast())
    1241        3008 :       Out << " fast";
    1242             :     else {
    1243       91879 :       if (FPO->hasAllowReassoc())
    1244         285 :         Out << " reassoc";
    1245       91879 :       if (FPO->hasNoNaNs())
    1246         237 :         Out << " nnan";
    1247       91879 :       if (FPO->hasNoInfs())
    1248          73 :         Out << " ninf";
    1249       91879 :       if (FPO->hasNoSignedZeros())
    1250          98 :         Out << " nsz";
    1251       91879 :       if (FPO->hasAllowReciprocal())
    1252          71 :         Out << " arcp";
    1253       91879 :       if (FPO->hasAllowContract())
    1254          42 :         Out << " contract";
    1255       91879 :       if (FPO->hasApproxFunc())
    1256          33 :         Out << " afn";
    1257             :     }
    1258             :   }
    1259             : 
    1260       95064 :   if (const OverflowingBinaryOperator *OBO =
    1261             :         dyn_cast<OverflowingBinaryOperator>(U)) {
    1262       95064 :     if (OBO->hasNoUnsignedWrap())
    1263        7624 :       Out << " nuw";
    1264       95064 :     if (OBO->hasNoSignedWrap())
    1265       63403 :       Out << " nsw";
    1266       11873 :   } else if (const PossiblyExactOperator *Div =
    1267             :                dyn_cast<PossiblyExactOperator>(U)) {
    1268       11873 :     if (Div->isExact())
    1269         462 :       Out << " exact";
    1270             :   } else if (const GEPOperator *GEP = dyn_cast<GEPOperator>(U)) {
    1271      190170 :     if (GEP->isInBounds())
    1272      172197 :       Out << " inbounds";
    1273             :   }
    1274     2327523 : }
    1275             : 
    1276     1257150 : static void WriteConstantInternal(raw_ostream &Out, const Constant *CV,
    1277             :                                   TypePrinting &TypePrinter,
    1278             :                                   SlotTracker *Machine,
    1279             :                                   const Module *Context) {
    1280             :   if (const ConstantInt *CI = dyn_cast<ConstantInt>(CV)) {
    1281      973511 :     if (CI->getType()->isIntegerTy(1)) {
    1282       23746 :       Out << (CI->getZExtValue() ? "true" : "false");
    1283       23746 :       return;
    1284             :     }
    1285             :     Out << CI->getValue();
    1286             :     return;
    1287             :   }
    1288             : 
    1289             :   if (const ConstantFP *CFP = dyn_cast<ConstantFP>(CV)) {
    1290             :     const APFloat &APF = CFP->getValueAPF();
    1291       25955 :     if (&APF.getSemantics() == &APFloat::IEEEsingle() ||
    1292        7927 :         &APF.getSemantics() == &APFloat::IEEEdouble()) {
    1293             :       // We would like to output the FP constant value in exponential notation,
    1294             :       // but we cannot do this if doing so will lose precision.  Check here to
    1295             :       // make sure that we only output it in exponential format if we can parse
    1296             :       // the value back and get the same value.
    1297             :       //
    1298             :       bool ignored;
    1299       16878 :       bool isDouble = &APF.getSemantics() == &APFloat::IEEEdouble();
    1300             :       bool isInf = APF.isInfinity();
    1301             :       bool isNaN = APF.isNaN();
    1302       16878 :       if (!isInf && !isNaN) {
    1303       16437 :         double Val = isDouble ? APF.convertToDouble() : APF.convertToFloat();
    1304             :         SmallString<128> StrVal;
    1305       16437 :         APF.toString(StrVal, 6, 0, false);
    1306             :         // Check to make sure that the stringized number is not some string like
    1307             :         // "Inf" or NaN, that atof will accept, but the lexer will not.  Check
    1308             :         // that the string matches the "[-+]?[0-9]" regex.
    1309             :         //
    1310             :         assert(((StrVal[0] >= '0' && StrVal[0] <= '9') ||
    1311             :                 ((StrVal[0] == '-' || StrVal[0] == '+') &&
    1312             :                  (StrVal[1] >= '0' && StrVal[1] <= '9'))) &&
    1313             :                "[-+]?[0-9] regex does not match!");
    1314             :         // Reparse stringized version!
    1315       32874 :         if (APFloat(APFloat::IEEEdouble(), StrVal).convertToDouble() == Val) {
    1316             :           Out << StrVal;
    1317             :           return;
    1318             :         }
    1319             :       }
    1320             :       // Otherwise we could not reparse it to exactly the same value, so we must
    1321             :       // output the string in hexadecimal format!  Note that loading and storing
    1322             :       // floating point types changes the bits of NaNs on some hosts, notably
    1323             :       // x86, so we must not use these types.
    1324             :       static_assert(sizeof(double) == sizeof(uint64_t),
    1325             :                     "assuming that double is 64 bits!");
    1326             :       APFloat apf = APF;
    1327             :       // Floats are represented in ASCII IR as double, convert.
    1328         893 :       if (!isDouble)
    1329         567 :         apf.convert(APFloat::IEEEdouble(), APFloat::rmNearestTiesToEven,
    1330             :                           &ignored);
    1331        2679 :       Out << format_hex(apf.bitcastToAPInt().getZExtValue(), 0, /*Upper=*/true);
    1332             :       return;
    1333             :     }
    1334             : 
    1335             :     // Either half, or some form of long double.
    1336             :     // These appear as a magic letter identifying the type, then a
    1337             :     // fixed number of hex digits.
    1338        1150 :     Out << "0x";
    1339        1150 :     APInt API = APF.bitcastToAPInt();
    1340        1150 :     if (&APF.getSemantics() == &APFloat::x87DoubleExtended()) {
    1341             :       Out << 'K';
    1342         942 :       Out << format_hex_no_prefix(API.getHiBits(16).getZExtValue(), 4,
    1343             :                                   /*Upper=*/true);
    1344         942 :       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
    1345             :                                   /*Upper=*/true);
    1346         314 :       return;
    1347         836 :     } else if (&APF.getSemantics() == &APFloat::IEEEquad()) {
    1348             :       Out << 'L';
    1349         636 :       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
    1350             :                                   /*Upper=*/true);
    1351         636 :       Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
    1352             :                                   /*Upper=*/true);
    1353         624 :     } else if (&APF.getSemantics() == &APFloat::PPCDoubleDouble()) {
    1354             :       Out << 'M';
    1355          81 :       Out << format_hex_no_prefix(API.getLoBits(64).getZExtValue(), 16,
    1356             :                                   /*Upper=*/true);
    1357          81 :       Out << format_hex_no_prefix(API.getHiBits(64).getZExtValue(), 16,
    1358             :                                   /*Upper=*/true);
    1359         597 :     } else if (&APF.getSemantics() == &APFloat::IEEEhalf()) {
    1360             :       Out << 'H';
    1361         597 :       Out << format_hex_no_prefix(API.getZExtValue(), 4,
    1362             :                                   /*Upper=*/true);
    1363             :     } else
    1364           0 :       llvm_unreachable("Unsupported floating point type");
    1365             :     return;
    1366             :   }
    1367             : 
    1368      265611 :   if (isa<ConstantAggregateZero>(CV)) {
    1369       15061 :     Out << "zeroinitializer";
    1370       15061 :     return;
    1371             :   }
    1372             : 
    1373             :   if (const BlockAddress *BA = dyn_cast<BlockAddress>(CV)) {
    1374         311 :     Out << "blockaddress(";
    1375         311 :     WriteAsOperandInternal(Out, BA->getFunction(), &TypePrinter, Machine,
    1376             :                            Context);
    1377         311 :     Out << ", ";
    1378         311 :     WriteAsOperandInternal(Out, BA->getBasicBlock(), &TypePrinter, Machine,
    1379             :                            Context);
    1380         311 :     Out << ")";
    1381         311 :     return;
    1382             :   }
    1383             : 
    1384             :   if (const ConstantArray *CA = dyn_cast<ConstantArray>(CV)) {
    1385        7930 :     Type *ETy = CA->getType()->getElementType();
    1386             :     Out << '[';
    1387        7930 :     TypePrinter.print(ETy, Out);
    1388             :     Out << ' ';
    1389       15860 :     WriteAsOperandInternal(Out, CA->getOperand(0),
    1390             :                            &TypePrinter, Machine,
    1391             :                            Context);
    1392       42108 :     for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
    1393       17089 :       Out << ", ";
    1394       17089 :       TypePrinter.print(ETy, Out);
    1395             :       Out << ' ';
    1396       17089 :       WriteAsOperandInternal(Out, CA->getOperand(i), &TypePrinter, Machine,
    1397             :                              Context);
    1398             :     }
    1399             :     Out << ']';
    1400             :     return;
    1401             :   }
    1402             : 
    1403             :   if (const ConstantDataArray *CA = dyn_cast<ConstantDataArray>(CV)) {
    1404             :     // As a special case, print the array as a string if it is an array of
    1405             :     // i8 with ConstantInt values.
    1406       24281 :     if (CA->isString()) {
    1407       16372 :       Out << "c\"";
    1408       16372 :       printEscapedString(CA->getAsString(), Out);
    1409             :       Out << '"';
    1410             :       return;
    1411             :     }
    1412             : 
    1413        7909 :     Type *ETy = CA->getType()->getElementType();
    1414             :     Out << '[';
    1415        7909 :     TypePrinter.print(ETy, Out);
    1416             :     Out << ' ';
    1417        7909 :     WriteAsOperandInternal(Out, CA->getElementAsConstant(0),
    1418             :                            &TypePrinter, Machine,
    1419             :                            Context);
    1420       19370 :     for (unsigned i = 1, e = CA->getNumElements(); i != e; ++i) {
    1421       11461 :       Out << ", ";
    1422       11461 :       TypePrinter.print(ETy, Out);
    1423             :       Out << ' ';
    1424       11461 :       WriteAsOperandInternal(Out, CA->getElementAsConstant(i), &TypePrinter,
    1425             :                              Machine, Context);
    1426             :     }
    1427             :     Out << ']';
    1428             :     return;
    1429             :   }
    1430             : 
    1431             :   if (const ConstantStruct *CS = dyn_cast<ConstantStruct>(CV)) {
    1432       26898 :     if (CS->getType()->isPacked())
    1433             :       Out << '<';
    1434             :     Out << '{';
    1435             :     unsigned N = CS->getNumOperands();
    1436       26898 :     if (N) {
    1437             :       Out << ' ';
    1438       53796 :       TypePrinter.print(CS->getOperand(0)->getType(), Out);
    1439             :       Out << ' ';
    1440             : 
    1441       26898 :       WriteAsOperandInternal(Out, CS->getOperand(0), &TypePrinter, Machine,
    1442             :                              Context);
    1443             : 
    1444      194250 :       for (unsigned i = 1; i < N; i++) {
    1445       83676 :         Out << ", ";
    1446       83676 :         TypePrinter.print(CS->getOperand(i)->getType(), Out);
    1447             :         Out << ' ';
    1448             : 
    1449       83676 :         WriteAsOperandInternal(Out, CS->getOperand(i), &TypePrinter, Machine,
    1450             :                                Context);
    1451             :       }
    1452             :       Out << ' ';
    1453             :     }
    1454             : 
    1455             :     Out << '}';
    1456       26898 :     if (CS->getType()->isPacked())
    1457             :       Out << '>';
    1458             :     return;
    1459             :   }
    1460             : 
    1461      191130 :   if (isa<ConstantVector>(CV) || isa<ConstantDataVector>(CV)) {
    1462       21626 :     Type *ETy = CV->getType()->getVectorElementType();
    1463             :     Out << '<';
    1464       21626 :     TypePrinter.print(ETy, Out);
    1465             :     Out << ' ';
    1466       21626 :     WriteAsOperandInternal(Out, CV->getAggregateElement(0U), &TypePrinter,
    1467             :                            Machine, Context);
    1468      391412 :     for (unsigned i = 1, e = CV->getType()->getVectorNumElements(); i != e;++i){
    1469      174080 :       Out << ", ";
    1470      174080 :       TypePrinter.print(ETy, Out);
    1471             :       Out << ' ';
    1472      174080 :       WriteAsOperandInternal(Out, CV->getAggregateElement(i), &TypePrinter,
    1473             :                              Machine, Context);
    1474             :     }
    1475             :     Out << '>';
    1476             :     return;
    1477             :   }
    1478             : 
    1479      169504 :   if (isa<ConstantPointerNull>(CV)) {
    1480       22527 :     Out << "null";
    1481       22527 :     return;
    1482             :   }
    1483             : 
    1484      146977 :   if (isa<ConstantTokenNone>(CV)) {
    1485         605 :     Out << "none";
    1486         605 :     return;
    1487             :   }
    1488             : 
    1489      146372 :   if (isa<UndefValue>(CV)) {
    1490       43928 :     Out << "undef";
    1491       43928 :     return;
    1492             :   }
    1493             : 
    1494             :   if (const ConstantExpr *CE = dyn_cast<ConstantExpr>(CV)) {
    1495      102444 :     Out << CE->getOpcodeName();
    1496      102444 :     WriteOptimizationInfo(Out, CE);
    1497      102444 :     if (CE->isCompare())
    1498         228 :       Out << ' ' << CmpInst::getPredicateName(
    1499         228 :                         static_cast<CmpInst::Predicate>(CE->getPredicate()));
    1500      102444 :     Out << " (";
    1501             : 
    1502             :     Optional<unsigned> InRangeOp;
    1503             :     if (const GEPOperator *GEP = dyn_cast<GEPOperator>(CE)) {
    1504       48635 :       TypePrinter.print(GEP->getSourceElementType(), Out);
    1505       48635 :       Out << ", ";
    1506             :       InRangeOp = GEP->getInRangeIndex();
    1507       48635 :       if (InRangeOp)
    1508        1338 :         ++*InRangeOp;
    1509             :     }
    1510             : 
    1511      304779 :     for (User::const_op_iterator OI=CE->op_begin(); OI != CE->op_end(); ++OI) {
    1512      207686 :       if (InRangeOp && unsigned(OI - CE->op_begin()) == *InRangeOp)
    1513        1338 :         Out << "inrange ";
    1514      202335 :       TypePrinter.print((*OI)->getType(), Out);
    1515             :       Out << ' ';
    1516      202335 :       WriteAsOperandInternal(Out, *OI, &TypePrinter, Machine, Context);
    1517      202335 :       if (OI+1 != CE->op_end())
    1518       99891 :         Out << ", ";
    1519             :     }
    1520             : 
    1521      102444 :     if (CE->hasIndices()) {
    1522           3 :       ArrayRef<unsigned> Indices = CE->getIndices();
    1523           6 :       for (unsigned i = 0, e = Indices.size(); i != e; ++i)
    1524           6 :         Out << ", " << Indices[i];
    1525             :     }
    1526             : 
    1527      102444 :     if (CE->isCast()) {
    1528       51767 :       Out << " to ";
    1529       51767 :       TypePrinter.print(CE->getType(), Out);
    1530             :     }
    1531             : 
    1532             :     Out << ')';
    1533             :     return;
    1534             :   }
    1535             : 
    1536           0 :   Out << "<placeholder or erroneous Constant>";
    1537             : }
    1538             : 
    1539       61119 : static void writeMDTuple(raw_ostream &Out, const MDTuple *Node,
    1540             :                          TypePrinting *TypePrinter, SlotTracker *Machine,
    1541             :                          const Module *Context) {
    1542       61119 :   Out << "!{";
    1543       61119 :   for (unsigned mi = 0, me = Node->getNumOperands(); mi != me; ++mi) {
    1544      161020 :     const Metadata *MD = Node->getOperand(mi);
    1545      161020 :     if (!MD)
    1546        1068 :       Out << "null";
    1547             :     else if (auto *MDV = dyn_cast<ValueAsMetadata>(MD)) {
    1548       72866 :       Value *V = MDV->getValue();
    1549       72866 :       TypePrinter->print(V->getType(), Out);
    1550             :       Out << ' ';
    1551       72866 :       WriteAsOperandInternal(Out, V, TypePrinter, Machine, Context);
    1552             :     } else {
    1553       87086 :       WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
    1554             :     }
    1555      161020 :     if (mi + 1 != me)
    1556      101880 :       Out << ", ";
    1557             :   }
    1558             : 
    1559       61119 :   Out << "}";
    1560       61119 : }
    1561             : 
    1562             : namespace {
    1563             : 
    1564             : struct FieldSeparator {
    1565             :   bool Skip = true;
    1566             :   const char *Sep;
    1567             : 
    1568       35954 :   FieldSeparator(const char *Sep = ", ") : Sep(Sep) {}
    1569             : };
    1570             : 
    1571             : raw_ostream &operator<<(raw_ostream &OS, FieldSeparator &FS) {
    1572      171498 :   if (FS.Skip) {
    1573       35942 :     FS.Skip = false;
    1574             :     return OS;
    1575             :   }
    1576      127373 :   return OS << FS.Sep;
    1577             : }
    1578             : 
    1579             : struct MDFieldPrinter {
    1580             :   raw_ostream &Out;
    1581             :   FieldSeparator FS;
    1582             :   TypePrinting *TypePrinter = nullptr;
    1583             :   SlotTracker *Machine = nullptr;
    1584             :   const Module *Context = nullptr;
    1585             : 
    1586        5598 :   explicit MDFieldPrinter(raw_ostream &Out) : Out(Out) {}
    1587             :   MDFieldPrinter(raw_ostream &Out, TypePrinting *TypePrinter,
    1588             :                  SlotTracker *Machine, const Module *Context)
    1589       66310 :       : Out(Out), TypePrinter(TypePrinter), Machine(Machine), Context(Context) {
    1590             :   }
    1591             : 
    1592             :   void printTag(const DINode *N);
    1593             :   void printMacinfoType(const DIMacroNode *N);
    1594             :   void printChecksum(const DIFile::ChecksumInfo<StringRef> &N);
    1595             :   void printString(StringRef Name, StringRef Value,
    1596             :                    bool ShouldSkipEmpty = true);
    1597             :   void printMetadata(StringRef Name, const Metadata *MD,
    1598             :                      bool ShouldSkipNull = true);
    1599             :   template <class IntTy>
    1600             :   void printInt(StringRef Name, IntTy Int, bool ShouldSkipZero = true);
    1601             :   void printBool(StringRef Name, bool Value, Optional<bool> Default = None);
    1602             :   void printDIFlags(StringRef Name, DINode::DIFlags Flags);
    1603             :   template <class IntTy, class Stringifier>
    1604             :   void printDwarfEnum(StringRef Name, IntTy Value, Stringifier toString,
    1605             :                       bool ShouldSkipZero = true);
    1606             :   void printEmissionKind(StringRef Name, DICompileUnit::DebugEmissionKind EK);
    1607             : };
    1608             : 
    1609             : } // end anonymous namespace
    1610             : 
    1611        5279 : void MDFieldPrinter::printTag(const DINode *N) {
    1612       10558 :   Out << FS << "tag: ";
    1613       10558 :   auto Tag = dwarf::TagString(N->getTag());
    1614        5279 :   if (!Tag.empty())
    1615        5274 :     Out << Tag;
    1616             :   else
    1617           5 :     Out << N->getTag();
    1618        5279 : }
    1619             : 
    1620        1073 : void MDFieldPrinter::printMacinfoType(const DIMacroNode *N) {
    1621        2146 :   Out << FS << "type: ";
    1622        2146 :   auto Type = dwarf::MacinfoString(N->getMacinfoType());
    1623        1073 :   if (!Type.empty())
    1624        1073 :     Out << Type;
    1625             :   else
    1626           0 :     Out << N->getMacinfoType();
    1627        1073 : }
    1628             : 
    1629          70 : void MDFieldPrinter::printChecksum(
    1630             :     const DIFile::ChecksumInfo<StringRef> &Checksum) {
    1631         210 :   Out << FS << "checksumkind: " << Checksum.getKindAsString();
    1632          70 :   printString("checksum", Checksum.Value, /* ShouldSkipEmpty */ false);
    1633          70 : }
    1634             : 
    1635       33653 : void MDFieldPrinter::printString(StringRef Name, StringRef Value,
    1636             :                                  bool ShouldSkipEmpty) {
    1637       63914 :   if (ShouldSkipEmpty && Value.empty())
    1638             :     return;
    1639             : 
    1640       45762 :   Out << FS << Name << ": \"";
    1641       22881 :   printEscapedString(Value, Out);
    1642       22881 :   Out << "\"";
    1643             : }
    1644             : 
    1645       64864 : static void writeMetadataAsOperand(raw_ostream &Out, const Metadata *MD,
    1646             :                                    TypePrinting *TypePrinter,
    1647             :                                    SlotTracker *Machine,
    1648             :                                    const Module *Context) {
    1649       64864 :   if (!MD) {
    1650         529 :     Out << "null";
    1651         529 :     return;
    1652             :   }
    1653       64335 :   WriteAsOperandInternal(Out, MD, TypePrinter, Machine, Context);
    1654             : }
    1655             : 
    1656      115158 : void MDFieldPrinter::printMetadata(StringRef Name, const Metadata *MD,
    1657             :                                    bool ShouldSkipNull) {
    1658      115158 :   if (ShouldSkipNull && !MD)
    1659             :     return;
    1660             : 
    1661      129698 :   Out << FS << Name << ": ";
    1662       64849 :   writeMetadataAsOperand(Out, MD, TypePrinter, Machine, Context);
    1663             : }
    1664             : 
    1665             : template <class IntTy>
    1666       80233 : void MDFieldPrinter::printInt(StringRef Name, IntTy Int, bool ShouldSkipZero) {
    1667       80233 :   if (ShouldSkipZero && !Int)
    1668             :     return;
    1669             : 
    1670       81342 :   Out << FS << Name << ": " << Int;
    1671             : }
    1672             : 
    1673       17845 : void MDFieldPrinter::printBool(StringRef Name, bool Value,
    1674             :                                Optional<bool> Default) {
    1675       17845 :   if (Default && Value == *Default)
    1676             :     return;
    1677       28782 :   Out << FS << Name << ": " << (Value ? "true" : "false");
    1678             : }
    1679             : 
    1680       16143 : void MDFieldPrinter::printDIFlags(StringRef Name, DINode::DIFlags Flags) {
    1681       16143 :   if (!Flags)
    1682        8884 :     return;
    1683             : 
    1684       14518 :   Out << FS << Name << ": ";
    1685             : 
    1686             :   SmallVector<DINode::DIFlags, 8> SplitFlags;
    1687        7259 :   auto Extra = DINode::splitFlags(Flags, SplitFlags);
    1688             : 
    1689             :   FieldSeparator FlagsFS(" | ");
    1690       25485 :   for (auto F : SplitFlags) {
    1691        9113 :     auto StringF = DINode::getFlagString(F);
    1692             :     assert(!StringF.empty() && "Expected valid flag");
    1693       18226 :     Out << FlagsFS << StringF;
    1694             :   }
    1695        7259 :   if (Extra || SplitFlags.empty())
    1696           0 :     Out << FlagsFS << Extra;
    1697             : }
    1698             : 
    1699        1111 : void MDFieldPrinter::printEmissionKind(StringRef Name,
    1700             :                                        DICompileUnit::DebugEmissionKind EK) {
    1701        2222 :   Out << FS << Name << ": " << DICompileUnit::emissionKindString(EK);
    1702        1111 : }
    1703             : 
    1704             : template <class IntTy, class Stringifier>
    1705        9791 : void MDFieldPrinter::printDwarfEnum(StringRef Name, IntTy Value,
    1706             :                                     Stringifier toString, bool ShouldSkipZero) {
    1707        9791 :   if (!Value)
    1708        7294 :     return;
    1709             : 
    1710        4994 :   Out << FS << Name << ": ";
    1711        2497 :   auto S = toString(Value);
    1712        2497 :   if (!S.empty())
    1713        2497 :     Out << S;
    1714             :   else
    1715           0 :     Out << Value;
    1716             : }
    1717             : 
    1718          20 : static void writeGenericDINode(raw_ostream &Out, const GenericDINode *N,
    1719             :                                TypePrinting *TypePrinter, SlotTracker *Machine,
    1720             :                                const Module *Context) {
    1721          20 :   Out << "!GenericDINode(";
    1722             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1723          20 :   Printer.printTag(N);
    1724          20 :   Printer.printString("header", N->getHeader());
    1725          20 :   if (N->getNumDwarfOperands()) {
    1726           5 :     Out << Printer.FS << "operands: {";
    1727             :     FieldSeparator IFS;
    1728          35 :     for (auto &I : N->dwarf_operands()) {
    1729             :       Out << IFS;
    1730          15 :       writeMetadataAsOperand(Out, I, TypePrinter, Machine, Context);
    1731             :     }
    1732           5 :     Out << "}";
    1733             :   }
    1734          20 :   Out << ")";
    1735          20 : }
    1736             : 
    1737       11371 : static void writeDILocation(raw_ostream &Out, const DILocation *DL,
    1738             :                             TypePrinting *TypePrinter, SlotTracker *Machine,
    1739             :                             const Module *Context) {
    1740       11371 :   Out << "!DILocation(";
    1741             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1742             :   // Always output the line, since 0 is a relevant and important value for it.
    1743       22742 :   Printer.printInt("line", DL->getLine(), /* ShouldSkipZero */ false);
    1744       22742 :   Printer.printInt("column", DL->getColumn());
    1745       11371 :   Printer.printMetadata("scope", DL->getRawScope(), /* ShouldSkipNull */ false);
    1746       11371 :   Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
    1747       11371 :   Out << ")";
    1748       11371 : }
    1749             : 
    1750         283 : static void writeDISubrange(raw_ostream &Out, const DISubrange *N,
    1751             :                             TypePrinting *TypePrinter, SlotTracker *Machine,
    1752             :                             const Module *Context) {
    1753         283 :   Out << "!DISubrange(";
    1754             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1755         566 :   if (auto *CE = N->getCount().dyn_cast<ConstantInt*>())
    1756         263 :     Printer.printInt("count", CE->getSExtValue(), /* ShouldSkipZero */ false);
    1757             :   else
    1758          40 :     Printer.printMetadata("count", N->getCount().dyn_cast<DIVariable*>(),
    1759             :                           /*ShouldSkipNull */ false);
    1760         566 :   Printer.printInt("lowerBound", N->getLowerBound());
    1761         283 :   Out << ")";
    1762         283 : }
    1763             : 
    1764          88 : static void writeDIEnumerator(raw_ostream &Out, const DIEnumerator *N,
    1765             :                               TypePrinting *, SlotTracker *, const Module *) {
    1766          88 :   Out << "!DIEnumerator(";
    1767             :   MDFieldPrinter Printer(Out);
    1768          88 :   Printer.printString("name", N->getName(), /* ShouldSkipEmpty */ false);
    1769          88 :   if (N->isUnsigned()) {
    1770          31 :     auto Value = static_cast<uint64_t>(N->getValue());
    1771          31 :     Printer.printInt("value", Value, /* ShouldSkipZero */ false);
    1772          31 :     Printer.printBool("isUnsigned", true);
    1773             :   } else {
    1774         114 :     Printer.printInt("value", N->getValue(), /* ShouldSkipZero */ false);
    1775             :   }
    1776          88 :   Out << ")";
    1777          88 : }
    1778             : 
    1779        1094 : static void writeDIBasicType(raw_ostream &Out, const DIBasicType *N,
    1780             :                              TypePrinting *, SlotTracker *, const Module *) {
    1781        1094 :   Out << "!DIBasicType(";
    1782             :   MDFieldPrinter Printer(Out);
    1783        1094 :   if (N->getTag() != dwarf::DW_TAG_base_type)
    1784           8 :     Printer.printTag(N);
    1785        1094 :   Printer.printString("name", N->getName());
    1786        2188 :   Printer.printInt("size", N->getSizeInBits());
    1787        2188 :   Printer.printInt("align", N->getAlignInBits());
    1788        2188 :   Printer.printDwarfEnum("encoding", N->getEncoding(),
    1789             :                          dwarf::AttributeEncodingString);
    1790        1094 :   Out << ")";
    1791        1094 : }
    1792             : 
    1793        3527 : static void writeDIDerivedType(raw_ostream &Out, const DIDerivedType *N,
    1794             :                                TypePrinting *TypePrinter, SlotTracker *Machine,
    1795             :                                const Module *Context) {
    1796        3527 :   Out << "!DIDerivedType(";
    1797             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1798        3527 :   Printer.printTag(N);
    1799        3527 :   Printer.printString("name", N->getName());
    1800        3527 :   Printer.printMetadata("scope", N->getRawScope());
    1801        3527 :   Printer.printMetadata("file", N->getRawFile());
    1802        7054 :   Printer.printInt("line", N->getLine());
    1803        3527 :   Printer.printMetadata("baseType", N->getRawBaseType(),
    1804             :                         /* ShouldSkipNull */ false);
    1805        7054 :   Printer.printInt("size", N->getSizeInBits());
    1806        7054 :   Printer.printInt("align", N->getAlignInBits());
    1807        7054 :   Printer.printInt("offset", N->getOffsetInBits());
    1808        7054 :   Printer.printDIFlags("flags", N->getFlags());
    1809        3527 :   Printer.printMetadata("extraData", N->getRawExtraData());
    1810        3527 :   if (const auto &DWARFAddressSpace = N->getDWARFAddressSpace())
    1811          25 :     Printer.printInt("dwarfAddressSpace", *DWARFAddressSpace,
    1812             :                      /* ShouldSkipZero */ false);
    1813        3527 :   Out << ")";
    1814        3527 : }
    1815             : 
    1816        1635 : static void writeDICompositeType(raw_ostream &Out, const DICompositeType *N,
    1817             :                                  TypePrinting *TypePrinter,
    1818             :                                  SlotTracker *Machine, const Module *Context) {
    1819        1635 :   Out << "!DICompositeType(";
    1820             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1821        1635 :   Printer.printTag(N);
    1822        1635 :   Printer.printString("name", N->getName());
    1823        1635 :   Printer.printMetadata("scope", N->getRawScope());
    1824        1635 :   Printer.printMetadata("file", N->getRawFile());
    1825        3270 :   Printer.printInt("line", N->getLine());
    1826        1635 :   Printer.printMetadata("baseType", N->getRawBaseType());
    1827        3270 :   Printer.printInt("size", N->getSizeInBits());
    1828        3270 :   Printer.printInt("align", N->getAlignInBits());
    1829        3270 :   Printer.printInt("offset", N->getOffsetInBits());
    1830        3270 :   Printer.printDIFlags("flags", N->getFlags());
    1831        1635 :   Printer.printMetadata("elements", N->getRawElements());
    1832        3270 :   Printer.printDwarfEnum("runtimeLang", N->getRuntimeLang(),
    1833             :                          dwarf::LanguageString);
    1834        1635 :   Printer.printMetadata("vtableHolder", N->getRawVTableHolder());
    1835        1635 :   Printer.printMetadata("templateParams", N->getRawTemplateParams());
    1836        1635 :   Printer.printString("identifier", N->getIdentifier());
    1837        1635 :   Printer.printMetadata("discriminator", N->getRawDiscriminator());
    1838        1635 :   Out << ")";
    1839        1635 : }
    1840             : 
    1841        1979 : static void writeDISubroutineType(raw_ostream &Out, const DISubroutineType *N,
    1842             :                                   TypePrinting *TypePrinter,
    1843             :                                   SlotTracker *Machine, const Module *Context) {
    1844        1979 :   Out << "!DISubroutineType(";
    1845             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1846        3958 :   Printer.printDIFlags("flags", N->getFlags());
    1847        3958 :   Printer.printDwarfEnum("cc", N->getCC(), dwarf::ConventionString);
    1848        1979 :   Printer.printMetadata("types", N->getRawTypeArray(),
    1849             :                         /* ShouldSkipNull */ false);
    1850        1979 :   Out << ")";
    1851        1979 : }
    1852             : 
    1853        1617 : static void writeDIFile(raw_ostream &Out, const DIFile *N, TypePrinting *,
    1854             :                         SlotTracker *, const Module *) {
    1855        1617 :   Out << "!DIFile(";
    1856             :   MDFieldPrinter Printer(Out);
    1857        1617 :   Printer.printString("filename", N->getFilename(),
    1858             :                       /* ShouldSkipEmpty */ false);
    1859        1617 :   Printer.printString("directory", N->getDirectory(),
    1860             :                       /* ShouldSkipEmpty */ false);
    1861             :   // Print all values for checksum together, or not at all.
    1862        1617 :   if (N->getChecksum())
    1863          70 :     Printer.printChecksum(*N->getChecksum());
    1864        3234 :   Printer.printString("source", N->getSource().getValueOr(StringRef()),
    1865             :                       /* ShouldSkipEmpty */ true);
    1866        1617 :   Out << ")";
    1867        1617 : }
    1868             : 
    1869        1111 : static void writeDICompileUnit(raw_ostream &Out, const DICompileUnit *N,
    1870             :                                TypePrinting *TypePrinter, SlotTracker *Machine,
    1871             :                                const Module *Context) {
    1872        1111 :   Out << "!DICompileUnit(";
    1873             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1874        2222 :   Printer.printDwarfEnum("language", N->getSourceLanguage(),
    1875             :                          dwarf::LanguageString, /* ShouldSkipZero */ false);
    1876        1111 :   Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
    1877        1111 :   Printer.printString("producer", N->getProducer());
    1878        2222 :   Printer.printBool("isOptimized", N->isOptimized());
    1879        1111 :   Printer.printString("flags", N->getFlags());
    1880        2222 :   Printer.printInt("runtimeVersion", N->getRuntimeVersion(),
    1881             :                    /* ShouldSkipZero */ false);
    1882        1111 :   Printer.printString("splitDebugFilename", N->getSplitDebugFilename());
    1883        2222 :   Printer.printEmissionKind("emissionKind", N->getEmissionKind());
    1884        1111 :   Printer.printMetadata("enums", N->getRawEnumTypes());
    1885        1111 :   Printer.printMetadata("retainedTypes", N->getRawRetainedTypes());
    1886        1111 :   Printer.printMetadata("globals", N->getRawGlobalVariables());
    1887        1111 :   Printer.printMetadata("imports", N->getRawImportedEntities());
    1888        1111 :   Printer.printMetadata("macros", N->getRawMacros());
    1889        2222 :   Printer.printInt("dwoId", N->getDWOId());
    1890        2222 :   Printer.printBool("splitDebugInlining", N->getSplitDebugInlining(), true);
    1891        2222 :   Printer.printBool("debugInfoForProfiling", N->getDebugInfoForProfiling(),
    1892             :                     false);
    1893        2222 :   Printer.printBool("gnuPubnames", N->getGnuPubnames(), false);
    1894        1111 :   Out << ")";
    1895        1111 : }
    1896             : 
    1897        3972 : static void writeDISubprogram(raw_ostream &Out, const DISubprogram *N,
    1898             :                               TypePrinting *TypePrinter, SlotTracker *Machine,
    1899             :                               const Module *Context) {
    1900        3972 :   Out << "!DISubprogram(";
    1901             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1902        3972 :   Printer.printString("name", N->getName());
    1903        3972 :   Printer.printString("linkageName", N->getLinkageName());
    1904        3972 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    1905        3972 :   Printer.printMetadata("file", N->getRawFile());
    1906        7944 :   Printer.printInt("line", N->getLine());
    1907        3972 :   Printer.printMetadata("type", N->getRawType());
    1908        7944 :   Printer.printBool("isLocal", N->isLocalToUnit());
    1909        7944 :   Printer.printBool("isDefinition", N->isDefinition());
    1910        7944 :   Printer.printInt("scopeLine", N->getScopeLine());
    1911        3972 :   Printer.printMetadata("containingType", N->getRawContainingType());
    1912        3972 :   Printer.printDwarfEnum("virtuality", N->getVirtuality(),
    1913             :                          dwarf::VirtualityString);
    1914        7849 :   if (N->getVirtuality() != dwarf::DW_VIRTUALITY_none ||
    1915        3877 :       N->getVirtualIndex() != 0)
    1916         516 :     Printer.printInt("virtualIndex", N->getVirtualIndex(), false);
    1917        7944 :   Printer.printInt("thisAdjustment", N->getThisAdjustment());
    1918        7944 :   Printer.printDIFlags("flags", N->getFlags());
    1919        7944 :   Printer.printBool("isOptimized", N->isOptimized());
    1920        3972 :   Printer.printMetadata("unit", N->getRawUnit());
    1921        3972 :   Printer.printMetadata("templateParams", N->getRawTemplateParams());
    1922        3972 :   Printer.printMetadata("declaration", N->getRawDeclaration());
    1923        3972 :   Printer.printMetadata("retainedNodes", N->getRawRetainedNodes());
    1924        3972 :   Printer.printMetadata("thrownTypes", N->getRawThrownTypes());
    1925        3972 :   Out << ")";
    1926        3972 : }
    1927             : 
    1928         825 : static void writeDILexicalBlock(raw_ostream &Out, const DILexicalBlock *N,
    1929             :                                 TypePrinting *TypePrinter, SlotTracker *Machine,
    1930             :                                 const Module *Context) {
    1931         825 :   Out << "!DILexicalBlock(";
    1932             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1933         825 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    1934         825 :   Printer.printMetadata("file", N->getRawFile());
    1935        1650 :   Printer.printInt("line", N->getLine());
    1936        1650 :   Printer.printInt("column", N->getColumn());
    1937         825 :   Out << ")";
    1938         825 : }
    1939             : 
    1940         247 : static void writeDILexicalBlockFile(raw_ostream &Out,
    1941             :                                     const DILexicalBlockFile *N,
    1942             :                                     TypePrinting *TypePrinter,
    1943             :                                     SlotTracker *Machine,
    1944             :                                     const Module *Context) {
    1945         247 :   Out << "!DILexicalBlockFile(";
    1946             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1947         247 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    1948         247 :   Printer.printMetadata("file", N->getRawFile());
    1949         494 :   Printer.printInt("discriminator", N->getDiscriminator(),
    1950             :                    /* ShouldSkipZero */ false);
    1951         247 :   Out << ")";
    1952         247 : }
    1953             : 
    1954         142 : static void writeDINamespace(raw_ostream &Out, const DINamespace *N,
    1955             :                              TypePrinting *TypePrinter, SlotTracker *Machine,
    1956             :                              const Module *Context) {
    1957         142 :   Out << "!DINamespace(";
    1958             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1959         142 :   Printer.printString("name", N->getName());
    1960         142 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    1961         284 :   Printer.printBool("exportSymbols", N->getExportSymbols(), false);
    1962         142 :   Out << ")";
    1963         142 : }
    1964             : 
    1965        1073 : static void writeDIMacro(raw_ostream &Out, const DIMacro *N,
    1966             :                          TypePrinting *TypePrinter, SlotTracker *Machine,
    1967             :                          const Module *Context) {
    1968        1073 :   Out << "!DIMacro(";
    1969             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1970        1073 :   Printer.printMacinfoType(N);
    1971        2146 :   Printer.printInt("line", N->getLine());
    1972        1073 :   Printer.printString("name", N->getName());
    1973        1073 :   Printer.printString("value", N->getValue());
    1974        1073 :   Out << ")";
    1975        1073 : }
    1976             : 
    1977          35 : static void writeDIMacroFile(raw_ostream &Out, const DIMacroFile *N,
    1978             :                              TypePrinting *TypePrinter, SlotTracker *Machine,
    1979             :                              const Module *Context) {
    1980          35 :   Out << "!DIMacroFile(";
    1981             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1982          70 :   Printer.printInt("line", N->getLine());
    1983          35 :   Printer.printMetadata("file", N->getRawFile(), /* ShouldSkipNull */ false);
    1984          35 :   Printer.printMetadata("nodes", N->getRawElements());
    1985          35 :   Out << ")";
    1986          35 : }
    1987             : 
    1988          39 : static void writeDIModule(raw_ostream &Out, const DIModule *N,
    1989             :                           TypePrinting *TypePrinter, SlotTracker *Machine,
    1990             :                           const Module *Context) {
    1991          39 :   Out << "!DIModule(";
    1992             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    1993          39 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    1994          39 :   Printer.printString("name", N->getName());
    1995          39 :   Printer.printString("configMacros", N->getConfigurationMacros());
    1996          39 :   Printer.printString("includePath", N->getIncludePath());
    1997          39 :   Printer.printString("isysroot", N->getISysRoot());
    1998          39 :   Out << ")";
    1999          39 : }
    2000             : 
    2001             : 
    2002         155 : static void writeDITemplateTypeParameter(raw_ostream &Out,
    2003             :                                          const DITemplateTypeParameter *N,
    2004             :                                          TypePrinting *TypePrinter,
    2005             :                                          SlotTracker *Machine,
    2006             :                                          const Module *Context) {
    2007         155 :   Out << "!DITemplateTypeParameter(";
    2008             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2009         155 :   Printer.printString("name", N->getName());
    2010         155 :   Printer.printMetadata("type", N->getRawType(), /* ShouldSkipNull */ false);
    2011         155 :   Out << ")";
    2012         155 : }
    2013             : 
    2014         270 : static void writeDITemplateValueParameter(raw_ostream &Out,
    2015             :                                           const DITemplateValueParameter *N,
    2016             :                                           TypePrinting *TypePrinter,
    2017             :                                           SlotTracker *Machine,
    2018             :                                           const Module *Context) {
    2019         270 :   Out << "!DITemplateValueParameter(";
    2020             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2021         270 :   if (N->getTag() != dwarf::DW_TAG_template_value_parameter)
    2022          13 :     Printer.printTag(N);
    2023         270 :   Printer.printString("name", N->getName());
    2024         270 :   Printer.printMetadata("type", N->getRawType());
    2025         270 :   Printer.printMetadata("value", N->getValue(), /* ShouldSkipNull */ false);
    2026         270 :   Out << ")";
    2027         270 : }
    2028             : 
    2029         656 : static void writeDIGlobalVariable(raw_ostream &Out, const DIGlobalVariable *N,
    2030             :                                   TypePrinting *TypePrinter,
    2031             :                                   SlotTracker *Machine, const Module *Context) {
    2032         656 :   Out << "!DIGlobalVariable(";
    2033             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2034         656 :   Printer.printString("name", N->getName());
    2035         656 :   Printer.printString("linkageName", N->getLinkageName());
    2036         656 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    2037         656 :   Printer.printMetadata("file", N->getRawFile());
    2038        1312 :   Printer.printInt("line", N->getLine());
    2039         656 :   Printer.printMetadata("type", N->getRawType());
    2040        1312 :   Printer.printBool("isLocal", N->isLocalToUnit());
    2041        1312 :   Printer.printBool("isDefinition", N->isDefinition());
    2042         656 :   Printer.printMetadata("declaration", N->getRawStaticDataMemberDeclaration());
    2043        1312 :   Printer.printInt("align", N->getAlignInBits());
    2044         656 :   Out << ")";
    2045         656 : }
    2046             : 
    2047        5030 : static void writeDILocalVariable(raw_ostream &Out, const DILocalVariable *N,
    2048             :                                  TypePrinting *TypePrinter,
    2049             :                                  SlotTracker *Machine, const Module *Context) {
    2050        5030 :   Out << "!DILocalVariable(";
    2051             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2052        5030 :   Printer.printString("name", N->getName());
    2053        5030 :   Printer.printInt("arg", N->getArg());
    2054        5030 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    2055        5030 :   Printer.printMetadata("file", N->getRawFile());
    2056       10060 :   Printer.printInt("line", N->getLine());
    2057        5030 :   Printer.printMetadata("type", N->getRawType());
    2058       10060 :   Printer.printDIFlags("flags", N->getFlags());
    2059       10060 :   Printer.printInt("align", N->getAlignInBits());
    2060        5030 :   Out << ")";
    2061        5030 : }
    2062             : 
    2063          13 : static void writeDILabel(raw_ostream &Out, const DILabel *N,
    2064             :                          TypePrinting *TypePrinter,
    2065             :                          SlotTracker *Machine, const Module *Context) {
    2066          13 :   Out << "!DILabel(";
    2067             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2068          13 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    2069          13 :   Printer.printString("name", N->getName());
    2070          13 :   Printer.printMetadata("file", N->getRawFile());
    2071          26 :   Printer.printInt("line", N->getLine());
    2072          13 :   Out << ")";
    2073          13 : }
    2074             : 
    2075        6515 : static void writeDIExpression(raw_ostream &Out, const DIExpression *N,
    2076             :                               TypePrinting *TypePrinter, SlotTracker *Machine,
    2077             :                               const Module *Context) {
    2078        6515 :   Out << "!DIExpression(";
    2079             :   FieldSeparator FS;
    2080        6515 :   if (N->isValid()) {
    2081       13849 :     for (auto I = N->expr_op_begin(), E = N->expr_op_end(); I != E; ++I) {
    2082         827 :       auto OpStr = dwarf::OperationEncodingString(I->getOp());
    2083             :       assert(!OpStr.empty() && "Expected valid opcode");
    2084             : 
    2085         827 :       Out << FS << OpStr;
    2086        1374 :       for (unsigned A = 0, AE = I->getNumArgs(); A != AE; ++A)
    2087         547 :         Out << FS << I->getArg(A);
    2088             :     }
    2089             :   } else {
    2090          34 :     for (const auto &I : N->getElements())
    2091          15 :       Out << FS << I;
    2092             :   }
    2093        6515 :   Out << ")";
    2094        6515 : }
    2095             : 
    2096         644 : static void writeDIGlobalVariableExpression(raw_ostream &Out,
    2097             :                                             const DIGlobalVariableExpression *N,
    2098             :                                             TypePrinting *TypePrinter,
    2099             :                                             SlotTracker *Machine,
    2100             :                                             const Module *Context) {
    2101         644 :   Out << "!DIGlobalVariableExpression(";
    2102             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2103         644 :   Printer.printMetadata("var", N->getVariable());
    2104         644 :   Printer.printMetadata("expr", N->getExpression());
    2105         644 :   Out << ")";
    2106         644 : }
    2107             : 
    2108          52 : static void writeDIObjCProperty(raw_ostream &Out, const DIObjCProperty *N,
    2109             :                                 TypePrinting *TypePrinter, SlotTracker *Machine,
    2110             :                                 const Module *Context) {
    2111          52 :   Out << "!DIObjCProperty(";
    2112             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2113          52 :   Printer.printString("name", N->getName());
    2114          52 :   Printer.printMetadata("file", N->getRawFile());
    2115         104 :   Printer.printInt("line", N->getLine());
    2116          52 :   Printer.printString("setter", N->getSetterName());
    2117          52 :   Printer.printString("getter", N->getGetterName());
    2118         104 :   Printer.printInt("attributes", N->getAttributes());
    2119          52 :   Printer.printMetadata("type", N->getRawType());
    2120          52 :   Out << ")";
    2121          52 : }
    2122             : 
    2123          76 : static void writeDIImportedEntity(raw_ostream &Out, const DIImportedEntity *N,
    2124             :                                   TypePrinting *TypePrinter,
    2125             :                                   SlotTracker *Machine, const Module *Context) {
    2126          76 :   Out << "!DIImportedEntity(";
    2127             :   MDFieldPrinter Printer(Out, TypePrinter, Machine, Context);
    2128          76 :   Printer.printTag(N);
    2129          76 :   Printer.printString("name", N->getName());
    2130          76 :   Printer.printMetadata("scope", N->getRawScope(), /* ShouldSkipNull */ false);
    2131          76 :   Printer.printMetadata("entity", N->getRawEntity());
    2132          76 :   Printer.printMetadata("file", N->getRawFile());
    2133         152 :   Printer.printInt("line", N->getLine());
    2134          76 :   Out << ")";
    2135          76 : }
    2136             : 
    2137       97073 : static void WriteMDNodeBodyInternal(raw_ostream &Out, const MDNode *Node,
    2138             :                                     TypePrinting *TypePrinter,
    2139             :                                     SlotTracker *Machine,
    2140             :                                     const Module *Context) {
    2141       97073 :   if (Node->isDistinct())
    2142       18223 :     Out << "distinct ";
    2143       78850 :   else if (Node->isTemporary())
    2144           1 :     Out << "<temporary!> "; // Handle broken code.
    2145             : 
    2146      194146 :   switch (Node->getMetadataID()) {
    2147           0 :   default:
    2148           0 :     llvm_unreachable("Expected uniquable MDNode");
    2149             : #define HANDLE_MDNODE_LEAF(CLASS)                                              \
    2150             :   case Metadata::CLASS##Kind:                                                  \
    2151             :     write##CLASS(Out, cast<CLASS>(Node), TypePrinter, Machine, Context);       \
    2152             :     break;
    2153             : #include "llvm/IR/Metadata.def"
    2154             :   }
    2155       97073 : }
    2156             : 
    2157             : // Full implementation of printing a Value as an operand with support for
    2158             : // TypePrinting, etc.
    2159     4896565 : static void WriteAsOperandInternal(raw_ostream &Out, const Value *V,
    2160             :                                    TypePrinting *TypePrinter,
    2161             :                                    SlotTracker *Machine,
    2162             :                                    const Module *Context) {
    2163     4896565 :   if (V->hasName()) {
    2164     2850686 :     PrintLLVMName(Out, V);
    2165     2850686 :     return;
    2166             :   }
    2167             : 
    2168             :   const Constant *CV = dyn_cast<Constant>(V);
    2169             :   if (CV && !isa<GlobalValue>(CV)) {
    2170             :     assert(TypePrinter && "Constants require TypePrinting!");
    2171     1257049 :     WriteConstantInternal(Out, CV, *TypePrinter, Machine, Context);
    2172     1257049 :     return;
    2173             :   }
    2174             : 
    2175             :   if (const InlineAsm *IA = dyn_cast<InlineAsm>(V)) {
    2176        2218 :     Out << "asm ";
    2177        2218 :     if (IA->hasSideEffects())
    2178        1467 :       Out << "sideeffect ";
    2179        2218 :     if (IA->isAlignStack())
    2180          10 :       Out << "alignstack ";
    2181             :     // We don't emit the AD_ATT dialect as it's the assumed default.
    2182        2218 :     if (IA->getDialect() == InlineAsm::AD_Intel)
    2183         149 :       Out << "inteldialect ";
    2184             :     Out << '"';
    2185        2218 :     printEscapedString(IA->getAsmString(), Out);
    2186        2218 :     Out << "\", \"";
    2187        2218 :     printEscapedString(IA->getConstraintString(), Out);
    2188             :     Out << '"';
    2189             :     return;
    2190             :   }
    2191             : 
    2192             :   if (auto *MD = dyn_cast<MetadataAsValue>(V)) {
    2193       16367 :     WriteAsOperandInternal(Out, MD->getMetadata(), TypePrinter, Machine,
    2194             :                            Context, /* FromValue */ true);
    2195       16367 :     return;
    2196             :   }
    2197             : 
    2198             :   char Prefix = '%';
    2199             :   int Slot;
    2200             :   // If we have a SlotTracker, use it.
    2201      770245 :   if (Machine) {
    2202             :     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
    2203       28022 :       Slot = Machine->getGlobalSlot(GV);
    2204             :       Prefix = '@';
    2205             :     } else {
    2206      741852 :       Slot = Machine->getLocalSlot(V);
    2207             : 
    2208             :       // If the local value didn't succeed, then we may be referring to a value
    2209             :       // from a different function.  Translate it, as this can happen when using
    2210             :       // address of blocks.
    2211      741852 :       if (Slot == -1)
    2212        1019 :         if ((Machine = createSlotTracker(V))) {
    2213        1019 :           Slot = Machine->getLocalSlot(V);
    2214        1019 :           delete Machine;
    2215             :         }
    2216             :     }
    2217         371 :   } else if ((Machine = createSlotTracker(V))) {
    2218             :     // Otherwise, create one to get the # and then destroy it.
    2219             :     if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
    2220          29 :       Slot = Machine->getGlobalSlot(GV);
    2221             :       Prefix = '@';
    2222             :     } else {
    2223         342 :       Slot = Machine->getLocalSlot(V);
    2224             :     }
    2225         371 :     delete Machine;
    2226             :     Machine = nullptr;
    2227             :   } else {
    2228             :     Slot = -1;
    2229             :   }
    2230             : 
    2231      770245 :   if (Slot != -1)
    2232             :     Out << Prefix << Slot;
    2233             :   else
    2234           2 :     Out << "<badref>";
    2235             : }
    2236             : 
    2237      299530 : static void WriteAsOperandInternal(raw_ostream &Out, const Metadata *MD,
    2238             :                                    TypePrinting *TypePrinter,
    2239             :                                    SlotTracker *Machine, const Module *Context,
    2240             :                                    bool FromValue) {
    2241             :   // Write DIExpressions inline when used as a value. Improves readability of
    2242             :   // debug info intrinsics.
    2243             :   if (const DIExpression *Expr = dyn_cast<DIExpression>(MD)) {
    2244        6466 :     writeDIExpression(Out, Expr, TypePrinter, Machine, Context);
    2245        6466 :     return;
    2246             :   }
    2247             : 
    2248             :   if (const MDNode *N = dyn_cast<MDNode>(MD)) {
    2249      489582 :     std::unique_ptr<SlotTracker> MachineStorage;
    2250      244791 :     if (!Machine) {
    2251           5 :       MachineStorage = make_unique<SlotTracker>(Context);
    2252             :       Machine = MachineStorage.get();
    2253             :     }
    2254      244791 :     int Slot = Machine->getMetadataSlot(N);
    2255      244791 :     if (Slot == -1)
    2256             :       // Give the pointer value instead of "badref", since this comes up all
    2257             :       // the time when debugging.
    2258          10 :       Out << "<" << N << ">";
    2259             :     else
    2260             :       Out << '!' << Slot;
    2261             :     return;
    2262             :   }
    2263             : 
    2264             :   if (const MDString *MDS = dyn_cast<MDString>(MD)) {
    2265       42574 :     Out << "!\"";
    2266       42574 :     printEscapedString(MDS->getString(), Out);
    2267             :     Out << '"';
    2268             :     return;
    2269             :   }
    2270             : 
    2271             :   auto *V = cast<ValueAsMetadata>(MD);
    2272             :   assert(TypePrinter && "TypePrinter required for metadata values");
    2273             :   assert((FromValue || !isa<LocalAsMetadata>(V)) &&
    2274             :          "Unexpected function-local metadata outside of value argument");
    2275             : 
    2276        5699 :   TypePrinter->print(V->getValue()->getType(), Out);
    2277             :   Out << ' ';
    2278        5699 :   WriteAsOperandInternal(Out, V->getValue(), TypePrinter, Machine, Context);
    2279             : }
    2280             : 
    2281             : namespace {
    2282             : 
    2283      102438 : class AssemblyWriter {
    2284             :   formatted_raw_ostream &Out;
    2285             :   const Module *TheModule = nullptr;
    2286             :   const ModuleSummaryIndex *TheIndex = nullptr;
    2287             :   std::unique_ptr<SlotTracker> SlotTrackerStorage;
    2288             :   SlotTracker &Machine;
    2289             :   TypePrinting TypePrinter;
    2290             :   AssemblyAnnotationWriter *AnnotationWriter = nullptr;
    2291             :   SetVector<const Comdat *> Comdats;
    2292             :   bool IsForDebug;
    2293             :   bool ShouldPreserveUseListOrder;
    2294             :   UseListOrderStack UseListOrders;
    2295             :   SmallVector<StringRef, 8> MDNames;
    2296             :   /// Synchronization scope names registered with LLVMContext.
    2297             :   SmallVector<StringRef, 8> SSNs;
    2298             :   DenseMap<const GlobalValueSummary *, GlobalValue::GUID> SummaryToGUIDMap;
    2299             : 
    2300             : public:
    2301             :   /// Construct an AssemblyWriter with an external SlotTracker
    2302             :   AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac, const Module *M,
    2303             :                  AssemblyAnnotationWriter *AAW, bool IsForDebug,
    2304             :                  bool ShouldPreserveUseListOrder = false);
    2305             : 
    2306             :   AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
    2307             :                  const ModuleSummaryIndex *Index, bool IsForDebug);
    2308             : 
    2309             :   void printMDNodeBody(const MDNode *MD);
    2310             :   void printNamedMDNode(const NamedMDNode *NMD);
    2311             : 
    2312             :   void printModule(const Module *M);
    2313             : 
    2314             :   void writeOperand(const Value *Op, bool PrintType);
    2315             :   void writeParamOperand(const Value *Operand, AttributeSet Attrs);
    2316             :   void writeOperandBundles(ImmutableCallSite CS);
    2317             :   void writeSyncScope(const LLVMContext &Context,
    2318             :                       SyncScope::ID SSID);
    2319             :   void writeAtomic(const LLVMContext &Context,
    2320             :                    AtomicOrdering Ordering,
    2321             :                    SyncScope::ID SSID);
    2322             :   void writeAtomicCmpXchg(const LLVMContext &Context,
    2323             :                           AtomicOrdering SuccessOrdering,
    2324             :                           AtomicOrdering FailureOrdering,
    2325             :                           SyncScope::ID SSID);
    2326             : 
    2327             :   void writeAllMDNodes();
    2328             :   void writeMDNode(unsigned Slot, const MDNode *Node);
    2329             :   void writeAllAttributeGroups();
    2330             : 
    2331             :   void printTypeIdentities();
    2332             :   void printGlobal(const GlobalVariable *GV);
    2333             :   void printIndirectSymbol(const GlobalIndirectSymbol *GIS);
    2334             :   void printComdat(const Comdat *C);
    2335             :   void printFunction(const Function *F);
    2336             :   void printArgument(const Argument *FA, AttributeSet Attrs);
    2337             :   void printBasicBlock(const BasicBlock *BB);
    2338             :   void printInstructionLine(const Instruction &I);
    2339             :   void printInstruction(const Instruction &I);
    2340             : 
    2341             :   void printUseListOrder(const UseListOrder &Order);
    2342             :   void printUseLists(const Function *F);
    2343             : 
    2344             :   void printModuleSummaryIndex();
    2345             :   void printSummaryInfo(unsigned Slot, const ValueInfo &VI);
    2346             :   void printSummary(const GlobalValueSummary &Summary);
    2347             :   void printAliasSummary(const AliasSummary *AS);
    2348             :   void printGlobalVarSummary(const GlobalVarSummary *GS);
    2349             :   void printFunctionSummary(const FunctionSummary *FS);
    2350             :   void printTypeIdSummary(const TypeIdSummary &TIS);
    2351             :   void printTypeTestResolution(const TypeTestResolution &TTRes);
    2352             :   void printArgs(const std::vector<uint64_t> &Args);
    2353             :   void printWPDRes(const WholeProgramDevirtResolution &WPDRes);
    2354             :   void printTypeIdInfo(const FunctionSummary::TypeIdInfo &TIDInfo);
    2355             :   void printVFuncId(const FunctionSummary::VFuncId VFId);
    2356             :   void
    2357             :   printNonConstVCalls(const std::vector<FunctionSummary::VFuncId> VCallList,
    2358             :                       const char *Tag);
    2359             :   void
    2360             :   printConstVCalls(const std::vector<FunctionSummary::ConstVCall> VCallList,
    2361             :                    const char *Tag);
    2362             : 
    2363             : private:
    2364             :   /// Print out metadata attachments.
    2365             :   void printMetadataAttachments(
    2366             :       const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
    2367             :       StringRef Separator);
    2368             : 
    2369             :   // printInfoComment - Print a little comment after the instruction indicating
    2370             :   // which slot it occupies.
    2371             :   void printInfoComment(const Value &V);
    2372             : 
    2373             :   // printGCRelocateComment - print comment after call to the gc.relocate
    2374             :   // intrinsic indicating base and derived pointer names.
    2375             :   void printGCRelocateComment(const GCRelocateInst &Relocate);
    2376             : };
    2377             : 
    2378             : } // end anonymous namespace
    2379             : 
    2380       51153 : AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
    2381             :                                const Module *M, AssemblyAnnotationWriter *AAW,
    2382       51153 :                                bool IsForDebug, bool ShouldPreserveUseListOrder)
    2383             :     : Out(o), TheModule(M), Machine(Mac), TypePrinter(M), AnnotationWriter(AAW),
    2384             :       IsForDebug(IsForDebug),
    2385      204612 :       ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {
    2386       51153 :   if (!TheModule)
    2387             :     return;
    2388             :   for (const GlobalObject &GO : TheModule->global_objects())
    2389     1310741 :     if (const Comdat *C = GO.getComdat())
    2390       28150 :       Comdats.insert(C);
    2391             : }
    2392             : 
    2393          66 : AssemblyWriter::AssemblyWriter(formatted_raw_ostream &o, SlotTracker &Mac,
    2394          66 :                                const ModuleSummaryIndex *Index, bool IsForDebug)
    2395             :     : Out(o), TheIndex(Index), Machine(Mac), TypePrinter(/*Module=*/nullptr),
    2396         330 :       IsForDebug(IsForDebug), ShouldPreserveUseListOrder(false) {}
    2397             : 
    2398     3256135 : void AssemblyWriter::writeOperand(const Value *Operand, bool PrintType) {
    2399     3256135 :   if (!Operand) {
    2400           0 :     Out << "<null operand!>";
    2401           0 :     return;
    2402             :   }
    2403     3256135 :   if (PrintType) {
    2404     1749838 :     TypePrinter.print(Operand->getType(), Out);
    2405     1749838 :     Out << ' ';
    2406             :   }
    2407     3256135 :   WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
    2408             : }
    2409             : 
    2410        3633 : void AssemblyWriter::writeSyncScope(const LLVMContext &Context,
    2411             :                                     SyncScope::ID SSID) {
    2412        3633 :   switch (SSID) {
    2413             :   case SyncScope::System: {
    2414             :     break;
    2415             :   }
    2416         423 :   default: {
    2417         423 :     if (SSNs.empty())
    2418          38 :       Context.getSyncScopeNames(SSNs);
    2419             : 
    2420         423 :     Out << " syncscope(\"";
    2421         846 :     printEscapedString(SSNs[SSID], Out);
    2422         423 :     Out << "\")";
    2423         423 :     break;
    2424             :   }
    2425             :   }
    2426        3633 : }
    2427             : 
    2428        2940 : void AssemblyWriter::writeAtomic(const LLVMContext &Context,
    2429             :                                  AtomicOrdering Ordering,
    2430             :                                  SyncScope::ID SSID) {
    2431        2940 :   if (Ordering == AtomicOrdering::NotAtomic)
    2432             :     return;
    2433             : 
    2434        2940 :   writeSyncScope(Context, SSID);
    2435        5880 :   Out << " " << toIRString(Ordering);
    2436             : }
    2437             : 
    2438         693 : void AssemblyWriter::writeAtomicCmpXchg(const LLVMContext &Context,
    2439             :                                         AtomicOrdering SuccessOrdering,
    2440             :                                         AtomicOrdering FailureOrdering,
    2441             :                                         SyncScope::ID SSID) {
    2442             :   assert(SuccessOrdering != AtomicOrdering::NotAtomic &&
    2443             :          FailureOrdering != AtomicOrdering::NotAtomic);
    2444             : 
    2445         693 :   writeSyncScope(Context, SSID);
    2446        1386 :   Out << " " << toIRString(SuccessOrdering);
    2447        1386 :   Out << " " << toIRString(FailureOrdering);
    2448         693 : }
    2449             : 
    2450      389898 : void AssemblyWriter::writeParamOperand(const Value *Operand,
    2451             :                                        AttributeSet Attrs) {
    2452      389898 :   if (!Operand) {
    2453           0 :     Out << "<null operand!>";
    2454           0 :     return;
    2455             :   }
    2456             : 
    2457             :   // Print the type
    2458      389898 :   TypePrinter.print(Operand->getType(), Out);
    2459             :   // Print parameter attributes list
    2460      389898 :   if (Attrs.hasAttributes())
    2461       81168 :     Out << ' ' << Attrs.getAsString();
    2462      389898 :   Out << ' ';
    2463             :   // Print the operand
    2464      389898 :   WriteAsOperandInternal(Out, Operand, &TypePrinter, &Machine, TheModule);
    2465             : }
    2466             : 
    2467      178415 : void AssemblyWriter::writeOperandBundles(ImmutableCallSite CS) {
    2468      178415 :   if (!CS.hasOperandBundles())
    2469             :     return;
    2470             : 
    2471        1159 :   Out << " [ ";
    2472             : 
    2473             :   bool FirstBundle = true;
    2474        2407 :   for (unsigned i = 0, e = CS.getNumOperandBundles(); i != e; ++i) {
    2475        1248 :     OperandBundleUse BU = CS.getOperandBundleAt(i);
    2476             : 
    2477        1248 :     if (!FirstBundle)
    2478          89 :       Out << ", ";
    2479             :     FirstBundle = false;
    2480             : 
    2481        1248 :     Out << '"';
    2482        2496 :     printEscapedString(BU.getTagName(), Out);
    2483        1248 :     Out << '"';
    2484             : 
    2485        1248 :     Out << '(';
    2486             : 
    2487             :     bool FirstInput = true;
    2488        3674 :     for (const auto &Input : BU.Inputs) {
    2489        1213 :       if (!FirstInput)
    2490         338 :         Out << ", ";
    2491             :       FirstInput = false;
    2492             : 
    2493        1213 :       TypePrinter.print(Input->getType(), Out);
    2494        1213 :       Out << " ";
    2495        1213 :       WriteAsOperandInternal(Out, Input, &TypePrinter, &Machine, TheModule);
    2496             :     }
    2497             : 
    2498        1248 :     Out << ')';
    2499             :   }
    2500             : 
    2501        1159 :   Out << " ]";
    2502             : }
    2503             : 
    2504       16686 : void AssemblyWriter::printModule(const Module *M) {
    2505       16686 :   Machine.initializeIfNeeded();
    2506             : 
    2507       16686 :   if (ShouldPreserveUseListOrder)
    2508        1508 :     UseListOrders = predictUseListOrder(M);
    2509             : 
    2510       33372 :   if (!M->getModuleIdentifier().empty() &&
    2511             :       // Don't print the ID if it will start a new line (which would
    2512             :       // require a comment char before it).
    2513       16686 :       M->getModuleIdentifier().find('\n') == std::string::npos)
    2514       33372 :     Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
    2515             : 
    2516       16686 :   if (!M->getSourceFileName().empty()) {
    2517       16686 :     Out << "source_filename = \"";
    2518       33372 :     printEscapedString(M->getSourceFileName(), Out);
    2519       16686 :     Out << "\"\n";
    2520             :   }
    2521             : 
    2522             :   const std::string &DL = M->getDataLayoutStr();
    2523       16686 :   if (!DL.empty())
    2524       23850 :     Out << "target datalayout = \"" << DL << "\"\n";
    2525       16686 :   if (!M->getTargetTriple().empty())
    2526       22310 :     Out << "target triple = \"" << M->getTargetTriple() << "\"\n";
    2527             : 
    2528       16686 :   if (!M->getModuleInlineAsm().empty()) {
    2529         107 :     Out << '\n';
    2530             : 
    2531             :     // Split the string into lines, to make it easier to read the .ll file.
    2532             :     StringRef Asm = M->getModuleInlineAsm();
    2533             :     do {
    2534             :       StringRef Front;
    2535         332 :       std::tie(Front, Asm) = Asm.split('\n');
    2536             : 
    2537             :       // We found a newline, print the portion of the asm string from the
    2538             :       // last newline up to this newline.
    2539         332 :       Out << "module asm \"";
    2540         332 :       printEscapedString(Front, Out);
    2541         332 :       Out << "\"\n";
    2542         332 :     } while (!Asm.empty());
    2543             :   }
    2544             : 
    2545       16686 :   printTypeIdentities();
    2546             : 
    2547             :   // Output all comdats.
    2548       16686 :   if (!Comdats.empty())
    2549        2906 :     Out << '\n';
    2550       41723 :   for (const Comdat *C : Comdats) {
    2551       25037 :     printComdat(C);
    2552       25037 :     if (C != Comdats.back())
    2553       22131 :       Out << '\n';
    2554             :   }
    2555             : 
    2556             :   // Output all globals.
    2557       16686 :   if (!M->global_empty()) Out << '\n';
    2558       99909 :   for (const GlobalVariable &GV : M->globals()) {
    2559       83223 :     printGlobal(&GV); Out << '\n';
    2560             :   }
    2561             : 
    2562             :   // Output all aliases.
    2563       16686 :   if (!M->alias_empty()) Out << "\n";
    2564       17853 :   for (const GlobalAlias &GA : M->aliases())
    2565        1167 :     printIndirectSymbol(&GA);
    2566             : 
    2567             :   // Output all ifuncs.
    2568       16686 :   if (!M->ifunc_empty()) Out << "\n";
    2569       16763 :   for (const GlobalIFunc &GI : M->ifuncs())
    2570          77 :     printIndirectSymbol(&GI);
    2571             : 
    2572             :   // Output global use-lists.
    2573       16686 :   printUseLists(nullptr);
    2574             : 
    2575             :   // Output all of the functions.
    2576      217605 :   for (const Function &F : *M)
    2577      200919 :     printFunction(&F);
    2578             :   assert(UseListOrders.empty() && "All use-lists should have been consumed");
    2579             : 
    2580             :   // Output all attribute groups.
    2581       33372 :   if (!Machine.as_empty()) {
    2582       11807 :     Out << '\n';
    2583       11807 :     writeAllAttributeGroups();
    2584             :   }
    2585             : 
    2586             :   // Output named metadata.
    2587       16686 :   if (!M->named_metadata_empty()) Out << '\n';
    2588             : 
    2589       36602 :   for (const NamedMDNode &Node : M->named_metadata())
    2590       19916 :     printNamedMDNode(&Node);
    2591             : 
    2592             :   // Output metadata.
    2593       33372 :   if (!Machine.mdn_empty()) {
    2594       10222 :     Out << '\n';
    2595       10222 :     writeAllMDNodes();
    2596             :   }
    2597       16686 : }
    2598             : 
    2599          66 : void AssemblyWriter::printModuleSummaryIndex() {
    2600             :   assert(TheIndex);
    2601          66 :   Machine.initializeIndexIfNeeded();
    2602             : 
    2603          66 :   Out << "\n";
    2604             : 
    2605             :   // Print module path entries. To print in order, add paths to a vector
    2606             :   // indexed by module slot.
    2607          66 :   std::vector<std::pair<std::string, ModuleHash>> moduleVec;
    2608          66 :   std::string RegularLTOModuleName = "[Regular LTO]";
    2609          66 :   moduleVec.resize(TheIndex->modulePaths().size());
    2610         208 :   for (auto &ModPath : TheIndex->modulePaths())
    2611         228 :     moduleVec[Machine.getModulePathSlot(ModPath.first())] = std::make_pair(
    2612             :         // A module id of -1 is a special entry for a regular LTO module created
    2613             :         // during the thin link.
    2614         152 :         ModPath.second.first == -1u ? RegularLTOModuleName
    2615             :                                     : (std::string)ModPath.first(),
    2616             :         ModPath.second.second);
    2617             : 
    2618             :   unsigned i = 0;
    2619         142 :   for (auto &ModPair : moduleVec) {
    2620         152 :     Out << "^" << i++ << " = module: (";
    2621         152 :     Out << "path: \"" << ModPair.first << "\"";
    2622          76 :     Out << ", hash: (";
    2623             :     FieldSeparator FS;
    2624         836 :     for (auto Hash : ModPair.second)
    2625         380 :       Out << FS << Hash;
    2626          76 :     Out << "))\n";
    2627             :   }
    2628             : 
    2629             :   // FIXME: Change AliasSummary to hold a ValueInfo instead of summary pointer
    2630             :   // for aliasee (then update BitcodeWriter.cpp and remove get/setAliaseeGUID).
    2631         442 :   for (auto &GlobalList : *TheIndex) {
    2632         310 :     auto GUID = GlobalList.first;
    2633         310 :     for (auto &Summary : GlobalList.second.SummaryList)
    2634         434 :       SummaryToGUIDMap[Summary.get()] = GUID;
    2635             :   }
    2636             : 
    2637             :   // Print the global value summary entries.
    2638         442 :   for (auto &GlobalList : *TheIndex) {
    2639         310 :     auto GUID = GlobalList.first;
    2640         620 :     auto VI = TheIndex->getValueInfo(GlobalList);
    2641         310 :     printSummaryInfo(Machine.getGUIDSlot(GUID), VI);
    2642             :   }
    2643             : 
    2644             :   // Print the TypeIdMap entries.
    2645         143 :   for (auto &TId : TheIndex->typeIds()) {
    2646             :     auto GUID = GlobalValue::getGUID(TId.first);
    2647          22 :     Out << "^" << Machine.getGUIDSlot(GUID) << " = typeid: (name: \""
    2648          11 :         << TId.first << "\"";
    2649          11 :     printTypeIdSummary(TId.second);
    2650          11 :     Out << ") ; guid = " << GUID << "\n";
    2651             :   }
    2652          66 : }
    2653             : 
    2654             : static const char *
    2655             : getWholeProgDevirtResKindName(WholeProgramDevirtResolution::Kind K) {
    2656           9 :   switch (K) {
    2657             :   case WholeProgramDevirtResolution::Indir:
    2658             :     return "indir";
    2659           4 :   case WholeProgramDevirtResolution::SingleImpl:
    2660             :     return "singleImpl";
    2661           4 :   case WholeProgramDevirtResolution::BranchFunnel:
    2662             :     return "branchFunnel";
    2663             :   }
    2664           0 :   llvm_unreachable("invalid WholeProgramDevirtResolution kind");
    2665             : }
    2666             : 
    2667             : static const char *getWholeProgDevirtResByArgKindName(
    2668             :     WholeProgramDevirtResolution::ByArg::Kind K) {
    2669           4 :   switch (K) {
    2670             :   case WholeProgramDevirtResolution::ByArg::Indir:
    2671             :     return "indir";
    2672           1 :   case WholeProgramDevirtResolution::ByArg::UniformRetVal:
    2673             :     return "uniformRetVal";
    2674           1 :   case WholeProgramDevirtResolution::ByArg::UniqueRetVal:
    2675             :     return "uniqueRetVal";
    2676           1 :   case WholeProgramDevirtResolution::ByArg::VirtualConstProp:
    2677             :     return "virtualConstProp";
    2678             :   }
    2679           0 :   llvm_unreachable("invalid WholeProgramDevirtResolution::ByArg kind");
    2680             : }
    2681             : 
    2682             : static const char *getTTResKindName(TypeTestResolution::Kind K) {
    2683          11 :   switch (K) {
    2684             :   case TypeTestResolution::Unsat:
    2685             :     return "unsat";
    2686           1 :   case TypeTestResolution::ByteArray:
    2687             :     return "byteArray";
    2688           1 :   case TypeTestResolution::Inline:
    2689             :     return "inline";
    2690           4 :   case TypeTestResolution::Single:
    2691             :     return "single";
    2692           4 :   case TypeTestResolution::AllOnes:
    2693             :     return "allOnes";
    2694             :   }
    2695           0 :   llvm_unreachable("invalid TypeTestResolution kind");
    2696             : }
    2697             : 
    2698          11 : void AssemblyWriter::printTypeTestResolution(const TypeTestResolution &TTRes) {
    2699          22 :   Out << "typeTestRes: (kind: " << getTTResKindName(TTRes.TheKind)
    2700          11 :       << ", sizeM1BitWidth: " << TTRes.SizeM1BitWidth;
    2701             : 
    2702             :   // The following fields are only used if the target does not support the use
    2703             :   // of absolute symbols to store constants. Print only if non-zero.
    2704          11 :   if (TTRes.AlignLog2)
    2705           1 :     Out << ", alignLog2: " << TTRes.AlignLog2;
    2706          11 :   if (TTRes.SizeM1)
    2707           1 :     Out << ", sizeM1: " << TTRes.SizeM1;
    2708          11 :   if (TTRes.BitMask)
    2709             :     // BitMask is uint8_t which causes it to print the corresponding char.
    2710           1 :     Out << ", bitMask: " << (unsigned)TTRes.BitMask;
    2711          11 :   if (TTRes.InlineBits)
    2712           1 :     Out << ", inlineBits: " << TTRes.InlineBits;
    2713             : 
    2714          11 :   Out << ")";
    2715          11 : }
    2716             : 
    2717          11 : void AssemblyWriter::printTypeIdSummary(const TypeIdSummary &TIS) {
    2718          11 :   Out << ", summary: (";
    2719          11 :   printTypeTestResolution(TIS.TTRes);
    2720          11 :   if (!TIS.WPDRes.empty()) {
    2721           4 :     Out << ", wpdResolutions: (";
    2722             :     FieldSeparator FS;
    2723          13 :     for (auto &WPDRes : TIS.WPDRes) {
    2724           9 :       Out << FS;
    2725           9 :       Out << "(offset: " << WPDRes.first << ", ";
    2726           9 :       printWPDRes(WPDRes.second);
    2727           9 :       Out << ")";
    2728             :     }
    2729           4 :     Out << ")";
    2730             :   }
    2731          11 :   Out << ")";
    2732          11 : }
    2733             : 
    2734          25 : void AssemblyWriter::printArgs(const std::vector<uint64_t> &Args) {
    2735          25 :   Out << "args: (";
    2736             :   FieldSeparator FS;
    2737          25 :   for (auto arg : Args) {
    2738          26 :     Out << FS;
    2739          26 :     Out << arg;
    2740             :   }
    2741          25 :   Out << ")";
    2742          25 : }
    2743             : 
    2744           9 : void AssemblyWriter::printWPDRes(const WholeProgramDevirtResolution &WPDRes) {
    2745           9 :   Out << "wpdRes: (kind: ";
    2746          18 :   Out << getWholeProgDevirtResKindName(WPDRes.TheKind);
    2747             : 
    2748           9 :   if (WPDRes.TheKind == WholeProgramDevirtResolution::SingleImpl)
    2749           8 :     Out << ", singleImplName: \"" << WPDRes.SingleImplName << "\"";
    2750             : 
    2751           9 :   if (!WPDRes.ResByArg.empty()) {
    2752           1 :     Out << ", resByArg: (";
    2753             :     FieldSeparator FS;
    2754           5 :     for (auto &ResByArg : WPDRes.ResByArg) {
    2755           4 :       Out << FS;
    2756           4 :       printArgs(ResByArg.first);
    2757           4 :       Out << ", byArg: (kind: ";
    2758           8 :       Out << getWholeProgDevirtResByArgKindName(ResByArg.second.TheKind);
    2759           8 :       if (ResByArg.second.TheKind ==
    2760           4 :               WholeProgramDevirtResolution::ByArg::UniformRetVal ||
    2761             :           ResByArg.second.TheKind ==
    2762             :               WholeProgramDevirtResolution::ByArg::UniqueRetVal)
    2763           2 :         Out << ", info: " << ResByArg.second.Info;
    2764             : 
    2765             :       // The following fields are only used if the target does not support the
    2766             :       // use of absolute symbols to store constants. Print only if non-zero.
    2767           4 :       if (ResByArg.second.Byte || ResByArg.second.Bit)
    2768           1 :         Out << ", byte: " << ResByArg.second.Byte
    2769           1 :             << ", bit: " << ResByArg.second.Bit;
    2770             : 
    2771           4 :       Out << ")";
    2772             :     }
    2773           1 :     Out << ")";
    2774             :   }
    2775           9 :   Out << ")";
    2776           9 : }
    2777             : 
    2778             : static const char *getSummaryKindName(GlobalValueSummary::SummaryKind SK) {
    2779         217 :   switch (SK) {
    2780             :   case GlobalValueSummary::AliasKind:
    2781             :     return "alias";
    2782         172 :   case GlobalValueSummary::FunctionKind:
    2783             :     return "function";
    2784          34 :   case GlobalValueSummary::GlobalVarKind:
    2785             :     return "variable";
    2786             :   }
    2787           0 :   llvm_unreachable("invalid summary kind");
    2788             : }
    2789             : 
    2790          11 : void AssemblyWriter::printAliasSummary(const AliasSummary *AS) {
    2791          11 :   Out << ", aliasee: ";
    2792             :   // The indexes emitted for distributed backends may not include the
    2793             :   // aliasee summary (only if it is being imported directly). Handle
    2794             :   // that case by just emitting "null" as the aliasee.
    2795          11 :   if (AS->hasAliasee())
    2796          30 :     Out << "^" << Machine.getGUIDSlot(SummaryToGUIDMap[&AS->getAliasee()]);
    2797             :   else
    2798           1 :     Out << "null";
    2799          11 : }
    2800             : 
    2801             : void AssemblyWriter::printGlobalVarSummary(const GlobalVarSummary *GS) {
    2802             :   // Nothing for now
    2803             : }
    2804             : 
    2805      106655 : static std::string getLinkageName(GlobalValue::LinkageTypes LT) {
    2806      106655 :   switch (LT) {
    2807             :   case GlobalValue::ExternalLinkage:
    2808         172 :     return "external";
    2809             :   case GlobalValue::PrivateLinkage:
    2810       25010 :     return "private";
    2811             :   case GlobalValue::InternalLinkage:
    2812       33332 :     return "internal";
    2813             :   case GlobalValue::LinkOnceAnyLinkage:
    2814        1031 :     return "linkonce";
    2815             :   case GlobalValue::LinkOnceODRLinkage:
    2816       26194 :     return "linkonce_odr";
    2817             :   case GlobalValue::WeakAnyLinkage:
    2818        9649 :     return "weak";
    2819             :   case GlobalValue::WeakODRLinkage:
    2820        2744 :     return "weak_odr";
    2821             :   case GlobalValue::CommonLinkage:
    2822        4313 :     return "common";
    2823             :   case GlobalValue::AppendingLinkage:
    2824        2993 :     return "appending";
    2825             :   case GlobalValue::ExternalWeakLinkage:
    2826         533 :     return "extern_weak";
    2827             :   case GlobalValue::AvailableExternallyLinkage:
    2828         684 :     return "available_externally";
    2829             :   }
    2830           0 :   llvm_unreachable("invalid linkage");
    2831             : }
    2832             : 
    2833             : // When printing the linkage types in IR where the ExternalLinkage is
    2834             : // not printed, and other linkage types are expected to be printed with
    2835             : // a space after the name.
    2836      285753 : static std::string getLinkageNameWithSpace(GlobalValue::LinkageTypes LT) {
    2837      285753 :   if (LT == GlobalValue::ExternalLinkage)
    2838      179315 :     return "";
    2839      212876 :   return getLinkageName(LT) + " ";
    2840             : }
    2841             : 
    2842             : static const char *getHotnessName(CalleeInfo::HotnessType HT) {
    2843          42 :   switch (HT) {
    2844             :   case CalleeInfo::HotnessType::Unknown:
    2845             :     return "unknown";
    2846           8 :   case CalleeInfo::HotnessType::Cold:
    2847             :     return "cold";
    2848          16 :   case CalleeInfo::HotnessType::None:
    2849             :     return "none";
    2850          16 :   case CalleeInfo::HotnessType::Hot:
    2851             :     return "hot";
    2852           2 :   case CalleeInfo::HotnessType::Critical:
    2853             :     return "critical";
    2854             :   }
    2855           0 :   llvm_unreachable("invalid hotness");
    2856             : }
    2857             : 
    2858         172 : void AssemblyWriter::printFunctionSummary(const FunctionSummary *FS) {
    2859         172 :   Out << ", insts: " << FS->instCount();
    2860             : 
    2861             :   FunctionSummary::FFlags FFlags = FS->fflags();
    2862         172 :   if (FFlags.ReadNone | FFlags.ReadOnly | FFlags.NoRecurse |
    2863             :       FFlags.ReturnDoesNotAlias) {
    2864           2 :     Out << ", funcFlags: (";
    2865           2 :     Out << "readNone: " << FFlags.ReadNone;
    2866           2 :     Out << ", readOnly: " << FFlags.ReadOnly;
    2867           2 :     Out << ", noRecurse: " << FFlags.NoRecurse;
    2868           2 :     Out << ", returnDoesNotAlias: " << FFlags.ReturnDoesNotAlias;
    2869           2 :     Out << ")";
    2870             :   }
    2871         172 :   if (!FS->calls().empty()) {
    2872          38 :     Out << ", calls: (";
    2873             :     FieldSeparator IFS;
    2874         198 :     for (auto &Call : FS->calls()) {
    2875          80 :       Out << IFS;
    2876         160 :       Out << "(callee: ^" << Machine.getGUIDSlot(Call.first.getGUID());
    2877          80 :       if (Call.second.getHotness() != CalleeInfo::HotnessType::Unknown)
    2878          84 :         Out << ", hotness: " << getHotnessName(Call.second.getHotness());
    2879          38 :       else if (Call.second.RelBlockFreq)
    2880           3 :         Out << ", relbf: " << Call.second.RelBlockFreq;
    2881          80 :       Out << ")";
    2882             :     }
    2883          38 :     Out << ")";
    2884             :   }
    2885             : 
    2886         172 :   if (const auto *TIdInfo = FS->getTypeIdInfo())
    2887          65 :     printTypeIdInfo(*TIdInfo);
    2888         172 : }
    2889             : 
    2890          65 : void AssemblyWriter::printTypeIdInfo(
    2891             :     const FunctionSummary::TypeIdInfo &TIDInfo) {
    2892          65 :   Out << ", typeIdInfo: (";
    2893             :   FieldSeparator TIDFS;
    2894          65 :   if (!TIDInfo.TypeTests.empty()) {
    2895          31 :     Out << TIDFS;
    2896          31 :     Out << "typeTests: (";
    2897             :     FieldSeparator FS;
    2898          31 :     for (auto &GUID : TIDInfo.TypeTests) {
    2899          38 :       Out << FS;
    2900          38 :       auto Slot = Machine.getGUIDSlot(GUID);
    2901          38 :       if (Slot != -1)
    2902           8 :         Out << "^" << Slot;
    2903             :       else
    2904          30 :         Out << GUID;
    2905             :     }
    2906          31 :     Out << ")";
    2907             :   }
    2908          65 :   if (!TIDInfo.TypeTestAssumeVCalls.empty()) {
    2909          13 :     Out << TIDFS;
    2910          26 :     printNonConstVCalls(TIDInfo.TypeTestAssumeVCalls, "typeTestAssumeVCalls");
    2911             :   }
    2912          65 :   if (!TIDInfo.TypeCheckedLoadVCalls.empty()) {
    2913          10 :     Out << TIDFS;
    2914          20 :     printNonConstVCalls(TIDInfo.TypeCheckedLoadVCalls, "typeCheckedLoadVCalls");
    2915             :   }
    2916          65 :   if (!TIDInfo.TypeTestAssumeConstVCalls.empty()) {
    2917           7 :     Out << TIDFS;
    2918           7 :     printConstVCalls(TIDInfo.TypeTestAssumeConstVCalls,
    2919             :                      "typeTestAssumeConstVCalls");
    2920             :   }
    2921          65 :   if (!TIDInfo.TypeCheckedLoadConstVCalls.empty()) {
    2922           7 :     Out << TIDFS;
    2923           7 :     printConstVCalls(TIDInfo.TypeCheckedLoadConstVCalls,
    2924             :                      "typeCheckedLoadConstVCalls");
    2925             :   }
    2926          65 :   Out << ")";
    2927          65 : }
    2928             : 
    2929          53 : void AssemblyWriter::printVFuncId(const FunctionSummary::VFuncId VFId) {
    2930          53 :   Out << "vFuncId: (";
    2931          53 :   auto Slot = Machine.getGUIDSlot(VFId.GUID);
    2932          53 :   if (Slot != -1)
    2933          11 :     Out << "^" << Slot;
    2934             :   else
    2935          42 :     Out << "guid: " << VFId.GUID;
    2936          53 :   Out << ", offset: " << VFId.Offset;
    2937          53 :   Out << ")";
    2938          53 : }
    2939             : 
    2940          23 : void AssemblyWriter::printNonConstVCalls(
    2941             :     const std::vector<FunctionSummary::VFuncId> VCallList, const char *Tag) {
    2942          23 :   Out << Tag << ": (";
    2943             :   FieldSeparator FS;
    2944          23 :   for (auto &VFuncId : VCallList) {
    2945          32 :     Out << FS;
    2946          32 :     printVFuncId(VFuncId);
    2947             :   }
    2948          23 :   Out << ")";
    2949          23 : }
    2950             : 
    2951          14 : void AssemblyWriter::printConstVCalls(
    2952             :     const std::vector<FunctionSummary::ConstVCall> VCallList, const char *Tag) {
    2953          14 :   Out << Tag << ": (";
    2954             :   FieldSeparator FS;
    2955          14 :   for (auto &ConstVCall : VCallList) {
    2956          21 :     Out << FS;
    2957          21 :     printVFuncId(ConstVCall.VFunc);
    2958          21 :     if (!ConstVCall.Args.empty()) {
    2959          21 :       Out << ", ";
    2960          21 :       printArgs(ConstVCall.Args);
    2961             :     }
    2962             :   }
    2963          14 :   Out << ")";
    2964          14 : }
    2965             : 
    2966         217 : void AssemblyWriter::printSummary(const GlobalValueSummary &Summary) {
    2967             :   GlobalValueSummary::GVFlags GVFlags = Summary.flags();
    2968         217 :   GlobalValue::LinkageTypes LT = (GlobalValue::LinkageTypes)GVFlags.Linkage;
    2969         434 :   Out << getSummaryKindName(Summary.getSummaryKind()) << ": ";
    2970         217 :   Out << "(module: ^" << Machine.getModulePathSlot(Summary.modulePath())
    2971         217 :       << ", flags: (";
    2972         434 :   Out << "linkage: " << getLinkageName(LT);
    2973         217 :   Out << ", notEligibleToImport: " << GVFlags.NotEligibleToImport;
    2974         217 :   Out << ", live: " << GVFlags.Live;
    2975         217 :   Out << ", dsoLocal: " << GVFlags.DSOLocal;
    2976         217 :   Out << ")";
    2977             : 
    2978         217 :   if (Summary.getSummaryKind() == GlobalValueSummary::AliasKind)
    2979          11 :     printAliasSummary(cast<AliasSummary>(&Summary));
    2980         206 :   else if (Summary.getSummaryKind() == GlobalValueSummary::FunctionKind)
    2981         172 :     printFunctionSummary(cast<FunctionSummary>(&Summary));
    2982             :   else
    2983             :     printGlobalVarSummary(cast<GlobalVarSummary>(&Summary));
    2984             : 
    2985             :   auto RefList = Summary.refs();
    2986         217 :   if (!RefList.empty()) {
    2987          36 :     Out << ", refs: (";
    2988             :     FieldSeparator FS;
    2989         138 :     for (auto &Ref : RefList) {
    2990          51 :       Out << FS;
    2991         102 :       Out << "^" << Machine.getGUIDSlot(Ref.getGUID());
    2992             :     }
    2993          36 :     Out << ")";
    2994             :   }
    2995             : 
    2996         217 :   Out << ")";
    2997         217 : }
    2998             : 
    2999         310 : void AssemblyWriter::printSummaryInfo(unsigned Slot, const ValueInfo &VI) {
    3000         620 :   Out << "^" << Slot << " = gv: (";
    3001         620 :   if (!VI.name().empty())
    3002         212 :     Out << "name: \"" << VI.name() << "\"";
    3003             :   else
    3004         196 :     Out << "guid: " << VI.getGUID();
    3005         310 :   if (!VI.getSummaryList().empty()) {
    3006         217 :     Out << ", summaries: (";
    3007             :     FieldSeparator FS;
    3008         651 :     for (auto &Summary : VI.getSummaryList()) {
    3009         217 :       Out << FS;
    3010         217 :       printSummary(*Summary);
    3011             :     }
    3012         217 :     Out << ")";
    3013             :   }
    3014         310 :   Out << ")";
    3015         620 :   if (!VI.name().empty())
    3016         424 :     Out << " ; guid = " << VI.getGUID();
    3017         310 :   Out << "\n";
    3018         310 : }
    3019             : 
    3020      149175 : static void printMetadataIdentifier(StringRef Name,
    3021             :                                     formatted_raw_ostream &Out) {
    3022      149175 :   if (Name.empty()) {
    3023           1 :     Out << "<empty name> ";
    3024             :   } else {
    3025      149202 :     if (isalpha(static_cast<unsigned char>(Name[0])) || Name[0] == '-' ||
    3026      149202 :         Name[0] == '$' || Name[0] == '.' || Name[0] == '_')
    3027      149148 :       Out << Name[0];
    3028             :     else
    3029          78 :       Out << '\\' << hexdigit(Name[0] >> 4) << hexdigit(Name[0] & 0x0F);
    3030     1612735 :     for (unsigned i = 1, e = Name.size(); i != e; ++i) {
    3031     2927122 :       unsigned char C = Name[i];
    3032     1463561 :       if (isalnum(static_cast<unsigned char>(C)) || C == '-' || C == '$' ||
    3033      156623 :           C == '.' || C == '_')
    3034     1463556 :         Out << C;
    3035             :       else
    3036          15 :         Out << '\\' << hexdigit(C >> 4) << hexdigit(C & 0x0F);
    3037             :     }
    3038             :   }
    3039      149175 : }
    3040             : 
    3041       19924 : void AssemblyWriter::printNamedMDNode(const NamedMDNode *NMD) {
    3042       19924 :   Out << '!';
    3043       19924 :   printMetadataIdentifier(NMD->getName(), Out);
    3044       19924 :   Out << " = !{";
    3045       51983 :   for (unsigned i = 0, e = NMD->getNumOperands(); i != e; ++i) {
    3046       32059 :     if (i)
    3047       12203 :       Out << ", ";
    3048             : 
    3049             :     // Write DIExpressions inline.
    3050             :     // FIXME: Ban DIExpressions in NamedMDNodes, they will serve no purpose.
    3051       32059 :     MDNode *Op = NMD->getOperand(i);
    3052          49 :     if (auto *Expr = dyn_cast<DIExpression>(Op)) {
    3053          49 :       writeDIExpression(Out, Expr, nullptr, nullptr, nullptr);
    3054             :       continue;
    3055             :     }
    3056             : 
    3057       32010 :     int Slot = Machine.getMetadataSlot(Op);
    3058       32010 :     if (Slot == -1)
    3059           0 :       Out << "<badref>";
    3060             :     else
    3061       32010 :       Out << '!' << Slot;
    3062             :   }
    3063       19924 :   Out << "}\n";
    3064       19924 : }
    3065             : 
    3066      285753 : static void PrintVisibility(GlobalValue::VisibilityTypes Vis,
    3067             :                             formatted_raw_ostream &Out) {
    3068      285753 :   switch (Vis) {
    3069             :   case GlobalValue::DefaultVisibility: break;
    3070        4861 :   case GlobalValue::HiddenVisibility:    Out << "hidden "; break;
    3071          70 :   case GlobalValue::ProtectedVisibility: Out << "protected "; break;
    3072             :   }
    3073      285753 : }
    3074             : 
    3075      285753 : static void PrintDSOLocation(const GlobalValue &GV,
    3076             :                              formatted_raw_ostream &Out) {
    3077             :   // GVs with local linkage or non default visibility are implicitly dso_local,
    3078             :   // so we don't print it.
    3079      227420 :   bool Implicit = GV.hasLocalLinkage() ||
    3080      226887 :                   (!GV.hasExternalWeakLinkage() && !GV.hasDefaultVisibility());
    3081      285753 :   if (GV.isDSOLocal() && !Implicit)
    3082       15558 :     Out << "dso_local ";
    3083      285753 : }
    3084             : 
    3085      285753 : static void PrintDLLStorageClass(GlobalValue::DLLStorageClassTypes SCT,
    3086             :                                  formatted_raw_ostream &Out) {
    3087      285753 :   switch (SCT) {
    3088             :   case GlobalValue::DefaultStorageClass: break;
    3089        1789 :   case GlobalValue::DLLImportStorageClass: Out << "dllimport "; break;
    3090        2732 :   case GlobalValue::DLLExportStorageClass: Out << "dllexport "; break;
    3091             :   }
    3092      285753 : }
    3093             : 
    3094       84477 : static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
    3095             :                                   formatted_raw_ostream &Out) {
    3096       84477 :   switch (TLM) {
    3097             :     case GlobalVariable::NotThreadLocal:
    3098             :       break;
    3099         351 :     case GlobalVariable::GeneralDynamicTLSModel:
    3100         351 :       Out << "thread_local ";
    3101         351 :       break;
    3102          33 :     case GlobalVariable::LocalDynamicTLSModel:
    3103          33 :       Out << "thread_local(localdynamic) ";
    3104          33 :       break;
    3105         768 :     case GlobalVariable::InitialExecTLSModel:
    3106         768 :       Out << "thread_local(initialexec) ";
    3107         768 :       break;
    3108          32 :     case GlobalVariable::LocalExecTLSModel:
    3109          32 :       Out << "thread_local(localexec) ";
    3110          32 :       break;
    3111             :   }
    3112       84477 : }
    3113             : 
    3114             : static StringRef getUnnamedAddrEncoding(GlobalVariable::UnnamedAddr UA) {
    3115      285753 :   switch (UA) {
    3116             :   case GlobalVariable::UnnamedAddr::None:
    3117             :     return "";
    3118             :   case GlobalVariable::UnnamedAddr::Local:
    3119             :     return "local_unnamed_addr";
    3120             :   case GlobalVariable::UnnamedAddr::Global:
    3121             :     return "unnamed_addr";
    3122             :   }
    3123           0 :   llvm_unreachable("Unknown UnnamedAddr");
    3124             : }
    3125             : 
    3126      284509 : static void maybePrintComdat(formatted_raw_ostream &Out,
    3127             :                              const GlobalObject &GO) {
    3128      284509 :   const Comdat *C = GO.getComdat();
    3129      284509 :   if (!C)
    3130             :     return;
    3131             : 
    3132       28150 :   if (isa<GlobalVariable>(GO))
    3133        7360 :     Out << ',';
    3134       28150 :   Out << " comdat";
    3135             : 
    3136       28150 :   if (GO.getName() == C->getName())
    3137             :     return;
    3138             : 
    3139             :   Out << '(';
    3140        3431 :   PrintLLVMName(Out, C->getName(), ComdatPrefix);
    3141             :   Out << ')';
    3142             : }
    3143             : 
    3144       83233 : void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
    3145       83233 :   if (GV->isMaterializable())
    3146           0 :     Out << "; Materializable\n";
    3147             : 
    3148       83233 :   WriteAsOperandInternal(Out, GV, &TypePrinter, &Machine, GV->getParent());
    3149       83233 :   Out << " = ";
    3150             : 
    3151       93929 :   if (!GV->hasInitializer() && GV->hasExternalLinkage())
    3152       10588 :     Out << "external ";
    3153             : 
    3154      249699 :   Out << getLinkageNameWithSpace(GV->getLinkage());
    3155       83233 :   PrintDSOLocation(*GV, Out);
    3156      166466 :   PrintVisibility(GV->getVisibility(), Out);
    3157      166466 :   PrintDLLStorageClass(GV->getDLLStorageClass(), Out);
    3158      166466 :   PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
    3159       83233 :   StringRef UA = getUnnamedAddrEncoding(GV->getUnnamedAddr());
    3160       83233 :   if (!UA.empty())
    3161       29350 :       Out << UA << ' ';
    3162             : 
    3163       83233 :   if (unsigned AddressSpace = GV->getType()->getAddressSpace())
    3164        2054 :     Out << "addrspace(" << AddressSpace << ") ";
    3165       83233 :   if (GV->isExternallyInitialized()) Out << "externally_initialized ";
    3166      166466 :   Out << (GV->isConstant() ? "constant " : "global ");
    3167       83233 :   TypePrinter.print(GV->getValueType(), Out);
    3168             : 
    3169       83233 :   if (GV->hasInitializer()) {
    3170       72537 :     Out << ' ';
    3171       72537 :     writeOperand(GV->getInitializer(), false);
    3172             :   }
    3173             : 
    3174       83233 :   if (GV->hasSection()) {
    3175       14568 :     Out << ", section \"";
    3176       29136 :     printEscapedString(GV->getSection(), Out);
    3177       14568 :     Out << '"';
    3178             :   }
    3179       83233 :   maybePrintComdat(Out, *GV);
    3180       83233 :   if (GV->getAlignment())
    3181       45769 :     Out << ", align " << GV->getAlignment();
    3182             : 
    3183             :   SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    3184       83233 :   GV->getAllMetadata(MDs);
    3185       83233 :   printMetadataAttachments(MDs, ", ");
    3186             : 
    3187             :   auto Attrs = GV->getAttributes();
    3188       83233 :   if (Attrs.hasAttributes())
    3189          33 :     Out << " #" << Machine.getAttributeGroupSlot(Attrs);
    3190             : 
    3191       83233 :   printInfoComment(*GV);
    3192       83233 : }
    3193             : 
    3194        1244 : void AssemblyWriter::printIndirectSymbol(const GlobalIndirectSymbol *GIS) {
    3195        1244 :   if (GIS->isMaterializable())
    3196           0 :     Out << "; Materializable\n";
    3197             : 
    3198        1244 :   WriteAsOperandInternal(Out, GIS, &TypePrinter, &Machine, GIS->getParent());
    3199        1244 :   Out << " = ";
    3200             : 
    3201        3732 :   Out << getLinkageNameWithSpace(GIS->getLinkage());
    3202        1244 :   PrintDSOLocation(*GIS, Out);
    3203        2488 :   PrintVisibility(GIS->getVisibility(), Out);
    3204        2488 :   PrintDLLStorageClass(GIS->getDLLStorageClass(), Out);
    3205        2488 :   PrintThreadLocalModel(GIS->getThreadLocalMode(), Out);
    3206        1244 :   StringRef UA = getUnnamedAddrEncoding(GIS->getUnnamedAddr());
    3207        1244 :   if (!UA.empty())
    3208         266 :       Out << UA << ' ';
    3209             : 
    3210        1244 :   if (isa<GlobalAlias>(GIS))
    3211        1167 :     Out << "alias ";
    3212          77 :   else if (isa<GlobalIFunc>(GIS))
    3213          77 :     Out << "ifunc ";
    3214             :   else
    3215           0 :     llvm_unreachable("Not an alias or ifunc!");
    3216             : 
    3217        1244 :   TypePrinter.print(GIS->getValueType(), Out);
    3218             : 
    3219        1244 :   Out << ", ";
    3220             : 
    3221             :   const Constant *IS = GIS->getIndirectSymbol();
    3222             : 
    3223             :   if (!IS) {
    3224           0 :     TypePrinter.print(GIS->getType(), Out);
    3225           0 :     Out << " <<NULL ALIASEE>>";
    3226             :   } else {
    3227        1244 :     writeOperand(IS, !isa<ConstantExpr>(IS));
    3228             :   }
    3229             : 
    3230        1244 :   printInfoComment(*GIS);
    3231        1244 :   Out << '\n';
    3232        1244 : }
    3233             : 
    3234             : void AssemblyWriter::printComdat(const Comdat *C) {
    3235       25037 :   C->print(Out);
    3236             : }
    3237             : 
    3238       16686 : void AssemblyWriter::printTypeIdentities() {
    3239       16686 :   if (TypePrinter.empty())
    3240             :     return;
    3241             : 
    3242        6023 :   Out << '\n';
    3243             : 
    3244             :   // Emit all numbered types.
    3245        6023 :   auto &NumberedTypes = TypePrinter.getNumberedTypes();
    3246       13003 :   for (unsigned I = 0, E = NumberedTypes.size(); I != E; ++I) {
    3247        1914 :     Out << '%' << I << " = type ";
    3248             : 
    3249             :     // Make sure we print out at least one level of the type structure, so
    3250             :     // that we do not get %2 = type %2
    3251        1914 :     TypePrinter.printStructBody(NumberedTypes[I], Out);
    3252         957 :     Out << '\n';
    3253             :   }
    3254             : 
    3255             :   auto &NamedTypes = TypePrinter.getNamedTypes();
    3256       36693 :   for (unsigned I = 0, E = NamedTypes.size(); I != E; ++I) {
    3257       30670 :     PrintLLVMName(Out, NamedTypes[I]->getName(), LocalPrefix);
    3258       30670 :     Out << " = type ";
    3259             : 
    3260             :     // Make sure we print out at least one level of the type structure, so
    3261             :     // that we do not get %FILE = type %FILE
    3262       61340 :     TypePrinter.printStructBody(NamedTypes[I], Out);
    3263       30670 :     Out << '\n';
    3264             :   }
    3265             : }
    3266             : 
    3267             : /// printFunction - Print all aspects of a function.
    3268      201276 : void AssemblyWriter::printFunction(const Function *F) {
    3269             :   // Print out the return type and name.
    3270      201276 :   Out << '\n';
    3271             : 
    3272      201276 :   if (AnnotationWriter) AnnotationWriter->emitFunctionAnnot(F, Out);
    3273             : 
    3274      201276 :   if (F->isMaterializable())
    3275           6 :     Out << "; Materializable\n";
    3276             : 
    3277      201276 :   const AttributeList &Attrs = F->getAttributes();
    3278      201276 :   if (Attrs.hasAttributes(AttributeList::FunctionIndex)) {
    3279      142353 :     AttributeSet AS = Attrs.getFnAttributes();
    3280             :     std::string AttrStr;
    3281             : 
    3282     1811670 :     for (const Attribute &Attr : AS) {
    3283     1669317 :       if (!Attr.isStringAttribute()) {
    3284      331418 :         if (!AttrStr.empty()) AttrStr += ' ';
    3285      662836 :         AttrStr += Attr.getAsString();
    3286             :       }
    3287             :     }
    3288             : 
    3289      142353 :     if (!AttrStr.empty())
    3290      132396 :       Out << "; Function Attrs: " << AttrStr << '\n';
    3291             :   }
    3292             : 
    3293      201276 :   Machine.incorporateFunction(F);
    3294             : 
    3295      201276 :   if (F->isDeclaration()) {
    3296       69530 :     Out << "declare";
    3297             :     SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    3298       69530 :     F->getAllMetadata(MDs);
    3299       69530 :     printMetadataAttachments(MDs, " ");
    3300       69530 :     Out << ' ';
    3301             :   } else
    3302      131746 :     Out << "define ";
    3303             : 
    3304      603828 :   Out << getLinkageNameWithSpace(F->getLinkage());
    3305      201276 :   PrintDSOLocation(*F, Out);
    3306      402552 :   PrintVisibility(F->getVisibility(), Out);
    3307      402552 :   PrintDLLStorageClass(F->getDLLStorageClass(), Out);
    3308             : 
    3309             :   // Print the calling convention.
    3310      201276 :   if (F->getCallingConv() != CallingConv::C) {
    3311        8573 :     PrintCallingConv(F->getCallingConv(), Out);
    3312        8573 :     Out << " ";
    3313             :   }
    3314             : 
    3315             :   FunctionType *FT = F->getFunctionType();
    3316      201276 :   if (Attrs.hasAttributes(AttributeList::ReturnIndex))
    3317       11808 :     Out << Attrs.getAsString(AttributeList::ReturnIndex) << ' ';
    3318      402552 :   TypePrinter.print(F->getReturnType(), Out);
    3319      201276 :   Out << ' ';
    3320      201276 :   WriteAsOperandInternal(Out, F, &TypePrinter, &Machine, F->getParent());
    3321      201276 :   Out << '(';
    3322             : 
    3323             :   // Loop over the arguments, printing them...
    3324      201276 :   if (F->isDeclaration() && !IsForDebug) {
    3325             :     // We're only interested in the type here - don't print argument names.
    3326      413298 :     for (unsigned I = 0, E = FT->getNumParams(); I != E; ++I) {
    3327             :       // Insert commas as we go... the first arg doesn't get a comma
    3328      137119 :       if (I)
    3329       77390 :         Out << ", ";
    3330             :       // Output type...
    3331      274238 :       TypePrinter.print(FT->getParamType(I), Out);
    3332             : 
    3333      137119 :       AttributeSet ArgAttrs = Attrs.getParamAttributes(I);
    3334      137119 :       if (ArgAttrs.hasAttributes())
    3335       24873 :         Out << ' ' << ArgAttrs.getAsString();
    3336             :     }
    3337             :   } else {
    3338             :     // The arguments are meaningful here, print them in detail.
    3339      628268 :     for (const Argument &Arg : F->args()) {
    3340             :       // Insert commas as we go... the first arg doesn't get a comma
    3341      182388 :       if (Arg.getArgNo() != 0)
    3342       84043 :         Out << ", ";
    3343      182388 :       printArgument(&Arg, Attrs.getParamAttributes(Arg.getArgNo()));
    3344             :     }
    3345             :   }
    3346             : 
    3347             :   // Finish printing arguments...
    3348      201276 :   if (FT->isVarArg()) {
    3349        4787 :     if (FT->getNumParams()) Out << ", ";
    3350        4787 :     Out << "...";  // Output varargs portion of signature!
    3351             :   }
    3352      201276 :   Out << ')';
    3353      201276 :   StringRef UA = getUnnamedAddrEncoding(F->getUnnamedAddr());
    3354      201276 :   if (!UA.empty())
    3355       51164 :     Out << ' ' << UA;
    3356      201276 :   if (Attrs.hasAttributes(AttributeList::FunctionIndex))
    3357      142353 :     Out << " #" << Machine.getAttributeGroupSlot(Attrs.getFnAttributes());
    3358      201276 :   if (F->hasSection()) {
    3359        1254 :     Out << " section \"";
    3360        2508 :     printEscapedString(F->getSection(), Out);
    3361        1254 :     Out << '"';
    3362             :   }
    3363      201276 :   maybePrintComdat(Out, *F);
    3364      201276 :   if (F->getAlignment())
    3365       23085 :     Out << " align " << F->getAlignment();
    3366      201276 :   if (F->hasGC())
    3367         414 :     Out << " gc \"" << F->getGC() << '"';
    3368      201276 :   if (F->hasPrefixData()) {
    3369          30 :     Out << " prefix ";
    3370          30 :     writeOperand(F->getPrefixData(), true);
    3371             :   }
    3372      201276 :   if (F->hasPrologueData()) {
    3373         145 :     Out << " prologue ";
    3374         145 :     writeOperand(F->getPrologueData(), true);
    3375             :   }
    3376      201276 :   if (F->hasPersonalityFn()) {
    3377        1662 :     Out << " personality ";
    3378        1662 :     writeOperand(F->getPersonalityFn(), /*PrintType=*/true);
    3379             :   }
    3380             : 
    3381      201276 :   if (F->isDeclaration()) {
    3382       69530 :     Out << '\n';
    3383             :   } else {
    3384             :     SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
    3385      131746 :     F->getAllMetadata(MDs);
    3386      131746 :     printMetadataAttachments(MDs, " ");
    3387             : 
    3388      131746 :     Out << " {";
    3389             :     // Output all of the function's basic blocks.
    3390      426536 :     for (const BasicBlock &BB : *F)
    3391      294790 :       printBasicBlock(&BB);
    3392             : 
    3393             :     // Output the function's use-lists.
    3394      131746 :     printUseLists(F);
    3395             : 
    3396      131746 :     Out << "}\n";
    3397             :   }
    3398             : 
    3399      201276 :   Machine.purgeFunction();
    3400      201276 : }
    3401             : 
    3402             : /// printArgument - This member is called for every argument that is passed into
    3403             : /// the function.  Simply print it out
    3404      182388 : void AssemblyWriter::printArgument(const Argument *Arg, AttributeSet Attrs) {
    3405             :   // Output type...
    3406      182388 :   TypePrinter.print(Arg->getType(), Out);
    3407             : 
    3408             :   // Output parameter attributes list
    3409      182388 :   if (Attrs.hasAttributes())
    3410      119502 :     Out << ' ' << Attrs.getAsString();
    3411             : 
    3412             :   // Output name, if available...
    3413      182388 :   if (Arg->hasName()) {
    3414      171898 :     Out << ' ';
    3415      171898 :     PrintLLVMName(Out, Arg);
    3416             :   }
    3417      182388 : }
    3418             : 
    3419             : /// printBasicBlock - This member is called for each basic block in a method.
    3420      294818 : void AssemblyWriter::printBasicBlock(const BasicBlock *BB) {
    3421      294818 :   if (BB->hasName()) {              // Print out the label if it exists...
    3422      266873 :     Out << "\n";
    3423      266873 :     PrintLLVMName(Out, BB->getName(), LabelPrefix);
    3424      266873 :     Out << ':';
    3425       27945 :   } else if (!BB->use_empty()) {      // Don't print block # of no uses...
    3426        2617 :     Out << "\n; <label>:";
    3427        2617 :     int Slot = Machine.getLocalSlot(BB);
    3428        2617 :     if (Slot != -1)
    3429        5234 :       Out << Slot << ":";
    3430             :     else
    3431           0 :       Out << "<badref>";
    3432             :   }
    3433             : 
    3434      294818 :   if (!BB->getParent()) {
    3435           0 :     Out.PadToColumn(50);
    3436           0 :     Out << "; Error: Block without parent!";
    3437      294818 :   } else if (BB != &BB->getParent()->getEntryBlock()) {  // Not the entry block?
    3438             :     // Output predecessors for the block.
    3439      163073 :     Out.PadToColumn(50);
    3440      163073 :     Out << ";";
    3441      163073 :     const_pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
    3442             : 
    3443      163073 :     if (PI == PE) {
    3444         866 :       Out << " No predecessors!";
    3445             :     } else {
    3446      162207 :       Out << " preds = ";
    3447      162207 :       writeOperand(*PI, false);
    3448      220290 :       for (++PI; PI != PE; ++PI) {
    3449       58083 :         Out << ", ";
    3450       58083 :         writeOperand(*PI, false);
    3451             :       }
    3452             :     }
    3453             :   }
    3454             : 
    3455      294818 :   Out << "\n";
    3456             : 
    3457      294818 :   if (AnnotationWriter) AnnotationWriter->emitBasicBlockStartAnnot(BB, Out);
    3458             : 
    3459             :   // Output all of the instructions in the basic block...
    3460     2485833 :   for (const Instruction &I : *BB) {
    3461     2191015 :     printInstructionLine(I);
    3462             :   }
    3463             : 
    3464      294818 :   if (AnnotationWriter) AnnotationWriter->emitBasicBlockEndAnnot(BB, Out);
    3465      294818 : }
    3466             : 
    3467             : /// printInstructionLine - Print an instruction and a newline character.
    3468     2191015 : void AssemblyWriter::printInstructionLine(const Instruction &I) {
    3469     2191015 :   printInstruction(I);
    3470     2191015 :   Out << '\n';
    3471     2191015 : }
    3472             : 
    3473             : /// printGCRelocateComment - print comment after call to the gc.relocate
    3474             : /// intrinsic indicating base and derived pointer names.
    3475         485 : void AssemblyWriter::printGCRelocateComment(const GCRelocateInst &Relocate) {
    3476         485 :   Out << " ; (";
    3477         485 :   writeOperand(Relocate.getBasePtr(), false);
    3478         485 :   Out << ", ";
    3479         485 :   writeOperand(Relocate.getDerivedPtr(), false);
    3480         485 :   Out << ")";
    3481         485 : }
    3482             : 
    3483             : /// printInfoComment - Print a little comment after the instruction indicating
    3484             : /// which slot it occupies.
    3485     2309556 : void AssemblyWriter::printInfoComment(const Value &V) {
    3486             :   if (const auto *Relocate = dyn_cast<GCRelocateInst>(&V))
    3487         485 :     printGCRelocateComment(*Relocate);
    3488             : 
    3489     2309556 :   if (AnnotationWriter)
    3490        1239 :     AnnotationWriter->printInfoComment(V, Out);
    3491     2309556 : }
    3492             : 
    3493             : // This member is called for each Instruction in a function..
    3494     2225079 : void AssemblyWriter::printInstruction(const Instruction &I) {
    3495     2225079 :   if (AnnotationWriter) AnnotationWriter->emitInstructionAnnot(&I, Out);
    3496             : 
    3497             :   // Print out indentation for an instruction.
    3498     2225079 :   Out << "  ";
    3499             : 
    3500             :   // Print out name if it exists...
    3501     2225079 :   if (I.hasName()) {
    3502      745275 :     PrintLLVMName(Out, &I);
    3503      745275 :     Out << " = ";
    3504     2959608 :   } else if (!I.getType()->isVoidTy()) {
    3505             :     // Print out the def slot taken.
    3506      679943 :     int SlotNum = Machine.getLocalSlot(&I);
    3507      679943 :     if (SlotNum == -1)
    3508          38 :       Out << "<badref> = ";
    3509             :     else
    3510     1359810 :       Out << '%' << SlotNum << " = ";
    3511             :   }
    3512             : 
    3513             :   if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
    3514      175245 :     if (CI->isMustTailCall())
    3515         176 :       Out << "musttail ";
    3516      175069 :     else if (CI->isTailCall())
    3517        8003 :       Out << "tail ";
    3518      167066 :     else if (CI->isNoTailCall())
    3519          54 :       Out << "notail ";
    3520             :   }
    3521             : 
    3522             :   // Print out the opcode...
    3523     4450158 :   Out << I.getOpcodeName();
    3524             : 
    3525             :   // If this is an atomic load or store, print out the atomic marker.
    3526     4449006 :   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isAtomic()) ||
    3527      393170 :       (isa<StoreInst>(I) && cast<StoreInst>(I).isAtomic()))
    3528        1762 :     Out << " atomic";
    3529             : 
    3530     2225772 :   if (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isWeak())
    3531          67 :     Out << " weak";
    3532             : 
    3533             :   // If this is a volatile operation, print out the volatile marker.
    3534      416383 :   if ((isa<LoadInst>(I)  && cast<LoadInst>(I).isVolatile()) ||
    3535      393170 :       (isa<StoreInst>(I) && cast<StoreInst>(I).isVolatile()) ||
    3536     2225772 :       (isa<AtomicCmpXchgInst>(I) && cast<AtomicCmpXchgInst>(I).isVolatile()) ||
    3537         947 :       (isa<AtomicRMWInst>(I) && cast<AtomicRMWInst>(I).isVolatile()))
    3538       17246 :     Out << " volatile";
    3539             : 
    3540             :   // Print out optimization information.
    3541     2225079 :   WriteOptimizationInfo(Out, &I);
    3542             : 
    3543             :   // Print out the compare instruction predicates
    3544             :   if (const CmpInst *CI = dyn_cast<CmpInst>(&I))
    3545      130602 :     Out << ' ' << CmpInst::getPredicateName(CI->getPredicate());
    3546             : 
    3547             :   // Print out the atomicrmw operation
    3548             :   if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I))
    3549         947 :     writeAtomicRMWOperation(Out, RMWI->getOperation());
    3550             : 
    3551             :   // Print out the type of the operands...
    3552     2225079 :   const Value *Operand = I.getNumOperands() ? I.getOperand(0) : nullptr;
    3553             : 
    3554             :   // Special case conditional branches to swizzle the condition out to the front
    3555     2225079 :   if (isa<BranchInst>(I) && cast<BranchInst>(I).isConditional()) {
    3556             :     const BranchInst &BI(cast<BranchInst>(I));
    3557       56098 :     Out << ' ';
    3558       56098 :     writeOperand(BI.getCondition(), true);
    3559       56098 :     Out << ", ";
    3560       56098 :     writeOperand(BI.getSuccessor(0), true);
    3561       56098 :     Out << ", ";
    3562       56098 :     writeOperand(BI.getSuccessor(1), true);
    3563             : 
    3564     2168981 :   } else if (isa<SwitchInst>(I)) {
    3565             :     const SwitchInst& SI(cast<SwitchInst>(I));
    3566             :     // Special case switch instruction to get formatting nice and correct.
    3567        1119 :     Out << ' ';
    3568        1119 :     writeOperand(SI.getCondition(), true);
    3569        1119 :     Out << ", ";
    3570        1119 :     writeOperand(SI.getDefaultDest(), true);
    3571        1119 :     Out << " [";
    3572        3840 :     for (auto Case : SI.cases()) {
    3573        2721 :       Out << "\n    ";
    3574        2721 :       writeOperand(Case.getCaseValue(), true);
    3575        2721 :       Out << ", ";
    3576        2721 :       writeOperand(Case.getCaseSuccessor(), true);
    3577             :     }
    3578        1119 :     Out << "\n  ]";
    3579     2167862 :   } else if (isa<IndirectBrInst>(I)) {
    3580             :     // Special case indirectbr instruction to get formatting nice and correct.
    3581         224 :     Out << ' ';
    3582         224 :     writeOperand(Operand, true);
    3583         224 :     Out << ", [";
    3584             : 
    3585        1432 :     for (unsigned i = 1, e = I.getNumOperands(); i != e; ++i) {
    3586         604 :       if (i != 1)
    3587         384 :         Out << ", ";
    3588         604 :       writeOperand(I.getOperand(i), true);
    3589             :     }
    3590         224 :     Out << ']';
    3591             :   } else if (const PHINode *PN = dyn_cast<PHINode>(&I)) {
    3592       26533 :     Out << ' ';
    3593       26533 :     TypePrinter.print(I.getType(), Out);
    3594       26533 :     Out << ' ';
    3595             : 
    3596      131917 :     for (unsigned op = 0, Eop = PN->getNumIncomingValues(); op < Eop; ++op) {
    3597       52692 :       if (op) Out << ", ";
    3598       52692 :       Out << "[ ";
    3599       52692 :       writeOperand(PN->getIncomingValue(op), false); Out << ", ";
    3600       52692 :       writeOperand(PN->getIncomingBlock(op), false); Out << " ]";
    3601             :     }
    3602             :   } else if (const ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(&I)) {
    3603        9222 :     Out << ' ';
    3604        9222 :     writeOperand(I.getOperand(0), true);
    3605       27776 :     for (const unsigned *i = EVI->idx_begin(), *e = EVI->idx_end(); i != e; ++i)
    3606        9277 :       Out << ", " << *i;
    3607             :   } else if (const InsertValueInst *IVI = dyn_cast<InsertValueInst>(&I)) {
    3608        2530 :     Out << ' ';
    3609        2530 :     writeOperand(I.getOperand(0), true); Out << ", ";
    3610        2530 :     writeOperand(I.getOperand(1), true);
    3611        7670 :     for (const unsigned *i = IVI->idx_begin(), *e = IVI->idx_end(); i != e; ++i)
    3612        2570 :       Out << ", " << *i;
    3613             :   } else if (const LandingPadInst *LPI = dyn_cast<LandingPadInst>(&I)) {
    3614        1795 :     Out << ' ';
    3615        1795 :     TypePrinter.print(I.getType(), Out);
    3616        2711 :     if (LPI->isCleanup() || LPI->getNumClauses() != 0)
    3617        1795 :       Out << '\n';
    3618             : 
    3619        1795 :     if (LPI->isCleanup())
    3620         879 :       Out << "          cleanup";
    3621             : 
    3622        3877 :     for (unsigned i = 0, e = LPI->getNumClauses(); i != e; ++i) {
    3623        2016 :       if (i != 0 || LPI->isCleanup()) Out << "\n";
    3624        1041 :       if (LPI->isCatch(i))
    3625         980 :         Out << "          catch ";
    3626             :       else
    3627          61 :         Out << "          filter ";
    3628             : 
    3629        1041 :       writeOperand(LPI->getClause(i), true);
    3630             :     }
    3631             :   } else if (const auto *CatchSwitch = dyn_cast<CatchSwitchInst>(&I)) {
    3632         299 :     Out << " within ";
    3633         299 :     writeOperand(CatchSwitch->getParentPad(), /*PrintType=*/false);
    3634         299 :     Out << " [";
    3635             :     unsigned Op = 0;
    3636         611 :     for (const BasicBlock *PadBB : CatchSwitch->handlers()) {
    3637         312 :       if (Op > 0)
    3638          13 :         Out << ", ";
    3639         312 :       writeOperand(PadBB, /*PrintType=*/true);
    3640         312 :       ++Op;
    3641             :     }
    3642         299 :     Out << "] unwind ";
    3643             :     if (const BasicBlock *UnwindDest = CatchSwitch->getUnwindDest())
    3644          83 :       writeOperand(UnwindDest, /*PrintType=*/true);
    3645             :     else
    3646         216 :       Out << "to caller";
    3647             :   } else if (const auto *FPI = dyn_cast<FuncletPadInst>(&I)) {
    3648         752 :     Out << " within ";
    3649         752 :     writeOperand(FPI->getParentPad(), /*PrintType=*/false);
    3650         752 :     Out << " [";
    3651        1648 :     for (unsigned Op = 0, NumOps = FPI->getNumArgOperands(); Op < NumOps;
    3652             :          ++Op) {
    3653         448 :       if (Op > 0)
    3654         223 :         Out << ", ";
    3655         448 :       writeOperand(FPI->getArgOperand(Op), /*PrintType=*/true);
    3656             :     }
    3657         752 :     Out << ']';
    3658     2126507 :   } else if (isa<ReturnInst>(I) && !Operand) {
    3659       73179 :     Out << " void";
    3660             :   } else if (const auto *CRI = dyn_cast<CatchReturnInst>(&I)) {
    3661         212 :     Out << " from ";
    3662         212 :     writeOperand(CRI->getOperand(0), /*PrintType=*/false);
    3663             : 
    3664         212 :     Out << " to ";
    3665         212 :     writeOperand(CRI->getOperand(1), /*PrintType=*/true);
    3666             :   } else if (const auto *CRI = dyn_cast<CleanupReturnInst>(&I)) {
    3667         319 :     Out << " from ";
    3668         319 :     writeOperand(CRI->getOperand(0), /*PrintType=*/false);
    3669             : 
    3670         319 :     Out << " unwind ";
    3671         319 :     if (CRI->hasUnwindDest())
    3672         117 :       writeOperand(CRI->getOperand(1), /*PrintType=*/true);
    3673             :     else
    3674         202 :       Out << "to caller";
    3675             :   } else if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
    3676             :     // Print the calling convention being used.
    3677      175245 :     if (CI->getCallingConv() != CallingConv::C) {
    3678        5080 :       Out << " ";
    3679       10160 :       PrintCallingConv(CI->getCallingConv(), Out);
    3680             :     }
    3681             : 
    3682             :     Operand = CI->getCalledValue();
    3683      175245 :     FunctionType *FTy = CI->getFunctionType();
    3684      175245 :     Type *RetTy = FTy->getReturnType();
    3685      175245 :     const AttributeList &PAL = CI->getAttributes();
    3686             : 
    3687      175245 :     if (PAL.hasAttributes(AttributeList::ReturnIndex))
    3688        7659 :       Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
    3689             : 
    3690             :     // If possible, print out the short form of the call instruction.  We can
    3691             :     // only do this if the first argument is a pointer to a nonvararg function,
    3692             :     // and if the return type is not a pointer to a function.
    3693             :     //
    3694      175245 :     Out << ' ';
    3695      350490 :     TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
    3696      175245 :     Out << ' ';
    3697      175245 :     writeOperand(Operand, false);
    3698      175245 :     Out << '(';
    3699     1124362 :     for (unsigned op = 0, Eop = CI->getNumArgOperands(); op < Eop; ++op) {
    3700      386936 :       if (op > 0)
    3701      235697 :         Out << ", ";
    3702      773872 :       writeParamOperand(CI->getArgOperand(op), PAL.getParamAttributes(op));
    3703             :     }
    3704             : 
    3705             :     // Emit an ellipsis if this is a musttail call in a vararg function.  This
    3706             :     // is only to aid readability, musttail calls forward varargs by default.
    3707         352 :     if (CI->isMustTailCall() && CI->getParent() &&
    3708      175597 :         CI->getParent()->getParent() &&
    3709             :         CI->getParent()->getParent()->isVarArg())
    3710          92 :       Out << ", ...";
    3711             : 
    3712      175245 :     Out << ')';
    3713      175245 :     if (PAL.hasAttributes(AttributeList::FunctionIndex))
    3714       54209 :       Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
    3715             : 
    3716      175245 :     writeOperandBundles(CI);
    3717             :   } else if (const InvokeInst *II = dyn_cast<InvokeInst>(&I)) {
    3718             :     Operand = II->getCalledValue();
    3719        3170 :     FunctionType *FTy = II->getFunctionType();
    3720        3170 :     Type *RetTy = FTy->getReturnType();
    3721        3170 :     const AttributeList &PAL = II->getAttributes();
    3722             : 
    3723             :     // Print the calling convention being used.
    3724        3170 :     if (II->getCallingConv() != CallingConv::C) {
    3725          43 :       Out << " ";
    3726          86 :       PrintCallingConv(II->getCallingConv(), Out);
    3727             :     }
    3728             : 
    3729        3170 :     if (PAL.hasAttributes(AttributeList::ReturnIndex))
    3730         849 :       Out << ' ' << PAL.getAsString(AttributeList::ReturnIndex);
    3731             : 
    3732             :     // If possible, print out the short form of the invoke instruction. We can
    3733             :     // only do this if the first argument is a pointer to a nonvararg function,
    3734             :     // and if the return type is not a pointer to a function.
    3735             :     //
    3736        3170 :     Out << ' ';
    3737        6340 :     TypePrinter.print(FTy->isVarArg() ? FTy : RetTy, Out);
    3738        3170 :     Out << ' ';
    3739        3170 :     writeOperand(Operand, false);
    3740        3170 :     Out << '(';
    3741       12264 :     for (unsigned op = 0, Eop = II->getNumArgOperands(); op < Eop; ++op) {
    3742        2962 :       if (op)
    3743        1286 :         Out << ", ";
    3744        5924 :       writeParamOperand(II->getArgOperand(op), PAL.getParamAttributes(op));
    3745             :     }
    3746             : 
    3747        3170 :     Out << ')';
    3748        3170 :     if (PAL.hasAttributes(AttributeList::FunctionIndex))
    3749         202 :       Out << " #" << Machine.getAttributeGroupSlot(PAL.getFnAttributes());
    3750             : 
    3751        3170 :     writeOperandBundles(II);
    3752             : 
    3753        3170 :     Out << "\n          to ";
    3754        3170 :     writeOperand(II->getNormalDest(), true);
    3755        3170 :     Out << " unwind ";
    3756        3170 :     writeOperand(II->getUnwindDest(), true);
    3757             :   } else if (const AllocaInst *AI = dyn_cast<AllocaInst>(&I)) {
    3758      309738 :     Out << ' ';
    3759      309738 :     if (AI->isUsedWithInAlloca())
    3760         117 :       Out << "inalloca ";
    3761      309738 :     if (AI->isSwiftError())
    3762          24 :       Out << "swifterror ";
    3763      309738 :     TypePrinter.print(AI->getAllocatedType(), Out);
    3764             : 
    3765             :     // Explicitly write the array size if the code is broken, if it's an array
    3766             :     // allocation, or if the type is not canonical for scalar allocations.  The
    3767             :     // latter case prevents the type from mutating when round-tripping through
    3768             :     // assembly.
    3769      618015 :     if (!AI->getArraySize() || AI->isArrayAllocation() ||
    3770      308277 :         !AI->getArraySize()->getType()->isIntegerTy(32)) {
    3771        1469 :       Out << ", ";
    3772        1469 :       writeOperand(AI->getArraySize(), true);
    3773             :     }
    3774      309738 :     if (AI->getAlignment()) {
    3775      306763 :       Out << ", align " << AI->getAlignment();
    3776             :     }
    3777             : 
    3778             :     unsigned AddrSpace = AI->getType()->getAddressSpace();
    3779      309738 :     if (AddrSpace != 0) {
    3780        1142 :       Out << ", addrspace(" << AddrSpace << ')';
    3781             :     }
    3782     1564644 :   } else if (isa<CastInst>(I)) {
    3783      196556 :     if (Operand) {
    3784      196556 :       Out << ' ';
    3785      196556 :       writeOperand(Operand, true);   // Work with broken code
    3786             :     }
    3787      196556 :     Out << " to ";
    3788      196556 :     TypePrinter.print(I.getType(), Out);
    3789     1368088 :   } else if (isa<VAArgInst>(I)) {
    3790          50 :     if (Operand) {
    3791          50 :       Out << ' ';
    3792          50 :       writeOperand(Operand, true);   // Work with broken code
    3793             :     }
    3794          50 :     Out << ", ";
    3795          50 :     TypePrinter.print(I.getType(), Out);
    3796     1368038 :   } else if (Operand) {   // Print the normal way.
    3797             :     if (const auto *GEP = dyn_cast<GetElementPtrInst>(&I)) {
    3798      141535 :       Out << ' ';
    3799      141535 :       TypePrinter.print(GEP->getSourceElementType(), Out);
    3800      141535 :       Out << ',';
    3801             :     } else if (const auto *LI = dyn_cast<LoadInst>(&I)) {
    3802      416383 :       Out << ' ';
    3803      416383 :       TypePrinter.print(LI->getType(), Out);
    3804      416383 :       Out << ',';
    3805             :     }
    3806             : 
    3807             :     // PrintAllTypes - Instructions who have operands of all the same type
    3808             :     // omit the type from all but the first operand.  If the instruction has
    3809             :     // different type operands (for example br), then they are all printed.
    3810             :     bool PrintAllTypes = false;
    3811     1363270 :     Type *TheType = Operand->getType();
    3812             : 
    3813             :     // Select, Store and ShuffleVector always print all types.
    3814     1356041 :     if (isa<SelectInst>(I) || isa<StoreInst>(I) || isa<ShuffleVectorInst>(I)
    3815     2314546 :         || isa<ReturnInst>(I)) {
    3816             :       PrintAllTypes = true;
    3817             :     } else {
    3818     1298755 :       for (unsigned i = 1, E = I.getNumOperands(); i != E; ++i) {
    3819             :         Operand = I.getOperand(i);
    3820             :         // note that Operand shouldn't be null, but the test helps make dump()
    3821             :         // more tolerant of malformed IR
    3822      372216 :         if (Operand && Operand->getType() != TheType) {
    3823             :           PrintAllTypes = true;    // We have differing types!  Print them all!
    3824             :           break;
    3825             :         }
    3826             :       }
    3827             :     }
    3828             : 
    3829      886069 :     if (!PrintAllTypes) {
    3830      720196 :       Out << ' ';
    3831      720196 :       TypePrinter.print(TheType, Out);
    3832             :     }
    3833             : 
    3834     1363270 :     Out << ' ';
    3835     5913888 :     for (unsigned i = 0, E = I.getNumOperands(); i != E; ++i) {
    3836     2275309 :       if (i) Out << ", ";
    3837     4550618 :       writeOperand(I.getOperand(i), PrintAllTypes);
    3838             :     }
    3839             :   }
    3840             : 
    3841             :   // Print atomic ordering/alignment for memory operations
    3842             :   if (const LoadInst *LI = dyn_cast<LoadInst>(&I)) {
    3843      416383 :     if (LI->isAtomic())
    3844        2304 :       writeAtomic(LI->getContext(), LI->getOrdering(), LI->getSyncScopeID());
    3845      416383 :     if (LI->getAlignment())
    3846      408504 :       Out << ", align " << LI->getAlignment();
    3847             :   } else if (const StoreInst *SI = dyn_cast<StoreInst>(&I)) {
    3848      393170 :     if (SI->isAtomic())
    3849        1220 :       writeAtomic(SI->getContext(), SI->getOrdering(), SI->getSyncScopeID());
    3850      393170 :     if (SI->getAlignment())
    3851      383049 :       Out << ", align " << SI->getAlignment();
    3852             :   } else if (const AtomicCmpXchgInst *CXI = dyn_cast<AtomicCmpXchgInst>(&I)) {
    3853        1386 :     writeAtomicCmpXchg(CXI->getContext(), CXI->getSuccessOrdering(),
    3854         693 :                        CXI->getFailureOrdering(), CXI->getSyncScopeID());
    3855             :   } else if (const AtomicRMWInst *RMWI = dyn_cast<AtomicRMWInst>(&I)) {
    3856        1894 :     writeAtomic(RMWI->getContext(), RMWI->getOrdering(),
    3857         947 :                 RMWI->getSyncScopeID());
    3858             :   } else if (const FenceInst *FI = dyn_cast<FenceInst>(&I)) {
    3859         462 :     writeAtomic(FI->getContext(), FI->getOrdering(), FI->getSyncScopeID());
    3860             :   }
    3861             : 
    3862             :   // Print Metadata info.
    3863             :   SmallVector<std::pair<unsigned, MDNode *>, 4> InstMD;
    3864             :   I.getAllMetadata(InstMD);
    3865     2225079 :   printMetadataAttachments(InstMD, ", ");
    3866             : 
    3867             :   // Print a nice comment.
    3868     2225079 :   printInfoComment(I);
    3869     2225079 : }
    3870             : 
    3871     2509588 : void AssemblyWriter::printMetadataAttachments(
    3872             :     const SmallVectorImpl<std::pair<unsigned, MDNode *>> &MDs,
    3873             :     StringRef Separator) {
    3874     2509588 :   if (MDs.empty())
    3875             :     return;
    3876             : 
    3877      120200 :   if (MDNames.empty())
    3878       12304 :     MDs[0].second->getContext().getMDKindNames(MDNames);
    3879             : 
    3880      378702 :   for (const auto &I : MDs) {
    3881      129251 :     unsigned Kind = I.first;
    3882      129251 :     Out << Separator;
    3883      258502 :     if (Kind < MDNames.size()) {
    3884      129251 :       Out << "!";
    3885      258502 :       printMetadataIdentifier(MDNames[Kind], Out);
    3886             :     } else
    3887           0 :       Out << "!<unknown kind #" << Kind << ">";
    3888      129251 :     Out << ' ';
    3889      129251 :     WriteAsOperandInternal(Out, I.second, &TypePrinter, &Machine, TheModule);
    3890             :   }
    3891             : }
    3892             : 
    3893       96895 : void AssemblyWriter::writeMDNode(unsigned Slot, const MDNode *Node) {
    3894      193790 :   Out << '!' << Slot << " = ";
    3895             :   printMDNodeBody(Node);
    3896       96895 :   Out << "\n";
    3897       96895 : }
    3898             : 
    3899       10222 : void AssemblyWriter::writeAllMDNodes() {
    3900             :   SmallVector<const MDNode *, 16> Nodes;
    3901       20444 :   Nodes.resize(Machine.mdn_size());
    3902       20444 :   for (SlotTracker::mdn_iterator I = Machine.mdn_begin(), E = Machine.mdn_end();
    3903      107117 :        I != E; ++I)
    3904      193790 :     Nodes[I->second] = cast<MDNode>(I->first);
    3905             : 
    3906      107117 :   for (unsigned i = 0, e = Nodes.size(); i != e; ++i) {
    3907      193790 :     writeMDNode(i, Nodes[i]);
    3908             :   }
    3909       10222 : }
    3910             : 
    3911             : void AssemblyWriter::printMDNodeBody(const MDNode *Node) {
    3912       96895 :   WriteMDNodeBodyInternal(Out, Node, &TypePrinter, &Machine, TheModule);
    3913             : }
    3914             : 
    3915       11807 : void AssemblyWriter::writeAllAttributeGroups() {
    3916             :   std::vector<std::pair<AttributeSet, unsigned>> asVec;
    3917       23614 :   asVec.resize(Machine.as_size());
    3918             : 
    3919       23614 :   for (SlotTracker::as_iterator I = Machine.as_begin(), E = Machine.as_end();
    3920       42176 :        I != E; ++I)
    3921       30369 :     asVec[I->second] = *I;
    3922             : 
    3923       42176 :   for (const auto &I : asVec)
    3924       60738 :     Out << "attributes #" << I.second << " = { "
    3925       91107 :         << I.first.getAsString(true) << " }\n";
    3926       11807 : }
    3927             : 
    3928         855 : void AssemblyWriter::printUseListOrder(const UseListOrder &Order) {
    3929         855 :   bool IsInFunction = Machine.getFunction();
    3930         855 :   if (IsInFunction)
    3931         740 :     Out << "  ";
    3932             : 
    3933         855 :   Out << "uselistorder";
    3934             :   if (const BasicBlock *BB =
    3935         855 :           IsInFunction ? nullptr : dyn_cast<BasicBlock>(Order.V)) {
    3936           0 :     Out << "_bb ";
    3937           0 :     writeOperand(BB->getParent(), false);
    3938           0 :     Out << ", ";
    3939           0 :     writeOperand(BB, false);
    3940             :   } else {
    3941         855 :     Out << " ";
    3942         855 :     writeOperand(Order.V, true);
    3943             :   }
    3944         855 :   Out << ", { ";
    3945             : 
    3946             :   assert(Order.Shuffle.size() >= 2 && "Shuffle too small");
    3947         855 :   Out << Order.Shuffle[0];
    3948        4034 :   for (unsigned I = 1, E = Order.Shuffle.size(); I != E; ++I)
    3949        4648 :     Out << ", " << Order.Shuffle[I];
    3950         855 :   Out << " }\n";
    3951         855 : }
    3952             : 
    3953      148432 : void AssemblyWriter::printUseLists(const Function *F) {
    3954             :   auto hasMore =
    3955      149674 :       [&]() { return !UseListOrders.empty() && UseListOrders.back().F == F; };
    3956             :   if (!hasMore())
    3957             :     // Nothing to do.
    3958             :     return;
    3959             : 
    3960         387 :   Out << "\n; uselistorder directives\n";
    3961             :   while (hasMore()) {
    3962         855 :     printUseListOrder(UseListOrders.back());
    3963             :     UseListOrders.pop_back();
    3964             :   }
    3965             : }
    3966             : 
    3967             : //===----------------------------------------------------------------------===//
    3968             : //                       External Interface declarations
    3969             : //===----------------------------------------------------------------------===//
    3970             : 
    3971         135 : void Function::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
    3972             :                      bool ShouldPreserveUseListOrder,
    3973             :                      bool IsForDebug) const {
    3974         270 :   SlotTracker SlotTable(this->getParent());
    3975         135 :   formatted_raw_ostream OS(ROS);
    3976             :   AssemblyWriter W(OS, SlotTable, this->getParent(), AAW,
    3977             :                    IsForDebug,
    3978         270 :                    ShouldPreserveUseListOrder);
    3979         135 :   W.printFunction(this);
    3980         135 : }
    3981             : 
    3982       16686 : void Module::print(raw_ostream &ROS, AssemblyAnnotationWriter *AAW,
    3983             :                    bool ShouldPreserveUseListOrder, bool IsForDebug) const {
    3984       33372 :   SlotTracker SlotTable(this);
    3985       16686 :   formatted_raw_ostream OS(ROS);
    3986             :   AssemblyWriter W(OS, SlotTable, this, AAW, IsForDebug,
    3987       33372 :                    ShouldPreserveUseListOrder);
    3988       16686 :   W.printModule(this);
    3989       16686 : }
    3990             : 
    3991           1 : void NamedMDNode::print(raw_ostream &ROS, bool IsForDebug) const {
    3992           2 :   SlotTracker SlotTable(getParent());
    3993           1 :   formatted_raw_ostream OS(ROS);
    3994           2 :   AssemblyWriter W(OS, SlotTable, getParent(), nullptr, IsForDebug);
    3995           1 :   W.printNamedMDNode(this);
    3996           1 : }
    3997             : 
    3998           7 : void NamedMDNode::print(raw_ostream &ROS, ModuleSlotTracker &MST,
    3999             :                         bool IsForDebug) const {
    4000             :   Optional<SlotTracker> LocalST;
    4001             :   SlotTracker *SlotTable;
    4002           7 :   if (auto *ST = MST.getMachine())
    4003             :     SlotTable = ST;
    4004             :   else {
    4005           0 :     LocalST.emplace(getParent());
    4006             :     SlotTable = &*LocalST;
    4007             :   }
    4008             : 
    4009           7 :   formatted_raw_ostream OS(ROS);
    4010          14 :   AssemblyWriter W(OS, *SlotTable, getParent(), nullptr, IsForDebug);
    4011           7 :   W.printNamedMDNode(this);
    4012           7 : }
    4013             : 
    4014       25037 : void Comdat::print(raw_ostream &ROS, bool /*IsForDebug*/) const {
    4015       25037 :   PrintLLVMName(ROS, getName(), ComdatPrefix);
    4016       25037 :   ROS << " = comdat ";
    4017             : 
    4018       25037 :   switch (getSelectionKind()) {
    4019       24807 :   case Comdat::Any:
    4020       24807 :     ROS << "any";
    4021       24807 :     break;
    4022          11 :   case Comdat::ExactMatch:
    4023          11 :     ROS << "exactmatch";
    4024          11 :     break;
    4025         194 :   case Comdat::Largest:
    4026         194 :     ROS << "largest";
    4027         194 :     break;
    4028          16 :   case Comdat::NoDuplicates:
    4029          16 :     ROS << "noduplicates";
    4030          16 :     break;
    4031           9 :   case Comdat::SameSize:
    4032           9 :     ROS << "samesize";
    4033           9 :     break;
    4034             :   }
    4035             : 
    4036             :   ROS << '\n';
    4037       25037 : }
    4038             : 
    4039       34110 : void Type::print(raw_ostream &OS, bool /*IsForDebug*/, bool NoDetails) const {
    4040       34110 :   TypePrinting TP;
    4041       34110 :   TP.print(const_cast<Type*>(this), OS);
    4042             : 
    4043       34110 :   if (NoDetails)
    4044           0 :     return;
    4045             : 
    4046             :   // If the type is a named struct type, print the body as well.
    4047             :   if (StructType *STy = dyn_cast<StructType>(const_cast<Type*>(this)))
    4048          86 :     if (!STy->isLiteral()) {
    4049          84 :       OS << " = type ";
    4050          84 :       TP.printStructBody(STy, OS);
    4051             :     }
    4052             : }
    4053             : 
    4054       33795 : static bool isReferencingMDNode(const Instruction &I) {
    4055             :   if (const auto *CI = dyn_cast<CallInst>(&I))
    4056             :     if (Function *F = CI->getCalledFunction())
    4057        3251 :       if (F->isIntrinsic())
    4058       14346 :         for (auto &Op : I.operands())
    4059             :           if (auto *V = dyn_cast_or_null<MetadataAsValue>(Op))
    4060           7 :             if (isa<MDNode>(V->getMetadata()))
    4061             :               return true;
    4062             :   return false;
    4063             : }
    4064             : 
    4065       34318 : void Value::print(raw_ostream &ROS, bool IsForDebug) const {
    4066             :   bool ShouldInitializeAllMetadata = false;
    4067             :   if (auto *I = dyn_cast<Instruction>(this))
    4068       33795 :     ShouldInitializeAllMetadata = isReferencingMDNode(*I);
    4069         523 :   else if (isa<Function>(this) || isa<MetadataAsValue>(this))
    4070             :     ShouldInitializeAllMetadata = true;
    4071             : 
    4072       68636 :   ModuleSlotTracker MST(getModuleFromVal(this), ShouldInitializeAllMetadata);
    4073       34318 :   print(ROS, MST, IsForDebug);
    4074       34318 : }
    4075             : 
    4076       34589 : void Value::print(raw_ostream &ROS, ModuleSlotTracker &MST,
    4077             :                   bool IsForDebug) const {
    4078       34589 :   formatted_raw_ostream OS(ROS);
    4079       69178 :   SlotTracker EmptySlotTable(static_cast<const Module *>(nullptr));
    4080             :   SlotTracker &SlotTable =
    4081       34589 :       MST.getMachine() ? *MST.getMachine() : EmptySlotTable;
    4082             :   auto incorporateFunction = [&](const Function *F) {
    4083       34054 :     if (F)
    4084       34054 :       MST.incorporateFunction(*F);
    4085             :   };
    4086             : 
    4087             :   if (const Instruction *I = dyn_cast<Instruction>(this)) {
    4088       34064 :     incorporateFunction(I->getParent() ? I->getParent()->getParent() : nullptr);
    4089       68128 :     AssemblyWriter W(OS, SlotTable, getModuleFromVal(I), nullptr, IsForDebug);
    4090       34064 :     W.printInstruction(*I);
    4091             :   } else if (const BasicBlock *BB = dyn_cast<BasicBlock>(this)) {
    4092          28 :     incorporateFunction(BB->getParent());
    4093          56 :     AssemblyWriter W(OS, SlotTable, getModuleFromVal(BB), nullptr, IsForDebug);
    4094          28 :     W.printBasicBlock(BB);
    4095             :   } else if (const GlobalValue *GV = dyn_cast<GlobalValue>(this)) {
    4096         464 :     AssemblyWriter W(OS, SlotTable, GV->getParent(), nullptr, IsForDebug);
    4097             :     if (const GlobalVariable *V = dyn_cast<GlobalVariable>(GV))
    4098          10 :       W.printGlobal(V);
    4099             :     else if (const Function *F = dyn_cast<Function>(GV))
    4100         222 :       W.printFunction(F);
    4101             :     else
    4102           0 :       W.printIndirectSymbol(cast<GlobalIndirectSymbol>(GV));
    4103             :   } else if (const MetadataAsValue *V = dyn_cast<MetadataAsValue>(this)) {
    4104           4 :     V->getMetadata()->print(ROS, MST, getModuleFromVal(V));
    4105             :   } else if (const Constant *C = dyn_cast<Constant>(this)) {
    4106         101 :     TypePrinting TypePrinter;
    4107         101 :     TypePrinter.print(C->getType(), OS);
    4108             :     OS << ' ';
    4109         101 :     WriteConstantInternal(OS, C, TypePrinter, MST.getMachine(), nullptr);
    4110         160 :   } else if (isa<InlineAsm>(this) || isa<Argument>(this)) {
    4111         160 :     this->printAsOperand(OS, /* PrintType */ true, MST);
    4112             :   } else {
    4113           0 :     llvm_unreachable("Unknown value to print out!");
    4114             :   }
    4115       34589 : }
    4116             : 
    4117             : /// Print without a type, skipping the TypePrinting object.
    4118             : ///
    4119             : /// \return \c true iff printing was successful.
    4120      318563 : static bool printWithoutType(const Value &V, raw_ostream &O,
    4121             :                              SlotTracker *Machine, const Module *M) {
    4122      337078 :   if (V.hasName() || isa<GlobalValue>(V) ||
    4123         348 :       (!isa<Constant>(V) && !isa<MetadataAsValue>(V))) {
    4124      300392 :     WriteAsOperandInternal(O, &V, nullptr, Machine, M);
    4125      300392 :     return true;
    4126             :   }
    4127             :   return false;
    4128             : }
    4129             : 
    4130       30983 : static void printAsOperandImpl(const Value &V, raw_ostream &O, bool PrintType,
    4131             :                                ModuleSlotTracker &MST) {
    4132       61966 :   TypePrinting TypePrinter(MST.getModule());
    4133       30983 :   if (PrintType) {
    4134       12812 :     TypePrinter.print(V.getType(), O);
    4135             :     O << ' ';
    4136             :   }
    4137             : 
    4138       30983 :   WriteAsOperandInternal(O, &V, &TypePrinter, MST.getMachine(),
    4139             :                          MST.getModule());
    4140       30983 : }
    4141             : 
    4142      321610 : void Value::printAsOperand(raw_ostream &O, bool PrintType,
    4143             :                            const Module *M) const {
    4144      321610 :   if (!M)
    4145       41995 :     M = getModuleFromVal(this);
    4146             : 
    4147      321610 :   if (!PrintType)
    4148      310759 :     if (printWithoutType(*this, O, nullptr, M))
    4149      292590 :       return;
    4150             : 
    4151             :   SlotTracker Machine(
    4152       58040 :       M, /* ShouldInitializeAllMetadata */ isa<MetadataAsValue>(this));
    4153       58040 :   ModuleSlotTracker MST(Machine, M);
    4154       29020 :   printAsOperandImpl(*this, O, PrintType, MST);
    4155             : }
    4156             : 
    4157        9765 : void Value::printAsOperand(raw_ostream &O, bool PrintType,
    4158             :                            ModuleSlotTracker &MST) const {
    4159        9765 :   if (!PrintType)
    4160        7804 :     if (printWithoutType(*this, O, MST.getMachine(), MST.getModule()))
    4161             :       return;
    4162             : 
    4163        1963 :   printAsOperandImpl(*this, O, PrintType, MST);
    4164             : }
    4165             : 
    4166        2491 : static void printMetadataImpl(raw_ostream &ROS, const Metadata &MD,
    4167             :                               ModuleSlotTracker &MST, const Module *M,
    4168             :                               bool OnlyAsOperand) {
    4169         178 :   formatted_raw_ostream OS(ROS);
    4170             : 
    4171         178 :   TypePrinting TypePrinter(M);
    4172             : 
    4173        2491 :   WriteAsOperandInternal(OS, &MD, &TypePrinter, MST.getMachine(), M,
    4174             :                          /* FromValue */ true);
    4175             : 
    4176             :   auto *N = dyn_cast<MDNode>(&MD);
    4177        2491 :   if (OnlyAsOperand || !N || isa<DIExpression>(MD))
    4178        2313 :     return;
    4179             : 
    4180         178 :   OS << " = ";
    4181         178 :   WriteMDNodeBodyInternal(OS, N, &TypePrinter, MST.getMachine(), M);
    4182             : }
    4183             : 
    4184           2 : void Metadata::printAsOperand(raw_ostream &OS, const Module *M) const {
    4185           4 :   ModuleSlotTracker MST(M, isa<MDNode>(this));
    4186           2 :   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
    4187           2 : }
    4188             : 
    4189        2283 : void Metadata::printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
    4190             :                               const Module *M) const {
    4191        2283 :   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ true);
    4192        2283 : }
    4193             : 
    4194          10 : void Metadata::print(raw_ostream &OS, const Module *M,
    4195             :                      bool /*IsForDebug*/) const {
    4196          20 :   ModuleSlotTracker MST(M, isa<MDNode>(this));
    4197          10 :   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
    4198          10 : }
    4199             : 
    4200         196 : void Metadata::print(raw_ostream &OS, ModuleSlotTracker &MST,
    4201             :                      const Module *M, bool /*IsForDebug*/) const {
    4202         196 :   printMetadataImpl(OS, *this, MST, M, /* OnlyAsOperand */ false);
    4203         196 : }
    4204             : 
    4205          66 : void ModuleSummaryIndex::print(raw_ostream &ROS, bool IsForDebug) const {
    4206         132 :   SlotTracker SlotTable(this);
    4207          66 :   formatted_raw_ostream OS(ROS);
    4208         132 :   AssemblyWriter W(OS, SlotTable, this, IsForDebug);
    4209          66 :   W.printModuleSummaryIndex();
    4210          66 : }
    4211             : 
    4212             : #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
    4213             : // Value::dump - allow easy printing of Values from the debugger.
    4214             : LLVM_DUMP_METHOD
    4215             : void Value::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
    4216             : 
    4217             : // Type::dump - allow easy printing of Types from the debugger.
    4218             : LLVM_DUMP_METHOD
    4219             : void Type::dump() const { print(dbgs(), /*IsForDebug=*/true); dbgs() << '\n'; }
    4220             : 
    4221             : // Module::dump() - Allow printing of Modules from the debugger.
    4222             : LLVM_DUMP_METHOD
    4223             : void Module::dump() const {
    4224             :   print(dbgs(), nullptr,
    4225             :         /*ShouldPreserveUseListOrder=*/false, /*IsForDebug=*/true);
    4226             : }
    4227             : 
    4228             : // Allow printing of Comdats from the debugger.
    4229             : LLVM_DUMP_METHOD
    4230             : void Comdat::dump() const { print(dbgs(), /*IsForDebug=*/true); }
    4231             : 
    4232             : // NamedMDNode::dump() - Allow printing of NamedMDNodes from the debugger.
    4233             : LLVM_DUMP_METHOD
    4234             : void NamedMDNode::dump() const { print(dbgs(), /*IsForDebug=*/true); }
    4235             : 
    4236             : LLVM_DUMP_METHOD
    4237             : void Metadata::dump() const { dump(nullptr); }
    4238             : 
    4239             : LLVM_DUMP_METHOD
    4240             : void Metadata::dump(const Module *M) const {
    4241             :   print(dbgs(), M, /*IsForDebug=*/true);
    4242             :   dbgs() << '\n';
    4243             : }
    4244             : 
    4245             : // Allow printing of ModuleSummaryIndex from the debugger.
    4246             : LLVM_DUMP_METHOD
    4247             : void ModuleSummaryIndex::dump() const { print(dbgs(), /*IsForDebug=*/true); }
    4248             : #endif

Generated by: LCOV version 1.13