Instruction::isAssociative() returns true for fmul/fadd if they are tagged "unsafe...
[oota-llvm.git] / test / Transforms / GVN / rle.ll
1 ; RUN: opt < %s -default-data-layout="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-n8:16:32" -basicaa -gvn -S -die | FileCheck %s
2 ; RUN: opt < %s -default-data-layout="E-p:32:32:32-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
3
4 ;; Trivial RLE test.
5 define i32 @test0(i32 %V, i32* %P) {
6   store i32 %V, i32* %P
7
8   %A = load i32* %P
9   ret i32 %A
10 ; CHECK: @test0
11 ; CHECK: ret i32 %V
12 }
13
14
15 ;;===----------------------------------------------------------------------===;;
16 ;; Tests for crashers
17 ;;===----------------------------------------------------------------------===;;
18
19 ;; PR5016
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*
23   %Y = load i8* %X
24   ret i8 %Y
25 }
26
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, i32 1, i1 false) nounwind
31   %tmp = load i8* bitcast (void ()* @helper to i8*)
32   %x = icmp eq i8 %tmp, 15
33   ret void
34 }
35
36
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 ;;===----------------------------------------------------------------------===;;
41
42 ;; i32 -> f32 forwarding.
43 define float @coerce_mustalias1(i32 %V, i32* %P) {
44   store i32 %V, i32* %P
45    
46   %P2 = bitcast i32* %P to float*
47
48   %A = load float* %P2
49   ret float %A
50 ; CHECK: @coerce_mustalias1
51 ; CHECK-NOT: load
52 ; CHECK: ret float 
53 }
54
55 ;; i32* -> float forwarding.
56 define float @coerce_mustalias2(i32* %V, i32** %P) {
57   store i32* %V, i32** %P
58    
59   %P2 = bitcast i32** %P to float*
60
61   %A = load float* %P2
62   ret float %A
63 ; CHECK: @coerce_mustalias2
64 ; CHECK-NOT: load
65 ; CHECK: ret float 
66 }
67
68 ;; float -> i32* forwarding.
69 define i32* @coerce_mustalias3(float %V, float* %P) {
70   store float %V, float* %P
71    
72   %P2 = bitcast float* %P to i32**
73
74   %A = load i32** %P2
75   ret i32* %A
76 ; CHECK: @coerce_mustalias3
77 ; CHECK-NOT: load
78 ; CHECK: ret i32* 
79 }
80
81 ;; i32 -> f32 load forwarding.
82 define float @coerce_mustalias4(i32* %P, i1 %cond) {
83   %A = load i32* %P
84   
85   %P2 = bitcast i32* %P to float*
86   %B = load float* %P2
87   br i1 %cond, label %T, label %F
88 T:
89   ret float %B
90   
91 F:
92   %X = bitcast i32 %A to float
93   ret float %X
94
95 ; CHECK: @coerce_mustalias4
96 ; CHECK: %A = load i32* %P
97 ; CHECK-NOT: load
98 ; CHECK: ret float
99 ; CHECK: F:
100 }
101
102 ;; i32 -> i8 forwarding
103 define i8 @coerce_mustalias5(i32 %V, i32* %P) {
104   store i32 %V, i32* %P
105    
106   %P2 = bitcast i32* %P to i8*
107
108   %A = load i8* %P2
109   ret i8 %A
110 ; CHECK: @coerce_mustalias5
111 ; CHECK-NOT: load
112 ; CHECK: ret i8
113 }
114
115 ;; i64 -> float forwarding
116 define float @coerce_mustalias6(i64 %V, i64* %P) {
117   store i64 %V, i64* %P
118    
119   %P2 = bitcast i64* %P to float*
120
121   %A = load float* %P2
122   ret float %A
123 ; CHECK: @coerce_mustalias6
124 ; CHECK-NOT: load
125 ; CHECK: ret float
126 }
127
128 ;; i64 -> i8* (32-bit) forwarding
129 define i8* @coerce_mustalias7(i64 %V, i64* %P) {
130   store i64 %V, i64* %P
131    
132   %P2 = bitcast i64* %P to i8**
133
134   %A = load i8** %P2
135   ret i8* %A
136 ; CHECK: @coerce_mustalias7
137 ; CHECK-NOT: load
138 ; CHECK: ret i8*
139 }
140
141 ; memset -> i16 forwarding.
142 define signext i16 @memset_to_i16_local(i16* %A) nounwind ssp {
143 entry:
144   %conv = bitcast i16* %A to i8* 
145   tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 1, i64 200, i32 1, i1 false)
146   %arrayidx = getelementptr inbounds i16* %A, i64 42
147   %tmp2 = load i16* %arrayidx
148   ret i16 %tmp2
149 ; CHECK: @memset_to_i16_local
150 ; CHECK-NOT: load
151 ; CHECK: ret i16 257
152 }
153
154 ; memset -> float forwarding.
155 define float @memset_to_float_local(float* %A, i8 %Val) nounwind ssp {
156 entry:
157   %conv = bitcast float* %A to i8*                ; <i8*> [#uses=1]
158   tail call void @llvm.memset.p0i8.i64(i8* %conv, i8 %Val, i64 400, i32 1, i1 false)
159   %arrayidx = getelementptr inbounds float* %A, i64 42 ; <float*> [#uses=1]
160   %tmp2 = load float* %arrayidx                   ; <float> [#uses=1]
161   ret float %tmp2
162 ; CHECK: @memset_to_float_local
163 ; CHECK-NOT: load
164 ; CHECK: zext
165 ; CHECK-NEXT: shl
166 ; CHECK-NEXT: or
167 ; CHECK-NEXT: shl
168 ; CHECK-NEXT: or
169 ; CHECK-NEXT: bitcast
170 ; CHECK-NEXT: ret float
171 }
172
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
177 T:
178   tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 1, i64 400, i32 1, i1 false)
179   br label %Cont
180   
181 F:
182   tail call void @llvm.memset.p0i8.i64(i8* %P3, i8 2, i64 400, i32 1, i1 false)
183   br label %Cont
184
185 Cont:
186   %P2 = getelementptr i16* %P, i32 4
187   %A = load i16* %P2
188   ret i16 %A
189
190 ; CHECK: @memset_to_i16_nonlocal0
191 ; CHECK: Cont:
192 ; CHECK-NEXT:   %A = phi i16 [ 514, %F ], [ 257, %T ]
193 ; CHECK-NOT: load
194 ; CHECK: ret i16 %A
195 }
196
197 @GCst = constant {i32, float, i32 } { i32 42, float 14., i32 97 }
198
199 ; memset -> float forwarding.
200 define float @memcpy_to_float_local(float* %A) nounwind ssp {
201 entry:
202   %conv = bitcast float* %A to i8*                ; <i8*> [#uses=1]
203   tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %conv, i8* bitcast ({i32, float, i32 }* @GCst to i8*), i64 12, i32 1, i1 false)
204   %arrayidx = getelementptr inbounds float* %A, i64 1 ; <float*> [#uses=1]
205   %tmp2 = load float* %arrayidx                   ; <float> [#uses=1]
206   ret float %tmp2
207 ; CHECK: @memcpy_to_float_local
208 ; CHECK-NOT: load
209 ; CHECK: ret float 1.400000e+01
210 }
211
212
213
214 ;; non-local i32/float -> i8 load forwarding.
215 define i8 @coerce_mustalias_nonlocal0(i32* %P, i1 %cond) {
216   %P2 = bitcast i32* %P to float*
217   %P3 = bitcast i32* %P to i8*
218   br i1 %cond, label %T, label %F
219 T:
220   store i32 42, i32* %P
221   br label %Cont
222   
223 F:
224   store float 1.0, float* %P2
225   br label %Cont
226
227 Cont:
228   %A = load i8* %P3
229   ret i8 %A
230
231 ; CHECK: @coerce_mustalias_nonlocal0
232 ; CHECK: Cont:
233 ; CHECK:   %A = phi i8 [
234 ; CHECK-NOT: load
235 ; CHECK: ret i8 %A
236 }
237
238
239 ;; non-local i32/float -> i8 load forwarding.  This also tests that the "P3"
240 ;; bitcast equivalence can be properly phi translated.
241 define i8 @coerce_mustalias_nonlocal1(i32* %P, i1 %cond) {
242   %P2 = bitcast i32* %P to float*
243   br i1 %cond, label %T, label %F
244 T:
245   store i32 42, i32* %P
246   br label %Cont
247   
248 F:
249   store float 1.0, float* %P2
250   br label %Cont
251
252 Cont:
253   %P3 = bitcast i32* %P to i8*
254   %A = load i8* %P3
255   ret i8 %A
256
257 ;; FIXME: This is disabled because this caused a miscompile in the llvm-gcc
258 ;; bootstrap, see r82411
259 ;
260 ; HECK: @coerce_mustalias_nonlocal1
261 ; HECK: Cont:
262 ; HECK:   %A = phi i8 [
263 ; HECK-NOT: load
264 ; HECK: ret i8 %A
265 }
266
267
268 ;; non-local i32 -> i8 partial redundancy load forwarding.
269 define i8 @coerce_mustalias_pre0(i32* %P, i1 %cond) {
270   %P3 = bitcast i32* %P to i8*
271   br i1 %cond, label %T, label %F
272 T:
273   store i32 42, i32* %P
274   br label %Cont
275   
276 F:
277   br label %Cont
278
279 Cont:
280   %A = load i8* %P3
281   ret i8 %A
282
283 ; CHECK: @coerce_mustalias_pre0
284 ; CHECK: F:
285 ; CHECK:   load i8* %P3
286 ; CHECK: Cont:
287 ; CHECK:   %A = phi i8 [
288 ; CHECK-NOT: load
289 ; CHECK: ret i8 %A
290 }
291
292 ;;===----------------------------------------------------------------------===;;
293 ;; Store -> Load  and  Load -> Load forwarding where src and dst are different
294 ;; types, and the reload is an offset from the store pointer.
295 ;;===----------------------------------------------------------------------===;;
296
297 ;; i32 -> i8 forwarding.
298 ;; PR4216
299 define i8 @coerce_offset0(i32 %V, i32* %P) {
300   store i32 %V, i32* %P
301    
302   %P2 = bitcast i32* %P to i8*
303   %P3 = getelementptr i8* %P2, i32 2
304
305   %A = load i8* %P3
306   ret i8 %A
307 ; CHECK: @coerce_offset0
308 ; CHECK-NOT: load
309 ; CHECK: ret i8
310 }
311
312 ;; non-local i32/float -> i8 load forwarding.
313 define i8 @coerce_offset_nonlocal0(i32* %P, i1 %cond) {
314   %P2 = bitcast i32* %P to float*
315   %P3 = bitcast i32* %P to i8*
316   %P4 = getelementptr i8* %P3, i32 2
317   br i1 %cond, label %T, label %F
318 T:
319   store i32 57005, i32* %P
320   br label %Cont
321   
322 F:
323   store float 1.0, float* %P2
324   br label %Cont
325
326 Cont:
327   %A = load i8* %P4
328   ret i8 %A
329
330 ; CHECK: @coerce_offset_nonlocal0
331 ; CHECK: Cont:
332 ; CHECK:   %A = phi i8 [
333 ; CHECK-NOT: load
334 ; CHECK: ret i8 %A
335 }
336
337
338 ;; non-local i32 -> i8 partial redundancy load forwarding.
339 define i8 @coerce_offset_pre0(i32* %P, i1 %cond) {
340   %P3 = bitcast i32* %P to i8*
341   %P4 = getelementptr i8* %P3, i32 2
342   br i1 %cond, label %T, label %F
343 T:
344   store i32 42, i32* %P
345   br label %Cont
346   
347 F:
348   br label %Cont
349
350 Cont:
351   %A = load i8* %P4
352   ret i8 %A
353
354 ; CHECK: @coerce_offset_pre0
355 ; CHECK: F:
356 ; CHECK:   load i8* %P4
357 ; CHECK: Cont:
358 ; CHECK:   %A = phi i8 [
359 ; CHECK-NOT: load
360 ; CHECK: ret i8 %A
361 }
362
363 define i32 @chained_load(i32** %p) {
364 block1:
365   %A = alloca i32*
366
367   %z = load i32** %p
368   store i32* %z, i32** %A
369   br i1 true, label %block2, label %block3
370
371 block2:
372  %a = load i32** %p
373  br label %block4
374
375 block3:
376   %b = load i32** %p
377   br label %block4
378
379 block4:
380   %c = load i32** %p
381   %d = load i32* %c
382   ret i32 %d
383   
384 ; CHECK: @chained_load
385 ; CHECK: %z = load i32** %p
386 ; CHECK-NOT: load
387 ; CHECK: %d = load i32* %z
388 ; CHECK-NEXT: ret i32 %d
389 }
390
391
392 declare i1 @cond() readonly
393 declare i1 @cond2() readonly
394
395 define i32 @phi_trans2() {
396 ; CHECK: @phi_trans2
397 entry:
398   %P = alloca i32, i32 400
399   br label %F1
400   
401 F1:
402   %A = phi i32 [1, %entry], [2, %F]
403   %cond2 = call i1 @cond()
404   br i1 %cond2, label %T1, label %TY
405   
406 T1:
407   %P2 = getelementptr i32* %P, i32 %A
408   %x = load i32* %P2
409   %cond = call i1 @cond2()
410   br i1 %cond, label %TX, label %F
411   
412 F:
413   %P3 = getelementptr i32* %P, i32 2
414   store i32 17, i32* %P3
415   
416   store i32 42, i32* %P2  ; Provides "P[A]".
417   br label %F1
418
419 TX:
420   ; This load should not be compiled to 'ret i32 42'.  An overly clever
421   ; implementation of GVN would see that we're returning 17 if the loop
422   ; executes once or 42 if it executes more than that, but we'd have to do
423   ; loop restructuring to expose this, and GVN shouldn't do this sort of CFG
424   ; transformation.
425   
426 ; CHECK: TX:
427 ; CHECK: ret i32 %x
428   ret i32 %x
429 TY:
430   ret i32 0
431 }
432
433 define i32 @phi_trans3(i32* %p) {
434 ; CHECK: @phi_trans3
435 block1:
436   br i1 true, label %block2, label %block3
437
438 block2:
439  store i32 87, i32* %p
440  br label %block4
441
442 block3:
443   %p2 = getelementptr i32* %p, i32 43
444   store i32 97, i32* %p2
445   br label %block4
446
447 block4:
448   %A = phi i32 [-1, %block2], [42, %block3]
449   br i1 true, label %block5, label %exit
450   
451 ; CHECK: block4:
452 ; CHECK-NEXT: %D = phi i32 [ 87, %block2 ], [ 97, %block3 ]  
453 ; CHECK-NOT: load
454
455 block5:
456   %B = add i32 %A, 1
457   br i1 true, label %block6, label %exit
458   
459 block6:
460   %C = getelementptr i32* %p, i32 %B
461   br i1 true, label %block7, label %exit
462   
463 block7:
464   %D = load i32* %C
465   ret i32 %D
466   
467 ; CHECK: block7:
468 ; CHECK-NEXT: ret i32 %D
469
470 exit:
471   ret i32 -1
472 }
473
474 define i8 @phi_trans4(i8* %p) {
475 ; CHECK: @phi_trans4
476 entry:
477   %X3 = getelementptr i8* %p, i32 192
478   store i8 192, i8* %X3
479   
480   %X = getelementptr i8* %p, i32 4
481   %Y = load i8* %X
482   br label %loop
483
484 loop:
485   %i = phi i32 [4, %entry], [192, %loop]
486   %X2 = getelementptr i8* %p, i32 %i
487   %Y2 = load i8* %X2
488   
489 ; CHECK: loop:
490 ; CHECK-NEXT: %Y2 = phi i8 [ %Y, %entry ], [ 0, %loop ]
491 ; CHECK-NOT: load i8
492   
493   %cond = call i1 @cond2()
494
495   %Z = bitcast i8 *%X3 to i32*
496   store i32 0, i32* %Z
497   br i1 %cond, label %loop, label %out
498   
499 out:
500   %R = add i8 %Y, %Y2
501   ret i8 %R
502 }
503
504 define i8 @phi_trans5(i8* %p) {
505 ; CHECK: @phi_trans5
506 entry:
507   
508   %X4 = getelementptr i8* %p, i32 2
509   store i8 19, i8* %X4
510   
511   %X = getelementptr i8* %p, i32 4
512   %Y = load i8* %X
513   br label %loop
514
515 loop:
516   %i = phi i32 [4, %entry], [3, %cont]
517   %X2 = getelementptr i8* %p, i32 %i
518   %Y2 = load i8* %X2  ; Ensure this load is not being incorrectly replaced.
519   %cond = call i1 @cond2()
520   br i1 %cond, label %cont, label %out
521
522 cont:
523   %Z = getelementptr i8* %X2, i32 -1
524   %Z2 = bitcast i8 *%Z to i32*
525   store i32 50462976, i32* %Z2  ;; (1 << 8) | (2 << 16) | (3 << 24)
526
527
528 ; CHECK: store i32
529 ; CHECK-NEXT: getelementptr i8* %p, i32 3
530 ; CHECK-NEXT: load i8*
531   br label %loop
532   
533 out:
534   %R = add i8 %Y, %Y2
535   ret i8 %R
536 }
537
538
539 ; PR6642
540 define i32 @memset_to_load() nounwind readnone {
541 entry:
542   %x = alloca [256 x i32], align 4                ; <[256 x i32]*> [#uses=2]
543   %tmp = bitcast [256 x i32]* %x to i8*           ; <i8*> [#uses=1]
544   call void @llvm.memset.p0i8.i64(i8* %tmp, i8 0, i64 1024, i32 4, i1 false)
545   %arraydecay = getelementptr inbounds [256 x i32]* %x, i32 0, i32 0 ; <i32*>
546   %tmp1 = load i32* %arraydecay                   ; <i32> [#uses=1]
547   ret i32 %tmp1
548 ; CHECK: @memset_to_load
549 ; CHECK: ret i32 0
550 }
551
552
553 ;;===----------------------------------------------------------------------===;;
554 ;; Load -> Load forwarding in partial alias case.
555 ;;===----------------------------------------------------------------------===;;
556
557 define i32 @load_load_partial_alias(i8* %P) nounwind ssp {
558 entry:
559   %0 = bitcast i8* %P to i32*
560   %tmp2 = load i32* %0
561   %add.ptr = getelementptr inbounds i8* %P, i64 1
562   %tmp5 = load i8* %add.ptr
563   %conv = zext i8 %tmp5 to i32
564   %add = add nsw i32 %tmp2, %conv
565   ret i32 %add
566
567 ; TEMPORARILYDISABLED: @load_load_partial_alias
568 ; TEMPORARILYDISABLED: load i32*
569 ; TEMPORARILYDISABLED-NOT: load
570 ; TEMPORARILYDISABLED: lshr i32 {{.*}}, 8
571 ; TEMPORARILYDISABLED-NOT: load
572 ; TEMPORARILYDISABLED: trunc i32 {{.*}} to i8
573 ; TEMPORARILYDISABLED-NOT: load
574 ; TEMPORARILYDISABLED: ret i32
575 }
576
577
578 ; Cross block partial alias case.
579 define i32 @load_load_partial_alias_cross_block(i8* %P) nounwind ssp {
580 entry:
581   %xx = bitcast i8* %P to i32*
582   %x1 = load i32* %xx, align 4
583   %cmp = icmp eq i32 %x1, 127
584   br i1 %cmp, label %land.lhs.true, label %if.end
585
586 land.lhs.true:                                    ; preds = %entry
587   %arrayidx4 = getelementptr inbounds i8* %P, i64 1
588   %tmp5 = load i8* %arrayidx4, align 1
589   %conv6 = zext i8 %tmp5 to i32
590   ret i32 %conv6
591
592 if.end:
593   ret i32 52
594 ; TEMPORARILY_DISABLED: @load_load_partial_alias_cross_block
595 ; TEMPORARILY_DISABLED: land.lhs.true:
596 ; TEMPORARILY_DISABLED-NOT: load i8
597 ; TEMPORARILY_DISABLED: ret i32 %conv6
598 }
599
600
601 ;;===----------------------------------------------------------------------===;;
602 ;; Load Widening
603 ;;===----------------------------------------------------------------------===;;
604
605 %widening1 = type { i32, i8, i8, i8, i8 }
606
607 @f = global %widening1 zeroinitializer, align 4
608
609 define i32 @test_widening1(i8* %P) nounwind ssp noredzone {
610 entry:
611   %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4
612   %conv = zext i8 %tmp to i32
613   %tmp1 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 2), align 1
614   %conv2 = zext i8 %tmp1 to i32
615   %add = add nsw i32 %conv, %conv2
616   ret i32 %add
617 ; CHECK: @test_widening1
618 ; CHECK-NOT: load
619 ; CHECK: load i16*
620 ; CHECK-NOT: load
621 ; CHECK: ret i32
622 }
623
624 define i32 @test_widening2() nounwind ssp noredzone {
625 entry:
626   %tmp = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 1), align 4
627   %conv = zext i8 %tmp to i32
628   %tmp1 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 2), align 1
629   %conv2 = zext i8 %tmp1 to i32
630   %add = add nsw i32 %conv, %conv2
631
632   %tmp2 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 3), align 2
633   %conv3 = zext i8 %tmp2 to i32
634   %add2 = add nsw i32 %add, %conv3
635
636   %tmp3 = load i8* getelementptr inbounds (%widening1* @f, i64 0, i32 4), align 1
637   %conv4 = zext i8 %tmp3 to i32
638   %add3 = add nsw i32 %add2, %conv3
639
640   ret i32 %add3
641 ; CHECK: @test_widening2
642 ; CHECK-NOT: load
643 ; CHECK: load i32*
644 ; CHECK-NOT: load
645 ; CHECK: ret i32
646 }
647
648 declare void @llvm.memset.p0i8.i64(i8* nocapture, i8, i64, i32, i1) nounwind
649
650 declare void @llvm.memcpy.p0i8.p0i8.i64(i8* nocapture, i8* nocapture, i64, i32, i1) nounwind
651
652 ;;===----------------------------------------------------------------------===;;
653 ;; Load -> Store dependency which isn't interfered with by a call that happens
654 ;; before the pointer was captured.
655 ;;===----------------------------------------------------------------------===;;
656
657 %class.X = type { [8 x i8] }
658
659 @_ZTV1X = weak_odr constant [5 x i8*] zeroinitializer
660 @_ZTV1Y = weak_odr constant [5 x i8*] zeroinitializer
661
662 declare void @use()
663 declare void @use3(i8***, i8**)
664
665 ; PR8908
666 define void @test_escape1() nounwind {
667   %x = alloca i8**, align 8
668   store i8** getelementptr inbounds ([5 x i8*]* @_ZTV1X, i64 0, i64 2), i8*** %x, align 8
669   call void @use() nounwind
670   %DEAD = load i8*** %x, align 8
671   call void @use3(i8*** %x, i8** %DEAD) nounwind
672   ret void
673 ; CHECK: test_escape1
674 ; CHECK-NOT: DEAD
675 ; CHECK: ret
676 }