]>
Commit | Line | Data |
---|---|---|
c8a2ab6d | 1 | /* Chains of recurrences. |
c75c517d | 2 | Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 |
66647d44 | 3 | Free Software Foundation, Inc. |
0ff4040e | 4 | Contributed by Sebastian Pop <pop@cri.ensmp.fr> |
c8a2ab6d SP |
5 | |
6 | This file is part of GCC. | |
7 | ||
8 | GCC is free software; you can redistribute it and/or modify it under | |
9 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 10 | Software Foundation; either version 3, or (at your option) any later |
c8a2ab6d SP |
11 | version. |
12 | ||
13 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
14 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
15 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
16 | for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
19 | along with GCC; see the file COPYING3. If not see |
20 | <http://www.gnu.org/licenses/>. */ | |
c8a2ab6d SP |
21 | |
22 | /* This file implements operations on chains of recurrences. Chains | |
23 | of recurrences are used for modeling evolution functions of scalar | |
24 | variables. | |
25 | */ | |
26 | ||
27 | #include "config.h" | |
28 | #include "system.h" | |
29 | #include "coretypes.h" | |
cf835838 | 30 | #include "tree-pretty-print.h" |
1e8552eb SP |
31 | #include "cfgloop.h" |
32 | #include "tree-flow.h" | |
c8a2ab6d | 33 | #include "tree-chrec.h" |
7ee2468b | 34 | #include "dumpfile.h" |
2412d35c | 35 | #include "params.h" |
18aed06a | 36 | #include "tree-scalar-evolution.h" |
c8a2ab6d | 37 | |
c8a2ab6d SP |
38 | /* Extended folder for chrecs. */ |
39 | ||
40 | /* Determines whether CST is not a constant evolution. */ | |
41 | ||
42 | static inline bool | |
ed7a4b4b | 43 | is_not_constant_evolution (const_tree cst) |
c8a2ab6d SP |
44 | { |
45 | return (TREE_CODE (cst) == POLYNOMIAL_CHREC); | |
46 | } | |
47 | ||
48 | /* Fold CODE for a polynomial function and a constant. */ | |
49 | ||
b8698a0f L |
50 | static inline tree |
51 | chrec_fold_poly_cst (enum tree_code code, | |
52 | tree type, | |
53 | tree poly, | |
c8a2ab6d SP |
54 | tree cst) |
55 | { | |
1e128c5f GB |
56 | gcc_assert (poly); |
57 | gcc_assert (cst); | |
58 | gcc_assert (TREE_CODE (poly) == POLYNOMIAL_CHREC); | |
59 | gcc_assert (!is_not_constant_evolution (cst)); | |
e2157b49 SP |
60 | gcc_assert (type == chrec_type (poly)); |
61 | ||
c8a2ab6d SP |
62 | switch (code) |
63 | { | |
64 | case PLUS_EXPR: | |
b8698a0f L |
65 | return build_polynomial_chrec |
66 | (CHREC_VARIABLE (poly), | |
c8a2ab6d SP |
67 | chrec_fold_plus (type, CHREC_LEFT (poly), cst), |
68 | CHREC_RIGHT (poly)); | |
b8698a0f | 69 | |
c8a2ab6d | 70 | case MINUS_EXPR: |
b8698a0f L |
71 | return build_polynomial_chrec |
72 | (CHREC_VARIABLE (poly), | |
c8a2ab6d SP |
73 | chrec_fold_minus (type, CHREC_LEFT (poly), cst), |
74 | CHREC_RIGHT (poly)); | |
b8698a0f | 75 | |
c8a2ab6d | 76 | case MULT_EXPR: |
b8698a0f L |
77 | return build_polynomial_chrec |
78 | (CHREC_VARIABLE (poly), | |
c8a2ab6d SP |
79 | chrec_fold_multiply (type, CHREC_LEFT (poly), cst), |
80 | chrec_fold_multiply (type, CHREC_RIGHT (poly), cst)); | |
b8698a0f | 81 | |
c8a2ab6d SP |
82 | default: |
83 | return chrec_dont_know; | |
84 | } | |
85 | } | |
86 | ||
87 | /* Fold the addition of two polynomial functions. */ | |
88 | ||
b8698a0f L |
89 | static inline tree |
90 | chrec_fold_plus_poly_poly (enum tree_code code, | |
91 | tree type, | |
92 | tree poly0, | |
c8a2ab6d SP |
93 | tree poly1) |
94 | { | |
95 | tree left, right; | |
677cc14d ZD |
96 | struct loop *loop0 = get_chrec_loop (poly0); |
97 | struct loop *loop1 = get_chrec_loop (poly1); | |
370f4759 | 98 | tree rtype = code == POINTER_PLUS_EXPR ? chrec_type (poly1) : type; |
1e128c5f GB |
99 | |
100 | gcc_assert (poly0); | |
101 | gcc_assert (poly1); | |
102 | gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC); | |
103 | gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC); | |
5be014d5 | 104 | if (POINTER_TYPE_P (chrec_type (poly0))) |
370f4759 | 105 | gcc_assert (ptrofftype_p (chrec_type (poly1))); |
5be014d5 AP |
106 | else |
107 | gcc_assert (chrec_type (poly0) == chrec_type (poly1)); | |
e2157b49 | 108 | gcc_assert (type == chrec_type (poly0)); |
b8698a0f | 109 | |
c8a2ab6d SP |
110 | /* |
111 | {a, +, b}_1 + {c, +, d}_2 -> {{a, +, b}_1 + c, +, d}_2, | |
112 | {a, +, b}_2 + {c, +, d}_1 -> {{c, +, d}_1 + a, +, b}_2, | |
113 | {a, +, b}_x + {c, +, d}_x -> {a+c, +, b+d}_x. */ | |
677cc14d | 114 | if (flow_loop_nested_p (loop0, loop1)) |
c8a2ab6d | 115 | { |
5be014d5 | 116 | if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) |
b8698a0f L |
117 | return build_polynomial_chrec |
118 | (CHREC_VARIABLE (poly1), | |
c8a2ab6d SP |
119 | chrec_fold_plus (type, poly0, CHREC_LEFT (poly1)), |
120 | CHREC_RIGHT (poly1)); | |
121 | else | |
b8698a0f L |
122 | return build_polynomial_chrec |
123 | (CHREC_VARIABLE (poly1), | |
c8a2ab6d | 124 | chrec_fold_minus (type, poly0, CHREC_LEFT (poly1)), |
b8698a0f | 125 | chrec_fold_multiply (type, CHREC_RIGHT (poly1), |
7e0923cd SP |
126 | SCALAR_FLOAT_TYPE_P (type) |
127 | ? build_real (type, dconstm1) | |
128 | : build_int_cst_type (type, -1))); | |
c8a2ab6d | 129 | } |
b8698a0f | 130 | |
677cc14d | 131 | if (flow_loop_nested_p (loop1, loop0)) |
c8a2ab6d | 132 | { |
5be014d5 | 133 | if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) |
b8698a0f L |
134 | return build_polynomial_chrec |
135 | (CHREC_VARIABLE (poly0), | |
c8a2ab6d SP |
136 | chrec_fold_plus (type, CHREC_LEFT (poly0), poly1), |
137 | CHREC_RIGHT (poly0)); | |
138 | else | |
b8698a0f L |
139 | return build_polynomial_chrec |
140 | (CHREC_VARIABLE (poly0), | |
c8a2ab6d SP |
141 | chrec_fold_minus (type, CHREC_LEFT (poly0), poly1), |
142 | CHREC_RIGHT (poly0)); | |
143 | } | |
b8698a0f | 144 | |
677cc14d ZD |
145 | /* This function should never be called for chrecs of loops that |
146 | do not belong to the same loop nest. */ | |
147 | gcc_assert (loop0 == loop1); | |
148 | ||
5be014d5 | 149 | if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) |
c8a2ab6d | 150 | { |
b8698a0f | 151 | left = chrec_fold_plus |
c8a2ab6d | 152 | (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1)); |
b8698a0f | 153 | right = chrec_fold_plus |
5be014d5 | 154 | (rtype, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1)); |
c8a2ab6d SP |
155 | } |
156 | else | |
157 | { | |
b8698a0f | 158 | left = chrec_fold_minus |
c8a2ab6d | 159 | (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1)); |
b8698a0f | 160 | right = chrec_fold_minus |
c8a2ab6d SP |
161 | (type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1)); |
162 | } | |
163 | ||
164 | if (chrec_zerop (right)) | |
165 | return left; | |
166 | else | |
b8698a0f L |
167 | return build_polynomial_chrec |
168 | (CHREC_VARIABLE (poly0), left, right); | |
c8a2ab6d SP |
169 | } |
170 | ||
171 | \f | |
172 | ||
173 | /* Fold the multiplication of two polynomial functions. */ | |
174 | ||
b8698a0f L |
175 | static inline tree |
176 | chrec_fold_multiply_poly_poly (tree type, | |
177 | tree poly0, | |
c8a2ab6d SP |
178 | tree poly1) |
179 | { | |
2c5f025d ZD |
180 | tree t0, t1, t2; |
181 | int var; | |
677cc14d ZD |
182 | struct loop *loop0 = get_chrec_loop (poly0); |
183 | struct loop *loop1 = get_chrec_loop (poly1); | |
2c5f025d | 184 | |
1e128c5f GB |
185 | gcc_assert (poly0); |
186 | gcc_assert (poly1); | |
187 | gcc_assert (TREE_CODE (poly0) == POLYNOMIAL_CHREC); | |
188 | gcc_assert (TREE_CODE (poly1) == POLYNOMIAL_CHREC); | |
e2157b49 SP |
189 | gcc_assert (chrec_type (poly0) == chrec_type (poly1)); |
190 | gcc_assert (type == chrec_type (poly0)); | |
b8698a0f | 191 | |
c8a2ab6d SP |
192 | /* {a, +, b}_1 * {c, +, d}_2 -> {c*{a, +, b}_1, +, d}_2, |
193 | {a, +, b}_2 * {c, +, d}_1 -> {a*{c, +, d}_1, +, b}_2, | |
194 | {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */ | |
677cc14d | 195 | if (flow_loop_nested_p (loop0, loop1)) |
c8a2ab6d | 196 | /* poly0 is a constant wrt. poly1. */ |
b8698a0f L |
197 | return build_polynomial_chrec |
198 | (CHREC_VARIABLE (poly1), | |
c8a2ab6d SP |
199 | chrec_fold_multiply (type, CHREC_LEFT (poly1), poly0), |
200 | CHREC_RIGHT (poly1)); | |
b8698a0f | 201 | |
677cc14d | 202 | if (flow_loop_nested_p (loop1, loop0)) |
c8a2ab6d | 203 | /* poly1 is a constant wrt. poly0. */ |
b8698a0f L |
204 | return build_polynomial_chrec |
205 | (CHREC_VARIABLE (poly0), | |
c8a2ab6d SP |
206 | chrec_fold_multiply (type, CHREC_LEFT (poly0), poly1), |
207 | CHREC_RIGHT (poly0)); | |
b8698a0f | 208 | |
677cc14d ZD |
209 | gcc_assert (loop0 == loop1); |
210 | ||
c8a2ab6d SP |
211 | /* poly0 and poly1 are two polynomials in the same variable, |
212 | {a, +, b}_x * {c, +, d}_x -> {a*c, +, a*d + b*c + b*d, +, 2*b*d}_x. */ | |
b8698a0f | 213 | |
2c5f025d ZD |
214 | /* "a*c". */ |
215 | t0 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_LEFT (poly1)); | |
216 | ||
3cb8677c | 217 | /* "a*d + b*c". */ |
2c5f025d ZD |
218 | t1 = chrec_fold_multiply (type, CHREC_LEFT (poly0), CHREC_RIGHT (poly1)); |
219 | t1 = chrec_fold_plus (type, t1, chrec_fold_multiply (type, | |
220 | CHREC_RIGHT (poly0), | |
221 | CHREC_LEFT (poly1))); | |
3cb8677c | 222 | /* "b*d". */ |
2c5f025d | 223 | t2 = chrec_fold_multiply (type, CHREC_RIGHT (poly0), CHREC_RIGHT (poly1)); |
3cb8677c RG |
224 | /* "a*d + b*c + b*d". */ |
225 | t1 = chrec_fold_plus (type, t1, t2); | |
226 | /* "2*b*d". */ | |
7e0923cd SP |
227 | t2 = chrec_fold_multiply (type, SCALAR_FLOAT_TYPE_P (type) |
228 | ? build_real (type, dconst2) | |
ff5e9a94 | 229 | : build_int_cst (type, 2), t2); |
2c5f025d ZD |
230 | |
231 | var = CHREC_VARIABLE (poly0); | |
232 | return build_polynomial_chrec (var, t0, | |
233 | build_polynomial_chrec (var, t1, t2)); | |
c8a2ab6d SP |
234 | } |
235 | ||
236 | /* When the operands are automatically_generated_chrec_p, the fold has | |
237 | to respect the semantics of the operands. */ | |
238 | ||
b8698a0f L |
239 | static inline tree |
240 | chrec_fold_automatically_generated_operands (tree op0, | |
c8a2ab6d SP |
241 | tree op1) |
242 | { | |
243 | if (op0 == chrec_dont_know | |
244 | || op1 == chrec_dont_know) | |
245 | return chrec_dont_know; | |
b8698a0f | 246 | |
c8a2ab6d SP |
247 | if (op0 == chrec_known |
248 | || op1 == chrec_known) | |
249 | return chrec_known; | |
b8698a0f | 250 | |
c8a2ab6d SP |
251 | if (op0 == chrec_not_analyzed_yet |
252 | || op1 == chrec_not_analyzed_yet) | |
253 | return chrec_not_analyzed_yet; | |
b8698a0f | 254 | |
8c27b7d4 | 255 | /* The default case produces a safe result. */ |
c8a2ab6d SP |
256 | return chrec_dont_know; |
257 | } | |
258 | ||
259 | /* Fold the addition of two chrecs. */ | |
260 | ||
261 | static tree | |
b8698a0f | 262 | chrec_fold_plus_1 (enum tree_code code, tree type, |
e2157b49 | 263 | tree op0, tree op1) |
c8a2ab6d SP |
264 | { |
265 | if (automatically_generated_chrec_p (op0) | |
266 | || automatically_generated_chrec_p (op1)) | |
267 | return chrec_fold_automatically_generated_operands (op0, op1); | |
b8698a0f | 268 | |
c8a2ab6d SP |
269 | switch (TREE_CODE (op0)) |
270 | { | |
271 | case POLYNOMIAL_CHREC: | |
272 | switch (TREE_CODE (op1)) | |
273 | { | |
274 | case POLYNOMIAL_CHREC: | |
275 | return chrec_fold_plus_poly_poly (code, type, op0, op1); | |
276 | ||
ccc5b640 SP |
277 | CASE_CONVERT: |
278 | if (tree_contains_chrecs (op1, NULL)) | |
279 | return chrec_dont_know; | |
280 | ||
c8a2ab6d | 281 | default: |
5be014d5 | 282 | if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) |
b8698a0f L |
283 | return build_polynomial_chrec |
284 | (CHREC_VARIABLE (op0), | |
c8a2ab6d SP |
285 | chrec_fold_plus (type, CHREC_LEFT (op0), op1), |
286 | CHREC_RIGHT (op0)); | |
287 | else | |
b8698a0f L |
288 | return build_polynomial_chrec |
289 | (CHREC_VARIABLE (op0), | |
c8a2ab6d SP |
290 | chrec_fold_minus (type, CHREC_LEFT (op0), op1), |
291 | CHREC_RIGHT (op0)); | |
292 | } | |
293 | ||
ccc5b640 SP |
294 | CASE_CONVERT: |
295 | if (tree_contains_chrecs (op0, NULL)) | |
296 | return chrec_dont_know; | |
297 | ||
c8a2ab6d SP |
298 | default: |
299 | switch (TREE_CODE (op1)) | |
300 | { | |
301 | case POLYNOMIAL_CHREC: | |
5be014d5 | 302 | if (code == PLUS_EXPR || code == POINTER_PLUS_EXPR) |
b8698a0f L |
303 | return build_polynomial_chrec |
304 | (CHREC_VARIABLE (op1), | |
c8a2ab6d SP |
305 | chrec_fold_plus (type, op0, CHREC_LEFT (op1)), |
306 | CHREC_RIGHT (op1)); | |
307 | else | |
b8698a0f L |
308 | return build_polynomial_chrec |
309 | (CHREC_VARIABLE (op1), | |
c8a2ab6d | 310 | chrec_fold_minus (type, op0, CHREC_LEFT (op1)), |
b8698a0f | 311 | chrec_fold_multiply (type, CHREC_RIGHT (op1), |
7e0923cd SP |
312 | SCALAR_FLOAT_TYPE_P (type) |
313 | ? build_real (type, dconstm1) | |
314 | : build_int_cst_type (type, -1))); | |
c8a2ab6d | 315 | |
ccc5b640 SP |
316 | CASE_CONVERT: |
317 | if (tree_contains_chrecs (op1, NULL)) | |
318 | return chrec_dont_know; | |
319 | ||
c8a2ab6d | 320 | default: |
2412d35c SP |
321 | { |
322 | int size = 0; | |
323 | if ((tree_contains_chrecs (op0, &size) | |
324 | || tree_contains_chrecs (op1, &size)) | |
325 | && size < PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE)) | |
326 | return build2 (code, type, op0, op1); | |
327 | else if (size < PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE)) | |
0d82a1c8 RG |
328 | { |
329 | if (code == POINTER_PLUS_EXPR) | |
330 | return fold_build_pointer_plus (fold_convert (type, op0), | |
331 | op1); | |
332 | else | |
333 | return fold_build2 (code, type, | |
334 | fold_convert (type, op0), | |
335 | fold_convert (type, op1)); | |
336 | } | |
2412d35c SP |
337 | else |
338 | return chrec_dont_know; | |
339 | } | |
c8a2ab6d SP |
340 | } |
341 | } | |
342 | } | |
343 | ||
344 | /* Fold the addition of two chrecs. */ | |
345 | ||
346 | tree | |
b8698a0f | 347 | chrec_fold_plus (tree type, |
c8a2ab6d SP |
348 | tree op0, |
349 | tree op1) | |
350 | { | |
5be014d5 | 351 | enum tree_code code; |
e2157b49 SP |
352 | if (automatically_generated_chrec_p (op0) |
353 | || automatically_generated_chrec_p (op1)) | |
354 | return chrec_fold_automatically_generated_operands (op0, op1); | |
355 | ||
c8a2ab6d | 356 | if (integer_zerop (op0)) |
726a989a | 357 | return chrec_convert (type, op1, NULL); |
c8a2ab6d | 358 | if (integer_zerop (op1)) |
726a989a | 359 | return chrec_convert (type, op0, NULL); |
5be014d5 AP |
360 | |
361 | if (POINTER_TYPE_P (type)) | |
362 | code = POINTER_PLUS_EXPR; | |
363 | else | |
364 | code = PLUS_EXPR; | |
b8698a0f | 365 | |
5be014d5 | 366 | return chrec_fold_plus_1 (code, type, op0, op1); |
c8a2ab6d SP |
367 | } |
368 | ||
369 | /* Fold the subtraction of two chrecs. */ | |
370 | ||
b8698a0f L |
371 | tree |
372 | chrec_fold_minus (tree type, | |
373 | tree op0, | |
c8a2ab6d SP |
374 | tree op1) |
375 | { | |
e2157b49 SP |
376 | if (automatically_generated_chrec_p (op0) |
377 | || automatically_generated_chrec_p (op1)) | |
378 | return chrec_fold_automatically_generated_operands (op0, op1); | |
379 | ||
c8a2ab6d SP |
380 | if (integer_zerop (op1)) |
381 | return op0; | |
b8698a0f | 382 | |
c8a2ab6d SP |
383 | return chrec_fold_plus_1 (MINUS_EXPR, type, op0, op1); |
384 | } | |
385 | ||
386 | /* Fold the multiplication of two chrecs. */ | |
387 | ||
388 | tree | |
b8698a0f | 389 | chrec_fold_multiply (tree type, |
c8a2ab6d SP |
390 | tree op0, |
391 | tree op1) | |
392 | { | |
393 | if (automatically_generated_chrec_p (op0) | |
394 | || automatically_generated_chrec_p (op1)) | |
395 | return chrec_fold_automatically_generated_operands (op0, op1); | |
b8698a0f | 396 | |
c8a2ab6d SP |
397 | switch (TREE_CODE (op0)) |
398 | { | |
399 | case POLYNOMIAL_CHREC: | |
400 | switch (TREE_CODE (op1)) | |
401 | { | |
402 | case POLYNOMIAL_CHREC: | |
403 | return chrec_fold_multiply_poly_poly (type, op0, op1); | |
b8698a0f | 404 | |
ccc5b640 SP |
405 | CASE_CONVERT: |
406 | if (tree_contains_chrecs (op1, NULL)) | |
407 | return chrec_dont_know; | |
408 | ||
c8a2ab6d SP |
409 | default: |
410 | if (integer_onep (op1)) | |
411 | return op0; | |
412 | if (integer_zerop (op1)) | |
ff5e9a94 | 413 | return build_int_cst (type, 0); |
b8698a0f L |
414 | |
415 | return build_polynomial_chrec | |
416 | (CHREC_VARIABLE (op0), | |
c8a2ab6d SP |
417 | chrec_fold_multiply (type, CHREC_LEFT (op0), op1), |
418 | chrec_fold_multiply (type, CHREC_RIGHT (op0), op1)); | |
419 | } | |
b8698a0f | 420 | |
ccc5b640 SP |
421 | CASE_CONVERT: |
422 | if (tree_contains_chrecs (op0, NULL)) | |
423 | return chrec_dont_know; | |
424 | ||
c8a2ab6d SP |
425 | default: |
426 | if (integer_onep (op0)) | |
427 | return op1; | |
b8698a0f | 428 | |
c8a2ab6d | 429 | if (integer_zerop (op0)) |
ff5e9a94 | 430 | return build_int_cst (type, 0); |
b8698a0f | 431 | |
c8a2ab6d SP |
432 | switch (TREE_CODE (op1)) |
433 | { | |
434 | case POLYNOMIAL_CHREC: | |
b8698a0f L |
435 | return build_polynomial_chrec |
436 | (CHREC_VARIABLE (op1), | |
c8a2ab6d SP |
437 | chrec_fold_multiply (type, CHREC_LEFT (op1), op0), |
438 | chrec_fold_multiply (type, CHREC_RIGHT (op1), op0)); | |
b8698a0f | 439 | |
ccc5b640 SP |
440 | CASE_CONVERT: |
441 | if (tree_contains_chrecs (op1, NULL)) | |
442 | return chrec_dont_know; | |
443 | ||
c8a2ab6d SP |
444 | default: |
445 | if (integer_onep (op1)) | |
446 | return op0; | |
447 | if (integer_zerop (op1)) | |
ff5e9a94 | 448 | return build_int_cst (type, 0); |
2412d35c | 449 | return fold_build2 (MULT_EXPR, type, op0, op1); |
c8a2ab6d SP |
450 | } |
451 | } | |
452 | } | |
453 | ||
454 | \f | |
455 | ||
456 | /* Operations. */ | |
457 | ||
1a9dddad RS |
458 | /* Evaluate the binomial coefficient. Return NULL_TREE if the intermediate |
459 | calculation overflows, otherwise return C(n,k) with type TYPE. */ | |
460 | ||
b8698a0f | 461 | static tree |
1a9dddad | 462 | tree_fold_binomial (tree type, tree n, unsigned int k) |
c8a2ab6d | 463 | { |
1a9dddad RS |
464 | unsigned HOST_WIDE_INT lidx, lnum, ldenom, lres, ldum; |
465 | HOST_WIDE_INT hidx, hnum, hdenom, hres, hdum; | |
466 | unsigned int i; | |
467 | tree res; | |
468 | ||
469 | /* Handle the most frequent cases. */ | |
470 | if (k == 0) | |
471 | return build_int_cst (type, 1); | |
472 | if (k == 1) | |
473 | return fold_convert (type, n); | |
474 | ||
475 | /* Check that k <= n. */ | |
476 | if (TREE_INT_CST_HIGH (n) == 0 | |
477 | && TREE_INT_CST_LOW (n) < k) | |
478 | return NULL_TREE; | |
479 | ||
480 | /* Numerator = n. */ | |
481 | lnum = TREE_INT_CST_LOW (n); | |
482 | hnum = TREE_INT_CST_HIGH (n); | |
483 | ||
484 | /* Denominator = 2. */ | |
485 | ldenom = 2; | |
486 | hdenom = 0; | |
487 | ||
488 | /* Index = Numerator-1. */ | |
489 | if (lnum == 0) | |
490 | { | |
491 | hidx = hnum - 1; | |
492 | lidx = ~ (unsigned HOST_WIDE_INT) 0; | |
493 | } | |
c8a2ab6d | 494 | else |
1a9dddad RS |
495 | { |
496 | hidx = hnum; | |
497 | lidx = lnum - 1; | |
498 | } | |
c8a2ab6d | 499 | |
1a9dddad RS |
500 | /* Numerator = Numerator*Index = n*(n-1). */ |
501 | if (mul_double (lnum, hnum, lidx, hidx, &lnum, &hnum)) | |
502 | return NULL_TREE; | |
c8a2ab6d | 503 | |
1a9dddad RS |
504 | for (i = 3; i <= k; i++) |
505 | { | |
506 | /* Index--. */ | |
507 | if (lidx == 0) | |
508 | { | |
509 | hidx--; | |
510 | lidx = ~ (unsigned HOST_WIDE_INT) 0; | |
511 | } | |
512 | else | |
513 | lidx--; | |
514 | ||
515 | /* Numerator *= Index. */ | |
516 | if (mul_double (lnum, hnum, lidx, hidx, &lnum, &hnum)) | |
517 | return NULL_TREE; | |
518 | ||
519 | /* Denominator *= i. */ | |
520 | mul_double (ldenom, hdenom, i, 0, &ldenom, &hdenom); | |
521 | } | |
522 | ||
523 | /* Result = Numerator / Denominator. */ | |
524 | div_and_round_double (EXACT_DIV_EXPR, 1, lnum, hnum, ldenom, hdenom, | |
525 | &lres, &hres, &ldum, &hdum); | |
526 | ||
527 | res = build_int_cst_wide (type, lres, hres); | |
528 | return int_fits_type_p (res, type) ? res : NULL_TREE; | |
c8a2ab6d SP |
529 | } |
530 | ||
531 | /* Helper function. Use the Newton's interpolating formula for | |
532 | evaluating the value of the evolution function. */ | |
533 | ||
b8698a0f | 534 | static tree |
1a9dddad | 535 | chrec_evaluate (unsigned var, tree chrec, tree n, unsigned int k) |
c8a2ab6d | 536 | { |
1a9dddad RS |
537 | tree arg0, arg1, binomial_n_k; |
538 | tree type = TREE_TYPE (chrec); | |
677cc14d | 539 | struct loop *var_loop = get_loop (var); |
1a9dddad RS |
540 | |
541 | while (TREE_CODE (chrec) == POLYNOMIAL_CHREC | |
677cc14d | 542 | && flow_loop_nested_p (var_loop, get_chrec_loop (chrec))) |
1a9dddad RS |
543 | chrec = CHREC_LEFT (chrec); |
544 | ||
545 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC | |
546 | && CHREC_VARIABLE (chrec) == var) | |
c8a2ab6d | 547 | { |
f6ee9fae JJ |
548 | arg1 = chrec_evaluate (var, CHREC_RIGHT (chrec), n, k + 1); |
549 | if (arg1 == chrec_dont_know) | |
1a9dddad RS |
550 | return chrec_dont_know; |
551 | binomial_n_k = tree_fold_binomial (type, n, k); | |
552 | if (!binomial_n_k) | |
553 | return chrec_dont_know; | |
f6ee9fae | 554 | arg0 = fold_build2 (MULT_EXPR, type, |
2412d35c | 555 | CHREC_LEFT (chrec), binomial_n_k); |
1a9dddad | 556 | return chrec_fold_plus (type, arg0, arg1); |
c8a2ab6d | 557 | } |
1a9dddad RS |
558 | |
559 | binomial_n_k = tree_fold_binomial (type, n, k); | |
560 | if (!binomial_n_k) | |
561 | return chrec_dont_know; | |
b8698a0f | 562 | |
2412d35c | 563 | return fold_build2 (MULT_EXPR, type, chrec, binomial_n_k); |
c8a2ab6d SP |
564 | } |
565 | ||
b8698a0f L |
566 | /* Evaluates "CHREC (X)" when the varying variable is VAR. |
567 | Example: Given the following parameters, | |
568 | ||
c8a2ab6d SP |
569 | var = 1 |
570 | chrec = {3, +, 4}_1 | |
571 | x = 10 | |
b8698a0f L |
572 | |
573 | The result is given by the Newton's interpolating formula: | |
c8a2ab6d SP |
574 | 3 * \binom{10}{0} + 4 * \binom{10}{1}. |
575 | */ | |
576 | ||
b8698a0f | 577 | tree |
c8a2ab6d | 578 | chrec_apply (unsigned var, |
b8698a0f | 579 | tree chrec, |
c8a2ab6d SP |
580 | tree x) |
581 | { | |
582 | tree type = chrec_type (chrec); | |
583 | tree res = chrec_dont_know; | |
584 | ||
585 | if (automatically_generated_chrec_p (chrec) | |
586 | || automatically_generated_chrec_p (x) | |
587 | ||
588 | /* When the symbols are defined in an outer loop, it is possible | |
589 | to symbolically compute the apply, since the symbols are | |
590 | constants with respect to the varying loop. */ | |
a6f778b2 | 591 | || chrec_contains_symbols_defined_in_loop (chrec, var)) |
c8a2ab6d | 592 | return chrec_dont_know; |
b8698a0f | 593 | |
dfedbe40 | 594 | if (dump_file && (dump_flags & TDF_SCEV)) |
c8a2ab6d SP |
595 | fprintf (dump_file, "(chrec_apply \n"); |
596 | ||
3c0c8f9d SP |
597 | if (TREE_CODE (x) == INTEGER_CST && SCALAR_FLOAT_TYPE_P (type)) |
598 | x = build_real_from_int_cst (type, x); | |
599 | ||
8b059232 | 600 | switch (TREE_CODE (chrec)) |
c8a2ab6d | 601 | { |
8b059232 SP |
602 | case POLYNOMIAL_CHREC: |
603 | if (evolution_function_is_affine_p (chrec)) | |
604 | { | |
605 | if (CHREC_VARIABLE (chrec) != var) | |
606 | return build_polynomial_chrec | |
607 | (CHREC_VARIABLE (chrec), | |
608 | chrec_apply (var, CHREC_LEFT (chrec), x), | |
609 | chrec_apply (var, CHREC_RIGHT (chrec), x)); | |
610 | ||
611 | /* "{a, +, b} (x)" -> "a + b*x". */ | |
612 | x = chrec_convert_rhs (type, x, NULL); | |
613 | res = chrec_fold_multiply (TREE_TYPE (x), CHREC_RIGHT (chrec), x); | |
614 | res = chrec_fold_plus (type, CHREC_LEFT (chrec), res); | |
615 | } | |
616 | else if (TREE_CODE (x) == INTEGER_CST | |
617 | && tree_int_cst_sgn (x) == 1) | |
618 | /* testsuite/.../ssa-chrec-38.c. */ | |
619 | res = chrec_evaluate (var, chrec, x, 0); | |
620 | else | |
621 | res = chrec_dont_know; | |
622 | break; | |
b8698a0f | 623 | |
8b059232 SP |
624 | CASE_CONVERT: |
625 | res = chrec_convert (TREE_TYPE (chrec), | |
626 | chrec_apply (var, TREE_OPERAND (chrec, 0), x), | |
627 | NULL); | |
628 | break; | |
b8698a0f | 629 | |
8b059232 SP |
630 | default: |
631 | res = chrec; | |
632 | break; | |
633 | } | |
b8698a0f | 634 | |
dfedbe40 | 635 | if (dump_file && (dump_flags & TDF_SCEV)) |
c8a2ab6d SP |
636 | { |
637 | fprintf (dump_file, " (varying_loop = %d\n", var); | |
638 | fprintf (dump_file, ")\n (chrec = "); | |
639 | print_generic_expr (dump_file, chrec, 0); | |
640 | fprintf (dump_file, ")\n (x = "); | |
641 | print_generic_expr (dump_file, x, 0); | |
642 | fprintf (dump_file, ")\n (res = "); | |
643 | print_generic_expr (dump_file, res, 0); | |
644 | fprintf (dump_file, "))\n"); | |
645 | } | |
b8698a0f | 646 | |
c8a2ab6d SP |
647 | return res; |
648 | } | |
649 | ||
2e286fd2 SP |
650 | /* For a given CHREC and an induction variable map IV_MAP that maps |
651 | (loop->num, expr) for every loop number of the current_loops an | |
652 | expression, calls chrec_apply when the expression is not NULL. */ | |
653 | ||
654 | tree | |
655 | chrec_apply_map (tree chrec, VEC (tree, heap) *iv_map) | |
656 | { | |
657 | int i; | |
658 | tree expr; | |
659 | ||
ac47786e | 660 | FOR_EACH_VEC_ELT (tree, iv_map, i, expr) |
2e286fd2 SP |
661 | if (expr) |
662 | chrec = chrec_apply (i, chrec, expr); | |
663 | ||
664 | return chrec; | |
665 | } | |
666 | ||
c8a2ab6d SP |
667 | /* Replaces the initial condition in CHREC with INIT_COND. */ |
668 | ||
b8698a0f L |
669 | tree |
670 | chrec_replace_initial_condition (tree chrec, | |
c8a2ab6d SP |
671 | tree init_cond) |
672 | { | |
673 | if (automatically_generated_chrec_p (chrec)) | |
674 | return chrec; | |
e2157b49 SP |
675 | |
676 | gcc_assert (chrec_type (chrec) == chrec_type (init_cond)); | |
677 | ||
c8a2ab6d SP |
678 | switch (TREE_CODE (chrec)) |
679 | { | |
680 | case POLYNOMIAL_CHREC: | |
b8698a0f | 681 | return build_polynomial_chrec |
c8a2ab6d SP |
682 | (CHREC_VARIABLE (chrec), |
683 | chrec_replace_initial_condition (CHREC_LEFT (chrec), init_cond), | |
684 | CHREC_RIGHT (chrec)); | |
b8698a0f | 685 | |
c8a2ab6d SP |
686 | default: |
687 | return init_cond; | |
688 | } | |
689 | } | |
690 | ||
691 | /* Returns the initial condition of a given CHREC. */ | |
692 | ||
b8698a0f | 693 | tree |
c8a2ab6d SP |
694 | initial_condition (tree chrec) |
695 | { | |
696 | if (automatically_generated_chrec_p (chrec)) | |
697 | return chrec; | |
b8698a0f | 698 | |
c8a2ab6d SP |
699 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC) |
700 | return initial_condition (CHREC_LEFT (chrec)); | |
701 | else | |
702 | return chrec; | |
703 | } | |
704 | ||
705 | /* Returns a univariate function that represents the evolution in | |
706 | LOOP_NUM. Mask the evolution of any other loop. */ | |
707 | ||
b8698a0f L |
708 | tree |
709 | hide_evolution_in_other_loops_than_loop (tree chrec, | |
c8a2ab6d SP |
710 | unsigned loop_num) |
711 | { | |
677cc14d | 712 | struct loop *loop = get_loop (loop_num), *chloop; |
c8a2ab6d SP |
713 | if (automatically_generated_chrec_p (chrec)) |
714 | return chrec; | |
b8698a0f | 715 | |
c8a2ab6d SP |
716 | switch (TREE_CODE (chrec)) |
717 | { | |
718 | case POLYNOMIAL_CHREC: | |
677cc14d ZD |
719 | chloop = get_chrec_loop (chrec); |
720 | ||
721 | if (chloop == loop) | |
b8698a0f L |
722 | return build_polynomial_chrec |
723 | (loop_num, | |
724 | hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec), | |
725 | loop_num), | |
c8a2ab6d | 726 | CHREC_RIGHT (chrec)); |
b8698a0f | 727 | |
677cc14d | 728 | else if (flow_loop_nested_p (chloop, loop)) |
c8a2ab6d SP |
729 | /* There is no evolution in this loop. */ |
730 | return initial_condition (chrec); | |
b8698a0f | 731 | |
c8a2ab6d | 732 | else |
677cc14d ZD |
733 | { |
734 | gcc_assert (flow_loop_nested_p (loop, chloop)); | |
b8698a0f | 735 | return hide_evolution_in_other_loops_than_loop (CHREC_LEFT (chrec), |
677cc14d ZD |
736 | loop_num); |
737 | } | |
b8698a0f | 738 | |
c8a2ab6d SP |
739 | default: |
740 | return chrec; | |
741 | } | |
742 | } | |
743 | ||
6775f1f3 IR |
744 | /* Returns the evolution part of CHREC in LOOP_NUM when RIGHT is |
745 | true, otherwise returns the initial condition in LOOP_NUM. */ | |
c8a2ab6d | 746 | |
b8698a0f L |
747 | static tree |
748 | chrec_component_in_loop_num (tree chrec, | |
6775f1f3 IR |
749 | unsigned loop_num, |
750 | bool right) | |
c8a2ab6d | 751 | { |
6775f1f3 | 752 | tree component; |
677cc14d | 753 | struct loop *loop = get_loop (loop_num), *chloop; |
6775f1f3 | 754 | |
c8a2ab6d SP |
755 | if (automatically_generated_chrec_p (chrec)) |
756 | return chrec; | |
b8698a0f | 757 | |
c8a2ab6d SP |
758 | switch (TREE_CODE (chrec)) |
759 | { | |
760 | case POLYNOMIAL_CHREC: | |
677cc14d ZD |
761 | chloop = get_chrec_loop (chrec); |
762 | ||
763 | if (chloop == loop) | |
c8a2ab6d | 764 | { |
6775f1f3 IR |
765 | if (right) |
766 | component = CHREC_RIGHT (chrec); | |
767 | else | |
768 | component = CHREC_LEFT (chrec); | |
769 | ||
c8a2ab6d SP |
770 | if (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC |
771 | || CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec)) | |
6775f1f3 | 772 | return component; |
b8698a0f | 773 | |
c8a2ab6d SP |
774 | else |
775 | return build_polynomial_chrec | |
b8698a0f L |
776 | (loop_num, |
777 | chrec_component_in_loop_num (CHREC_LEFT (chrec), | |
778 | loop_num, | |
779 | right), | |
6775f1f3 | 780 | component); |
c8a2ab6d | 781 | } |
b8698a0f | 782 | |
677cc14d | 783 | else if (flow_loop_nested_p (chloop, loop)) |
c8a2ab6d SP |
784 | /* There is no evolution part in this loop. */ |
785 | return NULL_TREE; | |
b8698a0f | 786 | |
c8a2ab6d | 787 | else |
677cc14d ZD |
788 | { |
789 | gcc_assert (flow_loop_nested_p (loop, chloop)); | |
b8698a0f L |
790 | return chrec_component_in_loop_num (CHREC_LEFT (chrec), |
791 | loop_num, | |
677cc14d ZD |
792 | right); |
793 | } | |
b8698a0f | 794 | |
6775f1f3 IR |
795 | default: |
796 | if (right) | |
797 | return NULL_TREE; | |
798 | else | |
799 | return chrec; | |
c8a2ab6d SP |
800 | } |
801 | } | |
802 | ||
6775f1f3 | 803 | /* Returns the evolution part in LOOP_NUM. Example: the call |
b8698a0f | 804 | evolution_part_in_loop_num ({{0, +, 1}_1, +, 2}_1, 1) returns |
6775f1f3 IR |
805 | {1, +, 2}_1 */ |
806 | ||
b8698a0f L |
807 | tree |
808 | evolution_part_in_loop_num (tree chrec, | |
6775f1f3 IR |
809 | unsigned loop_num) |
810 | { | |
811 | return chrec_component_in_loop_num (chrec, loop_num, true); | |
812 | } | |
813 | ||
814 | /* Returns the initial condition in LOOP_NUM. Example: the call | |
b8698a0f | 815 | initial_condition_in_loop_num ({{0, +, 1}_1, +, 2}_2, 2) returns |
6775f1f3 IR |
816 | {0, +, 1}_1 */ |
817 | ||
b8698a0f L |
818 | tree |
819 | initial_condition_in_loop_num (tree chrec, | |
6775f1f3 IR |
820 | unsigned loop_num) |
821 | { | |
822 | return chrec_component_in_loop_num (chrec, loop_num, false); | |
823 | } | |
824 | ||
c8a2ab6d SP |
825 | /* Set or reset the evolution of CHREC to NEW_EVOL in loop LOOP_NUM. |
826 | This function is essentially used for setting the evolution to | |
827 | chrec_dont_know, for example after having determined that it is | |
828 | impossible to say how many times a loop will execute. */ | |
829 | ||
b8698a0f | 830 | tree |
c8a2ab6d | 831 | reset_evolution_in_loop (unsigned loop_num, |
b8698a0f | 832 | tree chrec, |
c8a2ab6d SP |
833 | tree new_evol) |
834 | { | |
677cc14d ZD |
835 | struct loop *loop = get_loop (loop_num); |
836 | ||
5be014d5 | 837 | if (POINTER_TYPE_P (chrec_type (chrec))) |
370f4759 | 838 | gcc_assert (ptrofftype_p (chrec_type (new_evol))); |
5be014d5 AP |
839 | else |
840 | gcc_assert (chrec_type (chrec) == chrec_type (new_evol)); | |
e2157b49 | 841 | |
c8a2ab6d | 842 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC |
677cc14d | 843 | && flow_loop_nested_p (loop, get_chrec_loop (chrec))) |
6be74c4f JJ |
844 | { |
845 | tree left = reset_evolution_in_loop (loop_num, CHREC_LEFT (chrec), | |
846 | new_evol); | |
847 | tree right = reset_evolution_in_loop (loop_num, CHREC_RIGHT (chrec), | |
848 | new_evol); | |
849 | return build3 (POLYNOMIAL_CHREC, TREE_TYPE (left), | |
413581ba | 850 | CHREC_VAR (chrec), left, right); |
6be74c4f JJ |
851 | } |
852 | ||
c8a2ab6d SP |
853 | while (TREE_CODE (chrec) == POLYNOMIAL_CHREC |
854 | && CHREC_VARIABLE (chrec) == loop_num) | |
855 | chrec = CHREC_LEFT (chrec); | |
b8698a0f | 856 | |
c8a2ab6d SP |
857 | return build_polynomial_chrec (loop_num, chrec, new_evol); |
858 | } | |
859 | ||
860 | /* Merges two evolution functions that were found by following two | |
861 | alternate paths of a conditional expression. */ | |
862 | ||
863 | tree | |
b8698a0f | 864 | chrec_merge (tree chrec1, |
c8a2ab6d SP |
865 | tree chrec2) |
866 | { | |
867 | if (chrec1 == chrec_dont_know | |
868 | || chrec2 == chrec_dont_know) | |
869 | return chrec_dont_know; | |
870 | ||
b8698a0f | 871 | if (chrec1 == chrec_known |
c8a2ab6d SP |
872 | || chrec2 == chrec_known) |
873 | return chrec_known; | |
874 | ||
875 | if (chrec1 == chrec_not_analyzed_yet) | |
876 | return chrec2; | |
877 | if (chrec2 == chrec_not_analyzed_yet) | |
878 | return chrec1; | |
879 | ||
ace23abf | 880 | if (eq_evolutions_p (chrec1, chrec2)) |
c8a2ab6d SP |
881 | return chrec1; |
882 | ||
883 | return chrec_dont_know; | |
884 | } | |
885 | ||
886 | \f | |
887 | ||
888 | /* Observers. */ | |
889 | ||
890 | /* Helper function for is_multivariate_chrec. */ | |
891 | ||
b8698a0f | 892 | static bool |
ed7a4b4b | 893 | is_multivariate_chrec_rec (const_tree chrec, unsigned int rec_var) |
c8a2ab6d SP |
894 | { |
895 | if (chrec == NULL_TREE) | |
896 | return false; | |
b8698a0f | 897 | |
c8a2ab6d SP |
898 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC) |
899 | { | |
900 | if (CHREC_VARIABLE (chrec) != rec_var) | |
901 | return true; | |
902 | else | |
b8698a0f | 903 | return (is_multivariate_chrec_rec (CHREC_LEFT (chrec), rec_var) |
c8a2ab6d SP |
904 | || is_multivariate_chrec_rec (CHREC_RIGHT (chrec), rec_var)); |
905 | } | |
906 | else | |
907 | return false; | |
908 | } | |
909 | ||
910 | /* Determine whether the given chrec is multivariate or not. */ | |
911 | ||
b8698a0f | 912 | bool |
ed7a4b4b | 913 | is_multivariate_chrec (const_tree chrec) |
c8a2ab6d SP |
914 | { |
915 | if (chrec == NULL_TREE) | |
916 | return false; | |
b8698a0f | 917 | |
c8a2ab6d | 918 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC) |
b8698a0f | 919 | return (is_multivariate_chrec_rec (CHREC_LEFT (chrec), |
c8a2ab6d | 920 | CHREC_VARIABLE (chrec)) |
b8698a0f | 921 | || is_multivariate_chrec_rec (CHREC_RIGHT (chrec), |
c8a2ab6d SP |
922 | CHREC_VARIABLE (chrec))); |
923 | else | |
924 | return false; | |
925 | } | |
926 | ||
927 | /* Determines whether the chrec contains symbolic names or not. */ | |
928 | ||
b8698a0f | 929 | bool |
ed7a4b4b | 930 | chrec_contains_symbols (const_tree chrec) |
c8a2ab6d | 931 | { |
5039610b SL |
932 | int i, n; |
933 | ||
c8a2ab6d SP |
934 | if (chrec == NULL_TREE) |
935 | return false; | |
b8698a0f | 936 | |
c8a2ab6d SP |
937 | if (TREE_CODE (chrec) == SSA_NAME |
938 | || TREE_CODE (chrec) == VAR_DECL | |
939 | || TREE_CODE (chrec) == PARM_DECL | |
940 | || TREE_CODE (chrec) == FUNCTION_DECL | |
941 | || TREE_CODE (chrec) == LABEL_DECL | |
942 | || TREE_CODE (chrec) == RESULT_DECL | |
943 | || TREE_CODE (chrec) == FIELD_DECL) | |
944 | return true; | |
5039610b SL |
945 | |
946 | n = TREE_OPERAND_LENGTH (chrec); | |
947 | for (i = 0; i < n; i++) | |
948 | if (chrec_contains_symbols (TREE_OPERAND (chrec, i))) | |
949 | return true; | |
950 | return false; | |
c8a2ab6d SP |
951 | } |
952 | ||
953 | /* Determines whether the chrec contains undetermined coefficients. */ | |
954 | ||
b8698a0f | 955 | bool |
ed7a4b4b | 956 | chrec_contains_undetermined (const_tree chrec) |
c8a2ab6d | 957 | { |
5039610b SL |
958 | int i, n; |
959 | ||
e71d7f88 | 960 | if (chrec == chrec_dont_know) |
c8a2ab6d | 961 | return true; |
5039610b | 962 | |
e71d7f88 ZD |
963 | if (chrec == NULL_TREE) |
964 | return false; | |
965 | ||
5039610b SL |
966 | n = TREE_OPERAND_LENGTH (chrec); |
967 | for (i = 0; i < n; i++) | |
968 | if (chrec_contains_undetermined (TREE_OPERAND (chrec, i))) | |
969 | return true; | |
970 | return false; | |
c8a2ab6d SP |
971 | } |
972 | ||
2412d35c SP |
973 | /* Determines whether the tree EXPR contains chrecs, and increment |
974 | SIZE if it is not a NULL pointer by an estimation of the depth of | |
975 | the tree. */ | |
c8a2ab6d SP |
976 | |
977 | bool | |
ed7a4b4b | 978 | tree_contains_chrecs (const_tree expr, int *size) |
c8a2ab6d | 979 | { |
5039610b SL |
980 | int i, n; |
981 | ||
c8a2ab6d SP |
982 | if (expr == NULL_TREE) |
983 | return false; | |
2412d35c SP |
984 | |
985 | if (size) | |
986 | (*size)++; | |
b8698a0f | 987 | |
c8a2ab6d SP |
988 | if (tree_is_chrec (expr)) |
989 | return true; | |
2412d35c | 990 | |
5039610b SL |
991 | n = TREE_OPERAND_LENGTH (expr); |
992 | for (i = 0; i < n; i++) | |
993 | if (tree_contains_chrecs (TREE_OPERAND (expr, i), size)) | |
994 | return true; | |
995 | return false; | |
c8a2ab6d SP |
996 | } |
997 | ||
1e8552eb SP |
998 | /* Recursive helper function. */ |
999 | ||
1000 | static bool | |
1001 | evolution_function_is_invariant_rec_p (tree chrec, int loopnum) | |
1002 | { | |
1003 | if (evolution_function_is_constant_p (chrec)) | |
1004 | return true; | |
1005 | ||
6a732743 SP |
1006 | if (TREE_CODE (chrec) == SSA_NAME |
1007 | && (loopnum == 0 | |
1008 | || expr_invariant_in_loop_p (get_loop (loopnum), chrec))) | |
1e8552eb SP |
1009 | return true; |
1010 | ||
7ce7896c SP |
1011 | if (TREE_CODE (chrec) == POLYNOMIAL_CHREC) |
1012 | { | |
1013 | if (CHREC_VARIABLE (chrec) == (unsigned) loopnum | |
754870ed RG |
1014 | || flow_loop_nested_p (get_loop (loopnum), |
1015 | get_loop (CHREC_VARIABLE (chrec))) | |
7ce7896c SP |
1016 | || !evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), |
1017 | loopnum) | |
1018 | || !evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec), | |
1019 | loopnum)) | |
1020 | return false; | |
1021 | return true; | |
1022 | } | |
1e8552eb | 1023 | |
5039610b | 1024 | switch (TREE_OPERAND_LENGTH (chrec)) |
1e8552eb SP |
1025 | { |
1026 | case 2: | |
1027 | if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 1), | |
1028 | loopnum)) | |
1029 | return false; | |
b8698a0f | 1030 | |
1e8552eb SP |
1031 | case 1: |
1032 | if (!evolution_function_is_invariant_rec_p (TREE_OPERAND (chrec, 0), | |
1033 | loopnum)) | |
1034 | return false; | |
1035 | return true; | |
1036 | ||
1037 | default: | |
1038 | return false; | |
1039 | } | |
1040 | ||
1041 | return false; | |
1042 | } | |
1043 | ||
1044 | /* Return true if CHREC is invariant in loop LOOPNUM, false otherwise. */ | |
1045 | ||
1046 | bool | |
1047 | evolution_function_is_invariant_p (tree chrec, int loopnum) | |
1048 | { | |
d51157de | 1049 | return evolution_function_is_invariant_rec_p (chrec, loopnum); |
1e8552eb SP |
1050 | } |
1051 | ||
c8a2ab6d SP |
1052 | /* Determine whether the given tree is an affine multivariate |
1053 | evolution. */ | |
1054 | ||
b8698a0f | 1055 | bool |
ed7a4b4b | 1056 | evolution_function_is_affine_multivariate_p (const_tree chrec, int loopnum) |
c8a2ab6d SP |
1057 | { |
1058 | if (chrec == NULL_TREE) | |
1059 | return false; | |
b8698a0f | 1060 | |
c8a2ab6d SP |
1061 | switch (TREE_CODE (chrec)) |
1062 | { | |
1063 | case POLYNOMIAL_CHREC: | |
a50411de | 1064 | if (evolution_function_is_invariant_rec_p (CHREC_LEFT (chrec), loopnum)) |
c8a2ab6d | 1065 | { |
a50411de | 1066 | if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum)) |
c8a2ab6d SP |
1067 | return true; |
1068 | else | |
1069 | { | |
1070 | if (TREE_CODE (CHREC_RIGHT (chrec)) == POLYNOMIAL_CHREC | |
b8698a0f | 1071 | && CHREC_VARIABLE (CHREC_RIGHT (chrec)) |
c8a2ab6d | 1072 | != CHREC_VARIABLE (chrec) |
b8698a0f | 1073 | && evolution_function_is_affine_multivariate_p |
a50411de | 1074 | (CHREC_RIGHT (chrec), loopnum)) |
c8a2ab6d SP |
1075 | return true; |
1076 | else | |
1077 | return false; | |
1078 | } | |
1079 | } | |
1080 | else | |
1081 | { | |
a50411de | 1082 | if (evolution_function_is_invariant_rec_p (CHREC_RIGHT (chrec), loopnum) |
c8a2ab6d SP |
1083 | && TREE_CODE (CHREC_LEFT (chrec)) == POLYNOMIAL_CHREC |
1084 | && CHREC_VARIABLE (CHREC_LEFT (chrec)) != CHREC_VARIABLE (chrec) | |
b8698a0f | 1085 | && evolution_function_is_affine_multivariate_p |
a50411de | 1086 | (CHREC_LEFT (chrec), loopnum)) |
c8a2ab6d SP |
1087 | return true; |
1088 | else | |
1089 | return false; | |
1090 | } | |
b8698a0f | 1091 | |
c8a2ab6d SP |
1092 | default: |
1093 | return false; | |
1094 | } | |
1095 | } | |
1096 | ||
b8698a0f | 1097 | /* Determine whether the given tree is a function in zero or one |
c8a2ab6d SP |
1098 | variables. */ |
1099 | ||
1100 | bool | |
ed7a4b4b | 1101 | evolution_function_is_univariate_p (const_tree chrec) |
c8a2ab6d SP |
1102 | { |
1103 | if (chrec == NULL_TREE) | |
1104 | return true; | |
b8698a0f | 1105 | |
c8a2ab6d SP |
1106 | switch (TREE_CODE (chrec)) |
1107 | { | |
1108 | case POLYNOMIAL_CHREC: | |
1109 | switch (TREE_CODE (CHREC_LEFT (chrec))) | |
1110 | { | |
1111 | case POLYNOMIAL_CHREC: | |
1112 | if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (CHREC_LEFT (chrec))) | |
1113 | return false; | |
1114 | if (!evolution_function_is_univariate_p (CHREC_LEFT (chrec))) | |
1115 | return false; | |
1116 | break; | |
b8698a0f | 1117 | |
c8a2ab6d | 1118 | default: |
754870ed RG |
1119 | if (tree_contains_chrecs (CHREC_LEFT (chrec), NULL)) |
1120 | return false; | |
c8a2ab6d SP |
1121 | break; |
1122 | } | |
b8698a0f | 1123 | |
c8a2ab6d SP |
1124 | switch (TREE_CODE (CHREC_RIGHT (chrec))) |
1125 | { | |
1126 | case POLYNOMIAL_CHREC: | |
1127 | if (CHREC_VARIABLE (chrec) != CHREC_VARIABLE (CHREC_RIGHT (chrec))) | |
1128 | return false; | |
1129 | if (!evolution_function_is_univariate_p (CHREC_RIGHT (chrec))) | |
1130 | return false; | |
1131 | break; | |
b8698a0f | 1132 | |
c8a2ab6d | 1133 | default: |
754870ed RG |
1134 | if (tree_contains_chrecs (CHREC_RIGHT (chrec), NULL)) |
1135 | return false; | |
b8698a0f | 1136 | break; |
c8a2ab6d | 1137 | } |
b8698a0f | 1138 | |
c8a2ab6d SP |
1139 | default: |
1140 | return true; | |
1141 | } | |
1142 | } | |
1143 | ||
86df10e3 SP |
1144 | /* Returns the number of variables of CHREC. Example: the call |
1145 | nb_vars_in_chrec ({{0, +, 1}_5, +, 2}_6) returns 2. */ | |
1146 | ||
b8698a0f | 1147 | unsigned |
86df10e3 SP |
1148 | nb_vars_in_chrec (tree chrec) |
1149 | { | |
1150 | if (chrec == NULL_TREE) | |
1151 | return 0; | |
1152 | ||
1153 | switch (TREE_CODE (chrec)) | |
1154 | { | |
1155 | case POLYNOMIAL_CHREC: | |
b8698a0f | 1156 | return 1 + nb_vars_in_chrec |
86df10e3 SP |
1157 | (initial_condition_in_loop_num (chrec, CHREC_VARIABLE (chrec))); |
1158 | ||
1159 | default: | |
1160 | return 0; | |
1161 | } | |
1162 | } | |
1163 | ||
726a989a | 1164 | static tree chrec_convert_1 (tree, tree, gimple, bool); |
d7f5de76 ZD |
1165 | |
1166 | /* Converts BASE and STEP of affine scev to TYPE. LOOP is the loop whose iv | |
1167 | the scev corresponds to. AT_STMT is the statement at that the scev is | |
1168 | evaluated. USE_OVERFLOW_SEMANTICS is true if this function should assume that | |
1169 | the rules for overflow of the given language apply (e.g., that signed | |
1170 | arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary | |
1171 | tests, but also to enforce that the result follows them. Returns true if the | |
1172 | conversion succeeded, false otherwise. */ | |
1173 | ||
1174 | bool | |
1175 | convert_affine_scev (struct loop *loop, tree type, | |
726a989a | 1176 | tree *base, tree *step, gimple at_stmt, |
d7f5de76 ZD |
1177 | bool use_overflow_semantics) |
1178 | { | |
1179 | tree ct = TREE_TYPE (*step); | |
1180 | bool enforce_overflow_semantics; | |
1181 | bool must_check_src_overflow, must_check_rslt_overflow; | |
1182 | tree new_base, new_step; | |
5be014d5 | 1183 | tree step_type = POINTER_TYPE_P (type) ? sizetype : type; |
d7f5de76 ZD |
1184 | |
1185 | /* In general, | |
1186 | (TYPE) (BASE + STEP * i) = (TYPE) BASE + (TYPE -- sign extend) STEP * i, | |
1187 | but we must check some assumptions. | |
b8698a0f | 1188 | |
d7f5de76 ZD |
1189 | 1) If [BASE, +, STEP] wraps, the equation is not valid when precision |
1190 | of CT is smaller than the precision of TYPE. For example, when we | |
1191 | cast unsigned char [254, +, 1] to unsigned, the values on left side | |
1192 | are 254, 255, 0, 1, ..., but those on the right side are | |
1193 | 254, 255, 256, 257, ... | |
1194 | 2) In case that we must also preserve the fact that signed ivs do not | |
1195 | overflow, we must additionally check that the new iv does not wrap. | |
1196 | For example, unsigned char [125, +, 1] casted to signed char could | |
1197 | become a wrapping variable with values 125, 126, 127, -128, -127, ..., | |
1198 | which would confuse optimizers that assume that this does not | |
1199 | happen. */ | |
1200 | must_check_src_overflow = TYPE_PRECISION (ct) < TYPE_PRECISION (type); | |
1201 | ||
1202 | enforce_overflow_semantics = (use_overflow_semantics | |
1203 | && nowrap_type_p (type)); | |
1204 | if (enforce_overflow_semantics) | |
1205 | { | |
1206 | /* We can avoid checking whether the result overflows in the following | |
1207 | cases: | |
1208 | ||
1209 | -- must_check_src_overflow is true, and the range of TYPE is superset | |
1210 | of the range of CT -- i.e., in all cases except if CT signed and | |
1211 | TYPE unsigned. | |
20527215 ZD |
1212 | -- both CT and TYPE have the same precision and signedness, and we |
1213 | verify instead that the source does not overflow (this may be | |
1214 | easier than verifying it for the result, as we may use the | |
1215 | information about the semantics of overflow in CT). */ | |
d7f5de76 ZD |
1216 | if (must_check_src_overflow) |
1217 | { | |
1218 | if (TYPE_UNSIGNED (type) && !TYPE_UNSIGNED (ct)) | |
1219 | must_check_rslt_overflow = true; | |
1220 | else | |
1221 | must_check_rslt_overflow = false; | |
1222 | } | |
1223 | else if (TYPE_UNSIGNED (ct) == TYPE_UNSIGNED (type) | |
1224 | && TYPE_PRECISION (ct) == TYPE_PRECISION (type)) | |
20527215 ZD |
1225 | { |
1226 | must_check_rslt_overflow = false; | |
1227 | must_check_src_overflow = true; | |
1228 | } | |
d7f5de76 ZD |
1229 | else |
1230 | must_check_rslt_overflow = true; | |
1231 | } | |
1232 | else | |
1233 | must_check_rslt_overflow = false; | |
1234 | ||
1235 | if (must_check_src_overflow | |
1236 | && scev_probably_wraps_p (*base, *step, at_stmt, loop, | |
1237 | use_overflow_semantics)) | |
1238 | return false; | |
1239 | ||
1240 | new_base = chrec_convert_1 (type, *base, at_stmt, | |
1241 | use_overflow_semantics); | |
1242 | /* The step must be sign extended, regardless of the signedness | |
1243 | of CT and TYPE. This only needs to be handled specially when | |
1244 | CT is unsigned -- to avoid e.g. unsigned char [100, +, 255] | |
1245 | (with values 100, 99, 98, ...) from becoming signed or unsigned | |
b8698a0f | 1246 | [100, +, 255] with values 100, 355, ...; the sign-extension is |
d7f5de76 ZD |
1247 | performed by default when CT is signed. */ |
1248 | new_step = *step; | |
5be014d5 | 1249 | if (TYPE_PRECISION (step_type) > TYPE_PRECISION (ct) && TYPE_UNSIGNED (ct)) |
1bbc6693 XDL |
1250 | { |
1251 | tree signed_ct = build_nonstandard_integer_type (TYPE_PRECISION (ct), 0); | |
1252 | new_step = chrec_convert_1 (signed_ct, new_step, at_stmt, | |
1253 | use_overflow_semantics); | |
1254 | } | |
5be014d5 | 1255 | new_step = chrec_convert_1 (step_type, new_step, at_stmt, use_overflow_semantics); |
d7f5de76 ZD |
1256 | |
1257 | if (automatically_generated_chrec_p (new_base) | |
1258 | || automatically_generated_chrec_p (new_step)) | |
1259 | return false; | |
1260 | ||
1261 | if (must_check_rslt_overflow | |
1262 | /* Note that in this case we cannot use the fact that signed variables | |
1263 | do not overflow, as this is what we are verifying for the new iv. */ | |
1264 | && scev_probably_wraps_p (new_base, new_step, at_stmt, loop, false)) | |
1265 | return false; | |
1266 | ||
1267 | *base = new_base; | |
1268 | *step = new_step; | |
1269 | return true; | |
1270 | } | |
c8a2ab6d SP |
1271 | \f |
1272 | ||
072edf07 | 1273 | /* Convert CHREC for the right hand side of a CHREC. |
5be014d5 | 1274 | The increment for a pointer type is always sizetype. */ |
072edf07 | 1275 | |
b8698a0f | 1276 | tree |
726a989a | 1277 | chrec_convert_rhs (tree type, tree chrec, gimple at_stmt) |
5be014d5 AP |
1278 | { |
1279 | if (POINTER_TYPE_P (type)) | |
072edf07 SP |
1280 | type = sizetype; |
1281 | ||
5be014d5 AP |
1282 | return chrec_convert (type, chrec, at_stmt); |
1283 | } | |
1284 | ||
1e8552eb SP |
1285 | /* Convert CHREC to TYPE. When the analyzer knows the context in |
1286 | which the CHREC is built, it sets AT_STMT to the statement that | |
1287 | contains the definition of the analyzed variable, otherwise the | |
1288 | conversion is less accurate: the information is used for | |
1289 | determining a more accurate estimation of the number of iterations. | |
1290 | By default AT_STMT could be safely set to NULL_TREE. | |
1291 | ||
1292 | The following rule is always true: TREE_TYPE (chrec) == | |
1293 | TREE_TYPE (CHREC_LEFT (chrec)) == TREE_TYPE (CHREC_RIGHT (chrec)). | |
1294 | An example of what could happen when adding two chrecs and the type | |
1295 | of the CHREC_RIGHT is different than CHREC_LEFT is: | |
b8698a0f | 1296 | |
c4cdbeb4 EB |
1297 | {(uint) 0, +, (uchar) 10} + |
1298 | {(uint) 0, +, (uchar) 250} | |
b8698a0f | 1299 | |
c4cdbeb4 | 1300 | that would produce a wrong result if CHREC_RIGHT is not (uint): |
b8698a0f | 1301 | |
c4cdbeb4 EB |
1302 | {(uint) 0, +, (uchar) 4} |
1303 | ||
1304 | instead of | |
1305 | ||
1306 | {(uint) 0, +, (uint) 260} | |
1307 | */ | |
c8a2ab6d | 1308 | |
b8698a0f | 1309 | tree |
726a989a | 1310 | chrec_convert (tree type, tree chrec, gimple at_stmt) |
d7f5de76 ZD |
1311 | { |
1312 | return chrec_convert_1 (type, chrec, at_stmt, true); | |
1313 | } | |
1314 | ||
1315 | /* Convert CHREC to TYPE. When the analyzer knows the context in | |
1316 | which the CHREC is built, it sets AT_STMT to the statement that | |
1317 | contains the definition of the analyzed variable, otherwise the | |
1318 | conversion is less accurate: the information is used for | |
1319 | determining a more accurate estimation of the number of iterations. | |
1320 | By default AT_STMT could be safely set to NULL_TREE. | |
b8698a0f | 1321 | |
d7f5de76 ZD |
1322 | USE_OVERFLOW_SEMANTICS is true if this function should assume that |
1323 | the rules for overflow of the given language apply (e.g., that signed | |
1324 | arithmetics in C does not overflow) -- i.e., to use them to avoid unnecessary | |
1325 | tests, but also to enforce that the result follows them. */ | |
1326 | ||
b8698a0f | 1327 | static tree |
726a989a | 1328 | chrec_convert_1 (tree type, tree chrec, gimple at_stmt, |
d7f5de76 | 1329 | bool use_overflow_semantics) |
c8a2ab6d | 1330 | { |
1e8552eb | 1331 | tree ct, res; |
d7f5de76 ZD |
1332 | tree base, step; |
1333 | struct loop *loop; | |
1e8552eb | 1334 | |
c8a2ab6d SP |
1335 | if (automatically_generated_chrec_p (chrec)) |
1336 | return chrec; | |
b8698a0f | 1337 | |
c8a2ab6d SP |
1338 | ct = chrec_type (chrec); |
1339 | if (ct == type) | |
1340 | return chrec; | |
1341 | ||
d7f5de76 ZD |
1342 | if (!evolution_function_is_affine_p (chrec)) |
1343 | goto keep_cast; | |
18aed06a | 1344 | |
42fd6772 | 1345 | loop = get_chrec_loop (chrec); |
d7f5de76 ZD |
1346 | base = CHREC_LEFT (chrec); |
1347 | step = CHREC_RIGHT (chrec); | |
1e8552eb | 1348 | |
d7f5de76 ZD |
1349 | if (convert_affine_scev (loop, type, &base, &step, at_stmt, |
1350 | use_overflow_semantics)) | |
1351 | return build_polynomial_chrec (loop->num, base, step); | |
c8a2ab6d | 1352 | |
d7f5de76 ZD |
1353 | /* If we cannot propagate the cast inside the chrec, just keep the cast. */ |
1354 | keep_cast: | |
d73fb533 RG |
1355 | /* Fold will not canonicalize (long)(i - 1) to (long)i - 1 because that |
1356 | may be more expensive. We do want to perform this optimization here | |
1357 | though for canonicalization reasons. */ | |
1358 | if (use_overflow_semantics | |
1359 | && (TREE_CODE (chrec) == PLUS_EXPR | |
1360 | || TREE_CODE (chrec) == MINUS_EXPR) | |
dc01fed1 JJ |
1361 | && TREE_CODE (type) == INTEGER_TYPE |
1362 | && TREE_CODE (ct) == INTEGER_TYPE | |
d73fb533 RG |
1363 | && TYPE_PRECISION (type) > TYPE_PRECISION (ct) |
1364 | && TYPE_OVERFLOW_UNDEFINED (ct)) | |
1365 | res = fold_build2 (TREE_CODE (chrec), type, | |
1366 | fold_convert (type, TREE_OPERAND (chrec, 0)), | |
1367 | fold_convert (type, TREE_OPERAND (chrec, 1))); | |
195b4c50 RG |
1368 | /* Similar perform the trick that (signed char)((int)x + 2) can be |
1369 | narrowed to (signed char)((unsigned char)x + 2). */ | |
1370 | else if (use_overflow_semantics | |
1371 | && TREE_CODE (chrec) == POLYNOMIAL_CHREC | |
1372 | && TREE_CODE (ct) == INTEGER_TYPE | |
1373 | && TREE_CODE (type) == INTEGER_TYPE | |
1374 | && TYPE_OVERFLOW_UNDEFINED (type) | |
1375 | && TYPE_PRECISION (type) < TYPE_PRECISION (ct)) | |
1376 | { | |
1377 | tree utype = unsigned_type_for (type); | |
1378 | res = build_polynomial_chrec (CHREC_VARIABLE (chrec), | |
1379 | fold_convert (utype, | |
1380 | CHREC_LEFT (chrec)), | |
1381 | fold_convert (utype, | |
1382 | CHREC_RIGHT (chrec))); | |
1383 | res = chrec_convert_1 (type, res, at_stmt, use_overflow_semantics); | |
1384 | } | |
d73fb533 RG |
1385 | else |
1386 | res = fold_convert (type, chrec); | |
c4cdbeb4 | 1387 | |
1e8552eb SP |
1388 | /* Don't propagate overflows. */ |
1389 | if (CONSTANT_CLASS_P (res)) | |
455f14dd | 1390 | TREE_OVERFLOW (res) = 0; |
1e8552eb SP |
1391 | |
1392 | /* But reject constants that don't fit in their type after conversion. | |
1393 | This can happen if TYPE_MIN_VALUE or TYPE_MAX_VALUE are not the | |
1394 | natural values associated with TYPE_PRECISION and TYPE_UNSIGNED, | |
1395 | and can cause problems later when computing niters of loops. Note | |
1396 | that we don't do the check before converting because we don't want | |
1397 | to reject conversions of negative chrecs to unsigned types. */ | |
1398 | if (TREE_CODE (res) == INTEGER_CST | |
1399 | && TREE_CODE (type) == INTEGER_TYPE | |
1400 | && !int_fits_type_p (res, type)) | |
1401 | res = chrec_dont_know; | |
1402 | ||
1403 | return res; | |
c8a2ab6d SP |
1404 | } |
1405 | ||
2282a0e6 ZD |
1406 | /* Convert CHREC to TYPE, without regard to signed overflows. Returns the new |
1407 | chrec if something else than what chrec_convert would do happens, NULL_TREE | |
1408 | otherwise. */ | |
1409 | ||
1410 | tree | |
1411 | chrec_convert_aggressive (tree type, tree chrec) | |
1412 | { | |
5be014d5 | 1413 | tree inner_type, left, right, lc, rc, rtype; |
2282a0e6 ZD |
1414 | |
1415 | if (automatically_generated_chrec_p (chrec) | |
1416 | || TREE_CODE (chrec) != POLYNOMIAL_CHREC) | |
1417 | return NULL_TREE; | |
1418 | ||
1419 | inner_type = TREE_TYPE (chrec); | |
1420 | if (TYPE_PRECISION (type) > TYPE_PRECISION (inner_type)) | |
1421 | return NULL_TREE; | |
1422 | ||
5be014d5 AP |
1423 | rtype = POINTER_TYPE_P (type) ? sizetype : type; |
1424 | ||
2282a0e6 ZD |
1425 | left = CHREC_LEFT (chrec); |
1426 | right = CHREC_RIGHT (chrec); | |
1427 | lc = chrec_convert_aggressive (type, left); | |
1428 | if (!lc) | |
726a989a | 1429 | lc = chrec_convert (type, left, NULL); |
5be014d5 | 1430 | rc = chrec_convert_aggressive (rtype, right); |
2282a0e6 | 1431 | if (!rc) |
726a989a | 1432 | rc = chrec_convert (rtype, right, NULL); |
b8698a0f | 1433 | |
2282a0e6 ZD |
1434 | return build_polynomial_chrec (CHREC_VARIABLE (chrec), lc, rc); |
1435 | } | |
1436 | ||
0ff4040e SP |
1437 | /* Returns true when CHREC0 == CHREC1. */ |
1438 | ||
b8698a0f | 1439 | bool |
ed7a4b4b | 1440 | eq_evolutions_p (const_tree chrec0, const_tree chrec1) |
0ff4040e SP |
1441 | { |
1442 | if (chrec0 == NULL_TREE | |
1443 | || chrec1 == NULL_TREE | |
1444 | || TREE_CODE (chrec0) != TREE_CODE (chrec1)) | |
1445 | return false; | |
1446 | ||
1447 | if (chrec0 == chrec1) | |
1448 | return true; | |
1449 | ||
1450 | switch (TREE_CODE (chrec0)) | |
1451 | { | |
1452 | case INTEGER_CST: | |
e2157b49 SP |
1453 | return operand_equal_p (chrec0, chrec1, 0); |
1454 | ||
0ff4040e SP |
1455 | case POLYNOMIAL_CHREC: |
1456 | return (CHREC_VARIABLE (chrec0) == CHREC_VARIABLE (chrec1) | |
1457 | && eq_evolutions_p (CHREC_LEFT (chrec0), CHREC_LEFT (chrec1)) | |
1458 | && eq_evolutions_p (CHREC_RIGHT (chrec0), CHREC_RIGHT (chrec1))); | |
2bdea539 MM |
1459 | |
1460 | case PLUS_EXPR: | |
1461 | case MULT_EXPR: | |
1462 | case MINUS_EXPR: | |
1463 | case POINTER_PLUS_EXPR: | |
1464 | return eq_evolutions_p (TREE_OPERAND (chrec0, 0), | |
1465 | TREE_OPERAND (chrec1, 0)) | |
1466 | && eq_evolutions_p (TREE_OPERAND (chrec0, 1), | |
1467 | TREE_OPERAND (chrec1, 1)); | |
1468 | ||
0ff4040e SP |
1469 | default: |
1470 | return false; | |
b8698a0f | 1471 | } |
0ff4040e SP |
1472 | } |
1473 | ||
d7f5de76 ZD |
1474 | /* Returns EV_GROWS if CHREC grows (assuming that it does not overflow), |
1475 | EV_DECREASES if it decreases, and EV_UNKNOWN if we cannot determine | |
1476 | which of these cases happens. */ | |
1477 | ||
1478 | enum ev_direction | |
ed7a4b4b | 1479 | scev_direction (const_tree chrec) |
d7f5de76 | 1480 | { |
ed7a4b4b | 1481 | const_tree step; |
d7f5de76 ZD |
1482 | |
1483 | if (!evolution_function_is_affine_p (chrec)) | |
1484 | return EV_DIR_UNKNOWN; | |
1485 | ||
1486 | step = CHREC_RIGHT (chrec); | |
1487 | if (TREE_CODE (step) != INTEGER_CST) | |
1488 | return EV_DIR_UNKNOWN; | |
1489 | ||
1490 | if (tree_int_cst_sign_bit (step)) | |
1491 | return EV_DIR_DECREASES; | |
1492 | else | |
1493 | return EV_DIR_GROWS; | |
1494 | } | |
f8bf9252 SP |
1495 | |
1496 | /* Iterates over all the components of SCEV, and calls CBCK. */ | |
1497 | ||
1498 | void | |
1499 | for_each_scev_op (tree *scev, bool (*cbck) (tree *, void *), void *data) | |
1500 | { | |
1501 | switch (TREE_CODE_LENGTH (TREE_CODE (*scev))) | |
1502 | { | |
1503 | case 3: | |
1504 | for_each_scev_op (&TREE_OPERAND (*scev, 2), cbck, data); | |
1505 | ||
1506 | case 2: | |
1507 | for_each_scev_op (&TREE_OPERAND (*scev, 1), cbck, data); | |
765deab7 | 1508 | |
f8bf9252 SP |
1509 | case 1: |
1510 | for_each_scev_op (&TREE_OPERAND (*scev, 0), cbck, data); | |
1511 | ||
1512 | default: | |
1513 | cbck (scev, data); | |
1514 | break; | |
1515 | } | |
1516 | } | |
1517 | ||
6a114766 JS |
1518 | /* Returns true when the operation can be part of a linear |
1519 | expression. */ | |
1520 | ||
1521 | static inline bool | |
1522 | operator_is_linear (tree scev) | |
1523 | { | |
1524 | switch (TREE_CODE (scev)) | |
1525 | { | |
1526 | case INTEGER_CST: | |
1527 | case POLYNOMIAL_CHREC: | |
1528 | case PLUS_EXPR: | |
1529 | case POINTER_PLUS_EXPR: | |
1530 | case MULT_EXPR: | |
1531 | case MINUS_EXPR: | |
1532 | case NEGATE_EXPR: | |
1533 | case SSA_NAME: | |
1534 | case NON_LVALUE_EXPR: | |
765deab7 | 1535 | case BIT_NOT_EXPR: |
6a114766 JS |
1536 | CASE_CONVERT: |
1537 | return true; | |
1538 | ||
1539 | default: | |
1540 | return false; | |
1541 | } | |
1542 | } | |
1543 | ||
1544 | /* Return true when SCEV is a linear expression. Linear expressions | |
1545 | can contain additions, substractions and multiplications. | |
1546 | Multiplications are restricted to constant scaling: "cst * x". */ | |
1547 | ||
1548 | bool | |
1549 | scev_is_linear_expression (tree scev) | |
1550 | { | |
1551 | if (scev == NULL | |
1552 | || !operator_is_linear (scev)) | |
1553 | return false; | |
1554 | ||
1555 | if (TREE_CODE (scev) == MULT_EXPR) | |
1556 | return !(tree_contains_chrecs (TREE_OPERAND (scev, 0), NULL) | |
1557 | && tree_contains_chrecs (TREE_OPERAND (scev, 1), NULL)); | |
1558 | ||
765deab7 SP |
1559 | if (TREE_CODE (scev) == POLYNOMIAL_CHREC |
1560 | && !evolution_function_is_affine_multivariate_p (scev, CHREC_VARIABLE (scev))) | |
1561 | return false; | |
1562 | ||
6a114766 JS |
1563 | switch (TREE_CODE_LENGTH (TREE_CODE (scev))) |
1564 | { | |
1565 | case 3: | |
1566 | return scev_is_linear_expression (TREE_OPERAND (scev, 0)) | |
1567 | && scev_is_linear_expression (TREE_OPERAND (scev, 1)) | |
1568 | && scev_is_linear_expression (TREE_OPERAND (scev, 2)); | |
1569 | ||
1570 | case 2: | |
1571 | return scev_is_linear_expression (TREE_OPERAND (scev, 0)) | |
1572 | && scev_is_linear_expression (TREE_OPERAND (scev, 1)); | |
765deab7 | 1573 | |
6a114766 JS |
1574 | case 1: |
1575 | return scev_is_linear_expression (TREE_OPERAND (scev, 0)); | |
1576 | ||
1577 | case 0: | |
1578 | return true; | |
1579 | ||
1580 | default: | |
1581 | return false; | |
1582 | } | |
1583 | } | |
e7c705bb SP |
1584 | |
1585 | /* Determines whether the expression CHREC contains only interger consts | |
1586 | in the right parts. */ | |
1587 | ||
1588 | bool | |
1589 | evolution_function_right_is_integer_cst (const_tree chrec) | |
1590 | { | |
1591 | if (chrec == NULL_TREE) | |
1592 | return false; | |
1593 | ||
1594 | switch (TREE_CODE (chrec)) | |
1595 | { | |
1596 | case INTEGER_CST: | |
1597 | return true; | |
1598 | ||
1599 | case POLYNOMIAL_CHREC: | |
1f380314 SP |
1600 | return TREE_CODE (CHREC_RIGHT (chrec)) == INTEGER_CST |
1601 | && (TREE_CODE (CHREC_LEFT (chrec)) != POLYNOMIAL_CHREC | |
1602 | || evolution_function_right_is_integer_cst (CHREC_LEFT (chrec))); | |
e7c705bb | 1603 | |
1f380314 SP |
1604 | CASE_CONVERT: |
1605 | return evolution_function_right_is_integer_cst (TREE_OPERAND (chrec, 0)); | |
e7c705bb SP |
1606 | |
1607 | default: | |
1608 | return false; | |
1609 | } | |
1610 | } |