1 /* Scalar evolution detector.
2 Copyright (C) 2003-2023 Free Software Foundation, Inc.
3 Contributed by Sebastian Pop <s.pop@laposte.net>
5 This file is part of GCC.
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
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
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/>. */
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.
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).
45 A short sketch of the algorithm is:
47 Given a scalar variable to be analyzed, follow the SSA edge to
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.
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).
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.
75 Example 1: Illustration of the basic algorithm.
81 | if (c > 10) exit_loop
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:
119 or in terms of a C program:
122 | for (x = 0; x <= 7; x++)
128 Example 2a: Illustration of the algorithm on nested loops.
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:
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:
157 Example 2b: Multivariate chains of recurrences.
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.
180 Example 3: Higher degree polynomials.
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
197 Example 4: Lucas, Fibonacci, or mixers in general.
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)".
215 The symbolic chrec representation contains all the semantics of the
216 original code. What is more difficult is to use this information.
218 Example 5: Flip-flops, or exchangers.
230 Based on these symbolic chrecs, it is possible to refine this
231 information into the more precise PERIODIC_CHRECs:
236 This transformation is not yet implemented.
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.
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
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
258 #include "coretypes.h"
262 #include "optabs-query.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"
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"
288 static tree
analyze_scalar_evolution_1 (class loop
*, tree
);
289 static tree
analyze_scalar_evolution_for_address_of (class loop
*loop
,
292 /* The cached information about an SSA name with version NAME_VERSION,
293 claiming that below basic block with index INSTANTIATED_BELOW, the
294 value of the SSA name can be expressed as CHREC. */
296 struct GTY((for_user
)) scev_info_str
{
297 unsigned int name_version
;
298 int instantiated_below
;
302 /* Counters for the scev database. */
303 static unsigned nb_set_scev
= 0;
304 static unsigned nb_get_scev
= 0;
306 struct scev_info_hasher
: ggc_ptr_hash
<scev_info_str
>
308 static hashval_t
hash (scev_info_str
*i
);
309 static bool equal (const scev_info_str
*a
, const scev_info_str
*b
);
312 static GTY (()) hash_table
<scev_info_hasher
> *scalar_evolution_info
;
315 /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */
317 static inline struct scev_info_str
*
318 new_scev_info_str (basic_block instantiated_below
, tree var
)
320 struct scev_info_str
*res
;
322 res
= ggc_alloc
<scev_info_str
> ();
323 res
->name_version
= SSA_NAME_VERSION (var
);
324 res
->chrec
= chrec_not_analyzed_yet
;
325 res
->instantiated_below
= instantiated_below
->index
;
330 /* Computes a hash function for database element ELT. */
333 scev_info_hasher::hash (scev_info_str
*elt
)
335 return elt
->name_version
^ elt
->instantiated_below
;
338 /* Compares database elements E1 and E2. */
341 scev_info_hasher::equal (const scev_info_str
*elt1
, const scev_info_str
*elt2
)
343 return (elt1
->name_version
== elt2
->name_version
344 && elt1
->instantiated_below
== elt2
->instantiated_below
);
347 /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block.
348 A first query on VAR returns chrec_not_analyzed_yet. */
351 find_var_scev_info (basic_block instantiated_below
, tree var
)
353 struct scev_info_str
*res
;
354 struct scev_info_str tmp
;
356 tmp
.name_version
= SSA_NAME_VERSION (var
);
357 tmp
.instantiated_below
= instantiated_below
->index
;
358 scev_info_str
**slot
= scalar_evolution_info
->find_slot (&tmp
, INSERT
);
361 *slot
= new_scev_info_str (instantiated_below
, var
);
368 /* Hashtable helpers for a temporary hash-table used when
369 analyzing a scalar evolution, instantiating a CHREC or
372 class instantiate_cache_type
376 vec
<scev_info_str
> entries
;
378 instantiate_cache_type () : map (NULL
), entries (vNULL
) {}
379 ~instantiate_cache_type ();
380 tree
get (unsigned slot
) { return entries
[slot
].chrec
; }
381 void set (unsigned slot
, tree chrec
) { entries
[slot
].chrec
= chrec
; }
384 instantiate_cache_type::~instantiate_cache_type ()
393 /* Cache to avoid infinite recursion when instantiating an SSA name.
394 Live during the outermost analyze_scalar_evolution, instantiate_scev
395 or resolve_mixers call. */
396 static instantiate_cache_type
*global_cache
;
399 /* Return true when PHI is a loop-phi-node. */
402 loop_phi_node_p (gimple
*phi
)
404 /* The implementation of this function is based on the following
405 property: "all the loop-phi-nodes of a loop are contained in the
406 loop's header basic block". */
408 return loop_containing_stmt (phi
)->header
== gimple_bb (phi
);
411 /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP.
412 In general, in the case of multivariate evolutions we want to get
413 the evolution in different loops. LOOP specifies the level for
414 which to get the evolution.
418 | for (j = 0; j < 100; j++)
420 | for (k = 0; k < 100; k++)
422 | i = k + j; - Here the value of i is a function of j, k.
424 | ... = i - Here the value of i is a function of j.
426 | ... = i - Here the value of i is a scalar.
432 | i_1 = phi (i_0, i_2)
436 This loop has the same effect as:
437 LOOP_1 has the same effect as:
441 The overall effect of the loop, "i_0 + 20" in the previous example,
442 is obtained by passing in the parameters: LOOP = 1,
443 EVOLUTION_FN = {i_0, +, 2}_1.
447 compute_overall_effect_of_inner_loop (class loop
*loop
, tree evolution_fn
)
451 if (evolution_fn
== chrec_dont_know
)
452 return chrec_dont_know
;
454 else if (TREE_CODE (evolution_fn
) == POLYNOMIAL_CHREC
)
456 class loop
*inner_loop
= get_chrec_loop (evolution_fn
);
458 if (inner_loop
== loop
459 || flow_loop_nested_p (loop
, inner_loop
))
461 tree nb_iter
= number_of_latch_executions (inner_loop
);
463 if (nb_iter
== chrec_dont_know
)
464 return chrec_dont_know
;
469 /* evolution_fn is the evolution function in LOOP. Get
470 its value in the nb_iter-th iteration. */
471 res
= chrec_apply (inner_loop
->num
, evolution_fn
, nb_iter
);
473 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
474 res
= instantiate_parameters (loop
, res
);
476 /* Continue the computation until ending on a parent of LOOP. */
477 return compute_overall_effect_of_inner_loop (loop
, res
);
484 /* If the evolution function is an invariant, there is nothing to do. */
485 else if (no_evolution_in_loop_p (evolution_fn
, loop
->num
, &val
) && val
)
489 return chrec_dont_know
;
492 /* Associate CHREC to SCALAR. */
495 set_scalar_evolution (basic_block instantiated_below
, tree scalar
, tree chrec
)
499 if (TREE_CODE (scalar
) != SSA_NAME
)
502 scalar_info
= find_var_scev_info (instantiated_below
, scalar
);
506 if (dump_flags
& TDF_SCEV
)
508 fprintf (dump_file
, "(set_scalar_evolution \n");
509 fprintf (dump_file
, " instantiated_below = %d \n",
510 instantiated_below
->index
);
511 fprintf (dump_file
, " (scalar = ");
512 print_generic_expr (dump_file
, scalar
);
513 fprintf (dump_file
, ")\n (scalar_evolution = ");
514 print_generic_expr (dump_file
, chrec
);
515 fprintf (dump_file
, "))\n");
517 if (dump_flags
& TDF_STATS
)
521 *scalar_info
= chrec
;
524 /* Retrieve the chrec associated to SCALAR instantiated below
525 INSTANTIATED_BELOW block. */
528 get_scalar_evolution (basic_block instantiated_below
, tree scalar
)
534 if (dump_flags
& TDF_SCEV
)
536 fprintf (dump_file
, "(get_scalar_evolution \n");
537 fprintf (dump_file
, " (scalar = ");
538 print_generic_expr (dump_file
, scalar
);
539 fprintf (dump_file
, ")\n");
541 if (dump_flags
& TDF_STATS
)
545 if (VECTOR_TYPE_P (TREE_TYPE (scalar
))
546 || TREE_CODE (TREE_TYPE (scalar
)) == COMPLEX_TYPE
)
547 /* For chrec_dont_know we keep the symbolic form. */
550 switch (TREE_CODE (scalar
))
553 if (SSA_NAME_IS_DEFAULT_DEF (scalar
))
556 res
= *find_var_scev_info (instantiated_below
, scalar
);
566 res
= chrec_not_analyzed_yet
;
570 if (dump_file
&& (dump_flags
& TDF_SCEV
))
572 fprintf (dump_file
, " (scalar_evolution = ");
573 print_generic_expr (dump_file
, res
);
574 fprintf (dump_file
, "))\n");
581 /* Depth first search algorithm. */
592 scev_dfs (class loop
*loop_
, gphi
*phi_
, tree init_cond_
)
593 : loop (loop_
), loop_phi_node (phi_
), init_cond (init_cond_
) {}
594 t_bool
get_ev (tree
*, tree
);
597 t_bool
follow_ssa_edge_expr (gimple
*, tree
, tree
*, int);
598 t_bool
follow_ssa_edge_binary (gimple
*at_stmt
,
599 tree type
, tree rhs0
, enum tree_code code
,
600 tree rhs1
, tree
*evolution_of_loop
, int limit
);
601 t_bool
follow_ssa_edge_in_condition_phi_branch (int i
,
603 tree
*evolution_of_branch
,
604 tree init_cond
, int limit
);
605 t_bool
follow_ssa_edge_in_condition_phi (gphi
*condition_phi
,
606 tree
*evolution_of_loop
, int limit
);
607 t_bool
follow_ssa_edge_inner_loop_phi (gphi
*loop_phi_node
,
608 tree
*evolution_of_loop
, int limit
);
609 tree
add_to_evolution (tree chrec_before
, enum tree_code code
,
610 tree to_add
, gimple
*at_stmt
);
611 tree
add_to_evolution_1 (tree chrec_before
, tree to_add
, gimple
*at_stmt
);
619 scev_dfs::get_ev (tree
*ev_fn
, tree arg
)
621 *ev_fn
= chrec_dont_know
;
622 return follow_ssa_edge_expr (loop_phi_node
, arg
, ev_fn
, 0);
625 /* Helper function for add_to_evolution. Returns the evolution
626 function for an assignment of the form "a = b + c", where "a" and
627 "b" are on the strongly connected component. CHREC_BEFORE is the
628 information that we already have collected up to this point.
629 TO_ADD is the evolution of "c".
631 When CHREC_BEFORE has an evolution part in LOOP_NB, add to this
632 evolution the expression TO_ADD, otherwise construct an evolution
633 part for this loop. */
636 scev_dfs::add_to_evolution_1 (tree chrec_before
, tree to_add
, gimple
*at_stmt
)
638 tree type
, left
, right
;
639 unsigned loop_nb
= loop
->num
;
642 switch (TREE_CODE (chrec_before
))
644 case POLYNOMIAL_CHREC
:
645 chloop
= get_chrec_loop (chrec_before
);
647 || flow_loop_nested_p (chloop
, loop
))
651 type
= chrec_type (chrec_before
);
653 /* When there is no evolution part in this loop, build it. */
658 right
= SCALAR_FLOAT_TYPE_P (type
)
659 ? build_real (type
, dconst0
)
660 : build_int_cst (type
, 0);
664 var
= CHREC_VARIABLE (chrec_before
);
665 left
= CHREC_LEFT (chrec_before
);
666 right
= CHREC_RIGHT (chrec_before
);
669 to_add
= chrec_convert (type
, to_add
, at_stmt
);
670 right
= chrec_convert_rhs (type
, right
, at_stmt
);
671 right
= chrec_fold_plus (chrec_type (right
), right
, to_add
);
672 return build_polynomial_chrec (var
, left
, right
);
676 gcc_assert (flow_loop_nested_p (loop
, chloop
));
678 /* Search the evolution in LOOP_NB. */
679 left
= add_to_evolution_1 (CHREC_LEFT (chrec_before
),
681 right
= CHREC_RIGHT (chrec_before
);
682 right
= chrec_convert_rhs (chrec_type (left
), right
, at_stmt
);
683 return build_polynomial_chrec (CHREC_VARIABLE (chrec_before
),
688 /* These nodes do not depend on a loop. */
689 if (chrec_before
== chrec_dont_know
)
690 return chrec_dont_know
;
693 right
= chrec_convert_rhs (chrec_type (left
), to_add
, at_stmt
);
694 /* When we add the first evolution we need to replace the symbolic
695 evolution we've put in when the DFS reached the loop PHI node
696 with the initial value. There's only a limited cases of
697 extra operations ontop of that symbol allowed, namely
698 sign-conversions we can look through. For other cases we leave
699 the symbolic initial condition which causes build_polynomial_chrec
700 to return chrec_dont_know. See PR42512, PR66375 and PR107176 for
701 cases we mishandled before. */
702 STRIP_NOPS (chrec_before
);
703 if (chrec_before
== gimple_phi_result (loop_phi_node
))
704 left
= fold_convert (TREE_TYPE (left
), init_cond
);
705 return build_polynomial_chrec (loop_nb
, left
, right
);
709 /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension
712 Description (provided for completeness, for those who read code in
713 a plane, and for my poor 62 bytes brain that would have forgotten
714 all this in the next two or three months):
716 The algorithm of translation of programs from the SSA representation
717 into the chrecs syntax is based on a pattern matching. After having
718 reconstructed the overall tree expression for a loop, there are only
719 two cases that can arise:
721 1. a = loop-phi (init, a + expr)
722 2. a = loop-phi (init, expr)
724 where EXPR is either a scalar constant with respect to the analyzed
725 loop (this is a degree 0 polynomial), or an expression containing
726 other loop-phi definitions (these are higher degree polynomials).
733 | a = phi (init, a + 5)
740 | a = phi (inita, 2 * b + 3)
741 | b = phi (initb, b + 1)
744 For the first case, the semantics of the SSA representation is:
746 | a (x) = init + \sum_{j = 0}^{x - 1} expr (j)
748 that is, there is a loop index "x" that determines the scalar value
749 of the variable during the loop execution. During the first
750 iteration, the value is that of the initial condition INIT, while
751 during the subsequent iterations, it is the sum of the initial
752 condition with the sum of all the values of EXPR from the initial
753 iteration to the before last considered iteration.
755 For the second case, the semantics of the SSA program is:
757 | a (x) = init, if x = 0;
758 | expr (x - 1), otherwise.
760 The second case corresponds to the PEELED_CHREC, whose syntax is
761 close to the syntax of a loop-phi-node:
763 | phi (init, expr) vs. (init, expr)_x
765 The proof of the translation algorithm for the first case is a
766 proof by structural induction based on the degree of EXPR.
769 When EXPR is a constant with respect to the analyzed loop, or in
770 other words when EXPR is a polynomial of degree 0, the evolution of
771 the variable A in the loop is an affine function with an initial
772 condition INIT, and a step EXPR. In order to show this, we start
773 from the semantics of the SSA representation:
775 f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
777 and since "expr (j)" is a constant with respect to "j",
779 f (x) = init + x * expr
781 Finally, based on the semantics of the pure sum chrecs, by
782 identification we get the corresponding chrecs syntax:
784 f (x) = init * \binom{x}{0} + expr * \binom{x}{1}
785 f (x) -> {init, +, expr}_x
788 Suppose that EXPR is a polynomial of degree N with respect to the
789 analyzed loop_x for which we have already determined that it is
790 written under the chrecs syntax:
792 | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x)
794 We start from the semantics of the SSA program:
796 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j)
798 | f (x) = init + \sum_{j = 0}^{x - 1}
799 | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1})
801 | f (x) = init + \sum_{j = 0}^{x - 1}
802 | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k})
804 | f (x) = init + \sum_{k = 0}^{n - 1}
805 | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k})
807 | f (x) = init + \sum_{k = 0}^{n - 1}
808 | (b_k * \binom{x}{k + 1})
810 | f (x) = init + b_0 * \binom{x}{1} + ...
811 | + b_{n-1} * \binom{x}{n}
813 | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ...
814 | + b_{n-1} * \binom{x}{n}
817 And finally from the definition of the chrecs syntax, we identify:
818 | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x
820 This shows the mechanism that stands behind the add_to_evolution
821 function. An important point is that the use of symbolic
822 parameters avoids the need of an analysis schedule.
829 | a = phi (inita, a + 2 + b)
830 | b = phi (initb, b + 1)
833 When analyzing "a", the algorithm keeps "b" symbolically:
835 | a -> {inita, +, 2 + b}_1
837 Then, after instantiation, the analyzer ends on the evolution:
839 | a -> {inita, +, 2 + initb, +, 1}_1
844 scev_dfs::add_to_evolution (tree chrec_before
, enum tree_code code
,
845 tree to_add
, gimple
*at_stmt
)
847 tree type
= chrec_type (to_add
);
848 tree res
= NULL_TREE
;
850 if (to_add
== NULL_TREE
)
853 /* TO_ADD is either a scalar, or a parameter. TO_ADD is not
854 instantiated at this point. */
855 if (TREE_CODE (to_add
) == POLYNOMIAL_CHREC
)
856 /* This should not happen. */
857 return chrec_dont_know
;
859 if (dump_file
&& (dump_flags
& TDF_SCEV
))
861 fprintf (dump_file
, "(add_to_evolution \n");
862 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
863 fprintf (dump_file
, " (chrec_before = ");
864 print_generic_expr (dump_file
, chrec_before
);
865 fprintf (dump_file
, ")\n (to_add = ");
866 print_generic_expr (dump_file
, to_add
);
867 fprintf (dump_file
, ")\n");
870 if (code
== MINUS_EXPR
)
871 to_add
= chrec_fold_multiply (type
, to_add
, SCALAR_FLOAT_TYPE_P (type
)
872 ? build_real (type
, dconstm1
)
873 : build_int_cst_type (type
, -1));
875 res
= add_to_evolution_1 (chrec_before
, to_add
, at_stmt
);
877 if (dump_file
&& (dump_flags
& TDF_SCEV
))
879 fprintf (dump_file
, " (res = ");
880 print_generic_expr (dump_file
, res
);
881 fprintf (dump_file
, "))\n");
888 /* Follow the ssa edge into the binary expression RHS0 CODE RHS1.
889 Return true if the strongly connected component has been found. */
892 scev_dfs::follow_ssa_edge_binary (gimple
*at_stmt
, tree type
, tree rhs0
,
893 enum tree_code code
, tree rhs1
,
894 tree
*evolution_of_loop
, int limit
)
896 t_bool res
= t_false
;
901 case POINTER_PLUS_EXPR
:
903 if (TREE_CODE (rhs0
) == SSA_NAME
)
905 if (TREE_CODE (rhs1
) == SSA_NAME
)
907 /* Match an assignment under the form:
910 /* We want only assignments of form "name + name" contribute to
911 LIMIT, as the other cases do not necessarily contribute to
912 the complexity of the expression. */
915 evol
= *evolution_of_loop
;
916 res
= follow_ssa_edge_expr (at_stmt
, rhs0
, &evol
, limit
);
918 *evolution_of_loop
= add_to_evolution
919 (chrec_convert (type
, evol
, at_stmt
), code
, rhs1
, at_stmt
);
920 else if (res
== t_false
)
922 res
= follow_ssa_edge_expr
923 (at_stmt
, rhs1
, evolution_of_loop
, limit
);
925 *evolution_of_loop
= add_to_evolution
926 (chrec_convert (type
, *evolution_of_loop
, at_stmt
),
927 code
, rhs0
, at_stmt
);
932 gcc_unreachable (); /* Handled in caller. */
935 else if (TREE_CODE (rhs1
) == SSA_NAME
)
937 /* Match an assignment under the form:
939 res
= follow_ssa_edge_expr (at_stmt
, rhs1
, evolution_of_loop
, limit
);
941 *evolution_of_loop
= add_to_evolution
942 (chrec_convert (type
, *evolution_of_loop
, at_stmt
),
943 code
, rhs0
, at_stmt
);
947 /* Otherwise, match an assignment under the form:
949 /* And there is nothing to do. */
954 /* This case is under the form "opnd0 = rhs0 - rhs1". */
955 if (TREE_CODE (rhs0
) == SSA_NAME
)
956 gcc_unreachable (); /* Handled in caller. */
958 /* Otherwise, match an assignment under the form:
960 /* And there is nothing to do. */
971 /* Checks whether the I-th argument of a PHI comes from a backedge. */
974 backedge_phi_arg_p (gphi
*phi
, int i
)
976 const_edge e
= gimple_phi_arg_edge (phi
, i
);
978 /* We would in fact like to test EDGE_DFS_BACK here, but we do not care
979 about updating it anywhere, and this should work as well most of the
981 if (e
->flags
& EDGE_IRREDUCIBLE_LOOP
)
987 /* Helper function for one branch of the condition-phi-node. Return
988 true if the strongly connected component has been found following
992 scev_dfs::follow_ssa_edge_in_condition_phi_branch (int i
,
994 tree
*evolution_of_branch
,
995 tree init_cond
, int limit
)
997 tree branch
= PHI_ARG_DEF (condition_phi
, i
);
998 *evolution_of_branch
= chrec_dont_know
;
1000 /* Do not follow back edges (they must belong to an irreducible loop, which
1001 we really do not want to worry about). */
1002 if (backedge_phi_arg_p (condition_phi
, i
))
1005 if (TREE_CODE (branch
) == SSA_NAME
)
1007 *evolution_of_branch
= init_cond
;
1008 return follow_ssa_edge_expr (condition_phi
, branch
,
1009 evolution_of_branch
, limit
);
1012 /* This case occurs when one of the condition branches sets
1013 the variable to a constant: i.e. a phi-node like
1014 "a_2 = PHI <a_7(5), 2(6)>;".
1016 FIXME: This case have to be refined correctly:
1017 in some cases it is possible to say something better than
1018 chrec_dont_know, for example using a wrap-around notation. */
1022 /* This function merges the branches of a condition-phi-node in a
1026 scev_dfs::follow_ssa_edge_in_condition_phi (gphi
*condition_phi
,
1027 tree
*evolution_of_loop
, int limit
)
1030 tree init
= *evolution_of_loop
;
1031 tree evolution_of_branch
;
1032 t_bool res
= follow_ssa_edge_in_condition_phi_branch (0, condition_phi
,
1033 &evolution_of_branch
,
1035 if (res
== t_false
|| res
== t_dont_know
)
1038 *evolution_of_loop
= evolution_of_branch
;
1040 n
= gimple_phi_num_args (condition_phi
);
1041 for (i
= 1; i
< n
; i
++)
1043 /* Quickly give up when the evolution of one of the branches is
1045 if (*evolution_of_loop
== chrec_dont_know
)
1048 /* Increase the limit by the PHI argument number to avoid exponential
1049 time and memory complexity. */
1050 res
= follow_ssa_edge_in_condition_phi_branch (i
, condition_phi
,
1051 &evolution_of_branch
,
1053 if (res
== t_false
|| res
== t_dont_know
)
1056 *evolution_of_loop
= chrec_merge (*evolution_of_loop
,
1057 evolution_of_branch
);
1063 /* Follow an SSA edge in an inner loop. It computes the overall
1064 effect of the loop, and following the symbolic initial conditions,
1065 it follows the edges in the parent loop. The inner loop is
1066 considered as a single statement. */
1069 scev_dfs::follow_ssa_edge_inner_loop_phi (gphi
*loop_phi_node
,
1070 tree
*evolution_of_loop
, int limit
)
1072 class loop
*loop
= loop_containing_stmt (loop_phi_node
);
1073 tree ev
= analyze_scalar_evolution (loop
, PHI_RESULT (loop_phi_node
));
1075 /* Sometimes, the inner loop is too difficult to analyze, and the
1076 result of the analysis is a symbolic parameter. */
1077 if (ev
== PHI_RESULT (loop_phi_node
))
1079 t_bool res
= t_false
;
1080 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1082 for (i
= 0; i
< n
; i
++)
1084 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1087 /* Follow the edges that exit the inner loop. */
1088 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1089 if (!flow_bb_inside_loop_p (loop
, bb
))
1090 res
= follow_ssa_edge_expr (loop_phi_node
,
1091 arg
, evolution_of_loop
, limit
);
1096 /* If the path crosses this loop-phi, give up. */
1098 *evolution_of_loop
= chrec_dont_know
;
1103 /* Otherwise, compute the overall effect of the inner loop. */
1104 ev
= compute_overall_effect_of_inner_loop (loop
, ev
);
1105 return follow_ssa_edge_expr (loop_phi_node
, ev
, evolution_of_loop
, limit
);
1108 /* Follow the ssa edge into the expression EXPR.
1109 Return true if the strongly connected component has been found. */
1112 scev_dfs::follow_ssa_edge_expr (gimple
*at_stmt
, tree expr
,
1113 tree
*evolution_of_loop
, int limit
)
1115 gphi
*halting_phi
= loop_phi_node
;
1116 enum tree_code code
;
1117 tree type
, rhs0
, rhs1
= NULL_TREE
;
1119 /* The EXPR is one of the following cases:
1123 - a POINTER_PLUS_EXPR,
1125 - other cases are not yet handled. */
1127 /* For SSA_NAME look at the definition statement, handling
1128 PHI nodes and otherwise expand appropriately for the expression
1130 if (TREE_CODE (expr
) == SSA_NAME
)
1132 gimple
*def
= SSA_NAME_DEF_STMT (expr
);
1134 if (gimple_nop_p (def
))
1137 /* Give up if the path is longer than the MAX that we allow. */
1138 if (limit
> param_scev_max_expr_complexity
)
1140 *evolution_of_loop
= chrec_dont_know
;
1144 if (gphi
*phi
= dyn_cast
<gphi
*>(def
))
1146 if (!loop_phi_node_p (phi
))
1147 /* DEF is a condition-phi-node. Follow the branches, and
1148 record their evolutions. Finally, merge the collected
1149 information and set the approximation to the main
1151 return follow_ssa_edge_in_condition_phi (phi
, evolution_of_loop
,
1154 /* When the analyzed phi is the halting_phi, the
1155 depth-first search is over: we have found a path from
1156 the halting_phi to itself in the loop. */
1157 if (phi
== halting_phi
)
1159 *evolution_of_loop
= expr
;
1163 /* Otherwise, the evolution of the HALTING_PHI depends
1164 on the evolution of another loop-phi-node, i.e. the
1165 evolution function is a higher degree polynomial. */
1166 class loop
*def_loop
= loop_containing_stmt (def
);
1167 if (def_loop
== loop
)
1171 if (flow_loop_nested_p (loop
, def_loop
))
1172 return follow_ssa_edge_inner_loop_phi (phi
, evolution_of_loop
,
1179 /* At this level of abstraction, the program is just a set
1180 of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no
1181 other def to be handled. */
1182 if (!is_gimple_assign (def
))
1185 code
= gimple_assign_rhs_code (def
);
1186 switch (get_gimple_rhs_class (code
))
1188 case GIMPLE_BINARY_RHS
:
1189 rhs0
= gimple_assign_rhs1 (def
);
1190 rhs1
= gimple_assign_rhs2 (def
);
1192 case GIMPLE_UNARY_RHS
:
1193 case GIMPLE_SINGLE_RHS
:
1194 rhs0
= gimple_assign_rhs1 (def
);
1199 type
= TREE_TYPE (gimple_assign_lhs (def
));
1204 code
= TREE_CODE (expr
);
1205 type
= TREE_TYPE (expr
);
1206 /* Via follow_ssa_edge_inner_loop_phi we arrive here with the
1207 GENERIC scalar evolution of the inner loop. */
1211 rhs0
= TREE_OPERAND (expr
, 0);
1213 case POINTER_PLUS_EXPR
:
1216 rhs0
= TREE_OPERAND (expr
, 0);
1217 rhs1
= TREE_OPERAND (expr
, 1);
1218 STRIP_USELESS_TYPE_CONVERSION (rhs0
);
1219 STRIP_USELESS_TYPE_CONVERSION (rhs1
);
1230 /* This assignment is under the form "a_1 = (cast) rhs. We cannot
1231 validate any precision altering conversion during the SCC
1232 analysis, so don't even try. */
1233 if (!tree_nop_conversion_p (type
, TREE_TYPE (rhs0
)))
1235 t_bool res
= follow_ssa_edge_expr (at_stmt
, rhs0
,
1236 evolution_of_loop
, limit
);
1238 *evolution_of_loop
= chrec_convert (type
, *evolution_of_loop
,
1244 /* This assignment is under the form "a_1 = 7". */
1249 /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */
1250 if (TREE_CODE (TREE_OPERAND (rhs0
, 0)) != MEM_REF
)
1252 tree mem
= TREE_OPERAND (rhs0
, 0);
1253 rhs0
= TREE_OPERAND (mem
, 0);
1254 rhs1
= TREE_OPERAND (mem
, 1);
1255 code
= POINTER_PLUS_EXPR
;
1258 case POINTER_PLUS_EXPR
:
1261 /* This case is under the form "rhs0 +- rhs1". */
1262 if (TREE_CODE (rhs0
) == SSA_NAME
1263 && (TREE_CODE (rhs1
) != SSA_NAME
|| code
== MINUS_EXPR
))
1265 /* Match an assignment under the form:
1267 t_bool res
= follow_ssa_edge_expr (at_stmt
, rhs0
,
1268 evolution_of_loop
, limit
);
1270 *evolution_of_loop
= add_to_evolution
1271 (chrec_convert (type
, *evolution_of_loop
, at_stmt
),
1272 code
, rhs1
, at_stmt
);
1275 /* Else search for the SCC in both rhs0 and rhs1. */
1276 return follow_ssa_edge_binary (at_stmt
, type
, rhs0
, code
, rhs1
,
1277 evolution_of_loop
, limit
);
1285 /* This section selects the loops that will be good candidates for the
1286 scalar evolution analysis. For the moment, greedily select all the
1287 loop nests we could analyze. */
1289 /* For a loop with a single exit edge, return the COND_EXPR that
1290 guards the exit edge. If the expression is too difficult to
1291 analyze, then give up. */
1294 get_loop_exit_condition (const class loop
*loop
)
1297 edge exit_edge
= single_exit (loop
);
1299 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1300 fprintf (dump_file
, "(get_loop_exit_condition \n ");
1303 res
= safe_dyn_cast
<gcond
*> (*gsi_last_bb (exit_edge
->src
));
1305 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1307 print_gimple_stmt (dump_file
, res
, 0);
1308 fprintf (dump_file
, ")\n");
1315 /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP.
1316 Handle below case and return the corresponding POLYNOMIAL_CHREC:
1318 # i_17 = PHI <i_13(5), 0(3)>
1319 # _20 = PHI <_5(5), start_4(D)(3)>
1322 _5 = start_4(D) + i_13;
1324 Though variable _20 appears as a PEELED_CHREC in the form of
1325 (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP.
1330 simplify_peeled_chrec (class loop
*loop
, tree arg
, tree init_cond
)
1332 aff_tree aff1
, aff2
;
1333 tree ev
, left
, right
, type
, step_val
;
1334 hash_map
<tree
, name_expansion
*> *peeled_chrec_map
= NULL
;
1336 ev
= instantiate_parameters (loop
, analyze_scalar_evolution (loop
, arg
));
1337 if (ev
== NULL_TREE
|| TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
1338 return chrec_dont_know
;
1340 left
= CHREC_LEFT (ev
);
1341 right
= CHREC_RIGHT (ev
);
1342 type
= TREE_TYPE (left
);
1343 step_val
= chrec_fold_plus (type
, init_cond
, right
);
1345 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1346 if "left" equals to "init + right". */
1347 if (operand_equal_p (left
, step_val
, 0))
1349 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1350 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1352 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1355 /* The affine code only deals with pointer and integer types. */
1356 if (!POINTER_TYPE_P (type
)
1357 && !INTEGRAL_TYPE_P (type
))
1358 return chrec_dont_know
;
1360 /* Try harder to check if they are equal. */
1361 tree_to_aff_combination_expand (left
, type
, &aff1
, &peeled_chrec_map
);
1362 tree_to_aff_combination_expand (step_val
, type
, &aff2
, &peeled_chrec_map
);
1363 free_affine_expand_cache (&peeled_chrec_map
);
1364 aff_combination_scale (&aff2
, -1);
1365 aff_combination_add (&aff1
, &aff2
);
1367 /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP
1368 if "left" equals to "init + right". */
1369 if (aff_combination_zero_p (&aff1
))
1371 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1372 fprintf (dump_file
, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n");
1374 return build_polynomial_chrec (loop
->num
, init_cond
, right
);
1376 return chrec_dont_know
;
1379 /* Given a LOOP_PHI_NODE, this function determines the evolution
1380 function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */
1383 analyze_evolution_in_loop (gphi
*loop_phi_node
,
1386 int i
, n
= gimple_phi_num_args (loop_phi_node
);
1387 tree evolution_function
= chrec_not_analyzed_yet
;
1388 class loop
*loop
= loop_containing_stmt (loop_phi_node
);
1390 static bool simplify_peeled_chrec_p
= true;
1392 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1394 fprintf (dump_file
, "(analyze_evolution_in_loop \n");
1395 fprintf (dump_file
, " (loop_phi_node = ");
1396 print_gimple_stmt (dump_file
, loop_phi_node
, 0);
1397 fprintf (dump_file
, ")\n");
1400 for (i
= 0; i
< n
; i
++)
1402 tree arg
= PHI_ARG_DEF (loop_phi_node
, i
);
1403 tree ev_fn
= chrec_dont_know
;
1406 /* Select the edges that enter the loop body. */
1407 bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1408 if (!flow_bb_inside_loop_p (loop
, bb
))
1411 if (TREE_CODE (arg
) == SSA_NAME
)
1415 /* Pass in the initial condition to the follow edge function. */
1416 scev_dfs
dfs (loop
, loop_phi_node
, init_cond
);
1417 res
= dfs
.get_ev (&ev_fn
, arg
);
1419 /* If ev_fn has no evolution in the inner loop, and the
1420 init_cond is not equal to ev_fn, then we have an
1421 ambiguity between two possible values, as we cannot know
1422 the number of iterations at this point. */
1423 if (TREE_CODE (ev_fn
) != POLYNOMIAL_CHREC
1424 && no_evolution_in_loop_p (ev_fn
, loop
->num
, &val
) && val
1425 && !operand_equal_p (init_cond
, ev_fn
, 0))
1426 ev_fn
= chrec_dont_know
;
1431 /* When it is impossible to go back on the same
1432 loop_phi_node by following the ssa edges, the
1433 evolution is represented by a peeled chrec, i.e. the
1434 first iteration, EV_FN has the value INIT_COND, then
1435 all the other iterations it has the value of ARG.
1436 For the moment, PEELED_CHREC nodes are not built. */
1439 ev_fn
= chrec_dont_know
;
1440 /* Try to recognize POLYNOMIAL_CHREC which appears in
1441 the form of PEELED_CHREC, but guard the process with
1442 a bool variable to keep the analyzer from infinite
1443 recurrence for real PEELED_RECs. */
1444 if (simplify_peeled_chrec_p
&& TREE_CODE (arg
) == SSA_NAME
)
1446 simplify_peeled_chrec_p
= false;
1447 ev_fn
= simplify_peeled_chrec (loop
, arg
, init_cond
);
1448 simplify_peeled_chrec_p
= true;
1452 /* When there are multiple back edges of the loop (which in fact never
1453 happens currently, but nevertheless), merge their evolutions. */
1454 evolution_function
= chrec_merge (evolution_function
, ev_fn
);
1456 if (evolution_function
== chrec_dont_know
)
1460 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1462 fprintf (dump_file
, " (evolution_function = ");
1463 print_generic_expr (dump_file
, evolution_function
);
1464 fprintf (dump_file
, "))\n");
1467 return evolution_function
;
1470 /* Looks to see if VAR is a copy of a constant (via straightforward assignments
1471 or degenerate phi's). If so, returns the constant; else, returns VAR. */
1474 follow_copies_to_constant (tree var
)
1477 while (TREE_CODE (res
) == SSA_NAME
1478 /* We face not updated SSA form in multiple places and this walk
1479 may end up in sibling loops so we have to guard it. */
1480 && !name_registered_for_update_p (res
))
1482 gimple
*def
= SSA_NAME_DEF_STMT (res
);
1483 if (gphi
*phi
= dyn_cast
<gphi
*> (def
))
1485 if (tree rhs
= degenerate_phi_result (phi
))
1490 else if (gimple_assign_single_p (def
))
1491 /* Will exit loop if not an SSA_NAME. */
1492 res
= gimple_assign_rhs1 (def
);
1496 if (CONSTANT_CLASS_P (res
))
1501 /* Given a loop-phi-node, return the initial conditions of the
1502 variable on entry of the loop. When the CCP has propagated
1503 constants into the loop-phi-node, the initial condition is
1504 instantiated, otherwise the initial condition is kept symbolic.
1505 This analyzer does not analyze the evolution outside the current
1506 loop, and leaves this task to the on-demand tree reconstructor. */
1509 analyze_initial_condition (gphi
*loop_phi_node
)
1512 tree init_cond
= chrec_not_analyzed_yet
;
1513 class loop
*loop
= loop_containing_stmt (loop_phi_node
);
1515 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1517 fprintf (dump_file
, "(analyze_initial_condition \n");
1518 fprintf (dump_file
, " (loop_phi_node = \n");
1519 print_gimple_stmt (dump_file
, loop_phi_node
, 0);
1520 fprintf (dump_file
, ")\n");
1523 n
= gimple_phi_num_args (loop_phi_node
);
1524 for (i
= 0; i
< n
; i
++)
1526 tree branch
= PHI_ARG_DEF (loop_phi_node
, i
);
1527 basic_block bb
= gimple_phi_arg_edge (loop_phi_node
, i
)->src
;
1529 /* When the branch is oriented to the loop's body, it does
1530 not contribute to the initial condition. */
1531 if (flow_bb_inside_loop_p (loop
, bb
))
1534 if (init_cond
== chrec_not_analyzed_yet
)
1540 if (TREE_CODE (branch
) == SSA_NAME
)
1542 init_cond
= chrec_dont_know
;
1546 init_cond
= chrec_merge (init_cond
, branch
);
1549 /* Ooops -- a loop without an entry??? */
1550 if (init_cond
== chrec_not_analyzed_yet
)
1551 init_cond
= chrec_dont_know
;
1553 /* We may not have fully constant propagated IL. Handle degenerate PHIs here
1554 to not miss important early loop unrollings. */
1555 init_cond
= follow_copies_to_constant (init_cond
);
1557 if (dump_file
&& (dump_flags
& TDF_SCEV
))
1559 fprintf (dump_file
, " (init_cond = ");
1560 print_generic_expr (dump_file
, init_cond
);
1561 fprintf (dump_file
, "))\n");
1567 /* Analyze the scalar evolution for LOOP_PHI_NODE. */
1570 interpret_loop_phi (class loop
*loop
, gphi
*loop_phi_node
)
1572 class loop
*phi_loop
= loop_containing_stmt (loop_phi_node
);
1575 gcc_assert (phi_loop
== loop
);
1577 /* Otherwise really interpret the loop phi. */
1578 init_cond
= analyze_initial_condition (loop_phi_node
);
1579 return analyze_evolution_in_loop (loop_phi_node
, init_cond
);
1582 /* This function merges the branches of a condition-phi-node,
1583 contained in the outermost loop, and whose arguments are already
1587 interpret_condition_phi (class loop
*loop
, gphi
*condition_phi
)
1589 int i
, n
= gimple_phi_num_args (condition_phi
);
1590 tree res
= chrec_not_analyzed_yet
;
1592 for (i
= 0; i
< n
; i
++)
1596 if (backedge_phi_arg_p (condition_phi
, i
))
1598 res
= chrec_dont_know
;
1602 branch_chrec
= analyze_scalar_evolution
1603 (loop
, PHI_ARG_DEF (condition_phi
, i
));
1605 res
= chrec_merge (res
, branch_chrec
);
1606 if (res
== chrec_dont_know
)
1613 /* Interpret the operation RHS1 OP RHS2. If we didn't
1614 analyze this node before, follow the definitions until ending
1615 either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the
1616 return path, this function propagates evolutions (ala constant copy
1617 propagation). OPND1 is not a GIMPLE expression because we could
1618 analyze the effect of an inner loop: see interpret_loop_phi. */
1621 interpret_rhs_expr (class loop
*loop
, gimple
*at_stmt
,
1622 tree type
, tree rhs1
, enum tree_code code
, tree rhs2
)
1624 tree res
, chrec1
, chrec2
, ctype
;
1627 if (get_gimple_rhs_class (code
) == GIMPLE_SINGLE_RHS
)
1629 if (is_gimple_min_invariant (rhs1
))
1630 return chrec_convert (type
, rhs1
, at_stmt
);
1632 if (code
== SSA_NAME
)
1633 return chrec_convert (type
, analyze_scalar_evolution (loop
, rhs1
),
1640 if (TREE_CODE (TREE_OPERAND (rhs1
, 0)) == MEM_REF
1641 || handled_component_p (TREE_OPERAND (rhs1
, 0)))
1644 poly_int64 bitsize
, bitpos
;
1645 int unsignedp
, reversep
;
1651 base
= get_inner_reference (TREE_OPERAND (rhs1
, 0),
1652 &bitsize
, &bitpos
, &offset
, &mode
,
1653 &unsignedp
, &reversep
, &volatilep
);
1655 if (TREE_CODE (base
) == MEM_REF
)
1657 rhs2
= TREE_OPERAND (base
, 1);
1658 rhs1
= TREE_OPERAND (base
, 0);
1660 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1661 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1662 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1663 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1664 chrec1
= instantiate_parameters (loop
, chrec1
);
1665 chrec2
= instantiate_parameters (loop
, chrec2
);
1666 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1670 chrec1
= analyze_scalar_evolution_for_address_of (loop
, base
);
1671 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1675 if (offset
!= NULL_TREE
)
1677 chrec2
= analyze_scalar_evolution (loop
, offset
);
1678 chrec2
= chrec_convert (TREE_TYPE (offset
), chrec2
, at_stmt
);
1679 chrec2
= instantiate_parameters (loop
, chrec2
);
1680 res
= chrec_fold_plus (type
, res
, chrec2
);
1683 if (maybe_ne (bitpos
, 0))
1685 unitpos
= size_int (exact_div (bitpos
, BITS_PER_UNIT
));
1686 chrec3
= analyze_scalar_evolution (loop
, unitpos
);
1687 chrec3
= chrec_convert (TREE_TYPE (unitpos
), chrec3
, at_stmt
);
1688 chrec3
= instantiate_parameters (loop
, chrec3
);
1689 res
= chrec_fold_plus (type
, res
, chrec3
);
1693 res
= chrec_dont_know
;
1696 case POINTER_PLUS_EXPR
:
1697 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1698 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1699 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1700 chrec2
= chrec_convert (TREE_TYPE (rhs2
), chrec2
, at_stmt
);
1701 chrec1
= instantiate_parameters (loop
, chrec1
);
1702 chrec2
= instantiate_parameters (loop
, chrec2
);
1703 res
= chrec_fold_plus (type
, chrec1
, chrec2
);
1707 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1708 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1710 /* When the stmt is conditionally executed re-write the CHREC
1711 into a form that has well-defined behavior on overflow. */
1713 && INTEGRAL_TYPE_P (type
)
1714 && ! TYPE_OVERFLOW_WRAPS (type
)
1715 && ! dominated_by_p (CDI_DOMINATORS
, loop
->latch
,
1716 gimple_bb (at_stmt
)))
1717 ctype
= unsigned_type_for (type
);
1718 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1719 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1720 chrec1
= instantiate_parameters (loop
, chrec1
);
1721 chrec2
= instantiate_parameters (loop
, chrec2
);
1722 res
= chrec_fold_plus (ctype
, chrec1
, chrec2
);
1724 res
= chrec_convert (type
, res
, at_stmt
);
1728 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1729 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1731 /* When the stmt is conditionally executed re-write the CHREC
1732 into a form that has well-defined behavior on overflow. */
1734 && INTEGRAL_TYPE_P (type
)
1735 && ! TYPE_OVERFLOW_WRAPS (type
)
1736 && ! dominated_by_p (CDI_DOMINATORS
,
1737 loop
->latch
, gimple_bb (at_stmt
)))
1738 ctype
= unsigned_type_for (type
);
1739 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1740 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1741 chrec1
= instantiate_parameters (loop
, chrec1
);
1742 chrec2
= instantiate_parameters (loop
, chrec2
);
1743 res
= chrec_fold_minus (ctype
, chrec1
, chrec2
);
1745 res
= chrec_convert (type
, res
, at_stmt
);
1749 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1751 /* When the stmt is conditionally executed re-write the CHREC
1752 into a form that has well-defined behavior on overflow. */
1754 && INTEGRAL_TYPE_P (type
)
1755 && ! TYPE_OVERFLOW_WRAPS (type
)
1756 && ! dominated_by_p (CDI_DOMINATORS
,
1757 loop
->latch
, gimple_bb (at_stmt
)))
1758 ctype
= unsigned_type_for (type
);
1759 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1760 /* TYPE may be integer, real or complex, so use fold_convert. */
1761 chrec1
= instantiate_parameters (loop
, chrec1
);
1762 res
= chrec_fold_multiply (ctype
, chrec1
,
1763 fold_convert (ctype
, integer_minus_one_node
));
1765 res
= chrec_convert (type
, res
, at_stmt
);
1769 /* Handle ~X as -1 - X. */
1770 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1771 chrec1
= chrec_convert (type
, chrec1
, at_stmt
);
1772 chrec1
= instantiate_parameters (loop
, chrec1
);
1773 res
= chrec_fold_minus (type
,
1774 fold_convert (type
, integer_minus_one_node
),
1779 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1780 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1782 /* When the stmt is conditionally executed re-write the CHREC
1783 into a form that has well-defined behavior on overflow. */
1785 && INTEGRAL_TYPE_P (type
)
1786 && ! TYPE_OVERFLOW_WRAPS (type
)
1787 && ! dominated_by_p (CDI_DOMINATORS
,
1788 loop
->latch
, gimple_bb (at_stmt
)))
1789 ctype
= unsigned_type_for (type
);
1790 chrec1
= chrec_convert (ctype
, chrec1
, at_stmt
);
1791 chrec2
= chrec_convert (ctype
, chrec2
, at_stmt
);
1792 chrec1
= instantiate_parameters (loop
, chrec1
);
1793 chrec2
= instantiate_parameters (loop
, chrec2
);
1794 res
= chrec_fold_multiply (ctype
, chrec1
, chrec2
);
1796 res
= chrec_convert (type
, res
, at_stmt
);
1801 /* Handle A<<B as A * (1<<B). */
1802 tree uns
= unsigned_type_for (type
);
1803 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1804 chrec2
= analyze_scalar_evolution (loop
, rhs2
);
1805 chrec1
= chrec_convert (uns
, chrec1
, at_stmt
);
1806 chrec1
= instantiate_parameters (loop
, chrec1
);
1807 chrec2
= instantiate_parameters (loop
, chrec2
);
1809 tree one
= build_int_cst (uns
, 1);
1810 chrec2
= fold_build2 (LSHIFT_EXPR
, uns
, one
, chrec2
);
1811 res
= chrec_fold_multiply (uns
, chrec1
, chrec2
);
1812 res
= chrec_convert (type
, res
, at_stmt
);
1817 /* In case we have a truncation of a widened operation that in
1818 the truncated type has undefined overflow behavior analyze
1819 the operation done in an unsigned type of the same precision
1820 as the final truncation. We cannot derive a scalar evolution
1821 for the widened operation but for the truncated result. */
1822 if (TREE_CODE (type
) == INTEGER_TYPE
1823 && TREE_CODE (TREE_TYPE (rhs1
)) == INTEGER_TYPE
1824 && TYPE_PRECISION (type
) < TYPE_PRECISION (TREE_TYPE (rhs1
))
1825 && TYPE_OVERFLOW_UNDEFINED (type
)
1826 && TREE_CODE (rhs1
) == SSA_NAME
1827 && (def
= SSA_NAME_DEF_STMT (rhs1
))
1828 && is_gimple_assign (def
)
1829 && TREE_CODE_CLASS (gimple_assign_rhs_code (def
)) == tcc_binary
1830 && TREE_CODE (gimple_assign_rhs2 (def
)) == INTEGER_CST
)
1832 tree utype
= unsigned_type_for (type
);
1833 chrec1
= interpret_rhs_expr (loop
, at_stmt
, utype
,
1834 gimple_assign_rhs1 (def
),
1835 gimple_assign_rhs_code (def
),
1836 gimple_assign_rhs2 (def
));
1839 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1840 res
= chrec_convert (type
, chrec1
, at_stmt
, true, rhs1
);
1844 /* Given int variable A, handle A&0xffff as (int)(unsigned short)A.
1845 If A is SCEV and its value is in the range of representable set
1846 of type unsigned short, the result expression is a (no-overflow)
1848 res
= chrec_dont_know
;
1849 if (tree_fits_uhwi_p (rhs2
))
1852 unsigned HOST_WIDE_INT val
= tree_to_uhwi (rhs2
);
1855 /* Skip if value of rhs2 wraps in unsigned HOST_WIDE_INT or
1856 it's not the maximum value of a smaller type than rhs1. */
1858 && (precision
= exact_log2 (val
)) > 0
1859 && (unsigned) precision
< TYPE_PRECISION (TREE_TYPE (rhs1
)))
1861 tree utype
= build_nonstandard_integer_type (precision
, 1);
1863 if (TYPE_PRECISION (utype
) < TYPE_PRECISION (TREE_TYPE (rhs1
)))
1865 chrec1
= analyze_scalar_evolution (loop
, rhs1
);
1866 chrec1
= chrec_convert (utype
, chrec1
, at_stmt
);
1867 res
= chrec_convert (TREE_TYPE (rhs1
), chrec1
, at_stmt
);
1874 res
= chrec_dont_know
;
1881 /* Interpret the expression EXPR. */
1884 interpret_expr (class loop
*loop
, gimple
*at_stmt
, tree expr
)
1886 enum tree_code code
;
1887 tree type
= TREE_TYPE (expr
), op0
, op1
;
1889 if (automatically_generated_chrec_p (expr
))
1892 if (TREE_CODE (expr
) == POLYNOMIAL_CHREC
1893 || TREE_CODE (expr
) == CALL_EXPR
1894 || get_gimple_rhs_class (TREE_CODE (expr
)) == GIMPLE_TERNARY_RHS
)
1895 return chrec_dont_know
;
1897 extract_ops_from_tree (expr
, &code
, &op0
, &op1
);
1899 return interpret_rhs_expr (loop
, at_stmt
, type
,
1903 /* Interpret the rhs of the assignment STMT. */
1906 interpret_gimple_assign (class loop
*loop
, gimple
*stmt
)
1908 tree type
= TREE_TYPE (gimple_assign_lhs (stmt
));
1909 enum tree_code code
= gimple_assign_rhs_code (stmt
);
1911 return interpret_rhs_expr (loop
, stmt
, type
,
1912 gimple_assign_rhs1 (stmt
), code
,
1913 gimple_assign_rhs2 (stmt
));
1918 /* This section contains all the entry points:
1919 - number_of_iterations_in_loop,
1920 - analyze_scalar_evolution,
1921 - instantiate_parameters.
1924 /* Helper recursive function. */
1927 analyze_scalar_evolution_1 (class loop
*loop
, tree var
)
1931 class loop
*def_loop
;
1934 if (TREE_CODE (var
) != SSA_NAME
)
1935 return interpret_expr (loop
, NULL
, var
);
1937 def
= SSA_NAME_DEF_STMT (var
);
1938 bb
= gimple_bb (def
);
1939 def_loop
= bb
->loop_father
;
1941 if (!flow_bb_inside_loop_p (loop
, bb
))
1943 /* Keep symbolic form, but look through obvious copies for constants. */
1944 res
= follow_copies_to_constant (var
);
1948 if (loop
!= def_loop
)
1950 res
= analyze_scalar_evolution_1 (def_loop
, var
);
1951 class loop
*loop_to_skip
= superloop_at_depth (def_loop
,
1952 loop_depth (loop
) + 1);
1953 res
= compute_overall_effect_of_inner_loop (loop_to_skip
, res
);
1954 if (chrec_contains_symbols_defined_in_loop (res
, loop
->num
))
1955 res
= analyze_scalar_evolution_1 (loop
, res
);
1959 switch (gimple_code (def
))
1962 res
= interpret_gimple_assign (loop
, def
);
1966 if (loop_phi_node_p (def
))
1967 res
= interpret_loop_phi (loop
, as_a
<gphi
*> (def
));
1969 res
= interpret_condition_phi (loop
, as_a
<gphi
*> (def
));
1973 res
= chrec_dont_know
;
1979 /* Keep the symbolic form. */
1980 if (res
== chrec_dont_know
)
1983 if (loop
== def_loop
)
1984 set_scalar_evolution (block_before_loop (loop
), var
, res
);
1989 /* Analyzes and returns the scalar evolution of the ssa_name VAR in
1990 LOOP. LOOP is the loop in which the variable is used.
1992 Example of use: having a pointer VAR to a SSA_NAME node, STMT a
1993 pointer to the statement that uses this variable, in order to
1994 determine the evolution function of the variable, use the following
1997 loop_p loop = loop_containing_stmt (stmt);
1998 tree chrec_with_symbols = analyze_scalar_evolution (loop, var);
1999 tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols);
2003 analyze_scalar_evolution (class loop
*loop
, tree var
)
2007 /* ??? Fix callers. */
2011 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2013 fprintf (dump_file
, "(analyze_scalar_evolution \n");
2014 fprintf (dump_file
, " (loop_nb = %d)\n", loop
->num
);
2015 fprintf (dump_file
, " (scalar = ");
2016 print_generic_expr (dump_file
, var
);
2017 fprintf (dump_file
, ")\n");
2020 res
= get_scalar_evolution (block_before_loop (loop
), var
);
2021 if (res
== chrec_not_analyzed_yet
)
2023 /* We'll recurse into instantiate_scev, avoid tearing down the
2024 instantiate cache repeatedly and keep it live from here. */
2028 global_cache
= new instantiate_cache_type
;
2031 res
= analyze_scalar_evolution_1 (loop
, var
);
2034 delete global_cache
;
2035 global_cache
= NULL
;
2039 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2040 fprintf (dump_file
, ")\n");
2045 /* Analyzes and returns the scalar evolution of VAR address in LOOP. */
2048 analyze_scalar_evolution_for_address_of (class loop
*loop
, tree var
)
2050 return analyze_scalar_evolution (loop
, build_fold_addr_expr (var
));
2053 /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to
2054 WRTO_LOOP (which should be a superloop of USE_LOOP)
2056 FOLDED_CASTS is set to true if resolve_mixers used
2057 chrec_convert_aggressive (TODO -- not really, we are way too conservative
2058 at the moment in order to keep things simple).
2060 To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following
2063 for (i = 0; i < 100; i++) -- loop 1
2065 for (j = 0; j < 100; j++) -- loop 2
2072 for (t = 0; t < 100; t++) -- loop 3
2079 Both k1 and k2 are invariants in loop3, thus
2080 analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1
2081 analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2
2083 As they are invariant, it does not matter whether we consider their
2084 usage in loop 3 or loop 2, hence
2085 analyze_scalar_evolution_in_loop (loop2, loop3, k1) =
2086 analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i
2087 analyze_scalar_evolution_in_loop (loop2, loop3, k2) =
2088 analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2
2090 Similarly for their evolutions with respect to loop 1. The values of K2
2091 in the use in loop 2 vary independently on loop 1, thus we cannot express
2092 the evolution with respect to loop 1:
2093 analyze_scalar_evolution_in_loop (loop1, loop3, k1) =
2094 analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1
2095 analyze_scalar_evolution_in_loop (loop1, loop3, k2) =
2096 analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know
2098 The value of k2 in the use in loop 1 is known, though:
2099 analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1
2100 analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100
2104 analyze_scalar_evolution_in_loop (class loop
*wrto_loop
, class loop
*use_loop
,
2105 tree version
, bool *folded_casts
)
2108 tree ev
= version
, tmp
;
2110 /* We cannot just do
2112 tmp = analyze_scalar_evolution (use_loop, version);
2113 ev = resolve_mixers (wrto_loop, tmp, folded_casts);
2115 as resolve_mixers would query the scalar evolution with respect to
2116 wrto_loop. For example, in the situation described in the function
2117 comment, suppose that wrto_loop = loop1, use_loop = loop3 and
2120 analyze_scalar_evolution (use_loop, version) = k2
2122 and resolve_mixers (loop1, k2, folded_casts) finds that the value of
2123 k2 in loop 1 is 100, which is a wrong result, since we are interested
2124 in the value in loop 3.
2126 Instead, we need to proceed from use_loop to wrto_loop loop by loop,
2127 each time checking that there is no evolution in the inner loop. */
2130 *folded_casts
= false;
2133 tmp
= analyze_scalar_evolution (use_loop
, ev
);
2134 ev
= resolve_mixers (use_loop
, tmp
, folded_casts
);
2136 if (use_loop
== wrto_loop
)
2139 /* If the value of the use changes in the inner loop, we cannot express
2140 its value in the outer loop (we might try to return interval chrec,
2141 but we do not have a user for it anyway) */
2142 if (!no_evolution_in_loop_p (ev
, use_loop
->num
, &val
)
2144 return chrec_dont_know
;
2146 use_loop
= loop_outer (use_loop
);
2151 /* Computes a hash function for database element ELT. */
2153 static inline hashval_t
2154 hash_idx_scev_info (const void *elt_
)
2156 unsigned idx
= ((size_t) elt_
) - 2;
2157 return scev_info_hasher::hash (&global_cache
->entries
[idx
]);
2160 /* Compares database elements E1 and E2. */
2163 eq_idx_scev_info (const void *e1
, const void *e2
)
2165 unsigned idx1
= ((size_t) e1
) - 2;
2166 return scev_info_hasher::equal (&global_cache
->entries
[idx1
],
2167 (const scev_info_str
*) e2
);
2170 /* Returns from CACHE the slot number of the cached chrec for NAME. */
2173 get_instantiated_value_entry (instantiate_cache_type
&cache
,
2174 tree name
, edge instantiate_below
)
2178 cache
.map
= htab_create (10, hash_idx_scev_info
, eq_idx_scev_info
, NULL
);
2179 cache
.entries
.create (10);
2183 e
.name_version
= SSA_NAME_VERSION (name
);
2184 e
.instantiated_below
= instantiate_below
->dest
->index
;
2185 void **slot
= htab_find_slot_with_hash (cache
.map
, &e
,
2186 scev_info_hasher::hash (&e
), INSERT
);
2189 e
.chrec
= chrec_not_analyzed_yet
;
2190 *slot
= (void *)(size_t)(cache
.entries
.length () + 2);
2191 cache
.entries
.safe_push (e
);
2194 return ((size_t)*slot
) - 2;
2198 /* Return the closed_loop_phi node for VAR. If there is none, return
2202 loop_closed_phi_def (tree var
)
2209 if (var
== NULL_TREE
2210 || TREE_CODE (var
) != SSA_NAME
)
2213 loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (var
));
2214 exit
= single_exit (loop
);
2218 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); gsi_next (&psi
))
2221 if (PHI_ARG_DEF_FROM_EDGE (phi
, exit
) == var
)
2222 return PHI_RESULT (phi
);
2228 static tree
instantiate_scev_r (edge
, class loop
*, class loop
*,
2231 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2232 and EVOLUTION_LOOP, that were left under a symbolic form.
2234 CHREC is an SSA_NAME to be instantiated.
2236 CACHE is the cache of already instantiated values.
2238 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2239 conversions that may wrap in signed/pointer type are folded, as long
2240 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2241 then we don't do such fold.
2243 SIZE_EXPR is used for computing the size of the expression to be
2244 instantiated, and to stop if it exceeds some limit. */
2247 instantiate_scev_name (edge instantiate_below
,
2248 class loop
*evolution_loop
, class loop
*inner_loop
,
2250 bool *fold_conversions
,
2254 class loop
*def_loop
;
2255 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (chrec
));
2257 /* A parameter, nothing to do. */
2259 || !dominated_by_p (CDI_DOMINATORS
, def_bb
, instantiate_below
->dest
))
2262 /* We cache the value of instantiated variable to avoid exponential
2263 time complexity due to reevaluations. We also store the convenient
2264 value in the cache in order to prevent infinite recursion -- we do
2265 not want to instantiate the SSA_NAME if it is in a mixer
2266 structure. This is used for avoiding the instantiation of
2267 recursively defined functions, such as:
2269 | a_2 -> {0, +, 1, +, a_2}_1 */
2271 unsigned si
= get_instantiated_value_entry (*global_cache
,
2272 chrec
, instantiate_below
);
2273 if (global_cache
->get (si
) != chrec_not_analyzed_yet
)
2274 return global_cache
->get (si
);
2276 /* On recursion return chrec_dont_know. */
2277 global_cache
->set (si
, chrec_dont_know
);
2279 def_loop
= find_common_loop (evolution_loop
, def_bb
->loop_father
);
2281 if (! dominated_by_p (CDI_DOMINATORS
,
2282 def_loop
->header
, instantiate_below
->dest
))
2284 gimple
*def
= SSA_NAME_DEF_STMT (chrec
);
2285 if (gassign
*ass
= dyn_cast
<gassign
*> (def
))
2287 switch (gimple_assign_rhs_class (ass
))
2289 case GIMPLE_UNARY_RHS
:
2291 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2292 inner_loop
, gimple_assign_rhs1 (ass
),
2293 fold_conversions
, size_expr
);
2294 if (op0
== chrec_dont_know
)
2295 return chrec_dont_know
;
2296 res
= fold_build1 (gimple_assign_rhs_code (ass
),
2297 TREE_TYPE (chrec
), op0
);
2300 case GIMPLE_BINARY_RHS
:
2302 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2303 inner_loop
, gimple_assign_rhs1 (ass
),
2304 fold_conversions
, size_expr
);
2305 if (op0
== chrec_dont_know
)
2306 return chrec_dont_know
;
2307 tree op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2308 inner_loop
, gimple_assign_rhs2 (ass
),
2309 fold_conversions
, size_expr
);
2310 if (op1
== chrec_dont_know
)
2311 return chrec_dont_know
;
2312 res
= fold_build2 (gimple_assign_rhs_code (ass
),
2313 TREE_TYPE (chrec
), op0
, op1
);
2317 res
= chrec_dont_know
;
2321 res
= chrec_dont_know
;
2322 global_cache
->set (si
, res
);
2326 /* If the analysis yields a parametric chrec, instantiate the
2328 res
= analyze_scalar_evolution (def_loop
, chrec
);
2330 /* Don't instantiate default definitions. */
2331 if (TREE_CODE (res
) == SSA_NAME
2332 && SSA_NAME_IS_DEFAULT_DEF (res
))
2335 /* Don't instantiate loop-closed-ssa phi nodes. */
2336 else if (TREE_CODE (res
) == SSA_NAME
2337 && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res
)))
2338 > loop_depth (def_loop
))
2341 res
= loop_closed_phi_def (chrec
);
2345 /* When there is no loop_closed_phi_def, it means that the
2346 variable is not used after the loop: try to still compute the
2347 value of the variable when exiting the loop. */
2348 if (res
== NULL_TREE
)
2350 loop_p loop
= loop_containing_stmt (SSA_NAME_DEF_STMT (chrec
));
2351 res
= analyze_scalar_evolution (loop
, chrec
);
2352 res
= compute_overall_effect_of_inner_loop (loop
, res
);
2353 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2355 fold_conversions
, size_expr
);
2357 else if (dominated_by_p (CDI_DOMINATORS
,
2358 gimple_bb (SSA_NAME_DEF_STMT (res
)),
2359 instantiate_below
->dest
))
2360 res
= chrec_dont_know
;
2363 else if (res
!= chrec_dont_know
)
2366 && def_bb
->loop_father
!= inner_loop
2367 && !flow_loop_nested_p (def_bb
->loop_father
, inner_loop
))
2368 /* ??? We could try to compute the overall effect of the loop here. */
2369 res
= chrec_dont_know
;
2371 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2373 fold_conversions
, size_expr
);
2376 /* Store the correct value to the cache. */
2377 global_cache
->set (si
, res
);
2381 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2382 and EVOLUTION_LOOP, that were left under a symbolic form.
2384 CHREC is a polynomial chain of recurrence to be instantiated.
2386 CACHE is the cache of already instantiated values.
2388 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2389 conversions that may wrap in signed/pointer type are folded, as long
2390 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2391 then we don't do such fold.
2393 SIZE_EXPR is used for computing the size of the expression to be
2394 instantiated, and to stop if it exceeds some limit. */
2397 instantiate_scev_poly (edge instantiate_below
,
2398 class loop
*evolution_loop
, class loop
*,
2399 tree chrec
, bool *fold_conversions
, int size_expr
)
2402 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2403 get_chrec_loop (chrec
),
2404 CHREC_LEFT (chrec
), fold_conversions
,
2406 if (op0
== chrec_dont_know
)
2407 return chrec_dont_know
;
2409 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2410 get_chrec_loop (chrec
),
2411 CHREC_RIGHT (chrec
), fold_conversions
,
2413 if (op1
== chrec_dont_know
)
2414 return chrec_dont_know
;
2416 if (CHREC_LEFT (chrec
) != op0
2417 || CHREC_RIGHT (chrec
) != op1
)
2419 op1
= chrec_convert_rhs (chrec_type (op0
), op1
, NULL
);
2420 chrec
= build_polynomial_chrec (CHREC_VARIABLE (chrec
), op0
, op1
);
2426 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2427 and EVOLUTION_LOOP, that were left under a symbolic form.
2429 "C0 CODE C1" is a binary expression of type TYPE to be instantiated.
2431 CACHE is the cache of already instantiated values.
2433 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2434 conversions that may wrap in signed/pointer type are folded, as long
2435 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2436 then we don't do such fold.
2438 SIZE_EXPR is used for computing the size of the expression to be
2439 instantiated, and to stop if it exceeds some limit. */
2442 instantiate_scev_binary (edge instantiate_below
,
2443 class loop
*evolution_loop
, class loop
*inner_loop
,
2444 tree chrec
, enum tree_code code
,
2445 tree type
, tree c0
, tree c1
,
2446 bool *fold_conversions
, int size_expr
)
2449 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2450 c0
, fold_conversions
, size_expr
);
2451 if (op0
== chrec_dont_know
)
2452 return chrec_dont_know
;
2454 /* While we eventually compute the same op1 if c0 == c1 the process
2455 of doing this is expensive so the following short-cut prevents
2456 exponential compile-time behavior. */
2459 op1
= instantiate_scev_r (instantiate_below
, evolution_loop
, inner_loop
,
2460 c1
, fold_conversions
, size_expr
);
2461 if (op1
== chrec_dont_know
)
2462 return chrec_dont_know
;
2470 op0
= chrec_convert (type
, op0
, NULL
);
2471 op1
= chrec_convert_rhs (type
, op1
, NULL
);
2475 case POINTER_PLUS_EXPR
:
2477 return chrec_fold_plus (type
, op0
, op1
);
2480 return chrec_fold_minus (type
, op0
, op1
);
2483 return chrec_fold_multiply (type
, op0
, op1
);
2490 return chrec
? chrec
: fold_build2 (code
, type
, c0
, c1
);
2493 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2494 and EVOLUTION_LOOP, that were left under a symbolic form.
2496 "CHREC" that stands for a convert expression "(TYPE) OP" is to be
2499 CACHE is the cache of already instantiated values.
2501 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2502 conversions that may wrap in signed/pointer type are folded, as long
2503 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2504 then we don't do such fold.
2506 SIZE_EXPR is used for computing the size of the expression to be
2507 instantiated, and to stop if it exceeds some limit. */
2510 instantiate_scev_convert (edge instantiate_below
,
2511 class loop
*evolution_loop
, class loop
*inner_loop
,
2512 tree chrec
, tree type
, tree op
,
2513 bool *fold_conversions
, int size_expr
)
2515 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2517 fold_conversions
, size_expr
);
2519 if (op0
== chrec_dont_know
)
2520 return chrec_dont_know
;
2522 if (fold_conversions
)
2524 tree tmp
= chrec_convert_aggressive (type
, op0
, fold_conversions
);
2528 /* If we used chrec_convert_aggressive, we can no longer assume that
2529 signed chrecs do not overflow, as chrec_convert does, so avoid
2530 calling it in that case. */
2531 if (*fold_conversions
)
2533 if (chrec
&& op0
== op
)
2536 return fold_convert (type
, op0
);
2540 return chrec_convert (type
, op0
, NULL
);
2543 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2544 and EVOLUTION_LOOP, that were left under a symbolic form.
2546 CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated.
2547 Handle ~X as -1 - X.
2548 Handle -X as -1 * X.
2550 CACHE is the cache of already instantiated values.
2552 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2553 conversions that may wrap in signed/pointer type are folded, as long
2554 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2555 then we don't do such fold.
2557 SIZE_EXPR is used for computing the size of the expression to be
2558 instantiated, and to stop if it exceeds some limit. */
2561 instantiate_scev_not (edge instantiate_below
,
2562 class loop
*evolution_loop
, class loop
*inner_loop
,
2564 enum tree_code code
, tree type
, tree op
,
2565 bool *fold_conversions
, int size_expr
)
2567 tree op0
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2569 fold_conversions
, size_expr
);
2571 if (op0
== chrec_dont_know
)
2572 return chrec_dont_know
;
2576 op0
= chrec_convert (type
, op0
, NULL
);
2581 return chrec_fold_minus
2582 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2585 return chrec_fold_multiply
2586 (type
, fold_convert (type
, integer_minus_one_node
), op0
);
2593 return chrec
? chrec
: fold_build1 (code
, type
, op0
);
2596 /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW
2597 and EVOLUTION_LOOP, that were left under a symbolic form.
2599 CHREC is the scalar evolution to instantiate.
2601 CACHE is the cache of already instantiated values.
2603 Variable pointed by FOLD_CONVERSIONS is set to TRUE when the
2604 conversions that may wrap in signed/pointer type are folded, as long
2605 as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL
2606 then we don't do such fold.
2608 SIZE_EXPR is used for computing the size of the expression to be
2609 instantiated, and to stop if it exceeds some limit. */
2612 instantiate_scev_r (edge instantiate_below
,
2613 class loop
*evolution_loop
, class loop
*inner_loop
,
2615 bool *fold_conversions
, int size_expr
)
2617 /* Give up if the expression is larger than the MAX that we allow. */
2618 if (size_expr
++ > param_scev_max_expr_size
)
2619 return chrec_dont_know
;
2621 if (chrec
== NULL_TREE
2622 || automatically_generated_chrec_p (chrec
)
2623 || is_gimple_min_invariant (chrec
))
2626 switch (TREE_CODE (chrec
))
2629 return instantiate_scev_name (instantiate_below
, evolution_loop
,
2631 fold_conversions
, size_expr
);
2633 case POLYNOMIAL_CHREC
:
2634 return instantiate_scev_poly (instantiate_below
, evolution_loop
,
2636 fold_conversions
, size_expr
);
2638 case POINTER_PLUS_EXPR
:
2642 return instantiate_scev_binary (instantiate_below
, evolution_loop
,
2644 TREE_CODE (chrec
), chrec_type (chrec
),
2645 TREE_OPERAND (chrec
, 0),
2646 TREE_OPERAND (chrec
, 1),
2647 fold_conversions
, size_expr
);
2650 return instantiate_scev_convert (instantiate_below
, evolution_loop
,
2652 TREE_TYPE (chrec
), TREE_OPERAND (chrec
, 0),
2653 fold_conversions
, size_expr
);
2657 return instantiate_scev_not (instantiate_below
, evolution_loop
,
2659 TREE_CODE (chrec
), TREE_TYPE (chrec
),
2660 TREE_OPERAND (chrec
, 0),
2661 fold_conversions
, size_expr
);
2664 if (is_gimple_min_invariant (chrec
))
2667 case SCEV_NOT_KNOWN
:
2668 return chrec_dont_know
;
2674 if (CONSTANT_CLASS_P (chrec
))
2676 return chrec_dont_know
;
2680 /* Analyze all the parameters of the chrec that were left under a
2681 symbolic form. INSTANTIATE_BELOW is the basic block that stops the
2682 recursive instantiation of parameters: a parameter is a variable
2683 that is defined in a basic block that dominates INSTANTIATE_BELOW or
2684 a function parameter. */
2687 instantiate_scev (edge instantiate_below
, class loop
*evolution_loop
,
2692 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2694 fprintf (dump_file
, "(instantiate_scev \n");
2695 fprintf (dump_file
, " (instantiate_below = %d -> %d)\n",
2696 instantiate_below
->src
->index
, instantiate_below
->dest
->index
);
2698 fprintf (dump_file
, " (evolution_loop = %d)\n", evolution_loop
->num
);
2699 fprintf (dump_file
, " (chrec = ");
2700 print_generic_expr (dump_file
, chrec
);
2701 fprintf (dump_file
, ")\n");
2707 global_cache
= new instantiate_cache_type
;
2711 res
= instantiate_scev_r (instantiate_below
, evolution_loop
,
2712 NULL
, chrec
, NULL
, 0);
2716 delete global_cache
;
2717 global_cache
= NULL
;
2720 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2722 fprintf (dump_file
, " (res = ");
2723 print_generic_expr (dump_file
, res
);
2724 fprintf (dump_file
, "))\n");
2730 /* Similar to instantiate_parameters, but does not introduce the
2731 evolutions in outer loops for LOOP invariants in CHREC, and does not
2732 care about causing overflows, as long as they do not affect value
2733 of an expression. */
2736 resolve_mixers (class loop
*loop
, tree chrec
, bool *folded_casts
)
2739 bool fold_conversions
= false;
2742 global_cache
= new instantiate_cache_type
;
2746 tree ret
= instantiate_scev_r (loop_preheader_edge (loop
), loop
, NULL
,
2747 chrec
, &fold_conversions
, 0);
2749 if (folded_casts
&& !*folded_casts
)
2750 *folded_casts
= fold_conversions
;
2754 delete global_cache
;
2755 global_cache
= NULL
;
2761 /* Entry point for the analysis of the number of iterations pass.
2762 This function tries to safely approximate the number of iterations
2763 the loop will run. When this property is not decidable at compile
2764 time, the result is chrec_dont_know. Otherwise the result is a
2765 scalar or a symbolic parameter. When the number of iterations may
2766 be equal to zero and the property cannot be determined at compile
2767 time, the result is a COND_EXPR that represents in a symbolic form
2768 the conditions under which the number of iterations is not zero.
2770 Example of analysis: suppose that the loop has an exit condition:
2772 "if (b > 49) goto end_loop;"
2774 and that in a previous analysis we have determined that the
2775 variable 'b' has an evolution function:
2777 "EF = {23, +, 5}_2".
2779 When we evaluate the function at the point 5, i.e. the value of the
2780 variable 'b' after 5 iterations in the loop, we have EF (5) = 48,
2781 and EF (6) = 53. In this case the value of 'b' on exit is '53' and
2782 the loop body has been executed 6 times. */
2785 number_of_latch_executions (class loop
*loop
)
2788 class tree_niter_desc niter_desc
;
2792 /* Determine whether the number of iterations in loop has already
2794 res
= loop
->nb_iterations
;
2798 may_be_zero
= NULL_TREE
;
2800 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2801 fprintf (dump_file
, "(number_of_iterations_in_loop = \n");
2803 res
= chrec_dont_know
;
2804 exit
= single_exit (loop
);
2806 if (exit
&& number_of_iterations_exit (loop
, exit
, &niter_desc
, false))
2808 may_be_zero
= niter_desc
.may_be_zero
;
2809 res
= niter_desc
.niter
;
2812 if (res
== chrec_dont_know
2814 || integer_zerop (may_be_zero
))
2816 else if (integer_nonzerop (may_be_zero
))
2817 res
= build_int_cst (TREE_TYPE (res
), 0);
2819 else if (COMPARISON_CLASS_P (may_be_zero
))
2820 res
= fold_build3 (COND_EXPR
, TREE_TYPE (res
), may_be_zero
,
2821 build_int_cst (TREE_TYPE (res
), 0), res
);
2823 res
= chrec_dont_know
;
2825 if (dump_file
&& (dump_flags
& TDF_SCEV
))
2827 fprintf (dump_file
, " (set_nb_iterations_in_loop = ");
2828 print_generic_expr (dump_file
, res
);
2829 fprintf (dump_file
, "))\n");
2832 loop
->nb_iterations
= res
;
2837 /* Counters for the stats. */
2843 unsigned nb_affine_multivar
;
2844 unsigned nb_higher_poly
;
2845 unsigned nb_chrec_dont_know
;
2846 unsigned nb_undetermined
;
2849 /* Reset the counters. */
2852 reset_chrecs_counters (struct chrec_stats
*stats
)
2854 stats
->nb_chrecs
= 0;
2855 stats
->nb_affine
= 0;
2856 stats
->nb_affine_multivar
= 0;
2857 stats
->nb_higher_poly
= 0;
2858 stats
->nb_chrec_dont_know
= 0;
2859 stats
->nb_undetermined
= 0;
2862 /* Dump the contents of a CHREC_STATS structure. */
2865 dump_chrecs_stats (FILE *file
, struct chrec_stats
*stats
)
2867 fprintf (file
, "\n(\n");
2868 fprintf (file
, "-----------------------------------------\n");
2869 fprintf (file
, "%d\taffine univariate chrecs\n", stats
->nb_affine
);
2870 fprintf (file
, "%d\taffine multivariate chrecs\n", stats
->nb_affine_multivar
);
2871 fprintf (file
, "%d\tdegree greater than 2 polynomials\n",
2872 stats
->nb_higher_poly
);
2873 fprintf (file
, "%d\tchrec_dont_know chrecs\n", stats
->nb_chrec_dont_know
);
2874 fprintf (file
, "-----------------------------------------\n");
2875 fprintf (file
, "%d\ttotal chrecs\n", stats
->nb_chrecs
);
2876 fprintf (file
, "%d\twith undetermined coefficients\n",
2877 stats
->nb_undetermined
);
2878 fprintf (file
, "-----------------------------------------\n");
2879 fprintf (file
, "%d\tchrecs in the scev database\n",
2880 (int) scalar_evolution_info
->elements ());
2881 fprintf (file
, "%d\tsets in the scev database\n", nb_set_scev
);
2882 fprintf (file
, "%d\tgets in the scev database\n", nb_get_scev
);
2883 fprintf (file
, "-----------------------------------------\n");
2884 fprintf (file
, ")\n\n");
2887 /* Gather statistics about CHREC. */
2890 gather_chrec_stats (tree chrec
, struct chrec_stats
*stats
)
2892 if (dump_file
&& (dump_flags
& TDF_STATS
))
2894 fprintf (dump_file
, "(classify_chrec ");
2895 print_generic_expr (dump_file
, chrec
);
2896 fprintf (dump_file
, "\n");
2901 if (chrec
== NULL_TREE
)
2903 stats
->nb_undetermined
++;
2907 switch (TREE_CODE (chrec
))
2909 case POLYNOMIAL_CHREC
:
2910 if (evolution_function_is_affine_p (chrec
))
2912 if (dump_file
&& (dump_flags
& TDF_STATS
))
2913 fprintf (dump_file
, " affine_univariate\n");
2916 else if (evolution_function_is_affine_multivariate_p (chrec
, 0))
2918 if (dump_file
&& (dump_flags
& TDF_STATS
))
2919 fprintf (dump_file
, " affine_multivariate\n");
2920 stats
->nb_affine_multivar
++;
2924 if (dump_file
&& (dump_flags
& TDF_STATS
))
2925 fprintf (dump_file
, " higher_degree_polynomial\n");
2926 stats
->nb_higher_poly
++;
2935 if (chrec_contains_undetermined (chrec
))
2937 if (dump_file
&& (dump_flags
& TDF_STATS
))
2938 fprintf (dump_file
, " undetermined\n");
2939 stats
->nb_undetermined
++;
2942 if (dump_file
&& (dump_flags
& TDF_STATS
))
2943 fprintf (dump_file
, ")\n");
2946 /* Classify the chrecs of the whole database. */
2949 gather_stats_on_scev_database (void)
2951 struct chrec_stats stats
;
2956 reset_chrecs_counters (&stats
);
2958 hash_table
<scev_info_hasher
>::iterator iter
;
2960 FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info
, elt
, scev_info_str
*,
2962 gather_chrec_stats (elt
->chrec
, &stats
);
2964 dump_chrecs_stats (dump_file
, &stats
);
2968 /* Initialize the analysis of scalar evolutions for LOOPS. */
2971 scev_initialize (void)
2973 gcc_assert (! scev_initialized_p ()
2974 && loops_state_satisfies_p (cfun
, LOOPS_NORMAL
));
2976 scalar_evolution_info
= hash_table
<scev_info_hasher
>::create_ggc (100);
2978 for (auto loop
: loops_list (cfun
, 0))
2979 loop
->nb_iterations
= NULL_TREE
;
2982 /* Return true if SCEV is initialized. */
2985 scev_initialized_p (void)
2987 return scalar_evolution_info
!= NULL
;
2990 /* Cleans up the information cached by the scalar evolutions analysis
2991 in the hash table. */
2994 scev_reset_htab (void)
2996 if (!scalar_evolution_info
)
2999 scalar_evolution_info
->empty ();
3002 /* Cleans up the information cached by the scalar evolutions analysis
3003 in the hash table and in the loop->nb_iterations. */
3010 for (auto loop
: loops_list (cfun
, 0))
3011 loop
->nb_iterations
= NULL_TREE
;
3014 /* Return true if the IV calculation in TYPE can overflow based on the knowledge
3015 of the upper bound on the number of iterations of LOOP, the BASE and STEP
3018 We do not use information whether TYPE can overflow so it is safe to
3019 use this test even for derived IVs not computed every iteration or
3020 hypotetical IVs to be inserted into code. */
3023 iv_can_overflow_p (class loop
*loop
, tree type
, tree base
, tree step
)
3026 wide_int base_min
, base_max
, step_min
, step_max
, type_min
, type_max
;
3027 signop sgn
= TYPE_SIGN (type
);
3030 if (integer_zerop (step
))
3033 if (!INTEGRAL_TYPE_P (TREE_TYPE (base
))
3034 || !get_range_query (cfun
)->range_of_expr (r
, base
)
3036 || r
.undefined_p ())
3039 base_min
= r
.lower_bound ();
3040 base_max
= r
.upper_bound ();
3042 if (!INTEGRAL_TYPE_P (TREE_TYPE (step
))
3043 || !get_range_query (cfun
)->range_of_expr (r
, step
)
3045 || r
.undefined_p ())
3048 step_min
= r
.lower_bound ();
3049 step_max
= r
.upper_bound ();
3051 if (!get_max_loop_iterations (loop
, &nit
))
3054 type_min
= wi::min_value (type
);
3055 type_max
= wi::max_value (type
);
3057 /* Just sanity check that we don't see values out of the range of the type.
3058 In this case the arithmetics bellow would overflow. */
3059 gcc_checking_assert (wi::ge_p (base_min
, type_min
, sgn
)
3060 && wi::le_p (base_max
, type_max
, sgn
));
3062 /* Account the possible increment in the last ieration. */
3063 wi::overflow_type overflow
= wi::OVF_NONE
;
3064 nit
= wi::add (nit
, 1, SIGNED
, &overflow
);
3068 /* NIT is typeless and can exceed the precision of the type. In this case
3069 overflow is always possible, because we know STEP is non-zero. */
3070 if (wi::min_precision (nit
, UNSIGNED
) > TYPE_PRECISION (type
))
3072 wide_int nit2
= wide_int::from (nit
, TYPE_PRECISION (type
), UNSIGNED
);
3074 /* If step can be positive, check that nit*step <= type_max-base.
3075 This can be done by unsigned arithmetic and we only need to watch overflow
3076 in the multiplication. The right hand side can always be represented in
3078 if (sgn
== UNSIGNED
|| !wi::neg_p (step_max
))
3080 wi::overflow_type overflow
= wi::OVF_NONE
;
3081 if (wi::gtu_p (wi::mul (step_max
, nit2
, UNSIGNED
, &overflow
),
3082 type_max
- base_max
)
3086 /* If step can be negative, check that nit*(-step) <= base_min-type_min. */
3087 if (sgn
== SIGNED
&& wi::neg_p (step_min
))
3089 wi::overflow_type overflow
, overflow2
;
3090 overflow
= overflow2
= wi::OVF_NONE
;
3091 if (wi::gtu_p (wi::mul (wi::neg (step_min
, &overflow2
),
3092 nit2
, UNSIGNED
, &overflow
),
3093 base_min
- type_min
)
3094 || overflow
|| overflow2
)
3101 /* Given EV with form of "(type) {inner_base, inner_step}_loop", this
3102 function tries to derive condition under which it can be simplified
3103 into "{(type)inner_base, (type)inner_step}_loop". The condition is
3104 the maximum number that inner iv can iterate. */
3107 derive_simple_iv_with_niters (tree ev
, tree
*niters
)
3109 if (!CONVERT_EXPR_P (ev
))
3112 tree inner_ev
= TREE_OPERAND (ev
, 0);
3113 if (TREE_CODE (inner_ev
) != POLYNOMIAL_CHREC
)
3116 tree init
= CHREC_LEFT (inner_ev
);
3117 tree step
= CHREC_RIGHT (inner_ev
);
3118 if (TREE_CODE (init
) != INTEGER_CST
3119 || TREE_CODE (step
) != INTEGER_CST
|| integer_zerop (step
))
3122 tree type
= TREE_TYPE (ev
);
3123 tree inner_type
= TREE_TYPE (inner_ev
);
3124 if (TYPE_PRECISION (inner_type
) >= TYPE_PRECISION (type
))
3127 /* Type conversion in "(type) {inner_base, inner_step}_loop" can be
3128 folded only if inner iv won't overflow. We compute the maximum
3129 number the inner iv can iterate before overflowing and return the
3130 simplified affine iv. */
3132 init
= fold_convert (type
, init
);
3133 step
= fold_convert (type
, step
);
3134 ev
= build_polynomial_chrec (CHREC_VARIABLE (inner_ev
), init
, step
);
3135 if (tree_int_cst_sign_bit (step
))
3137 tree bound
= lower_bound_in_type (inner_type
, inner_type
);
3138 delta
= fold_build2 (MINUS_EXPR
, type
, init
, fold_convert (type
, bound
));
3139 step
= fold_build1 (NEGATE_EXPR
, type
, step
);
3143 tree bound
= upper_bound_in_type (inner_type
, inner_type
);
3144 delta
= fold_build2 (MINUS_EXPR
, type
, fold_convert (type
, bound
), init
);
3146 *niters
= fold_build2 (FLOOR_DIV_EXPR
, type
, delta
, step
);
3150 /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with
3151 respect to WRTO_LOOP and returns its base and step in IV if possible
3152 (see analyze_scalar_evolution_in_loop for more details on USE_LOOP
3153 and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be
3154 invariant in LOOP. Otherwise we require it to be an integer constant.
3156 IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g.
3157 because it is computed in signed arithmetics). Consequently, adding an
3160 for (i = IV->base; ; i += IV->step)
3162 is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is
3163 false for the type of the induction variable, or you can prove that i does
3164 not wrap by some other argument. Otherwise, this might introduce undefined
3168 for (; ; i = (type) ((unsigned type) i + (unsigned type) iv->step))
3170 must be used instead.
3172 When IV_NITERS is not NULL, this function also checks case in which OP
3173 is a conversion of an inner simple iv of below form:
3175 (outer_type){inner_base, inner_step}_loop.
3177 If type of inner iv has smaller precision than outer_type, it can't be
3178 folded into {(outer_type)inner_base, (outer_type)inner_step}_loop because
3179 the inner iv could overflow/wrap. In this case, we derive a condition
3180 under which the inner iv won't overflow/wrap and do the simplification.
3181 The derived condition normally is the maximum number the inner iv can
3182 iterate, and will be stored in IV_NITERS. This is useful in loop niter
3183 analysis, to derive break conditions when a loop must terminate, when is
3187 simple_iv_with_niters (class loop
*wrto_loop
, class loop
*use_loop
,
3188 tree op
, affine_iv
*iv
, tree
*iv_niters
,
3189 bool allow_nonconstant_step
)
3191 enum tree_code code
;
3192 tree type
, ev
, base
, e
;
3196 iv
->base
= NULL_TREE
;
3197 iv
->step
= NULL_TREE
;
3198 iv
->no_overflow
= false;
3200 type
= TREE_TYPE (op
);
3201 if (!POINTER_TYPE_P (type
)
3202 && !INTEGRAL_TYPE_P (type
))
3205 ev
= analyze_scalar_evolution_in_loop (wrto_loop
, use_loop
, op
,
3207 if (chrec_contains_undetermined (ev
)
3208 || chrec_contains_symbols_defined_in_loop (ev
, wrto_loop
->num
))
3211 if (tree_does_not_contain_chrecs (ev
))
3214 iv
->step
= build_int_cst (TREE_TYPE (ev
), 0);
3215 iv
->no_overflow
= true;
3219 /* If we can derive valid scalar evolution with assumptions. */
3220 if (iv_niters
&& TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
3221 ev
= derive_simple_iv_with_niters (ev
, iv_niters
);
3223 if (TREE_CODE (ev
) != POLYNOMIAL_CHREC
)
3226 if (CHREC_VARIABLE (ev
) != (unsigned) wrto_loop
->num
)
3229 iv
->step
= CHREC_RIGHT (ev
);
3230 if ((!allow_nonconstant_step
&& TREE_CODE (iv
->step
) != INTEGER_CST
)
3231 || tree_contains_chrecs (iv
->step
, NULL
))
3234 iv
->base
= CHREC_LEFT (ev
);
3235 if (tree_contains_chrecs (iv
->base
, NULL
))
3238 iv
->no_overflow
= !folded_casts
&& nowrap_type_p (type
);
3240 if (!iv
->no_overflow
3241 && !iv_can_overflow_p (wrto_loop
, type
, iv
->base
, iv
->step
))
3242 iv
->no_overflow
= true;
3244 /* Try to simplify iv base:
3246 (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T
3247 == (signed T)(unsigned T)base + step
3250 If we can prove operation (base + step) doesn't overflow or underflow.
3251 Specifically, we try to prove below conditions are satisfied:
3253 base <= UPPER_BOUND (type) - step ;;step > 0
3254 base >= LOWER_BOUND (type) - step ;;step < 0
3256 This is done by proving the reverse conditions are false using loop's
3259 The is necessary to make loop niter, or iv overflow analysis easier
3262 int foo (int *a, signed char s, signed char l)
3265 for (i = s; i < l; i++)
3270 Note variable I is firstly converted to type unsigned char, incremented,
3271 then converted back to type signed char. */
3273 if (wrto_loop
->num
!= use_loop
->num
)
3276 if (!CONVERT_EXPR_P (iv
->base
) || TREE_CODE (iv
->step
) != INTEGER_CST
)
3279 type
= TREE_TYPE (iv
->base
);
3280 e
= TREE_OPERAND (iv
->base
, 0);
3281 if (TREE_CODE (e
) != PLUS_EXPR
3282 || TREE_CODE (TREE_OPERAND (e
, 1)) != INTEGER_CST
3283 || !tree_int_cst_equal (iv
->step
,
3284 fold_convert (type
, TREE_OPERAND (e
, 1))))
3286 e
= TREE_OPERAND (e
, 0);
3287 if (!CONVERT_EXPR_P (e
))
3289 base
= TREE_OPERAND (e
, 0);
3290 if (!useless_type_conversion_p (type
, TREE_TYPE (base
)))
3293 if (tree_int_cst_sign_bit (iv
->step
))
3296 extreme
= wi::min_value (type
);
3301 extreme
= wi::max_value (type
);
3303 wi::overflow_type overflow
= wi::OVF_NONE
;
3304 extreme
= wi::sub (extreme
, wi::to_wide (iv
->step
),
3305 TYPE_SIGN (type
), &overflow
);
3308 e
= fold_build2 (code
, boolean_type_node
, base
,
3309 wide_int_to_tree (type
, extreme
));
3310 e
= simplify_using_initial_conditions (use_loop
, e
);
3311 if (!integer_zerop (e
))
3314 if (POINTER_TYPE_P (TREE_TYPE (base
)))
3315 code
= POINTER_PLUS_EXPR
;
3319 iv
->base
= fold_build2 (code
, TREE_TYPE (base
), base
, iv
->step
);
3323 /* Like simple_iv_with_niters, but return TRUE when OP behaves as a simple
3324 affine iv unconditionally. */
3327 simple_iv (class loop
*wrto_loop
, class loop
*use_loop
, tree op
,
3328 affine_iv
*iv
, bool allow_nonconstant_step
)
3330 return simple_iv_with_niters (wrto_loop
, use_loop
, op
, iv
,
3331 NULL
, allow_nonconstant_step
);
3334 /* Finalize the scalar evolution analysis. */
3337 scev_finalize (void)
3339 if (!scalar_evolution_info
)
3341 scalar_evolution_info
->empty ();
3342 scalar_evolution_info
= NULL
;
3343 free_numbers_of_iterations_estimates (cfun
);
3346 /* Returns true if the expression EXPR is considered to be too expensive
3347 for scev_const_prop. */
3350 expression_expensive_p (tree expr
, hash_map
<tree
, uint64_t> &cache
,
3353 enum tree_code code
;
3355 if (is_gimple_val (expr
))
3358 code
= TREE_CODE (expr
);
3359 if (code
== TRUNC_DIV_EXPR
3360 || code
== CEIL_DIV_EXPR
3361 || code
== FLOOR_DIV_EXPR
3362 || code
== ROUND_DIV_EXPR
3363 || code
== TRUNC_MOD_EXPR
3364 || code
== CEIL_MOD_EXPR
3365 || code
== FLOOR_MOD_EXPR
3366 || code
== ROUND_MOD_EXPR
3367 || code
== EXACT_DIV_EXPR
)
3369 /* Division by power of two is usually cheap, so we allow it.
3370 Forbid anything else. */
3371 if (!integer_pow2p (TREE_OPERAND (expr
, 1)))
3376 uint64_t &local_cost
= cache
.get_or_insert (expr
, &visited_p
);
3379 uint64_t tem
= cost
+ local_cost
;
3387 uint64_t op_cost
= 0;
3388 if (code
== CALL_EXPR
)
3391 call_expr_arg_iterator iter
;
3392 /* Even though is_inexpensive_builtin might say true, we will get a
3393 library call for popcount when backend does not have an instruction
3394 to do so. We consider this to be expensive and generate
3395 __builtin_popcount only when backend defines it. */
3397 combined_fn cfn
= get_call_combined_fn (expr
);
3401 optab
= popcount_optab
;
3409 /* Check if opcode for popcount is available in the mode required. */
3410 if (optab_handler (optab
,
3411 TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr
, 0))))
3412 == CODE_FOR_nothing
)
3415 mode
= TYPE_MODE (TREE_TYPE (CALL_EXPR_ARG (expr
, 0)));
3416 scalar_int_mode int_mode
;
3418 /* If the mode is of 2 * UNITS_PER_WORD size, we can handle
3419 double-word popcount by emitting two single-word popcount
3421 if (is_a
<scalar_int_mode
> (mode
, &int_mode
)
3422 && GET_MODE_SIZE (int_mode
) == 2 * UNITS_PER_WORD
3423 && (optab_handler (optab
, word_mode
)
3424 != CODE_FOR_nothing
))
3432 || !is_inexpensive_builtin (get_callee_fndecl (expr
)))
3437 FOR_EACH_CALL_EXPR_ARG (arg
, iter
, expr
)
3438 if (expression_expensive_p (arg
, cache
, op_cost
))
3440 *cache
.get (expr
) += op_cost
;
3441 cost
+= op_cost
+ 1;
3445 if (code
== COND_EXPR
)
3447 if (expression_expensive_p (TREE_OPERAND (expr
, 0), cache
, op_cost
)
3448 || (EXPR_P (TREE_OPERAND (expr
, 1))
3449 && EXPR_P (TREE_OPERAND (expr
, 2)))
3450 /* If either branch has side effects or could trap. */
3451 || TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 1))
3452 || generic_expr_could_trap_p (TREE_OPERAND (expr
, 1))
3453 || TREE_SIDE_EFFECTS (TREE_OPERAND (expr
, 0))
3454 || generic_expr_could_trap_p (TREE_OPERAND (expr
, 0))
3455 || expression_expensive_p (TREE_OPERAND (expr
, 1),
3457 || expression_expensive_p (TREE_OPERAND (expr
, 2),
3460 *cache
.get (expr
) += op_cost
;
3461 cost
+= op_cost
+ 1;
3465 switch (TREE_CODE_CLASS (code
))
3468 case tcc_comparison
:
3469 if (expression_expensive_p (TREE_OPERAND (expr
, 1), cache
, op_cost
))
3474 if (expression_expensive_p (TREE_OPERAND (expr
, 0), cache
, op_cost
))
3476 *cache
.get (expr
) += op_cost
;
3477 cost
+= op_cost
+ 1;
3486 expression_expensive_p (tree expr
)
3488 hash_map
<tree
, uint64_t> cache
;
3489 uint64_t expanded_size
= 0;
3490 return (expression_expensive_p (expr
, cache
, expanded_size
)
3491 || expanded_size
> cache
.elements ());
3494 /* Match.pd function to match bitwise inductive expression.
3498 tmp_9 = _3 & tmp_12; */
3499 extern bool gimple_bitwise_induction_p (tree
, tree
*, tree (*)(tree
));
3501 /* Return the inductive expression of bitwise operation if possible,
3502 otherwise returns DEF. */
3504 analyze_and_compute_bitwise_induction_effect (class loop
* loop
,
3506 unsigned HOST_WIDE_INT niter
)
3508 tree match_op
[3],inv
, bitwise_scev
;
3509 tree type
= TREE_TYPE (phidef
);
3510 gphi
* header_phi
= NULL
;
3512 /* Match things like op2(MATCH_OP[2]), op1(MATCH_OP[1]), phidef(PHIDEF)
3514 op2 = PHI <phidef, inv>
3518 phidef = op1 & op2; */
3519 if (!gimple_bitwise_induction_p (phidef
, &match_op
[0], NULL
)
3520 || TREE_CODE (match_op
[2]) != SSA_NAME
3521 || !(header_phi
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (match_op
[2])))
3522 || gimple_bb (header_phi
) != loop
->header
3523 || gimple_phi_num_args (header_phi
) != 2)
3526 if (PHI_ARG_DEF_FROM_EDGE (header_phi
, loop_latch_edge (loop
)) != phidef
)
3529 bitwise_scev
= analyze_scalar_evolution (loop
, match_op
[1]);
3530 bitwise_scev
= instantiate_parameters (loop
, bitwise_scev
);
3532 /* Make sure bits is in range of type precision. */
3533 if (TREE_CODE (bitwise_scev
) != POLYNOMIAL_CHREC
3534 || !INTEGRAL_TYPE_P (TREE_TYPE (bitwise_scev
))
3535 || !tree_fits_uhwi_p (CHREC_LEFT (bitwise_scev
))
3536 || tree_to_uhwi (CHREC_LEFT (bitwise_scev
)) >= TYPE_PRECISION (type
)
3537 || !tree_fits_shwi_p (CHREC_RIGHT (bitwise_scev
)))
3542 INDUCTION_BIT_CLEAR
,
3545 INDUCTION_BIT_RESET
,
3550 enum bit_op_kind induction_kind
;
3551 enum tree_code code1
3552 = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (phidef
));
3553 enum tree_code code2
3554 = gimple_assign_rhs_code (SSA_NAME_DEF_STMT (match_op
[0]));
3556 /* BIT_CLEAR: A &= ~(1 << bit)
3557 BIT_RESET: A ^= (1 << bit).
3558 BIT_IOR: A |= (1 << bit)
3559 BIT_ZERO: A &= (1 << bit)
3560 BIT_ALL: A |= ~(1 << bit)
3561 BIT_XOR: A ^= ~(1 << bit).
3562 bit is induction variable. */
3566 induction_kind
= code2
== BIT_NOT_EXPR
3567 ? INDUCTION_BIT_CLEAR
3571 induction_kind
= code2
== BIT_NOT_EXPR
3573 : INDUCTION_BIT_IOR
;
3576 induction_kind
= code2
== BIT_NOT_EXPR
3578 : INDUCTION_BIT_RESET
;
3580 /* A ^ ~(1 << bit) is equal to ~(A ^ (1 << bit)). */
3582 gcc_assert (code2
== BIT_XOR_EXPR
);
3583 induction_kind
= INDUCTION_BIT_XOR
;
3589 if (induction_kind
== INDUCTION_ZERO
)
3590 return build_zero_cst (type
);
3591 if (induction_kind
== INDUCTION_ALL
)
3592 return build_all_ones_cst (type
);
3594 wide_int bits
= wi::zero (TYPE_PRECISION (type
));
3595 HOST_WIDE_INT bit_start
= tree_to_shwi (CHREC_LEFT (bitwise_scev
));
3596 HOST_WIDE_INT step
= tree_to_shwi (CHREC_RIGHT (bitwise_scev
));
3597 HOST_WIDE_INT bit_final
= bit_start
+ step
* niter
;
3599 /* bit_start, bit_final in range of [0,TYPE_PRECISION)
3600 implies all bits are set in range. */
3601 if (bit_final
>= TYPE_PRECISION (type
)
3605 /* Loop tripcount should be niter + 1. */
3606 for (unsigned i
= 0; i
!= niter
+ 1; i
++)
3608 bits
= wi::set_bit (bits
, bit_start
);
3612 bool inverted
= false;
3613 switch (induction_kind
)
3615 case INDUCTION_BIT_CLEAR
:
3616 code1
= BIT_AND_EXPR
;
3619 case INDUCTION_BIT_IOR
:
3620 code1
= BIT_IOR_EXPR
;
3622 case INDUCTION_BIT_RESET
:
3623 code1
= BIT_XOR_EXPR
;
3625 /* A ^= ~(1 << bit) is special, when loop tripcount is even,
3626 it's equal to A ^= bits, else A ^= ~bits. */
3627 case INDUCTION_BIT_XOR
:
3628 code1
= BIT_XOR_EXPR
;
3637 bits
= wi::bit_not (bits
);
3639 inv
= PHI_ARG_DEF_FROM_EDGE (header_phi
, loop_preheader_edge (loop
));
3640 return fold_build2 (code1
, type
, inv
, wide_int_to_tree (type
, bits
));
3643 /* Match.pd function to match bitop with invariant expression
3646 extern bool gimple_bitop_with_inv_p (tree
, tree
*, tree (*)(tree
));
3648 /* Return the inductive expression of bitop with invariant if possible,
3649 otherwise returns DEF. */
3651 analyze_and_compute_bitop_with_inv_effect (class loop
* loop
, tree phidef
,
3654 tree match_op
[2],inv
;
3655 tree type
= TREE_TYPE (phidef
);
3656 gphi
* header_phi
= NULL
;
3657 enum tree_code code
;
3658 /* match thing like op0 (match[0]), op1 (match[1]), phidef (PHIDEF)
3660 op1 = PHI <phidef, inv>
3662 if op0 is an invariant, it could change to
3663 phidef = op0 & inv. */
3665 def
= SSA_NAME_DEF_STMT (phidef
);
3666 if (!(is_gimple_assign (def
)
3667 && ((code
= gimple_assign_rhs_code (def
)), true)
3668 && (code
== BIT_AND_EXPR
|| code
== BIT_IOR_EXPR
3669 || code
== BIT_XOR_EXPR
)))
3672 match_op
[0] = gimple_assign_rhs1 (def
);
3673 match_op
[1] = gimple_assign_rhs2 (def
);
3675 if (TREE_CODE (match_op
[1]) != SSA_NAME
3676 || !expr_invariant_in_loop_p (loop
, match_op
[0])
3677 || !(header_phi
= dyn_cast
<gphi
*> (SSA_NAME_DEF_STMT (match_op
[1])))
3678 || gimple_bb (header_phi
) != loop
->header
3679 || gimple_phi_num_args (header_phi
) != 2)
3682 if (PHI_ARG_DEF_FROM_EDGE (header_phi
, loop_latch_edge (loop
)) != phidef
)
3685 enum tree_code code1
3686 = gimple_assign_rhs_code (def
);
3688 if (code1
== BIT_XOR_EXPR
)
3690 if (!tree_fits_uhwi_p (niter
))
3692 unsigned HOST_WIDE_INT niter_num
;
3693 niter_num
= tree_to_uhwi (niter
);
3694 if (niter_num
% 2 != 0)
3695 match_op
[0] = build_zero_cst (type
);
3698 inv
= PHI_ARG_DEF_FROM_EDGE (header_phi
, loop_preheader_edge (loop
));
3699 return fold_build2 (code1
, type
, inv
, match_op
[0]);
3702 /* Do final value replacement for LOOP, return true if we did anything. */
3705 final_value_replacement_loop (class loop
*loop
)
3707 /* If we do not know exact number of iterations of the loop, we cannot
3708 replace the final value. */
3709 edge exit
= single_exit (loop
);
3713 tree niter
= number_of_latch_executions (loop
);
3714 if (niter
== chrec_dont_know
)
3717 /* Ensure that it is possible to insert new statements somewhere. */
3718 if (!single_pred_p (exit
->dest
))
3719 split_loop_exit_edge (exit
);
3721 /* Set stmt insertion pointer. All stmts are inserted before this point. */
3722 gimple_stmt_iterator gsi
= gsi_after_labels (exit
->dest
);
3725 = superloop_at_depth (loop
,
3726 loop_depth (exit
->dest
->loop_father
) + 1);
3730 for (psi
= gsi_start_phis (exit
->dest
); !gsi_end_p (psi
); )
3732 gphi
*phi
= psi
.phi ();
3733 tree rslt
= PHI_RESULT (phi
);
3734 tree phidef
= PHI_ARG_DEF_FROM_EDGE (phi
, exit
);
3736 if (virtual_operand_p (def
))
3742 if (!POINTER_TYPE_P (TREE_TYPE (def
))
3743 && !INTEGRAL_TYPE_P (TREE_TYPE (def
)))
3750 def
= analyze_scalar_evolution_in_loop (ex_loop
, loop
, def
,
3753 tree bitinv_def
, bit_def
;
3754 unsigned HOST_WIDE_INT niter_num
;
3756 if (def
!= chrec_dont_know
)
3757 def
= compute_overall_effect_of_inner_loop (ex_loop
, def
);
3759 /* Handle bitop with invariant induction expression.
3762 for (int i =0 ;i < 32; i++)
3764 if bit2 is an invariant in loop which could simple to
3766 else if ((bitinv_def
3767 = analyze_and_compute_bitop_with_inv_effect (loop
,
3771 /* Handle bitwise induction expression.
3774 for (int i = 0; i != 64; i+=3)
3777 RES can't be analyzed out by SCEV because it is not polynomially
3778 expressible, but in fact final value of RES can be replaced by
3779 RES & CONSTANT where CONSTANT all ones with bit {0,3,6,9,... ,63}
3780 being cleared, similar for BIT_IOR_EXPR/BIT_XOR_EXPR. */
3781 else if (tree_fits_uhwi_p (niter
)
3782 && (niter_num
= tree_to_uhwi (niter
)) != 0
3783 && niter_num
< TYPE_PRECISION (TREE_TYPE (phidef
))
3785 = analyze_and_compute_bitwise_induction_effect (loop
,
3790 if (!tree_does_not_contain_chrecs (def
)
3791 || chrec_contains_symbols_defined_in_loop (def
, ex_loop
->num
)
3792 /* Moving the computation from the loop may prolong life range
3793 of some ssa names, which may cause problems if they appear
3794 on abnormal edges. */
3795 || contains_abnormal_ssa_name_p (def
)
3796 /* Do not emit expensive expressions. The rationale is that
3797 when someone writes a code like
3799 while (n > 45) n -= 45;
3801 he probably knows that n is not large, and does not want it
3802 to be turned into n %= 45. */
3803 || expression_expensive_p (def
))
3805 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3807 fprintf (dump_file
, "not replacing:\n ");
3808 print_gimple_stmt (dump_file
, phi
, 0);
3809 fprintf (dump_file
, "\n");
3815 /* Eliminate the PHI node and replace it by a computation outside
3819 fprintf (dump_file
, "\nfinal value replacement:\n ");
3820 print_gimple_stmt (dump_file
, phi
, 0);
3821 fprintf (dump_file
, " with expr: ");
3822 print_generic_expr (dump_file
, def
);
3825 def
= unshare_expr (def
);
3826 remove_phi_node (&psi
, false);
3828 /* If def's type has undefined overflow and there were folded
3829 casts, rewrite all stmts added for def into arithmetics
3830 with defined overflow behavior. */
3831 if (folded_casts
&& ANY_INTEGRAL_TYPE_P (TREE_TYPE (def
))
3832 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def
)))
3835 gimple_stmt_iterator gsi2
;
3836 def
= force_gimple_operand (def
, &stmts
, true, NULL_TREE
);
3837 gsi2
= gsi_start (stmts
);
3838 while (!gsi_end_p (gsi2
))
3840 gimple
*stmt
= gsi_stmt (gsi2
);
3841 gimple_stmt_iterator gsi3
= gsi2
;
3843 gsi_remove (&gsi3
, false);
3844 if (is_gimple_assign (stmt
)
3845 && arith_code_with_undefined_signed_overflow
3846 (gimple_assign_rhs_code (stmt
)))
3847 gsi_insert_seq_before (&gsi
,
3848 rewrite_to_defined_overflow (stmt
),
3851 gsi_insert_before (&gsi
, stmt
, GSI_SAME_STMT
);
3855 def
= force_gimple_operand_gsi (&gsi
, def
, false, NULL_TREE
,
3856 true, GSI_SAME_STMT
);
3858 gassign
*ass
= gimple_build_assign (rslt
, def
);
3859 gimple_set_location (ass
,
3860 gimple_phi_arg_location (phi
, exit
->dest_idx
));
3861 gsi_insert_before (&gsi
, ass
, GSI_SAME_STMT
);
3864 fprintf (dump_file
, "\n final stmt:\n ");
3865 print_gimple_stmt (dump_file
, ass
, 0);
3866 fprintf (dump_file
, "\n");
3873 #include "gt-tree-scalar-evolution.h"