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