Skip to content

Commit aa3589f

Browse files
authored
[MC,X86] emitInstruction: remove virtual function calls due to Intel JCC Erratum
https://reviews.llvm.org/D70157 (for Intel Jump Conditional Code Erratum) introduced two virtual function calls in the generic MCObjectStreamer::emitInstruction, which added some overhead. This patch removes the virtual function overhead: * Define `llvm::X86_MC::emitInstruction` that calls `emitInstruction{Begin,End}`. * Define {X86ELFStreamer,X86WinCOFFStreamer}::emitInstruction to call `llvm::X86_MC::emitInstruction` Pull Request: llvm#96835
1 parent 4168233 commit aa3589f

File tree

6 files changed

+54
-12
lines changed

6 files changed

+54
-12
lines changed

llvm/include/llvm/MC/MCAsmBackend.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,6 @@ class MCAsmBackend {
6262
/// tricky way for optimization.
6363
virtual bool allowEnhancedRelaxation() const { return false; }
6464

65-
/// Give the target a chance to manipulate state related to instruction
66-
/// alignment (e.g. padding for optimization), instruction relaxablility, etc.
67-
/// before and after actually emitting the instruction.
68-
virtual void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
69-
const MCSubtargetInfo &STI) {}
70-
virtual void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) {}
71-
7265
/// lifetime management
7366
virtual void reset() {}
7467

llvm/lib/MC/MCObjectStreamer.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -330,9 +330,7 @@ void MCObjectStreamer::emitInstruction(const MCInst &Inst,
330330
"' cannot have instructions");
331331
return;
332332
}
333-
getAssembler().getBackend().emitInstructionBegin(*this, Inst, STI);
334333
emitInstructionImpl(Inst, STI);
335-
getAssembler().getBackend().emitInstructionEnd(*this, Inst);
336334
}
337335

338336
void MCObjectStreamer::emitInstructionImpl(const MCInst &Inst,

llvm/lib/Target/X86/MCTargetDesc/X86AsmBackend.cpp

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//===----------------------------------------------------------------------===//
88

99
#include "MCTargetDesc/X86BaseInfo.h"
10-
#include "MCTargetDesc/X86FixupKinds.h"
1110
#include "MCTargetDesc/X86EncodingOptimization.h"
11+
#include "MCTargetDesc/X86FixupKinds.h"
1212
#include "llvm/ADT/StringSwitch.h"
1313
#include "llvm/BinaryFormat/ELF.h"
1414
#include "llvm/BinaryFormat/MachO.h"
@@ -19,6 +19,7 @@
1919
#include "llvm/MC/MCContext.h"
2020
#include "llvm/MC/MCDwarf.h"
2121
#include "llvm/MC/MCELFObjectWriter.h"
22+
#include "llvm/MC/MCELFStreamer.h"
2223
#include "llvm/MC/MCExpr.h"
2324
#include "llvm/MC/MCFixupKindInfo.h"
2425
#include "llvm/MC/MCInst.h"
@@ -162,8 +163,8 @@ class X86AsmBackend : public MCAsmBackend {
162163
bool allowAutoPadding() const override;
163164
bool allowEnhancedRelaxation() const override;
164165
void emitInstructionBegin(MCObjectStreamer &OS, const MCInst &Inst,
165-
const MCSubtargetInfo &STI) override;
166-
void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst) override;
166+
const MCSubtargetInfo &STI);
167+
void emitInstructionEnd(MCObjectStreamer &OS, const MCInst &Inst);
167168

168169
unsigned getNumFixupKinds() const override {
169170
return X86::NumTargetFixupKinds;
@@ -1546,3 +1547,37 @@ MCAsmBackend *llvm::createX86_64AsmBackend(const Target &T,
15461547
return new ELFX86_X32AsmBackend(T, OSABI, STI);
15471548
return new ELFX86_64AsmBackend(T, OSABI, STI);
15481549
}
1550+
1551+
namespace {
1552+
class X86ELFStreamer : public MCELFStreamer {
1553+
public:
1554+
X86ELFStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> TAB,
1555+
std::unique_ptr<MCObjectWriter> OW,
1556+
std::unique_ptr<MCCodeEmitter> Emitter)
1557+
: MCELFStreamer(Context, std::move(TAB), std::move(OW),
1558+
std::move(Emitter)) {}
1559+
1560+
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
1561+
};
1562+
} // end anonymous namespace
1563+
1564+
void X86_MC::emitInstruction(MCObjectStreamer &S, const MCInst &Inst,
1565+
const MCSubtargetInfo &STI) {
1566+
auto &Backend = static_cast<X86AsmBackend &>(S.getAssembler().getBackend());
1567+
Backend.emitInstructionBegin(S, Inst, STI);
1568+
S.MCObjectStreamer::emitInstruction(Inst, STI);
1569+
Backend.emitInstructionEnd(S, Inst);
1570+
}
1571+
1572+
void X86ELFStreamer::emitInstruction(const MCInst &Inst,
1573+
const MCSubtargetInfo &STI) {
1574+
X86_MC::emitInstruction(*this, Inst, STI);
1575+
}
1576+
1577+
MCStreamer *llvm::createX86ELFStreamer(const Triple &T, MCContext &Context,
1578+
std::unique_ptr<MCAsmBackend> &&MAB,
1579+
std::unique_ptr<MCObjectWriter> &&MOW,
1580+
std::unique_ptr<MCCodeEmitter> &&MCE) {
1581+
return new X86ELFStreamer(Context, std::move(MAB), std::move(MOW),
1582+
std::move(MCE));
1583+
}

llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeX86TargetMC() {
742742
TargetRegistry::RegisterNullTargetStreamer(*T, createX86NullTargetStreamer);
743743

744744
TargetRegistry::RegisterCOFFStreamer(*T, createX86WinCOFFStreamer);
745+
TargetRegistry::RegisterELFStreamer(*T, createX86ELFStreamer);
745746

746747
// Register the MCInstPrinter.
747748
TargetRegistry::RegisterMCInstPrinter(*T, createX86MCInstPrinter);

llvm/lib/Target/X86/MCTargetDesc/X86MCTargetDesc.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ class MCContext;
2424
class MCInst;
2525
class MCInstPrinter;
2626
class MCInstrInfo;
27+
class MCObjectStreamer;
2728
class MCObjectTargetWriter;
2829
class MCObjectWriter;
2930
class MCRegister;
@@ -89,6 +90,9 @@ bool needsAddressSizeOverride(const MCInst &MI, const MCSubtargetInfo &STI,
8990
/// do not need to go through TargetRegistry.
9091
MCSubtargetInfo *createX86MCSubtargetInfo(const Triple &TT, StringRef CPU,
9192
StringRef FS);
93+
94+
void emitInstruction(MCObjectStreamer &, const MCInst &Inst,
95+
const MCSubtargetInfo &STI);
9296
}
9397

9498
MCCodeEmitter *createX86MCCodeEmitter(const MCInstrInfo &MCII,
@@ -123,6 +127,11 @@ MCStreamer *createX86WinCOFFStreamer(MCContext &C,
123127
std::unique_ptr<MCCodeEmitter> &&CE,
124128
bool IncrementalLinkerCompatible);
125129

130+
MCStreamer *createX86ELFStreamer(const Triple &T, MCContext &Context,
131+
std::unique_ptr<MCAsmBackend> &&MAB,
132+
std::unique_ptr<MCObjectWriter> &&MOW,
133+
std::unique_ptr<MCCodeEmitter> &&MCE);
134+
126135
/// Construct an X86 Mach-O object writer.
127136
std::unique_ptr<MCObjectTargetWriter>
128137
createX86MachObjectWriter(bool Is64Bit, uint32_t CPUType, uint32_t CPUSubtype);

llvm/lib/Target/X86/MCTargetDesc/X86WinCOFFStreamer.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,19 @@ class X86WinCOFFStreamer : public MCWinCOFFStreamer {
2626
std::unique_ptr<MCObjectWriter> OW)
2727
: MCWinCOFFStreamer(C, std::move(AB), std::move(CE), std::move(OW)) {}
2828

29+
void emitInstruction(const MCInst &Inst, const MCSubtargetInfo &STI) override;
2930
void emitWinEHHandlerData(SMLoc Loc) override;
3031
void emitWindowsUnwindTables(WinEH::FrameInfo *Frame) override;
3132
void emitWindowsUnwindTables() override;
3233
void emitCVFPOData(const MCSymbol *ProcSym, SMLoc Loc) override;
3334
void finishImpl() override;
3435
};
3536

37+
void X86WinCOFFStreamer::emitInstruction(const MCInst &Inst,
38+
const MCSubtargetInfo &STI) {
39+
X86_MC::emitInstruction(*this, Inst, STI);
40+
}
41+
3642
void X86WinCOFFStreamer::emitWinEHHandlerData(SMLoc Loc) {
3743
MCStreamer::emitWinEHHandlerData(Loc);
3844

0 commit comments

Comments
 (0)