[llvm] 5d5ce06 - MCSymbolELF: Migrate away from classof
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Aug 3 16:21:24 PDT 2025
Author: Fangrui Song
Date: 2025-08-03T16:21:19-07:00
New Revision: 5d5ce06cae12cd129cefd8f2d6ba2b6150fb18fd
URL: https://github.com/llvm/llvm-project/commit/5d5ce06cae12cd129cefd8f2d6ba2b6150fb18fd
DIFF: https://github.com/llvm/llvm-project/commit/5d5ce06cae12cd129cefd8f2d6ba2b6150fb18fd.diff
LOG: MCSymbolELF: Migrate away from classof
The object file format specific derived classes are used in context
where the type is statically known. We don't use isa/dyn_cast and we
want to eliminate MCSymbol::Kind in the base class.
Added:
Modified:
llvm/include/llvm/MC/MCContext.h
llvm/lib/MC/MCObjectFileInfo.cpp
llvm/lib/MC/MCParser/ELFAsmParser.cpp
llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp
llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h
index 3b393774e0037..d96609c0fec21 100644
--- a/llvm/include/llvm/MC/MCContext.h
+++ b/llvm/include/llvm/MC/MCContext.h
@@ -392,6 +392,7 @@ class MCContext {
Environment getObjectFileType() const { return Env; }
bool isELF() const { return Env == IsELF; }
bool isMachO() const { return Env == IsMachO; }
+ bool isXCOFF() const { return Env == IsXCOFF; }
const StringRef &getSwift5ReflectionSegmentName() const {
return Swift5ReflectionSegmentName;
diff --git a/llvm/lib/MC/MCObjectFileInfo.cpp b/llvm/lib/MC/MCObjectFileInfo.cpp
index 393eed18a54e6..4ac73ab34aeda 100644
--- a/llvm/lib/MC/MCObjectFileInfo.cpp
+++ b/llvm/lib/MC/MCObjectFileInfo.cpp
@@ -1135,9 +1135,10 @@ MCObjectFileInfo::getCallGraphSection(const MCSection &TextSec) const {
Flags |= ELF::SHF_GROUP;
}
- return Ctx->getELFSection(".callgraph", ELF::SHT_PROGBITS, Flags, 0,
- GroupName, true, ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec.getBeginSymbol()));
+ return Ctx->getELFSection(
+ ".callgraph", ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
+ ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
}
MCSection *
@@ -1154,9 +1155,10 @@ MCObjectFileInfo::getStackSizesSection(const MCSection &TextSec) const {
Flags |= ELF::SHF_GROUP;
}
- return Ctx->getELFSection(".stack_sizes", ELF::SHT_PROGBITS, Flags, 0,
- GroupName, true, ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec.getBeginSymbol()));
+ return Ctx->getELFSection(
+ ".stack_sizes", ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
+ ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
}
MCSection *
@@ -1174,9 +1176,10 @@ MCObjectFileInfo::getBBAddrMapSection(const MCSection &TextSec) const {
// Use the text section's begin symbol and unique ID to create a separate
// .llvm_bb_addr_map section associated with every unique text section.
- return Ctx->getELFSection(".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP,
- Flags, 0, GroupName, true, ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec.getBeginSymbol()));
+ return Ctx->getELFSection(
+ ".llvm_bb_addr_map", ELF::SHT_LLVM_BB_ADDR_MAP, Flags, 0, GroupName, true,
+ ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
}
MCSection *
@@ -1192,10 +1195,10 @@ MCObjectFileInfo::getKCFITrapSection(const MCSection &TextSec) const {
Flags |= ELF::SHF_GROUP;
}
- return Ctx->getELFSection(".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0,
- GroupName,
- /*IsComdat=*/true, ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec.getBeginSymbol()));
+ return Ctx->getELFSection(
+ ".kcfi_traps", ELF::SHT_PROGBITS, Flags, 0, GroupName,
+ /*IsComdat=*/true, ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
}
MCSection *
@@ -1211,9 +1214,10 @@ MCObjectFileInfo::getPseudoProbeSection(const MCSection &TextSec) const {
Flags |= ELF::SHF_GROUP;
}
- return Ctx->getELFSection(PseudoProbeSection->getName(), ELF::SHT_PROGBITS,
- Flags, 0, GroupName, true, ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec.getBeginSymbol()));
+ return Ctx->getELFSection(
+ PseudoProbeSection->getName(), ELF::SHT_PROGBITS, Flags, 0, GroupName,
+ true, ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec.getBeginSymbol()));
}
MCSection *
@@ -1261,7 +1265,7 @@ MCSection *MCObjectFileInfo::getPCSection(StringRef Name,
GroupName = Group->getName();
Flags |= ELF::SHF_GROUP;
}
- return Ctx->getELFSection(Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true,
- ElfSec.getUniqueID(),
- cast<MCSymbolELF>(TextSec->getBeginSymbol()));
+ return Ctx->getELFSection(
+ Name, ELF::SHT_PROGBITS, Flags, 0, GroupName, true, ElfSec.getUniqueID(),
+ static_cast<const MCSymbolELF *>(TextSec->getBeginSymbol()));
}
diff --git a/llvm/lib/MC/MCParser/ELFAsmParser.cpp b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
index 2e251cc0babae..6782c4b61863d 100644
--- a/llvm/lib/MC/MCParser/ELFAsmParser.cpp
+++ b/llvm/lib/MC/MCParser/ELFAsmParser.cpp
@@ -200,7 +200,7 @@ bool ELFAsmParser::parseDirectiveSize(StringRef, SMLoc) {
StringRef Name;
if (getParser().parseIdentifier(Name))
return TokError("expected identifier");
- MCSymbolELF *Sym = cast<MCSymbolELF>(getContext().getOrCreateSymbol(Name));
+ auto *Sym = static_cast<MCSymbolELF *>(getContext().getOrCreateSymbol(Name));
if (getLexer().isNot(AsmToken::Comma))
return TokError("expected comma");
@@ -466,7 +466,7 @@ bool ELFAsmParser::parseLinkedToSym(MCSymbolELF *&LinkedToSym) {
}
return TokError("invalid linked-to symbol");
}
- LinkedToSym = dyn_cast_or_null<MCSymbolELF>(getContext().lookupSymbol(Name));
+ LinkedToSym = static_cast<MCSymbolELF *>(getContext().lookupSymbol(Name));
if (!LinkedToSym || !LinkedToSym->isInSection())
return Error(StartLoc, "linked-to symbol is not in a section: " + Name);
return false;
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp
index 08f8be76cffa6..45ac023236b09 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64ELFObjectWriter.cpp
@@ -488,7 +488,8 @@ bool AArch64ELFObjectWriter::needsRelocateWithSymbol(const MCValue &Val,
// this global needs to be tagged. In addition, the linker needs to know
// whether to emit a special addend when relocating `end` symbols, and this
// can only be determined by the attributes of the symbol itself.
- if (Val.getAddSym() && cast<MCSymbolELF>(Val.getAddSym())->isMemtag())
+ if (Val.getAddSym() &&
+ static_cast<const MCSymbolELF *>(Val.getAddSym())->isMemtag())
return true;
if ((Val.getSpecifier() & AArch64::S_GOT) == AArch64::S_GOT)
diff --git a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
index 11c62f54a5370..16247bd732685 100644
--- a/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
+++ b/llvm/lib/Target/Mips/MCTargetDesc/MipsELFObjectWriter.cpp
@@ -450,6 +450,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
needsRelocateWithSymbol(V, (Type >> 8) & 0xff) ||
needsRelocateWithSymbol(V, (Type >> 16) & 0xff);
+ auto *Sym = static_cast<const MCSymbolELF *>(V.getAddSym());
switch (Type) {
default:
errs() << Type << "\n";
@@ -481,7 +482,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
// FIXME: It should be safe to return false for the STO_MIPS_MICROMIPS but
// we neglect to handle the adjustment to the LSB of the addend that
// it causes in applyFixup() and similar.
- if (cast<MCSymbolELF>(V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS)
+ if (Sym->getOther() & ELF::STO_MIPS_MICROMIPS)
return true;
return false;
@@ -492,7 +493,7 @@ bool MipsELFObjectWriter::needsRelocateWithSymbol(const MCValue &V,
case ELF::R_MIPS_16:
case ELF::R_MIPS_32:
case ELF::R_MIPS_GPREL32:
- if (cast<MCSymbolELF>(V.getAddSym())->getOther() & ELF::STO_MIPS_MICROMIPS)
+ if (Sym->getOther() & ELF::STO_MIPS_MICROMIPS)
return true;
[[fallthrough]];
case ELF::R_MIPS_26:
diff --git a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
index ec97e2ecd888f..04b886ae74993 100644
--- a/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
+++ b/llvm/lib/Target/PowerPC/MCTargetDesc/PPCAsmBackend.cpp
@@ -13,6 +13,7 @@
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAssembler.h"
+#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCELFObjectWriter.h"
#include "llvm/MC/MCMachObjectWriter.h"
#include "llvm/MC/MCObjectWriter.h"
@@ -112,14 +113,15 @@ class PPCAsmBackend : public MCAsmBackend {
// to resolve the fixup directly. Emit a relocation and leave
// resolution of the final target address to the linker.
if (const auto *A = Target.getAddSym()) {
- if (const auto *S = dyn_cast<MCSymbolELF>(A)) {
+ if (getContext().isELF()) {
// The "other" values are stored in the last 6 bits of the second
// byte. The traditional defines for STO values assume the full byte
// and thus the shift to pack it.
- unsigned Other = S->getOther() << 2;
+ unsigned Other = static_cast<const MCSymbolELF *>(A)->getOther() << 2;
if ((Other & ELF::STO_PPC64_LOCAL_MASK) != 0)
return true;
- } else if (const auto *S = dyn_cast<MCSymbolXCOFF>(A)) {
+ } else if (getContext().isXCOFF()) {
+ auto *S = static_cast<const MCSymbolXCOFF *>(A);
return !Target.isAbsolute() && S->isExternal() &&
S->getStorageClass() == XCOFF::C_WEAKEXT;
}
More information about the llvm-commits
mailing list