1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -S -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp < %s | FileCheck %s
3 ; RUN: opt -S -data-layout="p:32:32-p1:16:16" -passes=simplifycfg -simplifycfg-require-and-preserve-domtree=1 -switch-range-to-icmp < %s | FileCheck -check-prefix=CHECK -check-prefix=DL %s
9 define void @test1(i32 %V) {
10 ; CHECK-LABEL: @test1(
11 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[F:%.*]] [
12 ; CHECK-NEXT: i32 17, label [[T:%.*]]
13 ; CHECK-NEXT: i32 4, label [[T]]
16 ; CHECK-NEXT: ret void
18 ; CHECK-NEXT: call void @foo1()
19 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
21 ; CHECK-NEXT: call void @foo2()
22 ; CHECK-NEXT: br label [[COMMON_RET]]
24 %C1 = icmp eq i32 %V, 4 ; <i1> [#uses=1]
25 %C2 = icmp eq i32 %V, 17 ; <i1> [#uses=1]
26 %CN = or i1 %C1, %C2 ; <i1> [#uses=1]
27 br i1 %CN, label %T, label %F
36 define void @test1_select(i32 %V) {
37 ; CHECK-LABEL: @test1_select(
38 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[F:%.*]] [
39 ; CHECK-NEXT: i32 17, label [[T:%.*]]
40 ; CHECK-NEXT: i32 4, label [[T]]
43 ; CHECK-NEXT: ret void
45 ; CHECK-NEXT: call void @foo1()
46 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
48 ; CHECK-NEXT: call void @foo2()
49 ; CHECK-NEXT: br label [[COMMON_RET]]
51 %C1 = icmp eq i32 %V, 4
52 %C2 = icmp eq i32 %V, 17
53 %CN = select i1 %C1, i1 true, i1 %C2
54 br i1 %CN, label %T, label %F
63 define void @test1_ptr(ptr %V) {
64 ; DL-LABEL: @test1_ptr(
65 ; DL-NEXT: [[MAGICPTR:%.*]] = ptrtoint ptr [[V:%.*]] to i32
66 ; DL-NEXT: switch i32 [[MAGICPTR]], label [[F:%.*]] [
67 ; DL-NEXT: i32 17, label [[T:%.*]]
68 ; DL-NEXT: i32 4, label [[T]]
73 ; DL-NEXT: call void @foo1()
74 ; DL-NEXT: br label [[COMMON_RET:%.*]]
76 ; DL-NEXT: call void @foo2()
77 ; DL-NEXT: br label [[COMMON_RET]]
79 %C1 = icmp eq ptr %V, inttoptr (i32 4 to ptr)
80 %C2 = icmp eq ptr %V, inttoptr (i32 17 to ptr)
81 %CN = or i1 %C1, %C2 ; <i1> [#uses=1]
82 br i1 %CN, label %T, label %F
91 define void @test1_ptr_as1(ptr addrspace(1) %V) {
92 ; DL-LABEL: @test1_ptr_as1(
93 ; DL-NEXT: [[MAGICPTR:%.*]] = ptrtoint ptr addrspace(1) [[V:%.*]] to i16
94 ; DL-NEXT: switch i16 [[MAGICPTR]], label [[F:%.*]] [
95 ; DL-NEXT: i16 17, label [[T:%.*]]
96 ; DL-NEXT: i16 4, label [[T]]
101 ; DL-NEXT: call void @foo1()
102 ; DL-NEXT: br label [[COMMON_RET:%.*]]
104 ; DL-NEXT: call void @foo2()
105 ; DL-NEXT: br label [[COMMON_RET]]
107 %C1 = icmp eq ptr addrspace(1) %V, inttoptr (i32 4 to ptr addrspace(1))
108 %C2 = icmp eq ptr addrspace(1) %V, inttoptr (i32 17 to ptr addrspace(1))
109 %CN = or i1 %C1, %C2 ; <i1> [#uses=1]
110 br i1 %CN, label %T, label %F
119 define void @test2(i32 %V) {
120 ; CHECK-LABEL: @test2(
121 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[T:%.*]] [
122 ; CHECK-NEXT: i32 17, label [[F:%.*]]
123 ; CHECK-NEXT: i32 4, label [[F]]
126 ; CHECK-NEXT: ret void
128 ; CHECK-NEXT: call void @foo1()
129 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
131 ; CHECK-NEXT: call void @foo2()
132 ; CHECK-NEXT: br label [[COMMON_RET]]
134 %C1 = icmp ne i32 %V, 4 ; <i1> [#uses=1]
135 %C2 = icmp ne i32 %V, 17 ; <i1> [#uses=1]
136 %CN = and i1 %C1, %C2 ; <i1> [#uses=1]
137 br i1 %CN, label %T, label %F
146 define void @test2_select(i32 %V) {
147 ; CHECK-LABEL: @test2_select(
148 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[T:%.*]] [
149 ; CHECK-NEXT: i32 17, label [[F:%.*]]
150 ; CHECK-NEXT: i32 4, label [[F]]
153 ; CHECK-NEXT: ret void
155 ; CHECK-NEXT: call void @foo1()
156 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
158 ; CHECK-NEXT: call void @foo2()
159 ; CHECK-NEXT: br label [[COMMON_RET]]
161 %C1 = icmp ne i32 %V, 4
162 %C2 = icmp ne i32 %V, 17
163 %CN = select i1 %C1, i1 %C2, i1 false
164 br i1 %CN, label %T, label %F
173 define void @test3(i32 %V) {
174 ; CHECK-LABEL: @test3(
175 ; CHECK-NEXT: switch i32 [[V:%.*]], label [[F:%.*]] [
176 ; CHECK-NEXT: i32 4, label [[T:%.*]]
177 ; CHECK-NEXT: i32 17, label [[T]]
180 ; CHECK-NEXT: ret void
182 ; CHECK-NEXT: call void @foo1()
183 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
185 ; CHECK-NEXT: call void @foo2()
186 ; CHECK-NEXT: br label [[COMMON_RET]]
188 %C1 = icmp eq i32 %V, 4 ; <i1> [#uses=1]
189 br i1 %C1, label %T, label %N
191 %C2 = icmp eq i32 %V, 17 ; <i1> [#uses=1]
192 br i1 %C2, label %T, label %F
204 define i32 @test4(i8 zeroext %c) nounwind ssp noredzone {
205 ; CHECK-LABEL: @test4(
207 ; CHECK-NEXT: switch i8 [[C:%.*]], label [[LOR_RHS:%.*]] [
208 ; CHECK-NEXT: i8 62, label [[LOR_END:%.*]]
209 ; CHECK-NEXT: i8 34, label [[LOR_END]]
210 ; CHECK-NEXT: i8 92, label [[LOR_END]]
213 ; CHECK-NEXT: br label [[LOR_END]]
215 ; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[LOR_RHS]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
216 ; CHECK-NEXT: [[LOR_EXT:%.*]] = zext i1 [[TMP0]] to i32
217 ; CHECK-NEXT: ret i32 [[LOR_EXT]]
220 %cmp = icmp eq i8 %c, 62
221 br i1 %cmp, label %lor.end, label %lor.lhs.false
223 lor.lhs.false: ; preds = %entry
224 %cmp4 = icmp eq i8 %c, 34
225 br i1 %cmp4, label %lor.end, label %lor.rhs
227 lor.rhs: ; preds = %lor.lhs.false
228 %cmp8 = icmp eq i8 %c, 92
231 lor.end: ; preds = %lor.rhs, %lor.lhs.false, %entry
232 %0 = phi i1 [ true, %lor.lhs.false ], [ true, %entry ], [ %cmp8, %lor.rhs ]
233 %lor.ext = zext i1 %0 to i32
238 define i32 @test5(i8 zeroext %c) nounwind ssp noredzone {
239 ; CHECK-LABEL: @test5(
241 ; CHECK-NEXT: switch i8 [[C:%.*]], label [[LOR_RHS:%.*]] [
242 ; CHECK-NEXT: i8 62, label [[LOR_END:%.*]]
243 ; CHECK-NEXT: i8 34, label [[LOR_END]]
244 ; CHECK-NEXT: i8 92, label [[LOR_END]]
247 ; CHECK-NEXT: br label [[LOR_END]]
249 ; CHECK-NEXT: [[TMP0:%.*]] = phi i1 [ true, [[ENTRY:%.*]] ], [ false, [[LOR_RHS]] ], [ true, [[ENTRY]] ], [ true, [[ENTRY]] ]
250 ; CHECK-NEXT: [[LOR_EXT:%.*]] = zext i1 [[TMP0]] to i32
251 ; CHECK-NEXT: ret i32 [[LOR_EXT]]
254 switch i8 %c, label %lor.rhs [
255 i8 62, label %lor.end
256 i8 34, label %lor.end
257 i8 92, label %lor.end
260 lor.rhs: ; preds = %entry
261 %V = icmp eq i8 %c, 92
264 lor.end: ; preds = %entry, %entry, %entry, %lor.rhs
265 %0 = phi i1 [ true, %entry ], [ %V, %lor.rhs ], [ true, %entry ], [ true, %entry ]
266 %lor.ext = zext i1 %0 to i32
271 define i1 @test6(ptr %I) {
272 ; CHECK-LABEL: @test6(
274 ; CHECK-NEXT: [[TMP_1_I:%.*]] = getelementptr { i32, i32 }, ptr [[I:%.*]], i64 0, i32 1
275 ; CHECK-NEXT: [[TMP_2_I:%.*]] = load i32, ptr [[TMP_1_I]], align 4
276 ; CHECK-NEXT: [[TMP_2_I_OFF:%.*]] = add i32 [[TMP_2_I]], -14
277 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[TMP_2_I_OFF]], 6
278 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[SWITCH]], i1 true, i1 false
279 ; CHECK-NEXT: ret i1 [[SPEC_SELECT]]
282 %tmp.1.i = getelementptr { i32, i32 }, ptr %I, i64 0, i32 1 ; <ptr> [#uses=1]
283 %tmp.2.i = load i32, ptr %tmp.1.i ; <i32> [#uses=6]
284 %tmp.2 = icmp eq i32 %tmp.2.i, 14 ; <i1> [#uses=1]
285 br i1 %tmp.2, label %shortcirc_done.4, label %shortcirc_next.0
286 shortcirc_next.0: ; preds = %entry
287 %tmp.6 = icmp eq i32 %tmp.2.i, 15 ; <i1> [#uses=1]
288 br i1 %tmp.6, label %shortcirc_done.4, label %shortcirc_next.1
289 shortcirc_next.1: ; preds = %shortcirc_next.0
290 %tmp.11 = icmp eq i32 %tmp.2.i, 16 ; <i1> [#uses=1]
291 br i1 %tmp.11, label %shortcirc_done.4, label %shortcirc_next.2
292 shortcirc_next.2: ; preds = %shortcirc_next.1
293 %tmp.16 = icmp eq i32 %tmp.2.i, 17 ; <i1> [#uses=1]
294 br i1 %tmp.16, label %shortcirc_done.4, label %shortcirc_next.3
295 shortcirc_next.3: ; preds = %shortcirc_next.2
296 %tmp.21 = icmp eq i32 %tmp.2.i, 18 ; <i1> [#uses=1]
297 br i1 %tmp.21, label %shortcirc_done.4, label %shortcirc_next.4
298 shortcirc_next.4: ; preds = %shortcirc_next.3
299 %tmp.26 = icmp eq i32 %tmp.2.i, 19 ; <i1> [#uses=1]
300 br label %UnifiedReturnBlock
301 shortcirc_done.4: ; preds = %shortcirc_next.3, %shortcirc_next.2, %shortcirc_next.1, %shortcirc_next.0, %entry
302 br label %UnifiedReturnBlock
303 UnifiedReturnBlock: ; preds = %shortcirc_done.4, %shortcirc_next.4
304 %UnifiedRetVal = phi i1 [ %tmp.26, %shortcirc_next.4 ], [ true, %shortcirc_done.4 ] ; <i1> [#uses=1]
305 ret i1 %UnifiedRetVal
309 define void @test7(i8 zeroext %c, i32 %x) nounwind ssp noredzone {
310 ; CHECK-LABEL: @test7(
312 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 32
313 ; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
314 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
315 ; CHECK: switch.early.test:
316 ; CHECK-NEXT: switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
317 ; CHECK-NEXT: i8 99, label [[IF_THEN]]
318 ; CHECK-NEXT: i8 97, label [[IF_THEN]]
321 ; CHECK-NEXT: ret void
323 ; CHECK-NEXT: tail call void @foo1() #[[ATTR2:[0-9]+]]
324 ; CHECK-NEXT: br label [[COMMON_RET]]
327 %cmp = icmp ult i32 %x, 32
328 %cmp4 = icmp eq i8 %c, 97
329 %or.cond = or i1 %cmp, %cmp4
330 %cmp9 = icmp eq i8 %c, 99
331 %or.cond11 = or i1 %or.cond, %cmp9
332 br i1 %or.cond11, label %if.then, label %if.end
334 if.then: ; preds = %entry
335 tail call void @foo1() nounwind noredzone
338 if.end: ; preds = %entry
343 define i32 @test8(i8 zeroext %c, i32 %x, i1 %C) nounwind ssp noredzone {
344 ; CHECK-LABEL: @test8(
346 ; CHECK-NEXT: br i1 [[C:%.*]], label [[N:%.*]], label [[IF_THEN:%.*]]
348 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 32
349 ; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
350 ; CHECK-NEXT: br i1 [[TMP0]], label [[IF_THEN]], label [[SWITCH_EARLY_TEST:%.*]]
351 ; CHECK: switch.early.test:
352 ; CHECK-NEXT: switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
353 ; CHECK-NEXT: i8 99, label [[IF_THEN]]
354 ; CHECK-NEXT: i8 97, label [[IF_THEN]]
357 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ [[A:%.*]], [[IF_THEN]] ], [ 0, [[SWITCH_EARLY_TEST]] ]
358 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
360 ; CHECK-NEXT: [[A]] = phi i32 [ 0, [[ENTRY:%.*]] ], [ 42, [[SWITCH_EARLY_TEST]] ], [ 42, [[N]] ], [ 42, [[SWITCH_EARLY_TEST]] ]
361 ; CHECK-NEXT: tail call void @foo1() #[[ATTR2]]
362 ; CHECK-NEXT: br label [[COMMON_RET]]
365 br i1 %C, label %N, label %if.then
367 %cmp = icmp ult i32 %x, 32
368 %cmp4 = icmp eq i8 %c, 97
369 %or.cond = or i1 %cmp, %cmp4
370 %cmp9 = icmp eq i8 %c, 99
371 %or.cond11 = or i1 %or.cond, %cmp9
372 br i1 %or.cond11, label %if.then, label %if.end
374 if.then: ; preds = %entry
375 %A = phi i32 [0, %entry], [42, %N]
376 tail call void @foo1() nounwind noredzone
379 if.end: ; preds = %entry
384 ;; This is "Example 7" from http://blog.regehr.org/archives/320
385 define i32 @test9(i8 zeroext %c) nounwind ssp noredzone {
386 ; CHECK-LABEL: @test9(
388 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i8 [[C:%.*]], 33
389 ; CHECK-NEXT: [[TMP0:%.*]] = freeze i1 [[CMP]]
390 ; CHECK-NEXT: br i1 [[TMP0]], label [[LOR_END:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
391 ; CHECK: switch.early.test:
392 ; CHECK-NEXT: switch i8 [[C]], label [[LOR_RHS:%.*]] [
393 ; CHECK-NEXT: i8 92, label [[LOR_END]]
394 ; CHECK-NEXT: i8 62, label [[LOR_END]]
395 ; CHECK-NEXT: i8 60, label [[LOR_END]]
396 ; CHECK-NEXT: i8 59, label [[LOR_END]]
397 ; CHECK-NEXT: i8 58, label [[LOR_END]]
398 ; CHECK-NEXT: i8 46, label [[LOR_END]]
399 ; CHECK-NEXT: i8 44, label [[LOR_END]]
400 ; CHECK-NEXT: i8 34, label [[LOR_END]]
401 ; CHECK-NEXT: i8 39, label [[LOR_END]]
404 ; CHECK-NEXT: br label [[LOR_END]]
406 ; CHECK-NEXT: [[TMP1:%.*]] = phi i1 [ true, [[SWITCH_EARLY_TEST]] ], [ false, [[LOR_RHS]] ], [ true, [[ENTRY:%.*]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ], [ true, [[SWITCH_EARLY_TEST]] ]
407 ; CHECK-NEXT: [[CONV46:%.*]] = zext i1 [[TMP1]] to i32
408 ; CHECK-NEXT: ret i32 [[CONV46]]
411 %cmp = icmp ult i8 %c, 33
412 br i1 %cmp, label %lor.end, label %lor.lhs.false
414 lor.lhs.false: ; preds = %entry
415 %cmp4 = icmp eq i8 %c, 46
416 br i1 %cmp4, label %lor.end, label %lor.lhs.false6
418 lor.lhs.false6: ; preds = %lor.lhs.false
419 %cmp9 = icmp eq i8 %c, 44
420 br i1 %cmp9, label %lor.end, label %lor.lhs.false11
422 lor.lhs.false11: ; preds = %lor.lhs.false6
423 %cmp14 = icmp eq i8 %c, 58
424 br i1 %cmp14, label %lor.end, label %lor.lhs.false16
426 lor.lhs.false16: ; preds = %lor.lhs.false11
427 %cmp19 = icmp eq i8 %c, 59
428 br i1 %cmp19, label %lor.end, label %lor.lhs.false21
430 lor.lhs.false21: ; preds = %lor.lhs.false16
431 %cmp24 = icmp eq i8 %c, 60
432 br i1 %cmp24, label %lor.end, label %lor.lhs.false26
434 lor.lhs.false26: ; preds = %lor.lhs.false21
435 %cmp29 = icmp eq i8 %c, 62
436 br i1 %cmp29, label %lor.end, label %lor.lhs.false31
438 lor.lhs.false31: ; preds = %lor.lhs.false26
439 %cmp34 = icmp eq i8 %c, 34
440 br i1 %cmp34, label %lor.end, label %lor.lhs.false36
442 lor.lhs.false36: ; preds = %lor.lhs.false31
443 %cmp39 = icmp eq i8 %c, 92
444 br i1 %cmp39, label %lor.end, label %lor.rhs
446 lor.rhs: ; preds = %lor.lhs.false36
447 %cmp43 = icmp eq i8 %c, 39
450 lor.end: ; preds = %lor.rhs, %lor.lhs.false36, %lor.lhs.false31, %lor.lhs.false26, %lor.lhs.false21, %lor.lhs.false16, %lor.lhs.false11, %lor.lhs.false6, %lor.lhs.false, %entry
451 %0 = phi i1 [ true, %lor.lhs.false36 ], [ true, %lor.lhs.false31 ], [ true, %lor.lhs.false26 ], [ true, %lor.lhs.false21 ], [ true, %lor.lhs.false16 ], [ true, %lor.lhs.false11 ], [ true, %lor.lhs.false6 ], [ true, %lor.lhs.false ], [ true, %entry ], [ %cmp43, %lor.rhs ]
452 %conv46 = zext i1 %0 to i32
458 define i32 @test10(i32 %mode, i1 %Cond) {
459 ; CHECK-LABEL: @test10(
460 ; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[COND:%.*]]
461 ; CHECK-NEXT: br i1 [[TMP1]], label [[SWITCH_EARLY_TEST:%.*]], label [[F:%.*]]
462 ; CHECK: switch.early.test:
463 ; CHECK-NEXT: switch i32 [[MODE:%.*]], label [[T:%.*]] [
464 ; CHECK-NEXT: i32 51, label [[F]]
465 ; CHECK-NEXT: i32 0, label [[F]]
468 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ 123, [[T]] ], [ 324, [[F]] ]
469 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
471 ; CHECK-NEXT: call void @foo1()
472 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
474 ; CHECK-NEXT: call void @foo2()
475 ; CHECK-NEXT: br label [[COMMON_RET]]
477 %A = icmp ne i32 %mode, 0
478 %B = icmp ne i32 %mode, 51
480 %D = and i1 %C, %Cond
481 br i1 %D, label %T, label %F
491 define i32 @test10_select(i32 %mode, i1 %Cond) {
492 ; CHECK-LABEL: @test10_select(
493 ; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[COND:%.*]]
494 ; CHECK-NEXT: br i1 [[TMP1]], label [[SWITCH_EARLY_TEST:%.*]], label [[F:%.*]]
495 ; CHECK: switch.early.test:
496 ; CHECK-NEXT: switch i32 [[MODE:%.*]], label [[T:%.*]] [
497 ; CHECK-NEXT: i32 51, label [[F]]
498 ; CHECK-NEXT: i32 0, label [[F]]
501 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ 123, [[T]] ], [ 324, [[F]] ]
502 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
504 ; CHECK-NEXT: call void @foo1()
505 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
507 ; CHECK-NEXT: call void @foo2()
508 ; CHECK-NEXT: br label [[COMMON_RET]]
510 %A = icmp ne i32 %mode, 0
511 %B = icmp ne i32 %mode, 51
512 %C = select i1 %A, i1 %B, i1 false
513 %D = select i1 %C, i1 %Cond, i1 false
514 br i1 %D, label %T, label %F
524 ; TODO: %Cond doesn't need freeze
525 define i32 @test10_select_and(i32 %mode, i1 %Cond) {
526 ; CHECK-LABEL: @test10_select_and(
527 ; CHECK-NEXT: [[TMP1:%.*]] = freeze i1 [[COND:%.*]]
528 ; CHECK-NEXT: br i1 [[TMP1]], label [[SWITCH_EARLY_TEST:%.*]], label [[F:%.*]]
529 ; CHECK: switch.early.test:
530 ; CHECK-NEXT: switch i32 [[MODE:%.*]], label [[T:%.*]] [
531 ; CHECK-NEXT: i32 51, label [[F]]
532 ; CHECK-NEXT: i32 0, label [[F]]
535 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ 123, [[T]] ], [ 324, [[F]] ]
536 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
538 ; CHECK-NEXT: call void @foo1()
539 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
541 ; CHECK-NEXT: call void @foo2()
542 ; CHECK-NEXT: br label [[COMMON_RET]]
544 %A = icmp ne i32 %mode, 0
545 %B = icmp ne i32 %mode, 51
546 %C = select i1 %A, i1 %B, i1 false
547 %D = and i1 %C, %Cond
548 br i1 %D, label %T, label %F
558 define i32 @test10_select_nofreeze(i32 %mode, i1 noundef %Cond) {
559 ; CHECK-LABEL: @test10_select_nofreeze(
560 ; CHECK-NEXT: br i1 [[COND:%.*]], label [[SWITCH_EARLY_TEST:%.*]], label [[F:%.*]]
561 ; CHECK: switch.early.test:
562 ; CHECK-NEXT: switch i32 [[MODE:%.*]], label [[T:%.*]] [
563 ; CHECK-NEXT: i32 51, label [[F]]
564 ; CHECK-NEXT: i32 0, label [[F]]
567 ; CHECK-NEXT: [[COMMON_RET_OP:%.*]] = phi i32 [ 123, [[T]] ], [ 324, [[F]] ]
568 ; CHECK-NEXT: ret i32 [[COMMON_RET_OP]]
570 ; CHECK-NEXT: call void @foo1()
571 ; CHECK-NEXT: br label [[COMMON_RET:%.*]]
573 ; CHECK-NEXT: call void @foo2()
574 ; CHECK-NEXT: br label [[COMMON_RET]]
576 %A = icmp ne i32 %mode, 0
577 %B = icmp ne i32 %mode, 51
578 %C = select i1 %A, i1 %B, i1 false
579 %D = select i1 %C, i1 %Cond, i1 false
580 br i1 %D, label %T, label %F
591 define i32 @test11(i32 %bar) nounwind {
592 ; CHECK-LABEL: @test11(
594 ; CHECK-NEXT: switch i32 [[BAR:%.*]], label [[IF_END:%.*]] [
595 ; CHECK-NEXT: i32 55, label [[RETURN:%.*]]
596 ; CHECK-NEXT: i32 53, label [[RETURN]]
597 ; CHECK-NEXT: i32 35, label [[RETURN]]
598 ; CHECK-NEXT: i32 24, label [[RETURN]]
599 ; CHECK-NEXT: i32 23, label [[RETURN]]
600 ; CHECK-NEXT: i32 12, label [[RETURN]]
601 ; CHECK-NEXT: i32 4, label [[RETURN]]
604 ; CHECK-NEXT: br label [[RETURN]]
606 ; CHECK-NEXT: [[RETVAL_0:%.*]] = phi i32 [ 0, [[IF_END]] ], [ 1, [[ENTRY:%.*]] ], [ 1, [[ENTRY]] ], [ 1, [[ENTRY]] ], [ 1, [[ENTRY]] ], [ 1, [[ENTRY]] ], [ 1, [[ENTRY]] ], [ 1, [[ENTRY]] ]
607 ; CHECK-NEXT: ret i32 [[RETVAL_0]]
610 %cmp = icmp eq i32 %bar, 4
611 %cmp2 = icmp eq i32 %bar, 35
612 %or.cond = or i1 %cmp, %cmp2
613 %cmp5 = icmp eq i32 %bar, 53
614 %or.cond1 = or i1 %or.cond, %cmp5
615 %cmp8 = icmp eq i32 %bar, 24
616 %or.cond2 = or i1 %or.cond1, %cmp8
617 %cmp11 = icmp eq i32 %bar, 23
618 %or.cond3 = or i1 %or.cond2, %cmp11
619 %cmp14 = icmp eq i32 %bar, 55
620 %or.cond4 = or i1 %or.cond3, %cmp14
621 %cmp17 = icmp eq i32 %bar, 12
622 %or.cond5 = or i1 %or.cond4, %cmp17
623 %cmp20 = icmp eq i32 %bar, 35
624 %or.cond6 = or i1 %or.cond5, %cmp20
625 br i1 %or.cond6, label %if.then, label %if.end
627 if.then: ; preds = %entry
630 if.end: ; preds = %entry
633 return: ; preds = %if.end, %if.then
634 %retval.0 = phi i32 [ 1, %if.then ], [ 0, %if.end ]
639 define void @test12() nounwind {
640 ; CHECK-LABEL: @test12(
642 ; CHECK-NEXT: [[A_OLD:%.*]] = icmp eq i32 undef, undef
643 ; CHECK-NEXT: br i1 [[A_OLD]], label [[BB55_US_US:%.*]], label [[MALFORMED:%.*]]
645 ; CHECK-NEXT: [[B:%.*]] = icmp ugt i32 undef, undef
646 ; CHECK-NEXT: [[A:%.*]] = icmp eq i32 undef, undef
647 ; CHECK-NEXT: [[OR_COND:%.*]] = or i1 [[B]], [[A]]
648 ; CHECK-NEXT: br i1 [[OR_COND]], label [[BB55_US_US]], label [[MALFORMED]]
650 ; CHECK-NEXT: ret void
656 %A = icmp eq i32 undef, undef
657 br i1 %A, label %bb55.us.us, label %malformed
660 %B = icmp ugt i32 undef, undef
661 br i1 %B, label %bb55.us.us, label %bb49.us.us
671 ; test13 - handle switch formation with ult.
672 define void @test13(i32 %x) nounwind ssp noredzone {
673 ; CHECK-LABEL: @test13(
675 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[IF_END:%.*]] [
676 ; CHECK-NEXT: i32 6, label [[IF_THEN:%.*]]
677 ; CHECK-NEXT: i32 4, label [[IF_THEN]]
678 ; CHECK-NEXT: i32 3, label [[IF_THEN]]
679 ; CHECK-NEXT: i32 1, label [[IF_THEN]]
680 ; CHECK-NEXT: i32 0, label [[IF_THEN]]
683 ; CHECK-NEXT: call void @foo1() #[[ATTR3:[0-9]+]]
684 ; CHECK-NEXT: br label [[IF_END]]
686 ; CHECK-NEXT: ret void
689 %cmp = icmp ult i32 %x, 2
690 br i1 %cmp, label %if.then, label %lor.lhs.false3
692 lor.lhs.false3: ; preds = %lor.lhs.false
693 %cmp5 = icmp eq i32 %x, 3
694 br i1 %cmp5, label %if.then, label %lor.lhs.false6
696 lor.lhs.false6: ; preds = %lor.lhs.false3
697 %cmp8 = icmp eq i32 %x, 4
698 br i1 %cmp8, label %if.then, label %lor.lhs.false9
700 lor.lhs.false9: ; preds = %lor.lhs.false6
701 %cmp11 = icmp eq i32 %x, 6
702 br i1 %cmp11, label %if.then, label %if.end
704 if.then: ; preds = %lor.lhs.false9, %lor.lhs.false6, %lor.lhs.false3, %lor.lhs.false, %entry
705 call void @foo1() noredzone
708 if.end: ; preds = %if.then, %lor.lhs.false9
712 ; test14 - handle switch formation with ult.
713 define void @test14(i32 %x) nounwind ssp noredzone {
714 ; CHECK-LABEL: @test14(
716 ; CHECK-NEXT: switch i32 [[X:%.*]], label [[IF_END:%.*]] [
717 ; CHECK-NEXT: i32 6, label [[IF_THEN:%.*]]
718 ; CHECK-NEXT: i32 4, label [[IF_THEN]]
719 ; CHECK-NEXT: i32 3, label [[IF_THEN]]
720 ; CHECK-NEXT: i32 2, label [[IF_THEN]]
721 ; CHECK-NEXT: i32 1, label [[IF_THEN]]
722 ; CHECK-NEXT: i32 0, label [[IF_THEN]]
725 ; CHECK-NEXT: call void @foo1() #[[ATTR3]]
726 ; CHECK-NEXT: br label [[IF_END]]
728 ; CHECK-NEXT: ret void
731 %cmp = icmp ugt i32 %x, 2
732 br i1 %cmp, label %lor.lhs.false3, label %if.then
734 lor.lhs.false3: ; preds = %lor.lhs.false
735 %cmp5 = icmp ne i32 %x, 3
736 br i1 %cmp5, label %lor.lhs.false6, label %if.then
738 lor.lhs.false6: ; preds = %lor.lhs.false3
739 %cmp8 = icmp ne i32 %x, 4
740 br i1 %cmp8, label %lor.lhs.false9, label %if.then
742 lor.lhs.false9: ; preds = %lor.lhs.false6
743 %cmp11 = icmp ne i32 %x, 6
744 br i1 %cmp11, label %if.end, label %if.then
746 if.then: ; preds = %lor.lhs.false9, %lor.lhs.false6, %lor.lhs.false3, %lor.lhs.false, %entry
747 call void @foo1() noredzone
750 if.end: ; preds = %if.then, %lor.lhs.false9
754 ; Don't crash on ginormous ranges.
755 define void @test15(i128 %x) nounwind {
756 ; CHECK-LABEL: @test15(
757 ; CHECK-NEXT: if.end:
758 ; CHECK-NEXT: [[CMP:%.*]] = icmp ugt i128 [[X:%.*]], 2
759 ; CHECK-NEXT: ret void
761 %cmp = icmp ugt i128 %x, 2
762 br i1 %cmp, label %if.end, label %lor.false
765 %cmp2 = icmp ne i128 %x, 100000000000000000000
766 br i1 %cmp2, label %if.end, label %if.then
769 call void @foo1() noredzone
779 define zeroext i1 @test16(i32 %x) nounwind {
780 ; CHECK-LABEL: @test16(
782 ; CHECK-NEXT: [[X_OFF:%.*]] = add i32 [[X:%.*]], -1
783 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[X_OFF]], 3
784 ; CHECK-NEXT: [[SPEC_SELECT:%.*]] = select i1 [[SWITCH]], i1 true, i1 false
785 ; CHECK-NEXT: ret i1 [[SPEC_SELECT]]
788 %cmp.i = icmp eq i32 %x, 1
789 br i1 %cmp.i, label %lor.end, label %lor.lhs.false
792 %cmp.i2 = icmp eq i32 %x, 2
793 br i1 %cmp.i2, label %lor.end, label %lor.rhs
796 %cmp.i1 = icmp eq i32 %x, 3
800 %0 = phi i1 [ true, %lor.lhs.false ], [ true, %entry ], [ %cmp.i1, %lor.rhs ]
804 ; Check that we don't turn an icmp into a switch where it's not useful.
805 define void @test17(i32 %x, i32 %y) {
806 ; CHECK-LABEL: @test17(
807 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 3
808 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[Y:%.*]], 2
809 ; CHECK-NEXT: [[OR_COND775:%.*]] = or i1 [[CMP]], [[SWITCH]]
810 ; CHECK-NEXT: br i1 [[OR_COND775]], label [[LOR_LHS_FALSE8:%.*]], label [[COMMON_RET:%.*]]
812 ; CHECK-NEXT: ret void
813 ; CHECK: lor.lhs.false8:
814 ; CHECK-NEXT: tail call void @foo1()
815 ; CHECK-NEXT: br label [[COMMON_RET]]
817 %cmp = icmp ult i32 %x, 3
818 %switch = icmp ult i32 %y, 2
819 %or.cond775 = or i1 %cmp, %switch
820 br i1 %or.cond775, label %lor.lhs.false8, label %return
823 tail call void @foo1()
831 define void @test17_select(i32 %x, i32 %y) {
832 ; CHECK-LABEL: @test17_select(
833 ; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X:%.*]], 3
834 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[Y:%.*]], 2
835 ; CHECK-NEXT: [[OR_COND775:%.*]] = select i1 [[CMP]], i1 true, i1 [[SWITCH]]
836 ; CHECK-NEXT: br i1 [[OR_COND775]], label [[LOR_LHS_FALSE8:%.*]], label [[COMMON_RET:%.*]]
838 ; CHECK-NEXT: ret void
839 ; CHECK: lor.lhs.false8:
840 ; CHECK-NEXT: tail call void @foo1()
841 ; CHECK-NEXT: br label [[COMMON_RET]]
843 %cmp = icmp ult i32 %x, 3
844 %switch = icmp ult i32 %y, 2
845 %or.cond775 = select i1 %cmp, i1 true, i1 %switch
846 br i1 %or.cond775, label %lor.lhs.false8, label %return
849 tail call void @foo1()
857 define void @test18(i32 %arg) {
858 ; CHECK-LABEL: @test18(
860 ; CHECK-NEXT: [[ARG_OFF:%.*]] = add i32 [[ARG:%.*]], -8
861 ; CHECK-NEXT: [[SWITCH:%.*]] = icmp ult i32 [[ARG_OFF]], 11
862 ; CHECK-NEXT: br i1 [[SWITCH]], label [[BB19:%.*]], label [[BB20:%.*]]
864 ; CHECK-NEXT: tail call void @foo1()
865 ; CHECK-NEXT: br label [[BB20]]
867 ; CHECK-NEXT: ret void
870 %tmp = and i32 %arg, -2
871 %tmp1 = icmp eq i32 %tmp, 8
872 %tmp2 = icmp eq i32 %arg, 10
873 %tmp3 = or i1 %tmp1, %tmp2
874 %tmp4 = icmp eq i32 %arg, 11
875 %tmp5 = or i1 %tmp3, %tmp4
876 %tmp6 = icmp eq i32 %arg, 12
877 %tmp7 = or i1 %tmp5, %tmp6
878 br i1 %tmp7, label %bb19, label %bb8
881 %tmp9 = add i32 %arg, -13
882 %tmp10 = icmp ult i32 %tmp9, 2
883 %tmp11 = icmp eq i32 %arg, 16
884 %tmp12 = or i1 %tmp10, %tmp11
885 %tmp13 = icmp eq i32 %arg, 17
886 %tmp14 = or i1 %tmp12, %tmp13
887 %tmp15 = icmp eq i32 %arg, 18
888 %tmp16 = or i1 %tmp14, %tmp15
889 %tmp17 = icmp eq i32 %arg, 15
890 %tmp18 = or i1 %tmp16, %tmp17
891 br i1 %tmp18, label %bb19, label %bb20
893 bb19: ; preds = %bb8, %bb
894 tail call void @foo1()
897 bb20: ; preds = %bb19, %bb8
902 define void @PR26323(i1 %tobool23, i32 %tmp3) {
903 ; CHECK-LABEL: @PR26323(
905 ; CHECK-NEXT: [[TOBOOL5:%.*]] = icmp ne i32 [[TMP3:%.*]], 0
906 ; CHECK-NEXT: [[NEG14:%.*]] = and i32 [[TMP3]], -2
907 ; CHECK-NEXT: [[CMP17:%.*]] = icmp ne i32 [[NEG14]], -1
908 ; CHECK-NEXT: [[OR_COND:%.*]] = and i1 [[TOBOOL5]], [[TOBOOL23:%.*]]
909 ; CHECK-NEXT: [[OR_COND1:%.*]] = and i1 [[CMP17]], [[OR_COND]]
910 ; CHECK-NEXT: br i1 [[OR_COND1]], label [[IF_END29:%.*]], label [[IF_THEN27:%.*]]
912 ; CHECK-NEXT: call void @foo1()
913 ; CHECK-NEXT: unreachable
915 ; CHECK-NEXT: ret void
918 %tobool5 = icmp ne i32 %tmp3, 0
919 %neg14 = and i32 %tmp3, -2
920 %cmp17 = icmp ne i32 %neg14, -1
921 %or.cond = and i1 %tobool5, %tobool23
922 %or.cond1 = and i1 %cmp17, %or.cond
923 br i1 %or.cond1, label %if.end29, label %if.then27
925 if.then27: ; preds = %entry
929 if.end29: ; preds = %entry
933 ; Form a switch when and'ing a negated power of two
934 define void @test19(i32 %arg) {
935 ; CHECK-LABEL: @test19(
936 ; CHECK-NEXT: switch i32 [[ARG:%.*]], label [[COMMON_RET:%.*]] [
937 ; CHECK-NEXT: i32 32, label [[IF:%.*]]
938 ; CHECK-NEXT: i32 13, label [[IF]]
939 ; CHECK-NEXT: i32 12, label [[IF]]
942 ; CHECK-NEXT: ret void
944 ; CHECK-NEXT: call void @foo1()
945 ; CHECK-NEXT: br label [[COMMON_RET]]
947 %and = and i32 %arg, -2
948 %cmp1 = icmp eq i32 %and, 12
949 %cmp2 = icmp eq i32 %arg, 32
950 %pred = or i1 %cmp1, %cmp2
951 br i1 %pred, label %if, label %else
961 define void @test19_select(i32 %arg) {
962 ; CHECK-LABEL: @test19_select(
963 ; CHECK-NEXT: switch i32 [[ARG:%.*]], label [[COMMON_RET:%.*]] [
964 ; CHECK-NEXT: i32 32, label [[IF:%.*]]
965 ; CHECK-NEXT: i32 13, label [[IF]]
966 ; CHECK-NEXT: i32 12, label [[IF]]
969 ; CHECK-NEXT: ret void
971 ; CHECK-NEXT: call void @foo1()
972 ; CHECK-NEXT: br label [[COMMON_RET]]
974 %and = and i32 %arg, -2
975 %cmp1 = icmp eq i32 %and, 12
976 %cmp2 = icmp eq i32 %arg, 32
977 %pred = select i1 %cmp1, i1 true, i1 %cmp2
978 br i1 %pred, label %if, label %else
988 ; Since %cmp1 is always false, a switch is never formed
989 define void @test20(i32 %arg) {
990 ; CHECK-LABEL: @test20(
991 ; CHECK-NEXT: [[AND:%.*]] = and i32 [[ARG:%.*]], -2
992 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[AND]], 13
993 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[ARG]], 32
994 ; CHECK-NEXT: [[PRED:%.*]] = or i1 [[CMP1]], [[CMP2]]
995 ; CHECK-NEXT: br i1 [[PRED]], label [[IF:%.*]], label [[COMMON_RET:%.*]]
997 ; CHECK-NEXT: ret void
999 ; CHECK-NEXT: call void @foo1()
1000 ; CHECK-NEXT: br label [[COMMON_RET]]
1002 %and = and i32 %arg, -2
1003 %cmp1 = icmp eq i32 %and, 13
1004 %cmp2 = icmp eq i32 %arg, 32
1005 %pred = or i1 %cmp1, %cmp2
1006 br i1 %pred, label %if, label %else
1016 ; Form a switch when or'ing a power of two
1017 define void @test21(i32 %arg) {
1018 ; CHECK-LABEL: @test21(
1019 ; CHECK-NEXT: switch i32 [[ARG:%.*]], label [[IF:%.*]] [
1020 ; CHECK-NEXT: i32 32, label [[COMMON_RET:%.*]]
1021 ; CHECK-NEXT: i32 13, label [[COMMON_RET]]
1022 ; CHECK-NEXT: i32 12, label [[COMMON_RET]]
1024 ; CHECK: common.ret:
1025 ; CHECK-NEXT: ret void
1027 ; CHECK-NEXT: call void @foo1()
1028 ; CHECK-NEXT: br label [[COMMON_RET]]
1030 %and = or i32 %arg, 1
1031 %cmp1 = icmp ne i32 %and, 13
1032 %cmp2 = icmp ne i32 %arg, 32
1033 %pred = and i1 %cmp1, %cmp2
1034 br i1 %pred, label %if, label %else
1044 ; Since %cmp1 is always false, a switch is never formed
1045 define void @test22(i32 %arg) {
1046 ; CHECK-LABEL: @test22(
1047 ; CHECK-NEXT: [[AND:%.*]] = or i32 [[ARG:%.*]], 1
1048 ; CHECK-NEXT: [[CMP1:%.*]] = icmp ne i32 [[AND]], 12
1049 ; CHECK-NEXT: [[CMP2:%.*]] = icmp ne i32 [[ARG]], 32
1050 ; CHECK-NEXT: [[PRED:%.*]] = and i1 [[CMP1]], [[CMP2]]
1051 ; CHECK-NEXT: br i1 [[PRED]], label [[IF:%.*]], label [[COMMON_RET:%.*]]
1052 ; CHECK: common.ret:
1053 ; CHECK-NEXT: ret void
1055 ; CHECK-NEXT: call void @foo1()
1056 ; CHECK-NEXT: br label [[COMMON_RET]]
1058 %and = or i32 %arg, 1
1059 %cmp1 = icmp ne i32 %and, 12
1060 %cmp2 = icmp ne i32 %arg, 32
1061 %pred = and i1 %cmp1, %cmp2
1062 br i1 %pred, label %if, label %else