[llvm] [RISCV][XCV] Relax long `cv.beqimm`/`cv.bneimm` branches (PR #205096)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 22 08:35:28 PDT 2026
https://github.com/VittorioBurani updated https://github.com/llvm/llvm-project/pull/205096
>From c346cdd5fc13d218283f39b51c3b59b2d44a2346 Mon Sep 17 00:00:00 2001
From: vitbur <vittorioburani at gmail.com>
Date: Sun, 10 May 2026 17:28:49 +0200
Subject: [PATCH] [LLVM][RISCV][XCV] Relax long `cv.beqimm`/`cv.bneimm`
branches
`cv.beqimm` and `cv.bneimm` encode their target as a 13-bit signed
PC-relative offset (+/-4094 bytes). Branches further than that were
silently truncated by the MC fixup application, producing wrong
code with no diagnostic.
Mirror the relaxation flow already in place for the standard B-type
and Qualcomm Xqcibi vendor branches:
- Define `PseudoLongCV_BEQIMM` and `PseudoLongCV_BNEIMM` as 8-byte
`LongBcciPseudo` (inverted short branch + JAL trampoline) in
`RISCVInstrInfoXCV.td`.
- `RISCVAsmBackend::getRelaxedOpcode` maps `CV_BEQIMM`/`CV_BNEIMM`
to the corresponding pseudo.
- `RISCVAsmBackend::relaxInstruction` handles the two opcodes in
the existing 3-operand (rs1, imm5, target) case shared with
`QC_BxxI` / `QC_E_BxxI`.
- `RISCVMCCodeEmitter::getInvertedBranchOp` maps each pseudo to the
inverted short form (BEQIMM <-> BNEIMM).
- `RISCVMCCodeEmitter::encodeInstruction` routes the pseudos
through `expandQCLongCondBrImm` with size 4.
---
.../RISCV/MCTargetDesc/RISCVAsmBackend.cpp | 6 +++
.../RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp | 6 +++
llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td | 7 +++
llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll | 48 +++++++++++++++++++
4 files changed, 67 insertions(+)
create mode 100644 llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
index 8e4dab0a59c98..315d129aeb5fb 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
@@ -235,6 +235,10 @@ static unsigned getRelaxedOpcode(unsigned Opcode, ArrayRef<MCOperand> Operands,
return RISCV::PseudoLongQC_E_BLTUI;
case RISCV::QC_E_BGEUI:
return RISCV::PseudoLongQC_E_BGEUI;
+ case RISCV::CV_BEQIMM:
+ return RISCV::PseudoLongCV_BEQIMM;
+ case RISCV::CV_BNEIMM:
+ return RISCV::PseudoLongCV_BNEIMM;
}
// Returning the original opcode means we cannot relax the instruction.
@@ -310,6 +314,8 @@ void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
case RISCV::QC_E_BGEI:
case RISCV::QC_E_BLTUI:
case RISCV::QC_E_BGEUI:
+ case RISCV::CV_BEQIMM:
+ case RISCV::CV_BNEIMM:
Res.setOpcode(getRelaxedOpcode(Inst.getOpcode(), Inst.getOperands(), STI));
Res.addOperand(Inst.getOperand(0));
Res.addOperand(Inst.getOperand(1));
diff --git a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
index 31205fda3e9d2..23cbbf0954cd7 100644
--- a/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
+++ b/llvm/lib/Target/RISCV/MCTargetDesc/RISCVMCCodeEmitter.cpp
@@ -365,6 +365,10 @@ static unsigned getInvertedBranchOp(unsigned BrOp) {
return RISCV::QC_E_BGEUI;
case RISCV::PseudoLongQC_E_BGEUI:
return RISCV::QC_E_BLTUI;
+ case RISCV::PseudoLongCV_BEQIMM:
+ return RISCV::CV_BNEIMM;
+ case RISCV::PseudoLongCV_BNEIMM:
+ return RISCV::CV_BEQIMM;
}
}
@@ -602,6 +606,8 @@ void RISCVMCCodeEmitter::encodeInstruction(const MCInst &MI,
case RISCV::PseudoLongQC_BGEI:
case RISCV::PseudoLongQC_BLTUI:
case RISCV::PseudoLongQC_BGEUI:
+ case RISCV::PseudoLongCV_BEQIMM:
+ case RISCV::PseudoLongCV_BNEIMM:
expandQCLongCondBrImm(MI, CB, Fixups, STI, 4);
MCNumEmitted += 2;
return;
diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
index ec93f60366e1e..e8aafcd57be59 100644
--- a/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
+++ b/llvm/lib/Target/RISCV/RISCVInstrInfoXCV.td
@@ -594,6 +594,13 @@ let Predicates = [HasVendorXCVbi, IsRV32] in {
def CV_BNEIMM : CVInstImmBranch<0b111, (outs),
(ins GPR:$rs1, simm5:$imm5, bare_simm13_lsb0:$imm12),
"cv.bneimm", "$rs1, $imm5, $imm12">, Sched<[]>;
+ // Long-branch relaxation pseudos for cv.beqimm / cv.bneimm. These are emitted
+ // by the RISCVAsmBackend when the target of a CV_BEQIMM / CV_BNEIMM is
+ // further than +/-4 KiB away. Each expands to an inverted short cv.b{ne,eq}imm
+ // (4 bytes) plus an unconditional JAL trampoline (4 bytes). Reuses the
+ // LongBcciPseudo class from RISCVInstrInfo.td (size = 8 bytes, simm5 imm).
+ def PseudoLongCV_BEQIMM : LongBcciPseudo<simm5, 8>;
+ def PseudoLongCV_BNEIMM : LongBcciPseudo<simm5, 8>;
}
let Predicates = [HasVendorXCVmem, IsRV32] in {
diff --git a/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll b/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll
new file mode 100644
index 0000000000000..db125c8c30923
--- /dev/null
+++ b/llvm/test/CodeGen/RISCV/xcvbi-branch-relax.ll
@@ -0,0 +1,48 @@
+; NOTE: Do not use update_llc_test_checks.py on this file.
+; NOTE: Branch relaxation for RISC-V happens at the MC layer in RISCVAsmBackend, not in a MIR pass.
+; It is therefore only observable on object emission, not on textual asm. Use -filetype=obj + llvm-objdump.
+
+; RUN: llc -mtriple=riscv32 -mattr=+xcvbi -filetype=obj -verify-machineinstrs < %s \
+; RUN: | llvm-objdump -d -M no-aliases --no-show-raw-insn - | FileCheck %s
+
+; A short forward branch (target ~8 KiB away, well past the +/-4094 range
+; of cv.beqimm / cv.bneimm) must be relaxed by the MC layer into:
+; <inverted short branch> rs1, imm5, .Lskip
+; jal zero, target
+; .Lskip:
+; Without relaxation, the assembler would silently truncate the 13-bit
+; branch immediate and produce wrong code.
+
+define void @test_far_beqimm(i32 %x) {
+; CHECK-LABEL: <test_far_beqimm>:
+; CHECK: cv.bneimm a0, 0x3, 0x{{[0-9a-f]+}}
+; CHECK-NEXT: jal zero, 0x{{[0-9a-f]+}}
+entry:
+ %cmp = icmp eq i32 %x, 3
+ br i1 %cmp, label %target, label %filler
+
+filler:
+ ; ~8 KiB of dead bytes via inline asm, to force the branch out of range.
+ call void asm sideeffect ".zero 8192", ""()
+ br label %target
+
+target:
+ ret void
+}
+
+define void @test_far_bneimm(i32 %x) {
+; CHECK-LABEL: <test_far_bneimm>:
+; CHECK: cv.beqimm a0, 0x3, 0x{{[0-9a-f]+}}
+; CHECK-NEXT: jal zero, 0x{{[0-9a-f]+}}
+entry:
+ %cmp = icmp ne i32 %x, 3
+ br i1 %cmp, label %target, label %filler
+
+filler:
+ ; ~8 KiB of dead bytes via inline asm, to force the branch out of range.
+ call void asm sideeffect ".zero 8192", ""()
+ br label %target
+
+target:
+ ret void
+}
More information about the llvm-commits
mailing list