@@ -537,8 +537,9 @@ void DwarfCompileUnit::addWasmRelocBaseGlobal(DIELoc *Loc, StringRef GlobalName,
537537// and DW_AT_high_pc attributes. If there are global variables in this
538538// scope then create and insert DIEs for these variables.
539539DIE &DwarfCompileUnit::updateSubprogramScopeDIE (const DISubprogram *SP,
540+ const Function &F,
540541 MCSymbol *LineTableSym) {
541- DIE *SPDie = getOrCreateSubprogramDIE (SP, includeMinimalInlineScopes ());
542+ DIE *SPDie = getOrCreateSubprogramDIE (SP, &F, includeMinimalInlineScopes ());
542543 SmallVector<RangeSpan, 2 > BB_List;
543544 // If basic block sections are on, ranges for each basic block section has
544545 // to be emitted separately.
@@ -1122,9 +1123,10 @@ sortLocalVars(SmallVectorImpl<DbgVariable *> &Input) {
11221123}
11231124
11241125DIE &DwarfCompileUnit::constructSubprogramScopeDIE (const DISubprogram *Sub,
1126+ const Function &F,
11251127 LexicalScope *Scope,
11261128 MCSymbol *LineTableSym) {
1127- DIE &ScopeDIE = updateSubprogramScopeDIE (Sub, LineTableSym);
1129+ DIE &ScopeDIE = updateSubprogramScopeDIE (Sub, F, LineTableSym);
11281130
11291131 if (Scope) {
11301132 assert (!Scope->getInlinedAt ());
@@ -1198,32 +1200,17 @@ DIE *DwarfCompileUnit::createAndAddScopeChildren(LexicalScope *Scope,
11981200 return ObjectPointer;
11991201}
12001202
1201- void DwarfCompileUnit::constructAbstractSubprogramScopeDIE (
1202- LexicalScope *Scope) {
1203- auto *SP = cast<DISubprogram>(Scope->getScopeNode ());
1204- if (getAbstractScopeDIEs ().count (SP))
1205- return ;
1203+ DIE &DwarfCompileUnit::getOrCreateAbstractSubprogramDIE (
1204+ const DISubprogram *SP) {
1205+ if (auto *AbsDef = getAbstractScopeDIEs ().lookup (SP))
1206+ return *AbsDef;
12061207
1207- DIE *ContextDIE;
1208- DwarfCompileUnit *ContextCU = this ;
1209-
1210- if (includeMinimalInlineScopes ())
1211- ContextDIE = &getUnitDie ();
1212- // Some of this is duplicated from DwarfUnit::getOrCreateSubprogramDIE, with
1213- // the important distinction that the debug node is not associated with the
1214- // DIE (since the debug node will be associated with the concrete DIE, if
1215- // any). It could be refactored to some common utility function.
1216- else if (auto *SPDecl = SP->getDeclaration ()) {
1217- ContextDIE = &getUnitDie ();
1218- getOrCreateSubprogramDIE (SPDecl);
1219- } else {
1220- ContextDIE = getOrCreateContextDIE (SP->getScope ());
1221- // The scope may be shared with a subprogram that has already been
1222- // constructed in another CU, in which case we need to construct this
1223- // subprogram in the same CU.
1224- ContextCU = DD->lookupCU (ContextDIE->getUnitDie ());
1225- }
1208+ auto [ContextDIE, ContextCU] = getOrCreateAbstractSubprogramContextDIE (SP);
1209+ return createAbstractSubprogramDIE (SP, ContextDIE, ContextCU);
1210+ }
12261211
1212+ DIE &DwarfCompileUnit::createAbstractSubprogramDIE (
1213+ const DISubprogram *SP, DIE *ContextDIE, DwarfCompileUnit *ContextCU) {
12271214 // Passing null as the associated node because the abstract definition
12281215 // shouldn't be found by lookup.
12291216 DIE &AbsDef = ContextCU->createAndAddDIE (dwarf::DW_TAG_subprogram,
@@ -1237,8 +1224,45 @@ void DwarfCompileUnit::constructAbstractSubprogramScopeDIE(
12371224 DD->getDwarfVersion () <= 4 ? std::optional<dwarf::Form>()
12381225 : dwarf::DW_FORM_implicit_const,
12391226 dwarf::DW_INL_inlined);
1240- if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren (Scope, AbsDef))
1241- ContextCU->addDIEEntry (AbsDef, dwarf::DW_AT_object_pointer, *ObjectPointer);
1227+
1228+ return AbsDef;
1229+ }
1230+
1231+ std::pair<DIE *, DwarfCompileUnit *>
1232+ DwarfCompileUnit::getOrCreateAbstractSubprogramContextDIE (
1233+ const DISubprogram *SP) {
1234+ bool Minimal = includeMinimalInlineScopes ();
1235+ bool IgnoreScope = shouldPlaceInUnitDIE (SP, Minimal);
1236+ DIE *ContextDIE = getOrCreateSubprogramContextDIE (SP, IgnoreScope);
1237+
1238+ if (auto *SPDecl = SP->getDeclaration ())
1239+ if (!Minimal)
1240+ getOrCreateSubprogramDIE (SPDecl, nullptr );
1241+
1242+ // The scope may be shared with a subprogram that has already been
1243+ // constructed in another CU, in which case we need to construct this
1244+ // subprogram in the same CU.
1245+ auto *ContextCU = IgnoreScope ? this : DD->lookupCU (ContextDIE->getUnitDie ());
1246+
1247+ return std::make_pair (ContextDIE, ContextCU);
1248+ }
1249+
1250+ void DwarfCompileUnit::constructAbstractSubprogramScopeDIE (
1251+ LexicalScope *Scope) {
1252+ auto *SP = cast<DISubprogram>(Scope->getScopeNode ());
1253+
1254+ // Populate subprogram DIE only once.
1255+ if (!getFinalizedAbstractSubprograms ().insert (SP).second )
1256+ return ;
1257+
1258+ auto [ContextDIE, ContextCU] = getOrCreateAbstractSubprogramContextDIE (SP);
1259+ DIE *AbsDef = getAbstractScopeDIEs ().lookup (SP);
1260+ if (!AbsDef)
1261+ AbsDef = &createAbstractSubprogramDIE (SP, ContextDIE, ContextCU);
1262+
1263+ if (DIE *ObjectPointer = ContextCU->createAndAddScopeChildren (Scope, *AbsDef))
1264+ ContextCU->addDIEEntry (*AbsDef, dwarf::DW_AT_object_pointer,
1265+ *ObjectPointer);
12421266}
12431267
12441268bool DwarfCompileUnit::useGNUAnalogForDwarf5Feature () const {
@@ -1293,9 +1317,9 @@ DwarfCompileUnit::getDwarf5OrGNULocationAtom(dwarf::LocationAtom Loc) const {
12931317}
12941318
12951319DIE &DwarfCompileUnit::constructCallSiteEntryDIE (
1296- DIE &ScopeDIE, const DISubprogram *CalleeSP, bool IsTail ,
1297- const MCSymbol *PCAddr, const MCSymbol *CallAddr, unsigned CallReg ,
1298- DIType *AllocSiteTy) {
1320+ DIE &ScopeDIE, const DISubprogram *CalleeSP, const Function *CalleeF ,
1321+ bool IsTail, const MCSymbol *PCAddr, const MCSymbol *CallAddr,
1322+ unsigned CallReg, DIType *AllocSiteTy) {
12991323 // Insert a call site entry DIE within ScopeDIE.
13001324 DIE &CallSiteDIE = createAndAddDIE (getDwarf5OrGNUTag (dwarf::DW_TAG_call_site),
13011325 ScopeDIE, nullptr );
@@ -1305,7 +1329,7 @@ DIE &DwarfCompileUnit::constructCallSiteEntryDIE(
13051329 addAddress (CallSiteDIE, getDwarf5OrGNUAttr (dwarf::DW_AT_call_target),
13061330 MachineLocation (CallReg));
13071331 } else if (CalleeSP) {
1308- DIE *CalleeDIE = getOrCreateSubprogramDIE (CalleeSP);
1332+ DIE *CalleeDIE = getOrCreateSubprogramDIE (CalleeSP, CalleeF );
13091333 assert (CalleeDIE && " Could not create DIE for call site entry origin" );
13101334 if (AddLinkageNamesToDeclCallOriginsForTuning (DD) &&
13111335 !CalleeSP->isDefinition () &&
@@ -1396,7 +1420,7 @@ DIE *DwarfCompileUnit::constructImportedEntityDIE(
13961420 if (auto *AbsSPDie = getAbstractScopeDIEs ().lookup (SP))
13971421 EntityDie = AbsSPDie;
13981422 else
1399- EntityDie = getOrCreateSubprogramDIE (SP);
1423+ EntityDie = getOrCreateSubprogramDIE (SP, nullptr );
14001424 } else if (auto *T = dyn_cast<DIType>(Entity))
14011425 EntityDie = getOrCreateTypeDIE (T);
14021426 else if (auto *GV = dyn_cast<DIGlobalVariable>(Entity))
@@ -1805,3 +1829,16 @@ DIE *DwarfCompileUnit::getOrCreateContextDIE(const DIScope *Context) {
18051829 }
18061830 return DwarfUnit::getOrCreateContextDIE (Context);
18071831}
1832+
1833+ DIE *DwarfCompileUnit::getOrCreateSubprogramDIE (const DISubprogram *SP,
1834+ const Function *F,
1835+ bool Minimal) {
1836+ if (!F && SP->isDefinition ()) {
1837+ F = DD->getLexicalScopes ().getFunction (SP);
1838+
1839+ if (!F)
1840+ return &getCU ().getOrCreateAbstractSubprogramDIE (SP);
1841+ }
1842+
1843+ return DwarfUnit::getOrCreateSubprogramDIE (SP, F, Minimal);
1844+ }
0 commit comments