1 ; RUN: opt < %s -analyze -enable-new-pm=0 -scalar-evolution | FileCheck %s
2 ; RUN: opt < %s -disable-output "-passes=print<scalar-evolution>" 2>&1 | FileCheck %s
4 ; ScalarEvolution should be able to understand the loop and eliminate the casts.
8 define void @foo(i32* nocapture %d, i32 %n) nounwind {
10 %0 = icmp sgt i32 %n, 0 ; <i1> [#uses=1]
11 br i1 %0, label %bb.nph, label %return
13 bb.nph: ; preds = %entry
16 bb: ; preds = %bb1, %bb.nph
17 %i.02 = phi i32 [ %5, %bb1 ], [ 0, %bb.nph ] ; <i32> [#uses=2]
18 %p.01 = phi i8 [ %4, %bb1 ], [ -1, %bb.nph ] ; <i8> [#uses=2]
19 %1 = sext i8 %p.01 to i32 ; <i32> [#uses=1]
20 %2 = sext i32 %i.02 to i64 ; <i64> [#uses=1]
21 %3 = getelementptr i32, i32* %d, i64 %2 ; <i32*> [#uses=1]
22 store i32 %1, i32* %3, align 4
23 %4 = add i8 %p.01, 1 ; <i8> [#uses=1]
24 %5 = add i32 %i.02, 1 ; <i32> [#uses=2]
28 %6 = icmp slt i32 %5, %n ; <i1> [#uses=1]
29 br i1 %6, label %bb, label %bb1.return_crit_edge
31 bb1.return_crit_edge: ; preds = %bb1
34 return: ; preds = %bb1.return_crit_edge, %entry
38 ; ScalarEvolution should be able to find the maximum tripcount
39 ; of this multiple-exit loop, and if it doesn't know the exact
40 ; count, it should say so.
43 ; CHECK: Loop %for.cond: <multiple exits> Unpredictable backedge-taken count.
44 ; CHECK: Loop %for.cond: max backedge-taken count is 5
46 @.str = private constant [4 x i8] c"%d\0A\00" ; <[4 x i8]*> [#uses=2]
48 define i32 @main() nounwind {
52 for.cond: ; preds = %for.inc, %entry
53 %g_4.0 = phi i32 [ 0, %entry ], [ %add, %for.inc ] ; <i32> [#uses=5]
54 %cmp = icmp slt i32 %g_4.0, 5 ; <i1> [#uses=1]
55 br i1 %cmp, label %for.body, label %for.end
57 for.body: ; preds = %for.cond
58 %conv = trunc i32 %g_4.0 to i16 ; <i16> [#uses=1]
59 %tobool.not = icmp eq i16 %conv, 0 ; <i1> [#uses=1]
60 %tobool3 = icmp ne i32 %g_4.0, 0 ; <i1> [#uses=1]
61 %or.cond = and i1 %tobool.not, %tobool3 ; <i1> [#uses=1]
62 br i1 %or.cond, label %for.end, label %for.inc
64 for.inc: ; preds = %for.body
65 %add = add nsw i32 %g_4.0, 1 ; <i32> [#uses=1]
68 for.end: ; preds = %for.body, %for.cond
69 %call = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %g_4.0) nounwind ; <i32> [#uses=0]
73 declare i32 @printf(i8*, ...)
75 define void @test(i8* %a, i32 %n) nounwind {
77 %cmp1 = icmp sgt i32 %n, 0
78 br i1 %cmp1, label %for.body.lr.ph, label %for.end
80 for.body.lr.ph: ; preds = %entry
81 %tmp = zext i32 %n to i64
84 for.body: ; preds = %for.body, %for.body.lr.ph
85 %indvar = phi i64 [ %indvar.next, %for.body ], [ 0, %for.body.lr.ph ]
86 %arrayidx = getelementptr i8, i8* %a, i64 %indvar
87 store i8 0, i8* %arrayidx, align 1
88 %indvar.next = add i64 %indvar, 1
89 %exitcond = icmp ne i64 %indvar.next, %tmp
90 br i1 %exitcond, label %for.body, label %for.cond.for.end_crit_edge
92 for.cond.for.end_crit_edge: ; preds = %for.body
95 for.end: ; preds = %for.cond.for.end_crit_edge, %entry
99 ; CHECK: Determining loop execution counts for: @test
100 ; CHECK-NEXT: backedge-taken count is
101 ; CHECK-NEXT: max backedge-taken count is 2147483646
103 ; PR19799: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
104 ; CHECK-LABEL: @pr19799
105 ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
106 ; CHECK: Loop %for.body.i: max backedge-taken count is 1
107 @a = common global i32 0, align 4
109 define i32 @pr19799() {
111 store i32 -1, i32* @a, align 4
114 for.body.i: ; preds = %for.cond.i, %entry
115 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
116 %tobool.i = icmp eq i32 %storemerge1.i, 0
117 %add.i.i = add nsw i32 %storemerge1.i, 2
118 br i1 %tobool.i, label %bar.exit, label %for.cond.i
120 for.cond.i: ; preds = %for.body.i
121 store i32 %add.i.i, i32* @a, align 4
122 %cmp.i = icmp slt i32 %storemerge1.i, 0
123 br i1 %cmp.i, label %for.body.i, label %bar.exit
125 bar.exit: ; preds = %for.cond.i, %for.body.i
129 ; PR18886: Indvars miscompile due to an incorrect max backedge taken count from SCEV.
130 ; CHECK-LABEL: @pr18886
131 ; CHECK: Loop %for.body: <multiple exits> Unpredictable backedge-taken count.
132 ; CHECK: Loop %for.body: max backedge-taken count is 3
133 @aa = global i64 0, align 8
135 define i32 @pr18886() {
137 store i64 -21, i64* @aa, align 8
141 %storemerge1 = phi i64 [ -21, %entry ], [ %add, %for.cond ]
142 %tobool = icmp eq i64 %storemerge1, 0
143 %add = add nsw i64 %storemerge1, 8
144 br i1 %tobool, label %return, label %for.cond
147 store i64 %add, i64* @aa, align 8
148 %cmp = icmp slt i64 %add, 9
149 br i1 %cmp, label %for.body, label %return
152 %retval.0 = phi i32 [ 1, %for.body ], [ 0, %for.cond ]
156 ; Here we have a must-exit loop latch that is not computable and a
157 ; may-exit early exit that can only have one non-exiting iteration
158 ; before the check is forever skipped.
160 ; CHECK-LABEL: @cannot_compute_mustexit
161 ; CHECK: Loop %for.body.i: <multiple exits> Unpredictable backedge-taken count.
162 ; CHECK: Loop %for.body.i: Unpredictable max backedge-taken count.
163 @b = common global i32 0, align 4
165 define i32 @cannot_compute_mustexit() {
167 store i32 -1, i32* @a, align 4
170 for.body.i: ; preds = %for.cond.i, %entry
171 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
172 %tobool.i = icmp eq i32 %storemerge1.i, 0
173 %add.i.i = add nsw i32 %storemerge1.i, 2
174 br i1 %tobool.i, label %bar.exit, label %for.cond.i
176 for.cond.i: ; preds = %for.body.i
177 store i32 %add.i.i, i32* @a, align 4
178 %ld = load volatile i32, i32* @b
179 %cmp.i = icmp ne i32 %ld, 0
180 br i1 %cmp.i, label %for.body.i, label %bar.exit
182 bar.exit: ; preds = %for.cond.i, %for.body.i
186 ; This loop has two must-exits, both of which dominate the latch. The
187 ; MaxBECount should be the minimum of them.
189 ; CHECK-LABEL: @two_mustexit
190 ; CHECK: Loop %for.body.i: <multiple exits> backedge-taken count is 1
191 ; CHECK: Loop %for.body.i: max backedge-taken count is 1
192 define i32 @two_mustexit() {
194 store i32 -1, i32* @a, align 4
197 for.body.i: ; preds = %for.cond.i, %entry
198 %storemerge1.i = phi i32 [ -1, %entry ], [ %add.i.i, %for.cond.i ]
199 %tobool.i = icmp sgt i32 %storemerge1.i, 0
200 %add.i.i = add nsw i32 %storemerge1.i, 2
201 br i1 %tobool.i, label %bar.exit, label %for.cond.i
203 for.cond.i: ; preds = %for.body.i
204 store i32 %add.i.i, i32* @a, align 4
205 %cmp.i = icmp slt i32 %storemerge1.i, 3
206 br i1 %cmp.i, label %for.body.i, label %bar.exit
208 bar.exit: ; preds = %for.cond.i, %for.body.i
212 ; CHECK-LABEL: @ne_max_trip_count_1
213 ; CHECK: Loop %for.body: max backedge-taken count is 7
214 define i32 @ne_max_trip_count_1(i32 %n) {
216 %masked = and i32 %n, 7
220 %i = phi i32 [ 0, %entry ], [ %add, %for.body ]
221 %add = add nsw i32 %i, 1
222 %cmp = icmp ne i32 %i, %masked
223 br i1 %cmp, label %for.body, label %bar.exit
229 ; CHECK-LABEL: @ne_max_trip_count_2
230 ; CHECK: Loop %for.body: max backedge-taken count is -1
231 define i32 @ne_max_trip_count_2(i32 %n) {
233 %masked = and i32 %n, 7
237 %i = phi i32 [ 0, %entry ], [ %add, %for.body ]
238 %add = add nsw i32 %i, 1
239 %cmp = icmp ne i32 %add, %masked
240 br i1 %cmp, label %for.body, label %bar.exit
246 ; CHECK-LABEL: @ne_max_trip_count_3
247 ; CHECK: Loop %for.body: max backedge-taken count is 6
248 define i32 @ne_max_trip_count_3(i32 %n) {
250 %masked = and i32 %n, 7
251 %guard = icmp eq i32 %masked, 0
252 br i1 %guard, label %exit, label %for.preheader
258 %i = phi i32 [ 0, %for.preheader ], [ %add, %for.body ]
259 %add = add nsw i32 %i, 1
260 %cmp = icmp ne i32 %add, %masked
261 br i1 %cmp, label %for.body, label %loop.exit
270 ; CHECK-LABEL: @ne_max_trip_count_4
271 ; CHECK: Loop %for.body: max backedge-taken count is -2
272 define i32 @ne_max_trip_count_4(i32 %n) {
274 %guard = icmp eq i32 %n, 0
275 br i1 %guard, label %exit, label %for.preheader
281 %i = phi i32 [ 0, %for.preheader ], [ %add, %for.body ]
282 %add = add nsw i32 %i, 1
283 %cmp = icmp ne i32 %add, %n
284 br i1 %cmp, label %for.body, label %loop.exit
293 ; The end bound of the loop can change between iterations, so the exact trip
294 ; count is unknown, but SCEV can calculate the max trip count.
295 define void @changing_end_bound(i32* %n_addr, i32* %addr) {
296 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound
297 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
298 ; CHECK: Loop %loop: max backedge-taken count is 2147483646
303 %iv = phi i32 [ 0, %entry ], [ %iv.next, %loop ]
304 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
305 %val = load atomic i32, i32* %addr unordered, align 4
307 %acc.next = add i32 %acc, %val
308 %iv.next = add nsw i32 %iv, 1
309 %n = load atomic i32, i32* %n_addr unordered, align 4
310 %cmp = icmp slt i32 %iv.next, %n
311 br i1 %cmp, label %loop, label %loop.exit
317 ; Similar test as above, but unknown start value.
318 ; Also, there's no nsw on the iv.next, but SCEV knows
319 ; the termination condition is LT, so the IV cannot wrap.
320 define void @changing_end_bound2(i32 %start, i32* %n_addr, i32* %addr) {
321 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound2
322 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
323 ; CHECK: Loop %loop: max backedge-taken count is -1
328 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
329 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
330 %val = load atomic i32, i32* %addr unordered, align 4
332 %acc.next = add i32 %acc, %val
333 %iv.next = add i32 %iv, 1
334 %n = load atomic i32, i32* %n_addr unordered, align 4
335 %cmp = icmp slt i32 %iv.next, %n
336 br i1 %cmp, label %loop, label %loop.exit
342 ; changing end bound and greater than one stride
343 define void @changing_end_bound3(i32 %start, i32* %n_addr, i32* %addr) {
344 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound3
345 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
346 ; CHECK: Loop %loop: max backedge-taken count is 1073741823
351 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
352 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
353 %val = load atomic i32, i32* %addr unordered, align 4
355 %acc.next = add i32 %acc, %val
356 %iv.next = add nsw i32 %iv, 4
357 %n = load atomic i32, i32* %n_addr unordered, align 4
358 %cmp = icmp slt i32 %iv.next, %n
359 br i1 %cmp, label %loop, label %loop.exit
365 ; same as above test, but the IV can wrap around.
366 ; so the max backedge taken count is unpredictable.
367 define void @changing_end_bound4(i32 %start, i32* %n_addr, i32* %addr) {
368 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound4
369 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
370 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
375 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
376 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
377 %val = load atomic i32, i32* %addr unordered, align 4
379 %acc.next = add i32 %acc, %val
380 %iv.next = add i32 %iv, 4
381 %n = load atomic i32, i32* %n_addr unordered, align 4
382 %cmp = icmp slt i32 %iv.next, %n
383 br i1 %cmp, label %loop, label %loop.exit
389 ; unknown stride. Since it's not knownPositive, we do not estimate the max
390 ; backedge taken count.
391 define void @changing_end_bound5(i32 %stride, i32 %start, i32* %n_addr, i32* %addr) {
392 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound5
393 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
394 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
399 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
400 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
401 %val = load atomic i32, i32* %addr unordered, align 4
403 %acc.next = add i32 %acc, %val
404 %iv.next = add nsw i32 %iv, %stride
405 %n = load atomic i32, i32* %n_addr unordered, align 4
406 %cmp = icmp slt i32 %iv.next, %n
407 br i1 %cmp, label %loop, label %loop.exit
413 ; negative stride value
414 define void @changing_end_bound6(i32 %start, i32* %n_addr, i32* %addr) {
415 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound6
416 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
417 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
422 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
423 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
424 %val = load atomic i32, i32* %addr unordered, align 4
426 %acc.next = add i32 %acc, %val
427 %iv.next = add nsw i32 %iv, -1
428 %n = load atomic i32, i32* %n_addr unordered, align 4
429 %cmp = icmp slt i32 %iv.next, %n
430 br i1 %cmp, label %loop, label %loop.exit
436 ; sgt with negative stride
437 define void @changing_end_bound7(i32 %start, i32* %n_addr, i32* %addr) {
438 ; CHECK-LABEL: Determining loop execution counts for: @changing_end_bound7
439 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
440 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
445 %iv = phi i32 [ %start, %entry ], [ %iv.next, %loop ]
446 %acc = phi i32 [ 0, %entry ], [ %acc.next, %loop ]
447 %val = load atomic i32, i32* %addr unordered, align 4
449 %acc.next = add i32 %acc, %val
450 %iv.next = add i32 %iv, -1
451 %n = load atomic i32, i32* %n_addr unordered, align 4
452 %cmp = icmp sgt i32 %iv.next, %n
453 br i1 %cmp, label %loop, label %loop.exit
459 define void @max_overflow(i8 %n) mustprogress {
460 ; CHECK-LABEL: Determining loop execution counts for: @max_overflow
461 ; CHECK: Loop %loop: backedge-taken count is (-126 + (126 smax %n))<nsw>
462 ; CHECK: Loop %loop: max backedge-taken count is 0
467 %i = phi i8 [ 63, %entry ], [ %i.next, %loop ]
468 %i.next = add nsw i8 %i, 63
469 %t = icmp slt i8 %i.next, %n
470 br i1 %t, label %loop, label %exit
476 ; Max backedge-taken count is zero.
477 define void @bool_stride(i1 %s, i1 %n) mustprogress {
478 ; CHECK-LABEL: Determining loop execution counts for: @bool_stride
479 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
480 ; CHECK: Loop %loop: Unpredictable max backedge-taken count.
485 %i = phi i1 [ -1, %entry ], [ %i.next, %loop ]
486 %i.next = add nsw i1 %i, %s
487 %t = icmp slt i1 %i.next, %n
488 br i1 %t, label %loop, label %exit