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
1111using 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+
0 commit comments