1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; Vary legal integer types in data layout. 3; RUN: opt < %s -instcombine -S -data-layout=n32 | FileCheck %s --check-prefix=ALL --check-prefix=CHECK32 4; RUN: opt < %s -instcombine -S -data-layout=n32:64 | FileCheck %s --check-prefix=ALL --check-prefix=CHECK64 5 6define i32 @positive1(i64 %a) { 7; ALL-LABEL: @positive1( 8; ALL: switch i32 9; ALL-NEXT: i32 10, label %return 10; ALL-NEXT: i32 100, label %sw.bb1 11; ALL-NEXT: i32 1001, label %sw.bb2 12; ALL-NEXT: ] 13; 14entry: 15 %and = and i64 %a, 4294967295 16 switch i64 %and, label %sw.default [ 17 i64 10, label %return 18 i64 100, label %sw.bb1 19 i64 1001, label %sw.bb2 20 ] 21 22sw.bb1: 23 br label %return 24 25sw.bb2: 26 br label %return 27 28sw.default: 29 br label %return 30 31return: 32 %retval.0 = phi i32 [ 24, %sw.default ], [ 123, %sw.bb2 ], [ 213, %sw.bb1 ], [ 231, %entry ] 33 ret i32 %retval.0 34} 35 36define i32 @negative1(i64 %a) { 37; ALL-LABEL: @negative1( 38; ALL: switch i32 39; ALL-NEXT: i32 -10, label %return 40; ALL-NEXT: i32 -100, label %sw.bb1 41; ALL-NEXT: i32 -1001, label %sw.bb2 42; ALL-NEXT: ] 43; 44entry: 45 %or = or i64 %a, -4294967296 46 switch i64 %or, label %sw.default [ 47 i64 -10, label %return 48 i64 -100, label %sw.bb1 49 i64 -1001, label %sw.bb2 50 ] 51 52sw.bb1: 53 br label %return 54 55sw.bb2: 56 br label %return 57 58sw.default: 59 br label %return 60 61return: 62 %retval.0 = phi i32 [ 24, %sw.default ], [ 123, %sw.bb2 ], [ 213, %sw.bb1 ], [ 231, %entry ] 63 ret i32 %retval.0 64} 65 66; Make sure truncating a constant int larger than 64-bit doesn't trigger an 67; assertion. 68 69define i32 @trunc72to68(i72 %a) { 70; ALL-LABEL: @trunc72to68( 71; ALL: switch i68 72; ALL-NEXT: i68 10, label %return 73; ALL-NEXT: i68 100, label %sw.bb1 74; ALL-NEXT: i68 1001, label %sw.bb2 75; ALL-NEXT: ] 76; 77entry: 78 %and = and i72 %a, 295147905179352825855 79 switch i72 %and, label %sw.default [ 80 i72 10, label %return 81 i72 100, label %sw.bb1 82 i72 1001, label %sw.bb2 83 ] 84 85sw.bb1: 86 br label %return 87 88sw.bb2: 89 br label %return 90 91sw.default: 92 br label %return 93 94return: 95 %retval.0 = phi i32 [ 24, %sw.default ], [ 123, %sw.bb2 ], [ 213, %sw.bb1 ], [ 231, %entry ] 96 ret i32 %retval.0 97} 98 99; Make sure to avoid assertion crashes and use the type before 100; truncation to generate the sub constant expressions that leads 101; to the recomputed condition. 102; We allow to truncate from i64 to i59 if in 32-bit mode, 103; because both are illegal. 104 105define void @trunc64to59(i64 %a) { 106; ALL-LABEL: @trunc64to59( 107; CHECK32: switch i59 108; CHECK32-NEXT: i59 0, label %sw.bb1 109; CHECK32-NEXT: i59 18717182647723699, label %sw.bb2 110; CHECK32-NEXT: ] 111; CHECK64: switch i64 112; CHECK64-NEXT: i64 0, label %sw.bb1 113; CHECK64-NEXT: i64 18717182647723699, label %sw.bb2 114; CHECK64-NEXT: ] 115; 116entry: 117 %tmp0 = and i64 %a, 15 118 %tmp1 = mul i64 %tmp0, -6425668444178048401 119 %tmp2 = add i64 %tmp1, 5170979678563097242 120 %tmp3 = mul i64 %tmp2, 1627972535142754813 121 switch i64 %tmp3, label %sw.default [ 122 i64 847514119312061490, label %sw.bb1 123 i64 866231301959785189, label %sw.bb2 124 ] 125 126sw.bb1: 127 br label %sw.default 128 129sw.bb2: 130 br label %sw.default 131 132sw.default: 133 ret void 134} 135 136; https://llvm.org/bugs/show_bug.cgi?id=31260 137 138define i8 @PR31260(i8 %x) { 139; ALL-LABEL: @PR31260( 140; ALL-NEXT: entry: 141; ALL-NEXT: [[TMP0:%.*]] = trunc i8 %x to i2 142; ALL-NEXT: [[TRUNC:%.*]] = and i2 [[TMP0]], -2 143; ALL-NEXT: switch i2 [[TRUNC]], label %exit [ 144; ALL-NEXT: i2 0, label %case126 145; ALL-NEXT: i2 -2, label %case124 146; ALL-NEXT: ] 147; ALL: exit: 148; ALL-NEXT: ret i8 1 149; ALL: case126: 150; ALL-NEXT: ret i8 3 151; ALL: case124: 152; ALL-NEXT: ret i8 5 153; 154entry: 155 %t4 = and i8 %x, 2 156 %t5 = add nsw i8 %t4, -126 157 switch i8 %t5, label %exit [ 158 i8 -126, label %case126 159 i8 -124, label %case124 160 ] 161 162exit: 163 ret i8 1 164case126: 165 ret i8 3 166case124: 167 ret i8 5 168} 169 170; Make sure the arithmetic evaluation of the switch 171; condition is evaluated on the original type 172define i32 @trunc32to16(i32 %a0) #0 { 173; ALL-LABEL: @trunc32to16( 174; ALL: switch i16 175; ALL-NEXT: i16 63, label %sw.bb 176; ALL-NEXT: i16 1, label %sw.bb1 177; ALL-NEXT: i16 100, label %sw.bb2 178; ALL-NEXT: ] 179; 180entry: 181 %retval = alloca i32, align 4 182 %xor = xor i32 %a0, 1034460917 183 %shr = lshr i32 %xor, 16 184 %add = add i32 %shr, -917677090 185 switch i32 %add, label %sw.epilog [ 186 i32 -917677027, label %sw.bb 187 i32 -917677089, label %sw.bb1 188 i32 -917676990, label %sw.bb2 189 ] 190 191sw.bb: ; preds = %entry 192 store i32 90, i32* %retval, align 4 193 br label %return 194 195sw.bb1: ; preds = %entry 196 store i32 91, i32* %retval, align 4 197 br label %return 198 199sw.bb2: ; preds = %entry 200 store i32 92, i32* %retval, align 4 201 br label %return 202 203sw.epilog: ; preds = %entry 204 store i32 113, i32* %retval, align 4 205 br label %return 206 207return: ; preds = %sw.epilog, %sw.bb2, 208 %rval = load i32, i32* %retval, align 4 209 ret i32 %rval 210} 211 212; https://llvm.org/bugs/show_bug.cgi?id=29009 213 214@a = global i32 0, align 4 215@njob = global i32 0, align 4 216 217declare i32 @goo() 218 219; Make sure we do not shrink to illegal types (i3 in this case) 220; if original type is legal (i32 in this case) 221 222define void @PR29009() { 223; ALL-LABEL: @PR29009( 224; ALL: switch i32 225; ALL-NEXT: i32 0, label 226; ALL-NEXT: i32 3, label 227; ALL-NEXT: ] 228; 229 br label %1 230 231; <label>:1: ; preds = %10, %0 232 %2 = load volatile i32, i32* @njob, align 4 233 %3 = icmp ne i32 %2, 0 234 br i1 %3, label %4, label %11 235 236; <label>:4: ; preds = %1 237 %5 = call i32 @goo() 238 %6 = and i32 %5, 7 239 switch i32 %6, label %7 [ 240 i32 0, label %8 241 i32 3, label %9 242 ] 243 244; <label>:7: ; preds = %4 245 store i32 6, i32* @a, align 4 246 br label %10 247 248; <label>:8: ; preds = %4 249 store i32 1, i32* @a, align 4 250 br label %10 251 252; <label>:9: ; preds = %4 253 store i32 2, i32* @a, align 4 254 br label %10 255 256; <label>:10: ; preds = %13, %12, %11, %10, %9, %8, %7 257 br label %1 258 259; <label>:11: ; preds = %1 260 ret void 261} 262 263