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

Kane Wang via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 8 03:20:26 PDT 2026


================
@@ -1687,6 +1709,29 @@ bool RISCVInstructionSelector::isRegInFprb(Register Reg) const {
   return RBI.getRegBank(Reg, *MRI, TRI)->getID() == RISCV::FPRBRegBankID;
 }
 
+// A G_PTR_ADD result is worth splitting into Hi (materialized) +
+// Lo12 (folded offset) only if every user is a plain scalar load/store
+// using it as the address. Otherwise the ADD is selected on its own with
+// the full materialized constant, making the Hi materialization here redundant.
+bool RISCVInstructionSelector::isWorthFoldingAdd(Register AddResult) const {
+  for (const MachineOperand &Use : MRI->use_operands(AddResult)) {
----------------
ReVe1uv wrote:

Yes — the DAG does the same user scan: RISCVISelDAGToDAG.cpp:3400 (isWorthFoldingAdd), called from the large-constant branch of SelectAddrRegImm at :3572, so this matches the existing SelectionDAG behavior. The scan is only reached for constant offsets outside [-4096, 4094]; the common simm12 and AddiPair paths don't scan. Happy to drop the check or shorten it if you'd rather avoid the scan entirely.

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


More information about the llvm-commits mailing list