Skip to content

Commit da16130

Browse files
author
Yonghong Song
committed
[BPF] Remove unused globals whose were used for jump table
1 parent cdbe4fe commit da16130

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

llvm/lib/Target/BPF/BPFAsmPrinter.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,45 @@ bool BPFAsmPrinter::doInitialization(Module &M) {
5252
return false;
5353
}
5454

55+
const BPFTargetMachine &BPFAsmPrinter::getBTM() const {
56+
return static_cast<const BPFTargetMachine &>(TM);
57+
}
58+
59+
bool BPFAsmPrinter::doFinalization(Module &M) {
60+
// Remove unused globals which are previously used for jump table.
61+
const BPFSubtarget *Subtarget = getBTM().getSubtargetImpl();
62+
if (Subtarget->hasGotox()) {
63+
std::vector<GlobalVariable *> Targets;
64+
for (GlobalVariable &Global : M.globals()) {
65+
if (Global.getLinkage() != GlobalValue::PrivateLinkage)
66+
continue;
67+
if (!Global.isConstant() || !Global.hasInitializer())
68+
continue;
69+
70+
Constant *CV = dyn_cast<Constant>(Global.getInitializer());
71+
if (!CV)
72+
continue;
73+
ConstantArray *CA = dyn_cast<ConstantArray>(CV);
74+
if (!CA)
75+
continue;
76+
77+
for (unsigned i = 1, e = CA->getNumOperands(); i != e; ++i) {
78+
if (!dyn_cast<BlockAddress>(CA->getOperand(i)))
79+
continue;
80+
}
81+
Targets.push_back(&Global);
82+
}
83+
84+
for (GlobalVariable *GV : Targets) {
85+
GV->replaceAllUsesWith(PoisonValue::get(GV->getType()));
86+
GV->dropAllReferences();
87+
GV->eraseFromParent();
88+
}
89+
}
90+
91+
return AsmPrinter::doFinalization(M);
92+
}
93+
5594
void BPFAsmPrinter::printOperand(const MachineInstr *MI, int OpNum,
5695
raw_ostream &O) {
5796
const MachineOperand &MO = MI->getOperand(OpNum);

llvm/lib/Target/BPF/BPFAsmPrinter.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#ifndef LLVM_LIB_TARGET_BPF_BPFASMPRINTER_H
1010
#define LLVM_LIB_TARGET_BPF_BPFASMPRINTER_H
1111

12+
#include "BPFTargetMachine.h"
1213
#include "BTFDebug.h"
1314
#include "llvm/CodeGen/AsmPrinter.h"
1415

@@ -18,10 +19,11 @@ class BPFAsmPrinter : public AsmPrinter {
1819
public:
1920
explicit BPFAsmPrinter(TargetMachine &TM,
2021
std::unique_ptr<MCStreamer> Streamer)
21-
: AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr) {}
22+
: AsmPrinter(TM, std::move(Streamer), ID), BTF(nullptr), TM(TM) {}
2223

2324
StringRef getPassName() const override { return "BPF Assembly Printer"; }
2425
bool doInitialization(Module &M) override;
26+
bool doFinalization(Module &M) override;
2527
void printOperand(const MachineInstr *MI, int OpNum, raw_ostream &O);
2628
bool PrintAsmOperand(const MachineInstr *MI, unsigned OpNo,
2729
const char *ExtraCode, raw_ostream &O) override;
@@ -36,6 +38,9 @@ class BPFAsmPrinter : public AsmPrinter {
3638

3739
private:
3840
BTFDebug *BTF;
41+
TargetMachine &TM;
42+
43+
const BPFTargetMachine &getBTM() const;
3944
};
4045

4146
} // namespace llvm

0 commit comments

Comments
 (0)