Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 2c34155

Browse files
committed
[RISCV] Add InstAlias definitions for add[w], and, xor, or, sll[w], srl[w], sra[w], slt and sltu with immediate
Match the GNU assembler in supporting immediate operands for these instructions even when the reg-reg mnemonic is used. Differential Revision: https://reviews.llvm.org/D50046 Patch by Kito Cheng. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@339252 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 497f7d2 commit 2c34155

File tree

6 files changed

+112
-1
lines changed

6 files changed

+112
-1
lines changed

lib/Target/RISCV/RISCVInstrInfo.td

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -550,6 +550,37 @@ def : InstAlias<"csrci $csr, $imm", (CSRRCI X0, uimm12:$csr, uimm5:$imm)>;
550550
def : InstAlias<"sfence.vma", (SFENCE_VMA X0, X0)>;
551551
def : InstAlias<"sfence.vma $rs", (SFENCE_VMA GPR:$rs, X0)>;
552552

553+
let EmitPriority = 0 in {
554+
def : InstAlias<"add $rd, $rs1, $imm12",
555+
(ADDI GPR:$rd, GPR:$rs1, simm12:$imm12)>;
556+
def : InstAlias<"and $rd, $rs1, $imm12",
557+
(ANDI GPR:$rd, GPR:$rs1, simm12:$imm12)>;
558+
def : InstAlias<"xor $rd, $rs1, $imm12",
559+
(XORI GPR:$rd, GPR:$rs1, simm12:$imm12)>;
560+
def : InstAlias<"or $rd, $rs1, $imm12",
561+
(ORI GPR:$rd, GPR:$rs1, simm12:$imm12)>;
562+
def : InstAlias<"sll $rd, $rs1, $shamt",
563+
(SLLI GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
564+
def : InstAlias<"srl $rd, $rs1, $shamt",
565+
(SRLI GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
566+
def : InstAlias<"sra $rd, $rs1, $shamt",
567+
(SRAI GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
568+
let Predicates = [IsRV64] in {
569+
def : InstAlias<"addw $rd, $rs1, $imm12",
570+
(ADDIW GPR:$rd, GPR:$rs1, simm12:$imm12)>;
571+
def : InstAlias<"sllw $rd, $rs1, $shamt",
572+
(SLLIW GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
573+
def : InstAlias<"srlw $rd, $rs1, $shamt",
574+
(SRLIW GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
575+
def : InstAlias<"sraw $rd, $rs1, $shamt",
576+
(SRAIW GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
577+
} // Predicates = [IsRV64]
578+
def : InstAlias<"slt $rd, $rs1, $imm12",
579+
(SLTI GPR:$rd, GPR:$rs1, simm12:$imm12)>;
580+
def : InstAlias<"sltu $rd, $rs1, $imm12",
581+
(SLTIU GPR:$rd, GPR:$rs1, simm12:$imm12)>;
582+
}
583+
553584
//===----------------------------------------------------------------------===//
554585
// Pseudo-instructions and codegen patterns
555586
//

test/MC/RISCV/rv32i-aliases-invalid.s

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,13 @@ li t4, foo # CHECK: :[[@LINE]]:8: error: immediate must be an integer i
1111
negw x1, x2 # CHECK: :[[@LINE]]:1: error: instruction use requires an option to be enabled
1212
sext.w x3, x4 # CHECK: :[[@LINE]]:1: error: instruction use requires an option to be enabled
1313

14+
sll x2, x3, 32 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
15+
srl x2, x3, 32 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
16+
sra x2, x3, 32 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
17+
18+
sll x2, x3, -1 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
19+
srl x2, x3, -2 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
20+
sra x2, x3, -3 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 31]
21+
1422
foo:
1523
.space 4

test/MC/RISCV/rv32i-invalid.s

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ csrrci x0, 43, %pcrel_lo(d) # CHECK: :[[@LINE]]:16: error: immediate must be an
6969
ori a0, a1, %hi(foo) # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [-2048, 2047]
7070
andi ra, sp, %pcrel_hi(123) # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [-2048, 2047]
7171
xori a2, a3, %hi(345) # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [-2048, 2047]
72+
add a1, a2, (a3) # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [-2048, 2047]
7273

7374
## uimm12
7475
csrrw a0, %lo(1), a0 # CHECK: :[[@LINE]]:11: error: immediate must be an integer in the range [0, 4095]
@@ -126,7 +127,6 @@ sraw t0, s2, zero # CHECK: :[[@LINE]]:1: error: instruction use requires an opti
126127
# Invalid operand types
127128
xori sp, 22, 220 # CHECK: :[[@LINE]]:10: error: invalid operand for instruction
128129
sub t0, t2, 1 # CHECK: :[[@LINE]]:13: error: invalid operand for instruction
129-
add a1, a2, (a3) # CHECK: :[[@LINE]]:13: error: invalid operand for instruction
130130

131131
# Too many operands
132132
add ra, zero, zero, zero # CHECK: :[[@LINE]]:21: error: invalid operand for instruction

test/MC/RISCV/rv64i-aliases-invalid.s

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,21 @@ rdinstreth x29 # CHECK: :[[@LINE]]:1: error: instruction use requires an option
88
rdcycleh x27 # CHECK: :[[@LINE]]:1: error: instruction use requires an option to be enabled
99
rdtimeh x28 # CHECK: :[[@LINE]]:1: error: instruction use requires an option to be enabled
1010

11+
sll x2, x3, 64 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
12+
srl x2, x3, 64 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
13+
sra x2, x3, 64 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
14+
15+
sll x2, x3, -1 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
16+
srl x2, x3, -2 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
17+
sra x2, x3, -3 # CHECK: :[[@LINE]]:13: error: immediate must be an integer in the range [0, 63]
18+
19+
sllw x2, x3, 32 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
20+
srlw x2, x3, 32 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
21+
sraw x2, x3, 32 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
22+
23+
sllw x2, x3, -1 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
24+
srlw x2, x3, -2 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
25+
sraw x2, x3, -3 # CHECK: :[[@LINE]]:14: error: immediate must be an integer in the range [0, 31]
26+
1127
foo:
1228
.space 8

test/MC/RISCV/rv64i-aliases-valid.s

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,21 @@ negw x31, x1
111111
# CHECK-INST: addiw t6, ra, 0
112112
# CHECK-ALIAS: sext.w t6, ra
113113
sext.w x31, x1
114+
115+
# The following aliases are accepted as input but the canonical form
116+
# of the instruction will always be printed.
117+
# CHECK-INST: addiw a2, a3, 4
118+
# CHECK-ALIAS: addiw a2, a3, 4
119+
addw a2,a3,4
120+
121+
# CHECK-INST: slliw a2, a3, 4
122+
# CHECK-ALIAS: slliw a2, a3, 4
123+
sllw a2,a3,4
124+
125+
# CHECK-INST: srliw a2, a3, 4
126+
# CHECK-ALIAS: srliw a2, a3, 4
127+
srlw a2,a3,4
128+
129+
# CHECK-INST: sraiw a2, a3, 4
130+
# CHECK-ALIAS: sraiw a2, a3, 4
131+
sraw a2,a3,4

test/MC/RISCV/rvi-aliases-valid.s

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,3 +155,41 @@ sfence.vma
155155
# CHECK-INST: sfence.vma a0, zero
156156
# CHECK-ALIAS: sfence.vma a0
157157
sfence.vma a0
158+
159+
# The following aliases are accepted as input but the canonical form
160+
# of the instruction will always be printed.
161+
# CHECK-INST: addi a2, a3, 4
162+
# CHECK-ALIAS: addi a2, a3, 4
163+
add a2,a3,4
164+
165+
# CHECK-INST: andi a2, a3, 4
166+
# CHECK-ALIAS: andi a2, a3, 4
167+
and a2,a3,4
168+
169+
# CHECK-INST: xori a2, a3, 4
170+
# CHECK-ALIAS: xori a2, a3, 4
171+
xor a2,a3,4
172+
173+
# CHECK-INST: ori a2, a3, 4
174+
# CHECK-ALIAS: ori a2, a3, 4
175+
or a2,a3,4
176+
177+
# CHECK-INST: slli a2, a3, 4
178+
# CHECK-ALIAS: slli a2, a3, 4
179+
sll a2,a3,4
180+
181+
# CHECK-INST: srli a2, a3, 4
182+
# CHECK-ALIAS: srli a2, a3, 4
183+
srl a2,a3,4
184+
185+
# CHECK-INST: srai a2, a3, 4
186+
# CHECK-ALIAS: srai a2, a3, 4
187+
sra a2,a3,4
188+
189+
# CHECK-INST: slti a2, a3, 4
190+
# CHECK-ALIAS: slti a2, a3, 4
191+
slt a2,a3,4
192+
193+
# CHECK-INST: sltiu a2, a3, 4
194+
# CHECK-ALIAS: sltiu a2, a3, 4
195+
sltu a2,a3,4

0 commit comments

Comments
 (0)