[PATCH] RISC-V: Compress jr and jalr with a zero offset

Kito Cheng kito.cheng@sifive.com
Fri Nov 20 14:25:49 GMT 2026


gas compressed "jr a2" and "jalr a2", but not the same jumps written
with an explicit zero offset or an explicit x0 or ra destination, such
as "jalr x0, 0(a2)".  Add Zca aliases for those forms so they become
c.jr and c.jalr too, which matches LLVM.  The new Cr operand accepts
only ra, like Cz accepts only x0.
---
 gas/config/tc-riscv.c                      |  6 ++++
 gas/testsuite/gas/riscv/jalr-zero-offset.d | 32 ++++++++++++++++++++++
 gas/testsuite/gas/riscv/jalr-zero-offset.s | 28 +++++++++++++++++++
 opcodes/riscv-opc.c                        | 10 +++++++
 4 files changed, 76 insertions(+)
 create mode 100644 gas/testsuite/gas/riscv/jalr-zero-offset.d
 create mode 100644 gas/testsuite/gas/riscv/jalr-zero-offset.s

diff --git a/gas/config/tc-riscv.c b/gas/config/tc-riscv.c
index 667d015de45..d5ffb4c3ca4 100644
--- a/gas/config/tc-riscv.c
+++ b/gas/config/tc-riscv.c
@@ -1421,6 +1421,7 @@ validate_riscv_insn (const struct riscv_opcode *opc, int length)
 	    case 't': USE_BITS (OP_MASK_CRS2S, OP_SH_CRS2S); break;
 	    case 'x': break; /* CRS2S, constrained to equal RD.  */
 	    case 'z': break; /* CRS2S, constrained to be x0.  */
+	    case 'r': break; /* Register constrained to be ra.  */
 	    case '>': /* CITYPE immediate, compressed shift.  */
 	    case 'u': /* CITYPE immediate, compressed lui.  */
 	    case 'v': /* CITYPE immediate, li to compressed lui.  */
@@ -2995,6 +2996,11 @@ riscv_ip (char *str, struct riscv_cl_insn *ip, expressionS *imm_expr,
 		      || regno != 0)
 		    break;
 		  continue;
