xref: /llvm-project/llvm/test/Transforms/InstCombine/vscale_insertelement.ll (revision 9d25b28b9e412b66bb58993dff6646a8f08b8c8c)
1; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2; RUN: opt -S -passes=instcombine < %s | FileCheck %s
3
4; This test checks that bitcast is moved after insertelement when both vector and scalar are
5; bitcast from the same element type.
6; inselt (bitcast VecSrc), (bitcast ScalarSrc), IdxOp
7;  --> bitcast (inselt VecSrc, ScalarSrc, IdxOp)
8define <vscale x 4 x float> @insertelement_bitcast(<vscale x 4 x i32> %vec, i32 %x) {
9; CHECK-LABEL: @insertelement_bitcast(
10; CHECK-NEXT:    [[TMP1:%.*]] = insertelement <vscale x 4 x i32> [[VEC:%.*]], i32 [[X:%.*]], i64 0
11; CHECK-NEXT:    [[R:%.*]] = bitcast <vscale x 4 x i32> [[TMP1]] to <vscale x 4 x float>
12; CHECK-NEXT:    ret <vscale x 4 x float> [[R]]
13;
14  %x_cast = bitcast i32 %x to float
15  %vec_cast = bitcast <vscale x 4 x i32> %vec to <vscale x 4 x float>
16  %r = insertelement <vscale x 4 x float> %vec_cast, float %x_cast, i32 0
17  ret <vscale x 4 x float> %r
18}
19
20; This test checks that code-path "Try to form a shuffle from a chain of extract-insert ops" is
21; not taken when both extract and insert are scalable type.
22; For scalable type, the vector length needed to create shuffle mask is not a compile-time constant.
23; Meanwhile, for scalable type shuffle mask only support splat and undef in the current code base.
24; Otherwise we crash at:
25; "Assertion `isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"' failed."
26define <vscale x 4 x i32> @insertelement_extractelement(<vscale x 4 x i32> %a, <vscale x 4 x i32> %b) {
27; CHECK-LABEL: @insertelement_extractelement(
28; CHECK-NEXT:    [[T0:%.*]] = extractelement <vscale x 4 x i32> [[A:%.*]], i64 1
29; CHECK-NEXT:    [[T1:%.*]] = insertelement <vscale x 4 x i32> [[B:%.*]], i32 [[T0]], i64 0
30; CHECK-NEXT:    ret <vscale x 4 x i32> [[T1]]
31;
32  %t0 = extractelement <vscale x 4 x i32> %a, i32 1
33  %t1 = insertelement <vscale x 4 x i32> %b, i32 %t0, i32 0
34  ret <vscale x 4 x i32> %t1
35}
36
37; This test checks that we are not attempting to create a shuffle from extract/insert chain,
38; when extract is from a scalable type, and the insert vector is fixed-length.
39define <4 x i32> @insertelement_extractelement_fixed_vec_extract_from_scalable(<vscale x 4 x i32> %a, <4 x i32> %b) {
40; CHECK-LABEL: @insertelement_extractelement_fixed_vec_extract_from_scalable(
41; CHECK-NEXT:    [[T0:%.*]] = extractelement <vscale x 4 x i32> [[A:%.*]], i64 1
42; CHECK-NEXT:    [[T1:%.*]] = insertelement <4 x i32> [[B:%.*]], i32 [[T0]], i64 0
43; CHECK-NEXT:    ret <4 x i32> [[T1]]
44;
45  %t0 = extractelement <vscale x 4 x i32> %a, i32 1
46  %t1 = insertelement <4 x i32> %b, i32 %t0, i32 0
47  ret <4 x i32> %t1
48}
49
50; This test checks that the optimization "foldConstantInsEltInfoShuffle" is not taken for scalable type.
51; Particularly the fold:
52; insertelt (insertelt X, C1, CIndex1), C, CIndex
53;  --> shufflevector X, CVec', Mask'
54; For scalable type, the vector length needed to create shuffle mask is not a compile-time constant.
55; Meanwhile, for scalable type shuffle mask only support splat and undef in the current code base.
56; Otherwise we crash at:
57; "Assertion `isValidOperands(V1, V2, Mask) && "Invalid shuffle vector instruction operands!"' failed."
58define <vscale x 4 x i32> @insertelement_insertelement(<vscale x 4 x i32> %vec) {
59; CHECK-LABEL: @insertelement_insertelement(
60; CHECK-NEXT:    [[T0:%.*]] = insertelement <vscale x 4 x i32> [[VEC:%.*]], i32 1, i64 1
61; CHECK-NEXT:    [[T1:%.*]] = insertelement <vscale x 4 x i32> [[T0]], i32 2, i64 2
62; CHECK-NEXT:    ret <vscale x 4 x i32> [[T1]]
63;
64  %t0 = insertelement <vscale x 4 x i32> %vec, i32 1, i32 1
65  %t1 = insertelement <vscale x 4 x i32> %t0, i32 2, i32 2
66  ret <vscale x 4 x i32> %t1
67}
68
69; This test checks that the following insertelement sequence is not folded into shuffle splat.
70; The length of scalable vector is unknown at compile-time. Therefore the following insertelements
71; may not form a valid splat.
72define <vscale x 4 x float> @insertelement_sequene_may_not_be_splat(float %x) {
73; CHECK-LABEL: @insertelement_sequene_may_not_be_splat(
74; CHECK-NEXT:    [[T0:%.*]] = insertelement <vscale x 4 x float> undef, float [[X:%.*]], i64 0
75; CHECK-NEXT:    [[T1:%.*]] = insertelement <vscale x 4 x float> [[T0]], float [[X]], i64 1
76; CHECK-NEXT:    [[T2:%.*]] = insertelement <vscale x 4 x float> [[T1]], float [[X]], i64 2
77; CHECK-NEXT:    [[T3:%.*]] = insertelement <vscale x 4 x float> [[T2]], float [[X]], i64 3
78; CHECK-NEXT:    ret <vscale x 4 x float> [[T3]]
79;
80  %t0 = insertelement <vscale x 4 x float> undef, float %x, i32 0
81  %t1 = insertelement <vscale x 4 x float> %t0, float %x, i32 1
82  %t2 = insertelement <vscale x 4 x float> %t1, float %x, i32 2
83  %t3 = insertelement <vscale x 4 x float> %t2, float %x, i32 3
84  ret <vscale x 4 x float> %t3
85}
86
87; OSS-Fuzz #27416
88; https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=27416
89define void @ossfuzz_27416(i32 %v, ptr %p) {
90; CHECK-LABEL: @ossfuzz_27416(
91; CHECK-NEXT:    [[IN:%.*]] = insertelement <vscale x 4 x i32> undef, i32 [[V:%.*]], i64 0
92; CHECK-NEXT:    [[SPLAT:%.*]] = shufflevector <vscale x 4 x i32> [[IN]], <vscale x 4 x i32> poison, <vscale x 4 x i32> zeroinitializer
93; CHECK-NEXT:    [[I1:%.*]] = insertelement <vscale x 4 x i32> [[SPLAT]], i32 undef, i64 128
94; CHECK-NEXT:    store <vscale x 4 x i32> [[I1]], ptr [[P:%.*]], align 16
95; CHECK-NEXT:    ret void
96;
97  %in = insertelement <vscale x 4 x i32> undef, i32 %v, i32 0
98  %splat = shufflevector <vscale x 4 x i32> %in, <vscale x 4 x i32> undef, <vscale x 4 x i32> zeroinitializer
99  %I1 = insertelement <vscale x 4 x i32> %splat, i32 undef, i8 -128
100  store <vscale x 4 x i32> %I1, ptr %p, align 16
101  ret void
102}
103