2 ; RUN: opt < %s -loop-vectorize -force-vector-width=2 -force-vector-interleave=1 -disable-output -debug-only=loop-vectorize 2>&1 | FileCheck %s
4 target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
6 ; Test cases for PR50009, which require sinking a replicate-region due to a
7 ; first-order recurrence.
9 define void @sink_replicate_region_1(i32 %x, i8* %ptr) optsize {
10 ; CHECK-LABEL: sink_replicate_region_1
11 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' {
13 ; CHECK-NEXT: FIRST-ORDER-RECURRENCE-PHI ir<%0> = phi ir<0>, ir<%conv>
14 ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next
15 ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0>
16 ; CHECK-NEXT: Successor(s): loop.0
19 ; CHECK-NEXT: Successor(s): pred.load
21 ; CHECK-NEXT: <xVFxUF> pred.load: {
22 ; CHECK-NEXT: pred.load.entry:
23 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
24 ; CHECK-NEXT: Successor(s): pred.load.if, pred.load.continue
25 ; CHECK-NEXT: CondBit: vp<%3> (loop)
27 ; CHECK-NEXT: pred.load.if:
28 ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, ir<%iv>
29 ; CHECK-NEXT: REPLICATE ir<%lv> = load ir<%gep> (S->V)
30 ; CHECK-NEXT: Successor(s): pred.load.continue
32 ; CHECK-NEXT: pred.load.continue:
33 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%6> = ir<%lv>
34 ; CHECK-NEXT: No successors
36 ; CHECK-NEXT: Successor(s): loop.1
39 ; CHECK-NEXT: WIDEN ir<%conv> = sext vp<%6>
40 ; CHECK-NEXT: EMIT vp<%8> = first-order splice ir<%0> ir<%conv>
41 ; CHECK-NEXT: Successor(s): pred.srem
43 ; CHECK-NEXT: <xVFxUF> pred.srem: {
44 ; CHECK-NEXT: pred.srem.entry:
45 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
46 ; CHECK-NEXT: Successor(s): pred.srem.if, pred.srem.continue
47 ; CHECK-NEXT: CondBit: vp<%3> (loop)
49 ; CHECK-NEXT: pred.srem.if:
50 ; CHECK-NEXT: REPLICATE ir<%rem> = srem vp<%8>, ir<%x> (S->V)
51 ; CHECK-NEXT: Successor(s): pred.srem.continue
53 ; CHECK-NEXT: pred.srem.continue:
54 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%10> = ir<%rem>
55 ; CHECK-NEXT: No successors
57 ; CHECK-NEXT: Successor(s): loop.1.split
59 ; CHECK-NEXT: loop.1.split:
60 ; CHECK-NEXT: WIDEN ir<%add> = add ir<%conv>, vp<%10>
61 ; CHECK-NEXT: No successors
68 %0 = phi i32 [ 0, %entry ], [ %conv, %loop ]
69 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
70 %rem = srem i32 %0, %x
71 %gep = getelementptr i8, i8* %ptr, i32 %iv
72 %lv = load i8, i8* %gep
73 %conv = sext i8 %lv to i32
74 %add = add i32 %conv, %rem
75 %iv.next = add nsw i32 %iv, 1
76 %ec = icmp eq i32 %iv.next, 20001
77 br i1 %ec, label %exit, label %loop
83 define void @sink_replicate_region_2(i32 %x, i8 %y, i32* %ptr) optsize {
84 ; CHECK-LABEL: sink_replicate_region_2
85 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' {
87 ; CHECK-NEXT: FIRST-ORDER-RECURRENCE-PHI ir<%recur> = phi ir<0>, ir<%recur.next>
88 ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next
89 ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0>
90 ; CHECK-NEXT: Successor(s): loop.0
93 ; CHECK-NEXT: WIDEN ir<%recur.next> = sext ir<%y>
94 ; CHECK-NEXT: EMIT vp<%5> = first-order splice ir<%recur> ir<%recur.next>
95 ; CHECK-NEXT: Successor(s): loop.0.split
97 ; CHECK-NEXT: loop.0.split:
98 ; CHECK-NEXT: Successor(s): pred.store
100 ; CHECK-NEXT: <xVFxUF> pred.store: {
101 ; CHECK-NEXT: pred.store.entry:
102 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
103 ; CHECK-NEXT: Successor(s): pred.store.if, pred.store.continue
104 ; CHECK-NEXT: CondBit: vp<%3> (loop)
106 ; CHECK-NEXT: pred.store.if:
107 ; CHECK-NEXT: REPLICATE ir<%rem> = srem vp<%5>, ir<%x>
108 ; CHECK-NEXT: REPLICATE ir<%add> = add ir<%rem>, ir<%recur.next>
109 ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, ir<%iv>
110 ; CHECK-NEXT: REPLICATE store ir<%add>, ir<%gep>
111 ; CHECK-NEXT: Successor(s): pred.store.continue
113 ; CHECK-NEXT: pred.store.continue:
114 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%10> = ir<%rem>
115 ; CHECK-NEXT: No successors
117 ; CHECK-NEXT: Successor(s): loop.1
119 ; CHECK-NEXT: loop.1:
120 ; CHECK-NEXT: No successors
127 %recur = phi i32 [ 0, %entry ], [ %recur.next, %loop ]
128 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
129 %rem = srem i32 %recur, %x
130 %recur.next = sext i8 %y to i32
131 %add = add i32 %rem, %recur.next
132 %gep = getelementptr i32, i32* %ptr, i32 %iv
133 store i32 %add, i32* %gep
134 %iv.next = add nsw i32 %iv, 1
135 %ec = icmp eq i32 %iv.next, 20001
136 br i1 %ec, label %exit, label %loop
142 define i32 @sink_replicate_region_3_reduction(i32 %x, i8 %y, i32* %ptr) optsize {
143 ; CHECK-LABEL: sink_replicate_region_3_reduction
144 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' {
146 ; CHECK-NEXT: FIRST-ORDER-RECURRENCE-PHI ir<%recur> = phi ir<0>, ir<%recur.next>
147 ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next
148 ; CHECK-NEXT: WIDEN-REDUCTION-PHI ir<%and.red> = phi ir<1234>, ir<%and.red.next>
149 ; CHECK-NEXT: EMIT vp<%4> = icmp ule ir<%iv> vp<%0>
150 ; CHECK-NEXT: Successor(s): loop.0
152 ; CHECK-NEXT: loop.0:
153 ; CHECK-NEXT: WIDEN ir<%recur.next> = sext ir<%y>
154 ; CHECK-NEXT: EMIT vp<%6> = first-order splice ir<%recur> ir<%recur.next>
155 ; CHECK-NEXT: Successor(s): pred.srem
157 ; CHECK-NEXT: <xVFxUF> pred.srem: {
158 ; CHECK-NEXT: pred.srem.entry:
159 ; CHECK-NEXT: BRANCH-ON-MASK vp<%4>
160 ; CHECK-NEXT: Successor(s): pred.srem.if, pred.srem.continue
161 ; CHECK-NEXT: CondBit: vp<%4> (loop)
163 ; CHECK-NEXT: pred.srem.if:
164 ; CHECK-NEXT: REPLICATE ir<%rem> = srem vp<%6>, ir<%x> (S->V)
165 ; CHECK-NEXT: Successor(s): pred.srem.continue
167 ; CHECK-NEXT: pred.srem.continue:
168 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%8> = ir<%rem>
169 ; CHECK-NEXT: No successors
171 ; CHECK-NEXT: Successor(s): loop.0.split
173 ; CHECK-NEXT: loop.0.split:
174 ; CHECK-NEXT: WIDEN ir<%add> = add vp<%8>, ir<%recur.next>
175 ; CHECK-NEXT: WIDEN ir<%and.red.next> = and ir<%and.red>, ir<%add>
176 ; CHECK-NEXT: EMIT vp<%11> = select vp<%4> ir<%and.red.next> ir<%and.red>
177 ; CHECK-NEXT: No successors
184 %recur = phi i32 [ 0, %entry ], [ %recur.next, %loop ]
185 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
186 %and.red = phi i32 [ 1234, %entry ], [ %and.red.next, %loop ]
187 %rem = srem i32 %recur, %x
188 %recur.next = sext i8 %y to i32
189 %add = add i32 %rem, %recur.next
190 %and.red.next = and i32 %and.red, %add
191 %iv.next = add nsw i32 %iv, 1
192 %ec = icmp eq i32 %iv.next, 20001
193 br i1 %ec, label %exit, label %loop
196 %res = phi i32 [ %and.red.next, %loop ]
200 ; To sink the replicate region containing %rem, we need to split the block
201 ; containing %conv at the end, because %conv is the last recipe in the block.
202 define void @sink_replicate_region_4_requires_split_at_end_of_block(i32 %x, i8* %ptr) optsize {
203 ; CHECK-LABEL: sink_replicate_region_4_requires_split_at_end_of_block
204 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' {
206 ; CHECK-NEXT: FIRST-ORDER-RECURRENCE-PHI ir<%0> = phi ir<0>, ir<%conv>
207 ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next
208 ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0>
209 ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, ir<%iv>
210 ; CHECK-NEXT: Successor(s): loop.0
212 ; CHECK-NEXT: loop.0:
213 ; CHECK-NEXT: Successor(s): pred.load
215 ; CHECK-NEXT: <xVFxUF> pred.load: {
216 ; CHECK-NEXT: pred.load.entry:
217 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
218 ; CHECK-NEXT: Successor(s): pred.load.if, pred.load.continue
219 ; CHECK-NEXT: CondBit: vp<%3> (loop)
221 ; CHECK-NEXT: pred.load.if:
222 ; CHECK-NEXT: REPLICATE ir<%lv> = load ir<%gep> (S->V)
223 ; CHECK-NEXT: Successor(s): pred.load.continue
225 ; CHECK-NEXT: pred.load.continue:
226 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%6> = ir<%lv>
227 ; CHECK-NEXT: No successors
229 ; CHECK-NEXT: Successor(s): loop.1
231 ; CHECK-NEXT: loop.1:
232 ; CHECK-NEXT: WIDEN ir<%conv> = sext vp<%6>
233 ; CHECK-NEXT: EMIT vp<%8> = first-order splice ir<%0> ir<%conv>
234 ; CHECK-NEXT: Successor(s): loop.1.split
236 ; CHECK: loop.1.split:
237 ; CHECK-NEXT: Successor(s): pred.load
239 ; CHECK: <xVFxUF> pred.load: {
240 ; CHECK-NEXT: pred.load.entry:
241 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
242 ; CHECK-NEXT: Successor(s): pred.load.if, pred.load.continue
243 ; CHECK-NEXT: CondBit: vp<%3> (loop)
245 ; CHECK: pred.load.if:
246 ; CHECK-NEXT: REPLICATE ir<%rem> = srem vp<%8>, ir<%x> (S->V)
247 ; CHECK-NEXT: REPLICATE ir<%lv.2> = load ir<%gep> (S->V)
248 ; CHECK-NEXT: Successor(s): pred.load.continue
250 ; CHECK: pred.load.continue:
251 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%11> = ir<%rem>
252 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%12> = ir<%lv.2>
253 ; CHECK-NEXT: No successors
257 ; CHECK-NEXT: WIDEN ir<%add.1> = add ir<%conv>, vp<%11>
258 ; CHECK-NEXT: WIDEN ir<%conv.lv.2> = sext vp<%12>
259 ; CHECK-NEXT: WIDEN ir<%add> = add ir<%add.1>, ir<%conv.lv.2>
260 ; CHECK-NEXT: No successors
267 %0 = phi i32 [ 0, %entry ], [ %conv, %loop ]
268 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
269 %gep = getelementptr i8, i8* %ptr, i32 %iv
270 %rem = srem i32 %0, %x
271 %lv = load i8, i8* %gep
272 %conv = sext i8 %lv to i32
273 %lv.2 = load i8, i8* %gep
274 %add.1 = add i32 %conv, %rem
275 %conv.lv.2 = sext i8 %lv.2 to i32
276 %add = add i32 %add.1, %conv.lv.2
277 %iv.next = add nsw i32 %iv, 1
278 %ec = icmp eq i32 %iv.next, 20001
279 br i1 %ec, label %exit, label %loop
285 ; Test case that requires sinking a recipe in a replicate region after another replicate region.
286 define void @sink_replicate_region_after_replicate_region(i32* %ptr, i32 %x, i8 %y) optsize {
287 ; CHECK-LABEL: sink_replicate_region_after_replicate_region
288 ; CHECK: VPlan 'Initial VPlan for VF={2},UF>=1' {
290 ; CHECK-NEXT: FIRST-ORDER-RECURRENCE-PHI ir<%recur> = phi ir<0>, ir<%recur.next>
291 ; CHECK-NEXT: WIDEN-INDUCTION %iv = phi 0, %iv.next
292 ; CHECK-NEXT: EMIT vp<%3> = icmp ule ir<%iv> vp<%0>
293 ; CHECK-NEXT: Successor(s): loop.0
295 ; CHECK-NEXT: loop.0:
296 ; CHECK-NEXT: Successor(s): loop.1
298 ; CHECK-NEXT: loop.1:
299 ; CHECK-NEXT: WIDEN ir<%recur.next> = sext ir<%y>
300 ; CHECK-NEXT: EMIT vp<%5> = first-order splice ir<%recur> ir<%recur.next>
301 ; CHECK-NEXT: Successor(s): pred.srem
303 ; CHECK-NEXT: <xVFxUF> pred.srem: {
304 ; CHECK-NEXT: pred.srem.entry:
305 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
306 ; CHECK-NEXT: Successor(s): pred.srem.if, pred.srem.continue
307 ; CHECK-NEXT: CondBit: vp<%3> (loop)
309 ; CHECK-NEXT: pred.srem.if:
310 ; CHECK-NEXT: REPLICATE ir<%rem> = srem vp<%5>, ir<%x>
311 ; CHECK-NEXT: Successor(s): pred.srem.continue
313 ; CHECK-NEXT: pred.srem.continue:
314 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%7> = ir<%rem>
315 ; CHECK-NEXT: No successors
317 ; CHECK-NEXT: Successor(s): loop.1.split
319 ; CHECK-NEXT: loop.1.split:
320 ; CHECK-NEXT: Successor(s): pred.store
322 ; CHECK-NEXT: <xVFxUF> pred.store: {
323 ; CHECK-NEXT: pred.store.entry:
324 ; CHECK-NEXT: BRANCH-ON-MASK vp<%3>
325 ; CHECK-NEXT: Successor(s): pred.store.if, pred.store.continue
326 ; CHECK-NEXT: CondBit: vp<%3> (loop)
328 ; CHECK-NEXT: pred.store.if:
329 ; CHECK-NEXT: REPLICATE ir<%rem.div> = sdiv ir<20>, vp<%7>
330 ; CHECK-NEXT: REPLICATE ir<%gep> = getelementptr ir<%ptr>, ir<%iv>
331 ; CHECK-NEXT: REPLICATE store ir<%rem.div>, ir<%gep>
332 ; CHECK-NEXT: Successor(s): pred.store.continue
334 ; CHECK-NEXT: pred.store.continue:
335 ; CHECK-NEXT: PHI-PREDICATED-INSTRUCTION vp<%11> = ir<%rem.div>
336 ; CHECK-NEXT: No successors
338 ; CHECK-NEXT: Successor(s): loop.2
340 ; CHECK-NEXT: loop.2:
341 ; CHECK-NEXT: No successors
347 loop: ; preds = %loop, %entry
348 %recur = phi i32 [ 0, %entry ], [ %recur.next, %loop ]
349 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
350 %rem = srem i32 %recur, %x
351 %rem.div = sdiv i32 20, %rem
352 %recur.next = sext i8 %y to i32
353 %gep = getelementptr i32, i32* %ptr, i32 %iv
354 store i32 %rem.div, i32* %gep
355 %iv.next = add nsw i32 %iv, 1
356 %C = icmp sgt i32 %iv.next, %recur.next
357 br i1 %C, label %exit, label %loop
359 exit: ; preds = %loop