From e72c71671e044aa30ca35bed9e20da771ae216b5 Mon Sep 17 00:00:00 2001 From: Felipe de Azevedo Piovezan Date: Mon, 8 Jan 2024 17:04:07 -0300 Subject: [PATCH] [AccelTable][nfc] Add helper function to cast AccelTableData (#77100) Specializations of AccelTableBase are always interested in accessing the derived versions of their data classes (e.g. DWARF5AccelTableData). They do so by sprinkling `static_casts` all over the code. This commit adds a helper function to simplify this process, reducinng the number of casts that have to be made in the middle of code, making it easier to read. --- llvm/include/llvm/CodeGen/AccelTable.h | 15 +++++++++++---- llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp | 11 +++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/llvm/include/llvm/CodeGen/AccelTable.h b/llvm/include/llvm/CodeGen/AccelTable.h index 6eb09f32f9f951..0638fbffda4f31 100644 --- a/llvm/include/llvm/CodeGen/AccelTable.h +++ b/llvm/include/llvm/CodeGen/AccelTable.h @@ -143,6 +143,15 @@ class AccelTableBase { std::vector Values; MCSymbol *Sym; + /// Get all AccelTableData cast as a `T`. + template auto getValues() const { + static_assert(std::is_pointer()); + static_assert( + std::is_base_of>()); + return map_range( + Values, [](AccelTableData *Data) { return static_cast(Data); }); + } + #ifndef NDEBUG void print(raw_ostream &OS) const; void dump() const { print(dbgs()); } @@ -319,8 +328,7 @@ class DWARF5AccelTable : public AccelTable { /// Needs to be called after DIE offsets are computed. void convertDieToOffset() { for (auto &Entry : Entries) { - for (AccelTableData *Value : Entry.second.Values) { - DWARF5AccelTableData *Data = static_cast(Value); + for (auto *Data : Entry.second.getValues()) { // For TU we normalize as each Unit is emitted. // So when this is invoked after CU construction we will be in mixed // state. @@ -332,8 +340,7 @@ class DWARF5AccelTable : public AccelTable { void addTypeEntries(DWARF5AccelTable &Table) { for (auto &Entry : Table.getEntries()) { - for (AccelTableData *Value : Entry.second.Values) { - DWARF5AccelTableData *Data = static_cast(Value); + for (auto *Data : Entry.second.getValues()) { addName(Entry.second.Name, Data->getDieOffset(), Data->getDieTag(), Data->getUnitID(), true); } diff --git a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp index bf580269eca62e..b72c17aa6f54a3 100644 --- a/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp +++ b/llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp @@ -342,8 +342,8 @@ void AppleAccelTableWriter::emitData() const { Asm->emitDwarfStringOffset(Hash->Name); Asm->OutStreamer->AddComment("Num DIEs"); Asm->emitInt32(Hash->Values.size()); - for (const auto *V : Hash->Values) - static_cast(V)->emit(Asm); + for (const auto *V : Hash->getValues()) + V->emit(Asm); PrevHash = Hash->HashValue; } // Emit the final end marker for the bucket. @@ -415,11 +415,10 @@ static uint32_t constructAbbreviationTag( void Dwarf5AccelTableWriter::populateAbbrevsMap() { for (auto &Bucket : Contents.getBuckets()) { for (auto *Hash : Bucket) { - for (auto *Value : Hash->Values) { + for (auto *Value : Hash->getValues()) { std::optional EntryRet = - getIndexForEntry(*static_cast(Value)); - unsigned Tag = - static_cast(Value)->getDieTag(); + getIndexForEntry(*Value); + unsigned Tag = Value->getDieTag(); uint32_t AbbrvTag = constructAbbreviationTag(Tag, EntryRet); if (Abbreviations.count(AbbrvTag) == 0) { SmallVector UA;