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 ; Be careful with this one. If %n is INT4_MAX, %i.next will wrap. The nsw bit
124 ; says that the result is undefined, but ScalarEvolution must respect that
125 ; subsequent passes may result the undefined behavior in predictable ways.
126 ; CHECK: Determining loop execution counts for: @nsw_step2
127 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
128 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
129 define void @nsw_step2(i4 %n) {
131 %s = icmp sgt i4 %n, 0
132 br i1 %s, label %loop, label %exit
134 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
135 %i.next = add nsw i4 %i, 2
136 %t = icmp slt i4 %i.next, %n
137 br i1 %t, label %loop, label %exit
142 ; CHECK: Determining loop execution counts for: @nsw_start1
143 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax %n))
144 ; CHECK: Loop %loop: max backedge-taken count is 5
145 define void @nsw_start1(i4 %n) {
147 %s = icmp sgt i4 %n, 0
148 br i1 %s, label %loop, label %exit
150 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
151 %i.next = add nsw i4 %i, 1
152 %t = icmp slt i4 %i.next, %n
153 br i1 %t, label %loop, label %exit
158 ; CHECK: Determining loop execution counts for: @nsw_start1_step2
159 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
160 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
161 define void @nsw_start1_step2(i4 %n) {
163 %s = icmp sgt i4 %n, 0
164 br i1 %s, label %loop, label %exit
166 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
167 %i.next = add nsw i4 %i, 2
168 %t = icmp slt i4 %i.next, %n
169 br i1 %t, label %loop, label %exit
174 ; CHECK: Determining loop execution counts for: @nsw_startx
175 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax %n))
176 ; CHECK: Loop %loop: max backedge-taken count is -1
177 define void @nsw_startx(i4 %n, i4 %x) {
179 %s = icmp sgt i4 %n, 0
180 br i1 %s, label %loop, label %exit
182 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
183 %i.next = add nsw i4 %i, 1
184 %t = icmp slt i4 %i.next, %n
185 br i1 %t, label %loop, label %exit
190 ; CHECK: Determining loop execution counts for: @nsw_startx_step2
191 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
192 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
193 define void @nsw_startx_step2(i4 %n, i4 %x) {
195 %s = icmp sgt i4 %n, 0
196 br i1 %s, label %loop, label %exit
198 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
199 %i.next = add nsw i4 %i, 2
200 %t = icmp slt i4 %i.next, %n
201 br i1 %t, label %loop, label %exit
206 ; CHECK: Determining loop execution counts for: @even
207 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
208 ; CHECK: Loop %loop: max backedge-taken count is 5
209 define void @even(i4 %n) {
212 %s = icmp sgt i4 %m, 0
213 br i1 %s, label %loop, label %exit
215 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
216 %i.next = add i4 %i, 1
217 %t = icmp slt i4 %i.next, %m
218 br i1 %t, label %loop, label %exit
223 ; CHECK: Determining loop execution counts for: @even_step2
224 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
225 ; CHECK: Loop %loop: max backedge-taken count is 2
226 define void @even_step2(i4 %n) {
229 %s = icmp sgt i4 %m, 0
230 br i1 %s, label %loop, label %exit
232 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
233 %i.next = add i4 %i, 2
234 %t = icmp slt i4 %i.next, %m
235 br i1 %t, label %loop, label %exit
240 ; CHECK: Determining loop execution counts for: @even_start1
241 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
242 ; CHECK: Loop %loop: max backedge-taken count is 4
243 define void @even_start1(i4 %n) {
246 %s = icmp sgt i4 %m, 0
247 br i1 %s, label %loop, label %exit
249 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
250 %i.next = add i4 %i, 1
251 %t = icmp slt i4 %i.next, %m
252 br i1 %t, label %loop, label %exit
257 ; CHECK: Determining loop execution counts for: @even_start1_step2
258 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
259 ; CHECK: Loop %loop: max backedge-taken count is 2
260 define void @even_start1_step2(i4 %n) {
263 %s = icmp sgt i4 %m, 0
264 br i1 %s, label %loop, label %exit
266 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
267 %i.next = add i4 %i, 2
268 %t = icmp slt i4 %i.next, %m
269 br i1 %t, label %loop, label %exit
274 ; CHECK: Determining loop execution counts for: @even_startx
275 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
276 ; CHECK: Loop %loop: max backedge-taken count is -1
277 define void @even_startx(i4 %n, i4 %x) {
280 %s = icmp sgt i4 %m, 0
281 br i1 %s, label %loop, label %exit
283 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
284 %i.next = add i4 %i, 1
285 %t = icmp slt i4 %i.next, %m
286 br i1 %t, label %loop, label %exit
291 ; CHECK: Determining loop execution counts for: @even_startx_step2
292 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
293 ; CHECK: Loop %loop: max backedge-taken count is 7
294 define void @even_startx_step2(i4 %n, i4 %x) {
297 %s = icmp sgt i4 %m, 0
298 br i1 %s, label %loop, label %exit
300 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
301 %i.next = add i4 %i, 2
302 %t = icmp slt i4 %i.next, %m
303 br i1 %t, label %loop, label %exit
308 ; CHECK: Determining loop execution counts for: @even_nsw
309 ; CHECK: Loop %loop: backedge-taken count is (-1 + (2 * %n))
310 ; CHECK: Loop %loop: max backedge-taken count is 5
311 define void @even_nsw(i4 %n) {
314 %s = icmp sgt i4 %m, 0
315 br i1 %s, label %loop, label %exit
317 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
318 %i.next = add nsw i4 %i, 1
319 %t = icmp slt i4 %i.next, %m
320 br i1 %t, label %loop, label %exit
325 ; CHECK: Determining loop execution counts for: @even_nsw_step2
326 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (2 * %n)) /u 2)
327 ; CHECK: Loop %loop: max backedge-taken count is 2
328 define void @even_nsw_step2(i4 %n) {
331 %s = icmp sgt i4 %m, 0
332 br i1 %s, label %loop, label %exit
334 %i = phi i4 [ 0, %entry ], [ %i.next, %loop ]
335 %i.next = add nsw i4 %i, 2
336 %t = icmp slt i4 %i.next, %m
337 br i1 %t, label %loop, label %exit
342 ; CHECK: Determining loop execution counts for: @even_nsw_start1
343 ; CHECK: Loop %loop: backedge-taken count is (-2 + (2 smax (2 * %n)))
344 ; CHECK: Loop %loop: max backedge-taken count is 4
345 define void @even_nsw_start1(i4 %n) {
348 %s = icmp sgt i4 %m, 0
349 br i1 %s, label %loop, label %exit
351 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
352 %i.next = add nsw i4 %i, 1
353 %t = icmp slt i4 %i.next, %m
354 br i1 %t, label %loop, label %exit
359 ; CHECK: Determining loop execution counts for: @even_nsw_start1_step2
360 ; CHECK: Loop %loop: backedge-taken count is ((-2 + (3 smax (2 * %n))) /u 2)
361 ; CHECK: Loop %loop: max backedge-taken count is 2
362 define void @even_nsw_start1_step2(i4 %n) {
365 %s = icmp sgt i4 %m, 0
366 br i1 %s, label %loop, label %exit
368 %i = phi i4 [ 1, %entry ], [ %i.next, %loop ]
369 %i.next = add nsw i4 %i, 2
370 %t = icmp slt i4 %i.next, %m
371 br i1 %t, label %loop, label %exit
376 ; CHECK: Determining loop execution counts for: @even_nsw_startx
377 ; CHECK: Loop %loop: backedge-taken count is (-1 + (-1 * %x) + ((1 + %x) smax (2 * %n)))
378 ; CHECK: Loop %loop: max backedge-taken count is -1
379 define void @even_nsw_startx(i4 %n, i4 %x) {
382 %s = icmp sgt i4 %m, 0
383 br i1 %s, label %loop, label %exit
385 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
386 %i.next = add nsw i4 %i, 1
387 %t = icmp slt i4 %i.next, %m
388 br i1 %t, label %loop, label %exit
393 ; CHECK: Determining loop execution counts for: @even_nsw_startx_step2
394 ; CHECK: Loop %loop: backedge-taken count is ((-1 + (-1 * %x) + ((2 + %x) smax (2 * %n))) /u 2)
395 ; CHECK: Loop %loop: max backedge-taken count is 7
396 define void @even_nsw_startx_step2(i4 %n, i4 %x) {
399 %s = icmp sgt i4 %m, 0
400 br i1 %s, label %loop, label %exit
402 %i = phi i4 [ %x, %entry ], [ %i.next, %loop ]
403 %i.next = add nsw i4 %i, 2
404 %t = icmp slt i4 %i.next, %m
405 br i1 %t, label %loop, label %exit