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