Skip to content

Commit 0dc2cea

Browse files
committed
jumptable
1 parent 27bee8d commit 0dc2cea

File tree

4 files changed

+99
-20
lines changed

4 files changed

+99
-20
lines changed

llvm/lib/Target/X86/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ tablegen(LLVM X86GenFoldTables.inc -gen-x86-fold-tables -asmwriternum=1)
2323
add_public_tablegen_target(X86CommonTableGen)
2424

2525
set(sources
26-
X86MatchJumptablePass.cpp
26+
X86MatchJumptablePass.cpp
2727
X86ArgumentStackSlotRebase.cpp
2828
X86AsmPrinter.cpp
2929
X86AvoidTrailingCall.cpp

llvm/lib/Target/X86/X86MatchJumptablePass.cpp

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include "llvm/Support/raw_ostream.h"
77
#include "X86MatchJumptablePass.h"
88

9-
#define DEBUG_TYPE "x86-my-pass"
9+
#define DEBUG_TYPE "match-jump-table"
1010

1111
using namespace llvm;
1212

@@ -18,23 +18,97 @@ namespace {
1818
X86MatchJumptablePass() : MachineFunctionPass(ID) {}
1919

2020
bool runOnMachineFunction(MachineFunction &MF) override {
21-
LLVM_DEBUG(dbgs() << "Running X86MyBackendPass on function: "
22-
<< MF.getName() << "\n");
23-
24-
// Example: Iterate through instructions
25-
for (auto &MBB : MF) {
26-
for (auto &MI : MBB) {
27-
// Process instructions here
28-
LLVM_DEBUG(dbgs() << "Instruction: " << MI << "\n");
21+
LLVM_DEBUG(dbgs() << "Analyzing jump tables in function: " << MF.getName() << "\n");
22+
23+
// Get jump table information
24+
MachineJumpTableInfo *JumpTableInfo = MF.getJumpTableInfo();
25+
if (!JumpTableInfo) {
26+
LLVM_DEBUG(dbgs() << "No jump tables in this function.\n");
27+
return false;
28+
}
29+
// Assuming JumpTableInfo is available
30+
for (unsigned JTIndex = 0; JTIndex < JumpTableInfo->getJumpTables().size(); ++JTIndex) {
31+
const MachineJumpTableEntry &JTEntry = JumpTableInfo->getJumpTables()[JTIndex];
32+
33+
LLVM_DEBUG(dbgs() << "Jump Table #" << JTIndex << " Base Address: " << JTEntry.BaseAddress << "\n");
34+
35+
// Iterate through the entries (target basic blocks) in this jump table
36+
for (auto *MBB : JTEntry.MBBs) {
37+
LLVM_DEBUG(dbgs() << " Target BasicBlock: " << MBB->getName() << " Address: " << MBB->getAddress() << "\n");
38+
}
39+
40+
41+
// Trace potential indirect jumps related to this jump table
42+
traceIndirectJumps(MF, JTIndex, JumpTableInfo);
43+
}
44+
return false;
45+
46+
}
47+
48+
void traceIndirectJumps(MachineFunction &MF, unsigned JTIndex, MachineJumpTableInfo *JumpTableInfo) {
49+
const MachineJumpTableEntry &JTEntry = JumpTableInfo->getJumpTables()[JTIndex];
50+
51+
for (auto &MBB : MF) {
52+
for (auto &MI : MBB) {
53+
if (MI.isIndirectBranch()) {
54+
LLVM_DEBUG(dbgs() << "Found indirect jump: " << MI << "\n");
55+
56+
// Analyze data flow to check if this jump is related to the jump table
57+
if (isJumpTableRelated(MI, JTEntry, MF)) {
58+
LLVM_DEBUG(dbgs() << "This indirect jump is related to Jump Table #" << JTIndex << "\n");
59+
}
2960
}
3061
}
62+
}
63+
}
64+
65+
bool isJumpTableRelated(MachineInstr &MI, const MachineJumpTableEntry &JTEntry, MachineFunction &MF) {
66+
for (unsigned OpIdx = 0; OpIdx < MI.getNumOperands(); ++OpIdx) {
67+
const MachineOperand &Op = MI.getOperand(OpIdx);
3168

32-
return false; // Return true if the pass modifies the function
69+
if (Op.isReg()) {
70+
Register Reg = Op.getReg();
71+
MachineRegisterInfo &MRI = MF.getRegInfo();
72+
// Check if any of the definitions of the register are related to a jump table load
73+
for (MachineInstr &DefMI : MRI.def_instructions(Reg)) {
74+
if (isJumpTableLoad(DefMI, JTEntry)) {
75+
return true;
76+
}
77+
}
78+
} else if (Op.isImm()) {
79+
// Check if the immediate operand might be an offset/index into the jump table
80+
int64_t ImmValue = Op.getImm();
81+
82+
// For example, if the jump table has 10 entries, check if the immediate is between 0 and 9
83+
if (ImmValue >= 0 && ImmValue < JTEntry.MBBs.size()) {
84+
// This immediate value could be an index into the jump table
85+
LLVM_DEBUG(dbgs() << "Immediate operand is a possible jump table index: " << ImmValue << "\n");
86+
return true;
87+
}
3388
}
89+
}
90+
return false;
91+
}
3492

35-
StringRef getPassName() const override {
36-
return "X86 My Backend Pass";
93+
bool isJumpTableLoad(MachineInstr &MI, const MachineJumpTableEntry &JTEntry) {
94+
if (MI.mayLoad()) {
95+
for (unsigned i = 0; i < MI.getNumOperands(); ++i) {
96+
const MachineOperand &Op = MI.getOperand(i);
97+
if (Op.isGlobal() && Op.getGlobal() == JTEntry.BaseAddress) {
98+
return true;
99+
}
100+
}
37101
}
102+
return false;
103+
}
104+
105+
StringRef getPassName() const override {
106+
return "Match Jump Table Pass";
107+
}
108+
109+
// StringRef getPassName() const override {
110+
// return "X86 My Backend Pass";
111+
// }
38112
};
39113
}
40114

@@ -49,3 +123,7 @@ namespace llvm {
49123
}
50124

51125
} // end llvm namespace
126+
127+
static RegisterPass<X86MatchJumptablePass> X("match-jump-table", "Match Jump Table Pass", false, false);
128+
129+
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#ifndef LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
22
#define LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
33

4-
5-
#pragma once
64
#include "llvm/CodeGen/MachineFunctionPass.h"
75

86
namespace llvm {
97

10-
FunctionPass *createX86MatchJumptablePass();
8+
/// \brief Creates the X86MatchJumptablePass.
9+
/// This pass analyzes and processes jump tables in X86 backend code generation.
10+
FunctionPass *createX86MatchJumptablePass();
1111

12-
} // end namespace llvm
12+
} // namespace llvm
1313

14-
#endif // LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H
14+
#endif // LLVM_LIB_TARGET_X86_X86MATCHJUMPTABLEPASS_H

llvm/lib/Target/X86/X86TargetMachine.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
#include "X86TargetMachine.h"
14+
#include "X86MatchJumptablePass.h"
1415
#include "MCTargetDesc/X86MCTargetDesc.h"
15-
#include "X86MatchJumptablePass.h" // Include the header with the function declaration
1616
#include "TargetInfo/X86TargetInfo.h"
1717
#include "X86.h"
1818
#include "X86MachineFunctionInfo.h"
@@ -465,7 +465,7 @@ MachineFunctionInfo *X86TargetMachine::createMachineFunctionInfo(
465465

466466
void X86PassConfig::addIRPasses() {
467467
addPass(createAtomicExpandLegacyPass());
468-
addPass(llvm::createX86MatchJumptablePass());
468+
469469
// We add both pass anyway and when these two passes run, we skip the pass
470470
// based on the option level and option attribute.
471471
addPass(createX86LowerAMXIntrinsicsPass());
@@ -597,6 +597,7 @@ void X86PassConfig::addPreEmitPass() {
597597
addPass(createBreakFalseDeps());
598598
}
599599

600+
addPass(createX86MatchJumptablePass());
600601
addPass(createX86IndirectBranchTrackingPass());
601602

602603
addPass(createX86IssueVZeroUpperPass());

0 commit comments

Comments
 (0)