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