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.
3; REQUIRES: asserts
4; RUN: opt < %s -loop-vectorize -S | FileCheck %s
5; RUN: opt < %s -loop-vectorize -pgso -S | FileCheck %s -check-prefix=PGSO
6; RUN: opt < %s -loop-vectorize -pgso=false -S | FileCheck %s -check-prefix=NPGSO
7
8target datalayout = "E-m:e-p:32:32-i64:32-f64:32:64-a:0:32-n32-S128"
9
10@tab = common global [32 x i8] zeroinitializer, align 1
11
12define i32 @foo_optsize() #0 {
13; CHECK-LABEL: @foo_optsize(
14; CHECK-NOT: <2 x i8>
15; CHECK-NOT: <4 x i8>
16
17entry:
18  br label %for.body
19
20for.body:                                         ; preds = %for.body, %entry
21  %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
22  %arrayidx = getelementptr inbounds [32 x i8], [32 x i8]* @tab, i32 0, i32 %i.08
23  %0 = load i8, i8* %arrayidx, align 1
24  %cmp1 = icmp eq i8 %0, 0
25  %. = select i1 %cmp1, i8 2, i8 1
26  store i8 %., i8* %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
30
31for.end:                                          ; preds = %for.body
32  ret i32 0
33}
34
35attributes #0 = { optsize }
36
37define i32 @foo_minsize() #1 {
38; CHECK-LABEL: @foo_minsize(
39; CHECK-NOT: <2 x i8>
40; CHECK-NOT: <4 x i8>
41; CHECK-LABEL: @foo_pgso(
42
43entry:
44  br label %for.body
45
46for.body:                                         ; preds = %for.body, %entry
47  %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
48  %arrayidx = getelementptr inbounds [32 x i8], [32 x i8]* @tab, i32 0, i32 %i.08
49  %0 = load i8, i8* %arrayidx, align 1
50  %cmp1 = icmp eq i8 %0, 0
51  %. = select i1 %cmp1, i8 2, i8 1
52  store i8 %., i8* %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
56
57for.end:                                          ; preds = %for.body
58  ret i32 0
59}
60
61attributes #1 = { minsize }
62
63define 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>
68
69entry:
70  br label %for.body
71
72for.body:                                         ; preds = %for.body, %entry
73  %i.08 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
74  %arrayidx = getelementptr inbounds [32 x i8], [32 x i8]* @tab, i32 0, i32 %i.08
75  %0 = load i8, i8* %arrayidx, align 1
76  %cmp1 = icmp eq i8 %0, 0
77  %. = select i1 %cmp1, i8 2, i8 1
78  store i8 %., i8* %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
82
83for.end:                                          ; preds = %for.body
84  ret i32 0
85}
86
87; PR43371: don't run into an assert due to emitting SCEV runtime checks
88; with OptForSize.
89;
90@cm_array = external global [2592 x i16], align 1
91
92define void @pr43371() optsize {
93;
94; CHECK-LABEL: @pr43371
95; CHECK-NOT:   vector.scevcheck
96;
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:
101;
102; CHECK: vector.body:
103; CHECK: store i16 0, i16* %{{.*}}, align 1
104; CHECK: store i16 0, i16* %{{.*}}, align 1
105; CHECK: br i1 {{.*}}, label %vector.body
106;
107entry:
108  br label %for.body29
109
110for.cond.cleanup28:
111  unreachable
112
113for.body29:
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], [2592 x i16] * @cm_array, i32 0, i32 %idxprom34
118  store i16 0, i16 * %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
122}
123
124; PR45526: don't vectorize with fold-tail if first-order-recurrence is live-out.
125;
126define i32 @pr45526() optsize {
127;
128; CHECK-LABEL: @pr45526
129; CHECK-NEXT: entry:
130; CHECK-NEXT:   br label %loop
131; CHECK-EMPTY:
132; CHECK-NEXT: loop:
133; CHECK-NEXT:   %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
134; CHECK-NEXT:   %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
135; CHECK-NEXT:   %pivPlus1 = add nuw nsw i32 %piv, 1
136; CHECK-NEXT:   %cond = icmp ult i32 %piv, 510
137; CHECK-NEXT:   br i1 %cond, label %loop, label %exit
138; CHECK-EMPTY:
139; CHECK-NEXT: exit:
140; CHECK-NEXT:   %for.lcssa = phi i32 [ %for, %loop ]
141; CHECK-NEXT:   ret i32 %for.lcssa
142;
143entry:
144  br label %loop
145
146loop:
147  %piv = phi i32 [ 0, %entry ], [ %pivPlus1, %loop ]
148  %for = phi i32 [ 5, %entry ], [ %pivPlus1, %loop ]
149  %pivPlus1 = add nuw nsw i32 %piv, 1
150  %cond = icmp ult i32 %piv, 510
151  br i1 %cond, label %loop, label %exit
152
153exit:
154  ret i32 %for
155}
156
157; PR46228: Vectorize w/o versioning for unit stride under optsize and enabled
158; vectorization.
159
160; NOTE: Some assertions have been autogenerated by utils/update_test_checks.py
161define void @stride1(i16* noalias %B, i32 %BStride) optsize {
162; CHECK-LABEL: @stride1(
163; CHECK-NEXT:  entry:
164; CHECK-NEXT:    br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
165; CHECK:       vector.ph:
166; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <2 x i32> undef, i32 [[BSTRIDE:%.*]], i32 0
167; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <2 x i32> [[BROADCAST_SPLATINSERT]], <2 x i32> undef, <2 x i32> zeroinitializer
168; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
169; CHECK:       vector.body:
170; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE2:%.*]] ]
171; CHECK-NEXT:    [[VEC_IND:%.*]] = phi <2 x i32> [ <i32 0, i32 1>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[PRED_STORE_CONTINUE2]] ]
172; CHECK-NEXT:    [[TMP0:%.*]] = mul nsw <2 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
173; CHECK-NEXT:    [[TMP1:%.*]] = icmp ule <2 x i32> [[VEC_IND]], <i32 1024, i32 1024>
174; CHECK-NEXT:    [[TMP2:%.*]] = extractelement <2 x i1> [[TMP1]], i32 0
175; CHECK-NEXT:    br i1 [[TMP2]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
176; CHECK:       pred.store.if:
177; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <2 x i32> [[TMP0]], i32 0
178; CHECK-NEXT:    [[TMP4:%.*]] = getelementptr inbounds i16, i16* [[B:%.*]], i32 [[TMP3]]
179; CHECK-NEXT:    store i16 42, i16* [[TMP4]], align 4
180; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE]]
181; CHECK:       pred.store.continue:
182; CHECK-NEXT:    [[TMP5:%.*]] = extractelement <2 x i1> [[TMP1]], i32 1
183; CHECK-NEXT:    br i1 [[TMP5]], label [[PRED_STORE_IF1:%.*]], label [[PRED_STORE_CONTINUE2]]
184; CHECK:       pred.store.if1:
185; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <2 x i32> [[TMP0]], i32 1
186; CHECK-NEXT:    [[TMP7:%.*]] = getelementptr inbounds i16, i16* [[B]], i32 [[TMP6]]
187; CHECK-NEXT:    store i16 42, i16* [[TMP7]], align 4
188; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE2]]
189; CHECK:       pred.store.continue2:
190; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 2
191; CHECK-NEXT:    [[VEC_IND_NEXT]] = add <2 x i32> [[VEC_IND]], <i32 2, i32 2>
192; CHECK-NEXT:    [[TMP8:%.*]] = icmp eq i32 [[INDEX_NEXT]], 1026
193; CHECK-NEXT:    br i1 [[TMP8]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop !19
194; CHECK:       middle.block:
195; CHECK-NEXT:    br i1 true, label [[FOR_END:%.*]], label [[SCALAR_PH]]
196; CHECK:       scalar.ph:
197; CHECK:       for.end:
198; CHECK-NEXT:    ret void
199;
200; PGSO-LABEL: @stride1(
201; PGSO-NEXT:  entry:
202; PGSO-NEXT:    br i1 false, label %scalar.ph, label %vector.ph
203;
204; NPGSO-LABEL: @stride1(
205; NPGSO-NEXT:  entry:
206; NPGSO-NEXT:    br i1 false, label %scalar.ph, label %vector.ph
207
208entry:
209  br label %for.body
210
211for.body:
212  %iv = phi i32 [ %iv.next, %for.body ], [ 0, %entry ]
213  %mulB = mul nsw i32 %iv, %BStride
214  %gepOfB = getelementptr inbounds i16, i16* %B, i32 %mulB
215  store i16 42, i16* %gepOfB, align 4
216  %iv.next = add nuw nsw i32 %iv, 1
217  %exitcond = icmp eq i32 %iv.next, 1025
218  br i1 %exitcond, label %for.end, label %for.body, !llvm.loop !15
219
220for.end:
221  ret void
222}
223
224!llvm.module.flags = !{!0}
225!0 = !{i32 1, !"ProfileSummary", !1}
226!1 = !{!2, !3, !4, !5, !6, !7, !8, !9}
227!2 = !{!"ProfileFormat", !"InstrProf"}
228!3 = !{!"TotalCount", i64 10000}
229!4 = !{!"MaxCount", i64 10}
230!5 = !{!"MaxInternalCount", i64 1}
231!6 = !{!"MaxFunctionCount", i64 1000}
232!7 = !{!"NumCounts", i64 3}
233!8 = !{!"NumFunctions", i64 3}
234!9 = !{!"DetailedSummary", !10}
235!10 = !{!11, !12, !13}
236!11 = !{i32 10000, i64 100, i32 1}
237!12 = !{i32 999000, i64 100, i32 1}
238!13 = !{i32 999999, i64 1, i32 2}
239!14 = !{!"function_entry_count", i64 0}
240!15 = distinct !{!15, !16}
241!16 = !{!"llvm.loop.vectorize.enable", i1 true}
242