1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -passes=instcombine -S -data-layout="E" | FileCheck %s --check-prefixes=ALL,BE 3; RUN: opt < %s -passes=instcombine -S -data-layout="e" | FileCheck %s --check-prefixes=ALL,LE 4 5 6declare void @use(i16) 7declare void @use_vec(<8 x i16>) 8 9define <4 x i16> @insert_01_poison_v4i16(i32 %x) { 10; BE-LABEL: @insert_01_poison_v4i16( 11; BE-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 12; BE-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 13; BE-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 14; BE-NEXT: [[INS0:%.*]] = insertelement <4 x i16> poison, i16 [[LO16]], i64 0 15; BE-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[INS0]], i16 [[HI16]], i64 1 16; BE-NEXT: ret <4 x i16> [[INS1]] 17; 18; LE-LABEL: @insert_01_poison_v4i16( 19; LE-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i64 0 20; LE-NEXT: [[INS1:%.*]] = bitcast <2 x i32> [[TMP1]] to <4 x i16> 21; LE-NEXT: ret <4 x i16> [[INS1]] 22; 23 %hi32 = lshr i32 %x, 16 24 %hi16 = trunc i32 %hi32 to i16 25 %lo16 = trunc i32 %x to i16 26 %ins0 = insertelement <4 x i16> poison, i16 %lo16, i64 0 27 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 1 28 ret <4 x i16> %ins1 29} 30 31define <8 x i16> @insert_10_poison_v8i16(i32 %x) { 32; BE-LABEL: @insert_10_poison_v8i16( 33; BE-NEXT: [[TMP1:%.*]] = insertelement <4 x i32> poison, i32 [[X:%.*]], i64 0 34; BE-NEXT: [[INS1:%.*]] = bitcast <4 x i32> [[TMP1]] to <8 x i16> 35; BE-NEXT: ret <8 x i16> [[INS1]] 36; 37; LE-LABEL: @insert_10_poison_v8i16( 38; LE-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 39; LE-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 40; LE-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 41; LE-NEXT: [[TMP1:%.*]] = insertelement <8 x i16> poison, i16 [[HI16]], i64 0 42; LE-NEXT: [[INS1:%.*]] = insertelement <8 x i16> [[TMP1]], i16 [[LO16]], i64 1 43; LE-NEXT: ret <8 x i16> [[INS1]] 44; 45 %hi32 = lshr i32 %x, 16 46 %hi16 = trunc i32 %hi32 to i16 47 %lo16 = trunc i32 %x to i16 48 %ins0 = insertelement <8 x i16> poison, i16 %lo16, i64 1 49 %ins1 = insertelement <8 x i16> %ins0, i16 %hi16, i64 0 50 ret <8 x i16> %ins1 51} 52 53; negative test - larger element is not aligned in the vector 54 55define <4 x i32> @insert_12_poison_v4i32(i64 %x) { 56; ALL-LABEL: @insert_12_poison_v4i32( 57; ALL-NEXT: [[HI64:%.*]] = lshr i64 [[X:%.*]], 32 58; ALL-NEXT: [[HI32:%.*]] = trunc nuw i64 [[HI64]] to i32 59; ALL-NEXT: [[LO32:%.*]] = trunc i64 [[X]] to i32 60; ALL-NEXT: [[INS0:%.*]] = insertelement <4 x i32> poison, i32 [[LO32]], i64 1 61; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i32> [[INS0]], i32 [[HI32]], i64 2 62; ALL-NEXT: ret <4 x i32> [[INS1]] 63; 64 %hi64 = lshr i64 %x, 32 65 %hi32 = trunc i64 %hi64 to i32 66 %lo32 = trunc i64 %x to i32 67 %ins0 = insertelement <4 x i32> poison, i32 %lo32, i64 1 68 %ins1 = insertelement <4 x i32> %ins0, i32 %hi32, i64 2 69 ret <4 x i32> %ins1 70} 71 72; negative test - larger element is not aligned in the vector 73 74define <4 x i16> @insert_21_poison_v4i16(i32 %x) { 75; ALL-LABEL: @insert_21_poison_v4i16( 76; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 77; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 78; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 79; ALL-NEXT: [[TMP1:%.*]] = insertelement <4 x i16> poison, i16 [[HI16]], i64 1 80; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[LO16]], i64 2 81; ALL-NEXT: ret <4 x i16> [[INS1]] 82; 83 %hi32 = lshr i32 %x, 16 84 %hi16 = trunc i32 %hi32 to i16 85 %lo16 = trunc i32 %x to i16 86 %ins0 = insertelement <4 x i16> poison, i16 %lo16, i64 2 87 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 1 88 ret <4 x i16> %ins1 89} 90 91define <4 x i32> @insert_23_poison_v4i32(i64 %x) { 92; BE-LABEL: @insert_23_poison_v4i32( 93; BE-NEXT: [[HI64:%.*]] = lshr i64 [[X:%.*]], 32 94; BE-NEXT: [[HI32:%.*]] = trunc nuw i64 [[HI64]] to i32 95; BE-NEXT: [[LO32:%.*]] = trunc i64 [[X]] to i32 96; BE-NEXT: [[INS0:%.*]] = insertelement <4 x i32> poison, i32 [[LO32]], i64 2 97; BE-NEXT: [[INS1:%.*]] = insertelement <4 x i32> [[INS0]], i32 [[HI32]], i64 3 98; BE-NEXT: ret <4 x i32> [[INS1]] 99; 100; LE-LABEL: @insert_23_poison_v4i32( 101; LE-NEXT: [[TMP1:%.*]] = insertelement <2 x i64> poison, i64 [[X:%.*]], i64 1 102; LE-NEXT: [[INS1:%.*]] = bitcast <2 x i64> [[TMP1]] to <4 x i32> 103; LE-NEXT: ret <4 x i32> [[INS1]] 104; 105 %hi64 = lshr i64 %x, 32 106 %hi32 = trunc i64 %hi64 to i32 107 %lo32 = trunc i64 %x to i32 108 %ins0 = insertelement <4 x i32> poison, i32 %lo32, i64 2 109 %ins1 = insertelement <4 x i32> %ins0, i32 %hi32, i64 3 110 ret <4 x i32> %ins1 111} 112 113define <4 x i16> @insert_32_poison_v4i16(i32 %x) { 114; BE-LABEL: @insert_32_poison_v4i16( 115; BE-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i64 1 116; BE-NEXT: [[INS1:%.*]] = bitcast <2 x i32> [[TMP1]] to <4 x i16> 117; BE-NEXT: ret <4 x i16> [[INS1]] 118; 119; LE-LABEL: @insert_32_poison_v4i16( 120; LE-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 121; LE-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 122; LE-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 123; LE-NEXT: [[TMP1:%.*]] = insertelement <4 x i16> poison, i16 [[HI16]], i64 2 124; LE-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[LO16]], i64 3 125; LE-NEXT: ret <4 x i16> [[INS1]] 126; 127 %hi32 = lshr i32 %x, 16 128 %hi16 = trunc i32 %hi32 to i16 129 %lo16 = trunc i32 %x to i16 130 %ins0 = insertelement <4 x i16> poison, i16 %lo16, i64 3 131 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 2 132 ret <4 x i16> %ins1 133} 134 135; Similar to the above tests but with a non-poison base vector. 136 137; Vector is same size as scalar, so this is just a cast. 138; TODO: Could be swapped/rotated into place. 139 140define <2 x i16> @insert_01_v2i16(i32 %x, <2 x i16> %v) { 141; BE-LABEL: @insert_01_v2i16( 142; BE-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 143; BE-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 144; BE-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 145; BE-NEXT: [[INS0:%.*]] = insertelement <2 x i16> poison, i16 [[LO16]], i64 0 146; BE-NEXT: [[INS1:%.*]] = insertelement <2 x i16> [[INS0]], i16 [[HI16]], i64 1 147; BE-NEXT: ret <2 x i16> [[INS1]] 148; 149; LE-LABEL: @insert_01_v2i16( 150; LE-NEXT: [[INS1:%.*]] = bitcast i32 [[X:%.*]] to <2 x i16> 151; LE-NEXT: ret <2 x i16> [[INS1]] 152; 153 %hi32 = lshr i32 %x, 16 154 %hi16 = trunc i32 %hi32 to i16 155 %lo16 = trunc i32 %x to i16 156 %ins0 = insertelement <2 x i16> %v, i16 %lo16, i64 0 157 %ins1 = insertelement <2 x i16> %ins0, i16 %hi16, i64 1 158 ret <2 x i16> %ins1 159} 160 161; negative test - can't do this safely without knowing something about the base vector 162 163define <8 x i16> @insert_10_v8i16(i32 %x, <8 x i16> %v) { 164; ALL-LABEL: @insert_10_v8i16( 165; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 166; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 167; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 168; ALL-NEXT: [[TMP1:%.*]] = insertelement <8 x i16> [[V:%.*]], i16 [[HI16]], i64 0 169; ALL-NEXT: [[INS1:%.*]] = insertelement <8 x i16> [[TMP1]], i16 [[LO16]], i64 1 170; ALL-NEXT: ret <8 x i16> [[INS1]] 171; 172 %hi32 = lshr i32 %x, 16 173 %hi16 = trunc i32 %hi32 to i16 174 %lo16 = trunc i32 %x to i16 175 %ins0 = insertelement <8 x i16> %v, i16 %lo16, i64 1 176 %ins1 = insertelement <8 x i16> %ins0, i16 %hi16, i64 0 177 ret <8 x i16> %ins1 178} 179 180; negative test - larger element is not aligned in the vector 181 182define <4 x i32> @insert_12_v4i32(i64 %x, <4 x i32> %v) { 183; ALL-LABEL: @insert_12_v4i32( 184; ALL-NEXT: [[HI64:%.*]] = lshr i64 [[X:%.*]], 32 185; ALL-NEXT: [[HI32:%.*]] = trunc nuw i64 [[HI64]] to i32 186; ALL-NEXT: [[LO32:%.*]] = trunc i64 [[X]] to i32 187; ALL-NEXT: [[INS0:%.*]] = insertelement <4 x i32> [[V:%.*]], i32 [[LO32]], i64 1 188; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i32> [[INS0]], i32 [[HI32]], i64 2 189; ALL-NEXT: ret <4 x i32> [[INS1]] 190; 191 %hi64 = lshr i64 %x, 32 192 %hi32 = trunc i64 %hi64 to i32 193 %lo32 = trunc i64 %x to i32 194 %ins0 = insertelement <4 x i32> %v, i32 %lo32, i64 1 195 %ins1 = insertelement <4 x i32> %ins0, i32 %hi32, i64 2 196 ret <4 x i32> %ins1 197} 198 199; negative test - larger element is not aligned in the vector 200 201define <4 x i16> @insert_21_v4i16(i32 %x, <4 x i16> %v) { 202; ALL-LABEL: @insert_21_v4i16( 203; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 204; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 205; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 206; ALL-NEXT: [[TMP1:%.*]] = insertelement <4 x i16> [[V:%.*]], i16 [[HI16]], i64 1 207; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[LO16]], i64 2 208; ALL-NEXT: ret <4 x i16> [[INS1]] 209; 210 %hi32 = lshr i32 %x, 16 211 %hi16 = trunc i32 %hi32 to i16 212 %lo16 = trunc i32 %x to i16 213 %ins0 = insertelement <4 x i16> %v, i16 %lo16, i64 2 214 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 1 215 ret <4 x i16> %ins1 216} 217 218; negative test - can't do this safely without knowing something about the base vector 219 220define <4 x i32> @insert_23_v4i32(i64 %x, <4 x i32> %v) { 221; ALL-LABEL: @insert_23_v4i32( 222; ALL-NEXT: [[HI64:%.*]] = lshr i64 [[X:%.*]], 32 223; ALL-NEXT: [[HI32:%.*]] = trunc nuw i64 [[HI64]] to i32 224; ALL-NEXT: [[LO32:%.*]] = trunc i64 [[X]] to i32 225; ALL-NEXT: [[INS0:%.*]] = insertelement <4 x i32> [[V:%.*]], i32 [[LO32]], i64 2 226; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i32> [[INS0]], i32 [[HI32]], i64 3 227; ALL-NEXT: ret <4 x i32> [[INS1]] 228; 229 %hi64 = lshr i64 %x, 32 230 %hi32 = trunc i64 %hi64 to i32 231 %lo32 = trunc i64 %x to i32 232 %ins0 = insertelement <4 x i32> %v, i32 %lo32, i64 2 233 %ins1 = insertelement <4 x i32> %ins0, i32 %hi32, i64 3 234 ret <4 x i32> %ins1 235} 236 237; negative test - can't do this safely without knowing something about the base vector 238 239define <4 x i16> @insert_32_v4i16(i32 %x, <4 x i16> %v) { 240; ALL-LABEL: @insert_32_v4i16( 241; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 242; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 243; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 244; ALL-NEXT: [[TMP1:%.*]] = insertelement <4 x i16> [[V:%.*]], i16 [[HI16]], i64 2 245; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[LO16]], i64 3 246; ALL-NEXT: ret <4 x i16> [[INS1]] 247; 248 %hi32 = lshr i32 %x, 16 249 %hi16 = trunc i32 %hi32 to i16 250 %lo16 = trunc i32 %x to i16 251 %ins0 = insertelement <4 x i16> %v, i16 %lo16, i64 3 252 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 2 253 ret <4 x i16> %ins1 254} 255 256; negative test - need half-width shift 257 258define <4 x i16> @insert_01_v4i16_wrong_shift1(i32 %x) { 259; ALL-LABEL: @insert_01_v4i16_wrong_shift1( 260; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 8 261; ALL-NEXT: [[HI16:%.*]] = trunc i32 [[HI32]] to i16 262; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 263; ALL-NEXT: [[INS0:%.*]] = insertelement <4 x i16> poison, i16 [[LO16]], i64 0 264; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[INS0]], i16 [[HI16]], i64 1 265; ALL-NEXT: ret <4 x i16> [[INS1]] 266; 267 %hi32 = lshr i32 %x, 8 268 %hi16 = trunc i32 %hi32 to i16 269 %lo16 = trunc i32 %x to i16 270 %ins0 = insertelement <4 x i16> poison, i16 %lo16, i64 0 271 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 1 272 ret <4 x i16> %ins1 273} 274 275; negative test - need common scalar 276 277define <4 x i16> @insert_01_v4i16_wrong_op(i32 %x, i32 %y) { 278; ALL-LABEL: @insert_01_v4i16_wrong_op( 279; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 280; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 281; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[Y:%.*]] to i16 282; ALL-NEXT: [[INS0:%.*]] = insertelement <4 x i16> poison, i16 [[LO16]], i64 0 283; ALL-NEXT: [[INS1:%.*]] = insertelement <4 x i16> [[INS0]], i16 [[HI16]], i64 1 284; ALL-NEXT: ret <4 x i16> [[INS1]] 285; 286 %hi32 = lshr i32 %x, 16 287 %hi16 = trunc i32 %hi32 to i16 288 %lo16 = trunc i32 %y to i16 289 %ins0 = insertelement <4 x i16> poison, i16 %lo16, i64 0 290 %ins1 = insertelement <4 x i16> %ins0, i16 %hi16, i64 1 291 ret <4 x i16> %ins1 292} 293 294; TODO: extra use doesn't have to prevent the fold. 295 296define <8 x i16> @insert_67_v4i16_uses1(i32 %x, <8 x i16> %v) { 297; ALL-LABEL: @insert_67_v4i16_uses1( 298; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 299; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 300; ALL-NEXT: call void @use(i16 [[HI16]]) 301; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 302; ALL-NEXT: [[INS0:%.*]] = insertelement <8 x i16> [[V:%.*]], i16 [[LO16]], i64 6 303; ALL-NEXT: [[INS1:%.*]] = insertelement <8 x i16> [[INS0]], i16 [[HI16]], i64 7 304; ALL-NEXT: ret <8 x i16> [[INS1]] 305; 306 %hi32 = lshr i32 %x, 16 307 %hi16 = trunc i32 %hi32 to i16 308 call void @use(i16 %hi16) 309 %lo16 = trunc i32 %x to i16 310 %ins0 = insertelement <8 x i16> %v, i16 %lo16, i64 6 311 %ins1 = insertelement <8 x i16> %ins0, i16 %hi16, i64 7 312 ret <8 x i16> %ins1 313} 314 315; negative test - can't do this safely without knowing something about the base vector 316; extra use would be ok 317 318define <8 x i16> @insert_76_v4i16_uses2(i32 %x, <8 x i16> %v) { 319; ALL-LABEL: @insert_76_v4i16_uses2( 320; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 321; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 322; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 323; ALL-NEXT: call void @use(i16 [[LO16]]) 324; ALL-NEXT: [[TMP1:%.*]] = insertelement <8 x i16> [[V:%.*]], i16 [[HI16]], i64 6 325; ALL-NEXT: [[INS1:%.*]] = insertelement <8 x i16> [[TMP1]], i16 [[LO16]], i64 7 326; ALL-NEXT: ret <8 x i16> [[INS1]] 327; 328 %hi32 = lshr i32 %x, 16 329 %hi16 = trunc i32 %hi32 to i16 330 %lo16 = trunc i32 %x to i16 331 call void @use(i16 %lo16) 332 %ins0 = insertelement <8 x i16> %v, i16 %lo16, i64 7 333 %ins1 = insertelement <8 x i16> %ins0, i16 %hi16, i64 6 334 ret <8 x i16> %ins1 335} 336 337; TODO: extra use doesn't have to prevent the fold. 338 339define <8 x i16> @insert_67_v4i16_uses3(i32 %x, <8 x i16> %v) { 340; ALL-LABEL: @insert_67_v4i16_uses3( 341; ALL-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 342; ALL-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 343; ALL-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 344; ALL-NEXT: [[INS0:%.*]] = insertelement <8 x i16> [[V:%.*]], i16 [[LO16]], i64 6 345; ALL-NEXT: call void @use_vec(<8 x i16> [[INS0]]) 346; ALL-NEXT: [[INS1:%.*]] = insertelement <8 x i16> [[INS0]], i16 [[HI16]], i64 7 347; ALL-NEXT: ret <8 x i16> [[INS1]] 348; 349 %hi32 = lshr i32 %x, 16 350 %hi16 = trunc i32 %hi32 to i16 351 %lo16 = trunc i32 %x to i16 352 %ins0 = insertelement <8 x i16> %v, i16 %lo16, i64 6 353 call void @use_vec(<8 x i16> %ins0) 354 %ins1 = insertelement <8 x i16> %ins0, i16 %hi16, i64 7 355 ret <8 x i16> %ins1 356} 357 358; TODO: This is equivalent to the 1st test. 359 360define <4 x i16> @insert_01_poison_v4i16_high_first(i32 %x) { 361; BE-LABEL: @insert_01_poison_v4i16_high_first( 362; BE-NEXT: [[HI32:%.*]] = lshr i32 [[X:%.*]], 16 363; BE-NEXT: [[HI16:%.*]] = trunc nuw i32 [[HI32]] to i16 364; BE-NEXT: [[LO16:%.*]] = trunc i32 [[X]] to i16 365; BE-NEXT: [[TMP1:%.*]] = insertelement <4 x i16> poison, i16 [[LO16]], i64 0 366; BE-NEXT: [[INS0:%.*]] = insertelement <4 x i16> [[TMP1]], i16 [[HI16]], i64 1 367; BE-NEXT: ret <4 x i16> [[INS0]] 368; 369; LE-LABEL: @insert_01_poison_v4i16_high_first( 370; LE-NEXT: [[TMP1:%.*]] = insertelement <2 x i32> poison, i32 [[X:%.*]], i64 0 371; LE-NEXT: [[INS0:%.*]] = bitcast <2 x i32> [[TMP1]] to <4 x i16> 372; LE-NEXT: ret <4 x i16> [[INS0]] 373; 374 %hi32 = lshr i32 %x, 16 375 %hi16 = trunc i32 %hi32 to i16 376 %lo16 = trunc i32 %x to i16 377 %ins1 = insertelement <4 x i16> poison, i16 %hi16, i64 1 378 %ins0 = insertelement <4 x i16> %ins1, i16 %lo16, i64 0 379 ret <4 x i16> %ins0 380} 381