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