Line data Source code
1 : /* Scalar evolution detector.
2 : Copyright (C) 2003-2026 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, tree * = nullptr);
290 : static tree analyze_scalar_evolution_for_address_of (class loop *loop,
291 : tree var,
292 : tree * = nullptr);
293 :
294 : /* The cached information about an SSA name with version NAME_VERSION,
295 : claiming that below basic block with index INSTANTIATED_BELOW, the
296 : value of the SSA name can be expressed as CHREC.
297 : NOWRAP_BOUND can store an assumption to guarantee that the SSA name
298 : doesn't wrap. */
299 :
300 : struct GTY((for_user)) scev_info_str {
301 : unsigned int name_version;
302 : int instantiated_below;
303 : tree chrec;
304 : tree nowrap_bound;
305 : };
306 :
307 : /* Counters for the scev database. */
308 : static unsigned nb_set_scev = 0;
309 : static unsigned nb_get_scev = 0;
310 :
311 : struct scev_info_hasher : ggc_ptr_hash<scev_info_str>
312 : {
313 : static hashval_t hash (scev_info_str *i);
314 : static bool equal (const scev_info_str *a, const scev_info_str *b);
315 : };
316 :
317 : /* Keep regular hash entries and entries that have nowrap assumptions separate
318 : by using two hash tables. */
319 : static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info;
320 : static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_nowrap_info;
321 :
322 :
323 : /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
324 :
325 : static inline struct scev_info_str *
326 56393622 : new_scev_info_str (basic_block instantiated_below, tree var)
327 : {
328 56393622 : struct scev_info_str *res;
329 :
330 56393622 : res = ggc_alloc<scev_info_str> ();
331 56393622 : res->name_version = SSA_NAME_VERSION (var);
332 56393622 : res->instantiated_below = instantiated_below->index;
333 56393622 : res->chrec = chrec_not_analyzed_yet;
334 56393622 : res->nowrap_bound = NULL_TREE;
335 :
336 56393622 : return res;
337 : }
338 :
339 : /* Computes a hash function for database element ELT. */
340 :
341 : hashval_t
342 1125518187 : scev_info_hasher::hash (scev_info_str *elt)
343 : {
344 1125518187 : return (elt->name_version ^ elt->instantiated_below);
345 : }
346 :
347 : /* Compares database elements E1 and E2. */
348 :
349 : bool
350 1116182481 : scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2)
351 : {
352 1116182481 : return (elt1->name_version == elt2->name_version
353 1116182481 : && elt1->instantiated_below == elt2->instantiated_below);
354 : }
355 :
356 : /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
357 : A first query on VAR returns chrec_not_analyzed_yet. */
358 :
359 : static scev_info_str *
360 218355196 : find_var_scev_info (basic_block instantiated_below, tree var,
361 : bool nowrap_assumption_p)
362 : {
363 218355196 : struct scev_info_str tmp;
364 :
365 218355196 : tmp.name_version = SSA_NAME_VERSION (var);
366 218355196 : tmp.instantiated_below = instantiated_below->index;
367 218355196 : scev_info_str **slot;
368 218355196 : if (!nowrap_assumption_p)
369 216109207 : slot = scalar_evolution_info->find_slot (&tmp, INSERT);
370 : else
371 2245989 : slot = scalar_evolution_nowrap_info->find_slot (&tmp, INSERT);
372 :
373 218355196 : if (!*slot)
374 56393622 : *slot = new_scev_info_str (instantiated_below, var);
375 :
376 218355196 : return *slot;
377 : }
378 :
379 :
380 : /* Hashtable helpers for a temporary hash-table used when
381 : analyzing a scalar evolution, instantiating a CHREC or
382 : resolving mixers. */
383 :
384 : class instantiate_cache_type
385 : {
386 : public:
387 : htab_t map;
388 : vec<scev_info_str> entries;
389 :
390 136485626 : instantiate_cache_type () : map (NULL), entries (vNULL) {}
391 : ~instantiate_cache_type ();
392 133174586 : tree get (unsigned slot) { return entries[slot].chrec; }
393 103071886 : void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; }
394 : };
395 :
396 136485626 : instantiate_cache_type::~instantiate_cache_type ()
397 : {
398 136485626 : if (map != NULL)
399 : {
400 30256149 : htab_delete (map);
401 30256149 : entries.release ();
402 : }
403 136485626 : }
404 :
405 : /* Cache to avoid infinite recursion when instantiating an SSA name.
406 : Live during the outermost analyze_scalar_evolution, instantiate_scev
407 : or resolve_mixers call. */
408 : static instantiate_cache_type *global_cache;
409 :
410 : /* Build a combined bound from BASE and BOUND by taking the minimum
411 : of both. The result is stored in BASE. */
412 :
413 : void
414 70751494 : scev_combine_nowrap_bounds (tree *base, tree bound)
415 : {
416 70751494 : if (!base || !bound)
417 : return;
418 7769 : if (!*base)
419 : {
420 6191 : *base = bound;
421 6191 : return;
422 : }
423 :
424 1578 : tree t1 = TREE_TYPE (*base);
425 1578 : tree t2 = TREE_TYPE (bound);
426 :
427 : /* We should always be getting unsigned types from
428 : get_niter_type_bound. */
429 1578 : gcc_assert (TYPE_UNSIGNED (t1) && TYPE_UNSIGNED (t2));
430 :
431 1578 : tree wide_type = TYPE_PRECISION (t1) >= TYPE_PRECISION (t2) ? t1 : t2;
432 1578 : *base = fold_build2 (MIN_EXPR, wide_type,
433 : fold_convert (wide_type, *base),
434 : fold_convert (wide_type, bound));
435 : }
436 :
437 : /* Return true when PHI is a loop-phi-node. */
438 :
439 : static bool
440 28335476 : loop_phi_node_p (gimple *phi)
441 : {
442 : /* The implementation of this function is based on the following
443 : property: "all the loop-phi-nodes of a loop are contained in the
444 : loop's header basic block". */
445 :
446 0 : return loop_containing_stmt (phi)->header == gimple_bb (phi);
447 : }
448 :
449 : /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
450 : In general, in the case of multivariate evolutions we want to get
451 : the evolution in different loops. LOOP specifies the level for
452 : which to get the evolution.
453 :
454 : Example:
455 :
456 : | for (j = 0; j < 100; j++)
457 : | {
458 : | for (k = 0; k < 100; k++)
459 : | {
460 : | i = k + j; - Here the value of i is a function of j, k.
461 : | }
462 : | ... = i - Here the value of i is a function of j.
463 : | }
464 : | ... = i - Here the value of i is a scalar.
465 :
466 : Example:
467 :
468 : | i_0 = ...
469 : | loop_1 10 times
470 : | i_1 = phi (i_0, i_2)
471 : | i_2 = i_1 + 2
472 : | endloop
473 :
474 : This loop has the same effect as:
475 : LOOP_1 has the same effect as:
476 :
477 : | i_1 = i_0 + 20
478 :
479 : The overall effect of the loop, "i_0 + 20" in the previous example,
480 : is obtained by passing in the parameters: LOOP = 1,
481 : EVOLUTION_FN = {i_0, +, 2}_1.
482 : */
483 :
484 : tree
485 6183114 : compute_overall_effect_of_inner_loop (class loop *loop, tree evolution_fn)
486 : {
487 7122145 : bool val = false;
488 :
489 7122145 : if (evolution_fn == chrec_dont_know)
490 : return chrec_dont_know;
491 :
492 6981367 : else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC)
493 : {
494 2571613 : class loop *inner_loop = get_chrec_loop (evolution_fn);
495 :
496 2571613 : if (inner_loop == loop
497 2571613 : || flow_loop_nested_p (loop, inner_loop))
498 : {
499 2571613 : tree nb_iter = number_of_latch_executions (inner_loop);
500 :
501 2571613 : if (nb_iter == chrec_dont_know)
502 : return chrec_dont_know;
503 : else
504 : {
505 939031 : tree res;
506 :
507 : /* evolution_fn is the evolution function in LOOP. Get
508 : its value in the nb_iter-th iteration. */
509 939031 : res = chrec_apply (inner_loop->num, evolution_fn, nb_iter);
510 :
511 939031 : if (chrec_contains_symbols_defined_in_loop (res, loop->num))
512 59698 : res = instantiate_parameters (loop, res);
513 :
514 : /* Continue the computation until ending on a parent of LOOP. */
515 939031 : return compute_overall_effect_of_inner_loop (loop, res);
516 : }
517 : }
518 : else
519 : return evolution_fn;
520 : }
521 :
522 : /* If the evolution function is an invariant, there is nothing to do. */
523 4409754 : else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val)
524 : return evolution_fn;
525 :
526 : else
527 3604402 : return chrec_dont_know;
528 : }
529 :
530 : /* Associate CHREC to SCALAR. */
531 :
532 : static void
533 53661283 : set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec,
534 : tree *nowrap_bound)
535 : {
536 53661283 : scev_info_str *scalar_info;
537 :
538 53661283 : if (TREE_CODE (scalar) != SSA_NAME)
539 : return;
540 :
541 53661283 : scalar_info = find_var_scev_info (instantiated_below, scalar,
542 : nowrap_bound != nullptr);
543 :
544 53661283 : if (dump_file)
545 : {
546 131870 : if (dump_flags & TDF_SCEV)
547 : {
548 11 : fprintf (dump_file, "(set_scalar_evolution \n");
549 11 : fprintf (dump_file, " instantiated_below = %d \n",
550 : instantiated_below->index);
551 11 : fprintf (dump_file, " (scalar = ");
552 11 : print_generic_expr (dump_file, scalar);
553 11 : fprintf (dump_file, ")\n (scalar_evolution = ");
554 11 : print_generic_expr (dump_file, chrec);
555 11 : fprintf (dump_file, "))\n");
556 : }
557 131870 : if (dump_flags & TDF_STATS)
558 7195 : nb_set_scev++;
559 : }
560 :
561 53661283 : scalar_info->chrec = chrec;
562 53661283 : if (nowrap_bound)
563 636821 : scalar_info->nowrap_bound = *nowrap_bound;
564 : }
565 :
566 : /* Retrieve the chrec associated to SCALAR instantiated below
567 : INSTANTIATED_BELOW block. */
568 :
569 : static tree
570 205317152 : get_scalar_evolution (basic_block instantiated_below, tree scalar,
571 : tree *nowrap_bound)
572 : {
573 205317152 : tree res;
574 :
575 205317152 : if (dump_file)
576 : {
577 794372 : if (dump_flags & TDF_SCEV)
578 : {
579 38 : fprintf (dump_file, "(get_scalar_evolution \n");
580 38 : fprintf (dump_file, " (scalar = ");
581 38 : print_generic_expr (dump_file, scalar);
582 38 : fprintf (dump_file, ")\n");
583 : }
584 794372 : if (dump_flags & TDF_STATS)
585 51034 : nb_get_scev++;
586 : }
587 :
588 205317152 : if (VECTOR_TYPE_P (TREE_TYPE (scalar))
589 205317152 : || TREE_CODE (TREE_TYPE (scalar)) == COMPLEX_TYPE)
590 : /* For chrec_dont_know we keep the symbolic form. */
591 : res = scalar;
592 : else
593 204997221 : switch (TREE_CODE (scalar))
594 : {
595 166942179 : case SSA_NAME:
596 166942179 : if (SSA_NAME_IS_DEFAULT_DEF (scalar))
597 : res = scalar;
598 : else
599 : {
600 164693913 : scev_info_str *info
601 164693913 : = find_var_scev_info (instantiated_below, scalar,
602 : nowrap_bound != nullptr);
603 164693913 : res = info->chrec;
604 164693913 : if (info->nowrap_bound
605 1559 : && res != chrec_not_analyzed_yet)
606 1559 : scev_combine_nowrap_bounds (nowrap_bound, info->nowrap_bound);
607 : }
608 : break;
609 :
610 : case REAL_CST:
611 : case FIXED_CST:
612 : case INTEGER_CST:
613 : res = scalar;
614 : break;
615 :
616 : default:
617 205317152 : res = chrec_not_analyzed_yet;
618 : break;
619 : }
620 :
621 205317152 : if (dump_file && (dump_flags & TDF_SCEV))
622 : {
623 38 : fprintf (dump_file, " (scalar_evolution = ");
624 38 : print_generic_expr (dump_file, res);
625 38 : fprintf (dump_file, "))\n");
626 : }
627 :
628 205317152 : return res;
629 : }
630 :
631 :
632 : /* Depth first search algorithm. */
633 :
634 : enum t_bool {
635 : t_false,
636 : t_true,
637 : t_dont_know
638 : };
639 :
640 : class scev_dfs
641 : {
642 : public:
643 11710041 : scev_dfs (class loop *loop_, gphi *phi_, tree init_cond_)
644 11710041 : : loop (loop_), loop_phi_node (phi_), init_cond (init_cond_) {}
645 : t_bool get_ev (tree *, tree);
646 :
647 : private:
648 : t_bool follow_ssa_edge_expr (gimple *, tree, tree *, int);
649 : t_bool follow_ssa_edge_binary (gimple *at_stmt,
650 : tree type, tree rhs0, enum tree_code code,
651 : tree rhs1, tree *evolution_of_loop, int limit);
652 : t_bool follow_ssa_edge_in_condition_phi_branch (int i,
653 : gphi *condition_phi,
654 : tree *evolution_of_branch,
655 : tree init_cond, int limit);
656 : t_bool follow_ssa_edge_in_condition_phi (gphi *condition_phi,
657 : tree *evolution_of_loop, int limit);
658 : t_bool follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
659 : tree *evolution_of_loop, int limit);
660 : tree add_to_evolution (tree chrec_before, tree to_add, gimple *at_stmt);
661 : tree add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt);
662 :
663 : class loop *loop;
664 : gphi *loop_phi_node;
665 : tree init_cond;
666 : };
667 :
668 : t_bool
669 11710041 : scev_dfs::get_ev (tree *ev_fn, tree arg)
670 : {
671 11710041 : *ev_fn = chrec_dont_know;
672 11710041 : return follow_ssa_edge_expr (loop_phi_node, arg, ev_fn, 0);
673 : }
674 :
675 : /* Helper function for add_to_evolution. Returns the evolution
676 : function for an assignment of the form "a = b + c", where "a" and
677 : "b" are on the strongly connected component. CHREC_BEFORE is the
678 : information that we already have collected up to this point.
679 : TO_ADD is the evolution of "c".
680 :
681 : When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
682 : evolution the expression TO_ADD, otherwise construct an evolution
683 : part for this loop. */
684 :
685 : tree
686 9619850 : scev_dfs::add_to_evolution_1 (tree chrec_before, tree to_add, gimple *at_stmt)
687 : {
688 9619850 : tree type, left, right;
689 9619850 : unsigned loop_nb = loop->num;
690 9619850 : class loop *chloop;
691 :
692 9619850 : switch (TREE_CODE (chrec_before))
693 : {
694 117843 : case POLYNOMIAL_CHREC:
695 117843 : chloop = get_chrec_loop (chrec_before);
696 117843 : if (chloop == loop
697 117843 : || flow_loop_nested_p (chloop, loop))
698 : {
699 117843 : unsigned var;
700 :
701 117843 : type = chrec_type (chrec_before);
702 :
703 : /* When there is no evolution part in this loop, build it. */
704 117843 : if (chloop != loop)
705 : {
706 0 : var = loop_nb;
707 0 : left = chrec_before;
708 0 : right = SCALAR_FLOAT_TYPE_P (type)
709 0 : ? build_real (type, dconst0)
710 0 : : build_int_cst (type, 0);
711 : }
712 : else
713 : {
714 117843 : var = CHREC_VARIABLE (chrec_before);
715 117843 : left = CHREC_LEFT (chrec_before);
716 117843 : right = CHREC_RIGHT (chrec_before);
717 : }
718 :
719 117843 : to_add = chrec_convert (type, to_add, at_stmt);
720 117843 : right = chrec_convert_rhs (type, right, at_stmt);
721 117843 : right = chrec_fold_plus (chrec_type (right), right, to_add);
722 117843 : return build_polynomial_chrec (var, left, right);
723 : }
724 : else
725 : {
726 0 : gcc_assert (flow_loop_nested_p (loop, chloop));
727 :
728 : /* Search the evolution in LOOP_NB. */
729 0 : left = add_to_evolution_1 (CHREC_LEFT (chrec_before),
730 : to_add, at_stmt);
731 0 : right = CHREC_RIGHT (chrec_before);
732 0 : right = chrec_convert_rhs (chrec_type (left), right, at_stmt);
733 0 : return build_polynomial_chrec (CHREC_VARIABLE (chrec_before),
734 0 : left, right);
735 : }
736 :
737 9502007 : default:
738 : /* These nodes do not depend on a loop. */
739 9502007 : if (chrec_before == chrec_dont_know)
740 : return chrec_dont_know;
741 :
742 9482883 : left = chrec_before;
743 9482883 : right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt);
744 : /* When we add the first evolution we need to replace the symbolic
745 : evolution we've put in when the DFS reached the loop PHI node
746 : with the initial value. There's only a limited cases of
747 : extra operations on top of that symbol allowed, namely
748 : sign-conversions we can look through. For other cases we leave
749 : the symbolic initial condition which causes build_polynomial_chrec
750 : to return chrec_dont_know. See PR42512, PR66375 and PR107176 for
751 : cases we mishandled before. */
752 9482883 : STRIP_NOPS (chrec_before);
753 9482883 : if (chrec_before == gimple_phi_result (loop_phi_node))
754 9482108 : left = fold_convert (TREE_TYPE (left), init_cond);
755 9482883 : return build_polynomial_chrec (loop_nb, left, right);
756 : }
757 : }
758 :
759 : /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
760 : of LOOP_NB.
761 :
762 : Description (provided for completeness, for those who read code in
763 : a plane, and for my poor 62 bytes brain that would have forgotten
764 : all this in the next two or three months):
765 :
766 : The algorithm of translation of programs from the SSA representation
767 : into the chrecs syntax is based on a pattern matching. After having
768 : reconstructed the overall tree expression for a loop, there are only
769 : two cases that can arise:
770 :
771 : 1. a = loop-phi (init, a + expr)
772 : 2. a = loop-phi (init, expr)
773 :
774 : where EXPR is either a scalar constant with respect to the analyzed
775 : loop (this is a degree 0 polynomial), or an expression containing
776 : other loop-phi definitions (these are higher degree polynomials).
777 :
778 : Examples:
779 :
780 : 1.
781 : | init = ...
782 : | loop_1
783 : | a = phi (init, a + 5)
784 : | endloop
785 :
786 : 2.
787 : | inita = ...
788 : | initb = ...
789 : | loop_1
790 : | a = phi (inita, 2 * b + 3)
791 : | b = phi (initb, b + 1)
792 : | endloop
793 :
794 : For the first case, the semantics of the SSA representation is:
795 :
796 : | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
797 :
798 : that is, there is a loop index "x" that determines the scalar value
799 : of the variable during the loop execution. During the first
800 : iteration, the value is that of the initial condition INIT, while
801 : during the subsequent iterations, it is the sum of the initial
802 : condition with the sum of all the values of EXPR from the initial
803 : iteration to the before last considered iteration.
804 :
805 : For the second case, the semantics of the SSA program is:
806 :
807 : | a (x) = init, if x = 0;
808 : | expr (x - 1), otherwise.
809 :
810 : The second case corresponds to the PEELED_CHREC, whose syntax is
811 : close to the syntax of a loop-phi-node:
812 :
813 : | phi (init, expr) vs. (init, expr)_x
814 :
815 : The proof of the translation algorithm for the first case is a
816 : proof by structural induction based on the degree of EXPR.
817 :
818 : Degree 0:
819 : When EXPR is a constant with respect to the analyzed loop, or in
820 : other words when EXPR is a polynomial of degree 0, the evolution of
821 : the variable A in the loop is an affine function with an initial
822 : condition INIT, and a step EXPR. In order to show this, we start
823 : from the semantics of the SSA representation:
824 :
825 : f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
826 :
827 : and since "expr (j)" is a constant with respect to "j",
828 :
829 : f (x) = init + x * expr
830 :
831 : Finally, based on the semantics of the pure sum chrecs, by
832 : identification we get the corresponding chrecs syntax:
833 :
834 : f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
835 : f (x) -> {init, +, expr}_x
836 :
837 : Higher degree:
838 : Suppose that EXPR is a polynomial of degree N with respect to the
839 : analyzed loop_x for which we have already determined that it is
840 : written under the chrecs syntax:
841 :
842 : | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
843 :
844 : We start from the semantics of the SSA program:
845 :
846 : | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
847 : |
848 : | f (x) = init + \sum_{j = 0}^{x - 1}
849 : | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
850 : |
851 : | f (x) = init + \sum_{j = 0}^{x - 1}
852 : | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
853 : |
854 : | f (x) = init + \sum_{k = 0}^{n - 1}
855 : | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
856 : |
857 : | f (x) = init + \sum_{k = 0}^{n - 1}
858 : | (b_k * \binom{x}{k + 1})
859 : |
860 : | f (x) = init + b_0 * \binom{x}{1} + ...
861 : | + b_{n-1} * \binom{x}{n}
862 : |
863 : | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
864 : | + b_{n-1} * \binom{x}{n}
865 : |
866 :
867 : And finally from the definition of the chrecs syntax, we identify:
868 : | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
869 :
870 : This shows the mechanism that stands behind the add_to_evolution
871 : function. An important point is that the use of symbolic
872 : parameters avoids the need of an analysis schedule.
873 :
874 : Example:
875 :
876 : | inita = ...
877 : | initb = ...
878 : | loop_1
879 : | a = phi (inita, a + 2 + b)
880 : | b = phi (initb, b + 1)
881 : | endloop
882 :
883 : When analyzing "a", the algorithm keeps "b" symbolically:
884 :
885 : | a -> {inita, +, 2 + b}_1
886 :
887 : Then, after instantiation, the analyzer ends on the evolution:
888 :
889 : | a -> {inita, +, 2 + initb, +, 1}_1
890 :
891 : */
892 :
893 : tree
894 9619850 : scev_dfs::add_to_evolution (tree chrec_before, tree to_add, gimple *at_stmt)
895 : {
896 9619850 : tree res = NULL_TREE;
897 :
898 9619850 : if (to_add == NULL_TREE)
899 : return chrec_before;
900 :
901 : /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
902 : instantiated at this point. */
903 9619850 : if (TREE_CODE (to_add) == POLYNOMIAL_CHREC)
904 : /* This should not happen. */
905 0 : return chrec_dont_know;
906 :
907 9619850 : if (dump_file && (dump_flags & TDF_SCEV))
908 : {
909 1 : fprintf (dump_file, "(add_to_evolution \n");
910 1 : fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
911 1 : fprintf (dump_file, " (chrec_before = ");
912 1 : print_generic_expr (dump_file, chrec_before);
913 1 : fprintf (dump_file, ")\n (to_add = ");
914 1 : print_generic_expr (dump_file, to_add);
915 1 : fprintf (dump_file, ")\n");
916 : }
917 :
918 9619850 : res = add_to_evolution_1 (chrec_before, to_add, at_stmt);
919 :
920 9619850 : if (dump_file && (dump_flags & TDF_SCEV))
921 : {
922 1 : fprintf (dump_file, " (res = ");
923 1 : print_generic_expr (dump_file, res);
924 1 : fprintf (dump_file, "))\n");
925 : }
926 :
927 : return res;
928 : }
929 :
930 :
931 : /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
932 : Return true if the strongly connected component has been found. */
933 :
934 : t_bool
935 1192484 : scev_dfs::follow_ssa_edge_binary (gimple *at_stmt, tree type, tree rhs0,
936 : enum tree_code code, tree rhs1,
937 : tree *evolution_of_loop, int limit)
938 : {
939 1192484 : t_bool res = t_false;
940 1192484 : tree evol;
941 :
942 1192484 : switch (code)
943 : {
944 1192484 : case POINTER_PLUS_EXPR:
945 1192484 : case PLUS_EXPR:
946 1192484 : if (TREE_CODE (rhs0) == SSA_NAME)
947 : {
948 1169018 : if (TREE_CODE (rhs1) == SSA_NAME)
949 : {
950 : /* Match an assignment under the form:
951 : "a = b + c". */
952 :
953 : /* We want only assignments of form "name + name" contribute to
954 : LIMIT, as the other cases do not necessarily contribute to
955 : the complexity of the expression. */
956 1169018 : limit++;
957 :
958 1169018 : evol = *evolution_of_loop;
959 1169018 : res = follow_ssa_edge_expr (at_stmt, rhs0, &evol, limit);
960 1169018 : if (res == t_true)
961 441070 : *evolution_of_loop = add_to_evolution
962 441070 : (chrec_convert (type, evol, at_stmt), rhs1, at_stmt);
963 727948 : else if (res == t_false)
964 : {
965 707623 : res = follow_ssa_edge_expr
966 707623 : (at_stmt, rhs1, evolution_of_loop, limit);
967 707623 : if (res == t_true)
968 532452 : *evolution_of_loop = add_to_evolution
969 532452 : (chrec_convert (type, *evolution_of_loop, at_stmt),
970 : rhs0, at_stmt);
971 : }
972 : }
973 :
974 : else
975 0 : gcc_unreachable (); /* Handled in caller. */
976 : }
977 :
978 23466 : else if (TREE_CODE (rhs1) == SSA_NAME)
979 : {
980 : /* Match an assignment under the form:
981 : "a = ... + c". */
982 10114 : res = follow_ssa_edge_expr (at_stmt, rhs1, evolution_of_loop, limit);
983 10114 : if (res == t_true)
984 9372 : *evolution_of_loop = add_to_evolution
985 9372 : (chrec_convert (type, *evolution_of_loop, at_stmt),
986 : rhs0, at_stmt);
987 : }
988 :
989 : else
990 : /* Otherwise, match an assignment under the form:
991 : "a = ... + ...". */
992 : /* And there is nothing to do. */
993 : res = t_false;
994 : break;
995 :
996 : default:
997 : res = t_false;
998 : }
999 :
1000 1192484 : return res;
1001 : }
1002 :
1003 : /* Checks whether the I-th argument of a PHI comes from a backedge. */
1004 :
1005 : static bool
1006 9226920 : backedge_phi_arg_p (gphi *phi, int i)
1007 : {
1008 9226920 : const_edge e = gimple_phi_arg_edge (phi, i);
1009 :
1010 : /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
1011 : about updating it anywhere, and this should work as well most of the
1012 : time. */
1013 9226920 : if (e->flags & EDGE_IRREDUCIBLE_LOOP)
1014 59202 : return true;
1015 :
1016 : return false;
1017 : }
1018 :
1019 : /* Helper function for one branch of the condition-phi-node. Return
1020 : true if the strongly connected component has been found following
1021 : this path. */
1022 :
1023 : t_bool
1024 3800425 : scev_dfs::follow_ssa_edge_in_condition_phi_branch (int i,
1025 : gphi *condition_phi,
1026 : tree *evolution_of_branch,
1027 : tree init_cond, int limit)
1028 : {
1029 3800425 : tree branch = PHI_ARG_DEF (condition_phi, i);
1030 3800425 : *evolution_of_branch = chrec_dont_know;
1031 :
1032 : /* Do not follow back edges (they must belong to an irreducible loop, which
1033 : we really do not want to worry about). */
1034 3800425 : if (backedge_phi_arg_p (condition_phi, i))
1035 : return t_false;
1036 :
1037 3791573 : if (TREE_CODE (branch) == SSA_NAME)
1038 : {
1039 3532676 : *evolution_of_branch = init_cond;
1040 3532676 : return follow_ssa_edge_expr (condition_phi, branch,
1041 3532676 : evolution_of_branch, limit);
1042 : }
1043 :
1044 : /* This case occurs when one of the condition branches sets
1045 : the variable to a constant: i.e. a phi-node like
1046 : "a_2 = PHI <a_7(5), 2(6)>;".
1047 :
1048 : FIXME: This case have to be refined correctly:
1049 : in some cases it is possible to say something better than
1050 : chrec_dont_know, for example using a wrap-around notation. */
1051 : return t_false;
1052 : }
1053 :
1054 : /* This function merges the branches of a condition-phi-node in a
1055 : loop. */
1056 :
1057 : t_bool
1058 2399497 : scev_dfs::follow_ssa_edge_in_condition_phi (gphi *condition_phi,
1059 : tree *evolution_of_loop, int limit)
1060 : {
1061 2399497 : int i, n;
1062 2399497 : tree init = *evolution_of_loop;
1063 2399497 : tree evolution_of_branch;
1064 2399497 : t_bool res = follow_ssa_edge_in_condition_phi_branch (0, condition_phi,
1065 : &evolution_of_branch,
1066 : init, limit);
1067 2399497 : if (res == t_false || res == t_dont_know)
1068 : return res;
1069 :
1070 1370952 : *evolution_of_loop = evolution_of_branch;
1071 :
1072 1370952 : n = gimple_phi_num_args (condition_phi);
1073 1972495 : for (i = 1; i < n; i++)
1074 : {
1075 : /* Quickly give up when the evolution of one of the branches is
1076 : not known. */
1077 1554090 : if (*evolution_of_loop == chrec_dont_know)
1078 : return t_true;
1079 :
1080 : /* Increase the limit by the PHI argument number to avoid exponential
1081 : time and memory complexity. */
1082 1400928 : res = follow_ssa_edge_in_condition_phi_branch (i, condition_phi,
1083 : &evolution_of_branch,
1084 : init, limit + i);
1085 1400928 : if (res == t_false || res == t_dont_know)
1086 : return res;
1087 :
1088 601543 : *evolution_of_loop = chrec_merge (*evolution_of_loop,
1089 : evolution_of_branch);
1090 : }
1091 :
1092 : return t_true;
1093 : }
1094 :
1095 : /* Follow an SSA edge in an inner loop. It computes the overall
1096 : effect of the loop, and following the symbolic initial conditions,
1097 : it follows the edges in the parent loop. The inner loop is
1098 : considered as a single statement. */
1099 :
1100 : t_bool
1101 305635 : scev_dfs::follow_ssa_edge_inner_loop_phi (gphi *loop_phi_node,
1102 : tree *evolution_of_loop, int limit)
1103 : {
1104 305635 : class loop *loop = loop_containing_stmt (loop_phi_node);
1105 305635 : tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node));
1106 :
1107 : /* Sometimes, the inner loop is too difficult to analyze, and the
1108 : result of the analysis is a symbolic parameter. */
1109 305635 : if (ev == PHI_RESULT (loop_phi_node))
1110 : {
1111 115240 : t_bool res = t_false;
1112 115240 : int i, n = gimple_phi_num_args (loop_phi_node);
1113 :
1114 169579 : for (i = 0; i < n; i++)
1115 : {
1116 160149 : tree arg = PHI_ARG_DEF (loop_phi_node, i);
1117 160149 : basic_block bb;
1118 :
1119 : /* Follow the edges that exit the inner loop. */
1120 160149 : bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1121 160149 : if (!flow_bb_inside_loop_p (loop, bb))
1122 115240 : res = follow_ssa_edge_expr (loop_phi_node,
1123 : arg, evolution_of_loop, limit);
1124 160149 : if (res == t_true)
1125 : break;
1126 : }
1127 :
1128 : /* If the path crosses this loop-phi, give up. */
1129 115240 : if (res == t_true)
1130 105810 : *evolution_of_loop = chrec_dont_know;
1131 :
1132 : return res;
1133 : }
1134 :
1135 : /* Otherwise, compute the overall effect of the inner loop. */
1136 190395 : ev = compute_overall_effect_of_inner_loop (loop, ev);
1137 190395 : return follow_ssa_edge_expr (loop_phi_node, ev, evolution_of_loop, limit);
1138 : }
1139 :
1140 : /* Follow the ssa edge into the expression EXPR.
1141 : Return true if the strongly connected component has been found. */
1142 :
1143 : t_bool
1144 26589760 : scev_dfs::follow_ssa_edge_expr (gimple *at_stmt, tree expr,
1145 : tree *evolution_of_loop, int limit)
1146 : {
1147 26589760 : gphi *halting_phi = loop_phi_node;
1148 26589760 : enum tree_code code;
1149 26589760 : tree type, rhs0, rhs1 = NULL_TREE;
1150 :
1151 : /* The EXPR is one of the following cases:
1152 : - an SSA_NAME,
1153 : - an INTEGER_CST,
1154 : - a PLUS_EXPR,
1155 : - a POINTER_PLUS_EXPR,
1156 : - a MINUS_EXPR,
1157 : - other cases are not yet handled. */
1158 :
1159 : /* For SSA_NAME look at the definition statement, handling
1160 : PHI nodes and otherwise expand appropriately for the expression
1161 : handling below. */
1162 26589760 : if (TREE_CODE (expr) == SSA_NAME)
1163 : {
1164 26386128 : gimple *def = SSA_NAME_DEF_STMT (expr);
1165 :
1166 26386128 : if (gimple_nop_p (def))
1167 : return t_false;
1168 :
1169 : /* Give up if the path is longer than the MAX that we allow. */
1170 26369644 : if (limit > param_scev_max_expr_complexity)
1171 : {
1172 8415 : *evolution_of_loop = chrec_dont_know;
1173 8415 : return t_dont_know;
1174 : }
1175 :
1176 26361229 : if (gphi *phi = dyn_cast <gphi *>(def))
1177 : {
1178 27314326 : if (!loop_phi_node_p (phi))
1179 : /* DEF is a condition-phi-node. Follow the branches, and
1180 : record their evolutions. Finally, merge the collected
1181 : information and set the approximation to the main
1182 : variable. */
1183 2399497 : return follow_ssa_edge_in_condition_phi (phi, evolution_of_loop,
1184 2399497 : limit);
1185 :
1186 : /* When the analyzed phi is the halting_phi, the
1187 : depth-first search is over: we have found a path from
1188 : the halting_phi to itself in the loop. */
1189 11257666 : if (phi == halting_phi)
1190 : {
1191 10702293 : *evolution_of_loop = expr;
1192 10702293 : return t_true;
1193 : }
1194 :
1195 : /* Otherwise, the evolution of the HALTING_PHI depends
1196 : on the evolution of another loop-phi-node, i.e. the
1197 : evolution function is a higher degree polynomial. */
1198 555373 : class loop *def_loop = loop_containing_stmt (def);
1199 555373 : if (def_loop == loop)
1200 : return t_false;
1201 :
1202 : /* Inner loop. */
1203 330934 : if (flow_loop_nested_p (loop, def_loop))
1204 305635 : return follow_ssa_edge_inner_loop_phi (phi, evolution_of_loop,
1205 305635 : limit + 1);
1206 :
1207 : /* Outer loop. */
1208 : return t_false;
1209 : }
1210 :
1211 : /* At this level of abstraction, the program is just a set
1212 : of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1213 : other def to be handled. */
1214 12704066 : gassign *assign = dyn_cast<gassign *> (def);
1215 12587333 : if (!assign)
1216 : return t_false;
1217 :
1218 12587333 : code = gimple_assign_rhs_code (assign);
1219 12587333 : switch (get_gimple_rhs_class (code))
1220 : {
1221 10920374 : case GIMPLE_BINARY_RHS:
1222 10920374 : rhs0 = gimple_assign_rhs1 (assign);
1223 10920374 : rhs1 = gimple_assign_rhs2 (assign);
1224 : break;
1225 1634867 : case GIMPLE_UNARY_RHS:
1226 1634867 : case GIMPLE_SINGLE_RHS:
1227 1634867 : rhs0 = gimple_assign_rhs1 (assign);
1228 1634867 : break;
1229 : default:
1230 : return t_false;
1231 : }
1232 12555241 : type = TREE_TYPE (gimple_assign_lhs (assign));
1233 12555241 : at_stmt = def;
1234 : }
1235 : else
1236 : {
1237 203632 : code = TREE_CODE (expr);
1238 203632 : type = TREE_TYPE (expr);
1239 : /* Via follow_ssa_edge_inner_loop_phi we arrive here with the
1240 : GENERIC scalar evolution of the inner loop. */
1241 203632 : switch (code)
1242 : {
1243 11962 : CASE_CONVERT:
1244 11962 : rhs0 = TREE_OPERAND (expr, 0);
1245 11962 : break;
1246 30442 : case POINTER_PLUS_EXPR:
1247 30442 : case PLUS_EXPR:
1248 30442 : case MINUS_EXPR:
1249 30442 : rhs0 = TREE_OPERAND (expr, 0);
1250 30442 : rhs1 = TREE_OPERAND (expr, 1);
1251 30442 : STRIP_USELESS_TYPE_CONVERSION (rhs0);
1252 30442 : STRIP_USELESS_TYPE_CONVERSION (rhs1);
1253 30442 : break;
1254 : default:
1255 : rhs0 = expr;
1256 : }
1257 : }
1258 :
1259 12758873 : switch (code)
1260 : {
1261 374966 : CASE_CONVERT:
1262 374966 : {
1263 : /* This assignment is under the form "a_1 = (cast) rhs. We cannot
1264 : validate any precision altering conversion during the SCC
1265 : analysis, so don't even try. */
1266 374966 : if (!tree_nop_conversion_p (type, TREE_TYPE (rhs0)))
1267 : return t_false;
1268 243470 : t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
1269 : evolution_of_loop, limit);
1270 243470 : if (res == t_true)
1271 96042 : *evolution_of_loop = chrec_convert (type, *evolution_of_loop,
1272 : at_stmt);
1273 : return res;
1274 : }
1275 :
1276 : case INTEGER_CST:
1277 : /* This assignment is under the form "a_1 = 7". */
1278 : return t_false;
1279 :
1280 2208 : case ADDR_EXPR:
1281 2208 : {
1282 : /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1283 2208 : if (TREE_CODE (TREE_OPERAND (rhs0, 0)) != MEM_REF)
1284 : return t_false;
1285 0 : tree mem = TREE_OPERAND (rhs0, 0);
1286 0 : rhs0 = TREE_OPERAND (mem, 0);
1287 0 : rhs1 = TREE_OPERAND (mem, 1);
1288 0 : code = POINTER_PLUS_EXPR;
1289 : }
1290 : /* Fallthru. */
1291 9206261 : case POINTER_PLUS_EXPR:
1292 9206261 : case PLUS_EXPR:
1293 : /* This case is under the form "rhs0 +- rhs1". */
1294 9206261 : if (TREE_CODE (rhs0) == SSA_NAME && TREE_CODE (rhs1) != SSA_NAME)
1295 : {
1296 : /* Match an assignment under the form:
1297 : "a = b +- ...". */
1298 8013777 : t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
1299 : evolution_of_loop, limit);
1300 8013777 : if (res == t_true)
1301 7787597 : *evolution_of_loop = add_to_evolution
1302 7787597 : (chrec_convert (type, *evolution_of_loop, at_stmt),
1303 : rhs1, at_stmt);
1304 : return res;
1305 : }
1306 : /* Else search for the SCC in both rhs0 and rhs1. */
1307 1192484 : return follow_ssa_edge_binary (at_stmt, type, rhs0, code, rhs1,
1308 1192484 : evolution_of_loop, limit);
1309 :
1310 :
1311 903919 : case MINUS_EXPR:
1312 : /* This case is under the form "rhs0 - rhs1". */
1313 903919 : if (TREE_CODE (rhs0) == SSA_NAME)
1314 : {
1315 : /* Match an assignment under the form:
1316 : "a = b +- ...". */
1317 897406 : t_bool res = follow_ssa_edge_expr (at_stmt, rhs0,
1318 : evolution_of_loop, limit);
1319 897406 : if (res != t_true)
1320 : return res;
1321 : /* We have to avoid negating INT_MIN given that a) invokes UB,
1322 : b) results in a wrong scev_direction. See PR126171. */
1323 1698718 : if (INTEGRAL_TYPE_P (type)
1324 844336 : && TYPE_OVERFLOW_UNDEFINED (type)
1325 928457 : && !expr_not_equal_to (rhs1,
1326 928457 : wi::to_wide (TYPE_MIN_VALUE (type))))
1327 : {
1328 47035 : tree utype = unsigned_type_for (type);
1329 47035 : tree to_add = chrec_convert_rhs (utype, rhs1);
1330 47035 : to_add = chrec_fold_multiply (utype, to_add,
1331 : build_int_cst_type (utype, -1));
1332 47035 : *evolution_of_loop
1333 47035 : = chrec_convert (utype, *evolution_of_loop, at_stmt);
1334 47035 : *evolution_of_loop = add_to_evolution (*evolution_of_loop,
1335 : to_add, at_stmt);
1336 47035 : *evolution_of_loop
1337 47035 : = chrec_convert (type, *evolution_of_loop, at_stmt);
1338 : }
1339 : else
1340 : {
1341 802324 : tree to_add = chrec_fold_multiply (type, rhs1,
1342 : build_minus_one_cst (type));
1343 802324 : *evolution_of_loop
1344 802324 : = add_to_evolution (chrec_convert (type, *evolution_of_loop,
1345 : at_stmt),
1346 : to_add, at_stmt);
1347 : }
1348 849359 : return res;
1349 : }
1350 : /* There is nothing to do. */
1351 : return t_false;
1352 :
1353 : default:
1354 : return t_false;
1355 : }
1356 : }
1357 :
1358 :
1359 : /* This section selects the loops that will be good candidates for the
1360 : scalar evolution analysis. For the moment, greedily select all the
1361 : loop nests we could analyze. */
1362 :
1363 : /* For a loop with a single exit edge, return the COND_EXPR that
1364 : guards the exit edge. If the expression is too difficult to
1365 : analyze, then give up. */
1366 :
1367 : gcond *
1368 347 : get_loop_exit_condition (const class loop *loop)
1369 : {
1370 347 : return get_loop_exit_condition (single_exit (loop));
1371 : }
1372 :
1373 : /* If the statement just before the EXIT_EDGE contains a condition then
1374 : return the condition, otherwise NULL. */
1375 :
1376 : gcond *
1377 5342971 : get_loop_exit_condition (const_edge exit_edge)
1378 : {
1379 5342971 : gcond *res = NULL;
1380 :
1381 5342971 : if (dump_file && (dump_flags & TDF_SCEV))
1382 2 : fprintf (dump_file, "(get_loop_exit_condition \n ");
1383 :
1384 5342971 : if (exit_edge)
1385 10685942 : res = safe_dyn_cast <gcond *> (*gsi_last_bb (exit_edge->src));
1386 :
1387 5342971 : if (dump_file && (dump_flags & TDF_SCEV))
1388 : {
1389 2 : print_gimple_stmt (dump_file, res, 0);
1390 2 : fprintf (dump_file, ")\n");
1391 : }
1392 :
1393 5342971 : return res;
1394 : }
1395 :
1396 :
1397 : /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1398 : Handle below case and return the corresponding POLYNOMIAL_CHREC:
1399 :
1400 : # i_17 = PHI <i_13(5), 0(3)>
1401 : # _20 = PHI <_5(5), start_4(D)(3)>
1402 : ...
1403 : i_13 = i_17 + 1;
1404 : _5 = start_4(D) + i_13;
1405 :
1406 : Though variable _20 appears as a PEELED_CHREC in the form of
1407 : (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1408 :
1409 : See PR41488. */
1410 :
1411 : static tree
1412 1709459 : simplify_peeled_chrec (class loop *loop, tree arg, tree init_cond,
1413 : tree *nowrap_bound = nullptr)
1414 : {
1415 3418918 : aff_tree aff1, aff2;
1416 1709459 : tree ev, left, right, type, step_val;
1417 1709459 : hash_map<tree, name_expansion *> *peeled_chrec_map = NULL;
1418 :
1419 1709459 : tree local_nowrap_bound = NULL_TREE;
1420 1709459 : tree *bound = nullptr;
1421 1709459 : if (nowrap_bound)
1422 4925 : bound = &local_nowrap_bound;
1423 :
1424 1709459 : ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg));
1425 1709459 : if (ev == NULL_TREE)
1426 0 : return chrec_dont_know;
1427 :
1428 : /* Support the case where we can derive the original CHREC from the
1429 : peeled one if that's a converted other IV. This can be done
1430 : when the original unpeeled converted IV does not overflow and
1431 : has the same initial value. */
1432 1698409 : if (CONVERT_EXPR_P (ev)
1433 11050 : && TREE_CODE (init_cond) == INTEGER_CST
1434 3685 : && TREE_CODE (TREE_OPERAND (ev, 0)) == POLYNOMIAL_CHREC
1435 3421 : && (TYPE_PRECISION (TREE_TYPE (ev))
1436 3421 : > TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (ev, 0))))
1437 1712720 : && (!TYPE_UNSIGNED (TREE_TYPE (ev))
1438 3255 : || TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (ev, 0)))))
1439 : {
1440 3261 : left = CHREC_LEFT (TREE_OPERAND (ev, 0));
1441 3261 : right = CHREC_RIGHT (TREE_OPERAND (ev, 0));
1442 3261 : tree left_before = chrec_fold_minus (TREE_TYPE (TREE_OPERAND (ev, 0)),
1443 : left, right);
1444 3261 : if (TREE_CODE (left_before) == INTEGER_CST
1445 3260 : && wi::to_widest (init_cond) == wi::to_widest (left_before)
1446 6521 : && !scev_probably_wraps_p (NULL_TREE, left_before, right, NULL,
1447 : loop, false, bound))
1448 : {
1449 328 : tree tp = TREE_TYPE (right);
1450 :
1451 : /* We need a sign-extension to make things like
1452 : u8(6, 4, 2) => i32(6, 4, 2), instead of i32(6, 260, 514). */
1453 328 : if (TYPE_UNSIGNED (tp))
1454 328 : right = fold_convert (signed_type_for (tp), right);
1455 :
1456 328 : scev_combine_nowrap_bounds (nowrap_bound, local_nowrap_bound);
1457 328 : return build_polynomial_chrec
1458 328 : (loop->num, init_cond, chrec_convert (TREE_TYPE (ev), right, NULL,
1459 : nullptr, false,
1460 328 : NULL_TREE));
1461 : }
1462 2933 : return chrec_dont_know;
1463 : }
1464 :
1465 1706198 : if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
1466 1662427 : return chrec_dont_know;
1467 :
1468 43771 : left = CHREC_LEFT (ev);
1469 43771 : right = CHREC_RIGHT (ev);
1470 43771 : type = TREE_TYPE (left);
1471 43771 : step_val = chrec_fold_plus (type, init_cond, right);
1472 :
1473 : /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1474 : if "left" equals to "init + right". */
1475 43771 : if (operand_equal_p (left, step_val, 0)
1476 43771 : && ((!POINTER_TYPE_P (type) && !INTEGRAL_TYPE_P (type))
1477 22207 : || TYPE_OVERFLOW_WRAPS (type)
1478 : /* When overflow in the type doesn't wrap, make sure the
1479 : resulting CHREC does not either. */
1480 3831 : || !scev_probably_wraps_p (NULL_TREE, init_cond, right, NULL,
1481 : loop, false)))
1482 : {
1483 20379 : if (dump_file && (dump_flags & TDF_SCEV))
1484 1 : fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1485 :
1486 20379 : scev_combine_nowrap_bounds (nowrap_bound, local_nowrap_bound);
1487 20379 : return build_polynomial_chrec (loop->num, init_cond, right);
1488 : }
1489 :
1490 : /* The affine code only deals with pointer and integer types. */
1491 23392 : if (!POINTER_TYPE_P (type)
1492 18160 : && !INTEGRAL_TYPE_P (type))
1493 13 : return chrec_dont_know;
1494 :
1495 : /* Try harder to check if they are equal. */
1496 23379 : tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map);
1497 23379 : tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map);
1498 23379 : free_affine_expand_cache (&peeled_chrec_map);
1499 23379 : aff_combination_scale (&aff2, -1);
1500 23379 : aff_combination_add (&aff1, &aff2);
1501 :
1502 : /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1503 : if "left" equals to "init + right". */
1504 23379 : if (aff_combination_zero_p (&aff1)
1505 23379 : && ((!POINTER_TYPE_P (type) && !INTEGRAL_TYPE_P (type))
1506 15591 : || TYPE_OVERFLOW_WRAPS (type)
1507 : /* When overflow in the type doesn't wrap, make sure the
1508 : resulting CHREC does not either. */
1509 11263 : || !scev_probably_wraps_p (NULL_TREE, init_cond, right, NULL,
1510 : loop, false)))
1511 : {
1512 4375 : if (dump_file && (dump_flags & TDF_SCEV))
1513 1 : fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1514 :
1515 4375 : scev_combine_nowrap_bounds (nowrap_bound, local_nowrap_bound);
1516 4375 : return build_polynomial_chrec (loop->num, init_cond, right);
1517 : }
1518 19004 : return chrec_dont_know;
1519 1709459 : }
1520 :
1521 : /* Given a LOOP_PHI_NODE, this function determines the evolution
1522 : function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1523 :
1524 : static tree
1525 11837674 : analyze_evolution_in_loop (gphi *loop_phi_node,
1526 : tree init_cond,
1527 : tree *nowrap_bound = nullptr)
1528 : {
1529 11837674 : int i, n = gimple_phi_num_args (loop_phi_node);
1530 11837674 : tree evolution_function = chrec_not_analyzed_yet;
1531 11837674 : class loop *loop = loop_containing_stmt (loop_phi_node);
1532 11837674 : basic_block bb;
1533 11837674 : static bool simplify_peeled_chrec_p = true;
1534 :
1535 11837674 : if (dump_file && (dump_flags & TDF_SCEV))
1536 : {
1537 3 : fprintf (dump_file, "(analyze_evolution_in_loop \n");
1538 3 : fprintf (dump_file, " (loop_phi_node = ");
1539 3 : print_gimple_stmt (dump_file, loop_phi_node, 0);
1540 3 : fprintf (dump_file, ")\n");
1541 : }
1542 :
1543 31207914 : for (i = 0; i < n; i++)
1544 : {
1545 22264518 : tree arg = PHI_ARG_DEF (loop_phi_node, i);
1546 22264518 : tree ev_fn = chrec_dont_know;
1547 22264518 : t_bool res;
1548 :
1549 : /* Select the edges that enter the loop body. */
1550 22264518 : bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1551 22264518 : if (!flow_bb_inside_loop_p (loop, bb))
1552 10426844 : continue;
1553 :
1554 11837674 : if (TREE_CODE (arg) == SSA_NAME)
1555 : {
1556 11710041 : bool val = false;
1557 :
1558 : /* Pass in the initial condition to the follow edge function. */
1559 11710041 : scev_dfs dfs (loop, loop_phi_node, init_cond);
1560 11710041 : res = dfs.get_ev (&ev_fn, arg);
1561 :
1562 : /* If ev_fn has no evolution in the inner loop, and the
1563 : init_cond is not equal to ev_fn, then we have an
1564 : ambiguity between two possible values, as we cannot know
1565 : the number of iterations at this point. */
1566 11710041 : if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC
1567 2767915 : && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val
1568 11710041 : && !operand_equal_p (init_cond, ev_fn, 0))
1569 0 : ev_fn = chrec_dont_know;
1570 : }
1571 : else
1572 : res = t_false;
1573 :
1574 : /* When it is impossible to go back on the same
1575 : loop_phi_node by following the ssa edges, the
1576 : evolution is represented by a peeled chrec, i.e. the
1577 : first iteration, EV_FN has the value INIT_COND, then
1578 : all the other iterations it has the value of ARG.
1579 : For the moment, PEELED_CHREC nodes are not built. */
1580 11710041 : if (res != t_true)
1581 : {
1582 2536309 : ev_fn = chrec_dont_know;
1583 : /* Try to recognize POLYNOMIAL_CHREC which appears in
1584 : the form of PEELED_CHREC, but guard the process with
1585 : a bool variable to keep the analyzer from infinite
1586 : recurrence for real PEELED_RECs. */
1587 2536309 : if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME)
1588 : {
1589 1709459 : simplify_peeled_chrec_p = false;
1590 3418918 : ev_fn = simplify_peeled_chrec
1591 1709459 : (loop, arg, init_cond, nowrap_bound);
1592 1709459 : simplify_peeled_chrec_p = true;
1593 : }
1594 : }
1595 :
1596 : /* When there are multiple back edges of the loop (which in fact never
1597 : happens currently, but nevertheless), merge their evolutions. */
1598 11837674 : evolution_function = chrec_merge (evolution_function, ev_fn);
1599 :
1600 11837674 : if (evolution_function == chrec_dont_know)
1601 : break;
1602 : }
1603 :
1604 11837674 : if (dump_file && (dump_flags & TDF_SCEV))
1605 : {
1606 3 : fprintf (dump_file, " (evolution_function = ");
1607 3 : print_generic_expr (dump_file, evolution_function);
1608 3 : fprintf (dump_file, "))\n");
1609 : }
1610 :
1611 11837674 : return evolution_function;
1612 : }
1613 :
1614 : /* Looks to see if VAR is a copy of a constant (via straightforward assignments
1615 : or degenerate phi's). If so, returns the constant; else, returns VAR. */
1616 :
1617 : static tree
1618 24221652 : follow_copies_to_constant (tree var)
1619 : {
1620 24221652 : tree res = var;
1621 24221652 : while (TREE_CODE (res) == SSA_NAME
1622 : /* We face not updated SSA form in multiple places and this walk
1623 : may end up in sibling loops so we have to guard it. */
1624 29263756 : && !name_registered_for_update_p (res))
1625 : {
1626 17109981 : gimple *def = SSA_NAME_DEF_STMT (res);
1627 17109981 : if (gphi *phi = dyn_cast <gphi *> (def))
1628 : {
1629 4283676 : if (tree rhs = degenerate_phi_result (phi))
1630 : res = rhs;
1631 : else
1632 : break;
1633 : }
1634 12826305 : else if (gimple_assign_single_p (def))
1635 : /* Will exit loop if not an SSA_NAME. */
1636 4791513 : res = gimple_assign_rhs1 (def);
1637 : else
1638 : break;
1639 : }
1640 24221652 : if (CONSTANT_CLASS_P (res))
1641 7202746 : return res;
1642 : return var;
1643 : }
1644 :
1645 : /* Given a loop-phi-node, return the initial conditions of the
1646 : variable on entry of the loop. When the CCP has propagated
1647 : constants into the loop-phi-node, the initial condition is
1648 : instantiated, otherwise the initial condition is kept symbolic.
1649 : This analyzer does not analyze the evolution outside the current
1650 : loop, and leaves this task to the on-demand tree reconstructor. */
1651 :
1652 : static tree
1653 11837674 : analyze_initial_condition (gphi *loop_phi_node)
1654 : {
1655 11837674 : int i, n;
1656 11837674 : tree init_cond = chrec_not_analyzed_yet;
1657 11837674 : class loop *loop = loop_containing_stmt (loop_phi_node);
1658 :
1659 11837674 : if (dump_file && (dump_flags & TDF_SCEV))
1660 : {
1661 3 : fprintf (dump_file, "(analyze_initial_condition \n");
1662 3 : fprintf (dump_file, " (loop_phi_node = \n");
1663 3 : print_gimple_stmt (dump_file, loop_phi_node, 0);
1664 3 : fprintf (dump_file, ")\n");
1665 : }
1666 :
1667 11837674 : n = gimple_phi_num_args (loop_phi_node);
1668 35513022 : for (i = 0; i < n; i++)
1669 : {
1670 23675348 : tree branch = PHI_ARG_DEF (loop_phi_node, i);
1671 23675348 : basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src;
1672 :
1673 : /* When the branch is oriented to the loop's body, it does
1674 : not contribute to the initial condition. */
1675 23675348 : if (flow_bb_inside_loop_p (loop, bb))
1676 11837674 : continue;
1677 :
1678 11837674 : if (init_cond == chrec_not_analyzed_yet)
1679 : {
1680 11837674 : init_cond = branch;
1681 11837674 : continue;
1682 : }
1683 :
1684 0 : if (TREE_CODE (branch) == SSA_NAME)
1685 : {
1686 0 : init_cond = chrec_dont_know;
1687 0 : break;
1688 : }
1689 :
1690 0 : init_cond = chrec_merge (init_cond, branch);
1691 : }
1692 :
1693 : /* Ooops -- a loop without an entry??? */
1694 11837674 : if (init_cond == chrec_not_analyzed_yet)
1695 0 : init_cond = chrec_dont_know;
1696 :
1697 : /* We may not have fully constant propagated IL. Handle degenerate PHIs here
1698 : to not miss important early loop unrollings. */
1699 11837674 : init_cond = follow_copies_to_constant (init_cond);
1700 :
1701 11837674 : if (dump_file && (dump_flags & TDF_SCEV))
1702 : {
1703 3 : fprintf (dump_file, " (init_cond = ");
1704 3 : print_generic_expr (dump_file, init_cond);
1705 3 : fprintf (dump_file, "))\n");
1706 : }
1707 :
1708 11837674 : return init_cond;
1709 : }
1710 :
1711 : /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1712 :
1713 : static tree
1714 11837674 : interpret_loop_phi (class loop *loop, gphi *loop_phi_node,
1715 : tree *nowrap_bound = nullptr)
1716 : {
1717 11837674 : class loop *phi_loop = loop_containing_stmt (loop_phi_node);
1718 11837674 : tree init_cond;
1719 :
1720 11837674 : gcc_assert (phi_loop == loop);
1721 :
1722 : /* Otherwise really interpret the loop phi. */
1723 11837674 : init_cond = analyze_initial_condition (loop_phi_node);
1724 11837674 : return analyze_evolution_in_loop (loop_phi_node, init_cond, nowrap_bound);
1725 : }
1726 :
1727 : /* This function merges the branches of a condition-phi-node,
1728 : contained in the outermost loop, and whose arguments are already
1729 : analyzed. */
1730 :
1731 : static tree
1732 2840639 : interpret_condition_phi (class loop *loop, gphi *condition_phi,
1733 : tree *nowrap_bound)
1734 : {
1735 2840639 : int i, n = gimple_phi_num_args (condition_phi);
1736 2840639 : tree res = chrec_not_analyzed_yet;
1737 :
1738 5989643 : for (i = 0; i < n; i++)
1739 : {
1740 5426495 : tree branch_chrec;
1741 :
1742 5426495 : if (backedge_phi_arg_p (condition_phi, i))
1743 : {
1744 50350 : res = chrec_dont_know;
1745 50350 : break;
1746 : }
1747 :
1748 5376145 : branch_chrec = analyze_scalar_evolution
1749 5376145 : (loop, PHI_ARG_DEF (condition_phi, i), nowrap_bound);
1750 :
1751 5376145 : res = chrec_merge (res, branch_chrec);
1752 5376145 : if (res == chrec_dont_know)
1753 : break;
1754 : }
1755 :
1756 2840639 : return res;
1757 : }
1758 :
1759 : /* Interpret the operation RHS1 OP RHS2. If we didn't
1760 : analyze this node before, follow the definitions until ending
1761 : either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1762 : return path, this function propagates evolutions (ala constant copy
1763 : propagation). OPND1 is not a GIMPLE expression because we could
1764 : analyze the effect of an inner loop: see interpret_loop_phi. */
1765 :
1766 : static tree
1767 47819251 : interpret_rhs_expr (class loop *loop, gimple *at_stmt,
1768 : tree type, tree rhs1, enum tree_code code, tree rhs2,
1769 : tree *nowrap_bound = nullptr)
1770 : {
1771 47819251 : tree res, chrec1, chrec2, ctype;
1772 47819251 : gimple *def;
1773 :
1774 47819251 : if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1775 : {
1776 11574276 : if (is_gimple_min_invariant (rhs1))
1777 2551055 : return chrec_convert (type, rhs1, at_stmt);
1778 :
1779 9023221 : if (code == SSA_NAME)
1780 138321 : return chrec_convert (type,
1781 : analyze_scalar_evolution (loop, rhs1,
1782 : nowrap_bound),
1783 138321 : at_stmt, nowrap_bound);
1784 : }
1785 :
1786 45129875 : switch (code)
1787 : {
1788 356723 : case ADDR_EXPR:
1789 356723 : if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF
1790 356723 : || handled_component_p (TREE_OPERAND (rhs1, 0)))
1791 : {
1792 356609 : machine_mode mode;
1793 356609 : poly_int64 bitsize, bitpos;
1794 356609 : int unsignedp, reversep;
1795 356609 : int volatilep = 0;
1796 356609 : tree base, offset;
1797 356609 : tree chrec3;
1798 356609 : tree unitpos;
1799 :
1800 356609 : base = get_inner_reference (TREE_OPERAND (rhs1, 0),
1801 : &bitsize, &bitpos, &offset, &mode,
1802 : &unsignedp, &reversep, &volatilep);
1803 :
1804 356609 : if (TREE_CODE (base) == MEM_REF)
1805 : {
1806 272290 : rhs2 = TREE_OPERAND (base, 1);
1807 272290 : rhs1 = TREE_OPERAND (base, 0);
1808 :
1809 272290 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1810 272290 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1811 272290 : chrec1 = chrec_convert (type, chrec1, at_stmt, nowrap_bound);
1812 272290 : chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt,
1813 : nowrap_bound);
1814 272290 : chrec1 = instantiate_parameters (loop, chrec1);
1815 272290 : chrec2 = instantiate_parameters (loop, chrec2);
1816 272290 : res = chrec_fold_plus (type, chrec1, chrec2);
1817 : }
1818 : else
1819 : {
1820 84319 : chrec1 = analyze_scalar_evolution_for_address_of (loop, base,
1821 : nowrap_bound);
1822 84319 : chrec1 = chrec_convert (type, chrec1, at_stmt, nowrap_bound);
1823 84319 : res = chrec1;
1824 : }
1825 :
1826 356609 : if (offset != NULL_TREE)
1827 : {
1828 157433 : chrec2 = analyze_scalar_evolution (loop, offset, nowrap_bound);
1829 157433 : chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt,
1830 : nowrap_bound);
1831 157433 : chrec2 = instantiate_parameters (loop, chrec2);
1832 157433 : res = chrec_fold_plus (type, res, chrec2);
1833 : }
1834 :
1835 356609 : if (maybe_ne (bitpos, 0))
1836 : {
1837 130176 : unitpos = size_int (exact_div (bitpos, BITS_PER_UNIT));
1838 130176 : chrec3 = analyze_scalar_evolution (loop, unitpos, nowrap_bound);
1839 130176 : chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt,
1840 : nowrap_bound);
1841 130176 : chrec3 = instantiate_parameters (loop, chrec3);
1842 130176 : res = chrec_fold_plus (type, res, chrec3);
1843 : }
1844 : }
1845 : else
1846 114 : res = chrec_dont_know;
1847 : break;
1848 :
1849 3680387 : case POINTER_PLUS_EXPR:
1850 3680387 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1851 3680387 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1852 3680387 : chrec1 = chrec_convert (type, chrec1, at_stmt, nowrap_bound);
1853 3680387 : chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt,
1854 : nowrap_bound);
1855 3680387 : chrec1 = instantiate_parameters (loop, chrec1);
1856 3680387 : chrec2 = instantiate_parameters (loop, chrec2);
1857 3680387 : res = chrec_fold_plus (type, chrec1, chrec2);
1858 3680387 : break;
1859 :
1860 133661 : case POINTER_DIFF_EXPR:
1861 133661 : {
1862 133661 : tree utype = unsigned_type_for (type);
1863 133661 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1864 133661 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1865 133661 : chrec1 = chrec_convert (utype, chrec1, at_stmt, nowrap_bound);
1866 133661 : chrec2 = chrec_convert (utype, chrec2, at_stmt, nowrap_bound);
1867 133661 : chrec1 = instantiate_parameters (loop, chrec1);
1868 133661 : chrec2 = instantiate_parameters (loop, chrec2);
1869 133661 : res = chrec_fold_minus (utype, chrec1, chrec2);
1870 133661 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1871 133661 : break;
1872 : }
1873 :
1874 12562331 : case PLUS_EXPR:
1875 12562331 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1876 12562331 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1877 12562331 : ctype = type;
1878 : /* When the stmt is conditionally executed re-write the CHREC
1879 : into a form that has well-defined behavior on overflow. */
1880 12562331 : if (at_stmt
1881 11350859 : && INTEGRAL_TYPE_P (type)
1882 11255960 : && ! TYPE_OVERFLOW_WRAPS (type)
1883 21111053 : && ! dominated_by_p (CDI_DOMINATORS, loop->latch,
1884 8548722 : gimple_bb (at_stmt)))
1885 767138 : ctype = unsigned_type_for (type);
1886 12562331 : chrec1 = chrec_convert (ctype, chrec1, at_stmt, nowrap_bound);
1887 12562331 : chrec2 = chrec_convert (ctype, chrec2, at_stmt, nowrap_bound);
1888 12562331 : chrec1 = instantiate_parameters (loop, chrec1);
1889 12562331 : chrec2 = instantiate_parameters (loop, chrec2);
1890 12562331 : res = chrec_fold_plus (ctype, chrec1, chrec2);
1891 12562331 : if (type != ctype)
1892 767138 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1893 : break;
1894 :
1895 1549560 : case MINUS_EXPR:
1896 1549560 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1897 1549560 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1898 1549560 : ctype = type;
1899 : /* When the stmt is conditionally executed re-write the CHREC
1900 : into a form that has well-defined behavior on overflow. */
1901 1549560 : if (at_stmt
1902 1473313 : && INTEGRAL_TYPE_P (type)
1903 1435329 : && ! TYPE_OVERFLOW_WRAPS (type)
1904 2101094 : && ! dominated_by_p (CDI_DOMINATORS,
1905 551534 : loop->latch, gimple_bb (at_stmt)))
1906 141850 : ctype = unsigned_type_for (type);
1907 1549560 : chrec1 = chrec_convert (ctype, chrec1, at_stmt, nowrap_bound);
1908 1549560 : chrec2 = chrec_convert (ctype, chrec2, at_stmt, nowrap_bound);
1909 1549560 : chrec1 = instantiate_parameters (loop, chrec1);
1910 1549560 : chrec2 = instantiate_parameters (loop, chrec2);
1911 1549560 : res = chrec_fold_minus (ctype, chrec1, chrec2);
1912 1549560 : if (type != ctype)
1913 141850 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1914 : break;
1915 :
1916 78111 : case NEGATE_EXPR:
1917 78111 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1918 78111 : ctype = type;
1919 : /* When the stmt is conditionally executed re-write the CHREC
1920 : into a form that has well-defined behavior on overflow. */
1921 78111 : if (at_stmt
1922 70886 : && INTEGRAL_TYPE_P (type)
1923 68943 : && ! TYPE_OVERFLOW_WRAPS (type)
1924 118196 : && ! dominated_by_p (CDI_DOMINATORS,
1925 40085 : loop->latch, gimple_bb (at_stmt)))
1926 5894 : ctype = unsigned_type_for (type);
1927 78111 : chrec1 = chrec_convert (ctype, chrec1, at_stmt, nowrap_bound);
1928 : /* TYPE may be integer, real or complex, so use fold_convert. */
1929 78111 : chrec1 = instantiate_parameters (loop, chrec1);
1930 78111 : res = chrec_fold_multiply (ctype, chrec1,
1931 : fold_convert (ctype, integer_minus_one_node));
1932 78111 : if (type != ctype)
1933 5894 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1934 : break;
1935 :
1936 38343 : case BIT_NOT_EXPR:
1937 : /* Handle ~X as -1 - X. */
1938 38343 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1939 38343 : chrec1 = chrec_convert (type, chrec1, at_stmt, nowrap_bound);
1940 38343 : chrec1 = instantiate_parameters (loop, chrec1);
1941 38343 : res = chrec_fold_minus (type,
1942 : fold_convert (type, integer_minus_one_node),
1943 : chrec1);
1944 38343 : break;
1945 :
1946 6508530 : case MULT_EXPR:
1947 6508530 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1948 6508530 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1949 6508530 : ctype = type;
1950 : /* When the stmt is conditionally executed re-write the CHREC
1951 : into a form that has well-defined behavior on overflow. */
1952 6508530 : if (at_stmt
1953 4494757 : && INTEGRAL_TYPE_P (type)
1954 4372357 : && ! TYPE_OVERFLOW_WRAPS (type)
1955 8515465 : && ! dominated_by_p (CDI_DOMINATORS,
1956 2006935 : loop->latch, gimple_bb (at_stmt)))
1957 201330 : ctype = unsigned_type_for (type);
1958 6508530 : chrec1 = chrec_convert (ctype, chrec1, at_stmt, nowrap_bound);
1959 6508530 : chrec2 = chrec_convert (ctype, chrec2, at_stmt, nowrap_bound);
1960 6508530 : chrec1 = instantiate_parameters (loop, chrec1);
1961 6508530 : chrec2 = instantiate_parameters (loop, chrec2);
1962 6508530 : res = chrec_fold_multiply (ctype, chrec1, chrec2);
1963 6508530 : if (type != ctype)
1964 201330 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1965 : break;
1966 :
1967 164776 : case LSHIFT_EXPR:
1968 164776 : {
1969 : /* Handle A<<B as A * (1<<B). */
1970 164776 : tree uns = unsigned_type_for (type);
1971 164776 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
1972 164776 : chrec2 = analyze_scalar_evolution (loop, rhs2, nowrap_bound);
1973 164776 : chrec1 = chrec_convert (uns, chrec1, at_stmt, nowrap_bound);
1974 164776 : chrec1 = instantiate_parameters (loop, chrec1);
1975 164776 : chrec2 = instantiate_parameters (loop, chrec2);
1976 :
1977 164776 : tree one = build_int_cst (uns, 1);
1978 164776 : chrec2 = fold_build2 (LSHIFT_EXPR, uns, one, chrec2);
1979 164776 : res = chrec_fold_multiply (uns, chrec1, chrec2);
1980 164776 : res = chrec_convert (type, res, at_stmt, nowrap_bound);
1981 : }
1982 164776 : break;
1983 :
1984 9111022 : CASE_CONVERT:
1985 : /* In case we have a truncation of a widened operation that in
1986 : the truncated type has undefined overflow behavior analyze
1987 : the operation done in an unsigned type of the same precision
1988 : as the final truncation. We cannot derive a scalar evolution
1989 : for the widened operation but for the truncated result. */
1990 9108282 : if (INTEGRAL_NB_TYPE_P (type)
1991 8775140 : && INTEGRAL_NB_TYPE_P (TREE_TYPE (rhs1))
1992 8112820 : && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1))
1993 558651 : && TYPE_OVERFLOW_UNDEFINED (type)
1994 364096 : && TREE_CODE (rhs1) == SSA_NAME
1995 363840 : && (def = SSA_NAME_DEF_STMT (rhs1))
1996 363840 : && is_gimple_assign (def)
1997 204900 : && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary
1998 9258785 : && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST)
1999 : {
2000 110894 : tree utype = unsigned_type_for (type);
2001 110894 : chrec1 = interpret_rhs_expr (loop, at_stmt, utype,
2002 : gimple_assign_rhs1 (def),
2003 : gimple_assign_rhs_code (def),
2004 : gimple_assign_rhs2 (def),
2005 : nowrap_bound);
2006 : }
2007 : else
2008 9000128 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
2009 9111022 : res = chrec_convert (type, chrec1, at_stmt, nowrap_bound, true, rhs1);
2010 9111022 : break;
2011 :
2012 421763 : case BIT_AND_EXPR:
2013 : /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
2014 : If A is SCEV and its value is in the range of representable set
2015 : of type unsigned short, the result expression is a (no-overflow)
2016 : SCEV. */
2017 421763 : res = chrec_dont_know;
2018 421763 : if (tree_fits_uhwi_p (rhs2))
2019 : {
2020 283802 : int precision;
2021 283802 : unsigned HOST_WIDE_INT val = tree_to_uhwi (rhs2);
2022 :
2023 283802 : val ++;
2024 : /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
2025 : it's not the maximum value of a smaller type than rhs1. */
2026 283802 : if (val != 0
2027 221615 : && (precision = exact_log2 (val)) > 0
2028 505417 : && (unsigned) precision < TYPE_PRECISION (TREE_TYPE (rhs1)))
2029 : {
2030 221615 : tree utype = build_nonstandard_integer_type (precision, 1);
2031 :
2032 221615 : if (TYPE_PRECISION (utype) < TYPE_PRECISION (TREE_TYPE (rhs1)))
2033 : {
2034 221615 : chrec1 = analyze_scalar_evolution (loop, rhs1, nowrap_bound);
2035 221615 : chrec1 = chrec_convert (utype, chrec1, at_stmt,
2036 : nowrap_bound);
2037 221615 : res = chrec_convert (TREE_TYPE (rhs1), chrec1, at_stmt,
2038 : nowrap_bound);
2039 : }
2040 : }
2041 : }
2042 : break;
2043 :
2044 10524668 : default:
2045 10524668 : res = chrec_dont_know;
2046 10524668 : break;
2047 : }
2048 :
2049 : return res;
2050 : }
2051 :
2052 : /* Interpret the expression EXPR. */
2053 :
2054 : static tree
2055 9387080 : interpret_expr (class loop *loop, gimple *at_stmt, tree expr,
2056 : tree *nowrap_bound)
2057 : {
2058 9387080 : enum tree_code code;
2059 9387080 : tree type = TREE_TYPE (expr), op0, op1;
2060 :
2061 9387080 : if (automatically_generated_chrec_p (expr))
2062 : return expr;
2063 :
2064 9381914 : if (TREE_CODE (expr) == POLYNOMIAL_CHREC
2065 9381342 : || TREE_CODE (expr) == CALL_EXPR
2066 18763117 : || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS)
2067 : return chrec_dont_know;
2068 :
2069 9303764 : extract_ops_from_tree (expr, &code, &op0, &op1);
2070 :
2071 9303764 : return interpret_rhs_expr (loop, at_stmt, type,
2072 9303764 : op0, code, op1, nowrap_bound);
2073 : }
2074 :
2075 : /* Interpret the rhs of the assignment STMT. */
2076 :
2077 : static tree
2078 38404593 : interpret_gimple_assign (class loop *loop, gimple *stmt, tree *nowrap_bound)
2079 : {
2080 38404593 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
2081 38404593 : enum tree_code code = gimple_assign_rhs_code (stmt);
2082 :
2083 38404593 : return interpret_rhs_expr (loop, stmt, type,
2084 : gimple_assign_rhs1 (stmt), code,
2085 38404593 : gimple_assign_rhs2 (stmt), nowrap_bound);
2086 : }
2087 :
2088 :
2089 :
2090 : /* This section contains all the entry points:
2091 : - number_of_iterations_in_loop,
2092 : - analyze_scalar_evolution,
2093 : - instantiate_parameters.
2094 : */
2095 :
2096 : /* Helper recursive function. */
2097 :
2098 : static tree
2099 79616317 : analyze_scalar_evolution_1 (class loop *loop, tree var, tree *nowrap_bound)
2100 : {
2101 79616317 : gimple *def;
2102 79616317 : basic_block bb;
2103 79616317 : class loop *def_loop;
2104 79616317 : tree res;
2105 :
2106 79616317 : if (TREE_CODE (var) != SSA_NAME)
2107 9387080 : return interpret_expr (loop, NULL, var, nowrap_bound);
2108 :
2109 70229237 : tree local_nowrap_bound = NULL_TREE;
2110 70229237 : tree *bound = nullptr;
2111 70229237 : if (nowrap_bound)
2112 856432 : bound = &local_nowrap_bound;
2113 :
2114 70229237 : def = SSA_NAME_DEF_STMT (var);
2115 70229237 : bb = gimple_bb (def);
2116 70229237 : def_loop = bb->loop_father;
2117 :
2118 70229237 : if (!flow_bb_inside_loop_p (loop, bb))
2119 : {
2120 : /* Keep symbolic form, but look through obvious copies for constants. */
2121 12383978 : res = follow_copies_to_constant (var);
2122 12383978 : goto set_and_end;
2123 : }
2124 :
2125 57845259 : if (loop != def_loop)
2126 : {
2127 4183976 : res = analyze_scalar_evolution_1 (def_loop, var, bound);
2128 4183976 : class loop *loop_to_skip = superloop_at_depth (def_loop,
2129 4183976 : loop_depth (loop) + 1);
2130 4183976 : res = compute_overall_effect_of_inner_loop (loop_to_skip, res);
2131 4183976 : if (chrec_contains_symbols_defined_in_loop (res, loop->num))
2132 301543 : res = analyze_scalar_evolution_1 (loop, res, bound);
2133 4183976 : goto set_and_end;
2134 : }
2135 :
2136 53661283 : switch (gimple_code (def))
2137 : {
2138 38404593 : case GIMPLE_ASSIGN:
2139 38404593 : res = interpret_gimple_assign (loop, def, bound);
2140 38404593 : break;
2141 :
2142 14678313 : case GIMPLE_PHI:
2143 29356626 : if (loop_phi_node_p (def))
2144 11837674 : res = interpret_loop_phi (loop, as_a <gphi *> (def), bound);
2145 : else
2146 2840639 : res = interpret_condition_phi (loop, as_a <gphi *> (def), bound);
2147 : break;
2148 :
2149 578377 : default:
2150 578377 : res = chrec_dont_know;
2151 578377 : break;
2152 : }
2153 :
2154 70229237 : set_and_end:
2155 :
2156 : /* Keep the symbolic form. */
2157 70229237 : if (res == chrec_dont_know)
2158 26055244 : res = var;
2159 :
2160 70229237 : if (loop == def_loop)
2161 53661283 : set_scalar_evolution (block_before_loop (loop), var, res, bound);
2162 :
2163 70229237 : scev_combine_nowrap_bounds (nowrap_bound, local_nowrap_bound);
2164 :
2165 70229237 : return res;
2166 : }
2167 :
2168 : /* Analyzes and returns the scalar evolution of the ssa_name VAR in
2169 : LOOP. LOOP is the loop in which the variable is used.
2170 :
2171 : Example of use: having a pointer VAR to a SSA_NAME node, STMT a
2172 : pointer to the statement that uses this variable, in order to
2173 : determine the evolution function of the variable, use the following
2174 : calls:
2175 :
2176 : loop_p loop = loop_containing_stmt (stmt);
2177 : tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
2178 : tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2179 : */
2180 :
2181 : tree
2182 205502182 : analyze_scalar_evolution (class loop *loop, tree var, tree *nowrap_bound)
2183 : {
2184 205502182 : tree res;
2185 :
2186 : /* ??? Fix callers. */
2187 205502182 : if (! loop)
2188 : return var;
2189 :
2190 205317152 : if (dump_file && (dump_flags & TDF_SCEV))
2191 : {
2192 38 : fprintf (dump_file, "(analyze_scalar_evolution \n");
2193 38 : fprintf (dump_file, " (loop_nb = %d)\n", loop->num);
2194 38 : fprintf (dump_file, " (scalar = ");
2195 38 : print_generic_expr (dump_file, var);
2196 38 : fprintf (dump_file, ")\n");
2197 : }
2198 :
2199 205317152 : res = get_scalar_evolution (block_before_loop (loop), var, nowrap_bound);
2200 205317152 : if (res == chrec_not_analyzed_yet)
2201 : {
2202 : /* We'll recurse into instantiate_scev, avoid tearing down the
2203 : instantiate cache repeatedly and keep it live from here. */
2204 75130798 : bool destr = false;
2205 75130798 : if (!global_cache)
2206 : {
2207 44457348 : global_cache = new instantiate_cache_type;
2208 44457348 : destr = true;
2209 : }
2210 75130798 : res = analyze_scalar_evolution_1 (loop, var, nowrap_bound);
2211 75130798 : if (destr)
2212 : {
2213 44457348 : delete global_cache;
2214 44457348 : global_cache = NULL;
2215 : }
2216 : }
2217 :
2218 205317152 : if (dump_file && (dump_flags & TDF_SCEV))
2219 38 : fprintf (dump_file, ")\n");
2220 :
2221 : return res;
2222 : }
2223 :
2224 : /* If CHREC doesn't overflow, set the nonwrapping flag. */
2225 :
2226 11207510 : void record_nonwrapping_chrec (tree chrec)
2227 : {
2228 11207510 : CHREC_NOWRAP(chrec) = 1;
2229 :
2230 11207510 : if (dump_file && (dump_flags & TDF_SCEV))
2231 : {
2232 6 : fprintf (dump_file, "(record_nonwrapping_chrec: ");
2233 6 : print_generic_expr (dump_file, chrec);
2234 6 : fprintf (dump_file, ")\n");
2235 : }
2236 11207510 : }
2237 :
2238 : /* Return true if CHREC's nonwrapping flag is set. */
2239 :
2240 226031 : bool nonwrapping_chrec_p (tree chrec)
2241 : {
2242 226031 : if (!chrec || TREE_CODE(chrec) != POLYNOMIAL_CHREC)
2243 : return false;
2244 :
2245 225703 : return CHREC_NOWRAP(chrec);
2246 : }
2247 :
2248 : /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2249 :
2250 : static tree
2251 84319 : analyze_scalar_evolution_for_address_of (class loop *loop, tree var,
2252 : tree *nowrap_bound)
2253 : {
2254 84319 : return analyze_scalar_evolution (loop, build_fold_addr_expr (var),
2255 84319 : nowrap_bound);
2256 : }
2257 :
2258 : /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2259 : WRTO_LOOP (which should be a superloop of USE_LOOP)
2260 :
2261 : FOLDED_CASTS is set to true if resolve_mixers used
2262 : chrec_convert_aggressive (TODO -- not really, we are way too conservative
2263 : at the moment in order to keep things simple).
2264 :
2265 : To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2266 : example:
2267 :
2268 : for (i = 0; i < 100; i++) -- loop 1
2269 : {
2270 : for (j = 0; j < 100; j++) -- loop 2
2271 : {
2272 : k1 = i;
2273 : k2 = j;
2274 :
2275 : use2 (k1, k2);
2276 :
2277 : for (t = 0; t < 100; t++) -- loop 3
2278 : use3 (k1, k2);
2279 :
2280 : }
2281 : use1 (k1, k2);
2282 : }
2283 :
2284 : Both k1 and k2 are invariants in loop3, thus
2285 : analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2286 : analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2287 :
2288 : As they are invariant, it does not matter whether we consider their
2289 : usage in loop 3 or loop 2, hence
2290 : analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2291 : analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2292 : analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2293 : analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2294 :
2295 : Similarly for their evolutions with respect to loop 1. The values of K2
2296 : in the use in loop 2 vary independently on loop 1, thus we cannot express
2297 : the evolution with respect to loop 1:
2298 : analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2299 : analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2300 : analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2301 : analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2302 :
2303 : The value of k2 in the use in loop 1 is known, though:
2304 : analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2305 : analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2306 : */
2307 :
2308 : static tree
2309 52220165 : analyze_scalar_evolution_in_loop (class loop *wrto_loop, class loop *use_loop,
2310 : tree version, bool *folded_casts,
2311 : tree *nowrap_bound = nullptr)
2312 : {
2313 52220165 : bool val = false;
2314 52220165 : tree ev = version, tmp;
2315 :
2316 : /* We cannot just do
2317 :
2318 : tmp = analyze_scalar_evolution (use_loop, version);
2319 : ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2320 :
2321 : as resolve_mixers would query the scalar evolution with respect to
2322 : wrto_loop. For example, in the situation described in the function
2323 : comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2324 : version = k2. Then
2325 :
2326 : analyze_scalar_evolution (use_loop, version) = k2
2327 :
2328 : and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2329 : k2 in loop 1 is 100, which is a wrong result, since we are interested
2330 : in the value in loop 3.
2331 :
2332 : Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2333 : each time checking that there is no evolution in the inner loop. */
2334 :
2335 52220165 : if (folded_casts)
2336 52220165 : *folded_casts = false;
2337 54716145 : while (1)
2338 : {
2339 53468155 : tmp = analyze_scalar_evolution (use_loop, ev, nowrap_bound);
2340 53468155 : ev = resolve_mixers (use_loop, tmp, folded_casts);
2341 :
2342 53468155 : if (use_loop == wrto_loop)
2343 : return ev;
2344 :
2345 : /* If the value of the use changes in the inner loop, we cannot express
2346 : its value in the outer loop (we might try to return interval chrec,
2347 : but we do not have a user for it anyway) */
2348 4437380 : if (!no_evolution_in_loop_p (ev, use_loop->num, &val)
2349 4437380 : || !val)
2350 3189390 : return chrec_dont_know;
2351 :
2352 1247990 : use_loop = loop_outer (use_loop);
2353 : }
2354 : }
2355 :
2356 :
2357 : /* Computes a hash function for database element ELT. */
2358 :
2359 : static inline hashval_t
2360 476573 : hash_idx_scev_info (const void *elt_)
2361 : {
2362 476573 : unsigned idx = ((size_t) elt_) - 2;
2363 476573 : return scev_info_hasher::hash (&global_cache->entries[idx]);
2364 : }
2365 :
2366 : /* Compares database elements E1 and E2. */
2367 :
2368 : static inline int
2369 33659805 : eq_idx_scev_info (const void *e1, const void *e2)
2370 : {
2371 33659805 : unsigned idx1 = ((size_t) e1) - 2;
2372 33659805 : return scev_info_hasher::equal (&global_cache->entries[idx1],
2373 33659805 : (const scev_info_str *) e2);
2374 : }
2375 :
2376 : /* Returns from CACHE the slot number of the cached chrec for NAME. */
2377 :
2378 : static unsigned
2379 66587293 : get_instantiated_value_entry (instantiate_cache_type &cache,
2380 : tree name, edge instantiate_below)
2381 : {
2382 66587293 : if (!cache.map)
2383 : {
2384 30256149 : cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL);
2385 30256149 : cache.entries.create (10);
2386 : }
2387 :
2388 66587293 : scev_info_str e;
2389 66587293 : e.name_version = SSA_NAME_VERSION (name);
2390 66587293 : e.instantiated_below = instantiate_below->dest->index;
2391 66587293 : void **slot = htab_find_slot_with_hash (cache.map, &e,
2392 : scev_info_hasher::hash (&e), INSERT);
2393 66587293 : if (!*slot)
2394 : {
2395 34393090 : e.chrec = chrec_not_analyzed_yet;
2396 34393090 : *slot = (void *)(size_t)(cache.entries.length () + 2);
2397 34393090 : cache.entries.safe_push (e);
2398 : }
2399 :
2400 66587293 : return ((size_t)*slot) - 2;
2401 : }
2402 :
2403 :
2404 : /* Return the closed_loop_phi node for VAR. If there is none, return
2405 : NULL_TREE. */
2406 :
2407 : static tree
2408 2004680 : loop_closed_phi_def (tree var)
2409 : {
2410 2004680 : class loop *loop;
2411 2004680 : edge exit;
2412 2004680 : gphi *phi;
2413 2004680 : gphi_iterator psi;
2414 :
2415 2004680 : if (var == NULL_TREE
2416 2004680 : || TREE_CODE (var) != SSA_NAME)
2417 : return NULL_TREE;
2418 :
2419 2004680 : loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var));
2420 2004680 : exit = single_exit (loop);
2421 2004680 : if (!exit)
2422 : return NULL_TREE;
2423 :
2424 1578039 : for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2425 : {
2426 897081 : phi = psi.phi ();
2427 897081 : if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var)
2428 233494 : return PHI_RESULT (phi);
2429 : }
2430 :
2431 : return NULL_TREE;
2432 : }
2433 :
2434 : static tree instantiate_scev_r (edge, class loop *, class loop *,
2435 : tree, bool *, int);
2436 :
2437 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2438 : and EVOLUTION_LOOP, that were left under a symbolic form.
2439 :
2440 : CHREC is an SSA_NAME to be instantiated.
2441 :
2442 : CACHE is the cache of already instantiated values.
2443 :
2444 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2445 : conversions that may wrap in signed/pointer type are folded, as long
2446 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2447 : then we don't do such fold.
2448 :
2449 : SIZE_EXPR is used for computing the size of the expression to be
2450 : instantiated, and to stop if it exceeds some limit. */
2451 :
2452 : static tree
2453 116211455 : instantiate_scev_name (edge instantiate_below,
2454 : class loop *evolution_loop, class loop *inner_loop,
2455 : tree chrec,
2456 : bool *fold_conversions,
2457 : int size_expr)
2458 : {
2459 116211455 : tree res;
2460 116211455 : class loop *def_loop;
2461 116211455 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec));
2462 :
2463 : /* A parameter, nothing to do. */
2464 116211455 : if (!def_bb
2465 116211455 : || !dominated_by_p (CDI_DOMINATORS, def_bb, instantiate_below->dest))
2466 : return chrec;
2467 :
2468 : /* We cache the value of instantiated variable to avoid exponential
2469 : time complexity due to reevaluations. We also store the convenient
2470 : value in the cache in order to prevent infinite recursion -- we do
2471 : not want to instantiate the SSA_NAME if it is in a mixer
2472 : structure. This is used for avoiding the instantiation of
2473 : recursively defined functions, such as:
2474 :
2475 : | a_2 -> {0, +, 1, +, a_2}_1 */
2476 :
2477 66587293 : unsigned si = get_instantiated_value_entry (*global_cache,
2478 : chrec, instantiate_below);
2479 66587293 : if (global_cache->get (si) != chrec_not_analyzed_yet)
2480 : return global_cache->get (si);
2481 :
2482 : /* On recursion return chrec_dont_know. */
2483 34393090 : global_cache->set (si, chrec_dont_know);
2484 :
2485 34393090 : def_loop = find_common_loop (evolution_loop, def_bb->loop_father);
2486 :
2487 34393090 : if (! dominated_by_p (CDI_DOMINATORS,
2488 34393090 : def_loop->header, instantiate_below->dest))
2489 : {
2490 189716 : gimple *def = SSA_NAME_DEF_STMT (chrec);
2491 189716 : if (gassign *ass = dyn_cast <gassign *> (def))
2492 : {
2493 144281 : switch (gimple_assign_rhs_class (ass))
2494 : {
2495 4764 : case GIMPLE_UNARY_RHS:
2496 4764 : {
2497 4764 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2498 : inner_loop, gimple_assign_rhs1 (ass),
2499 : fold_conversions, size_expr);
2500 4764 : if (op0 == chrec_dont_know)
2501 : return chrec_dont_know;
2502 1283 : res = fold_build1 (gimple_assign_rhs_code (ass),
2503 : TREE_TYPE (chrec), op0);
2504 1283 : break;
2505 : }
2506 52109 : case GIMPLE_BINARY_RHS:
2507 52109 : {
2508 52109 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2509 : inner_loop, gimple_assign_rhs1 (ass),
2510 : fold_conversions, size_expr);
2511 52109 : if (op0 == chrec_dont_know)
2512 : return chrec_dont_know;
2513 11758 : tree op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2514 : inner_loop, gimple_assign_rhs2 (ass),
2515 : fold_conversions, size_expr);
2516 5879 : if (op1 == chrec_dont_know)
2517 : return chrec_dont_know;
2518 1898 : res = fold_build2 (gimple_assign_rhs_code (ass),
2519 : TREE_TYPE (chrec), op0, op1);
2520 1898 : break;
2521 : }
2522 87408 : default:
2523 87408 : res = chrec_dont_know;
2524 : }
2525 : }
2526 : else
2527 45435 : res = chrec_dont_know;
2528 136024 : global_cache->set (si, res);
2529 136024 : return res;
2530 : }
2531 :
2532 : /* If the analysis yields a parametric chrec, instantiate the
2533 : result again. */
2534 34203374 : res = analyze_scalar_evolution (def_loop, chrec);
2535 :
2536 : /* Don't instantiate default definitions. */
2537 34203374 : if (TREE_CODE (res) == SSA_NAME
2538 34203374 : && SSA_NAME_IS_DEFAULT_DEF (res))
2539 : ;
2540 :
2541 : /* Don't instantiate loop-closed-ssa phi nodes. */
2542 34186953 : else if (TREE_CODE (res) == SSA_NAME
2543 100806803 : && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res)))
2544 33310307 : > loop_depth (def_loop))
2545 : {
2546 2030270 : if (res == chrec)
2547 2004680 : res = loop_closed_phi_def (chrec);
2548 : else
2549 : res = chrec;
2550 :
2551 : /* When there is no loop_closed_phi_def, it means that the
2552 : variable is not used after the loop: try to still compute the
2553 : value of the variable when exiting the loop. */
2554 2030270 : if (res == NULL_TREE)
2555 : {
2556 1771186 : loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec));
2557 1771186 : res = analyze_scalar_evolution (loop, chrec);
2558 1771186 : res = compute_overall_effect_of_inner_loop (loop, res);
2559 1771186 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2560 : inner_loop, res,
2561 : fold_conversions, size_expr);
2562 : }
2563 259084 : else if (dominated_by_p (CDI_DOMINATORS,
2564 259084 : gimple_bb (SSA_NAME_DEF_STMT (res)),
2565 259084 : instantiate_below->dest))
2566 259084 : res = chrec_dont_know;
2567 : }
2568 :
2569 32156683 : else if (res != chrec_dont_know)
2570 : {
2571 32156683 : if (inner_loop
2572 1202708 : && def_bb->loop_father != inner_loop
2573 32787817 : && !flow_loop_nested_p (def_bb->loop_father, inner_loop))
2574 : /* ??? We could try to compute the overall effect of the loop here. */
2575 337 : res = chrec_dont_know;
2576 : else
2577 32156346 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2578 : inner_loop, res,
2579 : fold_conversions, size_expr);
2580 : }
2581 :
2582 : /* Store the correct value to the cache. */
2583 34203374 : global_cache->set (si, res);
2584 34203374 : return res;
2585 : }
2586 :
2587 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2588 : and EVOLUTION_LOOP, that were left under a symbolic form.
2589 :
2590 : CHREC is a polynomial chain of recurrence to be instantiated.
2591 :
2592 : CACHE is the cache of already instantiated values.
2593 :
2594 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2595 : conversions that may wrap in signed/pointer type are folded, as long
2596 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2597 : then we don't do such fold.
2598 :
2599 : SIZE_EXPR is used for computing the size of the expression to be
2600 : instantiated, and to stop if it exceeds some limit. */
2601 :
2602 : static tree
2603 63493635 : instantiate_scev_poly (edge instantiate_below,
2604 : class loop *evolution_loop, class loop *,
2605 : tree chrec, bool *fold_conversions, int size_expr)
2606 : {
2607 63493635 : tree op1;
2608 126987270 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2609 : get_chrec_loop (chrec),
2610 63493635 : CHREC_LEFT (chrec), fold_conversions,
2611 : size_expr);
2612 63493635 : if (op0 == chrec_dont_know)
2613 : return chrec_dont_know;
2614 :
2615 126529562 : op1 = instantiate_scev_r (instantiate_below, evolution_loop,
2616 : get_chrec_loop (chrec),
2617 63264781 : CHREC_RIGHT (chrec), fold_conversions,
2618 : size_expr);
2619 63264781 : if (op1 == chrec_dont_know)
2620 : return chrec_dont_know;
2621 :
2622 62565183 : if (CHREC_LEFT (chrec) != op0
2623 62565183 : || CHREC_RIGHT (chrec) != op1)
2624 : {
2625 8225751 : op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL);
2626 8225751 : chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1);
2627 : }
2628 :
2629 : return chrec;
2630 : }
2631 :
2632 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2633 : and EVOLUTION_LOOP, that were left under a symbolic form.
2634 :
2635 : "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2636 :
2637 : CACHE is the cache of already instantiated values.
2638 :
2639 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2640 : conversions that may wrap in signed/pointer type are folded, as long
2641 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2642 : then we don't do such fold.
2643 :
2644 : SIZE_EXPR is used for computing the size of the expression to be
2645 : instantiated, and to stop if it exceeds some limit. */
2646 :
2647 : static tree
2648 25113078 : instantiate_scev_binary (edge instantiate_below,
2649 : class loop *evolution_loop, class loop *inner_loop,
2650 : tree chrec, enum tree_code code,
2651 : tree type, tree c0, tree c1,
2652 : bool *fold_conversions, int size_expr)
2653 : {
2654 25113078 : tree op1;
2655 25113078 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2656 : c0, fold_conversions, size_expr);
2657 25113078 : if (op0 == chrec_dont_know)
2658 : return chrec_dont_know;
2659 :
2660 : /* While we eventually compute the same op1 if c0 == c1 the process
2661 : of doing this is expensive so the following short-cut prevents
2662 : exponential compile-time behavior. */
2663 24741934 : if (c0 != c1)
2664 : {
2665 24720701 : op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop,
2666 : c1, fold_conversions, size_expr);
2667 24720701 : if (op1 == chrec_dont_know)
2668 : return chrec_dont_know;
2669 : }
2670 : else
2671 : op1 = op0;
2672 :
2673 24669610 : if (c0 != op0
2674 24669610 : || c1 != op1)
2675 : {
2676 14164926 : op0 = chrec_convert (type, op0, NULL);
2677 14164926 : op1 = chrec_convert_rhs (type, op1, NULL);
2678 :
2679 14164926 : switch (code)
2680 : {
2681 8270695 : case POINTER_PLUS_EXPR:
2682 8270695 : case PLUS_EXPR:
2683 8270695 : return chrec_fold_plus (type, op0, op1);
2684 :
2685 914601 : case MINUS_EXPR:
2686 914601 : return chrec_fold_minus (type, op0, op1);
2687 :
2688 4979630 : case MULT_EXPR:
2689 4979630 : return chrec_fold_multiply (type, op0, op1);
2690 :
2691 0 : default:
2692 0 : gcc_unreachable ();
2693 : }
2694 : }
2695 :
2696 10504684 : return chrec ? chrec : fold_build2 (code, type, c0, c1);
2697 : }
2698 :
2699 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2700 : and EVOLUTION_LOOP, that were left under a symbolic form.
2701 :
2702 : "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2703 : instantiated.
2704 :
2705 : CACHE is the cache of already instantiated values.
2706 :
2707 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2708 : conversions that may wrap in signed/pointer type are folded, as long
2709 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2710 : then we don't do such fold.
2711 :
2712 : SIZE_EXPR is used for computing the size of the expression to be
2713 : instantiated, and to stop if it exceeds some limit. */
2714 :
2715 : static tree
2716 25954725 : instantiate_scev_convert (edge instantiate_below,
2717 : class loop *evolution_loop, class loop *inner_loop,
2718 : tree chrec, tree type, tree op,
2719 : bool *fold_conversions, int size_expr)
2720 : {
2721 25954725 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2722 : inner_loop, op,
2723 : fold_conversions, size_expr);
2724 :
2725 25954725 : if (op0 == chrec_dont_know)
2726 : return chrec_dont_know;
2727 :
2728 20533241 : if (fold_conversions)
2729 : {
2730 7953028 : tree tmp = chrec_convert_aggressive (type, op0, fold_conversions);
2731 7953028 : if (tmp)
2732 : return tmp;
2733 :
2734 : /* If we used chrec_convert_aggressive, we can no longer assume that
2735 : signed chrecs do not overflow, as chrec_convert does, so avoid
2736 : calling it in that case. */
2737 7402335 : if (*fold_conversions)
2738 : {
2739 15218 : if (chrec && op0 == op)
2740 : return chrec;
2741 :
2742 15218 : return fold_convert (type, op0);
2743 : }
2744 : }
2745 :
2746 19967330 : return chrec_convert (type, op0, NULL);
2747 : }
2748 :
2749 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2750 : and EVOLUTION_LOOP, that were left under a symbolic form.
2751 :
2752 : CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2753 : Handle ~X as -1 - X.
2754 : Handle -X as -1 * X.
2755 :
2756 : CACHE is the cache of already instantiated values.
2757 :
2758 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2759 : conversions that may wrap in signed/pointer type are folded, as long
2760 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2761 : then we don't do such fold.
2762 :
2763 : SIZE_EXPR is used for computing the size of the expression to be
2764 : instantiated, and to stop if it exceeds some limit. */
2765 :
2766 : static tree
2767 289474 : instantiate_scev_not (edge instantiate_below,
2768 : class loop *evolution_loop, class loop *inner_loop,
2769 : tree chrec,
2770 : enum tree_code code, tree type, tree op,
2771 : bool *fold_conversions, int size_expr)
2772 : {
2773 289474 : tree op0 = instantiate_scev_r (instantiate_below, evolution_loop,
2774 : inner_loop, op,
2775 : fold_conversions, size_expr);
2776 :
2777 289474 : if (op0 == chrec_dont_know)
2778 : return chrec_dont_know;
2779 :
2780 221630 : if (op != op0)
2781 : {
2782 156441 : op0 = chrec_convert (type, op0, NULL);
2783 :
2784 156441 : switch (code)
2785 : {
2786 2385 : case BIT_NOT_EXPR:
2787 2385 : return chrec_fold_minus
2788 2385 : (type, fold_convert (type, integer_minus_one_node), op0);
2789 :
2790 154056 : case NEGATE_EXPR:
2791 154056 : return chrec_fold_multiply
2792 154056 : (type, fold_convert (type, integer_minus_one_node), op0);
2793 :
2794 0 : default:
2795 0 : gcc_unreachable ();
2796 : }
2797 : }
2798 :
2799 65189 : return chrec ? chrec : fold_build1 (code, type, op0);
2800 : }
2801 :
2802 : /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2803 : and EVOLUTION_LOOP, that were left under a symbolic form.
2804 :
2805 : CHREC is the scalar evolution to instantiate.
2806 :
2807 : CACHE is the cache of already instantiated values.
2808 :
2809 : Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2810 : conversions that may wrap in signed/pointer type are folded, as long
2811 : as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2812 : then we don't do such fold.
2813 :
2814 : SIZE_EXPR is used for computing the size of the expression to be
2815 : instantiated, and to stop if it exceeds some limit. */
2816 :
2817 : static tree
2818 382813096 : instantiate_scev_r (edge instantiate_below,
2819 : class loop *evolution_loop, class loop *inner_loop,
2820 : tree chrec,
2821 : bool *fold_conversions, int size_expr)
2822 : {
2823 : /* Give up if the expression is larger than the MAX that we allow. */
2824 382813096 : if (size_expr++ > param_scev_max_expr_size)
2825 11 : return chrec_dont_know;
2826 :
2827 382813085 : if (chrec == NULL_TREE
2828 763474498 : || automatically_generated_chrec_p (chrec)
2829 763474487 : || is_gimple_min_invariant (chrec))
2830 : return chrec;
2831 :
2832 231825961 : switch (TREE_CODE (chrec))
2833 : {
2834 116211455 : case SSA_NAME:
2835 116211455 : return instantiate_scev_name (instantiate_below, evolution_loop,
2836 : inner_loop, chrec,
2837 116211455 : fold_conversions, size_expr);
2838 :
2839 63493635 : case POLYNOMIAL_CHREC:
2840 63493635 : return instantiate_scev_poly (instantiate_below, evolution_loop,
2841 : inner_loop, chrec,
2842 63493635 : fold_conversions, size_expr);
2843 :
2844 25113078 : case POINTER_PLUS_EXPR:
2845 25113078 : case PLUS_EXPR:
2846 25113078 : case MINUS_EXPR:
2847 25113078 : case MULT_EXPR:
2848 25113078 : return instantiate_scev_binary (instantiate_below, evolution_loop,
2849 : inner_loop, chrec,
2850 : TREE_CODE (chrec), chrec_type (chrec),
2851 25113078 : TREE_OPERAND (chrec, 0),
2852 25113078 : TREE_OPERAND (chrec, 1),
2853 25113078 : fold_conversions, size_expr);
2854 :
2855 25954725 : CASE_CONVERT:
2856 25954725 : return instantiate_scev_convert (instantiate_below, evolution_loop,
2857 : inner_loop, chrec,
2858 25954725 : TREE_TYPE (chrec), TREE_OPERAND (chrec, 0),
2859 25954725 : fold_conversions, size_expr);
2860 :
2861 289474 : case NEGATE_EXPR:
2862 289474 : case BIT_NOT_EXPR:
2863 289474 : return instantiate_scev_not (instantiate_below, evolution_loop,
2864 : inner_loop, chrec,
2865 289474 : TREE_CODE (chrec), TREE_TYPE (chrec),
2866 289474 : TREE_OPERAND (chrec, 0),
2867 289474 : fold_conversions, size_expr);
2868 :
2869 0 : case ADDR_EXPR:
2870 0 : if (is_gimple_min_invariant (chrec))
2871 : return chrec;
2872 : /* Fallthru. */
2873 0 : case SCEV_NOT_KNOWN:
2874 0 : return chrec_dont_know;
2875 :
2876 0 : case SCEV_KNOWN:
2877 0 : return chrec_known;
2878 :
2879 763594 : default:
2880 763594 : if (CONSTANT_CLASS_P (chrec))
2881 : return chrec;
2882 763594 : return chrec_dont_know;
2883 : }
2884 : }
2885 :
2886 : /* Analyze all the parameters of the chrec that were left under a
2887 : symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2888 : recursive instantiation of parameters: a parameter is a variable
2889 : that is defined in a basic block that dominates INSTANTIATE_BELOW or
2890 : a function parameter. */
2891 :
2892 : tree
2893 92518263 : instantiate_scev (edge instantiate_below, class loop *evolution_loop,
2894 : tree chrec)
2895 : {
2896 92518263 : tree res;
2897 :
2898 92518263 : if (dump_file && (dump_flags & TDF_SCEV))
2899 : {
2900 20 : fprintf (dump_file, "(instantiate_scev \n");
2901 20 : fprintf (dump_file, " (instantiate_below = %d -> %d)\n",
2902 20 : instantiate_below->src->index, instantiate_below->dest->index);
2903 20 : if (evolution_loop)
2904 20 : fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num);
2905 20 : fprintf (dump_file, " (chrec = ");
2906 20 : print_generic_expr (dump_file, chrec);
2907 20 : fprintf (dump_file, ")\n");
2908 : }
2909 :
2910 92518263 : bool destr = false;
2911 92518263 : if (!global_cache)
2912 : {
2913 39436206 : global_cache = new instantiate_cache_type;
2914 39436206 : destr = true;
2915 : }
2916 :
2917 92518263 : res = instantiate_scev_r (instantiate_below, evolution_loop,
2918 : NULL, chrec, NULL, 0);
2919 :
2920 92518263 : if (destr)
2921 : {
2922 39436206 : delete global_cache;
2923 39436206 : global_cache = NULL;
2924 : }
2925 :
2926 92518263 : if (dump_file && (dump_flags & TDF_SCEV))
2927 : {
2928 20 : fprintf (dump_file, " (res = ");
2929 20 : print_generic_expr (dump_file, res);
2930 20 : fprintf (dump_file, "))\n");
2931 : }
2932 :
2933 92518263 : return res;
2934 : }
2935 :
2936 : /* Similar to instantiate_parameters, but does not introduce the
2937 : evolutions in outer loops for LOOP invariants in CHREC, and does not
2938 : care about causing overflows, as long as they do not affect value
2939 : of an expression. */
2940 :
2941 : tree
2942 53468155 : resolve_mixers (class loop *loop, tree chrec, bool *folded_casts)
2943 : {
2944 53468155 : bool destr = false;
2945 53468155 : bool fold_conversions = false;
2946 53468155 : if (!global_cache)
2947 : {
2948 52592072 : global_cache = new instantiate_cache_type;
2949 52592072 : destr = true;
2950 : }
2951 :
2952 53468155 : tree ret = instantiate_scev_r (loop_preheader_edge (loop), loop, NULL,
2953 : chrec, &fold_conversions, 0);
2954 :
2955 53468155 : if (folded_casts && !*folded_casts)
2956 53468155 : *folded_casts = fold_conversions;
2957 :
2958 53468155 : if (destr)
2959 : {
2960 52592072 : delete global_cache;
2961 52592072 : global_cache = NULL;
2962 : }
2963 :
2964 53468155 : return ret;
2965 : }
2966 :
2967 : /* Entry point for the analysis of the number of iterations pass.
2968 : This function tries to safely approximate the number of iterations
2969 : the loop will run. When this property is not decidable at compile
2970 : time, the result is chrec_dont_know. Otherwise the result is a
2971 : scalar or a symbolic parameter. When the number of iterations may
2972 : be equal to zero and the property cannot be determined at compile
2973 : time, the result is a COND_EXPR that represents in a symbolic form
2974 : the conditions under which the number of iterations is not zero.
2975 :
2976 : Example of analysis: suppose that the loop has an exit condition:
2977 :
2978 : "if (b > 49) goto end_loop;"
2979 :
2980 : and that in a previous analysis we have determined that the
2981 : variable 'b' has an evolution function:
2982 :
2983 : "EF = {23, +, 5}_2".
2984 :
2985 : When we evaluate the function at the point 5, i.e. the value of the
2986 : variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2987 : and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2988 : the loop body has been executed 6 times. */
2989 :
2990 : tree
2991 10595315 : number_of_latch_executions (class loop *loop)
2992 : {
2993 10595315 : edge exit;
2994 10595315 : class tree_niter_desc niter_desc;
2995 10595315 : tree may_be_zero;
2996 10595315 : tree res;
2997 :
2998 : /* Determine whether the number of iterations in loop has already
2999 : been computed. */
3000 10595315 : res = loop->nb_iterations;
3001 10595315 : if (res)
3002 : return res;
3003 :
3004 7187616 : may_be_zero = NULL_TREE;
3005 :
3006 7187616 : if (dump_file && (dump_flags & TDF_SCEV))
3007 2 : fprintf (dump_file, "(number_of_iterations_in_loop = \n");
3008 :
3009 7187616 : res = chrec_dont_know;
3010 7187616 : exit = single_exit (loop);
3011 :
3012 7187616 : if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false))
3013 : {
3014 3632009 : may_be_zero = niter_desc.may_be_zero;
3015 3632009 : res = niter_desc.niter;
3016 : }
3017 :
3018 7187616 : if (res == chrec_dont_know
3019 3632009 : || !may_be_zero
3020 10819625 : || integer_zerop (may_be_zero))
3021 : ;
3022 586024 : else if (integer_nonzerop (may_be_zero))
3023 26 : res = build_int_cst (TREE_TYPE (res), 0);
3024 :
3025 585998 : else if (COMPARISON_CLASS_P (may_be_zero))
3026 585998 : res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero,
3027 : build_int_cst (TREE_TYPE (res), 0), res);
3028 : else
3029 0 : res = chrec_dont_know;
3030 :
3031 7187616 : if (dump_file && (dump_flags & TDF_SCEV))
3032 : {
3033 2 : fprintf (dump_file, " (set_nb_iterations_in_loop = ");
3034 2 : print_generic_expr (dump_file, res);
3035 2 : fprintf (dump_file, "))\n");
3036 : }
3037 :
3038 7187616 : loop->nb_iterations = res;
3039 7187616 : return res;
3040 10595315 : }
3041 :
3042 :
3043 : /* Counters for the stats. */
3044 :
3045 : struct chrec_stats
3046 : {
3047 : unsigned nb_chrecs;
3048 : unsigned nb_affine;
3049 : unsigned nb_affine_multivar;
3050 : unsigned nb_higher_poly;
3051 : unsigned nb_chrec_dont_know;
3052 : unsigned nb_undetermined;
3053 : };
3054 :
3055 : /* Reset the counters. */
3056 :
3057 : static inline void
3058 0 : reset_chrecs_counters (struct chrec_stats *stats)
3059 : {
3060 0 : stats->nb_chrecs = 0;
3061 0 : stats->nb_affine = 0;
3062 0 : stats->nb_affine_multivar = 0;
3063 0 : stats->nb_higher_poly = 0;
3064 0 : stats->nb_chrec_dont_know = 0;
3065 0 : stats->nb_undetermined = 0;
3066 : }
3067 :
3068 : /* Dump the contents of a CHREC_STATS structure. */
3069 :
3070 : static void
3071 0 : dump_chrecs_stats (FILE *file, struct chrec_stats *stats)
3072 : {
3073 0 : fprintf (file, "\n(\n");
3074 0 : fprintf (file, "-----------------------------------------\n");
3075 0 : fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine);
3076 0 : fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar);
3077 0 : fprintf (file, "%d\tdegree greater than 2 polynomials\n",
3078 : stats->nb_higher_poly);
3079 0 : fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know);
3080 0 : fprintf (file, "-----------------------------------------\n");
3081 0 : fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs);
3082 0 : fprintf (file, "%d\twith undetermined coefficients\n",
3083 : stats->nb_undetermined);
3084 0 : fprintf (file, "-----------------------------------------\n");
3085 0 : fprintf (file, "%d\tchrecs in the scev database\n",
3086 0 : (int) scalar_evolution_info->elements ());
3087 0 : fprintf (file, "%d\tchrecs in the nowrap scev database\n",
3088 0 : (int) scalar_evolution_nowrap_info->elements ());
3089 0 : fprintf (file, "%d\tsets in the scev database\n", nb_set_scev);
3090 0 : fprintf (file, "%d\tgets in the scev database\n", nb_get_scev);
3091 0 : fprintf (file, "-----------------------------------------\n");
3092 0 : fprintf (file, ")\n\n");
3093 0 : }
3094 :
3095 : /* Gather statistics about CHREC. */
3096 :
3097 : static void
3098 0 : gather_chrec_stats (tree chrec, struct chrec_stats *stats)
3099 : {
3100 0 : if (dump_file && (dump_flags & TDF_STATS))
3101 : {
3102 0 : fprintf (dump_file, "(classify_chrec ");
3103 0 : print_generic_expr (dump_file, chrec);
3104 0 : fprintf (dump_file, "\n");
3105 : }
3106 :
3107 0 : stats->nb_chrecs++;
3108 :
3109 0 : if (chrec == NULL_TREE)
3110 : {
3111 0 : stats->nb_undetermined++;
3112 0 : return;
3113 : }
3114 :
3115 0 : switch (TREE_CODE (chrec))
3116 : {
3117 0 : case POLYNOMIAL_CHREC:
3118 0 : if (evolution_function_is_affine_p (chrec))
3119 : {
3120 0 : if (dump_file && (dump_flags & TDF_STATS))
3121 0 : fprintf (dump_file, " affine_univariate\n");
3122 0 : stats->nb_affine++;
3123 : }
3124 0 : else if (evolution_function_is_affine_multivariate_p (chrec, 0))
3125 : {
3126 0 : if (dump_file && (dump_flags & TDF_STATS))
3127 0 : fprintf (dump_file, " affine_multivariate\n");
3128 0 : stats->nb_affine_multivar++;
3129 : }
3130 : else
3131 : {
3132 0 : if (dump_file && (dump_flags & TDF_STATS))
3133 0 : fprintf (dump_file, " higher_degree_polynomial\n");
3134 0 : stats->nb_higher_poly++;
3135 : }
3136 :
3137 : break;
3138 :
3139 : default:
3140 : break;
3141 : }
3142 :
3143 0 : if (chrec_contains_undetermined (chrec))
3144 : {
3145 0 : if (dump_file && (dump_flags & TDF_STATS))
3146 0 : fprintf (dump_file, " undetermined\n");
3147 0 : stats->nb_undetermined++;
3148 : }
3149 :
3150 0 : if (dump_file && (dump_flags & TDF_STATS))
3151 0 : fprintf (dump_file, ")\n");
3152 : }
3153 :
3154 : /* Classify the chrecs of the whole database. */
3155 :
3156 : void
3157 0 : gather_stats_on_scev_database (void)
3158 : {
3159 0 : struct chrec_stats stats;
3160 :
3161 0 : if (!dump_file)
3162 0 : return;
3163 :
3164 0 : reset_chrecs_counters (&stats);
3165 :
3166 0 : hash_table<scev_info_hasher>::iterator iter;
3167 0 : scev_info_str *elt;
3168 0 : FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *,
3169 : iter)
3170 0 : gather_chrec_stats (elt->chrec, &stats);
3171 0 : FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_nowrap_info, elt,
3172 : scev_info_str *, iter)
3173 0 : gather_chrec_stats (elt->chrec, &stats);
3174 :
3175 0 : dump_chrecs_stats (dump_file, &stats);
3176 : }
3177 :
3178 :
3179 : /* Initialize the analysis of scalar evolutions for LOOPS. */
3180 :
3181 : void
3182 15840540 : scev_initialize (void)
3183 : {
3184 15840540 : gcc_assert (! scev_initialized_p ()
3185 : && loops_state_satisfies_p (cfun, LOOPS_NORMAL));
3186 :
3187 15840540 : scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100);
3188 15840540 : scalar_evolution_nowrap_info = hash_table<scev_info_hasher>::create_ggc (100);
3189 :
3190 58272636 : for (auto loop : loops_list (cfun, 0))
3191 10751016 : loop->nb_iterations = NULL_TREE;
3192 15840540 : }
3193 :
3194 : /* Return true if SCEV is initialized. */
3195 :
3196 : bool
3197 101320201 : scev_initialized_p (void)
3198 : {
3199 101320201 : return scalar_evolution_info != NULL && scalar_evolution_nowrap_info != NULL;
3200 : }
3201 :
3202 : /* Cleans up the information cached by the scalar evolutions analysis
3203 : in the hash table. */
3204 :
3205 : void
3206 25051161 : scev_reset_htab (void)
3207 : {
3208 25051161 : if (!scalar_evolution_info && !scalar_evolution_nowrap_info)
3209 : return;
3210 :
3211 6378836 : if (scalar_evolution_info)
3212 6378836 : scalar_evolution_info->empty ();
3213 6378836 : if (scalar_evolution_nowrap_info)
3214 6378836 : scalar_evolution_nowrap_info->empty ();
3215 : }
3216 :
3217 : /* Cleans up the information cached by the scalar evolutions analysis
3218 : in the hash table and in the loop->nb_iterations. */
3219 :
3220 : void
3221 13617136 : scev_reset (void)
3222 : {
3223 13617136 : scev_reset_htab ();
3224 :
3225 67386012 : for (auto loop : loops_list (cfun, 0))
3226 26534604 : loop->nb_iterations = NULL_TREE;
3227 13617136 : }
3228 :
3229 : /* Return true if the IV calculation in TYPE can overflow based on the knowledge
3230 : of the upper bound on the number of iterations of LOOP, the BASE and STEP
3231 : of IV.
3232 :
3233 : We do not use information whether TYPE can overflow so it is safe to
3234 : use this test even for derived IVs not computed every iteration or
3235 : hypothetical IVs to be inserted into code. */
3236 :
3237 : bool
3238 15718525 : iv_can_overflow_p (class loop *loop, tree type, tree base, tree step)
3239 : {
3240 15718525 : widest_int nit;
3241 15718525 : wide_int base_min, base_max, step_min, step_max, type_min, type_max;
3242 15718525 : signop sgn = TYPE_SIGN (type);
3243 15718525 : int_range_max r;
3244 :
3245 15718525 : if (integer_zerop (step))
3246 : return false;
3247 :
3248 31433478 : if (!INTEGRAL_TYPE_P (TREE_TYPE (base))
3249 29220378 : || !get_range_query (cfun)->range_of_expr (r, base)
3250 14610189 : || r.varying_p ()
3251 28806709 : || r.undefined_p ())
3252 : return true;
3253 :
3254 13085945 : base_min = r.lower_bound ();
3255 13085945 : base_max = r.upper_bound ();
3256 :
3257 26168431 : if (!INTEGRAL_TYPE_P (TREE_TYPE (step))
3258 26171890 : || !get_range_query (cfun)->range_of_expr (r, step)
3259 13085945 : || r.varying_p ()
3260 26097737 : || r.undefined_p ())
3261 : return true;
3262 :
3263 13011792 : step_min = r.lower_bound ();
3264 13011792 : step_max = r.upper_bound ();
3265 :
3266 13011792 : if (!get_max_loop_iterations (loop, &nit))
3267 : return true;
3268 :
3269 12325831 : type_min = wi::min_value (type);
3270 12325831 : type_max = wi::max_value (type);
3271 :
3272 : /* Just sanity check that we don't see values out of the range of the type.
3273 : In this case the arithmetics below would overflow. */
3274 12325831 : gcc_checking_assert (wi::ge_p (base_min, type_min, sgn)
3275 : && wi::le_p (base_max, type_max, sgn));
3276 :
3277 : /* Account the possible increment in the last ieration. */
3278 12325831 : wi::overflow_type overflow = wi::OVF_NONE;
3279 12325831 : nit = wi::add (nit, 1, SIGNED, &overflow);
3280 12325831 : if (overflow)
3281 : return true;
3282 :
3283 : /* NIT is typeless and can exceed the precision of the type. In this case
3284 : overflow is always possible, because we know STEP is non-zero. */
3285 12325831 : if (wi::min_precision (nit, UNSIGNED) > TYPE_PRECISION (type))
3286 : return true;
3287 12086420 : wide_int nit2 = wide_int::from (nit, TYPE_PRECISION (type), UNSIGNED);
3288 :
3289 : /* If step can be positive, check that nit*step <= type_max-base.
3290 : This can be done by unsigned arithmetic and we only need to watch overflow
3291 : in the multiplication. The right hand side can always be represented in
3292 : the type. */
3293 12086420 : if (sgn == UNSIGNED || !wi::neg_p (step_max))
3294 : {
3295 12040022 : wi::overflow_type overflow = wi::OVF_NONE;
3296 12040022 : if (wi::gtu_p (wi::mul (step_max, nit2, UNSIGNED, &overflow),
3297 24080044 : type_max - base_max)
3298 24080044 : || overflow)
3299 6065259 : return true;
3300 : }
3301 : /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3302 6021161 : if (sgn == SIGNED && wi::neg_p (step_min))
3303 : {
3304 46714 : wi::overflow_type overflow, overflow2;
3305 46714 : overflow = overflow2 = wi::OVF_NONE;
3306 93428 : if (wi::gtu_p (wi::mul (wi::neg (step_min, &overflow2),
3307 : nit2, UNSIGNED, &overflow),
3308 93428 : base_min - type_min)
3309 93428 : || overflow || overflow2)
3310 15187 : return true;
3311 : }
3312 :
3313 : return false;
3314 15718525 : }
3315 :
3316 : /* Given EV with form of "(type) {inner_base, inner_step}_loop", this
3317 : function tries to derive condition under which it can be simplified
3318 : into "{(type)inner_base, (type)inner_step}_loop". The condition is
3319 : the maximum number that inner iv can iterate. */
3320 :
3321 : static tree
3322 38999 : derive_simple_iv_with_niters (tree ev, tree *niters)
3323 : {
3324 38999 : if (!CONVERT_EXPR_P (ev))
3325 : return ev;
3326 :
3327 38999 : tree inner_ev = TREE_OPERAND (ev, 0);
3328 38999 : if (TREE_CODE (inner_ev) != POLYNOMIAL_CHREC)
3329 : return ev;
3330 :
3331 38999 : tree init = CHREC_LEFT (inner_ev);
3332 38999 : tree step = CHREC_RIGHT (inner_ev);
3333 38999 : if (TREE_CODE (init) != INTEGER_CST
3334 38999 : || TREE_CODE (step) != INTEGER_CST || integer_zerop (step))
3335 : return ev;
3336 :
3337 30490 : tree type = TREE_TYPE (ev);
3338 30490 : tree inner_type = TREE_TYPE (inner_ev);
3339 30490 : if (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (type))
3340 : return ev;
3341 :
3342 30490 : int inner_sign = tree_int_cst_sign_bit (step);
3343 30490 : tree inner_sign_type = signed_or_unsigned_type_for (!inner_sign, inner_type);
3344 :
3345 : /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
3346 : folded only if inner iv won't overflow. We compute the maximum
3347 : number the inner iv can iterate before overflowing and return the
3348 : simplified affine iv. */
3349 30490 : ev = build_polynomial_chrec (CHREC_VARIABLE (inner_ev),
3350 : fold_convert (type, init),
3351 : fold_convert (type,
3352 : fold_convert (inner_sign_type,
3353 : step)));
3354 30490 : *niters = get_niter_type_bound (inner_type, init, step);
3355 30490 : return ev;
3356 : }
3357 :
3358 : /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3359 : respect to WRTO_LOOP and returns its base and step in IV if possible
3360 : (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3361 : and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3362 : invariant in LOOP. Otherwise we require it to be an integer constant.
3363 :
3364 : IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3365 : because it is computed in signed arithmetics). Consequently, adding an
3366 : induction variable
3367 :
3368 : for (i = IV->base; ; i += IV->step)
3369 :
3370 : is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3371 : false for the type of the induction variable, or you can prove that i does
3372 : not wrap by some other argument. Otherwise, this might introduce undefined
3373 : behavior, and
3374 :
3375 : i = iv->base;
3376 : for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3377 :
3378 : must be used instead.
3379 :
3380 : When IV_NITERS is not NULL, this function also checks case in which OP
3381 : is a conversion of an inner simple iv of below form:
3382 :
3383 : (outer_type){inner_base, inner_step}_loop.
3384 :
3385 : If type of inner iv has smaller precision than outer_type, it can't be
3386 : folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
3387 : the inner iv could overflow/wrap. In this case, we derive a condition
3388 : under which the inner iv won't overflow/wrap and do the simplification.
3389 : The derived condition normally is the maximum number the inner iv can
3390 : iterate, and will be stored in IV_NITERS. This is useful in loop niter
3391 : analysis, to derive break conditions when a loop must terminate, when is
3392 : infinite.
3393 :
3394 : If RECORD_NOWRAP_BOUNDS is true, let scev analysis continue rather than
3395 : stop if an IV might wrap. In such a case, store the recorded bound under
3396 : which no wrapping occurs in IV's nowrap_bound. */
3397 :
3398 : bool
3399 54229365 : simple_iv_with_niters (class loop *wrto_loop, class loop *use_loop,
3400 : tree op, affine_iv *iv, tree *iv_niters,
3401 : bool allow_nonconstant_step,
3402 : bool record_nowrap_bounds)
3403 : {
3404 54229365 : enum tree_code code;
3405 54229365 : tree type, ev, base, e;
3406 54229365 : wide_int extreme;
3407 54229365 : bool folded_casts;
3408 :
3409 54229365 : iv->base = NULL_TREE;
3410 54229365 : iv->step = NULL_TREE;
3411 54229365 : iv->no_overflow = false;
3412 54229365 : iv->nowrap_bound = NULL_TREE;
3413 :
3414 54229365 : type = TREE_TYPE (op);
3415 54229365 : if (!POINTER_TYPE_P (type)
3416 44764112 : && !INTEGRAL_TYPE_P (type))
3417 : return false;
3418 :
3419 52103010 : tree nowrap_bound = NULL_TREE;
3420 103395296 : ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op,
3421 : &folded_casts,
3422 : record_nowrap_bounds ? &nowrap_bound
3423 : : nullptr);
3424 :
3425 : /* If there was a nontrivial nowrap bound, add it to the IV. */
3426 52103010 : if (record_nowrap_bounds && nowrap_bound)
3427 1403 : iv->nowrap_bound = nowrap_bound;
3428 :
3429 52103010 : if (chrec_contains_undetermined (ev)
3430 52103010 : || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num))
3431 : return false;
3432 :
3433 38768881 : if (tree_does_not_contain_chrecs (ev))
3434 : {
3435 17233059 : iv->base = ev;
3436 17233059 : tree ev_type = TREE_TYPE (ev);
3437 17233059 : if (POINTER_TYPE_P (ev_type))
3438 2783726 : ev_type = sizetype;
3439 :
3440 17233059 : iv->step = build_int_cst (ev_type, 0);
3441 17233059 : iv->no_overflow = true;
3442 17233059 : return true;
3443 : }
3444 :
3445 : /* If we can derive valid scalar evolution with assumptions. */
3446 21535822 : if (iv_niters && TREE_CODE (ev) != POLYNOMIAL_CHREC)
3447 38999 : ev = derive_simple_iv_with_niters (ev, iv_niters);
3448 :
3449 21535822 : if (TREE_CODE (ev) != POLYNOMIAL_CHREC)
3450 : return false;
3451 :
3452 21485114 : if (CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num)
3453 : return false;
3454 :
3455 21485100 : iv->step = CHREC_RIGHT (ev);
3456 13849209 : if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST)
3457 35072159 : || tree_contains_chrecs (iv->step, NULL))
3458 : return false;
3459 :
3460 21211001 : iv->base = CHREC_LEFT (ev);
3461 21211001 : if (tree_contains_chrecs (iv->base, NULL))
3462 : return false;
3463 :
3464 21211001 : iv->no_overflow = !folded_casts && nowrap_type_p (type);
3465 :
3466 21211001 : if (!iv->no_overflow
3467 21211001 : && !iv_can_overflow_p (wrto_loop, type, iv->base, iv->step))
3468 4057567 : iv->no_overflow = true;
3469 :
3470 : /* Try to simplify iv base:
3471 :
3472 : (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3473 : == (signed T)(unsigned T)base + step
3474 : == base + step
3475 :
3476 : If we can prove operation (base + step) doesn't overflow or underflow.
3477 : Specifically, we try to prove below conditions are satisfied:
3478 :
3479 : base <= UPPER_BOUND (type) - step ;;step > 0
3480 : base >= LOWER_BOUND (type) - step ;;step < 0
3481 :
3482 : This is done by proving the reverse conditions are false using loop's
3483 : initial conditions.
3484 :
3485 : The is necessary to make loop niter, or iv overflow analysis easier
3486 : for below example:
3487 :
3488 : int foo (int *a, signed char s, signed char l)
3489 : {
3490 : signed char i;
3491 : for (i = s; i < l; i++)
3492 : a[i] = 0;
3493 : return 0;
3494 : }
3495 :
3496 : Note variable I is firstly converted to type unsigned char, incremented,
3497 : then converted back to type signed char. */
3498 :
3499 21211001 : if (wrto_loop->num != use_loop->num)
3500 : return true;
3501 :
3502 21027560 : if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST)
3503 : return true;
3504 :
3505 236426 : type = TREE_TYPE (iv->base);
3506 236426 : e = TREE_OPERAND (iv->base, 0);
3507 236426 : if (!tree_nop_conversion_p (type, TREE_TYPE (e))
3508 203211 : || TREE_CODE (e) != PLUS_EXPR
3509 111535 : || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST
3510 320867 : || !tree_int_cst_equal (iv->step,
3511 84441 : fold_convert (type, TREE_OPERAND (e, 1))))
3512 : return true;
3513 66421 : e = TREE_OPERAND (e, 0);
3514 66421 : if (!CONVERT_EXPR_P (e))
3515 : return true;
3516 36127 : base = TREE_OPERAND (e, 0);
3517 36127 : if (!useless_type_conversion_p (type, TREE_TYPE (base)))
3518 : return true;
3519 :
3520 27304 : if (tree_int_cst_sign_bit (iv->step))
3521 : {
3522 7171 : code = LT_EXPR;
3523 7171 : extreme = wi::min_value (type);
3524 : }
3525 : else
3526 : {
3527 20133 : code = GT_EXPR;
3528 20133 : extreme = wi::max_value (type);
3529 : }
3530 27304 : wi::overflow_type overflow = wi::OVF_NONE;
3531 27304 : extreme = wi::sub (extreme, wi::to_wide (iv->step),
3532 54608 : TYPE_SIGN (type), &overflow);
3533 27304 : if (overflow)
3534 : return true;
3535 27280 : e = fold_build2 (code, boolean_type_node, base,
3536 : wide_int_to_tree (type, extreme));
3537 27280 : e = simplify_using_initial_conditions (use_loop, e);
3538 27280 : if (!integer_zerop (e))
3539 : return true;
3540 :
3541 16006 : if (POINTER_TYPE_P (TREE_TYPE (base)))
3542 : code = POINTER_PLUS_EXPR;
3543 : else
3544 : code = PLUS_EXPR;
3545 :
3546 16006 : iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step);
3547 16006 : return true;
3548 54229365 : }
3549 :
3550 : /* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
3551 : affine iv unconditionally. */
3552 :
3553 : bool
3554 21925777 : simple_iv (class loop *wrto_loop, class loop *use_loop, tree op,
3555 : affine_iv *iv, bool allow_nonconstant_step,
3556 : bool record_nowrap_bounds)
3557 : {
3558 21925777 : return simple_iv_with_niters (wrto_loop, use_loop, op, iv,
3559 : NULL, allow_nonconstant_step,
3560 21925777 : record_nowrap_bounds);
3561 : }
3562 :
3563 : /* Finalize the scalar evolution analysis. */
3564 :
3565 : void
3566 15840541 : scev_finalize (void)
3567 : {
3568 15840541 : if (!scalar_evolution_info && !scalar_evolution_nowrap_info)
3569 : return;
3570 :
3571 15840540 : if (scalar_evolution_info)
3572 : {
3573 15840540 : scalar_evolution_info->empty ();
3574 15840540 : scalar_evolution_info = NULL;
3575 : }
3576 :
3577 15840540 : if (scalar_evolution_nowrap_info)
3578 : {
3579 15840540 : scalar_evolution_nowrap_info->empty ();
3580 15840540 : scalar_evolution_nowrap_info = NULL;
3581 : }
3582 :
3583 15840540 : free_numbers_of_iterations_estimates (cfun);
3584 : }
3585 :
3586 : /* Returns true if the expression EXPR is considered to be too expensive
3587 : for scev_const_prop. Sets *COND_OVERFLOW_P to true when the
3588 : expression might contain a sub-expression that is subject to undefined
3589 : overflow behavior and conditionally evaluated. */
3590 :
3591 : static bool
3592 10219394 : expression_expensive_p (tree expr, bool *cond_overflow_p,
3593 : hash_map<tree, uint64_t> &cache, uint64_t &cost)
3594 : {
3595 10219394 : enum tree_code code;
3596 :
3597 10219394 : if (is_gimple_val (expr))
3598 : return false;
3599 :
3600 4210645 : code = TREE_CODE (expr);
3601 4210645 : if (code == TRUNC_DIV_EXPR
3602 : || code == CEIL_DIV_EXPR
3603 : || code == FLOOR_DIV_EXPR
3604 : || code == ROUND_DIV_EXPR
3605 : || code == TRUNC_MOD_EXPR
3606 : || code == CEIL_MOD_EXPR
3607 : || code == FLOOR_MOD_EXPR
3608 4210645 : || code == ROUND_MOD_EXPR
3609 4210645 : || code == EXACT_DIV_EXPR)
3610 : {
3611 : /* Division by power of two is usually cheap, so we allow it.
3612 : Forbid anything else. */
3613 53427 : if (!integer_pow2p (TREE_OPERAND (expr, 1)))
3614 : return true;
3615 : }
3616 :
3617 4202247 : bool visited_p;
3618 4202247 : uint64_t &local_cost = cache.get_or_insert (expr, &visited_p);
3619 4202247 : if (visited_p)
3620 : {
3621 394 : uint64_t tem = cost + local_cost;
3622 394 : if (tem < cost)
3623 : return true;
3624 394 : cost = tem;
3625 394 : return false;
3626 : }
3627 4201853 : local_cost = 1;
3628 :
3629 4201853 : uint64_t op_cost = 0;
3630 4201853 : if (code == CALL_EXPR)
3631 : {
3632 162 : tree arg;
3633 162 : call_expr_arg_iterator iter;
3634 : /* Even though is_inexpensive_builtin might say true, we will get a
3635 : library call for popcount when backend does not have an instruction
3636 : to do so. We consider this to be expensive and generate
3637 : __builtin_popcount only when backend defines it. */
3638 162 : optab optab;
3639 162 : combined_fn cfn = get_call_combined_fn (expr);
3640 162 : switch (cfn)
3641 : {
3642 36 : CASE_CFN_POPCOUNT:
3643 36 : optab = popcount_optab;
3644 36 : goto bitcount_call;
3645 93 : CASE_CFN_CLZ:
3646 93 : optab = clz_optab;
3647 93 : goto bitcount_call;
3648 : CASE_CFN_CTZ:
3649 : optab = ctz_optab;
3650 162 : bitcount_call:
3651 : /* Check if opcode for popcount is available in the mode required. */
3652 162 : if (optab_handler (optab,
3653 162 : TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0))))
3654 : == CODE_FOR_nothing)
3655 : {
3656 32 : machine_mode mode;
3657 32 : mode = TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr, 0)));
3658 32 : scalar_int_mode int_mode;
3659 :
3660 : /* If the mode is of 2 * UNITS_PER_WORD size, we can handle
3661 : double-word popcount by emitting two single-word popcount
3662 : instructions. */
3663 32 : if (is_a <scalar_int_mode> (mode, &int_mode)
3664 34 : && GET_MODE_SIZE (int_mode) == 2 * UNITS_PER_WORD
3665 2 : && (optab_handler (optab, word_mode)
3666 : != CODE_FOR_nothing))
3667 : break;
3668 : /* If popcount is available for a wider mode, we emulate the
3669 : operation for a narrow mode by first zero-extending the value
3670 : and then computing popcount in the wider mode. Analogue for
3671 : ctz. For clz we do the same except that we additionally have
3672 : to subtract the difference of the mode precisions from the
3673 : result. */
3674 30 : if (is_a <scalar_int_mode> (mode, &int_mode))
3675 : {
3676 30 : machine_mode wider_mode_iter;
3677 149 : FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
3678 119 : if (optab_handler (optab, wider_mode_iter)
3679 : != CODE_FOR_nothing)
3680 0 : goto check_call_args;
3681 : /* Operation ctz may be emulated via clz in expand_ctz. */
3682 30 : if (optab == ctz_optab)
3683 : {
3684 0 : FOR_EACH_WIDER_MODE_FROM (wider_mode_iter, mode)
3685 0 : if (optab_handler (clz_optab, wider_mode_iter)
3686 : != CODE_FOR_nothing)
3687 0 : goto check_call_args;
3688 : }
3689 : }
3690 162 : return true;
3691 : }
3692 : break;
3693 :
3694 0 : default:
3695 0 : if (cfn == CFN_LAST
3696 0 : || !is_inexpensive_builtin (get_callee_fndecl (expr)))
3697 : return true;
3698 : break;
3699 : }
3700 :
3701 132 : check_call_args:
3702 440 : FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
3703 176 : if (expression_expensive_p (arg, cond_overflow_p, cache, op_cost))
3704 : return true;
3705 132 : *cache.get (expr) += op_cost;
3706 132 : cost += op_cost + 1;
3707 132 : return false;
3708 : }
3709 :
3710 4201691 : if (code == COND_EXPR)
3711 : {
3712 2120 : if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
3713 : cache, op_cost)
3714 2120 : || (EXPR_P (TREE_OPERAND (expr, 1))
3715 2119 : && EXPR_P (TREE_OPERAND (expr, 2)))
3716 : /* If either branch has side effects or could trap. */
3717 2112 : || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 1))
3718 2112 : || generic_expr_could_trap_p (TREE_OPERAND (expr, 1))
3719 2111 : || TREE_SIDE_EFFECTS (TREE_OPERAND (expr, 0))
3720 2111 : || generic_expr_could_trap_p (TREE_OPERAND (expr, 0))
3721 2111 : || expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
3722 : cache, op_cost)
3723 4128 : || expression_expensive_p (TREE_OPERAND (expr, 2), cond_overflow_p,
3724 : cache, op_cost))
3725 : return true;
3726 : /* Conservatively assume there's overflow for now. */
3727 2008 : *cond_overflow_p = true;
3728 2008 : *cache.get (expr) += op_cost;
3729 2008 : cost += op_cost + 1;
3730 2008 : return false;
3731 : }
3732 :
3733 4199571 : switch (TREE_CODE_CLASS (code))
3734 : {
3735 2192802 : case tcc_binary:
3736 2192802 : case tcc_comparison:
3737 2192802 : if (expression_expensive_p (TREE_OPERAND (expr, 1), cond_overflow_p,
3738 : cache, op_cost))
3739 : return true;
3740 :
3741 : /* Fallthru. */
3742 4197140 : case tcc_unary:
3743 4197140 : if (expression_expensive_p (TREE_OPERAND (expr, 0), cond_overflow_p,
3744 : cache, op_cost))
3745 : return true;
3746 4175548 : *cache.get (expr) += op_cost;
3747 4175548 : cost += op_cost + 1;
3748 4175548 : return false;
3749 :
3750 : default:
3751 : return true;
3752 : }
3753 : }
3754 :
3755 : bool
3756 3823037 : expression_expensive_p (tree expr, bool *cond_overflow_p)
3757 : {
3758 3823037 : hash_map<tree, uint64_t> cache;
3759 3823037 : uint64_t expanded_size = 0;
3760 3823037 : *cond_overflow_p = false;
3761 3823037 : return (expression_expensive_p (expr, cond_overflow_p, cache, expanded_size)
3762 : /* ??? Both the explicit unsharing and gimplification of expr will
3763 : expand shared trees to multiple copies.
3764 : Guard against exponential growth by counting the visits and
3765 : comparing against the number of original nodes. Allow a tiny
3766 : bit of duplication to catch some additional optimizations. */
3767 3831638 : || expanded_size > (cache.elements () + 1));
3768 3823037 : }
3769 :
3770 : /* Match.pd function to match bitwise inductive expression.
3771 : .i.e.
3772 : _2 = 1 << _1;
3773 : _3 = ~_2;
3774 : tmp_9 = _3 & tmp_12; */
3775 : extern bool gimple_bitwise_induction_p (tree, tree *, tree (*)(tree));
3776 :
3777 : /* Return the inductive expression of bitwise operation if possible,
3778 : otherwise returns DEF. */
3779 : static tree
3780 21482 : analyze_and_compute_bitwise_induction_effect (class loop* loop,
3781 : tree phidef,
3782 : unsigned HOST_WIDE_INT niter)
3783 : {
3784 21482 : tree match_op[3],inv, bitwise_scev;
3785 21482 : tree type = TREE_TYPE (phidef);
3786 21482 : gphi* header_phi = NULL;
3787 :
3788 : /* Match things like op2(MATCH_OP[2]), op1(MATCH_OP[1]), phidef(PHIDEF)
3789 :
3790 : op2 = PHI <phidef, inv>
3791 : _1 = (int) bit_17;
3792 : _3 = 1 << _1;
3793 : op1 = ~_3;
3794 : phidef = op1 & op2; */
3795 21482 : if (!gimple_bitwise_induction_p (phidef, &match_op[0], NULL)
3796 100 : || TREE_CODE (match_op[2]) != SSA_NAME
3797 100 : || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[2])))
3798 100 : || gimple_bb (header_phi) != loop->header
3799 21582 : || gimple_phi_num_args (header_phi) != 2)
3800 : return NULL_TREE;
3801 :
3802 100 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
3803 : return NULL_TREE;
3804 :
3805 100 : bitwise_scev = analyze_scalar_evolution (loop, match_op[1]);
3806 100 : bitwise_scev = instantiate_parameters (loop, bitwise_scev);
3807 :
3808 : /* Make sure bits is in range of type precision. */
3809 100 : if (TREE_CODE (bitwise_scev) != POLYNOMIAL_CHREC
3810 100 : || !INTEGRAL_TYPE_P (TREE_TYPE (bitwise_scev))
3811 100 : || !tree_fits_uhwi_p (CHREC_LEFT (bitwise_scev))
3812 100 : || tree_to_uhwi (CHREC_LEFT (bitwise_scev)) >= TYPE_PRECISION (type)
3813 200 : || !tree_fits_shwi_p (CHREC_RIGHT (bitwise_scev)))
3814 : return NULL_TREE;
3815 :
3816 100 : enum bit_op_kind
3817 : {
3818 : INDUCTION_BIT_CLEAR,
3819 : INDUCTION_BIT_IOR,
3820 : INDUCTION_BIT_XOR,
3821 : INDUCTION_BIT_RESET,
3822 : INDUCTION_ZERO,
3823 : INDUCTION_ALL
3824 : };
3825 :
3826 100 : enum bit_op_kind induction_kind;
3827 100 : enum tree_code code1
3828 100 : = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (phidef));
3829 100 : enum tree_code code2
3830 100 : = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (match_op[0]));
3831 :
3832 : /* BIT_CLEAR: A &= ~(1 << bit)
3833 : BIT_RESET: A ^= (1 << bit).
3834 : BIT_IOR: A |= (1 << bit)
3835 : BIT_ZERO: A &= (1 << bit)
3836 : BIT_ALL: A |= ~(1 << bit)
3837 : BIT_XOR: A ^= ~(1 << bit).
3838 : bit is induction variable. */
3839 100 : switch (code1)
3840 : {
3841 27 : case BIT_AND_EXPR:
3842 27 : induction_kind = code2 == BIT_NOT_EXPR
3843 27 : ? INDUCTION_BIT_CLEAR
3844 : : INDUCTION_ZERO;
3845 : break;
3846 49 : case BIT_IOR_EXPR:
3847 49 : induction_kind = code2 == BIT_NOT_EXPR
3848 49 : ? INDUCTION_ALL
3849 : : INDUCTION_BIT_IOR;
3850 : break;
3851 12 : case BIT_XOR_EXPR:
3852 12 : induction_kind = code2 == BIT_NOT_EXPR
3853 12 : ? INDUCTION_BIT_XOR
3854 : : INDUCTION_BIT_RESET;
3855 : break;
3856 : /* A ^ ~(1 << bit) is equal to ~(A ^ (1 << bit)). */
3857 12 : case BIT_NOT_EXPR:
3858 12 : gcc_assert (code2 == BIT_XOR_EXPR);
3859 : induction_kind = INDUCTION_BIT_XOR;
3860 : break;
3861 0 : default:
3862 0 : gcc_unreachable ();
3863 : }
3864 :
3865 37 : if (induction_kind == INDUCTION_ZERO)
3866 12 : return build_zero_cst (type);
3867 88 : if (induction_kind == INDUCTION_ALL)
3868 12 : return build_all_ones_cst (type);
3869 :
3870 152 : wide_int bits = wi::zero (TYPE_PRECISION (type));
3871 76 : HOST_WIDE_INT bit_start = tree_to_shwi (CHREC_LEFT (bitwise_scev));
3872 76 : HOST_WIDE_INT step = tree_to_shwi (CHREC_RIGHT (bitwise_scev));
3873 76 : HOST_WIDE_INT bit_final = bit_start + step * niter;
3874 :
3875 : /* bit_start, bit_final in range of [0,TYPE_PRECISION)
3876 : implies all bits are set in range. */
3877 76 : if (bit_final >= TYPE_PRECISION (type)
3878 76 : || bit_final < 0)
3879 : return NULL_TREE;
3880 :
3881 : /* Loop tripcount should be niter + 1. */
3882 1296 : for (unsigned i = 0; i != niter + 1; i++)
3883 : {
3884 1220 : bits = wi::set_bit (bits, bit_start);
3885 1220 : bit_start += step;
3886 : }
3887 :
3888 76 : bool inverted = false;
3889 76 : switch (induction_kind)
3890 : {
3891 : case INDUCTION_BIT_CLEAR:
3892 : code1 = BIT_AND_EXPR;
3893 : inverted = true;
3894 : break;
3895 : case INDUCTION_BIT_IOR:
3896 : code1 = BIT_IOR_EXPR;
3897 : break;
3898 : case INDUCTION_BIT_RESET:
3899 : code1 = BIT_XOR_EXPR;
3900 : break;
3901 : /* A ^= ~(1 << bit) is special, when loop tripcount is even,
3902 : it's equal to A ^= bits, else A ^= ~bits. */
3903 12 : case INDUCTION_BIT_XOR:
3904 12 : code1 = BIT_XOR_EXPR;
3905 12 : if (niter % 2 == 0)
3906 : inverted = true;
3907 : break;
3908 : default:
3909 : gcc_unreachable ();
3910 : }
3911 :
3912 : if (inverted)
3913 19 : bits = wi::bit_not (bits);
3914 :
3915 76 : inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
3916 76 : return fold_build2 (code1, type, inv, wide_int_to_tree (type, bits));
3917 : }
3918 :
3919 : /* Match.pd function to match bitop with invariant expression
3920 : .i.e.
3921 : tmp_7 = _0 & _1; */
3922 : extern bool gimple_bitop_with_inv_p (tree, tree *, tree (*)(tree));
3923 :
3924 : /* Return the inductive expression of bitop with invariant if possible,
3925 : otherwise returns DEF. */
3926 : static tree
3927 71825 : analyze_and_compute_bitop_with_inv_effect (class loop* loop, tree phidef,
3928 : tree niter)
3929 : {
3930 71825 : tree match_op[2],inv;
3931 71825 : tree type = TREE_TYPE (phidef);
3932 71825 : gphi* header_phi = NULL;
3933 71825 : enum tree_code code;
3934 : /* match thing like op0 (match[0]), op1 (match[1]), phidef (PHIDEF)
3935 :
3936 : op1 = PHI <phidef, inv>
3937 : phidef = op0 & op1
3938 : if op0 is an invariant, it could change to
3939 : phidef = op0 & inv. */
3940 71825 : auto def = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (phidef));
3941 25875 : if (!(def
3942 25875 : && ((code = gimple_assign_rhs_code (def)), true)
3943 25875 : && (code == BIT_AND_EXPR || code == BIT_IOR_EXPR
3944 23217 : || code == BIT_XOR_EXPR)))
3945 : return NULL_TREE;
3946 :
3947 3459 : match_op[0] = gimple_assign_rhs1 (def);
3948 3459 : match_op[1] = gimple_assign_rhs2 (def);
3949 :
3950 3459 : if (expr_invariant_in_loop_p (loop, match_op[1]))
3951 343 : std::swap (match_op[0], match_op[1]);
3952 :
3953 3459 : if (TREE_CODE (match_op[1]) != SSA_NAME
3954 3459 : || !expr_invariant_in_loop_p (loop, match_op[0])
3955 463 : || !(header_phi = dyn_cast <gphi *> (SSA_NAME_DEF_STMT (match_op[1])))
3956 240 : || gimple_bb (header_phi) != loop->header
3957 3691 : || gimple_phi_num_args (header_phi) != 2)
3958 : return NULL_TREE;
3959 :
3960 232 : if (PHI_ARG_DEF_FROM_EDGE (header_phi, loop_latch_edge (loop)) != phidef)
3961 : return NULL_TREE;
3962 :
3963 227 : enum tree_code code1
3964 227 : = gimple_assign_rhs_code (def);
3965 :
3966 227 : if (code1 == BIT_XOR_EXPR)
3967 : {
3968 61 : tree niter_type = TREE_TYPE (niter);
3969 61 : tree one = build_one_cst (niter_type);
3970 61 : tree contributes = fold_build2 (BIT_XOR_EXPR, niter_type,
3971 : fold_build2 (BIT_AND_EXPR, niter_type,
3972 : niter, one),
3973 : one);
3974 : /* mask is all-ones when the invariant contributes, zero otherwise. */
3975 61 : tree mask = fold_build1 (NEGATE_EXPR, type,
3976 : fold_convert (type, contributes));
3977 61 : match_op[0] = fold_build2 (BIT_AND_EXPR, type, match_op[0], mask);
3978 : }
3979 :
3980 227 : inv = PHI_ARG_DEF_FROM_EDGE (header_phi, loop_preheader_edge (loop));
3981 227 : return fold_build2 (code1, type, inv, match_op[0]);
3982 : }
3983 :
3984 : /* Try to compute the final value of PHIDEF when PHIDEF is the result of a
3985 : loop-header PHI.
3986 :
3987 : This handles the nonzero-latch-count delayed-value form:
3988 :
3989 : y_phi = PHI <latch_arg (latch), init (preheader)>
3990 :
3991 : If the latch count is known to be nonzero, the final value is:
3992 :
3993 : latch_arg evaluated at iteration niter - 1
3994 :
3995 : Return NULL_TREE if the pattern does not apply. */
3996 : static tree
3997 43552 : compute_final_value_from_loop_phi_latch (class loop *loop,
3998 : class loop *ex_loop, gphi *header_phi, tree niter, bool* folded_casts)
3999 : {
4000 43552 : if (gimple_bb (header_phi) != loop->header
4001 46040 : || gimple_phi_num_args (header_phi) != 2)
4002 : return NULL_TREE;
4003 :
4004 : /* If niter is a symbolic value make sure it can never be zero, otherwise we
4005 : do a bad replacement. */
4006 2488 : if (!tree_expr_nonzero_p (niter))
4007 : return NULL_TREE;
4008 :
4009 1083 : tree latch_arg = PHI_ARG_DEF_FROM_EDGE (header_phi,
4010 : loop_latch_edge (loop));
4011 :
4012 1083 : tree ev = analyze_scalar_evolution_in_loop (ex_loop,
4013 : loop,
4014 : latch_arg,
4015 : folded_casts);
4016 1083 : if (ev == chrec_dont_know)
4017 : return NULL_TREE;
4018 :
4019 491 : bool invariant_p;
4020 491 : if (no_evolution_in_loop_p (ev, ex_loop->num, &invariant_p) && invariant_p)
4021 : return ev;
4022 17 : else if (TREE_CODE (ev) == POLYNOMIAL_CHREC
4023 17 : && get_chrec_loop (ev) == ex_loop)
4024 : {
4025 17 : tree niter_type = TREE_TYPE (niter);
4026 17 : tree prev_iter = fold_build2 (MINUS_EXPR,
4027 : niter_type,
4028 : niter,
4029 : build_one_cst (niter_type));
4030 :
4031 17 : tree res = chrec_apply (ex_loop->num, ev, prev_iter);
4032 17 : if (res == chrec_dont_know)
4033 : return NULL_TREE;
4034 :
4035 17 : if (chrec_contains_symbols_defined_in_loop (res, ex_loop->num))
4036 : {
4037 0 : res = instantiate_parameters (ex_loop, res);
4038 0 : if (res == chrec_dont_know)
4039 : return NULL_TREE;
4040 : }
4041 :
4042 17 : return res;
4043 : }
4044 : return NULL_TREE;
4045 : }
4046 :
4047 : /* Do final value replacement for LOOP, return true if we did anything. */
4048 :
4049 : bool
4050 715224 : final_value_replacement_loop (class loop *loop)
4051 : {
4052 : /* If we do not know exact number of iterations of the loop, we cannot
4053 : replace the final value. */
4054 715224 : edge exit = single_exit (loop);
4055 715224 : if (!exit)
4056 : return false;
4057 :
4058 477564 : class tree_niter_desc niter_desc;
4059 477564 : if (!number_of_iterations_exit (loop, exit, &niter_desc, false))
4060 : return false;
4061 :
4062 359771 : tree niter = niter_desc.niter;
4063 359771 : if (niter == chrec_dont_know)
4064 : return false;
4065 :
4066 : /* Ensure that it is possible to insert new statements somewhere. */
4067 359771 : if (!single_pred_p (exit->dest))
4068 40761 : split_loop_exit_edge (exit);
4069 :
4070 : /* Set stmt insertion pointer. All stmts are inserted before this point. */
4071 :
4072 359771 : class loop *ex_loop
4073 719542 : = superloop_at_depth (loop,
4074 440939 : loop_depth (exit->dest->loop_father) + 1);
4075 :
4076 359771 : bool any = false;
4077 359771 : gphi_iterator psi;
4078 805600 : for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); )
4079 : {
4080 445829 : gphi *phi = psi.phi ();
4081 445829 : tree rslt = PHI_RESULT (phi);
4082 445829 : tree phidef = PHI_ARG_DEF_FROM_EDGE (phi, exit);
4083 445829 : tree def = phidef;
4084 890951 : if (virtual_operand_p (def))
4085 : {
4086 255263 : gsi_next (&psi);
4087 409260 : continue;
4088 : }
4089 :
4090 362206 : if (!POINTER_TYPE_P (TREE_TYPE (def))
4091 362109 : && !INTEGRAL_TYPE_P (TREE_TYPE (def)))
4092 : {
4093 74494 : gsi_next (&psi);
4094 74494 : continue;
4095 : }
4096 :
4097 116072 : bool folded_casts;
4098 116072 : def = analyze_scalar_evolution_in_loop (ex_loop, loop, def,
4099 : &folded_casts);
4100 :
4101 116072 : tree bitinv_def, bit_def, phi_latch_final_value;
4102 116072 : unsigned HOST_WIDE_INT niter_num;
4103 :
4104 116072 : gphi *header_phi = TREE_CODE (phidef) == SSA_NAME
4105 116072 : ? dyn_cast<gphi*> (SSA_NAME_DEF_STMT (phidef))
4106 : : NULL;
4107 :
4108 116072 : if (def != chrec_dont_know)
4109 37557 : def = compute_overall_effect_of_inner_loop (ex_loop, def);
4110 :
4111 : /* Handle bitop with invariant induction expression.
4112 :
4113 : .i.e
4114 : for (int i =0 ;i < 32; i++)
4115 : tmp &= bit2;
4116 : if bit2 is an invariant in loop which could simple to
4117 : tmp &= bit2. */
4118 78515 : else if (integer_zerop (niter_desc.may_be_zero)
4119 78515 : && (bitinv_def
4120 71825 : = analyze_and_compute_bitop_with_inv_effect (loop,
4121 : phidef,
4122 : niter)))
4123 : def = bitinv_def;
4124 :
4125 : /* Handle bitwise induction expression.
4126 :
4127 : .i.e.
4128 : for (int i = 0; i != 64; i+=3)
4129 : res &= ~(1UL << i);
4130 :
4131 : RES can't be analyzed out by SCEV because it is not polynomially
4132 : expressible, but in fact final value of RES can be replaced by
4133 : RES & CONSTANT where CONSTANT all ones with bit {0,3,6,9,... ,63}
4134 : being cleared, similar for BIT_IOR_EXPR/BIT_XOR_EXPR. */
4135 78288 : else if (tree_fits_uhwi_p (niter)
4136 32376 : && (niter_num = tree_to_uhwi (niter)) != 0
4137 32361 : && niter_num < TYPE_PRECISION (TREE_TYPE (phidef))
4138 78288 : && (bit_def
4139 21482 : = analyze_and_compute_bitwise_induction_effect (loop,
4140 : phidef,
4141 : niter_num)))
4142 : def = bit_def;
4143 :
4144 78188 : else if (header_phi
4145 46405 : && integer_zerop (niter_desc.may_be_zero)
4146 78188 : && (phi_latch_final_value
4147 43552 : = compute_final_value_from_loop_phi_latch (loop,
4148 : ex_loop,
4149 : header_phi,
4150 : niter,
4151 : &folded_casts)))
4152 : def = phi_latch_final_value;
4153 :
4154 116072 : bool cond_overflow_p;
4155 116072 : if (!tree_does_not_contain_chrecs (def)
4156 37755 : || chrec_contains_symbols_defined_in_loop (def, ex_loop->num)
4157 : /* Moving the computation from the loop may prolong life range
4158 : of some ssa names, which may cause problems if they appear
4159 : on abnormal edges. */
4160 37755 : || contains_abnormal_ssa_name_p (def)
4161 : /* Do not emit expensive expressions. The rationale is that
4162 : when someone writes a code like
4163 :
4164 : while (n > 45) n -= 45;
4165 :
4166 : he probably knows that n is not large, and does not want it
4167 : to be turned into n %= 45. */
4168 153827 : || expression_expensive_p (def, &cond_overflow_p))
4169 : {
4170 79503 : if (dump_file && (dump_flags & TDF_DETAILS))
4171 : {
4172 60 : fprintf (dump_file, "not replacing:\n ");
4173 60 : print_gimple_stmt (dump_file, phi, 0);
4174 60 : fprintf (dump_file, "\n");
4175 : }
4176 79503 : gsi_next (&psi);
4177 79503 : continue;
4178 : }
4179 :
4180 : /* Eliminate the PHI node and replace it by a computation outside
4181 : the loop. */
4182 36569 : if (dump_file)
4183 : {
4184 96 : fprintf (dump_file, "\nfinal value replacement:\n ");
4185 96 : print_gimple_stmt (dump_file, phi, 0);
4186 96 : fprintf (dump_file, " with expr: ");
4187 96 : print_generic_expr (dump_file, def);
4188 96 : fprintf (dump_file, "\n");
4189 : }
4190 36569 : any = true;
4191 : /* ??? Here we'd like to have a unshare_expr that would assign
4192 : shared sub-trees to new temporary variables either gimplified
4193 : to a GIMPLE sequence or to a statement list (keeping this a
4194 : GENERIC interface). */
4195 36569 : def = unshare_expr (def);
4196 36569 : auto loc = gimple_phi_arg_location (phi, exit->dest_idx);
4197 :
4198 : /* Create the replacement statements. */
4199 36569 : gimple_seq stmts;
4200 36569 : def = force_gimple_operand (def, &stmts, false, NULL_TREE);
4201 :
4202 : /* Propagate constants immediately, but leave an unused initialization
4203 : around to avoid invalidating the SCEV cache. */
4204 43680 : if (CONSTANT_CLASS_P (def) && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
4205 7110 : replace_uses_by (rslt, def);
4206 :
4207 : /* Remove the old phi after the gimplification to make sure the
4208 : SSA name is defined by a statement so that fold_stmt during
4209 : the gimplification does not crash. */
4210 36569 : remove_phi_node (&psi, false);
4211 36569 : gassign *ass = gimple_build_assign (rslt, def);
4212 36569 : gimple_set_location (ass, loc);
4213 36569 : gimple_seq_add_stmt (&stmts, ass);
4214 :
4215 : /* If def's type has undefined overflow and there were folded
4216 : casts, rewrite all stmts added for def into arithmetics
4217 : with defined overflow behavior. */
4218 36569 : if ((folded_casts
4219 428 : && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def))
4220 724 : && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def)))
4221 36997 : || cond_overflow_p)
4222 : {
4223 2249 : gimple_stmt_iterator gsi2;
4224 2249 : gsi2 = gsi_start (stmts);
4225 20701 : while (!gsi_end_p (gsi2))
4226 : {
4227 18452 : if (gimple_needing_rewrite_undefined (gsi_stmt (gsi2)))
4228 534 : rewrite_to_defined_unconditional (&gsi2);
4229 18452 : gsi_next (&gsi2);
4230 : }
4231 : }
4232 36569 : gimple_stmt_iterator gsi = gsi_after_labels (exit->dest);
4233 36569 : gsi_insert_seq_before (&gsi, stmts, GSI_SAME_STMT);
4234 36569 : if (dump_file)
4235 : {
4236 96 : fprintf (dump_file, " final stmt:\n ");
4237 96 : print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (rslt), 0);
4238 96 : fprintf (dump_file, "\n");
4239 : }
4240 :
4241 : /* Re-fold immediate uses of the replaced def, but avoid
4242 : CFG manipulations from this function. For now only do
4243 : a single-level re-folding, not re-folding uses of
4244 : folded uses. */
4245 36569 : if (! SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rslt))
4246 : {
4247 36568 : gimple *use_stmt;
4248 36568 : imm_use_iterator imm_iter;
4249 36568 : auto_vec<gimple *, 4> to_fold;
4250 73955 : FOR_EACH_IMM_USE_STMT (use_stmt, imm_iter, rslt)
4251 37387 : if (!stmt_can_throw_internal (cfun, use_stmt))
4252 37383 : to_fold.safe_push (use_stmt);
4253 : /* Delay folding until after the immediate use walk is completed
4254 : as we have an active ranger and that might walk immediate
4255 : uses of rslt again. See PR122502. */
4256 147087 : for (gimple *use_stmt : to_fold)
4257 : {
4258 37383 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
4259 37383 : if (fold_stmt (&gsi, follow_all_ssa_edges))
4260 1991 : update_stmt (gsi_stmt (gsi));
4261 : }
4262 36568 : }
4263 : }
4264 :
4265 : return any;
4266 715224 : }
4267 :
4268 : #include "gt-tree-scalar-evolution.h"
|