1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=instcombine -S < %s | FileCheck %s
4 target datalayout = "e-m:e-p:64:64:64-i64:64-f80:128-n8:16:32:64-S128-ni:1"
6 @X = constant i32 42 ; <ptr> [#uses=2]
7 @X2 = constant i32 47 ; <ptr> [#uses=1]
8 @Y = constant [2 x { i32, float }] [ { i32, float } { i32 12, float 1.000000e+00 }, { i32, float } { i32 37, float 0x3FF3B2FEC0000000 } ] ; <ptr> [#uses=2]
9 @Z = constant [2 x { i32, float }] zeroinitializer ; <ptr> [#uses=1]
11 @GLOBAL = internal constant [4 x i32] zeroinitializer
15 ; CHECK-LABEL: @test1(
16 ; CHECK-NEXT: ret i32 42
18 %B = load i32, ptr @X ; <i32> [#uses=1]
22 define float @test2() {
23 ; CHECK-LABEL: @test2(
24 ; CHECK-NEXT: ret float 0x3FF3B2FEC0000000
26 %A = getelementptr [2 x { i32, float }], ptr @Y, i64 0, i64 1, i32 1 ; <ptr> [#uses=1]
27 %B = load float, ptr %A ; <float> [#uses=1]
32 ; CHECK-LABEL: @test3(
33 ; CHECK-NEXT: ret i32 12
35 %A = getelementptr [2 x { i32, float }], ptr @Y, i64 0, i64 0, i32 0 ; <ptr> [#uses=1]
36 %B = load i32, ptr %A ; <i32> [#uses=1]
41 ; CHECK-LABEL: @test4(
42 ; CHECK-NEXT: ret i32 0
44 %A = getelementptr [2 x { i32, float }], ptr @Z, i64 0, i64 1, i32 0 ; <ptr> [#uses=1]
45 %B = load i32, ptr %A ; <i32> [#uses=1]
49 define i32 @test5(i1 %C) {
50 ; CHECK-LABEL: @test5(
51 ; CHECK-NEXT: [[Z:%.*]] = select i1 [[C:%.*]], i32 42, i32 47
52 ; CHECK-NEXT: ret i32 [[Z]]
54 %Y = select i1 %C, ptr @X, ptr @X2 ; <ptr> [#uses=1]
55 %Z = load i32, ptr %Y ; <i32> [#uses=1]
59 define i32 @load_gep_null_inbounds(i64 %X) {
60 ; CHECK-LABEL: @load_gep_null_inbounds(
61 ; CHECK-NEXT: store i1 true, ptr poison, align 1
62 ; CHECK-NEXT: ret i32 poison
64 %V = getelementptr inbounds i32, ptr null, i64 %X
69 define i32 @load_gep_null_not_inbounds(i64 %X) {
70 ; CHECK-LABEL: @load_gep_null_not_inbounds(
71 ; CHECK-NEXT: store i1 true, ptr poison, align 1
72 ; CHECK-NEXT: ret i32 poison
74 %V = getelementptr i32, ptr null, i64 %X
79 define i32 @test7_no_null_opt(i32 %X) #0 {
80 ; CHECK-LABEL: @test7_no_null_opt(
81 ; CHECK-NEXT: [[TMP1:%.*]] = sext i32 [[X:%.*]] to i64
82 ; CHECK-NEXT: [[V:%.*]] = getelementptr i32, ptr null, i64 [[TMP1]]
83 ; CHECK-NEXT: [[R:%.*]] = load i32, ptr [[V]], align 4
84 ; CHECK-NEXT: ret i32 [[R]]
86 %V = getelementptr i32, ptr null, i32 %X ; <ptr> [#uses=1]
87 %R = load i32, ptr %V ; <i32> [#uses=1]
90 attributes #0 = { null_pointer_is_valid }
92 define i32 @test8(ptr %P) {
93 ; CHECK-LABEL: @test8(
94 ; CHECK-NEXT: store i32 1, ptr [[P:%.*]], align 4
95 ; CHECK-NEXT: ret i32 1
98 %X = load i32, ptr %P ; <i32> [#uses=1]
102 define i32 @test9(ptr %P) {
103 ; CHECK-LABEL: @test9(
104 ; CHECK-NEXT: ret i32 0
106 %X = load i32, ptr %P ; <i32> [#uses=1]
107 %Y = load i32, ptr %P ; <i32> [#uses=1]
108 %Z = sub i32 %X, %Y ; <i32> [#uses=1]
112 define i32 @test10(i1 %C.upgrd.1, ptr %P, ptr %Q) {
113 ; CHECK-LABEL: @test10(
114 ; CHECK-NEXT: br i1 [[C_UPGRD_1:%.*]], label [[T:%.*]], label [[F:%.*]]
116 ; CHECK-NEXT: store i32 1, ptr [[Q:%.*]], align 4
117 ; CHECK-NEXT: br label [[C:%.*]]
119 ; CHECK-NEXT: br label [[C]]
121 ; CHECK-NEXT: store i32 0, ptr [[P:%.*]], align 4
122 ; CHECK-NEXT: ret i32 0
124 br i1 %C.upgrd.1, label %T, label %F
133 %V = load i32, ptr %P ; <i32> [#uses=1]
137 define double @test11(ptr %p) {
138 ; CHECK-LABEL: @test11(
139 ; CHECK-NEXT: [[T0:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 8
140 ; CHECK-NEXT: store double 2.000000e+00, ptr [[T0]], align 8
141 ; CHECK-NEXT: ret double 2.000000e+00
143 %t0 = getelementptr double, ptr %p, i32 1
144 store double 2.0, ptr %t0
145 %t1 = getelementptr double, ptr %p, i32 1
146 %x = load double, ptr %t1
150 define i32 @test12(ptr %P) {
151 ; CHECK-LABEL: @test12(
152 ; CHECK-NEXT: ret i32 123
155 store i32 123, ptr %A
156 ; Cast the result of the load not the source
157 %V = load i32, ptr %A
161 define <16 x i8> @test13(<2 x i64> %x) {
162 ; CHECK-LABEL: @test13(
163 ; CHECK-NEXT: ret <16 x i8> zeroinitializer
165 %tmp = load <16 x i8>, ptr @GLOBAL
169 ; This test must not have the store of %x forwarded to the load -- there is an
170 ; intervening store if %y. However, the intervening store occurs with a different
171 ; type and size and to a different pointer value. This is ensuring that none of
172 ; those confuse the analysis into thinking that the second store does not alias
175 define i8 @test14(i8 %x, i32 %y) {
176 ; CHECK-LABEL: @test14(
177 ; CHECK-NEXT: [[A:%.*]] = alloca i32, align 4
178 ; CHECK-NEXT: store i8 [[X:%.*]], ptr [[A]], align 1
179 ; CHECK-NEXT: store i32 [[Y:%.*]], ptr [[A]], align 4
180 ; CHECK-NEXT: [[R:%.*]] = load i8, ptr [[A]], align 1
181 ; CHECK-NEXT: ret i8 [[R]]
190 @test15_global = external global i32
192 ; Same test as @test14 essentially, but using a global instead of an alloca.
194 define i8 @test15(i8 %x, i32 %y) {
195 ; CHECK-LABEL: @test15(
196 ; CHECK-NEXT: store i8 [[X:%.*]], ptr @test15_global, align 1
197 ; CHECK-NEXT: store i32 [[Y:%.*]], ptr @test15_global, align 4
198 ; CHECK-NEXT: [[R:%.*]] = load i8, ptr @test15_global, align 1
199 ; CHECK-NEXT: ret i8 [[R]]
201 store i8 %x, ptr @test15_global
202 store i32 %y, ptr @test15_global
203 %r = load i8, ptr @test15_global
207 ; Check that we canonicalize loads which are only stored to use integer types
208 ; when there is a valid integer type.
210 define void @test16(ptr %x, ptr %a, ptr %b, ptr %c) {
211 ; CHECK-LABEL: @test16(
213 ; CHECK-NEXT: [[X1:%.*]] = load float, ptr [[X:%.*]], align 4
214 ; CHECK-NEXT: store float [[X1]], ptr [[A:%.*]], align 4
215 ; CHECK-NEXT: store float [[X1]], ptr [[B:%.*]], align 4
216 ; CHECK-NEXT: [[X2:%.*]] = load float, ptr [[X]], align 4
217 ; CHECK-NEXT: store float [[X2]], ptr [[B]], align 4
218 ; CHECK-NEXT: store float [[X2]], ptr [[C:%.*]], align 4
219 ; CHECK-NEXT: ret void
223 %x1 = load float, ptr %x
224 store float %x1, ptr %a
225 store float %x1, ptr %b
227 %x2 = load float, ptr %x
228 store float %x2, ptr %b
229 %x2.cast = bitcast float %x2 to i32
230 store i32 %x2.cast, ptr %c
235 define void @test16-vect(ptr %x, ptr %a, ptr %b, ptr %c) {
236 ; CHECK-LABEL: @test16-vect(
238 ; CHECK-NEXT: [[X1:%.*]] = load <4 x i8>, ptr [[X:%.*]], align 4
239 ; CHECK-NEXT: store <4 x i8> [[X1]], ptr [[A:%.*]], align 4
240 ; CHECK-NEXT: store <4 x i8> [[X1]], ptr [[B:%.*]], align 4
241 ; CHECK-NEXT: [[X2:%.*]] = load <4 x i8>, ptr [[X]], align 4
242 ; CHECK-NEXT: store <4 x i8> [[X2]], ptr [[B]], align 4
243 ; CHECK-NEXT: store <4 x i8> [[X2]], ptr [[C:%.*]], align 4
244 ; CHECK-NEXT: ret void
248 %x1 = load <4 x i8>, ptr %x
249 store <4 x i8> %x1, ptr %a
250 store <4 x i8> %x1, ptr %b
252 %x2 = load <4 x i8>, ptr %x
253 store <4 x i8> %x2, ptr %b
254 %x2.cast = bitcast <4 x i8> %x2 to i32
255 store i32 %x2.cast, ptr %c
261 ; Check that in cases similar to @test16 we don't try to rewrite a load when
262 ; its only use is a store but it is used as the pointer to that store rather
265 define void @test17(ptr %x, i8 %y) {
266 ; CHECK-LABEL: @test17(
268 ; CHECK-NEXT: [[X_LOAD:%.*]] = load ptr, ptr [[X:%.*]], align 8
269 ; CHECK-NEXT: store i8 [[Y:%.*]], ptr [[X_LOAD]], align 1
270 ; CHECK-NEXT: ret void
273 %x.load = load ptr, ptr %x
274 store i8 %y, ptr %x.load
279 ; Check that we don't try change the type of the load by inserting a bitcast
280 ; generating invalid IR.
281 %swift.error = type opaque
282 declare void @useSwiftError(ptr swifterror)
284 define void @test18(ptr swifterror %err) {
285 ; CHECK-LABEL: @test18(
287 ; CHECK-NEXT: [[SWIFTERROR:%.*]] = alloca swifterror ptr, align 8
288 ; CHECK-NEXT: store ptr null, ptr [[SWIFTERROR]], align 8
289 ; CHECK-NEXT: call void @useSwiftError(ptr nonnull swifterror [[SWIFTERROR]])
290 ; CHECK-NEXT: [[ERR_RES:%.*]] = load ptr, ptr [[SWIFTERROR]], align 8
291 ; CHECK-NEXT: store ptr [[ERR_RES]], ptr [[ERR:%.*]], align 8
292 ; CHECK-NEXT: ret void
295 %swifterror = alloca swifterror ptr, align 8
296 store ptr null, ptr %swifterror, align 8
297 call void @useSwiftError(ptr nonnull swifterror %swifterror)
298 %err.res = load ptr, ptr %swifterror, align 8
299 store ptr %err.res, ptr %err, align 8
303 ; Make sure we preseve the type of the store to a swifterror pointer.
305 declare void @initi8(ptr)
306 define void @test19(ptr swifterror %err) {
307 ; CHECK-LABEL: @test19(
309 ; CHECK-NEXT: [[TMP:%.*]] = alloca ptr, align 8
310 ; CHECK-NEXT: call void @initi8(ptr nonnull [[TMP]])
311 ; CHECK-NEXT: [[ERR_RES:%.*]] = load ptr, ptr [[TMP]], align 8
312 ; CHECK-NEXT: store ptr [[ERR_RES]], ptr [[ERR:%.*]], align 8
313 ; CHECK-NEXT: ret void
316 %tmp = alloca ptr, align 8
317 call void @initi8(ptr %tmp)
318 %err.res = load ptr, ptr %tmp, align 8
319 store ptr %err.res, ptr %err, align 8
323 ; Make sure we don't canonicalize accesses to scalable vectors.
324 define void @test20(ptr %x, ptr %y) {
325 ; CHECK-LABEL: @test20(
326 ; CHECK-NEXT: [[X_LOAD:%.*]] = load <vscale x 4 x i8>, ptr [[X:%.*]], align 1
327 ; CHECK-NEXT: store <vscale x 4 x i8> [[X_LOAD]], ptr [[Y:%.*]], align 1
328 ; CHECK-NEXT: ret void
330 %x.load = load <vscale x 4 x i8>, ptr %x, align 1
331 store <vscale x 4 x i8> %x.load, ptr %y, align 1
336 ; Check that non-integral pointers are not coverted using inttoptr
338 declare void @use(ptr)
339 declare void @use.p1(ptr addrspace(1))
341 define i64 @test21(ptr %P) {
342 ; CHECK-LABEL: @test21(
343 ; CHECK-NEXT: [[X:%.*]] = load i64, ptr [[P:%.*]], align 8
344 ; CHECK-NEXT: [[Y_CAST:%.*]] = inttoptr i64 [[X]] to ptr
345 ; CHECK-NEXT: call void @use(ptr [[Y_CAST]])
346 ; CHECK-NEXT: ret i64 [[X]]
348 %X = load i64, ptr %P
349 %Y = load ptr, ptr %P
350 call void @use(ptr %Y)
354 define i64 @test22(ptr %P) {
355 ; CHECK-LABEL: @test22(
356 ; CHECK-NEXT: [[X:%.*]] = load i64, ptr [[P:%.*]], align 8
357 ; CHECK-NEXT: [[Y:%.*]] = load ptr addrspace(1), ptr [[P]], align 8
358 ; CHECK-NEXT: call void @use.p1(ptr addrspace(1) [[Y]])
359 ; CHECK-NEXT: ret i64 [[X]]
361 %X = load i64, ptr %P
362 %Y = load ptr addrspace(1), ptr %P
363 call void @use.p1(ptr addrspace(1) %Y)
367 declare void @use.v2.p0(<2 x ptr>)
368 declare void @use.v2.p1(<2 x ptr addrspace(1)>)
370 define <2 x i64> @test23(ptr %P) {
371 ; CHECK-LABEL: @test23(
372 ; CHECK-NEXT: [[X:%.*]] = load <2 x i64>, ptr [[P:%.*]], align 16
373 ; CHECK-NEXT: [[Y:%.*]] = load <2 x ptr>, ptr [[P]], align 16
374 ; CHECK-NEXT: call void @use.v2.p0(<2 x ptr> [[Y]])
375 ; CHECK-NEXT: ret <2 x i64> [[X]]
377 %X = load <2 x i64>, ptr %P
378 %Y = load <2 x ptr>, ptr %P
379 call void @use.v2.p0(<2 x ptr> %Y)
383 define <2 x i64> @test24(ptr %P) {
384 ; CHECK-LABEL: @test24(
385 ; CHECK-NEXT: [[X:%.*]] = load <2 x i64>, ptr [[P:%.*]], align 16
386 ; CHECK-NEXT: [[Y:%.*]] = load <2 x ptr addrspace(1)>, ptr [[P]], align 16
387 ; CHECK-NEXT: call void @use.v2.p1(<2 x ptr addrspace(1)> [[Y]])
388 ; CHECK-NEXT: ret <2 x i64> [[X]]
390 %X = load <2 x i64>, ptr %P
391 %Y = load <2 x ptr addrspace(1)>, ptr %P
392 call void @use.v2.p1(<2 x ptr addrspace(1)> %Y)
396 define i16 @load_from_zero_with_dynamic_offset(i64 %idx) {
397 ; CHECK-LABEL: @load_from_zero_with_dynamic_offset(
398 ; CHECK-NEXT: ret i16 0
400 %gep = getelementptr i16, ptr @GLOBAL, i64 %idx
401 %v = load i16, ptr %gep
405 declare ptr @llvm.strip.invariant.group.p0(ptr %p)
407 define i32 @load_via_strip_invariant_group() {
408 ; CHECK-LABEL: @load_via_strip_invariant_group(
409 ; CHECK-NEXT: ret i32 37
411 %a = call ptr @llvm.strip.invariant.group.p0(ptr @Y)
412 %b = getelementptr i8, ptr %a, i64 8
413 %d = load i32, ptr %b
417 ; TODO: For non-byte-sized vectors, current implementation assumes there is
418 ; padding to the next byte boundary between elements.
419 @foo = constant <2 x i4> <i4 u0x1, i4 u0x2>, align 8
421 define i4 @test_vector_load_i4_non_byte_sized() {
422 ; CHECK-LABEL: @test_vector_load_i4_non_byte_sized(
423 ; CHECK-NEXT: [[RES0:%.*]] = load i4, ptr @foo, align 1
424 ; CHECK-NEXT: ret i4 [[RES0]]
426 %ptr0 = getelementptr i8, ptr @foo, i64 0
427 %res0 = load i4, ptr %ptr0, align 1