1 ; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
2 ; RUN: llc -mtriple=riscv32 -verify-machineinstrs < %s \
3 ; RUN: | FileCheck %s -check-prefix=RV32I
4 ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \
5 ; RUN: | FileCheck %s -check-prefix=RV64I
7 ; Materializing constants
9 ; TODO: It would be preferable if anyext constant returns were sign rather
10 ; than zero extended. See PR39092. For now, mark returns as explicitly signext
11 ; (this matches what Clang would generate for equivalent C/C++ anyway).
13 define signext i32 @zero() nounwind {
16 ; RV32I-NEXT: mv a0, zero
21 ; RV64I-NEXT: mv a0, zero
26 define signext i32 @pos_small() nounwind {
27 ; RV32I-LABEL: pos_small:
29 ; RV32I-NEXT: addi a0, zero, 2047
32 ; RV64I-LABEL: pos_small:
34 ; RV64I-NEXT: addi a0, zero, 2047
39 define signext i32 @neg_small() nounwind {
40 ; RV32I-LABEL: neg_small:
42 ; RV32I-NEXT: addi a0, zero, -2048
45 ; RV64I-LABEL: neg_small:
47 ; RV64I-NEXT: addi a0, zero, -2048
52 define signext i32 @pos_i32() nounwind {
53 ; RV32I-LABEL: pos_i32:
55 ; RV32I-NEXT: lui a0, 423811
56 ; RV32I-NEXT: addi a0, a0, -1297
59 ; RV64I-LABEL: pos_i32:
61 ; RV64I-NEXT: lui a0, 423811
62 ; RV64I-NEXT: addiw a0, a0, -1297
67 define signext i32 @neg_i32() nounwind {
68 ; RV32I-LABEL: neg_i32:
70 ; RV32I-NEXT: lui a0, 912092
71 ; RV32I-NEXT: addi a0, a0, -273
74 ; RV64I-LABEL: neg_i32:
76 ; RV64I-NEXT: lui a0, 912092
77 ; RV64I-NEXT: addiw a0, a0, -273
82 define signext i32 @pos_i32_hi20_only() nounwind {
83 ; RV32I-LABEL: pos_i32_hi20_only:
85 ; RV32I-NEXT: lui a0, 16
88 ; RV64I-LABEL: pos_i32_hi20_only:
90 ; RV64I-NEXT: lui a0, 16
95 define signext i32 @neg_i32_hi20_only() nounwind {
96 ; RV32I-LABEL: neg_i32_hi20_only:
98 ; RV32I-NEXT: lui a0, 1048560
101 ; RV64I-LABEL: neg_i32_hi20_only:
103 ; RV64I-NEXT: lui a0, 1048560
108 define i64 @imm64_1() nounwind {
109 ; RV32I-LABEL: imm64_1:
111 ; RV32I-NEXT: lui a0, 524288
112 ; RV32I-NEXT: mv a1, zero
115 ; RV64I-LABEL: imm64_1:
117 ; RV64I-NEXT: addi a0, zero, 1
118 ; RV64I-NEXT: slli a0, a0, 31
123 ; TODO: This and similar constants with all 0s in the upper bits and all 1s in
124 ; the lower bits could be lowered to addi a0, zero, -1 followed by a logical
126 define i64 @imm64_2() nounwind {
127 ; RV32I-LABEL: imm64_2:
129 ; RV32I-NEXT: addi a0, zero, -1
130 ; RV32I-NEXT: mv a1, zero
133 ; RV64I-LABEL: imm64_2:
135 ; RV64I-NEXT: addi a0, zero, 1
136 ; RV64I-NEXT: slli a0, a0, 32
137 ; RV64I-NEXT: addi a0, a0, -1
142 define i64 @imm64_3() nounwind {
143 ; RV32I-LABEL: imm64_3:
145 ; RV32I-NEXT: addi a1, zero, 1
146 ; RV32I-NEXT: mv a0, zero
149 ; RV64I-LABEL: imm64_3:
151 ; RV64I-NEXT: addi a0, zero, 1
152 ; RV64I-NEXT: slli a0, a0, 32
157 define i64 @imm64_4() nounwind {
158 ; RV32I-LABEL: imm64_4:
160 ; RV32I-NEXT: lui a1, 524288
161 ; RV32I-NEXT: mv a0, zero
164 ; RV64I-LABEL: imm64_4:
166 ; RV64I-NEXT: addi a0, zero, -1
167 ; RV64I-NEXT: slli a0, a0, 63
169 ret i64 9223372036854775808
172 define i64 @imm64_5() nounwind {
173 ; RV32I-LABEL: imm64_5:
175 ; RV32I-NEXT: lui a1, 524288
176 ; RV32I-NEXT: mv a0, zero
179 ; RV64I-LABEL: imm64_5:
181 ; RV64I-NEXT: addi a0, zero, -1
182 ; RV64I-NEXT: slli a0, a0, 63
184 ret i64 -9223372036854775808
187 define i64 @imm64_6() nounwind {
188 ; RV32I-LABEL: imm64_6:
190 ; RV32I-NEXT: lui a0, 74565
191 ; RV32I-NEXT: addi a1, a0, 1656
192 ; RV32I-NEXT: mv a0, zero
195 ; RV64I-LABEL: imm64_6:
197 ; RV64I-NEXT: lui a0, 9321
198 ; RV64I-NEXT: addiw a0, a0, -1329
199 ; RV64I-NEXT: slli a0, a0, 35
201 ret i64 1311768464867721216
204 define i64 @imm64_7() nounwind {
205 ; RV32I-LABEL: imm64_7:
207 ; RV32I-NEXT: lui a0, 45056
208 ; RV32I-NEXT: addi a0, a0, 15
209 ; RV32I-NEXT: lui a1, 458752
212 ; RV64I-LABEL: imm64_7:
214 ; RV64I-NEXT: addi a0, zero, 7
215 ; RV64I-NEXT: slli a0, a0, 36
216 ; RV64I-NEXT: addi a0, a0, 11
217 ; RV64I-NEXT: slli a0, a0, 24
218 ; RV64I-NEXT: addi a0, a0, 15
220 ret i64 8070450532432478223
223 ; TODO: it can be preferable to put constants that are expensive to materialise
224 ; into the constant pool, especially for -Os.
225 define i64 @imm64_8() nounwind {
226 ; RV32I-LABEL: imm64_8:
228 ; RV32I-NEXT: lui a0, 633806
229 ; RV32I-NEXT: addi a0, a0, -272
230 ; RV32I-NEXT: lui a1, 74565
231 ; RV32I-NEXT: addi a1, a1, 1656
234 ; RV64I-LABEL: imm64_8:
236 ; RV64I-NEXT: lui a0, 583
237 ; RV64I-NEXT: addiw a0, a0, -1875
238 ; RV64I-NEXT: slli a0, a0, 14
239 ; RV64I-NEXT: addi a0, a0, -947
240 ; RV64I-NEXT: slli a0, a0, 12
241 ; RV64I-NEXT: addi a0, a0, 1511
242 ; RV64I-NEXT: slli a0, a0, 13
243 ; RV64I-NEXT: addi a0, a0, -272
245 ret i64 1311768467463790320
248 define i64 @imm64_9() nounwind {
249 ; RV32I-LABEL: imm64_9:
251 ; RV32I-NEXT: addi a0, zero, -1
252 ; RV32I-NEXT: addi a1, zero, -1
255 ; RV64I-LABEL: imm64_9:
257 ; RV64I-NEXT: addi a0, zero, -1