137c52310SDavid Blaikie #include "DwarfCompileUnit.h"
237c52310SDavid Blaikie 
3cda2aa82SDavid Blaikie #include "llvm/CodeGen/MachineFunction.h"
437c52310SDavid Blaikie #include "llvm/IR/DataLayout.h"
537c52310SDavid Blaikie #include "llvm/IR/GlobalValue.h"
637c52310SDavid Blaikie #include "llvm/IR/GlobalVariable.h"
737c52310SDavid Blaikie #include "llvm/IR/Instruction.h"
837c52310SDavid Blaikie #include "llvm/MC/MCAsmInfo.h"
937c52310SDavid Blaikie #include "llvm/MC/MCStreamer.h"
10ee7df553SDavid Blaikie #include "llvm/Target/TargetFrameLowering.h"
1137c52310SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
12cda2aa82SDavid Blaikie #include "llvm/Target/TargetMachine.h"
13cda2aa82SDavid Blaikie #include "llvm/Target/TargetSubtargetInfo.h"
14cda2aa82SDavid Blaikie #include "llvm/Target/TargetRegisterInfo.h"
1537c52310SDavid Blaikie 
1637c52310SDavid Blaikie namespace llvm {
1737c52310SDavid Blaikie 
1837c52310SDavid Blaikie DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
1937c52310SDavid Blaikie                                    AsmPrinter *A, DwarfDebug *DW,
2037c52310SDavid Blaikie                                    DwarfFile *DWU)
2137c52310SDavid Blaikie     : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
2237c52310SDavid Blaikie   insertDIE(Node, &getUnitDie());
2337c52310SDavid Blaikie }
2437c52310SDavid Blaikie 
2537c52310SDavid Blaikie /// addLabelAddress - Add a dwarf label attribute data and value using
2637c52310SDavid Blaikie /// DW_FORM_addr or DW_FORM_GNU_addr_index.
2737c52310SDavid Blaikie ///
2837c52310SDavid Blaikie void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
2937c52310SDavid Blaikie                                        const MCSymbol *Label) {
3037c52310SDavid Blaikie 
3137c52310SDavid Blaikie   // Don't use the address pool in non-fission or in the skeleton unit itself.
3237c52310SDavid Blaikie   // FIXME: Once GDB supports this, it's probably worthwhile using the address
3337c52310SDavid Blaikie   // pool from the skeleton - maybe even in non-fission (possibly fewer
3437c52310SDavid Blaikie   // relocations by sharing them in the pool, but we have other ideas about how
3537c52310SDavid Blaikie   // to reduce the number of relocations as well/instead).
3637c52310SDavid Blaikie   if (!DD->useSplitDwarf() || !Skeleton)
3737c52310SDavid Blaikie     return addLocalLabelAddress(Die, Attribute, Label);
3837c52310SDavid Blaikie 
3937c52310SDavid Blaikie   if (Label)
4037c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
4137c52310SDavid Blaikie 
4237c52310SDavid Blaikie   unsigned idx = DD->getAddressPool().getIndex(Label);
4337c52310SDavid Blaikie   DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
4437c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
4537c52310SDavid Blaikie }
4637c52310SDavid Blaikie 
4737c52310SDavid Blaikie void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
4837c52310SDavid Blaikie                                             dwarf::Attribute Attribute,
4937c52310SDavid Blaikie                                             const MCSymbol *Label) {
5037c52310SDavid Blaikie   if (Label)
5137c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
5237c52310SDavid Blaikie 
5337c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_addr,
5437c52310SDavid Blaikie                Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
5537c52310SDavid Blaikie                      : new (DIEValueAllocator) DIEInteger(0));
5637c52310SDavid Blaikie }
5737c52310SDavid Blaikie 
588945219dSDavid Blaikie unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
598945219dSDavid Blaikie                                                StringRef DirName) {
6037c52310SDavid Blaikie   // If we print assembly, we can't separate .file entries according to
6137c52310SDavid Blaikie   // compile units. Thus all files will belong to the default compile unit.
6237c52310SDavid Blaikie 
6337c52310SDavid Blaikie   // FIXME: add a better feature test than hasRawTextSupport. Even better,
6437c52310SDavid Blaikie   // extend .file to support this.
6537c52310SDavid Blaikie   return Asm->OutStreamer.EmitDwarfFileDirective(
6637c52310SDavid Blaikie       0, DirName, FileName,
6737c52310SDavid Blaikie       Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
6837c52310SDavid Blaikie }
6937c52310SDavid Blaikie 
7037c52310SDavid Blaikie // Return const expression if value is a GEP to access merged global
7137c52310SDavid Blaikie // constant. e.g.
7237c52310SDavid Blaikie // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
7337c52310SDavid Blaikie static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
7437c52310SDavid Blaikie   const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
7537c52310SDavid Blaikie   if (!CE || CE->getNumOperands() != 3 ||
7637c52310SDavid Blaikie       CE->getOpcode() != Instruction::GetElementPtr)
7737c52310SDavid Blaikie     return nullptr;
7837c52310SDavid Blaikie 
7937c52310SDavid Blaikie   // First operand points to a global struct.
8037c52310SDavid Blaikie   Value *Ptr = CE->getOperand(0);
8137c52310SDavid Blaikie   if (!isa<GlobalValue>(Ptr) ||
8237c52310SDavid Blaikie       !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
8337c52310SDavid Blaikie     return nullptr;
8437c52310SDavid Blaikie 
8537c52310SDavid Blaikie   // Second operand is zero.
8637c52310SDavid Blaikie   const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
8737c52310SDavid Blaikie   if (!CI || !CI->isZero())
8837c52310SDavid Blaikie     return nullptr;
8937c52310SDavid Blaikie 
9037c52310SDavid Blaikie   // Third operand is offset.
9137c52310SDavid Blaikie   if (!isa<ConstantInt>(CE->getOperand(2)))
9237c52310SDavid Blaikie     return nullptr;
9337c52310SDavid Blaikie 
9437c52310SDavid Blaikie   return CE;
9537c52310SDavid Blaikie }
9637c52310SDavid Blaikie 
9737c52310SDavid Blaikie /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
9837c52310SDavid Blaikie DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) {
9937c52310SDavid Blaikie   // Check for pre-existence.
10037c52310SDavid Blaikie   if (DIE *Die = getDIE(GV))
10137c52310SDavid Blaikie     return Die;
10237c52310SDavid Blaikie 
10337c52310SDavid Blaikie   assert(GV.isGlobalVariable());
10437c52310SDavid Blaikie 
10537c52310SDavid Blaikie   DIScope GVContext = GV.getContext();
10637c52310SDavid Blaikie   DIType GTy = DD->resolve(GV.getType());
10737c52310SDavid Blaikie 
10837c52310SDavid Blaikie   // Construct the context before querying for the existence of the DIE in
10937c52310SDavid Blaikie   // case such construction creates the DIE.
11037c52310SDavid Blaikie   DIE *ContextDIE = getOrCreateContextDIE(GVContext);
11137c52310SDavid Blaikie 
11237c52310SDavid Blaikie   // Add to map.
11349cfc8caSDavid Blaikie   DIE *VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
11449cfc8caSDavid Blaikie   DIScope DeclContext;
11537c52310SDavid Blaikie 
11649cfc8caSDavid Blaikie   if (DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration()) {
11749cfc8caSDavid Blaikie     DeclContext = resolve(SDMDecl.getContext());
11849cfc8caSDavid Blaikie     assert(SDMDecl.isStaticMember() && "Expected static member decl");
11949cfc8caSDavid Blaikie     assert(GV.isDefinition());
12049cfc8caSDavid Blaikie     // We need the declaration DIE that is in the static member's class.
12149cfc8caSDavid Blaikie     DIE *VariableSpecDIE = getOrCreateStaticMemberDIE(SDMDecl);
12249cfc8caSDavid Blaikie     addDIEEntry(*VariableDIE, dwarf::DW_AT_specification, *VariableSpecDIE);
12349cfc8caSDavid Blaikie   } else {
12449cfc8caSDavid Blaikie     DeclContext = GV.getContext();
12537c52310SDavid Blaikie     // Add name and type.
12637c52310SDavid Blaikie     addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
12737c52310SDavid Blaikie     addType(*VariableDIE, GTy);
12837c52310SDavid Blaikie 
12937c52310SDavid Blaikie     // Add scoping info.
13037c52310SDavid Blaikie     if (!GV.isLocalToUnit())
13137c52310SDavid Blaikie       addFlag(*VariableDIE, dwarf::DW_AT_external);
13237c52310SDavid Blaikie 
13337c52310SDavid Blaikie     // Add line number info.
13437c52310SDavid Blaikie     addSourceLine(*VariableDIE, GV);
13537c52310SDavid Blaikie   }
13637c52310SDavid Blaikie 
13749cfc8caSDavid Blaikie   if (!GV.isDefinition())
13849cfc8caSDavid Blaikie     addFlag(*VariableDIE, dwarf::DW_AT_declaration);
13949cfc8caSDavid Blaikie 
14037c52310SDavid Blaikie   // Add location.
14137c52310SDavid Blaikie   bool addToAccelTable = false;
14237c52310SDavid Blaikie   bool isGlobalVariable = GV.getGlobal() != nullptr;
14337c52310SDavid Blaikie   if (isGlobalVariable) {
14437c52310SDavid Blaikie     addToAccelTable = true;
14537c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
14637c52310SDavid Blaikie     const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
14737c52310SDavid Blaikie     if (GV.getGlobal()->isThreadLocal()) {
14837c52310SDavid Blaikie       // FIXME: Make this work with -gsplit-dwarf.
14937c52310SDavid Blaikie       unsigned PointerSize = Asm->getDataLayout().getPointerSize();
15037c52310SDavid Blaikie       assert((PointerSize == 4 || PointerSize == 8) &&
15137c52310SDavid Blaikie              "Add support for other sizes if necessary");
15237c52310SDavid Blaikie       // Based on GCC's support for TLS:
15337c52310SDavid Blaikie       if (!DD->useSplitDwarf()) {
15437c52310SDavid Blaikie         // 1) Start with a constNu of the appropriate pointer size
15537c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1,
15637c52310SDavid Blaikie                 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
15737c52310SDavid Blaikie         // 2) containing the (relocated) offset of the TLS variable
15837c52310SDavid Blaikie         //    within the module's TLS block.
15937c52310SDavid Blaikie         addExpr(*Loc, dwarf::DW_FORM_udata,
16037c52310SDavid Blaikie                 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
16137c52310SDavid Blaikie       } else {
16237c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
16337c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_udata,
16437c52310SDavid Blaikie                 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
16537c52310SDavid Blaikie       }
16637c52310SDavid Blaikie       // 3) followed by a custom OP to make the debugger do a TLS lookup.
16737c52310SDavid Blaikie       addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
16837c52310SDavid Blaikie     } else {
16937c52310SDavid Blaikie       DD->addArangeLabel(SymbolCU(this, Sym));
17037c52310SDavid Blaikie       addOpAddress(*Loc, Sym);
17137c52310SDavid Blaikie     }
17249cfc8caSDavid Blaikie 
17337c52310SDavid Blaikie     addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
17437c52310SDavid Blaikie     // Add the linkage name.
17537c52310SDavid Blaikie     StringRef LinkageName = GV.getLinkageName();
17637c52310SDavid Blaikie     if (!LinkageName.empty())
17737c52310SDavid Blaikie       // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
17837c52310SDavid Blaikie       // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
17937c52310SDavid Blaikie       // TAG_variable.
18049cfc8caSDavid Blaikie       addString(*VariableDIE,
18137c52310SDavid Blaikie                 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
18237c52310SDavid Blaikie                                            : dwarf::DW_AT_MIPS_linkage_name,
18337c52310SDavid Blaikie                 GlobalValue::getRealLinkageName(LinkageName));
18437c52310SDavid Blaikie   } else if (const ConstantInt *CI =
18537c52310SDavid Blaikie                  dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
18637c52310SDavid Blaikie     addConstantValue(*VariableDIE, CI, GTy);
18737c52310SDavid Blaikie   } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) {
18837c52310SDavid Blaikie     addToAccelTable = true;
18937c52310SDavid Blaikie     // GV is a merged global.
19037c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
19137c52310SDavid Blaikie     Value *Ptr = CE->getOperand(0);
19237c52310SDavid Blaikie     MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
19337c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Sym));
19437c52310SDavid Blaikie     addOpAddress(*Loc, Sym);
19537c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
19637c52310SDavid Blaikie     SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
19737c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_udata,
19837c52310SDavid Blaikie             Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
19937c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
20037c52310SDavid Blaikie     addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
20137c52310SDavid Blaikie   }
20237c52310SDavid Blaikie 
20337c52310SDavid Blaikie   if (addToAccelTable) {
20449cfc8caSDavid Blaikie     DD->addAccelName(GV.getName(), *VariableDIE);
20537c52310SDavid Blaikie 
20637c52310SDavid Blaikie     // If the linkage name is different than the name, go ahead and output
20737c52310SDavid Blaikie     // that as well into the name table.
20837c52310SDavid Blaikie     if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
20949cfc8caSDavid Blaikie       DD->addAccelName(GV.getLinkageName(), *VariableDIE);
21037c52310SDavid Blaikie   }
21137c52310SDavid Blaikie 
21249cfc8caSDavid Blaikie   addGlobalName(GV.getName(), *VariableDIE, DeclContext);
21349cfc8caSDavid Blaikie   return VariableDIE;
21437c52310SDavid Blaikie }
21537c52310SDavid Blaikie 
21637c52310SDavid Blaikie void DwarfCompileUnit::addRange(RangeSpan Range) {
21737c52310SDavid Blaikie   bool SameAsPrevCU = this == DD->getPrevCU();
21837c52310SDavid Blaikie   DD->setPrevCU(this);
21937c52310SDavid Blaikie   // If we have no current ranges just add the range and return, otherwise,
22037c52310SDavid Blaikie   // check the current section and CU against the previous section and CU we
22137c52310SDavid Blaikie   // emitted into and the subprogram was contained within. If these are the
22237c52310SDavid Blaikie   // same then extend our current range, otherwise add this as a new range.
22337c52310SDavid Blaikie   if (CURanges.empty() || !SameAsPrevCU ||
22437c52310SDavid Blaikie       (&CURanges.back().getEnd()->getSection() !=
22537c52310SDavid Blaikie        &Range.getEnd()->getSection())) {
22637c52310SDavid Blaikie     CURanges.push_back(Range);
22737c52310SDavid Blaikie     return;
22837c52310SDavid Blaikie   }
22937c52310SDavid Blaikie 
23037c52310SDavid Blaikie   CURanges.back().setEnd(Range.getEnd());
23137c52310SDavid Blaikie }
23237c52310SDavid Blaikie 
2336c0ee4ecSDavid Blaikie void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
2346c0ee4ecSDavid Blaikie                                        const MCSymbol *Label,
2356c0ee4ecSDavid Blaikie                                        const MCSymbol *Sec) {
2366c0ee4ecSDavid Blaikie   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
2376c0ee4ecSDavid Blaikie     addLabel(Die, Attribute,
2386c0ee4ecSDavid Blaikie              DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
2396c0ee4ecSDavid Blaikie                                         : dwarf::DW_FORM_data4,
2406c0ee4ecSDavid Blaikie              Label);
2416c0ee4ecSDavid Blaikie   else
2426c0ee4ecSDavid Blaikie     addSectionDelta(Die, Attribute, Label, Sec);
2436c0ee4ecSDavid Blaikie }
2446c0ee4ecSDavid Blaikie 
24537c52310SDavid Blaikie void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
24637c52310SDavid Blaikie   // Define start line table label for each Compile Unit.
24737c52310SDavid Blaikie   MCSymbol *LineTableStartSym =
24837c52310SDavid Blaikie       Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
24937c52310SDavid Blaikie 
25037c52310SDavid Blaikie   stmtListIndex = UnitDie.getValues().size();
25137c52310SDavid Blaikie 
25237c52310SDavid Blaikie   // DW_AT_stmt_list is a offset of line number information for this
25337c52310SDavid Blaikie   // compile unit in debug_line section. For split dwarf this is
25437c52310SDavid Blaikie   // left in the skeleton CU and so not included.
25537c52310SDavid Blaikie   // The line table entries are not always emitted in assembly, so it
25637c52310SDavid Blaikie   // is not okay to use line_table_start here.
2576c0ee4ecSDavid Blaikie   addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
25837c52310SDavid Blaikie                   DwarfLineSectionSym);
25937c52310SDavid Blaikie }
26037c52310SDavid Blaikie 
26137c52310SDavid Blaikie void DwarfCompileUnit::applyStmtList(DIE &D) {
26237c52310SDavid Blaikie   D.addValue(dwarf::DW_AT_stmt_list,
26337c52310SDavid Blaikie              UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
26437c52310SDavid Blaikie              UnitDie.getValues()[stmtListIndex]);
26537c52310SDavid Blaikie }
26637c52310SDavid Blaikie 
26714499a7dSDavid Blaikie void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
26814499a7dSDavid Blaikie                                        const MCSymbol *End) {
26914499a7dSDavid Blaikie   assert(Begin && "Begin label should not be null!");
27014499a7dSDavid Blaikie   assert(End && "End label should not be null!");
27114499a7dSDavid Blaikie   assert(Begin->isDefined() && "Invalid starting label");
27214499a7dSDavid Blaikie   assert(End->isDefined() && "Invalid end label");
27314499a7dSDavid Blaikie 
27414499a7dSDavid Blaikie   addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
27514499a7dSDavid Blaikie   if (DD->getDwarfVersion() < 4)
27614499a7dSDavid Blaikie     addLabelAddress(D, dwarf::DW_AT_high_pc, End);
27714499a7dSDavid Blaikie   else
27814499a7dSDavid Blaikie     addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
27914499a7dSDavid Blaikie }
28014499a7dSDavid Blaikie 
281cda2aa82SDavid Blaikie // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
282cda2aa82SDavid Blaikie // and DW_AT_high_pc attributes. If there are global variables in this
283cda2aa82SDavid Blaikie // scope then create and insert DIEs for these variables.
284cda2aa82SDavid Blaikie DIE &DwarfCompileUnit::updateSubprogramScopeDIE(DISubprogram SP) {
285cda2aa82SDavid Blaikie   DIE *SPDie = getOrCreateSubprogramDIE(SP);
286cda2aa82SDavid Blaikie 
287cda2aa82SDavid Blaikie   attachLowHighPC(*SPDie, DD->getFunctionBeginSym(), DD->getFunctionEndSym());
288cda2aa82SDavid Blaikie   if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
289cda2aa82SDavid Blaikie           *DD->getCurrentFunction()))
290cda2aa82SDavid Blaikie     addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
291cda2aa82SDavid Blaikie 
292cda2aa82SDavid Blaikie   // Only include DW_AT_frame_base in full debug info
293cda2aa82SDavid Blaikie   if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) {
294cda2aa82SDavid Blaikie     const TargetRegisterInfo *RI =
295cda2aa82SDavid Blaikie         Asm->TM.getSubtargetImpl()->getRegisterInfo();
296cda2aa82SDavid Blaikie     MachineLocation Location(RI->getFrameRegister(*Asm->MF));
297cda2aa82SDavid Blaikie     addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
298cda2aa82SDavid Blaikie   }
299cda2aa82SDavid Blaikie 
300cda2aa82SDavid Blaikie   // Add name to the name table, we do this here because we're guaranteed
301cda2aa82SDavid Blaikie   // to have concrete versions of our DW_TAG_subprogram nodes.
302cda2aa82SDavid Blaikie   DD->addSubprogramNames(SP, *SPDie);
303cda2aa82SDavid Blaikie 
304cda2aa82SDavid Blaikie   return *SPDie;
305cda2aa82SDavid Blaikie }
306cda2aa82SDavid Blaikie 
3079c65b135SDavid Blaikie // Construct a DIE for this scope.
3089c65b135SDavid Blaikie void DwarfCompileUnit::constructScopeDIE(
3099c65b135SDavid Blaikie     LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) {
3109c65b135SDavid Blaikie   if (!Scope || !Scope->getScopeNode())
3119c65b135SDavid Blaikie     return;
3129c65b135SDavid Blaikie 
3139c65b135SDavid Blaikie   DIScope DS(Scope->getScopeNode());
3149c65b135SDavid Blaikie 
3159c65b135SDavid Blaikie   assert((Scope->getInlinedAt() || !DS.isSubprogram()) &&
3169c65b135SDavid Blaikie          "Only handle inlined subprograms here, use "
3179c65b135SDavid Blaikie          "constructSubprogramScopeDIE for non-inlined "
3189c65b135SDavid Blaikie          "subprograms");
3199c65b135SDavid Blaikie 
3209c65b135SDavid Blaikie   SmallVector<std::unique_ptr<DIE>, 8> Children;
3219c65b135SDavid Blaikie 
3229c65b135SDavid Blaikie   // We try to create the scope DIE first, then the children DIEs. This will
3239c65b135SDavid Blaikie   // avoid creating un-used children then removing them later when we find out
3249c65b135SDavid Blaikie   // the scope DIE is null.
3259c65b135SDavid Blaikie   std::unique_ptr<DIE> ScopeDIE;
3269c65b135SDavid Blaikie   if (Scope->getParent() && DS.isSubprogram()) {
32701b48a84SDavid Blaikie     ScopeDIE = constructInlinedScopeDIE(Scope);
3289c65b135SDavid Blaikie     if (!ScopeDIE)
3299c65b135SDavid Blaikie       return;
3309c65b135SDavid Blaikie     // We create children when the scope DIE is not null.
3318b2fdb83SDavid Blaikie     createScopeChildrenDIE(Scope, Children);
3329c65b135SDavid Blaikie   } else {
3339c65b135SDavid Blaikie     // Early exit when we know the scope DIE is going to be null.
3349c65b135SDavid Blaikie     if (DD->isLexicalScopeDIENull(Scope))
3359c65b135SDavid Blaikie       return;
3369c65b135SDavid Blaikie 
3379c65b135SDavid Blaikie     unsigned ChildScopeCount;
3389c65b135SDavid Blaikie 
3399c65b135SDavid Blaikie     // We create children here when we know the scope DIE is not going to be
3409c65b135SDavid Blaikie     // null and the children will be added to the scope DIE.
3418b2fdb83SDavid Blaikie     createScopeChildrenDIE(Scope, Children, &ChildScopeCount);
3429c65b135SDavid Blaikie 
3439c65b135SDavid Blaikie     // There is no need to emit empty lexical block DIE.
3449c65b135SDavid Blaikie     for (const auto &E : DD->findImportedEntitiesForScope(DS))
3459c65b135SDavid Blaikie       Children.push_back(
3469c65b135SDavid Blaikie           constructImportedEntityDIE(DIImportedEntity(E.second)));
3479c65b135SDavid Blaikie     // If there are only other scopes as children, put them directly in the
3489c65b135SDavid Blaikie     // parent instead, as this scope would serve no purpose.
3499c65b135SDavid Blaikie     if (Children.size() == ChildScopeCount) {
3509c65b135SDavid Blaikie       FinalChildren.insert(FinalChildren.end(),
3519c65b135SDavid Blaikie                            std::make_move_iterator(Children.begin()),
3529c65b135SDavid Blaikie                            std::make_move_iterator(Children.end()));
3539c65b135SDavid Blaikie       return;
3549c65b135SDavid Blaikie     }
3550fbf8bdbSDavid Blaikie     ScopeDIE = constructLexicalScopeDIE(Scope);
3569c65b135SDavid Blaikie     assert(ScopeDIE && "Scope DIE should not be null.");
3579c65b135SDavid Blaikie   }
3589c65b135SDavid Blaikie 
3599c65b135SDavid Blaikie   // Add children
3609c65b135SDavid Blaikie   for (auto &I : Children)
3619c65b135SDavid Blaikie     ScopeDIE->addChild(std::move(I));
3629c65b135SDavid Blaikie 
3639c65b135SDavid Blaikie   FinalChildren.push_back(std::move(ScopeDIE));
3649c65b135SDavid Blaikie }
3659c65b135SDavid Blaikie 
366e5feec50SDavid Blaikie void DwarfCompileUnit::addSectionDelta(DIE &Die, dwarf::Attribute Attribute,
367e5feec50SDavid Blaikie                                        const MCSymbol *Hi, const MCSymbol *Lo) {
368e5feec50SDavid Blaikie   DIEValue *Value = new (DIEValueAllocator) DIEDelta(Hi, Lo);
369e5feec50SDavid Blaikie   Die.addValue(Attribute, DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
370e5feec50SDavid Blaikie                                                      : dwarf::DW_FORM_data4,
371e5feec50SDavid Blaikie                Value);
372e5feec50SDavid Blaikie }
373e5feec50SDavid Blaikie 
37452400200SDavid Blaikie void
37552400200SDavid Blaikie DwarfCompileUnit::addScopeRangeList(DIE &ScopeDIE,
37652400200SDavid Blaikie                                     const SmallVectorImpl<InsnRange> &Range) {
37752400200SDavid Blaikie   // Emit offset in .debug_range as a relocatable label. emitDIE will handle
37852400200SDavid Blaikie   // emitting it appropriately.
37952400200SDavid Blaikie   MCSymbol *RangeSym =
38052400200SDavid Blaikie       Asm->GetTempSymbol("debug_ranges", DD->getNextRangeNumber());
38152400200SDavid Blaikie 
38252400200SDavid Blaikie   auto *RangeSectionSym = DD->getRangeSectionSym();
38352400200SDavid Blaikie 
38452400200SDavid Blaikie   // Under fission, ranges are specified by constant offsets relative to the
38552400200SDavid Blaikie   // CU's DW_AT_GNU_ranges_base.
38652400200SDavid Blaikie   if (DD->useSplitDwarf())
38752400200SDavid Blaikie     addSectionDelta(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym);
38852400200SDavid Blaikie   else
38952400200SDavid Blaikie     addSectionLabel(ScopeDIE, dwarf::DW_AT_ranges, RangeSym, RangeSectionSym);
39052400200SDavid Blaikie 
39152400200SDavid Blaikie   RangeSpanList List(RangeSym);
39252400200SDavid Blaikie   for (const InsnRange &R : Range)
39352400200SDavid Blaikie     List.addRange(RangeSpan(DD->getLabelBeforeInsn(R.first),
39452400200SDavid Blaikie                             DD->getLabelAfterInsn(R.second)));
39552400200SDavid Blaikie 
39652400200SDavid Blaikie   // Add the range list to the set of ranges to be emitted.
39752400200SDavid Blaikie   addRangeList(std::move(List));
39852400200SDavid Blaikie }
39952400200SDavid Blaikie 
400de12375cSDavid Blaikie void DwarfCompileUnit::attachRangesOrLowHighPC(
401de12375cSDavid Blaikie     DIE &Die, const SmallVectorImpl<InsnRange> &Ranges) {
402de12375cSDavid Blaikie   assert(!Ranges.empty());
403de12375cSDavid Blaikie   if (Ranges.size() == 1)
404de12375cSDavid Blaikie     attachLowHighPC(Die, DD->getLabelBeforeInsn(Ranges.front().first),
405de12375cSDavid Blaikie                     DD->getLabelAfterInsn(Ranges.front().second));
406de12375cSDavid Blaikie   else
407de12375cSDavid Blaikie     addScopeRangeList(Die, Ranges);
408de12375cSDavid Blaikie }
409de12375cSDavid Blaikie 
41001b48a84SDavid Blaikie // This scope represents inlined body of a function. Construct DIE to
41101b48a84SDavid Blaikie // represent this concrete inlined copy of the function.
41201b48a84SDavid Blaikie std::unique_ptr<DIE>
41301b48a84SDavid Blaikie DwarfCompileUnit::constructInlinedScopeDIE(LexicalScope *Scope) {
41401b48a84SDavid Blaikie   assert(Scope->getScopeNode());
41501b48a84SDavid Blaikie   DIScope DS(Scope->getScopeNode());
41601b48a84SDavid Blaikie   DISubprogram InlinedSP = getDISubprogram(DS);
41701b48a84SDavid Blaikie   // Find the subprogram's DwarfCompileUnit in the SPMap in case the subprogram
41801b48a84SDavid Blaikie   // was inlined from another compile unit.
41901b48a84SDavid Blaikie   DIE *OriginDIE = DD->getAbstractSPDies()[InlinedSP];
42001b48a84SDavid Blaikie   assert(OriginDIE && "Unable to find original DIE for an inlined subprogram.");
42101b48a84SDavid Blaikie 
42201b48a84SDavid Blaikie   auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_inlined_subroutine);
42301b48a84SDavid Blaikie   addDIEEntry(*ScopeDIE, dwarf::DW_AT_abstract_origin, *OriginDIE);
42401b48a84SDavid Blaikie 
42501b48a84SDavid Blaikie   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
42601b48a84SDavid Blaikie 
42701b48a84SDavid Blaikie   // Add the call site information to the DIE.
42801b48a84SDavid Blaikie   DILocation DL(Scope->getInlinedAt());
42901b48a84SDavid Blaikie   addUInt(*ScopeDIE, dwarf::DW_AT_call_file, None,
43001b48a84SDavid Blaikie           getOrCreateSourceID(DL.getFilename(), DL.getDirectory()));
43101b48a84SDavid Blaikie   addUInt(*ScopeDIE, dwarf::DW_AT_call_line, None, DL.getLineNumber());
43201b48a84SDavid Blaikie 
43301b48a84SDavid Blaikie   // Add name to the name table, we do this here because we're guaranteed
43401b48a84SDavid Blaikie   // to have concrete versions of our DW_TAG_inlined_subprogram nodes.
43501b48a84SDavid Blaikie   DD->addSubprogramNames(InlinedSP, *ScopeDIE);
43601b48a84SDavid Blaikie 
43701b48a84SDavid Blaikie   return ScopeDIE;
43801b48a84SDavid Blaikie }
43901b48a84SDavid Blaikie 
4400fbf8bdbSDavid Blaikie // Construct new DW_TAG_lexical_block for this scope and attach
4410fbf8bdbSDavid Blaikie // DW_AT_low_pc/DW_AT_high_pc labels.
4420fbf8bdbSDavid Blaikie std::unique_ptr<DIE>
4430fbf8bdbSDavid Blaikie DwarfCompileUnit::constructLexicalScopeDIE(LexicalScope *Scope) {
4440fbf8bdbSDavid Blaikie   if (DD->isLexicalScopeDIENull(Scope))
4450fbf8bdbSDavid Blaikie     return nullptr;
4460fbf8bdbSDavid Blaikie 
4470fbf8bdbSDavid Blaikie   auto ScopeDIE = make_unique<DIE>(dwarf::DW_TAG_lexical_block);
4480fbf8bdbSDavid Blaikie   if (Scope->isAbstractScope())
4490fbf8bdbSDavid Blaikie     return ScopeDIE;
4500fbf8bdbSDavid Blaikie 
4510fbf8bdbSDavid Blaikie   attachRangesOrLowHighPC(*ScopeDIE, Scope->getRanges());
4520fbf8bdbSDavid Blaikie 
4530fbf8bdbSDavid Blaikie   return ScopeDIE;
4540fbf8bdbSDavid Blaikie }
4550fbf8bdbSDavid Blaikie 
456ee7df553SDavid Blaikie /// constructVariableDIE - Construct a DIE for the given DbgVariable.
457ee7df553SDavid Blaikie std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(DbgVariable &DV,
458ee7df553SDavid Blaikie                                                             bool Abstract) {
459ee7df553SDavid Blaikie   auto D = constructVariableDIEImpl(DV, Abstract);
460ee7df553SDavid Blaikie   DV.setDIE(*D);
461ee7df553SDavid Blaikie   return D;
462ee7df553SDavid Blaikie }
463ee7df553SDavid Blaikie 
464ee7df553SDavid Blaikie std::unique_ptr<DIE>
465ee7df553SDavid Blaikie DwarfCompileUnit::constructVariableDIEImpl(const DbgVariable &DV,
466ee7df553SDavid Blaikie                                            bool Abstract) {
467ee7df553SDavid Blaikie   // Define variable debug information entry.
468ee7df553SDavid Blaikie   auto VariableDie = make_unique<DIE>(DV.getTag());
469ee7df553SDavid Blaikie 
470ee7df553SDavid Blaikie   if (Abstract) {
471ee7df553SDavid Blaikie     applyVariableAttributes(DV, *VariableDie);
472ee7df553SDavid Blaikie     return VariableDie;
473ee7df553SDavid Blaikie   }
474ee7df553SDavid Blaikie 
475ee7df553SDavid Blaikie   // Add variable address.
476ee7df553SDavid Blaikie 
477ee7df553SDavid Blaikie   unsigned Offset = DV.getDotDebugLocOffset();
478ee7df553SDavid Blaikie   if (Offset != ~0U) {
479ee7df553SDavid Blaikie     addLocationList(*VariableDie, dwarf::DW_AT_location, Offset);
480ee7df553SDavid Blaikie     return VariableDie;
481ee7df553SDavid Blaikie   }
482ee7df553SDavid Blaikie 
483ee7df553SDavid Blaikie   // Check if variable is described by a DBG_VALUE instruction.
484ee7df553SDavid Blaikie   if (const MachineInstr *DVInsn = DV.getMInsn()) {
485ee7df553SDavid Blaikie     assert(DVInsn->getNumOperands() == 4);
486ee7df553SDavid Blaikie     if (DVInsn->getOperand(0).isReg()) {
487ee7df553SDavid Blaikie       const MachineOperand RegOp = DVInsn->getOperand(0);
488ee7df553SDavid Blaikie       // If the second operand is an immediate, this is an indirect value.
489ee7df553SDavid Blaikie       if (DVInsn->getOperand(1).isImm()) {
490ee7df553SDavid Blaikie         MachineLocation Location(RegOp.getReg(),
491ee7df553SDavid Blaikie                                  DVInsn->getOperand(1).getImm());
492ee7df553SDavid Blaikie         addVariableAddress(DV, *VariableDie, Location);
493ee7df553SDavid Blaikie       } else if (RegOp.getReg())
494ee7df553SDavid Blaikie         addVariableAddress(DV, *VariableDie, MachineLocation(RegOp.getReg()));
495ee7df553SDavid Blaikie     } else if (DVInsn->getOperand(0).isImm())
496ee7df553SDavid Blaikie       addConstantValue(*VariableDie, DVInsn->getOperand(0), DV.getType());
497ee7df553SDavid Blaikie     else if (DVInsn->getOperand(0).isFPImm())
498ee7df553SDavid Blaikie       addConstantFPValue(*VariableDie, DVInsn->getOperand(0));
499ee7df553SDavid Blaikie     else if (DVInsn->getOperand(0).isCImm())
500ee7df553SDavid Blaikie       addConstantValue(*VariableDie, DVInsn->getOperand(0).getCImm(),
501ee7df553SDavid Blaikie                        DV.getType());
502ee7df553SDavid Blaikie 
503ee7df553SDavid Blaikie     return VariableDie;
504ee7df553SDavid Blaikie   }
505ee7df553SDavid Blaikie 
506ee7df553SDavid Blaikie   // .. else use frame index.
507ee7df553SDavid Blaikie   int FI = DV.getFrameIndex();
508ee7df553SDavid Blaikie   if (FI != ~0) {
509ee7df553SDavid Blaikie     unsigned FrameReg = 0;
510ee7df553SDavid Blaikie     const TargetFrameLowering *TFI =
511ee7df553SDavid Blaikie         Asm->TM.getSubtargetImpl()->getFrameLowering();
512ee7df553SDavid Blaikie     int Offset = TFI->getFrameIndexReference(*Asm->MF, FI, FrameReg);
513ee7df553SDavid Blaikie     MachineLocation Location(FrameReg, Offset);
514ee7df553SDavid Blaikie     addVariableAddress(DV, *VariableDie, Location);
515ee7df553SDavid Blaikie   }
516ee7df553SDavid Blaikie 
517ee7df553SDavid Blaikie   return VariableDie;
518ee7df553SDavid Blaikie }
519ee7df553SDavid Blaikie 
5204a1a44e3SDavid Blaikie std::unique_ptr<DIE> DwarfCompileUnit::constructVariableDIE(
5214a1a44e3SDavid Blaikie     DbgVariable &DV, const LexicalScope &Scope, DIE *&ObjectPointer) {
5224a1a44e3SDavid Blaikie   auto Var = constructVariableDIE(DV, Scope.isAbstractScope());
5234a1a44e3SDavid Blaikie   if (DV.isObjectPointer())
5244a1a44e3SDavid Blaikie     ObjectPointer = Var.get();
5254a1a44e3SDavid Blaikie   return Var;
5264a1a44e3SDavid Blaikie }
5274a1a44e3SDavid Blaikie 
5288b2fdb83SDavid Blaikie DIE *DwarfCompileUnit::createScopeChildrenDIE(
5298b2fdb83SDavid Blaikie     LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &Children,
5308b2fdb83SDavid Blaikie     unsigned *ChildScopeCount) {
5318b2fdb83SDavid Blaikie   DIE *ObjectPointer = nullptr;
5328b2fdb83SDavid Blaikie 
533*80e5b1ebSDavid Blaikie   for (DbgVariable *DV : DU->getScopeVariables().lookup(Scope))
5348b2fdb83SDavid Blaikie     Children.push_back(constructVariableDIE(*DV, *Scope, ObjectPointer));
5358b2fdb83SDavid Blaikie 
5368b2fdb83SDavid Blaikie   unsigned ChildCountWithoutScopes = Children.size();
5378b2fdb83SDavid Blaikie 
5388b2fdb83SDavid Blaikie   for (LexicalScope *LS : Scope->getChildren())
5398b2fdb83SDavid Blaikie     constructScopeDIE(LS, Children);
5408b2fdb83SDavid Blaikie 
5418b2fdb83SDavid Blaikie   if (ChildScopeCount)
5428b2fdb83SDavid Blaikie     *ChildScopeCount = Children.size() - ChildCountWithoutScopes;
5438b2fdb83SDavid Blaikie 
5448b2fdb83SDavid Blaikie   return ObjectPointer;
5458b2fdb83SDavid Blaikie }
5468b2fdb83SDavid Blaikie 
5471d072348SDavid Blaikie void DwarfCompileUnit::constructSubprogramScopeDIE(LexicalScope *Scope) {
5481d072348SDavid Blaikie   assert(Scope && Scope->getScopeNode());
5491d072348SDavid Blaikie   assert(!Scope->getInlinedAt());
5501d072348SDavid Blaikie   assert(!Scope->isAbstractScope());
5511d072348SDavid Blaikie   DISubprogram Sub(Scope->getScopeNode());
5521d072348SDavid Blaikie 
5531d072348SDavid Blaikie   assert(Sub.isSubprogram());
5541d072348SDavid Blaikie 
5551d072348SDavid Blaikie   DD->getProcessedSPNodes().insert(Sub);
5561d072348SDavid Blaikie 
5571d072348SDavid Blaikie   DIE &ScopeDIE = updateSubprogramScopeDIE(Sub);
5581d072348SDavid Blaikie 
5591d072348SDavid Blaikie   // If this is a variadic function, add an unspecified parameter.
5601d072348SDavid Blaikie   DITypeArray FnArgs = Sub.getType().getTypeArray();
5611dd573dbSDavid Blaikie 
5621dd573dbSDavid Blaikie   // Collect lexical scope children first.
5631dd573dbSDavid Blaikie   // ObjectPointer might be a local (non-argument) local variable if it's a
5641dd573dbSDavid Blaikie   // block's synthetic this pointer.
5651dd573dbSDavid Blaikie   if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, ScopeDIE))
5661dd573dbSDavid Blaikie     addDIEEntry(ScopeDIE, dwarf::DW_AT_object_pointer, *ObjectPointer);
5671dd573dbSDavid Blaikie 
5681d072348SDavid Blaikie   // If we have a single element of null, it is a function that returns void.
5691d072348SDavid Blaikie   // If we have more than one elements and the last one is null, it is a
5701d072348SDavid Blaikie   // variadic function.
5711d072348SDavid Blaikie   if (FnArgs.getNumElements() > 1 &&
5721d072348SDavid Blaikie       !FnArgs.getElement(FnArgs.getNumElements() - 1))
5731d072348SDavid Blaikie     ScopeDIE.addChild(make_unique<DIE>(dwarf::DW_TAG_unspecified_parameters));
5741d072348SDavid Blaikie }
5751d072348SDavid Blaikie 
57678b65b6fSDavid Blaikie DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
57778b65b6fSDavid Blaikie                                                  DIE &ScopeDIE) {
57878b65b6fSDavid Blaikie   // We create children when the scope DIE is not null.
57978b65b6fSDavid Blaikie   SmallVector<std::unique_ptr<DIE>, 8> Children;
58078b65b6fSDavid Blaikie   DIE *ObjectPointer = createScopeChildrenDIE(Scope, Children);
58178b65b6fSDavid Blaikie 
58278b65b6fSDavid Blaikie   // Add children
58378b65b6fSDavid Blaikie   for (auto &I : Children)
58478b65b6fSDavid Blaikie     ScopeDIE.addChild(std::move(I));
58578b65b6fSDavid Blaikie 
58678b65b6fSDavid Blaikie   return ObjectPointer;
58778b65b6fSDavid Blaikie }
58878b65b6fSDavid Blaikie 
58958410f24SDavid Blaikie DIE &
59058410f24SDavid Blaikie DwarfCompileUnit::constructAbstractSubprogramScopeDIE(LexicalScope *Scope) {
59158410f24SDavid Blaikie   DISubprogram SP(Scope->getScopeNode());
59258410f24SDavid Blaikie 
59358410f24SDavid Blaikie   DIE *ContextDIE;
59458410f24SDavid Blaikie 
59558410f24SDavid Blaikie   // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
59658410f24SDavid Blaikie   // the important distinction that the DIDescriptor is not associated with the
59758410f24SDavid Blaikie   // DIE (since the DIDescriptor will be associated with the concrete DIE, if
59858410f24SDavid Blaikie   // any). It could be refactored to some common utility function.
59958410f24SDavid Blaikie   if (DISubprogram SPDecl = SP.getFunctionDeclaration()) {
60058410f24SDavid Blaikie     ContextDIE = &getUnitDie();
60158410f24SDavid Blaikie     getOrCreateSubprogramDIE(SPDecl);
60258410f24SDavid Blaikie   } else
60358410f24SDavid Blaikie     ContextDIE = getOrCreateContextDIE(resolve(SP.getContext()));
60458410f24SDavid Blaikie 
60558410f24SDavid Blaikie   // Passing null as the associated DIDescriptor because the abstract definition
60658410f24SDavid Blaikie   // shouldn't be found by lookup.
60758410f24SDavid Blaikie   DIE &AbsDef =
60858410f24SDavid Blaikie       createAndAddDIE(dwarf::DW_TAG_subprogram, *ContextDIE, DIDescriptor());
60958410f24SDavid Blaikie   applySubprogramAttributesToDefinition(SP, AbsDef);
61058410f24SDavid Blaikie 
61158410f24SDavid Blaikie   if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
61258410f24SDavid Blaikie     addUInt(AbsDef, dwarf::DW_AT_inline, None, dwarf::DW_INL_inlined);
61358410f24SDavid Blaikie   if (DIE *ObjectPointer = createAndAddScopeChildren(Scope, AbsDef))
61458410f24SDavid Blaikie     addDIEEntry(AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
61558410f24SDavid Blaikie   return AbsDef;
61658410f24SDavid Blaikie }
61758410f24SDavid Blaikie 
6184191cbceSDavid Blaikie void DwarfCompileUnit::finishSubprogramDefinition(DISubprogram SP) {
6194191cbceSDavid Blaikie   DIE *D = getDIE(SP);
6204191cbceSDavid Blaikie   if (DIE *AbsSPDIE = DD->getAbstractSPDies().lookup(SP)) {
6214191cbceSDavid Blaikie     if (D)
6224191cbceSDavid Blaikie       // If this subprogram has an abstract definition, reference that
6234191cbceSDavid Blaikie       addDIEEntry(*D, dwarf::DW_AT_abstract_origin, *AbsSPDIE);
6244191cbceSDavid Blaikie   } else {
6254191cbceSDavid Blaikie     if (!D && getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly)
6264191cbceSDavid Blaikie       // Lazily construct the subprogram if we didn't see either concrete or
6274191cbceSDavid Blaikie       // inlined versions during codegen. (except in -gmlt ^ where we want
6284191cbceSDavid Blaikie       // to omit these entirely)
6294191cbceSDavid Blaikie       D = getOrCreateSubprogramDIE(SP);
6304191cbceSDavid Blaikie     if (D)
6314191cbceSDavid Blaikie       // And attach the attributes
6324191cbceSDavid Blaikie       applySubprogramAttributesToDefinition(SP, *D);
6334191cbceSDavid Blaikie   }
6344191cbceSDavid Blaikie }
6354191cbceSDavid Blaikie 
63637c52310SDavid Blaikie } // end llvm namespace
637