1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py 2; RUN: opt < %s -instcombine -S | FileCheck %s 3 4; insertelements should fold to shuffle 5define <4 x float> @foo(<4 x float> %x) { 6; CHECK-LABEL: @foo( 7; CHECK-NEXT: [[INS2:%.*]] = shufflevector <4 x float> %x, <4 x float> <float undef, float 1.000000e+00, float 2.000000e+00, float undef>, <4 x i32> <i32 0, i32 5, i32 6, i32 3> 8; CHECK-NEXT: ret <4 x float> [[INS2]] 9; 10 %ins1 = insertelement<4 x float> %x, float 1.0, i32 1 11 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2 12 ret <4 x float> %ins2 13} 14 15; Insert of a constant is canonicalized ahead of insert of a variable. 16 17define <4 x float> @bar(<4 x float> %x, float %a) { 18; CHECK-LABEL: @bar( 19; CHECK-NEXT: [[TMP1:%.*]] = insertelement <4 x float> %x, float 2.000000e+00, i32 2 20; CHECK-NEXT: [[INS2:%.*]] = insertelement <4 x float> [[TMP1]], float %a, i32 1 21; CHECK-NEXT: ret <4 x float> [[INS2]] 22; 23 %ins1 = insertelement<4 x float> %x, float %a, i32 1 24 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2 25 ret <4 x float> %ins2 26} 27 28define <4 x float> @baz(<4 x float> %x, i32 %a) { 29; CHECK-LABEL: @baz( 30; CHECK-NEXT: [[INS1:%.*]] = insertelement <4 x float> %x, float 1.000000e+00, i32 1 31; CHECK-NEXT: [[INS2:%.*]] = insertelement <4 x float> [[INS1]], float 2.000000e+00, i32 %a 32; CHECK-NEXT: ret <4 x float> [[INS2]] 33; 34 %ins1 = insertelement<4 x float> %x, float 1.0, i32 1 35 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 %a 36 ret <4 x float> %ins2 37} 38 39; insertelements should fold to shuffle 40define <4 x float> @bazz(<4 x float> %x, i32 %a) { 41; CHECK-LABEL: @bazz( 42; CHECK-NEXT: [[INS1:%.*]] = insertelement <4 x float> %x, float 1.000000e+00, i32 3 43; CHECK-NEXT: [[INS2:%.*]] = insertelement <4 x float> [[INS1]], float 5.000000e+00, i32 %a 44; CHECK-NEXT: [[INS5:%.*]] = shufflevector <4 x float> [[INS2]], <4 x float> <float undef, float 1.000000e+00, float 2.000000e+00, float undef>, <4 x i32> <i32 0, i32 5, i32 6, i32 3> 45; CHECK-NEXT: [[INS6:%.*]] = insertelement <4 x float> [[INS5]], float 7.000000e+00, i32 %a 46; CHECK-NEXT: ret <4 x float> [[INS6]] 47; 48 %ins1 = insertelement<4 x float> %x, float 1.0, i32 3 49 %ins2 = insertelement<4 x float> %ins1, float 5.0, i32 %a 50 %ins3 = insertelement<4 x float> %ins2, float 3.0, i32 2 51 %ins4 = insertelement<4 x float> %ins3, float 1.0, i32 1 52 %ins5 = insertelement<4 x float> %ins4, float 2.0, i32 2 53 %ins6 = insertelement<4 x float> %ins5, float 7.0, i32 %a 54 ret <4 x float> %ins6 55} 56 57; Out of bounds index folds to undef 58define <4 x float> @bazzz(<4 x float> %x) { 59; CHECK-LABEL: @bazzz( 60; CHECK-NEXT: ret <4 x float> <float undef, float undef, float 2.000000e+00, float undef> 61; 62 %ins1 = insertelement<4 x float> %x, float 1.0, i32 5 63 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2 64 ret <4 x float> %ins2 65} 66 67define <4 x float> @bazzzz(<4 x float> %x) { 68; CHECK-LABEL: @bazzzz( 69; CHECK-NEXT: ret <4 x float> <float undef, float undef, float 2.000000e+00, float undef> 70; 71 %ins1 = insertelement<4 x float> %x, float 1.0, i32 undef 72 %ins2 = insertelement<4 x float> %ins1, float 2.0, i32 2 73 ret <4 x float> %ins2 74} 75 76define <4 x float> @bazzzzz() { 77; CHECK-LABEL: @bazzzzz( 78; CHECK-NEXT: ret <4 x float> <float 1.000000e+00, float 5.000000e+00, float 1.000000e+01, float 4.000000e+00> 79; 80 %ins1 = insertelement <4 x float> insertelement (<4 x float> <float 1.0, float 2.0, float 3.0, float undef>, float 4.0, i32 3), float 5.0, i32 1 81 %ins2 = insertelement<4 x float> %ins1, float 10.0, i32 2 82 ret <4 x float> %ins2 83} 84 85define <4 x float> @bazzzzzz(<4 x float> %x, i32 %a) { 86; CHECK-LABEL: @bazzzzzz( 87; CHECK-NEXT: ret <4 x float> <float undef, float 5.000000e+00, float undef, float 4.000000e+00> 88; 89 %ins1 = insertelement <4 x float> insertelement (<4 x float> shufflevector (<4 x float> undef, <4 x float> <float 1.0, float 2.0, float 3.0, float 4.0> , <4 x i32> <i32 0, i32 5, i32 undef, i32 6> ), float 4.0, i32 3), float 5.0, i32 1 90 ret <4 x float> %ins1 91} 92 93 94