1; RUN: opt < %s -loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -dce -instcombine -S | FileCheck %s
2; RUN: opt < %s -loop-vectorize -force-vector-width=4 -force-vector-interleave=2 -dce -instcombine -S | FileCheck %s --check-prefix=UNROLL
3; RUN: opt < %s -loop-vectorize -force-vector-width=4 -force-vector-interleave=2 -S | FileCheck %s --check-prefix=UNROLL-NO-IC
4; RUN: opt < %s -loop-vectorize -force-vector-width=1 -force-vector-interleave=2 -S | FileCheck %s --check-prefix=UNROLL-NO-VF
5; RUN: opt < %s -loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S | FileCheck %s --check-prefix=SINK-AFTER
6; RUN: opt < %s -loop-vectorize -force-vector-width=4 -force-vector-interleave=1 -S | FileCheck %s --check-prefix=NO-SINK-AFTER
7
8target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
9
10; void recurrence_1(int *a, int *b, int n) {
11;   for(int i = 0; i < n; i++)
12;     b[i] =  a[i] + a[i - 1]
13; }
14;
15; CHECK-LABEL: @recurrence_1(
16; CHECK:       vector.ph:
17; CHECK:         %vector.recur.init = insertelement <4 x i32> poison, i32 %pre_load, i32 3
18; CHECK:       vector.body:
19; CHECK:         %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ [[L1:%[a-zA-Z0-9.]+]], %vector.body ]
20; CHECK:         [[L1]] = load <4 x i32>
21; CHECK:         {{.*}} = shufflevector <4 x i32> %vector.recur, <4 x i32> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
22; CHECK:       middle.block:
23; CHECK:         %vector.recur.extract = extractelement <4 x i32> [[L1]], i32 3
24; CHECK:       scalar.ph:
25; CHECK:         %scalar.recur.init = phi i32 [ %pre_load, %vector.memcheck ], [ %pre_load, %for.preheader ], [ %vector.recur.extract, %middle.block ]
26; CHECK:       scalar.body:
27; CHECK:         %scalar.recur = phi i32 [ %scalar.recur.init, %scalar.ph ], [ {{.*}}, %scalar.body ]
28;
29; UNROLL-LABEL: @recurrence_1(
30; UNROLL:       vector.body:
31; UNROLL:         %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ [[L2:%[a-zA-Z0-9.]+]], %vector.body ]
32; UNROLL:         [[L1:%[a-zA-Z0-9.]+]] = load <4 x i32>
33; UNROLL:         [[L2]] = load <4 x i32>
34; UNROLL:         {{.*}} = shufflevector <4 x i32> %vector.recur, <4 x i32> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
35; UNROLL:         {{.*}} = shufflevector <4 x i32> [[L1]], <4 x i32> [[L2]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
36; UNROLL:       middle.block:
37; UNROLL:         %vector.recur.extract = extractelement <4 x i32> [[L2]], i32 3
38;
39define void @recurrence_1(i32* nocapture readonly %a, i32* nocapture %b, i32 %n) {
40entry:
41  br label %for.preheader
42
43for.preheader:
44  %arrayidx.phi.trans.insert = getelementptr inbounds i32, i32* %a, i64 0
45  %pre_load = load i32, i32* %arrayidx.phi.trans.insert
46  br label %scalar.body
47
48scalar.body:
49  %0 = phi i32 [ %pre_load, %for.preheader ], [ %1, %scalar.body ]
50  %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
51  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
52  %arrayidx32 = getelementptr inbounds i32, i32* %a, i64 %indvars.iv.next
53  %1 = load i32, i32* %arrayidx32
54  %arrayidx34 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
55  %add35 = add i32 %1, %0
56  store i32 %add35, i32* %arrayidx34
57  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
58  %exitcond = icmp eq i32 %lftr.wideiv, %n
59  br i1 %exitcond, label %for.exit, label %scalar.body
60
61for.exit:
62  ret void
63}
64
65; int recurrence_2(int *a, int n) {
66;   int minmax;
67;   for (int i = 0; i < n; ++i)
68;     minmax = min(minmax, max(a[i] - a[i-1], 0));
69;   return minmax;
70; }
71;
72; CHECK-LABEL: @recurrence_2(
73; CHECK:       vector.ph:
74; CHECK:         %vector.recur.init = insertelement <4 x i32> poison, i32 %.pre, i32 3
75; CHECK:       vector.body:
76; CHECK:         %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ [[L1:%[a-zA-Z0-9.]+]], %vector.body ]
77; CHECK:         [[L1]] = load <4 x i32>
78; CHECK:         {{.*}} = shufflevector <4 x i32> %vector.recur, <4 x i32> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
79; CHECK:       middle.block:
80; CHECK:         %vector.recur.extract = extractelement <4 x i32> [[L1]], i32 3
81; CHECK:       scalar.ph:
82; CHECK:         %scalar.recur.init = phi i32 [ %.pre, %for.preheader ], [ %vector.recur.extract, %middle.block ]
83; CHECK:       scalar.body:
84; CHECK:         %scalar.recur = phi i32 [ %scalar.recur.init, %scalar.ph ], [ {{.*}}, %scalar.body ]
85;
86; UNROLL-LABEL: @recurrence_2(
87; UNROLL:       vector.body:
88; UNROLL:         %vector.recur = phi <4 x i32> [ %vector.recur.init, %vector.ph ], [ [[L2:%[a-zA-Z0-9.]+]], %vector.body ]
89; UNROLL:         [[L1:%[a-zA-Z0-9.]+]] = load <4 x i32>
90; UNROLL:         [[L2]] = load <4 x i32>
91; UNROLL:         {{.*}} = shufflevector <4 x i32> %vector.recur, <4 x i32> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
92; UNROLL:         {{.*}} = shufflevector <4 x i32> [[L1]], <4 x i32> [[L2]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
93; UNROLL:       middle.block:
94; UNROLL:         %vector.recur.extract = extractelement <4 x i32> [[L2]], i32 3
95;
96define i32 @recurrence_2(i32* nocapture readonly %a, i32 %n) {
97entry:
98  %cmp27 = icmp sgt i32 %n, 0
99  br i1 %cmp27, label %for.preheader, label %for.cond.cleanup
100
101for.preheader:
102  %arrayidx2.phi.trans.insert = getelementptr inbounds i32, i32* %a, i64 -1
103  %.pre = load i32, i32* %arrayidx2.phi.trans.insert, align 4
104  br label %scalar.body
105
106for.cond.cleanup.loopexit:
107  %minmax.0.cond.lcssa = phi i32 [ %minmax.0.cond, %scalar.body ]
108  br label %for.cond.cleanup
109
110for.cond.cleanup:
111  %minmax.0.lcssa = phi i32 [ poison, %entry ], [ %minmax.0.cond.lcssa, %for.cond.cleanup.loopexit ]
112  ret i32 %minmax.0.lcssa
113
114scalar.body:
115  %0 = phi i32 [ %.pre, %for.preheader ], [ %1, %scalar.body ]
116  %indvars.iv = phi i64 [ 0, %for.preheader ], [ %indvars.iv.next, %scalar.body ]
117  %minmax.028 = phi i32 [ poison, %for.preheader ], [ %minmax.0.cond, %scalar.body ]
118  %arrayidx = getelementptr inbounds i32, i32* %a, i64 %indvars.iv
119  %1 = load i32, i32* %arrayidx, align 4
120  %sub3 = sub nsw i32 %1, %0
121  %cmp4 = icmp sgt i32 %sub3, 0
122  %cond = select i1 %cmp4, i32 %sub3, i32 0
123  %cmp5 = icmp slt i32 %minmax.028, %cond
124  %minmax.0.cond = select i1 %cmp5, i32 %minmax.028, i32 %cond
125  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
126  %lftr.wideiv = trunc i64 %indvars.iv.next to i32
127  %exitcond = icmp eq i32 %lftr.wideiv, %n
128  br i1 %exitcond, label %for.cond.cleanup.loopexit, label %scalar.body
129}
130
131; void recurrence_3(short *a, double *b, int n, float f, short p) {
132;   b[0] = (double)a[0] - f * (double)p;
133;   for (int i = 1; i < n; i++)
134;     b[i] = (double)a[i] - f * (double)a[i - 1];
135; }
136;
137; CHECK-LABEL: @recurrence_3(
138; CHECK:       vector.ph:
139; CHECK:         %vector.recur.init = insertelement <4 x i16> poison, i16 %0, i32 3
140; CHECK:       vector.body:
141; CHECK:         %vector.recur = phi <4 x i16> [ %vector.recur.init, %vector.ph ], [ [[L1:%[a-zA-Z0-9.]+]], %vector.body ]
142; CHECK:         [[L1]] = load <4 x i16>
143; CHECK:         [[SHUF:%[a-zA-Z0-9.]+]] = shufflevector <4 x i16> %vector.recur, <4 x i16> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
144; Check also that the casts were not moved needlessly.
145; CHECK:         sitofp <4 x i16> [[L1]] to <4 x double>
146; CHECK:         sitofp <4 x i16> [[SHUF]] to <4 x double>
147; CHECK:       middle.block:
148; CHECK:         %vector.recur.extract = extractelement <4 x i16> [[L1]], i32 3
149; CHECK:       scalar.ph:
150; CHECK:         %scalar.recur.init = phi i16 [ %0, %vector.memcheck ], [ %0, %for.preheader ], [ %vector.recur.extract, %middle.block ]
151; CHECK:       scalar.body:
152; CHECK:         %scalar.recur = phi i16 [ %scalar.recur.init, %scalar.ph ], [ {{.*}}, %scalar.body ]
153;
154; UNROLL-LABEL: @recurrence_3(
155; UNROLL:       vector.body:
156; UNROLL:         %vector.recur = phi <4 x i16> [ %vector.recur.init, %vector.ph ], [ [[L2:%[a-zA-Z0-9.]+]], %vector.body ]
157; UNROLL:         [[L1:%[a-zA-Z0-9.]+]] = load <4 x i16>
158; UNROLL:         [[L2]] = load <4 x i16>
159; UNROLL:         {{.*}} = shufflevector <4 x i16> %vector.recur, <4 x i16> [[L1]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
160; UNROLL:         {{.*}} = shufflevector <4 x i16> [[L1]], <4 x i16> [[L2]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
161; UNROLL:       middle.block:
162; UNROLL:         %vector.recur.extract = extractelement <4 x i16> [[L2]], i32 3
163;
164define void @recurrence_3(i16* nocapture readonly %a, double* nocapture %b, i32 %n, float %f, i16 %p) {
165entry:
166  %0 = load i16, i16* %a, align 2
167  %conv = sitofp i16 %0 to double
168  %conv1 = fpext float %f to double
169  %conv2 = sitofp i16 %p to double
170  %mul = fmul fast double %conv2, %conv1
171  %sub = fsub fast double %conv, %mul
172  store double %sub, double* %b, align 8
173  %cmp25 = icmp sgt i32 %n, 1
174  br i1 %cmp25, label %for.preheader, label %for.end
175
176for.preheader:
177  br label %scalar.body
178
179scalar.body:
180  %1 = phi i16 [ %0, %for.preheader ], [ %2, %scalar.body ]
181  %advars.iv = phi i64 [ %advars.iv.next, %scalar.body ], [ 1, %for.preheader ]
182  %arrayidx5 = getelementptr inbounds i16, i16* %a, i64 %advars.iv
183  %2 = load i16, i16* %arrayidx5, align 2
184  %conv6 = sitofp i16 %2 to double
185  %conv11 = sitofp i16 %1 to double
186  %mul12 = fmul fast double %conv11, %conv1
187  %sub13 = fsub fast double %conv6, %mul12
188  %arrayidx15 = getelementptr inbounds double, double* %b, i64 %advars.iv
189  store double %sub13, double* %arrayidx15, align 8
190  %advars.iv.next = add nuw nsw i64 %advars.iv, 1
191  %lftr.wideiv = trunc i64 %advars.iv.next to i32
192  %exitcond = icmp eq i32 %lftr.wideiv, %n
193  br i1 %exitcond, label %for.end.loopexit, label %scalar.body
194
195for.end.loopexit:
196  br label %for.end
197
198for.end:
199  ret void
200}
201
202; void PR26734(short *a, int *b, int *c, int d, short *e) {
203;   for (; d != 21; d++) {
204;     *b &= *c;
205;     *e = *a - 6;
206;     *c = *e;
207;   }
208; }
209;
210; CHECK-LABEL: @PR26734(
211; CHECK-NOT:   vector.ph:
212; CHECK:       }
213;
214define void @PR26734(i16* %a, i32* %b, i32* %c, i32 %d, i16* %e) {
215entry:
216  %cmp4 = icmp eq i32 %d, 21
217  br i1 %cmp4, label %entry.for.end_crit_edge, label %for.body.lr.ph
218
219entry.for.end_crit_edge:
220  %.pre = load i32, i32* %b, align 4
221  br label %for.end
222
223for.body.lr.ph:
224  %0 = load i16, i16* %a, align 2
225  %sub = add i16 %0, -6
226  %conv2 = sext i16 %sub to i32
227  %c.promoted = load i32, i32* %c, align 4
228  %b.promoted = load i32, i32* %b, align 4
229  br label %for.body
230
231for.body:
232  %inc7 = phi i32 [ %d, %for.body.lr.ph ], [ %inc, %for.body ]
233  %and6 = phi i32 [ %b.promoted, %for.body.lr.ph ], [ %and, %for.body ]
234  %conv25 = phi i32 [ %c.promoted, %for.body.lr.ph ], [ %conv2, %for.body ]
235  %and = and i32 %and6, %conv25
236  %inc = add nsw i32 %inc7, 1
237  %cmp = icmp eq i32 %inc, 21
238  br i1 %cmp, label %for.cond.for.end_crit_edge, label %for.body
239
240for.cond.for.end_crit_edge:
241  %and.lcssa = phi i32 [ %and, %for.body ]
242  store i32 %conv2, i32* %c, align 4
243  store i32 %and.lcssa, i32* %b, align 4
244  store i16 %sub, i16* %e, align 2
245  br label %for.end
246
247for.end:
248  ret void
249}
250
251; int PR27246() {
252;   unsigned int e, n;
253;   for (int i = 1; i < 49; ++i) {
254;     for (int k = i; k > 1; --k)
255;       e = k;
256;     n = e;
257;   }
258;   return n;
259; }
260;
261; CHECK-LABEL: @PR27246(
262; CHECK-NOT:   vector.ph:
263; CHECK:       }
264;
265define i32 @PR27246() {
266entry:
267  br label %for.cond1.preheader
268
269for.cond1.preheader:
270  %i.016 = phi i32 [ 1, %entry ], [ %inc, %for.cond.cleanup3 ]
271  %e.015 = phi i32 [ poison, %entry ], [ %e.1.lcssa, %for.cond.cleanup3 ]
272  br label %for.cond1
273
274for.cond.cleanup:
275  %e.1.lcssa.lcssa = phi i32 [ %e.1.lcssa, %for.cond.cleanup3 ]
276  ret i32 %e.1.lcssa.lcssa
277
278for.cond1:
279  %e.1 = phi i32 [ %k.0, %for.cond1 ], [ %e.015, %for.cond1.preheader ]
280  %k.0 = phi i32 [ %dec, %for.cond1 ], [ %i.016, %for.cond1.preheader ]
281  %cmp2 = icmp sgt i32 %k.0, 1
282  %dec = add nsw i32 %k.0, -1
283  br i1 %cmp2, label %for.cond1, label %for.cond.cleanup3
284
285for.cond.cleanup3:
286  %e.1.lcssa = phi i32 [ %e.1, %for.cond1 ]
287  %inc = add nuw nsw i32 %i.016, 1
288  %exitcond = icmp eq i32 %inc, 49
289  br i1 %exitcond, label %for.cond.cleanup, label %for.cond1.preheader
290}
291
292; UNROLL-NO-IC-LABEL: @PR30183(
293; UNROLL-NO-IC:       vector.ph:
294; UNROLL-NO-IC:         [[VECTOR_RECUR_INIT:%.*]] = insertelement <4 x i32> poison, i32 [[PRE_LOAD:%.*]], i32 3
295; UNROLL-NO-IC-NEXT:    br label %vector.body
296; UNROLL-NO-IC:       vector.body:
297; UNROLL-NO-IC-NEXT:    [[INDEX:%.*]] = phi i64 [ 0, %vector.ph ], [ [[INDEX_NEXT:%.*]], %vector.body ]
298; UNROLL-NO-IC-NEXT:    [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ [[VECTOR_RECUR_INIT]], %vector.ph ], [ [[TMP42:%.*]], %vector.body ]
299; UNROLL-NO-IC:         [[TMP27:%.*]] = load i32, i32* {{.*}}
300; UNROLL-NO-IC-NEXT:    [[TMP28:%.*]] = load i32, i32* {{.*}}
301; UNROLL-NO-IC-NEXT:    [[TMP29:%.*]] = load i32, i32* {{.*}}
302; UNROLL-NO-IC-NEXT:    [[TMP30:%.*]] = load i32, i32* {{.*}}
303; UNROLL-NO-IC-NEXT:    [[TMP35:%.*]] = insertelement <4 x i32> poison, i32 [[TMP27]], i32 0
304; UNROLL-NO-IC-NEXT:    [[TMP36:%.*]] = insertelement <4 x i32> [[TMP35]], i32 [[TMP28]], i32 1
305; UNROLL-NO-IC-NEXT:    [[TMP37:%.*]] = insertelement <4 x i32> [[TMP36]], i32 [[TMP29]], i32 2
306; UNROLL-NO-IC-NEXT:    [[TMP38:%.*]] = insertelement <4 x i32> [[TMP37]], i32 [[TMP30]], i32 3
307; UNROLL-NO-IC-NEXT:    [[TMP31:%.*]] = load i32, i32* {{.*}}
308; UNROLL-NO-IC-NEXT:    [[TMP32:%.*]] = load i32, i32* {{.*}}
309; UNROLL-NO-IC-NEXT:    [[TMP33:%.*]] = load i32, i32* {{.*}}
310; UNROLL-NO-IC-NEXT:    [[TMP34:%.*]] = load i32, i32* {{.*}}
311; UNROLL-NO-IC-NEXT:    [[TMP39:%.*]] = insertelement <4 x i32> poison, i32 [[TMP31]], i32 0
312; UNROLL-NO-IC-NEXT:    [[TMP40:%.*]] = insertelement <4 x i32> [[TMP39]], i32 [[TMP32]], i32 1
313; UNROLL-NO-IC-NEXT:    [[TMP41:%.*]] = insertelement <4 x i32> [[TMP40]], i32 [[TMP33]], i32 2
314; UNROLL-NO-IC-NEXT:    [[TMP42]] = insertelement <4 x i32> [[TMP41]], i32 [[TMP34]], i32 3
315; UNROLL-NO-IC-NEXT:    [[TMP43:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[TMP38]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
316; UNROLL-NO-IC-NEXT:    [[TMP44:%.*]] = shufflevector <4 x i32> [[TMP38]], <4 x i32> [[TMP42]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
317; UNROLL-NO-IC-NEXT:    [[INDEX_NEXT]] = add nuw i64 [[INDEX]], 8
318; UNROLL-NO-IC:         br i1 {{.*}}, label %middle.block, label %vector.body
319;
320define void @PR30183(i32 %pre_load, i32* %a, i32* %b, i64 %n) {
321entry:
322  br label %scalar.body
323
324scalar.body:
325  %i = phi i64 [ 0, %entry ], [ %i.next, %scalar.body ]
326  %tmp0 = phi i32 [ %pre_load, %entry ], [ %tmp2, %scalar.body ]
327  %i.next = add nuw nsw i64 %i, 2
328  %tmp1 = getelementptr inbounds i32, i32* %a, i64 %i.next
329  %tmp2 = load i32, i32* %tmp1
330  %cond = icmp eq i64 %i.next,%n
331  br i1 %cond, label %for.end, label %scalar.body
332
333for.end:
334  ret void
335}
336
337; UNROLL-NO-IC-LABEL: @constant_folded_previous_value(
338; UNROLL-NO-IC:       vector.body:
339; UNROLL-NO-IC:         [[VECTOR_RECUR:%.*]] = phi <4 x i64> [ <i64 poison, i64 poison, i64 poison, i64 0>, %vector.ph ], [ <i64 1, i64 1, i64 1, i64 1>, %vector.body ]
340; UNROLL-NO-IC:         [[TMP0:%.*]] = shufflevector <4 x i64> [[VECTOR_RECUR]], <4 x i64> <i64 1, i64 1, i64 1, i64 1>, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
341; CHECK-NO-IC-NEXT:     add nuw i64
342; UNROLL-NO-IC:         br i1 {{.*}}, label %middle.block, label %vector.body
343;
344define void @constant_folded_previous_value() {
345entry:
346  br label %scalar.body
347
348scalar.body:
349  %i = phi i64 [ 0, %entry ], [ %i.next, %scalar.body ]
350  %tmp2 = phi i64 [ 0, %entry ], [ %tmp3, %scalar.body ]
351  %tmp3 = add i64 0, 1
352  %i.next = add nuw nsw i64 %i, 1
353  %cond = icmp eq i64 %i.next, undef
354  br i1 %cond, label %for.end, label %scalar.body
355
356for.end:
357  ret void
358}
359
360; We vectorize this first order recurrence, by generating two
361; extracts for the phi `val.phi` - one at the last index and
362; another at the second last index. We need these 2 extracts because
363; the first order recurrence phi is used outside the loop, so we require the phi
364; itself and not its update (addx).
365; UNROLL-NO-IC-LABEL: extract_second_last_iteration
366; UNROLL-NO-IC: vector.body
367; UNROLL-NO-IC:   %step.add = add <4 x i32> %vec.ind, <i32 4, i32 4, i32 4, i32 4>
368; UNROLL-NO-IC:   %[[L1:.+]] = add <4 x i32> %vec.ind, %broadcast.splat
369; UNROLL-NO-IC:   %[[L2:.+]] = add <4 x i32> %step.add, %broadcast.splat
370; UNROLL-NO-IC:   %index.next = add nuw i32 %index, 8
371; UNROLL-NO-IC:   icmp eq i32 %index.next, 96
372; UNROLL-NO-IC: middle.block
373; UNROLL-NO-IC:   icmp eq i32 96, 96
374; UNROLL-NO-IC:   %vector.recur.extract = extractelement <4 x i32> %[[L2]], i32 3
375; UNROLL-NO-IC:   %vector.recur.extract.for.phi = extractelement <4 x i32> %[[L2]], i32 2
376; UNROLL-NO-IC: for.end
377; UNROLL-NO-IC:   %val.phi.lcssa = phi i32 [ %scalar.recur, %for.body ], [ %vector.recur.extract.for.phi, %middle.block ]
378; Check the case when unrolled but not vectorized.
379; UNROLL-NO-VF-LABEL: extract_second_last_iteration
380; UNROLL-NO-VF: vector.body:
381; UNROLL-NO-VF:   %induction = add i32 %index, 0
382; UNROLL-NO-VF:   %induction1 = add i32 %index, 1
383; UNROLL-NO-VF:   %[[L1:.+]] = add i32 %induction, %x
384; UNROLL-NO-VF:   %[[L2:.+]] = add i32 %induction1, %x
385; UNROLL-NO-VF:   %index.next = add nuw i32 %index, 2
386; UNROLL-NO-VF:   icmp eq i32 %index.next, 96
387; UNROLL-NO-VF: for.end:
388; UNROLL-NO-VF:   %val.phi.lcssa = phi i32 [ %scalar.recur, %for.body ], [ %[[L1]], %middle.block ]
389define i32 @extract_second_last_iteration(i32* %cval, i32 %x)  {
390entry:
391  br label %for.body
392
393for.body:
394  %inc.phi = phi i32 [ 0, %entry ], [ %inc, %for.body ]
395  %val.phi = phi i32 [ 0, %entry ], [ %addx, %for.body ]
396  %inc = add i32 %inc.phi, 1
397  %bc = zext i32 %inc.phi to i64
398  %addx = add i32 %inc.phi, %x
399  %cmp = icmp eq i32 %inc.phi, 95
400  br i1 %cmp, label %for.end, label %for.body
401
402for.end:
403  ret i32 %val.phi
404}
405
406; We vectorize this first order recurrence, with a set of insertelements for
407; each unrolled part. Make sure these insertelements are generated in-order,
408; because the shuffle of the first order recurrence will be added after the
409; insertelement of the last part UF - 1, assuming the latter appears after the
410; insertelements of all other parts.
411;
412; int PR33613(double *b, double j, int d) {
413;   int a = 0;
414;   for(int i = 0; i < 10240; i++, b+=25) {
415;     double f = b[d]; // Scalarize to form insertelements
416;     if (j * f)
417;       a++;
418;     j = f;
419;   }
420;   return a;
421; }
422;
423; UNROLL-NO-IC-LABEL: @PR33613(
424; UNROLL-NO-IC:     vector.body:
425; UNROLL-NO-IC:       [[VECTOR_RECUR:%.*]] = phi <4 x double>
426; UNROLL-NO-IC:       shufflevector <4 x double> [[VECTOR_RECUR]], <4 x double> {{.*}}, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
427; UNROLL-NO-IC-NEXT:  shufflevector <4 x double> {{.*}}, <4 x double> {{.*}}, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
428; UNROLL-NO-IC-NOT:   insertelement <4 x double>
429; UNROLL-NO-IC:     middle.block:
430;
431define i32 @PR33613(double* %b, double %j, i32 %d) {
432entry:
433  %idxprom = sext i32 %d to i64
434  br label %for.body
435
436for.cond.cleanup:
437  %a.1.lcssa = phi i32 [ %a.1, %for.body ]
438  ret i32 %a.1.lcssa
439
440for.body:
441  %b.addr.012 = phi double* [ %b, %entry ], [ %add.ptr, %for.body ]
442  %i.011 = phi i32 [ 0, %entry ], [ %inc1, %for.body ]
443  %a.010 = phi i32 [ 0, %entry ], [ %a.1, %for.body ]
444  %j.addr.09 = phi double [ %j, %entry ], [ %0, %for.body ]
445  %arrayidx = getelementptr inbounds double, double* %b.addr.012, i64 %idxprom
446  %0 = load double, double* %arrayidx, align 8
447  %mul = fmul double %j.addr.09, %0
448  %tobool = fcmp une double %mul, 0.000000e+00
449  %inc = zext i1 %tobool to i32
450  %a.1 = add nsw i32 %a.010, %inc
451  %inc1 = add nuw nsw i32 %i.011, 1
452  %add.ptr = getelementptr inbounds double, double* %b.addr.012, i64 25
453  %exitcond = icmp eq i32 %inc1, 10240
454  br i1 %exitcond, label %for.cond.cleanup, label %for.body
455}
456
457; void sink_after(short *a, int n, int *b) {
458;   for(int i = 0; i < n; i++)
459;     b[i] = (a[i] * a[i + 1]);
460; }
461;
462; SINK-AFTER-LABEL: sink_after
463; Check that the sext sank after the load in the vector loop.
464; SINK-AFTER: vector.body
465; SINK-AFTER:   %vector.recur = phi <4 x i16> [ %vector.recur.init, %vector.ph ], [ %wide.load, %vector.body ]
466; SINK-AFTER:   %wide.load = load <4 x i16>
467; SINK-AFTER:   %[[VSHUF:.+]] = shufflevector <4 x i16> %vector.recur, <4 x i16> %wide.load, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
468; SINK-AFTER:   %[[VCONV:.+]] = sext <4 x i16> %[[VSHUF]] to <4 x i32>
469; SINK-AFTER:   %[[VCONV3:.+]] = sext <4 x i16> %wide.load to <4 x i32>
470; SINK-AFTER:   mul nsw <4 x i32> %[[VCONV3]], %[[VCONV]]
471;
472define void @sink_after(i16* %a, i32* %b, i64 %n) {
473entry:
474  %.pre = load i16, i16* %a
475  br label %for.body
476
477for.body:
478  %0 = phi i16 [ %.pre, %entry ], [ %1, %for.body ]
479  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
480  %conv = sext i16 %0 to i32
481  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
482  %arrayidx2 = getelementptr inbounds i16, i16* %a, i64 %indvars.iv.next
483  %1 = load i16, i16* %arrayidx2
484  %conv3 = sext i16 %1 to i32
485  %mul = mul nsw i32 %conv3, %conv
486  %arrayidx5 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
487  store i32 %mul, i32* %arrayidx5
488  %exitcond = icmp eq i64 %indvars.iv.next, %n
489  br i1 %exitcond, label %for.end, label %for.body
490
491for.end:
492  ret void
493}
494
495; PR34711: given three consecutive instructions such that the first will be
496; widened, the second is a cast that will be widened and needs to sink after the
497; third, and the third is a first-order-recurring load that will be replicated
498; instead of widened. Although the cast and the first instruction will both be
499; widened, and are originally adjacent to each other, make sure the replicated
500; load ends up appearing between them.
501;
502; void PR34711(short[2] *a, int *b, int *c, int n) {
503;   for(int i = 0; i < n; i++) {
504;     c[i] = 7;
505;     b[i] = (a[i][0] * a[i][1]);
506;   }
507; }
508;
509; SINK-AFTER-LABEL: @PR34711
510; Check that the sext sank after the load in the vector loop.
511; SINK-AFTER: vector.body
512; SINK-AFTER:   %vector.recur = phi <4 x i16> [ %vector.recur.init, %vector.ph ], [ {{.*}}, %vector.body ]
513; SINK-AFTER:   %[[VSHUF:.+]] = shufflevector <4 x i16> %vector.recur, <4 x i16> %{{.*}}, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
514; SINK-AFTER:   %[[VCONV:.+]] = sext <4 x i16> %[[VSHUF]] to <4 x i32>
515; SINK-AFTER:   %[[VCONV3:.+]] = sext <4 x i16> {{.*}} to <4 x i32>
516; SINK-AFTER:   mul nsw <4 x i32> %[[VCONV3]], %[[VCONV]]
517;
518define void @PR34711([2 x i16]* %a, i32* %b, i32* %c, i64 %n) {
519entry:
520  %pre.index = getelementptr inbounds [2 x i16], [2 x i16]* %a, i64 0, i64 0
521  %.pre = load i16, i16* %pre.index
522  br label %for.body
523
524for.body:
525  %0 = phi i16 [ %.pre, %entry ], [ %1, %for.body ]
526  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
527  %arraycidx = getelementptr inbounds i32, i32* %c, i64 %indvars.iv
528  %cur.index = getelementptr inbounds [2 x i16], [2 x i16]* %a, i64 %indvars.iv, i64 1
529  store i32 7, i32* %arraycidx   ; 1st instruction, to be widened.
530  %conv = sext i16 %0 to i32     ; 2nd, cast to sink after third.
531  %1 = load i16, i16* %cur.index ; 3rd, first-order-recurring load not widened.
532  %conv3 = sext i16 %1 to i32
533  %mul = mul nsw i32 %conv3, %conv
534  %arrayidx5 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
535  store i32 %mul, i32* %arrayidx5
536  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
537  %exitcond = icmp eq i64 %indvars.iv.next, %n
538  br i1 %exitcond, label %for.end, label %for.body
539
540for.end:
541  ret void
542}
543
544; void no_sink_after(short *a, int n, int *b) {
545;   for(int i = 0; i < n; i++)
546;     b[i] = ((a[i] + 2) * a[i + 1]);
547; }
548;
549; SINK-AFTER-LABEL:  @sink_after_with_multiple_users
550; SINK-AFTER-LABEL:  vector.ph:
551; SINK-AFTER-NEXT:    %n.mod.vf = urem i64 %n, 4
552; SINK-AFTER-NEXT:    %n.vec = sub i64 %n, %n.mod.vf
553; SINK-AFTER-NEXT:    %vector.recur.init = insertelement <4 x i16> poison, i16 %.pre, i32 3
554; SINK-AFTER-NEXT:    br label %vector.body
555
556; SINK-AFTER-LABEL:  vector.body:
557; SINK-AFTER-NEXT:    %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ]
558; SINK-AFTER-NEXT:    %vector.recur = phi <4 x i16> [ %vector.recur.init, %vector.ph ], [ %wide.load, %vector.body ]
559; SINK-AFTER-NEXT:    %1 = add i64 %index, 0
560; SINK-AFTER-NEXT:    %2 = add nuw nsw i64 %1, 1
561; SINK-AFTER-NEXT:    %3 = getelementptr inbounds i16, i16* %a, i64 %2
562; SINK-AFTER-NEXT:    %4 = getelementptr inbounds i16, i16* %3, i32 0
563; SINK-AFTER-NEXT:    %5 = bitcast i16* %4 to <4 x i16>*
564; SINK-AFTER-NEXT:    %wide.load = load <4 x i16>, <4 x i16>* %5, align 2, !alias.scope !43
565; SINK-AFTER-NEXT:    %6 = shufflevector <4 x i16> %vector.recur, <4 x i16> %wide.load, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
566; SINK-AFTER-NEXT:    %7 = sext <4 x i16> %6 to <4 x i32>
567; SINK-AFTER-NEXT:    %8 = add nsw <4 x i32> %7, <i32 2, i32 2, i32 2, i32 2>
568; SINK-AFTER-NEXT:    %9 = sext <4 x i16> %wide.load to <4 x i32>
569; SINK-AFTER-NEXT:    %10 = mul nsw <4 x i32> %8, %9
570; SINK-AFTER-NEXT:    %11 = getelementptr inbounds i32, i32* %b, i64 %1
571; SINK-AFTER-NEXT:    %12 = getelementptr inbounds i32, i32* %11, i32 0
572; SINK-AFTER-NEXT:    %13 = bitcast i32* %12 to <4 x i32>*
573; SINK-AFTER-NEXT:    store <4 x i32> %10, <4 x i32>* %13, align 4, !alias.scope !46, !noalias !43
574; SINK-AFTER-NEXT:    %index.next = add nuw i64 %index, 4
575; SINK-AFTER-NEXT:    %14 = icmp eq i64 %index.next, %n.vec
576; SINK-AFTER-NEXT:    br i1 %14, label %middle.block, label %vector.body, !llvm.loop !48
577;
578define void @sink_after_with_multiple_users(i16* %a, i32* %b, i64 %n) {
579entry:
580  %.pre = load i16, i16* %a
581  br label %for.body
582
583for.body:
584  %0 = phi i16 [ %.pre, %entry ], [ %1, %for.body ]
585  %indvars.iv = phi i64 [ 0, %entry ], [ %indvars.iv.next, %for.body ]
586  %conv = sext i16 %0 to i32
587  %add = add nsw i32 %conv, 2
588  %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1
589  %arrayidx2 = getelementptr inbounds i16, i16* %a, i64 %indvars.iv.next
590  %1 = load i16, i16* %arrayidx2
591  %conv3 = sext i16 %1 to i32
592  %mul = mul nsw i32 %add, %conv3
593  %arrayidx5 = getelementptr inbounds i32, i32* %b, i64 %indvars.iv
594  store i32 %mul, i32* %arrayidx5
595  %exitcond = icmp eq i64 %indvars.iv.next, %n
596  br i1 %exitcond, label %for.end, label %for.body
597
598for.end:
599  ret void
600}
601
602; Do not sink branches: While branches are if-converted and do not require
603; sinking, instructions with side effects (e.g. loads) conditioned by those
604; branches will become users of the condition bit after vectorization and would
605; need to be sunk if the loop is vectorized.
606define void @do_not_sink_branch(i32 %x, i32* %in, i32* %out, i32 %tc) local_unnamed_addr #0 {
607; NO-SINK-AFTER-LABEL: do_not_sink_branch
608; NO-SINK-AFTER-NOT:   vector.ph:
609; NO-SINK-AFTER:       }
610entry:
611  %cmp530 = icmp slt i32 0, %tc
612  br label %for.body4
613
614for.body4:                                        ; preds = %cond.end, %entry
615  %indvars.iv = phi i32 [ 0, %entry ], [ %indvars.iv.next, %cond.end ]
616  %cmp534 = phi i1 [ %cmp530, %entry ], [ %cmp5, %cond.end ]
617  br i1 %cmp534, label %cond.true, label %cond.end
618
619cond.true:                                        ; preds = %for.body4
620  %arrayidx7 = getelementptr inbounds i32, i32* %in, i32 %indvars.iv
621  %in.val = load i32, i32* %arrayidx7, align 4
622  br label %cond.end
623
624cond.end:                                         ; preds = %for.body4, %cond.true
625  %cond = phi i32 [ %in.val, %cond.true ], [ 0, %for.body4 ]
626  %arrayidx8 = getelementptr inbounds i32, i32* %out, i32 %indvars.iv
627  store i32 %cond, i32* %arrayidx8, align 4
628  %indvars.iv.next = add nuw nsw i32 %indvars.iv, 1
629  %cmp5 = icmp slt i32 %indvars.iv.next, %tc
630  %exitcond = icmp eq i32 %indvars.iv.next, %x
631  br i1 %exitcond, label %for.end12.loopexit, label %for.body4
632
633for.end12.loopexit:                               ; preds = %cond.end
634  ret void
635}
636
637; Dead instructions, like the exit condition are not part of the actual VPlan
638; and do not need to be sunk. PR44634.
639define void @sink_dead_inst() {
640; SINK-AFTER-LABEL: define void @sink_dead_inst(
641; SINK-AFTER-LABEL: vector.body:                                      ; preds = %vector.body, %vector.ph
642; SINK-AFTER-NEXT:    %index = phi i32 [ 0, %vector.ph ], [ %index.next, %vector.body ]
643; SINK-AFTER-NEXT:    %vec.ind = phi <4 x i16> [ <i16 -27, i16 -26, i16 -25, i16 -24>, %vector.ph ], [ %vec.ind.next, %vector.body ]
644; SINK-AFTER-NEXT:    %vector.recur = phi <4 x i16> [ <i16 poison, i16 poison, i16 poison, i16 0>, %vector.ph ], [ %3, %vector.body ]
645; SINK-AFTER-NEXT:    %vector.recur1 = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 -27>, %vector.ph ], [ %1, %vector.body ]
646; SINK-AFTER-NEXT:    %0 = add <4 x i16> %vec.ind, <i16 1, i16 1, i16 1, i16 1>
647; SINK-AFTER-NEXT:    %1 = zext <4 x i16> %0 to <4 x i32>
648; SINK-AFTER-NEXT:    %2 = shufflevector <4 x i32> %vector.recur1, <4 x i32> %1, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
649; SINK-AFTER-NEXT:    %3 = add <4 x i16> %0, <i16 5, i16 5, i16 5, i16 5>
650; SINK-AFTER-NEXT:    %4 = shufflevector <4 x i16> %vector.recur, <4 x i16> %3, <4 x i32> <i32 3, i32 4, i32 5, i32 6>
651; SINK-AFTER-NEXT:    %5 = sub <4 x i16> %4, <i16 10, i16 10, i16 10, i16 10>
652; SINK-AFTER-NEXT:    %index.next = add nuw i32 %index, 4
653; SINK-AFTER-NEXT:    %vec.ind.next = add <4 x i16> %vec.ind, <i16 4, i16 4, i16 4, i16 4>
654; SINK-AFTER-NEXT:    %6 = icmp eq i32 %index.next, 40
655; SINK-AFTER-NEXT:    br i1 %6, label %middle.block, label %vector.body, !llvm.loop !50
656;
657entry:
658  br label %for.cond
659
660for.cond:
661  %iv = phi i16 [ -27, %entry ], [ %iv.next, %for.cond ]
662  %rec.1 = phi i16 [ 0, %entry ], [ %rec.1.prev, %for.cond ]
663  %rec.2 = phi i32 [ -27, %entry ], [ %rec.2.prev, %for.cond ]
664  %use.rec.1 = sub i16 %rec.1, 10
665  %cmp = icmp eq i32 %rec.2, 15
666  %iv.next = add i16 %iv, 1
667  %rec.2.prev = zext i16 %iv.next to i32
668  %rec.1.prev = add i16 %iv.next, 5
669  br i1 %cmp, label %for.end, label %for.cond
670
671for.end:
672  ret void
673}
674
675define i32 @sink_into_replication_region(i32 %y) {
676; CHECK-LABEL: @sink_into_replication_region(
677; CHECK-NEXT:  bb:
678; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[Y:%.*]], 1
679; CHECK-NEXT:    [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y]], i32 1)
680; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 [[TMP0]], [[SMIN]]
681; CHECK-NEXT:    br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
682; CHECK:       vector.ph:
683; CHECK-NEXT:    [[N_RND_UP:%.*]] = add i32 [[TMP1]], 3
684; CHECK-NEXT:    [[N_VEC:%.*]] = and i32 [[N_RND_UP]], -4
685; CHECK-NEXT:    [[TRIP_COUNT_MINUS_1:%.*]] = add i32 [[TMP1]], -1
686; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TRIP_COUNT_MINUS_1]], i32 0
687; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
688; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
689; CHECK:       vector.body:
690; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_UDIV_CONTINUE9:%.*]] ]
691; CHECK-NEXT:    [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, [[VECTOR_PH]] ], [ [[TMP21:%.*]], [[PRED_UDIV_CONTINUE9]] ]
692; CHECK-NEXT:    [[VEC_PHI1:%.*]] = phi <4 x i32> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP23:%.*]], [[PRED_UDIV_CONTINUE9]] ]
693; CHECK-NEXT:    [[OFFSET_IDX:%.*]] = sub i32 [[Y]], [[INDEX]]
694; CHECK-NEXT:    [[BROADCAST_SPLATINSERT2:%.*]] = insertelement <4 x i32> poison, i32 [[INDEX]], i32 0
695; CHECK-NEXT:    [[BROADCAST_SPLAT3:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT2]], <4 x i32> poison, <4 x i32> zeroinitializer
696; CHECK-NEXT:    [[VEC_IV:%.*]] = or <4 x i32> [[BROADCAST_SPLAT3]], <i32 0, i32 1, i32 2, i32 3>
697; CHECK-NEXT:    [[TMP2:%.*]] = icmp ule <4 x i32> [[VEC_IV]], [[BROADCAST_SPLAT]]
698; CHECK-NEXT:    [[TMP3:%.*]] = extractelement <4 x i1> [[TMP2]], i32 0
699; CHECK-NEXT:    br i1 [[TMP3]], label [[PRED_UDIV_IF:%.*]], label [[PRED_UDIV_CONTINUE:%.*]]
700; CHECK:       pred.udiv.if:
701; CHECK-NEXT:    [[TMP4:%.*]] = udiv i32 219220132, [[OFFSET_IDX]]
702; CHECK-NEXT:    [[TMP5:%.*]] = insertelement <4 x i32> poison, i32 [[TMP4]], i32 0
703; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE]]
704; CHECK:       pred.udiv.continue:
705; CHECK-NEXT:    [[TMP6:%.*]] = phi <4 x i32> [ poison, [[VECTOR_BODY]] ], [ [[TMP5]], [[PRED_UDIV_IF]] ]
706; CHECK-NEXT:    [[TMP7:%.*]] = extractelement <4 x i1> [[TMP2]], i32 1
707; CHECK-NEXT:    br i1 [[TMP7]], label [[PRED_UDIV_IF4:%.*]], label [[PRED_UDIV_CONTINUE5:%.*]]
708; CHECK:       pred.udiv.if3:
709; CHECK-NEXT:    [[TMP8:%.*]] = add i32 [[OFFSET_IDX]], -1
710; CHECK-NEXT:    [[TMP9:%.*]] = udiv i32 219220132, [[TMP8]]
711; CHECK-NEXT:    [[TMP10:%.*]] = insertelement <4 x i32> [[TMP6]], i32 [[TMP9]], i32 1
712; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE5]]
713; CHECK:       pred.udiv.continue4:
714; CHECK-NEXT:    [[TMP11:%.*]] = phi <4 x i32> [ [[TMP6]], [[PRED_UDIV_CONTINUE]] ], [ [[TMP10]], [[PRED_UDIV_IF4]] ]
715; CHECK-NEXT:    [[TMP12:%.*]] = extractelement <4 x i1> [[TMP2]], i32 2
716; CHECK-NEXT:    br i1 [[TMP12]], label [[PRED_UDIV_IF6:%.*]], label [[PRED_UDIV_CONTINUE7:%.*]]
717; CHECK:       pred.udiv.if5:
718; CHECK-NEXT:    [[TMP13:%.*]] = add i32 [[OFFSET_IDX]], -2
719; CHECK-NEXT:    [[TMP14:%.*]] = udiv i32 219220132, [[TMP13]]
720; CHECK-NEXT:    [[TMP15:%.*]] = insertelement <4 x i32> [[TMP11]], i32 [[TMP14]], i32 2
721; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE7]]
722; CHECK:       pred.udiv.continue6:
723; CHECK-NEXT:    [[TMP16:%.*]] = phi <4 x i32> [ [[TMP11]], [[PRED_UDIV_CONTINUE5]] ], [ [[TMP15]], [[PRED_UDIV_IF6]] ]
724; CHECK-NEXT:    [[TMP17:%.*]] = extractelement <4 x i1> [[TMP2]], i32 3
725; CHECK-NEXT:    br i1 [[TMP17]], label [[PRED_UDIV_IF8:%.*]], label [[PRED_UDIV_CONTINUE9]]
726; CHECK:       pred.udiv.if7:
727; CHECK-NEXT:    [[TMP18:%.*]] = add i32 [[OFFSET_IDX]], -3
728; CHECK-NEXT:    [[TMP19:%.*]] = udiv i32 219220132, [[TMP18]]
729; CHECK-NEXT:    [[TMP20:%.*]] = insertelement <4 x i32> [[TMP16]], i32 [[TMP19]], i32 3
730; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE9]]
731; CHECK:       pred.udiv.continue8:
732; CHECK-NEXT:    [[TMP21]] = phi <4 x i32> [ [[TMP16]], [[PRED_UDIV_CONTINUE7]] ], [ [[TMP20]], [[PRED_UDIV_IF8]] ]
733; CHECK-NEXT:    [[TMP22:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[TMP21]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
734; CHECK-NEXT:    [[TMP23]] = add <4 x i32> [[VEC_PHI1]], [[TMP22]]
735; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
736; CHECK-NEXT:    [[TMP24:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
737; CHECK-NEXT:    br i1 [[TMP24]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !prof !52, [[LOOP46:!llvm.loop !.*]]
738; CHECK:       middle.block:
739; CHECK-NEXT:    [[TMP25:%.*]] = select <4 x i1> [[TMP2]], <4 x i32> [[TMP23]], <4 x i32> [[VEC_PHI1]]
740; CHECK-NEXT:    [[TMP26:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP25]])
741; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[SCALAR_PH]]
742; CHECK:       scalar.ph:
743; CHECK-NEXT:    br label [[BB2:%.*]]
744; CHECK:       bb1:
745; CHECK-NEXT:    [[TMP:%.*]] = phi i32 [ undef, [[BB2]] ], [ [[TMP26]], [[MIDDLE_BLOCK]] ]
746; CHECK-NEXT:    ret i32 [[TMP]]
747; CHECK:       bb2:
748; CHECK-NEXT:    br i1 undef, label [[BB1]], label [[BB2]], !prof !54, [[LOOP48:!llvm.loop !.*]]
749;
750bb:
751  br label %bb2
752
753  bb1:                                              ; preds = %bb2
754  %tmp = phi i32 [ %tmp6, %bb2 ]
755  ret i32 %tmp
756
757  bb2:                                              ; preds = %bb2, %bb
758  %tmp3 = phi i32 [ %tmp8, %bb2 ], [ %y, %bb ]
759  %tmp4 = phi i32 [ %tmp7, %bb2 ], [ 0, %bb ]
760  %tmp5 = phi i32 [ %tmp6, %bb2 ], [ 0, %bb ]
761  %tmp6 = add i32 %tmp5, %tmp4
762  %tmp7 = udiv i32 219220132, %tmp3
763  %tmp8 = add nsw i32 %tmp3, -1
764  %tmp9 = icmp slt i32 %tmp3, 2
765  br i1 %tmp9, label %bb1, label %bb2, !prof !2
766}
767
768define i32 @sink_into_replication_region_multiple(i32 *%x, i32 %y) {
769; CHECK-LABEL: @sink_into_replication_region_multiple(
770; CHECK-NEXT:  bb:
771; CHECK-NEXT:    [[TMP0:%.*]] = add i32 [[Y:%.*]], 1
772; CHECK-NEXT:    [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[Y]], i32 1)
773; CHECK-NEXT:    [[TMP1:%.*]] = sub i32 [[TMP0]], [[SMIN]]
774; CHECK-NEXT:    br i1 false, label [[SCALAR_PH:%.*]], label [[VECTOR_PH:%.*]]
775; CHECK:       vector.ph:
776; CHECK-NEXT:    [[N_RND_UP:%.*]] = add i32 [[TMP1]], 3
777; CHECK-NEXT:    [[N_VEC:%.*]] = and i32 [[N_RND_UP]], -4
778; CHECK-NEXT:    [[TRIP_COUNT_MINUS_1:%.*]] = add i32 [[TMP1]], -1
779; CHECK-NEXT:    [[BROADCAST_SPLATINSERT:%.*]] = insertelement <4 x i32> poison, i32 [[TRIP_COUNT_MINUS_1]], i32 0
780; CHECK-NEXT:    [[BROADCAST_SPLAT:%.*]] = shufflevector <4 x i32> [[BROADCAST_SPLATINSERT]], <4 x i32> poison, <4 x i32> zeroinitializer
781; CHECK-NEXT:    br label [[VECTOR_BODY:%.*]]
782; CHECK:       vector.body:
783; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, [[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], [[PRED_STORE_CONTINUE16:%.*]] ]
784; CHECK-NEXT:    [[VEC_IND2:%.*]] = phi <4 x i32> [ <i32 0, i32 1, i32 2, i32 3>, [[VECTOR_PH]] ], [ [[VEC_IND_NEXT3:%.*]], [[PRED_STORE_CONTINUE16]] ]
785; CHECK-NEXT:    [[VECTOR_RECUR:%.*]] = phi <4 x i32> [ <i32 poison, i32 poison, i32 poison, i32 0>, [[VECTOR_PH]] ], [ [[TMP21:%.*]], [[PRED_STORE_CONTINUE16]] ]
786; CHECK-NEXT:    [[VEC_PHI4:%.*]] = phi <4 x i32> [ zeroinitializer, [[VECTOR_PH]] ], [ [[TMP23:%.*]], [[PRED_STORE_CONTINUE16]] ]
787; CHECK-NEXT:    [[OFFSET_IDX:%.*]] = sub i32 [[Y]], [[INDEX]]
788; CHECK-NEXT:    [[TMP2:%.*]] = add i32 [[OFFSET_IDX]], -1
789; CHECK-NEXT:    [[TMP3:%.*]] = add i32 [[OFFSET_IDX]], -2
790; CHECK-NEXT:    [[TMP4:%.*]] = add i32 [[OFFSET_IDX]], -3
791; CHECK-NEXT:    [[TMP5:%.*]] = icmp ule <4 x i32> [[VEC_IND2]], [[BROADCAST_SPLAT]]
792; CHECK-NEXT:    [[TMP6:%.*]] = extractelement <4 x i1> [[TMP5]], i32 0
793; CHECK-NEXT:    br i1 [[TMP6]], label [[PRED_UDIV_IF:%.*]], label [[PRED_UDIV_CONTINUE:%.*]]
794; CHECK:       pred.udiv.if:
795; CHECK-NEXT:    [[TMP7:%.*]] = udiv i32 219220132, [[OFFSET_IDX]]
796; CHECK-NEXT:    [[TMP8:%.*]] = insertelement <4 x i32> poison, i32 [[TMP7]], i32 0
797; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE]]
798; CHECK:       pred.udiv.continue:
799; CHECK-NEXT:    [[TMP9:%.*]] = phi <4 x i32> [ poison, [[VECTOR_BODY]] ], [ [[TMP8]], [[PRED_UDIV_IF]] ]
800; CHECK-NEXT:    [[TMP10:%.*]] = extractelement <4 x i1> [[TMP5]], i32 1
801; CHECK-NEXT:    br i1 [[TMP10]], label [[PRED_UDIV_IF5:%.*]], label [[PRED_UDIV_CONTINUE6:%.*]]
802; CHECK:       pred.udiv.if4:
803; CHECK-NEXT:    [[TMP11:%.*]] = udiv i32 219220132, [[TMP2]]
804; CHECK-NEXT:    [[TMP12:%.*]] = insertelement <4 x i32> [[TMP9]], i32 [[TMP11]], i32 1
805; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE6]]
806; CHECK:       pred.udiv.continue5:
807; CHECK-NEXT:    [[TMP13:%.*]] = phi <4 x i32> [ [[TMP9]], [[PRED_UDIV_CONTINUE]] ], [ [[TMP12]], [[PRED_UDIV_IF5]] ]
808; CHECK-NEXT:    [[TMP14:%.*]] = extractelement <4 x i1> [[TMP5]], i32 2
809; CHECK-NEXT:    br i1 [[TMP14]], label [[PRED_UDIV_IF7:%.*]], label [[PRED_UDIV_CONTINUE8:%.*]]
810; CHECK:       pred.udiv.if6:
811; CHECK-NEXT:    [[TMP15:%.*]] = udiv i32 219220132, [[TMP3]]
812; CHECK-NEXT:    [[TMP16:%.*]] = insertelement <4 x i32> [[TMP13]], i32 [[TMP15]], i32 2
813; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE8]]
814; CHECK:       pred.udiv.continue7:
815; CHECK-NEXT:    [[TMP17:%.*]] = phi <4 x i32> [ [[TMP13]], [[PRED_UDIV_CONTINUE6]] ], [ [[TMP16]], [[PRED_UDIV_IF7]] ]
816; CHECK-NEXT:    [[TMP18:%.*]] = extractelement <4 x i1> [[TMP5]], i32 3
817; CHECK-NEXT:    br i1 [[TMP18]], label [[PRED_UDIV_IF9:%.*]], label [[PRED_UDIV_CONTINUE10:%.*]]
818; CHECK:       pred.udiv.if8:
819; CHECK-NEXT:    [[TMP19:%.*]] = udiv i32 219220132, [[TMP4]]
820; CHECK-NEXT:    [[TMP20:%.*]] = insertelement <4 x i32> [[TMP17]], i32 [[TMP19]], i32 3
821; CHECK-NEXT:    br label [[PRED_UDIV_CONTINUE10]]
822; CHECK:       pred.udiv.continue9:
823; CHECK-NEXT:    [[TMP21]] = phi <4 x i32> [ [[TMP17]], [[PRED_UDIV_CONTINUE8]] ], [ [[TMP20]], [[PRED_UDIV_IF9]] ]
824; CHECK-NEXT:    [[TMP22:%.*]] = shufflevector <4 x i32> [[VECTOR_RECUR]], <4 x i32> [[TMP21]], <4 x i32> <i32 3, i32 4, i32 5, i32 6>
825; CHECK-NEXT:    [[TMP23]] = add <4 x i32> [[VEC_PHI4]], [[TMP22]]
826; CHECK-NEXT:    [[TMP24:%.*]] = extractelement <4 x i1> [[TMP5]], i32 0
827; CHECK-NEXT:    br i1 [[TMP24]], label [[PRED_STORE_IF:%.*]], label [[PRED_STORE_CONTINUE:%.*]]
828; CHECK:       pred.store.if:
829; CHECK-NEXT:    [[TMP25:%.*]] = sext i32 [[INDEX]] to i64
830; CHECK-NEXT:    [[TMP26:%.*]] = getelementptr inbounds i32, i32* [[X:%.*]], i64 [[TMP25]]
831; CHECK-NEXT:    store i32 [[OFFSET_IDX]], i32* [[TMP26]], align 4
832; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE]]
833; CHECK:       pred.store.continue:
834; CHECK-NEXT:    [[TMP27:%.*]] = extractelement <4 x i1> [[TMP5]], i32 1
835; CHECK-NEXT:    br i1 [[TMP27]], label [[PRED_STORE_IF11:%.*]], label [[PRED_STORE_CONTINUE12:%.*]]
836; CHECK:       pred.store.if10:
837; CHECK-NEXT:    [[TMP28:%.*]] = or i32 [[INDEX]], 1
838; CHECK-NEXT:    [[TMP29:%.*]] = sext i32 [[TMP28]] to i64
839; CHECK-NEXT:    [[TMP30:%.*]] = getelementptr inbounds i32, i32* [[X]], i64 [[TMP29]]
840; CHECK-NEXT:    store i32 [[TMP2]], i32* [[TMP30]], align 4
841; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE12]]
842; CHECK:       pred.store.continue11:
843; CHECK-NEXT:    [[TMP31:%.*]] = extractelement <4 x i1> [[TMP5]], i32 2
844; CHECK-NEXT:    br i1 [[TMP31]], label [[PRED_STORE_IF13:%.*]], label [[PRED_STORE_CONTINUE14:%.*]]
845; CHECK:       pred.store.if12:
846; CHECK-NEXT:    [[TMP32:%.*]] = or i32 [[INDEX]], 2
847; CHECK-NEXT:    [[TMP33:%.*]] = sext i32 [[TMP32]] to i64
848; CHECK-NEXT:    [[TMP34:%.*]] = getelementptr inbounds i32, i32* [[X]], i64 [[TMP33]]
849; CHECK-NEXT:    store i32 [[TMP3]], i32* [[TMP34]], align 4
850; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE14]]
851; CHECK:       pred.store.continue13:
852; CHECK-NEXT:    [[TMP35:%.*]] = extractelement <4 x i1> [[TMP5]], i32 3
853; CHECK-NEXT:    br i1 [[TMP35]], label [[PRED_STORE_IF15:%.*]], label [[PRED_STORE_CONTINUE16]]
854; CHECK:       pred.store.if14:
855; CHECK-NEXT:    [[TMP36:%.*]] = or i32 [[INDEX]], 3
856; CHECK-NEXT:    [[TMP37:%.*]] = sext i32 [[TMP36]] to i64
857; CHECK-NEXT:    [[TMP38:%.*]] = getelementptr inbounds i32, i32* [[X]], i64 [[TMP37]]
858; CHECK-NEXT:    store i32 [[TMP4]], i32* [[TMP38]], align 4
859; CHECK-NEXT:    br label [[PRED_STORE_CONTINUE16]]
860; CHECK:       pred.store.continue15:
861; CHECK-NEXT:    [[INDEX_NEXT]] = add i32 [[INDEX]], 4
862; CHECK-NEXT:    [[VEC_IND_NEXT3]] = add <4 x i32> [[VEC_IND2]], <i32 4, i32 4, i32 4, i32 4>
863; CHECK-NEXT:    [[TMP39:%.*]] = icmp eq i32 [[INDEX_NEXT]], [[N_VEC]]
864; CHECK-NEXT:    br i1 [[TMP39]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !prof !52, [[LOOP49:!llvm.loop !.*]]
865; CHECK:       middle.block:
866; CHECK-NEXT:    [[TMP40:%.*]] = select <4 x i1> [[TMP5]], <4 x i32> [[TMP23]], <4 x i32> [[VEC_PHI4]]
867; CHECK-NEXT:    [[TMP41:%.*]] = call i32 @llvm.vector.reduce.add.v4i32(<4 x i32> [[TMP40]])
868; CHECK-NEXT:    br i1 true, label [[BB1:%.*]], label [[SCALAR_PH]]
869; CHECK:       scalar.ph:
870; CHECK-NEXT:    br label [[BB2:%.*]]
871; CHECK:       bb1:
872; CHECK-NEXT:    [[TMP:%.*]] = phi i32 [ undef, [[BB2]] ], [ [[TMP41]], [[MIDDLE_BLOCK]] ]
873; CHECK-NEXT:    ret i32 [[TMP]]
874; CHECK:       bb2:
875; CHECK-NEXT:    br i1 undef, label [[BB1]], label [[BB2]], !prof !54, [[LOOP50:!llvm.loop !.*]]
876;
877bb:
878  br label %bb2
879
880  bb1:                                              ; preds = %bb2
881  %tmp = phi i32 [ %tmp6, %bb2 ]
882  ret i32 %tmp
883
884  bb2:                                              ; preds = %bb2, %bb
885  %tmp3 = phi i32 [ %tmp8, %bb2 ], [ %y, %bb ]
886  %iv = phi i32 [ %iv.next, %bb2 ], [ 0, %bb ]
887  %tmp4 = phi i32 [ %tmp7, %bb2 ], [ 0, %bb ]
888  %tmp5 = phi i32 [ %tmp6, %bb2 ], [ 0, %bb ]
889  %g = getelementptr inbounds i32, i32* %x, i32 %iv
890  %tmp6 = add i32 %tmp5, %tmp4
891  %tmp7 = udiv i32 219220132, %tmp3
892  store i32 %tmp3, i32* %g, align 4
893  %tmp8 = add nsw i32 %tmp3, -1
894  %iv.next = add nsw i32 %iv, 1
895  %tmp9 = icmp slt i32 %tmp3, 2
896  br i1 %tmp9, label %bb1, label %bb2, !prof !2
897}
898
899; %vec.dead will be marked as dead instruction in the vector loop and no recipe
900; will be created for it. Make sure a valid sink target is used.
901define void @sink_after_dead_inst(i32* %A.ptr) {
902; CHECK-LABEL: @sink_after_dead_inst
903; CHECK-LABEL: vector.body:
904; CHECK-NEXT:    [[INDEX:%.*]] = phi i32 [ 0, %vector.ph ], [ [[INDEX_NEXT]], %vector.body ]
905; CHECK-NEXT:    [[OFFSET_IDX:%.*]] = zext i32 [[INDEX]] to i64
906; CHECK-NEXT:    [[SEXT:%.*]] = shl i64 [[OFFSET_IDX]], 48
907; CHECK-NEXT:    [[SHIFT:%.*]] = ashr exact i64 [[SEXT]], 48
908; CHECK-NEXT:    [[GEP:%.*]] = getelementptr i32, i32* %A.ptr, i64 [[SHIFT]]
909; CHECK-NEXT:    [[CAST:%.*]] = bitcast i32* [[GEP]]  to <4 x i32>*
910; CHECK-NEXT:    store <4 x i32> zeroinitializer, <4 x i32>* [[CAST]], align 4
911; CHECK-NEXT:    [[INDEX_NEXT:%.*]] = add nuw i32 [[INDEX]], 4
912; CHECK-NEXT:    [[EC:%.*]] = icmp eq i32 [[INDEX_NEXT]], 16
913; CHECK-NEXT:    br i1 [[EC]], label %middle.block, label %vector.body
914
915entry:
916  br label %loop
917
918loop:
919  %iv = phi i16 [ 0, %entry ], [ %iv.next, %loop ]
920  %for = phi i32 [ 0, %entry ], [ %for.prev, %loop ]
921  %cmp = icmp eq i32 %for, 15
922  %C = icmp eq i1 %cmp, true
923  %vec.dead = and i1 %C, 1
924  %iv.next = add i16 %iv, 1
925  %B1 = or i16 %iv.next, %iv.next
926  %B3 = and i1 %cmp, %C
927  %for.prev = zext i16 %B1 to i32
928
929  %ext = zext i1 %B3 to i32
930  %A.gep = getelementptr i32, i32* %A.ptr, i16 %iv
931  store i32 0, i32* %A.gep
932  br i1 %vec.dead, label %for.end, label %loop
933
934for.end:
935  ret void
936}
937
938!2 = !{!"branch_weights", i32 1, i32 1}
939