]>
Commit | Line | Data |
---|---|---|
e9eb809d | 1 | /* Functions to determine/estimate number of iterations of a loop. |
d1e082c2 | 2 | Copyright (C) 2004-2013 Free Software Foundation, Inc. |
b8698a0f | 3 | |
e9eb809d | 4 | This file is part of GCC. |
b8698a0f | 5 | |
e9eb809d ZD |
6 | GCC is free software; you can redistribute it and/or modify it |
7 | under the terms of the GNU General Public License as published by the | |
9dcd6f09 | 8 | Free Software Foundation; either version 3, or (at your option) any |
e9eb809d | 9 | later version. |
b8698a0f | 10 | |
e9eb809d ZD |
11 | GCC is distributed in the hope that it will be useful, but WITHOUT |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
b8698a0f | 15 | |
e9eb809d | 16 | You should have received a copy of the GNU General Public License |
9dcd6f09 NC |
17 | along with GCC; see the file COPYING3. If not see |
18 | <http://www.gnu.org/licenses/>. */ | |
e9eb809d ZD |
19 | |
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
23 | #include "tm.h" | |
24 | #include "tree.h" | |
d8a2d370 DN |
25 | #include "calls.h" |
26 | #include "expr.h" | |
e9eb809d | 27 | #include "tm_p.h" |
e9eb809d | 28 | #include "basic-block.h" |
cf835838 | 29 | #include "gimple-pretty-print.h" |
f9cc1a70 | 30 | #include "intl.h" |
18f429e2 | 31 | #include "gimple.h" |
45b0be94 | 32 | #include "gimplify.h" |
5be5c238 | 33 | #include "gimple-iterator.h" |
442b4905 AM |
34 | #include "gimple-ssa.h" |
35 | #include "tree-cfg.h" | |
36 | #include "tree-phinodes.h" | |
37 | #include "ssa-iterators.h" | |
e28030cf AM |
38 | #include "tree-ssa-loop-ivopts.h" |
39 | #include "tree-ssa-loop-niter.h" | |
442b4905 | 40 | #include "tree-ssa-loop.h" |
7ee2468b | 41 | #include "dumpfile.h" |
e9eb809d | 42 | #include "cfgloop.h" |
e9eb809d ZD |
43 | #include "ggc.h" |
44 | #include "tree-chrec.h" | |
45 | #include "tree-scalar-evolution.h" | |
86df10e3 | 46 | #include "tree-data-ref.h" |
e9eb809d ZD |
47 | #include "params.h" |
48 | #include "flags.h" | |
718f9c0f | 49 | #include "diagnostic-core.h" |
e9eb809d | 50 | #include "tree-inline.h" |
fbd28bc3 | 51 | #include "tree-pass.h" |
d8a2d370 | 52 | #include "stringpool.h" |
7190fdc1 | 53 | #include "tree-ssanames.h" |
e9eb809d | 54 | |
71343877 | 55 | |
c22940cd | 56 | #define SWAP(X, Y) do { affine_iv *tmp = (X); (X) = (Y); (Y) = tmp; } while (0) |
e9eb809d | 57 | |
b3ce5b6e ZD |
58 | /* The maximum number of dominator BBs we search for conditions |
59 | of loop header copies we use for simplifying a conditional | |
60 | expression. */ | |
61 | #define MAX_DOMINATORS_TO_WALK 8 | |
e9eb809d ZD |
62 | |
63 | /* | |
64 | ||
65 | Analysis of number of iterations of an affine exit test. | |
66 | ||
67 | */ | |
68 | ||
b3ce5b6e ZD |
69 | /* Bounds on some value, BELOW <= X <= UP. */ |
70 | ||
71 | typedef struct | |
72 | { | |
73 | mpz_t below, up; | |
74 | } bounds; | |
75 | ||
b3ce5b6e ZD |
76 | |
77 | /* Splits expression EXPR to a variable part VAR and constant OFFSET. */ | |
78 | ||
79 | static void | |
80 | split_to_var_and_offset (tree expr, tree *var, mpz_t offset) | |
81 | { | |
82 | tree type = TREE_TYPE (expr); | |
83 | tree op0, op1; | |
84 | double_int off; | |
85 | bool negate = false; | |
86 | ||
87 | *var = expr; | |
88 | mpz_set_ui (offset, 0); | |
89 | ||
90 | switch (TREE_CODE (expr)) | |
91 | { | |
92 | case MINUS_EXPR: | |
93 | negate = true; | |
94 | /* Fallthru. */ | |
95 | ||
96 | case PLUS_EXPR: | |
5be014d5 | 97 | case POINTER_PLUS_EXPR: |
b3ce5b6e ZD |
98 | op0 = TREE_OPERAND (expr, 0); |
99 | op1 = TREE_OPERAND (expr, 1); | |
100 | ||
101 | if (TREE_CODE (op1) != INTEGER_CST) | |
102 | break; | |
103 | ||
104 | *var = op0; | |
105 | /* Always sign extend the offset. */ | |
0f900dfa | 106 | off = tree_to_double_int (op1); |
27bcd47c | 107 | off = off.sext (TYPE_PRECISION (type)); |
b3ce5b6e | 108 | mpz_set_double_int (offset, off, false); |
eab1da69 UB |
109 | if (negate) |
110 | mpz_neg (offset, offset); | |
b3ce5b6e ZD |
111 | break; |
112 | ||
113 | case INTEGER_CST: | |
114 | *var = build_int_cst_type (type, 0); | |
115 | off = tree_to_double_int (expr); | |
116 | mpz_set_double_int (offset, off, TYPE_UNSIGNED (type)); | |
117 | break; | |
118 | ||
119 | default: | |
120 | break; | |
121 | } | |
122 | } | |
123 | ||
124 | /* Stores estimate on the minimum/maximum value of the expression VAR + OFF | |
125 | in TYPE to MIN and MAX. */ | |
126 | ||
127 | static void | |
7190fdc1 | 128 | determine_value_range (struct loop *loop, tree type, tree var, mpz_t off, |
b3ce5b6e ZD |
129 | mpz_t min, mpz_t max) |
130 | { | |
7190fdc1 JJ |
131 | double_int minv, maxv; |
132 | enum value_range_type rtype = VR_VARYING; | |
133 | ||
b3ce5b6e ZD |
134 | /* If the expression is a constant, we know its value exactly. */ |
135 | if (integer_zerop (var)) | |
136 | { | |
137 | mpz_set (min, off); | |
138 | mpz_set (max, off); | |
139 | return; | |
140 | } | |
141 | ||
7190fdc1 JJ |
142 | get_type_static_bounds (type, min, max); |
143 | ||
144 | /* See if we have some range info from VRP. */ | |
145 | if (TREE_CODE (var) == SSA_NAME && INTEGRAL_TYPE_P (type)) | |
146 | { | |
147 | edge e = loop_preheader_edge (loop); | |
148 | gimple_stmt_iterator gsi; | |
149 | ||
150 | /* Either for VAR itself... */ | |
151 | rtype = get_range_info (var, &minv, &maxv); | |
152 | /* Or for PHI results in loop->header where VAR is used as | |
153 | PHI argument from the loop preheader edge. */ | |
154 | for (gsi = gsi_start_phis (loop->header); !gsi_end_p (gsi); gsi_next (&gsi)) | |
155 | { | |
156 | gimple phi = gsi_stmt (gsi); | |
157 | double_int minc, maxc; | |
158 | if (PHI_ARG_DEF_FROM_EDGE (phi, e) == var | |
159 | && (get_range_info (gimple_phi_result (phi), &minc, &maxc) | |
160 | == VR_RANGE)) | |
161 | { | |
162 | if (rtype != VR_RANGE) | |
163 | { | |
164 | rtype = VR_RANGE; | |
165 | minv = minc; | |
166 | maxv = maxc; | |
167 | } | |
168 | else | |
169 | { | |
170 | minv = minv.max (minc, TYPE_UNSIGNED (type)); | |
171 | maxv = maxv.min (maxc, TYPE_UNSIGNED (type)); | |
172 | gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0); | |
173 | } | |
174 | } | |
175 | } | |
176 | if (rtype == VR_RANGE) | |
177 | { | |
178 | mpz_t minm, maxm; | |
179 | gcc_assert (minv.cmp (maxv, TYPE_UNSIGNED (type)) <= 0); | |
180 | mpz_init (minm); | |
181 | mpz_init (maxm); | |
182 | mpz_set_double_int (minm, minv, TYPE_UNSIGNED (type)); | |
183 | mpz_set_double_int (maxm, maxv, TYPE_UNSIGNED (type)); | |
184 | mpz_add (minm, minm, off); | |
185 | mpz_add (maxm, maxm, off); | |
186 | /* If the computation may not wrap or off is zero, then this | |
187 | is always fine. If off is negative and minv + off isn't | |
188 | smaller than type's minimum, or off is positive and | |
189 | maxv + off isn't bigger than type's maximum, use the more | |
190 | precise range too. */ | |
191 | if (nowrap_type_p (type) | |
192 | || mpz_sgn (off) == 0 | |
193 | || (mpz_sgn (off) < 0 && mpz_cmp (minm, min) >= 0) | |
194 | || (mpz_sgn (off) > 0 && mpz_cmp (maxm, max) <= 0)) | |
195 | { | |
196 | mpz_set (min, minm); | |
197 | mpz_set (max, maxm); | |
198 | mpz_clear (minm); | |
199 | mpz_clear (maxm); | |
200 | return; | |
201 | } | |
202 | mpz_clear (minm); | |
203 | mpz_clear (maxm); | |
204 | } | |
205 | } | |
206 | ||
b3ce5b6e ZD |
207 | /* If the computation may wrap, we know nothing about the value, except for |
208 | the range of the type. */ | |
b3ce5b6e ZD |
209 | if (!nowrap_type_p (type)) |
210 | return; | |
211 | ||
212 | /* Since the addition of OFF does not wrap, if OFF is positive, then we may | |
213 | add it to MIN, otherwise to MAX. */ | |
214 | if (mpz_sgn (off) < 0) | |
215 | mpz_add (max, max, off); | |
216 | else | |
217 | mpz_add (min, min, off); | |
218 | } | |
219 | ||
220 | /* Stores the bounds on the difference of the values of the expressions | |
221 | (var + X) and (var + Y), computed in TYPE, to BNDS. */ | |
222 | ||
223 | static void | |
224 | bound_difference_of_offsetted_base (tree type, mpz_t x, mpz_t y, | |
225 | bounds *bnds) | |
226 | { | |
227 | int rel = mpz_cmp (x, y); | |
228 | bool may_wrap = !nowrap_type_p (type); | |
229 | mpz_t m; | |
230 | ||
231 | /* If X == Y, then the expressions are always equal. | |
232 | If X > Y, there are the following possibilities: | |
233 | a) neither of var + X and var + Y overflow or underflow, or both of | |
234 | them do. Then their difference is X - Y. | |
235 | b) var + X overflows, and var + Y does not. Then the values of the | |
236 | expressions are var + X - M and var + Y, where M is the range of | |
237 | the type, and their difference is X - Y - M. | |
238 | c) var + Y underflows and var + X does not. Their difference again | |
239 | is M - X + Y. | |
240 | Therefore, if the arithmetics in type does not overflow, then the | |
241 | bounds are (X - Y, X - Y), otherwise they are (X - Y - M, X - Y) | |
242 | Similarly, if X < Y, the bounds are either (X - Y, X - Y) or | |
243 | (X - Y, X - Y + M). */ | |
244 | ||
245 | if (rel == 0) | |
246 | { | |
247 | mpz_set_ui (bnds->below, 0); | |
248 | mpz_set_ui (bnds->up, 0); | |
249 | return; | |
250 | } | |
251 | ||
252 | mpz_init (m); | |
27bcd47c | 253 | mpz_set_double_int (m, double_int::mask (TYPE_PRECISION (type)), true); |
b3ce5b6e ZD |
254 | mpz_add_ui (m, m, 1); |
255 | mpz_sub (bnds->up, x, y); | |
256 | mpz_set (bnds->below, bnds->up); | |
257 | ||
258 | if (may_wrap) | |
259 | { | |
260 | if (rel > 0) | |
261 | mpz_sub (bnds->below, bnds->below, m); | |
262 | else | |
263 | mpz_add (bnds->up, bnds->up, m); | |
264 | } | |
265 | ||
266 | mpz_clear (m); | |
267 | } | |
268 | ||
269 | /* From condition C0 CMP C1 derives information regarding the | |
270 | difference of values of VARX + OFFX and VARY + OFFY, computed in TYPE, | |
271 | and stores it to BNDS. */ | |
272 | ||
273 | static void | |
274 | refine_bounds_using_guard (tree type, tree varx, mpz_t offx, | |
275 | tree vary, mpz_t offy, | |
276 | tree c0, enum tree_code cmp, tree c1, | |
277 | bounds *bnds) | |
278 | { | |
17b236ed | 279 | tree varc0, varc1, tmp, ctype; |
b3ce5b6e ZD |
280 | mpz_t offc0, offc1, loffx, loffy, bnd; |
281 | bool lbound = false; | |
282 | bool no_wrap = nowrap_type_p (type); | |
283 | bool x_ok, y_ok; | |
284 | ||
285 | switch (cmp) | |
286 | { | |
287 | case LT_EXPR: | |
288 | case LE_EXPR: | |
289 | case GT_EXPR: | |
290 | case GE_EXPR: | |
17b236ed ZD |
291 | STRIP_SIGN_NOPS (c0); |
292 | STRIP_SIGN_NOPS (c1); | |
293 | ctype = TREE_TYPE (c0); | |
36618b93 | 294 | if (!useless_type_conversion_p (ctype, type)) |
17b236ed ZD |
295 | return; |
296 | ||
b3ce5b6e ZD |
297 | break; |
298 | ||
299 | case EQ_EXPR: | |
300 | /* We could derive quite precise information from EQ_EXPR, however, such | |
17b236ed ZD |
301 | a guard is unlikely to appear, so we do not bother with handling |
302 | it. */ | |
b3ce5b6e ZD |
303 | return; |
304 | ||
305 | case NE_EXPR: | |
17b236ed ZD |
306 | /* NE_EXPR comparisons do not contain much of useful information, except for |
307 | special case of comparing with the bounds of the type. */ | |
308 | if (TREE_CODE (c1) != INTEGER_CST | |
309 | || !INTEGRAL_TYPE_P (type)) | |
310 | return; | |
311 | ||
312 | /* Ensure that the condition speaks about an expression in the same type | |
313 | as X and Y. */ | |
314 | ctype = TREE_TYPE (c0); | |
315 | if (TYPE_PRECISION (ctype) != TYPE_PRECISION (type)) | |
316 | return; | |
317 | c0 = fold_convert (type, c0); | |
318 | c1 = fold_convert (type, c1); | |
319 | ||
320 | if (TYPE_MIN_VALUE (type) | |
321 | && operand_equal_p (c1, TYPE_MIN_VALUE (type), 0)) | |
322 | { | |
323 | cmp = GT_EXPR; | |
324 | break; | |
325 | } | |
326 | if (TYPE_MAX_VALUE (type) | |
327 | && operand_equal_p (c1, TYPE_MAX_VALUE (type), 0)) | |
328 | { | |
329 | cmp = LT_EXPR; | |
330 | break; | |
331 | } | |
332 | ||
b3ce5b6e ZD |
333 | return; |
334 | default: | |
335 | return; | |
b8698a0f | 336 | } |
b3ce5b6e ZD |
337 | |
338 | mpz_init (offc0); | |
339 | mpz_init (offc1); | |
340 | split_to_var_and_offset (expand_simple_operations (c0), &varc0, offc0); | |
341 | split_to_var_and_offset (expand_simple_operations (c1), &varc1, offc1); | |
342 | ||
343 | /* We are only interested in comparisons of expressions based on VARX and | |
344 | VARY. TODO -- we might also be able to derive some bounds from | |
345 | expressions containing just one of the variables. */ | |
346 | ||
347 | if (operand_equal_p (varx, varc1, 0)) | |
348 | { | |
349 | tmp = varc0; varc0 = varc1; varc1 = tmp; | |
350 | mpz_swap (offc0, offc1); | |
351 | cmp = swap_tree_comparison (cmp); | |
352 | } | |
353 | ||
354 | if (!operand_equal_p (varx, varc0, 0) | |
355 | || !operand_equal_p (vary, varc1, 0)) | |
356 | goto end; | |
357 | ||
358 | mpz_init_set (loffx, offx); | |
359 | mpz_init_set (loffy, offy); | |
360 | ||
361 | if (cmp == GT_EXPR || cmp == GE_EXPR) | |
362 | { | |
363 | tmp = varx; varx = vary; vary = tmp; | |
364 | mpz_swap (offc0, offc1); | |
365 | mpz_swap (loffx, loffy); | |
366 | cmp = swap_tree_comparison (cmp); | |
367 | lbound = true; | |
368 | } | |
369 | ||
370 | /* If there is no overflow, the condition implies that | |
371 | ||
372 | (VARX + OFFX) cmp (VARY + OFFY) + (OFFX - OFFY + OFFC1 - OFFC0). | |
373 | ||
374 | The overflows and underflows may complicate things a bit; each | |
375 | overflow decreases the appropriate offset by M, and underflow | |
376 | increases it by M. The above inequality would not necessarily be | |
377 | true if | |
b8698a0f | 378 | |
b3ce5b6e ZD |
379 | -- VARX + OFFX underflows and VARX + OFFC0 does not, or |
380 | VARX + OFFC0 overflows, but VARX + OFFX does not. | |
381 | This may only happen if OFFX < OFFC0. | |
382 | -- VARY + OFFY overflows and VARY + OFFC1 does not, or | |
383 | VARY + OFFC1 underflows and VARY + OFFY does not. | |
384 | This may only happen if OFFY > OFFC1. */ | |
385 | ||
386 | if (no_wrap) | |
387 | { | |
388 | x_ok = true; | |
389 | y_ok = true; | |
390 | } | |
391 | else | |
392 | { | |
393 | x_ok = (integer_zerop (varx) | |
394 | || mpz_cmp (loffx, offc0) >= 0); | |
395 | y_ok = (integer_zerop (vary) | |
396 | || mpz_cmp (loffy, offc1) <= 0); | |
397 | } | |
398 | ||
399 | if (x_ok && y_ok) | |
400 | { | |
401 | mpz_init (bnd); | |
402 | mpz_sub (bnd, loffx, loffy); | |
403 | mpz_add (bnd, bnd, offc1); | |
404 | mpz_sub (bnd, bnd, offc0); | |
405 | ||
406 | if (cmp == LT_EXPR) | |
407 | mpz_sub_ui (bnd, bnd, 1); | |
408 | ||
409 | if (lbound) | |
410 | { | |
411 | mpz_neg (bnd, bnd); | |
412 | if (mpz_cmp (bnds->below, bnd) < 0) | |
413 | mpz_set (bnds->below, bnd); | |
414 | } | |
415 | else | |
416 | { | |
417 | if (mpz_cmp (bnd, bnds->up) < 0) | |
418 | mpz_set (bnds->up, bnd); | |
419 | } | |
420 | mpz_clear (bnd); | |
421 | } | |
422 | ||
423 | mpz_clear (loffx); | |
424 | mpz_clear (loffy); | |
425 | end: | |
426 | mpz_clear (offc0); | |
427 | mpz_clear (offc1); | |
428 | } | |
429 | ||
430 | /* Stores the bounds on the value of the expression X - Y in LOOP to BNDS. | |
431 | The subtraction is considered to be performed in arbitrary precision, | |
432 | without overflows. | |
b8698a0f | 433 | |
b3ce5b6e ZD |
434 | We do not attempt to be too clever regarding the value ranges of X and |
435 | Y; most of the time, they are just integers or ssa names offsetted by | |
436 | integer. However, we try to use the information contained in the | |
437 | comparisons before the loop (usually created by loop header copying). */ | |
438 | ||
439 | static void | |
440 | bound_difference (struct loop *loop, tree x, tree y, bounds *bnds) | |
441 | { | |
442 | tree type = TREE_TYPE (x); | |
443 | tree varx, vary; | |
444 | mpz_t offx, offy; | |
445 | mpz_t minx, maxx, miny, maxy; | |
446 | int cnt = 0; | |
447 | edge e; | |
448 | basic_block bb; | |
726a989a RB |
449 | tree c0, c1; |
450 | gimple cond; | |
b3ce5b6e ZD |
451 | enum tree_code cmp; |
452 | ||
17b236ed ZD |
453 | /* Get rid of unnecessary casts, but preserve the value of |
454 | the expressions. */ | |
455 | STRIP_SIGN_NOPS (x); | |
456 | STRIP_SIGN_NOPS (y); | |
457 | ||
b3ce5b6e ZD |
458 | mpz_init (bnds->below); |
459 | mpz_init (bnds->up); | |
460 | mpz_init (offx); | |
461 | mpz_init (offy); | |
462 | split_to_var_and_offset (x, &varx, offx); | |
463 | split_to_var_and_offset (y, &vary, offy); | |
464 | ||
465 | if (!integer_zerop (varx) | |
466 | && operand_equal_p (varx, vary, 0)) | |
467 | { | |
468 | /* Special case VARX == VARY -- we just need to compare the | |
469 | offsets. The matters are a bit more complicated in the | |
470 | case addition of offsets may wrap. */ | |
471 | bound_difference_of_offsetted_base (type, offx, offy, bnds); | |
472 | } | |
473 | else | |
474 | { | |
475 | /* Otherwise, use the value ranges to determine the initial | |
476 | estimates on below and up. */ | |
477 | mpz_init (minx); | |
478 | mpz_init (maxx); | |
479 | mpz_init (miny); | |
480 | mpz_init (maxy); | |
7190fdc1 JJ |
481 | determine_value_range (loop, type, varx, offx, minx, maxx); |
482 | determine_value_range (loop, type, vary, offy, miny, maxy); | |
b3ce5b6e ZD |
483 | |
484 | mpz_sub (bnds->below, minx, maxy); | |
485 | mpz_sub (bnds->up, maxx, miny); | |
486 | mpz_clear (minx); | |
487 | mpz_clear (maxx); | |
488 | mpz_clear (miny); | |
489 | mpz_clear (maxy); | |
490 | } | |
491 | ||
492 | /* If both X and Y are constants, we cannot get any more precise. */ | |
493 | if (integer_zerop (varx) && integer_zerop (vary)) | |
494 | goto end; | |
495 | ||
496 | /* Now walk the dominators of the loop header and use the entry | |
497 | guards to refine the estimates. */ | |
498 | for (bb = loop->header; | |
499 | bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK; | |
500 | bb = get_immediate_dominator (CDI_DOMINATORS, bb)) | |
501 | { | |
502 | if (!single_pred_p (bb)) | |
503 | continue; | |
504 | e = single_pred_edge (bb); | |
505 | ||
506 | if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) | |
507 | continue; | |
508 | ||
726a989a RB |
509 | cond = last_stmt (e->src); |
510 | c0 = gimple_cond_lhs (cond); | |
511 | cmp = gimple_cond_code (cond); | |
512 | c1 = gimple_cond_rhs (cond); | |
b3ce5b6e ZD |
513 | |
514 | if (e->flags & EDGE_FALSE_VALUE) | |
515 | cmp = invert_tree_comparison (cmp, false); | |
516 | ||
517 | refine_bounds_using_guard (type, varx, offx, vary, offy, | |
518 | c0, cmp, c1, bnds); | |
519 | ++cnt; | |
520 | } | |
521 | ||
522 | end: | |
523 | mpz_clear (offx); | |
524 | mpz_clear (offy); | |
525 | } | |
526 | ||
527 | /* Update the bounds in BNDS that restrict the value of X to the bounds | |
528 | that restrict the value of X + DELTA. X can be obtained as a | |
529 | difference of two values in TYPE. */ | |
530 | ||
531 | static void | |
532 | bounds_add (bounds *bnds, double_int delta, tree type) | |
533 | { | |
534 | mpz_t mdelta, max; | |
535 | ||
536 | mpz_init (mdelta); | |
537 | mpz_set_double_int (mdelta, delta, false); | |
538 | ||
539 | mpz_init (max); | |
27bcd47c | 540 | mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true); |
b3ce5b6e ZD |
541 | |
542 | mpz_add (bnds->up, bnds->up, mdelta); | |
543 | mpz_add (bnds->below, bnds->below, mdelta); | |
544 | ||
545 | if (mpz_cmp (bnds->up, max) > 0) | |
546 | mpz_set (bnds->up, max); | |
547 | ||
548 | mpz_neg (max, max); | |
549 | if (mpz_cmp (bnds->below, max) < 0) | |
550 | mpz_set (bnds->below, max); | |
551 | ||
552 | mpz_clear (mdelta); | |
553 | mpz_clear (max); | |
554 | } | |
555 | ||
556 | /* Update the bounds in BNDS that restrict the value of X to the bounds | |
557 | that restrict the value of -X. */ | |
558 | ||
559 | static void | |
560 | bounds_negate (bounds *bnds) | |
561 | { | |
562 | mpz_t tmp; | |
563 | ||
564 | mpz_init_set (tmp, bnds->up); | |
565 | mpz_neg (bnds->up, bnds->below); | |
566 | mpz_neg (bnds->below, tmp); | |
567 | mpz_clear (tmp); | |
568 | } | |
569 | ||
e9eb809d ZD |
570 | /* Returns inverse of X modulo 2^s, where MASK = 2^s-1. */ |
571 | ||
572 | static tree | |
573 | inverse (tree x, tree mask) | |
574 | { | |
575 | tree type = TREE_TYPE (x); | |
26630a99 ZD |
576 | tree rslt; |
577 | unsigned ctr = tree_floor_log2 (mask); | |
578 | ||
579 | if (TYPE_PRECISION (type) <= HOST_BITS_PER_WIDE_INT) | |
580 | { | |
581 | unsigned HOST_WIDE_INT ix; | |
582 | unsigned HOST_WIDE_INT imask; | |
583 | unsigned HOST_WIDE_INT irslt = 1; | |
584 | ||
585 | gcc_assert (cst_and_fits_in_hwi (x)); | |
586 | gcc_assert (cst_and_fits_in_hwi (mask)); | |
587 | ||
588 | ix = int_cst_value (x); | |
589 | imask = int_cst_value (mask); | |
590 | ||
591 | for (; ctr; ctr--) | |
592 | { | |
593 | irslt *= ix; | |
594 | ix *= ix; | |
595 | } | |
596 | irslt &= imask; | |
e9eb809d | 597 | |
26630a99 ZD |
598 | rslt = build_int_cst_type (type, irslt); |
599 | } | |
600 | else | |
e9eb809d | 601 | { |
ff5e9a94 | 602 | rslt = build_int_cst (type, 1); |
26630a99 ZD |
603 | for (; ctr; ctr--) |
604 | { | |
d35936ab RG |
605 | rslt = int_const_binop (MULT_EXPR, rslt, x); |
606 | x = int_const_binop (MULT_EXPR, x, x); | |
26630a99 | 607 | } |
d35936ab | 608 | rslt = int_const_binop (BIT_AND_EXPR, rslt, mask); |
e9eb809d ZD |
609 | } |
610 | ||
611 | return rslt; | |
612 | } | |
613 | ||
b3ce5b6e | 614 | /* Derives the upper bound BND on the number of executions of loop with exit |
1987baa3 ZD |
615 | condition S * i <> C. If NO_OVERFLOW is true, then the control variable of |
616 | the loop does not overflow. EXIT_MUST_BE_TAKEN is true if we are guaranteed | |
617 | that the loop ends through this exit, i.e., the induction variable ever | |
618 | reaches the value of C. | |
619 | ||
620 | The value C is equal to final - base, where final and base are the final and | |
621 | initial value of the actual induction variable in the analysed loop. BNDS | |
622 | bounds the value of this difference when computed in signed type with | |
623 | unbounded range, while the computation of C is performed in an unsigned | |
624 | type with the range matching the range of the type of the induction variable. | |
625 | In particular, BNDS.up contains an upper bound on C in the following cases: | |
626 | -- if the iv must reach its final value without overflow, i.e., if | |
627 | NO_OVERFLOW && EXIT_MUST_BE_TAKEN is true, or | |
628 | -- if final >= base, which we know to hold when BNDS.below >= 0. */ | |
b3ce5b6e ZD |
629 | |
630 | static void | |
631 | number_of_iterations_ne_max (mpz_t bnd, bool no_overflow, tree c, tree s, | |
1987baa3 | 632 | bounds *bnds, bool exit_must_be_taken) |
b3ce5b6e ZD |
633 | { |
634 | double_int max; | |
635 | mpz_t d; | |
5a892248 | 636 | tree type = TREE_TYPE (c); |
1987baa3 ZD |
637 | bool bnds_u_valid = ((no_overflow && exit_must_be_taken) |
638 | || mpz_sgn (bnds->below) >= 0); | |
b3ce5b6e | 639 | |
5a892248 RB |
640 | if (integer_onep (s) |
641 | || (TREE_CODE (c) == INTEGER_CST | |
642 | && TREE_CODE (s) == INTEGER_CST | |
643 | && tree_to_double_int (c).mod (tree_to_double_int (s), | |
644 | TYPE_UNSIGNED (type), | |
645 | EXACT_DIV_EXPR).is_zero ()) | |
646 | || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (c)) | |
647 | && multiple_of_p (type, c, s))) | |
1987baa3 ZD |
648 | { |
649 | /* If C is an exact multiple of S, then its value will be reached before | |
650 | the induction variable overflows (unless the loop is exited in some | |
651 | other way before). Note that the actual induction variable in the | |
652 | loop (which ranges from base to final instead of from 0 to C) may | |
653 | overflow, in which case BNDS.up will not be giving a correct upper | |
654 | bound on C; thus, BNDS_U_VALID had to be computed in advance. */ | |
655 | no_overflow = true; | |
656 | exit_must_be_taken = true; | |
657 | } | |
658 | ||
659 | /* If the induction variable can overflow, the number of iterations is at | |
660 | most the period of the control variable (or infinite, but in that case | |
661 | the whole # of iterations analysis will fail). */ | |
662 | if (!no_overflow) | |
b3ce5b6e | 663 | { |
5a892248 | 664 | max = double_int::mask (TYPE_PRECISION (type) |
ae7e9ddd | 665 | - tree_to_uhwi (num_ending_zeros (s))); |
b3ce5b6e ZD |
666 | mpz_set_double_int (bnd, max, true); |
667 | return; | |
668 | } | |
669 | ||
1987baa3 ZD |
670 | /* Now we know that the induction variable does not overflow, so the loop |
671 | iterates at most (range of type / S) times. */ | |
5a892248 | 672 | mpz_set_double_int (bnd, double_int::mask (TYPE_PRECISION (type)), true); |
1987baa3 ZD |
673 | |
674 | /* If the induction variable is guaranteed to reach the value of C before | |
675 | overflow, ... */ | |
676 | if (exit_must_be_taken) | |
677 | { | |
073a8998 | 678 | /* ... then we can strengthen this to C / S, and possibly we can use |
1987baa3 ZD |
679 | the upper bound on C given by BNDS. */ |
680 | if (TREE_CODE (c) == INTEGER_CST) | |
681 | mpz_set_double_int (bnd, tree_to_double_int (c), true); | |
682 | else if (bnds_u_valid) | |
683 | mpz_set (bnd, bnds->up); | |
684 | } | |
b3ce5b6e ZD |
685 | |
686 | mpz_init (d); | |
687 | mpz_set_double_int (d, tree_to_double_int (s), true); | |
688 | mpz_fdiv_q (bnd, bnd, d); | |
689 | mpz_clear (d); | |
690 | } | |
691 | ||
7f17528a ZD |
692 | /* Determines number of iterations of loop whose ending condition |
693 | is IV <> FINAL. TYPE is the type of the iv. The number of | |
e36dc339 | 694 | iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if |
f08ac361 ZD |
695 | we know that the exit must be taken eventually, i.e., that the IV |
696 | ever reaches the value FINAL (we derived this earlier, and possibly set | |
b3ce5b6e ZD |
697 | NITER->assumptions to make sure this is the case). BNDS contains the |
698 | bounds on the difference FINAL - IV->base. */ | |
e9eb809d | 699 | |
7f17528a ZD |
700 | static bool |
701 | number_of_iterations_ne (tree type, affine_iv *iv, tree final, | |
e36dc339 | 702 | struct tree_niter_desc *niter, bool exit_must_be_taken, |
b3ce5b6e | 703 | bounds *bnds) |
e9eb809d | 704 | { |
7f17528a ZD |
705 | tree niter_type = unsigned_type_for (type); |
706 | tree s, c, d, bits, assumption, tmp, bound; | |
b3ce5b6e | 707 | mpz_t max; |
e9eb809d | 708 | |
17684618 ZD |
709 | niter->control = *iv; |
710 | niter->bound = final; | |
711 | niter->cmp = NE_EXPR; | |
712 | ||
b3ce5b6e ZD |
713 | /* Rearrange the terms so that we get inequality S * i <> C, with S |
714 | positive. Also cast everything to the unsigned type. If IV does | |
715 | not overflow, BNDS bounds the value of C. Also, this is the | |
716 | case if the computation |FINAL - IV->base| does not overflow, i.e., | |
717 | if BNDS->below in the result is nonnegative. */ | |
7f17528a | 718 | if (tree_int_cst_sign_bit (iv->step)) |
e9eb809d | 719 | { |
7f17528a ZD |
720 | s = fold_convert (niter_type, |
721 | fold_build1 (NEGATE_EXPR, type, iv->step)); | |
722 | c = fold_build2 (MINUS_EXPR, niter_type, | |
723 | fold_convert (niter_type, iv->base), | |
724 | fold_convert (niter_type, final)); | |
b3ce5b6e | 725 | bounds_negate (bnds); |
e9eb809d | 726 | } |
a6f778b2 | 727 | else |
e9eb809d | 728 | { |
7f17528a ZD |
729 | s = fold_convert (niter_type, iv->step); |
730 | c = fold_build2 (MINUS_EXPR, niter_type, | |
731 | fold_convert (niter_type, final), | |
732 | fold_convert (niter_type, iv->base)); | |
733 | } | |
e9eb809d | 734 | |
b3ce5b6e | 735 | mpz_init (max); |
1987baa3 ZD |
736 | number_of_iterations_ne_max (max, iv->no_overflow, c, s, bnds, |
737 | exit_must_be_taken); | |
e4fd22c6 | 738 | niter->max = mpz_get_double_int (niter_type, max, false); |
b3ce5b6e ZD |
739 | mpz_clear (max); |
740 | ||
7f17528a ZD |
741 | /* First the trivial cases -- when the step is 1. */ |
742 | if (integer_onep (s)) | |
743 | { | |
744 | niter->niter = c; | |
745 | return true; | |
e9eb809d ZD |
746 | } |
747 | ||
7f17528a ZD |
748 | /* Let nsd (step, size of mode) = d. If d does not divide c, the loop |
749 | is infinite. Otherwise, the number of iterations is | |
750 | (inverse(s/d) * (c/d)) mod (size of mode/d). */ | |
751 | bits = num_ending_zeros (s); | |
752 | bound = build_low_bits_mask (niter_type, | |
753 | (TYPE_PRECISION (niter_type) | |
ae7e9ddd | 754 | - tree_to_uhwi (bits))); |
e9eb809d | 755 | |
7f17528a | 756 | d = fold_binary_to_constant (LSHIFT_EXPR, niter_type, |
ff5e9a94 | 757 | build_int_cst (niter_type, 1), bits); |
7f17528a | 758 | s = fold_binary_to_constant (RSHIFT_EXPR, niter_type, s, bits); |
e9eb809d | 759 | |
e36dc339 | 760 | if (!exit_must_be_taken) |
7f17528a | 761 | { |
e36dc339 | 762 | /* If we cannot assume that the exit is taken eventually, record the |
7f17528a ZD |
763 | assumptions for divisibility of c. */ |
764 | assumption = fold_build2 (FLOOR_MOD_EXPR, niter_type, c, d); | |
765 | assumption = fold_build2 (EQ_EXPR, boolean_type_node, | |
766 | assumption, build_int_cst (niter_type, 0)); | |
6e682d7e | 767 | if (!integer_nonzerop (assumption)) |
7f17528a ZD |
768 | niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
769 | niter->assumptions, assumption); | |
e9eb809d | 770 | } |
b8698a0f | 771 | |
7f17528a ZD |
772 | c = fold_build2 (EXACT_DIV_EXPR, niter_type, c, d); |
773 | tmp = fold_build2 (MULT_EXPR, niter_type, c, inverse (s, bound)); | |
774 | niter->niter = fold_build2 (BIT_AND_EXPR, niter_type, tmp, bound); | |
775 | return true; | |
776 | } | |
e9eb809d | 777 | |
7f17528a ZD |
778 | /* Checks whether we can determine the final value of the control variable |
779 | of the loop with ending condition IV0 < IV1 (computed in TYPE). | |
780 | DELTA is the difference IV1->base - IV0->base, STEP is the absolute value | |
781 | of the step. The assumptions necessary to ensure that the computation | |
782 | of the final value does not overflow are recorded in NITER. If we | |
783 | find the final value, we adjust DELTA and return TRUE. Otherwise | |
b3ce5b6e | 784 | we return false. BNDS bounds the value of IV1->base - IV0->base, |
e36dc339 ZD |
785 | and will be updated by the same amount as DELTA. EXIT_MUST_BE_TAKEN is |
786 | true if we know that the exit must be taken eventually. */ | |
7f17528a ZD |
787 | |
788 | static bool | |
789 | number_of_iterations_lt_to_ne (tree type, affine_iv *iv0, affine_iv *iv1, | |
790 | struct tree_niter_desc *niter, | |
b3ce5b6e | 791 | tree *delta, tree step, |
e36dc339 | 792 | bool exit_must_be_taken, bounds *bnds) |
7f17528a ZD |
793 | { |
794 | tree niter_type = TREE_TYPE (step); | |
795 | tree mod = fold_build2 (FLOOR_MOD_EXPR, niter_type, *delta, step); | |
796 | tree tmod; | |
b3ce5b6e | 797 | mpz_t mmod; |
7f17528a | 798 | tree assumption = boolean_true_node, bound, noloop; |
e36dc339 | 799 | bool ret = false, fv_comp_no_overflow; |
5be014d5 AP |
800 | tree type1 = type; |
801 | if (POINTER_TYPE_P (type)) | |
802 | type1 = sizetype; | |
7f17528a ZD |
803 | |
804 | if (TREE_CODE (mod) != INTEGER_CST) | |
805 | return false; | |
6e682d7e | 806 | if (integer_nonzerop (mod)) |
7f17528a | 807 | mod = fold_build2 (MINUS_EXPR, niter_type, step, mod); |
5be014d5 | 808 | tmod = fold_convert (type1, mod); |
7f17528a | 809 | |
b3ce5b6e ZD |
810 | mpz_init (mmod); |
811 | mpz_set_double_int (mmod, tree_to_double_int (mod), true); | |
812 | mpz_neg (mmod, mmod); | |
813 | ||
e36dc339 ZD |
814 | /* If the induction variable does not overflow and the exit is taken, |
815 | then the computation of the final value does not overflow. This is | |
816 | also obviously the case if the new final value is equal to the | |
817 | current one. Finally, we postulate this for pointer type variables, | |
818 | as the code cannot rely on the object to that the pointer points being | |
819 | placed at the end of the address space (and more pragmatically, | |
820 | TYPE_{MIN,MAX}_VALUE is not defined for pointers). */ | |
821 | if (integer_zerop (mod) || POINTER_TYPE_P (type)) | |
822 | fv_comp_no_overflow = true; | |
823 | else if (!exit_must_be_taken) | |
824 | fv_comp_no_overflow = false; | |
825 | else | |
826 | fv_comp_no_overflow = | |
827 | (iv0->no_overflow && integer_nonzerop (iv0->step)) | |
828 | || (iv1->no_overflow && integer_nonzerop (iv1->step)); | |
829 | ||
6e42ce54 | 830 | if (integer_nonzerop (iv0->step)) |
e9eb809d | 831 | { |
7f17528a ZD |
832 | /* The final value of the iv is iv1->base + MOD, assuming that this |
833 | computation does not overflow, and that | |
834 | iv0->base <= iv1->base + MOD. */ | |
e36dc339 | 835 | if (!fv_comp_no_overflow) |
7f17528a | 836 | { |
97b4ba9f | 837 | bound = fold_build2 (MINUS_EXPR, type1, |
5be014d5 | 838 | TYPE_MAX_VALUE (type1), tmod); |
7f17528a ZD |
839 | assumption = fold_build2 (LE_EXPR, boolean_type_node, |
840 | iv1->base, bound); | |
6e682d7e | 841 | if (integer_zerop (assumption)) |
b3ce5b6e | 842 | goto end; |
7f17528a | 843 | } |
b3ce5b6e ZD |
844 | if (mpz_cmp (mmod, bnds->below) < 0) |
845 | noloop = boolean_false_node; | |
97b4ba9f JJ |
846 | else if (POINTER_TYPE_P (type)) |
847 | noloop = fold_build2 (GT_EXPR, boolean_type_node, | |
848 | iv0->base, | |
5d49b6a7 | 849 | fold_build_pointer_plus (iv1->base, tmod)); |
b3ce5b6e ZD |
850 | else |
851 | noloop = fold_build2 (GT_EXPR, boolean_type_node, | |
852 | iv0->base, | |
5be014d5 | 853 | fold_build2 (PLUS_EXPR, type1, |
b3ce5b6e | 854 | iv1->base, tmod)); |
e9eb809d ZD |
855 | } |
856 | else | |
857 | { | |
7f17528a ZD |
858 | /* The final value of the iv is iv0->base - MOD, assuming that this |
859 | computation does not overflow, and that | |
860 | iv0->base - MOD <= iv1->base. */ | |
e36dc339 | 861 | if (!fv_comp_no_overflow) |
7f17528a | 862 | { |
5be014d5 AP |
863 | bound = fold_build2 (PLUS_EXPR, type1, |
864 | TYPE_MIN_VALUE (type1), tmod); | |
7f17528a ZD |
865 | assumption = fold_build2 (GE_EXPR, boolean_type_node, |
866 | iv0->base, bound); | |
6e682d7e | 867 | if (integer_zerop (assumption)) |
b3ce5b6e | 868 | goto end; |
7f17528a | 869 | } |
b3ce5b6e ZD |
870 | if (mpz_cmp (mmod, bnds->below) < 0) |
871 | noloop = boolean_false_node; | |
97b4ba9f JJ |
872 | else if (POINTER_TYPE_P (type)) |
873 | noloop = fold_build2 (GT_EXPR, boolean_type_node, | |
5d49b6a7 RG |
874 | fold_build_pointer_plus (iv0->base, |
875 | fold_build1 (NEGATE_EXPR, | |
876 | type1, tmod)), | |
97b4ba9f | 877 | iv1->base); |
b3ce5b6e ZD |
878 | else |
879 | noloop = fold_build2 (GT_EXPR, boolean_type_node, | |
5be014d5 | 880 | fold_build2 (MINUS_EXPR, type1, |
b3ce5b6e ZD |
881 | iv0->base, tmod), |
882 | iv1->base); | |
e9eb809d ZD |
883 | } |
884 | ||
6e682d7e | 885 | if (!integer_nonzerop (assumption)) |
7f17528a ZD |
886 | niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
887 | niter->assumptions, | |
888 | assumption); | |
6e682d7e | 889 | if (!integer_zerop (noloop)) |
7f17528a ZD |
890 | niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, |
891 | niter->may_be_zero, | |
892 | noloop); | |
b3ce5b6e | 893 | bounds_add (bnds, tree_to_double_int (mod), type); |
7f17528a | 894 | *delta = fold_build2 (PLUS_EXPR, niter_type, *delta, mod); |
b3ce5b6e ZD |
895 | |
896 | ret = true; | |
897 | end: | |
898 | mpz_clear (mmod); | |
899 | return ret; | |
7f17528a ZD |
900 | } |
901 | ||
902 | /* Add assertions to NITER that ensure that the control variable of the loop | |
903 | with ending condition IV0 < IV1 does not overflow. Types of IV0 and IV1 | |
904 | are TYPE. Returns false if we can prove that there is an overflow, true | |
905 | otherwise. STEP is the absolute value of the step. */ | |
e9eb809d | 906 | |
7f17528a ZD |
907 | static bool |
908 | assert_no_overflow_lt (tree type, affine_iv *iv0, affine_iv *iv1, | |
909 | struct tree_niter_desc *niter, tree step) | |
910 | { | |
911 | tree bound, d, assumption, diff; | |
912 | tree niter_type = TREE_TYPE (step); | |
913 | ||
6e42ce54 | 914 | if (integer_nonzerop (iv0->step)) |
e9eb809d | 915 | { |
7f17528a ZD |
916 | /* for (i = iv0->base; i < iv1->base; i += iv0->step) */ |
917 | if (iv0->no_overflow) | |
918 | return true; | |
919 | ||
920 | /* If iv0->base is a constant, we can determine the last value before | |
921 | overflow precisely; otherwise we conservatively assume | |
922 | MAX - STEP + 1. */ | |
923 | ||
924 | if (TREE_CODE (iv0->base) == INTEGER_CST) | |
e9eb809d | 925 | { |
7f17528a ZD |
926 | d = fold_build2 (MINUS_EXPR, niter_type, |
927 | fold_convert (niter_type, TYPE_MAX_VALUE (type)), | |
928 | fold_convert (niter_type, iv0->base)); | |
929 | diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step); | |
e9eb809d ZD |
930 | } |
931 | else | |
7f17528a | 932 | diff = fold_build2 (MINUS_EXPR, niter_type, step, |
ff5e9a94 | 933 | build_int_cst (niter_type, 1)); |
7f17528a ZD |
934 | bound = fold_build2 (MINUS_EXPR, type, |
935 | TYPE_MAX_VALUE (type), fold_convert (type, diff)); | |
936 | assumption = fold_build2 (LE_EXPR, boolean_type_node, | |
937 | iv1->base, bound); | |
938 | } | |
939 | else | |
940 | { | |
941 | /* for (i = iv1->base; i > iv0->base; i += iv1->step) */ | |
942 | if (iv1->no_overflow) | |
943 | return true; | |
944 | ||
945 | if (TREE_CODE (iv1->base) == INTEGER_CST) | |
e9eb809d | 946 | { |
7f17528a ZD |
947 | d = fold_build2 (MINUS_EXPR, niter_type, |
948 | fold_convert (niter_type, iv1->base), | |
949 | fold_convert (niter_type, TYPE_MIN_VALUE (type))); | |
950 | diff = fold_build2 (FLOOR_MOD_EXPR, niter_type, d, step); | |
e9eb809d | 951 | } |
7f17528a ZD |
952 | else |
953 | diff = fold_build2 (MINUS_EXPR, niter_type, step, | |
ff5e9a94 | 954 | build_int_cst (niter_type, 1)); |
7f17528a ZD |
955 | bound = fold_build2 (PLUS_EXPR, type, |
956 | TYPE_MIN_VALUE (type), fold_convert (type, diff)); | |
957 | assumption = fold_build2 (GE_EXPR, boolean_type_node, | |
958 | iv0->base, bound); | |
e9eb809d ZD |
959 | } |
960 | ||
6e682d7e | 961 | if (integer_zerop (assumption)) |
7f17528a | 962 | return false; |
6e682d7e | 963 | if (!integer_nonzerop (assumption)) |
7f17528a ZD |
964 | niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
965 | niter->assumptions, assumption); | |
b8698a0f | 966 | |
7f17528a ZD |
967 | iv0->no_overflow = true; |
968 | iv1->no_overflow = true; | |
969 | return true; | |
970 | } | |
e9eb809d | 971 | |
7f17528a | 972 | /* Add an assumption to NITER that a loop whose ending condition |
b3ce5b6e ZD |
973 | is IV0 < IV1 rolls. TYPE is the type of the control iv. BNDS |
974 | bounds the value of IV1->base - IV0->base. */ | |
7f17528a ZD |
975 | |
976 | static void | |
977 | assert_loop_rolls_lt (tree type, affine_iv *iv0, affine_iv *iv1, | |
b3ce5b6e | 978 | struct tree_niter_desc *niter, bounds *bnds) |
7f17528a ZD |
979 | { |
980 | tree assumption = boolean_true_node, bound, diff; | |
5be014d5 | 981 | tree mbz, mbzl, mbzr, type1; |
b3ce5b6e ZD |
982 | bool rolls_p, no_overflow_p; |
983 | double_int dstep; | |
984 | mpz_t mstep, max; | |
985 | ||
986 | /* We are going to compute the number of iterations as | |
987 | (iv1->base - iv0->base + step - 1) / step, computed in the unsigned | |
b8698a0f L |
988 | variant of TYPE. This formula only works if |
989 | ||
b3ce5b6e | 990 | -step + 1 <= (iv1->base - iv0->base) <= MAX - step + 1 |
b8698a0f | 991 | |
b3ce5b6e | 992 | (where MAX is the maximum value of the unsigned variant of TYPE, and |
072edf07 SP |
993 | the computations in this formula are performed in full precision, |
994 | i.e., without overflows). | |
b3ce5b6e ZD |
995 | |
996 | Usually, for loops with exit condition iv0->base + step * i < iv1->base, | |
072edf07 | 997 | we have a condition of the form iv0->base - step < iv1->base before the loop, |
b3ce5b6e ZD |
998 | and for loops iv0->base < iv1->base - step * i the condition |
999 | iv0->base < iv1->base + step, due to loop header copying, which enable us | |
1000 | to prove the lower bound. | |
b8698a0f | 1001 | |
b3ce5b6e ZD |
1002 | The upper bound is more complicated. Unless the expressions for initial |
1003 | and final value themselves contain enough information, we usually cannot | |
1004 | derive it from the context. */ | |
1005 | ||
1006 | /* First check whether the answer does not follow from the bounds we gathered | |
1007 | before. */ | |
1008 | if (integer_nonzerop (iv0->step)) | |
1009 | dstep = tree_to_double_int (iv0->step); | |
1010 | else | |
1011 | { | |
27bcd47c LC |
1012 | dstep = tree_to_double_int (iv1->step).sext (TYPE_PRECISION (type)); |
1013 | dstep = -dstep; | |
b3ce5b6e ZD |
1014 | } |
1015 | ||
1016 | mpz_init (mstep); | |
1017 | mpz_set_double_int (mstep, dstep, true); | |
1018 | mpz_neg (mstep, mstep); | |
1019 | mpz_add_ui (mstep, mstep, 1); | |
1020 | ||
1021 | rolls_p = mpz_cmp (mstep, bnds->below) <= 0; | |
1022 | ||
1023 | mpz_init (max); | |
27bcd47c | 1024 | mpz_set_double_int (max, double_int::mask (TYPE_PRECISION (type)), true); |
b3ce5b6e ZD |
1025 | mpz_add (max, max, mstep); |
1026 | no_overflow_p = (mpz_cmp (bnds->up, max) <= 0 | |
1027 | /* For pointers, only values lying inside a single object | |
1028 | can be compared or manipulated by pointer arithmetics. | |
1029 | Gcc in general does not allow or handle objects larger | |
1030 | than half of the address space, hence the upper bound | |
1031 | is satisfied for pointers. */ | |
1032 | || POINTER_TYPE_P (type)); | |
1033 | mpz_clear (mstep); | |
1034 | mpz_clear (max); | |
1035 | ||
1036 | if (rolls_p && no_overflow_p) | |
1037 | return; | |
b8698a0f | 1038 | |
5be014d5 AP |
1039 | type1 = type; |
1040 | if (POINTER_TYPE_P (type)) | |
1041 | type1 = sizetype; | |
b3ce5b6e ZD |
1042 | |
1043 | /* Now the hard part; we must formulate the assumption(s) as expressions, and | |
1044 | we must be careful not to introduce overflow. */ | |
7f17528a | 1045 | |
6e42ce54 | 1046 | if (integer_nonzerop (iv0->step)) |
e9eb809d | 1047 | { |
5be014d5 AP |
1048 | diff = fold_build2 (MINUS_EXPR, type1, |
1049 | iv0->step, build_int_cst (type1, 1)); | |
e9eb809d | 1050 | |
7f17528a ZD |
1051 | /* We need to know that iv0->base >= MIN + iv0->step - 1. Since |
1052 | 0 address never belongs to any object, we can assume this for | |
1053 | pointers. */ | |
1054 | if (!POINTER_TYPE_P (type)) | |
e9eb809d | 1055 | { |
5be014d5 | 1056 | bound = fold_build2 (PLUS_EXPR, type1, |
7f17528a ZD |
1057 | TYPE_MIN_VALUE (type), diff); |
1058 | assumption = fold_build2 (GE_EXPR, boolean_type_node, | |
1059 | iv0->base, bound); | |
e9eb809d ZD |
1060 | } |
1061 | ||
7f17528a | 1062 | /* And then we can compute iv0->base - diff, and compare it with |
b8698a0f L |
1063 | iv1->base. */ |
1064 | mbzl = fold_build2 (MINUS_EXPR, type1, | |
d24a32a1 ZD |
1065 | fold_convert (type1, iv0->base), diff); |
1066 | mbzr = fold_convert (type1, iv1->base); | |
e9eb809d | 1067 | } |
7f17528a | 1068 | else |
e9eb809d | 1069 | { |
5be014d5 AP |
1070 | diff = fold_build2 (PLUS_EXPR, type1, |
1071 | iv1->step, build_int_cst (type1, 1)); | |
7f17528a ZD |
1072 | |
1073 | if (!POINTER_TYPE_P (type)) | |
e9eb809d | 1074 | { |
5be014d5 | 1075 | bound = fold_build2 (PLUS_EXPR, type1, |
7f17528a ZD |
1076 | TYPE_MAX_VALUE (type), diff); |
1077 | assumption = fold_build2 (LE_EXPR, boolean_type_node, | |
1078 | iv1->base, bound); | |
e9eb809d ZD |
1079 | } |
1080 | ||
d24a32a1 ZD |
1081 | mbzl = fold_convert (type1, iv0->base); |
1082 | mbzr = fold_build2 (MINUS_EXPR, type1, | |
1083 | fold_convert (type1, iv1->base), diff); | |
7f17528a | 1084 | } |
e9eb809d | 1085 | |
6e682d7e | 1086 | if (!integer_nonzerop (assumption)) |
7f17528a ZD |
1087 | niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
1088 | niter->assumptions, assumption); | |
b3ce5b6e ZD |
1089 | if (!rolls_p) |
1090 | { | |
1091 | mbz = fold_build2 (GT_EXPR, boolean_type_node, mbzl, mbzr); | |
1092 | niter->may_be_zero = fold_build2 (TRUTH_OR_EXPR, boolean_type_node, | |
1093 | niter->may_be_zero, mbz); | |
1094 | } | |
7f17528a | 1095 | } |
e9eb809d | 1096 | |
7f17528a ZD |
1097 | /* Determines number of iterations of loop whose ending condition |
1098 | is IV0 < IV1. TYPE is the type of the iv. The number of | |
b3ce5b6e | 1099 | iterations is stored to NITER. BNDS bounds the difference |
e36dc339 ZD |
1100 | IV1->base - IV0->base. EXIT_MUST_BE_TAKEN is true if we know |
1101 | that the exit must be taken eventually. */ | |
7f17528a ZD |
1102 | |
1103 | static bool | |
1104 | number_of_iterations_lt (tree type, affine_iv *iv0, affine_iv *iv1, | |
1105 | struct tree_niter_desc *niter, | |
e36dc339 | 1106 | bool exit_must_be_taken, bounds *bnds) |
7f17528a ZD |
1107 | { |
1108 | tree niter_type = unsigned_type_for (type); | |
1109 | tree delta, step, s; | |
b3ce5b6e | 1110 | mpz_t mstep, tmp; |
7f17528a | 1111 | |
6e42ce54 | 1112 | if (integer_nonzerop (iv0->step)) |
17684618 ZD |
1113 | { |
1114 | niter->control = *iv0; | |
1115 | niter->cmp = LT_EXPR; | |
1116 | niter->bound = iv1->base; | |
1117 | } | |
1118 | else | |
1119 | { | |
1120 | niter->control = *iv1; | |
1121 | niter->cmp = GT_EXPR; | |
1122 | niter->bound = iv0->base; | |
1123 | } | |
1124 | ||
7f17528a ZD |
1125 | delta = fold_build2 (MINUS_EXPR, niter_type, |
1126 | fold_convert (niter_type, iv1->base), | |
1127 | fold_convert (niter_type, iv0->base)); | |
1128 | ||
1129 | /* First handle the special case that the step is +-1. */ | |
6e42ce54 ZD |
1130 | if ((integer_onep (iv0->step) && integer_zerop (iv1->step)) |
1131 | || (integer_all_onesp (iv1->step) && integer_zerop (iv0->step))) | |
7f17528a ZD |
1132 | { |
1133 | /* for (i = iv0->base; i < iv1->base; i++) | |
1134 | ||
1135 | or | |
82b85a85 | 1136 | |
7f17528a | 1137 | for (i = iv1->base; i > iv0->base; i--). |
b8698a0f | 1138 | |
7f17528a | 1139 | In both cases # of iterations is iv1->base - iv0->base, assuming that |
b3ce5b6e ZD |
1140 | iv1->base >= iv0->base. |
1141 | ||
1142 | First try to derive a lower bound on the value of | |
1143 | iv1->base - iv0->base, computed in full precision. If the difference | |
1144 | is nonnegative, we are done, otherwise we must record the | |
1145 | condition. */ | |
1146 | ||
1147 | if (mpz_sgn (bnds->below) < 0) | |
1148 | niter->may_be_zero = fold_build2 (LT_EXPR, boolean_type_node, | |
1149 | iv1->base, iv0->base); | |
7f17528a | 1150 | niter->niter = delta; |
e4fd22c6 | 1151 | niter->max = mpz_get_double_int (niter_type, bnds->up, false); |
7f17528a | 1152 | return true; |
e9eb809d | 1153 | } |
7f17528a | 1154 | |
6e42ce54 | 1155 | if (integer_nonzerop (iv0->step)) |
7f17528a | 1156 | step = fold_convert (niter_type, iv0->step); |
e9eb809d | 1157 | else |
7f17528a ZD |
1158 | step = fold_convert (niter_type, |
1159 | fold_build1 (NEGATE_EXPR, type, iv1->step)); | |
1160 | ||
1161 | /* If we can determine the final value of the control iv exactly, we can | |
1162 | transform the condition to != comparison. In particular, this will be | |
1163 | the case if DELTA is constant. */ | |
b3ce5b6e | 1164 | if (number_of_iterations_lt_to_ne (type, iv0, iv1, niter, &delta, step, |
e36dc339 | 1165 | exit_must_be_taken, bnds)) |
e9eb809d | 1166 | { |
7f17528a ZD |
1167 | affine_iv zps; |
1168 | ||
ff5e9a94 | 1169 | zps.base = build_int_cst (niter_type, 0); |
7f17528a ZD |
1170 | zps.step = step; |
1171 | /* number_of_iterations_lt_to_ne will add assumptions that ensure that | |
1172 | zps does not overflow. */ | |
1173 | zps.no_overflow = true; | |
1174 | ||
b3ce5b6e | 1175 | return number_of_iterations_ne (type, &zps, delta, niter, true, bnds); |
e9eb809d ZD |
1176 | } |
1177 | ||
7f17528a ZD |
1178 | /* Make sure that the control iv does not overflow. */ |
1179 | if (!assert_no_overflow_lt (type, iv0, iv1, niter, step)) | |
1180 | return false; | |
e9eb809d | 1181 | |
7f17528a ZD |
1182 | /* We determine the number of iterations as (delta + step - 1) / step. For |
1183 | this to work, we must know that iv1->base >= iv0->base - step + 1, | |
1184 | otherwise the loop does not roll. */ | |
b3ce5b6e | 1185 | assert_loop_rolls_lt (type, iv0, iv1, niter, bnds); |
7f17528a ZD |
1186 | |
1187 | s = fold_build2 (MINUS_EXPR, niter_type, | |
ff5e9a94 | 1188 | step, build_int_cst (niter_type, 1)); |
7f17528a ZD |
1189 | delta = fold_build2 (PLUS_EXPR, niter_type, delta, s); |
1190 | niter->niter = fold_build2 (FLOOR_DIV_EXPR, niter_type, delta, step); | |
b3ce5b6e ZD |
1191 | |
1192 | mpz_init (mstep); | |
1193 | mpz_init (tmp); | |
1194 | mpz_set_double_int (mstep, tree_to_double_int (step), true); | |
1195 | mpz_add (tmp, bnds->up, mstep); | |
1196 | mpz_sub_ui (tmp, tmp, 1); | |
1197 | mpz_fdiv_q (tmp, tmp, mstep); | |
e4fd22c6 | 1198 | niter->max = mpz_get_double_int (niter_type, tmp, false); |
b3ce5b6e ZD |
1199 | mpz_clear (mstep); |
1200 | mpz_clear (tmp); | |
1201 | ||
7f17528a | 1202 | return true; |
e9eb809d ZD |
1203 | } |
1204 | ||
7f17528a ZD |
1205 | /* Determines number of iterations of loop whose ending condition |
1206 | is IV0 <= IV1. TYPE is the type of the iv. The number of | |
e36dc339 | 1207 | iterations is stored to NITER. EXIT_MUST_BE_TAKEN is true if |
f08ac361 | 1208 | we know that this condition must eventually become false (we derived this |
7f17528a | 1209 | earlier, and possibly set NITER->assumptions to make sure this |
b3ce5b6e | 1210 | is the case). BNDS bounds the difference IV1->base - IV0->base. */ |
7f17528a ZD |
1211 | |
1212 | static bool | |
1213 | number_of_iterations_le (tree type, affine_iv *iv0, affine_iv *iv1, | |
e36dc339 | 1214 | struct tree_niter_desc *niter, bool exit_must_be_taken, |
b3ce5b6e | 1215 | bounds *bnds) |
7f17528a ZD |
1216 | { |
1217 | tree assumption; | |
5be014d5 AP |
1218 | tree type1 = type; |
1219 | if (POINTER_TYPE_P (type)) | |
1220 | type1 = sizetype; | |
7f17528a ZD |
1221 | |
1222 | /* Say that IV0 is the control variable. Then IV0 <= IV1 iff | |
1223 | IV0 < IV1 + 1, assuming that IV1 is not equal to the greatest | |
1224 | value of the type. This we must know anyway, since if it is | |
e36dc339 | 1225 | equal to this value, the loop rolls forever. We do not check |
b8698a0f | 1226 | this condition for pointer type ivs, as the code cannot rely on |
e36dc339 ZD |
1227 | the object to that the pointer points being placed at the end of |
1228 | the address space (and more pragmatically, TYPE_{MIN,MAX}_VALUE is | |
1229 | not defined for pointers). */ | |
7f17528a | 1230 | |
e36dc339 | 1231 | if (!exit_must_be_taken && !POINTER_TYPE_P (type)) |
7f17528a | 1232 | { |
6e42ce54 | 1233 | if (integer_nonzerop (iv0->step)) |
7f17528a | 1234 | assumption = fold_build2 (NE_EXPR, boolean_type_node, |
97b4ba9f | 1235 | iv1->base, TYPE_MAX_VALUE (type)); |
7f17528a ZD |
1236 | else |
1237 | assumption = fold_build2 (NE_EXPR, boolean_type_node, | |
97b4ba9f | 1238 | iv0->base, TYPE_MIN_VALUE (type)); |
7f17528a | 1239 | |
6e682d7e | 1240 | if (integer_zerop (assumption)) |
7f17528a | 1241 | return false; |
6e682d7e | 1242 | if (!integer_nonzerop (assumption)) |
7f17528a ZD |
1243 | niter->assumptions = fold_build2 (TRUTH_AND_EXPR, boolean_type_node, |
1244 | niter->assumptions, assumption); | |
1245 | } | |
1246 | ||
6e42ce54 | 1247 | if (integer_nonzerop (iv0->step)) |
97b4ba9f JJ |
1248 | { |
1249 | if (POINTER_TYPE_P (type)) | |
5d49b6a7 | 1250 | iv1->base = fold_build_pointer_plus_hwi (iv1->base, 1); |
97b4ba9f JJ |
1251 | else |
1252 | iv1->base = fold_build2 (PLUS_EXPR, type1, iv1->base, | |
1253 | build_int_cst (type1, 1)); | |
1254 | } | |
1255 | else if (POINTER_TYPE_P (type)) | |
5d49b6a7 | 1256 | iv0->base = fold_build_pointer_plus_hwi (iv0->base, -1); |
7f17528a | 1257 | else |
5be014d5 AP |
1258 | iv0->base = fold_build2 (MINUS_EXPR, type1, |
1259 | iv0->base, build_int_cst (type1, 1)); | |
b3ce5b6e | 1260 | |
5be014d5 | 1261 | bounds_add (bnds, double_int_one, type1); |
b3ce5b6e | 1262 | |
e36dc339 ZD |
1263 | return number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken, |
1264 | bnds); | |
b3ce5b6e ZD |
1265 | } |
1266 | ||
1267 | /* Dumps description of affine induction variable IV to FILE. */ | |
1268 | ||
1269 | static void | |
1270 | dump_affine_iv (FILE *file, affine_iv *iv) | |
1271 | { | |
1272 | if (!integer_zerop (iv->step)) | |
1273 | fprintf (file, "["); | |
1274 | ||
1275 | print_generic_expr (dump_file, iv->base, TDF_SLIM); | |
1276 | ||
1277 | if (!integer_zerop (iv->step)) | |
1278 | { | |
1279 | fprintf (file, ", + , "); | |
1280 | print_generic_expr (dump_file, iv->step, TDF_SLIM); | |
1281 | fprintf (file, "]%s", iv->no_overflow ? "(no_overflow)" : ""); | |
1282 | } | |
7f17528a | 1283 | } |
e9eb809d | 1284 | |
7f17528a ZD |
1285 | /* Determine the number of iterations according to condition (for staying |
1286 | inside loop) which compares two induction variables using comparison | |
1287 | operator CODE. The induction variable on left side of the comparison | |
1288 | is IV0, the right-hand side is IV1. Both induction variables must have | |
1289 | type TYPE, which must be an integer or pointer type. The steps of the | |
1290 | ivs must be constants (or NULL_TREE, which is interpreted as constant zero). | |
f08ac361 | 1291 | |
b3ce5b6e ZD |
1292 | LOOP is the loop whose number of iterations we are determining. |
1293 | ||
f08ac361 ZD |
1294 | ONLY_EXIT is true if we are sure this is the only way the loop could be |
1295 | exited (including possibly non-returning function calls, exceptions, etc.) | |
1296 | -- in this case we can use the information whether the control induction | |
1297 | variables can overflow or not in a more efficient way. | |
b8698a0f | 1298 | |
870ca331 JH |
1299 | if EVERY_ITERATION is true, we know the test is executed on every iteration. |
1300 | ||
7f17528a ZD |
1301 | The results (number of iterations and assumptions as described in |
1302 | comments at struct tree_niter_desc in tree-flow.h) are stored to NITER. | |
1303 | Returns false if it fails to determine number of iterations, true if it | |
1304 | was determined (possibly with some assumptions). */ | |
c33e657d ZD |
1305 | |
1306 | static bool | |
b3ce5b6e ZD |
1307 | number_of_iterations_cond (struct loop *loop, |
1308 | tree type, affine_iv *iv0, enum tree_code code, | |
f08ac361 | 1309 | affine_iv *iv1, struct tree_niter_desc *niter, |
870ca331 | 1310 | bool only_exit, bool every_iteration) |
e9eb809d | 1311 | { |
e36dc339 | 1312 | bool exit_must_be_taken = false, ret; |
b3ce5b6e | 1313 | bounds bnds; |
7f17528a | 1314 | |
870ca331 JH |
1315 | /* If the test is not executed every iteration, wrapping may make the test |
1316 | to pass again. | |
1317 | TODO: the overflow case can be still used as unreliable estimate of upper | |
1318 | bound. But we have no API to pass it down to number of iterations code | |
1319 | and, at present, it will not use it anyway. */ | |
1320 | if (!every_iteration | |
1321 | && (!iv0->no_overflow || !iv1->no_overflow | |
1322 | || code == NE_EXPR || code == EQ_EXPR)) | |
1323 | return false; | |
1324 | ||
7f17528a ZD |
1325 | /* The meaning of these assumptions is this: |
1326 | if !assumptions | |
1327 | then the rest of information does not have to be valid | |
1328 | if may_be_zero then the loop does not roll, even if | |
1329 | niter != 0. */ | |
1330 | niter->assumptions = boolean_true_node; | |
1331 | niter->may_be_zero = boolean_false_node; | |
1332 | niter->niter = NULL_TREE; | |
b3ce5b6e | 1333 | niter->max = double_int_zero; |
e9eb809d | 1334 | |
17684618 ZD |
1335 | niter->bound = NULL_TREE; |
1336 | niter->cmp = ERROR_MARK; | |
1337 | ||
7f17528a ZD |
1338 | /* Make < comparison from > ones, and for NE_EXPR comparisons, ensure that |
1339 | the control variable is on lhs. */ | |
1340 | if (code == GE_EXPR || code == GT_EXPR | |
6e42ce54 | 1341 | || (code == NE_EXPR && integer_zerop (iv0->step))) |
c33e657d | 1342 | { |
a6f778b2 | 1343 | SWAP (iv0, iv1); |
c33e657d ZD |
1344 | code = swap_tree_comparison (code); |
1345 | } | |
e9eb809d | 1346 | |
7f17528a | 1347 | if (POINTER_TYPE_P (type)) |
e9eb809d | 1348 | { |
7f17528a ZD |
1349 | /* Comparison of pointers is undefined unless both iv0 and iv1 point |
1350 | to the same object. If they do, the control variable cannot wrap | |
1351 | (as wrap around the bounds of memory will never return a pointer | |
1352 | that would be guaranteed to point to the same object, even if we | |
e36dc339 | 1353 | avoid undefined behavior by casting to size_t and back). */ |
7f17528a ZD |
1354 | iv0->no_overflow = true; |
1355 | iv1->no_overflow = true; | |
1356 | } | |
e9eb809d | 1357 | |
e36dc339 ZD |
1358 | /* If the control induction variable does not overflow and the only exit |
1359 | from the loop is the one that we analyze, we know it must be taken | |
1360 | eventually. */ | |
1361 | if (only_exit) | |
1362 | { | |
1363 | if (!integer_zerop (iv0->step) && iv0->no_overflow) | |
1364 | exit_must_be_taken = true; | |
1365 | else if (!integer_zerop (iv1->step) && iv1->no_overflow) | |
1366 | exit_must_be_taken = true; | |
1367 | } | |
e9eb809d | 1368 | |
7f17528a ZD |
1369 | /* We can handle the case when neither of the sides of the comparison is |
1370 | invariant, provided that the test is NE_EXPR. This rarely occurs in | |
1371 | practice, but it is simple enough to manage. */ | |
6e42ce54 | 1372 | if (!integer_zerop (iv0->step) && !integer_zerop (iv1->step)) |
7f17528a | 1373 | { |
5ece9847 | 1374 | tree step_type = POINTER_TYPE_P (type) ? sizetype : type; |
7f17528a ZD |
1375 | if (code != NE_EXPR) |
1376 | return false; | |
e9eb809d | 1377 | |
5ece9847 | 1378 | iv0->step = fold_binary_to_constant (MINUS_EXPR, step_type, |
7f17528a ZD |
1379 | iv0->step, iv1->step); |
1380 | iv0->no_overflow = false; | |
5ece9847 | 1381 | iv1->step = build_int_cst (step_type, 0); |
7f17528a ZD |
1382 | iv1->no_overflow = true; |
1383 | } | |
c33e657d | 1384 | |
7f17528a ZD |
1385 | /* If the result of the comparison is a constant, the loop is weird. More |
1386 | precise handling would be possible, but the situation is not common enough | |
1387 | to waste time on it. */ | |
6e42ce54 | 1388 | if (integer_zerop (iv0->step) && integer_zerop (iv1->step)) |
7f17528a | 1389 | return false; |
c33e657d | 1390 | |
7f17528a ZD |
1391 | /* Ignore loops of while (i-- < 10) type. */ |
1392 | if (code != NE_EXPR) | |
1393 | { | |
1394 | if (iv0->step && tree_int_cst_sign_bit (iv0->step)) | |
c33e657d | 1395 | return false; |
c33e657d | 1396 | |
6e42ce54 | 1397 | if (!integer_zerop (iv1->step) && !tree_int_cst_sign_bit (iv1->step)) |
c33e657d | 1398 | return false; |
7f17528a | 1399 | } |
e9eb809d | 1400 | |
c0220ea4 | 1401 | /* If the loop exits immediately, there is nothing to do. */ |
5a892248 RB |
1402 | tree tem = fold_binary (code, boolean_type_node, iv0->base, iv1->base); |
1403 | if (tem && integer_zerop (tem)) | |
7f17528a | 1404 | { |
ff5e9a94 | 1405 | niter->niter = build_int_cst (unsigned_type_for (type), 0); |
b3ce5b6e | 1406 | niter->max = double_int_zero; |
7f17528a ZD |
1407 | return true; |
1408 | } | |
b8698a0f | 1409 | |
7f17528a ZD |
1410 | /* OK, now we know we have a senseful loop. Handle several cases, depending |
1411 | on what comparison operator is used. */ | |
b3ce5b6e ZD |
1412 | bound_difference (loop, iv1->base, iv0->base, &bnds); |
1413 | ||
1414 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1415 | { | |
1416 | fprintf (dump_file, | |
4dad0aca | 1417 | "Analyzing # of iterations of loop %d\n", loop->num); |
b3ce5b6e ZD |
1418 | |
1419 | fprintf (dump_file, " exit condition "); | |
1420 | dump_affine_iv (dump_file, iv0); | |
1421 | fprintf (dump_file, " %s ", | |
1422 | code == NE_EXPR ? "!=" | |
1423 | : code == LT_EXPR ? "<" | |
1424 | : "<="); | |
1425 | dump_affine_iv (dump_file, iv1); | |
1426 | fprintf (dump_file, "\n"); | |
1427 | ||
1428 | fprintf (dump_file, " bounds on difference of bases: "); | |
1429 | mpz_out_str (dump_file, 10, bnds.below); | |
1430 | fprintf (dump_file, " ... "); | |
1431 | mpz_out_str (dump_file, 10, bnds.up); | |
1432 | fprintf (dump_file, "\n"); | |
1433 | } | |
1434 | ||
7f17528a ZD |
1435 | switch (code) |
1436 | { | |
1437 | case NE_EXPR: | |
6e42ce54 | 1438 | gcc_assert (integer_zerop (iv1->step)); |
b3ce5b6e | 1439 | ret = number_of_iterations_ne (type, iv0, iv1->base, niter, |
e36dc339 | 1440 | exit_must_be_taken, &bnds); |
b3ce5b6e ZD |
1441 | break; |
1442 | ||
7f17528a | 1443 | case LT_EXPR: |
e36dc339 | 1444 | ret = number_of_iterations_lt (type, iv0, iv1, niter, exit_must_be_taken, |
b3ce5b6e ZD |
1445 | &bnds); |
1446 | break; | |
1447 | ||
7f17528a | 1448 | case LE_EXPR: |
e36dc339 | 1449 | ret = number_of_iterations_le (type, iv0, iv1, niter, exit_must_be_taken, |
b3ce5b6e ZD |
1450 | &bnds); |
1451 | break; | |
1452 | ||
c33e657d ZD |
1453 | default: |
1454 | gcc_unreachable (); | |
1455 | } | |
b3ce5b6e ZD |
1456 | |
1457 | mpz_clear (bnds.up); | |
1458 | mpz_clear (bnds.below); | |
1459 | ||
1460 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1461 | { | |
1462 | if (ret) | |
1463 | { | |
1464 | fprintf (dump_file, " result:\n"); | |
1465 | if (!integer_nonzerop (niter->assumptions)) | |
1466 | { | |
1467 | fprintf (dump_file, " under assumptions "); | |
1468 | print_generic_expr (dump_file, niter->assumptions, TDF_SLIM); | |
1469 | fprintf (dump_file, "\n"); | |
1470 | } | |
1471 | ||
1472 | if (!integer_zerop (niter->may_be_zero)) | |
1473 | { | |
1474 | fprintf (dump_file, " zero if "); | |
1475 | print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM); | |
1476 | fprintf (dump_file, "\n"); | |
1477 | } | |
1478 | ||
1479 | fprintf (dump_file, " # of iterations "); | |
1480 | print_generic_expr (dump_file, niter->niter, TDF_SLIM); | |
1481 | fprintf (dump_file, ", bounded by "); | |
1482 | dump_double_int (dump_file, niter->max, true); | |
1483 | fprintf (dump_file, "\n"); | |
1484 | } | |
1485 | else | |
1486 | fprintf (dump_file, " failed\n\n"); | |
1487 | } | |
1488 | return ret; | |
e9eb809d ZD |
1489 | } |
1490 | ||
1d481ba8 ZD |
1491 | /* Substitute NEW for OLD in EXPR and fold the result. */ |
1492 | ||
1493 | static tree | |
c22940cd | 1494 | simplify_replace_tree (tree expr, tree old, tree new_tree) |
1d481ba8 ZD |
1495 | { |
1496 | unsigned i, n; | |
1497 | tree ret = NULL_TREE, e, se; | |
1498 | ||
1499 | if (!expr) | |
1500 | return NULL_TREE; | |
1501 | ||
76c85743 RG |
1502 | /* Do not bother to replace constants. */ |
1503 | if (CONSTANT_CLASS_P (old)) | |
1504 | return expr; | |
1505 | ||
1d481ba8 ZD |
1506 | if (expr == old |
1507 | || operand_equal_p (expr, old, 0)) | |
c22940cd | 1508 | return unshare_expr (new_tree); |
1d481ba8 | 1509 | |
726a989a | 1510 | if (!EXPR_P (expr)) |
1d481ba8 ZD |
1511 | return expr; |
1512 | ||
5039610b | 1513 | n = TREE_OPERAND_LENGTH (expr); |
1d481ba8 ZD |
1514 | for (i = 0; i < n; i++) |
1515 | { | |
1516 | e = TREE_OPERAND (expr, i); | |
c22940cd | 1517 | se = simplify_replace_tree (e, old, new_tree); |
1d481ba8 ZD |
1518 | if (e == se) |
1519 | continue; | |
1520 | ||
1521 | if (!ret) | |
1522 | ret = copy_node (expr); | |
1523 | ||
1524 | TREE_OPERAND (ret, i) = se; | |
1525 | } | |
1526 | ||
1527 | return (ret ? fold (ret) : expr); | |
1528 | } | |
1529 | ||
be1b5cba ZD |
1530 | /* Expand definitions of ssa names in EXPR as long as they are simple |
1531 | enough, and return the new expression. */ | |
1532 | ||
d7bf3bcf | 1533 | tree |
be1b5cba ZD |
1534 | expand_simple_operations (tree expr) |
1535 | { | |
1536 | unsigned i, n; | |
726a989a | 1537 | tree ret = NULL_TREE, e, ee, e1; |
6fff2603 | 1538 | enum tree_code code; |
726a989a | 1539 | gimple stmt; |
6fff2603 JJ |
1540 | |
1541 | if (expr == NULL_TREE) | |
1542 | return expr; | |
be1b5cba ZD |
1543 | |
1544 | if (is_gimple_min_invariant (expr)) | |
1545 | return expr; | |
1546 | ||
6fff2603 | 1547 | code = TREE_CODE (expr); |
be1b5cba ZD |
1548 | if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code))) |
1549 | { | |
5039610b | 1550 | n = TREE_OPERAND_LENGTH (expr); |
be1b5cba ZD |
1551 | for (i = 0; i < n; i++) |
1552 | { | |
1553 | e = TREE_OPERAND (expr, i); | |
1554 | ee = expand_simple_operations (e); | |
1555 | if (e == ee) | |
1556 | continue; | |
1557 | ||
1558 | if (!ret) | |
1559 | ret = copy_node (expr); | |
1560 | ||
1561 | TREE_OPERAND (ret, i) = ee; | |
1562 | } | |
1563 | ||
6ac01510 ILT |
1564 | if (!ret) |
1565 | return expr; | |
1566 | ||
1567 | fold_defer_overflow_warnings (); | |
1568 | ret = fold (ret); | |
1569 | fold_undefer_and_ignore_overflow_warnings (); | |
1570 | return ret; | |
be1b5cba ZD |
1571 | } |
1572 | ||
1573 | if (TREE_CODE (expr) != SSA_NAME) | |
1574 | return expr; | |
1575 | ||
1576 | stmt = SSA_NAME_DEF_STMT (expr); | |
726a989a | 1577 | if (gimple_code (stmt) == GIMPLE_PHI) |
b3ce5b6e ZD |
1578 | { |
1579 | basic_block src, dest; | |
1580 | ||
726a989a | 1581 | if (gimple_phi_num_args (stmt) != 1) |
b3ce5b6e ZD |
1582 | return expr; |
1583 | e = PHI_ARG_DEF (stmt, 0); | |
1584 | ||
1585 | /* Avoid propagating through loop exit phi nodes, which | |
1586 | could break loop-closed SSA form restrictions. */ | |
726a989a | 1587 | dest = gimple_bb (stmt); |
b3ce5b6e ZD |
1588 | src = single_pred (dest); |
1589 | if (TREE_CODE (e) == SSA_NAME | |
1590 | && src->loop_father != dest->loop_father) | |
1591 | return expr; | |
1592 | ||
1593 | return expand_simple_operations (e); | |
1594 | } | |
726a989a | 1595 | if (gimple_code (stmt) != GIMPLE_ASSIGN) |
be1b5cba ZD |
1596 | return expr; |
1597 | ||
c3a9b91b RB |
1598 | /* Avoid expanding to expressions that contain SSA names that need |
1599 | to take part in abnormal coalescing. */ | |
1600 | ssa_op_iter iter; | |
1601 | FOR_EACH_SSA_TREE_OPERAND (e, stmt, iter, SSA_OP_USE) | |
1602 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (e)) | |
1603 | return expr; | |
1604 | ||
726a989a RB |
1605 | e = gimple_assign_rhs1 (stmt); |
1606 | code = gimple_assign_rhs_code (stmt); | |
1607 | if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) | |
1608 | { | |
1609 | if (is_gimple_min_invariant (e)) | |
1610 | return e; | |
1611 | ||
1612 | if (code == SSA_NAME) | |
1613 | return expand_simple_operations (e); | |
1614 | ||
1615 | return expr; | |
1616 | } | |
1617 | ||
1618 | switch (code) | |
1619 | { | |
1a87cf0c | 1620 | CASE_CONVERT: |
726a989a RB |
1621 | /* Casts are simple. */ |
1622 | ee = expand_simple_operations (e); | |
1623 | return fold_build1 (code, TREE_TYPE (expr), ee); | |
1624 | ||
1625 | case PLUS_EXPR: | |
1626 | case MINUS_EXPR: | |
1627 | case POINTER_PLUS_EXPR: | |
be1b5cba | 1628 | /* And increments and decrements by a constant are simple. */ |
726a989a RB |
1629 | e1 = gimple_assign_rhs2 (stmt); |
1630 | if (!is_gimple_min_invariant (e1)) | |
1631 | return expr; | |
1632 | ||
1633 | ee = expand_simple_operations (e); | |
1634 | return fold_build2 (code, TREE_TYPE (expr), ee, e1); | |
be1b5cba | 1635 | |
726a989a RB |
1636 | default: |
1637 | return expr; | |
1638 | } | |
be1b5cba ZD |
1639 | } |
1640 | ||
e9eb809d | 1641 | /* Tries to simplify EXPR using the condition COND. Returns the simplified |
be1b5cba | 1642 | expression (or EXPR unchanged, if no simplification was possible). */ |
e9eb809d ZD |
1643 | |
1644 | static tree | |
be1b5cba | 1645 | tree_simplify_using_condition_1 (tree cond, tree expr) |
e9eb809d ZD |
1646 | { |
1647 | bool changed; | |
be1b5cba | 1648 | tree e, te, e0, e1, e2, notcond; |
e9eb809d ZD |
1649 | enum tree_code code = TREE_CODE (expr); |
1650 | ||
1651 | if (code == INTEGER_CST) | |
1652 | return expr; | |
1653 | ||
1654 | if (code == TRUTH_OR_EXPR | |
1655 | || code == TRUTH_AND_EXPR | |
1656 | || code == COND_EXPR) | |
1657 | { | |
1658 | changed = false; | |
1659 | ||
be1b5cba | 1660 | e0 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 0)); |
e9eb809d ZD |
1661 | if (TREE_OPERAND (expr, 0) != e0) |
1662 | changed = true; | |
1663 | ||
be1b5cba | 1664 | e1 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 1)); |
e9eb809d ZD |
1665 | if (TREE_OPERAND (expr, 1) != e1) |
1666 | changed = true; | |
1667 | ||
1668 | if (code == COND_EXPR) | |
1669 | { | |
be1b5cba | 1670 | e2 = tree_simplify_using_condition_1 (cond, TREE_OPERAND (expr, 2)); |
e9eb809d ZD |
1671 | if (TREE_OPERAND (expr, 2) != e2) |
1672 | changed = true; | |
1673 | } | |
1674 | else | |
1675 | e2 = NULL_TREE; | |
1676 | ||
1677 | if (changed) | |
1678 | { | |
1679 | if (code == COND_EXPR) | |
c33e657d | 1680 | expr = fold_build3 (code, boolean_type_node, e0, e1, e2); |
e9eb809d | 1681 | else |
c33e657d | 1682 | expr = fold_build2 (code, boolean_type_node, e0, e1); |
e9eb809d ZD |
1683 | } |
1684 | ||
1685 | return expr; | |
1686 | } | |
1687 | ||
1d481ba8 ZD |
1688 | /* In case COND is equality, we may be able to simplify EXPR by copy/constant |
1689 | propagation, and vice versa. Fold does not handle this, since it is | |
1690 | considered too expensive. */ | |
1691 | if (TREE_CODE (cond) == EQ_EXPR) | |
1692 | { | |
1693 | e0 = TREE_OPERAND (cond, 0); | |
1694 | e1 = TREE_OPERAND (cond, 1); | |
1695 | ||
1696 | /* We know that e0 == e1. Check whether we cannot simplify expr | |
1697 | using this fact. */ | |
1698 | e = simplify_replace_tree (expr, e0, e1); | |
6e682d7e | 1699 | if (integer_zerop (e) || integer_nonzerop (e)) |
1d481ba8 ZD |
1700 | return e; |
1701 | ||
1702 | e = simplify_replace_tree (expr, e1, e0); | |
6e682d7e | 1703 | if (integer_zerop (e) || integer_nonzerop (e)) |
1d481ba8 ZD |
1704 | return e; |
1705 | } | |
1706 | if (TREE_CODE (expr) == EQ_EXPR) | |
1707 | { | |
1708 | e0 = TREE_OPERAND (expr, 0); | |
1709 | e1 = TREE_OPERAND (expr, 1); | |
1710 | ||
1711 | /* If e0 == e1 (EXPR) implies !COND, then EXPR cannot be true. */ | |
1712 | e = simplify_replace_tree (cond, e0, e1); | |
6e682d7e | 1713 | if (integer_zerop (e)) |
1d481ba8 ZD |
1714 | return e; |
1715 | e = simplify_replace_tree (cond, e1, e0); | |
6e682d7e | 1716 | if (integer_zerop (e)) |
1d481ba8 ZD |
1717 | return e; |
1718 | } | |
1719 | if (TREE_CODE (expr) == NE_EXPR) | |
1720 | { | |
1721 | e0 = TREE_OPERAND (expr, 0); | |
1722 | e1 = TREE_OPERAND (expr, 1); | |
1723 | ||
1724 | /* If e0 == e1 (!EXPR) implies !COND, then EXPR must be true. */ | |
1725 | e = simplify_replace_tree (cond, e0, e1); | |
6e682d7e | 1726 | if (integer_zerop (e)) |
1d481ba8 ZD |
1727 | return boolean_true_node; |
1728 | e = simplify_replace_tree (cond, e1, e0); | |
6e682d7e | 1729 | if (integer_zerop (e)) |
1d481ba8 ZD |
1730 | return boolean_true_node; |
1731 | } | |
1732 | ||
be1b5cba ZD |
1733 | te = expand_simple_operations (expr); |
1734 | ||
e9eb809d ZD |
1735 | /* Check whether COND ==> EXPR. */ |
1736 | notcond = invert_truthvalue (cond); | |
2f133f46 | 1737 | e = fold_binary (TRUTH_OR_EXPR, boolean_type_node, notcond, te); |
6e682d7e | 1738 | if (e && integer_nonzerop (e)) |
e9eb809d ZD |
1739 | return e; |
1740 | ||
1741 | /* Check whether COND ==> not EXPR. */ | |
2f133f46 | 1742 | e = fold_binary (TRUTH_AND_EXPR, boolean_type_node, cond, te); |
6e682d7e | 1743 | if (e && integer_zerop (e)) |
e9eb809d ZD |
1744 | return e; |
1745 | ||
1746 | return expr; | |
1747 | } | |
1748 | ||
be1b5cba ZD |
1749 | /* Tries to simplify EXPR using the condition COND. Returns the simplified |
1750 | expression (or EXPR unchanged, if no simplification was possible). | |
1751 | Wrapper around tree_simplify_using_condition_1 that ensures that chains | |
1752 | of simple operations in definitions of ssa names in COND are expanded, | |
1753 | so that things like casts or incrementing the value of the bound before | |
1754 | the loop do not cause us to fail. */ | |
1755 | ||
1756 | static tree | |
1757 | tree_simplify_using_condition (tree cond, tree expr) | |
1758 | { | |
1759 | cond = expand_simple_operations (cond); | |
1760 | ||
1761 | return tree_simplify_using_condition_1 (cond, expr); | |
1762 | } | |
b16fb82d | 1763 | |
e9eb809d | 1764 | /* Tries to simplify EXPR using the conditions on entry to LOOP. |
e9eb809d ZD |
1765 | Returns the simplified expression (or EXPR unchanged, if no |
1766 | simplification was possible).*/ | |
1767 | ||
1768 | static tree | |
b3ce5b6e | 1769 | simplify_using_initial_conditions (struct loop *loop, tree expr) |
e9eb809d ZD |
1770 | { |
1771 | edge e; | |
1772 | basic_block bb; | |
726a989a | 1773 | gimple stmt; |
b3ce5b6e | 1774 | tree cond; |
b16fb82d | 1775 | int cnt = 0; |
e9eb809d ZD |
1776 | |
1777 | if (TREE_CODE (expr) == INTEGER_CST) | |
1778 | return expr; | |
1779 | ||
b16fb82d RG |
1780 | /* Limit walking the dominators to avoid quadraticness in |
1781 | the number of BBs times the number of loops in degenerate | |
1782 | cases. */ | |
e9eb809d | 1783 | for (bb = loop->header; |
b16fb82d | 1784 | bb != ENTRY_BLOCK_PTR && cnt < MAX_DOMINATORS_TO_WALK; |
e9eb809d ZD |
1785 | bb = get_immediate_dominator (CDI_DOMINATORS, bb)) |
1786 | { | |
c5cbcccf | 1787 | if (!single_pred_p (bb)) |
e9eb809d | 1788 | continue; |
c5cbcccf | 1789 | e = single_pred_edge (bb); |
e9eb809d ZD |
1790 | |
1791 | if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))) | |
1792 | continue; | |
1793 | ||
726a989a RB |
1794 | stmt = last_stmt (e->src); |
1795 | cond = fold_build2 (gimple_cond_code (stmt), | |
1796 | boolean_type_node, | |
1797 | gimple_cond_lhs (stmt), | |
1798 | gimple_cond_rhs (stmt)); | |
e9eb809d ZD |
1799 | if (e->flags & EDGE_FALSE_VALUE) |
1800 | cond = invert_truthvalue (cond); | |
b3ce5b6e | 1801 | expr = tree_simplify_using_condition (cond, expr); |
b16fb82d | 1802 | ++cnt; |
e9eb809d ZD |
1803 | } |
1804 | ||
1805 | return expr; | |
1806 | } | |
1807 | ||
c33e657d ZD |
1808 | /* Tries to simplify EXPR using the evolutions of the loop invariants |
1809 | in the superloops of LOOP. Returns the simplified expression | |
1810 | (or EXPR unchanged, if no simplification was possible). */ | |
1811 | ||
1812 | static tree | |
1813 | simplify_using_outer_evolutions (struct loop *loop, tree expr) | |
1814 | { | |
1815 | enum tree_code code = TREE_CODE (expr); | |
1816 | bool changed; | |
1817 | tree e, e0, e1, e2; | |
1818 | ||
1819 | if (is_gimple_min_invariant (expr)) | |
1820 | return expr; | |
1821 | ||
1822 | if (code == TRUTH_OR_EXPR | |
1823 | || code == TRUTH_AND_EXPR | |
1824 | || code == COND_EXPR) | |
1825 | { | |
1826 | changed = false; | |
1827 | ||
1828 | e0 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 0)); | |
1829 | if (TREE_OPERAND (expr, 0) != e0) | |
1830 | changed = true; | |
1831 | ||
1832 | e1 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 1)); | |
1833 | if (TREE_OPERAND (expr, 1) != e1) | |
1834 | changed = true; | |
1835 | ||
1836 | if (code == COND_EXPR) | |
1837 | { | |
1838 | e2 = simplify_using_outer_evolutions (loop, TREE_OPERAND (expr, 2)); | |
1839 | if (TREE_OPERAND (expr, 2) != e2) | |
1840 | changed = true; | |
1841 | } | |
1842 | else | |
1843 | e2 = NULL_TREE; | |
1844 | ||
1845 | if (changed) | |
1846 | { | |
1847 | if (code == COND_EXPR) | |
1848 | expr = fold_build3 (code, boolean_type_node, e0, e1, e2); | |
1849 | else | |
1850 | expr = fold_build2 (code, boolean_type_node, e0, e1); | |
1851 | } | |
1852 | ||
1853 | return expr; | |
1854 | } | |
1855 | ||
1856 | e = instantiate_parameters (loop, expr); | |
1857 | if (is_gimple_min_invariant (e)) | |
1858 | return e; | |
1859 | ||
1860 | return expr; | |
1861 | } | |
1862 | ||
f08ac361 ZD |
1863 | /* Returns true if EXIT is the only possible exit from LOOP. */ |
1864 | ||
52778e2a | 1865 | bool |
22ea9ec0 | 1866 | loop_only_exit_p (const struct loop *loop, const_edge exit) |
f08ac361 ZD |
1867 | { |
1868 | basic_block *body; | |
726a989a | 1869 | gimple_stmt_iterator bsi; |
f08ac361 | 1870 | unsigned i; |
726a989a | 1871 | gimple call; |
f08ac361 | 1872 | |
ac8f6c69 | 1873 | if (exit != single_exit (loop)) |
f08ac361 ZD |
1874 | return false; |
1875 | ||
1876 | body = get_loop_body (loop); | |
1877 | for (i = 0; i < loop->num_nodes; i++) | |
1878 | { | |
726a989a | 1879 | for (bsi = gsi_start_bb (body[i]); !gsi_end_p (bsi); gsi_next (&bsi)) |
f08ac361 | 1880 | { |
726a989a RB |
1881 | call = gsi_stmt (bsi); |
1882 | if (gimple_code (call) != GIMPLE_CALL) | |
1883 | continue; | |
1884 | ||
1885 | if (gimple_has_side_effects (call)) | |
f08ac361 ZD |
1886 | { |
1887 | free (body); | |
1888 | return false; | |
1889 | } | |
1890 | } | |
1891 | } | |
1892 | ||
1893 | free (body); | |
1894 | return true; | |
1895 | } | |
1896 | ||
e9eb809d ZD |
1897 | /* Stores description of number of iterations of LOOP derived from |
1898 | EXIT (an exit edge of the LOOP) in NITER. Returns true if some | |
1899 | useful information could be derived (and fields of NITER has | |
1900 | meaning described in comments at struct tree_niter_desc | |
f9cc1a70 PB |
1901 | declaration), false otherwise. If WARN is true and |
1902 | -Wunsafe-loop-optimizations was given, warn if the optimizer is going to use | |
cd0f6278 JH |
1903 | potentially unsafe assumptions. |
1904 | When EVERY_ITERATION is true, only tests that are known to be executed | |
1905 | every iteration are considered (i.e. only test that alone bounds the loop). | |
1906 | */ | |
e9eb809d ZD |
1907 | |
1908 | bool | |
1909 | number_of_iterations_exit (struct loop *loop, edge exit, | |
f9cc1a70 | 1910 | struct tree_niter_desc *niter, |
cd0f6278 | 1911 | bool warn, bool every_iteration) |
e9eb809d | 1912 | { |
726a989a RB |
1913 | gimple stmt; |
1914 | tree type; | |
a6f778b2 | 1915 | tree op0, op1; |
e9eb809d | 1916 | enum tree_code code; |
a6f778b2 | 1917 | affine_iv iv0, iv1; |
870ca331 | 1918 | bool safe; |
e9eb809d | 1919 | |
870ca331 JH |
1920 | safe = dominated_by_p (CDI_DOMINATORS, loop->latch, exit->src); |
1921 | ||
1922 | if (every_iteration && !safe) | |
e9eb809d ZD |
1923 | return false; |
1924 | ||
1925 | niter->assumptions = boolean_false_node; | |
1926 | stmt = last_stmt (exit->src); | |
726a989a | 1927 | if (!stmt || gimple_code (stmt) != GIMPLE_COND) |
e9eb809d ZD |
1928 | return false; |
1929 | ||
1930 | /* We want the condition for staying inside loop. */ | |
726a989a | 1931 | code = gimple_cond_code (stmt); |
e9eb809d | 1932 | if (exit->flags & EDGE_TRUE_VALUE) |
726a989a | 1933 | code = invert_tree_comparison (code, false); |
e9eb809d | 1934 | |
e9eb809d ZD |
1935 | switch (code) |
1936 | { | |
1937 | case GT_EXPR: | |
1938 | case GE_EXPR: | |
e9eb809d ZD |
1939 | case LT_EXPR: |
1940 | case LE_EXPR: | |
870ca331 | 1941 | case NE_EXPR: |
e9eb809d ZD |
1942 | break; |
1943 | ||
1944 | default: | |
1945 | return false; | |
1946 | } | |
b8698a0f | 1947 | |
726a989a RB |
1948 | op0 = gimple_cond_lhs (stmt); |
1949 | op1 = gimple_cond_rhs (stmt); | |
e9eb809d ZD |
1950 | type = TREE_TYPE (op0); |
1951 | ||
1952 | if (TREE_CODE (type) != INTEGER_TYPE | |
b3393f1f | 1953 | && !POINTER_TYPE_P (type)) |
e9eb809d | 1954 | return false; |
b8698a0f | 1955 | |
f017bf5e | 1956 | if (!simple_iv (loop, loop_containing_stmt (stmt), op0, &iv0, false)) |
e9eb809d | 1957 | return false; |
f017bf5e | 1958 | if (!simple_iv (loop, loop_containing_stmt (stmt), op1, &iv1, false)) |
e9eb809d ZD |
1959 | return false; |
1960 | ||
6ac01510 | 1961 | /* We don't want to see undefined signed overflow warnings while |
ea2c620c | 1962 | computing the number of iterations. */ |
6ac01510 ILT |
1963 | fold_defer_overflow_warnings (); |
1964 | ||
7f17528a ZD |
1965 | iv0.base = expand_simple_operations (iv0.base); |
1966 | iv1.base = expand_simple_operations (iv1.base); | |
b3ce5b6e | 1967 | if (!number_of_iterations_cond (loop, type, &iv0, code, &iv1, niter, |
870ca331 | 1968 | loop_only_exit_p (loop, exit), safe)) |
6ac01510 ILT |
1969 | { |
1970 | fold_undefer_and_ignore_overflow_warnings (); | |
1971 | return false; | |
1972 | } | |
c33e657d ZD |
1973 | |
1974 | if (optimize >= 3) | |
1975 | { | |
1976 | niter->assumptions = simplify_using_outer_evolutions (loop, | |
1977 | niter->assumptions); | |
1978 | niter->may_be_zero = simplify_using_outer_evolutions (loop, | |
1979 | niter->may_be_zero); | |
1980 | niter->niter = simplify_using_outer_evolutions (loop, niter->niter); | |
1981 | } | |
e9eb809d | 1982 | |
e9eb809d ZD |
1983 | niter->assumptions |
1984 | = simplify_using_initial_conditions (loop, | |
b3ce5b6e | 1985 | niter->assumptions); |
e9eb809d ZD |
1986 | niter->may_be_zero |
1987 | = simplify_using_initial_conditions (loop, | |
b3ce5b6e | 1988 | niter->may_be_zero); |
f9cc1a70 | 1989 | |
6ac01510 ILT |
1990 | fold_undefer_and_ignore_overflow_warnings (); |
1991 | ||
f2a1b469 JH |
1992 | /* If NITER has simplified into a constant, update MAX. */ |
1993 | if (TREE_CODE (niter->niter) == INTEGER_CST) | |
1994 | niter->max = tree_to_double_int (niter->niter); | |
1995 | ||
f9cc1a70 PB |
1996 | if (integer_onep (niter->assumptions)) |
1997 | return true; | |
1998 | ||
1999 | /* With -funsafe-loop-optimizations we assume that nothing bad can happen. | |
2000 | But if we can prove that there is overflow or some other source of weird | |
2001 | behavior, ignore the loop even with -funsafe-loop-optimizations. */ | |
1551d44a | 2002 | if (integer_zerop (niter->assumptions) || !single_exit (loop)) |
f9cc1a70 PB |
2003 | return false; |
2004 | ||
2005 | if (flag_unsafe_loop_optimizations) | |
2006 | niter->assumptions = boolean_true_node; | |
2007 | ||
2008 | if (warn) | |
2009 | { | |
2010 | const char *wording; | |
726a989a | 2011 | location_t loc = gimple_location (stmt); |
b8698a0f | 2012 | |
f9cc1a70 PB |
2013 | /* We can provide a more specific warning if one of the operator is |
2014 | constant and the other advances by +1 or -1. */ | |
6e42ce54 ZD |
2015 | if (!integer_zerop (iv1.step) |
2016 | ? (integer_zerop (iv0.step) | |
a6f778b2 | 2017 | && (integer_onep (iv1.step) || integer_all_onesp (iv1.step))) |
6e42ce54 | 2018 | : (integer_onep (iv0.step) || integer_all_onesp (iv0.step))) |
f9cc1a70 PB |
2019 | wording = |
2020 | flag_unsafe_loop_optimizations | |
2021 | ? N_("assuming that the loop is not infinite") | |
2022 | : N_("cannot optimize possibly infinite loops"); | |
2023 | else | |
b8698a0f | 2024 | wording = |
f9cc1a70 PB |
2025 | flag_unsafe_loop_optimizations |
2026 | ? N_("assuming that the loop counter does not overflow") | |
2027 | : N_("cannot optimize loop, the loop counter may overflow"); | |
2028 | ||
fab922b1 MLI |
2029 | warning_at ((LOCATION_LINE (loc) > 0) ? loc : input_location, |
2030 | OPT_Wunsafe_loop_optimizations, "%s", gettext (wording)); | |
f9cc1a70 PB |
2031 | } |
2032 | ||
2033 | return flag_unsafe_loop_optimizations; | |
e9eb809d ZD |
2034 | } |
2035 | ||
ca4c3169 ZD |
2036 | /* Try to determine the number of iterations of LOOP. If we succeed, |
2037 | expression giving number of iterations is returned and *EXIT is | |
2038 | set to the edge from that the information is obtained. Otherwise | |
2039 | chrec_dont_know is returned. */ | |
2040 | ||
2041 | tree | |
2042 | find_loop_niter (struct loop *loop, edge *exit) | |
2043 | { | |
ca83d385 | 2044 | unsigned i; |
9771b263 | 2045 | vec<edge> exits = get_loop_exit_edges (loop); |
ca4c3169 ZD |
2046 | edge ex; |
2047 | tree niter = NULL_TREE, aniter; | |
2048 | struct tree_niter_desc desc; | |
2049 | ||
2050 | *exit = NULL; | |
9771b263 | 2051 | FOR_EACH_VEC_ELT (exits, i, ex) |
ca4c3169 | 2052 | { |
f9cc1a70 | 2053 | if (!number_of_iterations_exit (loop, ex, &desc, false)) |
ca4c3169 ZD |
2054 | continue; |
2055 | ||
6e682d7e | 2056 | if (integer_nonzerop (desc.may_be_zero)) |
ca4c3169 ZD |
2057 | { |
2058 | /* We exit in the first iteration through this exit. | |
2059 | We won't find anything better. */ | |
ff5e9a94 | 2060 | niter = build_int_cst (unsigned_type_node, 0); |
ca4c3169 ZD |
2061 | *exit = ex; |
2062 | break; | |
2063 | } | |
2064 | ||
6e682d7e | 2065 | if (!integer_zerop (desc.may_be_zero)) |
ca4c3169 ZD |
2066 | continue; |
2067 | ||
2068 | aniter = desc.niter; | |
2069 | ||
2070 | if (!niter) | |
2071 | { | |
2072 | /* Nothing recorded yet. */ | |
2073 | niter = aniter; | |
2074 | *exit = ex; | |
2075 | continue; | |
2076 | } | |
2077 | ||
2078 | /* Prefer constants, the lower the better. */ | |
2079 | if (TREE_CODE (aniter) != INTEGER_CST) | |
2080 | continue; | |
2081 | ||
2082 | if (TREE_CODE (niter) != INTEGER_CST) | |
2083 | { | |
2084 | niter = aniter; | |
2085 | *exit = ex; | |
2086 | continue; | |
2087 | } | |
2088 | ||
2089 | if (tree_int_cst_lt (aniter, niter)) | |
2090 | { | |
2091 | niter = aniter; | |
2092 | *exit = ex; | |
2093 | continue; | |
2094 | } | |
2095 | } | |
9771b263 | 2096 | exits.release (); |
ca4c3169 ZD |
2097 | |
2098 | return niter ? niter : chrec_dont_know; | |
2099 | } | |
2100 | ||
f87c9042 JH |
2101 | /* Return true if loop is known to have bounded number of iterations. */ |
2102 | ||
2103 | bool | |
2104 | finite_loop_p (struct loop *loop) | |
2105 | { | |
1bc60b18 | 2106 | double_int nit; |
9e3920e9 | 2107 | int flags; |
f87c9042 JH |
2108 | |
2109 | if (flag_unsafe_loop_optimizations) | |
2110 | return true; | |
9e3920e9 JJ |
2111 | flags = flags_from_decl_or_type (current_function_decl); |
2112 | if ((flags & (ECF_CONST|ECF_PURE)) && !(flags & ECF_LOOPING_CONST_OR_PURE)) | |
f87c9042 JH |
2113 | { |
2114 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2115 | fprintf (dump_file, "Found loop %i to be finite: it is within pure or const function.\n", | |
2116 | loop->num); | |
2117 | return true; | |
2118 | } | |
b8698a0f | 2119 | |
1bc60b18 JH |
2120 | if (loop->any_upper_bound |
2121 | || max_loop_iterations (loop, &nit)) | |
f87c9042 | 2122 | { |
1bc60b18 JH |
2123 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2124 | fprintf (dump_file, "Found loop %i to be finite: upper bound found.\n", | |
2125 | loop->num); | |
2126 | return true; | |
f87c9042 | 2127 | } |
1bc60b18 | 2128 | return false; |
f87c9042 JH |
2129 | } |
2130 | ||
e9eb809d ZD |
2131 | /* |
2132 | ||
2133 | Analysis of a number of iterations of a loop by a brute-force evaluation. | |
2134 | ||
2135 | */ | |
2136 | ||
2137 | /* Bound on the number of iterations we try to evaluate. */ | |
2138 | ||
2139 | #define MAX_ITERATIONS_TO_TRACK \ | |
2140 | ((unsigned) PARAM_VALUE (PARAM_MAX_ITERATIONS_TO_TRACK)) | |
2141 | ||
2142 | /* Returns the loop phi node of LOOP such that ssa name X is derived from its | |
2143 | result by a chain of operations such that all but exactly one of their | |
2144 | operands are constants. */ | |
2145 | ||
726a989a | 2146 | static gimple |
e9eb809d ZD |
2147 | chain_of_csts_start (struct loop *loop, tree x) |
2148 | { | |
726a989a | 2149 | gimple stmt = SSA_NAME_DEF_STMT (x); |
f47c96aa | 2150 | tree use; |
726a989a RB |
2151 | basic_block bb = gimple_bb (stmt); |
2152 | enum tree_code code; | |
e9eb809d ZD |
2153 | |
2154 | if (!bb | |
2155 | || !flow_bb_inside_loop_p (loop, bb)) | |
726a989a | 2156 | return NULL; |
b8698a0f | 2157 | |
726a989a | 2158 | if (gimple_code (stmt) == GIMPLE_PHI) |
e9eb809d ZD |
2159 | { |
2160 | if (bb == loop->header) | |
2161 | return stmt; | |
2162 | ||
726a989a | 2163 | return NULL; |
e9eb809d ZD |
2164 | } |
2165 | ||
726a989a RB |
2166 | if (gimple_code (stmt) != GIMPLE_ASSIGN) |
2167 | return NULL; | |
e9eb809d | 2168 | |
726a989a RB |
2169 | code = gimple_assign_rhs_code (stmt); |
2170 | if (gimple_references_memory_p (stmt) | |
726a989a | 2171 | || TREE_CODE_CLASS (code) == tcc_reference |
5006671f RG |
2172 | || (code == ADDR_EXPR |
2173 | && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))) | |
726a989a | 2174 | return NULL; |
f47c96aa AM |
2175 | |
2176 | use = SINGLE_SSA_TREE_OPERAND (stmt, SSA_OP_USE); | |
5006671f | 2177 | if (use == NULL_TREE) |
726a989a | 2178 | return NULL; |
e9eb809d | 2179 | |
f47c96aa | 2180 | return chain_of_csts_start (loop, use); |
e9eb809d ZD |
2181 | } |
2182 | ||
2183 | /* Determines whether the expression X is derived from a result of a phi node | |
2184 | in header of LOOP such that | |
2185 | ||
2186 | * the derivation of X consists only from operations with constants | |
2187 | * the initial value of the phi node is constant | |
2188 | * the value of the phi node in the next iteration can be derived from the | |
2189 | value in the current iteration by a chain of operations with constants. | |
b8698a0f | 2190 | |
726a989a | 2191 | If such phi node exists, it is returned, otherwise NULL is returned. */ |
e9eb809d | 2192 | |
726a989a | 2193 | static gimple |
e9eb809d ZD |
2194 | get_base_for (struct loop *loop, tree x) |
2195 | { | |
726a989a RB |
2196 | gimple phi; |
2197 | tree init, next; | |
e9eb809d ZD |
2198 | |
2199 | if (is_gimple_min_invariant (x)) | |
726a989a | 2200 | return NULL; |
e9eb809d ZD |
2201 | |
2202 | phi = chain_of_csts_start (loop, x); | |
2203 | if (!phi) | |
726a989a | 2204 | return NULL; |
e9eb809d ZD |
2205 | |
2206 | init = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); | |
2207 | next = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); | |
2208 | ||
2209 | if (TREE_CODE (next) != SSA_NAME) | |
726a989a | 2210 | return NULL; |
e9eb809d ZD |
2211 | |
2212 | if (!is_gimple_min_invariant (init)) | |
726a989a | 2213 | return NULL; |
e9eb809d ZD |
2214 | |
2215 | if (chain_of_csts_start (loop, next) != phi) | |
726a989a | 2216 | return NULL; |
e9eb809d ZD |
2217 | |
2218 | return phi; | |
2219 | } | |
2220 | ||
b8698a0f L |
2221 | /* Given an expression X, then |
2222 | ||
ed52affe | 2223 | * if X is NULL_TREE, we return the constant BASE. |
e9eb809d ZD |
2224 | * otherwise X is a SSA name, whose value in the considered loop is derived |
2225 | by a chain of operations with constant from a result of a phi node in | |
2226 | the header of the loop. Then we return value of X when the value of the | |
2227 | result of this phi node is given by the constant BASE. */ | |
2228 | ||
2229 | static tree | |
2230 | get_val_for (tree x, tree base) | |
2231 | { | |
726a989a | 2232 | gimple stmt; |
e9eb809d | 2233 | |
ed52affe RG |
2234 | gcc_assert (is_gimple_min_invariant (base)); |
2235 | ||
e9eb809d ZD |
2236 | if (!x) |
2237 | return base; | |
2238 | ||
2239 | stmt = SSA_NAME_DEF_STMT (x); | |
726a989a | 2240 | if (gimple_code (stmt) == GIMPLE_PHI) |
e9eb809d ZD |
2241 | return base; |
2242 | ||
726a989a RB |
2243 | gcc_assert (is_gimple_assign (stmt)); |
2244 | ||
2245 | /* STMT must be either an assignment of a single SSA name or an | |
2246 | expression involving an SSA name and a constant. Try to fold that | |
2247 | expression using the value for the SSA name. */ | |
0f336c35 RG |
2248 | if (gimple_assign_ssa_name_copy_p (stmt)) |
2249 | return get_val_for (gimple_assign_rhs1 (stmt), base); | |
2250 | else if (gimple_assign_rhs_class (stmt) == GIMPLE_UNARY_RHS | |
2251 | && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME) | |
2252 | { | |
2253 | return fold_build1 (gimple_assign_rhs_code (stmt), | |
2254 | gimple_expr_type (stmt), | |
2255 | get_val_for (gimple_assign_rhs1 (stmt), base)); | |
2256 | } | |
2257 | else if (gimple_assign_rhs_class (stmt) == GIMPLE_BINARY_RHS) | |
726a989a | 2258 | { |
0f336c35 RG |
2259 | tree rhs1 = gimple_assign_rhs1 (stmt); |
2260 | tree rhs2 = gimple_assign_rhs2 (stmt); | |
2261 | if (TREE_CODE (rhs1) == SSA_NAME) | |
2262 | rhs1 = get_val_for (rhs1, base); | |
2263 | else if (TREE_CODE (rhs2) == SSA_NAME) | |
2264 | rhs2 = get_val_for (rhs2, base); | |
2265 | else | |
2266 | gcc_unreachable (); | |
2267 | return fold_build2 (gimple_assign_rhs_code (stmt), | |
2268 | gimple_expr_type (stmt), rhs1, rhs2); | |
f47c96aa | 2269 | } |
726a989a | 2270 | else |
0f336c35 | 2271 | gcc_unreachable (); |
e9eb809d ZD |
2272 | } |
2273 | ||
726a989a | 2274 | |
e9eb809d ZD |
2275 | /* Tries to count the number of iterations of LOOP till it exits by EXIT |
2276 | by brute force -- i.e. by determining the value of the operands of the | |
2277 | condition at EXIT in first few iterations of the loop (assuming that | |
2278 | these values are constant) and determining the first one in that the | |
2279 | condition is not satisfied. Returns the constant giving the number | |
2280 | of the iterations of LOOP if successful, chrec_dont_know otherwise. */ | |
2281 | ||
2282 | tree | |
2283 | loop_niter_by_eval (struct loop *loop, edge exit) | |
2284 | { | |
726a989a RB |
2285 | tree acnd; |
2286 | tree op[2], val[2], next[2], aval[2]; | |
2287 | gimple phi, cond; | |
e9eb809d ZD |
2288 | unsigned i, j; |
2289 | enum tree_code cmp; | |
2290 | ||
2291 | cond = last_stmt (exit->src); | |
726a989a | 2292 | if (!cond || gimple_code (cond) != GIMPLE_COND) |
e9eb809d ZD |
2293 | return chrec_dont_know; |
2294 | ||
726a989a | 2295 | cmp = gimple_cond_code (cond); |
e9eb809d | 2296 | if (exit->flags & EDGE_TRUE_VALUE) |
726a989a | 2297 | cmp = invert_tree_comparison (cmp, false); |
e9eb809d | 2298 | |
e9eb809d ZD |
2299 | switch (cmp) |
2300 | { | |
2301 | case EQ_EXPR: | |
2302 | case NE_EXPR: | |
2303 | case GT_EXPR: | |
2304 | case GE_EXPR: | |
2305 | case LT_EXPR: | |
2306 | case LE_EXPR: | |
726a989a RB |
2307 | op[0] = gimple_cond_lhs (cond); |
2308 | op[1] = gimple_cond_rhs (cond); | |
e9eb809d ZD |
2309 | break; |
2310 | ||
2311 | default: | |
2312 | return chrec_dont_know; | |
2313 | } | |
2314 | ||
2315 | for (j = 0; j < 2; j++) | |
2316 | { | |
726a989a | 2317 | if (is_gimple_min_invariant (op[j])) |
e9eb809d | 2318 | { |
726a989a RB |
2319 | val[j] = op[j]; |
2320 | next[j] = NULL_TREE; | |
2321 | op[j] = NULL_TREE; | |
e9eb809d ZD |
2322 | } |
2323 | else | |
2324 | { | |
726a989a RB |
2325 | phi = get_base_for (loop, op[j]); |
2326 | if (!phi) | |
2327 | return chrec_dont_know; | |
2328 | val[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); | |
2329 | next[j] = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); | |
e9eb809d ZD |
2330 | } |
2331 | } | |
2332 | ||
6ac01510 ILT |
2333 | /* Don't issue signed overflow warnings. */ |
2334 | fold_defer_overflow_warnings (); | |
2335 | ||
e9eb809d ZD |
2336 | for (i = 0; i < MAX_ITERATIONS_TO_TRACK; i++) |
2337 | { | |
2338 | for (j = 0; j < 2; j++) | |
2339 | aval[j] = get_val_for (op[j], val[j]); | |
2340 | ||
2f133f46 | 2341 | acnd = fold_binary (cmp, boolean_type_node, aval[0], aval[1]); |
6e682d7e | 2342 | if (acnd && integer_zerop (acnd)) |
e9eb809d | 2343 | { |
6ac01510 | 2344 | fold_undefer_and_ignore_overflow_warnings (); |
e9eb809d ZD |
2345 | if (dump_file && (dump_flags & TDF_DETAILS)) |
2346 | fprintf (dump_file, | |
2347 | "Proved that loop %d iterates %d times using brute force.\n", | |
2348 | loop->num, i); | |
7d60be94 | 2349 | return build_int_cst (unsigned_type_node, i); |
e9eb809d ZD |
2350 | } |
2351 | ||
2352 | for (j = 0; j < 2; j++) | |
ed52affe RG |
2353 | { |
2354 | val[j] = get_val_for (next[j], val[j]); | |
2355 | if (!is_gimple_min_invariant (val[j])) | |
6ac01510 ILT |
2356 | { |
2357 | fold_undefer_and_ignore_overflow_warnings (); | |
2358 | return chrec_dont_know; | |
2359 | } | |
ed52affe | 2360 | } |
e9eb809d ZD |
2361 | } |
2362 | ||
6ac01510 ILT |
2363 | fold_undefer_and_ignore_overflow_warnings (); |
2364 | ||
e9eb809d ZD |
2365 | return chrec_dont_know; |
2366 | } | |
2367 | ||
2368 | /* Finds the exit of the LOOP by that the loop exits after a constant | |
2369 | number of iterations and stores the exit edge to *EXIT. The constant | |
2370 | giving the number of iterations of LOOP is returned. The number of | |
2371 | iterations is determined using loop_niter_by_eval (i.e. by brute force | |
2372 | evaluation). If we are unable to find the exit for that loop_niter_by_eval | |
2373 | determines the number of iterations, chrec_dont_know is returned. */ | |
2374 | ||
2375 | tree | |
2376 | find_loop_niter_by_eval (struct loop *loop, edge *exit) | |
2377 | { | |
ca83d385 | 2378 | unsigned i; |
9771b263 | 2379 | vec<edge> exits = get_loop_exit_edges (loop); |
e9eb809d ZD |
2380 | edge ex; |
2381 | tree niter = NULL_TREE, aniter; | |
2382 | ||
2383 | *exit = NULL; | |
2cee1509 RG |
2384 | |
2385 | /* Loops with multiple exits are expensive to handle and less important. */ | |
2386 | if (!flag_expensive_optimizations | |
9771b263 | 2387 | && exits.length () > 1) |
f5843d08 | 2388 | { |
9771b263 | 2389 | exits.release (); |
f5843d08 RG |
2390 | return chrec_dont_know; |
2391 | } | |
2cee1509 | 2392 | |
9771b263 | 2393 | FOR_EACH_VEC_ELT (exits, i, ex) |
e9eb809d | 2394 | { |
e9eb809d ZD |
2395 | if (!just_once_each_iteration_p (loop, ex->src)) |
2396 | continue; | |
2397 | ||
2398 | aniter = loop_niter_by_eval (loop, ex); | |
ca4c3169 | 2399 | if (chrec_contains_undetermined (aniter)) |
e9eb809d ZD |
2400 | continue; |
2401 | ||
2402 | if (niter | |
ca4c3169 | 2403 | && !tree_int_cst_lt (aniter, niter)) |
e9eb809d ZD |
2404 | continue; |
2405 | ||
2406 | niter = aniter; | |
2407 | *exit = ex; | |
2408 | } | |
9771b263 | 2409 | exits.release (); |
e9eb809d ZD |
2410 | |
2411 | return niter ? niter : chrec_dont_know; | |
2412 | } | |
2413 | ||
2414 | /* | |
2415 | ||
2416 | Analysis of upper bounds on number of iterations of a loop. | |
2417 | ||
2418 | */ | |
2419 | ||
726a989a RB |
2420 | static double_int derive_constant_upper_bound_ops (tree, tree, |
2421 | enum tree_code, tree); | |
2422 | ||
2423 | /* Returns a constant upper bound on the value of the right-hand side of | |
2424 | an assignment statement STMT. */ | |
2425 | ||
2426 | static double_int | |
2427 | derive_constant_upper_bound_assign (gimple stmt) | |
2428 | { | |
2429 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
2430 | tree op0 = gimple_assign_rhs1 (stmt); | |
2431 | tree op1 = gimple_assign_rhs2 (stmt); | |
2432 | ||
2433 | return derive_constant_upper_bound_ops (TREE_TYPE (gimple_assign_lhs (stmt)), | |
2434 | op0, code, op1); | |
2435 | } | |
2436 | ||
0ad1d5a1 ZD |
2437 | /* Returns a constant upper bound on the value of expression VAL. VAL |
2438 | is considered to be unsigned. If its type is signed, its value must | |
b3ce5b6e | 2439 | be nonnegative. */ |
b8698a0f | 2440 | |
0ad1d5a1 | 2441 | static double_int |
726a989a RB |
2442 | derive_constant_upper_bound (tree val) |
2443 | { | |
2444 | enum tree_code code; | |
2445 | tree op0, op1; | |
2446 | ||
2447 | extract_ops_from_tree (val, &code, &op0, &op1); | |
2448 | return derive_constant_upper_bound_ops (TREE_TYPE (val), op0, code, op1); | |
2449 | } | |
2450 | ||
2451 | /* Returns a constant upper bound on the value of expression OP0 CODE OP1, | |
2452 | whose type is TYPE. The expression is considered to be unsigned. If | |
2453 | its type is signed, its value must be nonnegative. */ | |
b8698a0f | 2454 | |
726a989a RB |
2455 | static double_int |
2456 | derive_constant_upper_bound_ops (tree type, tree op0, | |
2457 | enum tree_code code, tree op1) | |
763f4527 | 2458 | { |
726a989a | 2459 | tree subtype, maxt; |
0ad1d5a1 | 2460 | double_int bnd, max, mmax, cst; |
726a989a | 2461 | gimple stmt; |
0ad1d5a1 ZD |
2462 | |
2463 | if (INTEGRAL_TYPE_P (type)) | |
2464 | maxt = TYPE_MAX_VALUE (type); | |
2465 | else | |
2466 | maxt = upper_bound_in_type (type, type); | |
2467 | ||
2468 | max = tree_to_double_int (maxt); | |
2469 | ||
726a989a | 2470 | switch (code) |
0ad1d5a1 ZD |
2471 | { |
2472 | case INTEGER_CST: | |
726a989a | 2473 | return tree_to_double_int (op0); |
0ad1d5a1 | 2474 | |
1043771b | 2475 | CASE_CONVERT: |
0ad1d5a1 ZD |
2476 | subtype = TREE_TYPE (op0); |
2477 | if (!TYPE_UNSIGNED (subtype) | |
2478 | /* If TYPE is also signed, the fact that VAL is nonnegative implies | |
2479 | that OP0 is nonnegative. */ | |
2480 | && TYPE_UNSIGNED (type) | |
b3ce5b6e | 2481 | && !tree_expr_nonnegative_p (op0)) |
0ad1d5a1 ZD |
2482 | { |
2483 | /* If we cannot prove that the casted expression is nonnegative, | |
2484 | we cannot establish more useful upper bound than the precision | |
2485 | of the type gives us. */ | |
2486 | return max; | |
2487 | } | |
763f4527 | 2488 | |
0ad1d5a1 ZD |
2489 | /* We now know that op0 is an nonnegative value. Try deriving an upper |
2490 | bound for it. */ | |
b3ce5b6e | 2491 | bnd = derive_constant_upper_bound (op0); |
0ad1d5a1 ZD |
2492 | |
2493 | /* If the bound does not fit in TYPE, max. value of TYPE could be | |
2494 | attained. */ | |
27bcd47c | 2495 | if (max.ult (bnd)) |
0ad1d5a1 ZD |
2496 | return max; |
2497 | ||
2498 | return bnd; | |
2499 | ||
2500 | case PLUS_EXPR: | |
5be014d5 | 2501 | case POINTER_PLUS_EXPR: |
0ad1d5a1 | 2502 | case MINUS_EXPR: |
0ad1d5a1 | 2503 | if (TREE_CODE (op1) != INTEGER_CST |
b3ce5b6e | 2504 | || !tree_expr_nonnegative_p (op0)) |
0ad1d5a1 ZD |
2505 | return max; |
2506 | ||
20fb52af ZD |
2507 | /* Canonicalize to OP0 - CST. Consider CST to be signed, in order to |
2508 | choose the most logical way how to treat this constant regardless | |
2509 | of the signedness of the type. */ | |
0ad1d5a1 | 2510 | cst = tree_to_double_int (op1); |
27bcd47c | 2511 | cst = cst.sext (TYPE_PRECISION (type)); |
726a989a | 2512 | if (code != MINUS_EXPR) |
27bcd47c | 2513 | cst = -cst; |
0ad1d5a1 | 2514 | |
b3ce5b6e | 2515 | bnd = derive_constant_upper_bound (op0); |
0ad1d5a1 | 2516 | |
27bcd47c | 2517 | if (cst.is_negative ()) |
0ad1d5a1 | 2518 | { |
27bcd47c | 2519 | cst = -cst; |
0ad1d5a1 | 2520 | /* Avoid CST == 0x80000... */ |
27bcd47c | 2521 | if (cst.is_negative ()) |
0ad1d5a1 ZD |
2522 | return max;; |
2523 | ||
20fb52af | 2524 | /* OP0 + CST. We need to check that |
0ad1d5a1 ZD |
2525 | BND <= MAX (type) - CST. */ |
2526 | ||
27bcd47c LC |
2527 | mmax -= cst; |
2528 | if (bnd.ugt (mmax)) | |
0ad1d5a1 ZD |
2529 | return max; |
2530 | ||
27bcd47c | 2531 | return bnd + cst; |
0ad1d5a1 ZD |
2532 | } |
2533 | else | |
2534 | { | |
20fb52af ZD |
2535 | /* OP0 - CST, where CST >= 0. |
2536 | ||
2537 | If TYPE is signed, we have already verified that OP0 >= 0, and we | |
2538 | know that the result is nonnegative. This implies that | |
2539 | VAL <= BND - CST. | |
2540 | ||
2541 | If TYPE is unsigned, we must additionally know that OP0 >= CST, | |
2542 | otherwise the operation underflows. | |
2543 | */ | |
2544 | ||
2545 | /* This should only happen if the type is unsigned; however, for | |
b3ce5b6e | 2546 | buggy programs that use overflowing signed arithmetics even with |
20fb52af | 2547 | -fno-wrapv, this condition may also be true for signed values. */ |
27bcd47c | 2548 | if (bnd.ult (cst)) |
0ad1d5a1 ZD |
2549 | return max; |
2550 | ||
b3ce5b6e ZD |
2551 | if (TYPE_UNSIGNED (type)) |
2552 | { | |
2553 | tree tem = fold_binary (GE_EXPR, boolean_type_node, op0, | |
2554 | double_int_to_tree (type, cst)); | |
2555 | if (!tem || integer_nonzerop (tem)) | |
2556 | return max; | |
2557 | } | |
20fb52af | 2558 | |
27bcd47c | 2559 | bnd -= cst; |
0ad1d5a1 ZD |
2560 | } |
2561 | ||
2562 | return bnd; | |
2563 | ||
2564 | case FLOOR_DIV_EXPR: | |
2565 | case EXACT_DIV_EXPR: | |
0ad1d5a1 ZD |
2566 | if (TREE_CODE (op1) != INTEGER_CST |
2567 | || tree_int_cst_sign_bit (op1)) | |
2568 | return max; | |
2569 | ||
b3ce5b6e | 2570 | bnd = derive_constant_upper_bound (op0); |
27bcd47c | 2571 | return bnd.udiv (tree_to_double_int (op1), FLOOR_DIV_EXPR); |
0ad1d5a1 | 2572 | |
946e1bc7 | 2573 | case BIT_AND_EXPR: |
946e1bc7 ZD |
2574 | if (TREE_CODE (op1) != INTEGER_CST |
2575 | || tree_int_cst_sign_bit (op1)) | |
2576 | return max; | |
2577 | return tree_to_double_int (op1); | |
2578 | ||
2579 | case SSA_NAME: | |
726a989a RB |
2580 | stmt = SSA_NAME_DEF_STMT (op0); |
2581 | if (gimple_code (stmt) != GIMPLE_ASSIGN | |
2582 | || gimple_assign_lhs (stmt) != op0) | |
946e1bc7 | 2583 | return max; |
726a989a | 2584 | return derive_constant_upper_bound_assign (stmt); |
946e1bc7 | 2585 | |
b8698a0f | 2586 | default: |
0ad1d5a1 ZD |
2587 | return max; |
2588 | } | |
763f4527 ZD |
2589 | } |
2590 | ||
fbd28bc3 JJ |
2591 | /* Emit a -Waggressive-loop-optimizations warning if needed. */ |
2592 | ||
2593 | static void | |
2594 | do_warn_aggressive_loop_optimizations (struct loop *loop, | |
2595 | double_int i_bound, gimple stmt) | |
2596 | { | |
2597 | /* Don't warn if the loop doesn't have known constant bound. */ | |
2598 | if (!loop->nb_iterations | |
2599 | || TREE_CODE (loop->nb_iterations) != INTEGER_CST | |
2600 | || !warn_aggressive_loop_optimizations | |
2601 | /* To avoid warning multiple times for the same loop, | |
2602 | only start warning when we preserve loops. */ | |
2603 | || (cfun->curr_properties & PROP_loops) == 0 | |
2604 | /* Only warn once per loop. */ | |
2605 | || loop->warned_aggressive_loop_optimizations | |
2606 | /* Only warn if undefined behavior gives us lower estimate than the | |
2607 | known constant bound. */ | |
2608 | || i_bound.ucmp (tree_to_double_int (loop->nb_iterations)) >= 0 | |
2609 | /* And undefined behavior happens unconditionally. */ | |
2610 | || !dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (stmt))) | |
2611 | return; | |
2612 | ||
2613 | edge e = single_exit (loop); | |
2614 | if (e == NULL) | |
2615 | return; | |
2616 | ||
2617 | gimple estmt = last_stmt (e->src); | |
44398cbe PC |
2618 | if (warning_at (gimple_location (stmt), OPT_Waggressive_loop_optimizations, |
2619 | "iteration %E invokes undefined behavior", | |
2620 | double_int_to_tree (TREE_TYPE (loop->nb_iterations), | |
2621 | i_bound))) | |
2622 | inform (gimple_location (estmt), "containing loop"); | |
fbd28bc3 JJ |
2623 | loop->warned_aggressive_loop_optimizations = true; |
2624 | } | |
2625 | ||
b3ce5b6e | 2626 | /* Records that AT_STMT is executed at most BOUND + 1 times in LOOP. IS_EXIT |
946e1bc7 ZD |
2627 | is true if the loop is exited immediately after STMT, and this exit |
2628 | is taken at last when the STMT is executed BOUND + 1 times. | |
fa10beec | 2629 | REALISTIC is true if BOUND is expected to be close to the real number |
9bdb685e ZD |
2630 | of iterations. UPPER is true if we are sure the loop iterates at most |
2631 | BOUND times. I_BOUND is an unsigned double_int upper estimate on BOUND. */ | |
e9eb809d | 2632 | |
946e1bc7 | 2633 | static void |
b3ce5b6e | 2634 | record_estimate (struct loop *loop, tree bound, double_int i_bound, |
726a989a | 2635 | gimple at_stmt, bool is_exit, bool realistic, bool upper) |
e9eb809d | 2636 | { |
9bdb685e | 2637 | double_int delta; |
e9eb809d ZD |
2638 | |
2639 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2640 | { | |
946e1bc7 | 2641 | fprintf (dump_file, "Statement %s", is_exit ? "(exit)" : ""); |
726a989a | 2642 | print_gimple_stmt (dump_file, at_stmt, 0, TDF_SLIM); |
9bdb685e ZD |
2643 | fprintf (dump_file, " is %sexecuted at most ", |
2644 | upper ? "" : "probably "); | |
e9eb809d | 2645 | print_generic_expr (dump_file, bound, TDF_SLIM); |
763f4527 | 2646 | fprintf (dump_file, " (bounded by "); |
946e1bc7 ZD |
2647 | dump_double_int (dump_file, i_bound, true); |
2648 | fprintf (dump_file, ") + 1 times in loop %d.\n", loop->num); | |
e9eb809d ZD |
2649 | } |
2650 | ||
9bdb685e ZD |
2651 | /* If the I_BOUND is just an estimate of BOUND, it rarely is close to the |
2652 | real number of iterations. */ | |
2653 | if (TREE_CODE (bound) != INTEGER_CST) | |
2654 | realistic = false; | |
f2a1b469 JH |
2655 | else |
2656 | gcc_checking_assert (i_bound == tree_to_double_int (bound)); | |
9bdb685e ZD |
2657 | if (!upper && !realistic) |
2658 | return; | |
2659 | ||
2660 | /* If we have a guaranteed upper bound, record it in the appropriate | |
fbd28bc3 JJ |
2661 | list, unless this is an !is_exit bound (i.e. undefined behavior in |
2662 | at_stmt) in a loop with known constant number of iterations. */ | |
2663 | if (upper | |
2664 | && (is_exit | |
2665 | || loop->nb_iterations == NULL_TREE | |
2666 | || TREE_CODE (loop->nb_iterations) != INTEGER_CST)) | |
9bdb685e | 2667 | { |
a9429e29 | 2668 | struct nb_iter_bound *elt = ggc_alloc_nb_iter_bound (); |
9bdb685e ZD |
2669 | |
2670 | elt->bound = i_bound; | |
2671 | elt->stmt = at_stmt; | |
2672 | elt->is_exit = is_exit; | |
2673 | elt->next = loop->bounds; | |
2674 | loop->bounds = elt; | |
2675 | } | |
2676 | ||
cd0f6278 JH |
2677 | /* If statement is executed on every path to the loop latch, we can directly |
2678 | infer the upper bound on the # of iterations of the loop. */ | |
2679 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (at_stmt))) | |
2680 | return; | |
2681 | ||
9bdb685e | 2682 | /* Update the number of iteration estimates according to the bound. |
05322355 JH |
2683 | If at_stmt is an exit then the loop latch is executed at most BOUND times, |
2684 | otherwise it can be executed BOUND + 1 times. We will lower the estimate | |
2685 | later if such statement must be executed on last iteration */ | |
2686 | if (is_exit) | |
b4a9343c | 2687 | delta = double_int_zero; |
9bdb685e | 2688 | else |
b4a9343c | 2689 | delta = double_int_one; |
27bcd47c | 2690 | i_bound += delta; |
9bdb685e | 2691 | |
7fa7289d | 2692 | /* If an overflow occurred, ignore the result. */ |
27bcd47c | 2693 | if (i_bound.ult (delta)) |
9bdb685e ZD |
2694 | return; |
2695 | ||
fbd28bc3 JJ |
2696 | if (upper && !is_exit) |
2697 | do_warn_aggressive_loop_optimizations (loop, i_bound, at_stmt); | |
9bdb685e | 2698 | record_niter_bound (loop, i_bound, realistic, upper); |
e9eb809d ZD |
2699 | } |
2700 | ||
946e1bc7 ZD |
2701 | /* Record the estimate on number of iterations of LOOP based on the fact that |
2702 | the induction variable BASE + STEP * i evaluated in STMT does not wrap and | |
9bdb685e ZD |
2703 | its values belong to the range <LOW, HIGH>. REALISTIC is true if the |
2704 | estimated number of iterations is expected to be close to the real one. | |
2705 | UPPER is true if we are sure the induction variable does not wrap. */ | |
946e1bc7 ZD |
2706 | |
2707 | static void | |
726a989a | 2708 | record_nonwrapping_iv (struct loop *loop, tree base, tree step, gimple stmt, |
9bdb685e | 2709 | tree low, tree high, bool realistic, bool upper) |
946e1bc7 ZD |
2710 | { |
2711 | tree niter_bound, extreme, delta; | |
2712 | tree type = TREE_TYPE (base), unsigned_type; | |
b3ce5b6e | 2713 | double_int max; |
946e1bc7 | 2714 | |
6e682d7e | 2715 | if (TREE_CODE (step) != INTEGER_CST || integer_zerop (step)) |
946e1bc7 ZD |
2716 | return; |
2717 | ||
2718 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2719 | { | |
2720 | fprintf (dump_file, "Induction variable ("); | |
2721 | print_generic_expr (dump_file, TREE_TYPE (base), TDF_SLIM); | |
2722 | fprintf (dump_file, ") "); | |
2723 | print_generic_expr (dump_file, base, TDF_SLIM); | |
2724 | fprintf (dump_file, " + "); | |
2725 | print_generic_expr (dump_file, step, TDF_SLIM); | |
2726 | fprintf (dump_file, " * iteration does not wrap in statement "); | |
726a989a | 2727 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
946e1bc7 ZD |
2728 | fprintf (dump_file, " in loop %d.\n", loop->num); |
2729 | } | |
2730 | ||
2731 | unsigned_type = unsigned_type_for (type); | |
2732 | base = fold_convert (unsigned_type, base); | |
2733 | step = fold_convert (unsigned_type, step); | |
2734 | ||
2735 | if (tree_int_cst_sign_bit (step)) | |
2736 | { | |
2737 | extreme = fold_convert (unsigned_type, low); | |
2738 | if (TREE_CODE (base) != INTEGER_CST) | |
2739 | base = fold_convert (unsigned_type, high); | |
2740 | delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme); | |
2741 | step = fold_build1 (NEGATE_EXPR, unsigned_type, step); | |
2742 | } | |
2743 | else | |
2744 | { | |
2745 | extreme = fold_convert (unsigned_type, high); | |
2746 | if (TREE_CODE (base) != INTEGER_CST) | |
2747 | base = fold_convert (unsigned_type, low); | |
2748 | delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base); | |
2749 | } | |
2750 | ||
2751 | /* STMT is executed at most NITER_BOUND + 1 times, since otherwise the value | |
2752 | would get out of the range. */ | |
2753 | niter_bound = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step); | |
b3ce5b6e | 2754 | max = derive_constant_upper_bound (niter_bound); |
9bdb685e | 2755 | record_estimate (loop, niter_bound, max, stmt, false, realistic, upper); |
4839cb59 ZD |
2756 | } |
2757 | ||
946e1bc7 | 2758 | /* Determine information about number of iterations a LOOP from the index |
ac84e05e ZD |
2759 | IDX of a data reference accessed in STMT. RELIABLE is true if STMT is |
2760 | guaranteed to be executed in every iteration of LOOP. Callback for | |
2761 | for_each_index. */ | |
946e1bc7 ZD |
2762 | |
2763 | struct ilb_data | |
2764 | { | |
2765 | struct loop *loop; | |
726a989a | 2766 | gimple stmt; |
946e1bc7 ZD |
2767 | }; |
2768 | ||
2769 | static bool | |
2770 | idx_infer_loop_bounds (tree base, tree *idx, void *dta) | |
2771 | { | |
c22940cd | 2772 | struct ilb_data *data = (struct ilb_data *) dta; |
946e1bc7 ZD |
2773 | tree ev, init, step; |
2774 | tree low, high, type, next; | |
cd0f6278 | 2775 | bool sign, upper = true, at_end = false; |
946e1bc7 | 2776 | struct loop *loop = data->loop; |
870ca331 | 2777 | bool reliable = true; |
946e1bc7 | 2778 | |
9bdb685e | 2779 | if (TREE_CODE (base) != ARRAY_REF) |
946e1bc7 ZD |
2780 | return true; |
2781 | ||
9bdb685e ZD |
2782 | /* For arrays at the end of the structure, we are not guaranteed that they |
2783 | do not really extend over their declared size. However, for arrays of | |
2784 | size greater than one, this is unlikely to be intended. */ | |
2785 | if (array_at_struct_end_p (base)) | |
ac84e05e ZD |
2786 | { |
2787 | at_end = true; | |
2788 | upper = false; | |
2789 | } | |
9bdb685e | 2790 | |
8b679c9b RB |
2791 | struct loop *dloop = loop_containing_stmt (data->stmt); |
2792 | if (!dloop) | |
2793 | return true; | |
2794 | ||
2795 | ev = analyze_scalar_evolution (dloop, *idx); | |
2796 | ev = instantiate_parameters (loop, ev); | |
946e1bc7 ZD |
2797 | init = initial_condition (ev); |
2798 | step = evolution_part_in_loop_num (ev, loop->num); | |
2799 | ||
2800 | if (!init | |
2801 | || !step | |
2802 | || TREE_CODE (step) != INTEGER_CST | |
6e682d7e | 2803 | || integer_zerop (step) |
946e1bc7 ZD |
2804 | || tree_contains_chrecs (init, NULL) |
2805 | || chrec_contains_symbols_defined_in_loop (init, loop->num)) | |
2806 | return true; | |
2807 | ||
2808 | low = array_ref_low_bound (base); | |
2809 | high = array_ref_up_bound (base); | |
b8698a0f | 2810 | |
946e1bc7 ZD |
2811 | /* The case of nonconstant bounds could be handled, but it would be |
2812 | complicated. */ | |
2813 | if (TREE_CODE (low) != INTEGER_CST | |
2814 | || !high | |
2815 | || TREE_CODE (high) != INTEGER_CST) | |
2816 | return true; | |
2817 | sign = tree_int_cst_sign_bit (step); | |
2818 | type = TREE_TYPE (step); | |
9bdb685e ZD |
2819 | |
2820 | /* The array of length 1 at the end of a structure most likely extends | |
2821 | beyond its bounds. */ | |
ac84e05e | 2822 | if (at_end |
9bdb685e ZD |
2823 | && operand_equal_p (low, high, 0)) |
2824 | return true; | |
2825 | ||
946e1bc7 ZD |
2826 | /* In case the relevant bound of the array does not fit in type, or |
2827 | it does, but bound + step (in type) still belongs into the range of the | |
2828 | array, the index may wrap and still stay within the range of the array | |
2829 | (consider e.g. if the array is indexed by the full range of | |
2830 | unsigned char). | |
2831 | ||
2832 | To make things simpler, we require both bounds to fit into type, although | |
2f8e468b | 2833 | there are cases where this would not be strictly necessary. */ |
946e1bc7 ZD |
2834 | if (!int_fits_type_p (high, type) |
2835 | || !int_fits_type_p (low, type)) | |
2836 | return true; | |
2837 | low = fold_convert (type, low); | |
2838 | high = fold_convert (type, high); | |
2839 | ||
2840 | if (sign) | |
2841 | next = fold_binary (PLUS_EXPR, type, low, step); | |
2842 | else | |
2843 | next = fold_binary (PLUS_EXPR, type, high, step); | |
b8698a0f | 2844 | |
946e1bc7 ZD |
2845 | if (tree_int_cst_compare (low, next) <= 0 |
2846 | && tree_int_cst_compare (next, high) <= 0) | |
2847 | return true; | |
2848 | ||
870ca331 JH |
2849 | /* If access is not executed on every iteration, we must ensure that overlow may |
2850 | not make the access valid later. */ | |
2851 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, gimple_bb (data->stmt)) | |
2852 | && scev_probably_wraps_p (initial_condition_in_loop_num (ev, loop->num), | |
2853 | step, data->stmt, loop, true)) | |
2854 | reliable = false; | |
2855 | ||
2856 | record_nonwrapping_iv (loop, init, step, data->stmt, low, high, reliable, upper); | |
946e1bc7 ZD |
2857 | return true; |
2858 | } | |
2859 | ||
2860 | /* Determine information about number of iterations a LOOP from the bounds | |
ac84e05e ZD |
2861 | of arrays in the data reference REF accessed in STMT. RELIABLE is true if |
2862 | STMT is guaranteed to be executed in every iteration of LOOP.*/ | |
946e1bc7 ZD |
2863 | |
2864 | static void | |
cd0f6278 | 2865 | infer_loop_bounds_from_ref (struct loop *loop, gimple stmt, tree ref) |
946e1bc7 ZD |
2866 | { |
2867 | struct ilb_data data; | |
2868 | ||
2869 | data.loop = loop; | |
2870 | data.stmt = stmt; | |
2871 | for_each_index (&ref, idx_infer_loop_bounds, &data); | |
2872 | } | |
2873 | ||
2874 | /* Determine information about number of iterations of a LOOP from the way | |
ac84e05e ZD |
2875 | arrays are used in STMT. RELIABLE is true if STMT is guaranteed to be |
2876 | executed in every iteration of LOOP. */ | |
946e1bc7 ZD |
2877 | |
2878 | static void | |
cd0f6278 | 2879 | infer_loop_bounds_from_array (struct loop *loop, gimple stmt) |
946e1bc7 | 2880 | { |
726a989a | 2881 | if (is_gimple_assign (stmt)) |
946e1bc7 | 2882 | { |
726a989a RB |
2883 | tree op0 = gimple_assign_lhs (stmt); |
2884 | tree op1 = gimple_assign_rhs1 (stmt); | |
946e1bc7 ZD |
2885 | |
2886 | /* For each memory access, analyze its access function | |
2887 | and record a bound on the loop iteration domain. */ | |
2888 | if (REFERENCE_CLASS_P (op0)) | |
cd0f6278 | 2889 | infer_loop_bounds_from_ref (loop, stmt, op0); |
946e1bc7 ZD |
2890 | |
2891 | if (REFERENCE_CLASS_P (op1)) | |
cd0f6278 | 2892 | infer_loop_bounds_from_ref (loop, stmt, op1); |
946e1bc7 | 2893 | } |
726a989a | 2894 | else if (is_gimple_call (stmt)) |
946e1bc7 | 2895 | { |
726a989a RB |
2896 | tree arg, lhs; |
2897 | unsigned i, n = gimple_call_num_args (stmt); | |
946e1bc7 | 2898 | |
726a989a RB |
2899 | lhs = gimple_call_lhs (stmt); |
2900 | if (lhs && REFERENCE_CLASS_P (lhs)) | |
cd0f6278 | 2901 | infer_loop_bounds_from_ref (loop, stmt, lhs); |
726a989a RB |
2902 | |
2903 | for (i = 0; i < n; i++) | |
2904 | { | |
2905 | arg = gimple_call_arg (stmt, i); | |
2906 | if (REFERENCE_CLASS_P (arg)) | |
cd0f6278 | 2907 | infer_loop_bounds_from_ref (loop, stmt, arg); |
726a989a | 2908 | } |
946e1bc7 ZD |
2909 | } |
2910 | } | |
2911 | ||
bc69f7ff TV |
2912 | /* Determine information about number of iterations of a LOOP from the fact |
2913 | that pointer arithmetics in STMT does not overflow. */ | |
2914 | ||
2915 | static void | |
2916 | infer_loop_bounds_from_pointer_arith (struct loop *loop, gimple stmt) | |
2917 | { | |
2918 | tree def, base, step, scev, type, low, high; | |
2919 | tree var, ptr; | |
2920 | ||
2921 | if (!is_gimple_assign (stmt) | |
2922 | || gimple_assign_rhs_code (stmt) != POINTER_PLUS_EXPR) | |
2923 | return; | |
2924 | ||
2925 | def = gimple_assign_lhs (stmt); | |
2926 | if (TREE_CODE (def) != SSA_NAME) | |
2927 | return; | |
2928 | ||
2929 | type = TREE_TYPE (def); | |
2930 | if (!nowrap_type_p (type)) | |
2931 | return; | |
2932 | ||
2933 | ptr = gimple_assign_rhs1 (stmt); | |
2934 | if (!expr_invariant_in_loop_p (loop, ptr)) | |
2935 | return; | |
2936 | ||
2937 | var = gimple_assign_rhs2 (stmt); | |
2938 | if (TYPE_PRECISION (type) != TYPE_PRECISION (TREE_TYPE (var))) | |
2939 | return; | |
2940 | ||
2941 | scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def)); | |
2942 | if (chrec_contains_undetermined (scev)) | |
2943 | return; | |
2944 | ||
2945 | base = initial_condition_in_loop_num (scev, loop->num); | |
2946 | step = evolution_part_in_loop_num (scev, loop->num); | |
2947 | ||
2948 | if (!base || !step | |
2949 | || TREE_CODE (step) != INTEGER_CST | |
2950 | || tree_contains_chrecs (base, NULL) | |
2951 | || chrec_contains_symbols_defined_in_loop (base, loop->num)) | |
2952 | return; | |
2953 | ||
2954 | low = lower_bound_in_type (type, type); | |
2955 | high = upper_bound_in_type (type, type); | |
2956 | ||
0703f020 TV |
2957 | /* In C, pointer arithmetic p + 1 cannot use a NULL pointer, and p - 1 cannot |
2958 | produce a NULL pointer. The contrary would mean NULL points to an object, | |
2959 | while NULL is supposed to compare unequal with the address of all objects. | |
2960 | Furthermore, p + 1 cannot produce a NULL pointer and p - 1 cannot use a | |
2961 | NULL pointer since that would mean wrapping, which we assume here not to | |
2962 | happen. So, we can exclude NULL from the valid range of pointer | |
2963 | arithmetic. */ | |
2964 | if (flag_delete_null_pointer_checks && int_cst_value (low) == 0) | |
2965 | low = build_int_cstu (TREE_TYPE (low), TYPE_ALIGN_UNIT (TREE_TYPE (type))); | |
2966 | ||
bc69f7ff TV |
2967 | record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); |
2968 | } | |
2969 | ||
946e1bc7 ZD |
2970 | /* Determine information about number of iterations of a LOOP from the fact |
2971 | that signed arithmetics in STMT does not overflow. */ | |
2972 | ||
2973 | static void | |
726a989a | 2974 | infer_loop_bounds_from_signedness (struct loop *loop, gimple stmt) |
946e1bc7 ZD |
2975 | { |
2976 | tree def, base, step, scev, type, low, high; | |
2977 | ||
726a989a | 2978 | if (gimple_code (stmt) != GIMPLE_ASSIGN) |
946e1bc7 ZD |
2979 | return; |
2980 | ||
726a989a | 2981 | def = gimple_assign_lhs (stmt); |
946e1bc7 ZD |
2982 | |
2983 | if (TREE_CODE (def) != SSA_NAME) | |
2984 | return; | |
2985 | ||
2986 | type = TREE_TYPE (def); | |
2987 | if (!INTEGRAL_TYPE_P (type) | |
eeef0e45 | 2988 | || !TYPE_OVERFLOW_UNDEFINED (type)) |
946e1bc7 ZD |
2989 | return; |
2990 | ||
2991 | scev = instantiate_parameters (loop, analyze_scalar_evolution (loop, def)); | |
2992 | if (chrec_contains_undetermined (scev)) | |
2993 | return; | |
2994 | ||
2995 | base = initial_condition_in_loop_num (scev, loop->num); | |
2996 | step = evolution_part_in_loop_num (scev, loop->num); | |
2997 | ||
2998 | if (!base || !step | |
2999 | || TREE_CODE (step) != INTEGER_CST | |
3000 | || tree_contains_chrecs (base, NULL) | |
3001 | || chrec_contains_symbols_defined_in_loop (base, loop->num)) | |
3002 | return; | |
3003 | ||
3004 | low = lower_bound_in_type (type, type); | |
3005 | high = upper_bound_in_type (type, type); | |
3006 | ||
9bdb685e | 3007 | record_nonwrapping_iv (loop, base, step, stmt, low, high, false, true); |
946e1bc7 ZD |
3008 | } |
3009 | ||
d7770457 SP |
3010 | /* The following analyzers are extracting informations on the bounds |
3011 | of LOOP from the following undefined behaviors: | |
3012 | ||
3013 | - data references should not access elements over the statically | |
3014 | allocated size, | |
3015 | ||
3016 | - signed variables should not overflow when flag_wrapv is not set. | |
3017 | */ | |
3018 | ||
3019 | static void | |
3020 | infer_loop_bounds_from_undefined (struct loop *loop) | |
3021 | { | |
3022 | unsigned i; | |
946e1bc7 | 3023 | basic_block *bbs; |
726a989a | 3024 | gimple_stmt_iterator bsi; |
946e1bc7 | 3025 | basic_block bb; |
ac84e05e | 3026 | bool reliable; |
b8698a0f | 3027 | |
d7770457 SP |
3028 | bbs = get_loop_body (loop); |
3029 | ||
3030 | for (i = 0; i < loop->num_nodes; i++) | |
3031 | { | |
3032 | bb = bbs[i]; | |
3033 | ||
946e1bc7 | 3034 | /* If BB is not executed in each iteration of the loop, we cannot |
ac84e05e | 3035 | use the operations in it to infer reliable upper bound on the |
cd0f6278 JH |
3036 | # of iterations of the loop. However, we can use it as a guess. |
3037 | Reliable guesses come only from array bounds. */ | |
ac84e05e | 3038 | reliable = dominated_by_p (CDI_DOMINATORS, loop->latch, bb); |
946e1bc7 | 3039 | |
726a989a | 3040 | for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi)) |
946e1bc7 | 3041 | { |
726a989a | 3042 | gimple stmt = gsi_stmt (bsi); |
d7770457 | 3043 | |
cd0f6278 | 3044 | infer_loop_bounds_from_array (loop, stmt); |
ac84e05e ZD |
3045 | |
3046 | if (reliable) | |
bc69f7ff TV |
3047 | { |
3048 | infer_loop_bounds_from_signedness (loop, stmt); | |
3049 | infer_loop_bounds_from_pointer_arith (loop, stmt); | |
3050 | } | |
946e1bc7 ZD |
3051 | } |
3052 | ||
d7770457 SP |
3053 | } |
3054 | ||
3055 | free (bbs); | |
3056 | } | |
3057 | ||
ac84e05e | 3058 | |
ac84e05e | 3059 | |
73ddf95b JH |
3060 | /* Compare double ints, callback for qsort. */ |
3061 | ||
71343877 | 3062 | static int |
73ddf95b JH |
3063 | double_int_cmp (const void *p1, const void *p2) |
3064 | { | |
3065 | const double_int *d1 = (const double_int *)p1; | |
3066 | const double_int *d2 = (const double_int *)p2; | |
3067 | if (*d1 == *d2) | |
3068 | return 0; | |
3069 | if (d1->ult (*d2)) | |
3070 | return -1; | |
3071 | return 1; | |
3072 | } | |
3073 | ||
3074 | /* Return index of BOUND in BOUNDS array sorted in increasing order. | |
3075 | Lookup by binary search. */ | |
3076 | ||
71343877 | 3077 | static int |
9771b263 | 3078 | bound_index (vec<double_int> bounds, double_int bound) |
73ddf95b | 3079 | { |
9771b263 | 3080 | unsigned int end = bounds.length (); |
73ddf95b JH |
3081 | unsigned int begin = 0; |
3082 | ||
3083 | /* Find a matching index by means of a binary search. */ | |
3084 | while (begin != end) | |
3085 | { | |
3086 | unsigned int middle = (begin + end) / 2; | |
9771b263 | 3087 | double_int index = bounds[middle]; |
73ddf95b JH |
3088 | |
3089 | if (index == bound) | |
3090 | return middle; | |
3091 | else if (index.ult (bound)) | |
3092 | begin = middle + 1; | |
3093 | else | |
3094 | end = middle; | |
3095 | } | |
3096 | gcc_unreachable (); | |
3097 | } | |
3098 | ||
73ddf95b JH |
3099 | /* We recorded loop bounds only for statements dominating loop latch (and thus |
3100 | executed each loop iteration). If there are any bounds on statements not | |
3101 | dominating the loop latch we can improve the estimate by walking the loop | |
3102 | body and seeing if every path from loop header to loop latch contains | |
3103 | some bounded statement. */ | |
3104 | ||
3105 | static void | |
3106 | discover_iteration_bound_by_body_walk (struct loop *loop) | |
3107 | { | |
3108 | pointer_map_t *bb_bounds; | |
3109 | struct nb_iter_bound *elt; | |
6e1aa848 | 3110 | vec<double_int> bounds = vNULL; |
b4f9786b JJ |
3111 | vec<vec<basic_block> > queues = vNULL; |
3112 | vec<basic_block> queue = vNULL; | |
73ddf95b JH |
3113 | ptrdiff_t queue_index; |
3114 | ptrdiff_t latch_index = 0; | |
3115 | pointer_map_t *block_priority; | |
3116 | ||
3117 | /* Discover what bounds may interest us. */ | |
3118 | for (elt = loop->bounds; elt; elt = elt->next) | |
3119 | { | |
3120 | double_int bound = elt->bound; | |
3121 | ||
3122 | /* Exit terminates loop at given iteration, while non-exits produce undefined | |
3123 | effect on the next iteration. */ | |
3124 | if (!elt->is_exit) | |
4c052539 JJ |
3125 | { |
3126 | bound += double_int_one; | |
3127 | /* If an overflow occurred, ignore the result. */ | |
3128 | if (bound.is_zero ()) | |
3129 | continue; | |
3130 | } | |
73ddf95b JH |
3131 | |
3132 | if (!loop->any_upper_bound | |
3133 | || bound.ult (loop->nb_iterations_upper_bound)) | |
9771b263 | 3134 | bounds.safe_push (bound); |
73ddf95b JH |
3135 | } |
3136 | ||
3137 | /* Exit early if there is nothing to do. */ | |
9771b263 | 3138 | if (!bounds.exists ()) |
73ddf95b JH |
3139 | return; |
3140 | ||
3141 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3142 | fprintf (dump_file, " Trying to walk loop body to reduce the bound.\n"); | |
3143 | ||
3144 | /* Sort the bounds in decreasing order. */ | |
9771b263 | 3145 | qsort (bounds.address (), bounds.length (), |
73ddf95b JH |
3146 | sizeof (double_int), double_int_cmp); |
3147 | ||
3148 | /* For every basic block record the lowest bound that is guaranteed to | |
3149 | terminate the loop. */ | |
3150 | ||
3151 | bb_bounds = pointer_map_create (); | |
3152 | for (elt = loop->bounds; elt; elt = elt->next) | |
3153 | { | |
3154 | double_int bound = elt->bound; | |
3155 | if (!elt->is_exit) | |
4c052539 JJ |
3156 | { |
3157 | bound += double_int_one; | |
3158 | /* If an overflow occurred, ignore the result. */ | |
3159 | if (bound.is_zero ()) | |
3160 | continue; | |
3161 | } | |
73ddf95b JH |
3162 | |
3163 | if (!loop->any_upper_bound | |
3164 | || bound.ult (loop->nb_iterations_upper_bound)) | |
3165 | { | |
3166 | ptrdiff_t index = bound_index (bounds, bound); | |
3167 | void **entry = pointer_map_contains (bb_bounds, | |
3168 | gimple_bb (elt->stmt)); | |
3169 | if (!entry) | |
3170 | *pointer_map_insert (bb_bounds, | |
3171 | gimple_bb (elt->stmt)) = (void *)index; | |
3172 | else if ((ptrdiff_t)*entry > index) | |
3173 | *entry = (void *)index; | |
3174 | } | |
3175 | } | |
3176 | ||
3177 | block_priority = pointer_map_create (); | |
3178 | ||
3179 | /* Perform shortest path discovery loop->header ... loop->latch. | |
3180 | ||
3181 | The "distance" is given by the smallest loop bound of basic block | |
3182 | present in the path and we look for path with largest smallest bound | |
3183 | on it. | |
3184 | ||
b4f9786b | 3185 | To avoid the need for fibonacci heap on double ints we simply compress |
73ddf95b JH |
3186 | double ints into indexes to BOUNDS array and then represent the queue |
3187 | as arrays of queues for every index. | |
9771b263 | 3188 | Index of BOUNDS.length() means that the execution of given BB has |
73ddf95b JH |
3189 | no bounds determined. |
3190 | ||
3191 | VISITED is a pointer map translating basic block into smallest index | |
3192 | it was inserted into the priority queue with. */ | |
3193 | latch_index = -1; | |
3194 | ||
3195 | /* Start walk in loop header with index set to infinite bound. */ | |
9771b263 DN |
3196 | queue_index = bounds.length (); |
3197 | queues.safe_grow_cleared (queue_index + 1); | |
3198 | queue.safe_push (loop->header); | |
3199 | queues[queue_index] = queue; | |
73ddf95b JH |
3200 | *pointer_map_insert (block_priority, loop->header) = (void *)queue_index; |
3201 | ||
3202 | for (; queue_index >= 0; queue_index--) | |
3203 | { | |
3204 | if (latch_index < queue_index) | |
3205 | { | |
9771b263 | 3206 | while (queues[queue_index].length ()) |
73ddf95b JH |
3207 | { |
3208 | basic_block bb; | |
3209 | ptrdiff_t bound_index = queue_index; | |
3210 | void **entry; | |
3211 | edge e; | |
3212 | edge_iterator ei; | |
3213 | ||
9771b263 DN |
3214 | queue = queues[queue_index]; |
3215 | bb = queue.pop (); | |
73ddf95b JH |
3216 | |
3217 | /* OK, we later inserted the BB with lower priority, skip it. */ | |
3218 | if ((ptrdiff_t)*pointer_map_contains (block_priority, bb) > queue_index) | |
3219 | continue; | |
3220 | ||
3221 | /* See if we can improve the bound. */ | |
3222 | entry = pointer_map_contains (bb_bounds, bb); | |
3223 | if (entry && (ptrdiff_t)*entry < bound_index) | |
3224 | bound_index = (ptrdiff_t)*entry; | |
3225 | ||
3226 | /* Insert succesors into the queue, watch for latch edge | |
3227 | and record greatest index we saw. */ | |
3228 | FOR_EACH_EDGE (e, ei, bb->succs) | |
3229 | { | |
3230 | bool insert = false; | |
3231 | void **entry; | |
3232 | ||
3233 | if (loop_exit_edge_p (loop, e)) | |
3234 | continue; | |
3235 | ||
3236 | if (e == loop_latch_edge (loop) | |
3237 | && latch_index < bound_index) | |
3238 | latch_index = bound_index; | |
3239 | else if (!(entry = pointer_map_contains (block_priority, e->dest))) | |
3240 | { | |
3241 | insert = true; | |
3242 | *pointer_map_insert (block_priority, e->dest) = (void *)bound_index; | |
3243 | } | |
3244 | else if ((ptrdiff_t)*entry < bound_index) | |
3245 | { | |
3246 | insert = true; | |
3247 | *entry = (void *)bound_index; | |
3248 | } | |
3249 | ||
3250 | if (insert) | |
b4f9786b | 3251 | queues[bound_index].safe_push (e->dest); |
73ddf95b JH |
3252 | } |
3253 | } | |
3254 | } | |
b4f9786b | 3255 | queues[queue_index].release (); |
73ddf95b JH |
3256 | } |
3257 | ||
3258 | gcc_assert (latch_index >= 0); | |
9771b263 | 3259 | if ((unsigned)latch_index < bounds.length ()) |
73ddf95b JH |
3260 | { |
3261 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3262 | { | |
3263 | fprintf (dump_file, "Found better loop bound "); | |
9771b263 | 3264 | dump_double_int (dump_file, bounds[latch_index], true); |
73ddf95b JH |
3265 | fprintf (dump_file, "\n"); |
3266 | } | |
9771b263 | 3267 | record_niter_bound (loop, bounds[latch_index], false, true); |
73ddf95b JH |
3268 | } |
3269 | ||
9771b263 | 3270 | queues.release (); |
b4f9786b | 3271 | bounds.release (); |
73ddf95b JH |
3272 | pointer_map_destroy (bb_bounds); |
3273 | pointer_map_destroy (block_priority); | |
3274 | } | |
3275 | ||
05322355 JH |
3276 | /* See if every path cross the loop goes through a statement that is known |
3277 | to not execute at the last iteration. In that case we can decrese iteration | |
3278 | count by 1. */ | |
3279 | ||
3280 | static void | |
3281 | maybe_lower_iteration_bound (struct loop *loop) | |
3282 | { | |
0450d718 | 3283 | pointer_set_t *not_executed_last_iteration = NULL; |
05322355 JH |
3284 | struct nb_iter_bound *elt; |
3285 | bool found_exit = false; | |
6e1aa848 | 3286 | vec<basic_block> queue = vNULL; |
05322355 JH |
3287 | bitmap visited; |
3288 | ||
3289 | /* Collect all statements with interesting (i.e. lower than | |
3290 | nb_iterations_upper_bound) bound on them. | |
3291 | ||
3292 | TODO: Due to the way record_estimate choose estimates to store, the bounds | |
3293 | will be always nb_iterations_upper_bound-1. We can change this to record | |
3294 | also statements not dominating the loop latch and update the walk bellow | |
3295 | to the shortest path algorthm. */ | |
3296 | for (elt = loop->bounds; elt; elt = elt->next) | |
3297 | { | |
3298 | if (!elt->is_exit | |
3299 | && elt->bound.ult (loop->nb_iterations_upper_bound)) | |
3300 | { | |
3301 | if (!not_executed_last_iteration) | |
3302 | not_executed_last_iteration = pointer_set_create (); | |
3303 | pointer_set_insert (not_executed_last_iteration, elt->stmt); | |
3304 | } | |
3305 | } | |
3306 | if (!not_executed_last_iteration) | |
3307 | return; | |
3308 | ||
3309 | /* Start DFS walk in the loop header and see if we can reach the | |
3310 | loop latch or any of the exits (including statements with side | |
3311 | effects that may terminate the loop otherwise) without visiting | |
3312 | any of the statements known to have undefined effect on the last | |
3313 | iteration. */ | |
9771b263 | 3314 | queue.safe_push (loop->header); |
05322355 JH |
3315 | visited = BITMAP_ALLOC (NULL); |
3316 | bitmap_set_bit (visited, loop->header->index); | |
3317 | found_exit = false; | |
3318 | ||
3319 | do | |
3320 | { | |
9771b263 | 3321 | basic_block bb = queue.pop (); |
05322355 JH |
3322 | gimple_stmt_iterator gsi; |
3323 | bool stmt_found = false; | |
3324 | ||
3325 | /* Loop for possible exits and statements bounding the execution. */ | |
3326 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
3327 | { | |
3328 | gimple stmt = gsi_stmt (gsi); | |
3329 | if (pointer_set_contains (not_executed_last_iteration, stmt)) | |
3330 | { | |
3331 | stmt_found = true; | |
3332 | break; | |
3333 | } | |
3334 | if (gimple_has_side_effects (stmt)) | |
3335 | { | |
3336 | found_exit = true; | |
3337 | break; | |
3338 | } | |
3339 | } | |
3340 | if (found_exit) | |
3341 | break; | |
3342 | ||
3343 | /* If no bounding statement is found, continue the walk. */ | |
3344 | if (!stmt_found) | |
3345 | { | |
3346 | edge e; | |
3347 | edge_iterator ei; | |
3348 | ||
3349 | FOR_EACH_EDGE (e, ei, bb->succs) | |
3350 | { | |
3351 | if (loop_exit_edge_p (loop, e) | |
3352 | || e == loop_latch_edge (loop)) | |
3353 | { | |
3354 | found_exit = true; | |
3355 | break; | |
3356 | } | |
3357 | if (bitmap_set_bit (visited, e->dest->index)) | |
9771b263 | 3358 | queue.safe_push (e->dest); |
05322355 JH |
3359 | } |
3360 | } | |
3361 | } | |
9771b263 | 3362 | while (queue.length () && !found_exit); |
05322355 JH |
3363 | |
3364 | /* If every path through the loop reach bounding statement before exit, | |
3365 | then we know the last iteration of the loop will have undefined effect | |
3366 | and we can decrease number of iterations. */ | |
3367 | ||
3368 | if (!found_exit) | |
3369 | { | |
3370 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3371 | fprintf (dump_file, "Reducing loop iteration estimate by 1; " | |
3372 | "undefined statement must be executed at the last iteration.\n"); | |
3373 | record_niter_bound (loop, loop->nb_iterations_upper_bound - double_int_one, | |
3374 | false, true); | |
3375 | } | |
3376 | BITMAP_FREE (visited); | |
9771b263 | 3377 | queue.release (); |
a344216b | 3378 | pointer_set_destroy (not_executed_last_iteration); |
05322355 JH |
3379 | } |
3380 | ||
e3488283 RG |
3381 | /* Records estimates on numbers of iterations of LOOP. If USE_UNDEFINED_P |
3382 | is true also use estimates derived from undefined behavior. */ | |
e9eb809d | 3383 | |
71343877 | 3384 | static void |
421e6082 | 3385 | estimate_numbers_of_iterations_loop (struct loop *loop) |
e9eb809d | 3386 | { |
9771b263 | 3387 | vec<edge> exits; |
e9eb809d | 3388 | tree niter, type; |
ca83d385 | 3389 | unsigned i; |
e9eb809d | 3390 | struct tree_niter_desc niter_desc; |
ca83d385 | 3391 | edge ex; |
9bdb685e | 3392 | double_int bound; |
f9bf4777 | 3393 | edge likely_exit; |
e9eb809d | 3394 | |
79ebd55c | 3395 | /* Give up if we already have tried to compute an estimation. */ |
946e1bc7 | 3396 | if (loop->estimate_state != EST_NOT_COMPUTED) |
79ebd55c | 3397 | return; |
03fd03d5 | 3398 | |
9bdb685e | 3399 | loop->estimate_state = EST_AVAILABLE; |
03fd03d5 | 3400 | /* Force estimate compuation but leave any existing upper bound in place. */ |
9bdb685e | 3401 | loop->any_estimate = false; |
79ebd55c | 3402 | |
fbd28bc3 JJ |
3403 | /* Ensure that loop->nb_iterations is computed if possible. If it turns out |
3404 | to be constant, we avoid undefined behavior implied bounds and instead | |
3405 | diagnose those loops with -Waggressive-loop-optimizations. */ | |
3406 | number_of_latch_executions (loop); | |
3407 | ||
ca83d385 | 3408 | exits = get_loop_exit_edges (loop); |
f9bf4777 | 3409 | likely_exit = single_likely_exit (loop); |
9771b263 | 3410 | FOR_EACH_VEC_ELT (exits, i, ex) |
e9eb809d | 3411 | { |
cd0f6278 | 3412 | if (!number_of_iterations_exit (loop, ex, &niter_desc, false, false)) |
e9eb809d ZD |
3413 | continue; |
3414 | ||
3415 | niter = niter_desc.niter; | |
3416 | type = TREE_TYPE (niter); | |
946e1bc7 | 3417 | if (TREE_CODE (niter_desc.may_be_zero) != INTEGER_CST) |
e6845c23 | 3418 | niter = build3 (COND_EXPR, type, niter_desc.may_be_zero, |
ff5e9a94 | 3419 | build_int_cst (type, 0), |
e6845c23 | 3420 | niter); |
b3ce5b6e | 3421 | record_estimate (loop, niter, niter_desc.max, |
ca83d385 | 3422 | last_stmt (ex->src), |
f9bf4777 | 3423 | true, ex == likely_exit, true); |
e9eb809d | 3424 | } |
9771b263 | 3425 | exits.release (); |
b8698a0f | 3426 | |
6e616110 RB |
3427 | if (flag_aggressive_loop_optimizations) |
3428 | infer_loop_bounds_from_undefined (loop); | |
9bdb685e | 3429 | |
73ddf95b JH |
3430 | discover_iteration_bound_by_body_walk (loop); |
3431 | ||
05322355 JH |
3432 | maybe_lower_iteration_bound (loop); |
3433 | ||
9bdb685e ZD |
3434 | /* If we have a measured profile, use it to estimate the number of |
3435 | iterations. */ | |
3436 | if (loop->header->count != 0) | |
3437 | { | |
ac84e05e ZD |
3438 | gcov_type nit = expected_loop_iterations_unbounded (loop) + 1; |
3439 | bound = gcov_type_to_double_int (nit); | |
9bdb685e ZD |
3440 | record_niter_bound (loop, bound, true, false); |
3441 | } | |
fbd28bc3 JJ |
3442 | |
3443 | /* If we know the exact number of iterations of this loop, try to | |
3444 | not break code with undefined behavior by not recording smaller | |
3445 | maximum number of iterations. */ | |
3446 | if (loop->nb_iterations | |
3447 | && TREE_CODE (loop->nb_iterations) == INTEGER_CST) | |
3448 | { | |
3449 | loop->any_upper_bound = true; | |
3450 | loop->nb_iterations_upper_bound | |
3451 | = tree_to_double_int (loop->nb_iterations); | |
3452 | } | |
e9eb809d ZD |
3453 | } |
3454 | ||
b4a9343c ZD |
3455 | /* Sets NIT to the estimated number of executions of the latch of the |
3456 | LOOP. If CONSERVATIVE is true, we must be sure that NIT is at least as | |
3457 | large as the number of iterations. If we have no reliable estimate, | |
3458 | the function returns false, otherwise returns true. */ | |
3459 | ||
3460 | bool | |
652c4c71 | 3461 | estimated_loop_iterations (struct loop *loop, double_int *nit) |
b4a9343c | 3462 | { |
e3a8f1fa JH |
3463 | /* When SCEV information is available, try to update loop iterations |
3464 | estimate. Otherwise just return whatever we recorded earlier. */ | |
3465 | if (scev_initialized_p ()) | |
3466 | estimate_numbers_of_iterations_loop (loop); | |
3467 | ||
71343877 | 3468 | return (get_estimated_loop_iterations (loop, nit)); |
652c4c71 | 3469 | } |
b4a9343c | 3470 | |
1ef88893 AM |
3471 | /* Similar to estimated_loop_iterations, but returns the estimate only |
3472 | if it fits to HOST_WIDE_INT. If this is not the case, or the estimate | |
3473 | on the number of iterations of LOOP could not be derived, returns -1. */ | |
3474 | ||
3475 | HOST_WIDE_INT | |
3476 | estimated_loop_iterations_int (struct loop *loop) | |
3477 | { | |
3478 | double_int nit; | |
3479 | HOST_WIDE_INT hwi_nit; | |
3480 | ||
3481 | if (!estimated_loop_iterations (loop, &nit)) | |
3482 | return -1; | |
3483 | ||
3484 | if (!nit.fits_shwi ()) | |
3485 | return -1; | |
3486 | hwi_nit = nit.to_shwi (); | |
3487 | ||
3488 | return hwi_nit < 0 ? -1 : hwi_nit; | |
3489 | } | |
3490 | ||
3491 | ||
652c4c71 RG |
3492 | /* Sets NIT to an upper bound for the maximum number of executions of the |
3493 | latch of the LOOP. If we have no reliable estimate, the function returns | |
3494 | false, otherwise returns true. */ | |
3495 | ||
3496 | bool | |
3497 | max_loop_iterations (struct loop *loop, double_int *nit) | |
3498 | { | |
e3a8f1fa JH |
3499 | /* When SCEV information is available, try to update loop iterations |
3500 | estimate. Otherwise just return whatever we recorded earlier. */ | |
3501 | if (scev_initialized_p ()) | |
3502 | estimate_numbers_of_iterations_loop (loop); | |
b4a9343c | 3503 | |
71343877 | 3504 | return get_max_loop_iterations (loop, nit); |
652c4c71 RG |
3505 | } |
3506 | ||
3507 | /* Similar to max_loop_iterations, but returns the estimate only | |
3508 | if it fits to HOST_WIDE_INT. If this is not the case, or the estimate | |
3509 | on the number of iterations of LOOP could not be derived, returns -1. */ | |
3510 | ||
3511 | HOST_WIDE_INT | |
3512 | max_loop_iterations_int (struct loop *loop) | |
b4a9343c ZD |
3513 | { |
3514 | double_int nit; | |
3515 | HOST_WIDE_INT hwi_nit; | |
3516 | ||
652c4c71 | 3517 | if (!max_loop_iterations (loop, &nit)) |
b4a9343c ZD |
3518 | return -1; |
3519 | ||
27bcd47c | 3520 | if (!nit.fits_shwi ()) |
b4a9343c | 3521 | return -1; |
27bcd47c | 3522 | hwi_nit = nit.to_shwi (); |
b4a9343c ZD |
3523 | |
3524 | return hwi_nit < 0 ? -1 : hwi_nit; | |
3525 | } | |
3526 | ||
652c4c71 RG |
3527 | /* Returns an estimate for the number of executions of statements |
3528 | in the LOOP. For statements before the loop exit, this exceeds | |
3529 | the number of execution of the latch by one. */ | |
3530 | ||
3531 | HOST_WIDE_INT | |
3532 | estimated_stmt_executions_int (struct loop *loop) | |
3533 | { | |
3534 | HOST_WIDE_INT nit = estimated_loop_iterations_int (loop); | |
3535 | HOST_WIDE_INT snit; | |
3536 | ||
3537 | if (nit == -1) | |
3538 | return -1; | |
3539 | ||
3540 | snit = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) nit + 1); | |
3541 | ||
3542 | /* If the computation overflows, return -1. */ | |
3543 | return snit < 0 ? -1 : snit; | |
3544 | } | |
3545 | ||
3546 | /* Sets NIT to the estimated maximum number of executions of the latch of the | |
3547 | LOOP, plus one. If we have no reliable estimate, the function returns | |
3548 | false, otherwise returns true. */ | |
3549 | ||
3550 | bool | |
3551 | max_stmt_executions (struct loop *loop, double_int *nit) | |
3552 | { | |
3553 | double_int nit_minus_one; | |
3554 | ||
3555 | if (!max_loop_iterations (loop, nit)) | |
3556 | return false; | |
3557 | ||
3558 | nit_minus_one = *nit; | |
3559 | ||
27bcd47c | 3560 | *nit += double_int_one; |
652c4c71 | 3561 | |
27bcd47c | 3562 | return (*nit).ugt (nit_minus_one); |
652c4c71 RG |
3563 | } |
3564 | ||
b4a9343c | 3565 | /* Sets NIT to the estimated number of executions of the latch of the |
652c4c71 RG |
3566 | LOOP, plus one. If we have no reliable estimate, the function returns |
3567 | false, otherwise returns true. */ | |
b4a9343c ZD |
3568 | |
3569 | bool | |
652c4c71 | 3570 | estimated_stmt_executions (struct loop *loop, double_int *nit) |
b4a9343c ZD |
3571 | { |
3572 | double_int nit_minus_one; | |
3573 | ||
652c4c71 | 3574 | if (!estimated_loop_iterations (loop, nit)) |
b4a9343c ZD |
3575 | return false; |
3576 | ||
3577 | nit_minus_one = *nit; | |
3578 | ||
27bcd47c | 3579 | *nit += double_int_one; |
b4a9343c | 3580 | |
27bcd47c | 3581 | return (*nit).ugt (nit_minus_one); |
b4a9343c ZD |
3582 | } |
3583 | ||
d73be268 | 3584 | /* Records estimates on numbers of iterations of loops. */ |
e9eb809d ZD |
3585 | |
3586 | void | |
421e6082 | 3587 | estimate_numbers_of_iterations (void) |
e9eb809d | 3588 | { |
42fd6772 | 3589 | loop_iterator li; |
e9eb809d ZD |
3590 | struct loop *loop; |
3591 | ||
6ac01510 ILT |
3592 | /* We don't want to issue signed overflow warnings while getting |
3593 | loop iteration estimates. */ | |
3594 | fold_defer_overflow_warnings (); | |
3595 | ||
42fd6772 | 3596 | FOR_EACH_LOOP (li, loop, 0) |
e9eb809d | 3597 | { |
421e6082 | 3598 | estimate_numbers_of_iterations_loop (loop); |
e9eb809d | 3599 | } |
6ac01510 ILT |
3600 | |
3601 | fold_undefer_and_ignore_overflow_warnings (); | |
e9eb809d ZD |
3602 | } |
3603 | ||
e9eb809d ZD |
3604 | /* Returns true if statement S1 dominates statement S2. */ |
3605 | ||
bbc8a8dc | 3606 | bool |
726a989a | 3607 | stmt_dominates_stmt_p (gimple s1, gimple s2) |
e9eb809d | 3608 | { |
726a989a | 3609 | basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2); |
e9eb809d ZD |
3610 | |
3611 | if (!bb1 | |
3612 | || s1 == s2) | |
3613 | return true; | |
3614 | ||
3615 | if (bb1 == bb2) | |
3616 | { | |
726a989a | 3617 | gimple_stmt_iterator bsi; |
e9eb809d | 3618 | |
25c6036a RG |
3619 | if (gimple_code (s2) == GIMPLE_PHI) |
3620 | return false; | |
3621 | ||
3622 | if (gimple_code (s1) == GIMPLE_PHI) | |
3623 | return true; | |
3624 | ||
726a989a RB |
3625 | for (bsi = gsi_start_bb (bb1); gsi_stmt (bsi) != s2; gsi_next (&bsi)) |
3626 | if (gsi_stmt (bsi) == s1) | |
e9eb809d ZD |
3627 | return true; |
3628 | ||
3629 | return false; | |
3630 | } | |
3631 | ||
3632 | return dominated_by_p (CDI_DOMINATORS, bb2, bb1); | |
3633 | } | |
3634 | ||
763f4527 | 3635 | /* Returns true when we can prove that the number of executions of |
946e1bc7 ZD |
3636 | STMT in the loop is at most NITER, according to the bound on |
3637 | the number of executions of the statement NITER_BOUND->stmt recorded in | |
870ca331 JH |
3638 | NITER_BOUND and fact that NITER_BOUND->stmt dominate STMT. |
3639 | ||
3640 | ??? This code can become quite a CPU hog - we can have many bounds, | |
3641 | and large basic block forcing stmt_dominates_stmt_p to be queried | |
3642 | many times on a large basic blocks, so the whole thing is O(n^2) | |
3643 | for scev_probably_wraps_p invocation (that can be done n times). | |
3644 | ||
3645 | It would make more sense (and give better answers) to remember BB | |
3646 | bounds computed by discover_iteration_bound_by_body_walk. */ | |
e9eb809d | 3647 | |
1e8552eb | 3648 | static bool |
726a989a | 3649 | n_of_executions_at_most (gimple stmt, |
b8698a0f | 3650 | struct nb_iter_bound *niter_bound, |
7aa20a86 | 3651 | tree niter) |
e9eb809d | 3652 | { |
946e1bc7 | 3653 | double_int bound = niter_bound->bound; |
6e682d7e | 3654 | tree nit_type = TREE_TYPE (niter), e; |
2f133f46 | 3655 | enum tree_code cmp; |
1e8552eb | 3656 | |
946e1bc7 ZD |
3657 | gcc_assert (TYPE_UNSIGNED (nit_type)); |
3658 | ||
3659 | /* If the bound does not even fit into NIT_TYPE, it cannot tell us that | |
3660 | the number of iterations is small. */ | |
3661 | if (!double_int_fits_to_tree_p (nit_type, bound)) | |
3662 | return false; | |
3663 | ||
3664 | /* We know that NITER_BOUND->stmt is executed at most NITER_BOUND->bound + 1 | |
3665 | times. This means that: | |
b8698a0f | 3666 | |
870ca331 JH |
3667 | -- if NITER_BOUND->is_exit is true, then everything after |
3668 | it at most NITER_BOUND->bound times. | |
946e1bc7 ZD |
3669 | |
3670 | -- If NITER_BOUND->is_exit is false, then if we can prove that when STMT | |
3671 | is executed, then NITER_BOUND->stmt is executed as well in the same | |
870ca331 JH |
3672 | iteration then STMT is executed at most NITER_BOUND->bound + 1 times. |
3673 | ||
3674 | If we can determine that NITER_BOUND->stmt is always executed | |
3675 | after STMT, then STMT is executed at most NITER_BOUND->bound + 2 times. | |
3676 | We conclude that if both statements belong to the same | |
3677 | basic block and STMT is before NITER_BOUND->stmt and there are no | |
3678 | statements with side effects in between. */ | |
946e1bc7 ZD |
3679 | |
3680 | if (niter_bound->is_exit) | |
3681 | { | |
870ca331 JH |
3682 | if (stmt == niter_bound->stmt |
3683 | || !stmt_dominates_stmt_p (niter_bound->stmt, stmt)) | |
3684 | return false; | |
3685 | cmp = GE_EXPR; | |
946e1bc7 | 3686 | } |
1e8552eb | 3687 | else |
946e1bc7 | 3688 | { |
870ca331 | 3689 | if (!stmt_dominates_stmt_p (niter_bound->stmt, stmt)) |
946e1bc7 | 3690 | { |
870ca331 JH |
3691 | gimple_stmt_iterator bsi; |
3692 | if (gimple_bb (stmt) != gimple_bb (niter_bound->stmt) | |
3693 | || gimple_code (stmt) == GIMPLE_PHI | |
3694 | || gimple_code (niter_bound->stmt) == GIMPLE_PHI) | |
3695 | return false; | |
3696 | ||
3697 | /* By stmt_dominates_stmt_p we already know that STMT appears | |
3698 | before NITER_BOUND->STMT. Still need to test that the loop | |
3699 | can not be terinated by a side effect in between. */ | |
3700 | for (bsi = gsi_for_stmt (stmt); gsi_stmt (bsi) != niter_bound->stmt; | |
3701 | gsi_next (&bsi)) | |
3702 | if (gimple_has_side_effects (gsi_stmt (bsi))) | |
3703 | return false; | |
27bcd47c LC |
3704 | bound += double_int_one; |
3705 | if (bound.is_zero () | |
946e1bc7 ZD |
3706 | || !double_int_fits_to_tree_p (nit_type, bound)) |
3707 | return false; | |
3708 | } | |
3709 | cmp = GT_EXPR; | |
3710 | } | |
1e8552eb | 3711 | |
6e682d7e ZD |
3712 | e = fold_binary (cmp, boolean_type_node, |
3713 | niter, double_int_to_tree (nit_type, bound)); | |
3714 | return e && integer_nonzerop (e); | |
1e8552eb SP |
3715 | } |
3716 | ||
d7f5de76 | 3717 | /* Returns true if the arithmetics in TYPE can be assumed not to wrap. */ |
e9eb809d | 3718 | |
d7f5de76 ZD |
3719 | bool |
3720 | nowrap_type_p (tree type) | |
d7770457 | 3721 | { |
eeef0e45 ILT |
3722 | if (INTEGRAL_TYPE_P (type) |
3723 | && TYPE_OVERFLOW_UNDEFINED (type)) | |
d7f5de76 | 3724 | return true; |
d7770457 | 3725 | |
d7f5de76 ZD |
3726 | if (POINTER_TYPE_P (type)) |
3727 | return true; | |
d7770457 | 3728 | |
d7770457 SP |
3729 | return false; |
3730 | } | |
3731 | ||
1e8552eb SP |
3732 | /* Return false only when the induction variable BASE + STEP * I is |
3733 | known to not overflow: i.e. when the number of iterations is small | |
3734 | enough with respect to the step and initial condition in order to | |
3735 | keep the evolution confined in TYPEs bounds. Return true when the | |
3736 | iv is known to overflow or when the property is not computable. | |
b8698a0f | 3737 | |
d7f5de76 ZD |
3738 | USE_OVERFLOW_SEMANTICS is true if this function should assume that |
3739 | the rules for overflow of the given language apply (e.g., that signed | |
3740 | arithmetics in C does not overflow). */ | |
1e8552eb SP |
3741 | |
3742 | bool | |
b8698a0f | 3743 | scev_probably_wraps_p (tree base, tree step, |
726a989a | 3744 | gimple at_stmt, struct loop *loop, |
525dc87d | 3745 | bool use_overflow_semantics) |
1e8552eb | 3746 | { |
1e8552eb SP |
3747 | tree delta, step_abs; |
3748 | tree unsigned_type, valid_niter; | |
d7f5de76 | 3749 | tree type = TREE_TYPE (step); |
870ca331 JH |
3750 | tree e; |
3751 | double_int niter; | |
3752 | struct nb_iter_bound *bound; | |
d7f5de76 ZD |
3753 | |
3754 | /* FIXME: We really need something like | |
3755 | http://gcc.gnu.org/ml/gcc-patches/2005-06/msg02025.html. | |
3756 | ||
3757 | We used to test for the following situation that frequently appears | |
3758 | during address arithmetics: | |
b8698a0f | 3759 | |
d7770457 SP |
3760 | D.1621_13 = (long unsigned intD.4) D.1620_12; |
3761 | D.1622_14 = D.1621_13 * 8; | |
3762 | D.1623_15 = (doubleD.29 *) D.1622_14; | |
d7770457 | 3763 | |
d7f5de76 ZD |
3764 | And derived that the sequence corresponding to D_14 |
3765 | can be proved to not wrap because it is used for computing a | |
3766 | memory access; however, this is not really the case -- for example, | |
3767 | if D_12 = (unsigned char) [254,+,1], then D_14 has values | |
3768 | 2032, 2040, 0, 8, ..., but the code is still legal. */ | |
1e8552eb | 3769 | |
18aed06a | 3770 | if (chrec_contains_undetermined (base) |
24938ce9 | 3771 | || chrec_contains_undetermined (step)) |
d7f5de76 | 3772 | return true; |
d7770457 | 3773 | |
6e682d7e | 3774 | if (integer_zerop (step)) |
d7f5de76 | 3775 | return false; |
ab02cc4e | 3776 | |
d7f5de76 ZD |
3777 | /* If we can use the fact that signed and pointer arithmetics does not |
3778 | wrap, we are done. */ | |
dc5b3407 | 3779 | if (use_overflow_semantics && nowrap_type_p (TREE_TYPE (base))) |
d7f5de76 | 3780 | return false; |
ab02cc4e | 3781 | |
24938ce9 ZD |
3782 | /* To be able to use estimates on number of iterations of the loop, |
3783 | we must have an upper bound on the absolute value of the step. */ | |
3784 | if (TREE_CODE (step) != INTEGER_CST) | |
3785 | return true; | |
3786 | ||
6ac01510 ILT |
3787 | /* Don't issue signed overflow warnings. */ |
3788 | fold_defer_overflow_warnings (); | |
3789 | ||
d7f5de76 ZD |
3790 | /* Otherwise, compute the number of iterations before we reach the |
3791 | bound of the type, and verify that the loop is exited before this | |
3792 | occurs. */ | |
3793 | unsigned_type = unsigned_type_for (type); | |
3794 | base = fold_convert (unsigned_type, base); | |
1e8552eb | 3795 | |
d7f5de76 ZD |
3796 | if (tree_int_cst_sign_bit (step)) |
3797 | { | |
3798 | tree extreme = fold_convert (unsigned_type, | |
3799 | lower_bound_in_type (type, type)); | |
3800 | delta = fold_build2 (MINUS_EXPR, unsigned_type, base, extreme); | |
3801 | step_abs = fold_build1 (NEGATE_EXPR, unsigned_type, | |
3802 | fold_convert (unsigned_type, step)); | |
e9eb809d | 3803 | } |
d7f5de76 | 3804 | else |
3eca1bd7 | 3805 | { |
d7f5de76 ZD |
3806 | tree extreme = fold_convert (unsigned_type, |
3807 | upper_bound_in_type (type, type)); | |
3808 | delta = fold_build2 (MINUS_EXPR, unsigned_type, extreme, base); | |
3809 | step_abs = fold_convert (unsigned_type, step); | |
3eca1bd7 DN |
3810 | } |
3811 | ||
1e8552eb | 3812 | valid_niter = fold_build2 (FLOOR_DIV_EXPR, unsigned_type, delta, step_abs); |
e9eb809d | 3813 | |
421e6082 | 3814 | estimate_numbers_of_iterations_loop (loop); |
870ca331 JH |
3815 | |
3816 | if (max_loop_iterations (loop, &niter) | |
3817 | && double_int_fits_to_tree_p (TREE_TYPE (valid_niter), niter) | |
3818 | && (e = fold_binary (GT_EXPR, boolean_type_node, valid_niter, | |
3819 | double_int_to_tree (TREE_TYPE (valid_niter), | |
3820 | niter))) != NULL | |
3821 | && integer_nonzerop (e)) | |
6ac01510 | 3822 | { |
870ca331 JH |
3823 | fold_undefer_and_ignore_overflow_warnings (); |
3824 | return false; | |
6ac01510 | 3825 | } |
870ca331 JH |
3826 | if (at_stmt) |
3827 | for (bound = loop->bounds; bound; bound = bound->next) | |
3828 | { | |
3829 | if (n_of_executions_at_most (at_stmt, bound, valid_niter)) | |
3830 | { | |
3831 | fold_undefer_and_ignore_overflow_warnings (); | |
3832 | return false; | |
3833 | } | |
3834 | } | |
6ac01510 ILT |
3835 | |
3836 | fold_undefer_and_ignore_overflow_warnings (); | |
1e8552eb SP |
3837 | |
3838 | /* At this point we still don't have a proof that the iv does not | |
3839 | overflow: give up. */ | |
3840 | return true; | |
e9eb809d ZD |
3841 | } |
3842 | ||
e9eb809d ZD |
3843 | /* Frees the information on upper bounds on numbers of iterations of LOOP. */ |
3844 | ||
c9639aae | 3845 | void |
e9eb809d ZD |
3846 | free_numbers_of_iterations_estimates_loop (struct loop *loop) |
3847 | { | |
3848 | struct nb_iter_bound *bound, *next; | |
c9639aae ZD |
3849 | |
3850 | loop->nb_iterations = NULL; | |
946e1bc7 | 3851 | loop->estimate_state = EST_NOT_COMPUTED; |
e9eb809d ZD |
3852 | for (bound = loop->bounds; bound; bound = next) |
3853 | { | |
3854 | next = bound->next; | |
9e2f83a5 | 3855 | ggc_free (bound); |
e9eb809d ZD |
3856 | } |
3857 | ||
3858 | loop->bounds = NULL; | |
3859 | } | |
3860 | ||
d73be268 | 3861 | /* Frees the information on upper bounds on numbers of iterations of loops. */ |
e9eb809d ZD |
3862 | |
3863 | void | |
d73be268 | 3864 | free_numbers_of_iterations_estimates (void) |
e9eb809d | 3865 | { |
42fd6772 | 3866 | loop_iterator li; |
e9eb809d ZD |
3867 | struct loop *loop; |
3868 | ||
42fd6772 | 3869 | FOR_EACH_LOOP (li, loop, 0) |
e9eb809d | 3870 | { |
42fd6772 | 3871 | free_numbers_of_iterations_estimates_loop (loop); |
e9eb809d ZD |
3872 | } |
3873 | } | |
d5ab5675 ZD |
3874 | |
3875 | /* Substitute value VAL for ssa name NAME inside expressions held | |
3876 | at LOOP. */ | |
3877 | ||
3878 | void | |
3879 | substitute_in_loop_info (struct loop *loop, tree name, tree val) | |
3880 | { | |
d5ab5675 | 3881 | loop->nb_iterations = simplify_replace_tree (loop->nb_iterations, name, val); |
d5ab5675 | 3882 | } |