[llvm] e72c716 - [AccelTable][nfc] Add helper function to cast AccelTableData (#77100)

via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 8 12:04:12 PST 2024


Author: Felipe de Azevedo Piovezan
Date: 2024-01-08T17:04:07-03:00
New Revision: e72c71671e044aa30ca35bed9e20da771ae216b5

URL: https://github.com/llvm/llvm-project/commit/e72c71671e044aa30ca35bed9e20da771ae216b5
DIFF: https://github.com/llvm/llvm-project/commit/e72c71671e044aa30ca35bed9e20da771ae216b5.diff

LOG: [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.

Added: 
    

Modified: 
    llvm/include/llvm/CodeGen/AccelTable.h
    llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp

Removed: 
    


################################################################################
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<AccelTableData *> Values;
     MCSymbol *Sym;
 
+    /// Get all AccelTableData cast as a `T`.
+    template <typename T = AccelTableData *> auto getValues() const {
+      static_assert(std::is_pointer<T>());
+      static_assert(
+          std::is_base_of<AccelTableData, std::remove_pointer_t<T>>());
+      return map_range(
+          Values, [](AccelTableData *Data) { return static_cast<T>(Data); });
+    }
+
 #ifndef NDEBUG
     void print(raw_ostream &OS) const;
     void dump() const { print(dbgs()); }
@@ -319,8 +328,7 @@ class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
   /// 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<DWARF5AccelTableData *>(Value);
+      for (auto *Data : Entry.second.getValues<DWARF5AccelTableData *>()) {
         // 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<DWARF5AccelTableData> {
 
   void addTypeEntries(DWARF5AccelTable &Table) {
     for (auto &Entry : Table.getEntries()) {
-      for (AccelTableData *Value : Entry.second.Values) {
-        DWARF5AccelTableData *Data = static_cast<DWARF5AccelTableData *>(Value);
+      for (auto *Data : Entry.second.getValues<DWARF5AccelTableData *>()) {
         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<const AppleAccelTableData *>(V)->emit(Asm);
+      for (const auto *V : Hash->getValues<const AppleAccelTableData *>())
+        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<DWARF5AccelTableData *>()) {
         std::optional<DWARF5AccelTable::UnitIndexAndEncoding> EntryRet =
-            getIndexForEntry(*static_cast<const DWARF5AccelTableData *>(Value));
-        unsigned Tag =
-            static_cast<const DWARF5AccelTableData *>(Value)->getDieTag();
+            getIndexForEntry(*Value);
+        unsigned Tag = Value->getDieTag();
         uint32_t AbbrvTag = constructAbbreviationTag(Tag, EntryRet);
         if (Abbreviations.count(AbbrvTag) == 0) {
           SmallVector<DWARF5AccelTableData::AttributeEncoding, 2> UA;


        


More information about the llvm-commits mailing list