1 ; RUN: opt < %s -data-layout="e-p:32:32:32-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-n8:16:32" -basicaa -gvn -S -die | FileCheck %s
2 ; RUN: opt < %s -data-layout="E-p:32:32:32-p1:16:16:16-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:64:64-n32" -basicaa -gvn -S -die | FileCheck %s
5 define i32 @test0(i32 %V, i32* %P) {
10 ; CHECK-LABEL: @test0(
15 ;;===----------------------------------------------------------------------===;;
17 ;;===----------------------------------------------------------------------===;;
20 define i8 @crash0({i32, i32} %A, {i32, i32}* %P) {
21 store {i32, i32} %A, {i32, i32}* %P
22 %X = bitcast {i32, i32}* %P to i8*
27 ;; No PR filed, crashed in CaptureTracker.
28 declare void @helper()
29 define void @crash1() {
30 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* undef, i8* undef, i64 undef, i1 false) nounwind
31 %tmp = load i8, i8* bitcast (void ()* @helper to i8*)
32 %x = icmp eq i8 %tmp, 15
37 ;;===----------------------------------------------------------------------===;;
38 ;; Store -> Load and Load -> Load forwarding where src and dst are different
39 ;; types, but where the base pointer is a must alias.
40 ;;===----------------------------------------------------------------------===;;
42 ;; i32 -> f32 forwarding.
43 define float @coerce_mustalias1(i32 %V, i32* %P) {
46 %P2 = bitcast i32* %P to float*
48 %A = load float, float* %P2
50 ; CHECK-LABEL: @coerce_mustalias1(
55 ;; i32* -> float forwarding.
56 define float @coerce_mustalias2(i32* %V, i32** %P) {
57 store i32* %V, i32** %P
59 %P2 = bitcast i32** %P to float*
61 %A = load float, float* %P2
63 ; CHECK-LABEL: @coerce_mustalias2(
68 ;; float -> i32* forwarding.
69 define i32* @coerce_mustalias3(float %V, float* %P) {
70 store float %V, float* %P
72 %P2 = bitcast float* %P to i32**
74 %A = load i32*, i32** %P2
76 ; CHECK-LABEL: @coerce_mustalias3(
81 ;; i32 -> f32 load forwarding.
82 define float @coerce_mustalias4(i32* %P, i1 %cond) {
83 %A = load i32, i32* %P
85 %P2 = bitcast i32* %P to float*
86 %B = load float, float* %P2
87 br i1 %cond, label %T, label %F
92 %X = bitcast i32 %A to float
95 ; CHECK-LABEL: @coerce_mustalias4(
96 ; CHECK: %A = load i32, i32* %P
102 ;; i32 -> i8 forwarding
103 define i8 @coerce_mustalias5(i32 %V, i32* %P) {
104 store i32 %V, i32* %P
106 %P2 = bitcast i32* %P to i8*
108 %A = load i8, i8* %P2
110 ; CHECK-LABEL: @coerce_mustalias5(
115 ;; i64 -> float forwarding
116 define float @coerce_mustalias6(i64 %V, i64* %P) {
117 store i64 %V, i64* %P
119 %P2 = bitcast i64* %P to float*
121 %A = load float, float* %P2
123 ; CHECK-LABEL: @coerce_mustalias6(
128 ;; i64 -> i8* (32-bit) forwarding
129 define i8* @coerce_mustalias7(i64 %V, i64* %P) {
130 store i64 %V, i64* %P
132 %P2 = bitcast i64* %P to i8**
134 %A = load i8*, i8** %P2
136 ; CHECK-LABEL: @coerce_mustalias7(
141 ; memset -> i16 forwarding.
142 define signext i16 @memset_to_i16_local(i16* %A) nounwind ssp {
144 %conv = bitcast i16* %A to i8*
145 tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 1, i64 200, i1 false)
146 %arrayidx = getelementptr inbounds i16, i16* %A, i64 42
147 %tmp2 = load i16, i16* %arrayidx
149 ; CHECK-LABEL: @memset_to_i16_local(
154 ; memset -> float forwarding.
155 define float @memset_to_float_local(float* %A, i8 %Val) nounwind ssp {
157 %conv = bitcast float* %A to i8* ; <i8*> [#uses=1]
158 tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 %Val, i64 400, i1 false)
159 %arrayidx = getelementptr inbounds float, float* %A, i64 42 ; <float*> [#uses=1]
160 %tmp2 = load float, float* %arrayidx ; <float> [#uses=1]
162 ; CHECK-LABEL: @memset_to_float_local(
169 ; CHECK-NEXT: bitcast
170 ; CHECK-NEXT: ret float
173 ;; non-local memset -> i16 load forwarding.
174 define i16 @memset_to_i16_nonlocal0(i16* %P, i1 %cond) {
175 %P3 = bitcast i16* %P to i8*
176 br i1 %cond, label %T, label %F
178 tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 1, i64 400, i1 false)
182 tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 2, i64 400, i1 false)
186 %P2 = getelementptr i16, i16* %P, i32 4
187 %A = load i16, i16* %P2
190 ; CHECK-LABEL: @memset_to_i16_nonlocal0(
192 ; CHECK-NEXT: %A = phi i16 [ 514, %F ], [ 257, %T ]
197 @GCst = constant {i32, float, i32 } { i32 42, float 14., i32 97 }
198 @GCst_as1 = addrspace(1) constant {i32, float, i32 } { i32 42, float 14., i32 97 }
200 ; memset -> float forwarding.
201 define float @memcpy_to_float_local(float* %A) nounwind ssp {
203 %conv = bitcast float* %A to i8* ; <i8*> [#uses=1]
204 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %conv, i8* bitcast ({i32, float, i32 }* @GCst to i8*), i64 12, i1 false)
205 %arrayidx = getelementptr inbounds float, float* %A, i64 1 ; <float*> [#uses=1]
206 %tmp2 = load float, float* %arrayidx ; <float> [#uses=1]
208 ; CHECK-LABEL: @memcpy_to_float_local(
210 ; CHECK: ret float 1.400000e+01
213 ; memcpy from address space 1
214 define float @memcpy_to_float_local_as1(float* %A) nounwind ssp {
216 %conv = bitcast float* %A to i8* ; <i8*> [#uses=1]
217 tail call void @llvm.memcpy.p0i8.p1i8.i64(i8* %conv, i8 addrspace(1)* bitcast ({i32, float, i32 } addrspace(1)* @GCst_as1 to i8 addrspace(1)*), i64 12, i1 false)
218 %arrayidx = getelementptr inbounds float, float* %A, i64 1 ; <float*> [#uses=1]
219 %tmp2 = load float, float* %arrayidx ; <float> [#uses=1]
221 ; CHECK-LABEL: @memcpy_to_float_local_as1(
223 ; CHECK: ret float 1.400000e+01
226 ;; non-local i32/float -> i8 load forwarding.
227 define i8 @coerce_mustalias_nonlocal0(i32* %P, i1 %cond) {
228 %P2 = bitcast i32* %P to float*
229 %P3 = bitcast i32* %P to i8*
230 br i1 %cond, label %T, label %F
232 store i32 42, i32* %P
236 store float 1.0, float* %P2
240 %A = load i8, i8* %P3
243 ; CHECK-LABEL: @coerce_mustalias_nonlocal0(
245 ; CHECK: %A = phi i8 [
251 ;; non-local i32/float -> i8 load forwarding. This also tests that the "P3"
252 ;; bitcast equivalence can be properly phi translated.
253 define i8 @coerce_mustalias_nonlocal1(i32* %P, i1 %cond) {
254 %P2 = bitcast i32* %P to float*
255 br i1 %cond, label %T, label %F
257 store i32 42, i32* %P
261 store float 1.0, float* %P2
265 %P3 = bitcast i32* %P to i8*
266 %A = load i8, i8* %P3
269 ; CHECK-LABEL: @coerce_mustalias_nonlocal1(
271 ; CHECK: %A = phi i8 [
277 ;; non-local i32 -> i8 partial redundancy load forwarding.
278 define i8 @coerce_mustalias_pre0(i32* %P, i1 %cond) {
279 %P3 = bitcast i32* %P to i8*
280 br i1 %cond, label %T, label %F
282 store i32 42, i32* %P
289 %A = load i8, i8* %P3
292 ; CHECK-LABEL: @coerce_mustalias_pre0(
294 ; CHECK: load i8, i8* %P3
296 ; CHECK: %A = phi i8 [
301 ;;===----------------------------------------------------------------------===;;
302 ;; Store -> Load and Load -> Load forwarding where src and dst are different
303 ;; types, and the reload is an offset from the store pointer.
304 ;;===----------------------------------------------------------------------===;;
306 ;; i32 -> i8 forwarding.
308 define i8 @coerce_offset0(i32 %V, i32* %P) {
309 store i32 %V, i32* %P
311 %P2 = bitcast i32* %P to i8*
312 %P3 = getelementptr i8, i8* %P2, i32 2
314 %A = load i8, i8* %P3
316 ; CHECK-LABEL: @coerce_offset0(
321 ;; non-local i32/float -> i8 load forwarding.
322 define i8 @coerce_offset_nonlocal0(i32* %P, i1 %cond) {
323 %P2 = bitcast i32* %P to float*
324 %P3 = bitcast i32* %P to i8*
325 %P4 = getelementptr i8, i8* %P3, i32 2
326 br i1 %cond, label %T, label %F
328 store i32 57005, i32* %P
332 store float 1.0, float* %P2
336 %A = load i8, i8* %P4
339 ; CHECK-LABEL: @coerce_offset_nonlocal0(
341 ; CHECK: %A = phi i8 [
347 ;; non-local i32 -> i8 partial redundancy load forwarding.
348 define i8 @coerce_offset_pre0(i32* %P, i1 %cond) {
349 %P3 = bitcast i32* %P to i8*
350 %P4 = getelementptr i8, i8* %P3, i32 2
351 br i1 %cond, label %T, label %F
353 store i32 42, i32* %P
360 %A = load i8, i8* %P4
363 ; CHECK-LABEL: @coerce_offset_pre0(
365 ; CHECK: load i8, i8* %P4
367 ; CHECK: %A = phi i8 [
372 define i32 @chained_load(i32** %p, i32 %x, i32 %y) {
376 %z = load i32*, i32** %p
377 store i32* %z, i32** %A
378 %cmp = icmp eq i32 %x, %y
379 br i1 %cmp, label %block2, label %block3
382 %a = load i32*, i32** %p
386 %b = load i32*, i32** %p
390 %c = load i32*, i32** %p
391 %d = load i32, i32* %c
394 ; CHECK-LABEL: @chained_load(
395 ; CHECK: %z = load i32*, i32** %p
397 ; CHECK: %d = load i32, i32* %z
398 ; CHECK-NEXT: ret i32 %d
402 declare i1 @cond() readonly
403 declare i1 @cond2() readonly
405 define i32 @phi_trans2() {
406 ; CHECK-LABEL: @phi_trans2(
408 %P = alloca i32, i32 400
412 %A = phi i32 [1, %entry], [2, %F]
413 %cond2 = call i1 @cond()
414 br i1 %cond2, label %T1, label %TY
417 %P2 = getelementptr i32, i32* %P, i32 %A
418 %x = load i32, i32* %P2
419 %cond = call i1 @cond2()
420 br i1 %cond, label %TX, label %F
423 %P3 = getelementptr i32, i32* %P, i32 2
424 store i32 17, i32* %P3
426 store i32 42, i32* %P2 ; Provides "P[A]".
430 ; This load should not be compiled to 'ret i32 42'. An overly clever
431 ; implementation of GVN would see that we're returning 17 if the loop
432 ; executes once or 42 if it executes more than that, but we'd have to do
433 ; loop restructuring to expose this, and GVN shouldn't do this sort of CFG
443 define i32 @phi_trans3(i32* %p, i32 %x, i32 %y, i32 %z) {
444 ; CHECK-LABEL: @phi_trans3(
446 %cmpxy = icmp eq i32 %x, %y
447 br i1 %cmpxy, label %block2, label %block3
450 store i32 87, i32* %p
454 %p2 = getelementptr i32, i32* %p, i32 43
455 store i32 97, i32* %p2
459 %A = phi i32 [-1, %block2], [42, %block3]
460 br i1 %cmpxy, label %block5, label %exit
463 ; CHECK-NEXT: %D = phi i32 [ 87, %block2 ], [ 97, %block3 ]
468 br i1 %cmpxy, label %block6, label %exit
471 %C = getelementptr i32, i32* %p, i32 %B
472 br i1 %cmpxy, label %block7, label %exit
475 %D = load i32, i32* %C
479 ; CHECK-NEXT: ret i32 %D
485 define i8 @phi_trans4(i8* %p) {
486 ; CHECK-LABEL: @phi_trans4(
488 %X3 = getelementptr i8, i8* %p, i32 192
489 store i8 192, i8* %X3
491 %X = getelementptr i8, i8* %p, i32 4
496 %i = phi i32 [4, %entry], [192, %loop]
497 %X2 = getelementptr i8, i8* %p, i32 %i
498 %Y2 = load i8, i8* %X2
501 ; CHECK-NEXT: %Y2 = phi i8 [ %Y, %entry ], [ 0, %loop ]
504 %cond = call i1 @cond2()
506 %Z = bitcast i8 *%X3 to i32*
508 br i1 %cond, label %loop, label %out
515 define i8 @phi_trans5(i8* %p) {
516 ; CHECK-LABEL: @phi_trans5(
519 %X4 = getelementptr i8, i8* %p, i32 2
522 %X = getelementptr i8, i8* %p, i32 4
527 %i = phi i32 [4, %entry], [3, %cont]
528 %X2 = getelementptr i8, i8* %p, i32 %i
529 %Y2 = load i8, i8* %X2 ; Ensure this load is not being incorrectly replaced.
530 %cond = call i1 @cond2()
531 br i1 %cond, label %cont, label %out
534 %Z = getelementptr i8, i8* %X2, i32 -1
535 %Z2 = bitcast i8 *%Z to i32*
536 store i32 50462976, i32* %Z2 ;; (1 << 8) | (2 << 16) | (3 << 24)
540 ; CHECK-NEXT: getelementptr i8, i8* %p, i32 3
541 ; CHECK-NEXT: load i8, i8*
551 define i32 @memset_to_load() nounwind readnone {
553 %x = alloca [256 x i32], align 4 ; <[256 x i32]*> [#uses=2]
554 %tmp = bitcast [256 x i32]* %x to i8* ; <i8*> [#uses=1]
555 call void @llvm.memset.p0i8.i64(i8* align 4 %tmp, i8 0, i64 1024, i1 false)
556 %arraydecay = getelementptr inbounds [256 x i32], [256 x i32]* %x, i32 0, i32 0 ; <i32*>
557 %tmp1 = load i32, i32* %arraydecay ; <i32> [#uses=1]
559 ; CHECK-LABEL: @memset_to_load(
564 ;;===----------------------------------------------------------------------===;;
565 ;; Load -> Load forwarding in partial alias case.
566 ;;===----------------------------------------------------------------------===;;
568 define i32 @load_load_partial_alias(i8* %P) nounwind ssp {
570 %0 = bitcast i8* %P to i32*
571 %tmp2 = load i32, i32* %0
572 %add.ptr = getelementptr inbounds i8, i8* %P, i64 1
573 %tmp5 = load i8, i8* %add.ptr
574 %conv = zext i8 %tmp5 to i32
575 %add = add nsw i32 %tmp2, %conv
578 ; TEMPORARILYDISABLED-LABEL: @load_load_partial_alias(
579 ; TEMPORARILYDISABLED: load i32, i32*
580 ; TEMPORARILYDISABLED-NOT: load
581 ; TEMPORARILYDISABLED: lshr i32 {{.*}}, 8
582 ; TEMPORARILYDISABLED-NOT: load
583 ; TEMPORARILYDISABLED: trunc i32 {{.*}} to i8
584 ; TEMPORARILYDISABLED-NOT: load
585 ; TEMPORARILYDISABLED: ret i32
589 ; Cross block partial alias case.
590 define i32 @load_load_partial_alias_cross_block(i8* %P) nounwind ssp {
592 %xx = bitcast i8* %P to i32*
593 %x1 = load i32, i32* %xx, align 4
594 %cmp = icmp eq i32 %x1, 127
595 br i1 %cmp, label %land.lhs.true, label %if.end
597 land.lhs.true: ; preds = %entry
598 %arrayidx4 = getelementptr inbounds i8, i8* %P, i64 1
599 %tmp5 = load i8, i8* %arrayidx4, align 1
600 %conv6 = zext i8 %tmp5 to i32
605 ; TEMPORARILY_DISABLED-LABEL: @load_load_partial_alias_cross_block(
606 ; TEMPORARILY_DISABLED: land.lhs.true:
607 ; TEMPORARILY_DISABLED-NOT: load i8
608 ; TEMPORARILY_DISABLED: ret i32 %conv6
612 ;;===----------------------------------------------------------------------===;;
614 ;; We explicitly choose NOT to widen. And are testing to make sure we don't.
615 ;;===----------------------------------------------------------------------===;;
617 %widening1 = type { i32, i8, i8, i8, i8 }
619 @f = global %widening1 zeroinitializer, align 4
621 define i32 @test_widening1(i8* %P) nounwind ssp noredzone {
623 %tmp = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 1), align 4
624 %conv = zext i8 %tmp to i32
625 %tmp1 = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 2), align 1
626 %conv2 = zext i8 %tmp1 to i32
627 %add = add nsw i32 %conv, %conv2
629 ; CHECK-LABEL: @test_widening1(
631 ; CHECK: load i8, i8*
632 ; CHECK: load i8, i8*
637 define i32 @test_widening2() nounwind ssp noredzone {
639 %tmp = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 1), align 4
640 %conv = zext i8 %tmp to i32
641 %tmp1 = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 2), align 1
642 %conv2 = zext i8 %tmp1 to i32
643 %add = add nsw i32 %conv, %conv2
645 %tmp2 = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 3), align 2
646 %conv3 = zext i8 %tmp2 to i32
647 %add2 = add nsw i32 %add, %conv3
649 %tmp3 = load i8, i8* getelementptr inbounds (%widening1, %widening1* @f, i64 0, i32 4), align 1
650 %conv4 = zext i8 %tmp3 to i32
651 %add3 = add nsw i32 %add2, %conv3
654 ; CHECK-LABEL: @test_widening2(
656 ; CHECK: load i8, i8*
657 ; CHECK: load i8, i8*
658 ; CHECK: load i8, i8*
659 ; CHECK: load i8, i8*
664 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i1) nounwind
666 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i1) nounwind
667 declare void @llvm.memcpy.p0i8.p1i8.i64(i8* nocapture, i8 addrspace(1)* nocapture, i64, i1) nounwind
670 ;;===----------------------------------------------------------------------===;;
671 ;; Load -> Store dependency which isn't interfered with by a call that happens
672 ;; before the pointer was captured.
673 ;;===----------------------------------------------------------------------===;;
675 %class.X = type { [8 x i8] }
677 @_ZTV1X = weak_odr constant [5 x i8*] zeroinitializer
678 @_ZTV1Y = weak_odr constant [5 x i8*] zeroinitializer
681 declare void @use3(i8***, i8**)
684 define void @test_escape1() nounwind {
685 %x = alloca i8**, align 8
686 store i8** getelementptr inbounds ([5 x i8*], [5 x i8*]* @_ZTV1X, i64 0, i64 2), i8*** %x, align 8
687 call void @use() nounwind
688 %DEAD = load i8**, i8*** %x, align 8
689 call void @use3(i8*** %x, i8** %DEAD) nounwind
691 ; CHECK: test_escape1