1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt -passes=instcombine -S < %s | FileCheck %s 3 4target datalayout = "e-m:e-i64:64-n8:16:32:64" 5 6declare i32 @llvm.bswap.i32(i32) 7declare i128 @llvm.bswap.i128(i128) 8declare <2 x i64> @llvm.bswap.v2i64(<2 x i64>) 9declare i32 @llvm.cttz.i32(i32, i1) nounwind readnone 10declare i32 @llvm.ctlz.i32(i32, i1) nounwind readnone 11declare i32 @llvm.ctpop.i32(i32) nounwind readnone 12declare <2 x i8> @llvm.cttz.v2i8(<2 x i8>, i1) nounwind readnone 13declare <2 x i8> @llvm.ctlz.v2i8(<2 x i8>, i1) nounwind readnone 14declare <2 x i8> @llvm.ctpop.v2i8(<2 x i8>) nounwind readnone 15 16declare void @use(i32) 17declare void @usevec(<3 x i14>) 18 19define i32 @lshr_ctlz_zero_is_not_undef(i32 %x) { 20; CHECK-LABEL: @lshr_ctlz_zero_is_not_undef( 21; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 22; CHECK-NEXT: [[SH:%.*]] = zext i1 [[TMP1]] to i32 23; CHECK-NEXT: ret i32 [[SH]] 24; 25 %ct = call i32 @llvm.ctlz.i32(i32 %x, i1 false) 26 %sh = lshr i32 %ct, 5 27 ret i32 %sh 28} 29 30define i32 @lshr_cttz_zero_is_not_undef(i32 %x) { 31; CHECK-LABEL: @lshr_cttz_zero_is_not_undef( 32; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], 0 33; CHECK-NEXT: [[SH:%.*]] = zext i1 [[TMP1]] to i32 34; CHECK-NEXT: ret i32 [[SH]] 35; 36 %ct = call i32 @llvm.cttz.i32(i32 %x, i1 false) 37 %sh = lshr i32 %ct, 5 38 ret i32 %sh 39} 40 41define i32 @lshr_ctpop(i32 %x) { 42; CHECK-LABEL: @lshr_ctpop( 43; CHECK-NEXT: [[TMP1:%.*]] = icmp eq i32 [[X:%.*]], -1 44; CHECK-NEXT: [[SH:%.*]] = zext i1 [[TMP1]] to i32 45; CHECK-NEXT: ret i32 [[SH]] 46; 47 %ct = call i32 @llvm.ctpop.i32(i32 %x) 48 %sh = lshr i32 %ct, 5 49 ret i32 %sh 50} 51 52define <2 x i8> @lshr_ctlz_zero_is_not_undef_splat_vec(<2 x i8> %x) { 53; CHECK-LABEL: @lshr_ctlz_zero_is_not_undef_splat_vec( 54; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer 55; CHECK-NEXT: [[SH:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8> 56; CHECK-NEXT: ret <2 x i8> [[SH]] 57; 58 %ct = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> %x, i1 false) 59 %sh = lshr <2 x i8> %ct, <i8 3, i8 3> 60 ret <2 x i8> %sh 61} 62 63define <2 x i8> @lshr_cttz_zero_is_not_undef_splat_vec(<2 x i8> %x) { 64; CHECK-LABEL: @lshr_cttz_zero_is_not_undef_splat_vec( 65; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], zeroinitializer 66; CHECK-NEXT: [[SH:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8> 67; CHECK-NEXT: ret <2 x i8> [[SH]] 68; 69 %ct = call <2 x i8> @llvm.cttz.v2i8(<2 x i8> %x, i1 false) 70 %sh = lshr <2 x i8> %ct, <i8 3, i8 3> 71 ret <2 x i8> %sh 72} 73 74define <2 x i8> @lshr_ctpop_splat_vec(<2 x i8> %x) { 75; CHECK-LABEL: @lshr_ctpop_splat_vec( 76; CHECK-NEXT: [[TMP1:%.*]] = icmp eq <2 x i8> [[X:%.*]], <i8 -1, i8 -1> 77; CHECK-NEXT: [[SH:%.*]] = zext <2 x i1> [[TMP1]] to <2 x i8> 78; CHECK-NEXT: ret <2 x i8> [[SH]] 79; 80 %ct = call <2 x i8> @llvm.ctpop.v2i8(<2 x i8> %x) 81 %sh = lshr <2 x i8> %ct, <i8 3, i8 3> 82 ret <2 x i8> %sh 83} 84 85define i32 @lshr_ctlz_zero_is_undef(i32 %x) { 86; CHECK-LABEL: @lshr_ctlz_zero_is_undef( 87; CHECK-NEXT: ret i32 0 88; 89 %ct = call i32 @llvm.ctlz.i32(i32 %x, i1 true) 90 %sh = lshr i32 %ct, 5 91 ret i32 %sh 92} 93 94define i32 @lshr_cttz_zero_is_undef(i32 %x) { 95; CHECK-LABEL: @lshr_cttz_zero_is_undef( 96; CHECK-NEXT: ret i32 0 97; 98 %ct = call i32 @llvm.cttz.i32(i32 %x, i1 true) 99 %sh = lshr i32 %ct, 5 100 ret i32 %sh 101} 102 103define <2 x i8> @lshr_ctlz_zero_is_undef_splat_vec(<2 x i8> %x) { 104; CHECK-LABEL: @lshr_ctlz_zero_is_undef_splat_vec( 105; CHECK-NEXT: ret <2 x i8> zeroinitializer 106; 107 %ct = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> %x, i1 true) 108 %sh = lshr <2 x i8> %ct, <i8 3, i8 3> 109 ret <2 x i8> %sh 110} 111 112define i8 @lshr_ctlz_zero_is_undef_vec(<2 x i8> %x) { 113; CHECK-LABEL: @lshr_ctlz_zero_is_undef_vec( 114; CHECK-NEXT: ret i8 0 115; 116 %ct = call <2 x i8> @llvm.ctlz.v2i8(<2 x i8> %x, i1 true) 117 %sh = lshr <2 x i8> %ct, <i8 3, i8 0> 118 %ex = extractelement <2 x i8> %sh, i32 0 119 ret i8 %ex 120} 121 122define <2 x i8> @lshr_cttz_zero_is_undef_splat_vec(<2 x i8> %x) { 123; CHECK-LABEL: @lshr_cttz_zero_is_undef_splat_vec( 124; CHECK-NEXT: ret <2 x i8> zeroinitializer 125; 126 %ct = call <2 x i8> @llvm.cttz.v2i8(<2 x i8> %x, i1 true) 127 %sh = lshr <2 x i8> %ct, <i8 3, i8 3> 128 ret <2 x i8> %sh 129} 130 131define i8 @lshr_cttz_zero_is_undef_vec(<2 x i8> %x) { 132; CHECK-LABEL: @lshr_cttz_zero_is_undef_vec( 133; CHECK-NEXT: ret i8 0 134; 135 %ct = call <2 x i8> @llvm.cttz.v2i8(<2 x i8> %x, i1 true) 136 %sh = lshr <2 x i8> %ct, <i8 3, i8 0> 137 %ex = extractelement <2 x i8> %sh, i32 0 138 ret i8 %ex 139} 140 141 142define i8 @lshr_exact(i8 %x) { 143; CHECK-LABEL: @lshr_exact( 144; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X:%.*]], 1 145; CHECK-NEXT: [[LSHR:%.*]] = and i8 [[TMP1]], 63 146; CHECK-NEXT: ret i8 [[LSHR]] 147; 148 %shl = shl i8 %x, 2 149 %add = add i8 %shl, 4 150 %lshr = lshr i8 %add, 2 151 ret i8 %lshr 152} 153 154define <2 x i8> @lshr_exact_splat_vec(<2 x i8> %x) { 155; CHECK-LABEL: @lshr_exact_splat_vec( 156; CHECK-NEXT: [[TMP1:%.*]] = add <2 x i8> [[X:%.*]], <i8 1, i8 1> 157; CHECK-NEXT: [[LSHR:%.*]] = and <2 x i8> [[TMP1]], <i8 63, i8 63> 158; CHECK-NEXT: ret <2 x i8> [[LSHR]] 159; 160 %shl = shl <2 x i8> %x, <i8 2, i8 2> 161 %add = add <2 x i8> %shl, <i8 4, i8 4> 162 %lshr = lshr <2 x i8> %add, <i8 2, i8 2> 163 ret <2 x i8> %lshr 164} 165 166define i8 @shl_add(i8 %x, i8 %y) { 167; CHECK-LABEL: @shl_add( 168; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 [[Y:%.*]], 2 169; CHECK-NEXT: [[TMP2:%.*]] = add i8 [[TMP1]], [[X:%.*]] 170; CHECK-NEXT: [[R:%.*]] = and i8 [[TMP2]], 63 171; CHECK-NEXT: ret i8 [[R]] 172; 173 %l = shl i8 %x, 2 174 %a = add i8 %l, %y 175 %r = lshr i8 %a, 2 176 ret i8 %r 177} 178 179define <2 x i8> @shl_add_commute_vec(<2 x i8> %x, <2 x i8> %py) { 180; CHECK-LABEL: @shl_add_commute_vec( 181; CHECK-NEXT: [[Y:%.*]] = mul <2 x i8> [[PY:%.*]], [[PY]] 182; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i8> [[Y]], <i8 3, i8 3> 183; CHECK-NEXT: [[TMP2:%.*]] = add <2 x i8> [[TMP1]], [[X:%.*]] 184; CHECK-NEXT: [[R:%.*]] = and <2 x i8> [[TMP2]], <i8 31, i8 31> 185; CHECK-NEXT: ret <2 x i8> [[R]] 186; 187 %y = mul <2 x i8> %py, %py ; thwart complexity-based canonicalization 188 %l = shl <2 x i8> %x, <i8 3, i8 3> 189 %a = add <2 x i8> %y, %l 190 %r = lshr <2 x i8> %a, <i8 3, i8 3> 191 ret <2 x i8> %r 192} 193 194define i32 @shl_add_use1(i32 %x, i32 %y) { 195; CHECK-LABEL: @shl_add_use1( 196; CHECK-NEXT: [[L:%.*]] = shl i32 [[X:%.*]], 2 197; CHECK-NEXT: call void @use(i32 [[L]]) 198; CHECK-NEXT: [[A:%.*]] = add i32 [[L]], [[Y:%.*]] 199; CHECK-NEXT: [[R:%.*]] = lshr i32 [[A]], 2 200; CHECK-NEXT: ret i32 [[R]] 201; 202 %l = shl i32 %x, 2 203 call void @use(i32 %l) 204 %a = add i32 %l, %y 205 %r = lshr i32 %a, 2 206 ret i32 %r 207} 208 209define i32 @shl_add_use2(i32 %x, i32 %y) { 210; CHECK-LABEL: @shl_add_use2( 211; CHECK-NEXT: [[L:%.*]] = shl i32 [[X:%.*]], 2 212; CHECK-NEXT: [[A:%.*]] = add i32 [[L]], [[Y:%.*]] 213; CHECK-NEXT: call void @use(i32 [[A]]) 214; CHECK-NEXT: [[R:%.*]] = lshr i32 [[A]], 2 215; CHECK-NEXT: ret i32 [[R]] 216; 217 %l = shl i32 %x, 2 218 %a = add i32 %l, %y 219 call void @use(i32 %a) 220 %r = lshr i32 %a, 2 221 ret i32 %r 222} 223 224define i16 @bool_zext(i1 %x) { 225; CHECK-LABEL: @bool_zext( 226; CHECK-NEXT: [[HIBIT:%.*]] = zext i1 [[X:%.*]] to i16 227; CHECK-NEXT: ret i16 [[HIBIT]] 228; 229 %sext = sext i1 %x to i16 230 %hibit = lshr i16 %sext, 15 231 ret i16 %hibit 232} 233 234define i32 @bool_zext_use(i1 %x) { 235; CHECK-LABEL: @bool_zext_use( 236; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[X:%.*]] to i32 237; CHECK-NEXT: call void @use(i32 [[SEXT]]) 238; CHECK-NEXT: [[HIBIT:%.*]] = zext i1 [[X]] to i32 239; CHECK-NEXT: ret i32 [[HIBIT]] 240; 241 %sext = sext i1 %x to i32 242 call void @use(i32 %sext) 243 %hibit = lshr i32 %sext, 31 244 ret i32 %hibit 245} 246 247define <2 x i8> @bool_zext_splat(<2 x i1> %x) { 248; CHECK-LABEL: @bool_zext_splat( 249; CHECK-NEXT: [[HIBIT:%.*]] = zext <2 x i1> [[X:%.*]] to <2 x i8> 250; CHECK-NEXT: ret <2 x i8> [[HIBIT]] 251; 252 %sext = sext <2 x i1> %x to <2 x i8> 253 %hibit = lshr <2 x i8> %sext, <i8 7, i8 7> 254 ret <2 x i8> %hibit 255} 256 257define i32 @smear_sign_and_widen(i8 %x) { 258; CHECK-LABEL: @smear_sign_and_widen( 259; CHECK-NEXT: [[TMP1:%.*]] = ashr i8 [[X:%.*]], 7 260; CHECK-NEXT: [[HIBIT:%.*]] = zext i8 [[TMP1]] to i32 261; CHECK-NEXT: ret i32 [[HIBIT]] 262; 263 %sext = sext i8 %x to i32 264 %hibit = lshr i32 %sext, 24 265 ret i32 %hibit 266} 267 268define i16 @smear_sign_and_widen_should_not_change_type(i4 %x) { 269; CHECK-LABEL: @smear_sign_and_widen_should_not_change_type( 270; CHECK-NEXT: [[SEXT:%.*]] = sext i4 [[X:%.*]] to i16 271; CHECK-NEXT: [[HIBIT:%.*]] = lshr i16 [[SEXT]], 12 272; CHECK-NEXT: ret i16 [[HIBIT]] 273; 274 %sext = sext i4 %x to i16 275 %hibit = lshr i16 %sext, 12 276 ret i16 %hibit 277} 278 279define <2 x i8> @smear_sign_and_widen_splat(<2 x i6> %x) { 280; CHECK-LABEL: @smear_sign_and_widen_splat( 281; CHECK-NEXT: [[TMP1:%.*]] = ashr <2 x i6> [[X:%.*]], <i6 2, i6 2> 282; CHECK-NEXT: [[HIBIT:%.*]] = zext <2 x i6> [[TMP1]] to <2 x i8> 283; CHECK-NEXT: ret <2 x i8> [[HIBIT]] 284; 285 %sext = sext <2 x i6> %x to <2 x i8> 286 %hibit = lshr <2 x i8> %sext, <i8 2, i8 2> 287 ret <2 x i8> %hibit 288} 289 290define i18 @fake_sext(i3 %x) { 291; CHECK-LABEL: @fake_sext( 292; CHECK-NEXT: [[TMP1:%.*]] = lshr i3 [[X:%.*]], 2 293; CHECK-NEXT: [[SH:%.*]] = zext i3 [[TMP1]] to i18 294; CHECK-NEXT: ret i18 [[SH]] 295; 296 %sext = sext i3 %x to i18 297 %sh = lshr i18 %sext, 17 298 ret i18 %sh 299} 300 301; Avoid the transform if it would change the shift from a legal to illegal type. 302 303define i32 @fake_sext_but_should_not_change_type(i3 %x) { 304; CHECK-LABEL: @fake_sext_but_should_not_change_type( 305; CHECK-NEXT: [[SEXT:%.*]] = sext i3 [[X:%.*]] to i32 306; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[SEXT]], 31 307; CHECK-NEXT: ret i32 [[SH]] 308; 309 %sext = sext i3 %x to i32 310 %sh = lshr i32 %sext, 31 311 ret i32 %sh 312} 313 314define <2 x i8> @fake_sext_splat(<2 x i3> %x) { 315; CHECK-LABEL: @fake_sext_splat( 316; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i3> [[X:%.*]], <i3 2, i3 2> 317; CHECK-NEXT: [[SH:%.*]] = zext <2 x i3> [[TMP1]] to <2 x i8> 318; CHECK-NEXT: ret <2 x i8> [[SH]] 319; 320 %sext = sext <2 x i3> %x to <2 x i8> 321 %sh = lshr <2 x i8> %sext, <i8 7, i8 7> 322 ret <2 x i8> %sh 323} 324 325; Use a narrow shift: lshr (zext iM X to iN), C --> zext (lshr X, C) to iN 326 327define <2 x i32> @narrow_lshr_constant(<2 x i8> %x, <2 x i8> %y) { 328; CHECK-LABEL: @narrow_lshr_constant( 329; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i8> [[X:%.*]], <i8 3, i8 3> 330; CHECK-NEXT: [[SH:%.*]] = zext <2 x i8> [[TMP1]] to <2 x i32> 331; CHECK-NEXT: ret <2 x i32> [[SH]] 332; 333 %zx = zext <2 x i8> %x to <2 x i32> 334 %sh = lshr <2 x i32> %zx, <i32 3, i32 3> 335 ret <2 x i32> %sh 336} 337 338define i32 @mul_splat_fold(i32 %x) { 339; CHECK-LABEL: @mul_splat_fold( 340; CHECK-NEXT: [[T:%.*]] = and i32 [[X:%.*]], 65535 341; CHECK-NEXT: ret i32 [[T]] 342; 343 %m = mul nuw i32 %x, 65537 344 %t = lshr i32 %m, 16 345 ret i32 %t 346} 347 348; Vector type, extra use, weird types are all ok. 349 350define <3 x i14> @mul_splat_fold_vec(<3 x i14> %x) { 351; CHECK-LABEL: @mul_splat_fold_vec( 352; CHECK-NEXT: [[M:%.*]] = mul nuw <3 x i14> [[X:%.*]], <i14 129, i14 129, i14 129> 353; CHECK-NEXT: call void @usevec(<3 x i14> [[M]]) 354; CHECK-NEXT: [[T:%.*]] = and <3 x i14> [[X]], <i14 127, i14 127, i14 127> 355; CHECK-NEXT: ret <3 x i14> [[T]] 356; 357 %m = mul nuw <3 x i14> %x, <i14 129, i14 129, i14 129> 358 call void @usevec(<3 x i14> %m) 359 %t = lshr <3 x i14> %m, <i14 7, i14 7, i14 7> 360 ret <3 x i14> %t 361} 362 363; Negative test 364 365define i32 @mul_splat_fold_wrong_mul_const(i32 %x) { 366; CHECK-LABEL: @mul_splat_fold_wrong_mul_const( 367; CHECK-NEXT: [[M:%.*]] = mul nuw i32 [[X:%.*]], 65538 368; CHECK-NEXT: [[T:%.*]] = lshr i32 [[M]], 16 369; CHECK-NEXT: ret i32 [[T]] 370; 371 %m = mul nuw i32 %x, 65538 372 %t = lshr i32 %m, 16 373 ret i32 %t 374} 375 376; Negative test 377 378define i32 @mul_splat_fold_wrong_lshr_const(i32 %x) { 379; CHECK-LABEL: @mul_splat_fold_wrong_lshr_const( 380; CHECK-NEXT: [[M:%.*]] = mul nuw i32 [[X:%.*]], 65537 381; CHECK-NEXT: [[T:%.*]] = lshr i32 [[M]], 15 382; CHECK-NEXT: ret i32 [[T]] 383; 384 %m = mul nuw i32 %x, 65537 385 %t = lshr i32 %m, 15 386 ret i32 %t 387} 388 389; Negative test 390 391define i32 @mul_splat_fold_no_nuw(i32 %x) { 392; CHECK-LABEL: @mul_splat_fold_no_nuw( 393; CHECK-NEXT: [[M:%.*]] = mul nsw i32 [[X:%.*]], 65537 394; CHECK-NEXT: [[T:%.*]] = lshr i32 [[M]], 16 395; CHECK-NEXT: ret i32 [[T]] 396; 397 %m = mul nsw i32 %x, 65537 398 %t = lshr i32 %m, 16 399 ret i32 %t 400} 401 402; Negative test (but simplifies before we reach the mul_splat transform)- need more than 2 bits 403 404define i2 @mul_splat_fold_too_narrow(i2 %x) { 405; CHECK-LABEL: @mul_splat_fold_too_narrow( 406; CHECK-NEXT: ret i2 [[X:%.*]] 407; 408 %m = mul nuw i2 %x, 2 409 %t = lshr i2 %m, 1 410 ret i2 %t 411} 412 413define i32 @negative_and_odd(i32 %x) { 414; CHECK-LABEL: @negative_and_odd( 415; CHECK-NEXT: [[TMP1:%.*]] = lshr i32 [[X:%.*]], 31 416; CHECK-NEXT: [[R:%.*]] = and i32 [[TMP1]], [[X]] 417; CHECK-NEXT: ret i32 [[R]] 418; 419 %s = srem i32 %x, 2 420 %r = lshr i32 %s, 31 421 ret i32 %r 422} 423 424define <2 x i7> @negative_and_odd_vec(<2 x i7> %x) { 425; CHECK-LABEL: @negative_and_odd_vec( 426; CHECK-NEXT: [[TMP1:%.*]] = lshr <2 x i7> [[X:%.*]], <i7 6, i7 6> 427; CHECK-NEXT: [[R:%.*]] = and <2 x i7> [[TMP1]], [[X]] 428; CHECK-NEXT: ret <2 x i7> [[R]] 429; 430 %s = srem <2 x i7> %x, <i7 2, i7 2> 431 %r = lshr <2 x i7> %s, <i7 6, i7 6> 432 ret <2 x i7> %r 433} 434 435; Negative test - this is still worth trying to avoid srem? 436 437define i32 @negative_and_odd_uses(i32 %x, i32* %p) { 438; CHECK-LABEL: @negative_and_odd_uses( 439; CHECK-NEXT: [[S:%.*]] = srem i32 [[X:%.*]], 2 440; CHECK-NEXT: store i32 [[S]], i32* [[P:%.*]], align 4 441; CHECK-NEXT: [[R:%.*]] = lshr i32 [[S]], 31 442; CHECK-NEXT: ret i32 [[R]] 443; 444 %s = srem i32 %x, 2 445 store i32 %s, i32* %p 446 %r = lshr i32 %s, 31 447 ret i32 %r 448} 449 450; Negative test - wrong divisor 451 452define i32 @srem3(i32 %x) { 453; CHECK-LABEL: @srem3( 454; CHECK-NEXT: [[S:%.*]] = srem i32 [[X:%.*]], 3 455; CHECK-NEXT: [[R:%.*]] = lshr i32 [[S]], 31 456; CHECK-NEXT: ret i32 [[R]] 457; 458 %s = srem i32 %x, 3 459 %r = lshr i32 %s, 31 460 ret i32 %r 461} 462 463; Negative test - wrong shift amount 464 465define i32 @srem2_lshr30(i32 %x) { 466; CHECK-LABEL: @srem2_lshr30( 467; CHECK-NEXT: [[S:%.*]] = srem i32 [[X:%.*]], 2 468; CHECK-NEXT: [[R:%.*]] = lshr i32 [[S]], 30 469; CHECK-NEXT: ret i32 [[R]] 470; 471 %s = srem i32 %x, 2 472 %r = lshr i32 %s, 30 473 ret i32 %r 474} 475 476define i12 @trunc_sandwich(i32 %x) { 477; CHECK-LABEL: @trunc_sandwich( 478; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X:%.*]], 30 479; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 480; CHECK-NEXT: ret i12 [[R1]] 481; 482 %sh = lshr i32 %x, 28 483 %tr = trunc i32 %sh to i12 484 %r = lshr i12 %tr, 2 485 ret i12 %r 486} 487 488define <2 x i12> @trunc_sandwich_splat_vec(<2 x i32> %x) { 489; CHECK-LABEL: @trunc_sandwich_splat_vec( 490; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr <2 x i32> [[X:%.*]], <i32 30, i32 30> 491; CHECK-NEXT: [[R1:%.*]] = trunc <2 x i32> [[SUM_SHIFT]] to <2 x i12> 492; CHECK-NEXT: ret <2 x i12> [[R1]] 493; 494 %sh = lshr <2 x i32> %x, <i32 22, i32 22> 495 %tr = trunc <2 x i32> %sh to <2 x i12> 496 %r = lshr <2 x i12> %tr, <i12 8, i12 8> 497 ret <2 x i12> %r 498} 499 500define i12 @trunc_sandwich_min_shift1(i32 %x) { 501; CHECK-LABEL: @trunc_sandwich_min_shift1( 502; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X:%.*]], 21 503; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 504; CHECK-NEXT: ret i12 [[R1]] 505; 506 %sh = lshr i32 %x, 20 507 %tr = trunc i32 %sh to i12 508 %r = lshr i12 %tr, 1 509 ret i12 %r 510} 511 512define i12 @trunc_sandwich_small_shift1(i32 %x) { 513; CHECK-LABEL: @trunc_sandwich_small_shift1( 514; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X:%.*]], 20 515; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 516; CHECK-NEXT: [[R:%.*]] = and i12 [[R1]], 2047 517; CHECK-NEXT: ret i12 [[R]] 518; 519 %sh = lshr i32 %x, 19 520 %tr = trunc i32 %sh to i12 521 %r = lshr i12 %tr, 1 522 ret i12 %r 523} 524 525define i12 @trunc_sandwich_max_sum_shift(i32 %x) { 526; CHECK-LABEL: @trunc_sandwich_max_sum_shift( 527; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X:%.*]], 31 528; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 529; CHECK-NEXT: ret i12 [[R1]] 530; 531 %sh = lshr i32 %x, 20 532 %tr = trunc i32 %sh to i12 533 %r = lshr i12 %tr, 11 534 ret i12 %r 535} 536 537define i12 @trunc_sandwich_max_sum_shift2(i32 %x) { 538; CHECK-LABEL: @trunc_sandwich_max_sum_shift2( 539; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X:%.*]], 31 540; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 541; CHECK-NEXT: ret i12 [[R1]] 542; 543 %sh = lshr i32 %x, 30 544 %tr = trunc i32 %sh to i12 545 %r = lshr i12 %tr, 1 546 ret i12 %r 547} 548 549define i12 @trunc_sandwich_big_sum_shift1(i32 %x) { 550; CHECK-LABEL: @trunc_sandwich_big_sum_shift1( 551; CHECK-NEXT: ret i12 0 552; 553 %sh = lshr i32 %x, 21 554 %tr = trunc i32 %sh to i12 555 %r = lshr i12 %tr, 11 556 ret i12 %r 557} 558 559define i12 @trunc_sandwich_big_sum_shift2(i32 %x) { 560; CHECK-LABEL: @trunc_sandwich_big_sum_shift2( 561; CHECK-NEXT: ret i12 0 562; 563 %sh = lshr i32 %x, 31 564 %tr = trunc i32 %sh to i12 565 %r = lshr i12 %tr, 1 566 ret i12 %r 567} 568 569define i12 @trunc_sandwich_use1(i32 %x) { 570; CHECK-LABEL: @trunc_sandwich_use1( 571; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 28 572; CHECK-NEXT: call void @use(i32 [[SH]]) 573; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X]], 30 574; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 575; CHECK-NEXT: ret i12 [[R1]] 576; 577 %sh = lshr i32 %x, 28 578 call void @use(i32 %sh) 579 %tr = trunc i32 %sh to i12 580 %r = lshr i12 %tr, 2 581 ret i12 %r 582} 583 584define <3 x i9> @trunc_sandwich_splat_vec_use1(<3 x i14> %x) { 585; CHECK-LABEL: @trunc_sandwich_splat_vec_use1( 586; CHECK-NEXT: [[SH:%.*]] = lshr <3 x i14> [[X:%.*]], <i14 6, i14 6, i14 6> 587; CHECK-NEXT: call void @usevec(<3 x i14> [[SH]]) 588; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr <3 x i14> [[X]], <i14 11, i14 11, i14 11> 589; CHECK-NEXT: [[R1:%.*]] = trunc <3 x i14> [[SUM_SHIFT]] to <3 x i9> 590; CHECK-NEXT: ret <3 x i9> [[R1]] 591; 592 %sh = lshr <3 x i14> %x, <i14 6, i14 6, i14 6> 593 call void @usevec(<3 x i14> %sh) 594 %tr = trunc <3 x i14> %sh to <3 x i9> 595 %r = lshr <3 x i9> %tr, <i9 5, i9 5, i9 5> 596 ret <3 x i9> %r 597} 598 599define i12 @trunc_sandwich_min_shift1_use1(i32 %x) { 600; CHECK-LABEL: @trunc_sandwich_min_shift1_use1( 601; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 20 602; CHECK-NEXT: call void @use(i32 [[SH]]) 603; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X]], 21 604; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 605; CHECK-NEXT: ret i12 [[R1]] 606; 607 %sh = lshr i32 %x, 20 608 call void @use(i32 %sh) 609 %tr = trunc i32 %sh to i12 610 %r = lshr i12 %tr, 1 611 ret i12 %r 612} 613 614; negative test - trunc is bigger than first shift 615 616define i12 @trunc_sandwich_small_shift1_use1(i32 %x) { 617; CHECK-LABEL: @trunc_sandwich_small_shift1_use1( 618; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 19 619; CHECK-NEXT: call void @use(i32 [[SH]]) 620; CHECK-NEXT: [[TR:%.*]] = trunc i32 [[SH]] to i12 621; CHECK-NEXT: [[R:%.*]] = lshr i12 [[TR]], 1 622; CHECK-NEXT: ret i12 [[R]] 623; 624 %sh = lshr i32 %x, 19 625 call void @use(i32 %sh) 626 %tr = trunc i32 %sh to i12 627 %r = lshr i12 %tr, 1 628 ret i12 %r 629} 630 631define i12 @trunc_sandwich_max_sum_shift_use1(i32 %x) { 632; CHECK-LABEL: @trunc_sandwich_max_sum_shift_use1( 633; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 20 634; CHECK-NEXT: call void @use(i32 [[SH]]) 635; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X]], 31 636; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 637; CHECK-NEXT: ret i12 [[R1]] 638; 639 %sh = lshr i32 %x, 20 640 call void @use(i32 %sh) 641 %tr = trunc i32 %sh to i12 642 %r = lshr i12 %tr, 11 643 ret i12 %r 644} 645 646define i12 @trunc_sandwich_max_sum_shift2_use1(i32 %x) { 647; CHECK-LABEL: @trunc_sandwich_max_sum_shift2_use1( 648; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 30 649; CHECK-NEXT: call void @use(i32 [[SH]]) 650; CHECK-NEXT: [[SUM_SHIFT:%.*]] = lshr i32 [[X]], 31 651; CHECK-NEXT: [[R1:%.*]] = trunc i32 [[SUM_SHIFT]] to i12 652; CHECK-NEXT: ret i12 [[R1]] 653; 654 %sh = lshr i32 %x, 30 655 call void @use(i32 %sh) 656 %tr = trunc i32 %sh to i12 657 %r = lshr i12 %tr, 1 658 ret i12 %r 659} 660 661; negative test - but overshift is simplified to zero by another fold 662 663define i12 @trunc_sandwich_big_sum_shift1_use1(i32 %x) { 664; CHECK-LABEL: @trunc_sandwich_big_sum_shift1_use1( 665; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 21 666; CHECK-NEXT: call void @use(i32 [[SH]]) 667; CHECK-NEXT: ret i12 0 668; 669 %sh = lshr i32 %x, 21 670 call void @use(i32 %sh) 671 %tr = trunc i32 %sh to i12 672 %r = lshr i12 %tr, 11 673 ret i12 %r 674} 675 676; negative test - but overshift is simplified to zero by another fold 677 678define i12 @trunc_sandwich_big_sum_shift2_use1(i32 %x) { 679; CHECK-LABEL: @trunc_sandwich_big_sum_shift2_use1( 680; CHECK-NEXT: [[SH:%.*]] = lshr i32 [[X:%.*]], 31 681; CHECK-NEXT: call void @use(i32 [[SH]]) 682; CHECK-NEXT: ret i12 0 683; 684 %sh = lshr i32 %x, 31 685 call void @use(i32 %sh) 686 %tr = trunc i32 %sh to i12 687 %r = lshr i12 %tr, 1 688 ret i12 %r 689} 690 691define i16 @lshr_sext_i1_to_i16(i1 %a) { 692; CHECK-LABEL: @lshr_sext_i1_to_i16( 693; CHECK-NEXT: [[LSHR:%.*]] = select i1 [[A:%.*]], i16 4095, i16 0 694; CHECK-NEXT: ret i16 [[LSHR]] 695; 696 %sext = sext i1 %a to i16 697 %lshr = lshr i16 %sext, 4 698 ret i16 %lshr 699} 700 701define i128 @lshr_sext_i1_to_i128(i1 %a) { 702; CHECK-LABEL: @lshr_sext_i1_to_i128( 703; CHECK-NEXT: [[LSHR:%.*]] = select i1 [[A:%.*]], i128 77371252455336267181195263, i128 0 704; CHECK-NEXT: ret i128 [[LSHR]] 705; 706 %sext = sext i1 %a to i128 707 %lshr = lshr i128 %sext, 42 708 ret i128 %lshr 709} 710 711define i32 @lshr_sext_i1_to_i32_use(i1 %a) { 712; CHECK-LABEL: @lshr_sext_i1_to_i32_use( 713; CHECK-NEXT: [[SEXT:%.*]] = sext i1 [[A:%.*]] to i32 714; CHECK-NEXT: call void @use(i32 [[SEXT]]) 715; CHECK-NEXT: [[LSHR:%.*]] = select i1 [[A]], i32 262143, i32 0 716; CHECK-NEXT: ret i32 [[LSHR]] 717; 718 %sext = sext i1 %a to i32 719 call void @use(i32 %sext) 720 %lshr = lshr i32 %sext, 14 721 ret i32 %lshr 722} 723 724define <3 x i14> @lshr_sext_i1_to_i14_splat_vec_use1(<3 x i1> %a) { 725; CHECK-LABEL: @lshr_sext_i1_to_i14_splat_vec_use1( 726; CHECK-NEXT: [[SEXT:%.*]] = sext <3 x i1> [[A:%.*]] to <3 x i14> 727; CHECK-NEXT: call void @usevec(<3 x i14> [[SEXT]]) 728; CHECK-NEXT: [[LSHR:%.*]] = select <3 x i1> [[A]], <3 x i14> <i14 1023, i14 1023, i14 1023>, <3 x i14> zeroinitializer 729; CHECK-NEXT: ret <3 x i14> [[LSHR]] 730; 731 %sext = sext <3 x i1> %a to <3 x i14> 732 call void @usevec(<3 x i14> %sext) 733 %lshr = lshr <3 x i14> %sext, <i14 4, i14 4, i14 4> 734 ret <3 x i14> %lshr 735} 736 737define i1 @icmp_ule(i32 %x, i32 %y) { 738; CHECK-LABEL: @icmp_ule( 739; CHECK-NEXT: ret i1 true 740; 741 %x.shifted = lshr i32 %x, %y 742 %cmp = icmp ule i32 %x.shifted, %x 743 ret i1 %cmp 744} 745 746define i1 @icmp_ult(i32 %x, i32 %y) { 747; CHECK-LABEL: @icmp_ult( 748; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 749; CHECK-NEXT: [[CMP:%.*]] = icmp ult i32 [[X_SHIFTED]], [[X]] 750; CHECK-NEXT: ret i1 [[CMP]] 751; 752 %x.shifted = lshr i32 %x, %y 753 %cmp = icmp ult i32 %x.shifted, %x 754 ret i1 %cmp 755} 756 757define i1 @icmp_eq(i32 %x, i32 %y) { 758; CHECK-LABEL: @icmp_eq( 759; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 760; CHECK-NEXT: [[CMP:%.*]] = icmp eq i32 [[X_SHIFTED]], [[X]] 761; CHECK-NEXT: ret i1 [[CMP]] 762; 763 %x.shifted = lshr i32 %x, %y 764 %cmp = icmp eq i32 %x.shifted, %x 765 ret i1 %cmp 766} 767 768define i1 @icmp_ne(i32 %x, i32 %y) { 769; CHECK-LABEL: @icmp_ne( 770; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 771; CHECK-NEXT: [[CMP:%.*]] = icmp ne i32 [[X_SHIFTED]], [[X]] 772; CHECK-NEXT: ret i1 [[CMP]] 773; 774 %x.shifted = lshr i32 %x, %y 775 %cmp = icmp ne i32 %x.shifted, %x 776 ret i1 %cmp 777} 778 779define i1 @icmp_ugt(i32 %x, i32 %y) { 780; CHECK-LABEL: @icmp_ugt( 781; CHECK-NEXT: ret i1 false 782; 783 %x.shifted = lshr i32 %x, %y 784 %cmp = icmp ugt i32 %x.shifted, %x 785 ret i1 %cmp 786} 787 788define i1 @icmp_uge(i32 %x, i32 %y) { 789; CHECK-LABEL: @icmp_uge( 790; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 791; CHECK-NEXT: [[CMP:%.*]] = icmp uge i32 [[X_SHIFTED]], [[X]] 792; CHECK-NEXT: ret i1 [[CMP]] 793; 794 %x.shifted = lshr i32 %x, %y 795 %cmp = icmp uge i32 %x.shifted, %x 796 ret i1 %cmp 797} 798 799define i1 @icmp_sle(i32 %x, i32 %y) { 800; CHECK-LABEL: @icmp_sle( 801; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 802; CHECK-NEXT: [[CMP:%.*]] = icmp sle i32 [[X_SHIFTED]], [[X]] 803; CHECK-NEXT: ret i1 [[CMP]] 804; 805 %x.shifted = lshr i32 %x, %y 806 %cmp = icmp sle i32 %x.shifted, %x 807 ret i1 %cmp 808} 809 810define i1 @icmp_slt(i32 %x, i32 %y) { 811; CHECK-LABEL: @icmp_slt( 812; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 813; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[X_SHIFTED]], [[X]] 814; CHECK-NEXT: ret i1 [[CMP]] 815; 816 %x.shifted = lshr i32 %x, %y 817 %cmp = icmp slt i32 %x.shifted, %x 818 ret i1 %cmp 819} 820 821define i1 @icmp_sgt(i32 %x, i32 %y) { 822; CHECK-LABEL: @icmp_sgt( 823; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 824; CHECK-NEXT: [[CMP:%.*]] = icmp sgt i32 [[X_SHIFTED]], [[X]] 825; CHECK-NEXT: ret i1 [[CMP]] 826; 827 %x.shifted = lshr i32 %x, %y 828 %cmp = icmp sgt i32 %x.shifted, %x 829 ret i1 %cmp 830} 831 832define i1 @icmp_sge(i32 %x, i32 %y) { 833; CHECK-LABEL: @icmp_sge( 834; CHECK-NEXT: [[X_SHIFTED:%.*]] = lshr i32 [[X:%.*]], [[Y:%.*]] 835; CHECK-NEXT: [[CMP:%.*]] = icmp sge i32 [[X_SHIFTED]], [[X]] 836; CHECK-NEXT: ret i1 [[CMP]] 837; 838 %x.shifted = lshr i32 %x, %y 839 %cmp = icmp sge i32 %x.shifted, %x 840 ret i1 %cmp 841} 842 843define i32 @narrow_bswap(i16 %x) { 844; CHECK-LABEL: @narrow_bswap( 845; CHECK-NEXT: [[TMP1:%.*]] = call i16 @llvm.bswap.i16(i16 [[X:%.*]]) 846; CHECK-NEXT: [[S:%.*]] = zext i16 [[TMP1]] to i32 847; CHECK-NEXT: ret i32 [[S]] 848; 849 %z = zext i16 %x to i32 850 %b = call i32 @llvm.bswap.i32(i32 %z) 851 %s = lshr i32 %b, 16 852 ret i32 %s 853} 854 855define i128 @narrow_bswap_extra_wide(i16 %x) { 856; CHECK-LABEL: @narrow_bswap_extra_wide( 857; CHECK-NEXT: [[TMP1:%.*]] = call i16 @llvm.bswap.i16(i16 [[X:%.*]]) 858; CHECK-NEXT: [[S:%.*]] = zext i16 [[TMP1]] to i128 859; CHECK-NEXT: ret i128 [[S]] 860; 861 %z = zext i16 %x to i128 862 %b = call i128 @llvm.bswap.i128(i128 %z) 863 %s = lshr i128 %b, 112 864 ret i128 %s 865} 866 867; TODO: The bswap can be narrowed followed by shl. 868 869define i32 @narrow_bswap_undershift(i16 %x) { 870; CHECK-LABEL: @narrow_bswap_undershift( 871; CHECK-NEXT: [[Z:%.*]] = zext i16 [[X:%.*]] to i32 872; CHECK-NEXT: [[B:%.*]] = call i32 @llvm.bswap.i32(i32 [[Z]]) 873; CHECK-NEXT: [[S:%.*]] = lshr exact i32 [[B]], 8 874; CHECK-NEXT: ret i32 [[S]] 875; 876 %z = zext i16 %x to i32 877 %b = call i32 @llvm.bswap.i32(i32 %z) 878 %s = lshr i32 %b, 8 879 ret i32 %s 880} 881 882define <2 x i64> @narrow_bswap_splat(<2 x i16> %x) { 883; CHECK-LABEL: @narrow_bswap_splat( 884; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i16> @llvm.bswap.v2i16(<2 x i16> [[X:%.*]]) 885; CHECK-NEXT: [[S:%.*]] = zext <2 x i16> [[TMP1]] to <2 x i64> 886; CHECK-NEXT: ret <2 x i64> [[S]] 887; 888 %z = zext <2 x i16> %x to <2 x i64> 889 %b = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> %z) 890 %s = lshr <2 x i64> %b, <i64 48, i64 48> 891 ret <2 x i64> %s 892} 893 894; TODO: poison/undef in the shift amount is ok to propagate. 895 896define <2 x i64> @narrow_bswap_splat_poison_elt(<2 x i16> %x) { 897; CHECK-LABEL: @narrow_bswap_splat_poison_elt( 898; CHECK-NEXT: [[Z:%.*]] = zext <2 x i16> [[X:%.*]] to <2 x i64> 899; CHECK-NEXT: [[B:%.*]] = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> [[Z]]) 900; CHECK-NEXT: [[S:%.*]] = lshr <2 x i64> [[B]], <i64 48, i64 poison> 901; CHECK-NEXT: ret <2 x i64> [[S]] 902; 903 %z = zext <2 x i16> %x to <2 x i64> 904 %b = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> %z) 905 %s = lshr <2 x i64> %b, <i64 48, i64 poison> 906 ret <2 x i64> %s 907} 908 909define <2 x i64> @narrow_bswap_overshift(<2 x i32> %x) { 910; CHECK-LABEL: @narrow_bswap_overshift( 911; CHECK-NEXT: [[TMP1:%.*]] = call <2 x i32> @llvm.bswap.v2i32(<2 x i32> [[X:%.*]]) 912; CHECK-NEXT: [[TMP2:%.*]] = lshr <2 x i32> [[TMP1]], <i32 16, i32 16> 913; CHECK-NEXT: [[S:%.*]] = zext <2 x i32> [[TMP2]] to <2 x i64> 914; CHECK-NEXT: ret <2 x i64> [[S]] 915; 916 %z = zext <2 x i32> %x to <2 x i64> 917 %b = call <2 x i64> @llvm.bswap.v2i64(<2 x i64> %z) 918 %s = lshr <2 x i64> %b, <i64 48, i64 48> 919 ret <2 x i64> %s 920} 921 922define i128 @narrow_bswap_overshift2(i96 %x) { 923; CHECK-LABEL: @narrow_bswap_overshift2( 924; CHECK-NEXT: [[TMP1:%.*]] = call i96 @llvm.bswap.i96(i96 [[X:%.*]]) 925; CHECK-NEXT: [[TMP2:%.*]] = lshr i96 [[TMP1]], 29 926; CHECK-NEXT: [[S:%.*]] = zext i96 [[TMP2]] to i128 927; CHECK-NEXT: ret i128 [[S]] 928; 929 %z = zext i96 %x to i128 930 %b = call i128 @llvm.bswap.i128(i128 %z) 931 %s = lshr i128 %b, 61 932 ret i128 %s 933} 934 935; negative test - can't make a bswap with an odd number of bytes 936 937define i32 @not_narrow_bswap(i24 %x) { 938; CHECK-LABEL: @not_narrow_bswap( 939; CHECK-NEXT: [[Z:%.*]] = zext i24 [[X:%.*]] to i32 940; CHECK-NEXT: [[B:%.*]] = call i32 @llvm.bswap.i32(i32 [[Z]]) 941; CHECK-NEXT: [[R:%.*]] = lshr exact i32 [[B]], 8 942; CHECK-NEXT: ret i32 [[R]] 943; 944 %z = zext i24 %x to i32 945 %b = call i32 @llvm.bswap.i32(i32 %z) 946 %r = lshr i32 %b, 8 947 ret i32 %r 948} 949