1 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2 ; RUN: opt %s -passes=loop-vectorize -hoist-runtime-checks=false -force-vector-width=4 -force-vector-interleave=1 -S | FileCheck %s
4 target datalayout = "e-m:e-i64:64-i128:128-n32:64-S128"
6 define void @same_step_and_size(ptr %a, i32* %b, i64 %n) {
7 ; CHECK-LABEL: @same_step_and_size(
9 ; CHECK-NEXT: [[A2:%.*]] = ptrtoint ptr [[A:%.*]] to i64
10 ; CHECK-NEXT: [[B1:%.*]] = ptrtoint ptr [[B:%.*]] to i64
11 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
12 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
13 ; CHECK: vector.memcheck:
14 ; CHECK-NEXT: [[TMP0:%.*]] = sub i64 [[B1]], [[A2]]
15 ; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP0]], 16
16 ; CHECK-NEXT: br i1 [[DIFF_CHECK]], label %scalar.ph, label %vector.ph
22 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
23 %gep.a = getelementptr inbounds i32, ptr %a, i64 %iv
24 %l = load i32, ptr %gep.a
25 %mul = mul nsw i32 %l, 3
26 %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
27 store i32 %mul, ptr %gep.b
28 %iv.next = add nuw nsw i64 %iv, 1
29 %exitcond = icmp eq i64 %iv.next, %n
30 br i1 %exitcond, label %exit, label %loop
36 define void @same_step_and_size_no_dominance_between_accesses(ptr %a, ptr %b, i64 %n, i64 %x) {
37 ; CHECK-LABEL: @same_step_and_size_no_dominance_between_accesses(
39 ; CHECK-NEXT: [[B2:%.*]] = ptrtoint ptr [[B:%.*]] to i64
40 ; CHECK-NEXT: [[A1:%.*]] = ptrtoint ptr [[A:%.*]] to i64
41 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
42 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
43 ; CHECK: vector.memcheck:
44 ; CHECK-NEXT: [[TMP0:%.*]] = sub i64 [[A1]], [[B2]]
45 ; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP0]], 16
46 ; CHECK-NEXT: br i1 [[DIFF_CHECK]], label %scalar.ph, label %vector.ph
52 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]
53 %cmp = icmp ne i64 %iv, %x
54 br i1 %cmp, label %then, label %else
57 %gep.a = getelementptr inbounds i32, ptr %a, i64 %iv
58 store i32 0, ptr %gep.a
62 %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
63 store i32 10, ptr %gep.b
67 %iv.next = add nuw nsw i64 %iv, 1
68 %exitcond = icmp eq i64 %iv.next, %n
69 br i1 %exitcond, label %exit, label %loop
75 define void @different_steps_and_different_access_sizes(ptr %a, ptr %b, i64 %n) {
76 ; CHECK-LABEL: @different_steps_and_different_access_sizes(
78 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
79 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
80 ; CHECK: vector.memcheck:
81 ; CHECK-NEXT: [[N_SHL_2:%.]] = shl i64 %n, 2
82 ; CHECK-NEXT: [[SCEVGEP:%.*]] = getelementptr i8, ptr %b, i64 [[N_SHL_2]]
83 ; CHECK-NEXT: [[N_SHL_1:%.]] = shl i64 %n, 1
84 ; CHECK-NEXT: [[SCEVGEP4:%.*]] = getelementptr i8, ptr %a, i64 [[N_SHL_1]]
85 ; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr %b, [[SCEVGEP4]]
86 ; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr %a, [[SCEVGEP]]
87 ; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
88 ; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %scalar.ph, label %vector.ph
94 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
95 %gep.a = getelementptr inbounds i16, ptr %a, i64 %iv
96 %l = load i16, ptr %gep.a
97 %l.ext = sext i16 %l to i32
98 %mul = mul nsw i32 %l.ext, 3
99 %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
100 store i32 %mul, ptr %gep.b
101 %iv.next = add nuw nsw i64 %iv, 1
102 %exitcond = icmp eq i64 %iv.next, %n
103 br i1 %exitcond, label %exit, label %loop
109 define void @steps_match_but_different_access_sizes_1(ptr %a, ptr %b, i64 %n) {
110 ; CHECK-LABEL: @steps_match_but_different_access_sizes_1(
112 ; CHECK-NEXT: [[A2:%.*]] = ptrtoint ptr [[A:%.*]] to i64
113 ; CHECK-NEXT: [[B1:%.*]] = ptrtoint ptr [[B:%.*]] to i64
114 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
115 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
116 ; CHECK: vector.memcheck:
117 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[B1]], -2
118 ; CHECK-NEXT: [[TMP1:%.*]] = sub i64 [[TMP0]], [[A2]]
119 ; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP1]], 16
120 ; CHECK-NEXT: br i1 [[DIFF_CHECK]], label %scalar.ph, label %vector.ph
126 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
127 %gep.a = getelementptr inbounds [2 x i16], ptr %a, i64 %iv, i64 1
128 %l = load i16, ptr %gep.a
129 %l.ext = sext i16 %l to i32
130 %mul = mul nsw i32 %l.ext, 3
131 %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
132 store i32 %mul, ptr %gep.b
133 %iv.next = add nuw nsw i64 %iv, 1
134 %exitcond = icmp eq i64 %iv.next, %n
135 br i1 %exitcond, label %exit, label %loop
141 ; Same as @steps_match_but_different_access_sizes_1, but with source and sink
143 define void @steps_match_but_different_access_sizes_2(ptr %a, ptr %b, i64 %n) {
144 ; CHECK-LABEL: @steps_match_but_different_access_sizes_2(
146 ; CHECK-NEXT: [[B2:%.*]] = ptrtoint ptr [[B:%.*]] to i64
147 ; CHECK-NEXT: [[A1:%.*]] = ptrtoint ptr [[A:%.*]] to i64
148 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
149 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
150 ; CHECK: vector.memcheck:
151 ; CHECK-NEXT: [[TMP0:%.*]] = add i64 [[A1]], 2
152 ; CHECK-NEXT: [[TMP1:%.*]] = sub i64 [[TMP0]], [[B2]]
153 ; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[TMP1]], 16
154 ; CHECK-NEXT: br i1 [[DIFF_CHECK]], label %scalar.ph, label %vector.ph
160 %iv = phi i64 [ 0, %entry ], [ %iv.next, %loop ]
161 %gep.b = getelementptr inbounds i32, ptr %b, i64 %iv
162 %l = load i32, ptr %gep.b
163 %mul = mul nsw i32 %l, 3
164 %gep.a = getelementptr inbounds [2 x i16], ptr %a, i64 %iv, i64 1
165 %trunc = trunc i32 %mul to i16
166 store i16 %trunc, ptr %gep.a
167 %iv.next = add nuw nsw i64 %iv, 1
168 %exitcond = icmp eq i64 %iv.next, %n
169 br i1 %exitcond, label %exit, label %loop
175 ; Full no-overlap checks are required instead of difference checks, as
176 ; one of the add-recs used is invariant in the inner loop.
177 ; Test case for PR57315.
178 define void @nested_loop_outer_iv_addrec_invariant_in_inner1(ptr %a, ptr %b, i64 %n) {
179 ; CHECK-LABEL: @nested_loop_outer_iv_addrec_invariant_in_inner1(
181 ; CHECK-NEXT: [[N_SHL_2:%.]] = shl i64 %n, 2
182 ; CHECK-NEXT: [[B_GEP_UPPER:%.*]] = getelementptr i8, ptr %b, i64 [[N_SHL_2]]
183 ; CHECK-NEXT: br label %outer
185 ; CHECK: outer.header:
186 ; CHECK: [[OUTER_IV_SHL_2:%.]] = shl i64 %outer.iv, 2
187 ; CHECK-NEXT: [[A_GEP_UPPER:%.*]] = getelementptr i8, ptr %a, i64 [[OUTER_IV_SHL_2]]
188 ; CHECK-NEXT: [[OUTER_IV_4:%.]] = add i64 [[OUTER_IV_SHL_2]], 4
189 ; CHECK-NEXT: [[A_GEP_UPPER_4:%.*]] = getelementptr i8, ptr %a, i64 [[OUTER_IV_4]]
190 ; CHECK: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
191 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
193 ; CHECK: vector.memcheck:
194 ; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr [[A_GEP_UPPER]], [[B_GEP_UPPER]]
195 ; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr %b, [[A_GEP_UPPER_4]]
196 ; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
197 ; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %scalar.ph, label %vector.ph
200 br label %outer.header
203 %outer.iv = phi i64 [ %outer.iv.next, %outer.latch ], [ 0, %entry ]
204 %gep.a = getelementptr inbounds i32, ptr %a, i64 %outer.iv
208 %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
209 %gep.b = getelementptr inbounds i32, ptr %b, i64 %inner.iv
210 %l = load i32, ptr %gep.b, align 4
211 %sub = sub i32 %l, 10
212 store i32 %sub, ptr %gep.a, align 4
213 %inner.iv.next = add nuw nsw i64 %inner.iv, 1
214 %inner.cond = icmp eq i64 %inner.iv.next, %n
215 br i1 %inner.cond, label %outer.latch, label %inner.body
218 %outer.iv.next = add nuw nsw i64 %outer.iv, 1
219 %outer.cond = icmp eq i64 %outer.iv.next, %n
220 br i1 %outer.cond, label %exit, label %outer.header
226 ; Same as @nested_loop_outer_iv_addrec_invariant_in_inner1 but with dependence
227 ; sink and source swapped.
228 define void @nested_loop_outer_iv_addrec_invariant_in_inner2(ptr %a, ptr %b, i64 %n) {
229 ; CHECK-LABEL: @nested_loop_outer_iv_addrec_invariant_in_inner2(
231 ; CHECK-NEXT: [[N_SHL_2:%.]] = shl i64 %n, 2
232 ; CHECK-NEXT: [[B_GEP_UPPER:%.*]] = getelementptr i8, ptr %b, i64 [[N_SHL_2]]
233 ; CHECK-NEXT: br label %outer
235 ; CHECK: outer.header:
236 ; CHECK: [[OUTER_IV_SHL_2:%.]] = shl i64 %outer.iv, 2
237 ; CHECK-NEXT: [[A_GEP_UPPER:%.*]] = getelementptr i8, ptr %a, i64 [[OUTER_IV_SHL_2]]
238 ; CHECK-NEXT: [[OUTER_IV_4:%.]] = add i64 [[OUTER_IV_SHL_2]], 4
239 ; CHECK-NEXT: [[A_GEP_UPPER_4:%.*]] = getelementptr i8, ptr %a, i64 [[OUTER_IV_4]]
240 ; CHECK: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N:%.*]], 4
241 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %scalar.ph, label %vector.memcheck
243 ; CHECK: vector.memcheck:
244 ; CHECK-NEXT: [[BOUND0:%.*]] = icmp ult ptr %b, [[A_GEP_UPPER_4]]
245 ; CHECK-NEXT: [[BOUND1:%.*]] = icmp ult ptr [[A_GEP_UPPER]], [[B_GEP_UPPER]]
246 ; CHECK-NEXT: [[FOUND_CONFLICT:%.*]] = and i1 [[BOUND0]], [[BOUND1]]
247 ; CHECK-NEXT: br i1 [[FOUND_CONFLICT]], label %scalar.ph, label %vector.ph
250 br label %outer.header
253 %outer.iv = phi i64 [ %outer.iv.next, %outer.latch ], [ 0, %entry ]
254 %gep.a = getelementptr inbounds i32, ptr %a, i64 %outer.iv
258 %inner.iv = phi i64 [ 0, %outer.header ], [ %inner.iv.next, %inner.body ]
259 %l = load i32, ptr %gep.a, align 4
260 %sub = sub i32 %l, 10
261 %gep.b = getelementptr inbounds i32, ptr %b, i64 %inner.iv
262 store i32 %sub, ptr %gep.b, align 4
263 %inner.iv.next = add nuw nsw i64 %inner.iv, 1
264 %inner.cond = icmp eq i64 %inner.iv.next, %n
265 br i1 %inner.cond, label %outer.latch, label %inner.body
268 %outer.iv.next = add nuw nsw i64 %outer.iv, 1
269 %outer.cond = icmp eq i64 %outer.iv.next, %n
270 br i1 %outer.cond, label %exit, label %outer.header
276 ; Test case where the AddRec for the pointers in the inner loop have the AddRec
277 ; of the outer loop as start value. It is sufficient to subtract the start
278 ; values (%dst, %src) of the outer AddRecs.
279 define void @nested_loop_start_of_inner_ptr_addrec_is_same_outer_addrec(ptr nocapture noundef %dst, ptr nocapture noundef readonly %src, i64 noundef %m, i64 noundef %n) {
280 ; CHECK-LABEL: @nested_loop_start_of_inner_ptr_addrec_is_same_outer_addrec(
282 ; CHECK-NEXT: [[SRC2:%.*]] = ptrtoint ptr [[SRC:%.*]] to i64
283 ; CHECK-NEXT: [[DST1:%.*]] = ptrtoint ptr [[DST:%.*]] to i64
284 ; CHECK-NEXT: [[SUB:%.*]] = sub i64 [[DST1]], [[SRC2]]
285 ; CHECK-NEXT: br label [[OUTER_LOOP:%.*]]
287 ; CHECK-NEXT: [[OUTER_IV:%.*]] = phi i64 [ 0, [[ENTRY:%.*]] ], [ [[OUTER_IV_NEXT:%.*]], [[INNER_EXIT:%.*]] ]
288 ; CHECK-NEXT: [[MUL:%.*]] = mul nsw i64 [[OUTER_IV]], [[N]]
289 ; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 [[N]], 4
290 ; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label [[SCALAR_PH:%.*]], label [[VECTOR_MEMCHECK:%.*]]
291 ; CHECK: vector.memcheck:
292 ; CHECK-NEXT: [[DIFF_CHECK:%.*]] = icmp ult i64 [[SUB]], 16
293 ; CHECK-NEXT: br i1 [[DIFF_CHECK]], label [[SCALAR_PH]], label [[VECTOR_PH:%.*]]
299 %outer.iv = phi i64 [ 0, %entry ], [ %outer.iv.next, %inner.exit ]
300 %mul = mul nsw i64 %outer.iv, %n
304 %iv.inner = phi i64 [ 0, %outer.loop ], [ %iv.inner.next, %inner.loop ]
305 %idx = add nuw nsw i64 %iv.inner, %mul
306 %gep.src = getelementptr inbounds i32, ptr %src, i64 %idx
307 %l = load i32, ptr %gep.src, align 4
308 %gep.dst = getelementptr inbounds i32, ptr %dst, i64 %idx
309 %add = add nsw i32 %l, 10
310 store i32 %add, ptr %gep.dst, align 4
311 %iv.inner.next = add nuw nsw i64 %iv.inner, 1
312 %inner.exit.cond = icmp eq i64 %iv.inner.next, %n
313 br i1 %inner.exit.cond, label %inner.exit, label %inner.loop
316 %outer.iv.next = add nuw nsw i64 %outer.iv, 1
317 %outer.exit.cond = icmp eq i64 %outer.iv.next, %m
318 br i1 %outer.exit.cond, label %outer.exit, label %outer.loop