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