Skip to content

Commit 62245aa

Browse files
authored
[RISCV] Improve Errors for GPRNoX0X2/SP Reg Classes (#126394)
More adoption of better diagnostics for RISC-V register classes: - GPRNoX0X2 (GPRs excluding `zero` and `x2`, used for `c.lui`) - SP (only contains `sp`)
1 parent 5b9e6c7 commit 62245aa

File tree

3 files changed

+15
-9
lines changed

3 files changed

+15
-9
lines changed

llvm/lib/Target/RISCV/RISCVRegisterInfo.td

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,10 @@ def GPRX5 : GPRRegisterClass<(add X5)> {
260260

261261
def GPRNoX0 : GPRRegisterClass<(sub GPR, X0)>;
262262

263-
def GPRNoX0X2 : GPRRegisterClass<(sub GPR, X0, X2)>;
263+
def GPRNoX0X2 : GPRRegisterClass<(sub GPR, X0, X2)> {
264+
let DiagnosticType = "InvalidRegClassGPRNoX0X2";
265+
let DiagnosticString = "register must be a GPR excluding zero (x0) and sp (x2)";
266+
}
264267

265268
def GPRX7 : GPRRegisterClass<(add X7)>;
266269

@@ -284,7 +287,10 @@ def GPRTC : GPRRegisterClass<(add (sequence "X%u", 6, 7),
284287
(sequence "X%u", 28, 31))>;
285288
def GPRTCNonX7 : GPRRegisterClass<(sub GPRTC, X7)>;
286289

287-
def SP : GPRRegisterClass<(add X2)>;
290+
def SP : GPRRegisterClass<(add X2)> {
291+
let DiagnosticType = "InvalidRegClassSP";
292+
let DiagnosticString = "register must be sp (x2)";
293+
}
288294

289295
// Saved Registers from s0 to s7, for C.MVA01S07 instruction in Zcmp extension
290296
def SR07 : GPRRegisterClass<(add (sequence "X%u", 8, 9),

llvm/test/MC/RISCV/rv32c-invalid.s

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ c.add zero, zero, sp # CHECK: :[[@LINE]]:14: error: invalid operand for instruc
3333

3434
## GPRNoX0X2
3535
c.lui x0, 4 # CHECK: :[[@LINE]]:1: error: instruction requires the following: RVC Hint Instructions{{$}}
36-
c.lui x2, 4 # CHECK: :[[@LINE]]:7: error: invalid operand for instruction
36+
c.lui x2, 4 # CHECK: :[[@LINE]]:7: error: register must be a GPR excluding zero (x0) and sp (x2){{$}}
3737

3838
## SP
39-
c.addi4spn a0, a0, 12 # CHECK: :[[@LINE]]:17: error: invalid operand for instruction
40-
c.addi16sp t0, 16 # CHECK: :[[@LINE]]:13: error: invalid operand for instruction
39+
c.addi4spn a0, a0, 12 # CHECK: :[[@LINE]]:17: error: register must be sp (x2)
40+
c.addi16sp t0, 16 # CHECK: :[[@LINE]]:13: error: register must be sp (x2)
4141

4242
# Out of range immediates
4343

llvm/test/MC/RISCV/xwchc-invalid.s

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ qk.c.sh x8, 1(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a mul
88
qk.c.lhu x8, 64(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a multiple of 2 bytes in the range [0, 62]
99
qk.c.sh x8, 64(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a multiple of 2 bytes in the range [0, 62]
1010

11-
qk.c.lbusp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: invalid operand for instruction
12-
qk.c.sbsp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: invalid operand for instruction
11+
qk.c.lbusp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: register must be sp (x2)
12+
qk.c.sbsp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: register must be sp (x2)
1313
qk.c.lbusp x8, 32(sp) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be an integer in the range [0, 15]
1414
qk.c.sbsp x8, 32(sp) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be an integer in the range [0, 15]
1515

16-
qk.c.lhusp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: invalid operand for instruction
17-
qk.c.shsp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: invalid operand for instruction
16+
qk.c.lhusp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: register must be sp (x2)
17+
qk.c.shsp x8, 0(x8) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: register must be sp (x2)
1818
qk.c.lhusp x8, 1(sp) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a multiple of 2 bytes in the range [0, 30]
1919
qk.c.shsp x8, 1(sp) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a multiple of 2 bytes in the range [0, 30]
2020
qk.c.lhusp x8, 32(sp) # CHECK: :[[@LINE]]:{{[0-9]+}}: error: immediate must be a multiple of 2 bytes in the range [0, 30]

0 commit comments

Comments
 (0)