]>
Commit | Line | Data |
---|---|---|
e9eb809d | 1 | /* Scalar evolution detector. |
5624e564 | 2 | Copyright (C) 2003-2015 Free Software Foundation, Inc. |
e9eb809d ZD |
3 | Contributed by Sebastian Pop <s.pop@laposte.net> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
7 | GCC is free software; you can redistribute it and/or modify it under | |
8 | the terms of the GNU General Public License as published by the Free | |
9dcd6f09 | 9 | Software Foundation; either version 3, or (at your option) any later |
e9eb809d ZD |
10 | version. |
11 | ||
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
e9eb809d | 20 | |
b8698a0f L |
21 | /* |
22 | Description: | |
23 | ||
9baba81b SP |
24 | This pass analyzes the evolution of scalar variables in loop |
25 | structures. The algorithm is based on the SSA representation, | |
26 | and on the loop hierarchy tree. This algorithm is not based on | |
27 | the notion of versions of a variable, as it was the case for the | |
28 | previous implementations of the scalar evolution algorithm, but | |
29 | it assumes that each defined name is unique. | |
30 | ||
31 | The notation used in this file is called "chains of recurrences", | |
32 | and has been proposed by Eugene Zima, Robert Van Engelen, and | |
33 | others for describing induction variables in programs. For example | |
34 | "b -> {0, +, 2}_1" means that the scalar variable "b" is equal to 0 | |
35 | when entering in the loop_1 and has a step 2 in this loop, in other | |
36 | words "for (b = 0; b < N; b+=2);". Note that the coefficients of | |
37 | this chain of recurrence (or chrec [shrek]) can contain the name of | |
38 | other variables, in which case they are called parametric chrecs. | |
39 | For example, "b -> {a, +, 2}_1" means that the initial value of "b" | |
40 | is the value of "a". In most of the cases these parametric chrecs | |
41 | are fully instantiated before their use because symbolic names can | |
42 | hide some difficult cases such as self-references described later | |
43 | (see the Fibonacci example). | |
b8698a0f | 44 | |
9baba81b | 45 | A short sketch of the algorithm is: |
b8698a0f | 46 | |
9baba81b SP |
47 | Given a scalar variable to be analyzed, follow the SSA edge to |
48 | its definition: | |
b8698a0f | 49 | |
726a989a | 50 | - When the definition is a GIMPLE_ASSIGN: if the right hand side |
9baba81b | 51 | (RHS) of the definition cannot be statically analyzed, the answer |
b8698a0f | 52 | of the analyzer is: "don't know". |
9baba81b SP |
53 | Otherwise, for all the variables that are not yet analyzed in the |
54 | RHS, try to determine their evolution, and finally try to | |
55 | evaluate the operation of the RHS that gives the evolution | |
56 | function of the analyzed variable. | |
57 | ||
58 | - When the definition is a condition-phi-node: determine the | |
59 | evolution function for all the branches of the phi node, and | |
60 | finally merge these evolutions (see chrec_merge). | |
61 | ||
62 | - When the definition is a loop-phi-node: determine its initial | |
63 | condition, that is the SSA edge defined in an outer loop, and | |
64 | keep it symbolic. Then determine the SSA edges that are defined | |
65 | in the body of the loop. Follow the inner edges until ending on | |
66 | another loop-phi-node of the same analyzed loop. If the reached | |
67 | loop-phi-node is not the starting loop-phi-node, then we keep | |
68 | this definition under a symbolic form. If the reached | |
69 | loop-phi-node is the same as the starting one, then we compute a | |
70 | symbolic stride on the return path. The result is then the | |
71 | symbolic chrec {initial_condition, +, symbolic_stride}_loop. | |
72 | ||
73 | Examples: | |
b8698a0f | 74 | |
9baba81b | 75 | Example 1: Illustration of the basic algorithm. |
b8698a0f | 76 | |
9baba81b SP |
77 | | a = 3 |
78 | | loop_1 | |
79 | | b = phi (a, c) | |
80 | | c = b + 1 | |
81 | | if (c > 10) exit_loop | |
82 | | endloop | |
b8698a0f | 83 | |
9baba81b SP |
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 | |
3f227a8c | 104 | instantiate_parameters (loop_1, {a + 1, +, 1}_1), that gives after some |
9baba81b SP |
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: | |
b8698a0f | 114 | |
9baba81b SP |
115 | a -> 3 |
116 | b -> {3, +, 1}_1 | |
117 | c -> {4, +, 1}_1 | |
b8698a0f L |
118 | |
119 | or in terms of a C program: | |
120 | ||
9baba81b SP |
121 | | a = 3 |
122 | | for (x = 0; x <= 7; x++) | |
123 | | { | |
124 | | b = x + 3 | |
125 | | c = x + 4 | |
126 | | } | |
b8698a0f | 127 | |
3f227a8c | 128 | Example 2a: Illustration of the algorithm on nested loops. |
b8698a0f | 129 | |
9baba81b SP |
130 | | loop_1 |
131 | | a = phi (1, b) | |
132 | | c = a + 2 | |
133 | | loop_2 10 times | |
134 | | b = phi (c, d) | |
135 | | d = b + 3 | |
136 | | endloop | |
137 | | endloop | |
b8698a0f | 138 | |
9baba81b SP |
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 | |
b8698a0f L |
141 | loop-phi-node, and its analysis as in Example 1, gives: |
142 | ||
9baba81b SP |
143 | b -> {c, +, 3}_2 |
144 | d -> {c + 3, +, 3}_2 | |
b8698a0f | 145 | |
9baba81b SP |
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 | |
b8698a0f L |
152 | equal to "+32", and the result is: |
153 | ||
9baba81b SP |
154 | a -> {1, +, 32}_1 |
155 | c -> {3, +, 32}_1 | |
3f227a8c JS |
156 | |
157 | Example 2b: Multivariate chains of recurrences. | |
158 | ||
159 | | loop_1 | |
160 | | k = phi (0, k + 1) | |
161 | | loop_2 4 times | |
162 | | j = phi (0, j + 1) | |
163 | | loop_3 4 times | |
164 | | i = phi (0, i + 1) | |
165 | | A[j + k] = ... | |
166 | | endloop | |
167 | | endloop | |
168 | | endloop | |
169 | ||
170 | Analyzing the access function of array A with | |
171 | instantiate_parameters (loop_1, "j + k"), we obtain the | |
172 | instantiation and the analysis of the scalar variables "j" and "k" | |
173 | in loop_1. This leads to the scalar evolution {4, +, 1}_1: the end | |
174 | value of loop_2 for "j" is 4, and the evolution of "k" in loop_1 is | |
175 | {0, +, 1}_1. To obtain the evolution function in loop_3 and | |
176 | instantiate the scalar variables up to loop_1, one has to use: | |
a213b219 SP |
177 | instantiate_scev (block_before_loop (loop_1), loop_3, "j + k"). |
178 | The result of this call is {{0, +, 1}_1, +, 1}_2. | |
3f227a8c | 179 | |
9baba81b | 180 | Example 3: Higher degree polynomials. |
b8698a0f | 181 | |
9baba81b SP |
182 | | loop_1 |
183 | | a = phi (2, b) | |
184 | | c = phi (5, d) | |
185 | | b = a + 1 | |
186 | | d = c + a | |
187 | | endloop | |
b8698a0f | 188 | |
9baba81b SP |
189 | a -> {2, +, 1}_1 |
190 | b -> {3, +, 1}_1 | |
191 | c -> {5, +, a}_1 | |
192 | d -> {5 + a, +, a}_1 | |
b8698a0f | 193 | |
3f227a8c JS |
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 | |
b8698a0f | 196 | |
9baba81b | 197 | Example 4: Lucas, Fibonacci, or mixers in general. |
b8698a0f | 198 | |
9baba81b SP |
199 | | loop_1 |
200 | | a = phi (1, b) | |
201 | | c = phi (3, d) | |
202 | | b = c | |
203 | | d = c + a | |
204 | | endloop | |
b8698a0f | 205 | |
9baba81b SP |
206 | a -> (1, c)_1 |
207 | c -> {3, +, a}_1 | |
b8698a0f | 208 | |
9baba81b SP |
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)". | |
b8698a0f | 214 | |
9baba81b SP |
215 | The symbolic chrec representation contains all the semantics of the |
216 | original code. What is more difficult is to use this information. | |
b8698a0f | 217 | |
9baba81b | 218 | Example 5: Flip-flops, or exchangers. |
b8698a0f | 219 | |
9baba81b SP |
220 | | loop_1 |
221 | | a = phi (1, b) | |
222 | | c = phi (3, d) | |
223 | | b = c | |
224 | | d = a | |
225 | | endloop | |
b8698a0f | 226 | |
9baba81b SP |
227 | a -> (1, c)_1 |
228 | c -> (3, a)_1 | |
b8698a0f | 229 | |
9baba81b | 230 | Based on these symbolic chrecs, it is possible to refine this |
b8698a0f L |
231 | information into the more precise PERIODIC_CHRECs: |
232 | ||
9baba81b SP |
233 | a -> |1, 3|_1 |
234 | c -> |3, 1|_1 | |
b8698a0f | 235 | |
9baba81b | 236 | This transformation is not yet implemented. |
b8698a0f | 237 | |
9baba81b | 238 | Further readings: |
b8698a0f | 239 | |
9baba81b SP |
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. | |
b8698a0f | 247 | |
9baba81b SP |
248 | A set of slides show a high level overview of the algorithm and run |
249 | an example through the scalar evolution analyzer: | |
250 | http://cri.ensmp.fr/~pop/gcc/mar04/slides.pdf | |
251 | ||
252 | The slides that I have presented at the GCC Summit'04 are available | |
253 | at: http://cri.ensmp.fr/~pop/gcc/20040604/gccsummit-lno-spop.pdf | |
254 | */ | |
255 | ||
e9eb809d ZD |
256 | #include "config.h" |
257 | #include "system.h" | |
258 | #include "coretypes.h" | |
c7131fb2 | 259 | #include "backend.h" |
957060b5 | 260 | #include "rtl.h" |
cf2d1b38 | 261 | #include "tree.h" |
c7131fb2 | 262 | #include "gimple.h" |
c7131fb2 | 263 | #include "ssa.h" |
957060b5 AM |
264 | #include "expmed.h" |
265 | #include "insn-config.h" | |
266 | #include "emit-rtl.h" | |
267 | #include "gimple-pretty-print.h" | |
268 | #include "alias.h" | |
c7131fb2 | 269 | #include "fold-const.h" |
36566b39 | 270 | #include "flags.h" |
36566b39 PK |
271 | #include "dojump.h" |
272 | #include "explow.h" | |
273 | #include "calls.h" | |
36566b39 PK |
274 | #include "varasm.h" |
275 | #include "stmt.h" | |
276 | #include "expr.h" | |
2fb9a547 | 277 | #include "internal-fn.h" |
45b0be94 | 278 | #include "gimplify.h" |
5be5c238 | 279 | #include "gimple-iterator.h" |
18f429e2 | 280 | #include "gimplify-me.h" |
442b4905 | 281 | #include "tree-cfg.h" |
e28030cf AM |
282 | #include "tree-ssa-loop-ivopts.h" |
283 | #include "tree-ssa-loop-manip.h" | |
284 | #include "tree-ssa-loop-niter.h" | |
442b4905 | 285 | #include "tree-ssa-loop.h" |
7a300452 | 286 | #include "tree-ssa.h" |
e9eb809d ZD |
287 | #include "cfgloop.h" |
288 | #include "tree-chrec.h" | |
b83b5507 | 289 | #include "tree-affine.h" |
e9eb809d | 290 | #include "tree-scalar-evolution.h" |
7ee2468b | 291 | #include "dumpfile.h" |
c59dabbe | 292 | #include "params.h" |
744730a4 | 293 | #include "tree-ssa-propagate.h" |
19e51b40 | 294 | #include "gimple-fold.h" |
9baba81b SP |
295 | |
296 | static tree analyze_scalar_evolution_1 (struct loop *, tree, tree); | |
bef28ced JL |
297 | static tree analyze_scalar_evolution_for_address_of (struct loop *loop, |
298 | tree var); | |
9baba81b | 299 | |
a3cc13cc RB |
300 | /* The cached information about an SSA name with version NAME_VERSION, |
301 | claiming that below basic block with index INSTANTIATED_BELOW, the | |
302 | value of the SSA name can be expressed as CHREC. */ | |
9baba81b | 303 | |
907dadbd | 304 | struct GTY((for_user)) scev_info_str { |
a3cc13cc RB |
305 | unsigned int name_version; |
306 | int instantiated_below; | |
9baba81b SP |
307 | tree chrec; |
308 | }; | |
309 | ||
310 | /* Counters for the scev database. */ | |
311 | static unsigned nb_set_scev = 0; | |
312 | static unsigned nb_get_scev = 0; | |
313 | ||
314 | /* The following trees are unique elements. Thus the comparison of | |
315 | another element to these elements should be done on the pointer to | |
316 | these trees, and not on their value. */ | |
317 | ||
318 | /* The SSA_NAMEs that are not yet analyzed are qualified with NULL_TREE. */ | |
319 | tree chrec_not_analyzed_yet; | |
320 | ||
321 | /* Reserved to the cases where the analyzer has detected an | |
322 | undecidable property at compile time. */ | |
323 | tree chrec_dont_know; | |
324 | ||
325 | /* When the analyzer has detected that a property will never | |
326 | happen, then it qualifies it with chrec_known. */ | |
327 | tree chrec_known; | |
328 | ||
ca752f39 | 329 | struct scev_info_hasher : ggc_ptr_hash<scev_info_str> |
907dadbd TS |
330 | { |
331 | static hashval_t hash (scev_info_str *i); | |
332 | static bool equal (const scev_info_str *a, const scev_info_str *b); | |
333 | }; | |
334 | ||
335 | static GTY (()) hash_table<scev_info_hasher> *scalar_evolution_info; | |
9baba81b SP |
336 | |
337 | \f | |
a213b219 | 338 | /* Constructs a new SCEV_INFO_STR structure for VAR and INSTANTIATED_BELOW. */ |
9baba81b SP |
339 | |
340 | static inline struct scev_info_str * | |
a213b219 | 341 | new_scev_info_str (basic_block instantiated_below, tree var) |
9baba81b SP |
342 | { |
343 | struct scev_info_str *res; | |
b8698a0f | 344 | |
766090c2 | 345 | res = ggc_alloc<scev_info_str> (); |
a3cc13cc | 346 | res->name_version = SSA_NAME_VERSION (var); |
9baba81b | 347 | res->chrec = chrec_not_analyzed_yet; |
a3cc13cc | 348 | res->instantiated_below = instantiated_below->index; |
a213b219 | 349 | |
9baba81b SP |
350 | return res; |
351 | } | |
352 | ||
353 | /* Computes a hash function for database element ELT. */ | |
354 | ||
907dadbd TS |
355 | hashval_t |
356 | scev_info_hasher::hash (scev_info_str *elt) | |
9baba81b | 357 | { |
a3cc13cc | 358 | return elt->name_version ^ elt->instantiated_below; |
9baba81b SP |
359 | } |
360 | ||
361 | /* Compares database elements E1 and E2. */ | |
362 | ||
907dadbd TS |
363 | bool |
364 | scev_info_hasher::equal (const scev_info_str *elt1, const scev_info_str *elt2) | |
9baba81b | 365 | { |
a3cc13cc | 366 | return (elt1->name_version == elt2->name_version |
a213b219 | 367 | && elt1->instantiated_below == elt2->instantiated_below); |
9baba81b SP |
368 | } |
369 | ||
a213b219 SP |
370 | /* Get the scalar evolution of VAR for INSTANTIATED_BELOW basic block. |
371 | A first query on VAR returns chrec_not_analyzed_yet. */ | |
9baba81b SP |
372 | |
373 | static tree * | |
a213b219 | 374 | find_var_scev_info (basic_block instantiated_below, tree var) |
9baba81b SP |
375 | { |
376 | struct scev_info_str *res; | |
377 | struct scev_info_str tmp; | |
9baba81b | 378 | |
a3cc13cc RB |
379 | tmp.name_version = SSA_NAME_VERSION (var); |
380 | tmp.instantiated_below = instantiated_below->index; | |
907dadbd | 381 | scev_info_str **slot = scalar_evolution_info->find_slot (&tmp, INSERT); |
9baba81b SP |
382 | |
383 | if (!*slot) | |
a213b219 | 384 | *slot = new_scev_info_str (instantiated_below, var); |
907dadbd | 385 | res = *slot; |
9baba81b SP |
386 | |
387 | return &res->chrec; | |
388 | } | |
389 | ||
9baba81b SP |
390 | /* Return true when CHREC contains symbolic names defined in |
391 | LOOP_NB. */ | |
392 | ||
b8698a0f | 393 | bool |
ed7a4b4b | 394 | chrec_contains_symbols_defined_in_loop (const_tree chrec, unsigned loop_nb) |
9baba81b | 395 | { |
5039610b SL |
396 | int i, n; |
397 | ||
9baba81b SP |
398 | if (chrec == NULL_TREE) |
399 | return false; | |
400 | ||
ad6003f2 | 401 | if (is_gimple_min_invariant (chrec)) |
9baba81b SP |
402 | return false; |
403 | ||
9baba81b SP |
404 | if (TREE_CODE (chrec) == SSA_NAME) |
405 | { | |
355fe088 | 406 | gimple *def; |
492e5456 SP |
407 | loop_p def_loop, loop; |
408 | ||
409 | if (SSA_NAME_IS_DEFAULT_DEF (chrec)) | |
410 | return false; | |
411 | ||
412 | def = SSA_NAME_DEF_STMT (chrec); | |
413 | def_loop = loop_containing_stmt (def); | |
0fc822d0 | 414 | loop = get_loop (cfun, loop_nb); |
9baba81b SP |
415 | |
416 | if (def_loop == NULL) | |
417 | return false; | |
418 | ||
419 | if (loop == def_loop || flow_loop_nested_p (loop, def_loop)) | |
420 | return true; | |
421 | ||
422 | return false; | |
423 | } | |
424 | ||
5039610b SL |
425 | n = TREE_OPERAND_LENGTH (chrec); |
426 | for (i = 0; i < n; i++) | |
b8698a0f | 427 | if (chrec_contains_symbols_defined_in_loop (TREE_OPERAND (chrec, i), |
5039610b SL |
428 | loop_nb)) |
429 | return true; | |
430 | return false; | |
9baba81b SP |
431 | } |
432 | ||
433 | /* Return true when PHI is a loop-phi-node. */ | |
434 | ||
435 | static bool | |
355fe088 | 436 | loop_phi_node_p (gimple *phi) |
9baba81b SP |
437 | { |
438 | /* The implementation of this function is based on the following | |
439 | property: "all the loop-phi-nodes of a loop are contained in the | |
440 | loop's header basic block". */ | |
441 | ||
726a989a | 442 | return loop_containing_stmt (phi)->header == gimple_bb (phi); |
9baba81b SP |
443 | } |
444 | ||
445 | /* Compute the scalar evolution for EVOLUTION_FN after crossing LOOP. | |
446 | In general, in the case of multivariate evolutions we want to get | |
447 | the evolution in different loops. LOOP specifies the level for | |
448 | which to get the evolution. | |
b8698a0f | 449 | |
9baba81b | 450 | Example: |
b8698a0f | 451 | |
9baba81b SP |
452 | | for (j = 0; j < 100; j++) |
453 | | { | |
454 | | for (k = 0; k < 100; k++) | |
455 | | { | |
b8698a0f | 456 | | i = k + j; - Here the value of i is a function of j, k. |
9baba81b | 457 | | } |
b8698a0f | 458 | | ... = i - Here the value of i is a function of j. |
9baba81b | 459 | | } |
b8698a0f L |
460 | | ... = i - Here the value of i is a scalar. |
461 | ||
462 | Example: | |
463 | ||
9baba81b SP |
464 | | i_0 = ... |
465 | | loop_1 10 times | |
466 | | i_1 = phi (i_0, i_2) | |
467 | | i_2 = i_1 + 2 | |
468 | | endloop | |
b8698a0f | 469 | |
9baba81b SP |
470 | This loop has the same effect as: |
471 | LOOP_1 has the same effect as: | |
b8698a0f | 472 | |
9baba81b | 473 | | i_1 = i_0 + 20 |
b8698a0f L |
474 | |
475 | The overall effect of the loop, "i_0 + 20" in the previous example, | |
476 | is obtained by passing in the parameters: LOOP = 1, | |
9baba81b SP |
477 | EVOLUTION_FN = {i_0, +, 2}_1. |
478 | */ | |
b8698a0f | 479 | |
42e6eec5 | 480 | tree |
9baba81b SP |
481 | compute_overall_effect_of_inner_loop (struct loop *loop, tree evolution_fn) |
482 | { | |
483 | bool val = false; | |
484 | ||
485 | if (evolution_fn == chrec_dont_know) | |
486 | return chrec_dont_know; | |
487 | ||
488 | else if (TREE_CODE (evolution_fn) == POLYNOMIAL_CHREC) | |
489 | { | |
677cc14d ZD |
490 | struct loop *inner_loop = get_chrec_loop (evolution_fn); |
491 | ||
492 | if (inner_loop == loop | |
493 | || flow_loop_nested_p (loop, inner_loop)) | |
9baba81b | 494 | { |
a14865db | 495 | tree nb_iter = number_of_latch_executions (inner_loop); |
9baba81b SP |
496 | |
497 | if (nb_iter == chrec_dont_know) | |
498 | return chrec_dont_know; | |
499 | else | |
500 | { | |
501 | tree res; | |
502 | ||
9baba81b SP |
503 | /* evolution_fn is the evolution function in LOOP. Get |
504 | its value in the nb_iter-th iteration. */ | |
505 | res = chrec_apply (inner_loop->num, evolution_fn, nb_iter); | |
42e6eec5 SP |
506 | |
507 | if (chrec_contains_symbols_defined_in_loop (res, loop->num)) | |
508 | res = instantiate_parameters (loop, res); | |
509 | ||
8c27b7d4 | 510 | /* Continue the computation until ending on a parent of LOOP. */ |
9baba81b SP |
511 | return compute_overall_effect_of_inner_loop (loop, res); |
512 | } | |
513 | } | |
514 | else | |
515 | return evolution_fn; | |
516 | } | |
b8698a0f | 517 | |
9baba81b SP |
518 | /* If the evolution function is an invariant, there is nothing to do. */ |
519 | else if (no_evolution_in_loop_p (evolution_fn, loop->num, &val) && val) | |
520 | return evolution_fn; | |
b8698a0f | 521 | |
9baba81b SP |
522 | else |
523 | return chrec_dont_know; | |
524 | } | |
525 | ||
9baba81b SP |
526 | /* Associate CHREC to SCALAR. */ |
527 | ||
528 | static void | |
a213b219 | 529 | set_scalar_evolution (basic_block instantiated_below, tree scalar, tree chrec) |
9baba81b SP |
530 | { |
531 | tree *scalar_info; | |
b8698a0f | 532 | |
9baba81b SP |
533 | if (TREE_CODE (scalar) != SSA_NAME) |
534 | return; | |
535 | ||
a213b219 | 536 | scalar_info = find_var_scev_info (instantiated_below, scalar); |
b8698a0f | 537 | |
9baba81b SP |
538 | if (dump_file) |
539 | { | |
dfedbe40 | 540 | if (dump_flags & TDF_SCEV) |
9baba81b SP |
541 | { |
542 | fprintf (dump_file, "(set_scalar_evolution \n"); | |
a213b219 SP |
543 | fprintf (dump_file, " instantiated_below = %d \n", |
544 | instantiated_below->index); | |
9baba81b SP |
545 | fprintf (dump_file, " (scalar = "); |
546 | print_generic_expr (dump_file, scalar, 0); | |
547 | fprintf (dump_file, ")\n (scalar_evolution = "); | |
548 | print_generic_expr (dump_file, chrec, 0); | |
549 | fprintf (dump_file, "))\n"); | |
550 | } | |
551 | if (dump_flags & TDF_STATS) | |
552 | nb_set_scev++; | |
553 | } | |
b8698a0f | 554 | |
9baba81b SP |
555 | *scalar_info = chrec; |
556 | } | |
557 | ||
a213b219 SP |
558 | /* Retrieve the chrec associated to SCALAR instantiated below |
559 | INSTANTIATED_BELOW block. */ | |
9baba81b SP |
560 | |
561 | static tree | |
a213b219 | 562 | get_scalar_evolution (basic_block instantiated_below, tree scalar) |
9baba81b SP |
563 | { |
564 | tree res; | |
b8698a0f | 565 | |
9baba81b SP |
566 | if (dump_file) |
567 | { | |
dfedbe40 | 568 | if (dump_flags & TDF_SCEV) |
9baba81b SP |
569 | { |
570 | fprintf (dump_file, "(get_scalar_evolution \n"); | |
571 | fprintf (dump_file, " (scalar = "); | |
572 | print_generic_expr (dump_file, scalar, 0); | |
573 | fprintf (dump_file, ")\n"); | |
574 | } | |
575 | if (dump_flags & TDF_STATS) | |
576 | nb_get_scev++; | |
577 | } | |
b8698a0f | 578 | |
9baba81b SP |
579 | switch (TREE_CODE (scalar)) |
580 | { | |
581 | case SSA_NAME: | |
a213b219 | 582 | res = *find_var_scev_info (instantiated_below, scalar); |
9baba81b SP |
583 | break; |
584 | ||
585 | case REAL_CST: | |
325217ed | 586 | case FIXED_CST: |
9baba81b SP |
587 | case INTEGER_CST: |
588 | res = scalar; | |
589 | break; | |
590 | ||
591 | default: | |
592 | res = chrec_not_analyzed_yet; | |
593 | break; | |
594 | } | |
b8698a0f | 595 | |
dfedbe40 | 596 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
597 | { |
598 | fprintf (dump_file, " (scalar_evolution = "); | |
599 | print_generic_expr (dump_file, res, 0); | |
600 | fprintf (dump_file, "))\n"); | |
601 | } | |
b8698a0f | 602 | |
9baba81b SP |
603 | return res; |
604 | } | |
605 | ||
606 | /* Helper function for add_to_evolution. Returns the evolution | |
607 | function for an assignment of the form "a = b + c", where "a" and | |
608 | "b" are on the strongly connected component. CHREC_BEFORE is the | |
609 | information that we already have collected up to this point. | |
b8698a0f L |
610 | TO_ADD is the evolution of "c". |
611 | ||
9baba81b SP |
612 | When CHREC_BEFORE has an evolution part in LOOP_NB, add to this |
613 | evolution the expression TO_ADD, otherwise construct an evolution | |
614 | part for this loop. */ | |
615 | ||
616 | static tree | |
e2157b49 | 617 | add_to_evolution_1 (unsigned loop_nb, tree chrec_before, tree to_add, |
355fe088 | 618 | gimple *at_stmt) |
9baba81b | 619 | { |
e2157b49 | 620 | tree type, left, right; |
0fc822d0 | 621 | struct loop *loop = get_loop (cfun, loop_nb), *chloop; |
e2157b49 | 622 | |
9baba81b SP |
623 | switch (TREE_CODE (chrec_before)) |
624 | { | |
625 | case POLYNOMIAL_CHREC: | |
677cc14d ZD |
626 | chloop = get_chrec_loop (chrec_before); |
627 | if (chloop == loop | |
628 | || flow_loop_nested_p (chloop, loop)) | |
9baba81b SP |
629 | { |
630 | unsigned var; | |
e2157b49 SP |
631 | |
632 | type = chrec_type (chrec_before); | |
b8698a0f | 633 | |
9baba81b | 634 | /* When there is no evolution part in this loop, build it. */ |
677cc14d | 635 | if (chloop != loop) |
9baba81b SP |
636 | { |
637 | var = loop_nb; | |
638 | left = chrec_before; | |
7e0923cd SP |
639 | right = SCALAR_FLOAT_TYPE_P (type) |
640 | ? build_real (type, dconst0) | |
641 | : build_int_cst (type, 0); | |
9baba81b SP |
642 | } |
643 | else | |
644 | { | |
645 | var = CHREC_VARIABLE (chrec_before); | |
646 | left = CHREC_LEFT (chrec_before); | |
647 | right = CHREC_RIGHT (chrec_before); | |
648 | } | |
649 | ||
e2157b49 | 650 | to_add = chrec_convert (type, to_add, at_stmt); |
5be014d5 AP |
651 | right = chrec_convert_rhs (type, right, at_stmt); |
652 | right = chrec_fold_plus (chrec_type (right), right, to_add); | |
e2157b49 | 653 | return build_polynomial_chrec (var, left, right); |
9baba81b SP |
654 | } |
655 | else | |
e2157b49 | 656 | { |
677cc14d ZD |
657 | gcc_assert (flow_loop_nested_p (loop, chloop)); |
658 | ||
e2157b49 SP |
659 | /* Search the evolution in LOOP_NB. */ |
660 | left = add_to_evolution_1 (loop_nb, CHREC_LEFT (chrec_before), | |
661 | to_add, at_stmt); | |
662 | right = CHREC_RIGHT (chrec_before); | |
5be014d5 | 663 | right = chrec_convert_rhs (chrec_type (left), right, at_stmt); |
e2157b49 SP |
664 | return build_polynomial_chrec (CHREC_VARIABLE (chrec_before), |
665 | left, right); | |
666 | } | |
b8698a0f | 667 | |
9baba81b SP |
668 | default: |
669 | /* These nodes do not depend on a loop. */ | |
670 | if (chrec_before == chrec_dont_know) | |
671 | return chrec_dont_know; | |
e2157b49 SP |
672 | |
673 | left = chrec_before; | |
5be014d5 | 674 | right = chrec_convert_rhs (chrec_type (left), to_add, at_stmt); |
e2157b49 | 675 | return build_polynomial_chrec (loop_nb, left, right); |
9baba81b SP |
676 | } |
677 | } | |
678 | ||
679 | /* Add TO_ADD to the evolution part of CHREC_BEFORE in the dimension | |
b8698a0f L |
680 | of LOOP_NB. |
681 | ||
9baba81b SP |
682 | Description (provided for completeness, for those who read code in |
683 | a plane, and for my poor 62 bytes brain that would have forgotten | |
684 | all this in the next two or three months): | |
b8698a0f | 685 | |
9baba81b SP |
686 | The algorithm of translation of programs from the SSA representation |
687 | into the chrecs syntax is based on a pattern matching. After having | |
688 | reconstructed the overall tree expression for a loop, there are only | |
689 | two cases that can arise: | |
b8698a0f | 690 | |
9baba81b SP |
691 | 1. a = loop-phi (init, a + expr) |
692 | 2. a = loop-phi (init, expr) | |
b8698a0f | 693 | |
9baba81b SP |
694 | where EXPR is either a scalar constant with respect to the analyzed |
695 | loop (this is a degree 0 polynomial), or an expression containing | |
696 | other loop-phi definitions (these are higher degree polynomials). | |
b8698a0f | 697 | |
9baba81b | 698 | Examples: |
b8698a0f L |
699 | |
700 | 1. | |
9baba81b SP |
701 | | init = ... |
702 | | loop_1 | |
703 | | a = phi (init, a + 5) | |
704 | | endloop | |
b8698a0f L |
705 | |
706 | 2. | |
9baba81b SP |
707 | | inita = ... |
708 | | initb = ... | |
709 | | loop_1 | |
710 | | a = phi (inita, 2 * b + 3) | |
711 | | b = phi (initb, b + 1) | |
712 | | endloop | |
b8698a0f L |
713 | |
714 | For the first case, the semantics of the SSA representation is: | |
715 | ||
9baba81b | 716 | | a (x) = init + \sum_{j = 0}^{x - 1} expr (j) |
b8698a0f | 717 | |
9baba81b SP |
718 | that is, there is a loop index "x" that determines the scalar value |
719 | of the variable during the loop execution. During the first | |
720 | iteration, the value is that of the initial condition INIT, while | |
721 | during the subsequent iterations, it is the sum of the initial | |
722 | condition with the sum of all the values of EXPR from the initial | |
b8698a0f L |
723 | iteration to the before last considered iteration. |
724 | ||
9baba81b | 725 | For the second case, the semantics of the SSA program is: |
b8698a0f | 726 | |
9baba81b SP |
727 | | a (x) = init, if x = 0; |
728 | | expr (x - 1), otherwise. | |
b8698a0f | 729 | |
9baba81b | 730 | The second case corresponds to the PEELED_CHREC, whose syntax is |
b8698a0f L |
731 | close to the syntax of a loop-phi-node: |
732 | ||
9baba81b | 733 | | phi (init, expr) vs. (init, expr)_x |
b8698a0f | 734 | |
9baba81b | 735 | The proof of the translation algorithm for the first case is a |
b8698a0f L |
736 | proof by structural induction based on the degree of EXPR. |
737 | ||
9baba81b SP |
738 | Degree 0: |
739 | When EXPR is a constant with respect to the analyzed loop, or in | |
740 | other words when EXPR is a polynomial of degree 0, the evolution of | |
741 | the variable A in the loop is an affine function with an initial | |
742 | condition INIT, and a step EXPR. In order to show this, we start | |
743 | from the semantics of the SSA representation: | |
b8698a0f | 744 | |
9baba81b | 745 | f (x) = init + \sum_{j = 0}^{x - 1} expr (j) |
b8698a0f | 746 | |
9baba81b | 747 | and since "expr (j)" is a constant with respect to "j", |
b8698a0f L |
748 | |
749 | f (x) = init + x * expr | |
750 | ||
9baba81b SP |
751 | Finally, based on the semantics of the pure sum chrecs, by |
752 | identification we get the corresponding chrecs syntax: | |
b8698a0f L |
753 | |
754 | f (x) = init * \binom{x}{0} + expr * \binom{x}{1} | |
9baba81b | 755 | f (x) -> {init, +, expr}_x |
b8698a0f | 756 | |
9baba81b SP |
757 | Higher degree: |
758 | Suppose that EXPR is a polynomial of degree N with respect to the | |
759 | analyzed loop_x for which we have already determined that it is | |
760 | written under the chrecs syntax: | |
b8698a0f | 761 | |
9baba81b | 762 | | expr (x) -> {b_0, +, b_1, +, ..., +, b_{n-1}} (x) |
b8698a0f | 763 | |
9baba81b | 764 | We start from the semantics of the SSA program: |
b8698a0f | 765 | |
9baba81b SP |
766 | | f (x) = init + \sum_{j = 0}^{x - 1} expr (j) |
767 | | | |
b8698a0f | 768 | | f (x) = init + \sum_{j = 0}^{x - 1} |
9baba81b SP |
769 | | (b_0 * \binom{j}{0} + ... + b_{n-1} * \binom{j}{n-1}) |
770 | | | |
b8698a0f L |
771 | | f (x) = init + \sum_{j = 0}^{x - 1} |
772 | | \sum_{k = 0}^{n - 1} (b_k * \binom{j}{k}) | |
9baba81b | 773 | | |
b8698a0f L |
774 | | f (x) = init + \sum_{k = 0}^{n - 1} |
775 | | (b_k * \sum_{j = 0}^{x - 1} \binom{j}{k}) | |
9baba81b | 776 | | |
b8698a0f L |
777 | | f (x) = init + \sum_{k = 0}^{n - 1} |
778 | | (b_k * \binom{x}{k + 1}) | |
9baba81b | 779 | | |
b8698a0f L |
780 | | f (x) = init + b_0 * \binom{x}{1} + ... |
781 | | + b_{n-1} * \binom{x}{n} | |
9baba81b | 782 | | |
b8698a0f L |
783 | | f (x) = init * \binom{x}{0} + b_0 * \binom{x}{1} + ... |
784 | | + b_{n-1} * \binom{x}{n} | |
9baba81b | 785 | | |
b8698a0f | 786 | |
9baba81b | 787 | And finally from the definition of the chrecs syntax, we identify: |
b8698a0f L |
788 | | f (x) -> {init, +, b_0, +, ..., +, b_{n-1}}_x |
789 | ||
9baba81b SP |
790 | This shows the mechanism that stands behind the add_to_evolution |
791 | function. An important point is that the use of symbolic | |
792 | parameters avoids the need of an analysis schedule. | |
b8698a0f | 793 | |
9baba81b | 794 | Example: |
b8698a0f | 795 | |
9baba81b SP |
796 | | inita = ... |
797 | | initb = ... | |
b8698a0f | 798 | | loop_1 |
9baba81b SP |
799 | | a = phi (inita, a + 2 + b) |
800 | | b = phi (initb, b + 1) | |
801 | | endloop | |
b8698a0f | 802 | |
9baba81b | 803 | When analyzing "a", the algorithm keeps "b" symbolically: |
b8698a0f | 804 | |
9baba81b | 805 | | a -> {inita, +, 2 + b}_1 |
b8698a0f | 806 | |
9baba81b | 807 | Then, after instantiation, the analyzer ends on the evolution: |
b8698a0f | 808 | |
9baba81b SP |
809 | | a -> {inita, +, 2 + initb, +, 1}_1 |
810 | ||
811 | */ | |
812 | ||
b8698a0f | 813 | static tree |
e2157b49 | 814 | add_to_evolution (unsigned loop_nb, tree chrec_before, enum tree_code code, |
355fe088 | 815 | tree to_add, gimple *at_stmt) |
9baba81b SP |
816 | { |
817 | tree type = chrec_type (to_add); | |
818 | tree res = NULL_TREE; | |
b8698a0f | 819 | |
9baba81b SP |
820 | if (to_add == NULL_TREE) |
821 | return chrec_before; | |
b8698a0f | 822 | |
9baba81b SP |
823 | /* TO_ADD is either a scalar, or a parameter. TO_ADD is not |
824 | instantiated at this point. */ | |
825 | if (TREE_CODE (to_add) == POLYNOMIAL_CHREC) | |
826 | /* This should not happen. */ | |
827 | return chrec_dont_know; | |
b8698a0f | 828 | |
dfedbe40 | 829 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
830 | { |
831 | fprintf (dump_file, "(add_to_evolution \n"); | |
832 | fprintf (dump_file, " (loop_nb = %d)\n", loop_nb); | |
833 | fprintf (dump_file, " (chrec_before = "); | |
834 | print_generic_expr (dump_file, chrec_before, 0); | |
835 | fprintf (dump_file, ")\n (to_add = "); | |
836 | print_generic_expr (dump_file, to_add, 0); | |
837 | fprintf (dump_file, ")\n"); | |
838 | } | |
839 | ||
840 | if (code == MINUS_EXPR) | |
9d2b0e12 VR |
841 | to_add = chrec_fold_multiply (type, to_add, SCALAR_FLOAT_TYPE_P (type) |
842 | ? build_real (type, dconstm1) | |
843 | : build_int_cst_type (type, -1)); | |
9baba81b | 844 | |
e2157b49 | 845 | res = add_to_evolution_1 (loop_nb, chrec_before, to_add, at_stmt); |
9baba81b | 846 | |
dfedbe40 | 847 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
848 | { |
849 | fprintf (dump_file, " (res = "); | |
850 | print_generic_expr (dump_file, res, 0); | |
851 | fprintf (dump_file, "))\n"); | |
852 | } | |
853 | ||
854 | return res; | |
855 | } | |
856 | ||
9baba81b SP |
857 | \f |
858 | ||
859 | /* This section selects the loops that will be good candidates for the | |
860 | scalar evolution analysis. For the moment, greedily select all the | |
861 | loop nests we could analyze. */ | |
862 | ||
9baba81b SP |
863 | /* For a loop with a single exit edge, return the COND_EXPR that |
864 | guards the exit edge. If the expression is too difficult to | |
865 | analyze, then give up. */ | |
866 | ||
538dd0b7 | 867 | gcond * |
22ea9ec0 | 868 | get_loop_exit_condition (const struct loop *loop) |
9baba81b | 869 | { |
538dd0b7 | 870 | gcond *res = NULL; |
ac8f6c69 | 871 | edge exit_edge = single_exit (loop); |
b8698a0f | 872 | |
dfedbe40 | 873 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b | 874 | fprintf (dump_file, "(get_loop_exit_condition \n "); |
b8698a0f | 875 | |
82b85a85 | 876 | if (exit_edge) |
9baba81b | 877 | { |
355fe088 | 878 | gimple *stmt; |
b8698a0f | 879 | |
726a989a | 880 | stmt = last_stmt (exit_edge->src); |
538dd0b7 DM |
881 | if (gcond *cond_stmt = dyn_cast <gcond *> (stmt)) |
882 | res = cond_stmt; | |
9baba81b | 883 | } |
b8698a0f | 884 | |
dfedbe40 | 885 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b | 886 | { |
726a989a | 887 | print_gimple_stmt (dump_file, res, 0, 0); |
9baba81b SP |
888 | fprintf (dump_file, ")\n"); |
889 | } | |
b8698a0f | 890 | |
9baba81b SP |
891 | return res; |
892 | } | |
893 | ||
9baba81b SP |
894 | \f |
895 | /* Depth first search algorithm. */ | |
896 | ||
a79683d5 | 897 | enum t_bool { |
c59dabbe SP |
898 | t_false, |
899 | t_true, | |
900 | t_dont_know | |
a79683d5 | 901 | }; |
c59dabbe SP |
902 | |
903 | ||
355fe088 | 904 | static t_bool follow_ssa_edge (struct loop *loop, gimple *, gphi *, |
538dd0b7 | 905 | tree *, int); |
9baba81b | 906 | |
726a989a | 907 | /* Follow the ssa edge into the binary expression RHS0 CODE RHS1. |
9baba81b SP |
908 | Return true if the strongly connected component has been found. */ |
909 | ||
c59dabbe | 910 | static t_bool |
355fe088 | 911 | follow_ssa_edge_binary (struct loop *loop, gimple *at_stmt, |
726a989a | 912 | tree type, tree rhs0, enum tree_code code, tree rhs1, |
538dd0b7 DM |
913 | gphi *halting_phi, tree *evolution_of_loop, |
914 | int limit) | |
9baba81b | 915 | { |
c59dabbe | 916 | t_bool res = t_false; |
b2a93c0a | 917 | tree evol; |
726a989a | 918 | |
5be014d5 | 919 | switch (code) |
9baba81b | 920 | { |
5be014d5 | 921 | case POINTER_PLUS_EXPR: |
9baba81b | 922 | case PLUS_EXPR: |
9baba81b SP |
923 | if (TREE_CODE (rhs0) == SSA_NAME) |
924 | { | |
925 | if (TREE_CODE (rhs1) == SSA_NAME) | |
926 | { | |
b8698a0f | 927 | /* Match an assignment under the form: |
9baba81b | 928 | "a = b + c". */ |
b8698a0f | 929 | |
9e824336 ZD |
930 | /* We want only assignments of form "name + name" contribute to |
931 | LIMIT, as the other cases do not necessarily contribute to | |
932 | the complexity of the expression. */ | |
933 | limit++; | |
934 | ||
b2a93c0a | 935 | evol = *evolution_of_loop; |
b9b79ba4 | 936 | evol = add_to_evolution |
b8698a0f L |
937 | (loop->num, |
938 | chrec_convert (type, evol, at_stmt), | |
5be014d5 | 939 | code, rhs1, at_stmt); |
b9b79ba4 RB |
940 | res = follow_ssa_edge |
941 | (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, &evol, limit); | |
942 | if (res == t_true) | |
943 | *evolution_of_loop = evol; | |
c59dabbe | 944 | else if (res == t_false) |
9baba81b | 945 | { |
b9b79ba4 RB |
946 | *evolution_of_loop = add_to_evolution |
947 | (loop->num, | |
948 | chrec_convert (type, *evolution_of_loop, at_stmt), | |
949 | code, rhs0, at_stmt); | |
b8698a0f L |
950 | res = follow_ssa_edge |
951 | (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi, | |
c59dabbe | 952 | evolution_of_loop, limit); |
c59dabbe | 953 | if (res == t_true) |
b9b79ba4 | 954 | ; |
c59dabbe SP |
955 | else if (res == t_dont_know) |
956 | *evolution_of_loop = chrec_dont_know; | |
9baba81b | 957 | } |
c59dabbe SP |
958 | |
959 | else if (res == t_dont_know) | |
960 | *evolution_of_loop = chrec_dont_know; | |
9baba81b | 961 | } |
b8698a0f | 962 | |
9baba81b SP |
963 | else |
964 | { | |
b8698a0f | 965 | /* Match an assignment under the form: |
9baba81b | 966 | "a = b + ...". */ |
b9b79ba4 RB |
967 | *evolution_of_loop = add_to_evolution |
968 | (loop->num, chrec_convert (type, *evolution_of_loop, | |
969 | at_stmt), | |
970 | code, rhs1, at_stmt); | |
b8698a0f L |
971 | res = follow_ssa_edge |
972 | (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, | |
c59dabbe SP |
973 | evolution_of_loop, limit); |
974 | if (res == t_true) | |
b9b79ba4 | 975 | ; |
c59dabbe SP |
976 | else if (res == t_dont_know) |
977 | *evolution_of_loop = chrec_dont_know; | |
9baba81b SP |
978 | } |
979 | } | |
b8698a0f | 980 | |
9baba81b SP |
981 | else if (TREE_CODE (rhs1) == SSA_NAME) |
982 | { | |
b8698a0f | 983 | /* Match an assignment under the form: |
9baba81b | 984 | "a = ... + c". */ |
b9b79ba4 RB |
985 | *evolution_of_loop = add_to_evolution |
986 | (loop->num, chrec_convert (type, *evolution_of_loop, | |
987 | at_stmt), | |
988 | code, rhs0, at_stmt); | |
b8698a0f L |
989 | res = follow_ssa_edge |
990 | (loop, SSA_NAME_DEF_STMT (rhs1), halting_phi, | |
c59dabbe SP |
991 | evolution_of_loop, limit); |
992 | if (res == t_true) | |
b9b79ba4 | 993 | ; |
c59dabbe SP |
994 | else if (res == t_dont_know) |
995 | *evolution_of_loop = chrec_dont_know; | |
9baba81b SP |
996 | } |
997 | ||
998 | else | |
b8698a0f | 999 | /* Otherwise, match an assignment under the form: |
9baba81b SP |
1000 | "a = ... + ...". */ |
1001 | /* And there is nothing to do. */ | |
c59dabbe | 1002 | res = t_false; |
9baba81b | 1003 | break; |
b8698a0f | 1004 | |
9baba81b SP |
1005 | case MINUS_EXPR: |
1006 | /* This case is under the form "opnd0 = rhs0 - rhs1". */ | |
9baba81b | 1007 | if (TREE_CODE (rhs0) == SSA_NAME) |
9baba81b | 1008 | { |
b8698a0f | 1009 | /* Match an assignment under the form: |
f8e9d512 | 1010 | "a = b - ...". */ |
9e824336 ZD |
1011 | |
1012 | /* We want only assignments of form "name - name" contribute to | |
1013 | LIMIT, as the other cases do not necessarily contribute to | |
1014 | the complexity of the expression. */ | |
1015 | if (TREE_CODE (rhs1) == SSA_NAME) | |
1016 | limit++; | |
1017 | ||
b9b79ba4 RB |
1018 | *evolution_of_loop = add_to_evolution |
1019 | (loop->num, chrec_convert (type, *evolution_of_loop, at_stmt), | |
1020 | MINUS_EXPR, rhs1, at_stmt); | |
b8698a0f | 1021 | res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), halting_phi, |
c59dabbe SP |
1022 | evolution_of_loop, limit); |
1023 | if (res == t_true) | |
b9b79ba4 | 1024 | ; |
c59dabbe SP |
1025 | else if (res == t_dont_know) |
1026 | *evolution_of_loop = chrec_dont_know; | |
9baba81b | 1027 | } |
9baba81b | 1028 | else |
b8698a0f | 1029 | /* Otherwise, match an assignment under the form: |
9baba81b SP |
1030 | "a = ... - ...". */ |
1031 | /* And there is nothing to do. */ | |
c59dabbe | 1032 | res = t_false; |
9baba81b | 1033 | break; |
726a989a RB |
1034 | |
1035 | default: | |
1036 | res = t_false; | |
1037 | } | |
1038 | ||
1039 | return res; | |
1040 | } | |
b8698a0f | 1041 | |
726a989a RB |
1042 | /* Follow the ssa edge into the expression EXPR. |
1043 | Return true if the strongly connected component has been found. */ | |
1044 | ||
1045 | static t_bool | |
355fe088 | 1046 | follow_ssa_edge_expr (struct loop *loop, gimple *at_stmt, tree expr, |
538dd0b7 DM |
1047 | gphi *halting_phi, tree *evolution_of_loop, |
1048 | int limit) | |
726a989a | 1049 | { |
5aefc6a0 EB |
1050 | enum tree_code code = TREE_CODE (expr); |
1051 | tree type = TREE_TYPE (expr), rhs0, rhs1; | |
1052 | t_bool res; | |
1053 | ||
726a989a | 1054 | /* The EXPR is one of the following cases: |
b8698a0f | 1055 | - an SSA_NAME, |
726a989a | 1056 | - an INTEGER_CST, |
b8698a0f L |
1057 | - a PLUS_EXPR, |
1058 | - a POINTER_PLUS_EXPR, | |
726a989a RB |
1059 | - a MINUS_EXPR, |
1060 | - an ASSERT_EXPR, | |
1061 | - other cases are not yet handled. */ | |
5aefc6a0 | 1062 | |
726a989a RB |
1063 | switch (code) |
1064 | { | |
5aefc6a0 | 1065 | CASE_CONVERT: |
726a989a RB |
1066 | /* This assignment is under the form "a_1 = (cast) rhs. */ |
1067 | res = follow_ssa_edge_expr (loop, at_stmt, TREE_OPERAND (expr, 0), | |
1068 | halting_phi, evolution_of_loop, limit); | |
1069 | *evolution_of_loop = chrec_convert (type, *evolution_of_loop, at_stmt); | |
1070 | break; | |
1071 | ||
1072 | case INTEGER_CST: | |
1073 | /* This assignment is under the form "a_1 = 7". */ | |
1074 | res = t_false; | |
1075 | break; | |
5aefc6a0 | 1076 | |
726a989a RB |
1077 | case SSA_NAME: |
1078 | /* This assignment is under the form: "a_1 = b_2". */ | |
b8698a0f | 1079 | res = follow_ssa_edge |
726a989a RB |
1080 | (loop, SSA_NAME_DEF_STMT (expr), halting_phi, evolution_of_loop, limit); |
1081 | break; | |
5aefc6a0 | 1082 | |
726a989a RB |
1083 | case POINTER_PLUS_EXPR: |
1084 | case PLUS_EXPR: | |
1085 | case MINUS_EXPR: | |
1086 | /* This case is under the form "rhs0 +- rhs1". */ | |
1087 | rhs0 = TREE_OPERAND (expr, 0); | |
1088 | rhs1 = TREE_OPERAND (expr, 1); | |
5aefc6a0 EB |
1089 | type = TREE_TYPE (rhs0); |
1090 | STRIP_USELESS_TYPE_CONVERSION (rhs0); | |
1091 | STRIP_USELESS_TYPE_CONVERSION (rhs1); | |
1092 | res = follow_ssa_edge_binary (loop, at_stmt, type, rhs0, code, rhs1, | |
1093 | halting_phi, evolution_of_loop, limit); | |
1094 | break; | |
726a989a | 1095 | |
70f34814 RG |
1096 | case ADDR_EXPR: |
1097 | /* Handle &MEM[ptr + CST] which is equivalent to POINTER_PLUS_EXPR. */ | |
1098 | if (TREE_CODE (TREE_OPERAND (expr, 0)) == MEM_REF) | |
1099 | { | |
1100 | expr = TREE_OPERAND (expr, 0); | |
1101 | rhs0 = TREE_OPERAND (expr, 0); | |
1102 | rhs1 = TREE_OPERAND (expr, 1); | |
1103 | type = TREE_TYPE (rhs0); | |
1104 | STRIP_USELESS_TYPE_CONVERSION (rhs0); | |
1105 | STRIP_USELESS_TYPE_CONVERSION (rhs1); | |
1106 | res = follow_ssa_edge_binary (loop, at_stmt, type, | |
1107 | rhs0, POINTER_PLUS_EXPR, rhs1, | |
1108 | halting_phi, evolution_of_loop, limit); | |
1109 | } | |
1110 | else | |
1111 | res = t_false; | |
1112 | break; | |
1113 | ||
0bca51f0 | 1114 | case ASSERT_EXPR: |
5aefc6a0 EB |
1115 | /* This assignment is of the form: "a_1 = ASSERT_EXPR <a_2, ...>" |
1116 | It must be handled as a copy assignment of the form a_1 = a_2. */ | |
1117 | rhs0 = ASSERT_EXPR_VAR (expr); | |
1118 | if (TREE_CODE (rhs0) == SSA_NAME) | |
1119 | res = follow_ssa_edge (loop, SSA_NAME_DEF_STMT (rhs0), | |
1120 | halting_phi, evolution_of_loop, limit); | |
1121 | else | |
1122 | res = t_false; | |
1123 | break; | |
0bca51f0 | 1124 | |
9baba81b | 1125 | default: |
c59dabbe | 1126 | res = t_false; |
9baba81b SP |
1127 | break; |
1128 | } | |
5aefc6a0 | 1129 | |
9baba81b SP |
1130 | return res; |
1131 | } | |
1132 | ||
726a989a RB |
1133 | /* Follow the ssa edge into the right hand side of an assignment STMT. |
1134 | Return true if the strongly connected component has been found. */ | |
1135 | ||
1136 | static t_bool | |
355fe088 | 1137 | follow_ssa_edge_in_rhs (struct loop *loop, gimple *stmt, |
538dd0b7 DM |
1138 | gphi *halting_phi, tree *evolution_of_loop, |
1139 | int limit) | |
726a989a | 1140 | { |
726a989a | 1141 | enum tree_code code = gimple_assign_rhs_code (stmt); |
5aefc6a0 EB |
1142 | tree type = gimple_expr_type (stmt), rhs1, rhs2; |
1143 | t_bool res; | |
726a989a | 1144 | |
5aefc6a0 | 1145 | switch (code) |
726a989a | 1146 | { |
5aefc6a0 EB |
1147 | CASE_CONVERT: |
1148 | /* This assignment is under the form "a_1 = (cast) rhs. */ | |
1149 | res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt), | |
1150 | halting_phi, evolution_of_loop, limit); | |
1151 | *evolution_of_loop = chrec_convert (type, *evolution_of_loop, stmt); | |
1152 | break; | |
1153 | ||
1154 | case POINTER_PLUS_EXPR: | |
1155 | case PLUS_EXPR: | |
1156 | case MINUS_EXPR: | |
1157 | rhs1 = gimple_assign_rhs1 (stmt); | |
1158 | rhs2 = gimple_assign_rhs2 (stmt); | |
1159 | type = TREE_TYPE (rhs1); | |
1160 | res = follow_ssa_edge_binary (loop, stmt, type, rhs1, code, rhs2, | |
218d1c24 | 1161 | halting_phi, evolution_of_loop, limit); |
5aefc6a0 | 1162 | break; |
218d1c24 | 1163 | |
726a989a | 1164 | default: |
5aefc6a0 EB |
1165 | if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) |
1166 | res = follow_ssa_edge_expr (loop, stmt, gimple_assign_rhs1 (stmt), | |
1167 | halting_phi, evolution_of_loop, limit); | |
1168 | else | |
1169 | res = t_false; | |
1170 | break; | |
726a989a | 1171 | } |
5aefc6a0 EB |
1172 | |
1173 | return res; | |
726a989a RB |
1174 | } |
1175 | ||
9baba81b SP |
1176 | /* Checks whether the I-th argument of a PHI comes from a backedge. */ |
1177 | ||
1178 | static bool | |
538dd0b7 | 1179 | backedge_phi_arg_p (gphi *phi, int i) |
9baba81b | 1180 | { |
726a989a | 1181 | const_edge e = gimple_phi_arg_edge (phi, i); |
9baba81b SP |
1182 | |
1183 | /* We would in fact like to test EDGE_DFS_BACK here, but we do not care | |
1184 | about updating it anywhere, and this should work as well most of the | |
1185 | time. */ | |
1186 | if (e->flags & EDGE_IRREDUCIBLE_LOOP) | |
1187 | return true; | |
1188 | ||
1189 | return false; | |
1190 | } | |
1191 | ||
1192 | /* Helper function for one branch of the condition-phi-node. Return | |
1193 | true if the strongly connected component has been found following | |
1194 | this path. */ | |
1195 | ||
c59dabbe | 1196 | static inline t_bool |
9baba81b | 1197 | follow_ssa_edge_in_condition_phi_branch (int i, |
b8698a0f | 1198 | struct loop *loop, |
538dd0b7 DM |
1199 | gphi *condition_phi, |
1200 | gphi *halting_phi, | |
9baba81b | 1201 | tree *evolution_of_branch, |
c59dabbe | 1202 | tree init_cond, int limit) |
9baba81b SP |
1203 | { |
1204 | tree branch = PHI_ARG_DEF (condition_phi, i); | |
1205 | *evolution_of_branch = chrec_dont_know; | |
1206 | ||
1207 | /* Do not follow back edges (they must belong to an irreducible loop, which | |
1208 | we really do not want to worry about). */ | |
1209 | if (backedge_phi_arg_p (condition_phi, i)) | |
c59dabbe | 1210 | return t_false; |
9baba81b SP |
1211 | |
1212 | if (TREE_CODE (branch) == SSA_NAME) | |
1213 | { | |
1214 | *evolution_of_branch = init_cond; | |
b8698a0f | 1215 | return follow_ssa_edge (loop, SSA_NAME_DEF_STMT (branch), halting_phi, |
c59dabbe | 1216 | evolution_of_branch, limit); |
9baba81b SP |
1217 | } |
1218 | ||
b8698a0f | 1219 | /* This case occurs when one of the condition branches sets |
89dbed81 | 1220 | the variable to a constant: i.e. a phi-node like |
b8698a0f L |
1221 | "a_2 = PHI <a_7(5), 2(6)>;". |
1222 | ||
1223 | FIXME: This case have to be refined correctly: | |
9baba81b SP |
1224 | in some cases it is possible to say something better than |
1225 | chrec_dont_know, for example using a wrap-around notation. */ | |
c59dabbe | 1226 | return t_false; |
9baba81b SP |
1227 | } |
1228 | ||
1229 | /* This function merges the branches of a condition-phi-node in a | |
1230 | loop. */ | |
1231 | ||
c59dabbe | 1232 | static t_bool |
9baba81b | 1233 | follow_ssa_edge_in_condition_phi (struct loop *loop, |
538dd0b7 DM |
1234 | gphi *condition_phi, |
1235 | gphi *halting_phi, | |
c59dabbe | 1236 | tree *evolution_of_loop, int limit) |
9baba81b | 1237 | { |
726a989a | 1238 | int i, n; |
9baba81b SP |
1239 | tree init = *evolution_of_loop; |
1240 | tree evolution_of_branch; | |
c59dabbe SP |
1241 | t_bool res = follow_ssa_edge_in_condition_phi_branch (0, loop, condition_phi, |
1242 | halting_phi, | |
1243 | &evolution_of_branch, | |
1244 | init, limit); | |
1245 | if (res == t_false || res == t_dont_know) | |
1246 | return res; | |
9baba81b | 1247 | |
9baba81b SP |
1248 | *evolution_of_loop = evolution_of_branch; |
1249 | ||
726a989a | 1250 | n = gimple_phi_num_args (condition_phi); |
726a989a | 1251 | for (i = 1; i < n; i++) |
9baba81b | 1252 | { |
e0afb98a SP |
1253 | /* Quickly give up when the evolution of one of the branches is |
1254 | not known. */ | |
1255 | if (*evolution_of_loop == chrec_dont_know) | |
c59dabbe | 1256 | return t_true; |
e0afb98a | 1257 | |
788d3075 RG |
1258 | /* Increase the limit by the PHI argument number to avoid exponential |
1259 | time and memory complexity. */ | |
c59dabbe SP |
1260 | res = follow_ssa_edge_in_condition_phi_branch (i, loop, condition_phi, |
1261 | halting_phi, | |
1262 | &evolution_of_branch, | |
788d3075 | 1263 | init, limit + i); |
c59dabbe SP |
1264 | if (res == t_false || res == t_dont_know) |
1265 | return res; | |
9baba81b SP |
1266 | |
1267 | *evolution_of_loop = chrec_merge (*evolution_of_loop, | |
1268 | evolution_of_branch); | |
1269 | } | |
b8698a0f | 1270 | |
c59dabbe | 1271 | return t_true; |
9baba81b SP |
1272 | } |
1273 | ||
1274 | /* Follow an SSA edge in an inner loop. It computes the overall | |
1275 | effect of the loop, and following the symbolic initial conditions, | |
1276 | it follows the edges in the parent loop. The inner loop is | |
1277 | considered as a single statement. */ | |
1278 | ||
c59dabbe | 1279 | static t_bool |
9baba81b | 1280 | follow_ssa_edge_inner_loop_phi (struct loop *outer_loop, |
538dd0b7 DM |
1281 | gphi *loop_phi_node, |
1282 | gphi *halting_phi, | |
c59dabbe | 1283 | tree *evolution_of_loop, int limit) |
9baba81b SP |
1284 | { |
1285 | struct loop *loop = loop_containing_stmt (loop_phi_node); | |
1286 | tree ev = analyze_scalar_evolution (loop, PHI_RESULT (loop_phi_node)); | |
1287 | ||
1288 | /* Sometimes, the inner loop is too difficult to analyze, and the | |
1289 | result of the analysis is a symbolic parameter. */ | |
1290 | if (ev == PHI_RESULT (loop_phi_node)) | |
1291 | { | |
c59dabbe | 1292 | t_bool res = t_false; |
726a989a | 1293 | int i, n = gimple_phi_num_args (loop_phi_node); |
9baba81b | 1294 | |
726a989a | 1295 | for (i = 0; i < n; i++) |
9baba81b SP |
1296 | { |
1297 | tree arg = PHI_ARG_DEF (loop_phi_node, i); | |
1298 | basic_block bb; | |
1299 | ||
1300 | /* Follow the edges that exit the inner loop. */ | |
726a989a | 1301 | bb = gimple_phi_arg_edge (loop_phi_node, i)->src; |
9baba81b | 1302 | if (!flow_bb_inside_loop_p (loop, bb)) |
726a989a RB |
1303 | res = follow_ssa_edge_expr (outer_loop, loop_phi_node, |
1304 | arg, halting_phi, | |
1305 | evolution_of_loop, limit); | |
c59dabbe SP |
1306 | if (res == t_true) |
1307 | break; | |
9baba81b SP |
1308 | } |
1309 | ||
1310 | /* If the path crosses this loop-phi, give up. */ | |
c59dabbe | 1311 | if (res == t_true) |
9baba81b SP |
1312 | *evolution_of_loop = chrec_dont_know; |
1313 | ||
1314 | return res; | |
1315 | } | |
1316 | ||
1317 | /* Otherwise, compute the overall effect of the inner loop. */ | |
1318 | ev = compute_overall_effect_of_inner_loop (loop, ev); | |
726a989a RB |
1319 | return follow_ssa_edge_expr (outer_loop, loop_phi_node, ev, halting_phi, |
1320 | evolution_of_loop, limit); | |
9baba81b SP |
1321 | } |
1322 | ||
1323 | /* Follow an SSA edge from a loop-phi-node to itself, constructing a | |
1324 | path that is analyzed on the return walk. */ | |
1325 | ||
c59dabbe | 1326 | static t_bool |
355fe088 | 1327 | follow_ssa_edge (struct loop *loop, gimple *def, gphi *halting_phi, |
c59dabbe | 1328 | tree *evolution_of_loop, int limit) |
9baba81b SP |
1329 | { |
1330 | struct loop *def_loop; | |
b8698a0f | 1331 | |
726a989a | 1332 | if (gimple_nop_p (def)) |
c59dabbe | 1333 | return t_false; |
b8698a0f | 1334 | |
c59dabbe | 1335 | /* Give up if the path is longer than the MAX that we allow. */ |
14dd9aab | 1336 | if (limit > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_COMPLEXITY)) |
c59dabbe | 1337 | return t_dont_know; |
b8698a0f | 1338 | |
9baba81b | 1339 | def_loop = loop_containing_stmt (def); |
b8698a0f | 1340 | |
726a989a | 1341 | switch (gimple_code (def)) |
9baba81b | 1342 | { |
726a989a | 1343 | case GIMPLE_PHI: |
9baba81b SP |
1344 | if (!loop_phi_node_p (def)) |
1345 | /* DEF is a condition-phi-node. Follow the branches, and | |
1346 | record their evolutions. Finally, merge the collected | |
1347 | information and set the approximation to the main | |
1348 | variable. */ | |
b8698a0f | 1349 | return follow_ssa_edge_in_condition_phi |
538dd0b7 DM |
1350 | (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop, |
1351 | limit); | |
9baba81b SP |
1352 | |
1353 | /* When the analyzed phi is the halting_phi, the | |
1354 | depth-first search is over: we have found a path from | |
1355 | the halting_phi to itself in the loop. */ | |
1356 | if (def == halting_phi) | |
c59dabbe | 1357 | return t_true; |
b8698a0f | 1358 | |
9baba81b | 1359 | /* Otherwise, the evolution of the HALTING_PHI depends |
89dbed81 | 1360 | on the evolution of another loop-phi-node, i.e. the |
9baba81b SP |
1361 | evolution function is a higher degree polynomial. */ |
1362 | if (def_loop == loop) | |
c59dabbe | 1363 | return t_false; |
b8698a0f | 1364 | |
9baba81b SP |
1365 | /* Inner loop. */ |
1366 | if (flow_loop_nested_p (loop, def_loop)) | |
b8698a0f | 1367 | return follow_ssa_edge_inner_loop_phi |
538dd0b7 DM |
1368 | (loop, as_a <gphi *> (def), halting_phi, evolution_of_loop, |
1369 | limit + 1); | |
9baba81b SP |
1370 | |
1371 | /* Outer loop. */ | |
c59dabbe | 1372 | return t_false; |
9baba81b | 1373 | |
726a989a | 1374 | case GIMPLE_ASSIGN: |
b8698a0f | 1375 | return follow_ssa_edge_in_rhs (loop, def, halting_phi, |
c59dabbe | 1376 | evolution_of_loop, limit); |
b8698a0f | 1377 | |
9baba81b SP |
1378 | default: |
1379 | /* At this level of abstraction, the program is just a set | |
726a989a | 1380 | of GIMPLE_ASSIGNs and PHI_NODEs. In principle there is no |
9baba81b | 1381 | other node to be handled. */ |
c59dabbe | 1382 | return t_false; |
9baba81b SP |
1383 | } |
1384 | } | |
1385 | ||
1386 | \f | |
b83b5507 BC |
1387 | /* Simplify PEELED_CHREC represented by (init_cond, arg) in LOOP. |
1388 | Handle below case and return the corresponding POLYNOMIAL_CHREC: | |
1389 | ||
1390 | # i_17 = PHI <i_13(5), 0(3)> | |
1391 | # _20 = PHI <_5(5), start_4(D)(3)> | |
1392 | ... | |
1393 | i_13 = i_17 + 1; | |
1394 | _5 = start_4(D) + i_13; | |
1395 | ||
1396 | Though variable _20 appears as a PEELED_CHREC in the form of | |
1397 | (start_4, _5)_LOOP, it's a POLYNOMIAL_CHREC like {start_4, 1}_LOOP. | |
1398 | ||
1399 | See PR41488. */ | |
1400 | ||
1401 | static tree | |
1402 | simplify_peeled_chrec (struct loop *loop, tree arg, tree init_cond) | |
1403 | { | |
1404 | aff_tree aff1, aff2; | |
1405 | tree ev, left, right, type, step_val; | |
39c8aaa4 | 1406 | hash_map<tree, name_expansion *> *peeled_chrec_map = NULL; |
b83b5507 BC |
1407 | |
1408 | ev = instantiate_parameters (loop, analyze_scalar_evolution (loop, arg)); | |
1409 | if (ev == NULL_TREE || TREE_CODE (ev) != POLYNOMIAL_CHREC) | |
1410 | return chrec_dont_know; | |
1411 | ||
1412 | left = CHREC_LEFT (ev); | |
1413 | right = CHREC_RIGHT (ev); | |
1414 | type = TREE_TYPE (left); | |
1415 | step_val = chrec_fold_plus (type, init_cond, right); | |
1416 | ||
1417 | /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP | |
1418 | if "left" equals to "init + right". */ | |
1419 | if (operand_equal_p (left, step_val, 0)) | |
1420 | { | |
1421 | if (dump_file && (dump_flags & TDF_SCEV)) | |
1422 | fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n"); | |
1423 | ||
1424 | return build_polynomial_chrec (loop->num, init_cond, right); | |
1425 | } | |
1426 | ||
1427 | /* Try harder to check if they are equal. */ | |
1428 | tree_to_aff_combination_expand (left, type, &aff1, &peeled_chrec_map); | |
1429 | tree_to_aff_combination_expand (step_val, type, &aff2, &peeled_chrec_map); | |
1430 | free_affine_expand_cache (&peeled_chrec_map); | |
807e902e | 1431 | aff_combination_scale (&aff2, -1); |
b83b5507 BC |
1432 | aff_combination_add (&aff1, &aff2); |
1433 | ||
1434 | /* Transform (init, {left, right}_LOOP)_LOOP to {init, right}_LOOP | |
1435 | if "left" equals to "init + right". */ | |
1436 | if (aff_combination_zero_p (&aff1)) | |
1437 | { | |
1438 | if (dump_file && (dump_flags & TDF_SCEV)) | |
1439 | fprintf (dump_file, "Simplify PEELED_CHREC into POLYNOMIAL_CHREC.\n"); | |
1440 | ||
1441 | return build_polynomial_chrec (loop->num, init_cond, right); | |
1442 | } | |
1443 | return chrec_dont_know; | |
1444 | } | |
9baba81b SP |
1445 | |
1446 | /* Given a LOOP_PHI_NODE, this function determines the evolution | |
1447 | function from LOOP_PHI_NODE to LOOP_PHI_NODE in the loop. */ | |
1448 | ||
1449 | static tree | |
538dd0b7 | 1450 | analyze_evolution_in_loop (gphi *loop_phi_node, |
9baba81b SP |
1451 | tree init_cond) |
1452 | { | |
726a989a | 1453 | int i, n = gimple_phi_num_args (loop_phi_node); |
9baba81b SP |
1454 | tree evolution_function = chrec_not_analyzed_yet; |
1455 | struct loop *loop = loop_containing_stmt (loop_phi_node); | |
1456 | basic_block bb; | |
b83b5507 | 1457 | static bool simplify_peeled_chrec_p = true; |
b8698a0f | 1458 | |
dfedbe40 | 1459 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
1460 | { |
1461 | fprintf (dump_file, "(analyze_evolution_in_loop \n"); | |
1462 | fprintf (dump_file, " (loop_phi_node = "); | |
726a989a | 1463 | print_gimple_stmt (dump_file, loop_phi_node, 0, 0); |
9baba81b SP |
1464 | fprintf (dump_file, ")\n"); |
1465 | } | |
b8698a0f | 1466 | |
726a989a | 1467 | for (i = 0; i < n; i++) |
9baba81b SP |
1468 | { |
1469 | tree arg = PHI_ARG_DEF (loop_phi_node, i); | |
355fe088 | 1470 | gimple *ssa_chain; |
726a989a | 1471 | tree ev_fn; |
874caa00 | 1472 | t_bool res; |
9baba81b SP |
1473 | |
1474 | /* Select the edges that enter the loop body. */ | |
726a989a | 1475 | bb = gimple_phi_arg_edge (loop_phi_node, i)->src; |
9baba81b SP |
1476 | if (!flow_bb_inside_loop_p (loop, bb)) |
1477 | continue; | |
f29deac9 | 1478 | |
9baba81b SP |
1479 | if (TREE_CODE (arg) == SSA_NAME) |
1480 | { | |
f29deac9 SP |
1481 | bool val = false; |
1482 | ||
9baba81b SP |
1483 | ssa_chain = SSA_NAME_DEF_STMT (arg); |
1484 | ||
1485 | /* Pass in the initial condition to the follow edge function. */ | |
1486 | ev_fn = init_cond; | |
c59dabbe | 1487 | res = follow_ssa_edge (loop, ssa_chain, loop_phi_node, &ev_fn, 0); |
f29deac9 SP |
1488 | |
1489 | /* If ev_fn has no evolution in the inner loop, and the | |
1490 | init_cond is not equal to ev_fn, then we have an | |
1491 | ambiguity between two possible values, as we cannot know | |
1492 | the number of iterations at this point. */ | |
1493 | if (TREE_CODE (ev_fn) != POLYNOMIAL_CHREC | |
1494 | && no_evolution_in_loop_p (ev_fn, loop->num, &val) && val | |
1495 | && !operand_equal_p (init_cond, ev_fn, 0)) | |
1496 | ev_fn = chrec_dont_know; | |
9baba81b SP |
1497 | } |
1498 | else | |
874caa00 | 1499 | res = t_false; |
f29deac9 | 1500 | |
9baba81b SP |
1501 | /* When it is impossible to go back on the same |
1502 | loop_phi_node by following the ssa edges, the | |
89dbed81 | 1503 | evolution is represented by a peeled chrec, i.e. the |
9baba81b | 1504 | first iteration, EV_FN has the value INIT_COND, then |
b8698a0f | 1505 | all the other iterations it has the value of ARG. |
9baba81b | 1506 | For the moment, PEELED_CHREC nodes are not built. */ |
874caa00 | 1507 | if (res != t_true) |
b83b5507 BC |
1508 | { |
1509 | ev_fn = chrec_dont_know; | |
1510 | /* Try to recognize POLYNOMIAL_CHREC which appears in | |
1511 | the form of PEELED_CHREC, but guard the process with | |
1512 | a bool variable to keep the analyzer from infinite | |
1513 | recurrence for real PEELED_RECs. */ | |
1514 | if (simplify_peeled_chrec_p && TREE_CODE (arg) == SSA_NAME) | |
1515 | { | |
1516 | simplify_peeled_chrec_p = false; | |
1517 | ev_fn = simplify_peeled_chrec (loop, arg, init_cond); | |
1518 | simplify_peeled_chrec_p = true; | |
1519 | } | |
1520 | } | |
b8698a0f | 1521 | |
9baba81b | 1522 | /* When there are multiple back edges of the loop (which in fact never |
8c27b7d4 | 1523 | happens currently, but nevertheless), merge their evolutions. */ |
9baba81b SP |
1524 | evolution_function = chrec_merge (evolution_function, ev_fn); |
1525 | } | |
b8698a0f | 1526 | |
dfedbe40 | 1527 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
1528 | { |
1529 | fprintf (dump_file, " (evolution_function = "); | |
1530 | print_generic_expr (dump_file, evolution_function, 0); | |
1531 | fprintf (dump_file, "))\n"); | |
1532 | } | |
b8698a0f | 1533 | |
9baba81b SP |
1534 | return evolution_function; |
1535 | } | |
1536 | ||
1537 | /* Given a loop-phi-node, return the initial conditions of the | |
1538 | variable on entry of the loop. When the CCP has propagated | |
1539 | constants into the loop-phi-node, the initial condition is | |
1540 | instantiated, otherwise the initial condition is kept symbolic. | |
1541 | This analyzer does not analyze the evolution outside the current | |
1542 | loop, and leaves this task to the on-demand tree reconstructor. */ | |
1543 | ||
b8698a0f | 1544 | static tree |
538dd0b7 | 1545 | analyze_initial_condition (gphi *loop_phi_node) |
9baba81b | 1546 | { |
726a989a | 1547 | int i, n; |
9baba81b | 1548 | tree init_cond = chrec_not_analyzed_yet; |
726a989a | 1549 | struct loop *loop = loop_containing_stmt (loop_phi_node); |
b8698a0f | 1550 | |
dfedbe40 | 1551 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
1552 | { |
1553 | fprintf (dump_file, "(analyze_initial_condition \n"); | |
1554 | fprintf (dump_file, " (loop_phi_node = \n"); | |
726a989a | 1555 | print_gimple_stmt (dump_file, loop_phi_node, 0, 0); |
9baba81b SP |
1556 | fprintf (dump_file, ")\n"); |
1557 | } | |
b8698a0f | 1558 | |
726a989a RB |
1559 | n = gimple_phi_num_args (loop_phi_node); |
1560 | for (i = 0; i < n; i++) | |
9baba81b SP |
1561 | { |
1562 | tree branch = PHI_ARG_DEF (loop_phi_node, i); | |
726a989a | 1563 | basic_block bb = gimple_phi_arg_edge (loop_phi_node, i)->src; |
b8698a0f | 1564 | |
9baba81b SP |
1565 | /* When the branch is oriented to the loop's body, it does |
1566 | not contribute to the initial condition. */ | |
1567 | if (flow_bb_inside_loop_p (loop, bb)) | |
1568 | continue; | |
1569 | ||
1570 | if (init_cond == chrec_not_analyzed_yet) | |
1571 | { | |
1572 | init_cond = branch; | |
1573 | continue; | |
1574 | } | |
1575 | ||
1576 | if (TREE_CODE (branch) == SSA_NAME) | |
1577 | { | |
1578 | init_cond = chrec_dont_know; | |
1579 | break; | |
1580 | } | |
1581 | ||
1582 | init_cond = chrec_merge (init_cond, branch); | |
1583 | } | |
1584 | ||
1585 | /* Ooops -- a loop without an entry??? */ | |
1586 | if (init_cond == chrec_not_analyzed_yet) | |
1587 | init_cond = chrec_dont_know; | |
1588 | ||
bf1cbdc6 RG |
1589 | /* During early loop unrolling we do not have fully constant propagated IL. |
1590 | Handle degenerate PHIs here to not miss important unrollings. */ | |
1591 | if (TREE_CODE (init_cond) == SSA_NAME) | |
1592 | { | |
355fe088 | 1593 | gimple *def = SSA_NAME_DEF_STMT (init_cond); |
538dd0b7 DM |
1594 | if (gphi *phi = dyn_cast <gphi *> (def)) |
1595 | { | |
1596 | tree res = degenerate_phi_result (phi); | |
1597 | if (res != NULL_TREE | |
1598 | /* Only allow invariants here, otherwise we may break | |
1599 | loop-closed SSA form. */ | |
1600 | && is_gimple_min_invariant (res)) | |
1601 | init_cond = res; | |
1602 | } | |
bf1cbdc6 RG |
1603 | } |
1604 | ||
dfedbe40 | 1605 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
1606 | { |
1607 | fprintf (dump_file, " (init_cond = "); | |
1608 | print_generic_expr (dump_file, init_cond, 0); | |
1609 | fprintf (dump_file, "))\n"); | |
1610 | } | |
b8698a0f | 1611 | |
9baba81b SP |
1612 | return init_cond; |
1613 | } | |
1614 | ||
1615 | /* Analyze the scalar evolution for LOOP_PHI_NODE. */ | |
1616 | ||
b8698a0f | 1617 | static tree |
538dd0b7 | 1618 | interpret_loop_phi (struct loop *loop, gphi *loop_phi_node) |
9baba81b SP |
1619 | { |
1620 | tree res; | |
1621 | struct loop *phi_loop = loop_containing_stmt (loop_phi_node); | |
1622 | tree init_cond; | |
b8698a0f | 1623 | |
9baba81b SP |
1624 | if (phi_loop != loop) |
1625 | { | |
1626 | struct loop *subloop; | |
1627 | tree evolution_fn = analyze_scalar_evolution | |
1628 | (phi_loop, PHI_RESULT (loop_phi_node)); | |
1629 | ||
1630 | /* Dive one level deeper. */ | |
9ba025a2 | 1631 | subloop = superloop_at_depth (phi_loop, loop_depth (loop) + 1); |
9baba81b SP |
1632 | |
1633 | /* Interpret the subloop. */ | |
1634 | res = compute_overall_effect_of_inner_loop (subloop, evolution_fn); | |
1635 | return res; | |
1636 | } | |
1637 | ||
1638 | /* Otherwise really interpret the loop phi. */ | |
1639 | init_cond = analyze_initial_condition (loop_phi_node); | |
1640 | res = analyze_evolution_in_loop (loop_phi_node, init_cond); | |
1641 | ||
73c865fa RG |
1642 | /* Verify we maintained the correct initial condition throughout |
1643 | possible conversions in the SSA chain. */ | |
1644 | if (res != chrec_dont_know) | |
1645 | { | |
1646 | tree new_init = res; | |
1647 | if (CONVERT_EXPR_P (res) | |
1648 | && TREE_CODE (TREE_OPERAND (res, 0)) == POLYNOMIAL_CHREC) | |
1649 | new_init = fold_convert (TREE_TYPE (res), | |
1650 | CHREC_LEFT (TREE_OPERAND (res, 0))); | |
1651 | else if (TREE_CODE (res) == POLYNOMIAL_CHREC) | |
1652 | new_init = CHREC_LEFT (res); | |
1653 | STRIP_USELESS_TYPE_CONVERSION (new_init); | |
eb723fa3 RG |
1654 | if (TREE_CODE (new_init) == POLYNOMIAL_CHREC |
1655 | || !operand_equal_p (init_cond, new_init, 0)) | |
73c865fa RG |
1656 | return chrec_dont_know; |
1657 | } | |
1658 | ||
9baba81b SP |
1659 | return res; |
1660 | } | |
1661 | ||
1662 | /* This function merges the branches of a condition-phi-node, | |
1663 | contained in the outermost loop, and whose arguments are already | |
1664 | analyzed. */ | |
1665 | ||
1666 | static tree | |
538dd0b7 | 1667 | interpret_condition_phi (struct loop *loop, gphi *condition_phi) |
9baba81b | 1668 | { |
726a989a | 1669 | int i, n = gimple_phi_num_args (condition_phi); |
9baba81b | 1670 | tree res = chrec_not_analyzed_yet; |
b8698a0f | 1671 | |
726a989a | 1672 | for (i = 0; i < n; i++) |
9baba81b SP |
1673 | { |
1674 | tree branch_chrec; | |
b8698a0f | 1675 | |
9baba81b SP |
1676 | if (backedge_phi_arg_p (condition_phi, i)) |
1677 | { | |
1678 | res = chrec_dont_know; | |
1679 | break; | |
1680 | } | |
1681 | ||
1682 | branch_chrec = analyze_scalar_evolution | |
1683 | (loop, PHI_ARG_DEF (condition_phi, i)); | |
b8698a0f | 1684 | |
9baba81b SP |
1685 | res = chrec_merge (res, branch_chrec); |
1686 | } | |
1687 | ||
1688 | return res; | |
1689 | } | |
1690 | ||
726a989a | 1691 | /* Interpret the operation RHS1 OP RHS2. If we didn't |
29836d07 | 1692 | analyze this node before, follow the definitions until ending |
726a989a | 1693 | either on an analyzed GIMPLE_ASSIGN, or on a loop-phi-node. On the |
9baba81b SP |
1694 | return path, this function propagates evolutions (ala constant copy |
1695 | propagation). OPND1 is not a GIMPLE expression because we could | |
1696 | analyze the effect of an inner loop: see interpret_loop_phi. */ | |
1697 | ||
1698 | static tree | |
355fe088 | 1699 | interpret_rhs_expr (struct loop *loop, gimple *at_stmt, |
726a989a | 1700 | tree type, tree rhs1, enum tree_code code, tree rhs2) |
9baba81b | 1701 | { |
726a989a | 1702 | tree res, chrec1, chrec2; |
355fe088 | 1703 | gimple *def; |
726a989a RB |
1704 | |
1705 | if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS) | |
1706 | { | |
1707 | if (is_gimple_min_invariant (rhs1)) | |
1708 | return chrec_convert (type, rhs1, at_stmt); | |
1709 | ||
1710 | if (code == SSA_NAME) | |
1711 | return chrec_convert (type, analyze_scalar_evolution (loop, rhs1), | |
1712 | at_stmt); | |
1e8552eb | 1713 | |
726a989a RB |
1714 | if (code == ASSERT_EXPR) |
1715 | { | |
1716 | rhs1 = ASSERT_EXPR_VAR (rhs1); | |
1717 | return chrec_convert (type, analyze_scalar_evolution (loop, rhs1), | |
1718 | at_stmt); | |
1719 | } | |
726a989a | 1720 | } |
1e8552eb | 1721 | |
726a989a | 1722 | switch (code) |
9baba81b | 1723 | { |
6a02a719 | 1724 | case ADDR_EXPR: |
bef28ced JL |
1725 | if (TREE_CODE (TREE_OPERAND (rhs1, 0)) == MEM_REF |
1726 | || handled_component_p (TREE_OPERAND (rhs1, 0))) | |
1727 | { | |
ef4bddc2 | 1728 | machine_mode mode; |
bef28ced JL |
1729 | HOST_WIDE_INT bitsize, bitpos; |
1730 | int unsignedp; | |
1731 | int volatilep = 0; | |
1732 | tree base, offset; | |
1733 | tree chrec3; | |
1734 | tree unitpos; | |
1735 | ||
1736 | base = get_inner_reference (TREE_OPERAND (rhs1, 0), | |
1737 | &bitsize, &bitpos, &offset, | |
b3ecff82 | 1738 | &mode, &unsignedp, &volatilep, false); |
bef28ced JL |
1739 | |
1740 | if (TREE_CODE (base) == MEM_REF) | |
1741 | { | |
1742 | rhs2 = TREE_OPERAND (base, 1); | |
1743 | rhs1 = TREE_OPERAND (base, 0); | |
1744 | ||
1745 | chrec1 = analyze_scalar_evolution (loop, rhs1); | |
1746 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1747 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1748 | chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt); | |
0547c9b6 RB |
1749 | chrec1 = instantiate_parameters (loop, chrec1); |
1750 | chrec2 = instantiate_parameters (loop, chrec2); | |
bef28ced JL |
1751 | res = chrec_fold_plus (type, chrec1, chrec2); |
1752 | } | |
1753 | else | |
1754 | { | |
1755 | chrec1 = analyze_scalar_evolution_for_address_of (loop, base); | |
1756 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1757 | res = chrec1; | |
1758 | } | |
6a02a719 | 1759 | |
bef28ced JL |
1760 | if (offset != NULL_TREE) |
1761 | { | |
1762 | chrec2 = analyze_scalar_evolution (loop, offset); | |
1763 | chrec2 = chrec_convert (TREE_TYPE (offset), chrec2, at_stmt); | |
0547c9b6 | 1764 | chrec2 = instantiate_parameters (loop, chrec2); |
bef28ced JL |
1765 | res = chrec_fold_plus (type, res, chrec2); |
1766 | } | |
1767 | ||
1768 | if (bitpos != 0) | |
1769 | { | |
1770 | gcc_assert ((bitpos % BITS_PER_UNIT) == 0); | |
1771 | ||
18dae016 | 1772 | unitpos = size_int (bitpos / BITS_PER_UNIT); |
bef28ced JL |
1773 | chrec3 = analyze_scalar_evolution (loop, unitpos); |
1774 | chrec3 = chrec_convert (TREE_TYPE (unitpos), chrec3, at_stmt); | |
0547c9b6 | 1775 | chrec3 = instantiate_parameters (loop, chrec3); |
bef28ced JL |
1776 | res = chrec_fold_plus (type, res, chrec3); |
1777 | } | |
1778 | } | |
1779 | else | |
1780 | res = chrec_dont_know; | |
1781 | break; | |
6a02a719 | 1782 | |
5be014d5 | 1783 | case POINTER_PLUS_EXPR: |
726a989a RB |
1784 | chrec1 = analyze_scalar_evolution (loop, rhs1); |
1785 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1786 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
0d82a1c8 | 1787 | chrec2 = chrec_convert (TREE_TYPE (rhs2), chrec2, at_stmt); |
0547c9b6 RB |
1788 | chrec1 = instantiate_parameters (loop, chrec1); |
1789 | chrec2 = instantiate_parameters (loop, chrec2); | |
726a989a | 1790 | res = chrec_fold_plus (type, chrec1, chrec2); |
5be014d5 AP |
1791 | break; |
1792 | ||
9baba81b | 1793 | case PLUS_EXPR: |
726a989a RB |
1794 | chrec1 = analyze_scalar_evolution (loop, rhs1); |
1795 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1796 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1797 | chrec2 = chrec_convert (type, chrec2, at_stmt); | |
0547c9b6 RB |
1798 | chrec1 = instantiate_parameters (loop, chrec1); |
1799 | chrec2 = instantiate_parameters (loop, chrec2); | |
726a989a | 1800 | res = chrec_fold_plus (type, chrec1, chrec2); |
9baba81b | 1801 | break; |
b8698a0f | 1802 | |
9baba81b | 1803 | case MINUS_EXPR: |
726a989a RB |
1804 | chrec1 = analyze_scalar_evolution (loop, rhs1); |
1805 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1806 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1807 | chrec2 = chrec_convert (type, chrec2, at_stmt); | |
0547c9b6 RB |
1808 | chrec1 = instantiate_parameters (loop, chrec1); |
1809 | chrec2 = instantiate_parameters (loop, chrec2); | |
726a989a | 1810 | res = chrec_fold_minus (type, chrec1, chrec2); |
9baba81b SP |
1811 | break; |
1812 | ||
1813 | case NEGATE_EXPR: | |
726a989a RB |
1814 | chrec1 = analyze_scalar_evolution (loop, rhs1); |
1815 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
9a75ede0 | 1816 | /* TYPE may be integer, real or complex, so use fold_convert. */ |
0547c9b6 | 1817 | chrec1 = instantiate_parameters (loop, chrec1); |
726a989a | 1818 | res = chrec_fold_multiply (type, chrec1, |
9a75ede0 | 1819 | fold_convert (type, integer_minus_one_node)); |
9baba81b SP |
1820 | break; |
1821 | ||
418df9d7 JJ |
1822 | case BIT_NOT_EXPR: |
1823 | /* Handle ~X as -1 - X. */ | |
1824 | chrec1 = analyze_scalar_evolution (loop, rhs1); | |
1825 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
0547c9b6 | 1826 | chrec1 = instantiate_parameters (loop, chrec1); |
418df9d7 JJ |
1827 | res = chrec_fold_minus (type, |
1828 | fold_convert (type, integer_minus_one_node), | |
1829 | chrec1); | |
1830 | break; | |
1831 | ||
9baba81b | 1832 | case MULT_EXPR: |
726a989a RB |
1833 | chrec1 = analyze_scalar_evolution (loop, rhs1); |
1834 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1835 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1836 | chrec2 = chrec_convert (type, chrec2, at_stmt); | |
0547c9b6 RB |
1837 | chrec1 = instantiate_parameters (loop, chrec1); |
1838 | chrec2 = instantiate_parameters (loop, chrec2); | |
726a989a | 1839 | res = chrec_fold_multiply (type, chrec1, chrec2); |
0bca51f0 | 1840 | break; |
b8698a0f | 1841 | |
6f907165 AL |
1842 | case LSHIFT_EXPR: |
1843 | /* Handle A<<B as A * (1<<B). */ | |
1844 | chrec1 = analyze_scalar_evolution (loop, rhs1); | |
1845 | chrec2 = analyze_scalar_evolution (loop, rhs2); | |
1846 | chrec1 = chrec_convert (type, chrec1, at_stmt); | |
1847 | chrec1 = instantiate_parameters (loop, chrec1); | |
1848 | chrec2 = instantiate_parameters (loop, chrec2); | |
1849 | ||
1850 | chrec2 = fold_build2 (LSHIFT_EXPR, type, | |
1851 | build_int_cst (TREE_TYPE (rhs1), 1), | |
1852 | chrec2); | |
1853 | res = chrec_fold_multiply (type, chrec1, chrec2); | |
1854 | break; | |
1855 | ||
1043771b | 1856 | CASE_CONVERT: |
195b4c50 RG |
1857 | /* In case we have a truncation of a widened operation that in |
1858 | the truncated type has undefined overflow behavior analyze | |
1859 | the operation done in an unsigned type of the same precision | |
1860 | as the final truncation. We cannot derive a scalar evolution | |
1861 | for the widened operation but for the truncated result. */ | |
1862 | if (TREE_CODE (type) == INTEGER_TYPE | |
1863 | && TREE_CODE (TREE_TYPE (rhs1)) == INTEGER_TYPE | |
1864 | && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (rhs1)) | |
1865 | && TYPE_OVERFLOW_UNDEFINED (type) | |
1866 | && TREE_CODE (rhs1) == SSA_NAME | |
1867 | && (def = SSA_NAME_DEF_STMT (rhs1)) | |
1868 | && is_gimple_assign (def) | |
1869 | && TREE_CODE_CLASS (gimple_assign_rhs_code (def)) == tcc_binary | |
1870 | && TREE_CODE (gimple_assign_rhs2 (def)) == INTEGER_CST) | |
1871 | { | |
1872 | tree utype = unsigned_type_for (type); | |
1873 | chrec1 = interpret_rhs_expr (loop, at_stmt, utype, | |
1874 | gimple_assign_rhs1 (def), | |
1875 | gimple_assign_rhs_code (def), | |
1876 | gimple_assign_rhs2 (def)); | |
1877 | } | |
1878 | else | |
1879 | chrec1 = analyze_scalar_evolution (loop, rhs1); | |
726a989a | 1880 | res = chrec_convert (type, chrec1, at_stmt); |
9baba81b | 1881 | break; |
b8698a0f | 1882 | |
9baba81b SP |
1883 | default: |
1884 | res = chrec_dont_know; | |
1885 | break; | |
1886 | } | |
b8698a0f | 1887 | |
9baba81b SP |
1888 | return res; |
1889 | } | |
1890 | ||
726a989a RB |
1891 | /* Interpret the expression EXPR. */ |
1892 | ||
1893 | static tree | |
355fe088 | 1894 | interpret_expr (struct loop *loop, gimple *at_stmt, tree expr) |
726a989a RB |
1895 | { |
1896 | enum tree_code code; | |
1897 | tree type = TREE_TYPE (expr), op0, op1; | |
1898 | ||
1899 | if (automatically_generated_chrec_p (expr)) | |
1900 | return expr; | |
1901 | ||
4e71066d RG |
1902 | if (TREE_CODE (expr) == POLYNOMIAL_CHREC |
1903 | || get_gimple_rhs_class (TREE_CODE (expr)) == GIMPLE_TERNARY_RHS) | |
726a989a RB |
1904 | return chrec_dont_know; |
1905 | ||
1906 | extract_ops_from_tree (expr, &code, &op0, &op1); | |
1907 | ||
1908 | return interpret_rhs_expr (loop, at_stmt, type, | |
1909 | op0, code, op1); | |
1910 | } | |
1911 | ||
1912 | /* Interpret the rhs of the assignment STMT. */ | |
1913 | ||
1914 | static tree | |
355fe088 | 1915 | interpret_gimple_assign (struct loop *loop, gimple *stmt) |
726a989a RB |
1916 | { |
1917 | tree type = TREE_TYPE (gimple_assign_lhs (stmt)); | |
1918 | enum tree_code code = gimple_assign_rhs_code (stmt); | |
1919 | ||
1920 | return interpret_rhs_expr (loop, stmt, type, | |
1921 | gimple_assign_rhs1 (stmt), code, | |
1922 | gimple_assign_rhs2 (stmt)); | |
1923 | } | |
1924 | ||
9baba81b SP |
1925 | \f |
1926 | ||
b8698a0f | 1927 | /* This section contains all the entry points: |
9baba81b SP |
1928 | - number_of_iterations_in_loop, |
1929 | - analyze_scalar_evolution, | |
1930 | - instantiate_parameters. | |
1931 | */ | |
1932 | ||
1933 | /* Compute and return the evolution function in WRTO_LOOP, the nearest | |
1934 | common ancestor of DEF_LOOP and USE_LOOP. */ | |
1935 | ||
b8698a0f L |
1936 | static tree |
1937 | compute_scalar_evolution_in_loop (struct loop *wrto_loop, | |
1938 | struct loop *def_loop, | |
9baba81b SP |
1939 | tree ev) |
1940 | { | |
492e5456 | 1941 | bool val; |
9baba81b | 1942 | tree res; |
492e5456 | 1943 | |
9baba81b SP |
1944 | if (def_loop == wrto_loop) |
1945 | return ev; | |
1946 | ||
9ba025a2 | 1947 | def_loop = superloop_at_depth (def_loop, loop_depth (wrto_loop) + 1); |
9baba81b SP |
1948 | res = compute_overall_effect_of_inner_loop (def_loop, ev); |
1949 | ||
492e5456 SP |
1950 | if (no_evolution_in_loop_p (res, wrto_loop->num, &val) && val) |
1951 | return res; | |
1952 | ||
9baba81b SP |
1953 | return analyze_scalar_evolution_1 (wrto_loop, res, chrec_not_analyzed_yet); |
1954 | } | |
1955 | ||
1956 | /* Helper recursive function. */ | |
1957 | ||
1958 | static tree | |
1959 | analyze_scalar_evolution_1 (struct loop *loop, tree var, tree res) | |
1960 | { | |
726a989a | 1961 | tree type = TREE_TYPE (var); |
355fe088 | 1962 | gimple *def; |
9baba81b SP |
1963 | basic_block bb; |
1964 | struct loop *def_loop; | |
1965 | ||
42d375ed | 1966 | if (loop == NULL || TREE_CODE (type) == VECTOR_TYPE) |
9baba81b SP |
1967 | return chrec_dont_know; |
1968 | ||
1969 | if (TREE_CODE (var) != SSA_NAME) | |
726a989a | 1970 | return interpret_expr (loop, NULL, var); |
9baba81b SP |
1971 | |
1972 | def = SSA_NAME_DEF_STMT (var); | |
726a989a | 1973 | bb = gimple_bb (def); |
9baba81b SP |
1974 | def_loop = bb ? bb->loop_father : NULL; |
1975 | ||
1976 | if (bb == NULL | |
1977 | || !flow_bb_inside_loop_p (loop, bb)) | |
1978 | { | |
1979 | /* Keep the symbolic form. */ | |
1980 | res = var; | |
1981 | goto set_and_end; | |
1982 | } | |
1983 | ||
1984 | if (res != chrec_not_analyzed_yet) | |
1985 | { | |
1986 | if (loop != bb->loop_father) | |
b8698a0f | 1987 | res = compute_scalar_evolution_in_loop |
9baba81b SP |
1988 | (find_common_loop (loop, bb->loop_father), bb->loop_father, res); |
1989 | ||
1990 | goto set_and_end; | |
1991 | } | |
1992 | ||
1993 | if (loop != def_loop) | |
1994 | { | |
1995 | res = analyze_scalar_evolution_1 (def_loop, var, chrec_not_analyzed_yet); | |
1996 | res = compute_scalar_evolution_in_loop (loop, def_loop, res); | |
1997 | ||
1998 | goto set_and_end; | |
1999 | } | |
2000 | ||
726a989a | 2001 | switch (gimple_code (def)) |
9baba81b | 2002 | { |
726a989a RB |
2003 | case GIMPLE_ASSIGN: |
2004 | res = interpret_gimple_assign (loop, def); | |
9baba81b SP |
2005 | break; |
2006 | ||
726a989a | 2007 | case GIMPLE_PHI: |
9baba81b | 2008 | if (loop_phi_node_p (def)) |
538dd0b7 | 2009 | res = interpret_loop_phi (loop, as_a <gphi *> (def)); |
9baba81b | 2010 | else |
538dd0b7 | 2011 | res = interpret_condition_phi (loop, as_a <gphi *> (def)); |
9baba81b SP |
2012 | break; |
2013 | ||
2014 | default: | |
2015 | res = chrec_dont_know; | |
2016 | break; | |
2017 | } | |
2018 | ||
2019 | set_and_end: | |
2020 | ||
2021 | /* Keep the symbolic form. */ | |
2022 | if (res == chrec_dont_know) | |
2023 | res = var; | |
2024 | ||
2025 | if (loop == def_loop) | |
a213b219 | 2026 | set_scalar_evolution (block_before_loop (loop), var, res); |
9baba81b SP |
2027 | |
2028 | return res; | |
2029 | } | |
2030 | ||
52bdd655 SP |
2031 | /* Analyzes and returns the scalar evolution of the ssa_name VAR in |
2032 | LOOP. LOOP is the loop in which the variable is used. | |
b8698a0f | 2033 | |
9baba81b SP |
2034 | Example of use: having a pointer VAR to a SSA_NAME node, STMT a |
2035 | pointer to the statement that uses this variable, in order to | |
2036 | determine the evolution function of the variable, use the following | |
2037 | calls: | |
b8698a0f | 2038 | |
52bdd655 SP |
2039 | loop_p loop = loop_containing_stmt (stmt); |
2040 | tree chrec_with_symbols = analyze_scalar_evolution (loop, var); | |
3f227a8c | 2041 | tree chrec_instantiated = instantiate_parameters (loop, chrec_with_symbols); |
9baba81b SP |
2042 | */ |
2043 | ||
b8698a0f | 2044 | tree |
9baba81b SP |
2045 | analyze_scalar_evolution (struct loop *loop, tree var) |
2046 | { | |
2047 | tree res; | |
2048 | ||
dfedbe40 | 2049 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
2050 | { |
2051 | fprintf (dump_file, "(analyze_scalar_evolution \n"); | |
2052 | fprintf (dump_file, " (loop_nb = %d)\n", loop->num); | |
2053 | fprintf (dump_file, " (scalar = "); | |
2054 | print_generic_expr (dump_file, var, 0); | |
2055 | fprintf (dump_file, ")\n"); | |
2056 | } | |
2057 | ||
a213b219 SP |
2058 | res = get_scalar_evolution (block_before_loop (loop), var); |
2059 | res = analyze_scalar_evolution_1 (loop, var, res); | |
9baba81b | 2060 | |
dfedbe40 | 2061 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
2062 | fprintf (dump_file, ")\n"); |
2063 | ||
2064 | return res; | |
2065 | } | |
2066 | ||
bef28ced JL |
2067 | /* Analyzes and returns the scalar evolution of VAR address in LOOP. */ |
2068 | ||
2069 | static tree | |
2070 | analyze_scalar_evolution_for_address_of (struct loop *loop, tree var) | |
2071 | { | |
2072 | return analyze_scalar_evolution (loop, build_fold_addr_expr (var)); | |
2073 | } | |
2074 | ||
9baba81b | 2075 | /* Analyze scalar evolution of use of VERSION in USE_LOOP with respect to |
f017bf5e | 2076 | WRTO_LOOP (which should be a superloop of USE_LOOP) |
a6f778b2 ZD |
2077 | |
2078 | FOLDED_CASTS is set to true if resolve_mixers used | |
2079 | chrec_convert_aggressive (TODO -- not really, we are way too conservative | |
b8698a0f L |
2080 | at the moment in order to keep things simple). |
2081 | ||
f017bf5e ZD |
2082 | To illustrate the meaning of USE_LOOP and WRTO_LOOP, consider the following |
2083 | example: | |
2084 | ||
2085 | for (i = 0; i < 100; i++) -- loop 1 | |
2086 | { | |
2087 | for (j = 0; j < 100; j++) -- loop 2 | |
2088 | { | |
2089 | k1 = i; | |
2090 | k2 = j; | |
2091 | ||
2092 | use2 (k1, k2); | |
2093 | ||
2094 | for (t = 0; t < 100; t++) -- loop 3 | |
2095 | use3 (k1, k2); | |
2096 | ||
2097 | } | |
2098 | use1 (k1, k2); | |
2099 | } | |
2100 | ||
2101 | Both k1 and k2 are invariants in loop3, thus | |
2102 | analyze_scalar_evolution_in_loop (loop3, loop3, k1) = k1 | |
2103 | analyze_scalar_evolution_in_loop (loop3, loop3, k2) = k2 | |
2104 | ||
2105 | As they are invariant, it does not matter whether we consider their | |
2106 | usage in loop 3 or loop 2, hence | |
2107 | analyze_scalar_evolution_in_loop (loop2, loop3, k1) = | |
2108 | analyze_scalar_evolution_in_loop (loop2, loop2, k1) = i | |
2109 | analyze_scalar_evolution_in_loop (loop2, loop3, k2) = | |
2110 | analyze_scalar_evolution_in_loop (loop2, loop2, k2) = [0,+,1]_2 | |
2111 | ||
2112 | Similarly for their evolutions with respect to loop 1. The values of K2 | |
2113 | in the use in loop 2 vary independently on loop 1, thus we cannot express | |
2114 | the evolution with respect to loop 1: | |
2115 | analyze_scalar_evolution_in_loop (loop1, loop3, k1) = | |
2116 | analyze_scalar_evolution_in_loop (loop1, loop2, k1) = [0,+,1]_1 | |
2117 | analyze_scalar_evolution_in_loop (loop1, loop3, k2) = | |
2118 | analyze_scalar_evolution_in_loop (loop1, loop2, k2) = dont_know | |
2119 | ||
2120 | The value of k2 in the use in loop 1 is known, though: | |
2121 | analyze_scalar_evolution_in_loop (loop1, loop1, k1) = [0,+,1]_1 | |
2122 | analyze_scalar_evolution_in_loop (loop1, loop1, k2) = 100 | |
2123 | */ | |
9baba81b SP |
2124 | |
2125 | static tree | |
2126 | analyze_scalar_evolution_in_loop (struct loop *wrto_loop, struct loop *use_loop, | |
a6f778b2 | 2127 | tree version, bool *folded_casts) |
9baba81b SP |
2128 | { |
2129 | bool val = false; | |
a6f778b2 | 2130 | tree ev = version, tmp; |
9baba81b | 2131 | |
b8698a0f | 2132 | /* We cannot just do |
f017bf5e ZD |
2133 | |
2134 | tmp = analyze_scalar_evolution (use_loop, version); | |
c70ed622 | 2135 | ev = resolve_mixers (wrto_loop, tmp, folded_casts); |
f017bf5e ZD |
2136 | |
2137 | as resolve_mixers would query the scalar evolution with respect to | |
2138 | wrto_loop. For example, in the situation described in the function | |
2139 | comment, suppose that wrto_loop = loop1, use_loop = loop3 and | |
2140 | version = k2. Then | |
2141 | ||
2142 | analyze_scalar_evolution (use_loop, version) = k2 | |
2143 | ||
c70ed622 BC |
2144 | and resolve_mixers (loop1, k2, folded_casts) finds that the value of |
2145 | k2 in loop 1 is 100, which is a wrong result, since we are interested | |
2146 | in the value in loop 3. | |
f017bf5e ZD |
2147 | |
2148 | Instead, we need to proceed from use_loop to wrto_loop loop by loop, | |
2149 | each time checking that there is no evolution in the inner loop. */ | |
2150 | ||
a6f778b2 ZD |
2151 | if (folded_casts) |
2152 | *folded_casts = false; | |
9baba81b SP |
2153 | while (1) |
2154 | { | |
a6f778b2 | 2155 | tmp = analyze_scalar_evolution (use_loop, ev); |
c70ed622 | 2156 | ev = resolve_mixers (use_loop, tmp, folded_casts); |
9baba81b SP |
2157 | |
2158 | if (use_loop == wrto_loop) | |
2159 | return ev; | |
2160 | ||
2161 | /* If the value of the use changes in the inner loop, we cannot express | |
2162 | its value in the outer loop (we might try to return interval chrec, | |
2163 | but we do not have a user for it anyway) */ | |
2164 | if (!no_evolution_in_loop_p (ev, use_loop->num, &val) | |
2165 | || !val) | |
2166 | return chrec_dont_know; | |
2167 | ||
9ba025a2 | 2168 | use_loop = loop_outer (use_loop); |
9baba81b SP |
2169 | } |
2170 | } | |
2171 | ||
eb0bc7af | 2172 | |
fdd43ac4 RB |
2173 | /* Hashtable helpers for a temporary hash-table used when |
2174 | instantiating a CHREC or resolving mixers. For this use | |
2175 | instantiated_below is always the same. */ | |
2176 | ||
fdd43ac4 | 2177 | struct instantiate_cache_type |
eb0bc7af | 2178 | { |
a3cc13cc RB |
2179 | htab_t map; |
2180 | vec<scev_info_str> entries; | |
b8698a0f | 2181 | |
c3284718 | 2182 | instantiate_cache_type () : map (NULL), entries (vNULL) {} |
fdd43ac4 | 2183 | ~instantiate_cache_type (); |
0547c9b6 RB |
2184 | tree get (unsigned slot) { return entries[slot].chrec; } |
2185 | void set (unsigned slot, tree chrec) { entries[slot].chrec = chrec; } | |
fdd43ac4 | 2186 | }; |
eb0bc7af | 2187 | |
fdd43ac4 RB |
2188 | instantiate_cache_type::~instantiate_cache_type () |
2189 | { | |
0547c9b6 | 2190 | if (map != NULL) |
fdd43ac4 | 2191 | { |
a3cc13cc | 2192 | htab_delete (map); |
fdd43ac4 RB |
2193 | entries.release (); |
2194 | } | |
eb0bc7af ZD |
2195 | } |
2196 | ||
a3cc13cc RB |
2197 | /* Cache to avoid infinite recursion when instantiating an SSA name. |
2198 | Live during the outermost instantiate_scev or resolve_mixers call. */ | |
2199 | static instantiate_cache_type *global_cache; | |
2200 | ||
2201 | /* Computes a hash function for database element ELT. */ | |
2202 | ||
2203 | static inline hashval_t | |
2204 | hash_idx_scev_info (const void *elt_) | |
2205 | { | |
2206 | unsigned idx = ((size_t) elt_) - 2; | |
907dadbd | 2207 | return scev_info_hasher::hash (&global_cache->entries[idx]); |
a3cc13cc RB |
2208 | } |
2209 | ||
2210 | /* Compares database elements E1 and E2. */ | |
2211 | ||
2212 | static inline int | |
2213 | eq_idx_scev_info (const void *e1, const void *e2) | |
2214 | { | |
2215 | unsigned idx1 = ((size_t) e1) - 2; | |
907dadbd TS |
2216 | return scev_info_hasher::equal (&global_cache->entries[idx1], |
2217 | (const scev_info_str *) e2); | |
a3cc13cc RB |
2218 | } |
2219 | ||
0547c9b6 | 2220 | /* Returns from CACHE the slot number of the cached chrec for NAME. */ |
fdd43ac4 | 2221 | |
0547c9b6 | 2222 | static unsigned |
a3cc13cc RB |
2223 | get_instantiated_value_entry (instantiate_cache_type &cache, |
2224 | tree name, basic_block instantiate_below) | |
fdd43ac4 | 2225 | { |
0547c9b6 | 2226 | if (!cache.map) |
fdd43ac4 | 2227 | { |
a3cc13cc | 2228 | cache.map = htab_create (10, hash_idx_scev_info, eq_idx_scev_info, NULL); |
fdd43ac4 RB |
2229 | cache.entries.create (10); |
2230 | } | |
b8698a0f | 2231 | |
a3cc13cc RB |
2232 | scev_info_str e; |
2233 | e.name_version = SSA_NAME_VERSION (name); | |
2234 | e.instantiated_below = instantiate_below->index; | |
2235 | void **slot = htab_find_slot_with_hash (cache.map, &e, | |
907dadbd | 2236 | scev_info_hasher::hash (&e), INSERT); |
a3cc13cc | 2237 | if (!*slot) |
fdd43ac4 RB |
2238 | { |
2239 | e.chrec = chrec_not_analyzed_yet; | |
a3cc13cc | 2240 | *slot = (void *)(size_t)(cache.entries.length () + 2); |
fdd43ac4 | 2241 | cache.entries.safe_push (e); |
fdd43ac4 RB |
2242 | } |
2243 | ||
a3cc13cc | 2244 | return ((size_t)*slot) - 2; |
eb0bc7af ZD |
2245 | } |
2246 | ||
0547c9b6 | 2247 | |
18aed06a SP |
2248 | /* Return the closed_loop_phi node for VAR. If there is none, return |
2249 | NULL_TREE. */ | |
2250 | ||
2251 | static tree | |
2252 | loop_closed_phi_def (tree var) | |
2253 | { | |
2254 | struct loop *loop; | |
2255 | edge exit; | |
538dd0b7 DM |
2256 | gphi *phi; |
2257 | gphi_iterator psi; | |
18aed06a SP |
2258 | |
2259 | if (var == NULL_TREE | |
2260 | || TREE_CODE (var) != SSA_NAME) | |
2261 | return NULL_TREE; | |
2262 | ||
2263 | loop = loop_containing_stmt (SSA_NAME_DEF_STMT (var)); | |
ac8f6c69 | 2264 | exit = single_exit (loop); |
18aed06a SP |
2265 | if (!exit) |
2266 | return NULL_TREE; | |
2267 | ||
726a989a RB |
2268 | for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi)) |
2269 | { | |
538dd0b7 | 2270 | phi = psi.phi (); |
726a989a RB |
2271 | if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == var) |
2272 | return PHI_RESULT (phi); | |
2273 | } | |
18aed06a SP |
2274 | |
2275 | return NULL_TREE; | |
2276 | } | |
2277 | ||
8b679c9b | 2278 | static tree instantiate_scev_r (basic_block, struct loop *, struct loop *, |
c70ed622 | 2279 | tree, bool *, int); |
320f5a78 SP |
2280 | |
2281 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW | |
2282 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2283 | ||
2495a183 | 2284 | CHREC is an SSA_NAME to be instantiated. |
320f5a78 SP |
2285 | |
2286 | CACHE is the cache of already instantiated values. | |
2287 | ||
c70ed622 BC |
2288 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2289 | conversions that may wrap in signed/pointer type are folded, as long | |
2290 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2291 | then we don't do such fold. | |
320f5a78 SP |
2292 | |
2293 | SIZE_EXPR is used for computing the size of the expression to be | |
2294 | instantiated, and to stop if it exceeds some limit. */ | |
2295 | ||
2296 | static tree | |
2495a183 | 2297 | instantiate_scev_name (basic_block instantiate_below, |
8b679c9b RB |
2298 | struct loop *evolution_loop, struct loop *inner_loop, |
2299 | tree chrec, | |
c70ed622 | 2300 | bool *fold_conversions, |
4a8fb1a1 | 2301 | int size_expr) |
320f5a78 | 2302 | { |
2495a183 SP |
2303 | tree res; |
2304 | struct loop *def_loop; | |
2305 | basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (chrec)); | |
20179b0d | 2306 | |
2495a183 SP |
2307 | /* A parameter (or loop invariant and we do not want to include |
2308 | evolutions in outer loops), nothing to do. */ | |
2309 | if (!def_bb | |
2310 | || loop_depth (def_bb->loop_father) == 0 | |
2311 | || dominated_by_p (CDI_DOMINATORS, instantiate_below, def_bb)) | |
2312 | return chrec; | |
20179b0d | 2313 | |
2495a183 SP |
2314 | /* We cache the value of instantiated variable to avoid exponential |
2315 | time complexity due to reevaluations. We also store the convenient | |
2316 | value in the cache in order to prevent infinite recursion -- we do | |
2317 | not want to instantiate the SSA_NAME if it is in a mixer | |
2318 | structure. This is used for avoiding the instantiation of | |
2319 | recursively defined functions, such as: | |
320f5a78 | 2320 | |
2495a183 | 2321 | | a_2 -> {0, +, 1, +, a_2}_1 */ |
20179b0d | 2322 | |
a3cc13cc RB |
2323 | unsigned si = get_instantiated_value_entry (*global_cache, |
2324 | chrec, instantiate_below); | |
0547c9b6 RB |
2325 | if (global_cache->get (si) != chrec_not_analyzed_yet) |
2326 | return global_cache->get (si); | |
20179b0d | 2327 | |
fdd43ac4 | 2328 | /* On recursion return chrec_dont_know. */ |
0547c9b6 | 2329 | global_cache->set (si, chrec_dont_know); |
320f5a78 | 2330 | |
2495a183 SP |
2331 | def_loop = find_common_loop (evolution_loop, def_bb->loop_father); |
2332 | ||
320f5a78 SP |
2333 | /* If the analysis yields a parametric chrec, instantiate the |
2334 | result again. */ | |
2335 | res = analyze_scalar_evolution (def_loop, chrec); | |
2336 | ||
2847388e | 2337 | /* Don't instantiate default definitions. */ |
320f5a78 | 2338 | if (TREE_CODE (res) == SSA_NAME |
2847388e SP |
2339 | && SSA_NAME_IS_DEFAULT_DEF (res)) |
2340 | ; | |
2341 | ||
2342 | /* Don't instantiate loop-closed-ssa phi nodes. */ | |
2343 | else if (TREE_CODE (res) == SSA_NAME | |
2344 | && loop_depth (loop_containing_stmt (SSA_NAME_DEF_STMT (res))) | |
2345 | > loop_depth (def_loop)) | |
320f5a78 SP |
2346 | { |
2347 | if (res == chrec) | |
2348 | res = loop_closed_phi_def (chrec); | |
2349 | else | |
2350 | res = chrec; | |
2351 | ||
7472eb13 SP |
2352 | /* When there is no loop_closed_phi_def, it means that the |
2353 | variable is not used after the loop: try to still compute the | |
2354 | value of the variable when exiting the loop. */ | |
2355 | if (res == NULL_TREE) | |
2356 | { | |
2357 | loop_p loop = loop_containing_stmt (SSA_NAME_DEF_STMT (chrec)); | |
2358 | res = analyze_scalar_evolution (loop, chrec); | |
2359 | res = compute_overall_effect_of_inner_loop (loop, res); | |
8b679c9b RB |
2360 | res = instantiate_scev_r (instantiate_below, evolution_loop, |
2361 | inner_loop, res, | |
0547c9b6 | 2362 | fold_conversions, size_expr); |
7472eb13 SP |
2363 | } |
2364 | else if (!dominated_by_p (CDI_DOMINATORS, instantiate_below, | |
2365 | gimple_bb (SSA_NAME_DEF_STMT (res)))) | |
320f5a78 SP |
2366 | res = chrec_dont_know; |
2367 | } | |
2368 | ||
2369 | else if (res != chrec_dont_know) | |
8b679c9b RB |
2370 | { |
2371 | if (inner_loop | |
63fdb7be | 2372 | && def_bb->loop_father != inner_loop |
8b679c9b RB |
2373 | && !flow_loop_nested_p (def_bb->loop_father, inner_loop)) |
2374 | /* ??? We could try to compute the overall effect of the loop here. */ | |
2375 | res = chrec_dont_know; | |
2376 | else | |
2377 | res = instantiate_scev_r (instantiate_below, evolution_loop, | |
2378 | inner_loop, res, | |
0547c9b6 | 2379 | fold_conversions, size_expr); |
8b679c9b | 2380 | } |
320f5a78 SP |
2381 | |
2382 | /* Store the correct value to the cache. */ | |
0547c9b6 | 2383 | global_cache->set (si, res); |
320f5a78 | 2384 | return res; |
320f5a78 SP |
2385 | } |
2386 | ||
ec6636eb SP |
2387 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2388 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2389 | ||
2390 | CHREC is a polynomial chain of recurrence to be instantiated. | |
2391 | ||
2392 | CACHE is the cache of already instantiated values. | |
2393 | ||
c70ed622 BC |
2394 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2395 | conversions that may wrap in signed/pointer type are folded, as long | |
2396 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2397 | then we don't do such fold. | |
ec6636eb SP |
2398 | |
2399 | SIZE_EXPR is used for computing the size of the expression to be | |
2400 | instantiated, and to stop if it exceeds some limit. */ | |
2401 | ||
2402 | static tree | |
2403 | instantiate_scev_poly (basic_block instantiate_below, | |
8b679c9b | 2404 | struct loop *evolution_loop, struct loop *, |
c70ed622 | 2405 | tree chrec, bool *fold_conversions, int size_expr) |
ec6636eb SP |
2406 | { |
2407 | tree op1; | |
9e5dc77f | 2408 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2409 | get_chrec_loop (chrec), |
0547c9b6 | 2410 | CHREC_LEFT (chrec), fold_conversions, |
ec6636eb SP |
2411 | size_expr); |
2412 | if (op0 == chrec_dont_know) | |
2413 | return chrec_dont_know; | |
2414 | ||
9e5dc77f | 2415 | op1 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2416 | get_chrec_loop (chrec), |
0547c9b6 | 2417 | CHREC_RIGHT (chrec), fold_conversions, |
ec6636eb SP |
2418 | size_expr); |
2419 | if (op1 == chrec_dont_know) | |
2420 | return chrec_dont_know; | |
2421 | ||
2422 | if (CHREC_LEFT (chrec) != op0 | |
2423 | || CHREC_RIGHT (chrec) != op1) | |
2424 | { | |
2425 | op1 = chrec_convert_rhs (chrec_type (op0), op1, NULL); | |
8b679c9b | 2426 | chrec = build_polynomial_chrec (CHREC_VARIABLE (chrec), op0, op1); |
ec6636eb | 2427 | } |
4bf4e169 | 2428 | |
ec6636eb SP |
2429 | return chrec; |
2430 | } | |
2431 | ||
15fda317 SP |
2432 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2433 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2434 | ||
ffa34f4b | 2435 | "C0 CODE C1" is a binary expression of type TYPE to be instantiated. |
15fda317 SP |
2436 | |
2437 | CACHE is the cache of already instantiated values. | |
2438 | ||
c70ed622 BC |
2439 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2440 | conversions that may wrap in signed/pointer type are folded, as long | |
2441 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2442 | then we don't do such fold. | |
15fda317 SP |
2443 | |
2444 | SIZE_EXPR is used for computing the size of the expression to be | |
2445 | instantiated, and to stop if it exceeds some limit. */ | |
2446 | ||
2447 | static tree | |
2448 | instantiate_scev_binary (basic_block instantiate_below, | |
8b679c9b RB |
2449 | struct loop *evolution_loop, struct loop *inner_loop, |
2450 | tree chrec, enum tree_code code, | |
ffa34f4b | 2451 | tree type, tree c0, tree c1, |
c70ed622 | 2452 | bool *fold_conversions, int size_expr) |
15fda317 SP |
2453 | { |
2454 | tree op1; | |
8b679c9b | 2455 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop, |
0547c9b6 | 2456 | c0, fold_conversions, size_expr); |
15fda317 SP |
2457 | if (op0 == chrec_dont_know) |
2458 | return chrec_dont_know; | |
2459 | ||
8b679c9b | 2460 | op1 = instantiate_scev_r (instantiate_below, evolution_loop, inner_loop, |
0547c9b6 | 2461 | c1, fold_conversions, size_expr); |
15fda317 SP |
2462 | if (op1 == chrec_dont_know) |
2463 | return chrec_dont_know; | |
2464 | ||
ffa34f4b SP |
2465 | if (c0 != op0 |
2466 | || c1 != op1) | |
15fda317 | 2467 | { |
15fda317 SP |
2468 | op0 = chrec_convert (type, op0, NULL); |
2469 | op1 = chrec_convert_rhs (type, op1, NULL); | |
2470 | ||
ffa34f4b | 2471 | switch (code) |
15fda317 SP |
2472 | { |
2473 | case POINTER_PLUS_EXPR: | |
2474 | case PLUS_EXPR: | |
2475 | return chrec_fold_plus (type, op0, op1); | |
2476 | ||
2477 | case MINUS_EXPR: | |
2478 | return chrec_fold_minus (type, op0, op1); | |
2479 | ||
2480 | case MULT_EXPR: | |
2481 | return chrec_fold_multiply (type, op0, op1); | |
2482 | ||
2483 | default: | |
2484 | gcc_unreachable (); | |
2485 | } | |
2486 | } | |
2487 | ||
ffa34f4b | 2488 | return chrec ? chrec : fold_build2 (code, type, c0, c1); |
15fda317 SP |
2489 | } |
2490 | ||
dbc08079 SP |
2491 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2492 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2493 | ||
2494 | "CHREC" is an array reference to be instantiated. | |
2495 | ||
2496 | CACHE is the cache of already instantiated values. | |
2497 | ||
c70ed622 BC |
2498 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2499 | conversions that may wrap in signed/pointer type are folded, as long | |
2500 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2501 | then we don't do such fold. | |
dbc08079 SP |
2502 | |
2503 | SIZE_EXPR is used for computing the size of the expression to be | |
2504 | instantiated, and to stop if it exceeds some limit. */ | |
2505 | ||
2506 | static tree | |
2507 | instantiate_array_ref (basic_block instantiate_below, | |
8b679c9b | 2508 | struct loop *evolution_loop, struct loop *inner_loop, |
c70ed622 | 2509 | tree chrec, bool *fold_conversions, int size_expr) |
dbc08079 SP |
2510 | { |
2511 | tree res; | |
2512 | tree index = TREE_OPERAND (chrec, 1); | |
8b679c9b RB |
2513 | tree op1 = instantiate_scev_r (instantiate_below, evolution_loop, |
2514 | inner_loop, index, | |
0547c9b6 | 2515 | fold_conversions, size_expr); |
dbc08079 SP |
2516 | |
2517 | if (op1 == chrec_dont_know) | |
2518 | return chrec_dont_know; | |
2519 | ||
2520 | if (chrec && op1 == index) | |
2521 | return chrec; | |
2522 | ||
2523 | res = unshare_expr (chrec); | |
2524 | TREE_OPERAND (res, 1) = op1; | |
2525 | return res; | |
2526 | } | |
2527 | ||
a213b219 | 2528 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
9c382ce9 SP |
2529 | and EVOLUTION_LOOP, that were left under a symbolic form. |
2530 | ||
2531 | "CHREC" that stands for a convert expression "(TYPE) OP" is to be | |
2532 | instantiated. | |
2533 | ||
2534 | CACHE is the cache of already instantiated values. | |
2535 | ||
c70ed622 BC |
2536 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2537 | conversions that may wrap in signed/pointer type are folded, as long | |
2538 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2539 | then we don't do such fold. | |
9c382ce9 SP |
2540 | |
2541 | SIZE_EXPR is used for computing the size of the expression to be | |
2542 | instantiated, and to stop if it exceeds some limit. */ | |
2543 | ||
2544 | static tree | |
2545 | instantiate_scev_convert (basic_block instantiate_below, | |
8b679c9b | 2546 | struct loop *evolution_loop, struct loop *inner_loop, |
0547c9b6 | 2547 | tree chrec, tree type, tree op, |
c70ed622 | 2548 | bool *fold_conversions, int size_expr) |
9c382ce9 | 2549 | { |
8b679c9b RB |
2550 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, |
2551 | inner_loop, op, | |
0547c9b6 | 2552 | fold_conversions, size_expr); |
9c382ce9 SP |
2553 | |
2554 | if (op0 == chrec_dont_know) | |
2555 | return chrec_dont_know; | |
2556 | ||
2557 | if (fold_conversions) | |
2558 | { | |
c70ed622 | 2559 | tree tmp = chrec_convert_aggressive (type, op0, fold_conversions); |
9c382ce9 SP |
2560 | if (tmp) |
2561 | return tmp; | |
9c382ce9 | 2562 | |
c70ed622 BC |
2563 | /* If we used chrec_convert_aggressive, we can no longer assume that |
2564 | signed chrecs do not overflow, as chrec_convert does, so avoid | |
2565 | calling it in that case. */ | |
2566 | if (*fold_conversions) | |
2567 | { | |
2568 | if (chrec && op0 == op) | |
2569 | return chrec; | |
9c382ce9 | 2570 | |
c70ed622 BC |
2571 | return fold_convert (type, op0); |
2572 | } | |
2573 | } | |
9c382ce9 SP |
2574 | |
2575 | return chrec_convert (type, op0, NULL); | |
2576 | } | |
2577 | ||
7ec0665d SP |
2578 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2579 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2580 | ||
4b9d48a1 | 2581 | CHREC is a BIT_NOT_EXPR or a NEGATE_EXPR expression to be instantiated. |
7ec0665d | 2582 | Handle ~X as -1 - X. |
4b9d48a1 | 2583 | Handle -X as -1 * X. |
7ec0665d SP |
2584 | |
2585 | CACHE is the cache of already instantiated values. | |
2586 | ||
c70ed622 BC |
2587 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2588 | conversions that may wrap in signed/pointer type are folded, as long | |
2589 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2590 | then we don't do such fold. | |
7ec0665d SP |
2591 | |
2592 | SIZE_EXPR is used for computing the size of the expression to be | |
2593 | instantiated, and to stop if it exceeds some limit. */ | |
2594 | ||
2595 | static tree | |
4b9d48a1 | 2596 | instantiate_scev_not (basic_block instantiate_below, |
8b679c9b RB |
2597 | struct loop *evolution_loop, struct loop *inner_loop, |
2598 | tree chrec, | |
20179b0d | 2599 | enum tree_code code, tree type, tree op, |
c70ed622 | 2600 | bool *fold_conversions, int size_expr) |
7ec0665d | 2601 | { |
8b679c9b RB |
2602 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, |
2603 | inner_loop, op, | |
0547c9b6 | 2604 | fold_conversions, size_expr); |
20179b0d | 2605 | |
7ec0665d SP |
2606 | if (op0 == chrec_dont_know) |
2607 | return chrec_dont_know; | |
2608 | ||
20179b0d | 2609 | if (op != op0) |
7ec0665d SP |
2610 | { |
2611 | op0 = chrec_convert (type, op0, NULL); | |
4b9d48a1 | 2612 | |
20179b0d | 2613 | switch (code) |
4b9d48a1 SP |
2614 | { |
2615 | case BIT_NOT_EXPR: | |
2616 | return chrec_fold_minus | |
2617 | (type, fold_convert (type, integer_minus_one_node), op0); | |
2618 | ||
2619 | case NEGATE_EXPR: | |
2620 | return chrec_fold_multiply | |
2621 | (type, fold_convert (type, integer_minus_one_node), op0); | |
2622 | ||
2623 | default: | |
2624 | gcc_unreachable (); | |
2625 | } | |
7ec0665d | 2626 | } |
4b9d48a1 | 2627 | |
20179b0d | 2628 | return chrec ? chrec : fold_build1 (code, type, op0); |
7ec0665d SP |
2629 | } |
2630 | ||
d814176c SP |
2631 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2632 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2633 | ||
2634 | CHREC is an expression with 3 operands to be instantiated. | |
2635 | ||
2636 | CACHE is the cache of already instantiated values. | |
2637 | ||
c70ed622 BC |
2638 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2639 | conversions that may wrap in signed/pointer type are folded, as long | |
2640 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2641 | then we don't do such fold. | |
d814176c SP |
2642 | |
2643 | SIZE_EXPR is used for computing the size of the expression to be | |
2644 | instantiated, and to stop if it exceeds some limit. */ | |
2645 | ||
2646 | static tree | |
2647 | instantiate_scev_3 (basic_block instantiate_below, | |
8b679c9b RB |
2648 | struct loop *evolution_loop, struct loop *inner_loop, |
2649 | tree chrec, | |
c70ed622 | 2650 | bool *fold_conversions, int size_expr) |
d814176c SP |
2651 | { |
2652 | tree op1, op2; | |
9e5dc77f | 2653 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2654 | inner_loop, TREE_OPERAND (chrec, 0), |
0547c9b6 | 2655 | fold_conversions, size_expr); |
d814176c SP |
2656 | if (op0 == chrec_dont_know) |
2657 | return chrec_dont_know; | |
2658 | ||
9e5dc77f | 2659 | op1 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2660 | inner_loop, TREE_OPERAND (chrec, 1), |
0547c9b6 | 2661 | fold_conversions, size_expr); |
d814176c SP |
2662 | if (op1 == chrec_dont_know) |
2663 | return chrec_dont_know; | |
2664 | ||
9e5dc77f | 2665 | op2 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2666 | inner_loop, TREE_OPERAND (chrec, 2), |
0547c9b6 | 2667 | fold_conversions, size_expr); |
d814176c SP |
2668 | if (op2 == chrec_dont_know) |
2669 | return chrec_dont_know; | |
2670 | ||
2671 | if (op0 == TREE_OPERAND (chrec, 0) | |
2672 | && op1 == TREE_OPERAND (chrec, 1) | |
2673 | && op2 == TREE_OPERAND (chrec, 2)) | |
2674 | return chrec; | |
2675 | ||
2676 | return fold_build3 (TREE_CODE (chrec), | |
2677 | TREE_TYPE (chrec), op0, op1, op2); | |
2678 | } | |
2679 | ||
9c382ce9 SP |
2680 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW |
2681 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
5b78fc3e | 2682 | |
9e5dc77f SP |
2683 | CHREC is an expression with 2 operands to be instantiated. |
2684 | ||
2685 | CACHE is the cache of already instantiated values. | |
2686 | ||
c70ed622 BC |
2687 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2688 | conversions that may wrap in signed/pointer type are folded, as long | |
2689 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2690 | then we don't do such fold. | |
9e5dc77f SP |
2691 | |
2692 | SIZE_EXPR is used for computing the size of the expression to be | |
2693 | instantiated, and to stop if it exceeds some limit. */ | |
2694 | ||
2695 | static tree | |
2696 | instantiate_scev_2 (basic_block instantiate_below, | |
8b679c9b RB |
2697 | struct loop *evolution_loop, struct loop *inner_loop, |
2698 | tree chrec, | |
c70ed622 | 2699 | bool *fold_conversions, int size_expr) |
9e5dc77f SP |
2700 | { |
2701 | tree op1; | |
2702 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, | |
8b679c9b | 2703 | inner_loop, TREE_OPERAND (chrec, 0), |
0547c9b6 | 2704 | fold_conversions, size_expr); |
9e5dc77f SP |
2705 | if (op0 == chrec_dont_know) |
2706 | return chrec_dont_know; | |
2707 | ||
2708 | op1 = instantiate_scev_r (instantiate_below, evolution_loop, | |
8b679c9b | 2709 | inner_loop, TREE_OPERAND (chrec, 1), |
0547c9b6 | 2710 | fold_conversions, size_expr); |
9e5dc77f SP |
2711 | if (op1 == chrec_dont_know) |
2712 | return chrec_dont_know; | |
2713 | ||
2714 | if (op0 == TREE_OPERAND (chrec, 0) | |
2715 | && op1 == TREE_OPERAND (chrec, 1)) | |
2716 | return chrec; | |
2717 | ||
2718 | return fold_build2 (TREE_CODE (chrec), TREE_TYPE (chrec), op0, op1); | |
2719 | } | |
2720 | ||
2721 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW | |
2722 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2723 | ||
2724 | CHREC is an expression with 2 operands to be instantiated. | |
5b78fc3e JS |
2725 | |
2726 | CACHE is the cache of already instantiated values. | |
2727 | ||
c70ed622 BC |
2728 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2729 | conversions that may wrap in signed/pointer type are folded, as long | |
2730 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2731 | then we don't do such fold. | |
5b78fc3e | 2732 | |
3f227a8c JS |
2733 | SIZE_EXPR is used for computing the size of the expression to be |
2734 | instantiated, and to stop if it exceeds some limit. */ | |
9c382ce9 | 2735 | |
9baba81b | 2736 | static tree |
a213b219 | 2737 | instantiate_scev_1 (basic_block instantiate_below, |
8b679c9b RB |
2738 | struct loop *evolution_loop, struct loop *inner_loop, |
2739 | tree chrec, | |
c70ed622 | 2740 | bool *fold_conversions, int size_expr) |
9baba81b | 2741 | { |
9e5dc77f | 2742 | tree op0 = instantiate_scev_r (instantiate_below, evolution_loop, |
8b679c9b | 2743 | inner_loop, TREE_OPERAND (chrec, 0), |
0547c9b6 | 2744 | fold_conversions, size_expr); |
9e5dc77f SP |
2745 | |
2746 | if (op0 == chrec_dont_know) | |
2747 | return chrec_dont_know; | |
2748 | ||
2749 | if (op0 == TREE_OPERAND (chrec, 0)) | |
2750 | return chrec; | |
2751 | ||
2752 | return fold_build1 (TREE_CODE (chrec), TREE_TYPE (chrec), op0); | |
2753 | } | |
2754 | ||
2755 | /* Analyze all the parameters of the chrec, between INSTANTIATE_BELOW | |
2756 | and EVOLUTION_LOOP, that were left under a symbolic form. | |
2757 | ||
2758 | CHREC is the scalar evolution to instantiate. | |
2759 | ||
2760 | CACHE is the cache of already instantiated values. | |
2282a0e6 | 2761 | |
c70ed622 BC |
2762 | Variable pointed by FOLD_CONVERSIONS is set to TRUE when the |
2763 | conversions that may wrap in signed/pointer type are folded, as long | |
2764 | as the value of the chrec is preserved. If FOLD_CONVERSIONS is NULL | |
2765 | then we don't do such fold. | |
9e5dc77f SP |
2766 | |
2767 | SIZE_EXPR is used for computing the size of the expression to be | |
2768 | instantiated, and to stop if it exceeds some limit. */ | |
2769 | ||
2770 | static tree | |
2771 | instantiate_scev_r (basic_block instantiate_below, | |
8b679c9b RB |
2772 | struct loop *evolution_loop, struct loop *inner_loop, |
2773 | tree chrec, | |
c70ed622 | 2774 | bool *fold_conversions, int size_expr) |
9e5dc77f | 2775 | { |
47ae9e4c SP |
2776 | /* Give up if the expression is larger than the MAX that we allow. */ |
2777 | if (size_expr++ > PARAM_VALUE (PARAM_SCEV_MAX_EXPR_SIZE)) | |
2778 | return chrec_dont_know; | |
2779 | ||
81fada9a JJ |
2780 | if (chrec == NULL_TREE |
2781 | || automatically_generated_chrec_p (chrec) | |
d7770457 | 2782 | || is_gimple_min_invariant (chrec)) |
9baba81b SP |
2783 | return chrec; |
2784 | ||
2785 | switch (TREE_CODE (chrec)) | |
2786 | { | |
2787 | case SSA_NAME: | |
8b679c9b RB |
2788 | return instantiate_scev_name (instantiate_below, evolution_loop, |
2789 | inner_loop, chrec, | |
0547c9b6 | 2790 | fold_conversions, size_expr); |
9baba81b SP |
2791 | |
2792 | case POLYNOMIAL_CHREC: | |
8b679c9b RB |
2793 | return instantiate_scev_poly (instantiate_below, evolution_loop, |
2794 | inner_loop, chrec, | |
0547c9b6 | 2795 | fold_conversions, size_expr); |
9baba81b | 2796 | |
5be014d5 | 2797 | case POINTER_PLUS_EXPR: |
9baba81b | 2798 | case PLUS_EXPR: |
9baba81b | 2799 | case MINUS_EXPR: |
9baba81b | 2800 | case MULT_EXPR: |
8b679c9b RB |
2801 | return instantiate_scev_binary (instantiate_below, evolution_loop, |
2802 | inner_loop, chrec, | |
ffa34f4b SP |
2803 | TREE_CODE (chrec), chrec_type (chrec), |
2804 | TREE_OPERAND (chrec, 0), | |
2805 | TREE_OPERAND (chrec, 1), | |
0547c9b6 | 2806 | fold_conversions, size_expr); |
9baba81b | 2807 | |
1043771b | 2808 | CASE_CONVERT: |
8b679c9b RB |
2809 | return instantiate_scev_convert (instantiate_below, evolution_loop, |
2810 | inner_loop, chrec, | |
9c382ce9 | 2811 | TREE_TYPE (chrec), TREE_OPERAND (chrec, 0), |
0547c9b6 | 2812 | fold_conversions, size_expr); |
9baba81b | 2813 | |
4b9d48a1 | 2814 | case NEGATE_EXPR: |
418df9d7 | 2815 | case BIT_NOT_EXPR: |
8b679c9b RB |
2816 | return instantiate_scev_not (instantiate_below, evolution_loop, |
2817 | inner_loop, chrec, | |
20179b0d SP |
2818 | TREE_CODE (chrec), TREE_TYPE (chrec), |
2819 | TREE_OPERAND (chrec, 0), | |
0547c9b6 | 2820 | fold_conversions, size_expr); |
418df9d7 | 2821 | |
4c7d6755 | 2822 | case ADDR_EXPR: |
9baba81b SP |
2823 | case SCEV_NOT_KNOWN: |
2824 | return chrec_dont_know; | |
2825 | ||
2826 | case SCEV_KNOWN: | |
2827 | return chrec_known; | |
15fda317 | 2828 | |
dbc08079 | 2829 | case ARRAY_REF: |
8b679c9b RB |
2830 | return instantiate_array_ref (instantiate_below, evolution_loop, |
2831 | inner_loop, chrec, | |
0547c9b6 | 2832 | fold_conversions, size_expr); |
dbc08079 | 2833 | |
9baba81b SP |
2834 | default: |
2835 | break; | |
2836 | } | |
2837 | ||
0dfb0dc6 SP |
2838 | if (VL_EXP_CLASS_P (chrec)) |
2839 | return chrec_dont_know; | |
2840 | ||
9baba81b SP |
2841 | switch (TREE_CODE_LENGTH (TREE_CODE (chrec))) |
2842 | { | |
2843 | case 3: | |
8b679c9b RB |
2844 | return instantiate_scev_3 (instantiate_below, evolution_loop, |
2845 | inner_loop, chrec, | |
0547c9b6 | 2846 | fold_conversions, size_expr); |
9baba81b SP |
2847 | |
2848 | case 2: | |
8b679c9b RB |
2849 | return instantiate_scev_2 (instantiate_below, evolution_loop, |
2850 | inner_loop, chrec, | |
0547c9b6 | 2851 | fold_conversions, size_expr); |
7ec0665d | 2852 | |
9baba81b | 2853 | case 1: |
8b679c9b RB |
2854 | return instantiate_scev_1 (instantiate_below, evolution_loop, |
2855 | inner_loop, chrec, | |
0547c9b6 | 2856 | fold_conversions, size_expr); |
9baba81b SP |
2857 | |
2858 | case 0: | |
2859 | return chrec; | |
2860 | ||
2861 | default: | |
2862 | break; | |
2863 | } | |
2864 | ||
2865 | /* Too complicated to handle. */ | |
2866 | return chrec_dont_know; | |
2867 | } | |
e9eb809d ZD |
2868 | |
2869 | /* Analyze all the parameters of the chrec that were left under a | |
a213b219 SP |
2870 | symbolic form. INSTANTIATE_BELOW is the basic block that stops the |
2871 | recursive instantiation of parameters: a parameter is a variable | |
2872 | that is defined in a basic block that dominates INSTANTIATE_BELOW or | |
2873 | a function parameter. */ | |
e9eb809d ZD |
2874 | |
2875 | tree | |
a213b219 | 2876 | instantiate_scev (basic_block instantiate_below, struct loop *evolution_loop, |
3f227a8c | 2877 | tree chrec) |
e9eb809d | 2878 | { |
9baba81b SP |
2879 | tree res; |
2880 | ||
dfedbe40 | 2881 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b | 2882 | { |
3f227a8c | 2883 | fprintf (dump_file, "(instantiate_scev \n"); |
a213b219 | 2884 | fprintf (dump_file, " (instantiate_below = %d)\n", instantiate_below->index); |
3f227a8c | 2885 | fprintf (dump_file, " (evolution_loop = %d)\n", evolution_loop->num); |
9baba81b SP |
2886 | fprintf (dump_file, " (chrec = "); |
2887 | print_generic_expr (dump_file, chrec, 0); | |
2888 | fprintf (dump_file, ")\n"); | |
2889 | } | |
b8698a0f | 2890 | |
0547c9b6 RB |
2891 | bool destr = false; |
2892 | if (!global_cache) | |
2893 | { | |
2894 | global_cache = new instantiate_cache_type; | |
2895 | destr = true; | |
2896 | } | |
2897 | ||
8b679c9b | 2898 | res = instantiate_scev_r (instantiate_below, evolution_loop, |
c70ed622 | 2899 | NULL, chrec, NULL, 0); |
0547c9b6 RB |
2900 | |
2901 | if (destr) | |
2902 | { | |
2903 | delete global_cache; | |
2904 | global_cache = NULL; | |
2905 | } | |
9baba81b | 2906 | |
dfedbe40 | 2907 | if (dump_file && (dump_flags & TDF_SCEV)) |
9baba81b SP |
2908 | { |
2909 | fprintf (dump_file, " (res = "); | |
2910 | print_generic_expr (dump_file, res, 0); | |
2911 | fprintf (dump_file, "))\n"); | |
2912 | } | |
eb0bc7af | 2913 | |
9baba81b SP |
2914 | return res; |
2915 | } | |
2916 | ||
2917 | /* Similar to instantiate_parameters, but does not introduce the | |
2282a0e6 ZD |
2918 | evolutions in outer loops for LOOP invariants in CHREC, and does not |
2919 | care about causing overflows, as long as they do not affect value | |
2920 | of an expression. */ | |
9baba81b | 2921 | |
3cb960c7 | 2922 | tree |
c70ed622 | 2923 | resolve_mixers (struct loop *loop, tree chrec, bool *folded_casts) |
9baba81b | 2924 | { |
0547c9b6 | 2925 | bool destr = false; |
c70ed622 | 2926 | bool fold_conversions = false; |
0547c9b6 RB |
2927 | if (!global_cache) |
2928 | { | |
2929 | global_cache = new instantiate_cache_type; | |
2930 | destr = true; | |
2931 | } | |
2932 | ||
8b679c9b | 2933 | tree ret = instantiate_scev_r (block_before_loop (loop), loop, NULL, |
c70ed622 BC |
2934 | chrec, &fold_conversions, 0); |
2935 | ||
2936 | if (folded_casts && !*folded_casts) | |
2937 | *folded_casts = fold_conversions; | |
0547c9b6 RB |
2938 | |
2939 | if (destr) | |
2940 | { | |
2941 | delete global_cache; | |
2942 | global_cache = NULL; | |
2943 | } | |
2944 | ||
eb0bc7af | 2945 | return ret; |
9baba81b SP |
2946 | } |
2947 | ||
b8698a0f | 2948 | /* Entry point for the analysis of the number of iterations pass. |
9baba81b SP |
2949 | This function tries to safely approximate the number of iterations |
2950 | the loop will run. When this property is not decidable at compile | |
0a74c758 SP |
2951 | time, the result is chrec_dont_know. Otherwise the result is a |
2952 | scalar or a symbolic parameter. When the number of iterations may | |
2953 | be equal to zero and the property cannot be determined at compile | |
2954 | time, the result is a COND_EXPR that represents in a symbolic form | |
2955 | the conditions under which the number of iterations is not zero. | |
b8698a0f | 2956 | |
9baba81b | 2957 | Example of analysis: suppose that the loop has an exit condition: |
b8698a0f | 2958 | |
9baba81b | 2959 | "if (b > 49) goto end_loop;" |
b8698a0f | 2960 | |
9baba81b SP |
2961 | and that in a previous analysis we have determined that the |
2962 | variable 'b' has an evolution function: | |
b8698a0f L |
2963 | |
2964 | "EF = {23, +, 5}_2". | |
2965 | ||
9baba81b SP |
2966 | When we evaluate the function at the point 5, i.e. the value of the |
2967 | variable 'b' after 5 iterations in the loop, we have EF (5) = 48, | |
2968 | and EF (6) = 53. In this case the value of 'b' on exit is '53' and | |
2969 | the loop body has been executed 6 times. */ | |
2970 | ||
b8698a0f | 2971 | tree |
a14865db | 2972 | number_of_latch_executions (struct loop *loop) |
9baba81b | 2973 | { |
9baba81b SP |
2974 | edge exit; |
2975 | struct tree_niter_desc niter_desc; | |
0a74c758 SP |
2976 | tree may_be_zero; |
2977 | tree res; | |
9baba81b | 2978 | |
0a74c758 | 2979 | /* Determine whether the number of iterations in loop has already |
9baba81b SP |
2980 | been computed. */ |
2981 | res = loop->nb_iterations; | |
2982 | if (res) | |
2983 | return res; | |
0a74c758 SP |
2984 | |
2985 | may_be_zero = NULL_TREE; | |
9baba81b | 2986 | |
dfedbe40 | 2987 | if (dump_file && (dump_flags & TDF_SCEV)) |
0a74c758 | 2988 | fprintf (dump_file, "(number_of_iterations_in_loop = \n"); |
b8698a0f | 2989 | |
0a74c758 | 2990 | res = chrec_dont_know; |
ac8f6c69 | 2991 | exit = single_exit (loop); |
9baba81b | 2992 | |
0a74c758 SP |
2993 | if (exit && number_of_iterations_exit (loop, exit, &niter_desc, false)) |
2994 | { | |
2995 | may_be_zero = niter_desc.may_be_zero; | |
2996 | res = niter_desc.niter; | |
2997 | } | |
2998 | ||
2999 | if (res == chrec_dont_know | |
3000 | || !may_be_zero | |
3001 | || integer_zerop (may_be_zero)) | |
3002 | ; | |
3003 | else if (integer_nonzerop (may_be_zero)) | |
3004 | res = build_int_cst (TREE_TYPE (res), 0); | |
9baba81b | 3005 | |
0a74c758 SP |
3006 | else if (COMPARISON_CLASS_P (may_be_zero)) |
3007 | res = fold_build3 (COND_EXPR, TREE_TYPE (res), may_be_zero, | |
3008 | build_int_cst (TREE_TYPE (res), 0), res); | |
9baba81b SP |
3009 | else |
3010 | res = chrec_dont_know; | |
3011 | ||
dfedbe40 | 3012 | if (dump_file && (dump_flags & TDF_SCEV)) |
0a74c758 SP |
3013 | { |
3014 | fprintf (dump_file, " (set_nb_iterations_in_loop = "); | |
3015 | print_generic_expr (dump_file, res, 0); | |
3016 | fprintf (dump_file, "))\n"); | |
3017 | } | |
3018 | ||
3019 | loop->nb_iterations = res; | |
3020 | return res; | |
9baba81b | 3021 | } |
9baba81b SP |
3022 | \f |
3023 | ||
3024 | /* Counters for the stats. */ | |
3025 | ||
b8698a0f | 3026 | struct chrec_stats |
9baba81b SP |
3027 | { |
3028 | unsigned nb_chrecs; | |
3029 | unsigned nb_affine; | |
3030 | unsigned nb_affine_multivar; | |
3031 | unsigned nb_higher_poly; | |
3032 | unsigned nb_chrec_dont_know; | |
3033 | unsigned nb_undetermined; | |
3034 | }; | |
3035 | ||
3036 | /* Reset the counters. */ | |
3037 | ||
3038 | static inline void | |
3039 | reset_chrecs_counters (struct chrec_stats *stats) | |
3040 | { | |
3041 | stats->nb_chrecs = 0; | |
3042 | stats->nb_affine = 0; | |
3043 | stats->nb_affine_multivar = 0; | |
3044 | stats->nb_higher_poly = 0; | |
3045 | stats->nb_chrec_dont_know = 0; | |
3046 | stats->nb_undetermined = 0; | |
3047 | } | |
3048 | ||
3049 | /* Dump the contents of a CHREC_STATS structure. */ | |
3050 | ||
3051 | static void | |
3052 | dump_chrecs_stats (FILE *file, struct chrec_stats *stats) | |
3053 | { | |
3054 | fprintf (file, "\n(\n"); | |
3055 | fprintf (file, "-----------------------------------------\n"); | |
3056 | fprintf (file, "%d\taffine univariate chrecs\n", stats->nb_affine); | |
3057 | fprintf (file, "%d\taffine multivariate chrecs\n", stats->nb_affine_multivar); | |
b8698a0f | 3058 | fprintf (file, "%d\tdegree greater than 2 polynomials\n", |
9baba81b SP |
3059 | stats->nb_higher_poly); |
3060 | fprintf (file, "%d\tchrec_dont_know chrecs\n", stats->nb_chrec_dont_know); | |
3061 | fprintf (file, "-----------------------------------------\n"); | |
3062 | fprintf (file, "%d\ttotal chrecs\n", stats->nb_chrecs); | |
b8698a0f | 3063 | fprintf (file, "%d\twith undetermined coefficients\n", |
9baba81b SP |
3064 | stats->nb_undetermined); |
3065 | fprintf (file, "-----------------------------------------\n"); | |
b8698a0f | 3066 | fprintf (file, "%d\tchrecs in the scev database\n", |
907dadbd | 3067 | (int) scalar_evolution_info->elements ()); |
9baba81b SP |
3068 | fprintf (file, "%d\tsets in the scev database\n", nb_set_scev); |
3069 | fprintf (file, "%d\tgets in the scev database\n", nb_get_scev); | |
3070 | fprintf (file, "-----------------------------------------\n"); | |
3071 | fprintf (file, ")\n\n"); | |
3072 | } | |
3073 | ||
3074 | /* Gather statistics about CHREC. */ | |
3075 | ||
3076 | static void | |
3077 | gather_chrec_stats (tree chrec, struct chrec_stats *stats) | |
3078 | { | |
3079 | if (dump_file && (dump_flags & TDF_STATS)) | |
3080 | { | |
3081 | fprintf (dump_file, "(classify_chrec "); | |
3082 | print_generic_expr (dump_file, chrec, 0); | |
3083 | fprintf (dump_file, "\n"); | |
3084 | } | |
b8698a0f | 3085 | |
9baba81b | 3086 | stats->nb_chrecs++; |
b8698a0f | 3087 | |
9baba81b SP |
3088 | if (chrec == NULL_TREE) |
3089 | { | |
3090 | stats->nb_undetermined++; | |
3091 | return; | |
3092 | } | |
b8698a0f | 3093 | |
9baba81b SP |
3094 | switch (TREE_CODE (chrec)) |
3095 | { | |
3096 | case POLYNOMIAL_CHREC: | |
3097 | if (evolution_function_is_affine_p (chrec)) | |
3098 | { | |
3099 | if (dump_file && (dump_flags & TDF_STATS)) | |
3100 | fprintf (dump_file, " affine_univariate\n"); | |
3101 | stats->nb_affine++; | |
3102 | } | |
a50411de | 3103 | else if (evolution_function_is_affine_multivariate_p (chrec, 0)) |
9baba81b SP |
3104 | { |
3105 | if (dump_file && (dump_flags & TDF_STATS)) | |
3106 | fprintf (dump_file, " affine_multivariate\n"); | |
3107 | stats->nb_affine_multivar++; | |
3108 | } | |
3109 | else | |
3110 | { | |
3111 | if (dump_file && (dump_flags & TDF_STATS)) | |
3112 | fprintf (dump_file, " higher_degree_polynomial\n"); | |
3113 | stats->nb_higher_poly++; | |
3114 | } | |
b8698a0f | 3115 | |
9baba81b SP |
3116 | break; |
3117 | ||
3118 | default: | |
3119 | break; | |
3120 | } | |
b8698a0f | 3121 | |
9baba81b SP |
3122 | if (chrec_contains_undetermined (chrec)) |
3123 | { | |
3124 | if (dump_file && (dump_flags & TDF_STATS)) | |
3125 | fprintf (dump_file, " undetermined\n"); | |
3126 | stats->nb_undetermined++; | |
3127 | } | |
b8698a0f | 3128 | |
9baba81b SP |
3129 | if (dump_file && (dump_flags & TDF_STATS)) |
3130 | fprintf (dump_file, ")\n"); | |
3131 | } | |
3132 | ||
9baba81b SP |
3133 | /* Classify the chrecs of the whole database. */ |
3134 | ||
b8698a0f | 3135 | void |
9baba81b SP |
3136 | gather_stats_on_scev_database (void) |
3137 | { | |
3138 | struct chrec_stats stats; | |
b8698a0f | 3139 | |
9baba81b SP |
3140 | if (!dump_file) |
3141 | return; | |
b8698a0f | 3142 | |
9baba81b | 3143 | reset_chrecs_counters (&stats); |
b8698a0f | 3144 | |
907dadbd TS |
3145 | hash_table<scev_info_hasher>::iterator iter; |
3146 | scev_info_str *elt; | |
3147 | FOR_EACH_HASH_TABLE_ELEMENT (*scalar_evolution_info, elt, scev_info_str *, | |
3148 | iter) | |
3149 | gather_chrec_stats (elt->chrec, &stats); | |
9baba81b SP |
3150 | |
3151 | dump_chrecs_stats (dump_file, &stats); | |
3152 | } | |
3153 | ||
3154 | \f | |
3155 | ||
3156 | /* Initializer. */ | |
3157 | ||
3158 | static void | |
3159 | initialize_scalar_evolutions_analyzer (void) | |
3160 | { | |
3161 | /* The elements below are unique. */ | |
3162 | if (chrec_dont_know == NULL_TREE) | |
3163 | { | |
3164 | chrec_not_analyzed_yet = NULL_TREE; | |
3165 | chrec_dont_know = make_node (SCEV_NOT_KNOWN); | |
3166 | chrec_known = make_node (SCEV_KNOWN); | |
d5ab5675 ZD |
3167 | TREE_TYPE (chrec_dont_know) = void_type_node; |
3168 | TREE_TYPE (chrec_known) = void_type_node; | |
9baba81b SP |
3169 | } |
3170 | } | |
3171 | ||
3172 | /* Initialize the analysis of scalar evolutions for LOOPS. */ | |
3173 | ||
3174 | void | |
d73be268 | 3175 | scev_initialize (void) |
9baba81b | 3176 | { |
42fd6772 | 3177 | struct loop *loop; |
9baba81b | 3178 | |
907dadbd | 3179 | scalar_evolution_info = hash_table<scev_info_hasher>::create_ggc (100); |
b8698a0f | 3180 | |
9baba81b SP |
3181 | initialize_scalar_evolutions_analyzer (); |
3182 | ||
f0bd40b1 | 3183 | FOR_EACH_LOOP (loop, 0) |
42fd6772 ZD |
3184 | { |
3185 | loop->nb_iterations = NULL_TREE; | |
3186 | } | |
9baba81b SP |
3187 | } |
3188 | ||
e3a8f1fa JH |
3189 | /* Return true if SCEV is initialized. */ |
3190 | ||
3191 | bool | |
3192 | scev_initialized_p (void) | |
3193 | { | |
3194 | return scalar_evolution_info != NULL; | |
3195 | } | |
3196 | ||
a7bf45de SP |
3197 | /* Cleans up the information cached by the scalar evolutions analysis |
3198 | in the hash table. */ | |
3199 | ||
3200 | void | |
3201 | scev_reset_htab (void) | |
3202 | { | |
3203 | if (!scalar_evolution_info) | |
3204 | return; | |
3205 | ||
907dadbd | 3206 | scalar_evolution_info->empty (); |
a7bf45de SP |
3207 | } |
3208 | ||
3209 | /* Cleans up the information cached by the scalar evolutions analysis | |
3210 | in the hash table and in the loop->nb_iterations. */ | |
9baba81b SP |
3211 | |
3212 | void | |
3213 | scev_reset (void) | |
3214 | { | |
9baba81b SP |
3215 | struct loop *loop; |
3216 | ||
a7bf45de SP |
3217 | scev_reset_htab (); |
3218 | ||
f0bd40b1 | 3219 | FOR_EACH_LOOP (loop, 0) |
9baba81b | 3220 | { |
42fd6772 | 3221 | loop->nb_iterations = NULL_TREE; |
9baba81b | 3222 | } |
e9eb809d ZD |
3223 | } |
3224 | ||
f017bf5e ZD |
3225 | /* Checks whether use of OP in USE_LOOP behaves as a simple affine iv with |
3226 | respect to WRTO_LOOP and returns its base and step in IV if possible | |
3227 | (see analyze_scalar_evolution_in_loop for more details on USE_LOOP | |
3228 | and WRTO_LOOP). If ALLOW_NONCONSTANT_STEP is true, we want step to be | |
3229 | invariant in LOOP. Otherwise we require it to be an integer constant. | |
b8698a0f | 3230 | |
f017bf5e ZD |
3231 | IV->no_overflow is set to true if we are sure the iv cannot overflow (e.g. |
3232 | because it is computed in signed arithmetics). Consequently, adding an | |
3233 | induction variable | |
b8698a0f | 3234 | |
f017bf5e ZD |
3235 | for (i = IV->base; ; i += IV->step) |
3236 | ||
3237 | is only safe if IV->no_overflow is false, or TYPE_OVERFLOW_UNDEFINED is | |
3238 | false for the type of the induction variable, or you can prove that i does | |
3239 | not wrap by some other argument. Otherwise, this might introduce undefined | |
3240 | behavior, and | |
b8698a0f | 3241 | |
f017bf5e ZD |
3242 | for (i = iv->base; ; i = (type) ((unsigned type) i + (unsigned type) iv->step)) |
3243 | ||
3244 | must be used instead. */ | |
e9eb809d ZD |
3245 | |
3246 | bool | |
f017bf5e ZD |
3247 | simple_iv (struct loop *wrto_loop, struct loop *use_loop, tree op, |
3248 | affine_iv *iv, bool allow_nonconstant_step) | |
e9eb809d | 3249 | { |
f3c5f3a3 BC |
3250 | enum tree_code code; |
3251 | tree type, ev, base, e, stop; | |
3252 | wide_int extreme; | |
3253 | bool folded_casts, overflow; | |
9baba81b | 3254 | |
a6f778b2 ZD |
3255 | iv->base = NULL_TREE; |
3256 | iv->step = NULL_TREE; | |
3257 | iv->no_overflow = false; | |
9baba81b SP |
3258 | |
3259 | type = TREE_TYPE (op); | |
1ee0d660 EB |
3260 | if (!POINTER_TYPE_P (type) |
3261 | && !INTEGRAL_TYPE_P (type)) | |
9baba81b SP |
3262 | return false; |
3263 | ||
f017bf5e | 3264 | ev = analyze_scalar_evolution_in_loop (wrto_loop, use_loop, op, |
a6f778b2 | 3265 | &folded_casts); |
f017bf5e ZD |
3266 | if (chrec_contains_undetermined (ev) |
3267 | || chrec_contains_symbols_defined_in_loop (ev, wrto_loop->num)) | |
9baba81b SP |
3268 | return false; |
3269 | ||
f017bf5e | 3270 | if (tree_does_not_contain_chrecs (ev)) |
9baba81b | 3271 | { |
a6f778b2 | 3272 | iv->base = ev; |
6e42ce54 | 3273 | iv->step = build_int_cst (TREE_TYPE (ev), 0); |
a6f778b2 | 3274 | iv->no_overflow = true; |
9baba81b SP |
3275 | return true; |
3276 | } | |
3277 | ||
3278 | if (TREE_CODE (ev) != POLYNOMIAL_CHREC | |
f017bf5e | 3279 | || CHREC_VARIABLE (ev) != (unsigned) wrto_loop->num) |
9baba81b SP |
3280 | return false; |
3281 | ||
a6f778b2 | 3282 | iv->step = CHREC_RIGHT (ev); |
f017bf5e ZD |
3283 | if ((!allow_nonconstant_step && TREE_CODE (iv->step) != INTEGER_CST) |
3284 | || tree_contains_chrecs (iv->step, NULL)) | |
9baba81b | 3285 | return false; |
9be872b7 | 3286 | |
a6f778b2 | 3287 | iv->base = CHREC_LEFT (ev); |
f017bf5e | 3288 | if (tree_contains_chrecs (iv->base, NULL)) |
9baba81b SP |
3289 | return false; |
3290 | ||
20bd649a MP |
3291 | iv->no_overflow = (!folded_casts && ANY_INTEGRAL_TYPE_P (type) |
3292 | && TYPE_OVERFLOW_UNDEFINED (type)); | |
eeef0e45 | 3293 | |
f3c5f3a3 BC |
3294 | /* Try to simplify iv base: |
3295 | ||
3296 | (signed T) ((unsigned T)base + step) ;; TREE_TYPE (base) == signed T | |
3297 | == (signed T)(unsigned T)base + step | |
3298 | == base + step | |
3299 | ||
3300 | If we can prove operation (base + step) doesn't overflow or underflow. | |
3301 | Specifically, we try to prove below conditions are satisfied: | |
3302 | ||
3303 | base <= UPPER_BOUND (type) - step ;;step > 0 | |
3304 | base >= LOWER_BOUND (type) - step ;;step < 0 | |
3305 | ||
3306 | This is done by proving the reverse conditions are false using loop's | |
3307 | initial conditions. | |
3308 | ||
3309 | The is necessary to make loop niter, or iv overflow analysis easier | |
3310 | for below example: | |
3311 | ||
3312 | int foo (int *a, signed char s, signed char l) | |
3313 | { | |
3314 | signed char i; | |
3315 | for (i = s; i < l; i++) | |
3316 | a[i] = 0; | |
3317 | return 0; | |
3318 | } | |
3319 | ||
3320 | Note variable I is firstly converted to type unsigned char, incremented, | |
3321 | then converted back to type signed char. */ | |
3322 | ||
3323 | if (wrto_loop->num != use_loop->num) | |
3324 | return true; | |
3325 | ||
3326 | if (!CONVERT_EXPR_P (iv->base) || TREE_CODE (iv->step) != INTEGER_CST) | |
3327 | return true; | |
3328 | ||
3329 | type = TREE_TYPE (iv->base); | |
3330 | e = TREE_OPERAND (iv->base, 0); | |
3331 | if (TREE_CODE (e) != PLUS_EXPR | |
3332 | || TREE_CODE (TREE_OPERAND (e, 1)) != INTEGER_CST | |
3333 | || !tree_int_cst_equal (iv->step, | |
3334 | fold_convert (type, TREE_OPERAND (e, 1)))) | |
3335 | return true; | |
3336 | e = TREE_OPERAND (e, 0); | |
3337 | if (!CONVERT_EXPR_P (e)) | |
3338 | return true; | |
3339 | base = TREE_OPERAND (e, 0); | |
3340 | if (!useless_type_conversion_p (type, TREE_TYPE (base))) | |
3341 | return true; | |
3342 | ||
3343 | if (tree_int_cst_sign_bit (iv->step)) | |
3344 | { | |
3345 | code = LT_EXPR; | |
3346 | extreme = wi::min_value (type); | |
3347 | } | |
3348 | else | |
3349 | { | |
3350 | code = GT_EXPR; | |
3351 | extreme = wi::max_value (type); | |
3352 | } | |
3353 | overflow = false; | |
3354 | extreme = wi::sub (extreme, iv->step, TYPE_SIGN (type), &overflow); | |
3355 | if (overflow) | |
3356 | return true; | |
3357 | e = fold_build2 (code, boolean_type_node, base, | |
3358 | wide_int_to_tree (type, extreme)); | |
3359 | stop = (TREE_CODE (base) == SSA_NAME) ? base : NULL; | |
3360 | e = simplify_using_initial_conditions (use_loop, e, stop); | |
3361 | if (!integer_zerop (e)) | |
3362 | return true; | |
3363 | ||
3364 | if (POINTER_TYPE_P (TREE_TYPE (base))) | |
3365 | code = POINTER_PLUS_EXPR; | |
3366 | else | |
3367 | code = PLUS_EXPR; | |
3368 | ||
3369 | iv->base = fold_build2 (code, TREE_TYPE (base), base, iv->step); | |
9baba81b SP |
3370 | return true; |
3371 | } | |
3372 | ||
9baba81b SP |
3373 | /* Finalize the scalar evolution analysis. */ |
3374 | ||
3375 | void | |
3376 | scev_finalize (void) | |
3377 | { | |
d51157de ZD |
3378 | if (!scalar_evolution_info) |
3379 | return; | |
907dadbd | 3380 | scalar_evolution_info->empty (); |
c7b852c8 | 3381 | scalar_evolution_info = NULL; |
9baba81b SP |
3382 | } |
3383 | ||
771f882e ZD |
3384 | /* Returns true if the expression EXPR is considered to be too expensive |
3385 | for scev_const_prop. */ | |
3386 | ||
3387 | bool | |
3388 | expression_expensive_p (tree expr) | |
3389 | { | |
3390 | enum tree_code code; | |
3391 | ||
3392 | if (is_gimple_val (expr)) | |
3393 | return false; | |
3394 | ||
3395 | code = TREE_CODE (expr); | |
3396 | if (code == TRUNC_DIV_EXPR | |
3397 | || code == CEIL_DIV_EXPR | |
3398 | || code == FLOOR_DIV_EXPR | |
3399 | || code == ROUND_DIV_EXPR | |
3400 | || code == TRUNC_MOD_EXPR | |
3401 | || code == CEIL_MOD_EXPR | |
3402 | || code == FLOOR_MOD_EXPR | |
3403 | || code == ROUND_MOD_EXPR | |
3404 | || code == EXACT_DIV_EXPR) | |
3405 | { | |
3406 | /* Division by power of two is usually cheap, so we allow it. | |
3407 | Forbid anything else. */ | |
3408 | if (!integer_pow2p (TREE_OPERAND (expr, 1))) | |
3409 | return true; | |
3410 | } | |
3411 | ||
3412 | switch (TREE_CODE_CLASS (code)) | |
3413 | { | |
3414 | case tcc_binary: | |
3415 | case tcc_comparison: | |
3416 | if (expression_expensive_p (TREE_OPERAND (expr, 1))) | |
3417 | return true; | |
3418 | ||
3419 | /* Fallthru. */ | |
3420 | case tcc_unary: | |
3421 | return expression_expensive_p (TREE_OPERAND (expr, 0)); | |
3422 | ||
3423 | default: | |
3424 | return true; | |
3425 | } | |
3426 | } | |
3427 | ||
684aaf29 | 3428 | /* Replace ssa names for that scev can prove they are constant by the |
3ac01fde ZD |
3429 | appropriate constants. Also perform final value replacement in loops, |
3430 | in case the replacement expressions are cheap. | |
b8698a0f | 3431 | |
684aaf29 ZD |
3432 | We only consider SSA names defined by phi nodes; rest is left to the |
3433 | ordinary constant propagation pass. */ | |
3434 | ||
c2924966 | 3435 | unsigned int |
684aaf29 ZD |
3436 | scev_const_prop (void) |
3437 | { | |
3438 | basic_block bb; | |
726a989a | 3439 | tree name, type, ev; |
538dd0b7 DM |
3440 | gphi *phi; |
3441 | gassign *ass; | |
3ac01fde | 3442 | struct loop *loop, *ex_loop; |
684aaf29 | 3443 | bitmap ssa_names_to_remove = NULL; |
3ac01fde | 3444 | unsigned i; |
538dd0b7 | 3445 | gphi_iterator psi; |
684aaf29 | 3446 | |
0fc822d0 | 3447 | if (number_of_loops (cfun) <= 1) |
c2924966 | 3448 | return 0; |
684aaf29 | 3449 | |
11cd3bed | 3450 | FOR_EACH_BB_FN (bb, cfun) |
684aaf29 ZD |
3451 | { |
3452 | loop = bb->loop_father; | |
3453 | ||
726a989a | 3454 | for (psi = gsi_start_phis (bb); !gsi_end_p (psi); gsi_next (&psi)) |
684aaf29 | 3455 | { |
538dd0b7 | 3456 | phi = psi.phi (); |
684aaf29 ZD |
3457 | name = PHI_RESULT (phi); |
3458 | ||
ea057359 | 3459 | if (virtual_operand_p (name)) |
684aaf29 ZD |
3460 | continue; |
3461 | ||
3462 | type = TREE_TYPE (name); | |
3463 | ||
3464 | if (!POINTER_TYPE_P (type) | |
3465 | && !INTEGRAL_TYPE_P (type)) | |
3466 | continue; | |
3467 | ||
c70ed622 BC |
3468 | ev = resolve_mixers (loop, analyze_scalar_evolution (loop, name), |
3469 | NULL); | |
684aaf29 ZD |
3470 | if (!is_gimple_min_invariant (ev) |
3471 | || !may_propagate_copy (name, ev)) | |
3472 | continue; | |
3473 | ||
3474 | /* Replace the uses of the name. */ | |
18aed06a SP |
3475 | if (name != ev) |
3476 | replace_uses_by (name, ev); | |
684aaf29 ZD |
3477 | |
3478 | if (!ssa_names_to_remove) | |
3479 | ssa_names_to_remove = BITMAP_ALLOC (NULL); | |
3480 | bitmap_set_bit (ssa_names_to_remove, SSA_NAME_VERSION (name)); | |
3481 | } | |
3482 | } | |
3483 | ||
9b3b55a1 DN |
3484 | /* Remove the ssa names that were replaced by constants. We do not |
3485 | remove them directly in the previous cycle, since this | |
3486 | invalidates scev cache. */ | |
684aaf29 ZD |
3487 | if (ssa_names_to_remove) |
3488 | { | |
3489 | bitmap_iterator bi; | |
684aaf29 ZD |
3490 | |
3491 | EXECUTE_IF_SET_IN_BITMAP (ssa_names_to_remove, 0, i, bi) | |
3492 | { | |
726a989a | 3493 | gimple_stmt_iterator psi; |
684aaf29 | 3494 | name = ssa_name (i); |
538dd0b7 | 3495 | phi = as_a <gphi *> (SSA_NAME_DEF_STMT (name)); |
684aaf29 | 3496 | |
726a989a RB |
3497 | gcc_assert (gimple_code (phi) == GIMPLE_PHI); |
3498 | psi = gsi_for_stmt (phi); | |
3499 | remove_phi_node (&psi, true); | |
684aaf29 ZD |
3500 | } |
3501 | ||
3502 | BITMAP_FREE (ssa_names_to_remove); | |
3503 | scev_reset (); | |
3504 | } | |
3ac01fde ZD |
3505 | |
3506 | /* Now the regular final value replacement. */ | |
f0bd40b1 | 3507 | FOR_EACH_LOOP (loop, LI_FROM_INNERMOST) |
3ac01fde ZD |
3508 | { |
3509 | edge exit; | |
726a989a | 3510 | tree def, rslt, niter; |
19e51b40 | 3511 | gimple_stmt_iterator gsi; |
3ac01fde | 3512 | |
3ac01fde ZD |
3513 | /* If we do not know exact number of iterations of the loop, we cannot |
3514 | replace the final value. */ | |
ac8f6c69 | 3515 | exit = single_exit (loop); |
a6f778b2 ZD |
3516 | if (!exit) |
3517 | continue; | |
3518 | ||
a14865db | 3519 | niter = number_of_latch_executions (loop); |
b3ce5b6e | 3520 | if (niter == chrec_dont_know) |
3ac01fde | 3521 | continue; |
925196ed ZD |
3522 | |
3523 | /* Ensure that it is possible to insert new statements somewhere. */ | |
3524 | if (!single_pred_p (exit->dest)) | |
3525 | split_loop_exit_edge (exit); | |
19e51b40 | 3526 | gsi = gsi_after_labels (exit->dest); |
925196ed | 3527 | |
9ba025a2 ZD |
3528 | ex_loop = superloop_at_depth (loop, |
3529 | loop_depth (exit->dest->loop_father) + 1); | |
3ac01fde | 3530 | |
726a989a | 3531 | for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); ) |
3ac01fde | 3532 | { |
538dd0b7 | 3533 | phi = psi.phi (); |
925196ed | 3534 | rslt = PHI_RESULT (phi); |
3ac01fde | 3535 | def = PHI_ARG_DEF_FROM_EDGE (phi, exit); |
ea057359 | 3536 | if (virtual_operand_p (def)) |
726a989a RB |
3537 | { |
3538 | gsi_next (&psi); | |
3539 | continue; | |
3540 | } | |
3ac01fde ZD |
3541 | |
3542 | if (!POINTER_TYPE_P (TREE_TYPE (def)) | |
3543 | && !INTEGRAL_TYPE_P (TREE_TYPE (def))) | |
726a989a RB |
3544 | { |
3545 | gsi_next (&psi); | |
3546 | continue; | |
3547 | } | |
3ac01fde | 3548 | |
19e51b40 JJ |
3549 | bool folded_casts; |
3550 | def = analyze_scalar_evolution_in_loop (ex_loop, loop, def, | |
3551 | &folded_casts); | |
925196ed | 3552 | def = compute_overall_effect_of_inner_loop (ex_loop, def); |
3ac01fde | 3553 | if (!tree_does_not_contain_chrecs (def) |
e5db3515 ZD |
3554 | || chrec_contains_symbols_defined_in_loop (def, ex_loop->num) |
3555 | /* Moving the computation from the loop may prolong life range | |
3556 | of some ssa names, which may cause problems if they appear | |
3557 | on abnormal edges. */ | |
771f882e ZD |
3558 | || contains_abnormal_ssa_name_p (def) |
3559 | /* Do not emit expensive expressions. The rationale is that | |
3560 | when someone writes a code like | |
3561 | ||
3562 | while (n > 45) n -= 45; | |
3563 | ||
3564 | he probably knows that n is not large, and does not want it | |
3565 | to be turned into n %= 45. */ | |
3566 | || expression_expensive_p (def)) | |
726a989a | 3567 | { |
3571dde6 MP |
3568 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3569 | { | |
3570 | fprintf (dump_file, "not replacing:\n "); | |
3571 | print_gimple_stmt (dump_file, phi, 0, 0); | |
3572 | fprintf (dump_file, "\n"); | |
3573 | } | |
726a989a RB |
3574 | gsi_next (&psi); |
3575 | continue; | |
3576 | } | |
3ac01fde | 3577 | |
9b3b55a1 | 3578 | /* Eliminate the PHI node and replace it by a computation outside |
925196ed | 3579 | the loop. */ |
3571dde6 MP |
3580 | if (dump_file) |
3581 | { | |
3582 | fprintf (dump_file, "\nfinal value replacement:\n "); | |
3583 | print_gimple_stmt (dump_file, phi, 0, 0); | |
3584 | fprintf (dump_file, " with\n "); | |
3585 | } | |
925196ed | 3586 | def = unshare_expr (def); |
726a989a RB |
3587 | remove_phi_node (&psi, false); |
3588 | ||
19e51b40 JJ |
3589 | /* If def's type has undefined overflow and there were folded |
3590 | casts, rewrite all stmts added for def into arithmetics | |
3591 | with defined overflow behavior. */ | |
20bd649a MP |
3592 | if (folded_casts && ANY_INTEGRAL_TYPE_P (TREE_TYPE (def)) |
3593 | && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (def))) | |
19e51b40 JJ |
3594 | { |
3595 | gimple_seq stmts; | |
3596 | gimple_stmt_iterator gsi2; | |
3597 | def = force_gimple_operand (def, &stmts, true, NULL_TREE); | |
3598 | gsi2 = gsi_start (stmts); | |
3599 | while (!gsi_end_p (gsi2)) | |
3600 | { | |
355fe088 | 3601 | gimple *stmt = gsi_stmt (gsi2); |
19e51b40 JJ |
3602 | gimple_stmt_iterator gsi3 = gsi2; |
3603 | gsi_next (&gsi2); | |
3604 | gsi_remove (&gsi3, false); | |
3605 | if (is_gimple_assign (stmt) | |
3606 | && arith_code_with_undefined_signed_overflow | |
3607 | (gimple_assign_rhs_code (stmt))) | |
3608 | gsi_insert_seq_before (&gsi, | |
3609 | rewrite_to_defined_overflow (stmt), | |
3610 | GSI_SAME_STMT); | |
3611 | else | |
3612 | gsi_insert_before (&gsi, stmt, GSI_SAME_STMT); | |
3613 | } | |
3614 | } | |
3615 | else | |
3616 | def = force_gimple_operand_gsi (&gsi, def, false, NULL_TREE, | |
3617 | true, GSI_SAME_STMT); | |
3618 | ||
726a989a | 3619 | ass = gimple_build_assign (rslt, def); |
19e51b40 | 3620 | gsi_insert_before (&gsi, ass, GSI_SAME_STMT); |
3571dde6 MP |
3621 | if (dump_file) |
3622 | { | |
3623 | print_gimple_stmt (dump_file, ass, 0, 0); | |
3624 | fprintf (dump_file, "\n"); | |
3625 | } | |
3ac01fde ZD |
3626 | } |
3627 | } | |
c2924966 | 3628 | return 0; |
684aaf29 | 3629 | } |
9e2f83a5 ZD |
3630 | |
3631 | #include "gt-tree-scalar-evolution.h" |