1 ; RUN: opt -disable-output "-passes=print<scalar-evolution>" -S -debug-only=scalar-evolution,apint < %s 2>&1 2>&1 | FileCheck %s
4 ; Use the following template to get a chrec {L,+,M,+,N}.
6 ; define signext i32 @func() {
11 ; %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
12 ; %inc = phi i32 [ X, %entry ], [ %inc1, %loop ]
13 ; %acc = phi i32 [ Y, %entry ], [ %acc1, %loop ]
14 ; %ivr1 = add i32 %ivr, %inc
15 ; %inc1 = add i32 %inc, Z ; M = inc1 = inc + N = X + N
16 ; %acc1 = add i32 %acc, %inc ; L = acc1 = X + Y
17 ; %and = and i32 %acc1, 2^W-1 ; iW
18 ; %cond = icmp eq i32 %and, 0
19 ; br i1 %cond, label %exit, label %loop
22 ; %rv = phi i32 [ %acc1, %loop ]
35 ; The connection between the chrec coefficients {L,+,M,+,N} and the quadratic
36 ; coefficients is that the quadratic equation is N x^2 + (2M-N) x + 2L = 0,
37 ; where the equation was multiplied by 2 to make the coefficient at x^2 an
38 ; integer (the actual equation is N/2 x^2 + (M-N/2) x + L = 0).
40 ; Quadratic equation: 2x^2 + 2x + 4 in i4, solution (wrap): 4
41 ; {14,+,14,+,14} -> X=0, Y=14, Z=14
43 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test01'
44 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {-2,+,-2,+,-2}<%loop>
45 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 4
46 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation -2x^2 + -2x + -4, coeff bw: 5, multiplied by 2
47 ; CHECK: {{.*}}SolveQuadraticAddRecExact{{.*}}: solving for unsigned overflow
48 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving -2x^2 + -2x + -4, rw:5
49 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 2x^2 + 2x + -28, rw:5
50 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 4
51 ; CHECK: Loop %loop: Unpredictable backedge-taken count
52 define signext i32 @test01() {
57 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
58 %inc = phi i32 [ 0, %entry ], [ %inc1, %loop ]
59 %acc = phi i32 [ 14, %entry ], [ %acc1, %loop ]
60 %ivr1 = add i32 %ivr, %inc
61 %inc1 = add i32 %inc, 14
62 %acc1 = add i32 %acc, %inc
63 %and = and i32 %acc1, 15
64 %cond = icmp eq i32 %and, 0
65 br i1 %cond, label %exit, label %loop
68 %rv = phi i32 [ %acc1, %loop ]
72 ; Quadratic equation: 1x^2 + -73x + -146 in i32, solution (wrap): 75
73 ; {-72,+,-36,+,1} -> X=-37, Y=-35, Z=1
75 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test02':
76 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {0,+,-36,+,1}<%loop>
77 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 32
78 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 1x^2 + -73x + 0, coeff bw: 33, multiplied by 2
79 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
80 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -73x + 4294967154, rw:32
81 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -73x + -142, rw:32
82 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 75
83 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
84 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -73x + 4294967154, rw:33
85 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -73x + -4294967438, rw:33
86 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 65573
87 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
88 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -73x + -146, rw:32
89 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -73x + -146, rw:32
90 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 75
91 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
92 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -73x + -146, rw:33
93 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -73x + -146, rw:33
94 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 75
95 ; CHECK: Loop %loop: backedge-taken count is 75
96 define signext i32 @test02() {
101 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
102 %inc = phi i32 [ -37, %entry ], [ %inc1, %loop ]
103 %acc = phi i32 [ -35, %entry ], [ %acc1, %loop ]
104 %ivr1 = add i32 %ivr, %inc
105 %inc1 = add i32 %inc, 1
106 %acc1 = add i32 %acc, %inc
107 %and = and i32 %acc1, -1
108 %cond = icmp sgt i32 %and, 0
109 br i1 %cond, label %exit, label %loop
112 %rv = phi i32 [ %acc1, %loop ]
116 ; Quadratic equation: 2x^2 - 4x + 34 in i4, solution (exact): 1.
117 ; {17,+,-1,+,2} -> X=-3, Y=20, Z=2
119 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test03':
120 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {1,+,-1,+,2}<%loop>
121 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 4
122 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 2x^2 + -4x + 2, coeff bw: 5, multiplied by 2
123 ; CHECK: {{.*}}SolveQuadraticAddRecExact{{.*}}: solving for unsigned overflow
124 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 2x^2 + -4x + 2, rw:5
125 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 2x^2 + -4x + 2, rw:5
126 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (root): 1
127 ; CHECK: Loop %loop: backedge-taken count is 1
128 define signext i32 @test03() {
133 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
134 %inc = phi i32 [ -3, %entry ], [ %inc1, %loop ]
135 %acc = phi i32 [ 20, %entry ], [ %acc1, %loop ]
136 %ivr1 = add i32 %ivr, %inc
137 %inc1 = add i32 %inc, 2
138 %acc1 = add i32 %acc, %inc
139 %and = and i32 %acc1, 15
140 %cond = icmp eq i32 %and, 0
141 br i1 %cond, label %exit, label %loop
144 %rv = phi i32 [ %acc1, %loop ]
148 ; Quadratic equation 4x^2 + 2x + 2 in i16, solution (wrap): 181
149 ; {1,+,3,+,4} -> X=-1, Y=2, Z=4 (i16)
151 ; This is an example where the returned solution is the first time an
152 ; unsigned wrap occurs, whereas the actual exit condition occurs much
153 ; later. The number of iterations returned by SolveQuadraticEquation
154 ; is 181, but the loop will iterate 37174 times.
156 ; Here is a C code that corresponds to this case that calculates the number
165 ; while (acc & 0xffff) {
176 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test04':
177 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {0,+,3,+,4}<%loop>
178 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 16
179 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 4x^2 + 2x + 0, coeff bw: 17, multiplied by 2
180 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
181 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 4x^2 + 2x + 2, rw:16
182 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 4x^2 + 2x + -65534, rw:16
183 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 128
184 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
185 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 4x^2 + 2x + 2, rw:17
186 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 4x^2 + 2x + -131070, rw:17
187 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 181
188 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
189 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 4x^2 + 2x + 2, rw:16
190 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 4x^2 + 2x + -65534, rw:16
191 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 128
192 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
193 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 4x^2 + 2x + 2, rw:17
194 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 4x^2 + 2x + -131070, rw:17
195 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 181
196 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {1,+,3,+,4}<%loop>
197 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 16
198 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 4x^2 + 2x + 2, coeff bw: 17, multiplied by 2
199 ; CHECK: {{.*}}SolveQuadraticAddRecExact{{.*}}: solving for unsigned overflow
200 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 4x^2 + 2x + 2, rw:17
201 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 4x^2 + 2x + -131070, rw:17
202 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 181
203 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
204 define signext i32 @test04() {
209 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
210 %inc = phi i32 [ -1, %entry ], [ %inc1, %loop ]
211 %acc = phi i32 [ 2, %entry ], [ %acc1, %loop ]
212 %ivr1 = add i32 %ivr, %inc
213 %inc1 = add i32 %inc, 4
214 %acc1 = add i32 %acc, %inc
215 %and = trunc i32 %acc1 to i16
216 %cond = icmp eq i16 %and, 0
217 br i1 %cond, label %exit, label %loop
220 %rv = phi i32 [ %acc1, %loop ]
224 ; A case with signed arithmetic, but unsigned comparison.
226 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test05':
227 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {0,+,-1,+,-1}<%loop>
228 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 32
229 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation -1x^2 + -1x + 0, coeff bw: 33, multiplied by 2
230 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
231 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving -1x^2 + -1x + 4, rw:32
232 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + 1x + -4, rw:32
233 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 2
234 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
235 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving -1x^2 + -1x + 4, rw:33
236 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + 1x + -4, rw:33
237 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 2
238 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
239 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving -1x^2 + -1x + -2, rw:32
240 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + 1x + -4294967294, rw:32
241 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 65536
242 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
243 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving -1x^2 + -1x + -2, rw:33
244 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + 1x + -8589934590, rw:33
245 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 92682
246 ; CHECK: Loop %loop: backedge-taken count is 2
248 define signext i32 @test05() {
253 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
254 %inc = phi i32 [ 0, %entry ], [ %inc1, %loop ]
255 %acc = phi i32 [ -1, %entry ], [ %acc1, %loop ]
256 %ivr1 = add i32 %ivr, %inc
257 %inc1 = add i32 %inc, -1
258 %acc1 = add i32 %acc, %inc
259 %and = and i32 %acc1, -1
260 %cond = icmp ule i32 %and, -3
261 br i1 %cond, label %exit, label %loop
264 %rv = phi i32 [ %acc1, %loop ]
268 ; A test that used to crash with one of the earlier versions of the code.
270 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test06':
271 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {0,+,-99999,+,1}<%loop>
272 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 32
273 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 1x^2 + -199999x + 0, coeff bw: 33, multiplied by 2
274 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
275 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -199999x + -4294967294, rw:32
276 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -199999x + 2, rw:32
277 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 1
278 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
279 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -199999x + -4294967294, rw:33
280 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -199999x + 4294967298, rw:33
281 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 24469
282 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
283 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -199999x + -12, rw:32
284 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -199999x + 4294967284, rw:32
285 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 24469
286 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
287 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 1x^2 + -199999x + -12, rw:33
288 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 1x^2 + -199999x + 8589934580, rw:33
289 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solution (wrap): 62450
290 ; CHECK: Loop %loop: backedge-taken count is 24469
291 define signext i32 @test06() {
296 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
297 %inc = phi i32 [ -100000, %entry ], [ %inc1, %loop ]
298 %acc = phi i32 [ 100000, %entry ], [ %acc1, %loop ]
299 %ivr1 = add i32 %ivr, %inc
300 %inc1 = add i32 %inc, 1
301 %acc1 = add i32 %acc, %inc
302 %and = and i32 %acc1, -1
303 %cond = icmp sgt i32 %and, 5
304 br i1 %cond, label %exit, label %loop
307 %rv = phi i32 [ %acc1, %loop ]
312 ; 532052752x^2 + -450429774x + 71188414 = 0
313 ; has two exact solutions (up to two decimal digits): 0.21 and 0.64.
314 ; Since there is no integer between them, there is no integer n that either
315 ; solves the equation exactly, or changes the sign of it between n and n+1.
317 ; CHECK-LABEL: Printing analysis 'Scalar Evolution Analysis' for function 'test07':
318 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {0,+,40811489,+,532052752}<%loop>
319 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 32
320 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 532052752x^2 + -450429774x + 0, coeff bw: 33, multiplied by 2
321 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
322 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 532052752x^2 + -450429774x + 71188414, rw:32
323 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 532052752x^2 + -450429774x + 71188414, rw:32
324 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: no valid solution
325 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
326 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 532052752x^2 + -450429774x + 71188414, rw:33
327 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 532052752x^2 + -450429774x + 71188414, rw:33
328 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: no valid solution
329 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for signed overflow
330 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 532052752x^2 + -450429774x + 71188414, rw:32
331 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 532052752x^2 + -450429774x + 71188414, rw:32
332 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: no valid solution
333 ; CHECK: {{.*}}SolveQuadraticAddRecRange{{.*}}: solving for unsigned overflow
334 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 532052752x^2 + -450429774x + 71188414, rw:33
335 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 532052752x^2 + -450429774x + 71188414, rw:33
336 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: no valid solution
337 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: analyzing quadratic addrec: {35594207,+,40811489,+,532052752}<%loop>
338 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: addrec coeff bw: 32
339 ; CHECK: {{.*}}GetQuadraticEquation{{.*}}: equation 532052752x^2 + -450429774x + 71188414, coeff bw: 33, multiplied by 2
340 ; CHECK: {{.*}}SolveQuadraticAddRecExact{{.*}}: solving for unsigned overflow
341 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: solving 532052752x^2 + -450429774x + 71188414, rw:33
342 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: updated coefficients 532052752x^2 + -450429774x + 71188414, rw:33
343 ; CHECK: {{.*}}SolveQuadraticEquationWrap{{.*}}: no valid solution
344 ; CHECK: Loop %loop: Unpredictable backedge-taken count.
345 define signext i32 @test07() {
350 %ivr = phi i32 [ 0, %entry ], [ %ivr1, %loop ]
351 %inc = phi i32 [ -491241263, %entry ], [ %inc1, %loop ]
352 %acc = phi i32 [ 526835470, %entry ], [ %acc1, %loop ]
353 %ivr1 = add i32 %ivr, %inc
354 %inc1 = add i32 %inc, 532052752
355 %acc1 = add i32 %acc, %inc
356 %and = and i32 %acc1, -1
357 %cond = icmp eq i32 %and, 0
358 br i1 %cond, label %exit, label %loop
361 %rv = phi i32 [ %acc1, %loop ]