1eb8cbb3aSNuno Lopes; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2eb8cbb3aSNuno Lopes; RUN: opt -S -passes=instcombine %s | FileCheck %s 3eb8cbb3aSNuno Lopes 4eb8cbb3aSNuno Lopes; Eliminate the insertelement. 5eb8cbb3aSNuno Lopes 6eb8cbb3aSNuno Lopesdefine <4 x float> @PR29126(<4 x float> %x) { 7eb8cbb3aSNuno Lopes; CHECK-LABEL: @PR29126( 8eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> <float poison, float 1.000000e+00, float 2.000000e+00, float 4.200000e+01>, <4 x i32> <i32 0, i32 5, i32 6, i32 7> 9eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x float> [[INS]] 10eb8cbb3aSNuno Lopes; 11eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x float> %x, <4 x float> <float poison, float 1.0, float 2.0, float poison>, <4 x i32> <i32 0, i32 5, i32 6, i32 3> 12eb8cbb3aSNuno Lopes %ins = insertelement <4 x float> %shuf, float 42.0, i32 3 13eb8cbb3aSNuno Lopes ret <4 x float> %ins 14eb8cbb3aSNuno Lopes} 15eb8cbb3aSNuno Lopes 16eb8cbb3aSNuno Lopes; A chain of inserts should collapse. 17eb8cbb3aSNuno Lopes 18eb8cbb3aSNuno Lopesdefine <4 x float> @twoInserts(<4 x float> %x) { 19eb8cbb3aSNuno Lopes; CHECK-LABEL: @twoInserts( 20eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS2:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> <float poison, float 0.000000e+00, float 4.200000e+01, float 1.100000e+01>, <4 x i32> <i32 0, i32 5, i32 6, i32 7> 21eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x float> [[INS2]] 22eb8cbb3aSNuno Lopes; 23eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x float> %x, <4 x float> zeroinitializer, <4 x i32> <i32 0, i32 5, i32 6, i32 3> 24eb8cbb3aSNuno Lopes %ins1 = insertelement <4 x float> %shuf, float 42.0, i32 2 25eb8cbb3aSNuno Lopes %ins2 = insertelement <4 x float> %ins1, float 11.0, i32 3 26eb8cbb3aSNuno Lopes ret <4 x float> %ins2 27eb8cbb3aSNuno Lopes} 28eb8cbb3aSNuno Lopes 29eb8cbb3aSNuno Lopesdefine <4 x i32> @shuffleRetain(<4 x i32> %base) { 30eb8cbb3aSNuno Lopes; CHECK-LABEL: @shuffleRetain( 31*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x i32> [[BASE:%.*]], <4 x i32> <i32 poison, i32 poison, i32 poison, i32 1>, <4 x i32> <i32 1, i32 2, i32 poison, i32 7> 32eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x i32> [[SHUF]] 33eb8cbb3aSNuno Lopes; 34eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x i32> %base, <4 x i32> <i32 4, i32 3, i32 2, i32 1>, <4 x i32> <i32 1, i32 2, i32 poison, i32 7> 35eb8cbb3aSNuno Lopes ret <4 x i32> %shuf 36eb8cbb3aSNuno Lopes} 37eb8cbb3aSNuno Lopes 38eb8cbb3aSNuno Lopes; TODO: Transform an arbitrary shuffle with constant into a shuffle that is equivalant to a vector select. 39eb8cbb3aSNuno Lopes 40eb8cbb3aSNuno Lopesdefine <4 x float> @disguisedSelect(<4 x float> %x) { 41eb8cbb3aSNuno Lopes; CHECK-LABEL: @disguisedSelect( 42*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> <float poison, float 1.000000e+00, float 2.000000e+00, float poison>, <4 x i32> <i32 poison, i32 6, i32 5, i32 3> 43eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <4 x float> [[SHUF]], float 4.000000e+00, i64 0 44eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x float> [[INS]] 45eb8cbb3aSNuno Lopes; 46eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x float> %x, <4 x float> <float poison, float 1.0, float 2.0, float 3.0>, <4 x i32> <i32 7, i32 6, i32 5, i32 3> 47eb8cbb3aSNuno Lopes %ins = insertelement <4 x float> %shuf, float 4.0, i32 0 48eb8cbb3aSNuno Lopes ret <4 x float> %ins 49eb8cbb3aSNuno Lopes} 50eb8cbb3aSNuno Lopes 51eb8cbb3aSNuno Lopes; TODO: Fold arbitrary (non-select-equivalent) shuffles if the new shuffle would have the same shuffle mask. 52eb8cbb3aSNuno Lopes 53eb8cbb3aSNuno Lopesdefine <4 x float> @notSelectButNoMaskDifference(<4 x float> %x) { 54eb8cbb3aSNuno Lopes; CHECK-LABEL: @notSelectButNoMaskDifference( 55*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> <float poison, float 1.000000e+00, float 2.000000e+00, float poison>, <4 x i32> <i32 1, i32 5, i32 6, i32 poison> 56eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <4 x float> [[SHUF]], float 4.000000e+00, i64 3 57eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x float> [[INS]] 58eb8cbb3aSNuno Lopes; 59eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x float> %x, <4 x float> <float poison, float 1.0, float 2.0, float 3.0>, <4 x i32> <i32 1, i32 5, i32 6, i32 3> 60eb8cbb3aSNuno Lopes %ins = insertelement <4 x float> %shuf, float 4.0, i32 3 61eb8cbb3aSNuno Lopes ret <4 x float> %ins 62eb8cbb3aSNuno Lopes} 63eb8cbb3aSNuno Lopes 64eb8cbb3aSNuno Lopes; We purposely do not touch arbitrary (non-select-equivalent) shuffles because folding the insert may create a more expensive shuffle. 65eb8cbb3aSNuno Lopes 66eb8cbb3aSNuno Lopesdefine <4 x float> @tooRisky(<4 x float> %x) { 67eb8cbb3aSNuno Lopes; CHECK-LABEL: @tooRisky( 68*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <4 x float> [[X:%.*]], <4 x float> <float 1.000000e+00, float poison, float poison, float poison>, <4 x i32> <i32 1, i32 4, i32 4, i32 poison> 69eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <4 x float> [[SHUF]], float 4.000000e+00, i64 3 70eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <4 x float> [[INS]] 71eb8cbb3aSNuno Lopes; 72eb8cbb3aSNuno Lopes %shuf = shufflevector <4 x float> %x, <4 x float> <float 1.0, float poison, float poison, float poison>, <4 x i32> <i32 1, i32 4, i32 4, i32 4> 73eb8cbb3aSNuno Lopes %ins = insertelement <4 x float> %shuf, float 4.0, i32 3 74eb8cbb3aSNuno Lopes ret <4 x float> %ins 75eb8cbb3aSNuno Lopes} 76eb8cbb3aSNuno Lopes 77eb8cbb3aSNuno Lopes; Don't transform insert to shuffle if the original shuffle is not removed. 78eb8cbb3aSNuno Lopes; TODO: Ease the one-use restriction if the insert scalar would simplify the shuffle to a full vector constant? 79eb8cbb3aSNuno Lopes 80eb8cbb3aSNuno Lopesdefine <3 x float> @twoShufUses(<3 x float> %x) { 81eb8cbb3aSNuno Lopes; CHECK-LABEL: @twoShufUses( 82eb8cbb3aSNuno Lopes; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <3 x float> [[X:%.*]], <3 x float> <float poison, float 1.000000e+00, float 2.000000e+00>, <3 x i32> <i32 0, i32 4, i32 5> 83eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <3 x float> [[SHUF]], float 4.200000e+01, i64 1 84eb8cbb3aSNuno Lopes; CHECK-NEXT: [[ADD:%.*]] = fadd <3 x float> [[SHUF]], [[INS]] 85eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <3 x float> [[ADD]] 86eb8cbb3aSNuno Lopes; 87eb8cbb3aSNuno Lopes %shuf = shufflevector <3 x float> %x, <3 x float> <float poison, float 1.0, float 2.0>, <3 x i32> <i32 0, i32 4, i32 5> 88eb8cbb3aSNuno Lopes %ins = insertelement <3 x float> %shuf, float 42.0, i2 1 89eb8cbb3aSNuno Lopes %add = fadd <3 x float> %shuf, %ins 90eb8cbb3aSNuno Lopes ret <3 x float> %add 91eb8cbb3aSNuno Lopes} 92eb8cbb3aSNuno Lopes 93eb8cbb3aSNuno Lopes; The inserted scalar constant index is out-of-bounds for the shuffle vector constant. 94eb8cbb3aSNuno Lopes 95eb8cbb3aSNuno Lopesdefine <5 x i8> @longerMask(<3 x i8> %x) { 96eb8cbb3aSNuno Lopes; CHECK-LABEL: @longerMask( 97*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <3 x i8> [[X:%.*]], <3 x i8> <i8 poison, i8 1, i8 poison>, <5 x i32> <i32 2, i32 1, i32 4, i32 poison, i32 poison> 98eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <5 x i8> [[SHUF]], i8 42, i64 4 99eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <5 x i8> [[INS]] 100eb8cbb3aSNuno Lopes; 101eb8cbb3aSNuno Lopes %shuf = shufflevector <3 x i8> %x, <3 x i8> <i8 poison, i8 1, i8 2>, <5 x i32> <i32 2, i32 1, i32 4, i32 3, i32 0> 102eb8cbb3aSNuno Lopes %ins = insertelement <5 x i8> %shuf, i8 42, i17 4 103eb8cbb3aSNuno Lopes ret <5 x i8> %ins 104eb8cbb3aSNuno Lopes} 105eb8cbb3aSNuno Lopes 106eb8cbb3aSNuno Lopes; TODO: The inserted constant could get folded into the shuffle vector constant. 107eb8cbb3aSNuno Lopes 108eb8cbb3aSNuno Lopesdefine <3 x i8> @shorterMask(<5 x i8> %x) { 109eb8cbb3aSNuno Lopes; CHECK-LABEL: @shorterMask( 110*8b56da5eSManuelJBrito; CHECK-NEXT: [[SHUF:%.*]] = shufflevector <5 x i8> [[X:%.*]], <5 x i8> poison, <3 x i32> <i32 poison, i32 1, i32 4> 111eb8cbb3aSNuno Lopes; CHECK-NEXT: [[INS:%.*]] = insertelement <3 x i8> [[SHUF]], i8 42, i64 0 112eb8cbb3aSNuno Lopes; CHECK-NEXT: ret <3 x i8> [[INS]] 113eb8cbb3aSNuno Lopes; 114eb8cbb3aSNuno Lopes %shuf = shufflevector <5 x i8> %x, <5 x i8> <i8 poison, i8 1, i8 2, i8 3, i8 4>, <3 x i32> <i32 2, i32 1, i32 4> 115eb8cbb3aSNuno Lopes %ins = insertelement <3 x i8> %shuf, i8 42, i21 0 116eb8cbb3aSNuno Lopes ret <3 x i8> %ins 117eb8cbb3aSNuno Lopes} 118eb8cbb3aSNuno Lopes 119