Line data Source code
1 : /* Induction variable optimizations.
2 : Copyright (C) 2003-2026 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : /* This pass tries to find the optimal set of induction variables for the loop.
21 : It optimizes just the basic linear induction variables (although adding
22 : support for other types should not be too hard). It includes the
23 : optimizations commonly known as strength reduction, induction variable
24 : coalescing and induction variable elimination. It does it in the
25 : following steps:
26 :
27 : 1) The interesting uses of induction variables are found. This includes
28 :
29 : -- uses of induction variables in non-linear expressions
30 : -- addresses of arrays
31 : -- comparisons of induction variables
32 :
33 : Note the interesting uses are categorized and handled in group.
34 : Generally, address type uses are grouped together if their iv bases
35 : are different in constant offset.
36 :
37 : 2) Candidates for the induction variables are found. This includes
38 :
39 : -- old induction variables
40 : -- the variables defined by expressions derived from the "interesting
41 : groups/uses" above
42 :
43 : 3) The optimal (w.r. to a cost function) set of variables is chosen. The
44 : cost function assigns a cost to sets of induction variables and consists
45 : of three parts:
46 :
47 : -- The group/use costs. Each of the interesting groups/uses chooses
48 : the best induction variable in the set and adds its cost to the sum.
49 : The cost reflects the time spent on modifying the induction variables
50 : value to be usable for the given purpose (adding base and offset for
51 : arrays, etc.).
52 : -- The variable costs. Each of the variables has a cost assigned that
53 : reflects the costs associated with incrementing the value of the
54 : variable. The original variables are somewhat preferred.
55 : -- The set cost. Depending on the size of the set, extra cost may be
56 : added to reflect register pressure.
57 :
58 : All the costs are defined in a machine-specific way, using the target
59 : hooks and machine descriptions to determine them.
60 :
61 : 4) The trees are transformed to use the new variables, the dead code is
62 : removed.
63 :
64 : All of this is done loop by loop. Doing it globally is theoretically
65 : possible, it might give a better performance and it might enable us
66 : to decide costs more precisely, but getting all the interactions right
67 : would be complicated.
68 :
69 : For the targets supporting low-overhead loops, IVOPTs has to take care of
70 : the loops which will probably be transformed in RTL doloop optimization,
71 : to try to make selected IV candidate set optimal. The process of doloop
72 : support includes:
73 :
74 : 1) Analyze the current loop will be transformed to doloop or not, find and
75 : mark its compare type IV use as doloop use (iv_group field doloop_p), and
76 : set flag doloop_use_p of ivopts_data to notify subsequent processings on
77 : doloop. See analyze_and_mark_doloop_use and its callees for the details.
78 : The target hook predict_doloop_p can be used for target specific checks.
79 :
80 : 2) Add one doloop dedicated IV cand {(may_be_zero ? 1 : (niter + 1)), +, -1},
81 : set flag doloop_p of iv_cand, step cost is set as zero and no extra cost
82 : like biv. For cost determination between doloop IV cand and IV use, the
83 : target hooks doloop_cost_for_generic and doloop_cost_for_address are
84 : provided to add on extra costs for generic type and address type IV use.
85 : Zero cost is assigned to the pair between doloop IV cand and doloop IV
86 : use, and bound zero is set for IV elimination.
87 :
88 : 3) With the cost setting in step 2), the current cost model based IV
89 : selection algorithm will process as usual, pick up doloop dedicated IV if
90 : profitable. */
91 :
92 : #include "config.h"
93 : #include "system.h"
94 : #include "coretypes.h"
95 : #include "backend.h"
96 : #include "rtl.h"
97 : #include "tree.h"
98 : #include "gimple.h"
99 : #include "cfghooks.h"
100 : #include "tree-pass.h"
101 : #include "memmodel.h"
102 : #include "tm_p.h"
103 : #include "ssa.h"
104 : #include "expmed.h"
105 : #include "insn-config.h"
106 : #include "emit-rtl.h"
107 : #include "recog.h"
108 : #include "cgraph.h"
109 : #include "gimple-pretty-print.h"
110 : #include "alias.h"
111 : #include "fold-const.h"
112 : #include "stor-layout.h"
113 : #include "tree-eh.h"
114 : #include "gimplify.h"
115 : #include "gimple-iterator.h"
116 : #include "gimplify-me.h"
117 : #include "tree-cfg.h"
118 : #include "tree-ssa-loop-ivopts.h"
119 : #include "tree-ssa-loop-manip.h"
120 : #include "tree-ssa-loop-niter.h"
121 : #include "tree-ssa-loop.h"
122 : #include "explow.h"
123 : #include "expr.h"
124 : #include "tree-dfa.h"
125 : #include "tree-ssa.h"
126 : #include "cfgloop.h"
127 : #include "tree-scalar-evolution.h"
128 : #include "tree-affine.h"
129 : #include "tree-ssa-propagate.h"
130 : #include "tree-ssa-address.h"
131 : #include "builtins.h"
132 : #include "tree-vectorizer.h"
133 : #include "dbgcnt.h"
134 : #include "cfganal.h"
135 : #include "gimple-fold.h"
136 : #include "gimple-range.h"
137 :
138 : /* For lang_hooks.types.type_for_mode. */
139 : #include "langhooks.h"
140 :
141 : /* FIXME: Expressions are expanded to RTL in this pass to determine the
142 : cost of different addressing modes. This should be moved to a TBD
143 : interface between the GIMPLE and RTL worlds. */
144 :
145 : /* The infinite cost. */
146 : #define INFTY 1000000000
147 :
148 : /* Returns the expected number of loop iterations for LOOP.
149 : The average trip count is computed from profile data if it
150 : exists. */
151 :
152 : static inline unsigned HOST_WIDE_INT
153 8758692 : avg_loop_niter (class loop *loop)
154 : {
155 8758692 : HOST_WIDE_INT niter = estimated_stmt_executions_int (loop);
156 8758692 : if (niter == -1)
157 : {
158 4907682 : niter = likely_max_stmt_executions_int (loop);
159 :
160 4907682 : if (niter == -1 || niter > param_avg_loop_niter)
161 4122931 : return param_avg_loop_niter;
162 : }
163 :
164 4635761 : return niter;
165 : }
166 :
167 : struct iv_use;
168 :
169 : /* Representation of the induction variable. */
170 : struct iv
171 : {
172 : tree base; /* Initial value of the iv. */
173 : tree base_object; /* A memory object to that the induction variable points. */
174 : tree step; /* Step of the iv (constant only). */
175 : tree ssa_name; /* The ssa name with the value. */
176 : struct iv_use *nonlin_use; /* The identifier in the use if it is the case. */
177 : bool biv_p; /* Is it a biv? */
178 : bool no_overflow; /* True if the iv doesn't overflow. */
179 : bool have_address_use;/* For biv, indicate if it's used in any address
180 : type use. */
181 : };
182 :
183 : /* Per-ssa version information (induction variable descriptions, etc.). */
184 : struct version_info
185 : {
186 : tree name; /* The ssa name. */
187 : struct iv *iv; /* Induction variable description. */
188 : bool has_nonlin_use; /* For a loop-level invariant, whether it is used in
189 : an expression that is not an induction variable. */
190 : bool preserve_biv; /* For the original biv, whether to preserve it. */
191 : unsigned inv_id; /* Id of an invariant. */
192 : };
193 :
194 : /* Types of uses. */
195 : enum use_type
196 : {
197 : USE_NONLINEAR_EXPR, /* Use in a nonlinear expression. */
198 : USE_REF_ADDRESS, /* Use is an address for an explicit memory
199 : reference. */
200 : USE_PTR_ADDRESS, /* Use is a pointer argument to a function in
201 : cases where the expansion of the function
202 : will turn the argument into a normal address. */
203 : USE_COMPARE /* Use is a compare. */
204 : };
205 :
206 : /* Cost of a computation. */
207 : class comp_cost
208 : {
209 : public:
210 132111695 : comp_cost (): cost (0), complexity (0), scratch (0)
211 : {}
212 :
213 25270770 : comp_cost (int64_t cost, unsigned complexity, int64_t scratch = 0)
214 15399095 : : cost (cost), complexity (complexity), scratch (scratch)
215 : {}
216 :
217 : /* Returns true if COST is infinite. */
218 : bool infinite_cost_p ();
219 :
220 : /* Adds costs COST1 and COST2. */
221 : friend comp_cost operator+ (comp_cost cost1, comp_cost cost2);
222 :
223 : /* Adds COST to the comp_cost. */
224 : comp_cost operator+= (comp_cost cost);
225 :
226 : /* Adds constant C to this comp_cost. */
227 : comp_cost operator+= (HOST_WIDE_INT c);
228 :
229 : /* Subtracts constant C to this comp_cost. */
230 : comp_cost operator-= (HOST_WIDE_INT c);
231 :
232 : /* Divide the comp_cost by constant C. */
233 : comp_cost operator/= (HOST_WIDE_INT c);
234 :
235 : /* Multiply the comp_cost by constant C. */
236 : comp_cost operator*= (HOST_WIDE_INT c);
237 :
238 : /* Subtracts costs COST1 and COST2. */
239 : friend comp_cost operator- (comp_cost cost1, comp_cost cost2);
240 :
241 : /* Subtracts COST from this comp_cost. */
242 : comp_cost operator-= (comp_cost cost);
243 :
244 : /* Returns true if COST1 is smaller than COST2. */
245 : friend bool operator< (comp_cost cost1, comp_cost cost2);
246 :
247 : /* Returns true if COST1 and COST2 are equal. */
248 : friend bool operator== (comp_cost cost1, comp_cost cost2);
249 :
250 : /* Returns true if COST1 is smaller or equal than COST2. */
251 : friend bool operator<= (comp_cost cost1, comp_cost cost2);
252 :
253 : int64_t cost; /* The runtime cost. */
254 : unsigned complexity; /* The estimate of the complexity of the code for
255 : the computation (in no concrete units --
256 : complexity field should be larger for more
257 : complex expressions and addressing modes). */
258 : int64_t scratch; /* Scratch used during cost computation. */
259 : };
260 :
261 : static const comp_cost no_cost;
262 : static const comp_cost infinite_cost (INFTY, 0, INFTY);
263 :
264 : bool
265 1866654772 : comp_cost::infinite_cost_p ()
266 : {
267 1866654772 : return cost == INFTY;
268 : }
269 :
270 : comp_cost
271 248942288 : operator+ (comp_cost cost1, comp_cost cost2)
272 : {
273 248942288 : if (cost1.infinite_cost_p () || cost2.infinite_cost_p ())
274 1871146 : return infinite_cost;
275 :
276 247071142 : gcc_assert (cost1.cost + cost2.cost < infinite_cost.cost);
277 247071142 : cost1.cost += cost2.cost;
278 247071142 : cost1.complexity += cost2.complexity;
279 :
280 247071142 : return cost1;
281 : }
282 :
283 : comp_cost
284 213804573 : operator- (comp_cost cost1, comp_cost cost2)
285 : {
286 213804573 : if (cost1.infinite_cost_p ())
287 0 : return infinite_cost;
288 :
289 213804573 : gcc_assert (!cost2.infinite_cost_p ());
290 213804573 : gcc_assert (cost1.cost - cost2.cost < infinite_cost.cost);
291 :
292 213804573 : cost1.cost -= cost2.cost;
293 213804573 : cost1.complexity -= cost2.complexity;
294 :
295 213804573 : return cost1;
296 : }
297 :
298 : comp_cost
299 248942288 : comp_cost::operator+= (comp_cost cost)
300 : {
301 248942288 : *this = *this + cost;
302 248942288 : return *this;
303 : }
304 :
305 : comp_cost
306 882672252 : comp_cost::operator+= (HOST_WIDE_INT c)
307 : {
308 882672252 : if (c >= INFTY)
309 0 : this->cost = INFTY;
310 :
311 882672252 : if (infinite_cost_p ())
312 0 : return *this;
313 :
314 882672252 : gcc_assert (this->cost + c < infinite_cost.cost);
315 882672252 : this->cost += c;
316 :
317 882672252 : return *this;
318 : }
319 :
320 : comp_cost
321 543022 : comp_cost::operator-= (HOST_WIDE_INT c)
322 : {
323 543022 : if (infinite_cost_p ())
324 0 : return *this;
325 :
326 543022 : gcc_assert (this->cost - c < infinite_cost.cost);
327 543022 : this->cost -= c;
328 :
329 543022 : return *this;
330 : }
331 :
332 : comp_cost
333 0 : comp_cost::operator/= (HOST_WIDE_INT c)
334 : {
335 0 : gcc_assert (c != 0);
336 0 : if (infinite_cost_p ())
337 0 : return *this;
338 :
339 0 : this->cost /= c;
340 :
341 0 : return *this;
342 : }
343 :
344 : comp_cost
345 0 : comp_cost::operator*= (HOST_WIDE_INT c)
346 : {
347 0 : if (infinite_cost_p ())
348 0 : return *this;
349 :
350 0 : gcc_assert (this->cost * c < infinite_cost.cost);
351 0 : this->cost *= c;
352 :
353 0 : return *this;
354 : }
355 :
356 : comp_cost
357 213804573 : comp_cost::operator-= (comp_cost cost)
358 : {
359 213804573 : *this = *this - cost;
360 213804573 : return *this;
361 : }
362 :
363 : bool
364 190454650 : operator< (comp_cost cost1, comp_cost cost2)
365 : {
366 190454650 : if (cost1.cost == cost2.cost)
367 82611779 : return cost1.complexity < cost2.complexity;
368 :
369 107842871 : return cost1.cost < cost2.cost;
370 : }
371 :
372 : bool
373 3933622 : operator== (comp_cost cost1, comp_cost cost2)
374 : {
375 3933622 : return cost1.cost == cost2.cost
376 3933622 : && cost1.complexity == cost2.complexity;
377 : }
378 :
379 : bool
380 6457807 : operator<= (comp_cost cost1, comp_cost cost2)
381 : {
382 6457807 : return cost1 < cost2 || cost1 == cost2;
383 : }
384 :
385 : struct iv_inv_expr_ent;
386 :
387 : /* The candidate - cost pair. */
388 : class cost_pair
389 : {
390 : public:
391 : struct iv_cand *cand; /* The candidate. */
392 : comp_cost cost; /* The cost. */
393 : enum tree_code comp; /* For iv elimination, the comparison. */
394 : bitmap inv_vars; /* The list of invariant ssa_vars that have to be
395 : preserved when representing iv_use with iv_cand. */
396 : bitmap inv_exprs; /* The list of newly created invariant expressions
397 : when representing iv_use with iv_cand. */
398 : tree value; /* For final value elimination, the expression for
399 : the final value of the iv. For iv elimination,
400 : the new bound to compare with. */
401 : };
402 :
403 : /* Use. */
404 : struct iv_use
405 : {
406 : unsigned id; /* The id of the use. */
407 : unsigned group_id; /* The group id the use belongs to. */
408 : enum use_type type; /* Type of the use. */
409 : tree mem_type; /* The memory type to use when testing whether an
410 : address is legitimate, and what the address's
411 : cost is. */
412 : struct iv *iv; /* The induction variable it is based on. */
413 : gimple *stmt; /* Statement in that it occurs. */
414 : tree *op_p; /* The place where it occurs. */
415 :
416 : tree addr_base; /* Base address with const offset stripped. */
417 : poly_uint64 addr_offset;
418 : /* Const offset stripped from base address. */
419 : };
420 :
421 : /* Group of uses. */
422 : struct iv_group
423 : {
424 : /* The id of the group. */
425 : unsigned id;
426 : /* Uses of the group are of the same type. */
427 : enum use_type type;
428 : /* The set of "related" IV candidates, plus the important ones. */
429 : bitmap related_cands;
430 : /* Number of IV candidates in the cost_map. */
431 : unsigned n_map_members;
432 : /* The costs wrto the iv candidates. */
433 : class cost_pair *cost_map;
434 : /* The selected candidate for the group. */
435 : struct iv_cand *selected;
436 : /* To indicate this is a doloop use group. */
437 : bool doloop_p;
438 : /* Uses in the group. */
439 : vec<struct iv_use *> vuses;
440 : };
441 :
442 : /* The position where the iv is computed. */
443 : enum iv_position
444 : {
445 : IP_NORMAL, /* At the end, just before the exit condition. */
446 : IP_END, /* At the end of the latch block. */
447 : IP_BEFORE_USE, /* Immediately before a specific use. */
448 : IP_AFTER_USE, /* Immediately after a specific use. */
449 : IP_ORIGINAL /* The original biv. */
450 : };
451 :
452 : /* The induction variable candidate. */
453 : struct iv_cand
454 : {
455 : unsigned id; /* The number of the candidate. */
456 : bool important; /* Whether this is an "important" candidate, i.e. such
457 : that it should be considered by all uses. */
458 : bool involves_undefs; /* Whether the IV involves undefined values. */
459 : enum iv_position pos : 8;/* Where it is computed. */
460 : gimple *incremented_at;/* For original biv, the statement where it is
461 : incremented. */
462 : tree var_before; /* The variable used for it before increment. */
463 : tree var_after; /* The variable used for it after increment. */
464 : struct iv *iv; /* The value of the candidate. NULL for
465 : "pseudocandidate" used to indicate the possibility
466 : to replace the final value of an iv by direct
467 : computation of the value. */
468 : unsigned cost; /* Cost of the candidate. */
469 : unsigned cost_step; /* Cost of the candidate's increment operation. */
470 : struct iv_use *ainc_use; /* For IP_{BEFORE,AFTER}_USE candidates, the place
471 : where it is incremented. */
472 : bitmap inv_vars; /* The list of invariant ssa_vars used in step of the
473 : iv_cand. */
474 : bitmap inv_exprs; /* If step is more complicated than a single ssa_var,
475 : handle it as a new invariant expression which will
476 : be hoisted out of loop. */
477 : struct iv *orig_iv; /* The original iv if this cand is added from biv with
478 : smaller type. */
479 : bool doloop_p; /* Whether this is a doloop candidate. */
480 : };
481 :
482 : /* Hashtable entry for common candidate derived from iv uses. */
483 2628788 : class iv_common_cand
484 : {
485 : public:
486 : tree base;
487 : tree step;
488 : /* IV uses from which this common candidate is derived. */
489 : auto_vec<struct iv_use *> uses;
490 : hashval_t hash;
491 : };
492 :
493 : /* Hashtable helpers. */
494 :
495 : struct iv_common_cand_hasher : delete_ptr_hash <iv_common_cand>
496 : {
497 : static inline hashval_t hash (const iv_common_cand *);
498 : static inline bool equal (const iv_common_cand *, const iv_common_cand *);
499 : };
500 :
501 : /* Hash function for possible common candidates. */
502 :
503 : inline hashval_t
504 9659637 : iv_common_cand_hasher::hash (const iv_common_cand *ccand)
505 : {
506 9659637 : return ccand->hash;
507 : }
508 :
509 : /* Hash table equality function for common candidates. */
510 :
511 : inline bool
512 10917377 : iv_common_cand_hasher::equal (const iv_common_cand *ccand1,
513 : const iv_common_cand *ccand2)
514 : {
515 10917377 : return (ccand1->hash == ccand2->hash
516 1623599 : && operand_equal_p (ccand1->base, ccand2->base, 0)
517 1605248 : && operand_equal_p (ccand1->step, ccand2->step, 0)
518 12517841 : && (TYPE_PRECISION (TREE_TYPE (ccand1->base))
519 1600464 : == TYPE_PRECISION (TREE_TYPE (ccand2->base))));
520 : }
521 :
522 : /* Loop invariant expression hashtable entry. */
523 :
524 : struct iv_inv_expr_ent
525 : {
526 : /* Tree expression of the entry. */
527 : tree expr;
528 : /* Unique identifier. */
529 : int id;
530 : /* Hash value. */
531 : hashval_t hash;
532 : };
533 :
534 : /* Sort iv_inv_expr_ent pair A and B by id field. */
535 :
536 : static int
537 5739 : sort_iv_inv_expr_ent (const void *a, const void *b)
538 : {
539 5739 : const iv_inv_expr_ent * const *e1 = (const iv_inv_expr_ent * const *) (a);
540 5739 : const iv_inv_expr_ent * const *e2 = (const iv_inv_expr_ent * const *) (b);
541 :
542 5739 : unsigned id1 = (*e1)->id;
543 5739 : unsigned id2 = (*e2)->id;
544 :
545 5739 : if (id1 < id2)
546 : return -1;
547 2667 : else if (id1 > id2)
548 : return 1;
549 : else
550 0 : return 0;
551 : }
552 :
553 : /* Hashtable helpers. */
554 :
555 : struct iv_inv_expr_hasher : free_ptr_hash <iv_inv_expr_ent>
556 : {
557 : static inline hashval_t hash (const iv_inv_expr_ent *);
558 : static inline bool equal (const iv_inv_expr_ent *, const iv_inv_expr_ent *);
559 : };
560 :
561 : /* Return true if uses of type TYPE represent some form of address. */
562 :
563 : inline bool
564 9018040 : address_p (use_type type)
565 : {
566 9018040 : return type == USE_REF_ADDRESS || type == USE_PTR_ADDRESS;
567 : }
568 :
569 : /* Hash function for loop invariant expressions. */
570 :
571 : inline hashval_t
572 6480382 : iv_inv_expr_hasher::hash (const iv_inv_expr_ent *expr)
573 : {
574 6480382 : return expr->hash;
575 : }
576 :
577 : /* Hash table equality function for expressions. */
578 :
579 : inline bool
580 7784825 : iv_inv_expr_hasher::equal (const iv_inv_expr_ent *expr1,
581 : const iv_inv_expr_ent *expr2)
582 : {
583 7784825 : return expr1->hash == expr2->hash
584 7784825 : && operand_equal_p (expr1->expr, expr2->expr, 0);
585 : }
586 :
587 : struct ivopts_data
588 : {
589 : /* The currently optimized loop. */
590 : class loop *current_loop;
591 : location_t loop_loc;
592 :
593 : /* Numbers of iterations for all exits of the current loop. */
594 : hash_map<edge, tree_niter_desc *> *niters;
595 :
596 : /* Number of registers used in it. */
597 : unsigned regs_used;
598 :
599 : /* The size of version_info array allocated. */
600 : unsigned version_info_size;
601 :
602 : /* The array of information for the ssa names. */
603 : struct version_info *version_info;
604 :
605 : /* The hashtable of loop invariant expressions created
606 : by ivopt. */
607 : hash_table<iv_inv_expr_hasher> *inv_expr_tab;
608 :
609 : /* The bitmap of indices in version_info whose value was changed. */
610 : bitmap relevant;
611 :
612 : /* The uses of induction variables. */
613 : vec<iv_group *> vgroups;
614 :
615 : /* The candidates. */
616 : vec<iv_cand *> vcands;
617 :
618 : /* A bitmap of important candidates. */
619 : bitmap important_candidates;
620 :
621 : /* Cache used by tree_to_aff_combination_expand. */
622 : hash_map<tree, name_expansion *> *name_expansion_cache;
623 :
624 : /* The hashtable of common candidates derived from iv uses. */
625 : hash_table<iv_common_cand_hasher> *iv_common_cand_tab;
626 :
627 : /* The common candidates. */
628 : vec<iv_common_cand *> iv_common_cands;
629 :
630 : /* Hash map recording base object information of tree exp. */
631 : hash_map<tree, tree> *base_object_map;
632 :
633 : /* The maximum invariant variable id. */
634 : unsigned max_inv_var_id;
635 :
636 : /* The maximum invariant expression id. */
637 : unsigned max_inv_expr_id;
638 :
639 : /* Number of no_overflow BIVs which are not used in memory address. */
640 : unsigned bivs_not_used_in_addr;
641 :
642 : /* Obstack for iv structure. */
643 : struct obstack iv_obstack;
644 :
645 : /* Whether to consider just related and important candidates when replacing a
646 : use. */
647 : bool consider_all_candidates;
648 :
649 : /* Are we optimizing for speed? */
650 : bool speed;
651 :
652 : /* Whether the loop body includes any function calls. */
653 : bool body_includes_call;
654 :
655 : /* Whether the loop body can only be exited via single exit. */
656 : bool loop_single_exit_p;
657 :
658 : /* Whether the loop has doloop comparison use. */
659 : bool doloop_use_p;
660 : };
661 :
662 : /* An assignment of iv candidates to uses. */
663 :
664 : class iv_ca
665 : {
666 : public:
667 : /* The number of uses covered by the assignment. */
668 : unsigned upto;
669 :
670 : /* Number of uses that cannot be expressed by the candidates in the set. */
671 : unsigned bad_groups;
672 :
673 : /* Candidate assigned to a use, together with the related costs. */
674 : class cost_pair **cand_for_group;
675 :
676 : /* Number of times each candidate is used. */
677 : unsigned *n_cand_uses;
678 :
679 : /* The candidates used. */
680 : bitmap cands;
681 :
682 : /* The number of candidates in the set. */
683 : unsigned n_cands;
684 :
685 : /* The number of invariants needed, including both invariant variants and
686 : invariant expressions. */
687 : unsigned n_invs;
688 :
689 : /* Total cost of expressing uses. */
690 : comp_cost cand_use_cost;
691 :
692 : /* Total cost of candidates. */
693 : int64_t cand_cost;
694 :
695 : /* Number of times each invariant variable is used. */
696 : unsigned *n_inv_var_uses;
697 :
698 : /* Number of times each invariant expression is used. */
699 : unsigned *n_inv_expr_uses;
700 :
701 : /* Total cost of the assignment. */
702 : comp_cost cost;
703 : };
704 :
705 : /* Difference of two iv candidate assignments. */
706 :
707 : struct iv_ca_delta
708 : {
709 : /* Changed group. */
710 : struct iv_group *group;
711 :
712 : /* An old assignment (for rollback purposes). */
713 : class cost_pair *old_cp;
714 :
715 : /* A new assignment. */
716 : class cost_pair *new_cp;
717 :
718 : /* Next change in the list. */
719 : struct iv_ca_delta *next;
720 : };
721 :
722 : /* Bound on number of candidates below that all candidates are considered. */
723 :
724 : #define CONSIDER_ALL_CANDIDATES_BOUND \
725 : ((unsigned) param_iv_consider_all_candidates_bound)
726 :
727 : /* If there are more iv occurrences, we just give up (it is quite unlikely that
728 : optimizing such a loop would help, and it would take ages). */
729 :
730 : #define MAX_CONSIDERED_GROUPS \
731 : ((unsigned) param_iv_max_considered_uses)
732 :
733 : /* If there are at most this number of ivs in the set, try removing unnecessary
734 : ivs from the set always. */
735 :
736 : #define ALWAYS_PRUNE_CAND_SET_BOUND \
737 : ((unsigned) param_iv_always_prune_cand_set_bound)
738 :
739 : /* The list of trees for that the decl_rtl field must be reset is stored
740 : here. */
741 :
742 : static vec<tree> decl_rtl_to_reset;
743 :
744 : static comp_cost force_expr_to_var_cost (tree, bool);
745 :
746 : /* The single loop exit if it dominates the latch, NULL otherwise. */
747 :
748 : edge
749 707186 : single_dom_exit (class loop *loop)
750 : {
751 707186 : edge exit = single_exit (loop);
752 :
753 707186 : if (!exit)
754 : return NULL;
755 :
756 468334 : if (!just_once_each_iteration_p (loop, exit->src))
757 4942 : return NULL;
758 :
759 : return exit;
760 : }
761 :
762 : /* Dumps information about the induction variable IV to FILE. Don't dump
763 : variable's name if DUMP_NAME is FALSE. The information is dumped with
764 : preceding spaces indicated by INDENT_LEVEL. */
765 :
766 : void
767 1603 : dump_iv (FILE *file, struct iv *iv, bool dump_name, unsigned indent_level)
768 : {
769 1603 : const char *p;
770 1603 : const char spaces[9] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '\0'};
771 :
772 1603 : if (indent_level > 4)
773 : indent_level = 4;
774 1603 : p = spaces + 8 - (indent_level << 1);
775 :
776 1603 : fprintf (file, "%sIV struct:\n", p);
777 1603 : if (iv->ssa_name && dump_name)
778 : {
779 556 : fprintf (file, "%s SSA_NAME:\t", p);
780 556 : print_generic_expr (file, iv->ssa_name, TDF_SLIM);
781 556 : fprintf (file, "\n");
782 : }
783 :
784 1603 : fprintf (file, "%s Type:\t", p);
785 1603 : print_generic_expr (file, TREE_TYPE (iv->base), TDF_SLIM);
786 1603 : fprintf (file, "\n");
787 :
788 1603 : fprintf (file, "%s Base:\t", p);
789 1603 : print_generic_expr (file, iv->base, TDF_SLIM);
790 1603 : fprintf (file, "\n");
791 :
792 1603 : fprintf (file, "%s Step:\t", p);
793 1603 : print_generic_expr (file, iv->step, TDF_SLIM);
794 1603 : fprintf (file, "\n");
795 :
796 1603 : if (iv->base_object)
797 : {
798 501 : fprintf (file, "%s Object:\t", p);
799 501 : print_generic_expr (file, iv->base_object, TDF_SLIM);
800 501 : fprintf (file, "\n");
801 : }
802 :
803 2899 : fprintf (file, "%s Biv:\t%c\n", p, iv->biv_p ? 'Y' : 'N');
804 :
805 1603 : fprintf (file, "%s Overflowness wrto loop niter:\t%s\n",
806 1603 : p, iv->no_overflow ? "No-overflow" : "Overflow");
807 1603 : }
808 :
809 : /* Dumps information about the USE to FILE. */
810 :
811 : void
812 250 : dump_use (FILE *file, struct iv_use *use)
813 : {
814 250 : fprintf (file, " Use %d.%d:\n", use->group_id, use->id);
815 250 : fprintf (file, " At stmt:\t");
816 250 : print_gimple_stmt (file, use->stmt, 0);
817 250 : fprintf (file, " At pos:\t");
818 250 : if (use->op_p)
819 160 : print_generic_expr (file, *use->op_p, TDF_SLIM);
820 250 : fprintf (file, "\n");
821 250 : dump_iv (file, use->iv, false, 2);
822 250 : }
823 :
824 : /* Dumps information about the uses to FILE. */
825 :
826 : void
827 67 : dump_groups (FILE *file, struct ivopts_data *data)
828 : {
829 67 : unsigned i, j;
830 67 : struct iv_group *group;
831 :
832 287 : for (i = 0; i < data->vgroups.length (); i++)
833 : {
834 220 : group = data->vgroups[i];
835 220 : fprintf (file, "Group %d:\n", group->id);
836 220 : if (group->type == USE_NONLINEAR_EXPR)
837 90 : fprintf (file, " Type:\tGENERIC\n");
838 130 : else if (group->type == USE_REF_ADDRESS)
839 56 : fprintf (file, " Type:\tREFERENCE ADDRESS\n");
840 74 : else if (group->type == USE_PTR_ADDRESS)
841 0 : fprintf (file, " Type:\tPOINTER ARGUMENT ADDRESS\n");
842 : else
843 : {
844 74 : gcc_assert (group->type == USE_COMPARE);
845 74 : fprintf (file, " Type:\tCOMPARE\n");
846 : }
847 470 : for (j = 0; j < group->vuses.length (); j++)
848 250 : dump_use (file, group->vuses[j]);
849 : }
850 67 : }
851 :
852 : /* Dumps information about induction variable candidate CAND to FILE. */
853 :
854 : void
855 797 : dump_cand (FILE *file, struct iv_cand *cand)
856 : {
857 797 : struct iv *iv = cand->iv;
858 :
859 797 : fprintf (file, "Candidate %d:\n", cand->id);
860 797 : if (cand->inv_vars)
861 : {
862 26 : fprintf (file, " Depend on inv.vars: ");
863 26 : dump_bitmap (file, cand->inv_vars);
864 : }
865 797 : if (cand->inv_exprs)
866 : {
867 0 : fprintf (file, " Depend on inv.exprs: ");
868 0 : dump_bitmap (file, cand->inv_exprs);
869 : }
870 :
871 797 : if (cand->var_before)
872 : {
873 687 : fprintf (file, " Var before: ");
874 687 : print_generic_expr (file, cand->var_before, TDF_SLIM);
875 687 : fprintf (file, "\n");
876 : }
877 797 : if (cand->var_after)
878 : {
879 687 : fprintf (file, " Var after: ");
880 687 : print_generic_expr (file, cand->var_after, TDF_SLIM);
881 687 : fprintf (file, "\n");
882 : }
883 :
884 797 : switch (cand->pos)
885 : {
886 653 : case IP_NORMAL:
887 653 : fprintf (file, " Incr POS: before exit test\n");
888 653 : break;
889 :
890 0 : case IP_BEFORE_USE:
891 0 : fprintf (file, " Incr POS: before use %d\n", cand->ainc_use->id);
892 0 : break;
893 :
894 0 : case IP_AFTER_USE:
895 0 : fprintf (file, " Incr POS: after use %d\n", cand->ainc_use->id);
896 0 : break;
897 :
898 0 : case IP_END:
899 0 : fprintf (file, " Incr POS: at end\n");
900 0 : break;
901 :
902 144 : case IP_ORIGINAL:
903 144 : fprintf (file, " Incr POS: orig biv\n");
904 144 : break;
905 : }
906 :
907 797 : dump_iv (file, iv, false, 1);
908 797 : }
909 :
910 : /* Returns the info for ssa version VER. */
911 :
912 : static inline struct version_info *
913 118257258 : ver_info (struct ivopts_data *data, unsigned ver)
914 : {
915 118257258 : return data->version_info + ver;
916 : }
917 :
918 : /* Returns the info for ssa name NAME. */
919 :
920 : static inline struct version_info *
921 95719097 : name_info (struct ivopts_data *data, tree name)
922 : {
923 95719097 : return ver_info (data, SSA_NAME_VERSION (name));
924 : }
925 :
926 : /* Returns true if STMT is after the place where the IP_NORMAL ivs will be
927 : emitted in LOOP. */
928 :
929 : static bool
930 33667231 : stmt_after_ip_normal_pos (class loop *loop, gimple *stmt)
931 : {
932 33667231 : basic_block bb = ip_normal_pos (loop), sbb = gimple_bb (stmt);
933 :
934 33667231 : gcc_assert (bb);
935 :
936 33667231 : if (sbb == loop->latch)
937 : return true;
938 :
939 33552749 : if (sbb != bb)
940 : return false;
941 :
942 19816755 : return stmt == last_nondebug_stmt (bb);
943 : }
944 :
945 : /* Returns true if STMT if after the place where the original induction
946 : variable CAND is incremented. If TRUE_IF_EQUAL is set, we return true
947 : if the positions are identical. */
948 :
949 : static bool
950 7937338 : stmt_after_inc_pos (struct iv_cand *cand, gimple *stmt, bool true_if_equal)
951 : {
952 7937338 : basic_block cand_bb = gimple_bb (cand->incremented_at);
953 7937338 : basic_block stmt_bb = gimple_bb (stmt);
954 :
955 7937338 : if (!dominated_by_p (CDI_DOMINATORS, stmt_bb, cand_bb))
956 : return false;
957 :
958 5454891 : if (stmt_bb != cand_bb)
959 : return true;
960 :
961 5218931 : if (true_if_equal
962 5218931 : && gimple_uid (stmt) == gimple_uid (cand->incremented_at))
963 : return true;
964 5212163 : return gimple_uid (stmt) > gimple_uid (cand->incremented_at);
965 : }
966 :
967 : /* Returns true if STMT if after the place where the induction variable
968 : CAND is incremented in LOOP. */
969 :
970 : static bool
971 42796933 : stmt_after_increment (class loop *loop, struct iv_cand *cand, gimple *stmt)
972 : {
973 42796933 : switch (cand->pos)
974 : {
975 : case IP_END:
976 : return false;
977 :
978 33667231 : case IP_NORMAL:
979 33667231 : return stmt_after_ip_normal_pos (loop, stmt);
980 :
981 7927069 : case IP_ORIGINAL:
982 7927069 : case IP_AFTER_USE:
983 7927069 : return stmt_after_inc_pos (cand, stmt, false);
984 :
985 10269 : case IP_BEFORE_USE:
986 10269 : return stmt_after_inc_pos (cand, stmt, true);
987 :
988 0 : default:
989 0 : gcc_unreachable ();
990 : }
991 : }
992 :
993 : /* walk_tree callback for contains_abnormal_ssa_name_p. */
994 :
995 : static tree
996 14939361 : contains_abnormal_ssa_name_p_1 (tree *tp, int *walk_subtrees, void *)
997 : {
998 14939361 : if (TREE_CODE (*tp) == SSA_NAME
999 14939361 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (*tp))
1000 : return *tp;
1001 :
1002 14939345 : if (!EXPR_P (*tp))
1003 10217478 : *walk_subtrees = 0;
1004 :
1005 : return NULL_TREE;
1006 : }
1007 :
1008 : /* Returns true if EXPR contains a ssa name that occurs in an
1009 : abnormal phi node. */
1010 :
1011 : bool
1012 8025565 : contains_abnormal_ssa_name_p (tree expr)
1013 : {
1014 8025565 : return walk_tree_without_duplicates
1015 8025565 : (&expr, contains_abnormal_ssa_name_p_1, NULL) != NULL_TREE;
1016 : }
1017 :
1018 : /* Returns the structure describing number of iterations determined from
1019 : EXIT of DATA->current_loop, or NULL if something goes wrong. */
1020 :
1021 : static class tree_niter_desc *
1022 4403157 : niter_for_exit (struct ivopts_data *data, edge exit)
1023 : {
1024 4403157 : class tree_niter_desc *desc;
1025 4403157 : tree_niter_desc **slot;
1026 :
1027 4403157 : if (!data->niters)
1028 : {
1029 477030 : data->niters = new hash_map<edge, tree_niter_desc *>;
1030 477030 : slot = NULL;
1031 : }
1032 : else
1033 3926127 : slot = data->niters->get (exit);
1034 :
1035 4403157 : if (!slot)
1036 : {
1037 : /* Try to determine number of iterations. We cannot safely work with ssa
1038 : names that appear in phi nodes on abnormal edges, so that we do not
1039 : create overlapping life ranges for them (PR 27283). */
1040 489139 : desc = XNEW (class tree_niter_desc);
1041 489139 : ::new (static_cast<void*> (desc)) tree_niter_desc ();
1042 489139 : if (!number_of_iterations_exit (data->current_loop,
1043 : exit, desc, true)
1044 489139 : || contains_abnormal_ssa_name_p (desc->niter))
1045 : {
1046 41095 : desc->~tree_niter_desc ();
1047 41095 : XDELETE (desc);
1048 41095 : desc = NULL;
1049 : }
1050 489139 : data->niters->put (exit, desc);
1051 : }
1052 : else
1053 3914018 : desc = *slot;
1054 :
1055 4403157 : return desc;
1056 : }
1057 :
1058 : /* Returns the structure describing number of iterations determined from
1059 : single dominating exit of DATA->current_loop, or NULL if something
1060 : goes wrong. */
1061 :
1062 : static class tree_niter_desc *
1063 67 : niter_for_single_dom_exit (struct ivopts_data *data)
1064 : {
1065 67 : edge exit = single_dom_exit (data->current_loop);
1066 :
1067 67 : if (!exit)
1068 : return NULL;
1069 :
1070 57 : return niter_for_exit (data, exit);
1071 : }
1072 :
1073 : /* Initializes data structures used by the iv optimization pass, stored
1074 : in DATA. */
1075 :
1076 : static void
1077 245627 : tree_ssa_iv_optimize_init (struct ivopts_data *data)
1078 : {
1079 245627 : data->version_info_size = 2 * num_ssa_names;
1080 245627 : data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
1081 245627 : data->relevant = BITMAP_ALLOC (NULL);
1082 245627 : data->important_candidates = BITMAP_ALLOC (NULL);
1083 245627 : data->max_inv_var_id = 0;
1084 245627 : data->max_inv_expr_id = 0;
1085 245627 : data->niters = NULL;
1086 245627 : data->vgroups.create (20);
1087 245627 : data->vcands.create (20);
1088 245627 : data->inv_expr_tab = new hash_table<iv_inv_expr_hasher> (10);
1089 245627 : data->name_expansion_cache = NULL;
1090 245627 : data->base_object_map = NULL;
1091 245627 : data->iv_common_cand_tab = new hash_table<iv_common_cand_hasher> (10);
1092 245627 : data->iv_common_cands.create (20);
1093 245627 : decl_rtl_to_reset.create (20);
1094 245627 : gcc_obstack_init (&data->iv_obstack);
1095 245627 : }
1096 :
1097 : /* walk_tree callback for determine_base_object. */
1098 :
1099 : static tree
1100 19545406 : determine_base_object_1 (tree *tp, int *walk_subtrees, void *wdata)
1101 : {
1102 19545406 : tree_code code = TREE_CODE (*tp);
1103 19545406 : tree obj = NULL_TREE;
1104 19545406 : if (code == ADDR_EXPR)
1105 : {
1106 1049381 : tree base = get_base_address (TREE_OPERAND (*tp, 0));
1107 1049381 : if (!base)
1108 0 : obj = *tp;
1109 1049381 : else if (TREE_CODE (base) != MEM_REF)
1110 1049353 : obj = fold_convert (ptr_type_node, build_fold_addr_expr (base));
1111 : }
1112 18496025 : else if (code == SSA_NAME && POINTER_TYPE_P (TREE_TYPE (*tp)))
1113 1939783 : obj = fold_convert (ptr_type_node, *tp);
1114 :
1115 2989136 : if (!obj)
1116 : {
1117 16556270 : if (!EXPR_P (*tp))
1118 7236017 : *walk_subtrees = 0;
1119 :
1120 : return NULL_TREE;
1121 : }
1122 : /* Record special node for multiple base objects and stop. */
1123 2989136 : if (*static_cast<tree *> (wdata))
1124 : {
1125 4310 : *static_cast<tree *> (wdata) = integer_zero_node;
1126 4310 : return integer_zero_node;
1127 : }
1128 : /* Record the base object and continue looking. */
1129 2984826 : *static_cast<tree *> (wdata) = obj;
1130 2984826 : return NULL_TREE;
1131 : }
1132 :
1133 : /* Returns a memory object to that EXPR points with caching. Return NULL if we
1134 : are able to determine that it does not point to any such object; specially
1135 : return integer_zero_node if EXPR contains multiple base objects. */
1136 :
1137 : static tree
1138 10497354 : determine_base_object (struct ivopts_data *data, tree expr)
1139 : {
1140 10497354 : tree *slot, obj = NULL_TREE;
1141 10497354 : if (data->base_object_map)
1142 : {
1143 10331521 : if ((slot = data->base_object_map->get(expr)) != NULL)
1144 4776335 : return *slot;
1145 : }
1146 : else
1147 165833 : data->base_object_map = new hash_map<tree, tree>;
1148 :
1149 5721019 : (void) walk_tree_without_duplicates (&expr, determine_base_object_1, &obj);
1150 5721019 : data->base_object_map->put (expr, obj);
1151 5721019 : return obj;
1152 : }
1153 :
1154 : /* Allocates an induction variable with given initial value BASE and step STEP
1155 : for loop LOOP. NO_OVERFLOW implies the iv doesn't overflow. */
1156 :
1157 : static struct iv *
1158 10497354 : alloc_iv (struct ivopts_data *data, tree base, tree step,
1159 : bool no_overflow = false)
1160 : {
1161 10497354 : tree expr = base;
1162 10497354 : struct iv *iv = (struct iv*) obstack_alloc (&data->iv_obstack,
1163 : sizeof (struct iv));
1164 10497354 : gcc_assert (step != NULL_TREE);
1165 :
1166 : /* Canonicalize the address expression in base if it were an unsigned
1167 : computation. That leads to more equalities being detected and results in:
1168 :
1169 : 1) More accurate cost can be computed for address expressions;
1170 : 2) Duplicate candidates won't be created for bases in different
1171 : forms, like &a[0] and &a.
1172 : 3) Duplicate candidates won't be created for IV expressions that differ
1173 : only in their sign. */
1174 10497354 : aff_tree comb;
1175 10497354 : STRIP_NOPS (expr);
1176 10497354 : expr = fold_convert (unsigned_type_for (TREE_TYPE (expr)), expr);
1177 10497354 : tree_to_aff_combination (expr, TREE_TYPE (expr), &comb);
1178 10497354 : base = fold_convert (TREE_TYPE (base), aff_combination_to_tree (&comb));
1179 :
1180 10497354 : iv->base = base;
1181 10497354 : iv->base_object = determine_base_object (data, base);
1182 10497354 : iv->step = step;
1183 10497354 : iv->biv_p = false;
1184 10497354 : iv->nonlin_use = NULL;
1185 10497354 : iv->ssa_name = NULL_TREE;
1186 10497354 : if (!no_overflow
1187 10497354 : && !iv_can_overflow_p (data->current_loop, TREE_TYPE (base),
1188 : base, step))
1189 : no_overflow = true;
1190 10497354 : iv->no_overflow = no_overflow;
1191 10497354 : iv->have_address_use = false;
1192 :
1193 20994708 : return iv;
1194 10497354 : }
1195 :
1196 : /* Sets STEP and BASE for induction variable IV. NO_OVERFLOW implies the IV
1197 : doesn't overflow. */
1198 :
1199 : static void
1200 4969650 : set_iv (struct ivopts_data *data, tree iv, tree base, tree step,
1201 : bool no_overflow)
1202 : {
1203 4969650 : struct version_info *info = name_info (data, iv);
1204 :
1205 4969650 : gcc_assert (!info->iv);
1206 :
1207 4969650 : bitmap_set_bit (data->relevant, SSA_NAME_VERSION (iv));
1208 4969650 : info->iv = alloc_iv (data, base, step, no_overflow);
1209 4969650 : info->iv->ssa_name = iv;
1210 4969650 : }
1211 :
1212 : /* Finds induction variable declaration for VAR. */
1213 :
1214 : static struct iv *
1215 44936543 : get_iv (struct ivopts_data *data, tree var)
1216 : {
1217 44936543 : basic_block bb;
1218 44936543 : tree type = TREE_TYPE (var);
1219 :
1220 44936543 : if (!POINTER_TYPE_P (type)
1221 35549850 : && !INTEGRAL_TYPE_P (type))
1222 : return NULL;
1223 :
1224 39160109 : if (!name_info (data, var)->iv)
1225 : {
1226 18163970 : bb = gimple_bb (SSA_NAME_DEF_STMT (var));
1227 :
1228 18163970 : if (!bb
1229 18163970 : || !flow_bb_inside_loop_p (data->current_loop, bb))
1230 : {
1231 813807 : if (POINTER_TYPE_P (type))
1232 326892 : type = sizetype;
1233 813807 : set_iv (data, var, var, build_int_cst (type, 0), true);
1234 : }
1235 : }
1236 :
1237 39160109 : return name_info (data, var)->iv;
1238 : }
1239 :
1240 : /* Return the first non-invariant ssa var found in EXPR. */
1241 :
1242 : static tree
1243 4133237 : extract_single_var_from_expr (tree expr)
1244 : {
1245 4133237 : int i, n;
1246 4133237 : tree tmp;
1247 4133237 : enum tree_code code;
1248 :
1249 4133237 : if (!expr || is_gimple_min_invariant (expr))
1250 : return NULL;
1251 :
1252 672528 : code = TREE_CODE (expr);
1253 672528 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1254 : {
1255 363824 : n = TREE_OPERAND_LENGTH (expr);
1256 727699 : for (i = 0; i < n; i++)
1257 : {
1258 363875 : tmp = extract_single_var_from_expr (TREE_OPERAND (expr, i));
1259 :
1260 363875 : if (tmp)
1261 : return tmp;
1262 : }
1263 : }
1264 308704 : return (TREE_CODE (expr) == SSA_NAME) ? expr : NULL;
1265 : }
1266 :
1267 : /* Finds basic ivs. */
1268 :
1269 : static bool
1270 635843 : find_bivs (struct ivopts_data *data)
1271 : {
1272 635843 : gphi *phi;
1273 635843 : affine_iv iv;
1274 635843 : tree step, type, base, stop;
1275 635843 : bool found = false;
1276 635843 : class loop *loop = data->current_loop;
1277 635843 : gphi_iterator psi;
1278 :
1279 2373304 : for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
1280 : {
1281 1737461 : phi = psi.phi ();
1282 :
1283 1737461 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (phi)))
1284 248 : continue;
1285 :
1286 1737213 : if (virtual_operand_p (PHI_RESULT (phi)))
1287 416647 : continue;
1288 :
1289 1320566 : if (!simple_iv (loop, loop, PHI_RESULT (phi), &iv, true))
1290 441594 : continue;
1291 :
1292 878972 : if (integer_zerop (iv.step))
1293 0 : continue;
1294 :
1295 878972 : step = iv.step;
1296 878972 : base = PHI_ARG_DEF_FROM_EDGE (phi, loop_preheader_edge (loop));
1297 : /* Stop expanding iv base at the first ssa var referred by iv step.
1298 : Ideally we should stop at any ssa var, because that's expensive
1299 : and unusual to happen, we just do it on the first one.
1300 :
1301 : See PR64705 for the rationale. */
1302 878972 : stop = extract_single_var_from_expr (step);
1303 878972 : base = expand_simple_operations (base, stop);
1304 878972 : if (contains_abnormal_ssa_name_p (base)
1305 878972 : || contains_abnormal_ssa_name_p (step))
1306 11 : continue;
1307 :
1308 878961 : type = TREE_TYPE (PHI_RESULT (phi));
1309 878961 : base = fold_convert (type, base);
1310 878961 : if (step)
1311 : {
1312 878961 : if (POINTER_TYPE_P (type))
1313 166083 : step = convert_to_ptrofftype (step);
1314 : else
1315 712878 : step = fold_convert (type, step);
1316 : }
1317 :
1318 878961 : set_iv (data, PHI_RESULT (phi), base, step, iv.no_overflow);
1319 878961 : found = true;
1320 : }
1321 :
1322 635843 : return found;
1323 : }
1324 :
1325 : /* Marks basic ivs. */
1326 :
1327 : static void
1328 507706 : mark_bivs (struct ivopts_data *data)
1329 : {
1330 507706 : gphi *phi;
1331 507706 : gimple *def;
1332 507706 : tree var;
1333 507706 : struct iv *iv, *incr_iv;
1334 507706 : class loop *loop = data->current_loop;
1335 507706 : basic_block incr_bb;
1336 507706 : gphi_iterator psi;
1337 :
1338 507706 : data->bivs_not_used_in_addr = 0;
1339 1975279 : for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
1340 : {
1341 1467573 : phi = psi.phi ();
1342 :
1343 1467573 : iv = get_iv (data, PHI_RESULT (phi));
1344 1467573 : if (!iv)
1345 588612 : continue;
1346 :
1347 878961 : var = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (loop));
1348 878961 : def = SSA_NAME_DEF_STMT (var);
1349 : /* Don't mark iv peeled from other one as biv. */
1350 879084 : if (def
1351 878961 : && gimple_code (def) == GIMPLE_PHI
1352 880316 : && gimple_bb (def) == loop->header)
1353 123 : continue;
1354 :
1355 878838 : incr_iv = get_iv (data, var);
1356 878838 : if (!incr_iv)
1357 1243 : continue;
1358 :
1359 : /* If the increment is in the subloop, ignore it. */
1360 877595 : incr_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
1361 877595 : if (incr_bb->loop_father != data->current_loop
1362 877595 : || (incr_bb->flags & BB_IRREDUCIBLE_LOOP))
1363 0 : continue;
1364 :
1365 877595 : iv->biv_p = true;
1366 877595 : incr_iv->biv_p = true;
1367 877595 : if (iv->no_overflow)
1368 584900 : data->bivs_not_used_in_addr++;
1369 877595 : if (incr_iv->no_overflow)
1370 576656 : data->bivs_not_used_in_addr++;
1371 : }
1372 507706 : }
1373 :
1374 : /* Checks whether STMT defines a linear induction variable and stores its
1375 : parameters to IV. */
1376 :
1377 : static bool
1378 12732037 : find_givs_in_stmt_scev (struct ivopts_data *data, gimple *stmt, affine_iv *iv)
1379 : {
1380 12732037 : tree lhs, stop;
1381 12732037 : class loop *loop = data->current_loop;
1382 :
1383 12732037 : iv->base = NULL_TREE;
1384 12732037 : iv->step = NULL_TREE;
1385 :
1386 12732037 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1387 : return false;
1388 :
1389 10653663 : lhs = gimple_assign_lhs (stmt);
1390 10653663 : if (TREE_CODE (lhs) != SSA_NAME)
1391 : return false;
1392 :
1393 19052666 : if (!simple_iv (loop, loop_containing_stmt (stmt), lhs, iv, true))
1394 : return false;
1395 :
1396 : /* Stop expanding iv base at the first ssa var referred by iv step.
1397 : Ideally we should stop at any ssa var, because that's expensive
1398 : and unusual to happen, we just do it on the first one.
1399 :
1400 : See PR64705 for the rationale. */
1401 2890390 : stop = extract_single_var_from_expr (iv->step);
1402 2890390 : iv->base = expand_simple_operations (iv->base, stop);
1403 2890390 : if (contains_abnormal_ssa_name_p (iv->base)
1404 2890390 : || contains_abnormal_ssa_name_p (iv->step))
1405 : return false;
1406 :
1407 : /* If STMT could throw, then do not consider STMT as defining a GIV.
1408 : While this will suppress optimizations, we cannot safely delete this
1409 : GIV and associated statements, even if it appears it is not used. */
1410 2890386 : if (stmt_could_throw_p (cfun, stmt))
1411 8 : return false;
1412 :
1413 : return true;
1414 : }
1415 :
1416 : /* Finds general ivs in statement STMT. */
1417 :
1418 : static void
1419 12732037 : find_givs_in_stmt (struct ivopts_data *data, gimple *stmt)
1420 : {
1421 12732037 : affine_iv iv;
1422 :
1423 12732037 : if (!find_givs_in_stmt_scev (data, stmt, &iv))
1424 : return;
1425 :
1426 2890378 : set_iv (data, gimple_assign_lhs (stmt), iv.base, iv.step, iv.no_overflow);
1427 : }
1428 :
1429 : /* Finds general ivs in basic block BB. */
1430 :
1431 : static void
1432 2856478 : find_givs_in_bb (struct ivopts_data *data, basic_block bb)
1433 : {
1434 2856478 : gimple_stmt_iterator bsi;
1435 :
1436 28544816 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1437 22831860 : if (!is_gimple_debug (gsi_stmt (bsi)))
1438 12732037 : find_givs_in_stmt (data, gsi_stmt (bsi));
1439 2856478 : }
1440 :
1441 : /* Finds general ivs. */
1442 :
1443 : static void
1444 507706 : find_givs (struct ivopts_data *data, basic_block *body)
1445 : {
1446 507706 : class loop *loop = data->current_loop;
1447 507706 : unsigned i;
1448 :
1449 3364184 : for (i = 0; i < loop->num_nodes; i++)
1450 2856478 : find_givs_in_bb (data, body[i]);
1451 507706 : }
1452 :
1453 : /* For each ssa name defined in LOOP determines whether it is an induction
1454 : variable and if so, its initial value and step. */
1455 :
1456 : static bool
1457 635843 : find_induction_variables (struct ivopts_data *data, basic_block *body)
1458 : {
1459 635843 : unsigned i;
1460 635843 : bitmap_iterator bi;
1461 :
1462 635843 : if (!find_bivs (data))
1463 : return false;
1464 :
1465 507706 : find_givs (data, body);
1466 507706 : mark_bivs (data);
1467 :
1468 507706 : if (dump_file && (dump_flags & TDF_DETAILS))
1469 : {
1470 67 : class tree_niter_desc *niter = niter_for_single_dom_exit (data);
1471 :
1472 67 : if (niter)
1473 : {
1474 51 : fprintf (dump_file, " number of iterations ");
1475 51 : print_generic_expr (dump_file, niter->niter, TDF_SLIM);
1476 51 : if (!integer_zerop (niter->may_be_zero))
1477 : {
1478 1 : fprintf (dump_file, "; zero if ");
1479 1 : print_generic_expr (dump_file, niter->may_be_zero, TDF_SLIM);
1480 : }
1481 51 : fprintf (dump_file, "\n");
1482 67 : };
1483 :
1484 67 : fprintf (dump_file, "\n<Induction Vars>:\n");
1485 858 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1486 : {
1487 791 : struct version_info *info = ver_info (data, i);
1488 791 : if (info->iv && info->iv->step && !integer_zerop (info->iv->step))
1489 556 : dump_iv (dump_file, ver_info (data, i)->iv, true, 0);
1490 : }
1491 : }
1492 :
1493 : return true;
1494 : }
1495 :
1496 : /* Records a use of TYPE at *USE_P in STMT whose value is IV in GROUP.
1497 : For address type use, ADDR_BASE is the stripped IV base, ADDR_OFFSET
1498 : is the const offset stripped from IV base and MEM_TYPE is the type
1499 : of the memory being addressed. For uses of other types, ADDR_BASE
1500 : and ADDR_OFFSET are zero by default and MEM_TYPE is NULL_TREE. */
1501 :
1502 : static struct iv_use *
1503 2097455 : record_use (struct iv_group *group, tree *use_p, struct iv *iv,
1504 : gimple *stmt, enum use_type type, tree mem_type,
1505 : tree addr_base, poly_uint64 addr_offset)
1506 : {
1507 2097455 : struct iv_use *use = XCNEW (struct iv_use);
1508 :
1509 2097455 : use->id = group->vuses.length ();
1510 2097455 : use->group_id = group->id;
1511 2097455 : use->type = type;
1512 2097455 : use->mem_type = mem_type;
1513 2097455 : use->iv = iv;
1514 2097455 : use->stmt = stmt;
1515 2097455 : use->op_p = use_p;
1516 2097455 : use->addr_base = addr_base;
1517 2097455 : use->addr_offset = addr_offset;
1518 :
1519 2097455 : group->vuses.safe_push (use);
1520 2097455 : return use;
1521 : }
1522 :
1523 : /* Checks whether OP is a loop-level invariant and if so, records it.
1524 : NONLINEAR_USE is true if the invariant is used in a way we do not
1525 : handle specially. */
1526 :
1527 : static void
1528 22940197 : record_invariant (struct ivopts_data *data, tree op, bool nonlinear_use)
1529 : {
1530 22940197 : basic_block bb;
1531 22940197 : struct version_info *info;
1532 :
1533 22940197 : if (TREE_CODE (op) != SSA_NAME
1534 22940197 : || virtual_operand_p (op))
1535 : return;
1536 :
1537 21744233 : bb = gimple_bb (SSA_NAME_DEF_STMT (op));
1538 21744233 : if (bb
1539 21744233 : && flow_bb_inside_loop_p (data->current_loop, bb))
1540 : return;
1541 :
1542 3935451 : info = name_info (data, op);
1543 3935451 : info->name = op;
1544 3935451 : info->has_nonlin_use |= nonlinear_use;
1545 3935451 : if (!info->inv_id)
1546 1345464 : info->inv_id = ++data->max_inv_var_id;
1547 3935451 : bitmap_set_bit (data->relevant, SSA_NAME_VERSION (op));
1548 : }
1549 :
1550 : /* Record a group of TYPE. */
1551 :
1552 : static struct iv_group *
1553 1815225 : record_group (struct ivopts_data *data, enum use_type type)
1554 : {
1555 1815225 : struct iv_group *group = XCNEW (struct iv_group);
1556 :
1557 1815225 : group->id = data->vgroups.length ();
1558 1815225 : group->type = type;
1559 1815225 : group->related_cands = BITMAP_ALLOC (NULL);
1560 1815225 : group->vuses.create (1);
1561 1815225 : group->doloop_p = false;
1562 :
1563 1815225 : data->vgroups.safe_push (group);
1564 1815225 : return group;
1565 : }
1566 :
1567 : /* Record a use of TYPE at *USE_P in STMT whose value is IV in a group.
1568 : New group will be created if there is no existing group for the use.
1569 : MEM_TYPE is the type of memory being addressed, or NULL if this
1570 : isn't an address reference. */
1571 :
1572 : static struct iv_use *
1573 2097455 : record_group_use (struct ivopts_data *data, tree *use_p,
1574 : struct iv *iv, gimple *stmt, enum use_type type,
1575 : tree mem_type)
1576 : {
1577 2097455 : tree addr_base = NULL;
1578 2097455 : struct iv_group *group = NULL;
1579 2097455 : poly_uint64 addr_offset = 0;
1580 :
1581 : /* Record non address type use in a new group. */
1582 2097455 : if (address_p (type))
1583 : {
1584 866051 : unsigned int i;
1585 :
1586 866051 : gcc_assert (POINTER_TYPE_P (TREE_TYPE (iv->base)));
1587 866051 : tree addr_toffset;
1588 866051 : split_constant_offset (iv->base, &addr_base, &addr_toffset);
1589 866051 : addr_offset = int_cst_value (addr_toffset);
1590 1621424 : for (i = 0; i < data->vgroups.length (); i++)
1591 : {
1592 1089248 : struct iv_use *use;
1593 :
1594 1089248 : group = data->vgroups[i];
1595 1089248 : use = group->vuses[0];
1596 1089248 : if (!address_p (use->type))
1597 332111 : continue;
1598 :
1599 : /* Check if it has the same stripped base and step. */
1600 757137 : if (operand_equal_p (iv->base_object, use->iv->base_object, 0)
1601 401455 : && operand_equal_p (iv->step, use->iv->step, OEP_ASSUME_WRAPV)
1602 1155483 : && operand_equal_p (addr_base, use->addr_base, OEP_ASSUME_WRAPV))
1603 : break;
1604 : }
1605 1732102 : if (i == data->vgroups.length ())
1606 532176 : group = NULL;
1607 : }
1608 :
1609 866051 : if (!group)
1610 1763580 : group = record_group (data, type);
1611 :
1612 2097455 : return record_use (group, use_p, iv, stmt, type, mem_type,
1613 2097455 : addr_base, addr_offset);
1614 : }
1615 :
1616 : /* Checks whether the use OP is interesting and if so, records it. */
1617 :
1618 : static struct iv_use *
1619 7398795 : find_interesting_uses_op (struct ivopts_data *data, tree op)
1620 : {
1621 7398795 : struct iv *iv;
1622 7398795 : gimple *stmt;
1623 7398795 : struct iv_use *use;
1624 :
1625 7398795 : if (TREE_CODE (op) != SSA_NAME)
1626 : return NULL;
1627 :
1628 5947905 : iv = get_iv (data, op);
1629 5947905 : if (!iv)
1630 : return NULL;
1631 :
1632 2610906 : if (iv->nonlin_use)
1633 : {
1634 210011 : gcc_assert (iv->nonlin_use->type == USE_NONLINEAR_EXPR);
1635 : return iv->nonlin_use;
1636 : }
1637 :
1638 2400895 : if (integer_zerop (iv->step))
1639 : {
1640 1774508 : record_invariant (data, op, true);
1641 1774508 : return NULL;
1642 : }
1643 :
1644 626387 : stmt = SSA_NAME_DEF_STMT (op);
1645 626387 : gcc_assert (gimple_code (stmt) == GIMPLE_PHI || is_gimple_assign (stmt));
1646 :
1647 626387 : use = record_group_use (data, NULL, iv, stmt, USE_NONLINEAR_EXPR, NULL_TREE);
1648 626387 : iv->nonlin_use = use;
1649 626387 : return use;
1650 : }
1651 :
1652 : /* Indicate how compare type iv_use can be handled. */
1653 : enum comp_iv_rewrite
1654 : {
1655 : COMP_IV_NA,
1656 : /* We may rewrite compare type iv_use by expressing value of the iv_use. */
1657 : COMP_IV_EXPR,
1658 : /* We may rewrite compare type iv_uses on both sides of comparison by
1659 : expressing value of each iv_use. */
1660 : COMP_IV_EXPR_2,
1661 : /* We may rewrite compare type iv_use by expressing value of the iv_use
1662 : or by eliminating it with other iv_cand. */
1663 : COMP_IV_ELIM
1664 : };
1665 :
1666 : /* Given a condition in statement STMT, checks whether it is a compare
1667 : of an induction variable and an invariant. If this is the case,
1668 : CONTROL_VAR is set to location of the iv, BOUND to the location of
1669 : the invariant, IV_VAR and IV_BOUND are set to the corresponding
1670 : induction variable descriptions, and true is returned. If this is not
1671 : the case, CONTROL_VAR and BOUND are set to the arguments of the
1672 : condition and false is returned. */
1673 :
1674 : static enum comp_iv_rewrite
1675 7516180 : extract_cond_operands (struct ivopts_data *data, gimple *stmt,
1676 : tree **control_var, tree **bound,
1677 : struct iv **iv_var, struct iv **iv_bound)
1678 : {
1679 : /* The objects returned when COND has constant operands. */
1680 7516180 : static struct iv const_iv;
1681 7516180 : static tree zero;
1682 7516180 : tree *op0 = &zero, *op1 = &zero;
1683 7516180 : struct iv *iv0 = &const_iv, *iv1 = &const_iv;
1684 7516180 : enum comp_iv_rewrite rewrite_type = COMP_IV_NA;
1685 :
1686 7516180 : if (gimple_code (stmt) == GIMPLE_COND)
1687 : {
1688 7235905 : gcond *cond_stmt = as_a <gcond *> (stmt);
1689 7235905 : op0 = gimple_cond_lhs_ptr (cond_stmt);
1690 7235905 : op1 = gimple_cond_rhs_ptr (cond_stmt);
1691 : }
1692 : else
1693 : {
1694 280275 : op0 = gimple_assign_rhs1_ptr (stmt);
1695 280275 : op1 = gimple_assign_rhs2_ptr (stmt);
1696 : }
1697 :
1698 7516180 : zero = integer_zero_node;
1699 7516180 : const_iv.step = integer_zero_node;
1700 :
1701 7516180 : if (TREE_CODE (*op0) == SSA_NAME)
1702 7516023 : iv0 = get_iv (data, *op0);
1703 7516180 : if (TREE_CODE (*op1) == SSA_NAME)
1704 3364929 : iv1 = get_iv (data, *op1);
1705 :
1706 : /* If both sides of comparison are IVs. We can express ivs on both end. */
1707 7516180 : if (iv0 && iv1 && !integer_zerop (iv0->step) && !integer_zerop (iv1->step))
1708 : {
1709 88126 : rewrite_type = COMP_IV_EXPR_2;
1710 88126 : goto end;
1711 : }
1712 :
1713 : /* If none side of comparison is IV. */
1714 5792698 : if ((!iv0 || integer_zerop (iv0->step))
1715 8769718 : && (!iv1 || integer_zerop (iv1->step)))
1716 962739 : goto end;
1717 :
1718 : /* Control variable may be on the other side. */
1719 6465315 : if (!iv0 || integer_zerop (iv0->step))
1720 : {
1721 : std::swap (op0, op1);
1722 : std::swap (iv0, iv1);
1723 : }
1724 : /* If one side is IV and the other side isn't loop invariant. */
1725 6465315 : if (!iv1)
1726 : rewrite_type = COMP_IV_EXPR;
1727 : /* If one side is IV and the other side is loop invariant. */
1728 5460449 : else if (!integer_zerop (iv0->step) && integer_zerop (iv1->step))
1729 : rewrite_type = COMP_IV_ELIM;
1730 :
1731 7516180 : end:
1732 7516180 : if (control_var)
1733 7516180 : *control_var = op0;
1734 7516180 : if (iv_var)
1735 1565626 : *iv_var = iv0;
1736 7516180 : if (bound)
1737 7516180 : *bound = op1;
1738 7516180 : if (iv_bound)
1739 7516180 : *iv_bound = iv1;
1740 :
1741 7516180 : return rewrite_type;
1742 : }
1743 :
1744 : /* Checks whether the condition in STMT is interesting and if so,
1745 : records it. */
1746 :
1747 : static void
1748 1565626 : find_interesting_uses_cond (struct ivopts_data *data, gimple *stmt)
1749 : {
1750 1565626 : tree *var_p, *bound_p;
1751 1565626 : struct iv *var_iv, *bound_iv;
1752 1565626 : enum comp_iv_rewrite ret;
1753 :
1754 1565626 : ret = extract_cond_operands (data, stmt,
1755 : &var_p, &bound_p, &var_iv, &bound_iv);
1756 1565626 : if (ret == COMP_IV_NA)
1757 : {
1758 962739 : find_interesting_uses_op (data, *var_p);
1759 962739 : find_interesting_uses_op (data, *bound_p);
1760 962739 : return;
1761 : }
1762 :
1763 602887 : record_group_use (data, var_p, var_iv, stmt, USE_COMPARE, NULL_TREE);
1764 : /* Record compare type iv_use for iv on the other side of comparison. */
1765 602887 : if (ret == COMP_IV_EXPR_2)
1766 2130 : record_group_use (data, bound_p, bound_iv, stmt, USE_COMPARE, NULL_TREE);
1767 : }
1768 :
1769 : /* Returns the outermost loop EXPR is obviously invariant in
1770 : relative to the loop LOOP, i.e. if all its operands are defined
1771 : outside of the returned loop. Returns NULL if EXPR is not
1772 : even obviously invariant in LOOP. */
1773 :
1774 : class loop *
1775 374622 : outermost_invariant_loop_for_expr (class loop *loop, tree expr)
1776 : {
1777 374622 : basic_block def_bb;
1778 374622 : unsigned i, len;
1779 :
1780 374622 : if (is_gimple_min_invariant (expr))
1781 49104 : return current_loops->tree_root;
1782 :
1783 325518 : if (TREE_CODE (expr) == SSA_NAME)
1784 : {
1785 192281 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
1786 192281 : if (def_bb)
1787 : {
1788 114795 : if (flow_bb_inside_loop_p (loop, def_bb))
1789 : return NULL;
1790 229574 : return superloop_at_depth (loop,
1791 158095 : loop_depth (def_bb->loop_father) + 1);
1792 : }
1793 :
1794 77486 : return current_loops->tree_root;
1795 : }
1796 :
1797 133237 : if (!EXPR_P (expr))
1798 : return NULL;
1799 :
1800 133237 : unsigned maxdepth = 0;
1801 133237 : len = TREE_OPERAND_LENGTH (expr);
1802 347243 : for (i = 0; i < len; i++)
1803 : {
1804 214030 : class loop *ivloop;
1805 214030 : if (!TREE_OPERAND (expr, i))
1806 0 : continue;
1807 :
1808 214030 : ivloop = outermost_invariant_loop_for_expr (loop, TREE_OPERAND (expr, i));
1809 214030 : if (!ivloop)
1810 : return NULL;
1811 554523 : maxdepth = MAX (maxdepth, loop_depth (ivloop));
1812 : }
1813 :
1814 133213 : return superloop_at_depth (loop, maxdepth);
1815 : }
1816 :
1817 : /* Returns true if expression EXPR is obviously invariant in LOOP,
1818 : i.e. if all its operands are defined outside of the LOOP. LOOP
1819 : should not be the function body. */
1820 :
1821 : bool
1822 12412649 : expr_invariant_in_loop_p (class loop *loop, tree expr)
1823 : {
1824 12412649 : basic_block def_bb;
1825 12412649 : unsigned i, len;
1826 :
1827 12412649 : gcc_assert (loop_depth (loop) > 0);
1828 :
1829 12412649 : if (is_gimple_min_invariant (expr))
1830 : return true;
1831 :
1832 8575939 : if (TREE_CODE (expr) == SSA_NAME)
1833 : {
1834 8136760 : def_bb = gimple_bb (SSA_NAME_DEF_STMT (expr));
1835 8136760 : if (def_bb
1836 8136760 : && flow_bb_inside_loop_p (loop, def_bb))
1837 3910227 : return false;
1838 :
1839 : return true;
1840 : }
1841 :
1842 439179 : if (!EXPR_P (expr))
1843 : return false;
1844 :
1845 439176 : len = TREE_OPERAND_LENGTH (expr);
1846 962986 : for (i = 0; i < len; i++)
1847 569932 : if (TREE_OPERAND (expr, i)
1848 569932 : && !expr_invariant_in_loop_p (loop, TREE_OPERAND (expr, i)))
1849 : return false;
1850 :
1851 : return true;
1852 : }
1853 :
1854 : /* Given expression EXPR which computes inductive values with respect
1855 : to loop recorded in DATA, this function returns biv from which EXPR
1856 : is derived by tracing definition chains of ssa variables in EXPR. */
1857 :
1858 : static struct iv*
1859 873497 : find_deriving_biv_for_expr (struct ivopts_data *data, tree expr)
1860 : {
1861 1412935 : struct iv *iv;
1862 1412935 : unsigned i, n;
1863 1412935 : tree e2, e1;
1864 1412935 : enum tree_code code;
1865 1412935 : gimple *stmt;
1866 :
1867 1412935 : if (expr == NULL_TREE)
1868 : return NULL;
1869 :
1870 1412653 : if (is_gimple_min_invariant (expr))
1871 : return NULL;
1872 :
1873 1128323 : code = TREE_CODE (expr);
1874 1128323 : if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
1875 : {
1876 23429 : n = TREE_OPERAND_LENGTH (expr);
1877 25542 : for (i = 0; i < n; i++)
1878 : {
1879 25013 : iv = find_deriving_biv_for_expr (data, TREE_OPERAND (expr, i));
1880 25013 : if (iv)
1881 : return iv;
1882 : }
1883 : }
1884 :
1885 : /* Stop if it's not ssa name. */
1886 1105423 : if (code != SSA_NAME)
1887 : return NULL;
1888 :
1889 1104312 : iv = get_iv (data, expr);
1890 1104312 : if (!iv || integer_zerop (iv->step))
1891 : return NULL;
1892 1057376 : else if (iv->biv_p)
1893 : return iv;
1894 :
1895 786613 : stmt = SSA_NAME_DEF_STMT (expr);
1896 786613 : if (gphi *phi = dyn_cast <gphi *> (stmt))
1897 : {
1898 59 : ssa_op_iter iter;
1899 59 : use_operand_p use_p;
1900 59 : basic_block phi_bb = gimple_bb (phi);
1901 :
1902 : /* Skip loop header PHI that doesn't define biv. */
1903 59 : if (phi_bb->loop_father == data->current_loop)
1904 : return NULL;
1905 :
1906 0 : if (virtual_operand_p (gimple_phi_result (phi)))
1907 : return NULL;
1908 :
1909 0 : FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
1910 : {
1911 0 : tree use = USE_FROM_PTR (use_p);
1912 0 : iv = find_deriving_biv_for_expr (data, use);
1913 0 : if (iv)
1914 : return iv;
1915 : }
1916 : return NULL;
1917 : }
1918 786554 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
1919 : return NULL;
1920 :
1921 786554 : e1 = gimple_assign_rhs1 (stmt);
1922 786554 : code = gimple_assign_rhs_code (stmt);
1923 786554 : if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS)
1924 : return find_deriving_biv_for_expr (data, e1);
1925 :
1926 777233 : switch (code)
1927 : {
1928 576552 : case MULT_EXPR:
1929 576552 : case PLUS_EXPR:
1930 576552 : case MINUS_EXPR:
1931 576552 : case POINTER_PLUS_EXPR:
1932 : /* Increments, decrements and multiplications by a constant
1933 : are simple. */
1934 576552 : e2 = gimple_assign_rhs2 (stmt);
1935 576552 : iv = find_deriving_biv_for_expr (data, e2);
1936 576552 : if (iv)
1937 : return iv;
1938 : gcc_fallthrough ();
1939 :
1940 : CASE_CONVERT:
1941 : /* Casts are simple. */
1942 : return find_deriving_biv_for_expr (data, e1);
1943 :
1944 : default:
1945 : break;
1946 : }
1947 :
1948 : return NULL;
1949 : }
1950 :
1951 : /* Record BIV, its predecessor and successor that they are used in
1952 : address type uses. */
1953 :
1954 : static void
1955 601734 : record_biv_for_address_use (struct ivopts_data *data, struct iv *biv)
1956 : {
1957 601734 : unsigned i;
1958 601734 : tree type, base_1, base_2;
1959 601734 : bitmap_iterator bi;
1960 :
1961 600565 : if (!biv || !biv->biv_p || integer_zerop (biv->step)
1962 1202299 : || biv->have_address_use || !biv->no_overflow)
1963 335906 : return;
1964 :
1965 536898 : type = TREE_TYPE (biv->base);
1966 536898 : if (!INTEGRAL_TYPE_P (type))
1967 : return;
1968 :
1969 265828 : biv->have_address_use = true;
1970 265828 : data->bivs_not_used_in_addr--;
1971 265828 : base_1 = fold_build2 (PLUS_EXPR, type, biv->base, biv->step);
1972 2438935 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
1973 : {
1974 2173107 : struct iv *iv = ver_info (data, i)->iv;
1975 :
1976 1962163 : if (!iv || !iv->biv_p || integer_zerop (iv->step)
1977 3079839 : || iv->have_address_use || !iv->no_overflow)
1978 1880114 : continue;
1979 :
1980 292993 : if (type != TREE_TYPE (iv->base)
1981 292993 : || !INTEGRAL_TYPE_P (TREE_TYPE (iv->base)))
1982 30461 : continue;
1983 :
1984 262532 : if (!operand_equal_p (biv->step, iv->step, 0))
1985 5921 : continue;
1986 :
1987 256611 : base_2 = fold_build2 (PLUS_EXPR, type, iv->base, iv->step);
1988 256611 : if (operand_equal_p (base_1, iv->base, 0)
1989 256611 : || operand_equal_p (base_2, biv->base, 0))
1990 : {
1991 231145 : iv->have_address_use = true;
1992 231145 : data->bivs_not_used_in_addr--;
1993 : }
1994 : }
1995 : }
1996 :
1997 : /* Cumulates the steps of indices into DATA and replaces their values with the
1998 : initial ones. Returns false when the value of the index cannot be determined.
1999 : Callback for for_each_index. */
2000 :
2001 : struct ifs_ivopts_data
2002 : {
2003 : struct ivopts_data *ivopts_data;
2004 : gimple *stmt;
2005 : tree step;
2006 : };
2007 :
2008 : static bool
2009 2294204 : idx_find_step (tree base, tree *idx, void *data)
2010 : {
2011 2294204 : struct ifs_ivopts_data *dta = (struct ifs_ivopts_data *) data;
2012 2294204 : struct iv *iv;
2013 2294204 : bool use_overflow_semantics = false;
2014 2294204 : tree step, iv_base, iv_step, lbound, off;
2015 2294204 : class loop *loop = dta->ivopts_data->current_loop;
2016 :
2017 : /* If base is a component ref, require that the offset of the reference
2018 : be invariant. */
2019 2294204 : if (TREE_CODE (base) == COMPONENT_REF)
2020 : {
2021 78 : off = component_ref_field_offset (base);
2022 78 : return expr_invariant_in_loop_p (loop, off);
2023 : }
2024 :
2025 : /* If base is array, first check whether we will be able to move the
2026 : reference out of the loop (in order to take its address in strength
2027 : reduction). In order for this to work we need both lower bound
2028 : and step to be loop invariants. */
2029 2294126 : if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
2030 : {
2031 : /* Moreover, for a range, the size needs to be invariant as well. */
2032 543992 : if (TREE_CODE (base) == ARRAY_RANGE_REF
2033 543992 : && !expr_invariant_in_loop_p (loop, TYPE_SIZE (TREE_TYPE (base))))
2034 : return false;
2035 :
2036 543992 : step = array_ref_element_size (base);
2037 543992 : lbound = array_ref_low_bound (base);
2038 :
2039 543992 : if (!expr_invariant_in_loop_p (loop, step)
2040 543992 : || !expr_invariant_in_loop_p (loop, lbound))
2041 : return false;
2042 : }
2043 :
2044 2294120 : if (TREE_CODE (*idx) != SSA_NAME)
2045 : return true;
2046 :
2047 1855505 : iv = get_iv (dta->ivopts_data, *idx);
2048 1855505 : if (!iv)
2049 : return false;
2050 :
2051 : /* XXX We produce for a base of *D42 with iv->base being &x[0]
2052 : *&x[0], which is not folded and does not trigger the
2053 : ARRAY_REF path below. */
2054 1199003 : *idx = iv->base;
2055 :
2056 1199003 : if (integer_zerop (iv->step))
2057 : return true;
2058 :
2059 883060 : if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
2060 : {
2061 306119 : step = array_ref_element_size (base);
2062 :
2063 : /* We only handle addresses whose step is an integer constant. */
2064 306119 : if (TREE_CODE (step) != INTEGER_CST)
2065 : return false;
2066 : }
2067 : else
2068 : /* The step for pointer arithmetics already is 1 byte. */
2069 576941 : step = size_one_node;
2070 :
2071 882803 : iv_base = iv->base;
2072 882803 : iv_step = iv->step;
2073 882803 : if (iv->no_overflow && nowrap_type_p (TREE_TYPE (iv_step)))
2074 : use_overflow_semantics = true;
2075 :
2076 882803 : if (!convert_affine_scev (dta->ivopts_data->current_loop,
2077 : sizetype, &iv_base, &iv_step, dta->stmt,
2078 : use_overflow_semantics))
2079 : {
2080 : /* The index might wrap. */
2081 : return false;
2082 : }
2083 :
2084 879500 : step = fold_build2 (MULT_EXPR, sizetype, step, iv_step);
2085 879500 : dta->step = fold_build2 (PLUS_EXPR, sizetype, dta->step, step);
2086 :
2087 879500 : if (dta->ivopts_data->bivs_not_used_in_addr)
2088 : {
2089 601734 : if (!iv->biv_p)
2090 271932 : iv = find_deriving_biv_for_expr (dta->ivopts_data, iv->ssa_name);
2091 :
2092 601734 : record_biv_for_address_use (dta->ivopts_data, iv);
2093 : }
2094 : return true;
2095 : }
2096 :
2097 : /* Records use in index IDX. Callback for for_each_index. Ivopts data
2098 : object is passed to it in DATA. */
2099 :
2100 : static bool
2101 1869056 : idx_record_use (tree base, tree *idx,
2102 : void *vdata)
2103 : {
2104 1869056 : struct ivopts_data *data = (struct ivopts_data *) vdata;
2105 1869056 : find_interesting_uses_op (data, *idx);
2106 1869056 : if (TREE_CODE (base) == ARRAY_REF || TREE_CODE (base) == ARRAY_RANGE_REF)
2107 : {
2108 243567 : if (TREE_OPERAND (base, 2))
2109 5466 : find_interesting_uses_op (data, TREE_OPERAND (base, 2));
2110 243567 : if (TREE_OPERAND (base, 3))
2111 11482 : find_interesting_uses_op (data, TREE_OPERAND (base, 3));
2112 : }
2113 1869056 : return true;
2114 : }
2115 :
2116 : /* If we can prove that TOP = cst * BOT for some constant cst,
2117 : store cst to MUL and return true. Otherwise return false.
2118 : The returned value is always sign-extended, regardless of the
2119 : signedness of TOP and BOT. */
2120 :
2121 : static bool
2122 17321514 : constant_multiple_of (tree top, tree bot, widest_int *mul,
2123 : struct ivopts_data *data)
2124 : {
2125 34643028 : aff_tree aff_top, aff_bot;
2126 17321514 : tree_to_aff_combination_expand (top, TREE_TYPE (top), &aff_top,
2127 : &data->name_expansion_cache);
2128 17321514 : tree_to_aff_combination_expand (bot, TREE_TYPE (bot), &aff_bot,
2129 : &data->name_expansion_cache);
2130 :
2131 17321514 : poly_widest_int poly_mul;
2132 17321514 : if (aff_combination_constant_multiple_p (&aff_top, &aff_bot, &poly_mul)
2133 17321514 : && poly_mul.is_constant (mul))
2134 14355586 : return true;
2135 :
2136 : return false;
2137 17321514 : }
2138 :
2139 : /* Return true if memory reference REF with step STEP may be unaligned. */
2140 :
2141 : static bool
2142 0 : may_be_unaligned_p (tree ref, tree step)
2143 : {
2144 : /* TARGET_MEM_REFs are translated directly to valid MEMs on the target,
2145 : thus they are not misaligned. */
2146 0 : if (TREE_CODE (ref) == TARGET_MEM_REF)
2147 : return false;
2148 :
2149 0 : unsigned int align = TYPE_ALIGN (TREE_TYPE (ref));
2150 0 : if (GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref))) > align)
2151 0 : align = GET_MODE_ALIGNMENT (TYPE_MODE (TREE_TYPE (ref)));
2152 :
2153 0 : unsigned HOST_WIDE_INT bitpos;
2154 0 : unsigned int ref_align;
2155 0 : get_object_alignment_1 (ref, &ref_align, &bitpos);
2156 0 : if (ref_align < align
2157 0 : || (bitpos % align) != 0
2158 0 : || (bitpos % BITS_PER_UNIT) != 0)
2159 : return true;
2160 :
2161 0 : unsigned int trailing_zeros = tree_ctz (step);
2162 0 : if (trailing_zeros < HOST_BITS_PER_INT
2163 0 : && (1U << trailing_zeros) * BITS_PER_UNIT < align)
2164 0 : return true;
2165 :
2166 : return false;
2167 : }
2168 :
2169 : /* Return true if EXPR may be non-addressable. */
2170 :
2171 : bool
2172 13362190 : may_be_nonaddressable_p (tree expr)
2173 : {
2174 14249363 : switch (TREE_CODE (expr))
2175 : {
2176 9556042 : case VAR_DECL:
2177 : /* Check if it's a register variable. */
2178 9556042 : return DECL_HARD_REGISTER (expr);
2179 :
2180 : case TARGET_MEM_REF:
2181 : /* TARGET_MEM_REFs are translated directly to valid MEMs on the
2182 : target, thus they are always addressable. */
2183 : return false;
2184 :
2185 1966519 : case MEM_REF:
2186 : /* Likewise for MEM_REFs, modulo the storage order. */
2187 1966519 : return REF_REVERSE_STORAGE_ORDER (expr);
2188 :
2189 76 : case BIT_FIELD_REF:
2190 76 : if (REF_REVERSE_STORAGE_ORDER (expr))
2191 : return true;
2192 76 : return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2193 :
2194 1267191 : case COMPONENT_REF:
2195 1267191 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
2196 : return true;
2197 1267191 : return DECL_NONADDRESSABLE_P (TREE_OPERAND (expr, 1))
2198 1267191 : || may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2199 :
2200 864283 : case ARRAY_REF:
2201 864283 : case ARRAY_RANGE_REF:
2202 864283 : if (TYPE_REVERSE_STORAGE_ORDER (TREE_TYPE (TREE_OPERAND (expr, 0))))
2203 : return true;
2204 864283 : return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2205 :
2206 22824 : case VIEW_CONVERT_EXPR:
2207 : /* This kind of view-conversions may wrap non-addressable objects
2208 : and make them look addressable. After some processing the
2209 : non-addressability may be uncovered again, causing ADDR_EXPRs
2210 : of inappropriate objects to be built. */
2211 22824 : if (is_gimple_reg (TREE_OPERAND (expr, 0))
2212 22824 : || !is_gimple_addressable (TREE_OPERAND (expr, 0)))
2213 : return true;
2214 22814 : return may_be_nonaddressable_p (TREE_OPERAND (expr, 0));
2215 :
2216 : CASE_CONVERT:
2217 : return true;
2218 :
2219 : default:
2220 : break;
2221 : }
2222 :
2223 : return false;
2224 : }
2225 :
2226 : /* Finds addresses in *OP_P inside STMT. */
2227 :
2228 : static void
2229 2756316 : find_interesting_uses_address (struct ivopts_data *data, gimple *stmt,
2230 : tree *op_p)
2231 : {
2232 2756316 : tree base = *op_p, step = size_zero_node;
2233 2756316 : struct iv *civ;
2234 2756316 : struct ifs_ivopts_data ifs_ivopts_data;
2235 :
2236 : /* Do not play with volatile memory references. A bit too conservative,
2237 : perhaps, but safe. */
2238 5512632 : if (gimple_has_volatile_ops (stmt))
2239 7598 : goto fail;
2240 :
2241 : /* Ignore bitfields for now. Not really something terribly complicated
2242 : to handle. TODO. */
2243 2748718 : if (TREE_CODE (base) == BIT_FIELD_REF)
2244 97384 : goto fail;
2245 :
2246 2651334 : base = unshare_expr (base);
2247 :
2248 2651334 : if (TREE_CODE (base) == TARGET_MEM_REF)
2249 : {
2250 310211 : tree type = build_pointer_type (TREE_TYPE (base));
2251 310211 : tree astep;
2252 :
2253 310211 : if (TMR_BASE (base)
2254 310211 : && TREE_CODE (TMR_BASE (base)) == SSA_NAME)
2255 : {
2256 288917 : civ = get_iv (data, TMR_BASE (base));
2257 288917 : if (!civ)
2258 255036 : goto fail;
2259 :
2260 33881 : TMR_BASE (base) = civ->base;
2261 33881 : step = civ->step;
2262 : }
2263 55175 : if (TMR_INDEX2 (base)
2264 55175 : && TREE_CODE (TMR_INDEX2 (base)) == SSA_NAME)
2265 : {
2266 13807 : civ = get_iv (data, TMR_INDEX2 (base));
2267 13807 : if (!civ)
2268 4871 : goto fail;
2269 :
2270 8936 : TMR_INDEX2 (base) = civ->base;
2271 8936 : step = civ->step;
2272 : }
2273 50304 : if (TMR_INDEX (base)
2274 50304 : && TREE_CODE (TMR_INDEX (base)) == SSA_NAME)
2275 : {
2276 50304 : civ = get_iv (data, TMR_INDEX (base));
2277 50304 : if (!civ)
2278 50304 : goto fail;
2279 :
2280 0 : TMR_INDEX (base) = civ->base;
2281 0 : astep = civ->step;
2282 :
2283 0 : if (astep)
2284 : {
2285 0 : if (TMR_STEP (base))
2286 0 : astep = fold_build2 (MULT_EXPR, type, TMR_STEP (base), astep);
2287 :
2288 0 : step = fold_build2 (PLUS_EXPR, type, step, astep);
2289 : }
2290 : }
2291 :
2292 0 : if (integer_zerop (step))
2293 0 : goto fail;
2294 0 : base = tree_mem_ref_addr (type, base);
2295 : }
2296 : else
2297 : {
2298 2341123 : ifs_ivopts_data.ivopts_data = data;
2299 2341123 : ifs_ivopts_data.stmt = stmt;
2300 2341123 : ifs_ivopts_data.step = size_zero_node;
2301 2341123 : if (!for_each_index (&base, idx_find_step, &ifs_ivopts_data)
2302 2341123 : || integer_zerop (ifs_ivopts_data.step))
2303 1463400 : goto fail;
2304 877723 : step = ifs_ivopts_data.step;
2305 :
2306 : /* Check that the base expression is addressable. This needs
2307 : to be done after substituting bases of IVs into it. */
2308 877723 : if (may_be_nonaddressable_p (base))
2309 778 : goto fail;
2310 :
2311 : /* Moreover, on strict alignment platforms, check that it is
2312 : sufficiently aligned. */
2313 876945 : if (STRICT_ALIGNMENT && may_be_unaligned_p (base, step))
2314 : goto fail;
2315 :
2316 876945 : base = build_fold_addr_expr (base);
2317 :
2318 : /* Substituting bases of IVs into the base expression might
2319 : have caused folding opportunities. */
2320 876945 : if (TREE_CODE (base) == ADDR_EXPR)
2321 : {
2322 470374 : tree *ref = &TREE_OPERAND (base, 0);
2323 1609443 : while (handled_component_p (*ref))
2324 668695 : ref = &TREE_OPERAND (*ref, 0);
2325 470374 : if (TREE_CODE (*ref) == MEM_REF)
2326 : {
2327 305778 : tree tem = fold_binary (MEM_REF, TREE_TYPE (*ref),
2328 : TREE_OPERAND (*ref, 0),
2329 : TREE_OPERAND (*ref, 1));
2330 305778 : if (tem)
2331 0 : *ref = tem;
2332 : }
2333 : }
2334 : }
2335 :
2336 876945 : civ = alloc_iv (data, base, step);
2337 : /* Fail if base object of this memory reference is unknown. */
2338 876945 : if (civ->base_object == NULL_TREE)
2339 11531 : goto fail;
2340 :
2341 865414 : record_group_use (data, op_p, civ, stmt, USE_REF_ADDRESS, TREE_TYPE (*op_p));
2342 865414 : return;
2343 :
2344 1890902 : fail:
2345 1890902 : for_each_index (op_p, idx_record_use, data);
2346 : }
2347 :
2348 : /* Finds and records invariants used in STMT. */
2349 :
2350 : static void
2351 15628242 : find_invariants_stmt (struct ivopts_data *data, gimple *stmt)
2352 : {
2353 15628242 : ssa_op_iter iter;
2354 15628242 : use_operand_p use_p;
2355 15628242 : tree op;
2356 :
2357 52035669 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2358 : {
2359 20779185 : op = USE_FROM_PTR (use_p);
2360 20779185 : record_invariant (data, op, false);
2361 : }
2362 15628242 : }
2363 :
2364 : /* CALL calls an internal function. If operand *OP_P will become an
2365 : address when the call is expanded, return the type of the memory
2366 : being addressed, otherwise return null. */
2367 :
2368 : static tree
2369 3510 : get_mem_type_for_internal_fn (gcall *call, tree *op_p)
2370 : {
2371 3510 : switch (gimple_call_internal_fn (call))
2372 : {
2373 272 : case IFN_MASK_LOAD:
2374 272 : case IFN_MASK_LOAD_LANES:
2375 272 : case IFN_MASK_LEN_LOAD_LANES:
2376 272 : case IFN_LEN_LOAD:
2377 272 : case IFN_MASK_LEN_LOAD:
2378 272 : if (op_p == gimple_call_arg_ptr (call, 0))
2379 272 : return TREE_TYPE (gimple_call_lhs (call));
2380 : return NULL_TREE;
2381 :
2382 365 : case IFN_MASK_STORE:
2383 365 : case IFN_MASK_STORE_LANES:
2384 365 : case IFN_MASK_LEN_STORE_LANES:
2385 365 : case IFN_LEN_STORE:
2386 365 : case IFN_MASK_LEN_STORE:
2387 365 : {
2388 365 : if (op_p == gimple_call_arg_ptr (call, 0))
2389 : {
2390 365 : internal_fn ifn = gimple_call_internal_fn (call);
2391 365 : int index = internal_fn_stored_value_index (ifn);
2392 365 : return TREE_TYPE (gimple_call_arg (call, index));
2393 : }
2394 : return NULL_TREE;
2395 : }
2396 :
2397 : default:
2398 : return NULL_TREE;
2399 : }
2400 : }
2401 :
2402 : /* IV is a (non-address) iv that describes operand *OP_P of STMT.
2403 : Return true if the operand will become an address when STMT
2404 : is expanded and record the associated address use if so. */
2405 :
2406 : static bool
2407 1765958 : find_address_like_use (struct ivopts_data *data, gimple *stmt, tree *op_p,
2408 : struct iv *iv)
2409 : {
2410 : /* Fail if base object of this memory reference is unknown. */
2411 1765958 : if (iv->base_object == NULL_TREE)
2412 : return false;
2413 :
2414 681034 : tree mem_type = NULL_TREE;
2415 681034 : if (gcall *call = dyn_cast <gcall *> (stmt))
2416 127220 : if (gimple_call_internal_p (call))
2417 3510 : mem_type = get_mem_type_for_internal_fn (call, op_p);
2418 3510 : if (mem_type)
2419 : {
2420 637 : iv = alloc_iv (data, iv->base, iv->step);
2421 637 : record_group_use (data, op_p, iv, stmt, USE_PTR_ADDRESS, mem_type);
2422 637 : return true;
2423 : }
2424 : return false;
2425 : }
2426 :
2427 : /* Finds interesting uses of induction variables in the statement STMT. */
2428 :
2429 : static void
2430 15628242 : find_interesting_uses_stmt (struct ivopts_data *data, gimple *stmt)
2431 : {
2432 15628242 : struct iv *iv;
2433 15628242 : tree op, *lhs, *rhs;
2434 15628242 : ssa_op_iter iter;
2435 15628242 : use_operand_p use_p;
2436 15628242 : enum tree_code code;
2437 :
2438 15628242 : find_invariants_stmt (data, stmt);
2439 :
2440 15628242 : if (gimple_code (stmt) == GIMPLE_COND)
2441 : {
2442 1485943 : find_interesting_uses_cond (data, stmt);
2443 9153614 : return;
2444 : }
2445 :
2446 14142299 : if (is_gimple_assign (stmt))
2447 : {
2448 10653663 : lhs = gimple_assign_lhs_ptr (stmt);
2449 10653663 : rhs = gimple_assign_rhs1_ptr (stmt);
2450 :
2451 10653663 : if (TREE_CODE (*lhs) == SSA_NAME)
2452 : {
2453 : /* If the statement defines an induction variable, the uses are not
2454 : interesting by themselves. */
2455 :
2456 9526333 : iv = get_iv (data, *lhs);
2457 :
2458 9526333 : if (iv && !integer_zerop (iv->step))
2459 : return;
2460 : }
2461 :
2462 8285262 : code = gimple_assign_rhs_code (stmt);
2463 8285262 : if (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
2464 8285262 : && (REFERENCE_CLASS_P (*rhs)
2465 1276073 : || is_gimple_val (*rhs)))
2466 : {
2467 2854683 : if (REFERENCE_CLASS_P (*rhs))
2468 1783042 : find_interesting_uses_address (data, stmt, rhs);
2469 : else
2470 1071641 : find_interesting_uses_op (data, *rhs);
2471 :
2472 2854683 : if (REFERENCE_CLASS_P (*lhs))
2473 973274 : find_interesting_uses_address (data, stmt, lhs);
2474 : return;
2475 : }
2476 5430579 : else if (TREE_CODE_CLASS (code) == tcc_comparison)
2477 : {
2478 79683 : find_interesting_uses_cond (data, stmt);
2479 79683 : return;
2480 : }
2481 :
2482 : /* TODO -- we should also handle address uses of type
2483 :
2484 : memory = call (whatever);
2485 :
2486 : and
2487 :
2488 : call (memory). */
2489 : }
2490 :
2491 8839532 : if (gimple_code (stmt) == GIMPLE_PHI
2492 8839532 : && gimple_bb (stmt) == data->current_loop->header)
2493 : {
2494 1467573 : iv = get_iv (data, PHI_RESULT (stmt));
2495 :
2496 1467573 : if (iv && !integer_zerop (iv->step))
2497 : return;
2498 : }
2499 :
2500 26746612 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, iter, SSA_OP_USE)
2501 : {
2502 10825470 : op = USE_FROM_PTR (use_p);
2503 :
2504 10825470 : if (TREE_CODE (op) != SSA_NAME)
2505 527287 : continue;
2506 :
2507 10298183 : iv = get_iv (data, op);
2508 10298183 : if (!iv)
2509 8532225 : continue;
2510 :
2511 1765958 : if (!find_address_like_use (data, stmt, use_p->use, iv))
2512 1765321 : find_interesting_uses_op (data, op);
2513 : }
2514 : }
2515 :
2516 : /* Finds interesting uses of induction variables outside of loops
2517 : on loop exit edge EXIT. */
2518 :
2519 : static void
2520 907419 : find_interesting_uses_outside (struct ivopts_data *data, edge exit)
2521 : {
2522 907419 : gphi *phi;
2523 907419 : gphi_iterator psi;
2524 907419 : tree def;
2525 :
2526 2024383 : for (psi = gsi_start_phis (exit->dest); !gsi_end_p (psi); gsi_next (&psi))
2527 : {
2528 1116964 : phi = psi.phi ();
2529 1116964 : def = PHI_ARG_DEF_FROM_EDGE (phi, exit);
2530 2142488 : if (!virtual_operand_p (def))
2531 547793 : find_interesting_uses_op (data, def);
2532 : }
2533 907419 : }
2534 :
2535 : /* Return TRUE if OFFSET is within the range of [base + offset] addressing
2536 : mode for memory reference represented by USE. */
2537 :
2538 : static GTY (()) vec<rtx, va_gc> *addr_list;
2539 :
2540 : static bool
2541 224466 : addr_offset_valid_p (struct iv_use *use, poly_int64 offset)
2542 : {
2543 224466 : rtx reg, addr;
2544 224466 : unsigned list_index;
2545 224466 : addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (use->iv->base));
2546 224466 : machine_mode addr_mode, mem_mode = TYPE_MODE (use->mem_type);
2547 :
2548 224466 : list_index = (unsigned) as * MAX_MACHINE_MODE + (unsigned) mem_mode;
2549 224466 : if (list_index >= vec_safe_length (addr_list))
2550 10073 : vec_safe_grow_cleared (addr_list, list_index + MAX_MACHINE_MODE, true);
2551 :
2552 224466 : addr = (*addr_list)[list_index];
2553 224466 : if (!addr)
2554 : {
2555 13119 : addr_mode = targetm.addr_space.address_mode (as);
2556 13119 : reg = gen_raw_REG (addr_mode, LAST_VIRTUAL_REGISTER + 1);
2557 13119 : addr = gen_rtx_fmt_ee (PLUS, addr_mode, reg, NULL_RTX);
2558 13119 : (*addr_list)[list_index] = addr;
2559 : }
2560 : else
2561 211347 : addr_mode = GET_MODE (addr);
2562 :
2563 224466 : XEXP (addr, 1) = gen_int_mode (offset, addr_mode);
2564 224466 : return (memory_address_addr_space_p (mem_mode, addr, as));
2565 : }
2566 :
2567 : /* Comparison function to sort group in ascending order of addr_offset. */
2568 :
2569 : static int
2570 3324237 : group_compare_offset (const void *a, const void *b)
2571 : {
2572 3324237 : const struct iv_use *const *u1 = (const struct iv_use *const *) a;
2573 3324237 : const struct iv_use *const *u2 = (const struct iv_use *const *) b;
2574 :
2575 3324237 : return compare_sizes_for_sort ((*u1)->addr_offset, (*u2)->addr_offset);
2576 : }
2577 :
2578 : /* Check if small groups should be split. Return true if no group
2579 : contains more than two uses with distinct addr_offsets. Return
2580 : false otherwise. We want to split such groups because:
2581 :
2582 : 1) Small groups don't have much benefit and may interfere with
2583 : general candidate selection.
2584 : 2) Size for problem with only small groups is usually small and
2585 : general algorithm can handle it well.
2586 :
2587 : TODO -- Above claim may not hold when we want to merge memory
2588 : accesses with conseuctive addresses. */
2589 :
2590 : static bool
2591 507706 : split_small_address_groups_p (struct ivopts_data *data)
2592 : {
2593 507706 : unsigned int i, j, distinct = 1;
2594 507706 : struct iv_use *pre;
2595 507706 : struct iv_group *group;
2596 :
2597 2110812 : for (i = 0; i < data->vgroups.length (); i++)
2598 : {
2599 1603106 : group = data->vgroups[i];
2600 1603106 : if (group->vuses.length () == 1)
2601 1466479 : continue;
2602 :
2603 136627 : gcc_assert (address_p (group->type));
2604 136627 : if (group->vuses.length () == 2)
2605 : {
2606 77573 : if (compare_sizes_for_sort (group->vuses[0]->addr_offset,
2607 77573 : group->vuses[1]->addr_offset) > 0)
2608 19121 : std::swap (group->vuses[0], group->vuses[1]);
2609 : }
2610 : else
2611 59054 : group->vuses.qsort (group_compare_offset);
2612 :
2613 136627 : if (distinct > 2)
2614 13969 : continue;
2615 :
2616 122658 : distinct = 1;
2617 1791682 : for (pre = group->vuses[0], j = 1; j < group->vuses.length (); j++)
2618 : {
2619 188576 : if (maybe_ne (group->vuses[j]->addr_offset, pre->addr_offset))
2620 : {
2621 131034 : pre = group->vuses[j];
2622 131034 : distinct++;
2623 : }
2624 :
2625 188576 : if (distinct > 2)
2626 : break;
2627 : }
2628 : }
2629 :
2630 507706 : return (distinct <= 2);
2631 : }
2632 :
2633 : /* For each group of address type uses, this function further groups
2634 : these uses according to the maximum offset supported by target's
2635 : [base + offset] addressing mode. */
2636 :
2637 : static void
2638 507706 : split_address_groups (struct ivopts_data *data)
2639 : {
2640 507706 : unsigned int i, j;
2641 : /* Always split group. */
2642 507706 : bool split_p = split_small_address_groups_p (data);
2643 :
2644 2670163 : for (i = 0; i < data->vgroups.length (); i++)
2645 : {
2646 1654751 : struct iv_group *new_group = NULL;
2647 1654751 : struct iv_group *group = data->vgroups[i];
2648 1654751 : struct iv_use *use = group->vuses[0];
2649 :
2650 1654751 : use->id = 0;
2651 1654751 : use->group_id = group->id;
2652 1654751 : if (group->vuses.length () == 1)
2653 1512389 : continue;
2654 :
2655 142362 : gcc_assert (address_p (use->type));
2656 :
2657 1994810 : for (j = 1; j < group->vuses.length ();)
2658 : {
2659 340059 : struct iv_use *next = group->vuses[j];
2660 340059 : poly_int64 offset = next->addr_offset - use->addr_offset;
2661 :
2662 : /* Split group if asked to, or the offset against the first
2663 : use can't fit in offset part of addressing mode. IV uses
2664 : having the same offset are still kept in one group. */
2665 397888 : if (maybe_ne (offset, 0)
2666 340059 : && (split_p || !addr_offset_valid_p (use, offset)))
2667 : {
2668 57829 : if (!new_group)
2669 51645 : new_group = record_group (data, group->type);
2670 57829 : group->vuses.ordered_remove (j);
2671 57829 : new_group->vuses.safe_push (next);
2672 57829 : continue;
2673 : }
2674 :
2675 282230 : next->id = j;
2676 282230 : next->group_id = group->id;
2677 282230 : j++;
2678 : }
2679 : }
2680 507706 : }
2681 :
2682 : /* Finds uses of the induction variables that are interesting. */
2683 :
2684 : static void
2685 507706 : find_interesting_uses (struct ivopts_data *data, basic_block *body)
2686 : {
2687 507706 : basic_block bb;
2688 507706 : gimple_stmt_iterator bsi;
2689 507706 : unsigned i;
2690 507706 : edge e;
2691 :
2692 3364184 : for (i = 0; i < data->current_loop->num_nodes; i++)
2693 : {
2694 2856478 : edge_iterator ei;
2695 2856478 : bb = body[i];
2696 :
2697 7298383 : FOR_EACH_EDGE (e, ei, bb->succs)
2698 4441905 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
2699 4441905 : && !flow_bb_inside_loop_p (data->current_loop, e->dest))
2700 907419 : find_interesting_uses_outside (data, e);
2701 :
2702 5752683 : for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2703 2896205 : find_interesting_uses_stmt (data, gsi_stmt (bsi));
2704 28544816 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2705 22831860 : if (!is_gimple_debug (gsi_stmt (bsi)))
2706 12732037 : find_interesting_uses_stmt (data, gsi_stmt (bsi));
2707 : }
2708 :
2709 507706 : split_address_groups (data);
2710 :
2711 507706 : if (dump_file && (dump_flags & TDF_DETAILS))
2712 : {
2713 67 : fprintf (dump_file, "\n<IV Groups>:\n");
2714 67 : dump_groups (dump_file, data);
2715 67 : fprintf (dump_file, "\n");
2716 : }
2717 507706 : }
2718 :
2719 : /* Strips constant offsets from EXPR and stores them to OFFSET. If INSIDE_ADDR
2720 : is true, assume we are inside an address. If TOP_COMPREF is true, assume
2721 : we are at the top-level of the processed address. */
2722 :
2723 : static tree
2724 3396766 : strip_offset_1 (tree expr, bool inside_addr, bool top_compref,
2725 : poly_int64 *offset)
2726 : {
2727 3396766 : tree op0 = NULL_TREE, op1 = NULL_TREE, tmp, step;
2728 3396766 : enum tree_code code;
2729 3396766 : tree type, orig_type = TREE_TYPE (expr);
2730 3396766 : poly_int64 off0, off1;
2731 3396766 : HOST_WIDE_INT st;
2732 3396766 : tree orig_expr = expr;
2733 :
2734 3396766 : STRIP_NOPS (expr);
2735 :
2736 3396766 : type = TREE_TYPE (expr);
2737 3396766 : code = TREE_CODE (expr);
2738 3396766 : *offset = 0;
2739 :
2740 3396766 : switch (code)
2741 : {
2742 621640 : case POINTER_PLUS_EXPR:
2743 621640 : case PLUS_EXPR:
2744 621640 : case MINUS_EXPR:
2745 621640 : op0 = TREE_OPERAND (expr, 0);
2746 621640 : op1 = TREE_OPERAND (expr, 1);
2747 :
2748 621640 : op0 = strip_offset_1 (op0, false, false, &off0);
2749 621640 : op1 = strip_offset_1 (op1, false, false, &off1);
2750 :
2751 621640 : *offset = (code == MINUS_EXPR ? off0 - off1 : off0 + off1);
2752 621640 : if (op0 == TREE_OPERAND (expr, 0)
2753 621640 : && op1 == TREE_OPERAND (expr, 1))
2754 : return orig_expr;
2755 :
2756 384805 : if (integer_zerop (op1))
2757 : expr = op0;
2758 3045 : else if (integer_zerop (op0))
2759 : {
2760 675 : if (code == MINUS_EXPR)
2761 : {
2762 675 : if (TYPE_OVERFLOW_UNDEFINED (type))
2763 : {
2764 0 : type = unsigned_type_for (type);
2765 0 : op1 = fold_convert (type, op1);
2766 : }
2767 675 : expr = fold_build1 (NEGATE_EXPR, type, op1);
2768 : }
2769 : else
2770 : expr = op1;
2771 : }
2772 : else
2773 : {
2774 2370 : if (TYPE_OVERFLOW_UNDEFINED (type))
2775 : {
2776 0 : type = unsigned_type_for (type);
2777 0 : if (code == POINTER_PLUS_EXPR)
2778 0 : code = PLUS_EXPR;
2779 0 : op0 = fold_convert (type, op0);
2780 0 : op1 = fold_convert (type, op1);
2781 : }
2782 2370 : expr = fold_build2 (code, type, op0, op1);
2783 : }
2784 :
2785 384805 : return fold_convert (orig_type, expr);
2786 :
2787 218253 : case MULT_EXPR:
2788 218253 : op1 = TREE_OPERAND (expr, 1);
2789 218253 : if (!cst_and_fits_in_hwi (op1))
2790 : return orig_expr;
2791 :
2792 177394 : op0 = TREE_OPERAND (expr, 0);
2793 177394 : op0 = strip_offset_1 (op0, false, false, &off0);
2794 177394 : if (op0 == TREE_OPERAND (expr, 0))
2795 : return orig_expr;
2796 :
2797 7115 : *offset = off0 * int_cst_value (op1);
2798 7115 : if (integer_zerop (op0))
2799 : expr = op0;
2800 : else
2801 : {
2802 7115 : if (TYPE_OVERFLOW_UNDEFINED (type))
2803 : {
2804 0 : type = unsigned_type_for (type);
2805 0 : op0 = fold_convert (type, op0);
2806 0 : op1 = fold_convert (type, op1);
2807 : }
2808 7115 : expr = fold_build2 (MULT_EXPR, type, op0, op1);
2809 : }
2810 :
2811 7115 : return fold_convert (orig_type, expr);
2812 :
2813 11 : case ARRAY_REF:
2814 11 : case ARRAY_RANGE_REF:
2815 11 : if (!inside_addr)
2816 : return orig_expr;
2817 :
2818 11 : step = array_ref_element_size (expr);
2819 11 : if (!cst_and_fits_in_hwi (step))
2820 : break;
2821 :
2822 11 : st = int_cst_value (step);
2823 11 : op1 = TREE_OPERAND (expr, 1);
2824 11 : op1 = strip_offset_1 (op1, false, false, &off1);
2825 11 : *offset = off1 * st;
2826 :
2827 11 : if (top_compref
2828 11 : && integer_zerop (op1))
2829 : {
2830 : /* Strip the component reference completely. */
2831 9 : op0 = TREE_OPERAND (expr, 0);
2832 9 : op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
2833 9 : *offset += off0;
2834 9 : return op0;
2835 : }
2836 : break;
2837 :
2838 1 : case COMPONENT_REF:
2839 1 : {
2840 1 : tree field;
2841 :
2842 1 : if (!inside_addr)
2843 : return orig_expr;
2844 :
2845 1 : tmp = component_ref_field_offset (expr);
2846 1 : field = TREE_OPERAND (expr, 1);
2847 1 : if (top_compref
2848 1 : && cst_and_fits_in_hwi (tmp)
2849 2 : && cst_and_fits_in_hwi (DECL_FIELD_BIT_OFFSET (field)))
2850 : {
2851 1 : HOST_WIDE_INT boffset, abs_off;
2852 :
2853 : /* Strip the component reference completely. */
2854 1 : op0 = TREE_OPERAND (expr, 0);
2855 1 : op0 = strip_offset_1 (op0, inside_addr, top_compref, &off0);
2856 1 : boffset = int_cst_value (DECL_FIELD_BIT_OFFSET (field));
2857 1 : abs_off = abs_hwi (boffset) / BITS_PER_UNIT;
2858 1 : if (boffset < 0)
2859 0 : abs_off = -abs_off;
2860 :
2861 1 : *offset = off0 + int_cst_value (tmp) + abs_off;
2862 1 : return op0;
2863 : }
2864 : }
2865 : break;
2866 :
2867 321624 : case ADDR_EXPR:
2868 321624 : op0 = TREE_OPERAND (expr, 0);
2869 321624 : op0 = strip_offset_1 (op0, true, true, &off0);
2870 321624 : *offset += off0;
2871 :
2872 321624 : if (op0 == TREE_OPERAND (expr, 0))
2873 : return orig_expr;
2874 :
2875 10 : expr = build_fold_addr_expr (op0);
2876 10 : return fold_convert (orig_type, expr);
2877 :
2878 : case MEM_REF:
2879 : /* ??? Offset operand? */
2880 : inside_addr = false;
2881 : break;
2882 :
2883 2235235 : default:
2884 2235235 : if (ptrdiff_tree_p (expr, offset) && maybe_ne (*offset, 0))
2885 871146 : return build_int_cst (orig_type, 0);
2886 : return orig_expr;
2887 : }
2888 :
2889 : /* Default handling of expressions for that we want to recurse into
2890 : the first operand. */
2891 4 : op0 = TREE_OPERAND (expr, 0);
2892 4 : op0 = strip_offset_1 (op0, inside_addr, false, &off0);
2893 4 : *offset += off0;
2894 :
2895 4 : if (op0 == TREE_OPERAND (expr, 0)
2896 4 : && (!op1 || op1 == TREE_OPERAND (expr, 1)))
2897 : return orig_expr;
2898 :
2899 1 : expr = copy_node (expr);
2900 1 : TREE_OPERAND (expr, 0) = op0;
2901 1 : if (op1)
2902 1 : TREE_OPERAND (expr, 1) = op1;
2903 :
2904 : /* Inside address, we might strip the top level component references,
2905 : thus changing type of the expression. Handling of ADDR_EXPR
2906 : will fix that. */
2907 1 : expr = fold_convert (orig_type, expr);
2908 :
2909 1 : return expr;
2910 : }
2911 :
2912 : /* Strips constant offsets from EXPR and stores them to OFFSET. */
2913 :
2914 : static tree
2915 1654443 : strip_offset (tree expr, poly_uint64 *offset)
2916 : {
2917 1654443 : poly_int64 off;
2918 1654443 : tree core = strip_offset_1 (expr, false, false, &off);
2919 1654443 : *offset = off;
2920 1654443 : return core;
2921 : }
2922 :
2923 : /* Returns variant of TYPE that can be used as base for different uses.
2924 : We return unsigned type with the same precision, which avoids problems
2925 : with overflows. */
2926 :
2927 : static tree
2928 8113276 : generic_type_for (tree type)
2929 : {
2930 8113276 : if (POINTER_TYPE_P (type))
2931 1442149 : return unsigned_type_for (type);
2932 :
2933 6671127 : if (TYPE_UNSIGNED (type))
2934 : return type;
2935 :
2936 3119724 : return unsigned_type_for (type);
2937 : }
2938 :
2939 : /* Private data for walk_tree. */
2940 :
2941 : struct walk_tree_data
2942 : {
2943 : bitmap *inv_vars;
2944 : struct ivopts_data *idata;
2945 : };
2946 :
2947 : /* Callback function for walk_tree, it records invariants and symbol
2948 : reference in *EXPR_P. DATA is the structure storing result info. */
2949 :
2950 : static tree
2951 34198325 : find_inv_vars_cb (tree *expr_p, int *ws ATTRIBUTE_UNUSED, void *data)
2952 : {
2953 34198325 : tree op = *expr_p;
2954 34198325 : struct version_info *info;
2955 34198325 : struct walk_tree_data *wdata = (struct walk_tree_data*) data;
2956 :
2957 34198325 : if (TREE_CODE (op) != SSA_NAME)
2958 : return NULL_TREE;
2959 :
2960 7961607 : info = name_info (wdata->idata, op);
2961 : /* Because we expand simple operations when finding IVs, loop invariant
2962 : variable that isn't referred by the original loop could be used now.
2963 : Record such invariant variables here. */
2964 7961607 : if (!info->iv)
2965 : {
2966 386504 : struct ivopts_data *idata = wdata->idata;
2967 386504 : basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (op));
2968 :
2969 386504 : if (!bb || !flow_bb_inside_loop_p (idata->current_loop, bb))
2970 : {
2971 386504 : tree steptype = TREE_TYPE (op);
2972 386504 : if (POINTER_TYPE_P (steptype))
2973 192385 : steptype = sizetype;
2974 386504 : set_iv (idata, op, op, build_int_cst (steptype, 0), true);
2975 386504 : record_invariant (idata, op, false);
2976 : }
2977 : }
2978 7961607 : if (!info->inv_id || info->has_nonlin_use)
2979 : return NULL_TREE;
2980 :
2981 6577523 : if (!*wdata->inv_vars)
2982 5089272 : *wdata->inv_vars = BITMAP_ALLOC (NULL);
2983 6577523 : bitmap_set_bit (*wdata->inv_vars, info->inv_id);
2984 :
2985 6577523 : return NULL_TREE;
2986 : }
2987 :
2988 : /* Records invariants in *EXPR_P. INV_VARS is the bitmap to that we should
2989 : store it. */
2990 :
2991 : static inline void
2992 27910276 : find_inv_vars (struct ivopts_data *data, tree *expr_p, bitmap *inv_vars)
2993 : {
2994 27910276 : struct walk_tree_data wdata;
2995 :
2996 27910276 : if (!inv_vars)
2997 : return;
2998 :
2999 16015496 : wdata.idata = data;
3000 16015496 : wdata.inv_vars = inv_vars;
3001 16015496 : walk_tree (expr_p, find_inv_vars_cb, &wdata, NULL);
3002 : }
3003 :
3004 : /* Get entry from invariant expr hash table for INV_EXPR. New entry
3005 : will be recorded if it doesn't exist yet. Given below two exprs:
3006 : inv_expr + cst1, inv_expr + cst2
3007 : It's hard to make decision whether constant part should be stripped
3008 : or not. We choose to not strip based on below facts:
3009 : 1) We need to count ADD cost for constant part if it's stripped,
3010 : which isn't always trivial where this functions is called.
3011 : 2) Stripping constant away may be conflict with following loop
3012 : invariant hoisting pass.
3013 : 3) Not stripping constant away results in more invariant exprs,
3014 : which usually leads to decision preferring lower reg pressure. */
3015 :
3016 : static iv_inv_expr_ent *
3017 2607143 : get_loop_invariant_expr (struct ivopts_data *data, tree inv_expr)
3018 : {
3019 2607143 : STRIP_NOPS (inv_expr);
3020 :
3021 2607143 : if (poly_int_tree_p (inv_expr)
3022 2607143 : || TREE_CODE (inv_expr) == SSA_NAME)
3023 : return NULL;
3024 :
3025 : /* Don't strip constant part away as we used to. */
3026 :
3027 : /* Stores EXPR in DATA->inv_expr_tab, return pointer to iv_inv_expr_ent. */
3028 2520046 : struct iv_inv_expr_ent ent;
3029 2520046 : ent.expr = inv_expr;
3030 2520046 : ent.hash = iterative_hash_expr (inv_expr, 0);
3031 2520046 : struct iv_inv_expr_ent **slot = data->inv_expr_tab->find_slot (&ent, INSERT);
3032 :
3033 2520046 : if (!*slot)
3034 : {
3035 1151801 : *slot = XNEW (struct iv_inv_expr_ent);
3036 1151801 : (*slot)->expr = inv_expr;
3037 1151801 : (*slot)->hash = ent.hash;
3038 1151801 : (*slot)->id = ++data->max_inv_expr_id;
3039 : }
3040 :
3041 2520046 : return *slot;
3042 : }
3043 :
3044 :
3045 : /* Return *TP if it is an SSA_NAME marked with TREE_VISITED, i.e., as
3046 : unsuitable as ivopts candidates for potentially involving undefined
3047 : behavior. */
3048 :
3049 : static tree
3050 15413819 : find_ssa_undef (tree *tp, int *walk_subtrees, void *bb_)
3051 : {
3052 15413819 : basic_block bb = (basic_block) bb_;
3053 15413819 : if (TREE_CODE (*tp) == SSA_NAME
3054 2248492 : && ssa_name_maybe_undef_p (*tp)
3055 15422892 : && !ssa_name_any_use_dominates_bb_p (*tp, bb))
3056 3268 : return *tp;
3057 15410551 : if (!EXPR_P (*tp))
3058 10456839 : *walk_subtrees = 0;
3059 : return NULL;
3060 : }
3061 :
3062 : /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
3063 : position to POS. If USE is not NULL, the candidate is set as related to
3064 : it. If both BASE and STEP are NULL, we add a pseudocandidate for the
3065 : replacement of the final value of the iv by a direct computation. */
3066 :
3067 : static struct iv_cand *
3068 9097157 : add_candidate_1 (struct ivopts_data *data, tree base, tree step, bool important,
3069 : enum iv_position pos, struct iv_use *use,
3070 : gimple *incremented_at, struct iv *orig_iv = NULL,
3071 : bool doloop = false)
3072 : {
3073 9097157 : unsigned i;
3074 9097157 : struct iv_cand *cand = NULL;
3075 9097157 : tree type, orig_type;
3076 :
3077 9097157 : gcc_assert (base && step);
3078 :
3079 : /* -fkeep-gc-roots-live means that we have to keep a real pointer
3080 : live, but the ivopts code may replace a real pointer with one
3081 : pointing before or after the memory block that is then adjusted
3082 : into the memory block during the loop. FIXME: It would likely be
3083 : better to actually force the pointer live and still use ivopts;
3084 : for example, it would be enough to write the pointer into memory
3085 : and keep it there until after the loop. */
3086 9097157 : if (flag_keep_gc_roots_live && POINTER_TYPE_P (TREE_TYPE (base)))
3087 : return NULL;
3088 :
3089 : /* If BASE contains undefined SSA names make sure we only record
3090 : the original IV. */
3091 8991438 : bool involves_undefs = false;
3092 8991438 : if (walk_tree (&base, find_ssa_undef, data->current_loop->header, NULL))
3093 : {
3094 3268 : if (pos != IP_ORIGINAL)
3095 : return NULL;
3096 : important = false;
3097 : involves_undefs = true;
3098 : }
3099 :
3100 : /* For non-original variables, make sure their values are computed in a type
3101 : that does not invoke undefined behavior on overflows (since in general,
3102 : we cannot prove that these induction variables are non-wrapping). */
3103 8988170 : if (pos != IP_ORIGINAL)
3104 : {
3105 8113276 : orig_type = TREE_TYPE (base);
3106 8113276 : type = generic_type_for (orig_type);
3107 8113276 : if (type != orig_type)
3108 : {
3109 4561873 : base = fold_convert (type, base);
3110 4561873 : step = fold_convert (type, step);
3111 : }
3112 : }
3113 :
3114 44872198 : for (i = 0; i < data->vcands.length (); i++)
3115 : {
3116 40222076 : cand = data->vcands[i];
3117 :
3118 40222076 : if (cand->pos != pos)
3119 9889792 : continue;
3120 :
3121 30332284 : if (cand->incremented_at != incremented_at
3122 29838723 : || ((pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
3123 0 : && cand->ainc_use != use))
3124 493561 : continue;
3125 :
3126 29838723 : if (operand_equal_p (base, cand->iv->base, 0)
3127 9513820 : && operand_equal_p (step, cand->iv->step, 0)
3128 35574415 : && (TYPE_PRECISION (TREE_TYPE (base))
3129 5735692 : == TYPE_PRECISION (TREE_TYPE (cand->iv->base))))
3130 : break;
3131 : }
3132 :
3133 17977132 : if (i == data->vcands.length ())
3134 : {
3135 4650122 : cand = XCNEW (struct iv_cand);
3136 4650122 : cand->id = i;
3137 4650122 : cand->iv = alloc_iv (data, base, step);
3138 4650122 : cand->pos = pos;
3139 4650122 : if (pos != IP_ORIGINAL)
3140 : {
3141 3775008 : if (doloop)
3142 0 : cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "doloop");
3143 : else
3144 3775008 : cand->var_before = create_tmp_var_raw (TREE_TYPE (base), "ivtmp");
3145 3775008 : cand->var_after = cand->var_before;
3146 : }
3147 4650122 : cand->important = important;
3148 4650122 : cand->involves_undefs = involves_undefs;
3149 4650122 : cand->incremented_at = incremented_at;
3150 4650122 : cand->doloop_p = doloop;
3151 4650122 : data->vcands.safe_push (cand);
3152 :
3153 4650122 : if (!poly_int_tree_p (step))
3154 : {
3155 174010 : find_inv_vars (data, &step, &cand->inv_vars);
3156 :
3157 174010 : iv_inv_expr_ent *inv_expr = get_loop_invariant_expr (data, step);
3158 : /* Share bitmap between inv_vars and inv_exprs for cand. */
3159 174010 : if (inv_expr != NULL)
3160 : {
3161 93302 : cand->inv_exprs = cand->inv_vars;
3162 93302 : cand->inv_vars = NULL;
3163 93302 : if (cand->inv_exprs)
3164 75083 : bitmap_clear (cand->inv_exprs);
3165 : else
3166 18219 : cand->inv_exprs = BITMAP_ALLOC (NULL);
3167 :
3168 93302 : bitmap_set_bit (cand->inv_exprs, inv_expr->id);
3169 : }
3170 : }
3171 :
3172 4650122 : if (pos == IP_AFTER_USE || pos == IP_BEFORE_USE)
3173 : cand->ainc_use = use;
3174 : else
3175 4650122 : cand->ainc_use = NULL;
3176 :
3177 4650122 : cand->orig_iv = orig_iv;
3178 4650122 : if (dump_file && (dump_flags & TDF_DETAILS))
3179 686 : dump_cand (dump_file, cand);
3180 : }
3181 :
3182 8988566 : cand->important |= important;
3183 8988566 : cand->doloop_p |= doloop;
3184 :
3185 : /* Relate candidate to the group for which it is added. */
3186 8988566 : if (use)
3187 2511978 : bitmap_set_bit (data->vgroups[use->group_id]->related_cands, i);
3188 :
3189 : return cand;
3190 : }
3191 :
3192 : /* Returns true if incrementing the induction variable at the end of the LOOP
3193 : is allowed.
3194 :
3195 : The purpose is to avoid splitting latch edge with a biv increment, thus
3196 : creating a jump, possibly confusing other optimization passes and leaving
3197 : less freedom to scheduler. So we allow IP_END only if IP_NORMAL is not
3198 : available (so we do not have a better alternative), or if the latch edge
3199 : is already nonempty. */
3200 :
3201 : static bool
3202 7993003 : allow_ip_end_pos_p (class loop *loop)
3203 : {
3204 : /* Do not allow IP_END when creating the IV would need to split the
3205 : latch edge as that makes all IP_NORMAL invalid. */
3206 7993003 : auto pos = gsi_last_bb (ip_end_pos (loop));
3207 7993003 : if (!gsi_end_p (pos) && stmt_ends_bb_p (*pos))
3208 : return false;
3209 :
3210 7993003 : if (!ip_normal_pos (loop))
3211 : return true;
3212 :
3213 7894321 : if (!empty_block_p (ip_end_pos (loop)))
3214 226562 : return true;
3215 :
3216 : return false;
3217 : }
3218 :
3219 : /* If possible, adds autoincrement candidates BASE + STEP * i based on use USE.
3220 : Important field is set to IMPORTANT. */
3221 :
3222 : static void
3223 583818 : add_autoinc_candidates (struct ivopts_data *data, tree base, tree step,
3224 : bool important, struct iv_use *use)
3225 : {
3226 583818 : basic_block use_bb = gimple_bb (use->stmt);
3227 583818 : machine_mode mem_mode;
3228 583818 : unsigned HOST_WIDE_INT cstepi;
3229 :
3230 : /* If we insert the increment in any position other than the standard
3231 : ones, we must ensure that it is incremented once per iteration.
3232 : It must not be in an inner nested loop, or one side of an if
3233 : statement. */
3234 583818 : if (use_bb->loop_father != data->current_loop
3235 582154 : || !dominated_by_p (CDI_DOMINATORS, data->current_loop->latch, use_bb)
3236 555902 : || stmt_can_throw_internal (cfun, use->stmt)
3237 1135898 : || !cst_and_fits_in_hwi (step))
3238 : return;
3239 :
3240 523030 : cstepi = int_cst_value (step);
3241 :
3242 523030 : mem_mode = TYPE_MODE (use->mem_type);
3243 : if (((USE_LOAD_PRE_INCREMENT (mem_mode)
3244 : || USE_STORE_PRE_INCREMENT (mem_mode))
3245 : && known_eq (GET_MODE_SIZE (mem_mode), cstepi))
3246 : || ((USE_LOAD_PRE_DECREMENT (mem_mode)
3247 : || USE_STORE_PRE_DECREMENT (mem_mode))
3248 : && known_eq (GET_MODE_SIZE (mem_mode), -cstepi)))
3249 : {
3250 : enum tree_code code = MINUS_EXPR;
3251 : tree new_base;
3252 : tree new_step = step;
3253 :
3254 : if (POINTER_TYPE_P (TREE_TYPE (base)))
3255 : {
3256 : new_step = fold_build1 (NEGATE_EXPR, TREE_TYPE (step), step);
3257 : code = POINTER_PLUS_EXPR;
3258 : }
3259 : else
3260 : new_step = fold_convert (TREE_TYPE (base), new_step);
3261 : new_base = fold_build2 (code, TREE_TYPE (base), base, new_step);
3262 : add_candidate_1 (data, new_base, step, important, IP_BEFORE_USE, use,
3263 : use->stmt);
3264 : }
3265 : if (((USE_LOAD_POST_INCREMENT (mem_mode)
3266 : || USE_STORE_POST_INCREMENT (mem_mode))
3267 : && known_eq (GET_MODE_SIZE (mem_mode), cstepi))
3268 : || ((USE_LOAD_POST_DECREMENT (mem_mode)
3269 : || USE_STORE_POST_DECREMENT (mem_mode))
3270 : && known_eq (GET_MODE_SIZE (mem_mode), -cstepi)))
3271 : {
3272 : add_candidate_1 (data, base, step, important, IP_AFTER_USE, use,
3273 : use->stmt);
3274 : }
3275 : }
3276 :
3277 : /* Adds a candidate BASE + STEP * i. Important field is set to IMPORTANT and
3278 : position to POS. If USE is not NULL, the candidate is set as related to
3279 : it. The candidate computation is scheduled before exit condition and at
3280 : the end of loop. */
3281 :
3282 : static void
3283 7031038 : add_candidate (struct ivopts_data *data, tree base, tree step, bool important,
3284 : struct iv_use *use, struct iv *orig_iv = NULL,
3285 : bool doloop = false)
3286 : {
3287 7031038 : if (ip_normal_pos (data->current_loop))
3288 6947365 : add_candidate_1 (data, base, step, important, IP_NORMAL, use, NULL, orig_iv,
3289 : doloop);
3290 : /* Exclude doloop candidate here since it requires decrement then comparison
3291 : and jump, the IP_END position doesn't match. */
3292 7031038 : if (!doloop && ip_end_pos (data->current_loop)
3293 14062076 : && allow_ip_end_pos_p (data->current_loop))
3294 279396 : add_candidate_1 (data, base, step, important, IP_END, use, NULL, orig_iv);
3295 7031038 : }
3296 :
3297 : /* Adds standard iv candidates. */
3298 :
3299 : static void
3300 507705 : add_standard_iv_candidates (struct ivopts_data *data)
3301 : {
3302 507705 : add_candidate (data, integer_zero_node, integer_one_node, true, NULL);
3303 :
3304 : /* The same for a double-integer type if it is still fast enough. */
3305 507705 : if (TYPE_PRECISION
3306 507705 : (long_integer_type_node) > TYPE_PRECISION (integer_type_node)
3307 507705 : && TYPE_PRECISION (long_integer_type_node) <= BITS_PER_WORD)
3308 459556 : add_candidate (data, build_int_cst (long_integer_type_node, 0),
3309 : build_int_cst (long_integer_type_node, 1), true, NULL);
3310 :
3311 : /* The same for a double-integer type if it is still fast enough. */
3312 507705 : if (TYPE_PRECISION
3313 507705 : (long_long_integer_type_node) > TYPE_PRECISION (long_integer_type_node)
3314 555842 : && TYPE_PRECISION (long_long_integer_type_node) <= BITS_PER_WORD)
3315 12 : add_candidate (data, build_int_cst (long_long_integer_type_node, 0),
3316 : build_int_cst (long_long_integer_type_node, 1), true, NULL);
3317 507705 : }
3318 :
3319 :
3320 : /* Adds candidates bases on the old induction variable IV. */
3321 :
3322 : static void
3323 1755003 : add_iv_candidate_for_biv (struct ivopts_data *data, struct iv *iv)
3324 : {
3325 1755003 : gimple *phi;
3326 1755003 : tree def;
3327 1755003 : struct iv_cand *cand;
3328 :
3329 : /* Check if this biv is used in address type use. */
3330 1161369 : if (iv->no_overflow && iv->have_address_use
3331 496973 : && INTEGRAL_TYPE_P (TREE_TYPE (iv->base))
3332 2251976 : && TYPE_PRECISION (TREE_TYPE (iv->base)) < TYPE_PRECISION (sizetype))
3333 : {
3334 281238 : tree base = fold_convert (sizetype, iv->base);
3335 281238 : tree step = fold_convert (sizetype, iv->step);
3336 :
3337 : /* Add iv cand of same precision as index part in TARGET_MEM_REF. */
3338 281238 : add_candidate (data, base, step, true, NULL, iv);
3339 : /* Add iv cand of the original type only if it has nonlinear use. */
3340 281238 : if (iv->nonlin_use)
3341 27861 : add_candidate (data, iv->base, iv->step, true, NULL);
3342 : }
3343 : else
3344 1473765 : add_candidate (data, iv->base, iv->step, true, NULL);
3345 :
3346 : /* The same, but with initial value zero. */
3347 1755003 : if (POINTER_TYPE_P (TREE_TYPE (iv->base)))
3348 331654 : add_candidate (data, size_int (0), iv->step, true, NULL);
3349 : else
3350 1423349 : add_candidate (data, build_int_cst (TREE_TYPE (iv->base), 0),
3351 : iv->step, true, NULL);
3352 :
3353 1755003 : phi = SSA_NAME_DEF_STMT (iv->ssa_name);
3354 1755003 : if (gimple_code (phi) == GIMPLE_PHI)
3355 : {
3356 : /* Additionally record the possibility of leaving the original iv
3357 : untouched. */
3358 877592 : def = PHI_ARG_DEF_FROM_EDGE (phi, loop_latch_edge (data->current_loop));
3359 : /* Don't add candidate if it's from another PHI node because
3360 : it's an affine iv appearing in the form of PEELED_CHREC. */
3361 877592 : phi = SSA_NAME_DEF_STMT (def);
3362 877592 : if (gimple_code (phi) != GIMPLE_PHI)
3363 : {
3364 1755184 : cand = add_candidate_1 (data,
3365 : iv->base, iv->step, true, IP_ORIGINAL, NULL,
3366 877592 : SSA_NAME_DEF_STMT (def));
3367 877592 : if (cand)
3368 : {
3369 875290 : cand->var_before = iv->ssa_name;
3370 875290 : cand->var_after = def;
3371 : }
3372 : }
3373 : else
3374 0 : gcc_assert (gimple_bb (phi) == data->current_loop->header);
3375 : }
3376 1755003 : }
3377 :
3378 : /* Adds candidates based on the old induction variables. */
3379 :
3380 : static void
3381 507705 : add_iv_candidate_for_bivs (struct ivopts_data *data)
3382 : {
3383 507705 : unsigned i;
3384 507705 : struct iv *iv;
3385 507705 : bitmap_iterator bi;
3386 :
3387 5540063 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
3388 : {
3389 5032358 : iv = ver_info (data, i)->iv;
3390 5032358 : if (iv && iv->biv_p && !integer_zerop (iv->step))
3391 1755003 : add_iv_candidate_for_biv (data, iv);
3392 : }
3393 507705 : }
3394 :
3395 : /* Record common candidate {BASE, STEP} derived from USE in hashtable. */
3396 :
3397 : static void
3398 4180037 : record_common_cand (struct ivopts_data *data, tree base,
3399 : tree step, struct iv_use *use)
3400 : {
3401 4180037 : class iv_common_cand ent;
3402 4180037 : class iv_common_cand **slot;
3403 :
3404 4180037 : ent.base = base;
3405 4180037 : ent.step = step;
3406 4180037 : ent.hash = iterative_hash_expr (base, 0);
3407 4180037 : ent.hash = iterative_hash_expr (step, ent.hash);
3408 :
3409 4180037 : slot = data->iv_common_cand_tab->find_slot (&ent, INSERT);
3410 4180037 : if (*slot == NULL)
3411 : {
3412 2628788 : *slot = new iv_common_cand ();
3413 2628788 : (*slot)->base = base;
3414 2628788 : (*slot)->step = step;
3415 2628788 : (*slot)->uses.create (8);
3416 2628788 : (*slot)->hash = ent.hash;
3417 2628788 : data->iv_common_cands.safe_push ((*slot));
3418 : }
3419 :
3420 4180037 : gcc_assert (use != NULL);
3421 4180037 : (*slot)->uses.safe_push (use);
3422 4180037 : return;
3423 4180037 : }
3424 :
3425 : /* Comparison function used to sort common candidates. */
3426 :
3427 : static int
3428 19119932 : common_cand_cmp (const void *p1, const void *p2)
3429 : {
3430 19119932 : unsigned n1, n2;
3431 19119932 : const class iv_common_cand *const *const ccand1
3432 : = (const class iv_common_cand *const *)p1;
3433 19119932 : const class iv_common_cand *const *const ccand2
3434 : = (const class iv_common_cand *const *)p2;
3435 :
3436 19119932 : n1 = (*ccand1)->uses.length ();
3437 19119932 : n2 = (*ccand2)->uses.length ();
3438 19119932 : return n2 - n1;
3439 : }
3440 :
3441 : /* Adds IV candidates based on common candidated recorded. */
3442 :
3443 : static void
3444 507705 : add_iv_candidate_derived_from_uses (struct ivopts_data *data)
3445 : {
3446 507705 : unsigned i, j;
3447 507705 : struct iv_cand *cand_1, *cand_2;
3448 :
3449 507705 : data->iv_common_cands.qsort (common_cand_cmp);
3450 1469670 : for (i = 0; i < data->iv_common_cands.length (); i++)
3451 : {
3452 1453485 : class iv_common_cand *ptr = data->iv_common_cands[i];
3453 :
3454 : /* Only add IV candidate if it's derived from multiple uses. */
3455 1453485 : if (ptr->uses.length () <= 1)
3456 : break;
3457 :
3458 961965 : cand_1 = NULL;
3459 961965 : cand_2 = NULL;
3460 961965 : if (ip_normal_pos (data->current_loop))
3461 946956 : cand_1 = add_candidate_1 (data, ptr->base, ptr->step,
3462 : false, IP_NORMAL, NULL, NULL);
3463 :
3464 961965 : if (ip_end_pos (data->current_loop)
3465 961965 : && allow_ip_end_pos_p (data->current_loop))
3466 45848 : cand_2 = add_candidate_1 (data, ptr->base, ptr->step,
3467 : false, IP_END, NULL, NULL);
3468 :
3469 : /* Bind deriving uses and the new candidates. */
3470 3475179 : for (j = 0; j < ptr->uses.length (); j++)
3471 : {
3472 2513214 : struct iv_group *group = data->vgroups[ptr->uses[j]->group_id];
3473 2513214 : if (cand_1)
3474 2439447 : bitmap_set_bit (group->related_cands, cand_1->id);
3475 2513214 : if (cand_2)
3476 135907 : bitmap_set_bit (group->related_cands, cand_2->id);
3477 : }
3478 : }
3479 :
3480 : /* Release data since it is useless from this point. */
3481 507705 : data->iv_common_cand_tab->empty ();
3482 507705 : data->iv_common_cands.truncate (0);
3483 507705 : }
3484 :
3485 : /* Adds candidates based on the value of USE's iv. */
3486 :
3487 : static void
3488 1654747 : add_iv_candidate_for_use (struct ivopts_data *data, struct iv_use *use)
3489 : {
3490 1654747 : poly_uint64 offset;
3491 1654747 : tree base;
3492 1654747 : struct iv *iv = use->iv;
3493 1654747 : tree basetype = TREE_TYPE (iv->base);
3494 :
3495 : /* Don't add candidate for iv_use with non integer, pointer or non-mode
3496 : precision types, instead, add candidate for the corresponding scev in
3497 : unsigned type with the same precision. See PR93674 for more info. */
3498 780918 : if ((TREE_CODE (basetype) != INTEGER_TYPE && !POINTER_TYPE_P (basetype))
3499 2435396 : || !type_has_mode_precision_p (basetype))
3500 : {
3501 304 : basetype = lang_hooks.types.type_for_mode (TYPE_MODE (basetype),
3502 304 : TYPE_UNSIGNED (basetype));
3503 304 : add_candidate (data, fold_convert (basetype, iv->base),
3504 : fold_convert (basetype, iv->step), false, NULL);
3505 304 : return;
3506 : }
3507 :
3508 1654443 : add_candidate (data, iv->base, iv->step, false, use);
3509 :
3510 : /* Record common candidate for use in case it can be shared by others. */
3511 1654443 : record_common_cand (data, iv->base, iv->step, use);
3512 :
3513 : /* Record common candidate with initial value zero. */
3514 1654443 : basetype = TREE_TYPE (iv->base);
3515 1654443 : if (POINTER_TYPE_P (basetype))
3516 780649 : basetype = sizetype;
3517 1654443 : record_common_cand (data, build_int_cst (basetype, 0), iv->step, use);
3518 :
3519 : /* Compare the cost of an address with an unscaled index with the cost of
3520 : an address with a scaled index and add candidate if useful. */
3521 1654443 : poly_int64 step;
3522 1654443 : if (use != NULL
3523 1654443 : && poly_int_tree_p (iv->step, &step)
3524 1421543 : && address_p (use->type))
3525 : {
3526 534798 : poly_int64 new_step;
3527 534798 : unsigned int fact = preferred_mem_scale_factor
3528 1069596 : (use->iv->base,
3529 534798 : TYPE_MODE (use->mem_type),
3530 : optimize_loop_for_speed_p (data->current_loop));
3531 :
3532 534798 : if (fact != 1
3533 534798 : && multiple_p (step, fact, &new_step))
3534 0 : add_candidate (data, size_int (0),
3535 0 : wide_int_to_tree (sizetype, new_step),
3536 : true, NULL);
3537 : }
3538 :
3539 : /* Record common candidate with constant offset stripped in base.
3540 : Like the use itself, we also add candidate directly for it. */
3541 1654443 : base = strip_offset (iv->base, &offset);
3542 1654443 : if (maybe_ne (offset, 0U) || base != iv->base)
3543 : {
3544 871151 : record_common_cand (data, base, iv->step, use);
3545 871151 : add_candidate (data, base, iv->step, false, use);
3546 : }
3547 :
3548 : /* Record common candidate with base_object removed in base. */
3549 1654443 : base = iv->base;
3550 1654443 : STRIP_NOPS (base);
3551 1654443 : if (iv->base_object != NULL && TREE_CODE (base) == POINTER_PLUS_EXPR)
3552 : {
3553 0 : tree step = iv->step;
3554 :
3555 0 : STRIP_NOPS (step);
3556 0 : base = TREE_OPERAND (base, 1);
3557 0 : step = fold_convert (sizetype, step);
3558 0 : record_common_cand (data, base, step, use);
3559 : /* Also record common candidate with offset stripped. */
3560 0 : tree alt_base, alt_offset;
3561 0 : split_constant_offset (base, &alt_base, &alt_offset);
3562 0 : if (!integer_zerop (alt_offset))
3563 0 : record_common_cand (data, alt_base, step, use);
3564 : }
3565 :
3566 : /* At last, add auto-incremental candidates. Make such variables
3567 : important since other iv uses with same base object may be based
3568 : on it. */
3569 1654443 : if (use != NULL && address_p (use->type))
3570 583818 : add_autoinc_candidates (data, iv->base, iv->step, true, use);
3571 : }
3572 :
3573 : /* Adds candidates based on the uses. */
3574 :
3575 : static void
3576 507705 : add_iv_candidate_for_groups (struct ivopts_data *data)
3577 : {
3578 507705 : unsigned i;
3579 :
3580 : /* Only add candidate for the first use in group. */
3581 2162452 : for (i = 0; i < data->vgroups.length (); i++)
3582 : {
3583 1654747 : struct iv_group *group = data->vgroups[i];
3584 :
3585 1654747 : gcc_assert (group->vuses[0] != NULL);
3586 1654747 : add_iv_candidate_for_use (data, group->vuses[0]);
3587 : }
3588 507705 : add_iv_candidate_derived_from_uses (data);
3589 507705 : }
3590 :
3591 : /* Record important candidates and add them to related_cands bitmaps. */
3592 :
3593 : static void
3594 507705 : record_important_candidates (struct ivopts_data *data)
3595 : {
3596 507705 : unsigned i;
3597 507705 : struct iv_group *group;
3598 :
3599 5157827 : for (i = 0; i < data->vcands.length (); i++)
3600 : {
3601 4650122 : struct iv_cand *cand = data->vcands[i];
3602 :
3603 4650122 : if (cand->important)
3604 3726735 : bitmap_set_bit (data->important_candidates, i);
3605 : }
3606 :
3607 507705 : data->consider_all_candidates = (data->vcands.length ()
3608 507705 : <= CONSIDER_ALL_CANDIDATES_BOUND);
3609 :
3610 : /* Add important candidates to groups' related_cands bitmaps. */
3611 2162452 : for (i = 0; i < data->vgroups.length (); i++)
3612 : {
3613 1654747 : group = data->vgroups[i];
3614 1654747 : bitmap_ior_into (group->related_cands, data->important_candidates);
3615 : }
3616 507705 : }
3617 :
3618 : /* Allocates the data structure mapping the (use, candidate) pairs to costs.
3619 : If consider_all_candidates is true, we use a two-dimensional array, otherwise
3620 : we allocate a simple list to every use. */
3621 :
3622 : static void
3623 507705 : alloc_use_cost_map (struct ivopts_data *data)
3624 : {
3625 507705 : unsigned i, size, s;
3626 :
3627 2162452 : for (i = 0; i < data->vgroups.length (); i++)
3628 : {
3629 1654747 : struct iv_group *group = data->vgroups[i];
3630 :
3631 1654747 : if (data->consider_all_candidates)
3632 1645456 : size = data->vcands.length ();
3633 : else
3634 : {
3635 9291 : s = bitmap_count_bits (group->related_cands);
3636 :
3637 : /* Round up to the power of two, so that moduling by it is fast. */
3638 18582 : size = s ? (1 << ceil_log2 (s)) : 1;
3639 : }
3640 :
3641 1654747 : group->n_map_members = size;
3642 1654747 : group->cost_map = XCNEWVEC (class cost_pair, size);
3643 : }
3644 507705 : }
3645 :
3646 : /* Sets cost of (GROUP, CAND) pair to COST and record that it depends
3647 : on invariants INV_VARS and that the value used in expressing it is
3648 : VALUE, and in case of iv elimination the comparison operator is COMP. */
3649 :
3650 : static void
3651 17775508 : set_group_iv_cost (struct ivopts_data *data,
3652 : struct iv_group *group, struct iv_cand *cand,
3653 : comp_cost cost, bitmap inv_vars, tree value,
3654 : enum tree_code comp, bitmap inv_exprs)
3655 : {
3656 17775508 : unsigned i, s;
3657 :
3658 17775508 : if (cost.infinite_cost_p ())
3659 : {
3660 6138368 : BITMAP_FREE (inv_vars);
3661 6138368 : BITMAP_FREE (inv_exprs);
3662 6138368 : return;
3663 : }
3664 :
3665 11637140 : if (data->consider_all_candidates)
3666 : {
3667 11503902 : group->cost_map[cand->id].cand = cand;
3668 11503902 : group->cost_map[cand->id].cost = cost;
3669 11503902 : group->cost_map[cand->id].inv_vars = inv_vars;
3670 11503902 : group->cost_map[cand->id].inv_exprs = inv_exprs;
3671 11503902 : group->cost_map[cand->id].value = value;
3672 11503902 : group->cost_map[cand->id].comp = comp;
3673 11503902 : return;
3674 : }
3675 :
3676 : /* n_map_members is a power of two, so this computes modulo. */
3677 133238 : s = cand->id & (group->n_map_members - 1);
3678 143079 : for (i = s; i < group->n_map_members; i++)
3679 143012 : if (!group->cost_map[i].cand)
3680 133171 : goto found;
3681 144 : for (i = 0; i < s; i++)
3682 144 : if (!group->cost_map[i].cand)
3683 67 : goto found;
3684 :
3685 0 : gcc_unreachable ();
3686 :
3687 133238 : found:
3688 133238 : group->cost_map[i].cand = cand;
3689 133238 : group->cost_map[i].cost = cost;
3690 133238 : group->cost_map[i].inv_vars = inv_vars;
3691 133238 : group->cost_map[i].inv_exprs = inv_exprs;
3692 133238 : group->cost_map[i].value = value;
3693 133238 : group->cost_map[i].comp = comp;
3694 : }
3695 :
3696 : /* Gets cost of (GROUP, CAND) pair. */
3697 :
3698 : static class cost_pair *
3699 212628775 : get_group_iv_cost (struct ivopts_data *data, struct iv_group *group,
3700 : struct iv_cand *cand)
3701 : {
3702 212628775 : unsigned i, s;
3703 212628775 : class cost_pair *ret;
3704 :
3705 212628775 : if (!cand)
3706 : return NULL;
3707 :
3708 206747433 : if (data->consider_all_candidates)
3709 : {
3710 192947205 : ret = group->cost_map + cand->id;
3711 192947205 : if (!ret->cand)
3712 : return NULL;
3713 :
3714 112956366 : return ret;
3715 : }
3716 :
3717 : /* n_map_members is a power of two, so this computes modulo. */
3718 13800228 : s = cand->id & (group->n_map_members - 1);
3719 19838579 : for (i = s; i < group->n_map_members; i++)
3720 19752218 : if (group->cost_map[i].cand == cand)
3721 : return group->cost_map + i;
3722 12265463 : else if (group->cost_map[i].cand == NULL)
3723 : return NULL;
3724 253860 : for (i = 0; i < s; i++)
3725 226657 : if (group->cost_map[i].cand == cand)
3726 : return group->cost_map + i;
3727 225145 : else if (group->cost_map[i].cand == NULL)
3728 : return NULL;
3729 :
3730 : return NULL;
3731 : }
3732 :
3733 : /* Produce DECL_RTL for object obj so it looks like it is stored in memory. */
3734 : static rtx
3735 42722 : produce_memory_decl_rtl (tree obj, int *regno)
3736 : {
3737 42722 : addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (obj));
3738 42722 : machine_mode address_mode = targetm.addr_space.address_mode (as);
3739 42722 : rtx x;
3740 :
3741 42722 : gcc_assert (obj);
3742 42722 : if (TREE_STATIC (obj) || DECL_EXTERNAL (obj))
3743 : {
3744 42722 : const char *name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (obj));
3745 42722 : x = gen_rtx_SYMBOL_REF (address_mode, name);
3746 42722 : SET_SYMBOL_REF_DECL (x, obj);
3747 42722 : x = gen_rtx_MEM (DECL_MODE (obj), x);
3748 42722 : set_mem_addr_space (x, as);
3749 42722 : targetm.encode_section_info (obj, x, true);
3750 : }
3751 : else
3752 : {
3753 0 : x = gen_raw_REG (address_mode, (*regno)++);
3754 0 : x = gen_rtx_MEM (DECL_MODE (obj), x);
3755 0 : set_mem_addr_space (x, as);
3756 : }
3757 :
3758 42722 : return x;
3759 : }
3760 :
3761 : /* Prepares decl_rtl for variables referred in *EXPR_P. Callback for
3762 : walk_tree. DATA contains the actual fake register number. */
3763 :
3764 : static tree
3765 598108 : prepare_decl_rtl (tree *expr_p, int *ws, void *data)
3766 : {
3767 598108 : tree obj = NULL_TREE;
3768 598108 : rtx x = NULL_RTX;
3769 598108 : int *regno = (int *) data;
3770 :
3771 598108 : switch (TREE_CODE (*expr_p))
3772 : {
3773 170888 : case ADDR_EXPR:
3774 170888 : for (expr_p = &TREE_OPERAND (*expr_p, 0);
3775 170888 : handled_component_p (*expr_p);
3776 0 : expr_p = &TREE_OPERAND (*expr_p, 0))
3777 0 : continue;
3778 170888 : obj = *expr_p;
3779 170888 : if (DECL_P (obj) && HAS_RTL_P (obj) && !DECL_RTL_SET_P (obj))
3780 0 : x = produce_memory_decl_rtl (obj, regno);
3781 : break;
3782 :
3783 0 : case SSA_NAME:
3784 0 : *ws = 0;
3785 0 : obj = SSA_NAME_VAR (*expr_p);
3786 : /* Defer handling of anonymous SSA_NAMEs to the expander. */
3787 0 : if (!obj)
3788 : return NULL_TREE;
3789 0 : if (!DECL_RTL_SET_P (obj))
3790 0 : x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
3791 : break;
3792 :
3793 170888 : case VAR_DECL:
3794 170888 : case PARM_DECL:
3795 170888 : case RESULT_DECL:
3796 170888 : *ws = 0;
3797 170888 : obj = *expr_p;
3798 :
3799 170888 : if (DECL_RTL_SET_P (obj))
3800 : break;
3801 :
3802 0 : if (DECL_MODE (obj) == BLKmode)
3803 0 : x = produce_memory_decl_rtl (obj, regno);
3804 : else
3805 0 : x = gen_raw_REG (DECL_MODE (obj), (*regno)++);
3806 :
3807 : break;
3808 :
3809 : default:
3810 : break;
3811 : }
3812 :
3813 0 : if (x)
3814 : {
3815 0 : decl_rtl_to_reset.safe_push (obj);
3816 0 : SET_DECL_RTL (obj, x);
3817 : }
3818 :
3819 : return NULL_TREE;
3820 : }
3821 :
3822 : /* Predict whether the given loop will be transformed in the RTL
3823 : doloop_optimize pass. Attempt to duplicate some doloop_optimize checks.
3824 : This is only for target independent checks, see targetm.predict_doloop_p
3825 : for the target dependent ones.
3826 :
3827 : Note that according to some initial investigation, some checks like costly
3828 : niter check and invalid stmt scanning don't have much gains among general
3829 : cases, so keep this as simple as possible first.
3830 :
3831 : Some RTL specific checks seems unable to be checked in gimple, if any new
3832 : checks or easy checks _are_ missing here, please add them. */
3833 :
3834 : static bool
3835 507705 : generic_predict_doloop_p (struct ivopts_data *data)
3836 : {
3837 507705 : class loop *loop = data->current_loop;
3838 :
3839 : /* Call target hook for target dependent checks. */
3840 507705 : if (!targetm.predict_doloop_p (loop))
3841 : {
3842 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
3843 67 : fprintf (dump_file, "Predict doloop failure due to"
3844 : " target specific checks.\n");
3845 : return false;
3846 : }
3847 :
3848 : /* Similar to doloop_optimize, check iteration description to know it's
3849 : suitable or not. Keep it as simple as possible, feel free to extend it
3850 : if you find any multiple exits cases matter. */
3851 0 : edge exit = single_dom_exit (loop);
3852 0 : class tree_niter_desc *niter_desc;
3853 0 : if (!exit || !(niter_desc = niter_for_exit (data, exit)))
3854 : {
3855 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3856 0 : fprintf (dump_file, "Predict doloop failure due to"
3857 : " unexpected niters.\n");
3858 : return false;
3859 : }
3860 :
3861 : /* Similar to doloop_optimize, check whether iteration count too small
3862 : and not profitable. */
3863 0 : HOST_WIDE_INT est_niter = get_estimated_loop_iterations_int (loop);
3864 0 : if (est_niter == -1)
3865 0 : est_niter = get_likely_max_loop_iterations_int (loop);
3866 0 : if (est_niter >= 0 && est_niter < 3)
3867 : {
3868 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3869 0 : fprintf (dump_file,
3870 : "Predict doloop failure due to"
3871 : " too few iterations (%u).\n",
3872 : (unsigned int) est_niter);
3873 : return false;
3874 : }
3875 :
3876 : return true;
3877 : }
3878 :
3879 : /* Determines cost of the computation of EXPR. */
3880 :
3881 : static unsigned
3882 256332 : computation_cost (tree expr, bool speed)
3883 : {
3884 256332 : rtx_insn *seq;
3885 256332 : rtx rslt;
3886 256332 : tree type = TREE_TYPE (expr);
3887 256332 : unsigned cost;
3888 : /* Avoid using hard regs in ways which may be unsupported. */
3889 256332 : int regno = LAST_VIRTUAL_REGISTER + 1;
3890 256332 : struct cgraph_node *node = cgraph_node::get (current_function_decl);
3891 256332 : enum node_frequency real_frequency = node->frequency;
3892 :
3893 256332 : node->frequency = NODE_FREQUENCY_NORMAL;
3894 256332 : crtl->maybe_hot_insn_p = speed;
3895 256332 : walk_tree (&expr, prepare_decl_rtl, ®no, NULL);
3896 256332 : start_sequence ();
3897 256332 : rslt = expand_expr (expr, NULL_RTX, TYPE_MODE (type), EXPAND_NORMAL);
3898 256332 : seq = end_sequence ();
3899 256332 : default_rtl_profile ();
3900 256332 : node->frequency = real_frequency;
3901 :
3902 256332 : cost = seq_cost (seq, speed);
3903 256332 : if (MEM_P (rslt))
3904 0 : cost += address_cost (XEXP (rslt, 0), TYPE_MODE (type),
3905 0 : TYPE_ADDR_SPACE (type), speed);
3906 256332 : else if (!REG_P (rslt))
3907 512664 : cost += set_src_cost (rslt, TYPE_MODE (type), speed);
3908 :
3909 256332 : return cost;
3910 : }
3911 :
3912 : /* Returns variable containing the value of candidate CAND at statement AT. */
3913 :
3914 : static tree
3915 18584790 : var_at_stmt (class loop *loop, struct iv_cand *cand, gimple *stmt)
3916 : {
3917 18584790 : if (stmt_after_increment (loop, cand, stmt))
3918 4785264 : return cand->var_after;
3919 : else
3920 13799526 : return cand->var_before;
3921 : }
3922 :
3923 : /* If A is (TYPE) BA and B is (TYPE) BB, and the types of BA and BB have the
3924 : same precision that is at least as wide as the precision of TYPE, stores
3925 : BA to A and BB to B, and returns the type of BA. Otherwise, returns the
3926 : type of A and B. */
3927 :
3928 : static tree
3929 14349769 : determine_common_wider_type (tree *a, tree *b)
3930 : {
3931 14349769 : tree wider_type = NULL;
3932 14349769 : tree suba, subb;
3933 14349769 : tree atype = TREE_TYPE (*a);
3934 :
3935 14349769 : if (CONVERT_EXPR_P (*a))
3936 : {
3937 8093616 : suba = TREE_OPERAND (*a, 0);
3938 8093616 : wider_type = TREE_TYPE (suba);
3939 8093616 : if (TYPE_PRECISION (wider_type) < TYPE_PRECISION (atype))
3940 : return atype;
3941 : }
3942 : else
3943 : return atype;
3944 :
3945 8075484 : if (CONVERT_EXPR_P (*b))
3946 : {
3947 1645580 : subb = TREE_OPERAND (*b, 0);
3948 1645580 : if (TYPE_PRECISION (wider_type) != TYPE_PRECISION (TREE_TYPE (subb)))
3949 : return atype;
3950 : }
3951 : else
3952 : return atype;
3953 :
3954 1574458 : *a = suba;
3955 1574458 : *b = subb;
3956 1574458 : return wider_type;
3957 : }
3958 :
3959 : /* Determines the expression by that USE is expressed from induction variable
3960 : CAND at statement AT in DATA's current loop. The expression is stored in
3961 : two parts in a decomposed form. The invariant part is stored in AFF_INV;
3962 : while variant part in AFF_VAR. Store ratio of CAND.step over USE.step in
3963 : PRAT if it's non-null. Returns false if USE cannot be expressed using
3964 : CAND. */
3965 :
3966 : static bool
3967 17315611 : get_computation_aff_1 (struct ivopts_data *data, gimple *at, struct iv_use *use,
3968 : struct iv_cand *cand, class aff_tree *aff_inv,
3969 : class aff_tree *aff_var, widest_int *prat = NULL)
3970 : {
3971 17315611 : tree ubase = use->iv->base, ustep = use->iv->step;
3972 17315611 : tree cbase = cand->iv->base, cstep = cand->iv->step;
3973 17315611 : tree common_type, uutype, var, cstep_common;
3974 17315611 : tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
3975 17315611 : aff_tree aff_cbase;
3976 17315611 : widest_int rat;
3977 :
3978 : /* We must have a precision to express the values of use. */
3979 17315611 : if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
3980 : return false;
3981 :
3982 17314556 : var = var_at_stmt (data->current_loop, cand, at);
3983 17314556 : uutype = unsigned_type_for (utype);
3984 :
3985 : /* If the conversion is not noop, perform it. */
3986 17314556 : if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
3987 : {
3988 263182 : if (cand->orig_iv != NULL && CONVERT_EXPR_P (cbase)
3989 1636022 : && (CONVERT_EXPR_P (cstep) || poly_int_tree_p (cstep)))
3990 : {
3991 32484 : tree inner_base, inner_step, inner_type;
3992 32484 : inner_base = TREE_OPERAND (cbase, 0);
3993 32484 : if (CONVERT_EXPR_P (cstep))
3994 1427 : inner_step = TREE_OPERAND (cstep, 0);
3995 : else
3996 : inner_step = cstep;
3997 :
3998 32484 : inner_type = TREE_TYPE (inner_base);
3999 : /* If candidate is added from a biv whose type is smaller than
4000 : ctype, we know both candidate and the biv won't overflow.
4001 : In this case, it's safe to skip the conversion in candidate.
4002 : As an example, (unsigned short)((unsigned long)A) equals to
4003 : (unsigned short)A, if A has a type no larger than short. */
4004 32484 : if (TYPE_PRECISION (inner_type) <= TYPE_PRECISION (uutype))
4005 : {
4006 31359 : cbase = inner_base;
4007 31359 : cstep = inner_step;
4008 : }
4009 : }
4010 1603538 : cbase = fold_convert (uutype, cbase);
4011 1603538 : cstep = fold_convert (uutype, cstep);
4012 1603538 : var = fold_convert (uutype, var);
4013 : }
4014 :
4015 : /* Ratio is 1 when computing the value of biv cand by itself.
4016 : We can't rely on constant_multiple_of in this case because the
4017 : use is created after the original biv is selected. The call
4018 : could fail because of inconsistent fold behavior. See PR68021
4019 : for more information. */
4020 17314556 : if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
4021 : {
4022 2842 : gcc_assert (is_gimple_assign (use->stmt));
4023 2842 : gcc_assert (use->iv->ssa_name == cand->var_after);
4024 2842 : gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
4025 2842 : rat = 1;
4026 : }
4027 17311714 : else if (!constant_multiple_of (ustep, cstep, &rat, data))
4028 : return false;
4029 :
4030 14349769 : if (prat)
4031 12867488 : *prat = rat;
4032 :
4033 : /* In case both UBASE and CBASE are shortened to UUTYPE from some common
4034 : type, we achieve better folding by computing their difference in this
4035 : wider type, and cast the result to UUTYPE. We do not need to worry about
4036 : overflows, as all the arithmetics will in the end be performed in UUTYPE
4037 : anyway. */
4038 14349769 : common_type = determine_common_wider_type (&ubase, &cbase);
4039 :
4040 : /* use = ubase - ratio * cbase + ratio * var. */
4041 14349769 : tree_to_aff_combination (ubase, common_type, aff_inv);
4042 14349769 : tree_to_aff_combination (cbase, common_type, &aff_cbase);
4043 14349769 : tree_to_aff_combination (var, uutype, aff_var);
4044 :
4045 : /* We need to shift the value if we are after the increment. */
4046 14349769 : if (stmt_after_increment (data->current_loop, cand, at))
4047 : {
4048 3263033 : aff_tree cstep_aff;
4049 :
4050 3263033 : if (common_type != uutype)
4051 844937 : cstep_common = fold_convert (common_type, cstep);
4052 : else
4053 : cstep_common = cstep;
4054 :
4055 3263033 : tree_to_aff_combination (cstep_common, common_type, &cstep_aff);
4056 3263033 : aff_combination_add (&aff_cbase, &cstep_aff);
4057 3263033 : }
4058 :
4059 14349769 : aff_combination_scale (&aff_cbase, -rat);
4060 14349769 : aff_combination_add (aff_inv, &aff_cbase);
4061 14349769 : if (common_type != uutype)
4062 9730199 : aff_combination_convert (aff_inv, uutype);
4063 :
4064 14349769 : aff_combination_scale (aff_var, rat);
4065 14349769 : return true;
4066 17315611 : }
4067 :
4068 : /* Determines the expression by that USE is expressed from induction variable
4069 : CAND at statement AT in DATA's current loop. The expression is stored in a
4070 : decomposed form into AFF. Returns false if USE cannot be expressed using
4071 : CAND. */
4072 :
4073 : static bool
4074 1270264 : get_computation_aff (struct ivopts_data *data, gimple *at, struct iv_use *use,
4075 : struct iv_cand *cand, class aff_tree *aff)
4076 : {
4077 1270264 : aff_tree aff_var;
4078 :
4079 1270264 : if (!get_computation_aff_1 (data, at, use, cand, aff, &aff_var))
4080 : return false;
4081 :
4082 1142442 : aff_combination_add (aff, &aff_var);
4083 1142442 : return true;
4084 1270264 : }
4085 :
4086 : /* Return the type of USE. */
4087 :
4088 : static tree
4089 1030276 : get_use_type (struct iv_use *use)
4090 : {
4091 1030276 : tree base_type = TREE_TYPE (use->iv->base);
4092 1030276 : tree type;
4093 :
4094 1030276 : if (use->type == USE_REF_ADDRESS)
4095 : {
4096 : /* The base_type may be a void pointer. Create a pointer type based on
4097 : the mem_ref instead. */
4098 0 : type = build_pointer_type (TREE_TYPE (*use->op_p));
4099 0 : gcc_assert (TYPE_ADDR_SPACE (TREE_TYPE (type))
4100 : == TYPE_ADDR_SPACE (TREE_TYPE (base_type)));
4101 : }
4102 : else
4103 : type = base_type;
4104 :
4105 1030276 : return type;
4106 : }
4107 :
4108 : /* Determines the expression by that USE is expressed from induction variable
4109 : CAND at statement AT in DATA's current loop. The computation is
4110 : unshared. */
4111 :
4112 : static tree
4113 404478 : get_computation_at (struct ivopts_data *data, gimple *at,
4114 : struct iv_use *use, struct iv_cand *cand)
4115 : {
4116 404478 : aff_tree aff;
4117 404478 : tree type = get_use_type (use);
4118 :
4119 404478 : if (!get_computation_aff (data, at, use, cand, &aff))
4120 : return NULL_TREE;
4121 276656 : unshare_aff_combination (&aff);
4122 276656 : return fold_convert (type, aff_combination_to_tree (&aff));
4123 404478 : }
4124 :
4125 : /* Like get_computation_at, but try harder, even if the computation
4126 : is more expensive. Intended for debug stmts. */
4127 :
4128 : static tree
4129 196745 : get_debug_computation_at (struct ivopts_data *data, gimple *at,
4130 : struct iv_use *use, struct iv_cand *cand)
4131 : {
4132 196745 : if (tree ret = get_computation_at (data, at, use, cand))
4133 : return ret;
4134 :
4135 127822 : tree ubase = use->iv->base, ustep = use->iv->step;
4136 127822 : tree cbase = cand->iv->base, cstep = cand->iv->step;
4137 127822 : tree var;
4138 127822 : tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
4139 127822 : widest_int rat;
4140 :
4141 : /* We must have a precision to express the values of use. */
4142 127822 : if (TYPE_PRECISION (utype) >= TYPE_PRECISION (ctype))
4143 : return NULL_TREE;
4144 :
4145 : /* Try to handle the case that get_computation_at doesn't,
4146 : try to express
4147 : use = ubase + (var - cbase) / ratio. */
4148 9800 : if (!constant_multiple_of (cstep, fold_convert (TREE_TYPE (cstep), ustep),
4149 : &rat, data))
4150 : return NULL_TREE;
4151 :
4152 8659 : bool neg_p = false;
4153 8659 : if (wi::neg_p (rat))
4154 : {
4155 849 : if (TYPE_UNSIGNED (ctype))
4156 : return NULL_TREE;
4157 0 : neg_p = true;
4158 0 : rat = wi::neg (rat);
4159 : }
4160 :
4161 : /* If both IVs can wrap around and CAND doesn't have a power of two step,
4162 : it is unsafe. Consider uint16_t CAND with step 9, when wrapping around,
4163 : the values will be ... 0xfff0, 0xfff9, 2, 11 ... and when use is say
4164 : uint8_t with step 3, those values divided by 3 cast to uint8_t will be
4165 : ... 0x50, 0x53, 0, 3 ... rather than expected 0x50, 0x53, 0x56, 0x59. */
4166 7810 : if (!use->iv->no_overflow
4167 62 : && !cand->iv->no_overflow
4168 7860 : && !integer_pow2p (cstep))
4169 : return NULL_TREE;
4170 :
4171 7799 : int bits = wi::exact_log2 (rat);
4172 7799 : if (bits == -1)
4173 718 : bits = wi::floor_log2 (rat) + 1;
4174 7799 : if (!cand->iv->no_overflow
4175 7799 : && TYPE_PRECISION (utype) + bits > TYPE_PRECISION (ctype))
4176 : return NULL_TREE;
4177 :
4178 7799 : var = var_at_stmt (data->current_loop, cand, at);
4179 :
4180 7799 : if (POINTER_TYPE_P (ctype))
4181 : {
4182 130 : ctype = unsigned_type_for (ctype);
4183 130 : cbase = fold_convert (ctype, cbase);
4184 130 : cstep = fold_convert (ctype, cstep);
4185 130 : var = fold_convert (ctype, var);
4186 : }
4187 :
4188 7799 : if (stmt_after_increment (data->current_loop, cand, at))
4189 76 : var = fold_build2 (MINUS_EXPR, TREE_TYPE (var), var,
4190 : unshare_expr (cstep));
4191 :
4192 7799 : var = fold_build2 (MINUS_EXPR, TREE_TYPE (var), var, cbase);
4193 7799 : var = fold_build2 (EXACT_DIV_EXPR, TREE_TYPE (var), var,
4194 : wide_int_to_tree (TREE_TYPE (var), rat));
4195 7799 : if (POINTER_TYPE_P (utype))
4196 : {
4197 0 : var = fold_convert (sizetype, var);
4198 0 : if (neg_p)
4199 0 : var = fold_build1 (NEGATE_EXPR, sizetype, var);
4200 0 : var = fold_build2 (POINTER_PLUS_EXPR, utype, ubase, var);
4201 : }
4202 : else
4203 : {
4204 7799 : var = fold_convert (utype, var);
4205 15598 : var = fold_build2 (neg_p ? MINUS_EXPR : PLUS_EXPR, utype,
4206 : ubase, var);
4207 : }
4208 : return var;
4209 196745 : }
4210 :
4211 : /* Adjust the cost COST for being in loop setup rather than loop body.
4212 : If we're optimizing for space, the loop setup overhead is constant;
4213 : if we're optimizing for speed, amortize it over the per-iteration cost.
4214 : If ROUND_UP_P is true, the result is round up rather than to zero when
4215 : optimizing for speed. */
4216 : static int64_t
4217 10418764 : adjust_setup_cost (struct ivopts_data *data, int64_t cost,
4218 : bool round_up_p = false)
4219 : {
4220 10418764 : if (cost == INFTY)
4221 : return cost;
4222 10418764 : else if (optimize_loop_for_speed_p (data->current_loop))
4223 : {
4224 8758625 : uint64_t niters = avg_loop_niter (data->current_loop);
4225 8758625 : if (niters > (uint64_t) cost)
4226 6942976 : return (round_up_p && cost != 0) ? 1 : 0;
4227 1815649 : return (cost + (round_up_p ? niters - 1 : 0)) / niters;
4228 : }
4229 : else
4230 : return cost;
4231 : }
4232 :
4233 : /* Calculate the SPEED or size cost of shiftadd EXPR in MODE. MULT is the
4234 : EXPR operand holding the shift. COST0 and COST1 are the costs for
4235 : calculating the operands of EXPR. Returns true if successful, and returns
4236 : the cost in COST. */
4237 :
4238 : static bool
4239 1425618 : get_shiftadd_cost (tree expr, scalar_int_mode mode, comp_cost cost0,
4240 : comp_cost cost1, tree mult, bool speed, comp_cost *cost)
4241 : {
4242 1425618 : comp_cost res;
4243 1425618 : tree op1 = TREE_OPERAND (expr, 1);
4244 1425618 : tree cst = TREE_OPERAND (mult, 1);
4245 1425618 : tree multop = TREE_OPERAND (mult, 0);
4246 1425618 : int m = exact_log2 (int_cst_value (cst));
4247 4276600 : int maxm = MIN (BITS_PER_WORD, GET_MODE_BITSIZE (mode));
4248 1425618 : int as_cost, sa_cost;
4249 1425618 : bool mult_in_op1;
4250 :
4251 1425618 : if (!(m >= 0 && m < maxm))
4252 : return false;
4253 :
4254 950169 : STRIP_NOPS (op1);
4255 950169 : mult_in_op1 = operand_equal_p (op1, mult, 0);
4256 :
4257 950169 : as_cost = add_cost (speed, mode) + shift_cost (speed, mode, m);
4258 :
4259 : /* If the target has a cheap shift-and-add or shift-and-sub instruction,
4260 : use that in preference to a shift insn followed by an add insn. */
4261 950169 : sa_cost = (TREE_CODE (expr) != MINUS_EXPR
4262 950169 : ? shiftadd_cost (speed, mode, m)
4263 : : (mult_in_op1
4264 132384 : ? shiftsub1_cost (speed, mode, m)
4265 28430 : : shiftsub0_cost (speed, mode, m)));
4266 :
4267 950169 : res = comp_cost (MIN (as_cost, sa_cost), 0);
4268 1744164 : res += (mult_in_op1 ? cost0 : cost1);
4269 :
4270 950169 : STRIP_NOPS (multop);
4271 950169 : if (!is_gimple_val (multop))
4272 488387 : res += force_expr_to_var_cost (multop, speed);
4273 :
4274 950169 : *cost = res;
4275 950169 : return true;
4276 : }
4277 :
4278 : /* Estimates cost of forcing expression EXPR into a variable. */
4279 :
4280 : static comp_cost
4281 28920159 : force_expr_to_var_cost (tree expr, bool speed)
4282 : {
4283 28920159 : static bool costs_initialized = false;
4284 28920159 : static unsigned integer_cost [2];
4285 28920159 : static unsigned symbol_cost [2];
4286 28920159 : static unsigned address_cost [2];
4287 28920159 : tree op0, op1;
4288 28920159 : comp_cost cost0, cost1, cost;
4289 28920159 : machine_mode mode;
4290 28920159 : scalar_int_mode int_mode;
4291 :
4292 28920159 : if (!costs_initialized)
4293 : {
4294 42722 : tree type = build_pointer_type (integer_type_node);
4295 42722 : tree var, addr;
4296 42722 : rtx x;
4297 42722 : int i;
4298 :
4299 42722 : var = create_tmp_var_raw (integer_type_node, "test_var");
4300 42722 : TREE_STATIC (var) = 1;
4301 42722 : x = produce_memory_decl_rtl (var, NULL);
4302 42722 : SET_DECL_RTL (var, x);
4303 :
4304 42722 : addr = build1 (ADDR_EXPR, type, var);
4305 :
4306 :
4307 170888 : for (i = 0; i < 2; i++)
4308 : {
4309 85444 : integer_cost[i] = computation_cost (build_int_cst (integer_type_node,
4310 : 2000), i);
4311 :
4312 85444 : symbol_cost[i] = computation_cost (addr, i) + 1;
4313 :
4314 85444 : address_cost[i]
4315 85444 : = computation_cost (fold_build_pointer_plus_hwi (addr, 2000), i) + 1;
4316 85444 : if (dump_file && (dump_flags & TDF_DETAILS))
4317 : {
4318 105 : fprintf (dump_file, "force_expr_to_var_cost %s costs:\n", i ? "speed" : "size");
4319 70 : fprintf (dump_file, " integer %d\n", (int) integer_cost[i]);
4320 70 : fprintf (dump_file, " symbol %d\n", (int) symbol_cost[i]);
4321 70 : fprintf (dump_file, " address %d\n", (int) address_cost[i]);
4322 70 : fprintf (dump_file, " other %d\n", (int) target_spill_cost[i]);
4323 70 : fprintf (dump_file, "\n");
4324 : }
4325 : }
4326 :
4327 42722 : costs_initialized = true;
4328 : }
4329 :
4330 28920159 : STRIP_NOPS (expr);
4331 :
4332 28920159 : if (SSA_VAR_P (expr))
4333 5403296 : return no_cost;
4334 :
4335 23516863 : if (is_gimple_min_invariant (expr))
4336 : {
4337 14117942 : if (poly_int_tree_p (expr))
4338 11984701 : return comp_cost (integer_cost [speed], 0);
4339 :
4340 2133241 : if (TREE_CODE (expr) == ADDR_EXPR)
4341 : {
4342 2133241 : tree obj = TREE_OPERAND (expr, 0);
4343 :
4344 2133241 : if (VAR_P (obj)
4345 : || TREE_CODE (obj) == PARM_DECL
4346 : || TREE_CODE (obj) == RESULT_DECL)
4347 2067241 : return comp_cost (symbol_cost [speed], 0);
4348 : }
4349 :
4350 66000 : return comp_cost (address_cost [speed], 0);
4351 : }
4352 :
4353 9398921 : switch (TREE_CODE (expr))
4354 : {
4355 8075780 : case POINTER_PLUS_EXPR:
4356 8075780 : case PLUS_EXPR:
4357 8075780 : case MINUS_EXPR:
4358 8075780 : case MULT_EXPR:
4359 8075780 : case EXACT_DIV_EXPR:
4360 8075780 : case TRUNC_DIV_EXPR:
4361 8075780 : case BIT_AND_EXPR:
4362 8075780 : case BIT_IOR_EXPR:
4363 8075780 : case LSHIFT_EXPR:
4364 8075780 : case RSHIFT_EXPR:
4365 8075780 : op0 = TREE_OPERAND (expr, 0);
4366 8075780 : op1 = TREE_OPERAND (expr, 1);
4367 8075780 : STRIP_NOPS (op0);
4368 8075780 : STRIP_NOPS (op1);
4369 8075780 : break;
4370 :
4371 1323101 : CASE_CONVERT:
4372 1323101 : case NEGATE_EXPR:
4373 1323101 : case BIT_NOT_EXPR:
4374 1323101 : op0 = TREE_OPERAND (expr, 0);
4375 1323101 : STRIP_NOPS (op0);
4376 1323101 : op1 = NULL_TREE;
4377 1323101 : break;
4378 : /* See add_iv_candidate_for_doloop, for doloop may_be_zero case, we
4379 : introduce COND_EXPR for IV base, need to support better cost estimation
4380 : for this COND_EXPR and tcc_comparison. */
4381 0 : case COND_EXPR:
4382 0 : op0 = TREE_OPERAND (expr, 1);
4383 0 : STRIP_NOPS (op0);
4384 0 : op1 = TREE_OPERAND (expr, 2);
4385 0 : STRIP_NOPS (op1);
4386 0 : break;
4387 0 : case LT_EXPR:
4388 0 : case LE_EXPR:
4389 0 : case GT_EXPR:
4390 0 : case GE_EXPR:
4391 0 : case EQ_EXPR:
4392 0 : case NE_EXPR:
4393 0 : case UNORDERED_EXPR:
4394 0 : case ORDERED_EXPR:
4395 0 : case UNLT_EXPR:
4396 0 : case UNLE_EXPR:
4397 0 : case UNGT_EXPR:
4398 0 : case UNGE_EXPR:
4399 0 : case UNEQ_EXPR:
4400 0 : case LTGT_EXPR:
4401 0 : case MAX_EXPR:
4402 0 : case MIN_EXPR:
4403 0 : op0 = TREE_OPERAND (expr, 0);
4404 0 : STRIP_NOPS (op0);
4405 0 : op1 = TREE_OPERAND (expr, 1);
4406 0 : STRIP_NOPS (op1);
4407 0 : break;
4408 :
4409 40 : default:
4410 : /* Just an arbitrary value, FIXME. */
4411 40 : return comp_cost (target_spill_cost[speed], 0);
4412 : }
4413 :
4414 9398881 : if (op0 == NULL_TREE
4415 9398881 : || TREE_CODE (op0) == SSA_NAME || CONSTANT_CLASS_P (op0))
4416 4428544 : cost0 = no_cost;
4417 : else
4418 4970337 : cost0 = force_expr_to_var_cost (op0, speed);
4419 :
4420 9398881 : if (op1 == NULL_TREE
4421 8075780 : || TREE_CODE (op1) == SSA_NAME || CONSTANT_CLASS_P (op1))
4422 8650135 : cost1 = no_cost;
4423 : else
4424 748746 : cost1 = force_expr_to_var_cost (op1, speed);
4425 :
4426 9398881 : mode = TYPE_MODE (TREE_TYPE (expr));
4427 9398881 : switch (TREE_CODE (expr))
4428 : {
4429 5672676 : case POINTER_PLUS_EXPR:
4430 5672676 : case PLUS_EXPR:
4431 5672676 : case MINUS_EXPR:
4432 5672676 : case NEGATE_EXPR:
4433 5672676 : cost = comp_cost (add_cost (speed, mode), 0);
4434 5672676 : if (TREE_CODE (expr) != NEGATE_EXPR)
4435 : {
4436 5534656 : tree mult = NULL_TREE;
4437 5534656 : comp_cost sa_cost;
4438 5534656 : if (TREE_CODE (op1) == MULT_EXPR)
4439 : mult = op1;
4440 5165050 : else if (TREE_CODE (op0) == MULT_EXPR)
4441 : mult = op0;
4442 :
4443 : if (mult != NULL_TREE
4444 4584487 : && is_a <scalar_int_mode> (mode, &int_mode)
4445 1674093 : && cst_and_fits_in_hwi (TREE_OPERAND (mult, 1))
4446 1425618 : && get_shiftadd_cost (expr, int_mode, cost0, cost1, mult,
4447 : speed, &sa_cost))
4448 950169 : return sa_cost;
4449 : }
4450 : break;
4451 :
4452 1172855 : CASE_CONVERT:
4453 1172855 : {
4454 1172855 : tree inner_mode, outer_mode;
4455 1172855 : outer_mode = TREE_TYPE (expr);
4456 1172855 : inner_mode = TREE_TYPE (op0);
4457 1172855 : cost = comp_cost (convert_cost (TYPE_MODE (outer_mode),
4458 1172855 : TYPE_MODE (inner_mode), speed), 0);
4459 : }
4460 1172855 : break;
4461 :
4462 2455948 : case MULT_EXPR:
4463 2455948 : if (cst_and_fits_in_hwi (op0))
4464 0 : cost = comp_cost (mult_by_coeff_cost (int_cst_value (op0),
4465 0 : mode, speed), 0);
4466 2455948 : else if (cst_and_fits_in_hwi (op1))
4467 1978573 : cost = comp_cost (mult_by_coeff_cost (int_cst_value (op1),
4468 1978573 : mode, speed), 0);
4469 : else
4470 477375 : return comp_cost (target_spill_cost [speed], 0);
4471 : break;
4472 :
4473 44000 : case EXACT_DIV_EXPR:
4474 44000 : case TRUNC_DIV_EXPR:
4475 : /* Division by power of two is usually cheap, so we allow it. Forbid
4476 : anything else. */
4477 44000 : if (integer_pow2p (TREE_OPERAND (expr, 1)))
4478 44000 : cost = comp_cost (add_cost (speed, mode), 0);
4479 : else
4480 0 : cost = comp_cost (target_spill_cost[speed], 0);
4481 : break;
4482 :
4483 53402 : case BIT_AND_EXPR:
4484 53402 : case BIT_IOR_EXPR:
4485 53402 : case BIT_NOT_EXPR:
4486 53402 : case LSHIFT_EXPR:
4487 53402 : case RSHIFT_EXPR:
4488 53402 : cost = comp_cost (add_cost (speed, mode), 0);
4489 53402 : break;
4490 0 : case COND_EXPR:
4491 0 : op0 = TREE_OPERAND (expr, 0);
4492 0 : STRIP_NOPS (op0);
4493 0 : if (op0 == NULL_TREE || TREE_CODE (op0) == SSA_NAME
4494 0 : || CONSTANT_CLASS_P (op0))
4495 0 : cost = no_cost;
4496 : else
4497 0 : cost = force_expr_to_var_cost (op0, speed);
4498 : break;
4499 0 : case LT_EXPR:
4500 0 : case LE_EXPR:
4501 0 : case GT_EXPR:
4502 0 : case GE_EXPR:
4503 0 : case EQ_EXPR:
4504 0 : case NE_EXPR:
4505 0 : case UNORDERED_EXPR:
4506 0 : case ORDERED_EXPR:
4507 0 : case UNLT_EXPR:
4508 0 : case UNLE_EXPR:
4509 0 : case UNGT_EXPR:
4510 0 : case UNGE_EXPR:
4511 0 : case UNEQ_EXPR:
4512 0 : case LTGT_EXPR:
4513 0 : case MAX_EXPR:
4514 0 : case MIN_EXPR:
4515 : /* Simply use add cost for now, FIXME if there is some more accurate cost
4516 : evaluation way. */
4517 0 : cost = comp_cost (add_cost (speed, mode), 0);
4518 0 : break;
4519 :
4520 0 : default:
4521 0 : gcc_unreachable ();
4522 : }
4523 :
4524 7971337 : cost += cost0;
4525 7971337 : cost += cost1;
4526 7971337 : return cost;
4527 : }
4528 :
4529 : /* Estimates cost of forcing EXPR into a variable. INV_VARS is a set of the
4530 : invariants the computation depends on. */
4531 :
4532 : static comp_cost
4533 24756651 : force_var_cost (struct ivopts_data *data, tree expr, bitmap *inv_vars)
4534 : {
4535 24756651 : if (!expr)
4536 2043962 : return no_cost;
4537 :
4538 22712689 : find_inv_vars (data, &expr, inv_vars);
4539 22712689 : return force_expr_to_var_cost (expr, data->speed);
4540 : }
4541 :
4542 : /* Returns cost of auto-modifying address expression in shape base + offset.
4543 : AINC_STEP is step size of the address IV. AINC_OFFSET is offset of the
4544 : address expression. The address expression has ADDR_MODE in addr space
4545 : AS. The memory access has MEM_MODE. SPEED means we are optimizing for
4546 : speed or size. */
4547 :
4548 : enum ainc_type
4549 : {
4550 : AINC_PRE_INC, /* Pre increment. */
4551 : AINC_PRE_DEC, /* Pre decrement. */
4552 : AINC_POST_INC, /* Post increment. */
4553 : AINC_POST_DEC, /* Post decrement. */
4554 : AINC_NONE /* Also the number of auto increment types. */
4555 : };
4556 :
4557 : struct ainc_cost_data
4558 : {
4559 : int64_t costs[AINC_NONE];
4560 : };
4561 :
4562 : static comp_cost
4563 1842859 : get_address_cost_ainc (poly_int64 ainc_step, poly_int64 ainc_offset,
4564 : machine_mode addr_mode, machine_mode mem_mode,
4565 : addr_space_t as, bool speed)
4566 : {
4567 1842859 : if (!USE_LOAD_PRE_DECREMENT (mem_mode)
4568 : && !USE_STORE_PRE_DECREMENT (mem_mode)
4569 : && !USE_LOAD_POST_DECREMENT (mem_mode)
4570 : && !USE_STORE_POST_DECREMENT (mem_mode)
4571 : && !USE_LOAD_PRE_INCREMENT (mem_mode)
4572 : && !USE_STORE_PRE_INCREMENT (mem_mode)
4573 : && !USE_LOAD_POST_INCREMENT (mem_mode)
4574 : && !USE_STORE_POST_INCREMENT (mem_mode))
4575 1842859 : return infinite_cost;
4576 :
4577 : static vec<ainc_cost_data *> ainc_cost_data_list;
4578 : unsigned idx = (unsigned) as * MAX_MACHINE_MODE + (unsigned) mem_mode;
4579 : if (idx >= ainc_cost_data_list.length ())
4580 : {
4581 : unsigned nsize = ((unsigned) as + 1) *MAX_MACHINE_MODE;
4582 :
4583 : gcc_assert (nsize > idx);
4584 : ainc_cost_data_list.safe_grow_cleared (nsize, true);
4585 : }
4586 :
4587 : ainc_cost_data *data = ainc_cost_data_list[idx];
4588 : if (data == NULL)
4589 : {
4590 : rtx reg = gen_raw_REG (addr_mode, LAST_VIRTUAL_REGISTER + 1);
4591 :
4592 : data = (ainc_cost_data *) xcalloc (1, sizeof (*data));
4593 : data->costs[AINC_PRE_DEC] = INFTY;
4594 : data->costs[AINC_POST_DEC] = INFTY;
4595 : data->costs[AINC_PRE_INC] = INFTY;
4596 : data->costs[AINC_POST_INC] = INFTY;
4597 : if (USE_LOAD_PRE_DECREMENT (mem_mode)
4598 : || USE_STORE_PRE_DECREMENT (mem_mode))
4599 : {
4600 : rtx addr = gen_rtx_PRE_DEC (addr_mode, reg);
4601 :
4602 : if (memory_address_addr_space_p (mem_mode, addr, as))
4603 : data->costs[AINC_PRE_DEC]
4604 : = address_cost (addr, mem_mode, as, speed);
4605 : }
4606 : if (USE_LOAD_POST_DECREMENT (mem_mode)
4607 : || USE_STORE_POST_DECREMENT (mem_mode))
4608 : {
4609 : rtx addr = gen_rtx_POST_DEC (addr_mode, reg);
4610 :
4611 : if (memory_address_addr_space_p (mem_mode, addr, as))
4612 : data->costs[AINC_POST_DEC]
4613 : = address_cost (addr, mem_mode, as, speed);
4614 : }
4615 : if (USE_LOAD_PRE_INCREMENT (mem_mode)
4616 : || USE_STORE_PRE_INCREMENT (mem_mode))
4617 : {
4618 : rtx addr = gen_rtx_PRE_INC (addr_mode, reg);
4619 :
4620 : if (memory_address_addr_space_p (mem_mode, addr, as))
4621 : data->costs[AINC_PRE_INC]
4622 : = address_cost (addr, mem_mode, as, speed);
4623 : }
4624 : if (USE_LOAD_POST_INCREMENT (mem_mode)
4625 : || USE_STORE_POST_INCREMENT (mem_mode))
4626 : {
4627 : rtx addr = gen_rtx_POST_INC (addr_mode, reg);
4628 :
4629 : if (memory_address_addr_space_p (mem_mode, addr, as))
4630 : data->costs[AINC_POST_INC]
4631 : = address_cost (addr, mem_mode, as, speed);
4632 : }
4633 : ainc_cost_data_list[idx] = data;
4634 : }
4635 :
4636 : poly_int64 msize = GET_MODE_SIZE (mem_mode);
4637 : if (known_eq (ainc_offset, 0) && known_eq (msize, ainc_step))
4638 : return comp_cost (data->costs[AINC_POST_INC], 0);
4639 : if (known_eq (ainc_offset, 0) && known_eq (msize, -ainc_step))
4640 : return comp_cost (data->costs[AINC_POST_DEC], 0);
4641 : if (known_eq (ainc_offset, msize) && known_eq (msize, ainc_step))
4642 : return comp_cost (data->costs[AINC_PRE_INC], 0);
4643 : if (known_eq (ainc_offset, -msize) && known_eq (msize, -ainc_step))
4644 : return comp_cost (data->costs[AINC_PRE_DEC], 0);
4645 :
4646 : return infinite_cost;
4647 : }
4648 :
4649 : /* Return cost of computing USE's address expression by using CAND.
4650 : AFF_INV and AFF_VAR represent invariant and variant parts of the
4651 : address expression, respectively. If AFF_INV is simple, store
4652 : the loop invariant variables which are depended by it in INV_VARS;
4653 : if AFF_INV is complicated, handle it as a new invariant expression
4654 : and record it in INV_EXPR. RATIO indicates multiple times between
4655 : steps of USE and CAND. If CAN_AUTOINC is nonNULL, store boolean
4656 : value to it indicating if this is an auto-increment address. */
4657 :
4658 : static comp_cost
4659 5584385 : get_address_cost (struct ivopts_data *data, struct iv_use *use,
4660 : struct iv_cand *cand, aff_tree *aff_inv,
4661 : aff_tree *aff_var, HOST_WIDE_INT ratio,
4662 : bitmap *inv_vars, iv_inv_expr_ent **inv_expr,
4663 : bool *can_autoinc, bool speed)
4664 : {
4665 5584385 : rtx addr;
4666 5584385 : bool simple_inv = true;
4667 5584385 : tree comp_inv = NULL_TREE, type = aff_var->type;
4668 5584385 : comp_cost var_cost = no_cost, cost = no_cost;
4669 5584385 : struct mem_address parts = {NULL_TREE, integer_one_node,
4670 5584385 : NULL_TREE, NULL_TREE, NULL_TREE};
4671 5584385 : machine_mode addr_mode = TYPE_MODE (type);
4672 5584385 : machine_mode mem_mode = TYPE_MODE (use->mem_type);
4673 5584385 : addr_space_t as = TYPE_ADDR_SPACE (TREE_TYPE (use->iv->base));
4674 : /* Only true if ratio != 1. */
4675 5584385 : bool ok_with_ratio_p = false;
4676 5584385 : bool ok_without_ratio_p = false;
4677 5584385 : code_helper code = ERROR_MARK;
4678 :
4679 5584385 : if (use->type == USE_PTR_ADDRESS)
4680 : {
4681 3717 : gcall *call = as_a<gcall *> (use->stmt);
4682 3717 : gcc_assert (gimple_call_internal_p (call));
4683 3717 : code = gimple_call_internal_fn (call);
4684 : }
4685 :
4686 5584385 : if (!aff_combination_const_p (aff_inv))
4687 : {
4688 3675306 : parts.index = integer_one_node;
4689 : /* Addressing mode "base + index". */
4690 3675306 : ok_without_ratio_p = valid_mem_ref_p (mem_mode, as, &parts, code);
4691 3675306 : if (ratio != 1)
4692 : {
4693 2785574 : parts.step = wide_int_to_tree (type, ratio);
4694 : /* Addressing mode "base + index << scale". */
4695 2785574 : ok_with_ratio_p = valid_mem_ref_p (mem_mode, as, &parts, code);
4696 2785574 : if (!ok_with_ratio_p)
4697 1703061 : parts.step = NULL_TREE;
4698 : }
4699 2592793 : if (ok_with_ratio_p || ok_without_ratio_p)
4700 : {
4701 3675306 : if (maybe_ne (aff_inv->offset, 0))
4702 : {
4703 2405549 : parts.offset = wide_int_to_tree (sizetype, aff_inv->offset);
4704 : /* Addressing mode "base + index [<< scale] + offset". */
4705 2405549 : if (!valid_mem_ref_p (mem_mode, as, &parts, code))
4706 299 : parts.offset = NULL_TREE;
4707 : else
4708 2405250 : aff_inv->offset = 0;
4709 : }
4710 :
4711 3675306 : move_fixed_address_to_symbol (&parts, aff_inv);
4712 : /* Base is fixed address and is moved to symbol part. */
4713 3675306 : if (parts.symbol != NULL_TREE && aff_combination_zero_p (aff_inv))
4714 457594 : parts.base = NULL_TREE;
4715 :
4716 : /* Addressing mode "symbol + base + index [<< scale] [+ offset]". */
4717 3675306 : if (parts.symbol != NULL_TREE
4718 3675306 : && !valid_mem_ref_p (mem_mode, as, &parts, code))
4719 : {
4720 7216 : aff_combination_add_elt (aff_inv, parts.symbol, 1);
4721 7216 : parts.symbol = NULL_TREE;
4722 : /* Reset SIMPLE_INV since symbol address needs to be computed
4723 : outside of address expression in this case. */
4724 7216 : simple_inv = false;
4725 : /* Symbol part is moved back to base part, it can't be NULL. */
4726 7216 : parts.base = integer_one_node;
4727 : }
4728 : }
4729 : else
4730 0 : parts.index = NULL_TREE;
4731 : }
4732 : else
4733 : {
4734 1909079 : poly_int64 ainc_step;
4735 1909079 : if (can_autoinc
4736 1909079 : && ratio == 1
4737 3818150 : && ptrdiff_tree_p (cand->iv->step, &ainc_step))
4738 : {
4739 1842859 : poly_int64 ainc_offset = (aff_inv->offset).force_shwi ();
4740 :
4741 1842859 : if (stmt_after_increment (data->current_loop, cand, use->stmt))
4742 : ainc_offset += ainc_step;
4743 1842859 : cost = get_address_cost_ainc (ainc_step, ainc_offset,
4744 : addr_mode, mem_mode, as, speed);
4745 1842859 : if (!cost.infinite_cost_p ())
4746 : {
4747 0 : *can_autoinc = true;
4748 0 : return cost;
4749 : }
4750 1842859 : cost = no_cost;
4751 : }
4752 1909079 : if (!aff_combination_zero_p (aff_inv))
4753 : {
4754 1082919 : parts.offset = wide_int_to_tree (sizetype, aff_inv->offset);
4755 : /* Addressing mode "base + offset". */
4756 1082919 : if (!valid_mem_ref_p (mem_mode, as, &parts, code))
4757 38 : parts.offset = NULL_TREE;
4758 : else
4759 1082881 : aff_inv->offset = 0;
4760 : }
4761 : }
4762 :
4763 1916295 : if (simple_inv)
4764 5577169 : simple_inv = (aff_inv == NULL
4765 8801183 : || aff_combination_const_p (aff_inv)
4766 8793967 : || aff_combination_singleton_var_p (aff_inv));
4767 5584385 : if (!aff_combination_zero_p (aff_inv))
4768 3224082 : comp_inv = aff_combination_to_tree (aff_inv);
4769 3224082 : if (comp_inv != NULL_TREE)
4770 3224082 : cost = force_var_cost (data, comp_inv, inv_vars);
4771 5584385 : if (ratio != 1 && parts.step == NULL_TREE)
4772 1703069 : var_cost += mult_by_coeff_cost (ratio, addr_mode, speed);
4773 5584385 : if (comp_inv != NULL_TREE && parts.index == NULL_TREE)
4774 38 : var_cost += add_cost (speed, addr_mode);
4775 :
4776 5584385 : if (comp_inv && inv_expr && !simple_inv)
4777 : {
4778 743368 : *inv_expr = get_loop_invariant_expr (data, comp_inv);
4779 : /* Clear depends on. */
4780 743368 : if (*inv_expr != NULL && inv_vars && *inv_vars)
4781 430000 : bitmap_clear (*inv_vars);
4782 :
4783 : /* Cost of small invariant expression adjusted against loop niters
4784 : is usually zero, which makes it difficult to be differentiated
4785 : from candidate based on loop invariant variables. Secondly, the
4786 : generated invariant expression may not be hoisted out of loop by
4787 : following pass. We penalize the cost by rounding up in order to
4788 : neutralize such effects. */
4789 743368 : cost.cost = adjust_setup_cost (data, cost.cost, true);
4790 743368 : cost.scratch = cost.cost;
4791 : }
4792 :
4793 5584385 : cost += var_cost;
4794 5584385 : addr = addr_for_mem_ref (&parts, as, false);
4795 5584385 : gcc_assert (memory_address_addr_space_p (mem_mode, addr, as));
4796 5584385 : cost += address_cost (addr, mem_mode, as, speed);
4797 :
4798 5584385 : if (parts.symbol != NULL_TREE)
4799 510943 : cost.complexity += 1;
4800 : /* Don't increase the complexity of adding a scaled index if it's
4801 : the only kind of index that the target allows. */
4802 5584385 : if (parts.step != NULL_TREE && ok_without_ratio_p)
4803 1082513 : cost.complexity += 1;
4804 5584385 : if (parts.base != NULL_TREE && parts.index != NULL_TREE)
4805 3224044 : cost.complexity += 1;
4806 5584385 : if (parts.offset != NULL_TREE && !integer_zerop (parts.offset))
4807 3488131 : cost.complexity += 1;
4808 :
4809 : return cost;
4810 : }
4811 :
4812 : /* Scale (multiply) the computed COST (except scratch part that should be
4813 : hoisted out a loop) by header->frequency / AT->frequency, which makes
4814 : expected cost more accurate. */
4815 :
4816 : static comp_cost
4817 12867488 : get_scaled_computation_cost_at (ivopts_data *data, gimple *at, comp_cost cost)
4818 : {
4819 12867488 : if (data->speed
4820 12867488 : && data->current_loop->header->count.to_frequency (cfun) > 0)
4821 : {
4822 11252576 : basic_block bb = gimple_bb (at);
4823 11252576 : gcc_assert (cost.scratch <= cost.cost);
4824 11252576 : int scale_factor = (int)(intptr_t) bb->aux;
4825 11252576 : if (scale_factor == 1)
4826 10721584 : return cost;
4827 :
4828 530992 : int64_t scaled_cost
4829 530992 : = cost.scratch + (cost.cost - cost.scratch) * scale_factor;
4830 :
4831 530992 : if (dump_file && (dump_flags & TDF_DETAILS))
4832 93 : fprintf (dump_file, "Scaling cost based on bb prob by %2.2f: "
4833 : "%" PRId64 " (scratch: %" PRId64 ") -> %" PRId64 "\n",
4834 : 1.0f * scale_factor, cost.cost, cost.scratch, scaled_cost);
4835 :
4836 : cost.cost = scaled_cost;
4837 : }
4838 :
4839 2145904 : return cost;
4840 : }
4841 :
4842 : /* Determines the cost of the computation by that USE is expressed
4843 : from induction variable CAND. If ADDRESS_P is true, we just need
4844 : to create an address from it, otherwise we want to get it into
4845 : register. A set of invariants we depend on is stored in INV_VARS.
4846 : If CAN_AUTOINC is nonnull, use it to record whether autoinc
4847 : addressing is likely. If INV_EXPR is nonnull, record invariant
4848 : expr entry in it. */
4849 :
4850 : static comp_cost
4851 20246406 : get_computation_cost (struct ivopts_data *data, struct iv_use *use,
4852 : struct iv_cand *cand, bool address_p, bitmap *inv_vars,
4853 : bool *can_autoinc, iv_inv_expr_ent **inv_expr)
4854 : {
4855 20246406 : gimple *at = use->stmt;
4856 20246406 : tree ubase = use->iv->base, cbase = cand->iv->base;
4857 20246406 : tree utype = TREE_TYPE (ubase), ctype = TREE_TYPE (cbase);
4858 20246406 : tree comp_inv = NULL_TREE;
4859 20246406 : HOST_WIDE_INT ratio, aratio;
4860 20246406 : comp_cost cost;
4861 20246406 : widest_int rat;
4862 40492812 : aff_tree aff_inv, aff_var;
4863 20246406 : bool speed = optimize_bb_for_speed_p (gimple_bb (at));
4864 :
4865 20246406 : if (inv_vars)
4866 17717186 : *inv_vars = NULL;
4867 20246406 : if (can_autoinc)
4868 8827449 : *can_autoinc = false;
4869 20246406 : if (inv_expr)
4870 19828348 : *inv_expr = NULL;
4871 :
4872 : /* Check if we have enough precision to express the values of use. */
4873 20246406 : if (TYPE_PRECISION (utype) > TYPE_PRECISION (ctype))
4874 3070621 : return infinite_cost;
4875 :
4876 17175785 : if (address_p
4877 17175785 : || (use->iv->base_object
4878 2139043 : && cand->iv->base_object
4879 1055266 : && POINTER_TYPE_P (TREE_TYPE (use->iv->base_object))
4880 1043636 : && POINTER_TYPE_P (TREE_TYPE (cand->iv->base_object))))
4881 : {
4882 : /* Do not try to express address of an object with computation based
4883 : on address of a different object. This may cause problems in rtl
4884 : level alias analysis (that does not expect this to be happening,
4885 : as this is illegal in C), and would be unlikely to be useful
4886 : anyway. */
4887 8008180 : if (use->iv->base_object
4888 8008180 : && cand->iv->base_object
4889 12318531 : && !operand_equal_p (use->iv->base_object, cand->iv->base_object, 0))
4890 1470277 : return infinite_cost;
4891 : }
4892 :
4893 15705508 : if (!get_computation_aff_1 (data, at, use, cand, &aff_inv, &aff_var, &rat)
4894 15705508 : || !wi::fits_shwi_p (rat))
4895 2838020 : return infinite_cost;
4896 :
4897 12867488 : ratio = rat.to_shwi ();
4898 12867488 : if (address_p)
4899 : {
4900 5584385 : cost = get_address_cost (data, use, cand, &aff_inv, &aff_var, ratio,
4901 : inv_vars, inv_expr, can_autoinc, speed);
4902 5584385 : cost = get_scaled_computation_cost_at (data, at, cost);
4903 : /* For doloop IV cand, add on the extra cost. */
4904 5584385 : cost += cand->doloop_p ? targetm.doloop_cost_for_address : 0;
4905 5584385 : return cost;
4906 : }
4907 :
4908 7283103 : bool simple_inv = (aff_combination_const_p (&aff_inv)
4909 1983645 : || aff_combination_singleton_var_p (&aff_inv));
4910 7283103 : tree signed_type = signed_type_for (aff_combination_type (&aff_inv));
4911 7283103 : aff_combination_convert (&aff_inv, signed_type);
4912 7283103 : if (!aff_combination_zero_p (&aff_inv))
4913 5239141 : comp_inv = aff_combination_to_tree (&aff_inv);
4914 :
4915 7283103 : cost = force_var_cost (data, comp_inv, inv_vars);
4916 7283103 : if (comp_inv && inv_expr && !simple_inv)
4917 : {
4918 1376484 : *inv_expr = get_loop_invariant_expr (data, comp_inv);
4919 : /* Clear depends on. */
4920 1376484 : if (*inv_expr != NULL && inv_vars && *inv_vars)
4921 826034 : bitmap_clear (*inv_vars);
4922 :
4923 1376484 : cost.cost = adjust_setup_cost (data, cost.cost);
4924 : /* Record setup cost in scratch field. */
4925 1376484 : cost.scratch = cost.cost;
4926 : }
4927 : /* Cost of constant integer can be covered when adding invariant part to
4928 : variant part. */
4929 5906619 : else if (comp_inv && CONSTANT_CLASS_P (comp_inv))
4930 3255471 : cost = no_cost;
4931 :
4932 : /* Need type narrowing to represent use with cand. */
4933 7283103 : if (TYPE_PRECISION (utype) < TYPE_PRECISION (ctype))
4934 : {
4935 803738 : machine_mode outer_mode = TYPE_MODE (utype);
4936 803738 : machine_mode inner_mode = TYPE_MODE (ctype);
4937 803738 : cost += comp_cost (convert_cost (outer_mode, inner_mode, speed), 0);
4938 : }
4939 :
4940 : /* Turn a + i * (-c) into a - i * c. */
4941 7283103 : if (ratio < 0 && comp_inv && !integer_zerop (comp_inv))
4942 1853610 : aratio = -ratio;
4943 : else
4944 : aratio = ratio;
4945 :
4946 7283103 : if (ratio != 1)
4947 2732834 : cost += mult_by_coeff_cost (aratio, TYPE_MODE (utype), speed);
4948 :
4949 : /* TODO: We may also need to check if we can compute a + i * 4 in one
4950 : instruction. */
4951 : /* Need to add up the invariant and variant parts. */
4952 7283103 : if (comp_inv && !integer_zerop (comp_inv))
4953 10471914 : cost += add_cost (speed, TYPE_MODE (utype));
4954 :
4955 7283103 : cost = get_scaled_computation_cost_at (data, at, cost);
4956 :
4957 : /* For doloop IV cand, add on the extra cost. */
4958 7283103 : if (cand->doloop_p && use->type == USE_NONLINEAR_EXPR)
4959 0 : cost += targetm.doloop_cost_for_generic;
4960 :
4961 7283103 : return cost;
4962 20246406 : }
4963 :
4964 : /* Determines cost of computing the use in GROUP with CAND in a generic
4965 : expression. */
4966 :
4967 : static bool
4968 5526953 : determine_group_iv_cost_generic (struct ivopts_data *data,
4969 : struct iv_group *group, struct iv_cand *cand)
4970 : {
4971 5526953 : comp_cost cost;
4972 5526953 : iv_inv_expr_ent *inv_expr = NULL;
4973 5526953 : bitmap inv_vars = NULL, inv_exprs = NULL;
4974 5526953 : struct iv_use *use = group->vuses[0];
4975 :
4976 : /* The simple case first -- if we need to express value of the preserved
4977 : original biv, the cost is 0. This also prevents us from counting the
4978 : cost of increment twice -- once at this use and once in the cost of
4979 : the candidate. */
4980 5526953 : if (cand->pos == IP_ORIGINAL && cand->incremented_at == use->stmt)
4981 58322 : cost = no_cost;
4982 : /* If the IV candidate involves undefined SSA values and is not the
4983 : same IV as on the USE avoid using that candidate here. */
4984 5468631 : else if (cand->involves_undefs
4985 5468631 : && (!use->iv || !operand_equal_p (cand->iv->base, use->iv->base, 0)))
4986 : return false;
4987 : else
4988 5468403 : cost = get_computation_cost (data, use, cand, false,
4989 : &inv_vars, NULL, &inv_expr);
4990 :
4991 5526725 : if (inv_expr)
4992 : {
4993 968129 : inv_exprs = BITMAP_ALLOC (NULL);
4994 968129 : bitmap_set_bit (inv_exprs, inv_expr->id);
4995 : }
4996 5526725 : set_group_iv_cost (data, group, cand, cost, inv_vars,
4997 : NULL_TREE, ERROR_MARK, inv_exprs);
4998 5526725 : return !cost.infinite_cost_p ();
4999 : }
5000 :
5001 : /* Determines cost of computing uses in GROUP with CAND in addresses. */
5002 :
5003 : static bool
5004 6298229 : determine_group_iv_cost_address (struct ivopts_data *data,
5005 : struct iv_group *group, struct iv_cand *cand)
5006 : {
5007 6298229 : unsigned i;
5008 6298229 : bitmap inv_vars = NULL, inv_exprs = NULL;
5009 6298229 : bool can_autoinc;
5010 6298229 : iv_inv_expr_ent *inv_expr = NULL;
5011 6298229 : struct iv_use *use = group->vuses[0];
5012 6298229 : comp_cost sum_cost = no_cost, cost;
5013 :
5014 6298229 : cost = get_computation_cost (data, use, cand, true,
5015 : &inv_vars, &can_autoinc, &inv_expr);
5016 :
5017 6298229 : if (inv_expr)
5018 : {
5019 458459 : inv_exprs = BITMAP_ALLOC (NULL);
5020 458459 : bitmap_set_bit (inv_exprs, inv_expr->id);
5021 : }
5022 6298229 : sum_cost = cost;
5023 6298229 : if (!sum_cost.infinite_cost_p () && cand->ainc_use == use)
5024 : {
5025 0 : if (can_autoinc)
5026 0 : sum_cost -= cand->cost_step;
5027 : /* If we generated the candidate solely for exploiting autoincrement
5028 : opportunities, and it turns out it can't be used, set the cost to
5029 : infinity to make sure we ignore it. */
5030 0 : else if (cand->pos == IP_AFTER_USE || cand->pos == IP_BEFORE_USE)
5031 0 : sum_cost = infinite_cost;
5032 : }
5033 :
5034 : /* Compute and add costs for rest uses of this group. */
5035 8409391 : for (i = 1; i < group->vuses.length () && !sum_cost.infinite_cost_p (); i++)
5036 : {
5037 2111162 : struct iv_use *next = group->vuses[i];
5038 :
5039 : /* TODO: We could skip computing cost for sub iv_use when it has the
5040 : same cost as the first iv_use, but the cost really depends on the
5041 : offset and where the iv_use is. */
5042 2111162 : cost = get_computation_cost (data, next, cand, true,
5043 : NULL, &can_autoinc, &inv_expr);
5044 2111162 : if (inv_expr)
5045 : {
5046 284674 : if (!inv_exprs)
5047 78 : inv_exprs = BITMAP_ALLOC (NULL);
5048 :
5049 : /* Uses in a group can share setup code,
5050 : so only add setup cost once. */
5051 284674 : if (bitmap_bit_p (inv_exprs, inv_expr->id))
5052 284320 : cost -= cost.scratch;
5053 : else
5054 354 : bitmap_set_bit (inv_exprs, inv_expr->id);
5055 : }
5056 2111162 : sum_cost += cost;
5057 : }
5058 6298229 : set_group_iv_cost (data, group, cand, sum_cost, inv_vars,
5059 : NULL_TREE, ERROR_MARK, inv_exprs);
5060 :
5061 6298229 : return !sum_cost.infinite_cost_p ();
5062 : }
5063 :
5064 : /* Computes value of candidate CAND at position AT in iteration DESC->NITER,
5065 : and stores it to VAL. */
5066 :
5067 : static void
5068 3893924 : cand_value_at (class loop *loop, struct iv_cand *cand, gimple *at,
5069 : class tree_niter_desc *desc, aff_tree *val)
5070 : {
5071 11681772 : aff_tree step, delta, nit;
5072 3893924 : struct iv *iv = cand->iv;
5073 3893924 : tree type = TREE_TYPE (iv->base);
5074 3893924 : tree niter = desc->niter;
5075 3893924 : bool after_adjust = stmt_after_increment (loop, cand, at);
5076 3893924 : tree steptype;
5077 :
5078 3893924 : if (POINTER_TYPE_P (type))
5079 108904 : steptype = sizetype;
5080 : else
5081 3785020 : steptype = unsigned_type_for (type);
5082 :
5083 : /* If AFTER_ADJUST is required, the code below generates the equivalent
5084 : of BASE + NITER * STEP + STEP, when ideally we'd prefer the expression
5085 : BASE + (NITER + 1) * STEP, especially when NITER is often of the form
5086 : SSA_NAME - 1. Unfortunately, guaranteeing that adding 1 to NITER
5087 : doesn't overflow is tricky, so we peek inside the TREE_NITER_DESC
5088 : class for common idioms that we know are safe. */
5089 3893924 : if (after_adjust
5090 3664326 : && desc->control.no_overflow
5091 3656853 : && integer_onep (desc->control.step)
5092 983311 : && (desc->cmp == LT_EXPR
5093 36827 : || desc->cmp == NE_EXPR)
5094 4877235 : && TREE_CODE (desc->bound) == SSA_NAME)
5095 : {
5096 504931 : if (integer_onep (desc->control.base))
5097 : {
5098 381463 : niter = desc->bound;
5099 381463 : after_adjust = false;
5100 : }
5101 123468 : else if (TREE_CODE (niter) == MINUS_EXPR
5102 123468 : && integer_onep (TREE_OPERAND (niter, 1)))
5103 : {
5104 69624 : niter = TREE_OPERAND (niter, 0);
5105 69624 : after_adjust = false;
5106 : }
5107 : }
5108 :
5109 3893924 : tree_to_aff_combination (iv->step, TREE_TYPE (iv->step), &step);
5110 3893924 : aff_combination_convert (&step, steptype);
5111 3893924 : tree_to_aff_combination (niter, TREE_TYPE (niter), &nit);
5112 3893924 : aff_combination_convert (&nit, steptype);
5113 3893924 : aff_combination_mult (&nit, &step, &delta);
5114 3893924 : if (after_adjust)
5115 3213239 : aff_combination_add (&delta, &step);
5116 :
5117 3893924 : tree_to_aff_combination (iv->base, type, val);
5118 3893924 : if (!POINTER_TYPE_P (type))
5119 3785020 : aff_combination_convert (val, steptype);
5120 3893924 : aff_combination_add (val, &delta);
5121 3893924 : }
5122 :
5123 : /* Returns period of induction variable iv. */
5124 :
5125 : static tree
5126 4101175 : iv_period (struct iv *iv)
5127 : {
5128 4101175 : tree step = iv->step, period, type;
5129 4101175 : tree pow2div;
5130 :
5131 4101175 : gcc_assert (step && TREE_CODE (step) == INTEGER_CST);
5132 :
5133 4101175 : type = unsigned_type_for (TREE_TYPE (step));
5134 : /* Period of the iv is lcm (step, type_range)/step -1,
5135 : i.e., N*type_range/step - 1. Since type range is power
5136 : of two, N == (step >> num_of_ending_zeros_binary (step),
5137 : so the final result is
5138 :
5139 : (type_range >> num_of_ending_zeros_binary (step)) - 1
5140 :
5141 : */
5142 4101175 : pow2div = num_ending_zeros (step);
5143 :
5144 12303525 : period = build_low_bits_mask (type,
5145 4101175 : (TYPE_PRECISION (type)
5146 4101175 : - tree_to_uhwi (pow2div)));
5147 :
5148 4101175 : return period;
5149 : }
5150 :
5151 : /* Returns the comparison operator used when eliminating the iv USE. */
5152 :
5153 : static enum tree_code
5154 3893924 : iv_elimination_compare (struct ivopts_data *data, struct iv_use *use)
5155 : {
5156 3893924 : class loop *loop = data->current_loop;
5157 3893924 : basic_block ex_bb;
5158 3893924 : edge exit;
5159 :
5160 3893924 : ex_bb = gimple_bb (use->stmt);
5161 3893924 : exit = EDGE_SUCC (ex_bb, 0);
5162 3893924 : if (flow_bb_inside_loop_p (loop, exit->dest))
5163 2903856 : exit = EDGE_SUCC (ex_bb, 1);
5164 :
5165 3893924 : return (exit->flags & EDGE_TRUE_VALUE ? EQ_EXPR : NE_EXPR);
5166 : }
5167 :
5168 : /* Returns true if we can prove that BASE - OFFSET does not overflow. For now,
5169 : we only detect the situation that BASE = SOMETHING + OFFSET, where the
5170 : calculation is performed in non-wrapping type.
5171 :
5172 : TODO: More generally, we could test for the situation that
5173 : BASE = SOMETHING + OFFSET' and OFFSET is between OFFSET' and zero.
5174 : This would require knowing the sign of OFFSET. */
5175 :
5176 : static bool
5177 490 : difference_cannot_overflow_p (struct ivopts_data *data, tree base, tree offset)
5178 : {
5179 490 : enum tree_code code;
5180 490 : tree e1, e2;
5181 1470 : aff_tree aff_e1, aff_e2, aff_offset;
5182 :
5183 490 : if (!nowrap_type_p (TREE_TYPE (base)))
5184 : return false;
5185 :
5186 490 : base = expand_simple_operations (base);
5187 :
5188 490 : if (TREE_CODE (base) == SSA_NAME)
5189 : {
5190 489 : gimple *stmt = SSA_NAME_DEF_STMT (base);
5191 :
5192 489 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
5193 : return false;
5194 :
5195 32 : code = gimple_assign_rhs_code (stmt);
5196 32 : if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
5197 : return false;
5198 :
5199 19 : e1 = gimple_assign_rhs1 (stmt);
5200 19 : e2 = gimple_assign_rhs2 (stmt);
5201 : }
5202 : else
5203 : {
5204 1 : code = TREE_CODE (base);
5205 1 : if (get_gimple_rhs_class (code) != GIMPLE_BINARY_RHS)
5206 : return false;
5207 0 : e1 = TREE_OPERAND (base, 0);
5208 0 : e2 = TREE_OPERAND (base, 1);
5209 : }
5210 :
5211 : /* Use affine expansion as deeper inspection to prove the equality. */
5212 19 : tree_to_aff_combination_expand (e2, TREE_TYPE (e2),
5213 : &aff_e2, &data->name_expansion_cache);
5214 19 : tree_to_aff_combination_expand (offset, TREE_TYPE (offset),
5215 : &aff_offset, &data->name_expansion_cache);
5216 19 : aff_combination_scale (&aff_offset, -1);
5217 19 : switch (code)
5218 : {
5219 1 : case PLUS_EXPR:
5220 1 : aff_combination_add (&aff_e2, &aff_offset);
5221 1 : if (aff_combination_zero_p (&aff_e2))
5222 : return true;
5223 :
5224 1 : tree_to_aff_combination_expand (e1, TREE_TYPE (e1),
5225 : &aff_e1, &data->name_expansion_cache);
5226 1 : aff_combination_add (&aff_e1, &aff_offset);
5227 1 : return aff_combination_zero_p (&aff_e1);
5228 :
5229 18 : case POINTER_PLUS_EXPR:
5230 18 : aff_combination_add (&aff_e2, &aff_offset);
5231 18 : return aff_combination_zero_p (&aff_e2);
5232 :
5233 : default:
5234 : return false;
5235 : }
5236 490 : }
5237 :
5238 : /* Return true if STEP * VAL, computed in OFF_TYPE, is known to be a
5239 : non-negative offset which does not overflow, i.e. if the value of VAL is
5240 : non-negative and multiplying it by STEP fits in the signed range of
5241 : OFF_TYPE. */
5242 :
5243 : static bool
5244 28 : nonneg_scaled_offset_p (tree val, HOST_WIDE_INT step, tree off_type)
5245 : {
5246 28 : if (!INTEGRAL_TYPE_P (TREE_TYPE (val)) || step == 0)
5247 : return false;
5248 :
5249 28 : signop sgn = TYPE_SIGN (TREE_TYPE (val));
5250 28 : int_range_max r;
5251 56 : if (!get_range_query (cfun)->range_of_expr (r, val)
5252 28 : || r.undefined_p ()
5253 84 : || wi::neg_p (r.lower_bound (), sgn))
5254 : return false;
5255 :
5256 28 : widest_int max = widest_int::from (r.upper_bound (), sgn);
5257 28 : widest_int limit
5258 28 : = widest_int::from (wi::max_value (TYPE_PRECISION (off_type), SIGNED),
5259 28 : SIGNED);
5260 28 : return wi::leu_p (max, wi::udiv_trunc (limit, absu_hwi (step)));
5261 28 : }
5262 :
5263 : /* Tries to replace loop exit by one formulated in terms of a LT_EXPR
5264 : comparison with CAND. NITER describes the number of iterations of
5265 : the loops. If successful, the comparison in COMP_P is altered accordingly
5266 : and the bound in BOUND_P is recomputed.
5267 :
5268 : We aim to handle the following situation:
5269 :
5270 : sometype *base, *p;
5271 : unsigned a, b, i;
5272 :
5273 : i = a;
5274 : p = p_0 = base + a;
5275 :
5276 : do
5277 : {
5278 : bla (*p);
5279 : p++;
5280 : i++;
5281 : }
5282 : while (i < b);
5283 :
5284 : Here, the number of iterations of the loop is (a + 1 > b) ? 0 : b - a - 1.
5285 : We aim to optimize this to
5286 :
5287 : p = p_0 = base + a;
5288 : do
5289 : {
5290 : bla (*p);
5291 : p++;
5292 : }
5293 : while (p < p_0 - a + b);
5294 :
5295 : Note that the bound has to be computed as p_0 - a + b and not from the
5296 : number of iterations as p_0 + (b - a): the latter is only equivalent if
5297 : b - a does not wrap, which is not the case when the loop rolls zero times.
5298 :
5299 : For this to preserve correctness, we need to know that the values compared
5300 : in the transformed loop are ordered the same way as i and b are. Since the
5301 : comparison of the pointers is performed modulo the size of the address
5302 : space, this needs a + 1 > b to be an unsigned comparison, and the offsets
5303 : a and b scaled by the step of the candidate to be non-negative and to not
5304 : overflow. Then:
5305 :
5306 : 1) if a + 1 <= b, then p_0 - a + b is the final value of p, hence there is no
5307 : overflow in computing it or the values of p, and the pointers increase
5308 : monotonically together with i.
5309 : 2) if a + 1 > b, then the loop exits at the first test, and b <= a implies
5310 : that p_0 - a + b lies between the valid addresses p_0 - a and p_0, so
5311 : the test indeed fails. Here we also need to verify that the expression
5312 : p_0 - a does not overflow, which we prove using p_0 = base + a. */
5313 :
5314 : static bool
5315 238151 : iv_elimination_compare_lt (struct ivopts_data *data, struct iv_use *use,
5316 : struct iv_cand *cand, enum tree_code *comp_p,
5317 : class tree_niter_desc *niter, tree *bound_p)
5318 : {
5319 238151 : tree cand_type, a, b, mbz, nit_type = TREE_TYPE (niter->niter);
5320 238151 : tree off_type, offset, bound;
5321 714453 : class aff_tree nit, tmpa, tmpb;
5322 238151 : enum tree_code comp;
5323 238151 : HOST_WIDE_INT step;
5324 :
5325 : /* We need to know that the candidate induction variable does not overflow.
5326 : While more complex analysis may be used to prove this, for now just
5327 : check that the variable appears in the original program and that it
5328 : is computed in a type that guarantees no overflows. */
5329 238151 : cand_type = TREE_TYPE (cand->iv->base);
5330 238151 : if (cand->pos != IP_ORIGINAL || !nowrap_type_p (cand_type))
5331 : return false;
5332 :
5333 : /* Make sure that the loop iterates till the loop bound is hit, as otherwise
5334 : the calculation of the BOUND could overflow, making the comparison
5335 : invalid. */
5336 23633 : if (!data->loop_single_exit_p)
5337 : return false;
5338 :
5339 : /* We need to be able to decide whether candidate is increasing or decreasing
5340 : in order to choose the right comparison operator. */
5341 16617 : if (!cst_and_fits_in_hwi (cand->iv->step))
5342 : return false;
5343 16617 : step = int_cst_value (cand->iv->step);
5344 :
5345 : /* The bound we compute below is the value the candidate has after the last
5346 : iteration, so the exit test has to see the incremented candidate. */
5347 16617 : if (!stmt_after_increment (data->current_loop, cand, use->stmt))
5348 : return false;
5349 :
5350 : /* Check that the number of iterations matches the expected pattern:
5351 : a + 1 > b ? 0 : b - a - 1. */
5352 12547 : mbz = niter->may_be_zero;
5353 12547 : if (TREE_CODE (mbz) == GT_EXPR)
5354 : {
5355 : /* Handle a + 1 > b. */
5356 1365 : tree op0 = TREE_OPERAND (mbz, 0);
5357 1365 : if (TREE_CODE (op0) == PLUS_EXPR && integer_onep (TREE_OPERAND (op0, 1)))
5358 : {
5359 805 : a = TREE_OPERAND (op0, 0);
5360 805 : b = TREE_OPERAND (mbz, 1);
5361 : }
5362 : else
5363 : return false;
5364 : }
5365 11182 : else if (TREE_CODE (mbz) == LT_EXPR)
5366 : {
5367 1215 : tree op1 = TREE_OPERAND (mbz, 1);
5368 :
5369 : /* Handle b < a + 1. */
5370 1215 : if (TREE_CODE (op1) == PLUS_EXPR && integer_onep (TREE_OPERAND (op1, 1)))
5371 : {
5372 82 : a = TREE_OPERAND (op1, 0);
5373 82 : b = TREE_OPERAND (mbz, 0);
5374 : }
5375 : else
5376 : return false;
5377 : }
5378 : else
5379 : return false;
5380 :
5381 : /* Expected number of iterations is B - A - 1. Check that it matches
5382 : the actual number, i.e., that B - A - NITER = 1. */
5383 887 : tree_to_aff_combination (niter->niter, nit_type, &nit);
5384 887 : tree_to_aff_combination (fold_convert (nit_type, a), nit_type, &tmpa);
5385 887 : tree_to_aff_combination (fold_convert (nit_type, b), nit_type, &tmpb);
5386 887 : aff_combination_scale (&nit, -1);
5387 887 : aff_combination_scale (&tmpa, -1);
5388 887 : aff_combination_add (&tmpb, &tmpa);
5389 887 : aff_combination_add (&tmpb, &nit);
5390 887 : if (tmpb.n != 0 || maybe_ne (tmpb.offset, 1))
5391 : return false;
5392 :
5393 : /* The comparison A + 1 > B only tells us that B is at most A if it is
5394 : an unsigned one; otherwise B may well be negative. */
5395 490 : if (!TYPE_UNSIGNED (TREE_TYPE (a)))
5396 : return false;
5397 :
5398 : /* Check that CAND->IV->BASE - CAND->IV->STEP * A does not overflow. */
5399 490 : off_type = TREE_TYPE (cand->iv->step);
5400 490 : offset = fold_build2 (MULT_EXPR, off_type, cand->iv->step,
5401 : fold_convert (off_type, a));
5402 490 : if (!difference_cannot_overflow_p (data, cand->iv->base, offset))
5403 : return false;
5404 :
5405 : /* The candidate is compared as an unsigned quantity, so the offsets by
5406 : which A and B move it away from CAND->IV->BASE - CAND->IV->STEP * A have
5407 : to be ordered the same way as A and B themselves. */
5408 18 : if (!nonneg_scaled_offset_p (a, step, off_type)
5409 18 : || !nonneg_scaled_offset_p (b, step, off_type))
5410 : return false;
5411 :
5412 : /* Determine the new comparison operator. */
5413 10 : comp = step < 0 ? GT_EXPR : LT_EXPR;
5414 10 : if (*comp_p == NE_EXPR)
5415 10 : *comp_p = comp;
5416 0 : else if (*comp_p == EQ_EXPR)
5417 0 : *comp_p = invert_tree_comparison (comp, false);
5418 : else
5419 0 : gcc_unreachable ();
5420 :
5421 : /* Recompute the bound as CAND->IV->BASE - CAND->IV->STEP * A
5422 : + CAND->IV->STEP * B. Deriving it from the number of iterations, as
5423 : cand_value_at does, is not correct here: B - A is computed in NIT_TYPE
5424 : and converting it to OFF_TYPE is not value preserving when the loop
5425 : rolls zero times and B - A is thus negative. */
5426 10 : bound = fold_build2 (MINUS_EXPR, off_type,
5427 : fold_build2 (MULT_EXPR, off_type, cand->iv->step,
5428 : fold_convert (off_type, b)),
5429 : offset);
5430 10 : cand_type = TREE_TYPE (cand->iv->base);
5431 10 : if (POINTER_TYPE_P (cand_type))
5432 10 : *bound_p = fold_build_pointer_plus (cand->iv->base, bound);
5433 : else
5434 0 : *bound_p = fold_build2 (PLUS_EXPR, cand_type, cand->iv->base,
5435 : fold_convert (cand_type, bound));
5436 :
5437 : return true;
5438 238151 : }
5439 :
5440 : /* Check whether it is possible to express the condition in USE by comparison
5441 : of candidate CAND. If so, store the value compared with to BOUND, and the
5442 : comparison operator to COMP. */
5443 :
5444 : static bool
5445 4937581 : may_eliminate_iv (struct ivopts_data *data,
5446 : struct iv_use *use, struct iv_cand *cand, tree *bound,
5447 : enum tree_code *comp)
5448 : {
5449 4937581 : basic_block ex_bb;
5450 4937581 : edge exit;
5451 4937581 : tree period;
5452 4937581 : class loop *loop = data->current_loop;
5453 4937581 : aff_tree bnd;
5454 4937581 : class tree_niter_desc *desc = NULL;
5455 :
5456 : /* If the IV candidate involves undefs do not attempt to use it to
5457 : express a condition. */
5458 4937581 : if (cand->involves_undefs)
5459 : return false;
5460 :
5461 4937223 : if (TREE_CODE (cand->iv->step) != INTEGER_CST)
5462 : return false;
5463 :
5464 : /* For now works only for exits that dominate the loop latch.
5465 : TODO: extend to other conditions inside loop body. */
5466 4751571 : ex_bb = gimple_bb (use->stmt);
5467 4751571 : if (use->stmt != last_nondebug_stmt (ex_bb)
5468 4647528 : || gimple_code (use->stmt) != GIMPLE_COND
5469 9398089 : || !dominated_by_p (CDI_DOMINATORS, loop->latch, ex_bb))
5470 : return false;
5471 :
5472 4517566 : exit = EDGE_SUCC (ex_bb, 0);
5473 4517566 : if (flow_bb_inside_loop_p (loop, exit->dest))
5474 3409923 : exit = EDGE_SUCC (ex_bb, 1);
5475 4517566 : if (flow_bb_inside_loop_p (loop, exit->dest))
5476 : return false;
5477 :
5478 4403100 : desc = niter_for_exit (data, exit);
5479 4403100 : if (!desc)
5480 : return false;
5481 :
5482 : /* Determine whether we can use the variable to test the exit condition.
5483 : This is the case iff the period of the induction variable is greater
5484 : than the number of iterations for which the exit condition is true. */
5485 4101175 : period = iv_period (cand->iv);
5486 :
5487 : /* If the number of iterations is constant, compare against it directly. */
5488 4101175 : if (TREE_CODE (desc->niter) == INTEGER_CST)
5489 : {
5490 : /* See cand_value_at. */
5491 2698220 : if (stmt_after_increment (loop, cand, use->stmt))
5492 : {
5493 2640118 : if (!tree_int_cst_lt (desc->niter, period))
5494 : return false;
5495 : }
5496 : else
5497 : {
5498 58102 : if (tree_int_cst_lt (period, desc->niter))
5499 : return false;
5500 : }
5501 : }
5502 :
5503 : /* If not, and if this is the only possible exit of the loop, see whether
5504 : we can get a conservative estimate on the number of iterations of the
5505 : entire loop and compare against that instead. */
5506 : else
5507 : {
5508 1402955 : widest_int period_value, max_niter;
5509 :
5510 1402955 : max_niter = desc->max;
5511 1402955 : if (stmt_after_increment (loop, cand, use->stmt))
5512 1198196 : max_niter += 1;
5513 1402955 : period_value = wi::to_widest (period);
5514 1402955 : if (wi::gtu_p (max_niter, period_value))
5515 : {
5516 : /* See if we can take advantage of inferred loop bound
5517 : information. */
5518 350432 : if (data->loop_single_exit_p)
5519 : {
5520 223331 : if (!max_loop_iterations (loop, &max_niter))
5521 : return false;
5522 : /* The loop bound is already adjusted by adding 1. */
5523 223331 : if (wi::gtu_p (max_niter, period_value))
5524 : return false;
5525 : }
5526 : else
5527 : return false;
5528 : }
5529 1402955 : }
5530 :
5531 : /* For doloop IV cand, the bound would be zero. It's safe whether
5532 : may_be_zero set or not. */
5533 3893924 : if (cand->doloop_p)
5534 : {
5535 0 : *bound = build_int_cst (TREE_TYPE (cand->iv->base), 0);
5536 0 : *comp = iv_elimination_compare (data, use);
5537 0 : return true;
5538 : }
5539 :
5540 3893924 : cand_value_at (loop, cand, use->stmt, desc, &bnd);
5541 :
5542 3893924 : *bound = fold_convert (TREE_TYPE (cand->iv->base),
5543 : aff_combination_to_tree (&bnd));
5544 3893924 : *comp = iv_elimination_compare (data, use);
5545 :
5546 : /* Sometimes, it is possible to handle the situation that the number of
5547 : iterations may be zero unless additional assumptions by using <
5548 : instead of != in the exit condition.
5549 :
5550 : TODO: we could also calculate the value MAY_BE_ZERO ? 0 : NITER and
5551 : base the exit condition on it. However, that is often too
5552 : expensive. */
5553 3893924 : if (!integer_zerop (desc->may_be_zero)
5554 3893924 : && !iv_elimination_compare_lt (data, use, cand, comp, desc, bound))
5555 : return false;
5556 :
5557 : /* It is unlikely that computing the number of iterations using division
5558 : would be more profitable than keeping the original induction variable. */
5559 3655783 : bool cond_overflow_p;
5560 3655783 : if (expression_expensive_p (*bound, &cond_overflow_p))
5561 6993 : return false;
5562 :
5563 : return true;
5564 4937581 : }
5565 :
5566 : /* Calculates the cost of BOUND, if it is a PARM_DECL. A PARM_DECL must
5567 : be copied, if it is used in the loop body and DATA->body_includes_call. */
5568 :
5569 : static int
5570 8407066 : parm_decl_cost (struct ivopts_data *data, tree bound)
5571 : {
5572 8407066 : tree sbound = bound;
5573 8407066 : STRIP_NOPS (sbound);
5574 :
5575 8407066 : if (TREE_CODE (sbound) == SSA_NAME
5576 2893283 : && SSA_NAME_IS_DEFAULT_DEF (sbound)
5577 154074 : && TREE_CODE (SSA_NAME_VAR (sbound)) == PARM_DECL
5578 8558733 : && data->body_includes_call)
5579 36421 : return COSTS_N_INSNS (1);
5580 :
5581 : return 0;
5582 : }
5583 :
5584 : /* Determines cost of computing the use in GROUP with CAND in a condition. */
5585 :
5586 : static bool
5587 5950554 : determine_group_iv_cost_cond (struct ivopts_data *data,
5588 : struct iv_group *group, struct iv_cand *cand)
5589 : {
5590 5950554 : tree bound = NULL_TREE;
5591 5950554 : struct iv *cmp_iv;
5592 5950554 : bitmap inv_exprs = NULL;
5593 5950554 : bitmap inv_vars_elim = NULL, inv_vars_express = NULL, inv_vars;
5594 5950554 : comp_cost elim_cost = infinite_cost, express_cost, cost, bound_cost;
5595 5950554 : enum comp_iv_rewrite rewrite_type;
5596 5950554 : iv_inv_expr_ent *inv_expr_elim = NULL, *inv_expr_express = NULL, *inv_expr;
5597 5950554 : tree *control_var, *bound_cst;
5598 5950554 : enum tree_code comp = ERROR_MARK;
5599 5950554 : struct iv_use *use = group->vuses[0];
5600 :
5601 : /* Extract condition operands. */
5602 5950554 : rewrite_type = extract_cond_operands (data, use->stmt, &control_var,
5603 : &bound_cst, NULL, &cmp_iv);
5604 5950554 : gcc_assert (rewrite_type != COMP_IV_NA);
5605 :
5606 : /* Try iv elimination. */
5607 5950554 : if (rewrite_type == COMP_IV_ELIM
5608 5950554 : && may_eliminate_iv (data, use, cand, &bound, &comp))
5609 : {
5610 3648790 : elim_cost = force_var_cost (data, bound, &inv_vars_elim);
5611 3648790 : if (elim_cost.cost == 0)
5612 2478612 : elim_cost.cost = parm_decl_cost (data, bound);
5613 1170178 : else if (TREE_CODE (bound) == INTEGER_CST)
5614 0 : elim_cost.cost = 0;
5615 : /* If we replace a loop condition 'i < n' with 'p < base + n',
5616 : inv_vars_elim will have 'base' and 'n' set, which implies that both
5617 : 'base' and 'n' will be live during the loop. More likely,
5618 : 'base + n' will be loop invariant, resulting in only one live value
5619 : during the loop. So in that case we clear inv_vars_elim and set
5620 : inv_expr_elim instead. */
5621 3648790 : if (inv_vars_elim && bitmap_count_bits (inv_vars_elim) > 1)
5622 : {
5623 313281 : inv_expr_elim = get_loop_invariant_expr (data, bound);
5624 313281 : bitmap_clear (inv_vars_elim);
5625 : }
5626 : /* The bound is a loop invariant, so it will be only computed
5627 : once. */
5628 3648790 : elim_cost.cost = adjust_setup_cost (data, elim_cost.cost);
5629 : }
5630 :
5631 : /* When the condition is a comparison of the candidate IV against
5632 : zero, prefer this IV.
5633 :
5634 : TODO: The constant that we're subtracting from the cost should
5635 : be target-dependent. This information should be added to the
5636 : target costs for each backend. */
5637 5950554 : if (!elim_cost.infinite_cost_p () /* Do not try to decrease infinite! */
5638 3648790 : && integer_zerop (*bound_cst)
5639 8585161 : && (operand_equal_p (*control_var, cand->var_after, 0)
5640 2382314 : || operand_equal_p (*control_var, cand->var_before, 0)))
5641 258702 : elim_cost -= 1;
5642 :
5643 5950554 : express_cost = get_computation_cost (data, use, cand, false,
5644 : &inv_vars_express, NULL,
5645 : &inv_expr_express);
5646 5950554 : if (cmp_iv != NULL)
5647 5023577 : find_inv_vars (data, &cmp_iv->base, &inv_vars_express);
5648 :
5649 : /* Count the cost of the original bound as well. */
5650 5950554 : bound_cost = force_var_cost (data, *bound_cst, NULL);
5651 5950554 : if (bound_cost.cost == 0)
5652 5928454 : bound_cost.cost = parm_decl_cost (data, *bound_cst);
5653 22100 : else if (TREE_CODE (*bound_cst) == INTEGER_CST)
5654 0 : bound_cost.cost = 0;
5655 5950554 : express_cost += bound_cost;
5656 :
5657 : /* Choose the better approach, preferring the eliminated IV. */
5658 5950554 : if (elim_cost <= express_cost)
5659 : {
5660 4533045 : cost = elim_cost;
5661 4533045 : inv_vars = inv_vars_elim;
5662 4533045 : inv_vars_elim = NULL;
5663 4533045 : inv_expr = inv_expr_elim;
5664 : /* For doloop candidate/use pair, adjust to zero cost. */
5665 4533045 : if (group->doloop_p && cand->doloop_p && elim_cost.cost > no_cost.cost)
5666 0 : cost = no_cost;
5667 : }
5668 : else
5669 : {
5670 1417509 : cost = express_cost;
5671 1417509 : inv_vars = inv_vars_express;
5672 1417509 : inv_vars_express = NULL;
5673 1417509 : bound = NULL_TREE;
5674 1417509 : comp = ERROR_MARK;
5675 1417509 : inv_expr = inv_expr_express;
5676 : }
5677 :
5678 5950554 : if (inv_expr)
5679 : {
5680 594092 : inv_exprs = BITMAP_ALLOC (NULL);
5681 594092 : bitmap_set_bit (inv_exprs, inv_expr->id);
5682 : }
5683 5950554 : set_group_iv_cost (data, group, cand, cost,
5684 : inv_vars, bound, comp, inv_exprs);
5685 :
5686 5950554 : if (inv_vars_elim)
5687 23240 : BITMAP_FREE (inv_vars_elim);
5688 5950554 : if (inv_vars_express)
5689 1240845 : BITMAP_FREE (inv_vars_express);
5690 :
5691 5950554 : return !cost.infinite_cost_p ();
5692 : }
5693 :
5694 : /* Determines cost of computing uses in GROUP with CAND. Returns false
5695 : if USE cannot be represented with CAND. */
5696 :
5697 : static bool
5698 17775736 : determine_group_iv_cost (struct ivopts_data *data,
5699 : struct iv_group *group, struct iv_cand *cand)
5700 : {
5701 17775736 : switch (group->type)
5702 : {
5703 5526953 : case USE_NONLINEAR_EXPR:
5704 5526953 : return determine_group_iv_cost_generic (data, group, cand);
5705 :
5706 6298229 : case USE_REF_ADDRESS:
5707 6298229 : case USE_PTR_ADDRESS:
5708 6298229 : return determine_group_iv_cost_address (data, group, cand);
5709 :
5710 5950554 : case USE_COMPARE:
5711 5950554 : return determine_group_iv_cost_cond (data, group, cand);
5712 :
5713 0 : default:
5714 0 : gcc_unreachable ();
5715 : }
5716 : }
5717 :
5718 : /* Return true if get_computation_cost indicates that autoincrement is
5719 : a possibility for the pair of USE and CAND, false otherwise. */
5720 :
5721 : static bool
5722 1288385 : autoinc_possible_for_pair (struct ivopts_data *data, struct iv_use *use,
5723 : struct iv_cand *cand)
5724 : {
5725 1288385 : if (!address_p (use->type))
5726 : return false;
5727 :
5728 418058 : bool can_autoinc = false;
5729 418058 : get_computation_cost (data, use, cand, true, NULL, &can_autoinc, NULL);
5730 418058 : return can_autoinc;
5731 : }
5732 :
5733 : /* Examine IP_ORIGINAL candidates to see if they are incremented next to a
5734 : use that allows autoincrement, and set their AINC_USE if possible. */
5735 :
5736 : static void
5737 507705 : set_autoinc_for_original_candidates (struct ivopts_data *data)
5738 : {
5739 507705 : unsigned i, j;
5740 :
5741 5157827 : for (i = 0; i < data->vcands.length (); i++)
5742 : {
5743 4650122 : struct iv_cand *cand = data->vcands[i];
5744 4650122 : struct iv_use *closest_before = NULL;
5745 4650122 : struct iv_use *closest_after = NULL;
5746 4650122 : if (cand->pos != IP_ORIGINAL)
5747 3775008 : continue;
5748 :
5749 3840981 : for (j = 0; j < data->vgroups.length (); j++)
5750 : {
5751 2965867 : struct iv_group *group = data->vgroups[j];
5752 2965867 : struct iv_use *use = group->vuses[0];
5753 2965867 : unsigned uid = gimple_uid (use->stmt);
5754 :
5755 2965867 : if (gimple_bb (use->stmt) != gimple_bb (cand->incremented_at))
5756 1168194 : continue;
5757 :
5758 1797673 : if (uid < gimple_uid (cand->incremented_at)
5759 1797673 : && (closest_before == NULL
5760 378282 : || uid > gimple_uid (closest_before->stmt)))
5761 : closest_before = use;
5762 :
5763 1797673 : if (uid > gimple_uid (cand->incremented_at)
5764 1797673 : && (closest_after == NULL
5765 72684 : || uid < gimple_uid (closest_after->stmt)))
5766 : closest_after = use;
5767 : }
5768 :
5769 875114 : if (closest_before != NULL
5770 875114 : && autoinc_possible_for_pair (data, closest_before, cand))
5771 0 : cand->ainc_use = closest_before;
5772 875114 : else if (closest_after != NULL
5773 875114 : && autoinc_possible_for_pair (data, closest_after, cand))
5774 0 : cand->ainc_use = closest_after;
5775 : }
5776 507705 : }
5777 :
5778 : /* Relate compare use with all candidates. */
5779 :
5780 : static void
5781 299 : relate_compare_use_with_all_cands (struct ivopts_data *data)
5782 : {
5783 299 : unsigned i, count = data->vcands.length ();
5784 9590 : for (i = 0; i < data->vgroups.length (); i++)
5785 : {
5786 9291 : struct iv_group *group = data->vgroups[i];
5787 :
5788 9291 : if (group->type == USE_COMPARE)
5789 1909 : bitmap_set_range (group->related_cands, 0, count);
5790 : }
5791 299 : }
5792 :
5793 : /* If PREFERRED_MODE is suitable and profitable, use the preferred
5794 : PREFERRED_MODE to compute doloop iv base from niter: base = niter + 1. */
5795 :
5796 : static tree
5797 0 : compute_doloop_base_on_mode (machine_mode preferred_mode, tree niter,
5798 : const widest_int &iterations_max)
5799 : {
5800 0 : tree ntype = TREE_TYPE (niter);
5801 0 : tree pref_type = lang_hooks.types.type_for_mode (preferred_mode, 1);
5802 0 : if (!pref_type)
5803 0 : return fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
5804 : build_int_cst (ntype, 1));
5805 :
5806 0 : gcc_assert (TREE_CODE (pref_type) == INTEGER_TYPE);
5807 :
5808 0 : int prec = TYPE_PRECISION (ntype);
5809 0 : int pref_prec = TYPE_PRECISION (pref_type);
5810 :
5811 0 : tree base;
5812 :
5813 : /* Check if the PREFERRED_MODED is able to present niter. */
5814 0 : if (pref_prec > prec
5815 0 : || wi::ltu_p (iterations_max,
5816 0 : widest_int::from (wi::max_value (pref_prec, UNSIGNED),
5817 : UNSIGNED)))
5818 : {
5819 : /* No wrap, it is safe to use preferred type after niter + 1. */
5820 0 : if (wi::ltu_p (iterations_max,
5821 0 : widest_int::from (wi::max_value (prec, UNSIGNED),
5822 : UNSIGNED)))
5823 : {
5824 : /* This could help to optimize "-1 +1" pair when niter looks
5825 : like "n-1": n is in original mode. "base = (n - 1) + 1"
5826 : in PREFERRED_MODED: it could be base = (PREFERRED_TYPE)n. */
5827 0 : base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
5828 : build_int_cst (ntype, 1));
5829 0 : base = fold_convert (pref_type, base);
5830 : }
5831 :
5832 : /* To avoid wrap, convert niter to preferred type before plus 1. */
5833 : else
5834 : {
5835 0 : niter = fold_convert (pref_type, niter);
5836 0 : base = fold_build2 (PLUS_EXPR, pref_type, unshare_expr (niter),
5837 : build_int_cst (pref_type, 1));
5838 : }
5839 : }
5840 : else
5841 0 : base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
5842 : build_int_cst (ntype, 1));
5843 : return base;
5844 : }
5845 :
5846 : /* Add one doloop dedicated IV candidate:
5847 : - Base is (may_be_zero ? 1 : (niter + 1)).
5848 : - Step is -1. */
5849 :
5850 : static void
5851 0 : add_iv_candidate_for_doloop (struct ivopts_data *data)
5852 : {
5853 0 : tree_niter_desc *niter_desc = niter_for_single_dom_exit (data);
5854 0 : gcc_assert (niter_desc && niter_desc->assumptions);
5855 :
5856 0 : tree niter = niter_desc->niter;
5857 0 : tree ntype = TREE_TYPE (niter);
5858 0 : gcc_assert (INTEGRAL_NB_TYPE_P (ntype));
5859 :
5860 0 : tree may_be_zero = niter_desc->may_be_zero;
5861 0 : if (may_be_zero && integer_zerop (may_be_zero))
5862 : may_be_zero = NULL_TREE;
5863 0 : if (may_be_zero)
5864 : {
5865 0 : if (COMPARISON_CLASS_P (may_be_zero))
5866 : {
5867 0 : niter = fold_build3 (COND_EXPR, ntype, may_be_zero,
5868 : build_int_cst (ntype, 0),
5869 : rewrite_to_non_trapping_overflow (niter));
5870 : }
5871 : /* Don't try to obtain the iteration count expression when may_be_zero is
5872 : integer_nonzerop (actually iteration count is one) or else. */
5873 : else
5874 : return;
5875 : }
5876 :
5877 0 : machine_mode mode = TYPE_MODE (ntype);
5878 0 : machine_mode pref_mode = targetm.preferred_doloop_mode (mode);
5879 :
5880 0 : tree base;
5881 0 : if (mode != pref_mode)
5882 : {
5883 0 : base = compute_doloop_base_on_mode (pref_mode, niter, niter_desc->max);
5884 0 : ntype = TREE_TYPE (base);
5885 : }
5886 : else
5887 0 : base = fold_build2 (PLUS_EXPR, ntype, unshare_expr (niter),
5888 : build_int_cst (ntype, 1));
5889 :
5890 : /* For non integer types or non-mode precision types,
5891 : convert directly to an integer type. */
5892 0 : if (TREE_CODE (ntype) != INTEGER_TYPE
5893 0 : || !type_has_mode_precision_p (ntype))
5894 : {
5895 0 : ntype = lang_hooks.types.type_for_mode (TYPE_MODE (ntype),
5896 0 : TYPE_UNSIGNED (ntype));
5897 0 : base = fold_convert (ntype, base);
5898 : }
5899 :
5900 0 : add_candidate (data, base, build_int_cst (ntype, -1), true, NULL, NULL, true);
5901 : }
5902 :
5903 : /* Finds the candidates for the induction variables. */
5904 :
5905 : static void
5906 507705 : find_iv_candidates (struct ivopts_data *data)
5907 : {
5908 : /* Add commonly used ivs. */
5909 507705 : add_standard_iv_candidates (data);
5910 :
5911 : /* Add doloop dedicated ivs. */
5912 507705 : if (data->doloop_use_p)
5913 0 : add_iv_candidate_for_doloop (data);
5914 :
5915 : /* Add old induction variables. */
5916 507705 : add_iv_candidate_for_bivs (data);
5917 :
5918 : /* Add induction variables derived from uses. */
5919 507705 : add_iv_candidate_for_groups (data);
5920 :
5921 507705 : set_autoinc_for_original_candidates (data);
5922 :
5923 : /* Record the important candidates. */
5924 507705 : record_important_candidates (data);
5925 :
5926 : /* Relate compare iv_use with all candidates. */
5927 507705 : if (!data->consider_all_candidates)
5928 299 : relate_compare_use_with_all_cands (data);
5929 :
5930 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
5931 : {
5932 67 : unsigned i;
5933 :
5934 67 : fprintf (dump_file, "\n<Important Candidates>:\t");
5935 820 : for (i = 0; i < data->vcands.length (); i++)
5936 686 : if (data->vcands[i]->important)
5937 492 : fprintf (dump_file, " %d,", data->vcands[i]->id);
5938 67 : fprintf (dump_file, "\n");
5939 :
5940 67 : fprintf (dump_file, "\n<Group, Cand> Related:\n");
5941 354 : for (i = 0; i < data->vgroups.length (); i++)
5942 : {
5943 220 : struct iv_group *group = data->vgroups[i];
5944 :
5945 220 : if (group->related_cands)
5946 : {
5947 220 : fprintf (dump_file, " Group %d:\t", group->id);
5948 220 : dump_bitmap (dump_file, group->related_cands);
5949 : }
5950 : }
5951 67 : fprintf (dump_file, "\n");
5952 : }
5953 507705 : }
5954 :
5955 : /* Determines costs of computing use of iv with an iv candidate. */
5956 :
5957 : static void
5958 507705 : determine_group_iv_costs (struct ivopts_data *data)
5959 : {
5960 507705 : unsigned i, j;
5961 507705 : struct iv_cand *cand;
5962 507705 : struct iv_group *group;
5963 507705 : bitmap to_clear = BITMAP_ALLOC (NULL);
5964 :
5965 507705 : alloc_use_cost_map (data);
5966 :
5967 2162452 : for (i = 0; i < data->vgroups.length (); i++)
5968 : {
5969 1654747 : group = data->vgroups[i];
5970 :
5971 1654747 : if (data->consider_all_candidates)
5972 : {
5973 19095817 : for (j = 0; j < data->vcands.length (); j++)
5974 : {
5975 17441070 : cand = data->vcands[j];
5976 17441070 : determine_group_iv_cost (data, group, cand);
5977 : }
5978 : }
5979 : else
5980 : {
5981 9291 : bitmap_iterator bi;
5982 :
5983 343957 : EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, j, bi)
5984 : {
5985 334666 : cand = data->vcands[j];
5986 334666 : if (!determine_group_iv_cost (data, group, cand))
5987 201428 : bitmap_set_bit (to_clear, j);
5988 : }
5989 :
5990 : /* Remove the candidates for that the cost is infinite from
5991 : the list of related candidates. */
5992 9291 : bitmap_and_compl_into (group->related_cands, to_clear);
5993 9291 : bitmap_clear (to_clear);
5994 : }
5995 : }
5996 :
5997 507705 : BITMAP_FREE (to_clear);
5998 :
5999 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6000 : {
6001 67 : bitmap_iterator bi;
6002 :
6003 : /* Dump invariant variables. */
6004 67 : fprintf (dump_file, "\n<Invariant Vars>:\n");
6005 1080 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
6006 : {
6007 1013 : struct version_info *info = ver_info (data, i);
6008 1013 : if (info->inv_id)
6009 : {
6010 222 : fprintf (dump_file, "Inv %d:\t", info->inv_id);
6011 222 : print_generic_expr (dump_file, info->name, TDF_SLIM);
6012 222 : fprintf (dump_file, "%s\n",
6013 222 : info->has_nonlin_use ? "" : "\t(eliminable)");
6014 : }
6015 : }
6016 :
6017 : /* Dump invariant expressions. */
6018 67 : fprintf (dump_file, "\n<Invariant Expressions>:\n");
6019 67 : auto_vec <iv_inv_expr_ent *> list (data->inv_expr_tab->elements ());
6020 :
6021 372 : for (hash_table<iv_inv_expr_hasher>::iterator it
6022 506 : = data->inv_expr_tab->begin (); it != data->inv_expr_tab->end ();
6023 372 : ++it)
6024 372 : list.safe_push (*it);
6025 :
6026 67 : list.qsort (sort_iv_inv_expr_ent);
6027 :
6028 439 : for (i = 0; i < list.length (); ++i)
6029 : {
6030 372 : fprintf (dump_file, "inv_expr %d: \t", list[i]->id);
6031 372 : print_generic_expr (dump_file, list[i]->expr, TDF_SLIM);
6032 372 : fprintf (dump_file, "\n");
6033 : }
6034 :
6035 67 : fprintf (dump_file, "\n<Group-candidate Costs>:\n");
6036 :
6037 287 : for (i = 0; i < data->vgroups.length (); i++)
6038 : {
6039 220 : group = data->vgroups[i];
6040 :
6041 220 : fprintf (dump_file, "Group %d:\n", i);
6042 220 : fprintf (dump_file, " cand\tcost\tcompl.\tinv.expr.\tinv.vars\n");
6043 2982 : for (j = 0; j < group->n_map_members; j++)
6044 : {
6045 3851 : if (!group->cost_map[j].cand
6046 2762 : || group->cost_map[j].cost.infinite_cost_p ())
6047 1089 : continue;
6048 :
6049 1673 : fprintf (dump_file, " %d\t%" PRId64 "\t%d\t",
6050 1673 : group->cost_map[j].cand->id,
6051 : group->cost_map[j].cost.cost,
6052 1673 : group->cost_map[j].cost.complexity);
6053 1673 : if (!group->cost_map[j].inv_exprs
6054 1673 : || bitmap_empty_p (group->cost_map[j].inv_exprs))
6055 1173 : fprintf (dump_file, "NIL;\t");
6056 : else
6057 500 : bitmap_print (dump_file,
6058 : group->cost_map[j].inv_exprs, "", ";\t");
6059 1673 : if (!group->cost_map[j].inv_vars
6060 1673 : || bitmap_empty_p (group->cost_map[j].inv_vars))
6061 1347 : fprintf (dump_file, "NIL;\n");
6062 : else
6063 326 : bitmap_print (dump_file,
6064 : group->cost_map[j].inv_vars, "", "\n");
6065 : }
6066 :
6067 220 : fprintf (dump_file, "\n");
6068 : }
6069 67 : fprintf (dump_file, "\n");
6070 67 : }
6071 507705 : }
6072 :
6073 : /* Determines cost of the candidate CAND. */
6074 :
6075 : static void
6076 4650122 : determine_iv_cost (struct ivopts_data *data, struct iv_cand *cand)
6077 : {
6078 4650122 : comp_cost cost_base;
6079 4650122 : int64_t cost, cost_step;
6080 4650122 : tree base;
6081 :
6082 4650122 : gcc_assert (cand->iv != NULL);
6083 :
6084 : /* There are two costs associated with the candidate -- its increment
6085 : and its initialization. The second is almost negligible for any loop
6086 : that rolls enough, so we take it just very little into account. */
6087 :
6088 4650122 : base = cand->iv->base;
6089 4650122 : cost_base = force_var_cost (data, base, NULL);
6090 : /* It will be exceptional that the iv register happens to be initialized with
6091 : the proper value at no cost. In general, there will at least be a regcopy
6092 : or a const set. */
6093 4650122 : if (cost_base.cost == 0)
6094 3694933 : cost_base.cost = COSTS_N_INSNS (1);
6095 : /* Doloop decrement should be considered as zero cost. */
6096 4650122 : if (cand->doloop_p)
6097 : cost_step = 0;
6098 : else
6099 4650122 : cost_step = add_cost (data->speed, TYPE_MODE (TREE_TYPE (base)));
6100 4650122 : cost = cost_step + adjust_setup_cost (data, cost_base.cost);
6101 :
6102 : /* Prefer the original ivs unless we may gain something by replacing it.
6103 : The reason is to make debugging simpler; so this is not relevant for
6104 : artificial ivs created by other optimization passes. */
6105 4650122 : if ((cand->pos != IP_ORIGINAL
6106 875114 : || !SSA_NAME_VAR (cand->var_before)
6107 440607 : || DECL_ARTIFICIAL (SSA_NAME_VAR (cand->var_before)))
6108 : /* Prefer doloop as well. */
6109 5177481 : && !cand->doloop_p)
6110 4302367 : cost++;
6111 :
6112 : /* Prefer not to insert statements into latch unless there are some
6113 : already (so that we do not create unnecessary jumps). */
6114 4650122 : if (cand->pos == IP_END
6115 4650122 : && empty_block_p (ip_end_pos (data->current_loop)))
6116 1822 : cost++;
6117 :
6118 4650122 : cand->cost = cost;
6119 4650122 : cand->cost_step = cost_step;
6120 4650122 : }
6121 :
6122 : /* Determines costs of computation of the candidates. */
6123 :
6124 : static void
6125 507705 : determine_iv_costs (struct ivopts_data *data)
6126 : {
6127 507705 : unsigned i;
6128 :
6129 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6130 : {
6131 67 : fprintf (dump_file, "<Candidate Costs>:\n");
6132 67 : fprintf (dump_file, " cand\tcost\n");
6133 : }
6134 :
6135 5157827 : for (i = 0; i < data->vcands.length (); i++)
6136 : {
6137 4650122 : struct iv_cand *cand = data->vcands[i];
6138 :
6139 4650122 : determine_iv_cost (data, cand);
6140 :
6141 4650122 : if (dump_file && (dump_flags & TDF_DETAILS))
6142 686 : fprintf (dump_file, " %d\t%d\n", i, cand->cost);
6143 : }
6144 :
6145 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6146 67 : fprintf (dump_file, "\n");
6147 507705 : }
6148 :
6149 : /* Estimate register pressure for loop having N_INVS invariants and N_CANDS
6150 : induction variables. Note N_INVS includes both invariant variables and
6151 : invariant expressions. */
6152 :
6153 : static unsigned
6154 430918119 : ivopts_estimate_reg_pressure (struct ivopts_data *data, unsigned n_invs,
6155 : unsigned n_cands)
6156 : {
6157 430918119 : unsigned cost;
6158 430918119 : unsigned n_old = data->regs_used, n_new = n_invs + n_cands;
6159 430918119 : unsigned regs_needed = n_new + n_old, available_regs = target_avail_regs;
6160 430918119 : bool speed = data->speed;
6161 :
6162 : /* If there is a call in the loop body, the call-clobbered registers
6163 : are not available for loop invariants. */
6164 430918119 : if (data->body_includes_call)
6165 97865126 : available_regs = available_regs - target_clobbered_regs;
6166 :
6167 : /* If we have enough registers. */
6168 430918119 : if (regs_needed <= available_regs)
6169 : cost = 0;
6170 : /* If we run out of available registers but the number of candidates
6171 : does not, we penalize extra registers using target_spill_cost.
6172 : As we tend to spill invariants here, only take loading the
6173 : invariant into account, because the invariant won't change for the
6174 : duration of the loop and storing it every iteration is unnecessary. */
6175 137602937 : else if (n_cands <= available_regs)
6176 117858717 : cost = target_spill_cost [speed] * (regs_needed - available_regs) / 2;
6177 : /* If both IV cands and invariants spill, calculate additional cost for
6178 : having to store spilled candidates. */
6179 : else
6180 19744220 : cost = (target_spill_cost [speed] * (regs_needed - available_regs) / 2
6181 19744220 : + target_spill_cost[speed] * (n_cands - available_regs) / 2);
6182 :
6183 430918119 : return cost;
6184 : }
6185 :
6186 : /* For each size of the induction variable set determine the penalty. */
6187 :
6188 : static void
6189 507705 : determine_set_costs (struct ivopts_data *data)
6190 : {
6191 507705 : unsigned j, n;
6192 507705 : gphi *phi;
6193 507705 : gphi_iterator psi;
6194 507705 : tree op;
6195 507705 : class loop *loop = data->current_loop;
6196 507705 : bitmap_iterator bi;
6197 :
6198 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6199 : {
6200 67 : fprintf (dump_file, "<Global Costs>:\n");
6201 67 : fprintf (dump_file, " target_avail_regs %d\n", target_avail_regs);
6202 67 : fprintf (dump_file, " target_clobbered_regs %d\n", target_clobbered_regs);
6203 67 : fprintf (dump_file, " target_reg_cost %d\n", target_reg_cost[data->speed]);
6204 67 : fprintf (dump_file, " target_spill_cost %d\n", target_spill_cost[data->speed]);
6205 : }
6206 :
6207 507705 : n = 0;
6208 1975274 : for (psi = gsi_start_phis (loop->header); !gsi_end_p (psi); gsi_next (&psi))
6209 : {
6210 1467569 : phi = psi.phi ();
6211 1467569 : op = PHI_RESULT (phi);
6212 :
6213 2935138 : if (virtual_operand_p (op))
6214 311392 : continue;
6215 :
6216 1156177 : if (get_iv (data, op))
6217 878958 : continue;
6218 :
6219 508873 : if (!POINTER_TYPE_P (TREE_TYPE (op))
6220 508640 : && !INTEGRAL_TYPE_P (TREE_TYPE (op)))
6221 103031 : continue;
6222 :
6223 174188 : n++;
6224 : }
6225 :
6226 5622498 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
6227 : {
6228 5114793 : struct version_info *info = ver_info (data, j);
6229 :
6230 5114793 : if (info->inv_id && info->has_nonlin_use)
6231 523081 : n++;
6232 : }
6233 :
6234 507705 : data->regs_used = n;
6235 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6236 67 : fprintf (dump_file, " regs_used %d\n", n);
6237 :
6238 507705 : if (dump_file && (dump_flags & TDF_DETAILS))
6239 : {
6240 67 : fprintf (dump_file, " cost for size:\n");
6241 67 : fprintf (dump_file, " ivs\tcost\n");
6242 2144 : for (j = 0; j <= 2 * target_avail_regs; j++)
6243 2077 : fprintf (dump_file, " %d\t%d\n", j,
6244 : ivopts_estimate_reg_pressure (data, 0, j));
6245 67 : fprintf (dump_file, "\n");
6246 : }
6247 507705 : }
6248 :
6249 : /* Returns true if A is a cheaper cost pair than B. */
6250 :
6251 : static bool
6252 86248269 : cheaper_cost_pair (class cost_pair *a, class cost_pair *b)
6253 : {
6254 86248269 : if (!a)
6255 : return false;
6256 :
6257 80857364 : if (!b)
6258 : return true;
6259 :
6260 77642950 : if (a->cost < b->cost)
6261 : return true;
6262 :
6263 57037226 : if (b->cost < a->cost)
6264 : return false;
6265 :
6266 : /* In case the costs are the same, prefer the cheaper candidate. */
6267 32283614 : if (a->cand->cost < b->cand->cost)
6268 4836476 : return true;
6269 :
6270 : return false;
6271 : }
6272 :
6273 : /* Compare if A is a more expensive cost pair than B. Return 1, 0 and -1
6274 : for more expensive, equal and cheaper respectively. */
6275 :
6276 : static int
6277 30865658 : compare_cost_pair (class cost_pair *a, class cost_pair *b)
6278 : {
6279 30865658 : if (cheaper_cost_pair (a, b))
6280 : return -1;
6281 24525263 : if (cheaper_cost_pair (b, a))
6282 16157448 : return 1;
6283 :
6284 : return 0;
6285 : }
6286 :
6287 : /* Returns candidate by that USE is expressed in IVS. */
6288 :
6289 : static class cost_pair *
6290 288849477 : iv_ca_cand_for_group (class iv_ca *ivs, struct iv_group *group)
6291 : {
6292 288849477 : return ivs->cand_for_group[group->id];
6293 : }
6294 :
6295 : /* Computes the cost field of IVS structure. */
6296 :
6297 : static void
6298 430915792 : iv_ca_recount_cost (struct ivopts_data *data, class iv_ca *ivs)
6299 : {
6300 430915792 : comp_cost cost = ivs->cand_use_cost;
6301 :
6302 430915792 : cost += ivs->cand_cost;
6303 430915792 : cost += ivopts_estimate_reg_pressure (data, ivs->n_invs, ivs->n_cands);
6304 430915792 : ivs->cost = cost;
6305 430915792 : }
6306 :
6307 : /* Remove use of invariants in set INVS by decreasing counter in N_INV_USES
6308 : and IVS. */
6309 :
6310 : static void
6311 592002400 : iv_ca_set_remove_invs (class iv_ca *ivs, bitmap invs, unsigned *n_inv_uses)
6312 : {
6313 592002400 : bitmap_iterator bi;
6314 592002400 : unsigned iid;
6315 :
6316 592002400 : if (!invs)
6317 469924771 : return;
6318 :
6319 122077629 : gcc_assert (n_inv_uses != NULL);
6320 211363155 : EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
6321 : {
6322 89285526 : n_inv_uses[iid]--;
6323 89285526 : if (n_inv_uses[iid] == 0)
6324 66632217 : ivs->n_invs--;
6325 : }
6326 : }
6327 :
6328 : /* Set USE not to be expressed by any candidate in IVS. */
6329 :
6330 : static void
6331 213804573 : iv_ca_set_no_cp (struct ivopts_data *data, class iv_ca *ivs,
6332 : struct iv_group *group)
6333 : {
6334 213804573 : unsigned gid = group->id, cid;
6335 213804573 : class cost_pair *cp;
6336 :
6337 213804573 : cp = ivs->cand_for_group[gid];
6338 213804573 : if (!cp)
6339 : return;
6340 213804573 : cid = cp->cand->id;
6341 :
6342 213804573 : ivs->bad_groups++;
6343 213804573 : ivs->cand_for_group[gid] = NULL;
6344 213804573 : ivs->n_cand_uses[cid]--;
6345 :
6346 213804573 : if (ivs->n_cand_uses[cid] == 0)
6347 : {
6348 82196627 : bitmap_clear_bit (ivs->cands, cid);
6349 82196627 : if (!cp->cand->doloop_p || !targetm.have_count_reg_decr_p)
6350 82196627 : ivs->n_cands--;
6351 82196627 : ivs->cand_cost -= cp->cand->cost;
6352 82196627 : iv_ca_set_remove_invs (ivs, cp->cand->inv_vars, ivs->n_inv_var_uses);
6353 82196627 : iv_ca_set_remove_invs (ivs, cp->cand->inv_exprs, ivs->n_inv_expr_uses);
6354 : }
6355 :
6356 213804573 : ivs->cand_use_cost -= cp->cost;
6357 213804573 : iv_ca_set_remove_invs (ivs, cp->inv_vars, ivs->n_inv_var_uses);
6358 213804573 : iv_ca_set_remove_invs (ivs, cp->inv_exprs, ivs->n_inv_expr_uses);
6359 213804573 : iv_ca_recount_cost (data, ivs);
6360 : }
6361 :
6362 : /* Add use of invariants in set INVS by increasing counter in N_INV_USES and
6363 : IVS. */
6364 :
6365 : static void
6366 601523554 : iv_ca_set_add_invs (class iv_ca *ivs, bitmap invs, unsigned *n_inv_uses)
6367 : {
6368 601523554 : bitmap_iterator bi;
6369 601523554 : unsigned iid;
6370 :
6371 601523554 : if (!invs)
6372 478301092 : return;
6373 :
6374 123222462 : gcc_assert (n_inv_uses != NULL);
6375 213483891 : EXECUTE_IF_SET_IN_BITMAP (invs, 0, iid, bi)
6376 : {
6377 90261429 : n_inv_uses[iid]++;
6378 90261429 : if (n_inv_uses[iid] == 1)
6379 67535428 : ivs->n_invs++;
6380 : }
6381 : }
6382 :
6383 : /* Set cost pair for GROUP in set IVS to CP. */
6384 :
6385 : static void
6386 230246274 : iv_ca_set_cp (struct ivopts_data *data, class iv_ca *ivs,
6387 : struct iv_group *group, class cost_pair *cp)
6388 : {
6389 230246274 : unsigned gid = group->id, cid;
6390 :
6391 230246274 : if (ivs->cand_for_group[gid] == cp)
6392 : return;
6393 :
6394 217111219 : if (ivs->cand_for_group[gid])
6395 201543595 : iv_ca_set_no_cp (data, ivs, group);
6396 :
6397 217111219 : if (cp)
6398 : {
6399 217111219 : cid = cp->cand->id;
6400 :
6401 217111219 : ivs->bad_groups--;
6402 217111219 : ivs->cand_for_group[gid] = cp;
6403 217111219 : ivs->n_cand_uses[cid]++;
6404 217111219 : if (ivs->n_cand_uses[cid] == 1)
6405 : {
6406 83650558 : bitmap_set_bit (ivs->cands, cid);
6407 83650558 : if (!cp->cand->doloop_p || !targetm.have_count_reg_decr_p)
6408 83650558 : ivs->n_cands++;
6409 83650558 : ivs->cand_cost += cp->cand->cost;
6410 83650558 : iv_ca_set_add_invs (ivs, cp->cand->inv_vars, ivs->n_inv_var_uses);
6411 83650558 : iv_ca_set_add_invs (ivs, cp->cand->inv_exprs, ivs->n_inv_expr_uses);
6412 : }
6413 :
6414 217111219 : ivs->cand_use_cost += cp->cost;
6415 217111219 : iv_ca_set_add_invs (ivs, cp->inv_vars, ivs->n_inv_var_uses);
6416 217111219 : iv_ca_set_add_invs (ivs, cp->inv_exprs, ivs->n_inv_expr_uses);
6417 217111219 : iv_ca_recount_cost (data, ivs);
6418 : }
6419 : }
6420 :
6421 : /* Extend set IVS by expressing USE by some of the candidates in it
6422 : if possible. Consider all important candidates if candidates in
6423 : set IVS don't give any result. */
6424 :
6425 : static void
6426 3307550 : iv_ca_add_group (struct ivopts_data *data, class iv_ca *ivs,
6427 : struct iv_group *group)
6428 : {
6429 3307550 : class cost_pair *best_cp = NULL, *cp;
6430 3307550 : bitmap_iterator bi;
6431 3307550 : unsigned i;
6432 3307550 : struct iv_cand *cand;
6433 :
6434 3307550 : gcc_assert (ivs->upto >= group->id);
6435 3307550 : ivs->upto++;
6436 3307550 : ivs->bad_groups++;
6437 :
6438 6256078 : EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6439 : {
6440 2948528 : cand = data->vcands[i];
6441 2948528 : cp = get_group_iv_cost (data, group, cand);
6442 2948528 : if (cheaper_cost_pair (cp, best_cp))
6443 2033442 : best_cp = cp;
6444 : }
6445 :
6446 3307550 : if (best_cp == NULL)
6447 : {
6448 11877056 : EXECUTE_IF_SET_IN_BITMAP (data->important_candidates, 0, i, bi)
6449 : {
6450 10524364 : cand = data->vcands[i];
6451 10524364 : cp = get_group_iv_cost (data, group, cand);
6452 10524364 : if (cheaper_cost_pair (cp, best_cp))
6453 2417045 : best_cp = cp;
6454 : }
6455 : }
6456 :
6457 3307550 : iv_ca_set_cp (data, ivs, group, best_cp);
6458 3307550 : }
6459 :
6460 : /* Get cost for assignment IVS. */
6461 :
6462 : static comp_cost
6463 86192133 : iv_ca_cost (class iv_ca *ivs)
6464 : {
6465 : /* This was a conditional expression but it triggered a bug in
6466 : Sun C 5.5. */
6467 0 : if (ivs->bad_groups)
6468 93136 : return infinite_cost;
6469 : else
6470 86098997 : return ivs->cost;
6471 : }
6472 :
6473 : /* Compare if applying NEW_CP to GROUP for IVS introduces more invariants
6474 : than OLD_CP. Return 1, 0 and -1 for more, equal and fewer invariants
6475 : respectively. */
6476 :
6477 : static int
6478 40853258 : iv_ca_compare_deps (struct ivopts_data *data, class iv_ca *ivs,
6479 : struct iv_group *group, class cost_pair *old_cp,
6480 : class cost_pair *new_cp)
6481 : {
6482 40853258 : gcc_assert (old_cp && new_cp && old_cp != new_cp);
6483 40853258 : unsigned old_n_invs = ivs->n_invs;
6484 40853258 : iv_ca_set_cp (data, ivs, group, new_cp);
6485 40853258 : unsigned new_n_invs = ivs->n_invs;
6486 40853258 : iv_ca_set_cp (data, ivs, group, old_cp);
6487 :
6488 40853258 : return new_n_invs > old_n_invs ? 1 : (new_n_invs < old_n_invs ? -1 : 0);
6489 : }
6490 :
6491 : /* Creates change of expressing GROUP by NEW_CP instead of OLD_CP and chains
6492 : it before NEXT. */
6493 :
6494 : static struct iv_ca_delta *
6495 48200824 : iv_ca_delta_add (struct iv_group *group, class cost_pair *old_cp,
6496 : class cost_pair *new_cp, struct iv_ca_delta *next)
6497 : {
6498 0 : struct iv_ca_delta *change = XNEW (struct iv_ca_delta);
6499 :
6500 48200824 : change->group = group;
6501 48200824 : change->old_cp = old_cp;
6502 48200824 : change->new_cp = new_cp;
6503 48200824 : change->next = next;
6504 :
6505 48200824 : return change;
6506 : }
6507 :
6508 : /* Joins two lists of changes L1 and L2. Destructive -- old lists
6509 : are rewritten. */
6510 :
6511 : static struct iv_ca_delta *
6512 8156725 : iv_ca_delta_join (struct iv_ca_delta *l1, struct iv_ca_delta *l2)
6513 : {
6514 8156725 : struct iv_ca_delta *last;
6515 :
6516 0 : if (!l2)
6517 : return l1;
6518 :
6519 0 : if (!l1)
6520 : return l2;
6521 :
6522 3297203 : for (last = l1; last->next; last = last->next)
6523 1087404 : continue;
6524 2209799 : last->next = l2;
6525 :
6526 2209799 : return l1;
6527 1087404 : }
6528 :
6529 : /* Reverse the list of changes DELTA, forming the inverse to it. */
6530 :
6531 : static struct iv_ca_delta *
6532 0 : iv_ca_delta_reverse (struct iv_ca_delta *delta)
6533 : {
6534 0 : struct iv_ca_delta *act, *next, *prev = NULL;
6535 :
6536 161534392 : for (act = delta; act; act = next)
6537 : {
6538 90205752 : next = act->next;
6539 90205752 : act->next = prev;
6540 90205752 : prev = act;
6541 :
6542 90205752 : std::swap (act->old_cp, act->new_cp);
6543 : }
6544 :
6545 0 : return prev;
6546 : }
6547 :
6548 : /* Commit changes in DELTA to IVS. If FORWARD is false, the changes are
6549 : reverted instead. */
6550 :
6551 : static void
6552 75162908 : iv_ca_delta_commit (struct ivopts_data *data, class iv_ca *ivs,
6553 : struct iv_ca_delta *delta, bool forward)
6554 : {
6555 75162908 : class cost_pair *from, *to;
6556 75162908 : struct iv_ca_delta *act;
6557 :
6558 75162908 : if (!forward)
6559 75162908 : delta = iv_ca_delta_reverse (delta);
6560 :
6561 170289471 : for (act = delta; act; act = act->next)
6562 : {
6563 95126563 : from = act->old_cp;
6564 95126563 : to = act->new_cp;
6565 95126563 : gcc_assert (iv_ca_cand_for_group (ivs, act->group) == from);
6566 95126563 : iv_ca_set_cp (data, ivs, act->group, to);
6567 : }
6568 :
6569 75162908 : if (!forward)
6570 75162908 : iv_ca_delta_reverse (delta);
6571 75162908 : }
6572 :
6573 : /* Returns true if CAND is used in IVS. */
6574 :
6575 : static bool
6576 29709750 : iv_ca_cand_used_p (class iv_ca *ivs, struct iv_cand *cand)
6577 : {
6578 29709750 : return ivs->n_cand_uses[cand->id] > 0;
6579 : }
6580 :
6581 : /* Returns number of induction variable candidates in the set IVS. */
6582 :
6583 : static unsigned
6584 13070003 : iv_ca_n_cands (class iv_ca *ivs)
6585 : {
6586 13070003 : return ivs->n_cands;
6587 : }
6588 :
6589 : /* Free the list of changes DELTA. */
6590 :
6591 : static void
6592 45132108 : iv_ca_delta_free (struct iv_ca_delta **delta)
6593 : {
6594 45132108 : struct iv_ca_delta *act, *next;
6595 :
6596 93332932 : for (act = *delta; act; act = next)
6597 : {
6598 48200824 : next = act->next;
6599 48200824 : free (act);
6600 : }
6601 :
6602 45132108 : *delta = NULL;
6603 45132108 : }
6604 :
6605 : /* Allocates new iv candidates assignment. */
6606 :
6607 : static class iv_ca *
6608 1015410 : iv_ca_new (struct ivopts_data *data)
6609 : {
6610 1015410 : class iv_ca *nw = XNEW (class iv_ca);
6611 :
6612 1015410 : nw->upto = 0;
6613 1015410 : nw->bad_groups = 0;
6614 2030820 : nw->cand_for_group = XCNEWVEC (class cost_pair *,
6615 : data->vgroups.length ());
6616 2030820 : nw->n_cand_uses = XCNEWVEC (unsigned, data->vcands.length ());
6617 1015410 : nw->cands = BITMAP_ALLOC (NULL);
6618 1015410 : nw->n_cands = 0;
6619 1015410 : nw->n_invs = 0;
6620 1015410 : nw->cand_use_cost = no_cost;
6621 1015410 : nw->cand_cost = 0;
6622 1015410 : nw->n_inv_var_uses = XCNEWVEC (unsigned, data->max_inv_var_id + 1);
6623 1015410 : nw->n_inv_expr_uses = XCNEWVEC (unsigned, data->max_inv_expr_id + 1);
6624 1015410 : nw->cost = no_cost;
6625 :
6626 1015410 : return nw;
6627 : }
6628 :
6629 : /* Free memory occupied by the set IVS. */
6630 :
6631 : static void
6632 1015410 : iv_ca_free (class iv_ca **ivs)
6633 : {
6634 1015410 : free ((*ivs)->cand_for_group);
6635 1015410 : free ((*ivs)->n_cand_uses);
6636 1015410 : BITMAP_FREE ((*ivs)->cands);
6637 1015410 : free ((*ivs)->n_inv_var_uses);
6638 1015410 : free ((*ivs)->n_inv_expr_uses);
6639 1015410 : free (*ivs);
6640 1015410 : *ivs = NULL;
6641 1015410 : }
6642 :
6643 : /* Dumps IVS to FILE. */
6644 :
6645 : static void
6646 250 : iv_ca_dump (struct ivopts_data *data, FILE *file, class iv_ca *ivs)
6647 : {
6648 250 : unsigned i;
6649 250 : comp_cost cost = iv_ca_cost (ivs);
6650 :
6651 250 : fprintf (file, " cost: %" PRId64 " (complexity %d)\n", cost.cost,
6652 : cost.complexity);
6653 250 : fprintf (file, " reg_cost: %d\n",
6654 : ivopts_estimate_reg_pressure (data, ivs->n_invs, ivs->n_cands));
6655 250 : fprintf (file, " cand_cost: %" PRId64 "\n cand_group_cost: "
6656 : "%" PRId64 " (complexity %d)\n", ivs->cand_cost,
6657 : ivs->cand_use_cost.cost, ivs->cand_use_cost.complexity);
6658 250 : bitmap_print (file, ivs->cands, " candidates: ","\n");
6659 :
6660 1568 : for (i = 0; i < ivs->upto; i++)
6661 : {
6662 1068 : struct iv_group *group = data->vgroups[i];
6663 1068 : class cost_pair *cp = iv_ca_cand_for_group (ivs, group);
6664 1068 : if (cp)
6665 1068 : fprintf (file, " group:%d --> iv_cand:%d, cost=("
6666 1068 : "%" PRId64 ",%d)\n", group->id, cp->cand->id,
6667 : cp->cost.cost, cp->cost.complexity);
6668 : else
6669 0 : fprintf (file, " group:%d --> ??\n", group->id);
6670 : }
6671 :
6672 250 : const char *pref = "";
6673 250 : fprintf (file, " invariant variables: ");
6674 1454 : for (i = 1; i <= data->max_inv_var_id; i++)
6675 954 : if (ivs->n_inv_var_uses[i])
6676 : {
6677 142 : fprintf (file, "%s%d", pref, i);
6678 142 : pref = ", ";
6679 : }
6680 :
6681 250 : pref = "";
6682 250 : fprintf (file, "\n invariant expressions: ");
6683 2550 : for (i = 1; i <= data->max_inv_expr_id; i++)
6684 2050 : if (ivs->n_inv_expr_uses[i])
6685 : {
6686 308 : fprintf (file, "%s%d", pref, i);
6687 308 : pref = ", ";
6688 : }
6689 :
6690 250 : fprintf (file, "\n\n");
6691 250 : }
6692 :
6693 : /* Try changing candidate in IVS to CAND for each use. Return cost of the
6694 : new set, and store differences in DELTA. Number of induction variables
6695 : in the new set is stored to N_IVS. MIN_NCAND is a flag. When it is true
6696 : the function will try to find a solution with minimal iv candidates. */
6697 :
6698 : static comp_cost
6699 22116567 : iv_ca_extend (struct ivopts_data *data, class iv_ca *ivs,
6700 : struct iv_cand *cand, struct iv_ca_delta **delta,
6701 : unsigned *n_ivs, bool min_ncand)
6702 : {
6703 22116567 : unsigned i;
6704 22116567 : comp_cost cost;
6705 22116567 : struct iv_group *group;
6706 22116567 : class cost_pair *old_cp, *new_cp;
6707 :
6708 22116567 : *delta = NULL;
6709 125313553 : for (i = 0; i < ivs->upto; i++)
6710 : {
6711 103196986 : group = data->vgroups[i];
6712 103196986 : old_cp = iv_ca_cand_for_group (ivs, group);
6713 :
6714 103196986 : if (old_cp
6715 103196986 : && old_cp->cand == cand)
6716 9046564 : continue;
6717 :
6718 94150422 : new_cp = get_group_iv_cost (data, group, cand);
6719 94150422 : if (!new_cp)
6720 37788251 : continue;
6721 :
6722 56362171 : if (!min_ncand)
6723 : {
6724 40853258 : int cmp_invs = iv_ca_compare_deps (data, ivs, group, old_cp, new_cp);
6725 : /* Skip if new_cp depends on more invariants. */
6726 40853258 : if (cmp_invs > 0)
6727 9987600 : continue;
6728 :
6729 30865658 : int cmp_cost = compare_cost_pair (new_cp, old_cp);
6730 : /* Skip if new_cp is not cheaper. */
6731 30865658 : if (cmp_cost > 0 || (cmp_cost == 0 && cmp_invs == 0))
6732 24063844 : continue;
6733 : }
6734 :
6735 22310727 : *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
6736 : }
6737 :
6738 22116567 : iv_ca_delta_commit (data, ivs, *delta, true);
6739 22116567 : cost = iv_ca_cost (ivs);
6740 22116567 : if (n_ivs)
6741 13070003 : *n_ivs = iv_ca_n_cands (ivs);
6742 22116567 : iv_ca_delta_commit (data, ivs, *delta, false);
6743 :
6744 22116567 : return cost;
6745 : }
6746 :
6747 : /* Try narrowing set IVS by removing CAND. Return the cost of
6748 : the new set and store the differences in DELTA. START is
6749 : the candidate with which we start narrowing. */
6750 :
6751 : static comp_cost
6752 15865249 : iv_ca_narrow (struct ivopts_data *data, class iv_ca *ivs,
6753 : struct iv_cand *cand, struct iv_cand *start,
6754 : struct iv_ca_delta **delta)
6755 : {
6756 15865249 : unsigned i, ci;
6757 15865249 : struct iv_group *group;
6758 15865249 : class cost_pair *old_cp, *new_cp, *cp;
6759 15865249 : bitmap_iterator bi;
6760 15865249 : struct iv_cand *cnd;
6761 15865249 : comp_cost cost, best_cost, acost;
6762 :
6763 15865249 : *delta = NULL;
6764 86439112 : for (i = 0; i < data->vgroups.length (); i++)
6765 : {
6766 81048084 : group = data->vgroups[i];
6767 :
6768 81048084 : old_cp = iv_ca_cand_for_group (ivs, group);
6769 81048084 : if (old_cp->cand != cand)
6770 58382187 : continue;
6771 :
6772 22665897 : best_cost = iv_ca_cost (ivs);
6773 : /* Start narrowing with START. */
6774 22665897 : new_cp = get_group_iv_cost (data, group, start);
6775 :
6776 22665897 : if (data->consider_all_candidates)
6777 : {
6778 99846671 : EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, ci, bi)
6779 : {
6780 78343963 : if (ci == cand->id || (start && ci == start->id))
6781 37178511 : continue;
6782 :
6783 41165452 : cnd = data->vcands[ci];
6784 :
6785 41165452 : cp = get_group_iv_cost (data, group, cnd);
6786 41165452 : if (!cp)
6787 24318718 : continue;
6788 :
6789 16846734 : iv_ca_set_cp (data, ivs, group, cp);
6790 16846734 : acost = iv_ca_cost (ivs);
6791 :
6792 16846734 : if (acost < best_cost)
6793 : {
6794 1842776 : best_cost = acost;
6795 1842776 : new_cp = cp;
6796 : }
6797 : }
6798 : }
6799 : else
6800 : {
6801 4633248 : EXECUTE_IF_AND_IN_BITMAP (group->related_cands, ivs->cands, 0, ci, bi)
6802 : {
6803 3470059 : if (ci == cand->id || (start && ci == start->id))
6804 1923609 : continue;
6805 :
6806 1546450 : cnd = data->vcands[ci];
6807 :
6808 1546450 : cp = get_group_iv_cost (data, group, cnd);
6809 1546450 : if (!cp)
6810 0 : continue;
6811 :
6812 1546450 : iv_ca_set_cp (data, ivs, group, cp);
6813 1546450 : acost = iv_ca_cost (ivs);
6814 :
6815 1546450 : if (acost < best_cost)
6816 : {
6817 34189 : best_cost = acost;
6818 34189 : new_cp = cp;
6819 : }
6820 : }
6821 : }
6822 : /* Restore to old cp for use. */
6823 22665897 : iv_ca_set_cp (data, ivs, group, old_cp);
6824 :
6825 22665897 : if (!new_cp)
6826 : {
6827 10474221 : iv_ca_delta_free (delta);
6828 10474221 : return infinite_cost;
6829 : }
6830 :
6831 12191676 : *delta = iv_ca_delta_add (group, old_cp, new_cp, *delta);
6832 : }
6833 :
6834 5391028 : iv_ca_delta_commit (data, ivs, *delta, true);
6835 5391028 : cost = iv_ca_cost (ivs);
6836 5391028 : iv_ca_delta_commit (data, ivs, *delta, false);
6837 :
6838 5391028 : return cost;
6839 : }
6840 :
6841 : /* Try optimizing the set of candidates IVS by removing candidates different
6842 : from to EXCEPT_CAND from it. Return cost of the new set, and store
6843 : differences in DELTA. */
6844 :
6845 : static comp_cost
6846 9206197 : iv_ca_prune (struct ivopts_data *data, class iv_ca *ivs,
6847 : struct iv_cand *except_cand, struct iv_ca_delta **delta)
6848 : {
6849 9206197 : bitmap_iterator bi;
6850 9206197 : struct iv_ca_delta *act_delta, *best_delta;
6851 9206197 : unsigned i;
6852 9206197 : comp_cost best_cost, acost;
6853 9206197 : struct iv_cand *cand;
6854 :
6855 9206197 : best_delta = NULL;
6856 9206197 : best_cost = iv_ca_cost (ivs);
6857 :
6858 31546047 : EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6859 : {
6860 22339850 : cand = data->vcands[i];
6861 :
6862 22339850 : if (cand == except_cand)
6863 6474601 : continue;
6864 :
6865 15865249 : acost = iv_ca_narrow (data, ivs, cand, except_cand, &act_delta);
6866 :
6867 15865249 : if (acost < best_cost)
6868 : {
6869 2354027 : best_cost = acost;
6870 2354027 : iv_ca_delta_free (&best_delta);
6871 2354027 : best_delta = act_delta;
6872 : }
6873 : else
6874 13511222 : iv_ca_delta_free (&act_delta);
6875 : }
6876 :
6877 9206197 : if (!best_delta)
6878 : {
6879 6994460 : *delta = NULL;
6880 6994460 : return best_cost;
6881 : }
6882 :
6883 : /* Recurse to possibly remove other unnecessary ivs. */
6884 2211737 : iv_ca_delta_commit (data, ivs, best_delta, true);
6885 2211737 : best_cost = iv_ca_prune (data, ivs, except_cand, delta);
6886 2211737 : iv_ca_delta_commit (data, ivs, best_delta, false);
6887 2211737 : *delta = iv_ca_delta_join (best_delta, *delta);
6888 2211737 : return best_cost;
6889 : }
6890 :
6891 : /* Check if CAND_IDX is a candidate other than OLD_CAND and has
6892 : cheaper local cost for GROUP than BEST_CP. Return pointer to
6893 : the corresponding cost_pair, otherwise just return BEST_CP. */
6894 :
6895 : static class cost_pair*
6896 29251738 : cheaper_cost_with_cand (struct ivopts_data *data, struct iv_group *group,
6897 : unsigned int cand_idx, struct iv_cand *old_cand,
6898 : class cost_pair *best_cp)
6899 : {
6900 29251738 : struct iv_cand *cand;
6901 29251738 : class cost_pair *cp;
6902 :
6903 29251738 : gcc_assert (old_cand != NULL && best_cp != NULL);
6904 29251738 : if (cand_idx == old_cand->id)
6905 : return best_cp;
6906 :
6907 26430290 : cand = data->vcands[cand_idx];
6908 26430290 : cp = get_group_iv_cost (data, group, cand);
6909 26430290 : if (cp != NULL && cheaper_cost_pair (cp, best_cp))
6910 1708284 : return cp;
6911 :
6912 : return best_cp;
6913 : }
6914 :
6915 : /* Try breaking local optimal fixed-point for IVS by replacing candidates
6916 : which are used by more than one iv uses. For each of those candidates,
6917 : this function tries to represent iv uses under that candidate using
6918 : other ones with lower local cost, then tries to prune the new set.
6919 : If the new set has lower cost, It returns the new cost after recording
6920 : candidate replacement in list DELTA. */
6921 :
6922 : static comp_cost
6923 1014506 : iv_ca_replace (struct ivopts_data *data, class iv_ca *ivs,
6924 : struct iv_ca_delta **delta)
6925 : {
6926 1014506 : bitmap_iterator bi, bj;
6927 1014506 : unsigned int i, j, k;
6928 1014506 : struct iv_cand *cand;
6929 1014506 : comp_cost orig_cost, acost;
6930 1014506 : struct iv_ca_delta *act_delta, *tmp_delta;
6931 1014506 : class cost_pair *old_cp, *best_cp = NULL;
6932 :
6933 1014506 : *delta = NULL;
6934 1014506 : orig_cost = iv_ca_cost (ivs);
6935 :
6936 2393444 : EXECUTE_IF_SET_IN_BITMAP (ivs->cands, 0, i, bi)
6937 : {
6938 1411966 : if (ivs->n_cand_uses[i] == 1
6939 1040981 : || ivs->n_cand_uses[i] > ALWAYS_PRUNE_CAND_SET_BOUND)
6940 376152 : continue;
6941 :
6942 1035814 : cand = data->vcands[i];
6943 :
6944 1035814 : act_delta = NULL;
6945 : /* Represent uses under current candidate using other ones with
6946 : lower local cost. */
6947 5436849 : for (j = 0; j < ivs->upto; j++)
6948 : {
6949 4401035 : struct iv_group *group = data->vgroups[j];
6950 4401035 : old_cp = iv_ca_cand_for_group (ivs, group);
6951 :
6952 4401035 : if (old_cp->cand != cand)
6953 1579587 : continue;
6954 :
6955 2821448 : best_cp = old_cp;
6956 2821448 : if (data->consider_all_candidates)
6957 31961030 : for (k = 0; k < data->vcands.length (); k++)
6958 29147327 : best_cp = cheaper_cost_with_cand (data, group, k,
6959 : old_cp->cand, best_cp);
6960 : else
6961 112156 : EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, k, bj)
6962 104411 : best_cp = cheaper_cost_with_cand (data, group, k,
6963 : old_cp->cand, best_cp);
6964 :
6965 2821448 : if (best_cp == old_cp)
6966 1384005 : continue;
6967 :
6968 1437443 : act_delta = iv_ca_delta_add (group, old_cp, best_cp, act_delta);
6969 : }
6970 : /* No need for further prune. */
6971 1035814 : if (!act_delta)
6972 254701 : continue;
6973 :
6974 : /* Prune the new candidate set. */
6975 781113 : iv_ca_delta_commit (data, ivs, act_delta, true);
6976 781113 : acost = iv_ca_prune (data, ivs, NULL, &tmp_delta);
6977 781113 : iv_ca_delta_commit (data, ivs, act_delta, false);
6978 781113 : act_delta = iv_ca_delta_join (act_delta, tmp_delta);
6979 :
6980 781113 : if (acost < orig_cost)
6981 : {
6982 33028 : *delta = act_delta;
6983 33028 : return acost;
6984 : }
6985 : else
6986 748085 : iv_ca_delta_free (&act_delta);
6987 : }
6988 :
6989 981478 : return orig_cost;
6990 : }
6991 :
6992 : /* Tries to extend the sets IVS in the best possible way in order to
6993 : express the GROUP. If ORIGINALP is true, prefer candidates from
6994 : the original set of IVs, otherwise favor important candidates not
6995 : based on any memory object. */
6996 :
6997 : static bool
6998 3307550 : try_add_cand_for (struct ivopts_data *data, class iv_ca *ivs,
6999 : struct iv_group *group, bool originalp)
7000 : {
7001 3307550 : comp_cost best_cost, act_cost;
7002 3307550 : unsigned i;
7003 3307550 : bitmap_iterator bi;
7004 3307550 : struct iv_cand *cand;
7005 3307550 : struct iv_ca_delta *best_delta = NULL, *act_delta;
7006 3307550 : class cost_pair *cp;
7007 :
7008 3307550 : iv_ca_add_group (data, ivs, group);
7009 3307550 : best_cost = iv_ca_cost (ivs);
7010 3307550 : cp = iv_ca_cand_for_group (ivs, group);
7011 3307550 : if (cp)
7012 : {
7013 3214414 : best_delta = iv_ca_delta_add (group, NULL, cp, NULL);
7014 3214414 : iv_ca_set_no_cp (data, ivs, group);
7015 : }
7016 :
7017 : /* If ORIGINALP is true, try to find the original IV for the use. Otherwise
7018 : first try important candidates not based on any memory object. Only if
7019 : this fails, try the specific ones. Rationale -- in loops with many
7020 : variables the best choice often is to use just one generic biv. If we
7021 : added here many ivs specific to the uses, the optimization algorithm later
7022 : would be likely to get stuck in a local minimum, thus causing us to create
7023 : too many ivs. The approach from few ivs to more seems more likely to be
7024 : successful -- starting from few ivs, replacing an expensive use by a
7025 : specific iv should always be a win. */
7026 30773314 : EXECUTE_IF_SET_IN_BITMAP (group->related_cands, 0, i, bi)
7027 : {
7028 27465764 : cand = data->vcands[i];
7029 :
7030 27465764 : if (originalp && cand->pos !=IP_ORIGINAL)
7031 10823368 : continue;
7032 :
7033 13732882 : if (!originalp && cand->iv->base_object != NULL_TREE)
7034 2530744 : continue;
7035 :
7036 14111652 : if (iv_ca_cand_used_p (ivs, cand))
7037 1518662 : continue;
7038 :
7039 12592990 : cp = get_group_iv_cost (data, group, cand);
7040 12592990 : if (!cp)
7041 3661953 : continue;
7042 :
7043 8931037 : iv_ca_set_cp (data, ivs, group, cp);
7044 8931037 : act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL,
7045 : true);
7046 8931037 : iv_ca_set_no_cp (data, ivs, group);
7047 8931037 : act_delta = iv_ca_delta_add (group, NULL, cp, act_delta);
7048 :
7049 8931037 : if (act_cost < best_cost)
7050 : {
7051 361875 : best_cost = act_cost;
7052 :
7053 361875 : iv_ca_delta_free (&best_delta);
7054 361875 : best_delta = act_delta;
7055 : }
7056 : else
7057 8569162 : iv_ca_delta_free (&act_delta);
7058 : }
7059 :
7060 3307550 : if (best_cost.infinite_cost_p ())
7061 : {
7062 733302 : for (i = 0; i < group->n_map_members; i++)
7063 : {
7064 665562 : cp = group->cost_map + i;
7065 665562 : cand = cp->cand;
7066 665562 : if (!cand)
7067 550035 : continue;
7068 :
7069 : /* Already tried this. */
7070 115527 : if (cand->important)
7071 : {
7072 0 : if (originalp && cand->pos == IP_ORIGINAL)
7073 0 : continue;
7074 0 : if (!originalp && cand->iv->base_object == NULL_TREE)
7075 0 : continue;
7076 : }
7077 :
7078 115527 : if (iv_ca_cand_used_p (ivs, cand))
7079 0 : continue;
7080 :
7081 115527 : act_delta = NULL;
7082 115527 : iv_ca_set_cp (data, ivs, group, cp);
7083 115527 : act_cost = iv_ca_extend (data, ivs, cand, &act_delta, NULL, true);
7084 115527 : iv_ca_set_no_cp (data, ivs, group);
7085 115527 : act_delta = iv_ca_delta_add (group,
7086 : iv_ca_cand_for_group (ivs, group),
7087 : cp, act_delta);
7088 :
7089 115527 : if (act_cost < best_cost)
7090 : {
7091 69191 : best_cost = act_cost;
7092 :
7093 69191 : if (best_delta)
7094 2355 : iv_ca_delta_free (&best_delta);
7095 69191 : best_delta = act_delta;
7096 : }
7097 : else
7098 46336 : iv_ca_delta_free (&act_delta);
7099 : }
7100 : }
7101 :
7102 3307550 : iv_ca_delta_commit (data, ivs, best_delta, true);
7103 3307550 : iv_ca_delta_free (&best_delta);
7104 :
7105 3307550 : return !best_cost.infinite_cost_p ();
7106 : }
7107 :
7108 : /* Finds an initial assignment of candidates to uses. */
7109 :
7110 : static class iv_ca *
7111 1015410 : get_initial_solution (struct ivopts_data *data, bool originalp)
7112 : {
7113 1015410 : unsigned i;
7114 1015410 : class iv_ca *ivs = iv_ca_new (data);
7115 :
7116 4322056 : for (i = 0; i < data->vgroups.length (); i++)
7117 3307550 : if (!try_add_cand_for (data, ivs, data->vgroups[i], originalp))
7118 : {
7119 904 : iv_ca_free (&ivs);
7120 904 : return NULL;
7121 : }
7122 :
7123 : return ivs;
7124 : }
7125 :
7126 : /* Tries to improve set of induction variables IVS. TRY_REPLACE_P
7127 : points to a bool variable, this function tries to break local
7128 : optimal fixed-point by replacing candidates in IVS if it's true. */
7129 :
7130 : static bool
7131 1541224 : try_improve_iv_set (struct ivopts_data *data,
7132 : class iv_ca *ivs, bool *try_replace_p)
7133 : {
7134 1541224 : unsigned i, n_ivs;
7135 1541224 : comp_cost acost, best_cost = iv_ca_cost (ivs);
7136 1541224 : struct iv_ca_delta *best_delta = NULL, *act_delta, *tmp_delta;
7137 1541224 : struct iv_cand *cand;
7138 :
7139 : /* Try extending the set of induction variables by one. */
7140 17023795 : for (i = 0; i < data->vcands.length (); i++)
7141 : {
7142 15482571 : cand = data->vcands[i];
7143 :
7144 15482571 : if (iv_ca_cand_used_p (ivs, cand))
7145 2412568 : continue;
7146 :
7147 13070003 : acost = iv_ca_extend (data, ivs, cand, &act_delta, &n_ivs, false);
7148 13070003 : if (!act_delta)
7149 7839446 : continue;
7150 :
7151 : /* If we successfully added the candidate and the set is small enough,
7152 : try optimizing it by removing other candidates. */
7153 5230557 : if (n_ivs <= ALWAYS_PRUNE_CAND_SET_BOUND)
7154 : {
7155 5163875 : iv_ca_delta_commit (data, ivs, act_delta, true);
7156 5163875 : acost = iv_ca_prune (data, ivs, cand, &tmp_delta);
7157 5163875 : iv_ca_delta_commit (data, ivs, act_delta, false);
7158 5163875 : act_delta = iv_ca_delta_join (act_delta, tmp_delta);
7159 : }
7160 :
7161 5230557 : if (acost < best_cost)
7162 : {
7163 635390 : best_cost = acost;
7164 635390 : iv_ca_delta_free (&best_delta);
7165 635390 : best_delta = act_delta;
7166 : }
7167 : else
7168 4595167 : iv_ca_delta_free (&act_delta);
7169 : }
7170 :
7171 1541224 : if (!best_delta)
7172 : {
7173 : /* Try removing the candidates from the set instead. */
7174 1049472 : best_cost = iv_ca_prune (data, ivs, NULL, &best_delta);
7175 :
7176 1049472 : if (!best_delta && *try_replace_p)
7177 : {
7178 1014506 : *try_replace_p = false;
7179 : /* So far candidate selecting algorithm tends to choose fewer IVs
7180 : so that it can handle cases in which loops have many variables
7181 : but the best choice is often to use only one general biv. One
7182 : weakness is it can't handle opposite cases, in which different
7183 : candidates should be chosen with respect to each use. To solve
7184 : the problem, we replace candidates in a manner described by the
7185 : comments of iv_ca_replace, thus give general algorithm a chance
7186 : to break local optimal fixed-point in these cases. */
7187 1014506 : best_cost = iv_ca_replace (data, ivs, &best_delta);
7188 : }
7189 :
7190 1049472 : if (!best_delta)
7191 : return false;
7192 : }
7193 :
7194 526718 : iv_ca_delta_commit (data, ivs, best_delta, true);
7195 526718 : iv_ca_delta_free (&best_delta);
7196 1053436 : return best_cost == iv_ca_cost (ivs);
7197 : }
7198 :
7199 : /* Attempts to find the optimal set of induction variables. We do simple
7200 : greedy heuristic -- we try to replace at most one candidate in the selected
7201 : solution and remove the unused ivs while this improves the cost. */
7202 :
7203 : static class iv_ca *
7204 1015410 : find_optimal_iv_set_1 (struct ivopts_data *data, bool originalp)
7205 : {
7206 1015410 : class iv_ca *set;
7207 1015410 : bool try_replace_p = true;
7208 :
7209 : /* Get the initial solution. */
7210 1015410 : set = get_initial_solution (data, originalp);
7211 1015410 : if (!set)
7212 : {
7213 904 : if (dump_file && (dump_flags & TDF_DETAILS))
7214 0 : fprintf (dump_file, "Unable to substitute for ivs, failed.\n");
7215 : return NULL;
7216 : }
7217 :
7218 1014506 : if (dump_file && (dump_flags & TDF_DETAILS))
7219 : {
7220 134 : fprintf (dump_file, "Initial set of candidates:\n");
7221 134 : iv_ca_dump (data, dump_file, set);
7222 : }
7223 :
7224 1541224 : while (try_improve_iv_set (data, set, &try_replace_p))
7225 : {
7226 526718 : if (dump_file && (dump_flags & TDF_DETAILS))
7227 : {
7228 116 : fprintf (dump_file, "Improved to:\n");
7229 116 : iv_ca_dump (data, dump_file, set);
7230 : }
7231 : }
7232 :
7233 : /* If the set has infinite_cost, it can't be optimal. */
7234 2029012 : if (iv_ca_cost (set).infinite_cost_p ())
7235 : {
7236 0 : if (dump_file && (dump_flags & TDF_DETAILS))
7237 0 : fprintf (dump_file,
7238 : "Overflow to infinite cost in try_improve_iv_set.\n");
7239 0 : iv_ca_free (&set);
7240 : }
7241 1014506 : return set;
7242 : }
7243 :
7244 : static class iv_ca *
7245 507705 : find_optimal_iv_set (struct ivopts_data *data)
7246 : {
7247 507705 : unsigned i;
7248 507705 : comp_cost cost, origcost;
7249 507705 : class iv_ca *set, *origset;
7250 :
7251 : /* Determine the cost based on a strategy that starts with original IVs,
7252 : and try again using a strategy that prefers candidates not based
7253 : on any IVs. */
7254 507705 : origset = find_optimal_iv_set_1 (data, true);
7255 507705 : set = find_optimal_iv_set_1 (data, false);
7256 :
7257 507705 : if (!origset && !set)
7258 : return NULL;
7259 :
7260 507253 : origcost = origset ? iv_ca_cost (origset) : infinite_cost;
7261 507253 : cost = set ? iv_ca_cost (set) : infinite_cost;
7262 :
7263 507253 : if (dump_file && (dump_flags & TDF_DETAILS))
7264 : {
7265 67 : fprintf (dump_file, "Original cost %" PRId64 " (complexity %d)\n\n",
7266 : origcost.cost, origcost.complexity);
7267 67 : fprintf (dump_file, "Final cost %" PRId64 " (complexity %d)\n\n",
7268 : cost.cost, cost.complexity);
7269 : }
7270 :
7271 : /* Choose the one with the best cost. */
7272 507253 : if (origcost <= cost)
7273 : {
7274 473688 : if (set)
7275 473688 : iv_ca_free (&set);
7276 473688 : set = origset;
7277 : }
7278 33565 : else if (origset)
7279 33565 : iv_ca_free (&origset);
7280 :
7281 2159917 : for (i = 0; i < data->vgroups.length (); i++)
7282 : {
7283 1652664 : struct iv_group *group = data->vgroups[i];
7284 1652664 : group->selected = iv_ca_cand_for_group (set, group)->cand;
7285 : }
7286 :
7287 507253 : return set;
7288 : }
7289 :
7290 : /* Creates a new induction variable corresponding to CAND. */
7291 :
7292 : static void
7293 706189 : create_new_iv (struct ivopts_data *data, struct iv_cand *cand)
7294 : {
7295 706189 : gimple_stmt_iterator incr_pos;
7296 706189 : tree base;
7297 706189 : struct iv_use *use;
7298 706189 : struct iv_group *group;
7299 706189 : bool after = false;
7300 :
7301 706189 : gcc_assert (cand->iv != NULL);
7302 :
7303 706189 : switch (cand->pos)
7304 : {
7305 492144 : case IP_NORMAL:
7306 492144 : incr_pos = gsi_last_bb (ip_normal_pos (data->current_loop));
7307 492144 : break;
7308 :
7309 11487 : case IP_END:
7310 11487 : incr_pos = gsi_last_bb (ip_end_pos (data->current_loop));
7311 11487 : after = true;
7312 11487 : gcc_assert (gsi_end_p (incr_pos) || !stmt_ends_bb_p (*incr_pos));
7313 : break;
7314 :
7315 0 : case IP_AFTER_USE:
7316 0 : after = true;
7317 : /* fall through */
7318 0 : case IP_BEFORE_USE:
7319 0 : incr_pos = gsi_for_stmt (cand->incremented_at);
7320 0 : break;
7321 :
7322 202558 : case IP_ORIGINAL:
7323 : /* Mark that the iv is preserved. */
7324 202558 : name_info (data, cand->var_before)->preserve_biv = true;
7325 202558 : name_info (data, cand->var_after)->preserve_biv = true;
7326 :
7327 : /* Rewrite the increment so that it uses var_before directly. Missed
7328 : optimization can result in a use IV with zero step, avoid
7329 : crashing in that case. */
7330 202558 : use = find_interesting_uses_op (data, cand->var_after);
7331 202558 : if (use)
7332 : {
7333 202553 : group = data->vgroups[use->group_id];
7334 202553 : group->selected = cand;
7335 : }
7336 202558 : return;
7337 : }
7338 :
7339 503631 : gimple_add_tmp_var (cand->var_before);
7340 :
7341 503631 : base = unshare_expr (cand->iv->base);
7342 :
7343 : /* The step computation could invoke UB when the loop does not iterate.
7344 : Avoid inserting it on the preheader in its native form but rewrite
7345 : it to a well-defined form. This also helps masking SCEV issues
7346 : which freely re-associates the IV computations when building up
7347 : CHRECs without much regard for signed overflow invoking UB. */
7348 503631 : gimple_seq stmts = NULL;
7349 503631 : tree step = force_gimple_operand (unshare_expr (cand->iv->step), &stmts,
7350 : true, NULL_TREE);
7351 503631 : if (stmts)
7352 : {
7353 158790 : for (auto gsi = gsi_start (stmts); !gsi_end_p (gsi); gsi_next (&gsi))
7354 104575 : if (gimple_needing_rewrite_undefined (gsi_stmt (gsi)))
7355 11933 : rewrite_to_defined_unconditional (&gsi);
7356 54215 : gsi_insert_seq_on_edge_immediate
7357 54215 : (loop_preheader_edge (data->current_loop), stmts);
7358 : }
7359 :
7360 503631 : create_iv (base, PLUS_EXPR, step,
7361 : cand->var_before, data->current_loop,
7362 : &incr_pos, after, &cand->var_before, &cand->var_after);
7363 : }
7364 :
7365 : /* Creates new induction variables described in SET. */
7366 :
7367 : static void
7368 507253 : create_new_ivs (struct ivopts_data *data, class iv_ca *set)
7369 : {
7370 507253 : unsigned i;
7371 507253 : struct iv_cand *cand;
7372 507253 : bitmap_iterator bi;
7373 :
7374 1213442 : EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
7375 : {
7376 706189 : cand = data->vcands[i];
7377 706189 : create_new_iv (data, cand);
7378 : }
7379 :
7380 507253 : if (dump_file && (dump_flags & TDF_DETAILS))
7381 : {
7382 67 : fprintf (dump_file, "Selected IV set for loop %d",
7383 67 : data->current_loop->num);
7384 67 : if (data->loop_loc != UNKNOWN_LOCATION)
7385 65 : fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
7386 130 : LOCATION_LINE (data->loop_loc));
7387 67 : fprintf (dump_file, ", " HOST_WIDE_INT_PRINT_UNSIGNED " avg niters",
7388 : avg_loop_niter (data->current_loop));
7389 67 : fprintf (dump_file, ", %lu IVs:\n", bitmap_count_bits (set->cands));
7390 178 : EXECUTE_IF_SET_IN_BITMAP (set->cands, 0, i, bi)
7391 : {
7392 111 : cand = data->vcands[i];
7393 111 : dump_cand (dump_file, cand);
7394 : }
7395 67 : fprintf (dump_file, "\n");
7396 : }
7397 507253 : }
7398 :
7399 : /* Rewrites USE (definition of iv used in a nonlinear expression)
7400 : using candidate CAND. */
7401 :
7402 : static void
7403 625161 : rewrite_use_nonlinear_expr (struct ivopts_data *data,
7404 : struct iv_use *use, struct iv_cand *cand)
7405 : {
7406 625161 : gassign *ass;
7407 625161 : gimple_stmt_iterator bsi;
7408 625161 : tree comp, type = get_use_type (use), tgt;
7409 :
7410 : /* An important special case -- if we are asked to express value of
7411 : the original iv by itself, just exit; there is no need to
7412 : introduce a new computation (that might also need casting the
7413 : variable to unsigned and back). */
7414 625161 : if (cand->pos == IP_ORIGINAL
7415 331262 : && cand->incremented_at == use->stmt)
7416 : {
7417 202553 : tree op = NULL_TREE;
7418 202553 : enum tree_code stmt_code;
7419 :
7420 202553 : gcc_assert (is_gimple_assign (use->stmt));
7421 202553 : gcc_assert (gimple_assign_lhs (use->stmt) == cand->var_after);
7422 :
7423 : /* Check whether we may leave the computation unchanged.
7424 : This is the case only if it does not rely on other
7425 : computations in the loop -- otherwise, the computation
7426 : we rely upon may be removed in remove_unused_ivs,
7427 : thus leading to ICE. */
7428 202553 : stmt_code = gimple_assign_rhs_code (use->stmt);
7429 202553 : if (stmt_code == PLUS_EXPR
7430 202553 : || stmt_code == MINUS_EXPR
7431 202553 : || stmt_code == POINTER_PLUS_EXPR)
7432 : {
7433 199999 : if (gimple_assign_rhs1 (use->stmt) == cand->var_before)
7434 199213 : op = gimple_assign_rhs2 (use->stmt);
7435 786 : else if (gimple_assign_rhs2 (use->stmt) == cand->var_before)
7436 : op = gimple_assign_rhs1 (use->stmt);
7437 : }
7438 :
7439 199711 : if (op != NULL_TREE)
7440 : {
7441 199711 : if (expr_invariant_in_loop_p (data->current_loop, op))
7442 285322 : return;
7443 164 : if (TREE_CODE (op) == SSA_NAME)
7444 : {
7445 164 : struct iv *iv = get_iv (data, op);
7446 164 : if (iv != NULL && integer_zerop (iv->step))
7447 : return;
7448 : }
7449 : }
7450 : }
7451 :
7452 425450 : switch (gimple_code (use->stmt))
7453 : {
7454 127055 : case GIMPLE_PHI:
7455 127055 : tgt = PHI_RESULT (use->stmt);
7456 :
7457 : /* If we should keep the biv, do not replace it. */
7458 127055 : if (name_info (data, tgt)->preserve_biv)
7459 : return;
7460 :
7461 41444 : bsi = gsi_after_labels (gimple_bb (use->stmt));
7462 41444 : break;
7463 :
7464 298395 : case GIMPLE_ASSIGN:
7465 298395 : tgt = gimple_assign_lhs (use->stmt);
7466 298395 : bsi = gsi_for_stmt (use->stmt);
7467 298395 : break;
7468 :
7469 0 : default:
7470 0 : gcc_unreachable ();
7471 : }
7472 :
7473 1019517 : aff_tree aff_inv, aff_var;
7474 339839 : if (!get_computation_aff_1 (data, use->stmt, use, cand, &aff_inv, &aff_var))
7475 0 : gcc_unreachable ();
7476 :
7477 339839 : unshare_aff_combination (&aff_inv);
7478 339839 : unshare_aff_combination (&aff_var);
7479 : /* Prefer CSE opportunity than loop invariant by adding offset at last
7480 : so that iv_uses have different offsets can be CSEed. */
7481 679678 : poly_widest_int offset = aff_inv.offset;
7482 339839 : aff_inv.offset = 0;
7483 :
7484 339839 : gimple_seq stmt_list = NULL, seq = NULL;
7485 339839 : tree comp_op1 = aff_combination_to_tree (&aff_inv);
7486 339839 : tree comp_op2 = aff_combination_to_tree (&aff_var);
7487 339839 : gcc_assert (comp_op1 && comp_op2);
7488 :
7489 339839 : comp_op1 = force_gimple_operand (comp_op1, &seq, true, NULL);
7490 339839 : gimple_seq_add_seq (&stmt_list, seq);
7491 339839 : comp_op2 = force_gimple_operand (comp_op2, &seq, true, NULL);
7492 339839 : gimple_seq_add_seq (&stmt_list, seq);
7493 :
7494 339839 : if (POINTER_TYPE_P (TREE_TYPE (comp_op2)))
7495 : std::swap (comp_op1, comp_op2);
7496 :
7497 339839 : if (POINTER_TYPE_P (TREE_TYPE (comp_op1)))
7498 : {
7499 0 : comp = fold_build_pointer_plus (comp_op1,
7500 : fold_convert (sizetype, comp_op2));
7501 0 : comp = fold_build_pointer_plus (comp,
7502 : wide_int_to_tree (sizetype, offset));
7503 : }
7504 : else
7505 : {
7506 339839 : comp = fold_build2 (PLUS_EXPR, TREE_TYPE (comp_op1), comp_op1,
7507 : fold_convert (TREE_TYPE (comp_op1), comp_op2));
7508 339839 : comp = fold_build2 (PLUS_EXPR, TREE_TYPE (comp_op1), comp,
7509 : wide_int_to_tree (TREE_TYPE (comp_op1), offset));
7510 : }
7511 :
7512 339839 : comp = fold_convert (type, comp);
7513 339839 : comp = force_gimple_operand (comp, &seq, false, NULL);
7514 339839 : gimple_seq_add_seq (&stmt_list, seq);
7515 339839 : if (gimple_code (use->stmt) != GIMPLE_PHI
7516 : /* We can't allow re-allocating the stmt as it might be pointed
7517 : to still. */
7518 339839 : && (get_gimple_rhs_num_ops (TREE_CODE (comp))
7519 298395 : >= gimple_num_ops (gsi_stmt (bsi))))
7520 : {
7521 5742 : comp = force_gimple_operand (comp, &seq, true, NULL);
7522 5742 : gimple_seq_add_seq (&stmt_list, seq);
7523 5742 : if (POINTER_TYPE_P (TREE_TYPE (tgt)))
7524 : {
7525 0 : duplicate_ssa_name_ptr_info (comp, SSA_NAME_PTR_INFO (tgt));
7526 : /* As this isn't a plain copy we have to reset alignment
7527 : information. */
7528 0 : if (SSA_NAME_PTR_INFO (comp))
7529 0 : mark_ptr_info_alignment_unknown (SSA_NAME_PTR_INFO (comp));
7530 : }
7531 : }
7532 :
7533 339839 : gsi_insert_seq_before (&bsi, stmt_list, GSI_SAME_STMT);
7534 339839 : if (gimple_code (use->stmt) == GIMPLE_PHI)
7535 : {
7536 41444 : ass = gimple_build_assign (tgt, comp);
7537 41444 : gsi_insert_before (&bsi, ass, GSI_SAME_STMT);
7538 :
7539 41444 : bsi = gsi_for_stmt (use->stmt);
7540 41444 : remove_phi_node (&bsi, false);
7541 : }
7542 : else
7543 : {
7544 298395 : gimple_assign_set_rhs_from_tree (&bsi, comp);
7545 298395 : use->stmt = gsi_stmt (bsi);
7546 : }
7547 : }
7548 :
7549 : /* Performs a peephole optimization to reorder the iv update statement with
7550 : a mem ref to enable instruction combining in later phases. The mem ref uses
7551 : the iv value before the update, so the reordering transformation requires
7552 : adjustment of the offset. CAND is the selected IV_CAND.
7553 :
7554 : Example:
7555 :
7556 : t = MEM_REF (base, iv1, 8, 16); // base, index, stride, offset
7557 : iv2 = iv1 + 1;
7558 :
7559 : if (t < val) (1)
7560 : goto L;
7561 : goto Head;
7562 :
7563 :
7564 : directly propagating t over to (1) will introduce overlapping live range
7565 : thus increase register pressure. This peephole transform it into:
7566 :
7567 :
7568 : iv2 = iv1 + 1;
7569 : t = MEM_REF (base, iv2, 8, 8);
7570 : if (t < val)
7571 : goto L;
7572 : goto Head;
7573 : */
7574 :
7575 : static void
7576 865786 : adjust_iv_update_pos (struct iv_cand *cand, struct iv_use *use)
7577 : {
7578 865786 : tree var_after;
7579 865786 : gimple *iv_update, *stmt;
7580 865786 : basic_block bb;
7581 865786 : gimple_stmt_iterator gsi, gsi_iv;
7582 :
7583 865786 : if (cand->pos != IP_NORMAL)
7584 863530 : return;
7585 :
7586 662796 : var_after = cand->var_after;
7587 662796 : iv_update = SSA_NAME_DEF_STMT (var_after);
7588 :
7589 662796 : bb = gimple_bb (iv_update);
7590 662796 : gsi = gsi_last_nondebug_bb (bb);
7591 662796 : stmt = gsi_stmt (gsi);
7592 :
7593 : /* Only handle conditional statement for now. */
7594 662796 : if (gimple_code (stmt) != GIMPLE_COND)
7595 : return;
7596 :
7597 662796 : gsi_prev_nondebug (&gsi);
7598 662796 : stmt = gsi_stmt (gsi);
7599 662796 : if (stmt != iv_update)
7600 : return;
7601 :
7602 526614 : gsi_prev_nondebug (&gsi);
7603 526614 : if (gsi_end_p (gsi))
7604 : return;
7605 :
7606 523893 : stmt = gsi_stmt (gsi);
7607 523893 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
7608 : return;
7609 :
7610 523757 : if (stmt != use->stmt)
7611 : return;
7612 :
7613 4316 : if (TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
7614 : return;
7615 :
7616 2256 : if (dump_file && (dump_flags & TDF_DETAILS))
7617 : {
7618 0 : fprintf (dump_file, "Reordering \n");
7619 0 : print_gimple_stmt (dump_file, iv_update, 0);
7620 0 : print_gimple_stmt (dump_file, use->stmt, 0);
7621 0 : fprintf (dump_file, "\n");
7622 : }
7623 :
7624 2256 : gsi = gsi_for_stmt (use->stmt);
7625 2256 : gsi_iv = gsi_for_stmt (iv_update);
7626 2256 : gsi_move_before (&gsi_iv, &gsi);
7627 :
7628 2256 : cand->pos = IP_BEFORE_USE;
7629 2256 : cand->incremented_at = use->stmt;
7630 : }
7631 :
7632 : /* Return the alias pointer type that should be used for a MEM_REF
7633 : associated with USE, which has type USE_PTR_ADDRESS. */
7634 :
7635 : static tree
7636 637 : get_alias_ptr_type_for_ptr_address (iv_use *use)
7637 : {
7638 637 : gcall *call = as_a <gcall *> (use->stmt);
7639 637 : switch (gimple_call_internal_fn (call))
7640 : {
7641 637 : case IFN_MASK_LOAD:
7642 637 : case IFN_MASK_STORE:
7643 637 : case IFN_MASK_LOAD_LANES:
7644 637 : case IFN_MASK_STORE_LANES:
7645 637 : case IFN_MASK_LEN_LOAD_LANES:
7646 637 : case IFN_MASK_LEN_STORE_LANES:
7647 637 : case IFN_LEN_LOAD:
7648 637 : case IFN_LEN_STORE:
7649 637 : case IFN_MASK_LEN_LOAD:
7650 637 : case IFN_MASK_LEN_STORE:
7651 : /* The second argument contains the correct alias type. */
7652 637 : gcc_assert (use->op_p == gimple_call_arg_ptr (call, 0));
7653 637 : return TREE_TYPE (gimple_call_arg (call, 1));
7654 :
7655 0 : default:
7656 0 : gcc_unreachable ();
7657 : }
7658 : }
7659 :
7660 :
7661 : /* Rewrites USE (address that is an iv) using candidate CAND. */
7662 :
7663 : static void
7664 865786 : rewrite_use_address (struct ivopts_data *data,
7665 : struct iv_use *use, struct iv_cand *cand)
7666 : {
7667 865786 : aff_tree aff;
7668 865786 : bool ok;
7669 :
7670 865786 : adjust_iv_update_pos (cand, use);
7671 865786 : ok = get_computation_aff (data, use->stmt, use, cand, &aff);
7672 865786 : gcc_assert (ok);
7673 865786 : unshare_aff_combination (&aff);
7674 :
7675 : /* To avoid undefined overflow problems, all IV candidates use unsigned
7676 : integer types. The drawback is that this makes it impossible for
7677 : create_mem_ref to distinguish an IV that is based on a memory object
7678 : from one that represents simply an offset.
7679 :
7680 : To work around this problem, we pass a hint to create_mem_ref that
7681 : indicates which variable (if any) in aff is an IV based on a memory
7682 : object. Note that we only consider the candidate. If this is not
7683 : based on an object, the base of the reference is in some subexpression
7684 : of the use -- but these will use pointer types, so they are recognized
7685 : by the create_mem_ref heuristics anyway. */
7686 865786 : tree iv = var_at_stmt (data->current_loop, cand, use->stmt);
7687 865786 : tree base_hint = (cand->iv->base_object) ? iv : NULL_TREE;
7688 865786 : gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
7689 865786 : tree type = use->mem_type;
7690 865786 : tree alias_ptr_type;
7691 865786 : if (use->type == USE_PTR_ADDRESS)
7692 637 : alias_ptr_type = get_alias_ptr_type_for_ptr_address (use);
7693 : else
7694 : {
7695 865149 : gcc_assert (type == TREE_TYPE (*use->op_p));
7696 865149 : unsigned int align = get_object_alignment (*use->op_p);
7697 865149 : if (align != TYPE_ALIGN (type))
7698 34351 : type = build_aligned_type (type, align);
7699 865149 : alias_ptr_type = reference_alias_ptr_type (*use->op_p);
7700 : }
7701 865786 : tree ref = create_mem_ref (&bsi, type, &aff, alias_ptr_type,
7702 : iv, base_hint, data->speed);
7703 :
7704 865786 : if (use->type == USE_PTR_ADDRESS)
7705 : {
7706 637 : ref = fold_build1 (ADDR_EXPR, build_pointer_type (use->mem_type), ref);
7707 637 : ref = fold_convert (get_use_type (use), ref);
7708 637 : ref = force_gimple_operand_gsi (&bsi, ref, true, NULL_TREE,
7709 : true, GSI_SAME_STMT);
7710 : }
7711 : else
7712 : {
7713 : /* When we end up confused enough and have no suitable base but
7714 : stuffed everything to indexes use a LEA for the address and
7715 : create a plain MEM_REF to avoid basing a memory reference
7716 : on address zero which create_mem_ref_raw does as fallback. */
7717 865149 : if (TREE_CODE (ref) == TARGET_MEM_REF
7718 865149 : && integer_zerop (TREE_OPERAND (ref, 0)))
7719 : {
7720 23 : ref = fold_build1 (ADDR_EXPR, TREE_TYPE (TREE_OPERAND (ref, 0)), ref);
7721 23 : ref = force_gimple_operand_gsi (&bsi, ref, true, NULL_TREE,
7722 : true, GSI_SAME_STMT);
7723 23 : ref = build2 (MEM_REF, type, ref, build_zero_cst (alias_ptr_type));
7724 : }
7725 865149 : copy_ref_info (ref, *use->op_p);
7726 : }
7727 :
7728 865786 : *use->op_p = ref;
7729 865786 : }
7730 :
7731 : /* Rewrites USE (the condition such that one of the arguments is an iv) using
7732 : candidate CAND. */
7733 :
7734 : static void
7735 604382 : rewrite_use_compare (struct ivopts_data *data,
7736 : struct iv_use *use, struct iv_cand *cand)
7737 : {
7738 604382 : tree comp, op, bound;
7739 604382 : gimple_stmt_iterator bsi = gsi_for_stmt (use->stmt);
7740 604382 : enum tree_code compare;
7741 604382 : struct iv_group *group = data->vgroups[use->group_id];
7742 604382 : class cost_pair *cp = get_group_iv_cost (data, group, cand);
7743 :
7744 604382 : bound = cp->value;
7745 604382 : if (bound)
7746 : {
7747 396649 : tree var = var_at_stmt (data->current_loop, cand, use->stmt);
7748 396649 : tree var_type = TREE_TYPE (var);
7749 396649 : gimple_seq stmts;
7750 :
7751 396649 : if (dump_file && (dump_flags & TDF_DETAILS))
7752 : {
7753 56 : fprintf (dump_file, "Replacing exit test: ");
7754 56 : print_gimple_stmt (dump_file, use->stmt, 0, TDF_SLIM);
7755 : }
7756 396649 : compare = cp->comp;
7757 396649 : bound = unshare_expr (fold_convert (var_type, bound));
7758 396649 : op = force_gimple_operand (bound, &stmts, true, NULL_TREE);
7759 396649 : if (stmts)
7760 186084 : gsi_insert_seq_on_edge_immediate (
7761 186084 : loop_preheader_edge (data->current_loop),
7762 : stmts);
7763 :
7764 396649 : gcond *cond_stmt = as_a <gcond *> (use->stmt);
7765 396649 : gimple_cond_set_lhs (cond_stmt, var);
7766 396649 : gimple_cond_set_code (cond_stmt, compare);
7767 396649 : gimple_cond_set_rhs (cond_stmt, op);
7768 396649 : return;
7769 : }
7770 :
7771 : /* The induction variable elimination failed; just express the original
7772 : giv. */
7773 207733 : comp = get_computation_at (data, use->stmt, use, cand);
7774 207733 : gcc_assert (comp != NULL_TREE);
7775 207733 : gcc_assert (use->op_p != NULL);
7776 207733 : *use->op_p = force_gimple_operand_gsi (&bsi, comp, true,
7777 207733 : SSA_NAME_VAR (*use->op_p),
7778 : true, GSI_SAME_STMT);
7779 : }
7780 :
7781 : /* Rewrite the groups using the selected induction variables. */
7782 :
7783 : static void
7784 507253 : rewrite_groups (struct ivopts_data *data)
7785 : {
7786 507253 : unsigned i, j;
7787 :
7788 2320391 : for (i = 0; i < data->vgroups.length (); i++)
7789 : {
7790 1813138 : struct iv_group *group = data->vgroups[i];
7791 1813138 : struct iv_cand *cand = group->selected;
7792 :
7793 1813138 : gcc_assert (cand);
7794 :
7795 1813138 : if (group->type == USE_NONLINEAR_EXPR)
7796 : {
7797 1250322 : for (j = 0; j < group->vuses.length (); j++)
7798 : {
7799 625161 : rewrite_use_nonlinear_expr (data, group->vuses[j], cand);
7800 625161 : update_stmt (group->vuses[j]->stmt);
7801 : }
7802 : }
7803 1187977 : else if (address_p (group->type))
7804 : {
7805 1449381 : for (j = 0; j < group->vuses.length (); j++)
7806 : {
7807 865786 : rewrite_use_address (data, group->vuses[j], cand);
7808 865786 : update_stmt (group->vuses[j]->stmt);
7809 : }
7810 : }
7811 : else
7812 : {
7813 604382 : gcc_assert (group->type == USE_COMPARE);
7814 :
7815 2417520 : for (j = 0; j < group->vuses.length (); j++)
7816 : {
7817 604382 : rewrite_use_compare (data, group->vuses[j], cand);
7818 604382 : update_stmt (group->vuses[j]->stmt);
7819 : }
7820 : }
7821 : }
7822 507253 : }
7823 :
7824 : /* Removes the ivs that are not used after rewriting. */
7825 :
7826 : static void
7827 507253 : remove_unused_ivs (struct ivopts_data *data, bitmap toremove)
7828 : {
7829 507253 : unsigned j;
7830 507253 : bitmap_iterator bi;
7831 :
7832 : /* Figure out an order in which to release SSA DEFs so that we don't
7833 : release something that we'd have to propagate into a debug stmt
7834 : afterwards. */
7835 5607993 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, j, bi)
7836 : {
7837 5100740 : struct version_info *info;
7838 :
7839 5100740 : info = ver_info (data, j);
7840 5100740 : if (info->iv
7841 4956329 : && !integer_zerop (info->iv->step)
7842 3242394 : && !info->inv_id
7843 3242394 : && !info->iv->nonlin_use
7844 7717973 : && !info->preserve_biv)
7845 : {
7846 2500286 : bitmap_set_bit (toremove, SSA_NAME_VERSION (info->iv->ssa_name));
7847 :
7848 2500286 : tree def = info->iv->ssa_name;
7849 :
7850 3255282 : if (MAY_HAVE_DEBUG_BIND_STMTS && SSA_NAME_DEF_STMT (def))
7851 : {
7852 754996 : imm_use_iterator imm_iter;
7853 754996 : use_operand_p use_p;
7854 754996 : gimple *stmt;
7855 754996 : int count = 0;
7856 :
7857 1484440 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
7858 : {
7859 758594 : if (!gimple_debug_bind_p (stmt))
7860 637768 : continue;
7861 :
7862 : /* We just want to determine whether to do nothing
7863 : (count == 0), to substitute the computed
7864 : expression into a single use of the SSA DEF by
7865 : itself (count == 1), or to use a debug temp
7866 : because the SSA DEF is used multiple times or as
7867 : part of a larger expression (count > 1). */
7868 120826 : count++;
7869 120826 : if (gimple_debug_bind_get_value (stmt) != def)
7870 7945 : count++;
7871 :
7872 120826 : if (count > 1)
7873 : break;
7874 754996 : }
7875 :
7876 754996 : if (!count)
7877 680867 : continue;
7878 :
7879 99392 : struct iv_use dummy_use;
7880 99392 : struct iv_cand *best_cand = NULL, *cand;
7881 99392 : unsigned i, best_pref = 0, cand_pref;
7882 99392 : tree comp = NULL_TREE;
7883 :
7884 99392 : memset (&dummy_use, 0, sizeof (dummy_use));
7885 99392 : dummy_use.iv = info->iv;
7886 503690 : for (i = 0; i < data->vgroups.length () && i < 64; i++)
7887 : {
7888 404298 : cand = data->vgroups[i]->selected;
7889 404298 : if (cand == best_cand)
7890 155257 : continue;
7891 171849 : cand_pref = operand_equal_p (cand->iv->step,
7892 249041 : info->iv->step, 0)
7893 249041 : ? 4 : 0;
7894 249041 : cand_pref
7895 249041 : += TYPE_MODE (TREE_TYPE (cand->iv->base))
7896 249041 : == TYPE_MODE (TREE_TYPE (info->iv->base))
7897 249041 : ? 2 : 0;
7898 249041 : cand_pref
7899 498082 : += TREE_CODE (cand->iv->base) == INTEGER_CST
7900 249041 : ? 1 : 0;
7901 249041 : if (best_cand == NULL || best_pref < cand_pref)
7902 : {
7903 196745 : tree this_comp
7904 393490 : = get_debug_computation_at (data,
7905 196745 : SSA_NAME_DEF_STMT (def),
7906 : &dummy_use, cand);
7907 196745 : if (this_comp)
7908 : {
7909 404298 : best_cand = cand;
7910 404298 : best_pref = cand_pref;
7911 404298 : comp = this_comp;
7912 : }
7913 : }
7914 : }
7915 :
7916 99392 : if (!best_cand)
7917 25263 : continue;
7918 :
7919 74129 : comp = unshare_expr (comp);
7920 74129 : if (count > 1)
7921 : {
7922 24128 : tree vexpr = build_debug_expr_decl (TREE_TYPE (comp));
7923 : /* FIXME: Is setting the mode really necessary? */
7924 24128 : if (SSA_NAME_VAR (def))
7925 13350 : SET_DECL_MODE (vexpr, DECL_MODE (SSA_NAME_VAR (def)));
7926 : else
7927 10778 : SET_DECL_MODE (vexpr, TYPE_MODE (TREE_TYPE (vexpr)));
7928 24128 : gdebug *def_temp
7929 24128 : = gimple_build_debug_bind (vexpr, comp, NULL);
7930 24128 : gimple_stmt_iterator gsi;
7931 :
7932 24128 : if (gimple_code (SSA_NAME_DEF_STMT (def)) == GIMPLE_PHI)
7933 13939 : gsi = gsi_after_labels (gimple_bb
7934 13939 : (SSA_NAME_DEF_STMT (def)));
7935 : else
7936 10189 : gsi = gsi_for_stmt (SSA_NAME_DEF_STMT (def));
7937 :
7938 24128 : gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
7939 24128 : comp = vexpr;
7940 : }
7941 :
7942 283012 : FOR_EACH_IMM_USE_STMT (stmt, imm_iter, def)
7943 : {
7944 208883 : if (!gimple_debug_bind_p (stmt))
7945 83233 : continue;
7946 :
7947 251356 : FOR_EACH_IMM_USE_ON_STMT (use_p, imm_iter)
7948 125678 : SET_USE (use_p, comp);
7949 :
7950 125650 : update_stmt (stmt);
7951 74129 : }
7952 : }
7953 : }
7954 : }
7955 507253 : }
7956 :
7957 : /* Frees memory occupied by class tree_niter_desc in *VALUE. Callback
7958 : for hash_map::traverse. */
7959 :
7960 : bool
7961 489139 : free_tree_niter_desc (edge const &, tree_niter_desc *const &value, void *)
7962 : {
7963 489139 : if (value)
7964 : {
7965 448044 : value->~tree_niter_desc ();
7966 448044 : free (value);
7967 : }
7968 489139 : return true;
7969 : }
7970 :
7971 : /* Frees data allocated by the optimization of a single loop. */
7972 :
7973 : static void
7974 881470 : free_loop_data (struct ivopts_data *data)
7975 : {
7976 881470 : unsigned i, j;
7977 881470 : bitmap_iterator bi;
7978 881470 : tree obj;
7979 :
7980 881470 : if (data->niters)
7981 : {
7982 966169 : data->niters->traverse<void *, free_tree_niter_desc> (NULL);
7983 954060 : delete data->niters;
7984 477030 : data->niters = NULL;
7985 : }
7986 :
7987 5996273 : EXECUTE_IF_SET_IN_BITMAP (data->relevant, 0, i, bi)
7988 : {
7989 5114803 : struct version_info *info;
7990 :
7991 5114803 : info = ver_info (data, i);
7992 5114803 : info->iv = NULL;
7993 5114803 : info->has_nonlin_use = false;
7994 5114803 : info->preserve_biv = false;
7995 5114803 : info->inv_id = 0;
7996 : }
7997 881470 : bitmap_clear (data->relevant);
7998 881470 : bitmap_clear (data->important_candidates);
7999 :
8000 2696695 : for (i = 0; i < data->vgroups.length (); i++)
8001 : {
8002 1815225 : struct iv_group *group = data->vgroups[i];
8003 :
8004 3912680 : for (j = 0; j < group->vuses.length (); j++)
8005 2097455 : free (group->vuses[j]);
8006 1815225 : group->vuses.release ();
8007 :
8008 1815225 : BITMAP_FREE (group->related_cands);
8009 19713127 : for (j = 0; j < group->n_map_members; j++)
8010 : {
8011 17897902 : if (group->cost_map[j].inv_vars)
8012 3676389 : BITMAP_FREE (group->cost_map[j].inv_vars);
8013 17897902 : if (group->cost_map[j].inv_exprs)
8014 2020758 : BITMAP_FREE (group->cost_map[j].inv_exprs);
8015 : }
8016 :
8017 1815225 : free (group->cost_map);
8018 1815225 : free (group);
8019 : }
8020 881470 : data->vgroups.truncate (0);
8021 :
8022 5531592 : for (i = 0; i < data->vcands.length (); i++)
8023 : {
8024 4650122 : struct iv_cand *cand = data->vcands[i];
8025 :
8026 4650122 : if (cand->inv_vars)
8027 73715 : BITMAP_FREE (cand->inv_vars);
8028 4650122 : if (cand->inv_exprs)
8029 93302 : BITMAP_FREE (cand->inv_exprs);
8030 4650122 : free (cand);
8031 : }
8032 881470 : data->vcands.truncate (0);
8033 :
8034 881470 : if (data->version_info_size < num_ssa_names)
8035 : {
8036 175 : data->version_info_size = 2 * num_ssa_names;
8037 175 : free (data->version_info);
8038 175 : data->version_info = XCNEWVEC (struct version_info, data->version_info_size);
8039 : }
8040 :
8041 881470 : data->max_inv_var_id = 0;
8042 881470 : data->max_inv_expr_id = 0;
8043 :
8044 881470 : FOR_EACH_VEC_ELT (decl_rtl_to_reset, i, obj)
8045 0 : SET_DECL_RTL (obj, NULL_RTX);
8046 :
8047 881470 : decl_rtl_to_reset.truncate (0);
8048 :
8049 881470 : data->inv_expr_tab->empty ();
8050 :
8051 881470 : data->iv_common_cand_tab->empty ();
8052 881470 : data->iv_common_cands.truncate (0);
8053 881470 : }
8054 :
8055 : /* Finalizes data structures used by the iv optimization pass. LOOPS is the
8056 : loop tree. */
8057 :
8058 : static void
8059 245627 : tree_ssa_iv_optimize_finalize (struct ivopts_data *data)
8060 : {
8061 245627 : free_loop_data (data);
8062 245627 : free (data->version_info);
8063 245627 : BITMAP_FREE (data->relevant);
8064 245627 : BITMAP_FREE (data->important_candidates);
8065 :
8066 245627 : decl_rtl_to_reset.release ();
8067 245627 : data->vgroups.release ();
8068 245627 : data->vcands.release ();
8069 245627 : delete data->inv_expr_tab;
8070 245627 : data->inv_expr_tab = NULL;
8071 245627 : free_affine_expand_cache (&data->name_expansion_cache);
8072 245627 : if (data->base_object_map)
8073 165833 : delete data->base_object_map;
8074 245627 : delete data->iv_common_cand_tab;
8075 245627 : data->iv_common_cand_tab = NULL;
8076 245627 : data->iv_common_cands.release ();
8077 245627 : obstack_free (&data->iv_obstack, NULL);
8078 245627 : }
8079 :
8080 : /* Returns true if the loop body BODY includes any function calls. */
8081 :
8082 : static bool
8083 635843 : loop_body_includes_call (basic_block *body, unsigned num_nodes)
8084 : {
8085 635843 : gimple_stmt_iterator gsi;
8086 635843 : unsigned i;
8087 :
8088 2854223 : for (i = 0; i < num_nodes; i++)
8089 24386149 : for (gsi = gsi_start_bb (body[i]); !gsi_end_p (gsi); gsi_next (&gsi))
8090 : {
8091 19736820 : gimple *stmt = gsi_stmt (gsi);
8092 19736820 : if (is_gimple_call (stmt)
8093 284475 : && !gimple_call_internal_p (stmt)
8094 19955845 : && !is_inexpensive_builtin (gimple_call_fndecl (stmt)))
8095 : return true;
8096 : }
8097 : return false;
8098 : }
8099 :
8100 : /* Determine cost scaling factor for basic blocks in loop. */
8101 : #define COST_SCALING_FACTOR_BOUND (20)
8102 :
8103 : static void
8104 507705 : determine_scaling_factor (struct ivopts_data *data, basic_block *body)
8105 : {
8106 507705 : int lfreq = data->current_loop->header->count.to_frequency (cfun);
8107 507705 : if (!data->speed || lfreq <= 0)
8108 : return;
8109 :
8110 : int max_freq = lfreq;
8111 2916160 : for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
8112 : {
8113 2496188 : body[i]->aux = (void *)(intptr_t) 1;
8114 2496188 : if (max_freq < body[i]->count.to_frequency (cfun))
8115 103228 : max_freq = body[i]->count.to_frequency (cfun);
8116 : }
8117 419972 : if (max_freq > lfreq)
8118 : {
8119 65666 : int divisor, factor;
8120 : /* Check if scaling factor itself needs to be scaled by the bound. This
8121 : is to avoid overflow when scaling cost according to profile info. */
8122 65666 : if (max_freq / lfreq > COST_SCALING_FACTOR_BOUND)
8123 : {
8124 : divisor = max_freq;
8125 : factor = COST_SCALING_FACTOR_BOUND;
8126 : }
8127 : else
8128 : {
8129 49673 : divisor = lfreq;
8130 49673 : factor = 1;
8131 : }
8132 1005446 : for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
8133 : {
8134 939780 : int bfreq = body[i]->count.to_frequency (cfun);
8135 939780 : if (bfreq <= lfreq)
8136 518610 : continue;
8137 :
8138 421170 : body[i]->aux = (void*)(intptr_t) (factor * bfreq / divisor);
8139 : }
8140 : }
8141 : }
8142 :
8143 : /* Find doloop comparison use and set its doloop_p on if found. */
8144 :
8145 : static bool
8146 0 : find_doloop_use (struct ivopts_data *data)
8147 : {
8148 0 : struct loop *loop = data->current_loop;
8149 :
8150 0 : for (unsigned i = 0; i < data->vgroups.length (); i++)
8151 : {
8152 0 : struct iv_group *group = data->vgroups[i];
8153 0 : if (group->type == USE_COMPARE)
8154 : {
8155 0 : gcc_assert (group->vuses.length () == 1);
8156 0 : struct iv_use *use = group->vuses[0];
8157 0 : gimple *stmt = use->stmt;
8158 0 : if (gimple_code (stmt) == GIMPLE_COND)
8159 : {
8160 0 : basic_block bb = gimple_bb (stmt);
8161 0 : edge true_edge, false_edge;
8162 0 : extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
8163 : /* This comparison is used for loop latch. Require latch is empty
8164 : for now. */
8165 0 : if ((loop->latch == true_edge->dest
8166 0 : || loop->latch == false_edge->dest)
8167 0 : && empty_block_p (loop->latch))
8168 : {
8169 0 : group->doloop_p = true;
8170 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8171 : {
8172 0 : fprintf (dump_file, "Doloop cmp iv use: ");
8173 0 : print_gimple_stmt (dump_file, stmt, TDF_DETAILS);
8174 : }
8175 0 : return true;
8176 : }
8177 : }
8178 : }
8179 : }
8180 :
8181 : return false;
8182 : }
8183 :
8184 : /* For the targets which support doloop, to predict whether later RTL doloop
8185 : transformation will perform on this loop, further detect the doloop use and
8186 : mark the flag doloop_use_p if predicted. */
8187 :
8188 : void
8189 507705 : analyze_and_mark_doloop_use (struct ivopts_data *data)
8190 : {
8191 507705 : data->doloop_use_p = false;
8192 :
8193 507705 : if (!flag_branch_on_count_reg)
8194 : return;
8195 :
8196 507705 : if (data->current_loop->unroll == USHRT_MAX)
8197 : return;
8198 :
8199 507705 : if (!generic_predict_doloop_p (data))
8200 : return;
8201 :
8202 0 : if (find_doloop_use (data))
8203 : {
8204 0 : data->doloop_use_p = true;
8205 0 : if (dump_file && (dump_flags & TDF_DETAILS))
8206 : {
8207 0 : struct loop *loop = data->current_loop;
8208 0 : fprintf (dump_file,
8209 : "Predict loop %d can perform"
8210 : " doloop optimization later.\n",
8211 : loop->num);
8212 0 : flow_loop_dump (loop, dump_file, NULL, 1);
8213 : }
8214 : }
8215 : }
8216 :
8217 : /* Optimizes the LOOP. Returns true if anything changed. */
8218 :
8219 : static bool
8220 635843 : tree_ssa_iv_optimize_loop (struct ivopts_data *data, class loop *loop,
8221 : bitmap toremove)
8222 : {
8223 635843 : bool changed = false;
8224 635843 : class iv_ca *iv_ca;
8225 635843 : edge exit = single_dom_exit (loop);
8226 635843 : basic_block *body;
8227 :
8228 635843 : gcc_assert (!data->niters);
8229 635843 : data->current_loop = loop;
8230 635843 : data->loop_loc = find_loop_location (loop).get_location_t ();
8231 635843 : data->speed = optimize_loop_for_speed_p (loop);
8232 :
8233 635843 : if (dump_file && (dump_flags & TDF_DETAILS))
8234 : {
8235 67 : fprintf (dump_file, "Processing loop %d", loop->num);
8236 67 : if (data->loop_loc != UNKNOWN_LOCATION)
8237 65 : fprintf (dump_file, " at %s:%d", LOCATION_FILE (data->loop_loc),
8238 130 : LOCATION_LINE (data->loop_loc));
8239 67 : fprintf (dump_file, "\n");
8240 :
8241 67 : if (exit)
8242 : {
8243 57 : fprintf (dump_file, " single exit %d -> %d, exit condition ",
8244 57 : exit->src->index, exit->dest->index);
8245 114 : print_gimple_stmt (dump_file, *gsi_last_bb (exit->src),
8246 : 0, TDF_SLIM);
8247 57 : fprintf (dump_file, "\n");
8248 : }
8249 :
8250 67 : fprintf (dump_file, "\n");
8251 : }
8252 :
8253 635843 : body = get_loop_body (loop);
8254 635843 : data->body_includes_call = loop_body_includes_call (body, loop->num_nodes);
8255 635843 : renumber_gimple_stmt_uids_in_blocks (body, loop->num_nodes);
8256 :
8257 635843 : data->loop_single_exit_p
8258 635843 : = exit != NULL && loop_only_exit_p (loop, body, exit);
8259 :
8260 : /* For each ssa name determines whether it behaves as an induction variable
8261 : in some loop. */
8262 635843 : if (!find_induction_variables (data, body))
8263 128137 : goto finish;
8264 :
8265 : /* Finds interesting uses (item 1). */
8266 507706 : find_interesting_uses (data, body);
8267 507706 : if (data->vgroups.length () > MAX_CONSIDERED_GROUPS)
8268 1 : goto finish;
8269 :
8270 : /* Determine cost scaling factor for basic blocks in loop. */
8271 507705 : determine_scaling_factor (data, body);
8272 :
8273 : /* Analyze doloop possibility and mark the doloop use if predicted. */
8274 507705 : analyze_and_mark_doloop_use (data);
8275 :
8276 : /* Finds candidates for the induction variables (item 2). */
8277 507705 : find_iv_candidates (data);
8278 :
8279 : /* Calculates the costs (item 3, part 1). */
8280 507705 : determine_iv_costs (data);
8281 507705 : determine_group_iv_costs (data);
8282 507705 : determine_set_costs (data);
8283 :
8284 : /* Find the optimal set of induction variables (item 3, part 2). */
8285 507705 : iv_ca = find_optimal_iv_set (data);
8286 : /* Cleanup basic block aux field. */
8287 3364181 : for (unsigned i = 0; i < data->current_loop->num_nodes; i++)
8288 2856476 : body[i]->aux = NULL;
8289 507705 : if (!iv_ca)
8290 452 : goto finish;
8291 507253 : changed = true;
8292 :
8293 : /* Create the new induction variables (item 4, part 1). */
8294 507253 : create_new_ivs (data, iv_ca);
8295 507253 : iv_ca_free (&iv_ca);
8296 :
8297 : /* Rewrite the uses (item 4, part 2). */
8298 507253 : rewrite_groups (data);
8299 :
8300 : /* Remove the ivs that are unused after rewriting. */
8301 507253 : remove_unused_ivs (data, toremove);
8302 :
8303 635843 : finish:
8304 635843 : free (body);
8305 635843 : free_loop_data (data);
8306 :
8307 635843 : return changed;
8308 : }
8309 :
8310 : /* Main entry point. Optimizes induction variables in loops. */
8311 :
8312 : void
8313 245627 : tree_ssa_iv_optimize (void)
8314 : {
8315 245627 : struct ivopts_data data;
8316 245627 : auto_bitmap toremove;
8317 :
8318 245627 : tree_ssa_iv_optimize_init (&data);
8319 245627 : mark_ssa_maybe_undefs ();
8320 :
8321 : /* Optimize the loops starting with the innermost ones. */
8322 1372724 : for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
8323 : {
8324 635843 : if (!dbg_cnt (ivopts_loop))
8325 0 : continue;
8326 :
8327 635843 : if (dump_file && (dump_flags & TDF_DETAILS))
8328 67 : flow_loop_dump (loop, dump_file, NULL, 1);
8329 :
8330 635843 : tree_ssa_iv_optimize_loop (&data, loop, toremove);
8331 245627 : }
8332 :
8333 : /* Remove eliminated IV defs. */
8334 245627 : release_defs_bitset (toremove);
8335 :
8336 : /* We have changed the structure of induction variables; it might happen
8337 : that definitions in the scev database refer to some of them that were
8338 : eliminated. */
8339 245627 : scev_reset_htab ();
8340 : /* Likewise niter and control-IV information. */
8341 245627 : free_numbers_of_iterations_estimates (cfun);
8342 :
8343 245627 : tree_ssa_iv_optimize_finalize (&data);
8344 245627 : }
8345 :
8346 : #include "gt-tree-ssa-loop-ivopts.h"
|