Skip to content

Commit 3eb9b7d

Browse files
authored
[RISCV] Implement Relaxation for Xqcilb Jumps (#142702)
1 parent e295b0c commit 3eb9b7d

File tree

2 files changed

+214
-7
lines changed

2 files changed

+214
-7
lines changed

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -131,22 +131,42 @@ bool RISCVAsmBackend::fixupNeedsRelaxationAdvanced(const MCFixup &Fixup,
131131
// For conditional branch instructions the immediate must be
132132
// in the range [-4096, 4094].
133133
return Offset > 4094 || Offset < -4096;
134+
case RISCV::fixup_riscv_jal:
135+
// For jump instructions the immediate must be in the range
136+
// [-1048576, 1048574]
137+
return Offset > 1048574 || Offset < -1048576;
134138
}
135139
}
136140

137141
// Given a compressed control flow instruction this function returns
138-
// the expanded instruction.
139-
static unsigned getRelaxedOpcode(unsigned Op) {
140-
switch (Op) {
141-
default:
142-
return Op;
142+
// the expanded instruction, or the original instruction code if no
143+
// expansion is available.
144+
static unsigned getRelaxedOpcode(const MCInst &Inst,
145+
const MCSubtargetInfo &STI) {
146+
switch (Inst.getOpcode()) {
143147
case RISCV::C_BEQZ:
144148
return RISCV::BEQ;
145149
case RISCV::C_BNEZ:
146150
return RISCV::BNE;
147151
case RISCV::C_J:
148152
case RISCV::C_JAL: // fall through.
153+
// This only relaxes one "step" - i.e. from C.J to JAL, not from C.J to
154+
// QC.E.J, because we can always relax again if needed.
149155
return RISCV::JAL;
156+
case RISCV::JAL: {
157+
// We can only relax JAL if we have Xqcilb
158+
if (!STI.hasFeature(RISCV::FeatureVendorXqcilb))
159+
break;
160+
161+
// And only if it is using X0 or X1 for rd.
162+
MCRegister Reg = Inst.getOperand(0).getReg();
163+
if (Reg == RISCV::X0)
164+
return RISCV::QC_E_J;
165+
if (Reg == RISCV::X1)
166+
return RISCV::QC_E_JAL;
167+
168+
break;
169+
}
150170
case RISCV::BEQ:
151171
return RISCV::PseudoLongBEQ;
152172
case RISCV::BNE:
@@ -184,6 +204,9 @@ static unsigned getRelaxedOpcode(unsigned Op) {
184204
case RISCV::QC_E_BGEUI:
185205
return RISCV::PseudoLongQC_E_BGEUI;
186206
}
207+
208+
// Returning the original opcode means we cannot relax the instruction.
209+
return Inst.getOpcode();
187210
}
188211

189212
void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
@@ -201,6 +224,20 @@ void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
201224
case RISCV::C_JAL: {
202225
[[maybe_unused]] bool Success = RISCVRVC::uncompress(Res, Inst, STI);
203226
assert(Success && "Can't uncompress instruction");
227+
assert(Res.getOpcode() == getRelaxedOpcode(Inst, STI) &&
228+
"Branch Relaxation Error");
229+
break;
230+
}
231+
case RISCV::JAL: {
232+
// This has to be written manually because the QC.E.J -> JAL is
233+
// compression-only, so that it is not used when printing disassembly.
234+
assert(STI.hasFeature(RISCV::FeatureVendorXqcilb) &&
235+
"JAL is only relaxable with Xqcilb");
236+
assert((Inst.getOperand(0).getReg() == RISCV::X0 ||
237+
Inst.getOperand(0).getReg() == RISCV::X1) &&
238+
"JAL only relaxable with rd=x0 or rd=x1");
239+
Res.setOpcode(getRelaxedOpcode(Inst, STI));
240+
Res.addOperand(Inst.getOperand(1));
204241
break;
205242
}
206243
case RISCV::BEQ:
@@ -221,7 +258,7 @@ void RISCVAsmBackend::relaxInstruction(MCInst &Inst,
221258
case RISCV::QC_E_BGEI:
222259
case RISCV::QC_E_BLTUI:
223260
case RISCV::QC_E_BGEUI:
224-
Res.setOpcode(getRelaxedOpcode(Inst.getOpcode()));
261+
Res.setOpcode(getRelaxedOpcode(Inst, STI));
225262
Res.addOperand(Inst.getOperand(0));
226263
Res.addOperand(Inst.getOperand(1));
227264
Res.addOperand(Inst.getOperand(2));
@@ -370,7 +407,7 @@ bool RISCVAsmBackend::mayNeedRelaxation(const MCInst &Inst,
370407
if (STI.hasFeature(RISCV::FeatureExactAssembly))
371408
return false;
372409

373-
return getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode();
410+
return getRelaxedOpcode(Inst, STI) != Inst.getOpcode();
374411
}
375412

376413
bool RISCVAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count,
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
# RUN: split-file %s %t
2+
# RUN: llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-xqcilb %t/pass.s -o - \
3+
# RUN: | llvm-objdump -dr -M no-aliases - --mattr=+experimental-xqcilb | FileCheck %s
4+
# RUN: not llvm-mc -filetype=obj -triple riscv32 -mattr=+experimental-xqcilb %t/fail.s \
5+
# RUN: 2>&1 | FileCheck %t/fail.s --check-prefix=ERROR
6+
7+
## This testcase shows how `c.j`, `c.jal` and `jal` can be relaxed to `qc.e.j` and `qc.e.jal`
8+
## with Xqcilb, when the branches are out of range, but also that these can be compressed
9+
## when referencing close labels.
10+
11+
## The only problem we have here is when `jal` references an out-of-range label, and `rd` is
12+
## not `x0` or `x1` - for which we have no equivalent sequence, so we just fail to relax, and
13+
## emit a fixup-value-out-of-range error.
14+
15+
#--- pass.s
16+
17+
EXT_JUMP_NEGATIVE:
18+
c.nop
19+
.space 0x100000
20+
21+
FAR_JUMP_NEGATIVE:
22+
c.nop
23+
.space 0x1000
24+
25+
NEAR_NEGATIVE:
26+
c.nop
27+
28+
start:
29+
c.j NEAR
30+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR>
31+
c.j NEAR_NEGATIVE
32+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
33+
c.j FAR_JUMP
34+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP>
35+
c.j FAR_JUMP_NEGATIVE
36+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
37+
c.j EXT_JUMP
38+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP>
39+
c.j EXT_JUMP_NEGATIVE
40+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
41+
c.j undef
42+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
43+
# CHECK: R_RISCV_CUSTOM195 undef
44+
45+
c.jal NEAR
46+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
47+
c.jal NEAR_NEGATIVE
48+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
49+
c.jal FAR_JUMP
50+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP>
51+
c.jal FAR_JUMP_NEGATIVE
52+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
53+
c.jal EXT_JUMP
54+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP>
55+
c.jal EXT_JUMP_NEGATIVE
56+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
57+
c.jal undef
58+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
59+
# CHECK: R_RISCV_CUSTOM195 undef
60+
61+
jal zero, NEAR
62+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR>
63+
jal zero, NEAR_NEGATIVE
64+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
65+
jal zero, FAR_JUMP
66+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP>
67+
jal zero, FAR_JUMP_NEGATIVE
68+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
69+
jal zero, EXT_JUMP
70+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP>
71+
jal zero, EXT_JUMP_NEGATIVE
72+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
73+
jal zero, undef
74+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
75+
# CHECK: R_RISCV_CUSTOM195 undef
76+
77+
jal ra, NEAR
78+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
79+
jal ra, NEAR_NEGATIVE
80+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
81+
jal ra, FAR_JUMP
82+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP>
83+
jal ra, FAR_JUMP_NEGATIVE
84+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
85+
jal ra, EXT_JUMP
86+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP>
87+
jal ra, EXT_JUMP_NEGATIVE
88+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
89+
jal ra, undef
90+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
91+
# CHECK: R_RISCV_CUSTOM195 undef
92+
93+
qc.e.j NEAR
94+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR>
95+
qc.e.j NEAR_NEGATIVE
96+
# CHECK: c.j {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
97+
qc.e.j FAR_JUMP
98+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP>
99+
qc.e.j FAR_JUMP_NEGATIVE
100+
# CHECK: jal zero, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
101+
qc.e.j EXT_JUMP
102+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP>
103+
qc.e.j EXT_JUMP_NEGATIVE
104+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
105+
qc.e.j undef
106+
# CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
107+
# CHECK: R_RISCV_CUSTOM195 undef
108+
109+
qc.e.jal NEAR
110+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
111+
qc.e.jal NEAR_NEGATIVE
112+
# CHECK: c.jal {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
113+
qc.e.jal FAR_JUMP
114+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP>
115+
qc.e.jal FAR_JUMP_NEGATIVE
116+
# CHECK: jal ra, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
117+
qc.e.jal EXT_JUMP
118+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP>
119+
qc.e.jal EXT_JUMP_NEGATIVE
120+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
121+
qc.e.jal undef
122+
# CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
123+
# CHECK: R_RISCV_CUSTOM195 undef
124+
125+
126+
127+
jal t1, NEAR
128+
# CHECK: jal t1, {{0x[0-9a-f]+}} <NEAR>
129+
jal t1, NEAR_NEGATIVE
130+
# CHECK: jal t1, {{0x[0-9a-f]+}} <NEAR_NEGATIVE>
131+
jal t1, FAR_JUMP
132+
# CHECK: jal t1, {{0x[0-9a-f]+}} <FAR_JUMP>
133+
jal t1, FAR_JUMP_NEGATIVE
134+
# CHECK: jal t1, {{0x[0-9a-f]+}} <FAR_JUMP_NEGATIVE>
135+
136+
## The two cases with EXT_JUMP and EXT_JUMP_NEGATIVE are
137+
## in fail.s, below.
138+
139+
jal t1, undef
140+
# CHECK: jal t1, {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
141+
# CHECK: R_RISCV_JAL undef
142+
143+
144+
NEAR:
145+
c.nop
146+
.space 0x1000
147+
FAR_JUMP:
148+
c.nop
149+
.space 0x100000
150+
EXT_JUMP:
151+
c.nop
152+
153+
154+
#--- fail.s
155+
156+
157+
EXT_JUMP_NEGATIVE:
158+
c.nop
159+
.space 0x100000
160+
.space 0x1000
161+
162+
jal t1, EXT_JUMP
163+
# ERROR: [[@LINE-1]]:3: error: fixup value out of range
164+
jal t1, EXT_JUMP_NEGATIVE
165+
# ERROR: [[@LINE-1]]:3: error: fixup value out of range
166+
167+
.space 0x1000
168+
.space 0x100000
169+
EXT_JUMP:
170+
c.nop

0 commit comments

Comments
 (0)