[llvm] [RISCV] Add short forward branch support for `lb`, `lbu`, `lh`, `lhu`, `lw`, `lwu` and `ld` (PR #170829)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 10 00:48:28 PST 2025


================
@@ -897,6 +897,75 @@ MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
       .addImm(0);
 }
 
+static unsigned getLoadPredicatedOpcode(unsigned Opcode) {
+  switch (Opcode) {
+  case RISCV::LB:
+    return RISCV::PseudoCCLB;
+  case RISCV::LBU:
+    return RISCV::PseudoCCLBU;
+  case RISCV::LH:
+    return RISCV::PseudoCCLH;
+  case RISCV::LHU:
+    return RISCV::PseudoCCLHU;
+  case RISCV::LW:
+    return RISCV::PseudoCCLW;
+  case RISCV::LWU:
+    return RISCV::PseudoCCLWU;
+  case RISCV::LD:
+    return RISCV::PseudoCCLD;
+  default:
+    return 0;
+  }
+}
+
+MachineInstr *RISCVInstrInfo::foldMemoryOperandImpl(
+    MachineFunction &MF, MachineInstr &MI, ArrayRef<unsigned> Ops,
+    MachineBasicBlock::iterator InsertPt, MachineInstr &LoadMI,
+    LiveIntervals *LIS) const {
+  // For now, only handle RISCV::PseudoCCMOVGPR.
+  if (MI.getOpcode() != RISCV::PseudoCCMOVGPR)
+    return nullptr;
+
+  unsigned PredOpc = getLoadPredicatedOpcode(LoadMI.getOpcode());
+
+  if (!STI.hasShortForwardBranchILoad() || !PredOpc)
+    return nullptr;
+
+  MachineRegisterInfo &MRI = MF.getRegInfo();
+
+  if (Ops.size() != 1)
+    return nullptr;
+
+  bool Invert = MRI.getVRegDef(MI.getOperand(Ops[0]).getReg()) == &LoadMI;
----------------
topperc wrote:

We don't need to call `getVRegDef`. `Ops[0]` contains the operand number that references LoadMI. So `Invert` here will always be true. Are we missing test coverage?

I think we need to do.

```
if (Ops[0] != 4 && Ops[0] != 5)
  return nullptr;

bool Invert = Ops[0] == 5;
```

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


More information about the llvm-commits mailing list