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