[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:50:38 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:
+    llvm_unreachable("Unexpected jumptable entry kind");
+  case MachineJumpTableInfo::EK_LabelDifference32: {
+    // For PIC, the sequence is:
+    // BRIND(load(Jumptable + index) + RelocBase)
+    // RelocBase can be JumpTable, GOT or some sort of global base.
+    auto Load = MIRBuilder.buildLoadInstr(
+        TargetOpcode::G_LOAD, LLT::scalar(EntrySize * 8), Addr, *MMO);
+    Load = MIRBuilder.buildSExtOrTrunc(IndexTy, Load);
+    TargetReg = MIRBuilder.buildPtrAdd(PtrTy, PtrReg, Load).getReg(0);
+    break;
+  }
+  case MachineJumpTableInfo::EK_Custom32: {
----------------
topperc wrote:

EK_BlockAddress stores a pointer to the basic block target using a full pointer worth of bits. EK_Custom32 is an RV64 size optimization for the small code model where we assume that the 64 bit pointer is in +/-2GB so fits in 32 bits. Since its 32 bits it's type doesn't match p0(64 bits) so we have to sign extend it and convert it.

In instruction selection,  EK_BlockAddress used LW on rv32 and LD on rv64. EK_Custom32 used LW. We distinguished based on the EntrySize.

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


More information about the llvm-commits mailing list