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