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