1 ; RUN: opt -loop-vectorize -force-vector-width=4 -enable-vplan-native-path -S %s | FileCheck %s
3 ; Test that VPlan native path is able to widen select instruction in the
4 ; innermost loop under different conditions when outer loop is marked to be
5 ; vectorized. These conditions include following:
6 ; * Inner and outer loop invariant select condition
7 ; * Select condition depending on outer loop iteration variable.
8 ; * Select condidition depending on inner loop iteration variable.
9 ; * Select conditition depending on both outer and inner loop iteration
12 define void @loop_invariant_select(double* noalias nocapture %out, i1 %select, double %a, double %b) {
13 ; CHECK-LABEL: @loop_invariant_select(
15 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
17 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[A:%.*]], i32 0
18 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT]], <4 x double> poison, <4 x i32> zeroinitializer
19 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <4 x double> poison, double [[B:%.*]], i32 0
20 ; CHECK-NEXT: [[BROADCAST_SPLAT3:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT2]], <4 x double> poison, <4 x i32> zeroinitializer
21 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
23 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[FOR1_LATCH4:%.*]] ]
24 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[FOR1_LATCH4]] ]
25 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds double, double* [[OUT:%.*]], <4 x i64> [[VEC_IND]]
26 ; CHECK-NEXT: br label [[FOR2_HEADER1:%.*]]
27 ; CHECK: for2.header1:
28 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i64> [ zeroinitializer, [[VECTOR_BODY]] ], [ [[TMP2:%.*]], [[FOR2_HEADER1]] ]
29 ; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[SELECT:%.*]], <4 x double> [[BROADCAST_SPLAT]], <4 x double> [[BROADCAST_SPLAT3]]
30 ; CHECK-NEXT: call void @llvm.masked.scatter.v4f64.v4p0f64(<4 x double> [[TMP1]], <4 x double*> [[TMP0]], i32 8, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
35 %indvar1 = phi i64 [ 0, %entry ], [ %indvar11, %for1.latch ]
36 %ptr = getelementptr inbounds double, double* %out, i64 %indvar1
40 %indvar2 = phi i64 [ 0, %for1.header ], [ %indvar21, %for2.header ]
41 ; Select condition is loop invariant for both inner and outer loop.
42 %select.b = select i1 %select, double %a, double %b
43 store double %select.b, double* %ptr, align 8
44 %indvar21 = add nuw nsw i64 %indvar2, 1
45 %for2.cond = icmp eq i64 %indvar21, 10000
46 br i1 %for2.cond, label %for1.latch, label %for2.header
49 %indvar11 = add nuw nsw i64 %indvar1, 1
50 %for1.cond = icmp eq i64 %indvar11, 1000
51 br i1 %for1.cond, label %exit, label %for1.header, !llvm.loop !0
57 define void @outer_loop_dependant_select(double* noalias nocapture %out, double %a, double %b) {
58 ; CHECK-LABEL: @outer_loop_dependant_select(
60 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
62 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[A:%.*]], i32 0
63 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT]], <4 x double> poison, <4 x i32> zeroinitializer
64 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <4 x double> poison, double [[B:%.*]], i32 0
65 ; CHECK-NEXT: [[BROADCAST_SPLAT3:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT2]], <4 x double> poison, <4 x i32> zeroinitializer
66 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
68 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[FOR1_LATCH4:%.*]] ]
69 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[FOR1_LATCH4]] ]
70 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds double, double* [[OUT:%.*]], <4 x i64> [[VEC_IND]]
71 ; CHECK-NEXT: br label [[FOR2_HEADER1:%.*]]
72 ; CHECK: for2.header1:
73 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i64> [ zeroinitializer, [[VECTOR_BODY]] ], [ [[TMP3:%.*]], [[FOR2_HEADER1]] ]
74 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i64> [[VEC_IND]] to <4 x i1>
75 ; CHECK-NEXT: [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x double> [[BROADCAST_SPLAT]], <4 x double> [[BROADCAST_SPLAT3]]
76 ; CHECK-NEXT: call void @llvm.masked.scatter.v4f64.v4p0f64(<4 x double> [[TMP2]], <4 x double*> [[TMP0]], i32 8, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
81 %indvar1 = phi i64 [ 0, %entry ], [ %indvar11, %for1.latch ]
82 %ptr = getelementptr inbounds double, double* %out, i64 %indvar1
86 %indvar2 = phi i64 [ 0, %for1.header ], [ %indvar21, %for2.header ]
87 %select = trunc i64 %indvar1 to i1
88 ; Select condition only depends on outer loop iteration variable.
89 %select.b = select i1 %select, double %a, double %b
90 store double %select.b, double* %ptr, align 8
91 %indvar21 = add nuw nsw i64 %indvar2, 1
92 %for2.cond = icmp eq i64 %indvar21, 10000
93 br i1 %for2.cond, label %for1.latch, label %for2.header
96 %indvar11 = add nuw nsw i64 %indvar1, 1
97 %for1.cond = icmp eq i64 %indvar11, 1000
98 br i1 %for1.cond, label %exit, label %for1.header, !llvm.loop !0
104 define void @inner_loop_dependant_select(double* noalias nocapture %out, double %a, double %b) {
105 ; CHECK-LABEL: @inner_loop_dependant_select(
107 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
109 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[A:%.*]], i32 0
110 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT]], <4 x double> poison, <4 x i32> zeroinitializer
111 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <4 x double> poison, double [[B:%.*]], i32 0
112 ; CHECK-NEXT: [[BROADCAST_SPLAT3:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT2]], <4 x double> poison, <4 x i32> zeroinitializer
113 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
114 ; CHECK: vector.body:
115 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[FOR1_LATCH4:%.*]] ]
116 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[FOR1_LATCH4]] ]
117 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds double, double* [[OUT:%.*]], <4 x i64> [[VEC_IND]]
118 ; CHECK-NEXT: br label [[FOR2_HEADER1:%.*]]
119 ; CHECK: for2.header1:
120 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i64> [ zeroinitializer, [[VECTOR_BODY]] ], [ [[TMP3:%.*]], [[FOR2_HEADER1]] ]
121 ; CHECK-NEXT: [[TMP1:%.*]] = trunc <4 x i64> [[VEC_PHI]] to <4 x i1>
122 ; CHECK-NEXT: [[TMP2:%.*]] = select <4 x i1> [[TMP1]], <4 x double> [[BROADCAST_SPLAT]], <4 x double> [[BROADCAST_SPLAT3]]
123 ; CHECK-NEXT: call void @llvm.masked.scatter.v4f64.v4p0f64(<4 x double> [[TMP2]], <4 x double*> [[TMP0]], i32 8, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
125 br label %for1.header
128 %indvar1 = phi i64 [ 0, %entry ], [ %indvar11, %for1.latch ]
129 %ptr = getelementptr inbounds double, double* %out, i64 %indvar1
130 br label %for2.header
133 %indvar2 = phi i64 [ 0, %for1.header ], [ %indvar21, %for2.header ]
134 %select = trunc i64 %indvar2 to i1
135 ; Select condition only depends on inner loop iteration variable.
136 %select.b = select i1 %select, double %a, double %b
137 store double %select.b, double* %ptr, align 8
138 %indvar21 = add nuw nsw i64 %indvar2, 1
139 %for2.cond = icmp eq i64 %indvar21, 10000
140 br i1 %for2.cond, label %for1.latch, label %for2.header
143 %indvar11 = add nuw nsw i64 %indvar1, 1
144 %for1.cond = icmp eq i64 %indvar11, 1000
145 br i1 %for1.cond, label %exit, label %for1.header, !llvm.loop !0
151 define void @outer_and_inner_loop_dependant_select(double* noalias nocapture %out, double %a, double %b) {
152 ; CHECK-LABEL: @outer_and_inner_loop_dependant_select(
154 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
156 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x double> poison, double [[A:%.*]], i32 0
157 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT]], <4 x double> poison, <4 x i32> zeroinitializer
158 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <4 x double> poison, double [[B:%.*]], i32 0
159 ; CHECK-NEXT: [[BROADCAST_SPLAT3:%.*]] = shufflevector <4 x double> [[BROADCAST_SPLATINSERT2]], <4 x double> poison, <4 x i32> zeroinitializer
160 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
161 ; CHECK: vector.body:
162 ; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[FOR1_LATCH4:%.*]] ]
163 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i64> [ <i64 0, i64 1, i64 2, i64 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[FOR1_LATCH4]] ]
164 ; CHECK-NEXT: [[TMP0:%.*]] = getelementptr inbounds double, double* [[OUT:%.*]], <4 x i64> [[VEC_IND]]
165 ; CHECK-NEXT: br label [[FOR2_HEADER1:%.*]]
166 ; CHECK: for2.header1:
167 ; CHECK-NEXT: [[VEC_PHI:%.*]] = phi <4 x i64> [ zeroinitializer, [[VECTOR_BODY]] ], [ [[TMP4:%.*]], [[FOR2_HEADER1]] ]
168 ; CHECK-NEXT: [[TMP1:%.*]] = add nuw nsw <4 x i64> [[VEC_IND]], [[VEC_PHI]]
169 ; CHECK-NEXT: [[TMP2:%.*]] = trunc <4 x i64> [[TMP1]] to <4 x i1>
170 ; CHECK-NEXT: [[TMP3:%.*]] = select <4 x i1> [[TMP2]], <4 x double> [[BROADCAST_SPLAT]], <4 x double> [[BROADCAST_SPLAT3]]
171 ; CHECK-NEXT: call void @llvm.masked.scatter.v4f64.v4p0f64(<4 x double> [[TMP3]], <4 x double*> [[TMP0]], i32 8, <4 x i1> <i1 true, i1 true, i1 true, i1 true>)
173 br label %for1.header
176 %indvar1 = phi i64 [ 0, %entry ], [ %indvar11, %for1.latch ]
177 %ptr = getelementptr inbounds double, double* %out, i64 %indvar1
178 br label %for2.header
181 %indvar2 = phi i64 [ 0, %for1.header ], [ %indvar21, %for2.header ]
182 %sum = add nuw nsw i64 %indvar1, %indvar2
183 %select = trunc i64 %sum to i1
184 ; Select condition depends on both inner and outer loop iteration variables.
185 %select.b = select i1 %select, double %a, double %b
186 store double %select.b, double* %ptr, align 8
187 %indvar21 = add nuw nsw i64 %indvar2, 1
188 %for2.cond = icmp eq i64 %indvar21, 10000
189 br i1 %for2.cond, label %for1.latch, label %for2.header
192 %indvar11 = add nuw nsw i64 %indvar1, 1
193 %for1.cond = icmp eq i64 %indvar11, 1000
194 br i1 %for1.cond, label %exit, label %for1.header, !llvm.loop !0
199 !0 = distinct !{!0, !1}
200 !1 = !{!"llvm.loop.vectorize.enable", i1 true}