1 ; RUN: opt -analyze -scalar-evolution -S < %s | FileCheck %s
4 ; - starting at 0, 1, or %x
6 ; - stopping at %n or %n*2
9 ; Some of these represent missed opportunities.
11 ; CHECK: Determining loop execution counts for: @foo
12 ; CHECK: Loop %loop: backedge-taken count is (-1 + %n)
13 ; CHECK: Loop %loop: max backedge-taken count is 6
14 define void @foo(i4 %n) {
16 %s = icmp sgt i4 %n, 0
17 br i1 %s, label %loop, label %exit
19 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
20 %i.next = add i4 %i, 1
21 %t = icmp slt i4 %i.next, %n
22 br i1 %t, label %loop, label %exit
27 ; CHECK: Determining loop execution counts for: @step2
28 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
29 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
30 define void @step2(i4 %n) {
32 %s = icmp sgt i4 %n, 0
33 br i1 %s, label %loop, label %exit
35 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
36 %i.next = add i4 %i, 2
37 %t = icmp slt i4 %i.next, %n
38 br i1 %t, label %loop, label %exit
43 ; CHECK: Determining loop execution counts for: @start1
44 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax %n))
45 ; CHECK: Loop %loop: max backedge-taken count is 5
46 define void @start1(i4 %n) {
48 %s = icmp sgt i4 %n, 0
49 br i1 %s, label %loop, label %exit
51 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
52 %i.next = add i4 %i, 1
53 %t = icmp slt i4 %i.next, %n
54 br i1 %t, label %loop, label %exit
59 ; CHECK: Determining loop execution counts for: @start1_step2
60 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
61 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
62 define void @start1_step2(i4 %n) {
64 %s = icmp sgt i4 %n, 0
65 br i1 %s, label %loop, label %exit
67 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
68 %i.next = add i4 %i, 2
69 %t = icmp slt i4 %i.next, %n
70 br i1 %t, label %loop, label %exit
75 ; CHECK: Determining loop execution counts for: @startx
76 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n))
77 ; CHECK: Loop %loop: max backedge-taken count is -1
78 define void @startx(i4 %n, i4 %x) {
80 %s = icmp sgt i4 %n, 0
81 br i1 %s, label %loop, label %exit
83 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
84 %i.next = add i4 %i, 1
85 %t = icmp slt i4 %i.next, %n
86 br i1 %t, label %loop, label %exit
91 ; CHECK: Determining loop execution counts for: @startx_step2
92 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
93 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
94 define void @startx_step2(i4 %n, i4 %x) {
96 %s = icmp sgt i4 %n, 0
97 br i1 %s, label %loop, label %exit
99 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
100 %i.next = add i4 %i, 2
101 %t = icmp slt i4 %i.next, %n
102 br i1 %t, label %loop, label %exit
107 ; CHECK: Determining loop execution counts for: @nsw
108 ; CHECK: Loop %loop: backedge-taken count is (-1 + %n)
109 ; CHECK: Loop %loop: max backedge-taken count is 6
110 define void @nsw(i4 %n) {
112 %s = icmp sgt i4 %n, 0
113 br i1 %s, label %loop, label %exit
115 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
116 %i.next = add nsw i4 %i, 1
117 %t = icmp slt i4 %i.next, %n
118 br i1 %t, label %loop, label %exit
123 ; If %n is INT4_MAX, %i.next will wrap. The nsw bit says that the
124 ; result is undefined. Therefore, after the loop's second iteration,
125 ; we are free to assume that the loop exits. This is valid because:
126 ; (a) %i.next is a poison value after the second iteration, which can
127 ; also be considered an undef value.
128 ; (b) the return instruction enacts a side effect that is control
129 ; dependent on the poison value.
131 ; CHECK-LABEL: nsw_step2
132 ; CHECK: Determining loop execution counts for: @nsw_step2
133 ; CHECK: Loop %loop: backedge-taken count is ((-1 + %n) /u 2)
134 ; CHECK: Loop %loop: max backedge-taken count is 2
135 define void @nsw_step2(i4 %n) {
137 %s = icmp sgt i4 %n, 0
138 br i1 %s, label %loop, label %exit
140 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
141 %i.next = add nsw i4 %i, 2
142 %t = icmp slt i4 %i.next, %n
143 br i1 %t, label %loop, label %exit
148 ; CHECK-LABEL: nsw_start1
149 ; CHECK: Determining loop execution counts for: @nsw_start1
150 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax %n))
151 ; CHECK: Loop %loop: max backedge-taken count is 5
152 define void @nsw_start1(i4 %n) {
154 %s = icmp sgt i4 %n, 0
155 br i1 %s, label %loop, label %exit
157 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
158 %i.next = add nsw i4 %i, 1
159 %t = icmp slt i4 %i.next, %n
160 br i1 %t, label %loop, label %exit
165 ; CHECK: Determining loop execution counts for: @nsw_start1_step2
166 ; CHECK: Loop %loop: backedge-taken count is ((-2 + (3 smax %n)) /u 2)
167 ; CHECK: Loop %loop: max backedge-taken count is 2
168 define void @nsw_start1_step2(i4 %n) {
170 %s = icmp sgt i4 %n, 0
171 br i1 %s, label %loop, label %exit
173 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
174 %i.next = add nsw i4 %i, 2
175 %t = icmp slt i4 %i.next, %n
176 br i1 %t, label %loop, label %exit
181 ; CHECK: Determining loop execution counts for: @nsw_startx
182 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n))
183 ; CHECK: Loop %loop: max backedge-taken count is -1
184 define void @nsw_startx(i4 %n, i4 %x) {
186 %s = icmp sgt i4 %n, 0
187 br i1 %s, label %loop, label %exit
189 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
190 %i.next = add nsw i4 %i, 1
191 %t = icmp slt i4 %i.next, %n
192 br i1 %t, label %loop, label %exit
197 ; CHECK: Determining loop execution counts for: @nsw_startx_step2
198 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax %n)) /u 2)
199 ; CHECK: Loop %loop: max backedge-taken count is 7
200 define void @nsw_startx_step2(i4 %n, i4 %x) {
202 %s = icmp sgt i4 %n, 0
203 br i1 %s, label %loop, label %exit
205 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
206 %i.next = add nsw i4 %i, 2
207 %t = icmp slt i4 %i.next, %n
208 br i1 %t, label %loop, label %exit
213 ; CHECK: Determining loop execution counts for: @even
214 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
215 ; CHECK: Loop %loop: max backedge-taken count is 5
216 define void @even(i4 %n) {
219 %s = icmp sgt i4 %m, 0
220 br i1 %s, label %loop, label %exit
222 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
223 %i.next = add i4 %i, 1
224 %t = icmp slt i4 %i.next, %m
225 br i1 %t, label %loop, label %exit
230 ; CHECK: Determining loop execution counts for: @even_step2
231 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2)
232 ; CHECK: Loop %loop: max backedge-taken count is 2
233 define void @even_step2(i4 %n) {
236 %s = icmp sgt i4 %m, 0
237 br i1 %s, label %loop, label %exit
239 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
240 %i.next = add i4 %i, 2
241 %t = icmp slt i4 %i.next, %m
242 br i1 %t, label %loop, label %exit
247 ; CHECK: Determining loop execution counts for: @even_start1
248 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
249 ; CHECK: Loop %loop: max backedge-taken count is 4
250 define void @even_start1(i4 %n) {
253 %s = icmp sgt i4 %m, 0
254 br i1 %s, label %loop, label %exit
256 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
257 %i.next = add i4 %i, 1
258 %t = icmp slt i4 %i.next, %m
259 br i1 %t, label %loop, label %exit
264 ; CHECK: Determining loop execution counts for: @even_start1_step2
265 ; CHECK: Loop %loop: backedge-taken count is ((-2 + (3 smax (2 * %n))) /u 2)
266 ; CHECK: Loop %loop: max backedge-taken count is 2
267 define void @even_start1_step2(i4 %n) {
270 %s = icmp sgt i4 %m, 0
271 br i1 %s, label %loop, label %exit
273 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
274 %i.next = add i4 %i, 2
275 %t = icmp slt i4 %i.next, %m
276 br i1 %t, label %loop, label %exit
281 ; CHECK: Determining loop execution counts for: @even_startx
282 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
283 ; CHECK: Loop %loop: max backedge-taken count is -2
284 define void @even_startx(i4 %n, i4 %x) {
287 %s = icmp sgt i4 %m, 0
288 br i1 %s, label %loop, label %exit
290 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
291 %i.next = add i4 %i, 1
292 %t = icmp slt i4 %i.next, %m
293 br i1 %t, label %loop, label %exit
298 ; CHECK: Determining loop execution counts for: @even_startx_step2
299 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2)
300 ; CHECK: Loop %loop: max backedge-taken count is 7
301 define void @even_startx_step2(i4 %n, i4 %x) {
304 %s = icmp sgt i4 %m, 0
305 br i1 %s, label %loop, label %exit
307 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
308 %i.next = add i4 %i, 2
309 %t = icmp slt i4 %i.next, %m
310 br i1 %t, label %loop, label %exit
315 ; CHECK: Determining loop execution counts for: @even_nsw
316 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
317 ; CHECK: Loop %loop: max backedge-taken count is 5
318 define void @even_nsw(i4 %n) {
321 %s = icmp sgt i4 %m, 0
322 br i1 %s, label %loop, label %exit
324 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
325 %i.next = add nsw i4 %i, 1
326 %t = icmp slt i4 %i.next, %m
327 br i1 %t, label %loop, label %exit
332 ; CHECK: Determining loop execution counts for: @even_nsw_step2
333 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2)
334 ; CHECK: Loop %loop: max backedge-taken count is 2
335 define void @even_nsw_step2(i4 %n) {
338 %s = icmp sgt i4 %m, 0
339 br i1 %s, label %loop, label %exit
341 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
342 %i.next = add nsw i4 %i, 2
343 %t = icmp slt i4 %i.next, %m
344 br i1 %t, label %loop, label %exit
349 ; CHECK: Determining loop execution counts for: @even_nsw_start1
350 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
351 ; CHECK: Loop %loop: max backedge-taken count is 4
352 define void @even_nsw_start1(i4 %n) {
355 %s = icmp sgt i4 %m, 0
356 br i1 %s, label %loop, label %exit
358 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
359 %i.next = add nsw i4 %i, 1
360 %t = icmp slt i4 %i.next, %m
361 br i1 %t, label %loop, label %exit
366 ; CHECK: Determining loop execution counts for: @even_nsw_start1_step2
367 ; CHECK: Loop %loop: backedge-taken count is ((-2 + (3 smax (2 * %n))) /u 2)
368 ; CHECK: Loop %loop: max backedge-taken count is 2
369 define void @even_nsw_start1_step2(i4 %n) {
372 %s = icmp sgt i4 %m, 0
373 br i1 %s, label %loop, label %exit
375 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
376 %i.next = add nsw i4 %i, 2
377 %t = icmp slt i4 %i.next, %m
378 br i1 %t, label %loop, label %exit
383 ; CHECK: Determining loop execution counts for: @even_nsw_startx
384 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
385 ; CHECK: Loop %loop: max backedge-taken count is -2
386 define void @even_nsw_startx(i4 %n, i4 %x) {
389 %s = icmp sgt i4 %m, 0
390 br i1 %s, label %loop, label %exit
392 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
393 %i.next = add nsw i4 %i, 1
394 %t = icmp slt i4 %i.next, %m
395 br i1 %t, label %loop, label %exit
400 ; CHECK: Determining loop execution counts for: @even_nsw_startx_step2
401 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2)
402 ; CHECK: Loop %loop: max backedge-taken count is 7
403 define void @even_nsw_startx_step2(i4 %n, i4 %x) {
406 %s = icmp sgt i4 %m, 0
407 br i1 %s, label %loop, label %exit
409 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
410 %i.next = add nsw i4 %i, 2
411 %t = icmp slt i4 %i.next, %m
412 br i1 %t, label %loop, label %exit