Skip to content

Commit

Permalink
[AccelTable][nfc] Add helper function to cast AccelTableData (llvm#77100
Browse files Browse the repository at this point in the history
)

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.
  • Loading branch information
felipepiovezan authored Jan 8, 2024
1 parent eea627e commit e72c716
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
15 changes: 11 additions & 4 deletions llvm/include/llvm/CodeGen/AccelTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -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()); }
Expand Down Expand Up @@ -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.
Expand All @@ -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);
}
Expand Down
11 changes: 5 additions & 6 deletions llvm/lib/CodeGen/AsmPrinter/AccelTable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit e72c716

Please sign in to comment.