@@ -208,7 +208,9 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
208208 };
209209
210210 Header Header;
211- DenseMap<uint32_t , SmallVector<DWARF5AccelTableData::AttributeEncoding, 3 >>
211+ std::map<DWARF5AccelTable::TagIndex,
212+ SmallVector<DWARF5AccelTableData::AttributeEncoding, 3 >,
213+ DWARF5AccelTable::cmpByTagIndex>
212214 Abbreviations;
213215 ArrayRef<std::variant<MCSymbol *, uint64_t >> CompUnits;
214216 ArrayRef<std::variant<MCSymbol *, uint64_t >> TypeUnits;
@@ -223,6 +225,15 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
223225 bool IsSplitDwarf = false ;
224226 // / Stores the DIE offsets which are indexed by this table.
225227 DenseSet<OffsetAndUnitID> IndexedOffsets;
228+ // / Mapping between AbbrevTag and Index.
229+ std::unordered_map<uint32_t , uint32_t > AbbrevTagToIndexMap;
230+
231+ // / Constructs and returns a unique AbbrevTag that captures what a DIE
232+ // / accesses.
233+ DWARF5AccelTable::TagIndex getAbbrevIndex (
234+ const unsigned DieTag,
235+ const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> &EntryRet,
236+ const std::optional<dwarf::Form> &MaybeParentForm);
226237
227238 void populateAbbrevsMap ();
228239
@@ -234,7 +245,7 @@ class Dwarf5AccelTableWriter : public AccelTableWriter {
234245 void emitEntry (
235246 const DWARF5AccelTableData &Entry,
236247 const DenseMap<OffsetAndUnitID, MCSymbol *> &DIEOffsetToAccelEntryLabel,
237- DenseSet<MCSymbol *> &EmittedAccelEntrySymbols) const ;
248+ DenseSet<MCSymbol *> &EmittedAccelEntrySymbols);
238249 void emitData ();
239250
240251public:
@@ -409,49 +420,30 @@ DWARF5AccelTableData::getDefiningParentDieOffset(const DIE &Die) {
409420 return {};
410421}
411422
412- enum IdxParentEncoding : uint8_t {
413- NoIndexedParent = 0 , // / Parent information present but parent isn't indexed.
414- Ref4 = 1 , // / Parent information present and parent is indexed.
415- NoParent = 2 , // / Parent information missing.
416- };
417-
418- static uint32_t constexpr NumBitsIdxParent = 2 ;
419-
420- uint8_t encodeIdxParent (const std::optional<dwarf::Form> MaybeParentForm) {
421- if (!MaybeParentForm)
422- return NoParent;
423- switch (*MaybeParentForm) {
424- case dwarf::Form::DW_FORM_flag_present:
425- return NoIndexedParent;
426- case dwarf::Form::DW_FORM_ref4:
427- return Ref4;
428- default :
429- // This is not crashing on bad input: we should only reach this if the
430- // internal compiler logic is faulty; see getFormForIdxParent.
431- llvm_unreachable (" Bad form for IDX_parent" );
432- }
433- }
434-
435- static uint32_t constexpr ParentBitOffset = dwarf::DW_IDX_type_hash;
436- static uint32_t constexpr TagBitOffset = ParentBitOffset + NumBitsIdxParent;
437- static uint32_t getTagFromAbbreviationTag (const uint32_t AbbrvTag) {
438- return AbbrvTag >> TagBitOffset;
439- }
440-
441- // / Constructs a unique AbbrevTag that captures what a DIE accesses.
442- // / Using this tag we can emit a unique abbreviation for each DIE.
443- static uint32_t constructAbbreviationTag (
444- const unsigned Tag,
423+ DWARF5AccelTable::TagIndex Dwarf5AccelTableWriter::getAbbrevIndex (
424+ const unsigned DieTag,
445425 const std::optional<DWARF5AccelTable::UnitIndexAndEncoding> &EntryRet,
446- std::optional<dwarf::Form> MaybeParentForm) {
447- uint32_t AbbrvTag = 0 ;
448- if (EntryRet)
449- AbbrvTag |= 1 << EntryRet->Encoding .Index ;
450- AbbrvTag |= 1 << dwarf::DW_IDX_die_offset;
451- AbbrvTag |= 1 << dwarf::DW_IDX_parent;
452- AbbrvTag |= encodeIdxParent (MaybeParentForm) << ParentBitOffset;
453- AbbrvTag |= Tag << TagBitOffset;
454- return AbbrvTag;
426+ const std::optional<dwarf::Form> &MaybeParentForm) {
427+ DWARF5AccelTable::AbbrevDescriptor AbbrvDesc;
428+ if (EntryRet) {
429+ switch (EntryRet->Encoding .Index ) {
430+ case dwarf::DW_IDX_compile_unit:
431+ AbbrvDesc.Bits .CompUnit = true ;
432+ break ;
433+ case dwarf::DW_IDX_type_unit:
434+ AbbrvDesc.Bits .TypeUnit = true ;
435+ break ;
436+ default :
437+ llvm_unreachable (" Invalid encoding index" );
438+ break ;
439+ }
440+ }
441+ AbbrvDesc.Bits .Parent = DWARF5AccelTable::encodeIdxParent (MaybeParentForm);
442+ AbbrvDesc.Bits .DieOffset = true ;
443+ AbbrvDesc.Bits .Tag = DieTag;
444+ auto Iter = AbbrevTagToIndexMap.insert (
445+ {AbbrvDesc.Value , static_cast <uint32_t >(AbbrevTagToIndexMap.size () + 1 )});
446+ return {DieTag, Iter.first ->second };
455447}
456448
457449static std::optional<dwarf::Form>
@@ -476,8 +468,8 @@ void Dwarf5AccelTableWriter::populateAbbrevsMap() {
476468 unsigned Tag = Value->getDieTag ();
477469 std::optional<dwarf::Form> MaybeParentForm = getFormForIdxParent (
478470 IndexedOffsets, Value->getParentDieOffsetAndUnitID ());
479- uint32_t AbbrvTag =
480- constructAbbreviationTag (Tag, EntryRet, MaybeParentForm);
471+ const DWARF5AccelTable::TagIndex AbbrvTag =
472+ getAbbrevIndex (Tag, EntryRet, MaybeParentForm);
481473 if (Abbreviations.count (AbbrvTag) == 0 ) {
482474 SmallVector<DWARF5AccelTableData::AttributeEncoding, 3 > UA;
483475 if (EntryRet)
@@ -538,11 +530,9 @@ void Dwarf5AccelTableWriter::emitAbbrevs() const {
538530 Asm->OutStreamer ->emitLabel (AbbrevStart);
539531 for (const auto &Abbrev : Abbreviations) {
540532 Asm->OutStreamer ->AddComment (" Abbrev code" );
541- uint32_t Tag = getTagFromAbbreviationTag (Abbrev.first );
542- assert (Tag != 0 );
543- Asm->emitULEB128 (Abbrev.first );
544- Asm->OutStreamer ->AddComment (dwarf::TagString (Tag));
545- Asm->emitULEB128 (Tag);
533+ Asm->emitULEB128 (Abbrev.first .Index );
534+ Asm->OutStreamer ->AddComment (dwarf::TagString (Abbrev.first .DieTag ));
535+ Asm->emitULEB128 (Abbrev.first .DieTag );
546536 for (const auto &AttrEnc : Abbrev.second ) {
547537 Asm->emitULEB128 (AttrEnc.Index , dwarf::IndexString (AttrEnc.Index ).data ());
548538 Asm->emitULEB128 (AttrEnc.Form ,
@@ -558,20 +548,18 @@ void Dwarf5AccelTableWriter::emitAbbrevs() const {
558548void Dwarf5AccelTableWriter::emitEntry (
559549 const DWARF5AccelTableData &Entry,
560550 const DenseMap<OffsetAndUnitID, MCSymbol *> &DIEOffsetToAccelEntryLabel,
561- DenseSet<MCSymbol *> &EmittedAccelEntrySymbols) const {
551+ DenseSet<MCSymbol *> &EmittedAccelEntrySymbols) {
562552 std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
563553 getIndexForEntry (Entry);
564554 std::optional<OffsetAndUnitID> MaybeParentOffset =
565555 Entry.getParentDieOffsetAndUnitID ();
566556 std::optional<dwarf::Form> MaybeParentForm =
567557 getFormForIdxParent (IndexedOffsets, MaybeParentOffset);
568- uint32_t AbbrvTag =
569- constructAbbreviationTag (Entry.getDieTag (), EntryRet, MaybeParentForm);
570- auto AbbrevIt = Abbreviations.find (AbbrvTag );
558+ const DWARF5AccelTable::TagIndex TagIndexVal =
559+ getAbbrevIndex (Entry.getDieTag (), EntryRet, MaybeParentForm);
560+ auto AbbrevIt = Abbreviations.find (TagIndexVal );
571561 assert (AbbrevIt != Abbreviations.end () &&
572562 " Why wasn't this abbrev generated?" );
573- assert (getTagFromAbbreviationTag (AbbrevIt->first ) == Entry.getDieTag () &&
574- " Invalid Tag" );
575563
576564 auto EntrySymbolIt =
577565 DIEOffsetToAccelEntryLabel.find (Entry.getDieOffsetAndUnitID ());
@@ -584,7 +572,7 @@ void Dwarf5AccelTableWriter::emitEntry(
584572 if (EmittedAccelEntrySymbols.insert (EntrySymbol).second )
585573 Asm->OutStreamer ->emitLabel (EntrySymbol);
586574
587- Asm->emitULEB128 (AbbrevIt-> first , " Abbreviation code" );
575+ Asm->emitULEB128 (TagIndexVal. Index , " Abbreviation code" );
588576
589577 for (const auto &AttrEnc : AbbrevIt->second ) {
590578 Asm->OutStreamer ->AddComment (dwarf::IndexString (AttrEnc.Index ));
0 commit comments