1 ; RUN: opt < %s -basicaa -gvn -S -die | FileCheck %s
3 ; 32-bit little endian target.
4 target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128-n8:16:32"
7 define i32 @test0(i32 %V, i32* %P) {
17 ;;===----------------------------------------------------------------------===;;
19 ;;===----------------------------------------------------------------------===;;
22 define i8 @crash0({i32, i32} %A, {i32, i32}* %P) {
23 store {i32, i32} %A, {i32, i32}* %P
24 %X = bitcast {i32, i32}* %P to i8*
29 ;; No PR filed, crashed in CaptureTracker.
30 declare void @helper()
31 define void @crash1() {
32 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* undef, i8* undef, i64 undef, i32 1, i1 false) nounwind
33 %tmp = load i8* bitcast (void ()* @helper to i8*)
34 %x = icmp eq i8 %tmp, 15
39 ;;===----------------------------------------------------------------------===;;
40 ;; Store -> Load and Load -> Load forwarding where src and dst are different
41 ;; types, but where the base pointer is a must alias.
42 ;;===----------------------------------------------------------------------===;;
44 ;; i32 -> f32 forwarding.
45 define float @coerce_mustalias1(i32 %V, i32* %P) {
48 %P2 = bitcast i32* %P to float*
52 ; CHECK: @coerce_mustalias1
57 ;; i32* -> float forwarding.
58 define float @coerce_mustalias2(i32* %V, i32** %P) {
59 store i32* %V, i32** %P
61 %P2 = bitcast i32** %P to float*
65 ; CHECK: @coerce_mustalias2
70 ;; float -> i32* forwarding.
71 define i32* @coerce_mustalias3(float %V, float* %P) {
72 store float %V, float* %P
74 %P2 = bitcast float* %P to i32**
78 ; CHECK: @coerce_mustalias3
83 ;; i32 -> f32 load forwarding.
84 define float @coerce_mustalias4(i32* %P, i1 %cond) {
87 %P2 = bitcast i32* %P to float*
89 br i1 %cond, label %T, label %F
94 %X = bitcast i32 %A to float
97 ; CHECK: @coerce_mustalias4
98 ; CHECK: %A = load i32* %P
104 ;; i32 -> i8 forwarding
105 define i8 @coerce_mustalias5(i32 %V, i32* %P) {
106 store i32 %V, i32* %P
108 %P2 = bitcast i32* %P to i8*
112 ; CHECK: @coerce_mustalias5
117 ;; i64 -> float forwarding
118 define float @coerce_mustalias6(i64 %V, i64* %P) {
119 store i64 %V, i64* %P
121 %P2 = bitcast i64* %P to float*
125 ; CHECK: @coerce_mustalias6
130 ;; i64 -> i8* (32-bit) forwarding
131 define i8* @coerce_mustalias7(i64 %V, i64* %P) {
132 store i64 %V, i64* %P
134 %P2 = bitcast i64* %P to i8**
138 ; CHECK: @coerce_mustalias7
143 ; memset -> i16 forwarding.
144 define signext i16 @memset_to_i16_local(i16* %A) nounwind ssp {
146 %conv = bitcast i16* %A to i8*
147 tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 1, i64 200, i32 1, i1 false)
148 %arrayidx = getelementptr inbounds i16* %A, i64 42
149 %tmp2 = load i16* %arrayidx
151 ; CHECK: @memset_to_i16_local
156 ; memset -> float forwarding.
157 define float @memset_to_float_local(float* %A, i8 %Val) nounwind ssp {
159 %conv = bitcast float* %A to i8* ; <i8*> [#uses=1]
160 tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 %Val, i64 400, i32 1, i1 false)
161 %arrayidx = getelementptr inbounds float* %A, i64 42 ; <float*> [#uses=1]
162 %tmp2 = load float* %arrayidx ; <float> [#uses=1]
164 ; CHECK: @memset_to_float_local
171 ; CHECK-NEXT: bitcast
172 ; CHECK-NEXT: ret float
175 ;; non-local memset -> i16 load forwarding.
176 define i16 @memset_to_i16_nonlocal0(i16* %P, i1 %cond) {
177 %P3 = bitcast i16* %P to i8*
178 br i1 %cond, label %T, label %F
180 tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 1, i64 400, i32 1, i1 false)
184 tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 2, i64 400, i32 1, i1 false)
188 %P2 = getelementptr i16* %P, i32 4
192 ; CHECK: @memset_to_i16_nonlocal0
194 ; CHECK-NEXT: %A = phi i16 [ 514, %F ], [ 257, %T ]
199 @GCst = constant {i32, float, i32 } { i32 42, float 14., i32 97 }
201 ; memset -> float forwarding.
202 define float @memcpy_to_float_local(float* %A) nounwind ssp {
204 %conv = bitcast float* %A to i8* ; <i8*> [#uses=1]
205 tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %conv, i8* bitcast ({i32, float, i32 }* @GCst to i8*), i64 12, i32 1, i1 false)
206 %arrayidx = getelementptr inbounds float* %A, i64 1 ; <float*> [#uses=1]
207 %tmp2 = load float* %arrayidx ; <float> [#uses=1]
209 ; CHECK: @memcpy_to_float_local
211 ; CHECK: ret float 1.400000e+01
216 ;; non-local i32/float -> i8 load forwarding.
217 define i8 @coerce_mustalias_nonlocal0(i32* %P, i1 %cond) {
218 %P2 = bitcast i32* %P to float*
219 %P3 = bitcast i32* %P to i8*
220 br i1 %cond, label %T, label %F
222 store i32 42, i32* %P
226 store float 1.0, float* %P2
233 ; CHECK: @coerce_mustalias_nonlocal0
235 ; CHECK: %A = phi i8 [
241 ;; non-local i32/float -> i8 load forwarding. This also tests that the "P3"
242 ;; bitcast equivalence can be properly phi translated.
243 define i8 @coerce_mustalias_nonlocal1(i32* %P, i1 %cond) {
244 %P2 = bitcast i32* %P to float*
245 br i1 %cond, label %T, label %F
247 store i32 42, i32* %P
251 store float 1.0, float* %P2
255 %P3 = bitcast i32* %P to i8*
259 ;; FIXME: This is disabled because this caused a miscompile in the llvm-gcc
260 ;; bootstrap, see r82411
262 ; HECK: @coerce_mustalias_nonlocal1
264 ; HECK: %A = phi i8 [
270 ;; non-local i32 -> i8 partial redundancy load forwarding.
271 define i8 @coerce_mustalias_pre0(i32* %P, i1 %cond) {
272 %P3 = bitcast i32* %P to i8*
273 br i1 %cond, label %T, label %F
275 store i32 42, i32* %P
285 ; CHECK: @coerce_mustalias_pre0
287 ; CHECK: load i8* %P3
289 ; CHECK: %A = phi i8 [
294 ;;===----------------------------------------------------------------------===;;
295 ;; Store -> Load and Load -> Load forwarding where src and dst are different
296 ;; types, and the reload is an offset from the store pointer.
297 ;;===----------------------------------------------------------------------===;;
299 ;; i32 -> i8 forwarding.
301 define i8 @coerce_offset0(i32 %V, i32* %P) {
302 store i32 %V, i32* %P
304 %P2 = bitcast i32* %P to i8*
305 %P3 = getelementptr i8* %P2, i32 2
309 ; CHECK: @coerce_offset0
314 ;; non-local i32/float -> i8 load forwarding.
315 define i8 @coerce_offset_nonlocal0(i32* %P, i1 %cond) {
316 %P2 = bitcast i32* %P to float*
317 %P3 = bitcast i32* %P to i8*
318 %P4 = getelementptr i8* %P3, i32 2
319 br i1 %cond, label %T, label %F
321 store i32 42, i32* %P
325 store float 1.0, float* %P2
332 ; CHECK: @coerce_offset_nonlocal0
334 ; CHECK: %A = phi i8 [
340 ;; non-local i32 -> i8 partial redundancy load forwarding.
341 define i8 @coerce_offset_pre0(i32* %P, i1 %cond) {
342 %P3 = bitcast i32* %P to i8*
343 %P4 = getelementptr i8* %P3, i32 2
344 br i1 %cond, label %T, label %F
346 store i32 42, i32* %P
356 ; CHECK: @coerce_offset_pre0
358 ; CHECK: load i8* %P4
360 ; CHECK: %A = phi i8 [
365 define i32 @chained_load(i32** %p) {
370 store i32* %z, i32** %A
371 br i1 true, label %block2, label %block3
386 ; CHECK: @chained_load
387 ; CHECK: %z = load i32** %p
389 ; CHECK: %d = load i32* %z
390 ; CHECK-NEXT: ret i32 %d
394 declare i1 @cond() readonly
395 declare i1 @cond2() readonly
397 define i32 @phi_trans2() {
400 %P = alloca i32, i32 400
404 %A = phi i32 [1, %entry], [2, %F]
405 %cond2 = call i1 @cond()
406 br i1 %cond2, label %T1, label %TY
409 %P2 = getelementptr i32* %P, i32 %A
411 %cond = call i1 @cond2()
412 br i1 %cond, label %TX, label %F
415 %P3 = getelementptr i32* %P, i32 2
416 store i32 17, i32* %P3
418 store i32 42, i32* %P2 ; Provides "P[A]".
422 ; This load should not be compiled to 'ret i32 42'. An overly clever
423 ; implementation of GVN would see that we're returning 17 if the loop
424 ; executes once or 42 if it executes more than that, but we'd have to do
425 ; loop restructuring to expose this, and GVN shouldn't do this sort of CFG
435 define i32 @phi_trans3(i32* %p) {
438 br i1 true, label %block2, label %block3
441 store i32 87, i32* %p
445 %p2 = getelementptr i32* %p, i32 43
446 store i32 97, i32* %p2
450 %A = phi i32 [-1, %block2], [42, %block3]
451 br i1 true, label %block5, label %exit
454 ; CHECK-NEXT: %D = phi i32 [ 87, %block2 ], [ 97, %block3 ]
459 br i1 true, label %block6, label %exit
462 %C = getelementptr i32* %p, i32 %B
463 br i1 true, label %block7, label %exit
470 ; CHECK-NEXT: ret i32 %D
476 define i8 @phi_trans4(i8* %p) {
479 %X3 = getelementptr i8* %p, i32 192
480 store i8 192, i8* %X3
482 %X = getelementptr i8* %p, i32 4
487 %i = phi i32 [4, %entry], [192, %loop]
488 %X2 = getelementptr i8* %p, i32 %i
492 ; CHECK-NEXT: %Y2 = phi i8 [ %Y, %entry ], [ 0, %loop ]
495 %cond = call i1 @cond2()
497 %Z = bitcast i8 *%X3 to i32*
499 br i1 %cond, label %loop, label %out
506 define i8 @phi_trans5(i8* %p) {
510 %X4 = getelementptr i8* %p, i32 2
513 %X = getelementptr i8* %p, i32 4
518 %i = phi i32 [4, %entry], [3, %cont]
519 %X2 = getelementptr i8* %p, i32 %i
520 %Y2 = load i8* %X2 ; Ensure this load is not being incorrectly replaced.
521 %cond = call i1 @cond2()
522 br i1 %cond, label %cont, label %out
525 %Z = getelementptr i8* %X2, i32 -1
526 %Z2 = bitcast i8 *%Z to i32*
527 store i32 50462976, i32* %Z2 ;; (1 << 8) | (2 << 16) | (3 << 24)
531 ; CHECK-NEXT: getelementptr i8* %p, i32 3
532 ; CHECK-NEXT: load i8*
542 define i32 @memset_to_load() nounwind readnone {
544 %x = alloca [256 x i32], align 4 ; <[256 x i32]*> [#uses=2]
545 %tmp = bitcast [256 x i32]* %x to i8* ; <i8*> [#uses=1]
546 call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 1024, i32 4, i1 false)
547 %arraydecay = getelementptr inbounds [256 x i32]* %x, i32 0, i32 0 ; <i32*>
548 %tmp1 = load i32* %arraydecay ; <i32> [#uses=1]
550 ; CHECK: @memset_to_load
555 ;;===----------------------------------------------------------------------===;;
556 ;; Load -> Load forwarding in partial alias case.
557 ;;===----------------------------------------------------------------------===;;
559 define i32 @load_load_partial_alias(i8* %P) nounwind ssp {
561 %0 = bitcast i8* %P to i32*
563 %add.ptr = getelementptr inbounds i8* %P, i64 1
564 %tmp5 = load i8* %add.ptr
565 %conv = zext i8 %tmp5 to i32
566 %add = add nsw i32 %tmp2, %conv
569 ; TEMPORARILYDISABLED: @load_load_partial_alias
570 ; TEMPORARILYDISABLED: load i32*
571 ; TEMPORARILYDISABLED-NOT: load
572 ; TEMPORARILYDISABLED: lshr i32 {{.*}}, 8
573 ; TEMPORARILYDISABLED-NOT: load
574 ; TEMPORARILYDISABLED: trunc i32 {{.*}} to i8
575 ; TEMPORARILYDISABLED-NOT: load
576 ; TEMPORARILYDISABLED: ret i32
580 ; Cross block partial alias case.
581 define i32 @load_load_partial_alias_cross_block(i8* %P) nounwind ssp {
583 %xx = bitcast i8* %P to i32*
584 %x1 = load i32* %xx, align 4
585 %cmp = icmp eq i32 %x1, 127
586 br i1 %cmp, label %land.lhs.true, label %if.end
588 land.lhs.true: ; preds = %entry
589 %arrayidx4 = getelementptr inbounds i8* %P, i64 1
590 %tmp5 = load i8* %arrayidx4, align 1
591 %conv6 = zext i8 %tmp5 to i32
596 ; TEMPORARILY_DISABLED: @load_load_partial_alias_cross_block
597 ; TEMPORARILY_DISABLED: land.lhs.true:
598 ; TEMPORARILY_DISABLED-NOT: load i8
599 ; TEMPORARILY_DISABLED: ret i32 %conv6
603 ;;===----------------------------------------------------------------------===;;
605 ;;===----------------------------------------------------------------------===;;
607 %widening1 = type { i32, i8, i8, i8, i8 }
609 @f = global %widening1 zeroinitializer, align 4
611 define i32 @test_widening1(i8* %P) nounwind ssp noredzone {
613 %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4
614 %conv = zext i8 %tmp to i32
615 %tmp1 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 2), align 1
616 %conv2 = zext i8 %tmp1 to i32
617 %add = add nsw i32 %conv, %conv2
619 ; CHECK: @test_widening1
626 define i32 @test_widening2() nounwind ssp noredzone {
628 %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4
629 %conv = zext i8 %tmp to i32
630 %tmp1 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 2), align 1
631 %conv2 = zext i8 %tmp1 to i32
632 %add = add nsw i32 %conv, %conv2
634 %tmp2 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 3), align 2
635 %conv3 = zext i8 %tmp2 to i32
636 %add2 = add nsw i32 %add, %conv3
638 %tmp3 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 4), align 1
639 %conv4 = zext i8 %tmp3 to i32
640 %add3 = add nsw i32 %add2, %conv3
643 ; CHECK: @test_widening2
650 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
652 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
654 ;;===----------------------------------------------------------------------===;;
655 ;; Load -> Store dependency which isn't interfered with by a call that happens
656 ;; before the pointer was captured.
657 ;;===----------------------------------------------------------------------===;;
659 %class.X = type { [8 x i8] }
661 @_ZTV1X = weak_odr constant [5 x i8*] zeroinitializer
662 @_ZTV1Y = weak_odr constant [5 x i8*] zeroinitializer
665 declare void @use3(i8***, i8**)
668 define void @test_escape1() nounwind {
669 %x = alloca i8**, align 8
670 store i8** getelementptr inbounds ([5 x i8*]* @_ZTV1X, i64 0, i64 2), i8*** %x, align 8
671 call void @use() nounwind
672 %DEAD = load i8*** %x, align 8
673 call void @use3(i8*** %x, i8** %DEAD) nounwind
675 ; CHECK: test_escape1