[llvm] [RISCV] Support getJumpTableIndex (PR #224197)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 17 09:00:57 PDT 2026


================
@@ -1817,6 +1817,82 @@ bool RISCVInstrInfo::isBranchOffsetInRange(unsigned BranchOp,
   }
 }
 
+static bool isJumpTableLoad(const MachineInstr &MI) {
+  return any_of(MI.memoperands(), [](const MachineMemOperand *MMO) {
+    const PseudoSourceValue *PSV = MMO->getPseudoValue();
+    return PSV && PSV->isJumpTable();
+  });
+}
+
+/// Walk back from \p Reg through a jump-table address computation and return
+/// the index of the table it reads, or -1 if \p Reg is not part of one.
+static int getJumpTableIndexFromReg(const MachineRegisterInfo &MRI,
+                                    Register Reg, unsigned Depth) {
+  // Set the limit for the recursive search depth.
+  constexpr unsigned MaxDepth = 6;
+  if (Depth > MaxDepth)
+    return -1;
+
+  if (!Reg.isVirtual())
+    return -1;
+
+  const MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
+  if (!MI)
+    return -1;
+
+  // MI has jump table operand, return it.
+  for (const MachineOperand &MO : MI->operands())
+    if (MO.isJTI())
+      return MO.getIndex();
+
+  // Handle intermediate instructions when resolving to the JumpTableIndex.
+  switch (MI->getOpcode()) {
+  case RISCV::ADD:
+  case RISCV::ADDI:
+  case RISCV::SLLI:
----------------
topperc wrote:

When would we need to look through an SLLI?

https://github.com/llvm/llvm-project/pull/224197


More information about the llvm-commits mailing list