[llvm] [RISCV][GlobalISel] Fold large constant offsets in selectAddrRegImm (PR #219161)

Kane Wang via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 7 23:11:17 PDT 2026


================
@@ -603,10 +621,32 @@ RISCVInstructionSelector::selectAddrRegImm(MachineOperand &Root) const {
       return {{[=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
                [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); }}};
     }
+
+    // Large constant offset. Fold a -2048/2047 adjustment so the whole
+    // constant can be split across an ADDI and the load/store offset.
+    if (RHSC >= -4096 && RHSC <= 4094) {
+      int64_t Adj = RHSC < 0 ? -2048 : 2047;
+      return renderAddiPair(LHS.getReg(), Adj, RHSC - Adj);
+    }
+
+    if (isWorthFoldingAdd(Root.getReg()))
+      if (auto Fns = computeConstAddr(RHSC, /*IsPrefetch=*/false, LHS.getReg()))
+        return Fns;
+  }
+
+  // Bare constant address. IRTranslator lowers inttoptr(C) to
+  // G_INTTOPTR(G_CONSTANT); look through it to reach the constant.
+  if (RootDef->getOpcode() == TargetOpcode::G_INTTOPTR) {
+    MachineInstr *SrcDef = MRI->getVRegDef(RootDef->getOperand(1).getReg());
----------------
ReVe1uv wrote:

Friendly ping! This has been open for 2 weeks.
Would you have a chance to take a look?
Happy to address any concerns if needed.

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


More information about the llvm-commits mailing list