Improve the bail-out predicate to really only kick in when phi
[oota-llvm.git] / test / Transforms / GVN / rle.ll
1 ; RUN: opt < %s -basicaa -gvn -S | 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"
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   %z = load i32** %p
364         br i1 true, label %block2, label %block3
365
366 block2:
367  %a = load i32** %p
368  br label %block4
369
370 block3:
371   %b = load i32** %p
372   br label %block4
373
374 block4:
375   %c = load i32** %p
376   %d = load i32* %c
377   ret i32 %d
378   
379 ; CHECK: @chained_load
380 ; CHECK: %z = load i32** %p
381 ; CHECK-NOT: load
382 ; CHECK: %d = load i32* %z
383 ; CHECK-NEXT: ret i32 %d
384 }
385
386
387 declare i1 @cond() readonly
388 declare i1 @cond2() readonly
389
390 define i32 @phi_trans2() {
391 ; CHECK: @phi_trans2
392 entry:
393   %P = alloca i32, i32 400
394   br label %F1
395   
396 F1:
397   %A = phi i32 [1, %entry], [2, %F]
398   %cond2 = call i1 @cond()
399   br i1 %cond2, label %T1, label %TY
400   
401 T1:
402   %P2 = getelementptr i32* %P, i32 %A
403   %x = load i32* %P2
404   %cond = call i1 @cond2()
405   br i1 %cond, label %TX, label %F
406   
407 F:
408   %P3 = getelementptr i32* %P, i32 2
409   store i32 17, i32* %P3
410   
411   store i32 42, i32* %P2  ; Provides "P[A]".
412   br label %F1
413
414 TX:
415   ; This load should not be compiled to 'ret i32 42'.  An overly clever
416   ; implementation of GVN would see that we're returning 17 if the loop
417   ; executes once or 42 if it executes more than that, but we'd have to do
418   ; loop restructuring to expose this, and GVN shouldn't do this sort of CFG
419   ; transformation.
420   
421 ; CHECK: TX:
422 ; CHECK: ret i32 %x
423   ret i32 %x
424 TY:
425   ret i32 0
426 }
427
428 define i32 @phi_trans3(i32* %p) {
429 ; CHECK: @phi_trans3
430 block1:
431   br i1 true, label %block2, label %block3
432
433 block2:
434  store i32 87, i32* %p
435  br label %block4
436
437 block3:
438   %p2 = getelementptr i32* %p, i32 43
439   store i32 97, i32* %p2
440   br label %block4
441
442 block4:
443   %A = phi i32 [-1, %block2], [42, %block3]
444   br i1 true, label %block5, label %exit
445   
446 ; CHECK: block4:
447 ; CHECK-NEXT: %D = phi i32 [ 87, %block2 ], [ 97, %block3 ]  
448 ; CHECK-NOT: load
449
450 block5:
451   %B = add i32 %A, 1
452   br i1 true, label %block6, label %exit
453   
454 block6:
455   %C = getelementptr i32* %p, i32 %B
456   br i1 true, label %block7, label %exit
457   
458 block7:
459   %D = load i32* %C
460   ret i32 %D
461   
462 ; CHECK: block7:
463 ; CHECK-NEXT: ret i32 %D
464
465 exit:
466   ret i32 -1
467 }
468
469 define i8 @phi_trans4(i8* %p) {
470 ; CHECK: @phi_trans4
471 entry:
472   %X3 = getelementptr i8* %p, i32 192
473   store i8 192, i8* %X3
474   
475   %X = getelementptr i8* %p, i32 4
476   %Y = load i8* %X
477   br label %loop
478
479 loop:
480   %i = phi i32 [4, %entry], [192, %loop]
481   %X2 = getelementptr i8* %p, i32 %i
482   %Y2 = load i8* %X2
483   
484 ; CHECK: loop:
485 ; CHECK-NEXT: %Y2 = phi i8 [ %Y, %entry ], [ 0, %loop ]
486 ; CHECK-NOT: load i8
487   
488   %cond = call i1 @cond2()
489
490   %Z = bitcast i8 *%X3 to i32*
491   store i32 0, i32* %Z
492   br i1 %cond, label %loop, label %out
493   
494 out:
495   %R = add i8 %Y, %Y2
496   ret i8 %R
497 }
498
499 define i8 @phi_trans5(i8* %p) {
500 ; CHECK: @phi_trans5
501 entry:
502   
503   %X4 = getelementptr i8* %p, i32 2
504   store i8 19, i8* %X4
505   
506   %X = getelementptr i8* %p, i32 4
507   %Y = load i8* %X
508   br label %loop
509
510 loop:
511   %i = phi i32 [4, %entry], [3, %cont]
512   %X2 = getelementptr i8* %p, i32 %i
513   %Y2 = load i8* %X2  ; Ensure this load is not being incorrectly replaced.
514   %cond = call i1 @cond2()
515   br i1 %cond, label %cont, label %out
516
517 cont:
518   %Z = getelementptr i8* %X2, i32 -1
519   %Z2 = bitcast i8 *%Z to i32*
520   store i32 50462976, i32* %Z2  ;; (1 << 8) | (2 << 16) | (3 << 24)
521
522
523 ; CHECK: store i32
524 ; CHECK-NEXT: getelementptr i8* %p, i32 3
525 ; CHECK-NEXT: load i8*
526   br label %loop
527   
528 out:
529   %R = add i8 %Y, %Y2
530   ret i8 %R
531 }
532
533
534 ; PR6642
535 define i32 @memset_to_load() nounwind readnone {
536 entry:
537   %x = alloca [256 x i32], align 4                ; <[256 x i32]*> [#uses=2]
538   %tmp = bitcast [256 x i32]* %x to i8*           ; <i8*> [#uses=1]
539   call void @llvm.memset.i64(i8* %tmp, i8 0, i64 1024, i32 4)
540   %arraydecay = getelementptr inbounds [256 x i32]* %x, i32 0, i32 0 ; <i32*>
541   %tmp1 = load i32* %arraydecay                   ; <i32> [#uses=1]
542   ret i32 %tmp1
543 ; CHECK: @memset_to_load
544 ; CHECK: ret i32 0
545 }
546
547
548 ;;===----------------------------------------------------------------------===;;
549 ;; Load -> Load forwarding in partial alias case.
550 ;;===----------------------------------------------------------------------===;;
551
552 define i32 @load_load_partial_alias(i8* %P) nounwind ssp {
553 entry:
554   %0 = bitcast i8* %P to i32*
555   %tmp2 = load i32* %0
556   %add.ptr = getelementptr inbounds i8* %P, i64 1
557   %tmp5 = load i8* %add.ptr
558   %conv = zext i8 %tmp5 to i32
559   %add = add nsw i32 %tmp2, %conv
560   ret i32 %add
561
562 ; CHECK: @load_load_partial_alias
563 ; CHECK: load i32*
564 ; CHECK-NOT: load
565 ; CHECK: lshr i32 {{.*}}, 8
566 ; CHECK-NOT: load
567 ; CHECK: trunc i32 {{.*}} to i8
568 ; CHECK-NOT: load
569 ; CHECK: ret i32
570 }
571
572
573 ; Cross block partial alias case.
574 define i32 @load_load_partial_alias_cross_block(i8* %P) nounwind ssp {
575 entry:
576   %xx = bitcast i8* %P to i32*
577   %x1 = load i32* %xx, align 4
578   %cmp = icmp eq i32 %x1, 127
579   br i1 %cmp, label %land.lhs.true, label %if.end
580
581 land.lhs.true:                                    ; preds = %entry
582   %arrayidx4 = getelementptr inbounds i8* %P, i64 1
583   %tmp5 = load i8* %arrayidx4, align 1
584   %conv6 = zext i8 %tmp5 to i32
585   ret i32 %conv6
586
587 if.end:
588   ret i32 52
589 ; CHECK: @load_load_partial_alias_cross_block
590 ; CHECK: land.lhs.true:
591 ; CHECK-NOT: load i8
592 ; CHECK: ret i32 %conv6
593 }
594
595
596