]>
Commit | Line | Data |
---|---|---|
8b11a64c | 1 | /* Induction variable optimizations. |
ad616de1 | 2 | Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. |
8b11a64c ZD |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU General Public License as published by the | |
8 | Free Software Foundation; either version 2, or (at your option) any | |
9 | later version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT | |
12 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING. If not, write to the Free | |
18 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
19 | 02111-1307, USA. */ | |
20 | ||
21 | /* This pass tries to find the optimal set of induction variables for the loop. | |
22 | It optimizes just the basic linear induction variables (although adding | |
23 | support for other types should not be too hard). It includes the | |
24 | optimizations commonly known as strength reduction, induction variable | |
25 | coalescing and induction variable elimination. It does it in the | |
26 | following steps: | |
27 | ||
28 | 1) The interesting uses of induction variables are found. This includes | |
29 | ||
30 | -- uses of induction variables in non-linear expressions | |
31 | -- addresses of arrays | |
32 | -- comparisons of induction variables | |
33 | ||
34 | 2) Candidates for the induction variables are found. This includes | |
35 | ||
36 | -- old induction variables | |
37 | -- the variables defined by expressions derived from the "interesting | |
38 | uses" above | |
39 | ||
40 | 3) The optimal (w.r. to a cost function) set of variables is chosen. The | |
41 | cost function assigns a cost to sets of induction variables and consists | |
42 | of three parts: | |
43 | ||
44 | -- The use costs. Each of the interesting uses chooses the best induction | |
45 | variable in the set and adds its cost to the sum. The cost reflects | |
46 | the time spent on modifying the induction variables value to be usable | |
47 | for the given purpose (adding base and offset for arrays, etc.). | |
48 | -- The variable costs. Each of the variables has a cost assigned that | |
49 | reflects the costs associated with incrementing the value of the | |
50 | variable. The original variables are somewhat preferred. | |
51 | -- The set cost. Depending on the size of the set, extra cost may be | |
52 | added to reflect register pressure. | |
53 | ||
54 | All the costs are defined in a machine-specific way, using the target | |
55 | hooks and machine descriptions to determine them. | |
56 | ||
57 | 4) The trees are transformed to use the new variables, the dead code is | |
58 | removed. | |
59 | ||
60 | All of this is done loop by loop. Doing it globally is theoretically | |
61 | possible, it might give a better performance and it might enable us | |
62 | to decide costs more precisely, but getting all the interactions right | |
63 | would be complicated. */ | |
64 | ||
65 | #include "config.h" | |
66 | #include "system.h" | |
67 | #include "coretypes.h" | |
68 | #include "tm.h" | |
69 | #include "tree.h" | |
70 | #include "rtl.h" | |
71 | #include "tm_p.h" | |
72 | #include "hard-reg-set.h" | |
73 | #include "basic-block.h" | |
74 | #include "output.h" | |
75 | #include "diagnostic.h" | |
76 | #include "tree-flow.h" | |
77 | #include "tree-dump.h" | |
78 | #include "timevar.h" | |
79 | #include "cfgloop.h" | |
80 | #include "varray.h" | |
81 | #include "expr.h" | |
82 | #include "tree-pass.h" | |
83 | #include "ggc.h" | |
84 | #include "insn-config.h" | |
85 | #include "recog.h" | |
86 | #include "hashtab.h" | |
87 | #include "tree-chrec.h" | |
88 | #include "tree-scalar-evolution.h" | |
89 | #include "cfgloop.h" | |
90 | #include "params.h" | |
39b4020c | 91 | #include "langhooks.h" |
8b11a64c ZD |
92 | |
93 | /* The infinite cost. */ | |
94 | #define INFTY 10000000 | |
95 | ||
96 | /* The expected number of loop iterations. TODO -- use profiling instead of | |
97 | this. */ | |
98 | #define AVG_LOOP_NITER(LOOP) 5 | |
99 | ||
8b11a64c ZD |
100 | |
101 | /* Representation of the induction variable. */ | |
102 | struct iv | |
103 | { | |
104 | tree base; /* Initial value of the iv. */ | |
e6845c23 | 105 | tree base_object; /* A memory object to that the induction variable points. */ |
8b11a64c ZD |
106 | tree step; /* Step of the iv (constant only). */ |
107 | tree ssa_name; /* The ssa name with the value. */ | |
108 | bool biv_p; /* Is it a biv? */ | |
109 | bool have_use_for; /* Do we already have a use for it? */ | |
110 | unsigned use_id; /* The identifier in the use if it is the case. */ | |
111 | }; | |
112 | ||
113 | /* Per-ssa version information (induction variable descriptions, etc.). */ | |
114 | struct version_info | |
115 | { | |
116 | tree name; /* The ssa name. */ | |
117 | struct iv *iv; /* Induction variable description. */ | |
118 | bool has_nonlin_use; /* For a loop-level invariant, whether it is used in | |
119 | an expression that is not an induction variable. */ | |
120 | unsigned inv_id; /* Id of an invariant. */ | |
121 | bool preserve_biv; /* For the original biv, whether to preserve it. */ | |
122 | }; | |
123 | ||
124 | /* Information attached to loop. */ | |
125 | struct loop_data | |
126 | { | |
8b11a64c ZD |
127 | unsigned regs_used; /* Number of registers used. */ |
128 | }; | |
129 | ||
130 | /* Types of uses. */ | |
131 | enum use_type | |
132 | { | |
133 | USE_NONLINEAR_EXPR, /* Use in a nonlinear expression. */ | |
134 | USE_OUTER, /* The induction variable is used outside the loop. */ | |
135 | USE_ADDRESS, /* Use in an address. */ | |
136 | USE_COMPARE /* Use is a compare. */ | |
137 | }; | |
138 | ||
139 | /* The candidate - cost pair. */ | |
140 | struct cost_pair | |
141 | { | |
142 | struct iv_cand *cand; /* The candidate. */ | |
143 | unsigned cost; /* The cost. */ | |
144 | bitmap depends_on; /* The list of invariants that have to be | |
145 | preserved. */ | |
146 | }; | |
147 | ||
148 | /* Use. */ | |
149 | struct iv_use | |
150 | { | |
151 | unsigned id; /* The id of the use. */ | |
152 | enum use_type type; /* Type of the use. */ | |
153 | struct iv *iv; /* The induction variable it is based on. */ | |
154 | tree stmt; /* Statement in that it occurs. */ | |
155 | tree *op_p; /* The place where it occurs. */ | |
b1b02be2 ZD |
156 | bitmap related_cands; /* The set of "related" iv candidates, plus the common |
157 | important ones. */ | |
8b11a64c ZD |
158 | |
159 | unsigned n_map_members; /* Number of candidates in the cost_map list. */ | |
160 | struct cost_pair *cost_map; | |
161 | /* The costs wrto the iv candidates. */ | |
162 | ||
163 | struct iv_cand *selected; | |
164 | /* The selected candidate. */ | |
165 | }; | |
166 | ||
167 | /* The position where the iv is computed. */ | |
168 | enum iv_position | |
169 | { | |
170 | IP_NORMAL, /* At the end, just before the exit condition. */ | |
171 | IP_END, /* At the end of the latch block. */ | |
172 | IP_ORIGINAL /* The original biv. */ | |
173 | }; | |
174 | ||
175 | /* The induction variable candidate. */ | |
176 | struct iv_cand | |
177 | { | |
178 | unsigned id; /* The number of the candidate. */ | |
179 | bool important; /* Whether this is an "important" candidate, i.e. such | |
180 | that it should be considered by all uses. */ | |
181 | enum iv_position pos; /* Where it is computed. */ | |
182 | tree incremented_at; /* For original biv, the statement where it is | |
183 | incremented. */ | |
184 | tree var_before; /* The variable used for it before increment. */ | |
185 | tree var_after; /* The variable used for it after increment. */ | |
186 | struct iv *iv; /* The value of the candidate. NULL for | |
187 | "pseudocandidate" used to indicate the possibility | |
188 | to replace the final value of an iv by direct | |
189 | computation of the value. */ | |
190 | unsigned cost; /* Cost of the candidate. */ | |
191 | }; | |
192 | ||
193 | /* The data used by the induction variable optimizations. */ | |
194 | ||
195 | struct ivopts_data | |
196 | { | |
197 | /* The currently optimized loop. */ | |
198 | struct loop *current_loop; | |
199 | ||
ca4c3169 ZD |
200 | /* Numbers of iterations for all exits of the current loop. */ |
201 | htab_t niters; | |
202 | ||
8b11a64c ZD |
203 | /* The size of version_info array allocated. */ |
204 | unsigned version_info_size; | |
205 | ||
206 | /* The array of information for the ssa names. */ | |
207 | struct version_info *version_info; | |
208 | ||
209 | /* The bitmap of indices in version_info whose value was changed. */ | |
210 | bitmap relevant; | |
211 | ||
212 | /* The maximum invariant id. */ | |
213 | unsigned max_inv_id; | |
214 | ||
215 | /* The uses of induction variables. */ | |
216 | varray_type iv_uses; | |
217 | ||
218 | /* The candidates. */ | |
219 | varray_type iv_candidates; | |
220 | ||
80cad5fa ZD |
221 | /* A bitmap of important candidates. */ |
222 | bitmap important_candidates; | |
223 | ||
8b11a64c ZD |
224 | /* Whether to consider just related and important candidates when replacing a |
225 | use. */ | |
226 | bool consider_all_candidates; | |
227 | }; | |
228 | ||
b1b02be2 ZD |
229 | /* An assignment of iv candidates to uses. */ |
230 | ||
231 | struct iv_ca | |
232 | { | |
233 | /* The number of uses covered by the assignment. */ | |
234 | unsigned upto; | |
235 | ||
236 | /* Number of uses that cannot be expressed by the candidates in the set. */ | |
237 | unsigned bad_uses; | |
238 | ||
239 | /* Candidate assigned to a use, together with the related costs. */ | |
240 | struct cost_pair **cand_for_use; | |
241 | ||
242 | /* Number of times each candidate is used. */ | |
243 | unsigned *n_cand_uses; | |
244 | ||
245 | /* The candidates used. */ | |
246 | bitmap cands; | |
247 | ||
36f5ada1 ZD |
248 | /* The number of candidates in the set. */ |
249 | unsigned n_cands; | |
250 | ||
b1b02be2 ZD |
251 | /* Total number of registers needed. */ |
252 | unsigned n_regs; | |
253 | ||
254 | /* Total cost of expressing uses. */ | |
255 | unsigned cand_use_cost; | |
256 | ||
257 | /* Total cost of candidates. */ | |
258 | unsigned cand_cost; | |
259 | ||
260 | /* Number of times each invariant is used. */ | |
261 | unsigned *n_invariant_uses; | |
262 | ||
263 | /* Total cost of the assignment. */ | |
264 | unsigned cost; | |
265 | }; | |
266 | ||
267 | /* Difference of two iv candidate assignments. */ | |
268 | ||
269 | struct iv_ca_delta | |
270 | { | |
271 | /* Changed use. */ | |
272 | struct iv_use *use; | |
273 | ||
274 | /* An old assignment (for rollback purposes). */ | |
275 | struct cost_pair *old_cp; | |
276 | ||
277 | /* A new assignment. */ | |
278 | struct cost_pair *new_cp; | |
279 | ||
280 | /* Next change in the list. */ | |
281 | struct iv_ca_delta *next_change; | |
282 | }; | |
283 | ||
8b11a64c ZD |
284 | /* Bound on number of candidates below that all candidates are considered. */ |
285 | ||
286 | #define CONSIDER_ALL_CANDIDATES_BOUND \ | |
287 | ((unsigned) PARAM_VALUE (PARAM_IV_CONSIDER_ALL_CANDIDATES_BOUND)) | |
288 | ||
2a7e31df | 289 | /* If there are more iv occurrences, we just give up (it is quite unlikely that |
8b11a64c ZD |
290 | optimizing such a loop would help, and it would take ages). */ |
291 | ||
292 | #define MAX_CONSIDERED_USES \ | |
293 | ((unsigned) PARAM_VALUE (PARAM_IV_MAX_CONSIDERED_USES)) | |
294 | ||
36f5ada1 ZD |
295 | /* If there are at most this number of ivs in the set, try removing unnecessary |
296 | ivs from the set always. */ | |
297 | ||
298 | #define ALWAYS_PRUNE_CAND_SET_BOUND \ | |
299 | ((unsigned) PARAM_VALUE (PARAM_IV_ALWAYS_PRUNE_CAND_SET_BOUND)) | |
300 | ||
8b11a64c ZD |
301 | /* The list of trees for that the decl_rtl field must be reset is stored |
302 | here. */ | |
303 | ||
304 | static varray_type decl_rtl_to_reset; | |
305 | ||
306 | /* Number of uses recorded in DATA. */ | |
307 | ||
308 | static inline unsigned | |
309 | n_iv_uses (struct ivopts_data *data) | |
310 | { | |
311 | return VARRAY_ACTIVE_SIZE (data->iv_uses); | |
312 | } | |
313 | ||
314 | /* Ith use recorded in DATA. */ | |
315 | ||
316 | static inline struct iv_use * | |
317 | iv_use (struct ivopts_data *data, unsigned i) | |
318 | { | |
319 | return VARRAY_GENERIC_PTR_NOGC (data->iv_uses, i); | |
320 | } | |
321 | ||
322 | /* Number of candidates recorded in DATA. */ | |
323 | ||
324 | static inline unsigned | |
325 | n_iv_cands (struct ivopts_data *data) | |
326 | { | |
327 | return VARRAY_ACTIVE_SIZE (data->iv_candidates); | |
328 | } | |
329 | ||
330 | /* Ith candidate recorded in DATA. */ | |
331 | ||
332 | static inline struct iv_cand * | |
333 | iv_cand (struct ivopts_data *data, unsigned i) | |
334 | { | |
335 | return VARRAY_GENERIC_PTR_NOGC (data->iv_candidates, i); | |
336 | } | |
337 | ||
338 | /* The data for LOOP. */ | |
339 | ||
340 | static inline struct loop_data * | |
341 | loop_data (struct loop *loop) | |
342 | { | |
343 | return loop->aux; | |
344 | } | |
345 | ||
346 | /* The single loop exit if it dominates the latch, NULL otherwise. */ | |
347 | ||
348 | static edge | |
349 | single_dom_exit (struct loop *loop) | |
350 | { | |
351 | edge exit = loop->single_exit; | |
352 | ||
353 | if (!exit) | |
354 | return NULL; | |
355 | ||
356 | if (!just_once_each_iteration_p (loop, exit->src)) | |
357 | return NULL; | |
358 | ||
359 | return exit; | |
360 | } | |
361 | ||
362 | /* Dumps information about the induction variable IV to FILE. */ | |
363 | ||
364 | extern void dump_iv (FILE *, struct iv *); | |
365 | void | |
366 | dump_iv (FILE *file, struct iv *iv) | |
367 | { | |
e6845c23 ZD |
368 | if (iv->ssa_name) |
369 | { | |
370 | fprintf (file, "ssa name "); | |
371 | print_generic_expr (file, iv->ssa_name, TDF_SLIM); | |
372 | fprintf (file, "\n"); | |
373 | } | |
8b11a64c | 374 | |
2f4675b4 ZD |
375 | fprintf (file, " type "); |
376 | print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM); | |
377 | fprintf (file, "\n"); | |
378 | ||
8b11a64c ZD |
379 | if (iv->step) |
380 | { | |
381 | fprintf (file, " base "); | |
382 | print_generic_expr (file, iv->base, TDF_SLIM); | |
383 | fprintf (file, "\n"); | |
384 | ||
385 | fprintf (file, " step "); | |
386 | print_generic_expr (file, iv->step, TDF_SLIM); | |
387 | fprintf (file, "\n"); | |
388 | } | |
389 | else | |
390 | { | |
391 | fprintf (file, " invariant "); | |
392 | print_generic_expr (file, iv->base, TDF_SLIM); | |
393 | fprintf (file, "\n"); | |
394 | } | |
395 | ||
e6845c23 ZD |
396 | if (iv->base_object) |
397 | { | |
398 | fprintf (file, " base object "); | |
399 | print_generic_expr (file, iv->base_object, TDF_SLIM); | |
400 | fprintf (file, "\n"); | |
401 | } | |
402 | ||
8b11a64c ZD |
403 | if (iv->biv_p) |
404 | fprintf (file, " is a biv\n"); | |
405 | } | |
406 | ||
407 | /* Dumps information about the USE to FILE. */ | |
408 | ||
409 | extern void dump_use (FILE *, struct iv_use *); | |
410 | void | |
411 | dump_use (FILE *file, struct iv_use *use) | |
412 | { | |
8b11a64c ZD |
413 | fprintf (file, "use %d\n", use->id); |
414 | ||
415 | switch (use->type) | |
416 | { | |
417 | case USE_NONLINEAR_EXPR: | |
418 | fprintf (file, " generic\n"); | |
419 | break; | |
420 | ||
421 | case USE_OUTER: | |
422 | fprintf (file, " outside\n"); | |
423 | break; | |
424 | ||
425 | case USE_ADDRESS: | |
426 | fprintf (file, " address\n"); | |
427 | break; | |
428 | ||
429 | case USE_COMPARE: | |
430 | fprintf (file, " compare\n"); | |
431 | break; | |
432 | ||
433 | default: | |
1e128c5f | 434 | gcc_unreachable (); |
8b11a64c ZD |
435 | } |
436 | ||
2f4675b4 ZD |
437 | fprintf (file, " in statement "); |
438 | print_generic_expr (file, use->stmt, TDF_SLIM); | |
439 | fprintf (file, "\n"); | |
440 | ||
441 | fprintf (file, " at position "); | |
442 | if (use->op_p) | |
443 | print_generic_expr (file, *use->op_p, TDF_SLIM); | |
444 | fprintf (file, "\n"); | |
445 | ||
e6845c23 | 446 | dump_iv (file, use->iv); |
2f4675b4 | 447 | |
eec5fec9 ZD |
448 | if (use->related_cands) |
449 | { | |
450 | fprintf (file, " related candidates "); | |
451 | dump_bitmap (file, use->related_cands); | |
452 | } | |
8b11a64c ZD |
453 | } |
454 | ||
455 | /* Dumps information about the uses to FILE. */ | |
456 | ||
457 | extern void dump_uses (FILE *, struct ivopts_data *); | |
458 | void | |
459 | dump_uses (FILE *file, struct ivopts_data *data) | |
460 | { | |
461 | unsigned i; | |
462 | struct iv_use *use; | |
463 | ||
464 | for (i = 0; i < n_iv_uses (data); i++) | |
465 | { | |
466 | use = iv_use (data, i); | |
467 | ||
468 | dump_use (file, use); | |
469 | fprintf (file, "\n"); | |
470 | } | |
471 | } | |
472 | ||
473 | /* Dumps information about induction variable candidate CAND to FILE. */ | |
474 | ||
475 | extern void dump_cand (FILE *, struct iv_cand *); | |
476 | void | |
477 | dump_cand (FILE *file, struct iv_cand *cand) | |
478 | { | |
479 | struct iv *iv = cand->iv; | |
480 | ||
481 | fprintf (file, "candidate %d%s\n", | |
482 | cand->id, cand->important ? " (important)" : ""); | |
483 | ||
484 | if (!iv) | |
485 | { | |
486 | fprintf (file, " final value replacement\n"); | |
487 | return; | |
488 | } | |
489 | ||
490 | switch (cand->pos) | |
491 | { | |
492 | case IP_NORMAL: | |
493 | fprintf (file, " incremented before exit test\n"); | |
494 | break; | |
495 | ||
496 | case IP_END: | |
497 | fprintf (file, " incremented at end\n"); | |
498 | break; | |
499 | ||
500 | case IP_ORIGINAL: | |
501 | fprintf (file, " original biv\n"); | |
502 | break; | |
503 | } | |
504 | ||
e6845c23 | 505 | dump_iv (file, iv); |
8b11a64c ZD |
506 | } |
507 | ||
508 | /* Returns the info for ssa version VER. */ | |
509 | ||
510 | static inline struct version_info * | |
511 | ver_info (struct ivopts_data *data, unsigned ver) | |
512 | { | |
513 | return data->version_info + ver; | |
514 | } | |
515 | ||
516 | /* Returns the info for ssa name NAME. */ | |
517 | ||
518 | static inline struct version_info * | |
519 | name_info (struct ivopts_data *data, tree name) | |
520 | { | |
521 | return ver_info (data, SSA_NAME_VERSION (name)); | |
522 | } | |
523 | ||
524 | /* Checks whether there exists number X such that X * B = A, counting modulo | |
525 | 2^BITS. */ | |
526 | ||
527 | static bool | |
528 | divide (unsigned bits, unsigned HOST_WIDE_INT a, unsigned HOST_WIDE_INT b, | |
529 | HOST_WIDE_INT *x) | |
530 | { | |
531 | unsigned HOST_WIDE_INT mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1); | |
532 | unsigned HOST_WIDE_INT inv, ex, val; | |
533 | unsigned i; | |
534 | ||
535 | a &= mask; | |
536 | b &= mask; | |
537 | ||
538 | /* First divide the whole equation by 2 as long as possible. */ | |
539 | while (!(a & 1) && !(b & 1)) | |
540 | { | |
541 | a >>= 1; | |
542 | b >>= 1; | |
543 | bits--; | |
544 | mask >>= 1; | |
545 | } | |
546 | ||
547 | if (!(b & 1)) | |
548 | { | |
549 | /* If b is still even, a is odd and there is no such x. */ | |
550 | return false; | |
551 | } | |
552 | ||
553 | /* Find the inverse of b. We compute it as | |
554 | b^(2^(bits - 1) - 1) (mod 2^bits). */ | |
555 | inv = 1; | |
556 | ex = b; | |
557 | for (i = 0; i < bits - 1; i++) | |
558 | { | |
559 | inv = (inv * ex) & mask; | |
560 | ex = (ex * ex) & mask; | |
561 | } | |
562 | ||
563 | val = (a * inv) & mask; | |
564 | ||
1e128c5f | 565 | gcc_assert (((val * b) & mask) == a); |
8b11a64c ZD |
566 | |
567 | if ((val >> (bits - 1)) & 1) | |
568 | val |= ~mask; | |
569 | ||
570 | *x = val; | |
571 | ||
572 | return true; | |
573 | } | |
574 | ||
575 | /* Returns true if STMT is after the place where the IP_NORMAL ivs will be | |
576 | emitted in LOOP. */ | |
577 | ||
578 | static bool | |
579 | stmt_after_ip_normal_pos (struct loop *loop, tree stmt) | |
580 | { | |
581 | basic_block bb = ip_normal_pos (loop), sbb = bb_for_stmt (stmt); | |
582 | ||
1e128c5f | 583 | gcc_assert (bb); |
8b11a64c ZD |
584 | |
585 | if (sbb == loop->latch) | |
586 | return true; | |
587 | ||
588 | if (sbb != bb) | |
589 | return false; | |
590 | ||
591 | return stmt == last_stmt (bb); | |
592 | } | |
593 | ||
594 | /* Returns true if STMT if after the place where the original induction | |
595 | variable CAND is incremented. */ | |
596 | ||
597 | static bool | |
598 | stmt_after_ip_original_pos (struct iv_cand *cand, tree stmt) | |
599 | { | |
600 | basic_block cand_bb = bb_for_stmt (cand->incremented_at); | |
601 | basic_block stmt_bb = bb_for_stmt (stmt); | |
602 | block_stmt_iterator bsi; | |
603 | ||
604 | if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb)) | |
605 | return false; | |
606 | ||
607 | if (stmt_bb != cand_bb) | |
608 | return true; | |
609 | ||
610 | /* Scan the block from the end, since the original ivs are usually | |
611 | incremented at the end of the loop body. */ | |
612 | for (bsi = bsi_last (stmt_bb); ; bsi_prev (&bsi)) | |
613 | { | |
614 | if (bsi_stmt (bsi) == cand->incremented_at) | |
615 | return false; | |
616 | if (bsi_stmt (bsi) == stmt) | |
617 | return true; | |
618 | } | |
619 | } | |
620 | ||
621 | /* Returns true if STMT if after the place where the induction variable | |
622 | CAND is incremented in LOOP. */ | |
623 | ||
624 | static bool | |
625 | stmt_after_increment (struct loop *loop, struct iv_cand *cand, tree stmt) | |
626 | { | |
627 | switch (cand->pos) | |
628 | { | |
629 | case IP_END: | |
630 | return false; | |
631 | ||
632 | case IP_NORMAL: | |
633 | return stmt_after_ip_normal_pos (loop, stmt); | |
634 | ||
635 | case IP_ORIGINAL: | |
636 | return stmt_after_ip_original_pos (cand, stmt); | |
637 | ||
638 | default: | |
1e128c5f | 639 | gcc_unreachable (); |
8b11a64c ZD |
640 | } |
641 | } | |
642 | ||
ca4c3169 ZD |
643 | /* Element of the table in that we cache the numbers of iterations obtained |
644 | from exits of the loop. */ | |
645 | ||
646 | struct nfe_cache_elt | |
647 | { | |
648 | /* The edge for that the number of iterations is cached. */ | |
649 | edge exit; | |
650 | ||
0388d40a | 651 | /* True if the # of iterations was successfully determined. */ |
ca4c3169 ZD |
652 | bool valid_p; |
653 | ||
654 | /* Description of # of iterations. */ | |
655 | struct tree_niter_desc niter; | |
656 | }; | |
657 | ||
658 | /* Hash function for nfe_cache_elt E. */ | |
659 | ||
660 | static hashval_t | |
661 | nfe_hash (const void *e) | |
662 | { | |
663 | const struct nfe_cache_elt *elt = e; | |
664 | ||
665 | return htab_hash_pointer (elt->exit); | |
666 | } | |
667 | ||
668 | /* Equality function for nfe_cache_elt E1 and edge E2. */ | |
669 | ||
670 | static int | |
671 | nfe_eq (const void *e1, const void *e2) | |
672 | { | |
673 | const struct nfe_cache_elt *elt1 = e1; | |
674 | ||
675 | return elt1->exit == e2; | |
676 | } | |
677 | ||
678 | /* Returns structure describing number of iterations determined from | |
679 | EXIT of DATA->current_loop, or NULL if something goes wrong. */ | |
680 | ||
681 | static struct tree_niter_desc * | |
682 | niter_for_exit (struct ivopts_data *data, edge exit) | |
683 | { | |
684 | struct nfe_cache_elt *nfe_desc; | |
685 | PTR *slot; | |
686 | ||
687 | slot = htab_find_slot_with_hash (data->niters, exit, | |
688 | htab_hash_pointer (exit), | |
689 | INSERT); | |
690 | ||
691 | if (!*slot) | |
692 | { | |
693 | nfe_desc = xmalloc (sizeof (struct nfe_cache_elt)); | |
694 | nfe_desc->exit = exit; | |
695 | nfe_desc->valid_p = number_of_iterations_exit (data->current_loop, | |
696 | exit, &nfe_desc->niter); | |
697 | *slot = nfe_desc; | |
698 | } | |
699 | else | |
700 | nfe_desc = *slot; | |
701 | ||
702 | if (!nfe_desc->valid_p) | |
703 | return NULL; | |
704 | ||
705 | return &nfe_desc->niter; | |
706 | } | |
707 | ||
708 | /* Returns structure describing number of iterations determined from | |
709 | single dominating exit of DATA->current_loop, or NULL if something | |
710 | goes wrong. */ | |
711 | ||
712 | static struct tree_niter_desc * | |
713 | niter_for_single_dom_exit (struct ivopts_data *data) | |
714 | { | |
715 | edge exit = single_dom_exit (data->current_loop); | |
716 | ||
717 | if (!exit) | |
718 | return NULL; | |
719 | ||
720 | return niter_for_exit (data, exit); | |
721 | } | |
722 | ||
8b11a64c ZD |
723 | /* Initializes data structures used by the iv optimization pass, stored |
724 | in DATA. LOOPS is the loop tree. */ | |
725 | ||
726 | static void | |
727 | tree_ssa_iv_optimize_init (struct loops *loops, struct ivopts_data *data) | |
728 | { | |
729 | unsigned i; | |
730 | ||
731 | data->version_info_size = 2 * num_ssa_names; | |
732 | data->version_info = xcalloc (data->version_info_size, | |
733 | sizeof (struct version_info)); | |
8bdbfff5 NS |
734 | data->relevant = BITMAP_ALLOC (NULL); |
735 | data->important_candidates = BITMAP_ALLOC (NULL); | |
8b11a64c | 736 | data->max_inv_id = 0; |
ca4c3169 | 737 | data->niters = htab_create (10, nfe_hash, nfe_eq, free); |
8b11a64c ZD |
738 | |
739 | for (i = 1; i < loops->num; i++) | |
740 | if (loops->parray[i]) | |
741 | loops->parray[i]->aux = xcalloc (1, sizeof (struct loop_data)); | |
742 | ||
743 | VARRAY_GENERIC_PTR_NOGC_INIT (data->iv_uses, 20, "iv_uses"); | |
744 | VARRAY_GENERIC_PTR_NOGC_INIT (data->iv_candidates, 20, "iv_candidates"); | |
745 | VARRAY_GENERIC_PTR_NOGC_INIT (decl_rtl_to_reset, 20, "decl_rtl_to_reset"); | |
746 | } | |
747 | ||
e6845c23 ZD |
748 | /* Returns a memory object to that EXPR points. In case we are able to |
749 | determine that it does not point to any such object, NULL is returned. */ | |
750 | ||
751 | static tree | |
752 | determine_base_object (tree expr) | |
753 | { | |
754 | enum tree_code code = TREE_CODE (expr); | |
755 | tree base, obj, op0, op1; | |
756 | ||
757 | if (!POINTER_TYPE_P (TREE_TYPE (expr))) | |
758 | return NULL_TREE; | |
759 | ||
760 | switch (code) | |
761 | { | |
762 | case INTEGER_CST: | |
763 | return NULL_TREE; | |
764 | ||
765 | case ADDR_EXPR: | |
766 | obj = TREE_OPERAND (expr, 0); | |
767 | base = get_base_address (obj); | |
768 | ||
769 | if (!base) | |
f5e2738c | 770 | return expr; |
e6845c23 | 771 | |
7299dbfb | 772 | if (TREE_CODE (base) == INDIRECT_REF) |
f5e2738c | 773 | return determine_base_object (TREE_OPERAND (base, 0)); |
7299dbfb | 774 | |
e6845c23 ZD |
775 | return fold (build1 (ADDR_EXPR, ptr_type_node, base)); |
776 | ||
777 | case PLUS_EXPR: | |
778 | case MINUS_EXPR: | |
779 | op0 = determine_base_object (TREE_OPERAND (expr, 0)); | |
780 | op1 = determine_base_object (TREE_OPERAND (expr, 1)); | |
781 | ||
782 | if (!op1) | |
783 | return op0; | |
784 | ||
785 | if (!op0) | |
786 | return (code == PLUS_EXPR | |
787 | ? op1 | |
788 | : fold (build1 (NEGATE_EXPR, ptr_type_node, op1))); | |
789 | ||
790 | return fold (build (code, ptr_type_node, op0, op1)); | |
791 | ||
f5e2738c ZD |
792 | case NOP_EXPR: |
793 | case CONVERT_EXPR: | |
794 | return determine_base_object (TREE_OPERAND (expr, 0)); | |
795 | ||
e6845c23 ZD |
796 | default: |
797 | return fold_convert (ptr_type_node, expr); | |
798 | } | |
799 | } | |
800 | ||
8b11a64c ZD |
801 | /* Allocates an induction variable with given initial value BASE and step STEP |
802 | for loop LOOP. */ | |
803 | ||
804 | static struct iv * | |
805 | alloc_iv (tree base, tree step) | |
806 | { | |
807 | struct iv *iv = xcalloc (1, sizeof (struct iv)); | |
808 | ||
809 | if (step && integer_zerop (step)) | |
810 | step = NULL_TREE; | |
811 | ||
812 | iv->base = base; | |
e6845c23 | 813 | iv->base_object = determine_base_object (base); |
8b11a64c ZD |
814 | iv->step = step; |
815 | iv->biv_p = false; | |
816 | iv->have_use_for = false; | |
817 | iv->use_id = 0; | |
818 | iv->ssa_name = NULL_TREE; | |
819 | ||
820 | return iv; | |
821 | } | |
822 | ||
823 | /* Sets STEP and BASE for induction variable IV. */ | |
824 | ||
825 | static void | |
826 | set_iv (struct ivopts_data *data, tree iv, tree base, tree step) | |
827 | { | |
828 | struct version_info *info = name_info (data, iv); | |
829 | ||
1e128c5f | 830 | gcc_assert (!info->iv); |
8b11a64c ZD |
831 | |
832 | bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv)); | |
833 | info->iv = alloc_iv (base, step); | |
834 | info->iv->ssa_name = iv; | |
835 | } | |
836 | ||
837 | /* Finds induction variable declaration for VAR. */ | |
838 | ||
839 | static struct iv * | |
840 | get_iv (struct ivopts_data *data, tree var) | |
841 | { | |
842 | basic_block bb; | |
843 | ||
844 | if (!name_info (data, var)->iv) | |
845 | { | |
846 | bb = bb_for_stmt (SSA_NAME_DEF_STMT (var)); | |
847 | ||
848 | if (!bb | |
849 | || !flow_bb_inside_loop_p (data->current_loop, bb)) | |
850 | set_iv (data, var, var, NULL_TREE); | |
851 | } | |
852 | ||
853 | return name_info (data, var)->iv; | |
854 | } | |
855 | ||
856 | /* Determines the step of a biv defined in PHI. */ | |
857 | ||
858 | static tree | |
859 | determine_biv_step (tree phi) | |
860 | { | |
861 | struct loop *loop = bb_for_stmt (phi)->loop_father; | |
862 | tree name = PHI_RESULT (phi), base, step; | |
863 | tree type = TREE_TYPE (name); | |
864 | ||
865 | if (!is_gimple_reg (name)) | |
866 | return NULL_TREE; | |
867 | ||
868 | if (!simple_iv (loop, phi, name, &base, &step)) | |
869 | return NULL_TREE; | |
870 | ||
871 | if (!step) | |
5212068f | 872 | return build_int_cst (type, 0); |
8b11a64c ZD |
873 | |
874 | return step; | |
875 | } | |
876 | ||
be35cf60 ZD |
877 | /* Returns true if EXP is a ssa name that occurs in an abnormal phi node. */ |
878 | ||
879 | static bool | |
880 | abnormal_ssa_name_p (tree exp) | |
881 | { | |
882 | if (!exp) | |
883 | return false; | |
884 | ||
885 | if (TREE_CODE (exp) != SSA_NAME) | |
886 | return false; | |
887 | ||
888 | return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp) != 0; | |
889 | } | |
890 | ||
891 | /* Returns false if BASE or INDEX contains a ssa name that occurs in an | |
8b11a64c ZD |
892 | abnormal phi node. Callback for for_each_index. */ |
893 | ||
894 | static bool | |
be35cf60 | 895 | idx_contains_abnormal_ssa_name_p (tree base, tree *index, |
8b11a64c ZD |
896 | void *data ATTRIBUTE_UNUSED) |
897 | { | |
be35cf60 ZD |
898 | if (TREE_CODE (base) == ARRAY_REF) |
899 | { | |
900 | if (abnormal_ssa_name_p (TREE_OPERAND (base, 2))) | |
901 | return false; | |
902 | if (abnormal_ssa_name_p (TREE_OPERAND (base, 3))) | |
903 | return false; | |
904 | } | |
8b11a64c | 905 | |
be35cf60 | 906 | return !abnormal_ssa_name_p (*index); |
8b11a64c ZD |
907 | } |
908 | ||
909 | /* Returns true if EXPR contains a ssa name that occurs in an | |
910 | abnormal phi node. */ | |
911 | ||
912 | static bool | |
913 | contains_abnormal_ssa_name_p (tree expr) | |
914 | { | |
915 | enum tree_code code = TREE_CODE (expr); | |
6615c446 | 916 | enum tree_code_class class = TREE_CODE_CLASS (code); |
8b11a64c ZD |
917 | |
918 | if (code == SSA_NAME) | |
919 | return SSA_NAME_OCCURS_IN_ABNORMAL_PHI (expr) != 0; | |
920 | ||
921 | if (code == INTEGER_CST | |
922 | || is_gimple_min_invariant (expr)) | |
923 | return false; | |
924 | ||
925 | if (code == ADDR_EXPR) | |
6efa2c71 | 926 | return !for_each_index (&TREE_OPERAND (expr, 0), |
8b11a64c ZD |
927 | idx_contains_abnormal_ssa_name_p, |
928 | NULL); | |
929 | ||
930 | switch (class) | |
931 | { | |
6615c446 JO |
932 | case tcc_binary: |
933 | case tcc_comparison: | |
8b11a64c ZD |
934 | if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 1))) |
935 | return true; | |
936 | ||
937 | /* Fallthru. */ | |
6615c446 | 938 | case tcc_unary: |
8b11a64c ZD |
939 | if (contains_abnormal_ssa_name_p (TREE_OPERAND (expr, 0))) |
940 | return true; | |
941 | ||
942 | break; | |
943 | ||
944 | default: | |
1e128c5f | 945 | gcc_unreachable (); |
8b11a64c ZD |
946 | } |
947 | ||
948 | return false; | |
949 | } | |
950 | ||
951 | /* Finds basic ivs. */ | |
952 | ||
953 | static bool | |
954 | find_bivs (struct ivopts_data *data) | |
955 | { | |
956 | tree phi, step, type, base; | |
957 | bool found = false; | |
958 | struct loop *loop = data->current_loop; | |
959 | ||
bb29d951 | 960 | for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
961 | { |
962 | if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi))) | |
963 | continue; | |
964 | ||
965 | step = determine_biv_step (phi); | |
966 | ||
967 | if (!step) | |
968 | continue; | |
969 | if (cst_and_fits_in_hwi (step) | |
970 | && int_cst_value (step) == 0) | |
971 | continue; | |
972 | ||
973 | base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop)); | |
974 | if (contains_abnormal_ssa_name_p (base)) | |
975 | continue; | |
976 | ||
977 | type = TREE_TYPE (PHI_RESULT (phi)); | |
978 | base = fold_convert (type, base); | |
979 | step = fold_convert (type, step); | |
980 | ||
981 | /* FIXME: We do not handle induction variables whose step does | |
982 | not satisfy cst_and_fits_in_hwi. */ | |
983 | if (!cst_and_fits_in_hwi (step)) | |
984 | continue; | |
985 | ||
986 | set_iv (data, PHI_RESULT (phi), base, step); | |
987 | found = true; | |
988 | } | |
989 | ||
990 | return found; | |
991 | } | |
992 | ||
993 | /* Marks basic ivs. */ | |
994 | ||
995 | static void | |
996 | mark_bivs (struct ivopts_data *data) | |
997 | { | |
998 | tree phi, var; | |
999 | struct iv *iv, *incr_iv; | |
1000 | struct loop *loop = data->current_loop; | |
1001 | basic_block incr_bb; | |
1002 | ||
bb29d951 | 1003 | for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
1004 | { |
1005 | iv = get_iv (data, PHI_RESULT (phi)); | |
1006 | if (!iv) | |
1007 | continue; | |
1008 | ||
1009 | var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop)); | |
1010 | incr_iv = get_iv (data, var); | |
1011 | if (!incr_iv) | |
1012 | continue; | |
1013 | ||
1014 | /* If the increment is in the subloop, ignore it. */ | |
1015 | incr_bb = bb_for_stmt (SSA_NAME_DEF_STMT (var)); | |
1016 | if (incr_bb->loop_father != data->current_loop | |
1017 | || (incr_bb->flags & BB_IRREDUCIBLE_LOOP)) | |
1018 | continue; | |
1019 | ||
1020 | iv->biv_p = true; | |
1021 | incr_iv->biv_p = true; | |
1022 | } | |
1023 | } | |
1024 | ||
1025 | /* Checks whether STMT defines a linear induction variable and stores its | |
1026 | parameters to BASE and STEP. */ | |
1027 | ||
1028 | static bool | |
1029 | find_givs_in_stmt_scev (struct ivopts_data *data, tree stmt, | |
1030 | tree *base, tree *step) | |
1031 | { | |
1032 | tree lhs; | |
1033 | struct loop *loop = data->current_loop; | |
1034 | ||
1035 | *base = NULL_TREE; | |
1036 | *step = NULL_TREE; | |
1037 | ||
1038 | if (TREE_CODE (stmt) != MODIFY_EXPR) | |
1039 | return false; | |
1040 | ||
1041 | lhs = TREE_OPERAND (stmt, 0); | |
1042 | if (TREE_CODE (lhs) != SSA_NAME) | |
1043 | return false; | |
1044 | ||
1045 | if (!simple_iv (loop, stmt, TREE_OPERAND (stmt, 1), base, step)) | |
1046 | return false; | |
1047 | ||
1048 | /* FIXME: We do not handle induction variables whose step does | |
1049 | not satisfy cst_and_fits_in_hwi. */ | |
1050 | if (!zero_p (*step) | |
1051 | && !cst_and_fits_in_hwi (*step)) | |
1052 | return false; | |
1053 | ||
1054 | if (contains_abnormal_ssa_name_p (*base)) | |
1055 | return false; | |
1056 | ||
1057 | return true; | |
1058 | } | |
1059 | ||
1060 | /* Finds general ivs in statement STMT. */ | |
1061 | ||
1062 | static void | |
1063 | find_givs_in_stmt (struct ivopts_data *data, tree stmt) | |
1064 | { | |
1065 | tree base, step; | |
1066 | ||
1067 | if (!find_givs_in_stmt_scev (data, stmt, &base, &step)) | |
1068 | return; | |
1069 | ||
1070 | set_iv (data, TREE_OPERAND (stmt, 0), base, step); | |
1071 | } | |
1072 | ||
1073 | /* Finds general ivs in basic block BB. */ | |
1074 | ||
1075 | static void | |
1076 | find_givs_in_bb (struct ivopts_data *data, basic_block bb) | |
1077 | { | |
1078 | block_stmt_iterator bsi; | |
1079 | ||
1080 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1081 | find_givs_in_stmt (data, bsi_stmt (bsi)); | |
1082 | } | |
1083 | ||
1084 | /* Finds general ivs. */ | |
1085 | ||
1086 | static void | |
1087 | find_givs (struct ivopts_data *data) | |
1088 | { | |
1089 | struct loop *loop = data->current_loop; | |
1090 | basic_block *body = get_loop_body_in_dom_order (loop); | |
1091 | unsigned i; | |
1092 | ||
1093 | for (i = 0; i < loop->num_nodes; i++) | |
1094 | find_givs_in_bb (data, body[i]); | |
1095 | free (body); | |
1096 | } | |
1097 | ||
8b11a64c ZD |
1098 | /* For each ssa name defined in LOOP determines whether it is an induction |
1099 | variable and if so, its initial value and step. */ | |
1100 | ||
1101 | static bool | |
1102 | find_induction_variables (struct ivopts_data *data) | |
1103 | { | |
1104 | unsigned i; | |
87c476a2 | 1105 | bitmap_iterator bi; |
8b11a64c ZD |
1106 | |
1107 | if (!find_bivs (data)) | |
1108 | return false; | |
1109 | ||
1110 | find_givs (data); | |
1111 | mark_bivs (data); | |
8b11a64c ZD |
1112 | |
1113 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1114 | { | |
ca4c3169 ZD |
1115 | struct tree_niter_desc *niter; |
1116 | ||
1117 | niter = niter_for_single_dom_exit (data); | |
1118 | ||
1119 | if (niter) | |
8b11a64c ZD |
1120 | { |
1121 | fprintf (dump_file, " number of iterations "); | |
ca4c3169 | 1122 | print_generic_expr (dump_file, niter->niter, TDF_SLIM); |
8b11a64c ZD |
1123 | fprintf (dump_file, "\n"); |
1124 | ||
1125 | fprintf (dump_file, " may be zero if "); | |
ca4c3169 | 1126 | print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM); |
8b11a64c ZD |
1127 | fprintf (dump_file, "\n"); |
1128 | fprintf (dump_file, "\n"); | |
1129 | }; | |
1130 | ||
1131 | fprintf (dump_file, "Induction variables:\n\n"); | |
1132 | ||
87c476a2 | 1133 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi) |
8b11a64c ZD |
1134 | { |
1135 | if (ver_info (data, i)->iv) | |
1136 | dump_iv (dump_file, ver_info (data, i)->iv); | |
87c476a2 | 1137 | } |
8b11a64c ZD |
1138 | } |
1139 | ||
1140 | return true; | |
1141 | } | |
1142 | ||
1143 | /* Records a use of type USE_TYPE at *USE_P in STMT whose value is IV. */ | |
1144 | ||
1145 | static struct iv_use * | |
1146 | record_use (struct ivopts_data *data, tree *use_p, struct iv *iv, | |
1147 | tree stmt, enum use_type use_type) | |
1148 | { | |
1149 | struct iv_use *use = xcalloc (1, sizeof (struct iv_use)); | |
1150 | ||
1151 | use->id = n_iv_uses (data); | |
1152 | use->type = use_type; | |
1153 | use->iv = iv; | |
1154 | use->stmt = stmt; | |
1155 | use->op_p = use_p; | |
8bdbfff5 | 1156 | use->related_cands = BITMAP_ALLOC (NULL); |
8b11a64c | 1157 | |
e6845c23 ZD |
1158 | /* To avoid showing ssa name in the dumps, if it was not reset by the |
1159 | caller. */ | |
1160 | iv->ssa_name = NULL_TREE; | |
1161 | ||
8b11a64c ZD |
1162 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1163 | dump_use (dump_file, use); | |
1164 | ||
1165 | VARRAY_PUSH_GENERIC_PTR_NOGC (data->iv_uses, use); | |
1166 | ||
1167 | return use; | |
1168 | } | |
1169 | ||
1170 | /* Checks whether OP is a loop-level invariant and if so, records it. | |
1171 | NONLINEAR_USE is true if the invariant is used in a way we do not | |
1172 | handle specially. */ | |
1173 | ||
1174 | static void | |
1175 | record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use) | |
1176 | { | |
1177 | basic_block bb; | |
1178 | struct version_info *info; | |
1179 | ||
1180 | if (TREE_CODE (op) != SSA_NAME | |
1181 | || !is_gimple_reg (op)) | |
1182 | return; | |
1183 | ||
1184 | bb = bb_for_stmt (SSA_NAME_DEF_STMT (op)); | |
1185 | if (bb | |
1186 | && flow_bb_inside_loop_p (data->current_loop, bb)) | |
1187 | return; | |
1188 | ||
1189 | info = name_info (data, op); | |
1190 | info->name = op; | |
1191 | info->has_nonlin_use |= nonlinear_use; | |
1192 | if (!info->inv_id) | |
1193 | info->inv_id = ++data->max_inv_id; | |
1194 | bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op)); | |
1195 | } | |
1196 | ||
1197 | /* Checks whether the use OP is interesting and if so, records it | |
1198 | as TYPE. */ | |
1199 | ||
1200 | static struct iv_use * | |
1201 | find_interesting_uses_outer_or_nonlin (struct ivopts_data *data, tree op, | |
1202 | enum use_type type) | |
1203 | { | |
1204 | struct iv *iv; | |
1205 | struct iv *civ; | |
1206 | tree stmt; | |
1207 | struct iv_use *use; | |
1208 | ||
1209 | if (TREE_CODE (op) != SSA_NAME) | |
1210 | return NULL; | |
1211 | ||
1212 | iv = get_iv (data, op); | |
1213 | if (!iv) | |
1214 | return NULL; | |
1215 | ||
1216 | if (iv->have_use_for) | |
1217 | { | |
1218 | use = iv_use (data, iv->use_id); | |
1219 | ||
1e128c5f GB |
1220 | gcc_assert (use->type == USE_NONLINEAR_EXPR |
1221 | || use->type == USE_OUTER); | |
8b11a64c ZD |
1222 | |
1223 | if (type == USE_NONLINEAR_EXPR) | |
1224 | use->type = USE_NONLINEAR_EXPR; | |
1225 | return use; | |
1226 | } | |
1227 | ||
1228 | if (zero_p (iv->step)) | |
1229 | { | |
1230 | record_invariant (data, op, true); | |
1231 | return NULL; | |
1232 | } | |
1233 | iv->have_use_for = true; | |
1234 | ||
1235 | civ = xmalloc (sizeof (struct iv)); | |
1236 | *civ = *iv; | |
1237 | ||
1238 | stmt = SSA_NAME_DEF_STMT (op); | |
1e128c5f GB |
1239 | gcc_assert (TREE_CODE (stmt) == PHI_NODE |
1240 | || TREE_CODE (stmt) == MODIFY_EXPR); | |
8b11a64c ZD |
1241 | |
1242 | use = record_use (data, NULL, civ, stmt, type); | |
1243 | iv->use_id = use->id; | |
1244 | ||
1245 | return use; | |
1246 | } | |
1247 | ||
1248 | /* Checks whether the use OP is interesting and if so, records it. */ | |
1249 | ||
1250 | static struct iv_use * | |
1251 | find_interesting_uses_op (struct ivopts_data *data, tree op) | |
1252 | { | |
1253 | return find_interesting_uses_outer_or_nonlin (data, op, USE_NONLINEAR_EXPR); | |
1254 | } | |
1255 | ||
1256 | /* Records a definition of induction variable OP that is used outside of the | |
1257 | loop. */ | |
1258 | ||
1259 | static struct iv_use * | |
1260 | find_interesting_uses_outer (struct ivopts_data *data, tree op) | |
1261 | { | |
1262 | return find_interesting_uses_outer_or_nonlin (data, op, USE_OUTER); | |
1263 | } | |
1264 | ||
1265 | /* Checks whether the condition *COND_P in STMT is interesting | |
1266 | and if so, records it. */ | |
1267 | ||
1268 | static void | |
1269 | find_interesting_uses_cond (struct ivopts_data *data, tree stmt, tree *cond_p) | |
1270 | { | |
1271 | tree *op0_p; | |
1272 | tree *op1_p; | |
1273 | struct iv *iv0 = NULL, *iv1 = NULL, *civ; | |
1274 | struct iv const_iv; | |
1275 | tree zero = integer_zero_node; | |
1276 | ||
1277 | const_iv.step = NULL_TREE; | |
1278 | ||
1279 | if (integer_zerop (*cond_p) | |
1280 | || integer_nonzerop (*cond_p)) | |
1281 | return; | |
1282 | ||
1283 | if (TREE_CODE (*cond_p) == SSA_NAME) | |
1284 | { | |
1285 | op0_p = cond_p; | |
1286 | op1_p = &zero; | |
1287 | } | |
1288 | else | |
1289 | { | |
1290 | op0_p = &TREE_OPERAND (*cond_p, 0); | |
1291 | op1_p = &TREE_OPERAND (*cond_p, 1); | |
1292 | } | |
1293 | ||
1294 | if (TREE_CODE (*op0_p) == SSA_NAME) | |
1295 | iv0 = get_iv (data, *op0_p); | |
1296 | else | |
1297 | iv0 = &const_iv; | |
1298 | ||
1299 | if (TREE_CODE (*op1_p) == SSA_NAME) | |
1300 | iv1 = get_iv (data, *op1_p); | |
1301 | else | |
1302 | iv1 = &const_iv; | |
1303 | ||
1304 | if (/* When comparing with non-invariant value, we may not do any senseful | |
1305 | induction variable elimination. */ | |
1306 | (!iv0 || !iv1) | |
1307 | /* Eliminating condition based on two ivs would be nontrivial. | |
1308 | ??? TODO -- it is not really important to handle this case. */ | |
1309 | || (!zero_p (iv0->step) && !zero_p (iv1->step))) | |
1310 | { | |
1311 | find_interesting_uses_op (data, *op0_p); | |
1312 | find_interesting_uses_op (data, *op1_p); | |
1313 | return; | |
1314 | } | |
1315 | ||
1316 | if (zero_p (iv0->step) && zero_p (iv1->step)) | |
1317 | { | |
1318 | /* If both are invariants, this is a work for unswitching. */ | |
1319 | return; | |
1320 | } | |
1321 | ||
1322 | civ = xmalloc (sizeof (struct iv)); | |
1323 | *civ = zero_p (iv0->step) ? *iv1: *iv0; | |
1324 | record_use (data, cond_p, civ, stmt, USE_COMPARE); | |
1325 | } | |
1326 | ||
be35cf60 ZD |
1327 | /* Returns true if expression EXPR is obviously invariant in LOOP, |
1328 | i.e. if all its operands are defined outside of the LOOP. */ | |
1329 | ||
feb075f4 | 1330 | bool |
be35cf60 ZD |
1331 | expr_invariant_in_loop_p (struct loop *loop, tree expr) |
1332 | { | |
1333 | basic_block def_bb; | |
1334 | unsigned i, len; | |
1335 | ||
1336 | if (is_gimple_min_invariant (expr)) | |
1337 | return true; | |
1338 | ||
1339 | if (TREE_CODE (expr) == SSA_NAME) | |
1340 | { | |
1341 | def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (expr)); | |
1342 | if (def_bb | |
1343 | && flow_bb_inside_loop_p (loop, def_bb)) | |
1344 | return false; | |
1345 | ||
1346 | return true; | |
1347 | } | |
1348 | ||
1349 | if (!EXPR_P (expr)) | |
1350 | return false; | |
1351 | ||
54e4aedb | 1352 | len = TREE_CODE_LENGTH (TREE_CODE (expr)); |
be35cf60 ZD |
1353 | for (i = 0; i < len; i++) |
1354 | if (!expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i))) | |
1355 | return false; | |
1356 | ||
1357 | return true; | |
1358 | } | |
1359 | ||
8b11a64c ZD |
1360 | /* Cumulates the steps of indices into DATA and replaces their values with the |
1361 | initial ones. Returns false when the value of the index cannot be determined. | |
1362 | Callback for for_each_index. */ | |
1363 | ||
1364 | struct ifs_ivopts_data | |
1365 | { | |
1366 | struct ivopts_data *ivopts_data; | |
1367 | tree stmt; | |
1368 | tree *step_p; | |
1369 | }; | |
1370 | ||
1371 | static bool | |
1372 | idx_find_step (tree base, tree *idx, void *data) | |
1373 | { | |
1374 | struct ifs_ivopts_data *dta = data; | |
1375 | struct iv *iv; | |
be35cf60 | 1376 | tree step, type, iv_type, iv_step, lbound, off; |
2f4675b4 | 1377 | struct loop *loop = dta->ivopts_data->current_loop; |
be35cf60 ZD |
1378 | |
1379 | if (TREE_CODE (base) == MISALIGNED_INDIRECT_REF | |
1380 | || TREE_CODE (base) == ALIGN_INDIRECT_REF) | |
1381 | return false; | |
1382 | ||
1383 | /* If base is a component ref, require that the offset of the reference | |
3a7c155d | 1384 | be invariant. */ |
be35cf60 ZD |
1385 | if (TREE_CODE (base) == COMPONENT_REF) |
1386 | { | |
1387 | off = component_ref_field_offset (base); | |
1388 | return expr_invariant_in_loop_p (loop, off); | |
1389 | } | |
1390 | ||
1391 | /* If base is array, first check whether we will be able to move the | |
1392 | reference out of the loop (in order to take its address in strength | |
1393 | reduction). In order for this to work we need both lower bound | |
1394 | and step to be loop invariants. */ | |
1395 | if (TREE_CODE (base) == ARRAY_REF) | |
1396 | { | |
1397 | step = array_ref_element_size (base); | |
1398 | lbound = array_ref_low_bound (base); | |
1399 | ||
1400 | if (!expr_invariant_in_loop_p (loop, step) | |
1401 | || !expr_invariant_in_loop_p (loop, lbound)) | |
1402 | return false; | |
1403 | } | |
1404 | ||
8b11a64c ZD |
1405 | if (TREE_CODE (*idx) != SSA_NAME) |
1406 | return true; | |
1407 | ||
1408 | iv = get_iv (dta->ivopts_data, *idx); | |
1409 | if (!iv) | |
1410 | return false; | |
1411 | ||
1412 | *idx = iv->base; | |
1413 | ||
1414 | if (!iv->step) | |
1415 | return true; | |
1416 | ||
1417 | iv_type = TREE_TYPE (iv->base); | |
1418 | type = build_pointer_type (TREE_TYPE (base)); | |
1419 | if (TREE_CODE (base) == ARRAY_REF) | |
2f4675b4 ZD |
1420 | { |
1421 | step = array_ref_element_size (base); | |
2f4675b4 ZD |
1422 | |
1423 | /* We only handle addresses whose step is an integer constant. */ | |
1424 | if (TREE_CODE (step) != INTEGER_CST) | |
1425 | return false; | |
2f4675b4 | 1426 | } |
8b11a64c | 1427 | else |
5212068f NS |
1428 | /* The step for pointer arithmetics already is 1 byte. */ |
1429 | step = build_int_cst (type, 1); | |
8b11a64c ZD |
1430 | |
1431 | if (TYPE_PRECISION (iv_type) < TYPE_PRECISION (type)) | |
1432 | iv_step = can_count_iv_in_wider_type (dta->ivopts_data->current_loop, | |
1433 | type, iv->base, iv->step, dta->stmt); | |
1434 | else | |
1435 | iv_step = fold_convert (iv_type, iv->step); | |
1436 | ||
1437 | if (!iv_step) | |
1438 | { | |
1439 | /* The index might wrap. */ | |
1440 | return false; | |
1441 | } | |
1442 | ||
b52d5eaa | 1443 | step = fold_binary_to_constant (MULT_EXPR, type, step, iv_step); |
8b11a64c ZD |
1444 | |
1445 | if (!*dta->step_p) | |
1446 | *dta->step_p = step; | |
1447 | else | |
b52d5eaa RS |
1448 | *dta->step_p = fold_binary_to_constant (PLUS_EXPR, type, |
1449 | *dta->step_p, step); | |
8b11a64c ZD |
1450 | |
1451 | return true; | |
1452 | } | |
1453 | ||
1454 | /* Records use in index IDX. Callback for for_each_index. Ivopts data | |
1455 | object is passed to it in DATA. */ | |
1456 | ||
1457 | static bool | |
2f4675b4 | 1458 | idx_record_use (tree base, tree *idx, |
8b11a64c ZD |
1459 | void *data) |
1460 | { | |
1461 | find_interesting_uses_op (data, *idx); | |
2f4675b4 ZD |
1462 | if (TREE_CODE (base) == ARRAY_REF) |
1463 | { | |
1464 | find_interesting_uses_op (data, array_ref_element_size (base)); | |
1465 | find_interesting_uses_op (data, array_ref_low_bound (base)); | |
1466 | } | |
8b11a64c ZD |
1467 | return true; |
1468 | } | |
1469 | ||
0a915e3d ZD |
1470 | /* Returns true if memory reference REF may be unaligned. */ |
1471 | ||
1472 | static bool | |
1473 | may_be_unaligned_p (tree ref) | |
1474 | { | |
1475 | tree base; | |
1476 | tree base_type; | |
1477 | HOST_WIDE_INT bitsize; | |
1478 | HOST_WIDE_INT bitpos; | |
1479 | tree toffset; | |
1480 | enum machine_mode mode; | |
1481 | int unsignedp, volatilep; | |
1482 | unsigned base_align; | |
1483 | ||
1484 | /* The test below is basically copy of what expr.c:normal_inner_ref | |
1485 | does to check whether the object must be loaded by parts when | |
1486 | STRICT_ALIGNMENT is true. */ | |
1487 | base = get_inner_reference (ref, &bitsize, &bitpos, &toffset, &mode, | |
1488 | &unsignedp, &volatilep, true); | |
1489 | base_type = TREE_TYPE (base); | |
1490 | base_align = TYPE_ALIGN (base_type); | |
1491 | ||
1492 | if (mode != BLKmode | |
1493 | && (base_align < GET_MODE_ALIGNMENT (mode) | |
1494 | || bitpos % GET_MODE_ALIGNMENT (mode) != 0 | |
1495 | || bitpos % BITS_PER_UNIT != 0)) | |
1496 | return true; | |
1497 | ||
1498 | return false; | |
1499 | } | |
1500 | ||
8b11a64c ZD |
1501 | /* Finds addresses in *OP_P inside STMT. */ |
1502 | ||
1503 | static void | |
1504 | find_interesting_uses_address (struct ivopts_data *data, tree stmt, tree *op_p) | |
1505 | { | |
1506 | tree base = unshare_expr (*op_p), step = NULL; | |
1507 | struct iv *civ; | |
1508 | struct ifs_ivopts_data ifs_ivopts_data; | |
1509 | ||
1510 | /* Ignore bitfields for now. Not really something terribly complicated | |
1511 | to handle. TODO. */ | |
1512 | if (TREE_CODE (base) == COMPONENT_REF | |
1513 | && DECL_NONADDRESSABLE_P (TREE_OPERAND (base, 1))) | |
1514 | goto fail; | |
1515 | ||
0a915e3d ZD |
1516 | if (STRICT_ALIGNMENT |
1517 | && may_be_unaligned_p (base)) | |
1518 | goto fail; | |
1519 | ||
8b11a64c ZD |
1520 | ifs_ivopts_data.ivopts_data = data; |
1521 | ifs_ivopts_data.stmt = stmt; | |
1522 | ifs_ivopts_data.step_p = &step; | |
1523 | if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data) | |
1524 | || zero_p (step)) | |
1525 | goto fail; | |
1526 | ||
be35cf60 ZD |
1527 | gcc_assert (TREE_CODE (base) != ALIGN_INDIRECT_REF); |
1528 | gcc_assert (TREE_CODE (base) != MISALIGNED_INDIRECT_REF); | |
1529 | ||
1530 | if (TREE_CODE (base) == INDIRECT_REF) | |
8b11a64c ZD |
1531 | base = TREE_OPERAND (base, 0); |
1532 | else | |
1533 | base = build_addr (base); | |
1534 | ||
1535 | civ = alloc_iv (base, step); | |
1536 | record_use (data, op_p, civ, stmt, USE_ADDRESS); | |
1537 | return; | |
1538 | ||
1539 | fail: | |
1540 | for_each_index (op_p, idx_record_use, data); | |
1541 | } | |
1542 | ||
1543 | /* Finds and records invariants used in STMT. */ | |
1544 | ||
1545 | static void | |
1546 | find_invariants_stmt (struct ivopts_data *data, tree stmt) | |
1547 | { | |
1548 | use_optype uses = NULL; | |
1549 | unsigned i, n; | |
1550 | tree op; | |
1551 | ||
1552 | if (TREE_CODE (stmt) == PHI_NODE) | |
1553 | n = PHI_NUM_ARGS (stmt); | |
1554 | else | |
1555 | { | |
1556 | get_stmt_operands (stmt); | |
1557 | uses = STMT_USE_OPS (stmt); | |
1558 | n = NUM_USES (uses); | |
1559 | } | |
1560 | ||
1561 | for (i = 0; i < n; i++) | |
1562 | { | |
1563 | if (TREE_CODE (stmt) == PHI_NODE) | |
1564 | op = PHI_ARG_DEF (stmt, i); | |
1565 | else | |
1566 | op = USE_OP (uses, i); | |
1567 | ||
1568 | record_invariant (data, op, false); | |
1569 | } | |
1570 | } | |
1571 | ||
1572 | /* Finds interesting uses of induction variables in the statement STMT. */ | |
1573 | ||
1574 | static void | |
1575 | find_interesting_uses_stmt (struct ivopts_data *data, tree stmt) | |
1576 | { | |
1577 | struct iv *iv; | |
1578 | tree op, lhs, rhs; | |
1579 | use_optype uses = NULL; | |
1580 | unsigned i, n; | |
1581 | ||
1582 | find_invariants_stmt (data, stmt); | |
1583 | ||
1584 | if (TREE_CODE (stmt) == COND_EXPR) | |
1585 | { | |
1586 | find_interesting_uses_cond (data, stmt, &COND_EXPR_COND (stmt)); | |
1587 | return; | |
1588 | } | |
1589 | ||
1590 | if (TREE_CODE (stmt) == MODIFY_EXPR) | |
1591 | { | |
1592 | lhs = TREE_OPERAND (stmt, 0); | |
1593 | rhs = TREE_OPERAND (stmt, 1); | |
1594 | ||
1595 | if (TREE_CODE (lhs) == SSA_NAME) | |
1596 | { | |
1597 | /* If the statement defines an induction variable, the uses are not | |
1598 | interesting by themselves. */ | |
1599 | ||
1600 | iv = get_iv (data, lhs); | |
1601 | ||
1602 | if (iv && !zero_p (iv->step)) | |
1603 | return; | |
1604 | } | |
1605 | ||
1606 | switch (TREE_CODE_CLASS (TREE_CODE (rhs))) | |
1607 | { | |
6615c446 | 1608 | case tcc_comparison: |
8b11a64c ZD |
1609 | find_interesting_uses_cond (data, stmt, &TREE_OPERAND (stmt, 1)); |
1610 | return; | |
1611 | ||
6615c446 | 1612 | case tcc_reference: |
8b11a64c | 1613 | find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 1)); |
6615c446 | 1614 | if (REFERENCE_CLASS_P (lhs)) |
8b11a64c ZD |
1615 | find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 0)); |
1616 | return; | |
1617 | ||
1618 | default: ; | |
1619 | } | |
1620 | ||
6615c446 | 1621 | if (REFERENCE_CLASS_P (lhs) |
2f4675b4 | 1622 | && is_gimple_val (rhs)) |
8b11a64c ZD |
1623 | { |
1624 | find_interesting_uses_address (data, stmt, &TREE_OPERAND (stmt, 0)); | |
1625 | find_interesting_uses_op (data, rhs); | |
1626 | return; | |
1627 | } | |
2f4675b4 ZD |
1628 | |
1629 | /* TODO -- we should also handle address uses of type | |
1630 | ||
1631 | memory = call (whatever); | |
1632 | ||
1633 | and | |
1634 | ||
1635 | call (memory). */ | |
8b11a64c ZD |
1636 | } |
1637 | ||
1638 | if (TREE_CODE (stmt) == PHI_NODE | |
1639 | && bb_for_stmt (stmt) == data->current_loop->header) | |
1640 | { | |
1641 | lhs = PHI_RESULT (stmt); | |
1642 | iv = get_iv (data, lhs); | |
1643 | ||
1644 | if (iv && !zero_p (iv->step)) | |
1645 | return; | |
1646 | } | |
1647 | ||
1648 | if (TREE_CODE (stmt) == PHI_NODE) | |
1649 | n = PHI_NUM_ARGS (stmt); | |
1650 | else | |
1651 | { | |
1652 | uses = STMT_USE_OPS (stmt); | |
1653 | n = NUM_USES (uses); | |
1654 | } | |
1655 | ||
1656 | for (i = 0; i < n; i++) | |
1657 | { | |
1658 | if (TREE_CODE (stmt) == PHI_NODE) | |
1659 | op = PHI_ARG_DEF (stmt, i); | |
1660 | else | |
1661 | op = USE_OP (uses, i); | |
1662 | ||
1663 | if (TREE_CODE (op) != SSA_NAME) | |
1664 | continue; | |
1665 | ||
1666 | iv = get_iv (data, op); | |
1667 | if (!iv) | |
1668 | continue; | |
1669 | ||
1670 | find_interesting_uses_op (data, op); | |
1671 | } | |
1672 | } | |
1673 | ||
1674 | /* Finds interesting uses of induction variables outside of loops | |
1675 | on loop exit edge EXIT. */ | |
1676 | ||
1677 | static void | |
1678 | find_interesting_uses_outside (struct ivopts_data *data, edge exit) | |
1679 | { | |
1680 | tree phi, def; | |
1681 | ||
bb29d951 | 1682 | for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
1683 | { |
1684 | def = PHI_ARG_DEF_FROM_EDGE (phi, exit); | |
1685 | find_interesting_uses_outer (data, def); | |
1686 | } | |
1687 | } | |
1688 | ||
1689 | /* Finds uses of the induction variables that are interesting. */ | |
1690 | ||
1691 | static void | |
1692 | find_interesting_uses (struct ivopts_data *data) | |
1693 | { | |
1694 | basic_block bb; | |
1695 | block_stmt_iterator bsi; | |
1696 | tree phi; | |
1697 | basic_block *body = get_loop_body (data->current_loop); | |
1698 | unsigned i; | |
1699 | struct version_info *info; | |
1700 | edge e; | |
1701 | ||
1702 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1703 | fprintf (dump_file, "Uses:\n\n"); | |
1704 | ||
1705 | for (i = 0; i < data->current_loop->num_nodes; i++) | |
1706 | { | |
628f6a4e | 1707 | edge_iterator ei; |
8b11a64c ZD |
1708 | bb = body[i]; |
1709 | ||
628f6a4e | 1710 | FOR_EACH_EDGE (e, ei, bb->succs) |
8b11a64c ZD |
1711 | if (e->dest != EXIT_BLOCK_PTR |
1712 | && !flow_bb_inside_loop_p (data->current_loop, e->dest)) | |
1713 | find_interesting_uses_outside (data, e); | |
1714 | ||
bb29d951 | 1715 | for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
1716 | find_interesting_uses_stmt (data, phi); |
1717 | for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) | |
1718 | find_interesting_uses_stmt (data, bsi_stmt (bsi)); | |
1719 | } | |
1720 | ||
1721 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1722 | { | |
87c476a2 ZD |
1723 | bitmap_iterator bi; |
1724 | ||
8b11a64c ZD |
1725 | fprintf (dump_file, "\n"); |
1726 | ||
87c476a2 | 1727 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi) |
8b11a64c ZD |
1728 | { |
1729 | info = ver_info (data, i); | |
1730 | if (info->inv_id) | |
1731 | { | |
1732 | fprintf (dump_file, " "); | |
1733 | print_generic_expr (dump_file, info->name, TDF_SLIM); | |
1734 | fprintf (dump_file, " is invariant (%d)%s\n", | |
1735 | info->inv_id, info->has_nonlin_use ? "" : ", eliminable"); | |
1736 | } | |
87c476a2 | 1737 | } |
8b11a64c ZD |
1738 | |
1739 | fprintf (dump_file, "\n"); | |
1740 | } | |
1741 | ||
1742 | free (body); | |
1743 | } | |
1744 | ||
f5e2738c ZD |
1745 | /* Strips constant offsets from EXPR and stores them to OFFSET. If INSIDE_ADDR |
1746 | is true, assume we are inside an address. */ | |
1747 | ||
1748 | static tree | |
1749 | strip_offset (tree expr, bool inside_addr, unsigned HOST_WIDE_INT *offset) | |
1750 | { | |
1751 | tree op0 = NULL_TREE, op1 = NULL_TREE, step; | |
1752 | enum tree_code code; | |
1753 | tree type, orig_type = TREE_TYPE (expr); | |
1754 | unsigned HOST_WIDE_INT off0, off1, st; | |
1755 | tree orig_expr = expr; | |
1756 | ||
1757 | STRIP_NOPS (expr); | |
1758 | type = TREE_TYPE (expr); | |
1759 | code = TREE_CODE (expr); | |
1760 | *offset = 0; | |
1761 | ||
1762 | switch (code) | |
1763 | { | |
1764 | case INTEGER_CST: | |
1765 | if (!cst_and_fits_in_hwi (expr) | |
1766 | || zero_p (expr)) | |
1767 | return orig_expr; | |
1768 | ||
1769 | *offset = int_cst_value (expr); | |
1770 | return build_int_cst_type (orig_type, 0); | |
1771 | ||
1772 | case PLUS_EXPR: | |
1773 | case MINUS_EXPR: | |
1774 | op0 = TREE_OPERAND (expr, 0); | |
1775 | op1 = TREE_OPERAND (expr, 1); | |
1776 | ||
1777 | op0 = strip_offset (op0, false, &off0); | |
1778 | op1 = strip_offset (op1, false, &off1); | |
1779 | ||
1780 | *offset = (code == PLUS_EXPR ? off0 + off1 : off0 - off1); | |
1781 | if (op0 == TREE_OPERAND (expr, 0) | |
1782 | && op1 == TREE_OPERAND (expr, 1)) | |
1783 | return orig_expr; | |
1784 | ||
1785 | if (zero_p (op1)) | |
1786 | expr = op0; | |
1787 | else if (zero_p (op0)) | |
1788 | { | |
1789 | if (code == PLUS_EXPR) | |
1790 | expr = op1; | |
1791 | else | |
1792 | expr = build1 (NEGATE_EXPR, type, op1); | |
1793 | } | |
1794 | else | |
1795 | expr = build2 (code, type, op0, op1); | |
1796 | ||
1797 | return fold_convert (orig_type, expr); | |
1798 | ||
1799 | case ARRAY_REF: | |
1800 | if (!inside_addr) | |
1801 | return orig_expr; | |
1802 | ||
1803 | step = array_ref_element_size (expr); | |
1804 | if (!cst_and_fits_in_hwi (step)) | |
1805 | break; | |
1806 | ||
1807 | st = int_cst_value (step); | |
1808 | op1 = TREE_OPERAND (expr, 1); | |
1809 | op1 = strip_offset (op1, false, &off1); | |
1810 | *offset = off1 * st; | |
1811 | break; | |
1812 | ||
1813 | case COMPONENT_REF: | |
1814 | if (!inside_addr) | |
1815 | return orig_expr; | |
1816 | break; | |
1817 | ||
1818 | case ADDR_EXPR: | |
1819 | inside_addr = true; | |
1820 | break; | |
1821 | ||
1822 | default: | |
1823 | return orig_expr; | |
1824 | } | |
1825 | ||
1826 | /* Default handling of expressions for that we want to recurse into | |
1827 | the first operand. */ | |
1828 | op0 = TREE_OPERAND (expr, 0); | |
1829 | op0 = strip_offset (op0, inside_addr, &off0); | |
1830 | *offset += off0; | |
1831 | ||
1832 | if (op0 == TREE_OPERAND (expr, 0) | |
1833 | && (!op1 || op1 == TREE_OPERAND (expr, 1))) | |
1834 | return orig_expr; | |
1835 | ||
1836 | expr = copy_node (expr); | |
1837 | TREE_OPERAND (expr, 0) = op0; | |
1838 | if (op1) | |
1839 | TREE_OPERAND (expr, 1) = op1; | |
1840 | ||
1841 | return fold_convert (orig_type, expr); | |
1842 | } | |
1843 | ||
8b11a64c ZD |
1844 | /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and |
1845 | position to POS. If USE is not NULL, the candidate is set as related to | |
1846 | it. If both BASE and STEP are NULL, we add a pseudocandidate for the | |
1847 | replacement of the final value of the iv by a direct computation. */ | |
1848 | ||
1849 | static struct iv_cand * | |
1850 | add_candidate_1 (struct ivopts_data *data, | |
1851 | tree base, tree step, bool important, enum iv_position pos, | |
1852 | struct iv_use *use, tree incremented_at) | |
1853 | { | |
1854 | unsigned i; | |
1855 | struct iv_cand *cand = NULL; | |
1856 | tree type; | |
1857 | ||
1858 | if (base) | |
1859 | { | |
1860 | type = TREE_TYPE (base); | |
1861 | if (!TYPE_UNSIGNED (type)) | |
1862 | { | |
1863 | type = unsigned_type_for (type); | |
1864 | base = fold_convert (type, base); | |
1865 | if (step) | |
1866 | step = fold_convert (type, step); | |
1867 | } | |
1868 | } | |
1869 | ||
1870 | for (i = 0; i < n_iv_cands (data); i++) | |
1871 | { | |
1872 | cand = iv_cand (data, i); | |
1873 | ||
1874 | if (cand->pos != pos) | |
1875 | continue; | |
1876 | ||
1877 | if (cand->incremented_at != incremented_at) | |
1878 | continue; | |
1879 | ||
1880 | if (!cand->iv) | |
1881 | { | |
1882 | if (!base && !step) | |
1883 | break; | |
1884 | ||
1885 | continue; | |
1886 | } | |
1887 | ||
1888 | if (!base && !step) | |
1889 | continue; | |
1890 | ||
1891 | if (!operand_equal_p (base, cand->iv->base, 0)) | |
1892 | continue; | |
1893 | ||
1894 | if (zero_p (cand->iv->step)) | |
1895 | { | |
1896 | if (zero_p (step)) | |
1897 | break; | |
1898 | } | |
1899 | else | |
1900 | { | |
1901 | if (step && operand_equal_p (step, cand->iv->step, 0)) | |
1902 | break; | |
1903 | } | |
1904 | } | |
1905 | ||
1906 | if (i == n_iv_cands (data)) | |
1907 | { | |
1908 | cand = xcalloc (1, sizeof (struct iv_cand)); | |
1909 | cand->id = i; | |
1910 | ||
1911 | if (!base && !step) | |
1912 | cand->iv = NULL; | |
1913 | else | |
1914 | cand->iv = alloc_iv (base, step); | |
1915 | ||
1916 | cand->pos = pos; | |
1917 | if (pos != IP_ORIGINAL && cand->iv) | |
1918 | { | |
1919 | cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp"); | |
1920 | cand->var_after = cand->var_before; | |
1921 | } | |
1922 | cand->important = important; | |
1923 | cand->incremented_at = incremented_at; | |
1924 | VARRAY_PUSH_GENERIC_PTR_NOGC (data->iv_candidates, cand); | |
1925 | ||
1926 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1927 | dump_cand (dump_file, cand); | |
1928 | } | |
1929 | ||
1930 | if (important && !cand->important) | |
1931 | { | |
1932 | cand->important = true; | |
1933 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1934 | fprintf (dump_file, "Candidate %d is important\n", cand->id); | |
1935 | } | |
1936 | ||
1937 | if (use) | |
1938 | { | |
1939 | bitmap_set_bit (use->related_cands, i); | |
1940 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1941 | fprintf (dump_file, "Candidate %d is related to use %d\n", | |
1942 | cand->id, use->id); | |
1943 | } | |
1944 | ||
1945 | return cand; | |
1946 | } | |
1947 | ||
4366cf6d ZD |
1948 | /* Returns true if incrementing the induction variable at the end of the LOOP |
1949 | is allowed. | |
1950 | ||
1951 | The purpose is to avoid splitting latch edge with a biv increment, thus | |
1952 | creating a jump, possibly confusing other optimization passes and leaving | |
1953 | less freedom to scheduler. So we allow IP_END_POS only if IP_NORMAL_POS | |
1954 | is not available (so we do not have a better alternative), or if the latch | |
1955 | edge is already nonempty. */ | |
1956 | ||
1957 | static bool | |
1958 | allow_ip_end_pos_p (struct loop *loop) | |
1959 | { | |
1960 | if (!ip_normal_pos (loop)) | |
1961 | return true; | |
1962 | ||
1963 | if (!empty_block_p (ip_end_pos (loop))) | |
1964 | return true; | |
1965 | ||
1966 | return false; | |
1967 | } | |
1968 | ||
8b11a64c ZD |
1969 | /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and |
1970 | position to POS. If USE is not NULL, the candidate is set as related to | |
1971 | it. The candidate computation is scheduled on all available positions. */ | |
1972 | ||
1973 | static void | |
1974 | add_candidate (struct ivopts_data *data, | |
1975 | tree base, tree step, bool important, struct iv_use *use) | |
1976 | { | |
1977 | if (ip_normal_pos (data->current_loop)) | |
1978 | add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL_TREE); | |
4366cf6d ZD |
1979 | if (ip_end_pos (data->current_loop) |
1980 | && allow_ip_end_pos_p (data->current_loop)) | |
8b11a64c ZD |
1981 | add_candidate_1 (data, base, step, important, IP_END, use, NULL_TREE); |
1982 | } | |
1983 | ||
39b4020c RS |
1984 | /* Add a standard "0 + 1 * iteration" iv candidate for a |
1985 | type with SIZE bits. */ | |
1986 | ||
1987 | static void | |
1988 | add_standard_iv_candidates_for_size (struct ivopts_data *data, | |
1989 | unsigned int size) | |
1990 | { | |
1991 | tree type = lang_hooks.types.type_for_size (size, true); | |
1992 | add_candidate (data, build_int_cst (type, 0), build_int_cst (type, 1), | |
1993 | true, NULL); | |
1994 | } | |
1995 | ||
8b11a64c ZD |
1996 | /* Adds standard iv candidates. */ |
1997 | ||
1998 | static void | |
1999 | add_standard_iv_candidates (struct ivopts_data *data) | |
2000 | { | |
39b4020c | 2001 | add_standard_iv_candidates_for_size (data, INT_TYPE_SIZE); |
8b11a64c | 2002 | |
39b4020c RS |
2003 | /* The same for a double-integer type if it is still fast enough. */ |
2004 | if (BITS_PER_WORD >= INT_TYPE_SIZE * 2) | |
2005 | add_standard_iv_candidates_for_size (data, INT_TYPE_SIZE * 2); | |
8b11a64c ZD |
2006 | } |
2007 | ||
2008 | ||
2009 | /* Adds candidates bases on the old induction variable IV. */ | |
2010 | ||
2011 | static void | |
2012 | add_old_iv_candidates (struct ivopts_data *data, struct iv *iv) | |
2013 | { | |
2014 | tree phi, def; | |
2015 | struct iv_cand *cand; | |
2016 | ||
2017 | add_candidate (data, iv->base, iv->step, true, NULL); | |
2018 | ||
2019 | /* The same, but with initial value zero. */ | |
2020 | add_candidate (data, | |
5212068f | 2021 | build_int_cst (TREE_TYPE (iv->base), 0), |
8b11a64c ZD |
2022 | iv->step, true, NULL); |
2023 | ||
2024 | phi = SSA_NAME_DEF_STMT (iv->ssa_name); | |
2025 | if (TREE_CODE (phi) == PHI_NODE) | |
2026 | { | |
2027 | /* Additionally record the possibility of leaving the original iv | |
2028 | untouched. */ | |
2029 | def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop)); | |
2030 | cand = add_candidate_1 (data, | |
2031 | iv->base, iv->step, true, IP_ORIGINAL, NULL, | |
2032 | SSA_NAME_DEF_STMT (def)); | |
2033 | cand->var_before = iv->ssa_name; | |
2034 | cand->var_after = def; | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | /* Adds candidates based on the old induction variables. */ | |
2039 | ||
2040 | static void | |
2041 | add_old_ivs_candidates (struct ivopts_data *data) | |
2042 | { | |
2043 | unsigned i; | |
2044 | struct iv *iv; | |
87c476a2 | 2045 | bitmap_iterator bi; |
8b11a64c | 2046 | |
87c476a2 | 2047 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi) |
8b11a64c ZD |
2048 | { |
2049 | iv = ver_info (data, i)->iv; | |
2050 | if (iv && iv->biv_p && !zero_p (iv->step)) | |
2051 | add_old_iv_candidates (data, iv); | |
87c476a2 | 2052 | } |
8b11a64c ZD |
2053 | } |
2054 | ||
2055 | /* Adds candidates based on the value of the induction variable IV and USE. */ | |
2056 | ||
2057 | static void | |
2058 | add_iv_value_candidates (struct ivopts_data *data, | |
2059 | struct iv *iv, struct iv_use *use) | |
2060 | { | |
2061 | add_candidate (data, iv->base, iv->step, false, use); | |
2062 | ||
2063 | /* The same, but with initial value zero. */ | |
5212068f | 2064 | add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0), |
8b11a64c ZD |
2065 | iv->step, false, use); |
2066 | } | |
2067 | ||
2068 | /* Adds candidates based on the address IV and USE. */ | |
2069 | ||
2070 | static void | |
2071 | add_address_candidates (struct ivopts_data *data, | |
2072 | struct iv *iv, struct iv_use *use) | |
2073 | { | |
f5e2738c ZD |
2074 | tree base, abase; |
2075 | unsigned HOST_WIDE_INT offset; | |
8b11a64c ZD |
2076 | |
2077 | /* First, the trivial choices. */ | |
2078 | add_iv_value_candidates (data, iv, use); | |
2079 | ||
2080 | /* Second, try removing the COMPONENT_REFs. */ | |
2081 | if (TREE_CODE (iv->base) == ADDR_EXPR) | |
2082 | { | |
2083 | base = TREE_OPERAND (iv->base, 0); | |
2084 | while (TREE_CODE (base) == COMPONENT_REF | |
2085 | || (TREE_CODE (base) == ARRAY_REF | |
2086 | && TREE_CODE (TREE_OPERAND (base, 1)) == INTEGER_CST)) | |
2087 | base = TREE_OPERAND (base, 0); | |
2088 | ||
2089 | if (base != TREE_OPERAND (iv->base, 0)) | |
2090 | { | |
be35cf60 ZD |
2091 | gcc_assert (TREE_CODE (base) != ALIGN_INDIRECT_REF); |
2092 | gcc_assert (TREE_CODE (base) != MISALIGNED_INDIRECT_REF); | |
2093 | ||
2094 | if (TREE_CODE (base) == INDIRECT_REF) | |
8b11a64c ZD |
2095 | base = TREE_OPERAND (base, 0); |
2096 | else | |
2097 | base = build_addr (base); | |
2098 | add_candidate (data, base, iv->step, false, use); | |
2099 | } | |
2100 | } | |
2101 | ||
2102 | /* Third, try removing the constant offset. */ | |
2103 | abase = iv->base; | |
f5e2738c ZD |
2104 | base = strip_offset (abase, false, &offset); |
2105 | if (offset) | |
2106 | add_candidate (data, base, iv->step, false, use); | |
8b11a64c ZD |
2107 | } |
2108 | ||
2109 | /* Possibly adds pseudocandidate for replacing the final value of USE by | |
2110 | a direct computation. */ | |
2111 | ||
2112 | static void | |
2113 | add_iv_outer_candidates (struct ivopts_data *data, struct iv_use *use) | |
2114 | { | |
2115 | struct tree_niter_desc *niter; | |
8b11a64c ZD |
2116 | |
2117 | /* We must know where we exit the loop and how many times does it roll. */ | |
ca4c3169 ZD |
2118 | niter = niter_for_single_dom_exit (data); |
2119 | if (!niter | |
2120 | || !zero_p (niter->may_be_zero)) | |
8b11a64c ZD |
2121 | return; |
2122 | ||
2123 | add_candidate_1 (data, NULL, NULL, false, IP_NORMAL, use, NULL_TREE); | |
2124 | } | |
2125 | ||
2126 | /* Adds candidates based on the uses. */ | |
2127 | ||
2128 | static void | |
2129 | add_derived_ivs_candidates (struct ivopts_data *data) | |
2130 | { | |
2131 | unsigned i; | |
2132 | ||
2133 | for (i = 0; i < n_iv_uses (data); i++) | |
2134 | { | |
2135 | struct iv_use *use = iv_use (data, i); | |
2136 | ||
2137 | if (!use) | |
2138 | continue; | |
2139 | ||
2140 | switch (use->type) | |
2141 | { | |
2142 | case USE_NONLINEAR_EXPR: | |
2143 | case USE_COMPARE: | |
2144 | /* Just add the ivs based on the value of the iv used here. */ | |
2145 | add_iv_value_candidates (data, use->iv, use); | |
2146 | break; | |
2147 | ||
2148 | case USE_OUTER: | |
2149 | add_iv_value_candidates (data, use->iv, use); | |
2150 | ||
2151 | /* Additionally, add the pseudocandidate for the possibility to | |
2152 | replace the final value by a direct computation. */ | |
2153 | add_iv_outer_candidates (data, use); | |
2154 | break; | |
2155 | ||
2156 | case USE_ADDRESS: | |
2157 | add_address_candidates (data, use->iv, use); | |
2158 | break; | |
2159 | ||
2160 | default: | |
1e128c5f | 2161 | gcc_unreachable (); |
8b11a64c ZD |
2162 | } |
2163 | } | |
2164 | } | |
2165 | ||
b1b02be2 ZD |
2166 | /* Record important candidates and add them to related_cands bitmaps |
2167 | if needed. */ | |
2168 | ||
2169 | static void | |
2170 | record_important_candidates (struct ivopts_data *data) | |
2171 | { | |
2172 | unsigned i; | |
2173 | struct iv_use *use; | |
2174 | ||
2175 | for (i = 0; i < n_iv_cands (data); i++) | |
2176 | { | |
2177 | struct iv_cand *cand = iv_cand (data, i); | |
2178 | ||
2179 | if (cand->important) | |
2180 | bitmap_set_bit (data->important_candidates, i); | |
2181 | } | |
2182 | ||
2183 | data->consider_all_candidates = (n_iv_cands (data) | |
2184 | <= CONSIDER_ALL_CANDIDATES_BOUND); | |
2185 | ||
2186 | if (data->consider_all_candidates) | |
2187 | { | |
2188 | /* We will not need "related_cands" bitmaps in this case, | |
2189 | so release them to decrease peak memory consumption. */ | |
2190 | for (i = 0; i < n_iv_uses (data); i++) | |
2191 | { | |
2192 | use = iv_use (data, i); | |
8bdbfff5 | 2193 | BITMAP_FREE (use->related_cands); |
b1b02be2 ZD |
2194 | } |
2195 | } | |
2196 | else | |
2197 | { | |
2198 | /* Add important candidates to the related_cands bitmaps. */ | |
2199 | for (i = 0; i < n_iv_uses (data); i++) | |
2200 | bitmap_ior_into (iv_use (data, i)->related_cands, | |
2201 | data->important_candidates); | |
2202 | } | |
2203 | } | |
2204 | ||
8b11a64c ZD |
2205 | /* Finds the candidates for the induction variables. */ |
2206 | ||
2207 | static void | |
2208 | find_iv_candidates (struct ivopts_data *data) | |
2209 | { | |
2210 | /* Add commonly used ivs. */ | |
2211 | add_standard_iv_candidates (data); | |
2212 | ||
2213 | /* Add old induction variables. */ | |
2214 | add_old_ivs_candidates (data); | |
2215 | ||
2216 | /* Add induction variables derived from uses. */ | |
2217 | add_derived_ivs_candidates (data); | |
b1b02be2 ZD |
2218 | |
2219 | /* Record the important candidates. */ | |
2220 | record_important_candidates (data); | |
8b11a64c ZD |
2221 | } |
2222 | ||
2223 | /* Allocates the data structure mapping the (use, candidate) pairs to costs. | |
2224 | If consider_all_candidates is true, we use a two-dimensional array, otherwise | |
2225 | we allocate a simple list to every use. */ | |
2226 | ||
2227 | static void | |
2228 | alloc_use_cost_map (struct ivopts_data *data) | |
2229 | { | |
b1b02be2 | 2230 | unsigned i, size, s, j; |
8b11a64c ZD |
2231 | |
2232 | for (i = 0; i < n_iv_uses (data); i++) | |
2233 | { | |
2234 | struct iv_use *use = iv_use (data, i); | |
87c476a2 | 2235 | bitmap_iterator bi; |
8b11a64c ZD |
2236 | |
2237 | if (data->consider_all_candidates) | |
b1b02be2 | 2238 | size = n_iv_cands (data); |
8b11a64c ZD |
2239 | else |
2240 | { | |
b1b02be2 | 2241 | s = 0; |
87c476a2 ZD |
2242 | EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi) |
2243 | { | |
b1b02be2 | 2244 | s++; |
87c476a2 | 2245 | } |
b1b02be2 ZD |
2246 | |
2247 | /* Round up to the power of two, so that moduling by it is fast. */ | |
2248 | for (size = 1; size < s; size <<= 1) | |
2249 | continue; | |
8b11a64c ZD |
2250 | } |
2251 | ||
b1b02be2 | 2252 | use->n_map_members = size; |
8b11a64c ZD |
2253 | use->cost_map = xcalloc (size, sizeof (struct cost_pair)); |
2254 | } | |
2255 | } | |
2256 | ||
2257 | /* Sets cost of (USE, CANDIDATE) pair to COST and record that it depends | |
2258 | on invariants DEPENDS_ON. */ | |
2259 | ||
2260 | static void | |
2261 | set_use_iv_cost (struct ivopts_data *data, | |
2262 | struct iv_use *use, struct iv_cand *cand, unsigned cost, | |
2263 | bitmap depends_on) | |
2264 | { | |
b1b02be2 ZD |
2265 | unsigned i, s; |
2266 | ||
2267 | if (cost == INFTY) | |
8b11a64c | 2268 | { |
8bdbfff5 | 2269 | BITMAP_FREE (depends_on); |
b1b02be2 | 2270 | return; |
8b11a64c ZD |
2271 | } |
2272 | ||
2273 | if (data->consider_all_candidates) | |
2274 | { | |
2275 | use->cost_map[cand->id].cand = cand; | |
2276 | use->cost_map[cand->id].cost = cost; | |
2277 | use->cost_map[cand->id].depends_on = depends_on; | |
2278 | return; | |
2279 | } | |
2280 | ||
b1b02be2 ZD |
2281 | /* n_map_members is a power of two, so this computes modulo. */ |
2282 | s = cand->id & (use->n_map_members - 1); | |
2283 | for (i = s; i < use->n_map_members; i++) | |
2284 | if (!use->cost_map[i].cand) | |
2285 | goto found; | |
2286 | for (i = 0; i < s; i++) | |
2287 | if (!use->cost_map[i].cand) | |
2288 | goto found; | |
2289 | ||
2290 | gcc_unreachable (); | |
8b11a64c | 2291 | |
b1b02be2 ZD |
2292 | found: |
2293 | use->cost_map[i].cand = cand; | |
2294 | use->cost_map[i].cost = cost; | |
2295 | use->cost_map[i].depends_on = depends_on; | |
8b11a64c ZD |
2296 | } |
2297 | ||
b1b02be2 | 2298 | /* Gets cost of (USE, CANDIDATE) pair. */ |
8b11a64c | 2299 | |
b1b02be2 ZD |
2300 | static struct cost_pair * |
2301 | get_use_iv_cost (struct ivopts_data *data, struct iv_use *use, | |
2302 | struct iv_cand *cand) | |
8b11a64c | 2303 | { |
b1b02be2 ZD |
2304 | unsigned i, s; |
2305 | struct cost_pair *ret; | |
8b11a64c ZD |
2306 | |
2307 | if (!cand) | |
b1b02be2 | 2308 | return NULL; |
8b11a64c ZD |
2309 | |
2310 | if (data->consider_all_candidates) | |
8b11a64c | 2311 | { |
b1b02be2 ZD |
2312 | ret = use->cost_map + cand->id; |
2313 | if (!ret->cand) | |
2314 | return NULL; | |
8b11a64c | 2315 | |
b1b02be2 | 2316 | return ret; |
8b11a64c | 2317 | } |
b1b02be2 ZD |
2318 | |
2319 | /* n_map_members is a power of two, so this computes modulo. */ | |
2320 | s = cand->id & (use->n_map_members - 1); | |
2321 | for (i = s; i < use->n_map_members; i++) | |
2322 | if (use->cost_map[i].cand == cand) | |
2323 | return use->cost_map + i; | |
8b11a64c | 2324 | |
b1b02be2 ZD |
2325 | for (i = 0; i < s; i++) |
2326 | if (use->cost_map[i].cand == cand) | |
2327 | return use->cost_map + i; | |
2328 | ||
2329 | return NULL; | |
8b11a64c ZD |
2330 | } |
2331 | ||
2332 | /* Returns estimate on cost of computing SEQ. */ | |
2333 | ||
2334 | static unsigned | |
2335 | seq_cost (rtx seq) | |
2336 | { | |
2337 | unsigned cost = 0; | |
2338 | rtx set; | |
2339 | ||
2340 | for (; seq; seq = NEXT_INSN (seq)) | |
2341 | { | |
2342 | set = single_set (seq); | |
2343 | if (set) | |
2344 | cost += rtx_cost (set, SET); | |
2345 | else | |
2346 | cost++; | |
2347 | } | |
2348 | ||
2349 | return cost; | |
2350 | } | |
2351 | ||
8679c649 JH |
2352 | /* Produce DECL_RTL for object obj so it looks like it is stored in memory. */ |
2353 | static rtx | |
2354 | produce_memory_decl_rtl (tree obj, int *regno) | |
2355 | { | |
2356 | rtx x; | |
e16acfcd NS |
2357 | |
2358 | gcc_assert (obj); | |
8679c649 JH |
2359 | if (TREE_STATIC (obj) || DECL_EXTERNAL (obj)) |
2360 | { | |
2361 | const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj)); | |
2362 | x = gen_rtx_SYMBOL_REF (Pmode, name); | |
2363 | } | |
2364 | else | |
2365 | x = gen_raw_REG (Pmode, (*regno)++); | |
2366 | ||
2367 | return gen_rtx_MEM (DECL_MODE (obj), x); | |
2368 | } | |
2369 | ||
8b11a64c ZD |
2370 | /* Prepares decl_rtl for variables referred in *EXPR_P. Callback for |
2371 | walk_tree. DATA contains the actual fake register number. */ | |
2372 | ||
2373 | static tree | |
2374 | prepare_decl_rtl (tree *expr_p, int *ws, void *data) | |
2375 | { | |
2376 | tree obj = NULL_TREE; | |
2377 | rtx x = NULL_RTX; | |
2378 | int *regno = data; | |
2379 | ||
2380 | switch (TREE_CODE (*expr_p)) | |
2381 | { | |
8679c649 JH |
2382 | case ADDR_EXPR: |
2383 | for (expr_p = &TREE_OPERAND (*expr_p, 0); | |
afe84921 RH |
2384 | handled_component_p (*expr_p); |
2385 | expr_p = &TREE_OPERAND (*expr_p, 0)) | |
2386 | continue; | |
8679c649 JH |
2387 | obj = *expr_p; |
2388 | if (DECL_P (obj)) | |
2389 | x = produce_memory_decl_rtl (obj, regno); | |
2390 | break; | |
2391 | ||
8b11a64c ZD |
2392 | case SSA_NAME: |
2393 | *ws = 0; | |
2394 | obj = SSA_NAME_VAR (*expr_p); | |
2395 | if (!DECL_RTL_SET_P (obj)) | |
2396 | x = gen_raw_REG (DECL_MODE (obj), (*regno)++); | |
2397 | break; | |
2398 | ||
2399 | case VAR_DECL: | |
2400 | case PARM_DECL: | |
2401 | case RESULT_DECL: | |
2402 | *ws = 0; | |
2403 | obj = *expr_p; | |
2404 | ||
2405 | if (DECL_RTL_SET_P (obj)) | |
2406 | break; | |
2407 | ||
2408 | if (DECL_MODE (obj) == BLKmode) | |
8679c649 | 2409 | x = produce_memory_decl_rtl (obj, regno); |
8b11a64c ZD |
2410 | else |
2411 | x = gen_raw_REG (DECL_MODE (obj), (*regno)++); | |
2412 | ||
2413 | break; | |
2414 | ||
2415 | default: | |
2416 | break; | |
2417 | } | |
2418 | ||
2419 | if (x) | |
2420 | { | |
2421 | VARRAY_PUSH_GENERIC_PTR_NOGC (decl_rtl_to_reset, obj); | |
2422 | SET_DECL_RTL (obj, x); | |
2423 | } | |
2424 | ||
2425 | return NULL_TREE; | |
2426 | } | |
2427 | ||
2428 | /* Determines cost of the computation of EXPR. */ | |
2429 | ||
2430 | static unsigned | |
2431 | computation_cost (tree expr) | |
2432 | { | |
2433 | rtx seq, rslt; | |
2434 | tree type = TREE_TYPE (expr); | |
2435 | unsigned cost; | |
1d27fed4 DD |
2436 | /* Avoid using hard regs in ways which may be unsupported. */ |
2437 | int regno = LAST_VIRTUAL_REGISTER + 1; | |
8b11a64c ZD |
2438 | |
2439 | walk_tree (&expr, prepare_decl_rtl, ®no, NULL); | |
2440 | start_sequence (); | |
2441 | rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL); | |
2442 | seq = get_insns (); | |
2443 | end_sequence (); | |
2444 | ||
2445 | cost = seq_cost (seq); | |
2446 | if (GET_CODE (rslt) == MEM) | |
2447 | cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type)); | |
2448 | ||
2449 | return cost; | |
2450 | } | |
2451 | ||
2452 | /* Returns variable containing the value of candidate CAND at statement AT. */ | |
2453 | ||
2454 | static tree | |
2455 | var_at_stmt (struct loop *loop, struct iv_cand *cand, tree stmt) | |
2456 | { | |
2457 | if (stmt_after_increment (loop, cand, stmt)) | |
2458 | return cand->var_after; | |
2459 | else | |
2460 | return cand->var_before; | |
2461 | } | |
2462 | ||
2463 | /* Determines the expression by that USE is expressed from induction variable | |
2464 | CAND at statement AT in LOOP. */ | |
2465 | ||
2466 | static tree | |
2467 | get_computation_at (struct loop *loop, | |
2468 | struct iv_use *use, struct iv_cand *cand, tree at) | |
2469 | { | |
2f4675b4 ZD |
2470 | tree ubase = use->iv->base; |
2471 | tree ustep = use->iv->step; | |
2472 | tree cbase = cand->iv->base; | |
2473 | tree cstep = cand->iv->step; | |
8b11a64c ZD |
2474 | tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase); |
2475 | tree uutype; | |
2476 | tree expr, delta; | |
2477 | tree ratio; | |
2478 | unsigned HOST_WIDE_INT ustepi, cstepi; | |
2479 | HOST_WIDE_INT ratioi; | |
2480 | ||
2481 | if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype)) | |
2482 | { | |
2483 | /* We do not have a precision to express the values of use. */ | |
2484 | return NULL_TREE; | |
2485 | } | |
2486 | ||
2487 | expr = var_at_stmt (loop, cand, at); | |
2488 | ||
2489 | if (TREE_TYPE (expr) != ctype) | |
2490 | { | |
2491 | /* This may happen with the original ivs. */ | |
2492 | expr = fold_convert (ctype, expr); | |
2493 | } | |
2494 | ||
2495 | if (TYPE_UNSIGNED (utype)) | |
2496 | uutype = utype; | |
2497 | else | |
2498 | { | |
2499 | uutype = unsigned_type_for (utype); | |
2500 | ubase = fold_convert (uutype, ubase); | |
2501 | ustep = fold_convert (uutype, ustep); | |
2502 | } | |
2503 | ||
2504 | if (uutype != ctype) | |
2505 | { | |
2506 | expr = fold_convert (uutype, expr); | |
2507 | cbase = fold_convert (uutype, cbase); | |
2508 | cstep = fold_convert (uutype, cstep); | |
2509 | } | |
2510 | ||
2511 | if (!cst_and_fits_in_hwi (cstep) | |
2512 | || !cst_and_fits_in_hwi (ustep)) | |
2513 | return NULL_TREE; | |
2514 | ||
2515 | ustepi = int_cst_value (ustep); | |
2516 | cstepi = int_cst_value (cstep); | |
2517 | ||
2518 | if (!divide (TYPE_PRECISION (uutype), ustepi, cstepi, &ratioi)) | |
2519 | { | |
2520 | /* TODO maybe consider case when ustep divides cstep and the ratio is | |
2521 | a power of 2 (so that the division is fast to execute)? We would | |
2522 | need to be much more careful with overflows etc. then. */ | |
2523 | return NULL_TREE; | |
2524 | } | |
2525 | ||
2526 | /* We may need to shift the value if we are after the increment. */ | |
2527 | if (stmt_after_increment (loop, cand, at)) | |
2528 | cbase = fold (build2 (PLUS_EXPR, uutype, cbase, cstep)); | |
2529 | ||
b0a168b6 ZD |
2530 | /* use = ubase - ratio * cbase + ratio * var. |
2531 | ||
2532 | In general case ubase + ratio * (var - cbase) could be better (one less | |
2533 | multiplication), but often it is possible to eliminate redundant parts | |
2534 | of computations from (ubase - ratio * cbase) term, and if it does not | |
2535 | happen, fold is able to apply the distributive law to obtain this form | |
2536 | anyway. */ | |
8b11a64c ZD |
2537 | |
2538 | if (ratioi == 1) | |
2539 | { | |
2540 | delta = fold (build2 (MINUS_EXPR, uutype, ubase, cbase)); | |
2541 | expr = fold (build2 (PLUS_EXPR, uutype, expr, delta)); | |
2542 | } | |
2543 | else if (ratioi == -1) | |
2544 | { | |
2545 | delta = fold (build2 (PLUS_EXPR, uutype, ubase, cbase)); | |
2546 | expr = fold (build2 (MINUS_EXPR, uutype, delta, expr)); | |
2547 | } | |
b0a168b6 | 2548 | else |
8b11a64c ZD |
2549 | { |
2550 | ratio = build_int_cst_type (uutype, ratioi); | |
2551 | delta = fold (build2 (MULT_EXPR, uutype, ratio, cbase)); | |
2552 | delta = fold (build2 (MINUS_EXPR, uutype, ubase, delta)); | |
2553 | expr = fold (build2 (MULT_EXPR, uutype, ratio, expr)); | |
2554 | expr = fold (build2 (PLUS_EXPR, uutype, delta, expr)); | |
2555 | } | |
8b11a64c ZD |
2556 | |
2557 | return fold_convert (utype, expr); | |
2558 | } | |
2559 | ||
2560 | /* Determines the expression by that USE is expressed from induction variable | |
2561 | CAND in LOOP. */ | |
2562 | ||
2563 | static tree | |
2564 | get_computation (struct loop *loop, struct iv_use *use, struct iv_cand *cand) | |
2565 | { | |
2566 | return get_computation_at (loop, use, cand, use->stmt); | |
2567 | } | |
2568 | ||
8b11a64c ZD |
2569 | /* Returns cost of addition in MODE. */ |
2570 | ||
2571 | static unsigned | |
2572 | add_cost (enum machine_mode mode) | |
2573 | { | |
2574 | static unsigned costs[NUM_MACHINE_MODES]; | |
2575 | rtx seq; | |
2576 | unsigned cost; | |
2577 | ||
2578 | if (costs[mode]) | |
2579 | return costs[mode]; | |
2580 | ||
2581 | start_sequence (); | |
2582 | force_operand (gen_rtx_fmt_ee (PLUS, mode, | |
2583 | gen_raw_REG (mode, FIRST_PSEUDO_REGISTER), | |
2584 | gen_raw_REG (mode, FIRST_PSEUDO_REGISTER + 1)), | |
2585 | NULL_RTX); | |
2586 | seq = get_insns (); | |
2587 | end_sequence (); | |
2588 | ||
2589 | cost = seq_cost (seq); | |
2590 | if (!cost) | |
2591 | cost = 1; | |
2592 | ||
2593 | costs[mode] = cost; | |
2594 | ||
2595 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2596 | fprintf (dump_file, "Addition in %s costs %d\n", | |
2597 | GET_MODE_NAME (mode), cost); | |
2598 | return cost; | |
2599 | } | |
2600 | ||
2601 | /* Entry in a hashtable of already known costs for multiplication. */ | |
2602 | struct mbc_entry | |
2603 | { | |
2604 | HOST_WIDE_INT cst; /* The constant to multiply by. */ | |
2605 | enum machine_mode mode; /* In mode. */ | |
2606 | unsigned cost; /* The cost. */ | |
2607 | }; | |
2608 | ||
2609 | /* Counts hash value for the ENTRY. */ | |
2610 | ||
2611 | static hashval_t | |
2612 | mbc_entry_hash (const void *entry) | |
2613 | { | |
2614 | const struct mbc_entry *e = entry; | |
2615 | ||
2616 | return 57 * (hashval_t) e->mode + (hashval_t) (e->cst % 877); | |
2617 | } | |
2618 | ||
2619 | /* Compares the hash table entries ENTRY1 and ENTRY2. */ | |
2620 | ||
2621 | static int | |
2622 | mbc_entry_eq (const void *entry1, const void *entry2) | |
2623 | { | |
2624 | const struct mbc_entry *e1 = entry1; | |
2625 | const struct mbc_entry *e2 = entry2; | |
2626 | ||
2627 | return (e1->mode == e2->mode | |
2628 | && e1->cst == e2->cst); | |
2629 | } | |
2630 | ||
2631 | /* Returns cost of multiplication by constant CST in MODE. */ | |
2632 | ||
2633 | static unsigned | |
2634 | multiply_by_cost (HOST_WIDE_INT cst, enum machine_mode mode) | |
2635 | { | |
2636 | static htab_t costs; | |
2637 | struct mbc_entry **cached, act; | |
2638 | rtx seq; | |
2639 | unsigned cost; | |
2640 | ||
2641 | if (!costs) | |
2642 | costs = htab_create (100, mbc_entry_hash, mbc_entry_eq, free); | |
2643 | ||
2644 | act.mode = mode; | |
2645 | act.cst = cst; | |
2646 | cached = (struct mbc_entry **) htab_find_slot (costs, &act, INSERT); | |
2647 | if (*cached) | |
2648 | return (*cached)->cost; | |
2649 | ||
2650 | *cached = xmalloc (sizeof (struct mbc_entry)); | |
2651 | (*cached)->mode = mode; | |
2652 | (*cached)->cst = cst; | |
2653 | ||
2654 | start_sequence (); | |
2655 | expand_mult (mode, gen_raw_REG (mode, FIRST_PSEUDO_REGISTER), GEN_INT (cst), | |
2656 | NULL_RTX, 0); | |
2657 | seq = get_insns (); | |
2658 | end_sequence (); | |
2659 | ||
2660 | cost = seq_cost (seq); | |
2661 | ||
2662 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2663 | fprintf (dump_file, "Multiplication by %d in %s costs %d\n", | |
2664 | (int) cst, GET_MODE_NAME (mode), cost); | |
2665 | ||
2666 | (*cached)->cost = cost; | |
2667 | ||
2668 | return cost; | |
2669 | } | |
2670 | ||
2671 | /* Returns cost of address in shape symbol + var + OFFSET + RATIO * index. | |
2672 | If SYMBOL_PRESENT is false, symbol is omitted. If VAR_PRESENT is false, | |
2673 | variable is omitted. The created memory accesses MODE. | |
2674 | ||
2675 | TODO -- there must be some better way. This all is quite crude. */ | |
2676 | ||
2677 | static unsigned | |
2678 | get_address_cost (bool symbol_present, bool var_present, | |
2679 | unsigned HOST_WIDE_INT offset, HOST_WIDE_INT ratio) | |
2680 | { | |
2681 | #define MAX_RATIO 128 | |
2682 | static sbitmap valid_mult; | |
2683 | static HOST_WIDE_INT rat, off; | |
2684 | static HOST_WIDE_INT min_offset, max_offset; | |
2685 | static unsigned costs[2][2][2][2]; | |
2686 | unsigned cost, acost; | |
2687 | rtx seq, addr, base; | |
2688 | bool offset_p, ratio_p; | |
2689 | rtx reg1; | |
2690 | HOST_WIDE_INT s_offset; | |
2691 | unsigned HOST_WIDE_INT mask; | |
2692 | unsigned bits; | |
2693 | ||
2694 | if (!valid_mult) | |
2695 | { | |
2696 | HOST_WIDE_INT i; | |
2697 | ||
2698 | reg1 = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER); | |
2699 | ||
2700 | addr = gen_rtx_fmt_ee (PLUS, Pmode, reg1, NULL_RTX); | |
2701 | for (i = 1; i <= 1 << 20; i <<= 1) | |
2702 | { | |
2703 | XEXP (addr, 1) = GEN_INT (i); | |
2704 | if (!memory_address_p (Pmode, addr)) | |
2705 | break; | |
2706 | } | |
2707 | max_offset = i >> 1; | |
2708 | off = max_offset; | |
2709 | ||
2710 | for (i = 1; i <= 1 << 20; i <<= 1) | |
2711 | { | |
2712 | XEXP (addr, 1) = GEN_INT (-i); | |
2713 | if (!memory_address_p (Pmode, addr)) | |
2714 | break; | |
2715 | } | |
2716 | min_offset = -(i >> 1); | |
2717 | ||
2718 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2719 | { | |
2720 | fprintf (dump_file, "get_address_cost:\n"); | |
2721 | fprintf (dump_file, " min offset %d\n", (int) min_offset); | |
2722 | fprintf (dump_file, " max offset %d\n", (int) max_offset); | |
2723 | } | |
2724 | ||
2725 | valid_mult = sbitmap_alloc (2 * MAX_RATIO + 1); | |
2726 | sbitmap_zero (valid_mult); | |
2727 | rat = 1; | |
2728 | addr = gen_rtx_fmt_ee (MULT, Pmode, reg1, NULL_RTX); | |
2729 | for (i = -MAX_RATIO; i <= MAX_RATIO; i++) | |
2730 | { | |
2731 | XEXP (addr, 1) = GEN_INT (i); | |
2732 | if (memory_address_p (Pmode, addr)) | |
2733 | { | |
2734 | SET_BIT (valid_mult, i + MAX_RATIO); | |
2735 | rat = i; | |
2736 | } | |
2737 | } | |
2738 | ||
2739 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2740 | { | |
2741 | fprintf (dump_file, " allowed multipliers:"); | |
2742 | for (i = -MAX_RATIO; i <= MAX_RATIO; i++) | |
2743 | if (TEST_BIT (valid_mult, i + MAX_RATIO)) | |
2744 | fprintf (dump_file, " %d", (int) i); | |
2745 | fprintf (dump_file, "\n"); | |
2746 | fprintf (dump_file, "\n"); | |
2747 | } | |
2748 | } | |
2749 | ||
2750 | bits = GET_MODE_BITSIZE (Pmode); | |
2751 | mask = ~(~(unsigned HOST_WIDE_INT) 0 << (bits - 1) << 1); | |
2752 | offset &= mask; | |
2753 | if ((offset >> (bits - 1) & 1)) | |
2754 | offset |= ~mask; | |
2755 | s_offset = offset; | |
2756 | ||
2757 | cost = 0; | |
a85a9024 UW |
2758 | offset_p = (s_offset != 0 |
2759 | && min_offset <= s_offset && s_offset <= max_offset); | |
8b11a64c ZD |
2760 | ratio_p = (ratio != 1 |
2761 | && -MAX_RATIO <= ratio && ratio <= MAX_RATIO | |
2762 | && TEST_BIT (valid_mult, ratio + MAX_RATIO)); | |
2763 | ||
2764 | if (ratio != 1 && !ratio_p) | |
2765 | cost += multiply_by_cost (ratio, Pmode); | |
2766 | ||
2767 | if (s_offset && !offset_p && !symbol_present) | |
2768 | { | |
2769 | cost += add_cost (Pmode); | |
2770 | var_present = true; | |
2771 | } | |
2772 | ||
2773 | acost = costs[symbol_present][var_present][offset_p][ratio_p]; | |
2774 | if (!acost) | |
2775 | { | |
2776 | acost = 0; | |
2777 | ||
2778 | addr = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER); | |
2779 | reg1 = gen_raw_REG (Pmode, FIRST_PSEUDO_REGISTER + 1); | |
2780 | if (ratio_p) | |
2781 | addr = gen_rtx_fmt_ee (MULT, Pmode, addr, GEN_INT (rat)); | |
2782 | ||
a85a9024 | 2783 | if (var_present) |
7299dbfb | 2784 | addr = gen_rtx_fmt_ee (PLUS, Pmode, addr, reg1); |
a85a9024 | 2785 | |
8b11a64c ZD |
2786 | if (symbol_present) |
2787 | { | |
2788 | base = gen_rtx_SYMBOL_REF (Pmode, ggc_strdup ("")); | |
2789 | if (offset_p) | |
2790 | base = gen_rtx_fmt_e (CONST, Pmode, | |
2791 | gen_rtx_fmt_ee (PLUS, Pmode, | |
2792 | base, | |
2793 | GEN_INT (off))); | |
8b11a64c ZD |
2794 | } |
2795 | else if (offset_p) | |
2796 | base = GEN_INT (off); | |
2797 | else | |
2798 | base = NULL_RTX; | |
2799 | ||
2800 | if (base) | |
7299dbfb | 2801 | addr = gen_rtx_fmt_ee (PLUS, Pmode, addr, base); |
8b11a64c ZD |
2802 | |
2803 | start_sequence (); | |
2804 | addr = memory_address (Pmode, addr); | |
2805 | seq = get_insns (); | |
2806 | end_sequence (); | |
2807 | ||
2808 | acost = seq_cost (seq); | |
2809 | acost += address_cost (addr, Pmode); | |
2810 | ||
2811 | if (!acost) | |
2812 | acost = 1; | |
2813 | costs[symbol_present][var_present][offset_p][ratio_p] = acost; | |
2814 | } | |
2815 | ||
2816 | return cost + acost; | |
2817 | } | |
2818 | ||
2819 | /* Records invariants in *EXPR_P. Callback for walk_tree. DATA contains | |
2820 | the bitmap to that we should store it. */ | |
2821 | ||
2822 | static struct ivopts_data *fd_ivopts_data; | |
2823 | static tree | |
2824 | find_depends (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data) | |
2825 | { | |
2826 | bitmap *depends_on = data; | |
2827 | struct version_info *info; | |
2828 | ||
2829 | if (TREE_CODE (*expr_p) != SSA_NAME) | |
2830 | return NULL_TREE; | |
2831 | info = name_info (fd_ivopts_data, *expr_p); | |
2832 | ||
2833 | if (!info->inv_id || info->has_nonlin_use) | |
2834 | return NULL_TREE; | |
2835 | ||
2836 | if (!*depends_on) | |
8bdbfff5 | 2837 | *depends_on = BITMAP_ALLOC (NULL); |
8b11a64c ZD |
2838 | bitmap_set_bit (*depends_on, info->inv_id); |
2839 | ||
2840 | return NULL_TREE; | |
2841 | } | |
2842 | ||
7299dbfb | 2843 | /* Estimates cost of forcing EXPR into a variable. DEPENDS_ON is a set of the |
8b11a64c ZD |
2844 | invariants the computation depends on. */ |
2845 | ||
2846 | static unsigned | |
2847 | force_var_cost (struct ivopts_data *data, | |
2848 | tree expr, bitmap *depends_on) | |
2849 | { | |
2850 | static bool costs_initialized = false; | |
2851 | static unsigned integer_cost; | |
2852 | static unsigned symbol_cost; | |
2853 | static unsigned address_cost; | |
7299dbfb ZD |
2854 | tree op0, op1; |
2855 | unsigned cost0, cost1, cost; | |
2856 | enum machine_mode mode; | |
8b11a64c ZD |
2857 | |
2858 | if (!costs_initialized) | |
2859 | { | |
2860 | tree var = create_tmp_var_raw (integer_type_node, "test_var"); | |
2861 | rtx x = gen_rtx_MEM (DECL_MODE (var), | |
2862 | gen_rtx_SYMBOL_REF (Pmode, "test_var")); | |
2863 | tree addr; | |
2864 | tree type = build_pointer_type (integer_type_node); | |
2865 | ||
2866 | integer_cost = computation_cost (build_int_cst_type (integer_type_node, | |
2867 | 2000)); | |
2868 | ||
2869 | SET_DECL_RTL (var, x); | |
2870 | TREE_STATIC (var) = 1; | |
2871 | addr = build1 (ADDR_EXPR, type, var); | |
2872 | symbol_cost = computation_cost (addr) + 1; | |
2873 | ||
2874 | address_cost | |
2875 | = computation_cost (build2 (PLUS_EXPR, type, | |
2876 | addr, | |
2877 | build_int_cst_type (type, 2000))) + 1; | |
2878 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2879 | { | |
2880 | fprintf (dump_file, "force_var_cost:\n"); | |
2881 | fprintf (dump_file, " integer %d\n", (int) integer_cost); | |
2882 | fprintf (dump_file, " symbol %d\n", (int) symbol_cost); | |
2883 | fprintf (dump_file, " address %d\n", (int) address_cost); | |
2884 | fprintf (dump_file, " other %d\n", (int) target_spill_cost); | |
2885 | fprintf (dump_file, "\n"); | |
2886 | } | |
2887 | ||
2888 | costs_initialized = true; | |
2889 | } | |
2890 | ||
f5e2738c ZD |
2891 | STRIP_NOPS (expr); |
2892 | ||
8b11a64c ZD |
2893 | if (depends_on) |
2894 | { | |
2895 | fd_ivopts_data = data; | |
2896 | walk_tree (&expr, find_depends, depends_on, NULL); | |
2897 | } | |
2898 | ||
2899 | if (SSA_VAR_P (expr)) | |
2900 | return 0; | |
2901 | ||
2902 | if (TREE_INVARIANT (expr)) | |
2903 | { | |
2904 | if (TREE_CODE (expr) == INTEGER_CST) | |
2905 | return integer_cost; | |
2906 | ||
2907 | if (TREE_CODE (expr) == ADDR_EXPR) | |
2908 | { | |
2909 | tree obj = TREE_OPERAND (expr, 0); | |
2910 | ||
2911 | if (TREE_CODE (obj) == VAR_DECL | |
2912 | || TREE_CODE (obj) == PARM_DECL | |
2913 | || TREE_CODE (obj) == RESULT_DECL) | |
2914 | return symbol_cost; | |
2915 | } | |
2916 | ||
2917 | return address_cost; | |
2918 | } | |
2919 | ||
7299dbfb ZD |
2920 | switch (TREE_CODE (expr)) |
2921 | { | |
2922 | case PLUS_EXPR: | |
2923 | case MINUS_EXPR: | |
2924 | case MULT_EXPR: | |
2925 | op0 = TREE_OPERAND (expr, 0); | |
2926 | op1 = TREE_OPERAND (expr, 1); | |
f5e2738c ZD |
2927 | STRIP_NOPS (op0); |
2928 | STRIP_NOPS (op1); | |
7299dbfb ZD |
2929 | |
2930 | if (is_gimple_val (op0)) | |
2931 | cost0 = 0; | |
2932 | else | |
2933 | cost0 = force_var_cost (data, op0, NULL); | |
2934 | ||
2935 | if (is_gimple_val (op1)) | |
2936 | cost1 = 0; | |
2937 | else | |
2938 | cost1 = force_var_cost (data, op1, NULL); | |
2939 | ||
2940 | break; | |
2941 | ||
2942 | default: | |
2943 | /* Just an arbitrary value, FIXME. */ | |
2944 | return target_spill_cost; | |
2945 | } | |
2946 | ||
2947 | mode = TYPE_MODE (TREE_TYPE (expr)); | |
2948 | switch (TREE_CODE (expr)) | |
2949 | { | |
2950 | case PLUS_EXPR: | |
2951 | case MINUS_EXPR: | |
2952 | cost = add_cost (mode); | |
2953 | break; | |
2954 | ||
2955 | case MULT_EXPR: | |
2956 | if (cst_and_fits_in_hwi (op0)) | |
2957 | cost = multiply_by_cost (int_cst_value (op0), mode); | |
2958 | else if (cst_and_fits_in_hwi (op1)) | |
2959 | cost = multiply_by_cost (int_cst_value (op1), mode); | |
2960 | else | |
2961 | return target_spill_cost; | |
2962 | break; | |
2963 | ||
2964 | default: | |
2965 | gcc_unreachable (); | |
2966 | } | |
2967 | ||
2968 | cost += cost0; | |
2969 | cost += cost1; | |
2970 | ||
2971 | /* Bound the cost by target_spill_cost. The parts of complicated | |
2972 | computations often are either loop invariant or at least can | |
2973 | be shared between several iv uses, so letting this grow without | |
2974 | limits would not give reasonable results. */ | |
2975 | return cost < target_spill_cost ? cost : target_spill_cost; | |
8b11a64c ZD |
2976 | } |
2977 | ||
8b11a64c ZD |
2978 | /* Estimates cost of expressing address ADDR as var + symbol + offset. The |
2979 | value of offset is added to OFFSET, SYMBOL_PRESENT and VAR_PRESENT are set | |
2980 | to false if the corresponding part is missing. DEPENDS_ON is a set of the | |
2981 | invariants the computation depends on. */ | |
2982 | ||
2983 | static unsigned | |
2984 | split_address_cost (struct ivopts_data *data, | |
2985 | tree addr, bool *symbol_present, bool *var_present, | |
2986 | unsigned HOST_WIDE_INT *offset, bitmap *depends_on) | |
2987 | { | |
2f4675b4 ZD |
2988 | tree core; |
2989 | HOST_WIDE_INT bitsize; | |
2990 | HOST_WIDE_INT bitpos; | |
2991 | tree toffset; | |
2992 | enum machine_mode mode; | |
2993 | int unsignedp, volatilep; | |
2994 | ||
2995 | core = get_inner_reference (addr, &bitsize, &bitpos, &toffset, &mode, | |
2614034e | 2996 | &unsignedp, &volatilep, false); |
8b11a64c | 2997 | |
2f4675b4 ZD |
2998 | if (toffset != 0 |
2999 | || bitpos % BITS_PER_UNIT != 0 | |
3000 | || TREE_CODE (core) != VAR_DECL) | |
8b11a64c ZD |
3001 | { |
3002 | *symbol_present = false; | |
3003 | *var_present = true; | |
3004 | fd_ivopts_data = data; | |
3005 | walk_tree (&addr, find_depends, depends_on, NULL); | |
3006 | return target_spill_cost; | |
2f4675b4 ZD |
3007 | } |
3008 | ||
3009 | *offset += bitpos / BITS_PER_UNIT; | |
8b11a64c ZD |
3010 | if (TREE_STATIC (core) |
3011 | || DECL_EXTERNAL (core)) | |
3012 | { | |
3013 | *symbol_present = true; | |
3014 | *var_present = false; | |
3015 | return 0; | |
3016 | } | |
3017 | ||
3018 | *symbol_present = false; | |
3019 | *var_present = true; | |
3020 | return 0; | |
3021 | } | |
3022 | ||
3023 | /* Estimates cost of expressing difference of addresses E1 - E2 as | |
3024 | var + symbol + offset. The value of offset is added to OFFSET, | |
3025 | SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding | |
3026 | part is missing. DEPENDS_ON is a set of the invariants the computation | |
3027 | depends on. */ | |
3028 | ||
3029 | static unsigned | |
3030 | ptr_difference_cost (struct ivopts_data *data, | |
3031 | tree e1, tree e2, bool *symbol_present, bool *var_present, | |
3032 | unsigned HOST_WIDE_INT *offset, bitmap *depends_on) | |
3033 | { | |
2f4675b4 | 3034 | HOST_WIDE_INT diff = 0; |
8b11a64c ZD |
3035 | unsigned cost; |
3036 | ||
1e128c5f | 3037 | gcc_assert (TREE_CODE (e1) == ADDR_EXPR); |
8b11a64c | 3038 | |
7299dbfb | 3039 | if (ptr_difference_const (e1, e2, &diff)) |
8b11a64c ZD |
3040 | { |
3041 | *offset += diff; | |
3042 | *symbol_present = false; | |
3043 | *var_present = false; | |
3044 | return 0; | |
3045 | } | |
3046 | ||
3047 | if (e2 == integer_zero_node) | |
3048 | return split_address_cost (data, TREE_OPERAND (e1, 0), | |
3049 | symbol_present, var_present, offset, depends_on); | |
3050 | ||
3051 | *symbol_present = false; | |
3052 | *var_present = true; | |
3053 | ||
3054 | cost = force_var_cost (data, e1, depends_on); | |
3055 | cost += force_var_cost (data, e2, depends_on); | |
3056 | cost += add_cost (Pmode); | |
3057 | ||
3058 | return cost; | |
3059 | } | |
3060 | ||
3061 | /* Estimates cost of expressing difference E1 - E2 as | |
3062 | var + symbol + offset. The value of offset is added to OFFSET, | |
3063 | SYMBOL_PRESENT and VAR_PRESENT are set to false if the corresponding | |
3064 | part is missing. DEPENDS_ON is a set of the invariants the computation | |
3065 | depends on. */ | |
3066 | ||
3067 | static unsigned | |
3068 | difference_cost (struct ivopts_data *data, | |
3069 | tree e1, tree e2, bool *symbol_present, bool *var_present, | |
3070 | unsigned HOST_WIDE_INT *offset, bitmap *depends_on) | |
3071 | { | |
3072 | unsigned cost; | |
3073 | enum machine_mode mode = TYPE_MODE (TREE_TYPE (e1)); | |
f5e2738c ZD |
3074 | unsigned HOST_WIDE_INT off1, off2; |
3075 | ||
3076 | e1 = strip_offset (e1, false, &off1); | |
3077 | e2 = strip_offset (e2, false, &off2); | |
3078 | *offset += off1 - off2; | |
8b11a64c | 3079 | |
f5e2738c ZD |
3080 | STRIP_NOPS (e1); |
3081 | STRIP_NOPS (e2); | |
8b11a64c ZD |
3082 | |
3083 | if (TREE_CODE (e1) == ADDR_EXPR) | |
3084 | return ptr_difference_cost (data, e1, e2, symbol_present, var_present, offset, | |
3085 | depends_on); | |
3086 | *symbol_present = false; | |
3087 | ||
3088 | if (operand_equal_p (e1, e2, 0)) | |
3089 | { | |
3090 | *var_present = false; | |
3091 | return 0; | |
3092 | } | |
3093 | *var_present = true; | |
3094 | if (zero_p (e2)) | |
3095 | return force_var_cost (data, e1, depends_on); | |
3096 | ||
3097 | if (zero_p (e1)) | |
3098 | { | |
3099 | cost = force_var_cost (data, e2, depends_on); | |
3100 | cost += multiply_by_cost (-1, mode); | |
3101 | ||
3102 | return cost; | |
3103 | } | |
3104 | ||
3105 | cost = force_var_cost (data, e1, depends_on); | |
3106 | cost += force_var_cost (data, e2, depends_on); | |
3107 | cost += add_cost (mode); | |
3108 | ||
3109 | return cost; | |
3110 | } | |
3111 | ||
3112 | /* Determines the cost of the computation by that USE is expressed | |
3113 | from induction variable CAND. If ADDRESS_P is true, we just need | |
3114 | to create an address from it, otherwise we want to get it into | |
3115 | register. A set of invariants we depend on is stored in | |
3116 | DEPENDS_ON. AT is the statement at that the value is computed. */ | |
3117 | ||
3118 | static unsigned | |
3119 | get_computation_cost_at (struct ivopts_data *data, | |
3120 | struct iv_use *use, struct iv_cand *cand, | |
3121 | bool address_p, bitmap *depends_on, tree at) | |
3122 | { | |
3123 | tree ubase = use->iv->base, ustep = use->iv->step; | |
3124 | tree cbase, cstep; | |
3125 | tree utype = TREE_TYPE (ubase), ctype; | |
3126 | unsigned HOST_WIDE_INT ustepi, cstepi, offset = 0; | |
3127 | HOST_WIDE_INT ratio, aratio; | |
3128 | bool var_present, symbol_present; | |
3129 | unsigned cost = 0, n_sums; | |
3130 | ||
3131 | *depends_on = NULL; | |
3132 | ||
3133 | /* Only consider real candidates. */ | |
3134 | if (!cand->iv) | |
3135 | return INFTY; | |
3136 | ||
3137 | cbase = cand->iv->base; | |
3138 | cstep = cand->iv->step; | |
3139 | ctype = TREE_TYPE (cbase); | |
3140 | ||
3141 | if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype)) | |
3142 | { | |
3143 | /* We do not have a precision to express the values of use. */ | |
3144 | return INFTY; | |
3145 | } | |
3146 | ||
e6845c23 ZD |
3147 | if (address_p) |
3148 | { | |
3149 | /* Do not try to express address of an object with computation based | |
3150 | on address of a different object. This may cause problems in rtl | |
3151 | level alias analysis (that does not expect this to be happening, | |
3152 | as this is illegal in C), and would be unlikely to be useful | |
3153 | anyway. */ | |
3154 | if (use->iv->base_object | |
3155 | && cand->iv->base_object | |
3156 | && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0)) | |
3157 | return INFTY; | |
3158 | } | |
3159 | ||
8b11a64c ZD |
3160 | if (!cst_and_fits_in_hwi (ustep) |
3161 | || !cst_and_fits_in_hwi (cstep)) | |
3162 | return INFTY; | |
3163 | ||
3164 | if (TREE_CODE (ubase) == INTEGER_CST | |
3165 | && !cst_and_fits_in_hwi (ubase)) | |
3166 | goto fallback; | |
3167 | ||
3168 | if (TREE_CODE (cbase) == INTEGER_CST | |
3169 | && !cst_and_fits_in_hwi (cbase)) | |
3170 | goto fallback; | |
3171 | ||
3172 | ustepi = int_cst_value (ustep); | |
3173 | cstepi = int_cst_value (cstep); | |
3174 | ||
3175 | if (TYPE_PRECISION (utype) != TYPE_PRECISION (ctype)) | |
3176 | { | |
3177 | /* TODO -- add direct handling of this case. */ | |
3178 | goto fallback; | |
3179 | } | |
3180 | ||
3181 | if (!divide (TYPE_PRECISION (utype), ustepi, cstepi, &ratio)) | |
3182 | return INFTY; | |
3183 | ||
3184 | /* use = ubase + ratio * (var - cbase). If either cbase is a constant | |
3185 | or ratio == 1, it is better to handle this like | |
3186 | ||
3187 | ubase - ratio * cbase + ratio * var | |
3188 | ||
3189 | (also holds in the case ratio == -1, TODO. */ | |
3190 | ||
3191 | if (TREE_CODE (cbase) == INTEGER_CST) | |
3192 | { | |
3193 | offset = - ratio * int_cst_value (cbase); | |
3194 | cost += difference_cost (data, | |
3195 | ubase, integer_zero_node, | |
3196 | &symbol_present, &var_present, &offset, | |
3197 | depends_on); | |
3198 | } | |
3199 | else if (ratio == 1) | |
3200 | { | |
3201 | cost += difference_cost (data, | |
3202 | ubase, cbase, | |
3203 | &symbol_present, &var_present, &offset, | |
3204 | depends_on); | |
3205 | } | |
3206 | else | |
3207 | { | |
3208 | cost += force_var_cost (data, cbase, depends_on); | |
3209 | cost += add_cost (TYPE_MODE (ctype)); | |
3210 | cost += difference_cost (data, | |
3211 | ubase, integer_zero_node, | |
3212 | &symbol_present, &var_present, &offset, | |
3213 | depends_on); | |
3214 | } | |
3215 | ||
3216 | /* If we are after the increment, the value of the candidate is higher by | |
3217 | one iteration. */ | |
3218 | if (stmt_after_increment (data->current_loop, cand, at)) | |
3219 | offset -= ratio * cstepi; | |
3220 | ||
3221 | /* Now the computation is in shape symbol + var1 + const + ratio * var2. | |
3222 | (symbol/var/const parts may be omitted). If we are looking for an address, | |
3223 | find the cost of addressing this. */ | |
3224 | if (address_p) | |
7299dbfb | 3225 | return cost + get_address_cost (symbol_present, var_present, offset, ratio); |
8b11a64c ZD |
3226 | |
3227 | /* Otherwise estimate the costs for computing the expression. */ | |
3228 | aratio = ratio > 0 ? ratio : -ratio; | |
3229 | if (!symbol_present && !var_present && !offset) | |
3230 | { | |
3231 | if (ratio != 1) | |
3232 | cost += multiply_by_cost (ratio, TYPE_MODE (ctype)); | |
3233 | ||
3234 | return cost; | |
3235 | } | |
3236 | ||
3237 | if (aratio != 1) | |
3238 | cost += multiply_by_cost (aratio, TYPE_MODE (ctype)); | |
3239 | ||
3240 | n_sums = 1; | |
3241 | if (var_present | |
3242 | /* Symbol + offset should be compile-time computable. */ | |
3243 | && (symbol_present || offset)) | |
3244 | n_sums++; | |
3245 | ||
3246 | return cost + n_sums * add_cost (TYPE_MODE (ctype)); | |
3247 | ||
3248 | fallback: | |
3249 | { | |
3250 | /* Just get the expression, expand it and measure the cost. */ | |
3251 | tree comp = get_computation_at (data->current_loop, use, cand, at); | |
3252 | ||
3253 | if (!comp) | |
3254 | return INFTY; | |
3255 | ||
3256 | if (address_p) | |
3257 | comp = build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (comp)), comp); | |
3258 | ||
3259 | return computation_cost (comp); | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | /* Determines the cost of the computation by that USE is expressed | |
3264 | from induction variable CAND. If ADDRESS_P is true, we just need | |
3265 | to create an address from it, otherwise we want to get it into | |
3266 | register. A set of invariants we depend on is stored in | |
3267 | DEPENDS_ON. */ | |
3268 | ||
3269 | static unsigned | |
3270 | get_computation_cost (struct ivopts_data *data, | |
3271 | struct iv_use *use, struct iv_cand *cand, | |
3272 | bool address_p, bitmap *depends_on) | |
3273 | { | |
3274 | return get_computation_cost_at (data, | |
3275 | use, cand, address_p, depends_on, use->stmt); | |
3276 | } | |
3277 | ||
3278 | /* Determines cost of basing replacement of USE on CAND in a generic | |
3279 | expression. */ | |
3280 | ||
b1b02be2 | 3281 | static bool |
8b11a64c ZD |
3282 | determine_use_iv_cost_generic (struct ivopts_data *data, |
3283 | struct iv_use *use, struct iv_cand *cand) | |
3284 | { | |
3285 | bitmap depends_on; | |
eec5fec9 ZD |
3286 | unsigned cost; |
3287 | ||
3288 | /* The simple case first -- if we need to express value of the preserved | |
3289 | original biv, the cost is 0. This also prevents us from counting the | |
3290 | cost of increment twice -- once at this use and once in the cost of | |
3291 | the candidate. */ | |
3292 | if (cand->pos == IP_ORIGINAL | |
3293 | && cand->incremented_at == use->stmt) | |
3294 | { | |
3295 | set_use_iv_cost (data, use, cand, 0, NULL); | |
3296 | return true; | |
3297 | } | |
8b11a64c | 3298 | |
eec5fec9 | 3299 | cost = get_computation_cost (data, use, cand, false, &depends_on); |
8b11a64c | 3300 | set_use_iv_cost (data, use, cand, cost, depends_on); |
b1b02be2 ZD |
3301 | |
3302 | return cost != INFTY; | |
8b11a64c ZD |
3303 | } |
3304 | ||
3305 | /* Determines cost of basing replacement of USE on CAND in an address. */ | |
3306 | ||
b1b02be2 | 3307 | static bool |
8b11a64c ZD |
3308 | determine_use_iv_cost_address (struct ivopts_data *data, |
3309 | struct iv_use *use, struct iv_cand *cand) | |
3310 | { | |
3311 | bitmap depends_on; | |
3312 | unsigned cost = get_computation_cost (data, use, cand, true, &depends_on); | |
3313 | ||
3314 | set_use_iv_cost (data, use, cand, cost, depends_on); | |
b1b02be2 ZD |
3315 | |
3316 | return cost != INFTY; | |
8b11a64c ZD |
3317 | } |
3318 | ||
3319 | /* Computes value of induction variable IV in iteration NITER. */ | |
3320 | ||
3321 | static tree | |
3322 | iv_value (struct iv *iv, tree niter) | |
3323 | { | |
3324 | tree val; | |
3325 | tree type = TREE_TYPE (iv->base); | |
3326 | ||
3327 | niter = fold_convert (type, niter); | |
2f4675b4 | 3328 | val = fold (build2 (MULT_EXPR, type, iv->step, niter)); |
8b11a64c ZD |
3329 | |
3330 | return fold (build2 (PLUS_EXPR, type, iv->base, val)); | |
3331 | } | |
3332 | ||
3333 | /* Computes value of candidate CAND at position AT in iteration NITER. */ | |
3334 | ||
3335 | static tree | |
3336 | cand_value_at (struct loop *loop, struct iv_cand *cand, tree at, tree niter) | |
3337 | { | |
3338 | tree val = iv_value (cand->iv, niter); | |
3339 | tree type = TREE_TYPE (cand->iv->base); | |
3340 | ||
3341 | if (stmt_after_increment (loop, cand, at)) | |
3342 | val = fold (build2 (PLUS_EXPR, type, val, cand->iv->step)); | |
3343 | ||
3344 | return val; | |
3345 | } | |
3346 | ||
ca4c3169 ZD |
3347 | /* Returns period of induction variable iv. */ |
3348 | ||
3349 | static tree | |
3350 | iv_period (struct iv *iv) | |
3351 | { | |
3352 | tree step = iv->step, period, type; | |
3353 | tree pow2div; | |
3354 | ||
3355 | gcc_assert (step && TREE_CODE (step) == INTEGER_CST); | |
3356 | ||
3357 | /* Period of the iv is gcd (step, type range). Since type range is power | |
3358 | of two, it suffices to determine the maximum power of two that divides | |
3359 | step. */ | |
3360 | pow2div = num_ending_zeros (step); | |
3361 | type = unsigned_type_for (TREE_TYPE (step)); | |
3362 | ||
3363 | period = build_low_bits_mask (type, | |
3364 | (TYPE_PRECISION (type) | |
3365 | - tree_low_cst (pow2div, 1))); | |
3366 | ||
3367 | return period; | |
3368 | } | |
3369 | ||
8b11a64c ZD |
3370 | /* Check whether it is possible to express the condition in USE by comparison |
3371 | of candidate CAND. If so, store the comparison code to COMPARE and the | |
3372 | value compared with to BOUND. */ | |
3373 | ||
3374 | static bool | |
ca4c3169 | 3375 | may_eliminate_iv (struct ivopts_data *data, |
8b11a64c ZD |
3376 | struct iv_use *use, struct iv_cand *cand, |
3377 | enum tree_code *compare, tree *bound) | |
3378 | { | |
e6845c23 | 3379 | basic_block ex_bb; |
8b11a64c | 3380 | edge exit; |
ca4c3169 ZD |
3381 | struct tree_niter_desc *niter; |
3382 | tree nit, nit_type; | |
3383 | tree wider_type, period, per_type; | |
3384 | struct loop *loop = data->current_loop; | |
e6845c23 ZD |
3385 | |
3386 | /* For now works only for exits that dominate the loop latch. TODO -- extend | |
3387 | for other conditions inside loop body. */ | |
3388 | ex_bb = bb_for_stmt (use->stmt); | |
3389 | if (use->stmt != last_stmt (ex_bb) | |
3390 | || TREE_CODE (use->stmt) != COND_EXPR) | |
8b11a64c | 3391 | return false; |
e6845c23 | 3392 | if (!dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb)) |
8b11a64c ZD |
3393 | return false; |
3394 | ||
e6845c23 ZD |
3395 | exit = EDGE_SUCC (ex_bb, 0); |
3396 | if (flow_bb_inside_loop_p (loop, exit->dest)) | |
3397 | exit = EDGE_SUCC (ex_bb, 1); | |
3398 | if (flow_bb_inside_loop_p (loop, exit->dest)) | |
3399 | return false; | |
3400 | ||
ca4c3169 ZD |
3401 | niter = niter_for_exit (data, exit); |
3402 | if (!niter | |
3403 | || !zero_p (niter->may_be_zero)) | |
8b11a64c ZD |
3404 | return false; |
3405 | ||
ca4c3169 ZD |
3406 | nit = niter->niter; |
3407 | nit_type = TREE_TYPE (nit); | |
8b11a64c | 3408 | |
ca4c3169 ZD |
3409 | /* Determine whether we may use the variable to test whether niter iterations |
3410 | elapsed. This is the case iff the period of the induction variable is | |
3411 | greater than the number of iterations. */ | |
3412 | period = iv_period (cand->iv); | |
3413 | if (!period) | |
8b11a64c | 3414 | return false; |
ca4c3169 ZD |
3415 | per_type = TREE_TYPE (period); |
3416 | ||
3417 | wider_type = TREE_TYPE (period); | |
3418 | if (TYPE_PRECISION (nit_type) < TYPE_PRECISION (per_type)) | |
3419 | wider_type = per_type; | |
3420 | else | |
3421 | wider_type = nit_type; | |
8b11a64c | 3422 | |
ca4c3169 ZD |
3423 | if (!integer_nonzerop (fold (build2 (GE_EXPR, boolean_type_node, |
3424 | fold_convert (wider_type, period), | |
3425 | fold_convert (wider_type, nit))))) | |
8b11a64c ZD |
3426 | return false; |
3427 | ||
ca4c3169 ZD |
3428 | if (exit->flags & EDGE_TRUE_VALUE) |
3429 | *compare = EQ_EXPR; | |
3430 | else | |
3431 | *compare = NE_EXPR; | |
3432 | ||
3433 | *bound = cand_value_at (loop, cand, use->stmt, nit); | |
8b11a64c ZD |
3434 | return true; |
3435 | } | |
3436 | ||
3437 | /* Determines cost of basing replacement of USE on CAND in a condition. */ | |
3438 | ||
b1b02be2 | 3439 | static bool |
8b11a64c ZD |
3440 | determine_use_iv_cost_condition (struct ivopts_data *data, |
3441 | struct iv_use *use, struct iv_cand *cand) | |
3442 | { | |
3443 | tree bound; | |
3444 | enum tree_code compare; | |
3445 | ||
3446 | /* Only consider real candidates. */ | |
3447 | if (!cand->iv) | |
3448 | { | |
3449 | set_use_iv_cost (data, use, cand, INFTY, NULL); | |
b1b02be2 | 3450 | return false; |
8b11a64c ZD |
3451 | } |
3452 | ||
ca4c3169 | 3453 | if (may_eliminate_iv (data, use, cand, &compare, &bound)) |
8b11a64c ZD |
3454 | { |
3455 | bitmap depends_on = NULL; | |
3456 | unsigned cost = force_var_cost (data, bound, &depends_on); | |
3457 | ||
3458 | set_use_iv_cost (data, use, cand, cost, depends_on); | |
b1b02be2 | 3459 | return cost != INFTY; |
8b11a64c ZD |
3460 | } |
3461 | ||
3462 | /* The induction variable elimination failed; just express the original | |
3463 | giv. If it is compared with an invariant, note that we cannot get | |
3464 | rid of it. */ | |
3465 | if (TREE_CODE (*use->op_p) == SSA_NAME) | |
3466 | record_invariant (data, *use->op_p, true); | |
3467 | else | |
3468 | { | |
3469 | record_invariant (data, TREE_OPERAND (*use->op_p, 0), true); | |
3470 | record_invariant (data, TREE_OPERAND (*use->op_p, 1), true); | |
3471 | } | |
3472 | ||
b1b02be2 | 3473 | return determine_use_iv_cost_generic (data, use, cand); |
8b11a64c ZD |
3474 | } |
3475 | ||
3476 | /* Checks whether it is possible to replace the final value of USE by | |
3477 | a direct computation. If so, the formula is stored to *VALUE. */ | |
3478 | ||
3479 | static bool | |
ca4c3169 ZD |
3480 | may_replace_final_value (struct ivopts_data *data, struct iv_use *use, |
3481 | tree *value) | |
8b11a64c | 3482 | { |
ca4c3169 | 3483 | struct loop *loop = data->current_loop; |
8b11a64c ZD |
3484 | edge exit; |
3485 | struct tree_niter_desc *niter; | |
3486 | ||
3487 | exit = single_dom_exit (loop); | |
3488 | if (!exit) | |
3489 | return false; | |
3490 | ||
1e128c5f GB |
3491 | gcc_assert (dominated_by_p (CDI_DOMINATORS, exit->src, |
3492 | bb_for_stmt (use->stmt))); | |
8b11a64c | 3493 | |
ca4c3169 ZD |
3494 | niter = niter_for_single_dom_exit (data); |
3495 | if (!niter | |
3496 | || !zero_p (niter->may_be_zero)) | |
8b11a64c ZD |
3497 | return false; |
3498 | ||
3499 | *value = iv_value (use->iv, niter->niter); | |
3500 | ||
3501 | return true; | |
3502 | } | |
3503 | ||
3504 | /* Determines cost of replacing final value of USE using CAND. */ | |
3505 | ||
b1b02be2 | 3506 | static bool |
8b11a64c ZD |
3507 | determine_use_iv_cost_outer (struct ivopts_data *data, |
3508 | struct iv_use *use, struct iv_cand *cand) | |
3509 | { | |
3510 | bitmap depends_on; | |
3511 | unsigned cost; | |
3512 | edge exit; | |
3513 | tree value; | |
3514 | struct loop *loop = data->current_loop; | |
eec5fec9 ZD |
3515 | |
3516 | /* The simple case first -- if we need to express value of the preserved | |
3517 | original biv, the cost is 0. This also prevents us from counting the | |
3518 | cost of increment twice -- once at this use and once in the cost of | |
3519 | the candidate. */ | |
3520 | if (cand->pos == IP_ORIGINAL | |
3521 | && cand->incremented_at == use->stmt) | |
3522 | { | |
3523 | set_use_iv_cost (data, use, cand, 0, NULL); | |
3524 | return true; | |
3525 | } | |
3526 | ||
8b11a64c ZD |
3527 | if (!cand->iv) |
3528 | { | |
ca4c3169 | 3529 | if (!may_replace_final_value (data, use, &value)) |
8b11a64c ZD |
3530 | { |
3531 | set_use_iv_cost (data, use, cand, INFTY, NULL); | |
b1b02be2 | 3532 | return false; |
8b11a64c ZD |
3533 | } |
3534 | ||
3535 | depends_on = NULL; | |
3536 | cost = force_var_cost (data, value, &depends_on); | |
3537 | ||
3538 | cost /= AVG_LOOP_NITER (loop); | |
3539 | ||
3540 | set_use_iv_cost (data, use, cand, cost, depends_on); | |
b1b02be2 | 3541 | return cost != INFTY; |
8b11a64c ZD |
3542 | } |
3543 | ||
3544 | exit = single_dom_exit (loop); | |
3545 | if (exit) | |
3546 | { | |
3547 | /* If there is just a single exit, we may use value of the candidate | |
3548 | after we take it to determine the value of use. */ | |
3549 | cost = get_computation_cost_at (data, use, cand, false, &depends_on, | |
3550 | last_stmt (exit->src)); | |
3551 | if (cost != INFTY) | |
3552 | cost /= AVG_LOOP_NITER (loop); | |
3553 | } | |
3554 | else | |
3555 | { | |
3556 | /* Otherwise we just need to compute the iv. */ | |
3557 | cost = get_computation_cost (data, use, cand, false, &depends_on); | |
3558 | } | |
3559 | ||
3560 | set_use_iv_cost (data, use, cand, cost, depends_on); | |
b1b02be2 ZD |
3561 | |
3562 | return cost != INFTY; | |
8b11a64c ZD |
3563 | } |
3564 | ||
b1b02be2 ZD |
3565 | /* Determines cost of basing replacement of USE on CAND. Returns false |
3566 | if USE cannot be based on CAND. */ | |
8b11a64c | 3567 | |
b1b02be2 | 3568 | static bool |
8b11a64c ZD |
3569 | determine_use_iv_cost (struct ivopts_data *data, |
3570 | struct iv_use *use, struct iv_cand *cand) | |
3571 | { | |
3572 | switch (use->type) | |
3573 | { | |
3574 | case USE_NONLINEAR_EXPR: | |
b1b02be2 | 3575 | return determine_use_iv_cost_generic (data, use, cand); |
8b11a64c ZD |
3576 | |
3577 | case USE_OUTER: | |
b1b02be2 | 3578 | return determine_use_iv_cost_outer (data, use, cand); |
8b11a64c ZD |
3579 | |
3580 | case USE_ADDRESS: | |
b1b02be2 | 3581 | return determine_use_iv_cost_address (data, use, cand); |
8b11a64c ZD |
3582 | |
3583 | case USE_COMPARE: | |
b1b02be2 | 3584 | return determine_use_iv_cost_condition (data, use, cand); |
8b11a64c ZD |
3585 | |
3586 | default: | |
1e128c5f | 3587 | gcc_unreachable (); |
8b11a64c ZD |
3588 | } |
3589 | } | |
3590 | ||
3591 | /* Determines costs of basing the use of the iv on an iv candidate. */ | |
3592 | ||
3593 | static void | |
3594 | determine_use_iv_costs (struct ivopts_data *data) | |
3595 | { | |
3596 | unsigned i, j; | |
3597 | struct iv_use *use; | |
3598 | struct iv_cand *cand; | |
8bdbfff5 | 3599 | bitmap to_clear = BITMAP_ALLOC (NULL); |
8b11a64c ZD |
3600 | |
3601 | alloc_use_cost_map (data); | |
3602 | ||
8b11a64c ZD |
3603 | for (i = 0; i < n_iv_uses (data); i++) |
3604 | { | |
3605 | use = iv_use (data, i); | |
3606 | ||
3607 | if (data->consider_all_candidates) | |
3608 | { | |
3609 | for (j = 0; j < n_iv_cands (data); j++) | |
3610 | { | |
3611 | cand = iv_cand (data, j); | |
3612 | determine_use_iv_cost (data, use, cand); | |
3613 | } | |
3614 | } | |
3615 | else | |
3616 | { | |
87c476a2 ZD |
3617 | bitmap_iterator bi; |
3618 | ||
3619 | EXECUTE_IF_SET_IN_BITMAP (use->related_cands, 0, j, bi) | |
8b11a64c ZD |
3620 | { |
3621 | cand = iv_cand (data, j); | |
b1b02be2 ZD |
3622 | if (!determine_use_iv_cost (data, use, cand)) |
3623 | bitmap_set_bit (to_clear, j); | |
87c476a2 | 3624 | } |
b1b02be2 ZD |
3625 | |
3626 | /* Remove the candidates for that the cost is infinite from | |
3627 | the list of related candidates. */ | |
3628 | bitmap_and_compl_into (use->related_cands, to_clear); | |
3629 | bitmap_clear (to_clear); | |
8b11a64c ZD |
3630 | } |
3631 | } | |
3632 | ||
8bdbfff5 | 3633 | BITMAP_FREE (to_clear); |
b1b02be2 | 3634 | |
8b11a64c ZD |
3635 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3636 | { | |
3637 | fprintf (dump_file, "Use-candidate costs:\n"); | |
3638 | ||
3639 | for (i = 0; i < n_iv_uses (data); i++) | |
3640 | { | |
3641 | use = iv_use (data, i); | |
3642 | ||
3643 | fprintf (dump_file, "Use %d:\n", i); | |
3644 | fprintf (dump_file, " cand\tcost\tdepends on\n"); | |
3645 | for (j = 0; j < use->n_map_members; j++) | |
3646 | { | |
3647 | if (!use->cost_map[j].cand | |
3648 | || use->cost_map[j].cost == INFTY) | |
3649 | continue; | |
3650 | ||
3651 | fprintf (dump_file, " %d\t%d\t", | |
3652 | use->cost_map[j].cand->id, | |
3653 | use->cost_map[j].cost); | |
3654 | if (use->cost_map[j].depends_on) | |
3655 | bitmap_print (dump_file, | |
3656 | use->cost_map[j].depends_on, "",""); | |
3657 | fprintf (dump_file, "\n"); | |
3658 | } | |
3659 | ||
3660 | fprintf (dump_file, "\n"); | |
3661 | } | |
3662 | fprintf (dump_file, "\n"); | |
3663 | } | |
3664 | } | |
3665 | ||
3666 | /* Determines cost of the candidate CAND. */ | |
3667 | ||
3668 | static void | |
3669 | determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand) | |
3670 | { | |
3671 | unsigned cost_base, cost_step; | |
4366cf6d | 3672 | tree base; |
8b11a64c ZD |
3673 | |
3674 | if (!cand->iv) | |
3675 | { | |
3676 | cand->cost = 0; | |
3677 | return; | |
3678 | } | |
3679 | ||
3680 | /* There are two costs associated with the candidate -- its increment | |
3681 | and its initialization. The second is almost negligible for any loop | |
3682 | that rolls enough, so we take it just very little into account. */ | |
3683 | ||
3684 | base = cand->iv->base; | |
3685 | cost_base = force_var_cost (data, base, NULL); | |
3686 | cost_step = add_cost (TYPE_MODE (TREE_TYPE (base))); | |
3687 | ||
3688 | cand->cost = cost_step + cost_base / AVG_LOOP_NITER (current_loop); | |
3689 | ||
cf5d1802 ZD |
3690 | /* Prefer the original iv unless we may gain something by replacing it; |
3691 | this is not really relevant for artificial ivs created by other | |
3692 | passes. */ | |
3693 | if (cand->pos == IP_ORIGINAL | |
3694 | && !DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before))) | |
8b11a64c ZD |
3695 | cand->cost--; |
3696 | ||
3697 | /* Prefer not to insert statements into latch unless there are some | |
3698 | already (so that we do not create unnecessary jumps). */ | |
4366cf6d ZD |
3699 | if (cand->pos == IP_END |
3700 | && empty_block_p (ip_end_pos (data->current_loop))) | |
3701 | cand->cost++; | |
8b11a64c ZD |
3702 | } |
3703 | ||
3704 | /* Determines costs of computation of the candidates. */ | |
3705 | ||
3706 | static void | |
3707 | determine_iv_costs (struct ivopts_data *data) | |
3708 | { | |
3709 | unsigned i; | |
3710 | ||
3711 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3712 | { | |
3713 | fprintf (dump_file, "Candidate costs:\n"); | |
3714 | fprintf (dump_file, " cand\tcost\n"); | |
3715 | } | |
3716 | ||
3717 | for (i = 0; i < n_iv_cands (data); i++) | |
3718 | { | |
3719 | struct iv_cand *cand = iv_cand (data, i); | |
3720 | ||
3721 | determine_iv_cost (data, cand); | |
3722 | ||
3723 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3724 | fprintf (dump_file, " %d\t%d\n", i, cand->cost); | |
3725 | } | |
3726 | ||
3727 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3728 | fprintf (dump_file, "\n"); | |
3729 | } | |
3730 | ||
3731 | /* Calculates cost for having SIZE induction variables. */ | |
3732 | ||
3733 | static unsigned | |
3734 | ivopts_global_cost_for_size (struct ivopts_data *data, unsigned size) | |
3735 | { | |
3736 | return global_cost_for_size (size, | |
3737 | loop_data (data->current_loop)->regs_used, | |
3738 | n_iv_uses (data)); | |
3739 | } | |
3740 | ||
3741 | /* For each size of the induction variable set determine the penalty. */ | |
3742 | ||
3743 | static void | |
3744 | determine_set_costs (struct ivopts_data *data) | |
3745 | { | |
3746 | unsigned j, n; | |
3747 | tree phi, op; | |
3748 | struct loop *loop = data->current_loop; | |
87c476a2 | 3749 | bitmap_iterator bi; |
8b11a64c ZD |
3750 | |
3751 | /* We use the following model (definitely improvable, especially the | |
3752 | cost function -- TODO): | |
3753 | ||
3754 | We estimate the number of registers available (using MD data), name it A. | |
3755 | ||
3756 | We estimate the number of registers used by the loop, name it U. This | |
3757 | number is obtained as the number of loop phi nodes (not counting virtual | |
3758 | registers and bivs) + the number of variables from outside of the loop. | |
3759 | ||
3760 | We set a reserve R (free regs that are used for temporary computations, | |
3761 | etc.). For now the reserve is a constant 3. | |
3762 | ||
3763 | Let I be the number of induction variables. | |
3764 | ||
3765 | -- if U + I + R <= A, the cost is I * SMALL_COST (just not to encourage | |
3766 | make a lot of ivs without a reason). | |
3767 | -- if A - R < U + I <= A, the cost is I * PRES_COST | |
3768 | -- if U + I > A, the cost is I * PRES_COST and | |
3769 | number of uses * SPILL_COST * (U + I - A) / (U + I) is added. */ | |
3770 | ||
3771 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3772 | { | |
3773 | fprintf (dump_file, "Global costs:\n"); | |
3774 | fprintf (dump_file, " target_avail_regs %d\n", target_avail_regs); | |
3775 | fprintf (dump_file, " target_small_cost %d\n", target_small_cost); | |
3776 | fprintf (dump_file, " target_pres_cost %d\n", target_pres_cost); | |
3777 | fprintf (dump_file, " target_spill_cost %d\n", target_spill_cost); | |
3778 | } | |
3779 | ||
3780 | n = 0; | |
bb29d951 | 3781 | for (phi = phi_nodes (loop->header); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
3782 | { |
3783 | op = PHI_RESULT (phi); | |
3784 | ||
3785 | if (!is_gimple_reg (op)) | |
3786 | continue; | |
3787 | ||
3788 | if (get_iv (data, op)) | |
3789 | continue; | |
3790 | ||
3791 | n++; | |
3792 | } | |
3793 | ||
87c476a2 | 3794 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi) |
8b11a64c ZD |
3795 | { |
3796 | struct version_info *info = ver_info (data, j); | |
3797 | ||
3798 | if (info->inv_id && info->has_nonlin_use) | |
3799 | n++; | |
87c476a2 | 3800 | } |
8b11a64c ZD |
3801 | |
3802 | loop_data (loop)->regs_used = n; | |
3803 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3804 | fprintf (dump_file, " regs_used %d\n", n); | |
3805 | ||
3806 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
3807 | { | |
3808 | fprintf (dump_file, " cost for size:\n"); | |
3809 | fprintf (dump_file, " ivs\tcost\n"); | |
3810 | for (j = 0; j <= 2 * target_avail_regs; j++) | |
3811 | fprintf (dump_file, " %d\t%d\n", j, | |
3812 | ivopts_global_cost_for_size (data, j)); | |
3813 | fprintf (dump_file, "\n"); | |
3814 | } | |
3815 | } | |
3816 | ||
b1b02be2 | 3817 | /* Returns true if A is a cheaper cost pair than B. */ |
8b11a64c | 3818 | |
b1b02be2 ZD |
3819 | static bool |
3820 | cheaper_cost_pair (struct cost_pair *a, struct cost_pair *b) | |
8b11a64c | 3821 | { |
b1b02be2 ZD |
3822 | if (!a) |
3823 | return false; | |
8b11a64c | 3824 | |
b1b02be2 ZD |
3825 | if (!b) |
3826 | return true; | |
3827 | ||
3828 | if (a->cost < b->cost) | |
3829 | return true; | |
3830 | ||
3831 | if (a->cost > b->cost) | |
3832 | return false; | |
3833 | ||
3834 | /* In case the costs are the same, prefer the cheaper candidate. */ | |
3835 | if (a->cand->cost < b->cand->cost) | |
3836 | return true; | |
3837 | ||
3838 | return false; | |
3839 | } | |
3840 | ||
3841 | /* Computes the cost field of IVS structure. */ | |
3842 | ||
3843 | static void | |
3844 | iv_ca_recount_cost (struct ivopts_data *data, struct iv_ca *ivs) | |
3845 | { | |
3846 | unsigned cost = 0; | |
3847 | ||
3848 | cost += ivs->cand_use_cost; | |
3849 | cost += ivs->cand_cost; | |
3850 | cost += ivopts_global_cost_for_size (data, ivs->n_regs); | |
3851 | ||
3852 | ivs->cost = cost; | |
3853 | } | |
3854 | ||
3855 | /* Set USE not to be expressed by any candidate in IVS. */ | |
3856 | ||
3857 | static void | |
3858 | iv_ca_set_no_cp (struct ivopts_data *data, struct iv_ca *ivs, | |
3859 | struct iv_use *use) | |
3860 | { | |
3861 | unsigned uid = use->id, cid, iid; | |
3862 | bitmap deps; | |
3863 | struct cost_pair *cp; | |
3864 | bitmap_iterator bi; | |
3865 | ||
3866 | cp = ivs->cand_for_use[uid]; | |
3867 | if (!cp) | |
3868 | return; | |
3869 | cid = cp->cand->id; | |
3870 | ||
3871 | ivs->bad_uses++; | |
3872 | ivs->cand_for_use[uid] = NULL; | |
3873 | ivs->n_cand_uses[cid]--; | |
3874 | ||
3875 | if (ivs->n_cand_uses[cid] == 0) | |
8b11a64c | 3876 | { |
b1b02be2 ZD |
3877 | bitmap_clear_bit (ivs->cands, cid); |
3878 | /* Do not count the pseudocandidates. */ | |
3879 | if (cp->cand->iv) | |
3880 | ivs->n_regs--; | |
36f5ada1 | 3881 | ivs->n_cands--; |
b1b02be2 ZD |
3882 | ivs->cand_cost -= cp->cand->cost; |
3883 | } | |
3884 | ||
3885 | ivs->cand_use_cost -= cp->cost; | |
3886 | ||
3887 | deps = cp->depends_on; | |
80cad5fa | 3888 | |
b1b02be2 ZD |
3889 | if (deps) |
3890 | { | |
3891 | EXECUTE_IF_SET_IN_BITMAP (deps, 0, iid, bi) | |
3892 | { | |
3893 | ivs->n_invariant_uses[iid]--; | |
3894 | if (ivs->n_invariant_uses[iid] == 0) | |
3895 | ivs->n_regs--; | |
3896 | } | |
8b11a64c ZD |
3897 | } |
3898 | ||
b1b02be2 ZD |
3899 | iv_ca_recount_cost (data, ivs); |
3900 | } | |
3901 | ||
3902 | /* Set cost pair for USE in set IVS to CP. */ | |
3903 | ||
3904 | static void | |
3905 | iv_ca_set_cp (struct ivopts_data *data, struct iv_ca *ivs, | |
3906 | struct iv_use *use, struct cost_pair *cp) | |
3907 | { | |
3908 | unsigned uid = use->id, cid, iid; | |
3909 | bitmap deps; | |
3910 | bitmap_iterator bi; | |
3911 | ||
3912 | if (ivs->cand_for_use[uid] == cp) | |
3913 | return; | |
3914 | ||
3915 | if (ivs->cand_for_use[uid]) | |
3916 | iv_ca_set_no_cp (data, ivs, use); | |
3917 | ||
3918 | if (cp) | |
8b11a64c | 3919 | { |
b1b02be2 | 3920 | cid = cp->cand->id; |
8b11a64c | 3921 | |
b1b02be2 ZD |
3922 | ivs->bad_uses--; |
3923 | ivs->cand_for_use[uid] = cp; | |
3924 | ivs->n_cand_uses[cid]++; | |
3925 | if (ivs->n_cand_uses[cid] == 1) | |
8b11a64c | 3926 | { |
b1b02be2 ZD |
3927 | bitmap_set_bit (ivs->cands, cid); |
3928 | /* Do not count the pseudocandidates. */ | |
3929 | if (cp->cand->iv) | |
3930 | ivs->n_regs++; | |
36f5ada1 | 3931 | ivs->n_cands++; |
b1b02be2 | 3932 | ivs->cand_cost += cp->cand->cost; |
8b11a64c ZD |
3933 | } |
3934 | ||
b1b02be2 ZD |
3935 | ivs->cand_use_cost += cp->cost; |
3936 | ||
3937 | deps = cp->depends_on; | |
3938 | ||
3939 | if (deps) | |
8b11a64c | 3940 | { |
b1b02be2 | 3941 | EXECUTE_IF_SET_IN_BITMAP (deps, 0, iid, bi) |
87c476a2 | 3942 | { |
b1b02be2 ZD |
3943 | ivs->n_invariant_uses[iid]++; |
3944 | if (ivs->n_invariant_uses[iid] == 1) | |
3945 | ivs->n_regs++; | |
87c476a2 | 3946 | } |
8b11a64c ZD |
3947 | } |
3948 | ||
b1b02be2 | 3949 | iv_ca_recount_cost (data, ivs); |
87c476a2 | 3950 | } |
b1b02be2 ZD |
3951 | } |
3952 | ||
3953 | /* Extend set IVS by expressing USE by some of the candidates in it | |
3954 | if possible. */ | |
3955 | ||
3956 | static void | |
3957 | iv_ca_add_use (struct ivopts_data *data, struct iv_ca *ivs, | |
3958 | struct iv_use *use) | |
3959 | { | |
3960 | struct cost_pair *best_cp = NULL, *cp; | |
3961 | bitmap_iterator bi; | |
3962 | unsigned i; | |
8b11a64c | 3963 | |
b1b02be2 ZD |
3964 | gcc_assert (ivs->upto >= use->id); |
3965 | ||
3966 | if (ivs->upto == use->id) | |
3967 | { | |
3968 | ivs->upto++; | |
3969 | ivs->bad_uses++; | |
3970 | } | |
8b11a64c | 3971 | |
b1b02be2 ZD |
3972 | EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi) |
3973 | { | |
3974 | cp = get_use_iv_cost (data, use, iv_cand (data, i)); | |
8b11a64c | 3975 | |
b1b02be2 ZD |
3976 | if (cheaper_cost_pair (cp, best_cp)) |
3977 | best_cp = cp; | |
3978 | } | |
8b11a64c | 3979 | |
b1b02be2 | 3980 | iv_ca_set_cp (data, ivs, use, best_cp); |
8b11a64c ZD |
3981 | } |
3982 | ||
b1b02be2 | 3983 | /* Get cost for assignment IVS. */ |
8b11a64c ZD |
3984 | |
3985 | static unsigned | |
b1b02be2 ZD |
3986 | iv_ca_cost (struct iv_ca *ivs) |
3987 | { | |
3988 | return (ivs->bad_uses ? INFTY : ivs->cost); | |
3989 | } | |
3990 | ||
3991 | /* Returns true if all dependences of CP are among invariants in IVS. */ | |
3992 | ||
3993 | static bool | |
3994 | iv_ca_has_deps (struct iv_ca *ivs, struct cost_pair *cp) | |
8b11a64c ZD |
3995 | { |
3996 | unsigned i; | |
87c476a2 | 3997 | bitmap_iterator bi; |
8b11a64c | 3998 | |
b1b02be2 ZD |
3999 | if (!cp->depends_on) |
4000 | return true; | |
4001 | ||
4002 | EXECUTE_IF_SET_IN_BITMAP (cp->depends_on, 0, i, bi) | |
8b11a64c | 4003 | { |
b1b02be2 ZD |
4004 | if (ivs->n_invariant_uses[i] == 0) |
4005 | return false; | |
4006 | } | |
4007 | ||
4008 | return true; | |
4009 | } | |
4010 | ||
4011 | /* Creates change of expressing USE by NEW_CP instead of OLD_CP and chains | |
4012 | it before NEXT_CHANGE. */ | |
4013 | ||
4014 | static struct iv_ca_delta * | |
4015 | iv_ca_delta_add (struct iv_use *use, struct cost_pair *old_cp, | |
4016 | struct cost_pair *new_cp, struct iv_ca_delta *next_change) | |
4017 | { | |
4018 | struct iv_ca_delta *change = xmalloc (sizeof (struct iv_ca_delta)); | |
4019 | ||
4020 | change->use = use; | |
4021 | change->old_cp = old_cp; | |
4022 | change->new_cp = new_cp; | |
4023 | change->next_change = next_change; | |
4024 | ||
4025 | return change; | |
4026 | } | |
4027 | ||
36f5ada1 | 4028 | /* Joins two lists of changes L1 and L2. Destructive -- old lists |
6c6cfbfd | 4029 | are rewritten. */ |
36f5ada1 ZD |
4030 | |
4031 | static struct iv_ca_delta * | |
4032 | iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2) | |
4033 | { | |
4034 | struct iv_ca_delta *last; | |
4035 | ||
4036 | if (!l2) | |
4037 | return l1; | |
4038 | ||
4039 | if (!l1) | |
4040 | return l2; | |
4041 | ||
4042 | for (last = l1; last->next_change; last = last->next_change) | |
4043 | continue; | |
4044 | last->next_change = l2; | |
4045 | ||
4046 | return l1; | |
4047 | } | |
4048 | ||
b1b02be2 ZD |
4049 | /* Returns candidate by that USE is expressed in IVS. */ |
4050 | ||
4051 | static struct cost_pair * | |
4052 | iv_ca_cand_for_use (struct iv_ca *ivs, struct iv_use *use) | |
4053 | { | |
4054 | return ivs->cand_for_use[use->id]; | |
4055 | } | |
4056 | ||
36f5ada1 ZD |
4057 | /* Reverse the list of changes DELTA, forming the inverse to it. */ |
4058 | ||
4059 | static struct iv_ca_delta * | |
4060 | iv_ca_delta_reverse (struct iv_ca_delta *delta) | |
4061 | { | |
4062 | struct iv_ca_delta *act, *next, *prev = NULL; | |
4063 | struct cost_pair *tmp; | |
4064 | ||
4065 | for (act = delta; act; act = next) | |
4066 | { | |
4067 | next = act->next_change; | |
4068 | act->next_change = prev; | |
4069 | prev = act; | |
4070 | ||
4071 | tmp = act->old_cp; | |
4072 | act->old_cp = act->new_cp; | |
4073 | act->new_cp = tmp; | |
4074 | } | |
4075 | ||
4076 | return prev; | |
4077 | } | |
4078 | ||
b1b02be2 ZD |
4079 | /* Commit changes in DELTA to IVS. If FORWARD is false, the changes are |
4080 | reverted instead. */ | |
4081 | ||
4082 | static void | |
4083 | iv_ca_delta_commit (struct ivopts_data *data, struct iv_ca *ivs, | |
4084 | struct iv_ca_delta *delta, bool forward) | |
4085 | { | |
4086 | struct cost_pair *from, *to; | |
36f5ada1 | 4087 | struct iv_ca_delta *act; |
b1b02be2 | 4088 | |
36f5ada1 ZD |
4089 | if (!forward) |
4090 | delta = iv_ca_delta_reverse (delta); | |
b1b02be2 | 4091 | |
36f5ada1 ZD |
4092 | for (act = delta; act; act = act->next_change) |
4093 | { | |
4094 | from = act->old_cp; | |
4095 | to = act->new_cp; | |
4096 | gcc_assert (iv_ca_cand_for_use (ivs, act->use) == from); | |
4097 | iv_ca_set_cp (data, ivs, act->use, to); | |
8b11a64c | 4098 | } |
36f5ada1 ZD |
4099 | |
4100 | if (!forward) | |
4101 | iv_ca_delta_reverse (delta); | |
b1b02be2 | 4102 | } |
8b11a64c | 4103 | |
b1b02be2 | 4104 | /* Returns true if CAND is used in IVS. */ |
8b11a64c | 4105 | |
b1b02be2 ZD |
4106 | static bool |
4107 | iv_ca_cand_used_p (struct iv_ca *ivs, struct iv_cand *cand) | |
4108 | { | |
4109 | return ivs->n_cand_uses[cand->id] > 0; | |
4110 | } | |
8b11a64c | 4111 | |
36f5ada1 ZD |
4112 | /* Returns number of induction variable candidates in the set IVS. */ |
4113 | ||
4114 | static unsigned | |
4115 | iv_ca_n_cands (struct iv_ca *ivs) | |
4116 | { | |
4117 | return ivs->n_cands; | |
4118 | } | |
4119 | ||
b1b02be2 ZD |
4120 | /* Free the list of changes DELTA. */ |
4121 | ||
4122 | static void | |
4123 | iv_ca_delta_free (struct iv_ca_delta **delta) | |
4124 | { | |
4125 | struct iv_ca_delta *act, *next; | |
4126 | ||
4127 | for (act = *delta; act; act = next) | |
87c476a2 | 4128 | { |
b1b02be2 ZD |
4129 | next = act->next_change; |
4130 | free (act); | |
87c476a2 | 4131 | } |
8b11a64c | 4132 | |
b1b02be2 ZD |
4133 | *delta = NULL; |
4134 | } | |
4135 | ||
4136 | /* Allocates new iv candidates assignment. */ | |
4137 | ||
4138 | static struct iv_ca * | |
4139 | iv_ca_new (struct ivopts_data *data) | |
4140 | { | |
4141 | struct iv_ca *nw = xmalloc (sizeof (struct iv_ca)); | |
8b11a64c | 4142 | |
b1b02be2 ZD |
4143 | nw->upto = 0; |
4144 | nw->bad_uses = 0; | |
4145 | nw->cand_for_use = xcalloc (n_iv_uses (data), sizeof (struct cost_pair *)); | |
4146 | nw->n_cand_uses = xcalloc (n_iv_cands (data), sizeof (unsigned)); | |
8bdbfff5 | 4147 | nw->cands = BITMAP_ALLOC (NULL); |
36f5ada1 | 4148 | nw->n_cands = 0; |
b1b02be2 ZD |
4149 | nw->n_regs = 0; |
4150 | nw->cand_use_cost = 0; | |
4151 | nw->cand_cost = 0; | |
4152 | nw->n_invariant_uses = xcalloc (data->max_inv_id + 1, sizeof (unsigned)); | |
4153 | nw->cost = 0; | |
4154 | ||
4155 | return nw; | |
4156 | } | |
4157 | ||
4158 | /* Free memory occupied by the set IVS. */ | |
4159 | ||
4160 | static void | |
4161 | iv_ca_free (struct iv_ca **ivs) | |
4162 | { | |
4163 | free ((*ivs)->cand_for_use); | |
4164 | free ((*ivs)->n_cand_uses); | |
8bdbfff5 | 4165 | BITMAP_FREE ((*ivs)->cands); |
b1b02be2 ZD |
4166 | free ((*ivs)->n_invariant_uses); |
4167 | free (*ivs); | |
4168 | *ivs = NULL; | |
4169 | } | |
4170 | ||
4171 | /* Dumps IVS to FILE. */ | |
4172 | ||
4173 | static void | |
4174 | iv_ca_dump (struct ivopts_data *data, FILE *file, struct iv_ca *ivs) | |
4175 | { | |
4176 | const char *pref = " invariants "; | |
4177 | unsigned i; | |
4178 | ||
4179 | fprintf (file, " cost %d\n", iv_ca_cost (ivs)); | |
4180 | bitmap_print (file, ivs->cands, " candidates ","\n"); | |
4181 | ||
4182 | for (i = 1; i <= data->max_inv_id; i++) | |
4183 | if (ivs->n_invariant_uses[i]) | |
4184 | { | |
4185 | fprintf (file, "%s%d", pref, i); | |
4186 | pref = ", "; | |
4187 | } | |
4188 | fprintf (file, "\n"); | |
4189 | } | |
4190 | ||
4191 | /* Try changing candidate in IVS to CAND for each use. Return cost of the | |
36f5ada1 ZD |
4192 | new set, and store differences in DELTA. Number of induction variables |
4193 | in the new set is stored to N_IVS. */ | |
b1b02be2 ZD |
4194 | |
4195 | static unsigned | |
4196 | iv_ca_extend (struct ivopts_data *data, struct iv_ca *ivs, | |
36f5ada1 ZD |
4197 | struct iv_cand *cand, struct iv_ca_delta **delta, |
4198 | unsigned *n_ivs) | |
b1b02be2 ZD |
4199 | { |
4200 | unsigned i, cost; | |
4201 | struct iv_use *use; | |
4202 | struct cost_pair *old_cp, *new_cp; | |
4203 | ||
4204 | *delta = NULL; | |
4205 | for (i = 0; i < ivs->upto; i++) | |
4206 | { | |
4207 | use = iv_use (data, i); | |
4208 | old_cp = iv_ca_cand_for_use (ivs, use); | |
4209 | ||
4210 | if (old_cp | |
4211 | && old_cp->cand == cand) | |
4212 | continue; | |
4213 | ||
4214 | new_cp = get_use_iv_cost (data, use, cand); | |
4215 | if (!new_cp) | |
4216 | continue; | |
4217 | ||
4218 | if (!iv_ca_has_deps (ivs, new_cp)) | |
4219 | continue; | |
4220 | ||
4221 | if (!cheaper_cost_pair (new_cp, old_cp)) | |
4222 | continue; | |
4223 | ||
4224 | *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta); | |
4225 | } | |
4226 | ||
4227 | iv_ca_delta_commit (data, ivs, *delta, true); | |
4228 | cost = iv_ca_cost (ivs); | |
36f5ada1 ZD |
4229 | if (n_ivs) |
4230 | *n_ivs = iv_ca_n_cands (ivs); | |
b1b02be2 | 4231 | iv_ca_delta_commit (data, ivs, *delta, false); |
8b11a64c ZD |
4232 | |
4233 | return cost; | |
4234 | } | |
4235 | ||
a0eca485 | 4236 | /* Try narrowing set IVS by removing CAND. Return the cost of |
b1b02be2 | 4237 | the new set and store the differences in DELTA. */ |
8b11a64c ZD |
4238 | |
4239 | static unsigned | |
b1b02be2 ZD |
4240 | iv_ca_narrow (struct ivopts_data *data, struct iv_ca *ivs, |
4241 | struct iv_cand *cand, struct iv_ca_delta **delta) | |
8b11a64c | 4242 | { |
b1b02be2 ZD |
4243 | unsigned i, ci; |
4244 | struct iv_use *use; | |
4245 | struct cost_pair *old_cp, *new_cp, *cp; | |
4246 | bitmap_iterator bi; | |
4247 | struct iv_cand *cnd; | |
4248 | unsigned cost; | |
4249 | ||
4250 | *delta = NULL; | |
4251 | for (i = 0; i < n_iv_uses (data); i++) | |
4252 | { | |
4253 | use = iv_use (data, i); | |
4254 | ||
4255 | old_cp = iv_ca_cand_for_use (ivs, use); | |
4256 | if (old_cp->cand != cand) | |
4257 | continue; | |
4258 | ||
4259 | new_cp = NULL; | |
4260 | ||
4261 | if (data->consider_all_candidates) | |
4262 | { | |
4263 | EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi) | |
4264 | { | |
4265 | if (ci == cand->id) | |
4266 | continue; | |
4267 | ||
4268 | cnd = iv_cand (data, ci); | |
4269 | ||
4270 | cp = get_use_iv_cost (data, use, cnd); | |
4271 | if (!cp) | |
4272 | continue; | |
4273 | if (!iv_ca_has_deps (ivs, cp)) | |
4274 | continue; | |
4275 | ||
4276 | if (!cheaper_cost_pair (cp, new_cp)) | |
4277 | continue; | |
4278 | ||
4279 | new_cp = cp; | |
4280 | } | |
4281 | } | |
4282 | else | |
4283 | { | |
4284 | EXECUTE_IF_AND_IN_BITMAP (use->related_cands, ivs->cands, 0, ci, bi) | |
4285 | { | |
4286 | if (ci == cand->id) | |
4287 | continue; | |
4288 | ||
4289 | cnd = iv_cand (data, ci); | |
4290 | ||
4291 | cp = get_use_iv_cost (data, use, cnd); | |
4292 | if (!cp) | |
4293 | continue; | |
4294 | if (!iv_ca_has_deps (ivs, cp)) | |
4295 | continue; | |
4296 | ||
4297 | if (!cheaper_cost_pair (cp, new_cp)) | |
4298 | continue; | |
4299 | ||
4300 | new_cp = cp; | |
4301 | } | |
4302 | } | |
4303 | ||
4304 | if (!new_cp) | |
4305 | { | |
4306 | iv_ca_delta_free (delta); | |
4307 | return INFTY; | |
4308 | } | |
4309 | ||
4310 | *delta = iv_ca_delta_add (use, old_cp, new_cp, *delta); | |
4311 | } | |
4312 | ||
4313 | iv_ca_delta_commit (data, ivs, *delta, true); | |
4314 | cost = iv_ca_cost (ivs); | |
4315 | iv_ca_delta_commit (data, ivs, *delta, false); | |
4316 | ||
4317 | return cost; | |
8b11a64c ZD |
4318 | } |
4319 | ||
36f5ada1 ZD |
4320 | /* Try optimizing the set of candidates IVS by removing candidates different |
4321 | from to EXCEPT_CAND from it. Return cost of the new set, and store | |
4322 | differences in DELTA. */ | |
4323 | ||
4324 | static unsigned | |
4325 | iv_ca_prune (struct ivopts_data *data, struct iv_ca *ivs, | |
4326 | struct iv_cand *except_cand, struct iv_ca_delta **delta) | |
4327 | { | |
4328 | bitmap_iterator bi; | |
4329 | struct iv_ca_delta *act_delta, *best_delta; | |
4330 | unsigned i, best_cost, acost; | |
4331 | struct iv_cand *cand; | |
4332 | ||
4333 | best_delta = NULL; | |
4334 | best_cost = iv_ca_cost (ivs); | |
4335 | ||
4336 | EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi) | |
4337 | { | |
4338 | cand = iv_cand (data, i); | |
4339 | ||
4340 | if (cand == except_cand) | |
4341 | continue; | |
4342 | ||
4343 | acost = iv_ca_narrow (data, ivs, cand, &act_delta); | |
4344 | ||
4345 | if (acost < best_cost) | |
4346 | { | |
4347 | best_cost = acost; | |
4348 | iv_ca_delta_free (&best_delta); | |
4349 | best_delta = act_delta; | |
4350 | } | |
4351 | else | |
4352 | iv_ca_delta_free (&act_delta); | |
4353 | } | |
4354 | ||
4355 | if (!best_delta) | |
4356 | { | |
4357 | *delta = NULL; | |
4358 | return best_cost; | |
4359 | } | |
4360 | ||
4361 | /* Recurse to possibly remove other unnecessary ivs. */ | |
4362 | iv_ca_delta_commit (data, ivs, best_delta, true); | |
4363 | best_cost = iv_ca_prune (data, ivs, except_cand, delta); | |
4364 | iv_ca_delta_commit (data, ivs, best_delta, false); | |
4365 | *delta = iv_ca_delta_join (best_delta, *delta); | |
4366 | return best_cost; | |
4367 | } | |
4368 | ||
b1b02be2 | 4369 | /* Tries to extend the sets IVS in the best possible way in order |
8b11a64c ZD |
4370 | to express the USE. */ |
4371 | ||
4372 | static bool | |
b1b02be2 | 4373 | try_add_cand_for (struct ivopts_data *data, struct iv_ca *ivs, |
8b11a64c ZD |
4374 | struct iv_use *use) |
4375 | { | |
b1b02be2 | 4376 | unsigned best_cost, act_cost; |
8b11a64c | 4377 | unsigned i; |
38b0dcb8 ZD |
4378 | bitmap_iterator bi; |
4379 | struct iv_cand *cand; | |
b1b02be2 ZD |
4380 | struct iv_ca_delta *best_delta = NULL, *act_delta; |
4381 | struct cost_pair *cp; | |
4382 | ||
4383 | iv_ca_add_use (data, ivs, use); | |
4384 | best_cost = iv_ca_cost (ivs); | |
8b11a64c | 4385 | |
b1b02be2 ZD |
4386 | cp = iv_ca_cand_for_use (ivs, use); |
4387 | if (cp) | |
4388 | { | |
4389 | best_delta = iv_ca_delta_add (use, NULL, cp, NULL); | |
4390 | iv_ca_set_no_cp (data, ivs, use); | |
4391 | } | |
8b11a64c | 4392 | |
38b0dcb8 ZD |
4393 | /* First try important candidates. Only if it fails, try the specific ones. |
4394 | Rationale -- in loops with many variables the best choice often is to use | |
4395 | just one generic biv. If we added here many ivs specific to the uses, | |
4396 | the optimization algorithm later would be likely to get stuck in a local | |
4397 | minimum, thus causing us to create too many ivs. The approach from | |
1c170b5e | 4398 | few ivs to more seems more likely to be successful -- starting from few |
38b0dcb8 ZD |
4399 | ivs, replacing an expensive use by a specific iv should always be a |
4400 | win. */ | |
4401 | EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi) | |
8b11a64c | 4402 | { |
38b0dcb8 ZD |
4403 | cand = iv_cand (data, i); |
4404 | ||
b1b02be2 | 4405 | if (iv_ca_cand_used_p (ivs, cand)) |
8b11a64c ZD |
4406 | continue; |
4407 | ||
b1b02be2 ZD |
4408 | cp = get_use_iv_cost (data, use, cand); |
4409 | if (!cp) | |
4410 | continue; | |
4411 | ||
4412 | iv_ca_set_cp (data, ivs, use, cp); | |
36f5ada1 | 4413 | act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL); |
b1b02be2 ZD |
4414 | iv_ca_set_no_cp (data, ivs, use); |
4415 | act_delta = iv_ca_delta_add (use, NULL, cp, act_delta); | |
8b11a64c ZD |
4416 | |
4417 | if (act_cost < best_cost) | |
4418 | { | |
4419 | best_cost = act_cost; | |
b1b02be2 ZD |
4420 | |
4421 | iv_ca_delta_free (&best_delta); | |
4422 | best_delta = act_delta; | |
8b11a64c | 4423 | } |
b1b02be2 ZD |
4424 | else |
4425 | iv_ca_delta_free (&act_delta); | |
8b11a64c ZD |
4426 | } |
4427 | ||
38b0dcb8 ZD |
4428 | if (best_cost == INFTY) |
4429 | { | |
4430 | for (i = 0; i < use->n_map_members; i++) | |
4431 | { | |
4432 | cp = use->cost_map + i; | |
b1b02be2 ZD |
4433 | cand = cp->cand; |
4434 | if (!cand) | |
38b0dcb8 ZD |
4435 | continue; |
4436 | ||
4437 | /* Already tried this. */ | |
b1b02be2 ZD |
4438 | if (cand->important) |
4439 | continue; | |
4440 | ||
4441 | if (iv_ca_cand_used_p (ivs, cand)) | |
38b0dcb8 ZD |
4442 | continue; |
4443 | ||
b1b02be2 ZD |
4444 | act_delta = NULL; |
4445 | iv_ca_set_cp (data, ivs, use, cp); | |
36f5ada1 | 4446 | act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL); |
b1b02be2 ZD |
4447 | iv_ca_set_no_cp (data, ivs, use); |
4448 | act_delta = iv_ca_delta_add (use, iv_ca_cand_for_use (ivs, use), | |
4449 | cp, act_delta); | |
38b0dcb8 ZD |
4450 | |
4451 | if (act_cost < best_cost) | |
4452 | { | |
4453 | best_cost = act_cost; | |
b1b02be2 ZD |
4454 | |
4455 | if (best_delta) | |
4456 | iv_ca_delta_free (&best_delta); | |
4457 | best_delta = act_delta; | |
38b0dcb8 | 4458 | } |
b1b02be2 ZD |
4459 | else |
4460 | iv_ca_delta_free (&act_delta); | |
38b0dcb8 ZD |
4461 | } |
4462 | } | |
4463 | ||
b1b02be2 ZD |
4464 | iv_ca_delta_commit (data, ivs, best_delta, true); |
4465 | iv_ca_delta_free (&best_delta); | |
8b11a64c ZD |
4466 | |
4467 | return (best_cost != INFTY); | |
4468 | } | |
4469 | ||
b1b02be2 | 4470 | /* Finds an initial assignment of candidates to uses. */ |
8b11a64c | 4471 | |
b1b02be2 ZD |
4472 | static struct iv_ca * |
4473 | get_initial_solution (struct ivopts_data *data) | |
8b11a64c | 4474 | { |
b1b02be2 | 4475 | struct iv_ca *ivs = iv_ca_new (data); |
8b11a64c ZD |
4476 | unsigned i; |
4477 | ||
4478 | for (i = 0; i < n_iv_uses (data); i++) | |
b1b02be2 ZD |
4479 | if (!try_add_cand_for (data, ivs, iv_use (data, i))) |
4480 | { | |
4481 | iv_ca_free (&ivs); | |
4482 | return NULL; | |
4483 | } | |
8b11a64c | 4484 | |
b1b02be2 | 4485 | return ivs; |
8b11a64c ZD |
4486 | } |
4487 | ||
b1b02be2 | 4488 | /* Tries to improve set of induction variables IVS. */ |
8b11a64c ZD |
4489 | |
4490 | static bool | |
b1b02be2 | 4491 | try_improve_iv_set (struct ivopts_data *data, struct iv_ca *ivs) |
8b11a64c | 4492 | { |
36f5ada1 ZD |
4493 | unsigned i, acost, best_cost = iv_ca_cost (ivs), n_ivs; |
4494 | struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta; | |
b1b02be2 | 4495 | struct iv_cand *cand; |
8b11a64c | 4496 | |
36f5ada1 | 4497 | /* Try extending the set of induction variables by one. */ |
8b11a64c ZD |
4498 | for (i = 0; i < n_iv_cands (data); i++) |
4499 | { | |
b1b02be2 ZD |
4500 | cand = iv_cand (data, i); |
4501 | ||
4502 | if (iv_ca_cand_used_p (ivs, cand)) | |
36f5ada1 ZD |
4503 | continue; |
4504 | ||
4505 | acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs); | |
4506 | if (!act_delta) | |
4507 | continue; | |
4508 | ||
4509 | /* If we successfully added the candidate and the set is small enough, | |
4510 | try optimizing it by removing other candidates. */ | |
4511 | if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND) | |
4512 | { | |
4513 | iv_ca_delta_commit (data, ivs, act_delta, true); | |
4514 | acost = iv_ca_prune (data, ivs, cand, &tmp_delta); | |
4515 | iv_ca_delta_commit (data, ivs, act_delta, false); | |
4516 | act_delta = iv_ca_delta_join (act_delta, tmp_delta); | |
4517 | } | |
8b11a64c | 4518 | |
b1b02be2 | 4519 | if (acost < best_cost) |
8b11a64c | 4520 | { |
b1b02be2 | 4521 | best_cost = acost; |
36f5ada1 | 4522 | iv_ca_delta_free (&best_delta); |
b1b02be2 | 4523 | best_delta = act_delta; |
8b11a64c | 4524 | } |
8b11a64c | 4525 | else |
b1b02be2 | 4526 | iv_ca_delta_free (&act_delta); |
8b11a64c ZD |
4527 | } |
4528 | ||
b1b02be2 | 4529 | if (!best_delta) |
36f5ada1 ZD |
4530 | { |
4531 | /* Try removing the candidates from the set instead. */ | |
4532 | best_cost = iv_ca_prune (data, ivs, NULL, &best_delta); | |
4533 | ||
4534 | /* Nothing more we can do. */ | |
4535 | if (!best_delta) | |
4536 | return false; | |
4537 | } | |
8b11a64c | 4538 | |
b1b02be2 | 4539 | iv_ca_delta_commit (data, ivs, best_delta, true); |
36f5ada1 | 4540 | gcc_assert (best_cost == iv_ca_cost (ivs)); |
b1b02be2 | 4541 | iv_ca_delta_free (&best_delta); |
8b11a64c ZD |
4542 | return true; |
4543 | } | |
4544 | ||
4545 | /* Attempts to find the optimal set of induction variables. We do simple | |
4546 | greedy heuristic -- we try to replace at most one candidate in the selected | |
4547 | solution and remove the unused ivs while this improves the cost. */ | |
4548 | ||
b1b02be2 | 4549 | static struct iv_ca * |
8b11a64c ZD |
4550 | find_optimal_iv_set (struct ivopts_data *data) |
4551 | { | |
b1b02be2 ZD |
4552 | unsigned i; |
4553 | struct iv_ca *set; | |
8b11a64c ZD |
4554 | struct iv_use *use; |
4555 | ||
b1b02be2 ZD |
4556 | /* Get the initial solution. */ |
4557 | set = get_initial_solution (data); | |
4558 | if (!set) | |
8b11a64c ZD |
4559 | { |
4560 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4561 | fprintf (dump_file, "Unable to substitute for ivs, failed.\n"); | |
8b11a64c ZD |
4562 | return NULL; |
4563 | } | |
4564 | ||
4565 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4566 | { | |
b1b02be2 ZD |
4567 | fprintf (dump_file, "Initial set of candidates:\n"); |
4568 | iv_ca_dump (data, dump_file, set); | |
8b11a64c ZD |
4569 | } |
4570 | ||
b1b02be2 | 4571 | while (try_improve_iv_set (data, set)) |
8b11a64c ZD |
4572 | { |
4573 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
4574 | { | |
b1b02be2 ZD |
4575 | fprintf (dump_file, "Improved to:\n"); |
4576 | iv_ca_dump (data, dump_file, set); | |
8b11a64c ZD |
4577 | } |
4578 | } | |
4579 | ||
4580 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
b1b02be2 | 4581 | fprintf (dump_file, "Final cost %d\n\n", iv_ca_cost (set)); |
8b11a64c ZD |
4582 | |
4583 | for (i = 0; i < n_iv_uses (data); i++) | |
4584 | { | |
4585 | use = iv_use (data, i); | |
b1b02be2 | 4586 | use->selected = iv_ca_cand_for_use (set, use)->cand; |
8b11a64c ZD |
4587 | } |
4588 | ||
8b11a64c ZD |
4589 | return set; |
4590 | } | |
4591 | ||
4592 | /* Creates a new induction variable corresponding to CAND. */ | |
4593 | ||
4594 | static void | |
4595 | create_new_iv (struct ivopts_data *data, struct iv_cand *cand) | |
4596 | { | |
4597 | block_stmt_iterator incr_pos; | |
4598 | tree base; | |
4599 | bool after = false; | |
4600 | ||
4601 | if (!cand->iv) | |
4602 | return; | |
4603 | ||
4604 | switch (cand->pos) | |
4605 | { | |
4606 | case IP_NORMAL: | |
4607 | incr_pos = bsi_last (ip_normal_pos (data->current_loop)); | |
4608 | break; | |
4609 | ||
4610 | case IP_END: | |
4611 | incr_pos = bsi_last (ip_end_pos (data->current_loop)); | |
4612 | after = true; | |
4613 | break; | |
4614 | ||
4615 | case IP_ORIGINAL: | |
4616 | /* Mark that the iv is preserved. */ | |
4617 | name_info (data, cand->var_before)->preserve_biv = true; | |
4618 | name_info (data, cand->var_after)->preserve_biv = true; | |
4619 | ||
4620 | /* Rewrite the increment so that it uses var_before directly. */ | |
4621 | find_interesting_uses_op (data, cand->var_after)->selected = cand; | |
4622 | ||
4623 | return; | |
4624 | } | |
4625 | ||
4626 | gimple_add_tmp_var (cand->var_before); | |
4627 | add_referenced_tmp_var (cand->var_before); | |
4628 | ||
4629 | base = unshare_expr (cand->iv->base); | |
4630 | ||
4631 | create_iv (base, cand->iv->step, cand->var_before, data->current_loop, | |
4632 | &incr_pos, after, &cand->var_before, &cand->var_after); | |
4633 | } | |
4634 | ||
4635 | /* Creates new induction variables described in SET. */ | |
4636 | ||
4637 | static void | |
b1b02be2 | 4638 | create_new_ivs (struct ivopts_data *data, struct iv_ca *set) |
8b11a64c ZD |
4639 | { |
4640 | unsigned i; | |
4641 | struct iv_cand *cand; | |
87c476a2 | 4642 | bitmap_iterator bi; |
8b11a64c | 4643 | |
b1b02be2 | 4644 | EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi) |
8b11a64c ZD |
4645 | { |
4646 | cand = iv_cand (data, i); | |
4647 | create_new_iv (data, cand); | |
87c476a2 | 4648 | } |
8b11a64c ZD |
4649 | } |
4650 | ||
4651 | /* Removes statement STMT (real or a phi node). If INCLUDING_DEFINED_NAME | |
4652 | is true, remove also the ssa name defined by the statement. */ | |
4653 | ||
4654 | static void | |
4655 | remove_statement (tree stmt, bool including_defined_name) | |
4656 | { | |
4657 | if (TREE_CODE (stmt) == PHI_NODE) | |
4658 | { | |
4659 | if (!including_defined_name) | |
4660 | { | |
4661 | /* Prevent the ssa name defined by the statement from being removed. */ | |
4662 | SET_PHI_RESULT (stmt, NULL); | |
4663 | } | |
d19e3ef6 | 4664 | remove_phi_node (stmt, NULL_TREE); |
8b11a64c ZD |
4665 | } |
4666 | else | |
4667 | { | |
1a1804c2 | 4668 | block_stmt_iterator bsi = bsi_for_stmt (stmt); |
8b11a64c ZD |
4669 | |
4670 | bsi_remove (&bsi); | |
4671 | } | |
4672 | } | |
4673 | ||
4674 | /* Rewrites USE (definition of iv used in a nonlinear expression) | |
4675 | using candidate CAND. */ | |
4676 | ||
4677 | static void | |
4678 | rewrite_use_nonlinear_expr (struct ivopts_data *data, | |
4679 | struct iv_use *use, struct iv_cand *cand) | |
4680 | { | |
3520b745 | 4681 | tree comp; |
8b11a64c ZD |
4682 | tree op, stmts, tgt, ass; |
4683 | block_stmt_iterator bsi, pbsi; | |
3520b745 ZD |
4684 | |
4685 | /* An important special case -- if we are asked to express value of | |
4686 | the original iv by itself, just exit; there is no need to | |
4687 | introduce a new computation (that might also need casting the | |
4688 | variable to unsigned and back). */ | |
4689 | if (cand->pos == IP_ORIGINAL | |
4690 | && TREE_CODE (use->stmt) == MODIFY_EXPR | |
4691 | && TREE_OPERAND (use->stmt, 0) == cand->var_after) | |
4692 | { | |
4693 | op = TREE_OPERAND (use->stmt, 1); | |
4694 | ||
4695 | /* Be a bit careful. In case variable is expressed in some | |
4696 | complicated way, rewrite it so that we may get rid of this | |
4697 | complicated expression. */ | |
4698 | if ((TREE_CODE (op) == PLUS_EXPR | |
4699 | || TREE_CODE (op) == MINUS_EXPR) | |
4700 | && TREE_OPERAND (op, 0) == cand->var_before | |
4701 | && TREE_CODE (TREE_OPERAND (op, 1)) == INTEGER_CST) | |
4702 | return; | |
4703 | } | |
4704 | ||
4705 | comp = unshare_expr (get_computation (data->current_loop, | |
4706 | use, cand)); | |
1e128c5f | 4707 | switch (TREE_CODE (use->stmt)) |
8b11a64c | 4708 | { |
1e128c5f | 4709 | case PHI_NODE: |
8b11a64c ZD |
4710 | tgt = PHI_RESULT (use->stmt); |
4711 | ||
4712 | /* If we should keep the biv, do not replace it. */ | |
4713 | if (name_info (data, tgt)->preserve_biv) | |
4714 | return; | |
4715 | ||
4716 | pbsi = bsi = bsi_start (bb_for_stmt (use->stmt)); | |
4717 | while (!bsi_end_p (pbsi) | |
4718 | && TREE_CODE (bsi_stmt (pbsi)) == LABEL_EXPR) | |
4719 | { | |
4720 | bsi = pbsi; | |
4721 | bsi_next (&pbsi); | |
4722 | } | |
1e128c5f GB |
4723 | break; |
4724 | ||
4725 | case MODIFY_EXPR: | |
8b11a64c | 4726 | tgt = TREE_OPERAND (use->stmt, 0); |
1a1804c2 | 4727 | bsi = bsi_for_stmt (use->stmt); |
1e128c5f GB |
4728 | break; |
4729 | ||
4730 | default: | |
4731 | gcc_unreachable (); | |
8b11a64c | 4732 | } |
8b11a64c ZD |
4733 | |
4734 | op = force_gimple_operand (comp, &stmts, false, SSA_NAME_VAR (tgt)); | |
4735 | ||
4736 | if (TREE_CODE (use->stmt) == PHI_NODE) | |
4737 | { | |
4738 | if (stmts) | |
4739 | bsi_insert_after (&bsi, stmts, BSI_CONTINUE_LINKING); | |
4740 | ass = build2 (MODIFY_EXPR, TREE_TYPE (tgt), tgt, op); | |
4741 | bsi_insert_after (&bsi, ass, BSI_NEW_STMT); | |
4742 | remove_statement (use->stmt, false); | |
4743 | SSA_NAME_DEF_STMT (tgt) = ass; | |
4744 | } | |
4745 | else | |
4746 | { | |
4747 | if (stmts) | |
4748 | bsi_insert_before (&bsi, stmts, BSI_SAME_STMT); | |
4749 | TREE_OPERAND (use->stmt, 1) = op; | |
4750 | } | |
4751 | } | |
4752 | ||
4753 | /* Replaces ssa name in index IDX by its basic variable. Callback for | |
4754 | for_each_index. */ | |
4755 | ||
4756 | static bool | |
be35cf60 | 4757 | idx_remove_ssa_names (tree base, tree *idx, |
8b11a64c ZD |
4758 | void *data ATTRIBUTE_UNUSED) |
4759 | { | |
be35cf60 ZD |
4760 | tree *op; |
4761 | ||
8b11a64c ZD |
4762 | if (TREE_CODE (*idx) == SSA_NAME) |
4763 | *idx = SSA_NAME_VAR (*idx); | |
be35cf60 ZD |
4764 | |
4765 | if (TREE_CODE (base) == ARRAY_REF) | |
4766 | { | |
4767 | op = &TREE_OPERAND (base, 2); | |
4768 | if (*op | |
4769 | && TREE_CODE (*op) == SSA_NAME) | |
4770 | *op = SSA_NAME_VAR (*op); | |
4771 | op = &TREE_OPERAND (base, 3); | |
4772 | if (*op | |
4773 | && TREE_CODE (*op) == SSA_NAME) | |
4774 | *op = SSA_NAME_VAR (*op); | |
4775 | } | |
4776 | ||
8b11a64c ZD |
4777 | return true; |
4778 | } | |
4779 | ||
4780 | /* Unshares REF and replaces ssa names inside it by their basic variables. */ | |
4781 | ||
4782 | static tree | |
4783 | unshare_and_remove_ssa_names (tree ref) | |
4784 | { | |
4785 | ref = unshare_expr (ref); | |
4786 | for_each_index (&ref, idx_remove_ssa_names, NULL); | |
4787 | ||
4788 | return ref; | |
4789 | } | |
4790 | ||
4791 | /* Rewrites base of memory access OP with expression WITH in statement | |
4792 | pointed to by BSI. */ | |
4793 | ||
4794 | static void | |
4795 | rewrite_address_base (block_stmt_iterator *bsi, tree *op, tree with) | |
4796 | { | |
7ccf35ed | 4797 | tree bvar, var, new_var, new_name, copy, name; |
8b11a64c ZD |
4798 | tree orig; |
4799 | ||
7ccf35ed DN |
4800 | var = bvar = get_base_address (*op); |
4801 | ||
8b11a64c ZD |
4802 | if (!var || TREE_CODE (with) != SSA_NAME) |
4803 | goto do_rewrite; | |
be35cf60 ZD |
4804 | |
4805 | gcc_assert (TREE_CODE (var) != ALIGN_INDIRECT_REF); | |
4806 | gcc_assert (TREE_CODE (var) != MISALIGNED_INDIRECT_REF); | |
4807 | if (TREE_CODE (var) == INDIRECT_REF) | |
8b11a64c ZD |
4808 | var = TREE_OPERAND (var, 0); |
4809 | if (TREE_CODE (var) == SSA_NAME) | |
4810 | { | |
4811 | name = var; | |
4812 | var = SSA_NAME_VAR (var); | |
4813 | } | |
4814 | else if (DECL_P (var)) | |
4815 | name = NULL_TREE; | |
4816 | else | |
4817 | goto do_rewrite; | |
4818 | ||
4819 | if (var_ann (var)->type_mem_tag) | |
4820 | var = var_ann (var)->type_mem_tag; | |
4821 | ||
4822 | /* We need to add a memory tag for the variable. But we do not want | |
4823 | to add it to the temporary used for the computations, since this leads | |
4824 | to problems in redundancy elimination when there are common parts | |
4825 | in two computations referring to the different arrays. So we copy | |
4826 | the variable to a new temporary. */ | |
4827 | copy = build2 (MODIFY_EXPR, void_type_node, NULL_TREE, with); | |
4828 | if (name) | |
4829 | new_name = duplicate_ssa_name (name, copy); | |
4830 | else | |
4831 | { | |
4832 | new_var = create_tmp_var (TREE_TYPE (with), "ruatmp"); | |
4833 | add_referenced_tmp_var (new_var); | |
4834 | var_ann (new_var)->type_mem_tag = var; | |
4835 | new_name = make_ssa_name (new_var, copy); | |
4836 | } | |
4837 | TREE_OPERAND (copy, 0) = new_name; | |
4838 | bsi_insert_before (bsi, copy, BSI_SAME_STMT); | |
4839 | with = new_name; | |
4840 | ||
4841 | do_rewrite: | |
4842 | ||
4843 | orig = NULL_TREE; | |
be35cf60 ZD |
4844 | gcc_assert (TREE_CODE (*op) != ALIGN_INDIRECT_REF); |
4845 | gcc_assert (TREE_CODE (*op) != MISALIGNED_INDIRECT_REF); | |
4846 | ||
4847 | if (TREE_CODE (*op) == INDIRECT_REF) | |
8b11a64c ZD |
4848 | orig = REF_ORIGINAL (*op); |
4849 | if (!orig) | |
4850 | orig = unshare_and_remove_ssa_names (*op); | |
4851 | ||
be35cf60 | 4852 | *op = build1 (INDIRECT_REF, TREE_TYPE (*op), with); |
7ccf35ed | 4853 | |
8b11a64c ZD |
4854 | /* Record the original reference, for purposes of alias analysis. */ |
4855 | REF_ORIGINAL (*op) = orig; | |
4856 | } | |
4857 | ||
4858 | /* Rewrites USE (address that is an iv) using candidate CAND. */ | |
4859 | ||
4860 | static void | |
4861 | rewrite_use_address (struct ivopts_data *data, | |
4862 | struct iv_use *use, struct iv_cand *cand) | |
4863 | { | |
4864 | tree comp = unshare_expr (get_computation (data->current_loop, | |
4865 | use, cand)); | |
1a1804c2 | 4866 | block_stmt_iterator bsi = bsi_for_stmt (use->stmt); |
8b11a64c ZD |
4867 | tree stmts; |
4868 | tree op = force_gimple_operand (comp, &stmts, true, NULL_TREE); | |
4869 | ||
4870 | if (stmts) | |
4871 | bsi_insert_before (&bsi, stmts, BSI_SAME_STMT); | |
4872 | ||
4873 | rewrite_address_base (&bsi, use->op_p, op); | |
4874 | } | |
4875 | ||
4876 | /* Rewrites USE (the condition such that one of the arguments is an iv) using | |
4877 | candidate CAND. */ | |
4878 | ||
4879 | static void | |
4880 | rewrite_use_compare (struct ivopts_data *data, | |
4881 | struct iv_use *use, struct iv_cand *cand) | |
4882 | { | |
4883 | tree comp; | |
4884 | tree *op_p, cond, op, stmts, bound; | |
1a1804c2 | 4885 | block_stmt_iterator bsi = bsi_for_stmt (use->stmt); |
8b11a64c ZD |
4886 | enum tree_code compare; |
4887 | ||
ca4c3169 | 4888 | if (may_eliminate_iv (data, use, cand, &compare, &bound)) |
8b11a64c | 4889 | { |
9e7376e5 ZD |
4890 | tree var = var_at_stmt (data->current_loop, cand, use->stmt); |
4891 | tree var_type = TREE_TYPE (var); | |
4892 | ||
4893 | bound = fold_convert (var_type, bound); | |
8b11a64c ZD |
4894 | op = force_gimple_operand (unshare_expr (bound), &stmts, |
4895 | true, NULL_TREE); | |
4896 | ||
4897 | if (stmts) | |
4898 | bsi_insert_before (&bsi, stmts, BSI_SAME_STMT); | |
4899 | ||
9e7376e5 | 4900 | *use->op_p = build2 (compare, boolean_type_node, var, op); |
8b11a64c ZD |
4901 | modify_stmt (use->stmt); |
4902 | return; | |
4903 | } | |
4904 | ||
4905 | /* The induction variable elimination failed; just express the original | |
4906 | giv. */ | |
4907 | comp = unshare_expr (get_computation (data->current_loop, use, cand)); | |
4908 | ||
4909 | cond = *use->op_p; | |
4910 | op_p = &TREE_OPERAND (cond, 0); | |
4911 | if (TREE_CODE (*op_p) != SSA_NAME | |
4912 | || zero_p (get_iv (data, *op_p)->step)) | |
4913 | op_p = &TREE_OPERAND (cond, 1); | |
4914 | ||
4915 | op = force_gimple_operand (comp, &stmts, true, SSA_NAME_VAR (*op_p)); | |
4916 | if (stmts) | |
4917 | bsi_insert_before (&bsi, stmts, BSI_SAME_STMT); | |
4918 | ||
4919 | *op_p = op; | |
4920 | } | |
4921 | ||
4922 | /* Ensure that operand *OP_P may be used at the end of EXIT without | |
4923 | violating loop closed ssa form. */ | |
4924 | ||
4925 | static void | |
4926 | protect_loop_closed_ssa_form_use (edge exit, use_operand_p op_p) | |
4927 | { | |
4928 | basic_block def_bb; | |
4929 | struct loop *def_loop; | |
4930 | tree phi, use; | |
4931 | ||
4932 | use = USE_FROM_PTR (op_p); | |
4933 | if (TREE_CODE (use) != SSA_NAME) | |
4934 | return; | |
4935 | ||
4936 | def_bb = bb_for_stmt (SSA_NAME_DEF_STMT (use)); | |
4937 | if (!def_bb) | |
4938 | return; | |
4939 | ||
4940 | def_loop = def_bb->loop_father; | |
4941 | if (flow_bb_inside_loop_p (def_loop, exit->dest)) | |
4942 | return; | |
4943 | ||
4944 | /* Try finding a phi node that copies the value out of the loop. */ | |
bb29d951 | 4945 | for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
4946 | if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == use) |
4947 | break; | |
4948 | ||
4949 | if (!phi) | |
4950 | { | |
4951 | /* Create such a phi node. */ | |
4952 | tree new_name = duplicate_ssa_name (use, NULL); | |
4953 | ||
4954 | phi = create_phi_node (new_name, exit->dest); | |
4955 | SSA_NAME_DEF_STMT (new_name) = phi; | |
d2e398df | 4956 | add_phi_arg (phi, use, exit); |
8b11a64c ZD |
4957 | } |
4958 | ||
4959 | SET_USE (op_p, PHI_RESULT (phi)); | |
4960 | } | |
4961 | ||
4962 | /* Ensure that operands of STMT may be used at the end of EXIT without | |
4963 | violating loop closed ssa form. */ | |
4964 | ||
4965 | static void | |
4966 | protect_loop_closed_ssa_form (edge exit, tree stmt) | |
4967 | { | |
4968 | use_optype uses; | |
4969 | vuse_optype vuses; | |
4970 | v_may_def_optype v_may_defs; | |
4971 | unsigned i; | |
4972 | ||
4973 | get_stmt_operands (stmt); | |
4974 | ||
4975 | uses = STMT_USE_OPS (stmt); | |
4976 | for (i = 0; i < NUM_USES (uses); i++) | |
4977 | protect_loop_closed_ssa_form_use (exit, USE_OP_PTR (uses, i)); | |
4978 | ||
4979 | vuses = STMT_VUSE_OPS (stmt); | |
4980 | for (i = 0; i < NUM_VUSES (vuses); i++) | |
4981 | protect_loop_closed_ssa_form_use (exit, VUSE_OP_PTR (vuses, i)); | |
4982 | ||
4983 | v_may_defs = STMT_V_MAY_DEF_OPS (stmt); | |
4984 | for (i = 0; i < NUM_V_MAY_DEFS (v_may_defs); i++) | |
4985 | protect_loop_closed_ssa_form_use (exit, V_MAY_DEF_OP_PTR (v_may_defs, i)); | |
4986 | } | |
4987 | ||
4988 | /* STMTS compute a value of a phi argument OP on EXIT of a loop. Arrange things | |
4989 | so that they are emitted on the correct place, and so that the loop closed | |
4990 | ssa form is preserved. */ | |
4991 | ||
4992 | static void | |
4993 | compute_phi_arg_on_exit (edge exit, tree stmts, tree op) | |
4994 | { | |
4995 | tree_stmt_iterator tsi; | |
4996 | block_stmt_iterator bsi; | |
4997 | tree phi, stmt, def, next; | |
4998 | ||
c5cbcccf | 4999 | if (!single_pred_p (exit->dest)) |
8b11a64c ZD |
5000 | split_loop_exit_edge (exit); |
5001 | ||
5002 | if (TREE_CODE (stmts) == STATEMENT_LIST) | |
5003 | { | |
5004 | for (tsi = tsi_start (stmts); !tsi_end_p (tsi); tsi_next (&tsi)) | |
5005 | protect_loop_closed_ssa_form (exit, tsi_stmt (tsi)); | |
5006 | } | |
5007 | else | |
5008 | protect_loop_closed_ssa_form (exit, stmts); | |
5009 | ||
5010 | /* Ensure there is label in exit->dest, so that we can | |
5011 | insert after it. */ | |
5012 | tree_block_label (exit->dest); | |
5013 | bsi = bsi_after_labels (exit->dest); | |
5014 | bsi_insert_after (&bsi, stmts, BSI_CONTINUE_LINKING); | |
5015 | ||
5016 | if (!op) | |
5017 | return; | |
5018 | ||
5019 | for (phi = phi_nodes (exit->dest); phi; phi = next) | |
5020 | { | |
eaf0dc02 | 5021 | next = PHI_CHAIN (phi); |
8b11a64c ZD |
5022 | |
5023 | if (PHI_ARG_DEF_FROM_EDGE (phi, exit) == op) | |
5024 | { | |
5025 | def = PHI_RESULT (phi); | |
5026 | remove_statement (phi, false); | |
5027 | stmt = build2 (MODIFY_EXPR, TREE_TYPE (op), | |
5028 | def, op); | |
5029 | SSA_NAME_DEF_STMT (def) = stmt; | |
5030 | bsi_insert_after (&bsi, stmt, BSI_CONTINUE_LINKING); | |
5031 | } | |
5032 | } | |
5033 | } | |
5034 | ||
5035 | /* Rewrites the final value of USE (that is only needed outside of the loop) | |
5036 | using candidate CAND. */ | |
5037 | ||
5038 | static void | |
5039 | rewrite_use_outer (struct ivopts_data *data, | |
5040 | struct iv_use *use, struct iv_cand *cand) | |
5041 | { | |
5042 | edge exit; | |
5043 | tree value, op, stmts, tgt; | |
5044 | tree phi; | |
5045 | ||
1e128c5f GB |
5046 | switch (TREE_CODE (use->stmt)) |
5047 | { | |
5048 | case PHI_NODE: | |
5049 | tgt = PHI_RESULT (use->stmt); | |
5050 | break; | |
5051 | case MODIFY_EXPR: | |
5052 | tgt = TREE_OPERAND (use->stmt, 0); | |
5053 | break; | |
5054 | default: | |
5055 | gcc_unreachable (); | |
5056 | } | |
5057 | ||
8b11a64c ZD |
5058 | exit = single_dom_exit (data->current_loop); |
5059 | ||
5060 | if (exit) | |
5061 | { | |
5062 | if (!cand->iv) | |
5063 | { | |
ca4c3169 | 5064 | bool ok = may_replace_final_value (data, use, &value); |
1e128c5f | 5065 | gcc_assert (ok); |
8b11a64c ZD |
5066 | } |
5067 | else | |
5068 | value = get_computation_at (data->current_loop, | |
5069 | use, cand, last_stmt (exit->src)); | |
5070 | ||
2f4675b4 | 5071 | value = unshare_expr (value); |
8b11a64c ZD |
5072 | op = force_gimple_operand (value, &stmts, true, SSA_NAME_VAR (tgt)); |
5073 | ||
5074 | /* If we will preserve the iv anyway and we would need to perform | |
5075 | some computation to replace the final value, do nothing. */ | |
5076 | if (stmts && name_info (data, tgt)->preserve_biv) | |
5077 | return; | |
5078 | ||
bb29d951 | 5079 | for (phi = phi_nodes (exit->dest); phi; phi = PHI_CHAIN (phi)) |
8b11a64c ZD |
5080 | { |
5081 | use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, exit); | |
5082 | ||
5083 | if (USE_FROM_PTR (use_p) == tgt) | |
5084 | SET_USE (use_p, op); | |
5085 | } | |
5086 | ||
5087 | if (stmts) | |
5088 | compute_phi_arg_on_exit (exit, stmts, op); | |
5089 | ||
5090 | /* Enable removal of the statement. We cannot remove it directly, | |
5091 | since we may still need the aliasing information attached to the | |
5092 | ssa name defined by it. */ | |
5093 | name_info (data, tgt)->iv->have_use_for = false; | |
5094 | return; | |
5095 | } | |
5096 | ||
5097 | /* If the variable is going to be preserved anyway, there is nothing to | |
5098 | do. */ | |
5099 | if (name_info (data, tgt)->preserve_biv) | |
5100 | return; | |
5101 | ||
5102 | /* Otherwise we just need to compute the iv. */ | |
5103 | rewrite_use_nonlinear_expr (data, use, cand); | |
5104 | } | |
5105 | ||
5106 | /* Rewrites USE using candidate CAND. */ | |
5107 | ||
5108 | static void | |
5109 | rewrite_use (struct ivopts_data *data, | |
5110 | struct iv_use *use, struct iv_cand *cand) | |
5111 | { | |
5112 | switch (use->type) | |
5113 | { | |
5114 | case USE_NONLINEAR_EXPR: | |
5115 | rewrite_use_nonlinear_expr (data, use, cand); | |
5116 | break; | |
5117 | ||
5118 | case USE_OUTER: | |
5119 | rewrite_use_outer (data, use, cand); | |
5120 | break; | |
5121 | ||
5122 | case USE_ADDRESS: | |
5123 | rewrite_use_address (data, use, cand); | |
5124 | break; | |
5125 | ||
5126 | case USE_COMPARE: | |
5127 | rewrite_use_compare (data, use, cand); | |
5128 | break; | |
5129 | ||
5130 | default: | |
1e128c5f | 5131 | gcc_unreachable (); |
8b11a64c ZD |
5132 | } |
5133 | modify_stmt (use->stmt); | |
5134 | } | |
5135 | ||
5136 | /* Rewrite the uses using the selected induction variables. */ | |
5137 | ||
5138 | static void | |
5139 | rewrite_uses (struct ivopts_data *data) | |
5140 | { | |
5141 | unsigned i; | |
5142 | struct iv_cand *cand; | |
5143 | struct iv_use *use; | |
5144 | ||
5145 | for (i = 0; i < n_iv_uses (data); i++) | |
5146 | { | |
5147 | use = iv_use (data, i); | |
5148 | cand = use->selected; | |
1e128c5f | 5149 | gcc_assert (cand); |
8b11a64c ZD |
5150 | |
5151 | rewrite_use (data, use, cand); | |
5152 | } | |
5153 | } | |
5154 | ||
5155 | /* Removes the ivs that are not used after rewriting. */ | |
5156 | ||
5157 | static void | |
5158 | remove_unused_ivs (struct ivopts_data *data) | |
5159 | { | |
5160 | unsigned j; | |
87c476a2 | 5161 | bitmap_iterator bi; |
8b11a64c | 5162 | |
87c476a2 | 5163 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi) |
8b11a64c ZD |
5164 | { |
5165 | struct version_info *info; | |
5166 | ||
5167 | info = ver_info (data, j); | |
5168 | if (info->iv | |
5169 | && !zero_p (info->iv->step) | |
5170 | && !info->inv_id | |
5171 | && !info->iv->have_use_for | |
5172 | && !info->preserve_biv) | |
5173 | remove_statement (SSA_NAME_DEF_STMT (info->iv->ssa_name), true); | |
87c476a2 | 5174 | } |
8b11a64c ZD |
5175 | } |
5176 | ||
5177 | /* Frees data allocated by the optimization of a single loop. */ | |
5178 | ||
5179 | static void | |
5180 | free_loop_data (struct ivopts_data *data) | |
5181 | { | |
5182 | unsigned i, j; | |
87c476a2 | 5183 | bitmap_iterator bi; |
8b11a64c | 5184 | |
ca4c3169 ZD |
5185 | htab_empty (data->niters); |
5186 | ||
87c476a2 | 5187 | EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi) |
8b11a64c ZD |
5188 | { |
5189 | struct version_info *info; | |
5190 | ||
5191 | info = ver_info (data, i); | |
5192 | if (info->iv) | |
5193 | free (info->iv); | |
5194 | info->iv = NULL; | |
5195 | info->has_nonlin_use = false; | |
5196 | info->preserve_biv = false; | |
5197 | info->inv_id = 0; | |
87c476a2 | 5198 | } |
8b11a64c | 5199 | bitmap_clear (data->relevant); |
b1b02be2 | 5200 | bitmap_clear (data->important_candidates); |
8b11a64c ZD |
5201 | |
5202 | for (i = 0; i < n_iv_uses (data); i++) | |
5203 | { | |
5204 | struct iv_use *use = iv_use (data, i); | |
5205 | ||
5206 | free (use->iv); | |
8bdbfff5 | 5207 | BITMAP_FREE (use->related_cands); |
8b11a64c ZD |
5208 | for (j = 0; j < use->n_map_members; j++) |
5209 | if (use->cost_map[j].depends_on) | |
8bdbfff5 | 5210 | BITMAP_FREE (use->cost_map[j].depends_on); |
8b11a64c ZD |
5211 | free (use->cost_map); |
5212 | free (use); | |
5213 | } | |
5214 | VARRAY_POP_ALL (data->iv_uses); | |
5215 | ||
5216 | for (i = 0; i < n_iv_cands (data); i++) | |
5217 | { | |
5218 | struct iv_cand *cand = iv_cand (data, i); | |
5219 | ||
5220 | if (cand->iv) | |
5221 | free (cand->iv); | |
5222 | free (cand); | |
5223 | } | |
5224 | VARRAY_POP_ALL (data->iv_candidates); | |
5225 | ||
5226 | if (data->version_info_size < num_ssa_names) | |
5227 | { | |
5228 | data->version_info_size = 2 * num_ssa_names; | |
5229 | free (data->version_info); | |
5230 | data->version_info = xcalloc (data->version_info_size, | |
5231 | sizeof (struct version_info)); | |
5232 | } | |
5233 | ||
5234 | data->max_inv_id = 0; | |
5235 | ||
5236 | for (i = 0; i < VARRAY_ACTIVE_SIZE (decl_rtl_to_reset); i++) | |
5237 | { | |
5238 | tree obj = VARRAY_GENERIC_PTR_NOGC (decl_rtl_to_reset, i); | |
5239 | ||
5240 | SET_DECL_RTL (obj, NULL_RTX); | |
5241 | } | |
5242 | VARRAY_POP_ALL (decl_rtl_to_reset); | |
5243 | } | |
5244 | ||
5245 | /* Finalizes data structures used by the iv optimization pass. LOOPS is the | |
5246 | loop tree. */ | |
5247 | ||
5248 | static void | |
5249 | tree_ssa_iv_optimize_finalize (struct loops *loops, struct ivopts_data *data) | |
5250 | { | |
5251 | unsigned i; | |
5252 | ||
5253 | for (i = 1; i < loops->num; i++) | |
5254 | if (loops->parray[i]) | |
5255 | { | |
5256 | free (loops->parray[i]->aux); | |
5257 | loops->parray[i]->aux = NULL; | |
5258 | } | |
5259 | ||
5260 | free_loop_data (data); | |
5261 | free (data->version_info); | |
8bdbfff5 NS |
5262 | BITMAP_FREE (data->relevant); |
5263 | BITMAP_FREE (data->important_candidates); | |
ca4c3169 | 5264 | htab_delete (data->niters); |
8b11a64c ZD |
5265 | |
5266 | VARRAY_FREE (decl_rtl_to_reset); | |
5267 | VARRAY_FREE (data->iv_uses); | |
5268 | VARRAY_FREE (data->iv_candidates); | |
5269 | } | |
5270 | ||
5271 | /* Optimizes the LOOP. Returns true if anything changed. */ | |
5272 | ||
5273 | static bool | |
5274 | tree_ssa_iv_optimize_loop (struct ivopts_data *data, struct loop *loop) | |
5275 | { | |
5276 | bool changed = false; | |
b1b02be2 | 5277 | struct iv_ca *iv_ca; |
8b11a64c ZD |
5278 | edge exit; |
5279 | ||
5280 | data->current_loop = loop; | |
5281 | ||
5282 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
5283 | { | |
5284 | fprintf (dump_file, "Processing loop %d\n", loop->num); | |
5285 | ||
5286 | exit = single_dom_exit (loop); | |
5287 | if (exit) | |
5288 | { | |
5289 | fprintf (dump_file, " single exit %d -> %d, exit condition ", | |
5290 | exit->src->index, exit->dest->index); | |
5291 | print_generic_expr (dump_file, last_stmt (exit->src), TDF_SLIM); | |
5292 | fprintf (dump_file, "\n"); | |
5293 | } | |
5294 | ||
5295 | fprintf (dump_file, "\n"); | |
5296 | } | |
5297 | ||
5298 | /* For each ssa name determines whether it behaves as an induction variable | |
5299 | in some loop. */ | |
5300 | if (!find_induction_variables (data)) | |
5301 | goto finish; | |
5302 | ||
5303 | /* Finds interesting uses (item 1). */ | |
5304 | find_interesting_uses (data); | |
5305 | if (n_iv_uses (data) > MAX_CONSIDERED_USES) | |
5306 | goto finish; | |
5307 | ||
5308 | /* Finds candidates for the induction variables (item 2). */ | |
5309 | find_iv_candidates (data); | |
5310 | ||
5311 | /* Calculates the costs (item 3, part 1). */ | |
5312 | determine_use_iv_costs (data); | |
5313 | determine_iv_costs (data); | |
5314 | determine_set_costs (data); | |
5315 | ||
5316 | /* Find the optimal set of induction variables (item 3, part 2). */ | |
b1b02be2 ZD |
5317 | iv_ca = find_optimal_iv_set (data); |
5318 | if (!iv_ca) | |
8b11a64c ZD |
5319 | goto finish; |
5320 | changed = true; | |
5321 | ||
5322 | /* Create the new induction variables (item 4, part 1). */ | |
b1b02be2 ZD |
5323 | create_new_ivs (data, iv_ca); |
5324 | iv_ca_free (&iv_ca); | |
8b11a64c ZD |
5325 | |
5326 | /* Rewrite the uses (item 4, part 2). */ | |
5327 | rewrite_uses (data); | |
5328 | ||
5329 | /* Remove the ivs that are unused after rewriting. */ | |
5330 | remove_unused_ivs (data); | |
5331 | ||
8b11a64c ZD |
5332 | /* We have changed the structure of induction variables; it might happen |
5333 | that definitions in the scev database refer to some of them that were | |
5334 | eliminated. */ | |
5335 | scev_reset (); | |
5336 | ||
5337 | finish: | |
5338 | free_loop_data (data); | |
5339 | ||
5340 | return changed; | |
5341 | } | |
5342 | ||
5343 | /* Main entry point. Optimizes induction variables in LOOPS. */ | |
5344 | ||
5345 | void | |
5346 | tree_ssa_iv_optimize (struct loops *loops) | |
5347 | { | |
5348 | struct loop *loop; | |
5349 | struct ivopts_data data; | |
5350 | ||
5351 | tree_ssa_iv_optimize_init (loops, &data); | |
5352 | ||
5353 | /* Optimize the loops starting with the innermost ones. */ | |
5354 | loop = loops->tree_root; | |
5355 | while (loop->inner) | |
5356 | loop = loop->inner; | |
5357 | ||
5358 | #ifdef ENABLE_CHECKING | |
5359 | verify_loop_closed_ssa (); | |
8679c649 | 5360 | verify_stmts (); |
8b11a64c ZD |
5361 | #endif |
5362 | ||
5363 | /* Scan the loops, inner ones first. */ | |
5364 | while (loop != loops->tree_root) | |
5365 | { | |
8679c649 JH |
5366 | if (dump_file && (dump_flags & TDF_DETAILS)) |
5367 | flow_loop_dump (loop, dump_file, NULL, 1); | |
e9472263 ZD |
5368 | |
5369 | tree_ssa_iv_optimize_loop (&data, loop); | |
8b11a64c ZD |
5370 | |
5371 | if (loop->next) | |
5372 | { | |
5373 | loop = loop->next; | |
5374 | while (loop->inner) | |
5375 | loop = loop->inner; | |
5376 | } | |
5377 | else | |
5378 | loop = loop->outer; | |
5379 | } | |
5380 | ||
e9472263 ZD |
5381 | #ifdef ENABLE_CHECKING |
5382 | verify_loop_closed_ssa (); | |
5383 | verify_stmts (); | |
5384 | #endif | |
5385 | ||
8b11a64c ZD |
5386 | tree_ssa_iv_optimize_finalize (loops, &data); |
5387 | } |