1 ; RUN: opt -S -disable-output "-passes=print<scalar-evolution>" < %s 2>&1 2>&1 | FileCheck %s
3 ; umin is represented using -1 * umax in scalar evolution. -1 is considered as the
4 ; constant of the multiply expression (-1 * ((-1 + (-1 * %a)) umax (-1 + (-1 * %b)))).
5 ; Returns the greatest power of 2 divisor by evaluating the minimal trailing zeros
6 ; for the trip count expression.
8 ; int foo(uint32_t a, uint32_t b, uint32_t *c) {
9 ; for (uint32_t i = 0; i < (uint32_t)(a < b ? a : b) + 1; i++)
14 ; CHECK: Loop %for.body: Trip multiple is 1
16 define i32 @foo(i32 %a, i32 %b, ptr %c) {
18 %cmp = icmp ult i32 %a, %b
19 %cond = select i1 %cmp, i32 %a, i32 %b
20 %add = add i32 %cond, 1
21 %cmp18 = icmp eq i32 %add, 0
22 br i1 %cmp18, label %for.cond.cleanup, label %for.body.preheader
24 for.body.preheader: ; preds = %entry
27 for.cond.cleanup.loopexit: ; preds = %for.body
28 br label %for.cond.cleanup
30 for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
33 for.body: ; preds = %for.body.preheader, %for.body
34 %i.09 = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ]
35 %arrayidx = getelementptr inbounds i32, ptr %c, i32 %i.09
36 store i32 %i.09, ptr %arrayidx, align 4
37 %inc = add nuw i32 %i.09, 1
38 %cmp1 = icmp ult i32 %inc, %add
39 br i1 %cmp1, label %for.body, label %for.cond.cleanup.loopexit
42 ; Overflow may happen for the multiply expression n * 3, verify that trip
43 ; multiple is set to 1 if NUW/NSW are not set.
45 ; __attribute__((noinline)) void a(unsigned n) {
47 ; for (unsigned i = 0; i != n * 3; ++i)
48 ; printf("TEST%u\n", i);
50 ; int main() { a(2863311531U); }
52 ; CHECK: Loop %for.body: Trip multiple is 1
54 @.str2 = private unnamed_addr constant [8 x i8] c"TEST%u\0A\00", align 1
56 define void @foo2(i32 %n) {
59 %cmp4 = icmp eq i32 %mul, 0
60 br i1 %cmp4, label %for.cond.cleanup, label %for.body.preheader
62 for.body.preheader: ; preds = %entry
65 for.cond.cleanup.loopexit: ; preds = %for.body
66 br label %for.cond.cleanup
68 for.cond.cleanup: ; preds = %for.cond.cleanup.loopexit, %entry
71 for.body: ; preds = %for.body.preheader, %for.body
72 %i.05 = phi i32 [ %inc, %for.body ], [ 0, %for.body.preheader ]
73 %call = tail call i32 (ptr, ...) @printf(ptr @.str2, i32 %i.05)
74 %inc = add nuw i32 %i.05, 1
75 %cmp = icmp eq i32 %inc, %mul
76 br i1 %cmp, label %for.cond.cleanup.loopexit, label %for.body
79 declare i32 @printf(ptr nocapture readonly, ...)
82 ; If we couldn't prove no overflow for the multiply expression 24 * n,
83 ; returns the greatest power of 2 divisor. If overflows happens
84 ; the trip count is still divisible by the greatest power of 2 divisor.
86 ; CHECK: Loop %l3: Trip multiple is 8
90 define i32 @foo3(i32 %n) {
92 %loop_ctl = mul i32 %n, 24
96 %x.0 = phi i32 [ 0, %entry ], [ %inc, %l3 ]
98 %inc = add i32 %x.0, 1
99 %exitcond = icmp eq i32 %inc, %loop_ctl
100 br i1 %exitcond, label %exit, label %l3
106 ; If the trip count is a constant, verify that we obtained the trip
107 ; count itself. For huge trip counts, or zero, we return 1.
109 ; CHECK: Loop %l3: Trip multiple is 3
111 define i32 @foo4(i32 %n) {
116 %x.0 = phi i32 [ 0, %entry ], [ %inc, %l3 ]
118 %inc = add i32 %x.0, 1
119 %exitcond = icmp eq i32 %inc, 3
120 br i1 %exitcond, label %exit, label %l3
126 ; If there are multiple exits, the result is the GCD of the multiples
127 ; of each individual exit (since we don't know which is taken).
129 ; CHECK: Loop %l4: Trip multiple is 50
131 define i32 @foo5(i32 %n) {
136 %x.0 = phi i32 [ 0, %entry ], [ %inc, %l4-latch ]
138 %inc = add i32 %x.0, 1
139 %earlycond = icmp eq i32 %inc, 150
140 br i1 %earlycond, label %exit, label %l4-latch
143 %exitcond = icmp eq i32 %inc, 200
144 br i1 %exitcond, label %exit, label %l4