LCOV - code coverage report
Current view: top level - lib/CodeGen/AsmPrinter - DwarfDebug.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 1011 1067 94.8 %
Date: 2018-10-20 13:21:21 Functions: 85 89 95.5 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- llvm/CodeGen/DwarfDebug.cpp - Dwarf Debug Framework ----------------===//
       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 file contains support for writing dwarf debug info into asm files.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "DwarfDebug.h"
      15             : #include "ByteStreamer.h"
      16             : #include "DIEHash.h"
      17             : #include "DebugLocEntry.h"
      18             : #include "DebugLocStream.h"
      19             : #include "DwarfCompileUnit.h"
      20             : #include "DwarfExpression.h"
      21             : #include "DwarfFile.h"
      22             : #include "DwarfUnit.h"
      23             : #include "llvm/ADT/APInt.h"
      24             : #include "llvm/ADT/DenseMap.h"
      25             : #include "llvm/ADT/DenseSet.h"
      26             : #include "llvm/ADT/MapVector.h"
      27             : #include "llvm/ADT/STLExtras.h"
      28             : #include "llvm/ADT/SmallVector.h"
      29             : #include "llvm/ADT/StringRef.h"
      30             : #include "llvm/ADT/Triple.h"
      31             : #include "llvm/ADT/Twine.h"
      32             : #include "llvm/BinaryFormat/Dwarf.h"
      33             : #include "llvm/CodeGen/AccelTable.h"
      34             : #include "llvm/CodeGen/AsmPrinter.h"
      35             : #include "llvm/CodeGen/DIE.h"
      36             : #include "llvm/CodeGen/LexicalScopes.h"
      37             : #include "llvm/CodeGen/MachineBasicBlock.h"
      38             : #include "llvm/CodeGen/MachineFunction.h"
      39             : #include "llvm/CodeGen/MachineInstr.h"
      40             : #include "llvm/CodeGen/MachineModuleInfo.h"
      41             : #include "llvm/CodeGen/MachineOperand.h"
      42             : #include "llvm/CodeGen/TargetInstrInfo.h"
      43             : #include "llvm/CodeGen/TargetRegisterInfo.h"
      44             : #include "llvm/CodeGen/TargetSubtargetInfo.h"
      45             : #include "llvm/IR/Constants.h"
      46             : #include "llvm/IR/DebugInfoMetadata.h"
      47             : #include "llvm/IR/DebugLoc.h"
      48             : #include "llvm/IR/Function.h"
      49             : #include "llvm/IR/GlobalVariable.h"
      50             : #include "llvm/IR/Module.h"
      51             : #include "llvm/MC/MCAsmInfo.h"
      52             : #include "llvm/MC/MCContext.h"
      53             : #include "llvm/MC/MCDwarf.h"
      54             : #include "llvm/MC/MCSection.h"
      55             : #include "llvm/MC/MCStreamer.h"
      56             : #include "llvm/MC/MCSymbol.h"
      57             : #include "llvm/MC/MCTargetOptions.h"
      58             : #include "llvm/MC/MachineLocation.h"
      59             : #include "llvm/MC/SectionKind.h"
      60             : #include "llvm/Pass.h"
      61             : #include "llvm/Support/Casting.h"
      62             : #include "llvm/Support/CommandLine.h"
      63             : #include "llvm/Support/Debug.h"
      64             : #include "llvm/Support/ErrorHandling.h"
      65             : #include "llvm/Support/MD5.h"
      66             : #include "llvm/Support/MathExtras.h"
      67             : #include "llvm/Support/Timer.h"
      68             : #include "llvm/Support/raw_ostream.h"
      69             : #include "llvm/Target/TargetLoweringObjectFile.h"
      70             : #include "llvm/Target/TargetMachine.h"
      71             : #include "llvm/Target/TargetOptions.h"
      72             : #include <algorithm>
      73             : #include <cassert>
      74             : #include <cstddef>
      75             : #include <cstdint>
      76             : #include <iterator>
      77             : #include <string>
      78             : #include <utility>
      79             : #include <vector>
      80             : 
      81             : using namespace llvm;
      82             : 
      83             : #define DEBUG_TYPE "dwarfdebug"
      84             : 
      85             : static cl::opt<bool>
      86             : DisableDebugInfoPrinting("disable-debug-info-print", cl::Hidden,
      87             :                          cl::desc("Disable debug info printing"));
      88             : 
      89             : static cl::opt<bool> UseDwarfRangesBaseAddressSpecifier(
      90             :     "use-dwarf-ranges-base-address-specifier", cl::Hidden,
      91             :     cl::desc("Use base address specifiers in debug_ranges"), cl::init(false));
      92             : 
      93             : static cl::opt<bool> GenerateARangeSection("generate-arange-section",
      94             :                                            cl::Hidden,
      95             :                                            cl::desc("Generate dwarf aranges"),
      96             :                                            cl::init(false));
      97             : 
      98             : static cl::opt<bool>
      99             :     GenerateDwarfTypeUnits("generate-type-units", cl::Hidden,
     100             :                            cl::desc("Generate DWARF4 type units."),
     101             :                            cl::init(false));
     102             : 
     103             : static cl::opt<bool> SplitDwarfCrossCuReferences(
     104             :     "split-dwarf-cross-cu-references", cl::Hidden,
     105             :     cl::desc("Enable cross-cu references in DWO files"), cl::init(false));
     106             : 
     107             : enum DefaultOnOff { Default, Enable, Disable };
     108             : 
     109             : static cl::opt<DefaultOnOff> UnknownLocations(
     110             :     "use-unknown-locations", cl::Hidden,
     111             :     cl::desc("Make an absence of debug location information explicit."),
     112             :     cl::values(clEnumVal(Default, "At top of block or after label"),
     113             :                clEnumVal(Enable, "In all cases"), clEnumVal(Disable, "Never")),
     114             :     cl::init(Default));
     115             : 
     116             : static cl::opt<AccelTableKind> AccelTables(
     117             :     "accel-tables", cl::Hidden, cl::desc("Output dwarf accelerator tables."),
     118             :     cl::values(clEnumValN(AccelTableKind::Default, "Default",
     119             :                           "Default for platform"),
     120             :                clEnumValN(AccelTableKind::None, "Disable", "Disabled."),
     121             :                clEnumValN(AccelTableKind::Apple, "Apple", "Apple"),
     122             :                clEnumValN(AccelTableKind::Dwarf, "Dwarf", "DWARF")),
     123             :     cl::init(AccelTableKind::Default));
     124             : 
     125             : static cl::opt<DefaultOnOff>
     126             : DwarfInlinedStrings("dwarf-inlined-strings", cl::Hidden,
     127             :                  cl::desc("Use inlined strings rather than string section."),
     128             :                  cl::values(clEnumVal(Default, "Default for platform"),
     129             :                             clEnumVal(Enable, "Enabled"),
     130             :                             clEnumVal(Disable, "Disabled")),
     131             :                  cl::init(Default));
     132             : 
     133             : static cl::opt<bool>
     134             :     NoDwarfRangesSection("no-dwarf-ranges-section", cl::Hidden,
     135             :                          cl::desc("Disable emission .debug_ranges section."),
     136             :                          cl::init(false));
     137             : 
     138             : static cl::opt<DefaultOnOff> DwarfSectionsAsReferences(
     139             :     "dwarf-sections-as-references", cl::Hidden,
     140             :     cl::desc("Use sections+offset as references rather than labels."),
     141             :     cl::values(clEnumVal(Default, "Default for platform"),
     142             :                clEnumVal(Enable, "Enabled"), clEnumVal(Disable, "Disabled")),
     143             :     cl::init(Default));
     144             : 
     145             : enum LinkageNameOption {
     146             :   DefaultLinkageNames,
     147             :   AllLinkageNames,
     148             :   AbstractLinkageNames
     149             : };
     150             : 
     151             : static cl::opt<LinkageNameOption>
     152             :     DwarfLinkageNames("dwarf-linkage-names", cl::Hidden,
     153             :                       cl::desc("Which DWARF linkage-name attributes to emit."),
     154             :                       cl::values(clEnumValN(DefaultLinkageNames, "Default",
     155             :                                             "Default for platform"),
     156             :                                  clEnumValN(AllLinkageNames, "All", "All"),
     157             :                                  clEnumValN(AbstractLinkageNames, "Abstract",
     158             :                                             "Abstract subprograms")),
     159             :                       cl::init(DefaultLinkageNames));
     160             : 
     161             : static const char *const DWARFGroupName = "dwarf";
     162             : static const char *const DWARFGroupDescription = "DWARF Emission";
     163             : static const char *const DbgTimerName = "writer";
     164             : static const char *const DbgTimerDescription = "DWARF Debug Writer";
     165             : 
     166      106256 : void DebugLocDwarfExpression::emitOp(uint8_t Op, const char *Comment) {
     167      106256 :   BS.EmitInt8(
     168        2472 :       Op, Comment ? Twine(Comment) + " " + dwarf::OperationEncodingString(Op)
     169      106256 :                   : dwarf::OperationEncodingString(Op));
     170      106256 : }
     171             : 
     172       20180 : void DebugLocDwarfExpression::emitSigned(int64_t Value) {
     173       40360 :   BS.EmitSLEB128(Value, Twine(Value));
     174       20180 : }
     175             : 
     176        4823 : void DebugLocDwarfExpression::emitUnsigned(uint64_t Value) {
     177        9646 :   BS.EmitULEB128(Value, Twine(Value));
     178        4823 : }
     179             : 
     180       20105 : bool DebugLocDwarfExpression::isFrameRegister(const TargetRegisterInfo &TRI,
     181             :                                               unsigned MachineReg) {
     182             :   // This information is not available while emitting .debug_loc entries.
     183       20105 :   return false;
     184             : }
     185             : 
     186           0 : bool DbgVariable::isBlockByrefVariable() const {
     187             :   assert(getVariable() && "Invalid complex DbgVariable!");
     188           0 :   return getVariable()->getType().resolve()->isBlockByrefStruct();
     189             : }
     190             : 
     191      100291 : const DIType *DbgVariable::getType() const {
     192             :   DIType *Ty = getVariable()->getType().resolve();
     193             :   // FIXME: isBlockByrefVariable should be reformulated in terms of complex
     194             :   // addresses instead.
     195      100291 :   if (Ty->isBlockByrefStruct()) {
     196             :     /* Byref variables, in Blocks, are declared by the programmer as
     197             :        "SomeType VarName;", but the compiler creates a
     198             :        __Block_byref_x_VarName struct, and gives the variable VarName
     199             :        either the struct, or a pointer to the struct, as its type.  This
     200             :        is necessary for various behind-the-scenes things the compiler
     201             :        needs to do with by-reference variables in blocks.
     202             : 
     203             :        However, as far as the original *programmer* is concerned, the
     204             :        variable should still have type 'SomeType', as originally declared.
     205             : 
     206             :        The following function dives into the __Block_byref_x_VarName
     207             :        struct to find the original type of the variable.  This will be
     208             :        passed back to the code generating the type for the Debug
     209             :        Information Entry for the variable 'VarName'.  'VarName' will then
     210             :        have the original type 'SomeType' in its debug information.
     211             : 
     212             :        The original type 'SomeType' will be the type of the field named
     213             :        'VarName' inside the __Block_byref_x_VarName struct.
     214             : 
     215             :        NOTE: In order for this to not completely fail on the debugger
     216             :        side, the Debug Information Entry for the variable VarName needs to
     217             :        have a DW_AT_location that tells the debugger how to unwind through
     218             :        the pointers and __Block_byref_x_VarName struct to find the actual
     219             :        value of the variable.  The function addBlockByrefType does this.  */
     220             :     DIType *subType = Ty;
     221           4 :     uint16_t tag = Ty->getTag();
     222             : 
     223           4 :     if (tag == dwarf::DW_TAG_pointer_type)
     224             :       subType = resolve(cast<DIDerivedType>(Ty)->getBaseType());
     225             : 
     226             :     auto Elements = cast<DICompositeType>(subType)->getElements();
     227          28 :     for (unsigned i = 0, N = Elements.size(); i < N; ++i) {
     228             :       auto *DT = cast<DIDerivedType>(Elements[i]);
     229             :       if (getName() == DT->getName())
     230             :         return resolve(DT->getBaseType());
     231             :     }
     232             :   }
     233             :   return Ty;
     234             : }
     235             : 
     236         639 : ArrayRef<DbgVariable::FrameIndexExpr> DbgVariable::getFrameIndexExprs() const {
     237        1278 :   if (FrameIndexExprs.size() == 1)
     238         636 :     return FrameIndexExprs;
     239             : 
     240             :   assert(llvm::all_of(FrameIndexExprs,
     241             :                       [](const FrameIndexExpr &A) {
     242             :                         return A.Expr->isFragment();
     243             :                       }) &&
     244             :          "multiple FI expressions without DW_OP_LLVM_fragment");
     245             :   llvm::sort(FrameIndexExprs,
     246             :              [](const FrameIndexExpr &A, const FrameIndexExpr &B) -> bool {
     247             :                return A.Expr->getFragmentInfo()->OffsetInBits <
     248             :                       B.Expr->getFragmentInfo()->OffsetInBits;
     249             :              });
     250             : 
     251           3 :   return FrameIndexExprs;
     252             : }
     253             : 
     254           5 : void DbgVariable::addMMIEntry(const DbgVariable &V) {
     255             :   assert(DebugLocListIndex == ~0U && !MInsn && "not an MMI entry");
     256             :   assert(V.DebugLocListIndex == ~0U && !V.MInsn && "not an MMI entry");
     257             :   assert(V.getVariable() == getVariable() && "conflicting variable");
     258             :   assert(V.getInlinedAt() == getInlinedAt() && "conflicting inlined-at location");
     259             : 
     260             :   assert(!FrameIndexExprs.empty() && "Expected an MMI entry");
     261             :   assert(!V.FrameIndexExprs.empty() && "Expected an MMI entry");
     262             : 
     263             :   // FIXME: This logic should not be necessary anymore, as we now have proper
     264             :   // deduplication. However, without it, we currently run into the assertion
     265             :   // below, which means that we are likely dealing with broken input, i.e. two
     266             :   // non-fragment entries for the same variable at different frame indices.
     267          10 :   if (FrameIndexExprs.size()) {
     268           5 :     auto *Expr = FrameIndexExprs.back().Expr;
     269          10 :     if (!Expr || !Expr->isFragment())
     270           2 :       return;
     271             :   }
     272             : 
     273           6 :   for (const auto &FIE : V.FrameIndexExprs)
     274             :     // Ignore duplicate entries.
     275           3 :     if (llvm::none_of(FrameIndexExprs, [&](const FrameIndexExpr &Other) {
     276           0 :           return FIE.FI == Other.FI && FIE.Expr == Other.Expr;
     277             :         }))
     278           3 :       FrameIndexExprs.push_back(FIE);
     279             : 
     280             :   assert((FrameIndexExprs.size() == 1 ||
     281             :           llvm::all_of(FrameIndexExprs,
     282             :                        [](FrameIndexExpr &FIE) {
     283             :                          return FIE.Expr && FIE.Expr->isFragment();
     284             :                        })) &&
     285             :          "conflicting locations for variable");
     286             : }
     287             : 
     288             : static AccelTableKind computeAccelTableKind(unsigned DwarfVersion,
     289             :                                             bool GenerateTypeUnits,
     290             :                                             DebuggerKind Tuning,
     291             :                                             const Triple &TT) {
     292             :   // Honor an explicit request.
     293       26982 :   if (AccelTables != AccelTableKind::Default)
     294             :     return AccelTables;
     295             : 
     296             :   // Accelerator tables with type units are currently not supported.
     297       26965 :   if (GenerateTypeUnits)
     298             :     return AccelTableKind::None;
     299             : 
     300             :   // Accelerator tables get emitted if targetting DWARF v5 or LLDB.  DWARF v5
     301             :   // always implies debug_names. For lower standard versions we use apple
     302             :   // accelerator tables on apple platforms and debug_names elsewhere.
     303       26948 :   if (DwarfVersion >= 5)
     304             :     return AccelTableKind::Dwarf;
     305       26917 :   if (Tuning == DebuggerKind::LLDB)
     306        2428 :     return TT.isOSBinFormatMachO() ? AccelTableKind::Apple
     307             :                                    : AccelTableKind::Dwarf;
     308             :   return AccelTableKind::None;
     309             : }
     310             : 
     311       26982 : DwarfDebug::DwarfDebug(AsmPrinter *A, Module *M)
     312       26982 :     : DebugHandlerBase(A), DebugLocs(A->OutStreamer->isVerboseAsm()),
     313             :       InfoHolder(A, "info_string", DIEValueAllocator),
     314             :       SkeletonHolder(A, "skel_string", DIEValueAllocator),
     315      133345 :       IsDarwin(A->TM.getTargetTriple().isOSDarwin()) {
     316       26981 :   const Triple &TT = Asm->TM.getTargetTriple();
     317             : 
     318             :   // Make sure we know our "debugger tuning."  The target option takes
     319             :   // precedence; fall back to triple-based defaults.
     320       26981 :   if (Asm->TM.Options.DebuggerTuning != DebuggerKind::Default)
     321        6092 :     DebuggerTuning = Asm->TM.Options.DebuggerTuning;
     322       20889 :   else if (IsDarwin)
     323        2411 :     DebuggerTuning = DebuggerKind::LLDB;
     324             :   else if (TT.isPS4CPU())
     325          39 :     DebuggerTuning = DebuggerKind::SCE;
     326             :   else
     327       18439 :     DebuggerTuning = DebuggerKind::GDB;
     328             : 
     329       26981 :   if (DwarfInlinedStrings == Default)
     330       26978 :     UseInlineStrings = TT.isNVPTX();
     331             :   else
     332           3 :     UseInlineStrings = DwarfInlinedStrings == Enable;
     333             : 
     334       26981 :   UseLocSection = !TT.isNVPTX();
     335             : 
     336       26981 :   HasAppleExtensionAttributes = tuneForLLDB();
     337             : 
     338             :   // Handle split DWARF.
     339       53962 :   HasSplitDwarf = !Asm->TM.Options.MCOptions.SplitDwarfFile.empty();
     340             : 
     341             :   // SCE defaults to linkage names only for abstract subprograms.
     342       26981 :   if (DwarfLinkageNames == DefaultLinkageNames)
     343       26969 :     UseAllLinkageNames = !tuneForSCE();
     344             :   else
     345          12 :     UseAllLinkageNames = DwarfLinkageNames == AllLinkageNames;
     346             : 
     347       26981 :   unsigned DwarfVersionNumber = Asm->TM.Options.MCOptions.DwarfVersion;
     348       26981 :   unsigned DwarfVersion = DwarfVersionNumber ? DwarfVersionNumber
     349       26915 :                                     : MMI->getModule()->getDwarfVersion();
     350             :   // Use dwarf 4 by default if nothing is requested. For NVPTX, use dwarf 2.
     351             :   DwarfVersion =
     352       26740 :       TT.isNVPTX() ? 2 : (DwarfVersion ? DwarfVersion : dwarf::DWARF_VERSION);
     353             : 
     354       53962 :   UseRangesSection = !NoDwarfRangesSection && !TT.isNVPTX();
     355             : 
     356             :   // Use sections as references. Force for NVPTX.
     357       26982 :   if (DwarfSectionsAsReferences == Default)
     358       26981 :     UseSectionsAsReferences = TT.isNVPTX();
     359             :   else
     360           1 :     UseSectionsAsReferences = DwarfSectionsAsReferences == Enable;
     361             : 
     362             :   // Don't generate type units for unsupported object file formats.
     363       26982 :   GenerateTypeUnits =
     364       53964 :       A->TM.getTargetTriple().isOSBinFormatELF() && GenerateDwarfTypeUnits;
     365             : 
     366       53964 :   TheAccelTableKind = computeAccelTableKind(
     367       26982 :       DwarfVersion, GenerateTypeUnits, DebuggerTuning, A->TM.getTargetTriple());
     368             : 
     369             :   // Work around a GDB bug. GDB doesn't support the standard opcode;
     370             :   // SCE doesn't support GNU's; LLDB prefers the standard opcode, which
     371             :   // is defined as of DWARF 3.
     372             :   // See GDB bug 11616 - DW_OP_form_tls_address is unimplemented
     373             :   // https://sourceware.org/bugzilla/show_bug.cgi?id=11616
     374       26982 :   UseGNUTLSOpcode = tuneForGDB() || DwarfVersion < 3;
     375             : 
     376             :   // GDB does not fully support the DWARF 4 representation for bitfields.
     377       26982 :   UseDWARF2Bitfields = (DwarfVersion < 4) || tuneForGDB();
     378             : 
     379             :   // The DWARF v5 string offsets table has - possibly shared - contributions
     380             :   // from each compile and type unit each preceded by a header. The string
     381             :   // offsets table used by the pre-DWARF v5 split-DWARF implementation uses
     382             :   // a monolithic string offsets table without any header.
     383       26982 :   UseSegmentedStringOffsetsTable = DwarfVersion >= 5;
     384             : 
     385       53964 :   Asm->OutStreamer->getContext().setDwarfVersion(DwarfVersion);
     386       26982 : }
     387             : 
     388             : // Define out of line so we don't have to include DwarfUnit.h in DwarfDebug.h.
     389             : DwarfDebug::~DwarfDebug() = default;
     390             : 
     391             : static bool isObjCClass(StringRef Name) {
     392             :   return Name.startswith("+") || Name.startswith("-");
     393             : }
     394             : 
     395          10 : static bool hasObjCCategory(StringRef Name) {
     396             :   if (!isObjCClass(Name))
     397             :     return false;
     398             : 
     399          10 :   return Name.find(") ") != StringRef::npos;
     400             : }
     401             : 
     402          10 : static void getObjCClassCategory(StringRef In, StringRef &Class,
     403             :                                  StringRef &Category) {
     404          10 :   if (!hasObjCCategory(In)) {
     405          20 :     Class = In.slice(In.find('[') + 1, In.find(' '));
     406          10 :     Category = "";
     407          10 :     return;
     408             :   }
     409             : 
     410           0 :   Class = In.slice(In.find('[') + 1, In.find('('));
     411           0 :   Category = In.slice(In.find('[') + 1, In.find(' '));
     412             : }
     413             : 
     414          10 : static StringRef getObjCMethodName(StringRef In) {
     415          20 :   return In.slice(In.find(' ') + 1, In.find(']'));
     416             : }
     417             : 
     418             : // Add the various names to the Dwarf accelerator table names.
     419      167341 : void DwarfDebug::addSubprogramNames(const DICompileUnit &CU,
     420             :                                     const DISubprogram *SP, DIE &Die) {
     421      167341 :   if (getAccelTableKind() != AccelTableKind::Apple &&
     422      167015 :       CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None)
     423             :     return;
     424             : 
     425         998 :   if (!SP->isDefinition())
     426             :     return;
     427             : 
     428             :   if (SP->getName() != "")
     429         992 :     addAccelName(CU, SP->getName(), Die);
     430             : 
     431             :   // If the linkage name is different than the name, go ahead and output that as
     432             :   // well into the name table. Only do that if we are going to actually emit
     433             :   // that name.
     434         422 :   if (SP->getLinkageName() != "" && SP->getName() != SP->getLinkageName() &&
     435         450 :       (useAllLinkageNames() || InfoHolder.getAbstractSPDies().lookup(SP)))
     436         399 :     addAccelName(CU, SP->getLinkageName(), Die);
     437             : 
     438             :   // If this is an Objective-C selector name add it to the ObjC accelerator
     439             :   // too.
     440             :   if (isObjCClass(SP->getName())) {
     441          10 :     StringRef Class, Category;
     442          10 :     getObjCClassCategory(SP->getName(), Class, Category);
     443          10 :     addAccelObjC(CU, Class, Die);
     444             :     if (Category != "")
     445           0 :       addAccelObjC(CU, Category, Die);
     446             :     // Also add the base method name to the name table.
     447          10 :     addAccelName(CU, getObjCMethodName(SP->getName()), Die);
     448             :   }
     449             : }
     450             : 
     451             : /// Check whether we should create a DIE for the given Scope, return true
     452             : /// if we don't create a DIE (the corresponding DIE is null).
     453       29760 : bool DwarfDebug::isLexicalScopeDIENull(LexicalScope *Scope) {
     454       29760 :   if (Scope->isAbstractScope())
     455             :     return false;
     456             : 
     457             :   // We don't create a DIE if there is no Range.
     458             :   const SmallVectorImpl<InsnRange> &Ranges = Scope->getRanges();
     459       25513 :   if (Ranges.empty())
     460             :     return true;
     461             : 
     462       25513 :   if (Ranges.size() > 1)
     463             :     return false;
     464             : 
     465             :   // We don't create a DIE if we have a single Range and the end label
     466             :   // is null.
     467       13296 :   return !getLabelAfterInsn(Ranges.front().second);
     468             : }
     469             : 
     470        8412 : template <typename Func> static void forBothCUs(DwarfCompileUnit &CU, Func F) {
     471             :   F(CU);
     472        8412 :   if (auto *SkelCU = CU.getSkeleton())
     473          46 :     if (CU.getCUNode()->getSplitDebugInlining())
     474             :       F(*SkelCU);
     475        8412 : }
     476             : 
     477         882 : bool DwarfDebug::shareAcrossDWOCUs() const {
     478         882 :   return SplitDwarfCrossCuReferences;
     479             : }
     480             : 
     481       61919 : void DwarfDebug::constructAbstractSubprogramScopeDIE(DwarfCompileUnit &SrcCU,
     482             :                                                      LexicalScope *Scope) {
     483             :   assert(Scope && Scope->getScopeNode());
     484             :   assert(Scope->isAbstractScope());
     485             :   assert(!Scope->getInlinedAt());
     486             : 
     487       61919 :   auto *SP = cast<DISubprogram>(Scope->getScopeNode());
     488             : 
     489             :   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
     490             :   // was inlined from another compile unit.
     491       61924 :   if (useSplitDwarf() && !shareAcrossDWOCUs() && !SP->getUnit()->getSplitDebugInlining())
     492             :     // Avoid building the original CU if it won't be used
     493           2 :     SrcCU.constructAbstractSubprogramScopeDIE(Scope);
     494             :   else {
     495       61917 :     auto &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
     496       61917 :     if (auto *SkelCU = CU.getSkeleton()) {
     497           5 :       (shareAcrossDWOCUs() ? CU : SrcCU)
     498           8 :           .constructAbstractSubprogramScopeDIE(Scope);
     499           5 :       if (CU.getCUNode()->getSplitDebugInlining())
     500           5 :         SkelCU->constructAbstractSubprogramScopeDIE(Scope);
     501             :     } else
     502       61912 :       CU.constructAbstractSubprogramScopeDIE(Scope);
     503             :   }
     504       61919 : }
     505             : 
     506        8412 : void DwarfDebug::constructCallSiteEntryDIEs(const DISubprogram &SP,
     507             :                                             DwarfCompileUnit &CU, DIE &ScopeDIE,
     508             :                                             const MachineFunction &MF) {
     509             :   // Add a call site-related attribute (DWARF5, Sec. 3.3.1.3). Do this only if
     510             :   // the subprogram is required to have one.
     511        8412 :   if (!SP.areAllCallsDescribed() || !SP.isDefinition())
     512             :     return;
     513             : 
     514             :   // Use DW_AT_call_all_calls to express that call site entries are present
     515             :   // for both tail and non-tail calls. Don't use DW_AT_call_all_source_calls
     516             :   // because one of its requirements is not met: call site entries for
     517             :   // optimized-out calls are elided.
     518           8 :   CU.addFlag(ScopeDIE, dwarf::DW_AT_call_all_calls);
     519             : 
     520           8 :   const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo();
     521             :   assert(TII && "TargetInstrInfo not found: cannot label tail calls");
     522             : 
     523             :   // Emit call site entries for each call or tail call in the function.
     524          16 :   for (const MachineBasicBlock &MBB : MF) {
     525          50 :     for (const MachineInstr &MI : MBB.instrs()) {
     526             :       // Skip instructions which aren't calls. Both calls and tail-calling jump
     527             :       // instructions (e.g TAILJMPd64) are classified correctly here.
     528          42 :       if (!MI.isCall())
     529             :         continue;
     530             : 
     531             :       // TODO: Add support for targets with delay slots (see: beginInstruction).
     532          12 :       if (MI.hasDelaySlot())
     533             :         return;
     534             : 
     535             :       // If this is a direct call, find the callee's subprogram.
     536          12 :       const MachineOperand &CalleeOp = MI.getOperand(0);
     537          12 :       if (!CalleeOp.isGlobal())
     538             :         continue;
     539          10 :       const Function *CalleeDecl = dyn_cast<Function>(CalleeOp.getGlobal());
     540          10 :       if (!CalleeDecl || !CalleeDecl->getSubprogram())
     541           0 :         continue;
     542             : 
     543             :       // TODO: Omit call site entries for runtime calls (objc_msgSend, etc).
     544             :       // TODO: Add support for indirect calls.
     545             : 
     546          10 :       bool IsTail = TII->isTailCall(MI);
     547             : 
     548             :       // For tail calls, no return PC information is needed. For regular calls,
     549             :       // the return PC is needed to disambiguate paths in the call graph which
     550             :       // could lead to some target function.
     551          10 :       const MCSymbol *ReturnPC = IsTail ? nullptr : getLabelAfterInsn(&MI);
     552             : 
     553             :       assert((IsTail || ReturnPC) && "Call without return PC information");
     554             :       LLVM_DEBUG(dbgs() << "CallSiteEntry: " << MF.getName() << " -> "
     555             :                         << CalleeDecl->getName() << (IsTail ? " [tail]" : "")
     556             :                         << "\n");
     557          10 :       CU.constructCallSiteEntryDIE(ScopeDIE, *CalleeDecl->getSubprogram(),
     558          10 :                                    IsTail, ReturnPC);
     559             :     }
     560             :   }
     561             : }
     562             : 
     563        1346 : void DwarfDebug::addGnuPubAttributes(DwarfCompileUnit &U, DIE &D) const {
     564        1346 :   if (!U.hasDwarfPubSections())
     565             :     return;
     566             : 
     567         760 :   U.addFlag(D, dwarf::DW_AT_GNU_pubnames);
     568             : }
     569             : 
     570             : // Create new DwarfCompileUnit for the given metadata node with tag
     571             : // DW_TAG_compile_unit.
     572             : DwarfCompileUnit &
     573       80528 : DwarfDebug::getOrCreateDwarfCompileUnit(const DICompileUnit *DIUnit) {
     574       80528 :   if (auto *CU = CUMap.lookup(DIUnit))
     575             :     return *CU;
     576        1345 :   StringRef FN = DIUnit->getFilename();
     577        1346 :   CompilationDir = DIUnit->getDirectory();
     578             : 
     579             :   auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
     580        1346 :       InfoHolder.getUnits().size(), DIUnit, Asm, this, &InfoHolder);
     581             :   DwarfCompileUnit &NewCU = *OwnedUnit;
     582             :   DIE &Die = NewCU.getUnitDie();
     583        1346 :   InfoHolder.addUnit(std::move(OwnedUnit));
     584        1346 :   if (useSplitDwarf()) {
     585          43 :     NewCU.setSkeleton(constructSkeletonCU(NewCU));
     586          43 :     NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
     587          43 :                   Asm->TM.Options.MCOptions.SplitDwarfFile);
     588             :   }
     589             : 
     590       17985 :   for (auto *IE : DIUnit->getImportedEntities())
     591       16639 :     NewCU.addImportedEntity(IE);
     592             : 
     593             :   // LTO with assembly output shares a single line table amongst multiple CUs.
     594             :   // To avoid the compilation directory being ambiguous, let the line table
     595             :   // explicitly describe the directory of all files, never relying on the
     596             :   // compilation directory.
     597        2692 :   if (!Asm->OutStreamer->hasRawTextSupport() || SingleCU)
     598        2647 :     Asm->OutStreamer->emitDwarfFile0Directive(
     599             :         CompilationDir, FN, NewCU.getMD5AsBytes(DIUnit->getFile()),
     600        2647 :         DIUnit->getSource(), NewCU.getUniqueID());
     601             : 
     602             :   StringRef Producer = DIUnit->getProducer();
     603             :   StringRef Flags = DIUnit->getFlags();
     604        1346 :   if (!Flags.empty() && !useAppleExtensionAttributes()) {
     605           2 :     std::string ProducerWithFlags = Producer.str() + " " + Flags.str();
     606           2 :     NewCU.addString(Die, dwarf::DW_AT_producer, ProducerWithFlags);
     607             :   } else
     608        1345 :     NewCU.addString(Die, dwarf::DW_AT_producer, Producer);
     609             : 
     610        1346 :   NewCU.addUInt(Die, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
     611        1346 :                 DIUnit->getSourceLanguage());
     612        1346 :   NewCU.addString(Die, dwarf::DW_AT_name, FN);
     613             : 
     614             :   // Add DW_str_offsets_base to the unit DIE, except for split units.
     615        1346 :   if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
     616          37 :     NewCU.addStringOffsetsStart();
     617             : 
     618        1346 :   if (!useSplitDwarf()) {
     619        1303 :     NewCU.initStmtList();
     620             : 
     621             :     // If we're using split dwarf the compilation dir is going to be in the
     622             :     // skeleton CU and so we don't need to duplicate it here.
     623        1303 :     if (!CompilationDir.empty())
     624        1249 :       NewCU.addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
     625             : 
     626        1303 :     addGnuPubAttributes(NewCU, Die);
     627             :   }
     628             : 
     629        1346 :   if (useAppleExtensionAttributes()) {
     630         221 :     if (DIUnit->isOptimized())
     631          96 :       NewCU.addFlag(Die, dwarf::DW_AT_APPLE_optimized);
     632             : 
     633         221 :     StringRef Flags = DIUnit->getFlags();
     634         221 :     if (!Flags.empty())
     635           1 :       NewCU.addString(Die, dwarf::DW_AT_APPLE_flags, Flags);
     636             : 
     637         221 :     if (unsigned RVer = DIUnit->getRuntimeVersion())
     638          30 :       NewCU.addUInt(Die, dwarf::DW_AT_APPLE_major_runtime_vers,
     639             :                     dwarf::DW_FORM_data1, RVer);
     640             :   }
     641             : 
     642        1346 :   if (useSplitDwarf())
     643          43 :     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoDWOSection());
     644             :   else
     645        1303 :     NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
     646             : 
     647        1346 :   if (DIUnit->getDWOId()) {
     648             :     // This CU is either a clang module DWO or a skeleton CU.
     649          28 :     NewCU.addUInt(Die, dwarf::DW_AT_GNU_dwo_id, dwarf::DW_FORM_data8,
     650             :                   DIUnit->getDWOId());
     651          28 :     if (!DIUnit->getSplitDebugFilename().empty())
     652             :       // This is a prefabricated skeleton CU.
     653           5 :       NewCU.addString(Die, dwarf::DW_AT_GNU_dwo_name,
     654             :                       DIUnit->getSplitDebugFilename());
     655             :   }
     656             : 
     657        1346 :   CUMap.insert({DIUnit, &NewCU});
     658        1346 :   CUDieMap.insert({&Die, &NewCU});
     659             :   return NewCU;
     660             : }
     661             : 
     662       16637 : void DwarfDebug::constructAndAddImportedEntityDIE(DwarfCompileUnit &TheCU,
     663             :                                                   const DIImportedEntity *N) {
     664             :   if (isa<DILocalScope>(N->getScope()))
     665             :     return;
     666       16614 :   if (DIE *D = TheCU.getOrCreateContextDIE(N->getScope()))
     667       16614 :     D->addChild(TheCU.constructImportedEntityDIE(N));
     668             : }
     669             : 
     670             : /// Sort and unique GVEs by comparing their fragment offset.
     671             : static SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &
     672        1090 : sortGlobalExprs(SmallVectorImpl<DwarfCompileUnit::GlobalExpr> &GVEs) {
     673             :   llvm::sort(
     674             :       GVEs, [](DwarfCompileUnit::GlobalExpr A, DwarfCompileUnit::GlobalExpr B) {
     675             :         // Sort order: first null exprs, then exprs without fragment
     676             :         // info, then sort by fragment offset in bits.
     677             :         // FIXME: Come up with a more comprehensive comparator so
     678             :         // the sorting isn't non-deterministic, and so the following
     679             :         // std::unique call works correctly.
     680             :         if (!A.Expr || !B.Expr)
     681             :           return !!B.Expr;
     682             :         auto FragmentA = A.Expr->getFragmentInfo();
     683             :         auto FragmentB = B.Expr->getFragmentInfo();
     684             :         if (!FragmentA || !FragmentB)
     685             :           return !!FragmentB;
     686             :         return FragmentA->OffsetInBits < FragmentB->OffsetInBits;
     687             :       });
     688        1090 :   GVEs.erase(std::unique(GVEs.begin(), GVEs.end(),
     689             :                          [](DwarfCompileUnit::GlobalExpr A,
     690             :                             DwarfCompileUnit::GlobalExpr B) {
     691           0 :                            return A.Expr == B.Expr;
     692             :                          }),
     693             :              GVEs.end());
     694        1090 :   return GVEs;
     695             : }
     696             : 
     697             : // Emit all Dwarf sections that should come prior to the content. Create
     698             : // global DIEs and emit initial debug info sections. This is invoked by
     699             : // the target AsmPrinter.
     700       26982 : void DwarfDebug::beginModule() {
     701             :   NamedRegionTimer T(DbgTimerName, DbgTimerDescription, DWARFGroupName,
     702       53964 :                      DWARFGroupDescription, TimePassesIsEnabled);
     703       26982 :   if (DisableDebugInfoPrinting)
     704             :     return;
     705             : 
     706       26982 :   const Module *M = MMI->getModule();
     707             : 
     708       26982 :   unsigned NumDebugCUs = std::distance(M->debug_compile_units_begin(),
     709       26982 :                                        M->debug_compile_units_end());
     710             :   // Tell MMI whether we have debug info.
     711       26982 :   MMI->setDebugInfoAvailability(NumDebugCUs > 0);
     712       26982 :   SingleCU = NumDebugCUs == 1;
     713             :   DenseMap<DIGlobalVariable *, SmallVector<DwarfCompileUnit::GlobalExpr, 1>>
     714             :       GVMap;
     715      317702 :   for (const GlobalVariable &Global : M->globals()) {
     716             :     SmallVector<DIGlobalVariableExpression *, 1> GVs;
     717      290720 :     Global.getDebugInfo(GVs);
     718      291649 :     for (auto *GVE : GVs)
     719         929 :       GVMap[GVE->getVariable()].push_back({&Global, GVE->getExpression()});
     720             :   }
     721             : 
     722             :   // Create the symbol that designates the start of the unit's contribution
     723             :   // to the string offsets table. In a split DWARF scenario, only the skeleton
     724             :   // unit has the DW_AT_str_offsets_base attribute (and hence needs the symbol).
     725       26982 :   if (useSegmentedStringOffsetsTable())
     726          37 :     (useSplitDwarf() ? SkeletonHolder : InfoHolder)
     727          37 :         .setStringOffsetsStartSym(Asm->createTempSymbol("str_offsets_base"));
     728             : 
     729             :   // Create the symbol that designates the start of the DWARF v5 range list
     730             :   // table. It is located past the header and before the offsets table.
     731       26982 :   if (getDwarfVersion() >= 5)
     732          37 :     (useSplitDwarf() ? SkeletonHolder : InfoHolder)
     733          37 :         .setRnglistsTableBaseSym(Asm->createTempSymbol("rnglists_table_base"));
     734             : 
     735             :   // Create the symbol that points to the first entry following the debug
     736             :   // address table (.debug_addr) header.
     737       53964 :   AddrPool.setLabel(Asm->createTempSymbol("addr_table_base"));
     738             : 
     739       28378 :   for (DICompileUnit *CUNode : M->debug_compile_units()) {
     740             :     // FIXME: Move local imported entities into a list attached to the
     741             :     // subprogram, then this search won't be needed and a
     742             :     // getImportedEntities().empty() test should go below with the rest.
     743        1396 :     bool HasNonLocalImportedEntities = llvm::any_of(
     744        1396 :         CUNode->getImportedEntities(), [](const DIImportedEntity *IE) {
     745           0 :           return !isa<DILocalScope>(IE->getScope());
     746             :         });
     747             : 
     748             :     if (!HasNonLocalImportedEntities && CUNode->getEnumTypes().empty() &&
     749             :         CUNode->getRetainedTypes().empty() &&
     750        1396 :         CUNode->getGlobalVariables().empty() && CUNode->getMacros().empty())
     751         663 :       continue;
     752             : 
     753         733 :     DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(CUNode);
     754             : 
     755             :     // Global Variables.
     756        1829 :     for (auto *GVE : CUNode->getGlobalVariables()) {
     757             :       // Don't bother adding DIGlobalVariableExpressions listed in the CU if we
     758             :       // already know about the variable and it isn't adding a constant
     759             :       // expression.
     760        1096 :       auto &GVMapEntry = GVMap[GVE->getVariable()];
     761             :       auto *Expr = GVE->getExpression();
     762        1096 :       if (!GVMapEntry.size() || (Expr && Expr->isConstant()))
     763         173 :         GVMapEntry.push_back({nullptr, Expr});
     764             :     }
     765             :     DenseSet<DIGlobalVariable *> Processed;
     766        1829 :     for (auto *GVE : CUNode->getGlobalVariables()) {
     767        1096 :       DIGlobalVariable *GV = GVE->getVariable();
     768        1096 :       if (Processed.insert(GV).second)
     769        1090 :         CU.getOrCreateGlobalVariableDIE(GV, sortGlobalExprs(GVMap[GV]));
     770             :     }
     771             : 
     772        1012 :     for (auto *Ty : CUNode->getEnumTypes()) {
     773             :       // The enum types array by design contains pointers to
     774             :       // MDNodes rather than DIRefs. Unique them here.
     775         279 :       CU.getOrCreateTypeDIE(cast<DIType>(Ty));
     776             :     }
     777        2265 :     for (auto *Ty : CUNode->getRetainedTypes()) {
     778             :       // The retained types array by design contains pointers to
     779             :       // MDNodes rather than DIRefs. Unique them here.
     780             :       if (DIType *RT = dyn_cast<DIType>(Ty))
     781             :           // There is no point in force-emitting a forward declaration.
     782        1501 :           CU.getOrCreateTypeDIE(RT);
     783             :     }
     784             :     // Emit imported_modules last so that the relevant context is already
     785             :     // available.
     786       17370 :     for (auto *IE : CUNode->getImportedEntities())
     787       16637 :       constructAndAddImportedEntityDIE(CU, IE);
     788             :   }
     789             : }
     790             : 
     791        1068 : void DwarfDebug::finishEntityDefinitions() {
     792       64033 :   for (const auto &Entity : ConcreteEntities) {
     793       62965 :     DIE *Die = Entity->getDIE();
     794             :     assert(Die);
     795             :     // FIXME: Consider the time-space tradeoff of just storing the unit pointer
     796             :     // in the ConcreteEntities list, rather than looking it up again here.
     797             :     // DIE::getUnit isn't simple - it walks parent pointers, etc.
     798      125930 :     DwarfCompileUnit *Unit = CUDieMap.lookup(Die->getUnitDie());
     799             :     assert(Unit);
     800       62965 :     Unit->finishEntityDefinition(Entity.get());
     801             :   }
     802        1068 : }
     803             : 
     804        1068 : void DwarfDebug::finishSubprogramDefinitions() {
     805        9480 :   for (const DISubprogram *SP : ProcessedSPNodes) {
     806             :     assert(SP->getUnit()->getEmissionKind() != DICompileUnit::NoDebug);
     807        8412 :     forBothCUs(
     808             :         getOrCreateDwarfCompileUnit(SP->getUnit()),
     809        8454 :         [&](DwarfCompileUnit &CU) { CU.finishSubprogramDefinition(SP); });
     810             :   }
     811        1068 : }
     812             : 
     813        1068 : void DwarfDebug::finalizeModuleInfo() {
     814        1068 :   const TargetLoweringObjectFile &TLOF = Asm->getObjFileLowering();
     815             : 
     816        1067 :   finishSubprogramDefinitions();
     817             : 
     818        1068 :   finishEntityDefinitions();
     819             : 
     820             :   // Include the DWO file name in the hash if there's more than one CU.
     821             :   // This handles ThinLTO's situation where imported CUs may very easily be
     822             :   // duplicate with the same CU partially imported into another ThinLTO unit.
     823        1068 :   StringRef DWOName;
     824        1068 :   if (CUMap.size() > 1)
     825         106 :     DWOName = Asm->TM.Options.MCOptions.SplitDwarfFile;
     826             : 
     827             :   // Handle anything that needs to be done on a per-unit basis after
     828             :   // all other generation.
     829        2404 :   for (const auto &P : CUMap) {
     830        1336 :     auto &TheCU = *P.second;
     831        1336 :     if (TheCU.getCUNode()->isDebugDirectivesOnly())
     832             :       continue;
     833             :     // Emit DW_AT_containing_type attribute to connect types with their
     834             :     // vtable holding type.
     835        1334 :     TheCU.constructContainingTypeDIEs();
     836             : 
     837             :     // Add CU specific attributes if we need to add any.
     838             :     // If we're splitting the dwarf out now that we've got the entire
     839             :     // CU then add the dwo id to it.
     840        1334 :     auto *SkCU = TheCU.getSkeleton();
     841        1334 :     if (useSplitDwarf()) {
     842             :       // Emit a unique identifier for this CU.
     843             :       uint64_t ID =
     844          86 :           DIEHash(Asm).computeCUSignature(DWOName, TheCU.getUnitDie());
     845          43 :       if (getDwarfVersion() >= 5) {
     846             :         TheCU.setDWOId(ID);
     847             :         SkCU->setDWOId(ID);
     848             :       } else {
     849          35 :         TheCU.addUInt(TheCU.getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
     850             :                       dwarf::DW_FORM_data8, ID);
     851          70 :         SkCU->addUInt(SkCU->getUnitDie(), dwarf::DW_AT_GNU_dwo_id,
     852             :                       dwarf::DW_FORM_data8, ID);
     853             :       }
     854             :       // We don't keep track of which addresses are used in which CU so this
     855             :       // is a bit pessimistic under LTO.
     856          43 :       if (!AddrPool.isEmpty())
     857          40 :         SkCU->addAddrTableBase();
     858             : 
     859          78 :       if (getDwarfVersion() < 5 && !SkCU->getRangeLists().empty()) {
     860           2 :         const MCSymbol *Sym = TLOF.getDwarfRangesSection()->getBeginSymbol();
     861             :         SkCU->addSectionLabel(SkCU->getUnitDie(), dwarf::DW_AT_GNU_ranges_base,
     862           4 :                               Sym, Sym);
     863             :       }
     864             :     }
     865             : 
     866             :     // If we have code split among multiple sections or non-contiguous
     867             :     // ranges of code then emit a DW_AT_ranges attribute on the unit that will
     868             :     // remain in the .o file, otherwise add a DW_AT_low_pc.
     869             :     // FIXME: We should use ranges allow reordering of code ala
     870             :     // .subsections_via_symbols in mach-o. This would mean turning on
     871             :     // ranges for all subprogram DIEs for mach-o.
     872        1334 :     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
     873        1334 :     if (unsigned NumRanges = TheCU.getRanges().size()) {
     874         880 :       if (NumRanges > 1 && useRangesSection())
     875             :         // A DW_AT_low_pc attribute may also be specified in combination with
     876             :         // DW_AT_ranges to specify the default base address for use in
     877             :         // location lists (see Section 2.6.2) and range lists (see Section
     878             :         // 2.17.3).
     879         258 :         U.addUInt(U.getUnitDie(), dwarf::DW_AT_low_pc, dwarf::DW_FORM_addr, 0);
     880             :       else
     881         751 :         U.setBaseAddress(TheCU.getRanges().front().getStart());
     882        1760 :       U.attachRangesOrLowHighPC(U.getUnitDie(), TheCU.takeRanges());
     883             :     }
     884             : 
     885        1334 :     if (getDwarfVersion() >= 5 && !useSplitDwarf() &&
     886          37 :         !U.getRangeLists().empty())
     887           5 :       U.addRnglistsBase();
     888             : 
     889        1334 :     auto *CUNode = cast<DICompileUnit>(P.first);
     890             :     // If compile Unit has macros, emit "DW_AT_macro_info" attribute.
     891        1334 :     if (CUNode->getMacros())
     892             :       U.addSectionLabel(U.getUnitDie(), dwarf::DW_AT_macro_info,
     893           2 :                         U.getMacroLabelBegin(),
     894           4 :                         TLOF.getDwarfMacinfoSection()->getBeginSymbol());
     895             :   }
     896             : 
     897             :   // Emit all frontend-produced Skeleton CUs, i.e., Clang modules.
     898        2459 :   for (auto *CUNode : MMI->getModule()->debug_compile_units())
     899        1391 :     if (CUNode->getDWOId())
     900          28 :       getOrCreateDwarfCompileUnit(CUNode);
     901             : 
     902             :   // Compute DIE offsets and sizes.
     903        1068 :   InfoHolder.computeSizeAndOffsets();
     904        1068 :   if (useSplitDwarf())
     905          37 :     SkeletonHolder.computeSizeAndOffsets();
     906        1068 : }
     907             : 
     908             : // Emit all Dwarf sections that should come after the content.
     909       26844 : void DwarfDebug::endModule() {
     910             :   assert(CurFn == nullptr);
     911             :   assert(CurMI == nullptr);
     912             : 
     913             :   // If we aren't actually generating debug info (check beginModule -
     914             :   // conditionalized on !DisableDebugInfoPrinting and the presence of the
     915             :   // llvm.dbg.cu metadata node)
     916       26844 :   if (!MMI->hasDebugInfo())
     917             :     return;
     918             : 
     919             :   // Finalize the debug info for the module.
     920        1068 :   finalizeModuleInfo();
     921             : 
     922        1068 :   emitDebugStr();
     923             : 
     924        1068 :   if (useSplitDwarf())
     925          37 :     emitDebugLocDWO();
     926             :   else
     927             :     // Emit info into a debug loc section.
     928        1031 :     emitDebugLoc();
     929             : 
     930             :   // Corresponding abbreviations into a abbrev section.
     931        1067 :   emitAbbreviations();
     932             : 
     933             :   // Emit all the DIEs into a debug info section.
     934        1068 :   emitDebugInfo();
     935             : 
     936             :   // Emit info into a debug aranges section.
     937        1068 :   if (GenerateARangeSection)
     938           6 :     emitDebugARanges();
     939             : 
     940             :   // Emit info into a debug ranges section.
     941        1068 :   emitDebugRanges();
     942             : 
     943             :   // Emit info into a debug macinfo section.
     944        1068 :   emitDebugMacinfo();
     945             : 
     946        1068 :   if (useSplitDwarf()) {
     947          37 :     emitDebugStrDWO();
     948          37 :     emitDebugInfoDWO();
     949          37 :     emitDebugAbbrevDWO();
     950          37 :     emitDebugLineDWO();
     951          37 :     emitDebugAddr();
     952             :   }
     953             : 
     954             :   // Emit info into the dwarf accelerator table sections.
     955        1068 :   switch (getAccelTableKind()) {
     956         206 :   case AccelTableKind::Apple:
     957         206 :     emitAccelNames();
     958         206 :     emitAccelObjC();
     959         206 :     emitAccelNamespaces();
     960         206 :     emitAccelTypes();
     961         206 :     break;
     962          50 :   case AccelTableKind::Dwarf:
     963          50 :     emitAccelDebugNames();
     964          50 :     break;
     965             :   case AccelTableKind::None:
     966             :     break;
     967             :   case AccelTableKind::Default:
     968             :     llvm_unreachable("Default should have already been resolved.");
     969             :   }
     970             : 
     971             :   // Emit the pubnames and pubtypes sections if requested.
     972        1068 :   emitDebugPubSections();
     973             : 
     974             :   // clean up.
     975             :   // FIXME: AbstractVariables.clear();
     976             : }
     977             : 
     978       45083 : void DwarfDebug::ensureAbstractEntityIsCreated(DwarfCompileUnit &CU,
     979             :                                                const DINode *Node,
     980             :                                                const MDNode *ScopeNode) {
     981       45083 :   if (CU.getExistingAbstractEntity(Node))
     982             :     return;
     983             : 
     984        4100 :   CU.createAbstractEntity(Node, LScopes.getOrCreateAbstractScope(
     985             :                                        cast<DILocalScope>(ScopeNode)));
     986             : }
     987             : 
     988       62970 : void DwarfDebug::ensureAbstractEntityIsCreatedIfScoped(DwarfCompileUnit &CU,
     989             :     const DINode *Node, const MDNode *ScopeNode) {
     990       62970 :   if (CU.getExistingAbstractEntity(Node))
     991             :     return;
     992             : 
     993       18777 :   if (LexicalScope *Scope =
     994       18777 :           LScopes.findAbstractScope(cast_or_null<DILocalScope>(ScopeNode)))
     995       10180 :     CU.createAbstractEntity(Node, Scope);
     996             : }
     997             : 
     998             : // Collect variable information from side table maintained by MF.
     999        9433 : void DwarfDebug::collectVariableInfoFromMFTable(
    1000             :     DwarfCompileUnit &TheCU, DenseSet<InlinedEntity> &Processed) {
    1001             :   SmallDenseMap<InlinedEntity, DbgVariable *> MFVars;
    1002       10080 :   for (const auto &VI : Asm->MF->getVariableDbgInfo()) {
    1003         647 :     if (!VI.Var)
    1004           3 :       continue;
    1005             :     assert(VI.Var->isValidLocationForIntrinsic(VI.Loc) &&
    1006             :            "Expected inlined-at fields to agree");
    1007             : 
    1008         647 :     InlinedEntity Var(VI.Var, VI.Loc->getInlinedAt());
    1009             :     Processed.insert(Var);
    1010         647 :     LexicalScope *Scope = LScopes.findLexicalScope(VI.Loc);
    1011             : 
    1012             :     // If variable scope is not found then skip this variable.
    1013         647 :     if (!Scope)
    1014             :       continue;
    1015             : 
    1016         644 :     ensureAbstractEntityIsCreatedIfScoped(TheCU, Var.first, Scope->getScopeNode());
    1017             :     auto RegVar = llvm::make_unique<DbgVariable>(
    1018         644 :                     cast<DILocalVariable>(Var.first), Var.second);
    1019         644 :     RegVar->initializeMMI(VI.Expr, VI.Slot);
    1020           5 :     if (DbgVariable *DbgVar = MFVars.lookup(Var))
    1021           5 :       DbgVar->addMMIEntry(*RegVar);
    1022        1278 :     else if (InfoHolder.addScopeVariable(Scope, RegVar.get())) {
    1023         639 :       MFVars.insert({Var, RegVar.get()});
    1024        1917 :       ConcreteEntities.push_back(std::move(RegVar));
    1025             :     }
    1026             :   }
    1027        9433 : }
    1028             : 
    1029             : // Get .debug_loc entry for the instruction range starting at MI.
    1030      201517 : static DebugLocEntry::Value getDebugLocValue(const MachineInstr *MI) {
    1031      201517 :   const DIExpression *Expr = MI->getDebugExpression();
    1032             :   assert(MI->getNumOperands() == 4);
    1033      403034 :   if (MI->getOperand(0).isReg()) {
    1034      200818 :     auto RegOp = MI->getOperand(0);
    1035      200818 :     auto Op1 = MI->getOperand(1);
    1036             :     // If the second operand is an immediate, this is a
    1037             :     // register-indirect address.
    1038             :     assert((!Op1.isImm() || (Op1.getImm() == 0)) && "unexpected offset");
    1039             :     MachineLocation MLoc(RegOp.getReg(), Op1.isImm());
    1040             :     return DebugLocEntry::Value(Expr, MLoc);
    1041             :   }
    1042         699 :   if (MI->getOperand(0).isImm())
    1043         690 :     return DebugLocEntry::Value(Expr, MI->getOperand(0).getImm());
    1044           9 :   if (MI->getOperand(0).isFPImm())
    1045           9 :     return DebugLocEntry::Value(Expr, MI->getOperand(0).getFPImm());
    1046           0 :   if (MI->getOperand(0).isCImm())
    1047           0 :     return DebugLocEntry::Value(Expr, MI->getOperand(0).getCImm());
    1048             : 
    1049           0 :   llvm_unreachable("Unexpected 4-operand DBG_VALUE instruction!");
    1050             : }
    1051             : 
    1052             : /// If this and Next are describing different fragments of the same
    1053             : /// variable, merge them by appending Next's values to the current
    1054             : /// list of values.
    1055             : /// Return true if the merge was successful.
    1056         502 : bool DebugLocEntry::MergeValues(const DebugLocEntry &Next) {
    1057         502 :   if (Begin == Next.Begin) {
    1058         132 :     auto *FirstExpr = cast<DIExpression>(Values[0].Expression);
    1059         132 :     auto *FirstNextExpr = cast<DIExpression>(Next.Values[0].Expression);
    1060         264 :     if (!FirstExpr->isFragment() || !FirstNextExpr->isFragment())
    1061           0 :       return false;
    1062             : 
    1063             :     // We can only merge entries if none of the fragments overlap any others.
    1064             :     // In doing so, we can take advantage of the fact that both lists are
    1065             :     // sorted.
    1066         267 :     for (unsigned i = 0, j = 0; i < Values.size(); ++i) {
    1067         221 :       for (; j < Next.Values.size(); ++j) {
    1068         133 :         int res = cast<DIExpression>(Values[i].Expression)->fragmentCmp(
    1069         133 :             cast<DIExpression>(Next.Values[j].Expression));
    1070         133 :         if (res == 0) // The two expressions overlap, we can't merge.
    1071             :           return false;
    1072             :         // Values[i] is entirely before Next.Values[j],
    1073             :         // so go back to the next entry of Values.
    1074         120 :         else if (res == -1)
    1075             :           break;
    1076             :         // Next.Values[j] is entirely before Values[i], so go on to the
    1077             :         // next entry of Next.Values.
    1078             :       }
    1079             :     }
    1080             : 
    1081         119 :     addValues(Next.Values);
    1082         119 :     End = Next.End;
    1083         119 :     return true;
    1084             :   }
    1085             :   return false;
    1086             : }
    1087             : 
    1088             : /// Build the location list for all DBG_VALUEs in the function that
    1089             : /// describe the same variable.  If the ranges of several independent
    1090             : /// fragments of the same variable overlap partially, split them up and
    1091             : /// combine the ranges. The resulting DebugLocEntries are will have
    1092             : /// strict monotonically increasing begin addresses and will never
    1093             : /// overlap.
    1094             : //
    1095             : // Input:
    1096             : //
    1097             : //   Ranges History [var, loc, fragment ofs size]
    1098             : // 0 |      [x, (reg0, fragment 0, 32)]
    1099             : // 1 | |    [x, (reg1, fragment 32, 32)] <- IsFragmentOfPrevEntry
    1100             : // 2 | |    ...
    1101             : // 3   |    [clobber reg0]
    1102             : // 4        [x, (mem, fragment 0, 64)] <- overlapping with both previous fragments of
    1103             : //                                     x.
    1104             : //
    1105             : // Output:
    1106             : //
    1107             : // [0-1]    [x, (reg0, fragment  0, 32)]
    1108             : // [1-3]    [x, (reg0, fragment  0, 32), (reg1, fragment 32, 32)]
    1109             : // [3-4]    [x, (reg1, fragment 32, 32)]
    1110             : // [4- ]    [x, (mem,  fragment  0, 64)]
    1111             : void
    1112       48813 : DwarfDebug::buildLocationList(SmallVectorImpl<DebugLocEntry> &DebugLoc,
    1113             :                               const DbgValueHistoryMap::InstrRanges &Ranges) {
    1114             :   SmallVector<DebugLocEntry::Value, 4> OpenRanges;
    1115             : 
    1116      252666 :   for (auto I = Ranges.begin(), E = Ranges.end(); I != E; ++I) {
    1117      203853 :     const MachineInstr *Begin = I->first;
    1118      203853 :     const MachineInstr *End = I->second;
    1119             :     assert(Begin->isDebugValue() && "Invalid History entry");
    1120             : 
    1121             :     // Check if a variable is inaccessible in this range.
    1122      203853 :     if (Begin->getNumOperands() > 1 &&
    1123      203853 :         Begin->getOperand(0).isReg() && !Begin->getOperand(0).getReg()) {
    1124             :       OpenRanges.clear();
    1125        2336 :       continue;
    1126             :     }
    1127             : 
    1128             :     // If this fragment overlaps with any open ranges, truncate them.
    1129      201517 :     const DIExpression *DIExpr = Begin->getDebugExpression();
    1130             :     auto Last = remove_if(OpenRanges, [&](DebugLocEntry::Value R) {
    1131          29 :       return DIExpr->fragmentsOverlap(R.getExpression());
    1132             :     });
    1133             :     OpenRanges.erase(Last, OpenRanges.end());
    1134             : 
    1135      201517 :     const MCSymbol *StartLabel = getLabelBeforeInsn(Begin);
    1136             :     assert(StartLabel && "Forgot label before DBG_VALUE starting a range!");
    1137             : 
    1138             :     const MCSymbol *EndLabel;
    1139      201517 :     if (End != nullptr)
    1140      178766 :       EndLabel = getLabelAfterInsn(End);
    1141       22751 :     else if (std::next(I) == Ranges.end())
    1142        3766 :       EndLabel = Asm->getFunctionEnd();
    1143             :     else
    1144       18985 :       EndLabel = getLabelBeforeInsn(std::next(I)->first);
    1145             :     assert(EndLabel && "Forgot label after instruction ending a range!");
    1146             : 
    1147             :     LLVM_DEBUG(dbgs() << "DotDebugLoc: " << *Begin << "\n");
    1148             : 
    1149      201517 :     auto Value = getDebugLocValue(Begin);
    1150      201517 :     DebugLocEntry Loc(StartLabel, EndLabel, Value);
    1151             :     bool couldMerge = false;
    1152             : 
    1153             :     // If this is a fragment, it may belong to the current DebugLocEntry.
    1154      201517 :     if (DIExpr->isFragment()) {
    1155             :       // Add this value to the list of open ranges.
    1156         774 :       OpenRanges.push_back(Value);
    1157             : 
    1158             :       // Attempt to add the fragment to the last entry.
    1159         774 :       if (!DebugLoc.empty())
    1160         502 :         if (DebugLoc.back().MergeValues(Loc))
    1161             :           couldMerge = true;
    1162             :     }
    1163             : 
    1164             :     if (!couldMerge) {
    1165             :       // Need to add a new DebugLocEntry. Add all values from still
    1166             :       // valid non-overlapping fragments.
    1167      402796 :       if (OpenRanges.size())
    1168         655 :         Loc.addValues(OpenRanges);
    1169             : 
    1170      201398 :       DebugLoc.push_back(std::move(Loc));
    1171             :     }
    1172             : 
    1173             :     // Attempt to coalesce the ranges of two otherwise identical
    1174             :     // DebugLocEntries.
    1175             :     auto CurEntry = DebugLoc.rbegin();
    1176             :     LLVM_DEBUG({
    1177             :       dbgs() << CurEntry->getValues().size() << " Values:\n";
    1178             :       for (auto &Value : CurEntry->getValues())
    1179             :         Value.dump();
    1180             :       dbgs() << "-----\n";
    1181             :     });
    1182             : 
    1183             :     auto PrevEntry = std::next(CurEntry);
    1184      201517 :     if (PrevEntry != DebugLoc.rend() && PrevEntry->MergeRanges(*CurEntry))
    1185      115982 :       DebugLoc.pop_back();
    1186             :   }
    1187       48813 : }
    1188             : 
    1189       62326 : DbgEntity *DwarfDebug::createConcreteEntity(DwarfCompileUnit &TheCU,
    1190             :                                             LexicalScope &Scope,
    1191             :                                             const DINode *Node,
    1192             :                                             const DILocation *Location,
    1193             :                                             const MCSymbol *Sym) {
    1194       62326 :   ensureAbstractEntityIsCreatedIfScoped(TheCU, Node, Scope.getScopeNode());
    1195       62326 :   if (isa<const DILocalVariable>(Node)) {
    1196      124638 :     ConcreteEntities.push_back(
    1197       62319 :         llvm::make_unique<DbgVariable>(cast<const DILocalVariable>(Node),
    1198             :                                        Location));
    1199      124638 :     InfoHolder.addScopeVariable(&Scope,
    1200             :         cast<DbgVariable>(ConcreteEntities.back().get()));
    1201           7 :   } else if (isa<const DILabel>(Node)) {
    1202           7 :     ConcreteEntities.push_back(
    1203             :         llvm::make_unique<DbgLabel>(cast<const DILabel>(Node),
    1204             :                                     Location, Sym));
    1205          14 :     InfoHolder.addScopeLabel(&Scope,
    1206             :         cast<DbgLabel>(ConcreteEntities.back().get()));
    1207             :   }
    1208       62326 :   return ConcreteEntities.back().get();
    1209             : }
    1210             : 
    1211             : /// Determine whether a *singular* DBG_VALUE is valid for the entirety of its
    1212             : /// enclosing lexical scope. The check ensures there are no other instructions
    1213             : /// in the same lexical scope preceding the DBG_VALUE and that its range is
    1214             : /// either open or otherwise rolls off the end of the scope.
    1215       34039 : static bool validThroughout(LexicalScopes &LScopes,
    1216             :                             const MachineInstr *DbgValue,
    1217             :                             const MachineInstr *RangeEnd) {
    1218             :   assert(DbgValue->getDebugLoc() && "DBG_VALUE without a debug location");
    1219       34039 :   auto MBB = DbgValue->getParent();
    1220             :   auto DL = DbgValue->getDebugLoc();
    1221       34039 :   auto *LScope = LScopes.findLexicalScope(DL);
    1222             :   // Scope doesn't exist; this is a dead DBG_VALUE.
    1223       34039 :   if (!LScope)
    1224             :     return false;
    1225             :   auto &LSRange = LScope->getRanges();
    1226       34037 :   if (LSRange.size() == 0)
    1227             :     return false;
    1228             : 
    1229             :   // Determine if the DBG_VALUE is valid at the beginning of its lexical block.
    1230       34037 :   const MachineInstr *LScopeBegin = LSRange.front().first;
    1231             :   // Early exit if the lexical scope begins outside of the current block.
    1232       34037 :   if (LScopeBegin->getParent() != MBB)
    1233             :     return false;
    1234             :   MachineBasicBlock::const_reverse_iterator Pred(DbgValue);
    1235      273598 :   for (++Pred; Pred != MBB->rend(); ++Pred) {
    1236      254081 :     if (Pred->getFlag(MachineInstr::FrameSetup))
    1237             :       break;
    1238             :     auto PredDL = Pred->getDebugLoc();
    1239      251501 :     if (!PredDL || Pred->isMetaInstruction())
    1240             :       continue;
    1241             :     // Check whether the instruction preceding the DBG_VALUE is in the same
    1242             :     // (sub)scope as the DBG_VALUE.
    1243       41782 :     if (DL->getScope() == PredDL->getScope())
    1244             :       return false;
    1245       41173 :     auto *PredScope = LScopes.findLexicalScope(PredDL);
    1246       41173 :     if (!PredScope || LScope->dominates(PredScope))
    1247             :       return false;
    1248             :   }
    1249             : 
    1250             :   // If the range of the DBG_VALUE is open-ended, report success.
    1251       22097 :   if (!RangeEnd)
    1252             :     return true;
    1253             : 
    1254             :   // Fail if there are instructions belonging to our scope in another block.
    1255        8925 :   const MachineInstr *LScopeEnd = LSRange.back().second;
    1256        8925 :   if (LScopeEnd->getParent() != MBB)
    1257             :     return false;
    1258             : 
    1259             :   // Single, constant DBG_VALUEs in the prologue are promoted to be live
    1260             :   // throughout the function. This is a hack, presumably for DWARF v2 and not
    1261             :   // necessarily correct. It would be much better to use a dbg.declare instead
    1262             :   // if we know the constant is live throughout the scope.
    1263       16322 :   if (DbgValue->getOperand(0).isImm() && MBB->pred_empty())
    1264           0 :     return true;
    1265             : 
    1266             :   return false;
    1267             : }
    1268             : 
    1269             : // Find variables for each lexical scope.
    1270        9432 : void DwarfDebug::collectEntityInfo(DwarfCompileUnit &TheCU,
    1271             :                                    const DISubprogram *SP,
    1272             :                                    DenseSet<InlinedEntity> &Processed) {
    1273             :   // Grab the variable info that was squirreled away in the MMI side-table.
    1274        9432 :   collectVariableInfoFromMFTable(TheCU, Processed);
    1275             : 
    1276       86483 :   for (const auto &I : DbgValues) {
    1277       77050 :     InlinedEntity IV = I.first;
    1278             :     if (Processed.count(IV))
    1279       28237 :       continue;
    1280             : 
    1281             :     // Instruction ranges, specifying where IV is accessible.
    1282       77040 :     const auto &Ranges = I.second;
    1283       77040 :     if (Ranges.empty())
    1284             :       continue;
    1285             : 
    1286             :     LexicalScope *Scope = nullptr;
    1287       77040 :     const DILocalVariable *LocalVar = cast<DILocalVariable>(IV.first);
    1288       77040 :     if (const DILocation *IA = IV.second)
    1289             :       Scope = LScopes.findInlinedScope(LocalVar->getScope(), IA);
    1290             :     else
    1291       10677 :       Scope = LScopes.findLexicalScope(LocalVar->getScope());
    1292             :     // If variable scope is not found then skip this variable.
    1293             :     if (!Scope)
    1294       15049 :       continue;
    1295             : 
    1296             :     Processed.insert(IV);
    1297       61991 :     DbgVariable *RegVar = cast<DbgVariable>(createConcreteEntity(TheCU,
    1298             :                                             *Scope, LocalVar, IV.second));
    1299             : 
    1300       61991 :     const MachineInstr *MInsn = Ranges.front().first;
    1301             :     assert(MInsn->isDebugValue() && "History must begin with debug value");
    1302             : 
    1303             :     // Check if there is a single DBG_VALUE, valid throughout the var's scope.
    1304       96030 :     if (Ranges.size() == 1 &&
    1305       34039 :         validThroughout(LScopes, MInsn, Ranges.front().second)) {
    1306       13172 :       RegVar->initializeDbgValue(MInsn);
    1307       13172 :       continue;
    1308             :     }
    1309             :     // Do not emit location lists if .debug_loc secton is disabled.
    1310       48819 :     if (!useLocSection())
    1311             :       continue;
    1312             : 
    1313             :     // Handle multiple DBG_VALUE instructions describing one variable.
    1314       97626 :     DebugLocStream::ListBuilder List(DebugLocs, TheCU, *Asm, *RegVar, *MInsn);
    1315             : 
    1316             :     // Build the location list for this variable.
    1317       48813 :     SmallVector<DebugLocEntry, 8> Entries;
    1318       48813 :     buildLocationList(Entries, Ranges);
    1319             : 
    1320             :     // If the variable has a DIBasicType, extract it.  Basic types cannot have
    1321             :     // unique identifiers, so don't bother resolving the type with the
    1322             :     // identifier map.
    1323             :     const DIBasicType *BT = dyn_cast<DIBasicType>(
    1324             :         static_cast<const Metadata *>(LocalVar->getType()));
    1325             : 
    1326             :     // Finalize the entry by lowering it into a DWARF bytestream.
    1327      134229 :     for (auto &Entry : Entries)
    1328       85416 :       Entry.finalize(*Asm, List, BT);
    1329             :   }
    1330             : 
    1331             :   // For each InlinedEntity collected from DBG_LABEL instructions, convert to
    1332             :   // DWARF-related DbgLabel.
    1333        9439 :   for (const auto &I : DbgLabels) {
    1334           6 :     InlinedEntity IL = I.first;
    1335           6 :     const MachineInstr *MI = I.second;
    1336           6 :     if (MI == nullptr)
    1337           0 :       continue;
    1338             : 
    1339             :     LexicalScope *Scope = nullptr;
    1340           6 :     const DILabel *Label = cast<DILabel>(IL.first);
    1341             :     // Get inlined DILocation if it is inlined label.
    1342           6 :     if (const DILocation *IA = IL.second)
    1343             :       Scope = LScopes.findInlinedScope(Label->getScope(), IA);
    1344             :     else
    1345           5 :       Scope = LScopes.findLexicalScope(Label->getScope());
    1346             :     // If label scope is not found then skip this label.
    1347             :     if (!Scope)
    1348           0 :       continue;
    1349             : 
    1350             :     Processed.insert(IL);
    1351             :     /// At this point, the temporary label is created.
    1352             :     /// Save the temporary label to DbgLabel entity to get the
    1353             :     /// actually address when generating Dwarf DIE.
    1354           6 :     MCSymbol *Sym = getLabelBeforeInsn(MI);
    1355           6 :     createConcreteEntity(TheCU, *Scope, Label, IL.second, Sym);
    1356             :   }
    1357             : 
    1358             :   // Collect info for variables/labels that were optimized out.
    1359       20893 :   for (const DINode *DN : SP->getRetainedNodes()) {
    1360       11460 :     if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
    1361             :       continue;
    1362             :     LexicalScope *Scope = nullptr;
    1363             :     if (auto *DV = dyn_cast<DILocalVariable>(DN)) {
    1364        3255 :       Scope = LScopes.findLexicalScope(DV->getScope());
    1365             :     } else if (auto *DL = dyn_cast<DILabel>(DN)) {
    1366           1 :       Scope = LScopes.findLexicalScope(DL->getScope());
    1367             :     }
    1368             : 
    1369             :     if (Scope)
    1370         329 :       createConcreteEntity(TheCU, *Scope, DN, nullptr);
    1371             :   }
    1372        9433 : }
    1373             : 
    1374             : // Process beginning of an instruction.
    1375     2070406 : void DwarfDebug::beginInstruction(const MachineInstr *MI) {
    1376     2070406 :   DebugHandlerBase::beginInstruction(MI);
    1377             :   assert(CurMI);
    1378             : 
    1379     2070405 :   const auto *SP = MI->getMF()->getFunction().getSubprogram();
    1380     4062684 :   if (!SP || SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
    1381             :     return;
    1382             : 
    1383             :   // Check if source location changes, but ignore DBG_VALUE and CFI locations.
    1384             :   // If the instruction is part of the function frame setup code, do not emit
    1385             :   // any line record, as there is no correspondence with any user code.
    1386     1466891 :   if (MI->isMetaInstruction() || MI->getFlag(MachineInstr::FrameSetup))
    1387             :     return;
    1388             :   const DebugLoc &DL = MI->getDebugLoc();
    1389             :   // When we emit a line-0 record, we don't update PrevInstLoc; so look at
    1390             :   // the last line number actually emitted, to see if it was line 0.
    1391             :   unsigned LastAsmLine =
    1392     2880774 :       Asm->OutStreamer->getContext().getCurrentDwarfLoc().getLine();
    1393             : 
    1394             :   // Request a label after the call in order to emit AT_return_pc information
    1395             :   // in call site entries. TODO: Add support for targets with delay slots.
    1396     1440429 :   if (SP->areAllCallsDescribed() && MI->isCall() && !MI->hasDelaySlot())
    1397             :     requestLabelAfterInsn(MI);
    1398             : 
    1399     1440387 :   if (DL == PrevInstLoc) {
    1400             :     // If we have an ongoing unspecified location, nothing to do here.
    1401      608245 :     if (!DL)
    1402             :       return;
    1403             :     // We have an explicit location, same as the previous location.
    1404             :     // But we might be coming back to it after a line 0 record.
    1405      594083 :     if (LastAsmLine == 0 && DL.getLine() != 0) {
    1406             :       // Reinstate the source location but not marked as a statement.
    1407        9363 :       const MDNode *Scope = DL.getScope();
    1408        9363 :       recordSourceLine(DL.getLine(), DL.getCol(), Scope, /*Flags=*/0);
    1409             :     }
    1410      594083 :     return;
    1411             :   }
    1412             : 
    1413      832142 :   if (!DL) {
    1414             :     // We have an unspecified location, which might want to be line 0.
    1415             :     // If we have already emitted a line-0 record, don't repeat it.
    1416      253989 :     if (LastAsmLine == 0)
    1417             :       return;
    1418             :     // If user said Don't Do That, don't do that.
    1419      196237 :     if (UnknownLocations == Disable)
    1420             :       return;
    1421             :     // See if we have a reason to emit a line-0 record now.
    1422             :     // Reasons to emit a line-0 record include:
    1423             :     // - User asked for it (UnknownLocations).
    1424             :     // - Instruction has a label, so it's referenced from somewhere else,
    1425             :     //   possibly debug information; we want it to have a source location.
    1426             :     // - Instruction is at the top of a block; we don't want to inherit the
    1427             :     //   location from the physically previous (maybe unrelated) block.
    1428      196235 :     if (UnknownLocations == Enable || PrevLabel ||
    1429      177696 :         (PrevInstBB && PrevInstBB != MI->getParent())) {
    1430             :       // Preserve the file and column numbers, if we can, to save space in
    1431             :       // the encoded line table.
    1432             :       // Do not update PrevInstLoc, it remembers the last non-0 line.
    1433             :       const MDNode *Scope = nullptr;
    1434             :       unsigned Column = 0;
    1435       39460 :       if (PrevInstLoc) {
    1436       39460 :         Scope = PrevInstLoc.getScope();
    1437       39460 :         Column = PrevInstLoc.getCol();
    1438             :       }
    1439       39460 :       recordSourceLine(/*Line=*/0, Column, Scope, /*Flags=*/0);
    1440             :     }
    1441      196235 :     return;
    1442             :   }
    1443             : 
    1444             :   // We have an explicit location, different from the previous location.
    1445             :   // Don't repeat a line-0 record, but otherwise emit the new location.
    1446             :   // (The new location might be an explicit line 0, which we do emit.)
    1447      578153 :   if (PrevInstLoc && DL.getLine() == 0 && LastAsmLine == 0)
    1448             :     return;
    1449             :   unsigned Flags = 0;
    1450      563863 :   if (DL == PrologEndLoc) {
    1451             :     Flags |= DWARF2_FLAG_PROLOGUE_END | DWARF2_FLAG_IS_STMT;
    1452       18756 :     PrologEndLoc = DebugLoc();
    1453             :   }
    1454             :   // If the line changed, we call that a new statement; unless we went to
    1455             :   // line 0 and came back, in which case it is not a new statement.
    1456      563863 :   unsigned OldLine = PrevInstLoc ? PrevInstLoc.getLine() : LastAsmLine;
    1457      563863 :   if (DL.getLine() && DL.getLine() != OldLine)
    1458      462035 :     Flags |= DWARF2_FLAG_IS_STMT;
    1459             : 
    1460      563863 :   const MDNode *Scope = DL.getScope();
    1461      563863 :   recordSourceLine(DL.getLine(), DL.getCol(), Scope, Flags);
    1462             : 
    1463             :   // If we're not at line 0, remember this location.
    1464      563863 :   if (DL.getLine())
    1465             :     PrevInstLoc = DL;
    1466             : }
    1467             : 
    1468        9438 : static DebugLoc findPrologueEndLoc(const MachineFunction *MF) {
    1469             :   // First known non-DBG_VALUE and non-frame setup location marks
    1470             :   // the beginning of the function body.
    1471        9643 :   for (const auto &MBB : *MF)
    1472      141279 :     for (const auto &MI : MBB)
    1473       49950 :       if (!MI.isMetaInstruction() && !MI.getFlag(MachineInstr::FrameSetup) &&
    1474             :           MI.getDebugLoc())
    1475             :         return MI.getDebugLoc();
    1476          60 :   return DebugLoc();
    1477             : }
    1478             : 
    1479             : // Gather pre-function debug information.  Assumes being called immediately
    1480             : // after the function entry point has been emitted.
    1481        9438 : void DwarfDebug::beginFunctionImpl(const MachineFunction *MF) {
    1482        9438 :   CurFn = MF;
    1483             : 
    1484        9438 :   auto *SP = MF->getFunction().getSubprogram();
    1485             :   assert(LScopes.empty() || SP == LScopes.getCurrentFunctionScope()->getScopeNode());
    1486        9438 :   if (SP->getUnit()->getEmissionKind() == DICompileUnit::NoDebug)
    1487             :     return;
    1488             : 
    1489        9438 :   DwarfCompileUnit &CU = getOrCreateDwarfCompileUnit(SP->getUnit());
    1490             : 
    1491             :   // Set DwarfDwarfCompileUnitID in MCContext to the Compile Unit this function
    1492             :   // belongs to so that we add to the correct per-cu line table in the
    1493             :   // non-asm case.
    1494       18876 :   if (Asm->OutStreamer->hasRawTextSupport())
    1495             :     // Use a single line table if we are generating assembly.
    1496         766 :     Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
    1497             :   else
    1498       18110 :     Asm->OutStreamer->getContext().setDwarfCompileUnitID(CU.getUniqueID());
    1499             : 
    1500             :   // Record beginning of function.
    1501       18876 :   PrologEndLoc = findPrologueEndLoc(MF);
    1502        9438 :   if (PrologEndLoc) {
    1503             :     // We'd like to list the prologue as "not statements" but GDB behaves
    1504             :     // poorly if we do that. Revisit this with caution/GDB (7.5+) testing.
    1505       18756 :     auto *SP = PrologEndLoc->getInlinedAtScope()->getSubprogram();
    1506        9378 :     recordSourceLine(SP->getScopeLine(), 0, SP, DWARF2_FLAG_IS_STMT);
    1507             :   }
    1508             : }
    1509             : 
    1510      395723 : void DwarfDebug::skippedNonDebugFunction() {
    1511             :   // If we don't have a subprogram for this function then there will be a hole
    1512             :   // in the range information. Keep note of this by setting the previously used
    1513             :   // section to nullptr.
    1514      395723 :   PrevCU = nullptr;
    1515      395723 :   CurFn = nullptr;
    1516      395723 : }
    1517             : 
    1518             : // Gather and emit post-function debug information.
    1519        9438 : void DwarfDebug::endFunctionImpl(const MachineFunction *MF) {
    1520        9438 :   const DISubprogram *SP = MF->getFunction().getSubprogram();
    1521             : 
    1522             :   assert(CurFn == MF &&
    1523             :       "endFunction should be called with the same function as beginFunction");
    1524             : 
    1525             :   // Set DwarfDwarfCompileUnitID in MCContext to default value.
    1526       18876 :   Asm->OutStreamer->getContext().setDwarfCompileUnitID(0);
    1527             : 
    1528        9438 :   LexicalScope *FnScope = LScopes.getCurrentFunctionScope();
    1529             :   assert(!FnScope || SP == FnScope->getScopeNode());
    1530        9438 :   DwarfCompileUnit &TheCU = *CUMap.lookup(SP->getUnit());
    1531        9438 :   if (TheCU.getCUNode()->isDebugDirectivesOnly()) {
    1532           5 :     PrevLabel = nullptr;
    1533           5 :     CurFn = nullptr;
    1534        1026 :     return;
    1535             :   }
    1536             : 
    1537             :   DenseSet<InlinedEntity> Processed;
    1538        9433 :   collectEntityInfo(TheCU, SP, Processed);
    1539             : 
    1540             :   // Add the range of this function to the list of ranges for the CU.
    1541       18866 :   TheCU.addRange(RangeSpan(Asm->getFunctionBegin(), Asm->getFunctionEnd()));
    1542             : 
    1543             :   // Under -gmlt, skip building the subprogram if there are no inlined
    1544             :   // subroutines inside it. But with -fdebug-info-for-profiling, the subprogram
    1545             :   // is still needed as we need its source location.
    1546       18863 :   if (!TheCU.getCUNode()->getDebugInfoForProfiling() &&
    1547        9430 :       TheCU.getCUNode()->getEmissionKind() == DICompileUnit::LineTablesOnly &&
    1548       10457 :       LScopes.getAbstractScopesList().empty() && !IsDarwin) {
    1549             :     assert(InfoHolder.getScopeVariables().empty());
    1550        1021 :     PrevLabel = nullptr;
    1551        1021 :     CurFn = nullptr;
    1552             :     return;
    1553             :   }
    1554             : 
    1555             : #ifndef NDEBUG
    1556             :   size_t NumAbstractScopes = LScopes.getAbstractScopesList().size();
    1557             : #endif
    1558             :   // Construct abstract scopes.
    1559       70331 :   for (LexicalScope *AScope : LScopes.getAbstractScopesList()) {
    1560       61919 :     auto *SP = cast<DISubprogram>(AScope->getScopeNode());
    1561      107004 :     for (const DINode *DN : SP->getRetainedNodes()) {
    1562       45085 :       if (!Processed.insert(InlinedEntity(DN, nullptr)).second)
    1563             :         continue;
    1564             : 
    1565             :       const MDNode *Scope = nullptr;
    1566             :       if (auto *DV = dyn_cast<DILocalVariable>(DN))
    1567             :         Scope = DV->getScope();
    1568             :       else if (auto *DL = dyn_cast<DILabel>(DN))
    1569             :         Scope = DL->getScope();
    1570             :       else
    1571           0 :         llvm_unreachable("Unexpected DI type!");
    1572             : 
    1573             :       // Collect info for variables/labels that were optimized out.
    1574       45083 :       ensureAbstractEntityIsCreated(TheCU, DN, Scope);
    1575             :       assert(LScopes.getAbstractScopesList().size() == NumAbstractScopes
    1576             :              && "ensureAbstractEntityIsCreated inserted abstract scopes");
    1577             :     }
    1578       61919 :     constructAbstractSubprogramScopeDIE(TheCU, AScope);
    1579             :   }
    1580             : 
    1581        8412 :   ProcessedSPNodes.insert(SP);
    1582        8411 :   DIE &ScopeDIE = TheCU.constructSubprogramScopeDIE(SP, FnScope);
    1583        8412 :   if (auto *SkelCU = TheCU.getSkeleton())
    1584          46 :     if (!LScopes.getAbstractScopesList().empty() &&
    1585           7 :         TheCU.getCUNode()->getSplitDebugInlining())
    1586           5 :       SkelCU->constructSubprogramScopeDIE(SP, FnScope);
    1587             : 
    1588             :   // Construct call site entries.
    1589        8412 :   constructCallSiteEntryDIEs(*SP, TheCU, ScopeDIE, *MF);
    1590             : 
    1591             :   // Clear debug info
    1592             :   // Ownership of DbgVariables is a bit subtle - ScopeVariables owns all the
    1593             :   // DbgVariables except those that are also in AbstractVariables (since they
    1594             :   // can be used cross-function)
    1595        8412 :   InfoHolder.getScopeVariables().clear();
    1596        8412 :   InfoHolder.getScopeLabels().clear();
    1597        8411 :   PrevLabel = nullptr;
    1598        8411 :   CurFn = nullptr;
    1599             : }
    1600             : 
    1601             : // Register a source line with debug info. Returns the  unique label that was
    1602             : // emitted and which provides correspondence to the source line list.
    1603      622064 : void DwarfDebug::recordSourceLine(unsigned Line, unsigned Col, const MDNode *S,
    1604             :                                   unsigned Flags) {
    1605      622064 :   StringRef Fn;
    1606             :   unsigned FileNo = 1;
    1607             :   unsigned Discriminator = 0;
    1608             :   if (auto *Scope = cast_or_null<DIScope>(S)) {
    1609      622064 :     Fn = Scope->getFilename();
    1610      622064 :     if (Line != 0 && getDwarfVersion() >= 4)
    1611             :       if (auto *LBF = dyn_cast<DILexicalBlockFile>(Scope))
    1612       69362 :         Discriminator = LBF->getDiscriminator();
    1613             : 
    1614      622064 :     unsigned CUID = Asm->OutStreamer->getContext().getDwarfCompileUnitID();
    1615      622064 :     FileNo = static_cast<DwarfCompileUnit &>(*InfoHolder.getUnits()[CUID])
    1616      622064 :               .getOrCreateSourceID(Scope->getFile());
    1617             :   }
    1618      622064 :   Asm->OutStreamer->EmitDwarfLocDirective(FileNo, Line, Col, Flags, 0,
    1619      622064 :                                           Discriminator, Fn);
    1620      622064 : }
    1621             : 
    1622             : //===----------------------------------------------------------------------===//
    1623             : // Emit Methods
    1624             : //===----------------------------------------------------------------------===//
    1625             : 
    1626             : // Emit the debug info section.
    1627        1067 : void DwarfDebug::emitDebugInfo() {
    1628        1067 :   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
    1629        1067 :   Holder.emitUnits(/* UseOffsets */ false);
    1630        1068 : }
    1631             : 
    1632             : // Emit the abbreviation section.
    1633        1068 : void DwarfDebug::emitAbbreviations() {
    1634        1068 :   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
    1635             : 
    1636        1068 :   Holder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevSection());
    1637        1068 : }
    1638             : 
    1639          37 : void DwarfDebug::emitStringOffsetsTableHeader() {
    1640          37 :   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
    1641         111 :   Holder.getStringPool().emitStringOffsetsTableHeader(
    1642          37 :       *Asm, Asm->getObjFileLowering().getDwarfStrOffSection(),
    1643             :       Holder.getStringOffsetsStartSym());
    1644          37 : }
    1645             : 
    1646             : template <typename AccelTableT>
    1647           0 : void DwarfDebug::emitAccel(AccelTableT &Accel, MCSection *Section,
    1648             :                            StringRef TableName) {
    1649           0 :   Asm->OutStreamer->SwitchSection(Section);
    1650             : 
    1651             :   // Emit the full data.
    1652           0 :   emitAppleAccelTable(Asm, Accel, TableName, Section->getBeginSymbol());
    1653           0 : }
    1654           0 : 
    1655             : void DwarfDebug::emitAccelDebugNames() {
    1656           0 :   // Don't emit anything if we have no compilation units to index.
    1657             :   if (getUnits().empty())
    1658             :     return;
    1659           0 : 
    1660           0 :   emitDWARF5AccelTable(Asm, AccelDebugNames, *this, getUnits());
    1661           0 : }
    1662             : 
    1663           0 : // Emit visible names into a hashed accelerator table section.
    1664             : void DwarfDebug::emitAccelNames() {
    1665             :   emitAccel(AccelNames, Asm->getObjFileLowering().getDwarfAccelNamesSection(),
    1666           0 :             "Names");
    1667           0 : }
    1668             : 
    1669          50 : // Emit objective C classes and categories into a hashed accelerator table
    1670             : // section.
    1671          50 : void DwarfDebug::emitAccelObjC() {
    1672             :   emitAccel(AccelObjC, Asm->getObjFileLowering().getDwarfAccelObjCSection(),
    1673             :             "ObjC");
    1674          49 : }
    1675             : 
    1676             : // Emit namespace dies into a hashed accelerator table.
    1677             : void DwarfDebug::emitAccelNamespaces() {
    1678         206 :   emitAccel(AccelNamespace,
    1679         206 :             Asm->getObjFileLowering().getDwarfAccelNamespaceSection(),
    1680             :             "namespac");
    1681         206 : }
    1682             : 
    1683             : // Emit type dies into a hashed accelerator table.
    1684             : void DwarfDebug::emitAccelTypes() {
    1685         206 :   emitAccel(AccelTypes, Asm->getObjFileLowering().getDwarfAccelTypesSection(),
    1686         206 :             "types");
    1687             : }
    1688         206 : 
    1689             : // Public name handling.
    1690             : // The format for the various pubnames:
    1691         206 : //
    1692         206 : // dwarf pubnames - offset/name pairs where the offset is the offset into the CU
    1693         206 : // for the DIE that is named.
    1694             : //
    1695         206 : // gnu pubnames - offset/index value/name tuples where the offset is the offset
    1696             : // into the CU and the index value is computed according to the type of value
    1697             : // for the DIE that is named.
    1698         206 : //
    1699         206 : // For type units the offset is the offset of the skeleton DIE. For split dwarf
    1700             : // it's the offset within the debug_info/debug_types dwo section, however, the
    1701         206 : // reference in the pubname header doesn't change.
    1702             : 
    1703             : /// computeIndexValue - Compute the gdb index value for the DIE and CU.
    1704             : static dwarf::PubIndexEntryDescriptor computeIndexValue(DwarfUnit *CU,
    1705             :                                                         const DIE *Die) {
    1706             :   // Entities that ended up only in a Type Unit reference the CU instead (since
    1707             :   // the pub entry has offsets within the CU there's no real offset that can be
    1708             :   // provided anyway). As it happens all such entities (namespaces and types,
    1709             :   // types only in C++ at that) are rendered as TYPE+EXTERNAL. If this turns out
    1710             :   // not to be true it would be necessary to persist this information from the
    1711             :   // point at which the entry is added to the index data structure - since by
    1712             :   // the time the index is built from that, the original type/namespace DIE in a
    1713             :   // type unit has already been destroyed so it can't be queried for properties
    1714             :   // like tag, etc.
    1715             :   if (Die->getTag() == dwarf::DW_TAG_compile_unit)
    1716             :     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE,
    1717             :                                           dwarf::GIEL_EXTERNAL);
    1718          52 :   dwarf::GDBIndexEntryLinkage Linkage = dwarf::GIEL_STATIC;
    1719             : 
    1720             :   // We could have a specification DIE that has our most of our knowledge,
    1721             :   // look for that now.
    1722             :   if (DIEValue SpecVal = Die->findAttribute(dwarf::DW_AT_specification)) {
    1723             :     DIE &SpecDIE = SpecVal.getDIEEntry().getEntry();
    1724             :     if (SpecDIE.findAttribute(dwarf::DW_AT_external))
    1725             :       Linkage = dwarf::GIEL_EXTERNAL;
    1726             :   } else if (Die->findAttribute(dwarf::DW_AT_external))
    1727             :     Linkage = dwarf::GIEL_EXTERNAL;
    1728             : 
    1729          52 :   switch (Die->getTag()) {
    1730             :   case dwarf::DW_TAG_class_type:
    1731           2 :   case dwarf::DW_TAG_structure_type:
    1732             :   case dwarf::DW_TAG_union_type:
    1733             :   case dwarf::DW_TAG_enumeration_type:
    1734             :     return dwarf::PubIndexEntryDescriptor(
    1735             :         dwarf::GIEK_TYPE, CU->getLanguage() != dwarf::DW_LANG_C_plus_plus
    1736          50 :                               ? dwarf::GIEL_STATIC
    1737           6 :                               : dwarf::GIEL_EXTERNAL);
    1738           6 :   case dwarf::DW_TAG_typedef:
    1739             :   case dwarf::DW_TAG_base_type:
    1740          44 :   case dwarf::DW_TAG_subrange_type:
    1741             :     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_TYPE, dwarf::GIEL_STATIC);
    1742             :   case dwarf::DW_TAG_namespace:
    1743          50 :     return dwarf::GIEK_TYPE;
    1744           5 :   case dwarf::DW_TAG_subprogram:
    1745             :     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_FUNCTION, Linkage);
    1746             :   case dwarf::DW_TAG_variable:
    1747             :     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE, Linkage);
    1748           5 :   case dwarf::DW_TAG_enumerator:
    1749           5 :     return dwarf::PubIndexEntryDescriptor(dwarf::GIEK_VARIABLE,
    1750             :                                           dwarf::GIEL_STATIC);
    1751           5 :   default:
    1752             :     return dwarf::GIEK_NONE;
    1753             :   }
    1754             : }
    1755           3 : 
    1756             : /// emitDebugPubSections - Emit visible names and types into debug pubnames and
    1757          10 : /// pubtypes sections.
    1758             : void DwarfDebug::emitDebugPubSections() {
    1759          14 :   for (const auto &NU : CUMap) {
    1760             :     DwarfCompileUnit *TheU = NU.second;
    1761          18 :     if (!TheU->hasDwarfPubSections())
    1762             :       continue;
    1763             : 
    1764           0 :     bool GnuStyle = TheU->getCUNode()->getNameTableKind() ==
    1765             :                     DICompileUnit::DebugNameTableKind::GNU;
    1766           0 : 
    1767             :     Asm->OutStreamer->SwitchSection(
    1768             :         GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubNamesSection()
    1769             :                  : Asm->getObjFileLowering().getDwarfPubNamesSection());
    1770             :     emitDebugPubSection(GnuStyle, "Names", TheU, TheU->getGlobalNames());
    1771             : 
    1772        1068 :     Asm->OutStreamer->SwitchSection(
    1773        2411 :         GnuStyle ? Asm->getObjFileLowering().getDwarfGnuPubTypesSection()
    1774        1343 :                  : Asm->getObjFileLowering().getDwarfPubTypesSection());
    1775        1343 :     emitDebugPubSection(GnuStyle, "Types", TheU, TheU->getGlobalTypes());
    1776             :   }
    1777             : }
    1778         799 : 
    1779         799 : void DwarfDebug::emitSectionReference(const DwarfCompileUnit &CU) {
    1780             :   if (useSectionsAsReferences())
    1781        1598 :     Asm->EmitDwarfOffset(CU.getSection()->getBeginSymbol(),
    1782           6 :                          CU.getDebugSectionOffset());
    1783         799 :   else
    1784         799 :     Asm->emitDwarfSymbolReference(CU.getLabelBegin());
    1785             : }
    1786        1598 : 
    1787           6 : void DwarfDebug::emitDebugPubSection(bool GnuStyle, StringRef Name,
    1788         799 :                                      DwarfCompileUnit *TheU,
    1789         799 :                                      const StringMap<const DIE *> &Globals) {
    1790             :   if (auto *Skeleton = TheU->getSkeleton())
    1791        1068 :     TheU = Skeleton;
    1792             : 
    1793        1605 :   // Emit the header.
    1794        1605 :   Asm->OutStreamer->AddComment("Length of Public " + Name + " Info");
    1795           0 :   MCSymbol *BeginLabel = Asm->createTempSymbol("pub" + Name + "_begin");
    1796           0 :   MCSymbol *EndLabel = Asm->createTempSymbol("pub" + Name + "_end");
    1797             :   Asm->EmitLabelDifference(EndLabel, BeginLabel, 4);
    1798        1605 : 
    1799        1605 :   Asm->OutStreamer->EmitLabel(BeginLabel);
    1800             : 
    1801        1598 :   Asm->OutStreamer->AddComment("DWARF Version");
    1802             :   Asm->emitInt16(dwarf::DW_PUBNAMES_VERSION);
    1803             : 
    1804        1598 :   Asm->OutStreamer->AddComment("Offset of Compilation Unit Info");
    1805             :   emitSectionReference(*TheU);
    1806             : 
    1807             :   Asm->OutStreamer->AddComment("Compilation Unit Length");
    1808        3196 :   Asm->emitInt32(TheU->getLength());
    1809        1598 : 
    1810        1598 :   // Emit the pubnames for this compilation unit.
    1811        1598 :   for (const auto &GI : Globals) {
    1812             :     const char *Name = GI.getKeyData();
    1813        3196 :     const DIE *Entity = GI.second;
    1814             : 
    1815        4794 :     Asm->OutStreamer->AddComment("DIE offset");
    1816        1598 :     Asm->emitInt32(Entity->getOffset());
    1817             : 
    1818        4794 :     if (GnuStyle) {
    1819        1598 :       dwarf::PubIndexEntryDescriptor Desc = computeIndexValue(TheU, Entity);
    1820             :       Asm->OutStreamer->AddComment(
    1821        4794 :           Twine("Kind: ") + dwarf::GDBIndexEntryKindString(Desc.Kind) + ", " +
    1822        1598 :           dwarf::GDBIndexEntryLinkageString(Desc.Linkage));
    1823             :       Asm->emitInt8(Desc.toBits());
    1824             :     }
    1825        5268 : 
    1826             :     Asm->OutStreamer->AddComment("External Name");
    1827        2072 :     Asm->OutStreamer->EmitBytes(StringRef(Name, GI.getKeyLength() + 1));
    1828             :   }
    1829        6216 : 
    1830        2072 :   Asm->OutStreamer->AddComment("End Mark");
    1831             :   Asm->emitInt32(0);
    1832        2072 :   Asm->OutStreamer->EmitLabel(EndLabel);
    1833          52 : }
    1834         104 : 
    1835          52 : /// Emit null-terminated strings into a debug str section.
    1836          52 : void DwarfDebug::emitDebugStr() {
    1837         104 :   MCSection *StringOffsetsSection = nullptr;
    1838             :   if (useSegmentedStringOffsetsTable()) {
    1839             :     emitStringOffsetsTableHeader();
    1840        6216 :     StringOffsetsSection = Asm->getObjFileLowering().getDwarfStrOffSection();
    1841        6216 :   }
    1842             :   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
    1843             :   Holder.emitStrings(Asm->getObjFileLowering().getDwarfStrSection(),
    1844        4794 :                      StringOffsetsSection, /* UseRelativeOffsets = */ true);
    1845        1598 : }
    1846        3196 : 
    1847        1598 : void DwarfDebug::emitDebugLocEntry(ByteStreamer &Streamer,
    1848             :                                    const DebugLocStream::Entry &Entry) {
    1849             :   auto &&Comments = DebugLocs.getComments(Entry);
    1850        1068 :   auto Comment = Comments.begin();
    1851             :   auto End = Comments.end();
    1852        1068 :   for (uint8_t Byte : DebugLocs.getBytes(Entry))
    1853          37 :     Streamer.EmitInt8(Byte, Comment != End ? *(Comment++) : "");
    1854          37 : }
    1855             : 
    1856        1068 : static void emitDebugLocValue(const AsmPrinter &AP, const DIBasicType *BT,
    1857        1068 :                               const DebugLocEntry::Value &Value,
    1858             :                               DwarfExpression &DwarfExpr) {
    1859        1068 :   auto *DIExpr = Value.getExpression();
    1860             :   DIExpressionCursor ExprCursor(DIExpr);
    1861       85401 :   DwarfExpr.addFragmentOffset(DIExpr);
    1862             :   // Regular entry.
    1863             :   if (Value.isInt()) {
    1864             :     if (BT && (BT->getEncoding() == dwarf::DW_ATE_signed ||
    1865             :                BT->getEncoding() == dwarf::DW_ATE_signed_char))
    1866      229253 :       DwarfExpr.addSignedConstant(Value.getInt());
    1867      143925 :     else
    1868       85401 :       DwarfExpr.addUnsignedConstant(Value.getInt());
    1869             :   } else if (Value.isLocation()) {
    1870           0 :     MachineLocation Location = Value.getLoc();
    1871             :     if (Location.isIndirect())
    1872             :       DwarfExpr.setMemoryLocationKind();
    1873           0 :     DIExpressionCursor Cursor(DIExpr);
    1874             :     const TargetRegisterInfo &TRI = *AP.MF->getSubtarget().getRegisterInfo();
    1875           0 :     if (!DwarfExpr.addMachineRegExpression(TRI, Cursor, Location.getReg()))
    1876             :       return;
    1877           0 :     return DwarfExpr.addExpression(std::move(Cursor));
    1878           0 :   } else if (Value.isConstantFP()) {
    1879             :     APInt RawBytes = Value.getConstantFP()->getValueAPF().bitcastToAPInt();
    1880           0 :     DwarfExpr.addUnsignedConstant(RawBytes);
    1881             :   }
    1882           0 :   DwarfExpr.addExpression(std::move(ExprCursor));
    1883           0 : }
    1884             : 
    1885           0 : void DebugLocEntry::finalize(const AsmPrinter &AP,
    1886             :                              DebugLocStream::ListBuilder &List,
    1887             :                              const DIBasicType *BT) {
    1888           0 :   DebugLocStream::EntryBuilder Entry(List, Begin, End);
    1889           0 :   BufferByteStreamer Streamer = Entry.getStreamer();
    1890           0 :   DebugLocDwarfExpression DwarfExpr(AP.getDwarfVersion(), Streamer);
    1891           0 :   const DebugLocEntry::Value &Value = Values[0];
    1892           0 :   if (Value.isFragment()) {
    1893           0 :     // Emit all fragments that belong to the same variable and range.
    1894           0 :     assert(llvm::all_of(Values, [](DebugLocEntry::Value P) {
    1895             :           return P.isFragment();
    1896           0 :         }) && "all values are expected to be fragments");
    1897             :     assert(std::is_sorted(Values.begin(), Values.end()) &&
    1898             :            "fragments are expected to be sorted");
    1899       85416 : 
    1900             :     for (auto Fragment : Values)
    1901             :       emitDebugLocValue(AP, BT, Fragment, DwarfExpr);
    1902       85416 : 
    1903       85416 :   } else {
    1904       85416 :     assert(Values.size() == 1 && "only fragments may have >1 value");
    1905             :     emitDebugLocValue(AP, BT, Value, DwarfExpr);
    1906       85416 :   }
    1907             :   DwarfExpr.finalize();
    1908             : }
    1909             : 
    1910             : void DwarfDebug::emitDebugLocEntryLocation(const DebugLocStream::Entry &Entry) {
    1911             :   // Emit the size.
    1912             :   Asm->OutStreamer->AddComment("Loc expr size");
    1913             :   Asm->emitInt16(DebugLocs.getBytes(Entry).size());
    1914         980 : 
    1915         593 :   // Emit the entry.
    1916             :   APByteStreamer Streamer(*Asm);
    1917             :   emitDebugLocEntry(Streamer, Entry);
    1918             : }
    1919       85029 : 
    1920             : // Emit locations into the debug loc section.
    1921       85416 : void DwarfDebug::emitDebugLoc() {
    1922       85416 :   if (DebugLocs.getLists().empty())
    1923             :     return;
    1924       85397 : 
    1925             :   // Start the dwarf loc section.
    1926      256191 :   Asm->OutStreamer->SwitchSection(
    1927      170794 :       Asm->getObjFileLowering().getDwarfLocSection());
    1928             :   unsigned char Size = Asm->MAI->getCodePointerSize();
    1929             :   for (const auto &List : DebugLocs.getLists()) {
    1930       85397 :     Asm->OutStreamer->EmitLabel(List.Label);
    1931       85397 :     const DwarfCompileUnit *CU = List.CU;
    1932       85397 :     for (const auto &Entry : DebugLocs.getEntries(List)) {
    1933             :       // Set up the range. This range is relative to the entry point of the
    1934             :       // compile unit. This is a hard coded 0 for low_pc when we're emitting
    1935        1030 :       // ranges, or the DW_AT_low_pc on the compile unit otherwise.
    1936        1030 :       if (auto *Base = CU->getBaseAddress()) {
    1937             :         Asm->EmitLabelDifference(Entry.BeginSym, Base, Size);
    1938             :         Asm->EmitLabelDifference(Entry.EndSym, Base, Size);
    1939             :       } else {
    1940         242 :         Asm->OutStreamer->EmitSymbolValue(Entry.BeginSym, Size);
    1941         242 :         Asm->OutStreamer->EmitSymbolValue(Entry.EndSym, Size);
    1942         242 :       }
    1943       47407 : 
    1944       94330 :       emitDebugLocEntryLocation(Entry);
    1945       47165 :     }
    1946      132553 :     Asm->OutStreamer->EmitIntValue(0, Size);
    1947             :     Asm->OutStreamer->EmitIntValue(0, Size);
    1948             :   }
    1949             : }
    1950       85388 : 
    1951        2040 : void DwarfDebug::emitDebugLocDWO() {
    1952        2040 :   Asm->OutStreamer->SwitchSection(
    1953             :       Asm->getObjFileLowering().getDwarfLocDWOSection());
    1954      166696 :   for (const auto &List : DebugLocs.getLists()) {
    1955      166696 :     Asm->OutStreamer->EmitLabel(List.Label);
    1956             :     for (const auto &Entry : DebugLocs.getEntries(List)) {
    1957             :       // Just always use start_length for now - at least that's one address
    1958       85388 :       // rather than two. We could get fancier and try to, say, reuse an
    1959             :       // address we know we've emitted elsewhere (the start of the function?
    1960       94330 :       // The start of the CU or CU subrange that encloses this range?)
    1961       94330 :       Asm->emitInt8(dwarf::DW_LLE_startx_length);
    1962             :       unsigned idx = AddrPool.getIndex(Entry.BeginSym);
    1963             :       Asm->EmitULEB128(idx);
    1964             :       Asm->EmitLabelDifference(Entry.EndSym, Entry.BeginSym, 4);
    1965          37 : 
    1966          37 :       emitDebugLocEntryLocation(Entry);
    1967          37 :     }
    1968          44 :     Asm->emitInt8(dwarf::DW_LLE_end_of_list);
    1969          14 :   }
    1970          16 : }
    1971             : 
    1972             : struct ArangeSpan {
    1973             :   const MCSymbol *Start, *End;
    1974             : };
    1975           9 : 
    1976           9 : // Emit a debug aranges section, containing a CU lookup for any
    1977           9 : // address we can tie back to a CU.
    1978           9 : void DwarfDebug::emitDebugARanges() {
    1979             :   // Provides a unique id per text section.
    1980           9 :   MapVector<MCSection *, SmallVector<SymbolCU, 8>> SectionMap;
    1981             : 
    1982           7 :   // Filter labels by section.
    1983             :   for (const SymbolCU &SCU : ArangeLabels) {
    1984          37 :     if (SCU.Sym->isInSection()) {
    1985             :       // Make a note of this symbol and it's section.
    1986             :       MCSection *Section = &SCU.Sym->getSection();
    1987             :       if (!Section->getKind().isMetadata())
    1988             :         SectionMap[Section].push_back(SCU);
    1989             :     } else {
    1990             :       // Some symbols (e.g. common/bss on mach-o) can have no section but still
    1991             :       // appear in the output. This sucks as we rely on sections to build
    1992           6 :       // arange spans. We can do it without, but it's icky.
    1993             :       SectionMap[nullptr].push_back(SCU);
    1994             :     }
    1995             :   }
    1996             : 
    1997          24 :   DenseMap<DwarfCompileUnit *, std::vector<ArangeSpan>> Spans;
    1998          18 : 
    1999             :   for (auto &I : SectionMap) {
    2000          17 :     MCSection *Section = I.first;
    2001          17 :     SmallVector<SymbolCU, 8> &List = I.second;
    2002          17 :     if (List.size() < 1)
    2003             :       continue;
    2004             : 
    2005             :     // If we have no section (e.g. common), just write out
    2006             :     // individual spans for each symbol.
    2007           1 :     if (!Section) {
    2008             :       for (const SymbolCU &Cur : List) {
    2009             :         ArangeSpan Span;
    2010             :         Span.Start = Cur.Sym;
    2011             :         Span.End = nullptr;
    2012             :         assert(Cur.CU);
    2013          17 :         Spans[Cur.CU].push_back(Span);
    2014          11 :       }
    2015             :       continue;
    2016          22 :     }
    2017             : 
    2018             :     // Sort the symbols by offset within the section.
    2019             :     std::stable_sort(
    2020             :         List.begin(), List.end(), [&](const SymbolCU &A, const SymbolCU &B) {
    2021          11 :           unsigned IA = A.Sym ? Asm->OutStreamer->GetSymbolOrder(A.Sym) : 0;
    2022           2 :           unsigned IB = B.Sym ? Asm->OutStreamer->GetSymbolOrder(B.Sym) : 0;
    2023             : 
    2024           1 :           // Symbols with no order assigned should be placed at the end.
    2025           1 :           // (e.g. section end labels)
    2026             :           if (IA == 0)
    2027           1 :             return false;
    2028             :           if (IB == 0)
    2029             :             return true;
    2030             :           return IA < IB;
    2031             :         });
    2032             : 
    2033             :     // Insert a final terminator.
    2034             :     List.push_back(SymbolCU(nullptr, Asm->OutStreamer->endSection(Section)));
    2035             : 
    2036             :     // Build spans between each label.
    2037             :     const MCSymbol *StartSym = List[0].Sym;
    2038             :     for (size_t n = 1, e = List.size(); n < e; n++) {
    2039             :       const SymbolCU &Prev = List[n - 1];
    2040             :       const SymbolCU &Cur = List[n];
    2041             : 
    2042             :       // Try and build the longest span we can within the same CU.
    2043             :       if (Cur.CU != Prev.CU) {
    2044             :         ArangeSpan Span;
    2045             :         Span.Start = StartSym;
    2046             :         Span.End = Cur.Sym;
    2047             :         assert(Prev.CU);
    2048          20 :         Spans[Prev.CU].push_back(Span);
    2049             :         StartSym = Cur.Sym;
    2050             :       }
    2051          10 :     }
    2052          27 :   }
    2053          17 : 
    2054             :   // Start the dwarf aranges section.
    2055             :   Asm->OutStreamer->SwitchSection(
    2056             :       Asm->getObjFileLowering().getDwarfARangesSection());
    2057          17 : 
    2058             :   unsigned PtrSize = Asm->MAI->getCodePointerSize();
    2059          11 : 
    2060          11 :   // Build a list of CUs used.
    2061             :   std::vector<DwarfCompileUnit *> CUs;
    2062          11 :   for (const auto &it : Spans) {
    2063          11 :     DwarfCompileUnit *CU = it.first;
    2064             :     CUs.push_back(CU);
    2065             :   }
    2066             : 
    2067             :   // Sort the CU list (again, to ensure consistent output order).
    2068             :   llvm::sort(CUs, [](const DwarfCompileUnit *A, const DwarfCompileUnit *B) {
    2069           6 :     return A->getUniqueID() < B->getUniqueID();
    2070           6 :   });
    2071             : 
    2072           6 :   // Emit an arange table for each CU we used.
    2073             :   for (DwarfCompileUnit *CU : CUs) {
    2074             :     std::vector<ArangeSpan> &List = Spans[CU];
    2075             : 
    2076          13 :     // Describe the skeleton CU's offset and length, not the dwo file's.
    2077           7 :     if (auto *Skel = CU->getSkeleton())
    2078           7 :       CU = Skel;
    2079             : 
    2080             :     // Emit size of content not including length itself.
    2081             :     unsigned ContentSize =
    2082             :         sizeof(int16_t) + // DWARF ARange version number
    2083           0 :         sizeof(int32_t) + // Offset of CU in the .debug_info section
    2084             :         sizeof(int8_t) +  // Pointer Size (in bytes)
    2085             :         sizeof(int8_t);   // Segment Size (in bytes)
    2086             : 
    2087          13 :     unsigned TupleSize = PtrSize * 2;
    2088             : 
    2089             :     // 7.20 in the Dwarf specs requires the table to be aligned to a tuple.
    2090             :     unsigned Padding =
    2091           7 :         OffsetToAlignment(sizeof(int32_t) + ContentSize, TupleSize);
    2092           0 : 
    2093             :     ContentSize += Padding;
    2094             :     ContentSize += (List.size() + 1) * TupleSize;
    2095             : 
    2096             :     // For each compile unit, write the list of spans it covers.
    2097             :     Asm->OutStreamer->AddComment("Length of ARange Set");
    2098             :     Asm->emitInt32(ContentSize);
    2099             :     Asm->OutStreamer->AddComment("DWARF Arange version number");
    2100             :     Asm->emitInt16(dwarf::DW_ARANGES_VERSION);
    2101           7 :     Asm->OutStreamer->AddComment("Offset Into Debug Info Section");
    2102             :     emitSectionReference(*CU);
    2103             :     Asm->OutStreamer->AddComment("Address Size (in bytes)");
    2104             :     Asm->emitInt8(PtrSize);
    2105           7 :     Asm->OutStreamer->AddComment("Segment Size (in bytes)");
    2106             :     Asm->emitInt8(0);
    2107           7 : 
    2108           7 :     Asm->OutStreamer->emitFill(Padding, 0xff);
    2109             : 
    2110             :     for (const ArangeSpan &Span : List) {
    2111          21 :       Asm->EmitLabelReference(Span.Start, PtrSize);
    2112           7 : 
    2113          21 :       // Calculate the size as being from the span start to it's end.
    2114           7 :       if (Span.End) {
    2115          21 :         Asm->EmitLabelDifference(Span.End, Span.Start, PtrSize);
    2116           7 :       } else {
    2117          21 :         // For symbols without an end marker (e.g. common), we
    2118           7 :         // write a single arange entry containing just that one symbol.
    2119          21 :         uint64_t Size = SymSize[Span.Start];
    2120           7 :         if (Size == 0)
    2121             :           Size = 1;
    2122          14 : 
    2123             :         Asm->OutStreamer->EmitIntValue(Size, PtrSize);
    2124          19 :       }
    2125          12 :     }
    2126             : 
    2127             :     Asm->OutStreamer->AddComment("ARange terminator");
    2128          12 :     Asm->OutStreamer->EmitIntValue(0, PtrSize);
    2129          11 :     Asm->OutStreamer->EmitIntValue(0, PtrSize);
    2130             :   }
    2131             : }
    2132             : 
    2133           1 : /// Emit a single range list. We handle both DWARF v5 and earlier.
    2134           1 : static void emitRangeList(AsmPrinter *Asm, DwarfCompileUnit *CU,
    2135             :                           const RangeSpanList &List) {
    2136             : 
    2137           2 :   auto DwarfVersion = CU->getDwarfVersion();
    2138             :   // Emit our symbol so we can find the beginning of the range.
    2139             :   Asm->OutStreamer->EmitLabel(List.getSym());
    2140             :   // Gather all the ranges that apply to the same section so they can share
    2141          21 :   // a base address entry.
    2142          14 :   MapVector<const MCSection *, std::vector<const RangeSpan *>> SectionRanges;
    2143          14 :   // Size for our labels.
    2144             :   auto Size = Asm->MAI->getCodePointerSize();
    2145           6 : 
    2146             :   for (const RangeSpan &Range : List.getRanges())
    2147             :     SectionRanges[&Range.getStart()->getSection()].push_back(&Range);
    2148       79462 : 
    2149             :   auto *CUBase = CU->getBaseAddress();
    2150             :   bool BaseIsSet = false;
    2151       79462 :   for (const auto &P : SectionRanges) {
    2152             :     // Don't bother with a base address entry if there's only one range in
    2153       79462 :     // this section in this range list - for example ranges for a CU will
    2154             :     // usually consist of single regions from each of many sections
    2155             :     // (-ffunction-sections, or just C++ inline functions) except under LTO
    2156             :     // or optnone where there may be holes in a single CU's section
    2157             :     // contributions.
    2158       79462 :     auto *Base = CUBase;
    2159             :     if (!Base && P.second.size() > 1 &&
    2160      323619 :         (UseDwarfRangesBaseAddressSpecifier || DwarfVersion >= 5)) {
    2161      488314 :       BaseIsSet = true;
    2162             :       // FIXME/use care: This may not be a useful base address if it's not
    2163       79462 :       // the lowest address/range in this object.
    2164             :       Base = P.second.front()->getStart();
    2165      166288 :       if (DwarfVersion >= 5) {
    2166             :         Asm->OutStreamer->AddComment("DW_RLE_base_address");
    2167             :         Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_base_address, 1);
    2168             :       } else
    2169             :         Asm->OutStreamer->EmitIntValue(-1, Size);
    2170             :       Asm->OutStreamer->AddComment("  base address");
    2171             :       Asm->OutStreamer->EmitSymbolValue(Base, Size);
    2172             :     } else if (BaseIsSet && DwarfVersion < 5) {
    2173       86826 :       BaseIsSet = false;
    2174       79245 :       assert(!Base);
    2175             :       Asm->OutStreamer->EmitIntValue(-1, Size);
    2176             :       Asm->OutStreamer->EmitIntValue(0, Size);
    2177             :     }
    2178           3 : 
    2179           3 :     for (const auto *RS : P.second) {
    2180           4 :       const MCSymbol *Begin = RS->getStart();
    2181           2 :       const MCSymbol *End = RS->getEnd();
    2182             :       assert(Begin && "Range without a begin symbol?");
    2183           1 :       assert(End && "Range without an end symbol?");
    2184           6 :       if (Base) {
    2185           3 :         if (DwarfVersion >= 5) {
    2186       86823 :           // Emit DW_RLE_offset_pair when we have a base.
    2187             :           Asm->OutStreamer->AddComment("DW_RLE_offset_pair");
    2188             :           Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_offset_pair, 1);
    2189           1 :           Asm->OutStreamer->AddComment("  starting offset");
    2190           1 :           Asm->EmitLabelDifferenceAsULEB128(Begin, Base);
    2191             :           Asm->OutStreamer->AddComment("  ending offset");
    2192             :           Asm->EmitLabelDifferenceAsULEB128(End, Base);
    2193      330983 :         } else {
    2194      244157 :           Asm->EmitLabelDifference(Begin, Base, Size);
    2195      244157 :           Asm->EmitLabelDifference(End, Base, Size);
    2196             :         }
    2197             :       } else if (DwarfVersion >= 5) {
    2198      244157 :         Asm->OutStreamer->AddComment("DW_RLE_start_length");
    2199         283 :         Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_start_length, 1);
    2200             :         Asm->OutStreamer->AddComment("  start");
    2201          18 :         Asm->OutStreamer->EmitSymbolValue(Begin, Size);
    2202           9 :         Asm->OutStreamer->AddComment("  length");
    2203          18 :         Asm->EmitLabelDifferenceAsULEB128(End, Begin);
    2204           9 :       } else {
    2205          18 :         Asm->OutStreamer->EmitSymbolValue(Begin, Size);
    2206           9 :         Asm->OutStreamer->EmitSymbolValue(End, Size);
    2207             :       }
    2208         274 :     }
    2209         274 :   }
    2210             :   if (DwarfVersion >= 5) {
    2211      243874 :     Asm->OutStreamer->AddComment("DW_RLE_end_of_list");
    2212          14 :     Asm->OutStreamer->EmitIntValue(dwarf::DW_RLE_end_of_list, 1);
    2213           7 :   } else {
    2214          14 :     // Terminate the list with two 0 values.
    2215           7 :     Asm->OutStreamer->EmitIntValue(0, Size);
    2216          14 :     Asm->OutStreamer->EmitIntValue(0, Size);
    2217           7 :   }
    2218             : }
    2219      243867 : 
    2220      243867 : // Emit the header of a DWARF 5 range list table. Returns the symbol that
    2221             : // designates the end of the table for the caller to emit when the table is
    2222             : // complete.
    2223             : static MCSymbol *emitRnglistsTableHeader(AsmPrinter *Asm, DwarfFile &Holder) {
    2224       79462 :   // The length is described by a starting label right after the length field
    2225          12 :   // and an end label.
    2226           6 :   MCSymbol *TableStart = Asm->createTempSymbol("debug_rnglist_table_start");
    2227             :   MCSymbol *TableEnd = Asm->createTempSymbol("debug_rnglist_table_end");
    2228             :   // Build the range table header, which starts with the length field.
    2229       79456 :   Asm->EmitLabelDifference(TableEnd, TableStart, 4);
    2230       79456 :   Asm->OutStreamer->EmitLabel(TableStart);
    2231             :   // Version number (DWARF v5 and later).
    2232       79462 :   Asm->emitInt16(Asm->OutStreamer->getContext().getDwarfVersion());
    2233             :   // Address size.
    2234             :   Asm->emitInt8(Asm->MAI->getCodePointerSize());
    2235             :   // Segment selector size.
    2236             :   Asm->emitInt8(0);
    2237           5 : 
    2238             :   MCSymbol *RnglistTableBaseSym = Holder.getRnglistsTableBaseSym();
    2239             : 
    2240           5 :   // FIXME: Generate the offsets table and use DW_FORM_rnglistx with the
    2241           5 :   // DW_AT_ranges attribute. Until then set the number of offsets to 0.
    2242             :   Asm->emitInt32(0);
    2243           5 :   Asm->OutStreamer->EmitLabel(RnglistTableBaseSym);
    2244           5 :   return TableEnd;
    2245             : }
    2246           5 : 
    2247             : /// Emit address ranges into the .debug_ranges section or into the DWARF v5
    2248           5 : /// .debug_rnglists section.
    2249             : void DwarfDebug::emitDebugRanges() {
    2250           5 :   if (CUMap.empty())
    2251             :     return;
    2252           5 : 
    2253             :   auto NoRangesPresent = [this]() {
    2254             :     return llvm::all_of(
    2255             :         CUMap, [](const decltype(CUMap)::value_type &Pair) {
    2256           5 :           return Pair.second->getRangeLists().empty();
    2257           5 :         });
    2258           5 :   };
    2259             : 
    2260             :   if (llvm::all_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
    2261             :         return Pair.second->getCUNode()->isDebugDirectivesOnly();
    2262             :       })) {
    2263        1068 :     assert(NoRangesPresent() && "No debug ranges expected.");
    2264        1068 :     return;
    2265             :   }
    2266             : 
    2267             :   if (!useRangesSection()) {
    2268             :     assert(NoRangesPresent() && "No debug ranges expected.");
    2269             :     return;
    2270           0 :   }
    2271             : 
    2272             :   if (NoRangesPresent())
    2273             :     return;
    2274        1027 : 
    2275           0 :   // Start the dwarf ranges section.
    2276             :   MCSymbol *TableEnd = nullptr;
    2277             :   if (getDwarfVersion() >= 5) {
    2278             :     Asm->OutStreamer->SwitchSection(
    2279             :         Asm->getObjFileLowering().getDwarfRnglistsSection());
    2280             :     TableEnd = emitRnglistsTableHeader(Asm, useSplitDwarf() ? SkeletonHolder
    2281        1025 :                                                             : InfoHolder);
    2282             :   } else
    2283             :     Asm->OutStreamer->SwitchSection(
    2284             :         Asm->getObjFileLowering().getDwarfRangesSection());
    2285             : 
    2286        1018 :   // Grab the specific ranges for the compile units in the module.
    2287             :   for (const auto &I : CUMap) {
    2288             :     DwarfCompileUnit *TheCU = I.second;
    2289             :     if (TheCU->getCUNode()->isDebugDirectivesOnly())
    2290             :       continue;
    2291         151 : 
    2292           5 :     if (auto *Skel = TheCU->getSkeleton())
    2293           5 :       TheCU = Skel;
    2294           5 : 
    2295             :     // Iterate over the misc ranges for the compile units in the module.
    2296             :     for (const RangeSpanList &List : TheCU->getRangeLists())
    2297         146 :       emitRangeList(Asm, TheCU, List);
    2298         146 :   }
    2299             : 
    2300             :   if (TableEnd)
    2301         302 :     Asm->OutStreamer->EmitLabel(TableEnd);
    2302         151 : }
    2303         151 : 
    2304             : void DwarfDebug::handleMacroNodes(DIMacroNodeArray Nodes, DwarfCompileUnit &U) {
    2305             :   for (auto *MN : Nodes) {
    2306         151 :     if (auto *M = dyn_cast<DIMacro>(MN))
    2307             :       emitMacro(*M);
    2308             :     else if (auto *F = dyn_cast<DIMacroFile>(MN))
    2309             :       emitMacroFile(*F, U);
    2310       79613 :     else
    2311       79462 :       llvm_unreachable("Unexpected DI type!");
    2312             :   }
    2313             : }
    2314         151 : 
    2315          10 : void DwarfDebug::emitMacro(DIMacro &M) {
    2316             :   Asm->EmitULEB128(M.getMacinfoType());
    2317             :   Asm->EmitULEB128(M.getLine());
    2318           5 :   StringRef Name = M.getName();
    2319          12 :   StringRef Value = M.getValue();
    2320             :   Asm->OutStreamer->EmitBytes(Name);
    2321           4 :   if (!Value.empty()) {
    2322             :     // There should be one space between macro name and macro value.
    2323           3 :     Asm->emitInt8(' ');
    2324             :     Asm->OutStreamer->EmitBytes(Value);
    2325           0 :   }
    2326             :   Asm->emitInt8('\0');
    2327           5 : }
    2328             : 
    2329           4 : void DwarfDebug::emitMacroFile(DIMacroFile &F, DwarfCompileUnit &U) {
    2330           4 :   assert(F.getMacinfoType() == dwarf::DW_MACINFO_start_file);
    2331           4 :   Asm->EmitULEB128(dwarf::DW_MACINFO_start_file);
    2332           4 :   Asm->EmitULEB128(F.getLine());
    2333           4 :   Asm->EmitULEB128(U.getOrCreateSourceID(F.getFile()));
    2334           8 :   handleMacroNodes(F.getElements(), U);
    2335           4 :   Asm->EmitULEB128(dwarf::DW_MACINFO_end_file);
    2336             : }
    2337           2 : 
    2338           4 : /// Emit macros into a debug macinfo section.
    2339             : void DwarfDebug::emitDebugMacinfo() {
    2340           4 :   if (CUMap.empty())
    2341           4 :     return;
    2342             : 
    2343           3 :   if (llvm::all_of(CUMap, [](const decltype(CUMap)::value_type &Pair) {
    2344             :         return Pair.second->getCUNode()->isDebugDirectivesOnly();
    2345           3 :       }))
    2346           3 :     return;
    2347           3 : 
    2348           3 :   // Start the dwarf macinfo section.
    2349           3 :   Asm->OutStreamer->SwitchSection(
    2350           3 :       Asm->getObjFileLowering().getDwarfMacinfoSection());
    2351             : 
    2352             :   for (const auto &P : CUMap) {
    2353        1068 :     auto &TheCU = *P.second;
    2354        1068 :     if (TheCU.getCUNode()->isDebugDirectivesOnly())
    2355             :       continue;
    2356             :     auto *SkCU = TheCU.getSkeleton();
    2357        1027 :     DwarfCompileUnit &U = SkCU ? *SkCU : TheCU;
    2358           0 :     auto *CUNode = cast<DICompileUnit>(P.first);
    2359             :     DIMacroNodeArray Macros = CUNode->getMacros();
    2360             :     if (!Macros.empty()) {
    2361             :       Asm->OutStreamer->EmitLabel(U.getMacroLabelBegin());
    2362             :       handleMacroNodes(Macros, U);
    2363        1025 :     }
    2364        1025 :   }
    2365             :   Asm->OutStreamer->AddComment("End Of Macro List Mark");
    2366        2367 :   Asm->emitInt8(0);
    2367        1342 : }
    2368        1342 : 
    2369             : // DWARF5 Experimental Separate Dwarf emitters.
    2370        1342 : 
    2371        1342 : void DwarfDebug::initSkeletonUnit(const DwarfUnit &U, DIE &Die,
    2372        1342 :                                   std::unique_ptr<DwarfCompileUnit> NewU) {
    2373             :   NewU->addString(Die, dwarf::DW_AT_GNU_dwo_name,
    2374             :                   Asm->TM.Options.MCOptions.SplitDwarfFile);
    2375           4 : 
    2376           2 :   if (!CompilationDir.empty())
    2377             :     NewU->addString(Die, dwarf::DW_AT_comp_dir, CompilationDir);
    2378             : 
    2379        3075 :   addGnuPubAttributes(*NewU, Die);
    2380        1025 : 
    2381             :   SkeletonHolder.addUnit(std::move(NewU));
    2382             : }
    2383             : 
    2384             : DwarfCompileUnit &DwarfDebug::constructSkeletonCU(const DwarfCompileUnit &CU) {
    2385          43 : 
    2386             :   auto OwnedUnit = llvm::make_unique<DwarfCompileUnit>(
    2387          43 :       CU.getUniqueID(), CU.getCUNode(), Asm, this, &SkeletonHolder);
    2388          43 :   DwarfCompileUnit &NewCU = *OwnedUnit;
    2389             :   NewCU.setSection(Asm->getObjFileLowering().getDwarfInfoSection());
    2390          43 : 
    2391          43 :   NewCU.initStmtList();
    2392             : 
    2393          43 :   if (useSegmentedStringOffsetsTable())
    2394             :     NewCU.addStringOffsetsStart();
    2395          86 : 
    2396          43 :   initSkeletonUnit(CU, NewCU.getUnitDie(), std::move(OwnedUnit));
    2397             : 
    2398          43 :   return NewCU;
    2399             : }
    2400             : 
    2401          43 : // Emit the .debug_info.dwo section for separated dwarf. This contains the
    2402             : // compile units that would normally be in debug_info.
    2403          43 : void DwarfDebug::emitDebugInfoDWO() {
    2404             :   assert(useSplitDwarf() && "No split dwarf debug info?");
    2405          43 :   // Don't emit relocations into the dwo file.
    2406             :   InfoHolder.emitUnits(/* UseOffsets */ true);
    2407          43 : }
    2408           8 : 
    2409             : // Emit the .debug_abbrev.dwo section for separated dwarf. This contains the
    2410          43 : // abbreviations for the .debug_info.dwo section.
    2411             : void DwarfDebug::emitDebugAbbrevDWO() {
    2412          43 :   assert(useSplitDwarf() && "No split dwarf?");
    2413             :   InfoHolder.emitAbbrevs(Asm->getObjFileLowering().getDwarfAbbrevDWOSection());
    2414             : }
    2415             : 
    2416             : void DwarfDebug::emitDebugLineDWO() {
    2417          37 :   assert(useSplitDwarf() && "No split dwarf?");
    2418             :   SplitTypeUnitFileTable.Emit(
    2419             :       *Asm->OutStreamer, MCDwarfLineTableParams(),
    2420          37 :       Asm->getObjFileLowering().getDwarfLineDWOSection());
    2421          37 : }
    2422             : 
    2423             : void DwarfDebug::emitStringOffsetsTableHeaderDWO() {
    2424             :   assert(useSplitDwarf() && "No split dwarf?");
    2425          37 :   InfoHolder.getStringPool().emitStringOffsetsTableHeader(
    2426             :       *Asm, Asm->getObjFileLowering().getDwarfStrOffDWOSection(),
    2427          37 :       InfoHolder.getStringOffsetsStartSym());
    2428          37 : }
    2429             : 
    2430          37 : // Emit the .debug_str.dwo section for separated dwarf. This contains the
    2431             : // string section and is identical in format to traditional .debug_str
    2432          74 : // sections.
    2433          37 : void DwarfDebug::emitDebugStrDWO() {
    2434          37 :   if (useSegmentedStringOffsetsTable())
    2435          37 :     emitStringOffsetsTableHeaderDWO();
    2436             :   assert(useSplitDwarf() && "No split dwarf?");
    2437           6 :   MCSection *OffSec = Asm->getObjFileLowering().getDwarfStrOffDWOSection();
    2438             :   InfoHolder.emitStrings(Asm->getObjFileLowering().getDwarfStrDWOSection(),
    2439          18 :                          OffSec, /* UseRelativeOffsets = */ false);
    2440           6 : }
    2441             : 
    2442           6 : // Emit DWO addresses.
    2443             : void DwarfDebug::emitDebugAddr() {
    2444             :   assert(useSplitDwarf() && "No split dwarf?");
    2445             :   AddrPool.emit(*Asm, Asm->getObjFileLowering().getDwarfAddrSection());
    2446             : }
    2447          37 : 
    2448          37 : MCDwarfDwoLineTable *DwarfDebug::getDwoLineTable(const DwarfCompileUnit &CU) {
    2449           6 :   if (!useSplitDwarf())
    2450             :     return nullptr;
    2451          37 :   const DICompileUnit *DIUnit = CU.getCUNode();
    2452          37 :   SplitTypeUnitFileTable.maybeSetRootFile(
    2453             :       DIUnit->getDirectory(), DIUnit->getFilename(),
    2454          37 :       CU.getMD5AsBytes(DIUnit->getFile()), DIUnit->getSource());
    2455             :   return &SplitTypeUnitFileTable;
    2456             : }
    2457          37 : 
    2458             : uint64_t DwarfDebug::makeTypeSignature(StringRef Identifier) {
    2459          37 :   MD5 Hash;
    2460          37 :   Hash.update(Identifier);
    2461             :   // ... take the least significant 8 bytes and return those. Our MD5
    2462          44 :   // implementation always returns its results in little endian, so we actually
    2463          44 :   // need the "high" word.
    2464             :   MD5::MD5Result Result;
    2465          24 :   Hash.final(Result);
    2466          48 :   return Result.high();
    2467             : }
    2468          24 : 
    2469          24 : void DwarfDebug::addDwarfTypeUnitType(DwarfCompileUnit &CU,
    2470             :                                       StringRef Identifier, DIE &RefDie,
    2471             :                                       const DICompositeType *CTy) {
    2472          44 :   // Fast path if we're building some type units and one has already used the
    2473          44 :   // address pool we know we're going to throw away all this work anyway, so
    2474          44 :   // don't bother building dependent types.
    2475             :   if (!TypeUnitsUnderConstruction.empty() && AddrPool.hasBeenUsed())
    2476             :     return;
    2477             : 
    2478             :   auto Ins = TypeSignatures.insert(std::make_pair(CTy, 0));
    2479          44 :   if (!Ins.second) {
    2480          44 :     CU.addDIETypeSignature(RefDie, Ins.first->second);
    2481             :     return;
    2482             :   }
    2483          54 : 
    2484             :   bool TopLevelType = TypeUnitsUnderConstruction.empty();
    2485             :   AddrPool.resetUsedFlag();
    2486             : 
    2487             :   auto OwnedUnit = llvm::make_unique<DwarfTypeUnit>(CU, Asm, this, &InfoHolder,
    2488             :                                                     getDwoLineTable(CU));
    2489          54 :   DwarfTypeUnit &NewTU = *OwnedUnit;
    2490          17 :   DIE &UnitDie = NewTU.getUnitDie();
    2491             :   TypeUnitsUnderConstruction.emplace_back(std::move(OwnedUnit), CTy);
    2492          53 : 
    2493          53 :   NewTU.addUInt(UnitDie, dwarf::DW_AT_language, dwarf::DW_FORM_data2,
    2494           9 :                 CU.getLanguage());
    2495           9 : 
    2496             :   uint64_t Signature = makeTypeSignature(Identifier);
    2497             :   NewTU.setTypeSignature(Signature);
    2498          44 :   Ins.first->second = Signature;
    2499             : 
    2500             :   if (useSplitDwarf())
    2501          44 :     NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesDWOSection());
    2502          44 :   else {
    2503             :     NewTU.setSection(Asm->getObjFileLowering().getDwarfTypesSection(Signature));
    2504             :     // Non-split type units reuse the compile unit's line table.
    2505          44 :     CU.applyStmtList(UnitDie);
    2506             :   }
    2507          88 : 
    2508          44 :   // Add DW_AT_str_offsets_base to the type unit DIE, but not for split type
    2509             :   // units.
    2510          44 :   if (useSegmentedStringOffsetsTable() && !useSplitDwarf())
    2511             :     NewTU.addStringOffsetsStart();
    2512          44 : 
    2513             :   NewTU.setType(NewTU.createTypeDIE(CTy));
    2514          44 : 
    2515          24 :   if (TopLevelType) {
    2516             :     auto TypeUnitsToAdd = std::move(TypeUnitsUnderConstruction);
    2517          20 :     TypeUnitsUnderConstruction.clear();
    2518             : 
    2519          20 :     // Types referencing entries in the address table cannot be placed in type
    2520             :     // units.
    2521             :     if (AddrPool.hasBeenUsed()) {
    2522             : 
    2523             :       // Remove all the types built while building this type.
    2524          44 :       // This is pessimistic as some of these types might not be dependent on
    2525           4 :       // the type that used an address.
    2526             :       for (const auto &TU : TypeUnitsToAdd)
    2527          44 :         TypeSignatures.erase(TU.second);
    2528             : 
    2529          44 :       // Construct this type in the CU directly.
    2530          25 :       // This is inefficient because all the dependent types will be rebuilt
    2531          32 :       // from scratch, including building them in type units, discovering that
    2532             :       // they depend on addresses, throwing them out and rebuilding them.
    2533             :       CU.constructTypeDIE(RefDie, cast<DICompositeType>(CTy));
    2534             :       return;
    2535          32 :     }
    2536             : 
    2537             :     // If the type wasn't dependent on fission addresses, finish adding the type
    2538             :     // and all its dependent types.
    2539             :     for (auto &TU : TypeUnitsToAdd) {
    2540          18 :       InfoHolder.computeSizeAndOffsetsForUnit(TU.first.get());
    2541          11 :       InfoHolder.emitUnit(TU.first.get(), useSplitDwarf());
    2542             :     }
    2543             :   }
    2544             :   CU.addDIETypeSignature(RefDie, Signature);
    2545             : }
    2546             : 
    2547           7 : // Add the Name along with its companion DIE to the appropriate accelerator
    2548           7 : // table (for AccelTableKind::Dwarf it's always AccelDebugNames, for
    2549             : // AccelTableKind::Apple, we use the table we got as an argument). If
    2550             : // accelerator tables are disabled, this function does nothing.
    2551             : template <typename DataT>
    2552             : void DwarfDebug::addAccelNameImpl(const DICompileUnit &CU,
    2553          58 :                                   AccelTable<DataT> &AppleAccel, StringRef Name,
    2554          33 :                                   const DIE &Die) {
    2555          66 :   if (getAccelTableKind() == AccelTableKind::None)
    2556             :     return;
    2557             : 
    2558          37 :   if (getAccelTableKind() != AccelTableKind::Apple &&
    2559             :       CU.getNameTableKind() == DICompileUnit::DebugNameTableKind::None)
    2560             :     return;
    2561             : 
    2562             :   DwarfFile &Holder = useSplitDwarf() ? SkeletonHolder : InfoHolder;
    2563             :   DwarfStringPoolEntryRef Ref = Holder.getStringPool().getEntry(*Asm, Name);
    2564             : 
    2565             :   switch (getAccelTableKind()) {
    2566       21533 :   case AccelTableKind::Apple:
    2567             :     AppleAccel.addName(Ref, Die);
    2568             :     break;
    2569       21533 :   case AccelTableKind::Dwarf:
    2570       20049 :     AccelDebugNames.addName(Ref, Die);
    2571             :     break;
    2572        1485 :   case AccelTableKind::Default:
    2573         433 :     llvm_unreachable("Default should have already been resolved.");
    2574             :   case AccelTableKind::None:
    2575             :     llvm_unreachable("None handled above");
    2576        1484 :   }
    2577        1484 : }
    2578             : 
    2579        1484 : void DwarfDebug::addAccelName(const DICompileUnit &CU, StringRef Name,
    2580        1052 :                               const DIE &Die) {
    2581        1052 :   addAccelNameImpl(CU, AccelNames, Name, Die);
    2582        1052 : }
    2583         432 : 
    2584         432 : void DwarfDebug::addAccelObjC(const DICompileUnit &CU, StringRef Name,
    2585         432 :                               const DIE &Die) {
    2586             :   // ObjC names go only into the Apple accelerator tables.
    2587             :   if (getAccelTableKind() == AccelTableKind::Apple)
    2588             :     addAccelNameImpl(CU, AccelObjC, Name, Die);
    2589             : }
    2590             : 
    2591             : void DwarfDebug::addAccelNamespace(const DICompileUnit &CU, StringRef Name,
    2592       18524 :                                    const DIE &Die) {
    2593             :   addAccelNameImpl(CU, AccelNamespace, Name, Die);
    2594             : }
    2595       18524 : 
    2596       17986 : void DwarfDebug::addAccelType(const DICompileUnit &CU, StringRef Name,
    2597             :                               const DIE &Die, char Flags) {
    2598         539 :   addAccelNameImpl(CU, AccelTypes, Name, Die);
    2599          60 : }
    2600             : 
    2601             : uint16_t DwarfDebug::getDwarfVersion() const {
    2602         538 :   return Asm->OutStreamer->getContext().getDwarfVersion();
    2603         538 : }

Generated by: LCOV version 1.13