1; RUN: opt < %s -simple-loop-unswitch -enable-nontrivial-unswitch -S 2>&1 | FileCheck %s
2; RUN: opt < %s -passes='simple-loop-unswitch<nontrivial>' -S 2>&1 | FileCheck %s
3; RUN: opt < %s -passes='loop-mssa(simple-loop-unswitch<nontrivial>)' -S 2>&1 | FileCheck %s
4;
5; Checking that (dead) blocks from inner loop are deleted after unswitch.
6;
7declare void @foo()
8
9; CHECK-LABEL: @Test
10define void @Test(i32) {
11entry:
12  br label %outer
13outer:
14  %oi = phi i32 [ 0, %entry ], [ %oinc, %outer_continue]
15  br label %inner
16inner:
17  %ii = phi i32 [ 0, %outer ], [ %iinc, %continue]
18  call void @foo()
19  switch i32 %0, label %get_out2 [
20    i32 0, label %continue
21    i32 1, label %case1
22    i32 2, label %get_out
23  ]
24;
25; since we unswitch on the above switch, %case1 and %continue blocks
26; become dead in the original loop
27;
28; CHECK-NOT: case1:
29case1:
30  br label %continue
31; CHECK-NOT: {{^}}continue:
32continue:
33  %iinc = add i32 %ii, 1
34  %icmp = icmp eq i32 %ii, 100
35  br i1 %icmp, label %inner, label %outer_continue
36
37outer_continue:
38  %oinc = add i32 %oi, 1
39  %ocmp = icmp eq i32 %oi, 100
40  br i1 %ocmp, label %outer, label %get_out
41
42get_out:
43  ret void
44get_out2:
45  unreachable
46}
47
48;
49; This comes from PR38778
50; CHECK-LABEL: @Test2
51define void @Test2(i32) {
52header:
53  br label %loop
54loop:
55  switch i32 %0, label %continue [
56    i32 -2147483648, label %check
57    i32 98, label %guarded1
58    i32 99, label %guarded2
59  ]
60; CHECK-NOT: {{^}}guarded1:
61guarded1:
62  br i1 undef, label %continue, label %leave
63guarded2:
64  br label %continue
65check:
66  %val = add i32 0, 1
67  br i1 undef, label %continue, label %leave
68continue:
69  br label %loop
70leave:
71  %local = phi i32 [ 0, %guarded1 ], [ %val, %check ]
72  ret void
73}
74
75;
76; Yet another test from PR38778
77;
78; CHECK-LABEL: @Test3
79define void @Test3(i32) {
80header:
81  br label %outer
82outer:
83  %bad_input.i = icmp eq i32 %0, -2147483648
84  br label %inner
85inner:
86  br i1 %bad_input.i, label %overflow, label %switchme
87overflow:
88  br label %continue
89switchme:
90  switch i32 %0, label %continue [
91    i32 88, label %go_out
92    i32 99, label %case2
93  ]
94; CHECK-NOT: {{^}}case2:
95case2:
96  br label %continue
97continue:
98  %local_11_92 = phi i32 [ 0, %switchme ], [ 18, %case2 ], [ 0, %overflow ]
99  br i1 undef, label %outer, label %inner
100go_out:
101  unreachable
102}
103