1 ; RUN: opt -mtriple=riscv32-unknown-elf -S -passes=consthoist < %s | FileCheck %s
2 ; RUN: opt -mtriple=riscv64-unknown-elf -S -passes=consthoist < %s | FileCheck %s
4 ; Check that we don't hoist immediates with small values.
5 define i64 @test1(i64 %a) nounwind {
7 ; CHECK-NOT: %const = bitcast i64 2 to i64
13 ; Check that we don't hoist immediates with small values.
14 define i64 @test2(i64 %a) nounwind {
16 ; CHECK-NOT: %const = bitcast i64 2047 to i64
22 ; Check that we hoist immediates with large values.
23 define i64 @test3(i64 %a) nounwind {
25 ; CHECK: %const = bitcast i64 32766 to i64
26 %1 = mul i64 %a, 32766
27 %2 = add i64 %1, 32766
31 ; Check that we hoist immediates with very large values.
32 define i128 @test4(i128 %a) nounwind {
34 ; CHECK: %const = bitcast i128 12297829382473034410122878 to i128
35 %1 = add i128 %a, 12297829382473034410122878
36 %2 = add i128 %1, 12297829382473034410122878
40 ; Check that we hoist zext.h without Zbb.
41 define i32 @test5(i32 %a) nounwind {
43 ; CHECK: %const = bitcast i32 65535 to i32
44 %1 = and i32 %a, 65535
45 %2 = and i32 %1, 65535
49 ; Check that we don't hoist zext.h with 65535 with Zbb.
50 define i32 @test6(i32 %a) nounwind "target-features"="+zbb" {
52 ; CHECK: and i32 %a, 65535
53 %1 = and i32 %a, 65535
54 %2 = and i32 %1, 65535
58 ; Check that we hoist zext.w without Zba.
59 define i64 @test7(i64 %a) nounwind {
61 ; CHECK: %const = bitcast i64 4294967295 to i64
62 %1 = and i64 %a, 4294967295
63 %2 = and i64 %1, 4294967295
67 ; Check that we don't hoist zext.w with Zba.
68 define i64 @test8(i64 %a) nounwind "target-features"="+zba" {
70 ; CHECK: and i64 %a, 4294967295
71 %1 = and i64 %a, 4294967295
72 %2 = and i64 %1, 4294967295
76 ; Check that we don't hoist mul with negated power of 2.
77 define i64 @test9(i64 %a) nounwind {
79 ; CHECK: mul i64 %a, -4294967296
80 %1 = mul i64 %a, -4294967296
81 %2 = mul i64 %1, -4294967296
85 define i32 @test10(i32 %a, i32 %b) nounwind {
86 ; CHECK-LABEL: @test10(
87 ; CHECK: shl i32 %a, 8
88 ; CHECK: and i32 %1, 65280
89 ; CHECK: shl i32 %b, 8
90 ; CHECK: and i32 %3, 65280
92 %2 = and i32 %1, 65280
94 %4 = and i32 %3, 65280
100 define i64 @test11(i64 %a) nounwind "target-features"="+zbs" {
101 ; CHECK-LABEL: test11
102 ; CHECK: or i64 %a, 8589934592
103 %1 = or i64 %a, 8589934592 ; 1 << 33
104 %2 = or i64 %1, 8589934592 ; 1 << 33
109 define i64 @test12(i64 %a) nounwind "target-features"="+zbs" {
110 ; CHECK-LABEL: test12
111 ; CHECK: xor i64 %a, -9223372036854775808
112 %1 = xor i64 %a, -9223372036854775808 ; 1 << 63
113 %2 = xor i64 %1, -9223372036854775808 ; 1 << 63
118 define i64 @test13(i64 %a) nounwind "target-features"="+zbs" {
119 ; CHECK-LABEL: test13
120 ; CHECK: and i64 %a, -281474976710657
121 %1 = and i64 %a, -281474976710657 ; ~(1 << 48)
122 %2 = and i64 %1, -281474976710657 ; ~(1 << 48)
126 ; Check that we don't hoist mul by a power of 2.
127 define i64 @test14(i64 %a) nounwind {
128 ; CHECK-LABEL: test14
129 ; CHECK: mul i64 %a, 2048
130 %1 = mul i64 %a, 2048
131 %2 = mul i64 %1, 2048
135 ; Check that we don't hoist mul by one less than a power of 2.
136 define i64 @test15(i64 %a) nounwind {
137 ; CHECK-LABEL: test15
138 ; CHECK: mul i64 %a, 65535
139 %1 = mul i64 %a, 65535
140 %2 = mul i64 %1, 65535
144 ; Check that we don't hoist mul by one more than a power of 2.
145 define i64 @test16(i64 %a) nounwind {
146 ; CHECK-LABEL: test16
147 ; CHECK: mul i64 %a, 65537
148 %1 = mul i64 %a, 65537
149 %2 = mul i64 %1, 65537
153 ; Check that we hoist the absolute address of the stores to the entry block.
154 define void @test17(ptr %s, i32 %size) nounwind {
155 ; CHECK-LABEL: test17
156 ; CHECK: %const = bitcast i32 -1073741792 to i32
157 ; CHECK: %0 = inttoptr i32 %const to ptr
158 ; CHECK: store i32 20, ptr %0
159 ; CHECK: %1 = inttoptr i32 %const to ptr
160 ; CHECK: store i32 10, ptr %1
162 %cond = icmp eq i32 %size, 0
163 br i1 %cond, label %if.true, label %exit
165 store i32 20, ptr inttoptr (i32 -1073741792 to ptr)
168 store i32 10, ptr inttoptr (i32 -1073741792 to ptr)
172 ; Check that we hoist the absolute address of the loads to the entry block.
173 define i32 @test18(ptr %s, i32 %size) nounwind {
174 ; CHECK-LABEL: test18
175 ; CHECK: %const = bitcast i32 -1073741792 to i32
176 ; CHECK: %0 = inttoptr i32 %const to ptr
177 ; CHECK: %1 = load i32, ptr %0
178 ; CHECK: %2 = inttoptr i32 %const to ptr
179 ; CHECK: %3 = load i32, ptr %2
181 %cond = icmp eq i32 %size, 0
182 br i1 %cond, label %if.true, label %if.false
184 %0 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
187 %1 = load i32, ptr inttoptr (i32 -1073741792 to ptr)
190 %val = phi i32 [%0, %if.true], [%1, %if.false]
195 ; For addresses between [0, 2048), we can use ld/sd xN, address(zero), so don't
197 define void @test19(ptr %s, i32 %size) nounwind {
198 ; CHECK-LABEL: test19
199 ; CHECK: store i32 20, ptr inttoptr (i32 2044 to ptr)
200 ; CHECK: store i32 10, ptr inttoptr (i32 2044 to ptr)
202 %cond = icmp eq i32 %size, 0
203 br i1 %cond, label %if.true, label %exit
205 store i32 20, ptr inttoptr (i32 2044 to ptr)
208 store i32 10, ptr inttoptr (i32 2044 to ptr)
212 ; Check that we use a common base for immediates needed by a store if the
213 ; constants require more than 1 instruction.
214 define void @test20(ptr %p1, ptr %p2) {
215 ; CHECK-LABEL: test20
216 ; CHECK: %const = bitcast i32 15111111 to i32
217 ; CHECK: store i32 %const, ptr %p1, align 4
218 ; CHECK: %const_mat = add i32 %const, 1
219 ; CHECK: store i32 %const_mat, ptr %p2, align 4
220 store i32 15111111, ptr %p1, align 4
221 store i32 15111112, ptr %p2, align 4
225 define void @test21(ptr %p1, ptr %p2) {
226 ; CHECK-LABEL: define void @test21(
227 ; CHECK-SAME: ptr [[P1:%.*]], ptr [[P2:%.*]]) {
228 ; CHECK-NEXT: store i32 15111111, ptr [[P1]], align 1
229 ; CHECK-NEXT: store i32 15111112, ptr [[P2]], align 1
230 ; CHECK-NEXT: ret void
232 store i32 15111111, ptr %p1, align 1
233 store i32 15111112, ptr %p2, align 1
237 ; 0 immediates shouldn't be hoisted.
238 define void @test22(ptr %p1, ptr %p2) {
239 ; CHECK-LABEL: define void @test22(
240 ; CHECK-SAME: ptr [[P1:%.*]], ptr [[P2:%.*]]) {
241 ; CHECK-NEXT: store i64 0, ptr [[P1]], align 8
242 ; CHECK-NEXT: store i64 -1, ptr [[P2]], align 8
243 ; CHECK-NEXT: ret void
245 store i64 0, ptr %p1, align 8
246 store i64 -1, ptr %p2, align 8
250 ; 0 immediates shouldn't be hoisted.
251 define void @test23(ptr %p1, ptr %p2) {
252 ; CHECK-LABEL: define void @test23(
253 ; CHECK-SAME: ptr [[P1:%.*]], ptr [[P2:%.*]]) {
254 ; CHECK-NEXT: store i127 0, ptr [[P1]], align 8
255 ; CHECK-NEXT: store i127 -1, ptr [[P2]], align 8
256 ; CHECK-NEXT: ret void
258 store i127 0, ptr %p1, align 8
259 store i127 -1, ptr %p2, align 8
263 ; Hoisting doesn't happen for types that aren't legal.
264 define void @test24(ptr %p1, ptr %p2) {
265 ; CHECK-LABEL: define void @test24(
266 ; CHECK-SAME: ptr [[P1:%.*]], ptr [[P2:%.*]]) {
267 ; CHECK-NEXT: store i128 15111111, ptr [[P1]], align 4
268 ; CHECK-NEXT: store i128 15111112, ptr [[P2]], align 4
269 ; CHECK-NEXT: ret void
271 store i128 15111111, ptr %p1, align 4
272 store i128 15111112, ptr %p2, align 4