1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt -passes=simplifycfg --switch-to-lookup -S < %s | FileCheck %s
4 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
6 ; https://alive2.llvm.org/ce/z/tuxLhJ
7 define i1 @switch_lookup_with_small_i1(i64 %x) {
8 ; CHECK-LABEL: @switch_lookup_with_small_i1(
10 ; CHECK-NEXT: [[AND:%.*]] = and i64 [[X:%.*]], 15
11 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[AND]], 11
12 ; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[AND]] to i11
13 ; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i11 [[SWITCH_CAST]], 1
14 ; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i11 -1018, [[SWITCH_SHIFTAMT]]
15 ; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i11 [[SWITCH_DOWNSHIFT]] to i1
16 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i1 [[SWITCH_MASKED]], i1 false
17 ; CHECK-NEXT: ret i1 [[TMP1]]
21 switch i64 %and, label %default [
22 i64 10, label %lor.end
27 default: ; preds = %entry
30 lor.end: ; preds = %entry, %entry, %entry, %default
31 %0 = phi i1 [ true, %entry ], [ false, %default ], [ true, %entry ], [ true, %entry ]
35 ; https://godbolt.org/z/sjbjorKon
36 define i8 @switch_lookup_with_small_i8(i64 %x) {
37 ; CHECK-LABEL: @switch_lookup_with_small_i8(
39 ; CHECK-NEXT: [[REM:%.*]] = urem i64 [[X:%.*]], 5
40 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[REM]], 3
41 ; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[REM]] to i24
42 ; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i24 [[SWITCH_CAST]], 8
43 ; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i24 460303, [[SWITCH_SHIFTAMT]]
44 ; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i24 [[SWITCH_DOWNSHIFT]] to i8
45 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i8 [[SWITCH_MASKED]], i8 0
46 ; CHECK-NEXT: ret i8 [[TMP1]]
50 switch i64 %rem, label %default [
56 sw.bb0: ; preds = %entry
59 sw.bb1: ; preds = %entry
62 sw.bb2: ; preds = %entry
65 default: ; preds = %entry
69 %0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]
73 ; Negative test: Table size would not fit the register.
74 define i8 @switch_lookup_with_small_i8_negative(i64 %x) {
75 ; CHECK-LABEL: @switch_lookup_with_small_i8_negative(
77 ; CHECK-NEXT: [[REM:%.*]] = urem i64 [[X:%.*]], 9
78 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ult i64 [[REM]], 3
79 ; CHECK-NEXT: [[SWITCH_CAST:%.*]] = trunc i64 [[REM]] to i24
80 ; CHECK-NEXT: [[SWITCH_SHIFTAMT:%.*]] = mul nuw nsw i24 [[SWITCH_CAST]], 8
81 ; CHECK-NEXT: [[SWITCH_DOWNSHIFT:%.*]] = lshr i24 460303, [[SWITCH_SHIFTAMT]]
82 ; CHECK-NEXT: [[SWITCH_MASKED:%.*]] = trunc i24 [[SWITCH_DOWNSHIFT]] to i8
83 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[TMP0]], i8 [[SWITCH_MASKED]], i8 0
84 ; CHECK-NEXT: ret i8 [[TMP1]]
87 %rem = urem i64 %x, 9 ; 9 * 8 = 72 > 64, not fit the register
88 switch i64 %rem, label %default [
94 sw.bb0: ; preds = %entry
97 sw.bb1: ; preds = %entry
100 sw.bb2: ; preds = %entry
103 default: ; preds = %entry
107 %0 = phi i8 [ 15, %sw.bb0 ], [ 6, %sw.bb1 ], [ 7, %sw.bb2 ], [ 0, %default ]