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