1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt < %s -loop-vectorize -force-vector-width=4 -S | FileCheck %s
4 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
6 ; Make sure the loop is vectorized under -Os without folding its tail based on
7 ; its trip-count's lower bits known to be zero.
9 define dso_local void @alignTC(i32* noalias nocapture %A, i32 %n) optsize {
10 ; CHECK-LABEL: @alignTC(
12 ; CHECK-NEXT: [[ALIGNEDTC:%.*]] = and i32 [[N:%.*]], -8
13 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[ALIGNEDTC]], 4
14 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
16 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[ALIGNEDTC]], 4
17 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[ALIGNEDTC]], [[N_MOD_VF]]
18 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
20 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
21 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[INDEX]], i32 0
22 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
23 ; CHECK-NEXT: [[INDUCTION:%.*]] = add <4 x i32> [[BROADCAST_SPLAT]], <i32 0, i32 1, i32 2, i32 3>
24 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[INDEX]], 0
25 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i32 [[TMP0]]
26 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, i32* [[TMP1]], i32 0
27 ; CHECK-NEXT: [[TMP3:%.*]] = bitcast i32* [[TMP2]] to <4 x i32>*
28 ; CHECK-NEXT: store <4 x i32> <i32 13, i32 13, i32 13, i32 13>, <4 x i32>* [[TMP3]], align 1
29 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
30 ; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
31 ; CHECK-NEXT: br i1 [[TMP4]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP0:!llvm.loop !.*]]
32 ; CHECK: middle.block:
33 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[ALIGNEDTC]], [[N_VEC]]
34 ; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT:%.*]], label [[SCALAR_PH]]
36 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[ENTRY:%.*]] ]
37 ; CHECK-NEXT: br label [[LOOP:%.*]]
39 ; CHECK-NEXT: [[RIV:%.*]] = phi i32 [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ], [ [[RIVPLUS1:%.*]], [[LOOP]] ]
40 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[RIV]]
41 ; CHECK-NEXT: store i32 13, i32* [[ARRAYIDX]], align 1
42 ; CHECK-NEXT: [[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
43 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], [[ALIGNEDTC]]
44 ; CHECK-NEXT: br i1 [[COND]], label [[EXIT]], label [[LOOP]], [[LOOP2:!llvm.loop !.*]]
46 ; CHECK-NEXT: ret void
49 %alignedTC = and i32 %n, -8
53 %riv = phi i32 [ 0, %entry ], [ %rivPlus1, %loop ]
54 %arrayidx = getelementptr inbounds i32, i32* %A, i32 %riv
55 store i32 13, i32* %arrayidx, align 1
56 %rivPlus1 = add nuw nsw i32 %riv, 1
57 %cond = icmp eq i32 %rivPlus1, %alignedTC
58 br i1 %cond, label %exit, label %loop
64 ; Make sure the loop is vectorized under -Os without folding its tail based on
65 ; its trip-count's lower bits assumed to be zero.
67 define dso_local void @assumeAlignedTC(i32* noalias nocapture %A, i32 %p, i32 %q) optsize {
68 ; CHECK-LABEL: @assumeAlignedTC(
70 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[P:%.*]], 3
71 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[AND1]], 0
72 ; CHECK-NEXT: br i1 [[CMP1]], label [[GUARDED:%.*]], label [[EXIT:%.*]]
74 ; CHECK-NEXT: [[REM:%.*]] = urem i32 [[Q:%.*]], 8
75 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[REM]], 0
76 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
77 ; CHECK-NEXT: [[GT:%.*]] = icmp sgt i32 [[P]], [[Q]]
78 ; CHECK-NEXT: [[N:%.*]] = select i1 [[GT]], i32 [[P]], i32 [[Q]]
79 ; CHECK-NEXT: [[CMP110:%.*]] = icmp sgt i32 [[N]], 0
80 ; CHECK-NEXT: br i1 [[CMP110]], label [[LOOP_PREHEADER:%.*]], label [[EXIT]]
81 ; CHECK: loop.preheader:
82 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i32 [[N]], 4
83 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
85 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N]], 4
86 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N]], [[N_MOD_VF]]
87 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
89 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[VECTOR_BODY]] ]
90 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[INDEX]], i32 0
91 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
92 ; CHECK-NEXT: [[INDUCTION:%.*]] = add <4 x i32> [[BROADCAST_SPLAT]], <i32 0, i32 1, i32 2, i32 3>
93 ; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[INDEX]], 0
94 ; CHECK-NEXT: [[TMP1:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i32 [[TMP0]]
95 ; CHECK-NEXT: [[TMP2:%.*]] = getelementptr inbounds i32, i32* [[TMP1]], i32 0
96 ; CHECK-NEXT: [[TMP3:%.*]] = bitcast i32* [[TMP2]] to <4 x i32>*
97 ; CHECK-NEXT: store <4 x i32> <i32 13, i32 13, i32 13, i32 13>, <4 x i32>* [[TMP3]], align 1
98 ; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i32 [[INDEX]], 4
99 ; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
100 ; CHECK-NEXT: br i1 [[TMP4]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP4:!llvm.loop !.*]]
101 ; CHECK: middle.block:
102 ; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i32 [[N]], [[N_VEC]]
103 ; CHECK-NEXT: br i1 [[CMP_N]], label [[EXIT_LOOPEXIT:%.*]], label [[SCALAR_PH]]
105 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[LOOP_PREHEADER]] ]
106 ; CHECK-NEXT: br label [[LOOP:%.*]]
108 ; CHECK-NEXT: [[RIV:%.*]] = phi i32 [ [[RIVPLUS1:%.*]], [[LOOP]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
109 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[RIV]]
110 ; CHECK-NEXT: store i32 13, i32* [[ARRAYIDX]], align 1
111 ; CHECK-NEXT: [[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
112 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], [[N]]
113 ; CHECK-NEXT: br i1 [[COND]], label [[EXIT_LOOPEXIT]], label [[LOOP]], [[LOOP5:!llvm.loop !.*]]
114 ; CHECK: exit.loopexit:
115 ; CHECK-NEXT: br label [[EXIT]]
117 ; CHECK-NEXT: ret void
120 %and1 = and i32 %p, 3
121 %cmp1 = icmp eq i32 %and1, 0
122 br i1 %cmp1, label %guarded, label %exit
125 %rem = urem i32 %q, 8
126 %cmp2 = icmp eq i32 %rem, 0
127 tail call void @llvm.assume(i1 %cmp2)
128 %gt = icmp sgt i32 %p, %q
129 %n = select i1 %gt, i32 %p, i32 %q
130 %cmp110 = icmp sgt i32 %n, 0
131 br i1 %cmp110, label %loop, label %exit
134 %riv = phi i32 [ 0, %guarded ], [ %rivPlus1, %loop ]
135 %arrayidx = getelementptr inbounds i32, i32* %A, i32 %riv
136 store i32 13, i32* %arrayidx, align 1
137 %rivPlus1 = add nuw nsw i32 %riv, 1
138 %cond = icmp eq i32 %rivPlus1, %n
139 br i1 %cond, label %exit, label %loop
145 ; Make sure the loop's tail is folded when vectorized under -Os based on its trip-count's
146 ; not being provably divisible by chosen VF.
148 define dso_local void @cannotProveAlignedTC(i32* noalias nocapture %A, i32 %p, i32 %q) optsize {
149 ; CHECK-LABEL: @cannotProveAlignedTC(
151 ; CHECK-NEXT: [[AND1:%.*]] = and i32 [[P:%.*]], 3
152 ; CHECK-NEXT: [[CMP1:%.*]] = icmp eq i32 [[AND1]], 0
153 ; CHECK-NEXT: br i1 [[CMP1]], label [[GUARDED:%.*]], label [[EXIT:%.*]]
155 ; CHECK-NEXT: [[REM:%.*]] = urem i32 [[Q:%.*]], 3
156 ; CHECK-NEXT: [[CMP2:%.*]] = icmp eq i32 [[REM]], 0
157 ; CHECK-NEXT: tail call void @llvm.assume(i1 [[CMP2]])
158 ; CHECK-NEXT: [[GT:%.*]] = icmp sgt i32 [[P]], [[Q]]
159 ; CHECK-NEXT: [[N:%.*]] = select i1 [[GT]], i32 [[P]], i32 [[Q]]
160 ; CHECK-NEXT: [[CMP110:%.*]] = icmp sgt i32 [[N]], 0
161 ; CHECK-NEXT: br i1 [[CMP110]], label [[LOOP_PREHEADER:%.*]], label [[EXIT]]
162 ; CHECK: loop.preheader:
163 ; CHECK-NEXT: br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
165 ; CHECK-NEXT: [[N_RND_UP:%.*]] = add i32 [[N]], 3
166 ; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i32 [[N_RND_UP]], 4
167 ; CHECK-NEXT: [[N_VEC:%.*]] = sub i32 [[N_RND_UP]], [[N_MOD_VF]]
168 ; CHECK-NEXT: [[TRIP_COUNT_MINUS_1:%.*]] = sub i32 [[N]], 1
169 ; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TRIP_COUNT_MINUS_1]], i32 0
170 ; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
171 ; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
172 ; CHECK: vector.body:
173 ; CHECK-NEXT: [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE6:%.*]] ]
174 ; CHECK-NEXT: [[VEC_IND:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], [[PRED_STORE_CONTINUE6]] ]
175 ; CHECK-NEXT: [[TMP0:%.*]] = icmp ule <4 x i32> [[VEC_IND]], [[BROADCAST_SPLAT]]
176 ; CHECK-NEXT: [[TMP1:%.*]] = extractelement <4 x i1> [[TMP0]], i32 0
177 ; CHECK-NEXT: br i1 [[TMP1]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
178 ; CHECK: pred.store.if:
179 ; CHECK-NEXT: [[TMP2:%.*]] = add i32 [[INDEX]], 0
180 ; CHECK-NEXT: [[TMP3:%.*]] = getelementptr inbounds i32, i32* [[A:%.*]], i32 [[TMP2]]
181 ; CHECK-NEXT: store i32 13, i32* [[TMP3]], align 1
182 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE]]
183 ; CHECK: pred.store.continue:
184 ; CHECK-NEXT: [[TMP4:%.*]] = extractelement <4 x i1> [[TMP0]], i32 1
185 ; CHECK-NEXT: br i1 [[TMP4]], label [[PRED_STORE_IF1:%.*]], label [[PRED_STORE_CONTINUE2:%.*]]
186 ; CHECK: pred.store.if1:
187 ; CHECK-NEXT: [[TMP5:%.*]] = add i32 [[INDEX]], 1
188 ; CHECK-NEXT: [[TMP6:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[TMP5]]
189 ; CHECK-NEXT: store i32 13, i32* [[TMP6]], align 1
190 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE2]]
191 ; CHECK: pred.store.continue2:
192 ; CHECK-NEXT: [[TMP7:%.*]] = extractelement <4 x i1> [[TMP0]], i32 2
193 ; CHECK-NEXT: br i1 [[TMP7]], label [[PRED_STORE_IF3:%.*]], label [[PRED_STORE_CONTINUE4:%.*]]
194 ; CHECK: pred.store.if3:
195 ; CHECK-NEXT: [[TMP8:%.*]] = add i32 [[INDEX]], 2
196 ; CHECK-NEXT: [[TMP9:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[TMP8]]
197 ; CHECK-NEXT: store i32 13, i32* [[TMP9]], align 1
198 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE4]]
199 ; CHECK: pred.store.continue4:
200 ; CHECK-NEXT: [[TMP10:%.*]] = extractelement <4 x i1> [[TMP0]], i32 3
201 ; CHECK-NEXT: br i1 [[TMP10]], label [[PRED_STORE_IF5:%.*]], label [[PRED_STORE_CONTINUE6]]
202 ; CHECK: pred.store.if5:
203 ; CHECK-NEXT: [[TMP11:%.*]] = add i32 [[INDEX]], 3
204 ; CHECK-NEXT: [[TMP12:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[TMP11]]
205 ; CHECK-NEXT: store i32 13, i32* [[TMP12]], align 1
206 ; CHECK-NEXT: br label [[PRED_STORE_CONTINUE6]]
207 ; CHECK: pred.store.continue6:
208 ; CHECK-NEXT: [[INDEX_NEXT]] = add i32 [[INDEX]], 4
209 ; CHECK-NEXT: [[VEC_IND_NEXT]] = add <4 x i32> [[VEC_IND]], <i32 4, i32 4, i32 4, i32 4>
210 ; CHECK-NEXT: [[TMP13:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
211 ; CHECK-NEXT: br i1 [[TMP13]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], [[LOOP6:!llvm.loop !.*]]
212 ; CHECK: middle.block:
213 ; CHECK-NEXT: br i1 true, label [[EXIT_LOOPEXIT:%.*]], label [[SCALAR_PH]]
215 ; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i32 [ [[N_VEC]], [[MIDDLE_BLOCK]] ], [ 0, [[LOOP_PREHEADER]] ]
216 ; CHECK-NEXT: br label [[LOOP:%.*]]
218 ; CHECK-NEXT: [[RIV:%.*]] = phi i32 [ [[RIVPLUS1:%.*]], [[LOOP]] ], [ [[BC_RESUME_VAL]], [[SCALAR_PH]] ]
219 ; CHECK-NEXT: [[ARRAYIDX:%.*]] = getelementptr inbounds i32, i32* [[A]], i32 [[RIV]]
220 ; CHECK-NEXT: store i32 13, i32* [[ARRAYIDX]], align 1
221 ; CHECK-NEXT: [[RIVPLUS1]] = add nuw nsw i32 [[RIV]], 1
222 ; CHECK-NEXT: [[COND:%.*]] = icmp eq i32 [[RIVPLUS1]], [[N]]
223 ; CHECK-NEXT: br i1 [[COND]], label [[EXIT_LOOPEXIT]], label [[LOOP]], [[LOOP7:!llvm.loop !.*]]
224 ; CHECK: exit.loopexit:
225 ; CHECK-NEXT: br label [[EXIT]]
227 ; CHECK-NEXT: ret void
230 %and1 = and i32 %p, 3
231 %cmp1 = icmp eq i32 %and1, 0
232 br i1 %cmp1, label %guarded, label %exit
235 %rem = urem i32 %q, 3
236 %cmp2 = icmp eq i32 %rem, 0
237 tail call void @llvm.assume(i1 %cmp2)
238 %gt = icmp sgt i32 %p, %q
239 %n = select i1 %gt, i32 %p, i32 %q
240 %cmp110 = icmp sgt i32 %n, 0
241 br i1 %cmp110, label %loop, label %exit
244 %riv = phi i32 [ 0, %guarded ], [ %rivPlus1, %loop ]
245 %arrayidx = getelementptr inbounds i32, i32* %A, i32 %riv
246 store i32 13, i32* %arrayidx, align 1
247 %rivPlus1 = add nuw nsw i32 %riv, 1
248 %cond = icmp eq i32 %rivPlus1, %n
249 br i1 %cond, label %exit, label %loop
255 declare void @llvm.assume(i1 noundef) nofree nosync nounwind willreturn