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