Skip to content

Commit ddc6c1f

Browse files
eddyz87Yonghong Song
authored andcommitted
[BPF] consolidate jump table emit code in BPFAsmPrinter
The requirement to emit jump table entries as offsets measured in instructions, e.g. as follows: .L0_0_set_7 = ((LBB0_7-.LBPF.JX.0.0)>>3)-1 Makes it impossible to use generic AsmPrinter::emitJumpTableInfo() function. Merge request used this generic function before (and incorrect offsets were generated). This generic function required two overloads: - AsmPrinter::GetJTISymbol() - TargetLowering::getPICJumpTableRelocBaseExpr() Now all jump table emission logic is located in the BPFAsmPrinter::emitJumpTableInfo(), which does not require above overloads. Hence, remove the overloads and move corresponding code to BPFAsmPrinter to keep it in one place.
1 parent 9aec188 commit ddc6c1f

File tree

7 files changed

+77
-76
lines changed

7 files changed

+77
-76
lines changed

llvm/include/llvm/CodeGen/AsmPrinter.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -663,8 +663,7 @@ class LLVM_ABI AsmPrinter : public MachineFunctionPass {
663663
MCSymbol *GetExternalSymbolSymbol(const Twine &Sym) const;
664664

665665
/// Return the symbol for the specified jump table entry.
666-
virtual MCSymbol *GetJTISymbol(unsigned JTID,
667-
bool isLinkerPrivate = false) const;
666+
MCSymbol *GetJTISymbol(unsigned JTID, bool isLinkerPrivate = false) const;
668667

669668
/// Return the symbol for the specified jump table .set
670669
/// FIXME: privatize to AsmPrinter.

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
//
1212
//===----------------------------------------------------------------------===//
1313

14+
#include "BPFAsmPrinter.h"
1415
#include "BPF.h"
1516
#include "BPFInstrInfo.h"
1617
#include "BPFMCInstLower.h"
@@ -39,33 +40,6 @@ using namespace llvm;
3940

4041
#define DEBUG_TYPE "asm-printer"
4142

42-
namespace {
43-
class BPFAsmPrinter : public AsmPrinter {
44-
public:
45-
explicit BPFAsmPrinter(TargetMachine &TM,
46-
std::unique_ptr<MCStreamer> Streamer)
47-
: AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr) {}
48-
49-
StringRef getPassName() const override { return "BPF Assembly Printer"; }
50-
bool doInitialization(Module &M) override;
51-
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
52-
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
53-
const char *ExtraCode, raw_ostream &O) override;
54-
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
55-
const char *ExtraCode, raw_ostream &O) override;
56-
57-
void emitInstruction(const MachineInstr *MI) override;
58-
virtual MCSymbol *GetJTISymbol(unsigned JTID,
59-
bool isLinkerPrivate = false) const override;
60-
virtual void emitJumpTableInfo() override;
61-
62-
static char ID;
63-
64-
private:
65-
BTFDebug *BTF;
66-
};
67-
} // namespace
68-
6943
bool BPFAsmPrinter::doInitialization(Module &M) {
7044
AsmPrinter::doInitialization(M);
7145

@@ -159,11 +133,31 @@ void BPFAsmPrinter::emitInstruction(const MachineInstr *MI) {
159133
EmitToStreamer(*OutStreamer, TmpInst);
160134
}
161135

162-
MCSymbol *BPFAsmPrinter::GetJTISymbol(unsigned JTID,
163-
bool isLinkerPrivate) const {
136+
// This is a label for JX instructions, used for jump table offsets
137+
// computation, e.g.:
138+
//
139+
// .LBPF.JX.0.0: <------- this is the anchor
140+
// .reloc 0, FK_SecRel_8, BPF.JT.0.0
141+
// gotox r1
142+
// ...
143+
// .section .jumptables,"",@progbits
144+
// .L0_0_set_7 = ((LBB0_7-.LBPF.JX.0.0)>>3)-1
145+
// ...
146+
// BPF.JT.0.0: <------- JT definition
147+
// .long .L0_0_set_7
148+
// ...
149+
MCSymbol *BPFAsmPrinter::getJXAnchorSymbol(unsigned JTI) {
150+
const MCAsmInfo *MAI = MF->getContext().getAsmInfo();
151+
SmallString<60> Name;
152+
raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BPF.JX."
153+
<< MF->getFunctionNumber() << '.' << JTI;
154+
return MF->getContext().getOrCreateSymbol(Name);
155+
}
156+
157+
MCSymbol *BPFAsmPrinter::getJTPublicSymbol(unsigned JTI) {
164158
SmallString<60> Name;
165159
raw_svector_ostream(Name)
166-
<< "BPF.JT." << MF->getFunctionNumber() << '.' << JTID;
160+
<< "BPF.JT." << MF->getFunctionNumber() << '.' << JTI;
167161
MCSymbol *S = OutContext.getOrCreateSymbol(Name);
168162
if (auto *ES = dyn_cast<MCSymbolELF>(S))
169163
ES->setBinding(ELF::STB_GLOBAL);
@@ -191,8 +185,7 @@ void BPFAsmPrinter::emitJumpTableInfo() {
191185
continue;
192186

193187
SmallPtrSet<const MachineBasicBlock *, 16> EmittedSets;
194-
const TargetLowering *TLI = MF->getSubtarget().getTargetLowering();
195-
const MCExpr *Base = TLI->getPICJumpTableRelocBaseExpr(MF, JTI, OutContext);
188+
auto *Base = MCSymbolRefExpr::create(getJXAnchorSymbol(JTI), OutContext);
196189
for (const MachineBasicBlock *MBB : JTBBs) {
197190
if (!EmittedSets.insert(MBB).second)
198191
continue;
@@ -215,7 +208,7 @@ void BPFAsmPrinter::emitJumpTableInfo() {
215208
// .long .L0_0_set_2
216209
// ...
217210
// .size BPF.JT.0.0, 128
218-
MCSymbol *JTStart = GetJTISymbol(JTI);
211+
MCSymbol *JTStart = getJTPublicSymbol(JTI);
219212
OutStreamer->emitLabel(JTStart);
220213
for (const MachineBasicBlock *MBB : JTBBs) {
221214
MCSymbol *SetSymbol = GetJTSetSymbol(JTI, MBB->getNumber());
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//===-- BPFFrameLowering.h - Define frame lowering for BPF -----*- C++ -*--===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIB_TARGET_BPF_BPFASMPRINTER_H
10+
#define LLVM_LIB_TARGET_BPF_BPFASMPRINTER_H
11+
12+
#include "BTFDebug.h"
13+
#include "llvm/CodeGen/AsmPrinter.h"
14+
15+
namespace llvm {
16+
17+
class BPFAsmPrinter : public AsmPrinter {
18+
public:
19+
explicit BPFAsmPrinter(TargetMachine &TM,
20+
std::unique_ptr<MCStreamer> Streamer)
21+
: AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr) {}
22+
23+
StringRef getPassName() const override { return "BPF Assembly Printer"; }
24+
bool doInitialization(Module &M) override;
25+
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
26+
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
27+
const char *ExtraCode, raw_ostream &O) override;
28+
bool PrintAsmMemoryOperand(const MachineInstr *MI, unsigned OpNum,
29+
const char *ExtraCode, raw_ostream &O) override;
30+
31+
void emitInstruction(const MachineInstr *MI) override;
32+
MCSymbol *getJTPublicSymbol(unsigned JTI);
33+
MCSymbol *getJXAnchorSymbol(unsigned JTI);
34+
virtual void emitJumpTableInfo() override;
35+
36+
static char ID;
37+
38+
private:
39+
BTFDebug *BTF;
40+
};
41+
42+
} // namespace llvm
43+
44+
#endif /* LLVM_LIB_TARGET_BPF_BPFASMPRINTER_H */

llvm/lib/Target/BPF/BPFISelLowering.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,20 +1092,6 @@ bool BPFTargetLowering::isLegalAddressingMode(const DataLayout &DL,
10921092
return true;
10931093
}
10941094

1095-
MCSymbol *BPFTargetLowering::getJXAnchorSymbol(const MachineFunction *MF,
1096-
unsigned JTI) {
1097-
const MCAsmInfo *MAI = MF->getContext().getAsmInfo();
1098-
SmallString<60> Name;
1099-
raw_svector_ostream(Name) << MAI->getPrivateGlobalPrefix() << "BPF.JX."
1100-
<< MF->getFunctionNumber() << '.' << JTI;
1101-
return MF->getContext().getOrCreateSymbol(Name);
1102-
}
1103-
11041095
unsigned BPFTargetLowering::getJumpTableEncoding() const {
11051096
return MachineJumpTableInfo::EK_LabelDifference32;
11061097
}
1107-
1108-
const MCExpr *BPFTargetLowering::getPICJumpTableRelocBaseExpr(
1109-
const MachineFunction *MF, unsigned JTI, MCContext &Ctx) const {
1110-
return MCSymbolRefExpr::create(getJXAnchorSymbol(MF, JTI), Ctx);
1111-
}

llvm/lib/Target/BPF/BPFISelLowering.h

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -71,27 +71,6 @@ class BPFTargetLowering : public TargetLowering {
7171
// JX instruction location and target basic block label.
7272
virtual unsigned getJumpTableEncoding() const override;
7373

74-
// This is a label for JX instructions, used for jump table offsets
75-
// computation, e.g.:
76-
//
77-
// .LBPF.JX.0.0: <------- this is the anchor
78-
// .reloc 0, FK_SecRel_8, BPF.JT.0.0
79-
// gotox r1
80-
// ...
81-
// .section .jumptables,"",@progbits
82-
// .L0_0_set_7 = ((LBB0_7-.LBPF.JX.0.0)>>3)-1
83-
// ...
84-
// BPF.JT.0.0: <------- JT definition
85-
// .long .L0_0_set_7
86-
// ...
87-
static MCSymbol *getJXAnchorSymbol(const MachineFunction *MF, unsigned JTI);
88-
89-
// Refers to a symbol returned by getJXAnchorSymbol(), used by
90-
// AsmPrinter::emitJumpTableInfo() to define the .L0_0_set_7 etc above.
91-
virtual const MCExpr *
92-
getPICJumpTableRelocBaseExpr(const MachineFunction *MF, unsigned JTI,
93-
MCContext &Ctx) const override;
94-
9574
private:
9675
// Control Instruction Selection Features
9776
bool HasAlu32;

llvm/lib/Target/BPF/BPFMCInstLower.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "BPFMCInstLower.h"
15+
#include "BPFAsmPrinter.h"
1516
#include "BPFISelLowering.h"
1617
#include "llvm/CodeGen/AsmPrinter.h"
1718
#include "llvm/CodeGen/MachineBasicBlock.h"
@@ -57,9 +58,8 @@ MCOperand BPFMCInstLower::LowerJTIOperand(const MachineInstr &MI,
5758
// gotox r1
5859
assert((MI.getOpcode() == BPF::JX) &&
5960
"Jump Table Index operands are expected only for JX instructions");
60-
const MachineFunction *MF = MI.getMF();
61-
Printer.OutStreamer->emitLabel(BPFTargetLowering::getJXAnchorSymbol(MF, JTI));
62-
MCSymbol *JT = Printer.GetJTISymbol(JTI);
61+
Printer.OutStreamer->emitLabel(Printer.getJXAnchorSymbol(JTI));
62+
MCSymbol *JT = Printer.getJTPublicSymbol(JTI);
6363
const MCExpr *Zero = MCConstantExpr::create(0, Ctx);
6464
Printer.OutStreamer->emitRelocDirective(*Zero, "FK_SecRel_8",
6565
MCSymbolRefExpr::create(JT, Ctx), {},

llvm/lib/Target/BPF/BPFMCInstLower.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "llvm/Support/Compiler.h"
1313

1414
namespace llvm {
15-
class AsmPrinter;
15+
class BPFAsmPrinter;
1616
class MCContext;
1717
class MCInst;
1818
class MCOperand;
@@ -24,10 +24,10 @@ class MachineOperand;
2424
class LLVM_LIBRARY_VISIBILITY BPFMCInstLower {
2525
MCContext &Ctx;
2626

27-
AsmPrinter &Printer;
27+
BPFAsmPrinter &Printer;
2828

2929
public:
30-
BPFMCInstLower(MCContext &ctx, AsmPrinter &printer)
30+
BPFMCInstLower(MCContext &ctx, BPFAsmPrinter &printer)
3131
: Ctx(ctx), Printer(printer) {}
3232
void Lower(const MachineInstr *MI, MCInst &OutMI) const;
3333

0 commit comments

Comments
 (0)