[llvm] 2db7110 - [RISCV] Add regalloc hints for BSETI/BEXTI (#173964)

via llvm-commits llvm-commits at lists.llvm.org
Tue Dec 30 04:19:22 PST 2025


Author: Sudharsan Veeravalli
Date: 2025-12-30T17:49:18+05:30
New Revision: 2db7110be77e2107cd3269bdbd59ebf7f1a10f9e

URL: https://github.com/llvm/llvm-project/commit/2db7110be77e2107cd3269bdbd59ebf7f1a10f9e
DIFF: https://github.com/llvm/llvm-project/commit/2db7110be77e2107cd3269bdbd59ebf7f1a10f9e.diff

LOG: [RISCV] Add regalloc hints for BSETI/BEXTI (#173964)

This patch hints the register allocator to use the same source and
destination registers for the `BEXTI/BSETI` instructions when the
`Xqcibm` vendor extension is enabled. This enables the generation of the
compressed `QC_C_BEXTI/QC_C_BSETI` instructions when possible.

Added: 
    llvm/test/CodeGen/RISCV/xqcibm-regalloc-hints.ll

Modified: 
    llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp b/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
index ebd113dc62d6d..e69dbef3d1f71 100644
--- a/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
+++ b/llvm/lib/Target/RISCV/RISCVRegisterInfo.cpp
@@ -982,6 +982,11 @@ bool RISCVRegisterInfo::getRegAllocationHints(
              MI.getOperand(2).getImm() == -1;
     case RISCV::QC_EXTU:
       return MI.getOperand(2).getImm() >= 6 && MI.getOperand(3).getImm() == 0;
+    case RISCV::BSETI:
+    case RISCV::BEXTI:
+      // qc.c.bseti, qc.c.bexti
+      NeedGPRC = true;
+      return Subtarget.hasVendorXqcibm() && MI.getOperand(2).getImm() != 0;
     }
   };
 

diff  --git a/llvm/test/CodeGen/RISCV/xqcibm-regalloc-hints.ll b/llvm/test/CodeGen/RISCV/xqcibm-regalloc-hints.ll
new file mode 100644
index 0000000000000..371ab5793f8ce
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/xqcibm-regalloc-hints.ll
@@ -0,0 +1,65 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 6
+; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefixes=RV32I
+; RUN: llc -mtriple=riscv32 -mattr=+zbs -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefixes=RV32ZBS
+; RUN: llc -mtriple=riscv32 -mattr=+zbs,+xqcibm -verify-machineinstrs < %s \
+; RUN:   | FileCheck %s -check-prefixes=RV32ZBSXQCIBM
+
+define signext i32 @bexti_hints(i32 %a, i32 signext %b, i32 %c) nounwind {
+; RV32I-LABEL: bexti_hints:
+; RV32I:       # %bb.0:
+; RV32I-NEXT:    slli a1, a1, 26
+; RV32I-NEXT:    srli a1, a1, 31
+; RV32I-NEXT:    addi a0, a1, 2047
+; RV32I-NEXT:    addi a0, a0, 1286
+; RV32I-NEXT:    ret
+;
+; RV32ZBS-LABEL: bexti_hints:
+; RV32ZBS:       # %bb.0:
+; RV32ZBS-NEXT:    bexti a0, a1, 5
+; RV32ZBS-NEXT:    addi a0, a0, 2047
+; RV32ZBS-NEXT:    addi a0, a0, 1286
+; RV32ZBS-NEXT:    ret
+;
+; RV32ZBSXQCIBM-LABEL: bexti_hints:
+; RV32ZBSXQCIBM:       # %bb.0:
+; RV32ZBSXQCIBM-NEXT:    bexti a1, a1, 5
+; RV32ZBSXQCIBM-NEXT:    addi a0, a1, 2047
+; RV32ZBSXQCIBM-NEXT:    addi a0, a0, 1286
+; RV32ZBSXQCIBM-NEXT:    ret
+  %and = and i32 %b, 32
+  %cmp = icmp ne i32 %and, 0
+  %zext = zext i1 %cmp to i32
+  %add = add i32 %zext, 3333
+  ret i32 %add
+}
+
+define i32 @bseti_hints(i32 %a, i32 %b, i32 %c) nounwind {
+; RV32I-LABEL: bseti_hints:
+; RV32I:       # %bb.0:
+; RV32I-NEXT:    li a0, 1
+; RV32I-NEXT:    slli a0, a0, 11
+; RV32I-NEXT:    or a1, a1, a0
+; RV32I-NEXT:    add a0, a1, a0
+; RV32I-NEXT:    addi a0, a0, 1285
+; RV32I-NEXT:    ret
+;
+; RV32ZBS-LABEL: bseti_hints:
+; RV32ZBS:       # %bb.0:
+; RV32ZBS-NEXT:    bseti a0, a1, 11
+; RV32ZBS-NEXT:    addi a0, a0, 2047
+; RV32ZBS-NEXT:    addi a0, a0, 1286
+; RV32ZBS-NEXT:    ret
+;
+; RV32ZBSXQCIBM-LABEL: bseti_hints:
+; RV32ZBSXQCIBM:       # %bb.0:
+; RV32ZBSXQCIBM-NEXT:    bseti a1, a1, 11
+; RV32ZBSXQCIBM-NEXT:    addi a0, a1, 2047
+; RV32ZBSXQCIBM-NEXT:    addi a0, a0, 1286
+; RV32ZBSXQCIBM-NEXT:    ret
+  %or = or i32 %b, 2048
+  %add = add i32 %or, 3333
+  ret i32 %add
+}
+


        


More information about the llvm-commits mailing list