1; RUN: opt < %s -basicaa -gvn -S | FileCheck %s
2
3@a = external global i32		; <i32*> [#uses=7]
4
5; CHECK-LABEL: @test1(
6define i32 @test1() nounwind {
7entry:
8	%0 = load i32, i32* @a, align 4
9	%1 = icmp eq i32 %0, 4
10	br i1 %1, label %bb, label %bb1
11
12bb:		; preds = %entry
13	br label %bb8
14
15bb1:		; preds = %entry
16	%2 = load i32, i32* @a, align 4
17	%3 = icmp eq i32 %2, 5
18	br i1 %3, label %bb2, label %bb3
19
20bb2:		; preds = %bb1
21	br label %bb8
22
23bb3:		; preds = %bb1
24	%4 = load i32, i32* @a, align 4
25	%5 = icmp eq i32 %4, 4
26; CHECK: br i1 false, label %bb4, label %bb5
27	br i1 %5, label %bb4, label %bb5
28
29bb4:		; preds = %bb3
30	%6 = load i32, i32* @a, align 4
31	%7 = add i32 %6, 5
32	br label %bb8
33
34bb5:		; preds = %bb3
35	%8 = load i32, i32* @a, align 4
36	%9 = icmp eq i32 %8, 5
37; CHECK: br i1 false, label %bb6, label %bb7
38	br i1 %9, label %bb6, label %bb7
39
40bb6:		; preds = %bb5
41	%10 = load i32, i32* @a, align 4
42	%11 = add i32 %10, 4
43	br label %bb8
44
45bb7:		; preds = %bb5
46	%12 = load i32, i32* @a, align 4
47	br label %bb8
48
49bb8:		; preds = %bb7, %bb6, %bb4, %bb2, %bb
50	%.0 = phi i32 [ %12, %bb7 ], [ %11, %bb6 ], [ %7, %bb4 ], [ 4, %bb2 ], [ 5, %bb ]
51	br label %return
52
53return:		; preds = %bb8
54	ret i32 %.0
55}
56
57declare void @foo(i1)
58declare void @bar(i32)
59
60; CHECK-LABEL: @test3(
61define void @test3(i32 %x, i32 %y) {
62  %xz = icmp eq i32 %x, 0
63  %yz = icmp eq i32 %y, 0
64  %z = and i1 %xz, %yz
65  br i1 %z, label %both_zero, label %nope
66both_zero:
67  call void @foo(i1 %xz)
68; CHECK: call void @foo(i1 true)
69  call void @foo(i1 %yz)
70; CHECK: call void @foo(i1 true)
71  call void @bar(i32 %x)
72; CHECK: call void @bar(i32 0)
73  call void @bar(i32 %y)
74; CHECK: call void @bar(i32 0)
75  ret void
76nope:
77  call void @foo(i1 %z)
78; CHECK: call void @foo(i1 false)
79  ret void
80}
81
82; CHECK-LABEL: @test4(
83define void @test4(i1 %b, i32 %x) {
84  br i1 %b, label %sw, label %case3
85sw:
86  switch i32 %x, label %default [
87    i32 0, label %case0
88    i32 1, label %case1
89    i32 2, label %case0
90    i32 3, label %case3
91    i32 4, label %default
92  ]
93default:
94; CHECK: default:
95  call void @bar(i32 %x)
96; CHECK: call void @bar(i32 %x)
97  ret void
98case0:
99; CHECK: case0:
100  call void @bar(i32 %x)
101; CHECK: call void @bar(i32 %x)
102  ret void
103case1:
104; CHECK: case1:
105  call void @bar(i32 %x)
106; CHECK: call void @bar(i32 1)
107  ret void
108case3:
109; CHECK: case3:
110  call void @bar(i32 %x)
111; CHECK: call void @bar(i32 %x)
112  ret void
113}
114
115; CHECK-LABEL: @test5(
116define i1 @test5(i32 %x, i32 %y) {
117  %cmp = icmp eq i32 %x, %y
118  br i1 %cmp, label %same, label %different
119
120same:
121  %cmp2 = icmp ne i32 %x, %y
122; CHECK: ret i1 false
123  ret i1 %cmp2
124
125different:
126  %cmp3 = icmp eq i32 %x, %y
127; CHECK: ret i1 false
128  ret i1 %cmp3
129}
130
131; CHECK-LABEL: @test6(
132define i1 @test6(i32 %x, i32 %y) {
133  %cmp2 = icmp ne i32 %x, %y
134  %cmp = icmp eq i32 %x, %y
135  %cmp3 = icmp eq i32 %x, %y
136  br i1 %cmp, label %same, label %different
137
138same:
139; CHECK: ret i1 false
140  ret i1 %cmp2
141
142different:
143; CHECK: ret i1 false
144  ret i1 %cmp3
145}
146
147; CHECK-LABEL: @test6_fp(
148define i1 @test6_fp(float %x, float %y) {
149  %cmp2 = fcmp une float %x, %y
150  %cmp = fcmp oeq float %x, %y
151  %cmp3 = fcmp oeq float  %x, %y
152  br i1 %cmp, label %same, label %different
153
154same:
155; CHECK: ret i1 false
156  ret i1 %cmp2
157
158different:
159; CHECK: ret i1 false
160  ret i1 %cmp3
161}
162
163; CHECK-LABEL: @test7(
164define i1 @test7(i32 %x, i32 %y) {
165  %cmp = icmp sgt i32 %x, %y
166  br i1 %cmp, label %same, label %different
167
168same:
169  %cmp2 = icmp sle i32 %x, %y
170; CHECK: ret i1 false
171  ret i1 %cmp2
172
173different:
174  %cmp3 = icmp sgt i32 %x, %y
175; CHECK: ret i1 false
176  ret i1 %cmp3
177}
178
179; CHECK-LABEL: @test7_fp(
180define i1 @test7_fp(float %x, float %y) {
181  %cmp = fcmp ogt float %x, %y
182  br i1 %cmp, label %same, label %different
183
184same:
185  %cmp2 = fcmp ule float %x, %y
186; CHECK: ret i1 false
187  ret i1 %cmp2
188
189different:
190  %cmp3 = fcmp ogt float %x, %y
191; CHECK: ret i1 false
192  ret i1 %cmp3
193}
194
195; CHECK-LABEL: @test8(
196define i1 @test8(i32 %x, i32 %y) {
197  %cmp2 = icmp sle i32 %x, %y
198  %cmp = icmp sgt i32 %x, %y
199  %cmp3 = icmp sgt i32 %x, %y
200  br i1 %cmp, label %same, label %different
201
202same:
203; CHECK: ret i1 false
204  ret i1 %cmp2
205
206different:
207; CHECK: ret i1 false
208  ret i1 %cmp3
209}
210
211; CHECK-LABEL: @test8_fp(
212define i1 @test8_fp(float %x, float %y) {
213  %cmp2 = fcmp ule float %x, %y
214  %cmp = fcmp ogt float %x, %y
215  %cmp3 = fcmp ogt float %x, %y
216  br i1 %cmp, label %same, label %different
217
218same:
219; CHECK: ret i1 false
220  ret i1 %cmp2
221
222different:
223; CHECK: ret i1 false
224  ret i1 %cmp3
225}
226
227; PR1768
228; CHECK-LABEL: @test9(
229define i32 @test9(i32 %i, i32 %j) {
230  %cmp = icmp eq i32 %i, %j
231  br i1 %cmp, label %cond_true, label %ret
232
233cond_true:
234  %diff = sub i32 %i, %j
235  ret i32 %diff
236; CHECK: ret i32 0
237
238ret:
239  ret i32 5
240; CHECK: ret i32 5
241}
242
243; PR1768
244; CHECK-LABEL: @test10(
245define i32 @test10(i32 %j, i32 %i) {
246  %cmp = icmp eq i32 %i, %j
247  br i1 %cmp, label %cond_true, label %ret
248
249cond_true:
250  %diff = sub i32 %i, %j
251  ret i32 %diff
252; CHECK: ret i32 0
253
254ret:
255  ret i32 5
256; CHECK: ret i32 5
257}
258
259declare i32 @yogibar()
260
261; CHECK-LABEL: @test11(
262define i32 @test11(i32 %x) {
263  %v0 = call i32 @yogibar()
264  %v1 = call i32 @yogibar()
265  %cmp = icmp eq i32 %v0, %v1
266  br i1 %cmp, label %cond_true, label %next
267
268cond_true:
269  ret i32 %v1
270; CHECK: ret i32 %v0
271
272next:
273  %cmp2 = icmp eq i32 %x, %v0
274  br i1 %cmp2, label %cond_true2, label %next2
275
276cond_true2:
277  ret i32 %v0
278; CHECK: ret i32 %x
279
280next2:
281  ret i32 0
282}
283
284; CHECK-LABEL: @test12(
285define i32 @test12(i32 %x) {
286  %cmp = icmp eq i32 %x, 0
287  br i1 %cmp, label %cond_true, label %cond_false
288
289cond_true:
290  br label %ret
291
292cond_false:
293  br label %ret
294
295ret:
296  %res = phi i32 [ %x, %cond_true ], [ %x, %cond_false ]
297; CHECK: %res = phi i32 [ 0, %cond_true ], [ %x, %cond_false ]
298  ret i32 %res
299}
300