1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -basic-aa -gvn -S | FileCheck %s
4 @a = external global i32 ; <i32*> [#uses=7]
6 define i32 @test1() nounwind {
9 ; CHECK-NEXT: [[TMP0:%.*]] = load i32, i32* @a, align 4
10 ; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[TMP0]], 4
11 ; CHECK-NEXT: br i1 [[TMP1]], label [[BB:%.*]], label [[BB1:%.*]]
13 ; CHECK-NEXT: br label [[BB8:%.*]]
15 ; CHECK-NEXT: [[TMP2:%.*]] = icmp eq i32 [[TMP0]], 5
16 ; CHECK-NEXT: br i1 [[TMP2]], label [[BB2:%.*]], label [[BB3:%.*]]
18 ; CHECK-NEXT: br label [[BB8]]
20 ; CHECK-NEXT: br i1 false, label [[BB4:%.*]], label [[BB5:%.*]]
22 ; CHECK-NEXT: [[TMP3:%.*]] = load i32, i32* @a, align 4
23 ; CHECK-NEXT: [[TMP4:%.*]] = add i32 [[TMP3]], 5
24 ; CHECK-NEXT: br label [[BB8]]
26 ; CHECK-NEXT: br i1 false, label [[BB6:%.*]], label [[BB7:%.*]]
28 ; CHECK-NEXT: [[TMP5:%.*]] = load i32, i32* @a, align 4
29 ; CHECK-NEXT: [[TMP6:%.*]] = add i32 [[TMP5]], 4
30 ; CHECK-NEXT: br label [[BB8]]
32 ; CHECK-NEXT: br label [[BB8]]
34 ; CHECK-NEXT: [[DOT0:%.*]] = phi i32 [ [[TMP0]], [[BB7]] ], [ poison, [[BB6]] ], [ poison, [[BB4]] ], [ 4, [[BB2]] ], [ 5, [[BB]] ]
35 ; CHECK-NEXT: ret i32 [[DOT0]]
38 %0 = load i32, i32* @a, align 4
39 %1 = icmp eq i32 %0, 4
40 br i1 %1, label %bb, label %bb1
46 %2 = load i32, i32* @a, align 4
47 %3 = icmp eq i32 %2, 5
48 br i1 %3, label %bb2, label %bb3
54 %4 = load i32, i32* @a, align 4
55 %5 = icmp eq i32 %4, 4
56 br i1 %5, label %bb4, label %bb5
59 %6 = load i32, i32* @a, align 4
64 %8 = load i32, i32* @a, align 4
65 %9 = icmp eq i32 %8, 5
66 br i1 %9, label %bb6, label %bb7
69 %10 = load i32, i32* @a, align 4
74 %12 = load i32, i32* @a, align 4
77 bb8: ; preds = %bb7, %bb6, %bb4, %bb2, %bb
78 %.0 = phi i32 [ %12, %bb7 ], [ %11, %bb6 ], [ %7, %bb4 ], [ 4, %bb2 ], [ 5, %bb ]
81 return: ; preds = %bb8
86 declare void @bar(i32)
88 define void @test3(i32 %x, i32 %y) {
89 ; CHECK-LABEL: @test3(
90 ; CHECK-NEXT: [[XZ:%.*]] = icmp eq i32 [[X:%.*]], 0
91 ; CHECK-NEXT: [[YZ:%.*]] = icmp eq i32 [[Y:%.*]], 0
92 ; CHECK-NEXT: [[Z:%.*]] = and i1 [[XZ]], [[YZ]]
93 ; CHECK-NEXT: br i1 [[Z]], label [[BOTH_ZERO:%.*]], label [[NOPE:%.*]]
95 ; CHECK-NEXT: call void @foo(i1 true)
96 ; CHECK-NEXT: call void @foo(i1 true)
97 ; CHECK-NEXT: call void @bar(i32 0)
98 ; CHECK-NEXT: call void @bar(i32 0)
99 ; CHECK-NEXT: ret void
101 ; CHECK-NEXT: call void @foo(i1 false)
102 ; CHECK-NEXT: ret void
104 %xz = icmp eq i32 %x, 0
105 %yz = icmp eq i32 %y, 0
107 br i1 %z, label %both_zero, label %nope
109 call void @foo(i1 %xz)
110 call void @foo(i1 %yz)
111 call void @bar(i32 %x)
112 call void @bar(i32 %y)
115 call void @foo(i1 %z)
119 define void @test3_select(i32 %x, i32 %y) {
120 ; CHECK-LABEL: @test3_select(
121 ; CHECK-NEXT: [[XZ:%.*]] = icmp eq i32 [[X:%.*]], 0
122 ; CHECK-NEXT: [[YZ:%.*]] = icmp eq i32 [[Y:%.*]], 0
123 ; CHECK-NEXT: [[Z:%.*]] = select i1 [[XZ]], i1 [[YZ]], i1 false
124 ; CHECK-NEXT: br i1 [[Z]], label [[BOTH_ZERO:%.*]], label [[NOPE:%.*]]
126 ; CHECK-NEXT: call void @foo(i1 true)
127 ; CHECK-NEXT: call void @foo(i1 true)
128 ; CHECK-NEXT: call void @bar(i32 0)
129 ; CHECK-NEXT: call void @bar(i32 0)
130 ; CHECK-NEXT: ret void
132 ; CHECK-NEXT: call void @foo(i1 false)
133 ; CHECK-NEXT: ret void
135 %xz = icmp eq i32 %x, 0
136 %yz = icmp eq i32 %y, 0
137 %z = select i1 %xz, i1 %yz, i1 false
138 br i1 %z, label %both_zero, label %nope
140 call void @foo(i1 %xz)
141 call void @foo(i1 %yz)
142 call void @bar(i32 %x)
143 call void @bar(i32 %y)
146 call void @foo(i1 %z)
150 define void @test3_or(i32 %x, i32 %y) {
151 ; CHECK-LABEL: @test3_or(
152 ; CHECK-NEXT: [[XZ:%.*]] = icmp ne i32 [[X:%.*]], 0
153 ; CHECK-NEXT: [[YZ:%.*]] = icmp ne i32 [[Y:%.*]], 0
154 ; CHECK-NEXT: [[Z:%.*]] = or i1 [[XZ]], [[YZ]]
155 ; CHECK-NEXT: br i1 [[Z]], label [[NOPE:%.*]], label [[BOTH_ZERO:%.*]]
157 ; CHECK-NEXT: call void @foo(i1 false)
158 ; CHECK-NEXT: call void @foo(i1 false)
159 ; CHECK-NEXT: call void @bar(i32 0)
160 ; CHECK-NEXT: call void @bar(i32 0)
161 ; CHECK-NEXT: ret void
163 ; CHECK-NEXT: call void @foo(i1 true)
164 ; CHECK-NEXT: ret void
166 %xz = icmp ne i32 %x, 0
167 %yz = icmp ne i32 %y, 0
169 br i1 %z, label %nope, label %both_zero
171 call void @foo(i1 %xz)
172 call void @foo(i1 %yz)
173 call void @bar(i32 %x)
174 call void @bar(i32 %y)
177 call void @foo(i1 %z)
181 define void @test3_or_select(i32 %x, i32 %y) {
182 ; CHECK-LABEL: @test3_or_select(
183 ; CHECK-NEXT: [[XZ:%.*]] = icmp ne i32 [[X:%.*]], 0
184 ; CHECK-NEXT: [[YZ:%.*]] = icmp ne i32 [[Y:%.*]], 0
185 ; CHECK-NEXT: [[Z:%.*]] = select i1 [[XZ]], i1 true, i1 [[YZ]]
186 ; CHECK-NEXT: br i1 [[Z]], label [[NOPE:%.*]], label [[BOTH_ZERO:%.*]]
188 ; CHECK-NEXT: call void @foo(i1 false)
189 ; CHECK-NEXT: call void @foo(i1 false)
190 ; CHECK-NEXT: call void @bar(i32 0)
191 ; CHECK-NEXT: call void @bar(i32 0)
192 ; CHECK-NEXT: ret void
194 ; CHECK-NEXT: call void @foo(i1 true)
195 ; CHECK-NEXT: ret void
197 %xz = icmp ne i32 %x, 0
198 %yz = icmp ne i32 %y, 0
199 %z = select i1 %xz, i1 true, i1 %yz
200 br i1 %z, label %nope, label %both_zero
202 call void @foo(i1 %xz)
203 call void @foo(i1 %yz)
204 call void @bar(i32 %x)
205 call void @bar(i32 %y)
208 call void @foo(i1 %z)
212 define void @test4(i1 %b, i32 %x) {
213 ; CHECK-LABEL: @test4(
214 ; CHECK-NEXT: br i1 [[B:%.*]], label [[SW:%.*]], label [[CASE3:%.*]]
216 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[DEFAULT:%.*]] [
217 ; CHECK-NEXT: i32 0, label [[CASE0:%.*]]
218 ; CHECK-NEXT: i32 1, label [[CASE1:%.*]]
219 ; CHECK-NEXT: i32 2, label [[CASE0]]
220 ; CHECK-NEXT: i32 3, label [[CASE3]]
221 ; CHECK-NEXT: i32 4, label [[DEFAULT]]
224 ; CHECK-NEXT: call void @bar(i32 [[X]])
225 ; CHECK-NEXT: ret void
227 ; CHECK-NEXT: call void @bar(i32 [[X]])
228 ; CHECK-NEXT: ret void
230 ; CHECK-NEXT: call void @bar(i32 1)
231 ; CHECK-NEXT: ret void
233 ; CHECK-NEXT: call void @bar(i32 [[X]])
234 ; CHECK-NEXT: ret void
236 br i1 %b, label %sw, label %case3
238 switch i32 %x, label %default [
243 i32 4, label %default
246 call void @bar(i32 %x)
249 call void @bar(i32 %x)
252 call void @bar(i32 %x)
255 call void @bar(i32 %x)
259 define i1 @test5(i32 %x, i32 %y) {
260 ; CHECK-LABEL: @test5(
261 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], [[Y:%.*]]
262 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
264 ; CHECK-NEXT: ret i1 false
266 ; CHECK-NEXT: ret i1 false
268 %cmp = icmp eq i32 %x, %y
269 br i1 %cmp, label %same, label %different
272 %cmp2 = icmp ne i32 %x, %y
276 %cmp3 = icmp eq i32 %x, %y
280 define i1 @test6(i32 %x, i32 %y) {
281 ; CHECK-LABEL: @test6(
282 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[X:%.*]], [[Y:%.*]]
283 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X]], [[Y]]
284 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
286 ; CHECK-NEXT: ret i1 false
288 ; CHECK-NEXT: ret i1 false
290 %cmp2 = icmp ne i32 %x, %y
291 %cmp = icmp eq i32 %x, %y
292 %cmp3 = icmp eq i32 %x, %y
293 br i1 %cmp, label %same, label %different
302 define i1 @test6_fp(float %x, float %y) {
303 ; CHECK-LABEL: @test6_fp(
304 ; CHECK-NEXT: [[CMP2:%.*]] = fcmp une float [[X:%.*]], [[Y:%.*]]
305 ; CHECK-NEXT: [[CMP:%.*]] = fcmp oeq float [[X]], [[Y]]
306 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
308 ; CHECK-NEXT: ret i1 false
310 ; CHECK-NEXT: ret i1 false
312 %cmp2 = fcmp une float %x, %y
313 %cmp = fcmp oeq float %x, %y
314 %cmp3 = fcmp oeq float %x, %y
315 br i1 %cmp, label %same, label %different
324 define i1 @test7(i32 %x, i32 %y) {
325 ; CHECK-LABEL: @test7(
326 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X:%.*]], [[Y:%.*]]
327 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
329 ; CHECK-NEXT: ret i1 false
331 ; CHECK-NEXT: ret i1 false
333 %cmp = icmp sgt i32 %x, %y
334 br i1 %cmp, label %same, label %different
337 %cmp2 = icmp sle i32 %x, %y
341 %cmp3 = icmp sgt i32 %x, %y
345 define i1 @test7_fp(float %x, float %y) {
346 ; CHECK-LABEL: @test7_fp(
347 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X:%.*]], [[Y:%.*]]
348 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
350 ; CHECK-NEXT: ret i1 false
352 ; CHECK-NEXT: ret i1 false
354 %cmp = fcmp ogt float %x, %y
355 br i1 %cmp, label %same, label %different
358 %cmp2 = fcmp ule float %x, %y
362 %cmp3 = fcmp ogt float %x, %y
366 define i1 @test8(i32 %x, i32 %y) {
367 ; CHECK-LABEL: @test8(
368 ; CHECK-NEXT: [[CMP2:%.*]] = icmp sle i32 [[X:%.*]], [[Y:%.*]]
369 ; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X]], [[Y]]
370 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
372 ; CHECK-NEXT: ret i1 false
374 ; CHECK-NEXT: ret i1 false
376 %cmp2 = icmp sle i32 %x, %y
377 %cmp = icmp sgt i32 %x, %y
378 %cmp3 = icmp sgt i32 %x, %y
379 br i1 %cmp, label %same, label %different
388 define i1 @test8_fp(float %x, float %y) {
389 ; CHECK-LABEL: @test8_fp(
390 ; CHECK-NEXT: [[CMP2:%.*]] = fcmp ule float [[X:%.*]], [[Y:%.*]]
391 ; CHECK-NEXT: [[CMP:%.*]] = fcmp ogt float [[X]], [[Y]]
392 ; CHECK-NEXT: br i1 [[CMP]], label [[SAME:%.*]], label [[DIFFERENT:%.*]]
394 ; CHECK-NEXT: ret i1 false
396 ; CHECK-NEXT: ret i1 false
398 %cmp2 = fcmp ule float %x, %y
399 %cmp = fcmp ogt float %x, %y
400 %cmp3 = fcmp ogt float %x, %y
401 br i1 %cmp, label %same, label %different
411 define i32 @test9(i32 %i, i32 %j) {
412 ; CHECK-LABEL: @test9(
413 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I:%.*]], [[J:%.*]]
414 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[RET:%.*]]
416 ; CHECK-NEXT: ret i32 0
418 ; CHECK-NEXT: ret i32 5
420 %cmp = icmp eq i32 %i, %j
421 br i1 %cmp, label %cond_true, label %ret
424 %diff = sub i32 %i, %j
432 define i32 @test10(i32 %j, i32 %i) {
433 ; CHECK-LABEL: @test10(
434 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[I:%.*]], [[J:%.*]]
435 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[RET:%.*]]
437 ; CHECK-NEXT: ret i32 0
439 ; CHECK-NEXT: ret i32 5
441 %cmp = icmp eq i32 %i, %j
442 br i1 %cmp, label %cond_true, label %ret
445 %diff = sub i32 %i, %j
452 declare i32 @yogibar()
454 define i32 @test11(i32 %x) {
455 ; CHECK-LABEL: @test11(
456 ; CHECK-NEXT: [[V0:%.*]] = call i32 @yogibar()
457 ; CHECK-NEXT: [[V1:%.*]] = call i32 @yogibar()
458 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[V0]], [[V1]]
459 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[NEXT:%.*]]
461 ; CHECK-NEXT: ret i32 [[V0]]
463 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[X:%.*]], [[V0]]
464 ; CHECK-NEXT: br i1 [[CMP2]], label [[COND_TRUE2:%.*]], label [[NEXT2:%.*]]
466 ; CHECK-NEXT: ret i32 [[X]]
468 ; CHECK-NEXT: ret i32 0
470 %v0 = call i32 @yogibar()
471 %v1 = call i32 @yogibar()
472 %cmp = icmp eq i32 %v0, %v1
473 br i1 %cmp, label %cond_true, label %next
479 %cmp2 = icmp eq i32 %x, %v0
480 br i1 %cmp2, label %cond_true2, label %next2
489 define i32 @test12(i32 %x) {
490 ; CHECK-LABEL: @test12(
491 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 0
492 ; CHECK-NEXT: br i1 [[CMP]], label [[COND_TRUE:%.*]], label [[COND_FALSE:%.*]]
494 ; CHECK-NEXT: br label [[RET:%.*]]
496 ; CHECK-NEXT: br label [[RET]]
498 ; CHECK-NEXT: [[RES:%.*]] = phi i32 [ 0, [[COND_TRUE]] ], [ [[X]], [[COND_FALSE]] ]
499 ; CHECK-NEXT: ret i32 [[RES]]
501 %cmp = icmp eq i32 %x, 0
502 br i1 %cmp, label %cond_true, label %cond_false
511 %res = phi i32 [ %x, %cond_true ], [ %x, %cond_false ]
515 ; On the path from entry->if->end we know that ptr1==ptr2, so we can determine
516 ; that gep2 does not alias ptr1 on that path (as it would require that
517 ; ptr2==ptr2+2), so we can perform PRE of the load.
518 define i32 @test13(i32* %ptr1, i32* %ptr2) {
519 ; CHECK-LABEL: @test13(
521 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr i32, i32* [[PTR2:%.*]], i32 1
522 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr i32, i32* [[PTR2]], i32 2
523 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32* [[PTR1:%.*]], [[PTR2]]
524 ; CHECK-NEXT: br i1 [[CMP]], label [[IF:%.*]], label [[ENTRY_END_CRIT_EDGE:%.*]]
525 ; CHECK: entry.end_crit_edge:
526 ; CHECK-NEXT: [[VAL2_PRE:%.*]] = load i32, i32* [[GEP2]], align 4
527 ; CHECK-NEXT: br label [[END:%.*]]
529 ; CHECK-NEXT: [[VAL1:%.*]] = load i32, i32* [[GEP2]], align 4
530 ; CHECK-NEXT: br label [[END]]
532 ; CHECK-NEXT: [[VAL2:%.*]] = phi i32 [ [[VAL1]], [[IF]] ], [ [[VAL2_PRE]], [[ENTRY_END_CRIT_EDGE]] ]
533 ; CHECK-NEXT: [[PHI1:%.*]] = phi i32* [ [[PTR2]], [[IF]] ], [ [[GEP1]], [[ENTRY_END_CRIT_EDGE]] ]
534 ; CHECK-NEXT: [[PHI2:%.*]] = phi i32 [ [[VAL1]], [[IF]] ], [ 0, [[ENTRY_END_CRIT_EDGE]] ]
535 ; CHECK-NEXT: store i32 0, i32* [[PHI1]], align 4
536 ; CHECK-NEXT: [[RET:%.*]] = add i32 [[PHI2]], [[VAL2]]
537 ; CHECK-NEXT: ret i32 [[RET]]
540 %gep1 = getelementptr i32, i32* %ptr2, i32 1
541 %gep2 = getelementptr i32, i32* %ptr2, i32 2
542 %cmp = icmp eq i32* %ptr1, %ptr2
543 br i1 %cmp, label %if, label %end
547 %val1 = load i32, i32* %gep2, align 4
551 %phi1 = phi i32* [ %ptr1, %if ], [ %gep1, %entry ]
552 %phi2 = phi i32 [ %val1, %if ], [ 0, %entry ]
553 store i32 0, i32* %phi1, align 4
554 %val2 = load i32, i32* %gep2, align 4
555 %ret = add i32 %phi2, %val2
559 define void @test14(i32* %ptr1, i32* noalias %ptr2) {
560 ; CHECK-LABEL: @test14(
562 ; CHECK-NEXT: [[GEP1:%.*]] = getelementptr inbounds i32, i32* [[PTR1:%.*]], i32 1
563 ; CHECK-NEXT: [[GEP2:%.*]] = getelementptr inbounds i32, i32* [[PTR1]], i32 2
564 ; CHECK-NEXT: br label [[LOOP:%.*]]
566 ; CHECK-NEXT: br i1 undef, label [[LOOP_IF1_CRIT_EDGE:%.*]], label [[THEN:%.*]]
567 ; CHECK: loop.if1_crit_edge:
568 ; CHECK-NEXT: [[VAL2_PRE:%.*]] = load i32, i32* [[GEP2]], align 4
569 ; CHECK-NEXT: br label [[IF1:%.*]]
571 ; CHECK-NEXT: [[VAL2:%.*]] = phi i32 [ [[VAL2_PRE]], [[LOOP_IF1_CRIT_EDGE]] ], [ [[VAL3:%.*]], [[LOOP_END:%.*]] ]
572 ; CHECK-NEXT: store i32 [[VAL2]], i32* [[GEP2]], align 4
573 ; CHECK-NEXT: store i32 0, i32* [[GEP1]], align 4
574 ; CHECK-NEXT: br label [[THEN]]
576 ; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32* [[GEP2]], [[PTR2:%.*]]
577 ; CHECK-NEXT: br i1 [[CMP]], label [[LOOP_END]], label [[IF2:%.*]]
579 ; CHECK-NEXT: br label [[LOOP_END]]
581 ; CHECK-NEXT: [[PHI3:%.*]] = phi i32* [ [[PTR2]], [[THEN]] ], [ [[PTR1]], [[IF2]] ]
582 ; CHECK-NEXT: [[VAL3]] = load i32, i32* [[GEP2]], align 4
583 ; CHECK-NEXT: store i32 [[VAL3]], i32* [[PHI3]], align 4
584 ; CHECK-NEXT: br i1 undef, label [[LOOP]], label [[IF1]]
587 %gep1 = getelementptr inbounds i32, i32* %ptr1, i32 1
588 %gep2 = getelementptr inbounds i32, i32* %ptr1, i32 2
592 %phi1 = phi i32* [ %gep3, %loop.end ], [ %gep1, %entry ]
593 br i1 undef, label %if1, label %then
597 %val2 = load i32, i32* %gep2, align 4
598 store i32 %val2, i32* %gep2, align 4
599 store i32 0, i32* %phi1, align 4
603 %cmp = icmp eq i32* %gep2, %ptr2
604 br i1 %cmp, label %loop.end, label %if2
610 %phi3 = phi i32* [ %gep2, %then ], [ %ptr1, %if2 ]
611 %val3 = load i32, i32* %gep2, align 4
612 store i32 %val3, i32* %phi3, align 4
613 %gep3 = getelementptr inbounds i32, i32* %ptr1, i32 1
614 br i1 undef, label %loop, label %if1