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