Skip to content

Commit 109e096

Browse files
committed
MC: Don't use STI to determine fragment reuse
`MCObjectStreamer::emitInstToData` is hot. https://reviews.llvm.org/D44928 passed MCSubtargetInfo to both FT_Data and FT_Relaxable fragments, adding a little overhead. While FT_Relaxable does need STI, STI is not useful for FT_Data. https://reviews.llvm.org/D44928 added STI * For bundle alignment mode, which is now gone (llvm#148781). * For ARM, which inappropriately uses STI within `ARMAsmBackend::applyFixup`. Other targets don't do this.
1 parent 52a9c49 commit 109e096

File tree

6 files changed

+26
-25
lines changed

6 files changed

+26
-25
lines changed

llvm/include/llvm/MC/MCObjectStreamer.h

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,9 @@ class MCObjectStreamer : public MCStreamer {
7373
MCSymbol *emitCFILabel() override;
7474
void emitCFISections(bool EH, bool Debug) override;
7575

76-
void insert(MCFragment *F) {
77-
auto *Sec = CurFrag->getParent();
78-
F->setParent(Sec);
79-
F->setLayoutOrder(CurFrag->getLayoutOrder() + 1);
80-
CurFrag->Next = F;
81-
CurFrag = F;
82-
Sec->curFragList()->Tail = F;
83-
}
84-
8576
/// Get a data fragment to write into, creating a new one if the current
8677
/// fragment is not FT_Data.
87-
/// Optionally a \p STI can be passed in so that a new fragment is created
88-
/// if the Subtarget differs from the current fragment.
89-
MCFragment *getOrCreateDataFragment(const MCSubtargetInfo *STI = nullptr);
78+
MCFragment *getOrCreateDataFragment();
9079

9180
protected:
9281
bool changeSectionImpl(MCSection *Section, uint32_t Subsection);

llvm/include/llvm/MC/MCSection.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ class LLVM_ABI MCSection {
188188
// destructors.
189189
class MCFragment {
190190
friend class MCAssembler;
191+
friend class MCStreamer;
191192
friend class MCObjectStreamer;
192193
friend class MCSection;
193194

llvm/include/llvm/MC/MCStreamer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,6 @@ class LLVM_ABI MCStreamer {
429429
CurFrag->getParent() == getCurrentSection().first);
430430
return CurFrag;
431431
}
432-
433432
/// Save the current and previous section on the section stack.
434433
void pushSection() {
435434
SectionStack.push_back(
@@ -457,6 +456,9 @@ class LLVM_ABI MCStreamer {
457456

458457
MCSymbol *endSection(MCSection *Section);
459458

459+
void insert(MCFragment *F);
460+
void newFragment();
461+
460462
/// Returns the mnemonic for \p MI, if the streamer has access to a
461463
/// instruction printer and returns an empty string otherwise.
462464
virtual StringRef getMnemonic(const MCInst &MI) const { return ""; }

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -106,26 +106,18 @@ void MCObjectStreamer::emitFrames(MCAsmBackend *MAB) {
106106
MCDwarfFrameEmitter::Emit(*this, MAB, false);
107107
}
108108

109-
static bool canReuseDataFragment(const MCFragment &F,
110-
const MCAssembler &Assembler,
111-
const MCSubtargetInfo *STI) {
109+
static bool canReuseDataFragment(const MCFragment &F) {
112110
if (!F.hasInstructions())
113111
return true;
114112
// Do not add data after a linker-relaxable instruction. The difference
115113
// between a new label and a label at or before the linker-relaxable
116114
// instruction cannot be resolved at assemble-time.
117-
if (F.isLinkerRelaxable())
118-
return false;
119-
// If the subtarget is changed mid fragment we start a new fragment to record
120-
// the new STI.
121-
return !STI || F.getSubtargetInfo() == STI;
115+
return !F.isLinkerRelaxable();
122116
}
123117

124-
MCFragment *
125-
MCObjectStreamer::getOrCreateDataFragment(const MCSubtargetInfo *STI) {
118+
MCFragment *MCObjectStreamer::getOrCreateDataFragment() {
126119
auto *F = getCurrentFragment();
127-
if (F->getKind() != MCFragment::FT_Data ||
128-
!canReuseDataFragment(*F, *Assembler, STI)) {
120+
if (F->getKind() != MCFragment::FT_Data || !canReuseDataFragment(*F)) {
129121
F = getContext().allocFragment<MCFragment>();
130122
insert(F);
131123
}

llvm/lib/MC/MCParser/MCTargetAsmParser.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
1010
#include "llvm/MC/MCContext.h"
1111
#include "llvm/MC/MCRegister.h"
12+
#include "llvm/MC/MCStreamer.h"
1213

1314
using namespace llvm;
1415

@@ -22,6 +23,9 @@ MCTargetAsmParser::~MCTargetAsmParser() = default;
2223
MCSubtargetInfo &MCTargetAsmParser::copySTI() {
2324
MCSubtargetInfo &STICopy = getContext().getSubtargetCopy(getSTI());
2425
STI = &STICopy;
26+
// The returned STI will likely be modified. Create a new fragment to avoid
27+
// mixed STI values within a fragment.
28+
getStreamer().newFragment();
2529
return STICopy;
2630
}
2731

llvm/lib/MC/MCStreamer.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,19 @@ MCSymbol *MCStreamer::endSection(MCSection *Section) {
14041404
return Sym;
14051405
}
14061406

1407+
void MCStreamer::insert(MCFragment *F) {
1408+
auto *Sec = CurFrag->getParent();
1409+
F->setParent(Sec);
1410+
F->setLayoutOrder(CurFrag->getLayoutOrder() + 1);
1411+
CurFrag->Next = F;
1412+
CurFrag = F;
1413+
Sec->curFragList()->Tail = F;
1414+
}
1415+
1416+
void MCStreamer::newFragment() {
1417+
insert(getContext().allocFragment<MCFragment>());
1418+
}
1419+
14071420
static VersionTuple
14081421
targetVersionOrMinimumSupportedOSVersion(const Triple &Target,
14091422
VersionTuple TargetVersion) {

0 commit comments

Comments
 (0)