1 ; This test verifies that the loop vectorizer will NOT produce a tail
2 ; loop with the optimize for size or the minimize size attributes.
4 ; RUN: opt < %s -passes='require<profile-summary>,loop-vectorize' -S | FileCheck %s
5 ; RUN: opt < %s -passes='require<profile-summary>,loop-vectorize' -pgso -S | FileCheck %s -check-prefix=PGSO
6 ; RUN: opt < %s -passes='require<profile-summary>,loop-vectorize' -pgso=false -S | FileCheck %s -check-prefix=NPGSO
8 target datalayout = "E-m:e-p:32:32-i64:32-f64:32:64-a:0:32-n32-S128"
10 @tab = common global [32 x i8] zeroinitializer, align 1
12 define i32 @foo_optsize() #0 {
13 ; CHECK-LABEL: @foo_optsize(
20 for.body: ; preds = %for.body, %entry
21 %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
22 %arrayidx = getelementptr inbounds [32 x i8], ptr @tab, i32 0, i32 %i.08
23 %0 = load i8, ptr %arrayidx, align 1
24 %cmp1 = icmp eq i8 %0, 0
25 %. = select i1 %cmp1, i8 2, i8 1
26 store i8 %., ptr %arrayidx, align 1
27 %inc = add nsw i32 %i.08, 1
28 %exitcond = icmp eq i32 %i.08, 202
29 br i1 %exitcond, label %for.end, label %for.body
31 for.end: ; preds = %for.body
35 attributes #0 = { optsize }
37 define i32 @foo_minsize() #1 {
38 ; CHECK-LABEL: @foo_minsize(
41 ; CHECK-LABEL: @foo_pgso(
46 for.body: ; preds = %for.body, %entry
47 %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
48 %arrayidx = getelementptr inbounds [32 x i8], ptr @tab, i32 0, i32 %i.08
49 %0 = load i8, ptr %arrayidx, align 1
50 %cmp1 = icmp eq i8 %0, 0
51 %. = select i1 %cmp1, i8 2, i8 1
52 store i8 %., ptr %arrayidx, align 1
53 %inc = add nsw i32 %i.08, 1
54 %exitcond = icmp eq i32 %i.08, 202
55 br i1 %exitcond, label %for.end, label %for.body
57 for.end: ; preds = %for.body
61 attributes #1 = { minsize }
63 define i32 @foo_pgso() !prof !14 {
64 ; PGSO-LABEL: @foo_pgso(
65 ; PGSO-NOT: <{{[0-9]+}} x i8>
66 ; NPGSO-LABEL: @foo_pgso(
67 ; NPGSO: <{{[0-9]+}} x i8>
72 for.body: ; preds = %for.body, %entry
73 %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
74 %arrayidx = getelementptr inbounds [32 x i8], ptr @tab, i32 0, i32 %i.08
75 %0 = load i8, ptr %arrayidx, align 1
76 %cmp1 = icmp eq i8 %0, 0
77 %. = select i1 %cmp1, i8 2, i8 1
78 store i8 %., ptr %arrayidx, align 1
79 %inc = add nsw i32 %i.08, 1
80 %exitcond = icmp eq i32 %i.08, 202
81 br i1 %exitcond, label %for.end, label %for.body
83 for.end: ; preds = %for.body
87 ; PR43371: don't run into an assert due to emitting SCEV runtime checks
90 @cm_array = external global [2592 x i16], align 1
92 define void @pr43371() optsize {
94 ; CHECK-LABEL: @pr43371
95 ; CHECK-NOT: vector.scevcheck
97 ; We do not want to generate SCEV predicates when optimising for size, because
98 ; that will lead to extra code generation such as the SCEV overflow runtime
99 ; checks. Not generating SCEV predicates can still result in vectorisation as
100 ; the non-consecutive loads/stores can be scalarized:
102 ; CHECK: vector.body:
103 ; CHECK: store i16 0, ptr %{{.*}}, align 1
104 ; CHECK: store i16 0, ptr %{{.*}}, align 1
105 ; CHECK: br i1 {{.*}}, label %vector.body
114 %i24.0170 = phi i16 [ 0, %entry], [ %inc37, %for.body29]
115 %add33 = add i16 undef, %i24.0170
116 %idxprom34 = zext i16 %add33 to i32
117 %arrayidx35 = getelementptr [2592 x i16], ptr @cm_array, i32 0, i32 %idxprom34
118 store i16 0, ptr %arrayidx35, align 1
119 %inc37 = add i16 %i24.0170, 1
120 %cmp26 = icmp ult i16 %inc37, 756
121 br i1 %cmp26, label %for.body29, label %for.cond.cleanup28
124 define void @pr43371_pgso() !prof !14 {
126 ; CHECK-LABEL: @pr43371_pgso
127 ; CHECK-NOT: vector.scevcheck
129 ; We do not want to generate SCEV predicates when optimising for size, because
130 ; that will lead to extra code generation such as the SCEV overflow runtime
131 ; checks. Not generating SCEV predicates can still result in vectorisation as
132 ; the non-consecutive loads/stores can be scalarized:
134 ; CHECK: vector.body:
135 ; CHECK: store i16 0, ptr %{{.*}}, align 1
136 ; CHECK: store i16 0, ptr %{{.*}}, align 1
137 ; CHECK: br i1 {{.*}}, label %vector.body
146 %i24.0170 = phi i16 [ 0, %entry], [ %inc37, %for.body29]
147 %add33 = add i16 undef, %i24.0170
148 %idxprom34 = zext i16 %add33 to i32
149 %arrayidx35 = getelementptr [2592 x i16], ptr @cm_array, i32 0, i32 %idxprom34
150 store i16 0, ptr %arrayidx35, align 1
151 %inc37 = add i16 %i24.0170, 1
152 %cmp26 = icmp ult i16 %inc37, 756
153 br i1 %cmp26, label %for.body29, label %for.cond.cleanup28
156 ; PR45526: don't vectorize with fold-tail if first-order-recurrence is live-out.
158 define i32 @pr45526() optsize {
160 ; CHECK-LABEL: @pr45526
162 ; CHECK-NEXT: br label %loop
165 ; CHECK-NEXT: %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
166 ; CHECK-NEXT: %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
167 ; CHECK-NEXT: %pivPlus1 = add nuw nsw i32 %piv, 1
168 ; CHECK-NEXT: %cond = icmp ult i32 %piv, 510
169 ; CHECK-NEXT: br i1 %cond, label %loop, label %exit
172 ; CHECK-NEXT: %for.lcssa = phi i32 [ %for, %loop ]
173 ; CHECK-NEXT: ret i32 %for.lcssa
179 %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
180 %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
181 %pivPlus1 = add nuw nsw i32 %piv, 1
182 %cond = icmp ult i32 %piv, 510
183 br i1 %cond, label %loop, label %exit
189 define i32 @pr45526_pgso() !prof !14 {
191 ; CHECK-LABEL: @pr45526_pgso
193 ; CHECK-NEXT: br label %loop
196 ; CHECK-NEXT: %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
197 ; CHECK-NEXT: %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
198 ; CHECK-NEXT: %pivPlus1 = add nuw nsw i32 %piv, 1
199 ; CHECK-NEXT: %cond = icmp ult i32 %piv, 510
200 ; CHECK-NEXT: br i1 %cond, label %loop, label %exit
203 ; CHECK-NEXT: %for.lcssa = phi i32 [ %for, %loop ]
204 ; CHECK-NEXT: ret i32 %for.lcssa
210 %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
211 %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
212 %pivPlus1 = add nuw nsw i32 %piv, 1
213 %cond = icmp ult i32 %piv, 510
214 br i1 %cond, label %loop, label %exit
220 ; PR46228: Vectorize w/o versioning for unit stride under optsize and enabled
223 ; NOTE: Some assertions have been autogenerated by utils/update_test_checks.py
224 define void @stride1(ptr noalias %B, i32 %BStride) optsize {
225 ; CHECK-LABEL: @stride1(
227 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
229 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> poison, i32 [[BSTRIDE:%.*]], i64 0
230 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> poison, <2 x i32> zeroinitializer
231 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
232 ; CHECK: vector.body:
233 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE2:%.*]] ]
234 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[PRED_STORE_CONTINUE2]] ]
235 ; CHECK-NEXT: [[TMP1:%.*]] = icmp ule <2 x i32> [[VEC_IND]], <i32 1024, i32 1024>
236 ; CHECK-NEXT: [[TMP0:%.*]] = mul nsw <2 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
237 ; CHECK-NEXT: [[TMP2:%.*]] = extractelement <2 x i1> [[TMP1]], i32 0
238 ; CHECK-NEXT: br i1 [[TMP2]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
239 ; CHECK: pred.store.if:
240 ; CHECK-NEXT: [[TMP3:%.*]] = extractelement <2 x i32> [[TMP0]], i32 0
241 ; CHECK-NEXT: [[TMP4:%.*]] = getelementptr inbounds i16, ptr [[B:%.*]], i32 [[TMP3]]
242 ; CHECK-NEXT: store i16 42, ptr [[TMP4]], align 4
243 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE]]
244 ; CHECK: pred.store.continue:
245 ; CHECK-NEXT: [[TMP5:%.*]] = extractelement <2 x i1> [[TMP1]], i32 1
246 ; CHECK-NEXT: br i1 [[TMP5]], label [[PRED_STORE_IF1:%.*]], label [[PRED_STORE_CONTINUE2]]
247 ; CHECK: pred.store.if1:
248 ; CHECK-NEXT: [[TMP6:%.*]] = extractelement <2 x i32> [[TMP0]], i32 1
249 ; CHECK-NEXT: [[TMP7:%.*]] = getelementptr inbounds i16, ptr [[B]], i32 [[TMP6]]
250 ; CHECK-NEXT: store i16 42, ptr [[TMP7]], align 4
251 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE2]]
252 ; CHECK: pred.store.continue2:
253 ; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 2
254 ; CHECK-NEXT: [[VEC_IND_NEXT]] = add <2 x i32> [[VEC_IND]], <i32 2, i32 2>
255 ; CHECK-NEXT: [[TMP8:%.*]] = icmp eq i32 [[INDEX_NEXT]], 1026
256 ; CHECK-NEXT: br i1 [[TMP8]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]]
257 ; CHECK: middle.block:
258 ; CHECK-NEXT: br i1 true, label [[FOR_END:%.*]], label [[SCALAR_PH]]
261 ; CHECK-NEXT: ret void
263 ; PGSO-LABEL: @stride1(
265 ; PGSO-NEXT: br i1 false, label %scalar.ph, label %vector.ph
267 ; NPGSO-LABEL: @stride1(
269 ; NPGSO-NEXT: br i1 false, label %scalar.ph, label %vector.ph
275 %iv = phi i32 [ %iv.next, %for.body ], [ 0, %entry ]
276 %mulB = mul nsw i32 %iv, %BStride
277 %gepOfB = getelementptr inbounds i16, ptr %B, i32 %mulB
278 store i16 42, ptr %gepOfB, align 4
279 %iv.next = add nuw nsw i32 %iv, 1
280 %exitcond = icmp eq i32 %iv.next, 1025
281 br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !15
287 ; Vectorize with versioning for unit stride for PGSO and enabled vectorization.
289 define void @stride1_pgso(ptr noalias %B, i32 %BStride) !prof !14 {
290 ; CHECK-LABEL: @stride1_pgso(
293 ; PGSO-LABEL: @stride1_pgso(
296 ; NPGSO-LABEL: @stride1_pgso(
303 %iv = phi i32 [ %iv.next, %for.body ], [ 0, %entry ]
304 %mulB = mul nsw i32 %iv, %BStride
305 %gepOfB = getelementptr inbounds i16, ptr %B, i32 %mulB
306 store i16 42, ptr %gepOfB, align 4
307 %iv.next = add nuw nsw i32 %iv, 1
308 %exitcond = icmp eq i32 %iv.next, 1025
309 br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !15
315 ; PR46652: Check that the need for stride==1 check prevents vectorizing a loop
316 ; having tiny trip count, when compiling w/o -Os/-Oz.
317 ; CHECK-LABEL: @pr46652
318 ; CHECK-NOT: vector.scevcheck
319 ; CHECK-NOT: vector.body
320 ; CHECK-LABEL: for.body
322 @g = external global [1 x i16], align 1
324 define void @pr46652(i16 %stride) {
328 for.body: ; preds = %for.body, %entry
329 %l1.02 = phi i16 [ 1, %entry ], [ %inc9, %for.body ]
330 %mul = mul nsw i16 %l1.02, %stride
331 %arrayidx6 = getelementptr inbounds [1 x i16], ptr @g, i16 0, i16 %mul
332 %0 = load i16, ptr %arrayidx6, align 1
333 %inc9 = add nuw nsw i16 %l1.02, 1
334 %exitcond.not = icmp eq i16 %inc9, 16
335 br i1 %exitcond.not, label %for.end, label %for.body
337 for.end: ; preds = %for.body
341 ; Make sure we do not crash while building the VPlan for the loop with the
343 define i32 @PR48142(ptr %ptr.start, ptr %ptr.end) optsize {
344 ; CHECK-LABEL: PR48142
345 ; CHECK-NOT: vector.body
350 %i.014 = phi i32 [ 20, %entry ], [ %cond, %for.body ]
351 %ptr.iv = phi ptr [ %ptr.start, %entry ], [ %ptr.next, %for.body ]
352 %cmp4 = icmp slt i32 %i.014, 99
353 %cond = select i1 %cmp4, i32 99, i32 %i.014
354 store i32 0, ptr %ptr.iv
355 %ptr.next = getelementptr inbounds i32, ptr %ptr.iv, i64 1
356 %cmp.not = icmp eq ptr %ptr.next, %ptr.end
357 br i1 %cmp.not, label %exit, label %for.body
360 %res = phi i32 [ %cond, %for.body ]
364 !llvm.module.flags = !{!0}
365 !0 = !{i32 1, !"ProfileSummary", !1}
366 !1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
367 !2 = !{!"ProfileFormat", !"InstrProf"}
368 !3 = !{!"TotalCount", i64 10000}
369 !4 = !{!"MaxCount", i64 10}
370 !5 = !{!"MaxInternalCount", i64 1}
371 !6 = !{!"MaxFunctionCount", i64 1000}
372 !7 = !{!"NumCounts", i64 3}
373 !8 = !{!"NumFunctions", i64 3}
374 !9 = !{!"DetailedSummary", !10}
375 !10 = !{!11, !12, !13}
376 !11 = !{i32 10000, i64 100, i32 1}
377 !12 = !{i32 999000, i64 100, i32 1}
378 !13 = !{i32 999999, i64 1, i32 2}
379 !14 = !{!"function_entry_count", i64 0}
380 !15 = distinct !{!15, !16}
381 !16 = !{!"llvm.loop.vectorize.enable", i1 true}