1 ; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
2 ; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" -scalar-evolution-classify-expressions=0 2>&1 | FileCheck %s
4 ; A collection of tests that show we can use facts about an exit test to
5 ; infer tighter bounds on an IV, and thus refine an IV into an addrec. The
6 ; basic tactic being used is proving NW from exit structure and then
7 ; implying NUW/NSW. Once NSW/NUW is inferred, we can derive addrecs from
8 ; the zext/sext cases that we couldn't at initial SCEV construction.
10 @G = external global i8
12 define void @nw_implies_nuw(i16 %n) mustprogress {
13 ; CHECK-LABEL: 'nw_implies_nuw'
14 ; CHECK-NEXT: Determining loop execution counts for: @nw_implies_nuw
15 ; CHECK-NEXT: Loop %for.body: backedge-taken count is %n
16 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is -1
17 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is %n
18 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is %n
19 ; CHECK-NEXT: Predicates:
20 ; CHECK: Loop %for.body: Trip multiple is 1
25 for.body: ; preds = %entry, %for.body
26 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
27 %iv.next = add i8 %iv, 1
28 %zext = zext i8 %iv to i16
29 %cmp = icmp ult i16 %zext, %n
30 br i1 %cmp, label %for.body, label %for.end
32 for.end: ; preds = %for.body, %entry
36 define void @neg_nw_nuw(i16 %n) mustprogress {
37 ; CHECK-LABEL: 'neg_nw_nuw'
38 ; CHECK-NEXT: Determining loop execution counts for: @neg_nw_nuw
39 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
40 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
41 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
42 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
47 for.body: ; preds = %entry, %for.body
48 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
49 %iv.next = add i8 %iv, -1
50 %zext = zext i8 %iv to i16
51 %cmp = icmp ult i16 %zext, %n
52 br i1 %cmp, label %for.body, label %for.end
54 for.end: ; preds = %for.body, %entry
58 define void @nw_implies_nsw(i16 %n) mustprogress {
59 ; CHECK-LABEL: 'nw_implies_nsw'
60 ; CHECK-NEXT: Determining loop execution counts for: @nw_implies_nsw
61 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
62 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
63 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
64 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (128 + (-128 smax %n))
65 ; CHECK-NEXT: Predicates:
66 ; CHECK-NEXT: {-128,+,1}<%for.body> Added Flags: <nssw>
71 for.body: ; preds = %entry, %for.body
72 %iv = phi i8 [ %iv.next, %for.body ], [ -128, %entry ]
73 %iv.next = add i8 %iv, 1
74 %zext = sext i8 %iv to i16
75 %cmp = icmp slt i16 %zext, %n
76 br i1 %cmp, label %for.body, label %for.end
78 for.end: ; preds = %for.body, %entry
82 define void @neg_nw_nsw(i16 %n) mustprogress {
83 ; CHECK-LABEL: 'neg_nw_nsw'
84 ; CHECK-NEXT: Determining loop execution counts for: @neg_nw_nsw
85 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
86 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
87 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
88 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
93 for.body: ; preds = %entry, %for.body
94 %iv = phi i8 [ %iv.next, %for.body ], [ -128, %entry ]
95 %iv.next = add i8 %iv, -1
96 %zext = sext i8 %iv to i16
97 %cmp = icmp slt i16 %zext, %n
98 br i1 %cmp, label %for.body, label %for.end
100 for.end: ; preds = %for.body, %entry
105 define void @actually_infinite() {
106 ; CHECK-LABEL: 'actually_infinite'
107 ; CHECK-NEXT: Determining loop execution counts for: @actually_infinite
108 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
109 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
110 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
111 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is 257
112 ; CHECK-NEXT: Predicates:
113 ; CHECK-NEXT: {0,+,1}<%for.body> Added Flags: <nusw>
118 for.body: ; preds = %entry, %for.body
119 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
120 store volatile i8 %iv, ptr @G
121 %iv.next = add i8 %iv, 1
122 %zext = zext i8 %iv to i16
123 %cmp = icmp ult i16 %zext, 257
124 br i1 %cmp, label %for.body, label %for.end
126 for.end: ; preds = %for.body, %entry
130 define void @rhs_mustexit_1(i16 %n.raw) mustprogress {
131 ; CHECK-LABEL: 'rhs_mustexit_1'
132 ; CHECK-NEXT: Determining loop execution counts for: @rhs_mustexit_1
133 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
134 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
135 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
136 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (-1 + (1 umax (-1 + (zext i8 (trunc i16 %n.raw to i8) to i16))<nsw>))
137 ; CHECK-NEXT: Predicates:
138 ; CHECK-NEXT: {1,+,1}<nw><%for.body> Added Flags: <nusw>
141 %n.and = and i16 %n.raw, 255
142 %n = add nsw i16 %n.and, -1
145 for.body: ; preds = %entry, %for.body
146 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
147 %iv.next = add i8 %iv, 1
149 %zext = zext i8 %iv.next to i16
150 %cmp = icmp ult i16 %zext, %n
151 br i1 %cmp, label %for.body, label %for.end
153 for.end: ; preds = %for.body, %entry
157 define void @rhs_mustexit_3(i16 %n.raw) mustprogress {
158 ; CHECK-LABEL: 'rhs_mustexit_3'
159 ; CHECK-NEXT: Determining loop execution counts for: @rhs_mustexit_3
160 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
161 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
162 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
163 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
166 %n.and = and i16 %n.raw, 255
167 %n = add nsw i16 %n.and, -3
170 for.body: ; preds = %entry, %for.body
171 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
172 %iv.next = add i8 %iv, 3
174 %zext = zext i8 %iv.next to i16
175 %cmp = icmp ult i16 %zext, %n
176 br i1 %cmp, label %for.body, label %for.end
178 for.end: ; preds = %for.body, %entry
182 ; Unknown, but non-zero step
183 define void @rhs_mustexit_nonzero_step(i16 %n.raw, i8 %step.raw) mustprogress {
184 ; CHECK-LABEL: 'rhs_mustexit_nonzero_step'
185 ; CHECK-NEXT: Determining loop execution counts for: @rhs_mustexit_nonzero_step
186 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
187 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
188 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
189 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
192 %n.and = and i16 %n.raw, 255
193 %n = add nsw i16 %n.and, -3
194 %step = add nuw i8 %step.raw, 1
197 for.body: ; preds = %entry, %for.body
198 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
199 %iv.next = add i8 %iv, %step
201 %zext = zext i8 %iv.next to i16
202 %cmp = icmp ult i16 %zext, %n
203 br i1 %cmp, label %for.body, label %for.end
205 for.end: ; preds = %for.body, %entry
209 define void @neg_maybe_zero_step(i16 %n.raw, i8 %step) mustprogress {
210 ; CHECK-LABEL: 'neg_maybe_zero_step'
211 ; CHECK-NEXT: Determining loop execution counts for: @neg_maybe_zero_step
212 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
213 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
214 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
215 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
218 %n.and = and i16 %n.raw, 255
219 %n = add nsw i16 %n.and, -3
222 for.body: ; preds = %entry, %for.body
223 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
224 %iv.next = add i8 %iv, %step
226 %zext = zext i8 %iv.next to i16
227 %cmp = icmp ult i16 %zext, %n
228 br i1 %cmp, label %for.body, label %for.end
230 for.end: ; preds = %for.body, %entry
234 define void @neg_rhs_wrong_range(i16 %n.raw) mustprogress {
235 ; CHECK-LABEL: 'neg_rhs_wrong_range'
236 ; CHECK-NEXT: Determining loop execution counts for: @neg_rhs_wrong_range
237 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
238 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
239 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
240 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((-1 + (2 umax (-1 + (zext i8 (trunc i16 %n.raw to i8) to i16))<nsw>)) /u 2)
241 ; CHECK-NEXT: Predicates:
242 ; CHECK-NEXT: {2,+,2}<nw><%for.body> Added Flags: <nusw>
245 %n.and = and i16 %n.raw, 255
246 %n = add nsw i16 %n.and, -1
249 for.body: ; preds = %entry, %for.body
250 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
251 %iv.next = add i8 %iv, 2
253 %zext = zext i8 %iv.next to i16
254 %cmp = icmp ult i16 %zext, %n
255 br i1 %cmp, label %for.body, label %for.end
257 for.end: ; preds = %for.body, %entry
261 define void @neg_rhs_maybe_infinite(i16 %n.raw) {
262 ; CHECK-LABEL: 'neg_rhs_maybe_infinite'
263 ; CHECK-NEXT: Determining loop execution counts for: @neg_rhs_maybe_infinite
264 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
265 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
266 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
267 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (-1 + (1 umax (-1 + (zext i8 (trunc i16 %n.raw to i8) to i16))<nsw>))
268 ; CHECK-NEXT: Predicates:
269 ; CHECK-NEXT: {1,+,1}<%for.body> Added Flags: <nusw>
272 %n.and = and i16 %n.raw, 255
273 %n = add nsw i16 %n.and, -1
276 for.body: ; preds = %entry, %for.body
277 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
278 %iv.next = add i8 %iv, 1
280 %zext = zext i8 %iv.next to i16
281 %cmp = icmp ult i16 %zext, %n
282 br i1 %cmp, label %for.body, label %for.end
284 for.end: ; preds = %for.body, %entry
288 ; Because of the range on RHS including only values within i8, we don't need
289 ; the must exit property
290 define void @rhs_narrow_range(i16 %n.raw) {
291 ; CHECK-LABEL: 'rhs_narrow_range'
292 ; CHECK-NEXT: Determining loop execution counts for: @rhs_narrow_range
293 ; CHECK-NEXT: Loop %for.body: backedge-taken count is (-1 + (1 umax (2 * (zext i7 (trunc i16 (%n.raw /u 2) to i7) to i16))<nuw><nsw>))<nsw>
294 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is 253
295 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (-1 + (1 umax (2 * (zext i7 (trunc i16 (%n.raw /u 2) to i7) to i16))<nuw><nsw>))<nsw>
296 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (-1 + (1 umax (2 * (zext i7 (trunc i16 (%n.raw /u 2) to i7) to i16))<nuw><nsw>))<nsw>
297 ; CHECK-NEXT: Predicates:
298 ; CHECK: Loop %for.body: Trip multiple is 1
301 %n = and i16 %n.raw, 254
304 for.body: ; preds = %entry, %for.body
305 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
306 %iv.next = add i8 %iv, 1
308 %zext = zext i8 %iv.next to i16
309 %cmp = icmp ult i16 %zext, %n
310 br i1 %cmp, label %for.body, label %for.end
312 for.end: ; preds = %for.body, %entry
316 define void @ugt_constant_rhs(i16 %n.raw, i8 %start) mustprogress {
318 ; CHECK-LABEL: 'ugt_constant_rhs'
319 ; CHECK-NEXT: Determining loop execution counts for: @ugt_constant_rhs
320 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
321 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
322 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
323 ; CHECK-NEXT: Loop %for.body: Unpredictable predicated backedge-taken count.
328 for.body: ; preds = %entry, %for.body
329 %iv = phi i8 [ %iv.next, %for.body ], [ %start, %entry ]
330 %iv.next = add i8 %iv, 1
331 %zext = zext i8 %iv.next to i16
332 %cmp = icmp ugt i16 %zext, 254
333 br i1 %cmp, label %for.body, label %for.end
335 for.end: ; preds = %for.body, %entry
339 define void @ult_constant_rhs(i16 %n.raw, i8 %start) {
341 ; CHECK-LABEL: 'ult_constant_rhs'
342 ; CHECK-NEXT: Determining loop execution counts for: @ult_constant_rhs
343 ; CHECK-NEXT: Loop %for.body: backedge-taken count is (255 + (-1 * (zext i8 (1 + %start) to i16))<nsw>)<nsw>
344 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is 255
345 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (255 + (-1 * (zext i8 (1 + %start) to i16))<nsw>)<nsw>
346 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (255 + (-1 * (zext i8 (1 + %start) to i16))<nsw>)<nsw>
347 ; CHECK-NEXT: Predicates:
348 ; CHECK: Loop %for.body: Trip multiple is 1
353 for.body: ; preds = %entry, %for.body
354 %iv = phi i8 [ %iv.next, %for.body ], [ %start, %entry ]
355 %iv.next = add i8 %iv, 1
356 %zext = zext i8 %iv.next to i16
357 %cmp = icmp ult i16 %zext, 255
358 br i1 %cmp, label %for.body, label %for.end
360 for.end: ; preds = %for.body, %entry
364 define void @ult_constant_rhs_stride2(i16 %n.raw, i8 %start) {
366 ; CHECK-LABEL: 'ult_constant_rhs_stride2'
367 ; CHECK-NEXT: Determining loop execution counts for: @ult_constant_rhs_stride2
368 ; CHECK-NEXT: Loop %for.body: backedge-taken count is ((1 + (-1 * (zext i8 (2 + %start) to i16))<nsw> + (254 umax (zext i8 (2 + %start) to i16))) /u 2)
369 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is 127
370 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is ((1 + (-1 * (zext i8 (2 + %start) to i16))<nsw> + (254 umax (zext i8 (2 + %start) to i16))) /u 2)
371 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((1 + (-1 * (zext i8 (2 + %start) to i16))<nsw> + (254 umax (zext i8 (2 + %start) to i16))) /u 2)
372 ; CHECK-NEXT: Predicates:
373 ; CHECK: Loop %for.body: Trip multiple is 1
378 for.body: ; preds = %entry, %for.body
379 %iv = phi i8 [ %iv.next, %for.body ], [ %start, %entry ]
380 %iv.next = add i8 %iv, 2
381 %zext = zext i8 %iv.next to i16
382 %cmp = icmp ult i16 %zext, 254
383 br i1 %cmp, label %for.body, label %for.end
385 for.end: ; preds = %for.body, %entry
389 define void @ult_constant_rhs_stride2_neg(i16 %n.raw, i8 %start) {
391 ; CHECK-LABEL: 'ult_constant_rhs_stride2_neg'
392 ; CHECK-NEXT: Determining loop execution counts for: @ult_constant_rhs_stride2_neg
393 ; CHECK-NEXT: Loop %for.body: Unpredictable backedge-taken count.
394 ; CHECK-NEXT: Loop %for.body: Unpredictable constant max backedge-taken count.
395 ; CHECK-NEXT: Loop %for.body: Unpredictable symbolic max backedge-taken count.
396 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is ((256 + (-1 * (zext i8 (2 + %start) to i16))<nsw>)<nsw> /u 2)
397 ; CHECK-NEXT: Predicates:
398 ; CHECK-NEXT: {(2 + %start),+,2}<%for.body> Added Flags: <nusw>
403 for.body: ; preds = %entry, %for.body
404 %iv = phi i8 [ %iv.next, %for.body ], [ %start, %entry ]
405 %iv.next = add i8 %iv, 2
406 %zext = zext i8 %iv.next to i16
407 %cmp = icmp ult i16 %zext, 255
408 br i1 %cmp, label %for.body, label %for.end
410 for.end: ; preds = %for.body, %entry
415 define void @ult_restricted_rhs(i16 %n.raw) {
416 ; CHECK-LABEL: 'ult_restricted_rhs'
417 ; CHECK-NEXT: Determining loop execution counts for: @ult_restricted_rhs
418 ; CHECK-NEXT: Loop %for.body: backedge-taken count is (-1 + (1 umax (zext i8 (trunc i16 %n.raw to i8) to i16)))<nsw>
419 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is 254
420 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (-1 + (1 umax (zext i8 (trunc i16 %n.raw to i8) to i16)))<nsw>
421 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (-1 + (1 umax (zext i8 (trunc i16 %n.raw to i8) to i16)))<nsw>
422 ; CHECK-NEXT: Predicates:
423 ; CHECK: Loop %for.body: Trip multiple is 1
426 %n = and i16 %n.raw, 255
429 for.body: ; preds = %entry, %for.body
430 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
431 %iv.next = add i8 %iv, 1
432 %zext = zext i8 %iv.next to i16
433 %cmp = icmp ult i16 %zext, %n
434 br i1 %cmp, label %for.body, label %for.end
436 for.end: ; preds = %for.body, %entry
440 define void @ult_guarded_rhs(i16 %n) {;
441 ; CHECK-LABEL: 'ult_guarded_rhs'
442 ; CHECK-NEXT: Determining loop execution counts for: @ult_guarded_rhs
443 ; CHECK-NEXT: Loop %for.body: backedge-taken count is (-1 + (1 umax %n))
444 ; CHECK-NEXT: Loop %for.body: constant max backedge-taken count is -2
445 ; CHECK-NEXT: Loop %for.body: symbolic max backedge-taken count is (-1 + (1 umax %n))
446 ; CHECK-NEXT: Loop %for.body: Predicated backedge-taken count is (-1 + (1 umax %n))
447 ; CHECK-NEXT: Predicates:
448 ; CHECK: Loop %for.body: Trip multiple is 1
451 %in_range = icmp ult i16 %n, 256
452 br i1 %in_range, label %for.body, label %for.end
454 for.body: ; preds = %entry, %for.body
455 %iv = phi i8 [ %iv.next, %for.body ], [ 0, %entry ]
456 %iv.next = add i8 %iv, 1
457 %zext = zext i8 %iv.next to i16
458 %cmp = icmp ult i16 %zext, %n
459 br i1 %cmp, label %for.body, label %for.end
461 for.end: ; preds = %for.body, %entry
467 declare void @llvm.assume(i1)