From 1108cf64196a056aa350baba98e3fab6d7529a59 Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sat, 28 Jun 2025 17:36:14 -0700 Subject: [PATCH] ELFObjectWriter: Optimize isInSymtab --- llvm/lib/MC/ELFObjectWriter.cpp | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/llvm/lib/MC/ELFObjectWriter.cpp b/llvm/lib/MC/ELFObjectWriter.cpp index 3ee9a5e619df..bd87b42ff61f 100644 --- a/llvm/lib/MC/ELFObjectWriter.cpp +++ b/llvm/lib/MC/ELFObjectWriter.cpp @@ -121,7 +121,7 @@ struct ELFWriter { } Mode; uint64_t symbolValue(const MCSymbol &Sym); - bool isInSymtab(const MCSymbolELF &Symbol, bool Used, bool Renamed); + bool isInSymtab(const MCSymbolELF &Symbol); /// Helper struct for containing some precomputed information on symbols. struct ELFSymbolData { @@ -469,7 +469,7 @@ void ELFWriter::writeSymbol(SymbolTableWriter &Writer, uint32_t StringIndex, IsReserved); } -bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol, bool Used, bool Renamed) { +bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol) { if (Symbol.isVariable()) { const MCExpr *Expr = Symbol.getVariableValue(); // Target Expressions that are always inlined do not appear in the symtab @@ -479,27 +479,18 @@ bool ELFWriter::isInSymtab(const MCSymbolELF &Symbol, bool Used, bool Renamed) { // The .weakref alias does not appear in the symtab. if (Symbol.isWeakref()) return false; - } - if (Used) - return true; - - if (Renamed) - return false; - - if (Symbol.isVariable() && Symbol.isUndefined()) { - // FIXME: this is here just to diagnose the case of a var = commmon_sym. - Asm.getBaseSymbol(Symbol); - return false; + if (Symbol.isUndefined()) { + // FIXME: this is here just to diagnose the case of a var = commmon_sym. + Asm.getBaseSymbol(Symbol); + return false; + } } if (Symbol.isTemporary()) return false; - if (Symbol.getType() == ELF::STT_SECTION) - return false; - - return true; + return Symbol.getType() != ELF::STT_SECTION; } void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) { @@ -531,8 +522,7 @@ void ELFWriter::computeSymbolTable(const RevGroupMapTy &RevGroupMap) { const auto &Symbol = cast(It.value()); bool Used = Symbol.isUsedInReloc(); bool isSignature = Symbol.isSignature(); - if (!isInSymtab(Symbol, Used || isSignature, - OWriter.Renames.count(&Symbol))) + if (!(Used || (!OWriter.Renames.count(&Symbol) && isInSymtab(Symbol)))) continue; if (Symbol.isTemporary() && Symbol.isUndefined()) {