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