[llvm] [RISCV] Extend addressing-mode and add-immediate legality to 26-bit immediates (PR #204734)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 18 23:01:56 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Garvit Gupta (quic-garvgupt)

<details>
<summary>Changes</summary>

Extend `RISCVTargetLowering::isLegalAddImmediate` and `isLegalAddressingMode` to
accept 26-bit signed immediates/offsets when the corresponding Qualcomm uC
extensions are enabled

These callbacks are queried by some pre-ISel passes. Adding support for the wider
range lets those passes keep large immediates inline and large offsets folded
into addressing modes


---
Full diff: https://github.com/llvm/llvm-project/pull/204734.diff


4 Files Affected:

- (modified) llvm/lib/Target/RISCV/RISCVISelLowering.cpp (+12-2) 
- (added) llvm/test/CodeGen/RISCV/xqcilo-split-gep.ll (+54) 
- (added) llvm/test/Transforms/CodeGenPrepare/RISCV/xqcilo.ll (+44) 
- (added) llvm/test/Transforms/ConstantHoisting/RISCV/xqcilia.ll (+28) 


``````````diff
diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
index 4a937fe2c6462..1b8f9381293d3 100644
--- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
+++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp
@@ -2458,9 +2458,15 @@ bool RISCVTargetLowering::isLegalAddressingMode(const DataLayout &DL,
   if (Subtarget.hasVInstructions() && isa<VectorType>(Ty))
     return AM.HasBaseReg && AM.Scale == 0 && !AM.BaseOffs;
 
-  // Require a 12-bit signed offset.
-  if (!isInt<12>(AM.BaseOffs))
+  // The Xqcilo extension provides load/store instructions with a 26-bit signed
+  // offset.
+  if (Subtarget.hasVendorXqcilo()) {
+    if (!isInt<26>(AM.BaseOffs))
+      return false;
+  } else if (!isInt<12>(AM.BaseOffs)) {
+    // Otherwise require a 12-bit signed offset.
     return false;
+  }
 
   switch (AM.Scale) {
   case 0: // "r+i" or just "i", depending on HasBaseReg.
@@ -2481,6 +2487,10 @@ bool RISCVTargetLowering::isLegalICmpImmediate(int64_t Imm) const {
 }
 
 bool RISCVTargetLowering::isLegalAddImmediate(int64_t Imm) const {
+  // The Xqcilia extension provides add-immediate instructions with a 26-bit
+  // signed immediate.
+  if (Subtarget.hasVendorXqcilia())
+    return isInt<26>(Imm);
   return isInt<12>(Imm);
 }
 
diff --git a/llvm/test/CodeGen/RISCV/xqcilo-split-gep.ll b/llvm/test/CodeGen/RISCV/xqcilo-split-gep.ll
new file mode 100644
index 0000000000000..1d96ad4d13665
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/xqcilo-split-gep.ll
@@ -0,0 +1,54 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 5
+
+; CodeGenPrepare's splitLargeGEPOffsets queries
+; RISCVTargetLowering::isLegalAddressingMode to decide whether a large constant
+; GEP offset must be split into a separate base address. Currently, with Xqcilo
+; enabled, the offset is reported as not foldable, so the base address is
+; computed separately and each access uses a plain lw with a small offset. A
+; follow-up extends isLegalAddressingMode to accept 26-bit offsets, after which
+; each access folds its full offset into a single qc.e.lw.
+
+; RUN: llc < %s -mtriple=riscv32 | FileCheck %s -check-prefix=RV32I
+; RUN: llc < %s -mtriple=riscv32 -mattr=+xqcilo | FileCheck %s -check-prefix=RV32XQCILO
+
+define i32 @split_large_geps(ptr %p, i1 %c) {
+; RV32I-LABEL: split_large_geps:
+; RV32I:       # %bb.0: # %entry
+; RV32I-NEXT:    andi a1, a1, 1
+; RV32I-NEXT:    lui a2, 5
+; RV32I-NEXT:    add a0, a0, a2
+; RV32I-NEXT:    beqz a1, .LBB0_2
+; RV32I-NEXT:  # %bb.1: # %a
+; RV32I-NEXT:    lw a0, -480(a0)
+; RV32I-NEXT:    ret
+; RV32I-NEXT:  .LBB0_2: # %b
+; RV32I-NEXT:    lw a0, -476(a0)
+; RV32I-NEXT:    ret
+;
+; RV32XQCILO-LABEL: split_large_geps:
+; RV32XQCILO:       # %bb.0: # %entry
+; RV32XQCILO-NEXT:    andi a1, a1, 1
+; RV32XQCILO-NEXT:    beqz a1, .LBB0_2
+; RV32XQCILO-NEXT:  # %bb.1: # %a
+; RV32XQCILO-NEXT:    qc.e.lw a0, 20000(a0)
+; RV32XQCILO-NEXT:    ret
+; RV32XQCILO-NEXT:  .LBB0_2: # %b
+; RV32XQCILO-NEXT:    qc.e.lw a0, 20004(a0)
+; RV32XQCILO-NEXT:    ret
+entry:
+  br i1 %c, label %a, label %b
+
+a:
+  %g0 = getelementptr i8, ptr %p, i32 20000
+  %v0 = load i32, ptr %g0
+  br label %merge
+
+b:
+  %g1 = getelementptr i8, ptr %p, i32 20004
+  %v1 = load i32, ptr %g1
+  br label %merge
+
+merge:
+  %r = phi i32 [ %v0, %a ], [ %v1, %b ]
+  ret i32 %r
+}
diff --git a/llvm/test/Transforms/CodeGenPrepare/RISCV/xqcilo.ll b/llvm/test/Transforms/CodeGenPrepare/RISCV/xqcilo.ll
new file mode 100644
index 0000000000000..e33865e209685
--- /dev/null
+++ b/llvm/test/Transforms/CodeGenPrepare/RISCV/xqcilo.ll
@@ -0,0 +1,44 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -S -passes='require<profile-summary>,function(codegenprepare)' \
+; RUN:   -mtriple=riscv32 -mattr=+xqcilo < %s | FileCheck %s --check-prefix=XQCILO
+
+; Two loads in separate blocks share a base pointer with large (26-bit) byte
+; offsets. CodeGenPrepare's splitLargeGEPOffsets queries
+; RISCVTargetLowering::isLegalAddressingMode to decide whether the offsets can
+; be folded into the addressing mode. With Xqcilo, isLegalAddressingMode now
+; accepts 26-bit offsets, so the GEPs are left intact and each access keeps its
+; full offset instead of being split out into a separate base address.
+define i32 @split_large_geps(ptr %p, i1 %c) {
+; XQCILO-LABEL: define i32 @split_large_geps(
+; XQCILO-SAME: ptr [[P:%.*]], i1 [[C:%.*]]) #[[ATTR0:[0-9]+]] {
+; XQCILO-NEXT:  [[ENTRY:.*:]]
+; XQCILO-NEXT:    br i1 [[C]], label %[[A:.*]], label %[[B:.*]]
+; XQCILO:       [[A]]:
+; XQCILO-NEXT:    [[SPLITGEP:%.*]] = getelementptr i8, ptr [[P]], i32 20000
+; XQCILO-NEXT:    [[V0:%.*]] = load i32, ptr [[SPLITGEP]], align 4
+; XQCILO-NEXT:    br label %[[MERGE:.*]]
+; XQCILO:       [[B]]:
+; XQCILO-NEXT:    [[TMP0:%.*]] = getelementptr i8, ptr [[P]], i32 20004
+; XQCILO-NEXT:    [[V1:%.*]] = load i32, ptr [[TMP0]], align 4
+; XQCILO-NEXT:    br label %[[MERGE]]
+; XQCILO:       [[MERGE]]:
+; XQCILO-NEXT:    [[R:%.*]] = phi i32 [ [[V0]], %[[A]] ], [ [[V1]], %[[B]] ]
+; XQCILO-NEXT:    ret i32 [[R]]
+;
+entry:
+  br i1 %c, label %a, label %b
+
+a:
+  %g0 = getelementptr i8, ptr %p, i32 20000
+  %v0 = load i32, ptr %g0
+  br label %merge
+
+b:
+  %g1 = getelementptr i8, ptr %p, i32 20004
+  %v1 = load i32, ptr %g1
+  br label %merge
+
+merge:
+  %r = phi i32 [ %v0, %a ], [ %v1, %b ]
+  ret i32 %r
+}
diff --git a/llvm/test/Transforms/ConstantHoisting/RISCV/xqcilia.ll b/llvm/test/Transforms/ConstantHoisting/RISCV/xqcilia.ll
new file mode 100644
index 0000000000000..8e4bb42355a1d
--- /dev/null
+++ b/llvm/test/Transforms/ConstantHoisting/RISCV/xqcilia.ll
@@ -0,0 +1,28 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -mtriple=riscv32 -mattr=+xqcilia -passes=consthoist -S < %s \
+; RUN:   | FileCheck %s --check-prefix=XQCILIA
+
+; A 26-bit constant used several times. With Xqcilia,
+; RISCVTargetLowering::isLegalAddImmediate now accepts 26-bit immediates, so
+; constant hoisting (which queries it via getIntImmCostInst) considers the
+; constant free and leaves it inline in each add instead of hoisting it into a
+; shared register.
+define void @hoist_simm26(i32 %a, i32 %b, i32 %c, ptr %p, ptr %q, ptr %r) {
+; XQCILIA-LABEL: define void @hoist_simm26(
+; XQCILIA-SAME: i32 [[A:%.*]], i32 [[B:%.*]], i32 [[C:%.*]], ptr [[P:%.*]], ptr [[Q:%.*]], ptr [[R:%.*]]) #[[ATTR0:[0-9]+]] {
+; XQCILIA-NEXT:    [[TMP1:%.*]] = add i32 [[A]], 32766
+; XQCILIA-NEXT:    [[TMP2:%.*]] = add i32 [[B]], 32766
+; XQCILIA-NEXT:    [[TMP3:%.*]] = add i32 [[C]], 32766
+; XQCILIA-NEXT:    store i32 [[TMP1]], ptr [[P]], align 4
+; XQCILIA-NEXT:    store i32 [[TMP2]], ptr [[Q]], align 4
+; XQCILIA-NEXT:    store i32 [[TMP3]], ptr [[R]], align 4
+; XQCILIA-NEXT:    ret void
+;
+  %1 = add i32 %a, 32766
+  %2 = add i32 %b, 32766
+  %3 = add i32 %c, 32766
+  store i32 %1, ptr %p
+  store i32 %2, ptr %q
+  store i32 %3, ptr %r
+  ret void
+}

``````````

</details>


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


More information about the llvm-commits mailing list