[llvm] [RISCV][GISel] Move G_BRJT expansion to legalization (PR #73711)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 28 17:57:04 PST 2023


================
@@ -317,6 +318,62 @@ bool RISCVLegalizerInfo::legalizeShlAshrLshr(
   return true;
 }
 
+bool RISCVLegalizerInfo::legalizeBRJT(MachineInstr &MI,
+                                      MachineIRBuilder &MIRBuilder) const {
+  MachineRegisterInfo &MRI = *MIRBuilder.getMRI();
+  auto &MF = *MI.getParent()->getParent();
+  const MachineJumpTableInfo *MJTI = MF.getJumpTableInfo();
+  unsigned EntrySize = MJTI->getEntrySize(MF.getDataLayout());
+
+  Register PtrReg = MI.getOperand(0).getReg();
+  LLT PtrTy = MRI.getType(PtrReg);
+  Register IndexReg = MI.getOperand(2).getReg();
+  LLT IndexTy = MRI.getType(IndexReg);
+
+  MachineInstrBuilder Index;
+  if (isPowerOf2_32(EntrySize)) {
+    auto ShiftAmt = MIRBuilder.buildConstant(IndexTy, Log2_32(EntrySize));
+    Index = MIRBuilder.buildShl(IndexTy, IndexReg, ShiftAmt);
+  } else
+    return false;
+
+  auto Addr = MIRBuilder.buildPtrAdd(PtrTy, PtrReg, Index);
+
+  MachineMemOperand *MMO = MF.getMachineMemOperand(
+      MachinePointerInfo::getJumpTable(MF), MachineMemOperand::MOLoad,
+      EntrySize, Align(MJTI->getEntryAlignment(MF.getDataLayout())));
+
+  Register TargetReg;
+  switch (MJTI->getEntryKind()) {
+  default:
----------------
topperc wrote:

Other RISC-V code controls what possible values we could get here. We'd have to change something in RISC-V or someone would have to manually construct MIR that RISC-V doesn't expect. I'm not sure if that's worth having a special error for. I could return false here and we'd hit the default cannot legalize error.

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


More information about the llvm-commits mailing list