[llvm] 41a08e7 - [MC] Move Mach-O specific getAtom and isSectionAtomizableBySymbols to Mach-O files
Fangrui Song via llvm-commits
llvm-commits at lists.llvm.org
Sun Jun 30 10:23:18 PDT 2024
Author: Fangrui Song
Date: 2024-06-30T10:23:13-07:00
New Revision: 41a08e764aeec92703285754b5e8acd85283b1a6
URL: https://github.com/llvm/llvm-project/commit/41a08e764aeec92703285754b5e8acd85283b1a6
DIFF: https://github.com/llvm/llvm-project/commit/41a08e764aeec92703285754b5e8acd85283b1a6.diff
LOG: [MC] Move Mach-O specific getAtom and isSectionAtomizableBySymbols to Mach-O files
and devirtualize isSectionAtomizableBySymbols.
Added:
Modified:
llvm/include/llvm/MC/MCAsmInfo.h
llvm/include/llvm/MC/MCAsmInfoDarwin.h
llvm/include/llvm/MC/MCAssembler.h
llvm/include/llvm/MC/MCMachObjectWriter.h
llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
llvm/lib/MC/MCAsmInfo.cpp
llvm/lib/MC/MCAsmInfoDarwin.cpp
llvm/lib/MC/MCAssembler.cpp
llvm/lib/MC/MachObjectWriter.cpp
llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
llvm/test/CodeGen/X86/osx-private-labels.ll
Removed:
################################################################################
diff --git a/llvm/include/llvm/MC/MCAsmInfo.h b/llvm/include/llvm/MC/MCAsmInfo.h
index 48056b6ad6137..add60ef7006b8 100644
--- a/llvm/include/llvm/MC/MCAsmInfo.h
+++ b/llvm/include/llvm/MC/MCAsmInfo.h
@@ -580,12 +580,6 @@ class MCAsmInfo {
return nullptr;
}
- /// True if the section is atomized using the symbols in it.
- /// This is false if the section is not atomized at all (most ELF sections) or
- /// if it is atomized based on its contents (MachO' __TEXT,__cstring for
- /// example).
- virtual bool isSectionAtomizableBySymbols(const MCSection &Section) const;
-
virtual const MCExpr *getExprForPersonalitySymbol(const MCSymbol *Sym,
unsigned Encoding,
MCStreamer &Streamer) const;
diff --git a/llvm/include/llvm/MC/MCAsmInfoDarwin.h b/llvm/include/llvm/MC/MCAsmInfoDarwin.h
index c889ce99cebeb..4ca62b3a773e2 100644
--- a/llvm/include/llvm/MC/MCAsmInfoDarwin.h
+++ b/llvm/include/llvm/MC/MCAsmInfoDarwin.h
@@ -22,7 +22,10 @@ class MCAsmInfoDarwin : public MCAsmInfo {
public:
explicit MCAsmInfoDarwin();
- bool isSectionAtomizableBySymbols(const MCSection &Section) const override;
+ /// True if the section is atomized using the symbols in it.
+ /// This is false if the section is atomized based on its contents (MachO' __TEXT,__cstring for
+ /// example).
+ static bool isSectionAtomizableBySymbols(const MCSection &Section);
};
} // end namespace llvm
diff --git a/llvm/include/llvm/MC/MCAssembler.h b/llvm/include/llvm/MC/MCAssembler.h
index 62db4cf1faea3..7f076948c4afb 100644
--- a/llvm/include/llvm/MC/MCAssembler.h
+++ b/llvm/include/llvm/MC/MCAssembler.h
@@ -251,10 +251,6 @@ class MCAssembler {
uint64_t computeFragmentSize(const MCAsmLayout &Layout,
const MCFragment &F) const;
- /// Find the symbol which defines the atom containing the given symbol, or
- /// null if there is no such symbol.
- const MCSymbol *getAtom(const MCSymbol &S) const;
-
/// Check whether a particular symbol is visible to the linker and is required
/// in the symbol table, or whether it can be discarded by the assembler. This
/// also effects whether the assembler treats the label as potentially
diff --git a/llvm/include/llvm/MC/MCMachObjectWriter.h b/llvm/include/llvm/MC/MCMachObjectWriter.h
index 70515c9f6d2fe..6f47f12f73f13 100644
--- a/llvm/include/llvm/MC/MCMachObjectWriter.h
+++ b/llvm/include/llvm/MC/MCMachObjectWriter.h
@@ -162,6 +162,8 @@ class MachObjectWriter : public MCObjectWriter {
uint64_t getPaddingSize(const MCSection *SD, const MCAsmLayout &Layout) const;
+ const MCSymbol *getAtom(const MCSymbol &S) const;
+
bool doesSymbolRequireExternRelocation(const MCSymbol &S);
/// @}
diff --git a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
index 1eec346fe1591..976691b7de448 100644
--- a/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
+++ b/llvm/lib/CodeGen/TargetLoweringObjectFileImpl.cpp
@@ -43,6 +43,7 @@
#include "llvm/IR/PseudoProbe.h"
#include "llvm/IR/Type.h"
#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCAsmInfoDarwin.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCExpr.h"
#include "llvm/MC/MCSectionCOFF.h"
@@ -1555,7 +1556,7 @@ const MCExpr *TargetLoweringObjectFileMachO::getIndirectSymViaGOTPCRel(
static bool canUsePrivateLabel(const MCAsmInfo &AsmInfo,
const MCSection &Section) {
- if (!AsmInfo.isSectionAtomizableBySymbols(Section))
+ if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Section))
return true;
// FIXME: we should be able to use private labels for sections that can't be
diff --git a/llvm/lib/MC/MCAsmInfo.cpp b/llvm/lib/MC/MCAsmInfo.cpp
index 290be40371663..2cb0bf044ee96 100644
--- a/llvm/lib/MC/MCAsmInfo.cpp
+++ b/llvm/lib/MC/MCAsmInfo.cpp
@@ -76,10 +76,6 @@ void MCAsmInfo::addInitialFrameState(const MCCFIInstruction &Inst) {
InitialFrameState.push_back(Inst);
}
-bool MCAsmInfo::isSectionAtomizableBySymbols(const MCSection &Section) const {
- return false;
-}
-
const MCExpr *
MCAsmInfo::getExprForPersonalitySymbol(const MCSymbol *Sym,
unsigned Encoding,
diff --git a/llvm/lib/MC/MCAsmInfoDarwin.cpp b/llvm/lib/MC/MCAsmInfoDarwin.cpp
index 2137b81c799f3..a10be51ee61ac 100644
--- a/llvm/lib/MC/MCAsmInfoDarwin.cpp
+++ b/llvm/lib/MC/MCAsmInfoDarwin.cpp
@@ -19,7 +19,7 @@
using namespace llvm;
bool MCAsmInfoDarwin::isSectionAtomizableBySymbols(
- const MCSection &Section) const {
+ const MCSection &Section) {
const MCSectionMachO &SMO = static_cast<const MCSectionMachO &>(Section);
// Sections holding 1 byte strings are atomized based on the data they
diff --git a/llvm/lib/MC/MCAssembler.cpp b/llvm/lib/MC/MCAssembler.cpp
index d7eb22e085dae..dd563d8f6fb2b 100644
--- a/llvm/lib/MC/MCAssembler.cpp
+++ b/llvm/lib/MC/MCAssembler.cpp
@@ -172,25 +172,6 @@ bool MCAssembler::isSymbolLinkerVisible(const MCSymbol &Symbol) const {
return false;
}
-const MCSymbol *MCAssembler::getAtom(const MCSymbol &S) const {
- // Linker visible symbols define atoms.
- if (isSymbolLinkerVisible(S))
- return &S;
-
- // Absolute and undefined symbols have no defining atom.
- if (!S.isInSection())
- return nullptr;
-
- // Non-linker visible symbols in sections which can't be atomized have no
- // defining atom.
- if (!getContext().getAsmInfo()->isSectionAtomizableBySymbols(
- *S.getFragment()->getParent()))
- return nullptr;
-
- // Otherwise, return the atom for the containing fragment.
- return S.getFragment()->getAtom();
-}
-
bool MCAssembler::evaluateFixup(const MCAsmLayout &Layout, const MCFixup &Fixup,
const MCFragment *DF, MCValue &Target,
const MCSubtargetInfo *STI, uint64_t &Value,
diff --git a/llvm/lib/MC/MachObjectWriter.cpp b/llvm/lib/MC/MachObjectWriter.cpp
index 0ef449575f73d..cecca548d8f0e 100644
--- a/llvm/lib/MC/MachObjectWriter.cpp
+++ b/llvm/lib/MC/MachObjectWriter.cpp
@@ -12,6 +12,7 @@
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmBackend.h"
#include "llvm/MC/MCAsmLayout.h"
+#include "llvm/MC/MCAsmInfoDarwin.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCDirectives.h"
@@ -132,6 +133,36 @@ uint64_t MachObjectWriter::getPaddingSize(const MCSection *Sec,
return offsetToAlignment(EndAddr, NextSec.getAlign());
}
+static bool isSymbolLinkerVisible(const MCSymbol &Symbol) {
+ // Non-temporary labels should always be visible to the linker.
+ if (!Symbol.isTemporary())
+ return true;
+
+ if (Symbol.isUsedInReloc())
+ return true;
+
+ return false;
+}
+
+const MCSymbol *MachObjectWriter::getAtom(const MCSymbol &S) const {
+ // Linker visible symbols define atoms.
+ if (isSymbolLinkerVisible(S))
+ return &S;
+
+ // Absolute and undefined symbols have no defining atom.
+ if (!S.isInSection())
+ return nullptr;
+
+ // Non-linker visible symbols in sections which can't be atomized have no
+ // defining atom.
+ if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(
+ *S.getFragment()->getParent()))
+ return nullptr;
+
+ // Otherwise, return the atom for the containing fragment.
+ return S.getFragment()->getAtom();
+}
+
void MachObjectWriter::writeHeader(MachO::HeaderFileType Type,
unsigned NumLoadCommands,
unsigned LoadCommandsSize,
diff --git a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
index 04bd85260c560..51ae41c0ca192 100644
--- a/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
+++ b/llvm/lib/Target/AArch64/MCTargetDesc/AArch64MachObjectWriter.cpp
@@ -11,6 +11,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCAsmInfoDarwin.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
@@ -216,10 +217,10 @@ void AArch64MachObjectWriter::recordRelocation(
}
} else if (Target.getSymB()) { // A - B + constant
const MCSymbol *A = &Target.getSymA()->getSymbol();
- const MCSymbol *A_Base = Asm.getAtom(*A);
+ const MCSymbol *A_Base = Writer->getAtom(*A);
const MCSymbol *B = &Target.getSymB()->getSymbol();
- const MCSymbol *B_Base = Asm.getAtom(*B);
+ const MCSymbol *B_Base = Writer->getAtom(*B);
// Check for "_foo at got - .", which comes through here as:
// Ltmp0:
@@ -313,11 +314,12 @@ void AArch64MachObjectWriter::recordRelocation(
return;
}
const MCSection &Sec = Symbol->getSection();
- if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
+ if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Sec))
Symbol->setUsedInReloc();
}
- const MCSymbol *Base = Asm.getAtom(*Symbol);
+ const MCSymbol *Base = Writer->getAtom(*Symbol);
+
// If the symbol is a variable it can either be in a section and
// we have a base or it is absolute and should have been expanded.
assert(!Symbol->isVariable() || Base);
diff --git a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
index dff9b315f3cde..4c91a14be988f 100644
--- a/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
+++ b/llvm/lib/Target/X86/MCTargetDesc/X86MachObjectWriter.cpp
@@ -11,6 +11,7 @@
#include "llvm/ADT/Twine.h"
#include "llvm/BinaryFormat/MachO.h"
#include "llvm/MC/MCAsmInfo.h"
+#include "llvm/MC/MCAsmInfoDarwin.h"
#include "llvm/MC/MCAsmLayout.h"
#include "llvm/MC/MCAssembler.h"
#include "llvm/MC/MCContext.h"
@@ -145,12 +146,12 @@ void X86MachObjectWriter::RecordX86_64Relocation(
const MCSymbol *A = &Target.getSymA()->getSymbol();
if (A->isTemporary())
A = &Writer->findAliasedSymbol(*A);
- const MCSymbol *A_Base = Asm.getAtom(*A);
+ const MCSymbol *A_Base = Writer->getAtom(*A);
const MCSymbol *B = &Target.getSymB()->getSymbol();
if (B->isTemporary())
B = &Writer->findAliasedSymbol(*B);
- const MCSymbol *B_Base = Asm.getAtom(*B);
+ const MCSymbol *B_Base = Writer->getAtom(*B);
// Neither symbol can be modified.
if (Target.getSymA()->getKind() != MCSymbolRefExpr::VK_None) {
@@ -217,10 +218,10 @@ void X86MachObjectWriter::RecordX86_64Relocation(
const MCSymbol *Symbol = &Target.getSymA()->getSymbol();
if (Symbol->isTemporary() && Value) {
const MCSection &Sec = Symbol->getSection();
- if (!Asm.getContext().getAsmInfo()->isSectionAtomizableBySymbols(Sec))
+ if (!MCAsmInfoDarwin::isSectionAtomizableBySymbols(Sec))
Symbol->setUsedInReloc();
}
- RelSymbol = Asm.getAtom(*Symbol);
+ RelSymbol = Writer->getAtom(*Symbol);
// Relocations inside debug sections always use local relocations when
// possible. This seems to be done because the debugger doesn't fully
diff --git a/llvm/test/CodeGen/X86/osx-private-labels.ll b/llvm/test/CodeGen/X86/osx-private-labels.ll
index f3343ccb98f53..3ad06a25a0174 100644
--- a/llvm/test/CodeGen/X86/osx-private-labels.ll
+++ b/llvm/test/CodeGen/X86/osx-private-labels.ll
@@ -1,7 +1,6 @@
; RUN: llc < %s -mtriple=x86_64-apple-darwin | FileCheck %s
; Test all the cases where a L label is safe. Removing any entry from
-; TargetLoweringObjectFileMachO::isSectionAtomizableBySymbols should cause
-; this to fail.
+; isSectionAtomizableBySymbols should cause this to fail.
; We also test some noteworthy cases that require an l label.
@private1 = private unnamed_addr constant [4 x i8] c"zed\00"
More information about the llvm-commits
mailing list