blob: d9e757cd10fc06ba104436ee56db250a9bd44df2 [file] [log] [blame]
James Molloya9290632017-05-25 12:51:11 +00001; RUN: opt < %s -gvn-sink -simplifycfg -simplifycfg-sink-common=false -S | FileCheck %s
2
3define zeroext i1 @test1(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
4entry:
5 br i1 %flag, label %if.then, label %if.else
6
7; CHECK-LABEL: test1
8; CHECK: add
9; CHECK: select
10; CHECK: icmp
11; CHECK-NOT: br
12if.then:
13 %cmp = icmp uge i32 %blksA, %nblks
14 %frombool1 = zext i1 %cmp to i8
15 br label %if.end
16
17if.else:
18 %add = add i32 %nblks, %blksB
19 %cmp2 = icmp ule i32 %add, %blksA
20 %frombool3 = zext i1 %cmp2 to i8
21 br label %if.end
22
23if.end:
24 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
25 %tobool4 = icmp ne i8 %obeys.0, 0
26 ret i1 %tobool4
27}
28
29define zeroext i1 @test2(i1 zeroext %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
30entry:
31 br i1 %flag, label %if.then, label %if.else
32
33; CHECK-LABEL: test2
34; CHECK: add
35; CHECK: select
36; CHECK: icmp
37; CHECK-NOT: br
38if.then:
39 %cmp = icmp uge i32 %blksA, %nblks
40 %frombool1 = zext i1 %cmp to i8
41 br label %if.end
42
43if.else:
44 %add = add i32 %nblks, %blksB
45 %cmp2 = icmp uge i32 %blksA, %add
46 %frombool3 = zext i1 %cmp2 to i8
47 br label %if.end
48
49if.end:
50 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.else ]
51 %tobool4 = icmp ne i8 %obeys.0, 0
52 ret i1 %tobool4
53}
54
55declare i32 @foo(i32, i32) nounwind readnone
56
57define i32 @test3(i1 zeroext %flag, i32 %x, i32 %y) {
58entry:
59 br i1 %flag, label %if.then, label %if.else
60
61if.then:
62 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
63 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
64 br label %if.end
65
66if.else:
67 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
68 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
69 br label %if.end
70
71if.end:
72 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
73 %yy = phi i32 [ %y0, %if.then ], [ %y1, %if.else ]
74 %ret = add i32 %xx, %yy
75 ret i32 %ret
76}
77
78; CHECK-LABEL: test3
79; CHECK: select
80; CHECK: call
81; CHECK: call
82; CHECK: add
83; CHECK-NOT: br
84
85define i32 @test4(i1 zeroext %flag, i32 %x, i32* %y) {
86entry:
87 br i1 %flag, label %if.then, label %if.else
88
89if.then:
90 %a = add i32 %x, 5
91 store i32 %a, i32* %y
92 br label %if.end
93
94if.else:
95 %b = add i32 %x, 7
96 store i32 %b, i32* %y
97 br label %if.end
98
99if.end:
100 ret i32 1
101}
102
103; CHECK-LABEL: test4
104; CHECK: select
105; CHECK: store
106; CHECK-NOT: store
107
108define i32 @test5(i1 zeroext %flag, i32 %x, i32* %y) {
109entry:
110 br i1 %flag, label %if.then, label %if.else
111
112if.then:
113 %a = add i32 %x, 5
114 store volatile i32 %a, i32* %y
115 br label %if.end
116
117if.else:
118 %b = add i32 %x, 7
119 store i32 %b, i32* %y
120 br label %if.end
121
122if.end:
123 ret i32 1
124}
125
126; CHECK-LABEL: test5
127; CHECK: store volatile
128; CHECK: store
129
130define i32 @test6(i1 zeroext %flag, i32 %x, i32* %y) {
131entry:
132 br i1 %flag, label %if.then, label %if.else
133
134if.then:
135 %a = add i32 %x, 5
136 store volatile i32 %a, i32* %y
137 br label %if.end
138
139if.else:
140 %b = add i32 %x, 7
141 store volatile i32 %b, i32* %y
142 br label %if.end
143
144if.end:
145 ret i32 1
146}
147
148; CHECK-LABEL: test6
149; CHECK: select
150; CHECK: store volatile
151; CHECK-NOT: store
152
153define i32 @test7(i1 zeroext %flag, i32 %x, i32* %y) {
154entry:
155 br i1 %flag, label %if.then, label %if.else
156
157if.then:
158 %z = load volatile i32, i32* %y
159 %a = add i32 %z, 5
160 store volatile i32 %a, i32* %y
161 br label %if.end
162
163if.else:
164 %w = load volatile i32, i32* %y
165 %b = add i32 %w, 7
166 store volatile i32 %b, i32* %y
167 br label %if.end
168
169if.end:
170 ret i32 1
171}
172
173; CHECK-LABEL: test7
174; CHECK-DAG: select
175; CHECK-DAG: load volatile
176; CHECK: store volatile
177; CHECK-NOT: load
178; CHECK-NOT: store
179
180; The extra store in %if.then means %z and %w are not equivalent.
181define i32 @test9(i1 zeroext %flag, i32 %x, i32* %y, i32* %p) {
182entry:
183 br i1 %flag, label %if.then, label %if.else
184
185if.then:
186 store i32 7, i32* %p
187 %z = load volatile i32, i32* %y
188 store i32 6, i32* %p
189 %a = add i32 %z, 5
190 store volatile i32 %a, i32* %y
191 br label %if.end
192
193if.else:
194 %w = load volatile i32, i32* %y
195 %b = add i32 %w, 7
196 store volatile i32 %b, i32* %y
197 br label %if.end
198
199if.end:
200 ret i32 1
201}
202
203; CHECK-LABEL: test9
204; CHECK: add
205; CHECK: add
206
207%struct.anon = type { i32, i32 }
208
209; The GEP indexes a struct type so cannot have a variable last index.
210define i32 @test10(i1 zeroext %flag, i32 %x, i32* %y, %struct.anon* %s) {
211entry:
212 br i1 %flag, label %if.then, label %if.else
213
214if.then:
215 %dummy = add i32 %x, 5
216 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
217 store volatile i32 %x, i32* %gepa
218 br label %if.end
219
220if.else:
221 %dummy1 = add i32 %x, 6
222 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
223 store volatile i32 %x, i32* %gepb
224 br label %if.end
225
226if.end:
227 ret i32 1
228}
229
230; CHECK-LABEL: test10
231; CHECK: getelementptr
232; CHECK: store volatile
233; CHECK: getelementptr
234; CHECK: store volatile
235
236; The shufflevector's mask operand cannot be merged in a PHI.
237define i32 @test11(i1 zeroext %flag, i32 %w, <2 x i32> %x, <2 x i32> %y) {
238entry:
239 br i1 %flag, label %if.then, label %if.else
240
241if.then:
242 %dummy = add i32 %w, 5
243 %sv1 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 0, i32 1>
244 br label %if.end
245
246if.else:
247 %dummy1 = add i32 %w, 6
248 %sv2 = shufflevector <2 x i32> %x, <2 x i32> %y, <2 x i32> <i32 1, i32 0>
249 br label %if.end
250
251if.end:
252 %p = phi <2 x i32> [ %sv1, %if.then ], [ %sv2, %if.else ]
253 ret i32 1
254}
255
256; CHECK-LABEL: test11
257; CHECK: shufflevector
258; CHECK: shufflevector
259
260; We can't common an intrinsic!
261define i32 @test12(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
262entry:
263 br i1 %flag, label %if.then, label %if.else
264
265if.then:
266 %dummy = add i32 %w, 5
267 %sv1 = call i32 @llvm.ctlz.i32(i32 %x)
268 br label %if.end
269
270if.else:
271 %dummy1 = add i32 %w, 6
272 %sv2 = call i32 @llvm.cttz.i32(i32 %x)
273 br label %if.end
274
275if.end:
276 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
277 ret i32 1
278}
279
280declare i32 @llvm.ctlz.i32(i32 %x) readnone
281declare i32 @llvm.cttz.i32(i32 %x) readnone
282
283; CHECK-LABEL: test12
284; CHECK: call i32 @llvm.ctlz
285; CHECK: call i32 @llvm.cttz
286
287; The TBAA metadata should be properly combined.
288define i32 @test13(i1 zeroext %flag, i32 %x, i32* %y) {
289entry:
290 br i1 %flag, label %if.then, label %if.else
291
292if.then:
293 %z = load volatile i32, i32* %y
294 %a = add i32 %z, 5
295 store volatile i32 %a, i32* %y, !tbaa !3
296 br label %if.end
297
298if.else:
299 %w = load volatile i32, i32* %y
300 %b = add i32 %w, 7
301 store volatile i32 %b, i32* %y, !tbaa !4
302 br label %if.end
303
304if.end:
305 ret i32 1
306}
307
308!0 = !{ !"an example type tree" }
309!1 = !{ !"int", !0 }
310!2 = !{ !"float", !0 }
311!3 = !{ !"const float", !2, i64 0 }
312!4 = !{ !"special float", !2, i64 1 }
313
314; CHECK-LABEL: test13
315; CHECK-DAG: select
316; CHECK-DAG: load volatile
317; CHECK: store volatile {{.*}}, !tbaa !0
318; CHECK-NOT: load
319; CHECK-NOT: store
320
321; The call should be commoned.
322define i32 @test13a(i1 zeroext %flag, i32 %w, i32 %x, i32 %y) {
323entry:
324 br i1 %flag, label %if.then, label %if.else
325
326if.then:
327 %sv1 = call i32 @bar(i32 %x)
328 br label %if.end
329
330if.else:
331 %sv2 = call i32 @bar(i32 %y)
332 br label %if.end
333
334if.end:
335 %p = phi i32 [ %sv1, %if.then ], [ %sv2, %if.else ]
336 ret i32 1
337}
338declare i32 @bar(i32)
339
340; CHECK-LABEL: test13a
341; CHECK: %[[x:.*]] = select i1 %flag
342; CHECK: call i32 @bar(i32 %[[x]])
343
344; The load should be commoned.
345define i32 @test14(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
346entry:
347 br i1 %flag, label %if.then, label %if.else
348
349if.then:
350 %dummy = add i32 %x, 1
351 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
352 %sv1 = load i32, i32* %gepa
353 %cmp1 = icmp eq i32 %sv1, 56
354 br label %if.end
355
356if.else:
357 %dummy2 = add i32 %x, 4
358 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
359 %sv2 = load i32, i32* %gepb
360 %cmp2 = icmp eq i32 %sv2, 57
361 br label %if.end
362
363if.end:
364 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
365 ret i32 1
366}
367
368; CHECK-LABEL: test14
369; CHECK: getelementptr
370; CHECK: load
371; CHECK-NOT: load
372
373; The load should be commoned.
374define i32 @test15(i1 zeroext %flag, i32 %w, i32 %x, i32 %y, %struct.anon* %s) {
375entry:
376 br i1 %flag, label %if.then, label %if.else
377
378if.then:
379 %dummy = add i32 %x, 1
380 %gepa = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 0
381 %sv1 = load i32, i32* %gepa
382 %ext1 = zext i32 %sv1 to i64
383 %cmp1 = icmp eq i64 %ext1, 56
384 br label %if.end
385
386if.else:
387 %dummy2 = add i32 %x, 4
388 %gepb = getelementptr inbounds %struct.anon, %struct.anon* %s, i32 0, i32 1
389 %sv2 = load i32, i32* %gepb
390 %ext2 = zext i32 %sv2 to i64
391 %cmp2 = icmp eq i64 %ext2, 56
392 br label %if.end
393
394if.end:
395 %p = phi i1 [ %cmp1, %if.then ], [ %cmp2, %if.else ]
396 ret i32 1
397}
398
399; CHECK-LABEL: test15
400; CHECK: getelementptr
401; CHECK: load
402; CHECK-NOT: load
403
404define zeroext i1 @test_crash(i1 zeroext %flag, i32* %i4, i32* %m, i32* %n) {
405entry:
406 br i1 %flag, label %if.then, label %if.else
407
408if.then:
409 %tmp1 = load i32, i32* %i4
410 %tmp2 = add i32 %tmp1, -1
411 store i32 %tmp2, i32* %i4
412 br label %if.end
413
414if.else:
415 %tmp3 = load i32, i32* %m
416 %tmp4 = load i32, i32* %n
417 %tmp5 = add i32 %tmp3, %tmp4
418 store i32 %tmp5, i32* %i4
419 br label %if.end
420
421if.end:
422 ret i1 true
423}
424
425; CHECK-LABEL: test_crash
426; No checks for test_crash - just ensure it doesn't crash!
427
428define zeroext i1 @test16(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
429
430entry:
431 br i1 %flag, label %if.then, label %if.else
432
433if.then:
434 %cmp = icmp uge i32 %blksA, %nblks
435 %frombool1 = zext i1 %cmp to i8
436 br label %if.end
437
438if.else:
439 br i1 %flag2, label %if.then2, label %if.end
440
441if.then2:
442 %add = add i32 %nblks, %blksB
443 %cmp2 = icmp ule i32 %add, %blksA
444 %frombool3 = zext i1 %cmp2 to i8
445 br label %if.end
446
447if.end:
448 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ 0, %if.else ]
449 %tobool4 = icmp ne i8 %obeys.0, 0
450 ret i1 %tobool4
451}
452
453; CHECK-LABEL: test16
454; CHECK: zext
455; CHECK: zext
456
457define zeroext i1 @test16a(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks, i8* %p) {
458
459entry:
460 br i1 %flag, label %if.then, label %if.else
461
462if.then:
463 %cmp = icmp uge i32 %blksA, %nblks
464 %frombool1 = zext i1 %cmp to i8
465 %b1 = sext i8 %frombool1 to i32
466 %b2 = trunc i32 %b1 to i8
467 store i8 %b2, i8* %p
468 br label %if.end
469
470if.else:
471 br i1 %flag2, label %if.then2, label %if.end
472
473if.then2:
474 %add = add i32 %nblks, %blksB
475 %cmp2 = icmp ule i32 %add, %blksA
476 %frombool3 = zext i1 %cmp2 to i8
477 %a1 = sext i8 %frombool3 to i32
478 %a2 = trunc i32 %a1 to i8
479 store i8 %a2, i8* %p
480 br label %if.end
481
482if.end:
483 ret i1 true
484}
485
486; CHECK-LABEL: test16a
487; CHECK: zext
488; CHECK-NOT: zext
489
490define zeroext i1 @test17(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
491entry:
492 switch i32 %flag, label %if.end [
493 i32 0, label %if.then
494 i32 1, label %if.then2
495 ]
496
497if.then:
498 %cmp = icmp uge i32 %blksA, %nblks
499 %frombool1 = call i8 @i1toi8(i1 %cmp)
500 %a1 = sext i8 %frombool1 to i32
501 %a2 = trunc i32 %a1 to i8
502 br label %if.end
503
504if.then2:
505 %add = add i32 %nblks, %blksB
506 %cmp2 = icmp ule i32 %add, %blksA
507 %frombool3 = call i8 @i1toi8(i1 %cmp2)
508 %b1 = sext i8 %frombool3 to i32
509 %b2 = trunc i32 %b1 to i8
510 br label %if.end
511
512if.end:
513 %obeys.0 = phi i8 [ %a2, %if.then ], [ %b2, %if.then2 ], [ 0, %entry ]
514 %tobool4 = icmp ne i8 %obeys.0, 0
515 ret i1 %tobool4
516}
517declare i8 @i1toi8(i1)
518
519; FIXME: DISABLED - we don't consider this profitable. We should
520; - Consider argument setup/return mov'ing for calls, like InlineCost does.
521; - Consider the removal of the %obeys.0 PHI (zero PHI movement overall)
522
523; DISABLED-CHECK-LABEL: test17
524; DISABLED-CHECK: if.then:
525; DISABLED-CHECK-NEXT: icmp uge
526; DISABLED-CHECK-NEXT: br label %[[x:.*]]
527
528; DISABLED-CHECK: if.then2:
529; DISABLED-CHECK-NEXT: add
530; DISABLED-CHECK-NEXT: icmp ule
531; DISABLED-CHECK-NEXT: br label %[[x]]
532
533; DISABLED-CHECK: [[x]]:
534; DISABLED-CHECK-NEXT: %[[y:.*]] = phi i1 [ %cmp
535; DISABLED-CHECK-NEXT: %[[z:.*]] = call i8 @i1toi8(i1 %[[y]])
536; DISABLED-CHECK-NEXT: br label %if.end
537
538; DISABLED-CHECK: if.end:
539; DISABLED-CHECK-NEXT: phi i8
540; DISABLED-CHECK-DAG: [ %[[z]], %[[x]] ]
541; DISABLED-CHECK-DAG: [ 0, %entry ]
542
543define zeroext i1 @test18(i32 %flag, i32 %blksA, i32 %blksB, i32 %nblks) {
544entry:
545 switch i32 %flag, label %if.then3 [
546 i32 0, label %if.then
547 i32 1, label %if.then2
548 ]
549
550if.then:
551 %cmp = icmp uge i32 %blksA, %nblks
552 %frombool1 = zext i1 %cmp to i8
553 br label %if.end
554
555if.then2:
556 %add = add i32 %nblks, %blksB
557 %cmp2 = icmp ule i32 %add, %blksA
558 %frombool3 = zext i1 %cmp2 to i8
559 br label %if.end
560
561if.then3:
562 %add2 = add i32 %nblks, %blksA
563 %cmp3 = icmp ule i32 %add2, %blksA
564 %frombool4 = zext i1 %cmp3 to i8
565 br label %if.end
566
567if.end:
568 %obeys.0 = phi i8 [ %frombool1, %if.then ], [ %frombool3, %if.then2 ], [ %frombool4, %if.then3 ]
569 %tobool4 = icmp ne i8 %obeys.0, 0
570 ret i1 %tobool4
571}
572
573; CHECK-LABEL: test18
574; CHECK: if.end:
575; CHECK-NEXT: %[[x:.*]] = phi i1
576; CHECK-DAG: [ %cmp, %if.then ]
577; CHECK-DAG: [ %cmp2, %if.then2 ]
578; CHECK-DAG: [ %cmp3, %if.then3 ]
579; CHECK-NEXT: zext i1 %[[x]] to i8
580
581; The phi is confusing - both add instructions are used by it, but
582; not on their respective unconditional arcs. It should not be
583; optimized.
584define void @test_pr30292(i1 %cond, i1 %cond2, i32 %a, i32 %b) {
585entry:
586 %add1 = add i32 %a, 1
587 br label %succ
588
589one:
590 br i1 %cond, label %two, label %succ
591
592two:
593 call void @g()
594 %add2 = add i32 %a, 1
595 br label %succ
596
597succ:
598 %p = phi i32 [ 0, %entry ], [ %add1, %one ], [ %add2, %two ]
599 br label %one
600}
601declare void @g()
602
603; CHECK-LABEL: test_pr30292
604; CHECK: phi i32 [ 0, %entry ], [ %add1, %succ ], [ %add2, %two ]
605
606define zeroext i1 @test_pr30244(i1 zeroext %flag, i1 zeroext %flag2, i32 %blksA, i32 %blksB, i32 %nblks) {
607
608entry:
609 %p = alloca i8
610 br i1 %flag, label %if.then, label %if.else
611
612if.then:
613 %cmp = icmp uge i32 %blksA, %nblks
614 %frombool1 = zext i1 %cmp to i8
615 store i8 %frombool1, i8* %p
616 br label %if.end
617
618if.else:
619 br i1 %flag2, label %if.then2, label %if.end
620
621if.then2:
622 %add = add i32 %nblks, %blksB
623 %cmp2 = icmp ule i32 %add, %blksA
624 %frombool3 = zext i1 %cmp2 to i8
625 store i8 %frombool3, i8* %p
626 br label %if.end
627
628if.end:
629 ret i1 true
630}
631
632; CHECK-LABEL: @test_pr30244
633; CHECK: store
634; CHECK-NOT: store
635
636define i32 @test_pr30373a(i1 zeroext %flag, i32 %x, i32 %y) {
637entry:
638 br i1 %flag, label %if.then, label %if.else
639
640if.then:
641 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
642 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
643 %z0 = lshr i32 %y0, 8
644 br label %if.end
645
646if.else:
647 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
648 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
649 %z1 = lshr exact i32 %y1, 8
650 br label %if.end
651
652if.end:
653 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
654 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
655 %ret = add i32 %xx, %yy
656 ret i32 %ret
657}
658
659; CHECK-LABEL: test_pr30373a
660; CHECK: lshr
661; CHECK-NOT: exact
662; CHECK: }
663
664define i32 @test_pr30373b(i1 zeroext %flag, i32 %x, i32 %y) {
665entry:
666 br i1 %flag, label %if.then, label %if.else
667
668if.then:
669 %x0 = call i32 @foo(i32 %x, i32 0) nounwind readnone
670 %y0 = call i32 @foo(i32 %x, i32 1) nounwind readnone
671 %z0 = lshr exact i32 %y0, 8
672 br label %if.end
673
674if.else:
675 %x1 = call i32 @foo(i32 %y, i32 0) nounwind readnone
676 %y1 = call i32 @foo(i32 %y, i32 1) nounwind readnone
677 %z1 = lshr i32 %y1, 8
678 br label %if.end
679
680if.end:
681 %xx = phi i32 [ %x0, %if.then ], [ %x1, %if.else ]
682 %yy = phi i32 [ %z0, %if.then ], [ %z1, %if.else ]
683 %ret = add i32 %xx, %yy
684 ret i32 %ret
685}
686
687; CHECK-LABEL: test_pr30373b
688; CHECK: lshr
689; CHECK-NOT: exact
690; CHECK: }
691
692; CHECK: !0 = !{!1, !1, i64 0}
693; CHECK: !1 = !{!"float", !2}
694; CHECK: !2 = !{!"an example type tree"}