Branch data Line data Source code
1 : : /* Scalar evolution detector.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : : Contributed by Sebastian Pop <s.pop@laposte.net>
4 : :
5 : : This file is part of GCC.
6 : :
7 : : GCC is free software; you can redistribute it and/or modify it under
8 : : the terms of the GNU General Public License as published by the Free
9 : : Software Foundation; either version 3, or (at your option) any later
10 : : version.
11 : :
12 : : GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 : : WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : : for more details.
16 : :
17 : : You should have received a copy of the GNU General Public License
18 : : along with GCC; see the file COPYING3. If not see
19 : : <http://www.gnu.org/licenses/>. */
20 : :
21 : : /*
22 : : Description:
23 : :
24 : : This pass analyzes the evolution of scalar variables in loop
25 : : structures. The algorithm is based on the SSA representation,
26 : : and on the loop hierarchy tree. This algorithm is not based on
27 : : the notion of versions of a variable, as it was the case for the
28 : : previous implementations of the scalar evolution algorithm, but
29 : : it assumes that each defined name is unique.
30 : :
31 : : The notation used in this file is called "chains of recurrences",
32 : : and has been proposed by Eugene Zima, Robert Van Engelen, and
33 : : others for describing induction variables in programs. For example
34 : : "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0
35 : : when entering in the loop_1 and has a step 2 in this loop, in other
36 : : words "for (b = 0; b < N; b+=2);". Note that the coefficients of
37 : : this chain of recurrence (or chrec [shrek]) can contain the name of
38 : : other variables, in which case they are called parametric chrecs.
39 : : For example, "b -> {a, +, 2}_1" means that the initial value of "b"
40 : : is the value of "a". In most of the cases these parametric chrecs
41 : : are fully instantiated before their use because symbolic names can
42 : : hide some difficult cases such as self-references described later
43 : : (see the Fibonacci example).
44 : :
45 : : A short sketch of the algorithm is:
46 : :
47 : : Given a scalar variable to be analyzed, follow the SSA edge to
48 : : its definition:
49 : :
50 : : - When the definition is a GIMPLE_ASSIGN: if the right hand side
51 : : (RHS) of the definition cannot be statically analyzed, the answer
52 : : of the analyzer is: "don't know".
53 : : Otherwise, for all the variables that are not yet analyzed in the
54 : : RHS, try to determine their evolution, and finally try to
55 : : evaluate the operation of the RHS that gives the evolution
56 : : function of the analyzed variable.
57 : :
58 : : - When the definition is a condition-phi-node: determine the
59 : : evolution function for all the branches of the phi node, and
60 : : finally merge these evolutions (see chrec_merge).
61 : :
62 : : - When the definition is a loop-phi-node: determine its initial
63 : : condition, that is the SSA edge defined in an outer loop, and
64 : : keep it symbolic. Then determine the SSA edges that are defined
65 : : in the body of the loop. Follow the inner edges until ending on
66 : : another loop-phi-node of the same analyzed loop. If the reached
67 : : loop-phi-node is not the starting loop-phi-node, then we keep
68 : : this definition under a symbolic form. If the reached
69 : : loop-phi-node is the same as the starting one, then we compute a
70 : : symbolic stride on the return path. The result is then the
71 : : symbolic chrec {initial_condition, +, symbolic_stride}_loop.
72 : :
73 : : Examples:
74 : :
75 : : Example 1: Illustration of the basic algorithm.
76 : :
77 : : | a = 3
78 : : | loop_1
79 : : | b = phi (a, c)
80 : : | c = b + 1
81 : : | if (c > 10) exit_loop
82 : : | endloop
83 : :
84 : : Suppose that we want to know the number of iterations of the
85 : : loop_1. The exit_loop is controlled by a COND_EXPR (c > 10). We
86 : : ask the scalar evolution analyzer two questions: what's the
87 : : scalar evolution (scev) of "c", and what's the scev of "10". For
88 : : "10" the answer is "10" since it is a scalar constant. For the
89 : : scalar variable "c", it follows the SSA edge to its definition,
90 : : "c = b + 1", and then asks again what's the scev of "b".
91 : : Following the SSA edge, we end on a loop-phi-node "b = phi (a,
92 : : c)", where the initial condition is "a", and the inner loop edge
93 : : is "c". The initial condition is kept under a symbolic form (it
94 : : may be the case that the copy constant propagation has done its
95 : : work and we end with the constant "3" as one of the edges of the
96 : : loop-phi-node). The update edge is followed to the end of the
97 : : loop, and until reaching again the starting loop-phi-node: b -> c
98 : : -> b. At this point we have drawn a path from "b" to "b" from
99 : : which we compute the stride in the loop: in this example it is
100 : : "+1". The resulting scev for "b" is "b -> {a, +, 1}_1". Now
101 : : that the scev for "b" is known, it is possible to compute the
102 : : scev for "c", that is "c -> {a + 1, +, 1}_1". In order to
103 : : determine the number of iterations in the loop_1, we have to
104 : : instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some
105 : : more analysis the scev {4, +, 1}_1, or in other words, this is
106 : : the function "f (x) = x + 4", where x is the iteration count of
107 : : the loop_1. Now we have to solve the inequality "x + 4 > 10",
108 : : and take the smallest iteration number for which the loop is
109 : : exited: x = 7. This loop runs from x = 0 to x = 7, and in total
110 : : there are 8 iterations. In terms of loop normalization, we have
111 : : created a variable that is implicitly defined, "x" or just "_1",
112 : : and all the other analyzed scalars of the loop are defined in
113 : : function of this variable:
114 : :
115 : : a -> 3
116 : : b -> {3, +, 1}_1
117 : : c -> {4, +, 1}_1
118 : :
119 : : or in terms of a C program:
120 : :
121 : : | a = 3
122 : : | for (x = 0; x <= 7; x++)
123 : : | {
124 : : | b = x + 3
125 : : | c = x + 4
126 : : | }
127 : :
128 : : Example 2a: Illustration of the algorithm on nested loops.
129 : :
130 : : | loop_1
131 : : | a = phi (1, b)
132 : : | c = a + 2
133 : : | loop_2 10 times
134 : : | b = phi (c, d)
135 : : | d = b + 3
136 : : | endloop
137 : : | endloop
138 : :
139 : : For analyzing the scalar evolution of "a", the algorithm follows
140 : : the SSA edge into the loop's body: "a -> b". "b" is an inner
141 : : loop-phi-node, and its analysis as in Example 1, gives:
142 : :
143 : : b -> {c, +, 3}_2
144 : : d -> {c + 3, +, 3}_2
145 : :
146 : : Following the SSA edge for the initial condition, we end on "c = a
147 : : + 2", and then on the starting loop-phi-node "a". From this point,
148 : : the loop stride is computed: back on "c = a + 2" we get a "+2" in
149 : : the loop_1, then on the loop-phi-node "b" we compute the overall
150 : : effect of the inner loop that is "b = c + 30", and we get a "+30"
151 : : in the loop_1. That means that the overall stride in loop_1 is
152 : : equal to "+32", and the result is:
153 : :
154 : : a -> {1, +, 32}_1
155 : : c -> {3, +, 32}_1
156 : :
157 : : Example 2b: Multivariate chains of recurrences.
158 : :
159 : : | loop_1
160 : : | k = phi (0, k + 1)
161 : : | loop_2 4 times
162 : : | j = phi (0, j + 1)
163 : : | loop_3 4 times
164 : : | i = phi (0, i + 1)
165 : : | A[j + k] = ...
166 : : | endloop
167 : : | endloop
168 : : | endloop
169 : :
170 : : Analyzing the access function of array A with
171 : : instantiate_parameters (loop_1, "j + k"), we obtain the
172 : : instantiation and the analysis of the scalar variables "j" and "k"
173 : : in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end
174 : : value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is
175 : : {0, +, 1}_1. To obtain the evolution function in loop_3 and
176 : : instantiate the scalar variables up to loop_1, one has to use:
177 : : instantiate_scev (block_before_loop (loop_1), loop_3, "j + k").
178 : : The result of this call is {{0, +, 1}_1, +, 1}_2.
179 : :
180 : : Example 3: Higher degree polynomials.
181 : :
182 : : | loop_1
183 : : | a = phi (2, b)
184 : : | c = phi (5, d)
185 : : | b = a + 1
186 : : | d = c + a
187 : : | endloop
188 : :
189 : : a -> {2, +, 1}_1
190 : : b -> {3, +, 1}_1
191 : : c -> {5, +, a}_1
192 : : d -> {5 + a, +, a}_1
193 : :
194 : : instantiate_parameters (loop_1, {5, +, a}_1) -> {5, +, 2, +, 1}_1
195 : : instantiate_parameters (loop_1, {5 + a, +, a}_1) -> {7, +, 3, +, 1}_1
196 : :
197 : : Example 4: Lucas, Fibonacci, or mixers in general.
198 : :
199 : : | loop_1
200 : : | a = phi (1, b)
201 : : | c = phi (3, d)
202 : : | b = c
203 : : | d = c + a
204 : : | endloop
205 : :
206 : : a -> (1, c)_1
207 : : c -> {3, +, a}_1
208 : :
209 : : The syntax "(1, c)_1" stands for a PEELED_CHREC that has the
210 : : following semantics: during the first iteration of the loop_1, the
211 : : variable contains the value 1, and then it contains the value "c".
212 : : Note that this syntax is close to the syntax of the loop-phi-node:
213 : : "a -> (1, c)_1" vs. "a = phi (1, c)".
214 : :
215 : : The symbolic chrec representation contains all the semantics of the
216 : : original code. What is more difficult is to use this information.
217 : :
218 : : Example 5: Flip-flops, or exchangers.
219 : :
220 : : | loop_1
221 : : | a = phi (1, b)
222 : : | c = phi (3, d)
223 : : | b = c
224 : : | d = a
225 : : | endloop
226 : :
227 : : a -> (1, c)_1
228 : : c -> (3, a)_1
229 : :
230 : : Based on these symbolic chrecs, it is possible to refine this
231 : : information into the more precise PERIODIC_CHRECs:
232 : :
233 : : a -> |1, 3|_1
234 : : c -> |3, 1|_1
235 : :
236 : : This transformation is not yet implemented.
237 : :
238 : : Further readings:
239 : :
240 : : You can find a more detailed description of the algorithm in:
241 : : http://icps.u-strasbg.fr/~pop/DEA_03_Pop.pdf
242 : : http://icps.u-strasbg.fr/~pop/DEA_03_Pop.ps.gz. But note that
243 : : this is a preliminary report and some of the details of the
244 : : algorithm have changed. I'm working on a research report that
245 : : updates the description of the algorithms to reflect the design
246 : : choices used in this implementation.
247 : :
248 : : A set of slides show a high level overview of the algorithm and run
249 : : an example through the scalar evolution analyzer:
250 : : http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf
251 : :
252 : : The slides that I have presented at the GCC Summit'04 are available
253 : : at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf
254 : : */
255 : :
256 : : #include "config.h"
257 : : #include "system.h"
258 : : #include "coretypes.h"
259 : : #include "backend.h"
260 : : #include "target.h"
261 : : #include "rtl.h"
262 : : #include "optabs-query.h"
263 : : #include "tree.h"
264 : : #include "gimple.h"
265 : : #include "ssa.h"
266 : : #include "gimple-pretty-print.h"
267 : : #include "fold-const.h"
268 : : #include "gimplify.h"
269 : : #include "gimple-iterator.h"
270 : : #include "gimplify-me.h"
271 : : #include "tree-cfg.h"
272 : : #include "tree-ssa-loop-ivopts.h"
273 : : #include "tree-ssa-loop-manip.h"
274 : : #include "tree-ssa-loop-niter.h"
275 : : #include "tree-ssa-loop.h"
276 : : #include "tree-ssa.h"
277 : : #include "cfgloop.h"
278 : : #include "tree-chrec.h"
279 : : #include "tree-affine.h"
280 : : #include "tree-scalar-evolution.h"
281 : : #include "dumpfile.h"
282 : : #include "tree-ssa-propagate.h"
283 : : #include "gimple-fold.h"
284 : : #include "tree-into-ssa.h"
285 : : #include "builtins.h"
286 : : #include "case-cfn-macros.h"
287 : : #include "tree-eh.h"
288 : :
289 : : static tree analyze_scalar_evolution_1 (class loop *, tree);
290 : : static tree analyze_scalar_evolution_for_address_of (class loop *loop,
291 : : tree var);
292 : :
293 : : /* The cached information about an SSA name with version NAME_VERSION,
294 : : claiming that below basic block with index INSTANTIATED_BELOW, the
295 : : value of the SSA name can be expressed as CHREC. */
296 : :
297 : : struct GTY((for_user)) scev_info_str {
298 : : unsigned int name_version;
299 : : int instantiated_below;
300 : : tree chrec;
301 : : };
302 : :
303 : : /* Counters for the scev database. */
304 : : static unsigned nb_set_scev = 0;
305 : : static unsigned nb_get_scev = 0;
306 : :
307 : : struct scev_info_hasher : ggc_ptr_hash<scev_info_str>
308 : : {
309 : : static hashval_t hash (scev_info_str *i);
310 : : static bool equal (const scev_info_str *a, const scev_info_str *b);
311 : : };
312 : :
313 : : static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
314 : :
315 : :
316 : : /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
317 : :
318 : : static inline struct scev_info_str *
319 : 52314955 : new_scev_info_str (basic_block instantiated_below, tree var)
320 : : {
321 : 52314955 : struct scev_info_str *res;
322 : :
323 : 52314955 : res = ggc_alloc<scev_info_str> ();
324 : 52314955 : res->name_version = SSA_NAME_VERSION (var);
325 : 52314955 : res->chrec = chrec_not_analyzed_yet;
326 : 52314955 : res->instantiated_below = instantiated_below->index;
327 : :
328 : 52314955 : return res;
329 : : }
330 : :
331 : : /* Computes a hash function for database element ELT. */
332 : :
333 : : hashval_t
334 : 1066720907 : scev_info_hasher::hash (scev_info_str *elt)
335 : : {
336 : 1066720907 : return elt->name_version ^ elt->instantiated_below;
337 : : }
338 : :
339 : : /* Compares database elements E1 and E2. */
340 : :
341 : : bool
342 : 1057072158 : scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
343 : : {
344 : 1057072158 : return (elt1->name_version == elt2->name_version
345 : 1057072158 : && elt1->instantiated_below == elt2->instantiated_below);
346 : : }
347 : :
348 : : /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
349 : : A first query on VAR returns chrec_not_analyzed_yet. */
350 : :
351 : : static tree *
352 : 206576216 : find_var_scev_info (basic_block instantiated_below, tree var)
353 : : {
354 : 206576216 : struct scev_info_str *res;
355 : 206576216 : struct scev_info_str tmp;
356 : :
357 : 206576216 : tmp.name_version = SSA_NAME_VERSION (var);
358 : 206576216 : tmp.instantiated_below = instantiated_below->index;
359 : 206576216 : scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT);
360 : :
361 : 206576216 : if (!*slot)
362 : 52314955 : *slot = new_scev_info_str (instantiated_below, var);
363 : 206576216 : res = *slot;
364 : :
365 : 206576216 : return &res->chrec;
366 : : }
367 : :
368 : :
369 : : /* Hashtable helpers for a temporary hash-table used when
370 : : analyzing a scalar evolution, instantiating a CHREC or
371 : : resolving mixers. */
372 : :
373 : : class instantiate_cache_type
374 : : {
375 : : public:
376 : : htab_t map;
377 : : vec<scev_info_str> entries;
378 : :
379 : 129526469 : instantiate_cache_type () : map (NULL), entries (vNULL) {}
380 : : ~instantiate_cache_type ();
381 : 127612938 : tree get (unsigned slot) { return entries[slot].chrec; }
382 : 99418900 : void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
383 : : };
384 : :
385 : 129526469 : instantiate_cache_type::~instantiate_cache_type ()
386 : : {
387 : 129526469 : if (map != NULL)
388 : : {
389 : 29472291 : htab_delete (map);
390 : 29472291 : entries.release ();
391 : : }
392 : 129526469 : }
393 : :
394 : : /* Cache to avoid infinite recursion when instantiating an SSA name.
395 : : Live during the outermost analyze_scalar_evolution, instantiate_scev
396 : : or resolve_mixers call. */
397 : : static instantiate_cache_type *global_cache;
398 : :
399 : :
400 : : /* Return true when PHI is a loop-phi-node. */
401 : :
402 : : static bool
403 : 26550238 : loop_phi_node_p (gimple *phi)
404 : : {
405 : : /* The implementation of this function is based on the following
406 : : property: "all the loop-phi-nodes of a loop are contained in the
407 : : loop's header basic block". */
408 : :
409 : 0 : return loop_containing_stmt (phi)->header == gimple_bb (phi);
410 : : }
411 : :
412 : : /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
413 : : In general, in the case of multivariate evolutions we want to get
414 : : the evolution in different loops. LOOP specifies the level for
415 : : which to get the evolution.
416 : :
417 : : Example:
418 : :
419 : : | for (j = 0; j < 100; j++)
420 : : | {
421 : : | for (k = 0; k < 100; k++)
422 : : | {
423 : : | i = k + j; - Here the value of i is a function of j, k.
424 : : | }
425 : : | ... = i - Here the value of i is a function of j.
426 : : | }
427 : : | ... = i - Here the value of i is a scalar.
428 : :
429 : : Example:
430 : :
431 : : | i_0 = ...
432 : : | loop_1 10 times
433 : : | i_1 = phi (i_0, i_2)
434 : : | i_2 = i_1 + 2
435 : : | endloop
436 : :
437 : : This loop has the same effect as:
438 : : LOOP_1 has the same effect as:
439 : :
440 : : | i_1 = i_0 + 20
441 : :
442 : : The overall effect of the loop, "i_0 + 20" in the previous example,
443 : : is obtained by passing in the parameters: LOOP = 1,
444 : : EVOLUTION_FN = {i_0, +, 2}_1.
445 : : */
446 : :
447 : : tree
448 : 6686940 : compute_overall_effect_of_inner_loop (class loop *loop, tree evolution_fn)
449 : : {
450 : 7597044 : bool val = false;
451 : :
452 : 7597044 : if (evolution_fn == chrec_dont_know)
453 : : return chrec_dont_know;
454 : :
455 : 7479760 : else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
456 : : {
457 : 2525289 : class loop *inner_loop = get_chrec_loop (evolution_fn);
458 : :
459 : 2525289 : if (inner_loop == loop
460 : 2525289 : || flow_loop_nested_p (loop, inner_loop))
461 : : {
462 : 2525289 : tree nb_iter = number_of_latch_executions (inner_loop);
463 : :
464 : 2525289 : if (nb_iter == chrec_dont_know)
465 : : return chrec_dont_know;
466 : : else
467 : : {
468 : 910104 : tree res;
469 : :
470 : : /* evolution_fn is the evolution function in LOOP. Get
471 : : its value in the nb_iter-th iteration. */
472 : 910104 : res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
473 : :
474 : 910104 : if (chrec_contains_symbols_defined_in_loop (res, loop->num))
475 : 64881 : res = instantiate_parameters (loop, res);
476 : :
477 : : /* Continue the computation until ending on a parent of LOOP. */
478 : 910104 : return compute_overall_effect_of_inner_loop (loop, res);
479 : : }
480 : : }
481 : : else
482 : : return evolution_fn;
483 : : }
484 : :
485 : : /* If the evolution function is an invariant, there is nothing to do. */
486 : 4954471 : else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
487 : : return evolution_fn;
488 : :
489 : : else
490 : 4145672 : return chrec_dont_know;
491 : : }
492 : :
493 : : /* Associate CHREC to SCALAR. */
494 : :
495 : : static void
496 : 50096494 : set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec)
497 : : {
498 : 50096494 : tree *scalar_info;
499 : :
500 : 50096494 : if (TREE_CODE (scalar) != SSA_NAME)
501 : : return;
502 : :
503 : 50096494 : scalar_info = find_var_scev_info (instantiated_below, scalar);
504 : :
505 : 50096494 : if (dump_file)
506 : : {
507 : 81446 : if (dump_flags & TDF_SCEV)
508 : : {
509 : 11 : fprintf (dump_file, "(set_scalar_evolution \n");
510 : 11 : fprintf (dump_file, " instantiated_below = %d \n",
511 : : instantiated_below->index);
512 : 11 : fprintf (dump_file, " (scalar = ");
513 : 11 : print_generic_expr (dump_file, scalar);
514 : 11 : fprintf (dump_file, ")\n (scalar_evolution = ");
515 : 11 : print_generic_expr (dump_file, chrec);
516 : 11 : fprintf (dump_file, "))\n");
517 : : }
518 : 81446 : if (dump_flags & TDF_STATS)
519 : 7247 : nb_set_scev++;
520 : : }
521 : :
522 : 50096494 : *scalar_info = chrec;
523 : : }
524 : :
525 : : /* Retrieve the chrec associated to SCALAR instantiated below
526 : : INSTANTIATED_BELOW block. */
527 : :
528 : : static tree
529 : 193919746 : get_scalar_evolution (basic_block instantiated_below, tree scalar)
530 : : {
531 : 193919746 : tree res;
532 : :
533 : 193919746 : if (dump_file)
534 : : {
535 : 665572 : if (dump_flags & TDF_SCEV)
536 : : {
537 : 36 : fprintf (dump_file, "(get_scalar_evolution \n");
538 : 36 : fprintf (dump_file, " (scalar = ");
539 : 36 : print_generic_expr (dump_file, scalar);
540 : 36 : fprintf (dump_file, ")\n");
541 : : }
542 : 665572 : if (dump_flags & TDF_STATS)
543 : 51132 : nb_get_scev++;
544 : : }
545 : :
546 : 193919746 : if (VECTOR_TYPE_P (TREE_TYPE (scalar))
547 : 193919746 : || TREE_CODE (TREE_TYPE (scalar)) == COMPLEX_TYPE)
548 : : /* For chrec_dont_know we keep the symbolic form. */
549 : : res = scalar;
550 : : else
551 : 193625783 : switch (TREE_CODE (scalar))
552 : : {
553 : 158543863 : case SSA_NAME:
554 : 158543863 : if (SSA_NAME_IS_DEFAULT_DEF (scalar))
555 : : res = scalar;
556 : : else
557 : 156479722 : res = *find_var_scev_info (instantiated_below, scalar);
558 : : break;
559 : :
560 : : case REAL_CST:
561 : : case FIXED_CST:
562 : : case INTEGER_CST:
563 : : res = scalar;
564 : : break;
565 : :
566 : : default:
567 : 193919746 : res = chrec_not_analyzed_yet;
568 : : break;
569 : : }
570 : :
571 : 193919746 : if (dump_file && (dump_flags & TDF_SCEV))
572 : : {
573 : 36 : fprintf (dump_file, " (scalar_evolution = ");
574 : 36 : print_generic_expr (dump_file, res);
575 : 36 : fprintf (dump_file, "))\n");
576 : : }
577 : :
578 : 193919746 : return res;
579 : : }
580 : :
581 : :
582 : : /* Depth first search algorithm. */
583 : :
584 : : enum t_bool {
585 : : t_false,
586 : : t_true,
587 : : t_dont_know
588 : : };
589 : :
590 : : class scev_dfs
591 : : {
592 : : public:
593 : 10844090 : scev_dfs (class loop *loop_, gphi *phi_, tree init_cond_)
594 : 10844090 : : loop (loop_), loop_phi_node (phi_), init_cond (init_cond_) {}
595 : : t_bool get_ev (tree *, tree);
596 : :
597 : : private:
598 : : t_bool follow_ssa_edge_expr (gimple *, tree, tree *, int);
599 : : t_bool follow_ssa_edge_binary (gimple *at_stmt,
600 : : tree type, tree rhs0, enum tree_code code,
601 : : tree rhs1, tree *evolution_of_loop, int limit);
602 : : t_bool follow_ssa_edge_in_condition_phi_branch (int i,
603 : : gphi *condition_phi,
604 : : tree *evolution_of_branch,
605 : : tree init_cond, int limit);
606 : : t_bool follow_ssa_edge_in_condition_phi (gphi *condition_phi,
607 : : tree *evolution_of_loop, int limit);
608 : : t_bool follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
609 : : tree *evolution_of_loop, int limit);
610 : : tree add_to_evolution (tree chrec_before, enum tree_code code,
611 : : tree to_add, gimple *at_stmt);
612 : : tree add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt);
613 : :
614 : : class loop *loop;
615 : : gphi *loop_phi_node;
616 : : tree init_cond;
617 : : };
618 : :
619 : : t_bool
620 : 10844090 : scev_dfs::get_ev (tree *ev_fn, tree arg)
621 : : {
622 : 10844090 : *ev_fn = chrec_dont_know;
623 : 10844090 : return follow_ssa_edge_expr (loop_phi_node, arg, ev_fn, 0);
624 : : }
625 : :
626 : : /* Helper function for add_to_evolution. Returns the evolution
627 : : function for an assignment of the form "a = b + c", where "a" and
628 : : "b" are on the strongly connected component. CHREC_BEFORE is the
629 : : information that we already have collected up to this point.
630 : : TO_ADD is the evolution of "c".
631 : :
632 : : When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
633 : : evolution the expression TO_ADD, otherwise construct an evolution
634 : : part for this loop. */
635 : :
636 : : tree
637 : 8877623 : scev_dfs::add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt)
638 : : {
639 : 8877623 : tree type, left, right;
640 : 8877623 : unsigned loop_nb = loop->num;
641 : 8877623 : class loop *chloop;
642 : :
643 : 8877623 : switch (TREE_CODE (chrec_before))
644 : : {
645 : 133641 : case POLYNOMIAL_CHREC:
646 : 133641 : chloop = get_chrec_loop (chrec_before);
647 : 133641 : if (chloop == loop
648 : 133641 : || flow_loop_nested_p (chloop, loop))
649 : : {
650 : 133641 : unsigned var;
651 : :
652 : 133641 : type = chrec_type (chrec_before);
653 : :
654 : : /* When there is no evolution part in this loop, build it. */
655 : 133641 : if (chloop != loop)
656 : : {
657 : 0 : var = loop_nb;
658 : 0 : left = chrec_before;
659 : 0 : right = SCALAR_FLOAT_TYPE_P (type)
660 : 0 : ? build_real (type, dconst0)
661 : 0 : : build_int_cst (type, 0);
662 : : }
663 : : else
664 : : {
665 : 133641 : var = CHREC_VARIABLE (chrec_before);
666 : 133641 : left = CHREC_LEFT (chrec_before);
667 : 133641 : right = CHREC_RIGHT (chrec_before);
668 : : }
669 : :
670 : 133641 : to_add = chrec_convert (type, to_add, at_stmt);
671 : 133641 : right = chrec_convert_rhs (type, right, at_stmt);
672 : 133641 : right = chrec_fold_plus (chrec_type (right), right, to_add);
673 : : /* When we have an evolution in a non-wrapping type and
674 : : in the process of accumulating CHREC_RIGHT there was
675 : : overflow this indicates in the association that happened
676 : : in building the CHREC clearly involved UB. Avoid this.
677 : : In building a CHREC we basically turn (a + INCR1) + INCR2
678 : : into a + (INCR1 + INCR2) which is not always valid.
679 : : Note this check only catches few invalid cases. */
680 : 65442 : if ((INTEGRAL_TYPE_P (type) && ! TYPE_OVERFLOW_WRAPS (type))
681 : 24824 : && TREE_CODE (right) == INTEGER_CST
682 : 142181 : && TREE_OVERFLOW (right))
683 : 5 : return chrec_dont_know;
684 : 133636 : return build_polynomial_chrec (var, left, right);
685 : : }
686 : : else
687 : : {
688 : 0 : gcc_assert (flow_loop_nested_p (loop, chloop));
689 : :
690 : : /* Search the evolution in LOOP_NB. */
691 : 0 : left = add_to_evolution_1 (CHREC_LEFT (chrec_before),
692 : : to_add, at_stmt);
693 : 0 : right = CHREC_RIGHT (chrec_before);
694 : 0 : right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
695 : 0 : return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
696 : 0 : left, right);
697 : : }
698 : :
699 : 8743982 : default:
700 : : /* These nodes do not depend on a loop. */
701 : 8743982 : if (chrec_before == chrec_dont_know)
702 : : return chrec_dont_know;
703 : :
704 : 8712126 : left = chrec_before;
705 : 8712126 : right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
706 : : /* When we add the first evolution we need to replace the symbolic
707 : : evolution we've put in when the DFS reached the loop PHI node
708 : : with the initial value. There's only a limited cases of
709 : : extra operations ontop of that symbol allowed, namely
710 : : sign-conversions we can look through. For other cases we leave
711 : : the symbolic initial condition which causes build_polynomial_chrec
712 : : to return chrec_dont_know. See PR42512, PR66375 and PR107176 for
713 : : cases we mishandled before. */
714 : 8712126 : STRIP_NOPS (chrec_before);
715 : 8712126 : if (chrec_before == gimple_phi_result (loop_phi_node))
716 : 8711253 : left = fold_convert (TREE_TYPE (left), init_cond);
717 : 8712126 : return build_polynomial_chrec (loop_nb, left, right);
718 : : }
719 : : }
720 : :
721 : : /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
722 : : of LOOP_NB.
723 : :
724 : : Description (provided for completeness, for those who read code in
725 : : a plane, and for my poor 62 bytes brain that would have forgotten
726 : : all this in the next two or three months):
727 : :
728 : : The algorithm of translation of programs from the SSA representation
729 : : into the chrecs syntax is based on a pattern matching. After having
730 : : reconstructed the overall tree expression for a loop, there are only
731 : : two cases that can arise:
732 : :
733 : : 1. a = loop-phi (init, a + expr)
734 : : 2. a = loop-phi (init, expr)
735 : :
736 : : where EXPR is either a scalar constant with respect to the analyzed
737 : : loop (this is a degree 0 polynomial), or an expression containing
738 : : other loop-phi definitions (these are higher degree polynomials).
739 : :
740 : : Examples:
741 : :
742 : : 1.
743 : : | init = ...
744 : : | loop_1
745 : : | a = phi (init, a + 5)
746 : : | endloop
747 : :
748 : : 2.
749 : : | inita = ...
750 : : | initb = ...
751 : : | loop_1
752 : : | a = phi (inita, 2 * b + 3)
753 : : | b = phi (initb, b + 1)
754 : : | endloop
755 : :
756 : : For the first case, the semantics of the SSA representation is:
757 : :
758 : : | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
759 : :
760 : : that is, there is a loop index "x" that determines the scalar value
761 : : of the variable during the loop execution. During the first
762 : : iteration, the value is that of the initial condition INIT, while
763 : : during the subsequent iterations, it is the sum of the initial
764 : : condition with the sum of all the values of EXPR from the initial
765 : : iteration to the before last considered iteration.
766 : :
767 : : For the second case, the semantics of the SSA program is:
768 : :
769 : : | a (x) = init, if x = 0;
770 : : | expr (x - 1), otherwise.
771 : :
772 : : The second case corresponds to the PEELED_CHREC, whose syntax is
773 : : close to the syntax of a loop-phi-node:
774 : :
775 : : | phi (init, expr) vs. (init, expr)_x
776 : :
777 : : The proof of the translation algorithm for the first case is a
778 : : proof by structural induction based on the degree of EXPR.
779 : :
780 : : Degree 0:
781 : : When EXPR is a constant with respect to the analyzed loop, or in
782 : : other words when EXPR is a polynomial of degree 0, the evolution of
783 : : the variable A in the loop is an affine function with an initial
784 : : condition INIT, and a step EXPR. In order to show this, we start
785 : : from the semantics of the SSA representation:
786 : :
787 : : f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
788 : :
789 : : and since "expr (j)" is a constant with respect to "j",
790 : :
791 : : f (x) = init + x * expr
792 : :
793 : : Finally, based on the semantics of the pure sum chrecs, by
794 : : identification we get the corresponding chrecs syntax:
795 : :
796 : : f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
797 : : f (x) -> {init, +, expr}_x
798 : :
799 : : Higher degree:
800 : : Suppose that EXPR is a polynomial of degree N with respect to the
801 : : analyzed loop_x for which we have already determined that it is
802 : : written under the chrecs syntax:
803 : :
804 : : | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
805 : :
806 : : We start from the semantics of the SSA program:
807 : :
808 : : | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
809 : : |
810 : : | f (x) = init + \sum_{j = 0}^{x - 1}
811 : : | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
812 : : |
813 : : | f (x) = init + \sum_{j = 0}^{x - 1}
814 : : | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
815 : : |
816 : : | f (x) = init + \sum_{k = 0}^{n - 1}
817 : : | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
818 : : |
819 : : | f (x) = init + \sum_{k = 0}^{n - 1}
820 : : | (b_k * \binom{x}{k + 1})
821 : : |
822 : : | f (x) = init + b_0 * \binom{x}{1} + ...
823 : : | + b_{n-1} * \binom{x}{n}
824 : : |
825 : : | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
826 : : | + b_{n-1} * \binom{x}{n}
827 : : |
828 : :
829 : : And finally from the definition of the chrecs syntax, we identify:
830 : : | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
831 : :
832 : : This shows the mechanism that stands behind the add_to_evolution
833 : : function. An important point is that the use of symbolic
834 : : parameters avoids the need of an analysis schedule.
835 : :
836 : : Example:
837 : :
838 : : | inita = ...
839 : : | initb = ...
840 : : | loop_1
841 : : | a = phi (inita, a + 2 + b)
842 : : | b = phi (initb, b + 1)
843 : : | endloop
844 : :
845 : : When analyzing "a", the algorithm keeps "b" symbolically:
846 : :
847 : : | a -> {inita, +, 2 + b}_1
848 : :
849 : : Then, after instantiation, the analyzer ends on the evolution:
850 : :
851 : : | a -> {inita, +, 2 + initb, +, 1}_1
852 : :
853 : : */
854 : :
855 : : tree
856 : 8877623 : scev_dfs::add_to_evolution (tree chrec_before, enum tree_code code,
857 : : tree to_add, gimple *at_stmt)
858 : : {
859 : 8877623 : tree type = chrec_type (to_add);
860 : 8877623 : tree res = NULL_TREE;
861 : :
862 : 8877623 : if (to_add == NULL_TREE)
863 : : return chrec_before;
864 : :
865 : : /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
866 : : instantiated at this point. */
867 : 8877623 : if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
868 : : /* This should not happen. */
869 : 0 : return chrec_dont_know;
870 : :
871 : 8877623 : if (dump_file && (dump_flags & TDF_SCEV))
872 : : {
873 : 1 : fprintf (dump_file, "(add_to_evolution \n");
874 : 1 : fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
875 : 1 : fprintf (dump_file, " (chrec_before = ");
876 : 1 : print_generic_expr (dump_file, chrec_before);
877 : 1 : fprintf (dump_file, ")\n (to_add = ");
878 : 1 : print_generic_expr (dump_file, to_add);
879 : 1 : fprintf (dump_file, ")\n");
880 : : }
881 : :
882 : 8877623 : if (code == MINUS_EXPR)
883 : 1592112 : to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type)
884 : 4621 : ? build_real (type, dconstm1)
885 : 1587491 : : build_int_cst_type (type, -1));
886 : :
887 : 8877623 : res = add_to_evolution_1 (chrec_before, to_add, at_stmt);
888 : :
889 : 8877623 : if (dump_file && (dump_flags & TDF_SCEV))
890 : : {
891 : 1 : fprintf (dump_file, " (res = ");
892 : 1 : print_generic_expr (dump_file, res);
893 : 1 : fprintf (dump_file, "))\n");
894 : : }
895 : :
896 : : return res;
897 : : }
898 : :
899 : :
900 : : /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
901 : : Return true if the strongly connected component has been found. */
902 : :
903 : : t_bool
904 : 1054980 : scev_dfs::follow_ssa_edge_binary (gimple *at_stmt, tree type, tree rhs0,
905 : : enum tree_code code, tree rhs1,
906 : : tree *evolution_of_loop, int limit)
907 : : {
908 : 1054980 : t_bool res = t_false;
909 : 1054980 : tree evol;
910 : :
911 : 1054980 : switch (code)
912 : : {
913 : 1048802 : case POINTER_PLUS_EXPR:
914 : 1048802 : case PLUS_EXPR:
915 : 1048802 : if (TREE_CODE (rhs0) == SSA_NAME)
916 : : {
917 : 1031483 : if (TREE_CODE (rhs1) == SSA_NAME)
918 : : {
919 : : /* Match an assignment under the form:
920 : : "a = b + c". */
921 : :
922 : : /* We want only assignments of form "name + name" contribute to
923 : : LIMIT, as the other cases do not necessarily contribute to
924 : : the complexity of the expression. */
925 : 1031483 : limit++;
926 : :
927 : 1031483 : evol = *evolution_of_loop;
928 : 1031483 : res = follow_ssa_edge_expr (at_stmt, rhs0, &evol, limit);
929 : 1031483 : if (res == t_true)
930 : 459367 : *evolution_of_loop = add_to_evolution
931 : 459367 : (chrec_convert (type, evol, at_stmt), code, rhs1, at_stmt);
932 : 572116 : else if (res == t_false)
933 : : {
934 : 554091 : res = follow_ssa_edge_expr
935 : 554091 : (at_stmt, rhs1, evolution_of_loop, limit);
936 : 554091 : if (res == t_true)
937 : 407721 : *evolution_of_loop = add_to_evolution
938 : 407721 : (chrec_convert (type, *evolution_of_loop, at_stmt),
939 : : code, rhs0, at_stmt);
940 : : }
941 : : }
942 : :
943 : : else
944 : 0 : gcc_unreachable (); /* Handled in caller. */
945 : : }
946 : :
947 : 17319 : else if (TREE_CODE (rhs1) == SSA_NAME)
948 : : {
949 : : /* Match an assignment under the form:
950 : : "a = ... + c". */
951 : 5800 : res = follow_ssa_edge_expr (at_stmt, rhs1, evolution_of_loop, limit);
952 : 5800 : if (res == t_true)
953 : 5153 : *evolution_of_loop = add_to_evolution
954 : 5153 : (chrec_convert (type, *evolution_of_loop, at_stmt),
955 : : code, rhs0, at_stmt);
956 : : }
957 : :
958 : : else
959 : : /* Otherwise, match an assignment under the form:
960 : : "a = ... + ...". */
961 : : /* And there is nothing to do. */
962 : : res = t_false;
963 : : break;
964 : :
965 : 6178 : case MINUS_EXPR:
966 : : /* This case is under the form "opnd0 = rhs0 - rhs1". */
967 : 6178 : if (TREE_CODE (rhs0) == SSA_NAME)
968 : 0 : gcc_unreachable (); /* Handled in caller. */
969 : : else
970 : : /* Otherwise, match an assignment under the form:
971 : : "a = ... - ...". */
972 : : /* And there is nothing to do. */
973 : : res = t_false;
974 : : break;
975 : :
976 : : default:
977 : : res = t_false;
978 : : }
979 : :
980 : 1054980 : return res;
981 : : }
982 : :
983 : : /* Checks whether the I-th argument of a PHI comes from a backedge. */
984 : :
985 : : static bool
986 : 8791724 : backedge_phi_arg_p (gphi *phi, int i)
987 : : {
988 : 8791724 : const_edge e = gimple_phi_arg_edge (phi, i);
989 : :
990 : : /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
991 : : about updating it anywhere, and this should work as well most of the
992 : : time. */
993 : 8791724 : if (e->flags & EDGE_IRREDUCIBLE_LOOP)
994 : 49958 : return true;
995 : :
996 : : return false;
997 : : }
998 : :
999 : : /* Helper function for one branch of the condition-phi-node. Return
1000 : : true if the strongly connected component has been found following
1001 : : this path. */
1002 : :
1003 : : t_bool
1004 : 3803283 : scev_dfs::follow_ssa_edge_in_condition_phi_branch (int i,
1005 : : gphi *condition_phi,
1006 : : tree *evolution_of_branch,
1007 : : tree init_cond, int limit)
1008 : : {
1009 : 3803283 : tree branch = PHI_ARG_DEF (condition_phi, i);
1010 : 3803283 : *evolution_of_branch = chrec_dont_know;
1011 : :
1012 : : /* Do not follow back edges (they must belong to an irreducible loop, which
1013 : : we really do not want to worry about). */
1014 : 3803283 : if (backedge_phi_arg_p (condition_phi, i))
1015 : : return t_false;
1016 : :
1017 : 3797640 : if (TREE_CODE (branch) == SSA_NAME)
1018 : : {
1019 : 3566611 : *evolution_of_branch = init_cond;
1020 : 3566611 : return follow_ssa_edge_expr (condition_phi, branch,
1021 : 3566611 : evolution_of_branch, limit);
1022 : : }
1023 : :
1024 : : /* This case occurs when one of the condition branches sets
1025 : : the variable to a constant: i.e. a phi-node like
1026 : : "a_2 = PHI <a_7(5), 2(6)>;".
1027 : :
1028 : : FIXME: This case have to be refined correctly:
1029 : : in some cases it is possible to say something better than
1030 : : chrec_dont_know, for example using a wrap-around notation. */
1031 : : return t_false;
1032 : : }
1033 : :
1034 : : /* This function merges the branches of a condition-phi-node in a
1035 : : loop. */
1036 : :
1037 : : t_bool
1038 : 2368956 : scev_dfs::follow_ssa_edge_in_condition_phi (gphi *condition_phi,
1039 : : tree *evolution_of_loop, int limit)
1040 : : {
1041 : 2368956 : int i, n;
1042 : 2368956 : tree init = *evolution_of_loop;
1043 : 2368956 : tree evolution_of_branch;
1044 : 2368956 : t_bool res = follow_ssa_edge_in_condition_phi_branch (0, condition_phi,
1045 : : &evolution_of_branch,
1046 : : init, limit);
1047 : 2368956 : if (res == t_false || res == t_dont_know)
1048 : : return res;
1049 : :
1050 : 1465090 : *evolution_of_loop = evolution_of_branch;
1051 : :
1052 : 1465090 : n = gimple_phi_num_args (condition_phi);
1053 : 2252125 : for (i = 1; i < n; i++)
1054 : : {
1055 : : /* Quickly give up when the evolution of one of the branches is
1056 : : not known. */
1057 : 1572466 : if (*evolution_of_loop == chrec_dont_know)
1058 : : return t_true;
1059 : :
1060 : : /* Increase the limit by the PHI argument number to avoid exponential
1061 : : time and memory complexity. */
1062 : 1434327 : res = follow_ssa_edge_in_condition_phi_branch (i, condition_phi,
1063 : : &evolution_of_branch,
1064 : : init, limit + i);
1065 : 1434327 : if (res == t_false || res == t_dont_know)
1066 : : return res;
1067 : :
1068 : 787035 : *evolution_of_loop = chrec_merge (*evolution_of_loop,
1069 : : evolution_of_branch);
1070 : : }
1071 : :
1072 : : return t_true;
1073 : : }
1074 : :
1075 : : /* Follow an SSA edge in an inner loop. It computes the overall
1076 : : effect of the loop, and following the symbolic initial conditions,
1077 : : it follows the edges in the parent loop. The inner loop is
1078 : : considered as a single statement. */
1079 : :
1080 : : t_bool
1081 : 258707 : scev_dfs::follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
1082 : : tree *evolution_of_loop, int limit)
1083 : : {
1084 : 258707 : class loop *loop = loop_containing_stmt (loop_phi_node);
1085 : 258707 : tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1086 : :
1087 : : /* Sometimes, the inner loop is too difficult to analyze, and the
1088 : : result of the analysis is a symbolic parameter. */
1089 : 258707 : if (ev == PHI_RESULT (loop_phi_node))
1090 : : {
1091 : 108676 : t_bool res = t_false;
1092 : 108676 : int i, n = gimple_phi_num_args (loop_phi_node);
1093 : :
1094 : 161504 : for (i = 0; i < n; i++)
1095 : : {
1096 : 153062 : tree arg = PHI_ARG_DEF (loop_phi_node, i);
1097 : 153062 : basic_block bb;
1098 : :
1099 : : /* Follow the edges that exit the inner loop. */
1100 : 153062 : bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1101 : 153062 : if (!flow_bb_inside_loop_p (loop, bb))
1102 : 108676 : res = follow_ssa_edge_expr (loop_phi_node,
1103 : : arg, evolution_of_loop, limit);
1104 : 153062 : if (res == t_true)
1105 : : break;
1106 : : }
1107 : :
1108 : : /* If the path crosses this loop-phi, give up. */
1109 : 108676 : if (res == t_true)
1110 : 100234 : *evolution_of_loop = chrec_dont_know;
1111 : :
1112 : 108676 : return res;
1113 : : }
1114 : :
1115 : : /* Otherwise, compute the overall effect of the inner loop. */
1116 : 150031 : ev = compute_overall_effect_of_inner_loop (loop, ev);
1117 : 150031 : return follow_ssa_edge_expr (loop_phi_node, ev, evolution_of_loop, limit);
1118 : : }
1119 : :
1120 : : /* Follow the ssa edge into the expression EXPR.
1121 : : Return true if the strongly connected component has been found. */
1122 : :
1123 : : t_bool
1124 : 24747375 : scev_dfs::follow_ssa_edge_expr (gimple *at_stmt, tree expr,
1125 : : tree *evolution_of_loop, int limit)
1126 : : {
1127 : 24747375 : gphi *halting_phi = loop_phi_node;
1128 : 24747375 : enum tree_code code;
1129 : 24747375 : tree type, rhs0, rhs1 = NULL_TREE;
1130 : :
1131 : : /* The EXPR is one of the following cases:
1132 : : - an SSA_NAME,
1133 : : - an INTEGER_CST,
1134 : : - a PLUS_EXPR,
1135 : : - a POINTER_PLUS_EXPR,
1136 : : - a MINUS_EXPR,
1137 : : - other cases are not yet handled. */
1138 : :
1139 : : /* For SSA_NAME look at the definition statement, handling
1140 : : PHI nodes and otherwise expand appropriately for the expression
1141 : : handling below. */
1142 : 24747375 : if (TREE_CODE (expr) == SSA_NAME)
1143 : : {
1144 : 24584649 : gimple *def = SSA_NAME_DEF_STMT (expr);
1145 : :
1146 : 24584649 : if (gimple_nop_p (def))
1147 : : return t_false;
1148 : :
1149 : : /* Give up if the path is longer than the MAX that we allow. */
1150 : 24569521 : if (limit > param_scev_max_expr_complexity)
1151 : : {
1152 : 7418 : *evolution_of_loop = chrec_dont_know;
1153 : 7418 : return t_dont_know;
1154 : : }
1155 : :
1156 : 24562103 : if (gphi *phi = dyn_cast <gphi *>(def))
1157 : : {
1158 : 25910088 : if (!loop_phi_node_p (phi))
1159 : : /* DEF is a condition-phi-node. Follow the branches, and
1160 : : record their evolutions. Finally, merge the collected
1161 : : information and set the approximation to the main
1162 : : variable. */
1163 : 2368956 : return follow_ssa_edge_in_condition_phi (phi, evolution_of_loop,
1164 : 2368956 : limit);
1165 : :
1166 : : /* When the analyzed phi is the halting_phi, the
1167 : : depth-first search is over: we have found a path from
1168 : : the halting_phi to itself in the loop. */
1169 : 10586088 : if (phi == halting_phi)
1170 : : {
1171 : 10117875 : *evolution_of_loop = expr;
1172 : 10117875 : return t_true;
1173 : : }
1174 : :
1175 : : /* Otherwise, the evolution of the HALTING_PHI depends
1176 : : on the evolution of another loop-phi-node, i.e. the
1177 : : evolution function is a higher degree polynomial. */
1178 : 468213 : class loop *def_loop = loop_containing_stmt (def);
1179 : 468213 : if (def_loop == loop)
1180 : : return t_false;
1181 : :
1182 : : /* Inner loop. */
1183 : 280274 : if (flow_loop_nested_p (loop, def_loop))
1184 : 258707 : return follow_ssa_edge_inner_loop_phi (phi, evolution_of_loop,
1185 : 258707 : limit + 1);
1186 : :
1187 : : /* Outer loop. */
1188 : : return t_false;
1189 : : }
1190 : :
1191 : : /* At this level of abstraction, the program is just a set
1192 : : of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1193 : : other def to be handled. */
1194 : 11607059 : if (!is_gimple_assign (def))
1195 : : return t_false;
1196 : :
1197 : 11501519 : code = gimple_assign_rhs_code (def);
1198 : 11501519 : switch (get_gimple_rhs_class (code))
1199 : : {
1200 : 10032108 : case GIMPLE_BINARY_RHS:
1201 : 10032108 : rhs0 = gimple_assign_rhs1 (def);
1202 : 10032108 : rhs1 = gimple_assign_rhs2 (def);
1203 : 10032108 : break;
1204 : 1449553 : case GIMPLE_UNARY_RHS:
1205 : 1449553 : case GIMPLE_SINGLE_RHS:
1206 : 1449553 : rhs0 = gimple_assign_rhs1 (def);
1207 : 1449553 : break;
1208 : : default:
1209 : : return t_false;
1210 : : }
1211 : 11481661 : type = TREE_TYPE (gimple_assign_lhs (def));
1212 : 11481661 : at_stmt = def;
1213 : : }
1214 : : else
1215 : : {
1216 : 162726 : code = TREE_CODE (expr);
1217 : 162726 : type = TREE_TYPE (expr);
1218 : : /* Via follow_ssa_edge_inner_loop_phi we arrive here with the
1219 : : GENERIC scalar evolution of the inner loop. */
1220 : 162726 : switch (code)
1221 : : {
1222 : 11108 : CASE_CONVERT:
1223 : 11108 : rhs0 = TREE_OPERAND (expr, 0);
1224 : 11108 : break;
1225 : 22938 : case POINTER_PLUS_EXPR:
1226 : 22938 : case PLUS_EXPR:
1227 : 22938 : case MINUS_EXPR:
1228 : 22938 : rhs0 = TREE_OPERAND (expr, 0);
1229 : 22938 : rhs1 = TREE_OPERAND (expr, 1);
1230 : 22938 : STRIP_USELESS_TYPE_CONVERSION (rhs0);
1231 : 22938 : STRIP_USELESS_TYPE_CONVERSION (rhs1);
1232 : 22938 : break;
1233 : : default:
1234 : : rhs0 = expr;
1235 : : }
1236 : : }
1237 : :
1238 : 11644387 : switch (code)
1239 : : {
1240 : 346342 : CASE_CONVERT:
1241 : 346342 : {
1242 : : /* This assignment is under the form "a_1 = (cast) rhs. We cannot
1243 : : validate any precision altering conversion during the SCC
1244 : : analysis, so don't even try. */
1245 : 346342 : if (!tree_nop_conversion_p (type, TREE_TYPE (rhs0)))
1246 : : return t_false;
1247 : 229635 : t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
1248 : : evolution_of_loop, limit);
1249 : 229635 : if (res == t_true)
1250 : 109880 : *evolution_of_loop = chrec_convert (type, *evolution_of_loop,
1251 : : at_stmt);
1252 : : return res;
1253 : : }
1254 : :
1255 : : case INTEGER_CST:
1256 : : /* This assignment is under the form "a_1 = 7". */
1257 : : return t_false;
1258 : :
1259 : 2362 : case ADDR_EXPR:
1260 : 2362 : {
1261 : : /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1262 : 2362 : if (TREE_CODE (TREE_OPERAND (rhs0, 0)) != MEM_REF)
1263 : : return t_false;
1264 : 0 : tree mem = TREE_OPERAND (rhs0, 0);
1265 : 0 : rhs0 = TREE_OPERAND (mem, 0);
1266 : 0 : rhs1 = TREE_OPERAND (mem, 1);
1267 : 0 : code = POINTER_PLUS_EXPR;
1268 : : }
1269 : : /* Fallthru. */
1270 : 9311938 : case POINTER_PLUS_EXPR:
1271 : 9311938 : case PLUS_EXPR:
1272 : 9311938 : case MINUS_EXPR:
1273 : : /* This case is under the form "rhs0 +- rhs1". */
1274 : 9311938 : if (TREE_CODE (rhs0) == SSA_NAME
1275 : 9288441 : && (TREE_CODE (rhs1) != SSA_NAME || code == MINUS_EXPR))
1276 : : {
1277 : : /* Match an assignment under the form:
1278 : : "a = b +- ...". */
1279 : 8256958 : t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
1280 : : evolution_of_loop, limit);
1281 : 8256958 : if (res == t_true)
1282 : 8005382 : *evolution_of_loop = add_to_evolution
1283 : 8005382 : (chrec_convert (type, *evolution_of_loop, at_stmt),
1284 : : code, rhs1, at_stmt);
1285 : 8256958 : return res;
1286 : : }
1287 : : /* Else search for the SCC in both rhs0 and rhs1. */
1288 : 1054980 : return follow_ssa_edge_binary (at_stmt, type, rhs0, code, rhs1,
1289 : 1054980 : evolution_of_loop, limit);
1290 : :
1291 : : default:
1292 : : return t_false;
1293 : : }
1294 : : }
1295 : :
1296 : :
1297 : : /* This section selects the loops that will be good candidates for the
1298 : : scalar evolution analysis. For the moment, greedily select all the
1299 : : loop nests we could analyze. */
1300 : :
1301 : : /* For a loop with a single exit edge, return the COND_EXPR that
1302 : : guards the exit edge. If the expression is too difficult to
1303 : : analyze, then give up. */
1304 : :
1305 : : gcond *
1306 : 236 : get_loop_exit_condition (const class loop *loop)
1307 : : {
1308 : 236 : return get_loop_exit_condition (single_exit (loop));
1309 : : }
1310 : :
1311 : : /* If the statement just before the EXIT_EDGE contains a condition then
1312 : : return the condition, otherwise NULL. */
1313 : :
1314 : : gcond *
1315 : 5276840 : get_loop_exit_condition (const_edge exit_edge)
1316 : : {
1317 : 5276840 : gcond *res = NULL;
1318 : :
1319 : 5276840 : if (dump_file && (dump_flags & TDF_SCEV))
1320 : 2 : fprintf (dump_file, "(get_loop_exit_condition \n ");
1321 : :
1322 : 5276840 : if (exit_edge)
1323 : 15830520 : res = safe_dyn_cast <gcond *> (*gsi_last_bb (exit_edge->src));
1324 : :
1325 : 5276840 : if (dump_file && (dump_flags & TDF_SCEV))
1326 : : {
1327 : 2 : print_gimple_stmt (dump_file, res, 0);
1328 : 2 : fprintf (dump_file, ")\n");
1329 : : }
1330 : :
1331 : 5276840 : return res;
1332 : : }
1333 : :
1334 : :
1335 : : /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1336 : : Handle below case and return the corresponding POLYNOMIAL_CHREC:
1337 : :
1338 : : # i_17 = PHI <i_13(5), 0(3)>
1339 : : # _20 = PHI <_5(5), start_4(D)(3)>
1340 : : ...
1341 : : i_13 = i_17 + 1;
1342 : : _5 = start_4(D) + i_13;
1343 : :
1344 : : Though variable _20 appears as a PEELED_CHREC in the form of
1345 : : (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1346 : :
1347 : : See PR41488. */
1348 : :
1349 : : static tree
1350 : 1595790 : simplify_peeled_chrec (class loop *loop, tree arg, tree init_cond)
1351 : : {
1352 : 3191580 : aff_tree aff1, aff2;
1353 : 1595790 : tree ev, left, right, type, step_val;
1354 : 1595790 : hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
1355 : :
1356 : 1595790 : ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
1357 : 1595790 : if (ev == NULL_TREE)
1358 : 0 : return chrec_dont_know;
1359 : :
1360 : : /* Support the case where we can derive the original CHREC from the
1361 : : peeled one if that's a converted other IV. This can be done
1362 : : when the original unpeeled converted IV does not overflow and
1363 : : has the same initial value. */
1364 : 1585269 : if (CONVERT_EXPR_P (ev)
1365 : 10521 : && TREE_CODE (init_cond) == INTEGER_CST
1366 : 3499 : && TREE_CODE (TREE_OPERAND (ev, 0)) == POLYNOMIAL_CHREC
1367 : 3213 : && (TYPE_PRECISION (TREE_TYPE (ev))
1368 : 3213 : > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (ev, 0))))
1369 : 1598850 : && (!TYPE_UNSIGNED (TREE_TYPE (ev))
1370 : 3060 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (ev, 0)))))
1371 : : {
1372 : 3060 : left = CHREC_LEFT (TREE_OPERAND (ev, 0));
1373 : 3060 : right = CHREC_RIGHT (TREE_OPERAND (ev, 0));
1374 : 3060 : tree left_before = chrec_fold_minus (TREE_TYPE (TREE_OPERAND (ev, 0)),
1375 : : left, right);
1376 : 3060 : if (TREE_CODE (left_before) == INTEGER_CST
1377 : 3060 : && wi::to_widest (init_cond) == wi::to_widest (left_before)
1378 : 6120 : && !scev_probably_wraps_p (NULL_TREE, left_before, right, NULL,
1379 : : loop, false))
1380 : 157 : return build_polynomial_chrec (loop->num, init_cond,
1381 : 157 : chrec_convert (TREE_TYPE (ev),
1382 : : right, NULL,
1383 : 157 : false, NULL_TREE));
1384 : 2903 : return chrec_dont_know;
1385 : : }
1386 : :
1387 : 1592730 : if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
1388 : 1552005 : return chrec_dont_know;
1389 : :
1390 : 40725 : left = CHREC_LEFT (ev);
1391 : 40725 : right = CHREC_RIGHT (ev);
1392 : 40725 : type = TREE_TYPE (left);
1393 : 40725 : step_val = chrec_fold_plus (type, init_cond, right);
1394 : :
1395 : : /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1396 : : if "left" equals to "init + right". */
1397 : 40725 : if (operand_equal_p (left, step_val, 0))
1398 : : {
1399 : 17554 : if (dump_file && (dump_flags & TDF_SCEV))
1400 : 1 : fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1401 : :
1402 : 17554 : return build_polynomial_chrec (loop->num, init_cond, right);
1403 : : }
1404 : :
1405 : : /* The affine code only deals with pointer and integer types. */
1406 : 23171 : if (!POINTER_TYPE_P (type)
1407 : 18106 : && !INTEGRAL_TYPE_P (type))
1408 : 15 : return chrec_dont_know;
1409 : :
1410 : : /* Try harder to check if they are equal. */
1411 : 23156 : tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
1412 : 23156 : tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
1413 : 23156 : free_affine_expand_cache (&peeled_chrec_map);
1414 : 23156 : aff_combination_scale (&aff2, -1);
1415 : 23156 : aff_combination_add (&aff1, &aff2);
1416 : :
1417 : : /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1418 : : if "left" equals to "init + right". */
1419 : 23156 : if (aff_combination_zero_p (&aff1))
1420 : : {
1421 : 11816 : if (dump_file && (dump_flags & TDF_SCEV))
1422 : 1 : fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1423 : :
1424 : 11816 : return build_polynomial_chrec (loop->num, init_cond, right);
1425 : : }
1426 : 11340 : return chrec_dont_know;
1427 : 1595790 : }
1428 : :
1429 : : /* Given a LOOP_PHI_NODE, this function determines the evolution
1430 : : function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1431 : :
1432 : : static tree
1433 : 10961359 : analyze_evolution_in_loop (gphi *loop_phi_node,
1434 : : tree init_cond)
1435 : : {
1436 : 10961359 : int i, n = gimple_phi_num_args (loop_phi_node);
1437 : 10961359 : tree evolution_function = chrec_not_analyzed_yet;
1438 : 10961359 : class loop *loop = loop_containing_stmt (loop_phi_node);
1439 : 10961359 : basic_block bb;
1440 : 10961359 : static bool simplify_peeled_chrec_p = true;
1441 : :
1442 : 10961359 : if (dump_file && (dump_flags & TDF_SCEV))
1443 : : {
1444 : 3 : fprintf (dump_file, "(analyze_evolution_in_loop \n");
1445 : 3 : fprintf (dump_file, " (loop_phi_node = ");
1446 : 3 : print_gimple_stmt (dump_file, loop_phi_node, 0);
1447 : 3 : fprintf (dump_file, ")\n");
1448 : : }
1449 : :
1450 : 28958723 : for (i = 0; i < n; i++)
1451 : : {
1452 : 20585550 : tree arg = PHI_ARG_DEF (loop_phi_node, i);
1453 : 20585550 : tree ev_fn = chrec_dont_know;
1454 : 20585550 : t_bool res;
1455 : :
1456 : : /* Select the edges that enter the loop body. */
1457 : 20585550 : bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1458 : 20585550 : if (!flow_bb_inside_loop_p (loop, bb))
1459 : 9624191 : continue;
1460 : :
1461 : 10961359 : if (TREE_CODE (arg) == SSA_NAME)
1462 : : {
1463 : 10844090 : bool val = false;
1464 : :
1465 : : /* Pass in the initial condition to the follow edge function. */
1466 : 10844090 : scev_dfs dfs (loop, loop_phi_node, init_cond);
1467 : 10844090 : res = dfs.get_ev (&ev_fn, arg);
1468 : :
1469 : : /* If ev_fn has no evolution in the inner loop, and the
1470 : : init_cond is not equal to ev_fn, then we have an
1471 : : ambiguity between two possible values, as we cannot know
1472 : : the number of iterations at this point. */
1473 : 10844090 : if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
1474 : 2639598 : && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
1475 : 10844096 : && !operand_equal_p (init_cond, ev_fn, 0))
1476 : 0 : ev_fn = chrec_dont_know;
1477 : : }
1478 : : else
1479 : : res = t_false;
1480 : :
1481 : : /* When it is impossible to go back on the same
1482 : : loop_phi_node by following the ssa edges, the
1483 : : evolution is represented by a peeled chrec, i.e. the
1484 : : first iteration, EV_FN has the value INIT_COND, then
1485 : : all the other iterations it has the value of ARG.
1486 : : For the moment, PEELED_CHREC nodes are not built. */
1487 : 10844090 : if (res != t_true)
1488 : : {
1489 : 2277811 : ev_fn = chrec_dont_know;
1490 : : /* Try to recognize POLYNOMIAL_CHREC which appears in
1491 : : the form of PEELED_CHREC, but guard the process with
1492 : : a bool variable to keep the analyzer from infinite
1493 : : recurrence for real PEELED_RECs. */
1494 : 2277811 : if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
1495 : : {
1496 : 1595790 : simplify_peeled_chrec_p = false;
1497 : 1595790 : ev_fn = simplify_peeled_chrec (loop, arg, init_cond);
1498 : 1595790 : simplify_peeled_chrec_p = true;
1499 : : }
1500 : : }
1501 : :
1502 : : /* When there are multiple back edges of the loop (which in fact never
1503 : : happens currently, but nevertheless), merge their evolutions. */
1504 : 10961359 : evolution_function = chrec_merge (evolution_function, ev_fn);
1505 : :
1506 : 10961359 : if (evolution_function == chrec_dont_know)
1507 : : break;
1508 : : }
1509 : :
1510 : 10961359 : if (dump_file && (dump_flags & TDF_SCEV))
1511 : : {
1512 : 3 : fprintf (dump_file, " (evolution_function = ");
1513 : 3 : print_generic_expr (dump_file, evolution_function);
1514 : 3 : fprintf (dump_file, "))\n");
1515 : : }
1516 : :
1517 : 10961359 : return evolution_function;
1518 : : }
1519 : :
1520 : : /* Looks to see if VAR is a copy of a constant (via straightforward assignments
1521 : : or degenerate phi's). If so, returns the constant; else, returns VAR. */
1522 : :
1523 : : static tree
1524 : 22460347 : follow_copies_to_constant (tree var)
1525 : : {
1526 : 22460347 : tree res = var;
1527 : 22460347 : while (TREE_CODE (res) == SSA_NAME
1528 : : /* We face not updated SSA form in multiple places and this walk
1529 : : may end up in sibling loops so we have to guard it. */
1530 : 27211248 : && !name_registered_for_update_p (res))
1531 : : {
1532 : 16252219 : gimple *def = SSA_NAME_DEF_STMT (res);
1533 : 16252219 : if (gphi *phi = dyn_cast <gphi *> (def))
1534 : : {
1535 : 4535022 : if (tree rhs = degenerate_phi_result (phi))
1536 : : res = rhs;
1537 : : else
1538 : : break;
1539 : : }
1540 : 11717197 : else if (gimple_assign_single_p (def))
1541 : : /* Will exit loop if not an SSA_NAME. */
1542 : 4301786 : res = gimple_assign_rhs1 (def);
1543 : : else
1544 : : break;
1545 : : }
1546 : 22460347 : if (CONSTANT_CLASS_P (res))
1547 : 6508719 : return res;
1548 : : return var;
1549 : : }
1550 : :
1551 : : /* Given a loop-phi-node, return the initial conditions of the
1552 : : variable on entry of the loop. When the CCP has propagated
1553 : : constants into the loop-phi-node, the initial condition is
1554 : : instantiated, otherwise the initial condition is kept symbolic.
1555 : : This analyzer does not analyze the evolution outside the current
1556 : : loop, and leaves this task to the on-demand tree reconstructor. */
1557 : :
1558 : : static tree
1559 : 10961359 : analyze_initial_condition (gphi *loop_phi_node)
1560 : : {
1561 : 10961359 : int i, n;
1562 : 10961359 : tree init_cond = chrec_not_analyzed_yet;
1563 : 10961359 : class loop *loop = loop_containing_stmt (loop_phi_node);
1564 : :
1565 : 10961359 : if (dump_file && (dump_flags & TDF_SCEV))
1566 : : {
1567 : 3 : fprintf (dump_file, "(analyze_initial_condition \n");
1568 : 3 : fprintf (dump_file, " (loop_phi_node = \n");
1569 : 3 : print_gimple_stmt (dump_file, loop_phi_node, 0);
1570 : 3 : fprintf (dump_file, ")\n");
1571 : : }
1572 : :
1573 : 10961359 : n = gimple_phi_num_args (loop_phi_node);
1574 : 32884077 : for (i = 0; i < n; i++)
1575 : : {
1576 : 21922718 : tree branch = PHI_ARG_DEF (loop_phi_node, i);
1577 : 21922718 : basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1578 : :
1579 : : /* When the branch is oriented to the loop's body, it does
1580 : : not contribute to the initial condition. */
1581 : 21922718 : if (flow_bb_inside_loop_p (loop, bb))
1582 : 10961359 : continue;
1583 : :
1584 : 10961359 : if (init_cond == chrec_not_analyzed_yet)
1585 : : {
1586 : 10961359 : init_cond = branch;
1587 : 10961359 : continue;
1588 : : }
1589 : :
1590 : 0 : if (TREE_CODE (branch) == SSA_NAME)
1591 : : {
1592 : 0 : init_cond = chrec_dont_know;
1593 : 0 : break;
1594 : : }
1595 : :
1596 : 0 : init_cond = chrec_merge (init_cond, branch);
1597 : : }
1598 : :
1599 : : /* Ooops -- a loop without an entry??? */
1600 : 10961359 : if (init_cond == chrec_not_analyzed_yet)
1601 : 0 : init_cond = chrec_dont_know;
1602 : :
1603 : : /* We may not have fully constant propagated IL. Handle degenerate PHIs here
1604 : : to not miss important early loop unrollings. */
1605 : 10961359 : init_cond = follow_copies_to_constant (init_cond);
1606 : :
1607 : 10961359 : if (dump_file && (dump_flags & TDF_SCEV))
1608 : : {
1609 : 3 : fprintf (dump_file, " (init_cond = ");
1610 : 3 : print_generic_expr (dump_file, init_cond);
1611 : 3 : fprintf (dump_file, "))\n");
1612 : : }
1613 : :
1614 : 10961359 : return init_cond;
1615 : : }
1616 : :
1617 : : /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1618 : :
1619 : : static tree
1620 : 10961359 : interpret_loop_phi (class loop *loop, gphi *loop_phi_node)
1621 : : {
1622 : 10961359 : class loop *phi_loop = loop_containing_stmt (loop_phi_node);
1623 : 10961359 : tree init_cond;
1624 : :
1625 : 10961359 : gcc_assert (phi_loop == loop);
1626 : :
1627 : : /* Otherwise really interpret the loop phi. */
1628 : 10961359 : init_cond = analyze_initial_condition (loop_phi_node);
1629 : 10961359 : return analyze_evolution_in_loop (loop_phi_node, init_cond);
1630 : : }
1631 : :
1632 : : /* This function merges the branches of a condition-phi-node,
1633 : : contained in the outermost loop, and whose arguments are already
1634 : : analyzed. */
1635 : :
1636 : : static tree
1637 : 2633835 : interpret_condition_phi (class loop *loop, gphi *condition_phi)
1638 : : {
1639 : 2633835 : int i, n = gimple_phi_num_args (condition_phi);
1640 : 2633835 : tree res = chrec_not_analyzed_yet;
1641 : :
1642 : 5614697 : for (i = 0; i < n; i++)
1643 : : {
1644 : 4988441 : tree branch_chrec;
1645 : :
1646 : 4988441 : if (backedge_phi_arg_p (condition_phi, i))
1647 : : {
1648 : 44315 : res = chrec_dont_know;
1649 : 44315 : break;
1650 : : }
1651 : :
1652 : 4944126 : branch_chrec = analyze_scalar_evolution
1653 : 4944126 : (loop, PHI_ARG_DEF (condition_phi, i));
1654 : :
1655 : 4944126 : res = chrec_merge (res, branch_chrec);
1656 : 4944126 : if (res == chrec_dont_know)
1657 : : break;
1658 : : }
1659 : :
1660 : 2633835 : return res;
1661 : : }
1662 : :
1663 : : /* Interpret the operation RHS1 OP RHS2. If we didn't
1664 : : analyze this node before, follow the definitions until ending
1665 : : either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1666 : : return path, this function propagates evolutions (ala constant copy
1667 : : propagation). OPND1 is not a GIMPLE expression because we could
1668 : : analyze the effect of an inner loop: see interpret_loop_phi. */
1669 : :
1670 : : static tree
1671 : 44732681 : interpret_rhs_expr (class loop *loop, gimple *at_stmt,
1672 : : tree type, tree rhs1, enum tree_code code, tree rhs2)
1673 : : {
1674 : 44732681 : tree res, chrec1, chrec2, ctype;
1675 : 44732681 : gimple *def;
1676 : :
1677 : 44732681 : if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1678 : : {
1679 : 10939496 : if (is_gimple_min_invariant (rhs1))
1680 : 2457684 : return chrec_convert (type, rhs1, at_stmt);
1681 : :
1682 : 8481812 : if (code == SSA_NAME)
1683 : 148837 : return chrec_convert (type, analyze_scalar_evolution (loop, rhs1),
1684 : 148837 : at_stmt);
1685 : : }
1686 : :
1687 : 42126160 : switch (code)
1688 : : {
1689 : 336214 : case ADDR_EXPR:
1690 : 336214 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1691 : 336214 : || handled_component_p (TREE_OPERAND (rhs1, 0)))
1692 : : {
1693 : 336204 : machine_mode mode;
1694 : 336204 : poly_int64 bitsize, bitpos;
1695 : 336204 : int unsignedp, reversep;
1696 : 336204 : int volatilep = 0;
1697 : 336204 : tree base, offset;
1698 : 336204 : tree chrec3;
1699 : 336204 : tree unitpos;
1700 : :
1701 : 336204 : base = get_inner_reference (TREE_OPERAND (rhs1, 0),
1702 : : &bitsize, &bitpos, &offset, &mode,
1703 : : &unsignedp, &reversep, &volatilep);
1704 : :
1705 : 336204 : if (TREE_CODE (base) == MEM_REF)
1706 : : {
1707 : 257244 : rhs2 = TREE_OPERAND (base, 1);
1708 : 257244 : rhs1 = TREE_OPERAND (base, 0);
1709 : :
1710 : 257244 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1711 : 257244 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1712 : 257244 : chrec1 = chrec_convert (type, chrec1, at_stmt);
1713 : 257244 : chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1714 : 257244 : chrec1 = instantiate_parameters (loop, chrec1);
1715 : 257244 : chrec2 = instantiate_parameters (loop, chrec2);
1716 : 257244 : res = chrec_fold_plus (type, chrec1, chrec2);
1717 : : }
1718 : : else
1719 : : {
1720 : 78960 : chrec1 = analyze_scalar_evolution_for_address_of (loop, base);
1721 : 78960 : chrec1 = chrec_convert (type, chrec1, at_stmt);
1722 : 78960 : res = chrec1;
1723 : : }
1724 : :
1725 : 336204 : if (offset != NULL_TREE)
1726 : : {
1727 : 147376 : chrec2 = analyze_scalar_evolution (loop, offset);
1728 : 147376 : chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt);
1729 : 147376 : chrec2 = instantiate_parameters (loop, chrec2);
1730 : 147376 : res = chrec_fold_plus (type, res, chrec2);
1731 : : }
1732 : :
1733 : 336204 : if (maybe_ne (bitpos, 0))
1734 : : {
1735 : 127531 : unitpos = size_int (exact_div (bitpos, BITS_PER_UNIT));
1736 : 127531 : chrec3 = analyze_scalar_evolution (loop, unitpos);
1737 : 127531 : chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt);
1738 : 127531 : chrec3 = instantiate_parameters (loop, chrec3);
1739 : 127531 : res = chrec_fold_plus (type, res, chrec3);
1740 : : }
1741 : : }
1742 : : else
1743 : 10 : res = chrec_dont_know;
1744 : : break;
1745 : :
1746 : 3405101 : case POINTER_PLUS_EXPR:
1747 : 3405101 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1748 : 3405101 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1749 : 3405101 : chrec1 = chrec_convert (type, chrec1, at_stmt);
1750 : 3405101 : chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt);
1751 : 3405101 : chrec1 = instantiate_parameters (loop, chrec1);
1752 : 3405101 : chrec2 = instantiate_parameters (loop, chrec2);
1753 : 3405101 : res = chrec_fold_plus (type, chrec1, chrec2);
1754 : 3405101 : break;
1755 : :
1756 : 117107 : case POINTER_DIFF_EXPR:
1757 : 117107 : {
1758 : 117107 : tree utype = unsigned_type_for (type);
1759 : 117107 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1760 : 117107 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1761 : 117107 : chrec1 = chrec_convert (utype, chrec1, at_stmt);
1762 : 117107 : chrec2 = chrec_convert (utype, chrec2, at_stmt);
1763 : 117107 : chrec1 = instantiate_parameters (loop, chrec1);
1764 : 117107 : chrec2 = instantiate_parameters (loop, chrec2);
1765 : 117107 : res = chrec_fold_minus (utype, chrec1, chrec2);
1766 : 117107 : res = chrec_convert (type, res, at_stmt);
1767 : 117107 : break;
1768 : : }
1769 : :
1770 : 11741266 : case PLUS_EXPR:
1771 : 11741266 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1772 : 11741266 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1773 : 11741266 : ctype = type;
1774 : : /* When the stmt is conditionally executed re-write the CHREC
1775 : : into a form that has well-defined behavior on overflow. */
1776 : 11741266 : if (at_stmt
1777 : 10618298 : && INTEGRAL_TYPE_P (type)
1778 : 10524029 : && ! TYPE_OVERFLOW_WRAPS (type)
1779 : 19770882 : && ! dominated_by_p (CDI_DOMINATORS, loop->latch,
1780 : 8029616 : gimple_bb (at_stmt)))
1781 : 658707 : ctype = unsigned_type_for (type);
1782 : 11741266 : chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1783 : 11741266 : chrec2 = chrec_convert (ctype, chrec2, at_stmt);
1784 : 11741266 : chrec1 = instantiate_parameters (loop, chrec1);
1785 : 11741266 : chrec2 = instantiate_parameters (loop, chrec2);
1786 : 11741266 : res = chrec_fold_plus (ctype, chrec1, chrec2);
1787 : 11741266 : if (type != ctype)
1788 : 658707 : res = chrec_convert (type, res, at_stmt);
1789 : : break;
1790 : :
1791 : 1458476 : case MINUS_EXPR:
1792 : 1458476 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1793 : 1458476 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1794 : 1458476 : ctype = type;
1795 : : /* When the stmt is conditionally executed re-write the CHREC
1796 : : into a form that has well-defined behavior on overflow. */
1797 : 1458476 : if (at_stmt
1798 : 1385273 : && INTEGRAL_TYPE_P (type)
1799 : 1348759 : && ! TYPE_OVERFLOW_WRAPS (type)
1800 : 1957514 : && ! dominated_by_p (CDI_DOMINATORS,
1801 : 499038 : loop->latch, gimple_bb (at_stmt)))
1802 : 93638 : ctype = unsigned_type_for (type);
1803 : 1458476 : chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1804 : 1458476 : chrec2 = chrec_convert (ctype, chrec2, at_stmt);
1805 : 1458476 : chrec1 = instantiate_parameters (loop, chrec1);
1806 : 1458476 : chrec2 = instantiate_parameters (loop, chrec2);
1807 : 1458476 : res = chrec_fold_minus (ctype, chrec1, chrec2);
1808 : 1458476 : if (type != ctype)
1809 : 93638 : res = chrec_convert (type, res, at_stmt);
1810 : : break;
1811 : :
1812 : 83224 : case NEGATE_EXPR:
1813 : 83224 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1814 : 83224 : ctype = type;
1815 : : /* When the stmt is conditionally executed re-write the CHREC
1816 : : into a form that has well-defined behavior on overflow. */
1817 : 83224 : if (at_stmt
1818 : 71924 : && INTEGRAL_TYPE_P (type)
1819 : 70140 : && ! TYPE_OVERFLOW_WRAPS (type)
1820 : 122519 : && ! dominated_by_p (CDI_DOMINATORS,
1821 : 39295 : loop->latch, gimple_bb (at_stmt)))
1822 : 7720 : ctype = unsigned_type_for (type);
1823 : 83224 : chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1824 : : /* TYPE may be integer, real or complex, so use fold_convert. */
1825 : 83224 : chrec1 = instantiate_parameters (loop, chrec1);
1826 : 83224 : res = chrec_fold_multiply (ctype, chrec1,
1827 : : fold_convert (ctype, integer_minus_one_node));
1828 : 83224 : if (type != ctype)
1829 : 7720 : res = chrec_convert (type, res, at_stmt);
1830 : : break;
1831 : :
1832 : 35456 : case BIT_NOT_EXPR:
1833 : : /* Handle ~X as -1 - X. */
1834 : 35456 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1835 : 35456 : chrec1 = chrec_convert (type, chrec1, at_stmt);
1836 : 35456 : chrec1 = instantiate_parameters (loop, chrec1);
1837 : 35456 : res = chrec_fold_minus (type,
1838 : : fold_convert (type, integer_minus_one_node),
1839 : : chrec1);
1840 : 35456 : break;
1841 : :
1842 : 5995766 : case MULT_EXPR:
1843 : 5995766 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1844 : 5995766 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1845 : 5995766 : ctype = type;
1846 : : /* When the stmt is conditionally executed re-write the CHREC
1847 : : into a form that has well-defined behavior on overflow. */
1848 : 5995766 : if (at_stmt
1849 : 4127491 : && INTEGRAL_TYPE_P (type)
1850 : 4016674 : && ! TYPE_OVERFLOW_WRAPS (type)
1851 : 7824400 : && ! dominated_by_p (CDI_DOMINATORS,
1852 : 1828634 : loop->latch, gimple_bb (at_stmt)))
1853 : 157132 : ctype = unsigned_type_for (type);
1854 : 5995766 : chrec1 = chrec_convert (ctype, chrec1, at_stmt);
1855 : 5995766 : chrec2 = chrec_convert (ctype, chrec2, at_stmt);
1856 : 5995766 : chrec1 = instantiate_parameters (loop, chrec1);
1857 : 5995766 : chrec2 = instantiate_parameters (loop, chrec2);
1858 : 5995766 : res = chrec_fold_multiply (ctype, chrec1, chrec2);
1859 : 5995766 : if (type != ctype)
1860 : 157132 : res = chrec_convert (type, res, at_stmt);
1861 : : break;
1862 : :
1863 : 155438 : case LSHIFT_EXPR:
1864 : 155438 : {
1865 : : /* Handle A<<B as A * (1<<B). */
1866 : 155438 : tree uns = unsigned_type_for (type);
1867 : 155438 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1868 : 155438 : chrec2 = analyze_scalar_evolution (loop, rhs2);
1869 : 155438 : chrec1 = chrec_convert (uns, chrec1, at_stmt);
1870 : 155438 : chrec1 = instantiate_parameters (loop, chrec1);
1871 : 155438 : chrec2 = instantiate_parameters (loop, chrec2);
1872 : :
1873 : 155438 : tree one = build_int_cst (uns, 1);
1874 : 155438 : chrec2 = fold_build2 (LSHIFT_EXPR, uns, one, chrec2);
1875 : 155438 : res = chrec_fold_multiply (uns, chrec1, chrec2);
1876 : 155438 : res = chrec_convert (type, res, at_stmt);
1877 : : }
1878 : 155438 : break;
1879 : :
1880 : 8485737 : CASE_CONVERT:
1881 : : /* In case we have a truncation of a widened operation that in
1882 : : the truncated type has undefined overflow behavior analyze
1883 : : the operation done in an unsigned type of the same precision
1884 : : as the final truncation. We cannot derive a scalar evolution
1885 : : for the widened operation but for the truncated result. */
1886 : 8485737 : if (TREE_CODE (type) == INTEGER_TYPE
1887 : 8189924 : && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE
1888 : 7666755 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
1889 : 472923 : && TYPE_OVERFLOW_UNDEFINED (type)
1890 : 268900 : && TREE_CODE (rhs1) == SSA_NAME
1891 : 268732 : && (def = SSA_NAME_DEF_STMT (rhs1))
1892 : 268732 : && is_gimple_assign (def)
1893 : 165054 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
1894 : 8602456 : && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1895 : : {
1896 : 83595 : tree utype = unsigned_type_for (type);
1897 : 83595 : chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
1898 : : gimple_assign_rhs1 (def),
1899 : : gimple_assign_rhs_code (def),
1900 : : gimple_assign_rhs2 (def));
1901 : : }
1902 : : else
1903 : 8402142 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1904 : 8485737 : res = chrec_convert (type, chrec1, at_stmt, true, rhs1);
1905 : 8485737 : break;
1906 : :
1907 : 411620 : case BIT_AND_EXPR:
1908 : : /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
1909 : : If A is SCEV and its value is in the range of representable set
1910 : : of type unsigned short, the result expression is a (no-overflow)
1911 : : SCEV. */
1912 : 411620 : res = chrec_dont_know;
1913 : 411620 : if (tree_fits_uhwi_p (rhs2))
1914 : : {
1915 : 281175 : int precision;
1916 : 281175 : unsigned HOST_WIDE_INT val = tree_to_uhwi (rhs2);
1917 : :
1918 : 281175 : val ++;
1919 : : /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
1920 : : it's not the maximum value of a smaller type than rhs1. */
1921 : 281175 : if (val != 0
1922 : 224813 : && (precision = exact_log2 (val)) > 0
1923 : 505988 : && (unsigned) precision < TYPE_PRECISION (TREE_TYPE (rhs1)))
1924 : : {
1925 : 224813 : tree utype = build_nonstandard_integer_type (precision, 1);
1926 : :
1927 : 224813 : if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (rhs1)))
1928 : : {
1929 : 224813 : chrec1 = analyze_scalar_evolution (loop, rhs1);
1930 : 224813 : chrec1 = chrec_convert (utype, chrec1, at_stmt);
1931 : 224813 : res = chrec_convert (TREE_TYPE (rhs1), chrec1, at_stmt);
1932 : : }
1933 : : }
1934 : : }
1935 : : break;
1936 : :
1937 : 9900755 : default:
1938 : 9900755 : res = chrec_dont_know;
1939 : 9900755 : break;
1940 : : }
1941 : :
1942 : : return res;
1943 : : }
1944 : :
1945 : : /* Interpret the expression EXPR. */
1946 : :
1947 : : static tree
1948 : 8792189 : interpret_expr (class loop *loop, gimple *at_stmt, tree expr)
1949 : : {
1950 : 8792189 : enum tree_code code;
1951 : 8792189 : tree type = TREE_TYPE (expr), op0, op1;
1952 : :
1953 : 8792189 : if (automatically_generated_chrec_p (expr))
1954 : : return expr;
1955 : :
1956 : 8787233 : if (TREE_CODE (expr) == POLYNOMIAL_CHREC
1957 : 8786817 : || TREE_CODE (expr) == CALL_EXPR
1958 : 17573982 : || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
1959 : : return chrec_dont_know;
1960 : :
1961 : 8716594 : extract_ops_from_tree (expr, &code, &op0, &op1);
1962 : :
1963 : 8716594 : return interpret_rhs_expr (loop, at_stmt, type,
1964 : 8716594 : op0, code, op1);
1965 : : }
1966 : :
1967 : : /* Interpret the rhs of the assignment STMT. */
1968 : :
1969 : : static tree
1970 : 35932492 : interpret_gimple_assign (class loop *loop, gimple *stmt)
1971 : : {
1972 : 35932492 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
1973 : 35932492 : enum tree_code code = gimple_assign_rhs_code (stmt);
1974 : :
1975 : 35932492 : return interpret_rhs_expr (loop, stmt, type,
1976 : : gimple_assign_rhs1 (stmt), code,
1977 : 35932492 : gimple_assign_rhs2 (stmt));
1978 : : }
1979 : :
1980 : :
1981 : :
1982 : : /* This section contains all the entry points:
1983 : : - number_of_iterations_in_loop,
1984 : : - analyze_scalar_evolution,
1985 : : - instantiate_parameters.
1986 : : */
1987 : :
1988 : : /* Helper recursive function. */
1989 : :
1990 : : static tree
1991 : 74917289 : analyze_scalar_evolution_1 (class loop *loop, tree var)
1992 : : {
1993 : 74917289 : gimple *def;
1994 : 74917289 : basic_block bb;
1995 : 74917289 : class loop *def_loop;
1996 : 74917289 : tree res;
1997 : :
1998 : 74917289 : if (TREE_CODE (var) != SSA_NAME)
1999 : 8792189 : return interpret_expr (loop, NULL, var);
2000 : :
2001 : 66125100 : def = SSA_NAME_DEF_STMT (var);
2002 : 66125100 : bb = gimple_bb (def);
2003 : 66125100 : def_loop = bb->loop_father;
2004 : :
2005 : 66125100 : if (!flow_bb_inside_loop_p (loop, bb))
2006 : : {
2007 : : /* Keep symbolic form, but look through obvious copies for constants. */
2008 : 11498988 : res = follow_copies_to_constant (var);
2009 : 11498988 : goto set_and_end;
2010 : : }
2011 : :
2012 : 54626112 : if (loop != def_loop)
2013 : : {
2014 : 4529618 : res = analyze_scalar_evolution_1 (def_loop, var);
2015 : 4529618 : class loop *loop_to_skip = superloop_at_depth (def_loop,
2016 : 4529618 : loop_depth (loop) + 1);
2017 : 4529618 : res = compute_overall_effect_of_inner_loop (loop_to_skip, res);
2018 : 4529618 : if (chrec_contains_symbols_defined_in_loop (res, loop->num))
2019 : 307559 : res = analyze_scalar_evolution_1 (loop, res);
2020 : 4529618 : goto set_and_end;
2021 : : }
2022 : :
2023 : 50096494 : switch (gimple_code (def))
2024 : : {
2025 : 35932492 : case GIMPLE_ASSIGN:
2026 : 35932492 : res = interpret_gimple_assign (loop, def);
2027 : 35932492 : break;
2028 : :
2029 : 13595194 : case GIMPLE_PHI:
2030 : 27190388 : if (loop_phi_node_p (def))
2031 : 10961359 : res = interpret_loop_phi (loop, as_a <gphi *> (def));
2032 : : else
2033 : 2633835 : res = interpret_condition_phi (loop, as_a <gphi *> (def));
2034 : : break;
2035 : :
2036 : 568808 : default:
2037 : 568808 : res = chrec_dont_know;
2038 : 568808 : break;
2039 : : }
2040 : :
2041 : 66125100 : set_and_end:
2042 : :
2043 : : /* Keep the symbolic form. */
2044 : 66125100 : if (res == chrec_dont_know)
2045 : 24614266 : res = var;
2046 : :
2047 : 66125100 : if (loop == def_loop)
2048 : 50096494 : set_scalar_evolution (block_before_loop (loop), var, res);
2049 : :
2050 : : return res;
2051 : : }
2052 : :
2053 : : /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2054 : : LOOP. LOOP is the loop in which the variable is used.
2055 : :
2056 : : Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2057 : : pointer to the statement that uses this variable, in order to
2058 : : determine the evolution function of the variable, use the following
2059 : : calls:
2060 : :
2061 : : loop_p loop = loop_containing_stmt (stmt);
2062 : : tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2063 : : tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2064 : : */
2065 : :
2066 : : tree
2067 : 194065926 : analyze_scalar_evolution (class loop *loop, tree var)
2068 : : {
2069 : 194065926 : tree res;
2070 : :
2071 : : /* ??? Fix callers. */
2072 : 194065926 : if (! loop)
2073 : : return var;
2074 : :
2075 : 193919746 : if (dump_file && (dump_flags & TDF_SCEV))
2076 : : {
2077 : 36 : fprintf (dump_file, "(analyze_scalar_evolution \n");
2078 : 36 : fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2079 : 36 : fprintf (dump_file, " (scalar = ");
2080 : 36 : print_generic_expr (dump_file, var);
2081 : 36 : fprintf (dump_file, ")\n");
2082 : : }
2083 : :
2084 : 193919746 : res = get_scalar_evolution (block_before_loop (loop), var);
2085 : 193919746 : if (res == chrec_not_analyzed_yet)
2086 : : {
2087 : : /* We'll recurse into instantiate_scev, avoid tearing down the
2088 : : instantiate cache repeatedly and keep it live from here. */
2089 : 70080112 : bool destr = false;
2090 : 70080112 : if (!global_cache)
2091 : : {
2092 : 41857879 : global_cache = new instantiate_cache_type;
2093 : 41857879 : destr = true;
2094 : : }
2095 : 70080112 : res = analyze_scalar_evolution_1 (loop, var);
2096 : 70080112 : if (destr)
2097 : : {
2098 : 41857879 : delete global_cache;
2099 : 41857879 : global_cache = NULL;
2100 : : }
2101 : : }
2102 : :
2103 : 193919746 : if (dump_file && (dump_flags & TDF_SCEV))
2104 : 36 : fprintf (dump_file, ")\n");
2105 : :
2106 : : return res;
2107 : : }
2108 : :
2109 : : /* If CHREC doesn't overflow, set the nonwrapping flag. */
2110 : :
2111 : 10577218 : void record_nonwrapping_chrec (tree chrec)
2112 : : {
2113 : 10577218 : CHREC_NOWRAP(chrec) = 1;
2114 : :
2115 : 10577218 : if (dump_file && (dump_flags & TDF_SCEV))
2116 : : {
2117 : 6 : fprintf (dump_file, "(record_nonwrapping_chrec: ");
2118 : 6 : print_generic_expr (dump_file, chrec);
2119 : 6 : fprintf (dump_file, ")\n");
2120 : : }
2121 : 10577218 : }
2122 : :
2123 : : /* Return true if CHREC's nonwrapping flag is set. */
2124 : :
2125 : 240197 : bool nonwrapping_chrec_p (tree chrec)
2126 : : {
2127 : 240197 : if (!chrec || TREE_CODE(chrec) != POLYNOMIAL_CHREC)
2128 : : return false;
2129 : :
2130 : 240197 : return CHREC_NOWRAP(chrec);
2131 : : }
2132 : :
2133 : : /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2134 : :
2135 : : static tree
2136 : 78960 : analyze_scalar_evolution_for_address_of (class loop *loop, tree var)
2137 : : {
2138 : 78960 : return analyze_scalar_evolution (loop, build_fold_addr_expr (var));
2139 : : }
2140 : :
2141 : : /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2142 : : WRTO_LOOP (which should be a superloop of USE_LOOP)
2143 : :
2144 : : FOLDED_CASTS is set to true if resolve_mixers used
2145 : : chrec_convert_aggressive (TODO -- not really, we are way too conservative
2146 : : at the moment in order to keep things simple).
2147 : :
2148 : : To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2149 : : example:
2150 : :
2151 : : for (i = 0; i < 100; i++) -- loop 1
2152 : : {
2153 : : for (j = 0; j < 100; j++) -- loop 2
2154 : : {
2155 : : k1 = i;
2156 : : k2 = j;
2157 : :
2158 : : use2 (k1, k2);
2159 : :
2160 : : for (t = 0; t < 100; t++) -- loop 3
2161 : : use3 (k1, k2);
2162 : :
2163 : : }
2164 : : use1 (k1, k2);
2165 : : }
2166 : :
2167 : : Both k1 and k2 are invariants in loop3, thus
2168 : : analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2169 : : analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2170 : :
2171 : : As they are invariant, it does not matter whether we consider their
2172 : : usage in loop 3 or loop 2, hence
2173 : : analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2174 : : analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2175 : : analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2176 : : analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2177 : :
2178 : : Similarly for their evolutions with respect to loop 1. The values of K2
2179 : : in the use in loop 2 vary independently on loop 1, thus we cannot express
2180 : : the evolution with respect to loop 1:
2181 : : analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2182 : : analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2183 : : analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2184 : : analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2185 : :
2186 : : The value of k2 in the use in loop 1 is known, though:
2187 : : analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2188 : : analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2189 : : */
2190 : :
2191 : : static tree
2192 : 49353793 : analyze_scalar_evolution_in_loop (class loop *wrto_loop, class loop *use_loop,
2193 : : tree version, bool *folded_casts)
2194 : : {
2195 : 49353793 : bool val = false;
2196 : 49353793 : tree ev = version, tmp;
2197 : :
2198 : : /* We cannot just do
2199 : :
2200 : : tmp = analyze_scalar_evolution (use_loop, version);
2201 : : ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2202 : :
2203 : : as resolve_mixers would query the scalar evolution with respect to
2204 : : wrto_loop. For example, in the situation described in the function
2205 : : comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2206 : : version = k2. Then
2207 : :
2208 : : analyze_scalar_evolution (use_loop, version) = k2
2209 : :
2210 : : and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2211 : : k2 in loop 1 is 100, which is a wrong result, since we are interested
2212 : : in the value in loop 3.
2213 : :
2214 : : Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2215 : : each time checking that there is no evolution in the inner loop. */
2216 : :
2217 : 49353793 : if (folded_casts)
2218 : 49353793 : *folded_casts = false;
2219 : 51689325 : while (1)
2220 : : {
2221 : 50521559 : tmp = analyze_scalar_evolution (use_loop, ev);
2222 : 50521559 : ev = resolve_mixers (use_loop, tmp, folded_casts);
2223 : :
2224 : 50521559 : if (use_loop == wrto_loop)
2225 : : return ev;
2226 : :
2227 : : /* If the value of the use changes in the inner loop, we cannot express
2228 : : its value in the outer loop (we might try to return interval chrec,
2229 : : but we do not have a user for it anyway) */
2230 : 4403522 : if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2231 : 4403522 : || !val)
2232 : 3235756 : return chrec_dont_know;
2233 : :
2234 : 1167766 : use_loop = loop_outer (use_loop);
2235 : : }
2236 : : }
2237 : :
2238 : :
2239 : : /* Computes a hash function for database element ELT. */
2240 : :
2241 : : static inline hashval_t
2242 : 267851 : hash_idx_scev_info (const void *elt_)
2243 : : {
2244 : 267851 : unsigned idx = ((size_t) elt_) - 2;
2245 : 267851 : return scev_info_hasher::hash (&global_cache->entries[idx]);
2246 : : }
2247 : :
2248 : : /* Compares database elements E1 and E2. */
2249 : :
2250 : : static inline int
2251 : 31757629 : eq_idx_scev_info (const void *e1, const void *e2)
2252 : : {
2253 : 31757629 : unsigned idx1 = ((size_t) e1) - 2;
2254 : 31757629 : return scev_info_hasher::equal (&global_cache->entries[idx1],
2255 : 31757629 : (const scev_info_str *) e2);
2256 : : }
2257 : :
2258 : : /* Returns from CACHE the slot number of the cached chrec for NAME. */
2259 : :
2260 : : static unsigned
2261 : 63806469 : get_instantiated_value_entry (instantiate_cache_type &cache,
2262 : : tree name, edge instantiate_below)
2263 : : {
2264 : 63806469 : if (!cache.map)
2265 : : {
2266 : 29472291 : cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
2267 : 29472291 : cache.entries.create (10);
2268 : : }
2269 : :
2270 : 63806469 : scev_info_str e;
2271 : 63806469 : e.name_version = SSA_NAME_VERSION (name);
2272 : 63806469 : e.instantiated_below = instantiate_below->dest->index;
2273 : 63806469 : void **slot = htab_find_slot_with_hash (cache.map, &e,
2274 : : scev_info_hasher::hash (&e), INSERT);
2275 : 63806469 : if (!*slot)
2276 : : {
2277 : 33177018 : e.chrec = chrec_not_analyzed_yet;
2278 : 33177018 : *slot = (void *)(size_t)(cache.entries.length () + 2);
2279 : 33177018 : cache.entries.safe_push (e);
2280 : : }
2281 : :
2282 : 63806469 : return ((size_t)*slot) - 2;
2283 : : }
2284 : :
2285 : :
2286 : : /* Return the closed_loop_phi node for VAR. If there is none, return
2287 : : NULL_TREE. */
2288 : :
2289 : : static tree
2290 : 2237228 : loop_closed_phi_def (tree var)
2291 : : {
2292 : 2237228 : class loop *loop;
2293 : 2237228 : edge exit;
2294 : 2237228 : gphi *phi;
2295 : 2237228 : gphi_iterator psi;
2296 : :
2297 : 2237228 : if (var == NULL_TREE
2298 : 2237228 : || TREE_CODE (var) != SSA_NAME)
2299 : : return NULL_TREE;
2300 : :
2301 : 2237228 : loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
2302 : 2237228 : exit = single_exit (loop);
2303 : 2237228 : if (!exit)
2304 : : return NULL_TREE;
2305 : :
2306 : 1664058 : for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2307 : : {
2308 : 1032182 : phi = psi.phi ();
2309 : 1032182 : if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2310 : 267045 : return PHI_RESULT (phi);
2311 : : }
2312 : :
2313 : : return NULL_TREE;
2314 : : }
2315 : :
2316 : : static tree instantiate_scev_r (edge, class loop *, class loop *,
2317 : : tree, bool *, int);
2318 : :
2319 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2320 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2321 : :
2322 : : CHREC is an SSA_NAME to be instantiated.
2323 : :
2324 : : CACHE is the cache of already instantiated values.
2325 : :
2326 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2327 : : conversions that may wrap in signed/pointer type are folded, as long
2328 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2329 : : then we don't do such fold.
2330 : :
2331 : : SIZE_EXPR is used for computing the size of the expression to be
2332 : : instantiated, and to stop if it exceeds some limit. */
2333 : :
2334 : : static tree
2335 : 110930733 : instantiate_scev_name (edge instantiate_below,
2336 : : class loop *evolution_loop, class loop *inner_loop,
2337 : : tree chrec,
2338 : : bool *fold_conversions,
2339 : : int size_expr)
2340 : : {
2341 : 110930733 : tree res;
2342 : 110930733 : class loop *def_loop;
2343 : 110930733 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
2344 : :
2345 : : /* A parameter, nothing to do. */
2346 : 110930733 : if (!def_bb
2347 : 110930733 : || !dominated_by_p (CDI_DOMINATORS, def_bb, instantiate_below->dest))
2348 : 47124264 : return chrec;
2349 : :
2350 : : /* We cache the value of instantiated variable to avoid exponential
2351 : : time complexity due to reevaluations. We also store the convenient
2352 : : value in the cache in order to prevent infinite recursion -- we do
2353 : : not want to instantiate the SSA_NAME if it is in a mixer
2354 : : structure. This is used for avoiding the instantiation of
2355 : : recursively defined functions, such as:
2356 : :
2357 : : | a_2 -> {0, +, 1, +, a_2}_1 */
2358 : :
2359 : 63806469 : unsigned si = get_instantiated_value_entry (*global_cache,
2360 : : chrec, instantiate_below);
2361 : 63806469 : if (global_cache->get (si) != chrec_not_analyzed_yet)
2362 : : return global_cache->get (si);
2363 : :
2364 : : /* On recursion return chrec_dont_know. */
2365 : 33177018 : global_cache->set (si, chrec_dont_know);
2366 : :
2367 : 33177018 : def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
2368 : :
2369 : 33177018 : if (! dominated_by_p (CDI_DOMINATORS,
2370 : 33177018 : def_loop->header, instantiate_below->dest))
2371 : : {
2372 : 187802 : gimple *def = SSA_NAME_DEF_STMT (chrec);
2373 : 187802 : if (gassign *ass = dyn_cast <gassign *> (def))
2374 : : {
2375 : 136977 : switch (gimple_assign_rhs_class (ass))
2376 : : {
2377 : 5593 : case GIMPLE_UNARY_RHS:
2378 : 5593 : {
2379 : 5593 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2380 : : inner_loop, gimple_assign_rhs1 (ass),
2381 : : fold_conversions, size_expr);
2382 : 5593 : if (op0 == chrec_dont_know)
2383 : : return chrec_dont_know;
2384 : 1394 : res = fold_build1 (gimple_assign_rhs_code (ass),
2385 : : TREE_TYPE (chrec), op0);
2386 : 1394 : break;
2387 : : }
2388 : 54163 : case GIMPLE_BINARY_RHS:
2389 : 54163 : {
2390 : 54163 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2391 : : inner_loop, gimple_assign_rhs1 (ass),
2392 : : fold_conversions, size_expr);
2393 : 54163 : if (op0 == chrec_dont_know)
2394 : : return chrec_dont_know;
2395 : 12092 : tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2396 : : inner_loop, gimple_assign_rhs2 (ass),
2397 : : fold_conversions, size_expr);
2398 : 6046 : if (op1 == chrec_dont_know)
2399 : : return chrec_dont_know;
2400 : 2285 : res = fold_build2 (gimple_assign_rhs_code (ass),
2401 : : TREE_TYPE (chrec), op0, op1);
2402 : 2285 : break;
2403 : : }
2404 : 77221 : default:
2405 : 77221 : res = chrec_dont_know;
2406 : : }
2407 : : }
2408 : : else
2409 : 50825 : res = chrec_dont_know;
2410 : 131725 : global_cache->set (si, res);
2411 : 131725 : return res;
2412 : : }
2413 : :
2414 : : /* If the analysis yields a parametric chrec, instantiate the
2415 : : result again. */
2416 : 32989216 : res = analyze_scalar_evolution (def_loop, chrec);
2417 : :
2418 : : /* Don't instantiate default definitions. */
2419 : 32989216 : if (TREE_CODE (res) == SSA_NAME
2420 : 32989216 : && SSA_NAME_IS_DEFAULT_DEF (res))
2421 : : ;
2422 : :
2423 : : /* Don't instantiate loop-closed-ssa phi nodes. */
2424 : 32968396 : else if (TREE_CODE (res) == SSA_NAME
2425 : 97166460 : && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2426 : 32099408 : > loop_depth (def_loop))
2427 : : {
2428 : 2259676 : if (res == chrec)
2429 : 2237228 : res = loop_closed_phi_def (chrec);
2430 : : else
2431 : : res = chrec;
2432 : :
2433 : : /* When there is no loop_closed_phi_def, it means that the
2434 : : variable is not used after the loop: try to still compute the
2435 : : value of the variable when exiting the loop. */
2436 : 2259676 : if (res == NULL_TREE)
2437 : : {
2438 : 1970183 : loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2439 : 1970183 : res = analyze_scalar_evolution (loop, chrec);
2440 : 1970183 : res = compute_overall_effect_of_inner_loop (loop, res);
2441 : 1970183 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2442 : : inner_loop, res,
2443 : : fold_conversions, size_expr);
2444 : : }
2445 : 289493 : else if (dominated_by_p (CDI_DOMINATORS,
2446 : 289493 : gimple_bb (SSA_NAME_DEF_STMT (res)),
2447 : 289493 : instantiate_below->dest))
2448 : 289493 : res = chrec_dont_know;
2449 : : }
2450 : :
2451 : 30708720 : else if (res != chrec_dont_know)
2452 : : {
2453 : 30708720 : if (inner_loop
2454 : 1285993 : && def_bb->loop_father != inner_loop
2455 : 31314663 : && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
2456 : : /* ??? We could try to compute the overall effect of the loop here. */
2457 : 327 : res = chrec_dont_know;
2458 : : else
2459 : 30708393 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2460 : : inner_loop, res,
2461 : : fold_conversions, size_expr);
2462 : : }
2463 : :
2464 : : /* Store the correct value to the cache. */
2465 : 32989216 : global_cache->set (si, res);
2466 : 32989216 : return res;
2467 : : }
2468 : :
2469 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2470 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2471 : :
2472 : : CHREC is a polynomial chain of recurrence to be instantiated.
2473 : :
2474 : : CACHE is the cache of already instantiated values.
2475 : :
2476 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2477 : : conversions that may wrap in signed/pointer type are folded, as long
2478 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2479 : : then we don't do such fold.
2480 : :
2481 : : SIZE_EXPR is used for computing the size of the expression to be
2482 : : instantiated, and to stop if it exceeds some limit. */
2483 : :
2484 : : static tree
2485 : 60203796 : instantiate_scev_poly (edge instantiate_below,
2486 : : class loop *evolution_loop, class loop *,
2487 : : tree chrec, bool *fold_conversions, int size_expr)
2488 : : {
2489 : 60203796 : tree op1;
2490 : 120407592 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2491 : : get_chrec_loop (chrec),
2492 : 60203796 : CHREC_LEFT (chrec), fold_conversions,
2493 : : size_expr);
2494 : 60203796 : if (op0 == chrec_dont_know)
2495 : : return chrec_dont_know;
2496 : :
2497 : 119990112 : op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2498 : : get_chrec_loop (chrec),
2499 : 59995056 : CHREC_RIGHT (chrec), fold_conversions,
2500 : : size_expr);
2501 : 59995056 : if (op1 == chrec_dont_know)
2502 : : return chrec_dont_know;
2503 : :
2504 : 59201724 : if (CHREC_LEFT (chrec) != op0
2505 : 59201724 : || CHREC_RIGHT (chrec) != op1)
2506 : : {
2507 : 7832803 : op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
2508 : 7832803 : chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
2509 : : }
2510 : :
2511 : : return chrec;
2512 : : }
2513 : :
2514 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2515 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2516 : :
2517 : : "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2518 : :
2519 : : CACHE is the cache of already instantiated values.
2520 : :
2521 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2522 : : conversions that may wrap in signed/pointer type are folded, as long
2523 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2524 : : then we don't do such fold.
2525 : :
2526 : : SIZE_EXPR is used for computing the size of the expression to be
2527 : : instantiated, and to stop if it exceeds some limit. */
2528 : :
2529 : : static tree
2530 : 24032874 : instantiate_scev_binary (edge instantiate_below,
2531 : : class loop *evolution_loop, class loop *inner_loop,
2532 : : tree chrec, enum tree_code code,
2533 : : tree type, tree c0, tree c1,
2534 : : bool *fold_conversions, int size_expr)
2535 : : {
2536 : 24032874 : tree op1;
2537 : 24032874 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2538 : : c0, fold_conversions, size_expr);
2539 : 24032874 : if (op0 == chrec_dont_know)
2540 : : return chrec_dont_know;
2541 : :
2542 : : /* While we eventually compute the same op1 if c0 == c1 the process
2543 : : of doing this is expensive so the following short-cut prevents
2544 : : exponential compile-time behavior. */
2545 : 23668889 : if (c0 != c1)
2546 : : {
2547 : 23646674 : op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2548 : : c1, fold_conversions, size_expr);
2549 : 23646674 : if (op1 == chrec_dont_know)
2550 : : return chrec_dont_know;
2551 : : }
2552 : : else
2553 : : op1 = op0;
2554 : :
2555 : 23603013 : if (c0 != op0
2556 : 23603013 : || c1 != op1)
2557 : : {
2558 : 13552940 : op0 = chrec_convert (type, op0, NULL);
2559 : 13552940 : op1 = chrec_convert_rhs (type, op1, NULL);
2560 : :
2561 : 13552940 : switch (code)
2562 : : {
2563 : 7919645 : case POINTER_PLUS_EXPR:
2564 : 7919645 : case PLUS_EXPR:
2565 : 7919645 : return chrec_fold_plus (type, op0, op1);
2566 : :
2567 : 862587 : case MINUS_EXPR:
2568 : 862587 : return chrec_fold_minus (type, op0, op1);
2569 : :
2570 : 4770708 : case MULT_EXPR:
2571 : 4770708 : return chrec_fold_multiply (type, op0, op1);
2572 : :
2573 : 0 : default:
2574 : 0 : gcc_unreachable ();
2575 : : }
2576 : : }
2577 : :
2578 : 10050073 : return chrec ? chrec : fold_build2 (code, type, c0, c1);
2579 : : }
2580 : :
2581 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2582 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2583 : :
2584 : : "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2585 : : instantiated.
2586 : :
2587 : : CACHE is the cache of already instantiated values.
2588 : :
2589 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2590 : : conversions that may wrap in signed/pointer type are folded, as long
2591 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2592 : : then we don't do such fold.
2593 : :
2594 : : SIZE_EXPR is used for computing the size of the expression to be
2595 : : instantiated, and to stop if it exceeds some limit. */
2596 : :
2597 : : static tree
2598 : 24016662 : instantiate_scev_convert (edge instantiate_below,
2599 : : class loop *evolution_loop, class loop *inner_loop,
2600 : : tree chrec, tree type, tree op,
2601 : : bool *fold_conversions, int size_expr)
2602 : : {
2603 : 24016662 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2604 : : inner_loop, op,
2605 : : fold_conversions, size_expr);
2606 : :
2607 : 24016662 : if (op0 == chrec_dont_know)
2608 : : return chrec_dont_know;
2609 : :
2610 : 19021090 : if (fold_conversions)
2611 : : {
2612 : 7487641 : tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
2613 : 7487641 : if (tmp)
2614 : : return tmp;
2615 : :
2616 : : /* If we used chrec_convert_aggressive, we can no longer assume that
2617 : : signed chrecs do not overflow, as chrec_convert does, so avoid
2618 : : calling it in that case. */
2619 : 6983846 : if (*fold_conversions)
2620 : : {
2621 : 7529 : if (chrec && op0 == op)
2622 : : return chrec;
2623 : :
2624 : 7529 : return fold_convert (type, op0);
2625 : : }
2626 : : }
2627 : :
2628 : 18509766 : return chrec_convert (type, op0, NULL);
2629 : : }
2630 : :
2631 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2632 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2633 : :
2634 : : CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2635 : : Handle ~X as -1 - X.
2636 : : Handle -X as -1 * X.
2637 : :
2638 : : CACHE is the cache of already instantiated values.
2639 : :
2640 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2641 : : conversions that may wrap in signed/pointer type are folded, as long
2642 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2643 : : then we don't do such fold.
2644 : :
2645 : : SIZE_EXPR is used for computing the size of the expression to be
2646 : : instantiated, and to stop if it exceeds some limit. */
2647 : :
2648 : : static tree
2649 : 345092 : instantiate_scev_not (edge instantiate_below,
2650 : : class loop *evolution_loop, class loop *inner_loop,
2651 : : tree chrec,
2652 : : enum tree_code code, tree type, tree op,
2653 : : bool *fold_conversions, int size_expr)
2654 : : {
2655 : 345092 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2656 : : inner_loop, op,
2657 : : fold_conversions, size_expr);
2658 : :
2659 : 345092 : if (op0 == chrec_dont_know)
2660 : : return chrec_dont_know;
2661 : :
2662 : 270447 : if (op != op0)
2663 : : {
2664 : 36020 : op0 = chrec_convert (type, op0, NULL);
2665 : :
2666 : 36020 : switch (code)
2667 : : {
2668 : 1699 : case BIT_NOT_EXPR:
2669 : 1699 : return chrec_fold_minus
2670 : 1699 : (type, fold_convert (type, integer_minus_one_node), op0);
2671 : :
2672 : 34321 : case NEGATE_EXPR:
2673 : 34321 : return chrec_fold_multiply
2674 : 34321 : (type, fold_convert (type, integer_minus_one_node), op0);
2675 : :
2676 : 0 : default:
2677 : 0 : gcc_unreachable ();
2678 : : }
2679 : : }
2680 : :
2681 : 234427 : return chrec ? chrec : fold_build1 (code, type, op0);
2682 : : }
2683 : :
2684 : : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2685 : : and EVOLUTION_LOOP, that were left under a symbolic form.
2686 : :
2687 : : CHREC is the scalar evolution to instantiate.
2688 : :
2689 : : CACHE is the cache of already instantiated values.
2690 : :
2691 : : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2692 : : conversions that may wrap in signed/pointer type are folded, as long
2693 : : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2694 : : then we don't do such fold.
2695 : :
2696 : : SIZE_EXPR is used for computing the size of the expression to be
2697 : : instantiated, and to stop if it exceeds some limit. */
2698 : :
2699 : : static tree
2700 : 362492026 : instantiate_scev_r (edge instantiate_below,
2701 : : class loop *evolution_loop, class loop *inner_loop,
2702 : : tree chrec,
2703 : : bool *fold_conversions, int size_expr)
2704 : : {
2705 : : /* Give up if the expression is larger than the MAX that we allow. */
2706 : 362492026 : if (size_expr++ > param_scev_max_expr_size)
2707 : 11 : return chrec_dont_know;
2708 : :
2709 : 362492015 : if (chrec == NULL_TREE
2710 : 502434571 : || automatically_generated_chrec_p (chrec)
2711 : 722643481 : || is_gimple_min_invariant (chrec))
2712 : 142283105 : return chrec;
2713 : :
2714 : 220208910 : switch (TREE_CODE (chrec))
2715 : : {
2716 : 110930733 : case SSA_NAME:
2717 : 110930733 : return instantiate_scev_name (instantiate_below, evolution_loop,
2718 : : inner_loop, chrec,
2719 : 110930733 : fold_conversions, size_expr);
2720 : :
2721 : 60203796 : case POLYNOMIAL_CHREC:
2722 : 60203796 : return instantiate_scev_poly (instantiate_below, evolution_loop,
2723 : : inner_loop, chrec,
2724 : 60203796 : fold_conversions, size_expr);
2725 : :
2726 : 24032874 : case POINTER_PLUS_EXPR:
2727 : 24032874 : case PLUS_EXPR:
2728 : 24032874 : case MINUS_EXPR:
2729 : 24032874 : case MULT_EXPR:
2730 : 24032874 : return instantiate_scev_binary (instantiate_below, evolution_loop,
2731 : : inner_loop, chrec,
2732 : : TREE_CODE (chrec), chrec_type (chrec),
2733 : 24032874 : TREE_OPERAND (chrec, 0),
2734 : 24032874 : TREE_OPERAND (chrec, 1),
2735 : 24032874 : fold_conversions, size_expr);
2736 : :
2737 : 24016662 : CASE_CONVERT:
2738 : 24016662 : return instantiate_scev_convert (instantiate_below, evolution_loop,
2739 : : inner_loop, chrec,
2740 : 24016662 : TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
2741 : 24016662 : fold_conversions, size_expr);
2742 : :
2743 : 345092 : case NEGATE_EXPR:
2744 : 345092 : case BIT_NOT_EXPR:
2745 : 345092 : return instantiate_scev_not (instantiate_below, evolution_loop,
2746 : : inner_loop, chrec,
2747 : 345092 : TREE_CODE (chrec), TREE_TYPE (chrec),
2748 : 345092 : TREE_OPERAND (chrec, 0),
2749 : 345092 : fold_conversions, size_expr);
2750 : :
2751 : 0 : case ADDR_EXPR:
2752 : 0 : if (is_gimple_min_invariant (chrec))
2753 : : return chrec;
2754 : : /* Fallthru. */
2755 : 0 : case SCEV_NOT_KNOWN:
2756 : 0 : return chrec_dont_know;
2757 : :
2758 : 0 : case SCEV_KNOWN:
2759 : 0 : return chrec_known;
2760 : :
2761 : 679753 : default:
2762 : 679753 : if (CONSTANT_CLASS_P (chrec))
2763 : : return chrec;
2764 : 679753 : return chrec_dont_know;
2765 : : }
2766 : : }
2767 : :
2768 : : /* Analyze all the parameters of the chrec that were left under a
2769 : : symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2770 : : recursive instantiation of parameters: a parameter is a variable
2771 : : that is defined in a basic block that dominates INSTANTIATE_BELOW or
2772 : : a function parameter. */
2773 : :
2774 : : tree
2775 : 86985935 : instantiate_scev (edge instantiate_below, class loop *evolution_loop,
2776 : : tree chrec)
2777 : : {
2778 : 86985935 : tree res;
2779 : :
2780 : 86985935 : if (dump_file && (dump_flags & TDF_SCEV))
2781 : : {
2782 : 20 : fprintf (dump_file, "(instantiate_scev \n");
2783 : 20 : fprintf (dump_file, " (instantiate_below = %d -> %d)\n",
2784 : 20 : instantiate_below->src->index, instantiate_below->dest->index);
2785 : 20 : if (evolution_loop)
2786 : 20 : fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
2787 : 20 : fprintf (dump_file, " (chrec = ");
2788 : 20 : print_generic_expr (dump_file, chrec);
2789 : 20 : fprintf (dump_file, ")\n");
2790 : : }
2791 : :
2792 : 86985935 : bool destr = false;
2793 : 86985935 : if (!global_cache)
2794 : : {
2795 : 37849855 : global_cache = new instantiate_cache_type;
2796 : 37849855 : destr = true;
2797 : : }
2798 : :
2799 : 86985935 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2800 : : NULL, chrec, NULL, 0);
2801 : :
2802 : 86985935 : if (destr)
2803 : : {
2804 : 37849855 : delete global_cache;
2805 : 37849855 : global_cache = NULL;
2806 : : }
2807 : :
2808 : 86985935 : if (dump_file && (dump_flags & TDF_SCEV))
2809 : : {
2810 : 20 : fprintf (dump_file, " (res = ");
2811 : 20 : print_generic_expr (dump_file, res);
2812 : 20 : fprintf (dump_file, "))\n");
2813 : : }
2814 : :
2815 : 86985935 : return res;
2816 : : }
2817 : :
2818 : : /* Similar to instantiate_parameters, but does not introduce the
2819 : : evolutions in outer loops for LOOP invariants in CHREC, and does not
2820 : : care about causing overflows, as long as they do not affect value
2821 : : of an expression. */
2822 : :
2823 : : tree
2824 : 50521559 : resolve_mixers (class loop *loop, tree chrec, bool *folded_casts)
2825 : : {
2826 : 50521559 : bool destr = false;
2827 : 50521559 : bool fold_conversions = false;
2828 : 50521559 : if (!global_cache)
2829 : : {
2830 : 49818735 : global_cache = new instantiate_cache_type;
2831 : 49818735 : destr = true;
2832 : : }
2833 : :
2834 : 50521559 : tree ret = instantiate_scev_r (loop_preheader_edge (loop), loop, NULL,
2835 : : chrec, &fold_conversions, 0);
2836 : :
2837 : 50521559 : if (folded_casts && !*folded_casts)
2838 : 50521559 : *folded_casts = fold_conversions;
2839 : :
2840 : 50521559 : if (destr)
2841 : : {
2842 : 49818735 : delete global_cache;
2843 : 49818735 : global_cache = NULL;
2844 : : }
2845 : :
2846 : 50521559 : return ret;
2847 : : }
2848 : :
2849 : : /* Entry point for the analysis of the number of iterations pass.
2850 : : This function tries to safely approximate the number of iterations
2851 : : the loop will run. When this property is not decidable at compile
2852 : : time, the result is chrec_dont_know. Otherwise the result is a
2853 : : scalar or a symbolic parameter. When the number of iterations may
2854 : : be equal to zero and the property cannot be determined at compile
2855 : : time, the result is a COND_EXPR that represents in a symbolic form
2856 : : the conditions under which the number of iterations is not zero.
2857 : :
2858 : : Example of analysis: suppose that the loop has an exit condition:
2859 : :
2860 : : "if (b > 49) goto end_loop;"
2861 : :
2862 : : and that in a previous analysis we have determined that the
2863 : : variable 'b' has an evolution function:
2864 : :
2865 : : "EF = {23, +, 5}_2".
2866 : :
2867 : : When we evaluate the function at the point 5, i.e. the value of the
2868 : : variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2869 : : and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2870 : : the loop body has been executed 6 times. */
2871 : :
2872 : : tree
2873 : 10692826 : number_of_latch_executions (class loop *loop)
2874 : : {
2875 : 10692826 : edge exit;
2876 : 10692826 : class tree_niter_desc niter_desc;
2877 : 10692826 : tree may_be_zero;
2878 : 10692826 : tree res;
2879 : :
2880 : : /* Determine whether the number of iterations in loop has already
2881 : : been computed. */
2882 : 10692826 : res = loop->nb_iterations;
2883 : 10692826 : if (res)
2884 : : return res;
2885 : :
2886 : 7002062 : may_be_zero = NULL_TREE;
2887 : :
2888 : 7002062 : if (dump_file && (dump_flags & TDF_SCEV))
2889 : 1 : fprintf (dump_file, "(number_of_iterations_in_loop = \n");
2890 : :
2891 : 7002062 : res = chrec_dont_know;
2892 : 7002062 : exit = single_exit (loop);
2893 : :
2894 : 7002062 : if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
2895 : : {
2896 : 3469534 : may_be_zero = niter_desc.may_be_zero;
2897 : 3469534 : res = niter_desc.niter;
2898 : : }
2899 : :
2900 : 7002062 : if (res == chrec_dont_know
2901 : 3469534 : || !may_be_zero
2902 : 10471596 : || integer_zerop (may_be_zero))
2903 : : ;
2904 : 528282 : else if (integer_nonzerop (may_be_zero))
2905 : 99 : res = build_int_cst (TREE_TYPE (res), 0);
2906 : :
2907 : 528183 : else if (COMPARISON_CLASS_P (may_be_zero))
2908 : 528183 : res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
2909 : : build_int_cst (TREE_TYPE (res), 0), res);
2910 : : else
2911 : 0 : res = chrec_dont_know;
2912 : :
2913 : 7002062 : if (dump_file && (dump_flags & TDF_SCEV))
2914 : : {
2915 : 1 : fprintf (dump_file, " (set_nb_iterations_in_loop = ");
2916 : 1 : print_generic_expr (dump_file, res);
2917 : 1 : fprintf (dump_file, "))\n");
2918 : : }
2919 : :
2920 : 7002062 : loop->nb_iterations = res;
2921 : 7002062 : return res;
2922 : 10692826 : }
2923 : :
2924 : :
2925 : : /* Counters for the stats. */
2926 : :
2927 : : struct chrec_stats
2928 : : {
2929 : : unsigned nb_chrecs;
2930 : : unsigned nb_affine;
2931 : : unsigned nb_affine_multivar;
2932 : : unsigned nb_higher_poly;
2933 : : unsigned nb_chrec_dont_know;
2934 : : unsigned nb_undetermined;
2935 : : };
2936 : :
2937 : : /* Reset the counters. */
2938 : :
2939 : : static inline void
2940 : 0 : reset_chrecs_counters (struct chrec_stats *stats)
2941 : : {
2942 : 0 : stats->nb_chrecs = 0;
2943 : 0 : stats->nb_affine = 0;
2944 : 0 : stats->nb_affine_multivar = 0;
2945 : 0 : stats->nb_higher_poly = 0;
2946 : 0 : stats->nb_chrec_dont_know = 0;
2947 : 0 : stats->nb_undetermined = 0;
2948 : : }
2949 : :
2950 : : /* Dump the contents of a CHREC_STATS structure. */
2951 : :
2952 : : static void
2953 : 0 : dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
2954 : : {
2955 : 0 : fprintf (file, "\n(\n");
2956 : 0 : fprintf (file, "-----------------------------------------\n");
2957 : 0 : fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
2958 : 0 : fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
2959 : 0 : fprintf (file, "%d\tdegree greater than 2 polynomials\n",
2960 : : stats->nb_higher_poly);
2961 : 0 : fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
2962 : 0 : fprintf (file, "-----------------------------------------\n");
2963 : 0 : fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
2964 : 0 : fprintf (file, "%d\twith undetermined coefficients\n",
2965 : : stats->nb_undetermined);
2966 : 0 : fprintf (file, "-----------------------------------------\n");
2967 : 0 : fprintf (file, "%d\tchrecs in the scev database\n",
2968 : 0 : (int) scalar_evolution_info->elements ());
2969 : 0 : fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
2970 : 0 : fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
2971 : 0 : fprintf (file, "-----------------------------------------\n");
2972 : 0 : fprintf (file, ")\n\n");
2973 : 0 : }
2974 : :
2975 : : /* Gather statistics about CHREC. */
2976 : :
2977 : : static void
2978 : 0 : gather_chrec_stats (tree chrec, struct chrec_stats *stats)
2979 : : {
2980 : 0 : if (dump_file && (dump_flags & TDF_STATS))
2981 : : {
2982 : 0 : fprintf (dump_file, "(classify_chrec ");
2983 : 0 : print_generic_expr (dump_file, chrec);
2984 : 0 : fprintf (dump_file, "\n");
2985 : : }
2986 : :
2987 : 0 : stats->nb_chrecs++;
2988 : :
2989 : 0 : if (chrec == NULL_TREE)
2990 : : {
2991 : 0 : stats->nb_undetermined++;
2992 : 0 : return;
2993 : : }
2994 : :
2995 : 0 : switch (TREE_CODE (chrec))
2996 : : {
2997 : 0 : case POLYNOMIAL_CHREC:
2998 : 0 : if (evolution_function_is_affine_p (chrec))
2999 : : {
3000 : 0 : if (dump_file && (dump_flags & TDF_STATS))
3001 : 0 : fprintf (dump_file, " affine_univariate\n");
3002 : 0 : stats->nb_affine++;
3003 : : }
3004 : 0 : else if (evolution_function_is_affine_multivariate_p (chrec, 0))
3005 : : {
3006 : 0 : if (dump_file && (dump_flags & TDF_STATS))
3007 : 0 : fprintf (dump_file, " affine_multivariate\n");
3008 : 0 : stats->nb_affine_multivar++;
3009 : : }
3010 : : else
3011 : : {
3012 : 0 : if (dump_file && (dump_flags & TDF_STATS))
3013 : 0 : fprintf (dump_file, " higher_degree_polynomial\n");
3014 : 0 : stats->nb_higher_poly++;
3015 : : }
3016 : :
3017 : : break;
3018 : :
3019 : : default:
3020 : : break;
3021 : : }
3022 : :
3023 : 0 : if (chrec_contains_undetermined (chrec))
3024 : : {
3025 : 0 : if (dump_file && (dump_flags & TDF_STATS))
3026 : 0 : fprintf (dump_file, " undetermined\n");
3027 : 0 : stats->nb_undetermined++;
3028 : : }
3029 : :
3030 : 0 : if (dump_file && (dump_flags & TDF_STATS))
3031 : 0 : fprintf (dump_file, ")\n");
3032 : : }
3033 : :
3034 : : /* Classify the chrecs of the whole database. */
3035 : :
3036 : : void
3037 : 0 : gather_stats_on_scev_database (void)
3038 : : {
3039 : 0 : struct chrec_stats stats;
3040 : :
3041 : 0 : if (!dump_file)
3042 : 0 : return;
3043 : :
3044 : 0 : reset_chrecs_counters (&stats);
3045 : :
3046 : 0 : hash_table<scev_info_hasher>::iterator iter;
3047 : 0 : scev_info_str *elt;
3048 : 0 : FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
3049 : : iter)
3050 : 0 : gather_chrec_stats (elt->chrec, &stats);
3051 : :
3052 : 0 : dump_chrecs_stats (dump_file, &stats);
3053 : : }
3054 : :
3055 : :
3056 : : /* Initialize the analysis of scalar evolutions for LOOPS. */
3057 : :
3058 : : void
3059 : 15070657 : scev_initialize (void)
3060 : : {
3061 : 15070657 : gcc_assert (! scev_initialized_p ()
3062 : : && loops_state_satisfies_p (cfun, LOOPS_NORMAL));
3063 : :
3064 : 15070657 : scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
3065 : :
3066 : 54603143 : for (auto loop : loops_list (cfun, 0))
3067 : 9391172 : loop->nb_iterations = NULL_TREE;
3068 : 15070657 : }
3069 : :
3070 : : /* Return true if SCEV is initialized. */
3071 : :
3072 : : bool
3073 : 102101905 : scev_initialized_p (void)
3074 : : {
3075 : 102101905 : return scalar_evolution_info != NULL;
3076 : : }
3077 : :
3078 : : /* Cleans up the information cached by the scalar evolutions analysis
3079 : : in the hash table. */
3080 : :
3081 : : void
3082 : 24283700 : scev_reset_htab (void)
3083 : : {
3084 : 24283700 : if (!scalar_evolution_info)
3085 : : return;
3086 : :
3087 : 6177008 : scalar_evolution_info->empty ();
3088 : : }
3089 : :
3090 : : /* Cleans up the information cached by the scalar evolutions analysis
3091 : : in the hash table and in the loop->nb_iterations. */
3092 : :
3093 : : void
3094 : 13052670 : scev_reset (void)
3095 : : {
3096 : 13052670 : scev_reset_htab ();
3097 : :
3098 : 63995034 : for (auto loop : loops_list (cfun, 0))
3099 : 24837024 : loop->nb_iterations = NULL_TREE;
3100 : 13052670 : }
3101 : :
3102 : : /* Return true if the IV calculation in TYPE can overflow based on the knowledge
3103 : : of the upper bound on the number of iterations of LOOP, the BASE and STEP
3104 : : of IV.
3105 : :
3106 : : We do not use information whether TYPE can overflow so it is safe to
3107 : : use this test even for derived IVs not computed every iteration or
3108 : : hypotetical IVs to be inserted into code. */
3109 : :
3110 : : bool
3111 : 15032545 : iv_can_overflow_p (class loop *loop, tree type, tree base, tree step)
3112 : : {
3113 : 15032545 : widest_int nit;
3114 : 15032545 : wide_int base_min, base_max, step_min, step_max, type_min, type_max;
3115 : 15032545 : signop sgn = TYPE_SIGN (type);
3116 : 15032545 : int_range_max r;
3117 : :
3118 : 15032545 : if (integer_zerop (step))
3119 : : return false;
3120 : :
3121 : 30064490 : if (!INTEGRAL_TYPE_P (TREE_TYPE (base))
3122 : 27962574 : || !get_range_query (cfun)->range_of_expr (r, base)
3123 : 13981287 : || r.varying_p ()
3124 : 27368185 : || r.undefined_p ())
3125 : 2699148 : return true;
3126 : :
3127 : 12333390 : base_min = r.lower_bound ();
3128 : 12333390 : base_max = r.upper_bound ();
3129 : :
3130 : 24666221 : if (!INTEGRAL_TYPE_P (TREE_TYPE (step))
3131 : 24666780 : || !get_range_query (cfun)->range_of_expr (r, step)
3132 : 12333390 : || r.varying_p ()
3133 : 24587621 : || r.undefined_p ())
3134 : 79159 : return true;
3135 : :
3136 : 12254231 : step_min = r.lower_bound ();
3137 : 12254231 : step_max = r.upper_bound ();
3138 : :
3139 : 12254231 : if (!get_max_loop_iterations (loop, &nit))
3140 : : return true;
3141 : :
3142 : 11540660 : type_min = wi::min_value (type);
3143 : 11540660 : type_max = wi::max_value (type);
3144 : :
3145 : : /* Just sanity check that we don't see values out of the range of the type.
3146 : : In this case the arithmetics below would overflow. */
3147 : 11540660 : gcc_checking_assert (wi::ge_p (base_min, type_min, sgn)
3148 : : && wi::le_p (base_max, type_max, sgn));
3149 : :
3150 : : /* Account the possible increment in the last ieration. */
3151 : 11540660 : wi::overflow_type overflow = wi::OVF_NONE;
3152 : 11540660 : nit = wi::add (nit, 1, SIGNED, &overflow);
3153 : 11540660 : if (overflow)
3154 : : return true;
3155 : :
3156 : : /* NIT is typeless and can exceed the precision of the type. In this case
3157 : : overflow is always possible, because we know STEP is non-zero. */
3158 : 11540660 : if (wi::min_precision (nit, UNSIGNED) > TYPE_PRECISION (type))
3159 : : return true;
3160 : 11297848 : wide_int nit2 = wide_int::from (nit, TYPE_PRECISION (type), UNSIGNED);
3161 : :
3162 : : /* If step can be positive, check that nit*step <= type_max-base.
3163 : : This can be done by unsigned arithmetic and we only need to watch overflow
3164 : : in the multiplication. The right hand side can always be represented in
3165 : : the type. */
3166 : 11297848 : if (sgn == UNSIGNED || !wi::neg_p (step_max))
3167 : : {
3168 : 11253911 : wi::overflow_type overflow = wi::OVF_NONE;
3169 : 11253911 : if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow),
3170 : 22507822 : type_max - base_max)
3171 : 22507822 : || overflow)
3172 : 5717364 : return true;
3173 : : }
3174 : : /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3175 : 5580484 : if (sgn == SIGNED && wi::neg_p (step_min))
3176 : : {
3177 : 44264 : wi::overflow_type overflow, overflow2;
3178 : 44264 : overflow = overflow2 = wi::OVF_NONE;
3179 : 88528 : if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2),
3180 : : nit2, UNSIGNED, &overflow),
3181 : 88528 : base_min - type_min)
3182 : 88528 : || overflow || overflow2)
3183 : 16769 : return true;
3184 : : }
3185 : :
3186 : : return false;
3187 : 15032545 : }
3188 : :
3189 : : /* Given EV with form of "(type) {inner_base, inner_step}_loop", this
3190 : : function tries to derive condition under which it can be simplified
3191 : : into "{(type)inner_base, (type)inner_step}_loop". The condition is
3192 : : the maximum number that inner iv can iterate. */
3193 : :
3194 : : static tree
3195 : 38743 : derive_simple_iv_with_niters (tree ev, tree *niters)
3196 : : {
3197 : 38743 : if (!CONVERT_EXPR_P (ev))
3198 : : return ev;
3199 : :
3200 : 38743 : tree inner_ev = TREE_OPERAND (ev, 0);
3201 : 38743 : if (TREE_CODE (inner_ev) != POLYNOMIAL_CHREC)
3202 : : return ev;
3203 : :
3204 : 38743 : tree init = CHREC_LEFT (inner_ev);
3205 : 38743 : tree step = CHREC_RIGHT (inner_ev);
3206 : 38743 : if (TREE_CODE (init) != INTEGER_CST
3207 : 38743 : || TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
3208 : 7295 : return ev;
3209 : :
3210 : 31448 : tree type = TREE_TYPE (ev);
3211 : 31448 : tree inner_type = TREE_TYPE (inner_ev);
3212 : 31448 : if (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (type))
3213 : : return ev;
3214 : :
3215 : : /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
3216 : : folded only if inner iv won't overflow. We compute the maximum
3217 : : number the inner iv can iterate before overflowing and return the
3218 : : simplified affine iv. */
3219 : 31448 : tree delta;
3220 : 31448 : init = fold_convert (type, init);
3221 : 31448 : step = fold_convert (type, step);
3222 : 31448 : ev = build_polynomial_chrec (CHREC_VARIABLE (inner_ev), init, step);
3223 : 31448 : if (tree_int_cst_sign_bit (step))
3224 : : {
3225 : 0 : tree bound = lower_bound_in_type (inner_type, inner_type);
3226 : 0 : delta = fold_build2 (MINUS_EXPR, type, init, fold_convert (type, bound));
3227 : 0 : step = fold_build1 (NEGATE_EXPR, type, step);
3228 : : }
3229 : : else
3230 : : {
3231 : 31448 : tree bound = upper_bound_in_type (inner_type, inner_type);
3232 : 31448 : delta = fold_build2 (MINUS_EXPR, type, fold_convert (type, bound), init);
3233 : : }
3234 : 31448 : *niters = fold_build2 (FLOOR_DIV_EXPR, type, delta, step);
3235 : 31448 : return ev;
3236 : : }
3237 : :
3238 : : /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3239 : : respect to WRTO_LOOP and returns its base and step in IV if possible
3240 : : (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3241 : : and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3242 : : invariant in LOOP. Otherwise we require it to be an integer constant.
3243 : :
3244 : : IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3245 : : because it is computed in signed arithmetics). Consequently, adding an
3246 : : induction variable
3247 : :
3248 : : for (i = IV->base; ; i += IV->step)
3249 : :
3250 : : is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3251 : : false for the type of the induction variable, or you can prove that i does
3252 : : not wrap by some other argument. Otherwise, this might introduce undefined
3253 : : behavior, and
3254 : :
3255 : : i = iv->base;
3256 : : for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3257 : :
3258 : : must be used instead.
3259 : :
3260 : : When IV_NITERS is not NULL, this function also checks case in which OP
3261 : : is a conversion of an inner simple iv of below form:
3262 : :
3263 : : (outer_type){inner_base, inner_step}_loop.
3264 : :
3265 : : If type of inner iv has smaller precision than outer_type, it can't be
3266 : : folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
3267 : : the inner iv could overflow/wrap. In this case, we derive a condition
3268 : : under which the inner iv won't overflow/wrap and do the simplification.
3269 : : The derived condition normally is the maximum number the inner iv can
3270 : : iterate, and will be stored in IV_NITERS. This is useful in loop niter
3271 : : analysis, to derive break conditions when a loop must terminate, when is
3272 : : infinite. */
3273 : :
3274 : : bool
3275 : 51314703 : simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop,
3276 : : tree op, affine_iv *iv, tree *iv_niters,
3277 : : bool allow_nonconstant_step)
3278 : : {
3279 : 51314703 : enum tree_code code;
3280 : 51314703 : tree type, ev, base, e;
3281 : 51314703 : wide_int extreme;
3282 : 51314703 : bool folded_casts;
3283 : :
3284 : 51314703 : iv->base = NULL_TREE;
3285 : 51314703 : iv->step = NULL_TREE;
3286 : 51314703 : iv->no_overflow = false;
3287 : :
3288 : 51314703 : type = TREE_TYPE (op);
3289 : 51314703 : if (!POINTER_TYPE_P (type)
3290 : 42180989 : && !INTEGRAL_TYPE_P (type))
3291 : : return false;
3292 : :
3293 : 49243391 : ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
3294 : : &folded_casts);
3295 : 49243391 : if (chrec_contains_undetermined (ev)
3296 : 49243391 : || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
3297 : 12990546 : return false;
3298 : :
3299 : 36252845 : if (tree_does_not_contain_chrecs (ev))
3300 : : {
3301 : 16053789 : iv->base = ev;
3302 : 16053789 : tree ev_type = TREE_TYPE (ev);
3303 : 16053789 : if (POINTER_TYPE_P (ev_type))
3304 : 2674984 : ev_type = sizetype;
3305 : :
3306 : 16053789 : iv->step = build_int_cst (ev_type, 0);
3307 : 16053789 : iv->no_overflow = true;
3308 : 16053789 : return true;
3309 : : }
3310 : :
3311 : : /* If we can derive valid scalar evolution with assumptions. */
3312 : 20199056 : if (iv_niters && TREE_CODE (ev) != POLYNOMIAL_CHREC)
3313 : 38743 : ev = derive_simple_iv_with_niters (ev, iv_niters);
3314 : :
3315 : 20199056 : if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
3316 : : return false;
3317 : :
3318 : 20149805 : if (CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
3319 : : return false;
3320 : :
3321 : 20149791 : iv->step = CHREC_RIGHT (ev);
3322 : 12883753 : if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3323 : 32779991 : || tree_contains_chrecs (iv->step, NULL))
3324 : 264986 : return false;
3325 : :
3326 : 19884805 : iv->base = CHREC_LEFT (ev);
3327 : 19884805 : if (tree_contains_chrecs (iv->base, NULL))
3328 : : return false;
3329 : :
3330 : 19884805 : iv->no_overflow = !folded_casts && nowrap_type_p (type);
3331 : :
3332 : 19884805 : if (!iv->no_overflow
3333 : 19884805 : && !iv_can_overflow_p (wrto_loop, type, iv->base, iv->step))
3334 : 3718426 : iv->no_overflow = true;
3335 : :
3336 : : /* Try to simplify iv base:
3337 : :
3338 : : (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3339 : : == (signed T)(unsigned T)base + step
3340 : : == base + step
3341 : :
3342 : : If we can prove operation (base + step) doesn't overflow or underflow.
3343 : : Specifically, we try to prove below conditions are satisfied:
3344 : :
3345 : : base <= UPPER_BOUND (type) - step ;;step > 0
3346 : : base >= LOWER_BOUND (type) - step ;;step < 0
3347 : :
3348 : : This is done by proving the reverse conditions are false using loop's
3349 : : initial conditions.
3350 : :
3351 : : The is necessary to make loop niter, or iv overflow analysis easier
3352 : : for below example:
3353 : :
3354 : : int foo (int *a, signed char s, signed char l)
3355 : : {
3356 : : signed char i;
3357 : : for (i = s; i < l; i++)
3358 : : a[i] = 0;
3359 : : return 0;
3360 : : }
3361 : :
3362 : : Note variable I is firstly converted to type unsigned char, incremented,
3363 : : then converted back to type signed char. */
3364 : :
3365 : 19884805 : if (wrto_loop->num != use_loop->num)
3366 : : return true;
3367 : :
3368 : 19715449 : if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST)
3369 : : return true;
3370 : :
3371 : 234557 : type = TREE_TYPE (iv->base);
3372 : 234557 : e = TREE_OPERAND (iv->base, 0);
3373 : 234557 : if (!tree_nop_conversion_p (type, TREE_TYPE (e))
3374 : 202757 : || TREE_CODE (e) != PLUS_EXPR
3375 : 114311 : || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
3376 : 322112 : || !tree_int_cst_equal (iv->step,
3377 : 87555 : fold_convert (type, TREE_OPERAND (e, 1))))
3378 : 165294 : return true;
3379 : 69263 : e = TREE_OPERAND (e, 0);
3380 : 69263 : if (!CONVERT_EXPR_P (e))
3381 : : return true;
3382 : 37793 : base = TREE_OPERAND (e, 0);
3383 : 37793 : if (!useless_type_conversion_p (type, TREE_TYPE (base)))
3384 : : return true;
3385 : :
3386 : 29649 : if (tree_int_cst_sign_bit (iv->step))
3387 : : {
3388 : 12245 : code = LT_EXPR;
3389 : 12245 : extreme = wi::min_value (type);
3390 : : }
3391 : : else
3392 : : {
3393 : 17404 : code = GT_EXPR;
3394 : 17404 : extreme = wi::max_value (type);
3395 : : }
3396 : 29649 : wi::overflow_type overflow = wi::OVF_NONE;
3397 : 29649 : extreme = wi::sub (extreme, wi::to_wide (iv->step),
3398 : 59298 : TYPE_SIGN (type), &overflow);
3399 : 29649 : if (overflow)
3400 : : return true;
3401 : 29625 : e = fold_build2 (code, boolean_type_node, base,
3402 : : wide_int_to_tree (type, extreme));
3403 : 29625 : e = simplify_using_initial_conditions (use_loop, e);
3404 : 29625 : if (!integer_zerop (e))
3405 : : return true;
3406 : :
3407 : 16201 : if (POINTER_TYPE_P (TREE_TYPE (base)))
3408 : : code = POINTER_PLUS_EXPR;
3409 : : else
3410 : : code = PLUS_EXPR;
3411 : :
3412 : 16201 : iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step);
3413 : 16201 : return true;
3414 : 51314703 : }
3415 : :
3416 : : /* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
3417 : : affine iv unconditionally. */
3418 : :
3419 : : bool
3420 : 20961965 : simple_iv (class loop *wrto_loop, class loop *use_loop, tree op,
3421 : : affine_iv *iv, bool allow_nonconstant_step)
3422 : : {
3423 : 20961965 : return simple_iv_with_niters (wrto_loop, use_loop, op, iv,
3424 : 20961965 : NULL, allow_nonconstant_step);
3425 : : }
3426 : :
3427 : : /* Finalize the scalar evolution analysis. */
3428 : :
3429 : : void
3430 : 15070658 : scev_finalize (void)
3431 : : {
3432 : 15070658 : if (!scalar_evolution_info)
3433 : : return;
3434 : 15070657 : scalar_evolution_info->empty ();
3435 : 15070657 : scalar_evolution_info = NULL;
3436 : 15070657 : free_numbers_of_iterations_estimates (cfun);
3437 : : }
3438 : :
3439 : : /* Returns true if the expression EXPR is considered to be too expensive
3440 : : for scev_const_prop. Sets *COND_OVERFLOW_P to true when the
3441 : : expression might contain a sub-expression that is subject to undefined
3442 : : overflow behavior and conditionally evaluated. */
3443 : :
3444 : : static bool
3445 : 10450778 : expression_expensive_p (tree expr, bool *cond_overflow_p,
3446 : : hash_map<tree, uint64_t> &cache, uint64_t &cost)
3447 : : {
3448 : 10450778 : enum tree_code code;
3449 : :
3450 : 10450778 : if (is_gimple_val (expr))
3451 : : return false;
3452 : :
3453 : 4392505 : code = TREE_CODE (expr);
3454 : 4392505 : if (code == TRUNC_DIV_EXPR
3455 : : || code == CEIL_DIV_EXPR
3456 : : || code == FLOOR_DIV_EXPR
3457 : : || code == ROUND_DIV_EXPR
3458 : : || code == TRUNC_MOD_EXPR
3459 : : || code == CEIL_MOD_EXPR
3460 : : || code == FLOOR_MOD_EXPR
3461 : 4392505 : || code == ROUND_MOD_EXPR
3462 : 4392505 : || code == EXACT_DIV_EXPR)
3463 : : {
3464 : : /* Division by power of two is usually cheap, so we allow it.
3465 : : Forbid anything else. */
3466 : 56138 : if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3467 : : return true;
3468 : : }
3469 : :
3470 : 4382466 : bool visited_p;
3471 : 4382466 : uint64_t &local_cost = cache.get_or_insert (expr, &visited_p);
3472 : 4382466 : if (visited_p)
3473 : : {
3474 : 326 : uint64_t tem = cost + local_cost;
3475 : 326 : if (tem < cost)
3476 : : return true;
3477 : 326 : cost = tem;
3478 : 326 : return false;
3479 : : }
3480 : 4382140 : local_cost = 1;
3481 : :
3482 : 4382140 : uint64_t op_cost = 0;
3483 : 4382140 : if (code == CALL_EXPR)
3484 : : {
3485 : 140 : tree arg;
3486 : 140 : call_expr_arg_iterator iter;
3487 : : /* Even though is_inexpensive_builtin might say true, we will get a
3488 : : library call for popcount when backend does not have an instruction
3489 : : to do so. We consider this to be expensive and generate
3490 : : __builtin_popcount only when backend defines it. */
3491 : 140 : optab optab;
3492 : 140 : combined_fn cfn = get_call_combined_fn (expr);
3493 : 140 : switch (cfn)
3494 : : {
3495 : 31 : CASE_CFN_POPCOUNT:
3496 : 31 : optab = popcount_optab;
3497 : 31 : goto bitcount_call;
3498 : 85 : CASE_CFN_CLZ:
3499 : 85 : optab = clz_optab;
3500 : 85 : goto bitcount_call;
3501 : : CASE_CFN_CTZ:
3502 : : optab = ctz_optab;
3503 : 140 : bitcount_call:
3504 : : /* Check if opcode for popcount is available in the mode required. */
3505 : 140 : if (optab_handler (optab,
3506 : 140 : TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0))))
3507 : : == CODE_FOR_nothing)
3508 : : {
3509 : 27 : machine_mode mode;
3510 : 27 : mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0)));
3511 : 27 : scalar_int_mode int_mode;
3512 : :
3513 : : /* If the mode is of 2 * UNITS_PER_WORD size, we can handle
3514 : : double-word popcount by emitting two single-word popcount
3515 : : instructions. */
3516 : 27 : if (is_a <scalar_int_mode> (mode, &int_mode)
3517 : 29 : && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3518 : 2 : && (optab_handler (optab, word_mode)
3519 : : != CODE_FOR_nothing))
3520 : : break;
3521 : : /* If popcount is available for a wider mode, we emulate the
3522 : : operation for a narrow mode by first zero-extending the value
3523 : : and then computing popcount in the wider mode. Analogue for
3524 : : ctz. For clz we do the same except that we additionally have
3525 : : to subtract the difference of the mode precisions from the
3526 : : result. */
3527 : 25 : if (is_a <scalar_int_mode> (mode, &int_mode))
3528 : : {
3529 : 25 : machine_mode wider_mode_iter;
3530 : 124 : FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
3531 : 99 : if (optab_handler (optab, wider_mode_iter)
3532 : : != CODE_FOR_nothing)
3533 : 0 : goto check_call_args;
3534 : : /* Operation ctz may be emulated via clz in expand_ctz. */
3535 : 25 : if (optab == ctz_optab)
3536 : : {
3537 : 0 : FOR_EACH_WIDER_MODE_FROM (wider_mode_iter, mode)
3538 : 0 : if (optab_handler (clz_optab, wider_mode_iter)
3539 : : != CODE_FOR_nothing)
3540 : 0 : goto check_call_args;
3541 : : }
3542 : : }
3543 : 25 : return true;
3544 : : }
3545 : : break;
3546 : :
3547 : 0 : default:
3548 : 0 : if (cfn == CFN_LAST
3549 : 0 : || !is_inexpensive_builtin (get_callee_fndecl (expr)))
3550 : 0 : return true;
3551 : : break;
3552 : : }
3553 : :
3554 : 115 : check_call_args:
3555 : 345 : FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
3556 : 115 : if (expression_expensive_p (arg, cond_overflow_p, cache, op_cost))
3557 : : return true;
3558 : 115 : *cache.get (expr) += op_cost;
3559 : 115 : cost += op_cost + 1;
3560 : 115 : return false;
3561 : : }
3562 : :
3563 : 4382000 : if (code == COND_EXPR)
3564 : : {
3565 : 1918 : if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
3566 : : cache, op_cost)
3567 : 1918 : || (EXPR_P (TREE_OPERAND (expr, 1))
3568 : 1917 : && EXPR_P (TREE_OPERAND (expr, 2)))
3569 : : /* If either branch has side effects or could trap. */
3570 : 1912 : || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3571 : 1912 : || generic_expr_could_trap_p (TREE_OPERAND (expr, 1))
3572 : 1911 : || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))
3573 : 1911 : || generic_expr_could_trap_p (TREE_OPERAND (expr, 0))
3574 : 1911 : || expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
3575 : : cache, op_cost)
3576 : 3727 : || expression_expensive_p (TREE_OPERAND (expr, 2), cond_overflow_p,
3577 : : cache, op_cost))
3578 : 109 : return true;
3579 : : /* Conservatively assume there's overflow for now. */
3580 : 1809 : *cond_overflow_p = true;
3581 : 1809 : *cache.get (expr) += op_cost;
3582 : 1809 : cost += op_cost + 1;
3583 : 1809 : return false;
3584 : : }
3585 : :
3586 : 4380082 : switch (TREE_CODE_CLASS (code))
3587 : : {
3588 : 2264671 : case tcc_binary:
3589 : 2264671 : case tcc_comparison:
3590 : 2264671 : if (expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
3591 : : cache, op_cost))
3592 : : return true;
3593 : :
3594 : : /* Fallthru. */
3595 : 4377289 : case tcc_unary:
3596 : 4377289 : if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
3597 : : cache, op_cost))
3598 : : return true;
3599 : 4352523 : *cache.get (expr) += op_cost;
3600 : 4352523 : cost += op_cost + 1;
3601 : 4352523 : return false;
3602 : :
3603 : : default:
3604 : : return true;
3605 : : }
3606 : : }
3607 : :
3608 : : bool
3609 : 3803065 : expression_expensive_p (tree expr, bool *cond_overflow_p)
3610 : : {
3611 : 3803065 : hash_map<tree, uint64_t> cache;
3612 : 3803065 : uint64_t expanded_size = 0;
3613 : 3803065 : *cond_overflow_p = false;
3614 : 3803065 : return (expression_expensive_p (expr, cond_overflow_p, cache, expanded_size)
3615 : : /* ??? Both the explicit unsharing and gimplification of expr will
3616 : : expand shared trees to multiple copies.
3617 : : Guard against exponential growth by counting the visits and
3618 : : comparing againt the number of original nodes. Allow a tiny
3619 : : bit of duplication to catch some additional optimizations. */
3620 : 3813267 : || expanded_size > (cache.elements () + 1));
3621 : 3803065 : }
3622 : :
3623 : : /* Match.pd function to match bitwise inductive expression.
3624 : : .i.e.
3625 : : _2 = 1 << _1;
3626 : : _3 = ~_2;
3627 : : tmp_9 = _3 & tmp_12; */
3628 : : extern bool gimple_bitwise_induction_p (tree, tree *, tree (*)(tree));
3629 : :
3630 : : /* Return the inductive expression of bitwise operation if possible,
3631 : : otherwise returns DEF. */
3632 : : static tree
3633 : 20280 : analyze_and_compute_bitwise_induction_effect (class loop* loop,
3634 : : tree phidef,
3635 : : unsigned HOST_WIDE_INT niter)
3636 : : {
3637 : 20280 : tree match_op[3],inv, bitwise_scev;
3638 : 20280 : tree type = TREE_TYPE (phidef);
3639 : 20280 : gphi* header_phi = NULL;
3640 : :
3641 : : /* Match things like op2(MATCH_OP[2]), op1(MATCH_OP[1]), phidef(PHIDEF)
3642 : :
3643 : : op2 = PHI <phidef, inv>
3644 : : _1 = (int) bit_17;
3645 : : _3 = 1 << _1;
3646 : : op1 = ~_3;
3647 : : phidef = op1 & op2; */
3648 : 20280 : if (!gimple_bitwise_induction_p (phidef, &match_op[0], NULL)
3649 : 99 : || TREE_CODE (match_op[2]) != SSA_NAME
3650 : 20280 : || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[2])))
3651 : 99 : || gimple_bb (header_phi) != loop->header
3652 : 20377 : || gimple_phi_num_args (header_phi) != 2)
3653 : : return NULL_TREE;
3654 : :
3655 : 97 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
3656 : : return NULL_TREE;
3657 : :
3658 : 97 : bitwise_scev = analyze_scalar_evolution (loop, match_op[1]);
3659 : 97 : bitwise_scev = instantiate_parameters (loop, bitwise_scev);
3660 : :
3661 : : /* Make sure bits is in range of type precision. */
3662 : 97 : if (TREE_CODE (bitwise_scev) != POLYNOMIAL_CHREC
3663 : 97 : || !INTEGRAL_TYPE_P (TREE_TYPE (bitwise_scev))
3664 : 97 : || !tree_fits_uhwi_p (CHREC_LEFT (bitwise_scev))
3665 : 97 : || tree_to_uhwi (CHREC_LEFT (bitwise_scev)) >= TYPE_PRECISION (type)
3666 : 194 : || !tree_fits_shwi_p (CHREC_RIGHT (bitwise_scev)))
3667 : : return NULL_TREE;
3668 : :
3669 : 97 : enum bit_op_kind
3670 : : {
3671 : : INDUCTION_BIT_CLEAR,
3672 : : INDUCTION_BIT_IOR,
3673 : : INDUCTION_BIT_XOR,
3674 : : INDUCTION_BIT_RESET,
3675 : : INDUCTION_ZERO,
3676 : : INDUCTION_ALL
3677 : : };
3678 : :
3679 : 97 : enum bit_op_kind induction_kind;
3680 : 97 : enum tree_code code1
3681 : 97 : = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (phidef));
3682 : 97 : enum tree_code code2
3683 : 97 : = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (match_op[0]));
3684 : :
3685 : : /* BIT_CLEAR: A &= ~(1 << bit)
3686 : : BIT_RESET: A ^= (1 << bit).
3687 : : BIT_IOR: A |= (1 << bit)
3688 : : BIT_ZERO: A &= (1 << bit)
3689 : : BIT_ALL: A |= ~(1 << bit)
3690 : : BIT_XOR: A ^= ~(1 << bit).
3691 : : bit is induction variable. */
3692 : 97 : switch (code1)
3693 : : {
3694 : 27 : case BIT_AND_EXPR:
3695 : 27 : induction_kind = code2 == BIT_NOT_EXPR
3696 : 27 : ? INDUCTION_BIT_CLEAR
3697 : : : INDUCTION_ZERO;
3698 : : break;
3699 : 46 : case BIT_IOR_EXPR:
3700 : 46 : induction_kind = code2 == BIT_NOT_EXPR
3701 : 46 : ? INDUCTION_ALL
3702 : : : INDUCTION_BIT_IOR;
3703 : : break;
3704 : 12 : case BIT_XOR_EXPR:
3705 : 12 : induction_kind = code2 == BIT_NOT_EXPR
3706 : 12 : ? INDUCTION_BIT_XOR
3707 : : : INDUCTION_BIT_RESET;
3708 : : break;
3709 : : /* A ^ ~(1 << bit) is equal to ~(A ^ (1 << bit)). */
3710 : 12 : case BIT_NOT_EXPR:
3711 : 12 : gcc_assert (code2 == BIT_XOR_EXPR);
3712 : : induction_kind = INDUCTION_BIT_XOR;
3713 : : break;
3714 : 0 : default:
3715 : 0 : gcc_unreachable ();
3716 : : }
3717 : :
3718 : 34 : if (induction_kind == INDUCTION_ZERO)
3719 : 12 : return build_zero_cst (type);
3720 : 85 : if (induction_kind == INDUCTION_ALL)
3721 : 12 : return build_all_ones_cst (type);
3722 : :
3723 : 146 : wide_int bits = wi::zero (TYPE_PRECISION (type));
3724 : 73 : HOST_WIDE_INT bit_start = tree_to_shwi (CHREC_LEFT (bitwise_scev));
3725 : 73 : HOST_WIDE_INT step = tree_to_shwi (CHREC_RIGHT (bitwise_scev));
3726 : 73 : HOST_WIDE_INT bit_final = bit_start + step * niter;
3727 : :
3728 : : /* bit_start, bit_final in range of [0,TYPE_PRECISION)
3729 : : implies all bits are set in range. */
3730 : 73 : if (bit_final >= TYPE_PRECISION (type)
3731 : 73 : || bit_final < 0)
3732 : : return NULL_TREE;
3733 : :
3734 : : /* Loop tripcount should be niter + 1. */
3735 : 1248 : for (unsigned i = 0; i != niter + 1; i++)
3736 : : {
3737 : 1175 : bits = wi::set_bit (bits, bit_start);
3738 : 1175 : bit_start += step;
3739 : : }
3740 : :
3741 : 73 : bool inverted = false;
3742 : 73 : switch (induction_kind)
3743 : : {
3744 : : case INDUCTION_BIT_CLEAR:
3745 : : code1 = BIT_AND_EXPR;
3746 : : inverted = true;
3747 : : break;
3748 : : case INDUCTION_BIT_IOR:
3749 : : code1 = BIT_IOR_EXPR;
3750 : : break;
3751 : : case INDUCTION_BIT_RESET:
3752 : : code1 = BIT_XOR_EXPR;
3753 : : break;
3754 : : /* A ^= ~(1 << bit) is special, when loop tripcount is even,
3755 : : it's equal to A ^= bits, else A ^= ~bits. */
3756 : 12 : case INDUCTION_BIT_XOR:
3757 : 12 : code1 = BIT_XOR_EXPR;
3758 : 12 : if (niter % 2 == 0)
3759 : : inverted = true;
3760 : : break;
3761 : : default:
3762 : : gcc_unreachable ();
3763 : : }
3764 : :
3765 : : if (inverted)
3766 : 19 : bits = wi::bit_not (bits);
3767 : :
3768 : 73 : inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
3769 : 73 : return fold_build2 (code1, type, inv, wide_int_to_tree (type, bits));
3770 : : }
3771 : :
3772 : : /* Match.pd function to match bitop with invariant expression
3773 : : .i.e.
3774 : : tmp_7 = _0 & _1; */
3775 : : extern bool gimple_bitop_with_inv_p (tree, tree *, tree (*)(tree));
3776 : :
3777 : : /* Return the inductive expression of bitop with invariant if possible,
3778 : : otherwise returns DEF. */
3779 : : static tree
3780 : 73294 : analyze_and_compute_bitop_with_inv_effect (class loop* loop, tree phidef,
3781 : : tree niter)
3782 : : {
3783 : 73294 : tree match_op[2],inv;
3784 : 73294 : tree type = TREE_TYPE (phidef);
3785 : 73294 : gphi* header_phi = NULL;
3786 : 73294 : enum tree_code code;
3787 : : /* match thing like op0 (match[0]), op1 (match[1]), phidef (PHIDEF)
3788 : :
3789 : : op1 = PHI <phidef, inv>
3790 : : phidef = op0 & op1
3791 : : if op0 is an invariant, it could change to
3792 : : phidef = op0 & inv. */
3793 : 73294 : gimple *def;
3794 : 73294 : def = SSA_NAME_DEF_STMT (phidef);
3795 : 73294 : if (!(is_gimple_assign (def)
3796 : 27152 : && ((code = gimple_assign_rhs_code (def)), true)
3797 : 27152 : && (code == BIT_AND_EXPR || code == BIT_IOR_EXPR
3798 : 21568 : || code == BIT_XOR_EXPR)))
3799 : : return NULL_TREE;
3800 : :
3801 : 6277 : match_op[0] = gimple_assign_rhs1 (def);
3802 : 6277 : match_op[1] = gimple_assign_rhs2 (def);
3803 : :
3804 : 6277 : if (expr_invariant_in_loop_p (loop, match_op[1]))
3805 : 221 : std::swap (match_op[0], match_op[1]);
3806 : :
3807 : 6277 : if (TREE_CODE (match_op[1]) != SSA_NAME
3808 : 6277 : || !expr_invariant_in_loop_p (loop, match_op[0])
3809 : 314 : || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[1])))
3810 : 211 : || gimple_bb (header_phi) != loop->header
3811 : 6478 : || gimple_phi_num_args (header_phi) != 2)
3812 : 6076 : return NULL_TREE;
3813 : :
3814 : 201 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
3815 : : return NULL_TREE;
3816 : :
3817 : 196 : enum tree_code code1
3818 : 196 : = gimple_assign_rhs_code (def);
3819 : :
3820 : 196 : if (code1 == BIT_XOR_EXPR)
3821 : : {
3822 : 51 : if (!tree_fits_uhwi_p (niter))
3823 : : return NULL_TREE;
3824 : 19 : unsigned HOST_WIDE_INT niter_num;
3825 : 19 : niter_num = tree_to_uhwi (niter);
3826 : 19 : if (niter_num % 2 != 0)
3827 : 10 : match_op[0] = build_zero_cst (type);
3828 : : }
3829 : :
3830 : 164 : inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
3831 : 164 : return fold_build2 (code1, type, inv, match_op[0]);
3832 : : }
3833 : :
3834 : : /* Do final value replacement for LOOP, return true if we did anything. */
3835 : :
3836 : : bool
3837 : 698280 : final_value_replacement_loop (class loop *loop)
3838 : : {
3839 : : /* If we do not know exact number of iterations of the loop, we cannot
3840 : : replace the final value. */
3841 : 698280 : edge exit = single_exit (loop);
3842 : 698280 : if (!exit)
3843 : : return false;
3844 : :
3845 : 460299 : tree niter = number_of_latch_executions (loop);
3846 : 460299 : if (niter == chrec_dont_know)
3847 : : return false;
3848 : :
3849 : : /* Ensure that it is possible to insert new statements somewhere. */
3850 : 339837 : if (!single_pred_p (exit->dest))
3851 : 40827 : split_loop_exit_edge (exit);
3852 : :
3853 : : /* Set stmt insertion pointer. All stmts are inserted before this point. */
3854 : :
3855 : 339837 : class loop *ex_loop
3856 : 679674 : = superloop_at_depth (loop,
3857 : 415416 : loop_depth (exit->dest->loop_father) + 1);
3858 : :
3859 : 339837 : bool any = false;
3860 : 339837 : gphi_iterator psi;
3861 : 761846 : for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
3862 : : {
3863 : 422009 : gphi *phi = psi.phi ();
3864 : 422009 : tree rslt = PHI_RESULT (phi);
3865 : 422009 : tree phidef = PHI_ARG_DEF_FROM_EDGE (phi, exit);
3866 : 422009 : tree def = phidef;
3867 : 843343 : if (virtual_operand_p (def))
3868 : : {
3869 : 237846 : gsi_next (&psi);
3870 : 624206 : continue;
3871 : : }
3872 : :
3873 : 348139 : if (!POINTER_TYPE_P (TREE_TYPE (def))
3874 : 348044 : && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
3875 : : {
3876 : 73761 : gsi_next (&psi);
3877 : 73761 : continue;
3878 : : }
3879 : :
3880 : 110402 : bool folded_casts;
3881 : 110402 : def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
3882 : : &folded_casts);
3883 : :
3884 : 110402 : tree bitinv_def, bit_def;
3885 : 110402 : unsigned HOST_WIDE_INT niter_num;
3886 : :
3887 : 110402 : if (def != chrec_dont_know)
3888 : 37108 : def = compute_overall_effect_of_inner_loop (ex_loop, def);
3889 : :
3890 : : /* Handle bitop with invariant induction expression.
3891 : :
3892 : : .i.e
3893 : : for (int i =0 ;i < 32; i++)
3894 : : tmp &= bit2;
3895 : : if bit2 is an invariant in loop which could simple to
3896 : : tmp &= bit2. */
3897 : 146588 : else if ((bitinv_def
3898 : 73294 : = analyze_and_compute_bitop_with_inv_effect (loop,
3899 : : phidef, niter)))
3900 : : def = bitinv_def;
3901 : :
3902 : : /* Handle bitwise induction expression.
3903 : :
3904 : : .i.e.
3905 : : for (int i = 0; i != 64; i+=3)
3906 : : res &= ~(1UL << i);
3907 : :
3908 : : RES can't be analyzed out by SCEV because it is not polynomially
3909 : : expressible, but in fact final value of RES can be replaced by
3910 : : RES & CONSTANT where CONSTANT all ones with bit {0,3,6,9,... ,63}
3911 : : being cleared, similar for BIT_IOR_EXPR/BIT_XOR_EXPR. */
3912 : 73130 : else if (tree_fits_uhwi_p (niter)
3913 : 30774 : && (niter_num = tree_to_uhwi (niter)) != 0
3914 : 30747 : && niter_num < TYPE_PRECISION (TREE_TYPE (phidef))
3915 : 73130 : && (bit_def
3916 : 20280 : = analyze_and_compute_bitwise_induction_effect (loop,
3917 : : phidef,
3918 : : niter_num)))
3919 : : def = bit_def;
3920 : :
3921 : 110402 : bool cond_overflow_p;
3922 : 110402 : if (!tree_does_not_contain_chrecs (def)
3923 : 36791 : || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
3924 : : /* Moving the computation from the loop may prolong life range
3925 : : of some ssa names, which may cause problems if they appear
3926 : : on abnormal edges. */
3927 : 36791 : || contains_abnormal_ssa_name_p (def)
3928 : : /* Do not emit expensive expressions. The rationale is that
3929 : : when someone writes a code like
3930 : :
3931 : : while (n > 45) n -= 45;
3932 : :
3933 : : he probably knows that n is not large, and does not want it
3934 : : to be turned into n %= 45. */
3935 : 147193 : || expression_expensive_p (def, &cond_overflow_p))
3936 : : {
3937 : 74753 : if (dump_file && (dump_flags & TDF_DETAILS))
3938 : : {
3939 : 56 : fprintf (dump_file, "not replacing:\n ");
3940 : 56 : print_gimple_stmt (dump_file, phi, 0);
3941 : 56 : fprintf (dump_file, "\n");
3942 : : }
3943 : 74753 : gsi_next (&psi);
3944 : 74753 : continue;
3945 : : }
3946 : :
3947 : : /* Eliminate the PHI node and replace it by a computation outside
3948 : : the loop. */
3949 : 35649 : if (dump_file)
3950 : : {
3951 : 132 : fprintf (dump_file, "\nfinal value replacement:\n ");
3952 : 132 : print_gimple_stmt (dump_file, phi, 0);
3953 : 132 : fprintf (dump_file, " with expr: ");
3954 : 132 : print_generic_expr (dump_file, def);
3955 : 132 : fprintf (dump_file, "\n");
3956 : : }
3957 : 35649 : any = true;
3958 : : /* ??? Here we'd like to have a unshare_expr that would assign
3959 : : shared sub-trees to new temporary variables either gimplified
3960 : : to a GIMPLE sequence or to a statement list (keeping this a
3961 : : GENERIC interface). */
3962 : 35649 : def = unshare_expr (def);
3963 : 35649 : auto loc = gimple_phi_arg_location (phi, exit->dest_idx);
3964 : :
3965 : : /* Create the replacement statements. */
3966 : 35649 : gimple_seq stmts;
3967 : 35649 : def = force_gimple_operand (def, &stmts, false, NULL_TREE);
3968 : :
3969 : : /* Propagate constants immediately, but leave an unused initialization
3970 : : around to avoid invalidating the SCEV cache. */
3971 : 42346 : if (CONSTANT_CLASS_P (def) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
3972 : 6696 : replace_uses_by (rslt, def);
3973 : :
3974 : : /* Remove the old phi after the gimplification to make sure the
3975 : : SSA name is defined by a statement so that fold_stmt during
3976 : : the gimplification does not crash. */
3977 : 35649 : remove_phi_node (&psi, false);
3978 : 35649 : gassign *ass = gimple_build_assign (rslt, def);
3979 : 35649 : gimple_set_location (ass, loc);
3980 : 35649 : gimple_seq_add_stmt (&stmts, ass);
3981 : :
3982 : : /* If def's type has undefined overflow and there were folded
3983 : : casts, rewrite all stmts added for def into arithmetics
3984 : : with defined overflow behavior. */
3985 : 35649 : if ((folded_casts
3986 : 418 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
3987 : 704 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
3988 : 36067 : || cond_overflow_p)
3989 : : {
3990 : 2067 : gimple_stmt_iterator gsi2;
3991 : 2067 : gsi2 = gsi_start (stmts);
3992 : 18980 : while (!gsi_end_p (gsi2))
3993 : : {
3994 : 16913 : if (gimple_needing_rewrite_undefined (gsi_stmt (gsi2)))
3995 : 420 : rewrite_to_defined_unconditional (&gsi2);
3996 : 16913 : gsi_next (&gsi2);
3997 : : }
3998 : : }
3999 : 35649 : gimple_stmt_iterator gsi = gsi_after_labels (exit->dest);
4000 : 35649 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
4001 : 35649 : if (dump_file)
4002 : : {
4003 : 132 : fprintf (dump_file, " final stmt:\n ");
4004 : 132 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (rslt), 0);
4005 : 132 : fprintf (dump_file, "\n");
4006 : : }
4007 : :
4008 : : /* Re-fold immediate uses of the replaced def, but avoid
4009 : : CFG manipulations from this function. For now only do
4010 : : a single-level re-folding, not re-folding uses of
4011 : : folded uses. */
4012 : 35649 : if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
4013 : : {
4014 : 35648 : gimple *use_stmt;
4015 : 35648 : imm_use_iterator imm_iter;
4016 : 35648 : auto_vec<gimple *, 4> to_fold;
4017 : 70613 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, rslt)
4018 : 34965 : if (!stmt_can_throw_internal (cfun, use_stmt))
4019 : 34963 : to_fold.safe_push (use_stmt);
4020 : : /* Delay folding until after the immediate use walk is completed
4021 : : as we have an active ranger and that might walk immediate
4022 : : uses of rslt again. See PR122502. */
4023 : 141907 : for (gimple *use_stmt : to_fold)
4024 : : {
4025 : 34963 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
4026 : 34963 : if (fold_stmt (&gsi, follow_all_ssa_edges))
4027 : 1791 : update_stmt (gsi_stmt (gsi));
4028 : : }
4029 : 35648 : }
4030 : : }
4031 : :
4032 : : return any;
4033 : : }
4034 : :
4035 : : #include "gt-tree-scalar-evolution.h"
|