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"
1037c52310SDavid Blaikie #include "llvm/Target/TargetLoweringObjectFile.h"
11cda2aa82SDavid Blaikie #include "llvm/Target/TargetMachine.h"
12cda2aa82SDavid Blaikie #include "llvm/Target/TargetSubtargetInfo.h"
13cda2aa82SDavid Blaikie #include "llvm/Target/TargetRegisterInfo.h"
1437c52310SDavid Blaikie 
1537c52310SDavid Blaikie namespace llvm {
1637c52310SDavid Blaikie 
1737c52310SDavid Blaikie DwarfCompileUnit::DwarfCompileUnit(unsigned UID, DICompileUnit Node,
1837c52310SDavid Blaikie                                    AsmPrinter *A, DwarfDebug *DW,
1937c52310SDavid Blaikie                                    DwarfFile *DWU)
2037c52310SDavid Blaikie     : DwarfUnit(UID, dwarf::DW_TAG_compile_unit, Node, A, DW, DWU) {
2137c52310SDavid Blaikie   insertDIE(Node, &getUnitDie());
2237c52310SDavid Blaikie }
2337c52310SDavid Blaikie 
2437c52310SDavid Blaikie /// addLabelAddress - Add a dwarf label attribute data and value using
2537c52310SDavid Blaikie /// DW_FORM_addr or DW_FORM_GNU_addr_index.
2637c52310SDavid Blaikie ///
2737c52310SDavid Blaikie void DwarfCompileUnit::addLabelAddress(DIE &Die, dwarf::Attribute Attribute,
2837c52310SDavid Blaikie                                        const MCSymbol *Label) {
2937c52310SDavid Blaikie 
3037c52310SDavid Blaikie   // Don't use the address pool in non-fission or in the skeleton unit itself.
3137c52310SDavid Blaikie   // FIXME: Once GDB supports this, it's probably worthwhile using the address
3237c52310SDavid Blaikie   // pool from the skeleton - maybe even in non-fission (possibly fewer
3337c52310SDavid Blaikie   // relocations by sharing them in the pool, but we have other ideas about how
3437c52310SDavid Blaikie   // to reduce the number of relocations as well/instead).
3537c52310SDavid Blaikie   if (!DD->useSplitDwarf() || !Skeleton)
3637c52310SDavid Blaikie     return addLocalLabelAddress(Die, Attribute, Label);
3737c52310SDavid Blaikie 
3837c52310SDavid Blaikie   if (Label)
3937c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
4037c52310SDavid Blaikie 
4137c52310SDavid Blaikie   unsigned idx = DD->getAddressPool().getIndex(Label);
4237c52310SDavid Blaikie   DIEValue *Value = new (DIEValueAllocator) DIEInteger(idx);
4337c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_GNU_addr_index, Value);
4437c52310SDavid Blaikie }
4537c52310SDavid Blaikie 
4637c52310SDavid Blaikie void DwarfCompileUnit::addLocalLabelAddress(DIE &Die,
4737c52310SDavid Blaikie                                             dwarf::Attribute Attribute,
4837c52310SDavid Blaikie                                             const MCSymbol *Label) {
4937c52310SDavid Blaikie   if (Label)
5037c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Label));
5137c52310SDavid Blaikie 
5237c52310SDavid Blaikie   Die.addValue(Attribute, dwarf::DW_FORM_addr,
5337c52310SDavid Blaikie                Label ? (DIEValue *)new (DIEValueAllocator) DIELabel(Label)
5437c52310SDavid Blaikie                      : new (DIEValueAllocator) DIEInteger(0));
5537c52310SDavid Blaikie }
5637c52310SDavid Blaikie 
578945219dSDavid Blaikie unsigned DwarfCompileUnit::getOrCreateSourceID(StringRef FileName,
588945219dSDavid Blaikie                                                StringRef DirName) {
5937c52310SDavid Blaikie   // If we print assembly, we can't separate .file entries according to
6037c52310SDavid Blaikie   // compile units. Thus all files will belong to the default compile unit.
6137c52310SDavid Blaikie 
6237c52310SDavid Blaikie   // FIXME: add a better feature test than hasRawTextSupport. Even better,
6337c52310SDavid Blaikie   // extend .file to support this.
6437c52310SDavid Blaikie   return Asm->OutStreamer.EmitDwarfFileDirective(
6537c52310SDavid Blaikie       0, DirName, FileName,
6637c52310SDavid Blaikie       Asm->OutStreamer.hasRawTextSupport() ? 0 : getUniqueID());
6737c52310SDavid Blaikie }
6837c52310SDavid Blaikie 
6937c52310SDavid Blaikie // Return const expression if value is a GEP to access merged global
7037c52310SDavid Blaikie // constant. e.g.
7137c52310SDavid Blaikie // i8* getelementptr ({ i8, i8, i8, i8 }* @_MergedGlobals, i32 0, i32 0)
7237c52310SDavid Blaikie static const ConstantExpr *getMergedGlobalExpr(const Value *V) {
7337c52310SDavid Blaikie   const ConstantExpr *CE = dyn_cast_or_null<ConstantExpr>(V);
7437c52310SDavid Blaikie   if (!CE || CE->getNumOperands() != 3 ||
7537c52310SDavid Blaikie       CE->getOpcode() != Instruction::GetElementPtr)
7637c52310SDavid Blaikie     return nullptr;
7737c52310SDavid Blaikie 
7837c52310SDavid Blaikie   // First operand points to a global struct.
7937c52310SDavid Blaikie   Value *Ptr = CE->getOperand(0);
8037c52310SDavid Blaikie   if (!isa<GlobalValue>(Ptr) ||
8137c52310SDavid Blaikie       !isa<StructType>(cast<PointerType>(Ptr->getType())->getElementType()))
8237c52310SDavid Blaikie     return nullptr;
8337c52310SDavid Blaikie 
8437c52310SDavid Blaikie   // Second operand is zero.
8537c52310SDavid Blaikie   const ConstantInt *CI = dyn_cast_or_null<ConstantInt>(CE->getOperand(1));
8637c52310SDavid Blaikie   if (!CI || !CI->isZero())
8737c52310SDavid Blaikie     return nullptr;
8837c52310SDavid Blaikie 
8937c52310SDavid Blaikie   // Third operand is offset.
9037c52310SDavid Blaikie   if (!isa<ConstantInt>(CE->getOperand(2)))
9137c52310SDavid Blaikie     return nullptr;
9237c52310SDavid Blaikie 
9337c52310SDavid Blaikie   return CE;
9437c52310SDavid Blaikie }
9537c52310SDavid Blaikie 
9637c52310SDavid Blaikie /// getOrCreateGlobalVariableDIE - get or create global variable DIE.
9737c52310SDavid Blaikie DIE *DwarfCompileUnit::getOrCreateGlobalVariableDIE(DIGlobalVariable GV) {
9837c52310SDavid Blaikie   // Check for pre-existence.
9937c52310SDavid Blaikie   if (DIE *Die = getDIE(GV))
10037c52310SDavid Blaikie     return Die;
10137c52310SDavid Blaikie 
10237c52310SDavid Blaikie   assert(GV.isGlobalVariable());
10337c52310SDavid Blaikie 
10437c52310SDavid Blaikie   DIScope GVContext = GV.getContext();
10537c52310SDavid Blaikie   DIType GTy = DD->resolve(GV.getType());
10637c52310SDavid Blaikie 
10737c52310SDavid Blaikie   // If this is a static data member definition, some attributes belong
10837c52310SDavid Blaikie   // to the declaration DIE.
10937c52310SDavid Blaikie   DIE *VariableDIE = nullptr;
11037c52310SDavid Blaikie   bool IsStaticMember = false;
11137c52310SDavid Blaikie   DIDerivedType SDMDecl = GV.getStaticDataMemberDeclaration();
11237c52310SDavid Blaikie   if (SDMDecl.Verify()) {
11337c52310SDavid Blaikie     assert(SDMDecl.isStaticMember() && "Expected static member decl");
11437c52310SDavid Blaikie     // We need the declaration DIE that is in the static member's class.
11537c52310SDavid Blaikie     VariableDIE = getOrCreateStaticMemberDIE(SDMDecl);
11637c52310SDavid Blaikie     IsStaticMember = true;
11737c52310SDavid Blaikie   }
11837c52310SDavid Blaikie 
11937c52310SDavid Blaikie   // If this is not a static data member definition, create the variable
12037c52310SDavid Blaikie   // DIE and add the initial set of attributes to it.
12137c52310SDavid Blaikie   if (!VariableDIE) {
12237c52310SDavid Blaikie     // Construct the context before querying for the existence of the DIE in
12337c52310SDavid Blaikie     // case such construction creates the DIE.
12437c52310SDavid Blaikie     DIE *ContextDIE = getOrCreateContextDIE(GVContext);
12537c52310SDavid Blaikie 
12637c52310SDavid Blaikie     // Add to map.
12737c52310SDavid Blaikie     VariableDIE = &createAndAddDIE(GV.getTag(), *ContextDIE, GV);
12837c52310SDavid Blaikie 
12937c52310SDavid Blaikie     // Add name and type.
13037c52310SDavid Blaikie     addString(*VariableDIE, dwarf::DW_AT_name, GV.getDisplayName());
13137c52310SDavid Blaikie     addType(*VariableDIE, GTy);
13237c52310SDavid Blaikie 
13337c52310SDavid Blaikie     // Add scoping info.
13437c52310SDavid Blaikie     if (!GV.isLocalToUnit())
13537c52310SDavid Blaikie       addFlag(*VariableDIE, dwarf::DW_AT_external);
13637c52310SDavid Blaikie 
13737c52310SDavid Blaikie     // Add line number info.
13837c52310SDavid Blaikie     addSourceLine(*VariableDIE, GV);
13937c52310SDavid Blaikie   }
14037c52310SDavid Blaikie 
14137c52310SDavid Blaikie   // Add location.
14237c52310SDavid Blaikie   bool addToAccelTable = false;
14337c52310SDavid Blaikie   DIE *VariableSpecDIE = nullptr;
14437c52310SDavid Blaikie   bool isGlobalVariable = GV.getGlobal() != nullptr;
14537c52310SDavid Blaikie   if (isGlobalVariable) {
14637c52310SDavid Blaikie     addToAccelTable = true;
14737c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
14837c52310SDavid Blaikie     const MCSymbol *Sym = Asm->getSymbol(GV.getGlobal());
14937c52310SDavid Blaikie     if (GV.getGlobal()->isThreadLocal()) {
15037c52310SDavid Blaikie       // FIXME: Make this work with -gsplit-dwarf.
15137c52310SDavid Blaikie       unsigned PointerSize = Asm->getDataLayout().getPointerSize();
15237c52310SDavid Blaikie       assert((PointerSize == 4 || PointerSize == 8) &&
15337c52310SDavid Blaikie              "Add support for other sizes if necessary");
15437c52310SDavid Blaikie       // Based on GCC's support for TLS:
15537c52310SDavid Blaikie       if (!DD->useSplitDwarf()) {
15637c52310SDavid Blaikie         // 1) Start with a constNu of the appropriate pointer size
15737c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1,
15837c52310SDavid Blaikie                 PointerSize == 4 ? dwarf::DW_OP_const4u : dwarf::DW_OP_const8u);
15937c52310SDavid Blaikie         // 2) containing the (relocated) offset of the TLS variable
16037c52310SDavid Blaikie         //    within the module's TLS block.
16137c52310SDavid Blaikie         addExpr(*Loc, dwarf::DW_FORM_udata,
16237c52310SDavid Blaikie                 Asm->getObjFileLowering().getDebugThreadLocalSymbol(Sym));
16337c52310SDavid Blaikie       } else {
16437c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_const_index);
16537c52310SDavid Blaikie         addUInt(*Loc, dwarf::DW_FORM_udata,
16637c52310SDavid Blaikie                 DD->getAddressPool().getIndex(Sym, /* TLS */ true));
16737c52310SDavid Blaikie       }
16837c52310SDavid Blaikie       // 3) followed by a custom OP to make the debugger do a TLS lookup.
16937c52310SDavid Blaikie       addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_GNU_push_tls_address);
17037c52310SDavid Blaikie     } else {
17137c52310SDavid Blaikie       DD->addArangeLabel(SymbolCU(this, Sym));
17237c52310SDavid Blaikie       addOpAddress(*Loc, Sym);
17337c52310SDavid Blaikie     }
17437c52310SDavid Blaikie     // A static member's declaration is already flagged as such.
17537c52310SDavid Blaikie     if (!SDMDecl.Verify() && !GV.isDefinition())
17637c52310SDavid Blaikie       addFlag(*VariableDIE, dwarf::DW_AT_declaration);
17737c52310SDavid Blaikie     // Do not create specification DIE if context is either compile unit
17837c52310SDavid Blaikie     // or a subprogram.
17937c52310SDavid Blaikie     if (GVContext && GV.isDefinition() && !GVContext.isCompileUnit() &&
18037c52310SDavid Blaikie         !GVContext.isFile() && !DD->isSubprogramContext(GVContext)) {
18137c52310SDavid Blaikie       // Create specification DIE.
18237c52310SDavid Blaikie       VariableSpecDIE = &createAndAddDIE(dwarf::DW_TAG_variable, UnitDie);
18337c52310SDavid Blaikie       addDIEEntry(*VariableSpecDIE, dwarf::DW_AT_specification, *VariableDIE);
18437c52310SDavid Blaikie       addBlock(*VariableSpecDIE, dwarf::DW_AT_location, Loc);
18537c52310SDavid Blaikie       // A static member's declaration is already flagged as such.
18637c52310SDavid Blaikie       if (!SDMDecl.Verify())
18737c52310SDavid Blaikie         addFlag(*VariableDIE, dwarf::DW_AT_declaration);
18837c52310SDavid Blaikie     } else {
18937c52310SDavid Blaikie       addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
19037c52310SDavid Blaikie     }
19137c52310SDavid Blaikie     // Add the linkage name.
19237c52310SDavid Blaikie     StringRef LinkageName = GV.getLinkageName();
19337c52310SDavid Blaikie     if (!LinkageName.empty())
19437c52310SDavid Blaikie       // From DWARF4: DIEs to which DW_AT_linkage_name may apply include:
19537c52310SDavid Blaikie       // TAG_common_block, TAG_constant, TAG_entry_point, TAG_subprogram and
19637c52310SDavid Blaikie       // TAG_variable.
19737c52310SDavid Blaikie       addString(IsStaticMember && VariableSpecDIE ? *VariableSpecDIE
19837c52310SDavid Blaikie                                                   : *VariableDIE,
19937c52310SDavid Blaikie                 DD->getDwarfVersion() >= 4 ? dwarf::DW_AT_linkage_name
20037c52310SDavid Blaikie                                            : dwarf::DW_AT_MIPS_linkage_name,
20137c52310SDavid Blaikie                 GlobalValue::getRealLinkageName(LinkageName));
20237c52310SDavid Blaikie   } else if (const ConstantInt *CI =
20337c52310SDavid Blaikie                  dyn_cast_or_null<ConstantInt>(GV.getConstant())) {
20437c52310SDavid Blaikie     // AT_const_value was added when the static member was created. To avoid
20537c52310SDavid Blaikie     // emitting AT_const_value multiple times, we only add AT_const_value when
20637c52310SDavid Blaikie     // it is not a static member.
20737c52310SDavid Blaikie     if (!IsStaticMember)
20837c52310SDavid Blaikie       addConstantValue(*VariableDIE, CI, GTy);
20937c52310SDavid Blaikie   } else if (const ConstantExpr *CE = getMergedGlobalExpr(GV.getConstant())) {
21037c52310SDavid Blaikie     addToAccelTable = true;
21137c52310SDavid Blaikie     // GV is a merged global.
21237c52310SDavid Blaikie     DIELoc *Loc = new (DIEValueAllocator) DIELoc();
21337c52310SDavid Blaikie     Value *Ptr = CE->getOperand(0);
21437c52310SDavid Blaikie     MCSymbol *Sym = Asm->getSymbol(cast<GlobalValue>(Ptr));
21537c52310SDavid Blaikie     DD->addArangeLabel(SymbolCU(this, Sym));
21637c52310SDavid Blaikie     addOpAddress(*Loc, Sym);
21737c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_constu);
21837c52310SDavid Blaikie     SmallVector<Value *, 3> Idx(CE->op_begin() + 1, CE->op_end());
21937c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_udata,
22037c52310SDavid Blaikie             Asm->getDataLayout().getIndexedOffset(Ptr->getType(), Idx));
22137c52310SDavid Blaikie     addUInt(*Loc, dwarf::DW_FORM_data1, dwarf::DW_OP_plus);
22237c52310SDavid Blaikie     addBlock(*VariableDIE, dwarf::DW_AT_location, Loc);
22337c52310SDavid Blaikie   }
22437c52310SDavid Blaikie 
22537c52310SDavid Blaikie   DIE *ResultDIE = VariableSpecDIE ? VariableSpecDIE : VariableDIE;
22637c52310SDavid Blaikie 
22737c52310SDavid Blaikie   if (addToAccelTable) {
22837c52310SDavid Blaikie     DD->addAccelName(GV.getName(), *ResultDIE);
22937c52310SDavid Blaikie 
23037c52310SDavid Blaikie     // If the linkage name is different than the name, go ahead and output
23137c52310SDavid Blaikie     // that as well into the name table.
23237c52310SDavid Blaikie     if (GV.getLinkageName() != "" && GV.getName() != GV.getLinkageName())
23337c52310SDavid Blaikie       DD->addAccelName(GV.getLinkageName(), *ResultDIE);
23437c52310SDavid Blaikie   }
23537c52310SDavid Blaikie 
23637c52310SDavid Blaikie   addGlobalName(GV.getName(), *ResultDIE, GV.getContext());
23737c52310SDavid Blaikie   return ResultDIE;
23837c52310SDavid Blaikie }
23937c52310SDavid Blaikie 
24037c52310SDavid Blaikie void DwarfCompileUnit::addRange(RangeSpan Range) {
24137c52310SDavid Blaikie   bool SameAsPrevCU = this == DD->getPrevCU();
24237c52310SDavid Blaikie   DD->setPrevCU(this);
24337c52310SDavid Blaikie   // If we have no current ranges just add the range and return, otherwise,
24437c52310SDavid Blaikie   // check the current section and CU against the previous section and CU we
24537c52310SDavid Blaikie   // emitted into and the subprogram was contained within. If these are the
24637c52310SDavid Blaikie   // same then extend our current range, otherwise add this as a new range.
24737c52310SDavid Blaikie   if (CURanges.empty() || !SameAsPrevCU ||
24837c52310SDavid Blaikie       (&CURanges.back().getEnd()->getSection() !=
24937c52310SDavid Blaikie        &Range.getEnd()->getSection())) {
25037c52310SDavid Blaikie     CURanges.push_back(Range);
25137c52310SDavid Blaikie     return;
25237c52310SDavid Blaikie   }
25337c52310SDavid Blaikie 
25437c52310SDavid Blaikie   CURanges.back().setEnd(Range.getEnd());
25537c52310SDavid Blaikie }
25637c52310SDavid Blaikie 
257*6c0ee4ecSDavid Blaikie void DwarfCompileUnit::addSectionLabel(DIE &Die, dwarf::Attribute Attribute,
258*6c0ee4ecSDavid Blaikie                                        const MCSymbol *Label,
259*6c0ee4ecSDavid Blaikie                                        const MCSymbol *Sec) {
260*6c0ee4ecSDavid Blaikie   if (Asm->MAI->doesDwarfUseRelocationsAcrossSections())
261*6c0ee4ecSDavid Blaikie     addLabel(Die, Attribute,
262*6c0ee4ecSDavid Blaikie              DD->getDwarfVersion() >= 4 ? dwarf::DW_FORM_sec_offset
263*6c0ee4ecSDavid Blaikie                                         : dwarf::DW_FORM_data4,
264*6c0ee4ecSDavid Blaikie              Label);
265*6c0ee4ecSDavid Blaikie   else
266*6c0ee4ecSDavid Blaikie     addSectionDelta(Die, Attribute, Label, Sec);
267*6c0ee4ecSDavid Blaikie }
268*6c0ee4ecSDavid Blaikie 
26937c52310SDavid Blaikie void DwarfCompileUnit::initStmtList(MCSymbol *DwarfLineSectionSym) {
27037c52310SDavid Blaikie   // Define start line table label for each Compile Unit.
27137c52310SDavid Blaikie   MCSymbol *LineTableStartSym =
27237c52310SDavid Blaikie       Asm->OutStreamer.getDwarfLineTableSymbol(getUniqueID());
27337c52310SDavid Blaikie 
27437c52310SDavid Blaikie   stmtListIndex = UnitDie.getValues().size();
27537c52310SDavid Blaikie 
27637c52310SDavid Blaikie   // DW_AT_stmt_list is a offset of line number information for this
27737c52310SDavid Blaikie   // compile unit in debug_line section. For split dwarf this is
27837c52310SDavid Blaikie   // left in the skeleton CU and so not included.
27937c52310SDavid Blaikie   // The line table entries are not always emitted in assembly, so it
28037c52310SDavid Blaikie   // is not okay to use line_table_start here.
281*6c0ee4ecSDavid Blaikie   addSectionLabel(UnitDie, dwarf::DW_AT_stmt_list, LineTableStartSym,
28237c52310SDavid Blaikie                   DwarfLineSectionSym);
28337c52310SDavid Blaikie }
28437c52310SDavid Blaikie 
28537c52310SDavid Blaikie void DwarfCompileUnit::applyStmtList(DIE &D) {
28637c52310SDavid Blaikie   D.addValue(dwarf::DW_AT_stmt_list,
28737c52310SDavid Blaikie              UnitDie.getAbbrev().getData()[stmtListIndex].getForm(),
28837c52310SDavid Blaikie              UnitDie.getValues()[stmtListIndex]);
28937c52310SDavid Blaikie }
29037c52310SDavid Blaikie 
29114499a7dSDavid Blaikie void DwarfCompileUnit::attachLowHighPC(DIE &D, const MCSymbol *Begin,
29214499a7dSDavid Blaikie                                        const MCSymbol *End) {
29314499a7dSDavid Blaikie   assert(Begin && "Begin label should not be null!");
29414499a7dSDavid Blaikie   assert(End && "End label should not be null!");
29514499a7dSDavid Blaikie   assert(Begin->isDefined() && "Invalid starting label");
29614499a7dSDavid Blaikie   assert(End->isDefined() && "Invalid end label");
29714499a7dSDavid Blaikie 
29814499a7dSDavid Blaikie   addLabelAddress(D, dwarf::DW_AT_low_pc, Begin);
29914499a7dSDavid Blaikie   if (DD->getDwarfVersion() < 4)
30014499a7dSDavid Blaikie     addLabelAddress(D, dwarf::DW_AT_high_pc, End);
30114499a7dSDavid Blaikie   else
30214499a7dSDavid Blaikie     addLabelDelta(D, dwarf::DW_AT_high_pc, End, Begin);
30314499a7dSDavid Blaikie }
30414499a7dSDavid Blaikie 
305cda2aa82SDavid Blaikie // Find DIE for the given subprogram and attach appropriate DW_AT_low_pc
306cda2aa82SDavid Blaikie // and DW_AT_high_pc attributes. If there are global variables in this
307cda2aa82SDavid Blaikie // scope then create and insert DIEs for these variables.
308cda2aa82SDavid Blaikie DIE &DwarfCompileUnit::updateSubprogramScopeDIE(DISubprogram SP) {
309cda2aa82SDavid Blaikie   DIE *SPDie = getOrCreateSubprogramDIE(SP);
310cda2aa82SDavid Blaikie 
311cda2aa82SDavid Blaikie   attachLowHighPC(*SPDie, DD->getFunctionBeginSym(), DD->getFunctionEndSym());
312cda2aa82SDavid Blaikie   if (!DD->getCurrentFunction()->getTarget().Options.DisableFramePointerElim(
313cda2aa82SDavid Blaikie           *DD->getCurrentFunction()))
314cda2aa82SDavid Blaikie     addFlag(*SPDie, dwarf::DW_AT_APPLE_omit_frame_ptr);
315cda2aa82SDavid Blaikie 
316cda2aa82SDavid Blaikie   // Only include DW_AT_frame_base in full debug info
317cda2aa82SDavid Blaikie   if (getCUNode().getEmissionKind() != DIBuilder::LineTablesOnly) {
318cda2aa82SDavid Blaikie     const TargetRegisterInfo *RI =
319cda2aa82SDavid Blaikie         Asm->TM.getSubtargetImpl()->getRegisterInfo();
320cda2aa82SDavid Blaikie     MachineLocation Location(RI->getFrameRegister(*Asm->MF));
321cda2aa82SDavid Blaikie     addAddress(*SPDie, dwarf::DW_AT_frame_base, Location);
322cda2aa82SDavid Blaikie   }
323cda2aa82SDavid Blaikie 
324cda2aa82SDavid Blaikie   // Add name to the name table, we do this here because we're guaranteed
325cda2aa82SDavid Blaikie   // to have concrete versions of our DW_TAG_subprogram nodes.
326cda2aa82SDavid Blaikie   DD->addSubprogramNames(SP, *SPDie);
327cda2aa82SDavid Blaikie 
328cda2aa82SDavid Blaikie   return *SPDie;
329cda2aa82SDavid Blaikie }
330cda2aa82SDavid Blaikie 
3319c65b135SDavid Blaikie // Construct a DIE for this scope.
3329c65b135SDavid Blaikie void DwarfCompileUnit::constructScopeDIE(
3339c65b135SDavid Blaikie     LexicalScope *Scope, SmallVectorImpl<std::unique_ptr<DIE>> &FinalChildren) {
3349c65b135SDavid Blaikie   if (!Scope || !Scope->getScopeNode())
3359c65b135SDavid Blaikie     return;
3369c65b135SDavid Blaikie 
3379c65b135SDavid Blaikie   DIScope DS(Scope->getScopeNode());
3389c65b135SDavid Blaikie 
3399c65b135SDavid Blaikie   assert((Scope->getInlinedAt() || !DS.isSubprogram()) &&
3409c65b135SDavid Blaikie          "Only handle inlined subprograms here, use "
3419c65b135SDavid Blaikie          "constructSubprogramScopeDIE for non-inlined "
3429c65b135SDavid Blaikie          "subprograms");
3439c65b135SDavid Blaikie 
3449c65b135SDavid Blaikie   SmallVector<std::unique_ptr<DIE>, 8> Children;
3459c65b135SDavid Blaikie 
3469c65b135SDavid Blaikie   // We try to create the scope DIE first, then the children DIEs. This will
3479c65b135SDavid Blaikie   // avoid creating un-used children then removing them later when we find out
3489c65b135SDavid Blaikie   // the scope DIE is null.
3499c65b135SDavid Blaikie   std::unique_ptr<DIE> ScopeDIE;
3509c65b135SDavid Blaikie   if (Scope->getParent() && DS.isSubprogram()) {
3519c65b135SDavid Blaikie     ScopeDIE = DD->constructInlinedScopeDIE(*this, Scope);
3529c65b135SDavid Blaikie     if (!ScopeDIE)
3539c65b135SDavid Blaikie       return;
3549c65b135SDavid Blaikie     // We create children when the scope DIE is not null.
3559c65b135SDavid Blaikie     DD->createScopeChildrenDIE(*this, Scope, Children);
3569c65b135SDavid Blaikie   } else {
3579c65b135SDavid Blaikie     // Early exit when we know the scope DIE is going to be null.
3589c65b135SDavid Blaikie     if (DD->isLexicalScopeDIENull(Scope))
3599c65b135SDavid Blaikie       return;
3609c65b135SDavid Blaikie 
3619c65b135SDavid Blaikie     unsigned ChildScopeCount;
3629c65b135SDavid Blaikie 
3639c65b135SDavid Blaikie     // We create children here when we know the scope DIE is not going to be
3649c65b135SDavid Blaikie     // null and the children will be added to the scope DIE.
3659c65b135SDavid Blaikie     DD->createScopeChildrenDIE(*this, Scope, Children, &ChildScopeCount);
3669c65b135SDavid Blaikie 
3679c65b135SDavid Blaikie     // There is no need to emit empty lexical block DIE.
3689c65b135SDavid Blaikie     for (const auto &E : DD->findImportedEntitiesForScope(DS))
3699c65b135SDavid Blaikie       Children.push_back(
3709c65b135SDavid Blaikie           constructImportedEntityDIE(DIImportedEntity(E.second)));
3719c65b135SDavid Blaikie     // If there are only other scopes as children, put them directly in the
3729c65b135SDavid Blaikie     // parent instead, as this scope would serve no purpose.
3739c65b135SDavid Blaikie     if (Children.size() == ChildScopeCount) {
3749c65b135SDavid Blaikie       FinalChildren.insert(FinalChildren.end(),
3759c65b135SDavid Blaikie                            std::make_move_iterator(Children.begin()),
3769c65b135SDavid Blaikie                            std::make_move_iterator(Children.end()));
3779c65b135SDavid Blaikie       return;
3789c65b135SDavid Blaikie     }
3799c65b135SDavid Blaikie     ScopeDIE = DD->constructLexicalScopeDIE(*this, Scope);
3809c65b135SDavid Blaikie     assert(ScopeDIE && "Scope DIE should not be null.");
3819c65b135SDavid Blaikie   }
3829c65b135SDavid Blaikie 
3839c65b135SDavid Blaikie   // Add children
3849c65b135SDavid Blaikie   for (auto &I : Children)
3859c65b135SDavid Blaikie     ScopeDIE->addChild(std::move(I));
3869c65b135SDavid Blaikie 
3879c65b135SDavid Blaikie   FinalChildren.push_back(std::move(ScopeDIE));
3889c65b135SDavid Blaikie }
3899c65b135SDavid Blaikie 
39037c52310SDavid Blaikie } // end llvm namespace
391