+		case 'r': /* Register constrained to equal ra.  */
+		  if (!reg_lookup (&asarg, RCLASS_GPR, &regno)
+		      || regno != X_RA)
+		    break;
+		  continue;
 		case '>': /* Shift amount, 0 - (XLEN-1).  */
 		  if (my_getSmallExpression (imm_expr, imm_reloc, asarg, p)
 		      || imm_expr->X_op != O_constant
diff --git a/gas/testsuite/gas/riscv/jalr-zero-offset.d b/gas/testsuite/gas/riscv/jalr-zero-offset.d
new file mode 100644
index 00000000000..c7b0badcaee
--- /dev/null
+++ b/gas/testsuite/gas/riscv/jalr-zero-offset.d
@@ -0,0 +1,32 @@
+#as: -march=rv64ic
+#objdump: -d
+
+.*:[ 	]+file format .*
+
+
+Disassembly of section .text:
+
+0+000 <target>:
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+8602[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+9602[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+00460067[ 	]+jr[ 	]+4\(a2\)
+[ 	]+[0-9a-f]+:[ 	]+00000067[ 	]+jr[ 	]+zero.*
+[ 	]+[0-9a-f]+:[ 	]+00060567[ 	]+jalr[ 	]+a0,a2
+[ 	]+[0-9a-f]+:[ 	]+00060067[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+000600e7[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+00060067[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+00060067[ 	]+jr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+000600e7[ 	]+jalr[ 	]+a2
+[ 	]+[0-9a-f]+:[ 	]+00060067[ 	]+jr[ 	]+a2
diff --git a/gas/testsuite/gas/riscv/jalr-zero-offset.s b/gas/testsuite/gas/riscv/jalr-zero-offset.s
new file mode 100644
index 00000000000..8c84c9e9462
--- /dev/null
+++ b/gas/testsuite/gas/riscv/jalr-zero-offset.s
@@ -0,0 +1,28 @@
+target:
+	jr	0(a2)
+	jr	(a2)
+	jr	a2, 0
+	jalr	0(a2)
+	jalr	(a2)
+	jalr	a2, 0
+	jalr	x0, a2
+	jalr	x0, 0(a2)
+	jalr	x0, (a2)
+	jalr	x0, a2, 0
+	jalr	ra, a2
+	jalr	ra, 0(a2)
+	jalr	ra, (a2)
+	jalr	ra, a2, 0
+	# Not compressible.
+	jalr	x0, 4(a2)
+	jalr	x0, 0(x0)
+	jalr	a0, 0(a2)
+	.option norvc
+	jalr	x0, 0(a2)
+	jalr	ra, 0(a2)
+	jr	(a2)
+	.option rvc
+	.option exact
+	jalr	x0, 0(a2)
+	jalr	ra, 0(a2)
+	jr	(a2)
diff --git a/opcodes/riscv-opc.c b/opcodes/riscv-opc.c
index bb052bdc7bc..146cb8a4890 100644
--- a/opcodes/riscv-opc.c
+++ b/opcodes/riscv-opc.c
@@ -490,10 +490,20 @@ const struct riscv_opcode riscv_opcodes[] =
 {"ret",         0, INSN_CLASS_ZCA, "",          MATCH_C_JR|(X_RA << OP_SH_RD), MASK_C_JR|MASK_RD, match_opcode, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
 {"ret",         0, INSN_CLASS_I, "",          MATCH_JALR|(X_RA << OP_SH_RS1), MASK_JALR|MASK_RD|MASK_RS1|MASK_IMM, match_opcode, INSN_ALIAS|INSN_BRANCH },
 {"jr",          0, INSN_CLASS_ZCA, "d",         MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
+{"jr",          0, INSN_CLASS_ZCA, "0(d)",      MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
+{"jr",          0, INSN_CLASS_ZCA, "d,0",       MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
 {"jr",          0, INSN_CLASS_I, "s",         MATCH_JALR, MASK_JALR|MASK_RD|MASK_IMM, match_opcode, INSN_ALIAS|INSN_BRANCH },
 {"jr",          0, INSN_CLASS_I, "o(s)",      MATCH_JALR, MASK_JALR|MASK_RD, match_opcode, INSN_ALIAS|INSN_BRANCH },
 {"jr",          0, INSN_CLASS_I, "s,j",       MATCH_JALR, MASK_JALR|MASK_RD, match_opcode, INSN_ALIAS|INSN_BRANCH },
 {"jalr",        0, INSN_CLASS_ZCA, "d",         MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "0(d)",      MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "d,0",       MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cr,d",      MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cr,0(d)",   MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cr,d,0",    MATCH_C_JALR, MASK_C_JALR, match_rd_nonzero, INSN_ALIAS|INSN_JSR|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cz,d",      MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cz,0(d)",   MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
+{"jalr",        0, INSN_CLASS_ZCA, "Cz,d,0",    MATCH_C_JR, MASK_C_JR, match_rd_nonzero, INSN_ALIAS|INSN_BRANCH|INSN_NON_EXACT },
 {"jalr",        0, INSN_CLASS_I, "s",         MATCH_JALR|(X_RA << OP_SH_RD), MASK_JALR|MASK_RD|MASK_IMM, match_opcode, INSN_ALIAS|INSN_JSR },
 {"jalr",        0, INSN_CLASS_I, "o(s)",      MATCH_JALR|(X_RA << OP_SH_RD), MASK_JALR|MASK_RD, match_opcode, INSN_ALIAS|INSN_JSR },
 {"jalr",        0, INSN_CLASS_I, "s,j",       MATCH_JALR|(X_RA << OP_SH_RD), MASK_JALR|MASK_RD, match_opcode, INSN_ALIAS|INSN_JSR },
-- 
2.54.0



More information about the Binutils mailing list