1; RUN: opt < %s -simplifycfg -S | FileCheck %s
2
3; Test basic folding to a conditional branch.
4define i32 @foo(i64 %x, i64 %y) nounwind {
5; CHECK-LABEL: @foo(
6entry:
7    %eq = icmp eq i64 %x, %y
8    br i1 %eq, label %b, label %switch
9switch:
10    %lt = icmp slt i64 %x, %y
11; CHECK: br i1 %lt, label %a, label %b
12    %qux = select i1 %lt, i32 0, i32 2
13    switch i32 %qux, label %bees [
14        i32 0, label %a
15        i32 1, label %b
16        i32 2, label %b
17    ]
18a:
19    tail call void @bees.a() nounwind
20    ret i32 1
21; CHECK: b:
22; CHECK-NEXT: %retval = phi i32 [ 0, %switch ], [ 2, %entry ]
23b:
24    %retval = phi i32 [0, %switch], [0, %switch], [2, %entry]
25    tail call void @bees.b() nounwind
26    ret i32 %retval
27; CHECK-NOT: bees:
28bees:
29    tail call void @llvm.trap() nounwind
30    unreachable
31}
32
33; Test basic folding to an unconditional branch.
34define i32 @bar(i64 %x, i64 %y) nounwind {
35; CHECK-LABEL: @bar(
36entry:
37; CHECK-NEXT: entry:
38; CHECK-NEXT: tail call void @bees.a() [[NUW:#[0-9]+]]
39; CHECK-NEXT: ret i32 0
40    %lt = icmp slt i64 %x, %y
41    %qux = select i1 %lt, i32 0, i32 2
42    switch i32 %qux, label %bees [
43        i32 0, label %a
44        i32 1, label %b
45        i32 2, label %a
46    ]
47a:
48    %retval = phi i32 [0, %entry], [0, %entry], [1, %b]
49    tail call void @bees.a() nounwind
50    ret i32 0
51b:
52    tail call void @bees.b() nounwind
53    br label %a
54bees:
55    tail call void @llvm.trap() nounwind
56    unreachable
57}
58
59; Test the edge case where both values from the select are the default case.
60define void @bazz(i64 %x, i64 %y) nounwind {
61; CHECK-LABEL: @bazz(
62entry:
63; CHECK-NEXT: entry:
64; CHECK-NEXT: tail call void @bees.b() [[NUW]]
65; CHECK-NEXT: ret void
66    %lt = icmp slt i64 %x, %y
67    %qux = select i1 %lt, i32 10, i32 12
68    switch i32 %qux, label %b [
69        i32 0, label %a
70        i32 1, label %bees
71        i32 2, label %bees
72    ]
73a:
74    tail call void @bees.a() nounwind
75    ret void
76b:
77    tail call void @bees.b() nounwind
78    ret void
79bees:
80    tail call void @llvm.trap()
81    unreachable
82}
83
84; Test the edge case where both values from the select are equal.
85define void @quux(i64 %x, i64 %y) nounwind {
86; CHECK-LABEL: @quux(
87entry:
88; CHECK-NEXT: entry:
89; CHECK-NEXT: tail call void @bees.a() [[NUW]]
90; CHECK-NEXT: ret void
91    %lt = icmp slt i64 %x, %y
92    %qux = select i1 %lt, i32 0, i32 0
93    switch i32 %qux, label %b [
94        i32 0, label %a
95        i32 1, label %bees
96        i32 2, label %bees
97    ]
98a:
99    tail call void @bees.a() nounwind
100    ret void
101b:
102    tail call void @bees.b() nounwind
103    ret void
104bees:
105    tail call void @llvm.trap()
106    unreachable
107}
108
109; A final test, for phi node munging.
110define i32 @xyzzy(i64 %x, i64 %y) {
111; CHECK-LABEL: @xyzzy(
112entry:
113    %eq = icmp eq i64 %x, %y
114    br i1 %eq, label %r, label %cont
115cont:
116; CHECK: %lt = icmp slt i64 %x, %y
117    %lt = icmp slt i64 %x, %y
118; CHECK-NEXT: select i1 %lt, i32 -1, i32 1
119    %qux = select i1 %lt, i32 0, i32 2
120    switch i32 %qux, label %bees [
121        i32 0, label %a
122        i32 1, label %r
123        i32 2, label %r
124    ]
125r:
126    %val = phi i32 [0, %entry], [1, %cont], [1, %cont]
127    ret i32 %val
128a:
129    ret i32 -1
130; CHECK-NOT: bees:
131bees:
132    tail call void @llvm.trap()
133    unreachable
134}
135
136declare void @llvm.trap() nounwind noreturn
137declare void @bees.a() nounwind
138declare void @bees.b() nounwind
139
140; CHECK: attributes [[NUW]] = { nounwind }
141; CHECK: attributes #1 = { noreturn nounwind }
142